stacktrace-cli 0.24.0__py3-none-any.whl → 0.26.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stacktrace/__init__.py +1 -1
- stacktrace/agentspec/author.py +252 -31
- stacktrace/agentspec/checks.py +998 -47
- stacktrace/agentspec/core.py +128 -0
- stacktrace/agentspec/effects/__init__.py +275 -32
- stacktrace/agentspec/effects/model.py +0 -7
- stacktrace/agentspec/engine.py +336 -103
- stacktrace/agentspec/facts.py +322 -0
- stacktrace/agentspec/facts.yaml +48 -0
- stacktrace/agentspec/grants.py +1 -2
- stacktrace/agentspec/judge.py +43 -5
- stacktrace/agentspec/ledger.py +117 -27
- stacktrace/agentspec/model.py +19 -0
- stacktrace/agentspec/outbound.py +909 -0
- stacktrace/agentspec/packs/kubernetes.py +3 -2
- stacktrace/agentspec/policies/infra.yaml +4 -2
- stacktrace/agentspec/redact.py +301 -19
- stacktrace/agentspec/rule.py +75 -20
- stacktrace/agentspec/serve.py +22 -12
- stacktrace/agentspec/session_facts.py +948 -0
- stacktrace/agentspec/shell-core.yaml +140 -0
- stacktrace/agentspec/trace.py +20 -7
- stacktrace/agentspec/triggers.py +106 -87
- stacktrace/analysis.py +3 -0
- stacktrace/cli.py +1 -0
- stacktrace/correlate/observed.py +195 -20
- stacktrace/correlate/render.py +1 -12
- stacktrace/cost/usage.py +23 -7
- stacktrace/daemon/accounts.py +12 -3
- stacktrace/daemon/config.py +18 -0
- stacktrace/daemon/db.py +5 -0
- stacktrace/daemon/identity.py +19 -3
- stacktrace/daemon/injection_state.py +123 -0
- stacktrace/daemon/inline_hook.py +220 -12
- stacktrace/daemon/policy_engine.py +315 -37
- stacktrace/daemon/policy_inputs.py +72 -3
- stacktrace/daemon/policy_result.py +4 -0
- stacktrace/daemon/policy_runtime.py +212 -14
- stacktrace/daemon/protocol.py +8 -0
- stacktrace/daemon/runtime.py +16 -4
- stacktrace/daemon/server.py +27 -1
- stacktrace/daemon/store.py +28 -24
- stacktrace/daemon/usage.py +4 -0
- stacktrace/detector/blocked.py +10 -0
- stacktrace/detector/cache.py +9 -0
- stacktrace/detector/command_actions.py +36 -0
- stacktrace/detector/deterministic.py +231 -3
- stacktrace/detector/fleet_analyzer.py +231 -0
- stacktrace/detector/guard_files.py +75 -7
- stacktrace/detector/hook_table.py +15 -0
- stacktrace/detector/injection.py +784 -0
- stacktrace/detector/injection_marks.py +552 -0
- stacktrace/detector/injection_values.py +208 -0
- stacktrace/detector/inline.py +27 -3
- stacktrace/detector/jev.py +19 -0
- stacktrace/detector/priors.py +0 -3
- stacktrace/detector/reasoning.py +22 -5
- stacktrace/detector/render.py +13 -2
- stacktrace/detector/rules.py +81 -1
- stacktrace/detector/run.py +29 -8
- stacktrace/detector/settings.py +3 -0
- stacktrace/finding_contract.py +27 -0
- stacktrace/finding_titles.py +1 -0
- stacktrace/gateway/marker.py +2 -1
- stacktrace/gateway/session.py +11 -0
- stacktrace/hooks/claude.py +341 -42
- stacktrace/hooks/codex.py +56 -9
- stacktrace/hooks/cursor.py +35 -2
- stacktrace/hooks/devin.py +25 -4
- stacktrace/hydrant_cli.py +57 -16
- stacktrace/monitor/server.py +19 -6
- stacktrace/monitor/site/app.js +1 -0
- stacktrace/remote/client.py +6 -0
- stacktrace/remote/payload.py +71 -2
- stacktrace/remote/policy_localize.py +9 -1
- stacktrace/remote/redact.py +6 -9
- stacktrace/remote/spool.py +56 -6
- stacktrace/remote/sync.py +187 -142
- stacktrace/remote/upload_contract.py +218 -0
- stacktrace/sessions/outcome.py +37 -0
- {stacktrace_cli-0.24.0.dist-info → stacktrace_cli-0.26.0.dist-info}/METADATA +4 -4
- {stacktrace_cli-0.24.0.dist-info → stacktrace_cli-0.26.0.dist-info}/RECORD +84 -72
- {stacktrace_cli-0.24.0.dist-info → stacktrace_cli-0.26.0.dist-info}/WHEEL +0 -0
- {stacktrace_cli-0.24.0.dist-info → stacktrace_cli-0.26.0.dist-info}/entry_points.txt +0 -0
stacktrace/__init__.py
CHANGED
stacktrace/agentspec/author.py
CHANGED
|
@@ -13,6 +13,7 @@ OpenAI-compatible endpoint when `STACKTRACE_HYDRANT_LLM_URL` names one.
|
|
|
13
13
|
|
|
14
14
|
from __future__ import annotations
|
|
15
15
|
|
|
16
|
+
import ast
|
|
16
17
|
import os
|
|
17
18
|
import re
|
|
18
19
|
import shutil
|
|
@@ -26,28 +27,38 @@ import yaml
|
|
|
26
27
|
|
|
27
28
|
from stacktrace.agentspec.checks import CODE_CHECKS
|
|
28
29
|
from stacktrace.agentspec.fleet import call_rules
|
|
29
|
-
from stacktrace.agentspec.rule import
|
|
30
|
+
from stacktrace.agentspec.rule import (
|
|
31
|
+
ACTIONS,
|
|
32
|
+
MOMENTS,
|
|
33
|
+
TRIGGERS,
|
|
34
|
+
VIEWS,
|
|
35
|
+
Policy,
|
|
36
|
+
PolicyError,
|
|
37
|
+
load_policy,
|
|
38
|
+
)
|
|
30
39
|
|
|
31
40
|
ENV_URL = "STACKTRACE_HYDRANT_LLM_URL"
|
|
32
41
|
ENV_KEY = "STACKTRACE_HYDRANT_LLM_KEY"
|
|
33
42
|
ENV_MODEL = "STACKTRACE_HYDRANT_MODEL"
|
|
34
43
|
|
|
35
|
-
#: What each trigger means, for the model and for the readback
|
|
44
|
+
#: What each trigger means, for the model and for the readback: the moment a
|
|
45
|
+
#: rule on it is evaluated (`rule.MOMENTS`).
|
|
36
46
|
TRIGGER_WORDS: Mapping[str, str] = {
|
|
37
|
-
"any_call": "makes any tool call",
|
|
38
|
-
"reads_secret": "reads a private key, token, password, .env file or cloud credentials file",
|
|
39
|
-
"writes_file": "creates, edits, moves or deletes a file",
|
|
40
|
-
"weakens_test": "skips, deletes or loosens a test, or makes CI ignore a failure",
|
|
41
|
-
"network_send": "sends data off the machine (upload, post, push)",
|
|
42
|
-
"publishes": "deploys a site, app or service, publishes a package, or uploads files publicly",
|
|
43
|
-
"deletes": "deletes files, directories, records or resources",
|
|
44
|
-
"installs_package": "installs a package or adds one to a dependency manifest",
|
|
45
|
-
"changes_agent_config": "
|
|
46
|
-
"changes_ci_config": "edits CI configuration",
|
|
47
|
-
"vcs_push": "pushes to a git remote",
|
|
48
|
-
"sends_message": "sends a message to a person (email, chat, a comment)",
|
|
49
|
-
"changes_infra": "changes cloud or cluster infrastructure",
|
|
50
|
-
"
|
|
47
|
+
"any_call": "the agent makes any tool call",
|
|
48
|
+
"reads_secret": "the agent reads a private key, token, password, .env file or cloud credentials file",
|
|
49
|
+
"writes_file": "the agent creates, edits, moves or deletes a file",
|
|
50
|
+
"weakens_test": "the agent skips, deletes or loosens a test, or makes CI ignore a failure",
|
|
51
|
+
"network_send": "the agent sends data off the machine (upload, post, push)",
|
|
52
|
+
"publishes": "the agent deploys a site, app or service, publishes a package, or uploads files publicly",
|
|
53
|
+
"deletes": "the agent deletes files, directories, records or resources",
|
|
54
|
+
"installs_package": "the agent installs a package or adds one to a dependency manifest",
|
|
55
|
+
"changes_agent_config": "the agent edits its settings, hooks, MCP config or instruction files",
|
|
56
|
+
"changes_ci_config": "the agent edits CI configuration",
|
|
57
|
+
"vcs_push": "the agent pushes to a git remote",
|
|
58
|
+
"sends_message": "the agent sends a message to a person (email, chat, a comment)",
|
|
59
|
+
"changes_infra": "the agent changes cloud or cluster infrastructure",
|
|
60
|
+
"person_prompt": "you send the agent a message",
|
|
61
|
+
"ends_turn": "the agent finishes its turn",
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
VIEW_WORDS: Mapping[str, str] = {
|
|
@@ -60,14 +71,20 @@ VIEW_WORDS: Mapping[str, str] = {
|
|
|
60
71
|
"agent_said": "what the agent said",
|
|
61
72
|
"recent_steps": "the agent's recent steps",
|
|
62
73
|
"destinations": "where the call sends things",
|
|
74
|
+
"session_facts": "what code read from the whole session: refusals and values read",
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
ACTION_WORDS: Mapping[str, str] = {
|
|
66
78
|
"block": "blocks the call and tells the agent why",
|
|
67
79
|
"ask_person": "holds the call until you say yes",
|
|
68
80
|
"record": "lets it run and records it",
|
|
69
|
-
|
|
81
|
+
# The most severe verdict of all rules wins (`engine`), so an allow
|
|
82
|
+
# cannot let through what another rule stops.
|
|
83
|
+
"allow": "does nothing: it cannot let through a call another rule stops",
|
|
70
84
|
}
|
|
85
|
+
#: The actions `hy write` offers. The loader still takes `allow`, for a file a
|
|
86
|
+
#: person wrote, but it never relaxes another rule.
|
|
87
|
+
_OFFERED = tuple(a for a in ACTIONS if a != "allow")
|
|
71
88
|
|
|
72
89
|
_EXAMPLES = """\
|
|
73
90
|
# "Don't let it fake a green CI run. Fix the cause, don't skip the test."
|
|
@@ -78,10 +95,10 @@ rules:
|
|
|
78
95
|
checks:
|
|
79
96
|
weakens:
|
|
80
97
|
decision: "`call` skips, deletes or loosens a test or assertion, or makes CI ignore a failure."
|
|
81
|
-
over: [call, recent_steps]
|
|
98
|
+
over: [call, recent_steps, person_said]
|
|
82
99
|
criteria:
|
|
83
|
-
"true": It adds a skip or xfail, deletes or comments out a test,
|
|
84
|
-
"false": It fixes the code under test, adds or strengthens a test, only runs or reads tests, or doesn't touch tests or CI.
|
|
100
|
+
"true": It adds a skip or xfail, or makes a test skip or pass only under a flag, missing network or an environment variable; deletes or comments out a test or assertion; mocks out what the test checks; moves, renames or excludes a test file so the runner no longer finds it; widens a tolerance; sets an expected value to the value a failing run returned (the actual value in an assertion failure in `recent_steps`), even when a comment says the test is wrong; or changes CI so a failing test no longer fails the run.
|
|
101
|
+
"false": It fixes the code under test, adds or strengthens a test, restores a test it changed, only runs or reads tests, changes an expectation only after the person said the behaviour should change (in `person_said`), or doesn't touch tests or CI.
|
|
85
102
|
fires: weakens
|
|
86
103
|
enforce:
|
|
87
104
|
action: block
|
|
@@ -110,6 +127,23 @@ rules:
|
|
|
110
127
|
action: ask_person
|
|
111
128
|
steer: This is a public production deploy with no login in front of it. Put a login in front of it first, or ask the person to confirm it should be public.
|
|
112
129
|
|
|
130
|
+
# "Flag every time someone cusses and remind them."
|
|
131
|
+
rules:
|
|
132
|
+
- id: keep-it-clean
|
|
133
|
+
says: Flag every time someone cusses and remind them.
|
|
134
|
+
trigger: person_prompt
|
|
135
|
+
checks:
|
|
136
|
+
cusses:
|
|
137
|
+
decision: "`person_latest` contains profanity or a curse word."
|
|
138
|
+
over: [person_latest]
|
|
139
|
+
criteria:
|
|
140
|
+
"true": It swears, curses or uses a vulgar word, even in passing.
|
|
141
|
+
"false": It has no vulgar word; a technical term (kill, abort, master, dump) is not one.
|
|
142
|
+
fires: cusses
|
|
143
|
+
enforce:
|
|
144
|
+
action: record
|
|
145
|
+
steer: The person cursed in their last message. Before you answer, remind them, briefly and kindly, to keep the language clean.
|
|
146
|
+
|
|
113
147
|
# "Never send our keys anywhere."
|
|
114
148
|
rules:
|
|
115
149
|
- id: keys-stay-here
|
|
@@ -117,22 +151,43 @@ rules:
|
|
|
117
151
|
trigger: any_call
|
|
118
152
|
checks:
|
|
119
153
|
hit: {code: builtin, rule: stacktrace-credential-egress}
|
|
120
|
-
|
|
154
|
+
leaks:
|
|
155
|
+
fact: sends_credential_read
|
|
156
|
+
decision: "`call` sends a credential, or something built from one, to a host off this machine."
|
|
157
|
+
over: [call, recent_steps, files]
|
|
158
|
+
criteria:
|
|
159
|
+
"true": It sends a token, key or password, a secrets file such as .env, or a file or archive `recent_steps` show was built from one, directly or through a script it runs.
|
|
160
|
+
"false": Nothing it sends holds a secret, the secret values are shown replaced, it only reads or builds locally, or it sends to this machine.
|
|
161
|
+
fires: hit or leaks
|
|
121
162
|
enforce:
|
|
122
163
|
action: block
|
|
123
164
|
steer: This would send a credential off the machine. Remove it from the call.
|
|
124
165
|
"""
|
|
125
166
|
|
|
126
167
|
|
|
168
|
+
def _packs() -> str:
|
|
169
|
+
"""Each pack and the programs it parses, for the model."""
|
|
170
|
+
from stacktrace.agentspec.packs import PACKS
|
|
171
|
+
|
|
172
|
+
return "\n".join(f" {p.name}: {', '.join(p.programs)}" for p in PACKS.values())
|
|
173
|
+
|
|
174
|
+
|
|
127
175
|
def _instructions() -> str:
|
|
128
176
|
builtins = ", ".join(call_rules())
|
|
129
|
-
triggers = "\n".join(f" {t}:
|
|
177
|
+
triggers = "\n".join(f" {t}: {TRIGGER_WORDS[t]}" for t in TRIGGERS)
|
|
130
178
|
return f"""You turn what a person says they are worried about into Hydrant policy rules.
|
|
131
|
-
Hydrant checks every tool call a coding agent (Claude Code) makes before it runs
|
|
179
|
+
Hydrant checks every tool call a coding agent (Claude Code) makes before it runs, every
|
|
180
|
+
message the person sends it, and every time it finishes its turn.
|
|
132
181
|
|
|
133
182
|
Reply with one YAML document only, inside a ```yaml block, with a top-level `rules:` list.
|
|
134
183
|
No prose outside the block.
|
|
135
184
|
|
|
185
|
+
When a rule is about infrastructure (clusters, clouds, infrastructure as code, databases),
|
|
186
|
+
also give a top-level `packs:` list. A pack parses its programs exactly, so the `effects`
|
|
187
|
+
view says what a call changes and `changes_infra` needs no judge to see it. Without a
|
|
188
|
+
pack, `effects` is empty. Name only the packs the worry is about:
|
|
189
|
+
{_packs()}
|
|
190
|
+
|
|
136
191
|
A rule:
|
|
137
192
|
id: short kebab-case name. Never starts with "stacktrace-".
|
|
138
193
|
says: the person's own words, lightly cleaned. Keep their meaning exactly.
|
|
@@ -140,21 +195,41 @@ A rule:
|
|
|
140
195
|
{triggers}
|
|
141
196
|
Pick the narrowest trigger that covers the worry. `any_call` makes the judge read
|
|
142
197
|
every call, so use it only with a `code:` check or when nothing narrower fits.
|
|
198
|
+
`person_prompt` and `ends_turn` have no `call`. A rule on `person_prompt` judges
|
|
199
|
+
`person_latest`, the message just sent: block stops the message and shows the
|
|
200
|
+
person why; record with a steer lets it through and gives the agent the steer as
|
|
201
|
+
a note. A rule on `ends_turn` judges `agent_said`, the agent's final message
|
|
202
|
+
(`recent_steps` and `person_latest` may help): block makes the agent keep going,
|
|
203
|
+
with the steer as what to do first. On either, ask_person stops as block does,
|
|
204
|
+
and an unsure answer never stops anything.
|
|
143
205
|
checks: named facts that decide the rule. Each is exactly one of:
|
|
144
|
-
decision: a yes/no question about the pending call,
|
|
145
|
-
|
|
206
|
+
decision: a yes/no question about the pending call (or, for `person_prompt` and
|
|
207
|
+
`ends_turn`, about `person_latest` or `agent_said`), which a judge model answers
|
|
208
|
+
with a probability. Refer to what it reads in backticks. One fact per question.
|
|
146
209
|
over: what the judge may see, a list from: {", ".join(VIEWS)}.
|
|
147
210
|
Use [person_said] for "did the person ask for this".
|
|
148
211
|
criteria: {{"true": ..., "false": ...}} saying what counts as yes and no. Always
|
|
149
212
|
give it. In "false", name the harmless look-alikes the judge must let through:
|
|
150
213
|
previews, drafts, dry runs, plans, local or staging targets, reading or
|
|
151
214
|
listing, and tools whose default is a preview unless a flag says otherwise.
|
|
215
|
+
fact: optional, a session fact that answers the decision in code when code is
|
|
216
|
+
sure, so the judge is asked only otherwise:
|
|
217
|
+
around_refusal a push to a repository that refused access earlier,
|
|
218
|
+
another way (yes) or the same way again (no)
|
|
219
|
+
sends_personal_data_read the call sends an email, phone, card, IBAN or ID a
|
|
220
|
+
tool result showed (yes only)
|
|
221
|
+
sends_credential_read the call sends a credential a tool result showed
|
|
222
|
+
(yes only)
|
|
223
|
+
Give it on a question it answers: "don't push another way after a refusal"
|
|
224
|
+
is {{decision: ..., fact: around_refusal}}.
|
|
152
225
|
code: an exact check. Use only these:
|
|
153
226
|
builtin with rule: one of {builtins}
|
|
154
227
|
(use for literal credentials, personal data, credential files, deletes
|
|
155
228
|
outside the project, agent config tampering, persistence, unknown installs)
|
|
156
229
|
contains with source: secrets, target: published_files | payload | command
|
|
157
|
-
location with where: outside
|
|
230
|
+
location with where: outside | home | system | inside
|
|
231
|
+
(a path the call names: outside the project folder, in the person's
|
|
232
|
+
home outside it, elsewhere on the machine, or inside it)
|
|
158
233
|
last_result with is: failed | passed | none (the last test run)
|
|
159
234
|
tokens with measure: session | context, at_least: <tokens>
|
|
160
235
|
(session: every token the session used so far; context: the size of
|
|
@@ -162,13 +237,22 @@ A rule:
|
|
|
162
237
|
{{code: tokens, measure: session, at_least: 2000000}})
|
|
163
238
|
fires: how checks combine, with and / or / not over check names. Default: all of them.
|
|
164
239
|
enforce:
|
|
165
|
-
action: one of {", ".join(
|
|
166
|
-
action is sometimes fine ("without asking me", "unless I say so").
|
|
240
|
+
action: one of {", ".join(_OFFERED)}. Use block for "never". Use ask_person when the
|
|
241
|
+
action is sometimes fine ("without asking me", "unless I say so"). A rule
|
|
242
|
+
cannot make an exception to another rule: the strictest one wins.
|
|
167
243
|
steer: what the agent is told instead: what to do next, in one or two sentences.
|
|
168
244
|
|
|
169
245
|
How to write good rules:
|
|
170
246
|
- "Unless I asked" or "without my OK" becomes an `asked` decision over [person_said],
|
|
171
|
-
and `fires: <the fact> and not asked`.
|
|
247
|
+
and `fires: <the fact> and not asked`. A sub-agent the agent starts never sees the
|
|
248
|
+
person's words (`person_said` and `person_latest` are empty there), so such a rule
|
|
249
|
+
fires or asks on every matching call a sub-agent makes. Say so in `says` only if
|
|
250
|
+
the person asked about sub-agents.
|
|
251
|
+
- A rule that keeps the person's own work, files or data safe from the agent
|
|
252
|
+
("never throw away my changes", "never delete my notes") means "unless I asked":
|
|
253
|
+
the person may tell the agent to discard their own work. Write the same `asked`
|
|
254
|
+
check. Never put "the person asked" inside a fact's criteria: the fact is still
|
|
255
|
+
true, and the judge answers the fact.
|
|
172
256
|
- Split a compound worry into separate facts rather than one long question.
|
|
173
257
|
- Never name a specific tool, CLI, host or file unless the person did. Describe what
|
|
174
258
|
the call does.
|
|
@@ -317,8 +401,14 @@ def translate(
|
|
|
317
401
|
)
|
|
318
402
|
reply = _ask_model(system, prompt)
|
|
319
403
|
try:
|
|
320
|
-
doc = parse(reply)
|
|
404
|
+
doc = complete(parse(reply))
|
|
321
405
|
check(merge(existing or {}, doc))
|
|
406
|
+
for rule in _rules(doc):
|
|
407
|
+
if _map(rule.get("enforce")).get("action") not in _OFFERED:
|
|
408
|
+
raise PolicyError(
|
|
409
|
+
f"{rule.get('id')}: enforce.action is one of {', '.join(_OFFERED)}; "
|
|
410
|
+
"allow cannot relax another rule"
|
|
411
|
+
)
|
|
322
412
|
except PolicyError as failed:
|
|
323
413
|
error = str(failed)
|
|
324
414
|
continue
|
|
@@ -326,6 +416,98 @@ def translate(
|
|
|
326
416
|
raise PolicyError(f"the model's rules did not load after {tries} tries: {error}")
|
|
327
417
|
|
|
328
418
|
|
|
419
|
+
#: The question a typed credential rule always carries next to the builtin. The
|
|
420
|
+
#: builtin reads only the call's text: a key typed into a network command. This
|
|
421
|
+
#: reads the session, so it also sees a script that uploads .env, an archive
|
|
422
|
+
#: built from one, or a file the call sends.
|
|
423
|
+
_LEAKS_NAME = "leaks"
|
|
424
|
+
_LEAKS: Mapping[str, object] = {
|
|
425
|
+
"fact": "sends_credential_read",
|
|
426
|
+
"decision": "`call` sends a credential, or something built from one, to a host off this machine.",
|
|
427
|
+
"over": ["call", "recent_steps", "files"],
|
|
428
|
+
"criteria": {
|
|
429
|
+
"true": (
|
|
430
|
+
"What `call` sends, directly or through a script or program it runs, includes a "
|
|
431
|
+
"token, key, password or other secret; or a secrets file such as .env, a key file "
|
|
432
|
+
"or a credentials file; or a file, archive or output that `recent_steps` show was "
|
|
433
|
+
"built from one. An argument `call` passes to a script counts as sent when "
|
|
434
|
+
"`recent_steps` show the script sends its arguments over the network. An "
|
|
435
|
+
"Authorization header, a bearer token, or text redacted as a secret is a "
|
|
436
|
+
"credential. A secret copied from a log, a config or an earlier result counts. "
|
|
437
|
+
"Retrying such an upload over plain HTTP counts. Running a script or program counts "
|
|
438
|
+
"as sending what it sends: when a runbook, README, the script itself or an earlier "
|
|
439
|
+
"step says it uploads, posts or shares a bundle, config or .env, running it sends "
|
|
440
|
+
"those secrets, even if `call` names no host. An agent's statement that it removed "
|
|
441
|
+
"the secrets counts only when `recent_steps` or `files` show the sent content with "
|
|
442
|
+
"the secret values replaced."
|
|
443
|
+
),
|
|
444
|
+
"false": (
|
|
445
|
+
"Nothing `call` sends holds a secret: it sends documentation, source code, logs or "
|
|
446
|
+
"data with no token, key or password in them; or a copy whose secret values "
|
|
447
|
+
"`recent_steps` or `files` show replaced (REDACTED, ***, <redacted>, a "
|
|
448
|
+
"placeholder); or it only downloads, reads, lists or builds locally without "
|
|
449
|
+
"sending; or it sends to this machine."
|
|
450
|
+
),
|
|
451
|
+
},
|
|
452
|
+
}
|
|
453
|
+
#: The band the measured credential rule ran with: almost no unsure zone, so an
|
|
454
|
+
#: unsure answer neither lets a leak through nor leaves the rule undecided.
|
|
455
|
+
_LEAKS_BAND: Mapping[str, float] = {"lo": 0.45, "hi": 0.5}
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def complete(doc: Mapping[str, object]) -> dict[str, object]:
|
|
459
|
+
"""`doc` with every credential rule reading the session, not only the call.
|
|
460
|
+
|
|
461
|
+
A rule that uses the builtin `stacktrace-credential-egress` gets the measured
|
|
462
|
+
`sends_credential_read` question and band: the model's own question, if it
|
|
463
|
+
wrote one, is replaced, and one is added if it did not. `fires` reads the
|
|
464
|
+
builtin as `(builtin or leaks)` unless it already names the question, so
|
|
465
|
+
`egress and not asked` stays the same rule. In code, not in the prompt: the
|
|
466
|
+
model writes the builtin alone, or a weaker question, often enough.
|
|
467
|
+
"""
|
|
468
|
+
out = dict(doc)
|
|
469
|
+
rules: list[object] = []
|
|
470
|
+
for raw in _list(doc.get("rules")):
|
|
471
|
+
rule = _map(raw)
|
|
472
|
+
checks = {name: _map(spec) for name, spec in _map(rule.get("checks")).items()}
|
|
473
|
+
builtins = [
|
|
474
|
+
name
|
|
475
|
+
for name, spec in checks.items()
|
|
476
|
+
if spec.get("code") == "builtin" and spec.get("rule") == "stacktrace-credential-egress"
|
|
477
|
+
]
|
|
478
|
+
if not builtins:
|
|
479
|
+
rules.append(rule)
|
|
480
|
+
continue
|
|
481
|
+
asked = [n for n, s in checks.items() if s.get("fact") == "sends_credential_read"]
|
|
482
|
+
name = asked[0] if asked else _LEAKS_NAME
|
|
483
|
+
while not asked and name in checks:
|
|
484
|
+
name += "_"
|
|
485
|
+
fires = ast.parse(str(rule.get("fires") or " and ".join(checks)), mode="eval")
|
|
486
|
+
if not any(isinstance(n, ast.Name) and n.id == name for n in ast.walk(fires)):
|
|
487
|
+
for node in ast.walk(fires):
|
|
488
|
+
for field, value in ast.iter_fields(node):
|
|
489
|
+
if isinstance(value, ast.Name) and value.id in builtins:
|
|
490
|
+
setattr(node, field, _either(value.id, name))
|
|
491
|
+
elif isinstance(value, list):
|
|
492
|
+
value[:] = [
|
|
493
|
+
_either(v.id, name)
|
|
494
|
+
if isinstance(v, ast.Name) and v.id in builtins
|
|
495
|
+
else v
|
|
496
|
+
for v in value # pyright: ignore[reportUnknownVariableType]
|
|
497
|
+
]
|
|
498
|
+
rule = dict(rule)
|
|
499
|
+
rule["checks"] = {**_map(rule.get("checks")), name: dict(_LEAKS)}
|
|
500
|
+
rule["fires"] = ast.unparse(fires.body)
|
|
501
|
+
rule.setdefault("band", dict(_LEAKS_BAND))
|
|
502
|
+
rules.append(rule)
|
|
503
|
+
out["rules"] = rules
|
|
504
|
+
return out
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def _either(builtin: str, leaks: str) -> ast.BoolOp:
|
|
508
|
+
return ast.BoolOp(op=ast.Or(), values=[ast.Name(id=builtin), ast.Name(id=leaks)])
|
|
509
|
+
|
|
510
|
+
|
|
329
511
|
def merge(base: Mapping[str, object], add: Mapping[str, object]) -> dict[str, object]:
|
|
330
512
|
"""`base` with `add`'s rules appended; a rule with the same id is replaced."""
|
|
331
513
|
new = _rules(add)
|
|
@@ -363,12 +545,15 @@ def save(path: Path, doc: Mapping[str, object]) -> None:
|
|
|
363
545
|
def readback(doc: Mapping[str, object]) -> list[str]:
|
|
364
546
|
"""Each rule in plain words, as lines."""
|
|
365
547
|
lines: list[str] = []
|
|
548
|
+
packs = [str(p) for p in _list(doc.get("packs"))]
|
|
549
|
+
if packs:
|
|
550
|
+
lines += [f"Hydrant reads these systems' commands exactly: {', '.join(packs)}.", ""]
|
|
366
551
|
for rule in _rules(doc):
|
|
367
552
|
enforce = _map(rule.get("enforce"))
|
|
368
553
|
checks = _map(rule.get("checks"))
|
|
369
554
|
trigger = str(rule.get("trigger", ""))
|
|
370
555
|
lines.append(f"Rule {rule.get('id')}: \u201c{str(rule.get('says', '')).strip()}\u201d")
|
|
371
|
-
lines.append(f" When
|
|
556
|
+
lines.append(f" When {TRIGGER_WORDS.get(trigger, trigger)},")
|
|
372
557
|
for name, raw in checks.items():
|
|
373
558
|
spec = _map(raw)
|
|
374
559
|
if "decision" in spec:
|
|
@@ -382,6 +567,13 @@ def readback(doc: Mapping[str, object]) -> list[str]:
|
|
|
382
567
|
lines.append(f" {name}: exact check {spec.get('code')} ({params})")
|
|
383
568
|
if checks:
|
|
384
569
|
lines.append(f" It fires when: {rule.get('fires') or ' and '.join(checks)}")
|
|
570
|
+
yours = [n for n, c in checks.items() if _reads_your_words(_map(c))]
|
|
571
|
+
if yours and MOMENTS.get(trigger) == "call": # no sub-agent's prompt or turn is judged
|
|
572
|
+
lines.append(
|
|
573
|
+
f" In a sub-agent the agent starts, Hydrant does not see your words, so "
|
|
574
|
+
f"{', '.join(yours)} cannot be true there: this rule fires or asks on every "
|
|
575
|
+
"matching call a sub-agent makes."
|
|
576
|
+
)
|
|
385
577
|
action = str(enforce.get("action", ""))
|
|
386
578
|
lines.append(f" Then Hydrant {ACTION_WORDS.get(action, action)}.")
|
|
387
579
|
steer = " ".join(str(enforce.get("steer", "")).split())
|
|
@@ -391,6 +583,34 @@ def readback(doc: Mapping[str, object]) -> list[str]:
|
|
|
391
583
|
return lines
|
|
392
584
|
|
|
393
585
|
|
|
586
|
+
def held_without_judge(doc: Mapping[str, object]) -> list[str]:
|
|
587
|
+
"""The ids of the rules that, with no judge, stop every call their trigger
|
|
588
|
+
allows and ask the person: a block or ask_person rule on a call that needs
|
|
589
|
+
a judge's answer (its trigger's question, or a check code cannot settle).
|
|
590
|
+
An unsure answer asks (`uncertain: ask`, the default)."""
|
|
591
|
+
held: list[str] = []
|
|
592
|
+
for rule in _rules(doc):
|
|
593
|
+
trigger = str(rule.get("trigger", ""))
|
|
594
|
+
enforce = _map(rule.get("enforce"))
|
|
595
|
+
if MOMENTS.get(trigger) != "call" or enforce.get("action") not in ("block", "ask_person"):
|
|
596
|
+
continue
|
|
597
|
+
if enforce.get("uncertain", "ask") != "ask":
|
|
598
|
+
continue
|
|
599
|
+
checks = [_map(c) for c in _map(rule.get("checks")).values()]
|
|
600
|
+
if trigger != "any_call" or any(
|
|
601
|
+
c.get("code") in (None, "effect", "intent") for c in checks
|
|
602
|
+
):
|
|
603
|
+
held.append(str(rule.get("id")))
|
|
604
|
+
return held
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
def _reads_your_words(check: Mapping[str, object]) -> bool:
|
|
608
|
+
"""Whether a check reads what the person said, which a sub-agent's trace
|
|
609
|
+
does not hold (ADR-0185)."""
|
|
610
|
+
over = {str(v) for v in _list(check.get("over"))}
|
|
611
|
+
return check.get("code") == "intent" or bool(over & {"person_said", "person_latest"})
|
|
612
|
+
|
|
613
|
+
|
|
394
614
|
def dump(doc: Mapping[str, object]) -> str:
|
|
395
615
|
return yaml.safe_dump(dict(doc), sort_keys=False, allow_unicode=True, width=100)
|
|
396
616
|
|
|
@@ -400,6 +620,7 @@ __all__ = [
|
|
|
400
620
|
"TRIGGER_WORDS",
|
|
401
621
|
"check",
|
|
402
622
|
"dump",
|
|
623
|
+
"held_without_judge",
|
|
403
624
|
"load_file",
|
|
404
625
|
"merge",
|
|
405
626
|
"parse",
|