stacktrace-cli 0.18.0__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.
Files changed (36) hide show
  1. stacktrace/__init__.py +1 -1
  2. stacktrace/analysis.py +9 -2
  3. stacktrace/correlate/acquire.py +59 -64
  4. stacktrace/correlate/composition.py +13 -0
  5. stacktrace/correlate/orchestrate.py +11 -4
  6. stacktrace/daemon/costs.py +7 -7
  7. stacktrace/daemon/db.py +149 -19
  8. stacktrace/daemon/gateway.py +3 -13
  9. stacktrace/daemon/gateway_policy.py +14 -9
  10. stacktrace/daemon/inline_hook.py +348 -95
  11. stacktrace/daemon/observer.py +308 -48
  12. stacktrace/daemon/policy_collect.py +68 -17
  13. stacktrace/daemon/policy_inputs.py +13 -2
  14. stacktrace/daemon/policy_runtime.py +225 -75
  15. stacktrace/daemon/runtime.py +30 -19
  16. stacktrace/daemon/scan.py +11 -0
  17. stacktrace/daemon/server.py +8 -0
  18. stacktrace/daemon/store.py +212 -32
  19. stacktrace/detector/deterministic.py +157 -11
  20. stacktrace/detector/inline.py +41 -7
  21. stacktrace/detector/rules.py +51 -11
  22. stacktrace/detector/run.py +15 -3
  23. stacktrace/gateway/analyzer.py +155 -63
  24. stacktrace/gateway/marker.py +729 -0
  25. stacktrace/gateway/service.py +13 -2
  26. stacktrace/gateway/sink.py +17 -9
  27. stacktrace/hooks/claude.py +41 -9
  28. stacktrace/hooks/codex.py +48 -5
  29. stacktrace/hooks/cursor.py +21 -3
  30. stacktrace/hooks/devin.py +125 -0
  31. stacktrace/remote/detect_payload.py +8 -2
  32. stacktrace/remote/hook_policy.py +21 -1
  33. {stacktrace_cli-0.18.0.dist-info → stacktrace_cli-0.19.0.dist-info}/METADATA +4 -4
  34. {stacktrace_cli-0.18.0.dist-info → stacktrace_cli-0.19.0.dist-info}/RECORD +36 -34
  35. {stacktrace_cli-0.18.0.dist-info → stacktrace_cli-0.19.0.dist-info}/WHEEL +0 -0
  36. {stacktrace_cli-0.18.0.dist-info → stacktrace_cli-0.19.0.dist-info}/entry_points.txt +0 -0
stacktrace/__init__.py CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  import logging as _logging
4
4
 
5
- __version__ = "0.18.0"
5
+ __version__ = "0.19.0"
6
6
 
7
7
  # Only the daemon attaches a handler (ADR-0048). Without this one, a WARNING
8
8
  # logged in any other process would reach Python's last-resort handler and
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,
@@ -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
- with tempfile.TemporaryDirectory() as scratch:
246
- directory = Path(scratch)
247
- argv = [
248
- _openaca(),
249
- "bom",
250
- "endpoint",
251
- "--kind",
252
- agent_kind,
253
- "--output-dir",
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
- if result.returncode != 0:
265
- # A missing composition is the difference between "nothing ran" and
266
- # "we could not look"; the second must not read as the first.
267
- scope = f" --project {project}" if project is not None else ""
268
- raise RuntimeError(
269
- f"openaca bom endpoint --kind {agent_kind}{scope} failed "
270
- f"(exit {result.returncode}): {result.stderr.strip()}"
271
- )
272
-
273
- # `*.cdx.json`, not `*.json`: OpenACA also writes a
274
- # `.openaca-bom-manifest.json` index beside the documents, and counting
275
- # it as a BOM would read one agent as two.
276
- documents = sorted(directory.glob("*.cdx.json"))
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
- # Read now: the first moment this document is known to exist, and to be
300
- # the kind that was asked for. Before the subprocess it did not exist, so
301
- # that reading could only ever be a lower bound.
302
- #
303
- # Advisories are *not* fetched here. They are matched once, after
304
- # correlation, over the components sessions actually invoked — see
305
- # `advisories_for`.
306
- return Built(
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[[CorrelatedView, "Sequence[Built]"], CorrelatedView]
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(correlated: CorrelatedView, builts: Sequence[Built]) -> CorrelatedView:
238
- return _attach_advisories(correlated, builts, lookup=lookup)
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
- keys = _invoked_keys(correlated)
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 {}
@@ -526,13 +526,13 @@ class CostPass:
526
526
  | None = None,
527
527
  org_baselines: Callable[[], Mapping[Group, Bars]] = dict,
528
528
  repo_name: Callable[[str | None], str | None] | None = None,
529
- muted: Callable[[], frozenset[str]] | None = None,
529
+ off: Callable[[], frozenset[str]] | None = None,
530
530
  ) -> None:
531
531
  self.store = store
532
532
  self.publish = publish
533
- #: The rules whose policy entry says `notify: false`; their findings
534
- #: are stored but never sent (`PolicyRuntime.muted`).
535
- self._muted = muted or frozenset
533
+ #: The rules that are Off; their findings are never recorded, and
534
+ #: never notify again (`PolicyRuntime.off`).
535
+ self._off = off or frozenset
536
536
  #: The team's similar sessions, the second of the three groups a run
537
537
  #: is compared with, found by its repo's name (cost-notifications
538
538
  #: §2.3).
@@ -647,11 +647,11 @@ class CostPass:
647
647
  writes.append(self._written[key])
648
648
  return min(writes).isoformat() if writes else now.isoformat()
649
649
 
650
- muted = self._muted()
650
+ off = self._off()
651
651
  for firing in firings:
652
+ if firing.rule_id in off:
653
+ continue
652
654
  document = detection_json(to_detection(firing))
653
- if firing.rule_id in muted:
654
- document = {**document, "notify": False}
655
655
  rang = self._record(firing.session, document, now, started(firing.session))
656
656
  if firing.rule_id == RUNAWAY_SESSION and firing.extra is not None:
657
657
  self._again(firing.session, document, firing.extra, rang=rang)
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
- -- Each rule a hook blocked a call for, by the agent's own call id, so the
394
- -- scan drops every rule's finding on the call when it reads the transcript
395
- -- (`docs/specs/inline-detection-enforcement.md`, "Recording a block").
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 TEXT NOT NULL,
399
- session_id TEXT NOT NULL,
400
- call_id TEXT NOT NULL,
401
- rule_id TEXT NOT NULL,
402
- blocked_at TEXT NOT NULL,
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
- if held_back:
938
- placeholders = ",".join("?" * len(held_back))
939
- connection.execute(
940
- "DELETE FROM blocked_calls WHERE blocked_at < ? AND agent_kind NOT IN ("
941
- + placeholders
942
- + ")",
943
- (cutoff, *held_back),
944
- )
945
- else:
946
- connection.execute("DELETE FROM blocked_calls WHERE blocked_at < ?", (cutoff,))
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({"agent_kind", "session_id", "call_id", "rule_id", "blocked_at"}),
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
  }
@@ -108,14 +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["actor"] = stamp.actor
112
- payload["action"] = stamp.action
113
- payload["decided_by"] = stamp.decided_by
114
- payload["where"] = stamp.where
115
- if not stamp.notify:
116
- # Store-only: never sent to Fleet (`GATEWAY_FIELDS` in
117
- # `remote/detect_payload.py` does not declare it).
118
- payload["notify"] = False
111
+ payload.update(stamp.fields())
119
112
  if stamp.principal is not None:
120
113
  # The run's account and its card, as a laptop run's sign-in has
121
114
  # them, so Fleet names the person with no new field (ADR-0153).
@@ -124,11 +117,8 @@ def store_finding(
124
117
  notifications = store.record(session, (payload,), underbuilt=None)
125
118
  # As a scan commit does, so the finding leaves before the next drain interval.
126
119
  on_commit()
127
- # The finding is stored either way; an entry with `notify: false` keeps
128
- # it out of Slack and the session only.
129
- if stamp.notify:
130
- for notification in notifications:
131
- publish(notification)
120
+ for notification in notifications:
121
+ publish(notification)
132
122
 
133
123
  return write
134
124
 
@@ -29,6 +29,8 @@ from stacktrace.hooks.claude import policy_message
29
29
  from stacktrace.policy_display import policy_rows
30
30
  from stacktrace.sessions.access import GatewayToolCall
31
31
 
32
+ from .policy_inputs import off_rules
33
+
32
34
  # `Policy` through the runtime, which is this package's one OpenACA policy importer.
33
35
  from .policy_runtime import REFRESH_INTERVAL, Policy, PolicyRuntime, Result, Subject, decide
34
36
 
@@ -166,10 +168,12 @@ def runtime_entries(
166
168
  runtime: PolicyRuntime, *, ttl_s: float = POLICY_TTL_S
167
169
  ) -> Callable[[], EntrySet]:
168
170
  """The rules whose entry blocks at the response check, each with its entry's
169
- name, and the revision that published them.
171
+ name, the revision that published them, and the rules that are Off.
170
172
 
171
173
  The same time to live as the policy: a gateway stops trusting both together.
172
- With no policy, a stale one, or no entry, nothing blocks (ADR-0159).
174
+ With no policy, a stale one, or no entry, nothing blocks (ADR-0159). An Off
175
+ rule is Off whatever the policy's age, as it is for the scan
176
+ (`PolicyRuntime.off`).
173
177
  """
174
178
 
175
179
  def read() -> EntrySet:
@@ -177,17 +181,18 @@ def runtime_entries(
177
181
  entries = runtime.cache.state.detection_rules
178
182
  revision = runtime.cache.state.desired_revision
179
183
  unified = runtime.cache.state.entries
184
+ off = off_rules(unified)
180
185
  refreshed = runtime.refreshed_at
181
186
  if refreshed is None or runtime.clock() - refreshed > ttl_s:
182
- return EntrySet()
187
+ return EntrySet(off=off)
183
188
  blocking = [
184
189
  e for e in entries if e.action == "block" and gateway_blockable(e.rule_id, "response")
185
190
  ]
186
- names = {e.rule_id: e.name for e in blocking}
187
- notify = {e.rule_id: e.notify for e in blocking}
188
- # The unified policy's `notify` mutes every finding of its rule, not
189
- # only a block (unified-policy.md, "Notification").
190
- notify.update({e.rule_id: e.notify for e in unified})
191
- return EntrySet(names, revision, notify)
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