Repository navigation
fix(session): keep a prefetch parent that still owes sub-agent work (#10987) - #14464
Conversation
Design Review (Opus 5.5) — 🟡 CONCERNSDesign-level review of Design-Verdict: CONCERNS A kept parent now returns Watch
[DESIGN-REVIEWED] f2096e9 Description read[DESCRIPTION-READ] ee1f2460f2993cd0b5062777433951432700e827a8149d89b662fcce07a7a7f0 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Check Not justified as shipped
What this change shipsInventory (6 items) — 5 justifiedIntent: stop a prefetch or prewarm reclaim from killing sub-agents that a parent session still owes, or dropping their reports. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] f2096e9 Description read[DESCRIPTION-READ] 47d0687eb3a1a8b4c514671d7d2c2fe4ce83c2997ae8c706ce08f75307daf77e That sha256 names the PR description this verdict read. Recompute it with |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
44d190e to
c9748bf
Compare
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
c9748bf to
d17833b
Compare
ed12833 to
12ab45f
Compare
12ab45f to
7e6ce9f
Compare
7e6ce9f to
5286eb7
Compare
|
Following up on the design call above: The new head
The diff is now one source file plus tests and a spec line. The PR body is rewritten to match. |
UX Review (Opus 5.5) — ⏭️ skippedRevision |
|
GPT 6.1 BLOCK on The finding holds. A report task registered in Fix: |
|
First Principles CONCERNS on
|
buluoray
left a comment
There was a problem hiding this comment.
LGTM. I checked this at head cf54519.
What it fixes: an unclaimed resume-prefetch session that timed out, or was evicted by the prefetch cap, while sub-agents it owned were still running or still delivering their report, cancelled those sub-agents and lost their results. The reclaim now keeps that session.
Checked: the reclaim asks both has_unfinished_report_for and has_pending_work_for_async (session_lifecycle.py:2772-2773), re-checks under the lock, and treats a raising read as "keep" (fail-closed). The fence is released in finally.
Mutations: removing both pending-work probes reddens test_remove_if_unclaimed_keeps_a_parent_that_owes_work[pending_work] and test_remove_if_unclaimed_keeps_a_parent_that_gains_work_during_the_read; removing has_unfinished_report_for reddens ...owes_work[unfinished_reports]; falling through after a raising read reddens ...owes_work[store_read_raises]; dropping the fence release reddens 6 tests; dropping not task.done() reddens test_an_unfinished_report_is_owed_after_its_run_task_ends.
CI: the one red, macOS Tests / Backend Tests (macOS) (2), failed on both attempts only on test_provider_spawn_off_loop_18172.py::...::test_cancellation_during_the_spawn_still_reaps, a test this PR does not touch. #19310 and #19451 already track that flake. The other non-green lanes are cancelled runs.
Non-blocking follow-ups:
- No test pins the async-only half of
has_pending_work_for_async(store-side overflow): removing the off-lock call alone stays green because the test fake drives both probes from one dict. chat_runner.py:5576-5577still saysFalsemeans "claimed or already gone"; it can now also mean kept because it owes work.- A kept prefetch session no longer counts against the prefetch cap while its process lives on. It is bounded by the idle sweep, but it quietly changes what the cap bounds.
subagent.mdhas no entry for the new publicSubagentManager.has_unfinished_report_for, while its siblings are listed.
cf54519 to
0a01ff7
Compare
0a01ff7 to
f3ad874
Compare
remove_if_unclaimed reclaimed an idle speculative session on the strength of a free semaphore, which proves only that the parent's own turn ended. Children spawned by that turn run on the parent's runtime, and a finished child still owes the parent its report, so the reclaim cancelled live work. The reclaim now takes the identity sweep's spare-children shape: open a tentative teardown fence under the lock, ask the manager's pending-work signal and the fence-aware store read off the lock, then re-ask the in-memory half and promote the fence under the lock before the pop. Owed work, a failed read or a pending injection keeps the parent. Co-authored-by: Premshay <28099628+Premshay@users.noreply.github.com>
f3ad874 to
f2096e9
Compare
Problem / Motivation
Goal: Stop a speculative-prefetch reclaim from tearing down a parent session that still owes sub-agent work.
SessionLifecycleService.remove_if_unclaimed(the resume-prefetch TTL and the prewarm cap eviction) reclaims an idle speculative session when its first turn is unclaimed and its semaphore is free. A free semaphore proves only that the parent's own turn ended. Children spawned by that turn run on the parent's runtime, and a finished child still owes the parent its report. On currentmainthe reclaim checks neither, so it cancels live children and drops their results.Why it matters
The reclaim fires on a timer and on tab-focus pressure, so it silently kills in-progress sub-agent work under a race that is hard to see from the outside.
Not a goal
has_unfinished_report_forstays a separate probe rather than a term insidehas_pending_work_for, because that signal also drives the gateway reset-deferral sites and the folder-move guard, and widening it there is its own change.SubagentManager. The earlier revision of this PR added a per-parent attachment generation and an in-flight delivery count; both are gone.What changed
The reclaim now reuses the two primitives
mainalready has for the identity sweep's spare-children path, so it reads one owed-work signal instead of a union of per-interleaving guards:open_teardown_fence).has_pending_work_for_async(queued, running, a finished child whose terminal report is not registered yet, a live follow-up watcher) and_parent_has_sparable_child(live/queued, fenced admissions, store-only rows). A read that raises keeps the parent.has_unfinished_report_for(new, in-memory) is asked here and again under the lock: a registered terminal report still delivering after its run's task ended — a reaped run's report waits across the reap's awaits — which_has_live_parent_run_taskdoes not see.has_live_or_queued_childrenandhas_in_memory_pending_work_for, then commit only ifcommit_identity_sweep_retirepromotes the fence. A commit selects children withsnapshot_teardown_children_only, as the identity sweep does._ChildTeardownHandlergains the two pending-work methodsSubagentManageralready exposes, plushas_unfinished_report_for(a read over_report_owners).Backwards compatibility
Compatible. No field, key or validator changes. The only behaviour change is that a reclaim now keeps a parent it would previously have reaped.
Tests
test/test_session.py::TestParentEndCancelsItsChildren:test_remove_if_unclaimed_keeps_a_parent_with_a_live_child— the regression: fails onmain(the reclaim returns True and cancelsrun-1).test_remove_if_unclaimed_keeps_a_parent_that_owes_work[pending_work|unfinished_reports|store_children|fenced_children|store_read_raises]— owed work the live probe cannot see, and fail-closed on an unreadable store.test_remove_if_unclaimed_keeps_a_parent_that_gains_work_during_the_read— work appearing during the off-loop read.test_remove_if_unclaimed_reclaims_a_parent_that_owes_nothing— the reclaim still works and promotes the fence.test/test_subagent_queued_visibility.py::test_an_unfinished_report_is_owed_after_its_run_task_ends— the real manager's_report_ownersread.The keep-tests fail with
main'ssession_lifecycle.pyand pass with this change. Local:test/test_session.py -k "TestParentEndCancelsItsChildren or unclaimed",test/test_eager_spawn.py,test/test_session_coverage.py,test/metrics/green; black ratchet, isort, flake8, mypy and comment-history gates green.Manual verification
N/A — no user-visible surface; the race is covered by the unit tests above.
Related Issues
Replacement for #10987 (fork PR by @Premshay, not pushable to this repository). Original author credited via
Co-authored-by.Pattern harvest
Rule candidate: review-prompt
Pattern: a teardown path that gates on a free semaphore alone ("idle") must also ask the sub-agent manager's pending-work signal before it pops, because children and owed reports outlive the parent's own turn.
Checklist
docs/system-specs/modules/session.md