Repository navigation
fix(apple-runner): keep runner trees another session may read through a cache rebuild - #4916
Conversation
Size Report
Startup median (7 runs, lower is better):
|
Live check: a rebuild while another session is mid-launchSource
iPhone 17 Pro was claimed by a live session from another worktree, so the iPad stood in for it.
Directory listing after the run: On base, B's clean would have deleted Disk cost. A replaced tree stays until a clean or trim runs after its one-day window, unless a live lease still names it. One trimmed Cleanup: both sessions closed, both daemons stopped, |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on 54bd9f3. The verdict and the full list are in the summary comment.
|
Correction to the live-check comment's disk-cost note. Since 1c75467, a replaced tree with no live lease stays for at most 3 minutes after its last resolution ( |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on 1c75467. The verdict and the full list are in the summary comment.
|
The code at 1c75467 is ready to merge. Not blocking, and fine to take or leave: the launch-grace doc in runner-cache-trim.ts says the lease is written once the runner answers, but the code writes it right after spawn; the lease match in runner-cache-trim.ts uses the directory of the session xctestrun, which with The earlier thread on unbounded retention of rebuilt trees is fixed at this commit, since the window is now 180 s plus the lease: #4916 (comment). It can be resolved. CI is still running. Smoke Tests (ios) and (macos) cover the changed route (runner build, clean, reuse, launch) and must finish green. Coverage and Prose fail on Not verified here: the apple-runner suite and mutation checks were not run, the live run was at cdd9133 and not at this commit (the later change only narrows retention), and it is unconfirmed whether |
|
Thanks. Taking the three notes in order:
The earlier retention thread is resolved. Local checks at 75fea74: the apple-runner suite passes. One readiness-preflight case is flaky on this host (load average about 100); it also failed 1 in 3 runs on 1c75467. Lint, typecheck and |
75fea74 to
ab6e209
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on ab6e209. The verdict and the full list are in the summary comment.
This comment has been minimized.
This comment has been minimized.
|
The earlier review at 1c75467 found no problems, and the root-level xctestrun handling is now fixed at e5b09e6: runner-cache-trim.ts:86-90 implements it with a test, so #4916 (comment) can be resolved. One defect remains. When the final managed cache key is a directory symlink, a forced clean reaches the trim call at https://github.com/callstack/agent-device/blob/e5b09e6/packages/apple-runner/src/runner-cache.ts#L257, because assertSafeDerivedCleanup returns immediately with no override (lines 326-332). trimDirectory then opens that path with readdir and removes unkept entries through the symlink (runner-cache-trim.ts:145-156). The base calls rmSync on the key path itself, which unlinks the symlink and leaves the target intact. A rebuild can now delete files in the directory the key points to, including files outside the cache. The rule: the final managed key must be checked with lstat before it is walked, and a symlink at that entry must be unlinked so the rebuild creates a real key directory, as the base does. Please add a test with a symlinked key that holds a file outside the cache. Could runner-cache and the existing runner-lease owner hold a reservation from resolution under the cache lock until lease publication or cancellation, so the filesystem trim consumes the protected paths without depending on a transport readiness timeout that starts after spawn? Not verified here: tests were not run, and the runner cache clean and trim layouts were compared against main by reading only. Body rules shows as cancelled and one other check is still running. Neither exercises runner code and no failure output exists. Nothing else is known to block merge, so after the symlink fix the remaining step is for both checks to finish. |
e5b09e6 to
5eb1381
Compare
|
Thanks. The symlinked-key defect is fixed in 5eb1381, which is rebased on d6767e0. Symlinked key. Reservation from resolution to lease publication. I looked at this, and it would not be smaller, so I kept the window. A reservation needs:
That is a second lease kind with its own lifecycle. The window reuses the tree's mtime and adds no state. The window also does not depend on the readiness timeout. Local checks at 5eb1381: apple-runner 991/991, |
This comment has been minimized.
This comment has been minimized.
|
Two of the earlier findings are fixed at 5eb1381, but one defect remains in the cache eviction path. CI is green, with 23 checks and none failing, and no conflicts are known. Tests were not run here, and the new test is judged only by reading the code. Only the last commit was reviewed line by line, since the rest is unchanged by the rebase. A session can resolve a tree in key A before it writes its lease. Another session then indicts A, and the cleanup keeps the stamped tree but removes its cache metadata (runner-cache-retention.ts#L106). If a different key then builds successfully in the same platform folder, key eviction runs (runner-artifact.ts#L526-L536). With Could runner-cache-retention.ts own the resolved-tree stamp and the pre-lease retention rule for both whole-key eviction and directory cleanup, with runner-cache-trim.ts applying the resulting keep set? The key-retention code already owns the age and lease decision, and the transport readiness timeout measures a later phase. The three open inline threads on duplicate products trees, the stamp on a missing products directory, and the root-level Once the eviction path is fixed and CI is green again, the PR is ready for maintainer review. |
5eb1381 to
d2ef8f1
Compare
|
Fixed in d2ef8f1. Whole-key eviction now skips a key that holds a live tree: runner-cache-retention.ts#L116. The check runs under the key lock, after the lease check. It uses the rule the clean and the trim use, Regression: retention test L374 runs with On ownership: I kept the rule in |
This comment has been minimized.
This comment has been minimized.
|
The three findings from the earlier review at 5eb1381 (#4916 (comment)) are resolved, and the new early-return guard on the eviction path looks correct at d2ef8f1. Two problems remain, one in code and one in the stated guarantee. A managed key that is a symlink to a directory with a matching manifest still loses its reader. Session A resolves and stamps its .xctestrun through the symlink, then releases the key lock. Before A publishes its lease, session B forces a rebuild under the same key lock. The forced clean unlinks the symlink at https://github.com/callstack/agent-device/blob/d2ef8f1/packages/apple-runner/src/runner-cache.ts#L252 without checking liveness, and B builds under a different Products directory. A then fails when it reads the old path in prepareXctestrunWithEnv. The rule should be: no code path may return a launch path through a managed key symlink. Please normalize a symlink at the managed key entry under the key lock before reuse or build can return a path through it. Unlink the entry and build in a real key directory, leaving the target untouched. The guarantee that every automatic cleanup of a managed key keeps the path a session just resolved does not hold for forced clean. Eviction takes the key lock and checks lease and tree first (runner-cache-retention.ts:100), but forced clean bypasses that check at runner-cache.ts:251-253. The new symlink test at runner-cache-live-readers.test.ts:188-204 removes the tree before installing the link, so no reader of a certified tree through the link is exercised. Please make the guarantee true for this route, or narrow the claim, and add a test where the reader holds a certified tree through the link when the forced rebuild runs. Could runner-cache.ts own both publishing and checking the pre-lease tree protection, with clean, trim and eviction consuming that policy and runner-lease.ts supplying only published readers? Today the policy sits in the transport module, which exists for a post-spawn readiness timeout, while it protects work before spawn. Not verified here: the tests were not run, and the conclusions come from reading the source. The live two-daemon run in the PR body predates this change. No new live run is needed for the eviction guard alone, but the symlink route above needs a test that reaches the forced rebuild while a reader holds the link. The only check that did not pass is Body rules, which was cancelled. It lints the PR body and does not touch apple-runner code, and the other 22 checks pass. Please re-run it. No conflicts are known. After the symlink fix and a passing Body rules run, the PR is ready for maintainer review. |
d2ef8f1 to
e47a2f2
Compare
|
Fixed in e47a2f2, which is rebased on d1ace81. Symlinked key. Under the key lock, before reuse or build returns a path, a managed key that is a symlink is unlinked and its target left alone (runner-artifact.ts#L599). The key is then built as a real directory. The forced clean uses the same helper, Guarantee. The PR promises that automatic cleanup (rebuild after an indictment, post-build trim, key eviction) never removes a tree a live session resolved. Ownership. Unchanged. The previous round asked for retention.ts and this one for runner-cache.ts. Both moves bring back the cycle Fallow rejected. Today there is one rule ( Local checks: apple-runner 993/993 (readiness-preflight flakes under host load and passed on rerun), |
This comment has been minimized.
This comment has been minimized.
|
The earlier findings from d2ef8f1 are fixed at e47a2f2, and one defect remains in the new live-reader check. Tests were not run here, and the two-daemon live run in the PR body predates both fixes. A session can still lose its runner tree during a rebuild. runner-cache-trim.ts:74 treats a tree as live when its mtime is under 180,000 ms old or a published lease points at it. Session A resolves a managed artifact and releases the key lock before it prepares its per-session plist (runner-artifact.ts:513-530, runner-session.ts:288-320). A publishes its lease only after spawn (runner-session.ts:328-388). The plutil conversion has no timeout (runner-artifact-env.ts:145-162), and the start budget is ten minutes (runner-start-budget.ts:35). If the conversion stalls for three minutes, the tree stops changing. Session B on a second device with the same key can then start the forced rebuild. Could runner-lease.ts own this pending reference as part of the existing launch lease lifecycle, with runner-cache-trim.ts reading it next to published leases? The directory mtime only approximates a live reader, and its window does not bound plist preparation. The only check that does not pass is Body rules, which was cancelled. It lints the PR body and does not exercise packages/apple-runner or the docs, so it looks unrelated. The other 23 checks pass. No review threads are open. Before merge, the cancelled Body rules check needs a re-run. Not verified here: a live run that stalls the plist conversion while a second session rebuilds. |
… a cache rebuild Each runner build lands in a fresh products directory, and a clean or the post-build trim keeps every tree resolved within the reader grace window or named by a live runner lease. An indicted or forced rebuild no longer deletes files a concurrent session is launching from (#4886). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Breaks the runner-cache -> runner-cache-trim import cycle: the trim module owns the live-tree rule, and runner-cache-retention reads the window from it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A live runner lease already keeps a tree a running runner uses, so the resolution stamp only has to cover the launch before the lease exists. Replaced trees now outlive their last resolution by four runner startup timeouts instead of a day, so a rebuild loop no longer piles up copies. Key eviction keeps its own one-day rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tamp A tree that vanished between certification and the resolution stamp now takes the no-usable-cache path (typed tree_missing decision) instead of failing the start with ENOENT. A tree the user cannot stamp, such as one another user owns, is still reused: the session's own .xctestrun written into it moves its mtime anyway. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A build's products directory sits under Build/, so an .xctestrun at the cache root (a legacy or foreign layout) is no tree: a clean removes it as main did, and it no longer turns the post-build trim into a no-op. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…king it A forced clean of a managed key that is a directory symlink walked and removed the files it points at. Like main's rmSync, the clean now unlinks the symlink, so the rebuild creates a real key directory and nothing outside the cache is touched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…still read An indictment removes a key's metadata but keeps a tree a launch resolved and has not leased yet. Whole-key eviction then saw the key as idle since epoch and deleted it inside the launch window. Eviction now applies the same live-tree rule as the clean and the trim. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he key Under the key lock, before reuse or build returns a path, a managed key that is a symlink is unlinked (its target untouched) and the key is built as a real directory. The forced clean shares the same unlink. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…over The mtime window did not bound plist preparation. A start now records a pending hold on the exact tree it resolves, under the cache key lock and inside its runner lease lock; the hold is released when that lock exits, after the runner lease is published or the start failed, and lapses with a dead owner. Every deletion path reads live trees from published leases plus pending holds. The resolution stamp and the time window are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
e47a2f2 to
3a4a216
Compare
|
Fixed in 3a4a216, rebased on a4f2dca. The mtime window and the resolution stamp are gone.
Tests:
Docs: configuration.md#L153. |
Apex Sentinel · ✅ Approve · review of
|
| Where | Finding |
|---|---|
packages/apple-runner/src/runner-cache-trim.ts:51 |
🆕 A lease's cacheKey is ignored by listLiveRunnerTrees, so a rebuild deletes a running runner's tree |
1 finding below minor hidden (user_settings.min_severity).
Reviewed with this repository's own Sentinel configuration (a4f2dcad0eae).
💬 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.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on 3a4a216. The verdict and the full list are in the summary comment.
|
The earlier finding from the review of e47a2f2 is fixed at 3a4a216, and no new problems were found. The runner tree a second session may read is now kept through a cache rebuild, because the hold is taken on the managed tree before plist preparation. CI is green, with all 23 checks passing, and no conflicts are known. Nothing else must happen before merge. The resolved thread on the env-dir .xctestrun (#4916 (comment)) does not apply: the file keeps product paths relative to the env dir, and the launch hold already names the managed tree. Not verified here: tests were not run, and the regression test was judged by reading runner-cache-live-readers.test.ts. Not verified here: the |
Summary
Invariant: while a launch holds a managed runner tree, no clean, rebuild, trim or eviction removes it. Fixes #4886.
Build/Products-<id>directory; the cache metadata points at the current tree.Before / after
Two sessions on one keyed
cache-<key>(runner-cache-live-readers.test.ts):Validation
At
3a4a2169: apple-runner passes (three load flakes passed on rerun); lint, typecheck,check:command-docs,fallow auditpass. With the hold made a no-op, four tests fail.🤖 Generated with Claude Code