Skip to content

fix(dashboard): name a stopped turn's own opener in its restore - #19959

Merged
iamwhatever merged 1 commit into
mainfrom
fix/stop-restore-skips-steer-rows
Nov 1, 2026
Merged

iamwhatever merged 1 commit into
mainfrom
fix/stop-restore-skips-steer-rows

Conversation

@javenciu

@javenciu javenciu commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: After a Stop on a turn the user steered, the next prompt's cancelled-turn restore names the request that opened the turn and keeps the steer, in order, with the partial reply, instead of naming the steer as the request.

  • cancelled_turn_preamble (src/kiro_crew/context_assembly/replay.py:538 on 95abd91) walks back from the stop_event row and takes the first user row, other than a steer the person took back, as the cancelled request (src/kiro_crew/context_assembly/replay.py:576-580 on 95abd91), then collects the assistant rows after it (src/kiro_crew/context_assembly/replay.py:589 on 95abd91).
  • A mid-turn steer is persisted as a user row with meta.steer (src/kiro_crew/dashboard/chat_delivery.py:1025 on 95abd91, in steer_into_running_turn). It sits between the row that opened the turn and the rest of the reply, so the walk stops at it.
  • For a request R, a partial reply A1, a steer S and a partial reply A2, followed by Stop, the restore reads Cancelled user request: S and Partial assistant response before cancel: A2. R and A1 are not in it.
  • The reader half of this fix is already on main. feat(chat): take back a steer the running turn has not read yet #23388 made build_cancelled_turn_preamble read whole rows, so meta reaches the walk, drop warm-task-brief rows, and run in a worker thread in both callers (src/kiro_crew/context.py:1154-1157, src/kiro_crew/dashboard/chat_turn/prompt_assembly.py:706 and src/kiro_crew/slack/handler_runtime/turn.py:745, all on 95abd91). The walk uses that meta only to step past a steer the person took back (replay.py:578), so a consumed, written or requeued steer is still named as the request. This PR now carries the opener fix only.

Why it matters

kiro-cli drops a cancelled turn from its own log, so this restore is all the model has of the turn that was stopped (docs/architecture/design-notes/soft-stop.md, "Context restore after a soft cancel"). After a Stop on a steered turn the next reply is handed the interruption as the whole request, and the request the user actually made and the work already done on it are missing.

The web client already holds that a steer does not open a turn: opensTurn returns !row.meta?.steer for a user row (website/src/pages/chat/RecoveryCard.tsx:456-457), and #9594 fixed the same class for the pinned banner. This builder did not apply the rule.

Not a goal

  • build_interrupted_turn_preamble (src/kiro_crew/context_assembly/replay.py:178), the restore behind Resume, walked back to the newest opener the same way; fix(context): skip mid-turn steers when Resume restores the interrupted request #20467 has since fixed it on main (Resume restore should skip mid-turn steers when finding the interrupted request #20269). It has a different trigger, its own caller and its own tests. Its opener walk stays as main has it, and its partial-output loop moves into _partial_turn_output unchanged, so both restores read the partial output by one rule.
  • An inject row that did not open the turn (a recovery or user_replay row from a Resume press) between the opener and the steer. The walk stops at any inject row, because whether one opens a turn depends on its injectKind and this function is not handed TURN_OPENING_INJECT_KINDS. A steer after a Resume press keeps today's result, the steer as the request. Passing the kinds through both callers is a separate change.
  • Anything else in the restore: the markers, both caps, the stop_event lookup, the fall back to the latest user entry, the row read and the worker-thread calls (feat(chat): take back a steer the running turn has not read yet #23388) are unchanged.

What changed (motivation → approach → change)

  • Symptom: request, partial reply, steer, partial reply, Stop. The restore names the steer as the request and drops the first two.
  • Root cause: the walk treats the newest user row as the row that opened the turn, and a steer is a user row that did not. Main's reader (feat(chat): take back a steer the running turn has not read yet #23388) already hands it whole rows with meta, so it can tell them apart, and it does not.
  • Change in src/kiro_crew/context_assembly/replay.py, the only src/ file this PR changes:
    • _is_steer_row (:100, on main since fix(context): skip mid-turn steers when Resume restores the interrupted request #20467) is true for a user or nudge row whose meta.steer is True, the value chat_delivery.py:1025 stores.
    • In cancelled_turn_preamble, when the newest user row before the marker is a steer, tested with _is_steer_row, the search continues back to the nearest row that could have opened a turn (src/kiro_crew/context_assembly/replay.py:595-607). A plain user row is adopted. A nudge, a subagent row or any inject row, or the start of the window, keeps the steer as the request, because each of those may have opened the turn.
    • The partial output after the chosen opener is read through _partial_turn_output (src/kiro_crew/context_assembly/replay.py:131-175), the loop build_interrupted_turn_preamble already had, moved out of it so the Resume and Stop restores share one rule. Each steer the turn took is added in order as [Steered mid-turn] <text> (_STEER_LABEL, src/kiro_crew/context_assembly/replay.py:86). The opener keeps its own user_cap; a steer is capped at user_cap on its own and does not share assist_cap with the assistant text, so a long reply streamed before a steer cannot cut it (:138-141, :172-173). A steer marked requeued or retracted is left out (_STEER_STATES_NOT_TAKEN, :97, tested at :167): the Stop teardown moved a requeued one to the queue (src/kiro_crew/dashboard/chat_turn/steer_queue.py:279 on 95abd91), where it runs as the next prompt when the queue drains (src/kiro_crew/dashboard/chat_turn/queue_cycle.py:409, :425 on 95abd91) or the user dismisses it, and carrying it as well would put it in the prompt twice; a retracted one never reached the turn. A row that still reads written or has no state may have redirected the turn and is carried, as in the Resume restore.
  • Not in this PR: the row read and the worker-thread calls. feat(chat): take back a steer the running turn has not read yet #23388 shipped both (src/kiro_crew/context.py:1154-1157, prompt_assembly.py:706 and turn.py:745 on 95abd91), so context.py, dashboard/chat_runner.py and slack/handler_runtime/turn.py have no diff against main.
  • Docs: docs/architecture/design-notes/soft-stop.md and docs/system-specs/modules/history.md describe the restore and now say how a steer is handled.

Backwards compatibility

Compatible: a turn with no steer row takes the same path and produces the same bytes (test_a_turn_without_a_steer_restores_exactly_what_it_did_before compares the whole string, and it also passes on 95abd91; test_without_steers_the_cap_cuts_where_it_always_did pins where the cap on the assistant text cuts, and it also passes on 95abd91). The bracketed markers that context_blocks.py parses are untouched. Two results change: a steered turn is restored under its own opener with each steer the turn took carried in the partial output, and a steer marked requeued is not carried at all.

Removes one case: a steer marked requeued, which the old restore named as the request, is no longer carried, because it runs as the next prompt or the user dismissed it. Every other row the old restore carried is still carried. A steer's text moves from the request slot to the partial-response slot, outside the cap on the assistant text, so no other restore loses a row.

Tests

In short: four new tests fail with the restore source from main (95abd91) and pass with the change. On main each of the four restores a steer as the request (replay.py:576-580); in the requeued test that also puts the steer in the prompt twice, and in the long-stream test the long reply is missing from the partial output. Five controls (an unsteered turn, the cap on a turn with no steer, a window that lost the opener, a steered turn opened by a nudge, one opened by an inject row) pass on both. Red 3 of 3 runs (4 failed, 5 passed), green 3 of 3 runs (9 passed), 20 of 20 at -n0, 10 of 10 at -n 4. Nine one-line mutants are each caught by the tests written for them.

Added to test/test_history_restore_preambles.py (main's TestCancelledTurnPreambleRequest, the taken-back steer test, stays, and the new class sits after it), over a real ConversationLog in tmp_path, with steer rows written the way the dashboard stores them (extra_meta={"steer": True, ...}):

  • test_the_restore_names_the_original_request_and_keeps_the_steer_in_order (:196): request, reply, steer, reply, Stop. The request is the opener, the partial section reads reply, [Steered mid-turn] steer, reply, and the steer text appears once.
  • test_several_steers_keep_their_order_around_the_partial_reply (:216): two steers around a reply keep their order.
  • test_a_turn_without_a_steer_restores_exactly_what_it_did_before (:235): a drained queue row, which carries steer_delivery_ids and sendId but not steer, is a real opener. The full restore text is compared.
  • test_a_window_that_lost_the_opener_still_names_the_newest_steer (:265), test_a_steered_turn_opened_by_a_nudge_does_not_name_an_earlier_request (:280) and test_a_steered_turn_opened_by_an_inject_does_not_name_an_earlier_request (:296): the cases where the walk must not reach past a row that may have opened the turn into an earlier, finished request.
  • test_a_requeued_steer_is_not_carried_into_the_prompt_that_drains_it (:312): a steer marked requeued is not in the restore, so the prompt that drains it holds its text once.
  • test_a_steer_after_a_long_stream_survives_the_cap (:329): a 5000-character reply, then a steer; the steer is in the restore, after the cut reply.
  • test_without_steers_the_cap_cuts_where_it_always_did (:347): a control over eight chunk shapes against the joined-then-cut reference.

No other test file changes: test/context_composition_contract_helpers.py, test/test_chat_runner_coverage.py and test/test_slack_handler_turn_outcomes.py are as on main.

Before the change, in a checkout of main (95abd91) with this branch's test file copied in, python -m pytest -n0 -p no:cacheprovider -q test/test_history_restore_preambles.py -k TestCancelledTurnPreambleSteer, RC=1 in 3 of 3 runs:

E       AssertionError: assert 'Cancelled user request:\nplease refactor the parser\n' in '[PREVIOUS TURN WAS CANCELLED BY THE USER — context restore]\nThe following user request was interrupted mid-response....\nkeep the public API stable\n\nPartial assistant response before cancel:\nSwitching to a wrapper\n[END PREVIOUS TURN]'
test/test_history_restore_preambles.py:206: AssertionError
E       AssertionError: assert 'Cancelled user request:\nmigrate the config loader\n' in '[PREVIOUS TURN WAS CANCELLED BY THE USER — context restore]\nThe following user request was interrupted mid-response....ted work only when the current request depends on it.\n\nCancelled user request:\nthen the loader\n[END PREVIOUS TURN]'
test/test_history_restore_preambles.py:226: AssertionError
E       AssertionError: assert 2 == 1
test/test_history_restore_preambles.py:325: AssertionError
E       AssertionError: assert 'Narrowing to...REVIOUS TURN]' == 'xxxxxxxxxxxx...REVIOUS TURN]'
test/test_history_restore_preambles.py:341: AssertionError
============ 4 failed, 5 passed, 12 deselected, 2 warnings in 3.21s ============

With the change, the same command, RC=0 in 3 of 3 runs: 9 passed, 12 deselected.

Mutants, one line each, run once each on this head with replay.py put back after each, over test/test_history_restore_preambles.py and test/test_interrupted_turn_steers.py, all RC=1, each failing only the tests named:

  • the steer walk never fires: the same four tests that fail on main.
  • steer text dropped from the partial output: those four and ten Resume-restore tests in test/test_interrupted_turn_steers.py.
  • the predicate also accepts a row carrying steer_delivery_ids: the no-steer test.
  • the search no longer stops at a nudge or subagent row: the nudge test.
  • the search passes over an inject row: the inject test.
  • no opener in the window returns nothing instead of keeping the steer: the lost-opener test.
  • the label text changed: the same fourteen as the dropped steer text.
  • the requeued exclusion removed from _STEER_STATES_NOT_TAKEN: the requeued test here and the Resume restore's requeued test.
  • the cap applied again to the joined partial output: the two cap tests here and four cap tests of the Resume restore.

Repeats of the class on this head: 20 of 20 at -n0, 9 passed each; 10 of 10 at -n 4 --dist loadgroup --max-worker-restart=2 with test/test_history_pagination_projection.py and test/test_stop_turn_boundary_mirror.py as co-tenants, 36 passed each time.

Neighbours on this head, -n0: the three test files the earlier revision of this PR touched (test/test_chat_runner_coverage.py, test/test_history_restore_preambles.py and test/test_slack_handler_turn_outcomes.py), 164 passed in each of 3 runs. In one run, 553 passed: the test files that name the two preamble builders or the replay module (test/test_context_composition_contract.py, test/test_context_composition_contract_facade.py, test/test_interrupted_turn_steers.py, test/test_automation_steer_row_role.py, test/test_regenerate_swap.py, test/test_decisions_model_route_apply.py, test/test_slack_handler_composition_contract.py, test/test_slack_handler_composition_contract_golden.py, test/test_context_management_doc.py), the two that read the touched docs (test/test_ephemeral_sessions.py, test/test_history_locking_remediation_dashboard_save.py), and test/test_history_pagination_projection.py, test/test_stop_turn_boundary_mirror.py and test/test_history_composition_contract.py. The steer tests patch no module-global.

Gates on this head, each exit code read before any pipe, all RC=0: flake8 and isort on src/kiro_crew/context_assembly/replay.py and test/test_history_restore_preambles.py; the black formatting gate (4 changed files in scope, 2 checked); the comment-history gate with COMMENT_HISTORY_BASE_REF at origin/main; the flake-pattern ratchet (64 passed); the docs-lint gate with its self-test; mypy on src/kiro_crew/context_assembly/replay.py, the one src/ file this PR changes (no issues in 1 source file). The diff touches no website/ file and no deploy template, so the website gates and the cfn-lint check were not run.

Not run: scripts/local-gate.py and scripts/run_scoped_tests.py --surface backend, the full-suite-sized runs that CI repeats on the merge ref.

Manual verification

N/A: the tests drive the real builder over a real ConversationLog, and no gateway or dashboard was started. The callers are not changed here. One check outside the tests: the dashboard's row builder _build_message_entry (src/kiro_crew/dashboard/chat_persistence.py:586 on 95abd91), run on a row carrying the meta that steer_into_running_turn stores (src/kiro_crew/dashboard/chat_delivery.py:1024-1033 on 95abd91), returned meta.steer as the boolean True, so a stored steer row matches _is_steer_row.

Related Issues

Tracking: #20269, #20270. Landed on main, the reader half of this fix: #23388.

Pattern harvest

Rule candidate: review-prompt
Pattern: a backwards search for the row that opened a turn, keyed on role alone, when a row of that role can be injected into a running turn (a steer is a user row). The web client's opensTurn and the two Python restores each carry their own copy of this search.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 13, 2026 21:34
@javenciu
javenciu requested a review from mbriones98 October 13, 2026 21:34
@javenciu

Copy link
Copy Markdown
Contributor Author

Closing and reopening right after opening so CI runs on hosted runners: same-repo runs from this account queue on the fleet label without a runner (details on #19683).

@javenciu javenciu closed this Oct 13, 2026
@javenciu javenciu reopened this Oct 13, 2026
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 4ef0fe29be1d5ee26a891af342df399adabc0bef touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 4ef0fe29be1d5ee26a891af342df399adabc0bef — 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 does not.

First-Principles-Verdict: PASS

This fixes the mechanism: the walk now uses meta.steer to tell a steer from the row that opened the turn. The partial-output loop moved into a shared helper instead of being copied.

Notes

  • Item 1 — symptom-level (deferred): the Stop restore gets a second opener walk (replay.py:601-607), next to the Resume restore's walk at replay.py:217-226. It stops at every inject row because "this function is not handed TURN_OPENING_INJECT_KINDS". That makes 2 opener walks in replay.py (grepped _TURN_OPENER_ROLES: 2 uses in replay.py, plus 2 local copies in chat_handlers.py:854 and turn_marker.py:52). Subtraction: when the inject kinds reach both callers, delete the Stop-side step-back loop and have it call the one walk build_interrupted_turn_preamble uses.

What this change ships

Inventory (6 items) — 5 justified

Intent: after a Stop on a steered turn, the model gets back the request that opened the turn and the work done on it, not the steer. FIX.

  1. The Stop restore names the plain user row that opened the turn, not the newest steer — symptom-level (deferred): a second opener walk
  2. Steers the turn took appear in order as [Steered mid-turn] … in the partial reply — justified
  3. A requeued steer is no longer in the Stop restore, so the prompt does not carry it twice — justified
  4. The Stop restore's assistant cap applies to assistant text only, and steers are capped on their own — justified
  5. The Resume restore's partial-output loop moved into the shared _partial_turn_output, unchanged — justified
  6. soft-stop.md and history.md say how a steer is restored — justified

[FIRST-PRINCIPLES-REVIEWED] 4ef0fe2

Description read

[DESCRIPTION-READ] 21678968b1be121465d5be214e8402464e58298a2c31e4f1e6a9bd445620e239

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 4ef0fe29be1d5ee26a891af342df399adabc0bef — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks. The fix targets the root cause (a steer row being taken as the turn's opener) and reuses the Resume restore's partial-output rule instead of adding a second one. Restores of turns with no steer keep their exact bytes.

Notes

  • The only thing taken away is the requeued steer, and the ## Backwards compatibility section lists it and says why. No other reader of cancelled_turn_preamble is affected: its output markers and both callers are unchanged.

[DESIGN-REVIEWED] 4ef0fe2

Description read

[DESCRIPTION-READ] 79fc52e471b4b44648c6e4266f6d4a29c3d6d3b24d443f5ea61695ecbcb315b9

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 4ef0fe29be1d5ee26a891af342df399adabc0bef — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 4ef0fe2

Verdict parsed from the review's SHA-scoped output markers for commit 4ef0fe29be1d5ee26a891af342df399adabc0bef.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 4ef0fe29be1d5ee26a891af342df399adabc0bef false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 4ef0fe29be1d5ee26a891af342df399adabc0bef and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 4ef0fe2

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 4ef0fe29be1d5ee26a891af342df399adabc0bef false-positive: <reason, 20 to 500 characters>

@github-actions github-actions Bot added the readiness: action required A blocking check or review needs attention label Oct 13, 2026
@javenciu

Copy link
Copy Markdown
Contributor Author
  1. GPT 6.1, src/kiro_crew/context.py:1470 on df59967: the chained-tail read can fall back to the chained page read, which indexes every file in the chain, on the gateway event loop. Fixed in 160b908. Both callers build the preamble through asyncio.to_thread, as the session replay beside each of them already does: the dashboard in _run_chat at src/kiro_crew/dashboard/chat_runner.py:11697-11701, and Slack in handle_message at src/kiro_crew/slack/handler.py:1740-1742. build_cancelled_turn_preamble stays synchronous, and the comment above its read (src/kiro_crew/context.py:1467-1473) names the fallback. Tests: test/test_chat_runner_coverage.py::TestStopRestoreTranscriptRead::test_the_loop_runs_while_the_restore_reads_the_transcript and test/test_slack_handler.py::TestCancelledTurnRestoreReadsOffTheLoop::test_the_loop_runs_while_the_restore_reads_the_transcript drive a stopped turn through each caller. They hold the transcript read open until a coroutine on the loop runs, then assert that the coroutine ran while the read was open and that the restore reached the prompt. Red on df59967: 2 failed in 3 of 3 runs, each with AssertionError: no coroutine ran while the stop restore read the transcript: the read ran on the event loop. Green on 160b908: 2 passed in 3 of 3 runs, 20 of 20 at -n0, and 10 of 10 at -n 4; both touched test files in a shuffled order pass. Both test files, run in one process at -n0 ahead of test/test_taskrunner_atomic_persistence.py, test/test_taskrunner_execution_capture_async.py, test/test_unattended_slot_guardrails.py and test/test_wheel_apply.py, pass. On the new head, black, isort, flake8 and mypy pass, as do scripts/check_agent_sdk_boundary.py, scripts/check_sync_io_in_async.py and the comment-history gate.

@javenciu

Copy link
Copy Markdown
Contributor Author

span=dc2d7de999a7

fixed in 160b908: both callers build the preamble through asyncio.to_thread, the dashboard in _run_chat (src/kiro_crew/dashboard/chat_runner.py:11697-11701) and Slack in handle_message (src/kiro_crew/slack/handler.py:1740-1742); build_cancelled_turn_preamble stays synchronous.
test/test_chat_runner_coverage.py::TestStopRestoreTranscriptRead and test/test_slack_handler.py::TestCancelledTurnRestoreReadsOffTheLoop hold the read open until a loop coroutine runs: red 3 of 3 on df59967 (the read ran on the event loop), green 3 of 3 on 160b908.

@javenciu

Copy link
Copy Markdown
Contributor Author

Closing and reopening so CI runs on hosted runners: same-repo runs from this account queue on the fleet label without a runner (details on #19683).

@javenciu javenciu closed this Oct 14, 2026
@javenciu javenciu reopened this Oct 14, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 14, 2026
@javenciu
javenciu enabled auto-merge (squash) October 14, 2026 05:05

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

REQUEST_CHANGES at 160b908, for follow-up tracking. The description explicitly leaves two known restore cases unfixed. One is build_interrupted_turn_preamble, behind Resume. The other is a steer after a recovery/user_replay injection. Neither has a linked issue; Related Issues says none open. Fix these cases or link one issue for each accepted deferral. Also answer whether read_messages_chained_tail always ends with this session's transcript. These are tracking and review requirements, not a claimed new regression. Source and tests were read; nothing ran locally. — crew-pr-reviewer (r99)

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: passed Eligible automated validation passed for the current revision labels Oct 14, 2026
@javenciu
javenciu force-pushed the fix/stop-restore-skips-steer-rows branch from 160b908 to ebf3a87 Compare October 14, 2026 11:10
@javenciu

Copy link
Copy Markdown
Contributor Author
  1. Resume restore: deferred to Resume restore should skip mid-turn steers when finding the interrupted request #20269. build_interrupted_turn_preamble (src/kiro_crew/context_assembly/replay.py:115-168) walks backward to the first row whose role is in _TURN_OPENER_ROLES (:148-158). Because user is an opener role, a mid-turn steer can be selected as the interrupted request; _is_steer_row (:89-96) is not applied on this path. The issue tracks the Resume-specific opener rule and regression coverage.

  2. Steer after a recovery or user-replay injection: deferred to Cancelled-turn restore should cross continuing recovery and replay injections before a steer #20270. cancelled_turn_preamble (src/kiro_crew/context_assembly/replay.py:486-512) currently stops its backward search at any inject row (:492-498). That treats recovery and user_replay as boundaries even though TURN_OPENING_INJECT_KINDS excludes them (src/kiro_crew/dashboard/state.py:1337-1341), so a later steer can remain in the cancelled-request slot. The issue tracks the continuing-inject distinction and its regression coverage.

  3. read_messages_chained_tail does not promise that the final key is always the exact key passed by the caller. The established contract is the newest limit rows of the chronological tab chain: _chain_keys returns chained_keys(key) or [key] (src/kiro_crew/history_projection.py:407-409), chained_keys returns the indexed keys for the row's tab_id (:411-431), and the tab index scans sorted transcript paths and appends matching members (src/kiro_crew/history_projection.py:1026-1054). The tail read then reads keys[-1] (src/kiro_crew/history_projection.py:443-461). If the key has no indexed tab chain, the fallback is [key]; otherwise the result ends at the newest file in that chain. When the newest file cannot fill the limit, the page fallback computes the end of the concatenated chain and reads the final range (src/kiro_crew/history_projection.py:935-968). build_cancelled_turn_preamble calls this method for the current session key (src/kiro_crew/context.py:1466-1476).

@javenciu
javenciu requested a review from iamwhatever October 14, 2026 11:31
@javenciu

Copy link
Copy Markdown
Contributor Author

span=a3946a06bf37

  • The requeued-steer filter reads the disk tail, which gets the requeued mark only at the next flush. kept: the finding is accurate, and main does the same for every steer. _requeue_unconsumed_steers (src/kiro_crew/dashboard/chat_runner.py:3612) marks the row on the live slot only (src/kiro_crew/dashboard/slot_buffers.py:617-624), and the transcript the restore reads gets the mark at the 5-second dirty-slot flush (src/kiro_crew/dashboard/state.py:1544). A restore built inside that window still carries the steer. On main the restore names the newest user row whatever its state, so a requeued steer lands in the prompt twice every time. test_a_requeued_steer_is_not_carried_into_the_prompt_that_drains_it fails on main with assert 2 == 1 (test/test_history_restore_preambles.py:281). This change removes the double once the flush has landed and adds no new case. As the finding says, closing the window needs code outside this diff: a slot save after the requeue, or a read of the steer state from the live window. That changes the dashboard teardown, so it is a separate change.

@javenciu

Copy link
Copy Markdown
Contributor Author

@cruisercohen one addition since my last comment, so this head does change src/: four lines in build_cancelled_turn_preamble (src/kiro_crew/context.py:1144-1147) and one test. Both review lanes asked whether the tail read keeps warm-task-brief rows that recent() dropped. It did. With no stop_event in the window, a brief posted after the cancel was read as the cancelled turn's partial reply. The restore now drops those rows through _without_warm_task_brief_messages, as recent() does.

test_a_brief_posted_after_the_cancel_is_not_the_partial_reply (test/test_history_restore_preambles.py:326) fails on b97a0bf and passes on main and on this head. Everything else is as in my last comment: the other src/ and docs/ lines are the ones you approved at c7e97e8. The steer tests pass 3 of 3 runs (10 passed with the new test). The four touched test files give 266 passed, and mypy on context.py, flake8, isort, the black gate and the comment-history gate pass. Please approve this head.

@javenciu

Copy link
Copy Markdown
Contributor Author
  • Check that the Stop teardown writes steerState: requeued before the next prompt's restore reads the transcript. kept: it does not always, as answered on c7e97e8 (#issuecomment-6246375546), and this head leaves that window as main has it. The order is right: _hand_off_queue requeues (src/kiro_crew/dashboard/chat_runner.py:3612) before it starts the next queued turn (:3628). But the mark lands on the live slot row (src/kiro_crew/dashboard/slot_buffers.py:617-624), and the transcript the restore reads gets it at the 5-second dirty-slot flush (src/kiro_crew/dashboard/state.py:1544). A restore built inside that window still carries the steer. Main carries every steer, requeued or not, so its prompt holds a requeued steer twice every time. test_a_requeued_steer_is_not_carried_into_the_prompt_that_drains_it fails on main with assert 2 == 1 (test/test_history_restore_preambles.py:281). Flushing the slot after the requeue changes the dashboard teardown, so it is a separate change.

@javenciu

Copy link
Copy Markdown
Contributor Author
  • There are now two opener walks, and they differ on inject rows. kept: they do, and the description's "Not a goal" declares it in its second bullet. The Resume walk passes over an inject row whose kind continues a turn and stops at one that opens a turn (src/kiro_crew/context_assembly/replay.py:208-211). It is handed those kinds as opener_inject_kinds (:171, from src/kiro_crew/dashboard/chat_runner.py:7383). The Stop walk is not handed them, so it stops at any inject row and keeps the steer as the request (replay.py:546-549). That matches main whenever it differs from Resume. So for a steer sent after a Resume press, with a recovery or user_replay row (src/kiro_crew/dashboard/state_parts/transcript_rows.py:311) between the opener and the steer, Stop names the steer and Resume names the opener. test_a_steered_turn_opened_by_an_inject_does_not_name_an_earlier_request (test/test_history_restore_preambles.py:252) pins the half that is handled. Passing the opening kinds through both callers of build_cancelled_turn_preamble, dashboard and Slack, is a separate change.

@javenciu

Copy link
Copy Markdown
Contributor Author

Closing and reopening so CI runs on hosted runners: same-repo runs from this account queue on the fleet label without a runner (details on #19683).

@javenciu javenciu closed this Oct 18, 2026
auto-merge was automatically disabled October 18, 2026 07:47

Pull request was closed

@javenciu javenciu reopened this Oct 18, 2026
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: checking Automated validation is still running labels Oct 18, 2026
@javenciu
javenciu force-pushed the fix/stop-restore-skips-steer-rows branch from da06d8a to b166c3d Compare October 19, 2026 02:04
@javenciu

Copy link
Copy Markdown
Contributor Author

@cruisercohen this push rebases the PR onto main at 1b96d89 as one commit. The last head, da06d8a, conflicted with two of main's refactors, so two of our hunks moved to the files that now hold that code. Nothing else in src/ or docs/ changed since da06d8a (item 2). The repo requires approval of the latest push, so please approve the new head.

  1. Main's refactor(slack): run handle_message as phases in handler_runtime owners #21040 (8204775) moved handle_message's turn body into src/kiro_crew/slack/handler_runtime/turn.py, and test: split four oversized test files by behaviour under test #21922 (145fac9) split test/test_context_composition_contract.py. Where each of our lines went, old -> new:
    • The Slack off-loop build, a two-line comment and await asyncio.to_thread(build_cancelled_turn_preamble, context_builder.conversation_log, session_key): src/kiro_crew/slack/handler.py:1740-1744 -> src/kiro_crew/slack/handler_runtime/turn.py:649-653. The enclosing function is async def _run_turn (turn.py:282) and asyncio is imported at turn.py:22. The PR's diff to handler.py is now zero. handler.py:78 still imports build_cancelled_turn_preamble, which the composed owner function reads from the handler's globals.
    • _TranscriptLog.read_messages_chained_tail, two lines: test/test_context_composition_contract.py:93 -> test/context_composition_contract_helpers.py:52, in the class now at :37. The PR's diff to test/test_context_composition_contract.py is now zero, and the golden for cancelled_turn_preamble in it is unchanged.
    • test/test_slack_handler_turn_outcomes.py took the patch without a conflict. The tests we add there patch nothing at kiro_crew.slack.handler, so no patch target had to move. The one existing test that does patch build_cancelled_turn_preamble there, test/test_slack_handler_composition_contract.py:1185, passes unchanged: turn.py reads that name from the handler's globals.
  2. Nothing else in src/ or docs/ changed. The check compares the + and - lines of the two patches, the old one against its own base:
    MB=$(git merge-base da06d8a49a origin/main)
    diff <(git diff $MB da06d8a49a -- src docs | grep -E '^[+-]' | grep -vE '^(\+\+\+|---) ') <(git diff origin/main HEAD -- src docs | grep -E '^[+-]' | grep -vE '^(\+\+\+|---) ')
    It prints nothing (RC=0). Each side is 169 lines over the same six files, except that src/kiro_crew/slack/handler.py on the old side is src/kiro_crew/slack/handler_runtime/turn.py on the new one: docs/architecture/design-notes/soft-stop.md, docs/system-specs/modules/history.md, src/kiro_crew/context.py, src/kiro_crew/context_assembly/replay.py, src/kiro_crew/dashboard/chat_runner.py and the Slack hunk.
  3. Red-first on the moved Slack line. With main's turn.py swapped in, which differs from ours only in that call, TestCancelledTurnRestoreReadsOffTheLoop fails at test/test_slack_handler_turn_outcomes.py:800 (no coroutine ran while the stop restore read the transcript: the read ran on the event loop), 3 of 3 runs. With the dashboard's asyncio.to_thread at src/kiro_crew/dashboard/chat_runner.py:4624 replaced by a direct call, TestStopRestoreTranscriptRead fails at test/test_chat_runner_coverage.py:179, 3 of 3 runs. With replay.py and context.py from main, 4 of the 9 steer tests fail (test/test_history_restore_preambles.py:162, :182, :281, :297), 3 of 3 runs. Each file was restored afterwards and the tree equals the head.
  4. Tests on the rebased head:
    • test/test_history_restore_preambles.py, test/test_interrupted_turn_steers.py, test/test_chat_runner_coverage.py, test/test_slack_handler_turn_outcomes.py, test/test_context_composition_contract.py, test/test_context_composition_contract_facade.py and test/test_slack_handler_composition_contract.py: 452 passed, 3 of 3 runs at -n 4.
    • The two off-loop tests: 20 of 20 runs at -n0 (2 passed), and 10 of 10 at -n 4 --dist loadgroup --max-worker-restart=2 with test/test_chat_runner_folder_steering.py and test/test_slack_handler_composition_contract.py as co-tenants (182 passed). The 9 steer tests: 20 of 20 at -n0 (9 passed), and 10 of 10 at -n 4 with test/test_history_pagination_projection.py and test/test_stop_turn_boundary_mirror.py (36 passed).
    • The three test files this change touches and the two contract files that use the helper module: 266 passed. The neighbours in the body: 333 passed. The wait-discipline, pid-reader, dir-fd, main-ratchet audit and ratchet-scope tests with test/test_tmpdir_helpers.py: 133 passed. The flake-pattern ratchet: 60 passed.
    • Black gate (10 changed files in scope, 8 checked), isort, flake8, the comment-history gate, the agent-SDK boundary and sync-IO-in-async gates with their self-tests, docs lint, the file line cap and mypy on context.py, replay.py, chat_runner.py and turn.py all pass.
  5. The PR body now cites the moved lines and the counts above. A few other line references in it had drifted, with main's refactors or with our own four lines in build_cancelled_turn_preamble, so I re-read each on this head: the dashboard call at src/kiro_crew/dashboard/chat_runner.py:4624-4628, the queue drain now in src/kiro_crew/dashboard/chat_turn/queue_cycle.py:404 and :420, src/kiro_crew/history_projection.py, src/kiro_crew/history.py:299 and src/kiro_crew/context.py:1171.

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention labels Oct 19, 2026
@javenciu

Copy link
Copy Markdown
Contributor Author

Closing and reopening so CI runs on hosted runners: same-repo runs from this account queue on the fleet label without a runner (details on #19683).

@javenciu javenciu closed this Oct 19, 2026
@javenciu javenciu reopened this Oct 19, 2026
cruisercohen
cruisercohen previously approved these changes Oct 19, 2026

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

Thanks @javenciu. I re-reviewed b166c3d and approve it.

  • The rebase moves no logic. Against da06d8a, the src/ and docs/ changes are the same lines, with the Slack call now in handler_runtime/turn.py after main's #21040, and the test helper in context_composition_contract_helpers.py after #21922.
  • The change since c7e97e8, the head I approved before, is the four lines in build_cancelled_turn_preamble that drop warm-task-brief rows, as recent() already does. With that filter removed, test_a_brief_posted_after_the_cancel_is_not_the_partial_reply fails and the other 20 tests in test_history_restore_preambles.py pass.
  • Locally on this head, the seven test files your comment lists and the flake-pattern ratchet pass (512).

The only red is Backend Tests (3.12, 2): three whole-tree scans in test_facade_mirror_guards.py hit the 120 s per-test timeout, the slow-runner timeout tracked in #19907. The same three tests time out on other open PRs this weekend, so it does not come from this change. I re-ran the failed jobs once. Coverage Gate fails behind that shard.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 19, 2026
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 23, 2026
The Stop restore walked back to the newest user row and took it as the
request the cancelled turn answered. A mid-turn steer is a user row
too, so a turn that was steered and then stopped was restored with the
steer as its request, and the original request and the reply streamed
before the steer were dropped.

The walk now passes over steer rows (a user row marked meta.steer) to
the plain user row before them, and carries each steer the turn took,
labelled and in order, inside the partial output. When a nudge, a
sub-agent completion or an inject row sits in between, or the window
holds no opener, the steer stays the request, because any of those may
have opened the turn.

The partial output is read through the one rule the Resume restore
already used, now shared as _partial_turn_output. A steer the turn never
took (steerState requeued or retracted) is left out: the Stop teardown
moved a requeued one to the queue, where it runs as the next prompt, so
carrying it would put it in the prompt twice, and a taken-back one never
reached the turn. A steer is capped on its own at the request cap and
does not share the cap on the assistant text, so a long reply streamed
before it cannot cut it.

Only context_assembly/replay.py changes in src. The builder's row read
and its worker-thread calls are already on main.
@javenciu

javenciu commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the approval. Main moved under this PR, and the scope changed with it: #23388 now ships the full-row read in build_cancelled_turn_preamble, the warm-brief filter and the worker-thread calls that this PR had added, so those hunks and the tests that covered them are gone from the diff (context.py and both callers now match main exactly). What remains is the opener fix in context_assembly/replay.py (the steer walk and the shared _partial_turn_output), its tests in test/test_history_restore_preambles.py, and the two doc paragraphs.

On current main, 4 of the 9 steer tests fail in 3 of 3 runs. With the change, all 9 pass in 3 of 3 runs, and the 5 controls pass on both.

The head changed. Could you re-approve it?

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

APPROVE at 4ef0fe2.

The root cause is fixed: when the newest user row before the stop marker is a steer, the restore walks back to the row that opened the turn and stops at any row that may itself have opened it. Partial output goes through the Resume restore's own loop, _partial_turn_output, so requeued steers are left out and a steer no longer spends the assistant cap. The new tests cover the steered, multi-steer, no-steer byte-identity, lost-opener, nudge, inject, requeued and long-stream cases.

Our r99 items are settled: the Resume case was fixed on main (#20269 closed), and the inject-row case is tracked in #20270, which also covers the First Principles note about sharing one opener walk. Every check on this head is green and every AI lane passes.

I read the diff and CI; I did not run tests locally.

— crew-pr-reviewer (r139)

The red Sensitive Change Review rows on this head are two cancelled runs; a newer run of the same check on the same head (4ef0fe2) passed, so they do not count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants