agent-bios 0.15.0 → 0.17.0

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 (66) hide show
  1. package/DEPENDENCIES.md +37 -13
  2. package/README.md +421 -32
  3. package/claude/CLAUDE.md +3 -3
  4. package/claude/agents/frontier.md +1 -1
  5. package/claude/agents/sweep.md +3 -3
  6. package/claude/agents/workhorse.md +2 -2
  7. package/claude/guides/claude-prompting.md +72 -39
  8. package/claude/guides/cli-multi-model-workflow.md +33 -15
  9. package/claude/guides/gpt-prompting.md +103 -39
  10. package/claude/guides/review-request.md +27 -0
  11. package/claude/guides/session-distill-workflow.md +54 -2
  12. package/claude/guides/slide-writing/RUNBOOK.md +137 -0
  13. package/claude/guides/slide-writing/scripts/pair.py +979 -0
  14. package/claude/guides/slide-writing/scripts/render.mjs +82 -0
  15. package/claude/guides/slide-writing.md +195 -0
  16. package/claude/guides/svg-visualization-guide.md +9 -0
  17. package/claude/guides/tooling-gotchas.md +1 -1
  18. package/claude/guides/verification-discipline.md +5 -1
  19. package/claude/hooks/tooling-gotchas-hook.py +9 -14
  20. package/claude/skills/understand/SKILL.md +83 -0
  21. package/codex/AGENTS.md +3 -3
  22. package/codex/agents/frontier.toml +2 -1
  23. package/codex/agents/reviewer.toml +1 -1
  24. package/codex/agents/sweep.toml +3 -3
  25. package/codex/agents/workhorse.toml +1 -1
  26. package/codex/config-additions.toml +1 -1
  27. package/codex/guides/claude-prompting.md +72 -39
  28. package/codex/guides/cli-multi-model-workflow.md +33 -15
  29. package/codex/guides/gpt-prompting.md +103 -39
  30. package/codex/guides/review-request.md +27 -0
  31. package/codex/guides/session-distill-workflow.md +54 -2
  32. package/codex/guides/slide-writing/RUNBOOK.md +137 -0
  33. package/codex/guides/slide-writing/scripts/pair.py +979 -0
  34. package/codex/guides/slide-writing/scripts/render.mjs +82 -0
  35. package/codex/guides/slide-writing.md +195 -0
  36. package/codex/guides/svg-visualization-guide.md +9 -0
  37. package/codex/guides/tooling-gotchas.md +1 -1
  38. package/codex/guides/verification-discipline.md +5 -1
  39. package/compose/assemble.py +290 -14
  40. package/compose/bootstrap/SKILL.md +129 -0
  41. package/compose/check-domains.py +102 -9
  42. package/compose/corpus-state.py +4 -0
  43. package/compose/corpus.py +387 -0
  44. package/compose/corpus_catalog.py +931 -0
  45. package/compose/corpus_install.py +1670 -0
  46. package/compose/corpus_session.py +825 -0
  47. package/compose/corpus_store.py +1423 -0
  48. package/compose/corpus_transaction.py +256 -0
  49. package/compose/corpus_ui.py +655 -0
  50. package/compose/corpus_understand.py +522 -0
  51. package/compose/domains.json +102 -100
  52. package/compose/register-hooks.py +6 -8
  53. package/install.sh +84 -18
  54. package/launch/agent-launch.py +1413 -193
  55. package/launch/agent-launch.toml +12 -16
  56. package/launch/agent-launch.zsh +16 -2
  57. package/launch/i18n/en.toml +135 -7
  58. package/launch/i18n/ja.toml +135 -7
  59. package/launch/i18n/ko.toml +135 -7
  60. package/launch/shell_integration.py +267 -0
  61. package/learn/collect-learning.py +46 -19
  62. package/learn/migrate-learnings.py +10 -1
  63. package/package.json +13 -3
  64. package/provenance.json +1 -1
  65. package/session-cost.py +22 -2
  66. package/wrappers/codex-helm.sh +3 -3
@@ -0,0 +1,1670 @@
1
+ #!/usr/bin/env python3
2
+ """Private corpus installation and explicit legacy migration.
3
+
4
+ This command owns package-at-rest installation. It deliberately has no host
5
+ activation path: an installed corpus is stored and verifiable, while a launcher
6
+ activation creates the per-session snapshot and pin.
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import argparse
11
+ import base64
12
+ import contextlib
13
+ import hashlib
14
+ import importlib
15
+ import importlib.util
16
+ import json
17
+ import os
18
+ from pathlib import Path, PurePosixPath
19
+ import re
20
+ import shlex
21
+ import shutil
22
+ import sys
23
+ import tempfile
24
+ import time
25
+ import tomllib
26
+ import uuid
27
+ from typing import Any
28
+
29
+ from corpus_transaction import (
30
+ _valid_release,
31
+ TransactionError,
32
+ guard_pending,
33
+ operation_scope,
34
+ operation_scope_active,
35
+ pending_status,
36
+ reject_symlink_ancestors,
37
+ transaction_lock,
38
+ )
39
+
40
+
41
+ SCHEMA_VERSION = 1
42
+ PRIVATE_RECORD = "private-install.json"
43
+ CENTRAL_START = "<!-- agent-bios:central:start -->"
44
+ CENTRAL_END = "<!-- agent-bios:central:end -->"
45
+ PERSONAL_START = "<!-- agent-bios:personal-learnings:start -->"
46
+ PERSONAL_END = "<!-- agent-bios:personal-learnings:end -->"
47
+ CLAUDE_IMPORTS = frozenset(("@central/bundle.md", "@personal/learnings.md"))
48
+ ZSH_HOOK = '[ -r "$HOME/.config/agent-launch/shell.zsh" ] && source "$HOME/.config/agent-launch/shell.zsh"'
49
+ LEARNING_ID = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
50
+
51
+
52
+ class InstallError(RuntimeError):
53
+ pass
54
+
55
+
56
+ def _serialized(method):
57
+ """Keep installer metadata and its owned paths one transaction at a time."""
58
+ def wrapped(self, *args, **kwargs):
59
+ name = method.__name__
60
+ dry = bool(kwargs.get("dry_run", False))
61
+ if name == "install" and len(args) >= 2:
62
+ dry = dry or bool(args[1])
63
+ elif name in {"uninstall"} and args:
64
+ dry = dry or bool(args[0])
65
+ apply = bool(args[0]) if args and name in {"migrate", "reset"} else bool(kwargs.get("apply", False))
66
+ if name == "status" or dry or (name in {"migrate", "reset"} and not apply):
67
+ return method(self, *args, **kwargs)
68
+ with self._installer_lock():
69
+ if name in {"install", "uninstall", "reset"} and not operation_scope_active(self.state_root):
70
+ if any(row["kind"] == "migrate" for row in pending_status(self.state_root)["transactions"]):
71
+ raise InstallError("pending migration requires agent-bios migrate --apply --yes before other writes")
72
+ return method(self, *args, **kwargs)
73
+ return wrapped
74
+
75
+
76
+ def _sha256(path: Path) -> str:
77
+ digest = hashlib.sha256()
78
+ with path.open("rb") as source:
79
+ for chunk in iter(lambda: source.read(1024 * 1024), b""):
80
+ digest.update(chunk)
81
+ return digest.hexdigest()
82
+
83
+
84
+ def _canonical(value: Any) -> bytes:
85
+ return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8")
86
+
87
+
88
+ def _atomic_bytes(path: Path, data: bytes, mode: int | None = None) -> None:
89
+ path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
90
+ if path.is_symlink():
91
+ raise InstallError(f"refusing symlink output: {path}")
92
+ descriptor, raw = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
93
+ temporary = Path(raw)
94
+ try:
95
+ with os.fdopen(descriptor, "wb") as target:
96
+ target.write(data)
97
+ target.flush()
98
+ os.fsync(target.fileno())
99
+ if mode is not None:
100
+ temporary.chmod(mode)
101
+ elif path.exists():
102
+ temporary.chmod(path.stat().st_mode & 0o777)
103
+ os.replace(temporary, path)
104
+ finally:
105
+ temporary.unlink(missing_ok=True)
106
+
107
+
108
+ def _atomic_json(path: Path, value: Any) -> None:
109
+ _atomic_bytes(path, _canonical(value) + b"\n", 0o600)
110
+
111
+
112
+ def _read_json(path: Path) -> dict[str, Any]:
113
+ if path.is_symlink() or not path.is_file():
114
+ raise InstallError(f"missing or unsafe JSON record: {path}")
115
+ try:
116
+ data = json.loads(path.read_text(encoding="utf-8"))
117
+ except (OSError, ValueError) as exc:
118
+ raise InstallError(f"cannot read JSON record {path}: {exc}") from exc
119
+ if not isinstance(data, dict):
120
+ raise InstallError(f"JSON record must be an object: {path}")
121
+ return data
122
+
123
+
124
+ def _relative(value: str, label: str) -> str:
125
+ if not isinstance(value, str) or not value:
126
+ raise InstallError(f"{label} must be a non-empty relative path")
127
+ path = PurePosixPath(value.rstrip("/"))
128
+ if path.is_absolute() or not path.parts or any(part in {"", ".", ".."} for part in path.parts):
129
+ raise InstallError(f"unsafe {label}: {value!r}")
130
+ return path.as_posix()
131
+
132
+
133
+ def _inside(path: Path, root: Path) -> bool:
134
+ try:
135
+ path.absolute().relative_to(root.absolute())
136
+ return True
137
+ except ValueError:
138
+ return False
139
+
140
+
141
+ def _active_import_lines(body: str) -> list[int]:
142
+ """Line numbers of real import directives, excluding fenced examples."""
143
+ result: list[int] = []
144
+ fenced = False
145
+ for index, line in enumerate(body.splitlines(keepends=True)):
146
+ stripped = line.strip()
147
+ if stripped.startswith(("```", "~~~")):
148
+ fenced = not fenced
149
+ continue
150
+ if not fenced and stripped in CLAUDE_IMPORTS:
151
+ result.append(index)
152
+ return result
153
+
154
+
155
+ def _remove_claude_imports(body: str) -> tuple[str, list[str]]:
156
+ lines = body.splitlines(keepends=True)
157
+ indexes = set(_active_import_lines(body))
158
+ removed = [lines[index].strip() for index in sorted(indexes)]
159
+ return "".join(line for index, line in enumerate(lines) if index not in indexes), removed
160
+
161
+
162
+ def _remove_spans(body: str) -> tuple[str | None, list[str]]:
163
+ """Strip exact, non-overlapping managed marker spans or report ambiguity."""
164
+ spans = ((CENTRAL_START, CENTRAL_END, "central"), (PERSONAL_START, PERSONAL_END, "personal-learnings"))
165
+ found: list[tuple[int, int, str]] = []
166
+ for start, end, label in spans:
167
+ starts = [index for index in range(len(body)) if body.startswith(start, index)]
168
+ ends = [index for index in range(len(body)) if body.startswith(end, index)]
169
+ if not starts and not ends:
170
+ continue
171
+ if len(starts) != 1 or len(ends) != 1 or starts[0] >= ends[0]:
172
+ return None, [f"malformed {label} marker span"]
173
+ found.append((starts[0], ends[0] + len(end), label))
174
+ if len(found) == 2 and not (found[0][1] <= found[1][0] or found[1][1] <= found[0][0]):
175
+ return None, ["overlapping managed marker spans"]
176
+ text = body
177
+ removed: list[str] = []
178
+ for start, end, label in sorted(found, reverse=True):
179
+ text = text[:start] + text[end:]
180
+ removed.append(label)
181
+ return text.lstrip("\n"), list(reversed(removed))
182
+
183
+
184
+ def _strip_codex_config_additions(body: str) -> tuple[str | None, bool]:
185
+ """Remove only the exact legacy config block and tagged feature line."""
186
+ begin, end, tag = "# >>> agent-bios additions >>>", "# <<< agent-bios additions <<<", "# agent-bios"
187
+ starts = body.count(begin)
188
+ ends = body.count(end)
189
+ if starts != ends or starts > 1:
190
+ return None, False
191
+ skipping = False
192
+ changed = False
193
+ lines: list[str] = []
194
+ for line in body.splitlines(keepends=True):
195
+ stripped = line.strip()
196
+ if stripped == begin:
197
+ skipping, changed = True, True
198
+ continue
199
+ if stripped == end:
200
+ skipping = False
201
+ continue
202
+ if skipping:
203
+ continue
204
+ if stripped.endswith(tag) and "multi_agent" in stripped:
205
+ changed = True
206
+ continue
207
+ lines.append(line)
208
+ result = "".join(lines)
209
+ if changed:
210
+ try:
211
+ tomllib.loads(result)
212
+ except tomllib.TOMLDecodeError:
213
+ return None, False
214
+ return result, changed
215
+
216
+
217
+ class CorpusInstaller:
218
+ """Private install state plus carefully scoped legacy cleanup."""
219
+
220
+ def __init__(self, repo: Path, environ: dict[str, str] | None = None):
221
+ self.repo = Path(repo).resolve()
222
+ self.env = dict(os.environ if environ is None else environ)
223
+ home = Path(self.env.get("HOME", str(Path.home()))).expanduser()
224
+ self.state_root = Path(self.env.get("AGENT_BIOS_STATE_DIR", str(home / ".local/share/agent-bios"))).expanduser()
225
+ self.user_root = Path(self.env.get("AGENT_BIOS_CORPUS_DIR", str(home / ".config/agent-bios/corpus"))).expanduser()
226
+ self.claude_root = Path(self.env.get("CLAUDE_CONFIG_DIR", str(home / ".claude"))).expanduser()
227
+ self.codex_root = Path(self.env.get("CODEX_HOME", str(home / ".codex"))).expanduser()
228
+ self.zdotdir = Path(self.env.get("ZDOTDIR", str(home))).expanduser()
229
+ self.launch_root = home / ".config" / "agent-launch"
230
+ self.bin_root = home / ".local" / "bin"
231
+ self._installer_lock_depth = 0
232
+
233
+ @property
234
+ def runtime(self) -> Path:
235
+ return self.state_root / "runtime"
236
+
237
+ @property
238
+ def record_path(self) -> Path:
239
+ return self.runtime / PRIVATE_RECORD
240
+
241
+ @contextlib.contextmanager
242
+ def _installer_lock(self):
243
+ """Use the store's lock; lock ordering cannot diverge by caller."""
244
+ try:
245
+ with transaction_lock(self.state_root):
246
+ yield
247
+ except TransactionError as exc:
248
+ raise InstallError(str(exc)) from exc
249
+
250
+ def _package_manifest(self) -> dict[str, Any]:
251
+ return _read_json(self.repo / "package.json")
252
+
253
+ def _package_files(self) -> list[str]:
254
+ manifest = self._package_manifest()
255
+ listed = manifest.get("files")
256
+ if not isinstance(listed, list) or not listed:
257
+ raise InstallError("package.json files must declare the private runtime payload")
258
+ paths: set[str] = {"package.json"} # npm includes this manifest even when files[] omits it.
259
+ prohibited = ("gates/", "design/", "benchmarks/", "research/", "packages/", "session-distill/")
260
+ for raw in listed:
261
+ relative = _relative(raw, "package files entry")
262
+ if relative.startswith(prohibited):
263
+ raise InstallError(f"author-only path is not a private runtime payload: {relative}")
264
+ source = self.repo / relative
265
+ # npm's prepack stamps this author-side provenance receipt. A
266
+ # checkout after postpack legitimately lacks it and no private
267
+ # runtime path consumes it, so it is not an installation blocker.
268
+ if relative == "provenance.json" and not source.exists():
269
+ continue
270
+ if source.is_symlink() or not source.exists():
271
+ raise InstallError(f"declared package file is missing or symlinked: {relative}")
272
+ if source.is_file():
273
+ paths.add(relative)
274
+ continue
275
+ if not source.is_dir():
276
+ raise InstallError(f"declared package entry is not file or directory: {relative}")
277
+ for child in sorted(source.rglob("*")):
278
+ if child.is_symlink():
279
+ raise InstallError(f"package payload contains symlink: {child}")
280
+ if child.is_file():
281
+ child_relative = child.relative_to(self.repo).as_posix()
282
+ if "/test_" in child_relative or child_relative.startswith("tests/"):
283
+ raise InstallError(f"test file is not a runtime payload: {child_relative}")
284
+ paths.add(child_relative)
285
+ return sorted(paths)
286
+
287
+ def _release_digest(self, files: list[str]) -> str:
288
+ digest = hashlib.sha256()
289
+ for relative in files:
290
+ source = self.repo / relative
291
+ digest.update(relative.encode("utf-8") + b"\0" + source.read_bytes() + b"\0")
292
+ return digest.hexdigest()
293
+
294
+ def _copy_release(self, files: list[str], dry_run: bool) -> tuple[Path, list[dict[str, str]], str]:
295
+ digest = self._release_digest(files)
296
+ release = self.runtime / "releases" / digest
297
+ entries = [{"path": relative, "sha256": _sha256(self.repo / relative)} for relative in files]
298
+ if dry_run:
299
+ return release, entries, digest
300
+ if release.exists():
301
+ if release.is_symlink() or not release.is_dir():
302
+ raise InstallError(f"immutable release path is unsafe: {release}")
303
+ self._verify_release(release, entries)
304
+ return release, entries, digest
305
+ release.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
306
+ staging = Path(tempfile.mkdtemp(prefix=f".{digest}.", dir=release.parent))
307
+ try:
308
+ for entry in entries:
309
+ source = self.repo / entry["path"]
310
+ target = staging / entry["path"]
311
+ target.parent.mkdir(parents=True, exist_ok=True)
312
+ shutil.copy2(source, target)
313
+ self._verify_release(staging, entries)
314
+ os.replace(staging, release)
315
+ except BaseException:
316
+ shutil.rmtree(staging, ignore_errors=True)
317
+ raise
318
+ return release, entries, digest
319
+
320
+ def _verify_release(self, release: Path, entries: list[dict[str, str]]) -> None:
321
+ for entry in entries:
322
+ target = release / _relative(entry.get("path", ""), "installed file")
323
+ if target.is_symlink() or not target.is_file() or _sha256(target) != entry.get("sha256"):
324
+ raise InstallError(f"private package release drifted: {target}")
325
+
326
+ def _private_module(self, package_root: Path, stem: str):
327
+ compose = str(package_root / "compose")
328
+ if compose not in sys.path:
329
+ sys.path.insert(0, compose)
330
+ path = package_root / "compose" / f"{stem}.py"
331
+ if not path.is_file() or path.is_symlink():
332
+ raise InstallError(f"private package lacks compose/{stem}.py")
333
+ name = f"agent_bios_private_{stem}_{hashlib.sha256(str(package_root).encode()).hexdigest()[:12]}"
334
+ spec = importlib.util.spec_from_file_location(name, path)
335
+ if spec is None or spec.loader is None:
336
+ raise InstallError(f"cannot load private {stem}")
337
+ module = importlib.util.module_from_spec(spec)
338
+ sys.modules[name] = module
339
+ spec.loader.exec_module(module)
340
+ return module
341
+
342
+ def _store(self, package_root: Path):
343
+ module = self._private_module(package_root, "corpus_store")
344
+ store = module.CorpusStore(package_root, self.state_root, self.user_root)
345
+ # CorpusStore delegates compile/load to a module name for normal package
346
+ # execution. Bind this instance to the release's exact catalog so a
347
+ # long-lived manager cannot retain an older release through sys.modules.
348
+ store._catalog_module = lambda: self._private_module(package_root, "corpus_catalog")
349
+ return store
350
+
351
+ def _catalog(self, package_root: Path) -> dict[str, Any]:
352
+ """Load the catalog from the immutable release being verified."""
353
+ module = self._private_module(package_root, "corpus_catalog")
354
+ return module.load_catalog(package_root)
355
+
356
+ def _normalized_domains(self, raw: str | None) -> list[str]:
357
+ if raw is None or raw.strip() in {"", "all"}:
358
+ return ["all"]
359
+ catalog_path = self.repo / "compose" / "domains.json"
360
+ manifest = _read_json(catalog_path)
361
+ package_id = manifest.get("package_id", "@agent-bios/core")
362
+ known = set((manifest.get("domains") or {}).keys())
363
+ names = [value.strip() for value in raw.split(",") if value.strip()]
364
+ if names == ["none"]:
365
+ return []
366
+ if "none" in names:
367
+ raise InstallError("--domains none must be the only selection value")
368
+ unknown = sorted(set(names) - known)
369
+ if unknown:
370
+ raise InstallError(f"unknown domains: {', '.join(unknown)}")
371
+ return [f"{package_id}/{name}" for name in sorted(set(names))]
372
+
373
+ def _write_launcher_status(self, package_root: Path, selection: list[str]) -> None:
374
+ """Keep the legacy launcher panel readable without claiming activation."""
375
+ manifest = _read_json(package_root / "compose" / "domains.json")
376
+ available = sorted((manifest.get("domains") or {}).keys())
377
+ applied = available if selection == ["all"] else sorted(value.rsplit("/", 1)[-1] for value in selection)
378
+ _atomic_json(self.state_root / "corpus-status.json", {
379
+ "repo": str(package_root), "current_version": None, "latest_version": None,
380
+ "rolled_back_to": None, "versions": None, "summary": None,
381
+ "domains": {"available": available, "applied": applied}, "last_apply": None,
382
+ "deployed_corpus": None, "generated": int(time.time()),
383
+ "mode": "private-session-scoped",
384
+ })
385
+
386
+ def _launcher_status_bytes(self, package_root: Path, selection: list[str], generated: int | None = None) -> bytes:
387
+ """The status projection is part of the same publication as its record."""
388
+ manifest = _read_json(package_root / "compose" / "domains.json")
389
+ available = sorted((manifest.get("domains") or {}).keys())
390
+ applied = available if selection == ["all"] else sorted(value.rsplit("/", 1)[-1] for value in selection)
391
+ return _canonical({
392
+ "repo": str(package_root), "current_version": None, "latest_version": None,
393
+ "rolled_back_to": None, "versions": None, "summary": None,
394
+ "domains": {"available": available, "applied": applied}, "last_apply": None,
395
+ "deployed_corpus": None, "generated": int(time.time()) if generated is None else generated,
396
+ "mode": "private-session-scoped",
397
+ }) + b"\n"
398
+
399
+ @staticmethod
400
+ def _safe_transaction_target(path: Path) -> None:
401
+ try:
402
+ reject_symlink_ancestors(path)
403
+ except TransactionError as exc:
404
+ raise InstallError(str(exc)) from exc
405
+
406
+ @staticmethod
407
+ def _file_version(path: Path, *, allow_bytes: bool = True) -> dict[str, Any]:
408
+ """A journaled exact-state predicate; secrets use hash-only predicates."""
409
+ CorpusInstaller._safe_transaction_target(path)
410
+ if not path.exists():
411
+ return {"exists": False}
412
+ if not path.is_file():
413
+ raise InstallError(f"transaction target is not a regular file: {path}")
414
+ data = path.read_bytes()
415
+ result: dict[str, Any] = {"exists": True, "sha256": hashlib.sha256(data).hexdigest()}
416
+ if allow_bytes:
417
+ result["bytes_b64"] = base64.b64encode(data).decode("ascii")
418
+ return result
419
+
420
+ @staticmethod
421
+ def _planned_version(data: bytes | None) -> dict[str, Any]:
422
+ if data is None:
423
+ return {"exists": False}
424
+ return {"exists": True, "sha256": hashlib.sha256(data).hexdigest(),
425
+ "bytes_b64": base64.b64encode(data).decode("ascii")}
426
+
427
+ @staticmethod
428
+ def _matches_version(path: Path, version: dict[str, Any]) -> bool:
429
+ try:
430
+ CorpusInstaller._safe_transaction_target(path)
431
+ except InstallError:
432
+ return False
433
+ if not version.get("exists"):
434
+ return not path.exists()
435
+ return path.is_file() and _sha256(path) == version.get("sha256")
436
+
437
+ def _transaction_path(self, transaction_id: str) -> Path:
438
+ if not re.fullmatch(r"[A-Za-z0-9._-]+", transaction_id):
439
+ raise InstallError("unsafe transaction id")
440
+ # Store keeps its source-only candidate at ``transactions/<id>``.
441
+ # The installer must never overwrite that source journal.
442
+ return self.runtime / "installer-transactions" / transaction_id / "journal.json"
443
+
444
+ def _journal_write(self, journal: Path, value: dict[str, Any]) -> None:
445
+ _atomic_json(journal, value)
446
+
447
+ def _write_planned(self, path: Path, version: dict[str, Any], mode: int) -> None:
448
+ self._safe_transaction_target(path)
449
+ encoded = version.get("bytes_b64")
450
+ if not isinstance(encoded, str):
451
+ if not version.get("exists"):
452
+ path.unlink(missing_ok=True)
453
+ return
454
+ raise InstallError(f"journal has no replay bytes for {path}")
455
+ _atomic_bytes(path, base64.b64decode(encoded.encode("ascii"), validate=True), mode)
456
+
457
+ def _assert_preflight_paths(self, paths: list[dict[str, Any]]) -> None:
458
+ for entry in paths:
459
+ self._safe_transaction_target(Path(entry["path"]))
460
+ conflicts = [entry["path"] for entry in paths
461
+ if not self._matches_version(Path(entry["path"]), entry["before"])
462
+ and not self._matches_version(Path(entry["path"]), entry["after"])]
463
+ if conflicts:
464
+ raise InstallError("owned path changed during transaction preflight: " + ", ".join(conflicts))
465
+
466
+ def _projection_matches(self, entry: dict[str, Any]) -> bool:
467
+ path, after = Path(entry["path"]), entry["after"]
468
+ return self._matches_version(path, after) and (
469
+ not after.get("exists") or path.stat().st_mode & 0o777 == int(entry["mode"]))
470
+
471
+ def _old_record(self) -> dict[str, Any] | None:
472
+ return _read_json(self.record_path) if self.record_path.exists() else None
473
+
474
+ def _safe_owned_path(self, path: Path, root: Path) -> None:
475
+ """An ownership record cannot redirect writes through a symlinked parent."""
476
+ if not _inside(path, root):
477
+ raise InstallError(f"owned path is outside its private destination: {path}")
478
+ current = root.absolute()
479
+ if current.is_symlink():
480
+ raise InstallError(f"owned destination root is symlinked: {current}")
481
+ relative = path.absolute().relative_to(current)
482
+ for part in relative.parts:
483
+ current = current / part
484
+ if current.is_symlink():
485
+ raise InstallError(f"owned destination has symlink ancestor: {current}")
486
+
487
+ def _expected_owned(self, release: Path) -> dict[str, str]:
488
+ expected = {str(self.bin_root / "agent-launch"): hashlib.sha256(self._launcher_body(release)).hexdigest(),
489
+ str(self.launch_root / "profiles.toml"): _sha256(release / "launch" / "agent-launch.toml")}
490
+ i18n = release / "launch" / "i18n"
491
+ for source in sorted(i18n.glob("*.toml")):
492
+ expected[str(self.launch_root / "i18n" / source.name)] = _sha256(source)
493
+ return expected
494
+
495
+ def _validate_owned_record(self, record: dict[str, Any], release: Path) -> None:
496
+ expected = self._expected_owned(release)
497
+ launcher = record.get("launcher")
498
+ if launcher is not None:
499
+ if not isinstance(launcher, dict) or launcher.get("path") != str(self.bin_root / "agent-launch") \
500
+ or launcher.get("sha256") != expected[str(self.bin_root / "agent-launch")]:
501
+ raise InstallError("private install record has an invalid launcher ownership claim")
502
+ self._safe_owned_path(Path(launcher["path"]), self.bin_root)
503
+ seen: set[str] = set()
504
+ for entry in record.get("config_files") or []:
505
+ if not isinstance(entry, dict) or not isinstance(entry.get("path"), str) or entry["path"] in seen:
506
+ raise InstallError("private install record has invalid config ownership entries")
507
+ seen.add(entry["path"])
508
+ if entry["path"] not in expected or entry.get("sha256") != expected[entry["path"]]:
509
+ raise InstallError(f"private install record has an invalid config ownership claim: {entry['path']}")
510
+ self._safe_owned_path(Path(entry["path"]), self.launch_root)
511
+
512
+ def _owned_hash(self, record: dict[str, Any] | None, path: Path) -> str | None:
513
+ if not record:
514
+ return None
515
+ for entry in [record.get("launcher"), *(record.get("config_files") or [])]:
516
+ if isinstance(entry, dict) and entry.get("path") == str(path):
517
+ value = entry.get("sha256")
518
+ return value if isinstance(value, str) else None
519
+ return None
520
+
521
+ def _write_owned(self, path: Path, content: bytes, old: dict[str, Any] | None, mode: int) -> dict[str, str] | None:
522
+ root = self.bin_root if path == self.bin_root / "agent-launch" else self.launch_root
523
+ self._safe_owned_path(path, root)
524
+ expected = self._owned_hash(old, path)
525
+ if path.exists() and (path.is_symlink() or (expected is None and _sha256(path) != hashlib.sha256(content).hexdigest())
526
+ or (expected is not None and _sha256(path) != expected)):
527
+ return None
528
+ _atomic_bytes(path, content, mode)
529
+ return {"path": str(path), "sha256": hashlib.sha256(content).hexdigest()}
530
+
531
+ def _launcher_body(self, package_root: Path) -> bytes:
532
+ quoted = shlex.quote(str(package_root))
533
+ return ("#!/bin/sh\n"
534
+ "export AGENT_BIOS_PRIVATE_CORPUS=1\n"
535
+ f"export AGENT_BIOS_PACKAGE_ROOT={quoted}\n"
536
+ "exec python3 \"$AGENT_BIOS_PACKAGE_ROOT/launch/agent-launch.py\" \"$@\"\n").encode("utf-8")
537
+
538
+ def _shell_manager(self):
539
+ path = Path(__file__).resolve().parent.parent / "launch/shell_integration.py"
540
+ spec = importlib.util.spec_from_file_location("agent_bios_shell_integration", path)
541
+ if spec is None or spec.loader is None:
542
+ raise InstallError("shell connection manager is unavailable")
543
+ module = importlib.util.module_from_spec(spec)
544
+ spec.loader.exec_module(module)
545
+ return module.ShellIntegration(self.env, self.repo)
546
+
547
+ def _shell_paths(self, action: str) -> list[dict[str, Any]]:
548
+ try:
549
+ manager = self._shell_manager()
550
+ # A receipt-less managed script may survive an interrupted opt-in.
551
+ # With neither, do not inspect or claim the user's startup file.
552
+ if not manager.has_connection():
553
+ return []
554
+ # Install owns and validates the upcoming launcher projection; it
555
+ # must also be able to repair a missing/non-executable entrypoint.
556
+ changes = manager.plan(action, installing=action == "restore")["changes"]
557
+ except (OSError, RuntimeError) as exc:
558
+ raise InstallError(str(exc)) from exc
559
+ return [{"path": str(row["path"]), "before": self._planned_version(row["before"]),
560
+ "after": self._planned_version(row["after"]), "mode": row["mode"], "archive": False}
561
+ for row in changes]
562
+
563
+ @_serialized
564
+ def install(self, domains: str | None = None, dry_run: bool = False) -> dict[str, Any]:
565
+ files = self._package_files()
566
+ requested = self._normalized_domains(domains)
567
+ prior = self._old_record()
568
+ # An update without an explicit selection must not broaden a saved
569
+ # core-only or domain-limited environment back to every domain.
570
+ if domains is None and isinstance((prior or {}).get("selection"), list):
571
+ requested = prior["selection"]
572
+ # A preview is strictly read-only. A real install first refuses an
573
+ # unrelated reset, then finishes only its own pending install before
574
+ # creating an immutable release directory.
575
+ if not dry_run:
576
+ pending = pending_status(self.state_root)["transactions"]
577
+ if any(row["owner"] == "installer" and row["kind"] == "reset" for row in pending):
578
+ raise InstallError("pending reset requires recovery before install")
579
+ with operation_scope(self.state_root):
580
+ self._recover_pending_transactions()
581
+ release, entries, digest = self._copy_release(files, dry_run)
582
+ if dry_run:
583
+ return {"dry_run": True, "release": str(release), "release_digest": digest,
584
+ "files": len(entries), "selection": requested}
585
+ shell_paths = self._shell_paths("restore")
586
+ # Stage the immutable baseline before publishing either source pointers
587
+ # or launcher projections. Store owns those source pointers; this
588
+ # coordinator owns all cross-owner ordering.
589
+ store = self._store(release)
590
+ prepare = getattr(store, "prepare_install", None)
591
+ if not callable(prepare):
592
+ raise InstallError("private corpus store does not support staged install recovery")
593
+ candidate = prepare(requested)
594
+ if not isinstance(candidate, dict) or not isinstance(candidate.get("details"), dict):
595
+ raise InstallError("private corpus store returned an invalid install candidate")
596
+ details = candidate["details"]
597
+ baseline_ref = details.get("baseline_ref")
598
+ if not isinstance(baseline_ref, str):
599
+ raise InstallError("private corpus store candidate has no baseline ref")
600
+ owned: list[tuple[Path, bytes, int]] = [
601
+ (self.bin_root / "agent-launch", self._launcher_body(release), 0o755),
602
+ (self.launch_root / "profiles.toml", (release / "launch" / "agent-launch.toml").read_bytes(), 0o644),
603
+ ]
604
+ owned.extend((self.launch_root / "i18n" / source.name, source.read_bytes(), 0o644)
605
+ for source in sorted((release / "launch" / "i18n").glob("*.toml")))
606
+ conflicts: list[str] = []
607
+ for path, content, _mode in owned:
608
+ current = self._file_version(path)
609
+ expected = self._owned_hash(prior, path)
610
+ if current["exists"] and current.get("sha256") != hashlib.sha256(content).hexdigest() and \
611
+ (expected is None or current.get("sha256") != expected):
612
+ conflicts.append(str(path))
613
+ if conflicts:
614
+ raise InstallError("private install cannot replace user-owned paths: " + ", ".join(conflicts))
615
+ launcher = {"path": str(owned[0][0]), "sha256": hashlib.sha256(owned[0][1]).hexdigest()}
616
+ config = [{"path": str(path), "sha256": hashlib.sha256(content).hexdigest()}
617
+ for path, content, _mode in owned[1:]]
618
+ record = {
619
+ "schema_version": SCHEMA_VERSION, "package_root": str(release), "release_digest": digest,
620
+ "installed_files": entries, "selection": requested, "baseline_ref": baseline_ref,
621
+ "launcher": launcher, "config_files": config, "created_at": int(time.time()),
622
+ "mode": "private-session-scoped", "needs_action": [],
623
+ }
624
+ paths = [{"path": str(path), "before": self._file_version(path),
625
+ "after": self._planned_version(content), "mode": mode}
626
+ for path, content, mode in owned]
627
+ paths.extend(shell_paths)
628
+ paths.extend([
629
+ {"path": str(self.record_path), "before": self._file_version(self.record_path),
630
+ "after": self._planned_version(_canonical(record) + b"\n"), "mode": 0o600},
631
+ {"path": str(self.state_root / "corpus-status.json"),
632
+ "before": self._file_version(self.state_root / "corpus-status.json"),
633
+ "after": self._planned_version(self._launcher_status_bytes(release, requested)), "mode": 0o600},
634
+ ])
635
+ transaction_id = str(candidate.get("transaction_id") or uuid.uuid4().hex)
636
+ journal = self._transaction_path(transaction_id)
637
+ # Source candidates are persisted by CorpusStore under the transaction
638
+ # id. Keep only hashes here: personal source text is free-form and
639
+ # must never be accidentally archived by the installer journal.
640
+ association = {"transaction_id": transaction_id, "baseline_ref": baseline_ref,
641
+ "selected_baseline_ref": details.get("selected_baseline_ref"),
642
+ "items": details.get("items"),
643
+ "source_before_sha256": hashlib.sha256(_canonical(candidate.get("before"))).hexdigest(),
644
+ "source_after_sha256": hashlib.sha256(_canonical(candidate.get("after"))).hexdigest()}
645
+ journal_data = {"schema_version": SCHEMA_VERSION, "owner": "installer", "kind": "install", "state": "PREPARED",
646
+ "phase": "preflight", "candidate": association, "paths": paths,
647
+ "created_at": int(time.time())}
648
+ self._journal_write(journal, journal_data)
649
+ try:
650
+ with operation_scope(self.state_root):
651
+ self._finish_install_transaction(journal, journal_data, store, candidate)
652
+ return {"dry_run": False, "stored": True, "activation": "unverified", "record": record}
653
+ except BaseException as exc:
654
+ journal_data["state"] = "NEEDS_RECOVERY"
655
+ journal_data["error"] = type(exc).__name__
656
+ self._journal_write(journal, journal_data)
657
+ raise
658
+
659
+ def _finish_install_transaction(self, journal: Path, data: dict[str, Any], store: Any,
660
+ candidate: dict[str, Any] | None) -> None:
661
+ self._assert_preflight_paths(data["paths"])
662
+ data["state"], data["phase"] = "APPLYING", "projections"
663
+ self._journal_write(journal, data)
664
+ # Write candidate projections before source publication. A crash here
665
+ # is harmless to readers because the journal guard is already durable.
666
+ for entry in data["paths"]:
667
+ path = Path(entry["path"])
668
+ if not self._projection_matches(entry):
669
+ self._write_planned(path, entry["after"], int(entry["mode"]))
670
+ data["phase"] = "source"
671
+ self._journal_write(journal, data)
672
+ commit = getattr(store, "commit_install", None)
673
+ if not callable(commit):
674
+ raise InstallError("private corpus store does not support staged install commit")
675
+ if candidate is None:
676
+ # The store persists the complete source candidate under this id;
677
+ # passing only it prevents installer journals from duplicating user
678
+ # authoring bytes (which could include sensitive free text).
679
+ commit({"transaction_id": data["candidate"]["transaction_id"]})
680
+ else:
681
+ commit(candidate)
682
+ data["phase"] = "verify"
683
+ self._journal_write(journal, data)
684
+ # The record/status are included in paths and now exact. Verify the
685
+ # installed source and every owned projection before success is visible.
686
+ self._verify_transaction_install(data)
687
+ data["state"], data["phase"], data["committed_at"] = "COMMITTED", "complete", int(time.time())
688
+ self._journal_write(journal, data)
689
+
690
+ def _verify_transaction_install(self, data: dict[str, Any]) -> None:
691
+ for entry in data["paths"]:
692
+ if not self._projection_matches(entry):
693
+ raise InstallError(f"transaction projection did not verify: {entry['path']}")
694
+ record = _read_json(self.record_path)
695
+ release = Path(record["package_root"])
696
+ self._verify_release(release, record["installed_files"])
697
+ self._validate_owned_record(record, release)
698
+ source = _read_json(self.runtime / "state.json")
699
+ if source.get("last_successful_install_ref") != record.get("baseline_ref"):
700
+ raise InstallError("staged corpus baseline was not published")
701
+
702
+ def _recover_pending_transactions(self) -> None:
703
+ root = self.runtime / "installer-transactions"
704
+ if not root.exists():
705
+ return
706
+ for journal in sorted(root.glob("*/journal.json")):
707
+ data = _read_json(journal)
708
+ if data.get("state") not in {"PREPARED", "APPLYING", "NEEDS_RECOVERY"}:
709
+ continue
710
+ if data.get("kind") != "install" or not isinstance(data.get("candidate"), dict) \
711
+ or not isinstance(data.get("paths"), list):
712
+ raise InstallError(f"unrecoverable transaction journal: {journal}")
713
+ association = data["candidate"]
714
+ release_record = next((entry for entry in data["paths"] if entry.get("path") == str(self.record_path)), None)
715
+ if not isinstance(release_record, dict):
716
+ raise InstallError(f"transaction journal lacks install record: {journal}")
717
+ encoded = release_record.get("after", {}).get("bytes_b64")
718
+ if not isinstance(encoded, str):
719
+ raise InstallError(f"transaction journal lacks record bytes: {journal}")
720
+ record = json.loads(base64.b64decode(encoded.encode("ascii"), validate=True))
721
+ release = Path(record.get("package_root", ""))
722
+ if not release.is_dir() or release.is_symlink():
723
+ raise InstallError(f"transaction release is unavailable: {journal}")
724
+ try:
725
+ with operation_scope(self.state_root):
726
+ self._finish_install_transaction(journal, data, self._store(release), None)
727
+ except BaseException as exc:
728
+ data["state"], data["error"] = "NEEDS_RECOVERY", type(exc).__name__
729
+ self._journal_write(journal, data)
730
+ raise InstallError(f"pending install requires recovery: {journal}") from exc
731
+
732
+ @_serialized
733
+ def verify(self) -> dict[str, Any]:
734
+ try:
735
+ guard_pending(self.state_root)
736
+ except TransactionError as exc:
737
+ raise InstallError(str(exc)) from exc
738
+ record = self._old_record()
739
+ if record is None:
740
+ raise InstallError("no private install record")
741
+ if record.get("schema_version") != SCHEMA_VERSION:
742
+ raise InstallError("private install record schema mismatch")
743
+ if record.get("needs_action"):
744
+ raise InstallError("private install is incomplete; resolve owned-path conflicts: "
745
+ + ", ".join(str(path) for path in record["needs_action"]))
746
+ release = Path(record.get("package_root", ""))
747
+ releases = self.runtime / "releases"
748
+ if not _inside(release, releases) or release.is_symlink() or not release.is_dir():
749
+ raise InstallError("private install record has unsafe package root")
750
+ entries = record.get("installed_files")
751
+ if not isinstance(entries, list) or not entries:
752
+ raise InstallError("private install record has no installed files")
753
+ self._verify_release(release, entries)
754
+ self._validate_owned_record(record, release)
755
+ catalog = self._catalog(release)
756
+ if not catalog.get("items"):
757
+ raise InstallError("private catalog is empty")
758
+ source_state = _read_json(self.runtime / "state.json")
759
+ if source_state.get("last_successful_install_ref") != record.get("baseline_ref"):
760
+ raise InstallError("private baseline does not match install record")
761
+ for entry in [record.get("launcher"), *(record.get("config_files") or [])]:
762
+ if not isinstance(entry, dict):
763
+ continue
764
+ path = Path(entry.get("path", ""))
765
+ if path.is_symlink() or not path.is_file() or _sha256(path) != entry.get("sha256"):
766
+ raise InstallError(f"private owned file drifted: {path}")
767
+ launcher_status = _read_json(self.state_root / "corpus-status.json")
768
+ domains = launcher_status.get("domains")
769
+ if launcher_status.get("repo") != str(release) or not isinstance(domains, dict) or \
770
+ not isinstance(domains.get("available"), list) or not isinstance(domains.get("applied"), list):
771
+ raise InstallError("launcher corpus-status projection is missing or malformed")
772
+ return {"stored": True, "catalog_items": len(catalog["items"]), "baseline_ref": record["baseline_ref"],
773
+ "activation": "unverified"}
774
+
775
+ @_serialized
776
+ def status(self) -> dict[str, Any]:
777
+ try:
778
+ pending = pending_status(self.state_root)
779
+ except TransactionError as exc:
780
+ raise InstallError(str(exc)) from exc
781
+ record = self._old_record()
782
+ if not record:
783
+ return {"installed": False, "activation": "unverified", **pending}
784
+ return {"installed": True, "package_root": record.get("package_root"),
785
+ "baseline_ref": record.get("baseline_ref"), "needs_action": record.get("needs_action", []),
786
+ "activation": "unverified", **pending}
787
+
788
+ def _active_intents(self) -> list[Path]:
789
+ root = self.runtime / "activations"
790
+ if not root.is_dir():
791
+ return []
792
+ active = []
793
+ for journal in root.glob("*/journal.json"):
794
+ with contextlib.suppress(InstallError):
795
+ data = _read_json(journal)
796
+ if data.get("state") in {"PREPARED", "HOST_OBSERVED"}:
797
+ active.append(journal)
798
+ return active
799
+
800
+ @_serialized
801
+ def uninstall(self, dry_run: bool = False) -> dict[str, Any]:
802
+ record = self._old_record()
803
+ if record is not None:
804
+ release = Path(record.get("package_root", ""))
805
+ if not _inside(release, self.runtime / "releases") or release.is_symlink() or not release.is_dir():
806
+ raise InstallError("private install record has unsafe package root")
807
+ self._validate_owned_record(record, release)
808
+ shell_removed: list[str] = []
809
+ try:
810
+ manager = self._shell_manager()
811
+ if manager.has_connection():
812
+ shell_removed = manager.apply("remove", dry_run)["changed_paths"]
813
+ except (OSError, RuntimeError) as exc:
814
+ raise InstallError(str(exc)) from exc
815
+ if record is None:
816
+ return {"removed": shell_removed, "preserved": ["no private install record"]}
817
+ removed: list[str] = list(shell_removed)
818
+ preserved: list[str] = []
819
+ for entry in [record.get("launcher"), *(record.get("config_files") or [])]:
820
+ if not isinstance(entry, dict):
821
+ continue
822
+ path = Path(entry.get("path", ""))
823
+ expected = entry.get("sha256")
824
+ if path.is_file() and not path.is_symlink() and isinstance(expected, str) and _sha256(path) == expected:
825
+ if not dry_run:
826
+ path.unlink()
827
+ removed.append(str(path))
828
+ elif path.exists():
829
+ preserved.append(str(path))
830
+ intents = self._active_intents()
831
+ if intents:
832
+ preserved.extend(str(path) for path in intents)
833
+ elif _inside(release, self.runtime / "releases") and release.is_dir() and not release.is_symlink():
834
+ if not dry_run:
835
+ shutil.rmtree(release)
836
+ removed.append(str(release))
837
+ else:
838
+ preserved.append(str(release))
839
+ if not intents and not dry_run:
840
+ self.record_path.unlink(missing_ok=True)
841
+ # User package/overlays/learnings and session snapshots/pins are purposely
842
+ # not enumerated or deleted here. No whole state-root removal occurs.
843
+ return {"removed": removed, "preserved": preserved,
844
+ "retained": [str(self.user_root), str(self.state_root / "sessions")]}
845
+
846
+ def _reset_layout(self, record: dict[str, Any], release: Path) -> tuple[list[Path], Path, Path, list[tuple[Path, bytes, int]]]:
847
+ local = [self.launch_root / name for name in ("presets.local.toml", "review-methods.local.toml", "launcher.local.toml")]
848
+ connections = self.launch_root.parent / "agent-bios"
849
+ cleanup = [*local, connections / "ingest-url", self.user_root / "understand" / "state.json"]
850
+ owned = [(self.bin_root / "agent-launch", self._launcher_body(release), 0o755),
851
+ (self.launch_root / "profiles.toml", (release / "launch" / "agent-launch.toml").read_bytes(), 0o644)]
852
+ owned.extend((self.launch_root / "i18n" / p.name, p.read_bytes(), 0o644)
853
+ for p in sorted((release / "launch" / "i18n").glob("*.toml")))
854
+ return cleanup, connections / "token", connections, owned
855
+
856
+ def _learning_versions(self) -> dict[str, dict[str, Any]]:
857
+ """Learning events are authoring input, not reset-owned cleanup."""
858
+ return {host: self._file_version(self.user_root / "learnings" / host / "events.jsonl")
859
+ for host in ("claude", "codex")}
860
+
861
+ def _reset_preview(self) -> dict[str, Any]:
862
+ record = self._old_record()
863
+ if record is None:
864
+ raise InstallError("cannot reset before private install")
865
+ release = Path(record.get("package_root", ""))
866
+ if not _inside(release, self.runtime / "releases") or release.is_symlink() or not release.is_dir():
867
+ raise InstallError("private install record has unsafe package root")
868
+ self._validate_owned_record(record, release)
869
+ cleanup, secret, _connections, owned = self._reset_layout(record, release)
870
+ targets = [{"path": str(path), "before": self._file_version(path), "after": {"exists": False}, "mode": 0o600,
871
+ "archive": True} for path in cleanup]
872
+ targets.extend(self._shell_paths("remove"))
873
+ targets.extend({"path": str(path), "before": self._file_version(path), "after": self._planned_version(body),
874
+ "mode": mode, "archive": False} for path, body, mode in owned)
875
+ status_path = self.state_root / "corpus-status.json"
876
+ targets.append({"path": str(status_path), "before": self._file_version(status_path),
877
+ "after": self._planned_version(self._launcher_status_bytes(
878
+ release, record["selection"], record.get("created_at") if isinstance(record.get("created_at"), int) else 0)),
879
+ "mode": 0o600, "archive": False})
880
+ secret_before = self._file_version(secret, allow_bytes=False)
881
+ pending = pending_status(self.state_root)
882
+ learning = self._learning_versions()
883
+ fingerprint = hashlib.sha256(_canonical({"record": _sha256(self.record_path), "runtime": self._file_version(self.runtime / "state.json"),
884
+ "user": self._file_version(self.user_root / "state.json"), "targets": targets,
885
+ "secret": secret_before, "learning": learning, "pending": pending["transactions"]})).hexdigest()
886
+ return {"preview": True, "expected_revision": fingerprint, "pending": pending,
887
+ "archive": [entry["path"] for entry in targets if entry["archive"] and entry["before"]["exists"]],
888
+ "delete_without_archive": [str(secret)] if secret_before["exists"] else [],
889
+ "restore_defaults": [entry["path"] for entry in targets if not entry["archive"]],
890
+ "retained": [str(self.state_root / "sessions"), str(self.user_root / "learnings")],
891
+ "record": record, "release": release, "targets": targets, "secret_before": secret_before,
892
+ "learning_before": learning}
893
+
894
+ @_serialized
895
+ def reset(self, apply: bool = False, yes: bool = False, expected_revision: str | None = None) -> dict[str, Any]:
896
+ """Preview first; an apply accepts exactly that observed generation."""
897
+ preview = self._reset_preview()
898
+ if not apply:
899
+ return {key: value for key, value in preview.items() if key not in {"record", "release", "targets", "secret_before", "learning_before"}}
900
+ if not yes or not isinstance(expected_revision, str):
901
+ raise InstallError("reset apply requires --yes and preview expected_revision")
902
+ pending = [row for row in preview["pending"]["transactions"] if row["owner"] == "installer"]
903
+ if any(row["kind"] == "install" for row in pending):
904
+ raise InstallError("pending install requires recovery before reset")
905
+ for row in pending:
906
+ data = _read_json(Path(row["path"]))
907
+ if data.get("owner") != "installer" or data.get("kind") != "reset":
908
+ raise InstallError(f"pending reset requires fresh preview: {row['path']}")
909
+ if data.get("intent", {}).get("accepted_revision") == expected_revision:
910
+ return self._recover_reset(Path(row["path"]), data)
911
+ if preview["expected_revision"] != expected_revision:
912
+ raise InstallError("reset preview is stale; preview again before applying")
913
+ record, release, targets = preview["record"], preview["release"], preview["targets"]
914
+ with operation_scope(self.state_root):
915
+ plan = self._store(release).plan({"operation": "reset"})
916
+ reset_id = f"{int(time.time())}-{uuid.uuid4().hex[:12]}"
917
+ journal = self.runtime / "resets" / reset_id / "journal.json"
918
+ archive = self.user_root / "history" / reset_id / "launcher-config"
919
+ data = {"schema_version": SCHEMA_VERSION, "owner": "installer", "kind": "reset", "state": "PREPARED", "phase": "preflight",
920
+ "archive": str(archive), "targets": targets, "secret_before": preview["secret_before"], "learning_before": preview["learning_before"],
921
+ "intent": {"plan_id": plan["plan_id"], "expected_revision": plan["expected_revision"],
922
+ "accepted_revision": expected_revision, "record_sha256": _sha256(self.record_path)},
923
+ "retained": preview["retained"], "created_at": int(time.time())}
924
+ _atomic_json(journal, data)
925
+ # Keep the old publication guard until the replacement intent is durable.
926
+ for row in pending:
927
+ prior = _read_json(Path(row["path"]))
928
+ prior["state"] = "SUPERSEDED"
929
+ _atomic_json(Path(row["path"]), prior)
930
+ return self._recover_reset(journal, data)
931
+
932
+ def _source_pair_ok(self, data: dict[str, Any]) -> bool:
933
+ plan_path = self.runtime / "transactions" / data["intent"]["plan_id"] / "journal.json"
934
+ plan = _read_json(plan_path).get("plan", {})
935
+ before, after = plan.get("before", {}), plan.get("after", {})
936
+ runtime, user = _read_json(self.runtime / "state.json"), _read_json(self.user_root / "state.json")
937
+ return runtime in (before.get("runtime"), after.get("runtime")) and user in (before.get("user"), after.get("user"))
938
+
939
+ def _recover_reset(self, journal: Path, data: dict[str, Any]) -> dict[str, Any]:
940
+ """Preflight every target and source pair before changing one byte."""
941
+ try:
942
+ if data["intent"].get("record_sha256") != _sha256(self.record_path) or not self._source_pair_ok(data):
943
+ raise InstallError("reset state changed; obtain a fresh preview")
944
+ if data.get("learning_before") != self._learning_versions():
945
+ raise InstallError("reset learning source changed; obtain a fresh preview")
946
+ targets = data.get("targets")
947
+ if not isinstance(targets, list):
948
+ raise InstallError("pending reset requires fresh preview")
949
+ for entry in targets:
950
+ if not (self._matches_version(Path(entry["path"]), entry["before"]) or self._matches_version(Path(entry["path"]), entry["after"])):
951
+ raise InstallError(f"reset target changed; obtain a fresh preview: {entry['path']}")
952
+ secret = self.launch_root.parent / "agent-bios" / "token"; prior = data.get("secret_before", {"exists": False})
953
+ if not (self._matches_version(secret, prior) or not secret.exists()):
954
+ raise InstallError("reset token changed; obtain a fresh preview")
955
+ release = Path(self._old_record()["package_root"])
956
+ archive = Path(data["archive"])
957
+ data.update({"state": "APPLYING", "phase": "archive"}); _atomic_json(journal, data)
958
+ for entry in targets:
959
+ path = Path(entry["path"])
960
+ if entry.get("archive") and entry["before"].get("exists"):
961
+ target = archive / path.name
962
+ if not target.exists(): self._write_planned(target, entry["before"], 0o600)
963
+ if entry.get("archive") and not self._matches_version(path, entry["after"]): self._write_planned(path, entry["after"], int(entry["mode"]))
964
+ data["phase"] = "source"; _atomic_json(journal, data)
965
+ with operation_scope(self.state_root):
966
+ result = self._store(release).apply(data["intent"]["plan_id"], data["intent"]["expected_revision"])
967
+ data["result"] = result; data["phase"] = "projections"; _atomic_json(journal, data)
968
+ for entry in targets:
969
+ if not entry.get("archive") and not self._projection_matches(entry): self._write_planned(Path(entry["path"]), entry["after"], int(entry["mode"]))
970
+ if prior.get("exists"):
971
+ if self._matches_version(secret, prior):
972
+ secret.unlink()
973
+ elif secret.exists():
974
+ raise InstallError("reset token changed; obtain a fresh preview")
975
+ data.update({"state": "COMMITTED", "phase": "complete", "committed_at": int(time.time())}); _atomic_json(journal, data)
976
+ return {"preview": False, "reset": data["result"], "archive": data["archive"], "retained": data["retained"], "recovered": True}
977
+ except BaseException as exc:
978
+ data.update({"state": "NEEDS_RECOVERY", "error": type(exc).__name__}); _atomic_json(journal, data); raise
979
+
980
+ def _legacy_manifest_files(self, needs_action: list[str], read=None) -> list[Path]:
981
+ manifest = self.state_root / "manifest.txt"
982
+ if manifest.is_symlink():
983
+ needs_action.append(f"unsafe legacy manifest symlink: {manifest}")
984
+ return []
985
+ content = read(manifest) if read else manifest.read_bytes() if manifest.is_file() else None
986
+ if content is None:
987
+ return []
988
+ private = self._old_record()
989
+ protected: set[str] = set()
990
+ if private is not None:
991
+ self._validate_owned_record(private, Path(private.get("package_root", "")))
992
+ protected = {str(entry["path"]) for entry in
993
+ [private["launcher"], *private.get("config_files", [])]}
994
+ owned = self._legacy_manifest_destinations()
995
+ paths: list[Path] = []
996
+ body = content.decode("utf-8")
997
+ for raw in body.splitlines():
998
+ if not raw.strip():
999
+ continue
1000
+ path = Path(raw.strip())
1001
+ if str(path) in protected:
1002
+ continue
1003
+ if not path.is_absolute() or path in {self.claude_root / "CLAUDE.md", self.codex_root / "AGENTS.md"}:
1004
+ needs_action.append(f"unowned legacy manifest target: {path}")
1005
+ continue
1006
+ if path.is_symlink():
1007
+ needs_action.append(f"unsafe legacy manifest target: {path}")
1008
+ continue
1009
+ try:
1010
+ self._migration_target_root(path)
1011
+ except InstallError as exc:
1012
+ needs_action.append(str(exc))
1013
+ continue
1014
+ if path not in owned:
1015
+ # A prior installer wrote broad directory walks into its manifest.
1016
+ # Root containment proves only where a file lives, never who wrote it.
1017
+ # A stale absent entry is harmless; an existing unknown file is not.
1018
+ if path.exists():
1019
+ needs_action.append(f"unowned legacy manifest target: {path}")
1020
+ continue
1021
+ if path.exists() and not path.is_file():
1022
+ needs_action.append(f"legacy manifest target is not a regular file: {path}")
1023
+ continue
1024
+ paths.append(path)
1025
+ return paths
1026
+
1027
+ def _legacy_manifest_destinations(self, package_root: Path | None = None) -> set[Path]:
1028
+ """Return only paths a legacy installer could derive from this source.
1029
+
1030
+ The old manifest included a recursive scan of ``central``. This map is
1031
+ deliberately source-derived instead of treating a shared native root as
1032
+ a claim of ownership, so a same-directory user file remains protected.
1033
+ """
1034
+ source_root = (package_root or self.repo).resolve()
1035
+ owned: set[Path] = {
1036
+ self.state_root / "version.json",
1037
+ self.claude_root / "central" / "bundle.md",
1038
+ self.claude_root / "personal" / "learnings.jsonl",
1039
+ self.codex_root / "personal" / "learnings.jsonl",
1040
+ }
1041
+
1042
+ def source_file(relative: str) -> Path | None:
1043
+ path = source_root / relative
1044
+ return path if path.is_file() and not path.is_symlink() else None
1045
+
1046
+ native = (
1047
+ ("wrappers/codex-run.sh", self.codex_root / "bin" / "codex-run"),
1048
+ ("wrappers/codex-helm.sh", self.codex_root / "bin" / "codex-helm"),
1049
+ ("wrappers/claude-run.sh", self.claude_root / "bin" / "claude-run"),
1050
+ ("launch/agent-launch.py", self.bin_root / "agent-launch"),
1051
+ ("launch/agent-launch.toml", self.launch_root / "profiles.toml"),
1052
+ ("launch/agent-launch.zsh", self.launch_root / "shell.zsh"),
1053
+ )
1054
+ owned.update(destination for source, destination in native if source_file(source) is not None)
1055
+ i18n = source_root / "launch" / "i18n"
1056
+ if i18n.is_dir() and not i18n.is_symlink():
1057
+ owned.update(self.launch_root / "i18n" / source.name
1058
+ for source in i18n.glob("*.toml") if source.is_file() and not source.is_symlink())
1059
+
1060
+ catalog = self._catalog(source_root)
1061
+ for item in catalog.get("items", []):
1062
+ if not isinstance(item, dict):
1063
+ continue
1064
+ kind = item.get("kind")
1065
+ origin = item.get("origin")
1066
+ source = origin.get("source_path") if isinstance(origin, dict) else None
1067
+ members = item.get("members")
1068
+ if not isinstance(source, str) or not isinstance(members, dict):
1069
+ continue
1070
+ for member in members:
1071
+ if not isinstance(member, str):
1072
+ continue
1073
+ relative = PurePosixPath(member)
1074
+ if relative.is_absolute() or ".." in relative.parts or len(relative.parts) < 2:
1075
+ continue
1076
+ tail = Path(*relative.parts[1:])
1077
+ if kind == "guide" and source.startswith("claude/guides/") and relative.parts[0] == "guides":
1078
+ owned.update({self.claude_root / "guides" / tail,
1079
+ self.claude_root / "central" / "guides" / tail,
1080
+ self.codex_root / "guides" / tail})
1081
+ elif kind == "hook" and source.startswith("claude/hooks/") and relative.parts[0] == "hooks":
1082
+ owned.add(self.claude_root / "central" / "hooks" / tail)
1083
+ elif kind == "agent" and source.startswith("claude/agents/") and relative.parts[0] == "agents":
1084
+ owned.add(self.claude_root / "central" / "agents" / tail)
1085
+ elif kind == "skill" and source.startswith("claude/skills/") and relative.parts[0] == "skills":
1086
+ owned.update({self.claude_root / "skills" / tail,
1087
+ self.codex_root / "skills" / tail})
1088
+
1089
+ agents = source_root / "codex" / "agents"
1090
+ if agents.is_dir() and not agents.is_symlink():
1091
+ owned.update(self.codex_root / "agents" / source.name
1092
+ for source in agents.glob("*.toml") if source.is_file() and not source.is_symlink())
1093
+ return owned
1094
+
1095
+ def _migration_cleanup_destinations(self, release: Path) -> set[Path]:
1096
+ """The pinned release plus bounded native edits define replay authority."""
1097
+ return self._legacy_manifest_destinations(release) | {
1098
+ self.state_root / "manifest.txt",
1099
+ self.claude_root / "CLAUDE.md",
1100
+ self.claude_root / "personal" / "learnings.md",
1101
+ self.claude_root / "settings.json",
1102
+ self.codex_root / "AGENTS.md",
1103
+ self.codex_root / "config.toml",
1104
+ self.zdotdir / ".zshrc",
1105
+ self.zdotdir / ".zshenv",
1106
+ self.zdotdir / ".zprofile",
1107
+ }
1108
+
1109
+ def _has_legacy_learning_projection(self, host: str, prose: Path, read=None) -> bool:
1110
+ if prose.is_symlink():
1111
+ return True
1112
+ body = read(prose) if read else prose.read_bytes() if prose.is_file() else None
1113
+ if body is None:
1114
+ return False
1115
+ if host == "codex":
1116
+ return PERSONAL_START.encode() in body or PERSONAL_END.encode() in body
1117
+ # The published legacy installer seeds this exact empty automation-owned file.
1118
+ assembler = self._private_module(self.repo, "assemble")
1119
+ return body != assembler.PERSONAL_LEARNINGS_HEADER.encode("utf-8")
1120
+
1121
+ def _legacy_events(self, host: str, needs_action: list[str], read=None) -> tuple[list[dict[str, str]], Path | None]:
1122
+ home = self.claude_root if host == "claude" else self.codex_root
1123
+ source = home / "personal" / "learnings.jsonl"
1124
+ prose = home / "personal" / "learnings.md" if host == "claude" else home / "AGENTS.md"
1125
+ projection = self._has_legacy_learning_projection(host, prose, read)
1126
+ if source.is_symlink():
1127
+ needs_action.append(f"legacy {host} learning JSONL is symlinked: {source}")
1128
+ return [], None
1129
+ content = read(source) if read else source.read_bytes() if source.is_file() else None
1130
+ if projection and content is None:
1131
+ needs_action.append(f"legacy {host} learning projection has no immutable JSONL source: {prose}")
1132
+ return [], None
1133
+ if content is None:
1134
+ return [], None
1135
+ schema = _read_json(self.repo / "learn" / "learning.schema.json")
1136
+ allowed = set((schema.get("properties") or {}).keys())
1137
+ required = set(schema.get("required") or [])
1138
+ events: list[dict[str, str]] = []
1139
+ seen: dict[str, str] = {}
1140
+ body = content.decode("utf-8")
1141
+ for number, line in enumerate(body.splitlines(), 1):
1142
+ if not line.strip():
1143
+ continue
1144
+ try:
1145
+ raw = json.loads(line)
1146
+ except ValueError:
1147
+ needs_action.append(f"invalid legacy {host} learning JSONL at {source}:{number}")
1148
+ continue
1149
+ if (not isinstance(raw, dict) or set(raw) - allowed or required - set(raw)
1150
+ or raw.get("schema_version") != 1 or not isinstance(raw.get("learning_id"), str)
1151
+ or not LEARNING_ID.fullmatch(raw["learning_id"])
1152
+ or not isinstance(raw.get("lesson"), str) or not isinstance(raw.get("domain"), str)
1153
+ or not isinstance(raw.get("created"), str) or not isinstance(raw.get("supporting_sessions"), list)
1154
+ or not all(isinstance(value, str) for value in raw["supporting_sessions"])):
1155
+ needs_action.append(f"invalid collector v1 {host} learning record at {source}:{number}")
1156
+ continue
1157
+ prior = seen.get(raw["learning_id"])
1158
+ if prior is not None:
1159
+ if prior != line:
1160
+ needs_action.append(f"same learning_id has different legacy bytes at {source}:{number}")
1161
+ continue
1162
+ seen[raw["learning_id"]] = line
1163
+ events.append({"learning_id": raw["learning_id"], "raw": line})
1164
+ return events, source
1165
+
1166
+ def _event_destination_conflicts(self, host: str, events: list[dict[str, str]], needs_action: list[str]) -> None:
1167
+ target = self.user_root / "learnings" / host / "events.jsonl"
1168
+ if not target.exists():
1169
+ return
1170
+ if target.is_symlink():
1171
+ needs_action.append(f"unsafe private {host} learning event symlink: {target}")
1172
+ return
1173
+ existing: dict[str, str] = {}
1174
+ for number, line in enumerate(target.read_text(encoding="utf-8").splitlines(), 1):
1175
+ if not line.strip():
1176
+ continue
1177
+ try:
1178
+ record = json.loads(line)
1179
+ except ValueError:
1180
+ needs_action.append(f"invalid private {host} learning JSONL at {target}:{number}")
1181
+ continue
1182
+ learning_id = record.get("learning_id") if isinstance(record, dict) else None
1183
+ if not isinstance(learning_id, str):
1184
+ needs_action.append(f"invalid private {host} learning record at {target}:{number}")
1185
+ continue
1186
+ previous = existing.get(learning_id)
1187
+ if previous is not None and previous != line:
1188
+ needs_action.append(f"same learning_id has different private bytes at {target}:{number}")
1189
+ existing[learning_id] = line
1190
+ for event in events:
1191
+ current = existing.get(event["learning_id"])
1192
+ if current is not None and current != event["raw"]:
1193
+ needs_action.append(f"same learning_id has different source/private bytes: {event['learning_id']}")
1194
+
1195
+ def _legacy_hook_registration_needed(self, needs_action: list[str], read=None) -> bool:
1196
+ """Use the assembler's exact command-ownership rule, after parse proof."""
1197
+ settings = self.claude_root / "settings.json"
1198
+ if settings.is_symlink():
1199
+ needs_action.append(f"unsafe Claude settings symlink: {settings}")
1200
+ return False
1201
+ content = read(settings) if read else settings.read_bytes() if settings.is_file() else None
1202
+ if content is None:
1203
+ return False
1204
+ try:
1205
+ data = json.loads(content.decode("utf-8"))
1206
+ except ValueError:
1207
+ needs_action.append(f"unreadable Claude settings for legacy hook removal: {settings}")
1208
+ return False
1209
+ hooks = data.get("hooks") if isinstance(data, dict) else None
1210
+ if not isinstance(hooks, dict):
1211
+ return False
1212
+ names = set((_read_json(self.repo / "compose" / "domains.json").get("hooks") or {}).keys())
1213
+ for entries in hooks.values():
1214
+ if not isinstance(entries, list):
1215
+ continue
1216
+ for entry in entries:
1217
+ for hook in entry.get("hooks", []) if isinstance(entry, dict) else []:
1218
+ command = hook.get("command", "") if isinstance(hook, dict) else ""
1219
+ if isinstance(command, str) and any(f"/hooks/{name}" in command for name in names):
1220
+ return True
1221
+ return False
1222
+
1223
+ def _legacy_hook_settings(self, release: Path, before: bytes) -> bytes:
1224
+ """Plan the assembler's exact ownership-sensitive edit without touching the host."""
1225
+ module = self._private_module(release, "assemble")
1226
+ manifest = _read_json(release / "compose" / "domains.json")
1227
+ with tempfile.TemporaryDirectory(prefix="corpus-hook-plan-") as raw:
1228
+ root = Path(raw)
1229
+ _atomic_bytes(root / "settings.json", before, 0o600)
1230
+ module.merge_settings(root, [], release / "claude" / "settings.template.json",
1231
+ owned_names=manifest.get("hooks", {}))
1232
+ return (root / "settings.json").read_bytes()
1233
+
1234
+ def migration_plan(self) -> dict[str, Any]:
1235
+ return self._plan_migration()[0]
1236
+
1237
+ def _plan_migration(self) -> tuple[dict[str, Any], dict[str, dict[str, Any]]]:
1238
+ # A transformation and its version predicate must describe the same read.
1239
+ inputs: dict[str, dict[str, Any]] = {}
1240
+
1241
+ def read(path: Path) -> bytes | None:
1242
+ raw = str(path)
1243
+ if raw not in inputs:
1244
+ self._migration_target_root(path)
1245
+ inputs[raw] = self._file_version(path)
1246
+ if not inputs[raw]["exists"]:
1247
+ return None
1248
+ return base64.b64decode(inputs[raw]["bytes_b64"].encode("ascii"), validate=True)
1249
+
1250
+ needs_action: list[str] = []
1251
+
1252
+ def text(path: Path) -> str | None:
1253
+ if path.is_symlink():
1254
+ needs_action.append(f"unsafe native migration input symlink: {path}")
1255
+ return None
1256
+ content = read(path)
1257
+ return content.decode("utf-8") if content is not None else None
1258
+
1259
+ writes: list[dict[str, str]] = []
1260
+ claude_entry = self.claude_root / "CLAUDE.md"
1261
+ body = text(claude_entry)
1262
+ if body is not None:
1263
+ new, removed = _remove_claude_imports(body)
1264
+ if removed:
1265
+ writes.append({"path": str(claude_entry), "content": new, "kind": "claude-imports"})
1266
+ codex_entry = self.codex_root / "AGENTS.md"
1267
+ body = text(codex_entry)
1268
+ if body is not None:
1269
+ new, outcome = _remove_spans(body)
1270
+ if new is None:
1271
+ needs_action.extend(f"{codex_entry}: {message}" for message in outcome)
1272
+ elif outcome:
1273
+ writes.append({"path": str(codex_entry), "content": new, "kind": "codex-markers"})
1274
+ codex_config = self.codex_root / "config.toml"
1275
+ body = text(codex_config)
1276
+ if body is not None:
1277
+ stripped, changed = _strip_codex_config_additions(body)
1278
+ if stripped is None:
1279
+ needs_action.append(f"cannot safely remove legacy Codex config additions: {codex_config}")
1280
+ elif changed:
1281
+ writes.append({"path": str(codex_config), "content": stripped, "kind": "codex-config-additions"})
1282
+ for startup in (self.zdotdir / ".zshrc", self.zdotdir / ".zshenv", self.zdotdir / ".zprofile"):
1283
+ body = text(startup)
1284
+ if body is not None:
1285
+ lines = body.splitlines(keepends=True)
1286
+ changed = [line for line in lines if line.strip() == ZSH_HOOK]
1287
+ if changed:
1288
+ writes.append({"path": str(startup), "content": "".join(line for line in lines if line.strip() != ZSH_HOOK), "kind": "zsh-hook"})
1289
+ events: dict[str, list[dict[str, str]]] = {}
1290
+ learning_sources: dict[str, Path] = {}
1291
+ for host in ("claude", "codex"):
1292
+ captured, source = self._legacy_events(host, needs_action, read)
1293
+ if captured:
1294
+ events[host] = captured
1295
+ self._event_destination_conflicts(host, captured, needs_action)
1296
+ if source is not None:
1297
+ learning_sources[host] = source
1298
+ deletes = self._legacy_manifest_files(needs_action, read)
1299
+ manifest = self.state_root / "manifest.txt"
1300
+ if not manifest.is_symlink() and read(manifest) is not None:
1301
+ deletes.append(manifest)
1302
+ seed = self.claude_root / "personal" / "learnings.md"
1303
+ if not seed.is_symlink() and read(seed) is not None and not self._has_legacy_learning_projection("claude", seed, read):
1304
+ deletes.append(seed)
1305
+ # Legacy learning projections are removed only when their immutable source
1306
+ # was successfully parsed and is scheduled for the private event store.
1307
+ for host, source in learning_sources.items():
1308
+ deletes.append(Path(source))
1309
+ if host == "claude":
1310
+ projection = self.claude_root / "personal" / "learnings.md"
1311
+ if not projection.is_symlink() and read(projection) is not None:
1312
+ deletes.append(projection)
1313
+ remove_hooks = self._legacy_hook_registration_needed(needs_action, read)
1314
+ if not needs_action:
1315
+ for path in deletes:
1316
+ read(path)
1317
+ plan = {"schema_version": SCHEMA_VERSION, "writes": writes, "deletes": sorted({str(p) for p in deletes}),
1318
+ "events": events, "remove_hook_registrations": remove_hooks, "needs_action": needs_action}
1319
+ return plan, inputs
1320
+
1321
+ def _backup(self, root: Path, path: Path) -> Path:
1322
+ target = root / "files" / str(path).lstrip("/")
1323
+ _atomic_bytes(target, path.read_bytes(), 0o600)
1324
+ return target
1325
+
1326
+ def _migration_target_root(self, path: Path) -> Path:
1327
+ if not path.is_absolute() or ".." in path.parts:
1328
+ raise InstallError(f"unsafe migration target: {path}")
1329
+ roots = (self.claude_root, self.codex_root, self.launch_root, self.bin_root)
1330
+ if path in {self.state_root / name for name in ("manifest.txt", "version.json")}:
1331
+ root = self.state_root
1332
+ elif path in {self.zdotdir / name for name in (".zshrc", ".zshenv", ".zprofile")}:
1333
+ root = self.zdotdir
1334
+ else:
1335
+ root = next((r for r in roots if path != r and _inside(path, r)), None)
1336
+ if root is None:
1337
+ raise InstallError(f"migration target is outside owned roots: {path}")
1338
+ self._safe_owned_path(path, root)
1339
+ return root
1340
+
1341
+ def _migration_paths(self, plan: dict[str, Any], release: Path,
1342
+ inputs: dict[str, dict[str, Any]]) -> list[dict[str, Any]]:
1343
+ changes: dict[str, bytes | None] = {raw: None for raw in plan["deletes"]}
1344
+ for item in plan["writes"]:
1345
+ if item["path"] in changes:
1346
+ raise InstallError(f"migration both rewrites and deletes {item['path']}")
1347
+ changes[item["path"]] = item["content"].encode("utf-8")
1348
+ if plan["remove_hook_registrations"]:
1349
+ path = str(self.claude_root / "settings.json")
1350
+ if path in changes:
1351
+ raise InstallError("legacy manifest conflicts with managed hook settings")
1352
+ before = base64.b64decode(inputs[path]["bytes_b64"].encode("ascii"), validate=True)
1353
+ changes[path] = self._legacy_hook_settings(release, before)
1354
+ result = []
1355
+ for raw, content in sorted(changes.items()):
1356
+ path = Path(raw)
1357
+ self._migration_target_root(path)
1358
+ before = {key: value for key, value in inputs[raw].items() if key != "bytes_b64"}
1359
+ result.append({"path": raw, "before": before,
1360
+ "after": self._planned_version(content),
1361
+ "mode": (path.stat().st_mode & 0o777) if path.exists() else 0o600})
1362
+ return result
1363
+
1364
+ def _validate_migration(self, journal: Path, data: dict[str, Any]) -> Path:
1365
+ self._safe_owned_path(journal, self.runtime / "migrations")
1366
+ phases = {"preflight", "cleanup", "install", "verify", "complete"}
1367
+ if data.get("kind") != "migrate" or data.get("phase") not in phases or \
1368
+ not isinstance(data.get("paths"), list) or not isinstance(data.get("plan"), dict) or \
1369
+ not isinstance(data.get("inputs"), dict):
1370
+ raise InstallError(f"legacy migration journal has no safe replay plan; recover its backup first: {journal}")
1371
+ release_record = data.get("release")
1372
+ if not isinstance(release_record, dict):
1373
+ raise InstallError(f"migration journal lacks its pinned release: {journal}")
1374
+ try:
1375
+ release = _valid_release(self.state_root, release_record)
1376
+ except TransactionError as exc:
1377
+ raise InstallError(f"invalid migration release: {journal}: {exc}") from exc
1378
+ self._safe_owned_path(release, self.runtime / "releases")
1379
+ if data.get("backup_root") != str(journal.parent / "backup"):
1380
+ raise InstallError(f"unsafe migration backup root: {journal}")
1381
+ self._safe_owned_path(Path(data["backup_root"]), journal.parent)
1382
+ cleanup_destinations = self._migration_cleanup_destinations(release)
1383
+ seen = set()
1384
+ path_names = {entry.get("path") for entry in data["paths"] if isinstance(entry, dict)}
1385
+ for raw, version in data["inputs"].items():
1386
+ self._migration_target_root(Path(raw))
1387
+ if not isinstance(version, dict) or not isinstance(version.get("exists"), bool):
1388
+ raise InstallError(f"invalid migration input version: {journal}")
1389
+ encoded = version.get("bytes_b64")
1390
+ if encoded is not None:
1391
+ if raw in path_names:
1392
+ raise InstallError(f"migration target input retains duplicate replay bytes: {journal}")
1393
+ if not version["exists"] or not isinstance(encoded, str):
1394
+ raise InstallError(f"invalid retained migration input bytes: {journal}")
1395
+ try:
1396
+ body = base64.b64decode(encoded.encode("ascii"), validate=True)
1397
+ except (ValueError, AttributeError) as exc:
1398
+ raise InstallError(f"invalid retained migration input bytes: {journal}") from exc
1399
+ if hashlib.sha256(body).hexdigest() != version.get("sha256"):
1400
+ raise InstallError(f"retained migration input digest mismatch: {journal}")
1401
+ for entry in data["paths"]:
1402
+ if not isinstance(entry, dict) or not isinstance(entry.get("path"), str) or entry["path"] in seen:
1403
+ raise InstallError(f"invalid migration path entries: {journal}")
1404
+ seen.add(entry["path"])
1405
+ if data["inputs"].get(entry["path"]) != entry.get("before"):
1406
+ raise InstallError(f"migration input and target versions disagree: {journal}")
1407
+ path = Path(entry["path"])
1408
+ self._migration_target_root(path)
1409
+ if path not in cleanup_destinations:
1410
+ raise InstallError(f"migration target is not a pinned legacy cleanup destination: {path}")
1411
+ if not all(isinstance(entry.get(k), dict) for k in ("before", "after")):
1412
+ raise InstallError(f"migration path lacks exact state: {journal}")
1413
+ for key in ("before", "after"):
1414
+ version = entry[key]
1415
+ if not isinstance(version.get("exists"), bool):
1416
+ raise InstallError(f"invalid migration file state: {journal}")
1417
+ if version["exists"]:
1418
+ if not re.fullmatch(r"[0-9a-f]{64}", str(version.get("sha256", ""))):
1419
+ raise InstallError(f"invalid migration file digest: {journal}")
1420
+ if key == "after":
1421
+ try:
1422
+ body = base64.b64decode(version["bytes_b64"].encode("ascii"), validate=True)
1423
+ except (KeyError, TypeError, ValueError, AttributeError) as exc:
1424
+ raise InstallError(f"invalid migration replay bytes: {journal}") from exc
1425
+ if hashlib.sha256(body).hexdigest() != version["sha256"]:
1426
+ raise InstallError(f"migration replay digest mismatch: {journal}")
1427
+ elif set(version) != {"exists"}:
1428
+ raise InstallError(f"absent migration file carries replay data: {journal}")
1429
+ return release
1430
+
1431
+ def _verify_transferred_events(self, plan: dict[str, Any]) -> None:
1432
+ """Keep every planned legacy event byte-for-byte present until commit."""
1433
+ for host, events in plan.get("events", {}).items():
1434
+ target = self.user_root / "learnings" / host / "events.jsonl"
1435
+ self._safe_owned_path(target, self.user_root)
1436
+ if target.is_symlink() or not target.is_file():
1437
+ raise InstallError(f"legacy learning transfer changed: {target}")
1438
+ conflicts: list[str] = []
1439
+ self._event_destination_conflicts(host, events, conflicts)
1440
+ if conflicts:
1441
+ raise InstallError("legacy learning transfer changed: " + "; ".join(conflicts))
1442
+ lines = set(target.read_text(encoding="utf-8").splitlines())
1443
+ missing = [event["learning_id"] for event in events if event["raw"] not in lines]
1444
+ if missing:
1445
+ raise InstallError(f"legacy learning transfer changed: {target}: " + ", ".join(missing))
1446
+
1447
+ def _verify_migration_native_state(self, release: Path, data: dict[str, Any], *, child_required: bool) -> None:
1448
+ """Check cleanup outputs and untouched native inputs before a resume commits.
1449
+
1450
+ The private child owns only its recorded launcher/config projections.
1451
+ Those exact paths may move from the migration's retired state to the
1452
+ child state; every other native path must remain at the journaled state.
1453
+ """
1454
+ child = CorpusInstaller(release, self.env)
1455
+ child_owned = set(child._expected_owned(release))
1456
+ targets = {entry["path"]: entry for entry in data["paths"]}
1457
+ changed_inputs = [raw for raw, before in data["inputs"].items()
1458
+ if raw not in targets and not self._matches_version(Path(raw), before)]
1459
+ if changed_inputs:
1460
+ raise InstallError("migration input changed after cleanup: " + ", ".join(changed_inputs))
1461
+
1462
+ child_needs_verification = False
1463
+ for raw, entry in targets.items():
1464
+ path = Path(raw)
1465
+ if raw in child_owned:
1466
+ if not self._matches_version(path, entry["after"]):
1467
+ child_needs_verification = True
1468
+ continue
1469
+ if not self._matches_version(path, entry["after"]):
1470
+ raise InstallError(f"migration target changed after cleanup: {path}")
1471
+
1472
+ if not child_required and not child_needs_verification:
1473
+ return
1474
+ child_pending = any(row["kind"] == "install" for row in pending_status(self.state_root)["transactions"])
1475
+ if child_pending:
1476
+ if child_required:
1477
+ raise InstallError("pending private install prevents migration verification")
1478
+ # ``install`` below owns replaying this child journal. Its preflight
1479
+ # validates its exact projections before it writes any of them.
1480
+ return
1481
+ record = child._old_record()
1482
+ if record is None or record.get("package_root") != str(release):
1483
+ raise InstallError("migration private install is missing its pinned release")
1484
+ child.verify()
1485
+
1486
+ def _finish_migration(self, journal: Path, data: dict[str, Any]) -> dict[str, Any]:
1487
+ release = self._validate_migration(journal, data)
1488
+ plan, paths = data["plan"], data["paths"]
1489
+ try:
1490
+ with operation_scope(self.state_root):
1491
+ if data["phase"] in {"preflight", "cleanup"}:
1492
+ self._assert_preflight_paths(paths)
1493
+ targets = {entry["path"] for entry in paths}
1494
+ for raw, before in data["inputs"].items():
1495
+ if raw not in targets and not self._matches_version(Path(raw), before):
1496
+ raise InstallError(f"migration input changed: {raw}")
1497
+ for entry in paths:
1498
+ path = Path(entry["path"])
1499
+ if not entry["before"].get("exists"):
1500
+ continue
1501
+ backup = Path(data["backup_root"]) / "files" / str(path).lstrip("/")
1502
+ self._safe_owned_path(backup, journal.parent)
1503
+ if not self._matches_version(backup, entry["before"]):
1504
+ if not self._matches_version(path, entry["before"]):
1505
+ raise InstallError(f"original migration backup is missing or changed: {path}")
1506
+ self._backup(Path(data["backup_root"]), path)
1507
+ for host, events in plan["events"].items():
1508
+ conflicts: list[str] = []
1509
+ self._event_destination_conflicts(host, events, conflicts)
1510
+ if conflicts:
1511
+ raise InstallError("; ".join(conflicts))
1512
+ target = self.user_root / "learnings" / host / "events.jsonl"
1513
+ self._safe_owned_path(target, self.user_root)
1514
+ prior = target.read_text(encoding="utf-8").splitlines() if target.is_file() else []
1515
+ known = {json.loads(line)["learning_id"] for line in prior if line.strip()}
1516
+ additions = [event["raw"] for event in events if event["learning_id"] not in known]
1517
+ if additions:
1518
+ _atomic_bytes(target, ("\n".join(prior + additions) + "\n").encode("utf-8"), 0o600)
1519
+ self._verify_transferred_events(plan)
1520
+ data["state"], data["phase"] = "APPLYING", "cleanup"
1521
+ self._journal_write(journal, data)
1522
+ for entry in paths:
1523
+ path = Path(entry["path"])
1524
+ if not self._matches_version(path, entry["after"]):
1525
+ if not self._matches_version(path, entry["before"]):
1526
+ raise InstallError(f"migration target changed: {path}")
1527
+ self._write_planned(path, entry["after"], int(entry["mode"]))
1528
+ # This durable boundary prevents any retry from deleting a new projection.
1529
+ data["phase"] = "install"
1530
+ self._journal_write(journal, data)
1531
+ if data["phase"] == "install":
1532
+ self._verify_migration_native_state(release, data, child_required=False)
1533
+ self._verify_transferred_events(plan)
1534
+ installer = CorpusInstaller(release, self.env)
1535
+ current = installer._old_record()
1536
+ child_pending = any(row["kind"] == "install" for row in
1537
+ pending_status(self.state_root)["transactions"])
1538
+ if current is not None and current.get("package_root") == str(release) and not child_pending:
1539
+ # A lost parent receipt must not publish a second installation.
1540
+ installer.verify()
1541
+ else:
1542
+ installer.install()
1543
+ data["phase"] = "verify"
1544
+ self._journal_write(journal, data)
1545
+ self._verify_migration_native_state(release, data, child_required=True)
1546
+ self._verify_transferred_events(plan)
1547
+ self.verify()
1548
+ # The last pre-commit observation is deliberately repeated: a
1549
+ # cooperative old collector or native writer can race a resume.
1550
+ # It cannot make a noncooperating producer atomic after this check.
1551
+ self._verify_migration_native_state(release, data, child_required=True)
1552
+ self._verify_transferred_events(plan)
1553
+ data.update(state="COMMITTED", phase="complete", committed_at=int(time.time()))
1554
+ self._journal_write(journal, data)
1555
+ return {"preview": False, "migration_id": journal.parent.name,
1556
+ "backup_root": data["backup_root"], "package_root": str(release), **plan}
1557
+ except BaseException as exc:
1558
+ data.update(state="NEEDS_RECOVERY", error=type(exc).__name__)
1559
+ self._journal_write(journal, data)
1560
+ raise
1561
+
1562
+ @_serialized
1563
+ def migrate(self, apply: bool = False, yes: bool = False) -> dict[str, Any]:
1564
+ pending = pending_status(self.state_root)["transactions"]
1565
+ migrations = [item for item in pending if item["kind"] == "migrate"]
1566
+ if len(migrations) > 1:
1567
+ raise InstallError("multiple pending migration journals require explicit recovery")
1568
+ if migrations:
1569
+ journal = Path(migrations[0]["path"])
1570
+ data = _read_json(journal)
1571
+ self._validate_migration(journal, data)
1572
+ if not apply:
1573
+ return {"preview": True, **data["plan"], "recovery_journal": str(journal), "phase": data["phase"]}
1574
+ if not yes:
1575
+ raise InstallError("migration apply requires --yes")
1576
+ return self._finish_migration(journal, data)
1577
+ if apply:
1578
+ try:
1579
+ guard_pending(self.state_root)
1580
+ except TransactionError as exc:
1581
+ raise InstallError(str(exc)) from exc
1582
+ plan, inputs = self._plan_migration()
1583
+ if not apply:
1584
+ return {"preview": True, **plan}
1585
+ if not yes:
1586
+ raise InstallError("migration apply requires --yes")
1587
+ if plan["needs_action"]:
1588
+ raise InstallError("legacy migration has unresolved ownership: " + "; ".join(plan["needs_action"]))
1589
+ release, entries, digest = self._copy_release(self._package_files(), False)
1590
+ paths = self._migration_paths(plan, release, inputs)
1591
+ changed = [raw for raw, before in inputs.items() if not self._matches_version(Path(raw), before)]
1592
+ if changed:
1593
+ raise InstallError("migration input changed during planning; retry with a fresh plan: " + ", ".join(changed))
1594
+ # Refuse an unlisted/user-edited shared destination before retiring anything.
1595
+ prior = self._old_record()
1596
+ retiring = set(plan["deletes"])
1597
+ for raw, expected in self._expected_owned(release).items():
1598
+ path = Path(raw)
1599
+ self._safe_owned_path(path, self.bin_root if path.parent == self.bin_root else self.launch_root)
1600
+ if raw not in retiring and path.exists() and _sha256(path) != expected and \
1601
+ _sha256(path) != self._owned_hash(prior, path):
1602
+ raise InstallError(f"private install cannot replace user-owned paths: {path}")
1603
+ migration_id = f"{int(time.time())}-{uuid.uuid4().hex[:12]}"
1604
+ journal = self.runtime / "migrations" / migration_id / "journal.json"
1605
+ backup_root = journal.parent / "backup"
1606
+ path_inputs = {entry["path"] for entry in paths}
1607
+ journal_data = {"schema_version": SCHEMA_VERSION, "owner": "installer", "kind": "migrate",
1608
+ "state": "PREPARED", "phase": "preflight", "plan": plan, "paths": paths,
1609
+ "inputs": {raw: ({k: v for k, v in before.items() if k != "bytes_b64"}
1610
+ if raw in path_inputs else before)
1611
+ for raw, before in inputs.items()},
1612
+ "prior_install": self._file_version(self.record_path),
1613
+ "release": {"package_root": str(release), "installed_files": entries, "release_digest": digest},
1614
+ "backup_root": str(backup_root), "created_at": int(time.time())}
1615
+ self._journal_write(journal, journal_data)
1616
+ return self._finish_migration(journal, journal_data)
1617
+
1618
+
1619
+ def main(argv: list[str] | None = None) -> int:
1620
+ parser = argparse.ArgumentParser(prog="agent-bios corpus")
1621
+ parser.add_argument("--repo", help=argparse.SUPPRESS)
1622
+ subparsers = parser.add_subparsers(dest="command", required=True)
1623
+ install = subparsers.add_parser("install")
1624
+ install.add_argument("--domains")
1625
+ install.add_argument("--dry-run", action="store_true")
1626
+ install.add_argument("--with", dest="with_capabilities")
1627
+ onboard = subparsers.add_parser("onboard")
1628
+ onboard.add_argument("--domains")
1629
+ onboard.add_argument("--dry-run", action="store_true")
1630
+ onboard.add_argument("--with", dest="with_capabilities")
1631
+ subparsers.add_parser("verify")
1632
+ subparsers.add_parser("status")
1633
+ uninstall = subparsers.add_parser("uninstall")
1634
+ uninstall.add_argument("--dry-run", action="store_true")
1635
+ migrate = subparsers.add_parser("migrate")
1636
+ migrate.add_argument("--dry-run", action="store_true")
1637
+ migrate.add_argument("--apply", action="store_true")
1638
+ migrate.add_argument("--yes", action="store_true")
1639
+ reset = subparsers.add_parser("reset")
1640
+ reset.add_argument("--dry-run", action="store_true")
1641
+ reset.add_argument("--apply", action="store_true")
1642
+ reset.add_argument("--yes", action="store_true")
1643
+ reset.add_argument("--expected-revision", help="reset preview generation to accept")
1644
+ args = parser.parse_args(argv)
1645
+ installer = CorpusInstaller(Path(args.repo) if args.repo else Path(__file__).resolve().parent.parent)
1646
+ try:
1647
+ if args.command in {"install", "onboard"}:
1648
+ if args.with_capabilities:
1649
+ raise InstallError("optional --with dependencies are not installed by private corpus mode")
1650
+ result = installer.install(args.domains, args.dry_run)
1651
+ elif args.command == "verify":
1652
+ result = installer.verify()
1653
+ elif args.command == "status":
1654
+ result = installer.status()
1655
+ elif args.command == "uninstall":
1656
+ result = installer.uninstall(args.dry_run)
1657
+ elif args.command == "reset":
1658
+ result = installer.reset(apply=args.apply and not args.dry_run, yes=args.yes,
1659
+ expected_revision=args.expected_revision)
1660
+ else:
1661
+ result = installer.migrate(apply=args.apply and not args.dry_run, yes=args.yes)
1662
+ print(json.dumps(result, ensure_ascii=False, indent=2, sort_keys=True))
1663
+ return 0
1664
+ except InstallError as exc:
1665
+ print(f"corpus-install: {exc}", file=sys.stderr)
1666
+ return 1
1667
+
1668
+
1669
+ if __name__ == "__main__":
1670
+ raise SystemExit(main())