Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/kiro_crew/acp/runtime.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""ACP Runtime for multiplexed kiro-cli sessions.

Single-reader demux architecture: one AcpRuntime owns the subprocess and a
Expand Down Expand Up @@ -6835,7 +6835,7 @@

async def _unpooled_control_planes(
self, entries: list[dict[str, Any]], agent: str | None, work_dir: str | Path
) -> list[dict[str, Any]]:
) -> tuple[list[dict[str, Any]], frozenset[tuple[str, str]]]:
# A shared Kiro process has no session-valued environment. Its native
# managed servers need per-element identity even with the broker off.
if self.acp_backend == ACP_BACKEND_KIRO:
Expand Down Expand Up @@ -6878,6 +6878,14 @@
# this agent's skill resources on the element's promise, and
# without the element kirocrew-core mounts natively, carries no
# identity and answers identity_unattested to every skill_search.
#
# A ``disabledTools`` toggle on kirocrew-core no longer reaches
# here as a withheld element: the mount carries it on the gate
# and emits the element, so the element IS in ``mount.elements``
# and this branch is not taken for it. What is still kept native
# -- a mute, a non-stdio transport, a key the element cannot
# express -- has no per-call form, so refusing stays the only
# honest answer for those (#14125).
withheld = mount.withheld.get("kirocrew-core")
if withheld is not None:
raise AcpRuntimeError(f"Agent {name!r}: {withheld.explain('skill search')}")
Expand All @@ -6886,8 +6894,8 @@
"Cannot bind skill_search to this session without losing native MCP "
"restrictions. Check the agent's kirocrew-core server configuration."
)
return [*entries, *mount.elements]
return entries
return [*entries, *mount.elements], mount.carried
return entries, frozenset()

@staticmethod
async def _source_agent(agent: str | None) -> str | None:
Expand Down Expand Up @@ -7021,9 +7029,18 @@
self.acp_backend,
session_work_dir,
)
mcp_servers = await self._unpooled_control_planes(
mcp_servers, carried_denied = await self._unpooled_control_planes(
pooled, agent or self._agent, session_work_dir
)
# On the kiro backend a ``disabledTools`` toggle on the control
# plane is carried on the per-call gate rather than withholding
# the element and refusing the session (#14125). The pairs land
# on this session's ``spec_denied_tools`` the same way a mirrored
# host's do -- the handle refuses a switched-off tool at the
# permission request -- so the session runs minus the toggled
# tool and keeps skill_search. Empty on every other backend and
# whenever nothing is toggled.
denied_tools = carried_denied
mcp_servers, stub_token = await self._own_stub_session(mcp_servers, session_key)
else:
# An explicit array is the caller's own composition (a mirror's
Expand Down Expand Up @@ -7906,9 +7923,13 @@
self.acp_backend,
session_work_dir,
)
mcp_servers = await self._unpooled_control_planes(
mcp_servers, carried_denied = await self._unpooled_control_planes(
pooled, active_agent, session_work_dir
)
# Carry a control-plane ``disabledTools`` toggle on the gate here too,
# so a RESUMED session keeps the same deny set a fresh one gets and
# the toggled tool stays refused across session/load (#14125).
denied_tools = carried_denied
mcp_servers, stub_token = await self._own_stub_session(mcp_servers, session_key)
member_withheld = False
# False for every non-member session, set without an awaited call so the
Expand Down
117 changes: 110 additions & 7 deletions src/kiro_crew/acp/session_mcp.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Kiro agent spec -> the ACP ``session/new`` ``mcpServers`` array.

For a harness in :data:`~kiro_crew.acp_backends.ACP_BACKENDS_SESSION_MCP_ARRAY`,
Expand Down Expand Up @@ -795,6 +795,36 @@
return frozenset(pairs)


def native_carried_disabled_tools(
name: str, spec_entry: Any, settings: Collection[NativeSettingsSource]
) -> frozenset[tuple[str, str]]:
"""The ``(name, tool)`` pairs *name*'s declarations switch off, for the gate.

The companion of :func:`native_mount_withholding` for the one restriction it
now carries rather than withholds (#14125): when a control-plane server's
per-session element mounts with a ``disabledTools`` toggle standing, the
restriction has nowhere native to live, so :func:`kiro_control_plane_servers`
feeds these pairs to the session's per-call gate instead. Read from the SAME
declarations the withholding predicate reads -- the spec entry and each
settings file, through :func:`native_declarations` -- so the mount's two
decisions (mount the element, deny these calls) come from one source set and
cannot disagree. This is the ONE place the mount's reader spells the
``disabledTools`` key; the mount itself keeps no copy of it. Free of I/O: the
sources are already read.
"""
pairs: set[tuple[str, str]] = set()
for _source, declared in native_declarations(name, spec_entry, settings):
if not isinstance(declared, dict):
continue
disabled = declared.get("disabledTools")
if not isinstance(disabled, list):
continue
for tool in disabled:
if isinstance(tool, str) and tool:
pairs.add((name, tool))
return frozenset(pairs)


def session_mcp_disabled_servers(spec: Any, settings: Any) -> frozenset[str]:
"""Every server switched off WHOLE by ``disabled: true``, from both sources.

Expand Down Expand Up @@ -1511,10 +1541,22 @@
files the mount performs, so a caller that refuses a session on ``withheld``
refuses it for the reason the array was built on, never for what the files
say a moment later.

``carried`` names the ``(server, tool)`` pairs the mount DID produce an
element for while a ``disabledTools`` restriction stood on that server, so
the restriction rides the session's per-call permission gate instead of
withholding the element (#14125). Only a control-plane server appears here:
its tools carry no annotations, so every call reaches Crew as a permission
request the client can reject (see :func:`session_mcp_restricted_servers`).
The caller MUST feed these to ``AcpSessionHandle.spec_denied_tools`` -- the
element carries only command/args/env and expresses no restriction on its
own, so without the gate the toggled tool would run. From the same one read
as ``elements`` and ``withheld``.
"""

elements: list[dict[str, Any]]
withheld: dict[str, NativeMountWithholding | NativeSettingsUnreadable]
carried: frozenset[tuple[str, str]] = frozenset()


def _named(value: Any) -> str:
Expand Down Expand Up @@ -1552,13 +1594,17 @@


def _native_restriction(
declared: Any, *, dashboard_writes: bool = False, crews_registry_marker: bool = False
declared: Any,
*,
dashboard_writes: bool = False,
crews_registry_marker: bool = False,
carry_disabled_tools: bool = False,
) -> tuple[str, str] | None:
"""``(restriction, remedy)`` when one declaration keeps its server native.

The order is the order the questions are cheapest to answer and matters only
for which reason a declaration with several is named for. The two flags say
which declaration this is, because two answers depend on it. ``dashboard_writes``:
for which reason a declaration with several is named for. The three flags say
which declaration this is, because three answers depend on it. ``dashboard_writes``:
the declaration lives in the file the dashboard's MCP tab writes -- the global
settings -- so a mute or a ``disabledTools`` there is offered the tab as the
way back; a spec or project entry is not, since following that remedy would
Expand All @@ -1568,6 +1614,20 @@
registry mode is on, the one state in which the spec rebuild stamps it there
-- so it restricts nothing. In every other state the same value is one
kiro-cli drops, and the caller says so by leaving the flag off.
``carry_disabled_tools``: the server this declaration is for has a per-CALL
channel that CAN carry a ``disabledTools`` restriction (a control-plane
server, whose tools carry no annotations, so every call reaches Crew as a
``session/request_permission`` the client answers with the reject option --
see :func:`session_mcp_restricted_servers`). For such a server the
``disabledTools`` arm is NOT a reason to withhold the element: the element
mounts and the restriction rides the per-call gate instead, so the session
runs minus the toggled tool rather than being refused outright (#14125).
This narrows ONLY the ``disabledTools`` arm; a mute, a non-stdio transport
and a key the element cannot express still keep the declaration native --
none of those has a per-call form a gate could carry. The flag is off by
default, so a server without that channel (an opt-in server whose
read-only-annotated tools codex auto-approves without asking) keeps the old
withholding behaviour.
"""
if not isinstance(declared, dict):
return ("its entry is not a server object", "declare it as an object there")
Expand Down Expand Up @@ -1606,7 +1666,7 @@
_remedy("re-enable the server", "set disabled to false there", tab=dashboard_writes),
)
disabled_tools = declared.get("disabledTools", [])
if disabled_tools != []:
if disabled_tools != [] and not carry_disabled_tools:
return (
f"its disabledTools {_named_tools(disabled_tools)}",
_remedy(
Expand All @@ -1632,7 +1692,11 @@


def native_mount_withholding(
name: str, spec_entry: Any, settings: Collection[NativeSettingsSource]
name: str,
spec_entry: Any,
settings: Collection[NativeSettingsSource],
*,
carry_disabled_tools: bool = False,
) -> NativeMountWithholding | None:
"""Whether *name*'s per-session element must be withheld, and why.

Expand Down Expand Up @@ -1666,6 +1730,18 @@
``session/new`` would fail with an error pointing at the wrong file; so both
call this one function, and neither keeps a copy of the merge or of the
predicate.

``carry_disabled_tools`` says the caller has a per-CALL channel that can
honour a ``disabledTools`` restriction on *name* (a control-plane server:
its tools carry no annotations, so every call reaches Crew as a permission
request the client answers with the reject option). For such a server a
``disabledTools`` declaration no longer withholds the element; the element
mounts and the restriction is carried on the gate instead, so the session
runs minus the toggled tool rather than being refused (#14125). Only the
``disabledTools`` arm is narrowed -- a mute, a non-stdio transport and an
uncarriable key still withhold, because none has a per-call form -- and the
caller must then actually feed those ``(server, tool)`` pairs to the gate,
which :func:`kiro_control_plane_servers` does.
"""
dashboard_writes = {s.named for s in settings if s.label == NATIVE_SOURCE_GLOBAL}
registry_mode = _registry_mode()
Expand All @@ -1674,6 +1750,7 @@
declared,
dashboard_writes=source in dashboard_writes,
crews_registry_marker=source == NATIVE_SOURCE_SPEC and registry_mode,
carry_disabled_tools=carry_disabled_tools,
)
if verdict is not None:
restriction, remedy = verdict
Expand Down Expand Up @@ -1740,18 +1817,44 @@
return NativeControlPlaneMount([], {name: exc for name in IDENTITY_BOUND_SERVERS})
out: list[dict[str, Any]] = []
withheld_by_name: dict[str, NativeMountWithholding | NativeSettingsUnreadable] = {}
carried: set[tuple[str, str]] = set()
for name in IDENTITY_BOUND_SERVERS:
if not allow.grants(name):
continue
entry = spec["mcpServers"].get(name)
managed = managed_mcp_spec_entry(name, include_opt_in=True)
if not isinstance(entry, dict) or not isinstance(managed, dict):
continue
withheld = native_mount_withholding(name, entry, settings)
# The control plane has a per-call channel that CAN carry a
# ``disabledTools`` restriction: its tools carry no annotations, so every
# call reaches Crew as a ``session/request_permission`` the client
# answers with the reject option. So for it a ``disabledTools`` toggle no
# longer withholds the element and refuses the session -- the element
# mounts and the pairs ride the gate instead (#14125, carried below). An
# opt-in server has no such channel (a ``readOnlyHint`` tool is
# auto-approved inside codex without asking), so it keeps the old
# withholding behaviour and the flag stays off for it -- the same split
# :func:`session_mcp_restricted_servers` draws, for the same reason.
can_carry = name in CONTROL_PLANE_SERVERS

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.

[P0] kiro backend: a kirocrew-core disabledTools toggle stops being enforced (fail-open) — see the review summary for mechanism, fix and test.

withheld = native_mount_withholding(
name, entry, settings, carry_disabled_tools=can_carry
)
if withheld is not None:
logger.debug("session MCP: %s", withheld.explain("its per-session element"))
withheld_by_name[name] = withheld
continue
# The element (or a pre-existing broker stub for the same name) will be
# on the session, and neither can express ``disabledTools`` -- so a
# restriction we chose to carry rather than withhold has to reach the
# gate. Collect its pairs from the SAME source set the withholding
# predicate read, before the ``existing_names`` continue: a stub carries
# the restriction no better than the element does, so the pairs are owed
# on that path too. The reader lives in
# :func:`native_carried_disabled_tools`, so this keeps no copy of the key
# -- only the control plane reaches it with a non-empty set, the opt-in
# servers withheld above.
if can_carry:
carried |= native_carried_disabled_tools(name, entry, settings)
if name in existing_names:
continue
# The launch is the managed source's, never the spec's. A hand-authored
Expand Down Expand Up @@ -1793,4 +1896,4 @@
element = acp_server_element(name, owned)
if element is not None:
out.append(element)
return NativeControlPlaneMount(out, withheld_by_name)
return NativeControlPlaneMount(out, withheld_by_name, frozenset(carried))
12 changes: 11 additions & 1 deletion src/kiro_crew/acp/skill_projection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Native Kiro launch views whose skill directory is supplied by Crew.

The authored resource mapping remains the authority for Crew search/list/read.
Expand Down Expand Up @@ -2956,7 +2956,17 @@
# withholding the view for it would refuse a supported customization
# and abort the spawn over a restriction that reaches the session.
if per_session_element:
withheld = session_mcp.native_mount_withholding("kirocrew-core", original_core, [])
# kirocrew-core is a control-plane server: its ``disabledTools``
# ride the per-call gate the mount feeds, so they no longer
# withhold the element and must not refuse the view either --
# ``carry_disabled_tools`` makes this reader agree with the mount
# (#14125). ``skill_search`` itself being disabled was already
# refused above; what remains for carry are the OTHER tools the
# toggle named, which leave skill search standing. A mute, a
# non-stdio transport and an uncarriable key still withhold.
withheld = session_mcp.native_mount_withholding(
"kirocrew-core", original_core, [], carry_disabled_tools=True
)
if withheld is not None:
errors[agent.name] = withheld.explain("skill search")
continue
Expand Down
Loading
Loading