Skip to content

fix(governance): scope app-bound profiles to their app's agents - #25028

Closed
buluoray wants to merge 1 commit into
mainfrom
crew/mcp-allowedtools-profile-scope
Closed

buluoray wants to merge 1 commit into
mainfrom
crew/mcp-allowedtools-profile-scope

Conversation

@buluoray

@buluoray buluoray commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: a governance profile bound to one app no longer removes @server auto-approvals from agents that app can never run.

After an update or restart, every agent lost the @server entries in its allowedTools (and its servers' autoApprove), so each MCP call started prompting again. The host had one app-bound profile that confines network.egress and switches capabilities.spawn off.

Why it matters

Every crew-bound agent on such a host is affected, on every start, and the user's own hand-written grants are rewritten away. The prompts it adds come from a profile that, at the PreToolUse gate, can never govern those agents' calls.

Not a goal

  • Changing what any profile enforces at the PreToolUse gate.
  • Changing the builtin floor that withholds auto-approval from fs_read, fs_write, execute_bash, code, grep and glob.
  • Scoping surface or task binds, or the writers that name no agent (build_agent_config and the install paths).

What changed (motivation → approach → change)

Symptom → cause. On every start the fork refresh re-filters each crew-bound shared template (src/kiro_crew/agent_materialization/fork_refresh.py:496, the pass at :888). It asks may_skip_gate_now(ref) with no agent, which withholds a ref if ANY loaded profile governs it (src/kiro_crew/platform/governance.py:850 → src/kiro_crew/platform/governance_profiles.py:1037). A profile with a network.egress rule governs every @server, so one app-bound profile stripped every agent. The autoApprove strip (governance.py:505) and the KAS read-time re-filter (src/kiro_crew/acp/kas_agents.py:448) asked the same agent-less question.

Approach. The gate resolves an app bind only for a call carrying that app's context. Such a call comes from the app's own agents, a sub-agent the app spawned, a chat session the app opened on an agent of its choosing (POST /api/chat/slots takes the agent from the request body and stamps the app on the slot, dashboard/chat_api/slot_create.py:155; the agent switch checks only slot ownership), or an in-process runner that drives other agents under its app id. So an app-bound profile is left out for an agent only when spawn is off, the app is not such a runner, the app cannot open chat sessions, and the app did not register the agent. Every lookup fails closed.

Change.

Profile Withholds from an unrelated agent?
app bind, spawn off, app cannot open chat sessions, agent not the app's No (this PR)
app bind whose manifest declares a backend, has a permissions.api entry reaching /api/chat (incl. /api/*, *), or does not read Yes
app bind, agent registered by the app, <app>-- name, or ownership unreadable Yes
app bind with spawn left on Yes
app bind of an in-process runner (IN_PROCESS_AGENT_APP_CONTEXTS) Yes
surface / task bind, fallback profile, no agent named Yes
  • governance_profiles.py: any_configured_profile_governs(ref, *, agent="") skips a profile _profile_may_bind_agent rules out. _app_may_host_sessions reads the app's manifest (no backend, no /api/chat grant) and returns True on any doubt. Ownership reads apps.manager_lookup._app_agent_registration; anything but "missing" counts as owned.
  • governance.py: may_skip_gate_now and strip_ungoverned_auto_approve take agent= and pass it through; with none, every profile counts as before.
  • agent_materialization/auto_approve.py, fork_refresh.py: the shared-template pass names the bound agent for both allowedTools and autoApprove.
  • acp/kas_agents.py: the KAS projection asks for the agent it projects.
  • docs/system-specs/modules/governance.md: the rule.

Backwards compatibility

Compatible: every writer that names no agent, and every profile other than a spawn-off app bind, gives the same answer as before.

Removes nothing: the change only keeps grants a profile could never govern; the profile's own app agents, the gate's per-call decision and the builtin floor are unchanged.

Tests

  • test/test_governance_profile_agent_scope.py (new): an unrelated agent keeps @builder-mcp in allowedTools, in autoApprove and in the KAS projection when the app (like the internal tunnel app: one /api/edition/tunnel grant, no backend) cannot open sessions; an app that can open chat sessions strips it from any agent in all three (test_an_app_that_may_open_chat_sessions_governs_every_agent over /api/chat, /api/chat/slots, /api/chat/slots/*, /api/*, /api/c*, *, the agent-switch route; test_the_kas_projection_governs_agents_a_chat_capable_app_can_run), as does an app with a backend, an unreadable manifest or a manifest lookup that raises; the app's own agent (registered, ambiguous, unverifiable, <app>-- name, a lookup that raises) is still stripped; spawn-on and in-process-runner app binds, surface and task binds and an unnamed writer still strip; the six floor builtins stay withheld with or without a profile; every literal app= gate argument in the package is listed as an in-process runner.
  • Existing stubs and pinned signatures updated for the keyword in test_agent_refactor_contracts.py, test_agent_refactor_fork_refresh.py, test_conductor_allowed_tools_survive.py, test_agent_fork_templates.py, test_kas_agents.py, test_kas_permissions.py, test_kas_tools_settings.py, test_mate_worker.py, test_mcp_confirm_tools.py.
  • The 11 session-capability tests fail with the _app_may_host_sessions check removed and pass with it. Governance, KAS, fork refresh, agent refactor, conductor and mate-worker suites: 2616 passed; test_mcp_confirm_tools.py: 140 passed. flake8, isort, black, mypy, comment-history, brand, line-cap, docs-lint: clean.

Proof: PROVEN test/test_agent_refactor_contracts.py::test_every_pre_split_name_resolves_with_its_pre_split_shape, test/test_governance_profile_agent_scope.py::test_the_kas_projection_scopes_by_agent[missing-True] (head c686ee9)

Manual verification

N/A — unit coverage sufficient: the decision is a predicate over loaded profiles, driven through the real shared-template pass and KAS projection.

Not verified: an end-to-end start of the internal edition with its app profile.

Related Issues

No issue found. Searched open and closed issues and PRs for "allowedTools MCP removed restart", "profile governs every server", "any_configured_profile_governs". Regression from #18811. Reported by a user of the internal edition in Slack.

Sibling sweep

Swept: git grep -nE "may_skip_gate_now(|any_configured_profile_governs(" -- src/
Sibling: src/kiro_crew/agent_materialization/fork_refresh.py:888 -- changed here
Sibling: src/kiro_crew/platform/governance.py:505 -- changed here (agent= is threaded; the shared-template pass passes it)
Sibling: src/kiro_crew/acp/kas_agents.py:448 -- changed here
Sibling: src/kiro_crew/agent_parts/spec_build.py:612 -- not affected -- build_agent_config serves every installer and names no single agent, so every profile still counts
Sibling: src/kiro_crew/dashboard/chat_api/slot_create.py:155 -- not changed -- an app picks any agent for its slot; this PR counts such apps instead of narrowing the route
Sibling: src/kiro_crew/apps/bridges_agents.py:190 -- not affected -- an app's own agents, which its own profile must keep governing

Pattern harvest

Rule candidate: review-prompt
Pattern: a static grant filter that asks "does any profile govern this" without the subject the grant belongs to over-applies scoped profiles globally.

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

@buluoray
buluoray requested review from a team and KiroCrewCaptain as code owners October 27, 2026 21:54
@buluoray
buluoray requested a review from cruisercohen October 27, 2026 21:54
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

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

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks. Scoping the static grant filter by agent fixes the root cause, every lookup fails closed, and the PreToolUse gate is unchanged.

Notes

  • _profile_may_bind_agent hard-codes the ways an app's context can reach an agent it never registered: spawn, /api/chat, a backend, and in-process runners. The only pin is the test on literal app= gate arguments. A future route or bridge that runs an arbitrary agent under an app's context would quietly let that agent keep auto-approvals the profile should withhold. A spec line, or a test pinned to the route that stamps app context, would make that boundary visible.

[DESIGN-REVIEWED] c64fddb

Description read

[DESCRIPTION-READ] 5e611d252d9ee8c532f14941e426f35812bd200d7d7aa3cdeb90ab46cc968278

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 27, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — 🔴 changes requested (blocking)

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

Blocks: grants on shared agent templates are now kept or stripped based on app manifest state, but updating an app re-derives nothing. So once an app gains session hosting, its profile's denials are bypassed through stale static grants.

BLOCKING — src/kiro_crew/platform/governance_profiles.py:1135
if _app_may_host_sessions(bind.id): (it reads get_app_manifest(app) at line 1103), used by _govern_json_spec(..., agent=name) in the shared-template pass
Trigger: An operator saves a profile bound to app A that turns spawn off and denies @srv. A has no backend and no /api/chat grant. The shared-template pass (fork_refresh._apply_governance_passes(..., agent=name)) scopes that profile out and keeps @srv in shared spec X's allowedTools/autoApprove. The operator then updates A to a version that declares /api/chat or a backend (routes_install.handle_update_app → update_app → _register_app_off_loop). That path never calls rebuild_agent_config, and the manifest is not part of governance_answer_generation().
Outcome: A now opens chat slots on X through slot_create (app=request.get("app")), and the gate resolves A's profile for them. But on the default Kiro backend, kiro-cli auto-approves @srv from X's on-disk allowedTools before the PreToolUse gate runs. The deny the app-bound profile sets is bypassed until some unrelated rebuild runs. Before this PR, the grant was stripped as soon as the profile was saved.
Anchor: residual/security
Fix: Re-run the shared-template governance pass (rebuild_agent_config) after app install, update or registration. Alternatively, fold a fingerprint of the app manifests and registrations into the generation the pass memoizes on.

[BLOCK-MERGE] c64fddb
[OPUS-REVIEWED] c64fddb

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

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

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — 🔴 changes requested (blocking)

GPT 6.1 found at least one blocking issue that must be resolved before merging c64fddb6e910be2e3358b70a0e05adef489061bb. 1 of 1 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

This comment is updated in place on each push.

BLOCKING -- src/kiro_crew/platform/governance_profiles.py:1171 -- App capability changes leave previously scoped grants active (origin: validation)

and _profile_may_bind_agent(profile, agent)
Trigger: At boot, a frontend-only app with spawn disabled and no chat grant is excluded from an unrelated shared template’s grant filter. handle_update_app later adds /api/chat, and handle_enable_app approves it. Neither path re-filters that template; reproject_for_profile_change also skips rebuilding because governance_answer_generation() tracks only ceiling/profile changes. The app then opens /api/chat/slots using that template.
Outcome: On Kiro, the retained allowedTools/autoApprove grants bypass PreToolUse, allowing calls the app-bound profile forbids—for example, web_fetch despite an empty egress allowlist—until a separate rebuild occurs.
Anchor: residual/security
Fix: Invalidate and complete the affected grant projection when an app gains session capability, before activating its widened API grants.

[BLOCK-MERGE] c64fddb
[GPT-REVIEWED] c64fddb

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

The adjudicable block is empty, so there are no regular verdicts. The one fenced finding, F1, stays blocking. I'm not flagging it for override, because the condition chain it needs is ordinary use, not something extreme.

F1, unbounded harm (bypasses the governance ceiling):

  • Conditions confirmed:

    • governance_profiles.py:1171 filters grants with _profile_may_bind_agent.
    • For an app bind whose spawn is off, the check at :1134-1137 turns on _app_may_host_sessions, which reads the app's current manifest.
    • Neither handle_update_app (apps/routes_install.py:54) nor handle_enable_app (apps/routes_enable.py:227) triggers a rebuild or a re-projection.
    • reproject_for_profile_change (agent_parts/spec_rebuild.py:311) returns early when the answer generation hasn't changed, so the grants written at boot stay in place.
  • Recovery: nothing automatic. The stale grants last until some unrelated rebuild runs.

  • Why it's plausible: every step is routine. An operator writes an app-bound profile with spawn off, then updates the app's manifest and approves it.

    [ADJUDICATION] c64fddb total=0 uphold=0 downgrade=0
    [GPT-ADJUDICATED] c64fddb

    [ADJUDICATION-FENCED] c64fddb fenced=1 flagged=0
    UPHOLD-FENCED F1 src/kiro_crew/platform/governance_profiles.py:1171 -- An ordinary manifest update and approval widens an app's session capability, and neither the update/enable routes nor the generation-gated profile watch re-filters grants written at boot, so the app-bound profile's ceiling is silently bypassed until an unrelated rebuild runs.
    [GPT-ADJUDICATED-FENCED] c64fddb

False positive or not applicable? A repository writer can comment:
/ai-review override gpt c64fddb6e910be2e3358b70a0e05adef489061bb 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 27, 2026
@buluoray
buluoray force-pushed the crew/mcp-allowedtools-profile-scope branch from d23f6c1 to c686ee9 Compare October 27, 2026 22:45
@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 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

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

This fix works on the cause: the agent-less "does any profile govern this ref" question. The only extra is a redundant branch at the call sites.

Notes

  • Item 5 — oversized: may_skip_gate_now(ref, agent=agent) if agent else may_skip_gate_now(ref). The default is already "", so both arms are the same call. The pattern appears 4 times: auto_approve._strip_ungoverned_auto_approve, _may_auto_approve, _apply_allowed_tools_ceiling, and governance.strip_ungoverned_auto_approve. The PR already updates the stubs, so no test needs the bare form. Subtraction: delete the conditional and always pass agent=agent.

What this change ships

Inventory (6 items) — 5 justified

Intent: stop a governance profile bound to one app from taking away every agent's @server auto-approvals on each start. This is a FIX, a regression from #18811.

  1. Agents the bound app can never run keep their @server entries in allowedTools across refreshes — justified
  2. Those agents' MCP servers keep autoApprove across refreshes — justified
  3. The KAS projection filters grants for the agent it projects — justified
  4. New IN_PROCESS_AGENT_APP_CONTEXTS set (auto-improvement, code-review-sage), pinned by a test — justified
  5. Optional agent= keyword on may_skip_gate_now, strip_ungoverned_auto_approve and any_configured_profile_governs, with conditional call sites — oversized
  6. The governance spec states the scoping rule — justified

[FIRST-PRINCIPLES-REVIEWED] c64fddb

Description read

[DESCRIPTION-READ] 8bf9a294e1902e4604de9bc9d45bd125bcbce901e1a7c6db769b6be45d627090

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.

@buluoray

Copy link
Copy Markdown
Contributor Author
  • App chat slots run unregistered agents under the app's context span=cc3101dbea31

fixed in c686ee9: the chain holds, and an app that can open sessions now counts its profile for every agent.
Verified: POST /api/chat/slots takes agent from the request body (dashboard/chat_api/slot_create.py:155) and stamps the app on the slot; nothing after it limits an app to its registered agents, and the agent switch checks only slot ownership (deny_app_slot_access).
Change: _profile_may_bind_agent now calls _app_may_host_sessions(app), which returns True (profile counts, grant stripped) when the manifest does not read, declares a backend, or has any permissions.api entry reaching /api/chat (/api/chat, /api/chat/slots/*, /api/*, /api/c*, *), or the lookup raises. Only an app with none of those, spawn off, and no registration of the agent leaves the grant.
Tests: test_an_app_that_may_open_chat_sessions_governs_every_agent (7 grant shapes; allowedTools and autoApprove), test_the_kas_projection_governs_agents_a_chat_capable_app_can_run, test_an_app_with_a_backend_governs_every_agent, test_an_app_with_no_readable_manifest_governs_every_agent, test_a_session_capability_lookup_that_raises_strips. All 11 cases fail with the check removed.

@buluoray

Copy link
Copy Markdown
Contributor Author
  • Spawn-off apps can still run unrelated templates through the chat API span=a9e87b5085be

fixed in c686ee9: spawn-off plus a missing registration no longer leaves the profile out when the app can open chat sessions.
Verified the chain: api_chat_slot_create takes the agent from the body (dashboard/chat_api/slot_create.py:155), the slot keeps _app, and no route confines an app caller to its own agents; the agent switch checks only slot ownership.
Change: _app_may_host_sessions(app) counts the profile when the manifest does not read, declares a backend, or grants any permissions.api entry reaching /api/chat (including /api/* and *), and on any lookup error. The KAS projection asks the same predicate.
Tests: test_an_app_that_may_open_chat_sessions_governs_every_agent, test_the_kas_projection_governs_agents_a_chat_capable_app_can_run, test_an_app_with_a_backend_governs_every_agent, test_an_app_with_no_readable_manifest_governs_every_agent, test_a_session_capability_lookup_that_raises_strips (fail without the check, pass with it).

… run

The shared-template pass re-filters every crew-bound spec's allowedTools
on each start. It asked may_skip_gate_now with no agent, so any profile
with an opinion on a ref withheld it from every spec. An app-bound
profile with a network.egress rule therefore stripped every @server
grant from every agent, though only calls carrying that app's context
can resolve to it.

The pass, the servers' autoApprove strip and the KAS projection now
name the spec. An app-bound profile is left out for it only when all of
these hold: the profile switches spawn off, the app is not an in-process
runner that drives other agents (auto-improvement), the app cannot open
chat sessions on an agent of its choosing (no backend, no permissions.api
entry reaching /api/chat, readable manifest), and the app did not
register the spec. Any lookup that fails counts the profile. Surface and
task binds, fallback profiles, every writer that names no agent, and the
builtin floor that withholds fs_read, grep, glob, code and the shell are
unchanged.
@buluoray
buluoray force-pushed the crew/mcp-allowedtools-profile-scope branch from c686ee9 to c64fddb Compare October 27, 2026 23:05
@buluoray

Copy link
Copy Markdown
Contributor Author
  • code-review-sage drives unregistered agents under its app context but is not an in-process runner span=cc3101dbea31

fixed in c64fddb: code-review-sage is added to IN_PROCESS_AGENT_APP_CONTEXTS, so a profile bound to it governs every agent again.
Verified: ReviewPool.send calls asyncio.to_thread(refusal_for, ..., agent=self._agent, app="code-review-sage") (code_review_sage/sage_lib/review_pool.py:717) for agents the app does not register; its manifest has no backend and only /api/apps/code-review-sage grants, so nothing else counted it.
Pin widened: test_every_literal_app_context_at_the_gate_is_an_in_process_runner now also matches gate functions passed to to_thread and bare-name calls, and asserts it finds code-review-sage. A whole-package AST scan with that matcher finds exactly two literal app= gate contexts: auto-improvement (agent_runner_gate.py:393) and code-review-sage (review_pool.py:717), both listed.
Tests: test_the_code_review_runner_app_governs_every_agent and the widened pin fail without the fix and pass with it; test_governance_profile_agent_scope.py 33 passed.

@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 27, 2026
@buluoray

Copy link
Copy Markdown
Contributor Author

Closing this PR: the symptom is fixed in the internal edition instead.

The stripped grants came from one bundled app profile whose network.egress rule the static allowedTools writers read as an opinion on every @server. That profile is never selected for its app at runtime, so removing the rule there fixes the symptom without making these writers track which agents an app can run. Review showed that tracking needs app state the writers would have to re-derive on every app change.

The branch is left in place for reference.

@buluoray buluoray closed this Oct 28, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 28, 2026
@bolichen97
bolichen97 deleted the crew/mcp-allowedtools-profile-scope branch November 6, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant