Repository navigation
refactor(dashboard): give each chat turn one state object - #20526
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Check that the plumbing guard catches every way a mirrored local can be rebound (augmented, tuple, walrus, [DESIGN-REVIEWED] b7afca5 Description read[DESCRIPTION-READ] 21a5c2940be31a400507da08ff187bf6b256fe72edb526ce24dab9c3544e4134 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) — 🟡 CONCERNSPremise-level review of First-Principles-Verdict: CONCERNS 61 Not justified as shipped
What this change shipsInventory (6 items) — 4 justifiedIntent: prepare
[FIRST-PRINCIPLES-REVIEWED] b7afca5 Description read[DESCRIPTION-READ] 2cc547e7b6127592ebf2b4c2e399ce018b45751ec20088727e3a98a6b42a33d5 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: |
mbriones98
left a comment
There was a problem hiding this comment.
Checks green and readiness passed at 9bb72ba; no behavior change.
_run_chat's nested functions shared its state through nonlocal closure cells, so no block could leave the function without losing the cell, and every chat_turn owner name had to be re-exported from the runner by hand. _run_chat now builds one turn_locals._ChatTurn as its first statement. A name a closure writes is spelled turn.<name> at every site; a name a closure reads after the turn rebinds it is copied onto the turn right after each store and read off it inside the closure. A nested function stays a closure cell, so the turn holds no closure and an ended turn is still freed by reference counting. An unset name raises UnboundLocalError, as the unbound local did. chat_turn.compose finds the package's modules and binds each owner name on the runner unless the runner already holds that object; a different object under one name fails the import. Patches of chat_runner.<name> keep reaching every owner function. The composition contract maps each path-guarded construct and task entry to its home file. Tests that read the turn's code as text read every chat_turn module and then the runner as one text, so a construct they pin may live in either. Two new tests pin the binding and the turn's plumbing, each with self-tests on synthetic source, and a characterization row pins that the turn object dies without the collector. Part of #20192
9bb72ba to
b7afca5
Compare
|
|
|
Rebased onto main c64afca (the PR was DIRTY): main's #17729 added |
|
|
|
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Approved by a maintainer who read the review lane notes and accepted them as follow-up work.
|
Post-merge audit of head b7afca5 (merged while I was reviewing): correct. Reviewed as a change to how a turn holds its state, not as a move, and I found no change in what a turn does. What I checked, independently of the PR's own guards:
Non-blocking: since the package directory is now the owner list, a stray module left in I have nothing to add to the review lanes. The First Principles concerns are about scope and were answered on the PR; they do not affect correctness. |
Eight owner packages each carried a private copy of compose/_functions_of/ _rebind (slack.gateway_runtime, chat_api, messaging_api, file_api, agent_admin, server_runtime, hook_runtime, slack.handler_runtime), kept byte-identical only by contract-test pins. Hoist one compose(namespace, owners, host=None) into the neutral kiro_crew/_facade_compose.py, which no facade owns, so the per-package import guards still hold. With host=None it runs the host-less copies' code; with a host class it also swaps and requalifies that class's owner methods, as the Slack gateway's copy did. The eight facades call it once at their foot; the package __init__ files keep only their docstrings. dashboard.chat_turn keeps its own compose: since #20526 it also finds its owners (owner_modules()) and binds on chat_runner every name an owner defines that the runner lacks. That contract is the runner's alone, so chat_runner and its tests are unchanged and only docstrings note it. The byte-identity pins become "the facade composes through the shared helper and the package defines no function"; the synthetic-module probes call the shared helper. Pure refactor: no behaviour change. Closes #16231
Eight owner packages each carried a private copy of compose/_functions_of/ _rebind (slack.gateway_runtime, chat_api, messaging_api, file_api, agent_admin, server_runtime, hook_runtime, slack.handler_runtime), kept byte-identical only by contract-test pins. Hoist one compose(namespace, owners, host=None) into the neutral kiro_crew/_facade_compose.py, which no facade owns, so the per-package import guards still hold. With host=None it runs the host-less copies' code; with a host class it also swaps and requalifies that class's owner methods, as the Slack gateway's copy did. The eight facades call it once at their foot; the package __init__ files keep only their docstrings. dashboard.chat_turn keeps its own compose: since #20526 it also finds its owners (owner_modules()) and binds on chat_runner every name an owner defines that the runner lacks. That contract is the runner's alone, so chat_runner and its tests are unchanged and only docstrings note it. The byte-identity pins become "the facade composes through the shared helper and the package defines no function"; the synthetic-module probes call the shared helper. Pure refactor: no behaviour change. Refs #16231
Eight owner packages each carried a private copy of compose/_functions_of/ _rebind (slack.gateway_runtime, chat_api, messaging_api, file_api, agent_admin, server_runtime, hook_runtime, slack.handler_runtime), kept byte-identical only by contract-test pins. Hoist one compose(namespace, owners, host=None) into the neutral kiro_crew/_facade_compose.py, which no facade owns, so the per-package import guards still hold. With host=None it runs the host-less copies' code; with a host class it also swaps and requalifies that class's owner methods, as the Slack gateway's copy did. The eight facades call it once at their foot; the package __init__ files keep only their docstrings. dashboard.chat_turn keeps its own compose: since #20526 it also finds its owners (owner_modules()) and binds on chat_runner every name an owner defines that the runner lacks. That contract is the runner's alone, so chat_runner and its tests are unchanged and only docstrings note it. The byte-identity pins become "the facade composes through the shared helper and the package defines no function"; the synthetic-module probes call the shared helper. Pure refactor: no behaviour change. Refs #16231
Eight owner packages each carried a private copy of compose/_functions_of/ _rebind (slack.gateway_runtime, chat_api, messaging_api, file_api, agent_admin, server_runtime, hook_runtime, slack.handler_runtime), kept byte-identical only by contract-test pins. Hoist one compose(namespace, owners, host=None) into the neutral kiro_crew/_facade_compose.py, which no facade owns, so the per-package import guards still hold. With host=None it runs the host-less copies' code; with a host class it also swaps and requalifies that class's owner methods, as the Slack gateway's copy did. The eight facades call it once at their foot; the package __init__ files keep only their docstrings. dashboard.chat_turn keeps its own compose: since #20526 it also finds its owners (owner_modules()) and binds on chat_runner every name an owner defines that the runner lacks. That contract is the runner's alone, so chat_runner and its tests are unchanged and only docstrings note it. The byte-identity pins become "the facade composes through the shared helper and the package defines no function"; the synthetic-module probes call the shared helper. Pure refactor: no behaviour change. Refs #16231
Nine owner packages each carried a private copy of compose/_functions_of/ _rebind (slack.gateway_runtime, chat_api, messaging_api, file_api, agent_admin, server_runtime, hook_runtime, slack.handler_runtime, subagent_parts), kept identical only by contract-test pins. Hoist one compose(namespace, owners, host=None) into the neutral kiro_crew/_facade_compose.py, which no facade owns, so the per-package import guards still hold. With host=None it runs the host-less copies' code; with a host class it also swaps and requalifies that class's owner methods, as the Slack gateway's and subagent's copies did. The nine facades call it once at their foot; the package __init__ files keep only their docstrings. Its rebind is public: hooks.py uses it for the one function compose cannot reach, the generator under ScriptHookStore._atomic_mutation. dashboard.chat_turn keeps its own compose: since #20526 it also finds its owners (owner_modules()) and binds on chat_runner every name an owner defines that the runner lacks. That contract is the runner's alone, so chat_runner and its tests are unchanged and only docstrings note it. The byte-identity pins become "the facade composes through the shared helper and the package defines no function"; the synthetic-module probes call the shared helper. Pure refactor: no behaviour change. Refs #16231
Nine owner packages each carried a private copy of compose/_functions_of/ _rebind (slack.gateway_runtime, chat_api, messaging_api, file_api, agent_admin, server_runtime, hook_runtime, slack.handler_runtime, subagent_parts), kept identical only by contract-test pins. Hoist one compose(namespace, owners, host=None) into the neutral kiro_crew/_facade_compose.py, which no facade owns, so the per-package import guards still hold. With host=None it runs the host-less copies' code; with a host class it also swaps and requalifies that class's owner methods, as the Slack gateway's and subagent's copies did. The nine facades call it once at their foot; the package __init__ files keep only their docstrings. Its rebind is public: hooks.py uses it for the one function compose cannot reach, the generator under ScriptHookStore._atomic_mutation. dashboard.chat_turn keeps its own compose: since #20526 it also finds its owners (owner_modules()) and binds on chat_runner every name an owner defines that the runner lacks. That contract is the runner's alone, so chat_runner and its tests are unchanged and only docstrings note it. The byte-identity pins become "the facade composes through the shared helper and the package defines no function"; the synthetic-module probes call the shared helper. Pure refactor: no behaviour change. Refs #16231 Co-authored-by: w <w@k.local>
Problem / Motivation
Goal: Give a dashboard chat turn one shared state object and let
chat_runner.pycompose itschat_turn/owners by discovery, so the next PRs can move_run_chatcode into owners without changing a turn's behaviour or touching the tests that read it.src/kiro_crew/dashboard/chat_runner.pyis 20,849 lines and_run_chatalone is 12,062; the campaign rule is that no hand-written file goes over 2,000 lines. Three things stop code from leaving_run_chattoday. Its nested functions share state throughnonlocalclosure cells, so a block that moves to another module loses the cell. Every owner name has to be re-exported from the runner by hand, so the runner cannot shrink. And about fifty tests read the runner's text by path, so a construct that moves goes out of their sight.Why it matters
Every dashboard, Slack and channel turn runs through
_run_chat. Without a turn object, a moved phase would read a stale copy of a value a closure rewrote, for example the reply text the steer cut clears. Without discovery, every new owner grows the runner by its re-exports. Without the union read, a guard that reads the runner by path would pass vacuously once its construct moves. This PR lays all three foundations while moving nothing out of_run_chat.Not a goal
_run_chatin this PR; phases move in the next PRs of the series.test_dashboard_chat.py,test_prompts.py, spec_buildertest_routes.py); their deltas are relayed to those scopes, and they are re-pointed here once those scopes land.What changed (motivation → approach → change)
_run_chatnow builds oneturn_locals._ChatTurn(message=message)as its first statement. A value a closure writes (assistant_text, which the steer cut writes from the steer handler's task, the consumption reports, the usage-row latch) is spelledturn.<name>at every site, so every reader shares one cell, as the closure cell did. A value a closure reads after the turn rebinds it (message,client, the crew-log ids, the per-turn stats, the turn's project snapshot_turn_project) is copied onto the turn right after each store and read off it inside the closure. A nested function stays a closure cell, so the turn holds no closure and an ended turn is freed by reference counting, as before. Reading an attribute the turn has not set raisesUnboundLocalError(aNameError), as the unbound local did.chat_turn.composenow finds the package's modules withpkgutiland binds each owner name on the runner unless the runner already holds the same object; a different object under one name fails the import. A patch ofchat_runner.<name>still reaches every owner function, and the 13 per-owner aliases and the hand-kept owner tuple are gone.chat_runner.pyturn.xat every closure-written site, copies after each late-bound store, thenonlocallines gone); compose by discovery; two comments restatedchat_turn/__init__.py,chat_turn/turn_locals.pyowner_modules()(already-imported modules first), the binding with its collision check;_ChatTurn(the one input its closures read off it,message, and 36 values its closures share)test_chat_runner_composition_contract.py_STAYS_IN_THE_RUNNERbecomes_GUARDED_CONSTRUCT_HOMES({pattern: homes});_RUNNER_FILE_DEFSbecomes_TASK_ENTRY_HOMES({entry: home}) plus a stall-attribution reach testtest_chat_runner_compose_binding.py(binding, collisions, owner set, every runner import is read);test_chat_runner_turn_plumbing.py(every store of a name read off the turn is copied at once, every binding form counted; a closure-written name is never bare; no closure declaresnonlocal; an unset name raisesUnboundLocalError)test_chat_runner_turn_shared_state.pychat_turn/module, then the runner, as one text (budget-neutral); two use(?:turn\.)?assistant_text, including the reply-text reset sweep's AST check; recovery_l1 checks both context-build arms and both suppression paymentslearn-cron-dashboard.md,test_source_pin_budget.pyBackwards compatibility
Compatible: every name
chat_runnerbound at the base is still bound, as the same object (the base-names contract test), and every patch target still reaches the code it reached.Removes nothing: no public def, route, key or file leaves; the runner's per-owner module aliases (
_owner_*, private) are replaced bycomposediscovery.Tests
test_chat_runner_turn_shared_state.py.test_a_construct_the_runner_guards_count_stays_in_the_runner[...]→test_a_construct_guards_count_lives_only_in_its_homes[...];test_a_task_entry_stays_defined_in_the_runner_file[...]→test_a_task_entry_is_defined_in_its_home_file[...]; newtest_a_stalled_task_entry_reads_as_a_dashboard_chat_turn[...].nonlocal, and the runner's own suppression payment.test_context_trace.py:294, :399; metrics/test_turn_profile.py:478; metrics/test_usage_turns.py:320;test_autonudge_banner.py:1472;test_channel_connect_row.py:276;test_chat_hooks.py:131;test_chat_runner_early_recovery_actor.py:164;test_chat_turn_timeout_consistency.py:223;test_credential_redaction_switch.py:268;test_credential_sources.py:222;test_crew_log_emit.py:823/824, :2888, :3218, :6178, :6218;test_crew_log_order.py:275;test_dashboard_approval.py:2341, :2675;test_decisions_compaction_hook.py:442;test_deny_audit_first.py:352;test_external_logout_detection_baseline.py(the turn-gate pin);test_leaked_toolcall_notice.py:445, :507;test_mcp_session_report.py:452;test_member_turn_context.py:304;test_post_compaction_continuation.py:387, :446, :604;test_promise_only_autoapprove_parity.py:60;test_recovery_l1_chat_runner.py:246;test_redaction_blocked_links.py:525;test_refusal_inband_notice.py:676, :766;test_runloop_integration.py:1572;test_runtime_death_is_a_process_event.py:398, :758, :892;test_session_control.py:2882;test_session_control_set_model.py:383/432, :450;test_start_priority.py:907;test_stderr_redact_then_tail.py:408;test_steer_requeue.py:468;test_subagent_stop_reason_consistency.py:1985;test_turn_duration_recorded.py:169;test_turn_metric_all_surfaces.py:231, :328;test_wake_judge_feedback.py:1955;test_ws_event_scoping_frames.py(the approval_resolved scan's roots).chat_runnerorchat_turn, plus the flake, wait, line-cap and base-names guards: 20,177 passed, 0 failed (-n 3 --dist loadgroup).test_chat_runner_turn_*.py), which pins whole turns, passes unchanged except for the added turn-object row.-n0green.src/kiro_crew: clean. semgrep 1.78 (CI configs, baseline at the base): 0 findings, 0 parse errors (per-rule timeouts on the 20,955-line runner). black, isort, flake8 (no per-file ignore), comment-history, brand, agent-sdk, subprocess-encoding and sync-io gates: clean. Installed-mode import of the built wheel: every owner,turn_localsincluded, imports.Line cap (
scripts/check_file_line_cap.py): 0 failures.chat_runner.pygrows by 33 lines (20,922 → 20,955; the copies onto the turn); the next PRs of the series move about 2,400 lines out of it. The union reads grow seven already-baselined test files by 7 to 41 lines each (reported, not failed);test_subagent_stop_reason_consistency.pyuses a three-line form of the read to stay at 2,000 lines. Every file's default and--minimaldiffs add the same lines exceptdocs/system-specs/modules/learn-cron-dashboard.md: 48 added and 14 removed under the default diff, 47 and 13 under--minimal. The difference is one blank line before "What stays inchat_runner.py", shown as removed and re-added; it carries no gate token.Manual verification
N/A — a structural refactor; the turn-level characterization suite and the mutation-proven source pins are the verification.
Related Issues
Part of #20192
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Move audit
N/A for moves: nothing leaves
_run_chatin this PR. The move audit's changed units are_run_chat(the S1 re-plumbing only) and thecomposecall at the end of the file. Recipe: every change is produced by a script and re-applies from main byte-for-byte.