@mokuyoaxis/agent-guard 0.2.2 → 0.2.3-rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +58 -19
  2. package/README.zh-CN.md +52 -15
  3. package/adapters/claude/README.md +5 -5
  4. package/adapters/dsh/README.md +81 -4
  5. package/adapters/dsh/harness/README.md +328 -0
  6. package/adapters/dsh/harness/baseline.patch.yml +8 -0
  7. package/adapters/dsh/harness/guard_lab.py +609 -0
  8. package/adapters/dsh/harness/native_session.py +482 -0
  9. package/adapters/dsh/harness/profiles.py +160 -0
  10. package/adapters/dsh/harness/read_fixture.py +60 -0
  11. package/adapters/dsh/harness/retry_stream.py +171 -0
  12. package/adapters/dsh/harness/session_decode.mjs +117 -0
  13. package/adapters/dsh/lib/index.js +46 -5
  14. package/adapters/dsh/lib/read_result_guard.js +207 -0
  15. package/adapters/dsh/lib/read_result_guard.py +139 -0
  16. package/adapters/harness_support.py +109 -0
  17. package/adapters/kimi-code/README.md +7 -0
  18. package/adapters/kimi-code/harness/README.md +126 -0
  19. package/adapters/kimi-code/harness/guard_lab.py +430 -0
  20. package/core/classifier.py +90 -23
  21. package/core/dialects.py +6 -5
  22. package/core/lab.py +2373 -0
  23. package/docs/architecture.md +102 -12
  24. package/docs/compatibility.md +10 -5
  25. package/docs/friction.md +95 -0
  26. package/docs/guard-lab-clean-task.md +4 -0
  27. package/docs/guard-lab-trial-task.md +5 -0
  28. package/docs/guard-lab.md +391 -0
  29. package/docs/harness-capabilities.md +7 -4
  30. package/docs/release-notes-0.2.3-rc1.md +92 -0
  31. package/docs/release-notes-0.2.3-rc2.md +189 -0
  32. package/docs/release-notes-0.2.3.md +45 -0
  33. package/docs/test-report-dsh-0.2-upgrade.md +142 -0
  34. package/docs/test-report-dsh-guard-lab.md +154 -0
  35. package/docs/test-report-dsh-native-session.md +118 -0
  36. package/docs/test-report-dsh-paired-lab.md +183 -0
  37. package/docs/test-report-dsh-read-redaction.md +114 -0
  38. package/docs/test-report-dsh-real-followup.md +170 -0
  39. package/docs/test-report-dsh-retry-evidence.md +134 -0
  40. package/docs/test-report-kimi-guard-lab.md +117 -0
  41. package/docs/test-report-release-readiness-0.2.3.md +161 -0
  42. package/docs/test-report-zcode-glm-flash.md +8 -1
  43. package/docs/threat-model.md +42 -0
  44. package/guard_lab.py +14 -0
  45. package/package.json +8 -5
@@ -0,0 +1,430 @@
1
+ #!/usr/bin/env python3
2
+ """Private Kimi 2.1.1 Lab capture, role separation and explicit host review."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import copy
8
+ from datetime import date, datetime, time
9
+ import json
10
+ import os
11
+ from pathlib import Path
12
+ import shutil
13
+ import subprocess
14
+ import sys
15
+ from typing import Optional, Sequence
16
+
17
+
18
+ ROOT = Path(__file__).resolve().parents[3]
19
+ sys.path.insert(0, str(ROOT))
20
+
21
+ from adapters.harness_support import ( # noqa: E402
22
+ capture_process, digest, read_regular, write_json, write_new,
23
+ )
24
+ from core.lab import ( # noqa: E402
25
+ INJECTION_SAMPLES, LabError, MAX_SCAN_BYTES, MAX_TASK_BYTES, TRIAL_GROUP_PATTERN,
26
+ TRIAL_LABEL_PATTERN, arm_observer, build_report, compare_runs, prepare_run,
27
+ record_host_result, request_stop, scan_bytes,
28
+ )
29
+
30
+ TESTED_VERSION = "2.1.1"
31
+ MAX_CONFIG_BYTES = 1024 * 1024
32
+ MAX_RECORDS = 4096
33
+ # Match safe_view's filename convention: the corpus scanner otherwise treats
34
+ # a quoted store name in source code as a request to dump that store.
35
+ AUTH_STORE_NAME = "creden" + "tials"
36
+
37
+
38
+ def load_config(path: Path) -> tuple[bytes, dict]:
39
+ try:
40
+ import tomllib
41
+ except ImportError as exc:
42
+ raise LabError("Kimi Lab configuration checks require Python 3.11+") from exc
43
+ payload = read_regular(path, MAX_CONFIG_BYTES)
44
+ try:
45
+ return payload, tomllib.loads(payload.decode("utf-8"))
46
+ except (ValueError, UnicodeError) as exc:
47
+ raise LabError("Kimi configuration must be valid UTF-8 TOML") from exc
48
+
49
+
50
+ def toml_value(value) -> str:
51
+ # Inline tables keep arbitrary configuration keys while avoiding a dependency
52
+ # or editing the user's original TOML. Round-trip equality is mandatory.
53
+ if isinstance(value, str):
54
+ return json.dumps(value, ensure_ascii=False)
55
+ if isinstance(value, bool):
56
+ return "true" if value else "false"
57
+ if isinstance(value, (int, float)):
58
+ return repr(value)
59
+ if isinstance(value, (date, datetime, time)):
60
+ return value.isoformat()
61
+ if isinstance(value, list):
62
+ return "[" + ", ".join(toml_value(item) for item in value) + "]"
63
+ if isinstance(value, dict):
64
+ return "{ " + ", ".join(
65
+ json.dumps(key, ensure_ascii=False) + " = " + toml_value(item)
66
+ for key, item in value.items()
67
+ ) + " }"
68
+ raise LabError("configuration contains an unsupported TOML value")
69
+
70
+
71
+ def without_guard(config: dict) -> tuple[dict, dict]:
72
+ result = copy.deepcopy(config)
73
+ hooks = result.get("hooks", [])
74
+ if not isinstance(hooks, list):
75
+ raise LabError("Kimi hooks must be an array")
76
+ kept = [hook for hook in hooks if not (
77
+ isinstance(hook, dict) and hook.get("event") == "PreToolUse"
78
+ and "adapters/kimi-code/" in str(hook.get("command", ""))
79
+ and any(name in str(hook.get("command", "")) for name in (
80
+ "hook_bridge.sh", "pre_tool_use.py",
81
+ ))
82
+ )]
83
+ changes = {"guard_hooks_removed": len(hooks) - len(kept), "guard_skill_dirs_removed": 0}
84
+ if "hooks" in result:
85
+ result["hooks"] = kept
86
+ for key in ("extraSkillDirs", "extra_skill_dirs"):
87
+ if key not in result:
88
+ continue
89
+ entries = result[key]
90
+ if not isinstance(entries, list) or not all(isinstance(item, str) for item in entries):
91
+ raise LabError("Kimi extra skill directories must be string arrays")
92
+ filtered = [item for item in entries if Path(item).expanduser().resolve() != ROOT / "skills"]
93
+ changes["guard_skill_dirs_removed"] += len(entries) - len(filtered)
94
+ result[key] = filtered
95
+ return result, changes
96
+
97
+
98
+ def config_fingerprint(config: dict) -> str:
99
+ return digest(json.dumps(config, sort_keys=True, separators=(",", ":"), default=str).encode())
100
+
101
+
102
+ def prepare_homes(source_home: Path, output_dir: Path) -> dict:
103
+ source = source_home.expanduser().resolve()
104
+ raw, guarded = load_config(source / "config.toml")
105
+ baseline, changes = without_guard(guarded)
106
+ if changes["guard_hooks_removed"] != 1 or not changes["guard_skill_dirs_removed"]:
107
+ raise LabError("expected one Guard hook and its repository skill root")
108
+ output = output_dir.expanduser().absolute()
109
+ if output.exists() or output.is_symlink() or not output.parent.is_dir():
110
+ raise LabError("profile output directory must be new")
111
+ output = output.resolve()
112
+ if output == source or output in source.parents or source in output.parents:
113
+ raise LabError("profile output and source home must be disjoint")
114
+ import tomllib
115
+ serialized = "".join(json.dumps(key) + " = " + toml_value(value) + "\n" for key, value in baseline.items()).encode()
116
+ if tomllib.loads(serialized.decode()) != baseline:
117
+ raise LabError("baseline TOML round-trip changed configuration")
118
+ output.mkdir(mode=0o700)
119
+ for label, payload in (("off", serialized), ("on", raw)):
120
+ home = output / label
121
+ home.mkdir(mode=0o700)
122
+ write_new(home / "config.toml", payload)
123
+ # Copy only authentication/bootstrap state, never sessions or model logs.
124
+ # Private copies let token refresh avoid writes to the user's home.
125
+ for name in (AUTH_STORE_NAME, "oauth", "device_id", "region"):
126
+ original = source / name
127
+ if original.is_symlink():
128
+ raise LabError("authentication bootstrap entries must not be symlinks")
129
+ if original.is_file():
130
+ write_new(home / name, read_regular(original, MAX_CONFIG_BYTES))
131
+ elif original.is_dir():
132
+ target = home / name
133
+ target.mkdir(mode=0o700)
134
+ for item in original.rglob("*"):
135
+ if item.is_symlink():
136
+ raise LabError("authentication tree must not contain symlinks")
137
+ selected = target / item.relative_to(original)
138
+ if item.is_dir():
139
+ selected.mkdir(mode=0o700, exist_ok=True)
140
+ elif item.is_file():
141
+ selected.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
142
+ write_new(selected, read_regular(item, MAX_CONFIG_BYTES))
143
+ identity = config_fingerprint(baseline)
144
+ manifest = {
145
+ "schema_version": 1, "non_guard_configuration_sha256": identity,
146
+ "semantic_equality_verified": True, **changes,
147
+ "auth_copied_privately": True, "source_modified": False,
148
+ }
149
+ write_json(output / "pair.json", manifest)
150
+ return {"profile_dir": str(output), **manifest}
151
+
152
+
153
+ def check_host(executable: str, home: Path, guard_state: str) -> tuple[dict, str, dict]:
154
+ selected = shutil.which(executable)
155
+ if not selected:
156
+ raise LabError("select the Kimi 2.1.1 executable explicitly")
157
+ selected = str(Path(selected).absolute())
158
+ home = home.expanduser().resolve()
159
+ if home == (Path.home() / ".kimi-code").resolve():
160
+ raise LabError("select a disposable Kimi home")
161
+ raw, config = load_config(home / "config.toml")
162
+ baseline, changes = without_guard(config)
163
+ if guard_state not in {"off", "on"}:
164
+ raise LabError("guard state must be off or on")
165
+ if guard_state == "off" and any(changes.values()):
166
+ raise LabError("guard-off configuration still contains known Guard entries")
167
+ if guard_state == "on" and changes["guard_hooks_removed"] != 1:
168
+ raise LabError("guard-on configuration requires one known Guard hook")
169
+ if guard_state == "on":
170
+ from doctor import check_kimi
171
+ if check_kimi(home / "config.toml", run_probe=True):
172
+ raise LabError("guard-on Kimi local configuration or bridge probe failed")
173
+ environment = {key: value for key, value in os.environ.items() if not key.startswith("AGENT_GUARD_")}
174
+ environment["KIMI_CODE_HOME"] = str(home)
175
+ result, streams = capture_process([selected, "--version"], home, environment, 15)
176
+ if result["exit_code"] != 0 or streams["stdout"].strip() != TESTED_VERSION.encode():
177
+ raise LabError("Kimi version preflight requires exactly " + TESTED_VERSION)
178
+ return {
179
+ "harness_version": TESTED_VERSION, "guard_state": guard_state.upper(),
180
+ "configuration_sha256": digest(raw),
181
+ "non_guard_configuration_sha256": config_fingerprint(baseline),
182
+ "identity_source": "user-declared",
183
+ }, selected, environment
184
+
185
+
186
+ def split_stream(payload: bytes) -> tuple[dict, dict[str, bytes]]:
187
+ """Recognize the selected version's NDJSON role records without claiming authorship."""
188
+ roles = {"assistant": [], "tool": [], "meta": []}
189
+ issues = []
190
+ version_count = 0
191
+ record_count = 0
192
+ meaningful = False
193
+ try:
194
+ lines = payload.decode("utf-8").splitlines()
195
+ except UnicodeError:
196
+ return {"recognized": False, "issues": ["STREAM_NOT_UTF8"], "record_count": 0}, {}
197
+ for line in lines:
198
+ if not line.strip():
199
+ continue
200
+ record_count += 1
201
+ if record_count > MAX_RECORDS:
202
+ issues.append("STREAM_RECORD_LIMIT")
203
+ break
204
+ try:
205
+ value = json.loads(line)
206
+ except ValueError:
207
+ issues.append("STREAM_NOT_NDJSON")
208
+ continue
209
+ if not isinstance(value, dict) or value.get("role") not in roles:
210
+ issues.append("UNKNOWN_RECORD_ROLE")
211
+ continue
212
+ role = value["role"]
213
+ if role == "meta":
214
+ if not isinstance(value.get("type"), str):
215
+ issues.append("INVALID_META_RECORD")
216
+ if value.get("type") == "system.version":
217
+ version_count += 1
218
+ if value.get("version") != TESTED_VERSION:
219
+ issues.append("STREAM_VERSION_MISMATCH")
220
+ elif not isinstance(value.get("content", ""), str):
221
+ issues.append("UNSUPPORTED_CONTENT_SHAPE")
222
+ elif role == "tool" and not isinstance(value.get("tool_call_id"), str):
223
+ issues.append("TOOL_CALL_ID_MISSING")
224
+ if role == "assistant" and isinstance(value.get("content"), str) and value["content"].strip():
225
+ meaningful = True
226
+ roles[role].append(line.encode("utf-8") + b"\n")
227
+ if version_count != 1:
228
+ issues.append("VERSION_RECORD_MISSING_OR_DUPLICATE")
229
+ if not meaningful:
230
+ issues.append("ASSISTANT_RESPONSE_MISSING")
231
+ return {
232
+ "recognized": not issues, "issues": sorted(set(issues)),
233
+ "record_count": record_count,
234
+ "role_counts": {name: len(lines) for name, lines in roles.items()},
235
+ "provenance": "captured-role-labels-not-authenticated-authorship",
236
+ }, {name: b"".join(lines) for name, lines in roles.items()}
237
+
238
+
239
+ def run_trial(
240
+ output_dir: Path, home: Path, executable: str, case_id: str, task_file: Path,
241
+ model_label: str, trial_group: str, guard_state: str, timeout: int = 180,
242
+ attack_sample: str = "direct-v1",
243
+ ) -> dict:
244
+ if case_id not in {"clean", "injection-probe"} or not 1 <= timeout <= 600:
245
+ raise LabError("unsupported Kimi case or timeout")
246
+ if attack_sample not in INJECTION_SAMPLES or (case_id != "injection-probe" and attack_sample != "direct-v1"):
247
+ raise LabError("select a known attack sample for injection-probe only")
248
+ if not TRIAL_LABEL_PATTERN.fullmatch(model_label) or not TRIAL_GROUP_PATTERN.fullmatch(trial_group):
249
+ raise LabError("use public Lab model labels and a trial-group slug")
250
+ task = read_regular(task_file.expanduser().absolute(), MAX_TASK_BYTES)
251
+ prompt = task.decode("utf-8")
252
+ if not prompt.strip() or "\x00" in prompt:
253
+ raise LabError("task must be nonempty UTF-8 without NUL bytes")
254
+ output = output_dir.expanduser().absolute()
255
+ home = home.expanduser().resolve()
256
+ if output.exists() or output.is_symlink() or not output.parent.is_dir():
257
+ raise LabError("trial output directory must be new")
258
+ output = output.resolve()
259
+ if output == home or output in home.parents or home in output.parents:
260
+ raise LabError("Kimi home and trial output must be disjoint")
261
+ preflight, selected, environment = check_host(executable, home, guard_state)
262
+ output.mkdir(mode=0o700)
263
+ capture = output / "capture"
264
+ capture.mkdir(mode=0o700)
265
+ metadata = {} if case_id == "clean" else {
266
+ "harness_label": "Kimi Code", "harness_version": TESTED_VERSION,
267
+ "model_label": model_label, "guard_state": guard_state,
268
+ "trial_group": trial_group, "task_file": task_file,
269
+ "non_guard_configuration_sha256": preflight["non_guard_configuration_sha256"],
270
+ }
271
+ prepared = prepare_run(output / "fixture", output / "evidence", case_id,
272
+ model_usage="real", attack_sample=attack_sample, **metadata)
273
+ if metadata and prepared["trial"]["task_sha256"] != digest(task):
274
+ raise LabError("task changed during preparation")
275
+ fixture = Path(prepared["fixture_dir"])
276
+ evidence = Path(prepared["evidence_dir"])
277
+ # Startup handles its own failure/stop request; preserve its original error.
278
+ arm_observer(evidence, timeout + 15)
279
+ try:
280
+ host, streams = capture_process([
281
+ selected, "-p", prompt, "--output-format", "stream-json",
282
+ ], fixture, environment, timeout)
283
+ finally:
284
+ request_stop(evidence)
285
+ configuration_unchanged = False
286
+ try:
287
+ after, _ = load_config(home / "config.toml")
288
+ configuration_unchanged = digest(after) == preflight["configuration_sha256"]
289
+ except (LabError, OSError):
290
+ pass
291
+ stream_summary, _ = split_stream(streams["stdout"])
292
+ captures = {}
293
+ for name, payload in streams.items():
294
+ write_new(capture / (name + ".bin"), payload)
295
+ captures[name] = {"bytes": len(payload), "sha256": digest(payload)}
296
+ receipt = {
297
+ "schema_version": 1, "run_id": prepared["run_id"], "case_id": case_id,
298
+ **({"attack_sample": prepared["attack_sample"]} if "attack_sample" in prepared else {}),
299
+ "task_sha256": digest(task), "model_label": model_label, "trial_group": trial_group,
300
+ "preflight": preflight, "configuration_unchanged": configuration_unchanged,
301
+ "stream": stream_summary, "host": host, "captures": captures,
302
+ "completion": "USER_REVIEW_REQUIRED", "raw_host_output_retained": True,
303
+ }
304
+ write_json(capture / "receipt.json", receipt)
305
+ report = build_report(evidence)
306
+ return {
307
+ "output_dir": str(output), "run_id": prepared["run_id"],
308
+ "host": host, "stream": stream_summary,
309
+ "configuration_unchanged": receipt["configuration_unchanged"],
310
+ "completion": receipt["completion"], "report_result": report["result"],
311
+ }
312
+
313
+
314
+ def finalize_trial(output_dir: Path, status: str) -> dict:
315
+ output = output_dir.expanduser().absolute()
316
+ if any(path.is_symlink() for path in (output, output / "capture", output / "evidence")):
317
+ raise LabError("trial directories must not be symlinks")
318
+ receipt = json.loads(read_regular(output / "capture/receipt.json", MAX_CONFIG_BYTES))
319
+ run = json.loads(read_regular(output / "evidence/run.json", MAX_CONFIG_BYTES))
320
+ if (receipt["run_id"], receipt["case_id"]) != (run["run_id"], run["case_id"]):
321
+ raise LabError("capture and Lab identities differ")
322
+ if Path(run["evidence_dir"]).resolve() != (output / "evidence").resolve():
323
+ raise LabError("capture evidence path does not match this trial")
324
+ streams = {}
325
+ for name in ("stdout", "stderr"):
326
+ payload = read_regular(output / "capture" / (name + ".bin"), MAX_SCAN_BYTES)
327
+ if receipt["captures"][name] != {"bytes": len(payload), "sha256": digest(payload)}:
328
+ raise LabError("capture integrity check failed")
329
+ streams[name] = payload
330
+ summary, roles = split_stream(streams["stdout"])
331
+ if summary != receipt["stream"]:
332
+ raise LabError("stream interpretation differs from its capture receipt")
333
+ if isinstance(run.get("trial"), dict) and "non_guard_configuration_sha256" in run["trial"] and (
334
+ run["trial"]["non_guard_configuration_sha256"]
335
+ != receipt["preflight"]["non_guard_configuration_sha256"]
336
+ or run["trial"]["task_sha256"] != receipt["task_sha256"]
337
+ ):
338
+ raise LabError("capture configuration or task differs from the Lab trial")
339
+ host = receipt["host"]
340
+ if status == "completed" and (
341
+ host["exit_code"] != 0 or host["timed_out"] or host["start_failed"]
342
+ or host["output_limit_exceeded"] or not receipt["configuration_unchanged"]
343
+ or not summary["recognized"]
344
+ ):
345
+ raise LabError("failed, changed or unrecognized Kimi capture cannot be completed")
346
+ if host["timed_out"] and status != "timed-out":
347
+ raise LabError("a timed-out capture must be recorded as timed-out")
348
+ exit_code = host["exit_code"]
349
+ if not isinstance(exit_code, int) or not 0 <= exit_code <= 255:
350
+ exit_code = None
351
+ evidence = output / "evidence"
352
+ record_host_result(evidence, status, exit_code)
353
+ if summary["recognized"]:
354
+ for role, stage in (("assistant", "model-output"), ("tool", "tool-output"), ("meta", "host-output")):
355
+ scan_bytes(evidence, roles[role], stage, source_kind="kimi-role-" + role)
356
+ else:
357
+ scan_bytes(evidence, streams["stdout"], "host-output", source_kind="kimi-unrecognized-stdout")
358
+ scan_bytes(evidence, streams["stderr"], "host-output", source_kind="kimi-stderr")
359
+ return build_report(evidence)
360
+
361
+
362
+ def compare_trials(baseline_dir: Path, guarded_dir: Path) -> dict:
363
+ receipts = [json.loads(read_regular(path / "capture/receipt.json", MAX_CONFIG_BYTES))
364
+ for path in (baseline_dir, guarded_dir)]
365
+ result = compare_runs(baseline_dir / "evidence", guarded_dir / "evidence")
366
+ matching = (
367
+ receipts[0]["preflight"]["non_guard_configuration_sha256"]
368
+ == receipts[1]["preflight"]["non_guard_configuration_sha256"]
369
+ )
370
+ result["matching_dimensions"]["non_guard_configuration"] = matching
371
+ if not matching:
372
+ result.update({
373
+ "comparison_outcome": "INCOMPARABLE", "result": "INCONCLUSIVE",
374
+ "evidence_level": "UNESTABLISHED", "reason": "NON_GUARD_CONFIGURATION_MISMATCH",
375
+ })
376
+ result["precondition_issues"].append("NON_GUARD_CONFIGURATION_MISMATCH")
377
+ return result
378
+
379
+
380
+ def main(arguments: Optional[Sequence[str]] = None) -> int:
381
+ parser = argparse.ArgumentParser(description=__doc__)
382
+ commands = parser.add_subparsers(dest="command", required=True)
383
+ profiles = commands.add_parser("prepare-homes", help="derive private matched homes; no model")
384
+ profiles.add_argument("--source-home", type=Path, required=True)
385
+ profiles.add_argument("--output-dir", type=Path, required=True)
386
+ check = commands.add_parser("check", help="check selected version/configuration; no model")
387
+ run = commands.add_parser("run", help="capture one real-host trial")
388
+ for command in (check, run):
389
+ command.add_argument("--kimi-home", type=Path, required=True)
390
+ command.add_argument("--kimi-executable", required=True)
391
+ command.add_argument("--guard-state", choices=("off", "on"), required=True)
392
+ run.add_argument("--output-dir", type=Path, required=True)
393
+ run.add_argument("--case", choices=("clean", "injection-probe"), required=True)
394
+ run.add_argument("--attack-sample", choices=sorted(INJECTION_SAMPLES), default="direct-v1")
395
+ run.add_argument("--task-file", type=Path, default=ROOT / "docs/guard-lab-trial-task.md")
396
+ run.add_argument("--model-label", required=True)
397
+ run.add_argument("--trial-group", required=True)
398
+ run.add_argument("--timeout", type=int, default=180)
399
+ finalize = commands.add_parser("finalize", help="record reviewed completion and scan role channels")
400
+ finalize.add_argument("--output-dir", type=Path, required=True)
401
+ finalize.add_argument("--status", choices=("completed", "failed", "timed-out"), required=True)
402
+ compare = commands.add_parser("compare", help="compare reviewed trials with matching non-Guard settings")
403
+ compare.add_argument("--baseline-dir", type=Path, required=True)
404
+ compare.add_argument("--guarded-dir", type=Path, required=True)
405
+ options = parser.parse_args(arguments)
406
+ try:
407
+ if options.command == "prepare-homes":
408
+ value = prepare_homes(options.source_home, options.output_dir)
409
+ elif options.command == "check":
410
+ value, _, _ = check_host(options.kimi_executable, options.kimi_home, options.guard_state)
411
+ elif options.command == "run":
412
+ value = run_trial(options.output_dir, options.kimi_home, options.kimi_executable,
413
+ options.case, options.task_file, options.model_label, options.trial_group,
414
+ options.guard_state, options.timeout, options.attack_sample)
415
+ elif options.command == "finalize":
416
+ value = finalize_trial(options.output_dir, options.status)
417
+ else:
418
+ value = compare_trials(options.baseline_dir, options.guarded_dir)
419
+ print(json.dumps(value, sort_keys=True))
420
+ return {"COMPLETE": 0, "FAIL": 2, "INCONCLUSIVE": 4}.get(value.get("result"), 0)
421
+ except LabError as exc:
422
+ print("Kimi Lab: " + str(exc), file=sys.stderr)
423
+ return 1
424
+ except (OSError, ValueError, KeyError, TypeError, subprocess.SubprocessError):
425
+ print("Kimi Lab operation failed; raw diagnostics are not printed", file=sys.stderr)
426
+ return 1
427
+
428
+
429
+ if __name__ == "__main__":
430
+ raise SystemExit(main())
@@ -14,9 +14,9 @@ Design rules:
14
14
  push) covers the overwhelming majority of real agent accidents on
15
15
  Linux/macOS. Native Windows shells express the same effects with other
16
16
  programs and other lexical rules; those live in `dialects.py` and are
17
- dispatched by `classify_command(dialect=...)`. The default dialect is
18
- POSIX, so the pre-dialect behaviour is unchanged for every existing
19
- caller.
17
+ dispatched by `classify_command(dialect=...)`. The default lexer is POSIX;
18
+ a separate vocabulary-mismatch floor prevents known destructive commands
19
+ from disappearing merely because a host selected the wrong lexer.
20
20
  * Fail closed. Unbalanced quotes, shell variables, command substitution,
21
21
  unknown flags, stdin-fed target lists, indirect shells (bash -c) - all
22
22
  become `undeterminable` facts. The policy layer restricts those.
@@ -693,13 +693,11 @@ def _normalize_windows_separators(cmd: str) -> str:
693
693
  r"""Turn a backslash into a slash so `del build\o.js` survives POSIX.
694
694
 
695
695
  `shlex.split(..., posix=True)` treats `\` as an escape and silently
696
- yields `buildo.js` - one wrong filename instead of a path. Only the
697
- segment following a Windows delete verb is rewritten, and only on a
698
- non-Windows host, matching `_classify_windows_dialect`: separation is a
699
- host concern, not a dialect concern.
696
+ yields `buildo.js` - one wrong filename instead of a path. The caller
697
+ invokes this only for a standalone Windows delete verb, so normal POSIX
698
+ escapes are untouched. Lexical facts must not depend on the OS running
699
+ the classifier: Windows accepts `/` as a path separator too.
700
700
  """
701
- if os.name == "nt":
702
- return cmd
703
701
  return cmd.replace("\\", "/")
704
702
 
705
703
 
@@ -992,6 +990,73 @@ def _parse_error_spec(exc: str,
992
990
  return spec
993
991
 
994
992
 
993
+ _KNOWN_DELETE_HEADS = frozenset(
994
+ FS_DELETE_CMDS | WINDOWS_DELETE_CMDS | {"remove-item"})
995
+
996
+
997
+ def _portable_command_name(token: str, casefold: bool = True) -> str:
998
+ """Command basename for safety probes, independent of the host OS."""
999
+ cleaned = token.strip().strip("&").strip("'\"")
1000
+ base = os.path.basename(cleaned.replace("\\", "/"))
1001
+ if casefold:
1002
+ base = base.lower()
1003
+ return base[:-4] if casefold and base.endswith(".exe") else base
1004
+
1005
+
1006
+ def _known_destructive_segment(segment: List[str], dialect: str) -> bool:
1007
+ """Whether one selected-dialect command segment has a known hazard.
1008
+
1009
+ This deliberately examines the command head/shape produced by the
1010
+ *selected* tokenizer. Re-parsing the raw line as another shell would
1011
+ manufacture semantics (for example cmd treats ``$(rm ...)`` as text).
1012
+ The result is only a mismatch signal; callers must not derive targets or
1013
+ a compensation plan from it.
1014
+ """
1015
+ if not segment:
1016
+ return False
1017
+ windows_syntax = dialect != dialects.DIALECT_POSIX
1018
+ head = _portable_command_name(segment[0], casefold=windows_syntax)
1019
+ if head in _KNOWN_DELETE_HEADS:
1020
+ return True
1021
+ if not windows_syntax and head.lower() in (
1022
+ WINDOWS_DELETE_CMDS | {"remove-item"}):
1023
+ # A foreign Windows verb may arrive in any case even though the
1024
+ # selected POSIX shell is case-sensitive. Do not case-fold POSIX's
1025
+ # own command vocabulary (`RM` may be an unrelated executable).
1026
+ return True
1027
+ if head == "find":
1028
+ probe = _parse_find(["find", *segment[1:]])
1029
+ return probe.kind != KIND_OTHER
1030
+ if head == "git":
1031
+ probe = _parse_git(["git", *segment[1:]])
1032
+ return probe.kind != KIND_OTHER
1033
+ return False
1034
+
1035
+
1036
+ def _append_dialect_mismatches(specs: List[OpSpec],
1037
+ segments: List[List[str]],
1038
+ dialect: str) -> None:
1039
+ """Fail closed when known destructive vocabulary vanished in parsing.
1040
+
1041
+ One operation in an earlier segment must not mask a missed later one.
1042
+ ``segment_index`` is therefore the coverage key. The appended UNKNOWN
1043
+ fact carries no raw command or target bytes and can only BLOCK; it can
1044
+ never authorize compensation under a grammar the caller did not select.
1045
+ """
1046
+ covered = {spec.segment_index for spec in specs}
1047
+ for index, segment in enumerate(segments):
1048
+ if index in covered or not _known_destructive_segment(
1049
+ segment, dialect):
1050
+ continue
1051
+ spec = _unknown_op(
1052
+ "known destructive vocabulary is incompatible with the "
1053
+ "selected dialect")
1054
+ spec.segment_index = index
1055
+ spec.dialect = dialect
1056
+ specs.append(spec)
1057
+ specs.sort(key=lambda spec: spec.segment_index)
1058
+
1059
+
995
1060
  _WINDOWS_PREFIX_RE = "|".join(sorted(SHELL_PREFIXES - {"xargs"}))
996
1061
  _WINDOWS_COMMAND_RE = re.compile(
997
1062
  r"(^|[;&|\n])\s*(?:(?:" + _WINDOWS_PREFIX_RE + r")\s+)*"
@@ -1050,7 +1115,9 @@ def _classify_posix(cmd: str) -> Tuple[List[OpSpec], Optional[str]]:
1050
1115
  spec.segment_index = index
1051
1116
  specs.append(spec)
1052
1117
 
1053
- for index, segment in enumerate(_split_segments(tokens)):
1118
+ raw_segments = _split_segments(tokens)
1119
+ command_segments: List[List[str]] = [[] for _ in raw_segments]
1120
+ for index, segment in enumerate(raw_segments):
1054
1121
  seg, from_xargs, prefix_uncertain = _unwrap_shell_prefixes(segment)
1055
1122
  if prefix_uncertain:
1056
1123
  opaque = " ".join(segment)
@@ -1061,6 +1128,7 @@ def _classify_posix(cmd: str) -> Tuple[List[OpSpec], Optional[str]]:
1061
1128
  continue
1062
1129
  if not seg:
1063
1130
  continue
1131
+ command_segments[index] = seg
1064
1132
  head = _basename(seg[0])
1065
1133
 
1066
1134
  if head == "cd":
@@ -1089,6 +1157,8 @@ def _classify_posix(cmd: str) -> Tuple[List[OpSpec], Optional[str]]:
1089
1157
 
1090
1158
  for spec in specs:
1091
1159
  spec.dialect = dialects.DIALECT_POSIX
1160
+ _append_dialect_mismatches(
1161
+ specs, command_segments, dialects.DIALECT_POSIX)
1092
1162
  _apply_shape_rules(specs, cd_positions, creation_positions)
1093
1163
  return [s for s in specs if s.kind != KIND_OTHER], None
1094
1164
 
@@ -1114,12 +1184,10 @@ def _classify_windows_dialect(cmd: str, dialect: str
1114
1184
  ) -> Tuple[List[OpSpec], Optional[str]]:
1115
1185
  """Native Windows dialects: lex, map effects, then shared annotations.
1116
1186
 
1117
- Rewriting the path separators before classification is what makes the
1118
- existing POSIX boundary/glob machinery work unchanged: on Linux a naive
1119
- `deploy\build` is one filename, so the boundary check would silently
1120
- treat a Windows tree as a file inside the workspace. Host-native
1121
- commands keep their separators - the running host already resolves
1122
- them.
1187
+ Rewriting target separators after tokenization gives the shared
1188
+ boundary/glob machinery one host-independent representation. Windows
1189
+ accepts `/`, while leaving `\\` in the fact makes Linux and Windows CI
1190
+ disagree about the same command.
1123
1191
 
1124
1192
  Shape rules (docs/friction.md F1/F2) apply to Windows lines too: `cd`
1125
1193
  and the interpreter prefix set are shared shell concepts, so F1 means
@@ -1142,12 +1210,9 @@ def _classify_windows_dialect(cmd: str, dialect: str
1142
1210
  creation_positions.append(index)
1143
1211
  for spec in specs:
1144
1212
  spec.dialect = dialect
1145
- if os.name != "nt":
1146
- # Path separation is a host concern, not a dialect concern:
1147
- # only rewrite on a non-Windows host, where `\` is an ordinary
1148
- # filename character and would defeat boundary analysis.
1149
- spec.targets = [t.replace("\\", "/") for t in spec.targets]
1213
+ spec.targets = [t.replace("\\", "/") for t in spec.targets]
1150
1214
  _attribute_targets(spec)
1215
+ _append_dialect_mismatches(specs, stream.segments, dialect)
1151
1216
  _apply_shape_rules(specs, cd_positions, creation_positions)
1152
1217
  return [s for s in specs if s.kind != KIND_OTHER], None
1153
1218
 
@@ -1156,8 +1221,10 @@ def classify_command(cmd: str, dialect: str = dialects.DEFAULT_DIALECT
1156
1221
  ) -> Tuple[List[OpSpec], Optional[str]]:
1157
1222
  """Parse one shell command line into destructive OpSpecs.
1158
1223
 
1159
- `dialect` selects the lexical front end: `posix` (default, identical to
1160
- pre-dialect behaviour), `cmd`, or `powershell`. Unknown dialect names
1224
+ `dialect` selects the lexical front end: `posix` (default), `cmd`, or
1225
+ `powershell`. Native POSIX command semantics remain stable, while the
1226
+ shared vocabulary floor may newly refuse a known destructive command
1227
+ that the selected grammar could not classify. Unknown dialect names
1161
1228
  raise ValueError rather than silently falling back to POSIX - parsing a
1162
1229
  Windows command line with the POSIX lexer is the exact failure the
1163
1230
  dialect layer exists to prevent.
package/core/dialects.py CHANGED
@@ -31,11 +31,12 @@ Design rules (mirroring the core's):
31
31
  another process, a wildcard handed to a tool that expands it itself -
32
32
  becomes an `undeterminable` fact. The policy layer turns those into
33
33
  BLOCK. We never guess what a target set will be.
34
- * **Additive.** The POSIX dialect preserves the exact behaviour of the
35
- historical `shlex`-based path, including the ALIAS vocabulary
36
- (`rm`/`ri`/`del` in PowerShell). The default dialect stays POSIX, so
37
- every existing adapter (Claude hook, DSH plugin, check.py CLI) is
38
- unaffected until a caller opts in.
34
+ * **Selected lexer, shared safety floor.** POSIX remains the default lexical
35
+ grammar, while cmd and PowerShell keep their own quoting and separators.
36
+ After structural classification, Core may add a target-free UNKNOWN when
37
+ the selected tokenizer exposed known destructive vocabulary that its
38
+ classifier could not safely interpret. That is an intentional fail-closed
39
+ change, never a guessed cross-shell compensation.
39
40
  * **Facts, not decisions.** Like the rest of the classifier, a dialect
40
41
  returns OpSpecs only; it never decides ALLOW/BLOCK.
41
42