Skip to content

fix(session): decline a Stop only once the compaction holds the session - #18935

Merged
javenciu merged 2 commits into
kirodotdev:mainfrom
javenciu:fix/stop-not-declined-before-compaction-holds
Oct 13, 2026
Merged

javenciu merged 2 commits into
kirodotdev:mainfrom
javenciu:fix/stop-not-declined-before-compaction-holds

Conversation

@javenciu

@javenciu javenciu commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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 /compact turn holds it". docs/architecture/design-notes/soft-stop.md step 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 to compact_wait_timeout_secs for 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, after check_context_usage). In that window SessionLifecycle.stop_turn and compaction_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

  • Changing what the dashboard shows while a compaction is in flight: is_compacting and the compacting set keep their meaning.
  • Moving _set_compacting(key, True) after the acquire: its synchronous add is what keeps a second trigger from committing in the same loop turn.
  • A turn that takes the permit without recording turn_owner (session_background.py): the new check cannot see it, so a compaction waiting behind such a turn keeps today's decline.
  • The uncompactable recycle and the /compact timeout 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_turn and compaction_in_flight declined on membership in the compacting set alone, which _trigger_compaction sets before _compact_session holds 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_tasks records the task running each compaction. _trigger_compaction records the task it creates; compact_if_needed records 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 on SessionManager: True only while the session's turn permit is held by a live task, named in turn_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_unmanaged since 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.
  • The release that ends a holding that named itself clears turn_owner in the same step: SessionManager.release (session_allocation.py), the three compaction paths, and recycle_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_turn and compaction_in_flight read 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.md step 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:

  • Changing is_compacting itself: it also drives the compacting indicator and the gate's in-progress check.
  • Checking only stop_turn: every stop surface probes compaction_in_flight first and skips its own side effects on a True, so the two must give the same answer.
  • Recording the holder in a new flag set by each permit holder: it needs edits inside every compaction path. Comparing turn_owner with the recorded task reuses the record reset already reads to find the permit holder (_orphan_turn_holder).
  • An acquire count the probe compares with one stored beside 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 /compact is declined and not recorded, as before (test_a_stop_once_the_triggered_compaction_holds_the_session_is_still_declined, and the existing test_a_cooperative_stop_during_compaction_is_declined_and_not_recorded, which drives _compact_in_place directly 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=True is never declined, unchanged.
Compatible: turn_owner names nobody from a release until the next holder names itself. Its two other readers, _orphan_turn_holder (reset) and cancel_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_owner field.

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_usage triggers the compaction, which parks on the permit before /compact starts. compaction_in_flight answers 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 /compact runs, compaction_in_flight answers 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_flight answers 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_flight answers 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 clears turn_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.

E       AssertionError: a cooperative Stop on the turn the compaction waits behind was declined: (compaction_in_flight, stop_turn, cancels) = (True, 'compacting', 0)
FAILED test/test_compaction_stop_declined.py::test_a_stop_on_the_turn_a_compaction_waits_behind_is_a_soft_cancel

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_unmanaged did not name itself turn_owner, and no release cleared the record. The restart, hand-off and free-permit tests read CompactionState.compaction_tasks, so they cannot run on main, where the decline is unconditional.

E           AssertionError: a cooperative Stop during the permit hand-off to the compaction was not declined: (compaction_in_flight, stop_turn, cancels) = (False, 'soft', 1)
E       AssertionError: the turn's release left its record
E           AssertionError: a cooperative Stop during the uncompactable restart was not declined: (compaction_in_flight, stop_turn, cancels) = (False, 'soft', 1)

Removing only the clear in _compact_in_place fails the release test at "the compaction's release left its record", while the hand-off test, which depends on SessionManager.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 0 over test/test_compaction_stop_declined.py first and then the 52 other test files that name session_compaction, compaction_in_flight, stop_turn, is_compacting or the decline markers: 5,369 passed.
  • pytest -n 8 over 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_loads fails the same way on origin/main 0a47021 in the same environment, and test_gateway_lock_diagnosis.py::test_flock_is_held_by_a_fork_orphan passes when run alone.
  • mypy (5 changed source files), flake8, isort, black (no changed file is in the black baseline), scripts/check_black_formatting.py, scripts/check_subprocess_encoding.py, COMMENT_HISTORY_BASE_REF=origin/main scripts/check_comment_history.py and ./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 drive SessionManager, check_context_usage and stop_turn in-process with an AsyncMock provider; no gateway, model or network.

Related Issues

Related: #14850 fixing issue #14841 (added the decline). #18926 (merged) gave _recycle_unmanaged the turn_owner record this check reads. Open #3753 adds lines next to the stop_turn decline, and open #11377 adds fields to CompactionState. At the previous base 843abe3, all 63 open PRs that touch a changed file were checked with git merge-tree against origin/main and 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

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 10, 2026 20:09
@javenciu
javenciu requested a review from CrysisDeu October 10, 2026 20:09
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Oct 10, 2026
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision 0044ec15bfa631fbbf6ab62265d3e0edf432c3d1 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of 0044ec15bfa631fbbf6ab62265d3e0edf432c3d1 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Clearing turn_owner on release also changes what _orphan_turn_holder (reset) and cancel_owned_turn read. Before merging, check that neither relied on the stale holder.

What this change ships

Inventory (7 items) — 7 justified

Intent: 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.

  1. Stop pressed while a compaction waits behind the running turn now soft-cancels that turn — justified
  2. compaction_in_flight, the probe every stop surface runs first, gives the same answer as stop_turn in that window — justified
  3. A Stop during the hand-off, after the turn released the permit and before the compaction runs, is still declined — justified
  4. Every release of a holding that named itself now clears turn_owner (5 sites) — justified
  5. New per-key compaction task record, kept in step with the compacting set — justified
  6. SessionManager.compaction_waits_for_turn facade (1 consumer: the session_lifecycle helper) — justified
  7. session.md step 0 now describes the waiting and hand-off windows — justified

I counted 12 semaphore.release() calls under src/kiro_crew. Besides the 5 the PR covers, 4 are session-permit releases it leaves alone: session_allocation.py:1170, session_allocation.py:1173, session_lifecycle.py:1147 and session_background.py:246. None of them releases a holding that had already written turn_owner, so none is an unfixed sibling. The other 3 are unrelated semaphores (two hook semaphores in dashboard/handlers/hooks.py and one in knowledge/llm_pool.py).

[FIRST-PRINCIPLES-REVIEWED] 0044ec1

Description read

[DESCRIPTION-READ] 990d626036d388db9ed465f8162cfcca64781dfb1eb0bfd16f22073f84e350bd

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 0044ec15bfa631fbbf6ab62265d3e0edf432c3d1 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that the turn-deadline cancel (dashboard/turn_dispatch.py → cancel_owned_turn) never needs to fire after release, because a cleared turn_owner now makes it return None.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed 0044ec15bfa631fbbf6ab62265d3e0edf432c3d1 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 0044ec1

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 0044ec15bfa631fbbf6ab62265d3e0edf432c3d1 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 0044ec1

@javenciu
javenciu force-pushed the fix/stop-not-declined-before-compaction-holds branch from 7fa8f5b to 28b14de Compare October 11, 2026 09:32
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 11, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@javenciu
javenciu force-pushed the fix/stop-not-declined-before-compaction-holds branch from 28b14de to e6d4289 Compare October 12, 2026 09:32
@javenciu

Copy link
Copy Markdown
Contributor Author

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. _recycle_unmanaged names itself turn_owner in the step its acquire returns (session_compaction.py:1076) and releases only through absorb_orphaned_release (session_compaction.py:1091), the same contract _compact_in_place follows (session_compaction.py:1123). With all three compaction paths writing the record, holder is task is the answer once a compaction holds the permit. The compaction_waits_for_turn docstring (session_compaction.py:393) states the rules the answer depends on, so a path that breaks them is visible in review.

The test you asked for is test_a_cooperative_stop_during_the_triggered_uncompactable_restart_is_declined (test/test_compaction_stop_declined.py:2635). It follows your trace: check_context_usage triggers the restart on a backend with no /compact while the user's turn holds the permit. The turn releases and its task keeps running. The restart takes the permit and waits for a shared sub-agent. The test asserts (compaction_in_flight, stop_turn, cancels) == (True, "compacting", 0), then that the Stop was not recorded and that the restart goes on to recycle. On this PR's previous head 28b14de it fails 3 of 3 with (False, 'soft', 1); on the new head it passes 3 of 3. This also settles the Opus advisory finding, which is the same issue.

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. turn_owner is never cleared on release (the Design Review's advisory). Fixed. It also caused a wrong answer the earlier tests did not reach: when the turn released, the semaphore handed the permit to the parked compaction, but until the compaction's task ran, turn_owner still named the turn's task, which was alive. The probe answered True there, and a Stop was not declined while the permit was the compaction's.

Every release of a holding that named itself now clears the record in the same step: SessionManager.release (session_allocation.py:1673), the Claude arm (session_compaction.py:776), _recycle_unmanaged (session_compaction.py:1093), _compact_in_place (session_compaction.py:1201) and recycle_background (session_background.py:1083). In the hand-off the record names nobody until the compaction's task runs, and the decline applies. The remaining release sites release holdings that never named themselves: the refusal paths of _reacquire_and_validate (session_allocation.py:1170, :1173; it names the holder only on the valid path, :1175), the background prompt (session_background.py:248), and _wake_turn_waiters (session_lifecycle.py:1167), which runs after reset has moved the record to orphaned_holders. A holder that has not named itself reads as nobody and keeps the decline, as on main. That covers a claimant queued ahead of the compaction, for the one step before it runs. It also makes the Not a goal line about a turn that takes the permit without recording turn_owner hold as written.

test_a_stop_while_the_turn_hands_the_permit_to_the_compaction_is_declined (test/test_compaction_stop_declined.py:310) drives the hand-off with no sleeps. The test's permit sets an event when the compaction parks on it, the turn releases, and nothing awaits before the checks. It asserts (True, "compacting", 0) and that the Stop was not recorded; on 28b14de it fails 3 of 3 with (False, 'soft', 1). test_a_stop_after_the_turn_releases_before_the_compaction_ran_is_declined (:350) covers the window where nobody holds the permit yet: declined, on both heads. test_a_release_leaves_no_holder_named_on_the_session (:380) pins the clears for a turn and for an awaited compaction: it fails 3 of 3 on 28b14de, and removing only the _compact_in_place clear fails it at the compaction's record.

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.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: passed Eligible automated validation passed for the current revision readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
@javenciu

Copy link
Copy Markdown
Contributor Author

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.
The file counted 47 short fixed waits and counts 35 again, its baseline (eight 5 s ceilings raised to 30 s, two polls moved to async_wait_until); the ratchet passes on the head merged with main (c4c51aa), and the file passes 3 of 3 runs (98 tests each).

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
@javenciu
javenciu force-pushed the fix/stop-not-declined-before-compaction-holds branch from 740f753 to 6ed04dc Compare October 12, 2026 14:51
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
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.
@javenciu
javenciu force-pushed the fix/stop-not-declined-before-compaction-holds branch from 6ed04dc to 0044ec1 Compare October 12, 2026 19:41
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
@javenciu
javenciu enabled auto-merge (squash) October 12, 2026 22:34
@javenciu
javenciu requested a review from iamwhatever October 12, 2026 22:34

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@javenciu
javenciu merged commit efd064d into kirodotdev:main Oct 13, 2026
105 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants