Skip to content

fix(cli): one refusal walk for every leaf apply can write - #1231

Draft
HugoXK wants to merge 4 commits into
mainfrom
agent/1230-refusal-walk
Draft

HugoXK wants to merge 4 commits into
mainfrom
agent/1230-refusal-walk

Conversation

@HugoXK

@HugoXK HugoXK commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1230.

What changed

#1215 / #1222 / #1224 each fixed the one leaf its report named. The fourth leaf of the same walk — a link planted inside the destination — still read differently in the preview and in the writer: with a tree artifact writing alpha and dest/alpha a symlink to a file outside, show --diff and apply --dry-run returned written: ["alpha"] while the real apply raised an uncaught TreeError traceback out of main (refusing to write 'alpha' under …: 'alpha' is a symlink), not the exit-2 JSON refusal every other blocked leaf returns. The same held one level down: dest/sub a symlink to a directory and the artifact writing sub/leaf.md.

After: one refusal in _check_apply_paths (the funnel show, apply, --from and --install-to all call) runs filetree.dest_problem(dest, path) for every produced path — the rule materialize already enforces, not a second copy of it:

cannot write <dest>/alpha: 'alpha' is a symlink
cannot write <dest>/sub/leaf.md: 'sub/leaf.md' is a symlink
  • apply --from's per-leaf _refuse_dest_link loop is subsumed by this check, so the two surfaces name one block with one sentence instead of two.
  • The refusal is the funnel's RunStoreError, which the CLI already turns into the exit-2 JSON refusal — so the real apply stops tracing back. For Python callers of EvolutionResult.write_to, the exception for this case changes from materialize's TreeError to the RunStoreError every other blocked leaf uses; tests/test_apply_symlink_dest.py pinned the old type and now pins the new one. Rule and sentence unchanged.
  • A text artifact's written is [dest] itself and is skipped here; its link and directory refusals run before the funnel, unchanged.
  • Unchanged: the #1215/#1222/#1224 sentences, the #977 destination-link refusals, backups, and a legal sibling in dest.

Known limits at this tip

  • A hardlink at a dest leaf is not detected. filetree.dest_problem rules on symlinks and realpaths; a leaf that is a hardlink to a file outside (nlink > 1) passes, and writing it truncates the shared inode, so the outside file changes with it. Measured at ef336178 and identically at base 761eb4cb: an outside victim.txt holding SECRET\n, hardlinked to dest/alpha, comes back as evolved\n while keeping the same inode — pre-existing, not introduced by this PR. So the scope here is symlink leaves (and symlinked intermediates) only; hardlinks are a separate follow-up, no issue opened for them yet.

  • The prune=True path changed shape, and no live caller uses it. Whole-repo sweep at this tip (grep -rn 'write_to(' and grep -rn 'prune=' across agentdescent/, tests/, examples/, tools): every production call site passes no prune, so it gets the default False — campaign.py:109 write_to(target, backup=False), cli.py:3316 _write_to(dest, dry_run=True, check_write=…), cli.py:3722 _write_to(dest, backup=backup, dry_run=dry_run, check_write=…), and the four examples/* write_to(args.…) calls. The only callers passing prune=True are two test rows over regular files (tests/test_apply_noop.py:69, tests/test_dir_evolution.py:315), unaffected and still passing. What did change on that path: the funnel now rules on plan["deleted"] too, so write_to(prune=True) refuses to delete a stray symlink where base 761eb4cb deleted it (base deleted: ['stray'], link gone; tip RunStoreError: cannot write <dest>/stray: 'stray' is a symlink, link and its target untouched, the outside file still keep\n), while a stray regular file is still deleted (deleted: ['notes.md'], gone). The refusal is fail-closed in the direction this PR wants and it reaches no live path; it is pinned at this tip by test_prune_refuses_to_delete_a_symlink_it_did_not_evolve in tests/test_apply_noop.py.

  • The pin covers the refusal, not its position in the plan. test_prune_refuses_to_delete_a_symlink_it_did_not_evolve fixes that write_to(prune=True) refuses a stray symlink instead of deleting it silently; it does not fix "the link is refused before anything is removed", because its plan holds only that link. That ordering is a code fact — evolution.py:2214 feeds changed + plan["deleted"] through the funnel ahead of the :2221 removal loop — plus a measurement both readers made with a mixed plan (notes.md beside the link → the refusal fires and notes.md is still present, dest snapshot identical; rules.md old→new beside the link → rules.md stays old\n with no backup, where base wrote new\n and deleted the link). A regression that deleted regular extras before reaching the link would still pass this row; a row that pins that order is on the bench, no issue opened.

Reads

Seats at 10c957318fd898bbb2aa649b266864512b0135e5 (this tip): 2 of the 2 available seats have read it — Test and Security, both naming this sha. The third seat is absent, not pending a favour: Reviewer's route resolves CSI_API_KEY, which is not set. The ef336178… reads at the bottom are history.

  • Security — Blocking: none @ 10c957318fd898bbb2aa649b266864512b0135e5 (its API read Date: Mon, 23 Nov 2026 11:42:18 GMT; PR re-read 11:53:47Z: draft, 4 commits, 6 files +267/−14, base 761eb4cb, mergeable_state: clean). It re-measured at this sha instead of carrying the ef336178 read over:

    • the increment is test-only — git diff ef336178..HEAD is tests/test_apply_noop.py alone, +19/−1, and git diff --name-only ef336178..HEAD -- agentdescent/ .github/ pyproject.toml docs/ is 0 files;
    • placement re-checked here: one leaf inside the existing funnel, evolution.py:1690 in _check_apply_paths, no new call site; the prune path feeds it from evolution.py:2214 (changed + plan["deleted"]) before the :2215 dry-run return, :2218 copytree, :2220 materialize and the :2221 removal loop;
    • five-surface probe re-run at this sha: 215 PASS, 0 FAIL. Each of the six instrumented paths does exactly one _check_apply_paths, one refusal, zero copytree/copy2/materialize; the read-only previews exit 0 carrying apply_plan.error, every write surface exits 2 with cannot write <dest>/alpha: 'alpha' is a symlink;
    • zero-write: dest and home snapshots (kind, bytes sha, mtime_ns) identical, the link still the same link with the same target and lstat mtime, victim bytes+mtime unchanged, no Traceback;
    • gates: refusal_walk 6, four-file subset 19, test_apply_symlink_dest+test_write_dirs_apply_show 8, test_apply_noop.py 15, noop+dir_evolution 35; tools.changelog_fragments check → 25 OK; tools.gen_api_docs --check → up to date; hygiene grep over ^+ lines rc=1; CI 10/10, zero red legs. Not run: full suite; mkdocs build --strict (mkdocs absent here, 0 docs/ files in the diff).
    • out of this card, pre-existing and identical at base: a hardlink at a dest leaf still rewrites the outside file sharing its inode.
  • Test — Blocking: none @ 10c957318fd898bbb2aa649b266864512b0135e5 (base 761eb4cb, draft, 4 commits, 6 files +267/−14, mergeable_state: clean; head re-read from the API at the end of its read). Fresh clone, no GitHub writes.

    • delta: git diff ef336178..10c95731 -- agentdescent/ is 0 lines, so the five-surface and both-link-shape measurements from ef336178 apply unchanged here — it did not re-run those probes, and the unchanged five-file refusal family is 24 passed at this sha;
    • red first, and for the right reason: base 761eb4cb with only this tip's tests/test_apply_noop.py → 1 failed, 14 passed, E Failed: DID NOT RAISE <class 'agentdescent.runstore.RunStoreError'> at tests/test_apply_noop.py:83. It also measured what base did instead: the call returned {deleted: ['stray'], written: []} with the link gone and a bak-0 dropped under $AGENTDESCENT_HOME/backups — base deleted a link silently, which is the behaviour change, not a wording mismatch. Tip → 15 passed;
    • mutation pins the row to this fix rather than to the funnel in general: reverting evolution.py:2213-2214 from changed + plan["deleted"] to changed in a throwaway worktree puts the row back to 1 failed, 14 passed at line 83 while tests/test_apply_refusal_walk.py stays 6 passed;
    • trap check: the message is pinned exactly (cannot write <abs dest>/stray: 'stray' is a symlink), as are islink+readlink of the link, the target's bytes, and _baks(dest) == []. It also checked the backup root directly: across three probe calls (link only, link plus a regular extra, regular extra only) the tip produced 1 slot, where base produced 3 — a refused leaf returns before copytree, so no bak-N exists for it. A stray regular file is still deleted (deleted: ['notes.md'], gone);
    • full suite, its own run (-o addopts="" -q -p no:cacheprovider -n 20 --dist loadfile): 21 failed, 8021 passed, 53 skipped. Against its ef336178 run of the same suite, two rows are newly failing (test_dsh_superagent_run.py::test_a_status_written_after_the_window_and_before_the_verdict_is_the_handoff, test_sandbox.py::test_a_sandbox_that_is_still_being_renewed_is_left_alone) and one is absent; both new ones pass alone at this sha and at base, so they are parallel-order flakes, and this commit adds a test file that cannot reach them. The rest is the known docker/sandbox/era_srbench/genesis environment set that is red at base too;
    • gates: tools.gen_api_docs --check → up to date; tools.changelog_fragments check → 25 OK, fragment changelog.d/1230-refusal-walk.md present. Not run: mkdocs build --strict (no docs//mkdocs.yml change); single-process full pytest (parallel run stated instead). No UI surface: one test row, the CLI surface it pins unchanged since ef336178.

Coverage of the new prune row, as both readers found it. One gap is real, and both readers named it independently: the row's plan holds only the link, so "the link is refused before anything is removed" is measured but not pinned by the row — a future regression that deleted regular extras before refusing the link would still pass it. Both call it non-blocking, and the property holds at this sha: with notes.md and stray in one plan the refusal fires and notes.md is still present, the dest snapshot identical, where base deleted both; likewise rules.md old→new plus the link refuses with rules.md still old\n and no backup, where base wrote new\n and deleted the link. What the row does pin is the refusal itself and the link/target/backup-shadow facts, and the mutation above pins it to the changed + plan["deleted"] line. The ordering is also a code fact with a line reference (evolution.py:2214 ahead of the :2221 removal loop).

One reader point does not hold, checked against the code: assert _baks(dest) == [] is not a "no backup anywhere" assert. _baks (tests/test_apply_noop.py:30-32, byte-identical at base 761eb4cb) lists a sibling *.bak-* beside the destination — the #758 shadow: :127 documents that intent (no sibling shadow next to the skill (#758)) and :191 shows the sibling shape it catches, and 15 rows in this file use it. Backups have lived under AGENTDESCENT_HOME/backups since #758, so this row's scope is "no dest.bak-N sibling", which is what the old layout violated. The wider "no backup slot for the refused leaf" fact is measured separately (1 slot for the tip's three-call matrix, 3 for base's) and the refusal never reaches the backup step (evolution.py:2214 runs ahead of it).

History — both of these name the superseded ef336178:

  • Test — Blocking: none @ ef336178ce199e8a552f25cda27f7138ebd856d0. Red first at base 761eb4cb + only the new file → 2 failed, 4 passed (the two link rows: test_a_symlinked_leaf_is_named_on_every_surface line 79 assert None == "cannot write …/lib/alpha: 'alpha' is a symlink", and test_a_symlinked_intermediate_directory_is_named_on_every_surface), tip → 6 passed; test_apply_symlink_dest + blocked_parent + blocked_child + blocked_leaf 18 passed, five-file set 24 passed. Shape A (a link at dest/alpha) — 6 invocations, one sentence, no Traceback, link lstat and victim bytes/mtime unchanged, no bak-*; shape B (dest/sub a link to a directory) — 5 invocations, same shape. At base the real apply exited 1 with a traceback and dropped a bak-0 (the #977 hazard). Controls hold: legal sibling, text artifact (pre-funnel #977 refusal), dest-is-a-directory message identical to base. Full pytest -q at the tip: 20 failed, 8023 passed, 51 skipped — 19 identical on base 761eb4cb, the 20th a docker mount failure that also fails alone at base. No UI surface.

  • Security — Blocking: none @ ef336178ce199e8a552f25cda27f7138ebd856d0 (the only sha read for a verdict; a16500e2/df940ab6 not read; head re-read by it). Placement measured, not taken from this body: the delta adds one leaf inside the existing funnel — evolution.py:1690 _refuse_link_in_dest(dest, path) in _check_apply_paths (:1686-1690, helper :1646-1669, rule = filetree.dest_problem) — and no new call site; cli.py's only code change is deleting --from's per-leaf loop. The guard runs before every writer: tree evolution.py:2213 check → :2215 dry-run return → :2218 copytree → :2220 materialize; --from cli.py:3898 → :3904 copytree → copy2; show tree cli.py:3316 _write_to(dry_run=True). Five surfaces with counters on _check_apply_paths/copytree/copy2/materialize — exactly one funnel call, exactly one refusal, zero write primitives each; show --diff --json exits 0 carrying apply_plan.error (read-only preview exits 0 by design, as the #1215/#1222/#1224 rows and this PR's own test require), every write surface exits 2 with the same cannot write <dest>/alpha: 'alpha' is a symlink; intermediate dest/sub → dir gives 'sub/leaf.md' is a symlink. Zero-write evidence: dest and home snapshots (name + bytes sha + mtime_ns) identical, bak-* 0, victim bytes+mtime unchanged, leaf still the same link with the same target and lstat mtime, no Traceback on stderr. Attack set: link→outside, link→$AGENTDESCENT_HOME/backups/… (backup file untouched), dangling leaf, and dangling intermediate (refused by the pre-existing blocked-parent shape, word-for-word at base) all refuse; a legal sibling writes alpha and leaves keep.txt intact; a refused leaf beside a sibling writes nothing. Unchanged: #1215/#1222/#1224 sentences word-for-word, #1204 text dir, #977 text link. Hygiene grep (API_KEY|FORWARDED_ENV|environ|getenv|subprocess|secret|token|password|shell=True|requests\.|urllib over ^\+ lines) → rc=1, no hit; no .github/, dependency, lockfile or docs/api.md change; 1 changelog fragment added. CI settled 10/10. Two notes out of this card, both pre-existing and identical at base 761eb4cb, no issue opened: a hardlink at a dest leaf still rewrites the outside file sharing its inode (dest_problem's symlink/realpath rule cannot see hardlinks), and _refuse_link_in_dest also runs over plan["deleted"], so write_to(prune=True) now refuses to delete a stray symlink where base deleted it (the CLI never passes prune, cli.py:3737-3744, so none of the five surfaces change).

  • At ef336178ce199e8a552f25cda27f7138ebd856d0 both available seats had read it — Test and Security, both naming that sha; superseded by the prune-row commit. The third seat stayed absent there too.

Verification at the tip

Tip SHA: 10c957318fd898bbb2aa649b266864512b0135e5

Ran:

  • new test file at base 761eb4cb, only that file added (first commit df940ab6) → 2 failed, 4 passed; the two are test_a_symlinked_leaf_is_named_on_every_surface and test_a_symlinked_intermediate_directory_is_named_on_every_surface; the four passers are the #1215/#1222/#1224 shapes as locks plus a legal-sibling control

  • pytest -q -o addopts='' tests/test_apply_refusal_walk.py at a16500e2 as committed → 1 failed, 5 passed: the leaf row still expects the absolute path (…: '/abs/alpha' is a symlink), while the code, this issue and the sentence materialize raises name the relative path (…: 'alpha' is a symlink). The delta is one line in that row (expected = f"cannot write {leaf}: 'alpha' is a symlink"); with it the file is 6 passed and the family below is 65 passed. That one-line edit is now committed as ef336178ce199e8a552f25cda27f7138ebd856d0 (test-only, one file, +1/−1).

  • family: refusal_walk + blocked_parent + blocked_child + blocked_leaf + show_dest_refusals + write_dirs_apply_show + apply_symlink_dest + cli_apply_created_target + cli_apply_text_missing_parent + apply_backup_away_from_skills + apply_from + cli_write_error + demo_dest_link → 1 failed, 64 passed at a16500e2 as committed (the same leaf row); 65 passed with the one-line fix

  • at ef336178ce199e8a552f25cda27f7138ebd856d0 — the committed tip — pytest -q -o addopts='' tests/test_apply_refusal_walk.py → 6 passed; the same run by the Lead from a clean tarball of this sha, and the four-file subset (refusal_walk + blocked_parent + blocked_child + blocked_leaf) → 19 passed; the tail note on a16500e2 was a wrong assertion in the row, not a defect in the refusal

  • broader tests/test_apply*.py tests/test_show*.py tests/test_write*.py tests/test_install*.py tests/test_demo*.py → 6 failed, 193 passed at a16500e2 as committed; five are tests/test_apply_noop.py rows and fail identically on unpatched base 761eb4cb (this host's ~/.agentdescent is read-only: OSError: [Errno 30] … read-only file system), the sixth is the leaf row above

  • reverse check, contract wording against the base: base 761eb4cb + only this test file (head ef336178's copy, expected string already the contract form) → 2 failed, 4 passed — test_a_symlinked_leaf_is_named_on_every_surface and test_a_symlinked_intermediate_directory_is_named_on_every_surface fail on the defect itself (show advertised written: ["sub/leaf.md"] where the real apply refuses), not on a message mismatch; the same file at ef336178 → 6 passed. So the row is red-first for the right reason and the fix is the code, not the assertion. This PR's changes to tests are three and no implementation line was touched to make any row pass: that one line of expected (+1/−1), the TreeError→RunStoreError pin in tests/test_apply_symlink_dest.py, and the new prune row (+19/−1). ef336178ce199e8a552f25cda27f7138ebd856d0 is a16500e2 plus that expected line; 10c957318fd898bbb2aa649b266864512b0135e5 is ef336178ce199e8a552f25cda27f7138ebd856d0 plus the prune row alone (that commit's git diff: one file).

  • base run with the new assertion only — implementation untouched, diff -rq against a pristine 761eb4cb1d2d44a4d69eb7ce17f8830f2e31cae9 extraction shows tests/test_apply_refusal_walk.py as the only added source file — fail verbatim: AssertionError: show advertised a write the real apply refuses: {"written": ["alpha"], "unchanged": [], "extra": [], "deleted": [], "backup": [], "noop": false} / assert None == "cannot write /tmp/…/lib/alpha: 'alpha' is a symlink" (tests/test_apply_refusal_walk.py:79). Red on the assertion itself, not on a message mismatch.

  • prune row, red first: base 761eb4cb1d2d44a4d69eb7ce17f8830f2e31cae9 with only this row — the file is otherwise byte-identical to the tip's, diff clean — → 1 failed, 14 passed, E Failed: DID NOT RAISE <class 'agentdescent.runstore.RunStoreError'> at tests/test_apply_noop.py:83, because base's write_to(prune=True) deleted stray and returned a plan. The same file at 10c957318fd898bbb2aa649b266864512b0135e5 → 15 passed. So the row pins the behaviour change itself: prune=True now refuses to delete a stray symlink instead of deleting it silently, which is what this tip introduced.

  • python -m tools.gen_api_docs --check → docs/api.md is up to date

  • python -m tools.changelog_fragments check → 25 changelog fragment(s) OK

  • CI at the superseded ef336178 (GitHub Date 2026-11-23T10:27:16Z): 10/10 checks concluded — build, otel-wire, test (3.9), (3.10), (3.11), (3.12), (3.13) all success; deploy skipped; risk tier / reviewed tip report-only success. Zero failures. CI at 10c957318fd898bbb2aa649b266864512b0135e5 (the prune row) has settled the same way: 10 runs, test (3.9)–(3.13) success (3.13 11:49:36Z, 3.11 11:50:07Z, 3.12 11:50:23Z, 3.10 11:50:49Z, 3.9 11:53:18Z), build 11:35:14Z, otel-wire 11:35:17Z, risk tier / reviewed tip report-only success, deploy skipped — zero red legs.

Not run:

  • full pytest -q — the CI matrix is the line
  • mkdocs build --strict — no docs/ page or mkdocs.yml change

Draft on purpose: it was opened in the claim turn with the red test; it stays draft until the Lead schedules the ready flip.

HugoXK and others added 4 commits November 23, 2026 09:56
The preview and the tree writer read different rules for a link planted
inside the destination: `_refuse_blocked_parent` follows a link to a
directory, `_refuse_blocked_leaf` skips one, so `show --diff` and
`apply --dry-run` advertise `written: ["alpha"]` while the real apply
raises an uncaught `TreeError` traceback instead of the exit-2 refusal.

Red at 761eb4c with only this file added: 2 failed, 4 passed. The two
are the new link leaves (at the leaf, on the way to it); the four
passers are the earlier shapes as locks plus a legal sibling.
`_refuse_blocked_parent` treats a link to a directory as a directory and
`_refuse_blocked_leaf` skips links, so a link planted inside the tree was
the one leaf of this walk the preview did not see: `show --diff` and
`apply --dry-run` returned `written` while `materialize` refused at write
time with a `TreeError` -- an uncaught traceback out of `main`.

`_check_apply_paths` now runs `filetree.dest_problem(dest, path)` for every
produced path, the same rule `materialize` enforces, so preview and write
cannot drift; the refusal is the funnel's `RunStoreError`, which the CLI
already turns into the exit-2 JSON refusal. `apply --from`'s per-leaf
`_refuse_dest_link` loop is subsumed by it, so the two surfaces name one
block with one sentence. A text artifact's `written` is `[dest]` and is
skipped here; its link refusals run before the funnel.

`tests/test_apply_symlink_dest.py` asserted the old `TreeError` from
`materialize` for the run-id path; the refusal now arrives earlier and as
the type every other blocked leaf uses. Rule and sentence unchanged.
…pecifies

`dest_problem` returns `'<rel>' is a symlink` (filetree.py:359) and
`_refuse_linked_dest` prefixes the absolute destination path
(cli.py:1328), so the sentence is `cannot write <abs dest>: 'alpha' is a
symlink`. The row asserted the absolute path inside the reason and was
red at a16500e. Test-only: no production file changes.
…volve

`prune=True` no longer removes a stray link inside the destination:
`_check_apply_paths` now runs over `plan["deleted"]` too, so the link is
refused with the same `cannot write <path>: '<rel>' is a symlink` sentence
the write leaves use (#1230). Base deleted it silently, which is why this
row fails at 761eb4c.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(cli): one refusal walk for every leaf apply can write

1 participant