stacktrace-cli 0.18.1__py3-none-any.whl → 0.19.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/analysis.py +9 -2
- stacktrace/correlate/acquire.py +59 -64
- stacktrace/correlate/composition.py +13 -0
- stacktrace/correlate/orchestrate.py +11 -4
- stacktrace/daemon/db.py +149 -19
- stacktrace/daemon/gateway.py +1 -4
- stacktrace/daemon/gateway_policy.py +6 -1
- stacktrace/daemon/inline_hook.py +350 -93
- stacktrace/daemon/observer.py +306 -43
- stacktrace/daemon/policy_collect.py +68 -17
- stacktrace/daemon/policy_inputs.py +2 -1
- stacktrace/daemon/policy_runtime.py +217 -67
- stacktrace/daemon/runtime.py +28 -17
- stacktrace/daemon/scan.py +11 -0
- stacktrace/daemon/server.py +8 -0
- stacktrace/daemon/store.py +212 -28
- stacktrace/detector/deterministic.py +157 -11
- stacktrace/detector/inline.py +41 -7
- stacktrace/detector/rules.py +51 -11
- stacktrace/detector/run.py +15 -3
- stacktrace/gateway/analyzer.py +151 -53
- stacktrace/gateway/marker.py +729 -0
- stacktrace/gateway/service.py +13 -2
- stacktrace/gateway/sink.py +9 -0
- stacktrace/hooks/claude.py +41 -9
- stacktrace/hooks/codex.py +48 -5
- stacktrace/hooks/cursor.py +21 -3
- stacktrace/hooks/devin.py +125 -0
- stacktrace/remote/detect_payload.py +8 -2
- stacktrace/remote/hook_policy.py +21 -1
- {stacktrace_cli-0.18.1.dist-info → stacktrace_cli-0.19.0.dist-info}/METADATA +4 -4
- {stacktrace_cli-0.18.1.dist-info → stacktrace_cli-0.19.0.dist-info}/RECORD +35 -33
- {stacktrace_cli-0.18.1.dist-info → stacktrace_cli-0.19.0.dist-info}/WHEEL +0 -0
- {stacktrace_cli-0.18.1.dist-info → stacktrace_cli-0.19.0.dist-info}/entry_points.txt +0 -0
stacktrace/__init__.py
CHANGED
stacktrace/analysis.py
CHANGED
|
@@ -20,7 +20,7 @@ path's tests already pin.
|
|
|
20
20
|
from __future__ import annotations
|
|
21
21
|
|
|
22
22
|
import re
|
|
23
|
-
from collections.abc import Iterator
|
|
23
|
+
from collections.abc import Iterator, Mapping
|
|
24
24
|
from dataclasses import dataclass, replace
|
|
25
25
|
from datetime import UTC, datetime, timedelta
|
|
26
26
|
from pathlib import Path
|
|
@@ -245,8 +245,12 @@ def analyse_sessions(
|
|
|
245
245
|
analyzer: str = NO_ANALYZER,
|
|
246
246
|
jev_key: str | None = None,
|
|
247
247
|
history: DriftHistory | None = None,
|
|
248
|
+
gate_conditions: Mapping[str, object] | None = None,
|
|
248
249
|
) -> Analysis:
|
|
249
|
-
"""Correlate and judge a session view already collected by a long-lived reader.
|
|
250
|
+
"""Correlate and judge a session view already collected by a long-lived reader.
|
|
251
|
+
|
|
252
|
+
`gate_conditions` travels to `run_detector` unchanged.
|
|
253
|
+
"""
|
|
250
254
|
window_end = datetime.now(UTC)
|
|
251
255
|
window_start = since if isinstance(since, datetime) else parse_since(since)
|
|
252
256
|
return _analyse_sessions(
|
|
@@ -264,6 +268,7 @@ def analyse_sessions(
|
|
|
264
268
|
analyzer=analyzer,
|
|
265
269
|
jev_key=jev_key,
|
|
266
270
|
history=history,
|
|
271
|
+
gate_conditions=gate_conditions,
|
|
267
272
|
)
|
|
268
273
|
|
|
269
274
|
|
|
@@ -283,6 +288,7 @@ def _analyse_sessions(
|
|
|
283
288
|
analyzer: str,
|
|
284
289
|
jev_key: str | None,
|
|
285
290
|
history: DriftHistory | None,
|
|
291
|
+
gate_conditions: Mapping[str, object] | None = None,
|
|
286
292
|
) -> Analysis:
|
|
287
293
|
acquired = _acquire(
|
|
288
294
|
sessions,
|
|
@@ -300,6 +306,7 @@ def _analyse_sessions(
|
|
|
300
306
|
analyzer=analyzer,
|
|
301
307
|
jev_key=jev_key,
|
|
302
308
|
history=history,
|
|
309
|
+
gate_conditions=gate_conditions,
|
|
303
310
|
)
|
|
304
311
|
return Analysis(
|
|
305
312
|
run=run,
|
stacktrace/correlate/acquire.py
CHANGED
|
@@ -25,7 +25,7 @@ import sys
|
|
|
25
25
|
import tempfile
|
|
26
26
|
from collections.abc import Callable, Sequence
|
|
27
27
|
from collections.abc import Set as AbstractSet
|
|
28
|
-
from dataclasses import dataclass
|
|
28
|
+
from dataclasses import dataclass, replace
|
|
29
29
|
from datetime import UTC, datetime
|
|
30
30
|
from importlib.metadata import (
|
|
31
31
|
Distribution,
|
|
@@ -36,9 +36,12 @@ from importlib.metadata import (
|
|
|
36
36
|
from pathlib import Path
|
|
37
37
|
from typing import Any
|
|
38
38
|
|
|
39
|
+
from openaca.core import collect_installed_agents
|
|
40
|
+
|
|
39
41
|
from stacktrace.correlate.composition import (
|
|
40
42
|
Advisory,
|
|
41
43
|
Composition,
|
|
44
|
+
Posture,
|
|
42
45
|
composable,
|
|
43
46
|
composition_from_bom,
|
|
44
47
|
reject_malformed_shape,
|
|
@@ -242,71 +245,38 @@ def build_bom(
|
|
|
242
245
|
"""
|
|
243
246
|
if not composable(agent_kind):
|
|
244
247
|
return None
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
"
|
|
254
|
-
str(directory),
|
|
255
|
-
]
|
|
256
|
-
if project is not None:
|
|
257
|
-
argv += ["--project", str(project)]
|
|
258
|
-
result = subprocess.run(
|
|
259
|
-
argv,
|
|
260
|
-
capture_output=True,
|
|
261
|
-
text=True,
|
|
262
|
-
check=False,
|
|
248
|
+
try:
|
|
249
|
+
agents = collect_installed_agents(project=project, kind_id=agent_kind)
|
|
250
|
+
except Exception as error:
|
|
251
|
+
raise RuntimeError(f"OpenACA composition acquisition failed: {error}") from error
|
|
252
|
+
if not agents:
|
|
253
|
+
return None
|
|
254
|
+
if len(agents) != 1:
|
|
255
|
+
raise ValueError(
|
|
256
|
+
f"OpenACA produced {len(agents)} BOMs for kind {agent_kind!r}; one kind selects one composition"
|
|
263
257
|
)
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if not documents:
|
|
278
|
-
return None
|
|
279
|
-
if len(documents) > 1:
|
|
280
|
-
raise ValueError(
|
|
281
|
-
f"openaca produced {len(documents)} BOMs for kind {agent_kind!r}; "
|
|
282
|
-
"one kind selects one composition, and choosing between instances "
|
|
283
|
-
"would invent one"
|
|
284
|
-
)
|
|
285
|
-
|
|
286
|
-
document = json.loads(documents[0].read_text(encoding="utf-8"))
|
|
287
|
-
# Same rule as `load_bom`, for the same reason. This document came from
|
|
288
|
-
# `openaca` rather than from a user, but a version skew that changed a
|
|
289
|
-
# container's type would still reach `_declared_kind` first, and the
|
|
290
|
-
# boundary is *every* entry point that reads a raw field.
|
|
291
|
-
reject_malformed_shape(document)
|
|
292
|
-
declared = _declared_kind(document)
|
|
293
|
-
if declared != agent_kind:
|
|
294
|
-
raise ValueError(
|
|
295
|
-
f"requested kind {agent_kind!r} but the BOM declares {declared!r}; "
|
|
296
|
-
"caching it under the requested kind would resolve sessions "
|
|
297
|
-
"against another agent's composition"
|
|
258
|
+
agent = agents[0]
|
|
259
|
+
document = agent.bom
|
|
260
|
+
reject_malformed_shape(document)
|
|
261
|
+
declared = _declared_kind(document)
|
|
262
|
+
if declared != agent_kind:
|
|
263
|
+
raise ValueError(f"requested kind {agent_kind!r} but the BOM declares {declared!r}")
|
|
264
|
+
composition = composition_from_bom(document, agent_kind, clock(), True)
|
|
265
|
+
posture: dict[str, list[Posture]] = {}
|
|
266
|
+
refs = {component.bom_ref for component in composition.components}
|
|
267
|
+
for finding in agent.posture_findings:
|
|
268
|
+
if finding.bom_ref in refs:
|
|
269
|
+
posture.setdefault(finding.bom_ref, []).append(
|
|
270
|
+
Posture(finding.rule_id, finding.severity)
|
|
298
271
|
)
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
composition=composition_from_bom(document, agent_kind, clock(), True),
|
|
308
|
-
document=document,
|
|
309
|
-
)
|
|
272
|
+
return Built(
|
|
273
|
+
composition=replace(
|
|
274
|
+
composition,
|
|
275
|
+
posture={ref: tuple(found) for ref, found in posture.items()},
|
|
276
|
+
posture_checked=True,
|
|
277
|
+
),
|
|
278
|
+
document=document,
|
|
279
|
+
)
|
|
310
280
|
|
|
311
281
|
|
|
312
282
|
@dataclass(frozen=True)
|
|
@@ -504,6 +474,9 @@ def _scan(
|
|
|
504
474
|
return None
|
|
505
475
|
if not all(_text_or_absent(value) for value in (fixed_in, source, severity, title)):
|
|
506
476
|
return None
|
|
477
|
+
aliases = _aliases(finding.get("matched_advisory"))
|
|
478
|
+
if aliases is None:
|
|
479
|
+
return None
|
|
507
480
|
key = key_by_ref.get(ref)
|
|
508
481
|
if key is None:
|
|
509
482
|
# Not malformed: we sent the components, and a ref we do not hold is
|
|
@@ -517,11 +490,33 @@ def _scan(
|
|
|
517
490
|
title=title or "",
|
|
518
491
|
fixed_in=fixed_in,
|
|
519
492
|
source=source,
|
|
493
|
+
aliases=aliases,
|
|
520
494
|
)
|
|
521
495
|
)
|
|
522
496
|
return {key: tuple(items) for key, items in matched.items()}
|
|
523
497
|
|
|
524
498
|
|
|
499
|
+
def _aliases(matched: object) -> tuple[str, ...] | None:
|
|
500
|
+
"""`matched_advisory.aliases` as a tuple, `()` when absent, or None when
|
|
501
|
+
the response is not the shape OpenACA writes.
|
|
502
|
+
|
|
503
|
+
OpenACA's own vulnerability gate matches a listed id against the advisory's
|
|
504
|
+
id and its aliases (`tools/overlays.py`, `id_set`). Detect filters by the
|
|
505
|
+
same ids, so it reads the same aliases (unified-policy.md, "Detect with a
|
|
506
|
+
condition").
|
|
507
|
+
"""
|
|
508
|
+
if matched is None:
|
|
509
|
+
return ()
|
|
510
|
+
if not isinstance(matched, dict):
|
|
511
|
+
return None
|
|
512
|
+
aliases = matched.get("aliases")
|
|
513
|
+
if aliases is None:
|
|
514
|
+
return ()
|
|
515
|
+
if not isinstance(aliases, list) or not all(isinstance(a, str) for a in aliases):
|
|
516
|
+
return None
|
|
517
|
+
return tuple(aliases)
|
|
518
|
+
|
|
519
|
+
|
|
525
520
|
def _text_or_absent(value: object) -> bool:
|
|
526
521
|
"""Whether a field arrived as the `str | None` `Advisory` declares it.
|
|
527
522
|
|
|
@@ -98,6 +98,17 @@ class Advisory:
|
|
|
98
98
|
title: str
|
|
99
99
|
fixed_in: str | None
|
|
100
100
|
source: str | None
|
|
101
|
+
#: The advisory's other ids, such as a GHSA advisory's CVE, as OpenACA's
|
|
102
|
+
#: `matched_advisory.aliases` lists them. A gate's `ids` match any of them.
|
|
103
|
+
aliases: tuple[str, ...] = ()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass(frozen=True)
|
|
107
|
+
class Posture:
|
|
108
|
+
"""A posture check OpenACA matched to a component in the scanned BOM."""
|
|
109
|
+
|
|
110
|
+
rule_id: str
|
|
111
|
+
severity: str
|
|
101
112
|
|
|
102
113
|
|
|
103
114
|
@dataclass(frozen=True)
|
|
@@ -127,6 +138,8 @@ class Composition:
|
|
|
127
138
|
#: Whether advisory matching ran at all. An unchecked composition must never
|
|
128
139
|
#: read as a clean one.
|
|
129
140
|
advisories_checked: bool = False
|
|
141
|
+
posture: dict[str, tuple[Posture, ...]] = field(default_factory=dict)
|
|
142
|
+
posture_checked: bool = False
|
|
130
143
|
#: The directory a session named, when this composition was built somewhere
|
|
131
144
|
#: else because that directory no longer exists. None for the ordinary case,
|
|
132
145
|
#: where the scanned location is the one the session recorded.
|
|
@@ -181,7 +181,7 @@ AdvisoryLookup = Callable[
|
|
|
181
181
|
["Sequence[dict[str, Any]]", "AbstractSet[AdvisoryKey]"],
|
|
182
182
|
"dict[AdvisoryKey, tuple[Advisory, ...]] | None",
|
|
183
183
|
]
|
|
184
|
-
AttachAdvisories = Callable[
|
|
184
|
+
AttachAdvisories = Callable[..., CorrelatedView]
|
|
185
185
|
|
|
186
186
|
|
|
187
187
|
class CachingAdvisoryLookup:
|
|
@@ -234,8 +234,12 @@ def advisory_attacher(lookup: AdvisoryLookup = advisories_for) -> AttachAdvisori
|
|
|
234
234
|
that reasoning would be a second chance to get it wrong.
|
|
235
235
|
"""
|
|
236
236
|
|
|
237
|
-
def attach(
|
|
238
|
-
|
|
237
|
+
def attach(
|
|
238
|
+
correlated: CorrelatedView,
|
|
239
|
+
builts: Sequence[Built],
|
|
240
|
+
also: AbstractSet[AdvisoryKey] = frozenset(),
|
|
241
|
+
) -> CorrelatedView:
|
|
242
|
+
return _attach_advisories(correlated, builts, lookup=lookup, also=also)
|
|
239
243
|
|
|
240
244
|
return attach
|
|
241
245
|
|
|
@@ -245,6 +249,7 @@ def _attach_advisories(
|
|
|
245
249
|
builts: Sequence[Built],
|
|
246
250
|
*,
|
|
247
251
|
lookup: AdvisoryLookup = advisories_for,
|
|
252
|
+
also: AbstractSet[AdvisoryKey] = frozenset(),
|
|
248
253
|
) -> CorrelatedView:
|
|
249
254
|
"""Match advisories once, over what ran, and hang them on every composition.
|
|
250
255
|
|
|
@@ -267,7 +272,9 @@ def _attach_advisories(
|
|
|
267
272
|
session independently would give two sessions that shared one composition
|
|
268
273
|
two different enriched objects and break that dedup silently.
|
|
269
274
|
"""
|
|
270
|
-
|
|
275
|
+
# `also` adds what a caller needs beyond what ran: the component of a
|
|
276
|
+
# component deny's marker, which did not run (ADR-0184).
|
|
277
|
+
keys = _invoked_keys(correlated) | set(also)
|
|
271
278
|
matched = lookup([b.document for b in builts], keys)
|
|
272
279
|
checked = matched is not None
|
|
273
280
|
by_key = matched or {}
|
stacktrace/daemon/db.py
CHANGED
|
@@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS policy_inputs (
|
|
|
82
82
|
whole_envelope_metadata_json TEXT,
|
|
83
83
|
-- The organization's policy entries (unified-policy.md), when Fleet serves them.
|
|
84
84
|
entries_json TEXT,
|
|
85
|
+
runtime_mcp_decisions_json TEXT,
|
|
85
86
|
PRIMARY KEY (api_url, org_id)
|
|
86
87
|
);
|
|
87
88
|
|
|
@@ -390,18 +391,50 @@ CREATE TABLE IF NOT EXISTS account_cards (
|
|
|
390
391
|
name_from TEXT,
|
|
391
392
|
PRIMARY KEY (agent_kind, account)
|
|
392
393
|
);
|
|
393
|
-
--
|
|
394
|
-
--
|
|
395
|
-
--
|
|
394
|
+
-- The marker of each rule a hook blocked a call or a prompt for, by the
|
|
395
|
+
-- agent's own call id, or by the prompt key for a prompt. When the scan reads
|
|
396
|
+
-- the transcript, it writes the finding from the marker and drops every other
|
|
397
|
+
-- rule's finding on the call or the prompt (`docs/specs/unified-policy.md`,
|
|
398
|
+
-- "Findings"). A row with no `outcome` is from a release whose hook wrote the
|
|
399
|
+
-- finding itself.
|
|
396
400
|
-- Additive, so `SCHEMA_VERSION` does not move (ADR-0057).
|
|
397
401
|
CREATE TABLE IF NOT EXISTS blocked_calls (
|
|
398
|
-
agent_kind
|
|
399
|
-
session_id
|
|
400
|
-
call_id
|
|
401
|
-
rule_id
|
|
402
|
-
blocked_at
|
|
402
|
+
agent_kind TEXT NOT NULL,
|
|
403
|
+
session_id TEXT NOT NULL,
|
|
404
|
+
call_id TEXT NOT NULL,
|
|
405
|
+
rule_id TEXT NOT NULL,
|
|
406
|
+
blocked_at TEXT NOT NULL,
|
|
407
|
+
entry_id TEXT,
|
|
408
|
+
entry_name TEXT,
|
|
409
|
+
policy_revision INTEGER,
|
|
410
|
+
outcome TEXT,
|
|
411
|
+
decided_where TEXT,
|
|
412
|
+
rules_version TEXT,
|
|
413
|
+
component_kind TEXT,
|
|
414
|
+
component_name TEXT,
|
|
415
|
+
component_version TEXT,
|
|
416
|
+
component_identity TEXT,
|
|
417
|
+
gate_evidence TEXT,
|
|
418
|
+
paid_at TEXT,
|
|
403
419
|
PRIMARY KEY (agent_kind, session_id, call_id, rule_id)
|
|
404
420
|
);
|
|
421
|
+
-- Each rule that a step 5 release's hook blocked a prompt for: the session,
|
|
422
|
+
-- Claude Code's `prompt_id` (empty when the hook had none), and the SHA-256 of
|
|
423
|
+
-- the text. That release wrote the prompt's finding at the hook. This build
|
|
424
|
+
-- writes no row here: a blocked prompt keeps a marker in `blocked_calls`.
|
|
425
|
+
-- `_carry_blocked_prompts` copies each row there, and the sweep removes it.
|
|
426
|
+
-- Kept, not dropped, so that a step 5 daemon still running during an upgrade
|
|
427
|
+
-- can read and write it.
|
|
428
|
+
-- Additive, so `SCHEMA_VERSION` does not move (ADR-0057).
|
|
429
|
+
CREATE TABLE IF NOT EXISTS blocked_prompts (
|
|
430
|
+
agent_kind TEXT NOT NULL,
|
|
431
|
+
session_id TEXT NOT NULL,
|
|
432
|
+
prompt_id TEXT NOT NULL,
|
|
433
|
+
text_sha256 TEXT NOT NULL,
|
|
434
|
+
rule_id TEXT NOT NULL,
|
|
435
|
+
blocked_at TEXT NOT NULL,
|
|
436
|
+
PRIMARY KEY (agent_kind, session_id, prompt_id, text_sha256, rule_id)
|
|
437
|
+
);
|
|
405
438
|
"""
|
|
406
439
|
|
|
407
440
|
|
|
@@ -583,6 +616,8 @@ def connect(path: Path, *, read_only: bool = False) -> sqlite3.Connection:
|
|
|
583
616
|
_grow_usage_runs(connection)
|
|
584
617
|
_grow_usage_buckets(connection)
|
|
585
618
|
_grow_usage_responses(connection)
|
|
619
|
+
_grow_blocked_calls(connection)
|
|
620
|
+
_carry_blocked_prompts(connection)
|
|
586
621
|
_reclaim_usage_responses(connection)
|
|
587
622
|
_name_the_store(connection)
|
|
588
623
|
if stored_version is not None and stored_version < SCHEMA_VERSION:
|
|
@@ -729,6 +764,7 @@ def _grow_policy_inputs(connection: sqlite3.Connection) -> None:
|
|
|
729
764
|
"whole_envelope_digest",
|
|
730
765
|
"whole_envelope_metadata_json",
|
|
731
766
|
"entries_json",
|
|
767
|
+
"runtime_mcp_decisions_json",
|
|
732
768
|
):
|
|
733
769
|
if column in columns:
|
|
734
770
|
continue
|
|
@@ -755,6 +791,74 @@ def _grow_usage_runs(connection: sqlite3.Connection) -> None:
|
|
|
755
791
|
# Another connection's own migration already added it.
|
|
756
792
|
|
|
757
793
|
|
|
794
|
+
def _grow_blocked_calls(connection: sqlite3.Connection) -> None:
|
|
795
|
+
"""Add the rest of a block's marker to a database a previous build wrote
|
|
796
|
+
(ADR-0178). Guarded and idempotent, as `_grow_policy_inputs` is, so
|
|
797
|
+
`SCHEMA_VERSION` does not move: an older build never selects them. Its
|
|
798
|
+
rows keep no `outcome`, and the scan writes no finding for them, because
|
|
799
|
+
that build's hook already wrote it."""
|
|
800
|
+
columns = {row[1] for row in connection.execute('PRAGMA table_info("blocked_calls")')}
|
|
801
|
+
for column, kind in (
|
|
802
|
+
("entry_id", "TEXT"),
|
|
803
|
+
("entry_name", "TEXT"),
|
|
804
|
+
("policy_revision", "INTEGER"),
|
|
805
|
+
("outcome", "TEXT"),
|
|
806
|
+
("decided_where", "TEXT"),
|
|
807
|
+
("rules_version", "TEXT"),
|
|
808
|
+
# A component deny's component and the ids its gate matched (ADR-0184).
|
|
809
|
+
("component_kind", "TEXT"),
|
|
810
|
+
("component_name", "TEXT"),
|
|
811
|
+
("component_version", "TEXT"),
|
|
812
|
+
("component_identity", "TEXT"),
|
|
813
|
+
("gate_evidence", "TEXT"),
|
|
814
|
+
# When the scan wrote the marker's finding. Until then the marker is a
|
|
815
|
+
# debt the scan owes, across restarts (unified-policy.md, "Findings").
|
|
816
|
+
("paid_at", "TEXT"),
|
|
817
|
+
):
|
|
818
|
+
if column in columns:
|
|
819
|
+
continue
|
|
820
|
+
try:
|
|
821
|
+
connection.execute(f"ALTER TABLE blocked_calls ADD COLUMN {column} {kind}")
|
|
822
|
+
except sqlite3.OperationalError as exc:
|
|
823
|
+
if "duplicate column" not in str(exc):
|
|
824
|
+
raise
|
|
825
|
+
# Another connection's own migration already added it.
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
#: The prompt key of a step 5 row, in SQL: `gateway/marker.py`'s `prompt_key`.
|
|
829
|
+
_PROMPT_KEY_SQL = "'prompt-sha256:' || p.text_sha256"
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
def _carry_blocked_prompts(connection: sqlite3.Connection) -> None:
|
|
833
|
+
"""Copy each prompt that a step 5 release blocked into `blocked_calls`.
|
|
834
|
+
|
|
835
|
+
That release's hook wrote the prompt's finding itself, and kept the prompt
|
|
836
|
+
in `blocked_prompts` so that its scan dropped the same prompt. This build
|
|
837
|
+
reads a blocked prompt's marker from `blocked_calls` only, keyed by the
|
|
838
|
+
prompt key. Each row is copied with no `outcome`, the shape of a marker
|
|
839
|
+
whose hook wrote the finding: the scan drops every match on that prompt
|
|
840
|
+
and writes none, so the prompt is not reported twice. `blocked_at` is
|
|
841
|
+
copied too, so both rows age out together. `prompt_id` is not: no scan
|
|
842
|
+
reads it.
|
|
843
|
+
|
|
844
|
+
A copy, not a move: a step 5 daemon that still runs during an upgrade
|
|
845
|
+
reads the table on each scan, and writes to it on each block. Asked on
|
|
846
|
+
every open, and written only when a row is missing, so an open after the
|
|
847
|
+
copy takes no write lock.
|
|
848
|
+
"""
|
|
849
|
+
missing = (
|
|
850
|
+
f"FROM blocked_prompts p WHERE NOT EXISTS (SELECT 1 FROM blocked_calls c"
|
|
851
|
+
f" WHERE c.agent_kind = p.agent_kind AND c.session_id = p.session_id"
|
|
852
|
+
f" AND c.call_id = {_PROMPT_KEY_SQL} AND c.rule_id = p.rule_id)"
|
|
853
|
+
)
|
|
854
|
+
if connection.execute(f"SELECT 1 {missing} LIMIT 1").fetchone() is None:
|
|
855
|
+
return
|
|
856
|
+
connection.execute(
|
|
857
|
+
"INSERT OR IGNORE INTO blocked_calls (agent_kind, session_id, call_id, rule_id, blocked_at)"
|
|
858
|
+
f" SELECT p.agent_kind, p.session_id, {_PROMPT_KEY_SQL}, p.rule_id, p.blocked_at {missing}"
|
|
859
|
+
)
|
|
860
|
+
|
|
861
|
+
|
|
758
862
|
def _grow_usage_buckets(connection: sqlite3.Connection) -> None:
|
|
759
863
|
"""Add the 1-hour part of a bucket's cache writes to a database a previous
|
|
760
864
|
build wrote (ADR-0130). Guarded and idempotent, as `_grow_usage_components`
|
|
@@ -934,16 +1038,18 @@ def prune(
|
|
|
934
1038
|
).fetchone()
|
|
935
1039
|
is not None
|
|
936
1040
|
)
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1041
|
+
# A step 5 release's blocked prompt is kept by the same rule, for the same reason.
|
|
1042
|
+
for table in ("blocked_calls", "blocked_prompts"):
|
|
1043
|
+
if held_back:
|
|
1044
|
+
placeholders = ",".join("?" * len(held_back))
|
|
1045
|
+
connection.execute(
|
|
1046
|
+
f"DELETE FROM {table} WHERE blocked_at < ? AND agent_kind NOT IN ("
|
|
1047
|
+
+ placeholders
|
|
1048
|
+
+ ")",
|
|
1049
|
+
(cutoff, *held_back),
|
|
1050
|
+
)
|
|
1051
|
+
else:
|
|
1052
|
+
connection.execute(f"DELETE FROM {table} WHERE blocked_at < ?", (cutoff,))
|
|
947
1053
|
# Counters for a session 90 days gone are the same dead weight its findings
|
|
948
1054
|
# are, and nothing reads them after a drain.
|
|
949
1055
|
return deleted
|
|
@@ -999,7 +1105,30 @@ _KNOWN_TABLE_COLUMNS: dict[str, frozenset[str]] = {
|
|
|
999
1105
|
"account_cards": frozenset(
|
|
1000
1106
|
{"agent_kind", "account", "email_digest", "display_name", "plan", "seen_at", "name_from"}
|
|
1001
1107
|
),
|
|
1002
|
-
"blocked_calls": frozenset(
|
|
1108
|
+
"blocked_calls": frozenset(
|
|
1109
|
+
{
|
|
1110
|
+
"agent_kind",
|
|
1111
|
+
"session_id",
|
|
1112
|
+
"call_id",
|
|
1113
|
+
"rule_id",
|
|
1114
|
+
"blocked_at",
|
|
1115
|
+
"entry_id",
|
|
1116
|
+
"entry_name",
|
|
1117
|
+
"policy_revision",
|
|
1118
|
+
"outcome",
|
|
1119
|
+
"decided_where",
|
|
1120
|
+
"rules_version",
|
|
1121
|
+
"component_kind",
|
|
1122
|
+
"component_name",
|
|
1123
|
+
"component_version",
|
|
1124
|
+
"component_identity",
|
|
1125
|
+
"gate_evidence",
|
|
1126
|
+
"paid_at",
|
|
1127
|
+
}
|
|
1128
|
+
),
|
|
1129
|
+
"blocked_prompts": frozenset(
|
|
1130
|
+
{"agent_kind", "session_id", "prompt_id", "text_sha256", "rule_id", "blocked_at"}
|
|
1131
|
+
),
|
|
1003
1132
|
"stage_times": frozenset(
|
|
1004
1133
|
{
|
|
1005
1134
|
"event_id",
|
|
@@ -1115,6 +1244,7 @@ _KNOWN_TABLE_COLUMNS: dict[str, frozenset[str]] = {
|
|
|
1115
1244
|
"whole_envelope_digest",
|
|
1116
1245
|
"whole_envelope_metadata_json",
|
|
1117
1246
|
"entries_json",
|
|
1247
|
+
"runtime_mcp_decisions_json",
|
|
1118
1248
|
}
|
|
1119
1249
|
),
|
|
1120
1250
|
}
|
stacktrace/daemon/gateway.py
CHANGED
|
@@ -108,10 +108,7 @@ def store_finding(
|
|
|
108
108
|
document["session_id"] = _qualify_spans(
|
|
109
109
|
payload, kind, detection.session.session_id, session.session_id
|
|
110
110
|
)
|
|
111
|
-
payload
|
|
112
|
-
payload["action"] = stamp.action
|
|
113
|
-
payload["decided_by"] = stamp.decided_by
|
|
114
|
-
payload["where"] = stamp.where
|
|
111
|
+
payload.update(stamp.fields())
|
|
115
112
|
if stamp.principal is not None:
|
|
116
113
|
# The run's account and its card, as a laptop run's sign-in has
|
|
117
114
|
# them, so Fleet names the person with no new field (ADR-0153).
|
|
@@ -188,6 +188,11 @@ def runtime_entries(
|
|
|
188
188
|
blocking = [
|
|
189
189
|
e for e in entries if e.action == "block" and gateway_blockable(e.rule_id, "response")
|
|
190
190
|
]
|
|
191
|
-
return EntrySet(
|
|
191
|
+
return EntrySet(
|
|
192
|
+
{e.rule_id: e.name for e in blocking},
|
|
193
|
+
revision,
|
|
194
|
+
off,
|
|
195
|
+
{e.rule_id: e.id for e in blocking},
|
|
196
|
+
)
|
|
192
197
|
|
|
193
198
|
return read
|