Repository navigation
Conversation
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Clearing What this change shipsInventory (7 items) — 7 justifiedIntent: a user who presses Stop at the end of a turn gets that turn cancelled cooperatively, rather than a "compacting" refusal while the compaction is still queued behind that turn. This is a FIX. Its provenance is session.md "Stop Orchestration" step 0 plus a test that fails on base.
I counted 12 [FIRST-PRINCIPLES-REVIEWED] 0044ec1 Description read[DESCRIPTION-READ] 990d626036d388db9ed465f8162cfcca64781dfb1eb0bfd16f22073f84e350bd That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Check that the turn-deadline cancel ( [DESIGN-REVIEWED] 0044ec1 Description read[DESCRIPTION-READ] 2ea3d82a055c8328c0e83fff5390dc2b3729acdb7dcdddf99b7d90dcc54ac062 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 |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
7fa8f5b to
28b14de
Compare
iamwhatever
left a comment
There was a problem hiding this comment.
This PR narrows the "compacting" Stop decline so that a Stop pressed while an auto-compaction is still waiting for the turn permit cancels the turn that holds the permit. It does this through compaction_waits_for_turn, which compares session.turn_owner with the compaction's recorded task. That is the right root cause for the in-place and Claude paths, and the test proves it. But it adds a regression on the uncompactable-recycle path. _recycle_unmanaged (session_compaction.py:1032) takes the permit and never sets session.turn_owner, and no writer clears turn_owner on release (it is only ever assigned, in session_allocation.py and the two compaction paths). Here is the sequence I traced. The dashboard turn triggers check_context_usage, releases the permit, and its task keeps running the end-of-turn tail. The recycle task then holds the permit, but turn_owner is still that live turn task, so compaction_waits_for_turn returns True. A Stop in that window then skips the decline that main gives it: it runs note_stop and provider.cancel on a provider _recycle_held is shutting down, and can escalate to reset mid-recycle. This is the Opus advisory finding, and it has no disposition yet. Listing the recycle path under Not a goal does not cover it, because this PR is what changes that path's behaviour. Fix: set session.turn_owner = asyncio.current_task() right after the acquire in _recycle_unmanaged (as _compact_in_place does at :1070), and add a test that a Stop during an unmanaged recycle is still declined as "compacting". — crew-pr-reviewer (deep re-check)
28b14de to
e6d4289
Compare
|
Thanks, you were right about the recycle path. Point by point: 1. The uncompactable restart reads as the turn it waited behind. Fixed by rebasing onto main 0a47021, which includes #18926. The test you asked for is 2. The Not a goal bullet does not cover the recycle path. Agreed. This PR changed that path's decline, so the bullet was wrong to set it aside. The decline is kept there now (test above). I left the original text in place and am correcting it here: the bullet was meant for changes to those two paths themselves, such as #18926, not for this PR's effect on the Stop decline there. 3. Every release of a holding that named itself now clears the record in the same step:
Tests on the new head: the test file, 3 of 3 serial runs, 98 passed each; the file plus the 52 neighbour files, 5,369 passed; the 110 further files that name the permit machinery, 10,383 passed, 41 skipped, and 2 failures that are not from this change (one fails the same way on origin/main here, the other passes alone). The description's Change, Backwards compatibility, Tests and Related Issues sections are updated to match. |
|
The new commit replaces the short fixed waits that the repo's wait-discipline ratchet (test/test_wait_discipline_ratchet.py) rejects in test/test_compaction_stop_declined.py; it changes that file only. |
740f753 to
6ed04dc
Compare
A cooperative Stop pressed on the turn that triggered an automatic compaction was declined as "compacting" while the compaction was still waiting for that turn's permit. Nothing was cancelled, the turn kept running, and only a second press, a hard kill, stopped it. The threshold trigger commits the key to the compacting set before its task has the turn permit, and the task then waits for it behind the turn that triggered it. stop_turn, and the compaction_in_flight probe every channel runs before its own side effects, declined on membership in that set alone. The spec limits the decline to the window in which the compaction's own /compact turn holds the session. Each compaction now records its task, cleared with the compacting set. compaction_waits_for_turn answers True while the turn permit is held by a live task other than the compaction's own. stop_turn and compaction_in_flight read it through one helper and do not decline in that window. Every other state keeps the decline as it was.
compaction_waits_for_turn reads the permit holder from turn_owner, and nothing cleared that record on release. When the turn a compaction waits behind released the permit, the semaphore handed it to the parked compaction, but until the compaction's task ran, turn_owner still named the turn's task, which was alive. A cooperative Stop in that step was not declined: it was recorded, cleared the queue and went to a provider with no turn, while the permit was the compaction's. Every release of a holding that named itself now clears turn_owner in the same step: SessionManager.release, the Claude arm of _compact_session, _recycle_unmanaged, _compact_in_place and recycle_background. A waiter the permit was handed to names itself when it runs; until then the record names nobody and the decline applies. Tests: a Stop in the hand-off, driven with an event and no await between the release and the Stop, is declined; a Stop after the turn released and before the compaction ran is declined; a turn's release and an awaited compaction's release leave no holder named; and a Stop while the uncompactable restart holds the permit is declined, which relies on the turn_owner record kirodotdev#18926 gave _recycle_unmanaged.
6ed04dc to
0044ec1
Compare
iamwhatever
left a comment
There was a problem hiding this comment.
This narrows the cooperative-Stop "compacting" decline to the window the spec names: a Stop pressed while a triggered compaction still waits for the turn permit now cancels the turn holding it, through compaction_waits_for_turn, which compares turn_owner with the compaction's recorded task, and stop_turn and compaction_in_flight read the same helper so they cannot disagree. Head 0044ec1 meets my change request: rebased onto #18926, _recycle_unmanaged names itself turn_owner right after its acquire, and every release of a named holding now clears it in the same step (SessionManager.release in session_allocation.py, the three compaction paths, recycle_background), which removes the stale record I traced; test_a_cooperative_stop_during_the_triggered_uncompactable_restart_is_declined is the test I asked for, and the author reports it red on 28b14de with (False, 'soft', 1). I tried to break it: in the hand-off step after the turn releases but before the compaction task runs, the record names nobody, so the probe answers False and the Stop is declined (tested); a Stop recorded during the wait cannot be misread as cancelling the compaction, because _recycle_unmanaged and _compact_in_place read stop_gen after the acquire; a compaction with no recorded task, a holder that never named itself, a done holder, or a double without the method all keep the old decline; force=True is untouched. The release only clears the record on the non-ValueError branch, so a stray release cannot wipe a successor's record. All AI lanes PASS, checks are green and the PR is MERGEABLE on main. — crew-pr-reviewer (deep re-check)
Problem / Motivation
Goal: A cooperative Stop is declined as "compacting" only while the automatic compaction holds the session, not while it is still waiting for the turn permit behind the turn the Stop is aimed at.
The spec limits the decline to that window.
docs/system-specs/modules/session.md, "Stop Orchestration", step 0: "Decline a cooperative stop while the session's own automatic/compactturn holds it".docs/architecture/design-notes/soft-stop.mdstep 0 says the same.The code declined on membership in the compacting set alone.
CompactionCoordinator._trigger_compaction(src/kiro_crew/session_compaction.py) adds the key synchronously, then schedules_compact_session, which waits up tocompact_wait_timeout_secsfor the turn permit. The dashboard and every channel call the trigger at the end of a turn, while that turn still holds its lease (dashboard/chat_runner.py, aftercheck_context_usage). In that windowSessionLifecycle.stop_turnandcompaction_in_flight, the probe every stop surface runs before its own side effects (src/kiro_crew/session_lifecycle.py), both answered "compacting".Why it matters
A Stop pressed at the end of a turn was answered "compacting": nothing was cancelled, and the turn kept running. Only a second press stopped it, and the second press is a hard kill and respawn instead of a cooperative cancel.
Not a goal
is_compactingand the compacting set keep their meaning._set_compacting(key, True)after the acquire: its synchronous add is what keeps a second trigger from committing in the same loop turn.turn_owner(session_background.py): the new check cannot see it, so a compaction waiting behind such a turn keeps today's decline./compacttimeout path, which other changes address.What changed (motivation → approach → change)
Symptom: a cooperative Stop on the turn a compaction waits behind returned "compacting" and cancelled nothing (see Tests).
Root cause:
SessionLifecycle.stop_turnandcompaction_in_flightdeclined on membership in the compacting set alone, which_trigger_compactionsets before_compact_sessionholds the turn permit. So the decline read "a compaction is committed" where the spec says "the compaction holds the session".Change:
session_compaction.py:CompactionState.compaction_tasksrecords the task running each compaction._trigger_compactionrecords the task it creates;compact_if_neededrecords the awaiting task._set_compacting(key, False)drops the record with the compacting membership, so the two stay in step.CompactionCoordinator.compaction_waits_for_turn(key), exposed onSessionManager: True only while the session's turn permit is held by a live task, named inturn_owner, other than the compaction's own task. All three compaction paths name themselves in the step their acquire returns (the Claude arm of_compact_session,_compact_in_place, and_recycle_unmanagedsince fix(compaction): honor a Stop during the uncompactable recycle #18926), so once the compaction holds the permit the answer is False. Every other state answers False too, including a compaction with no recorded task and a permit held by a task that has not named itself.turn_ownerin the same step:SessionManager.release(session_allocation.py), the three compaction paths, andrecycle_background(session_background.py). Without it, the record kept naming a turn that had released the permit and was still running its end-of-turn work. In the step after the semaphore handed the permit to the parked compaction, before the compaction's task ran, the probe answered True and a Stop was not declined. The woken waiter names itself when it runs; until then the record names nobody and the decline applies.session_lifecycle.py:stop_turnandcompaction_in_flightread that answer through one helper,_compaction_waits_for_turn, and do not decline while it is True. Only an explicit True narrows the decline, so a manager double without the method keeps the old behaviour.docs/system-specs/modules/session.mdstep 0 says a compaction still waiting for the permit does not hold the session, and that a permit the releasing turn hands to the parked compaction is the compaction's from that release on.Rejected alternatives:
is_compactingitself: it also drives the compacting indicator and the gate's in-progress check.stop_turn: every stop surface probescompaction_in_flightfirst and skips its own side effects on a True, so the two must give the same answer.turn_ownerwith the recorded task reuses the recordresetalready reads to find the permit holder (_orphan_turn_holder).turn_owner: it needs a change at every acquire site as well as at the releases, and leaves the record stale for its other readers.Backwards compatibility
Compatible: a Stop while the compaction holds the permit and runs
/compactis declined and not recorded, as before (test_a_stop_once_the_triggered_compaction_holds_the_session_is_still_declined, and the existingtest_a_cooperative_stop_during_compaction_is_declined_and_not_recorded, which drives_compact_in_placedirectly with no recorded task).Compatible: a Stop while the uncompactable restart holds the permit is declined and not recorded, as before (
test_a_cooperative_stop_during_the_triggered_uncompactable_restart_is_declined).Compatible:
force=Trueis never declined, unchanged.Compatible:
turn_ownernames nobody from a release until the next holder names itself. Its two other readers,_orphan_turn_holder(reset) andcancel_owned_turn, look for the task that holds the permit, which a task that released it is not.Security: no redaction, auth, sandbox, permission or deny code changes (none in this branch's diff).
Removes nothing: no function, outcome or field is removed; one field, one method and one facade method are added, and the background session protocol declares the existing
turn_ownerfield.Tests
Summary, 3 of 3 serial runs for each red and for the file. Red on origin/main 0a47021: the section-1 soft-cancel test fails with
(True, 'compacting', 0). Red on the previous head 28b14de: the hand-off and restart tests fail with(False, 'soft', 1), and the release test with "the turn's release left its record". Green on this branch: the test file, 98 passed; with the 52 neighbour files, 5,369 passed; the 110 permit-machinery files, 10,383 passed and 2 failures that are not from this change (details below).New tests in
test/test_compaction_stop_declined.py:test_a_stop_on_the_turn_a_compaction_waits_behind_is_a_soft_cancel(section 1): the user's turn holds the permit,check_context_usagetriggers the compaction, which parks on the permit before/compactstarts.compaction_in_flightanswers False,stop_turn(force=False)answers "soft", the provider's cancel runs once, and the Stop is recorded.test_a_stop_once_the_triggered_compaction_holds_the_session_is_still_declined(section 1, control): the same trigger on an idle session. Once/compactruns,compaction_in_flightanswers True and the Stop is declined as "compacting", not recorded, and cancels nothing.test_a_cooperative_stop_during_the_triggered_uncompactable_restart_is_declined(uncompactable-restart section): the same trigger on a backend with no/compact. The turn releases the permit and its task keeps running; the restart takes the permit and waits for a shared sub-agent.compaction_in_flightanswers True,stop_turn(force=False)answers "compacting", nothing is cancelled or recorded, and the restart then recycles.test_a_stop_while_the_turn_hands_the_permit_to_the_compaction_is_declined(section 1): the turn holds the permit and the triggered compaction parks on it, seen through an event the test's permit sets when a claimant parks (no sleeps). The turn releases, and nothing awaits before the checks, so the semaphore has handed the permit to the compaction, whose task has not run, while the turn's task is alive.compaction_in_flightanswers True,stop_turn(force=False)answers "compacting", nothing is cancelled or recorded.test_a_stop_after_the_turn_releases_before_the_compaction_ran_is_declined(section 1, control): the turn releases before the compaction's task has run at all, so nobody holds the permit, and the Stop is declined.test_a_release_leaves_no_holder_named_on_the_session(section 1): a turn's release clearsturn_owner, and so does an awaited compaction (compact_if_needed), which holds the permit in its caller's task.Red on origin/main 0a47021 (the test file copied onto it, the two section-1 tests, 3 of 3 serial runs; the control passes there, as a control should): 1 failed, 1 passed each.
Red on this PR's previous head 28b14de (the test file copied onto it; the restart, hand-off, free-permit and release tests; 3 of 3 serial runs): 3 failed, 1 passed each. The free-permit control passes there, as a control should. That head's
_recycle_unmanageddid not name itselfturn_owner, and no release cleared the record. The restart, hand-off and free-permit tests readCompactionState.compaction_tasks, so they cannot run on main, where the decline is unconditional.Removing only the clear in
_compact_in_placefails the release test at "the compaction's release left its record", while the hand-off test, which depends onSessionManager.release, still passes.Green on this branch (rebased onto origin/main 0a47021):
pytest -n 0 test/test_compaction_stop_declined.py, 3 of 3 serial runs: 98 passed each.pytest -n 0overtest/test_compaction_stop_declined.pyfirst and then the 52 other test files that namesession_compaction,compaction_in_flight,stop_turn,is_compactingor the decline markers: 5,369 passed.pytest -n 8over the 110 further test files that name the permit machinery (session_allocation,session_background,_reacquire_and_validate,try_acquire,open_task_session,recycle_background,cancel_owned_turn,absorb_orphaned_release,orphaned_holders,adopt_turn,_orphan_turn_holder,_wake_turn_waiters,_ProviderBgSession): 10,383 passed, 41 skipped, 2 failed. Neither failure is from this change:test_native_skill_projection.py::test_launch_record_resources_match_the_alias_file_kiro_cli_loadsfails the same way on origin/main 0a47021 in the same environment, andtest_gateway_lock_diagnosis.py::test_flock_is_held_by_a_fork_orphanpasses when run alone.scripts/check_black_formatting.py,scripts/check_subprocess_encoding.py,COMMENT_HISTORY_BASE_REF=origin/main scripts/check_comment_history.pyand./scripts/docs-lint.sh: all exit 0.Manual verification
Ran the new tests against the unchanged source (red, above) and on this branch (green), with
pytest -n 0. They driveSessionManager,check_context_usageandstop_turnin-process with an AsyncMock provider; no gateway, model or network.Related Issues
Related: #14850 fixing issue #14841 (added the decline). #18926 (merged) gave
_recycle_unmanagedtheturn_ownerrecord this check reads. Open #3753 adds lines next to thestop_turndecline, and open #11377 adds fields toCompactionState. At the previous base 843abe3, all 63 open PRs that touch a changed file were checked withgit merge-treeagainstorigin/mainand against this branch: this change added no conflict to any of them.Pattern harvest
Rule candidate: review-prompt
Pattern: a "busy" flag set when work is scheduled, read as if the work had already started; check whether a reader needs "scheduled" or "running".
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)