Codex Ownership Ledger Migration¶
Implemented by PR #1540 (commit
4a5067e6) incrates/amplihack-cli/src/commands/install/codex_plugin/. This page describes that change and lands together with it.
Earlier Amplihack releases wrote ~/.amplihack/codex/ownership.json with
schema_version: 1 and an unframed SHA-256 digest (64 hex characters) of the
Codex plugin package at ~/.amplihack/codex/market/plugin. Current releases
compute a framed digest (v2:<hex>). Upgrading used to leave these users
stuck on:
The Codex plugin preparation step (planning::prepare, run when the Codex
launcher is prepared and by amplihack install) now migrates a verified legacy
ledger automatically. Only the Codex package is affected; the change touches
only the codex_plugin module and adds a test file, so Claude and Copilot code
paths are unchanged.
What you will see¶
On the first Codex preparation after upgrading, one line is printed to stderr:
ownership.json is rewritten with the framed digest. Later runs take the
normal path, so migration happens once.
Safety model¶
The ledger is only a claim. Ownership is re-derived from the files on disk. Migration proceeds only when all of these hold:
- The ledger parses,
schema_versionis1,codex_homematches the current Codex home, and the digest is framed or exactly 64 hex characters. - Native plugin identity verification (
verify_identity) has passed. - The package directory is a real directory, not a symlink.
- The unframed digest recomputed from the package equals the ledger digest.
plugin.json,hooks/hooks.jsonandbin/hookare regular files (not symlinks); the manifest name isamplihack;hooks.jsonis the expected empty hooks document;bin/hookis byte-identical to Amplihack's own generated wrapper (never content from the ledger).- The ledger's hook declarations equal the declarations Amplihack generates for that package path (fixed events and subcommands only).
The unframed digest is accepted only to migrate. Afterwards the ledger holds a framed digest, so a legacy digest cannot authorize anything else.
The new ledger is written with the existing atomic_json routine: temp file in
the same directory, fsync, check the ledger is unchanged since it was read,
rename, then sync the directory. Existing file permissions are preserved.
path_scope checks reject symlinked or foreign directory components.
Residual risk¶
The checks use pathnames, not file descriptors, so a small TOCTOU window remains
between verification and the write. atomic_json re-reads the ledger just
before the rename and fails with "Codex JSON changed concurrently; retry" if it
changed, but a same-user process that swaps package files in that window is not
excluded. This matches the module's existing documented guarantees
(path_scope does not promise syscall-level race exclusion).
Refusal behavior (fail closed)¶
Migration is refused, nothing is modified, and the
legacy or changed Codex ownership proof error is returned when:
| Condition | Result |
|---|---|
| Package content modified since install | refused |
| Ledger digest altered | refused |
| Foreign content whose digest matches the ledger | refused |
| Hooks pointing at other commands or paths | refused |
schema_version other than 1, or codex_home mismatch |
refused |
Amplihack never overwrites a package it cannot prove it generated. Inspect
~/.amplihack/codex/, back up anything you want to keep, then remove that
directory and run amplihack install to reinstall cleanly.
Testing¶
Tests are in codex_plugin/legacy_migration_tests.rs and use a temporary
HOME; they never touch the real ~/.amplihack:
verified_legacy_digest_ownership_migrates_to_framed_prooftampered_legacy_package_is_refused_without_changestampered_legacy_digest_is_refused_without_changesmatching_legacy_digest_over_foreign_content_is_not_trustedlegacy_hooks_pointing_elsewhere_are_refused
Use any writable directory for CARGO_TARGET_DIR if the default target/ is
read-only or shared.
Failure-recovery tests¶
Tests in crates/amplihack-cli/src/commands/install/tests/codex_failure_recovery.rs
verify that a failed or interrupted Codex upgrade rolls back exactly. They use
a temporary HOME guarded by EnvGuard and home_env_lock, so the real
~/.codex, ~/.claude and ~/.copilot are never touched.
An upgrade only exercises recovery if the packaged content actually changed.
The upgrade tests therefore install once, then call make_source_newer(), which
rewrites the packaged amplifier-bundle/skills/marker.txt to upgraded\n
before the second install. Without it the second install is a no-op and the
rollback assertions pass vacuously.
After the injected failure, the tests assert byte-exact restoration of:
market/plugin/plugin.jsonandmarket/plugin/skills/marker.txt- the ownership ledger and hooks file
- removal of
pending.jsonon a clean rollback
For the simulated interruption (package replaced, ledger not committed),
previous-package/plugin.json and previous-package/skills/marker.txt must
hold the pre-upgrade bytes and pending.json must remain for retry.
Claude and Copilot install behavior is covered by claude_command_staging_tests,
settings_tests and install_flow.
Maintainer note¶
If the framed digest format changes again, add a new migration step rather than widening the set of accepted legacy digests.