stacktrace-cli 0.19.1__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.
- stacktrace/__init__.py +19 -7
- stacktrace/__main__.py +19 -3
- stacktrace/analysis.py +12 -1
- stacktrace/cli.py +31 -0
- stacktrace/correlate/observed.py +26 -1
- stacktrace/cost/baseline.py +39 -10
- stacktrace/cost/rules.py +32 -10
- stacktrace/daemon/activity_log.py +6 -4
- stacktrace/daemon/cli.py +2 -2
- stacktrace/daemon/composition.py +2 -2
- stacktrace/daemon/config.py +12 -0
- stacktrace/daemon/costs.py +29 -12
- stacktrace/daemon/db.py +14 -0
- stacktrace/daemon/frame_limits.py +11 -0
- stacktrace/daemon/gateway.py +2 -2
- stacktrace/daemon/inline_hook.py +61 -6
- stacktrace/daemon/mcp_records.py +180 -0
- stacktrace/daemon/observer.py +4 -2
- stacktrace/daemon/paths.py +2 -5
- stacktrace/daemon/policy_collect.py +10 -2
- stacktrace/daemon/policy_inputs.py +54 -12
- stacktrace/daemon/policy_result.py +38 -0
- stacktrace/daemon/policy_runtime.py +211 -36
- stacktrace/daemon/protocol.py +2 -5
- stacktrace/daemon/runtime.py +51 -17
- stacktrace/daemon/server.py +2 -2
- stacktrace/daemon/socket_path.py +26 -0
- stacktrace/daemon/store.py +3 -2
- stacktrace/daemon/unreadable.py +101 -0
- stacktrace/daemon/usage.py +12 -3
- stacktrace/detector/deterministic.py +2506 -197
- stacktrace/detector/guard_files.py +115 -0
- stacktrace/detector/hook_table.py +41 -0
- stacktrace/detector/inline.py +23 -2
- stacktrace/detector/installs.py +413 -0
- stacktrace/detector/known-crates.txt +9831 -0
- stacktrace/detector/known-npm.txt +15946 -0
- stacktrace/detector/known-pypi.txt +12401 -0
- stacktrace/detector/mcp_accounts.py +275 -0
- stacktrace/detector/persistence_files.py +183 -0
- stacktrace/detector/rules.py +191 -4
- stacktrace/detector/run.py +9 -4
- stacktrace/detector/secret_paths.py +206 -0
- stacktrace/detector/secret_refs.py +231 -0
- stacktrace/detector/wipers.py +494 -0
- stacktrace/finding_titles.py +11 -1
- stacktrace/gateway/analyzer.py +2 -2
- stacktrace/gateway/marker.py +36 -2
- stacktrace/gateway/service.py +2 -2
- stacktrace/gateway/sink.py +2 -2
- stacktrace/gateway/tetrate/guardrail.py +2 -2
- stacktrace/gateway/tetrate/requestlog.py +2 -2
- stacktrace/hooks/admit.py +102 -0
- stacktrace/hooks/claude.py +61 -28
- stacktrace/monitor/costs.py +7 -7
- stacktrace/monitor/server.py +1 -2
- stacktrace/monitor/site/app.js +5 -0
- stacktrace/package_logger.py +21 -0
- stacktrace/policy_display.py +5 -1
- stacktrace/remote/hook_policy.py +8 -12
- stacktrace/sessions/hook_journal.py +28 -1
- stacktrace/sessions/outcome.py +19 -0
- stacktrace/sessions/toolclass.py +13 -0
- stacktrace/telemetry/posthog.py +3 -2
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.21.0.dist-info}/METADATA +4 -4
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.21.0.dist-info}/RECORD +68 -49
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.21.0.dist-info}/WHEEL +0 -0
- {stacktrace_cli-0.19.1.dist-info → stacktrace_cli-0.21.0.dist-info}/entry_points.txt +0 -0
stacktrace/__init__.py
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
"""The `stacktrace` command-line interface — detection and response for AI agents."""
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
4
|
|
|
5
|
-
__version__ = "0.
|
|
5
|
+
__version__ = "0.21.0"
|
|
6
6
|
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
|
|
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.
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from stacktrace.sdk import ObservationClient
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def __getattr__(name: str) -> object:
|
|
16
|
+
# Loaded on first use: the SDK imports httpx and pydantic, and every
|
|
17
|
+
# `stacktrace` process imports this package, the per-call hook included
|
|
18
|
+
# (ADR-0194).
|
|
19
|
+
if name == "ObservationClient":
|
|
20
|
+
from stacktrace.sdk import ObservationClient
|
|
21
|
+
|
|
22
|
+
return ObservationClient
|
|
23
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
11
24
|
|
|
12
|
-
from stacktrace.sdk import ObservationClient
|
|
13
25
|
|
|
14
26
|
__all__ = ["ObservationClient", "__version__"]
|
stacktrace/__main__.py
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
"""Entry point shared by the ``stacktrace`` command and ``python -m stacktrace``.
|
|
2
2
|
|
|
3
|
-
The distribution's console script resolves to
|
|
4
|
-
``
|
|
3
|
+
The distribution's console script resolves to ``main`` here. It answers the
|
|
4
|
+
per-call hook command, ``remote policy hooks admit``, before the command line
|
|
5
|
+
loads (ADR-0194), and hands every other command to the Click group in
|
|
6
|
+
``stacktrace.cli``.
|
|
5
7
|
"""
|
|
6
8
|
|
|
7
9
|
from __future__ import annotations
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
import os
|
|
12
|
+
import sys
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main() -> None:
|
|
16
|
+
from stacktrace.hooks.admit import fast_kind, run_standalone
|
|
17
|
+
|
|
18
|
+
kind = fast_kind(sys.argv[1:], os.environ)
|
|
19
|
+
if kind is not None:
|
|
20
|
+
run_standalone(kind)
|
|
21
|
+
return
|
|
22
|
+
from stacktrace.cli import main as cli_main
|
|
23
|
+
|
|
24
|
+
cli_main()
|
|
25
|
+
|
|
10
26
|
|
|
11
27
|
__all__ = ["main"]
|
|
12
28
|
|
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(
|
stacktrace/correlate/observed.py
CHANGED
|
@@ -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
|
|
|
@@ -1594,7 +1613,7 @@ def command_segments_with_ops(
|
|
|
1594
1613
|
that preceded it (``None`` for the first segment, then ``&&``, ``||``,
|
|
1595
1614
|
``;``, ``|`` or ``\\n``).
|
|
1596
1615
|
|
|
1597
|
-
Only `
|
|
1616
|
+
Only `_resolve_wipes` in the deterministic detector needs the operator
|
|
1598
1617
|
context — to decide whether an inline ``cd`` reliably changed the directory
|
|
1599
1618
|
and whether an ``rm`` segment is unconditionally reachable. Every other
|
|
1600
1619
|
caller uses the plain `command_segments`, which discards the operator.
|
|
@@ -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
|
stacktrace/cost/baseline.py
CHANGED
|
@@ -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=
|
|
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
|
|
418
|
-
|
|
419
|
-
|
|
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
|
|
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
|
|
288
|
-
above their p95; at least 40M tokens above
|
|
289
|
-
judged against the first group of similar
|
|
290
|
-
which at least 10 runs reached it, and not
|
|
291
|
-
|
|
292
|
-
|
|
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
|
-
|
|
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
|
|
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]
|
|
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,12 +1,13 @@
|
|
|
1
1
|
"""The daemon's activity log: leveled, private and capped (ADR-0048).
|
|
2
2
|
|
|
3
|
-
Every module logs to its own `
|
|
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
|
|
|
7
7
|
A line names kinds, counts, class names and event ids. It never carries a
|
|
8
8
|
finding's title or body, an exception message, or anything from a transcript,
|
|
9
|
-
and a peer-supplied session id is written with `%r`.
|
|
9
|
+
and a peer-supplied session id is written with `%r`. The one exception is a
|
|
10
|
+
reader's failure text, written only through `unreadable.describe` (ADR-0191).
|
|
10
11
|
"""
|
|
11
12
|
|
|
12
13
|
from __future__ import annotations
|
|
@@ -19,6 +20,7 @@ from collections.abc import Mapping
|
|
|
19
20
|
from logging.handlers import RotatingFileHandler
|
|
20
21
|
from pathlib import Path
|
|
21
22
|
|
|
23
|
+
from stacktrace.package_logger import get_logger
|
|
22
24
|
from stacktrace.private_state import ensure_private_directory
|
|
23
25
|
|
|
24
26
|
LEVEL_VARIABLE = "STACKTRACE_DAEMON_LOG_LEVEL"
|
|
@@ -81,14 +83,14 @@ def configure(
|
|
|
81
83
|
)
|
|
82
84
|
formatter.converter = time.gmtime
|
|
83
85
|
handler.setFormatter(formatter)
|
|
84
|
-
logger =
|
|
86
|
+
logger = get_logger(PACKAGE)
|
|
85
87
|
logger.setLevel(level_from(os.environ if environ is None else environ))
|
|
86
88
|
logger.addHandler(handler)
|
|
87
89
|
return handler
|
|
88
90
|
|
|
89
91
|
|
|
90
92
|
def release(handler: logging.Handler) -> None:
|
|
91
|
-
logger =
|
|
93
|
+
logger = get_logger(PACKAGE)
|
|
92
94
|
logger.removeHandler(handler)
|
|
93
95
|
logger.setLevel(logging.NOTSET)
|
|
94
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 =
|
|
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)"
|
stacktrace/daemon/composition.py
CHANGED
|
@@ -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 =
|
|
16
|
+
logger = get_logger(__name__)
|
|
17
17
|
|
|
18
18
|
Scope = tuple[str, Path | None]
|
|
19
19
|
Key = tuple[str, str | None]
|
stacktrace/daemon/config.py
CHANGED
|
@@ -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")
|
stacktrace/daemon/costs.py
CHANGED
|
@@ -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
|
|
@@ -55,16 +54,19 @@ from stacktrace.cost.rules import (
|
|
|
55
54
|
to_detection,
|
|
56
55
|
)
|
|
57
56
|
from stacktrace.cost.runs import Run, runs_of
|
|
57
|
+
from stacktrace.daemon.jobs import Backoff
|
|
58
58
|
from stacktrace.daemon.observer import Collected
|
|
59
59
|
from stacktrace.daemon.store import FindingStore, Notification, SessionKey
|
|
60
|
-
from stacktrace.
|
|
60
|
+
from stacktrace.daemon.unreadable import OncePerChange, describe
|
|
61
|
+
from stacktrace.detector.deterministic import run_blocked
|
|
61
62
|
from stacktrace.detector.render import detection_json
|
|
62
63
|
from stacktrace.detector.rules import COST_RULES, RELIABILITY_RULES
|
|
64
|
+
from stacktrace.package_logger import get_logger
|
|
63
65
|
from stacktrace.sessions.access import SessionView, collect_sessions
|
|
64
66
|
from stacktrace.sessions.lineage import parent_session_id
|
|
65
67
|
from stacktrace.sessions.protocols import SessionLike
|
|
66
68
|
|
|
67
|
-
logger =
|
|
69
|
+
logger = get_logger(__name__)
|
|
68
70
|
|
|
69
71
|
#: A changed sub-agent transcript is looked for back to the last walk, less
|
|
70
72
|
#: this much, so a file written during the walk is not missed.
|
|
@@ -357,7 +359,8 @@ class Baseline:
|
|
|
357
359
|
# Rebuilt when a row is written, and also when the local day turns
|
|
358
360
|
# (the window moves) or a run gains a cost finding (flagged runs
|
|
359
361
|
# leave the baseline, ADR-0093).
|
|
360
|
-
# The window
|
|
362
|
+
# The window, and a flagged run's return, move only at midnight, so
|
|
363
|
+
# nothing expires in between.
|
|
361
364
|
built_for = (now.astimezone(zone).date(), self.store.cost_flagged())
|
|
362
365
|
if self._dirty or self._built is None or built_for != self._built_for:
|
|
363
366
|
self._built = snapshot(
|
|
@@ -558,6 +561,14 @@ class CostPass:
|
|
|
558
561
|
#: warm-up that failed or could not reach every session must retry on
|
|
559
562
|
#: a later tick rather than being read as its one chance.
|
|
560
563
|
self._warmed = False
|
|
564
|
+
#: When a warm-up that failed may read again: 15 s on, doubling,
|
|
565
|
+
#: never past an hour (cost-phase-1 §3). A source that stays
|
|
566
|
+
#: unreadable, such as a Devin store newer than its reader, otherwise
|
|
567
|
+
#: had 36 hours re-read on every tick for as long as the daemon ran.
|
|
568
|
+
self._warm_retries = Backoff(ceiling=3600.0, first=15.0)
|
|
569
|
+
self._warm_after: datetime | None = None
|
|
570
|
+
#: The same failures are logged once, and again when they change.
|
|
571
|
+
self._unreadable = OncePerChange()
|
|
561
572
|
#: The earliest write this pass was handed for each run, main session
|
|
562
573
|
#: and sub-agents alike, by main session: where a finding's clock may
|
|
563
574
|
#: start.
|
|
@@ -592,7 +603,7 @@ class CostPass:
|
|
|
592
603
|
# Every kind's sessions and every member of a run, sub-agents
|
|
593
604
|
# included: the scanner reads them all (ADR-0107).
|
|
594
605
|
changed = [session for item in collected for session in item.view.sessions]
|
|
595
|
-
if not self._warmed:
|
|
606
|
+
if not self._warmed and (self._warm_after is None or now >= self._warm_after):
|
|
596
607
|
changed.extend(self._warm(now, {_key(s) for s in changed}))
|
|
597
608
|
self._written = {}
|
|
598
609
|
for item in collected:
|
|
@@ -719,17 +730,23 @@ class CostPass:
|
|
|
719
730
|
"""Every session active in the last 36 hours that the scanner did not
|
|
720
731
|
hand over, read cold after a start: the scanner resumes from its
|
|
721
732
|
watermark, so a run quiet since then would otherwise be judged short of
|
|
722
|
-
its members, whichever of them resumed it (cost-phase-1 §3). Retried
|
|
723
|
-
|
|
724
|
-
a transient failure does not cost a run's
|
|
725
|
-
being backfilled
|
|
733
|
+
its members, whichever of them resumed it (cost-phase-1 §3). Retried
|
|
734
|
+
15 s later when the read raised or could not reach every session, the
|
|
735
|
+
wait doubling to an hour, so a transient failure does not cost a run's
|
|
736
|
+
members their one chance at being backfilled, and a source that never
|
|
737
|
+
becomes readable is not read again on every tick."""
|
|
726
738
|
try:
|
|
727
739
|
view = self._cold_read(now - _WARM_WINDOW)
|
|
728
740
|
except Exception as error: # noqa: BLE001 - a failed warm-up is not the pass
|
|
729
741
|
logger.debug("cost: could not read the last 36 hours: %s", error)
|
|
742
|
+
self._warm_after = now + timedelta(seconds=self._warm_retries.next())
|
|
730
743
|
return []
|
|
731
744
|
if view.unavailable:
|
|
732
|
-
|
|
745
|
+
self._warm_after = now + timedelta(seconds=self._warm_retries.next())
|
|
746
|
+
if self._unreadable.due(view.unavailable):
|
|
747
|
+
logger.debug(
|
|
748
|
+
"cost: some sessions could not be read: %s", describe(view.unavailable)
|
|
749
|
+
)
|
|
733
750
|
return [session for session in view.sessions if _key(session) not in have]
|
|
734
751
|
self._warmed = True
|
|
735
752
|
return [session for session in view.sessions if _key(session) not in have]
|
|
@@ -757,9 +774,9 @@ class CostPass:
|
|
|
757
774
|
|
|
758
775
|
def _reliability_spans(session: SessionLike) -> frozenset[str]:
|
|
759
776
|
"""The spans the reliability rules cite in one session, from the
|
|
760
|
-
deterministic stage
|
|
777
|
+
deterministic stage's `blocked-*` rules, which is where all of them run."""
|
|
761
778
|
try:
|
|
762
|
-
detections
|
|
779
|
+
detections = run_blocked(session)
|
|
763
780
|
except Exception as error: # noqa: BLE001 - one odd session is not the pass
|
|
764
781
|
logger.debug("cost: could not check %s for reliability findings: %s", session, error)
|
|
765
782
|
return frozenset()
|