Repository navigation
fix(chat): skip the resume prefetch a tool-search replay would refuse - #16160
Conversation
Design Review (Fable 5) — ✅ PASSDesign-level review of Design-Verdict: PASS Verify the claimed fresh-interpreter import-cycle check exists — this diff adds no test guarding the lazy [DESIGN-REVIEWED] 6e5c0e6 Description read[DESCRIPTION-READ] 5e2778805f617f36c8055645f7a6432a4b44c079cafaf688fdc36fc7bb00f5fc 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 (Fable 5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Verify the premise that the slot-focus prefetch always allocates channel-less: a dashboard-keyed slot with an inbound Slack link would otherwise lose a prefetch that could have succeeded — a latency cost only. What this change shipsInventory (6 items) — 6 justifiedIntent: FIX — stop the slot-focus resume prefetch from spawning a kiro-cli runtime the provider's tool-search replay guarantees will be refused and killed. Provenance: red-first test failing on base (quoted assertion), issue log quoted.
Inputs check out: [FIRST-PRINCIPLES-REVIEWED] 6e5c0e6 Description read[DESCRIPTION-READ] 169e912929fc02bbf03ab61b1013e7755bea844721e7cdfb73a0d0721bbc8420 That sha256 names the PR description this verdict read. Recompute it with |
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 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 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: |
9c3c0f4 to
9422734
Compare
With Tool Search on, the kiro backend answers a direct dashboard resume with a fresh native session plus conversation replay instead of session/load, so a speculative resume can only come back resumed=False and be refused -- after a full kiro-cli runtime was spawned, waited on and hard-killed with its whole process group. Every focus of a reopened chat paid that spawn and saved nothing, and the kill was logged as a leaked provider teardown, which reads as a leak. Extract the provider's decision into one pure function, resume_takes_tool_search_replay, read by the provider start once a resume sid is in hand and by _eager_spawn before any runtime exists. A prefetch the provider would replace with a replay is left to the first turn under its own log reason, "left to first turn (tool-search replay)". The first real turn keeps its fresh-session-plus-replay behaviour, the refusal path for every other case stays as it is, and a resume that stays native (Tool Search off, a non-kiro backend, a channel identity) still prefetches. Specs: providers.md (the predicate), learn-cron-dashboard.md (the slot_focused prefetch) and session.md (the get_or_create row). Fixes #15725
9422734 to
6e5c0e6
Compare
iamwhatever
left a comment
There was a problem hiding this comment.
Reviewed: moving the provider's replay decision into one pure predicate and asking it before spawning removes a spawn the provider was certain to kill. The prefetch reads the same tool_search value and backend selection the factory uses (config/loader.py:5120, :5208); only the focus-resume path is gated and fresh eager spawn is untouched. Tests check the skip log, the no-spawn outcome and both native-resume paths. Optional: a unit test for resume_takes_tool_search_replay with a channel id and a non-dashboard key.
Problem / Motivation
Goal: Focusing a reopened dashboard chat with Tool Search on starts no kiro-cli runtime that can only be killed again; the slot simply waits for its first turn, which starts exactly as it does today.
With
agent.tool_search: true(the default), the kiro backend andsession.eager_spawnon, focusing a reopened dashboard chat whose runtime is gone (after a gateway restart, or once the idle sweep retired it) runs the resume prefetch:_eager_spawnasksget_or_create(speculative=True, speculative_resume=True)for a speculativesession/load. But on this path the provider never loads --AcpProvider._start_kiro_runtime_implreplaces the nativesession/loadof a direct dashboard session with a fresh session plus conversation replay -- soclient.resumedis alwaysFalse, andsession_allocationraisesSpeculativeResumeRefusedafter the runtime is fully up, hard-killing the process tree it just spawned. The gateway log at INFO for one reopened chat (from the issue):About 6 s of startup and a 17-process tree per focus, saving nothing: the first real turn spawns its own runtime and starts its own fresh session anyway.
Why it matters
This is the default configuration, so every reopened dashboard chat pays it after every gateway restart. The doomed spawn also holds one of the host-derived prewarm admission slots and the eager-spawn concurrency semaphore for as long as it lives, so it can crowd out a prefetch that could have succeeded. And the kill is logged as
leaked provider teardown, which sends an operator hunting for a leak that does not exist.Not a goal
SLIM_RESUMEon the sametool_search_replaybranch (context.py). A separate defect; nothing incontext.pyis touched and this fix neither depends on nor assumes that one.speculative_resumeobservation semantics.leaked provider teardownkill reason of_sync_kill_providerfor a refused speculative spawn. Unchanged; with this fix the refused spawn simply stops happening on this path.What changed (motivation → approach → change)
Symptom: a reopened chat's focus spawns a full runtime and kills it. Root cause:
_eager_spawndecides to prefetch from the in-memory resumable hint alone, while the provider decides -- later, with the runtime already up -- that this resume is a tool-search replay, whichsession_allocationcan only refuse. Two sites, one decision, and the expensive one ran first.Change, ONE predicate:
src/kiro_crew/agent_sdk/tool_search.py: the provider's inline condition (Tool Search on,backend == ACP_BACKEND_KIRO, no channel identity,telemetry_channel_of(session_key) == "dashboard") is extracted into the pure module-level functionresume_takes_tool_search_replay(tool_search, backend, channel_id, session_key), hosted in the module that already resolvesagent.tool_search*and documents what Tool Search does on both kiro engines -- below both callers, because the dashboard may not import the ACP layer (scripts/check_agent_sdk_boundary.py, see Review lanes). Its onemessaging.linkimport is function-level: at module level it is an import cycle (kiro_crew.messaging→ its driver →kiro_crew.acp→acp.runtime→ this module), named in a comment and checked in fresh interpreters.src/kiro_crew/providers/acp.py:_start_kiro_runtime_implimports that function and calls it once a resume sid is in hand (if resume_sid and resume_takes_tool_search_replay(...)) and does exactly what it did before. Whether a resume is attempted at all (a persisted sid, persistent memory) stays the caller's precondition; the function answers only which shape the resume takes.src/kiro_crew/dashboard/chat_runner.py:_eager_spawnasks the same function (imported fromkiro_crew.agent_sdk.tool_search, so the file's ACP-layer import count stays at its baselined 3) before any runtime exists -- after the binding and memory-store "left to first turn" guards, before the default-model resolve, the selection write, admission and the spawn -- withtool_search=cfg.agent.tool_search, the backendselect_provider_backend(session_key, cfg.agent.member_acp_backend, cfg.agent.acp_backend)resolves for this key (the factory's own selection, not a second copy of it),channel_id=None(the prefetch allocates with no channel identity) and the slot's effective session key. When it answers replay, the slot is left to its first turn under one INFO line in the existing style,Eager spawn: <key> left to first turn (tool-search replay), and nothing is spawned. Only theallow_resume(slot-focused) path is gated; fresh eager spawn from the create/project/agent signals is untouched.docs/system-specs/modules/providers.md(Native-resume compatibility names the predicate and the prefetch that reads it),docs/system-specs/modules/learn-cron-dashboard.md(theslot_focusedresume-prefetch paragraph),docs/system-specs/modules/session.md(theget_or_createrow'sspeculative_resumecontract).Not a product-shape change: the user-visible outcome is identical -- the first message on a reopened chat cold-starts a fresh session with the conversation replayed, as it does today. What disappears is the wasted spawn, its kill and the misleading log line. The resume prefetch's latency win was never available on this path (the load it would have overlapped is the one the provider replaces), so nothing that worked is lost; with Tool Search off, or on a non-kiro backend, or for a channel-linked slot, the speculative
session/loadruns exactly as before.Backwards compatibility
Compatible: no config key, API, wire message or persisted format changes.
resume_takes_tool_search_replayis a new module-level function inkiro_crew.agent_sdk.tool_search; the provider branch that used the inline condition evaluates the same condition through it. No caller passes anything new toget_or_create.Tests
Red-first,
-n0.test/test_eager_spawn.py::TestToolSearchReplayPrefetchGatewas written before the fix and run against the unfixed code; the failing assertion:(the two native-path cases of the class already passed, as they must: without the gate the load is attempted.) After the fix the class passes, and the whole module is green: 110 passed.
What the tests lock in:
test_replayed_resume_spawns_nothing: with the default config (Tool Search on, kiro backend) and a dashboard key, the slot-focused eager spawn awaits noget_or_createand logs exactly oneleft to first turnreason,Eager spawn: dashboard:t1 left to first turn (tool-search replay).test_native_resume_still_prefetches[tool-search-off]and[non-kiro-backend](table-driven): withagent.tool_search=False, or withagent.acp_backend=ACP_BACKEND_KAS, the speculative resume still reachesget_or_create(speculative_resume=True).test_allow_resume_passes_speculative_resume,test_resumed_prefetch_arms_ttl,test_fresh_spawn_does_not_arm_ttl,test_failed_speculative_load_leaves_nothing_behind) move to a_native_resume_cfg()helper that turns Tool Search off, so they keep exercising the load path they describe instead of passing vacuously behind the new gate. Every other test in the module is unchanged.Provider and session lane (
-n0, each file named):test/test_acp_provider.py test/test_acp_tool_search_wire.py test/test_acp_backend_kas.py test/test_session.py test/metrics/test_startup_metric.py test/test_eager_spawn_start_backoff.py-- 586 passed, 5 skipped. The provider's tool-search replay branch andresume_outcomemetric are pinned there and unchanged.Gates from the worktree:
python3 scripts/check_black_formatting.py,python3 scripts/check_subprocess_encoding.py,isort src/kiro_crew test,flake8 src/kiro_crew test,mypy src/kiro_crew(2053 source files, no issues),COMMENT_HISTORY_BASE_REF=<merge-base> python3 scripts/check_comment_history.py(passed: no history narration on added lines),HARNESS_BASE_REF=origin/main python3 scripts/check_harness_parity.py(Kiro identity tested positively on the added lines),bash scripts/docs-lint.sh(all documentation checks passed).black --target-version py310ran only on the three touched files, none of which is in.github/black-baseline.txt.scripts/local-gate.py(backend related set: 732 test files in 2 batches; host headroom 68-69 GB read before every lane, so no lane was skipped for memory):test_auto_improvement_members.py::test_assignments_build_real_context_from_only_their_private_store(_ManagedEssentialSourceError: this host's scratch root sits under the data home) andtest_model_served_default.py::TestClientInheritExits::test_withheld_pin_still_lands_on_a_served_model(asession/set_modelexpectation shaped by this host's own kiro state) fail identically on a pristine detached worktree of the base6916ba17f0with the same interpreter;test_issue_radar_crew_runtime.py::TestSession::test_wake_runs_a_turn_carrying_the_brief_and_the_nudge(CrewLedgerNotRecorded) is an order-dependent flake under xdist that passes alone and with its whole file (160 passed) under-n0, and imports nothing this diff changes.pip install -e ".[dev]"-- which resolves the unpinned setup.cfg extra (pytest 9.1.1, pytest-asyncio 1.4.0) -- failed 52test_acp_runtime.pyKAS-readiness cases withTimeoutError(the runtime's reader loop lands on a different event loop). On the pinned dev group (pip install --group dev: pytest 9.0.3, pytest-asyncio 0.20.3), which is what CI runs, that file is 497/497 green here, in isolation and inside batch 1 above.Frontend lane: not run here. The diff touches no
website/**file; the 359 "related" frontend files are the cross-surface list plus stem matches on the changed paths, none referencing a changed file by name, and this fresh worktree has nonode_modules. CI runs the frontend suite regardless.Manual verification
N/A -- unit coverage sufficient: the gate is a pure predicate over the loaded config and the session key, pinned at both callers by the tests above, and the log reason is asserted verbatim. No live gateway was driven in this round.
Related Issues
Fixes #15725
Related: #2386 (resume prefetch), #9762 (the Tool Search fresh-session path), #10245 (process-group kill of a refused speculative session, unchanged), #16059 (the dropped replay on the same branch, out of scope here).
Pattern harvest
Rule candidate: review-prompt
Pattern: a speculative or prefetch path decides to start expensive work without reading the predicate the real path uses to decide the work's outcome, so it attempts something whose refusal is already decidable before the expensive step -- "the cheap path and the expensive path read ONE predicate, and the cheap one asks first" is checkable wherever a
speculative=/ prefetch call sits in front of a provider or allocator that can refuse.Review lanes
Round 2 (head after
9c3c0f433632):Backend Lint & Type Check (3.12), step "Check agent-SDK import boundary":scripts/check_agent_sdk_boundary.pyrefused the round-1 head withsrc/kiro_crew/dashboard/chat_runner.py: ACP-layer imports grew from 3 to 4. The baseline is shrink-only.-- the newfrom kiro_crew.providers.acp import resume_takes_tool_search_replaywas a fourthkiro_crew.providers*import in a file.github/agent-sdk-boundary-baseline.txtpins at three, and the gate has no opt-out marker by design. Opus 5's one FINDING (chat_runner.py:345) is the same defect. Fixed by moving the predicate, not by raising the baseline: it now lives insrc/kiro_crew/agent_sdk/tool_search.py, the module that already resolvesagent.tool_search*and sits in the import surface the gate lets application code reach;providers/acp.py(which already imported that module) andchat_runner.pyboth import it from there, so there is still exactly one definition andchat_runner.pyis back at three ACP-layer imports. Red first: the gate script fails on the round-1 tree with the line above and passes on this head. One consequence of the new home: a module-level import ofmessaging.link.telemetry_channel_ofthere is a cycle (kiro_crew.messagingimports its driver, which importskiro_crew.acp, whose runtime importsagent_sdk.tool_search), proven in a fresh interpreter; that import is function-level with the cycle named in a comment, andimport kiro_crew.agent_sdk.tool_searchnow succeeds first, afterkiro_crew.messaging, afterkiro_crew.acp.typesand afterkiro_crew.agent_sdk.backends.Backend Tests (Windows) (4):test/test_prompt_override_encoding.py::TestUtf16OverrideDegradesToShippedPrompt::test_unreadable_override_still_degrades_to_shipped_prompt(up to 13vsup to 14: two renders of a host-dependent sub-agent cap) is tests: prompt override degrade tests compare two renders of a host-dependent sub-agent cap (Windows main red) #16109; the file is untouched by this diff and the shard re-runs with this push.9c3c0f433632.26ae7429b4(main's tip; its five commits since6916ba17f0-- fix(crew-log): ignore a segment name whose number is not plain ascii #15961, test: touch the crew log off the event loop in every async test #16102, feat(decisions): add noul and score question types #16014, fix(decisions): keep a pinned decisions deny covering local models #16056, fix(decisions): resume the local model under the provider-switch lock #16090 -- touch no file of this diff). The diff is byte-identical to the pre-rebase candidate and the commit message is unchanged; the boundary, comment-history andpush_guardgates and the named-n0tests were re-run on the rebased tree.Gates on this head:
python3 scripts/check_agent_sdk_boundary.pypasses (chat_runner.pyback at three edges; the same script fails on the round-1 tree with the line quoted above);COMMENT_HISTORY_BASE_REF=origin/main python3 scripts/check_comment_history.pypassed;push_guard.py --base main --require-single-on-baseSAFE (single commit on26ae7429b4); black ratchet,black --checkon the three touched Python files (none baselined), isort and flake8 clean;python3 scripts/docs_lint.pyall checks passed; mypy over the three touched modules (imports followed) no issues. Named-n0:test/test_eager_spawn.py110 passed;test/test_acp_provider.py+test/test_providers_base_cov80.py101 passed;test/test_acp_mandatory_mcps.py+test/test_acp_tool_search_wire.py+test/test_kas_spawn.py108 passed, 5 skipped.scripts/local-gate.pybackend related set (736 files): batch 1 (734 files) 41,950 passed, 140 skipped, 2 failed; batch 2 (2 files) 6 passed. The two are the same host-environment failures recorded for round 1 under Tests --test_auto_improvement_members.py::test_assignments_build_real_context_from_only_their_private_store(_ManagedEssentialSourceError: this host's pytest scratch root sits under the data home) andtest_model_served_default.py::TestClientInheritExits::test_withheld_pin_still_lands_on_a_served_model(asession/set_modelexpectation shaped by this host's served-model state) -- in files this diff does not touch; they reproduce alone on this head, reproduced identically on the base in round 1, and both passed in every Linux shard of the round-1 CI run. Frontend lane not run here (nowebsite/**change, nonode_modulesin this worktree); CI runs it.Round 3 (head after
94227347a771):af162847d6(fix(acp): let Crew's own MCP servers defer under Tool Search again #16131, "let Crew's own MCP servers defer under Tool Search again") landed on main: it movesMANDATORY_MCPS_ENVandmandatory_mcps_env_valueout ofsrc/kiro_crew/agent_sdk/tool_search.py, the module this PR's predicate lives in since round 2. The one conflict was that module's__all__list, where this PR's"resume_takes_tool_search_replay"entry sat next to the removed"mandatory_mcps_env_value"; resolved by keeping only this PR's entry (theIterableimport fix(acp): let Crew's own MCP servers defer under Tool Search again #16131 dropped auto-merged, and the predicate uses neither name). Semantics checked before resolving: fix(acp): let Crew's own MCP servers defer under Tool Search again #16131 changes which of Crew's own MCP servers stay out of Tool Search deferral and under which kiro-cli version (the harness spawn-env hook andASBX_KIRO_MANDATORY_MCPS); it touches neitherproviders/acp.py, nor the resume path, norspec_grants_tool_search, nor the backend id, nor any input ofresume_takes_tool_search_replay, so the predicate's condition is unchanged.af162847d6(main's tip; 64 commits since26ae7429b4, nine of them touching a file of this diff: fix(acp): let Crew's own MCP servers defer under Tool Search again #16131, fix: resolve each cron agent_sequence step as its own crew (#15139) #16047, feat(onboarding): default to Kiro CLI, offer other coding agents #13888, feat(settings): move explanatory help behind an info tip #15823, fix(acp): bound the shared-runtime request write on reader progress #15905, fix(subagent): show gate-deferred spawns as queued and hold synthesis #16012, feat: add crew mode toggle to chat sessions #16088, refactor(dashboard): compose chat persistence from slot owners #16104, fix: compare session pid signatures as bytes #16153 -- all absorbed, with the single__all__conflict above). Every+/-line of the diff is identical to the round-2 head (only context lines and hunk offsets moved) and the commit message is unchanged.Gates on this head:
python3 scripts/check_agent_sdk_boundary.pypasses (chat_runner.pystill at three ACP-layer edges);COMMENT_HISTORY_BASE_REF=origin/main python3 scripts/check_comment_history.pypassed;push_guard.py --base main --require-single-on-baseSAFE (single commit onaf162847d6); black ratchet, isort and flake8 clean on the touched files; mypy over the three touched modules (imports followed) no issues;import kiro_crew.agent_sdk.tool_searchstill succeeds first, afterkiro_crew.messaging.link, afterkiro_crew.providers.acpand afterkiro_crew.dashboard.chat_runner. Named-n0:test/test_eager_spawn.py110 passed;test/test_acp_provider.py+test/test_providers_base_cov80.py+test/test_acp_tool_search_wire.py+test/test_kas_spawn.py+test/test_acp_tool_search_spawn_env.py(the file #16131 added in place oftest_acp_mandatory_mcps.py) 243 passed, 5 skipped.scripts/local-gate.pybackend related set (737 files): batch 1 (666 files) 43,278 passed, 140 skipped, 2 failed; batch 2 (71 files) 3,766 passed, 20 skipped. The two are the same host-environment failures recorded for rounds 1 and 2 (test_auto_improvement_members.py::test_assignments_build_real_context_from_only_their_private_store,test_model_served_default.py::TestClientInheritExits::test_withheld_pin_still_lands_on_a_served_model), in files this diff does not touch; they reproduce alone on this head and both passed in every Linux shard of the round-2 CI run. Frontend lane not run here (nowebsite/**change); CI runs it.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement