stacktrace-cli 0.22.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/author.py +1 -1
- stacktrace/agentspec/checks.py +222 -21
- stacktrace/agentspec/effects/__init__.py +0 -7
- stacktrace/agentspec/engine.py +5 -3
- stacktrace/agentspec/grants.py +16 -5
- stacktrace/agentspec/judge.py +32 -6
- stacktrace/agentspec/redact.py +286 -2
- stacktrace/agentspec/serve.py +6 -1
- stacktrace/agentspec/triggers.py +2 -1
- stacktrace/analysis.py +7 -0
- stacktrace/cli.py +15 -1
- stacktrace/daemon/config.py +19 -0
- stacktrace/daemon/db.py +5 -0
- stacktrace/daemon/inline_hook.py +121 -16
- stacktrace/daemon/observer.py +76 -8
- stacktrace/daemon/policy_inputs.py +70 -11
- stacktrace/daemon/policy_result.py +36 -4
- stacktrace/daemon/policy_runtime.py +53 -46
- stacktrace/daemon/presentation.py +73 -8
- stacktrace/daemon/runtime.py +5 -0
- stacktrace/daemon/store.py +30 -5
- stacktrace/daemon/usage.py +16 -3
- stacktrace/detector/deterministic.py +228 -7
- stacktrace/detector/hook_table.py +2 -0
- stacktrace/detector/inline.py +40 -16
- stacktrace/detector/personal_data.py +45 -10
- stacktrace/detector/profanity-en.txt +375 -0
- stacktrace/detector/profanity.py +234 -0
- stacktrace/detector/rules.py +141 -27
- 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 +28 -4
- stacktrace/hooks/cursor.py +5 -2
- stacktrace/hooks/devin.py +21 -5
- stacktrace/hydrant_cli.py +42 -8
- stacktrace/monitor/render.py +6 -1
- stacktrace/monitor/site/app.js +2 -0
- stacktrace/prompt_text.py +50 -0
- stacktrace/remote/cli.py +2 -1
- stacktrace/remote/sync.py +17 -1
- stacktrace/remote/sync_detect.py +8 -0
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.24.0.dist-info}/METADATA +4 -4
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.24.0.dist-info}/RECORD +49 -45
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.24.0.dist-info}/WHEEL +0 -0
- {stacktrace_cli-0.22.0.dist-info → stacktrace_cli-0.24.0.dist-info}/entry_points.txt +0 -0
stacktrace/agentspec/redact.py
CHANGED
|
@@ -14,14 +14,27 @@ the model still knows a secret was there and what kind. Four passes:
|
|
|
14
14
|
A `$NAME` reference is kept: it names a variable, not a value, and the model
|
|
15
15
|
needs it to see a secret travel.
|
|
16
16
|
|
|
17
|
+
`SessionValues` adds what one string cannot know: every value those passes
|
|
18
|
+
recognise anywhere in the session, plus personal data (`personal_data.py`),
|
|
19
|
+
each with an indexed placeholder (`<BEARER_TOKEN#1>`, `<EMAIL_ADDRESS#2>`).
|
|
20
|
+
Every view of one judged call replaces every occurrence of every known value,
|
|
21
|
+
so a token first seen in a log line and later pasted bare is still taken out,
|
|
22
|
+
and the judge can see it is the same token (ADR-0224).
|
|
23
|
+
|
|
17
24
|
`assert_clean` is the boundary check. The client calls it on every request
|
|
18
25
|
and refuses to send one that still carries a credential shape.
|
|
19
26
|
"""
|
|
20
27
|
|
|
21
28
|
from __future__ import annotations
|
|
22
29
|
|
|
30
|
+
import hashlib
|
|
31
|
+
import json
|
|
23
32
|
import re
|
|
33
|
+
import threading
|
|
34
|
+
from collections import OrderedDict
|
|
35
|
+
from collections.abc import Iterable, Iterator, Mapping
|
|
24
36
|
|
|
37
|
+
from stacktrace.detector.personal_data import personal_data_values, unescaped
|
|
25
38
|
from stacktrace.detector.secret_paths import kind_of
|
|
26
39
|
from stacktrace.detector.secrets import (
|
|
27
40
|
_PATTERNS, # pyright: ignore[reportPrivateUsage]
|
|
@@ -37,9 +50,27 @@ _ASSIGNMENT = re.compile(
|
|
|
37
50
|
r"""(?P<name>["']?[A-Za-z_][A-Za-z0-9_.-]*["']?)(?P<sep>\s*[:=]\s*)"""
|
|
38
51
|
r"""(?P<value>"[^"\n]*"|'[^'\n]*'|[^\s"',;&|)]+)"""
|
|
39
52
|
)
|
|
53
|
+
#: `_ASSIGNMENT` tried only where a name can start: the same matches over a
|
|
54
|
+
#: session's text, without re-reading every word from each of its letters.
|
|
55
|
+
_ASSIGNMENT_AT_A_NAME = re.compile(r"(?<![A-Za-z0-9_])" + _ASSIGNMENT.pattern)
|
|
40
56
|
_AUTH_HEADER = re.compile(r"(?i)(authorization\s*:\s*(?:bearer|basic|token)\s+)([^\s\"']+)")
|
|
41
57
|
_SCHEMES = frozenset({"bearer", "basic", "token"})
|
|
42
58
|
_DOTENV_LINE = re.compile(r"(?m)^(\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=\s*)(.+)$")
|
|
59
|
+
#: A credential file's line as a step shows it, perhaps numbered (`cat -n`,
|
|
60
|
+
#: `grep -n`, a reading tool).
|
|
61
|
+
_NUMBERED_DOTENV_LINE = re.compile(
|
|
62
|
+
r"(?m)^(\s*(?:\d+[\t:])?\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=\s*)(.+)$"
|
|
63
|
+
)
|
|
64
|
+
#: A placeholder, indexed or not: what a view keeps when it shortens text.
|
|
65
|
+
PLACEHOLDER = re.compile(r"<[A-Z][A-Z0-9_]*(?:#\d+)?>")
|
|
66
|
+
_INDEXED = re.compile(r"<[A-Z][A-Z0-9_]*#\d+>")
|
|
67
|
+
#: RFC 6750's b64token: every character a bearer credential can hold.
|
|
68
|
+
_TOKEN_RUN = re.compile(r"[A-Za-z0-9._~+/-]+=*")
|
|
69
|
+
#: A bearer value the writer already masked (`pl_live_[REDACTED]`, `sk-****`,
|
|
70
|
+
#: `abc…`): a few token characters, then one no token can hold.
|
|
71
|
+
_MASKED = re.compile(r"[A-Za-z0-9._~+/-]{0,15}[\[*\u2026<{]")
|
|
72
|
+
#: A JSON escape inside a string: where a value written in escaped text ends.
|
|
73
|
+
_ESCAPE = re.compile(r"\\[nrt\"]")
|
|
43
74
|
|
|
44
75
|
|
|
45
76
|
def _placeholder(kind: str) -> str:
|
|
@@ -47,8 +78,20 @@ def _placeholder(kind: str) -> str:
|
|
|
47
78
|
|
|
48
79
|
|
|
49
80
|
def _is_reference(value: str) -> bool:
|
|
81
|
+
"""A variable, a placeholder, or nothing: no value to take out. A value
|
|
82
|
+
that starts with an indexed placeholder already has been."""
|
|
50
83
|
bare = value.strip("\"'")
|
|
51
|
-
return
|
|
84
|
+
return (
|
|
85
|
+
bare.startswith("$")
|
|
86
|
+
or (bare.startswith("<") and bare.endswith(">"))
|
|
87
|
+
or not bare
|
|
88
|
+
or bool(_INDEXED.match(bare))
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _bearer(value: str) -> bool:
|
|
93
|
+
"""Whether an Authorization header's value is a credential to take out."""
|
|
94
|
+
return not _is_reference(value) and not _MASKED.match(value)
|
|
52
95
|
|
|
53
96
|
|
|
54
97
|
def _secret_name(name: str) -> bool:
|
|
@@ -65,7 +108,7 @@ def redact_text(text: str) -> str:
|
|
|
65
108
|
text = text[: found.start()] + _placeholder(kind) + text[found.end() :]
|
|
66
109
|
|
|
67
110
|
text = _AUTH_HEADER.sub(
|
|
68
|
-
lambda m: m.group(
|
|
111
|
+
lambda m: f"{m.group(1)}<BEARER_TOKEN>" if _bearer(m.group(2)) else m.group(0),
|
|
69
112
|
text,
|
|
70
113
|
)
|
|
71
114
|
|
|
@@ -101,6 +144,247 @@ def redact_value(value: object, depth: int = 0) -> object:
|
|
|
101
144
|
return value
|
|
102
145
|
|
|
103
146
|
|
|
147
|
+
# --- one session's values ----------------------------------------------------
|
|
148
|
+
|
|
149
|
+
#: Named only by the name it was assigned to, so it must look like a value
|
|
150
|
+
#: before every copy of it is taken out: not a path, not code, not a word.
|
|
151
|
+
_CODE = re.compile(r"[()\[\]{}<>\s]")
|
|
152
|
+
_PERSONAL = frozenset({"payment_card", "iban", "us_ssn", "email_address", "phone_number"})
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _recognised(text: str, path: str | None) -> Iterator[tuple[str, str]]:
|
|
156
|
+
"""(kind, value) for each value the four passes would take out of `text`,
|
|
157
|
+
then each piece of personal data, normalized."""
|
|
158
|
+
for kind, found in _claimed_matches(text, _PATTERNS):
|
|
159
|
+
yield kind, found.group(0)
|
|
160
|
+
for m in _AUTH_HEADER.finditer(text):
|
|
161
|
+
run = _TOKEN_RUN.match(m.group(2))
|
|
162
|
+
if _bearer(m.group(2)) and run:
|
|
163
|
+
# The token itself, not the escaped text after it (`TOKEN\nHost:`).
|
|
164
|
+
yield "bearer_token", run.group(0)
|
|
165
|
+
for m in _ASSIGNMENT_AT_A_NAME.finditer(text):
|
|
166
|
+
name, value = m.group("name"), m.group("value")
|
|
167
|
+
if _secret_name(name) and not _is_reference(value) and value.lower() not in _SCHEMES:
|
|
168
|
+
yield "secret", _ESCAPE.split(value.strip("\"'"), maxsplit=1)[0]
|
|
169
|
+
if path is not None and kind_of(path) is not None:
|
|
170
|
+
for m in _NUMBERED_DOTENV_LINE.finditer(text):
|
|
171
|
+
value = m.group(2).strip()
|
|
172
|
+
if not _is_reference(value):
|
|
173
|
+
bare = value[1:-1] if value[:1] in "\"'" and value[-1:] == value[:1] else None
|
|
174
|
+
yield "secret", bare if bare is not None else value.split()[0]
|
|
175
|
+
for kind, values in personal_data_values(text).items():
|
|
176
|
+
for value in sorted(values):
|
|
177
|
+
yield kind, value
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _linkable(kind: str, value: str) -> bool:
|
|
181
|
+
"""Whether every copy of `value` can be taken out without taking ordinary
|
|
182
|
+
text with it."""
|
|
183
|
+
if kind in _PERSONAL:
|
|
184
|
+
return True
|
|
185
|
+
if len(value) < 8 or _is_reference(value):
|
|
186
|
+
return False
|
|
187
|
+
if kind == "secret":
|
|
188
|
+
return (
|
|
189
|
+
not value.startswith(("/", "~", "."))
|
|
190
|
+
and not _CODE.search(value)
|
|
191
|
+
and (any(c.isdigit() for c in value) or len(value) >= 16)
|
|
192
|
+
)
|
|
193
|
+
return True
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
#: What each text recognised, by digest: a session's map is rebuilt for every
|
|
197
|
+
#: judged call, and a long session's earlier steps are the same text each time.
|
|
198
|
+
_FOUND: OrderedDict[bytes, tuple[tuple[str, str], ...]] = OrderedDict()
|
|
199
|
+
_FOUND_LIMIT = 100_000
|
|
200
|
+
_FOUND_LOCK = threading.Lock()
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _found(text: str, path: str | None) -> tuple[tuple[str, str], ...]:
|
|
204
|
+
"""The linkable (kind, value) pairs in `text`, read once per distinct text."""
|
|
205
|
+
credential_file = path is not None and kind_of(path) is not None
|
|
206
|
+
key = hashlib.blake2b(text.encode(errors="surrogatepass"), digest_size=16).digest()
|
|
207
|
+
key += b"f" if credential_file else b"t"
|
|
208
|
+
with _FOUND_LOCK:
|
|
209
|
+
hit = _FOUND.get(key)
|
|
210
|
+
if hit is not None:
|
|
211
|
+
_FOUND.move_to_end(key)
|
|
212
|
+
return hit
|
|
213
|
+
found = tuple(
|
|
214
|
+
(kind, value)
|
|
215
|
+
for kind, value in _recognised(text, path if credential_file else None)
|
|
216
|
+
if _linkable(kind, value)
|
|
217
|
+
)
|
|
218
|
+
with _FOUND_LOCK:
|
|
219
|
+
_FOUND[key] = found
|
|
220
|
+
while len(_FOUND) > _FOUND_LIMIT:
|
|
221
|
+
_FOUND.popitem(last=False)
|
|
222
|
+
return found
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _written(kind: str, value: str) -> re.Pattern[str] | None:
|
|
226
|
+
"""A pattern for `value` as text writes it, or None where text writes it
|
|
227
|
+
exactly. Personal data is found normalized, so a card or phone number
|
|
228
|
+
matches with or without its separators, and an address in any case."""
|
|
229
|
+
if kind not in ("payment_card", "iban", "phone_number", "email_address"):
|
|
230
|
+
return None
|
|
231
|
+
return re.compile(_written_source(kind, value))
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _written_source(kind: str, value: str) -> str:
|
|
235
|
+
if kind in ("payment_card", "iban"):
|
|
236
|
+
return r"(?<![0-9A-Za-z])" + "[ -]?".join(map(re.escape, value)) + r"(?![0-9A-Za-z])"
|
|
237
|
+
if kind == "phone_number":
|
|
238
|
+
# A North American number is found with its country code, and is
|
|
239
|
+
# often written without it.
|
|
240
|
+
national = len(value) == 11 and value[0] == "1"
|
|
241
|
+
lead = r"(?:\+?1[ .-]?)?" if national else r"\+?"
|
|
242
|
+
digits = value[1:] if national else value
|
|
243
|
+
return r"(?<![\w+])" + lead + r"\(?" + r"[ ().-]{0,2}".join(digits) + r"(?!\w)"
|
|
244
|
+
return "(?i:" + re.escape(value) + ")"
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _could_hold(kind: str, value: str, text: str) -> bool:
|
|
248
|
+
"""A cheap test that `text` may hold `value` as `_written` matches it."""
|
|
249
|
+
if kind == "email_address":
|
|
250
|
+
return "@" in text
|
|
251
|
+
if kind == "iban":
|
|
252
|
+
return value[:2] in text
|
|
253
|
+
return value[-2:] in text
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class SessionValues:
|
|
257
|
+
"""Every sensitive value seen in one session, each with a typed, indexed
|
|
258
|
+
placeholder, numbered per kind in the order the session first showed it.
|
|
259
|
+
|
|
260
|
+
Built for one judged call and used by every view of it, so one value has
|
|
261
|
+
one placeholder across them all. Holds values only in memory: nothing here
|
|
262
|
+
is written, logged or put in a finding.
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
def __init__(self) -> None:
|
|
266
|
+
self._tags: dict[str, str] = {}
|
|
267
|
+
self._kinds: dict[str, str] = {}
|
|
268
|
+
self._per_kind: dict[str, int] = {}
|
|
269
|
+
self._order: list[tuple[str, str, str, re.Pattern[str] | None]] | None = None
|
|
270
|
+
|
|
271
|
+
def __len__(self) -> int:
|
|
272
|
+
return len(self._tags)
|
|
273
|
+
|
|
274
|
+
def learn(self, text: str, path: str | None = None) -> None:
|
|
275
|
+
"""Every value redaction recognises in `text`. `path` is the file the
|
|
276
|
+
text came from, when it did: a credential file loses every value."""
|
|
277
|
+
if not text:
|
|
278
|
+
return
|
|
279
|
+
for kind, value in _found(text, path):
|
|
280
|
+
if value in self._tags:
|
|
281
|
+
continue
|
|
282
|
+
n = self._per_kind[kind] = self._per_kind.get(kind, 0) + 1
|
|
283
|
+
self._tags[value] = f"<{kind.upper()}#{n}>"
|
|
284
|
+
self._kinds[value] = kind
|
|
285
|
+
self._order = None
|
|
286
|
+
|
|
287
|
+
def learn_value(self, value: object, path: str | None = None, depth: int = 0) -> None:
|
|
288
|
+
"""Every string in a JSON-shaped value, read as `learn` reads text, and
|
|
289
|
+
the value serialized whole: `{"api_key": "..."}` names its secret only
|
|
290
|
+
through its key, which a leaf alone does not carry."""
|
|
291
|
+
if depth > 24:
|
|
292
|
+
return
|
|
293
|
+
if depth == 0 and isinstance(value, (dict, list, tuple)):
|
|
294
|
+
self.learn(json.dumps(value, default=str), path)
|
|
295
|
+
if isinstance(value, str):
|
|
296
|
+
self.learn(value, path)
|
|
297
|
+
elif isinstance(value, dict):
|
|
298
|
+
# A key is text too: a mapping keyed by an address holds that address.
|
|
299
|
+
for key, item in value.items(): # pyright: ignore[reportUnknownVariableType]
|
|
300
|
+
if isinstance(key, str):
|
|
301
|
+
self.learn(key, path)
|
|
302
|
+
self.learn_value(item, path, depth + 1)
|
|
303
|
+
elif isinstance(value, (list, tuple)):
|
|
304
|
+
for item in value: # pyright: ignore[reportUnknownVariableType]
|
|
305
|
+
self.learn_value(item, path, depth + 1)
|
|
306
|
+
|
|
307
|
+
def _entries(self) -> list[tuple[str, str, str, re.Pattern[str] | None]]:
|
|
308
|
+
"""(value, kind, placeholder, pattern), longest value first: where one
|
|
309
|
+
value holds another, the whole is taken. One pass per value, each a
|
|
310
|
+
substring test first: a single pattern of every value is far slower."""
|
|
311
|
+
if self._order is None:
|
|
312
|
+
self._order = [
|
|
313
|
+
(v, self._kinds[v], self._tags[v], _written(self._kinds[v], v))
|
|
314
|
+
for v in sorted(self._tags, key=len, reverse=True)
|
|
315
|
+
]
|
|
316
|
+
return self._order
|
|
317
|
+
|
|
318
|
+
def replace(self, text: str) -> str:
|
|
319
|
+
"""`text` with every known value replaced by its placeholder. Learning
|
|
320
|
+
reads text with JSON's escapes undone; where a value still hides behind
|
|
321
|
+
an escape (`ana\\u0040acme.io`), the text is replaced in that form."""
|
|
322
|
+
if not text:
|
|
323
|
+
return text
|
|
324
|
+
out = self._replace(text)
|
|
325
|
+
if "\\" in out and self.kinds_in([out]):
|
|
326
|
+
out = self._replace(unescaped(out))
|
|
327
|
+
return out
|
|
328
|
+
|
|
329
|
+
def _replace(self, text: str) -> str:
|
|
330
|
+
for value, kind, tag, pattern in self._entries():
|
|
331
|
+
if pattern is None:
|
|
332
|
+
if value in text:
|
|
333
|
+
text = text.replace(value, tag)
|
|
334
|
+
elif _could_hold(kind, value, text):
|
|
335
|
+
text = pattern.sub(tag, text)
|
|
336
|
+
return text
|
|
337
|
+
|
|
338
|
+
def redact(self, text: str) -> str:
|
|
339
|
+
"""Known values first, so each keeps its index, then `redact_text`."""
|
|
340
|
+
return redact_text(self.replace(text))
|
|
341
|
+
|
|
342
|
+
def redact_file(self, path: str, text: str) -> str:
|
|
343
|
+
return redact_file(path, self.replace(text))
|
|
344
|
+
|
|
345
|
+
def redact_value(self, value: object, depth: int = 0) -> object:
|
|
346
|
+
"""A JSON-shaped value with every string in it redacted."""
|
|
347
|
+
if depth > 24:
|
|
348
|
+
return "<TOO_DEEP>"
|
|
349
|
+
if isinstance(value, str):
|
|
350
|
+
return self.redact(value)
|
|
351
|
+
if isinstance(value, dict):
|
|
352
|
+
out: dict[str, object] = {}
|
|
353
|
+
for k, v in value.items(): # pyright: ignore[reportUnknownVariableType]
|
|
354
|
+
key = self.redact(str(k)) # pyright: ignore[reportUnknownArgumentType]
|
|
355
|
+
# Two spellings of one value redact to one key; keep both entries.
|
|
356
|
+
n = 2
|
|
357
|
+
while key in out:
|
|
358
|
+
key, n = f"{self.redact(str(k))} ({n})", n + 1 # pyright: ignore[reportUnknownArgumentType]
|
|
359
|
+
out[key] = self.redact_value(v, depth + 1)
|
|
360
|
+
return out
|
|
361
|
+
if isinstance(value, (list, tuple)):
|
|
362
|
+
return [self.redact_value(v, depth + 1) for v in value] # pyright: ignore[reportUnknownVariableType]
|
|
363
|
+
return value
|
|
364
|
+
|
|
365
|
+
def guard(self, state: Mapping[str, object]) -> dict[str, object]:
|
|
366
|
+
"""A judge request's state as it may leave: every value in it learned,
|
|
367
|
+
then every string and key redacted. The one boundary every request to
|
|
368
|
+
the judge passes (`judge.JevJudge.ask`), whatever built the state."""
|
|
369
|
+
self.learn_value(dict(state))
|
|
370
|
+
out = self.redact_value(dict(state))
|
|
371
|
+
assert isinstance(out, dict)
|
|
372
|
+
return {str(k): v for k, v in out.items()} # pyright: ignore[reportUnknownVariableType]
|
|
373
|
+
|
|
374
|
+
def kinds_in(self, texts: Iterable[str]) -> list[str]:
|
|
375
|
+
"""The kinds of known values still in `texts`, raw. Never a value."""
|
|
376
|
+
found: set[str] = set()
|
|
377
|
+
for text in map(unescaped, texts):
|
|
378
|
+
for value, kind, _, pattern in self._entries():
|
|
379
|
+
if pattern is None:
|
|
380
|
+
hit = value in text
|
|
381
|
+
else:
|
|
382
|
+
hit = _could_hold(kind, value, text) and pattern.search(text) is not None
|
|
383
|
+
if hit:
|
|
384
|
+
found.add(kind)
|
|
385
|
+
return sorted(found)
|
|
386
|
+
|
|
387
|
+
|
|
104
388
|
class SecretLeak(ValueError):
|
|
105
389
|
"""A request still carried a credential shape after redaction."""
|
|
106
390
|
|
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/agentspec/triggers.py
CHANGED
|
@@ -199,7 +199,8 @@ _CONTEXT = (
|
|
|
199
199
|
"`call` is one tool call an AI coding agent is about to make. `recent_steps` are the "
|
|
200
200
|
"calls before it with short results, `person_said` is what the person who owns the agent "
|
|
201
201
|
"typed, and `project_config` holds small config and readme files beside what the call "
|
|
202
|
-
"touches. Secret values appear as placeholders such as
|
|
202
|
+
"touches. Secret values and personal data appear as placeholders such as "
|
|
203
|
+
"<GITHUB_TOKEN#1>; one <KIND#n> marks the same value wherever it appears. "
|
|
203
204
|
)
|
|
204
205
|
|
|
205
206
|
|
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/config.py
CHANGED
|
@@ -42,6 +42,10 @@ SETTINGS: tuple[tuple[str, str, str], ...] = (
|
|
|
42
42
|
# Environment only: the detector reads the same variable wherever it runs,
|
|
43
43
|
# so the daemon, a detached `start` and `guard serve` cannot disagree.
|
|
44
44
|
("company_domains", "STACKTRACE_COMPANY_DOMAINS", ""),
|
|
45
|
+
# ADR-0220: where the enforced policy comes from. `local` never fetches
|
|
46
|
+
# the Fleet org's policy and enforces only the custom rules file; findings
|
|
47
|
+
# still upload. Environment only, as the engine's own switch is.
|
|
48
|
+
("policy_source", "STACKTRACE_POLICY_SOURCE", "fleet"),
|
|
45
49
|
)
|
|
46
50
|
|
|
47
51
|
_UNITS: dict[str, int] = {"s": 1, "m": 60, "h": 3600, "d": 86400}
|
|
@@ -157,6 +161,14 @@ def parse_gateway_mode(value: str) -> str:
|
|
|
157
161
|
return value
|
|
158
162
|
|
|
159
163
|
|
|
164
|
+
def parse_policy_source(value: str) -> str:
|
|
165
|
+
# Refused, never read as `fleet`: a mistyped `local` would otherwise
|
|
166
|
+
# enforce the org's policy on a machine told to ignore it.
|
|
167
|
+
if value not in ("fleet", "local"):
|
|
168
|
+
raise ValueError(f"policy source must be 'fleet' or 'local', not {value!r}")
|
|
169
|
+
return value
|
|
170
|
+
|
|
171
|
+
|
|
160
172
|
@dataclass(frozen=True)
|
|
161
173
|
class Setting:
|
|
162
174
|
"""One resolved value, the text it was resolved from, and where that came from."""
|
|
@@ -180,6 +192,7 @@ class DaemonConfig:
|
|
|
180
192
|
gateway_listen: Setting
|
|
181
193
|
gateway_mode: Setting
|
|
182
194
|
company_domains: Setting
|
|
195
|
+
policy_source: Setting
|
|
183
196
|
|
|
184
197
|
@property
|
|
185
198
|
def scan_interval_seconds(self) -> float:
|
|
@@ -212,6 +225,10 @@ class DaemonConfig:
|
|
|
212
225
|
def reasoning_enabled(self) -> bool:
|
|
213
226
|
return parse_reasoning(self.reasoning.value)
|
|
214
227
|
|
|
228
|
+
@property
|
|
229
|
+
def local_policy(self) -> bool:
|
|
230
|
+
return parse_policy_source(self.policy_source.value) == "local"
|
|
231
|
+
|
|
215
232
|
@property
|
|
216
233
|
def company_domain_set(self) -> frozenset[str]:
|
|
217
234
|
return parse_company_domains(self.company_domains.value)
|
|
@@ -295,6 +312,7 @@ class DaemonConfig:
|
|
|
295
312
|
"gateway_listen": None,
|
|
296
313
|
"gateway_mode": None,
|
|
297
314
|
"company_domains": None,
|
|
315
|
+
"policy_source": None,
|
|
298
316
|
}
|
|
299
317
|
resolved: dict[str, Setting] = {}
|
|
300
318
|
for name, variable, default in SETTINGS:
|
|
@@ -324,6 +342,7 @@ class DaemonConfig:
|
|
|
324
342
|
parse_listen(config.gateway_listen.value),
|
|
325
343
|
parse_gateway_mode(config.gateway_mode.value),
|
|
326
344
|
config.company_domain_set,
|
|
345
|
+
config.local_policy,
|
|
327
346
|
)
|
|
328
347
|
if not config.kinds:
|
|
329
348
|
raise ValueError("--agent-kind must name at least one agent kind")
|
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(
|