Skip to content

fix: rewrite the lite spec from its template before refusing a turn - #33965

Merged
bolichen97 merged 1 commit into
mainfrom
fix/33345-lite-spec-repair
Nov 13, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/33345-lite-spec-repair

Conversation

@bolichen97

@bolichen97 bolichen97 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: History consolidation and the other background helper turns keep running on a host where another tool writes its own MCP server into kirocrew-lite.json, and the background session still never starts on a spec that mounts anything.

A credentials sandbox tool writes a creds-agent MCP entry into every agent spec under ~/.kiro/agents/ each time it launches, kirocrew-lite.json included. The background helper refuses to start a kirocrew-lite session on a spec that mounts anything, so every background turn is refused before its session starts:

Background session _consolidate: kirocrew-lite spec mounts a tool, a grant, an MCP server or a hook; not starting task=consolidation

Nothing puts the file back until the next rebuild, so history consolidation stops for good: #33345 counts 1,401 refusals and no consolidation in five days, and a new session's Recent History stops on the day the tool started.

Why it matters

Everyone who runs such a tool loses history consolidation and the other background helpers (titles, summaries, skill dedupe), and editing the file by hand does not last because the tool writes it back on its next launch. The background agent reads text Kiro Crew does not author (chat history, issue bodies, transcripts), so the fix must never let it start on a spec that mounts a tool.

Not a goal

What changed (motivation → approach → change)

The refusal is right; what was missing is a way back. kirocrew-lite.json is Kiro Crew's own file and only a rebuild re-derives it, so once another tool writes into it every background turn is refused until then. The guest spec already has a repair before it would refuse; the lite spec now runs through that same repair code before the helper would refuse.

Part What it does
repair_lite_agent_spec (agent_materialization/service_agents.py) Rewrites an existing kirocrew-lite.json that is not its template and says whether it did, through the same governed writer the installer uses, with a WARNING that names the differing keys and never their values. A missing file stays missing, and a data home that does not own the agents directory leaves the file alone. It and repair_guest_agent_spec share one body (_repair_spec_from_template), and the installer and the repair build the spec from one _lite_agent_config(). Exported through the kiro_crew.agent facade (agent.py).
reset_after_spec_repair (agent_materialization/service_agents.py) The one reset loop both repairs run after a rewrite. Each reset is bounded (_SPEC_REPAIR_RESET_TIMEOUT_SECS, 30 s), so one hung teardown cannot hold every turn on that spec refused. A provider whose reset raises or times out stays in that spec's failed-reset map, so the clean file never vouches for it. The guest path in messaging/dispatch_tools.py calls it in place of its own loop, with the same bound it had.
background_turn (llm_helpers_parts/background_turns.py) When the read before the spawn says the lite spec mounts something, runs the repair off the event loop and reads the spec again. That second read decides: a spec that still mounts something is refused as before. A session that started before the rewrite may have loaded the old file, so only the first read vouches for a session, and only while no rewrite is running. A rewrite resets the _bg and _consolidate sessions, the two that outlive a turn and can start without the pre-spawn read, the turn's own when it is one of them, and those resets finish even when the turn is cancelled. On a backend with no prepared projection, which cannot tell such a session from a new one, the turn that rewrote the file is refused and its own session reset, and so is a turn whose read before the spawn overlapped a rewrite or was followed by one before its check after the start. A session left unconfirmed, because its turn was cancelled inside that check or a reset of it raised or timed out, is recorded by key in _lite_resets_failed: the next turn on that same session is refused and resets it, and only a reset that returns forgets it. The lite counters, task set and failed-reset map stay separate from the guest's, so a lite repair never refuses a channel turn. The vouching rule differs from the guest's because _bg can start outside any turn (_ensure_background), so a session the second read sees may still have loaded the old file; a guest session only starts inside a guest turn. The next turn starts on the rewritten file. The new names are listed in the llm_helpers.py facade manifest.
_ensure_background_single (session_background.py) A _bg cold start still in flight when a reset of _bg runs, the rewrite's reset included, is shut down instead of registered, so no later turn can claim a session that loaded the old file. The start reads the key's ownership generation before the spawn, and a reset moves it even with nothing registered yet.

src/kiro_crew/docs/agent-spec-fields.md describes the repair beside the tool-less check, docs/system-specs/modules/session.md says a _bg start a reset overtook is not registered, and docs/system-specs/modules/messaging.md names the shared reset bound for the guest repair.

Backwards compatibility

Compatible: a lite spec that mounts nothing is neither rewritten nor read a second time, and a spec the repair cannot restore is refused exactly as on main. The guest repair keeps its 30 s reset bound and its rewrite log lines. Two things differ there: a reset the guest repair runs that returns now forgets its failed-reset entry only when that entry is still the provider it reset, so a newer provider a refusal recorded meanwhile stays refused; and the warning for a reset that fails reads Could not reset session <key> after its agent spec was rewritten on the kiro_crew.agent logger. Any reset of _bg that overtakes an _ensure_background cold start now discards that start, and its caller finds no _bg, as after a failed start.

Removes nothing: the rebuild already rewrites kirocrew-lite.json from this same template on every boot; this rewrites it once more, only when a background turn would otherwise be refused, and every other session opened on kirocrew-lite (the nudge.wake judge, workflows, the prompt optimizer) is built for that same tool-less template. The guest reset bound moves from messaging/dispatch_tools.py to agent_materialization/service_agents.py with the same value; every reader of it is in this diff.

Tests

  • test/test_messaging_dispatch.py, TestBackgroundTurnRefusesATooledLiteSpec:
    • test_an_injected_server_is_rewritten_away_and_the_next_turn_runs: the issue's injected tools, allowedTools, mcpServers, permissions and toolsSettings are rewritten back to the template; the _bg turn right after the rewrite, on a session that was already running, resets _bg and _consolidate, is refused and has its own session reset, and the next turn starts.
    • test_a_rewrite_resets_both_background_sessions: a rewrite during a _bg, _consolidate or memory-store turn resets both persistent background sessions, then that turn's own.
    • test_a_hung_reset_does_not_hold_the_rewrite: a rewrite whose resets never return gives up on them after the bound; the turn that triggered it is refused, the rewrite finishes, and a later _bg turn runs.
    • test_a_turn_reading_the_file_while_a_rewrite_resets_is_refused: a second _bg turn whose read sees the rewritten file while the rewrite is still resetting sessions is refused and never runs.
    • test_a_rewrite_begun_after_the_pre_spawn_read_voids_it: a turn that read the template, then had the server injected and the file rewritten by another turn before its check after the start, is refused.
    • test_a_cancelled_turn_still_finishes_its_rewrite_resets: a turn cancelled while its rewrite resets the first session leaves both resets to finish before another turn's read vouches again.
    • test_a_turn_cancelled_in_its_check_after_a_repair_leaves_its_session_refused: a _consolidate cold start that another turn's repair overtakes, cancelled inside its check after the start, leaves the next turn on that session refused and the session reset although the file and the pre-spawn read are clean; the turn after runs.
    • test_a_session_a_failed_reset_left_registered_stays_refused[repair] and [refusal]: a repair's or a refusal's reset that raised leaves the next turn on that session refused although the file reads clean; once a reset returns, the turn after runs.
    • test_the_read_after_the_rewrite_still_decides: a rewrite the file does not keep is refused before any session starts.
    • The refusal tests (a tooled spec on a cold or warm start, an MCP server alone, includePowers, a hardlinked spec) decline the repair the way a data home that does not own the agents directory does, and still refuse before any session starts.
    • Two specs claiming the name with no kirocrew-lite.json are still refused, and no third file is created.
  • test/test_messaging_dispatch.py, TestToollessTurnReadsTheGuestSpecOnDisk: test_a_hung_reset_does_not_hold_the_repair and test_a_session_whose_reset_failed_stays_unconfirmed set the shared bound to 0 and pass unchanged otherwise.
  • test/test_session.py, TestWarmPool::test_a_background_start_overtaken_by_a_reset_is_shut_down: a reset of _bg while its cold start is in flight leaves _bg unregistered and shuts that start's provider down.
  • test/test_agent_spec_hardened_reads.py: the comment over the strict reader's service_agents.py call site names the lite repair beside the knowledge and guest ones.

Proof: PROVEN test/test_messaging_dispatch.py::TestBackgroundTurnRefusesATooledLiteSpec::test_an_injected_server_is_rewritten_away_and_the_next_turn_runs, test/test_messaging_dispatch.py::TestBackgroundTurnRefusesATooledLiteSpec::test_a_rewrite_resets_both_background_sessions[_bg], test/test_messaging_dispatch.py::TestBackgroundTurnRefusesATooledLiteSpec::test_a_rewrite_resets_both_background_sessions[_consolidate], test/test_messaging_dispatch.py::TestBackgroundTurnRefusesATooledLiteSpec::test_a_rewrite_resets_both_background_sessions[memory-consolidation:x], test/test_messaging_dispatch.py::TestBackgroundTurnRefusesATooledLiteSpec::test_a_turn_reading_the_file_while_a_rewrite_resets_is_refused (head 3e80b0f)

Red check: the tests in the Proof: line fail with the production hunks put back to the merge base (prove.py). test_a_hung_reset_does_not_hold_the_rewrite fails with only the bound removed from reset_after_spec_repair (the turn waits on the rewrite until the test's own 60 s limit). Each concurrency test fails with only its own part of the fence removed: test_a_turn_reading_the_file_while_a_rewrite_resets_is_refused without the count of repairs in flight, test_a_rewrite_begun_after_the_pre_spawn_read_voids_it without the re-check after the start, test_a_cancelled_turn_still_finishes_its_rewrite_resets without the shield (assert [0, 0] == [1, 0]), test_a_background_start_overtaken_by_a_reset_is_shut_down without the generation check (assert '_bg' not in {'_bg': _Session(...)}), and test_a_turn_cancelled_in_its_check_after_a_repair_leaves_its_session_refused plus both cases of test_a_session_a_failed_reset_left_registered_stays_refused without _lite_resets_failed (the next turn runs; DID NOT RAISE). With the fix every test of the lite class and of TestWarmPool passes.

Determinism: the lite class, the guest class TestToollessTurnReadsTheGuestSpecOnDisk, TestWarmPool and test/test_agent_spec_hardened_reads.py (152 tests) passed 20 runs at -n0 and 10 runs at -n 4, each run in its own shuffled order.

Manual verification

Verified on a clean host

Ubuntu 24.04.5 LTS (x86_64), Python 3.12.3. This host has no signed-in ACP backend, so a real SessionManager drove the real background_turn that history consolidation uses (task="consolidation", agent="kirocrew-lite", session_key="_consolidate"). The only stub was the provider its factory returns: a mock that carries a real, unstarted AcpClient with no prepared projection. The installer (rebuild_agent_config) wrote kirocrew-lite.json into an isolated HOME/KIROCREW_HOME, and the issue's creds-agent entries were then written into that file.

Before (f9fb5aa): every turn is refused, nothing is spawned, and the file keeps the injected entries:

WARNING kiro_crew.llm_helpers: Background session _consolidate: kirocrew-lite spec mounts a tool, a grant, an MCP server or a hook; not starting task=consolidation
## turn 1: REFUSED ... providers spawned during turn = 0 ... spec_is_toolless(on disk) = False
## turn 2: REFUSED ... providers spawned during turn = 0 ... spec_is_toolless(on disk) = False

After (3e80b0f), same script unchanged:

WARNING kiro_crew.agent: Background agent spec .../kirocrew-lite.json was not the spec Kiro Crew writes (differs in 'allowedTools', 'mcpServers', 'permissions', 'tools', 'toolsSettings'); rewrote it from the template
## turn 1: REFUSED (no prepared projection: the rewriting turn is refused and its session reset, as designed); spec on disk = template
## turn 2: RAN (session acquired, body entered)
## turn 3: RAN
  • The WARNING names keys only; no injected value appears in any log line or on disk afterwards.
  • Negative check: when the repair writer declines (SharedAgentHomeRefused, a data home that does not own the agents directory), the file keeps the injected entries and every turn is still refused before the spawn, with 0 providers started.
  • Happy path: a clean lite spec runs on its first turn, with no rewrite and no WARNING.

Gates on 3e80b0f (umask 022):

  • PR tests (test_messaging_dispatch.py, test_session.py, test_agent_spec_hardened_reads.py): 197 passed on HEAD; the 16 new or changed tests fail on f9fb5aa.
  • Profile gates[] Python entries (0-43, 60-61): all exit 0 except the scoped backend run (1 failed out of 17,957 run). That failure, ops_mission_control ... test_github_marks_a_full_page_as_truncated, also fails on a clean origin/main, so it predates this PR. Entries 45-59 and setup[] (frontend/Playwright) were skipped because the diff has no website/ change.
  • scripts/local-gate.py: backend batches 2, 3, 6 and 7 green. Batches 1, 4 and 5 failed only on tests that fail the same way on a clean origin/main (the ops_mission_control test above, test_host_git_repo_exec_neutralization ... test_up_to_date_diff_runs_no_textconv_planted_during_the_fetch, and two EIO-injection errors in test_live_target_pointer_visibility / test_sandbox_mount_pinned_target). Its 4 frontend vitest batches were not run because the diff has no website/ change.
  • semgrep 1.78.0 (--baseline-commit merge-base, --error): exit 0
  • test/test_flake_pattern_ratchet.py: exit 0
  • prove.py --base f9fb5aa2a: Proof: PROVEN test/test_messaging_dispatch.py::TestBackgroundTurnRefusesATooledLiteSpec::test_an_injected_server_is_rewritten_away_and_the_next_turn_runs, ...[_bg], ...[_consolidate], ...[memory-consolidation:x], ...test_a_turn_reading_the_file_while_a_rewrite_resets_is_refused (head 3e80b0fbe1e7)

Related Issues

Closes #33345

Sibling sweep

Swept: git grep -nE "authored_spec_toolless_verdict(|authored_spec_is_toolless(|authored_spec_declares_zero_tools|effective_spec_declares_zero_tools" -- src/
Sibling: src/kiro_crew/llm_helpers_parts/background_turns.py -- changed here
Sibling: src/kiro_crew/messaging/dispatch_tools.py:213 -- changed here
Sibling: src/kiro_crew/knowledge/llm_pool.py -- not affected -- repair_knowledge_agent_spec rewrites kirocrew-knowledge.json before each worker spawn, and creates a missing file, so it keeps its own body

Swept: git grep -nE "background_key not in self._owner._sessions|_sessions.get(background_key) is session" -- 'src/kiro_crew/*.py'
Sibling: src/kiro_crew/session_background.py:487 -- changed here

Pattern harvest

Rule candidate: review-prompt
Pattern: a fail-closed check on a file Kiro Crew re-derives only at rebuild, with no repair of that file before the refusal

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

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

@bolichen97
bolichen97 requested review from a team and KiroCrewCaptain as code owners November 9, 2026 16:15
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 3e80b0fbe1e7081ba8950331cde24133a13a729b 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 Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks: the change reuses the guest spec's repair-before-refuse path for the lite spec, and the refusal still comes from a second read of the spec.

Notes

  • The state behind the vouching rule is spread across _lite_rewrites, _lite_rewrite_tasks and _lite_resets_failed, which are module-level dicts. A small object owned by SessionManager that holds these per spec would be easier to keep correct the next time a spec gets this repair.

[DESIGN-REVIEWED] 3e80b0f

Description read

[DESCRIPTION-READ] 8e57df1dd54bebee6b29838b6425d68ebae2acff19fdf8345096117e8c15c1ec

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 Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 3e80b0fbe1e7081ba8950331cde24133a13a729b — 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

Nothing blocks: the rewrite reuses the guest repair's body and reset loop, and the _bg generation guard is the fix's own race closure.

What this change ships

Inventory (7 items) — 7 justified

Intent: keep history consolidation and the other background helpers running after another tool writes an MCP server into kirocrew-lite.json, without ever starting a tooled lite session (#33345). FIX.

  1. A background turn that would be refused for a tooled lite spec first rewrites that spec from its template, then reads it again, and the second read decides — justified
  2. After a rewrite, _bg and _consolidate are reset, each reset capped at 30 s — justified
  3. With no prepared projection, the turn that triggered the rewrite is refused and its session reset; the next turn runs — justified
  4. A session whose check was cancelled, or whose reset failed, is refused and reset on its next turn (_lite_resets_failed) — justified
  5. A _bg cold start that a reset overtakes is shut down instead of registered — justified
  6. The guest repair now goes through the shared reset_after_spec_repair; its warning text and logger change, and a newer failed-reset entry is kept — justified
  7. The reset bound constant moves from dispatch_tools to service_agents — justified

[FIRST-PRINCIPLES-REVIEWED] 3e80b0f

Description read

[DESCRIPTION-READ] d77b2500a2cd409b8913a12cf221120dda35a26c4dc43df8a3734a0074bd961e

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.

@bolichen97
bolichen97 enabled auto-merge (squash) November 9, 2026 16:17
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 3e80b0fbe1e7081ba8950331cde24133a13a729b — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 3e80b0f

Verdict parsed from the review's SHA-scoped output markers for commit 3e80b0fbe1e7081ba8950331cde24133a13a729b.

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

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 3e80b0fbe1e7081ba8950331cde24133a13a729b and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 3e80b0f

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

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

Approved at f6dc330. When another tool injects an MCP server into kirocrew-lite.json, the background helper now rewrites the file from its template and re-reads it before refusing (src/kiro_crew/llm_helpers_parts/background_turns.py:593-619, src/kiro_crew/agent_materialization/service_agents.py:422-469). Only the first read vouches for a live session, and _bg/_consolidate are reset after a rewrite, so the tool-less boundary still fails closed while consolidation recovers without a rebuild.

@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 Nov 9, 2026
buluoray
buluoray previously approved these changes Nov 10, 2026

@buluoray buluoray 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 on f6dc330. When another tool injects an MCP server into kirocrew-lite.json, the background helper now rewrites the file from its template and re-reads it before refusing (llm_helpers_parts/background_turns.py:593-619, agent_materialization/service_agents.py:422-469). Only the first read vouches for a live session, and _bg/_consolidate reset after a rewrite, so the tool-less boundary still fails closed while consolidation recovers without a rebuild. The remaining red is the main-wide lazy-import check, not this diff.

@bolichen97

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in src/kiro_crew/agent.py, src/kiro_crew/agent_materialization/service_agents.py, src/kiro_crew/llm_helpers_parts/background_turns.py, src/kiro_crew/docs/agent-spec-fields.md and test/test_agent_spec_hardened_reads.py (and 343 files they import) since this head's CI ran (2caa69b -> ddb535e); CI re-running.

The red Backend Tests (3.12, 6), Backend Tests (Windows) (6) and Coverage Gate on f6dc330 were main's: test/test_dashboard_server_composition_contract.py::test_the_lazy_imports_stay_inside_the_functions_that_need_them failed with assert 143 == 142 on main at c5758a3 too (https://github.com/kirodotdev/KiroCrew/actions/runs/42652599694). #34010 re-pinned it, and the new base carries that fix.

The PR's added and removed lines are unchanged from the approved f6dc330; only their context moved. The push dismisses that approval, so the new head needs a fresh one.
[operator: bolichen97]

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 11, 2026
@bolichen97
bolichen97 force-pushed the fix/33345-lite-spec-repair branch from 31c4c75 to 1c28e6f Compare November 11, 2026 14:36
@bolichen97

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in test/test_agent_spec_hardened_reads.py, src/kiro_crew/agent_materialization/service_agents.py, src/kiro_crew/docs/agent-spec-fields.md since this head went green (ddb535e -> 8759744); CI re-running.
[operator: bolichen97]

@bolichen97

bolichen97 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: a pre-spawn read of the lite spec vouching for a warm session across a rewrite

  • Repair leaves a tooled runtime available to concurrent turns span=2937da9ef451

fixed in 1c28e6f: a rewrite resets both _bg and _consolidate, the repairing turn's own key included, in a shielded task, so a cancelled turn cannot end it before both resets are done.
A count of repairs in flight fences the vouch: a pre-spawn read that returns while one runs vouches for nothing, and a turn whose check after the start finds a repair begun since its read re-checks unvouched, so on a backend with no projection both are refused before they yield.
Pinned by test_a_turn_reading_the_file_while_a_rewrite_resets_is_refused (this trigger), test_a_rewrite_begun_after_the_pre_spawn_read_voids_it and test_a_cancelled_turn_still_finishes_its_rewrite_resets, each red with its part of the fence removed.
[operator: bolichen97]

@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 11, 2026
@KiroCrewCaptain

KiroCrewCaptain commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: the decider thinks the change is probably fine but estimates an 18% chance that this change introduces a defect, above the 12% limit for approving on its own.

The PR adds a template rewrite of kirocrew-lite.json before a background turn would be refused, then resets _bg and _consolidate. A second read still decides whether the turn runs, so the refusal stays fail-closed. Verified in the code: the vouching fence (the running/begun counts plus the _lite_resets_failed map); the reset advancing _bg's generation even with nothing registered (teardown.py:249), which the _ensure_background_single guard relies on; a discarded _bg start falling through to a normal cold start in get_or_create; the installer and the repair sharing _lite_agent_config(), so a correct file is never seen as drifted; and the guest path keeping its bound and its set of reset sessions. Tests cover each new branch. No regression or trust-boundary problem found.

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit 3e80b0fbe1 · 633 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@buluoray buluoray 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 on 1c28e6f. The new fence still misses one path. A _bg cold start that loaded the injected spec, and registers after the repair's resets ran, is later confirmed by a turn whose reads both see the restored template. On a backend with no prepared projection, that turn runs on a runtime with the injected server mounted, and main would have refused it. GPT 6.1's fenced security finding still holds at this head. Decisive lines: src/kiro_crew/llm_helpers_parts/background_turns.py:529-535, src/kiro_crew/session_background.py:466-470, src/kiro_crew/acp/client_mcp_array.py:228-231, src/kiro_crew/llm_helpers_parts/background_turns.py:652,698

@bolichen97
bolichen97 force-pushed the fix/33345-lite-spec-repair branch from 1c28e6f to 646a3bc Compare November 11, 2026 20:14
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Nov 11, 2026
@bolichen97

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: a reset of _bg that an in-flight _ensure_background cold start outlives

  • Repair misses background providers still starting span=2937da9ef451

fixed in 646a3bc: _ensure_background_single reads _bg's ownership generation after its start permit and before the spawn, and registers only if it is unchanged; otherwise the provider is shut down on the existing lost-race path.
reset advances that generation even when nothing is registered, so the repair's reset of a _bg still starting makes that start stale: it never registers, and a later claimant starts _bg after the rewrite, on the template.
That start was the one path that registers a background agent session no turn has claimed; recycle_background adopts only into the session object a reset pops.
Pinned by test/test_session.py::TestWarmPool::test_a_background_start_overtaken_by_a_reset_is_shut_down, red without the check.

[operator: bolichen97]

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 11, 2026
@bolichen97
bolichen97 force-pushed the fix/33345-lite-spec-repair branch from 81c5e6d to d1870ae Compare November 12, 2026 15:48
@bolichen97

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in src/kiro_crew/llm_helpers.py, test/test_agent_spec_hardened_reads.py, docs/system-specs/modules/session.md and src/kiro_crew/docs/agent-spec-fields.md since this head's CI ran (e1b079c -> f9fb5aa); CI re-running.

The red Backend Tests (3.12, 2), Backend Tests (Windows) (2) and Coverage Gate on 81c5e6d were main's: test/test_cloud_launch_customize.py::TestAmiArchitectureCheck::test_matching_architecture_proceeds_to_deploy failed on main too, and #35746 fixed it (#35745). The new base carries that fix; the test class passes locally on the new head.

The new head also drops one redundant check from repair_lite_agent_spec: the os.path.lexists early return. _spec_drift already reports a missing file as "missing", which returns False, so a missing lite spec file is still never created. The function now has the same shape as repair_guest_agent_spec. Nothing else in the patch changed (git range-diff shows the rest identical).
[operator: bolichen97]

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Nov 12, 2026
A tool that writes its own MCP server into every agent spec under the
agents directory leaves kirocrew-lite.json mounting a server. The
background helper correctly refuses to start a session on such a spec,
but nothing put the file back before the next rebuild, so every
background turn, history consolidation included, was refused.

Before refusing, the helper now rewrites an existing kirocrew-lite.json
that is not its template, through the same governed writer the
installer uses, and reads the spec again. That second read still
decides; a spec that still mounts something is refused as before, and
a missing file is never created. The rewrite and the session resets
after it run through the same code the guest spec repair uses, so each
reset is bounded and one hung teardown cannot hold background turns.

A session that started before the rewrite may have loaded the old
file, so only the read before the rewrite vouches for a session, and
not while a rewrite runs. A rewrite resets the _bg and _consolidate
sessions, the two that outlive a turn and can start without the
pre-spawn read, the turn's own included, and those resets finish even
when the turn is cancelled. On a backend with no prepared projection,
which cannot tell such a session from a new one, the turn that rewrote
the file is refused and its own session reset, and so is a turn whose
pre-spawn read overlapped a rewrite. A _bg start still in flight when
the rewrite resets _bg is shut down instead of registered, so no later
turn can take a session that loaded the old file. The next turn starts
on the rewritten file.

Closes #33345
@bolichen97
bolichen97 force-pushed the fix/33345-lite-spec-repair branch from d1870ae to 3e80b0f Compare November 12, 2026 22:50
@bolichen97

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: a second copy of the guest spec repair body and its post-repair reset loop, written for the lite spec
resolution: narrowed -- the lite repair body and reset loop run through the one copy the guest repair uses; the per-spec counters, task set and failed-reset map stay

  • Second copy of the toolless-spec repair mechanism span=a8970e175eeb

fixed in 3e80b0f: one repair body (_repair_spec_from_template) and one bounded reset loop (reset_after_spec_repair, _SPEC_REPAIR_RESET_TIMEOUT_SECS = 30 s) in agent_materialization/service_agents.py now serve both specs; repair_guest_agent_spec, repair_lite_agent_spec, _restore_guest_spec and _rewrite_lite_spec call them, so the drift is gone and the lite resets are bounded like the guest's.
Pinned by test_a_hung_reset_does_not_hold_the_rewrite, red without the bound (the turn waits on the rewrite past its 60 s limit).
Rebutted for the state half: the counters, task set and failed-reset map stay per spec. _refuse_unless_toolless refuses and resets every guest turn whose repair count moved or whose repair is still running, so a shared count would refuse channel turns on every lite repair though no guest file or session changed.
The lite path cannot read the guest state either: test_channel_turn_structure.py::test_the_export_budget_may_only_fall caps the names imported from the channel pipeline at today's 31.
The lite vouching rule differs on purpose: _bg starts outside any turn (_ensure_background, not visible in session_keys()), so lite resets _bg/_consolidate by key and only its first read vouches; a guest session starts only inside a guest turn.

[operator: bolichen97]

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Nov 12, 2026
@bolichen97

Copy link
Copy Markdown
Contributor Author

Review state on 3e80b0fbe1, for @iamwhatever:

  • AI lanes pass on this head: GPT 6.1, Opus 5.5, Design and First Principles; UX is skipped.
  • The path from the earlier change request is still fenced. It was the _bg cold start that loaded the injected spec and registered after the repair's reset. That was closed on 81c5e6d056, and the fence is unchanged here: the session-generation check in src/kiro_crew/session_background.py (lines 444, 488, 496 and 506), pinned by test_a_background_start_overtaken_by_a_reset_is_shut_down in test/test_session.py:637. The only change since that head moves the post-repair reset into one helper, reset_after_spec_repair, shared by the guest and lite repairs.
  • Local tests: the three touched test files pass on this head (197 passed).
  • CI: the one red, CodeQL "Analyze (javascript-typescript)", failed on the installation rate limit. Its re-run is run 43323635870.

AI-assisted, sent by CommunityCrew

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention labels Nov 13, 2026

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Checked on this head:

  • Before refusing a turn, the repair rewrites an existing kirocrew-lite.json only when it differs from _lite_agent_config(), the same config the installer writes from. A second read then decides whether the turn runs.
  • The regression test test_an_injected_server_is_rewritten_away_and_the_next_turn_runs is red on main and green here.
  • Each part of the concurrency fence (running and begun counts, the shield, _lite_resets_failed, the _bg generation check) has a test that fails when that part is removed.
  • No new blocking-call or determinism problem. The guest repair now goes through the shared reset_after_spec_repair and keeps its 30 s bound.

Security: the refusal still fails closed. A missing or linked file is never rewritten. A pre-spawn read taken during or before a repair vouches for nothing. A failed or cancelled reset leaves its provider refused. A _bg cold start that a reset overtook is shut down, not registered. So no session ever starts on a spec that mounts an injected server.

Well done: the guest repair's copied reset loop is gone, and both specs now go through one repair body.

Optional:

  • service_agents.py:164: the new "keep a newer failed provider" rule has no test. If a repair's reset stub records a second provider whose own reset raises, a test could check that the entry survives. That test would go red on a plain pop.
  • background_turns.py:665: if a member-store consolidation turn is cancelled during the shielded repair wait, _cleanup_memory_consolidation_session never runs. The except BaseException cleanup that get_or_create already has could cover this block too.
  • background_turns.py:708: the lite tests never give a fresh provider after a failed reset, so the identity check could be loosened to key in _lite_resets_failed and no test would fail.

@bolichen97
bolichen97 merged commit ca95636 into main Nov 13, 2026
164 of 169 checks passed
@bolichen97
bolichen97 deleted the fix/33345-lite-spec-repair branch November 13, 2026 10:51
bolichen97 added a commit that referenced this pull request Nov 13, 2026
A history consolidation that never reaches a provider (kiro-cli
missing, or the lite spec refused before its session starts, as in
#33345) arms the session's environment backoff, and that wait widens
to a day. Nothing released it when the host recovered, so after the
#33965 fix landed every session refused meanwhile still sat out up to
24 hours before consolidating.

The first consolidation turn a provider answers after a process start,
after the startup scan adds sessions, or after any environment failure
now releases the environment backoff of every tracked session. A turn
that raised releases nothing. Each session is released once until it
next consolidates and keeps its failure count, so a session whose
failure is its own resumes widening to the ceiling. A session with a
billed attempt on its current span keeps its wait, and nothing is
marked consolidated.

Part of #33345
bolichen97 added a commit that referenced this pull request Nov 13, 2026
A history consolidation that never reaches a provider (kiro-cli
missing, or the lite spec refused before its session starts, as in
#33345) arms the session's environment backoff, and that wait widens
to a day. Nothing released it when the host recovered, so after the
#33965 fix landed every session refused meanwhile still sat out up to
24 hours before consolidating.

The first consolidation turn a provider answers after a process start,
after the startup scan adds sessions, or after any environment failure
now releases the environment backoff of every tracked session. A turn
that raised releases nothing. Each session is released once until it
next consolidates and keeps its failure count, so a session whose
failure is its own resumes widening to the ceiling. A session with a
billed attempt on its current span keeps its wait, and nothing is
marked consolidated.

Part of #33345
bolichen97 added a commit that referenced this pull request Nov 13, 2026
A history consolidation that never reaches a provider (kiro-cli
missing, or the lite spec refused before its session starts, as in
#33345) arms the session's environment backoff, and that wait widens
to a day. Nothing released it when the host recovered, so after the
#33965 fix landed every session refused meanwhile still sat out up to
24 hours before consolidating.

The first consolidation turn a provider answers after a process start,
after the startup scan adds sessions, or after any environment failure
now releases the environment backoff of every tracked session. A turn
that raised releases nothing. Each session is released once until it
next consolidates and keeps its failure count, so a session whose
failure is its own resumes widening to the ceiling. A session with a
billed attempt on its current span keeps its wait, and nothing is
marked consolidated.

Part of #33345
bolichen97 added a commit that referenced this pull request Nov 13, 2026
…36632)

A history consolidation that never reaches a provider (kiro-cli
missing, or the lite spec refused before its session starts, as in
#33345) arms the session's environment backoff, and that wait widens
to a day. Nothing released it when the host recovered, so after the
#33965 fix landed every session refused meanwhile still sat out up to
24 hours before consolidating.

The first consolidation turn a provider answers after a process start,
after the startup scan adds sessions, or after any environment failure
now releases the environment backoff of every tracked session. A turn
that raised releases nothing. Each session is released once until it
next consolidates and keeps its failure count, so a session whose
failure is its own resumes widening to the ceiling. A session with a
billed attempt on its current span keeps its wait, and nothing is
marked consolidated.

Part of #33345
bolichen97 added a commit that referenced this pull request Nov 13, 2026
…ox tool (#33345) (#36777)

* fix: rewrite the guest spec from its template before refusing a turn (#34253)

A tool that writes its own MCP server into every agent spec under the
kiro agents directory leaves kirocrew-guest.json mounting that server,
and a deny_all_tools channel turn refuses before its spawn on a guest
spec that mounts anything. Only a rebuild put the file back, so every
non-operator turn was refused until then.

When the pre-spawn read refuses, the channel pipeline now rewrites an
existing kirocrew-guest.json that is not its template through the same
governed writer the installer uses, resets every live session bound to
the guest agent, and reads the spec again; that second read decides.
A live guest process may still mount the injected server after the
file reads clean, since kiro-cli applies a reload only at a turn
boundary and not on every version. A turn whose
spawn overlaps a repair, which the resets may not reach, is refused:
the count of started repairs is read before the vouching read and the
read after the start confirms only when no repair started since and
none is running. Each reset is bounded so a hung teardown cannot keep
every guest turn refused. A missing file is not created. _spec_drift takes its
reader's operation label from the caller.

Closes #33522

(cherry picked from commit 387ea66)

* fix: rewrite the lite spec from its template before refusing a turn (#33965)

A tool that writes its own MCP server into every agent spec under the
agents directory leaves kirocrew-lite.json mounting a server. The
background helper correctly refuses to start a session on such a spec,
but nothing put the file back before the next rebuild, so every
background turn, history consolidation included, was refused.

Before refusing, the helper now rewrites an existing kirocrew-lite.json
that is not its template, through the same governed writer the
installer uses, and reads the spec again. That second read still
decides; a spec that still mounts something is refused as before, and
a missing file is never created. The rewrite and the session resets
after it run through the same code the guest spec repair uses, so each
reset is bounded and one hung teardown cannot hold background turns.

A session that started before the rewrite may have loaded the old
file, so only the read before the rewrite vouches for a session, and
not while a rewrite runs. A rewrite resets the _bg and _consolidate
sessions, the two that outlive a turn and can start without the
pre-spawn read, the turn's own included, and those resets finish even
when the turn is cancelled. On a backend with no prepared projection,
which cannot tell such a session from a new one, the turn that rewrote
the file is refused and its own session reset, and so is a turn whose
pre-spawn read overlapped a rewrite. A _bg start still in flight when
the rewrite resets _bg is shut down instead of registered, so no later
turn can take a session that loaded the old file. The next turn starts
on the rewritten file.

Closes #33345

(cherry picked from commit ca95636)

* fix: release the stale consolidation backoff once a turn is answered (#36632)

A history consolidation that never reaches a provider (kiro-cli
missing, or the lite spec refused before its session starts, as in
#33345) arms the session's environment backoff, and that wait widens
to a day. Nothing released it when the host recovered, so after the
#33965 fix landed every session refused meanwhile still sat out up to
24 hours before consolidating.

The first consolidation turn a provider answers after a process start,
after the startup scan adds sessions, or after any environment failure
now releases the environment backoff of every tracked session. A turn
that raised releases nothing. Each session is released once until it
next consolidates and keeps its failure count, so a session whose
failure is its own resumes widening to the ceiling. A session with a
billed attempt on its current span keeps its wait, and nothing is
marked consolidated.

Part of #33345

(cherry picked from commit 0e5b203)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: passed Eligible automated validation passed for the current revision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

History consolidation never runs when a credentials sandbox tool injects creds-agent into kirocrew-lite.json

3 participants