Skip to content

fix(sessions): remind a Kiro agent that ends its turn without finish_session, and judge silence by the screen (task #208) - #666

Merged
aixle-com[bot] merged 4 commits into
developfrom
feature/kiro-turn-end-nudge
Nov 8, 2026
Merged

aixle-com[bot] merged 4 commits into
developfrom
feature/kiro-turn-end-nudge

Conversation

@aixle-com

@aixle-com aixle-com Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Task #208. An automatic Kiro session whose agent ends its turn without finish_session no longer waits for a person. For Kiro, a screen that stopped changing now counts as silence, even while the pane keeps sending bytes. The screen rule covers Kiro only for now; the other runtimes keep the log-mtime rule.

Which option, and what the measurement showed

Built: B (typed reminder) and C (silence judged by the screen). I measured A first and did not build it.

Measured on kiro-cli 3.0.0, the pin (checksum as in docker/kiro-cli/Dockerfile), with --v3 chat in the TUI and a global ~/.kiro/hooks/*.json Stop command hook:

  • A {"decision":"block","reason":"…"} answer with exit 0 makes the agent continue in the same turn. The reason reaches it inside <HOOK_INSTRUCTION>.
  • The hook runs only once per prompt. When the continued turn ended, the hook did not run again, and Kiro went back to its idle prompt. A new prompt typed into the pane runs it again.
  • The hook does not run when a provider or network error ends the turn. I made the proxy drop connections mid-turn. The pane showed Proxy connection ended before receiving CONNECT response, then the Credits line and the idle prompt, and the hook never ran.
  • The hook's stdin is only {session_id, hook_event_name, cwd}, with Kiro's own session id. Nothing in it says whether finish_session was called, or whether the platform session is finishing or paused.

So A gives one continuation per session and misses case 2. A typed message is a new prompt: it works on every turn end, including one ended by an error. I checked that a reminder sent with tmux send-keys -l … ; Enter is submitted and answered.

The bytes in case 1. An idle 3.0.0 TUI with no client sent 0 bytes in 40 s. Two pane resizes made it repaint 12,238 bytes while capture-pane stayed byte-identical. A web terminal attaching to the pane or resizing it does exactly this, which fits the fresh terminal_output.log in case 1. I didn't reproduce the exact trigger in that session.

What changed

B: Sessions::TurnEndNudge + TurnEndDetector, called from ScanQuotaErrorsActivity. The scan already reads each pane once a minute, and this reuses that read.

  • TurnEndDetector recognises Kiro's idle prompt (› ask a question or describe a task ↵). The prompt must be among the last lines of the current screen, with a ▸ Credits: turn … summary just above it. A draft typed in the box, a working indicator, or the startup screen (no summary yet) does not match.
  • The same screen must show on two reads at least 50 s apart (turn_end_screen digest and turn_end_seen_at in metadata). The reminder is then claimed under the session's row lock: the session, the confirmed screen and the count are checked again, the count goes up and the screen is forgotten, before anything is typed. A Temporal retry of the scan, or a second scan of the same screen, types nothing. A reminder whose typing failed is used up. Then the reminder is typed: "Aixle: your turn ended, but this automatic session is not finished. If all work is done, call finish_session. Otherwise continue from where you stopped."
  • Guard. One exec runs date +%s && tmux list-clients -t agent -F '#{client_readonly} #{client_activity}'. While a writable client has sent input within 10 minutes, nothing is typed, and the reminder goes out on the first scan after that. The rule is based on input: a read-only viewer never holds it. tmux sets client_activity when a client attaches, so opening the owner's terminal also holds reminders for 10 minutes. Clients that cannot be read hold the reminder too.
  • Typing. Sessions::PaneInput.type sends the text and Enter in one tmux call (send-keys -t agent -l MSG ; send-keys -t agent Enter), so a partial failure cannot leave an unsent draft. Sessions::AuthPause#nudge uses it too. Sessions::ScreenDigest.lines(text, last:) is now the one place that turns a pane into its screen lines, for TurnEndDetector, ScreenDigest, AuthPause, InteractivePromptDetector and QuotaErrorDetector.
  • Cap. At most 3 reminders (turn_end_nudges). The turn end after the third fails the session: "Session terminated: the agent ended its turn without calling finish_session 4 times, and Aixle's 3 reminders to finish or continue did not change that. …"
  • Never touched: sessions that aren't ready (finishing ones included), interactive mode, non-workflow-step sessions, sessions Sessions::AuthPause holds or has just acted on, and runtimes without a measured signature.

C: Sessions::NoOutputWatchdog judges Kiro's silence by a digest of the rendered screen.

  • Kiro only for now. The screen rule applies to the runtimes in NoOutputWatchdog::SCREEN_RULE_RUNTIMES, an opt-in list like TurnEndDetector::SIGNATURES that holds kiro_cli only. Every other runtime keeps the old rule, for the watchdog and for the token-refresh sweep: the log's mtime. Their working screens were not captured, and an indicator that animates only through colour could read as silence while the agent works. They move to the screen rule in a separate task, through the same list.
  • Read from the start, every 5 minutes. ScanNoOutputSessionsActivity no longer skips sessions younger than 31 minutes, so the screen record starts at the first sweep. no_output_scan_workflow now runs every 5 minutes (*/5 * * * *, overlap: skip) to keep those reads affordable. A silent session is ended 30 to 35 minutes after its screen (or, for other runtimes, its log) stopped changing.
  • Each read stores screen_digest and screen_changed_at on the session. A screen can't change without bytes, so the log mtime still caps the time: a first read, or a read that sees a changed screen, starts from the mtime. An unchanged screen keeps its recorded time, so 30 minutes of repaints now count as silence.
  • When the pane shows the idle prompt, the failure says the agent ended its turn without calling finish_session. Other messages are unchanged.
  • A working Kiro session stays alive because its indicator ticks: Kiro is working · 6s (measured).

Token-refresh sweep (RefreshExpiringTokensActivity#parked?). It still calls silent_for?(10.minutes). For Kiro it now reads the same screen record: a session parked at its prompt counts as parked even while it repaints. Before, it read as mid-turn and its rotating grant was not refreshed. An agent mid-turn changes its screen, so it still defers. The first read of a session judges by the mtime, as before, and an unreadable container still answers "not parked". Every other runtime is judged exactly as before this PR, by the log's mtime.

Turn-end gap in other runtimes (out of scope)

Automatic sessions run the TUI on Claude Code, Cursor CLI, Codex, Gemini CLI, Grok and Kiro, so all of them wait at their prompt when a turn ends. Antigravity runs --print in automatic mode and exits back to the shell. With this PR, every runtime is still ended by the watchdog after 30 to 35 minutes of silence: Kiro judged by its screen, the others by their log's bytes. Only Kiro has a reminder signature. Adding one for another runtime means measuring its idle screen and adding an entry to TurnEndDetector::SIGNATURES, with nothing else to change.

Tests

The fixtures are real capture-pane output from kiro-cli 3.0.0 (test/fixtures/files/terminal_panes/kiro_cli/): a normal turn end, a turn ended by a network error (case 2's shape), the turn after a reminder, and a working pane.

  • test/services/turn_end_detector_test.rb: matches the turn end and the error-ended turn. A working pane, a draft, the startup screen, a turn end left in the scrollback and other runtimes don't match.
  • test/services/sessions/turn_end_nudge_test.rb:
    • one reminder after two reads, and the agent sent back to work;
    • a person who typed 5 minutes earlier is not interrupted, and the reminder goes out once 10 minutes have passed;
    • read-only viewers don't hold it, and unreadable clients do;
    • the cap fails the session with the message;
    • an interruption after the keys were sent sends no second reminder, two scans of the same confirmed screen send one, and a failed type counts toward the cap;
    • auth-paused, interactive, finishing and non-step sessions are left alone.
  • test/services/sessions/no_output_watchdog_test.rb: a Kiro screen unchanged for 31 minutes is stale while the mtime stays fresh, and a ticking working screen is not. The first read uses the mtime as before. The refresh sweep's 10-minute question follows the screen. The idle-prompt message is checked. A runtime outside SCREEN_RULE_RUNTIMES is judged by the log's mtime, for stale? and silent_for?, and keeps no screen record.
  • test/temporal/activities/workflow/scan_no_output_sessions_activity_test.rb: a Kiro session that starts with no screen record, whose screen stays unchanged while redraw bytes keep the mtime fresh, is ended by the real watchdog at the 5-minute cadence 30 to 35 minutes after its screen stopped changing.
  • test/temporal/activities/workflow/scan_quota_errors_turn_end_test.rb: the scan sends the reminder on its second pass and fails the session after the cap.
  • test/services/sessions/pane_input_test.rb: one tmux call for the text and Enter, and false on a refused or failed exec.
  • FakeRuntime#set_terminal_clients answers tmux list-clients.

Fix CR round 1, on Coder at 176ef030: the changed tests (151 runs) and the neighbouring suites (215 runs) pass, and rubocop on the changed files is clean. Details are in the task.

First round, on Coder at a2b99665:

  • the new and changed tests (42 runs) pass;
  • the neighbouring tests (117 runs) pass;
  • make rails_check_all (Vite build + the full backend suite, 9841 tests) is OK;
  • full rubocop (2898 files) and brakeman are clean.

Docs

  • docs/architecture/workflows.md gets a new section, When an automatic session's agent stops without finish_session.
  • The user guide's workflows Gotchas gets a bullet, in both the repo copy and the in-app copy.
  • The watchdog lines in docs/design/agent-runtimes/DESIGN.md and docs/design/agent-credential-lifecycle.md now describe screen-based silence for Kiro and the 5-minute sweep.
  • CHANGELOG.md gets two Sessions & Runs fixes.

How to test (QA, staging)

Staging has no Kiro credential and none can be connected there (Alex, 2026-11-08), so option B is verified by the automated tests built on real pane captures. On staging, the watchdog can be checked on any runtime: a Claude Code step parked at its prompt fails 30 to 35 minutes after its last output.

With a Kiro credential:

  1. Run a Kiro workflow step whose instruction is "Reply with one word and end your turn. Do not call any tools." The reminder appears in the terminal a minute or two after each stop. After the third reminder, the step fails with the message above.
  2. Open the session's terminal as its owner and type to the agent. Nothing is typed while you work. Reminders resume 10 minutes after your last keystroke.
  3. A normal Kiro step finishes as before.

…session, and judge silence by the screen

Automatic sessions run the CLI's TUI, which waits for a person when a turn ends.
The quota scan now types a reminder into a ready non_interactive Kiro step that
sits at its idle prompt on two reads, holds it while a person has typed in the
last 10 minutes, and fails the session after 3 reminders. The no-output
watchdog judges silence by a digest of the rendered screen, so an idle TUI that
keeps repainting no longer reads as busy.

Kiro's Stop hook was measured on 3.0.0 and not used: it fires once per prompt
and not on a provider error (task #208).

Signed-off-by: Aixle Flow <[email protected]>
aixle-com Bot pushed a commit that referenced this pull request Nov 7, 2026
Comment thread app/services/sessions/turn_end_nudge.rb Resolved
Comment thread app/services/sessions/turn_end_nudge.rb Resolved
Comment thread app/services/sessions/turn_end_nudge.rb Resolved
Aixle Flow added 2 commits November 8, 2026 09:35
…ng, one tmux call (task #208 CR)

- NoOutputWatchdog judges silence by the screen only for SCREEN_RULE_RUNTIMES
  (kiro_cli); other runtimes keep the log-mtime rule, for the watchdog and the
  token-refresh sweep.
- ScanNoOutputSessionsActivity reads sessions from their start (no MIN_AGE) and
  runs every 5 minutes.
- TurnEndNudge counts a reminder and forgets the confirmed screen under the row
  lock before typing, so a retry or a second scan types nothing.
- Sessions::PaneInput types text and Enter in one tmux call, used by AuthPause
  and TurnEndNudge; ScreenDigest.lines replaces the copied pane normalization.
- ScanQuotaErrorsActivity reads AuthPause's verdict with auth.paused?.

Signed-off-by: Aixle Flow <[email protected]>
… watchdog delay from the recorded screen time

Signed-off-by: Aixle Flow <[email protected]>
Comment thread app/services/sessions/turn_end_nudge.rb Resolved
Comment thread app/services/turn_end_detector.rb Resolved
… the reminder cap; match Kiro's idle prompt at any pane width (task #208 CR)

TurnEndNudge#give_up reads the session again under its row lock, as claim does, and fails it only if it still takes reminders (ready, non_interactive, workflow step, not auth-paused) and the cap is reached. SessionService.fail_session runs after the lock is released.

TurnEndDetector matches Kiro's prompt on its start, allows three lines under it and no longer needs the Credits line's session figure on the same line: at 30 columns the placeholder, the hint and the Credits line wrap. New real kiro-cli captures: 3.0.0 at 80x24, 220x50 and 30x20, and 2.34.0 at 80x24.

Signed-off-by: Aixle Flow <[email protected]>
@aixle-com
aixle-com Bot merged commit 6a21b8b into develop Nov 8, 2026
17 checks passed
aixle-com Bot pushed a commit that referenced this pull request Nov 8, 2026
Conflicts were between the squash merges of #665 and #666 on develop and the earlier feature-branch versions of the same PRs already on staging. Every conflicted file was touched on staging only by those two branches, so each takes develop's final version.
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.

1 participant