Skip to content

refactor(server): one completion decision for both cohort owners - #471

Merged
tannerpolley merged 2 commits into
mainfrom
issue-435-418-step-3-unify-managed-completion-and
Nov 18, 2026
Merged

tannerpolley merged 2 commits into
mainfrom
issue-435-418-step-3-unify-managed-completion-and

Conversation

@tannerpolley

@tannerpolley tannerpolley commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Problem

Step 3 of #418 asks for one completion path for both cohort owners: ordinary delegation keeps its cohort on the parent run, and run-less managed admissions keep it on the parent thread. Three pieces still sat in the wrong place or were duplicated:

  • issue closeout policy lived in the orchestration completion helper;
  • the "is this result still owed work" check behind task_status, wait and the parent wake was split between Orchestrator.ts and the fork helper;
  • finalize built its wake events inline instead of using the shared delegatedCompletionPlanEvents.

Writing tests for both owners also found a real defect. A late, unobserved result read of a thread-owned task (observedByRunId: null, for example a wait that ends after its turn did) acknowledged the result and dropped it from the queued wake, so the parent never saw it. Upstream's rule from PR pingdotgg#32573 (36d0dcbf55, wake idle parents for late delegated results) says an unseen read must not acknowledge a result; the fork's 2026-11-11 sync merge (436c0d0f32) had exempted run-less tasks from it.

Fix

finalize / recovery ──▶ planDelegatedCompletionDelivery (one planner, completionCohort picks run or thread)
                   ├──▶ delegatedCompletionPlanEvents (shared; inline copy removed)
                   └──▶ issueCloseout.publish / publishUntagged   (issues/IssueCommentEffects.ts)
task_status · wait · parent wake ──▶ ForkDelegatedCompletion.taskResultPending (single owner)
late unobserved read ──▶ upstream rule for both owners (claimed stays claimed; pending is offered)

Closes #435. Part of #418 (step 3 of 4).

Rollout (owner decision)

The late-read fix changes result hand-back, so before the first rollout that includes it:

  1. Pause new admissions (set_repository_work_paused).
  2. Drain: let active workers finish and their results reach their parents.
  3. Install the build.
  4. Resume admissions.

In-flight restart recovery is still covered by the fixture tests below; the pause and drain are the owner's choice, not a substitute for them.

Evidence

Case ledger. All new tests are in DelegatedCompletionDelivery.fork.test.ts and run each case for both owners (run and thread). They extend the existing thread-owned restart test rather than adding a parallel file.

Case Test
Concurrent siblings restart recovery adds a second worker to a queued {run,thread}-owned delivery and settles both: a second worker joins the queued wake without a new message ID or generation
Restart at pending / claimed / delivered Same test: the second worker is finished-but-unpublished at restart; the first is reserved with no dispatched wake (one offer, same ID and generation); the delivery run ended before the listener attached; a further restart offers nothing and leaves exactly one delegated message
Explicit result read a {observed,late} result read of a {run,thread}-owned task settles its queued wake only when seen. An observed read acknowledges and cancels the wake. A late read leaves the result claimed in the queued wake.
Stopped, archived or deleted parent a {run,thread}-owned worker that finishes after its parent was {archived,deleted,stopped}. Archived and deleted give disposed with no offer. A stopped run cohort gives disposed.
Old-format fixture The same test's thread-owned stopped case is the pre-#418 whole-mailbox stop. It reopens, and the result is reserved and offered with one message ID and generation. All fixtures are committed events, recovered by recoverDelegatedTasks.
Failed workspace preparation a {run,thread}-owned worker whose workspace preparation fails wakes its parent with the failure: real defer_start then prepared-run.fail; waits on the persisted subagent.updated event
Untagged descendant wake Owner-independent child-side logic, already covered: DelegatedCompletionDelivery.test.ts "holds a follow-up open while the child's own subagent still runs"
Background task (#350) Already covered: "finalizes a child whose run ended before a background command / nested subagent settled", "... keeps while an unawaited background command runs"

No sleeps: tests use committed events, recoverDelegatedTasks, and persisted-event streams.

Failing first. Before the fix, a late result read of a thread-owned task … failed with expected 'acknowledged' to equal 'claimed'. The run-owned variant passed. After the fix, all 35 tests in the file pass.

One changed expectation. ThreadLaunchService.repositoryHandoff.fork.test.ts sent an unobserved acknowledgement to an idle root, which has no run, and expected the result to leave the root's wake. That encoded the defect. It now expects the result to stay in the wake.

Runs (agent-heavy, Node from vite-plus fallback-bin):

Footprint: whole-file Orchestrator.ts numstat. These are fork totals, not issue-only counts.

Upstream base Before (main) +/− After +/−
7d32075e37 (plan pin) 1690 / 775 1671 / 771
b387e3a179 (newest sync, #469) 1497 / 687 1478 / 683

ForkDelegatedCompletion.ts: +16 / −90. The other five shared files are unchanged.

What remains, and which step owns it:

Merge Danger

Door: two-way. No migration and no persisted-format change. Rolling back restores the old late-read behavior.

Blast radius: delegated result hand-back for run-less managed workers. A late, unobserved read no longer acknowledges a result, so the parent sees it through its wake instead. Live userdata was not read or copied, and no dev server was started.

Implemented with Claude Opus 5.5 through the Claude Code harness in T3 Code.

🤖 Generated with Claude Code

tannerpolley and others added 2 commits November 17, 2026 18:22
Issue closeout moves from ForkDelegatedCompletion into IssueCommentEffects.
The "result still owed work" decision behind task_status, wait and the parent
wake now has one owner (ForkDelegatedCompletion.taskResultPending). Finalize
reuses delegatedCompletionPlanEvents instead of an inline copy.

Fix: a late, unobserved result read of a thread-owned (run-less managed) task
acknowledged it and dropped it from the queued wake. It now follows upstream's
late-read rule for both owners.

Narrows reads: run-less admission reads only the parent thread row,
reuseManagedChild reads only the current owner's task, and the
closed-child pending check reads only this task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

#418 step 3: Unify managed completion and restart recovery with upstream delegation

1 participant