Skip to content

fix(autonudge): keep goals active during child work - #10098

Open
rubencu wants to merge 1 commit into
kirodotdev:mainfrom
rubencu:fix/keep-goal-active-during-subagents
Open

rubencu wants to merge 1 commit into
kirodotdev:mainfrom
rubencu:fix/keep-goal-active-during-subagents

Conversation

@rubencu

@rubencu rubencu commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Keep a goal or monitor active until every attached child and accepted completion delivery has settled, while preserving an explicit human stop path.

A goal driver could stop itself while delegated subagents still had running, queued, or accepted but unconsumed completion work. Queue handoffs and channel-specific slot-less turns could hide that ownership from the stop fence.

Why it matters

Terminal completion delivery must remain exactly once, fail closed, and observable. Losing ownership can drop a child result or leave its parent permanently fenced, while incomplete diagnostics can suggest a recovery action that cannot work.

Not a goal

  • Prevent a person from directly stopping or deleting a session.
  • Change goal or monitor product behavior after child work has settled.
  • Redesign channel dispatch beyond carrying the live subagent manager into the shared guard.

What changed (motivation → approach → change)

A goal or monitor stays active while its child work owns a terminal report or accepted completion. Agent-issued autonudge_stop and monitor_stop use this stop-only extension. Other session operations keep their running, queued, and active-handoff guard. Direct human Stop and Delete remain authoritative.

Area Change
src/kiro_crew/slack/ The completion consumer in gateway_runtime/subagent_injection.py transfers completion ownership before queue or runner handoff, owns a completion parked behind its inline turn until the park ends, keeps prompt enrichment separate from delivery-debt identity, and clears report-only store debt without a result tombstone. A discard reads back only tombstones the writer persists (tombstone_persists in subagent_persistence_tombstones.py), so a dropped incognito or temporary completion releases the fence.
src/kiro_crew/dashboard/ Gives the queue repository every destructive removal, retires each discard callback once, preserves pre-entry and retry ownership (including a teardown cancel), queues a failed completion's retry behind waiting messages and holds it out of automatic drains until a turn ends well or a person acts, retires a handed-over slot's producers only after its tail commits, releases the human Stop/Delete fence under the parent session key, and applies both stop directives through one attachment verdict (chat_utils_completion.py). Session-mutation 409 bodies are unchanged.
discord/, feishu/, imessage/, teams/, webex/, wecom/, weixin/, messaging/dispatch.py Each channel gateway gives its dispatcher the live subagent manager, which it passes into the slot-less directive consumer.
src/kiro_crew/subagent_parts/ and src/kiro_crew/subagent_manager/ Track report, announcement, and accepted-delivery ownership (admission/gate_reporting.py); queued report-only settlement waits for the store write before release (admission/taskq_bridge_expiry.py); queued counts read the window twice (run.py); a failed owed-report clear raises after the tombstones (waves.py).
src/kiro_crew/apps/builtins/spec_builder/ Spec Builder Pause and Delete drop queued work through the slot's queue_discard_all(), so a dropped completion no longer fences the parent's stop.
src/kiro_crew/dashboard/chat_runner.py and chat_turn/ The unconsumed-completion settlement is registered before the first consume-seam veto and discards only on a recorded Stop, not a responsive send's pause. A raw-announce retry restores the plain context and cancelled-turn flag it drained; main restores failure notices (#32411) and merge cards (#17165), and its tracing (#35077) wraps this code.
docs/system-specs/ Defines the stop-only fence, loop budgets, queue lifecycle, channel wiring, recovery guidance and the SEL audit row.
test/ Covers cancellation, exact-once settlement, held failed retries, queue removal, enriched prompts, report-only debt, channel wiring, and human override.

No accepted RFC records the agent-issued stop refusal during child work, so the First Principles product-shape decision stays with maintainers.

Backwards compatibility

Breaking: an agent-issued autonudge_stop or monitor_stop that main accepts is refused while child delivery ownership remains. Writer sweep on origin/main @ 6ccab43c14c0: both directive writers use the shared applier, and configured cycle or wall-clock budgets still deactivate their loops independently.

Reader: src/kiro_crew/dashboard/session_directive_apply_monitor.py:_autonudge_stop -- chat, channel, and cron turns -- shared attachment verdict refuses only the agent directive.

Reader: src/kiro_crew/dashboard/session_directive_apply_monitor.py:_monitor_stop -- structured and legacy monitors -- same verdict, agent directive only.

Reader: src/kiro_crew/dashboard/chat_api/turn_stop.py:stop_slot_turn -- dashboard Stop -- bypasses the agent-only refusal.

Reader: src/kiro_crew/dashboard/chat_api/slot_lifecycle.py:_close_slot -- dashboard Delete and session close -- bypasses the agent-only refusal and retires queued ownership exactly once, after its history commit.

Reader: src/kiro_crew/dashboard/chat_turn/queue_drain.py:_start_next_queued_turn -- every automatic drain -- runs past a held failed retry; a completion whose turn never starts is requeued with its retry held, and only a Stop or teardown discards it.

Tests

  • Regressions that fail without their fix: two failed completions hold their retries instead of running each other; an unconsumed steer returns ahead of a held retry; a replay vetoed at its consume seam or beside a hook continuation keeps one owner; a send pausing a completion turn reruns it in full after the reply, while Stop discards it (test_completion_retry_ownership.py); Stop on an incognito or temporary child's queued completion releases the fence, while a persistent child whose tombstone write failed keeps it, and a tab closed during a busy-turn wait still rings the bell (test_cron_thread_routing.py); Spec Builder Pause and Delete release a queued completion's fence (test_routes_delete.py); dashboard Delete releases the ledger entry keyed dashboard:<slot> (test_autonudge_handlers_completion_release.py); agent stops wait for a failed child finalizing or one admitted mid-count, context-cap and cron teardown for one started then (test_autonudge_stop_auth.py, test_gateway_subagent_injection.py); a full queue keeps a held completion queued with its fence (test_session_deliver.py); a failed owed-report clear keeps the tombstones (test_subagent_delivery_ttl_anchor.py); idle-tab deliveries and the stall kick skip a held retry until a good turn end, a send, Run now or Stop (test_completion_retry_hold_drains.py); a stop in the turn that got a result inline waits for its reply (test_inline_park_completion_ownership.py).
  • test_queue_cancel.py guards that only the queue repository empties a slot queue; test_directive_consumer_subagents.py checks every consumer call site; a stop in the turn reading the last completion applies, and a retry still reads the notices its attempt drained (fix(dashboard): settle a sub-agent notice only once the model reads it #32411) (test_completion_retry_ownership.py).
  • Related backend suite: 43,772 passed; 2 host-only failures fail the same on clean main.

Manual verification

N/A -- unit tests only.

Related Issues

Part of #8060

Pattern harvest

Rule candidate: review-prompt

Pattern: ownership handed over before an async handoff needs one release path per outcome.

Checklist

  • At most two commits, with a Conventional Commits title
  • Existing tests pass and regression tests cover the repaired behavior
  • Self-review completed and code follows project style guidelines
  • Documentation updated
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the repository's Apache 2.0 license.

@rubencu
rubencu requested review from a team and smeyffret September 11, 2026 09:31
@rubencu

rubencu commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

Intent: Keep a goal loop active until same-session sub-agent work and terminal delivery settle, while preserving the user-owned direct Stop control.
Not a goal: Change direct user Stop, monitor budgets, or sub-agent delivery order.

@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 11, 2026
@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — 🔴 changes requested (blocking)

Reviewed 457a14e1dca4b9bdcf36fe05961dddea568f010c via the fork AI-review pipeline; updated in place on each push.

1 of 1 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

BLOCKING -- test/test_autonudge_handlers_completion_release.py:71 -- Close task stores opened by the new manager tests

manager = SubagentManager(sessions=MagicMock(), ctx_builder=None)
Trigger: Running this test with the default-enabled task queue calls SubagentManager.__init__ → _open_taskq → taskq_open, opening SQLite without teardown closure. The new admission, retry, and inline-ownership modules and several added constructors in existing test modules have the same omission.
Outcome: SQLite handles outlive test teardown until cyclic collection; repeated parametrized cases accumulate descriptors and exhaust low descriptor limits, failing unrelated subsequent tests.
Anchor: no-test-side-effects
Fix: Request close_subagent_managers in every affected test/ module, including the changed coverage modules using _manager(). Close the Spec Builder test’s manager in a finally block, since that suite does not inherit the opt-in fixture.

[BLOCK-MERGE] 457a14e
[GPT-REVIEWED] 457a14e

Adjudication (Opus 5.5) — is blocking on each finding proportionate?

Adjudication result: no adjudicable findings. The one fenced finding, F1, gets UPHOLD-FENCED, not FLAG, so it keeps blocking the merge.

F1 — confirmed in the patch at authentic.patch:3663: the new test module builds a SubagentManager inline and never requests close_subagent_managers (test/conftest_opt_in.py:265, which closes the manager at teardown, lines 290-295). That fixture exists because this exact leak was measured (fixture docstring, lines 268-272), and more than 60 test modules already use it. The leak happens on every run, not under rare conditions, so a FLAG isn't justified. The fix is one line per module.

[ADJUDICATION] 457a14e total=0 uphold=0 downgrade=0
[GPT-ADJUDICATED] 457a14e

[ADJUDICATION-FENCED] 457a14e fenced=1 flagged=0
UPHOLD-FENCED F1 test/test_autonudge_handlers_completion_release.py:71 -- The inline manager at patch line 3663 opens the task-queue store on every run without the repo's established close_subagent_managers teardown (test/conftest_opt_in.py:265), so the condition is routine rather than extreme and the one-line fix leaves no residual risk worth an override.
[GPT-ADJUDICATED-FENCED] 457a14e

@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 457a14e1dca4b9bdcf36fe05961dddea568f010c via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the change is reversible, and a person can still clear the new agent-stop refusal with direct Stop or Delete, or wait for the loop's budgets to run out.

Notes

  • The fence only releases if a discard callback runs. Any app that empties slot._queue directly, as Spec Builder did, leaves its parent's agent stop refused until a person acts. Spec Builder is pinned in apps/adoption-pins.json, so the _discard_queued_work fix in runtime.py also has to land in KiroCrewCaptain/kirocrew-app-spec-builder and be named with App repo: <owner>/<repo>@<sha>. Otherwise adopting the pinned app brings the leak back. The description is truncated, so that line may sit past the cut.
  • A failed probe ("child-work status unavailable", "terminal-report status unavailable") also refuses the agent's stop. A broken probe therefore keeps a goal running until its cycle or wall-clock budget ends. That is the right default, but it is the main operational risk; logging the refusal at debug level makes it hard to diagnose.
  • By the author's own statement, no RFC records the refusal of an agent-issued stop during child work. A maintainer needs to sign off on that product decision.

[DESIGN-REVIEWED] 457a14e

Description read

[DESCRIPTION-READ] 9908404626b3cc58f4245e800f114fe414b5a445d444d9fc65cecfe0943fa5dd

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 Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🔴 BLOCK

Premise-level review of 457a14e1dca4b9bdcf36fe05961dddea568f010c via the fork AI-review pipeline — 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: BLOCK

product-shape change without accepted RFC: by default, the agent-issued autonudge_stop and monitor_stop are now refused while child work is outstanding. The description calls this "Breaking", and the base RFC list records no decision for it.

Notes

  • Item 2 — oversized: the description says "nothing the user sends there releases a fence retained after a failed settlement", so a goal on a channel-only session with no cap keeps nudging until a dashboard Stop or a restart. Subtraction: limit the refusal to running or queued children, the predicate other operations already use; drop the extension to accepted-but-unconsumed completions.

What this change ships

Inventory (8 items) — 6 justified

Intent: keep a goal loop alive until the work it delegated has settled. Framed as a FIX, but it changes behavior.

  1. The agent's own goal/monitor stop is refused while children run, queue, or still owe a report — product shape, no recorded decision
  2. A failed settlement leaves a fence that no channel verb can release — oversized
  3. Discarding a queued completion releases the parent's fence exactly once — justified
  4. A failed completion retry waits behind queued messages and is held out of automatic drains — justified
  5. Spec Builder Pause/Delete clear the queue through the repository and release the fence — justified
  6. Every channel gateway hands the live subagent manager to its directive consumer — justified
  7. Dashboard Stop/Delete releases the fence under the parent key — justified
  8. New SEL row subagent_completion_discard/retained — justified

Blockers

product-shape change without accepted RFC — item 1
Trigger: the PR adds, in _autonudge_stop/_monitor_stop, "refuses while the session has running or queued children, an owned terminal report …, or an accepted completion turn not yet consumed". On base both directives stop unconditionally (session_directive_apply_monitor.py:1041-1103). The description itself says: "No accepted RFC records the agent-issued stop refusal during child work."
Outcome: every goal and monitor loop changes what its stop command does by default, and nobody has recorded that decision.
Clears when: either an RFC covering agent-issued goal/monitor stop refusal during child work is merged non-draft on the base, followed by a push or rebase; or a maintainer posts /ai-review override first-principles 457a14e1dca4b9bdcf36fe05961dddea568f010c product-shape: <decision>.

[FIRST-PRINCIPLES-REVIEWED] 457a14e

Description read

[DESCRIPTION-READ] ef9af29aeda7d84d5cf54d49f288d5b869f0db5ae653d93905dad57f07da5c28

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 Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 457a14e1dca4b9bdcf36fe05961dddea568f010c via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 457a14e

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Sep 11, 2026

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

Blocking: the slot-less child-work guard is wired only into the two Slack directive-consumer call sites. Discord and Webex are also nudge-able bindings, but their dispatcher-less fallbacks do not pass a subagent manager; without attached dashboard state, subagents_attached sees no registry and autonudge_stop can still remove the goal while child work or terminal delivery is pending. Pass each channel live subagent manager into build_directive_consumer and add slot-less regressions for Discord and Webex. Also state explicitly whether monitor_stop intentionally remains outside this guard.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Sep 11, 2026
@rubencu
rubencu force-pushed the fix/keep-goal-active-during-subagents branch from 9c93d6e to afc2f1c Compare September 13, 2026 19:38
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: passed Eligible automated validation passed for the current revision readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Sep 13, 2026
@rubencu

rubencu commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

@cixuuz This change request reviewed 9c93d6e0368a1a64a81af58bda0fc3e385253264; current head afc2f1c9b66c607f625d592d2020b0fb9f0dac74 addresses each item:

  • Discord passes the live manager from discord/gateway.py through discord/transport_dispatch.py into build_directive_consumer, with the slot-less regression in test/test_discord.py.
  • Webex has the parallel wiring and regression in webex/gateway.py, webex/transport_dispatch.py, and test/test_webex_dispatch.py.
  • Agent-issued monitor_stop is intentionally inside the same child-work guard; test_structured_monitor_stop_refuses_while_subagent_work_is_attached locks that behavior. The user-owned direct dashboard Stop remains outside the agent directive guard.

All current-head checks pass, and there are no unresolved review threads. Please re-review the current head so the stale CHANGES_REQUESTED state can be cleared.

@rubencu

rubencu commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author
  • rebutted — listing child names in the refusal

The refusal already gives the only safe next action: wait until running, queued, and completing children settle. Naming children would add a new progress-snapshot contract only to tune retry timing; it does not make the teardown decision safer. This PR stays scoped to preserving the goal driver, while existing sub-agent status surfaces remain the source for child identities.

@rubencu

rubencu commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author
  • rebutted — shared-predicate terminal interval

The broadening is intentional and is now stated in the PR description. subagents_attached already guards reset, model, workspace, reload, and related session mutations while work is running, queued, or in slot delivery. A manager-owned _on_done report is the same attached lifecycle: mutating the parent before that await finishes can change or remove the report target. _ON_DONE_TIMEOUT bounds the interval, and the user-owned direct dashboard Stop remains unchanged.

@rubencu

rubencu commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author
  • fixed — Discord wiring was undeclared

The PR description and test inventory now explicitly name the slot-less Discord dispatcher. Current head passes the live manager through discord/gateway.py and discord/transport_dispatch.py; test_slotless_directive_consumer_gets_live_subagent_manager proves the fallback receives it.

@rubencu

rubencu commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author
  • fixed — Webex wiring was undeclared

The PR description and test inventory now explicitly name the slot-less Webex dispatcher. Current head passes the live manager through webex/gateway.py and webex/transport_dispatch.py; test_slotless_directive_consumer_gets_live_subagent_manager proves the fallback receives it.

@rubencu

rubencu commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto c24c7bc8d3; the new head is 174a942b47. There was one conflict, in a test:

  • test_dashboard_slot_busy_queues in test/test_slack_gateway_subagents.py. Main test(chat-slack): give weak tests real checks and merge twin cases #23743 strengthened its checks on the queued completion: the message text, kind == SUBAGENT_COMPLETION_KIND, and no notify. Main still checks them against queue_append, but this PR queues the completion with queue_insert(len(queue), …, on_consumed=…, on_discarded=…). The resolved test keeps both: this PR's insert position and callbacks, and main's text, kind and no-notify checks, now read from the queue_insert call.

git range-diff shows that test hunk is the only change to this PR's patch. Main's edits to queue_cycle.py, turn_entry.py, turn_locals.py, messaging/dispatch.py, nudge_channel_fire.py and transport_dispatch.py merged cleanly. The writer sweep is unchanged on c24c7bc8d3: the shared applier is still called from tool_results.py (2), session_directive_apply.py (4) and messaging/dispatch.py (1). The description now names that base and the new test count.

Tests:

  • A 1,111-file scoped selection passed 47,120. The only failure was the boot-order pin, which needs a built website/dist and passes without one (65/65).
  • Static gates, the line cap, docs lint and the body check passed.

@rubencu

rubencu commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the fenced GPT finding on 20d899aae0 (turn_ledgers.py:184, user follow-ups discard unread child completions). The new head is e86bf40071, rebased onto efc8f03203.

  • Cause: when a completion turn lost its session at prompt depth 0, _on_acp_session_not_found queued the retry with _queue_recovery. That retry carries the completion's on_discarded callback, and the handler armed the user-revocable SESSION_NOT_FOUND record for it. A message the user sent while the reset was awaited revoked that record in _drop_revoked_replays. Dropping the row fired _note_completion_discarded, which settled the unread result as delivered.
  • Fix: _on_acp_session_not_found now takes the branch the binding-mismatch and image-history recoveries already take. When _replay_owes_delivery(turn), it calls _requeue_owed_completion(turn): the retry is queued as the completion it is (SUBAGENT_COMPLETION_KIND), with no lost-session record, so nothing a user sends can cancel it. Every other lost-session retry keeps its revocable record, unchanged.
  • Test: test_a_user_follow_up_during_the_reset_keeps_an_owed_completion in test/test_chat_session_not_found_recovery.py. A user message is queued while the reset is awaited. The test fails on 20d899aae0 (on_discarded is called once) and passes here: the completion runs again, then the follow-up, and nothing is discarded.

Main #23929 rewrote test/test_cron_thread_routing.py, which conflicted with this PR's edits there. The resolution keeps main's file, adds this PR's 12 tests and its two imports, and keeps main's new kind and ownership checks in test_busy_slot_timeout_queues_result, now read from this PR's queue_insert call. It drops this PR's earlier re-formatting of lines main has since rewritten; that file is on the black baseline.

Tests:

  • Run on eee0f6aab6: a 724-file scoped selection passed 33,681. Two failed, both host state:
    • The boot-order pin, which needs a built website/dist and passes without one (65/65).
    • The new test_governance_policy_viewer.py enterprise-baseline case, which fails the same way on a clean main checkout here.
  • Re-run on each later base: the tests main touched plus this PR's tests passed (3,257, then 1,079).
  • The static gates, the line cap and docs lint passed.

@rubencu

rubencu commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 813e25c8fe (#23278) and fixed the Flake Pattern Ratchet red on e86bf40.

  • The ratchet failed on e86bf40 with test/test_chat_session_not_found_recovery.py: K3-stdlib-clock-patch 5 > 4 against efc8f032. The cause was the patch("asyncio.sleep", new_callable=AsyncMock) in test_a_user_follow_up_during_the_reset_keeps_an_owed_completion, the regression added for the fenced GPT finding. Await Fast Gate, Fast Gate, CI and Coverage Gate failed downstream of it.
  • That test now bounds the turn with asyncio.wait_for(..., timeout=30) and drains after it, with no clock patch. It still fails on 20d899a's pre-fix acp_recovery.py ("Expected 'mock' to not have been called. Called 1 times.") and passes here in 0.19 s. The ratchet passes locally against 813e25c8.
  • Main's src/kiro_crew/dashboard/chat_utils.py change (/issue joins the blocked slash commands) merged cleanly beside the PR's three TYPE_CHECKING imports. The range-diff against e86bf40 is that one test file.
  • Checks on c20ca1d: 3,538 tests (the PR's tests, main's slash-command tests, the chat_utils facade tests), the dist-less composition contract (65), the static gates, the file line cap and --check-body.

@rubencu

rubencu commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the fenced GPT finding on c20ca1d (turn_ledgers.py:150: an enriched completion retry lost its lifecycle protection), and rebased onto 27cb0849ed, which includes #23933's fix for the shard-4 governance-viewer red.

  • _queue_recovery now treats a full replay of an unconsumed completion turn's message as a replay of its announce. It puts the failure notices that attempt drained back on the slot, then queues the raw announce. The process-death retry and the first empty-response retry both pass turn.message to _queue_recovery when nothing was emitted. After a drained child-failure notice, that text differs from the announce, so the row was an ordinary synthetic_recovery that still carried the completion's discard callback. It is now the lifecycle_recovery row the admission sweep exempts, like the PR's other raw-announce retries (_settle_unconsumed_completion, _requeue_owed_completion).
  • The refusal-retry re-arm still keys on the requeue being this turn's own text, decided before the announce replaces it.
  • New regression test_an_enriched_completion_lost_to_a_process_death_survives_a_new_link in test/test_completion_retry_ownership.py: a completion turn drains a failure notice, its process dies before any output, then the slot is linked to a channel and the admission sweep runs. On c20ca1d the retry is queued as the enriched text with kind synthetic_recovery; the sweep drops it and the discard callback runs once. Here the retry is the raw announce with kind lifecycle_recovery, the notice is back on the slot, the sweep keeps the row and the callback is not called.
  • Like those other raw-announce retries, this retry does not replay background context the failed attempt drained (drain_pending_context).
  • Checks on a33907dfe0 (27cb0849ed adds only RFC docs): a 760-file selection (35,177 passed; the one failure is the dist-built boot-order pin, which passes in the dist-less composition contract, 65), the Flake Pattern Ratchet against main, the static gates, the file line cap and --check-body. The PR body is unchanged: it already says every requeued completion retry carries the failure notices its attempt drained.

@rubencu

rubencu commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the GPT 6.1 blocking finding on b52579f (turn_ledgers.py:168): a Stop-hook continuation queued beside a completion's exact replay inherited the completion's callbacks, so cancelling or consuming it could settle the completion while its replay was still queued.

_queue_recovery now gives a completion's on_consumed, on_irreversibly_consumed and on_discarded only to the first exact replay of its announce. Continuations, retry prompts and rows queued after that transfer carry none. Turns without a discard owner keep main's behaviour.

  • Regression: test_a_stop_hook_continuation_beside_a_completion_replay_owns_nothing (test/test_completion_retry_ownership.py) fails on b52579f, because the continuation row carries _on_discarded, and passes on a0e1bf2.
  • Rebased onto 3b96b6c; main's delta is two test files and two workflow files, with no overlap.
  • Scoped run: 4,245 passed, plus 634 in related recovery tests. The one failure is the dist-built boot pin, which passes dist-less (65 passed). Black, mypy, Semgrep and the other static gates passed.

@rubencu

rubencu commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 4acd9b7 to clear a conflict with #24706 in test/test_cron_thread_routing.py. Both sides added imports after from kiro_crew.cron import ...: main added SessionMap, session_links and set_dashboard_surfaced, and this PR added _ChatSlot. The resolution keeps all four imports in isort order. No test bodies changed, and the rest of the patch is identical (116/116 files).

Scoped run: 7,326 passed, including main's new strict-xfail and lenient-read ratchets. The one failure is the dist-built boot pin, which passes dist-less (65 passed). Static gates passed.

@rubencu

rubencu commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto aeb5c02 to clear a conflict with #23388 in src/kiro_crew/dashboard/chat_api/turn_stop.py. On a hard stop, main now clears the steer take-back (steer_retract.on_hard_stop(slot)) right after taking the queue, and this PR takes the queue through slot.queue_discard_all(), so each queued producer's discard callback is retired once. The resolution keeps both: queue_discard_all() first, then main's take-back clear, both before the settle can suspend. The rest of the patch is identical (116/116 files).

Scoped run: 25,044 passed, plus 156 in main's newest test delta. The two failures are main's strict XPASS in test_crew_log_fullscan_guard.py and the dist-built boot pin, which passes dist-less (65 passed). Static gates passed.

@rubencu

rubencu commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the Opus 5.5 blocking finding on 7dfc258 (turn_ledgers.py). When a completion turn's first try failed, the retry queued the raw announce. The background-context entries and cancelled-turn preamble that try had drained were then gone for good, so the retry's prompt carried neither.

The prompt build now also records the slot's pending background-context entries (after foreign notes are dropped), and the session's cancelled-turn flag when its preamble was used. _restore_drained_failures puts these back along with the failure notices, so the retry's own build drains them again. Expired context entries are still discarded there.

  • Regression: test_a_completion_retry_carries_back_the_context_and_preamble_its_attempt_drained (test/test_completion_retry_ownership.py) fails on 7dfc258 (the context entry is gone) and passes on 1afe32b.
  • Rebased onto a62012b with no conflicts. drain_pending_context keeps its pinned signature.
  • Scoped run: 26,124 passed. The two failures are main's strict XPASS in test_crew_log_fullscan_guard.py and the dist-built boot pin, which passes dist-less (65 passed). Static gates passed.

@rubencu

rubencu commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main to resolve conflicts with #24714 and #22254.

  • src/kiro_crew/subagent_manager/terminal.py (fix(subagent): announce restart orphans in their dashboard chat #24714): main's early return for a run left to the next start's orphan sweep now comes first, and this PR's _retain_failure helper follows it.
  • src/kiro_crew/dashboard/chat_turn/turn_ledgers.py (fix(slack): keep links in the mirrored prompt echo, retire Thinking line #22254): _queue_recovery keeps main's mirror-echo stamp and this PR's callback and kind selection.
    • A completion turn skips the echo check. A completion is never a person's words, so it never owes the echo, and _settle_unconsumed_completion can run before the turn binds _mirror_echo_posted.
    • Without the guard, test_a_completion_replay_vetoed_at_its_consume_seam_keeps_one_owner[False] fails with UnboundLocalError.
  • test/test_slack_mirror_echo.py (fix(slack): keep links in the mirrored prompt echo, retire Thinking line #22254): TestTheDrainCarriesOrigin now runs the spawned coroutine. The drain calls _run_chat inside its owned runner, so a spawn stub that closes the coroutine never sees the kwargs. This follows the pattern this PR already uses in test_chat_runner_coverage_queue.py.

This PR's tests, the tests main changed since the last push, the composition contracts and the static gates pass. The only failures are ones main shows on this host too.

@rubencu

rubencu commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 8ca3e82, rebased onto main 3cdcfe893c. The rebase resolved one conflict in src/kiro_crew/dashboard/chat_turn/queue_drain.py: main's #25480 added _record_queued_crew_steers at the same spot where this PR adds _next_drain_is_completion_retry. Both functions are kept unchanged, and the rest of the patch is identical.

The PR's tests, the tests main changed since the last published base ca8f35673b, the composition contracts and the static gates pass; the only failure also fails on main on this host.

@rubencu

rubencu commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

mechanism: the async queued count reads the window on both sides of the store await and counts the larger reading

  • span=3b96d9d7798e run.py:399: Queue-to-running handoff bypasses the teardown guard

fixed in 3d69749: _queued_depth_async_impl now reads the in-memory window before and after the store await and counts the larger reading, so a queued child that starts during the await still counts and the attachment guard keeps the session. The one change covers both callers that read running children before this await: the attachment probe behind the context-cap trim, resets and stops, and the Slack cron reset. test_a_queued_child_that_starts_while_the_store_count_is_read_still_holds_the_session and test_a_queued_sibling_that_starts_during_the_count_keeps_the_cron_session fail on 8ca3e82.

@rubencu

rubencu commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 3d69749, rebased onto main 6b5bedad1b without conflicts. It fixes the GPT lane's finding on 8ca3e82 (run.py:399, queue-to-running handoff).

  • The async queued count now reads the in-memory window before and after the store await and counts the larger reading. A queued child that starts during the await, including an incognito or temporary child with no store row, still counts, so the context-cap trim, resets and the stop fence keep the session instead of discarding it and cancelling the child. A spawn admitted during the await still counts, as before.
  • The Slack cron reset reads running children before awaiting the same count, so it had the same gap; the one change covers it too.
  • test_a_queued_child_that_starts_while_the_store_count_is_read_still_holds_the_session (test_autonudge_stop_auth.py) and test_a_queued_sibling_that_starts_during_the_count_keeps_the_cron_session (test_gateway_subagent_injection.py) fail on 8ca3e82.
  • Every test that imports the attachment probe, the sub-agent manager or the context cap passes (8,076). On 6b5bedad1b, the PR's tests, the tests main changed since 3cdcfe893c, the composition contracts and the static gates pass (4,535); the only failure also fails on main on this host.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 24f9a5f, rebased onto main 18553fb4b6. Main rewrote its idle-tab drains (#25138, #25711) and added an inline park for a result the running turn received (#27508), so two of this PR's rules are re-applied to main's code rather than carried over as text.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 4609534, rebased onto main ec2e1a1609. Two conflicts, no behaviour change in this PR:

  • Main's refactor: make core independent of the spec-builder app #30091 moved the Spec Builder Pause tests out of test/test_compaction_stop_declined_surfaces.py into the app's own test_pause_while_compacting.py. That file now takes main's text, and the queue_discard_all this PR gave the test's slot double moves with the tests, so the Pause still drops queued work through the slot's queue repository.
  • Main's feat(session): opt-in idle compaction before prompt-cache expiry #24384 added idle_compaction_blocker to the chat_utils type-checking import list next to this PR's subagent_attachment_detail_async; both stay.
  • The Spec Builder suite, the compaction-stop, idle-compaction, chat_utils and completion-ownership tests pass (2,042), and the composition contracts pass in a clean checkout.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: the drain hands a popped completion to its turn only once the turn body registers the completion's settlement

  • span=d0cfad1260e9 src/kiro_crew/dashboard/chat_turn/queue_drain.py:1504: Admission refusal strands completion ownership

fixed in ec94067: the drain no longer marks a popped completion's runner as entered when the guarded call starts. _turn_open marks it right after it registers the completion's settlement, through a new _on_completion_registered hook. When fair admission refuses the turn, or its wait ends before run(), the drain's existing never-entered callback settles the completion through its _on_discarded, so the retained manager ownership is released and an agent's goal stop applies again. The busy-tab queue_insert, idle-tab queue_append and stall-kick hand-offs all reach this drain, so the one change covers them. test_completion_admission_ownership.py runs 18 cases (rejected, wait expired, slot closed, for each hand-off and both autonudge_stop and monitor_stop); all 18 fail on 4609534.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: the drain hands a popped completion to its turn only once the turn body registers the completion's settlement

  • span=156d59329fce src/kiro_crew/dashboard/chat_turn/queue_drain.py:1018: A refused or ended admission keeps a drained completion's ledger count

fixed in ec94067: the runner is now marked entered only once _turn_open has registered turn_exit.settle_completion, through a new _on_completion_registered hook, not when _owned() starts. When gate_turn returns without calling run() (admission rejected, the wait expired, or the slot closed), _discard_if_runner_never_entered settles the completion through its _on_discarded, so _retained_completion_owners is released, terminal_delivery_inflight_for turns false and an agent's autonudge_stop or monitor_stop applies. Rows without discard callbacks are still marked entered at _owned() start. test_completion_admission_ownership.py covers the three outcomes through the busy-tab, idle-tab and stall-kick hand-offs with both stop tools; all 18 cases fail on 4609534.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed ec94067, rebased onto main 857e6b3d16 without conflicts. It fixes the GPT and Opus finding: a completion that fair admission refuses, or whose wait ends before its turn runs, is now discarded through the same path as any completion that never ran, so the agent's goal stop is no longer refused afterwards. The drain hands the completion to the turn only once the turn body registers it, which covers the busy-tab, idle-tab and stall-kick hand-offs alike. One consequence: the parent session does not receive that child's result. The change is 15 lines added and 3 removed outside tests. The rebase brought main's new K19 flake rule, so one existing test's literal timeout=30 now uses the wait helper's default.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 68f6832, rebased onto main 59e91f239b without conflicts. The push only rewords the docstring of a test added in the last push, which the comment-wording check flagged (test/test_completion_admission_ownership.py). No code or test logic changed.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: the manager takes a completion's ownership count only at its hand-off, a queue row or a discard settlement
resolution: narrowed -- the manager owns a completion only from its hand-off (a queue row or a discard settlement), so a wait cancelled before it holds nothing

  • span=ffbb335430d2 src/kiro_crew/slack/gateway_runtime/subagent_injection.py:790: Cancellation before queue handoff leaks completion ownership

fixed in 30eb5ea: _route_subagent_to_tab takes the manager's ownership count only at a hand-off: right before the busy branch's queue_insert, inside the tab lock right before the idle branch's queue_append, and before the discard settlement when the tab closed during the busy wait. Until then the caller's own fence covers the waits (its report or announce task, or a park's count), so a report timeout that cancels the lock wait or the busy wait leaves nothing retained. test_a_route_cancelled_while_it_waits_for_the_tab_lock_retains_nothing (autonudge_stop and monitor_stop) holds the tab lock, cancels the route while it waits, then checks that nothing is queued, terminal_delivery_inflight_for is false and the agent's stop removes the loop. It fails on 68f6832.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 30eb5ea for GPT's blocking finding on 68f6832 (subagent_injection.py:790). The tab route now takes the manager's ownership count only when it hands the completion off: to a queue row, or to the discard settlement when the tab closed during the wait. A report timeout that cancels the busy wait or the tab-lock wait leaves nothing retained, so the agent's later autonudge_stop or monitor_stop is not refused. New test test_a_route_cancelled_while_it_waits_for_the_tab_lock_retains_nothing fails on 68f6832; the four cron-routing tests that pinned the earlier count now check the hand-off.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: the drain discards a completion whose turn never ran only on a Stop or the slot's teardown, and requeues it under the retry hold otherwise
resolution: narrowed -- the drain's pre-entry discard now takes only a Stop or the slot's teardown; any other end requeues the row under the existing completion retry hold

  • span=156d59329fce src/kiro_crew/dashboard/chat_turn/queue_drain.py:1529: A refused admission marks a queued completion delivered

fixed in ef8f54b: _settle_unentered replaces _discard_if_runner_never_entered and also runs when spawn_guarded_turn raises. A completion whose turn ends before the turn body registered it is discarded only when a Stop is under way (slot._stopping, or the stop generation moved since dispatch) or the slot is no longer live. Every other end (admission rejected, its wait expired, a bare cancel, a failed start) puts the popped rows back at the end of the queue and adds their ids to _held_completion_retries. Nothing writes a delivered tombstone, so a restart re-announces the child, and the next turn that runs releases the hold and gives the model the result. The agent's autonudge_stop and monitor_stop stay refused while the row waits and apply once it is delivered. In test_completion_admission_ownership.py, test_a_refused_completion_is_requeued_and_the_next_admitted_turn_delivers_it (rejected and wait expired, through the idle-tab, busy-tab and stall-kick hand-offs), test_a_bare_cancel_while_the_turn_waits_requeues_the_completion (the three hand-offs), test_a_completion_turn_that_fails_to_start_is_requeued and test_the_goal_stop_waits_for_a_requeued_completion_and_applies_once_it_is_delivered (both stop tools) fail on 30eb5ea: 12 cases. The Stop and teardown cases of test_a_completion_admission_never_runs_is_discarded_and_the_goal_stop_applies still pass.

@rubencu

rubencu commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed ef8f54b, rebased onto main 02a1152510 without conflicts, for Opus's blocking finding on 30eb5ea (queue_drain.py:1529). A sub-agent result whose turn ends before the turn body runs (fair admission refuses it, its wait ends, the turn is cancelled, or it fails to start) now goes back in the queue under the existing retry hold, and the next turn that runs gives it to the model. Only a Stop or the tab's teardown discards it. This replaces the line in my earlier note saying the parent session does not receive that child's result: it now receives it. The PR description's drain line now says the same. No further changes are planned unless a maintainer asks.

@rubencu

rubencu commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author
  • span=7b1dd39921d0 — needs-a-decision

The goal requires an agent-stop refusal while child work remains attached. The PR records the breaking behavior and preserves direct human Stop/Delete. A maintainer must accept the product shape through an RFC or exact-head ruling.

@rubencu

rubencu commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the requested channel parity in ef8f54ba76: Discord and Webex now receive the live subagent manager from their gateways and pass it into the slot-less directive consumer, with focused regressions for both paths. The PR body also states the chosen monitor_stop behavior explicitly: agent-issued monitor_stop uses the same child-work fence, while direct human Stop/Delete remains authoritative.

@rubencu

rubencu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Pushed f1b8afb, a conflict-resolving rebase onto main 0feaa8be90. Since 02a1152510, main rewrote the code these conflicts sit in, so the resolution adapts this PR to it:

  • fix(dashboard): settle a sub-agent notice only once the model reads it #32411 now settles a drained sub-agent notice only once the model has read the prompt, and puts an unread one back at the front of the queue when the turn ends. This PR therefore drops its own copy of the drained failure notices (_drained_subagent_failures) and their settlement at drain time. Its raw-announce retries still hand back what main does not: plain background context and the cancelled-turn flag. The helper that does it is renamed from _restore_drained_failures to _restore_drained_inputs, because it no longer restores failure notices.
  • Merge-card replay (feat(chat): backend to merge a fork back into its parent chat #17165): a raw-announce retry restores only plain context, because main puts merge cards back itself (_restore_consumed_merge_contexts), and a note authorized for another session is left out. _queue_recovery treats message and _message_without_merge_cards alike as a full replay. Main's two new hand-over exits in slot_lifecycle.py retire the queued producers only after the tail commits, as the other exits do. _message_without_merge_cards is bound before the completion settlement registers, because a replay vetoed at its consume seam settles before the runner binds it.
  • feat(tracing): sub-agent nesting, completion join and run roots (phase 1b, #1643) #35077: main's sub-agent tracing hooks (note_delivery on the completion row, delivered in the drain's spawn) wrap this PR's ownership code unchanged.
  • Tests: stand-in gateways set sessions (main's _inline_verdict reads it), one test marks its inline result confirmed, the queued-row checks expect main's final-result note (fix(subagent): skip the synthesis when the last result was reported #32498), and main's test_an_owed_completion_replays_the_card_once notes the delivery debt, as this PR's other completion tests do, because a completion is replayed as owed only while the slot still holds its debt.

Size: non-test lines go from +1365/-180 to +1372/-182 (net +5), with about 55 non-test lines rewritten in place of 46. Tests grow by 14 lines and lose 3. Every other conflict keeps both sides.

The related backend suite passes except test/test_harness_parity.py::test_is_kiro_cli_is_positive and test/test_search_segment_route.py::test_one_dictionary_per_host, which fail the same way on a clean main checkout on this machine.

Keep agent-issued goal stops fenced while child execution or terminal delivery remains attached. Preserve popped queue-completion ownership across pre-entry cancellation, and distinguish one-shot discard failures that require restart orphan recovery.
@rubencu

rubencu commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 457a14e, rebased onto main 6bd6d5b3ce (1,502 commits behind before this push). This is a conflict-only rebase: every conflict was a place where main and this PR each added lines, and each keeps both sides. All 125 of the PR's patches are unchanged.

  • docs/system-specs/modules/session.md: main's new "A stricter agent.sandbox recycles a session at its next turn" subsection is followed by this PR's "Queued completion cancellation ownership" section.
  • dashboard/chat_turn/queue_drain.py: main's new session_run_queued run-now branch stays in the promotion chain, and this PR's held-retry release and hiding follow it unchanged. Only a caller's Run now or a person's waiting send releases a held retry, as before. Main's workflow-trace comment follows this PR's pre-entry owner block.
  • dashboard/chat_turn/turn_ledgers.py: main's workflow-trace import comes before this PR's callback-ownership block.
  • dashboard/chat_utils.py and spec builder's routes_coverage_helpers.py: both sides' imports are kept.

No other code, test or description changed.

This branch has not been deployed

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

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge needs-author-decision PR blocked on author input readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants