Repository navigation
refactor(dashboard): hoist the chat turn's closures into chat_turn - #22057
Conversation
The 30 closures _run_chat defines become functions that take the turn (the per-turn state object), each moved into the chat_turn owner of its responsibility. A closure that read a local of _run_chat reads it off the turn, or as an alias bound from it at entry; its call sites pass the turn. Four owners are new: turn_entry (the live Stop signal and the entry state), turn_admission (the session binding a turn may run on), turn_reply_seams (what a turn writes as its reply) and turn_ledgers (what a turn records about itself). The rest join existing owners. The three helpers main added beside the previous step's owners move with them: stop_eager_spawns to eager_spawn, and the app-agent host gates to app_agent_binding. compose binds every moved name on the runner, so chat_runner.<name> still resolves and a patch of it still reaches the moved code. The guards that read a moved construct by path read its new home: the composition contract's owner set and homes, the base surface, the turn plumbing and shared-state inventories, the security posture non-egress registry, the redact-before-bound floor and the config-dir guard. Three source pins follow the closures to the turn spelling, and the two async-path guards (no config_dir() call, no inline mention resolve) read a function that takes the turn as they read a coroutine, the reach they had over the closures. Part of #20192
Sensitive Change ReviewSensitive files changed:
|
Sensitive change review
|
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Check that the existing copy-after-store rule in [DESIGN-REVIEWED] fa282d1 Description read[DESCRIPTION-READ] 9d8fc85b3e10d13e6941a0b2059daff15ecc4ebcbd69a44163e729b573bc54e6 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 (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Before merge, check that every place What this change shipsInventory (9 items) — 9 justifiedIntent: shrink
[FIRST-PRINCIPLES-REVIEWED] fa282d1 Description read[DESCRIPTION-READ] 990139f3bb6a2fb9ef33dc36bd6743eaccedc5ddb6bf9b2c1c8608dcfa736371 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.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: |
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: |
|
|
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Every review lane on the head is clean (only runner notices); held by the triage cron on the 20-file cap alone. Approved on the watch holder's request.
Problem / Motivation
Goal: Turn the dashboard chat runner's in-function closures into functions that take the turn, each in the
chat_turnowner of its responsibility, sochat_runner.pysheds another 1,000 lines with no change to what a turn does.src/kiro_crew/dashboard/chat_runner.pyis about 13,700 lines after the previous PR of this series, and almost all of it is one function,_run_chat. The campaign rule is that no hand-written file goes over 2,000 lines._run_chatdefines 31 helper closures inline (the live Stop signal, the reply flushes, the recovery ledgers, the owner check, the refusal fallback retry), and they hold the turn's state in closure cells. Nothing in that body can move until those helpers stop being closures.Why it matters
The next PRs of this series move
_run_chat's phases out by responsibility. A phase can only leave the function when the helpers it calls are reachable from outside it, which is what turning each closure into a function of the turn does. Keeping them as closures keeps the runner far over the cap and keeps every phase pinned inside it.Not a goal
_run_chat's phases stay where they are; they move in later PRs of the series._claim_sessionstays a closure until the next PR, which moves the phase that defines it.What changed (motivation → approach → change)
Thirty of
_run_chat's closures become functions whose first argument is the turn (turn_locals._ChatTurn), and each moves to the owner of its responsibility. A value the closure wrote is spelledturn.<name>at every site; a value it read that_run_chatrebinds is copied onto the turn right after each store and read off it. A call passes the turn (_flush_text_stream(turn)), a callback isfunctools.partial(<function>, turn), and an off-loop call passes it toasyncio.to_thread. The three helpers main added beside the previous PR's owners move with them.turn_entry_stop_pressed), the session's Stop count, the directive provenance a channel steer narrowsturn_admissionturn_reply_seamsturn_ledgersturn_context,turn_spec_hooks,slash_commands,model_fallback,acp_recovery_fire, the blocked-slash refusal, the refused-cycle record and refusal fallback retry, the binding-rebuild gateeager_spawn,app_agent_bindingstop_eager_spawns(#21552); the app-agent host gates_app_agent_host_allowed,_app_agent_turn_host_allowed(#21673)turn_entry(65 lines) andturn_admission(69 lines) are small here because they hold only the helpers so far; the next PR moves the turn's entry and admission phases into them.Repository guards that read a moved construct by path read its new home: the composition contract's owner set and homes, the base surface, the turn plumbing and shared-state inventories,
security_posture.py's non-egress registry for the three owners that now hold redactor calls (turn_reply_seams,turn_ledgers,model_fallback), the redact-before-bound floor and the config-dir guard. Three source pins follow the closures to the turn spelling, and the two async-path guards (noconfig_dir()call, no inline mention resolve) read a function that takes the turn as they read a coroutine, the reach they had over the closures (see Claim).Backwards compatibility
Compatible: every name
chat_runnerbound at the base is still bound, as the same object (the base-names contract test); every patch target still reaches the code it reached.The one public def that moved,
stop_eager_spawns, still resolves onchat_runner(imported there by name for its production importer and bound bycompose):Reader: src/kiro_crew/slack/gateway_runtime/exit_path.py:stop_eager_spawns -- release -- imports it from the runner at gateway shutdown; test_eager_spawn_shutdown.py
Tests
test_an_owner_function_never_rebinds_a_name_it_read_off_the_turn,test_the_closure_rule_reads_an_owner_functions_writes) and the convention pin (test_every_function_cut_from_run_chat_takes_the_turn_first,test_the_async_guards_reach_every_function_cut_from_run_chat,test_the_config_dir_guard_reads_a_function_that_takes_the_turn). Each new test passes 5 runs at-n0and one at-n 3.turnparameter, the entry aliases, theturn.<name>spellings, the(turn, ...)/partial/to_threadcall forms), is identical, and so is_run_chatitself apart from its_ChatTurn(...)line, which gains the 15 inputs the moved functions read.config_dir()planted in_queue_recoveryand in_stop_pressed, an inline mention resolve in a turn function, a renamedturnparameter, a guard that stops reading turn functions). The same plants with the guard change reverted stay green.chat_runner,chat_turnor a moved name, plus the guards that scan them: 30,984 passed (-n 3 --dist loadgroup). The only reds came from running in a git-less export (two line-cap checkout tests and the harness-paritygit ls-filestest), all of which pass in a checkout. CI runs the full suite.turn_entry100%,turn_admission100%,turn_reply_seams97%,turn_ledgers91%,turn_context95%,turn_spec_hooks93%,slash_commands96%,model_fallback94%,acp_recovery93%,eager_spawn92%,app_agent_binding91%,turn_locals99%; each clears the 80% floor on its own.src/kiro_crew: clean. semgrep 1.78 (CI configs, baseline at the base): 0 findings. black, isort, flake8, comment-history, brand, docs-lint, feature-map, harness-parity, agent-sdk boundary and line-cap gates: clean; the flake-pattern ratchet passes. Installed-mode smoke: a wheel built from the head imports the runner and the four new owners from the installed copy under an isolated HOME.Move audit:
chat_runner.py13,738 → 12,742 lines. The three helpers move unchanged;_run_chatshows as one changed unit because its closures leave it, which the closure-fidelity check above accounts for.Manual verification
N/A — a structural move; the turn-level characterization suite, the closure-fidelity check and the mutation-proven guards are the verification.
Related Issues
Part of #20192
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Move audit
_run_chatis the one changed unit: its 30 closures leave it, and the closure-fidelity check shows the rest of its body and every closure unchanged apart from the re-plumbing. The contract's changed units are its owner set and homes table.Claim
Granted edits outside the claim:
test/test_refusal_inband_notice.pyandtest/test_post_compaction_continuation.py: the(turn)spelling of_stop_pressed/_session_stop_generationand of_flush_text_stream, granted for this step (L30).test/test_no_config_dir_in_async.pyandtest/test_prompts.py: one-off grant for this PR, to restore BASE reach after hoisting_run_chatclosures. Theconfig_dir()scan reads a function whose first parameter isturn(_takes_the_turn) as it reads a coroutine, and lists the owners that hold one; the inline mention-resolve scan does the same. No other line changes.test/test_leaked_toolcall_notice.py: one-off grant for this PR, for the same reason (restore BASE reach after hoisting_run_chatclosures). The bare-reset sweep reads every function whose first parameter isturnas well as_run_chat; its assertion text is unchanged.src/kiro_crew/dashboard/chat_delivery.py,src/kiro_crew/dashboard/chat_utils_turn_recovery.py,test/test_stderr_redact_then_tail.pyandtest/test_crew_log_order.pyname where_queue_recovery,_refusal_fallback_retry,_fireand the steer cut live now.Baselined growth (L56 addendum 1, 1b):
src/kiro_crew/security_posture.py(line-cap baseline row 3,181) goes from 3,413 lines on main to 3,416, the threeNON_EGRESS_REDACTION_MODULESrows (1b); its reason comment keeps its seven lines.