Skip to content

fix(channel): let an idle listening member answer a clear-context - #11707

Merged
bolichen97 merged 1 commit into
kirodotdev:mainfrom
rnoack1:split/idle-member-clear-context
Sep 22, 2026
Merged

bolichen97 merged 1 commit into
kirodotdev:mainfrom
rnoack1:split/idle-member-clear-context

Conversation

@rnoack1

@rnoack1 rnoack1 commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Carved from #7161 under an authorised split. That PR is +12601 across 89 files; this is one
reviewable behaviour from it, cut off the current main tip. It is the member-side half of
the clear-context work whose handler-side half is #11691.

Problem / Motivation

A channel member holds its lifecycle lease for its whole listening life, not for a turn. Three
consequences, each with a test here:

  • An idle member refuses clear-context forever, because the lease is held whether or not a
    turn is running.
  • A member in the window between dequeuing a message and declaring its turn looks idle to
    has_active_turn, so a clear in that window would destroy a session that is about to stream.
  • A background sweep that recycles idle sessions tears down the provider a listener cached
    at spawn, leaving every later message driving a dead provider with nothing to re-fetch it.

Why it matters

The first is a feature that silently never works on a live channel. The third is worse than a
refusal: the sweep succeeds, the listener keeps running, and its next message goes to a shut-down
provider — a failure that surfaces later and nowhere near its cause.

What changed (motivation → approach → change)

_turn_in_flight asks two different questions depending on who is asking.

A background sweep passes nothing and keeps the lease test, which is deliberately the
stricter one: it also covers a turn that has acquired the semaphore but not yet put a prompt in
flight, and it is what keeps the sweep off a listening member.

A caller acting on an explicit user request passes refuse_only_on_active_turn and gets the
narrower question — the holder's own lifecycle_turn_active first, since a turn begins at
dequeue and the setup before the prompt reads as idle, then the provider's has_active_turn. An
unknown provider shape keeps the strict answer, because refusing a teardown is recoverable and
tearing down a streaming reply is not.

On the member side, a session discarded from under a listener is re-acquired rather than reused,
so the cached provider cannot outlive the clear.

path added kind
test/test_channel_clear_context_idle_member.py 386 the tests for this behaviour (new file)
test/test_channel_prompt_busy.py 130 carried identically with #11691 — test-fake update
src/kiro_crew/channel.py 58 a strict subset of #11691's copy
src/kiro_crew/session_lifecycle.py 50 a strict subset of #11691's copy — _turn_in_flight
src/kiro_crew/session.py 45 carried identically with #11691 — lease and turn markers
total 669 5 files

session.py and test/test_channel_prompt_busy.py are byte-identical to #11691's copies.
channel.py and session_lifecycle.py are now a strict subset of them: every one of the 58
added channel.py lines and all 50 in session_lifecycle.py appear in #11691's added set, while
124 of #11691's channel.py added lines are absent here. What came out is the _log_lock hunk,
has_queued_work, and one stray blank line — subtracted as undeclared riders, so post() is now
byte-identical to main.

This unit still compiles and tests alone, and merge order still imposes no dependency — but
whichever lands second now needs a real rebase of those two files rather than a no-op textual
match.

Tests

test/test_channel_clear_context_idle_member.py is new, 12 tests in four classes:

  • An idle listening member does not refuse forever (5) — a clear succeeds while the member
    holds its lease; a deferred project reset lands the same way; a declared turn still defers
    it; a reply in flight still refuses; and a plain turn lease still refuses without asking the
    provider
    , which is the control that keeps the narrow question from being applied to
    non-lifecycle callers.
  • A clear during pre-stream setup does not destroy the session (2) — the dequeue-to-declare
    window refuses, and the same clear succeeds once the turn is released.
  • A re-acquired session keeps its turn protection (1) — a clear cannot tear down a session
    re-acquired mid-turn.
  • A background sweep leaves a listening member alone (4) — the sweep does not recycle a
    member holding its lease, the user-initiated clear still clears that same member, a session
    the listener acquires still answers a user clear, and a static check that no acquire site
    sits outside the functions these controls drive.

Manual verification

N/A — unit coverage is sufficient.

A regression sweep over 129 suites (every test importing channel, session or
session_lifecycle) gives 4531 passed, 0 failed. The identical sweep on a pristine checkout of
the same base gives 4515 passed, 0 failed, 2 errors, and both errors are can't start new thread from host thread exhaustion rather than from the tree. The +14 passes are this unit's 12 new
tests plus 2 from the carried fake update.

Pattern harvest

Rule candidate: review-prompt

Pattern: a busy predicate that answers from a held lease rather than a declared turn,
applied to a caller whose lease is held for a lifetime.

skip_if_busy read session.semaphore.locked() and nothing else. That is the right answer for a
caller whose lease spans one turn, and a permanent refusal for one that holds it across an idle
listening life — the refusal never lifts, so the retry-when-idle the caller offers can never
succeed. The defect is not a wrong predicate; it is one predicate serving two questions that
differ in whose "busy" is meant.

The generalizable check is that a busy/blocked predicate must name the scope it measures, and
every call site must be classifiable against that scope. A predicate whose refusal has no
reachable end state is the signature: if no state transition can make it return false, the
feature behind it silently never works. That reads as a review prompt rather than a semgrep
pattern, because the lease-lifetime relationship is not local to the expression.

Related Issues

Carved from #7161. Handler-side half is #11691; shares carried primitives with #11163.

One test class from the source file is not included: TestAnAgentOnlyArmDoesNotForceAColdStart EveryTurn calls mark_retire_on_next_claim and reads arms through
SessionAllocationService._arm_if_any, which do not exist on main. It reopens with the
retirement-arm subsystem.

@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 Sep 18, 2026
@dwu96

dwu96 commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR is currently in draft status. Workflow runs won't be auto-approved until it's marked as ready for review.

When you're ready, click "Ready for review" and the workflows will be approved on the next cycle automatically.

3 similar comments
@dwu96

dwu96 commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR is currently in draft status. Workflow runs won't be auto-approved until it's marked as ready for review.

When you're ready, click "Ready for review" and the workflows will be approved on the next cycle automatically.

@dwu96

dwu96 commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR is currently in draft status. Workflow runs won't be auto-approved until it's marked as ready for review.

When you're ready, click "Ready for review" and the workflows will be approved on the next cycle automatically.

@dwu96

dwu96 commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! This PR is currently in draft status. Workflow runs won't be auto-approved until it's marked as ready for review.

When you're ready, click "Ready for review" and the workflows will be approved on the next cycle automatically.

@rnoack1
rnoack1 marked this pull request as ready for review September 18, 2026 10:49
@rnoack1
rnoack1 requested review from a team and hoang-phan98 September 18, 2026 10:49
@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 Sep 18, 2026
@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ✅ no blocking findings

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

Review details

FINDING -- src/kiro_crew/channel.py:772 -- lifecycle marker calls can be deleted without failing tests -> Fix: extend the existing run_channel_agent test to assert lease and turn-marker transitions.
[GPT-REVIEWED] 7180a23

@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — 🟡 CONCERNS

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

Design-Verdict: CONCERNS

The lifecycle-lease marker is never unmarked, so it outlives a dead member on its surviving session and strips later turn-scoped holders of the strict busy answer.

Watch

mark_lifecycle_lease only sets True, and the member's finally resets only the turn flag before sessions.release(agent.session_key) (channel.py:875) leaves the session registered. A turn-scoped holder that later acquires that same key — the PR's own test names "a surfaced dashboard tab can then be linked to the same key" — inherits lifecycle_lease=True, so an explicit clear with refuse_only_on_active_turn=True falls through to has_active_turn and can tear the session down in that holder's pre-prompt window: precisely the loss test_a_plain_turn_lease_still_refuses_without_asking_the_provider pins for unmarked sessions. One dropped message, retryable, but it is a hole in this PR's own invariant.
Clears when: the member's finally (and the failed-reacquire exit) unmarks the lease alongside the existing turn backstop, with a test that a post-death re-acquire of the key answers the strict question again.

[DESIGN-REVIEWED] 7180a23

@github-actions

github-actions Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — 🟡 CONCERNS

Premise-level review of 7180a2355d31d8fd9b3acaf6b9552d9fdb61fbbf 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: CONCERNS

The narrow-question machinery — flag, two markers, two manager methods — has zero production callers here; its only consumer is unmerged #11691.

Not justified as shipped

  • Item 3 — zero consumers: grep for refuse_only_on_active_turn in src/ finds only the definition and plumbing (session.py, session_lifecycle.py); no production caller passes it, and the description itself says the caller is "the handler-side half is fix(channel): refuse clear-context while a turn is in flight #11691". The base handler (handlers_channel.py:652) resets with no skip_if_busy at all, so the "idle member refuses clear-context forever" defect is not reachable on base — it exists only in combination with the unmerged half.
    Subtraction: defer the refuse_only_on_active_turn parameter, _turn_in_flight's narrow branch, and the two SessionManager marker methods to land with their one caller in fix(channel): refuse clear-context while a turn is in flight #11691.
    Clears when: fix(channel): refuse clear-context while a turn is in flight #11691 (or any production handler passing refuse_only_on_active_turn=True) merges, giving the surface a counted consumer.
  • Item 4 — zero consumers: lifecycle_lease and lifecycle_turn_active are written by channel.py but read only by the branch in item 3, which nothing production reaches. Same root cause; same clearance.
    Clears when: same as item 3.
  • Item 5 — rides along: _reset_busy_session marking lease+turn is inert for the same reason; harmless rider.

The re-acquire fix (items 1–2) is real on base: api_channel_clear_context resets a member's session unconditionally, stranding the listener on the shut-down provider it cached at spawn — that half is production-active and justified here.

What this change ships

Inventory (5 items) — 2 justified

Intent: make clearing a live channel member's context both possible and survivable — the member-side half of a declared two-PR split; FIX.

  1. A member whose context was cleared re-acquires a session and serves its next message instead of streaming a dead provider until restart — justified
  2. When re-acquire fails, the member posts a visible error and stops listening — justified
  3. Teardowns can ask "declared turn?" instead of "lease held?" via a new refuse_only_on_active_turn option on reset/discard — zero consumers (only caller is unmerged fix(channel): refuse clear-context while a turn is in flight #11691)
  4. Sessions carry lifecycle-lease and turn-declared markers, set by members at spawn/dequeue/finish — zero consumers (read only by item 3's branch)
  5. A recovered wedged session carries the same markers — rides along (inert until item 3 has a caller)

[FIRST-PRINCIPLES-REVIEWED] 7180a23

@github-actions

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] e1f61d4

@rnoack1
rnoack1 force-pushed the split/idle-member-clear-context branch from e1f61d4 to cc09be5 Compare September 19, 2026 19:37
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 20, 2026
Covers the member-side half: an idle member's lifetime lease no longer refuses
forever, and a background sweep leaves a listener's cached provider alone.
@rnoack1
rnoack1 force-pushed the split/idle-member-clear-context branch from cc09be5 to 7180a23 Compare September 22, 2026 01:14
@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 merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Sep 22, 2026
@bolichen97
bolichen97 enabled auto-merge (squash) September 22, 2026 04:42

@bolichen97 bolichen97 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.

Approved after a live re-verification: readiness: passed label, PR Readiness status success on this head, no required check pending/failed/cancelled, mergeable, no effective CHANGES_REQUESTED, and no unresolved human blocking request found in comments or review threads. Native auto-merge (squash) enabled; no admin override used.

@bolichen97
bolichen97 merged commit 3d8f882 into kirodotdev:main Sep 22, 2026
98 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 22, 2026
amadsalmon pushed a commit to amadsalmon/KiroCrew that referenced this pull request Sep 22, 2026
…rodotdev#11707)

Covers the member-side half: an idle member's lifetime lease no longer refuses
forever, and a background sweep leaves a listener's cached provider alone.
wknight1 pushed a commit to wknight1/KiroCrew that referenced this pull request Oct 5, 2026
…rodotdev#11707)

Covers the member-side half: an idle member's lifetime lease no longer refuses
forever, and a background sweep leaves a listener's cached provider alone.
@rnoack1
rnoack1 deleted the split/idle-member-clear-context branch November 14, 2026 17:28
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.

3 participants