Skip to content

fix(cli): text apply --to creates a missing parent - #1125

Open
HugoXK wants to merge 1 commit into
mainfrom
agent/1124-text-apply-missing-parent
Open

HugoXK wants to merge 1 commit into
mainfrom
agent/1124-text-apply-missing-parent

Conversation

@HugoXK

@HugoXK HugoXK commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1124

What changed

A text-kind apply <run> --to <fresh>/rule.md now creates the missing parent directory before writing. Before: the dry run printed "written": ["…/newdir/rule.md"], "noop": false and exited 0, then the real run exited 2 with error: cannot write …/newdir/rule.md: No such file or directory, writing nothing. After: the real run exits 0, the file lands at that path, and the parent is created — the same shape a tree artifact already had, and the same thing apply --from already does before its open.

Scope is the run-store text branch only (agentdescent/cli.py, the block before open in apply_payload): os.makedirs(parent, exist_ok=True) after the backup step and before open, with an OSError from makedirs still going through the existing _cannot_write single-line path. The dry run is unchanged and still writes nothing, parents included. Not touched: the tree branch (filetree.materialize), --from, and _text_plan's noop rules.

Tests, two rows in tests/test_cli_apply_text_missing_parent.py:

  • positive: dry run 0 and still no directory; real run 0; file content and parent asserted.
  • reverse pin (green at base, stated as a pin and not a second red): a --to under a regular file still exits 2 with exactly one error: cannot write … line and no traceback. tests/test_cli_write_error.py is byte-identical to base.

Red at base 5048349aaceb1ad4c93318581f9a2b49481a5a99, clean worktree with only the new test file added, pytest -q tests/test_cli_apply_text_missing_parent.py:

F.                                                                       [100%]
__________________ test_a_text_apply_creates_a_missing_parent __________________

>       assert cli.main(["--store", store, "apply", rid, "--to", str(dest)]) == 0
E       AssertionError: assert 2 == 0
E        +  where 2 = <function main at 0x7f34da0f6ef0>([... '--to', '.../newdir/rule.md'])
----------------------------- Captured stderr call -----------------------------
error: cannot write .../newdir/rule.md: No such file or directory
=========================== short test summary info ============================
FAILED tests/test_cli_apply_text_missing_parent.py::test_a_text_apply_creates_a_missing_parent
1 failed, 1 passed in 5.11s

The dry run is already green at that base, which is the bug: the plan is not a prediction.

Verification at the tip

Tip SHA: 9e0c669
Ran:

  • pytest -q tests/test_cli_apply_text_missing_parent.py tests/test_cli_write_error.py tests/test_apply_from.py tests/test_apply_noop.py tests/test_apply_symlink_dest.py tests/test_apply_backup_away_from_skills.py tests/test_skill_library_apply_install.py → 67 passed
  • pytest -q tests/test_cli_missing_target.py tests/test_missing_cli_kind_remedy.py tests/test_cli_json_errors.py tests/test_cli_surface_error_envelope.py tests/test_plain_cli_warnings.py → 57 passed
  • pytest -q tests/test_repo_hygiene.py tests/test_site_navigation.py tests/test_docs_links.py → 26 passed
  • python -m tools.gen_api_docs --check → docs/api.md is up to date
  • python -m tools.changelog_fragments check → 50 changelog fragment(s) OK
  • python -c "py_compile.compile('agentdescent/cli.py', doraise=True)" → clean
  • pytest -q -n 8 tests → 7499 passed, 60 skipped, 20 failed in 695.27s — the 20 are the host-only modules (12 test_sandbox_container with no reachable Docker daemon, 6 test_era_srbench, 2 test_genesis_example), none in a file this PR touches
    Not run:
  • mkdocs build --strict — mkdocs is not installed in this clone's venv; CI's build job is the line.

Known non-blocking

None of these is fixed here, and none asks for a new head.

  • The 11-line block is byte-identical to the --from text branch (the review's diff -u is empty). A small _ensure_parent(dest) would leave one copy; that consolidation is left for a later change.
  • --to newdir/ (trailing slash, parent missing) now creates the empty newdir/ and then fails with one error: cannot write …: Is a directory line, exit 2. Base created nothing and died at open, so the leftover directory is a side effect introduced by this PR. It is benign — the command still refuses with the same one line — and it is deliberately not fixed here; the dry run's prediction gap for a directory-shaped --to is a separate, pre-existing gap, not bug(cli): text apply --to into a missing parent: dry run green, real run exit 2 #1124.
  • A NUL byte in --to raises ValueError: embedded null byte out of _text_plan at base and at this head alike — pre-existing, not introduced by this PR.

@HugoXK HugoXK added area:cli The agentdescent command line (cli.py, doctor, plan/evolve/status/apply) bug Something isn't working labels Nov 16, 2026
@HugoXK
HugoXK marked this pull request as ready for review November 16, 2026 09:16
`_text_plan` only asks whether `dest` already holds the evolved text, so a
missing parent was invisible to the dry run: it printed `written` with exit 0
and the real run died at `open` with `error: cannot write ...: No such file or
directory`, writing nothing. The tree branch materialises its parents and so
does the `--from` branch; the run-store text branch now does the same before
`open`, and an `OSError` from `makedirs` still takes the existing
`_cannot_write` single-line path.

The reverse row pins the #990 contract (exit 2, one `error: cannot write ...`
line); `tests/test_cli_write_error.py` is unchanged. Not in scope: the tree
branch, `--from`, `_text_plan`'s noop rules.

@HugoXK HugoXK left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review tip: 9e0c669

Verdict: PASS

Internal AD Security review of this tip, from a fresh clone. It is not the PASS .github/agent/review-policy.json counts (that file lists only login Birfy) and it is not the merge gate; the owner's PASS at the tip and merge click still decide.

Security surface: no finding. 3 files (agentdescent/cli.py +11, changelog.d/1124-text-apply-missing-parent.md, tests/test_cli_apply_text_missing_parent.py), no .github/**, dependency or lockfile, integrations/**, ARK_API_KEY/FORWARDED_ENV, network or model call. The one new filesystem effect is directory creation, and it sits behind the two gates the branch already had — the link refusal and the operator write guard.

1. The fix is the tree's existing shape, byte for byte. diff -u between the new block (agentdescent/cli.py:3426-3431) and the --from text branch (:3525-3530) is empty. It is the same parent = os.path.dirname(dest) / if parent: os.makedirs(parent, exist_ok=True) / except OSError: raise _cannot_write(dest, e) the tree writer (agentdescent/filetree.py:393,403) and --from already use — not a second rule. The duplication is real; see the non-blocking note, no change requested.

2. The dry run still writes nothing, parents included. if dry_run: returns at :3404-3405, before the new makedirs at :3426. Measured from a fresh cwd with a relative --to sub/rule.md --dry-run: exit 0, written: ["sub/rule.md"], sub/ absent; the real run then exits 0, creates sub/, writes the text, content matches. The positive row pins the dry-run half at tests/test_cli_apply_text_missing_parent.py:26-30.

3. A refused parent is still one line and exit 2. Parent path occupied by a regular file (linkfile -> real.txt, --to linkfile/rule.md): makedirs raises FileExistsError, the new except OSError turns it into error: cannot write …/linkfile/rule.md: File exists, exit 2, one line, no traceback. Base reached the same handler from open (NotADirectoryError), so the contract is unchanged. tests/test_cli_write_error.py is byte-identical to base (git diff length 0); the reverse row is a pin, as the body states.

4. Security — makedirs is behind the link check and the write guard, checked at the lines. Order in the text branch: _refuse_dest_link(os.path.dirname(os.path.abspath(dest)) or os.sep, dest) at :3399 → _text_plan (read only, :3400) → _check_apply_paths(dest, plan["written"], slot, check_write) at :3403 → dry-run return :3404 → optional .bak-N copy :3408-3416 (only when dest exists, so its parent exists and the makedirs is a no-op) → new makedirs :3426-3431 → open :3433. No write precedes the check.

  • The check is filetree.dest_problem(parent, dest) (filetree.py:344-364): it walks every component from the parent down to the leaf and refuses a symlink, then requires os.path.realpath(dest) to stay under os.path.realpath(parent). Measured: a symlinked leaf → exit 2, one line, victim file unchanged; a parent that is a symlink to a file → exit 2, one line.
  • A symlinked directory above the file stays legal — filetree.py:367-380 (leaf_problem docstring) says so and I measured it: --to <linkdir>/rule.md exits 0 and the file lands in the linked directory. That is the pre-existing rule for --from and the tree writer, not widened by this PR.
  • The operator guard runs before creation: with AGENTDESCENT_SURFACE_WRITE_DIRS=<allowed> and a dest outside it → exit 2 and the outside directory is not created (empty allowlist behaves the same); with the dest inside, the missing subdirectory is created and written.
  • Non-blocking, for accuracy: this is a single pre-write check, and nothing re-checks between makedirs and open. --from has the identical ordering (:3508 check, :3528 makedirs), so the race window is the shape this branch is copying, not a new one, and the delta adds no capability beyond creating the parent of a destination the guard already accepts.

Evidence. Red reproduced at base 5048349a with only the new test file: 1 failed, 1 passed — assert 2 == 0, stderr error: cannot write …/newdir/rule.md: No such file or directory; the reverse row is green at base. At this tip: new file + tests/test_cli_write_error.py → 19 passed; the two apply/CLI selections → 67 passed and 57 passed; tests/test_repo_hygiene.py tests/test_site_navigation.py → 4 passed; tools.gen_api_docs --check → up to date; tools.changelog_fragments check → 50 OK; py_compile clean; no docs/** change, so mkdocs is CI's line. Checks at this head: build, otel-wire, risk tier (report only), reviewed tip (report only) and test (3.9)–(3.13) success, deploy skipped. One environment note: 7 rows of the apply selection fail in this review sandbox only because $HOME/.agentdescent is read-only here; with AGENTDESCENT_HOME pointed at a writable directory all 67 pass — not the PR.

Non-blocking, not findings.

  • The 11-line block is byte-identical to the --from copy; a small _ensure_parent(dest) would leave one copy, but the shape is the point of this PR and I do not ask for a new head.
  • --to newdir/ (trailing slash, missing): exit 2 and one line (Is a directory), but the empty newdir/ is now created before the failure, and the dry run still reports written. The prediction gap is the pre-existing #1124 class for a directory-shaped path; the leftover directory is new and benign.
  • A NUL byte in --to raises ValueError: embedded null byte out of _text_plan at base and at this head alike — pre-existing, not introduced.
  • The PR currently carries bug/area:cli and no risk:T* yet; the controller sets it, and the report-only risk tier check already ran green. Not mine to set.

One review object was written; no label/assignee/draft change, no push, no merge.

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

area:cli The agentdescent command line (cli.py, doctor, plan/evolve/status/apply) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(cli): text apply --to into a missing parent: dry run green, real run exit 2

1 participant