Skip to content

fix: refuse a save-script destination another local user can steer - #4584

Draft
thymikee wants to merge 27 commits into
mainfrom
fix/save-script-ancestor-redirect
Draft

thymikee wants to merge 27 commits into
mainfrom
fix/save-script-ancestor-redirect

Conversation

@thymikee

@thymikee thymikee commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Closes #4565.

open --save-script=<path> resolved the destination by name, so a second local user who could write any directory on the way could replace that component, or leave a link there before the entry was created, and move the recording wherever they chose. Both the local writer and the remote staging path had it.

Node exposes no openat or renameat, so no re-check closes a race with a local attacker. The window is closed by permissions: a destination is accepted only where no other non-root local user can modify any directory the publish traverses. Then nobody can swap a component out or plant one, so the path the rule judged is the path the publish reaches.

The walk resolves the parent as the kernel does, one component at a time, judging every directory it steps into rather than only the final real path. Each step rests on one lstat of the ENTRY and never on a stat, which would resolve the same name twice and answer whatever it points at by then: a link is judged as a link, against the directory that holds it, and the walk goes on to its target one component at a time. A .. applies to the directory stood in, and a destination that is not absolute is refused on every platform.

host-kit owns the sequence, and the surface is two calls. prepareScriptDestination walks, refuses before creating anything, creates each missing level at 0700 with one non-recursive mkdir inside the judged directory, walks the completed chain, and answers with that directory and its dev:ino. confirmScriptDestination(destination, prepared) is the one confirmation a caller that waits owes: it walks again, compares the identity, and returns the directory to create the entry in, so every parent_replaced refusal is built by scriptAncestryFailure and no route holds a copy of the reason, exposure, ancestor or hint. A missing level that exists by the time mkdir reaches it is judged by that same lstat rule rather than refused, which is where two processes saving into one new directory meet. Remote delivery walks twice per version where the parent already stands and three times where a level has to be created, and compares identity once, after the download and before the rename; the local publish walks once, twice with a missing parent, and compares nothing.

Nothing recognises ground a walk already stood on. An earlier shape kept a per-walk memory of judged directories, re-compared an identity on every re-visit and before every creation, and refused parent_replaced from inside the walk. That layer is cut: a directory the walk stands in is one this user or root owns that others cannot write in, or a sticky one where this user's entry cannot be replaced, so nobody the rule accepted is left to make the swap it defended against. The one retained comparison is the remote pre-rename one, and it is host-kit's.

The daemon creates and repairs its own state directories at 0700, so a umask-002 host no longer refuses its own recordings. Startup is what forces a state chain a legacy release left at 0775, and that repair is best-effort on two counts. It judges the chain DOWN from the state base rather than the requested level alone, because O_NOFOLLOW guards only the last name of an open: each level is lstated under its own name, and a level is repaired only when every level above it passed, so a sessions another user left as a link to /etc stops the descent before anything below it is opened — a root daemon no longer reaches through it to force 0700 on /etc/ssh. And it catches every step it takes, so EACCES, ENOENT and EIO from the lstat, the open, the fstat, the fchmod or the closing close are reported and stopped rather than thrown. A link, a name that is not a directory, and a level another uid owns are each reported the same way, as daemon_state_mode_repair_skipped; a level is asked what it IS before its mode, so a link carrying its own 0700 is reported too. Only mkdir keeps its own failures. A ~/.agent-device symlinked to another disk, and a container whose sessions/ belongs to the uid the volume was mounted with, therefore start exactly as they did before this change and answer every command. The repair never decides whether the daemon starts. What it ANSWERS is separate from what it forces: it reports whether the whole chain from the state base down to the level it was asked for is private ground — a real directory, this uid's, 0700, reached without following a link — and a script the DAEMON picks for itself, a default recording or a remote version, is written only on such a chain, refused with state_chain_untrusted otherwise. That refusal is the chain verdict's own, not the walk's: the walk judges ground and accepts a link standing in a parent others cannot write, so a sessions left as a link would otherwise carry a remote version outside the state tree. So the host whose state tree belongs to another uid keeps a working daemon and loses its daemon-picked recordings, which is the decision here: a level somebody else owns can be replaced by them at any moment, so it is not ground to publish a recording on. The hint names that host shape and both fixes (chown, or AGENT_DEVICE_STATE_DIR at a directory the daemon owns), and replay-e2e.md and remote-proxy.md say the same to users. A caller's --save-script=<path> is unaffected on every one of those hosts: that destination stays the walk's alone, and a recording under ground the daemon does not own is refused there, per recording, with script_ancestor_untrusted and its hint. Creation is best-effort too and happens BEFORE any chain is judged — one recursive mkdir builds the whole tail, which is what keeps a base that is a link working — so a chain the repair stops on can be left with an empty directory beyond the level it stopped at. Nothing is validated before that creation, and nothing is ever written into what it leaves.

A group-writable directory is refused outright, with a chmod g-w hint: the private-group exception is dropped, since membership cannot be proven where a name service answers outside the account files. Scripts are 0600. What sits outside the guarantee — ACLs, network mounts, a mount over a checked directory — is named in the ADR and the guide.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.33 MB 6.34 MB +8.0 kB
Package (unpacked) 6.33 MB 6.34 MB +8.0 kB
Package (download) 1.92 MB 1.92 MB +2.5 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 20.8 ms 20.2 ms -0.6 ms
CLI --help 45.2 ms 42.1 ms -3.2 ms

@thymikee

Copy link
Copy Markdown
Member Author

I found two confirmed gaps in the new ancestry rule at 02cfd69, and one docs claim that is wrong. CI is failing on the CodeQL alert described below, and there are no conflicts. The ancestry rule and the docs need to change before this can merge.

The rule trusts a directory from its mode bits and group only. untrustedWritableExposure and othersCreateEntriesIn never compare stats.uid with the current uid or root. Three real inputs pass. A sticky directory owned by another user (a teammate's chmod 1777 team) is exempted, yet its owner can rename or remove any entry. A 0755 directory owned by someone else that holds a component of your path lets that user rename the component and plant a symlink. An attacker-owned directory in sticky /tmp (for example /tmp/recordings, created before your mkdir -p) is silently accepted. The owner can also grant you write through a macOS ACL that mode bits do not show, or chmod it from 0777 to 0755 between the staging write and the second check. In each case the script is published into or under a directory another local user owns, which is the exposure #4565 closes. The rule should be: a component is trusted only when its owner is the current uid or root, and only then do the sticky and private-group rules apply. Please put that in untrustedWritableExposure (return an owner exposure before the sticky exemption) and make othersCreateEntriesIn treat such a directory as one others can create entries in. Host-kit tests that fake stats.uid should prove it, as the test file already does for the group database.

A symlink component can also bypass the rule. When the link's own parent is private, line 230 stats only the final target, and the walk continues up the lexical chain, so the ancestors of the real target are never checked. With ~/rec -> /srv/team/rec and /srv/team at 0775 group team without sticky, a teammate can swap /srv/team/rec for a link to a directory you can write, or for their own directory. Writing to /srv/team/rec/journey.ad directly is refused, but going through the link is not, so the docs sentence that a symbolic link inside a writable directory is refused is false here. The rule should be: every directory the kernel traverses to reach the destination's parent passes the ownership and mode test. Please keep the lexical lstat walk for planted links and also walk fs.realpathSync of the deepest existing ancestor. That also covers /tmp/shared/work/../x/journey.ad, where the lexical normalization hides a planted work link. A test with a private link whose target sits under a 0777 or 0775-shared directory should prove it. The existing test at atomic-file-ancestry.test.ts:192 shows only that a link is followed.

The docs misstate sticky semantics. replay-e2e.md:75 says a sticky team directory is safe, but the directory's owner can still rename and remove entries. Line 74 and remote-proxy.md:111 ("only you and root can write") describe a guarantee the code enforces from mode bits only, and the scriptAncestryFailure hint and the assertPrivateScriptDestination JSDoc repeat it. Once the owner check lands, please say that a sticky directory is accepted only when you or root own it, and that every directory must be owned by you or root. Fix the hint and the JSDoc in the same change.

Not blocking, take or leave: the local writer runs mkdirSync before any ancestry check (session-script-writer.ts:138) while the remote path asserts first, so a refused destination can leave a directory behind; the 'swapped ancestor' writer test (session-script-writer-ancestry.test.ts:109) repeats the world-writable branch and no test drives a swap after the identity is captured on the local route; privateGroupIds reads only /etc/group and /etc/passwd, so an LDAP or sssd private group is refused as shared; remote re-declares the two ancestry reason literals that host-kit owns and statDirectoryChain stats a single path; and atomic-publish-ownership.test.ts asserts source text.

The CodeQL alert on the quadratic /\/+$/ in session-script-destination.ts:420 still applies and fails the check, and this PR adds that file from #4511, so it counts against the PR even though it is outside the #4565 delta. Trimming slashes with new URL(baseUrl) or a loop clears it: #4584 (comment). The Coverage result has no captured failure. The new host-kit win32 and catch branches may trip a patch-coverage gate; the failing job log should show which.

Before merge, the ancestry rule should trust only directories owned by you or root, follow the resolved chain of any symlink, and the docs and hint should match. The CodeQL alert should be cleared too.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 3f17db5 to 450a915 Compare November 16, 2026 11:25
@thymikee

thymikee commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Thanks for the update. At 450a915 the CodeQL alert on the trailing-slash regex is gone, and all 21 checks pass. Two defects remain in the destination checks, and there are no conflicts. Both are traced from the source.

The new rule no longer judges every directory the path traverses, so atomic-file.ts:278 accepts destinations that the earlier revision refused. The spelled walk skips every component that is not a link, and the realpath walk covers only the final resolved chain. For example, take <root>/shared (0777, not sticky), <root>/shared/mine (0700, mine) and mine/rec as a link to ~/private. The destination <root>/shared/mine/rec/journey.ad passes. Another local user can rename mine and plant their own rec. The same gap exists for a directory reached through an intermediate link, and for a foreign-owned grand-ancestor of a link. This breaks the promise in replay-e2e.md:74 and remote-proxy.md:111, for both the local writer and the remote client. The rule should be that every directory the kernel traverses to reach the parent passes untrustedAncestryExposure. Enumerating them means taking directoryChain(fs.realpathSync(P)) for every existing prefix P of the spelled parent, deduplicating, and judging each entry. The current deepest-realpath walk becomes the last iteration, and assertNoPlantedLink keeps the link-stability rule. Please add a host-kit test for the 0777 shared / 0700 mine / rec link case that expects world_writable at <root>/shared, and a second test where the shared directory is an intermediate link's target. I assumed a same-parent rename needs write access on the parent only. That holds for Linux rename(2); macOS is unconfirmed.

The remote client refuses a path the local writer accepts. session-script-destination.ts:206 runs assertPrivateScriptDestination before the missing parent is created at :207. For open --save-script /tmp/rec/journey.ad with rec absent, the deepest existing directory is /tmp, so the new shared_parent rule fails the version with script_ancestor_untrusted, and every later version repeats it. The local writer creates the directory first (session-script-writer.ts:137), so it accepts the same path. This contradicts replay-e2e.md:73 and ADR 0016, and a remote recording to a fresh directory under /tmp is never delivered. Both routes should apply the rule at the same point relative to creating missing parents. The smallest fix is to drop the pre-mkdir assert at :206, because allocateStaging at :256 already judges after the mkdir and refuses a directory that another user created first. If you prefer to refuse before mkdir on both routes, please say so in the docs, since that reverses the promise that missing parents are created. Please add a remote test for a missing parent under a sticky directory that this user or root owns.

Not blocking, and you can take or leave these: the test at atomic-file-ancestry.test.ts:661 is named 'admits it with root named among its members' but asserts .toBeUndefined(), which means privateGroupId refuses it, so rename it or admit root; file.ts:4 exports othersCreateEntriesIn, privateGroupId, untrustedAncestryExposure, untrustedLinkExposure and four types that nothing outside host-kit uses, so keeping only assertPrivateScriptDestination, publishDirectoryIdentity and publishScriptFileSync would be enough; and the daemon test 'a recording refuses a directory another user made sticky and owns' (session-script-writer-ancestry.test.ts:155) puts the destination directly in the 1777 directory, which the old shared_parent rule already refused, so it could put the destination in a subdirectory and fake only the sticky directory's uid, or be dropped as a duplicate of the host-kit case.

The code-scanning thread on the trailing-slash regex no longer applies, because the regex is gone at this head, so please resolve it: #4584 (comment)

@thymikee

Copy link
Copy Markdown
Member Author

Head 546303c389 addresses both reviews.

Traversed directories. assertPrivateScriptDestination now judges every directory the kernel steps into, not the real path of the deepest existing ancestor alone. Each existing prefix of the spelled parent is expanded link by link, and the directory holding each link target is judged with it. Two cases that passed before now refuse, on the local writer and on the remote route: a plain directory under a 0777 one holding a link into a private destination, and ~/rec -> /srv/team/relay -> ~/chosen. Directories left through .. are judged as before.

Remote ordering. The remote client judged the chain before mkdir, so it refused the ordinary --save-script /tmp/rec/journey.ad shape while the local writer accepted it. The pre-mkdir call is gone; the check runs where the entry is really made, covered by a sticky-directory case.

Groups. privateGroupId now reads EVERY record carrying the numeric gid, not only the one named after the user, so ada:x:1000: beside team:x:1000:grace refuses. A duplicate-GID refusal, a same-gid alias admission, and a second-group primary refusal are covered. The residual risk is stated as reaching both ways: a name-service member can cause unsafe acceptance, not only refusal.

Scope of the claim. No ACL code. ADR 0016, the guide, and the remote page now name macOS and POSIX ACLs, network filesystems and mounts as outside the guarantee, and the unconditional confidentiality line is gone: the file is written 0600, that is not a promise about who can read it.

Hardening. The source-text check for the publisher entry point is deleted; the writer tests drive a real write and expect the typed refusals, so they cover dispatch by behaviour. The @agent-device/host-kit/file barrel keeps admission, identity capture and publication, and drops the six rule internals only its own tests used.

Checks: pnpm typecheck, pnpm lint, pnpm check:layering, pnpm check:production-exports, pnpm check:daemon-wire-compat, pnpm check:doc-links, pnpm check:doc-prose, pnpm check:adr-citations pass; the ancestry suites pass locally (106). Each new test fails on 450a915.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 546303c the link to a last-component target such as rec -> shared/relay is now refused, and the trailing-slash regex is gone. Three ways to steer the destination still pass the rule, so the problem from the earlier review is not fully closed.

First, traversedDirectories judges only realpath(dirname(target)) for a link hop, so a link in the middle of the target path hides its directory. With home/rec -> <root>/shared/relay/sub, shared at 0777 and not sticky, and shared/relay -> home/chosen, the destination home/rec/journey.ad is accepted, and so is ../shared/relay/sub. Any local user who can write shared can swap relay between the check and the rename, on both the local writer and the remote client. This is the #4565 exposure and it contradicts the docs and ADR 0016. The rule should be that every directory where the kernel looks up a name, including the components of every link target at any depth, passes untrustedAncestryExposure. A walk that expands link targets component by component enumerates them; a realpath of the target's dirname does not. Please add a host-kit, daemon and remote test with rec -> shared/relay/sub that expects world_writable at shared.

Second, link stability is asked only by assertNoPlantedLink on the spelled path. Hop links are followed but their owner is never checked. Take ~/rec -> /tmp/relay, where another user created /tmp/relay in sticky /tmp pointing at ~/chosen. The spelled link is in a private home and passes, and /private/tmp is sticky and root-owned, so the foreign link is never asked. Spelling /tmp/relay/journey.ad directly is refused with planted_link, so one route gets two answers. The owner can replace the link after the check and redirect the publish. The rule should be that every link the kernel follows, spelled, hop, or inside a target, passes untrustedLinkExposure against the directory that holds it, judged in the same walk that enumerates directories. Please add a test that fakes the uid of a hop link in a sticky directory, as the existing tests fake ownership. I confirmed this from the code path only, because I could not create a link owned by another uid without root.

Third, resolvedDirectory uses JS fs.realpathSync, which folds .. lexically, while the kernel applies .. to the link's target. With home/work -> priv/x, priv/y at 0777 and home/y at 0700, the destination <root>/home/work/../y/journey.ad judges home/y and is accepted, yet the file is created in priv/y. Spelling priv/y/journey.ad directly is refused with world_writable. Absolute --save-script paths reach the rule unnormalized, so both routes are affected, and this breaks the .. claim in replay-e2e.md and ADR 0016. The rule should be that .. applies to the directory the walk is actually in, never to the spelled string. Please add a test with the home/work/../y shape that expects world_writable at priv/y.

Could one component walker replace assertNoPlantedLink, traversedDirectories, resolvedDirectory, linkTarget and the realpath calls? Start at / with the spelled components on a queue. For each component, judge the current directory with untrustedAncestryExposure, deduplicated. On .., move to parentDirectory(current). Otherwise lstat current/name: judge a link with untrustedLinkExposure against current, count one hop, and put its target components at the front of the queue, resetting to / for an absolute target. A directory becomes current, and an absent entry stops the walk with current as deepest. Then ask othersCreateEntriesIn(deepest). That one loop closes all three by construction and needs no realpath.

The Repo Guards & Compatibility check fails in Run Fallow audit on complexity of traversedDirectories (cognitive 35) and privateGroupId (cognitive 17), both added or rewritten in this delta. All checks passed at the earlier head. The one-walker design would also clear the first. There are no conflicts.

The github-advanced-security thread on the trailing-slash regex no longer applies: parentDirectory now uses endsWith/lastIndexOf, so it can be resolved.

Not blocking, take or leave: the four-line comment at atomic-publish-ownership.test.ts:31 that points to another test file can be deleted.

The shapes above were probed against the head atomic-file.ts on macOS.

Before merge, the two link walks need to become one kernel-lookup walk that judges every looked-up directory and every followed link, with the three regression shapes added and the Fallow audit passing.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 7041497 to 4964b44 Compare November 16, 2026 13:32
@thymikee

Copy link
Copy Markdown
Member Author

Yes — one walker replaces all five, and it is now the only thing that resolves a destination. Head: 4964b44.

The one loop, and what it cost. walkParent plus six named steps (startWalk, standIn, pathComponents, stepWalk, stepIntoEntry, stepThroughLink) is 92 code lines, 165 with the doc block that states the rules. What it deleted was also 92 code lines: traversedDirectories (36), assertNoPlantedLink (18), resolvedDirectory (10), directoryChain (10), a duplicate lstatIfPresentSync (8), parentDirectory (6) and linkTarget (4). So the walk is line-for-line parity with the four passes it replaced, and no realpath call remains in the file. path.dirname is used where a parent is needed, and .. is applied to state.deepest — the directory physically stood in — never to the spelling.

atomic-file.ts is 778 lines (was 667). The net production delta for this round is +126 lines: +111 here and +15 in the writer, where the two failure wrappers shared an 11-line clone. Almost all of it is the group exception, which went from 33 code lines to 98: /etc/nsswitch.conf is now a condition, the account name is read out of the same passwd database the rule judges rather than from a second host lookup, and the record parsing is split into named steps so neither decision is over the complexity floor. assertNoPlantedLink, traversedDirectories, resolvedDirectory, linkTarget, parentDirectory and directoryChain are gone, and the two oxlint-disable lines with them.

The three shapes, at all three levels. Each is refused at 4964b44 and each fails at 546303c:

  • rec -> shared/relay/sub, shared 0777 not sticky, relay itself a link: world_writable at shared. Host-kit refuses the ground under a link that leads into a shared directory, writer a recording refuses the ground a link target crosses on the way in, remote a remote recording refuses the ground a link target crosses on the way in. refuses the spelled spelling of a link the walk also follows holds the two routes to one answer.
  • ~/rec -> <tmp>/relay -> ~/chosen, relay another uid's file in sticky <tmp>: planted_link at relay. Writer and remote versions named for it, and host-kit refuses a link whose target stands in ground another local user owns. The link's uid is served by a fixture (serveHostOwnership in test-kit, which the sticky-directory test now uses too), so nothing here depends on the account the suite runs under — that also settles the root case you raised.
  • home/work/../y with home/work -> priv/x and priv/y 0777: world_writable at priv/y. Host-kit applies .. to the directory the walk is in, not to the spelling, writer a recording refuses the directory a .. steps back into, remote a remote recording refuses the directory a .. steps back into.

Two hardening items rode along: the hop count is refused as link_loop_exceeded at 30, kept below what the kernels apply themselves (32 on macOS, 40 on Linux) so this rule always reaches its limit first and a loop is never a pass; and a component that is neither directory nor link is refused as not_a_directory instead of reaching the rename. A link's own mode is never consulted — Linux reports one mode for every link — so what is judged is the directory the walk lands in, plus the ownership of the link and of the directory holding it, which is why an alias at 0777 into a 0700 directory now publishes.

The group exception fails closed. It is admitted only where /etc/nsswitch.conf exists, is readable, and lists files as the only source for both passwd and group. systemd is refused as well, not kept: systemd-userdbd answers membership from records these two files never show, which is exactly the member the check could not read. Bracketed actions and trailing comments are ignored; a missing line, an unreadable file, or any other source ends the claim and a group-writable directory is treated as shared. Tests cover ldap, sss, systemd, a source-less line, and a missing nsswitch.conf. The "can cause unsafe acceptance" paragraph is out of ADR 0016 and the guide, replaced by the condition.

The unused configurable mode on the script publisher is gone (SCRIPT_FILE_MODE is 0600), and the four-line comment at atomic-publish-ownership.test.ts:31 is deleted.

Fallow. FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all exits 0: dead code 0, complexity 0, duplication 0 over 19 changed files, with no baseline edits and no suppressions. traversedDirectories is gone with the second half of its cognitive load, and privateGroupId now delegates parsing (parseGroupRecord, parsePasswdRecord, nsswitchLine) to leave a pure decision in admittedPrivateGid. Two fixtures that had forced ENOTDIR with a file where a directory must be now use an owned but unwritable directory, so they still exercise the filesystem's own failure rather than being intercepted earlier by the walk.

Checks. CI is green at 4964b44, including Repo Guards & Compatibility, Coverage, Integration Tests, and the doc gates. Locally: pnpm lint, pnpm typecheck, pnpm check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose and check:doc-links pass. pnpm check:affected --run --head HEAD is queued behind the host validation lock here, so I have not seen it terminate locally; CI ran the suites. One test file is split: atomic-file-ancestry.test.ts is 872 lines and the account-database decisions are in atomic-file-private-group.test.ts beside it, under the 1,000-line cap.

Reverting only atomic-file.ts to 546303c and re-running the new tests: 11 failed / 57 passed in host-kit, 7 failed / 93 passed across the writer, close-path and remote files. Nothing new passes without the walker.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 4964b44 the Fallow failure from the earlier round is fixed and all 21 checks are green. The quadratic trailing-slash regex is also gone from session-script-destination.ts, so that scanning thread is fixed. There are no conflicts. One new defect blocks merge: relative link targets skip the walk.

stepThroughLink queues pathComponents(path.join(state.deepest, target)) for a relative target. That is an absolute path, but the walk then sets home = state.deepest (atomic-file.ts:390). The walk looks up <deepest>/<first absolute component>, finds nothing, and stops at the link's own directory. The target and the directories on the way to it are never judged. path.join also folds a .. in the target. I probed this on a scratch copy of the head file with stubbed imports, on macOS. home/rel -> ../shared/x with shared at 0777 is accepted, while 546303c refused it. home/rel2 -> open with home/open at 0777 is accepted, while spelling home/open is refused. The relative forms of the earlier shapes 1 and 3 are accepted too. This reopens #4565 for the common ln -s ../team/rec rec form, and it makes the promise in replay-e2e.md and ADR 0016 untrue. The rule: resolve a link target as the kernel does, from / for an absolute target and from the directory that holds the link for a relative one, applying each .. in the walk. Queueing the raw target components ([...pathComponents(target), ...rest]) should do it. Please add a relative-target case at host-kit level for rec -> ../shared/relay/sub and for a .. in the target. Please add one relative-target case on the writer or remote route.

lstatIfPresentSync (atomic-file.ts:739) has the same body as the exported lstatIfPresent in verified-file.ts:136. A third private copy sits at :771. Your comment says the duplicate was deleted, but one copy remains. Please import the shared helper, use it at :364 and :760, and delete both local copies. Is there a reason to keep any of them?

Not blocking, take or leave: the assertPrivateScriptDestination JSDoc at :195-198 and :215-216 still describes the deleted design and contradicts the new not_a_directory refusal, and the scriptAncestryFailure text says "owns or can write" for link_loop_exceeded and not_a_directory, which are neither.

Shape 2 (a foreign-owned hop link in a sticky directory) was traced from the code only, because an unprivileged process cannot create it. The probe confirms that 546303c accepts shapes 1 and 3. The change is 715 net production lines, just over the usual size line. There is no smaller owner beyond the two cleanups above.

Before merge, the relative-target walk must be fixed with regressions, and the duplicate lstat helpers must go.

@thymikee

Copy link
Copy Markdown
Member Author

All five are in. Head: 93156c8. Each one is a test that fails at 4964b44: reverting the production files to that commit turns 13 red across 46 files, and all 13 pass here. Two of them are shapes this change itself exposed, at the end.

1. The relative target. stepThroughLink now queues [...pathComponents(target), ...rest] and picks the starting directory by the target's own spelling: path.isAbsolute(target) ? '/' : path.dirname(link). Nothing about a target goes through path.join or path.resolve any more, so a .. inside one is applied one component at a time to the directory the walk has physically reached. ln -s ../team/rec rec is refused for the same reason the spelled-out chain is — writer, remote and host-kit each have a test for it — and priv/../x refuses at the shared ground the .. steps back into. Two notes from writing them: the fixture has to spell that target literally (fs.symlinkSync('priv/../x', …)), because path.join folds the .. away before the link exists; and the walk's answer for a relative target is now tested where it lands as well as where it refuses.

2. Staging escaped the judged directory. assertPrivateScriptDestination returns the directory it verified, and that value is what everything stages in: publishScriptFileSync builds its temp path with createAtomicPublishTempPath(judged, destination), re-walks, and refuses parent_replaced if the answer moved; allocateStaging mkdtemps inside it and keeps its identity for publishStaged. No parent is re-derived by path.dirname/path.join/path.resolve on either path — withAtomicPublishTempPathSync (the other publisher, which has no walk) keeps its path.dirname and that is the only remaining one. spelledParent is now plain path.dirname, used only by the walk.

Two consequences worth naming. The parent identity is compared against walk.deepest, not the spelled parent, so a .. destination no longer reports a replacement that never happened, and a refusal now names the directory the entry would really have been created in — elsewhere, not the link that points at it; two existing tests are updated to that. And the daemon writer stopped passing parentIdentity at all: the identity it had was taken from path.dirname(scriptPath), which is the wrong directory for exactly the destinations this round is about, so publishHealedScriptAtomically no longer takes the parameter. The remote path still passes its own, captured where staging was created, which is the pair worth comparing across a download. Both staging tests assert on where the bytes went: expect(staged).toEqual([privY]) with the shared home/y still empty.

3. initgroups:. nameServiceListsOnlyFiles checks passwd, group and initgroups, where a missing initgroups line falls back to the verified group line — what the kernel does — and an explicit one has to be files-only. Tests name sss, ldap files and systemd, and hold the inherited and initgroups: files cases at the same answer.

4. A bare destination. spelledParent('journey.ad') is . now, and startWalk resolves any non-absolute parent with path.resolve(''), so assertPrivateScriptDestination('journey.ad', publishDirectoryIdentity('.')) answers the cwd instead of starting at / and then refusing itself. path.resolve('') rather than process.cwd() because R86 keeps host-kit from reading the latter.

5. Judged once. WalkGround { stats: Map<string, fs.Stats>; judged: Set<string> } travels in WalkState: judged keyed by dev:ino so an alias or a .. does not re-judge ground already passed, stats keyed by path so the host is asked once per directory per walk. WalkOutcome and the stop step carry the walk's DirectoryAuthority, which is what othersCreateEntriesIn is asked with — the post-walk statDirectoryChain(walk.deepest) is gone, so the deepest directory is not read twice either. The test counts fs.statSync calls over home/a/../b/sub where a and b are two links to one directory and asserts every read is a distinct path; at 4964b44 that is 14 reads over 7 directories.

Also folded in: untrustedGroundWording gives each exposure its own wording so a link_loop_exceeded no longer claims someone else owns the path, and one 13-line clone fallow found between the two temp-path cleanups is closed by routing both through removeTempBestEffort.

One thing the first push broke, and a fourth shape it exposed. check:affected caught two suites this head now fails, both for reasons the rule itself owns — separate commit 93156c8:

  • isAtomicPublishTemporaryPath compared path.dirname(temp) to path.dirname(destination) as strings. The temp file lives in the directory the walk judged, which on macOS is /private/var/... while the destination is spelled /var/.... So the predicate stopped recognizing temp files produced for its own destination: the six filesystem-boundary-faults rows for the script writer and the session store went red (an injected EIO/ENOSPC/EMFILE at the write was no longer claimed), and any cleanup that names those files would have left them behind. It resolves both directories and compares them as directories. Test: recognizes a temp path spelled through the directory the entry is really created in, an alias in a scratch root so it is not macOS-specific.
  • Importing lstatIfPresent from verified-file.ts as planned made 64 top-level statements of eager work load on the daemon's startup path (host-kit/process.ts → owned-process-record.ts → atomic-file.ts → verified-file.ts), which eager-closure-budgets rightly refuses: a module the merge-base tree already had cannot be made eager by a split, and APPROVED_CLOSURE_ADDITIONS is not the honest fix for a nine-line helper. The helper now lives in atomic-file.ts and verified-file.ts imports it — the verified-file guards already sit behind this module via the façade, so the edge count is back to what it was. A module of its own was tried first; it grows nine capture-kit closures by one module and 49 statements, also red.

Delta. atomic-file.ts is 890 lines (was 778). Net production across the five files is +105: +112 in atomic-file.ts, −9 in verified-file.ts, +7 in the remote client, −5 in the writer. The growth is the return value and its contract (the doc block that says stage here is most of it), WalkGround, the wording table and the resolved-directory comparison; the lstat copies and the duplicated cleanup went the other way. atomic-file-ancestry.test.ts is over the cap with the new tests, so the three pure decisions moved to atomic-file-decisions.test.ts beside it (919 / 113 lines).

Checks. Reverting the five production files to 4964b44 turns 13 tests red across 46 files (the 12 above plus the temp-path one), and all 13 pass here. Focused: 46 files, 623 passed, 7 skipped, including filesystem-boundary-faults and eager-closure-budgets. pnpm format, lint, typecheck, check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose and check:doc-links pass. pnpm check:affected --run --head HEAD terminates green at this head: lint, typecheck, layering, fallow, build, vitest-related (90 files, 415 tests) and unit (2126 files, 20135 passed, 10 skipped), then daemon-wire-compat, adr-citations and both doc gates. FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all exits 0 — dead code 0, complexity 0, duplication 0 over 22 changed files, no baseline edits and no suppressions. ADR 0016 and the guide carry the new claims: raw components and the link's own directory, the walk answering the staging directory, one judgment per directory, and initgroups in the name-service condition.

@apex-sentinel-staging

apex-sentinel-staging Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve

Since the last review: 0 new · 0 still open · 1 no longer raised (1 marked fixed)

No open findings.

Reviewed with this repository's own Sentinel configuration (a30accf38081).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

Earlier reviews

Review of fa64dfe5 · 17.11.2026 02:10 UTC

Apex Sentinel · ✅ Approve

Since the last review: 1 new · 0 still open · 0 no longer raised

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/daemon-contracts/src/daemon-registration-owner.ts:378 🆕 ADR 0016 claim that a root container with foreign sessions/ still has every command working is false for recordings

2 findings below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (a30accf38081).

Review of 8cd5b8ac · 17.11.2026 01:13 UTC

Apex Sentinel · ✅ Approve

Since the last review: 0 new · 0 still open · 0 no longer raised

No open findings.

1 finding below minor hidden (user_settings.min_severity).

1 new finding away from what changed since 4b630c1 not shown. Comment @apex-sentinel-staging review for a full review.

Reviewed with this repository's own Sentinel configuration (a30accf38081).

Review of 4b630c1f · 17.11.2026 00:39 UTC

Apex Sentinel · ✅ Approve

Since the last review: 0 new · 0 still open · 3 no longer raised (1 marked fixed)

No open findings.

Reviewed with this repository's own Sentinel configuration (a30accf38081).

Review of 45d4eb97 · 16.11.2026 23:50 UTC

Apex Sentinel · ✅ Approve

Since the last review: 3 new · 0 still open · 0 no longer raised

3 finding(s): 3 minor.

🟡 Minor (3)

Where Finding
packages/daemon-contracts/src/daemon-registration-owner.ts:295 🆕 ensurePrivateDaemonStateDir creates state levels through a recursive mkdir, crossing planted links
packages/daemon-contracts/src/daemon-registration-owner.ts:355 🆕 Daemon startup now dies on a foreign-owned state dir it could previously write
packages/daemon-contracts/src/daemon-registration-owner.ts:423 🆕 State-repair refusals pass an untyped reason: string, unlike the union the rule uses

Reviewed with this repository's own Sentinel configuration (a30accf38081).

Review of 235d0db4 · 16.11.2026 20:55 UTC

Apex Sentinel · ✅ Approve

Since the last review: 0 new · 0 still open · 0 no longer raised

No open findings.

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (ea414582b7fa).

Review of c852884d · 16.11.2026 19:44 UTC

Apex Sentinel · ✅ Approve

Since the last review: 0 new · 0 still open · 0 no longer raised

No open findings.

Reviewed with this repository's own Sentinel configuration (ea414582b7fa).

Review of d86ee29f · 16.11.2026 17:49 UTC

Apex Sentinel · ✅ Approve

Since the last review: 0 new · 0 still open · 1 no longer raised (1 marked fixed)

No open findings.

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (cb335952017e).

Review of 7ed8e267 · 16.11.2026 16:35 UTC

Apex Sentinel · ✅ Approve

Since the last review: 1 new · 0 still open · 1 no longer raised (1 marked fixed)

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/remote/src/session-script-destination.ts:372 🆕 Remote route still creates the entry through the unverified spelled destination

Reviewed with this repository's own Sentinel configuration (244d1ff68344).

Review of 49ada505 · 16.11.2026 15:48 UTC

Apex Sentinel · ✅ Approve

Since the last review: 1 new · 0 still open · 0 no longer raised

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/remote/src/session-script-destination.ts:201 🆕 Missing parent created before the ancestry rule judges it

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (cfe08502f0f6).

Review of 93156c82 · 16.11.2026 15:17 UTC

Apex Sentinel · ✅ Approve

No open findings.

Reviewed with this repository's own Sentinel configuration (cfe08502f0f6).

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 93156c8 to 49ada50 Compare November 16, 2026 15:38
@thymikee

Copy link
Copy Markdown
Member Author

All six are in. Head: 49ada50, rebased onto origin/main so the branch now carries #4586 — the push is a force-push, and 54eefe8 below is the old 93156c8 with that rebase applied. Reverting the production files to 54eefe8 (this branch before the round) turns the two behaviour tests in items 1 and 4 red. Items 3 and 5 are tests that could not fail before, so each is proved by removing the rule it pins; items 2 and 6 are a split and a deletion, covered rather than pre-failed.

1. The relative destination (blocker). absoluteParent replaces spelledParent: a relative destination is queued as the resolved working directory's components with the destination's own components following them RAW, and startWalk always starts at /. So cd /shared/mine under a /shared at 0777 and not sticky is now refused at /shared instead of judging mine and nothing above it. The test publishes through the exported publishScriptFileSync exactly as you reproduced it — refuses a relative destination whose working directory sits in shared ground, with the cwd restored in a finally — and a companion holds the accepting case (answers the working directory for a relative destination over a safe chain), asserting the absolute cwd is what comes back. path.resolve(destination) is not used anywhere in this path: it folds, which is the whole reason the walk exists.

2. The test file is split. daemon-artifacts-script-ancestry.test.ts (369 lines) holds the eleven remote ancestry and refusal cases; daemon-artifacts-script-download.test.ts is 594. The client helper, the fake artifact daemon and the record helpers move to __tests__/support/script-publication-fixtures.ts (127) beside the two files, following the existing artifact-download-fixtures.ts convention — and the server is now closed by onTestFinished inside artifactServer, so no per-file registry is duplicated. Both names satisfy R84 as <module>-<aspect> for daemon-artifacts.ts; the file held 41 tests before the split and holds 41 after (30 + 11), with no case dropped or renamed. Lint is green with no suppression and no limit change, and #4586's retirement rows stayed in the download file untouched.

3. The alias test now walks two routes. a and b both name sub, and the spelling is home/a/../b/below/journey.ad, so sub is genuinely entered twice and the walk ends below it. It asserts assertPrivateScriptDestination(...) === below — that's the assertion that the second alias was really visited, since a walk that gave up answers home — and then that no path is read twice. Disabling the ground.stats cache fails it (22 reads over 8 distinct paths); the previous version stayed green with the cache gone.

4. The temp predicate compares directories, not spellings. Two things were wrong: it compared path text, and it used realpathSync, which applies a spelled .. lexically — for home/work/../y it answers home/y, not priv/y. So it rejected the real sibling in priv/y and accepted a look-alike in home/y. sameDirectory now stats both and compares dev:ino, and stat resolves .. the way the kernel does, so no realpath is involved. Test: recognizes a temp sibling reached through a .. that steps back from a link, asserting true for the real sibling and false for the planted one.

5. The three writer tests ask about one directory. All three now call serveHostOwnership on the directory at issue and assert the ancestor named (foreign, team, tmp/recordings). No process.getuid mock is left in that file, so nothing depends on who runs the suite. Removing the ownership half of untrustedAncestryExposure fails exactly those three.

6. parentIdentity is gone from publishScriptFileSync. The publisher takes the identity from the directory the walk judged, so an outside caller cannot steer what the comparison is against; the only remaining caller is the remote client, which legitimately captures one before its download. The externally captured identity is tested where it is actually honoured: assertPrivateScriptDestination(destination, publishDirectoryIdentity(staged)) refuses a replaced parent naming the linked-to directory, and accepts the identity of the directory the walk answers.

Delta. This round is production +17 (atomic-file.ts 890 → 907), nothing else in production. Across the PR, the five production files the rule touches go from 1,416 lines at merge-base to 2,223: atomic-file.ts 170 → 907, session-script-destination.ts 490 → 555, the writer 573 → 584, file.ts 31 → 34, verified-file.ts 152 → 143.

Checks. pnpm lint, format, typecheck, check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose, check:doc-links pass; FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all is dead code 0 · complexity 0 · duplication 0 over 24 files, no baseline edits, no suppressions. Focused: 63 files, 1,641 passed, 7 skipped, including the remote suites, filesystem-boundary-faults and eager-closure-budgets. pnpm check:affected --run --head HEAD terminates green: lint, typecheck, layering, fallow, build, vitest-related (90 files, 415 tests) and unit (2,127 files, 20,194 passed, 10 skipped), then daemon-wire-compat, adr-citations and both doc gates. ADR 0016 and the guide carry the relative-destination rule and the fourth shape.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Apex Sentinel: 1 new finding on 49ada50. The verdict and the full list are in the summary comment.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 49ada50 to 7ed8e26 Compare November 16, 2026 16:27

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Apex Sentinel: 1 new finding on 7ed8e26. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

All four review items plus both review threads are in. Head: d86ee29 (on origin/main, carrying #4582). Each item below says how it is proved: a revert to 49ada50, or a perturbation of the rule the test pins where a revert cannot show anything.

1. The two chmod(0500) fixtures are gone (session-script-writer.test.ts, session-close-script.test.ts). Both now inject a deterministic EACCES at the staging-write seam, through one support module, daemon/src/__tests__/support/script-staging-failure.ts. It decides which write fails with host-kit's own isAtomicPublishTemporaryPath, so it does not rebuild the temp name, and the real writeFileSync serves every other call. Nothing about either test now depends on who runs the suite. Proved by the fixture rather than a revert — the tests passed at 49ada50 on a non-root host, which is the point: disabling the injection fails exactly those two.

2. The walk no longer trusts Node's remembered working directory, and both publishers create the entry in the judged directory.

  • absoluteParent confirms the spelling it queues is where the process stands — dev:ino of the resolved cwd against stat('.') — and refuses with parent_replaced on mismatch. Writing this fixture taught me something: Node answers the working directory from the first query after chdir, so the drift exists only once something has asked. The test settles that cache, renames the directory and plants a private one at the old path, which is the sequence a real process walks into.
  • The local publisher creates the entry at path.join(judged, basename(destination)). The remote publisher does the same in publishStaged, taking the directory from the judgment it already makes under the record lock, so nothing new is stored in the record.

Tests, all through the exported publisher: refuses a relative destination whose working directory has been moved out from under it, publishes a relative destination into the working directory it judged (asserts the path handed to renameSync), and refuses a relative destination whose working directory sits in shared ground, which now does go through publishScriptFileSync as my last reply claimed it did. The first two fail on 49ada50; separately, dropping the cwd confirmation fails the first and publishing through the spelled destination fails the second. The third is a strengthening — it passed at 49ada50 by the assertion helper, and now exercises the publisher.

3. ground.judged is deleted, with its doc comment, the walk's once-per-inode claim and the ADR sentence. All tests stay green without it, which is the finding: only the per-path stat cache is observable, and removing that fails the alias test (22 reads over 8 distinct paths).

4. The hop test states its numbers. A 30-link chain publishes, a 31-link chain is refused naming link30. MAX_SCRIPT_PATH_LINK_HOPS is imported only for a closing expect(…).toBe(30) that ties the literal to the constant. Lowering the constant to 29 fails this test; the derived version could not fail.

5. Missing parents are refused before they are created, on both routes, with the same walker. assertPrivateScriptDestinationPrefix is the pre-pass: the same walkParent, which ends at the first component that does not exist, so it judges exactly the prefix already there. Both routes run it before their mkdir, and the post-mkdir judgment is unchanged. The creation question is deliberately only asked after the mkdir — below the pre-pass those directories are the caller's own to create — which is what keeps mkdir -p <sticky shared>/recordings acceptable. Tests: a refused destination with a missing parent leaves no directories behind (local) and a remote recording refuses a destination with a missing parent and creates no directories. Both fail on 49ada50 with ['recordings'] where [] is expected. Thread resolved.

Remote entry path (discussion_r4588529567, fixed in the same commit): a remote recording creates the entry in the directory the walk judged, not the spelled one. work -> priv/x; between the last judgment and the link, work is repointed at a second private tree, so the spelled path answers elsewhere/y and nothing about it would raise an ownership refusal. The test asserts the recording is in priv/y and the swapped-to tree is empty. Restoring current.destination on that one line fails it with the wrong-directory landing, not an error.

Wire ledger. publishScriptVersion moved, so the digest is pasted as the coverage job asked, with a compatibleChanges rationale and no protocol bump: the declaration gained one destination check before its mkdir; no request field, response field, route, status or artifact framing changes, so a released peer sends and parses exactly the same bytes. npx vitest run test/wire-compat passes (51 tests) and check:daemon-wire-compat is clean against v0.21.37, protocol unchanged.

One split rode along. The new host-kit cases pushed atomic-file-ancestry.test.ts past the 1,000-line cap, so shared host fixtures moved to atomic-file-host.fixtures.ts (119 lines) beside the existing tmp-dir.fixtures.ts; the ancestry file is 951. The ownership-serving helper stayed inside the test file on purpose: as a shared module it clones test-kit's serveHostOwnership (fallow flagged the 6-line clone), and host-kit cannot depend on test-kit without inverting the package edge.

Delta. Production +68 this round: atomic-file.ts 907 → 959, the writer 584 → 591, the remote destination 555 → 563, the façade 34 → 35. Across the PR those five files go 1,416 at merge-base → 2,291.

Checks. pnpm lint, format, typecheck, check:layering, check:production-exports, check:daemon-wire-compat, check:agent-guidance, check:adr-citations, check:doc-prose, check:doc-links pass; FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all is dead code 0 · complexity 0 · duplication 0 over 26 files, no baseline edits, no suppressions. Focused: 65 files, 1,697 passed, 7 skipped, including both wire-compat lanes. ADR 0016 carries the two-question walk, the cwd confirmation, and that neither publisher hands the spelled destination to the kernel; the guide says a refused destination leaves no new directories behind.

@thymikee

Copy link
Copy Markdown
Member Author

I reviewed d86ee29 and found two problems to fix before merge. Compared with the earlier review, the shared-ancestor and spelled-destination issues are fixed. The 20 CI checks pass at this commit, and there are no conflicts.

The docs promise that a refused save leaves no directories behind (see replay-e2e.md and ADR 0016), but the code does not keep it. Both routes create the missing parent with no mode: session-script-writer.ts:144 and session-script-destination.ts:208. Under umask 002, the new directory is 0775. Without an admitted private group, the check that runs right after the mkdir refuses it as shared_group_writable. The earlier prefix check only judged ground that existed before. On the head file with process.umask(0o002), I got refused shared_group_writable .../home/recordings and recordings (775) stayed behind. On a umask-002 host, every save into a new directory fails and leaves a stray group-writable directory. The rule: every directory a script publish creates must itself pass the rule it is judged by. Please create missing parents with mode: 0o700 on both routes. A recursive mkdir applies the mode to every directory it creates. Please add one regression per route under process.umask(0o002) that asserts the save succeeds and the created parent is 0700.

nameServiceListsOnlyFiles admits only files for passwd, group and initgroups. Stock Ubuntu and Debian ship passwd: files systemd and group: files systemd, so privateGroupId returns undefined there. Those users run with umask 002, so every directory they create is 0775 with their private group, including git clones. untrustedAncestryExposure then refuses each one as shared_group_writable. I believe the docs' own example, --save-script ./workflows/e2e-settings.ad inside a cloned repo, is refused on stock Ubuntu. Before this PR the local route had no check, so this is a new false refusal. The exception that ADR 0016 justifies by the shape Ubuntu gives home directories admits nothing there, and the group, passwd and nsswitch parsing buys nothing on its target platform. Please resolve this at its owner. Either admit systemd as a source (userdb records come from root-owned drop-ins, the same trust level as /etc/group), or drop the exception, refuse group-writable directories with a chmod g-w hint, and rely on the 0700 creation above. Please run it once on a stock Ubuntu 24.04 install (not a Docker base image, which lists only files) against a 0775 project directory and show the save outcome.

Open threads: neither earlier inline thread still applies, so please resolve both. shared ancestor before mkdir is fixed by the prefix check before the mkdir (the leftover under umask 002 is the first problem above, a different mechanism). publish destination spelling is fixed because publishStaged now creates the file under the directory judged under the lock.

The walk itself is justified, since a per-component O_NOFOLLOW open cannot carry the create without openat. Production code grew from about 715 to about 875 net lines across five files, and the growth is patches around the walk. Would these cuts make it smaller and better owned? (1) Both callers already resolve an absolute destination, so could host-kit refuse a non-absolute one at its entry and delete the cwd branch of absoluteParent, standsIn and WalkStart? (2) Could host-kit own parent creation as one call (walk, mkdir the missing tail at 0700 inside the judged directory, judge again)? That removes the exported assertPrivateScriptDestinationPrefix seam and the two hand-sequenced copies, and makes "no directories left behind" true by construction. (3) Could the private-group exception be resolved one way, as above? (4) Could publishScriptFileSync keep one defense-in-depth comparison, either the parentIdentity check in the walk or the stillJudged !== judged check, since ADR 0016 says permissions, not re-checks, close the window? Please amend ADR 0016 first for the 0700 creation rule and the private-group decision.

Not blocking, take or leave: the relative-destination half of absoluteParent cannot fire from production because both callers pass absolute paths, and the publishScriptFileSync JSDoc at line 53 still documents the removed parentIdentity option.

The Ubuntu case comes from tracing the code and a macOS umask-002 reproduction, not a live Linux run.

Before merge, please create missing script parents at 0700 on both routes with the umask-002 regressions, and decide the private-group exception for stock Ubuntu.

@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from d86ee29 to c852884 Compare November 16, 2026 19:36
@thymikee

Copy link
Copy Markdown
Member Author

Redone as one smaller rule, with ADR 0016 amended to match in c852884 (this round is squashed into that one commit). Both blockers are fixed and all four cuts are taken.

Blocker 1 — created directories now pass the rule they are judged by. mkdir with no mode is gone from both routes; host-kit creates at 0o700 and a recursive mkdir applies that mode to every level it makes, so a umask-002 host no longer creates 0775 and then refuses it. Your repro, run against d86ee29's packages/host-kit/src/file.ts on this macOS host with process.umask(0o002) and the writer's own sequence:

written: false
message: Refusing to save the script under .../home/recordings: another local user can write that part of the path.
details: {"reason":"script_ancestor_untrusted","exposure":"shared_group_writable",
          "ancestor":".../home/recordings", ...}
left behind recordings: 775

Same sequence at this head: the save goes through and both new levels are 700. Regressions: a recording creates a missing parent at 0700 on a shared umask (local route) and a remote recording creates a missing parent at 0700 on a shared umask (remote route), each asserting the save succeeds and both created levels are 0700. a refused destination with a missing parent leaves no directories behind and a recording that steps back out of a missing directory leaves no directories behind cover the leftovers, including the private/missing/../../shared/recordings shape.

Blocker 2 — the private-group exception is dropped, not widened to systemd. I took your second option. I did not go with systemd because I could not turn "userdb drop-ins are root-owned" into something the code can check: on the box that motivated the exception, membership is answered by a service, and /etc/group says nothing about whether it agrees, so an admitted systemd line would be a guess about the host's drop-ins. What replaced it: the mode alone decides, a group-writable directory is refused as shared_group_writable, and the hint names the directory and the command. Every line of /etc/group, /etc/passwd and /etc/nsswitch.conf parsing is deleted, along with the 181-line suite that tested it, so the answer no longer depends on which name services a host runs. The 0700 creation covers new directories, which is the half that mattered operationally.

outcome: refused
  message : Refusing to save the script under .../my-repo: the group can write that part of the path, and agent-device cannot tell who else belongs to that group.
  exposure: shared_group_writable | reason: script_ancestor_untrusted
  hint    : Close the directory to its group with `chmod g-w .../my-repo`, or save the script under a directory only you can write.
created under a private parent: workflows=700
project dir untouched: no directories left behind

That is a project directory at 0775 in this user's own primary group, umask 002: refusal with the hint and nothing created, then a save under a private parent at 0700. I could not run it on stock Ubuntu 24.04 — this environment has no Linux host, and no Docker, Lima, multipass or Vagrant to start one, so I did not fake it with a container either. The output above is the same code path on macOS, and a recording refuses a group-writable home and says how to close it pins the Ubuntu case as a unit test (a 0775 directory in this user's own group, refused with chmod g-w in the hint) — which is deterministic precisely because nothing reads an account database any more.

The four smaller-design questions

  1. Refuse a non-absolute destination at the entry, delete the cwd branch. Yes, done. spelledParent refuses a relative destination with a typed relative_destination, and absoluteParent, standsIn, WalkStart and the dev:ino-against-. guard are deleted — 52 lines out, 14 back. Both callers already expand the path (expandSessionPath locally, resolveUserPath on the remote client), so the branch you pointed at really was unreachable, and the stale-cwd hazard moves out of the rule rather than being emulated and then defended. refuses a destination that is not absolute, before judging or creating anything holds the refusal and that nothing was created.
  2. Host-kit owns parent creation as one call. Yes, done, as prepareScriptDestination: walk → refuse → create the missing tail at 0700 inside the directory the walk judged → judge the completed chain → answer with that directory. The assertPrivateScriptDestinationPrefix export and both hand-sequenced copies are gone; the local writer's pre-pass plus mkdirSync, and the remote client's mkdir, are each one call now. Two things fall out of it: "a refused destination leaves no directories behind" is true by construction rather than by each caller remembering the order, and the ..-behind-a-missing-component shape is refused as unresolvable_parent before the first mkdir — which is the only way that one can be refused, since a mkdir resolves that spelling as text and builds shared/recordings behind it. The walk also reports whether traversal completed, and publishing requires completion, so a walk that stopped short can never publish into the nearest ancestor. Where nothing was missing, the first walk's answer is the answer and there is no second walk at all.
  3. Resolve the private-group exception one way. Done as above: refused on the mode, with the hint, plus 0700 creation. refuses a group-writable 0775 directory with the hint to close the group and refuses a setgid group-writable directory on the same mode are the accepting-shape-now-refused cases; othersCreateEntriesIn and untrustedAncestryExposure take a trust of { uid } and nothing else.
  4. Keep one defense-in-depth comparison. Yes, and the local one is the one deleted: publishScriptFileSync walks once and the parentIdentity round-trip plus stillJudged !== judged are gone. The remote route keeps the single comparison you already accepted, because it is the only caller where real time sits between the two looks — it captures dev:ino when it allocates staging and compares once under the record lock before the rename. ADR 0016 now says the ownership rule is what closes the window and that a second re-check answers a race the permissions already exclude.

Also from your not-blocking list: the stale parentIdentity JSDoc is gone with the option it documented, the unreachable component === undefined branch in stepThrough is gone (its caller answers an empty queue), and the root-owned alias case now serves root ownership for the alias, its target and the sticky directory that holds it, so it cannot pass a rule that refused every root-owned link.

Lines. Measured your way — net added over origin/main, same five production files: 875 → 735, so 140 fewer. atomic-file.ts is 829 lines where d86ee29 was 959; the writer is 583 (was 591) and the remote client 561 (was 563), both because the sequencing moved into host-kit instead of being duplicated. atomic-file-host.fixtures.ts is 72 lines (was 119) and the private-group suite is deleted, so the test tree shrank too.

Checks. FALLOW_BASE=origin/main pnpm check:fallow --base origin/main --gate all clean (dead code 0, complexity 0, duplication 0), wire-compat green with the publishScriptVersion digest re-acked and its rationale extended — the only code change in that declaration is which host-kit call creates the parent, nothing on the wire moves. check:layering, check:production-exports, check:doc-prose, check:adr-citations and typecheck pass. Each new test fails against d86ee29's production code: the four host-kit parent cases, both umask-002 regressions and both routes' unresolvable_parent cases fail outright; the relative-destination and chmod g-w cases fail because d86ee29 walks the cwd instead and gives the old hint.

One thing to flag for your judgement, since it is a user-visible change I made under decision 3: a local 0775 checkout owned by its own group, which the exception admitted on a files-only host, is now refused with the chmod g-w hint. That is the false refusal you found on Ubuntu, moved to the hosts where the exception did work. I took it because the alternative was trusting a database I cannot read, and the fix is one command the message names.

Both inline threads were already resolved, so there is nothing to close there; I added a short note to each saying which change answers it.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At c852884 the two earlier threads on the shared ancestor and the publish destination are fixed, but one new problem remains: a umask-002 daemon host now refuses its own default recordings.

The daemon creates sessions/ itself, without a mode. open always runs ensureSessionDir, which calls fs.mkdirSync(<baseDir>/sessions/<name>, { recursive: true }) (session-store.ts:525; the tombstone writers at :373 and :442 do the same). Under umask 002, such as a stock Ubuntu login, sessions/ comes out 0775. baseDir is repaired to 0700, but sessions/ is not. Both the default path (session-script-writer.ts:182) and every remote-delivery version (session-script-writer.ts:169-172) walk through sessions/, and the new rule refuses a group-writable directory. A Node check of the two mkdir calls under umask 002 gave sessions=775 and remote-scripts=700. State directories that older versions created under umask 002 have the same 0775 sessions/. So on such a host, bare open --save-script and every remote-delivered recording would fail with script_ancestor_untrusted, and a remote user cannot apply the chmod g-w hint. Before this PR those saves worked. The tests miss it because each route test passes an explicit destination under a 0700 home and never runs ensureSessionDir first.

The rule to satisfy: every directory that agent-device creates on the chain of a script destination it chooses (state dir, sessions/, remote-scripts/) is created at 0700 and repaired to 0700 when it already exists. The daemon-paths owner should hold this, next to where daemon-registration-owner already creates and repairs baseDir, rather than each mkdir site. Please add one regression under process.umask(0o002) that runs ensureSessionDir (or open) first, then asserts that a default-path recording and a remote-delivery version are both written.

Not blocking, and fine to take or leave: atomic-file.ts:501 has a stray duplicate doc line, the two WalkOutcome doc blocks at :416-433 should merge, and the parentIdentity doc at :316-319 says "two walks" when the remote route now walks three or four times; publishScriptVersion discards the directory prepareScriptDestination returns and then re-walks at allocate and again at publish, so passing that answer through would drop the extra seam; in a sticky shared directory with two or more missing names, another user can plant the first name as a link between the walk and the recursive mkdir, which leaves a 0700 directory behind in the link target (from Node's recursive mkdir behavior), so a level-by-level non-recursive mkdir would close it, or the race can be stated as outside the no-leftover guarantee; and the PR body still describes the dropped private-group exception and the publisher-side comparison on both routes.

The finding above comes from a code trace plus a Node mode reproduction of the two daemon mkdir calls. No live umask-002 daemon run exists yet. The rule no longer reads account databases, so the Ubuntu outcome depends only on directory modes, which the test a recording refuses a group-writable home and says how to close it pins.

All 21 checks pass at c852884, but CI hosts use umask 022 and explicit 0700 test homes, so they do not reach this route. There are no conflicts. Before merge, sessions/ needs to be created and repaired at 0700 and the umask-002 regression added.

The open inline threads on the shared ancestor before mkdir (#4584 (comment)) and on the publish destination spelling (#4584 (comment)) are fixed at this head, so they can be resolved.

@thymikee

Copy link
Copy Markdown
Member Author

Head 235d0db439. The umask blocker and both cross-check races are in, each as a test that fails at c852884.

The daemon's own directories (your blocker). ensurePrivateDaemonStateDir now sits in daemon-registration-owner.ts beside repairStateMode, and every creator routes through it: launchDaemonProcess, SessionStore.ensureSessionDir and both tombstone writers, remoteScriptPath, and daemon startup for baseDir and sessionsDir. Creation carries mode: 0o700 and the repair walks every level the call created, not just the leaf — mkdirSync reports the first directory it made, so repairing only the leaf left sessions/ at 0775 while the session directory below it looked correct. The regression runs ensureSessionDir under process.umask(0o002), then writes a default-path recording and a remote-delivery version: both succeed, and sessions/, sessions/journey/ and remote-scripts/ are 0700. Reverted to c852884 it fails with your exact symptom — script_ancestor_untrusted / shared_group_writable at sessions.

lstat versus stat (blocker 1). Every step is judged from the lstat of the ENTRY; stat is gone from the rule. WalkGround is now judged: Map<path, {identity, uid, gid, mode}>: a name the walk already judged keeps that judgment and is re-read only to recognize the entry, so a directory swapped for a link between the judgment and entering it is refused (parent_replaced) instead of entered on its target's permissions. The answer is confirmed the same way before any caller stages, and identities are read { bigint: true } — Number(2n**53n + 1n) === Number(2n**53n), which the bigint test pins.

Recursive mkdir (your item 2). createMissingLevels issues one non-recursive mkdir per name, inside the directory the walk established, confirming that directory's identity before each creation and lstat-ing each level it made. EEXIST is a refusal (entry_appeared), never a retry, and the second walk starts from the first walk's whole memory plus the creations — so a level swapped out in between is refused rather than entered. Windows keeps the recursive call, which this race cannot reach, and the absolute check now runs before the platform branch, pinned by a test that is deliberately not skipped on Windows.

Pass-through. prepareScriptDestination returns { directory, identity }; allocateStaging stages in that directory after one identity check (destinationMoved), so a version is walked once rather than twice. One thing you may want to know: I first moved the three ground decisions into a sibling module to keep atomic-file.ts under the cap, and eager-closure-budgets refused it — a new module on the host-kit/file.ts edge is eager work for every entry that publishes a file. They stayed put, and the file now sits exactly at 1,000 lines.

Delta. Net production across the five files this rule owns is +926 (was 735 at c852884): +830 atomic-file.ts, +92 remote client, +8 writer, +5 façade, −9 verified-file.ts. Counting the daemon-side files the umask fix touches, all production is +976. Of the 816 added lines in atomic-file.ts, 291 are doc comments.

Checks. pnpm check:affected --run --head HEAD passes all runnable checks at this head (receipt run-U645gX, exactCommit): format, lint, typecheck, layering, fallow, build, the affected vitest projects, the full unit run, docs-links and docs-prose. Reverting only the production files to c852884 turns 8 tests red across the four suites, every one for the intended reason. Also locally: focused suites 505 passed; eager-closure-budgets 903; wire-compat 51, with the publishScriptVersion digest re-acked and the ack saying why nothing moves on the wire; check:fallow --gate all exits 0 — dead code 0, complexity 0, duplication 0, no baseline edits, no suppressions; check:production-exports and check:adr-citations green.

One caveat on that last test, so the count is not read as more than it is: eight of the nine new or rewritten tests go red at c852884 on this machine. The every-platform absolute check does not, because macOS already refused a relative destination through the walk — the check only becomes load-bearing where the walk is a no-op, so it is pinned by the Windows branch it now runs before, and by the test running unskipped there, not by this revert.

ADR 0016 and the guide now state one mkdir per level, the identity carried by the answer, bigint identities, and the daemon's 0700 state directories. The PR body describes the current design.

thymikee and others added 7 commits November 16, 2026 22:24
A publish resolved the destination by name, so a second local user who
could write any directory on the way to it could replace that component,
or leave a symbolic link there before the entry was created, and move the
saved script wherever they chose.

`publishScriptFileSync` now asks first: `assertPrivateScriptDestination`
walks each component with `lstat` and refuses the chain under
`script_ancestor_untrusted` when a directory is writable by other, or by
a group that is not this user's own private group, unless the sticky bit
cancels the exposure. A symbolic link whose own directory others can
create entries in is refused whatever it points at. Ubuntu and Debian set
up every home directory as group-writable and owned by the user's private
group, so that shape is admitted, and `privateGroupIds` is exported so
the membership rule is testable on its own.

The caller also captures the parent's `dev:ino` before staging, and the
publish compares it before the link or rename, so a parent swapped out by
name while the file was written refuses under `script_ancestry_replaced`
instead of receiving the script. Node exposes no `openat` or `renameat`,
so publishing relative to a verified directory handle needs native code;
this is the rule that closes the same window without it.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The writer now publishes with `publishScriptFileSync`, and hands it the
identity of the directory it created, so a `--save-script` path with a
directory another local user can write refuses instead of landing the
recording there, and a parent replaced by name while the recording was
staged never receives it. A refusal is reported with the reason the rule
chose, and a refused destination is created by nothing.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A remote recording staged the version beside the destination and renamed
it into place, so an ancestor another local user could write steered both
the download and the entry.

`allocateStaging` now asks the destination rule before it creates
anything, so a refused path leaves no directory behind, and it reports
the destination directory's identity. `publishStaged` re-asks under the
record lock, immediately before the entry is created, so whoever could
write an ancestor while the version was downloaded cannot move it now.

The command keeps its success and carries the refusal's typed reason as
the `scriptDeliveryFailure` cause, so a caller can tell an unsafe
destination from a download that failed.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The replay guide states the destination rule for `--save-script`: every
directory on the way must be one only the user and root can write, why a
sticky shared team directory and a group-writable home of the user's own
private group are accepted, and why a symbolic link inside a directory
others can write is refused whatever it targets. Each surface's outcome
is stated: a local `close` fails and keeps its bytes, a remote recording
keeps its success and reports the reason as the `scriptDeliveryFailure`
cause, and the next recorded action repeats the refusal. The remote proxy
page points at that rule instead of repeating it, and ADR 0016 records the
rule, the private-group exception, the parent-identity re-check, and why
publishing relative to a directory handle was not available.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The daemon RPC manifest reached `PublishOutcome` through
`publishScriptVersion` and could not place it, so the outcome type the
client builds now carries a waiver naming the payload half it serializes,
and `ScriptDeliveryFailure` — the field a peer actually parses — is listed
with it. `publishScriptVersion`'s digest moves with the destination rule.

The same-directory publisher guard read the shared atomic publish owner by
name, so the script writer's new entry point failed it. The guard now
accepts either owner and states separately that the session script uses the
ancestry-owning one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Three ways through the destination rule stayed open.

Ownership was never asked, so a directory another user owned passed at
0755, and its owner could open it up or replace the subtree whenever they
liked; the sticky bit excused a directory whatever its ownership. The rule
now requires every directory on the chain to be owned by this user or by
root, and keeps the writability rule on top of that. A sticky shared
directory is still trusted for passing through, and the link rule requires
a link there to be owned by this user or root; nothing is created inside a
directory others can add entries to, so a shared parent no longer receives
the entry.

A symbolic link's target escaped the walk entirely: a link in the user's
own home pointing at a world-writable directory was judged only as the
link. The existing prefix is now resolved once and the whole resolved
chain is judged, so the shared target is refused; root-owned system
aliases such as macOS /tmp and /var keep working.

The private-group exception assumed a membership it could not prove. It
now fails closed: both database files must have been read, the group must
be this process's primary group and carry this user's name, its member
list may name nobody else, and no other passwd record may hold it as its
primary group. A name service that can add a member without touching
/etc/passwd is named as the limitation it is.

A published script is created 0600 locally as well as remotely, so a
recording is not world-readable in a directory that is merely shared.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Three ways through the ownership rule stayed open, and the identity check
was described as the thing that closes a race.

Ownership is now asked before the mode. `othersCreateEntriesIn` treats a
directory another user owns as theirs whatever its mode, so a link that
stands in their private-mode component is refused: they can rename that
component and leave one of their own there. A sticky bit excuses a
directory only when this user or root owns it, because both the sticky bit
and the group bits shield other users FROM an owner rather than the
reverse.

The chain keeps the `..` segments it was spelled with, so
`<dir>/shared/work/../x` is judged as the walk the kernel performs, with
the link named `work` in it, instead of the directory `path.resolve` names
after folding the `..` away; the fold points at a directory that receives
nothing.

The documentation and the refusal hint now say what the rule does:
a directory is accepted when this user or root owns it and its mode keeps
its contents closed to others, and the destination is never created where
others may add entries. The hint names ownership as well as writability.

Three ancestry tests were macOS-only in effect: an assertion that the
scratch root must differ from its real path cannot hold where `/tmp` is a
real directory, and two identity checks passed only because the filesystem
handed back a just-freed inode. The alias case now builds the alias it
needs, and a replaced parent is replaced with a directory that really is
another one.

Refs #4565

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
thymikee and others added 9 commits November 16, 2026 22:24
…olds it

A relative link target was joined onto the directory the walk stood in, which
made it absolute, and the walk then continued from the link's own directory.
The lookup missed the target and the walk stopped, so the target and the
directories on the way to it were never judged: `ln -s ../team/rec rec`
reopened #4565. `path.join` also folded a `..` inside a target away. Targets
are now queued as RAW components, restarting at `/` for an absolute target and
continuing from the directory that holds the link for a relative one.

The same folding let a publisher escape the judged directory. Staging used
`path.join` on the spelled destination, which answers a DIFFERENT directory
wherever the walk turned aside, so the temp file was written into ground nobody
judged and renamed into the judged one from outside it.
`assertPrivateScriptDestination` now returns the physical directory it verified,
and both the local publisher and the remote client stage inside it. The parent
identity is taken from that directory too, so a `..` destination no longer
reports a replacement that never happened, and the refusal names the directory
the entry would really have been created in.

A relative destination with no directory in it had the working directory as its
parent rather than nothing, so the walk started there instead of at `/`. Each
physical directory is judged once per walk and each path is read once, so an
alias or a `..` back over ground already crossed costs nothing.

The private-group exception also reads `initgroups:`, the database glibc answers
SUPPLEMENTARY membership from: an explicit line has to be files-only, and an
absent one inherits the verified `group` setting. `lstatIfPresent` is imported
from the module that owns it, the two local copies are gone, and the message
says what a link-loop or a non-directory refusal actually found.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A script's temp file is created beside the destination AS THE FILESYSTEM
RESOLVES IT, because that is the directory the entry is created in. So a
destination spelled through a symbolic link -- any scratch path under `/var` on
macOS -- has its temp file one directory over in TEXT, and
`isAtomicPublishTemporaryPath` compared the two spellings as strings. It stopped
recognizing temp files it had produced for its own destination: a fault injected
at the write went unclaimed, and the cleanup that names those files left them
behind. The two directories are now resolved and compared as directories.

`lstatIfPresent` moves from `verified-file.ts` to `atomic-file.ts`, which the
verified-file guards already sit behind. Importing it the other way round made
64 top-level statements of eager work load from the daemon's startup path, which
the no-growth rule in `eager-closure-budgets` refuses: a module the merge-base
tree already had cannot be made eager by a split.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`cd /shared/mine` under a `/shared` at 0777 and not sticky, then save a script
to `journey.ad`: the destination names no directory, so the walk started IN the
working directory, judged it, and never looked above it. `mine` is a name in
`/shared`, and anyone who can add an entry there can point it at a directory of
their own between the check and the rename -- the swap this rule exists to
refuse, reached through the exported publisher. A relative destination is now
walked from `/` over the resolved working directory's components with the
destination's own components following them raw, so every ancestor is judged.

`isAtomicPublishTemporaryPath` resolved the destination's spelling to compare
it with the temp file's directory, and a JS path resolution applies a spelled
`..` lexically. For `home/work/../y` with `work` a link, that answers `home/y`
while the entry is created in `priv/y`, so the real temp sibling was called
someone else's and a look-alike beside `home/y` was called ours. Both
directories are asked of the filesystem and compared as device and inode.

`publishScriptFileSync` no longer takes a `parentIdentity`: it takes the
identity from the directory the walk judged, so an outside caller cannot steer
what the comparison is against, and only the remote client still passes one,
captured before its download. The externally captured identity is tested
against `assertPrivateScriptDestination`, which is what compares it.

Three writer tests moved the caller's uid, which made the whole scratch tree
foreign, so they could not tell a leaf refusal from a root refusal and did not
run under root. They now report the ownership of the one directory at issue and
assert the ancestor named. The alias test entered through one link and stopped,
so it passed with the read cache removed: both names are now traversed, and the
walk's own answer is asserted. `atomic-file-ancestry.test.ts` gained enough
cases to pass its cap, so the ancestry cases for a remote publication move to
`daemon-artifacts-script-ancestry.test.ts`, sharing the client and the fake
daemon with the download suite through a support module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both routes created the missing parent before the ancestry rule was asked, so a
destination under a world-writable directory left the directories its `mkdir -p`
made standing in that shared ground, even though the publish that followed was
refused. The same walk now runs over the prefix that already exists, refuses, and
runs again after the `mkdir` once the directory the entry lands in exists. Only
the second question asks whether others could create the name inside it, because
below the first question the directories are the caller's own to create: that is
what keeps `mkdir -p <sticky shared>/recordings` acceptable.

A relative destination walked the working directory Node remembers, which is the
path the first query after `chdir` returned. Move that directory and put another
at its old path and the walk judges a directory the process is not in, while the
kernel resolves the destination in the moved one. The remembered spelling is
confirmed against `.` by `dev:ino`, and the entry is created in the directory the
walk answered rather than by handing the kernel the destination back.

`ground.judged` is gone: the once-per-inode judgment it held could be deleted
with every test green, and only the per-path `stat` cache is observable. The hop
test stopped deriving its inputs from the production constant, which let a limit
of 29 pass as 30, and now builds a 30-link chain and a 31-link chain. Two tests
that made a destination directory `0500` now fail the staging write directly:
mode bits do not bind root, so those two reported the host's permission model
instead of the writer's error handling under a root caller.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fied

`open --save-script <home>/work/../y/journey.ad`, with `work` a link: the
destination is judged, the version is downloaded and staged, and the chain is
judged a second time before the entry is created. The last step then handed the
spelled destination to the kernel, so the path was resolved once more and
`home/work/../y` was answered through whatever `work` pointed at by then --
link or rename into a directory the rule had just refused to accept. The entry is
now created in the directory that second judgment answers, spelled from it, which
is what the local publisher already did.

No state has to travel between staging and publishing: the judgment the publish
already makes returns the directory.

The ledger records the new digest of `publishScriptVersion` with a
`compatibleChanges` entry: the call the declaration gained is one more
destination check before the `mkdir`, so request, response and artifact framing
are byte-for-byte what a released peer sends and parses.
Three shapes the destination-ancestry rule answered too generously:

- `/etc/nsswitch.conf` now answers a database named on TWO lines with no
  sources, which refuses the private-group exception. glibc appends the
  sources of every line naming a database and consults them in order, so
  `group: files` above `group: files sss` really does consult `sss`, and
  holding to one line judged by a line the lookup never stopped at.
- The walk reports whether it ARRIVED at the directory the entry is created
  in. `publishScriptFileSync` creates nothing, so it now refuses
  `root/missing/journey.ad` as `unreachable_parent` instead of publishing
  `root/journey.ad` — a file nobody asked for.
- A `..` queued behind the first component that does not exist is refused as
  `unresolvable_parent` before either route's `mkdir` runs. The kernel cannot
  apply that step until the name before it exists, while `mkdir -p` resolves
  the same spelling as text and builds what stands on the far side of it, so
  `private/missing/../../shared/recordings` used to create a world-writable
  directory the rule never judged. A `..` over directories that all exist is
  applied one step at a time, as the kernel applies it.

The walk's empty-queue exit moves to the loop that consumes the queue, the
one place that sees a finished queue, and the unreachable branch inside a
step is gone. The root-owned alias case now serves root for the alias and
the directory that holds it: an alias owned by whoever runs the suite passed
even if every root-owned link were refused, which is the macOS `/tmp` shape
that must keep working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Amends ADR 0016 so the rule is smaller and sits in one owner.

A script publish now creates every directory it makes at 0700, so a new
directory passes the check a later recording judges it by instead of being refused at 0775 on a shared
umask. host-kit owns the whole sequence as `prepareScriptDestination`: walk, refuse before creating
anything, create what is missing inside the judged directory, judge again, answer with the judged
directory. Both routes call it, and the exported prefix seam plus the two hand-sequenced copies are gone.
Where nothing was missing, the first walk already judged the directory the entry is created in, so its
answer is the answer and there is no second walk.

The private-group exception is dropped. Membership cannot be proven on a host whose name service answers
it from outside the account files, so a group-writable directory is refused on the mode alone and the
hint names `chmod g-w`. No account database is read. A destination that is not absolute is refused at the
entry, and the local publish keeps one walk instead of two.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Four shapes the destination-ancestry rule still answered too generously, plus the daemon's own state
directories under a shared umask:

- Every step of the walk is judged from the `lstat` of the ENTRY, never re-read with `stat`: `stat`
  resolves a name a second time, and the name can answer a different entry by then, so a directory
  swapped for a link mid-walk was entered on the strength of its target. Where a walk returns to a name
  it judged, it re-reads that name only to recognize it and keeps the first judgment. Directory
  identities are read as BIGINT, because a 32-bit `ino` collapses two distinct large inodes onto one
  identity, and the answer is confirmed to hold the judged entry before a caller stages anything there.
- A missing tail is created ONE LEVEL AT A TIME. A recursive `mkdir` resolves the whole tail as text, so
  a link another local user plants at one of the absent names while the walk runs carries the creation to
  its far side, where as root it creates with privileges. Each level is confirmed before the name below it
  is created, and a name that appeared between the check and the creation is refused (`entry_appeared`).
- A destination that is not absolute is refused on every platform, Windows included, where no walk runs to
  catch it.
- The daemon creates and repairs its state directory, `sessions/` and `remote-scripts/` at 0700 rather than
  through a mode-less `mkdir`, so a umask-002 host no longer gets 0775 `sessions/` and a refusal of the
  daemon's own default recordings and remote-delivered versions.

`prepareScriptDestination` answers the judged directory WITH its identity, so the remote route stages each
version where that answer points instead of walking the destination a second time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`repairStateMode` forced a mode by pathname, with no type and no ownership check. A root daemon starting
against a legacy state directory another user could write can be handed a `sessions` that is a LINK, and the
chmod travels through it to a directory that user owns. `ensurePrivateDaemonStateDir` now repairs each level
it was asked for from its own `lstat` of the ENTRY: a link or a non-directory is refused
(`state_dir_not_a_directory`), a level this process does not own is refused (`state_dir_foreign_owned`), and
in both cases nothing is changed. What remains to repair is forced through a descriptor opened
`O_DIRECTORY | O_NOFOLLOW`, whose `fstat` dev:ino is confirmed to be the entry the `lstat` judged, so the
mode cannot be applied to a second entry behind the name. Ownership is asked only when a mode has to change,
so the common startup is still one `lstat`, and no second rule is introduced.

Coverage: the repair owner's refusals, and the same two shapes seen through the daemon's own state paths,
with foreign ownership simulated as root state (serveHostOwnership); plus pre-existing owned 0775 `sessions`
and `remote-scripts` repaired to 0700, which the previous test could not see because it started from 0700.

ADR 0016 said remote delivery compares the judged identity ONCE. It asks the rule twice per version, because
a download runs in between: prepare and allocate staging, then walk again and compare before the rename. What
this branch removed is the THIRD walk the allocation used to run to be told a directory the call before it
had already answered. The ADR, and the PR body, say that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 235d0db, the regex on the destination is gone, the walk now runs before any creation, and staging is allocated in the judged directory, so those parts of the earlier review (#4584 (comment)) are fixed. One finding remains.

The remote route copies host-kit's refusal instead of calling it. destinationMoved in session-script-destination.ts:415 repeats the parent_replaced branch of scriptAncestryFailure (atomic-file.ts:894-907): same message, reason, exposure, path, ancestor and hint. The identity check that throws it (:264) also re-implements host-kit's private stillTheJudgedEntry, without the isDirectory test. A later wording or detail change in host-kit would miss the remote route, and isAncestryReason (:477) already matches on the hand-kept reason string. The rule should be: host-kit confirms a ScriptDestination, and every parent_replaced refusal is built by scriptAncestryFailure. Please export one host-kit call, for example confirmScriptDestination(destination, prepared), call it from allocateStaging, and delete destinationMoved and the local identity comparison.

Not blocking, and you can take or leave these: ADR 0016 :227 still says the remote pre-rename comparison is the only identity comparison, while :222 and the code re-check identity on every walk, so one of them needs to change; createMissingLevels (atomic-file.ts:962) refuses on any EEXIST, so two processes saving into the same new directory can fail with entry_appeared, and judging the appeared name with the same lstat rule would accept this user's own directory and still refuse a plant; ensurePrivateDaemonStateDir (daemon-runtime.ts:216) repairs a pre-existing 0775 sessions/ only through the startup lines, and deleting them leaves every test green, so one startup test that a 0775 sessions/ ends at 0700 and a default recording is then written would pin it.

Could the change be smaller? The two fixes that carry the safety are judging every step from its single lstat, and creating each missing level with one non-recursive mkdir that judges an appeared entry by the same rule. The identity-compare and 'replaced' branch in judgeGround, the known-identity check in stepIntoEntry, stillTheJudgedEntry before each level, the ground seeded into the second walk, the bigint identities and remote destinationMoved look like they only catch swaps the ownership rule already excludes. A judged entry is owned by this user or root and sits in a directory others cannot rename in, or in a sticky one where they cannot replace it. The existing remote pre-rename comparison could stay as the one extra check. That would remove roughly 100-150 production lines and bring atomic-file.ts under the 1,000-line cap. Net production is now +1075 against the 700-line threshold, up from 735 at c852884; the description's +926/+976 counts do not match the diff. What am I missing that makes the recognition layer necessary?

The three earlier inline threads are fixed at this commit and can be resolved: the polynomial regex (#4584 (comment)), creation before the walk (#4584 (comment)), and staging outside the judged directory (#4584 (comment)).

CI shows 20 checks with 0 not passing at 235d0db. The CI hosts run umask 022, so the umask route is covered only by the process.umask regression, not by host configuration. The ownership argument above has not been checked with a second uid. No conflicts. Before merge, route the remote identity check and its parent_replaced refusal through one exported host-kit call and delete destinationMoved.

…e rest

Host-kit now exports one confirmation: `confirmScriptDestination(destination,
prepared)` walks the destination again, compares it with the identity
`prepareScriptDestination` answered, and returns the directory to create the
entry in. The remote route calls it after its download, under the record lock,
so every `parent_replaced` refusal is built by `scriptAncestryFailure` and no
route holds a copy of the reason, exposure, ancestor or hint. `destinationMoved`
and the local identity compare are deleted, along with the hand-kept reason
literals: the route reads the reason back through `scriptAncestryReason`.

The recognition layer that grew around the walk is cut, because the ownership
rule is what excludes anyone who could move a judged directory. The walk keeps
no memory of ground it stood on, does not re-recognise a name it reached
through a `..` or an alias, and runs no second identity check before a creation.
What is kept per step is one `lstat` of that step, never a `stat`.

`createMissingLevel` judges a name that appeared instead of refusing it: the
same single `lstat`, the same rule. It adopts a private directory this user
owns, refuses a foreign or writable one, and asks whether other local users
could add the destination's own name inside it.

Startup is pinned as the thing that repairs a state chain a legacy release or a
shared checkout left at 0775, and a state repair that forces a mode through a
descriptor is asserted as such.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 235d0db to 45d4eb9 Compare November 16, 2026 23:40
@thymikee

Copy link
Copy Markdown
Member Author

Cut the recognition layer rather than defend it — head 45d4eb97.

Blocking finding: routed through one exported host-kit call. confirmScriptDestination(destination, prepared) is exported from @agent-device/host-kit/file and does the confirmation and the refusal: it walks the destination again, compares that walk's dev:ino with the identity prepareScriptDestination answered, and returns the directory to create the entry in. destinationMoved and the local identity comparison in allocateStaging are deleted, so the route holds no copy of the message, reason, exposure, ancestor or hint — every parent_replaced is built by scriptAncestryFailure. The hand-kept reason literals are gone too: host-kit exports scriptAncestryReason(error), which reads back the reason of the refusal it built. The surface for this rule is now exactly prepareScriptDestination + confirmScriptDestination + scriptAncestryReason; assertPrivateScriptDestination is deleted outright and publishDirectoryIdentity is module-internal, because check:production-exports refuses exported surface nobody consumes.

One placement difference from your suggestion: the confirm runs after the download and before the rename, not in allocateStaging. Calling it at allocation walked the destination a third time per version to re-derive a directory the call one line earlier had answered, and the wait that needs the check is the download. Per version at this head: two walks, one identity comparison.

What was cut. Your ownership argument is the reason the layer is gone rather than the reason it stays, so it is gone — the whole thing, not a part:

  • WalkGround, the per-walk Map of every directory the walk judged, and the map seeded from creation into the second walk.
  • The 'replaced' answer in judgeGround — re-reading a name to RECOGNIZE it and keeping the first judgment.
  • The known-identity check in stepIntoEntry, which compared the fresh lstat against the recorded identity of a name the walk had seen before.
  • stillTheJudgedEntry, run before answering and before creating each level.
  • The parentIdentity parameter that made the create-nothing walk compare a second identity.
  • The remote's destinationMoved, its identity comparison, and its hand-kept reason strings.

I could not name an attack that layer caught which the ownership rule lets through, and I could not reproduce one against the pre-cut code. Every actor able to swap a judged directory had already been refused at it: a level the walk stands in is owned by this user or root with its contents closed to others, or is sticky, where this user's entry cannot be replaced. Nothing here was kept for a reproduced attack, and I found nothing whose removal reopens one. −96 production lines in this commit; atomic-file.ts 1,000 → 939.

What was kept, and the scenario each one answers.

  1. One lstat per step, never stat. Destination ~/work/rec/journey.ad where rec is ln -s /tmp/attacker rec, with ~/work at 0700. A stat of rec answers the TARGET: a directory the attacker owns at 0700, which reads as safe, and the publish then creates through the link. lstat answers the ENTRY — a link, judged against the mode of ~/work that let it be planted — and the walk follows it one component at a time so a .. written inside the target is applied to the directory the kernel actually reaches. Pinned in atomic-file-ancestry.test.ts: the link-in-private-mode cases, the .. cases, and the two-routes-one-directory case.
  2. One non-recursive mkdir per missing level, at 0700. Destination <sticky /tmp>/recordings/sub/journey.ad, recordings absent. A recursive mkdir resolves the whole tail as text, so a link planted at recordings in that window carries the creation to its far side — and run as root it creates there with privileges. One mkdir per name can only land inside the directory the walk stopped in. Pinned in atomic-file-parent.test.ts: "refuses a destination that steps back out of a missing directory, and creates none", "refuses a missing level planted as a link, and creates nothing beyond it".
  3. EEXIST is judged, not refused and not trusted. Two sessions saving into the same new recordings/: one wins mkdir, the other finds the name taken. Your point is right — the old blind refusal failed that routine save with entry_appeared. Blind trust is the other failure: in that same window an attacker with a write into the parent can put a directory there that is theirs, or 0775, or a link. So the appeared entry gets the same single lstat and the same rule. Four tests: "adopts a missing level another process created, and creates the rest below it", "refuses a missing level that appeared as another user directory", "refuses a missing level that appeared group-writable", and "refuses a missing level that appeared sticky and world-writable" — that last one refuses even this user's own directory, because the sticky bit holds entries already there and says nothing about a name the destination still has to create.
  4. The one remote pre-rename comparison, as host-kit's call. Kept because you named it as the one extra check worth keeping, and because it is six lines and one call. What it does mechanically: an answer held across the download is used only where the name still resolves to the directory that answer named. The concrete shape is ~/rec at 0700 judged before the download; while the download runs the name is replaced — with a link to another 0700 directory of this user's, or with a different real directory. The second walk judges that replacement on its own merits and ACCEPTS it, because it is a private directory of this user; nothing about the walk itself notices the ground moved. Only the comparison refuses, and it also refuses the case where the chain is gone, instead of answering a nearer ancestor. I am not claiming more than that: under the cross-user threat model, whoever could make that swap had already been refused at the parent, and the only actor who can do it to its own private directory is code running as this user, which can read the recording anyway. So it is defense in depth against ground that moved under a held answer, not what excludes the attacker — which is what the ADR now says in those words. If you would rather have no comparison at all, that is a five-minute change and I would not argue: confirmScriptDestination would shrink to the second walk alone and the answer would stop carrying an identity.

Daemon state repair: yes to both, and named. repairPrivateStateDir lstats the entry first and refuses before touching anything: absent is a no-op; a link or a non-directory is state_dir_not_a_directory; a directory another uid owns is state_dir_foreign_owned with its mode left alone; already 0700 is a no-op. forcePrivateStateDir then opens the name with O_RDONLY | O_DIRECTORY | O_NOFOLLOW, fstats that descriptor and requires the same dev:ino as the entry it judged (state_dir_entry_replaced if not), re-checks ownership on the opened entry, and only then calls fchmodSync on that descriptor. ELOOP and ENOTDIR from the open become the not-a-directory refusal; EPERM/EROFS/ENOTSUP/EACCES emit a daemon_state_mode_repair_skipped warning rather than killing startup. The reason for the fd: chmod given a NAME resolves it again, so a repair that reached a legacy sessions under a directory another local user can write, run by a root daemon, would be changing the mode of a directory that user chose. The mode either lands on the entry that was judged or nothing happens.

Tests for it:

  • packages/daemon-contracts/src/daemon-registration-owner-state-dir.test.ts — "repairs the state directories it owns, whatever mode they were created with", "refuses a state name that is a link, and leaves where it leads alone" (asserts the link target's mode is untouched), "refuses a state directory another user owns, and leaves its mode", "warns and changes nothing when a state directory cannot be opened at all".
  • packages/daemon/src/core/__tests__/session-store-teardown-script.test.ts — "on a umask-002 host the daemon creates and repairs its own state directories at 0700", "repairs state directories a legacy version or a shared checkout created at 0775", "refuses to repair a sessions directory another user owns, and changes nothing there", "refuses a planted sessions link under state another user can write, and leaves where it leads alone".
  • packages/daemon/src/server/http/daemon-runtime-state-dir.test.ts — your startup pin: "startup repairs a state chain a legacy release or a shared checkout left group-writable" (0775 baseDir and sessionsDir end at 0700, then a default recording is really written through SessionStore), and "startup refuses state it may not force private" (state_dir_foreign_owned, mode intact). Deleting the two ensurePrivateDaemonStateDir lines from daemon-runtime.ts turns both red — I ran your experiment.
  • src/__tests__/daemon-launch-cwd.test.ts — "a launched daemon starts when forcing a mode on its state paths fails with EPERM/EROFS", which asserts fchmodSync(…, 0o700) exactly once for the state directory and chmodSync only on the log file, i.e. the directory's mode can only be forced through a descriptor.

ADR 0016. :222 and :227 no longer contradict, because the half that contradicted is cut. :227 now says the remote walks twice and compares identity once, which is what the code does, and :213 describes the EEXIST judgement in place of entry_appeared.

The delta, reconciled. Our counts differ only in which files count as test files. Under the rule your numbers use (everything except *.test.ts, __tests__/, test/, scripts/, docs/, website/, ledger.json), this head is +1,094 and 235d0db was +1,075 — your figure was right for that rule. My earlier +976 additionally excluded two src/*.fixtures.ts files (atomic-file-host.fixtures.ts +72, test-kit/src/host-ownership.fixtures.ts +27); +976 + 99 = +1,075.

File 235d0db 45d4eb97
packages/host-kit/src/internal/atomic-file.ts +830 +769
packages/daemon-contracts/src/daemon-registration-owner.ts +40 +155
packages/remote/src/session-script-destination.ts +92 +57
packages/host-kit/src/internal/atomic-file-host.fixtures.ts +72 +72
packages/test-kit/src/host-ownership.fixtures.ts +27 +27
packages/daemon/src/core/session-script-writer.ts +8 +8
packages/daemon/src/server/http/daemon-runtime.ts +7 +7
packages/host-kit/src/file.ts +5 +5
test/wire-compat/closure-policy.ts +2 +2
packages/test-kit/src/index.ts +1 +1
packages/host-kit/src/internal/verified-file.ts −9 −9
Total +1,075 +1,094

Why the total did not follow atomic-file.ts down: the state-directory repair grew +115 on this PR because your round-9 finding asked for judging before forcing and a descriptor-based mode change. So one file fell −61, the remote fell −35, and the repair rose +115. Excluding comment lines the head is +692 of code against +702 at 235d0db; 402 of the added lines are comments, and the reasoning a reviewer needs in order to not re-add the cut layer is precisely what those comments are, so I would rather keep the prose than thin it to move a count.

If the total still needs to come down, the honest split is by concern, not by deletion: the ancestry rule (atomic-file.ts: roughly 370 lines of kernel-shaped walk, 290 for the two destination calls plus per-level creation, 215 for exposure decisions and the refusal shape) is one change, and the daemon state-chain repair (+155 in daemon-registration-owner.ts, +7 in daemon-runtime.ts, plus the four suites above) is another that could be its own PR. What I cannot cut without reopening #4565 is a per-route copy of the ownership rule, a walk that trusts path.dirname of a destination containing a .., or stat where lstat is what judges the entry.

Red at 235d0db. With only the four production files reverted to 235d0db and these tests kept, 12 tests fail across 5 files: the four EEXIST cases, the foreign-owned and cannot-open repair cases, the two startup cases, and the two chmod-shape cases. The startup file is additionally red when only the two ensurePrivateDaemonStateDir lines are deleted. No suppressions, baselines or allowlists.

Checks at 45d4eb97. pnpm check:affected --run --head HEAD green on the final tree; unit sweep 17,372 passed / 9 skipped over 1,943 files; wire-compat 51 (the publishScriptVersion declaration hash moved with its doc text and is re-acked — no request, response, status or framing change); eager-closure-budgets 905; layering 483 pass / 0 fail; check:fallow --gate all dead 0 / complexity 0 / duplication 0; check:production-exports, check:doc-prose (32 clean), check:adr-citations, check:doc-links clean. atomic-file.ts is 939 lines; the largest file in the diff is 968 and none exceeds 1,000. The head carries one extra test-only commit: test/repo-guards/macos-helper-package-contents.test.ts asserts the packed tarball's file list through a real npm pack, measured 1.9s alone and over the 5s default once the suite runs every worker, so it went red on a full-suite run of this branch. It now names a 30s budget the way the other subprocess-backed tests do. Nothing in ed5c7b96 touches that file or the pack list.

Two things your comment names that this head does not claim to fix: CI still runs umask 022, so the shared-checkout route is covered by the process.umask regression plus tests that force 0775 with chmod rather than by host configuration — the new startup test deliberately sets modes on disk instead of moving the umask, so it is host-independent, but no CI host exercises the umask itself. And ownership is still served by a fixture uid (65534), not a real second account. A CI job that owns a scratch tree as another uid and skips when it cannot is the right fix; I would take it as a follow-up rather than inside this PR.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Apex Sentinel: 3 new findings on 45d4eb9. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 45d4eb9 the host-kit changes close the earlier destination concerns, but one new problem remains: the daemon can now refuse to start on setups that work on main.

ensurePrivateDaemonStateDir runs on daemon launch (launchDaemonProcess, for baseDir), on startup (daemon-runtime.ts:215-216, for baseDir and sessions/) and per session in session-store.ts. When a level already exists, the repair throws state_dir_not_a_directory if it is a symlink (daemon-registration-owner.ts:341). It throws state_dir_foreign_owned if the level is not 0700 and this uid does not own it (:356). Two examples: a user whose ~/.agent-device or sessions/ links to another disk, and a root daemon in a container with a 0755 sessions/ owned by the mounted uid. On main both start, because the repair chmods or skips with daemon_state_mode_repair_skipped. At this head, launch or startup throws. Every command fails, not only --save-script. This also breaks the rule repairStateMode documents in the same file (:435-437). The test at daemon-runtime-state-dir.test.ts:66-92 pins the startup rejection.

The rule the code must satisfy: a state-mode repair never decides whether the daemon starts. It forces 0700 only on a directory entry this uid owns, reached without following a link. Otherwise it changes nothing and emits daemon_state_mode_repair_skipped. The destination walk stays the only thing that refuses a recording. The smallest form is an lstat fast path for a directory already at 0700. Otherwise open with O_RDONLY|O_DIRECTORY|O_NOFOLLOW, fstat, and fchmod only when the fstat uid is this process. Treat ELOOP/ENOTDIR, a foreign owner and EACCES/EPERM/EROFS alike as skip-with-diagnostic. Please flip the startup test to assert that startup succeeds, the foreign mode is unchanged, and a default recording is refused with script_ancestor_untrusted.

Could the repair be the smaller design above? The host-kit cut helps: atomic-file.ts is -61 net and the remote side is -35 net. Production net still rose from 1075 to 1094, because eaa9747 added about 115 lines of state-repair guards (three refusal reasons, an lstat-then-fstat identity compare, a double ownership check). One repair path would be about 40 lines, remove sameStateEntry, state_dir_entry_replaced, the duplicate owner check and privateStateRefusal, and bring production net to roughly 1020. ADR 0016:225 would first need to describe the repair as best-effort and never refusing, with the walk as the only refusal.

Not blocking, take or leave: the comment at atomic-file.ts:916 says the created level "would stand 0775" without the chmod, but mkdirSync(level, {mode: 0o700}) cannot exceed 0700 and the lstat judgment now runs before the chmod, so fix the comment or drop the chmod; and the timeout bump in test/repo-guards/macos-helper-package-contents.test.ts is unrelated, so ship it as its own one-line PR or name it in the PR body.

The three earlier inline threads no longer apply, so please resolve them: no regex runs on the destination at 45d4eb9 (#4584 (comment)); prepareScriptDestination walks before creating and creates one level at a time inside the judged directory (#4584 (comment)); allocateStaging runs mkdtemp in the judged directory (#4584 (comment)).

Checks: 20 pass and one review check is still running; no failing job overlaps the diff. CI hosts run umask 022 as a uid that owns its temp dirs, so the symlink and foreign-owner shapes only run through the simulated-ownership test, which asserts the refusal. The startup problem comes from the code and the pinning test, not a run against a symlinked ~/.agent-device or a real second uid. There are no conflicts. Before merge, the repair must skip with daemon_state_mode_repair_skipped instead of refusing on a symlinked or foreign-owned state level.

Separately, on main, launchDaemonProcess still opens daemon.log with a and repairs its mode by pathname. That is not a regression from this PR.

thymikee and others added 3 commits November 17, 2026 01:00
…artup

The repair forces 0700 through an O_DIRECTORY | O_NOFOLLOW descriptor only when the fstat of that
descriptor says this uid owns the entry. Every other shape - a link, a name that is not a directory,
a level another uid owns, EACCES/EPERM/EROFS from a mount it cannot force - changes nothing and emits
daemon_state_mode_repair_skipped, so a symlinked state dir and a container whose sessions/ belongs to
the mounted uid start as they did on main. The destination walk stays the only thing that refuses a
recording. sameStateEntry, state_dir_entry_replaced, the duplicate ownership check and
privateStateRefusal go with the refusal path they served.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ot force

The suites that pinned a state refusal are flipped to the rule: a level this uid does not own, a link,
and a name the host will not open are all skipped with daemon_state_mode_repair_skipped, the daemon
starts, and the destination walk is what refuses the recording under that tree with
script_ancestor_untrusted. The unopenable case injects EACCES at the open seam rather than staging a
0000 mode, because a root process opens a 0000 directory it owns. serveHostOwnership now serves the ids
through fstat as well, which is where a repair that forces a mode asks who owns the entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The paragraph that introduced the repair said the descriptor's fstat must match the lstat that judged the
name. The lstat is now only the fast path, and ownership is answered by the fstat of the descriptor the
chmod goes through, so the ADR says that instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee
thymikee force-pushed the fix/save-script-ancestor-redirect branch from 45d4eb9 to 4b630c1 Compare November 17, 2026 00:32
@thymikee

Copy link
Copy Markdown
Member Author

Folded in as three commits on top of the host-kit round: b9f17ce472 (the repair + the ADR rule), 632cc84121 (the suites that pinned the old refusal), 4b630c1f5b (the ADR sentence that still described the deleted lstat-vs-fstat match).

The rule, in the ADR before the code (docs/adr/0016-active-session-script-publication.md:225): a state-mode repair never decides whether the daemon starts, and it is not a second destination rule. It forces 0700 on a directory entry this uid owns, reached without following a link, and on nothing else. Every other shape changes nothing and emits daemon_state_mode_repair_skipped. The walk stays the only refusal, and the ADR says so in the same paragraph that says the repair exists, so the next reader does not turn the skip back into a gate.

ensurePrivateDaemonStateDir: lstat fast path for a level that already stands 0700 → otherwise open(O_RDONLY | O_DIRECTORY | O_NOFOLLOW) → fstat → fchmod 0700 only if that descriptor's uid is this one. A link, a non-directory, a foreign owner, EACCES, EPERM, EROFS, ENOTDIR (through the one MODE_REPAIR_SKIPPED_CODES set the log repair already had, now shared): skip with the warning. Nothing in that path throws. sameStateEntry, state_dir_entry_replaced, the second ownership check and privateStateRefusal are deleted — the fstat of the descriptor about to be fchmoded is what decides, and there is no refusal left to carry a typed reason.

Both setups you named are pinned as startup tests, not unit tests: a sessions/ another uid owns at 0755, and ~/.agent-device as a symlink. Each asserts the runtime started, that daemon_state_mode_repair_skipped was emitted (counted in a diagnostics scope), that the owner's mode is untouched and that nothing was created on the link's far side — and, for the foreign-owned one, that a default-path recording is refused with script_ancestor_untrusted / exposure: foreign_owned naming that directory. The 0000-directory case is injected as EACCES at the open seam rather than staged as a mode, because a root process opens a 0000 directory it owns and the old staging would have proved nothing under CI's uid. serveHostOwnership now serves ids through fstat as well, since that is where a repair that changes an entry asks who owns it.

Creation of missing state levels keeps the plain recursive mkdir at 0700, which is the choice your item 5 left open; the reasoning and the cost of the alternative are on thread 4593326010, together with what I did change about the force.

The other cuts from the cross-check: WalkOutcome is a three-way ended: 'arrived' | 'short' | 'refused' union and the arrived/refused/stoppedShort builders are folded into the walk — they existed to fill fields no consumer read; the walked.identity === undefined guard in prepareScriptDestination is gone, since a short walk always carries the identity of the directory it judged; the two errno sets are one; the atomic-file.ts comment claiming a umask-002 level "would stand 0775" is corrected to what a umask can actually do to the mode it is handed; and the repeated walk/confirmation prose is trimmed to one place each. The unrelated repo-guards timeout bump is dropped from the branch — that check passes at origin/main here in 1.44s, so I cannot claim a budget I did not measure.

ADR walk counts are stated: remote walks twice per version where the parent stands, three times where a level has to be created, one identity comparison either way; local walks once, twice with a missing parent, compares nothing.

Net production is +1,022 (676 code, 380 comment; excluding tests, __tests__, scripts/, docs/, website/, ledger.json) — 72 lines below the head you reviewed. Red-at-235d0db accounting, so a pin is not mistaken for a rename: every flipped case is red there — the link, foreign-owner and EACCES cases in daemon-registration-owner-state-dir.test.ts, the same two repair shapes again in session-store-teardown-script.test.ts, and both startup cases (foreign-owned, symlinked) in daemon-runtime-state-dir.test.ts. Measured by checking those files out at 235d0db and running them: 5 failed in the first run, 2 in the second, all seven accounted for. Two tests in the rewritten state-dir suite are green at 235d0db on purpose: the repair-it-owns case carried over unchanged, and a new one that asserts a level standing 0700 is never opened at all, which is a cost pin for the fast path rather than a behavior change.

Disclosures carried over: the suites run under umask 022 and foreign ownership is a fixture uid (65534), so neither CI's umask nor a real second account is exercised on real ids; and test/repo-guards/macos-helper-package-contents.test.ts now has main's budget, so it can flake where one npm pack is slow — it runs in well under a second here, and the base comparison at origin/main under concurrent load was 1.44s.

@thymikee

Copy link
Copy Markdown
Member Author

This PR is ready at 4b630c1. The destination walk now refuses before it creates any level, the temp file is made inside the judged directory, and a foreign-owned state level is skipped with a diagnostic. The points from my earlier review are covered. All 21 checks pass at this commit, and the PR is still a draft, so it needs to be marked ready for review before merge.

Not blocking: the fast path in daemon-registration-owner.ts returns on the lstat mode alone, so a symlink with its own 0700 mode (macOS gives this under umask 077) gets no daemon_state_mode_repair_skipped diagnostic, while ADR 0016 says every link shape emits one. Gating it on judged.isDirectory() && fixes this, or the ADR can say that a level already at 0700 is not reported. You can take or leave it.

On evidence: Foreign ownership is simulated by rewriting the uid (65534) and EACCES is injected at openSync. No run used a real second uid, a root daemon, a read-only mount, or umask 002. One accepted trade-off: a user-owned symlinked ~/.agent-device whose target is 0775 is no longer chmod-ed through the link. Default recordings there are refused with shared_group_writable and a chmod g-w hint. Startup is unaffected.

These earlier inline threads no longer apply, so you can resolve them: regex on the destination (no regex runs on it now), walk before level creation (the walk refuses first), mkdtemp location (it runs inside the judged directory), recursive mkdir through a planted level (pre-existing on main, and the chmod half is fixed via an O_NOFOLLOW fd), foreign-owned state level (now skipped with a diagnostic), and typed state_dir reasons (the untyped reasons are deleted).

Two blockers in the best-effort state repair.

A repair step that failed could still stop startup: the `lstat` stood outside the
catch, unlisted errnos were rethrown, and the `close` that ends the repair could
throw. Every step is now inside one catch, so `EACCES` on a level this user cannot
read, `ENOENT` on one that disappears meanwhile, and `EIO` on the close are all
reported as a skipped level and stop there. Creation keeps its own failures.

`O_NOFOLLOW` guards only the LAST name of an open, so judging the requested level
alone let an intermediate link redirect the repair: `sessions -> /etc` makes the
leaf `sessions/ssh` resolve to `/etc/ssh`, and a root daemon forced that mode to
0700. The repair now walks DOWN from the state base, `lstat`s each level under its
own name, and repairs a level only when every level above it passed. A link, a
non-directory or a foreign-owned level stops the descent, and every level beneath
it is left as it is. Callers pass the base they judge from.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Correction to my note above: 4b630c1 is not ready. An independent security cross-check of this head found two blockers in the best-effort state repair in daemon-registration-owner.ts:

  1. Repair errors can still stop daemon startup. The lstat at :337 sits outside the catch, :355 rethrows unlisted errno values, and the close at :358 can throw. EACCES, ENOENT after the directory disappears, and EIO on close all escape with no skip diagnostic.
  2. An intermediate symlink can redirect the repair. O_NOFOLLOW covers only the last path component, so with sessions -> /etc planted in a legacy writable, root-owned state dir, a later ensureSessionDir('ssh') chmods /etc/ssh from 0755 to 0700 as root.

The rule to satisfy: the repair never throws and never changes anything outside the state tree. It walks down from the state base and skips a linked, non-directory or foreign level and everything beneath it. I will review the next head.

A link that carries its own mode answers the mode question with the private mode, so
a repair that read the mode first passed an `lchmod 0700` link without judging it.
The level-by-level repair already asks `isDirectory` before the fast path; this pins
the order, and a skip is what such a link now reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Both blockers are fixed, each with a test that fails on 4b630c1.

1. No repair error reaches startup. The whole repair — lstat, open, fstat, fchmod, close — is now inside one catch. EACCES on a level this user cannot read, ENOENT on a name that disappears between the mkdir and the lstat, and EIO on the close that ends it are each reported as daemon_state_mode_repair_skipped for the level the failure happened on, and the descent stops there. mkdir keeps its own failures, as before.

daemon-registration-owner-state-dir.test.ts runs each of EACCES, ENOENT and EIO through all five seams (lstatSync, openSync, fstatSync, fchmodSync, closeSync), denying the errno for the one entry under test and forwarding every other call to the host. All three are red on 4b630c1, where the lstat stood outside the catch, an unlisted errno was rethrown, and the close could throw.

2. The repair walks down from the state base. ensurePrivateDaemonStateDir(directory, stateBase) judges the chain, not the leaf: lstat each level under its own name, from the state base down, and repair a level only when every level above it passed. A level that is a link, is not a directory, or belongs to another uid stops the descent, and that level and every level beneath it is left exactly as it is. Callers pass the base they judge from — startup passes baseDir, SessionStore passes resolveDaemonStateDir(), the remote writer passes path.dirname(this.sessionsDir).

The reported shape is pinned directly: sessions -> outside, then a repair of sessions/ssh, which resolves on the far side of the link. That test asserts the far-side child keeps 0755, and that both calls report a stop rather than the leaf one passing silently. On 4b630c1 the leaf repair opens through the link and forces 0700. A second case judges it the other way — sessions is a real directory that does get repaired, remote-scripts under it is a link — so the stop is shown to be per level, not an all-or-nothing bail. A state base that is itself a link (~/.agent-device on another disk) is pinned too: creation goes through, the repair reports the one level and stops.

On the recursive mkdir. I left creation as one recursive mkdir, and added no second rule. Creating one level at a time would force a decision about a BASE that is a link — the other-disk shape above — and refusing to create there would break a host the operator pointed at that disk. What creation can leave on ground the chain rejects is an empty directory, and the destination walk refuses any recording under it. The ADR says the same.

The lchmod 0700 link (comment 6934280279, "not blocking"): the level-by-level repair already asks isDirectory before the mode fast path, so the silent pass falls out and the diagnostic is emitted. I made the ordering itself the thing under test rather than a side effect — a new case plants a link carrying its own 0700 and asserts the skip is reported. It is red on 4b630c1, and red on this code with the two checks swapped, so it pins the order and not just the shape. I took the gate option, so the ADR still says every link shape is reported.

Head 8cd5b8ac35, two commits on 4b630c1. Net production vs origin/main is +1,081 (706 code, 409 comment) under the same exclusion as last round. Disclosures carried over: these suites run under umask 022 and foreign ownership is a fixture uid (65534), so neither CI's umask nor a real second account is exercised on real ids.

pnpm check:affected --run --head HEAD passes on a frozen tree at this head, exactCommit=true: format, lint, typecheck, layering, fallow, build, vitest-related, unit, daemon-wire-compat, adr-citations, docs-links and docs-prose.

Keeping this draft until the checks report, so the head under review is the head that was tested.

Two blockers in the state repair.

A remote version followed a state link the repair had skipped. The repair stops at a
link, but the writer used the path the ensure call returned anyway, and the
destination rule accepts a link standing in a parent others cannot write, so
`sessions -> outside` and `remote-scripts -> outside` both published outside the
state tree. Ensuring a state level now answers whether the whole chain from the
state base is private ground, and a destination the daemon picked for itself — a
remote version, or a default recording — is written only on such a chain. The write
returns a typed refusal (`state_chain_untrusted`), the capability carries it as the
`scriptDeliveryFailure` cause and keeps the action's success, and startup ignores
the answer and still starts.

A descriptor ownership rejection did not stop the descent: `forcePrivateStateDir`
returned on a foreign uid while its caller returned `true` regardless, so the repair
continued beneath a level it had just refused. The decision now travels out of the
force step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Both blockers are fixed, each with a test that fails on 8cd5b8a.

1. A remote version no longer follows a state link the repair skipped. Ensuring a state level now answers two things: the path, and whether every level from the state base down to it is private ground (a real directory, this uid's, standing 0700, reached without following a link). ensurePrivateDaemonStateDir(directory, stateBase) returns that record; ensureDaemonStateDir is the path-only form for callers that write no script under the directory — startup and tombstones — which ignore the verdict and keep starting exactly as before.

The two destinations the daemon picks for itself read the verdict: a remote version under sessions/remote-scripts/, and a default recording under sessions/. A chain that was not accepted is a failed write with a typed reason, state_chain_untrusted, and the capability carries it as the scriptDeliveryFailure cause and keeps the action's success — no outside file is written either way. A caller's --save-script=<path> is untouched: that destination is the caller's own, and the destination walk alone decides it.

The parameterized writer tests plant a link at sessions and at remote-scripts, pointing at a current-user-owned 0755 directory, and assert the refused write, no file on the far side, and — through remoteScriptVersionDelivery with remote delivery armed — a scriptDeliveryFailure whose cause is state_chain_untrusted on an ok: true response. Startup's own case (daemon-runtime-state-dir.test.ts) now asserts the foreign-state daemon starts, warns, and refuses its default recording with state_chain_untrusted and an empty sessions/. The ADR says this where the old "the walk is the ONLY thing that refuses a recording" sentence used to be: what the repair ANSWERS is separate from what it forces, and a script reads the answer.

2. A descriptor ownership rejection now stops the descent. forcePrivateStateDir returns its decision and repairStateLevel propagates it, so a level refused at the fstat of the descriptor it was about to force ends the chain instead of continuing beneath it.

The new test injects a foreign uid through that descriptor only — serveHostOwnership gained a mode that moves the id of the entry the open REACHED and leaves the name answering this uid, which is also the name-swap shape the check exists for — asks for the CHILD below the refused level, and asserts: no fchmod at all, both modes unchanged, one skip, the only opened name being the refused level (so the child was never reached), and that descriptor being closed. The open/close accounting moved into the fixture, because the fixture already owns those mocks and a second spy over it loses the descriptor-to-name mapping.

Red at 8cd5b8a for the right reason in each case: the writer tests fail with written: true — the version really was published through the link — and the descriptor test fails on the fchmod of the child the old code went on to force.

Head fa64dfe53b (one commit on 8cd5b8ac35). Net production vs origin/main is +1,205 (780 code, 464 comment) under the same exclusion as last round.

Disclosures carried over: these suites run under umask 022, and foreign ownership is a fixture uid (65534) — no real second account, root daemon, read-only mount, or umask 002 run. The recursive mkdir can still leave an empty directory on the far side of a planted link; the tests assert no file is written there rather than no directory, and the destination walk refuses any publication under it.

pnpm check:affected --run --head HEAD passes on a frozen tree at this head, exactCommit=true: format, lint, typecheck, layering, fallow, build, vitest-related, unit, daemon-wire-compat, adr-citations, docs-links, docs-prose. Locally, the whole unit project and check:production-exports are green at this commit.

A remote close dropped the typed reason. `remoteCloseScript` received only a
boolean, so the close response's `scriptDeliveryFailure` said a version failed
without saying which way it failed. The close write's AppError now travels into
it, and its `details.reason` becomes the failure's `cause`, the same field the
per-action route already fills.

A level that disappeared before the repair reached it stopped the chain with no
diagnostic: `lstat` answering `undefined` returned false silently, while every
other stop reports itself. It reports now.

The refusal hint names the host shape the cross-check found undocumented — a
container or mounted volume that leaves the state tree to another uid — and its
two fixes. The ADR and the two user docs say the same thing plainly: that host
starts and answers every command, and loses only the recordings the daemon picks
a path for; the refusal comes from the chain verdict, not the destination walk.
State-level creation stays best-effort and happens before validation, so it can
leave an empty directory beyond a rejected link, and that is stated too.

Trimmed: the unchanged `prepared` value returned through `allocateStaging`, the
one-use state-directory wrappers, the unused `gid` on `DirectoryAuthority`, and
comments that repeated the same rule at every call site.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

All four items are in, plus the review thread.

1. The remote close keeps its typed reason. finalizeOrdinaryCloseScript already computed the typed AppError and then passed error !== undefined into remoteCloseScript; the error now travels instead. Its details.reason becomes the scriptDeliveryFailure.cause, which is what the per-action route already did. The regression mocks writeSessionLog returning a state_chain_untrusted AppError and asserts remoteCloseScriptDelivery answers ok: true with cause: 'state_chain_untrusted' — red at fa64dfe, where the failure object had no cause key at all.

2. A vanished level reports itself. lstatSync(..., { throwIfNoEntry: false }) answering undefined returned false silently; it now emits daemon_state_mode_repair_skipped. The chain is walked after a recursive mkdir, so at rest every level it asks about exists and the absence is a between-calls race — the test therefore answers the level ABSENT with the real undefined a missing name genuinely produces (asserted against a truly absent name in the same test, so the injected value is the host's own answer), and asserts one skip, unchanged mode, and privateChain: false. Red at fa64dfe with zero skips.

3. The ADR and the docs state the exception and the ownership consequence. ADR 0016 now says creation is best-effort and happens BEFORE any chain is judged — one recursive mkdir builds the whole tail, which is what keeps a base that is a link working — so a stopped chain can leave an EMPTY directory beyond the level it stopped at, and nothing is ever written into it. The stale "every command still works" claim is corrected: such a host keeps a working daemon and loses its daemon-picked recordings, because a level another uid owns can be replaced by them at any time. The refusal is stated as the CHAIN VERDICT's own, not the walk's. Same statement in replay-e2e.md and remote-proxy.md, in user terms, with the two fixes. This closes @apex-sentinel-staging's thread (r4594379825) with the second option the review offered — keep the refusal, document it, and have the hint name the host shape: the state_chain_untrusted hint now names the bind-mount/container shape, chown, AGENT_DEVICE_STATE_DIR, and that an explicit --save-script path is unaffected. That is pinned in the foreign-state startup test, red at fa64dfe.

4. Trimmed: allocateStaging answers the staging directory instead of re-returning the unchanged prepared; the one-use remoteScriptDir/stateSessionsDir wrappers are inlined; the unused gid is out of DirectoryAuthority (and out of the fixture that only existed to serve it — the mode-only test is stronger evidence of the no-private-group rule, which that field proves nothing about); and the comments that repeated the same "walk accepts a link" rule at four call sites now state it once, beside the refusal.

Head 687822affe, one commit on fa64dfe53b. Net production vs origin/main: +1,195 (was +1,205).

The allocateStaging trim is inside the publishScriptVersion digest span, so the wire ledger carries the new digest with a refreshed ack for this PR's own entry (it is unreleased, and the ack is keyed by the digest it covers). check:daemon-wire-compat answers: 217 declarations, 1 changed, 0 removed, 0 added, 0 moved, protocol unchanged.

Disclosures carried over: suites run under umask 022, and foreign ownership is a fixture uid (65534). The recursive mkdir can still leave an empty directory beyond a planted link; tests assert no file, not no directory.

pnpm check:affected --run --head HEAD passes on a frozen tree at this head, exactCommit=true: format, lint, typecheck, layering, fallow, build, vitest-related, unit, daemon-wire-compat, adr-citations, docs-links, docs-prose.

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.

--save-script destination can be redirected by other local users who can write an ancestor directory

2 participants