Skip to content

fix(record): run a cached Swift helper only from ground this user controls - #4670

Merged
thymikee merged 26 commits into
mainfrom
fix/swift-cache-trust
Nov 19, 2026
Merged

thymikee merged 26 commits into
mainfrom
fix/swift-cache-trust

Conversation

@thymikee

@thymikee thymikee commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Summary

Scope is 38 changed files across recording, host-kit, tests, and docs.

  • Move Swift helpers from shared temp to a private v3 cache, with AGENT_DEVICE_SWIFT_CACHE_DIR as an override. Ignore the old cache.
  • Host-kit's creation walk refuses shared or foreign-owned ancestry before creating directories (ADR 0016). Cache subdirectories must be real, owner-only, and owned by this user. Verified executables are reused.
  • Build embedded sources with xcrun --sdk macosx and an allowlisted environment. Resolve and check DEVELOPER_DIR when compiling. ACLs are outside this check.
  • Report swift_cache_untrusted at record stop.
  • Size cuts at 93f6ac6: the embedded recording helpers are comment-stripped with the packager's stripper, lines kept in place. The video validator compiles through compileSwiftSources, so compileSwiftSourceText is gone.

Validation

  • Swift cache link regression: fails at 73b8445, passes at b0f9412.
  • pnpm check:affected --run --head 93f6ac6 passed all runnable checks (exactCommit: true). Evidence is on the PR comment. CI owns native smoke and coverage.
  • Size, b0f9412 to 93f6ac6: unpacked −5,610 B, download −2,370 B.
  • Older evidence stays at its SHA: live iOS recording at 1aa0069, host-only MP4 validation at 41f82c7, 24 passing checks at b0f9412.
  • Gap: no live recording at 93f6ac6. swiftc -typecheck of the stripped helpers passed on macOS.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.40 MB 6.41 MB +4.6 kB
Package (unpacked) 6.40 MB 6.41 MB +4.6 kB
Package (download) 1.94 MB 1.94 MB +2.4 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 21.0 ms 23.9 ms +2.8 ms
CLI --help 43.6 ms 47.5 ms +3.9 ms

@apex-sentinel-staging

This comment has been minimized.

@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: 2 new findings on a062eb4. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

I found one regression and one missing validation at a062eb4. CI is green (18 checks, none failing), and there are no conflicts.

The cache walk now runs before the compile, so its refusal comes before xcrun's TOOL_MISSING (swift-cache.ts:203). On a Linux daemon host with no Swift, take a /tmp/agent-device-swift-cache that an earlier version created as 0775 (umask 002). Android record stop reaches isPlayableVideo, which calls compileSwiftSourceText. That now throws swift_cache_untrusted. isPlayableVideo only swallows a TOOL_MISSING cause (video.ts:117-124), so it rethrows instead of using the container check. Android recording on that host used to succeed and now fails at validation, although no Swift helper can run there. The rule: judge the cache ground only where a Swift helper can be compiled. Please make the validator's no-toolchain outcome cover non-Apple hosts before it touches the cache, and add a test that a refused cache on a non-darwin host still gives the container verdict. This follows from the code and has not been reproduced on Linux.

Every new test mocks runCmd, but the real compile flow changed (swift-cache.ts:222). The source is now written inside a private temp directory, chmod 0755, and the HOME and module-cache dirs are judged on every helper run. The PR body's real-host run covers only the walk. Please run this on macOS with an empty AGENT_DEVICE_SWIFT_CACHE_DIR: record start and record stop on an iOS simulator (MP4 path) must succeed. Show ls -la of the cache root, bin (0700) and bin/video-validator-<key> (0755, your uid). A second record stop must show the same mtime, which proves no recompile. No CI job is known to run a real swiftc compile of these helpers.

Not blocking, take or leave: unavailableVideoValidator (video.ts:199) overwrites the reason, so swift_cache_untrusted never reaches the record-stop caller, and returning that error unchanged would fix it; the wrapper's single fixed hint drops host-kit's chmod g-w <ancestor> hint for shared_group_writable (swift-cache.ts:188), and the as AppError cast there can be an instanceof narrow; FOREIGN_UID and reportForeignOwner in swift-cache-trust.test.ts:25 are a third copy of the test-kit fixture, so the 'change both together' comments there and in host-kit could name it.

Two open inline threads do not apply, so you can resolve them. The walk already makes every level of the spelled path ground only this user or root can change, so publishing via the spelling gives no one a window: #4670 (comment). No user doc describes the Swift cache or AGENT_DEVICE_SWIFT_CACHE_DIR, and the refusal carries its remedy in the hint: #4670 (comment).

Before merge, keep non-Apple hosts out of the cache walk, and attach the live macOS record run with a fresh cache.

@thymikee

Copy link
Copy Markdown
Member Author

Host-only evidence at cefeebd on macOS 27 with Xcode's swiftc. No device and no daemon were used. validate.ts imports this branch's packages/capture-kit/src/recording/video.ts and calls prepareVideoValidator, then isPlayableVideo on the MP4 given as an argument. AGENT_DEVICE_SWIFT_CACHE_DIR points at a fresh, empty directory under $TMPDIR. out.mp4 comes from ffmpeg -f lavfi -i testsrc=d=1 -pix_fmt yuv420p. not-video.mp4 is 64 random bytes.

What this shows:

  • Cold run: it compiled the validator in about 21 s. The root, v3, bin, home and module-cache are all drwx------, and the helper is -rwxr-xr-x, owned by uid 501.
  • Warm run: it took 0.2 s, and the helper's mtime is unchanged (1794996395), so it did not recompile. isPlayableVideo returned true for the real MP4.
  • Helper verdicts: the helper exits 0 for out.mp4 and 1 for not-video.mp4.
  • Group-writable bin: after chmod g+w on bin, the next run is refused with swift_cache_untrusted / shared_group_writable, and the hint names the chmod g-w remedy.

A record start / record stop run on an iOS simulator is still pending.

Output
## run 1 (cold)
validator: $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26
node --experimental-strip-types validate.ts  7.38s user 2.20s system 46% cpu 20.803 total
$ ls -la $TMPDIR/swift-cache-live.JtGjg3/cache
total 0
drwx------@ 3 <user>  staff   96 Nov 18 11:06 .
drwx------  3 <user>  staff   96 Nov 18 11:06 ..
drwx------@ 5 <user>  staff  160 Nov 18 11:06 v3

$ ls -la $TMPDIR/swift-cache-live.JtGjg3/cache/v3
total 0
drwx------@  5 <user>  staff   160 Nov 18 11:06 .
drwx------@  3 <user>  staff    96 Nov 18 11:06 ..
drwx------@  3 <user>  staff    96 Nov 18 11:06 bin
drwx------@  2 <user>  staff    64 Nov 18 11:06 home
drwx------@ 41 <user>  staff  1312 Nov 18 11:06 module-cache

$ ls -la $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin
total 120
drwx------@ 3 <user>  staff     96 Nov 18 11:06 .
drwx------@ 5 <user>  staff    160 Nov 18 11:06 ..
-rwxr-xr-x@ 1 <user>  staff  60416 Nov 18 11:06 video-validator-5e6ff927dd0cdb26

$ ls -la $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26
-rwxr-xr-x@ 1 <user>  staff  60416 Nov 18 11:06 $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26

$ stat -f '%m %N' $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26
1794996395 $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26

uid: 501
## run 2 (warm)
validator: $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26
isPlayableVideo: true
node --experimental-strip-types validate.ts out.mp4  0.20s user 0.06s system 115% cpu 0.224 total
$ stat -f '%m %N' $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26
1794996395 $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26

## helper verdicts
$ $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin/video-validator-5e6ff927dd0cdb26 $TMPDIR/live/out.mp4

exit=0
$ <helper> not-video.mp4 -> exit=1
## bin made group-writable
$ chmod g+w $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin

$ ls -ld $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin
drwx-w----@ 3 <user>  staff  96 Nov 18 11:06 $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin

refused: {
  "code": "COMMAND_FAILED",
  "message": "Refusing to use the Swift helper cache directory $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin: it is not private to this user.",
  "details": {
    "reason": "swift_cache_untrusted",
    "exposure": "shared_group_writable",
    "ancestor": "$TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin",
    "hint": "Close the directory to its group with `chmod g-w $TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin`, or set AGENT_DEVICE_SWIFT_CACHE_DIR to a directory you own that only you can write. Only owner and mode bits are checked: ACLs on an override directory are yours to keep closed.",
    "directory": "$TMPDIR/swift-cache-live.JtGjg3/cache/v3/bin"
  }
}
exit=1

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@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: 2 new findings on 2ca6d59. The verdict and the full list are in the summary comment.

Comment thread packages/test-kit/src/tmp-dir.ts Resolved
Comment thread packages/test-kit/src/tmp-dir.ts Resolved
@apex-sentinel-staging

This comment has been minimized.

@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: 2 new findings on 9628850. The verdict and the full list are in the summary comment.

Comment thread scripts/vitest-tmpdir-global-setup.ts Resolved
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the follow-up. At 3ea710b the code fixes from the earlier review are in, but a separate security cross-check found two blockers in how the compiler is launched, and one piece of evidence is still missing. CI is green on all 18 checks, but no CI job runs a real swiftc compile of these helpers, so green CI does not cover the changed compile and exec route.

The real compile, publish and exec flow at this head has never run through the daemon (swift-cache.ts:375). The only live run is the host-only script at cefeebd. Five later commits changed swift-cache.ts: the sticky-ancestor refusal (7145837), judging children inside v3 (c6094bb), refuse instead of delete (8665c41) and the hint changes (9628850, 2c0e0d3). Every unit and provider test stubs xcrun. A wrong refusal on the real default root, or a broken HOME or module-cache env for the real swiftc, would break every MP4 record stop on macOS, and no test would catch it. Please run this on macOS at this head, or a later head where swift-cache.ts is unchanged. Leave AGENT_DEVICE_SWIFT_CACHE_DIR unset and move ~/Library/Caches/agent-device/swift-cache aside. On an iOS simulator, run record start, one tap and record stop (MP4); both must succeed. Then show ls -la of swift-cache, v3, bin, home and module-cache (all drwx------, your uid) and of bin/video-validator- and bin/recording-overlay- (-rwxr-xr-x, your uid). Run a second record stop on a new recording and show the same validator mtime (stat -f %m), which proves no recompile.

Not blocking, take or leave: the docs row (configuration.md:165) and the record help (recording/index.ts:132) say chmod does not fix an exposed v3, bin, home or module-cache, but judgeOwnedDirectory judges only the current mode, so chmod 700 clears the refusal and reuses whatever was written while it was open, so both texts could say chmod clears the refusal but not that content, and to remove the directory or point AGENT_DEVICE_SWIFT_CACHE_DIR at a new one; untrustedAncestorOf restates the ancestor rule from swift-cache.ts:103-118, which makes three spellings of one rule; and test-kit exports mkdtempUnsharedForTestSync with no caller, because the capture-kit tests use their own copy.

Could host-kit own one "no sticky pass-through" trust mode for private ancestry, for example a prepareScriptDestination option or an exported privateAncestryReason(path) that requires uid-or-root and no 0o022, sticky or not? swift-cache.ts could call it in place of the post-walk loop at :103-118, the test sweep could call it in place of untrustedAncestorOf, and the unused test-kit helper could go. The rest (v3 0700 judging, reuse predicate, O_NOFOLLOW fchmod, in-flight memo, darwin gate) looks like the minimal shape for the threat. The earlier question about moving the default to the per-user cache dir is answered. This would need an ADR 0016 amendment first, naming the mode for executable state, so host-kit owns both variants of the rule.

None of the four open inline threads still applies at this head, so please resolve them: no chmod of a shared root remains, and a missing unshared dir now skips or fails on CI; both helpers now return realpath of the mkdtemp result; swiftCacheHint names the ancestor as text and builds no command, with a test for a path with a space and a quote; a skipped sweep records the unavailable marker and tests skip, or throw on CI.

Security blockers from the cross-check (the cache tree itself holds up: a swapped binary is refused, and Linux Android stop keeps the MP4 container check):

  1. Compiler intermediates can leave the private cache (swift-cache.ts:413). swiftc inherits TMPDIR, TEMP and TMP. If they point at scratch space where another local user can replace entries, the compile and link jobs can consume altered intermediates. The published binary is then written by your own toolchain, so it passes every owner and mode check. Set TMPDIR, and set or clear TEMP and TMP, to the private per-compile staging directory. Add a test at the compiler launch boundary with an inherited shared temp path.
  2. The compiler launch trusts PATH (swift-cache.ts:409). xcrun is launched by bare name with the inherited PATH. A PATH entry that another user can write to, ahead of the system tools, runs their xcrun as the daemon user, even with a private cache and a trusted install. Launch /usr/bin/xcrun by absolute path with a controlled child PATH. Constrain the inherited compiler and loader overrides (DYLD_*, SDKROOT, TOOLCHAINS). Check where a DEVELOPER_DIR override points before using it.

Before the label: fix both blockers, then attach the live simulator record run described above, at a head where swift-cache.ts no longer changes.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

Live simulator evidence at 6a81c44, the frozen head where swift-cache.ts no longer changes. The maintainer approved this one run of the branch's own CLI.

  • Build: this branch at 6a81c44 with pnpm build. agent-device --version reports 0.21.40-dev.
  • Device: iOS simulator BC54AC11-CD86-4C10-8C4E-5101C773FFEB (iPhone 17 Pro, iOS 27.0). It was already booted, so this run did not boot or shut it down.
  • Daemon: this branch's own daemon, with AGENT_DEVICE_STATE_DIR set to a fresh directory (<state> below), so the shared daemon was not involved. Stopped with daemon stop --clean at the end, and the state directory removed.
  • Cache: AGENT_DEVICE_SWIFT_CACHE_DIR unset and DEVELOPER_DIR unset (xcode-select picks Xcode 27.1). The existing ~/Library/Caches/agent-device/swift-cache was moved aside first, so this run compiled into a fresh default cache, and the original was restored afterwards.

Result: record start and record stop both succeeded, three times. The default cache tree is drwx------ throughout, owned by uid 501. Both helpers compiled at the first stop as -rwxr-xr-x, uid 501. On two later stops neither helper's mtime changed, so neither was recompiled.

Commands

Every agent-device call ran with --platform ios --udid BC54AC11-CD86-4C10-8C4E-5101C773FFEB --session live-4670.

$ agent-device open com.apple.Preferences
Building Apple runner...
Opened: com.apple.Preferences
[exit 0]
$ agent-device snapshot -i
@e6 [cell] "General"   (19 visible nodes, excerpt)
[exit 0]
$ agent-device record start <state>/recording-1.mp4
<state>/recording-1.mp4
[exit 0]
$ agent-device press @e6
Tapped @e6 (201, 319)
[exit 0]
$ agent-device record stop
<state>/recording-1.mp4
[exit 0]

Cache tree after the first stop (uid 501)

$ ls -la ~/Library/Caches/agent-device/swift-cache
drwx------@ 3 <user>  staff   96 Nov 18 15:07 .
drwx------@ 5 <user>  staff  160 Nov 18 15:07 v3

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3
drwx------@  5 <user>  staff   160 Nov 18 15:07 .
drwx------@  4 <user>  staff   128 Nov 18 15:07 bin
drwx------@  2 <user>  staff    64 Nov 18 15:07 home
drwx------@ 54 <user>  staff  1728 Nov 18 15:07 module-cache

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/bin
drwx------@ 4 <user>  staff     128 Nov 18 15:07 .
-rwxr-xr-x@ 1 <user>  staff  198736 Nov 18 15:07 recording-overlay-179e2defc1ad0648
-rwxr-xr-x@ 1 <user>  staff   60416 Nov 18 15:07 video-validator-5e6ff927dd0cdb26

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/home
drwx------@ 2 <user>  staff   64 Nov 18 15:07 .

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/module-cache   (excerpt)
drwx------@ 54 <user>  staff     1728 Nov 18 15:07 .
drwxr-x---@ 74 <user>  staff     2368 Nov 18 15:07 15FYUDQ3LLGLA
-rw-r--r--@  1 <user>  staff      898 Nov 18 15:07 _Concurrency-3R4E5Q7Z0QBD7.swiftmodule

The entries inside module-cache are created by Clang with its own modes. They sit inside the drwx------ module-cache, which only this user can enter.

No recompile on later stops

$ stat -f '%m %N' bin/video-validator-* bin/recording-overlay-*        (before)
1795010857 ~/Library/Caches/agent-device/swift-cache/v3/bin/video-validator-5e6ff927dd0cdb26
1795010867 ~/Library/Caches/agent-device/swift-cache/v3/bin/recording-overlay-179e2defc1ad0648

$ agent-device record start <state>/recording-2.mp4
[exit 0]
$ agent-device press @e7
Error (COMMAND_FAILED): Ref @e7 belongs to an expired ref frame ... Not dispatched.
[exit 1]
$ agent-device record stop
<state>/recording-2.mp4
[exit 0]

$ agent-device snapshot -i
[exit 0]
$ agent-device record start <state>/recording-3.mp4
[exit 0]
$ agent-device press @e3
Tapped @e3 (38, 84)
[exit 0]
$ agent-device record stop
<state>/recording-3.mp4
[exit 0]

$ stat -f '%m %N' bin/video-validator-* bin/recording-overlay-*        (after both stops)
1795010857 ~/Library/Caches/agent-device/swift-cache/v3/bin/video-validator-5e6ff927dd0cdb26
1795010867 ~/Library/Caches/agent-device/swift-cache/v3/bin/recording-overlay-179e2defc1ad0648

The tap in the second recording was refused because I reused a ref from a snapshot taken before the first tap navigated. That is the operator's mistake, not a product failure, and that record stop still succeeded. The third recording took a fresh snapshot, and its tap and stop both succeeded. Recording sizes: 1,750,431, 40,172 and 1,610,664 bytes.

Cleanup

$ agent-device close
Closed: live-4670
[exit 0]
$ agent-device daemon stop --clean
Daemon stopped (graceful).
Retained runner cleanup completed.
[exit 0]

The state directory was removed and the original cache was put back.

@thymikee

Copy link
Copy Markdown
Member Author

Thanks for the update. At 6a81c44 the new DEVELOPER_DIR judging and the stripped compiler env close most of the earlier findings, and the live simulator run you posted (#4670 (comment)) shows the real compile, publish and reuse working on a fresh default cache. Two environment bypasses remain.

The compiler env is still built by removing names from the inherited environment. swiftCompilerEnv drops only the names in COMPILER_ENV_OVERRIDES (:28). On macOS, swift-driver links through clang (swiftc -### lists the clang link job on Xcode 27.1). clang honors COMPILER_PATH and CCC_OVERRIDE_OPTIONS, and neither is on the list: COMPILER_PATH=/usr/bin xcrun --sdk macosx clang -### runs /usr/bin/ld in place of the toolchain linker, and CCC_OVERRIDE_OPTIONS='+-fuse-ld=/usr/bin/true' runs /usr/bin/true as the linker. The driver also reads SWIFT_DRIVER_SWIFTSCAN_LIB and SWIFT_DRIVER_TOOLCHAIN_CASPLUGIN_LIB, which are not stripped either. So a daemon env with COMPILER_PATH pointing into a directory another account can write runs that account's linker as this user. That linker writes the published helper, and the helper then passes every owner and mode check. The cache can still run a helper chosen by another local user, and the PR body claim "no loader, search-path or driver overrides" does not hold. Each new toolchain variable would need another regex entry, so the denylist does not converge. The rule the code must satisfy: the compiler child env is constructed, not filtered, and no inherited variable reaches xcrun, swiftc, clang or ld unless it is named. Build it from an allowlist (HOME and CLANG_MODULE_CACHE_PATH from the cache, PATH, TMPDIR/TEMP/TMP set to staging, the judged DEVELOPER_DIR, and at most LANG/LC_*), then delete COMPILER_ENV_OVERRIDES. Please replace the STRIPPED_COMPILER_OVERRIDES test with one that sets unknown variables (for example COMPILER_PATH and CCC_OVERRIDE_OPTIONS) and asserts the launch env keys equal the allowlist.

The cached helpers have the same problem at launch. The validator, overlay and frame decoder all run with buildSwiftToolEnv, which copies the host environment (swift-cache.ts:324). A daemon started with DYLD_INSERT_LIBRARIES or DYLD_LIBRARY_PATH pointing somewhere another account can write makes every helper load that code, even on a cache hit, while the binary on disk stays private and unchanged. Please give the three helper launches the same constructed environment, without loader variables, and test both a cold launch and a warm launch at the subprocess boundary.

Once the allowlist lands, please repeat the simulator run once at that head, since the compiler launch changes again. Include one pass with DEVELOPER_DIR=/Applications/<your Xcode>.app exported to the daemon, so the bundle normalization and the /Applications allowance run on a real install.

One item is a scope decision for the maintainer rather than a code fix. The system-selected Xcode, and an explicit one under root:admin 0775 /Applications, can still be replaced by another member of the admin group. Closing that fully would mean pinning and validating the whole toolchain tree. Members of the admin group can already use sudo, so I will post the maintainer's call on whether they count as trusted (like root) here.

Not blocking, take or leave: untrustedDeveloperDirLevel (:412) is a fourth spelling of the private-ancestry rule, and effectiveDeveloperDir duplicates apple-runner's resolveSelectedDeveloperDir with a different bundle rule (.app suffix here, Contents/Developer there), so host-kit could own both when the ADR 0016 amendment lands; and the new swift_cache_untrusted refusal with exposure: untrusted_developer_dir is not covered by the recording help or the configuration.md row, so one sentence saying to unset DEVELOPER_DIR or point it at an absolute Xcode path that root or you own along with every directory above it would help agents that follow the help.

Could the allowlist and a single host-kit DEVELOPER_DIR resolver, judged by the same private-ancestry predicate as the cache with a named /Applications allowance, replace the denylist and the effectiveDeveloperDir/realpathIfPresent/untrustedDeveloperDirLevel trio? The three DEVELOPER_DIR commits in this delta (ae952ad, 1be1062, 6a81c44) look like special cases around one selection that a single owner would absorb. The allowlist needs nothing first; the resolver needs the ADR 0016 amendment already named in the PR body.

All 18 checks pass at this commit. None of them compile the helpers with a real swiftc. The bypass comes from dry runs on a macOS host: swiftc -### shows clang as the link job, and clang -### shows clang honoring both variables. The two SWIFT_DRIVER_* library variables were found in the swift-driver binary, and a plain compile has not been shown to load them.

The four earlier inline threads on the unshared test directory helpers and judgeOwnedDirectory no longer apply at this head, so they can be resolved: r4613109624 (no chmod of a shared root remains; the helper skips, or throws on CI), r4613109637 (the second helper was deleted, so no unresolved spelling is left), r4613441113 (the hint still only names the directory as text and builds no command), and r4613441152 (vitest-tmpdir-global-setup.ts is unchanged and its skip-or-throw behavior still stands).

Before merge, please build the compiler env from an allowlist so inherited COMPILER_PATH, CCC_OVERRIDE_OPTIONS and SWIFT_DRIVER_* cannot choose the linker or driver libraries. Give the helper launches the same constructed environment, then repeat the simulator run once at that head.

@thymikee

Copy link
Copy Markdown
Member Author

Live simulator evidence at e707e65, the new frozen head after the allowlisted compiler and helper environments. This repeats the run once under the same one-run approval.

  • Build: this branch at e707e65 with pnpm build. agent-device --version reports 0.21.40-dev.
  • Device: iOS simulator BC54AC11-CD86-4C10-8C4E-5101C773FFEB (iPhone 17 Pro, iOS 27.0). It was already booted, so this run did not boot or shut it down.
  • Daemon: this branch's own daemon, each pass with its own fresh AGENT_DEVICE_STATE_DIR (<state> below). Each daemon was stopped with daemon stop --clean at the end of its pass, and both state directories were removed.
  • Cache: AGENT_DEVICE_SWIFT_CACHE_DIR unset. The existing ~/Library/Caches/agent-device/swift-cache was moved aside before pass 1. Each pass compiled into its own fresh default cache, both were removed afterwards, and the original was restored.
  • Flags: every agent-device call ran with --platform ios --udid BC54AC11-CD86-4C10-8C4E-5101C773FFEB --session live-4670.

Result: both passes passed. In each, record start and record stop (MP4) succeeded, and the cache tree was drwx------ throughout, owned by uid 501. Both helpers were compiled as -rwxr-xr-x, uid 501. In pass 1, neither helper's mtime changed across a second recording, so neither was recompiled.

Pass 1: DEVELOPER_DIR unset (xcode-select picks Xcode 27.1)

$ agent-device open com.apple.Preferences --relaunch
[exit 0]
$ agent-device snapshot -i
[exit 0]
$ agent-device record start <state>/recording-1.mp4
[exit 0]
$ agent-device press @e4
Tapped @e4 (201, 213)
[exit 0]
$ agent-device record stop
[exit 0]

$ ls -la ~/Library/Caches/agent-device/swift-cache
drwx------@ 3 <user>  staff   96 Nov 18 15:25 .
drwx------@ 5 <user>  staff  160 Nov 18 15:25 v3

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3
drwx------@  5 <user>  staff   160 Nov 18 15:25 .
drwx------@  4 <user>  staff   128 Nov 18 15:26 bin
drwx------@  2 <user>  staff    64 Nov 18 15:25 home
drwx------@ 54 <user>  staff  1728 Nov 18 15:26 module-cache

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/bin
drwx------@ 4 <user>  staff     128 Nov 18 15:26 .
-rwxr-xr-x@ 1 <user>  staff  198736 Nov 18 15:26 recording-overlay-179e2defc1ad0648
-rwxr-xr-x@ 1 <user>  staff   60416 Nov 18 15:26 video-validator-5e6ff927dd0cdb26

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/home
drwx------@ 2 <user>  staff   64 Nov 18 15:25 .

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/module-cache   (excerpt)
-rw-r--r--@  1 <user>  staff      898 Nov 18 15:25 _Concurrency-3R4E5Q7Z0QBD7.swiftmodule
drwxr-x---@ 74 <user>  staff     2368 Nov 18 15:26 15FYUDQ3LLGLA

$ stat -f '%m %N' bin/video-validator-* bin/recording-overlay-*   (after the first stop)
1795011962 ~/Library/Caches/agent-device/swift-cache/v3/bin/video-validator-5e6ff927dd0cdb26
1795011972 ~/Library/Caches/agent-device/swift-cache/v3/bin/recording-overlay-179e2defc1ad0648

$ agent-device snapshot -i
[exit 0]
$ agent-device record start <state>/recording-2.mp4
[exit 0]
$ agent-device press @e9
Tapped @e9 (201, 523)
[exit 0]
$ agent-device record stop
[exit 0]

$ stat -f '%m %N' bin/video-validator-* bin/recording-overlay-*   (after the second stop)
1795011962 ~/Library/Caches/agent-device/swift-cache/v3/bin/video-validator-5e6ff927dd0cdb26
1795011972 ~/Library/Caches/agent-device/swift-cache/v3/bin/recording-overlay-179e2defc1ad0648

$ agent-device close
Closed: live-4670
$ agent-device daemon stop --clean
Daemon stopped (graceful).
Retained runner cleanup completed.

Clang creates the entries inside module-cache with its own modes. They sit inside the drwx------ module-cache, which only this user can enter.

Pass 2: DEVELOPER_DIR=/Applications/Xcode-27.1.0.app exported to the branch daemon, fresh cache

The pass-1 cache was moved aside first. The daemon for this pass was started with the variable set. Its bundle form is taken at Contents/Developer, and /Applications is admitted only by the stock root:admin exception.

$ export DEVELOPER_DIR=/Applications/Xcode-27.1.0.app
$ agent-device open com.apple.Preferences --relaunch
Opened: com.apple.Preferences
[exit 0]
$ agent-device snapshot -i
[exit 0]
$ agent-device record start <state>/recording-1.mp4
<state>/recording-1.mp4
[exit 0]
$ agent-device press @e4
Tapped @e4 (201, 213)
[exit 0]
$ agent-device record stop
<state>/recording-1.mp4
[exit 0]

$ ls -la ~/Library/Caches/agent-device/swift-cache
drwx------@ 3 <user>  staff   96 Nov 18 15:27 .
drwx------@ 5 <user>  staff  160 Nov 18 15:27 v3

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3
drwx------@  5 <user>  staff   160 Nov 18 15:27 .
drwx------@  4 <user>  staff   128 Nov 18 15:27 bin
drwx------@  2 <user>  staff    64 Nov 18 15:27 home
drwx------@ 54 <user>  staff  1728 Nov 18 15:27 module-cache

$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/bin
drwx------@ 4 <user>  staff     128 Nov 18 15:27 .
-rwxr-xr-x@ 1 <user>  staff  198736 Nov 18 15:27 recording-overlay-179e2defc1ad0648
-rwxr-xr-x@ 1 <user>  staff   60416 Nov 18 15:27 video-validator-5e6ff927dd0cdb26

$ stat -f '%m %N' bin/video-validator-* bin/recording-overlay-*
1795012050 ~/Library/Caches/agent-device/swift-cache/v3/bin/video-validator-5e6ff927dd0cdb26
1795012059 ~/Library/Caches/agent-device/swift-cache/v3/bin/recording-overlay-179e2defc1ad0648

$ agent-device close
Closed: live-4670
$ agent-device daemon stop --clean
Daemon stopped (graceful).
Retained runner cleanup completed.

The newer mtimes show that pass 2 compiled both helpers itself, with the bundle-form DEVELOPER_DIR, into its own fresh cache.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

Live simulator evidence at 59ab46d, after the helper-launcher change. This is pass 1 only, under the same one-run approval.

  • Build: this branch at 59ab46d with pnpm build. agent-device --version reports 0.21.40-dev.
  • Device: iOS simulator BC54AC11-CD86-4C10-8C4E-5101C773FFEB. It was already booted, so this run did not boot or shut it down.
  • Daemon: this branch's own daemon with a fresh AGENT_DEVICE_STATE_DIR (<state>), stopped with daemon stop --clean, and its state directory removed.
  • Cache: DEVELOPER_DIR and AGENT_DEVICE_SWIFT_CACHE_DIR unset. The existing default cache was moved aside, this run compiled into a fresh one, and the original was restored afterwards.
  • Flags: every agent-device call ran with --platform ios --udid BC54AC11-CD86-4C10-8C4E-5101C773FFEB --session live-4670.

Result: passed. record start, the tap and record stop (MP4) succeeded twice. The cache tree is drwx------, owned by uid 501, and both helpers are -rwxr-xr-x, uid 501. Neither helper's mtime changed across the second recording, so neither was recompiled.

$ agent-device open com.apple.Preferences --relaunch
[exit 0]
$ agent-device snapshot -i
[exit 0]
$ agent-device record start <state>/recording-1.mp4
<state>/recording-1.mp4
[exit 0]
$ agent-device press @e4
Tapped @e4 (201, 213)
[exit 0]
$ agent-device record stop
<state>/recording-1.mp4
[exit 0]
$ ls -la ~/Library/Caches/agent-device/swift-cache
drwx------@ 3 <user>  staff   96 Nov 18 15:51 .
drwx------@ 4 <user>  staff  128 Nov 18 15:51 ..
drwx------@ 5 <user>  staff  160 Nov 18 15:51 v3
$ ls -la ~/Library/Caches/agent-device/swift-cache/v3
drwx------@  5 <user>  staff   160 Nov 18 15:51 .
drwx------@  3 <user>  staff    96 Nov 18 15:51 ..
drwx------@  4 <user>  staff   128 Nov 18 15:51 bin
drwx------@  2 <user>  staff    64 Nov 18 15:51 home
drwx------@ 54 <user>  staff  1728 Nov 18 15:51 module-cache
$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/bin
drwx------@ 4 <user>  staff     128 Nov 18 15:51 .
drwx------@ 5 <user>  staff     160 Nov 18 15:51 ..
-rwxr-xr-x@ 1 <user>  staff  198736 Nov 18 15:51 recording-overlay-179e2defc1ad0648
-rwxr-xr-x@ 1 <user>  staff   60416 Nov 18 15:51 video-validator-5e6ff927dd0cdb26
$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/home
drwx------@ 2 <user>  staff   64 Nov 18 15:51 .
drwx------@ 5 <user>  staff  160 Nov 18 15:51 ..
$ ls -la ~/Library/Caches/agent-device/swift-cache/v3/module-cache   (excerpt)
-rw-r--r--@  1 <user>  staff      901 Nov 18 15:51 _Builtin_float-27CXJTMXMP3XB.swiftmodule
-rw-r--r--@  1 <user>  staff      898 Nov 18 15:51 _Concurrency-3R4E5Q7Z0QBD7.swiftmodule
-rw-r--r--@  1 <user>  staff     1105 Nov 18 15:51 _DarwinFoundation1-3GRWFIAQM915D.swiftmodule
$ stat -f '%m %N' bin/video-validator-* bin/recording-overlay-*  (after the first stop)
1795013512 ~/Library/Caches/agent-device/swift-cache/v3/bin/video-validator-5e6ff927dd0cdb26
1795013518 ~/Library/Caches/agent-device/swift-cache/v3/bin/recording-overlay-179e2defc1ad0648
$ agent-device snapshot -i
[exit 0]
$ agent-device record start <state>/recording-2.mp4
<state>/recording-2.mp4
[exit 0]
$ agent-device press @e9
Tapped @e9 (201, 523)
[exit 0]
$ agent-device record stop
<state>/recording-2.mp4
[exit 0]
$ stat -f '%m %N' bin/video-validator-* bin/recording-overlay-*  (after the second stop)
1795013512 ~/Library/Caches/agent-device/swift-cache/v3/bin/video-validator-5e6ff927dd0cdb26
1795013518 ~/Library/Caches/agent-device/swift-cache/v3/bin/recording-overlay-179e2defc1ad0648
$ agent-device close
Closed: live-4670
[exit 0]
$ agent-device daemon stop --clean
Daemon stopped (graceful).
Retained runner cleanup completed.
[exit 0]

thymikee and others added 9 commits November 18, 2026 15:52
…trols

The Swift helper cache defaults to a fixed name under the system temp
directory and reused any executable file found at a predictable path. On a
host where that directory is shared, another local user could plant the
helper first and have it run as the current user.

Each cache directory is now judged by host-kit's ancestry rule and missing
levels are created owner-only. A cached executable is reused only when it is
a regular file, not a link, owned by this user and not group- or
other-writable; anything else is compiled over. Source text is written inside
the compile's private temp directory. Untrusted ground is refused with
reason swift_cache_untrusted and a hint naming AGENT_DEVICE_SWIFT_CACHE_DIR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
All cache state now lives under <root>/v3: bin, home, module-cache, the
compile lock and its temp directories. Each level is judged by host-kit's
ancestry rule and created 0700, the version directory is the one trust
boundary, and nothing an earlier layout compiled or cached is reused.

- The default root moves to this user's cache directory
  (~/Library/Caches/agent-device/swift-cache on macOS, XDG cache elsewhere);
  AGENT_DEVICE_SWIFT_CACHE_DIR stays the override. Test lanes no longer pin
  the old temp location.
- Work uses the judged physical bin, confirmed unchanged before the reuse
  check and before the rename.
- The video validator memoizes only in-flight work, so every resolution
  runs the reuse check again, and it answers no-toolchain on non-macOS
  hosts before touching the cache.
- swift_cache_untrusted survives the validator wrapper, permission errors
  in the walk are classified as it, and a group-writable directory keeps
  the chmod g-w remedy in the hint.
- Configuration docs and record help name the variable, the default, the
  privacy rule and the operator's ACL responsibility.

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

The video validator now compiles only on macOS hosts, so on a Linux runner
the iOS recording case never reached its compile and waited for a call
that cannot come. The file now runs on the darwin platform it describes,
wherever the suite runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The version directory is the boundary. Every directory above it must be
  closed to the group and other users, sticky or not, so a cleanup cannot
  remove the cache and let another account claim its name before a helper
  runs.
- A version directory this user owns is made 0700 through an
  O_DIRECTORY|O_NOFOLLOW descriptor, so a lock created under umask 002 in an
  older 0755 cache is unreachable. One owned by another uid is refused.
- The compile output is made 0755 through an O_NOFOLLOW descriptor.
- EACCES/EPERM from the lock, mkdtemp, chmod and rename are
  swift_cache_untrusted. Compiler failures keep the toolchain path.
- The first bin confirmation is gone: the walk just answered it. The
  confirmation after the lock wait now has its own test.
- Exports with no outside consumer are private.
- Tests that reach the cache keep it in the checkout's ignored .tmp/, since
  /tmp is a sticky shared directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- bin, home and module-cache are judged directly. Each is created 0700 in
  the judged version directory, an existing one must be a real directory
  (lstat, no link) owned by this uid, and a wider one is closed to its owner
  through an O_DIRECTORY|O_NOFOLLOW descriptor. The link-following child
  walk is gone, so no link can send the compile, lock or helper outside v3.
- Confirmation compares the directory's own lstat identity.
- One wrapper maps EACCES/EPERM from every cache filesystem operation
  (lookup, lock, mkdtemp, source write, chmod, rename, cleanup) to
  swift_cache_untrusted. Typed errors, compiler failures included, keep
  their path.
- Test caches live in a per-run directory under the checkout's .tmp/, which
  the Vitest global setup removes after the run and prunes for killed runs.
  The provider harness removes its cache in finally.
- The configuration row states the ancestry rule precisely.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- If v3 or one of its cache directories is wider than 0700, the version
  directory is removed (rmSync does not follow links) and recreated private,
  so a link or lock entry planted while it was open never survives. This
  replaces the tighten-through-descriptor code. Links, non-directories and
  foreign owners are still refused.
- A staging cleanup that fails no longer replaces the compiler failure: the
  failure stays primary with the cleanup error as secondary detail.
- The checkout-local test sweep acts only on a real directory this uid owns
  (lstat) and skips with a warning otherwise. It keeps a run directory with
  no parseable pid. A test pins teardown's removal of the run directory.
- The configuration row scopes the chmod remedy to mode bits writable by the
  group or other users.

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

The Swift cache no longer deletes cache state. If v3, bin, home or
module-cache is wider than 0700, a link, not a directory, or owned by
another uid, it is refused with swift_cache_untrusted and a hint that names
it for rm -rf, or AGENT_DEVICE_SWIFT_CACHE_DIR. This code creates those
directories at 0700, so an exposed one means someone changed it. With no
deletion, a v3 link cannot redirect a recursive removal, and two processes
cannot delete each other's cache.

- The cache root is judged by the walk. v3 must then be a real directory
  directly beneath it, owned by this uid and created 0700 if missing, so a
  link-resolved v3 never reaches a mutation.
- The checkout-local test sweep, and the helpers' fallback, require every
  level from the checkout root down to be a real directory this uid owns
  that the group and other users cannot write. The sweep no longer chmods.

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

- The checkout-local root for test state is now unshared-test-dirs-<uid>,
  so in a shared checkout another user's runs never share it. The helper's
  fallback already verifies every level and runs no chmod.
- mkdtempUnsharedForTestSync returns fs.realpathSync of the directory it
  created, so callers get the physical path the Swift cache compares. The
  per-test realpath workarounds are gone.

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

- sweepUnsharedRunDirectories is split at its seams: judging the chain from
  the checkout root, creating a missing level, the private-directory test,
  and choosing the prunable run directories. Behavior is unchanged.
- mkdtempUnsharedForTestSync moves to its own module in test-kit and in
  capture-kit, and keeps only the run-directory path: every caller runs under
  the Vitest global setup, which names that directory once the chain is
  private. The fallback chain check was the clone and is gone. tmp-dir.ts
  and tmp-dir.fixtures.ts are back to their main versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The cache key for embedded sources and for inline source text now
  includes the fixed compiler arguments (--sdk macosx swiftc). A release
  that changes them without bumping the cache version gets new entries
  instead of reusing a binary built with other flags. A test plants
  executables at the old, argument-less entry names and shows they are not
  reused.
- The overlay provenance test asserts that every Swift input handed to
  swiftc lies in the compile's private staging directory, as well as
  holding the embedded bytes. Bytes alone could not tell a staged file from
  the real checkout source.
- The drift test asserts each helper's exact, ordered file list.

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

Copy link
Copy Markdown
Member Author

The two findings from the earlier review (#4670 (comment)) are fixed at 9eb7311: the disk-path sources and the competing script above the package are gone, and the overlay and contact-sheet helpers now compile embedded sources from staging. One docs finding remains.

The new configuration entry says recording refuses an unsafe DEVELOPER_DIR selection (configuration.md:165). A macOS recording with reusable Swift executables returns at swift-cache.ts:465 before the selection is checked. The check at swift-cache.ts:336-342 runs only when a helper compiles, and helper execution drops DEVELOPER_DIR at lines 293-296. So the same value can pass with a warm cache and fail when a helper next needs compilation. Please qualify the entry: recording validates DEVELOPER_DIR when it compiles a Swift helper, and it can reuse an existing helper without consulting that selection.

The security cross-check of this head is clean. No conflicts are known. Only one check is still pending and has not failed, and the other 23 pass. Writing staged sources with an explicit 0o600 mode is optional, since the 0700 staging directory already confines them.

@apex-sentinel-staging

This comment has been minimized.

…ualify the DEVELOPER_DIR docs

- The configuration row and record help say DEVELOPER_DIR is checked when
  a Swift helper compiles, and that a cached helper is reused without
  checking it.
- The stricter cache ancestry rule now runs on the nearest existing
  directory before host-kit's walk can create the cache root, so no level is
  made under ground the cache would then refuse. It runs again on the
  completed chain. The sticky-directory refusal test asserts nothing was
  created.
- Staged sources are written with mode 0600 alongside wx. A test checks it
  under umask 022.
- The source generator refuses an input that is not a regular file, such
  as a link.

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

Copy link
Copy Markdown
Member Author

At d3ba120 the docs finding from the 9eb7311 review (#4670 (comment)) is fixed. The configuration entry now says DEVELOPER_DIR is validated when a helper compiles. The stricter ancestry rule also runs before anything is created, and the code review found nothing new. A separate security cross-check of this head found one remaining case of that new ordering, so this is not ready yet.

A cache root reached through a symlink is created before the refusal. Suppose AGENT_DEVICE_SWIFT_CACHE_DIR names a symlink in a private directory, and the link target is a missing directory under a sticky world-writable directory such as /tmp. realpath fails on the missing target, so the preflight at swift-cache.ts:115 judges the symlink's private parent. prepareScriptDestination then follows the link and creates the missing root under /tmp, and only the second walk refuses it. Nothing is compiled or run from it, but this breaks the rule this delta set out to keep: nothing is created before the cache is judged. Please apply the strict rule in the walk that resolves links and finds the missing tail. Judge the resolved target's existing ancestors before creating any level, and keep the typed refusal and hint. Add a test with a dangling symlink whose target lies under a sticky shared directory, and assert that nothing is created there.

Not blocking, and you can take or leave these:

23 of 24 checks pass; the remaining one is a review check. There are no conflicts.

…reated

A cache root that was a link to a missing directory made the preflight
judge the link's own parent, because realpath fails on a dangling link.
host-kit's walk then created the target, for example under /tmp, before
the second check refused it.

The preflight now resolves the cache root the way the kernel does,
following each link on the way down, and judges the deepest existing
directory it reaches with the strict ancestry rule before anything is
created. A test points the cache root at a missing directory under a
sticky shared directory and asserts nothing is created there.

Tests now pick the compiler's Swift inputs by their .swift suffix rather
than by argument position. The compiler-arguments cache-key test moved
into swift-cache.test.ts to keep the trust test under the line limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 73b8445 the d3ba120 finding is fixed for stable link chains. The preflight follows links down to the deepest existing directory and judges that chain with the strict rule, and the new test asserts nothing is created in the shared directory. The code review found no problems, and all 24 checks pass. A separate security cross-check of this head found one remaining gap. It comes from the same cause the review flagged as optional: the preflight is a second link walk next to host-kit's.

The strict preflight and the creation walk can resolve different paths. Suppose a cache-root link in a private directory points through a second link, owned by this user, that sits in a sticky shared directory such as /tmp, and that second link leads to a private directory with a missing cache tail. The preflight at swift-cache.ts:119 resolves the chain to the private directory and passes. If a temp-file cleaner removes the second link before host-kit walks the spelling, host-kit accepts the sticky directory and treats the removed link name as the first missing component. It creates that directory and the cache tail under /tmp, and only the final strict check refuses. Nothing is compiled or run from it, but two walks that can disagree are what let this through, and the review's note already shows them drifting: MAX_LINK_HOPS is copied, and a loop past the hop limit is reported as not_a_directory instead of a link loop.

Please use one walk. Apply the Swift cache ancestry rule to the physical parent and missing tail that host-kit's own walk computes when it authorizes creation, and refuse that plan before its first mkdir. The separate link walk and the copied MAX_LINK_HOPS can then go. Keep the test asserting nothing is created in the shared directory, and add one where the intermediate link disappears between resolution and creation.

Not blocking: the test moves and the args.filter rewrites are unrelated to the fix.

…'s one walk

The cache judged its ancestry in its own preflight walk, separate from
host-kit's creation walk. If a temp cleaner removed an intermediate link
between the two, host-kit could create directories under /tmp before the
final check refused them.

- prepareScriptDestination takes { privateAncestry: true }. With it, the
  walk refuses any directory it traverses that is group- or world-writable,
  sticky or not, or owned by anyone but root or this user. It applies this
  to every level, including the existing ancestors of a link's resolved
  target, before its first mkdir. The default for script destinations is
  unchanged.
- The Swift cache passes the option. Its separate preflight, the post-walk
  check, deepestExistingLevel and the copied MAX_LINK_HOPS are gone.
- New host-kit tests: sticky world- and group-writable ancestors, a
  group-writable ancestor, a foreign owner, a link into a sticky shared
  directory, and an intermediate link in a sticky directory that vanishes
  mid-walk. Each is refused with nothing created. The default mode still
  passes the sticky ancestor through.
- ADR 0016 notes that executable state asks the walk for private ancestry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 41f82c7 host-kit's creation walk applies the private Swift cache ancestry rule before its first mkdir. The separate preflight and the copied link-hop limit are gone, and script destinations keep their existing rule. A separate security cross-check of this head is clean. The code review found two remaining items, so this is not ready yet.

  1. The disappearing-link test doesn't prove the old race. The current test unlinks the intermediate hop right after the first link is read. On the old two-walk code at 73b8445 that happens during the preflight, which then sees the missing hop under the shared directory and refuses before creation. So the test would also pass on 73b8445. Please add a production-path case through the Swift cache that removes the intermediate link after it has been resolved and before creation reaches it. Assert that the shared directory stays empty, and show in the PR body that it fails at 73b8445 and passes at this head.
  2. No live recording run since the ancestry changes. The last live simulator record run was at 1aa0069, before the cache ancestry and host-kit walk changed. Please run an iOS simulator recording at this head (record start, a tap, record stop) with a fresh default Swift cache, then a second recording. Show both MP4s, the owner and modes of v3 and its children, and that the validator and overlay helpers keep their modification times on reuse.

All 24 checks pass, and there are no conflicts.

@thymikee

Copy link
Copy Markdown
Member Author

At b0f9412 the production Swift-cache regression is in: it fails on the old two-walk code at 73b8445 and passes at this head. The code is unchanged since 41f82c7, which a separate cross-check found clean. But the shipped recording route still needs live validation.

Once the device tooling mismatch is resolved, could you run the real daemon and iOS simulator record start/stop route twice, first with a cold private directory and then with the existing helper, with touch overlay enabled? The previous live recording was at 1aa0069, and the mocked test cannot prove dispatch, capture, Swift compilation, or MP4 finalization at this head. Please provide both playable MP4s, verify overlay output and validator completion, and record the commands, device, SHA, directory and executable ownership and modes, plus executable mtime before and after reuse. Recording route

All nine listed CI jobs are in progress; no failure output was supplied. No conflicts are known. Before merge, provide the live evidence above and wait for the required CI jobs to finish.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of b0f9412

Since the review of 41f82c7: 0 new · 0 still open · 0 no longer raised

No open findings.

Not in the reviewers' prompt (2 files)
  • packages/capture-kit/src/recording/recording-swift-sources.ts: its very long or mostly numeric lines were cut; reviewers could still search them
  • packages/command-surface/src/recording/index.ts: its very long or mostly numeric lines were cut; reviewers could still search them

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

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

💬 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.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 18, 2026
@thymikee

Copy link
Copy Markdown
Member Author

The maintainer accepted host-only evidence for this head. The live recording route was exercised at 1aa0069. Since then the changes are confined to the private Swift cache and host-kit's creation walk, and real-filesystem tests cover them, including the production regression that fails on 73b8445. The security cross-check of the code at 41f82c7 is clean, and b0f9412 adds only that test. All checks pass, and there are no conflicts. This is ready for review.

… compileSwiftSourceText

The generator embeds the three recording helpers through the packager's comment stripper,
which keeps every line in place, and the parity guard compares against the stripped files.
The video validator compiles through compileSwiftSources under a fixed file name, so the
second compile entry point and its duplicate cache-key construction are gone.

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

Copy link
Copy Markdown
Member Author

Evidence for the two size cuts at 93f6ac6 (parent b0f9412, the head before these cuts). Everything earlier on this PR stays attributed to its own SHA.

Changes

  1. scripts/generate-recording-swift-sources.ts embeds the three recording helpers through scripts/strip-swift-comments.mjs. Source admission (lstat regular file only) is unchanged. The parity guard moved to test/repo-guards/recording-swift-sources.test.ts, because a workspace package test may not import scripts/. It now compares each embedded text to the stripped canonical file and also checks that the line count matches.
  2. video.ts calls compileSwiftSources with the fixed file name video-validator.swift. compileSwiftSourceText and its second cache-key construction are removed. compileSwiftSources keeps the optional signal, so cancellation is unchanged. The validator's cache entry is keyed differently, so each user compiles it once more after upgrading.

Validation

  • pnpm check:affected --run --head 93f6ac6: all runnable checks passed, exactCommit: true, with the validation lock held. Earlier runs failed lint (a test file over 1,000 lines) and layering (a package test importing scripts/); both were fixed and the gate was rerun in full.
  • Focused: vitest --project unit-core packages/capture-kit/src/recording 218 passed; guard test 6 passed.
  • Regression, same command: with the unstripped embedded module restored, the parity guard fails 4 of 6. With the validator file name changed, video.test.ts fails 1 of 23 (shared-compile test).
  • swiftc -typecheck on macOS, canonical against stripped helpers: 0 errors, and the diagnostics are identical by file, line and column (24 for the overlay, 11 for the frame decoder).

Size (scripts/size-report.mjs, pnpm build at each commit, same machine; base is b0f9412, not main)

Metric b0f9412 93f6ac6 Diff
Bundled JS raw 4,915,152 4,909,542 −5,610
Bundled JS gzip 1,705,971 1,703,578 −2,393
Package unpacked 6,410,910 6,405,300 −5,610
Package download (tarball) 1,945,478 1,943,108 −2,370
Installed total 6,411,796 6,406,186 −5,610

Changed files: dist/src/recording-swift-sources.js 37,448 → 32,020 B, and dist/src/video.js 13,689 → 13,507 B. Files in the package stay at 831. The earlier 31,770 B of removed packaged Swift was already counted in the PR's size report against main. The 5,392 B probe was an input measure; this table is the packed result.

Gaps

  • No live daemon or simulator recording at 93f6ac6. The only native check is the host-only swiftc typecheck above. CI owns native smoke and coverage.
  • No independent reviewer ran on these two cuts. I read the full diff cold; the cuts are net −17 lines across 8 files, so no growth threshold applies.

@thymikee
thymikee merged commit f415b7d into main Nov 19, 2026
23 of 24 checks passed
@thymikee
thymikee deleted the fix/swift-cache-trust branch November 19, 2026 10:11
thymikee added a commit that referenced this pull request Nov 19, 2026
Rebased onto main, `atomic-file.ts` stands at 1014 lines against the 1000-line
lint limit (this branch 1000, #4670 +14). Trimmed this branch's own restated
prose rather than splitting the module, which would move closure counts the
eager-closure gate holds: the two staging guards are documented once, at the
staging owner, and the `claimed` parameter every call site passed as `true` is
gone with the claim step it described.

The state chain now says beside `scriptTrust({})` why it asks the script rule's
own trust: its extra strictness is the walk's policy, not #4670's
private-ancestry option, because a state record is data rather than an
executable another user could compile.
thymikee added a commit that referenced this pull request Nov 19, 2026
main's boundary line said the daemon trusts its state directory. This branch
made that false: the state route and every state write, read and open are judged
now (#4649), so the line says what the code does, and one line points at the
ADR that names what the judgment leaves out.

docs/agents sits 9 bytes under its 40000 budget after #4670, so the state
pointer is that one line and the cleanup boundary lives in ADR 0016's "Where the
guarantee does not reach".
thymikee added a commit that referenced this pull request Nov 19, 2026
Rebased onto main, `atomic-file.ts` stands at 1014 lines against the 1000-line
lint limit (this branch 1000, #4670 +14). Trimmed this branch's own restated
prose rather than splitting the module, which would move closure counts the
eager-closure gate holds: the two staging guards are documented once, at the
staging owner, and the `claimed` parameter every call site passed as `true` is
gone with the claim step it described.

The state chain now says beside `scriptTrust({})` why it asks the script rule's
own trust: its extra strictness is the walk's policy, not #4670's
private-ancestry option, because a state record is data rather than an
executable another user could compile.
thymikee added a commit that referenced this pull request Nov 19, 2026
main's boundary line said the daemon trusts its state directory. This branch
made that false: the state route and every state write, read and open are judged
now (#4649), so the line says what the code does, and one line points at the
ADR that names what the judgment leaves out.

docs/agents sits 9 bytes under its 40000 budget after #4670, so the state
pointer is that one line and the cleanup boundary lives in ADR 0016's "Where the
guarantee does not reach".
thymikee added a commit that referenced this pull request Nov 19, 2026
Rebased onto main, `atomic-file.ts` stands at 1014 lines against the 1000-line
lint limit (this branch 1000, #4670 +14). Trimmed this branch's own restated
prose rather than splitting the module, which would move closure counts the
eager-closure gate holds: the two staging guards are documented once, at the
staging owner, and the `claimed` parameter every call site passed as `true` is
gone with the claim step it described.

The state chain now says beside `scriptTrust({})` why it asks the script rule's
own trust: its extra strictness is the walk's policy, not #4670's
private-ancestry option, because a state record is data rather than an
executable another user could compile.
thymikee added a commit that referenced this pull request Nov 19, 2026
main's boundary line said the daemon trusts its state directory. This branch
made that false: the state route and every state write, read and open are judged
now (#4649), so the line says what the code does, and one line points at the
ADR that names what the judgment leaves out.

docs/agents sits 9 bytes under its 40000 budget after #4670, so the state
pointer is that one line and the cleanup boundary lives in ADR 0016's "Where the
guarantee does not reach".
thymikee added a commit that referenced this pull request Nov 19, 2026
Rebased onto main, `atomic-file.ts` stands at 1014 lines against the 1000-line
lint limit (this branch 1000, #4670 +14). Trimmed this branch's own restated
prose rather than splitting the module, which would move closure counts the
eager-closure gate holds: the two staging guards are documented once, at the
staging owner, and the `claimed` parameter every call site passed as `true` is
gone with the claim step it described.

The state chain now says beside `scriptTrust({})` why it asks the script rule's
own trust: its extra strictness is the walk's policy, not #4670's
private-ancestry option, because a state record is data rather than an
executable another user could compile.
thymikee added a commit that referenced this pull request Nov 19, 2026
main's boundary line said the daemon trusts its state directory. This branch
made that false: the state route and every state write, read and open are judged
now (#4649), so the line says what the code does, and one line points at the
ADR that names what the judgment leaves out.

docs/agents sits 9 bytes under its 40000 budget after #4670, so the state
pointer is that one line and the cleanup boundary lives in ADR 0016's "Where the
guarantee does not reach".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant