Skip to content

fix(apps): ask the shared-home guard before app spec writes and removals - #24034

Open
smeyffret wants to merge 2 commits into
mainfrom
fix/agents-spec-home-guard
Open

smeyffret wants to merge 2 commits into
mainfrom
fix/agents-spec-home-guard

Conversation

@smeyffret

@smeyffret smeyffret commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A gateway started on a non-default KIROCREW_HOME leaves the shared ~/.kiro/agents directory that another home owns untouched, for app MCP entries and app agent files alike.

A preview gateway on its own KIROCREW_HOME rewrote one entry in the live ~/.kiro/agents/kirocrew.json: mcpServers["auto-improvement:auto-improvement"].command became the preview build's interpreter. Every new live session then started that MCP server from the preview tree, and deleting the tree broke it for all of them. The same boot pass can also delete the owner's app agent specs and launch copies.

Why it matters

Any preview or scratch-home gateway poisons or prunes the live gateway's agent specs at boot. An agent cannot repair kirocrew.json because tool calls are refused write access to it, so the owner has to edit the file by hand.

Not a goal

  • Changing who owns the shared directory. The ownership decision stays _decline_shared_agent_home, unchanged.
  • Uninstall residue cleanup (purge_uninstalled_agent_residue). It removes lock files only for specs that are already gone, and overlays in the instance's own overlay dir.

What changed (motivation → approach → change)

Path into the shared directory Before After
App MCP register / deregister / prune (_write_mcp_json_unlocked) wrote kirocrew.json asks the guard; writes nothing when refused
First app MCP write from a relocated home into an empty shared directory wrote an app-only kirocrew.json with no owner also writes the managed kirocrew-core entry, whose KIROCREW_HOME pin lets this home change the file later; writes nothing if that entry cannot be resolved
App agent deregister (_deregister_agents) unlinked <app>--*.json and launch copies asks once per sweep under agents_spec_lock; deletes nothing and reports 0 removed
App agent upgrade prune (_prune_stale_app_resources) unlinked dropped specs and launch copies same

At boot every gateway re-registers its enabled apps and deregisters the ones it denies, whatever its home. A stdio server that runs a kiro_crew module gets command = sys.executable. The managed-spec rebuild and the derived specs already ask _declined_foreign_spec_write, and _register_agents asks it per app agent, but the app MCP writer and the two app removals did not. That is the root cause.

The MCP writer now asks that guard and returns False when refused. When it creates the file, it asks _new_shared_spec_needs_owner_pin (in agent_parts/shared_home.py, exported through agent.py) whether the new file needs this home's kirocrew-core pin; bridges_mcp.py then registers nothing and reports nothing removed. The agent removals ask it once per sweep, through _owned_agents_dir in bridges_agents.py, which holds agents_spec_lock from the answer through the last unlink so ownership cannot change in between. When another home owns the directory they delete nothing and report 0 removed, the settled state registration already leaves there, so kirocrew app disable completes and the health watch does not retry it. A lock that cannot be opened or acquired raises instead, so disable reports the cleanup as incomplete and the watch retries; the denied-app branch of backend_runtime/startup.py revokes agents, skills and MCP servers in separate steps, so that failure skips neither of the others. Writes keep the guard's own exemptions: a default-home instance, a self-pinned instance, a private redirected target and isolated_agents_dir write as before. Removals ask a narrower question, _declined_foreign_spec_removal: do the specs belong to another data home? A removal of this home's own app entries pins nothing of the running checkout, so the ephemeral arms (a linked worktree, a temp checkout) do not refuse it. The MCP entry removals pass removal=True to _write_mcp_json_unlocked for the same reason. The spec docs in docs/system-specs/modules/app-kit-platform.md and config.md and the guard's docstring in agent_parts/spec_files.py name the new callers.

Backwards compatibility

Compatible: removals behave as on main for every instance except a non-default home whose shared directory another home owns, which now deletes nothing. A default-home gateway run from a linked worktree or a temp checkout still removes a disabled or denied app's agents and MCP entries. Writes behave as on main for the default home, a self-pinned home and isolated_agents_dir. One write changes on the default home: a default-home gateway run from a linked worktree or a temp checkout no longer registers app MCP entries into the shared kirocrew.json, the same refusal every other spec writer already gives that instance.

Removes nothing: a refused instance keeps the owner's files instead of deleting them; no key, record, validator or function is removed.

Tests

test/test_app_bridges_home_guard.py, 16 tests, temp dirs only:

  • a foreign-owned kirocrew.json stays byte-identical through app MCP registration and deregistration, with one SEL denial;
  • a foreign-owned directory keeps app agent specs and launch copies through _deregister_agents and the upgrade prune;
  • disabling an app there completes with no error and leaves the files in place;
  • when the agent-spec lock cannot be taken, _deregister_agents raises, deregister_app reports the cleanup as incomplete so the health watch retries, the upgrade prune still prunes MCP entries, and the boot reconcile still revokes a denied app's skills and MCP servers;
  • a default-home instance still registers MCP servers and still prunes dropped agents;
  • isolated_agents_dir still accepts registration;
  • a relocated home that created the shared kirocrew.json can deregister its own app entries, and creates no file when it cannot mark itself as the owner;
  • a deregistration's ownership check and its launch-copy removal both run while agents_spec_lock is held (fails with the lock removed);
  • a default-home gateway run from a linked worktree revokes a denied app at boot: its agent spec, launch copy, .template record and MCP entry are gone (fails with the removal routed back through the write guard);
  • positive control: the same setup still refuses an app MCP registration write.

Proof: PROVEN test/test_app_bridges_home_guard.py::test_a_foreign_owned_shared_spec_is_left_byte_identical_by_app_mcp_registration, test/test_app_bridges_home_guard.py::test_a_foreign_owned_shared_spec_keeps_its_app_entries_through_deregistration, test/test_app_bridges_home_guard.py::test_a_relocated_home_creates_no_shared_spec_it_could_not_mark_as_its_own, test/test_app_bridges_home_guard.py::test_a_foreign_owned_shared_dir_keeps_app_agent_files_through_deregistration, test/test_app_bridges_home_guard.py::test_a_foreign_owned_shared_dir_keeps_app_agent_files_through_the_upgrade_prune (head 0d4528a)

The linked-worktree test (test_a_default_home_gateway_in_a_linked_worktree_revokes_a_denied_app) is PROVEN against the first commit (prove.py --base 3b815bb81); against main it passes, because main's removals are unguarded.

test_install_while_execution_denied_registers_nothing now allows the lock's sidecar .kirocrew-agents.lock, which every spec writer of the directory leaves.

Related suites (22 files: app bridges, agent launch, agent home isolation, app backend): 1269 passed, 8 skipped. One test failed in the first run and passed in the second (not in this diff's paths). Semgrep (semgrep/, 1.78.0) is clean on every changed Python file.

Manual verification

N/A -- the tests reproduce each write and removal path on a foreign-owned directory. A full preview-gateway boot was not replayed.

Related Issues

no linked issue: found while reproducing a preview gateway that rewrote the live agent spec; no GitHub issue tracks it.

Sibling sweep

Swept: git grep -nE ".unlink()|atomic_write(|_atomic_json_write(" -- src/kiro_crew/apps/
Sibling: src/kiro_crew/apps/bridges_mcp_config.py -- changed here
Sibling: src/kiro_crew/apps/bridges_agents.py -- changed here
Sibling: src/kiro_crew/apps/bridges_register.py -- changed here
Sibling: src/kiro_crew/apps/agent_launch.py -- tracked in #30989

remove_app_agent_launches in that file is reached only from the two removals that now ask first; publish_app_agent_launch writes through the guarded _atomic_json_write (agent_launch.py), pinned by #36847, so it already asks the write guard.

Sibling: src/kiro_crew/apps/backend_runtime/startup.py -- changed here
Sibling: src/kiro_crew/apps/app_storage.py -- not affected -- writes the app's own data dir, not the agents dir

Pattern harvest

Rule candidate: review-prompt
Pattern: a new writer of a file under kiro_agents_dir() that does not ask _declined_foreign_spec_write, or a remover that does not ask _declined_foreign_spec_removal

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

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

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 25, 2026
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks: the app MCP writer and the two app removals now ask the same shared-home guard the other spec writers already use, so this fixes the root cause.

Notes

  • Accepted change: a default-home gateway run from a linked worktree or temp checkout now silently skips registering app MCP servers (_register_mcp_servers returns []). Make sure the existing worktree-declined hint (_worktree_declined_spec_hint) shows up somewhere visible for this path, or developers will see missing app tools with no explanation.
  • Take-away check: only the backwards-compatibility section's opening lines were read. Its Compatible: line names no <path>:<symbol> reader, but no removed key, symbol or validator was found with a reader outside the patch. The description is truncated within the Sibling sweep section.

[DESIGN-REVIEWED] 0d4528a

Description read

[DESCRIPTION-READ] 0710b9b3f2ac52a3989f813c1157a176accdef6de68e3b8e7a069d96ed760cd1

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 0d4528a4e2b05259c2f0e8c3f93613ee61c056fa — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

The fix is aimed at the cause: the app MCP writer and the two app removals were the shared-directory writers that skipped the ownership guard. Every other item exists to keep that fix working.

What this change ships

Inventory (8 items) — 8 justified

Intent: stop a gateway on a scratch or preview KIROCREW_HOME from rewriting or deleting the live home's shared ~/.kiro/agents specs. This is a FIX.

  1. App MCP registration leaves a kirocrew.json owned by another home untouched and registers nothing — justified
  2. App MCP deregistration and the upgrade prune leave another home's entries in place and report 0 removed — justified
  3. App agent deregistration and the upgrade prune delete no specs or launch copies in a directory another home owns — justified
  4. A relocated home's first kirocrew.json write adds its kirocrew-core pin, and writes nothing if the pin can't be resolved — justified
  5. A default-home gateway run from a linked worktree or temp checkout no longer registers app MCP entries — justified
  6. Removals ignore the worktree and temp-checkout refusals, so a default home in a worktree still revokes denied apps — justified
  7. Boot revokes a denied app's agents, skills and MCP servers separately, so one failure doesn't skip the other two — justified
  8. Agent removal sweeps hold agents_spec_lock, leave its .kirocrew-agents.lock sidecar in the agents directory, and log a SEL allowed or denied event per sweep — justified

[FIRST-PRINCIPLES-REVIEWED] 0d4528a

Description read

[DESCRIPTION-READ] 5759962485afab6d865f58538f414d8ac2643a14bb3f34b7f53c21436ab8a804

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

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 0d4528a4e2b05259c2f0e8c3f93613ee61c056fa — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 0d4528a

Verdict parsed from the review's SHA-scoped output markers for commit 0d4528a4e2b05259c2f0e8c3f93613ee61c056fa.

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

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 0d4528a4e2b05259c2f0e8c3f93613ee61c056fa and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 0d4528a

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

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 25, 2026
@smeyffret
smeyffret force-pushed the fix/agents-spec-home-guard branch from 3a7ffb5 to 1f89dd8 Compare October 29, 2026 22:09
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 29, 2026
@smeyffret
smeyffret force-pushed the fix/agents-spec-home-guard branch from 1f89dd8 to 2a7b3f7 Compare October 29, 2026 23:03
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 29, 2026
@smeyffret
smeyffret force-pushed the fix/agents-spec-home-guard branch from 2a7b3f7 to 06c5d36 Compare October 29, 2026 23:52
@smeyffret

smeyffret commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: shared-home guard on the app MCP writer refused the app-only kirocrew.json its own home created

  • span=1574c570db4d First app registration locks a relocated home out of its own registry

fixed in 06c5d36: a relocated home that creates kirocrew.json in the shared directory now writes the managed kirocrew-core entry with it.
The KIROCREW_HOME pin in that entry is the provenance _existing_specs_are_mine reads, so the same home can later deregister and prune its app entries.
When the entry cannot be resolved, _write_mcp_json_unlocked creates no file and returns False, so no ownerless file is written.
A file another home owns is still refused byte-identical; default home, private redirect and isolated_agents_dir paths are unchanged.
Tests: test_a_relocated_home_can_remove_the_app_entries_it_created, test_a_relocated_home_creates_no_shared_spec_it_could_not_mark_as_its_own.

@smeyffret
smeyffret force-pushed the fix/agents-spec-home-guard branch from 06c5d36 to f2a8e1a Compare October 30, 2026 01:16
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 30, 2026
@smeyffret
smeyffret force-pushed the fix/agents-spec-home-guard branch 2 times, most recently from 36f7919 to eca4b90 Compare October 31, 2026 02:53
@smeyffret

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in docs/system-specs/modules/app-kit-platform.md, docs/system-specs/modules/config.md since this head went green (0835536 -> cccc6f7); CI re-running.

@smeyffret
smeyffret marked this pull request as ready for review October 31, 2026 03:31
@smeyffret
smeyffret requested review from a team and KiroCrewCaptain as code owners October 31, 2026 03:31
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 31, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Request changes at eca4b90: a deferred follow-up needs a tracking issue.

The PR's sibling sweep leaves publish_app_agent_launch as an unguarded writer into the shared ~/.kiro/agents directory, "left for a follow-up". The Design Review asks for an issue as well. I found no GitHub issue for it. Please either guard it here, or open one issue (guard the launch-copy writer with the shared-home check, plus a test) and link it in the description.

The rest reads as sound. The MCP writer asks the existing guard and writes nothing when refused (bridges_mcp_config.py#L154-L193). Both app removals check ownership and unlink under one agents_spec_lock section (bridges_agents.py#L1083-L1101), and remove_app_agent_launches takes no nested spec lock.

AI findings judged non-blocking:

  • Design Review: when the agent prune is refused, the MCP prune is skipped and the caller logs a generic WARNING. The same guard would refuse that write anyway, and the exception message names the foreign owner.
  • First Principles: the refresh_app_agents catch is not described in the PR. It only absorbs the new exception.

This PR also changes a deletion path and which data home is pinned as owner of the shared spec. Those changes still need a maintainer's review after the issue is linked.

CI is green on this head.

— crew-pr-reviewer (r138)

@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 1, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Hold at 366cd61 for a maintainer review. No defect found.

The r138 change request is resolved. The unguarded publish_app_agent_launch writer is now tracked in #30989. That issue is open, labelled deferred-finding, assigned and due 2026-11-20, and the description links it on the sibling line.

Why this still needs a person: the PR changes deletion paths in the shared ~/.kiro/agents directory (bridges_agents.py#L1175-L1215, bridges_register.py#L153-L171). It also changes which data home is pinned as owner of kirocrew.json (bridges_mcp_config.py#L154-L193, shared_home.py#L302-L317). Those areas are reserved for human review.

What I checked: each refused path writes or deletes nothing. The ownership check and the unlinks share one agents_spec_lock section. Every caller handles the new bool from the MCP writer. The denied-app boot branch now revokes each resource kind on its own, so a lock failure skips none of the others.

AI findings judged non-blocking:

  • Design Review: a preview home that boots first on a fresh machine becomes the owner. A default-home gateway is exempt from the guard and still writes, and the pin is described in the spec.
  • First Principles: _new_shared_spec_needs_owner_pin copies the guard's exemption arms. That is a drift risk with one consumer, and it gives no wrong answer today.

CI is green on this head (77 success, 18 skipped).

— crew-pr-reviewer (r149)

iamwhatever
iamwhatever previously approved these changes Nov 8, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: passed Eligible automated validation passed for the current revision labels Nov 8, 2026
@smeyffret
smeyffret dismissed stale reviews from iamwhatever and bolichen97 via 1679820 November 14, 2026 12:47
@smeyffret
smeyffret force-pushed the fix/agents-spec-home-guard branch from 366cd61 to 1679820 Compare November 14, 2026 12:47
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: passed Eligible automated validation passed for the current revision and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Nov 14, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

This changes behaviour users already rely on, so it needs a design sign-off from a maintainer before it can merge. A maintainer will post the decision here.

To propose a design, follow the request-for-change process in CONTRIBUTING.md (docs/request-for-change/).

Review result on 1679820: no blocking finding. PR Readiness is green on this head. The review checked these points:

  • Every production caller of _write_mcp_json_unlocked handles the new bool. A refusal writes nothing and logs no success.
  • The ownership check and every unlink in _deregister_agents and the stale prune share one agents_spec_lock section. No lock is taken twice.
  • A lock failure raises and is caught by every caller. The denied-app boot branch now revokes agents, skills and MCP servers in separate try blocks.
  • Each negative test in test_app_bridges_home_guard.py has a positive control on the same setup, so a surviving file cannot pass by accident.

Done well: the fix reuses the existing _declined_foreign_spec_write decision at the one write seam that register, deregister and prune share. It adds no new ownership rule.

Optional:

  • bridges_agents.py:1288: the removal guard reuses the write guard's ephemeral arm. On the default home, kirocrew app disable run from a linked worktree or a temp checkout now leaves that home's own app agent files in place and still reports success. This is a dev-only setup. Either say so in the body and in app-kit-platform.md, or skip the guard on removals when no override home is set. Then add a test that pins the chosen behaviour.
  • bridges_agents.py:1312: the .template record unlink that moved inside the owned block has no test. Writing a test-app--dropped.template file in _app_files would pin it in both the foreign-dir and default-home tests.
  • The sibling sweep says publish_app_agent_launch writes without asking the guard. It already writes through the guarded _atomic_json_write (agent_launch.py:402), and test: pin the launch copy refusal in a foreign data home #36847 pins that. A body edit is enough. The Proof line also still names the earlier head 366cd61.

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

KiroCrewCaptain commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

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

The app MCP writer (_write_mcp_json_unlocked) and the two app agent removals (_deregister_agents and the upgrade prune) now ask the existing shared-home ownership guard. Removals ask a narrower ownership-only check under agents_spec_lock. Verified: no new lock-order or deadlock risk, since remove_app_agent_launches takes only an in-process lock. Every caller of the now-raising _deregister_agents catches the error, and boot revokes each resource kind independently. A refused registration returns [], so the agent refresh is skipped and the health watch does not retry. The kirocrew-core pin added when a relocated home creates the file is what _existing_specs_are_mine reads back. The SEL audit gap an earlier review lane raised is fixed on this head. 16 targeted tests cover the paths. Nothing blocks.

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

@smeyffret
smeyffret force-pushed the fix/agents-spec-home-guard branch from 1679820 to 3b815bb Compare November 14, 2026 17:45
@smeyffret

Copy link
Copy Markdown
Contributor Author

Rebased: onto main to clear a conflict in src/kiro_crew/apps/bridges_mcp.py and src/kiro_crew/apps/bridges_register.py (1679820 -> 3b815bb, base 4163203). Main added _sync_app_tool_refs before each MCP write; the resolution keeps that call and still checks the guarded write result. CI re-running.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Nov 14, 2026
smeyffret and others added 2 commits November 14, 2026 23:28
A gateway on a non-default KIROCREW_HOME rewrote one entry of the shared
~/.kiro/agents/kirocrew.json: mcpServers["auto-improvement:auto-improvement"]
.command became that gateway's sys.executable. Every new live session then
started the auto-improvement MCP server from a preview build tree, and
deleting that tree breaks the server for all of them.

The chain: reconcile_enabled_app_resources runs register_app for every
enabled app at boot; _register_mcp_servers resolves the app's stdio
command, and a server that launches a kiro_crew module gets
command = sys.executable (resolve_stdio_command); the entry is written by
_write_mcp_json_unlocked straight through atomic_write. That primitive is
the one writer of kirocrew.json the shared-home guard never saw: the
managed-spec rebuild and the derived specs ask
_decline_shared_agent_home / _declined_foreign_spec_write, and
_register_agents asks it per app agent, but the app MCP registry did not.

The fix is at that primitive. _write_mcp_json_unlocked asks
_declined_foreign_spec_write for kirocrew.json under the _mcp_lock it
already holds and returns False, writing nothing, when the shared
directory belongs to another home. _register_mcp_servers then registers
nothing, _deregister_mcp_servers reports nothing removed, and the prune
logs no prune. No new policy: the exemptions are the guard's own, so a
default-home instance, a self-pinned instance, a private redirected
target and isolated_agents_dir write exactly as before. Returning False
rather than raising matches the sibling app writer, _register_agents,
which skips a refused spec; the guard emits the WARNING and the SEL
agent_home_write denial.

A relocated home that creates kirocrew.json in the shared directory
writes the managed kirocrew-core entry with it. That entry's
KIROCREW_HOME pin is the provenance the guard reads, so the same home
can later deregister and prune the app entries it wrote. Without it the
new file has no owner and every later write from that home is refused.
When the entry cannot be resolved, the writer creates no file.

The same home also deleted the owner's files: _deregister_agents (run at
boot for every app this instance denies) and the agent half of
_prune_stale_app_resources unlink app specs and launch copies in the
shared directory without asking. Both now ask the guard once per sweep,
through _owned_agents_dir, and remove nothing when another home owns
the directory. The answer and every unlink of the sweep run under
agents_spec_lock, the lock every spec writer of the directory holds,
so ownership cannot change between the check and a delete. The lock's
sidecar, .kirocrew-agents.lock, already sits in any directory a spec
writer touched; the denied-install test now allows it. A refused
removal is a settled no-op, not an error: registration already declines
every write there, so nothing in it is this home's to remove, and an
error would make `kirocrew app disable` fail and the health watch retry
on every sweep. A lock that cannot be opened or acquired is
a transient failure, so it raises: deregister_app records it, app
disable reports incomplete cleanup and the health watch retries. The
upgrade prune logs it and still runs its MCP half, and the boot
reconcile's denied-app branch revokes agents, skills and MCP servers
in separate steps, so a failed agent sweep skips neither of the others.

Cost of doing nothing: every preview or scratch-home gateway poisons the
live spec on boot, and the agent cannot repair it because kirocrew.json
is write-protected from tool calls, so the owner edits the file by hand.
The app removals asked the write guard, _declined_foreign_spec_write,
whose ephemeral arms refuse any instance run from a linked worktree or
a temp checkout. So a default-home gateway run from one removed nothing
on disable, trust withdrawal or the boot revocation of a denied app,
and still reported success: the denied app's MCP server stayed in
kirocrew.json.

A removal of this home's own app entries pins nothing of the running
checkout into the shared specs, so those arms do not apply. Removals
now ask _declined_foreign_spec_removal, which keeps only the ownership
arm: specs another data home owns are still left untouched.
_owned_agents_dir asks it for the agent sweeps, and the MCP entry
removals pass removal=True to _write_mcp_json_unlocked. Writes are
unchanged.
@smeyffret

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: removal guard returned early on an admitted shared-dir removal without the allowed audit

  • span=a4929814535e Audit admitted shared-home removals

fixed in 0d4528a: _declined_foreign_spec_removal now calls _audit_allowed_shared_write(path) when a removal from the shared agents dir is admitted, matching the write half.
A private target (_shared_agents_target() is None) still returns silently, the same exemption the write half and _audit_allowed_shared_write keep.
One guard serves every listed site: _owned_agents_dir (deregister, stale-agent prune) and _write_mcp_json_unlocked(removal=True) (MCP deregister, stale-MCP prune).
The foreign-owner refusal and its denied record are unchanged.
Test: test_an_admitted_removal_from_the_shared_dir_is_audited_as_allowed (red without the fix: no allowed events; green with it).

@smeyffret

Copy link
Copy Markdown
Contributor Author

The red Backend Tests shards on 0d4528a come from main, not this diff. Run 43653586092: Linux (3.12) shards 1, 2 and 8, and Windows shards 1, 2 and 8. Coverage Gate fails closed on backend-test=failure.

Failing tests, all checking that persisted conversation rows are scrubbed of credentials:

  • test/test_autonudge_row_scrub.py: 5 tests, including test_a_user_row_is_scrubbed_at_rest and test_the_persisted_user_row_carries_no_credential[webex]
  • test/test_one_conversation_one_session.py::TestOneRedactionRule::test_a_user_row_arriving_through_the_shared_helper_is_scrubbed
  • test/test_history.py::TestAMiscomposedHostPersistsAWholeTurn::test_both_rows_persist
  • test/test_telegram_sessions_resume.py::TestTelegramColdResume::test_the_live_projection_receives_scrubbed_text_not_raw

They fail the same way on Linux and Windows, and on #37411, #37410, #37408 and #37359, so they are not a flake. This PR changes none of the redaction or history code. No open issue or flake-ledger entry covers them yet; #36640 ("scrub every persisted conversation row") is the recent main change in that area. No push on this PR for it.

This branch has not been deployed

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

Labels

readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants