stacktrace-cli 0.20.0__py3-none-any.whl → 0.21.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 (53) hide show
  1. stacktrace/__init__.py +4 -6
  2. stacktrace/analysis.py +12 -1
  3. stacktrace/cli.py +31 -0
  4. stacktrace/correlate/observed.py +25 -0
  5. stacktrace/cost/baseline.py +39 -10
  6. stacktrace/cost/rules.py +32 -10
  7. stacktrace/daemon/activity_log.py +4 -3
  8. stacktrace/daemon/cli.py +2 -2
  9. stacktrace/daemon/composition.py +2 -2
  10. stacktrace/daemon/config.py +12 -0
  11. stacktrace/daemon/costs.py +7 -6
  12. stacktrace/daemon/db.py +14 -0
  13. stacktrace/daemon/gateway.py +2 -2
  14. stacktrace/daemon/inline_hook.py +2 -2
  15. stacktrace/daemon/mcp_records.py +180 -0
  16. stacktrace/daemon/observer.py +4 -2
  17. stacktrace/daemon/paths.py +2 -10
  18. stacktrace/daemon/policy_inputs.py +54 -12
  19. stacktrace/daemon/policy_runtime.py +148 -3
  20. stacktrace/daemon/runtime.py +48 -15
  21. stacktrace/daemon/server.py +2 -2
  22. stacktrace/daemon/socket_path.py +26 -0
  23. stacktrace/daemon/store.py +3 -2
  24. stacktrace/daemon/usage.py +2 -2
  25. stacktrace/detector/deterministic.py +646 -29
  26. stacktrace/detector/hook_table.py +41 -0
  27. stacktrace/detector/inline.py +3 -1
  28. stacktrace/detector/installs.py +413 -0
  29. stacktrace/detector/known-crates.txt +9831 -0
  30. stacktrace/detector/known-npm.txt +15946 -0
  31. stacktrace/detector/known-pypi.txt +12401 -0
  32. stacktrace/detector/mcp_accounts.py +275 -0
  33. stacktrace/detector/persistence_files.py +183 -0
  34. stacktrace/detector/rules.py +127 -6
  35. stacktrace/detector/run.py +7 -2
  36. stacktrace/finding_titles.py +3 -0
  37. stacktrace/gateway/analyzer.py +2 -2
  38. stacktrace/gateway/marker.py +36 -2
  39. stacktrace/gateway/service.py +2 -2
  40. stacktrace/gateway/sink.py +2 -2
  41. stacktrace/gateway/tetrate/guardrail.py +2 -2
  42. stacktrace/gateway/tetrate/requestlog.py +2 -2
  43. stacktrace/hooks/claude.py +6 -26
  44. stacktrace/monitor/costs.py +7 -7
  45. stacktrace/monitor/server.py +1 -2
  46. stacktrace/monitor/site/app.js +3 -0
  47. stacktrace/package_logger.py +21 -0
  48. stacktrace/sessions/hook_journal.py +28 -1
  49. stacktrace/telemetry/posthog.py +3 -2
  50. {stacktrace_cli-0.20.0.dist-info → stacktrace_cli-0.21.0.dist-info}/METADATA +4 -4
  51. {stacktrace_cli-0.20.0.dist-info → stacktrace_cli-0.21.0.dist-info}/RECORD +53 -43
  52. {stacktrace_cli-0.20.0.dist-info → stacktrace_cli-0.21.0.dist-info}/WHEEL +0 -0
  53. {stacktrace_cli-0.20.0.dist-info → stacktrace_cli-0.21.0.dist-info}/entry_points.txt +0 -0
stacktrace/__init__.py CHANGED
@@ -1,14 +1,12 @@
1
1
  """The `stacktrace` command-line interface — detection and response for AI agents."""
2
2
 
3
- import logging as _logging
4
3
  from typing import TYPE_CHECKING
5
4
 
6
- __version__ = "0.20.0"
5
+ __version__ = "0.21.0"
7
6
 
8
- # Only the daemon attaches a handler (ADR-0048). Without this one, a WARNING
9
- # logged in any other process would reach Python's last-resort handler and
10
- # print onto the user's terminal.
11
- _logging.getLogger(__name__).addHandler(_logging.NullHandler())
7
+ # The package logger's `NullHandler` (ADR-0048) is attached by
8
+ # `package_logger`, which every module that logs imports. Not here: every
9
+ # `stacktrace` process imports this package, the per-call hook included.
12
10
 
13
11
  if TYPE_CHECKING:
14
12
  from stacktrace.sdk import ObservationClient
stacktrace/analysis.py CHANGED
@@ -173,6 +173,7 @@ def analyse(
173
173
  analyzer: str = NO_ANALYZER,
174
174
  jev_key: str | None = None,
175
175
  history: DriftHistory | None = None,
176
+ company_domains: frozenset[str] | None = None,
176
177
  ) -> Analysis:
177
178
  """Read this machine's sessions, correlate them, and judge them.
178
179
 
@@ -200,6 +201,9 @@ def analyse(
200
201
  hands the analyzer a key a caller already holds, in place of reading
201
202
  `TYPESAFE_API_KEY`. Both travel to `run_detector` unchanged, so a
202
203
  programmatic caller controls exactly what `detect` controls.
204
+
205
+ `company_domains` is `detect --company-domain`; `None` leaves the rule to
206
+ read `STACKTRACE_COMPANY_DOMAINS` (ADR-0152).
203
207
  """
204
208
  window_end = datetime.now(UTC)
205
209
  window_start = since if isinstance(since, datetime) else parse_since(since)
@@ -224,6 +228,7 @@ def analyse(
224
228
  analyzer=analyzer,
225
229
  jev_key=jev_key,
226
230
  history=history,
231
+ company_domains=company_domains,
227
232
  )
228
233
  return scoped_to_window(found)
229
234
 
@@ -246,10 +251,13 @@ def analyse_sessions(
246
251
  jev_key: str | None = None,
247
252
  history: DriftHistory | None = None,
248
253
  gate_conditions: Mapping[str, object] | None = None,
254
+ company_domains: frozenset[str] | None = None,
249
255
  ) -> Analysis:
250
256
  """Correlate and judge a session view already collected by a long-lived reader.
251
257
 
252
- `gate_conditions` travels to `run_detector` unchanged.
258
+ `gate_conditions` travels to `run_detector` unchanged. `company_domains`
259
+ is the daemon's choice of list (ADR-0154); `None` leaves the rule to read
260
+ `STACKTRACE_COMPANY_DOMAINS`.
253
261
  """
254
262
  window_end = datetime.now(UTC)
255
263
  window_start = since if isinstance(since, datetime) else parse_since(since)
@@ -269,6 +277,7 @@ def analyse_sessions(
269
277
  jev_key=jev_key,
270
278
  history=history,
271
279
  gate_conditions=gate_conditions,
280
+ company_domains=company_domains,
272
281
  )
273
282
 
274
283
 
@@ -289,6 +298,7 @@ def _analyse_sessions(
289
298
  jev_key: str | None,
290
299
  history: DriftHistory | None,
291
300
  gate_conditions: Mapping[str, object] | None = None,
301
+ company_domains: frozenset[str] | None = None,
292
302
  ) -> Analysis:
293
303
  acquired = _acquire(
294
304
  sessions,
@@ -307,6 +317,7 @@ def _analyse_sessions(
307
317
  jev_key=jev_key,
308
318
  history=history,
309
319
  gate_conditions=gate_conditions,
320
+ company_domains=company_domains,
310
321
  )
311
322
  return Analysis(
312
323
  run=run,
stacktrace/cli.py CHANGED
@@ -29,6 +29,7 @@ from .daemon.cli import daemon as daemon_cmd
29
29
  from .daemon.cli import findings
30
30
  from .detector.cache import VerdictCache, default_directory
31
31
  from .detector.history import DriftHistory, default_history_path
32
+ from .detector.mcp_accounts import parse_company_domains
32
33
  from .detector.render import render_json as render_detections_json
33
34
  from .detector.render import render_text as render_detections_text
34
35
  from .detector.run import DEFAULT_BUDGET, DEFAULT_SAMPLE_BUDGET
@@ -394,6 +395,16 @@ def sessions(
394
395
  default=None,
395
396
  help="Read agent transcripts from here instead of the default location.",
396
397
  )
398
+ @click.option(
399
+ "--company-domain",
400
+ "company_domains",
401
+ multiple=True,
402
+ metavar="DOMAIN",
403
+ help="A domain your company's accounts use, such as acme.com; repeatable. An MCP "
404
+ "server that acts as an account outside every one is a finding. Default: the "
405
+ "daemon's Fleet policy entry, then STACKTRACE_COMPANY_DOMAINS; with none, that "
406
+ "rule is off (ADR-0152, ADR-0154).",
407
+ )
397
408
  def detect(
398
409
  agent_kinds: tuple[str, ...],
399
410
  since: datetime,
@@ -407,6 +418,7 @@ def detect(
407
418
  cache: bool,
408
419
  project_map: tuple[str, ...],
409
420
  root: Path | None,
421
+ company_domains: tuple[str, ...],
410
422
  ) -> None:
411
423
  """Find security and reliability findings in what agents did.
412
424
 
@@ -451,6 +463,9 @@ def detect(
451
463
  bom_paths=bom_paths,
452
464
  project_map=project_map,
453
465
  root=root,
466
+ company_domains=parse_company_domains(company_domains)
467
+ if company_domains
468
+ else _policy_company_domains(),
454
469
  reasoning=reasoning,
455
470
  budget=budget,
456
471
  sample_budget=sample_budget,
@@ -482,6 +497,22 @@ def detect(
482
497
  click.echo(output, nl=not output.endswith("\n"))
483
498
 
484
499
 
500
+ def _policy_company_domains() -> frozenset[str] | None:
501
+ """The daemon's policy entry's company domains, or None to read the
502
+ variable: `--company-domain`, then the entry, then
503
+ `STACKTRACE_COMPANY_DOMAINS` (ADR-0154)."""
504
+ from stacktrace.daemon.paths import RuntimePaths
505
+ from stacktrace.daemon.policy_inputs import stored_detection_entry
506
+ from stacktrace.remote.config import get_config_path
507
+
508
+ entry = stored_detection_entry(
509
+ RuntimePaths.from_environment().state,
510
+ get_config_path(),
511
+ "stacktrace-mcp-personal-account",
512
+ )
513
+ return entry.company_domains if entry is not None else None
514
+
515
+
485
516
  @click.command()
486
517
  @click.option("--port", type=int, default=0, help="Serve on this port instead of an unused one.")
487
518
  @click.option(
@@ -18,6 +18,7 @@ observation deliberately does not.
18
18
 
19
19
  from __future__ import annotations
20
20
 
21
+ import functools
21
22
  import re
22
23
  import shlex
23
24
  from collections.abc import Sequence
@@ -241,6 +242,17 @@ _MAX_SHELL_DEPTH = 3
241
242
  #: makes one constant up. Real command lines nest one or two deep.
242
243
  _MAX_EXPANSION_DEPTH = 64
243
244
 
245
+ #: How many command texts each parse cache keeps. A parse is a pure function of
246
+ #: the text, and the daemon analyses a whole session again each time its
247
+ #: transcript grows, with several rules asking about the same command: parsed
248
+ #: anew each time, one long session's Bash calls cost two seconds a scan.
249
+ #: Command text is not bounded in size -- a heredoc or inline-script body can
250
+ #: run to several kilobytes -- so the bound stays well above the 1,076 calls
251
+ #: one long session profiled at, four sessions' worth, rather than large
252
+ #: enough to let ordinary multi-kilobyte commands reach gigabytes across the
253
+ #: three caches before an entry is evicted.
254
+ _PARSE_CACHE = 4_096
255
+
244
256
  _ASSIGNMENT = re.compile(r"^\w+=")
245
257
 
246
258
  #: What ends an unquoted heredoc delimiter. A delimiter is an ordinary shell
@@ -867,6 +879,7 @@ def _mask_uninvoked_functions(text: str, depth: int) -> str:
867
879
  return _blank(text, headers + [span for name, _, span in definitions if name not in called])
868
880
 
869
881
 
882
+ @functools.lru_cache(maxsize=_PARSE_CACHE)
870
883
  def _readable_commands(command: str, depth: int) -> str:
871
884
  """The command line with everything that is not a command blanked out.
872
885
 
@@ -1530,6 +1543,12 @@ def command_segments(command: object, depth: int = 0) -> tuple[tuple[str, tuple[
1530
1543
  """
1531
1544
  if not isinstance(command, str) or not command:
1532
1545
  return ()
1546
+ return _command_segments(command, depth)
1547
+
1548
+
1549
+ @functools.lru_cache(maxsize=_PARSE_CACHE)
1550
+ def _command_segments(command: str, depth: int) -> tuple[tuple[str, tuple[str, ...]], ...]:
1551
+ """`command_segments` for a non-empty string, parsed once for each text."""
1533
1552
  return _segments_of(_readable_commands(command, depth), depth)
1534
1553
 
1535
1554
 
@@ -1660,6 +1679,12 @@ def redirect_targets(command: object) -> tuple[str, ...]:
1660
1679
  """
1661
1680
  if not isinstance(command, str) or not command:
1662
1681
  return ()
1682
+ return _redirect_targets(command)
1683
+
1684
+
1685
+ @functools.lru_cache(maxsize=_PARSE_CACHE)
1686
+ def _redirect_targets(command: str) -> tuple[str, ...]:
1687
+ """`redirect_targets` for a non-empty string, parsed once for each text."""
1663
1688
  found: list[str] = []
1664
1689
  # The same masks the head parse reads, so the two halves of this module
1665
1690
  # cannot disagree about which text is a command: a `>` inside a heredoc
@@ -56,6 +56,9 @@ LARGE_CONTEXT = 50_000
56
56
  FINISHED_AFTER = timedelta(hours=1)
57
57
  #: How far back the baseline reaches.
58
58
  WINDOW = timedelta(days=30)
59
+ #: How long a run a cost finding was raised on stays out of the baseline: the
60
+ #: local day of its last response and the next (cost-notifications §2.2).
61
+ FLAGGED_FOR = timedelta(days=2)
59
62
 
60
63
 
61
64
  def baseline_since(now: datetime, zone: tzinfo | None) -> datetime:
@@ -64,11 +67,42 @@ def baseline_since(now: datetime, zone: tzinfo | None) -> datetime:
64
67
  is, so the window, and every baseline built from it, moves only at
65
68
  midnight (cost-phase-1 §1, "Finished run"). In UTC, as the store keeps
66
69
  times."""
70
+ return _day_start(now, zone, WINDOW.days)
71
+
72
+
73
+ def _day_start(now: datetime, zone: tzinfo | None, days: int) -> datetime:
74
+ """The start of the local day `days` before today, in UTC."""
67
75
  zone = zone or UTC
68
- first = now.astimezone(zone).date() - timedelta(days=WINDOW.days)
76
+ first = now.astimezone(zone).date() - timedelta(days=days)
69
77
  return datetime.combine(first, time.min, tzinfo=zone).astimezone(UTC)
70
78
 
71
79
 
80
+ def in_history(
81
+ summaries: Iterable[Summary],
82
+ *,
83
+ flagged: frozenset[tuple[str, str]],
84
+ now: datetime,
85
+ zone: tzinfo | None,
86
+ ) -> list[Summary]:
87
+ """The runs a baseline built at `now` holds: the last 30 days of runs,
88
+ without each run a cost finding was raised on (ADR-0093) for `FLAGGED_FOR`
89
+ local days. A second runaway the next day is then judged without the
90
+ first. After that the run returns: the long legitimate runs that get
91
+ flagged would otherwise leave for good and lower the bar for every long
92
+ run after them. By local dates, so this too moves only at midnight.
93
+
94
+ `flagged` holds `(agent_kind, run_id)` pairs: a run id is unique only
95
+ within the reader that emitted it."""
96
+ since = baseline_since(now, zone)
97
+ returns = _day_start(now, zone, FLAGGED_FOR.days - 1)
98
+ return [
99
+ s
100
+ for s in summaries
101
+ if s.last_activity_at >= since
102
+ and (s.last_activity_at < returns or (s.agent_kind, s.run_id) not in flagged)
103
+ ]
104
+
105
+
72
106
  Cohort = tuple[str | None, str | None, str | None]
73
107
  #: Sorts an undated response first; only ever a sort key.
74
108
  _NEVER = datetime.min.replace(tzinfo=UTC)
@@ -414,15 +448,10 @@ def snapshot(
414
448
  zone: tzinfo | None = None,
415
449
  repo_name: Callable[[str | None], str | None] | None = None,
416
450
  ) -> Snapshot:
417
- """The baseline as of `now`: the last 30 days of runs, without the runs a
418
- cost finding was raised on (ADR-0093), grouped by agent and repo, and by
419
- agent alone.
420
-
421
- `flagged` holds `(agent_kind, run_id)` pairs: a run id is unique only
422
- within the reader that emitted it. `repo_name` names a working
423
- directory's repo, as `repo_of` reads it."""
424
- recent = [s for s in summaries if s.last_activity_at >= baseline_since(now, zone)]
425
- kept = [s for s in recent if (s.agent_kind, s.run_id) not in flagged]
451
+ """The baseline as of `now`: the runs `in_history` keeps, grouped by agent
452
+ and repo, and by agent alone. `repo_name` names a working directory's
453
+ repo, as `repo_of` reads it."""
454
+ kept = in_history(summaries, flagged=flagged, now=now, zone=zone)
426
455
  by_repo: dict[Group, list[Summary]] = {}
427
456
  by_agent: dict[str | None, list[Summary]] = {}
428
457
  for summary in kept:
stacktrace/cost/rules.py CHANGED
@@ -26,7 +26,14 @@ from datetime import UTC, datetime, timedelta, tzinfo
26
26
  from typing import Any, TypeVar
27
27
 
28
28
  from stacktrace.correlate.composition import Component
29
- from stacktrace.cost.baseline import MAX_MINUTES, Bars, Snapshot, judged_at, repo_of
29
+ from stacktrace.cost.baseline import (
30
+ MAX_MINUTES,
31
+ Bars,
32
+ Snapshot,
33
+ is_finished,
34
+ judged_at,
35
+ repo_of,
36
+ )
30
37
  from stacktrace.cost.components import Placement, context_shares
31
38
  from stacktrace.cost.evidence import times, tokens
32
39
  from stacktrace.cost.measures import (
@@ -257,6 +264,18 @@ class Context:
257
264
 
258
265
  _RUNAWAY_MINUTES = 10
259
266
 
267
+
268
+ def _bar(row: tuple[float, float, float]) -> float:
269
+ """The tokens a run must pass at one active minute, from the group's
270
+ (p50, p95, p99) there: the lower of twice the p99, and the usual plus 3
271
+ times the p99's own extra. Over as few as 10 runs the p99 is close to the
272
+ largest of them, so about one ordinary run in 11 passed it. The second
273
+ bar keeps a steady group, whose p99 is near its usual, firing at 3x the
274
+ usual (ADR-0203)."""
275
+ usual, _p95, p99 = row
276
+ return min(2 * p99, usual + 3 * (p99 - usual))
277
+
278
+
260
279
  #: The least extra a finding is worth, in tokens above the usual, so a small run
261
280
  #: far above a small usual stays quiet (ADR-0099 says where it came from).
262
281
  _FLOOR = 40_000_000
@@ -284,12 +303,13 @@ def runaway_session(
284
303
  runs: Sequence[Run[L]], context: Context
285
304
  ) -> tuple[tuple[Firing, ...], tuple[NearMiss, ...]]:
286
305
  """Session cost (cost-notifications §2.2): for 10 active minutes, tokens
287
- above the p99 of similar sessions at the same minute and 5-minute burn
288
- above their p95; at least 40M tokens above their usual. Each minute is
289
- judged against the first group of similar sessions, narrowest first, in
290
- which at least 10 runs reached it, and not at all without one. The finding
291
- carries up to three reasons (`cost/reasons.py`). A session started by a
292
- program is unattended."""
306
+ above a bar set by the p99 of similar sessions at the same minute
307
+ (`_bar`), and 5-minute burn above their p95; at least 40M tokens above
308
+ their usual. Each minute is judged against the first group of similar
309
+ sessions, narrowest first, in which at least 10 runs reached it, and not
310
+ at all without one. Only a run that has not finished is judged. The
311
+ finding carries up to three reasons (`cost/reasons.py`). A session started
312
+ by a program is unattended."""
293
313
  snapshot = context.snapshot
294
314
  if snapshot is None:
295
315
  return (), ()
@@ -297,7 +317,9 @@ def runaway_session(
297
317
  misses: list[NearMiss] = []
298
318
  for run in runs:
299
319
  main = run.main
300
- if main is None:
320
+ # A finished run has nothing left to stop (cost-notifications §2.2). A
321
+ # later pass that reads it, after a restart or a new baseline, is silent.
322
+ if main is None or is_finished(run, context.now):
301
323
  continue
302
324
  name = context.repo_name(main.working_directory) if context.repo_name else None
303
325
  levels = snapshot.levels(
@@ -328,7 +350,7 @@ def runaway_session(
328
350
  held = 0
329
351
  continue
330
352
  row = bars.curve[index]
331
- over = spent > row[2] and minute_burn(responses, minute) > bars.burn[index][1]
353
+ over = spent > _bar(row) and minute_burn(responses, minute) > bars.burn[index][1]
332
354
  held = held + 1 if over else 0
333
355
  if held < _RUNAWAY_MINUTES:
334
356
  continue
@@ -407,7 +429,7 @@ def _held_through(
407
429
  bars = judged_at(levels, index)
408
430
  if bars is None:
409
431
  break
410
- over = curve[index] > bars.curve[index][2] and (
432
+ over = curve[index] > _bar(bars.curve[index]) and (
411
433
  minute_burn(responses, minutes[index]) > bars.burn[index][1]
412
434
  )
413
435
  if not over:
@@ -1,6 +1,6 @@
1
1
  """The daemon's activity log: leveled, private and capped (ADR-0048).
2
2
 
3
- Every module logs to its own `logging.getLogger(__name__)`. Only the daemon
3
+ Every module logs to its own `package_logger.get_logger(__name__)`. Only the daemon
4
4
  attaches a file handler, to the `stacktrace` package logger; everywhere
5
5
  else the package's `NullHandler` keeps those lines off the terminal.
6
6
 
@@ -20,6 +20,7 @@ from collections.abc import Mapping
20
20
  from logging.handlers import RotatingFileHandler
21
21
  from pathlib import Path
22
22
 
23
+ from stacktrace.package_logger import get_logger
23
24
  from stacktrace.private_state import ensure_private_directory
24
25
 
25
26
  LEVEL_VARIABLE = "STACKTRACE_DAEMON_LOG_LEVEL"
@@ -82,14 +83,14 @@ def configure(
82
83
  )
83
84
  formatter.converter = time.gmtime
84
85
  handler.setFormatter(formatter)
85
- logger = logging.getLogger(PACKAGE)
86
+ logger = get_logger(PACKAGE)
86
87
  logger.setLevel(level_from(os.environ if environ is None else environ))
87
88
  logger.addHandler(handler)
88
89
  return handler
89
90
 
90
91
 
91
92
  def release(handler: logging.Handler) -> None:
92
- logger = logging.getLogger(PACKAGE)
93
+ logger = get_logger(PACKAGE)
93
94
  logger.removeHandler(handler)
94
95
  logger.setLevel(logging.NOTSET)
95
96
  handler.close()
stacktrace/daemon/cli.py CHANGED
@@ -341,7 +341,7 @@ def _baseline_line(paths: RuntimePaths) -> str:
341
341
  finding was raised on, grouped by agent and repo as the rules group them,
342
342
  and a repo counted as having enough history when its own runs can set a
343
343
  minute's bar. Nothing here recomputes what the baseline already decides."""
344
- from stacktrace.cost.baseline import MIN_RUNS_AT_MINUTE, baseline_since, snapshot
344
+ from stacktrace.cost.baseline import MIN_RUNS_AT_MINUTE, baseline_since, in_history, snapshot
345
345
  from stacktrace.cost.clock import environment_zone
346
346
  from stacktrace.daemon.costs import summaries_from_rows
347
347
  from stacktrace.daemon.usage import RepoNames
@@ -361,7 +361,7 @@ def _baseline_line(paths: RuntimePaths) -> str:
361
361
  if "no such table" not in str(error):
362
362
  raise
363
363
  return "none (the daemon running here predates cost; restart it to start one)"
364
- summaries = [s for s in summaries_from_rows(rows) if (s.agent_kind, s.run_id) not in flagged]
364
+ summaries = in_history(summaries_from_rows(rows), flagged=flagged, now=now, zone=zone)
365
365
  # Said first: an incomplete backfill is why the rules and the usual wait,
366
366
  # whether or not any rows are stored yet.
367
367
  waiting = "; incomplete, the rules that need it wait (see the daemon log)"
@@ -2,7 +2,6 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- import logging
6
5
  import threading
7
6
  from collections.abc import Callable, Sequence
8
7
  from contextlib import suppress
@@ -12,8 +11,9 @@ from time import monotonic
12
11
  from stacktrace.correlate.acquire import Built
13
12
  from stacktrace.correlate.join import candidates_for, listed_key
14
13
  from stacktrace.correlate.orchestrate import BuildAll, build_compositions, declares_agent_config
14
+ from stacktrace.package_logger import get_logger
15
15
 
16
- logger = logging.getLogger(__name__)
16
+ logger = get_logger(__name__)
17
17
 
18
18
  Scope = tuple[str, Path | None]
19
19
  Key = tuple[str, str | None]
@@ -16,6 +16,7 @@ from dataclasses import dataclass
16
16
  from datetime import timedelta
17
17
  from decimal import Decimal, InvalidOperation
18
18
 
19
+ from stacktrace.detector.mcp_accounts import parse_company_domains
19
20
  from stacktrace.sessions.access import observable_kinds
20
21
 
21
22
  #: Every setting, as `(attribute, environment variable, default spelling)`.
@@ -37,6 +38,10 @@ SETTINGS: tuple[tuple[str, str, str], ...] = (
37
38
  # through the environment; `daemon run` gains no flag for it.
38
39
  ("gateway_listen", "STACKTRACE_GATEWAY_LISTEN", "off"),
39
40
  ("gateway_mode", "STACKTRACE_GATEWAY_MODE", "enforce"),
41
+ # ADR-0152: the domains `stacktrace-mcp-personal-account` compares against.
42
+ # Environment only: the detector reads the same variable wherever it runs,
43
+ # so the daemon, a detached `start` and `guard serve` cannot disagree.
44
+ ("company_domains", "STACKTRACE_COMPANY_DOMAINS", ""),
40
45
  )
41
46
 
42
47
  _UNITS: dict[str, int] = {"s": 1, "m": 60, "h": 3600, "d": 86400}
@@ -174,6 +179,7 @@ class DaemonConfig:
174
179
  reasoning: Setting
175
180
  gateway_listen: Setting
176
181
  gateway_mode: Setting
182
+ company_domains: Setting
177
183
 
178
184
  @property
179
185
  def scan_interval_seconds(self) -> float:
@@ -206,6 +212,10 @@ class DaemonConfig:
206
212
  def reasoning_enabled(self) -> bool:
207
213
  return parse_reasoning(self.reasoning.value)
208
214
 
215
+ @property
216
+ def company_domain_set(self) -> frozenset[str]:
217
+ return parse_company_domains(self.company_domains.value)
218
+
209
219
  @property
210
220
  def kinds(self) -> tuple[str, ...]:
211
221
  """The agent kinds to observe, in the order given, without repeats.
@@ -284,6 +294,7 @@ class DaemonConfig:
284
294
  "reasoning": None if reasoning is None else ("true" if reasoning else "false"),
285
295
  "gateway_listen": None,
286
296
  "gateway_mode": None,
297
+ "company_domains": None,
287
298
  }
288
299
  resolved: dict[str, Setting] = {}
289
300
  for name, variable, default in SETTINGS:
@@ -312,6 +323,7 @@ class DaemonConfig:
312
323
  config.reasoning_enabled,
313
324
  parse_listen(config.gateway_listen.value),
314
325
  parse_gateway_mode(config.gateway_mode.value),
326
+ config.company_domain_set,
315
327
  )
316
328
  if not config.kinds:
317
329
  raise ValueError("--agent-kind must name at least one agent kind")
@@ -21,7 +21,6 @@ Nothing here prices, judges or formats; `cost/` does.
21
21
  from __future__ import annotations
22
22
 
23
23
  import json
24
- import logging
25
24
  import os
26
25
  import sqlite3
27
26
  import threading
@@ -59,14 +58,15 @@ from stacktrace.daemon.jobs import Backoff
59
58
  from stacktrace.daemon.observer import Collected
60
59
  from stacktrace.daemon.store import FindingStore, Notification, SessionKey
61
60
  from stacktrace.daemon.unreadable import OncePerChange, describe
62
- from stacktrace.detector.deterministic import run_deterministic
61
+ from stacktrace.detector.deterministic import run_blocked
63
62
  from stacktrace.detector.render import detection_json
64
63
  from stacktrace.detector.rules import COST_RULES, RELIABILITY_RULES
64
+ from stacktrace.package_logger import get_logger
65
65
  from stacktrace.sessions.access import SessionView, collect_sessions
66
66
  from stacktrace.sessions.lineage import parent_session_id
67
67
  from stacktrace.sessions.protocols import SessionLike
68
68
 
69
- logger = logging.getLogger(__name__)
69
+ logger = get_logger(__name__)
70
70
 
71
71
  #: A changed sub-agent transcript is looked for back to the last walk, less
72
72
  #: this much, so a file written during the walk is not missed.
@@ -359,7 +359,8 @@ class Baseline:
359
359
  # Rebuilt when a row is written, and also when the local day turns
360
360
  # (the window moves) or a run gains a cost finding (flagged runs
361
361
  # leave the baseline, ADR-0093).
362
- # The window moves only at midnight, so nothing expires in between.
362
+ # The window, and a flagged run's return, move only at midnight, so
363
+ # nothing expires in between.
363
364
  built_for = (now.astimezone(zone).date(), self.store.cost_flagged())
364
365
  if self._dirty or self._built is None or built_for != self._built_for:
365
366
  self._built = snapshot(
@@ -773,9 +774,9 @@ class CostPass:
773
774
 
774
775
  def _reliability_spans(session: SessionLike) -> frozenset[str]:
775
776
  """The spans the reliability rules cite in one session, from the
776
- deterministic stage alone, which is where all of them run."""
777
+ deterministic stage's `blocked-*` rules, which is where all of them run."""
777
778
  try:
778
- detections, _unknowns = run_deterministic(session)
779
+ detections = run_blocked(session)
779
780
  except Exception as error: # noqa: BLE001 - one odd session is not the pass
780
781
  logger.debug("cost: could not check %s for reliability findings: %s", session, error)
781
782
  return frozenset()
stacktrace/daemon/db.py CHANGED
@@ -391,6 +391,17 @@ CREATE TABLE IF NOT EXISTS account_cards (
391
391
  name_from TEXT,
392
392
  PRIMARY KEY (agent_kind, account)
393
393
  );
394
+ -- One account record per MCP server (ADR-0154): whether its latest witness
395
+ -- was a company or an outside account, and when. Never the address or the
396
+ -- domain. Additive, so `SCHEMA_VERSION` does not move (ADR-0057).
397
+ CREATE TABLE IF NOT EXISTS mcp_accounts (
398
+ agent_kind TEXT NOT NULL,
399
+ server TEXT NOT NULL,
400
+ account TEXT NOT NULL CHECK (account IN ('company', 'outside')),
401
+ witnessed_at TEXT NOT NULL,
402
+ domains_digest TEXT NOT NULL,
403
+ PRIMARY KEY (agent_kind, server)
404
+ );
394
405
  -- The marker of each rule a hook blocked a call or a prompt for, by the
395
406
  -- agent's own call id, or by the prompt key for a prompt. When the scan reads
396
407
  -- the transcript, it writes the finding from the marker and drops every other
@@ -1102,6 +1113,9 @@ _KNOWN_TABLE_COLUMNS: dict[str, frozenset[str]] = {
1102
1113
  "notified": frozenset({"event_id", "notified_at"}),
1103
1114
  "underbuilt": frozenset({"agent_kind", "session_id"}),
1104
1115
  "failed_reads": frozenset({"agent_kind", "path"}),
1116
+ "mcp_accounts": frozenset(
1117
+ {"agent_kind", "server", "account", "witnessed_at", "domains_digest"}
1118
+ ),
1105
1119
  "account_cards": frozenset(
1106
1120
  {"agent_kind", "account", "email_digest", "display_name", "plan", "seen_at", "name_from"}
1107
1121
  ),
@@ -17,7 +17,6 @@ than that socket's frame and carries the arguments its contract excludes.
17
17
  from __future__ import annotations
18
18
 
19
19
  import json
20
- import logging
21
20
  from collections.abc import Callable
22
21
  from datetime import UTC, datetime
23
22
  from http.server import ThreadingHTTPServer
@@ -30,6 +29,7 @@ from stacktrace.gateway.principal import Principal, session_key_id
30
29
  from stacktrace.gateway.service import Guard, loopback, make_server, serve_in_thread
31
30
  from stacktrace.gateway.sink import Recorder, Stamp
32
31
  from stacktrace.gateway.tetrate import EVALUATE_PATH, LOGS_PATH, guardrail, requestlog
32
+ from stacktrace.package_logger import get_logger
33
33
 
34
34
  from .accounts import email_digest
35
35
  from .config import DaemonConfig, parse_listen
@@ -38,7 +38,7 @@ from .usage import person_name
38
38
 
39
39
  __all__ = ["parse_listen", "start_gateway"]
40
40
 
41
- logger = logging.getLogger(__name__)
41
+ logger = get_logger(__name__)
42
42
 
43
43
 
44
44
  class _LogSink:
@@ -32,7 +32,6 @@ finding (ADR-0186).
32
32
  from __future__ import annotations
33
33
 
34
34
  import json
35
- import logging
36
35
  import os
37
36
  import re
38
37
  import threading
@@ -59,6 +58,7 @@ from stacktrace.gateway.marker import (
59
58
  )
60
59
  from stacktrace.gateway.session import AGENT_HEADER, SESSION_HEADER, build
61
60
  from stacktrace.hooks.claude import inspected
61
+ from stacktrace.package_logger import get_logger
62
62
 
63
63
  if TYPE_CHECKING:
64
64
  from collections.abc import Callable
@@ -67,7 +67,7 @@ if TYPE_CHECKING:
67
67
  from stacktrace.daemon.policy_runtime import Entry
68
68
  from stacktrace.daemon.store import SessionKey
69
69
 
70
- logger = logging.getLogger(__name__)
70
+ logger = get_logger(__name__)
71
71
 
72
72
  #: The name a pending MCP call is read under when its hook sent no tool name
73
73
  #: (ADR-0195). Its server and transport are set on the read call directly, so