stacktrace-cli 0.23.0__py3-none-any.whl → 0.24.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 +1 -1
- stacktrace/agentspec/grants.py +16 -5
- stacktrace/agentspec/serve.py +6 -1
- stacktrace/analysis.py +7 -0
- stacktrace/cli.py +15 -1
- stacktrace/daemon/db.py +5 -0
- stacktrace/daemon/inline_hook.py +121 -16
- stacktrace/daemon/observer.py +76 -8
- stacktrace/daemon/policy_inputs.py +57 -0
- stacktrace/daemon/policy_result.py +36 -4
- stacktrace/daemon/policy_runtime.py +31 -34
- stacktrace/daemon/runtime.py +1 -0
- stacktrace/daemon/store.py +21 -3
- stacktrace/detector/deterministic.py +148 -1
- stacktrace/detector/hook_table.py +2 -0
- stacktrace/detector/inline.py +16 -16
- stacktrace/detector/profanity-en.txt +375 -0
- stacktrace/detector/profanity.py +234 -0
- stacktrace/detector/rules.py +105 -2
- stacktrace/detector/run.py +6 -2
- stacktrace/detector/settings.py +27 -0
- stacktrace/finding_titles.py +2 -0
- stacktrace/gateway/marker.py +53 -2
- stacktrace/hooks/claude.py +41 -6
- stacktrace/hooks/codex.py +5 -2
- stacktrace/hooks/cursor.py +5 -2
- stacktrace/hooks/devin.py +21 -5
- stacktrace/monitor/render.py +6 -1
- stacktrace/monitor/site/app.js +2 -0
- stacktrace/prompt_text.py +50 -0
- stacktrace/remote/sync_detect.py +8 -0
- {stacktrace_cli-0.23.0.dist-info → stacktrace_cli-0.24.0.dist-info}/METADATA +2 -2
- {stacktrace_cli-0.23.0.dist-info → stacktrace_cli-0.24.0.dist-info}/RECORD +35 -31
- {stacktrace_cli-0.23.0.dist-info → stacktrace_cli-0.24.0.dist-info}/WHEEL +0 -0
- {stacktrace_cli-0.23.0.dist-info → stacktrace_cli-0.24.0.dist-info}/entry_points.txt +0 -0
stacktrace/__init__.py
CHANGED
stacktrace/agentspec/grants.py
CHANGED
|
@@ -79,29 +79,39 @@ class Grants:
|
|
|
79
79
|
self._grants: list[Grant] = []
|
|
80
80
|
self._lock = threading.Lock()
|
|
81
81
|
|
|
82
|
+
def _drop_expired(self, now: float) -> None:
|
|
83
|
+
"""Forget asks and grants whose minutes are over.
|
|
84
|
+
|
|
85
|
+
`answer` and `take` already ignore one that is over. A daemon that
|
|
86
|
+
keeps every one still holds a session that never came back.
|
|
87
|
+
"""
|
|
88
|
+
self._asks = [a for a in self._asks if now - a.at < a.minutes * 60]
|
|
89
|
+
self._grants = [g for g in self._grants if g.expires > now]
|
|
90
|
+
|
|
82
91
|
def asked(self, session: str, key: str, rule: str, minutes: int) -> None:
|
|
83
92
|
"""One pending ask per (session, call, rule): an agent retrying a held
|
|
84
93
|
call before the person answers renews its ask, never adds a second."""
|
|
85
94
|
with self._lock:
|
|
95
|
+
now = time.time()
|
|
96
|
+
self._drop_expired(now)
|
|
86
97
|
self._asks = [
|
|
87
98
|
a for a in self._asks if (a.session, a.key, a.rule) != (session, key, rule)
|
|
88
99
|
]
|
|
89
|
-
self._asks.append(_Ask(session, key, rule,
|
|
100
|
+
self._asks.append(_Ask(session, key, rule, now, minutes))
|
|
90
101
|
|
|
91
102
|
def answer(self, session: str, text: str) -> list[Grant]:
|
|
92
103
|
"""A yes grants every rule that asked about the session's latest held
|
|
93
104
|
call, the call the person was shown; anything else drops them."""
|
|
94
105
|
with self._lock:
|
|
106
|
+
now = time.time()
|
|
107
|
+
self._drop_expired(now)
|
|
95
108
|
pending = [
|
|
96
|
-
a
|
|
97
|
-
for a in self._asks
|
|
98
|
-
if a.session == session and time.time() - a.at < a.minutes * 60
|
|
109
|
+
a for a in self._asks if a.session == session and now - a.at < a.minutes * 60
|
|
99
110
|
]
|
|
100
111
|
self._asks = [a for a in self._asks if a.session != session]
|
|
101
112
|
if not pending or not is_yes(text):
|
|
102
113
|
return []
|
|
103
114
|
key = pending[-1].key
|
|
104
|
-
now = time.time()
|
|
105
115
|
# One grant per (call, rule), so one yes lifts a call once: a
|
|
106
116
|
# duplicate would let the same call through again unasked.
|
|
107
117
|
rules = dict.fromkeys(a.rule for a in pending if a.key == key)
|
|
@@ -122,6 +132,7 @@ class Grants:
|
|
|
122
132
|
and did not then still asks."""
|
|
123
133
|
with self._lock:
|
|
124
134
|
now = time.time()
|
|
135
|
+
self._drop_expired(now)
|
|
125
136
|
found: dict[str, Grant] = {}
|
|
126
137
|
for grant in self._grants:
|
|
127
138
|
if grant.session == session and grant.key == key and grant.expires > now:
|
stacktrace/agentspec/serve.py
CHANGED
|
@@ -73,7 +73,12 @@ class _Host:
|
|
|
73
73
|
action = request["action"]
|
|
74
74
|
if action.get("action_kind") == "prompt":
|
|
75
75
|
prompt = pending_prompt(action)
|
|
76
|
-
|
|
76
|
+
# A long prompt arrives as its head; a grant is a short reply (ADR-0226).
|
|
77
|
+
said = (
|
|
78
|
+
self.engine.on_prompt(prompt.agent_kind, prompt.session_id, prompt.text)
|
|
79
|
+
if prompt.whole
|
|
80
|
+
else None
|
|
81
|
+
)
|
|
77
82
|
return {"action": "allow", "to_person": said}
|
|
78
83
|
pc = validated_call(action)
|
|
79
84
|
transcript = request.get("transcript_path")
|
stacktrace/analysis.py
CHANGED
|
@@ -38,6 +38,7 @@ from stacktrace.detector.run import (
|
|
|
38
38
|
DetectorRun,
|
|
39
39
|
run_detector,
|
|
40
40
|
)
|
|
41
|
+
from stacktrace.detector.settings import NO_SETTINGS, RuleSettings
|
|
41
42
|
from stacktrace.sessions.access import SessionView, collect_sessions
|
|
42
43
|
|
|
43
44
|
|
|
@@ -174,6 +175,7 @@ def analyse(
|
|
|
174
175
|
jev_key: str | None = None,
|
|
175
176
|
history: DriftHistory | None = None,
|
|
176
177
|
company_domains: frozenset[str] | None = None,
|
|
178
|
+
settings: RuleSettings = NO_SETTINGS,
|
|
177
179
|
) -> Analysis:
|
|
178
180
|
"""Read this machine's sessions, correlate them, and judge them.
|
|
179
181
|
|
|
@@ -229,6 +231,7 @@ def analyse(
|
|
|
229
231
|
jev_key=jev_key,
|
|
230
232
|
history=history,
|
|
231
233
|
company_domains=company_domains,
|
|
234
|
+
settings=settings,
|
|
232
235
|
)
|
|
233
236
|
return scoped_to_window(found)
|
|
234
237
|
|
|
@@ -252,6 +255,7 @@ def analyse_sessions(
|
|
|
252
255
|
history: DriftHistory | None = None,
|
|
253
256
|
gate_conditions: Mapping[str, object] | None = None,
|
|
254
257
|
company_domains: frozenset[str] | None = None,
|
|
258
|
+
settings: RuleSettings = NO_SETTINGS,
|
|
255
259
|
) -> Analysis:
|
|
256
260
|
"""Correlate and judge a session view already collected by a long-lived reader.
|
|
257
261
|
|
|
@@ -278,6 +282,7 @@ def analyse_sessions(
|
|
|
278
282
|
history=history,
|
|
279
283
|
gate_conditions=gate_conditions,
|
|
280
284
|
company_domains=company_domains,
|
|
285
|
+
settings=settings,
|
|
281
286
|
)
|
|
282
287
|
|
|
283
288
|
|
|
@@ -299,6 +304,7 @@ def _analyse_sessions(
|
|
|
299
304
|
history: DriftHistory | None,
|
|
300
305
|
gate_conditions: Mapping[str, object] | None = None,
|
|
301
306
|
company_domains: frozenset[str] | None = None,
|
|
307
|
+
settings: RuleSettings = NO_SETTINGS,
|
|
302
308
|
) -> Analysis:
|
|
303
309
|
acquired = _acquire(
|
|
304
310
|
sessions,
|
|
@@ -318,6 +324,7 @@ def _analyse_sessions(
|
|
|
318
324
|
history=history,
|
|
319
325
|
gate_conditions=gate_conditions,
|
|
320
326
|
company_domains=company_domains,
|
|
327
|
+
settings=settings,
|
|
321
328
|
)
|
|
322
329
|
return Analysis(
|
|
323
330
|
run=run,
|
stacktrace/cli.py
CHANGED
|
@@ -18,7 +18,7 @@ from __future__ import annotations
|
|
|
18
18
|
import re
|
|
19
19
|
from datetime import UTC, datetime, timedelta
|
|
20
20
|
from pathlib import Path
|
|
21
|
-
from typing import Final
|
|
21
|
+
from typing import TYPE_CHECKING, Final
|
|
22
22
|
|
|
23
23
|
import click
|
|
24
24
|
from openaca.cli import main as openaca_cli
|
|
@@ -45,6 +45,9 @@ from .telemetry.cli import telemetry as telemetry_cmd
|
|
|
45
45
|
from .telemetry.events import remember_internal
|
|
46
46
|
from .webhook.cli import webhook as webhook_cmd
|
|
47
47
|
|
|
48
|
+
if TYPE_CHECKING:
|
|
49
|
+
from .detector.settings import RuleSettings
|
|
50
|
+
|
|
48
51
|
PASSTHROUGH: Final = ("scan", "bom", "policy")
|
|
49
52
|
|
|
50
53
|
#: Commands whose runs are never counted as `command_run` (ADR-0090).
|
|
@@ -467,6 +470,7 @@ def detect(
|
|
|
467
470
|
company_domains=parse_company_domains(company_domains)
|
|
468
471
|
if company_domains
|
|
469
472
|
else _policy_company_domains(),
|
|
473
|
+
settings=_policy_rule_settings(),
|
|
470
474
|
reasoning=reasoning,
|
|
471
475
|
budget=budget,
|
|
472
476
|
sample_budget=sample_budget,
|
|
@@ -514,6 +518,16 @@ def _policy_company_domains() -> frozenset[str] | None:
|
|
|
514
518
|
return entry.company_domains if entry is not None else None
|
|
515
519
|
|
|
516
520
|
|
|
521
|
+
def _policy_rule_settings() -> RuleSettings:
|
|
522
|
+
"""What the daemon's cached policy gives the rules that run only under an
|
|
523
|
+
entry. With no policy, those rules are Off (ADR-0228, ADR-0229)."""
|
|
524
|
+
from stacktrace.daemon.paths import RuntimePaths
|
|
525
|
+
from stacktrace.daemon.policy_inputs import stored_rule_settings
|
|
526
|
+
from stacktrace.remote.config import get_config_path
|
|
527
|
+
|
|
528
|
+
return stored_rule_settings(RuntimePaths.from_environment().state, get_config_path())
|
|
529
|
+
|
|
530
|
+
|
|
517
531
|
@click.command()
|
|
518
532
|
@click.option("--port", type=int, default=0, help="Serve on this port instead of an unused one.")
|
|
519
533
|
@click.option(
|
stacktrace/daemon/db.py
CHANGED
|
@@ -427,6 +427,7 @@ CREATE TABLE IF NOT EXISTS blocked_calls (
|
|
|
427
427
|
component_identity TEXT,
|
|
428
428
|
gate_evidence TEXT,
|
|
429
429
|
paid_at TEXT,
|
|
430
|
+
rule_params TEXT,
|
|
430
431
|
PRIMARY KEY (agent_kind, session_id, call_id, rule_id)
|
|
431
432
|
);
|
|
432
433
|
-- Each rule that a step 5 release's hook blocked a prompt for: the session,
|
|
@@ -825,6 +826,9 @@ def _grow_blocked_calls(connection: sqlite3.Connection) -> None:
|
|
|
825
826
|
# When the scan wrote the marker's finding. Until then the marker is a
|
|
826
827
|
# debt the scan owes, across restarts (unified-policy.md, "Findings").
|
|
827
828
|
("paid_at", "TEXT"),
|
|
829
|
+
# The Block entry's own parsed params, from the snapshot that decided
|
|
830
|
+
# the block (prompt-guards.md, "Off until an entry sets it").
|
|
831
|
+
("rule_params", "TEXT"),
|
|
828
832
|
):
|
|
829
833
|
if column in columns:
|
|
830
834
|
continue
|
|
@@ -1138,6 +1142,7 @@ _KNOWN_TABLE_COLUMNS: dict[str, frozenset[str]] = {
|
|
|
1138
1142
|
"component_identity",
|
|
1139
1143
|
"gate_evidence",
|
|
1140
1144
|
"paid_at",
|
|
1145
|
+
"rule_params",
|
|
1141
1146
|
}
|
|
1142
1147
|
),
|
|
1143
1148
|
"blocked_prompts": frozenset(
|
stacktrace/daemon/inline_hook.py
CHANGED
|
@@ -40,25 +40,32 @@ from collections.abc import Mapping, Sequence
|
|
|
40
40
|
from dataclasses import dataclass, replace
|
|
41
41
|
from datetime import UTC, datetime
|
|
42
42
|
from pathlib import Path
|
|
43
|
-
from typing import TYPE_CHECKING
|
|
43
|
+
from typing import TYPE_CHECKING, cast
|
|
44
44
|
|
|
45
45
|
from stacktrace import __version__
|
|
46
46
|
from stacktrace.correlate.observed import declares_remote_transport
|
|
47
47
|
from stacktrace.daemon.policy_runtime import sub_agent_session
|
|
48
48
|
from stacktrace.daemon.store import marker_from_json, marker_json
|
|
49
|
+
from stacktrace.detector.deterministic import prompt_length_detection
|
|
49
50
|
from stacktrace.detector.finding import Detection
|
|
50
51
|
from stacktrace.detector.inline import judge_pending
|
|
52
|
+
from stacktrace.detector.profanity import Profanity, compile_words
|
|
53
|
+
from stacktrace.detector.rules import BLOCKABLE, PROFANITY_RULE, PROMPT_LENGTH_RULE
|
|
54
|
+
from stacktrace.detector.settings import RuleSettings
|
|
51
55
|
from stacktrace.gateway.formats import read
|
|
52
56
|
from stacktrace.gateway.marker import (
|
|
57
|
+
PROMPT_KEY,
|
|
53
58
|
Marker,
|
|
54
59
|
denied_component,
|
|
55
60
|
gate_evidence,
|
|
61
|
+
prompt_evidence,
|
|
56
62
|
prompt_key,
|
|
57
63
|
unkeyed_key,
|
|
58
64
|
)
|
|
59
65
|
from stacktrace.gateway.session import AGENT_HEADER, SESSION_HEADER, build
|
|
60
66
|
from stacktrace.hooks.claude import inspected
|
|
61
67
|
from stacktrace.package_logger import get_logger
|
|
68
|
+
from stacktrace.prompt_text import INLINE_READ_CHARS, is_relayed
|
|
62
69
|
|
|
63
70
|
if TYPE_CHECKING:
|
|
64
71
|
from collections.abc import Callable
|
|
@@ -189,7 +196,18 @@ class PendingPrompt:
|
|
|
189
196
|
agent_id: str | None
|
|
190
197
|
prompt_id: str | None
|
|
191
198
|
project_root: str
|
|
199
|
+
#: What the judge reads: the whole prompt, or its first `INLINE_READ_CHARS`
|
|
200
|
+
#: characters for a longer one (ADR-0226).
|
|
192
201
|
text: str
|
|
202
|
+
#: Whether `text` is the whole prompt.
|
|
203
|
+
whole: bool
|
|
204
|
+
#: The whole prompt's length in characters (Unicode code points).
|
|
205
|
+
chars: int
|
|
206
|
+
#: The call key a blocked prompt's marker names it by (`prompt_key`).
|
|
207
|
+
key: str
|
|
208
|
+
#: Whether the whole prompt is one relay block, such as a subagent's
|
|
209
|
+
#: report, which the guard rules do not judge (prompt-guards.md).
|
|
210
|
+
relayed: bool
|
|
193
211
|
|
|
194
212
|
|
|
195
213
|
def pending_prompt(action: Mapping[str, object]) -> PendingPrompt:
|
|
@@ -203,11 +221,9 @@ def pending_prompt(action: Mapping[str, object]) -> PendingPrompt:
|
|
|
203
221
|
agent_id, prompt_id = action.get("agent_id"), action.get("prompt_id")
|
|
204
222
|
if not isinstance(session_id, str) or not _SESSION_ID.fullmatch(session_id):
|
|
205
223
|
raise ValueError("invalid_session")
|
|
206
|
-
if not isinstance(call, dict)
|
|
207
|
-
raise ValueError("invalid_call")
|
|
208
|
-
text = call["prompt"]
|
|
209
|
-
if not isinstance(text, str):
|
|
224
|
+
if not isinstance(call, dict):
|
|
210
225
|
raise ValueError("invalid_call") # noqa: TRY004 - one refusal type, read by its caller
|
|
226
|
+
text, whole, chars, key, relayed = _prompt_of(cast(dict[str, object], call))
|
|
211
227
|
agent_kind = action.get("agent_kind")
|
|
212
228
|
if not isinstance(agent_kind, str) or (action.get("name"), action.get("source")) != (
|
|
213
229
|
"prompt",
|
|
@@ -224,12 +240,60 @@ def pending_prompt(action: Mapping[str, object]) -> PendingPrompt:
|
|
|
224
240
|
),
|
|
225
241
|
project_root=str(action["project_root"]),
|
|
226
242
|
text=text,
|
|
243
|
+
whole=whole,
|
|
244
|
+
chars=chars,
|
|
245
|
+
key=key,
|
|
246
|
+
relayed=relayed,
|
|
227
247
|
)
|
|
228
248
|
|
|
229
249
|
|
|
230
|
-
|
|
250
|
+
#: A long prompt's digest: `prompt_digest`'s lowercase SHA-256 hex.
|
|
251
|
+
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
|
252
|
+
_HEAD_KEYS = frozenset({"prompt_head", "prompt_chars", "prompt_digest", "prompt_relayed"})
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _prompt_of(call: Mapping[str, object]) -> tuple[str, bool, int, str, bool]:
|
|
256
|
+
"""What the judge reads of `call`, whether it is the whole prompt, the
|
|
257
|
+
prompt's length, its marker key and whether it is a relay block.
|
|
258
|
+
|
|
259
|
+
`{"prompt": text}`, or, for a prompt longer than `INLINE_READ_CHARS`,
|
|
260
|
+
`{"prompt_head", "prompt_chars", "prompt_digest", "prompt_relayed"}`
|
|
261
|
+
(ADR-0226). A whole prompt longer than the limit, from a client that
|
|
262
|
+
sends one, is read to the limit as well, so no client can make the judge
|
|
263
|
+
read more.
|
|
264
|
+
"""
|
|
265
|
+
if set(call) == {"prompt"}:
|
|
266
|
+
text = call["prompt"]
|
|
267
|
+
if not isinstance(text, str):
|
|
268
|
+
raise ValueError("invalid_call")
|
|
269
|
+
whole = len(text) <= INLINE_READ_CHARS
|
|
270
|
+
return text[:INLINE_READ_CHARS], whole, len(text), prompt_key(text), is_relayed(text)
|
|
271
|
+
if set(call) == _HEAD_KEYS:
|
|
272
|
+
head, chars = call["prompt_head"], call["prompt_chars"]
|
|
273
|
+
digest, relayed = call["prompt_digest"], call["prompt_relayed"]
|
|
274
|
+
if (
|
|
275
|
+
isinstance(head, str)
|
|
276
|
+
and 0 < len(head) <= INLINE_READ_CHARS
|
|
277
|
+
and type(chars) is int
|
|
278
|
+
and chars > len(head)
|
|
279
|
+
and isinstance(digest, str)
|
|
280
|
+
and _DIGEST.fullmatch(digest)
|
|
281
|
+
and type(relayed) is bool
|
|
282
|
+
):
|
|
283
|
+
return head, False, chars, PROMPT_KEY + digest, relayed
|
|
284
|
+
raise ValueError("invalid_call")
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def judge_prompt(
|
|
288
|
+
prompt: PendingPrompt, rules: Mapping[str, Mapping[str, object]]
|
|
289
|
+
) -> tuple[Detection, ...]:
|
|
231
290
|
"""The detections of `rules` that the prompt would be, if the model received it.
|
|
232
291
|
|
|
292
|
+
The content rules read `prompt.text`, at most `INLINE_READ_CHARS`, so the
|
|
293
|
+
answer stays within the prompt's 100 ms (ADR-0226). The length rule reads
|
|
294
|
+
the whole prompt's count. Each rule judges with its Block entry's
|
|
295
|
+
parameters, from the snapshot that decides the block.
|
|
296
|
+
|
|
233
297
|
The turn's key is the prompt's own id, or a random one when it has none,
|
|
234
298
|
so two blocked prompts in one session are two events. Never a digest of
|
|
235
299
|
the text: the key is in the finding's span, which leaves the machine, and
|
|
@@ -237,6 +301,7 @@ def judge_prompt(prompt: PendingPrompt, rules: frozenset[str]) -> tuple[Detectio
|
|
|
237
301
|
"""
|
|
238
302
|
if not rules:
|
|
239
303
|
return ()
|
|
304
|
+
limit, words = _settings(rules)
|
|
240
305
|
request = {
|
|
241
306
|
"model": "stacktrace-hook",
|
|
242
307
|
"system": "",
|
|
@@ -264,10 +329,42 @@ def judge_prompt(prompt: PendingPrompt, rules: frozenset[str]) -> tuple[Detectio
|
|
|
264
329
|
if t.role == "user"
|
|
265
330
|
),
|
|
266
331
|
)
|
|
267
|
-
|
|
332
|
+
content = frozenset(rules) - {PROMPT_LENGTH_RULE}
|
|
333
|
+
found = list(
|
|
334
|
+
judge_pending(
|
|
335
|
+
session,
|
|
336
|
+
frozenset(),
|
|
337
|
+
content,
|
|
338
|
+
prompt=key,
|
|
339
|
+
# A relay block's head does not show its closing tag, so the
|
|
340
|
+
# hook's answer decides it (ADR-0226).
|
|
341
|
+
settings=RuleSettings(profanity=None if prompt.relayed else words),
|
|
342
|
+
)
|
|
343
|
+
if content
|
|
344
|
+
else ()
|
|
345
|
+
)
|
|
346
|
+
if limit is not None and not (prompt.subagent or prompt.relayed) and prompt.chars > limit:
|
|
347
|
+
found.extend(
|
|
348
|
+
prompt_length_detection(session, turn, prompt.chars, limit) for turn in session.turns
|
|
349
|
+
)
|
|
350
|
+
order = {rule: index for index, rule in enumerate(BLOCKABLE)}
|
|
351
|
+
return tuple(sorted(found, key=lambda d: order.get(d.rule_id, len(order))))
|
|
352
|
+
|
|
268
353
|
|
|
354
|
+
def _settings(rules: Mapping[str, Mapping[str, object]]) -> tuple[int | None, Profanity | None]:
|
|
355
|
+
"""The length limit and the word list of the prompt rules' Block entries,
|
|
356
|
+
from the same snapshot that decides the block; None for a rule with no
|
|
357
|
+
Block entry."""
|
|
358
|
+
limit = rules.get(PROMPT_LENGTH_RULE, {}).get("max_chars")
|
|
359
|
+
params = rules.get(PROFANITY_RULE)
|
|
360
|
+
words = None
|
|
361
|
+
if params is not None:
|
|
362
|
+
add, ignore = params.get("add", ()), params.get("ignore", ())
|
|
363
|
+
words = compile_words(cast(tuple[str, ...], add), cast(tuple[str, ...], ignore))
|
|
364
|
+
return (limit if type(limit) is int else None), words
|
|
269
365
|
|
|
270
|
-
|
|
366
|
+
|
|
367
|
+
def judge(call: PendingCall, rules: Mapping[str, Mapping[str, object]]) -> tuple[Detection, ...]:
|
|
271
368
|
"""The detections of `rules` that the call would be, if it ran."""
|
|
272
369
|
if not rules:
|
|
273
370
|
return ()
|
|
@@ -335,7 +432,7 @@ def judge(call: PendingCall, rules: frozenset[str]) -> tuple[Detection, ...]:
|
|
|
335
432
|
return judge_pending(
|
|
336
433
|
session,
|
|
337
434
|
frozenset({span}) if pending else frozenset(),
|
|
338
|
-
rules,
|
|
435
|
+
frozenset(rules),
|
|
339
436
|
declared_transport=call.declared_transport,
|
|
340
437
|
)
|
|
341
438
|
|
|
@@ -347,8 +444,11 @@ def _reader_session(agent_kind: str, session_id: str, agent_id: str | None) -> s
|
|
|
347
444
|
return session_id if agent_id is None else sub_agent_session(agent_kind, session_id, agent_id)
|
|
348
445
|
|
|
349
446
|
|
|
350
|
-
def inspect_call(
|
|
351
|
-
|
|
447
|
+
def inspect_call(
|
|
448
|
+
action: dict[str, object], rules: Mapping[str, Mapping[str, object]]
|
|
449
|
+
) -> tuple[Detection, ...]:
|
|
450
|
+
"""The policy runtime's `Inspector`: read the call or the prompt, then
|
|
451
|
+
judge it for each blocking rule, with that rule's entry's parameters."""
|
|
352
452
|
if action.get("action_kind") == "prompt":
|
|
353
453
|
return judge_prompt(pending_prompt(action), rules)
|
|
354
454
|
return judge(pending_call(action), rules)
|
|
@@ -497,11 +597,8 @@ def block_marker(
|
|
|
497
597
|
def record(
|
|
498
598
|
action: dict[str, object], detection: Detection, entry: DetectionEntry, revision: int
|
|
499
599
|
) -> None:
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if action.get("action_kind") == "prompt"
|
|
503
|
-
else pending_call(action).tool_use_id
|
|
504
|
-
)
|
|
600
|
+
prompt = pending_prompt(action) if action.get("action_kind") == "prompt" else None
|
|
601
|
+
call_key = prompt.key if prompt is not None else pending_call(action).tool_use_id
|
|
505
602
|
# The judged session's id, not the hook's: for a subagent's call it is
|
|
506
603
|
# `<session>:agent-<id>`, the session the scan reads the transcript as.
|
|
507
604
|
session = detection.session
|
|
@@ -514,9 +611,17 @@ def block_marker(
|
|
|
514
611
|
entry_id=entry.id,
|
|
515
612
|
entry_name=entry.name,
|
|
516
613
|
policy_revision=revision,
|
|
614
|
+
rule_params=entry.params,
|
|
517
615
|
outcome="blocked",
|
|
518
616
|
where="hook",
|
|
519
617
|
rules_version=__version__,
|
|
618
|
+
# A prompt's numbers, so its finding counts what was blocked
|
|
619
|
+
# when the transcript does not record the prompt.
|
|
620
|
+
gate_evidence=(
|
|
621
|
+
prompt_evidence(detection, None if prompt.whole else len(prompt.text))
|
|
622
|
+
if prompt is not None
|
|
623
|
+
else ()
|
|
624
|
+
),
|
|
520
625
|
blocked_at=clock(),
|
|
521
626
|
)
|
|
522
627
|
)
|
stacktrace/daemon/observer.py
CHANGED
|
@@ -28,7 +28,7 @@ from dataclasses import dataclass, field, replace
|
|
|
28
28
|
from datetime import UTC, datetime
|
|
29
29
|
from pathlib import Path
|
|
30
30
|
from time import monotonic
|
|
31
|
-
from typing import Any, Literal
|
|
31
|
+
from typing import Any, Literal, cast
|
|
32
32
|
|
|
33
33
|
from stacktrace.analysis import analyse_sessions
|
|
34
34
|
from stacktrace.correlate.orchestrate import (
|
|
@@ -36,10 +36,13 @@ from stacktrace.correlate.orchestrate import (
|
|
|
36
36
|
CachingAdvisoryLookup,
|
|
37
37
|
advisory_attacher,
|
|
38
38
|
)
|
|
39
|
-
from stacktrace.correlate.record import CorrelatedView
|
|
39
|
+
from stacktrace.correlate.record import CorrelatedSession, CorrelatedView
|
|
40
40
|
from stacktrace.cost.components import Placement, placement_of
|
|
41
|
+
from stacktrace.detector.profanity import compile_words
|
|
41
42
|
from stacktrace.detector.render import detection_json, unknown_json
|
|
43
|
+
from stacktrace.detector.rules import PROFANITY_RULE, PROMPT_LENGTH_RULE
|
|
42
44
|
from stacktrace.detector.run import NO_ANALYZER, run_detector
|
|
45
|
+
from stacktrace.detector.settings import NO_SETTINGS, RuleSettings
|
|
43
46
|
from stacktrace.gateway.marker import (
|
|
44
47
|
Marker,
|
|
45
48
|
as_ran,
|
|
@@ -828,6 +831,47 @@ def _asking_also(attach: Any, keys: frozenset[tuple[str, str | None]]) -> Any:
|
|
|
828
831
|
return attach_also
|
|
829
832
|
|
|
830
833
|
|
|
834
|
+
#: A hashable stand-in for the fields of `RuleSettings` a rejudge groups blocked
|
|
835
|
+
#: spans by: the limit, and the list's single words and its phrases.
|
|
836
|
+
#: `RuleSettings.profanity` holds a `Profanity`, which carries a dict field and
|
|
837
|
+
#: so is not itself hashable.
|
|
838
|
+
_RejudgeKey = tuple[int | None, tuple[frozenset[str], frozenset[str]] | None]
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
def _rejudge_settings(markers: Iterable[Marker], base: RuleSettings) -> RuleSettings:
|
|
842
|
+
"""The settings a rejudge of `markers` reads: a prompt-too-long or
|
|
843
|
+
profanity marker's own parameters, from the snapshot that decided its
|
|
844
|
+
block, never today's policy, which can hold a later revision
|
|
845
|
+
(prompt-guards.md, "What a finding counts"). A marker from an
|
|
846
|
+
older release, or of any other blockable rule, carries no parameters and
|
|
847
|
+
leaves `base`'s field as it is: no other rule reads either field.
|
|
848
|
+
"""
|
|
849
|
+
prompt_max_chars = base.prompt_max_chars
|
|
850
|
+
profanity = base.profanity
|
|
851
|
+
for marker in markers:
|
|
852
|
+
if marker.rule_params is None:
|
|
853
|
+
continue
|
|
854
|
+
if marker.rule_id == PROMPT_LENGTH_RULE:
|
|
855
|
+
limit = marker.rule_params.get("max_chars")
|
|
856
|
+
if type(limit) is int:
|
|
857
|
+
prompt_max_chars = limit
|
|
858
|
+
elif marker.rule_id == PROFANITY_RULE:
|
|
859
|
+
add = marker.rule_params.get("add", ())
|
|
860
|
+
ignore = marker.rule_params.get("ignore", ())
|
|
861
|
+
profanity = compile_words(
|
|
862
|
+
tuple(cast(tuple[str, ...], add)), tuple(cast(tuple[str, ...], ignore))
|
|
863
|
+
)
|
|
864
|
+
return RuleSettings(prompt_max_chars=prompt_max_chars, profanity=profanity)
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
def _rejudge_key(settings: RuleSettings) -> _RejudgeKey:
|
|
868
|
+
words = settings.profanity
|
|
869
|
+
return (
|
|
870
|
+
settings.prompt_max_chars,
|
|
871
|
+
None if words is None else (words.words, words.phrase_text),
|
|
872
|
+
)
|
|
873
|
+
|
|
874
|
+
|
|
831
875
|
def analyse_batch(
|
|
832
876
|
view: SessionView,
|
|
833
877
|
collected: tuple[Collected, ...],
|
|
@@ -841,6 +885,7 @@ def analyse_batch(
|
|
|
841
885
|
blocked: Callable[[tuple[SessionKey, ...]], frozenset[Marker]] | None = None,
|
|
842
886
|
gate_conditions: Mapping[str, object] | None = None,
|
|
843
887
|
company_domains: frozenset[str] | None = None,
|
|
888
|
+
settings: RuleSettings = NO_SETTINGS,
|
|
844
889
|
) -> dict[SessionKey, ScanResult]:
|
|
845
890
|
"""**One** analysis pass over every changed session, split per session.
|
|
846
891
|
|
|
@@ -902,6 +947,7 @@ def analyse_batch(
|
|
|
902
947
|
build_all=build_all,
|
|
903
948
|
gate_conditions=gate_conditions,
|
|
904
949
|
company_domains=company_domains,
|
|
950
|
+
settings=settings,
|
|
905
951
|
)
|
|
906
952
|
|
|
907
953
|
def _for(qualified_id: str) -> SessionKey | None:
|
|
@@ -943,7 +989,12 @@ def analyse_batch(
|
|
|
943
989
|
if call_id not in keyed_calls:
|
|
944
990
|
by_call.setdefault((session, call_id), []).extend(found)
|
|
945
991
|
marked: dict[SessionKey, dict[str, tuple[Marker, ...]]] = {}
|
|
946
|
-
|
|
992
|
+
# Each blocked span is judged again with its own markers' settings, so one
|
|
993
|
+
# session can be read under several (prompt-guards.md, "What a finding
|
|
994
|
+
# counts"). A group's run stamps only its own spans.
|
|
995
|
+
rejudge: dict[_RejudgeKey, list[CorrelatedSession]] = {}
|
|
996
|
+
rejudge_settings: dict[_RejudgeKey, RuleSettings] = {}
|
|
997
|
+
rejudge_spans: dict[_RejudgeKey, dict[SessionKey, frozenset[str]]] = {}
|
|
947
998
|
held = {session for session, _key in by_call}
|
|
948
999
|
if by_call:
|
|
949
1000
|
for correlated in analysis.view.sessions:
|
|
@@ -969,8 +1020,17 @@ def analyse_batch(
|
|
|
969
1020
|
continue
|
|
970
1021
|
marked[session] = spans
|
|
971
1022
|
stamping = [span for span, markers in spans.items() if any(m.stamps for m in markers)]
|
|
972
|
-
|
|
973
|
-
|
|
1023
|
+
grouped: dict[_RejudgeKey, list[str]] = {}
|
|
1024
|
+
for span in stamping:
|
|
1025
|
+
span_settings = _rejudge_settings(spans[span], settings)
|
|
1026
|
+
key = _rejudge_key(span_settings)
|
|
1027
|
+
grouped.setdefault(key, []).append(span)
|
|
1028
|
+
rejudge_settings[key] = span_settings
|
|
1029
|
+
for key, group in grouped.items():
|
|
1030
|
+
rejudge.setdefault(key, []).append(
|
|
1031
|
+
replace(correlated, session=as_ran(correlated.session, group))
|
|
1032
|
+
)
|
|
1033
|
+
rejudge_spans.setdefault(key, {})[session] = frozenset(group)
|
|
974
1034
|
# A component deny's finding is built from its marker, at the span
|
|
975
1035
|
# the transcript reader gives the call (ADR-0184).
|
|
976
1036
|
for turn in correlated.session.turns:
|
|
@@ -1013,14 +1073,22 @@ def analyse_batch(
|
|
|
1013
1073
|
if session in not_evidence and not unmarked((finding,), not_evidence[session]):
|
|
1014
1074
|
continue
|
|
1015
1075
|
findings.setdefault(session, []).append(detection_json(finding))
|
|
1016
|
-
|
|
1076
|
+
for key, sessions in rejudge.items():
|
|
1017
1077
|
# Through the same detector the scan runs, so a blocked finding names
|
|
1018
1078
|
# what a detected one names. Only the marker's own rules are kept.
|
|
1019
|
-
|
|
1079
|
+
# With the deciding prompt length limit and word list, or a blocked
|
|
1080
|
+
# long prompt is never judged again and its block is never stamped
|
|
1081
|
+
# (ADR-0228).
|
|
1082
|
+
rejudged = run_detector(
|
|
1083
|
+
CorrelatedView(sessions=tuple(sessions)), settings=rejudge_settings[key]
|
|
1084
|
+
)
|
|
1085
|
+
for finding in rejudged.detections:
|
|
1020
1086
|
session = _for(finding.session.session_id)
|
|
1021
1087
|
if session is None:
|
|
1022
1088
|
continue
|
|
1023
|
-
|
|
1089
|
+
own = rejudge_spans[key].get(session, frozenset())
|
|
1090
|
+
group = {span: m for span, m in marked.get(session, {}).items() if span in own}
|
|
1091
|
+
for detection, marker in cites((finding,), group):
|
|
1024
1092
|
written, stamp = stamped(detection, marker)
|
|
1025
1093
|
findings.setdefault(session, []).append(
|
|
1026
1094
|
{**detection_json(written), **stamp.fields()}
|
|
@@ -29,11 +29,14 @@ from stacktrace.detector.rules import (
|
|
|
29
29
|
CATALOGUE,
|
|
30
30
|
GATE_ONLY_RULES,
|
|
31
31
|
POSTURE_RULE,
|
|
32
|
+
PROFANITY_RULE,
|
|
33
|
+
PROMPT_LENGTH_RULE,
|
|
32
34
|
RULE_IDS,
|
|
33
35
|
VULNERABILITY_RULE,
|
|
34
36
|
blockable,
|
|
35
37
|
entry_params,
|
|
36
38
|
)
|
|
39
|
+
from stacktrace.detector.settings import NO_SETTINGS, RuleSettings
|
|
37
40
|
from stacktrace.remote.client import PolicyEnvelope, RemoteClient, RemoteClientError
|
|
38
41
|
from stacktrace.remote.config import (
|
|
39
42
|
ConfigError,
|
|
@@ -102,6 +105,15 @@ class DetectionEntry:
|
|
|
102
105
|
domains = self.params.get("company_domains")
|
|
103
106
|
return cast(frozenset[str], domains) if isinstance(domains, frozenset) else None
|
|
104
107
|
|
|
108
|
+
@property
|
|
109
|
+
def max_chars(self) -> int | None:
|
|
110
|
+
"""The prompt length rule's limit, for an entry that is Detect or
|
|
111
|
+
Block (ADR-0228). None for an Off entry, which leaves the rule Off."""
|
|
112
|
+
limit = self.params.get("max_chars")
|
|
113
|
+
if type(limit) is not int or (self.action == "allow" and not self.notify):
|
|
114
|
+
return None
|
|
115
|
+
return limit
|
|
116
|
+
|
|
105
117
|
|
|
106
118
|
def _invalid() -> PolicyInputError:
|
|
107
119
|
return PolicyInputError("policy_detection_rules_invalid")
|
|
@@ -674,6 +686,51 @@ def stored_detection_entry(
|
|
|
674
686
|
return None
|
|
675
687
|
|
|
676
688
|
|
|
689
|
+
def rule_settings(state: PolicyState) -> RuleSettings:
|
|
690
|
+
"""What `state`'s entries give the rules that run only under one
|
|
691
|
+
(ADR-0228, ADR-0229). An Off entry, or none, leaves its rule Off.
|
|
692
|
+
|
|
693
|
+
Both rules can block, so their entries are among the detection rules.
|
|
694
|
+
"""
|
|
695
|
+
from stacktrace.detector.profanity import compile_words
|
|
696
|
+
|
|
697
|
+
length = state.detection_entry(PROMPT_LENGTH_RULE)
|
|
698
|
+
words = state.detection_entry(PROFANITY_RULE)
|
|
699
|
+
profanity = None
|
|
700
|
+
if words is not None and (words.action == "block" or words.notify):
|
|
701
|
+
add, ignore = words.params.get("add", ()), words.params.get("ignore", ())
|
|
702
|
+
profanity = compile_words(cast(tuple[str, ...], add), cast(tuple[str, ...], ignore))
|
|
703
|
+
return RuleSettings(
|
|
704
|
+
prompt_max_chars=length.max_chars if length is not None else None,
|
|
705
|
+
profanity=profanity,
|
|
706
|
+
)
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
def stored_rule_settings(database: Path, config_path: Path) -> RuleSettings:
|
|
710
|
+
"""`rule_settings` of the enrolled tenant's stored policy, read without
|
|
711
|
+
writing, for `stacktrace detect`. Any failure leaves the rules Off."""
|
|
712
|
+
try:
|
|
713
|
+
config = load_remote_config(config_path)
|
|
714
|
+
if not config.token or not config.org_id or not database.exists():
|
|
715
|
+
return NO_SETTINGS
|
|
716
|
+
tenant = Tenant(config.api_url, config.org_id)
|
|
717
|
+
connection = connect(database, read_only=True)
|
|
718
|
+
try:
|
|
719
|
+
row = connection.execute(
|
|
720
|
+
"SELECT detection_rules_json, entries_json FROM policy_inputs "
|
|
721
|
+
"WHERE api_url = ? AND org_id = ?",
|
|
722
|
+
(tenant.api_url, tenant.org_id),
|
|
723
|
+
).fetchone()
|
|
724
|
+
finally:
|
|
725
|
+
connection.close()
|
|
726
|
+
if row is None:
|
|
727
|
+
return NO_SETTINGS
|
|
728
|
+
detection_rules, entries = _stored_entries(row[0], row[1])
|
|
729
|
+
return rule_settings(PolicyState(detection_rules=detection_rules, entries=entries))
|
|
730
|
+
except (ConfigError, OSError, PolicyInputError, ValueError, sqlite3.Error):
|
|
731
|
+
return NO_SETTINGS
|
|
732
|
+
|
|
733
|
+
|
|
677
734
|
class PolicyFetcher:
|
|
678
735
|
"""A single ingestion pass, called by the future reconciliation worker.
|
|
679
736
|
|