stacktrace-cli 0.19.1__py3-none-any.whl → 0.20.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stacktrace/__init__.py +16 -2
- stacktrace/__main__.py +19 -3
- stacktrace/correlate/observed.py +1 -1
- stacktrace/daemon/activity_log.py +2 -1
- stacktrace/daemon/costs.py +22 -6
- stacktrace/daemon/frame_limits.py +11 -0
- stacktrace/daemon/inline_hook.py +59 -4
- stacktrace/daemon/paths.py +7 -2
- stacktrace/daemon/policy_collect.py +10 -2
- stacktrace/daemon/policy_result.py +38 -0
- stacktrace/daemon/policy_runtime.py +63 -33
- stacktrace/daemon/protocol.py +2 -5
- stacktrace/daemon/runtime.py +3 -2
- stacktrace/daemon/unreadable.py +101 -0
- stacktrace/daemon/usage.py +10 -1
- stacktrace/detector/deterministic.py +1870 -178
- stacktrace/detector/guard_files.py +115 -0
- stacktrace/detector/inline.py +20 -1
- stacktrace/detector/rules.py +70 -4
- stacktrace/detector/run.py +2 -2
- stacktrace/detector/secret_paths.py +206 -0
- stacktrace/detector/secret_refs.py +231 -0
- stacktrace/detector/wipers.py +494 -0
- stacktrace/finding_titles.py +8 -1
- stacktrace/hooks/admit.py +102 -0
- stacktrace/hooks/claude.py +63 -10
- stacktrace/monitor/site/app.js +2 -0
- stacktrace/policy_display.py +5 -1
- stacktrace/remote/hook_policy.py +8 -12
- stacktrace/sessions/outcome.py +19 -0
- stacktrace/sessions/toolclass.py +13 -0
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.20.0.dist-info}/METADATA +4 -4
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.20.0.dist-info}/RECORD +35 -26
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.20.0.dist-info}/WHEEL +0 -0
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.20.0.dist-info}/entry_points.txt +0 -0
stacktrace/__init__.py
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
"""The `stacktrace` command-line interface — detection and response for AI agents."""
|
|
2
2
|
|
|
3
3
|
import logging as _logging
|
|
4
|
+
from typing import TYPE_CHECKING
|
|
4
5
|
|
|
5
|
-
__version__ = "0.
|
|
6
|
+
__version__ = "0.20.0"
|
|
6
7
|
|
|
7
8
|
# Only the daemon attaches a handler (ADR-0048). Without this one, a WARNING
|
|
8
9
|
# logged in any other process would reach Python's last-resort handler and
|
|
9
10
|
# print onto the user's terminal.
|
|
10
11
|
_logging.getLogger(__name__).addHandler(_logging.NullHandler())
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from stacktrace.sdk import ObservationClient
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def __getattr__(name: str) -> object:
|
|
18
|
+
# Loaded on first use: the SDK imports httpx and pydantic, and every
|
|
19
|
+
# `stacktrace` process imports this package, the per-call hook included
|
|
20
|
+
# (ADR-0194).
|
|
21
|
+
if name == "ObservationClient":
|
|
22
|
+
from stacktrace.sdk import ObservationClient
|
|
23
|
+
|
|
24
|
+
return ObservationClient
|
|
25
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
26
|
+
|
|
13
27
|
|
|
14
28
|
__all__ = ["ObservationClient", "__version__"]
|
stacktrace/__main__.py
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
"""Entry point shared by the ``stacktrace`` command and ``python -m stacktrace``.
|
|
2
2
|
|
|
3
|
-
The distribution's console script resolves to
|
|
4
|
-
``
|
|
3
|
+
The distribution's console script resolves to ``main`` here. It answers the
|
|
4
|
+
per-call hook command, ``remote policy hooks admit``, before the command line
|
|
5
|
+
loads (ADR-0194), and hands every other command to the Click group in
|
|
6
|
+
``stacktrace.cli``.
|
|
5
7
|
"""
|
|
6
8
|
|
|
7
9
|
from __future__ import annotations
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
import os
|
|
12
|
+
import sys
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main() -> None:
|
|
16
|
+
from stacktrace.hooks.admit import fast_kind, run_standalone
|
|
17
|
+
|
|
18
|
+
kind = fast_kind(sys.argv[1:], os.environ)
|
|
19
|
+
if kind is not None:
|
|
20
|
+
run_standalone(kind)
|
|
21
|
+
return
|
|
22
|
+
from stacktrace.cli import main as cli_main
|
|
23
|
+
|
|
24
|
+
cli_main()
|
|
25
|
+
|
|
10
26
|
|
|
11
27
|
__all__ = ["main"]
|
|
12
28
|
|
stacktrace/correlate/observed.py
CHANGED
|
@@ -1594,7 +1594,7 @@ def command_segments_with_ops(
|
|
|
1594
1594
|
that preceded it (``None`` for the first segment, then ``&&``, ``||``,
|
|
1595
1595
|
``;``, ``|`` or ``\\n``).
|
|
1596
1596
|
|
|
1597
|
-
Only `
|
|
1597
|
+
Only `_resolve_wipes` in the deterministic detector needs the operator
|
|
1598
1598
|
context — to decide whether an inline ``cd`` reliably changed the directory
|
|
1599
1599
|
and whether an ``rm`` segment is unconditionally reachable. Every other
|
|
1600
1600
|
caller uses the plain `command_segments`, which discards the operator.
|
|
@@ -6,7 +6,8 @@ else the package's `NullHandler` keeps those lines off the terminal.
|
|
|
6
6
|
|
|
7
7
|
A line names kinds, counts, class names and event ids. It never carries a
|
|
8
8
|
finding's title or body, an exception message, or anything from a transcript,
|
|
9
|
-
and a peer-supplied session id is written with `%r`.
|
|
9
|
+
and a peer-supplied session id is written with `%r`. The one exception is a
|
|
10
|
+
reader's failure text, written only through `unreadable.describe` (ADR-0191).
|
|
10
11
|
"""
|
|
11
12
|
|
|
12
13
|
from __future__ import annotations
|
stacktrace/daemon/costs.py
CHANGED
|
@@ -55,8 +55,10 @@ from stacktrace.cost.rules import (
|
|
|
55
55
|
to_detection,
|
|
56
56
|
)
|
|
57
57
|
from stacktrace.cost.runs import Run, runs_of
|
|
58
|
+
from stacktrace.daemon.jobs import Backoff
|
|
58
59
|
from stacktrace.daemon.observer import Collected
|
|
59
60
|
from stacktrace.daemon.store import FindingStore, Notification, SessionKey
|
|
61
|
+
from stacktrace.daemon.unreadable import OncePerChange, describe
|
|
60
62
|
from stacktrace.detector.deterministic import run_deterministic
|
|
61
63
|
from stacktrace.detector.render import detection_json
|
|
62
64
|
from stacktrace.detector.rules import COST_RULES, RELIABILITY_RULES
|
|
@@ -558,6 +560,14 @@ class CostPass:
|
|
|
558
560
|
#: warm-up that failed or could not reach every session must retry on
|
|
559
561
|
#: a later tick rather than being read as its one chance.
|
|
560
562
|
self._warmed = False
|
|
563
|
+
#: When a warm-up that failed may read again: 15 s on, doubling,
|
|
564
|
+
#: never past an hour (cost-phase-1 §3). A source that stays
|
|
565
|
+
#: unreadable, such as a Devin store newer than its reader, otherwise
|
|
566
|
+
#: had 36 hours re-read on every tick for as long as the daemon ran.
|
|
567
|
+
self._warm_retries = Backoff(ceiling=3600.0, first=15.0)
|
|
568
|
+
self._warm_after: datetime | None = None
|
|
569
|
+
#: The same failures are logged once, and again when they change.
|
|
570
|
+
self._unreadable = OncePerChange()
|
|
561
571
|
#: The earliest write this pass was handed for each run, main session
|
|
562
572
|
#: and sub-agents alike, by main session: where a finding's clock may
|
|
563
573
|
#: start.
|
|
@@ -592,7 +602,7 @@ class CostPass:
|
|
|
592
602
|
# Every kind's sessions and every member of a run, sub-agents
|
|
593
603
|
# included: the scanner reads them all (ADR-0107).
|
|
594
604
|
changed = [session for item in collected for session in item.view.sessions]
|
|
595
|
-
if not self._warmed:
|
|
605
|
+
if not self._warmed and (self._warm_after is None or now >= self._warm_after):
|
|
596
606
|
changed.extend(self._warm(now, {_key(s) for s in changed}))
|
|
597
607
|
self._written = {}
|
|
598
608
|
for item in collected:
|
|
@@ -719,17 +729,23 @@ class CostPass:
|
|
|
719
729
|
"""Every session active in the last 36 hours that the scanner did not
|
|
720
730
|
hand over, read cold after a start: the scanner resumes from its
|
|
721
731
|
watermark, so a run quiet since then would otherwise be judged short of
|
|
722
|
-
its members, whichever of them resumed it (cost-phase-1 §3). Retried
|
|
723
|
-
|
|
724
|
-
a transient failure does not cost a run's
|
|
725
|
-
being backfilled
|
|
732
|
+
its members, whichever of them resumed it (cost-phase-1 §3). Retried
|
|
733
|
+
15 s later when the read raised or could not reach every session, the
|
|
734
|
+
wait doubling to an hour, so a transient failure does not cost a run's
|
|
735
|
+
members their one chance at being backfilled, and a source that never
|
|
736
|
+
becomes readable is not read again on every tick."""
|
|
726
737
|
try:
|
|
727
738
|
view = self._cold_read(now - _WARM_WINDOW)
|
|
728
739
|
except Exception as error: # noqa: BLE001 - a failed warm-up is not the pass
|
|
729
740
|
logger.debug("cost: could not read the last 36 hours: %s", error)
|
|
741
|
+
self._warm_after = now + timedelta(seconds=self._warm_retries.next())
|
|
730
742
|
return []
|
|
731
743
|
if view.unavailable:
|
|
732
|
-
|
|
744
|
+
self._warm_after = now + timedelta(seconds=self._warm_retries.next())
|
|
745
|
+
if self._unreadable.due(view.unavailable):
|
|
746
|
+
logger.debug(
|
|
747
|
+
"cost: some sessions could not be read: %s", describe(view.unavailable)
|
|
748
|
+
)
|
|
733
749
|
return [session for session in view.sessions if _key(session) not in have]
|
|
734
750
|
self._warmed = True
|
|
735
751
|
return [session for session in view.sessions if _key(session) not in have]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""The wire version and the frame limits that the per-call hook reads.
|
|
2
|
+
|
|
3
|
+
`protocol.py` imports the store, and the store costs the hook about 0.8 s, so
|
|
4
|
+
these three live here and `protocol.py` imports them (ADR-0194).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
VERSION = 1
|
|
8
|
+
MAX_FRAME_BYTES = 64 * 1024
|
|
9
|
+
#: An `authorize` request that carries a built-in call's input (ADR-0158). Only
|
|
10
|
+
#: that request may be this large; every other incoming frame keeps the usual limit.
|
|
11
|
+
MAX_CALL_FRAME_BYTES = 2 * 1024 * 1024
|
stacktrace/daemon/inline_hook.py
CHANGED
|
@@ -44,6 +44,7 @@ from pathlib import Path
|
|
|
44
44
|
from typing import TYPE_CHECKING
|
|
45
45
|
|
|
46
46
|
from stacktrace import __version__
|
|
47
|
+
from stacktrace.correlate.observed import declares_remote_transport
|
|
47
48
|
from stacktrace.daemon.policy_runtime import sub_agent_session
|
|
48
49
|
from stacktrace.daemon.store import marker_from_json, marker_json
|
|
49
50
|
from stacktrace.detector.finding import Detection
|
|
@@ -68,6 +69,12 @@ if TYPE_CHECKING:
|
|
|
68
69
|
|
|
69
70
|
logger = logging.getLogger(__name__)
|
|
70
71
|
|
|
72
|
+
#: The name a pending MCP call is read under when its hook sent no tool name
|
|
73
|
+
#: (ADR-0195). Its server and transport are set on the read call directly, so
|
|
74
|
+
#: the name only marks it as an MCP call.
|
|
75
|
+
_PENDING_MCP_TOOL = "mcp__stacktrace_pending__call"
|
|
76
|
+
#: The keys every pending call carries.
|
|
77
|
+
_CALL_KEYS = frozenset({"tool_use_id", "cwd", "tool_input"})
|
|
71
78
|
#: Claude Code's session ids are UUIDs. Anything else is refused rather than
|
|
72
79
|
#: kept, because the id names the finding's session and a `:` would forge one.
|
|
73
80
|
_SESSION_ID = re.compile(r"[A-Za-z0-9-]{1,128}")
|
|
@@ -92,6 +99,12 @@ class PendingCall:
|
|
|
92
99
|
tool_use_id: str
|
|
93
100
|
cwd: str
|
|
94
101
|
tool_input: Mapping[str, object]
|
|
102
|
+
#: For an MCP call, the server Claude Code named, if any (ADR-0202), and
|
|
103
|
+
#: the remote transport its configuration names, if any (ADR-0195). The
|
|
104
|
+
#: transport is declared, never observed, so it is never written into the
|
|
105
|
+
#: call (ADR-0134).
|
|
106
|
+
mcp_server: str | None = None
|
|
107
|
+
declared_transport: str | None = None
|
|
95
108
|
|
|
96
109
|
|
|
97
110
|
def pending_call(action: Mapping[str, object]) -> PendingCall:
|
|
@@ -108,7 +121,10 @@ def pending_call(action: Mapping[str, object]) -> PendingCall:
|
|
|
108
121
|
# costs only attribution, where refusing the call would cost the block.
|
|
109
122
|
if not isinstance(agent_id, str) or not _AGENT_ID.fullmatch(agent_id):
|
|
110
123
|
agent_id = None
|
|
111
|
-
|
|
124
|
+
mcp = action.get("action_kind") == "mcp_tool"
|
|
125
|
+
# An MCP call may name its tool (ADR-0202); a hook from ADR-0195 does not.
|
|
126
|
+
allowed = _CALL_KEYS | ({"tool_name"} if mcp else set())
|
|
127
|
+
if not isinstance(call, dict) or not _CALL_KEYS <= set(call) <= allowed: # pyright: ignore[reportUnknownArgumentType]
|
|
112
128
|
raise ValueError("invalid_call")
|
|
113
129
|
tool_use_id, cwd, tool_input = call["tool_use_id"], call["cwd"], call["tool_input"]
|
|
114
130
|
if not isinstance(tool_use_id, str) or not _TOOL_USE_ID.fullmatch(tool_use_id):
|
|
@@ -117,7 +133,31 @@ def pending_call(action: Mapping[str, object]) -> PendingCall:
|
|
|
117
133
|
raise ValueError("invalid_call")
|
|
118
134
|
if not isinstance(tool_input, dict):
|
|
119
135
|
raise ValueError("invalid_call") # noqa: TRY004 - one refusal type, read by its caller
|
|
120
|
-
|
|
136
|
+
server, transport = None, None
|
|
137
|
+
if mcp:
|
|
138
|
+
# Judged whatever the server (ADR-0202). A transport is passed only
|
|
139
|
+
# for a server configured as remote, and adds egress (ADR-0195).
|
|
140
|
+
given = action.get("transport")
|
|
141
|
+
if given is not None and (
|
|
142
|
+
not isinstance(given, str) or not declares_remote_transport(given)
|
|
143
|
+
):
|
|
144
|
+
raise ValueError("uninspected_tool")
|
|
145
|
+
transport = given
|
|
146
|
+
named = call.get("tool_name", _PENDING_MCP_TOOL) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
147
|
+
if (
|
|
148
|
+
not isinstance(named, str)
|
|
149
|
+
or not named.startswith("mcp__")
|
|
150
|
+
or len(named) > 4096
|
|
151
|
+
or "\x00" in named
|
|
152
|
+
):
|
|
153
|
+
raise ValueError("invalid_call")
|
|
154
|
+
if not isinstance(agent_kind, str) or not isinstance(tool, str):
|
|
155
|
+
raise ValueError("uninspected_tool")
|
|
156
|
+
# The selector names the server when Claude Code named one; with an
|
|
157
|
+
# empty source it is the tool's name, and the tool's name says it.
|
|
158
|
+
server = tool if action.get("source") else None
|
|
159
|
+
tool = named
|
|
160
|
+
elif (
|
|
121
161
|
not isinstance(agent_kind, str)
|
|
122
162
|
or not isinstance(tool, str)
|
|
123
163
|
or not inspected(agent_kind, tool)
|
|
@@ -132,6 +172,8 @@ def pending_call(action: Mapping[str, object]) -> PendingCall:
|
|
|
132
172
|
tool_use_id=tool_use_id,
|
|
133
173
|
cwd=cwd,
|
|
134
174
|
tool_input={str(k): v for k, v in tool_input.items()}, # pyright: ignore[reportUnknownVariableType]
|
|
175
|
+
mcp_server=server,
|
|
176
|
+
declared_transport=transport,
|
|
135
177
|
)
|
|
136
178
|
|
|
137
179
|
|
|
@@ -270,14 +312,27 @@ def judge(call: PendingCall, rules: frozenset[str]) -> tuple[Detection, ...]:
|
|
|
270
312
|
replace(
|
|
271
313
|
t,
|
|
272
314
|
tool_calls=tuple(
|
|
273
|
-
|
|
315
|
+
(
|
|
316
|
+
replace(c, span=span, working_directory=call.cwd)
|
|
317
|
+
if call.mcp_server is None
|
|
318
|
+
else replace(
|
|
319
|
+
c, span=span, working_directory=call.cwd, mcp_server=call.mcp_server
|
|
320
|
+
)
|
|
321
|
+
)
|
|
322
|
+
if c.span in pending
|
|
323
|
+
else c
|
|
274
324
|
for c in t.tool_calls
|
|
275
325
|
),
|
|
276
326
|
)
|
|
277
327
|
for t in turn.session.turns
|
|
278
328
|
),
|
|
279
329
|
)
|
|
280
|
-
return judge_pending(
|
|
330
|
+
return judge_pending(
|
|
331
|
+
session,
|
|
332
|
+
frozenset({span}) if pending else frozenset(),
|
|
333
|
+
rules,
|
|
334
|
+
declared_transport=call.declared_transport,
|
|
335
|
+
)
|
|
281
336
|
|
|
282
337
|
|
|
283
338
|
def inspect_call(action: dict[str, object], rules: frozenset[str]) -> tuple[Detection, ...]:
|
stacktrace/daemon/paths.py
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import os
|
|
6
|
-
import tempfile
|
|
7
6
|
from collections.abc import Mapping
|
|
8
7
|
from dataclasses import dataclass
|
|
9
8
|
from pathlib import Path
|
|
@@ -29,7 +28,13 @@ class RuntimePaths:
|
|
|
29
28
|
environ = os.environ if environment is None else environment
|
|
30
29
|
state_root = private_state_root(environ)
|
|
31
30
|
user_key = str(os.getuid()) if hasattr(os, "getuid") else str(os.getpid())
|
|
32
|
-
|
|
31
|
+
if os.name == "posix":
|
|
32
|
+
runtime_root = Path("/tmp")
|
|
33
|
+
else:
|
|
34
|
+
# Imported here: the per-call hook reads these paths (ADR-0194).
|
|
35
|
+
import tempfile
|
|
36
|
+
|
|
37
|
+
runtime_root = Path(tempfile.gettempdir())
|
|
33
38
|
short_runtime = runtime_root / f"stacktrace-{user_key}"
|
|
34
39
|
return cls(
|
|
35
40
|
socket=Path(environ.get("STACKTRACE_DAEMON_SOCKET", short_runtime / "daemon-v1.sock")),
|
|
@@ -143,6 +143,12 @@ def evaluate_bundle(
|
|
|
143
143
|
if blocked
|
|
144
144
|
else {}
|
|
145
145
|
)
|
|
146
|
+
if component_type == "mcp_server":
|
|
147
|
+
# The egress rules judge a call's input only for a server whose
|
|
148
|
+
# configuration names a remote transport (ADR-0195).
|
|
149
|
+
transport = ref.extra.get("transport")
|
|
150
|
+
if isinstance(transport, str) and 0 < len(transport) <= 64:
|
|
151
|
+
found["transport"] = transport
|
|
146
152
|
if scope.agent_kind == "codex":
|
|
147
153
|
entries.extend(
|
|
148
154
|
(Entry("mcp_tool", namespace, "codex", blocked, rule, detail, **found), 0)
|
|
@@ -327,7 +333,7 @@ def collect_bounded(document: dict[str, object], scope: Scope) -> Collected:
|
|
|
327
333
|
for row in rows:
|
|
328
334
|
if (
|
|
329
335
|
not isinstance(row, list)
|
|
330
|
-
or len(row) !=
|
|
336
|
+
or len(row) != 10
|
|
331
337
|
or not all(isinstance(v, str) and len(v) <= 4096 for v in row[:3])
|
|
332
338
|
or not isinstance(row[3], bool)
|
|
333
339
|
or not (row[4:6] == [None, None] or valid_rule_detail(row[4], row[5]))
|
|
@@ -335,9 +341,10 @@ def collect_bounded(document: dict[str, object], scope: Scope) -> Collected:
|
|
|
335
341
|
or not isinstance(row[8], list)
|
|
336
342
|
or len(row[8]) > 256
|
|
337
343
|
or not all(isinstance(v, str) and len(v) <= 4096 for v in row[8])
|
|
344
|
+
or not (row[9] is None or (isinstance(row[9], str) and 0 < len(row[9]) <= 64))
|
|
338
345
|
):
|
|
339
346
|
raise ValueError("policy_evidence_invalid")
|
|
340
|
-
entries.append(Entry(*row[:8], evidence=tuple(row[8])))
|
|
347
|
+
entries.append(Entry(*row[:8], evidence=tuple(row[8]), transport=row[9]))
|
|
341
348
|
paths = bundle["watch_paths"]
|
|
342
349
|
if (
|
|
343
350
|
not isinstance(paths, list)
|
|
@@ -493,6 +500,7 @@ def main() -> int:
|
|
|
493
500
|
e.version,
|
|
494
501
|
e.identity,
|
|
495
502
|
list(e.evidence),
|
|
503
|
+
e.transport,
|
|
496
504
|
]
|
|
497
505
|
for e in entries
|
|
498
506
|
],
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""An admission answer, apart from the runtime that computes it.
|
|
2
|
+
|
|
3
|
+
The hook builds these from the daemon's reply on every covered call, so this
|
|
4
|
+
module imports only the standard library (ADR-0194). `policy_runtime` imports
|
|
5
|
+
them from here.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Callable
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class DecidedBy:
|
|
16
|
+
"""Which policy entry, rule and revision decided an answer (ADR-0159)."""
|
|
17
|
+
|
|
18
|
+
policy_entry_id: str
|
|
19
|
+
rule_id: str
|
|
20
|
+
policy_revision: int
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True)
|
|
24
|
+
class Result:
|
|
25
|
+
outcome: str
|
|
26
|
+
reason: str
|
|
27
|
+
revision: int | None = None
|
|
28
|
+
rule: str | None = None
|
|
29
|
+
detail: str | None = None
|
|
30
|
+
decided_by: DecidedBy | None = None
|
|
31
|
+
#: Run only once the caller has confirmed this decision reached the hook.
|
|
32
|
+
#: A detection block's `on_blocked` commit and a component deny's
|
|
33
|
+
#: `on_component_denied` commit are deferred to here: committing either
|
|
34
|
+
#: eagerly, inside `authorize()`, would keep a block's marker for a prompt
|
|
35
|
+
#: or call whose deny never reached the agent, because the hook's own
|
|
36
|
+
#: timeout (`CALL_TIMEOUT`) expired first and it already let the prompt or
|
|
37
|
+
#: call through. The scan would then report a call that ran as blocked.
|
|
38
|
+
commit: tuple[Callable[[], None], ...] = ()
|
|
@@ -17,7 +17,8 @@ from typing import TYPE_CHECKING, Literal
|
|
|
17
17
|
|
|
18
18
|
from openaca.core import Policy, PolicyValidationError, parse_policy
|
|
19
19
|
|
|
20
|
-
from stacktrace.
|
|
20
|
+
from stacktrace.correlate.observed import declares_remote_transport
|
|
21
|
+
from stacktrace.daemon.jobs import Backoff, Job, JobThread
|
|
21
22
|
from stacktrace.daemon.policy_inputs import (
|
|
22
23
|
DetectionEntry,
|
|
23
24
|
PolicyEntry,
|
|
@@ -28,6 +29,7 @@ from stacktrace.daemon.policy_inputs import (
|
|
|
28
29
|
Tenant,
|
|
29
30
|
off_rules,
|
|
30
31
|
)
|
|
32
|
+
from stacktrace.daemon.policy_result import DecidedBy, Result
|
|
31
33
|
from stacktrace.detector.rules import ADMISSION_RULES, BLOCKABLE, POSTURE_RULE, VULNERABILITY_RULE
|
|
32
34
|
from stacktrace.policy_display import default_description
|
|
33
35
|
from stacktrace.remote.config import ConfigError, RemoteConfig, load_remote_config
|
|
@@ -203,33 +205,9 @@ class Entry:
|
|
|
203
205
|
version: str | None = None
|
|
204
206
|
identity: str | None = None
|
|
205
207
|
evidence: tuple[str, ...] = ()
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
class DecidedBy:
|
|
210
|
-
"""Which policy entry, rule and revision decided an answer (ADR-0159)."""
|
|
211
|
-
|
|
212
|
-
policy_entry_id: str
|
|
213
|
-
rule_id: str
|
|
214
|
-
policy_revision: int
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
@dataclass(frozen=True)
|
|
218
|
-
class Result:
|
|
219
|
-
outcome: str
|
|
220
|
-
reason: str
|
|
221
|
-
revision: int | None = None
|
|
222
|
-
rule: str | None = None
|
|
223
|
-
detail: str | None = None
|
|
224
|
-
decided_by: DecidedBy | None = None
|
|
225
|
-
#: Run only once the caller has confirmed this decision reached the hook.
|
|
226
|
-
#: A detection block's `on_blocked` commit and a component deny's
|
|
227
|
-
#: `on_component_denied` commit are deferred to here: committing either
|
|
228
|
-
#: eagerly, inside `authorize()`, would keep a block's marker for a prompt
|
|
229
|
-
#: or call whose deny never reached the agent, because the hook's own
|
|
230
|
-
#: timeout (`CALL_TIMEOUT`) expired first and it already let the prompt or
|
|
231
|
-
#: call through. The scan would then report a call that ran as blocked.
|
|
232
|
-
commit: tuple[Callable[[], None], ...] = ()
|
|
208
|
+
#: An MCP server's configured transport. The egress rules judge a call's
|
|
209
|
+
#: input only for a server whose configuration names a remote one (ADR-0195).
|
|
210
|
+
transport: str | None = None
|
|
233
211
|
|
|
234
212
|
|
|
235
213
|
@dataclass(frozen=True)
|
|
@@ -533,6 +511,18 @@ def configuration_digest(scope: Scope, watch_paths: tuple[str, ...]) -> str:
|
|
|
533
511
|
return hashlib.sha256(json.dumps(records).encode()).hexdigest()
|
|
534
512
|
|
|
535
513
|
|
|
514
|
+
def _occurrence_of(call: dict[str, object]) -> dict[str, object]:
|
|
515
|
+
"""An MCP action's occurrence keys, with the call id its `call` carries as
|
|
516
|
+
`tool_use_id`, the key a component deny's marker names the call by
|
|
517
|
+
(ADR-0195). The call itself is kept for the egress judge."""
|
|
518
|
+
inner = call.get("call")
|
|
519
|
+
tool_use_id = inner.get("tool_use_id") if isinstance(inner, dict) else None # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
520
|
+
result = dict(call)
|
|
521
|
+
if isinstance(tool_use_id, str):
|
|
522
|
+
result["tool_use_id"] = tool_use_id
|
|
523
|
+
return result
|
|
524
|
+
|
|
525
|
+
|
|
536
526
|
class PolicyRuntime:
|
|
537
527
|
"""One coalescing worker; hooks only register/read, never collect or fetch."""
|
|
538
528
|
|
|
@@ -563,6 +553,14 @@ class PolicyRuntime:
|
|
|
563
553
|
self._attempt_interval = min(30, poll_interval)
|
|
564
554
|
self._fetch_at = float("-inf")
|
|
565
555
|
self._attempts: dict[Scope, float] = {}
|
|
556
|
+
#: How long a scope whose last collection failed waits before the
|
|
557
|
+
#: next: 30 s, doubling, never past an hour. A collection that fails
|
|
558
|
+
#: every time, such as a project too large to walk within the
|
|
559
|
+
#: deadline, otherwise ran again every 30 s for as long as the daemon
|
|
560
|
+
#: did. A published snapshot resets it; a configuration change does
|
|
561
|
+
#: not, because `~/.claude.json` changes about once a minute.
|
|
562
|
+
self._retries: dict[Scope, Backoff] = {}
|
|
563
|
+
self._waits: dict[Scope, float] = {}
|
|
566
564
|
self._digests: dict[Scope, str] = {}
|
|
567
565
|
self._paths: dict[Scope, tuple[str, ...]] = {}
|
|
568
566
|
self._error: str | None = None
|
|
@@ -660,7 +658,14 @@ class PolicyRuntime:
|
|
|
660
658
|
# Freshness begins before collection; slow work does not extend it.
|
|
661
659
|
started = self.clock()
|
|
662
660
|
wall_started = self.wall_clock()
|
|
663
|
-
|
|
661
|
+
try:
|
|
662
|
+
collected = self.collect(document, scope)
|
|
663
|
+
except Exception:
|
|
664
|
+
retries = self._retries.setdefault(
|
|
665
|
+
scope, Backoff(ceiling=REFRESH_INTERVAL, first=self._attempt_interval)
|
|
666
|
+
)
|
|
667
|
+
self._waits[scope] = retries.next()
|
|
668
|
+
raise
|
|
664
669
|
# Recheck the inputs observed before collection. New discovered paths
|
|
665
670
|
# join the next pass's watch set only after successful publication.
|
|
666
671
|
digest = configuration_digest(scope, self._paths.get(scope, ()))
|
|
@@ -682,6 +687,8 @@ class PolicyRuntime:
|
|
|
682
687
|
collected.builtin_skills,
|
|
683
688
|
):
|
|
684
689
|
self._paths[scope] = collected.watch_paths
|
|
690
|
+
self._retries.pop(scope, None)
|
|
691
|
+
self._waits.pop(scope, None)
|
|
685
692
|
self._digests[scope] = configuration_digest(scope, collected.watch_paths)
|
|
686
693
|
self._error = None
|
|
687
694
|
if len(ready) > 1:
|
|
@@ -698,7 +705,8 @@ class PolicyRuntime:
|
|
|
698
705
|
def _due(self, scope: Scope) -> bool:
|
|
699
706
|
if scope.agent_kind in UNCOLLECTED:
|
|
700
707
|
return False
|
|
701
|
-
|
|
708
|
+
wait = self._waits.get(scope, self._attempt_interval)
|
|
709
|
+
if self.clock() - self._attempts.get(scope, float("-inf")) < wait:
|
|
702
710
|
return False
|
|
703
711
|
snapshot = self.cache.snapshots.get(scope)
|
|
704
712
|
return (
|
|
@@ -787,6 +795,10 @@ class PolicyRuntime:
|
|
|
787
795
|
kind, name, source = action["action_kind"], action["name"], action["source"]
|
|
788
796
|
builtin = kind == "tool" and source == "builtin" and "call" in call
|
|
789
797
|
prompt = kind == "prompt" and "call" in call
|
|
798
|
+
# An MCP call's input, judged once its server is decided, whether
|
|
799
|
+
# or not the snapshot places the server (ADR-0202). A server
|
|
800
|
+
# configured as remote adds egress (ADR-0195).
|
|
801
|
+
mcp = kind == "mcp_tool" and "call" in call
|
|
790
802
|
if "prompt_id" in call and not prompt:
|
|
791
803
|
return Result("defer", "invalid_action")
|
|
792
804
|
if (
|
|
@@ -795,9 +807,7 @@ class PolicyRuntime:
|
|
|
795
807
|
and not prompt
|
|
796
808
|
):
|
|
797
809
|
return Result("defer", "uncovered_action")
|
|
798
|
-
if "call" in call and not builtin and not prompt:
|
|
799
|
-
# This step judges built-in calls only; an MCP call's egress
|
|
800
|
-
# needs its server's declared transport first.
|
|
810
|
+
if "call" in call and not builtin and not prompt and not mcp:
|
|
801
811
|
return Result("defer", "invalid_action")
|
|
802
812
|
scope = Scope(
|
|
803
813
|
str(action["config_root"]),
|
|
@@ -840,10 +850,30 @@ class PolicyRuntime:
|
|
|
840
850
|
if e.action == "block"
|
|
841
851
|
and e.rule_id in BLOCKABLE
|
|
842
852
|
and event in BLOCKABLE[e.rule_id].events
|
|
853
|
+
and (not mcp or BLOCKABLE[e.rule_id].mcp)
|
|
854
|
+
)
|
|
855
|
+
# The one collected server the selector names: a selector
|
|
856
|
+
# two components share has none, and no transport.
|
|
857
|
+
server = (
|
|
858
|
+
snapshot.resolved.get((str(kind), str(name), str(source)))
|
|
859
|
+
if mcp and snapshot is not None
|
|
860
|
+
else None
|
|
843
861
|
)
|
|
844
862
|
self.worker.wake()
|
|
845
863
|
if builtin or prompt:
|
|
846
864
|
return self._detection({**action, **call}, blocking, revision) or result
|
|
865
|
+
if mcp:
|
|
866
|
+
call = _occurrence_of(call)
|
|
867
|
+
transport = server.transport if server is not None else None
|
|
868
|
+
if result.outcome != "deny":
|
|
869
|
+
# Admission is decided; the arguments need no server.
|
|
870
|
+
remote = (
|
|
871
|
+
{"transport": transport} if declares_remote_transport(transport) else {}
|
|
872
|
+
)
|
|
873
|
+
judged = self._detection({**action, **call, **remote}, blocking, revision)
|
|
874
|
+
if judged is not None and judged.outcome == "deny":
|
|
875
|
+
return judged
|
|
876
|
+
call.pop("call", None)
|
|
847
877
|
if result.outcome == "deny":
|
|
848
878
|
# A decision from the snapshot's own table names one component;
|
|
849
879
|
# a category default names none.
|
stacktrace/daemon/protocol.py
CHANGED
|
@@ -6,16 +6,13 @@ import json
|
|
|
6
6
|
from dataclasses import asdict
|
|
7
7
|
from typing import Any
|
|
8
8
|
|
|
9
|
+
# The client, the server and the tests read all three from here.
|
|
10
|
+
from .frame_limits import MAX_CALL_FRAME_BYTES, MAX_FRAME_BYTES, VERSION # noqa: F401
|
|
9
11
|
from .store import Notification, SessionKey
|
|
10
12
|
|
|
11
|
-
VERSION = 1
|
|
12
|
-
MAX_FRAME_BYTES = 64 * 1024
|
|
13
13
|
# Status includes bounded project paths whose JSON Unicode escapes can exceed the usual frame limit.
|
|
14
14
|
MAX_POLICY_STATUS_FRAME_BYTES = 128 * 1024
|
|
15
15
|
MAX_POLICY_VIEW_FRAME_BYTES = 4 * 1024 * 1024
|
|
16
|
-
#: An `authorize` request that carries a built-in call's input (ADR-0158). Only
|
|
17
|
-
#: that request may be this large; every other incoming frame keeps the usual limit.
|
|
18
|
-
MAX_CALL_FRAME_BYTES = 2 * 1024 * 1024
|
|
19
16
|
|
|
20
17
|
#: How many entries any one list-shaped field puts on the wire -- BOM progress
|
|
21
18
|
#: lines carried back to an operator who asked for the upload, the agent kinds a
|
stacktrace/daemon/runtime.py
CHANGED
|
@@ -29,7 +29,7 @@ from stacktrace.telemetry import emit_error
|
|
|
29
29
|
from stacktrace.telemetry.posthog import flush as telemetry_flush
|
|
30
30
|
from stacktrace.webhook.config import get_config_path
|
|
31
31
|
|
|
32
|
-
from . import activity_log
|
|
32
|
+
from . import activity_log, unreadable
|
|
33
33
|
from .accounts import FleetOrg, SignedInAccounts
|
|
34
34
|
from .bom import BomJobReport, run_bom_job
|
|
35
35
|
from .client import MonitorOutputError, ProtocolError, relay_notifications
|
|
@@ -1051,7 +1051,8 @@ def _backfill(baseline: Baseline, root: Path, kinds: tuple[str, ...]) -> None:
|
|
|
1051
1051
|
return
|
|
1052
1052
|
logger.warning(
|
|
1053
1053
|
"cost baseline: %d transcript sources could not be read; the rules that need "
|
|
1054
|
-
"the baseline stay silent, and it is read again in an hour",
|
|
1054
|
+
"the baseline stay silent, and it is read again in an hour; %s",
|
|
1055
1055
|
len(baseline.unreadable),
|
|
1056
|
+
unreadable.describe(baseline.unreadable),
|
|
1056
1057
|
)
|
|
1057
1058
|
time.sleep(_BACKFILL_RETRY)
|