Skip to content
Merged
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
1 change: 0 additions & 1 deletion .github/agent-sdk-boundary-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,5 @@
2 src/kiro_crew/task_executor.py
1 src/kiro_crew/task_planner.py
1 src/kiro_crew/taskrunner.py
1 src/kiro_crew/telegram/transport_dispatch.py
1 src/kiro_crew/workflows/agent_pool.py
2 src/kiro_crew/workflows/service.py
19 changes: 9 additions & 10 deletions src/kiro_crew/telegram/transport_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
from typing import TYPE_CHECKING, Any, NamedTuple, cast # noqa: F401

from kiro_crew import runtime_death
from kiro_crew.acp.client import AcpError
from kiro_crew.agent_discovery import AgentInfo, list_agents # noqa: F401
from kiro_crew.agent_sdk.drivers.acp_vocab import is_user_worded_failure
from kiro_crew.config import live
from kiro_crew.config.loader import ACTIVATION_MENTION, ACTIVATION_OFF # noqa: F401
from kiro_crew.config.sections import _clamp_pct
Expand Down Expand Up @@ -293,19 +293,18 @@
def _user_safe_failure_reason(exc: BaseException) -> str | None:
"""A bounded, user-safe reason for a failed turn, or None for the generic text.

A private-memory refusal or *permanent* :class:`AcpError` yields a
reason: its message is already user-facing and actionable (e.g. names the
models the account does include), and the generic "please try again"
placeholder would be actively wrong for it. Transient and unclassified
failures keep the retry wording, and any other exception type returns
None — arbitrary internal errors must never leak into chat (CWE-209).
A private-memory refusal or a backend failure Kiro Crew worded for a person
(``is_user_worded_failure``) yields a reason: its message is already
user-facing and actionable (e.g. names the models the account does
include), and the generic "please try again" placeholder would be actively
wrong for it. Every other failure keeps the retry wording, including a
non-retryable ``AcpError`` whose text is the provider's own fallback
message — arbitrary internal errors must never leak into chat (CWE-209).

The text is untrusted output: credentials/exfil URLs and local filesystem
paths are redacted, newlines are collapsed, and the length is hard-capped.
"""
if not isinstance(exc, UnknownMemoryStore) and (
not isinstance(exc, AcpError) or exc.transient is not False
):
if not isinstance(exc, UnknownMemoryStore) and not is_user_worded_failure(exc):
return None
try:
text = redact_local_paths(redact(str(exc)))[0]
Expand Down
2 changes: 1 addition & 1 deletion test/test_acp_error_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ async def test_malformed_request_guidance_names_no_unknown_command(self, driver)

This error is TERMINAL, so a channel dispatcher hands the formatted
string straight to the user (telegram's ``_user_safe_failure_reason``
only surfaces ``transient is False``). The formatter does not know which
surfaces a user-worded, non-retryable error). The formatter does not know which
surface will render it, so it may name only commands spelled the same
everywhere. ``/chat new`` was not one: it exists nowhere in the product
(``/new`` on Telegram and Discord, a new tab on the dashboard), so the
Expand Down
54 changes: 37 additions & 17 deletions test/test_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -3270,7 +3270,7 @@ def test_permanent_acp_error_reason_reaches_user(self) -> None:

class _FailingProvider(FakeProvider):
async def stream(self, message: str) -> Any:
raise AcpError(msg, transient=False)
raise AcpError(msg, transient=False, user_worded=True)
yield # pragma: no cover — makes this an async generator

class _Sessions(FakeSessions):
Expand Down Expand Up @@ -6750,25 +6750,34 @@ async def _go() -> None:


class TestUserSafeFailureReason:
def test_permanent_acp_error_yields_bounded_single_line(self) -> None:
exc = AcpError("line one\nline two\ttail", transient=False)
def test_user_worded_acp_error_yields_bounded_single_line(self) -> None:
exc = AcpError("line one\nline two\ttail", transient=False, user_worded=True)
assert _user_safe_failure_reason(exc) == "⚠️ line one line two tail"

def test_local_paths_are_redacted(self) -> None:
exc = AcpError("failed reading /home/alice/.kiro/crew/creds", transient=False)
exc = AcpError(
"failed reading /home/alice/.kiro/crew/creds", transient=False, user_worded=True
)
out = _user_safe_failure_reason(exc)
assert out is not None
assert "/home/alice" not in out

def test_length_hard_cap(self) -> None:
out = _user_safe_failure_reason(AcpError("x" * 5000, transient=False))
out = _user_safe_failure_reason(AcpError("x" * 5000, transient=False, user_worded=True))
assert out is not None
# "⚠️ " prefix + capped body (ellipsis included in the cap).
assert len(out) <= 500 + len("⚠️ ")
assert out.endswith("…")

def test_non_retryable_provider_fallback_returns_none(self) -> None:
# The formatter's fallback passes the provider's own text through and
# still marks it non-retryable; only Kiro Crew's own wording may reach chat.
exc = AcpError("provider said: {'data': 'opaque'}", transient=False)
assert _user_safe_failure_reason(exc) is None

def test_transient_returns_none(self) -> None:
assert _user_safe_failure_reason(AcpError("5xx", transient=True)) is None
exc = AcpError("5xx", transient=True, user_worded=True)
assert _user_safe_failure_reason(exc) is None

def test_unclassified_returns_none(self) -> None:
assert _user_safe_failure_reason(AcpError("unknown", transient=None)) is None
Expand All @@ -6777,7 +6786,8 @@ def test_non_acp_returns_none(self) -> None:
assert _user_safe_failure_reason(ValueError("boom")) is None

def test_empty_message_returns_none(self) -> None:
assert _user_safe_failure_reason(AcpError(" \n ", transient=False)) is None
exc = AcpError(" \n ", transient=False, user_worded=True)
assert _user_safe_failure_reason(exc) is None


# ── /yolo (dispatch/commands.py) + /model (dispatch/pickers.py) ────────────
Expand Down Expand Up @@ -9126,20 +9136,23 @@ class TestTurnLifecycleCharacterization:
def test_a_failure_reason_is_bounded_and_scrubbed(self) -> None:
from kiro_crew.memory_stores import UnknownMemoryStore

assert _user_safe_failure_reason(AcpError("x" * 500, transient=False)) == "⚠️ " + "x" * 500
assert _user_safe_failure_reason(AcpError("x" * 501, transient=False)) == (
"⚠️ " + "x" * 499 + "…"
)
assert _user_safe_failure_reason(AcpError("a" * 498 + " " + "b" * 10, transient=False)) == (
"⚠️ " + "a" * 498 + "…"
assert (
_user_safe_failure_reason(AcpError("x" * 500, transient=False, user_worded=True))
== "⚠️ " + "x" * 500
)
assert _user_safe_failure_reason(
AcpError("x" * 501, transient=False, user_worded=True)
) == ("⚠️ " + "x" * 499 + "…")
assert _user_safe_failure_reason(
AcpError("a" * 498 + " " + "b" * 10, transient=False, user_worded=True)
) == ("⚠️ " + "a" * 498 + "…")
scrubbed = _user_safe_failure_reason(
AcpError("failed reading /srv/op/.kiro/crew/creds", transient=False)
AcpError("failed reading /srv/op/.kiro/crew/creds", transient=False, user_worded=True)
)
assert scrubbed is not None and "/srv/op" not in scrubbed
assert scrubbed.startswith("⚠️ failed reading ")
secret = _user_safe_failure_reason(
AcpError("token AKIAIOSFODNN7EXAMPLE rejected", transient=False)
AcpError("token AKIAIOSFODNN7EXAMPLE rejected", transient=False, user_worded=True)
)
assert secret is not None and "AKIAIOSFODNN7EXAMPLE" not in secret
assert _user_safe_failure_reason(
Expand All @@ -9150,7 +9163,9 @@ class _Unprintable(AcpError):
def __str__(self) -> str:
raise RuntimeError("boom")

assert _user_safe_failure_reason(_Unprintable("x", transient=False)) is None
assert (
_user_safe_failure_reason(_Unprintable("x", transient=False, user_worded=True)) is None
)

def _instrument(self, monkeypatch: Any, d: Any, sess: Any, stream: Any = None) -> list:
from kiro_crew.messaging import auto_title
Expand Down Expand Up @@ -9315,7 +9330,12 @@ def test_close_gets_the_failure_reason_the_turn_classified(self, monkeypatch: An
from kiro_crew.memory_stores import UnknownMemoryStore

cases = [
(AcpError("model not in your plan", transient=False), "⚠️ model not in your plan"),
(
AcpError("model not in your plan", transient=False, user_worded=True),
"⚠️ model not in your plan",
),
# Non-retryable, but the provider's own fallback text: generic reply.
(AcpError("provider said: {'data': 'opaque'}", transient=False), None),
(AcpError("5xx", transient=True), None),
(RuntimeError("boom"), None),
(
Expand Down
11 changes: 7 additions & 4 deletions test/test_telegram_transport_dispatch_composition_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"ACTIVATION_MENTION",
"ACTIVATION_OFF",
"APPROVAL_INTERACTIVE",
"AcpError",
"AgentInfo",
"Any",
"CHAT_TYPE_DIRECT",
Expand Down Expand Up @@ -176,6 +175,7 @@
"html",
"is_bare_mid_turn_override",
"is_session_trusted",
"is_user_worded_failure",
"list_agents",
"lists_host_state",
"live",
Expand Down Expand Up @@ -976,11 +976,12 @@ def test_the_package_initializer_imports_nothing() -> None:
assert [n for n in tree.body if not isinstance(n, ast.Expr)] == []


#: The constructs no owner may hold, each with the reason. All but the two in
#: The constructs no owner may hold, each with the reason. All but the ones in
#: ``_FORBIDDEN_IN_THE_FACADE_TOO`` stay in ``transport_dispatch.py``, either because a
#: repository guard locates them there by path or by ``inspect.getsource`` of that
#: module, or (where marked) as a placement rule that keeps the turn engine whole. Those
#: two are shapes a guard forbids in the facade, which it can only read there. A guard
#: are shapes a guard forbids in the facade as well. The agent-SDK import is one: the
#: facade's boundary baseline is empty, so check_agent_sdk_boundary refuses any. A guard
#: that requires its construct in the facade fails by itself when the construct leaves;
#: a guard that forbids a shape goes blind when the shape lands in an owner instead, so
#: ``_facade_pinned_shapes`` makes both cases fail here, by name. Unguarded
Expand All @@ -1002,7 +1003,9 @@ def test_the_package_initializer_imports_nothing() -> None:
"the queue drain": "test_queue_drain_wake: async def _drain_queue",
"an agent-SDK import": "check_agent_sdk_boundary: kiro_crew.acp / kiro_crew.providers",
}
_FORBIDDEN_IN_THE_FACADE_TOO = frozenset({"a name-grant check", "the forgeable spawn title"})
_FORBIDDEN_IN_THE_FACADE_TOO = frozenset(
{"a name-grant check", "the forgeable spawn title", "an agent-SDK import"}
)


def _facade_pinned_shapes(source: str) -> list[str]:
Expand Down
Loading