Repository navigation
fix(session): recycle the shared _bg conversation after one-liners - #18234
Conversation
766506e to
4118ee3
Compare
On a background backend outside _bg_runtime_backends() (claude, opencode, pi, goose, deepseek) every run_bg_oneliner turn (titles, nav labels, folder icons, summaries) is appended to the one persistent _bg conversation. _ProviderBgSession.destroy() only releases the turn semaphore and run_bg_oneliner never called recycle_background(), so no criterion bounded that conversation: it grew for the whole gateway uptime until the provider refused it as prompt-too-long. After that the reported percentage was still the last successful turn's, so even background_turn()'s recycle kept the poisoned conversation and every later background turn failed the same way until a restart. - run_bg_oneliner runs recycle_background() after destroy() whenever the handle is a _ProviderBgSession (shares_background_conversation). - A turn refused as too long forces the recycle on that path: the one-liner passes context_overflowed=True, and stream_and_collect leaves the verdict on the provider (mark_context_overflowed) because callers such as history consolidation swallow the error. - is_context_overflow_error() matches the classifier tag or the backend text; the claude adapter's "Prompt is too long" is in the JSON-RPC message, which the classifier does not read. - Runtime-capable backends (kiro, kas, codex) are unchanged: their one-liner handles carry no marker and the forced recycle is gated off. Closes #18230 Refs #15336
4118ee3 to
76722e4
Compare
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — 🟡 CONCERNSPremise-level review of First-Principles-Verdict: CONCERNS
Not justified as shipped
What this change shipsInventory (6 items) — 5 justifiedIntent: keep background titles, labels and consolidation working on non-kiro backends however long the gateway runs. FIX.
[FIRST-PRINCIPLES-REVIEWED] 76722e4 Description read[DESCRIPTION-READ] 764d2080e1723cdb4b55ecec4086d39c94d3fde8cbd005b0df47474fcac87404 That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — 🟡 CONCERNSDesign-level review of Design-Verdict: CONCERNS The recycle runs inside the caller's Watch
[DESIGN-REVIEWED] 76722e4 Description read[DESCRIPTION-READ] 33f5353ef6bec2305bb17387db63b665ff6e4717f7bb7bfc1fd6c6d2c3b4dddc 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 — ✅ 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: |
Opus 5.5 Review — ✅ no blocking findingsReviewed Review detailsNo findings that block the merge; two advisory findings below. FINDING — src/kiro_crew/llm_helpers.py:1915 — The FINDING — src/kiro_crew/llm_helpers.py:1915 — [OPUS-REVIEWED] 76722e4 Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
CrysisDeu
left a comment
There was a problem hiding this comment.
Approving at 76722e4. Read the full diff plus the owning spec; nothing blocking. Non-blocking notes, worth a follow-up or a quick fix before merge:
- [MINOR] src/kiro_crew/llm_helpers.py:1916 -- the recycle (which may spawn a replacement provider under
_start_sematStartPriority.BACKGROUNDandawait doomed.shutdown()) runs inside the caller'sawait, in thefinally, OUTSIDE thetimeoutthe caller passed torun_bg_oneliner. Before this PR a one-liner never triggered a respawn; now ~1 in 40 does, and a FOREGROUND caller with a tight deadline (STT endpointing,stt_stream.py:430, 5s) waits a whole claude/opencode spawn for a result it then likely discards. Already posted by the Design Review lane (CONCERNS, advisory) and not addressed.background_turn()has the same shape today, so this is consistent rather than new, but a_fire_and_forget-style tracked task for the recycle (or passingstart_prioritythrough) would clear it cheaply. - [NIT] src/kiro_crew/llm_helpers.py:249 -- second overflow detector reading
str(exc)(incl. JSON-RPCmessage), whiletransport_errorsdeliberately tagscontext_overflowfromdataonly; the two now disagree on the claude "Prompt is too long" error andsubagent_manager/run.py:1020(the tag's other reader) still misses it. Already posted by First Principles (CONCERNS) and explicitly declared a non-goal in the body. Acceptable for a recycle-only consumer (false positive = one respawn), but worth a follow-up issue so detection does not stay split.
Problem / Motivation
Goal: On non-runtime background backends, background one-liners stop growing one shared conversation until every background feature fails with prompt-too-long.
On any background backend outside
_bg_runtime_backends()(today claude, opencode, pi, goose and deepseek), chat titles, nav link labels, folder icons, session summaries and tips each append a turn to the one persistent_bgconversation. Nothing recycles that conversation after a one-liner, so it grows for the whole gateway uptime. Eventually the provider refuses it as too long. From then on every background caller sharing it (titles, link labels, memory consolidation) fails the same way until the gateway restarts.Why it matters
Until it overflows, each one-liner re-sends the whole accumulated transcript, so background spend grows with uptime instead of staying flat. After it overflows, titles, nav labels and memory consolidation silently stop working. Everyone who runs Crew on a non-kiro backend for more than a short session is affected.
Not a goal
_bgruntime used by kiro, kas and codex._bgsession (that is fix(history): isolate consolidation sessions #9036).What changed (motivation → approach → change)
Symptom → cause:
get_bg_session()gives a non-runtime backend a_ProviderBgSessionover the sharedBACKGROUND_KEYentry. Itsdestroy()only releases the turn semaphore.run_bg_oneliner()ended atdestroy()and never calledrecycle_background(), even thoughdocs/system-specs/modules/session.mdsays that runs "after every background task". So no recycle criterion was ever checked for one-liner traffic.recycle_background()decides fromcontext_usage_pct(). After a prompt-too-long refusal that is still the last successful turn's figure, so evenbackground_turn()'s recycle could keep a conversation every later turn would be refused on.Change:
_ProviderBgSession.shares_background_conversation = True.run_bg_oneliner()callssessions.recycle_background()afterdestroy()only when its handle carries that marker. Runtime handles carry no marker, so they are unaffected.recycle_background(*, context_overflowed=False)forces a recycle on a non-runtime backend when the caller saw the refusal, or when the provider carries the markmark_context_overflowed()leaves. The forced branch is gated onnot _bg_backend_supports_runtime(), so kiro/kas/codex keep exactly their old criteria.stream_and_collect()marks the provider in itsAcpErrorbranch whenis_context_overflow_error()matches. History consolidation catches its own turn error beforebackground_turn()'s recycle runs, so the verdict has to ride on the provider.llm_helpers.is_context_overflow_error()returns true for the classifier'scontext_overflowtag, or for "Prompt is too long" / "context window overflowed" / "maximum context length". It needs the text match because the claude adapter puts its refusal in the JSON-RPCmessage, and the classifier only readsdata. A false positive costs one provider respawn.run_bg_oneliner()as a caller.Enumeration of the paths into the shared
_bgentry:run_bg_onelinerwith a provider-backed handle: fixed (recycle after every turn, forced on overflow).run_bg_onelinerwith a runtime handle (kiro/kas/codex): unchanged, because the session is ephemeral.background_turn(consolidation, otherstream_and_collectcallers): already recycled; now forced after a swallowed overflow on non-runtime backends.recycle_background()with the default, so it gets the threshold criteria plus the provider mark._hbkey, not touched.Backwards compatibility
Compatible:
recycle_background()gains a keyword-only argument with a default, and every existing caller and test fake keeps working. The only new behaviour is extra recycles on non-runtime backends, which spawn a fresh provider exactly as the existing criteria already do.Removes nothing: the change only adds a recycle trigger and a call site. No key, record, validator or public function is removed or narrowed.
Tests
test/test_bg_oneliner_recycle.py(15 cases, realSessionManagerwith a mock provider factory):claudereach the blind backstop and replace the provider; a single one-liner advancesprompt_count.AcpErrordoes not recycle.stream_and_collectleaves the overflow mark, so the followingrecycle_background()replaces the provider; the same 3% without the mark is kept.""(kiro),kasandcodex, a mark pluscontext_overflowed=Truedoes not recycle. A handle without the marker is not followed by anyrecycle_background()call.is_context_overflow_erroraccepts the classifier tag and the three spellings, and rejects otherAcpErrors and non-ACP exceptions.Each mutation reddens the named tests:
stream_and_collectmark: the swallowed-overflow case.Ran locally with
pytest -n0 test/test_bg_oneliner_recycle.pyagainst this worktree'ssrc. I ran no other suite; that is left to CI.Manual verification
N/A — the failure needs hours of real background traffic on a non-kiro backend to reach the window. The unit cases drive the real
SessionManagerrecycle path with the provider's actual refusal text, and I did not run it against a live claude or deepseek backend. macOS/Windows: unverified; the change has no platform-specific code.Related Issues
Closes #18230
Refs #15336
Pattern harvest
Rule candidate: review-prompt
Pattern: a handle over a SHARED persistent session whose
destroy()is a no-op means the caller owns the lifecycle check, and every acquire/drive/destroy helper over it must also run the recycle.Decision for the owner
The decision is whether a prompt-too-long refusal should force a
_bgrecycle on every backend or only on the shared-conversation (non-runtime) path. This draft scopes it to non-runtime backends so kiro, kas and codex are byte-for-byte unchanged, and leaves extending the forced recycle to kiro's persistent_bgentry as a follow-up if you want it.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.