@mokuyoaxis/agent-guard 0.2.3-rc1 → 0.2.3
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.
- package/README.md +124 -52
- package/README.zh-CN.md +102 -41
- package/adapters/dsh/README.md +92 -10
- package/adapters/dsh/harness/README.md +384 -0
- package/adapters/dsh/harness/baseline.patch.yml +8 -0
- package/adapters/dsh/harness/bootstrap.patch.yml +5 -0
- package/adapters/dsh/harness/bootstrap_init.mjs +48 -0
- package/adapters/dsh/harness/guard_lab.py +771 -0
- package/adapters/dsh/harness/native_session.py +503 -0
- package/adapters/dsh/harness/profiles.py +212 -0
- package/adapters/dsh/harness/read_fixture.py +60 -0
- package/adapters/dsh/harness/retry_stream.py +171 -0
- package/adapters/dsh/harness/session_decode.mjs +117 -0
- package/adapters/dsh/lib/index.js +46 -5
- package/adapters/dsh/lib/read_result_guard.js +207 -0
- package/adapters/dsh/lib/read_result_guard.py +139 -0
- package/adapters/harness_support.py +109 -0
- package/adapters/kimi-code/README.md +15 -7
- package/adapters/kimi-code/harness/README.md +126 -0
- package/adapters/kimi-code/harness/guard_lab.py +430 -0
- package/core/lab.py +2396 -0
- package/core/redaction.py +157 -55
- package/docs/architecture.md +75 -0
- package/docs/development-note-unguarded-deletion.md +6 -7
- package/docs/friction.md +140 -4
- package/docs/guard-lab-clean-task.md +4 -0
- package/docs/guard-lab-read-trial-task.md +7 -0
- package/docs/guard-lab-trial-task.md +5 -0
- package/docs/guard-lab.md +398 -0
- package/docs/harness-capabilities.md +8 -5
- package/docs/release-notes-0.2.0-rc1.md +2 -2
- package/docs/release-notes-0.2.0.md +2 -2
- package/docs/release-notes-0.2.2.md +7 -8
- package/docs/release-notes-0.2.3-rc1.md +4 -2
- package/docs/release-notes-0.2.3-rc2.md +189 -0
- package/docs/release-notes-0.2.3.md +84 -0
- package/docs/secret-guard-analysis.md +13 -12
- package/docs/secret-guard-architecture.md +5 -3
- package/docs/secret-guard-design.md +7 -3
- package/docs/test-report-codex-gpt-5.6-sol.md +10 -11
- package/docs/test-report-dsh-0.2-upgrade.md +142 -0
- package/docs/test-report-dsh-guard-lab.md +154 -0
- package/docs/test-report-dsh-native-session.md +118 -0
- package/docs/test-report-dsh-paired-lab.md +183 -0
- package/docs/test-report-dsh-read-redaction.md +114 -0
- package/docs/test-report-dsh-real-followup.md +170 -0
- package/docs/test-report-dsh-retry-evidence.md +134 -0
- package/docs/test-report-kimi-code-block.md +19 -20
- package/docs/test-report-kimi-guard-lab.md +117 -0
- package/docs/test-report-release-readiness-0.2.3.md +239 -0
- package/docs/test-report-workbuddy-windows-core.md +53 -0
- package/docs/test-report-zcode-glm-flash.md +9 -9
- package/docs/threat-model.md +42 -0
- package/guard_lab.py +14 -0
- package/package.json +8 -5
- package/skills/delete-guard/SKILL.md +2 -3
- package/skills/exfil-guard/SKILL.md +37 -7
- package/skills/exfil-guard/references/rules.md +84 -16
- package/skills/exfil-guard/scripts/_payload.py +99 -0
- package/skills/exfil-guard/scripts/check_span.py +14 -81
- package/skills/exfil-guard/scripts/sanitize.py +113 -34
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
"""Bounded capture of fresh DSH v3/v4 single-turn sessions, without replay."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import shutil
|
|
9
|
+
|
|
10
|
+
from adapters.harness_support import capture_process, digest, read_regular, write_new
|
|
11
|
+
from core.lab import LabError, MAX_SCAN_BYTES
|
|
12
|
+
from adapters.dsh.harness.retry_stream import (
|
|
13
|
+
MAX_ATTEMPTS, MAX_STREAM_CHUNKS, failure_supported, finite_number,
|
|
14
|
+
inspect_stream, json_bytes,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
DECODER = Path(__file__).with_name("session_decode.mjs")
|
|
19
|
+
MAX_RECORDS = 4096
|
|
20
|
+
MAX_TREE_ENTRIES = 512
|
|
21
|
+
PARSER_VERSION = 3
|
|
22
|
+
# Frozen vocabulary from DSH 0.1.5-rc.1's dsh-session known-event-types.
|
|
23
|
+
KNOWN_EVENTS = frozenset("""
|
|
24
|
+
agent-preset/selected agent/inbox/spliced approval/asked approval/decided
|
|
25
|
+
approval/policy assistant/attempt assistant/message command/done command/run
|
|
26
|
+
compaction/end compaction/prune compaction/start compaction/summary
|
|
27
|
+
deliverables/presented feedback/message-delete feedback/message-put feedback/record
|
|
28
|
+
goal/change hook/invoked hook/result llm/retry llm/retry-started model/selection
|
|
29
|
+
permission/preset plan/mode request/context request/header sandbox/mode
|
|
30
|
+
schedule/change session-log-deepseek/delivery-accepted session/end-seed session/title
|
|
31
|
+
session/title-llm-request step/end step/start subagent/catalog subagent/descriptor
|
|
32
|
+
subagent/model-selection-policy system/message team/member team/message/delivered
|
|
33
|
+
team/message/queued team/task todo/write tool-workflow/agent-end
|
|
34
|
+
tool-workflow/agent-start tool-workflow/run-end tool-workflow/run-start tool/call
|
|
35
|
+
tool/ptc-dispatch tool/ptc-dispatch-start tool/result turn/end turn/start
|
|
36
|
+
user/message web/deepseek-search-llm-request
|
|
37
|
+
""".split())
|
|
38
|
+
V4_EVENTS = KNOWN_EVENTS | {"developer/message", "image/offload", "workspace/changes"}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def check_decoder(executable: str, cwd: Path, environment: dict) -> tuple[dict, str]:
|
|
42
|
+
selected = shutil.which(executable)
|
|
43
|
+
if not selected:
|
|
44
|
+
raise LabError("native DSH capture requires a Node runtime with Zstandard support")
|
|
45
|
+
selected = str(Path(selected).absolute())
|
|
46
|
+
host, streams = capture_process([selected, str(DECODER), "--probe"], cwd, environment, 10)
|
|
47
|
+
try:
|
|
48
|
+
value = json.loads(streams["stdout"])
|
|
49
|
+
except (ValueError, UnicodeError):
|
|
50
|
+
value = {}
|
|
51
|
+
if (
|
|
52
|
+
host["exit_code"] != 0 or host["timed_out"] or host["output_limit_exceeded"]
|
|
53
|
+
or not isinstance(value, dict) or value.get("zstd_supported") is not True
|
|
54
|
+
or not isinstance(value.get("version"), str)
|
|
55
|
+
):
|
|
56
|
+
raise LabError("native DSH capture requires a Node runtime with Zstandard support")
|
|
57
|
+
return {"node_version": value["version"], "decoder": "concatenated-zstd-v1"}, selected
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def session_inventory(home: Path) -> set[Path]:
|
|
61
|
+
"""Inventory names, never contents; refuse symlinks and oversized trees."""
|
|
62
|
+
root = home / "sessions"
|
|
63
|
+
if root.is_symlink():
|
|
64
|
+
raise LabError("native session tree must not contain symlinks")
|
|
65
|
+
if not root.exists():
|
|
66
|
+
return set()
|
|
67
|
+
if not root.is_dir():
|
|
68
|
+
raise LabError("native session root must be a directory")
|
|
69
|
+
files = set()
|
|
70
|
+
count = 0
|
|
71
|
+
def fail(_error):
|
|
72
|
+
raise LabError("native session inventory was unreadable")
|
|
73
|
+
for base, dirs, names in os.walk(root, followlinks=False, onerror=fail):
|
|
74
|
+
if len(Path(base).relative_to(root).parts) > 4:
|
|
75
|
+
raise LabError("native session tree exceeds its depth limit")
|
|
76
|
+
count += len(dirs) + len(names)
|
|
77
|
+
if count > MAX_TREE_ENTRIES:
|
|
78
|
+
raise LabError("native session tree exceeds its entry limit")
|
|
79
|
+
for name in (*dirs, *names):
|
|
80
|
+
item = Path(base) / name
|
|
81
|
+
if item.is_symlink():
|
|
82
|
+
raise LabError("native session tree must not contain symlinks")
|
|
83
|
+
for name in names:
|
|
84
|
+
if name.startswith("session.") and name.endswith((".jsonl", ".jsonl.zstd")):
|
|
85
|
+
files.add(Path(base) / name)
|
|
86
|
+
return files
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _integer(value) -> bool:
|
|
90
|
+
return type(value) is int and 0 <= value <= 2**53 - 1
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _object_pairs(pairs):
|
|
94
|
+
result = {}
|
|
95
|
+
for key, value in pairs:
|
|
96
|
+
if key in result:
|
|
97
|
+
raise ValueError("duplicate key")
|
|
98
|
+
result[key] = value
|
|
99
|
+
return result
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _invalid_constant(_value):
|
|
103
|
+
raise ValueError("non-JSON constant")
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _blocks_supported(blocks, depth=0) -> bool:
|
|
107
|
+
if not isinstance(blocks, list) or depth > 8:
|
|
108
|
+
return False
|
|
109
|
+
for block in blocks:
|
|
110
|
+
if not isinstance(block, dict):
|
|
111
|
+
return False
|
|
112
|
+
kind = block.get("type")
|
|
113
|
+
if not isinstance(kind, str):
|
|
114
|
+
return False
|
|
115
|
+
if kind in {"text", "reasoning"}:
|
|
116
|
+
if not isinstance(block.get("text"), str):
|
|
117
|
+
return False
|
|
118
|
+
elif kind == "tool-call":
|
|
119
|
+
if not all(isinstance(block.get(k), str) and block[k] for k in ("id", "name", "arguments")):
|
|
120
|
+
return False
|
|
121
|
+
elif kind == "tool-result":
|
|
122
|
+
if not isinstance(block.get("toolCallId"), str) or not block["toolCallId"]:
|
|
123
|
+
return False
|
|
124
|
+
if "isError" in block and type(block["isError"]) is not bool:
|
|
125
|
+
return False
|
|
126
|
+
if not _blocks_supported(block.get("content"), depth + 1):
|
|
127
|
+
return False
|
|
128
|
+
else:
|
|
129
|
+
# Attachment bytes and unknown blocks cannot be cleared by text scans.
|
|
130
|
+
return False
|
|
131
|
+
return True
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def split_session(payload: bytes, fixture: Path, task_sha256: str,
|
|
135
|
+
*, parser_version: int = PARSER_VERSION,
|
|
136
|
+
native_format_version: int = 3) -> tuple[dict, dict[str, bytes]]:
|
|
137
|
+
"""Classify model-facing payloads by native event type, not message.role.
|
|
138
|
+
|
|
139
|
+
ToolResultMessage.role is 'user' in v3. Tool-private meta and user/system
|
|
140
|
+
input are preserved in the private raw file, excluded from output scans.
|
|
141
|
+
This reads append-only, unseeded, one-turn fixtures; it does not reconstruct
|
|
142
|
+
arbitrary forks, replacements, attachments or model-visible context.
|
|
143
|
+
Version 2 also scans failed-attempt streams and verifies pinned retry chains.
|
|
144
|
+
Version 1/2 keep the frozen interpretations used by earlier receipts.
|
|
145
|
+
Version 3 additionally accepts a v4 flat tool message whose source event
|
|
146
|
+
reference binds it to the corresponding earlier call, without replay.
|
|
147
|
+
"""
|
|
148
|
+
if type(parser_version) is not int or parser_version not in {1, 2, 3}:
|
|
149
|
+
raise LabError("unsupported native parser version")
|
|
150
|
+
if (type(native_format_version) is not int or native_format_version not in {3, 4}
|
|
151
|
+
or native_format_version == 4 and parser_version < 2):
|
|
152
|
+
raise LabError("unsupported native format/parser contract")
|
|
153
|
+
channels = {"tool": bytearray(), "assistant": bytearray()}
|
|
154
|
+
if parser_version >= 2:
|
|
155
|
+
channels["attempt"] = bytearray()
|
|
156
|
+
counts = {"tool": 0, "assistant": 0, "tool_calls": 0}
|
|
157
|
+
summary = {
|
|
158
|
+
"format_version": native_format_version, "recognized": False, "completed": False,
|
|
159
|
+
"record_count": 0, "counts": counts, "issues": [],
|
|
160
|
+
"cwd_matches": False, "task_matches": False,
|
|
161
|
+
"provenance": "captured-native-event-labels-not-authenticated-authorship",
|
|
162
|
+
}
|
|
163
|
+
if parser_version >= 2:
|
|
164
|
+
summary.update({"parser_version": parser_version, "retry_evidence": {
|
|
165
|
+
"attempts": 0, "scheduled": 0, "started": 0, "stream_chunks": 0,
|
|
166
|
+
}})
|
|
167
|
+
def refuse(issue):
|
|
168
|
+
summary["issues"] = [issue]
|
|
169
|
+
if parser_version >= 2:
|
|
170
|
+
summary["completed"] = False
|
|
171
|
+
return summary, {k: b"" for k in channels}
|
|
172
|
+
if not payload or len(payload) > MAX_SCAN_BYTES or not payload.endswith(b"\n"):
|
|
173
|
+
return refuse("EMPTY_OVERSIZED_OR_UNTERMINATED_SESSION")
|
|
174
|
+
try:
|
|
175
|
+
lines = payload.decode("utf-8").splitlines()
|
|
176
|
+
if not 2 <= len(lines) <= MAX_RECORDS or any(not line.strip() for line in lines):
|
|
177
|
+
return refuse("SESSION_RECORD_LIMIT_OR_EMPTY_LINE")
|
|
178
|
+
records = [json.loads(line, object_pairs_hook=_object_pairs, parse_constant=_invalid_constant) for line in lines]
|
|
179
|
+
except (ValueError, UnicodeError, RecursionError):
|
|
180
|
+
return refuse("INVALID_SESSION_JSON")
|
|
181
|
+
summary["record_count"] = len(records)
|
|
182
|
+
header = records[0]
|
|
183
|
+
if (
|
|
184
|
+
not isinstance(header, dict) or header.get("type") != "session"
|
|
185
|
+
or type(header.get("version")) is not int or header["version"] != native_format_version
|
|
186
|
+
or not isinstance(header.get("id"), str) or not header["id"]
|
|
187
|
+
or not _integer(header.get("createdAt")) or header.get("isSeeded") is not False
|
|
188
|
+
or header.get("parentSession") is not None or header.get("origin") is not None
|
|
189
|
+
or type(header.get("delegationDepth", 0)) is not int or header.get("delegationDepth", 0) != 0
|
|
190
|
+
):
|
|
191
|
+
return refuse("UNSUPPORTED_SESSION_HEADER")
|
|
192
|
+
if native_format_version == 4 and "seedLength" in header:
|
|
193
|
+
return refuse("UNSUPPORTED_SESSION_HEADER")
|
|
194
|
+
try:
|
|
195
|
+
summary["session_id_sha256"] = digest(header["id"].encode())
|
|
196
|
+
except UnicodeError:
|
|
197
|
+
return refuse("INVALID_SESSION_JSON")
|
|
198
|
+
cwd = header.get("cwd")
|
|
199
|
+
try:
|
|
200
|
+
summary["cwd_matches"] = isinstance(cwd, str) and Path(cwd).is_absolute() and Path(cwd).resolve() == fixture.resolve()
|
|
201
|
+
except (OSError, ValueError):
|
|
202
|
+
return refuse("SESSION_CWD_MISMATCH")
|
|
203
|
+
if not summary["cwd_matches"]:
|
|
204
|
+
return refuse("SESSION_CWD_MISMATCH")
|
|
205
|
+
turn = None
|
|
206
|
+
ended = False
|
|
207
|
+
user_count = 0
|
|
208
|
+
calls = {}
|
|
209
|
+
call_events = {}
|
|
210
|
+
results = set()
|
|
211
|
+
step = None
|
|
212
|
+
closed_steps = set()
|
|
213
|
+
route = None
|
|
214
|
+
pending_retry = None
|
|
215
|
+
retry_chain = None
|
|
216
|
+
step_assistant_seen = False
|
|
217
|
+
unresolved_attempt = False
|
|
218
|
+
request_headers = {}
|
|
219
|
+
for expected_seq, record in enumerate(records[1:]):
|
|
220
|
+
if (
|
|
221
|
+
not isinstance(record, dict) or type(record.get("seq")) is not int
|
|
222
|
+
or record["seq"] != expected_seq or not _integer(record.get("time"))
|
|
223
|
+
or not isinstance(record.get("type"), str) or not isinstance(record.get("data"), dict)
|
|
224
|
+
):
|
|
225
|
+
return refuse("INVALID_EVENT_ENVELOPE_OR_SEQUENCE")
|
|
226
|
+
kind = record["type"]
|
|
227
|
+
data = record["data"]
|
|
228
|
+
if native_format_version == 4 and "sourceEventSeqs" in record:
|
|
229
|
+
if parser_version < 3 or kind != "tool/result" or record.get("surfaceOp") != "append":
|
|
230
|
+
return refuse("UNSUPPORTED_SURFACE_REPLAY")
|
|
231
|
+
known_events = V4_EVENTS if native_format_version == 4 else KNOWN_EVENTS
|
|
232
|
+
if kind not in known_events and record.get("ignorable") is not True:
|
|
233
|
+
return refuse("UNKNOWN_REQUIRED_EVENT")
|
|
234
|
+
if kind in {"system/message", "user/message", "assistant/message", "tool/result"} or (
|
|
235
|
+
native_format_version == 4 and kind == "developer/message"):
|
|
236
|
+
if record.get("surfaceOp") != "append":
|
|
237
|
+
return refuse("UNSUPPORTED_SURFACE_OPERATION")
|
|
238
|
+
if kind == "turn/start":
|
|
239
|
+
if turn is not None or not _integer(data.get("turn")):
|
|
240
|
+
return refuse("UNSUPPORTED_MULTIPLE_OR_INVALID_TURNS")
|
|
241
|
+
turn = data["turn"]
|
|
242
|
+
elif kind == "step/start":
|
|
243
|
+
if turn is None or ended or not _integer(data.get("turn")) or data.get("turn") != turn or step is not None or not _integer(data.get("step")) or data["step"] in closed_steps:
|
|
244
|
+
return refuse("INVALID_STEP_START")
|
|
245
|
+
step = data["step"]
|
|
246
|
+
pending_retry, retry_chain, step_assistant_seen = None, None, False
|
|
247
|
+
elif kind == "step/end":
|
|
248
|
+
if step is None or not _integer(data.get("turn")) or not _integer(data.get("step")) or data.get("turn") != turn or data.get("step") != step:
|
|
249
|
+
return refuse("INVALID_STEP_END")
|
|
250
|
+
if parser_version >= 2 and pending_retry is not None:
|
|
251
|
+
if pending_retry["state"] != "attempt":
|
|
252
|
+
return refuse("UNFINISHED_RETRY_CHAIN")
|
|
253
|
+
unresolved_attempt = True
|
|
254
|
+
closed_steps.add(step)
|
|
255
|
+
step = None
|
|
256
|
+
elif kind == "turn/end":
|
|
257
|
+
if turn is None or ended or step is not None or not _integer(data.get("turn")) or data.get("turn") != turn or not isinstance(data.get("reason"), dict) or not isinstance(data["reason"].get("kind"), str):
|
|
258
|
+
return refuse("INVALID_TURN_END")
|
|
259
|
+
ended = True
|
|
260
|
+
summary["completed"] = data["reason"].get("kind") == "completed"
|
|
261
|
+
if parser_version >= 2 and summary["completed"] and unresolved_attempt:
|
|
262
|
+
return refuse("UNRECOVERED_ASSISTANT_ATTEMPT")
|
|
263
|
+
elif parser_version >= 2 and kind == "request/header":
|
|
264
|
+
config = data.get("header", {}).get("config") if isinstance(data.get("header"), dict) else None
|
|
265
|
+
if (turn is None or ended or step is None or not isinstance(config, dict)
|
|
266
|
+
or not all(isinstance(config.get(k), str) and config[k] for k in ("provider", "model"))):
|
|
267
|
+
return refuse("INVALID_RETRY_REQUEST_HEADER")
|
|
268
|
+
if native_format_version == 4 and "system" in data["header"]:
|
|
269
|
+
return refuse("UNSUPPORTED_REQUEST_HEADER")
|
|
270
|
+
request_headers[expected_seq] = data["header"].get("tools", [])
|
|
271
|
+
selected_route = (config["provider"], config["model"])
|
|
272
|
+
if pending_retry is not None and selected_route != route:
|
|
273
|
+
return refuse("RETRY_ROUTE_CHANGED")
|
|
274
|
+
route = selected_route
|
|
275
|
+
elif native_format_version == 4 and kind == "developer/message":
|
|
276
|
+
message = data.get("message")
|
|
277
|
+
if (turn is None or ended or step is None or not _integer(data.get("turn"))
|
|
278
|
+
or not _integer(data.get("step")) or data["turn"] != turn
|
|
279
|
+
or data["step"] != step or not isinstance(message, dict)
|
|
280
|
+
or message.get("role") != "developer"
|
|
281
|
+
or not isinstance(message.get("id"), str) or not message["id"]
|
|
282
|
+
or message.get("source") != {"kind": "tool-registry"}
|
|
283
|
+
or not isinstance(message.get("content"), list) or not message["content"]):
|
|
284
|
+
return refuse("UNSUPPORTED_DEVELOPER_MESSAGE")
|
|
285
|
+
additions = False
|
|
286
|
+
for block in message["content"]:
|
|
287
|
+
if (not isinstance(block, dict) or set(block) != {"type", "toolName"}
|
|
288
|
+
or block["type"] not in {"tool-addition", "tool-removal"}
|
|
289
|
+
or not isinstance(block["toolName"], str) or not block["toolName"]):
|
|
290
|
+
return refuse("UNSUPPORTED_DEVELOPER_MESSAGE")
|
|
291
|
+
additions |= block["type"] == "tool-addition"
|
|
292
|
+
if additions and (not _integer(data.get("headerSeq")) or data["headerSeq"] not in request_headers):
|
|
293
|
+
return refuse("UNBOUND_TOOL_REGISTRY_UPDATE")
|
|
294
|
+
if additions:
|
|
295
|
+
definitions = request_headers[data["headerSeq"]]
|
|
296
|
+
if (not isinstance(definitions, list) or any(not isinstance(item, dict)
|
|
297
|
+
or not isinstance(item.get("name"), str) for item in definitions)
|
|
298
|
+
or any(block["toolName"] not in {item.get("name") for item in definitions}
|
|
299
|
+
for block in message["content"] if block["type"] == "tool-addition")):
|
|
300
|
+
return refuse("UNBOUND_TOOL_REGISTRY_UPDATE")
|
|
301
|
+
elif native_format_version == 4 and kind == "image/offload":
|
|
302
|
+
return refuse("UNSUPPORTED_IMAGE_OFFLOAD")
|
|
303
|
+
elif native_format_version == 4 and kind == "workspace/changes":
|
|
304
|
+
if set(data) != {"turn"} or not _integer(data["turn"]) or data["turn"] != turn:
|
|
305
|
+
return refuse("INVALID_WORKSPACE_CHANGE_NOTICE")
|
|
306
|
+
elif parser_version >= 2 and kind in {"llm/retry", "llm/retry-started"}:
|
|
307
|
+
if (turn is None or ended or step is None or not _integer(data.get("turn"))
|
|
308
|
+
or not _integer(data.get("step")) or data["turn"] != turn or data["step"] != step
|
|
309
|
+
or pending_retry is None or not isinstance(data.get("retryId"), str)
|
|
310
|
+
or not data["retryId"] or not _integer(data.get("retry")) or data["retry"] < 1):
|
|
311
|
+
return refuse("RETRY_LIFECYCLE_MISMATCH")
|
|
312
|
+
if kind == "llm/retry":
|
|
313
|
+
expected = {"retryId", "turn", "step", "provider", "mode", "policyKey", "retry", "delayMs", "failure"}
|
|
314
|
+
if data.get("mode") == "normal":
|
|
315
|
+
expected.add("maxRetries")
|
|
316
|
+
if (set(data) != expected or not isinstance(data.get("mode"), str) or data["mode"] not in {"normal", "always"}
|
|
317
|
+
or route is None or data["provider"] != route[0]
|
|
318
|
+
or not isinstance(data.get("policyKey"), str) or not data["policyKey"]
|
|
319
|
+
or not finite_number(data.get("delayMs")) or not failure_supported(data.get("failure"))
|
|
320
|
+
or pending_retry["state"] != "attempt"
|
|
321
|
+
or pending_retry["finish"] not in {"error", "aborted"}
|
|
322
|
+
or data["failure"] != pending_retry["failure"]
|
|
323
|
+
or data["mode"] == "normal" and (not _integer(data["maxRetries"]) or data["retry"] > data["maxRetries"])):
|
|
324
|
+
return refuse("INVALID_RETRY_SCHEDULE")
|
|
325
|
+
chain = (data["retryId"], data["provider"], data["mode"], data["policyKey"], data.get("maxRetries"))
|
|
326
|
+
expected_retry = 1 if retry_chain is None else retry_chain[1] + 1
|
|
327
|
+
if data["retry"] != expected_retry or retry_chain is not None and retry_chain[0] != chain:
|
|
328
|
+
return refuse("RETRY_CHAIN_ID_OR_SEQUENCE_MISMATCH")
|
|
329
|
+
retry_chain = (chain, data["retry"])
|
|
330
|
+
pending_retry.update({"state": "scheduled", "retryId": data["retryId"], "retry": data["retry"]})
|
|
331
|
+
summary["retry_evidence"]["scheduled"] += 1
|
|
332
|
+
else:
|
|
333
|
+
if (set(data) != {"retryId", "turn", "step", "retry"}
|
|
334
|
+
or pending_retry["state"] != "scheduled"
|
|
335
|
+
or (data["retryId"], data["retry"]) != (pending_retry["retryId"], pending_retry["retry"])):
|
|
336
|
+
return refuse("RETRY_LIFECYCLE_MISMATCH")
|
|
337
|
+
pending_retry["state"] = "started"
|
|
338
|
+
summary["retry_evidence"]["started"] += 1
|
|
339
|
+
try:
|
|
340
|
+
channels["attempt"].extend(json_bytes({"event_type": kind, "data": data}))
|
|
341
|
+
except (ValueError, UnicodeError):
|
|
342
|
+
return refuse("INVALID_SESSION_JSON")
|
|
343
|
+
elif kind in {"tool/call", "tool/result", "assistant/message", "assistant/attempt"}:
|
|
344
|
+
if turn is None or ended or step is None or not _integer(data.get("turn")) or not _integer(data.get("step")) or data.get("turn") != turn or data.get("step") != step:
|
|
345
|
+
return refuse("OUTPUT_OUTSIDE_TURN")
|
|
346
|
+
if kind == "tool/call":
|
|
347
|
+
if parser_version >= 2 and pending_retry is not None:
|
|
348
|
+
return refuse("RETRY_LIFECYCLE_MISMATCH")
|
|
349
|
+
call = data.get("callId")
|
|
350
|
+
if not isinstance(call, str) or not call or call in calls or not all(isinstance(data.get(k), str) and data[k] for k in ("name", "arguments")):
|
|
351
|
+
return refuse("INVALID_OR_DUPLICATE_TOOL_CALL")
|
|
352
|
+
calls[call] = data["step"]
|
|
353
|
+
call_events[call] = expected_seq
|
|
354
|
+
counts["tool_calls"] += 1
|
|
355
|
+
continue
|
|
356
|
+
if kind == "assistant/attempt":
|
|
357
|
+
if parser_version == 1:
|
|
358
|
+
return refuse("UNSUPPORTED_ASSISTANT_ATTEMPT")
|
|
359
|
+
if (set(data) != {"turn", "step", "stream"} or "surfaceOp" in record
|
|
360
|
+
or "sourceEventSeqs" in record or route is None
|
|
361
|
+
or step_assistant_seen or any(call_step == step for call_step in calls.values())
|
|
362
|
+
or pending_retry is not None and pending_retry["state"] != "started"):
|
|
363
|
+
return refuse("INVALID_ASSISTANT_ATTEMPT")
|
|
364
|
+
retry = summary["retry_evidence"]
|
|
365
|
+
if retry["attempts"] >= MAX_ATTEMPTS:
|
|
366
|
+
return refuse("ATTEMPT_STREAM_LIMIT_OR_SHAPE")
|
|
367
|
+
try:
|
|
368
|
+
inspected, scan = inspect_stream(data.get("stream"), MAX_STREAM_CHUNKS - retry["stream_chunks"])
|
|
369
|
+
except (ValueError, TypeError, KeyError, OverflowError, UnicodeError):
|
|
370
|
+
return refuse("INVALID_OR_UNSUPPORTED_ATTEMPT_STREAM")
|
|
371
|
+
channels["attempt"].extend(scan)
|
|
372
|
+
retry["attempts"] += 1
|
|
373
|
+
retry["stream_chunks"] += inspected["chunks"]
|
|
374
|
+
failure = None
|
|
375
|
+
if inspected["finish_kind"] in {"error", "aborted"}:
|
|
376
|
+
failure = data["stream"][-1]["chunk"]["reason"]["failure"]
|
|
377
|
+
pending_retry = {"state": "attempt", "finish": inspected["finish_kind"], "failure": failure}
|
|
378
|
+
continue
|
|
379
|
+
message = data.get("message")
|
|
380
|
+
role = "tool" if kind == "tool/result" else "assistant"
|
|
381
|
+
flat_tool = (role == "tool" and parser_version >= 3 and native_format_version == 4
|
|
382
|
+
and isinstance(message, dict) and message.get("role") == "tool")
|
|
383
|
+
if (
|
|
384
|
+
not isinstance(message, dict) or not isinstance(message.get("id"), str) or not message["id"]
|
|
385
|
+
or message.get("role") != ("tool" if flat_tool else "user" if role == "tool" else "assistant")
|
|
386
|
+
or not isinstance(message.get("source"), dict)
|
|
387
|
+
or message["source"].get("kind") != ("tool" if role == "tool" else "model")
|
|
388
|
+
or not _blocks_supported(message.get("content"))
|
|
389
|
+
):
|
|
390
|
+
return refuse("INVALID_OR_UNSUPPORTED_OUTPUT_MESSAGE")
|
|
391
|
+
if role == "tool":
|
|
392
|
+
blocks = message["content"]
|
|
393
|
+
call = message["source"].get("callId")
|
|
394
|
+
if (
|
|
395
|
+
not isinstance(call, str) or call not in calls or call in results
|
|
396
|
+
or calls[call] != data["step"]
|
|
397
|
+
):
|
|
398
|
+
return refuse("TOOL_RESULT_CORRELATION_FAILED")
|
|
399
|
+
if flat_tool:
|
|
400
|
+
refs = record.get("sourceEventSeqs")
|
|
401
|
+
if (message.get("toolCallId") != call or type(message.get("isError")) is not bool
|
|
402
|
+
or message["source"] != {"kind": "tool", "callId": call}
|
|
403
|
+
or not isinstance(refs, list) or len(refs) != 1
|
|
404
|
+
or type(refs[0]) is not int or refs[0] != call_events[call]
|
|
405
|
+
or any(block["type"] not in {"text", "reasoning"} for block in blocks)):
|
|
406
|
+
return refuse("INVALID_FLAT_TOOL_RESULT_OR_SOURCE")
|
|
407
|
+
is_error = message["isError"]
|
|
408
|
+
else:
|
|
409
|
+
if (len(blocks) != 1 or blocks[0]["type"] != "tool-result"
|
|
410
|
+
or blocks[0]["toolCallId"] != call
|
|
411
|
+
or parser_version >= 3 and "sourceEventSeqs" in record):
|
|
412
|
+
return refuse("TOOL_RESULT_CORRELATION_FAILED")
|
|
413
|
+
is_error = blocks[0].get("isError")
|
|
414
|
+
if "error" in data and (not isinstance(data["error"], dict) or is_error is not True):
|
|
415
|
+
return refuse("INVALID_TOOL_ERROR_METADATA")
|
|
416
|
+
results.add(call)
|
|
417
|
+
elif not all(isinstance(message["source"].get(k), str) and message["source"][k] for k in ("provider", "model")):
|
|
418
|
+
return refuse("INVALID_ASSISTANT_SOURCE")
|
|
419
|
+
if parser_version >= 2 and role == "assistant":
|
|
420
|
+
if step_assistant_seen:
|
|
421
|
+
return refuse("DUPLICATE_ASSISTANT_SETTLEMENT")
|
|
422
|
+
if pending_retry is not None:
|
|
423
|
+
if pending_retry["state"] != "started":
|
|
424
|
+
return refuse("RETRY_LIFECYCLE_MISMATCH")
|
|
425
|
+
if (message["source"]["provider"], message["source"]["model"]) != route:
|
|
426
|
+
return refuse("RETRY_ROUTE_CHANGED")
|
|
427
|
+
pending_retry = None
|
|
428
|
+
step_assistant_seen = True
|
|
429
|
+
# Only the model-facing content, never data.meta, source, or streams.
|
|
430
|
+
try:
|
|
431
|
+
channels[role].extend((json.dumps(message["content"], ensure_ascii=False, separators=(",", ":")) + "\n").encode())
|
|
432
|
+
except UnicodeError:
|
|
433
|
+
return refuse("INVALID_SESSION_JSON")
|
|
434
|
+
counts[role] += 1
|
|
435
|
+
elif kind == "user/message":
|
|
436
|
+
if not isinstance(data.get("source"), dict):
|
|
437
|
+
return refuse("INVALID_TASK_MESSAGE")
|
|
438
|
+
if data["source"].get("kind") == "user":
|
|
439
|
+
user_count += 1
|
|
440
|
+
content = data.get("content")
|
|
441
|
+
if (
|
|
442
|
+
turn is None or ended or step is None or data.get("role") != "user"
|
|
443
|
+
or not isinstance(content, list) or len(content) != 1
|
|
444
|
+
or not isinstance(content[0], dict) or content[0].get("type") != "text"
|
|
445
|
+
or not isinstance(content[0].get("text"), str)
|
|
446
|
+
):
|
|
447
|
+
return refuse("INVALID_TASK_MESSAGE")
|
|
448
|
+
try:
|
|
449
|
+
summary["task_matches"] = digest(content[0]["text"].encode()) == task_sha256
|
|
450
|
+
except UnicodeError:
|
|
451
|
+
return refuse("INVALID_SESSION_JSON")
|
|
452
|
+
if user_count != 1 or not summary["task_matches"]:
|
|
453
|
+
return refuse("SESSION_TASK_MISMATCH")
|
|
454
|
+
if not ended or not counts["assistant"] or set(calls) != results:
|
|
455
|
+
return refuse("INCOMPLETE_SESSION_OR_TOOL_RESULTS")
|
|
456
|
+
if any(len(value) > MAX_SCAN_BYTES for value in channels.values()):
|
|
457
|
+
return refuse("OUTPUT_CHANNEL_LIMIT")
|
|
458
|
+
summary["recognized"] = True
|
|
459
|
+
return summary, {k: bytes(v) for k, v in channels.items()}
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def capture_session(home: Path, before: set[Path], capture: Path, fixture: Path,
|
|
463
|
+
task_sha256: str, node: str, environment: dict,
|
|
464
|
+
*, parser_version: int = PARSER_VERSION,
|
|
465
|
+
native_format_version: int = 3) -> tuple[dict, dict]:
|
|
466
|
+
"""Copy one new artifact. Failure retains CLI captures and stays inconclusive."""
|
|
467
|
+
value = {"status": "REFUSED", "recognized": False, "issues": []}
|
|
468
|
+
captures = {}
|
|
469
|
+
try:
|
|
470
|
+
fresh = session_inventory(home) - before
|
|
471
|
+
value["new_artifact_count"] = len(fresh)
|
|
472
|
+
if len(fresh) != 1:
|
|
473
|
+
value["issues"] = ["NATIVE_SESSION_MISSING" if not fresh else "NATIVE_SESSION_AMBIGUOUS"]
|
|
474
|
+
return value, captures
|
|
475
|
+
source = next(iter(fresh))
|
|
476
|
+
if type(native_format_version) is not int or native_format_version not in {3, 4}:
|
|
477
|
+
raise LabError("unsupported native capture format")
|
|
478
|
+
stem = "session.v" + str(native_format_version) + ".jsonl"
|
|
479
|
+
if source.name not in {stem, stem + ".zstd"}:
|
|
480
|
+
value["issues"] = ["UNSUPPORTED_NATIVE_LAYOUT"]
|
|
481
|
+
return value, captures
|
|
482
|
+
raw = read_regular(source, MAX_SCAN_BYTES)
|
|
483
|
+
target = capture / "session.bin"
|
|
484
|
+
write_new(target, raw)
|
|
485
|
+
captures["session"] = {"bytes": len(raw), "sha256": digest(raw)}
|
|
486
|
+
compression = "zstd" if source.name.endswith(".zstd") else "none"
|
|
487
|
+
value["compression"] = compression
|
|
488
|
+
if compression == "zstd":
|
|
489
|
+
host, streams = capture_process([node, str(DECODER), str(target)], capture, environment, 15)
|
|
490
|
+
if host["exit_code"] != 0 or host["timed_out"] or host["output_limit_exceeded"]:
|
|
491
|
+
value["issues"] = ["NATIVE_DECODE_FAILED_OR_OVERSIZED"]
|
|
492
|
+
return value, captures
|
|
493
|
+
decoded = streams["stdout"]
|
|
494
|
+
else:
|
|
495
|
+
decoded = raw
|
|
496
|
+
write_new(capture / "session.jsonl", decoded)
|
|
497
|
+
captures["session_jsonl"] = {"bytes": len(decoded), "sha256": digest(decoded)}
|
|
498
|
+
summary, _ = split_session(decoded, fixture, task_sha256, parser_version=parser_version,
|
|
499
|
+
native_format_version=native_format_version)
|
|
500
|
+
value.update({"status": "CAPTURED", "recognized": summary["recognized"], "parser": summary})
|
|
501
|
+
except (LabError, OSError, ValueError, RecursionError):
|
|
502
|
+
value["issues"] = ["NATIVE_SESSION_UNREADABLE_OR_OVERSIZED"]
|
|
503
|
+
return value, captures
|