From bf4563dc881da715f53b32449e7736af4c9b6078 Mon Sep 17 00:00:00 2001 From: Ray Xu <38930179+buluoray@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:03 -0700 Subject: [PATCH 1/2] docs(decisions): every crewmate on a user-level agent moves to the conductor (#25332) * docs(decisions): every crewmate on a user-level agent moves to the conductor * docs(decisions): a restriction the crewmate file cannot carry moves with it (cherry picked from commit 6512997ba805114a4de94736f6af1ed9b50def65) --- ...user-level-agent-moves-to-the-conductor.md | 20 +++++++++++++++++++ docs/decisions/README.md | 1 + 2 files changed, 21 insertions(+) create mode 100644 docs/decisions/2026-10-28-every-crewmate-on-a-user-level-agent-moves-to-the-conductor.md diff --git a/docs/decisions/2026-10-28-every-crewmate-on-a-user-level-agent-moves-to-the-conductor.md b/docs/decisions/2026-10-28-every-crewmate-on-a-user-level-agent-moves-to-the-conductor.md new file mode 100644 index 000000000000..65f55d9c7e0f --- /dev/null +++ b/docs/decisions/2026-10-28-every-crewmate-on-a-user-level-agent-moves-to-the-conductor.md @@ -0,0 +1,20 @@ +# Every crewmate still bound to a user-level agent moves onto its conductor, and a restriction its file cannot carry moves with it and is named in its notice + +Decided by: Ray (project owner, @buluoray) +Date: 2026-10-28 +Supersedes: 2026-10-27-every-used-crewmate-moves-to-the-conductor.md + +## Decision + +The startup migration moves every crewmate still bound to an agent spec in the user-level agents folder onto its conductor and its own crewmate file: a loss moves with the one-time notice; a grant the worker would gain is never written and is offered only through the capability card; a `hooks`, `permissions` or `toolsSettings` restriction the crewmate file cannot carry no longer keeps the crewmate where it is and withholds no tool, the crewmate moves as it is, and its one-time notice names each restriction that no longer applies; the governance ceiling still governs the merged worker spec; a crewmate whose id is shadowed by a Kiro Crew-owned private copy moves after that copy is retired, one shadowed by a spec Kiro Crew does not own stays and is told once; a used row with no `member_id` is given one and its own V2 store before it moves; and the shared-template governance pass never writes a spec Kiro Crew does not own. + +## Why + +- A row left on a user-level agent made the shared-template governance pass rewrite the user's own file, which kiro-cli also loads outside Kiro Crew, so the user's own sessions lost their auto-approvals and a package manager reinstalling the spec fought the rewrite on every restart. +- Keeping a crewmate on its old agent for a restriction its file cannot carry left the crewmate doing its work in its chat, the opposite of what the upgrade notice describes; the user is told which restrictions no longer apply, and the governance ceiling still bounds what the worker may do. +- This replaces only the earlier entry's rule that a hook or a `permissions` / `toolsSettings` restriction keeps the crewmate where it is; its rules on losses, gains and unreadable agents stand. + +## Evidence + +- https://github.com/kirodotdev/KiroCrew/issues/25289 -- the reported rewrite of a bound package spec. +- https://github.com/kirodotdev/KiroCrew/pull/25325 -- the pull request that implements this entry. diff --git a/docs/decisions/README.md b/docs/decisions/README.md index 691d8928ad36..18e0b270d6ae 100644 --- a/docs/decisions/README.md +++ b/docs/decisions/README.md @@ -110,4 +110,5 @@ Links to the pull requests, issues and commits that carry the decision. | [2026-10-22-heartbeat-is-a-schedules-tab-row-not-a-goals-check-in.md](2026-10-22-heartbeat-is-a-schedules-tab-row-not-a-goals-check-in.md) | The crewmate's Heartbeat switch is the first row of the Profile card's Schedules tab, not a check-in on the Goals tab. | | [2026-10-22-a-new-goal-starts-in-the-crewmate-chat.md](2026-10-22-a-new-goal-starts-in-the-crewmate-chat.md) | A new goal starts from the Goals tab's Create a goal row as a conversation in the crewmate's chat, with no text editor for it on the tab; the "Add something to track" row is gone. | | [2026-10-27-every-used-crewmate-moves-to-the-conductor.md](2026-10-27-every-used-crewmate-moves-to-the-conductor.md) | On upgrade a crewmate moves onto its conductor even when its worker would lose a grant (told once) or its agent is unreadable (default template); a gain is withheld for the capability card, and a loosened limit still keeps it where it is. | +| [2026-10-28-every-crewmate-on-a-user-level-agent-moves-to-the-conductor.md](2026-10-28-every-crewmate-on-a-user-level-agent-moves-to-the-conductor.md) | Every crewmate still bound to a user-level agent moves onto its conductor; a hook or `permissions` / `toolsSettings` restriction its file cannot carry moves with it and is named in its notice instead of keeping it in place, and the shared-template pass never writes a spec Kiro Crew does not own. Supersedes the 2026-10-27 crewmate-migration entry. | | [2026-10-27-a-crewmate-the-person-created-is-listed-with-an-empty-chat.md](2026-10-27-a-crewmate-the-person-created-is-listed-with-an-empty-chat.md) | A crewmate the person created (on the Mates page, or by approving Mate's card) is listed even with an empty chat; product-made rows still wait for a message, and the reserved `default` member waits for a message the person sent. | From 6b61b1af71728457d969732a188389cd9cc27dfa Mon Sep 17 00:00:00 2001 From: Ray Xu Date: Wed, 28 Oct 2026 17:59:53 +0000 Subject: [PATCH 2/2] fix(agent): never rewrite a crew-bound spec Crew does not own The shared-template governance pass rewrote every crew-bound spec Crew does not own to filter its allowedTools/autoApprove, writing the user's own file in ~/.kiro/agents. kiro-cli loads that same file outside Crew, so the user's own sessions lost those grants, and a package manager reinstalling the spec fought the rewrite on every restart. Per the owner decision superseding the 2026-10-27 migration entry: - The pass never writes such a spec. KAS filters both lists against the live ceiling in its projection; a kiro-cli start on such a file is refused while the governance passes would narrow it (or it cannot be read), so no Crew session auto-approves a ceiling-denied tool. - The startup migration moves the rows that still reach such a file onto the conductor and their own crewmate file. A hooks/permissions/ toolsSettings restriction the file cannot carry no longer keeps a crewmate in place: it moves as is and its one notice names each dropped restriction. A gain is still only offered through the capability card. - A crewmate shadowed by its own private copy (a fork) moves after the copy is retired to ~/.kiro/mates/retired/; one shadowed by a spec Crew does not own stays, is told once, and that spec is never written. - A used row with no member_id (person- or package-written, DM thread holds a turn) is given an id and its own V2 store first, allocated as the upgrade allocates it so it keeps its slug. Closes #25289 (cherry picked from commit e778f7133b1ed2ad8e1f9a10577fc71279c94f86) Backport note: the HARNESS_EXEMPT_SETS row #25325 adds is dropped; that table is not on release/0.11.0 (same as the #25154 backport). --- docs/system-specs/modules/crew-mode.md | 53 +- docs/system-specs/modules/governance.md | 148 +++-- .../modules/harness-onboarding.md | 1 + src/kiro_crew/acp/client_start.py | 11 + src/kiro_crew/acp/harness/kiro.py | 9 + src/kiro_crew/acp/runtime_class.py | 1 + src/kiro_crew/acp/runtime_mode_activation.py | 19 +- src/kiro_crew/acp/runtime_session_create.py | 9 +- src/kiro_crew/acp/runtime_session_load.py | 8 +- src/kiro_crew/acp/runtime_session_setup.py | 24 + src/kiro_crew/acp_backends.py | 2 + src/kiro_crew/agent.py | 6 + .../agent_materialization/auto_approve.py | 21 +- .../agent_materialization/fork_refresh.py | 531 ++++++++++-------- src/kiro_crew/agent_sdk/backend_cards.py | 6 + src/kiro_crew/agent_sdk/backends.py | 4 + src/kiro_crew/agent_sdk/backends_mcp.py | 17 + src/kiro_crew/mate_config_migration.py | 410 +++++++++++++- src/kiro_crew/memory_stores.py | 17 +- test/test_acp_harness_contract.py | 20 + test/test_acp_runtime_session_start.py | 76 +++ test/test_agent_refactor_contracts.py | 4 +- test/test_agent_refactor_fork_refresh.py | 356 ++---------- test/test_bg_runtime_session_start_gate.py | 4 + test/test_conductor_allowed_tools_survive.py | 141 +---- test/test_governed_spec_tail.py | 11 +- test/test_mate_config_migration.py | 355 +++++++++++- test/test_mate_upgrade_notice.py | 29 +- test/test_session_start_gate.py | 4 + test/test_shared_template_unowned_spec.py | 365 ++++++++++++ 30 files changed, 1793 insertions(+), 869 deletions(-) create mode 100644 test/test_shared_template_unowned_spec.py diff --git a/docs/system-specs/modules/crew-mode.md b/docs/system-specs/modules/crew-mode.md index e55782a11364..be9d7816f630 100644 --- a/docs/system-specs/modules/crew-mode.md +++ b/docs/system-specs/modules/crew-mode.md @@ -3909,9 +3909,23 @@ check". **Existing crewmates move at startup** (`kiro_crew.mate_config_migration`, off the loop before the slot restores, never gating readiness). Every crewmate whose -`kiro_agent` does not name its own file is a candidate. A binding with an empty -`member_id` is not a crewmate (an old `assistant`, the built-in -`kirocrew-conductor`): skipped silently. Its roster rows follow the rule for +`kiro_agent` does not name its own file is a candidate +(the 2026-10-28 owner decision in docs/decisions, every crewmate on a user-level agent moves to the conductor +supersedes the 2026-10-27 entry). A binding with an empty `member_id` is not a crewmate +(an old `assistant`, the built-in `kirocrew-conductor`): skipped silently, except a used +row a person or a package sync wrote on a user-level spec Kiro Crew does not own. Such a +row (`source` `kirocrew`, `package` or `aim`; not `default`, the default agent, a legacy +built-in or an app's row) whose DM thread holds a turn (the evidence the synced-crewmate +prune keeps a row on) is first given a `member_id` and its own V2 store +(`_move_with_identity`, `provision_member_memory(upgrade=True)`) once its crewmate file is +written and proven, published together with its repoint in one compare-and-set (a failure +before that publish retires the allocation), the id allocated as the upgrade allocates it +so it keeps the name-derived slug its DM binding and member directory already use; a row +that stays keeps the memory it had. A conversation from before the move keeps the template +and store its own record names, so the notice says a new chat starts on its conductor, and +the agent the crewmate ran on stays one a kiro-cli start is checked on +(`moved_from_agents`, read from the ledger's `threads..from`). A row never chatted +with is left for the prune. Its roster rows follow the rule for any row the person did not create: `GET /api/members` ships it with `dashboard_created` false and the page lists it once `has_dm_message` holds, and `GET /api/agents` (the pickers) lists it only when its DM thread holds a @@ -3926,9 +3940,12 @@ prompt, servers the base lacks with their approvals and selectors, skills, and its restrictions. Before any write, `plan_move` merges the planned file as a worker session would (`differences`; `dry_run` is the same as one sentence) and sorts what differs (`docs/decisions/2026-10-27-every-used-crewmate-moves-to-the-conductor.md`). -A **loosening** keeps the crewmate where it is: a grant the worker would gain, -a switched-off server it would start, a `hooks` entry it would not run, or a -`permissions` / `toolsSettings` restriction it would not keep. Two are cured +A **loosening** keeps the crewmate where it is: a grant the worker would gain +or a switched-off server it would start. A `hooks` entry it would not run or a +`permissions` / `toolsSettings` restriction it would not keep does not: the crewmate +moves as it is, no tool is withheld for it, and its one notice names each restriction +that no longer applies (`dropped_restrictions`, `restrictions_notice`); the governance +ceiling still governs the merged worker spec. The two loosenings are cured first: a server the source switched off is written `{"disabled": true}`, and a grant the worker would gain is taken out of the file (`_strip_grants`), so nothing the old agent lacked is ever granted; the user gets it only through @@ -3947,16 +3964,22 @@ read raw, and the default template's file never loosens them (`_fallback_keeping_limits`): a server it switched off is written `{"disabled": true}`, a tool it excludes or disables goes into `excludedTools`, and a grant it never had is taken out. A hook or a -`permissions` / `toolsSettings` restriction the file cannot carry leaves it -where it is. Governance applies as before: the source is read +`permissions` / `toolsSettings` restriction the file cannot carry moves with it +and is named in its notice. Governance applies as before: the source is read governed, and the worker session still passes POLICY ∩ PROFILE. A crewmate -whose id is also a user-level agent's name, or a spec Kiro Crew generates, is -skipped: its file would never be read, and giving it another file name breaks -every reader that knows a crewmate's file by its binding key -(`mate_capabilities.binding_is_mate`, `member_mate_tools`, -`mate_worker._claimed_file`, the revocations keyed by file id). A binding with -no `member_id` stays out of the migration: a `member_id` on a store that is not -its own V2 store fails every turn (`memory_stores.require_member_memory_store`). +whose id is also a user-level agent's name is moved only when that agent is its +own private copy (a fork whose lineage is private to it and that its binding runs, +`_crew_owned_shadow`): the copy is moved to `~/.kiro/mates/retired/` and its +lineage dropped just before the repoint (`_retire_shadow`), so pointing the binding +back and moving the file back is a rollback. Any other user-level agent under its id +is not Kiro Crew's: the crewmate stays and is told once, and that spec is never +written. A crewmate named after a spec Kiro Crew generates is skipped. Its file would +never be read, and giving it another file name breaks every reader that knows a +crewmate's file by its binding key (`mate_capabilities.binding_is_mate`, +`member_mate_tools`, `mate_worker._claimed_file`, the revocations keyed by file id). Any other binding +with no `member_id` stays out of the migration: a `member_id` on a store that is not +its own V2 store fails every turn (`memory_stores.require_member_memory_store`), which +is why a minted row gets its store with its id. A crewmate left where it was gets one WARNING (`MigrationReport.dry_run_failed`) and is tried again next boot. A written file is read back and checked against the same loosening rule, and the kit record names its source. The ledger keeps diff --git a/docs/system-specs/modules/governance.md b/docs/system-specs/modules/governance.md index cf3306908554..1f97c6d6efae 100644 --- a/docs/system-specs/modules/governance.md +++ b/docs/system-specs/modules/governance.md @@ -1006,90 +1006,76 @@ unconditional rebuild would rewrite a file kiro-cli watches every refresh interv "The file" is every spec a session can be started on through a crew, not only `kirocrew.json`. The rebuild re-derives Kiro Crew's own specs, then the fork refresh (`agent_materialization.fork_refresh`) re-filters `allowedTools` and strips governed -`autoApprove` on every corroborated fork, and then runs the same two passes over every -**shared template** a crew in `config.json` is bound to: a spec created on the Agent -templates tab, edited on the agent detail page, or turned into a shared template by publish. -Those writers filter only when they write, so without this pass such a template would keep -the grants the ceiling now denies. The shared-template pass touches nothing but those two -lists, rewrites a file only when a pass removed something, and never writes a spec no crew is -bound to. A spec counts as the bound template when its stem is the bound name or it declares -that name, and when two specs claim it every JSON one is filtered, since which is live is -undefined. That is why the pass does not resolve through `agent.agent_spec_path`: a resolver -for a writer acting on one agent prefers a declared match over a stem match and refuses two -declared matches, and either rule would leave a claimant's grants unfiltered here. The -exemption for Kiro Crew's own specs and for forks is per file, not per name: the pass skips -`.json` for an owned name its installer governs and the file `agent.agent_spec_path` -resolves for a fork, because those are the only files those writers rewrite, and filters -every other file that claims the name. A second file declaring `kirocrew`, for example, is -one kiro-cli may load as that agent while the installer re-filters only `kirocrew.json`. A -fork whose name two files declare has no file the fork refresh wrote, so every claimant is -filtered. The pass does -not hold spawns the way the fork gate does. A bound template it fails to rewrite for a -reason a retry can clear (a spec file raised an `OSError` on its read, the agents directory could not be listed, its write raised, `config.json` or -`config.local.json` could not be read whole by this load) is folded into the rebuild's -conductor hold (`_held_out` and `agent._conductor_spec_held`, below), so the ceiling memo -stays behind and the next poll or maintenance wake retries it; the hook does not skip an -unchanged generation while that hold is set, and neither does the gateway's profile watch -(`agent.reproject_for_profile_change`, on its own retry backoff, the retry a host without -distribution polling has between maintenance wakes), which covers a hold the boot path's deferred -pass sets after `prime_ceiling_projection` seeded the baseline. The hold lasts until the -template is filtered, like a held conductor spec: releasing it while the failure persists -would mark the tightened ceiling projected with a denied grant possibly still on disk, and -nothing would retry it once the failure cleared. That includes a read this process is -denied (`PermissionError`). kiro-cli runs as the same user and cannot load the file while -its permissions deny the read, but restoring them puts whatever grants it carries back in -front of kiro-cli with no ceiling move to trigger a retry. A Windows sharing or lock -violation arrives as the same `PermissionError` and clears when the other handle closes. -Neither rebuilds on every poll: the probe below re-reads the file and lets the next tick -rebuild once the read succeeds. The directory is listed with `os.scandir`, whose errors -propagate (`agent_spec_format.iter_agent_spec_files_strict`): `Path.glob` swallows a failed -listing and answers with an empty directory, which would let the memo advance past every -bound template in it. A spec file with a second hard link is held too, until its link count is -one again, and logged at WARNING with the remedy. The fenced reader refuses a -multiply-linked inode for good, because the other name may be any file on the volume, but -kiro-cli reads the same bytes without that fence, so its grants are live and this pass cannot -filter them; the file is never written through the shared inode. Any other regular file the -reader refuses is held the same way, since its link count or kernel path changed under the -read and a retry may read it; a refused directory or pipe at a spec name is not held. Whether a config file could -not be read is asked of the load itself (`KiroCrewConfig._base_unreadable` and -`_overlay_unreadable`), not of `degraded_sections`, whose whole-config marker lasts the -process: a hold on it would never clear after the file was repaired. A template no retry can -fix (a markdown spec this writer -cannot re-serialize, bytes that do not parse, a capability reconcile that refuses) is logged -at WARNING and not held. - -A file the pass cannot read holds whether or not it claims a bound name, because an unread -file may be the one that declares it. Such a hold cannot clear until the file changes or a -read of it succeeds, so it does not rebuild on every poll. The same is true of a config file -the load could not read whole. When the pass's hold rests only on what it could not read, it -records each unreadable file's path, inode, link count and mtime, the loader's fingerprint of -both config files when one was unreadable, and the governance answer generation it ran under -(`fork_refresh._shared_template_hold_record`). The ceiling hook and the maintenance wake skip -the rebuild while that answer is current and is also the one the last rebuild derived its -grants under (`agent._rebuild_answer_generation`), every recorded file is unchanged and still reads as -held, and a fresh config load still cannot read a file whole -(`fork_refresh.shared_template_hold_unchanged`; it re-reads each file through the same fence -and logs nothing, and the loader never caches a read that failed). The memo stays behind -either way, so the tightened answer is still not recorded as projected. A new answer, any -change to a held file or config file, or a read that now succeeds (a sharing violation that -cleared, an extra link that was removed) runs the rebuild on the next tick. A hold with any -other cause is retried as before, because it can clear with nothing on disk changing: a -conductor installer's hold (`agent._conductor_installers_held`), a write or governance pass -that raised, or a rebuild that raised. The boot, pending and retry warnings name which kind -of spec is held. - -A spec no crew is bound to is not re-filtered, whoever wrote it. A file hand-placed in the -agents directory is outside every Kiro Crew writer. A template created on the Agent +`autoApprove` on every corroborated fork. A **shared template** a crew in `config.json` is +bound to (a spec created on the Agent templates tab, edited on the agent detail page, turned +into a shared template by publish, or installed by a package) is filtered only when one of +those writers writes it, and the rebuild does not rewrite it. + +A shared template is a spec Kiro Crew does not own, and the pass never writes one +(the 2026-10-28 owner decision in docs/decisions, every crewmate on a user-level agent moves to the conductor): +it is the same file kiro-cli loads for that agent OUTSIDE Kiro Crew, so a rewrite stripped +the user's own sessions too, and a package manager that reinstalls the spec fought it on +every restart. The startup crewmate migration moves the rows that used to reach such a file +onto their own crewmate file (crew-mode.md, "Existing crewmates move at startup"), and for a +row still bound to one the ceiling holds two ways. KAS never takes grants from the file: +Crew projects the spec at session start and filters both lists against the live ceiling +there (`acp.kas_agents._ceiling_permitted`, `kas_permissions.merge_user_permissions`). +kiro-cli does read them, so a kiro-cli start on a crew-bound spec Crew does not own is +REFUSED while the two governance passes would remove a grant (an `allowedTools` entry or a +server's `autoApprove` entry; an empty list is no grant) from any file that claims the +agent, or while such a file cannot be read or has a second hard link +(`fork_refresh.require_shared_template_within_ceiling`, called from the kiro-cli harness and +from `AcpClient` for every host in `ACP_BACKENDS_SPEC_FILE_GRANTS`), and +again when a shared runtime activates another agent's spec from disk at `session/set_mode` +(`acp.runtime_mode_activation._activate_mode_bracketed`, which ends the session first), since +that spec is read there and not at the spawn. A runtime on such a host also checks the +session's effective agent before each `session/new` and `session/load` +(`acp.runtime_session_setup._refuse_over_ceiling_start`), because a session that names no +agent inherits the spawn agent on a warm runtime, and its spec may have gained a grant since +the spawn checked it. A binding counts when it records the agent's +name or the file stem kiro-cli is dispatched under its declared name +(`config.loader_snapshots.dispatch_kiro_agent`), and a config load that could not read a +config file whole checks the agent's claimants as if it were bound, because the missing +binding may be this one. So does an agent a crewmate ran on before the startup migration +moved it (`mate_config_migration.moved_from_agents`), because a conversation from before +the move keeps the template its own record names; a ledger that cannot be read counts as +naming it. The refusal names the file and the remedy and writes nothing. So the rebuild's pass over shared +templates now runs only the capability reconcile (which writes the materialization Crew +owns) and reads no template file. +Kiro Crew's own specs and forks are untouched by any of this: their writers own them. + +The start check reads every spec that claims the agent: one whose stem is the name or +that declares it, every JSON claimant when two do, since which one kiro-cli loads is +undefined. That is why it does not resolve through `agent.agent_spec_path`, which prefers a +declared match over a stem match and refuses two declared matches. The exemption for Kiro +Crew's own specs and for forks is per file, not per name: it skips `.json` for an owned +name its installer governs and the file `agent.agent_spec_path` resolves for a fork, and +checks every other file that claims the name. A markdown claimant is skipped, because +kiro-cli does not load the markdown form. Bytes that do not parse as a spec carry no grant +kiro-cli could load. A file the fenced reader refuses (a second hard link, a link count or +path that changed under the read) or one whose read raises refuses the start, since +kiro-cli may read what the check could not; so does a binding or lineage read that raises. + +The rebuild still holds for what its shared-template pass does: a capability reconcile that +raised, and a `config.json` or `config.local.json` this load could not read whole (asked of +the load itself, `KiroCrewConfig._base_unreadable` and `_overlay_unreadable`, not of +`degraded_sections`, whose whole-config marker lasts the process). Either is folded into the +rebuild's conductor hold (`_held_out` and `agent._conductor_spec_held`, below), so the +ceiling memo stays behind and the next poll or maintenance wake retries. For an unreadable +config file the pass records the loader's fingerprint of both config files and the +governance answer generation it ran under (`fork_refresh._shared_template_hold_record`), and +the ceiling hook and the maintenance wake skip the rebuild while that answer is current, is +the one the last rebuild derived its grants under (`agent._rebuild_answer_generation`), and a +fresh config load still cannot read a file whole (`fork_refresh.shared_template_hold_unchanged`). +A hold with any other cause is retried as before, because it can clear with nothing on disk +changing: a conductor installer's hold (`agent._conductor_installers_held`), a reconcile that +raised, or a rebuild that raised. The boot, pending and retry warnings name which kind of spec +is held. + +A spec no crew is bound to is not checked, whoever wrote it. A template created on the Agent templates tab and left unbound keeps the grants it was saved with, and a subagent started on it through `spawn` runs with them. That spawn path is not covered here. -The removal is one-way. A denied `allowedTools` entry or governed `autoApprove` list is -dropped from the template, not parked: when the ceiling later loosens, nothing puts it back, -and the template's author re-adds it on the Agent templates tab or the detail page. That is -the contract every writer of these lists already has (a template saved under a tight ceiling -loses the same entries at write time), and keeping a restorable copy would mean storing a -grant the fleet forbids beside a file kiro-cli reads. - Two details make the generation bound safe rather than merely cheap. The baseline is seeded by `prime_ceiling_projection` **before the poller starts**, because the first poll can itself install a new ceiling and a first-call baseline would record that generation and skip the very diff --git a/docs/system-specs/modules/harness-onboarding.md b/docs/system-specs/modules/harness-onboarding.md index a72b34ad2a0d..eed7dd19218d 100644 --- a/docs/system-specs/modules/harness-onboarding.md +++ b/docs/system-specs/modules/harness-onboarding.md @@ -112,6 +112,7 @@ vocabulary. | `ACP_BACKENDS_CLIENT_META_SETTINGS` | Takes feature settings from `initialize.clientCapabilities._meta.kiro.settings`; currently this is the KAS Tool Search channel. | | `ACP_BACKENDS_MARKDOWN_AGENT_SPECS` | The harness loads an agent defined as ONE markdown file (`~/.kiro/agents/.md`, YAML frontmatter + body as prompt), the v3 / Kiro IDE form Crew's roster lists for every backend. Answered through the harness seam `reads_markdown_agent_specs` (`MembershipHarness`). A non-member that fails to activate such an agent has the activation guard explain the markdown file and name the members, instead of the generic "rewrite the JSON spec" advice; it is never refused BEFORE the spawn, so the Kiro path gains no gate (H13). KAS is the only member today: Crew parses the file and hands it over the wire. | | `ACP_BACKENDS_USER_LEVEL_AGENT_SPECS_ONLY` | The host reads agent specs only from the user-level directory, so the broker-overlay lookup must not be scoped to a project checkout that the harness never consults. Callers use `overlay_project_scope()` rather than testing a harness id. | +| `ACP_BACKENDS_SPEC_FILE_GRANTS` | The host takes an agent's `allowedTools` and a server's `autoApprove` from the `~/.kiro/agents` spec file itself, before Crew's gate (kiro-cli). Kiro Crew never rewrites a crew-bound spec it does not own, so a start on a member is refused while that file's grants exceed the ceiling (see governance.md). Every other host gets Crew's projection, which filters both lists (KAS) or forwards neither. A harness that reads the file's grants itself must join. | | `ACP_BACKENDS_SESSION_MCP_ARRAY` | The harness reads its MCP surface from the `session/new` array rather than from Crew's agent spec. A non-member that is added here gets an empty array and works with every Crew tool silently absent. | | `ACP_BACKENDS_META_IDENTITY` | Tool-call frames carry a harness-specific `_meta` identity that can classify calls whose ACP `kind` is absent. Membership opts into fail-closed refusal when a frame has neither identity channel. | | `ACP_BACKENDS_SPEC_SERVERS_OFF_WIRE` | The agent spec's own `mcpServers` reach the session through a channel other than the `session/new` array, so unresolved-`@server` checks count those declarations as mounted. | diff --git a/src/kiro_crew/acp/client_start.py b/src/kiro_crew/acp/client_start.py index cfecfedd8178..20200ae1ad15 100644 --- a/src/kiro_crew/acp/client_start.py +++ b/src/kiro_crew/acp/client_start.py @@ -90,9 +90,11 @@ class binds it by name, so it reads and writes the session's own state on the in DerivedSpecSnapshot, DerivedSpecStale, ForkGovernanceUnresolved, + SharedTemplateOverCeiling, ensure_agent_materialized, require_fork_governance, require_fresh_derived_spec, + require_shared_template_within_ceiling, require_unchanged_derived_spec, ) from kiro_crew.agent_sdk import host_auth @@ -286,6 +288,15 @@ async def _spawn(self: AcpClient) -> None: await asyncio.to_thread(require_fork_governance, self._agent, self._work_dir) except ForkGovernanceUnresolved as exc: raise AcpError(str(exc)) from exc + # A harness that may read the spec file's own grants is refused on a + # crew-bound spec Crew does not own that the ceiling would narrow: Crew never + # rewrites such a file. + try: + await asyncio.to_thread( + require_shared_template_within_ceiling, self._agent, self.backend + ) + except SharedTemplateOverCeiling as exc: + raise AcpError(str(exc)) from exc # The same cwd-first resolution makes a checkout that declares a MANAGED # agent name the spec that runs, whatever Kiro Crew installed under it. shadowed = await asyncio.to_thread( diff --git a/src/kiro_crew/acp/harness/kiro.py b/src/kiro_crew/acp/harness/kiro.py index 8f99b56c800f..50578256211a 100644 --- a/src/kiro_crew/acp/harness/kiro.py +++ b/src/kiro_crew/acp/harness/kiro.py @@ -113,6 +113,15 @@ async def resolve_spawn(self, ctx: SpawnContext) -> SpawnPlan: await asyncio.to_thread(agent_mod.require_fork_governance, ctx.agent, ctx.work_dir) except ForkGovernanceUnresolved as exc: raise AcpRuntimeError(str(exc)) from exc + # kiro-cli honours the spec file's own grants before Crew's gate, and Crew + # never rewrites a crew-bound spec it does not own, so a start on one the + # ceiling would narrow is refused. + try: + await asyncio.to_thread( + agent_mod.require_shared_template_within_ceiling, ctx.agent, ACP_BACKEND_KIRO + ) + except agent_mod.SharedTemplateOverCeiling as exc: + raise AcpRuntimeError(str(exc)) from exc # kiro-cli resolves ``--agent`` against ``ctx.work_dir`` first, so a checkout # that declares a managed agent name would run its own copy in place of the # installed one. Off the loop: it scans the checkout's agents directory. diff --git a/src/kiro_crew/acp/runtime_class.py b/src/kiro_crew/acp/runtime_class.py index bf4bf1e45b32..6150ece5f346 100644 --- a/src/kiro_crew/acp/runtime_class.py +++ b/src/kiro_crew/acp/runtime_class.py @@ -137,6 +137,7 @@ class AcpRuntime: # What a session start is built from: ``kiro_crew.acp.runtime_session_setup``. _session_work_dir = _rt_setup._session_work_dir _refuse_if_loader_unreachable = _rt_setup._refuse_if_loader_unreachable + _refuse_over_ceiling_start = _rt_setup._refuse_over_ceiling_start _kas_custom_agents = _rt_setup._kas_custom_agents _mount_member_panel = _rt_setup._mount_member_panel _session_start_budget = _rt_setup._session_start_budget diff --git a/src/kiro_crew/acp/runtime_mode_activation.py b/src/kiro_crew/acp/runtime_mode_activation.py index 63b162b20304..1cc8bc859c7d 100644 --- a/src/kiro_crew/acp/runtime_mode_activation.py +++ b/src/kiro_crew/acp/runtime_mode_activation.py @@ -11,7 +11,7 @@ ``_skill_projection_issued``, ``_skill_projection_lock_obj``, ``_skill_projection_unadopted``, ``_switched_models``. -Instance fields read: ``_agent``, ``_launch_agent``, ``_spawn_skill_projection``, +Instance fields read: ``_acp_backend``, ``_agent``, ``_launch_agent``, ``_spawn_skill_projection``, ``_wire_mode_id``, ``_work_dir``. """ @@ -554,6 +554,23 @@ async def _activate_mode_bracketed( except DerivedSpecStale as exc: await self.terminate_session(session_id) raise AcpRuntimeError(str(exc)) from exc + # The host reads *mode_agent*'s spec from disk here and honours its own + # grants before Crew's gate, and Crew never rewrites a crew-bound spec it + # does not own: the same over-ceiling refusal the spawn runs, for the agent + # this line activates rather than the one the process was spawned as. + from kiro_crew import agent as agent_mod + + try: + await asyncio.to_thread( + agent_mod.require_shared_template_within_ceiling, + mode_agent, + # A runtime that names no backend is kiro-cli's (``ACP_BACKEND_KIRO`` + # is ""), so the check runs: the fail-closed answer. + getattr(self, "_acp_backend", ""), + ) + except agent_mod.SharedTemplateOverCeiling as exc: + await self.terminate_session(session_id) + raise AcpRuntimeError(str(exc)) from exc try: projection_now: Any = None used_generation = 0 diff --git a/src/kiro_crew/acp/runtime_session_create.py b/src/kiro_crew/acp/runtime_session_create.py index 1c661b721aec..4b8efe363ba1 100644 --- a/src/kiro_crew/acp/runtime_session_create.py +++ b/src/kiro_crew/acp/runtime_session_create.py @@ -16,8 +16,9 @@ Instance fields read: ``_agent``, ``_agent_capabilities``, ``_crew_agent``, ``_dead``, ``_entitlement_probe_lock``, ``_expect_mcp_reports``, ``_initialized``, ``_mcp_gateway_overlay``, ``_member_context``, ``_pending_init_notifications``, ``_pid``, -``_pool_lookup``, ``_process``, ``_remote_session_servers``, ``_remote_workspace_agents``, -``_start_collect_timeout``, ``_wire_mode_id``, ``_work_dir``, ``_write_remote_token``. +``_pool_lookup``, ``_process``, ``_refuse_over_ceiling_start``, ``_remote_session_servers``, +``_remote_workspace_agents``, ``_start_collect_timeout``, ``_wire_mode_id``, ``_work_dir``, +``_write_remote_token``. """ from __future__ import annotations @@ -169,6 +170,10 @@ async def create_session( self._stderr_lines.clear() if not self._initialized: raise AcpRuntimeError("Runtime not initialized — call spawn() first") + # The effective agent, checked before anything is acquired: a session that names + # no agent inherits the spawn agent, whose spec the spawn check saw only as it + # stood then. + await self._refuse_over_ceiling_start(agent or self._agent) # Inject the shared gateway's broker stubs unless the caller supplied an # explicit list. A session-injected server outranks the same-named entry diff --git a/src/kiro_crew/acp/runtime_session_load.py b/src/kiro_crew/acp/runtime_session_load.py index b4ab5766d910..bc606bf44140 100644 --- a/src/kiro_crew/acp/runtime_session_load.py +++ b/src/kiro_crew/acp/runtime_session_load.py @@ -10,8 +10,9 @@ Instance fields read: ``_acp_backend``, ``_agent``, ``_agent_capabilities``, ``_can_load_session``, ``_crew_agent``, ``_expect_mcp_reports``, ``_initialized``, -``_mcp_gateway_overlay``, ``_pid``, ``_pool_lookup``, ``_remote_session_servers``, -``_remote_workspace_agents``, ``_wire_mode_id``, ``_work_dir``, ``_write_remote_token``. +``_mcp_gateway_overlay``, ``_pid``, ``_pool_lookup``, ``_refuse_over_ceiling_start``, +``_remote_session_servers``, ``_remote_workspace_agents``, ``_wire_mode_id``, ``_work_dir``, +``_write_remote_token``. """ from __future__ import annotations @@ -98,6 +99,9 @@ async def load_session( if not self._can_load_session: raise AcpRuntimeError("Backend does not advertise session/load support") agent = await self._source_agent(agent) + # The effective agent, checked before anything is acquired, as create_session() + # does: a resume that names no agent inherits the spawn agent. + await self._refuse_over_ceiling_start(agent or self._agent) # Re-declare the pooled broker stubs so a resumed session keeps talking # to the broker — same injection as create_session() and the AcpClient diff --git a/src/kiro_crew/acp/runtime_session_setup.py b/src/kiro_crew/acp/runtime_session_setup.py index 41b037414359..4e85eb920e27 100644 --- a/src/kiro_crew/acp/runtime_session_setup.py +++ b/src/kiro_crew/acp/runtime_session_setup.py @@ -250,6 +250,30 @@ def _refuse_if_loader_unreachable(self: AcpRuntime, active_agent: str, kas_agent ) +async def _refuse_over_ceiling_start(self: AcpRuntime, active_agent: str | None) -> None: + """Refuse a ``session/new`` or ``session/load`` on a spec the ceiling would narrow. + + The spawn checked the agent it launched as, but a warm runtime starts sessions + long after that: a session that names no agent inherits the spawn agent, and its + spec may have gained a grant since. kiro-cli reads the effective agent's spec + from disk at each start, so the same over-ceiling refusal runs here, before the + request goes out, for the agent this session will actually run. + """ + from kiro_crew import agent as agent_mod + + # The same host rule the spawn check uses (``ACP_BACKENDS_SPEC_FILE_GRANTS``). A + # runtime that names no backend is kiro-cli's (``ACP_BACKEND_KIRO`` is ""), a + # member, so the check runs: the fail-closed answer. + try: + await asyncio.to_thread( + agent_mod.require_shared_template_within_ceiling, + active_agent, + getattr(self, "_acp_backend", ""), + ) + except agent_mod.SharedTemplateOverCeiling as exc: + raise AcpRuntimeError(str(exc)) from exc + + async def _kas_custom_agents( self: AcpRuntime, agent: str, diff --git a/src/kiro_crew/acp_backends.py b/src/kiro_crew/acp_backends.py index 98de87c6714e..c864f3fe5043 100644 --- a/src/kiro_crew/acp_backends.py +++ b/src/kiro_crew/acp_backends.py @@ -88,6 +88,7 @@ ACP_BACKENDS_SESSION_MCP_ARRAY, ACP_BACKENDS_SESSION_SHARING, ACP_BACKENDS_SIDE_READONLY, + ACP_BACKENDS_SPEC_FILE_GRANTS, ACP_BACKENDS_SPEC_SERVERS_OFF_WIRE, ACP_BACKENDS_STEER, ACP_BACKENDS_STEER_CONCURRENT_PROMPT, @@ -151,6 +152,7 @@ "ACP_BACKENDS_HOST_AUTH_CALLBACK", "ACP_BACKENDS_INLINE_COMPACTION", "ACP_BACKENDS_INTERNAL_SANDBOX", + "ACP_BACKENDS_SPEC_FILE_GRANTS", "ACP_BACKENDS_CLIENT_META_SETTINGS", "ACP_BACKENDS_KIRO_FAMILY", "ACP_BACKENDS_KIRO_SLASH_COMMANDS", diff --git a/src/kiro_crew/agent.py b/src/kiro_crew/agent.py index 00950c273313..823c40bec632 100644 --- a/src/kiro_crew/agent.py +++ b/src/kiro_crew/agent.py @@ -303,6 +303,7 @@ ) from kiro_crew.agent_materialization.fork_refresh import ( # noqa: F401 _FORK_REFRESH_WAIT_SECS, + SharedTemplateOverCeiling, _fork_refresh_count_lock, _fork_refresh_failed, _fork_refresh_lock, @@ -312,6 +313,8 @@ _refresh_forked_templates_locked, fork_origin_is_owned, hold_spawn_gate, + name_has_another_claimant, + require_shared_template_within_ceiling, ) from kiro_crew.agent_materialization.guide_platform import ( # noqa: F401 grant_guide_platform_once, @@ -987,6 +990,9 @@ def _all_skill_paths() -> list[str]: "_refresh_forked_templates", "_refresh_forked_templates_locked", "fork_origin_is_owned", + "require_shared_template_within_ceiling", + "SharedTemplateOverCeiling", + "name_has_another_claimant", "hold_spawn_gate", ), "kiro_crew.agent_materialization.service_agents": ( diff --git a/src/kiro_crew/agent_materialization/auto_approve.py b/src/kiro_crew/agent_materialization/auto_approve.py index 840c2560d02c..2aa04db4cd58 100644 --- a/src/kiro_crew/agent_materialization/auto_approve.py +++ b/src/kiro_crew/agent_materialization/auto_approve.py @@ -66,9 +66,16 @@ def declared_auto_approve(emitted: Mapping[str, object]) -> dict[str, tuple[str, } -def _strip_ungoverned_auto_approve(servers: dict[str, Any]) -> dict[str, Any]: - """Local alias so tests can monkeypatch one name (see governance).""" - return dict(strip_ungoverned_auto_approve(servers)) +def _strip_ungoverned_auto_approve( + servers: dict[str, Any], *, audit: bool = True +) -> dict[str, Any]: + """Local alias so tests can monkeypatch one name (see governance). + + *audit* False is for a read-only check that writes the result nowhere. + """ + if audit: + return dict(strip_ungoverned_auto_approve(servers)) + return dict(strip_ungoverned_auto_approve(servers, audit=False)) def _write_derived_permissions( @@ -308,7 +315,7 @@ def _may_auto_approve(ref: str) -> bool: return may_skip_gate_now(ref) -def _apply_allowed_tools_ceiling(config: dict, *, source: str) -> None: +def _apply_allowed_tools_ceiling(config: dict, *, source: str, audit: bool = True) -> None: """Filter ``config["allowedTools"]`` through the governance ceiling, in place. ``allowedTools`` is the ONE path that never reaches the PreToolUse gate, so @@ -327,7 +334,9 @@ def _apply_allowed_tools_ceiling(config: dict, *, source: str) -> None: Withholding a grant is a permission DECISION, so it leaves the same ``mcp_auto_approve_withheld`` SEL record every other ``allowedTools`` writer emits — best-effort, never raising, so an audit failure cannot - break a build or an install. + break a build or an install. *audit* False is for a caller that only asks + whether a pass would remove something and writes the result nowhere, so no + grant was withheld and there is no decision to record. """ allowed = config.get("allowedTools") if not isinstance(allowed, list): @@ -339,7 +348,7 @@ def _apply_allowed_tools_ceiling(config: dict, *, source: str) -> None: continue (kept if _may_auto_approve(ref) else withheld).append(ref) config["allowedTools"] = kept - if withheld: + if withheld and audit: try: agent_mod.sel().log_api_access( caller="system", diff --git a/src/kiro_crew/agent_materialization/fork_refresh.py b/src/kiro_crew/agent_materialization/fork_refresh.py index 2aee3f588095..09ef42ec19a1 100644 --- a/src/kiro_crew/agent_materialization/fork_refresh.py +++ b/src/kiro_crew/agent_materialization/fork_refresh.py @@ -166,15 +166,13 @@ def _run_deferred() -> None: # into its conductor hold so a tightened ceiling is retried rather than marked projected. _shared_template_held: bool = False -# Set when the last pass's hold rests ONLY on what it could not read: files or the -# directory listing, each recorded with the (inode, link count, mtime) it had then, and, -# when its config load could not read a config file whole, the loader's fingerprint of -# both config files; plus the governance answer generation the pass ran under. None when -# nothing is held or the hold has another cause (a write, a capability reconcile or the -# pass itself raised), which a retry may clear with nothing on disk changing. Assigned -# whole, cleared at the start of every pass. Read by shared_template_hold_unchanged. -_HeldPath = tuple[Path, tuple[int, int, int] | None] -_shared_template_hold_record: tuple[int, tuple[_HeldPath, ...], tuple | None] | None = None +# Set when the last pass's hold rests ONLY on a config file its load could not read +# whole: the governance answer generation the pass ran under and the loader's +# fingerprint of both config files. None when nothing is held or the hold has another +# cause (a capability reconcile or the pass itself raised), which a retry may clear with +# nothing on disk changing. Assigned whole, cleared at the start of every pass. Read by +# shared_template_hold_unchanged. +_shared_template_hold_record: tuple[int, tuple] | None = None # Bounded so the spawn path's never-hangs contract survives a wedged refresh # thread; a module constant so tests can shrink it. A timeout is treated as a @@ -234,18 +232,23 @@ def hold_spawn_gate(): _fork_refresh_settled.set() -def _apply_governance_passes(config: dict, *, source: str) -> None: +def _apply_governance_passes(config: dict, *, source: str, audit: bool = True) -> None: """Re-filter the two grant lists kiro-cli honours before the PreToolUse gate, in place. ``allowedTools`` goes through the ceiling and every governed server loses its - ``autoApprove``. The shared-template pass runs these two alone: a shared template - is the user's own file, so its ``mcp.json`` merge and KAS block stay as written. A - fork gets the same two inside the governed tail (``auto_approve.govern_spec``). + ``autoApprove``. A fork gets the same two inside the governed tail + (``auto_approve.govern_spec``); the kiro-cli start check runs them on a copy of a + shared template to ask whether they would remove anything + (:func:`require_shared_template_within_ceiling`), and never writes the result. """ - auto_approve._apply_allowed_tools_ceiling(config, source=source) + auto_approve._apply_allowed_tools_ceiling(config, source=source, audit=audit) servers_map = config.get("mcpServers") if isinstance(servers_map, dict): - config["mcpServers"] = auto_approve._strip_ungoverned_auto_approve(servers_map) + config["mcpServers"] = ( + auto_approve._strip_ungoverned_auto_approve(servers_map) + if audit + else auto_approve._strip_ungoverned_auto_approve(servers_map, audit=False) + ) def _owned_spec_has_its_own_writer(name: str, owned_names: "set[str]") -> bool: @@ -324,11 +327,10 @@ def _refresh_forked_templates_locked(*, gated_off: "frozenset[str] | None" = Non passes (ceiling + auto-approve strip) run for every corroborated fork regardless of origin — no other writer sanitizes these files. - After the forks, the same two governance passes run over every other spec a crew - in ``config.json`` is bound to (:func:`_govern_bound_shared_specs`): a template - created on the Agent templates tab, edited through the agent detail page, or - turned into a shared template by publish was filtered only when it was written, - so without this pass it would keep the grants a tightened ceiling now denies. + After the forks, every other spec a crew in ``config.json`` is bound to gets its + capability reconcile (:func:`_govern_bound_shared_specs`). Its file is never + written: Kiro Crew does not own it, so a kiro-cli start on one the ceiling would + narrow is refused instead (:func:`require_shared_template_within_ceiling`). """ forks = agent_state.all_fork_info() global _fork_refresh_failed, _shared_template_held, _shared_template_hold_record @@ -487,30 +489,108 @@ def _origin_is_owned(name: str) -> bool: "fork refresh failed for %r; its sessions stay blocked", fork_name, exc_info=True ) _fork_refresh_failed = frozenset(failures) - held_paths: frozenset[Path] = frozenset() try: from kiro_crew.platform.governance_profiles import governance_answer_generation # Read before the pass derives a grant, like the rebuild's own answer. generation = governance_answer_generation() - held_paths, held_otherwise = _govern_bound_shared_specs(cfg_agents, forks, owned_names) + held_otherwise = _govern_bound_shared_specs(cfg_agents, forks, owned_names) # The load fell back to defaults for an unreadable config file, so a crew bound # to a template may be missing from cfg_agents entirely. config_incomplete = _bindings_may_be_incomplete(cfg) - if (held_paths or config_incomplete) and not held_otherwise: + if config_incomplete and not held_otherwise: from kiro_crew.config.loader import _config_fingerprint - _shared_template_hold_record = ( - generation, - tuple((path, _path_ident(path)) for path in sorted(held_paths)), - _config_fingerprint() if config_incomplete else None, - ) + _shared_template_hold_record = (generation, _config_fingerprint()) except Exception: held_otherwise = True config_incomplete = False _shared_template_hold_record = None agent_mod.logger.warning("shared template governance pass failed", exc_info=True) - _shared_template_held = held_otherwise or config_incomplete or bool(held_paths) + _shared_template_held = held_otherwise or config_incomplete + + +class SharedTemplateOverCeiling(Exception): + """A crew-bound spec Kiro Crew does not own carries grants the ceiling denies.""" + + +def require_shared_template_within_ceiling(agent: str | None, backend: str) -> None: + """Refuse a *backend* start on a crew-bound spec Kiro Crew does not own that the + ceiling would narrow. + + kiro-cli honours a spec file's ``allowedTools`` and server ``autoApprove`` before + Kiro Crew's gate, and Kiro Crew never writes a spec it does not own (owner + decision, docs/decisions). So a session on such a file is started only when the + two governance passes would remove nothing from any file that claims *agent*: + otherwise it would auto-approve what the ceiling denies. Only a host in + ``ACP_BACKENDS_SPEC_FILE_GRANTS`` reads the file's grants, so only its starts + are checked: every other host gets Crew's projection, which filters or drops them. Bounded like the rebuild pass: only a name + ``config.json`` binds a crew to, never Kiro Crew's own specs or a fork (their + writers govern them). A file that cannot be read, or one with a second hard link, + is refused too: kiro-cli may read what this check could not. Raises + :class:`SharedTemplateOverCeiling` naming the file and the remedy; reads only. + """ + from kiro_crew.acp_backends import ACP_BACKENDS_SPEC_FILE_GRANTS + + if not agent or backend not in ACP_BACKENDS_SPEC_FILE_GRANTS: + return + from kiro_crew.config.loader import KiroCrewConfig # circular import + + try: + from kiro_crew.config.loader import dispatch_kiro_agent + + cfg = KiroCrewConfig.load() + cfg_agents = cfg.agents + # A binding can record the file stem (``KiroPkg-captain``) while kiro-cli is + # dispatched the name that file declares (``captain``), so a binding counts + # when either spelling is *agent*. + names = { + stored + for stored in ( + getattr(binding, "kiro_agent", None) + for binding in (cfg_agents.values() if isinstance(cfg_agents, dict) else ()) + ) + if isinstance(stored, str) + and stored + and (stored == agent or dispatch_kiro_agent(stored) == agent) + } + # A load that could not read a config file whole may be missing the very + # binding this start runs, so its absence proves nothing: check *agent*'s + # claimants as if it were bound. So is an agent a crewmate ran on before the + # startup migration moved it: a conversation from before the move keeps the + # template its own record names. + from kiro_crew.mate_config_migration import LedgerUnreadable, moved_from_agents + + try: + # Recorded as the binding spelled it, so a stem matches under the name + # its file declares too, the name the old conversation is dispatched. + moved = { + m for m in moved_from_agents() if m == agent or dispatch_kiro_agent(m) == agent + } + moved_from = bool(moved) + names |= moved + except LedgerUnreadable: + # Not provably never moved, so its claimants are checked; this refuses + # only a spec the ceiling would narrow, not every start. + moved_from = True + if not names and not _bindings_may_be_incomplete(cfg) and not moved_from: + return + owned_names = {Path(f).stem for f in OWNED_KIRO_AGENT_FILES} + over = _claimants_over_ceiling(names | {agent}, agent_state.all_fork_info(), owned_names) + except Exception as exc: + # Fail closed: an unread binding or lineage may be the one this start runs. + raise SharedTemplateOverCeiling( + f"agent {agent!r}: its spec could not be checked against the governance " + f"ceiling ({exc}); the session was not started" + ) from exc + if over: + files = ", ".join(str(path) for path in sorted(over)) or "its spec" + raise SharedTemplateOverCeiling( + f"agent {agent!r}: {files} grants tools the governance ceiling denies, and " + "Kiro Crew does not rewrite a spec it does not own. Remove those entries " + "from its allowedTools or autoApprove, or start the session on KAS, whose " + "projection filters them." + ) def _bindings_may_be_incomplete(cfg: object) -> bool: @@ -527,42 +607,39 @@ def _bindings_may_be_incomplete(cfg: object) -> bool: def _scan_agent_specs(agents_dir: Path) -> "tuple[list[tuple[Path, str]], list[Path]]": - """Read every spec file in *agents_dir* once: the readable ones, and what to retry. - - Returns each readable file with its text, and every path whose read a retry may clear. - Reads go through the same fence and capped, descriptor-pinned reader the resolver - uses: a link, a target outside the directory or a sensitive one, and an oversized - file are refused for good and never opened. - - Any ``OSError`` on the directory or on a file, a denied read included, is returned - for a retry, so the caller holds until the read succeeds instead of reading the miss - as absence: an unreadable file may be the one that declares a bound name, and a file - kiro-cli cannot load while its permissions deny the read loads whatever grants it - carries once they are restored. The probe (:func:`shared_template_hold_unchanged`) - keeps such a hold from rebuilding on every poll. The listing - raises its own errors (:func:`iter_agent_spec_files_strict`) rather than yielding an - empty directory. A file the reader refuses for a second hard link is returned for a - retry too (:func:`_refusal_holds`). Each read is classified by :func:`_read_for_pass`, - which the locked re-read and the hold probe share. + """Read every spec file in *agents_dir* once: the readable ones, and what it could not read. + + Returns each readable file with its text, and every path whose read failed in a way + that leaves its grants unknown. Reads go through the same fence and capped, + descriptor-pinned reader the resolver uses: a link, a target outside the directory + or a sensitive one, and an oversized file are refused for good and never opened. + + Any ``OSError`` on the directory or on a file, a denied read included, is returned, + so the start check refuses rather than reading the miss as absence: an unreadable + file may be the one that declares the agent, and a file kiro-cli cannot load while + its permissions deny the read loads whatever grants it carries once they are + restored. The listing raises its own errors (:func:`iter_agent_spec_files_strict`) + rather than yielding an empty directory. A regular file the reader refuses, a second + hard link above all, is returned too (:func:`_refusal_holds`). """ - files, holds = _list_for_pass(agents_dir) - if holds: + files, unread = _list_for_pass(agents_dir) + if unread: return [], [agents_dir] readable: list[tuple[Path, str]] = [] - retry: list[Path] = [] + failed: list[Path] = [] for path in files: - raw, holds = _read_for_pass(path, agents_dir) + raw, unknown = _read_for_pass(path, agents_dir) if raw is not None: readable.append((path, raw.decode("utf-8", errors="replace"))) - elif holds: - retry.append(path) - return readable, retry + elif unknown: + failed.append(path) + return readable, failed def _list_for_pass(agents_dir: Path) -> "tuple[list[Path], bool]": - """The spec files in *agents_dir*, and whether a failed listing holds the pass. + """The spec files in *agents_dir*, and whether the listing failed. - A missing directory has no specs. Any other ``OSError`` holds. + A missing directory has no specs. Any other ``OSError`` is a failed listing. """ from kiro_crew.agent_spec_format import iter_agent_spec_files_strict @@ -575,17 +652,14 @@ def _list_for_pass(agents_dir: Path) -> "tuple[list[Path], bool]": return [], True -def _read_for_pass( - path: Path, agents_dir: Path, *, warn: bool = True -) -> "tuple[bytes | None, bool]": +def _read_for_pass(path: Path, agents_dir: Path) -> "tuple[bytes | None, bool]": """One fenced, capped read of the spec at *path*: ``(bytes, False)`` when it reads, - ``(None, True)`` when the pass must hold on it, ``(None, False)`` when it is skipped. + ``(None, True)`` when its grants are unknown, ``(None, False)`` when it is no spec. - Skipped: a refused shape (a link, a path outside the directory, a sensitive target), + No spec: a refused shape (a link, a path outside the directory, a sensitive target), a missing, oversized or directory entry, and a refusal of something that is not a - regular file when checked. Held: a refused regular file (:func:`_refusal_holds`) and - any other ``OSError``, a denied read included. *warn* False is the hold probe's quiet - mode, so a probe on every poll adds no log lines. + regular file when checked. Unknown: a refused regular file (:func:`_refusal_holds`) + and any other ``OSError``, a denied read included. """ from kiro_crew.agent_discovery import _read_spec_bytes, _SpecReadRefused from kiro_crew.hooks import FileTooLargeError @@ -599,54 +673,31 @@ def _read_for_pass( # reaches the reader's non-regular refusal; neither is a spec, so neither holds. return None, False except _SpecReadRefused: - return None, _refusal_holds(path, warn=warn) + return None, _refusal_holds(path) except OSError: return None, True -def _path_ident(path: Path) -> "tuple[int, int, int] | None": - """``(st_ino, st_nlink, st_mtime_ns)`` of *path* itself, ``None`` when lstat fails.""" - try: - info = path.lstat() - except OSError: - return None - return info.st_ino, info.st_nlink, info.st_mtime_ns - - -def _still_held(path: Path, agents_dir: Path) -> bool: - """Whether a pass's read of the held *path* would hold again now. Quiet.""" - if path == agents_dir: - return _list_for_pass(agents_dir)[1] - raw, holds = _read_for_pass(path, agents_dir, warn=False) - return raw is None and holds - - def shared_template_hold_unchanged(generation: int) -> bool: - """Whether running the pass again now could only end held on the same files. - - True only when the last pass's hold rests on what it could not read and nothing else - (:data:`_shared_template_hold_record`): spec files, the listing, or a config file its - load could not read whole. *generation* must be the governance answer that pass ran - under and no pass may be running. Every held path must still have the inode, link - count and mtime it had and still read as held, and a held config must be unchanged - and still unreadable (:func:`_config_still_incomplete`). The rebuild that - would run it rewrites ``kirocrew.json`` and the owned specs, which kiro-cli watches, - so the ceiling hook and the maintenance wake skip it on this answer and leave the - memo where it is: still not projected. A change to the answer or to any held path, a - read that would now succeed (a sharing violation that cleared, an extra link that - went), or any other cause of the hold makes this False, and that caller rebuilds. - MAY BLOCK: it reads each held file once, through the same fence as the pass. + """Whether running the pass again now could only end held on the same config read. + + True only when the last pass's hold rests on a config file its load could not read + whole and nothing else (:data:`_shared_template_hold_record`). *generation* must be + the governance answer that pass ran under, no pass may be running, and both config + files must be unchanged and a load still unable to read one whole + (:func:`_config_still_incomplete`). The rebuild that would run it rewrites + ``kirocrew.json`` and the owned specs, which kiro-cli watches, so the ceiling hook + and the maintenance wake skip it on this answer and leave the memo where it is: + still not projected. A change to the answer or to either config file, or any other + cause of the hold, makes this False, and that caller rebuilds. """ record = _shared_template_hold_record if record is None or not _shared_template_held or not _fork_refresh_settled.is_set(): return False - held_generation, held, config_fingerprint = record + held_generation, config_fingerprint = record if held_generation != generation: return False - if config_fingerprint is not None and not _config_still_incomplete(config_fingerprint): - return False - agents_dir = agent_mod.kiro_agents_dir_path() - return all(_path_ident(path) == ident and _still_held(path, agents_dir) for path, ident in held) + return _config_still_incomplete(config_fingerprint) def _config_still_incomplete(fingerprint: tuple) -> bool: @@ -666,22 +717,18 @@ def _config_still_incomplete(fingerprint: tuple) -> bool: return False -def _refusal_holds(path: Path, *, warn: bool = True) -> bool: - """Whether a spec the fenced reader refused at *path* must hold the pass. +def _refusal_holds(path: Path) -> bool: + """Whether a spec the fenced reader refused at *path* still carries unknown grants. - It must whenever *path* is still a regular file. The usual cause is a second hard + It does whenever *path* is still a regular file. The usual cause is a second hard link: the reader refuses a multiply-linked inode for good, because the other name may be any file on the volume, but kiro-cli reads the same bytes without that - fence. So the grants in it are live and cannot be filtered, and a pass that went on - would advance the memo past them; removing the extra link would not bring the - filter back. A regular file refused for another reason (its link count or kernel - path changed between the open and this check) is one a retry may read, so it holds - as well. The hold lasts until the read succeeds, and a WARNING names the file on - each pass that runs (*warn* False silences it for the hold probe); the ceiling hook - runs no pass while nothing held has changed (:func:`shared_template_hold_unchanged`). - A path that is not a regular file at this check (a link, a directory, a pipe) is not - a spec this pass could read, and one that is gone is absent. A metadata read that - fails holds, like a failed read. + fence, so the grants in it are live and this check cannot read them. A regular file + refused for another reason (its link count or kernel path changed between the open + and this check) is one this check could not read either. A WARNING names the file + and the remedy. A path that is not a regular file at this check (a link, a + directory, a pipe) is no spec, and one that is gone is absent. A metadata read that + fails counts as unknown, like a failed read. """ try: info = path.lstat() @@ -691,19 +738,20 @@ def _refusal_holds(path: Path, *, warn: bool = True) -> bool: return True if not stat.S_ISREG(info.st_mode): return False - if not warn: - return True if info.st_nlink > 1: agent_mod.logger.warning( "shared template governance: %s has %d hard links, so it cannot be read " - "safely while kiro-cli still loads it; held until it is a single-link file " - "(copy it to a new file and remove the other links)", + "safely while kiro-cli still loads it; a kiro-cli start on it is refused " + "until it is a single-link file (copy it to a new file and remove the " + "other links)", path, info.st_nlink, ) else: agent_mod.logger.warning( - "shared template governance: the read of %s was refused; held for a retry", path + "shared template governance: the read of %s was refused; a kiro-cli start " + "on it is refused until it reads", + path, ) return True @@ -731,6 +779,21 @@ def _claims(path: Path, text: str, name: str) -> bool: return isinstance(doc, dict) and (path.stem == name or doc.get("name") == name) +def name_has_another_claimant(name: str, path: Path, agents_dir: Path) -> bool: + """Whether a spec in *agents_dir* other than *path* claims *name*, or may. + + Every claimant counts (:func:`_claims`), and so does the ``.md`` twin a + JSON *path* hides, which surfaces once *path* is gone. A file that cannot be read + may be such a claimant, so it answers True. Reads only. + """ + from kiro_crew.agent_spec_format import shadowed_markdown_specs + + readable, unread = _scan_agent_specs(agents_dir) + if unread or any(other != path and _claims(other, text, name) for other, text in readable): + return True + return path.with_suffix(".md") in shadowed_markdown_specs(agents_dir) + + def _files_with_their_own_writer( name: str, forks: "dict[str, dict]", owned_names: "set[str]", agents_dir: Path ) -> "frozenset[Path] | None": @@ -757,39 +820,20 @@ def _files_with_their_own_writer( def _govern_bound_shared_specs( cfg_agents: object, forks: "dict[str, dict]", owned_names: "set[str]" -) -> "tuple[frozenset[Path], bool]": - """Re-filter ``allowedTools``/``autoApprove`` on every crew-bound spec that is not a fork. - - kiro-cli reads both lists from the spec file and honours them before Kiro Crew's - PreToolUse gate, so a grant written under an older ceiling stays live until the - file is rewritten. Forks and Kiro Crew's own specs have writers that re-filter - them on every rebuild, so this pass skips the one file each of those writers - rewrites (:func:`_files_with_their_own_writer`) and filters every other file that - claims the same name. Everything else a crew is bound to is - a shared template, and this pass gives it the same two governance passes the - fork refresh runs, and nothing more: no plumbing, no bookkeeping, no prompt. - - The set is bounded the way the fork refresh bounds its own: a spec qualifies - only when ``config.json`` binds a crew to it, so an unbound file in the agents - directory is never written by this pass. The file is rewritten ONLY when a pass - actually removed something, which leaves an unaffected hand-written spec - byte-for-byte as its author saved it. - - Returns ``(held_paths, held_otherwise)``. *held_paths* are the files (or the agents - directory itself) whose read left a bound template possibly unfiltered for a reason a - retry can clear: any read ``OSError``, a denied read included, or a regular file the - fenced reader refuses, such as one with a second hard link (see - :func:`_scan_agent_specs`). Any one of them holds, bound or not, because an unread - file may be the one that declares a bound name. *held_otherwise* is a hold no file - marks: a write or a capability reconcile that raised. The rebuild folds either into - its conductor hold, so the ceiling memo does not advance; the caller records the paths - so the ceiling hook can tell when a retry could read them - (:func:`shared_template_hold_unchanged`). A capability reconcile - that refuses (``CapabilityError``) is deterministic and is warned about, not held. - Every spec that claims the bound name is filtered (:func:`_claims`). A template a - retry cannot fix (a markdown spec this writer cannot re-serialize, bytes that do - not parse, which kiro-cli cannot load either) is logged at WARNING and not held. - Non-forks are never held at the spawn gate (``require_fork_governance``) either way. +) -> bool: + """Reconcile the saved capabilities of every crew-bound spec that is not a fork. + + A spec a crew is bound to that is neither Kiro Crew's own nor a fork is a file Kiro + Crew does not own (a shared template), and nothing here reads or writes it (owner + decision, docs/decisions): it is also what kiro-cli loads for that agent outside + Kiro Crew. A kiro-cli start on one the ceiling would narrow is refused instead + (:func:`require_shared_template_within_ceiling`). What this pass still does is + the capability reconcile of a bound name with saved capabilities, which writes the + materialization Kiro Crew owns. + + Returns whether a reconcile raised: a hold no file marks, which the rebuild folds + into its conductor hold so the ceiling memo does not advance. A reconcile that + refuses (``CapabilityError``) is deterministic and is warned about, not held. """ agents = cfg_agents if isinstance(cfg_agents, dict) else {} bound: dict[str, str] = {} @@ -797,95 +841,120 @@ def _govern_bound_shared_specs( name = getattr(binding, "kiro_agent", None) if isinstance(name, str) and name and isinstance(crew, str): bound.setdefault(name, crew) - if not bound: - return frozenset(), False - held_paths: set[Path] = set() - held_otherwise = False + held = False agents_dir = agent_mod.kiro_agents_dir_path() - # Read lazily and once: one unreadable file is one read and one warning, however - # many crews are bound. - scanned: "list[tuple[Path, str]] | None" = None for name in sorted(bound): try: - exempt = _files_with_their_own_writer(name, forks, owned_names, agents_dir) - if exempt is None and agent_state.get_capabilities(name) is not None: - from kiro_crew.agent_capabilities import ( - CapabilityError, - reconcile_member_capabilities, - ) - - try: - reconcile_member_capabilities(bound[name]) - except CapabilityError as exc: - # A refusal such as a hand-edited materialization: deterministic, - # so a retry would refuse again. - agent_mod.logger.warning( - "shared template governance: %r was refused by its capability " - "reconcile (%s); its grants were not re-filtered against the " - "current ceiling", - name, - exc, - ) + if _files_with_their_own_writer(name, forks, owned_names, agents_dir) is not None: continue - if scanned is None: - scanned, retry = _scan_agent_specs(agents_dir) - if retry: - held_paths.update(retry) - agent_mod.logger.warning( - "shared template governance: %s could not be read; held for a retry", - ", ".join(str(path) for path in retry), - ) - for spec_path, text in scanned: - if (exempt is not None and spec_path in exempt) or not _claims( - spec_path, text, name - ): - continue - if is_markdown_spec(spec_path): - agent_mod.logger.warning( - "shared template governance: %r has a markdown spec %s, which this " - "pass cannot rewrite; its grants were not re-filtered against the " - "current ceiling", - name, - spec_path, - ) - continue - if _govern_json_spec(name, spec_path, agents_dir): - held_paths.add(spec_path) + if agent_state.get_capabilities(name) is None: + continue + from kiro_crew.agent_capabilities import ( + CapabilityError, + reconcile_member_capabilities, + ) + + try: + reconcile_member_capabilities(bound[name]) + except CapabilityError as exc: + # A refusal such as a hand-edited materialization: deterministic, + # so a retry would refuse again. + agent_mod.logger.warning( + "shared template governance: %r was refused by its capability " + "reconcile (%s); its grants were not re-filtered against the " + "current ceiling", + name, + exc, + ) except Exception: - held_otherwise = True + held = True agent_mod.logger.warning( "shared template governance failed for %r", name, exc_info=True ) - return frozenset(held_paths), held_otherwise + return held + + +def _claimants_over_ceiling( + names: "set[str]", forks: "dict[str, dict]", owned_names: "set[str]" +) -> "frozenset[Path]": + """Every spec claiming one of *names* that the ceiling would narrow or that cannot be read. + + Reads only. A spec claims a name when its stem is the name or it declares it + (:func:`_claims`), and every claimant counts, since which one kiro-cli loads is + undefined. The one file a writer of Kiro Crew's own re-filters is skipped + (:func:`_files_with_their_own_writer`), and so is a name with saved capabilities, + whose materialization Kiro Crew owns. A markdown claimant is skipped: kiro-cli does + not load the markdown form (``ACP_BACKENDS_MARKDOWN_AGENT_SPECS``). A file the + fenced reader refuses or cannot read, and an agents directory that cannot be listed, + are returned too (:func:`_scan_agent_specs`): kiro-cli may read what this could not. + """ + agents_dir = agent_mod.kiro_agents_dir_path() + over: set[Path] = set() + scanned: "list[tuple[Path, str]] | None" = None + for name in sorted(names): + exempt = _files_with_their_own_writer(name, forks, owned_names, agents_dir) + if exempt is None and agent_state.get_capabilities(name) is not None: + continue + if scanned is None: + scanned, unread = _scan_agent_specs(agents_dir) + # kiro-cli never loads the markdown form, so an unread markdown file + # cannot be the spec it runs; an unlisted directory still counts. + over.update(p for p in unread if p == agents_dir or not is_markdown_spec(p)) + for spec_path, text in scanned: + if (exempt is not None and spec_path in exempt) or not _claims(spec_path, text, name): + continue + if is_markdown_spec(spec_path): + continue + if _spec_exceeds_ceiling(name, spec_path, agents_dir): + over.add(spec_path) + return frozenset(over) -def _govern_json_spec(name: str, spec_path: Path, agents_dir: Path) -> bool: - """Run the governance passes over one JSON spec; True when its re-read holds. +def _spec_exceeds_ceiling(name: str, spec_path: Path, agents_dir: Path) -> bool: + """Would the governance passes narrow the JSON spec at *spec_path*? Reads only. - The read-modify-write sits under the shared spec lock so a concurrent dashboard - edit is never reverted, the read is the scan's own (:func:`_read_for_pass`: fenced, - capped, a refused regular file or any read failure held), and the file is written - only when a pass removed something. A write that - raises propagates: it is a hold no file change marks. + True when they would remove something, and when the scan's own read + (:func:`_read_for_pass`: fenced, capped) refuses or fails, since kiro-cli may + read what this check could not. Bytes that do not parse as a spec are no spec + kiro-cli can load, so they carry no grant: False. """ - with agent_mod.agents_spec_lock(agents_dir): - raw, holds = _read_for_pass(spec_path, agents_dir) - if raw is None: - return holds - try: - config = user_json.loads_user_json(raw.decode("utf-8", errors="replace")) - except ValueError: - config = None - if not isinstance(config, dict): - agent_mod.logger.warning( - "shared template governance: %s for %r could not be read as a spec; its " - "grants were not re-filtered against the current ceiling", - spec_path, - name, - ) - return False - before = copy.deepcopy(config) - _apply_governance_passes(config, source=f"shared-template-refresh:{name}") - if config != before: - agent_mod._atomic_json_write(spec_path, config) - return False + raw, holds = _read_for_pass(spec_path, agents_dir) + if raw is None: + return holds + try: + config = user_json.loads_user_json(raw.decode("utf-8", errors="replace")) + except ValueError: + return False + if not isinstance(config, dict): + return False + before = _spec_grants(config) + # A check only: nothing is withheld or written, so it leaves no SEL record. + after = copy.deepcopy(config) + _apply_governance_passes(after, source=f"shared-template-check:{name}", audit=False) + # Grants, not documents: a pass that drops an empty list removes no grant. + return bool(before - _spec_grants(after)) + + +def _spec_grants(config: dict) -> "set[tuple[str, str]]": + """Every grant in *config* kiro-cli honours before the gate, as comparable pairs. + + ``allowedTools`` entries, and each server's ``autoApprove`` entries. A value that + is not a list is one grant of its own, so a pass that drops it still counts. An + absent key and an empty list are the same: no grant. + """ + grants: set[tuple[str, str]] = set() + allowed = config.get("allowedTools") + if isinstance(allowed, list): + grants.update(("allowedTools", repr(v)) for v in allowed) + elif allowed is not None: + grants.add(("allowedTools", repr(allowed))) + servers = config.get("mcpServers") + for server, spec in (servers.items() if isinstance(servers, dict) else ()): + if not isinstance(spec, dict) or "autoApprove" not in spec: + continue + asked = spec["autoApprove"] + if isinstance(asked, list): + grants.update((f"@{server}", repr(v)) for v in asked) + elif asked is not None: + grants.add((f"@{server}", repr(asked))) + return grants diff --git a/src/kiro_crew/agent_sdk/backend_cards.py b/src/kiro_crew/agent_sdk/backend_cards.py index cf468ae77a33..b1eca53f54b2 100644 --- a/src/kiro_crew/agent_sdk/backend_cards.py +++ b/src/kiro_crew/agent_sdk/backend_cards.py @@ -579,6 +579,12 @@ class _LineSpec: "whether an advertised [] id is applied as two writes. The " "model switch either lands or is refused, which its own line already says" ), + "ACP_BACKENDS_SPEC_FILE_GRANTS": ( + "whether a start on a crew-bound spec Kiro Crew does not own is refused " + "while that file's grants exceed the ceiling, because the host reads them. " + "Both states hold the governance ceiling, so it is no feature a reader " + "would choose a harness for" + ), "ACP_BACKENDS_USER_LEVEL_AGENT_SPECS_ONLY": ( "which directory a host resolves its agent specs from, and therefore " "whether the session's project checkout scopes the broker-overlay lookup. " diff --git a/src/kiro_crew/agent_sdk/backends.py b/src/kiro_crew/agent_sdk/backends.py index f5f85135f490..877e191a2961 100644 --- a/src/kiro_crew/agent_sdk/backends.py +++ b/src/kiro_crew/agent_sdk/backends.py @@ -196,6 +196,9 @@ * - ``ACP_BACKENDS_MCP_WIRE_REINJECT`` - pre-session registry query (whether the dashboard may skip a session reset because Crew re-sends the projection at the next turn boundary) + * - ``ACP_BACKENDS_SPEC_FILE_GRANTS`` + - pre-session registry query (whether a start on a crew-bound spec Crew does + not own is refused while the file's grants exceed the ceiling) * - ``ACP_BACKENDS_STRUCTURED_REFUSAL`` - driver-internal (whether the metadata refusal parser is consulted) * - ``ACP_BACKENDS_UNATTRIBUTED_TERMINAL_ERROR`` @@ -848,6 +851,7 @@ def resolve_selected_backend(value: object) -> str: ACP_BACKENDS_MCP_WIRE_REINJECT, ACP_BACKENDS_PRUNE_UNSTARTABLE_MCP, ACP_BACKENDS_SESSION_MCP_ARRAY, + ACP_BACKENDS_SPEC_FILE_GRANTS, ACP_BACKENDS_SPEC_SERVERS_OFF_WIRE, ACP_BACKENDS_TOOL_SEARCH_OVERLAY, ACP_BACKENDS_USER_LEVEL_AGENT_SPECS_ONLY, diff --git a/src/kiro_crew/agent_sdk/backends_mcp.py b/src/kiro_crew/agent_sdk/backends_mcp.py index f972dcc1489e..78d5a04f95d6 100644 --- a/src/kiro_crew/agent_sdk/backends_mcp.py +++ b/src/kiro_crew/agent_sdk/backends_mcp.py @@ -182,6 +182,23 @@ # host added later whose servers come from the user level alone joins here. ACP_BACKENDS_USER_LEVEL_AGENT_SPECS_ONLY = frozenset({ACP_BACKEND_KAS}) +# Backends that never take an agent's grants (``allowedTools``, a server's +# ``autoApprove``) from the spec FILE: Crew projects the spec onto the wire at +# session start and filters both lists against the live governance ceiling there +# (``acp.kas_agents._ceiling_permitted``, ``kas_permissions.merge_user_permissions``). +# kiro-cli is the opposite: it reads the file for ``--agent`` and honours both lists +# before Crew's PreToolUse gate, so the only way Crew can hold the ceiling on it is to +# rewrite the file. +# +# Members are the hosts that take ``allowedTools`` and ``autoApprove`` from the +# ``~/.kiro/agents`` spec file itself. Crew never rewrites a crew-bound spec it does +# not own, so a start on a member is refused while that file's grants exceed the +# ceiling (``fork_refresh.require_shared_template_within_ceiling``). Every other host +# gets Crew's projection of the spec, which filters both lists (KAS) or forwards +# neither (the adapted harnesses), so its starts are never refused for them. A +# harness that reads the file's grants itself must join. +ACP_BACKENDS_SPEC_FILE_GRANTS = frozenset({ACP_BACKEND_KIRO}) + def overlay_project_scope(backend: str, work_dir: Any) -> dict[str, Any]: """The overlay-lookup scope keywords for *backend*'s session. diff --git a/src/kiro_crew/mate_config_migration.py b/src/kiro_crew/mate_config_migration.py index 76c7319571c6..1e7ba8c7f1fc 100644 --- a/src/kiro_crew/mate_config_migration.py +++ b/src/kiro_crew/mate_config_migration.py @@ -30,7 +30,7 @@ import dataclasses import json import logging -from typing import Any +from typing import Any, Callable from kiro_crew.agent_files import is_mate_service_agent from kiro_crew.agent_sdk import agent_specs @@ -75,6 +75,8 @@ class MigrationReport: moved_from_default: list[str] = dataclasses.field(default_factory=list) #: Per crewmate, the pre-upgrade DM threads moved onto its conductor. threads_repointed: dict[str, list[str]] = dataclasses.field(default_factory=dict) + #: Used rows with no ``member_id`` given one and their own V2 store, then moved. + identities_minted: list[str] = dataclasses.field(default_factory=list) def _ledger_path(): @@ -321,7 +323,230 @@ def _shadowed_by_user_agent(name: str) -> bool: return True -def _candidates(cfg: KiroCrewConfig) -> dict[str, str]: +def _crew_owned_shadow(cfg: KiroCrewConfig, name: str) -> Any: + """The user-level spec shadowing crewmate *name*, when Kiro Crew owns it, else ``None``. + + Kiro Crew owns it when it is this crewmate's own private copy: a fork whose + lineage names *name* as the crew it is private to, that *name*'s binding runs + (the corroboration the fork refresh requires), and that resolves to exactly one + JSON file. Anything else (a spec the user or a package wrote, two files claiming + the name, an unreadable lineage) is not Kiro Crew's to move. + """ + from kiro_crew import agent_state + from kiro_crew.agent import agent_spec_path + + try: + info = agent_state.get_fork_info(name, strict=True) + binding = cfg.agents.get(name) + if not info or info.get("private_to") != name or binding is None: + return None + if getattr(binding, "kiro_agent", "") != name: + return None + path = agent_spec_path(name, agents_dir=paths_mod.kiro_agents_dir()) + except Exception: # noqa: BLE001 - not provably Crew's, so it stays + return None + if path is None or path.suffix != ".json": + return None + # Retiring it must leave nothing else claiming the name: a markdown twin or a + # stem match the resolver hid would shadow the crewmate file just the same. + # A file that cannot be read may be such a claimant, so it keeps it too. + from kiro_crew.agent import name_has_another_claimant + + if name_has_another_claimant(name, path, paths_mod.kiro_agents_dir()): + return None + return path + + +def _retire_shadow(cfg: KiroCrewConfig, name: str, path: Any) -> Callable[[], None]: + """Move crewmate *name*'s private copy *path* out of the agents folder and drop its lineage. + + Kept under ``~/.kiro/mates/retired/`` so pointing the binding back and moving + the file back is a rollback; the fork lineage goes, so the fork refresh and the + spawn gate stop treating the crewmate's name as a fork. Done under the shared spec + lock, which the fork refresh holds across its own read and rewrite of the copy, and + the copy is proved Crew's again inside it (:func:`_crew_owned_shadow`), so a + refresh cannot write the copy back after it moved. The agent snapshot is then + rescanned, so new chats see the name as the crewmate's own file at once. A failure + to drop the lineage moves the copy back before it raises. Returns the undo for a + later step of the same move that fails: it puts the copy and its lineage back, so + the crewmate keeps running where it was and its notice stays owed. + """ + import os + + from kiro_crew import agent as agent_mod + from kiro_crew import agent_state + from kiro_crew.config.loader_snapshots import refresh_materialized_agents + + agents_dir = paths_mod.kiro_agents_dir() + with agent_mod.agents_spec_lock(agents_dir): + if _crew_owned_shadow(cfg, name) != path: + raise MateConfigError(f"{name!r}'s private copy changed before it was retired") + retired = paths_mod.mate_agents_dir().parent / "retired" + retired.mkdir(parents=True, exist_ok=True) + target = retired / path.name + n = 1 + while target.exists(): + target = retired / f"{path.stem}.{n}{path.suffix}" + n += 1 + info = agent_state.get_fork_info(name, strict=True) or {} + os.replace(path, target) + try: + agent_state.clear_fork_info(name) + except BaseException: + os.replace(target, path) + raise + refresh_materialized_agents(heal_default=False) + logger.info("crewmate migration: retired %s's private copy to %s", name, target) + + def undo() -> None: + with agent_mod.agents_spec_lock(agents_dir): + if not path.exists(): + os.replace(target, path) + agent_state.set_fork_info(name, info["forked_from"], info["private_to"]) + refresh_materialized_agents(heal_default=False) + logger.info("crewmate migration: put %s's private copy back at %s", name, path) + + return undo + + +#: The ``source`` of a row a person or a package sync wrote. An app writes its own +#: name there, and an app's crew is the app's to manage, never given an identity here. +_IDENTITY_SOURCES = frozenset({"kirocrew", "package", "aim"}) + + +def _on_unowned_user_spec(agent_name: str) -> bool: + """Whether *agent_name* is a user-level spec Kiro Crew does not own. + + Not one of Kiro Crew's own specs, not the conductor or worker, not a private + copy (a fork), and present in the user-level agents folder. + """ + from kiro_crew import agent_state + from kiro_crew.agent import agent_spec_path + from kiro_crew.agent_files import OWNED_KIRO_AGENT_FILES + + if not agent_name or is_mate_service_agent(agent_name): + return False + if agent_name in {name.removesuffix(".json") for name in OWNED_KIRO_AGENT_FILES}: + return False + try: + if agent_state.get_fork_info(agent_name, strict=True): + return False + return agent_spec_path(agent_name, agents_dir=paths_mod.kiro_agents_dir()) is not None + except Exception: # noqa: BLE001 - not provably a user spec, so nothing is minted + return False + + +def moved_from_agents() -> frozenset[str]: + """Every agent a moved crewmate ran on before it moved (``threads..from``). + + A conversation that started before the move keeps the template its own record + names, even once the binding names the crewmate's file, so these agents are + still ones a crew session can start on. Raises :class:`LedgerUnreadable` when + ``migrated.json`` exists but cannot be read. + """ + threads = _read_ledger().get("threads") + if not isinstance(threads, dict): + return frozenset() + return frozenset( + entry["from"] + for entry in threads.values() + if isinstance(entry, dict) and isinstance(entry.get("from"), str) and entry["from"] + ) + + +def _mint_candidates(cfg: KiroCrewConfig) -> set[str]: + """The used rows with no ``member_id`` that move once given an identity. Reads only. + + A row a person created or a package sync wrote, bound to a user-level spec Kiro + Crew does not own, whose DM thread holds a turn (the evidence the synced-crewmate + prune keeps a row on, ``crewmate_prune_migration._chatted``), is a crewmate the + migration moves onto its conductor (owner decision, docs/decisions). Never + ``default``, the default agent, a legacy built-in (``mate_worker.is_legacy_binding``) + or an app's row; a row never chatted with is left for the prune. Nothing is written + here: the identity is given only once the move is proven (:func:`_move_with_identity`). + """ + from kiro_crew.crewmate_prune_migration import HistoryUnreadable, _chatted + from kiro_crew.history_files import _sessions_dir + from kiro_crew.mate_worker import is_legacy_binding + + out: set[str] = set() + try: + sessions_dir = _sessions_dir() + except Exception: # noqa: BLE001 - no history means nothing is shown used + return out + for name, binding in cfg.agents.items(): + if name in ("default", getattr(cfg, "default_agent", "")): + continue + if str(getattr(binding, "member_id", "") or "").strip(): + continue + if is_legacy_binding(name, binding) or getattr(binding, "source", "") not in ( + _IDENTITY_SOURCES + ): + continue + if not _on_unowned_user_spec(getattr(binding, "kiro_agent", "")): + continue + try: + if not _chatted(cfg, name, sessions_dir): + continue + except HistoryUnreadable as exc: + logger.info("crewmate migration: %r kept as it is: %s", name, exc) + continue + out.add(name) + return out + + +def _move_with_identity( + cfg: KiroCrewConfig, name: str, *, source: str, before: Callable[[], None] +) -> None: + """Give row *name* a ``member_id`` and its own V2 store and repoint it, in ONE publish. + + The store is allocated as the upgrade allocates it + (``provision_member_memory(upgrade=True)``), so the row keeps the name-derived slug + its DM binding and member directory already use. *before* (the ledger intent, a + private copy's retirement) runs after the allocation and before the publish. The + identity, the store and ``kiro_agent`` are then written together by one + compare-and-set on ``kiro_agent`` == *source*, so a row is never left on its old + agent with a new, empty store. Anything that fails before the publish lands retires + the allocation and restores the in-memory binding, then re-raises. + """ + from kiro_crew.memory_stores import ( + persist_member_config, + provision_member_memory, + retire_unpublished_allocation, + ) + + binding = cfg.agents[name] + previous_store = binding.memory_store + previous_member_id = binding.member_id + try: + provision_member_memory(cfg, name, upgrade=True) + before() + binding.kiro_agent = name + persist_member_config( + cfg, + name, + expected_store=previous_store, + expected_values={"kiro_agent": source}, + changed_fields={"kiro_agent", "member_id", "memory_store"}, + ) + except BaseException: + binding.kiro_agent = source + allocated = binding.memory_store + if allocated != previous_store: + retire_unpublished_allocation( + cfg, + name, + allocated, + previous_store=previous_store, + previous_member_id=previous_member_id, + ) + raise + logger.info("crewmate migration: %r is now a crewmate with its own memory", name) + + +def _candidates( + cfg: KiroCrewConfig, minting: "set[str] | frozenset[str]" = frozenset() +) -> dict[str, str]: """Crewmates whose ``kiro_agent`` does not resolve to their own file yet. ``{member_name: source_agent}``, leaving out ``default``, bindings with no @@ -333,10 +558,10 @@ def _candidates(cfg: KiroCrewConfig) -> dict[str, str]: if name == "default": continue member_id = str(getattr(binding, "member_id", "") or "").strip() - if not member_id: + if not member_id and name not in minting: logger.debug("crewmate migration: %r has no member id; not a crewmate", name) continue - if _already_moved(ledger, name, member_id): + if member_id and _already_moved(ledger, name, member_id): continue if is_mate_service_agent(name): # Its file would overwrite a spec Kiro Crew generates in the same folder. @@ -349,7 +574,9 @@ def _candidates(cfg: KiroCrewConfig) -> dict[str, str]: source = getattr(binding, "kiro_agent", "") if not source: continue - if source == name and has_mate_config(name): + if source == name and has_mate_config(name) and not _shadowed_by_user_agent(name): + # A file that a user-level spec still shadows is not read yet: a move + # that stopped before retiring its private copy is resumed. continue out[name] = source return out @@ -483,7 +710,9 @@ class Differences: its user is told once (owner decision, ``docs/decisions``). A *loosening* (a grant gained, a switched-off server started, a hook or a ``permissions`` / ``toolsSettings`` restriction not kept) would let the worker do more than the - old agent was allowed to: the crewmate never moves with one. + old agent was allowed to. A gain or a relaunched server keeps the crewmate where + it is; a restriction not kept moves with it and its notice names it + (:func:`dropped_restrictions`, owner decision, ``docs/decisions``). """ lost: set[tuple[str, ...]] = dataclasses.field(default_factory=set) @@ -661,6 +890,59 @@ def plan_move( return planned, diff, withheld +def dropped_restrictions( + base: dict[str, Any], source: dict[str, Any], merged: dict[str, Any] +) -> list[str]: + """Each restriction *source* states that the worker *merged* would not keep. + + The worker merge carries no ``hooks``, ``permissions`` or ``toolsSettings`` from + the file, so such a restriction does not apply to the moved crewmate. It + does not keep the crewmate where it is (owner decision, docs/decisions); the + governance ceiling still governs the merged worker. One phrase per restriction, + for the notice: a hook by its event and matcher, a ``permissions`` rule by its + effect and capability, a ``toolsSettings`` entry by its tool. + """ + out: list[str] = [] + for event, entry in sorted(_hook_entries(source) - _hook_entries(base) - _hook_entries(merged)): + try: + matcher = json.loads(entry).get("matcher") if entry.startswith("{") else None + except ValueError: + matcher = None + on = f" on {matcher}" if isinstance(matcher, str) and matcher else "" + out.append(f"its {event} hook{on}") + for kind, text in sorted(_restrictions(source) - _restrictions(merged)): + value = json.loads(text) + if kind == "toolsSettings": + for key in sorted(value) if isinstance(value, dict) else (): + out.append(f"its toolsSettings for {key}") + continue + rule = value if isinstance(value, dict) else {} + effect = str(rule.get("effect") or "deny") + capability = str(rule.get("capability") or "every tool") + match = ", ".join(_strings(rule.get("match"))) + out.append( + f"its permissions rule to {effect} {capability}" + (f" ({match})" if match else "") + ) + return out + + +def restrictions_notice(dropped: list[str]) -> str: + """The notice's sentence naming each restriction that does not apply on the worker.""" + if not dropped: + return "" + return "these no longer apply on its worker: " + "; ".join(dropped) + + +def _loosening_besides_restrictions(diff: Differences) -> str: + """:meth:`Differences.loosening` without the restrictions the file cannot carry. + + A dropped hook or ``permissions`` / ``toolsSettings`` restriction moves with the + crewmate and is named in its notice; a gain or a switched-off server starting + still keeps it where it is. + """ + return dataclasses.replace(diff, hooks_lost=False, limits_lost=set()).loosening() + + def change_notice(diff: Differences, withheld: set[tuple[str, ...]]) -> str: """The sentence a moved crewmate's notice carries on what changed, ``""`` when nothing.""" parts = [diff.losses()] if diff.losses() else [] @@ -788,7 +1070,7 @@ def _tool_offs(spec: dict[str, Any]) -> set[str]: def _fallback_keeping_limits( source_agent: str, base: dict[str, Any], fallback: dict[str, Any] -) -> tuple[dict[str, Any], str]: +) -> tuple[dict[str, Any], str, str]: """The default-template *fallback* made to keep *source_agent*'s readable limits. For an agent :func:`_governed_source` refused. One whose spec does not parse @@ -796,13 +1078,15 @@ def _fallback_keeping_limits( ``file://`` prompt that does not resolve, say) is read raw, and the fallback never loosens it: a server it switched off is written switched off, a tool it takes out is written into ``excludedTools``, and a grant it never had is - taken out. Returns the file and why it would still loosen (a hook or a - ``permissions`` / ``toolsSettings`` restriction it cannot carry), else ``""``. + taken out. A hook or a ``permissions`` / ``toolsSettings`` restriction the + file cannot carry moves with it (:func:`dropped_restrictions`). Returns the + file, why it would still loosen (a gain or a relaunched server), else ``""``, + and the notice's sentence naming each restriction that does not apply there. """ try: raw = agent_specs.load_agent_spec(source_agents_dir(source_agent), source_agent) except (agent_specs.AgentSpecUnreadable, OSError): - return fallback, "" + return fallback, "", "" raw = {**copy_json_document(raw), "prompt": ""} planned = copy_json_document(fallback) diff = differences(base, raw, planned) @@ -820,7 +1104,7 @@ def _fallback_keeping_limits( planned = _strip_grants(planned, diff.gained) diff = differences(base, raw, planned) merged = merge_worker_spec(base, worker_additions(base, planned)) - reason = diff.loosening() + reason = _loosening_besides_restrictions(diff) kept_off = offs - set(_strings(merged.get("excludedTools"))) if kept_off: reason = "; ".join( @@ -828,7 +1112,7 @@ def _fallback_keeping_limits( None, [reason, _describe("its worker would run", {("tool", t) for t in kept_off})] ) ) - return planned, reason + return planned, reason, restrictions_notice(dropped_restrictions(base, raw, merged)) def _worker_base() -> dict[str, Any]: @@ -912,7 +1196,8 @@ def migrate_existing_crewmates( try: cfg = cfg or KiroCrewConfig.load() paths_mod.mate_agents_dir().mkdir(parents=True, exist_ok=True) - candidates = _candidates(cfg) + to_mint = _mint_candidates(cfg) + candidates = _candidates(cfg, to_mint) if not candidates: report.threads_repointed = repoint_moved_threads() return report @@ -931,6 +1216,9 @@ def migrate_existing_crewmates( verified: list[str] = [] unchanged: set[str] = set() + #: Crewmate -> the private copy of its own that shadows its file, retired before + #: its repoint (owner decision, docs/decisions). + retire: dict[str, Any] = {} #: Per crewmate moved with a difference, the sentence its notice names. changes: dict[str, str] = {} for name, source in candidates.items(): @@ -950,14 +1238,20 @@ def migrate_existing_crewmates( continue verified.append(name) continue - if _shadowed_by_user_agent(name): - # A user-level agent carries the crewmate's own id, so a file under - # that id would never be read: resolution prefers the user level, and - # the binding already names it. Moving would change nothing but the - # thread records. Kept where it is, said once. + shadow = _crew_owned_shadow(cfg, name) if _shadowed_by_user_agent(name) else None + if shadow is not None: + # The shadowing spec is this crewmate's own private copy, which Kiro + # Crew wrote: it moves, and the copy is retired before its repoint. + retire[name] = shadow + elif _shadowed_by_user_agent(name): + # A user-level agent Kiro Crew does not own carries the crewmate's id, + # so a file under that id would never be read: resolution prefers the + # user level. Kept where it is, said once; that spec is never written. report.dry_run_failed[name] = ( "its id is also the name of a user-level agent, so its own file would " - "never be read; rename that agent or the crewmate to move it" + "never be read; rename that agent or the crewmate to move it. While that " + "agent grants tools the governance ceiling denies, a kiro-cli session on " + "it is refused" ) logger.warning( "crewmate migration: %s stays on %r because %s", @@ -972,7 +1266,7 @@ def migrate_existing_crewmates( # Moved anyway (owner decision, docs/decisions): its worker starts from # the default template, as a fresh create's does, and its user is told. logger.info("crewmate migration: %s's agent %r unreadable: %s", label, source, exc) - fallback, kept_limit = _fallback_keeping_limits( + fallback, kept_limit, limits_said = _fallback_keeping_limits( source, base, _default_template_file(name, base) ) if kept_limit: @@ -997,18 +1291,27 @@ def migrate_existing_crewmates( f"the agent it ran on ({source}) could not be read, so its worker starts " "from the default template and nothing that agent added was carried over" ) + if limits_said: + changes[name] += "; " + limits_said verified.append(name) continue planned, diff, withheld = plan_move(name, base, old) - reason = diff.loosening() + # A hook or a ``permissions`` / ``toolsSettings`` restriction the file + # cannot carry moves with the crewmate and is named in its notice (owner + # decision, docs/decisions); a gain or a relaunched server still keeps it. + reason = _loosening_besides_restrictions(diff) if reason: report.dry_run_failed[name] = reason logger.warning("crewmate migration: %s stays on %r because %s", label, source, reason) continue try: _write_file(name, planned) - diff = differences(base, old, _read_back(name)) - reason = diff.loosening() + written = _read_back(name) + diff = differences(base, old, written) + reason = _loosening_besides_restrictions(diff) + dropped = dropped_restrictions( + base, old, merge_worker_spec(base, worker_additions(base, written)) + ) except (MateConfigError, agent_specs.AgentSpecUnreadable) as exc: report.copy_failed[name] = str(exc) logger.warning("crewmate migration: %s's file was not written: %s", label, exc) @@ -1022,7 +1325,9 @@ def migrate_existing_crewmates( reason, ) continue - change = change_notice(diff, withheld) + change = "; ".join( + filter(None, [change_notice(diff, withheld), restrictions_notice(dropped)]) + ) if change: # Moved anyway (owner decision, docs/decisions): a loss is told once, a gain # was not written and waits for the user's card. @@ -1031,23 +1336,66 @@ def migrate_existing_crewmates( _record_kit(name, source, planned) verified.append(name) + for name in verified: + if name in to_mint: + # Its memory moved to a store of its own. A conversation from before the + # move keeps the template and store its own record names, so the user is + # told to start a new chat; a kiro-cli start on that template is still + # checked against the ceiling (``moved_from_agents``). + changes[name] = "; ".join( + filter( + None, + [ + "it now keeps its own memory, so its earlier chat stays as it was " + "and a new chat starts on its conductor", + changes.get(name, ""), + ], + ) + ) + # Pointers move only once every candidate above has been written and proven. for name in verified: binding = cfg.agents.get(name) source = "" if name in unchanged else candidates[name] try: - # Intent first: the thread pass is owed before the binding moves. - _record_migrated(name, source, moved=False) - _repoint( - name, - source=candidates[name], - store=binding.memory_store if binding is not None else None, - ) + + undos: list[Callable[[], None]] = [] + + def _before_repoint( + name: str = name, source: str = source, undos: list = undos + ) -> None: + # Intent first: the thread pass is owed before the binding moves. + _record_migrated(name, source, moved=False) + if name in retire: + undos.append(_retire_shadow(cfg, name, retire[name])) + + if name in to_mint: + # Only now that its file is written and proven, and in the same + # publish as its repoint: a row that stays keeps the memory it had. + _move_with_identity(cfg, name, source=candidates[name], before=_before_repoint) + else: + _before_repoint() + _repoint( + name, + source=candidates[name], + store=binding.memory_store if binding is not None else None, + ) except Exception as exc: + for undo in undos: + try: + undo() + except Exception: # noqa: BLE001 - logged; the next boot retries the move + logger.warning( + "crewmate migration: %s's private copy was not put back", + name, + exc_info=True, + ) report.repoint_failed[name] = str(exc) logger.warning("crewmate migration: repoint failed for %s: %s", _label(cfg, name), exc) continue report.migrated.append(name) + if name in to_mint: + report.identities_minted.append(name) if name in changes and name not in report.moved_from_default: report.moved_with_changes[name] = changes[name] from kiro_crew.mate_upgrade_notice import clear_kept_owed, mark_notice_owed diff --git a/src/kiro_crew/memory_stores.py b/src/kiro_crew/memory_stores.py index 4ae37fe1897d..1a9fac349081 100644 --- a/src/kiro_crew/memory_stores.py +++ b/src/kiro_crew/memory_stores.py @@ -1506,17 +1506,24 @@ def repair_legacy_member_stores() -> list[str]: return [] -def provision_member_memory(config, member: str) -> str: - """Validate configuration before allocating or acquiring a store write lock.""" +def provision_member_memory(config, member: str, *, upgrade: bool = False) -> str: + """Validate configuration before allocating or acquiring a store write lock. + + *upgrade* is for a LIVE row that predates durable ids being given one (the crewmate + migration): the id is allocated as the upgrade allocates it + (``_allocate_member_id(refuse_damaged=False)``), so the row keeps the + name-derived slug its DM binding, member directory and thread already use. A + create never passes it: a same-name binding there is a deleted predecessor's. + """ from kiro_crew.config.resolution import DEGRADED_WHOLE_CONFIG if set(getattr(config, "degraded_sections", ())) & {"memory", DEGRADED_WHOLE_CONFIG}: raise UnknownMemoryStore("Memory configuration is unreadable") with memory_store_namespace_lock(): - return _provision_member_memory(config, member) + return _provision_member_memory(config, member, upgrade=upgrade) -def _provision_member_memory(config, member: str) -> str: +def _provision_member_memory(config, member: str, *, upgrade: bool = False) -> str: from kiro_crew import platform_compat from kiro_crew.config.sections import MemoryStoreConfig from kiro_crew.vector_memory import create_member_database @@ -1532,7 +1539,7 @@ def _provision_member_memory(config, member: str) -> str: raise UnknownMemoryStore("Existing member identity has no valid store; allocation refused") if current is not None and (current.owner_member_id or current.owner_member): raise UnknownMemoryStore("Existing member store identity is invalid") - member_id = _allocate_member_id(config, member) + member_id = _allocate_member_id(config, member, refuse_damaged=not upgrade) root = memory_stores_root() platform_compat.make_owner_only_dir(root) while True: diff --git a/test/test_acp_harness_contract.py b/test/test_acp_harness_contract.py index 8237a96e6075..6cf8cbe89bfc 100644 --- a/test/test_acp_harness_contract.py +++ b/test/test_acp_harness_contract.py @@ -243,6 +243,26 @@ def _boom(agent, work_dir): await harness_for(ACP_BACKEND_KIRO).resolve_spawn(_ctx(tmp_path)) +@pytest.mark.asyncio +async def test_kiro_spawn_refuses_a_bound_unowned_spec_over_the_ceiling( + found_binary, kiro_gates_pass, monkeypatch, tmp_path +): + """kiro-cli honours the spec file's grants and Crew never rewrites a spec it does + not own, so the spawn asks, under kiro-cli's own backend id, and is refused.""" + from kiro_crew.acp.session_handle import AcpRuntimeError + + calls: list[tuple[str | None, str]] = [] + + def _over(agent, backend): + calls.append((agent, backend)) + raise agent_mod.SharedTemplateOverCeiling("over the ceiling") + + monkeypatch.setattr(agent_mod, "require_shared_template_within_ceiling", _over) + with pytest.raises(AcpRuntimeError, match="over the ceiling"): + await harness_for(ACP_BACKEND_KIRO).resolve_spawn(_ctx(tmp_path)) + assert calls == [("a", ACP_BACKEND_KIRO)] + + @pytest.mark.asyncio async def test_kiro_spawn_refuses_a_workspace_overlapping_the_agents_tree( found_binary, kiro_gates_pass, monkeypatch, tmp_path diff --git a/test/test_acp_runtime_session_start.py b/test/test_acp_runtime_session_start.py index c564ae9b0c9e..3dc864c5d723 100644 --- a/test/test_acp_runtime_session_start.py +++ b/test/test_acp_runtime_session_start.py @@ -980,6 +980,9 @@ async def test_session_start_budget_follows_config(monkeypatch): rt, _, _ = _make_runtime() rt._expect_mcp_reports = False + # The fake config carries no bindings, which the over-ceiling check reads as a + # damaged load and refuses; this test is about the budget alone. + rt._refuse_over_ceiling_start = AsyncMock() # type: ignore[method-assign] # Lazy: construction must not have resolved (or read) config. assert rt._session_start_timeout is None seen: dict[str, object] = {} @@ -1053,3 +1056,76 @@ async def test_send_and_await_timeout_error_names_the_budget(): assert "timed out" in msg assert "0.01s" in msg assert "probe/method" in msg + + +@pytest.mark.asyncio +async def test_activating_a_disk_spec_over_the_ceiling_ends_the_session_before_set_mode(): + """A shared runtime spawned as one agent and switched to another reads that + agent's spec from disk at ``set_mode``. A crew-bound spec Crew does not own whose + grants the ceiling denies is refused there, for the agent being activated.""" + from kiro_crew import agent as agent_mod + from kiro_crew.acp.session_handle import AcpRuntimeError + + rt, _, _ = _make_runtime() + rt._agent = "kirocrew" + rt._send_and_await = AsyncMock() # type: ignore[method-assign] + rt.terminate_session = AsyncMock() # type: ignore[method-assign] + asked: list[tuple[str, str]] = [] + + def _over(agent: str, backend: str) -> None: + asked.append((agent, backend)) + raise agent_mod.SharedTemplateOverCeiling("over the ceiling") + + with ( + patch.object(agent_mod, "require_fresh_derived_spec", return_value=None), + patch.object(agent_mod, "require_shared_template_within_ceiling", side_effect=_over), + ): + with pytest.raises(AcpRuntimeError, match="over the ceiling"): + await rt._activate_mode_bracketed( + "s1", "pkg-reviewer", budget=30.0, payload_snapshot=None, wire_registered=False + ) + assert asked == [("pkg-reviewer", rt._acp_backend)] + rt.terminate_session.assert_awaited_once_with("s1") + rt._send_and_await.assert_not_called() + + +@pytest.mark.parametrize("start", ["create", "load"]) +@pytest.mark.asyncio +async def test_a_warm_session_that_inherits_the_spawn_agent_is_checked_before_the_request(start): + """A session that names no agent runs the spawn agent, and its spec may have + gained a grant since the spawn checked it. The over-ceiling refusal runs for + that effective agent before ``session/new`` or ``session/load`` goes out.""" + from kiro_crew import agent as agent_mod + from kiro_crew.acp.session_handle import AcpRuntimeError + + rt, _, _ = _make_runtime() + rt._agent = "pkg-reviewer" + rt._can_load_session = True + rt._send_and_await = AsyncMock() # type: ignore[method-assign] + asked: list[tuple[str, str]] = [] + + def _over(agent: str, backend: str) -> None: + asked.append((agent, backend)) + raise agent_mod.SharedTemplateOverCeiling("over the ceiling") + + with patch.object(agent_mod, "require_shared_template_within_ceiling", side_effect=_over): + with pytest.raises(AcpRuntimeError, match="over the ceiling"): + if start == "create": + await rt.create_session(cwd="/w") + else: + await rt.load_session("/home/u/.kiro/sessions/cli/sid-9.json", "sid-9", cwd="/w") + assert asked == [("pkg-reviewer", rt._acp_backend)] + rt._send_and_await.assert_not_called() + + +@pytest.mark.asyncio +async def test_a_kas_runtime_is_not_refused_at_session_start(): + """KAS filters the spec's grants in its own projection, so its start passes.""" + from kiro_crew.acp_backends import ACP_BACKEND_KAS + from kiro_crew.config.loader import KiroCrewConfig + + rt, _, _ = _make_runtime() + rt._acp_backend = ACP_BACKEND_KAS + # A config read that fails would refuse the start, so reaching it fails the test. + with patch.object(KiroCrewConfig, "load", side_effect=OSError("unreadable")): + await rt._refuse_over_ceiling_start("pkg-reviewer") diff --git a/test/test_agent_refactor_contracts.py b/test/test_agent_refactor_contracts.py index 8cad1ebbe1e8..3f83a2b03ded 100644 --- a/test/test_agent_refactor_contracts.py +++ b/test/test_agent_refactor_contracts.py @@ -146,7 +146,7 @@ "_alias_family_base": "callable _alias_family_base(key: 'str') -> 'str'", "_all_skill_paths": "callable _all_skill_paths() -> 'list[str]'", "_app_owned_mcp_keys": "callable _app_owned_mcp_keys() -> 'dict[str, bool]'", - "_apply_allowed_tools_ceiling": "callable _apply_allowed_tools_ceiling(config: 'dict', *, source: 'str') -> 'None'", + "_apply_allowed_tools_ceiling": "callable _apply_allowed_tools_ceiling(config: 'dict', *, source: 'str', audit: 'bool' = True) -> 'None'", "_apply_connection_tool_aliases": "callable _apply_connection_tool_aliases(config: 'dict', claimed: 'frozenset[tuple[str, str, str]]' = frozenset()) -> 'tuple[str, frozenset[tuple[str, str, str]]] | None'", "_apply_operator_oauth_client": "callable _apply_operator_oauth_client(name: 'str', entry: 'dict', *, managed: 'bool') -> 'dict'", "_apply_user_kiro_hooks": "callable _apply_user_kiro_hooks(config: 'dict', mc_cfg: 'dict') -> 'None'", @@ -267,7 +267,7 @@ "_stem_claimant_fork": "callable _stem_claimant_fork(agent: 'str') -> 'str | None'", "_strip_excluded_auto_approve": "callable _strip_excluded_auto_approve(servers: 'dict[str, Any]') -> 'tuple[dict[str, Any], list[str]]'", "_strip_legacy_denied_commands": "callable _strip_legacy_denied_commands(config: 'dict') -> 'None'", - "_strip_ungoverned_auto_approve": "callable _strip_ungoverned_auto_approve(servers: 'dict[str, Any]') -> 'dict[str, Any]'", + "_strip_ungoverned_auto_approve": "callable _strip_ungoverned_auto_approve(servers: 'dict[str, Any]', *, audit: 'bool' = True) -> 'dict[str, Any]'", "_under_system_tmp": "callable _under_system_tmp(path: 'Path') -> 'bool'", "_user_dir": "callable _user_dir() -> 'Path'", "_user_overrides_path": "callable _user_overrides_path() -> 'Path'", diff --git a/test/test_agent_refactor_fork_refresh.py b/test/test_agent_refactor_fork_refresh.py index 778a912291d6..2c52372a1048 100644 --- a/test/test_agent_refactor_fork_refresh.py +++ b/test/test_agent_refactor_fork_refresh.py @@ -396,7 +396,7 @@ def strip(servers: dict[str, Any]) -> dict[str, Any]: return spec -def test_a_bound_shared_template_is_re_filtered_after_the_ceiling_tightens( +def test_a_bound_shared_template_is_not_rewritten_after_the_ceiling_tightens( shared_template: Path, ) -> None: doc = { @@ -409,8 +409,9 @@ def test_a_bound_shared_template_is_re_filtered_after_the_ceiling_tightens( shared_template.write_text(json.dumps(doc), encoding="utf-8") agent._refresh_forked_templates_locked(gated_off=frozenset()) written = json.loads(shared_template.read_text(encoding="utf-8")) - assert written["allowedTools"] == ["fs_read"] - assert written["mcpServers"] == {"gh": {"command": "gh-mcp"}} + # Kiro Crew does not own it, so it is never rewritten (owner decision, + # docs/decisions); a kiro-cli start on it is refused instead. + assert written == doc # Mount list and human-authored fields are untouched. assert written["tools"] == ["fs_read", "execute_bash"] assert written["prompt"] == "review carefully" @@ -447,29 +448,28 @@ def test_an_owned_spec_bound_to_a_crew_is_left_to_its_own_writer( assert owned.read_text(encoding="utf-8") == raw -def test_a_second_file_claiming_an_owned_name_is_filtered( +def test_a_second_file_claiming_an_owned_name_is_left_as_written( shared_template: Path, ) -> None: """The owned installer rewrites only ``.json``. Another file that declares the - owned name is one kiro-cli may load as it, so it is filtered like a shared template - while the owned file stays with its own writer.""" + owned name is one Kiro Crew does not own, so it is left as written (a kiro-cli + start on it is refused instead) while the owned file stays with its own writer.""" owned = shared_template.parent / "kirocrew.json" raw = json.dumps({"name": "kirocrew", "allowedTools": ["execute_bash"]}) owned.write_text(raw, encoding="utf-8") claimant = shared_template.parent / "pkg-kirocrew.json" claimant.write_text(json.dumps({"name": "kirocrew", "allowedTools": ["execute_bash"]})) agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert json.loads(claimant.read_text(encoding="utf-8"))["allowedTools"] == [] + assert json.loads(claimant.read_text(encoding="utf-8"))["allowedTools"] == ["execute_bash"] assert owned.read_text(encoding="utf-8") == raw assert fork_refresh._shared_template_held is False -def test_a_second_file_claiming_a_fork_name_is_filtered( +def test_a_second_file_claiming_a_fork_name_is_left_as_written( shared_template: Path, monkeypatch: pytest.MonkeyPatch ) -> None: """The fork refresh rewrites only the file the resolver picks for the fork. Another - file that claims the fork's name is filtered here; the fork's own file is not - written a second time.""" + file that claims the fork's name is not Kiro Crew's, so it is left as written.""" _forks(monkeypatch, {"crewfork": {"private_to": "crew", "forked_from": "custom"}}) _bindings(monkeypatch, {"crew": "crewfork"}) fork_spec = shared_template.parent / "crewfork.json" @@ -485,16 +485,16 @@ def record(path: Path, config: dict[str, Any]) -> None: monkeypatch.setattr(agent, "_atomic_json_write", record) agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert json.loads(claimant.read_text(encoding="utf-8"))["allowedTools"] == [] - assert written == ["crewfork.json", "squat.json"] + assert json.loads(claimant.read_text(encoding="utf-8"))["allowedTools"] == ["execute_bash"] + assert written == ["crewfork.json"] assert agent._fork_refresh_failed == frozenset() -def test_every_file_claiming_an_ambiguous_fork_name_is_filtered( +def test_every_file_claiming_an_ambiguous_fork_name_is_left_as_written( shared_template: Path, monkeypatch: pytest.MonkeyPatch ) -> None: """Two files declare the fork's name, so the fork refresh wrote neither and the fork - is refused at the spawn gate; both claimants are still filtered here.""" + is refused at the spawn gate; neither claimant is written.""" _forks(monkeypatch, {"crewfork": {"private_to": "crew", "forked_from": "custom"}}) _bindings(monkeypatch, {"crew": "crewfork"}) @@ -507,22 +507,12 @@ def ambiguous(_name: str) -> Path: path.write_text(json.dumps({"name": "crewfork", "allowedTools": ["execute_bash"]})) agent._refresh_forked_templates_locked(gated_off=frozenset()) for path in paths: - assert json.loads(path.read_text(encoding="utf-8"))["allowedTools"] == [], path.name + assert json.loads(path.read_text(encoding="utf-8"))["allowedTools"] == [ + "execute_bash" + ], path.name assert agent._fork_refresh_failed == frozenset({"crewfork"}) -def test_a_markdown_shared_template_is_warned_about_not_blocked( - shared_template: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture -) -> None: - md = shared_template.parent / "reviewer.md" - md.write_text("---\nname: reviewer\n---\n", encoding="utf-8") - monkeypatch.setattr(agent, "agent_spec_path", lambda _name: md) - with caplog.at_level(logging.WARNING, logger=agent.logger.name): - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert "markdown spec" in caplog.text - assert agent._fork_refresh_failed == frozenset() - - def test_a_fork_is_not_governed_twice_by_the_shared_pass( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -550,16 +540,6 @@ def test_no_forks_and_an_unreadable_config_records_no_failure( assert "shared template governance skipped" in caplog.text -def test_a_shared_template_whose_write_raises_is_held_for_a_retry( - shared_template: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - monkeypatch.setattr(agent, "_atomic_json_write", _raise) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - assert agent._fork_refresh_failed == frozenset() - - def test_a_shared_template_a_retry_cannot_fix_is_not_held( shared_template: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -577,7 +557,9 @@ def test_a_landed_pass_clears_the_hold( shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) agent._refresh_forked_templates_locked(gated_off=frozenset()) assert fork_refresh._shared_template_held is False - assert json.loads(shared_template.read_text(encoding="utf-8"))["allowedTools"] == [] + assert json.loads(shared_template.read_text(encoding="utf-8"))["allowedTools"] == [ + "execute_bash" + ] def test_a_deferred_pass_that_holds_a_shared_template_sets_the_rebuild_hold( @@ -610,25 +592,6 @@ def refuse(_member: str) -> None: assert "capability reconcile" in caplog.text -def test_an_unresolved_template_beside_an_unreadable_spec_is_held( - shared_template: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - monkeypatch.setattr(agent, "agent_spec_path", lambda _name: None) - from kiro_crew import agent_discovery - - real_read = agent_discovery._read_spec_bytes - - def unreadable(path: Path) -> bytes: - if path.name == "reviewer.json": - raise OSError(errno.EIO, "input/output error") - return real_read(path) - - monkeypatch.setattr(agent_discovery, "_read_spec_bytes", unreadable) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - - def _unreadable(monkeypatch: pytest.MonkeyPatch, filename: str, exc: OSError) -> None: """Make every read of *filename* in the agents directory raise *exc*.""" from kiro_crew import agent_discovery @@ -643,112 +606,24 @@ def read(path: Path) -> bytes: monkeypatch.setattr(agent_discovery, "_read_spec_bytes", read) -@pytest.mark.parametrize("filename", ["unrelated.json", "reviewer.json"]) -def test_a_spec_this_user_may_not_read_holds_until_it_reads( - shared_template: Path, monkeypatch: pytest.MonkeyPatch, filename: str -) -> None: - """kiro-cli cannot load a file whose permissions deny this user the read, but it - loads whatever grants the file carries once they are restored, and nothing moves - the ceiling then. So a denied read holds like any other; the probe keeps it from - rebuilding on every poll and ends the hold once the read succeeds.""" - from kiro_crew import agent_discovery - - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - if filename != "reviewer.json": - (shared_template.parent / filename).write_text(json.dumps({"name": "unrelated"})) - real_read = agent_discovery._read_spec_bytes - _unreadable(monkeypatch, filename, PermissionError(errno.EACCES, "permission denied")) - for _ in range(3): - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - assert fork_refresh.shared_template_hold_unchanged(_answer()) is True - monkeypatch.setattr(agent_discovery, "_read_spec_bytes", real_read) - assert fork_refresh.shared_template_hold_unchanged(_answer()) is False - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is False - assert json.loads(shared_template.read_text(encoding="utf-8"))["allowedTools"] == [] - - -def _sharing_violation() -> PermissionError: - """The error Windows raises while another process holds the file without read sharing.""" - exc = PermissionError(errno.EACCES, "the process cannot access the file") - exc.winerror = 32 # type: ignore[attr-defined] - return exc - - -@pytest.mark.parametrize("failing_read", [1, 2], ids=["scan", "locked-re-read"]) -def test_a_windows_sharing_violation_is_held_not_skipped( - shared_template: Path, monkeypatch: pytest.MonkeyPatch, failing_read: int -) -> None: - """Windows raises a sharing violation as PermissionError, but it clears when the - other handle closes: skipping it would let the memo advance on a template whose - denied grant is still on disk, so both the scan and the re-read under the lock - hold it.""" - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - from kiro_crew import agent_discovery - - real_read = agent_discovery._read_spec_bytes - reads: list[str] = [] - - def read(path: Path) -> bytes: - if path.name == "reviewer.json": - reads.append(path.name) - if len(reads) == failing_read: - raise _sharing_violation() - return real_read(path) - - monkeypatch.setattr(agent_discovery, "_read_spec_bytes", read) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - - -def test_a_spec_that_keeps_failing_stays_held_until_its_read_succeeds( - shared_template: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - """Releasing the hold while the read still fails would let the ceiling memo - advance with a denied grant possibly still on disk, and nothing would retry it - after the failure cleared. So a failure holds on every pass, and only the pass - that reads the file clears it.""" - (shared_template.parent / "unrelated.json").write_text(json.dumps({"name": "unrelated"})) - from kiro_crew import agent_discovery - - real_read = agent_discovery._read_spec_bytes - _unreadable(monkeypatch, "unrelated.json", OSError(errno.EIO, "input/output error")) - for _ in range(6): - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - monkeypatch.setattr(agent_discovery, "_read_spec_bytes", real_read) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is False - - -def test_a_stem_match_declaring_another_name_is_filtered_beside_a_declared_match( +def test_a_stem_match_declaring_another_name_is_left_as_written_beside_a_declared_match( shared_template: Path, ) -> None: """The single-file resolver picks the spec that DECLARES the name and leaves the one that only matches by filename, so resolving through it would leave the - stem match's denied grant on disk. This pass filters both.""" + stem match's denied grant on disk. Neither is written; the start check reads both.""" shared_template.write_text(json.dumps({"name": "legacy", "allowedTools": ["execute_bash"]})) declared = shared_template.parent / "renamed.json" declared.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) assert _REAL_AGENT_SPEC_PATH("reviewer", agents_dir=shared_template.parent) == declared agent._refresh_forked_templates_locked(gated_off=frozenset()) for path in (shared_template, declared): - assert json.loads(path.read_text(encoding="utf-8"))["allowedTools"] == [], path.name + assert json.loads(path.read_text(encoding="utf-8"))["allowedTools"] == [ + "execute_bash" + ], path.name assert fork_refresh._shared_template_held is False -def test_an_unreadable_spec_is_read_once_per_pass_whatever_the_number_of_bindings( - shared_template: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture -) -> None: - _bindings(monkeypatch, {"a": "reviewer", "b": "second", "c": "third"}) - (shared_template.parent / "unrelated.json").write_text(json.dumps({"name": "unrelated"})) - _unreadable(monkeypatch, "unrelated.json", OSError(errno.EIO, "input/output error")) - with caplog.at_level(logging.WARNING, logger=agent.logger.name): - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert caplog.text.count("could not be read; held for a retry") == 1 - - @requires_symlinks def test_the_retry_probe_never_follows_a_link_the_resolver_refuses( shared_template: Path, @@ -783,28 +658,29 @@ def test_an_unresolved_template_with_every_spec_readable_is_not_held( assert fork_refresh._shared_template_held is False -def test_a_template_declaring_the_name_under_another_filename_is_filtered( +def test_a_template_declaring_the_name_under_another_filename_is_left_as_written( shared_template: Path, ) -> None: renamed = shared_template.parent / "renamed.json" renamed.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert json.loads(renamed.read_text(encoding="utf-8"))["allowedTools"] == [] + assert json.loads(renamed.read_text(encoding="utf-8"))["allowedTools"] == ["execute_bash"] assert fork_refresh._shared_template_held is False -def test_every_spec_declaring_a_contested_name_is_filtered( +def test_every_spec_declaring_a_contested_name_is_left_as_written( shared_template: Path, ) -> None: - """Two specs declare the bound name, so which one kiro-cli loads is undefined: - both are filtered, and a markdown claimant does not stop the JSON one.""" + """Two specs declare the bound name; neither is Kiro Crew's, so neither is written.""" shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) other = shared_template.parent / "z-other.json" other.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) (shared_template.parent / "y-other.md").write_text("---\nname: reviewer\n---\n") agent._refresh_forked_templates_locked(gated_off=frozenset()) for path in (shared_template, other): - assert json.loads(path.read_text(encoding="utf-8"))["allowedTools"] == [], path.name + assert json.loads(path.read_text(encoding="utf-8"))["allowedTools"] == [ + "execute_bash" + ], path.name assert fork_refresh._shared_template_held is False @@ -844,81 +720,16 @@ def test_a_config_degradation_seen_earlier_in_the_process_does_not_hold( ) monkeypatch.setattr(KiroCrewConfig, "load", classmethod(lambda cls: repaired)) agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert json.loads(shared_template.read_text(encoding="utf-8"))["allowedTools"] == [] + assert json.loads(shared_template.read_text(encoding="utf-8"))["allowedTools"] == [ + "execute_bash" + ] assert fork_refresh._shared_template_held is False -def test_a_directory_listing_that_fails_after_its_stat_is_held( - shared_template: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - """``Path.glob`` swallows a listing error and yields nothing. Read that way, a - transient I/O error on the directory looks like an empty one, the memo advances, - and the bound template keeps its revoked grants after the directory recovers.""" - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - agents_dir = os.fspath(shared_template.parent) - real_scandir = os.scandir - - def failing(path: Any = ".") -> Any: - if isinstance(path, (str, os.PathLike)) and os.fspath(path) == agents_dir: - raise OSError(errno.EIO, "input/output error") - return real_scandir(path) - - monkeypatch.setattr(os, "scandir", failing) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - - def _hard_link(source: Path, alias: Path) -> None: os.link(source, alias) -def test_a_hard_linked_bound_template_is_held_until_the_extra_link_goes( - shared_template: Path, - tmp_path_factory: pytest.TempPathFactory, - caplog: pytest.LogCaptureFixture, -) -> None: - """The fenced reader refuses a second hard link for good, while kiro-cli reads the - same bytes without that fence. A refusal read as absence would advance the memo, - and removing the extra link later would not bring the filter back.""" - shared_template.write_text( - json.dumps({"name": "reviewer", "allowedTools": ["fs_read", "execute_bash"]}) - ) - alias = tmp_path_factory.mktemp("elsewhere") / "reviewer-copy.json" - _hard_link(shared_template, alias) - with caplog.at_level(logging.WARNING, logger=agent.logger.name): - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - assert json.loads(alias.read_text(encoding="utf-8"))["allowedTools"] == [ - "fs_read", - "execute_bash", - ], "nothing is written through the shared inode" - assert "hard link" in caplog.text - - alias.unlink() - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert json.loads(shared_template.read_text(encoding="utf-8"))["allowedTools"] == ["fs_read"] - assert fork_refresh._shared_template_held is False - - -def test_a_hard_link_made_between_the_scan_and_the_locked_re_read_is_held( - shared_template: Path, - monkeypatch: pytest.MonkeyPatch, - tmp_path_factory: pytest.TempPathFactory, -) -> None: - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - alias = tmp_path_factory.mktemp("elsewhere") / "alias.json" - real_scan = fork_refresh._scan_agent_specs - - def scan_then_link(agents_dir: Path) -> Any: - scanned = real_scan(agents_dir) - _hard_link(shared_template, alias) - return scanned - - monkeypatch.setattr(fork_refresh, "_scan_agent_specs", scan_then_link) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - - def test_a_hold_set_after_the_baseline_was_seeded_still_rebuilds( monkeypatch: pytest.MonkeyPatch, ) -> None: @@ -941,25 +752,6 @@ def fake_reporting(**kw: Any) -> tuple[Path, bool]: assert calls == [1], "a hold must not be skipped as an unchanged generation" -def test_a_regular_file_the_reader_refuses_is_held_and_a_directory_is_not( - shared_template: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - """A refusal of a regular file is never read as absence: its link count or kernel - path changed between the open and the check, and a retry may read it. A directory - at a spec name is no spec kiro-cli loads, so the reader's refusal of it holds - nothing.""" - from kiro_crew.agent_discovery import _SpecReadRefused - - (shared_template.parent / "folder.json").mkdir() - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is False - - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - _unreadable(monkeypatch, "reviewer.json", _SpecReadRefused("refusing to read a file")) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - - def test_a_directory_at_a_spec_name_holds_nothing_where_the_open_reports_it( shared_template: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -980,84 +772,6 @@ def _answer() -> int: return governance_answer_generation() -def test_an_unrelated_hard_linked_file_holds_and_the_probe_finds_it_unchanged( - shared_template: Path, - tmp_path_factory: pytest.TempPathFactory, - caplog: pytest.LogCaptureFixture, -) -> None: - """A hard-linked file no crew binds may still be the one that declares a bound name, - so it holds. While it is unchanged, under the same answer, a rebuild could only hold - on it again, so the probe says so, quietly; a new answer or a removed link does not.""" - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": []})) - notes = shared_template.parent / "notes.json" - notes.write_text(json.dumps({"name": "notes"}), encoding="utf-8") - alias = tmp_path_factory.mktemp("elsewhere") / "notes.json" - _hard_link(notes, alias) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - assert fork_refresh._shared_template_hold_record is not None - caplog.clear() - with caplog.at_level(logging.DEBUG, logger=agent.logger.name): - for _ in range(3): - assert fork_refresh.shared_template_hold_unchanged(_answer()) is True - assert caplog.records == [], "the probe must not log on every poll" - assert fork_refresh.shared_template_hold_unchanged(_answer() + 1) is False - alias.unlink() - assert fork_refresh.shared_template_hold_unchanged(_answer()) is False - - -def test_a_read_failure_that_clears_with_nothing_changed_on_disk_is_retried( - shared_template: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - """A sharing violation or an I/O error clears without touching the file's inode, - link count or mtime. The probe re-reads the file, so the poll after it clears - rebuilds instead of keeping the template unfiltered.""" - from kiro_crew import agent_discovery - - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - real_read = agent_discovery._read_spec_bytes - _unreadable(monkeypatch, "reviewer.json", _sharing_violation()) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - assert fork_refresh.shared_template_hold_unchanged(_answer()) is True - monkeypatch.setattr(agent_discovery, "_read_spec_bytes", real_read) - assert fork_refresh.shared_template_hold_unchanged(_answer()) is False - - -def test_a_file_held_unread_that_is_edited_is_retried( - shared_template: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": []})) - unrelated = shared_template.parent / "unrelated.json" - unrelated.write_text(json.dumps({"name": "unrelated"}), encoding="utf-8") - _unreadable(monkeypatch, "unrelated.json", OSError(errno.EIO, "input/output error")) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh.shared_template_hold_unchanged(_answer()) is True - stat = unrelated.stat() - os.utime(unrelated, ns=(stat.st_atime_ns, stat.st_mtime_ns + 1_000_000_000)) - assert fork_refresh.shared_template_hold_unchanged(_answer()) is False - - -@pytest.mark.parametrize("cause", ["write", "governance"]) -def test_a_hold_no_file_marks_records_nothing_to_probe( - shared_template: Path, monkeypatch: pytest.MonkeyPatch, cause: str -) -> None: - """A write or a governance pass that raised may clear with nothing on disk - changing, so the probe never vouches for it.""" - from kiro_crew.agent_materialization import auto_approve - - shared_template.write_text(json.dumps({"name": "reviewer", "allowedTools": ["execute_bash"]})) - monkeypatch.setattr(fork_refresh, "_shared_template_hold_record", (_answer(), (), None)) - if cause == "write": - monkeypatch.setattr(agent, "_atomic_json_write", _raise) - else: - monkeypatch.setattr(auto_approve, "_apply_allowed_tools_ceiling", _raise) - agent._refresh_forked_templates_locked(gated_off=frozenset()) - assert fork_refresh._shared_template_held is True - assert fork_refresh._shared_template_hold_record is None - assert fork_refresh.shared_template_hold_unchanged(_answer()) is False - - def test_an_unreadable_config_holds_until_a_load_reads_it_whole( shared_template: Path, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/test/test_bg_runtime_session_start_gate.py b/test/test_bg_runtime_session_start_gate.py index dec7650b6948..18ac70b41ff0 100644 --- a/test/test_bg_runtime_session_start_gate.py +++ b/test/test_bg_runtime_session_start_gate.py @@ -50,6 +50,10 @@ def _fresh_gates(monkeypatch): def _make_runtime() -> AcpRuntime: rt = AcpRuntime(work_dir="/tmp") + # The over-ceiling check is a thread hop before the gate; these tests count the + # loop yields between a start and its wire request, so it answers at once here + # (test_acp_runtime_session_start covers the check itself). + rt._refuse_over_ceiling_start = AsyncMock() # type: ignore[method-assign] proc = MagicMock() proc.stdout = asyncio.StreamReader() proc.stdin = MagicMock() diff --git a/test/test_conductor_allowed_tools_survive.py b/test/test_conductor_allowed_tools_survive.py index 6d3bd36391d3..90f77258e183 100644 --- a/test/test_conductor_allowed_tools_survive.py +++ b/test/test_conductor_allowed_tools_survive.py @@ -1237,13 +1237,13 @@ def security_write_fails(path: Path, data: Any) -> None: warnings = [r.getMessage() for r in caplog.records if r.levelno >= logging.WARNING] assert any("pending" in w and "read or written" in w for w in warnings), warnings - def test_a_bound_shared_template_whose_write_raises_holds_the_memo( + def test_a_bound_shared_template_over_the_ceiling_is_left_as_written_and_holds_nothing( self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path ) -> None: - """A crew-bound shared template carries a grant the ceiling now denies, and its - rewrite fails. The real rebuild must report the hold and the real hook must leave - the memo pending, so the next poll retries; once the write clears, the retry - re-filters the template and the hold is gone.""" + """A crew-bound shared template carries a grant the ceiling now denies. Kiro Crew + does not own it, so the real rebuild never writes it and holds nothing for it, + and the real hook advances the memo: the ceiling holds at a kiro-cli start on it + instead (``fork_refresh.require_shared_template_within_ceiling``).""" from types import SimpleNamespace from kiro_crew.agent_materialization import auto_approve, fork_refresh @@ -1266,77 +1266,17 @@ def test_a_bound_shared_template_whose_write_raises_holds_the_memo( auto_approve, "_may_auto_approve", lambda ref: ref != "web_fetch" and real_gate(ref) ) before = template.read_bytes() - real_write = agent._atomic_json_write - - def template_write_fails(path: Path, data: Any) -> None: - if path.name == "reviewer.json": - raise PermissionError(errno.EACCES, "Permission denied", str(path)) - real_write(path, data) - - with monkeypatch.context() as scoped: - scoped.setattr(agent, "_atomic_json_write", template_write_fails) - wrote, held = self._report() - self._pending_memo(scoped) - agent.reproject_for_ceiling_change() - pending = agent._projected_ceiling_generation - assert template.read_bytes() == before - assert wrote is True, "kirocrew.json was written; this is not a refusal" - assert held == [True], "a shared template left unfiltered must hold the ceiling memo" - assert pending is None, "the memo advanced over a template that was never rewritten" - assert self._report() == (True, [False]) - assert json.loads(template.read_text(encoding="utf-8"))["allowedTools"] == [] - - def test_an_unrelated_hard_linked_file_holds_without_a_rebuild_per_poll( - self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path - ) -> None: - """A hard-linked file no crew binds is refused by the fenced reader, so it may be - the file that declares a bound name and the memo stays pending. A rebuild cannot - read it either until it changes, so a poll or maintenance wake that finds it - unchanged under the same answer runs no rebuild (each one rewrites files kiro-cli - watches). The poll after its extra link goes rebuilds, and that rebuild lands.""" - from types import SimpleNamespace - - from kiro_crew.agent_materialization import fork_refresh - from kiro_crew.config.loader import KiroCrewConfig - - shared = self._shared_home_rig(monkeypatch, tmp_path) - agent.rebuild_agent_config() - (shared / "reviewer.json").write_text( - json.dumps({"name": "reviewer", "allowedTools": []}), encoding="utf-8" - ) - notes = shared / "notes.json" - notes.write_text(json.dumps({"name": "notes"}), encoding="utf-8") - elsewhere = tmp_path / "elsewhere" - elsewhere.mkdir() - alias = elsewhere / "notes.json" - os.link(notes, alias) - monkeypatch.setattr(fork_refresh, "_shared_template_held", False) - bindings = {"reviewer-crew": SimpleNamespace(kiro_agent="reviewer")} - monkeypatch.setattr( - KiroCrewConfig, "load", classmethod(lambda cls: SimpleNamespace(agents=bindings)) - ) - rebuilds: list[int] = [] - real_reporting = agent.rebuild_agent_config_reporting - - def counting(**kw: Any) -> tuple[Path, bool]: - rebuilds.append(1) - return real_reporting(**kw) - - monkeypatch.setattr(agent, "rebuild_agent_config_reporting", counting) + wrote, held = self._report() self._pending_memo(monkeypatch) - for _ in range(4): - agent.reproject_for_ceiling_change() - assert agent.retry_held_conductor_specs() is False - assert rebuilds == [1], "an unchanged hold under the same answer was rebuilt again" - assert agent._conductor_spec_held is True - assert agent._projected_ceiling_generation is None, "the memo advanced over the hold" - - alias.unlink() agent.reproject_for_ceiling_change() - assert rebuilds == [1, 1], "the poll after the held file changed must rebuild" - assert agent._conductor_spec_held is False + + assert template.read_bytes() == before + assert wrote is True + assert held == [False], "a spec Kiro Crew does not own holds nothing" assert agent._projected_ceiling_generation is not None + with pytest.raises(fork_refresh.SharedTemplateOverCeiling): + fork_refresh.require_shared_template_within_ceiling("reviewer", "") def test_a_malformed_config_overlay_holds_without_a_rebuild_per_poll( self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path @@ -1379,63 +1319,6 @@ def counting(**kw: Any) -> tuple[Path, bool]: assert agent._conductor_spec_held is False assert agent._projected_ceiling_generation is not None - @pytest.mark.skipif( - os.name == "nt" or (hasattr(os, "geteuid") and os.geteuid() == 0), - reason="a mode-000 file is still readable on Windows and to root", - ) - def test_a_bound_template_made_unreadable_is_filtered_once_readable_again( - self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path - ) -> None: - """A bound template whose permissions deny the read during a tighten cannot be - filtered, and kiro-cli cannot load it either. Once the permissions are restored, - kiro-cli loads the revoked grant, and no ceiling move follows to retry. So the - memo stays pending, no poll rebuilds while the read stays denied, and the poll - after the restore filters the template.""" - from types import SimpleNamespace - - from kiro_crew.agent_materialization import auto_approve, fork_refresh - from kiro_crew.config.loader import KiroCrewConfig - - shared = self._shared_home_rig(monkeypatch, tmp_path) - agent.rebuild_agent_config() - template = shared / "reviewer.json" - template.write_text( - json.dumps({"name": "reviewer", "tools": ["*"], "allowedTools": ["web_fetch"]}), - encoding="utf-8", - ) - bindings = {"reviewer-crew": SimpleNamespace(kiro_agent="reviewer")} - monkeypatch.setattr( - KiroCrewConfig, "load", classmethod(lambda cls: SimpleNamespace(agents=bindings)) - ) - real_gate = auto_approve._may_auto_approve - monkeypatch.setattr( - auto_approve, "_may_auto_approve", lambda ref: ref != "web_fetch" and real_gate(ref) - ) - rebuilds: list[int] = [] - real_reporting = agent.rebuild_agent_config_reporting - - def counting(**kw: Any) -> tuple[Path, bool]: - rebuilds.append(1) - return real_reporting(**kw) - - monkeypatch.setattr(agent, "rebuild_agent_config_reporting", counting) - self._pending_memo(monkeypatch) - template.chmod(0o000) - try: - for _ in range(3): - agent.reproject_for_ceiling_change() - finally: - template.chmod(0o644) - assert rebuilds == [1], "a read that stays denied was rebuilt again" - assert fork_refresh._shared_template_held is True - assert agent._projected_ceiling_generation is None, "the memo advanced over the hold" - - agent.reproject_for_ceiling_change() - assert rebuilds == [1, 1], "the poll after the read was restored must rebuild" - assert json.loads(template.read_text(encoding="utf-8"))["allowedTools"] == [] - assert agent._conductor_spec_held is False - assert agent._projected_ceiling_generation is not None - def test_the_hook_advances_only_when_the_rebuild_wrote_and_held_nothing( self, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/test/test_governed_spec_tail.py b/test/test_governed_spec_tail.py index 85015b438794..c21c0ff43056 100644 --- a/test/test_governed_spec_tail.py +++ b/test/test_governed_spec_tail.py @@ -44,12 +44,11 @@ #: ``_refresh_dynamic_fields`` ``includeMcpJson`` pin) rather than a derived one. _PRIMARY_SPEC_WRITER = "default_spec_commit.py" -#: ``(module, function)`` writers outside the tail by design. The shared-template -#: pass re-filters a user's own template in place: it runs the two grant passes -#: (``fork_refresh._apply_governance_passes``) and leaves the user's ``mcp.json`` -#: merge and KAS block as written, so the full tail would rewrite fields it does not -#: own. -_USER_TEMPLATE_WRITERS = frozenset({("fork_refresh.py", "_govern_json_spec")}) +#: ``(module, function)`` writers outside the tail by design. None: the shared-template +#: pass never writes a spec Kiro Crew does not own +#: (``fork_refresh.require_shared_template_within_ceiling`` refuses a kiro-cli start on +#: one the ceiling would narrow instead). +_USER_TEMPLATE_WRITERS: frozenset[tuple[str, str]] = frozenset() @pytest.fixture(autouse=True) diff --git a/test/test_mate_config_migration.py b/test/test_mate_config_migration.py index fe8d9325cea0..988bf807a8ca 100644 --- a/test/test_mate_config_migration.py +++ b/test/test_mate_config_migration.py @@ -97,6 +97,14 @@ def _cfg(**agents: KiroCrewAgentConfig) -> KiroCrewConfig: return cfg +def saved_notice(name: str) -> str: + """The one upgrade notice crewmate *name* is owed, as its thread will read it.""" + from kiro_crew.mate_upgrade_notice import claim_notice_prompt + from kiro_crew.members import member_slug + + return claim_notice_prompt(member_slug(name), name) + + def _file(mates: Path, name: str) -> dict: return json.loads((mates / f"{name}.json").read_text(encoding="utf-8")) @@ -282,7 +290,8 @@ def with_a_gain(name, base, source): def test_an_agent_whose_prompt_does_not_resolve_keeps_its_stated_limits(layout, persisted): - """It parses, so the deny it states is read; the default template would drop it.""" + """It parses, so the deny it states is read; the default template cannot carry it, + so the crewmate moves as is and its notice names the restriction.""" user, mates = layout _write(user, "kirocrew", _template("kirocrew")) deny = {"fs_write": {"deniedPaths": ["/etc/**"]}} @@ -296,10 +305,11 @@ def test_an_agent_whose_prompt_does_not_resolve_keeps_its_stated_limits(layout, report = migrate_existing_crewmates(cfg) - assert report.migrated == [] and report.moved_from_default == [] - assert "toolsSettings" in report.dry_run_failed["ijk"] - assert cfg.agents["ijk"].kiro_agent == "careful" - assert saved == {} and not (mates / "ijk.json").exists() + assert report.moved_from_default == ["ijk"] and report.dry_run_failed == {} + assert cfg.agents["ijk"].kiro_agent == "ijk" + assert saved["ijk"]["expected_values"] == {"kiro_agent": "careful"} + assert "fs_write" not in _file(mates, "ijk").get("excludedTools", []) + assert "toolsSettings for fs_write" in saved_notice("ijk") def test_a_prompt_that_does_not_resolve_keeps_its_switched_off_server_and_tools_off( @@ -378,8 +388,9 @@ def test_an_unreadable_agent_moves_onto_the_default_template_and_says_so(layout, ("permissions", {"rules": [{"capability": "fs_write", "effect": "deny"}]}), ], ) -def test_a_crewmate_whose_template_denies_writes_is_not_moved(layout, persisted, field, limit): - """The worker merge carries neither field, so the template's deny would be lost.""" +def test_a_crewmate_whose_template_denies_writes_moves_and_is_told(layout, persisted, field, limit): + """The worker merge carries neither field, so the deny does not apply on the + worker: the crewmate moves as is, nothing is withheld, and the one notice names it.""" user, mates = layout _write(user, "careful", _template("careful", **{field: limit})) cfg = _cfg(ijk=KiroCrewAgentConfig(member_id="ijk", kiro_agent="careful")) @@ -387,10 +398,14 @@ def test_a_crewmate_whose_template_denies_writes_is_not_moved(layout, persisted, report = migrate_existing_crewmates(cfg) - assert report.migrated == [] - assert field in report.dry_run_failed["ijk"] - assert cfg.agents["ijk"].kiro_agent == "careful" - assert not (mates / "ijk.json").exists() + assert report.migrated == ["ijk"] and report.dry_run_failed == {} + written = _file(mates, "ijk") + assert field not in written and "excludedTools" not in written + merged = mate_worker.merge_worker_spec(BASE, mate_worker.worker_additions(BASE, written)) + assert "fs_write" not in merged.get("excludedTools", []) + assert "fs_write" in report.moved_with_changes["ijk"] + notice = saved_notice("ijk") + assert "no longer apply" in notice and field in notice and "fs_write" in notice def test_allow_only_permissions_are_grants_not_a_restriction(layout, persisted): @@ -517,7 +532,7 @@ def test_the_default_template_as_installed_moves_onto_the_worker(layout, persist assert "hooks" not in _file(mates, "rty") -def test_a_hook_the_worker_base_lacks_still_keeps_the_crewmate_where_it_is(layout, persisted): +def test_a_hook_the_worker_base_lacks_moves_and_is_named(layout, persisted): user, mates = layout base = _installed_layout(user) hooks = {"postToolUse": [*AUDIT_HOOK["postToolUse"], {"matcher": "fs_write", "command": "x"}]} @@ -527,9 +542,10 @@ def test_a_hook_the_worker_base_lacks_still_keeps_the_crewmate_where_it_is(layou report = migrate_existing_crewmates(cfg) - assert report.migrated == [] - assert "hooks" in report.dry_run_failed["uio"] - assert not (mates / "uio.json").exists() + assert report.migrated == ["uio"] + written = _file(mates, "uio") + assert "hooks" not in written and "excludedTools" not in written + assert "its postToolUse hook on fs_write" in report.moved_with_changes["uio"] def _ray_kirocrew(base: dict) -> dict: @@ -812,8 +828,10 @@ def resolver(config, selected, *project, **kwargs): def test_other_records_and_a_skipped_crewmate_s_thread_are_left_alone(layout, persisted, threads): user, _mates = layout _write(user, "builder", _template("builder", mcpServers={"jira": JIRA}, tools=["@jira"])) - # A hook the worker base lacks keeps this one where it is (see the dry-run tests). - _write(user, "hooked", _template("hooked", hooks={"stop": [{"command": "x"}]})) + # A user-level agent named after it keeps this one where it is: its own file + # would never be read. + _write(user, "hooked", _template("hooked")) + _write(user, "sty", _template("sty")) cfg = _cfg( zxc=KiroCrewAgentConfig(member_id="zxc", kiro_agent="builder"), qwe=KiroCrewAgentConfig(member_id="qwe", kiro_agent="builder"), @@ -1052,3 +1070,306 @@ def _crash(_name, _changes=""): assert migrate_existing_crewmates(cfg).threads_repointed == {"zxc": [key]} assert _now(ec, key).template_id == mate_worker.MATE_CONDUCTOR_AGENT + + +def test_a_crewmate_shadowed_by_its_own_private_copy_moves_and_the_copy_is_retired( + layout, persisted, monkeypatch +): + """The copy that shadows its file is the fork Kiro Crew made for it, so it is + retired (kept for a rollback) and its lineage dropped before the repoint.""" + from kiro_crew import agent_state + + user, mates = layout + fork = _template("rust", prompt="Write Rust.", mcpServers={"jira": JIRA}, tools=["@jira"]) + _write(user, "rust", fork) + lineage = {"rust": {"forked_from": "kirocrew", "private_to": "rust"}} + monkeypatch.setattr(agent_state, "get_fork_info", lambda n, strict=False: lineage.get(n)) + monkeypatch.setattr(agent_state, "clear_fork_info", lambda n: lineage.pop(n, None)) + cfg = _cfg(rust=KiroCrewAgentConfig(member_id="rust", kiro_agent="rust")) + persisted(cfg) + + report = migrate_existing_crewmates(cfg) + + assert report.migrated == ["rust"] and report.dry_run_failed == {} + assert not (user / "rust.json").exists() + assert json.loads((mates.parent / "retired" / "rust.json").read_text()) == fork + assert lineage == {} + assert _file(mates, "rust")["prompt"] == "Write Rust." + + +def test_a_crewmate_shadowed_by_a_spec_crew_does_not_own_stays_and_it_is_untouched( + layout, persisted, monkeypatch +): + """Someone else's spec under the crewmate's id: the crewmate stays, is told once, + and that file is byte-identical after the pass.""" + from kiro_crew import agent_state + + user, mates = layout + raw = '{ "name": "pkg", "allowedTools": ["execute_bash"] }\n' + (user / "pkg.json").write_text(raw, encoding="utf-8") + monkeypatch.setattr(agent_state, "get_fork_info", lambda n, strict=False: None) + cfg = _cfg(pkg=KiroCrewAgentConfig(member_id="pkg", kiro_agent="pkg")) + persisted(cfg) + + report = migrate_existing_crewmates(cfg) + + assert report.migrated == [] + assert "user-level agent" in report.dry_run_failed["pkg"] + assert (user / "pkg.json").read_text(encoding="utf-8") == raw + assert not (mates / "pkg.json").exists() + + +def test_an_unmigrated_row_on_a_package_spec_moves_and_leaves_that_spec_byte_identical( + layout, persisted +): + """The reported case: a crewmate on a package-installed spec. It moves onto its own + file and the package's file is exactly as the package wrote it.""" + user, mates = layout + raw = json.dumps( + _template("pkg-reviewer", prompt="Review.", allowedTools=["execute_bash"]), indent=3 + ) + (user / "pkg-reviewer.json").write_text(raw, encoding="utf-8") + cfg = _cfg(rvw=KiroCrewAgentConfig(member_id="rvw", kiro_agent="pkg-reviewer")) + persisted(cfg) + + report = migrate_existing_crewmates(cfg) + + assert report.migrated == ["rvw"] + assert cfg.agents["rvw"].kiro_agent == "rvw" + assert (user / "pkg-reviewer.json").read_text(encoding="utf-8") == raw + assert _file(mates, "rvw")["prompt"] == "Review." + + +@pytest.fixture +def minting(monkeypatch): + """Provisioning that records its calls; chat evidence answered per name.""" + from kiro_crew import crewmate_prune_migration, memory_stores + + calls: list[tuple[str, bool]] = [] + chatted: set[str] = set() + + def provision(config, member, *, upgrade=False): + calls.append((member, upgrade)) + config.agents[member].member_id = member + config.agents[member].memory_store = f"member-{member}" + return f"member-{member}" + + monkeypatch.setattr(memory_stores, "provision_member_memory", provision) + monkeypatch.setattr( + crewmate_prune_migration, "_chatted", lambda cfg, name, sessions: name in chatted + ) + return calls, chatted + + +def test_a_used_synced_row_with_no_identity_gets_one_and_moves(layout, persisted, minting): + calls, chatted = minting + user, mates = layout + _write(user, "pkg-reviewer", _template("pkg-reviewer", prompt="Review.")) + cfg = _cfg( + rvw=KiroCrewAgentConfig(kiro_agent="pkg-reviewer", source="package"), + idle=KiroCrewAgentConfig(kiro_agent="pkg-reviewer", source="package"), + app=KiroCrewAgentConfig(kiro_agent="pkg-reviewer", source="auto-improvement"), + old=KiroCrewAgentConfig(kiro_agent="pkg-reviewer", source="builtin"), + ) + chatted.update({"rvw", "app", "old"}) + persisted(cfg) + + report = migrate_existing_crewmates(cfg) + + # Upgrade allocation keeps the name-derived slug its thread already uses. + assert calls == [("rvw", True)] + assert report.identities_minted == ["rvw"] and report.migrated == ["rvw"] + assert cfg.agents["rvw"].kiro_agent == "rvw" + assert "own memory" in report.moved_with_changes["rvw"] + # Never chatted (left for the prune), an app's row, a legacy built-in: untouched. + for name in ("idle", "app", "old"): + assert cfg.agents[name].member_id == "" and cfg.agents[name].kiro_agent == "pkg-reviewer" + + +def test_a_row_on_a_spec_crew_owns_is_not_given_an_identity(layout, persisted, minting): + calls, chatted = minting + user, _mates = layout + _write(user, "kirocrew", _template("kirocrew")) + cfg = _cfg(helper=KiroCrewAgentConfig(kiro_agent="kirocrew", source="kirocrew")) + chatted.add("helper") + persisted(cfg) + + migrate_existing_crewmates(cfg) + + assert calls == [] + + +def test_a_private_copy_that_changed_is_not_retired(layout, persisted, monkeypatch): + """Retirement re-proves the copy is Crew's under the spec lock, so a copy the fork + refresh or the user changed in between is left where it is.""" + from kiro_crew import agent_state + from kiro_crew.mate_config_migration import MateConfigError, _retire_shadow + + user, mates = layout + _write(user, "rust", _template("rust")) + monkeypatch.setattr(agent_state, "get_fork_info", lambda n, strict=False: None) + cfg = _cfg(rust=KiroCrewAgentConfig(member_id="rust", kiro_agent="rust")) + + with pytest.raises(MateConfigError, match="changed"): + _retire_shadow(cfg, "rust", user / "rust.json") + assert (user / "rust.json").is_file() + + +def test_retirement_rescans_the_agent_snapshot(layout, persisted, monkeypatch): + from kiro_crew import agent_state + from kiro_crew.config import loader_snapshots + + user, _mates = layout + _write(user, "rust", _template("rust", prompt="Write Rust.")) + lineage = {"rust": {"forked_from": "kirocrew", "private_to": "rust"}} + monkeypatch.setattr(agent_state, "get_fork_info", lambda n, strict=False: lineage.get(n)) + monkeypatch.setattr(agent_state, "clear_fork_info", lambda n: lineage.pop(n, None)) + scans: list[bool] = [] + monkeypatch.setattr( + loader_snapshots, "refresh_materialized_agents", lambda **kw: scans.append(True) + ) + cfg = _cfg(rust=KiroCrewAgentConfig(member_id="rust", kiro_agent="rust")) + persisted(cfg) + + assert migrate_existing_crewmates(cfg).migrated == ["rust"] + assert scans == [True] + + +def test_a_used_row_that_cannot_move_is_not_given_an_identity(layout, persisted, minting): + """The identity is given only once the move is proven: a row the migration keeps + where it is keeps the memory it had.""" + calls, chatted = minting + user, _mates = layout + _write(user, "pkg-reviewer", _template("pkg-reviewer")) + # A user-level agent under its own id keeps it where it is. + _write(user, "rvw", _template("rvw")) + cfg = _cfg(rvw=KiroCrewAgentConfig(kiro_agent="pkg-reviewer", source="package")) + chatted.add("rvw") + persisted(cfg) + + report = migrate_existing_crewmates(cfg) + + assert report.migrated == [] and "rvw" in report.dry_run_failed + assert calls == [] and cfg.agents["rvw"].member_id == "" + + +def test_moved_from_agents_lists_the_agents_moved_crewmates_ran_on(layout, persisted): + from kiro_crew.mate_config_migration import moved_from_agents + + user, _mates = layout + _write(user, "builder", _template("builder", mcpServers={"jira": JIRA}, tools=["@jira"])) + cfg = _cfg(zxc=KiroCrewAgentConfig(member_id="zxc", kiro_agent="builder")) + persisted(cfg) + + assert migrate_existing_crewmates(cfg).migrated == ["zxc"] + assert moved_from_agents() == {"builder"} + + +def test_a_minted_row_publishes_its_identity_with_its_repoint(layout, persisted, minting): + """One write carries ``member_id``, ``memory_store`` and ``kiro_agent`` together.""" + calls, chatted = minting + user, _mates = layout + _write(user, "pkg-reviewer", _template("pkg-reviewer")) + cfg = _cfg(rvw=KiroCrewAgentConfig(kiro_agent="pkg-reviewer", source="package")) + chatted.add("rvw") + saved = persisted(cfg) + + assert migrate_existing_crewmates(cfg).migrated == ["rvw"] + assert saved["rvw"]["changed_fields"] == {"kiro_agent", "member_id", "memory_store"} + assert saved["rvw"]["expected_values"] == {"kiro_agent": "pkg-reviewer"} + + +def test_a_minted_row_whose_move_fails_before_publish_keeps_its_memory( + layout, persisted, minting, monkeypatch +): + """A failure after the allocation and before the publish retires the allocation, + so the row stays on its old agent with the memory it had.""" + from kiro_crew import mate_config_migration as migration + from kiro_crew import memory_stores + + calls, chatted = minting + user, _mates = layout + _write(user, "pkg-reviewer", _template("pkg-reviewer")) + cfg = _cfg(rvw=KiroCrewAgentConfig(kiro_agent="pkg-reviewer", source="package")) + chatted.add("rvw") + saved = persisted(cfg) + retired: list[str] = [] + monkeypatch.setattr( + memory_stores, "retire_unpublished_allocation", lambda c, n, store, **kw: retired.append(n) + ) + real_record = migration._record_migrated + + def disk_full(name, source="", *, moved=True, reason=""): + if not moved: + raise OSError(28, "No space left on device") + real_record(name, source, moved=moved, reason=reason) + + monkeypatch.setattr(migration, "_record_migrated", disk_full) + + report = migrate_existing_crewmates(cfg) + + assert report.migrated == [] and "rvw" in report.repoint_failed + assert calls == [("rvw", True)] and retired == ["rvw"] + assert saved == {} and cfg.agents["rvw"].kiro_agent == "pkg-reviewer" + + +def test_a_private_copy_with_a_markdown_twin_is_not_retired(layout, persisted, monkeypatch): + """Retiring the copy would leave the user's markdown twin shadowing the crewmate's + file, so the crewmate stays, is told once, and both files are left as they are.""" + from kiro_crew import agent_state + + user, mates = layout + _write(user, "rust", _template("rust")) + twin = "---\nname: rust\n---\nmine\n" + (user / "rust.md").write_text(twin, encoding="utf-8") + lineage = {"rust": {"forked_from": "kirocrew", "private_to": "rust"}} + monkeypatch.setattr(agent_state, "get_fork_info", lambda n, strict=False: lineage.get(n)) + cfg = _cfg(rust=KiroCrewAgentConfig(member_id="rust", kiro_agent="rust")) + persisted(cfg) + + report = migrate_existing_crewmates(cfg) + + assert report.migrated == [] and "user-level agent" in report.dry_run_failed["rust"] + assert (user / "rust.json").is_file() and (user / "rust.md").read_text() == twin + assert lineage + + +@pytest.mark.parametrize("step", ["lineage", "repoint"]) +def test_a_retirement_whose_move_fails_puts_the_copy_back(layout, persisted, monkeypatch, step): + """A lineage drop or a repoint that fails after the copy moved puts the copy and + its lineage back, so the crewmate keeps running where it was.""" + from kiro_crew import agent_state + from kiro_crew import mate_config_migration as migration + + user, _mates = layout + fork = _template("rust", prompt="Write Rust.") + _write(user, "rust", fork) + original = {"forked_from": "kirocrew", "private_to": "rust"} + lineage = {"rust": dict(original)} + monkeypatch.setattr(agent_state, "get_fork_info", lambda n, strict=False: lineage.get(n)) + monkeypatch.setattr( + agent_state, + "set_fork_info", + lambda n, f, p: lineage.__setitem__(n, {"forked_from": f, "private_to": p}), + ) + if step == "lineage": + + def unwritable(_name): + raise OSError(13, "Permission denied") + + monkeypatch.setattr(agent_state, "clear_fork_info", unwritable) + else: + monkeypatch.setattr(agent_state, "clear_fork_info", lambda n: lineage.pop(n, None)) + + def refused(*_a, **_k): + raise OSError(28, "No space left on device") + + monkeypatch.setattr(migration, "_repoint", refused) + cfg = _cfg(rust=KiroCrewAgentConfig(member_id="rust", kiro_agent="rust")) + persisted(cfg) + + report = migrate_existing_crewmates(cfg) + + assert report.migrated == [] and "rust" in report.repoint_failed + assert json.loads((user / "rust.json").read_text()) == fork + assert lineage == {"rust": original} diff --git a/test/test_mate_upgrade_notice.py b/test/test_mate_upgrade_notice.py index 07c684014dac..39cafc122a29 100644 --- a/test/test_mate_upgrade_notice.py +++ b/test/test_mate_upgrade_notice.py @@ -142,8 +142,9 @@ def test_the_migration_owes_the_notice_only_for_crewmates_it_moved(monkeypatch, def test_a_crewmate_the_migration_left_behind_tells_its_user_why_once( monkeypatch, tmp_path, dispatched ): - """``orbit``'s template denies writes under ``/etc``, which its worker would not - keep, so it stays on its template; its thread says so once, with the reason.""" + """A user-level agent Kiro Crew does not own carries ``orbit``'s id, so its own file + would never be read and it stays on its template; its thread says so once, with + the reason.""" _migrate_atlas_and_orbit(monkeypatch, tmp_path) assert notice.claim_kept_notice(members_mod.member_slug("atlas"), "atlas") == "" slot = _thread("orbit") @@ -154,7 +155,7 @@ def test_a_crewmate_the_migration_left_behind_tells_its_user_why_once( assert len(dispatched) == 1 key, prompt = dispatched[0] assert key == slot.key and prompt.startswith(notice.WORKER_UPGRADE_PREFIX) - assert "toolsSettings" in prompt and "left as it was" in prompt + assert "user-level agent" in prompt and "left as it was" in prompt def test_a_left_behind_crewmate_that_moves_later_never_says_it_was_left( @@ -162,16 +163,11 @@ def test_a_left_behind_crewmate_that_moves_later_never_says_it_was_left( ): """Start 1 leaves ``orbit`` behind with its thread unopened; start 2 moves it. Its thread then gets the moved notice only, never the stale kept one.""" - import json - import kiro_crew.config.paths as paths_mod from kiro_crew.mate_config_migration import migrate_existing_crewmates cfg = _migrate_atlas_and_orbit(monkeypatch, tmp_path) - shell = paths_mod.kiro_agents_dir() / "shell.json" - spec = json.loads(shell.read_text(encoding="utf-8")) - spec.pop("toolsSettings") - shell.write_text(json.dumps(spec), encoding="utf-8") + (paths_mod.kiro_agents_dir() / "orbit.json").unlink() assert migrate_existing_crewmates(cfg).migrated == ["orbit"] slot = _thread("orbit") @@ -216,7 +212,7 @@ async def _drive(): def _migrate_atlas_and_orbit(monkeypatch, tmp_path): - """``atlas`` moves; ``orbit``'s worker would not keep its write limit, so it stays.""" + """``atlas`` moves; a user-level agent named ``orbit`` shadows its file, so it stays.""" import json import kiro_crew.config.paths as paths_mod @@ -232,16 +228,9 @@ def _migrate_atlas_and_orbit(monkeypatch, tmp_path): mates.mkdir(parents=True) (mates / "kirocrew-mate-worker.json").write_text(json.dumps(base), encoding="utf-8") (user / "plain.json").write_text(json.dumps({**base, "name": "plain"}), encoding="utf-8") - (user / "shell.json").write_text( - json.dumps( - { - **base, - "name": "shell", - "toolsSettings": {"fs_write": {"deniedPaths": ["/etc/**"]}}, - } - ), - encoding="utf-8", - ) + (user / "shell.json").write_text(json.dumps({**base, "name": "shell"}), encoding="utf-8") + # Someone else's agent under orbit's id: not Kiro Crew's to move or write. + (user / "orbit.json").write_text(json.dumps({**base, "name": "orbit"}), encoding="utf-8") cfg = KiroCrewConfig() cfg.agents = { "atlas": KiroCrewAgentConfig(member_id="atlas", kiro_agent="plain"), diff --git a/test/test_session_start_gate.py b/test/test_session_start_gate.py index e6132a91b101..0629b2da59b6 100644 --- a/test/test_session_start_gate.py +++ b/test/test_session_start_gate.py @@ -71,6 +71,10 @@ def _make_runtime( # Sub-second budgets through the cached seams production reads. rt._session_start_timeout = 0.05 rt._start_collect_timeout = 0.4 + # The over-ceiling check is a thread hop before the gate; these tests count the + # loop yields between a start and its wire request, so it answers at once here + # (test_acp_runtime_session_start covers the check itself). + rt._refuse_over_ceiling_start = AsyncMock() # type: ignore[method-assign] if backend is not None: rt._acp_backend = backend return rt, reader, proc diff --git a/test/test_shared_template_unowned_spec.py b/test/test_shared_template_unowned_spec.py new file mode 100644 index 000000000000..d8115def454b --- /dev/null +++ b/test/test_shared_template_unowned_spec.py @@ -0,0 +1,365 @@ +"""Kiro Crew never writes a crew-bound spec it does not own, and the ceiling still holds. + +That file is the one kiro-cli loads for the agent outside Kiro Crew, so rewriting it +would strip the user's own sessions. The rebuild leaves it byte-identical on every harness; KAS +filters the grants in its projection; and a kiro-cli start on such a file is refused +while its grants exceed the ceiling, since kiro-cli would honour them before Crew's gate. +""" + +from __future__ import annotations + +import json +from pathlib import Path +from types import SimpleNamespace +from typing import Any, Iterator + +import pytest + +from kiro_crew import agent, agent_state +from kiro_crew.acp import kas_agents +from kiro_crew.acp_backends import ( + ACP_BACKEND_KAS, + ACP_BACKEND_KIRO, + ACP_BACKENDS_KNOWN, + ACP_BACKENDS_SPEC_FILE_GRANTS, +) +from kiro_crew.agent_materialization import auto_approve, fork_refresh +from kiro_crew.config.loader import KiroCrewConfig + +#: A spec a package manager installed: denied grants on both lists, odd formatting. +_RAW = ( + '{\n "name": "pkg-reviewer",\n "prompt": "review",\n' + ' "tools": ["web_search", "web_fetch", "@gh"],\n' + ' "allowedTools": ["web_search", "web_fetch"],\n' + ' "mcpServers": {"gh": {"command": "gh-mcp", "autoApprove": ["*"]}}\n}\n' +) + + +def _denied(ref: str) -> bool: + return ref == "web_fetch" or ref.startswith("@gh") + + +@pytest.fixture(autouse=True) +def _settled(_floor_monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: + _floor_monkeypatch.setattr(agent, "_fork_refresh_failed", frozenset()) + _floor_monkeypatch.setattr(fork_refresh, "_shared_template_held", False) + _floor_monkeypatch.setattr(fork_refresh, "_shared_template_hold_record", None) + yield + + +@pytest.fixture +def bound_spec(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + monkeypatch.setattr(agent_state, "all_fork_info", lambda: {}) + monkeypatch.setattr(agent_state, "get_capabilities", lambda _name: None) + monkeypatch.setattr(agent, "kiro_agents_dir_path", lambda: tmp_path) + monkeypatch.setattr(agent, "agent_spec_path", lambda name: tmp_path / f"{name}.json") + # The tightened ceiling, as the governance passes see it. + monkeypatch.setattr(auto_approve, "_may_auto_approve", lambda ref: not _denied(ref)) + + def strip(servers: dict[str, Any], **_kw: Any) -> dict[str, Any]: + return { + k: {kk: vv for kk, vv in v.items() if kk != "autoApprove"} for k, v in servers.items() + } + + monkeypatch.setattr(auto_approve, "_strip_ungoverned_auto_approve", strip) + spec = tmp_path / "pkg-reviewer.json" + spec.write_text(_RAW, encoding="utf-8") + _bind(monkeypatch, {"reviewer-crew": "pkg-reviewer"}) + return spec + + +def _bind(monkeypatch: pytest.MonkeyPatch, bound: dict[str, str]) -> None: + agents = {crew: SimpleNamespace(kiro_agent=name) for crew, name in bound.items()} + cfg = SimpleNamespace(agents=agents) + monkeypatch.setattr(KiroCrewConfig, "load", classmethod(lambda cls: cfg)) + + +def test_a_bound_unowned_spec_is_byte_identical_after_a_rebuild(bound_spec: Path) -> None: + agent._refresh_forked_templates_locked(gated_off=frozenset()) + assert bound_spec.read_text(encoding="utf-8") == _RAW + # Nothing is read for it, so nothing is held for it either. + assert fork_refresh._shared_template_held is False + + +def test_a_kas_session_from_that_spec_still_cannot_auto_approve_a_denied_tool( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + agent._refresh_forked_templates_locked(gated_off=frozenset()) + monkeypatch.setattr(kas_agents, "may_skip_gate_now", lambda ref: not _denied(ref)) + spec = json.loads(bound_spec.read_text(encoding="utf-8")) + projected = kas_agents.to_client_custom_agent("pkg-reviewer", spec, "review") + allows = [ + rule + for rule in (projected.get("permissions") or {}).get("rules", []) + if rule.get("effect") == "allow" + ] + granted = json.dumps(allows) + assert "web_fetch" not in granted + assert "gh" not in granted + # The grant the ceiling permits still travels, so the filter is not a blanket drop. + assert [r.get("capability") for r in allows] == ["web_search"] + + +def test_a_kiro_cli_start_on_it_is_refused_and_the_file_is_untouched(bound_spec: Path) -> None: + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="pkg-reviewer.json"): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + assert bound_spec.read_text(encoding="utf-8") == _RAW + + +def test_a_kiro_cli_start_within_the_ceiling_is_not_refused(bound_spec: Path) -> None: + bound_spec.write_text( + json.dumps({"name": "pkg-reviewer", "allowedTools": ["web_search"]}), encoding="utf-8" + ) + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_empty_grant_lists_on_a_governed_server_are_not_refused(bound_spec: Path) -> None: + """The strip drops an empty ``autoApprove`` key, but that removes no grant.""" + bound_spec.write_text( + json.dumps( + { + "name": "pkg-reviewer", + "allowedTools": [], + "mcpServers": {"gh": {"command": "gh-mcp", "autoApprove": []}}, + } + ), + encoding="utf-8", + ) + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_a_non_list_auto_approve_on_a_governed_server_is_refused(bound_spec: Path) -> None: + bound_spec.write_text( + json.dumps( + {"name": "pkg-reviewer", "mcpServers": {"gh": {"command": "x", "autoApprove": "*"}}} + ), + encoding="utf-8", + ) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_a_kas_start_on_it_is_not_refused(bound_spec: Path) -> None: + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KAS) + assert bound_spec.read_text(encoding="utf-8") == _RAW + + +def test_a_start_on_an_unbound_spec_is_not_checked( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + loose = bound_spec.parent / "loose.json" + loose.write_text(json.dumps({"name": "loose", "allowedTools": ["web_fetch"]})) + fork_refresh.require_shared_template_within_ceiling("loose", ACP_BACKEND_KIRO) + + +def test_a_crew_owned_spec_is_left_to_its_own_writer( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + owned = bound_spec.parent / "kirocrew.json" + raw = json.dumps({"name": "kirocrew", "allowedTools": ["web_fetch"]}) + owned.write_text(raw, encoding="utf-8") + _bind(monkeypatch, {"default": "kirocrew"}) + agent._refresh_forked_templates_locked(gated_off=frozenset()) + fork_refresh.require_shared_template_within_ceiling("kirocrew", ACP_BACKEND_KIRO) + assert owned.read_text(encoding="utf-8") == raw + + +def test_an_unreadable_binding_refuses_the_start(monkeypatch: pytest.MonkeyPatch) -> None: + def boom(cls: object) -> None: + raise OSError("config.json unreadable") + + monkeypatch.setattr(KiroCrewConfig, "load", classmethod(boom)) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="could not be checked"): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_a_kiro_cli_client_spawn_runs_the_check_first(tmp_path: Path) -> None: + """The ``AcpClient`` route to kiro-cli runs the same check as the harness.""" + from unittest.mock import patch + + from acp_launch_capture import capture + + from kiro_crew.acp import client_start + + calls: list[tuple[str | None, str]] = [] + seen: list[str | None] = [] + capture( + ACP_BACKEND_KIRO, + tmp_path, + extra_patches=[ + patch.object( + client_start, + "require_shared_template_within_ceiling", + side_effect=lambda agent, backend: calls.append((agent, backend)), + ) + ], + observe=lambda client: seen.append(client._agent), + ) + assert calls == [(seen[0], ACP_BACKEND_KIRO)] + + +def test_every_claimant_of_the_name_is_checked(bound_spec: Path) -> None: + """Which of two files claiming a name kiro-cli loads is undefined, so one over the + ceiling refuses the start even when the stem match is clean.""" + bound_spec.write_text(json.dumps({"name": "pkg-reviewer", "allowedTools": []})) + other = bound_spec.parent / "z-other.json" + other.write_text(json.dumps({"name": "pkg-reviewer", "allowedTools": ["web_fetch"]})) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="z-other.json"): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_a_hard_linked_spec_refuses_the_start( + bound_spec: Path, tmp_path_factory: pytest.TempPathFactory +) -> None: + """The fenced reader refuses a second link, and kiro-cli reads the bytes anyway.""" + import os + + bound_spec.write_text(json.dumps({"name": "pkg-reviewer", "allowedTools": []})) + os.link(bound_spec, tmp_path_factory.mktemp("outside") / "elsewhere.json") + with pytest.raises(fork_refresh.SharedTemplateOverCeiling): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_a_hard_linked_markdown_file_does_not_refuse_a_json_start( + bound_spec: Path, tmp_path_factory: pytest.TempPathFactory +) -> None: + """kiro-cli never loads the markdown form, so an unread one cannot be its spec.""" + import os + + bound_spec.write_text(json.dumps({"name": "pkg-reviewer", "allowedTools": []})) + md = bound_spec.parent / "notes.md" + md.write_text("---\nname: notes\n---\nhello\n", encoding="utf-8") + os.link(md, tmp_path_factory.mktemp("outside") / "elsewhere.md") + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +@pytest.mark.parametrize("backend", sorted(ACP_BACKENDS_KNOWN - ACP_BACKENDS_SPEC_FILE_GRANTS)) +def test_only_a_host_that_reads_the_file_grants_is_refused(bound_spec: Path, backend: str) -> None: + """Every other host gets Crew's projection, which filters or drops both lists.""" + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", backend) + + +def test_kiro_cli_is_the_host_that_reads_the_file_grants() -> None: + assert ACP_BACKEND_KIRO in ACP_BACKENDS_SPEC_FILE_GRANTS + + +def test_a_binding_on_the_file_stem_is_checked_under_its_declared_name( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A row can record the file stem while kiro-cli is dispatched the declared name.""" + from kiro_crew.config import loader + + stem = bound_spec.parent / "KiroPkg-captain.json" + stem.write_text(json.dumps({"name": "captain", "allowedTools": ["web_fetch"]})) + _bind(monkeypatch, {"captain-crew": "KiroPkg-captain"}) + monkeypatch.setattr( + loader, + "dispatch_kiro_agent", + lambda name: "captain" if name == "KiroPkg-captain" else name, + ) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="KiroPkg-captain.json"): + fork_refresh.require_shared_template_within_ceiling("captain", ACP_BACKEND_KIRO) + + +def test_a_config_load_missing_a_file_still_checks_the_agent( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A load that could not read a config file whole may lack the binding this start + runs, so an absent binding is no proof the spec is unbound.""" + cfg = SimpleNamespace(agents={}, _overlay_unreadable=True) + monkeypatch.setattr(KiroCrewConfig, "load", classmethod(lambda cls: cfg)) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="pkg-reviewer.json"): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_the_spec_a_moved_crewmate_ran_on_is_still_checked( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A conversation from before the move keeps the template its record names, so + that template stays checked after no binding names it any more.""" + from kiro_crew import mate_config_migration + + _bind(monkeypatch, {"rvw": "rvw"}) + monkeypatch.setattr(mate_config_migration, "moved_from_agents", lambda: {"pkg-reviewer"}) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="pkg-reviewer.json"): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_an_unreadable_ledger_checks_the_agent_rather_than_skipping_it( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + from kiro_crew import mate_config_migration + + def unreadable() -> frozenset[str]: + raise mate_config_migration.LedgerUnreadable("migrated.json is unreadable") + + _bind(monkeypatch, {"rvw": "rvw"}) + monkeypatch.setattr(mate_config_migration, "moved_from_agents", unreadable) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="pkg-reviewer.json"): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + + +def test_a_moved_stem_binding_is_checked_under_its_declared_name( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """The ledger records the stem the binding used; the old conversation is + dispatched the name that file declares.""" + from kiro_crew import mate_config_migration + from kiro_crew.config import loader + + stem = bound_spec.parent / "KiroPkg-captain.json" + stem.write_text(json.dumps({"name": "captain", "allowedTools": ["web_fetch"]})) + _bind(monkeypatch, {"rvw": "rvw"}) + monkeypatch.setattr(mate_config_migration, "moved_from_agents", lambda: {"KiroPkg-captain"}) + monkeypatch.setattr( + loader, + "dispatch_kiro_agent", + lambda name: "captain" if name == "KiroPkg-captain" else name, + ) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling, match="KiroPkg-captain.json"): + fork_refresh.require_shared_template_within_ceiling("captain", ACP_BACKEND_KIRO) + + +def test_a_refused_start_writes_no_withheld_record( + bound_spec: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """The check runs the passes on a copy and writes nothing, so it records no + ``mcp_auto_approve_withheld`` decision: the file still grants the tool.""" + events: list[dict[str, Any]] = [] + monkeypatch.setattr( + agent, "sel", lambda: SimpleNamespace(log_api_access=lambda **kw: events.append(kw)) + ) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling): + fork_refresh.require_shared_template_within_ceiling("pkg-reviewer", ACP_BACKEND_KIRO) + assert [e for e in events if e.get("operation") == "mcp_auto_approve_withheld"] == [] + + +def test_a_refused_auto_approve_only_spec_writes_no_record( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """The real strip runs on the check path with its audit off: a spec whose only + over-ceiling grant is a server ``autoApprove`` records no withheld decision.""" + from kiro_crew.platform import governance + + monkeypatch.setattr(agent_state, "all_fork_info", lambda: {}) + monkeypatch.setattr(agent_state, "get_capabilities", lambda _name: None) + monkeypatch.setattr(agent, "kiro_agents_dir_path", lambda: tmp_path) + monkeypatch.setattr(agent, "agent_spec_path", lambda name: tmp_path / f"{name}.json") + seen: list[bool] = [] + real = governance.strip_ungoverned_auto_approve + + def recording(servers: Any, *, audit: bool = True, **kw: Any) -> Any: + seen.append(audit) + return { + k: {kk: vv for kk, vv in v.items() if kk != "autoApprove"} for k, v in servers.items() + } + + monkeypatch.setattr(auto_approve, "strip_ungoverned_auto_approve", recording) + (tmp_path / "pkg.json").write_text( + json.dumps({"name": "pkg", "mcpServers": {"gh": {"command": "gh", "autoApprove": ["*"]}}}) + ) + _bind(monkeypatch, {"crew": "pkg"}) + with pytest.raises(fork_refresh.SharedTemplateOverCeiling): + fork_refresh.require_shared_template_within_ceiling("pkg", ACP_BACKEND_KIRO) + assert seen == [False] + assert real is not recording