tempergraph 0.7.1__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.
- tempergraph/__init__.py +8 -0
- tempergraph/__main__.py +5 -0
- tempergraph/api.py +428 -0
- tempergraph/audit/__init__.py +305 -0
- tempergraph/audit/catalog.py +342 -0
- tempergraph/audit/definition.py +387 -0
- tempergraph/audit/history.py +414 -0
- tempergraph/audit/history_rules.py +268 -0
- tempergraph/audit/history_rules_more.py +244 -0
- tempergraph/audit/host.py +270 -0
- tempergraph/audit/owned.py +96 -0
- tempergraph/audit/prompts.py +89 -0
- tempergraph/audit/report.py +248 -0
- tempergraph/audit/revisit.py +220 -0
- tempergraph/audit/surface.py +224 -0
- tempergraph/audit/text_health.py +176 -0
- tempergraph/audit/tools.py +391 -0
- tempergraph/author/__init__.py +2 -0
- tempergraph/author/blueprint.py +346 -0
- tempergraph/author/compare.py +122 -0
- tempergraph/author/criteria_lint.py +200 -0
- tempergraph/author/draw.py +112 -0
- tempergraph/author/inventory.py +210 -0
- tempergraph/author/literals.py +138 -0
- tempergraph/author/scaffold.py +568 -0
- tempergraph/author/session.py +439 -0
- tempergraph/author/trial.py +391 -0
- tempergraph/author/verify.py +353 -0
- tempergraph/builtin/__init__.py +1 -0
- tempergraph/builtin/upgrade.py +288 -0
- tempergraph/catalog.py +134 -0
- tempergraph/check_helpers.py +99 -0
- tempergraph/cli/__init__.py +1 -0
- tempergraph/cli/audit.py +190 -0
- tempergraph/cli/author.py +361 -0
- tempergraph/cli/improve.py +564 -0
- tempergraph/cli/improve_review.py +111 -0
- tempergraph/cli/install.py +35 -0
- tempergraph/cli/main.py +1261 -0
- tempergraph/cli/observe_cli.py +544 -0
- tempergraph/cli/outcome.py +77 -0
- tempergraph/cli/schedule_cli.py +1090 -0
- tempergraph/cli/upgrade.py +126 -0
- tempergraph/cli/workflows.py +96 -0
- tempergraph/compiler/__init__.py +6 -0
- tempergraph/compiler/graph.py +305 -0
- tempergraph/compiler/state.py +229 -0
- tempergraph/constants.py +72 -0
- tempergraph/contracts.py +1025 -0
- tempergraph/data/executor_compat.json +11 -0
- tempergraph/data/promotion.json +35 -0
- tempergraph/data/release.json +20 -0
- tempergraph/detach.py +65 -0
- tempergraph/docs.py +90 -0
- tempergraph/durability/__init__.py +2 -0
- tempergraph/durability/alias.py +66 -0
- tempergraph/durability/carry.py +591 -0
- tempergraph/durability/checkpoint.py +179 -0
- tempergraph/durability/control.py +109 -0
- tempergraph/durability/engine_file.py +301 -0
- tempergraph/durability/lock.py +138 -0
- tempergraph/durability/status.py +390 -0
- tempergraph/engine_identity.py +42 -0
- tempergraph/executor_compat.py +56 -0
- tempergraph/executors/__init__.py +4 -0
- tempergraph/executors/binding.py +91 -0
- tempergraph/executors/capabilities.py +95 -0
- tempergraph/executors/claude.py +603 -0
- tempergraph/executors/launcher.py +394 -0
- tempergraph/executors/mcpconfig.py +156 -0
- tempergraph/executors/mcpproxy.py +216 -0
- tempergraph/executors/preflight.py +196 -0
- tempergraph/executors/registry.py +71 -0
- tempergraph/executors/scripted.py +18 -0
- tempergraph/executors/session.py +107 -0
- tempergraph/executors/standins.py +118 -0
- tempergraph/executors/summary.py +110 -0
- tempergraph/executors/viewcatalog.py +27 -0
- tempergraph/git.py +403 -0
- tempergraph/identity.py +65 -0
- tempergraph/improve/__init__.py +5 -0
- tempergraph/improve/analysis.py +437 -0
- tempergraph/improve/artifact.py +477 -0
- tempergraph/improve/autonomy.py +192 -0
- tempergraph/improve/bindings.py +191 -0
- tempergraph/improve/calibration.py +1016 -0
- tempergraph/improve/causes.py +416 -0
- tempergraph/improve/child.py +651 -0
- tempergraph/improve/codehost.py +520 -0
- tempergraph/improve/digest.py +472 -0
- tempergraph/improve/escape.py +100 -0
- tempergraph/improve/filing.py +1047 -0
- tempergraph/improve/harvest.py +158 -0
- tempergraph/improve/issue_context.py +192 -0
- tempergraph/improve/lenses.py +204 -0
- tempergraph/improve/lessons.py +233 -0
- tempergraph/improve/loop_report.py +138 -0
- tempergraph/improve/loop_summary.py +85 -0
- tempergraph/improve/lost.py +119 -0
- tempergraph/improve/measure.py +121 -0
- tempergraph/improve/params.py +98 -0
- tempergraph/improve/pause_facts.py +134 -0
- tempergraph/improve/pr_later.py +186 -0
- tempergraph/improve/prebinding.py +130 -0
- tempergraph/improve/promotion.py +413 -0
- tempergraph/improve/proposals.py +934 -0
- tempergraph/improve/record.py +322 -0
- tempergraph/improve/release.py +89 -0
- tempergraph/improve/reliability.py +457 -0
- tempergraph/improve/repeat.py +115 -0
- tempergraph/improve/review.py +910 -0
- tempergraph/improve/suites.py +586 -0
- tempergraph/improve/trials.py +525 -0
- tempergraph/install/__init__.py +11 -0
- tempergraph/install/bootstrap.py +133 -0
- tempergraph/install/environment.py +173 -0
- tempergraph/install/launch.py +37 -0
- tempergraph/install/python.py +53 -0
- tempergraph/install/reconcile.py +52 -0
- tempergraph/install/selfcheck.py +29 -0
- tempergraph/install/shim.py +81 -0
- tempergraph/install/update.py +400 -0
- tempergraph/model/__init__.py +0 -0
- tempergraph/model/diagram.py +185 -0
- tempergraph/model/flow.py +324 -0
- tempergraph/model/prompt_lint.py +203 -0
- tempergraph/model/retire.py +52 -0
- tempergraph/model/subworkflow.py +274 -0
- tempergraph/model/text.py +67 -0
- tempergraph/model/validate.py +1158 -0
- tempergraph/observe/__init__.py +1 -0
- tempergraph/observe/attention.py +196 -0
- tempergraph/observe/detail.py +773 -0
- tempergraph/observe/forget.py +138 -0
- tempergraph/observe/hooks.py +272 -0
- tempergraph/observe/labels.py +95 -0
- tempergraph/observe/overlay.py +46 -0
- tempergraph/observe/report.py +402 -0
- tempergraph/observe/risk_report.py +110 -0
- tempergraph/observe/rows.py +444 -0
- tempergraph/observe/sinks.py +793 -0
- tempergraph/observe/supervision_cost.py +797 -0
- tempergraph/observe/supervision_report.py +295 -0
- tempergraph/observe/switch_report.py +140 -0
- tempergraph/package/__init__.py +2 -0
- tempergraph/package/change_report.py +116 -0
- tempergraph/package/changelog.py +263 -0
- tempergraph/package/docs_check.py +115 -0
- tempergraph/package/importer.py +90 -0
- tempergraph/package/inventory.py +111 -0
- tempergraph/package/loader.py +250 -0
- tempergraph/package/manifest.py +256 -0
- tempergraph/package/readiness.py +124 -0
- tempergraph/package/registry.py +383 -0
- tempergraph/package/rules.py +128 -0
- tempergraph/package/scan.py +65 -0
- tempergraph/package/selftest.py +348 -0
- tempergraph/package/stamp.py +213 -0
- tempergraph/package/transforms.py +481 -0
- tempergraph/package/upgrade.py +625 -0
- tempergraph/provenance.py +183 -0
- tempergraph/py.typed +0 -0
- tempergraph/release.py +17 -0
- tempergraph/render/__init__.py +0 -0
- tempergraph/render/output.py +72 -0
- tempergraph/render/prompt.py +166 -0
- tempergraph/render/template.py +86 -0
- tempergraph/run/__init__.py +2 -0
- tempergraph/run/asking.py +233 -0
- tempergraph/run/audit.py +63 -0
- tempergraph/run/check_worker.py +167 -0
- tempergraph/run/checks.py +517 -0
- tempergraph/run/commands.py +210 -0
- tempergraph/run/complexity.py +268 -0
- tempergraph/run/contest.py +76 -0
- tempergraph/run/council.py +368 -0
- tempergraph/run/council_run.py +527 -0
- tempergraph/run/events.py +279 -0
- tempergraph/run/finalize.py +132 -0
- tempergraph/run/gate.py +747 -0
- tempergraph/run/guard.py +89 -0
- tempergraph/run/judging.py +357 -0
- tempergraph/run/ladder.py +230 -0
- tempergraph/run/log.py +600 -0
- tempergraph/run/mapping.py +183 -0
- tempergraph/run/nodes.py +2304 -0
- tempergraph/run/outcome.py +255 -0
- tempergraph/run/outcome_record.py +292 -0
- tempergraph/run/outcome_trust.py +99 -0
- tempergraph/run/rerun.py +351 -0
- tempergraph/run/reserve.py +85 -0
- tempergraph/run/resources.py +76 -0
- tempergraph/run/risk.py +231 -0
- tempergraph/run/runner.py +1579 -0
- tempergraph/run/scriptcache.py +178 -0
- tempergraph/run/selfcheck.py +105 -0
- tempergraph/run/session_record.py +72 -0
- tempergraph/run/subrun.py +278 -0
- tempergraph/run/supervision.py +125 -0
- tempergraph/run/tool_access.py +101 -0
- tempergraph/schedule/__init__.py +25 -0
- tempergraph/schedule/credentials.py +167 -0
- tempergraph/schedule/echo.py +169 -0
- tempergraph/schedule/exact.py +275 -0
- tempergraph/schedule/export.py +164 -0
- tempergraph/schedule/launch.py +89 -0
- tempergraph/schedule/ledger.py +133 -0
- tempergraph/schedule/loop.py +176 -0
- tempergraph/schedule/model.py +440 -0
- tempergraph/schedule/policies.py +414 -0
- tempergraph/schedule/store.py +150 -0
- tempergraph/schedule/tick.py +377 -0
- tempergraph/schedule/token.py +81 -0
- tempergraph/schedule/triggers.py +210 -0
- tempergraph/schedule/words.py +423 -0
- tempergraph/security/__init__.py +0 -0
- tempergraph/security/launch.py +25 -0
- tempergraph/security/scrub.py +58 -0
- tempergraph/settings.py +950 -0
- tempergraph/store/__init__.py +0 -0
- tempergraph/store/codelog.py +73 -0
- tempergraph/store/layout.py +198 -0
- tempergraph/store/logs.py +325 -0
- tempergraph/store/manifest.py +99 -0
- tempergraph/store/migrate.py +188 -0
- tempergraph/workspace.py +874 -0
- tempergraph-0.7.1.dist-info/METADATA +70 -0
- tempergraph-0.7.1.dist-info/RECORD +232 -0
- tempergraph-0.7.1.dist-info/WHEEL +5 -0
- tempergraph-0.7.1.dist-info/entry_points.txt +5 -0
- tempergraph-0.7.1.dist-info/top_level.txt +2 -0
- tempergraph_pytest_plugin.py +64 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"""`tempergraph audit` (AUD, ARC, TSA, PRA): advisory, read-only, model-free (OVR-B15).
|
|
2
|
+
|
|
3
|
+
Release 3 runs the `definition`, `host` and `history` layers, the tool-surface audit and the prompt audit. The
|
|
4
|
+
`history` layer reads the workflow's local run records once (`audit.history`) and every history rule reads those
|
|
5
|
+
facts only; a rule with fewer runs than its `min_n` is listed as skipped with `n=<runs> of <min>`, a layer with no
|
|
6
|
+
runs as skipped with `no runs in window`, never as passed (AUD-B1, AUD-B5). Owned checks (validation, prompt lint,
|
|
7
|
+
the readiness report, executor compatibility, the cost lens and the engine's own findings) are run through their
|
|
8
|
+
component and rendered under `<owner>:<code>` (ADR `audit-joins-existing-checks-and-files-nothing`, AUD-B8).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import datetime as dt
|
|
14
|
+
from collections.abc import Callable, Mapping
|
|
15
|
+
from dataclasses import dataclass, field
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
from tempergraph.audit import revisit
|
|
20
|
+
from tempergraph.audit.catalog import RULES, Layer
|
|
21
|
+
from tempergraph.audit.definition import Definition
|
|
22
|
+
from tempergraph.audit.history import HistoryFacts
|
|
23
|
+
from tempergraph.audit.history_rules import HISTORY_C1_C3, RuleSkipped, hot_spots
|
|
24
|
+
from tempergraph.audit.history_rules_more import HISTORY_C5_C8, SAMPLE_N
|
|
25
|
+
from tempergraph.audit.host import Host, HostInputs, ShellTiming, shell_timing
|
|
26
|
+
from tempergraph.audit.owned import HISTORY_OWNED
|
|
27
|
+
from tempergraph.audit.report import AuditFinding, AuditReport, Ignore, apply_ignores, order, parse_ignores
|
|
28
|
+
from tempergraph.audit.surface import build_surface
|
|
29
|
+
from tempergraph.audit.tools import TOOL_HISTORY, ToolRules, run_history
|
|
30
|
+
from tempergraph.constants import ENGINE_VERSION
|
|
31
|
+
from tempergraph.contracts import Capabilities, WorkflowSpec
|
|
32
|
+
from tempergraph.model.validate import Finding, validate
|
|
33
|
+
|
|
34
|
+
BUILT_LAYERS: frozenset[Layer] = frozenset({"definition", "host", "history"})
|
|
35
|
+
CONFIDENCE = {"lint-vague-word": "medium", "lint-rule-branching": "medium", "lint-human-wait": "medium",
|
|
36
|
+
"lint-self-grading": "medium", "lint-accepting-brief": "medium"}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(kw_only=True)
|
|
40
|
+
class AuditContext:
|
|
41
|
+
target: Path
|
|
42
|
+
caps: Mapping[str, Capabilities]
|
|
43
|
+
probe: Callable[[str], str | None] = lambda name: None
|
|
44
|
+
timing: Callable[[float], ShellTiming | None] = shell_timing
|
|
45
|
+
which: Callable[[str], str | None] | None = None
|
|
46
|
+
plugin_cache: Path | None = None
|
|
47
|
+
window_days: int = 30
|
|
48
|
+
version: str = "current"
|
|
49
|
+
executor: str | None = None
|
|
50
|
+
steps: tuple[str, ...] = ()
|
|
51
|
+
show_all: bool = False
|
|
52
|
+
today: dt.date = field(default_factory=dt.date.today)
|
|
53
|
+
history: HistoryFacts | None = None # the facts of the runs in the window and version (AUD-B3, AUD-B11)
|
|
54
|
+
tool_executor: str | None = None # show every session on this executor's tool map (`audit tools --executor`)
|
|
55
|
+
mcp_servers: Callable[[], set[str] | None] | None = None # the MCP servers of the operator's own configuration
|
|
56
|
+
ambient_tools: Callable[[str], list[str] | None] | None = None # plugin tools a probe lists for an executor
|
|
57
|
+
issue_state: Callable[[int], str | None] | None = None # `open` or `closed` of an issue (DRV-B2)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _decl_file(spec: WorkflowSpec, manifest: Any) -> str:
|
|
61
|
+
root = spec.package_root
|
|
62
|
+
if root is None or manifest is None:
|
|
63
|
+
return "workflow"
|
|
64
|
+
mod = str(manifest.entry).partition(":")[0]
|
|
65
|
+
for rel in (f"workflow/{mod}/__init__.py", f"workflow/{mod}.py"):
|
|
66
|
+
if (root / rel).is_file():
|
|
67
|
+
return rel
|
|
68
|
+
return f"workflow/{mod}"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _owned(found: list[Finding]) -> list[AuditFinding]:
|
|
72
|
+
out = []
|
|
73
|
+
for f in found:
|
|
74
|
+
lint = f.code.startswith("lint-")
|
|
75
|
+
owner = "prompt-lint" if lint else "validate"
|
|
76
|
+
where, _, message = f.message.partition(": ") if lint else ("", "", f.message)
|
|
77
|
+
file, _, line = where.partition(":")
|
|
78
|
+
loc: dict[str, Any] = {}
|
|
79
|
+
if file and not file.startswith("inline"):
|
|
80
|
+
loc["file"] = file
|
|
81
|
+
if line.isdigit():
|
|
82
|
+
loc["line"] = int(line)
|
|
83
|
+
out.append(AuditFinding(rule=f"{owner}:{f.code}", layer="definition", severity=f.severity,
|
|
84
|
+
confidence=CONFIDENCE.get(f.code, "high"), unit=f.unit, owner=owner,
|
|
85
|
+
statement=message or f.message, location=loc,
|
|
86
|
+
remedy="See the owning check's rule" + (" (LIF-B12)" if lint else " (WFM-B12)")))
|
|
87
|
+
return out
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _readiness(spec: WorkflowSpec, already: set[str | None]) -> list[AuditFinding]:
|
|
91
|
+
from tempergraph.package.readiness import readiness
|
|
92
|
+
|
|
93
|
+
out = []
|
|
94
|
+
for g in readiness(spec):
|
|
95
|
+
if g.grade == "ready" or g.gate in already: # stable mode: validation's `gate-not-ready` stands
|
|
96
|
+
continue
|
|
97
|
+
sev = "warning" if g.grade == "needs work" else "info"
|
|
98
|
+
out.append(AuditFinding(rule=f"readiness:{g.grade.replace(' ', '-')}", layer="definition", severity=sev,
|
|
99
|
+
confidence="high", unit=g.gate, owner="readiness",
|
|
100
|
+
statement=f"judge readiness of {g.gate}: {g.grade}"
|
|
101
|
+
+ (f" ({'; '.join(g.findings)})" if g.findings else ""),
|
|
102
|
+
evidence={"criteria": g.criteria, "blockers": g.blockers,
|
|
103
|
+
"calibration_cases": g.calibration_cases, "acceptance": g.acceptance},
|
|
104
|
+
remedy="Fix the criteria the report names; calibrate the judge (LIF-B13, JCL)"))
|
|
105
|
+
return out
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def audit(spec: WorkflowSpec, layers: set[Layer], ctx: AuditContext, *, manifest: Any = None,
|
|
109
|
+
stamp: dict[str, Any] | None = None) -> AuditReport:
|
|
110
|
+
"""AUD-B11, AUD-B6, AUD-B9: pure over its inputs, apart from the host layer's read-only probes."""
|
|
111
|
+
rep = AuditReport(workflow=spec.name, version=spec.version, engine_version=ENGINE_VERSION,
|
|
112
|
+
window_days=ctx.window_days, show_all=ctx.show_all)
|
|
113
|
+
found = validate(spec, ctx.caps)
|
|
114
|
+
broken = any(f.severity == "error" for f in found)
|
|
115
|
+
owned = _owned(found)
|
|
116
|
+
owned += _readiness(spec, {f.unit for f in found if f.code == "gate-not-ready"})
|
|
117
|
+
findings = list(owned)
|
|
118
|
+
decl = _decl_file(spec, manifest)
|
|
119
|
+
run: list[Layer] = []
|
|
120
|
+
why: dict[str, str] = {}
|
|
121
|
+
facts = ctx.history
|
|
122
|
+
if facts is not None:
|
|
123
|
+
rep.runs, rep.unreadable_runs = facts.runs, facts.unreadable
|
|
124
|
+
for layer in ("definition", "host", "history"):
|
|
125
|
+
if layer not in layers:
|
|
126
|
+
why[layer] = "--layer not given"
|
|
127
|
+
elif layer not in BUILT_LAYERS:
|
|
128
|
+
why[layer] = "not in this release"
|
|
129
|
+
elif broken and layer != "definition":
|
|
130
|
+
why[layer] = "the workflow does not validate"
|
|
131
|
+
elif layer == "history" and (facts is None or facts.runs == 0):
|
|
132
|
+
why[layer] = "no runs in window"
|
|
133
|
+
else:
|
|
134
|
+
run.append(layer)
|
|
135
|
+
continue
|
|
136
|
+
rep.layers_skipped.append({"layer": layer, "reason": why[layer]})
|
|
137
|
+
rep.layers_run = list(run)
|
|
138
|
+
tools = _tools(spec, ctx, rep, run, manifest, decl, stamp) # the session surface, built once per audit
|
|
139
|
+
if "definition" in run:
|
|
140
|
+
findings += _guarded(rep, "definition", lambda: Definition(spec, decl, dict(getattr(manifest, "surfaces", {})
|
|
141
|
+
or {}), ctx.caps).run())
|
|
142
|
+
if tools is not None:
|
|
143
|
+
findings += _guarded(rep, "tools definition", tools.definition)
|
|
144
|
+
findings += _guarded(rep, "decision revisit", lambda: revisit.findings(spec, stamp, readers(ctx)))
|
|
145
|
+
if "host" in run:
|
|
146
|
+
inputs = HostInputs(target=ctx.target, probe=ctx.probe, timing=ctx.timing, plugin_cache=ctx.plugin_cache,
|
|
147
|
+
stamp=stamp)
|
|
148
|
+
if ctx.which is not None:
|
|
149
|
+
inputs.which = ctx.which
|
|
150
|
+
findings += _guarded(rep, "host", lambda: Host(spec, decl, inputs).run())
|
|
151
|
+
rep.rules_skipped += inputs.skipped
|
|
152
|
+
if tools is not None:
|
|
153
|
+
findings += _guarded(rep, "tools host", tools.host)
|
|
154
|
+
rep.rules_skipped += tools.skipped
|
|
155
|
+
if "history" in run and facts is not None:
|
|
156
|
+
hist = _history_layer(spec, ctx, rep, facts, tools)
|
|
157
|
+
findings += hist
|
|
158
|
+
owned += [f for f in hist if f.owner != "audit"]
|
|
159
|
+
for r in RULES:
|
|
160
|
+
if r.layer not in run:
|
|
161
|
+
entry: dict[str, Any] = {"rule": r.id, "reason": why.get(r.layer, "--layer not given")}
|
|
162
|
+
if r.min_n is not None:
|
|
163
|
+
entry["min_n"] = r.min_n
|
|
164
|
+
rep.rules_skipped.append(entry)
|
|
165
|
+
elif r.id in _NOT_BUILT:
|
|
166
|
+
rep.rules_skipped.append({"rule": r.id, "reason": _NOT_BUILT[r.id]})
|
|
167
|
+
findings = _narrow(spec, findings, ctx)
|
|
168
|
+
findings = _dedupe(findings)
|
|
169
|
+
ignores, _problems = parse_ignores(dict(getattr(manifest, "audit", {}) or {}).get("ignore"))
|
|
170
|
+
findings += apply_ignores(findings, ignores, ctx.today)
|
|
171
|
+
rep.findings = order(findings, spec.order)
|
|
172
|
+
rep.owned = _owned_counts(owned)
|
|
173
|
+
return rep
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def readers(ctx: AuditContext) -> revisit.Readers:
|
|
177
|
+
"""DRV-B2: where the audit reads a revisit trigger's state."""
|
|
178
|
+
return revisit.Readers(issue=ctx.issue_state or (lambda n: None),
|
|
179
|
+
claude_version=lambda: ctx.probe("claude"), engine_version=ENGINE_VERSION)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
_NOT_BUILT = { # definition rules whose unit kinds the engine does not build yet (ARC-B15)
|
|
183
|
+
"unbounded-wait": "kind not in release 1 (wait and ask are release 2)",
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _tools(spec: WorkflowSpec, ctx: AuditContext, rep: AuditReport, run: list[Layer], manifest: Any, decl: str,
|
|
188
|
+
stamp: dict[str, Any] | None) -> ToolRules | None:
|
|
189
|
+
"""TSA-B6: the session surface, built once from the tool maps, the manifest and (with history) the recorded use,
|
|
190
|
+
shared by the C4 rules and by `audit tools`."""
|
|
191
|
+
if not run:
|
|
192
|
+
return None
|
|
193
|
+
try:
|
|
194
|
+
surface = build_surface(spec, ctx.caps, manifest, ctx.history if "history" in run else None,
|
|
195
|
+
executor=ctx.tool_executor)
|
|
196
|
+
except Exception as exc: # noqa: BLE001 - AUD-B15: the rules that need the surface are skipped
|
|
197
|
+
import logging
|
|
198
|
+
|
|
199
|
+
logging.getLogger("tempergraph").warning("audit surface error: %s", type(exc).__name__)
|
|
200
|
+
rep.rules_skipped.append({"rule": "tool surface", "reason": f"rule error ({type(exc).__name__})"})
|
|
201
|
+
return None
|
|
202
|
+
rep.surface = surface
|
|
203
|
+
return ToolRules(spec, surface, ctx.caps, decl, stamp=stamp, plugin_cache=ctx.plugin_cache,
|
|
204
|
+
mcp_servers=ctx.mcp_servers, ambient_tools=ctx.ambient_tools)
|
|
205
|
+
|
|
206
|
+
HistoryRule = Callable[[WorkflowSpec, HistoryFacts], list[AuditFinding] | None]
|
|
207
|
+
# rule id -> its function; None back means the owner is not present
|
|
208
|
+
HISTORY_RULES: dict[str, HistoryRule] = {**HISTORY_C1_C3, **HISTORY_C5_C8, **HISTORY_OWNED}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def sample_n(rule_id: str, facts: HistoryFacts) -> int:
|
|
212
|
+
"""The sample a history rule counts: the runs in the window, or the records of its own shape (AUD-B5)."""
|
|
213
|
+
count = SAMPLE_N.get(rule_id)
|
|
214
|
+
return count(facts) if count is not None else facts.runs
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _history_layer(spec: WorkflowSpec, ctx: AuditContext, rep: AuditReport, facts: HistoryFacts,
|
|
218
|
+
tools: ToolRules | None = None) -> list[AuditFinding]:
|
|
219
|
+
"""AUD-B3, AUD-B5, ARC-B7: every history rule on the facts; below `min_n` it is skipped with `n=<runs> of <min>`."""
|
|
220
|
+
out: list[AuditFinding] = []
|
|
221
|
+
for r in RULES:
|
|
222
|
+
if r.layer != "history":
|
|
223
|
+
continue
|
|
224
|
+
n, need = sample_n(r.id, facts), r.min_n or 1
|
|
225
|
+
if n < need:
|
|
226
|
+
rep.rules_skipped.append({"rule": r.id, "reason": f"n={n} of {need}", "min_n": need})
|
|
227
|
+
continue
|
|
228
|
+
fn: HistoryRule | None = HISTORY_RULES.get(r.id)
|
|
229
|
+
if r.id in TOOL_HISTORY:
|
|
230
|
+
if tools is None:
|
|
231
|
+
rep.rules_skipped.append({"rule": r.id, "reason": "tool surface not built", "min_n": need})
|
|
232
|
+
continue
|
|
233
|
+
fn = lambda _s, f, rid=r.id: run_history(tools, rid, f) # noqa: E731
|
|
234
|
+
if fn is None:
|
|
235
|
+
rep.rules_skipped.append({"rule": r.id, "reason": "not built", "min_n": need})
|
|
236
|
+
continue
|
|
237
|
+
try:
|
|
238
|
+
got = fn(spec, facts)
|
|
239
|
+
except RuleSkipped as why:
|
|
240
|
+
rep.rules_skipped.append({"rule": r.id, "reason": why.reason, "min_n": need})
|
|
241
|
+
continue
|
|
242
|
+
except Exception as exc: # noqa: BLE001 - AUD-B15: a rule that raises is skipped, the report continues
|
|
243
|
+
import logging
|
|
244
|
+
|
|
245
|
+
logging.getLogger("tempergraph").warning("audit rule error in %s: %s", r.id, type(exc).__name__)
|
|
246
|
+
rep.rules_skipped.append({"rule": r.id, "reason": f"rule error ({type(exc).__name__})", "min_n": need})
|
|
247
|
+
continue
|
|
248
|
+
if got is None:
|
|
249
|
+
rep.rules_skipped.append({"rule": r.id, "reason": "owner not present", "min_n": need})
|
|
250
|
+
continue
|
|
251
|
+
out += got
|
|
252
|
+
rep.hot_spots = hot_spots(facts, out)
|
|
253
|
+
return out
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _guarded(rep: AuditReport, layer: str, fn: Callable[[], list[AuditFinding]]) -> list[AuditFinding]:
|
|
257
|
+
"""AUD-B15, AUD-B9, AUD-B2: a rule that raises is skipped with reason `rule error`; the report continues."""
|
|
258
|
+
try:
|
|
259
|
+
return fn()
|
|
260
|
+
except Exception as exc: # noqa: BLE001
|
|
261
|
+
import logging
|
|
262
|
+
|
|
263
|
+
logging.getLogger("tempergraph").warning("audit rule error in %s: %s", layer, type(exc).__name__)
|
|
264
|
+
rep.rules_skipped.append({"rule": f"{layer} layer", "reason": f"rule error ({type(exc).__name__})"})
|
|
265
|
+
return []
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _narrow(spec: WorkflowSpec, findings: list[AuditFinding], ctx: AuditContext) -> list[AuditFinding]:
|
|
269
|
+
keep: set[str] | None = None
|
|
270
|
+
if ctx.steps:
|
|
271
|
+
keep = set(ctx.steps)
|
|
272
|
+
if ctx.executor:
|
|
273
|
+
bound = {n for n, u in spec.units.items() if u.step is not None
|
|
274
|
+
and (u.step.executor or spec.default_executor) == ctx.executor}
|
|
275
|
+
keep = bound if keep is None else keep & bound
|
|
276
|
+
if keep is None:
|
|
277
|
+
return findings
|
|
278
|
+
return [f for f in findings if f.unit is None or f.unit in keep]
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _dedupe(findings: list[AuditFinding]) -> list[AuditFinding]:
|
|
282
|
+
"""One finding per (rule, unit, item) (AUD-B7, AUD-B12)."""
|
|
283
|
+
seen: set[tuple[Any, ...]] = set()
|
|
284
|
+
out = []
|
|
285
|
+
for f in findings:
|
|
286
|
+
key = (*f.key, f.location.get("file"), f.statement if f.owner != "audit" else "")
|
|
287
|
+
if key not in seen:
|
|
288
|
+
seen.add(key)
|
|
289
|
+
out.append(f)
|
|
290
|
+
return out
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _owned_counts(owned: list[AuditFinding]) -> list[dict[str, Any]]:
|
|
294
|
+
counts: dict[str, int] = {}
|
|
295
|
+
for f in owned:
|
|
296
|
+
counts[f.owner] = counts.get(f.owner, 0) + 1
|
|
297
|
+
return [{"owner": k, "count": v} for k, v in sorted(counts.items())]
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def audit_layers(given: list[str] | None) -> set[Layer]:
|
|
301
|
+
"""`--layer` given: those; otherwise every layer whose inputs exist (AUD-B11, AUD-B6, AUD-B9)."""
|
|
302
|
+
return set(given) if given else {"definition", "host", "history"} # type: ignore[arg-type]
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
__all__ = ["AuditContext", "AuditFinding", "AuditReport", "Ignore", "audit", "audit_layers", "parse_ignores"]
|