Skip to content

fix(ui-server): never retain a worktree checkpoint refused before its pool lease (#3528) - #4060

Merged
schlessera merged 6 commits into
mainfrom
fix/3528-worktree-checkpoint-abort
Nov 16, 2026
Merged

schlessera merged 6 commits into
mainfrom
fix/3528-worktree-checkpoint-abort

Conversation

@schlessera

@schlessera schlessera commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #3528

What changed

A managed worktree's journal observer no longer keeps a checkpoint that the native lease owner refused before it recorded a pool lease. The observer adds an identity to its retained set before it asks the manager, and releases it if the call is refused. For a worktree, the lease owner could refuse before any lease existed: on an abort while the worktree ledger recorded the identity, or while the owner settled the worktree's interrupted release (settleRelease). The cleanup release was then refused with "no recorded app lease", the identity stayed retained, and close() threw "native checkpoint(s) remain retained after shutdown". Now the lease owner remembers each identity it refused before recording any lease (unleased, with its worktree) and acknowledges one release of it with a deleted receipt that says no root was created. It does not ask the pool to delete anything. Any other identity without a lease is still refused. The worktree journal manager also handles a ledger record that rejects: that identity never reaches the pool, and its release only forgets whatever part of the record may have committed. A checkpoint whose lease was recorded keeps its retryable cleanup as before. The worktree assessment checkpoint in reference-worktrees.ts follows the same record-then-checkpoint shape, so an abort there before the lease is now cleaned up through the same acknowledgment.

Why this shape

#3521 closed the brain's window with a synchronous signal check, because the brain's lease owner records its intent with no earlier wait. A worktree checkpoint has two waits before its lease, and one of them (settleRelease) must come before the intent, because poolCheckpointIntent requires a held worktree. A synchronous check cannot close that window. Only the lease owner knows whether it recorded a lease, so the handoff belongs there. Two alternatives were rejected. Treating every "no recorded lease" refusal as released would let an arbitrary identity pass as cleaned up. Marking the refusal's error object would leak between callers, because one abort reason is shared by many calls. The acknowledgment is per identity, recorded only when the refusal comes before any lease row exists, cleared when a new poolCheckpoint of the same identity starts, and spent by one release.

Proportionate containment (checklist): this narrows how the "no recorded app lease" refusal and the observer's "remain retained after shutdown" refusal apply, and weakens neither.

  • Trigger and coverage: the acknowledgment applies only to an identity this owner generation refused in poolCheckpoint before poolCheckpointIntent ran, while no lease row exists. It does not cover a failure of the intent write itself, or a crash between the ledger record and the lease (see below).
  • Restriction: for anything else, poolRelease still refuses an identity with no recorded lease. The observer still retains, and refuses shutdown over, any identity whose release fails.
  • Restricted mode / clearing: the acknowledgment clears itself on first use. A leased checkpoint whose release fails stays retained, and retries clear it automatically. No human action is needed.
  • Cause recorded: unchanged. A retained identity is still reported through releaseFailed and the shutdown error.
  • Scope test: a worktree checkpoint aborted after its lease is recorded keeps retryable cleanup ownership (#3528) shows that a leased checkpoint's failed release stays owned and retries cleanly. The ledger-wait test shows that the acknowledgment is spent once and that an unrelated identity is still refused.

Proof

bun run check:pr --base origin/main   # exit 0 on merge head 78744afeb (origin/main merged in after #4050), receipt below
bun run test packages/ui-server/tests/native-journal-observer.test.ts packages/ui-server/tests/reference-worktree-journal.test.ts packages/ui-server/tests/native-brain-leases.test.ts packages/ui-server/tests/reference-writes-offloop.test.ts packages/ui-server/tests/reference-worktrees.test.ts
# 85 pass, 0 fail after merging origin/main
NODE_OPTIONS=--max-old-space-size=8192 bun run typecheck   # exit 0
bun run build   # exit 0

The new tests in packages/ui-server/tests/native-brain-leases.test.ts drive the real lease owner, the real referenceWorktreeJournalManager and the real observer over a raw pool double that records every request.

Failing-first. I reverted only packages/ui-server/src to origin/main and kept the new tests:

  • a worktree observation aborted while its checkpoint is being recorded closes with nothing retained failed: 1 native checkpoint(s) remain retained after shutdown: <id>. NativeBrainManagerError: This reference pool identity has no recorded app lease; arbitrary pool roots cannot be deleted. This is the ledger wait.
  • a worktree observation aborted while the owner settles the worktree's release closes with nothing retained failed with the same error. This is the release-settlement wait.
  • a worktree checkpoint ledger record that fails asks the pool nothing and leaves nothing retained failed with the same error.
  • a worktree checkpoint aborted after its lease is recorded keeps retryable cleanup ownership passed on the base too, as intended. It guards behaviour that must not change.

With the fix restored, all 18 tests in the file pass. After merging origin/main, which brought in #4050's turn-tree tests in the same file and kept both sets, all 21 pass.

Mutations (each applied alone and then restored; results are for native-brain-leases.test.ts):

Mutation Fails
Owner never records the pre-lease refusal (unleased.set removed) ledger-wait and settle-wait tests (remain retained after shutdown)
poolRelease never acknowledges ledger-wait and settle-wait tests
poolRelease acknowledges any identity without a lease REFERENCE_POOL_UNRECORDED_NOT_DELETED (existing test) and WORKTREE_REFUSED_CHECKPOINT_ACKNOWLEDGED_ONCE
Acknowledgment not spent (repeatable) WORKTREE_REFUSED_CHECKPOINT_ACKNOWLEDGED_ONCE
Worktree manager does not track a failed ledger record (unsent.add removed) ledger-failure test
Observer drops every refused identity without releasing all four, including WORKTREE_LEASED_CHECKPOINT_STAYS_RETAINED (post-lease ownership)

Collisions (bun scripts/collisions.ts --json --pr 4060, head 16371eec2): textual with #4050 in native-brain-leases.test.ts (both append tests). #4050 has since merged, and its conflict is resolved in the merge commit with both sets of tests kept, closure-only with 24 open PRs, nothing else. Details are in the comment below.

{"version":1,"kind":"brain-check-pr-receipt","head":"78744afeb71a1f4956d13c8841857926cb9745be","base":"origin/main","mode":"focused","startedAt":"2026-11-16T10:36:22.205Z","finishedAt":"2026-11-16T10:37:35.335Z","checks":[{"name":"all lint and leakage gates","categories":["changeset"],"outcome":"passed","elapsedMs":14035.977394999998},{"name":"environment documentation","categories":["changeset"],"outcome":"passed","elapsedMs":111.26734700000088},{"name":"pending changeset package names","categories":["changeset"],"outcome":"passed","elapsedMs":96.65781699999934},{"name":"contribution changeset","categories":["changeset"],"outcome":"passed","elapsedMs":21.687089999999444},{"name":"affected fast invariants","categories":["verify","unit"],"outcome":"passed","elapsedMs":58846.71882200001}]}

Checklist

  • Focused local preflight/behavioral receipts passed; hosted proof runs on confirmation.
  • Acceptance criteria from the issue are all met.
  • The PR and ui-server: worktree journal observer can retain a checkpoint aborted before its pool lease is recorded #3528 carry milestone 0.45.0.
  • A changeset is included (.changeset/3528-worktree-checkpoint-abort.md, ui-server patch).
  • No personal data anywhere in the diff.
  • Nothing in the machine surface moved.
  • Docs: three code citations my line moves shifted are repaired (governed-writable-workers.md, chat-history-deletion.md).
  • The changed guard answers the proportionate-containment checklist above.
  • Nothing on a turn's provider-history path changed.

Anything left open

The process can crash after a worktree ledger entry is recorded and before the pool lease intent. Startup recovery then still asks poolRelease for that identity, is refused, and keeps the entry with a warning on every boot until the worktree's removal forgets it. That was already true before this change and is out of scope here. Follow-up: #4059.

🤖 Generated with Claude Code

… pool lease (#3528)

The journal observer retains a checkpoint identity before it asks the
manager, and releases it if the call is refused. For a managed worktree,
the native lease owner could refuse before recording any pool lease: on an
abort while the worktree ledger recorded the identity, or while the owner
settled the worktree's interrupted release. The cleanup release was then
refused too ("no recorded app lease"), the identity stayed retained, and
close() threw "native checkpoint(s) remain retained after shutdown".

The lease owner now remembers an identity it refused before any lease
existed and acknowledges one release of it without asking the pool to
delete anything. Every other identity without a lease is still refused.
The worktree journal manager forgets the ledger record of an identity
whose record failed, without reaching the pool. A checkpoint whose lease
was recorded keeps its retryable cleanup as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schlessera schlessera added this to the 0.45.0 milestone Nov 15, 2026
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. labels Nov 15, 2026
@schlessera

Copy link
Copy Markdown
Owner Author

Collisions for head 16371eec2d21b1df47e1437f672739c45a9a088a (bun scripts/collisions.ts --json --pr 4060):

…ckpoint-abort

# Conflicts:
#	packages/ui-server/tests/native-brain-leases.test.ts
@schlessera
schlessera marked this pull request as ready for review November 16, 2026 15:30
@schlessera
schlessera marked this pull request as draft November 16, 2026 17:25
@schlessera
schlessera marked this pull request as ready for review November 16, 2026 17:25
@schlessera
schlessera merged commit 85497b3 into main Nov 16, 2026
40 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: ui-server Hono app factory: turn coordinator, auth, session catalog, routes, cron. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui-server: worktree journal observer can retain a checkpoint aborted before its pool lease is recorded

1 participant