Skip to content

Hold Claude Code's lock when engine edits a harness's trust file - #316

Merged
dinoderek merged 7 commits into
mainfrom
fix/trust-lock
Nov 23, 2026
Merged

dinoderek merged 7 commits into
mainfrom
fix/trust-lock

Conversation

@dinoderek

@dinoderek dinoderek commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Objective

Spawned Claude Code sessions no longer lose their workspace trust entry to a concurrent write, so they stop stalling at the trust dialog. Closes #298.

  • Cause: Claude Code saves ~/.claude.json under the proper-lockfile directory lock ~/.claude.json.lock and applies each save to a fresh read. Engine wrote the file without that lock. A session saving between its own read and write put back a copy without engine's new entry. On a Mac with live sessions, the lock was seen dozens of times in a few seconds.
  • Lock: session.EditFile, used only for harness trust edits, now holds <file>.lock. It waits up to 30s for a live holder and takes over a lock left stale for 10s. This also orders engine's own concurrent spawns, including Codex's joined config.toml. The stamp check stays, for writers that skip the lock.
  • Clone key: Claude's Trust also records the worktree's clone. For any worktree, Claude Code checks projects[<canonical git root>] first, so a later worktree key lost to a write leaves the session trusted. This holds even if a future Claude Code changes its locking.
  • Codex: embedded sessions already use a per-session profile file, so they have no shared-file race.

Gates

Lane Result Evidence
./dev test (every required lane) PASS on CI at 995dc00 https://github.com/dinoderek/engine/actions/runs/45142214872
  • Local ./dev test on 3af2d0d, which differs from head only in comments and one test's fake clock:
    • Passed: format, vet, lint, file-length, imports, versions, docs-check and harness-names.
    • Failed: unit and coverage. The only failures were 10-minute package timeouts (cli, session, hostops) and two timing tests (TestDaemonInstanceIdentifiesOnlyItsOwnProcess, TestDashboardScripts).
    • Load: the Mac ran at a load average of 35–65 from other sessions in all three full runs.
    • Each failing package alone: every one passes with go test -race (cli, session, hostops, control, controlrun, harness/..., ui).
  • On head: lint, format, docs-check and file-length pass, and the lock and Claude trust tests pass with -race -count=3.
  • The operator agreed that CI's ./dev test is the gate for this PR. Merge only on green CI.
  • New or changed tests:
    • internal/session/filelock_test.go:
      • a lock holder that saves its own read is waited for, and engine's edit lands on top;
      • a stale lock is taken over on the first look;
      • a live lock held past 30s fails with ErrLockHeld and leaves the file alone;
      • the lock is released whatever the edit does;
      • a no-op edit in a missing folder needs no lock;
      • 8 concurrent edits all survive.
    • internal/harness/claude/trust_test.go:
      • the launcher trusts the worktree, its clone and the plugin directory;
      • 8 concurrent TrustFolder calls keep every folder.
  • After review: merged main (a conflicting row in docs/harnesses.md, both kept) and trimmed that doc to its 580-word budget. No code changed since round 4.
  • Not run, with reason: none skipped. The unit and coverage lanes did not complete green locally because of host load (above).

Rulings

  • Ruling (risk: low): trusting the clone also trusts every other worktree of that clone for Claude Code. That is how Claude Code keys a worktree's project anyway, and the worktrees hold the clone's code.
  • Ruling (risk: low): a lock nobody refreshes for 10s counts as stale. That matches proper-lockfile's default, which Claude Code uses. If two writers take over the same stale lock at once, both can end up holding it, as with proper-lockfile. That needs a dead holder's lock.
  • Ruling (risk: low): no automatic recovery of a window already at the dialog. The dialog's default answer is "No, exit", so engine never types into it. Restarting the window is a larger change. A stuck session is still reported: the claim notice after 5 minutes, then the dialog stall notice after 10.
  • Ruling (risk: low): the issue's suggested engine start --check probe with two concurrent spawns is not built. Concurrency is proven in unit tests, deterministically, rather than with two real model sessions per check.

Review

  • Fresh reviewers, Claude Code with Claude Opus 5.5:
    • Round 1 (ef200c6+b9077dc): APPROVED. Verified both Claude Code 2.1.330 facts in the binary.
    • Round 2 (907be4a): CHANGES REQUESTED. The stale-takeover comment overclaimed what the stamp check catches; reworded in 3af2d0d.
    • Round 3 (3af2d0d): CHANGES REQUESTED.
      • The clone-trust and lock comments overclaimed (a later worktree key can still be dropped; Claude's exit-time and contended saves may skip the lock); reworded.
      • The stale-lock test's fake clock didn't exercise the first-look takeover; fixed.
    • Round 4 (head): APPROVED.
  • Declined findings:
    • Round 1, optional: reword or re-check the mtime before taking over a stale lock. The comment was reworded; a re-check before removing still leaves a window.
    • Round 2, optional: lock the symlink target's .lock. Declined: Claude Code passes an explicit lockfilePath built from the unresolved config path, which proper-lockfile uses as given. Engine's <path>.lock taken before resolving is the same lock. EditFile's doc now says so.
    • Round 4, optional, all declined:
      • Add Claude's clone to hosts-and-sessions.md step 2: the harnesses.md row owns per-harness trust, and step 2 stays accurate.
      • edit runs twice when the folder is missing: both callers' edits are pure.
      • The outer error names only the worktree; the check writes one key twice: both cosmetic.
      • Codex doesn't take config.toml.lock: as stated above, the lock only orders engine's writers there.
  • Review hard:
    • internal/session/filelock.go: stale takeover and wait bounds.
    • internal/session/editfile.go: lock scope and the missing-folder shortcut.
    • internal/harness/claude/claude.go: Trust now records the clone.

Deviations

  • The gate was not green locally before the PR. The operator agreed that CI's ./dev test is the gate, given the host's load.

🤖 Generated with Claude Code

dinoderek and others added 7 commits November 22, 2026 20:58
Claude Code saves ~/.claude.json under the proper-lockfile lock
~/.claude.json.lock, applying each change to a fresh read. Engine wrote
the file without that lock, so a session saving between its own read and
write put back a copy without engine's new trust entry, and the spawn
stopped at the workspace trust dialog (#298).

session.EditFile now holds <file>.lock for every trust edit, which also
serializes engine's own concurrent spawns (Codex's joined config.toml
too). Claude's trust also records the worktree's clone, the canonical
git root whose key Claude Code checks first, so once a clone is trusted
a later spawn has nothing a concurrent write could lose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Releasing a session whose Codex home was never made used to be a no-op;
taking the lock there failed it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dinoderek
dinoderek merged commit fa471d7 into main Nov 23, 2026
1 check passed
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.

Spawned Claude sessions stop at the workspace trust dialog (trust entry lost under concurrent writes)

1 participant