Skip to content

Commit ef89585

Browse files
wshallwshallwshallwshallclaude
authored
fix(scripts): anchor webconsole_seam_snapshot.py on its own repo, not on sys.path (BACKLOG #1439) (#826)
* fix(scripts): anchor webconsole_seam_snapshot.py on its own repo, not on sys.path (BACKLOG #1439) The generator derived the seam digest from whichever `messagefoundry` sys.path happened to offer, while reading _CONSOLE_DIR and _ENGINE_DIR out of the repository it lives in. Run from a git worktree with no .venv of its own -- the normal state for these sessions -- those are two different trees. Python puts the SCRIPT's directory on sys.path[0], never the caller's cwd, so `scripts/` led the path and the import fell through to site-packages: the primary checkout's, via its path-based editable install (_editable_impl_messagefoundry.pth holds the primary root). The failure was silent and it pointed the wrong way. Measured 2026-09-03 in a worktree with no .venv of its own: `--write` printed the primary tree's digest 93ba1f10b9dccfc8, rewrote both files with that unchanged value, and reported success naming both, while tests/test_webconsole_seam_snapshot.py kept failing against 266cbfd342b22819 -- a digest the script had never computed. The only loud word was "rewrote", so the repair on offer was to a gate that was right. This is the SDS-3.8 shape: an instrument answering an adjacent question. The fix is the sys.path insert three sibling scripts already carry, above the `from messagefoundry...` imports, citing the rule scripts/coord/alloc.ps1 states for `git` (#1060): anchor on the script, not on the caller. Two tests, because the obvious one is not enough: test_the_script_run_by_path_computes_the_same_digest runs the generator as a subprocess, by path, from a cwd that is not the repo root and with PYTHONPATH scrubbed. Both scrubs are load-bearing: under pytest the root is already on sys.path, so every in-process check in that file is blind to this defect by construction. test_the_script_prefers_its_own_repo_over_an_earlier_path_entry supplies a second engine tree itself -- a decoy messagefoundry package on PYTHONPATH, which for a by-path invocation sits ahead of site-packages and behind an explicit sys.path.insert(0, repo_root). It reds if and only if the anchor is gone, on any machine. The first test cannot carry that weight: one tree on a hosted runner means an unanchored script is right by luck, and measured here it stayed green with the anchor deleted, because this box's two trees currently carry the same seam. Census of scripts/ for the same class: six .py files import an in-repo top-level package. Three already anchor on __file__ (bench/stage_residency.py, security/dast_auth_sweep.py, tray/make_icons.py). Two are libraries with no __main__ reached only as scripts.security.X, so they inherit a corrected path and are not exposed (security/dast_target.py, security/route_gates.py). This was the only one. Verification: 8 passed in tests/test_webconsole_seam_snapshot.py. Mutation check run rather than argued -- anchor deleted, the decoy test reds naming the decoy import; anchor restored, 8 passed. ruff check and ruff format --check clean on both files; the 4 mypy findings in the test file are pre-existing (identical at HEAD) and tests/ is outside CI's mypy scope. The seam constant and the golden are deliberately untouched: the contract did not move. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * docs(backlog): key #1439's census on the missing anchor, not on observed resolution Three peer sessions raised the same methodological point independently: a script can resolve to the correct tree today for a reason that has nothing to do with the script, so a census keyed on "which tree did it resolve to" clears every script whose cwd currently rescues it. Verified here, one interpreter, one command, three working directories. From a worktree root `python -c "import messagefoundry"` resolves to the WORKTREE, because cwd is sys.path[0] and precedes the editable install. From the primary root or a neutral cwd the same command resolves to the primary. A by-path script resolves to the primary from everywhere. That is this defect's mirror image: a by-path script reads the primary because nothing anchors it, a bare interpreter reads the worktree because cwd happens to win, and both are the same missing anchor. Only one of them looks wrong. The fleet's recipe for a .venv-less worktree depends entirely on that rescue. The census table was already keyed on the anchor rather than on resolution, but the item did not say so, and a future reader re-running it could reasonably key it the other way. It says so now. Also re-derived the table by AST on the predicate "imports an in-repo package AND is runnable by path AND puts nothing on sys.path". It agreed with the reading exactly. Positive control, since a pattern that finds nothing anywhere is indistinguishable from a clean tree: 53 .py files under scripts/, 28 deriving a path from __file__, 6 importing an in-repo package. Control on the instrument itself: against the parent commit the census flags webconsole_seam_snapshot.py as EXPOSED and nothing else; against the fix it reports none. Two further corrections to the record: The two library modules are structurally safe, not accidentally safe. The scripts.security.X import form cannot resolve unless the repo root is already importable, so the root supplying the module necessarily supplies its messagefoundry. One tree by construction, not by cwd. Added what the census could NOT see: its scope is scripts/**/*.py only, its runnability test is a __main__ guard which would misjudge a module invoked by path without one, and it says nothing about the wider class the peers named. No code change. The fix, both tests and the seam value are untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: wshallwshall <mefordev@messagefoundry.org> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent bac7d94 commit ef89585

3 files changed

Lines changed: 208 additions & 7 deletions

File tree

docs/BACKLOG.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21483,3 +21483,75 @@ The other four are posture, inherited from the tripwire as it already stood:
2148321483
**FOUR SESSIONS INDEPENDENTLY READ THIS THE OTHER WAY, so it is pinned by test rather than by argument.** The shared reading was that this row pins a digest of the corpus, so a CRLF checkout would make the tripwire accuse a clean install of tampering. It does not pin one: `_ATTESTED_ASSETS` holds two relative path strings and the diff contains no hex literal of 32 characters or more, checked with a positive control that fired. `test_line_endings_alone_never_report_tampering` now covers both arms -- an LF wheel and a CRLF wheel each attest clean, because the content and the baseline move together -- and `test_a_swap_of_line_endings_AFTER_install_is_still_drift` is its control, sealing a baseline over LF and rewriting the file as CRLF, which `pip` cannot produce but an in-place editor can, and which must drift. Only that mixed state fails, and nothing that builds a wheel can create it.
2148421484

2148521485
**The missing pin is a real and separate defect**, and it is a reproducible-builds question rather than an attestation one: a wheel built from a Windows checkout carries a byte-different corpus from one built on Linux, so the two wheels' `RECORD` rows for that file disagree while the content is identical. Not filed against a number here, because allocating one to gesture at unowned work is what the ledger rules forbid; named by subject instead, and left for whoever owns the corpus build.
21486+
21487+
---
21488+
21489+
## 1439. webconsole_seam_snapshot.py resolves messagefoundry from sys.path, not from its own repo
21490+
21491+
> 🔢 **Filed 2026-09-03 - BUILT IN THIS COMMIT, not yet landed.** Found while building #1139, by a session that spent its debugging on the gate instead of the generator. The census in LIMB 3 is the reason this is filed as one defect and not a class.
21492+
21493+
**Cluster:** repository tooling. **Priority:** P3. **Verdict:** build.
21494+
**Severity:** no engine effect, no PHI axis, and **no deployment axis (sec. 0)** -- `scripts/` ships in no wheel and this tool touches no product surface. The cost is real and bounded to a developer's session: **wasted debugging, and a gate a reader would "fix" in the wrong direction.** Nothing was mis-shipped, because CI runs on a hosted runner with exactly one engine tree, where the defect cannot express itself.
21495+
21496+
**What:** `scripts/webconsole_seam_snapshot.py` derived the seam digest from whichever `messagefoundry` `sys.path` happened to offer, while reading `_CONSOLE_DIR` and `_ENGINE_DIR` out of the repository it lives in. Run from a git worktree with no `.venv` of its own -- the normal state for these sessions -- those are two different trees.
21497+
21498+
**Python puts the SCRIPT's directory on `sys.path[0]`, never the caller's cwd.** So `scripts/` led the path, no entry offered `messagefoundry`, and the import fell through to site-packages. The interpreter reached for in a `.venv`-less worktree is the primary checkout's, and its site-packages holds `_editable_impl_messagefoundry.pth` containing the **primary checkout's root** -- a path-based editable install, which is why an ordinary `sys.path.insert(0, ...)` is enough to beat it.
21499+
21500+
**LIMB 1 -- THE SUCCESS OUTPUT IS THE PART THAT MISLEADS.** Measured 2026-09-03 in a worktree with no `.venv` of its own: `python scripts/webconsole_seam_snapshot.py --write` printed the **primary** tree's digest `93ba1f10b9dccfc8`, rewrote `messagefoundry/api/_ui_seam.py` and the golden with that unchanged value, and reported success naming both files it had rewritten -- while `tests/test_webconsole_seam_snapshot.py` kept failing against `266cbfd342b22819`, the digest of the tree the test was reading and the script never had.
21501+
21502+
This is the **SDS-3.8** shape: the instrument answered a question adjacent to the one asked. It is worse than a plain error because the only loud word in the room was `rewrote`. The remediation on offer to a reader is that the **gate** is broken, and the gate was right the whole time. A tool that fails by printing `--write` twice teaches a repair to the wrong artifact.
21503+
21504+
**LIMB 2 -- THE MEASUREMENT, AS A CONTROLLED COMPARISON.** Reproduced 2026-09-03 in a second such worktree under the primary checkout's interpreter, four scripts run by path from one cwd, with `sys.path[0]` set to each target's own directory. The **only** variable is whether the script anchors itself:
21505+
21506+
```
21507+
scripts/bench/stage_residency.py -> <THIS WORKTREE>/messagefoundry/__init__.py
21508+
scripts/security/dast_auth_sweep.py -> <THIS WORKTREE>/messagefoundry/__init__.py
21509+
scripts/tray/make_icons.py -> <THIS WORKTREE>/messagefoundry/__init__.py
21510+
scripts/webconsole_seam_snapshot.py -> <PRIMARY CHECKOUT>/messagefoundry/__init__.py
21511+
```
21512+
21513+
**The first instrument tried was the wrong one and is recorded here so it is not tried again.** A `runpy.run_path` probe from the repo root reported all four resolving correctly. It could not have done otherwise: `runpy` in that process left cwd on `sys.path`, so it never reproduced a by-path invocation. Confirming the instrument answers the asked question is the same SDS-3.8 discipline this item is about, and it caught a false clean one step in.
21514+
21515+
**LIMB 3 -- THE CENSUS BOUNDS IT AT EXACTLY ONE SCRIPT.** Every `.py` under `scripts/` importing an in-repo top-level package (`messagefoundry`, `messagefoundry_webconsole`, `harness`, `tee`, `ide`), including indented and deferred imports:
21516+
21517+
| Script | Anchors on `__file__` | Runnable by path | Verdict |
21518+
|---|---|---|---|
21519+
| `bench/stage_residency.py` | yes, `parents[2]` | yes | safe |
21520+
| `security/dast_auth_sweep.py` | yes, `parents[2]` | yes (CI + by hand) | safe |
21521+
| `security/dast_target.py` | no | **no** -- library, no `__main__` | not exposed |
21522+
| `security/route_gates.py` | no | **no** -- library, no `__main__`, says so | not exposed |
21523+
| `tray/make_icons.py` | yes, `parents[2]` | yes | safe |
21524+
| `webconsole_seam_snapshot.py` | **no** | yes, and the docs prescribe it | **this item** |
21525+
21526+
The two unanchored security modules are reached only as `scripts.security.X`, which already requires the root on `sys.path` -- `dast_auth_sweep.py` inserts it before importing them, and pytest supplies it. They inherit a corrected path and are not in the class. `scripts/security/crypto_inventory_check.py` names those packages only in AST-matching string constants, not imports.
21527+
21528+
**So three siblings already carried this fix and one was missed.** That is the strongest thing the census says: the pattern was settled here, and the shape of a per-script anchor is exactly why a fourth omission was invisible -- there is no list of anchored scripts for an unanchored one to be absent from.
21529+
21530+
**THE CENSUS IS KEYED ON THE ABSENCE OF AN EXPLICIT ANCHOR, NEVER ON OBSERVED RESOLUTION, AND THE DIFFERENCE IS NOT ACADEMIC.** Three peer sessions raised this independently on 2026-09-03 and they are right: a script can resolve to the correct tree today for a reason that has nothing to do with the script. Measured here, one interpreter, one command, three working directories:
21531+
21532+
| Invocation | `sys.path[0]` | Resolves to |
21533+
|---|---|---|
21534+
| `python -c "import messagefoundry"` from a worktree root | `''` (cwd) | **the worktree** |
21535+
| the same, from the primary checkout root | `''` (cwd) | the primary |
21536+
| the same, from a neutral cwd | `''` (cwd) | the primary, via the `.pth` |
21537+
| `python scripts/<any>.py` from anywhere | the script's dir | the primary, via the `.pth` |
21538+
21539+
**That is this defect's mirror image.** A by-path script reads the primary because nothing anchors it; a bare interpreter reads the worktree because cwd happens to win. Both are the same missing anchor, and only one of them looks wrong. **A census keyed on "which tree did it resolve to" would clear every script whose cwd currently rescues it, and each of those flips silently the day it is launched from somewhere else.** The fleet's standard recipe for a `.venv`-less worktree -- the primary checkout's interpreter run from the worktree root -- depends entirely on that rescue.
21540+
21541+
**THE TABLE ABOVE WAS THEREFORE RE-DERIVED BY AST, NOT BY READING**, on the predicate *imports an in-repo top-level package AND is runnable by path AND puts nothing on `sys.path`*. It agreed with the reading exactly. **Positive control, because a pattern that finds nothing anywhere is indistinguishable from a clean tree:** 53 `.py` files under `scripts/`, 28 deriving a path from `__file__`, 6 importing an in-repo package -- the walk is live. **Control on the instrument itself:** run against the parent commit's source the census flags `scripts/webconsole_seam_snapshot.py` as `EXPOSED` and nothing else; run against the fix it reports none. A census that cannot see the defect it was written for is not evidence of a clean directory.
21542+
21543+
**The two library modules are structurally safe, not accidentally safe, and the distinction matters given the above.** `dast_target.py` and `route_gates.py` are reached only as `scripts.security.X`. That import form cannot resolve at all unless the repo root is already importable, so the root that supplies the module is necessarily the root that supplies its `messagefoundry` -- one tree by construction, not by cwd. They are not relying on the rescue.
21544+
21545+
**WHAT THIS CENSUS COULD NOT SEE.** Its scope is `scripts/**/*.py` and nothing else -- `harness/`, `tee/`, `ide/` and the workflow-embedded `python -c` invocations were not examined. Its runnability test is the presence of a `__main__` guard, which would misjudge a module invoked by path despite having none. And it says nothing about the wider class the peers named: any process that resolves the package while cwd is not the worktree root reads the primary instead, which is a property of the fleet's test recipe rather than of any file in this directory.
21546+
21547+
**THE FIX IS THE INSERT THE SIBLINGS ALREADY HAVE**, above the `from messagefoundry...` imports, citing the rule `scripts/coord/alloc.ps1` states for `git` (#1060): anchor on the script, not on the caller.
21548+
21549+
**LIMB 4 -- WHY THERE ARE TWO TESTS, AND WHY THE OBVIOUS ONE IS NOT ENOUGH.** `test_the_script_run_by_path_computes_the_same_digest` runs the generator as a subprocess, by path, from a cwd that is not the repo root and with `PYTHONPATH` scrubbed, and requires its digest to equal the one the test derives in-process. Both scrubs are load-bearing: under pytest the root is already on `sys.path`, so **every in-process check in that file is blind to this defect by construction**, and a subprocess inheriting either rescue would pass for a reason unrelated to the script.
21550+
21551+
**That test cannot fail on a hosted runner, and measured here it did not fail without the fix either.** One engine tree means an unanchored script finds the right one by luck; and on this box the primary and the worktree currently carry the *same* seam, so the wrong tree returned the right number. **A test whose environment cannot produce the failure is not evidence the failure is absent.**
21552+
21553+
So `test_the_script_prefers_its_own_repo_over_an_earlier_path_entry` supplies the second tree itself: a decoy `messagefoundry` package on `PYTHONPATH`, which for a by-path invocation sits **ahead of site-packages and behind** an explicit `sys.path.insert(0, repo_root)`. The decoy wins if and only if the anchor is gone, on any machine, with no worktree and no second checkout needed.
21554+
21555+
**Verification:** 8 passed in `tests/test_webconsole_seam_snapshot.py`. Mutation check run rather than argued -- with the `sys.path.insert` line deleted, the decoy test reds naming the decoy import, and the by-path digest test **stays green**, which is the luck described above measured rather than predicted. Anchor restored, 8 passed again.
21556+
21557+
**Adjacent and NOT fixed here, named rather than numbered.** `docs/WEBCONSOLE-PACKAGE.md`'s seam-refresh procedure is stale in three steps left behind by #1220: it says to bump `ENGINE_UI_SEAM` by hand (`1` to `2`) when the value is a derived digest, it says to update curated lists in this script that #1220 retired, and its step 5 prescribes `python scripts/webconsole_seam_snapshot.py > tests/golden/...`, the shell redirect this script's own docstring forbids because PowerShell's `>` writes UTF-16LE with a BOM into a file the test reads as UTF-8. That is doc drift with its own cause and it wants its own item; folding a documentation rewrite into a `sys.path` fix would make both harder to review.

scripts/webconsole_seam_snapshot.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,44 @@
5353
from pathlib import Path
5454
from typing import Any
5555

56-
from messagefoundry.api import security
57-
from messagefoundry.api._ui_seam import (
56+
_REPO_ROOT = Path(__file__).resolve().parents[1]
57+
_CONSOLE_DIR = _REPO_ROOT / "messagefoundry_webconsole"
58+
_ENGINE_DIR = _REPO_ROOT / "messagefoundry"
59+
60+
# ANCHOR ON THE SCRIPT, NOT ON WHATEVER sys.path OFFERS (BACKLOG #1439). It is the same rule
61+
# scripts/coord/alloc.ps1 states for `git` (BACKLOG #1060), and the same insert the siblings
62+
# scripts/bench/stage_residency.py and scripts/tray/make_icons.py already carry. This file measures
63+
# the engine/console contract of the repository it LIVES IN; without the insert it measured whichever
64+
# engine the interpreter happened to find, and said nothing about the difference.
65+
#
66+
# Python puts the SCRIPT's directory on sys.path[0], never the current directory, so `scripts/` led
67+
# the path and no entry offered `messagefoundry` until site-packages. In a worktree with no .venv of
68+
# its own -- the normal state for these sessions -- the interpreter reached for is the primary
69+
# checkout's, whose site-packages holds a path-based editable install (`_editable_impl_*.pth`
70+
# containing the PRIMARY checkout's root). So `import messagefoundry` resolved to the PRIMARY tree
71+
# while the discovery walk below read _CONSOLE_DIR and _ENGINE_DIR out of THIS one.
72+
#
73+
# The failure was silent and it pointed the wrong way. Measured 2026-09-03 in a worktree with no
74+
# .venv of its own: `--write` printed the unchanged digest, rewrote both files with it, and
75+
# reported success, while tests/test_webconsole_seam_snapshot.py (which runs under pytest,
76+
# with the repo root already on sys.path) kept failing against a digest the script had never
77+
# computed. The script's own success output is what misled -- a reader concludes the GATE is broken
78+
# and starts "fixing" a gate that was right. This is the SDS-3.8 shape: an instrument answering a
79+
# question adjacent to the one asked.
80+
#
81+
# Inserted at 0, so it wins over that .pth entry. tests/test_webconsole_seam_snapshot.py::
82+
# test_the_script_run_by_path_computes_the_same_digest holds the property from outside, in a
83+
# subprocess, because nothing INSIDE this process can observe which tree it failed to import.
84+
sys.path.insert(0, str(_REPO_ROOT))
85+
86+
from messagefoundry.api import security # noqa: E402
87+
from messagefoundry.api._ui_seam import ( # noqa: E402
5888
ENGINE_UI_SEAM,
5989
AdminHandlers,
6090
CoreHandlers,
6191
UiDeps,
6292
)
63-
from messagefoundry.auth.service import AuthService
64-
65-
_REPO_ROOT = Path(__file__).resolve().parents[1]
66-
_CONSOLE_DIR = _REPO_ROOT / "messagefoundry_webconsole"
67-
_ENGINE_DIR = _REPO_ROOT / "messagefoundry"
93+
from messagefoundry.auth.service import AuthService # noqa: E402
6894

6995

7096
def _load_discovery() -> Any:

0 commit comments

Comments
 (0)