diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index 2adddf7ce..57c7a752a 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -10,7 +10,7 @@ "plugins": [ { "name": "genie", - "version": "5.260722.8", + "version": "5.260724.1", "source": "./plugins/genie", "description": "Human-AI partnership for Claude Code. Share a terminal, orchestrate workers, evolve together. Brainstorm ideas, wish them into plans, make with parallel agents, ship as one team. A coding genie that grows with your project." } diff --git a/.genie/INDEX.md b/.genie/INDEX.md index c73c8b0c2..9ca3499d5 100644 --- a/.genie/INDEX.md +++ b/.genie/INDEX.md @@ -20,6 +20,7 @@ +- [WISH: khal-rebrand](wishes/khal-rebrand/WISH.md) — **APPROVED 2026-07-23** (plan SHIP after 2 fix loops — 12 findings gate-executed against the fork, then 7 repaired in-document with the 3 empirically risky repairs re-executed; 4 pre-commit 3-lens ledger-verify rounds, final round zero must-fix, all flagged record inaccuracies repaired before commit — incl. the true `DASH_*` census: 5 env + `__DASH_ZDOTDIR` + 8 identifiers; per-round figures live in WISH.md): rebrand khal-os/genie-desktop Dash→**Khal** (`ai.khal.genie`, `.khal/` dirs, `DASH_*`→`KHAL_*` env contract, `~/genie` clone default, attribution `Automagik Genie `), real update path (AutoUpdateService) silent-while-private, CI workflow renames, upstream detachment (no more syv pulls — Felipe verbatim), fresh start / NO data migration (Felipe pick). 6 groups; execution gated on genie-ui-dash + live-dev-loop QA passing; G6 icons blocked-on-asset - [WISH: hook-injection-hardening](wishes/hook-injection-hardening/WISH.md) — BLOCKED-clearing safety edit: `execFileSync` at 3 hook sites (audit-context, freshness×2) + hostile-filename regression tests + `core.bare` probe removal; flips panel verdict BLOCKED→FIX-FIRST — SHIPPED → PR #2536 (wish/hook-injection-hardening→main), G1+G2+whole-wish reviews SHIP, 729 pass/0 fail (2026-07-09) - [WISH: v5-completion](wishes/v5-completion/WISH.md) — **DONE** (all 3 groups SHIP-reviewed 2026-07-02; reconcile scope w/ cross-agent-delegate + agent-sync per HANDOFF §4): CLAUDE.md-for-v5 rewrite ∥ Codex launch target + Hermes decision ∥ distribution 5.x - [WISH: dispatch-inproc-default](wishes/dispatch-inproc-default/WISH.md) — **DONE** (both groups SHIP-reviewed 2026-07-02; branch-guard re-armed, regression-gated — hooks demonstrably armed tonight): v5 hooks no longer fall open by default; re-arm branch-guard + unblock omni approvals @@ -48,9 +49,9 @@ - [genie-ui-dash — WISH](wishes/genie-ui-dash/WISH.md) — direct wish (no brainstorm; Felipe-ratified verbatim), plan SHIP + amendment re-review SHIP; **ALL FIVE GROUPS EXECUTED 2026-07-21** on private `khal-os/genie-desktop` branch `genie-dash` @ `9bf43fc`: base+build proof · agent-adapter contract (zero-behavior-change proven) · codex/hermes/rlmx adapters + picker · genie kanban as ui-bridge client · hire roster via roster_hire (single write path). IN_PROGRESS pending Felipe GUI QA + rlmx live spawn; polish-pass LOW backlog consolidated in the G5 disposition. - [WISH: stable-release-security-gate](wishes/stable-release-security-gate/WISH.md) · [brainstorm](brainstorms/stable-release-security-gate/DRAFT.md) — **APPROVED 2026-07-14, plan review SHIP (1 FIX-FIRST loop)**: the stable-channel unblock for inherited Ultra findings F16 (CRITICAL arbitrary-ref stable publish, all 4 dispatch entry points) / F17 (unbound run_id/version inputs) / F18 (unpinned actions, unfrozen installs, broad permissions) / F31 (non-transactional swap; verify-release.sh can't verify real releases). G1 repo-code hardening dispatched to engineer on `wish/stable-release-security-gate`; G2 GitHub Environment (`production` + prevent-self-review) / `v*` ruleset + exported evidence is **Felipe-owned**, depends-on G1 - [hermes-homogeneous-integration](brainstorms/hermes-homogeneous-integration/DESIGN.md) · [WISH](wishes/hermes-homogeneous-integration/WISH.md) — **SHIPPED** (2026-07-13): PR [#2565](https://github.com/automagik-dev/genie/pull/2565) merged to dev `40512016`, released `v5.260713.2` (dev channel); all 6 groups SHIP-reviewed (G2 one fix loop), final gate SHIP; live isit dogfood PASS ([qa/](wishes/hermes-homogeneous-integration/qa/live-dogfood-20260713.md)) — 23 product skills first-class in Hermes, `mcp_servers.genie` + backups in the live profile, 4/4 doctor legs green. **2 dogfood defects filed as follow-up tasks:** D1 duplicate nested `external_dirs` key on merge into block `skills:` (PyYAML last-wins keeps it working); D2 `[auto-version]` skips the plugin.yaml YAML sync (shipped plugin reads 5.260712.2 vs genie 5.260713.2) — both fixed in PR [#2566](https://github.com/automagik-dev/genie/pull/2566) (merged to dev). Also open: khaw-bridge re-home to KHAW plugin; 2 codex-manifest failures pre-existing on main -- [WISH: PR #2545 Ultra release-gate remediation](wishes/pr-2545-ultra-release-gate/WISH.md) — **IN PROGRESS as a follow-up targeting `dev`** (2026-07-11): PR #2545 itself merged via `6f682e2b` from promoted source `10ceb2c0`; the follow-up owns the three-hook reduction, physical 23-skill Codex payload, user-asset preservation, install/update/runtime recovery, documentation, and final evidence. PR [#2556](https://github.com/automagik-dev/genie/pull/2556) merged into `dev` at `a7c3c7ce` and reached `main` via #2557. Successor work on `fix/pr2545-ultra-gate` closed both HIGH follow-ups (F42 `09b368a5`, F43 `8e147d87`, independent SHIP reviews) and ran the seven-lane replay at `8e147d87` (12 verified findings → fixed in loop 1 or dispositioned as F44–F52; the fixes were covered by a targeted SHIP re-review, not a full re-replay); aggregate, merge simulation, and live baseline are green at `e3abf2b5`. Pending: successor-PR CI + human approval, and disabling the external metrics-updater routine (F44) that broke dev CI. Stable promotion remains BLOCKED by F16–F18/F31 in [stable-release-security-gate](wishes/stable-release-security-gate/WISH.md). +- [WISH: PR #2545 Ultra release-gate remediation](wishes/pr-2545-ultra-release-gate/WISH.md) — **DONE** (closed 2026-07-24): PR #2545 merged via `6f682e2b`; follow-up #2556 merged into `dev` at `a7c3c7ce` and promoted via #2557; successor #2562 merged as `137dede8` on 2026-07-12 with 15 checks passing on its exact head `bd8c6128`, and is now on `main`. Both HIGH follow-ups ship fixed (F42 `09b368a5`, F43 `8e147d87`), the seven-lane replay ran at `8e147d87` with its 12 findings dispositioned F44–F52, and the metrics-updater routine has been silent since 2026-07-14. Recorded as unmet rather than waived: **F02** — no independent `APPROVED` review exists on #2562. Stable promotion remains BLOCKED by F16–F18/F31 in [stable-release-security-gate](wishes/stable-release-security-gate/WISH.md); installed hook hashes remain untrusted. - [agent-sync](wishes/agent-sync/DESIGN.md) · [WISH](wishes/agent-sync/WISH.md) · [COORDINATION](wishes/agent-sync/COORDINATION.md) — **MERGED #2541; original implementation superseded by current safety contract** — explicit `genie install`/`genie update` converge detected clients; Codex fallback skills use `~/.agents/skills` (the hidden `.curated` lane is retired), same-name user assets are never adopted, and no lifecycle hook triggers synchronization. Historical execution commits remain in the wish; current proof and the one-time operator-run second-hop caveat live in the [PR #2545 remediation ledger](wishes/pr-2545-ultra-release-gate/REVIEW-DISPOSITION.md). -- [WISH: agent-sync-hardening](wishes/agent-sync-hardening/WISH.md) — **SUPERSEDED CRITERION-BY-CRITERION, not closed wholesale** (2026-07-11): the original PR #2546 execution narrative is historical. The current [PR #2545 remediation ledger](wishes/pr-2545-ultra-release-gate/REVIEW-DISPOSITION.md) maps B1–B7 to concrete hook, lock, ownership, convergence, Windows, and migration evidence. Remaining external/final gates stay open; there is no SessionStart updater and no hook-driven second-hop compatibility path. +- [WISH: agent-sync-hardening](wishes/agent-sync-hardening/WISH.md) — **SUPERSEDED CRITERION-BY-CRITERION, not closed wholesale** (2026-07-11): the original PR #2546 execution narrative is historical. The current [PR #2545 remediation ledger](wishes/pr-2545-ultra-release-gate/REVIEW-DISPOSITION.md) maps B1–B7 to concrete hook, lock, ownership, convergence, Windows, and migration evidence. Its mapped gates closed with the pr-2545 wish on 2026-07-24 except F02 (no independent `APPROVED` review); there is no SessionStart updater and no hook-driven second-hop compatibility path. - [council-workflow](brainstorms/council-workflow/DESIGN.md) · [WISH](wishes/council-workflow/WISH.md) — /council vira saved workflow nativo (deliberation + audit), lens library 13 (7 persona skills renomeadas por lane + 6 cards), distribuição via install-stamp em ~/.claude/workflows; implementa a disposição council do skill-absorbs G4 — EXECUTADO 2026-07-10: G1 `0b222e17`, G2 `12270b21` (1 fix loop: unwrap p/ body-style do runtime), G3 `22a7ed50`, G4 `2af7ebd9`, G5-eng `6494253d` — todos SHIP; QA vivo USER-GATED pós-release (ritual Felipe: merge→release→plugin update→rodar /council "revisar tudo"; g5-gate para na cauda qa/ até lá); final execution review após o QA - [plugin-resource-shipping](brainstorms/plugin-resource-shipping/DRAFT.md) · [WISH](wishes/plugin-resource-shipping/WISH.md) — in-skill template via ${CLAUDE_SKILL_DIR}, probe-guarded lint refs, resource-shipping lint, fresh-install CI smoke — EXECUTED 2026-07-10 (first wish under the routing matrix: engineers opus·high, gate fable·high; 0 fix loops all groups; gate SHIP after branch surgery — concurrent session had switched the shared checkout): G1 ecbb67fc, G3 203c97df, G2 bbd6439e → PR #2540 merged, CI 10/10; QA pending: live installed-plugin scaffold on next release - [routing-matrix](brainstorms/routing-matrix/DRAFT.md) · [WISH](wishes/routing-matrix/WISH.md) — pinned role agents (Fable gates / Opus ladder / Haiku scouts), stage pins + pane flags, complexity columns + lint, escalation caps — executed; execution review SHIP, final gate 719 pass / 1 skip / 0 fail. **QA CLOSED 2026-07-21 ([evidence](wishes/routing-matrix/qa/routing-pin-qa-20260721.md)): fresh-session resolved model IDs 7/7 PASS (haiku / opus×5 / fable exactly per pins); effort observation optional follow-up.** Day-3 07-14 had failed only by logout. diff --git a/.genie/repo-profile.md b/.genie/repo-profile.md index 0a9331bd7..96d0728cc 100644 --- a/.genie/repo-profile.md +++ b/.genie/repo-profile.md @@ -54,8 +54,10 @@ ## Security & Supply Chain (Lorenc) - **Boundary map:** (1) hook stdin→`genie hook dispatch` fork [builtin handlers reach execSync w/ file_path — INJECTION]; repo `.claude/settings.json:15-23` routes `"*"`, shipped plugin `hooks.json:26-36` routes SendMessage-only. (2) Omni NATS inbound→approve+`claude -p` spawn [unauthenticated NATS, OFF by default]. (3) Omni outbound HTTP bearer OMNI_API_KEY + ed25519. (4) `genie update` GitHub-Release chain — re-verified after the 984-line update.ts refactor at dev tip 67b5c5ff: mandatory cosign/attestation verification intact and non-bypassable, but the identity pin is now ANCHORED (`^...sign-attest.yml@refs/heads/main$`) instead of a prefix match — all releases ≤ v5.260715.1 are signed @refs/tags/v* and FAIL the new pin; verified-safe status holds for integrity, with an OPEN cutover/availability hazard until a new-chain release exists. update.ts VERIFIED-SAFE entry needs re-verification (delegates to install-promotion engine; only the promotion journal authorizes release mutation now); cosign.pub sentinel doc tightened (anchored escaped identity-regexp, stale `genie sec` reference removed). (5) CI [no pull_request_target; workflow_run gated]. (6) SessionStart smart-install [degraded root script can be copied over shipped by build:plugin]. -- **NEW — Release chain v2:** Trust model at dev 67b5c5ff: admit jobs in sign-attest.yml/release-publish.yml bind caller to release.yml@refs/heads/main (stable, workflow_dispatch, production env w/ 2-person rule) or version.yml@refs/heads/main (dev/homolog, workflow_run); anchored cosign SAN @refs/heads/main; native SLSA predicate binds channel/source_sha/source_branch/source_ci_run_id; manifests advance monotonically via reconcile-channel-manifests.sh pushed with RELEASE_MANIFESTS_DEPLOY_KEY through the release-manifests environment. Stable publication guard-gated at all 4 entry points: release.yml `guard` job (needs:-wired), sign-attest.yml + release-publish.yml `admit` jobs, build-tarballs.yml standalone dispatch REMOVED (workflow_call+PR only). Guards extracted to fixture-tested scripts (release-guard.sh 401 lines + release-guard.test.ts, reconcile-release-assets.sh, release-generic-provenance.sh, release-native-predicate.sh, release-immutability.sh). install.sh cosign identity pin tightened to `@refs/heads/main$`. production env: stable-only, prevent_self_review, no admin bypass; v* tags immutable ruleset. AS OF 2026-07-15 NEVER EXECUTED: version.yml+release.yml disabled_manually, deploy-key secret nonexistent, org policy blocks repo deploy keys (wish qa/github-settings-evidence-20260714.json). +- **NEW — Release chain v2:** Trust model at dev 67b5c5ff: admit jobs in sign-attest.yml/release-publish.yml bind caller to release.yml@refs/heads/main (stable, workflow_dispatch, production env w/ 2-person rule) or version.yml@refs/heads/main (dev/homolog, workflow_run); anchored cosign SAN @refs/heads/main; native SLSA predicate binds channel/source_sha/source_branch/source_ci_run_id; manifests advance monotonically via reconcile-channel-manifests.sh through the release-manifests environment. Stable publication guard-gated at all 4 entry points: release.yml `guard` job (needs:-wired), sign-attest.yml + release-publish.yml `admit` jobs, build-tarballs.yml standalone dispatch REMOVED (workflow_call+PR only). Guards extracted to fixture-tested scripts (release-guard.sh 401 lines + release-guard.test.ts, reconcile-release-assets.sh, release-generic-provenance.sh, release-native-predicate.sh, release-immutability.sh). install.sh cosign identity pin tightened to `@refs/heads/main$`; v* tags have an immutable ruleset. LIVE STATE 2026-07-23: Version and Release have executed; main ruleset 9203218 has no bypass actors; production requires two reviewers, prevents self-review, disables admin bypass, and is pinned to main; release-manifests disables admin bypass and is pinned to main. RELEASE OPERABILITY BLOCKED: RELEASE_MANIFESTS_TOKEN authenticates as a human and cannot bypass main, so manifest publication fails closed after immutable release finalization until a dedicated GitHub App/service principal is provisioned and a live manifest-advancement proof passes. Never restore a human or repository-role bypass. - **NEW — Installer mutation authority:** install.sh no longer swaps/backs up/rolls back; the cosign-verified staged binary itself runs `genie __install-promote` under a borrowed hard-link lifecycle lease and proves it IS the staged artifact (execPath physical-identity compare, install-promote.ts:160-167); all moves go through install-transaction.ts native no-clobber renames (renameat2/RENAME_NOREPLACE, renameatx_np/RENAME_EXCL, raw-syscall fallback 316/276, musl loader candidates) which never unlink. VERIFIED-SAFE additions to re-verify on change: install-transaction.ts constants/syscall table; uninstall ownership-proof removal (managed-clean only, uninstall.ts:1063+). +- **NEW — Codex lifecycle serialization:** install, update, and staged promotion acquire the outer agent-sync lease before the inner Codex lifecycle lease and release them in reverse order; payload recovery, mutation, delivery publication, and terminal projection occur only inside that authority. The shared lease uses bounded staging slots, generation-bound cleanup, and recovery claims; fresh/stale multi-process barriers plus setup-held install/update/promoter tests lock the fail-closed behavior. +- **NEW — Exact task-CWD MCP database binding:** MCP resolution rejects symlinked, hardlinked, non-regular, escaped, or substituted `.genie/genie.db` paths and binds the opened readonly SQLite handle with `SQLITE_FCNTL_HAS_MOVED` checks around physical path revalidation. Unsupported/error/moved results close the handle and fail closed; a real A→B-open/B→A-restore regression covers the pathname-restoration race. - **NEW — CI surface, version.yml executes no repo code:** the write-capable version workflow no longer runs bun install or package scripts from the dev checkout; six version fields rewritten by trusted jq/sed with symlink rejection and exact staged-delta allowlist, commit --no-verify. Regression here (reintroducing repo-script execution under contents:write) would reopen a code-to-write-token escalation — treat as a LOCKED CONTRACT. - **NEW — Stable manifest pointer drift:** Observed 2026-07-15: GH latest release v5.260714.3 (non-prerelease) vs main .well-known/latest.json = 5.260711.6; the (dev,homolog,stable) manifest commit 0563c9db for v5.260714.3 is stranded on dev. Pre-existing chain defect; should disappear after cutover — re-check next audit. - **Credential inventory:** ed25519 host key `~/.genie/keys/`; Omni bearer OMNI_API_KEY; CI GITHUB_TOKEN; cosign keyless OIDC (no long-lived key). All at-rest creds user-local. diff --git a/.genie/wishes/codex-plugin-dogfood-remediation/HANDOFF-20260722.md b/.genie/wishes/codex-plugin-dogfood-remediation/HANDOFF-20260722.md new file mode 100644 index 000000000..6404a75a5 --- /dev/null +++ b/.genie/wishes/codex-plugin-dogfood-remediation/HANDOFF-20260722.md @@ -0,0 +1,43 @@ +# Handoff — codex-plugin-dogfood-remediation (2026-07-22) + +**Wish:** `.genie/wishes/codex-plugin-dogfood-remediation/WISH.md` (APPROVED, 7 groups A–G). +**Status: Waves 1 + 2 DONE and merged to dev. Next = Wave 3 (Group E).** + +## DAG +`B → A`, `B → D`, `{A,B,D} → E`, `{C,E} → F`, `F → G`. Groups B,C = Wave 1; A,D = Wave 2; E = Wave 3; F = Wave 4; G = Wave 5. + +## Merged so far (all in `origin/dev`, tip was `8a92fbd6`) +| Group | PR | Verified | +|---|---|---| +| B host-observation-attestation | #2625 | 165/0 incl. live app-server proof; pre-mutation guard real | +| C managed-assets-convergence | #2626 | 403/0; byte-identical preservation, R3 intact | +| A project-route-context | #2629 | 94/0; empty-board masquerade closed; plugin Codex MCP route removed | +| D immutable-delivery-repair | #2628 | 397/0; one-shot repair + `.install-version` module | + +Files landed: `src/lib/codex-host-observation.ts`, `src/fixtures/codex-role-agent-allowlist.json`, `src/lib/codex-project-mcp.ts` + `resolveProjectContext` in `src/lib/v5/mcp-tools.ts`/`mcp-server.ts`, `src/lib/install-version-marker.ts`, delivery-repair in `src/genie-commands/update.ts`/`install.ts`/`codex-delivery*.ts`. Plugin `.mcp.json` + manifest `mcpServers` DELETED (Claude's `scripts/mcp-launcher.cjs` retained). + +## CARRY-FORWARD into Group E (lifecycle-truth-integration) — do these, they were deliberately deferred, not dropped +1. **A's config-layer typed states** (deferred per Decision 15): distinct classification for `untrusted-config`, `global same-key route`, `effective-layer shadowing`, `route-collision`, `route-shadowed`, `project-trust-required` — these are doctor/init diagnostics, not read-only-MCP-server states. A shipped only `project-context-unavailable`/`project-database-unavailable`/`unsupported-project-layout`. +2. **D's `uninstallInstallVersionMarker` wiring**: exported+unit-tested but NOT called by `src/genie-commands/uninstall.ts`. Behavior is already correct (`.install-version` removed by the digest-verified wholesale GENIE_HOME child removal — it's absent from `preservedGenieDirEntry`). Wire the explicit symlink-safe call **with awareness the wholesale removal is digest-verified** (removing the marker first could invalidate `scope.genieHomeRemovalDigest`), OR delete it + document. Do NOT rush this into the digest path. +3. **A's live-QA-only item (AC3)**: `codex mcp get genie --json` showing one marker-owned facade route + no plugin route — Felipe's post-merge live-QA ritual, kept out of CI. + +## Group E scope (from WISH) +"lifecycle-truth-integration: wire route/context, observation, repair handoff, setup, and doctor" — integrate A+B+D contracts into setup/doctor WITHOUT changing their contracts. Complexity 5. depends-on {A,B,D} (all merged ✓). + +## HOW to run a wave (the working pattern — reuse it) +`Workflow` tool, isolated worktrees, `model:'opus' effort:'max'`, `agentType:'engineer-complex'`, `isolation:'worktree'`. Pipeline: implement → adversarial `reviewer`. Engineers commit to `wish/dogfood-` (NO push), report structured. Then ORCHESTRATOR: +- Re-run the group's validation YOURSELF in its worktree (`.claude/worktrees/wf_*-N`) — do not trust the report. +- **Run codex-touching tests with `codex` STRIPPED from PATH** (`PATH="$HOME/.bun/bin:/usr/bin:/bin"`) — this is the CI condition; it caught the flake. +- Strip any stray `.well-known/dev.json` (`git checkout origin/dev -- .well-known/dev.json`). +- Push, open PR to dev, then **`gh run watch` the CI to green** — the black-box **Codex Plugin Smoke** and **E2E** jobs run things the file-level review can't. Fix reds via a `fixer` (opus) with the smoke as the gate. +- `SKIP_CI_CHECK=1 git commit` is the sanctioned escape when a commit IS the CI fix. + +## Hard-won gotchas (cost us fix loops this session) +- **CI has NO `codex` binary and NO network.** Any test spawning `codex` must do a synchronous `execFileSync('codex',['--version'])` preflight and skip; never let an async spawn `'error'` (ENOENT) throw unhandled at module load (that aborts the whole file — Group B's first CI fail). +- **Readonly SQLite opens must carry `busy_timeout`.** Raw `new Database(p,{readonly:true})` bypasses `sqlite-open.ts`'s `BUSY_TIMEOUT_MS` → flaky "database is locked" under concurrent writers (Group A's HIGH). Import `BUSY_TIMEOUT_MS`; checkpoint (`wal_checkpoint(TRUNCATE)`) writers before readers in tests. +- **Plugin health ≠ Codex MCP provider anymore.** A removed the plugin MCP route; `proveCodexPluginHealth`/`parseCodexPluginSnapshot`/`codex-mcp-health-session.ts` were reconciled so health = installed+enabled+version+canonical-root+payload+working launcher, and the throwaway-cwd health probe now accepts A's typed `project-context-*` errors as healthy. Don't regress this. +- Discipline reminders: reviewer ≠ engineer; verify every subagent claim with your own runs; Felipe merges PRs himself (present them, don't merge); the roadmap board + this wish are the trackers. + +## Separately open (not this wish) +- `stable-release-security-gate` wish still blocks STABLE promotion. +- codex-plugin-update-handoff live homolog dogfood ritual (Felipe's) gates that wish → SHIPPED. diff --git a/.genie/wishes/codex-plugin-dogfood-remediation/WISH.md b/.genie/wishes/codex-plugin-dogfood-remediation/WISH.md index f4591853f..2fa8801e8 100644 --- a/.genie/wishes/codex-plugin-dogfood-remediation/WISH.md +++ b/.genie/wishes/codex-plugin-dogfood-remediation/WISH.md @@ -2,7 +2,7 @@ | Field | Value | |-------|-------| -| **Status** | APPROVED | +| **Status** | IN_PROGRESS | | **Slug** | `codex-plugin-dogfood-remediation` | | **Date** | 2026-07-22 | | **Author** | Felipe Rosa + Codex dogfood investigation | @@ -182,16 +182,16 @@ collision requiring user resolution rather than rewritten by this command. ## Success Criteria -- [ ] An app-server black-box test proves `process.cwd()` in the MCP child exactly equals +- [x] An app-server black-box test proves `process.cwd()` in the MCP child exactly equals a Codex control process's effective `process.cwd()` by string and OS directory identity for a repository root, nested directory, linked worktree, symlink/case-normalized path, and two repos; raw `thread/start.cwd` remains separately visible only in `CodexCwdEvidence`, not production `CodexHostObservation`. -- [ ] One pinned long-lived Codex app-server runs sequential and concurrent threads in two repositories, +- [x] One pinned long-lived Codex app-server runs sequential and concurrent threads in two repositories, records raw request plus child PID/CWD per harness case, and proves a child PID is never reused when the effective CWD string or OS identity differs. Reuse is allowed when both effective-CWD values match, irrespective of case labels; tagged sentinels still detect cross-talk. -- [ ] The linked-worktree case keeps the child and marker config in the linked worktree while +- [x] The linked-worktree case keeps the child and marker config in the linked worktree while `gitCommonDir` is the absolute main-repository Git directory, `genieStorageRoot = dirname(gitCommonDir)`, and `resolveDbPath` reads exactly `/.genie/genie.db`; neither main-worktree CWD nor plugin cache appears as launch @@ -199,53 +199,53 @@ collision requiring user resolution rather than rewritten by this command. - [ ] `codex mcp get genie --json` reports the marker-owned absolute project command with no effective CWD override, accepting either absent or `null` host serialization, and the installed Codex plugin contributes no second Genie MCP route. -- [ ] Group B's black-box proof receives independent execution-review SHIP before any Group A route +- [x] Group B's black-box proof receives independent execution-review SHIP before any Group A route migration merges, activates, or removes the plugin MCP route; a failed proof leaves the old route intact and blocks migration. -- [ ] Enabled, disabled, absent, malformed, and upgraded plugin fixtures retain exactly one owned project +- [x] Enabled, disabled, absent, malformed, and upgraded plugin fixtures retain exactly one owned project route, preserve unrelated TOML byte-for-byte, and converge idempotently; user-owned project/global same-key routes, damaged markers, and shadowing are never overwritten. -- [ ] A two-repository 5.260722.1 migration leaves reconciled A healthy, untouched B explicitly route +- [x] A two-repository 5.260722.1 migration leaves reconciled A healthy, untouched B explicitly route absent with no cache fallback, and makes `cd B && genie init` the only one-command reconciliation; afterward B returns only B's sentinel. -- [ ] The marker uses the canonical stable absolute `GENIE_HOME/bin/genie` facade across plugin updates +- [x] The marker uses the canonical stable absolute `GENIE_HOME/bin/genie` facade across plugin updates and symlinked invocation; explicit home relocation is reconciled only by trusted init, and uninstall leaves an inert/actionable missing executable rather than a versioned-cache fallback. -- [ ] Nested config shadowing and untrusted project config are reported distinctly; untrusted config is +- [x] Nested config shadowing and untrusted project config are reported distinctly; untrusted config is left intact and doctor says trust is required rather than claiming MCP health. -- [ ] A nested initialized repository uses its own common/storage root; a nested Git repository without +- [x] A nested initialized repository uses its own common/storage root; a nested Git repository without Genie initialization fails at that boundary and never reads an outer database. -- [ ] Bare repositories, submodules, and external/separate-Git-dir layouts return +- [x] Bare repositories, submodules, and external/separate-Git-dir layouts return `unsupported-project-layout` before config/DB success and never use an outer repository or plugin cache as fallback. -- [ ] Missing Git/Genie context or `.genie/genie.db` returns a stable structured MCP error and can never +- [x] Missing Git/Genie context or `.genie/genie.db` returns a stable structured MCP error and can never serialize a healthy empty board. -- [ ] Upgrade messaging requires a new Codex task after route changes; no claim is made that an existing +- [x] Upgrade messaging requires a new Codex task after route changes; no claim is made that an existing task's already-started MCP process was rebound. -- [ ] Outside trusted `genie init`, missing/invalid/mismatched attestation causes zero prompt or +- [x] Outside trusted `genie init`, missing/invalid/mismatched attestation causes zero prompt or activation-owned journal, plugin, enabled-state, project-route, role-agent, or cache mutation and reports `deliveryComplete: false`; doctor remains read-only in every state. -- [ ] A provenance-verified current-version repair handles both an old registered parent and the live +- [x] A provenance-verified current-version repair handles both an old registered parent and the live target-current/removal-observed intent, publishes once, preserves the parent until activation, and remains idempotent with a matching record. -- [ ] Same-version repair pins channel, immutable target version/platform, release tag/name, and fetched +- [x] Same-version repair pins channel, immutable target version/platform, release tag/name, and fetched manifest bytes/digest before download; downloads that exact named asset, computes SHA-256 only afterward, authenticates the digest for repository `automagik-dev/genie`, predicate `https://github.com/automagik-dev/genie/release-tarballs/v1`, workflow identity `https://github.com/automagik-dev/genie/.github/workflows/sign-attest.yml@refs/heads/main`, and issuer `https://token.actions.githubusercontent.com`, then persists the authenticated digest. -- [ ] Under the lifecycle lease, same-version repair rechecks the pinned channel and routes an advance to +- [x] Under the lifecycle lease, same-version repair rechecks the pinned channel and routes an advance to ordinary upgrade; it never claims the release manifest supplied an artifact digest or relabels installed bytes. -- [ ] Setup finalization after repair performs no second plugin add, restores enabled state, completes +- [x] Setup finalization after repair performs no second plugin add, restores enabled state, completes parity/H3, clears the journal normally, and ends with doctor state `current`. -- [ ] Doctor spawns exactly one Codex plugin query and never reports PASS together with a query-failed +- [x] Doctor spawns exactly one Codex plugin query and never reports PASS together with a query-failed integration summary; the real bounded PATH advisory does not corrupt JSON stdout or force failure. -- [ ] Failed or pending standalone `setup --codex` preserves config bytes and prints no green saved +- [x] Failed or pending standalone `setup --codex` preserves config bytes and prints no green saved banner, including on a historically configured machine. -- [ ] Missing-inventory migration ends with all delivered role agents current, including the reviewer, +- [x] Missing-inventory migration ends with all delivered role agents current, including the reviewer, with one managed surface per role and every unrelated/personal agent byte- and mode-identical. -- [ ] Group D's successful install/update convergence leaves no `.install-version`; interrupted/failed +- [x] Group D's successful install/update convergence leaves no `.install-version`; interrupted/failed install/update preserves the prior marker and installed trees, uninstall tolerates both layouts, and every path is safe to rerun without Group C role writes. - [ ] Live evidence validation accepts only real nested doctor JSON, rejects the obsolete flat fixture, @@ -353,23 +353,23 @@ and incapable of returning an outer/cache-root empty board. inert actionable marker, never a cache path. **Acceptance Criteria:** -- [ ] Root, ordinary nested directory, initialized nested repository, uninitialized nested repository, +- [x] Root, ordinary nested directory, initialized nested repository, uninitialized nested repository, and linked-worktree fixtures resolve the specified config root, absolute `gitCommonDir`, and exact `dirname(gitCommonDir)` storage root without changing effective launch CWD. -- [ ] Linked worktree config is read from the linked root and its sentinel from the common-root DB; +- [x] Linked worktree config is read from the linked root and its sentinel from the common-root DB; the DB path is exactly `/.genie/genie.db`, and neither outer, main-worktree-CWD, nor cache storage is substituted. - [ ] `codex mcp list/get --json` shows one owned project route with a stable absolute command and no effective CWD override (accepting absent or `null` serialization); no Codex plugin route remains and Claude's launcher remains covered. -- [ ] User-owned project/global same-key routes, damaged markers, malformed config, trust boundaries, +- [x] User-owned project/global same-key routes, damaged markers, malformed config, trust boundaries, and shadowing are byte-identical after inspection/reconciliation attempts and receive distinct actionable states. -- [ ] Bare, submodule, and external/separate-Git-dir fixtures return `unsupported-project-layout` before +- [x] Bare, submodule, and external/separate-Git-dir fixtures return `unsupported-project-layout` before DB/tool success and never probe an outer repository or plugin-cache database. -- [ ] Two-repo migration proves reconciled A works, untouched B has explicit absence/no fallback, and +- [x] Two-repo migration proves reconciled A works, untouched B has explicit absence/no fallback, and `cd B && genie init` safely reconciles only B. -- [ ] Stable command fixtures cover plugin update, symlinked launch, explicit home relocation, and +- [x] Stable command fixtures cover plugin update, symlinked launch, explicit home relocation, and uninstall without writing a versioned plugin-cache path or overwriting an unowned route. **Validation:** @@ -415,18 +415,18 @@ and refuse activation before mutation without a matching authenticated record. 7. Add mutation spies and record-tampering fixtures for every bound record field and stale permits. **Acceptance Criteria:** -- [ ] One app-server proves root/nested/linked/symlink-normalized launch behavior and both sequential and +- [x] One app-server proves root/nested/linked/symlink-normalized launch behavior and both sequential and concurrent two-repo threads; each harness case records raw request, child PID/effective CWD, and a tagged sentinel. A child PID never crosses differing effective-CWD strings or OS identities; same-effective-CWD reuse is allowed, and no cross-talk or cache-root context occurs. -- [ ] Child and control effective CWDs have exact string and directory-identity equality; evidence keeps +- [x] Child and control effective CWDs have exact string and directory-identity equality; evidence keeps the potentially different raw requested spelling rather than normalizing it away, while production `CodexHostObservation` contains neither raw request nor control-only fields. -- [ ] No non-matching record path prompts or performs activation-owned journal, registration, enabled, +- [x] No non-matching record path prompts or performs activation-owned journal, registration, enabled, project-route, role, or cache mutation; a stale permit cannot bypass the inner guard. -- [ ] Matching records preserve downgrade binding and final callback revalidation; tampering with any +- [x] Matching records preserve downgrade binding and final callback revalidation; tampering with any version/platform/manifest/artifact/binary/payload/delivery/intent binding becomes invalid/mismatch. -- [ ] One bounded result supplies all downstream projections; the real PATH advisory succeeds only with +- [x] One bounded result supplies all downstream projections; the real PATH advisory succeeds only with valid exit/stdout, and every failure remains one ANSI-free typed result. **Validation:** @@ -459,12 +459,12 @@ bun test src/lib/codex-host-observation.test.ts \ unrelated personal agents, interrupted role migration, and repeated role convergence. **Acceptance Criteria:** -- [ ] The live-shape missing-inventory fixture converges every delivered role and the current reviewer +- [x] The live-shape missing-inventory fixture converges every delivered role and the current reviewer while unrelated/personal files retain exact bytes, type, permissions, and timestamps where supported. -- [ ] Enabled plugin state exposes one managed surface per role; disabled/absent state has the intended +- [x] Enabled plugin state exposes one managed surface per role; disabled/absent state has the intended fallback, and reruns create no duplicates. -- [ ] Modified, unknown, symlinked, or profile-lookalike collisions are reported and never overwritten, +- [x] Modified, unknown, symlinked, or profile-lookalike collisions are reported and never overwritten, adopted, or deleted. **Validation:** @@ -507,18 +507,18 @@ performing activation or redefining that target from a moving channel. repeated repair neither downloads nor republishes. **Acceptance Criteria:** -- [ ] The old-parent fixture publishes one record bound to the pinned tuple, keeps N registered, and exits +- [x] The old-parent fixture publishes one record bound to the pinned tuple, keeps N registered, and exits 2 with the exact next action; a matching rerun performs no download/publication. -- [ ] The live target-current/removal-observed fixture repairs without any plugin command or other +- [x] The live target-current/removal-observed fixture repairs without any plugin command or other activation-owned mutation. -- [ ] A channel advance routes to ordinary upgrade and never mints a record for stale installed bytes. -- [ ] Repair pins channel/version/platform/release tag/name plus manifest bytes/digest before download, +- [x] A channel advance routes to ordinary upgrade and never mints a record for stale installed bytes. +- [x] Repair pins channel/version/platform/release tag/name plus manifest bytes/digest before download, downloads the exact named asset, computes SHA-256 afterward, authenticates it against every stated repository/predicate/workflow/OIDC anchor, and persists the computed authenticated digest. -- [ ] Field, artifact, manifest, payload, platform, installed-byte, and intent tampering plus lease or +- [x] Field, artifact, manifest, payload, platform, installed-byte, and intent tampering plus lease or reobservation failure leave record/journal/plugin/cache/config/roles unchanged and report `deliveryComplete: false`. -- [ ] Successful install/update retires `.install-version`; injected failures preserve its prior bytes +- [x] Successful install/update retires `.install-version`; injected failures preserve its prior bytes and installed tree, reruns converge safely, and uninstall accepts present/absent markers without any role-agent/inventory mutation. @@ -558,15 +558,15 @@ activation, and doctor surfaces. doctor current, including PATH advisory, stale historical config, route collision, and context errors. **Acceptance Criteria:** -- [ ] Missing/invalid/mismatched record reaches neither prompt nor activation-owned mutation and produces +- [x] Missing/invalid/mismatched record reaches neither prompt nor activation-owned mutation and produces one consistent `delivery-incomplete` result with the update/install recovery command. -- [ ] After target-current repair, setup performs zero second plugin add, completes parity/H3, restores +- [x] After target-current repair, setup performs zero second plugin add, completes parity/H3, restores enabled state, and clears the journal only through normal protocol. -- [ ] Failed/pending standalone setup preserves config bytes and omits the green banner even when +- [x] Failed/pending standalone setup preserves config bytes and omits the green banner even when historical config says configured; only this invocation's current success persists that state. -- [ ] Doctor spawns exactly one plugin observation and cannot combine PASS/current with query-failed, +- [x] Doctor spawns exactly one plugin observation and cannot combine PASS/current with query-failed, unhealthy project context, route collision/shadowing, or missing delivery. -- [ ] Real PATH advisory yields one ANSI-free JSON object and consistent human/trailer/exit; timeout, +- [x] Real PATH advisory yields one ANSI-free JSON object and consistent human/trailer/exit; timeout, overflow, malformed/duplicate JSON, and nonzero exit fail consistently. **Validation:** @@ -795,6 +795,74 @@ _The read-only reviewer returns evidence; the invoking orchestrator appends a ti dogfood/publication/channel advancement, and prevent write-capable jobs from executing candidate code. The advisory audit passed 258 focused tests with 0 failures and made no profile changes. +### 2026-07-23T01:55:00Z — Group E execution review + +- **Context:** Group E (lifecycle-truth-integration) execution review, PR #2630 (`wish/dogfood-E` → dev) +- **Branch commits:** fc96d3ee (handoff doc), f660d902 (doctor single observation), 51374f2c (setup + delivery gate + typed outcome), c73850cb (post-activation route fix + real-PTY flow), ed75b432 + (uninstall-marker retirement), 6878189 (review LOW fixes: dotted-key route detection, typed + context-state PTY assertion) +- **Reviewer:** independent adversarial execution reviewer (not the author); verdict **SHIP** — + CRITICAL none; HIGH none; MEDIUM two (both confirmed intended: doctor `deliveryComplete` now + requires a matching authenticated record per the authority matrix, so pre-record installs report + `delivery-incomplete` until one `genie update`; `project-trust-required` warns for never-trusted + projects per "never claim health for an untrusted project"); LOW three (two fixed in 6878189, one + noted: collision-after-activation throw is pre-existing polish debt) +- **Reviewer validation (self-run):** 277/0 across the seven touched suites; 194/0 adjacent + executor/project-mcp/activation/host-observation suites; typecheck and lint exit 0; A/B/D contract + files byte-identical to origin/dev (empty diffstat) +- **Orchestrator validation (own runs):** full `bun test` 2528 tests with the sole failure being the + pre-existing Linux-only `ss`-based ui-bridge socket test (green in CI); CI condition (codex stripped + from PATH) 277/0 including the real-PTY flow; `bun run smoke:codex` exit 0; PR CI rollup all + SUCCESS, merge state CLEAN +- **Defect found by the new PTY flow and fixed in-wave:** setup's post-activation reconcile used the + pre-Group-A synthetic usable-plugin probe whose documented behavior removed the project fallback, + leaving a repository with NO Codex route after successful activation (Decision 1 violation); the + post-activation probe now declares route-unusability and reconciles the stable absolute + `GENIE_HOME/bin/genie` facade route exactly as trusted init writes it (Decision 2) +- **Carry-forwards resolved:** A's deferred typed config-layer states shipped as the route-layer + classifier (`route-collision`, `route-shadowed`, `global-route-same-key`, `untrusted-config`, + `project-trust-required`) with doctor JSON riders; D's `uninstallInstallVersionMarker` retired with + documented digest-window rationale and both legacy layouts pinned through the real digest-verified + batch path; A's live-QA item (AC3 `codex mcp get genie --json`) remains reserved for the operator's + post-merge ritual +- **Verdict:** **SHIP** — Group E awaits operator merge of PR #2630; durable wish status unchanged + until merge + +### 2026-07-23T05:30:00Z — Ledger maintenance: waves 1+2 execution evidence + live-QA follow-through + +- **Context:** the H3 SessionStart line surfaced ledger drift (status still APPROVED, 5/67 criteria, + waves 1+2 evidence never recorded here). This block backfills the durable record; no code changed. +- **Status:** APPROVED → **IN_PROGRESS** (execution began 2026-07-22; upstream + `codex-plugin-update-handoff` code merged to dev, its WISH→SHIPPED remains gated on the operator's + homolog ritual, so the dependency-derived `blocked` flag persists by design). +- **Waves 1+2 (merged to dev 2026-07-22, per-group execution review SHIP, validations reproduced by + the invoking orchestrator before merge):** + - Group B host-observation-attestation — PR #2625, 165/0 including the live pinned app-server + black-box proof; B SHIP preceded any Group A route change (Decision 3 honored). + - Group C managed-assets-convergence — PR #2626, 403/0; byte-identical preservation of + modified/unknown/symlinked/personal files; R3 intact. + - Group A project-route-context — PR #2629, 94/0; empty-board masquerade closed; plugin Codex MCP + route removed; marker-owned project route authoritative. + - Group D immutable-delivery-repair — PR #2628, 397/0; one-shot pinned repair + `.install-version` + retirement module. +- **Wave 3 (Group E):** PR #2630 SHIP evidence recorded in the 2026-07-23T01:55Z block above; the + operator merge that block awaited landed as dev commit `4be6917f` (2026-07-23T02:22Z), which is + the merge the Group-E criterion ticks rest on. +- **Live-QA follow-through (operator dogfood on two hosts, seven defects found and fixed same-day, + all merged to dev):** #2631 journal-quarantine permit consumption; #2632 `$HOME` + trusted-executable false positive + dead-end already-current recovery + codex-only recovery text + on a claude failure; #2633 post-A manifest false-warns in doctor; #2634 absent-N delivery-record + publication (fresh host); #2636 stale-record republication when a verified delivery lands on a + current generation. +- **Open follow-up (deferred by #2633, previously tracked only in that PR body):** the route + probe's `usable`/`activeManifestError` semantics still encode the pre-A manifest expectation; + retiring those pre-A plugin-route arms wholesale needs its own reviewed pass (naively flipping + `usable` would resurrect route-conflict/fallback-removal behavior). Queued behind Group F. +- **Checkbox policy applied:** ticked only criteria owned by merged groups with CI-reproduced or + PTY-flow evidence. Deliberately NOT ticked: the two live `codex mcp get genie --json` proofs (the + operator's AC3 ritual — output never captured), Group F/G criteria, and all post-merge QA rows. + --- ## Files to Create/Modify diff --git a/.genie/wishes/khal-rebrand/WISH.md b/.genie/wishes/khal-rebrand/WISH.md new file mode 100644 index 000000000..73373295b --- /dev/null +++ b/.genie/wishes/khal-rebrand/WISH.md @@ -0,0 +1,371 @@ +# Wish: khal-rebrand — the UI is Khal, the tether to syv is cut + +| Field | Value | +|-------|-------| +| **Status** | APPROVED | +| **Slug** | `khal-rebrand` | +| **Date** | 2026-07-22 | +| **Author** | Felipe (7 explicit picks) + Fable orchestrator (derivations flagged in Decisions) | +| **Appetite** | medium | +| **Branch** | `wish/khal-rebrand` (docs in genie). Fork code: execution groups branch off fork `main` (`khal-os/genie-desktop`, working copy `~/prod/genie-ui-ab/dash-fork`), push plain branches, merge server-side (`gh api -X POST repos/khal-os/genie-desktop/merges`). Remote trap until Group 5 lands: local `main` in the Linux working copy tracks UPSTREAM syv-ai/dash — never build/push there. | +| **Repos touched** | dash fork (khal-os/genie-desktop): all code/config/CI/docs; genie (this repo): wish docs only | +| **Design** | _No brainstorm — direct wish_ | + +## Summary + +Rebrand the forked desktop app from Dash (syv-ai) to **Khal**: product name, appId `ai.khal.genie`, per-project dirs, the `DASH_*` PTY env contract, in-app strings, update path, CI workflow, and docs — and cut the upstream tether (Felipe: "i dont intend to keep pulling updates from them"). Fresh start on app state: **no data migration** (Felipe's explicit pick — the rename moves Electron userData and the app's SQLite DB; old state is abandoned in place, not deleted). The MIT license notice from syv is retained; rebrand is identity, not attribution-stripping. + +## Scope + +### IN + +- Product identity: `package.json` name/description/`productName: Khal`/`appId: ai.khal.genie`/`author: Automagik Genie `, `app.setName('Khal')`, electron-builder `publish` block → `khal-os/genie-desktop`, artifact names, Makefile override cleanup. +- CI: `.github/workflows/build.yml` — `Khal.app` codesign path, `Khal-*` artifact names, release job consistent with the khal-os publish target (it checks out the fork, so `gh release create` already lands there; naming and paths must not break at `productName: Khal`). +- Update path: `electron-updater`/`AutoUpdateService` (the REAL packaged-app path) absorbs private-repo feed failures silently (log only, no user-visible error; genuine update flow preserved), feed follows the Group 1 publish block; dev-only `getDevVersion()` repointed; release-notes links and update toast → khal-os/genie-desktop. +- Machine-contract rename: per-project dir `.dash/` → `.khal/` via ONE shared constant (179 literal occurrences / 39 files, compound forms included); skills marker filenames `.dash-skill.json`/`.dash-skill-checked.json`/`.dash-tmp` → `.khal-*` via their OWN constants with the legacy in-tree migration logic removed (fresh start); the `DASH_*` namespace → `KHAL_*` — 14 distinct tokens verified in-tree: 5 injected PTY env vars, `__DASH_ZDOTDIR`, and 8 non-env JS identifiers (exact partition with source citations in Group 3 deliverable 3). +- Human-facing sweep: "Dash" strings across src (~71 files), `PortsSetupPrompt` canonical paragraph → Khal + `https://github.com/khal-os/genie-desktop`, commit co-author trailer → `Automagik Genie `, `syv.ai` links → the fork repo URL, default clone dir `~/Dash` → `~/genie`. +- Docs + repo hygiene: README rewrite, GENIE.md, Makefile comment, LICENSE keeps the syv MIT copyright line and adds the new one, ITERATE.md remote-topology rewrite. +- Upstream detachment: remove the syv-ai remote from working copies, promote the fork to `origin` everywhere. +- Icon/visual asset swap (`build/icon.icns`, `icon.svg`, `build/win/`) — **blocked until Felipe supplies assets** (his pick: "You'll supply assets"). + +### OUT + +- **Data migration of any kind** — no userData copy, no `.dash/` → `.khal/` content migration, no `~/Dash` clone moves, no genie.db moves. Felipe explicitly chose fresh start; the earlier queued idea (".dash → .genie/desktop + ~/.genie/...") is SUPERSEDED (Decision 3). +- The syv plugin marketplace: `SYV_MARKETPLACE_REPO`, `SYV_MARKETPLACE_NAME`, `SYV_PLUGIN_ID` (`syv-skills@syv-skills`) in `PluginsService.ts` AND the `.claude/settings.json` `syv-skills@syv-skills` entry are KEPT verbatim (Felipe's pick: content source, not branding) and are excluded from every sweep and every "no syv" gate. +- The genie CLI, ui-bridge protocol, and `.genie/genie.db` contract — untouched; this wish is fork-side identity only. (Verified: genie repo src has zero `DASH_` references — the env rename breaks nothing cross-repo.) +- Making khal-os/genie-desktop public or changing release visibility — the update path must degrade silently while private, not force a visibility change. +- Renaming the GitHub repo itself (stays `khal-os/genie-desktop`). +- Backward-compat shims for `DASH_*` env vars or legacy `.dash-skill` markers — clean break, one-time cost accepted. + +## Decisions + +| # | Decision | Rationale | +|---|----------|-----------| +| 1 | Product name **Khal**, appId **`ai.khal.genie`** | Felipe verbatim: "ai.khal.genie the ui is going to be khal only". | +| 2 | **Fresh start, no migration** | Felipe's explicit pick, made with the data-loss consequence stated. Old dirs/userData left in place as manual backup, never deleted. Extends to skills markers: legacy `.dash-skill.json` markers are unrecognized post-rename; skills re-verify once. | +| 3 | Per-project dir = **`.khal/`**, userData = **"Khal"** — DERIVED, veto point | Derived from "the ui is going to be khal only". Supersedes the earlier ".dash → .genie/desktop" idea, which predates the Khal naming. Single-constant design makes a veto a one-line change. | +| 4 | Update path: **silent degrade while private, via the real path** | Felipe picked "repoint + graceful degrade". Plan review proved the real packaged path is `electron-updater`/`AutoUpdateService` (dev-only `getDevVersion()` never runs shipped); silence is implemented and tested THERE. Feed itself follows the electron-builder publish block. | +| 5 | LICENSE keeps the syv MIT copyright notice | MIT requires it. New copyright line added above; syv attribution stays in LICENSE. Founder emails leave `package.json` `author` (Decision 6 replaces it) — LICENSE is where upstream credit lives. | +| 6 | Attribution identity = **`Automagik Genie `** | Felipe verbatim: "author Automagik Genie genie@namastex.ai". Applied to `package.json` `author` AND the commit co-author trailer (replacing `via Dash `). | +| 7 | `DASH_*` → **`KHAL_*`**, the full namespace (5 injected env vars + `__DASH_ZDOTDIR` + 8 non-env identifiers), no dual-export | Felipe's pick. Breaks any personal scripts reading `DASH_*` from PTYs — accepted one-time cost, consistent with no-upstream fresh break. | +| 8 | Default clone dir = **`~/genie`** | Felipe verbatim: "genie man, genie...". Not `~/Khal`; existing `~/Dash` clones untouched and keep working. | +| 9 | One shared constant per contract name, then sweep | 179 raw `.dash` literals across 39 files is the defect, not just the name. Project dir, each skills marker, and the env prefix each get exactly one defining constant. | +| 10 | Execution waits behind the two in-flight QA wishes | Rebranding ~100 files mid-QA would muddy genie-ui-dash and live-dev-loop evidence; see Dependencies. | + +## Dependencies + +**depends-on:** genie-ui-dash, live-dev-loop +**blocks:** none + +## Success Criteria + +- [ ] `rg -i syv` over the fork (excluding node_modules/dist/release/.git) hits ONLY: the LICENSE upstream copyright notice, the three `SYV_*` marketplace constants in `PluginsService.ts` (+ their direct tests/references), and the `.claude/settings.json` `syv-skills@syv-skills` entry. +- [ ] `grep -rE '\.dash([^a-zA-Z]|$)' src` → zero (catches `.dash/ports.json`, `.dash-skill.json`, `.dash-tmp`, all compound forms); each contract name (project dir, each marker, env prefix) has exactly one defining constant, all consumers import it. +- [ ] `grep -rE '(_*)DASH_[A-Z_]+' src` → zero — the `(_*)` prefix is load-bearing: `\bDASH_` misses `__DASH_ZDOTDIR` (ptyShellConfig.ts ×3) because the underscore defeats the word boundary. PTYs receive the same var set under `KHAL_*` (test asserts the injected-env key set). +- [ ] Launched app: menu/dock name **Khal**, `app.getPath('userData')` ends in `Khal`, fresh state on first launch, no read of legacy `.dash/`, Dash userData, or legacy skill markers. +- [ ] Packaged-path update check: `AutoUpdateService` on a private-repo/unreachable feed produces NO user-visible error (asserted by a new test); genuine update-available flow unchanged; feed target derives from the khal-os publish block. +- [ ] Release-notes and toast links point to `https://github.com/khal-os/genie-desktop/...`; dev-only `getDevVersion()` repointed. +- [ ] `package.json` `author` is exactly `Automagik Genie `; the app's commit trailer is exactly `Co-Authored-By: Claude via Automagik Genie ` — the existing trailer STRUCTURE is preserved (Felipe chose the identity, not a format change; dropping the Claude co-author line would be an unchosen behavior change). +- [ ] New-project default clone destination is `~/genie` (UI placeholder matches). +- [ ] `.github/workflows/build.yml` is green end-to-end at `productName: Khal`: codesign path `Khal.app`, `Khal-*` artifacts, release job unchanged in target (the fork). +- [ ] `make mac` publishes a `Khal--mac-arm64.dmg` release to khal-os/genie-desktop with NO command-line publish overrides. +- [ ] LICENSE contains both the syv MIT notice and the new copyright line. +- [ ] Full fork suite ≥ baseline (118 files / 1085 pass / 1 skip) and `pnpm run type-check` green after every group. +- [ ] Linux working copy remotes: `origin` = khal-os/genie-desktop only; ITERATE.md describes the new topology. + +## Execution Strategy + +### Wave 1 (parallel — disjoint files) + +| Group | Agent | Complexity | Model | Description | +|-------|-------|------------|-------|-------------| +| 1 | engineer | 3 (+1 CI/release, +1 multi-surface config, +1 release-model reconciliation) | engineer-standard / high | Core identity + CI workflow | +| 2 | engineer | 3 (+2 stateful update lifecycle, +1 no deterministic live test) | engineer-standard / high | Real update path + release feeds | + +### Wave 2 (sequential — overlapping file sets, after Wave 1 merges) + +| Group | Agent | Complexity | Model | Description | +|-------|-------|------------|-------|-------------| +| 3 | engineer | 4 (+2 stateful machine-contract rename, +1 multi-surface 39+ files, +1 prior-rework risk from compound literals) | engineer-complex / high | Machine contracts: `.khal` dir, skill markers, `KHAL_*` env | +| 4 | engineer | 2 (wide but mechanical; +1 subjective prompt tone) | engineer-standard / medium | Human-facing sweep: strings, trailer, links, `~/genie` | + +### Wave 3 (after Waves 1–2) + +| Group | Agent | Complexity | Model | Description | +|-------|-------|------------|-------|-------------| +| 5 | engineer | 1 (docs + scripted remote surgery) | engineer-trivial / low | Docs, LICENSE, remote detachment, ITERATE.md | +| 6 | engineer | 1 (asset drop-in) — **blocked-on-asset** | engineer-trivial / low | Icon swap once Felipe supplies art | + +## Execution Groups + +### Group 1: Core identity + CI workflow + +**Goal:** The app builds, signs, publishes, and passes CI as Khal (`ai.khal.genie`) targeting khal-os/genie-desktop, with package.json as the single source of publish truth. + +**Deliverables:** +1. `package.json`: `name: "khal"`, description rewritten, `author: "Automagik Genie "`, `build.appId: "ai.khal.genie"`, `build.productName: "Khal"`, `build.publish` → `{ owner: "khal-os", repo: "genie-desktop" }`. +2. `src/main/entry.ts`: `app.setName('Khal')` (comment: userData moves; fresh start intentional, no migration). +3. `Makefile`: remove the now-redundant `-c.publish.*` overrides and the syv comment; keep the macOS/GH_TOKEN guards. +4. `.github/workflows/build.yml`: `APP_PATH` → `release/mac-arm64/Khal.app`, artifact names `Khal-mac-arm64`/`Khal-linux-x64`/`Khal-windows-x64`, release job verified consistent (it releases on the checked-out repo — the fork — via `gh release create`; document in the workflow that package.json `publish` is authoritative for `make mac` and the workflow mirrors the same target). +5. Tests asserting old name/appId updated. + +**Acceptance Criteria:** +- [ ] `node -e` assertion on appId/productName/publish/author passes (see Validation). +- [ ] `rg -i syv Makefile .github/workflows/build.yml` → zero; the only syv in `package.json` is none (author replaced; no other fields carry it). +- [ ] `! grep -q 'Dash' .github/workflows/build.yml` AND all three `Khal-mac-arm64`/`Khal-linux-x64`/`Khal-windows-x64` artifact names present — the `Dash-*` artifact names are "dash" not "syv" and live outside `src/`, so no other group's gate would catch a leftover. +- [ ] `pnpm run type-check` and the full suite pass; workflow YAML parses (`node -e "require('js-yaml')..."` or `gh workflow view` after merge). + +**Validation:** +```bash +export PATH="$HOME/.hermes/node/bin:$PATH" && cd ~/prod/genie-ui-ab/dash-fork && node -e "const p=require('./package.json'); const b=p.build; if(b.appId!=='ai.khal.genie'||b.productName!=='Khal'||b.publish.owner!=='khal-os'||b.publish.repo!=='genie-desktop'||p.author!=='Automagik Genie ')process.exit(1)" && ! grep -riq syv package.json Makefile .github/workflows/build.yml && grep -q "setName('Khal')" src/main/entry.ts && grep -q 'Khal.app' .github/workflows/build.yml && ! grep -q 'Dash' .github/workflows/build.yml && grep -q 'Khal-mac-arm64' .github/workflows/build.yml && grep -q 'Khal-linux-x64' .github/workflows/build.yml && grep -q 'Khal-windows-x64' .github/workflows/build.yml && pnpm run type-check && pnpm test +``` + +**depends-on:** none + +--- + +### Group 2: Real update path + release feeds + +**Goal:** The packaged app's update machinery targets khal-os/genie-desktop and fails SILENTLY while the repo is private; every release-facing link points at the fork. + +**Deliverables:** +1. `src/main/services/AutoUpdateService.ts`: suppression boundary is LIFECYCLE-STATE-BASED, not error-content-based — suppress the `autoUpdate:error` renderer event while `state === 'checking'` (that's where a private/unreachable feed fails); keep surfacing errors during `downloading`/install. Content-classifying electron-updater error messages is fragile; state is testable. Also remove the per-check `console.error` noise (AutoUpdateService.ts:138). New test file covering: check-phase feed failure → no renderer error event; download-phase error → still surfaced; update-available path unchanged. (Ground truth, CORRECTED by ledger-verify 2026-07-23: the renderer has TWO `onAutoUpdateError` subscribers — `SettingsModal.tsx:799` resets spinner state only, but `Toast.tsx:79-86` shows a user-visible `toast.error` (10s, "Download manually" action) gated by `updateNotificationsEnabled`, which DEFAULTS TO TRUE. A private-feed check failure today therefore produces a visible error toast — suppressing at the main-process source while `checking` silences both subscribers at once, which is precisely why the boundary lives in `AutoUpdateService`, not the renderer. The feed URL derives from the electron-builder publish block — Group 1.) +2. `src/main/ipc/appIpc.ts`: dev-only `getDevVersion()` fetch → `https://api.github.com/repos/khal-os/genie-desktop/releases/latest`, `User-Agent: khal-app` (stays dev-only; existing local-version fallback preserved). +3. `src/renderer/utils/releaseNotes.ts`: `RELEASE_TAG_BASE` → `https://github.com/khal-os/genie-desktop/releases/tag`; `releaseNotes.test.ts` updated. +4. `src/renderer/components/ui/Toast.tsx`: latest-release link → khal-os/genie-desktop. + +**Acceptance Criteria:** +- [ ] `grep -rn 'syv-ai/dash' src/main/services/AutoUpdateService.ts src/main/ipc/appIpc.ts src/renderer/utils/releaseNotes.ts src/renderer/utils/__tests__/releaseNotes.test.ts src/renderer/components/ui/Toast.tsx` → zero (gate scoped to THIS group's files; `PortsSetupPrompt.ts` belongs to Group 4). +- [ ] New AutoUpdateService test fails if a feed-unavailable error reaches the renderer. + +**Validation:** +```bash +export PATH="$HOME/.hermes/node/bin:$PATH" && cd ~/prod/genie-ui-ab/dash-fork && ! grep -q 'syv-ai/dash' src/main/services/AutoUpdateService.ts src/main/ipc/appIpc.ts src/renderer/utils/releaseNotes.ts src/renderer/utils/__tests__/releaseNotes.test.ts src/renderer/components/ui/Toast.tsx && pnpm test -- releaseNotes && pnpm test -- AutoUpdate && pnpm run type-check +``` + +**depends-on:** none + +--- + +### Group 3: Machine contracts — `.khal` dir, skill markers, `KHAL_*` env + +**Goal:** Every machine-readable name the app writes or injects is khal-branded, each defined exactly once; legacy names are dead to the app (never read, never deleted, no shims). + +**Deliverables:** +1. `PROJECT_DIR_NAME = '.khal'` exported from one module importable by main, renderer, and `src/types` (respect existing layering); ALL compound uses (`.dash/ports.json`, `.dash/config.json`, `.dash/ports.local.json`, `.dash/setup.sh`, …) derive from it. The three duplicated per-file consts `const DASH_DIR = '.dash'` (`WorkspaceConfigService.ts:5`, `PortsConfigWatcher.ts:19`, `WorkspacePortsService.ts:4`) FOLD INTO this import — `DASH_DIR` is a JS path const, NOT an env var; do not create a `KHAL_DIR` alongside `PROJECT_DIR_NAME` (Decision 9: one constant per contract). +2. Skills markers in `SkillsService.ts`: `MARKER_FILENAME` → `.khal-skill.json`, `VERIFIED_CUSTOM_FILENAME` → `.khal-skill-checked.json`, tmp suffix → `.khal-tmp` — kept as their OWN constants (not folded into `PROJECT_DIR_NAME`); the legacy in-tree→central migration block is REMOVED (fresh start: legacy `.dash-skill.json` markers unrecognized; re-verify is idempotent — asserted by test). +3. `DASH_*` → `KHAL_*` across the full namespace, correctly partitioned (ledger-verify round-2 census, cited to source): + - **Injected PTY env vars (5)** — the actual env contract: `DASH_HOOK_PORT` (ptyManager.ts:336) and `DASH_TASK_ID`/`DASH_BRANCH`/`DASH_WORKTREE_PATH`/`DASH_PROJECT_PATH` (WorkspaceConfigService.ts:183-187) → `KHAL_*`, plus any others the sweep finds. Deliverable 4's test asserts THIS 5-key set (plus sweep finds) — not a larger phantom set. + - **Shell var (1)**: `__DASH_ZDOTDIR` → `__KHAL_ZDOTDIR` (ptyShellConfig.ts ×3). + - **JS identifier consts (8)** — renamed as identifiers for namespace hygiene, NOT env-contract changes: `DASH_DIR` (folds into `PROJECT_DIR_NAME`, deliverable 1), `DASH_HOOK_EVENTS`, `DASH_HOOK_ENDPOINTS`, `DASH_ENDPOINT_SET`, `DASH_DEFAULT_ATTRIBUTION`, `DASH_URL_FULL_RE`, `DASH_URL_SUBSTR_RE`, and `DASH_BASE64_DECODE_RE`. Locations: the hook/URL/endpoint/base64 consts live in hookSettingsMerge.ts; `DASH_DIR` per deliverable 1; `DASH_DEFAULT_ATTRIBUTION` is defined in SettingsModal.tsx:60-61 AND ptyHookSettings.ts:30-31 (Group 3 renames the identifier; Group 4 owns its VALUE change — sequential, no conflict). The gate regex catches `DASH_BASE64_DECODE_RE` via its `DASH_BASE` prefix — the digit-less `[A-Z_]+` class stops at `64`, which is fine for a must-be-zero gate. + Generated `setup.sh`/export-file content follows the env renames. No dual-export, no `KHAL_DIR` const (deliverable 1). +4. All fixtures/tests updated; a test asserts the PTY injected-env key set is `KHAL_*`-complete with no `DASH_*` remnant. + +**Acceptance Criteria:** +- [ ] `grep -rE '\.dash([^a-zA-Z]|$)' src` → zero. +- [ ] `grep -rE '(_*)DASH_[A-Z_]+' src` → zero (`(_*)` catches `__DASH_ZDOTDIR`; `\b` does not). +- [ ] Raw `'.khal'`-style literals exist ONLY in the defining modules (one per contract name). This has no scripted gate — the greps prove absence elsewhere, not single-definition — so the group's completion note MUST enumerate each defining module (project dir, each skills marker, env prefix) for the reviewer to verify. +- [ ] Opening a project creates `.khal/`; existing `.dash/` and legacy markers are never read or deleted. + +**Validation:** +```bash +export PATH="$HOME/.hermes/node/bin:$PATH" && cd ~/prod/genie-ui-ab/dash-fork && ! grep -rqE '\.dash([^a-zA-Z]|$)' src && ! grep -rqE '(_*)DASH_[A-Z_]+' src && pnpm test && pnpm run type-check +``` + +**depends-on:** group-1, group-2 + +--- + +### Group 4: Human-facing sweep — strings, trailer, links, `~/genie` + +**Goal:** No user-visible surface says Dash, credits syv outside LICENSE/marketplace, or clones into `~/Dash`. + +**Deliverables:** +1. Sweep the ~71 src files mentioning `Dash`: UI strings, titles, aria labels, product-describing comments. +2. `src/main/services/PortsSetupPrompt.ts`: canonical paragraph rewritten for Khal with `https://github.com/khal-os/genie-desktop` as the only URL to cite. +3. Commit co-author trailer: the full default attribution string in `SettingsModal.tsx:60-61` and `ptyHookSettings.ts:30-31` becomes exactly `\n\nCo-Authored-By: Claude via Automagik Genie ` — structure preserved, only the `via` identity replaced (label text updated to match); `https://syv.ai` links → `https://github.com/khal-os/genie-desktop`. +4. Default clone destination: `~/Dash` → `~/genie` (`gitIpc.ts:30`, `LocationStep.tsx:172` placeholder, related tests). Existing `~/Dash` clones untouched. +5. EXCLUDED from this sweep: the three `SYV_*` marketplace constants, `.claude/settings.json` `syv-skills@syv-skills`, LICENSE, and false positives (CSS `dashed`, generic `dashboard`, third-party identifiers) — each surviving `\bDash\b` match listed and justified in the completion note. + +**Acceptance Criteria:** +- [ ] `rg '\bDash\b' src` returns only the justified list from the completion note. +- [ ] `grep -rn 'syv' src` hits only `PluginsService.ts` marketplace constants (+ their tests). +- [ ] Full suite green. + +**Validation:** +```bash +export PATH="$HOME/.hermes/node/bin:$PATH" && cd ~/prod/genie-ui-ab/dash-fork && ! grep -rq 'syv-ai/dash' src && ! grep -rq 'dash@syv.ai' src && grep -q 'genie@namastex.ai' src/main/services/ptyHookSettings.ts && grep -rq "'genie'" src/main/ipc/gitIpc.ts && pnpm test && pnpm run type-check +``` + +**depends-on:** group-3 + +--- + +### Group 5: Docs, LICENSE, and upstream detachment + +**Goal:** Docs describe Khal; the syv remote is gone from working copies; the MIT notice survives; the kept marketplace is untouched. + +**Deliverables:** +1. README.md rewritten for Khal (what it is, `make desktop`, `make mac`, dev:web/dev:live loops). +2. GENIE.md syv/dash references updated. `.claude/settings.json` is NOT touched (its only syv content is the kept marketplace plugin id). +3. LICENSE: add the new copyright line, RETAIN the syv MIT notice beneath it. +4. Remote surgery, Linux working copy: remove the syv-ai remote, rename `khal` → `origin`, re-track local `main` to the fork. Executed on the box; commands recorded in the group note. +5. ITERATE.md: remote-trap paragraph rewritten for the single-remote topology. + +**Acceptance Criteria:** +- [ ] `git remote -v` shows only the fork, as `origin`. +- [ ] `grep -i syv LICENSE` shows exactly the retained MIT notice; `rg -i syv README.md GENIE.md ITERATE.md` → zero; `.claude/settings.json` unchanged (git diff empty for it). + +**Validation:** +```bash +cd ~/prod/genie-ui-ab/dash-fork && test "$(git remote | tr '\n' ' ')" = "origin " && git remote get-url origin | grep -q 'khal-os/genie-desktop' && grep -qi 'syv' LICENSE && ! grep -qi 'syv' README.md GENIE.md ITERATE.md +``` + +**depends-on:** group-1, group-4 + +--- + +### Group 6: Icon and visual assets — blocked-on-asset + +**Goal:** The app, DMG, and window icons are Khal art, not the Dash icon. + +**Deliverables:** +1. Replace `build/icon.icns`, `build/icon.svg`, `build/win/*` with Felipe-supplied Khal assets. +2. Verify packaged artifacts (dock icon, DMG volume icon, window icon on Linux) render the new art. + +**Acceptance Criteria:** +- [ ] `make mac` artifact shows Khal iconography end to end — **manual QA gate**: this group's acceptance is human-verified at packaging time; there is deliberately no scriptable assertion on binary art. +- [ ] No Dash-era binary assets remain under `build/` (checked by asset checksums differing from the Dash-era ones recorded in the group note). + +**Validation:** +```bash +cd ~/prod/genie-ui-ab/dash-fork && test -f build/icon.icns && test -f build/icon.svg +# Iconography itself is a manual QA gate — packaged-app visual check by Felipe. +``` + +**depends-on:** group-1 _(and Felipe-supplied assets — do not start until they exist)_ + +--- + +## QA Criteria + +_What must be verified on dev after merge. The QA agent tests each criterion._ + +- [ ] Launch the packaged (or `make desktop`) app: dock/menu says **Khal**; a FRESH userData dir named `Khal` is created; no read of Dash userData, `.dash/`, or legacy skill markers. +- [ ] Open a genie repo project: `.khal/` appears with ports/config files inside; a PTY spawned in a task shows `KHAL_*` env vars and zero `DASH_*`; terminals/PTY flows work end to end. +- [ ] New-project wizard defaults its clone destination under `~/genie`. +- [ ] A commit made from the app carries the exact trailer `Co-Authored-By: Claude via Automagik Genie `. +- [ ] With khal-os/genie-desktop private: no update-error toast/banner appears at launch or on manual check; release-notes/toast links open khal-os/genie-desktop. +- [ ] `make mac` on Felipe's Mac produces `Khal--mac-arm64.dmg`, signed, published as a khal-os/genie-desktop GitHub release; CI workflow green on fork `main`. +- [ ] Regression: full suite ≥ 118 files / 1085 pass; dev:web + dev:live loops still work post-rebrand. + +--- + +## Assumptions / Risks + +| Risk | Severity | Mitigation | +|------|----------|------------| +| Fresh start abandons existing app state (projects list, settings, roster history, skill-marker verification) | Low (accepted) | Felipe chose this knowingly; old dirs left in place, recoverable by hand. Skills re-verify once; idempotence asserted by test. | +| `.khal/` and "Khal" userData are DERIVED names | Medium | Decision 3 is a named veto point at plan review; single-constant design makes a rename one line. | +| `KHAL_*` rename breaks external scripts reading `DASH_*` from PTYs | Low (accepted) | Felipe's explicit pick, no dual-export; one-time cost. | +| Silencing check-phase update errors could mask genuine update failures | Medium | Suppression is lifecycle-state-based (`checking` only); `downloading`/install errors still surface; test pins the boundary. | +| NO auto-updates arrive at all while khal-os/genie-desktop is private | Low (accepted) | electron-updater cannot read private-repo releases without a token; `make mac` + manual install is the update channel until the repo goes public or a token is provisioned. Stated so dogfood users aren't surprised. | +| Rebrand churn collides with in-flight QA evidence | Medium | Wish-level depends-on: genie-ui-dash + live-dev-loop must reach SHIPPED first. | +| CI workflow has its own release path alongside `make mac` | Low | Group 1 aligns naming/paths and documents package.json `publish` as the shared target of truth; both paths land on the fork. | +| Upstream detachment means future syv fixes arrive only by manual cherry-pick | Low (accepted) | Felipe: "i dont intend to keep pulling updates from them." | +| Icon assets never arrive → Dash icon ships under Khal name | Medium | Group 6 is a formal blocked-on-asset gate; release QA criterion fails until art lands. | + +--- + +## Review Results + +_The read-only reviewer returns evidence; the invoking orchestrator appends a timestamped block here after plan, execution, and PR reviews._ + +### Plan review — 2026-07-22 — VERDICT: FIX-FIRST + +Reviewer (independent, read fork code directly; agent ab99c10ec1bf4f6c1): + +1. **[HIGH] Wrong update path targeted.** `getDevVersion()` in `appIpc.ts` is gated by `!app.isPackaged` — it never runs in a shipped app. The real production update path is `electron-updater` via `src/main/services/AutoUpdateService.ts`, which on a private-repo 404 fires `autoUpdater.on('error')` → visible `autoUpdate:error`. Group 2 must cover AutoUpdateService (repoint feed via the publish block, absorb private-repo failures silently, add the missing test). +2. **[HIGH] Group 3 gate too narrow.** Bare `'.dash'`/`".dash"` greps match only 32 of 179 occurrences; compound literals (`.dash/ports.json` ×55, `.dash/config.json` ×26 _[×27 per ledger-verify r3 recount]_, `.dash-skill.json` ×16, …) would survive a green gate. +3. **[HIGH] Group 2 gate un-passable at its wave** — `! grep 'syv-ai/dash' src` includes `PortsSetupPrompt.ts`, owned by Group 4 (Wave 2). Gate must be scoped to Group 2's files. +4. **[HIGH] Group 1 gate un-passable / smuggles a decision** — `package.json` `author` retains `*@syv.ai` emails; `! grep -riq syv package.json` either can't pass or silently strips authorship. Needs an explicit attribution decision. +5. **[HIGH] `DASH_*` env/protocol namespace unaddressed** — 14+ vars injected into every PTY and consumed by the hook contract; rename-vs-keep undecided, no gate. (Genie repo has zero `DASH_` refs — no cross-repo break.) +6. **[HIGH] `.github/workflows/build.yml` missing from the plan** — hardcodes `release/mac-arm64/Dash.app` (breaks at `productName: Khal`), `Dash-*` artifact names, and a second release path (`--publish never` + `gh release create`) that ignores the publish block Group 1 calls authoritative. +7. **[MEDIUM] `.dash-skill.json`/`.dash-tmp` are marker-filename constants** in `SkillsService.ts` with their own legacy-migration logic — folding them into one constant is wrong; fresh-start interaction undecided. +8. **[MEDIUM] `~/Dash` default clone dir** (`gitIpc.ts:30`, `LocationStep.tsx:172`) is a third data location renamed by the sweep with no veto point and no risk-table entry. +9. **[MEDIUM] Commit co-author trailer** `via Dash ` (`SettingsModal.tsx:61`, `ptyHookSettings.ts:31`) + `syv.ai` links unowned by any group; replacement identity undecided. +10. **[LOW] Marketplace allow-list too narrow** — `SYV_MARKETPLACE_NAME`, `SYV_PLUGIN_ID`, `.claude/settings.json` `syv-skills@syv-skills` must be explicitly kept and excluded from Group 5's sweep. +11. **[LOW] Wave table vs `depends-on` disagree for Group 3.** +12. **[NIT] Group 6 validation is `|| true`** — should say plainly it is a manual QA gate. + +Clean: appId change doesn't break signing (cert/team-based; entitlements carry no bundle id); no protocol handlers/deep links to rebrand; `${productName}` artifact templates auto-render; baseline numbers match. + +**Orchestrator action:** status → FIX-FIRST. Felipe supplied the three missing decisions (KHAL_* rename; attribution `Automagik Genie `; clone dir `~/genie`). Wish revised 2026-07-23 addressing all 12 findings: Group 2 rebuilt around AutoUpdateService; Group 3 widened to compound literals + skill-marker constants + env rename with hardened gates; Group 1 absorbed the CI workflow and author change; allow-lists widened; wave/deps aligned; Group 6 declared a manual QA gate. Re-review requested. + +### Plan re-review — 2026-07-23 — VERDICT: FIX-FIRST (all 12 prior findings resolved; 7 new soundness issues) + +Reviewer ran the wish's gates directly against the fork tree. Prior findings 1–12: all verified RESOLVED (`.dash` regex gate proven complete — 179/179 matched, zero false positives; author gate passable; allow-lists sufficient; `~/genie` clone-dir reasoning upgraded — plain git checkouts, no orphaning). + +New findings, all plan-document repairs: +1. **[MEDIUM]** `\bDASH_` gate false-negative on `__DASH_ZDOTDIR` (ptyShellConfig.ts ×3) — underscore defeats `\b`. Fix: `(_*)DASH_[A-Z_]+`. +2. **[MEDIUM]** `DASH_DIR` is a per-file JS path const, not an env var — listing `KHAL_DIR` in the env set steers a second constant next to `PROJECT_DIR_NAME`, violating Decision 9. (Same conflation, lower stakes: `DASH_DEFAULT_ATTRIBUTION`, `DASH_URL_*_RE` are identifiers, not env.) +3. **[MEDIUM]** Trailer criteria contradiction: replacing only `via Dash ` yields `Co-Authored-By: Claude via Automagik Genie `, which does not contain the asserted substring `Co-Authored-By: Automagik Genie <...>` — a literal-matching QA agent fails a correct implementation, or an engineer "fixes" it by dropping the Claude line (unchosen behavior change). Pin the exact final string. +4. **[LOW]** build.yml `Dash-*` artifact names ungated ("dash" not "syv", outside src/) — add explicit workflow gates. +5. **[LOW]** Update-error suppression should key on lifecycle state (`checking`) not error content; renderer already near-silent (`onAutoUpdateError` resets spinner only) — real deliverable is the missing test + state boundary + killing the per-check `console.error`. _[Reviewer's "near-silent renderer" claim CORRECTED by ledger-verify 2026-07-23: `Toast.tsx:79-86` is a second subscriber showing a default-on visible error toast — today's failure IS user-visible. The state-based-boundary prescription stands and silences both subscribers; Group 2's deliverable text carries the corrected ground truth.]_ +6. **[LOW]** "One defining constant per contract" has no scripted gate — completion note must enumerate defining modules. +7. **[NIT]** While private, electron-updater delivers NO updates at all (token-less) — record as accepted limitation. + +**Orchestrator action:** all 7 repaired in place 2026-07-23 (gate regexes hardened incl. validation commands; `KHAL_DIR` removed from env set with fold-into-`PROJECT_DIR_NAME` instruction; exact trailer string pinned in Decision-6 surfaces, Success Criteria, and QA Criteria; build.yml `Dash`/`Khal-*` gates added to Group 1; Group 2 rewritten state-based; defining-module enumeration required in Group 3 acceptance; no-updates-while-private risk row added). No new Felipe decisions were needed — the trailer fix preserves existing structure with his chosen identity. Third review round requested. + +### Plan re-review round 3 — 2026-07-23 — VERDICT: SHIP + +All 7 second-round findings verified repaired; the three empirically risky repairs re-executed against the live fork: +- `(_*)DASH_[A-Z_]+` catches all 3 `__DASH_ZDOTDIR` occurrences; `[A-Z]DASH_` probe → empty (zero false positives in the tree); token parity 85/85. All 14 distinct `DASH_` tokens map to exactly one bucket — the must-be-zero gate is achievable exactly when all deliverables complete. _[Reviewer's bucketing ("9 env → KHAL_*, 3 identifier renames") CORRECTED by ledger-verify round 2, 2026-07-23: true partition is 5 injected env vars + `__DASH_ZDOTDIR` + 8 JS identifiers; "DASH_BASE" was a phantom — the digit-less regex fragment of `DASH_BASE64_DECODE_RE`, which is now explicitly named in deliverable 3's identifier bucket. The gate-achievability conclusion stands: the zero-gate fires on every token including the `DASH_BASE` prefix of `DASH_BASE64_DECODE_RE`.]_ +- Trailer pin verified byte-for-byte: applying deliverable-3's replacement to both source files produces exactly the pinned string (Python equality → True). +- build.yml: exactly 4 `Dash` occurrences (lines 107/116/154/193), all owned by Group 1 deliverable 4; zero lowercase evaders; the `! grep -q 'Dash'` gate is passable precisely after Group 1's own renames. + +Non-blocking operational note (recorded, no action): per-group server-side merges to fork `main` each trigger build.yml; intermediate merges without a version bump show a transient-red release job (duplicate-tag guard) and half-branded artifacts, resolving green once all groups land with a bump — the QA criterion checks the green end-state. + +**Orchestrator action:** status → APPROVED (2026-07-23). Execution remains gated by wish-level depends-on (genie-ui-dash, live-dev-loop must reach SHIPPED); Group 6 additionally blocked-on-asset. Gating is DOCUMENTARY, per the wish-skill contract (the DAG lives in WISH.md, not task rows): the six seeded tasks are all `ready` in genie.db with no dependency rows, so the state machine will not stop an early claim — `work` and any dispatching orchestrator MUST consult this document's depends-on before checkout. + +### Ledger verification — 2026-07-23 — pm-ledger-verify (3 lenses, pre-commit) + +1 must-fix + 3 accuracy findings, all repaired in place before commit: +- **[MEDIUM, must-fix] False "near-silent renderer" ground truth** — `Toast.tsx:79-86` is a second `onAutoUpdateError` subscriber with a default-enabled visible error toast; today's private-feed failure IS user-visible. Corrected in Group 2 deliverable 1 and annotated on re-review finding 5. Design unchanged (main-process suppression silences both subscribers — the correction strengthens Group 2's justification). +- **[MEDIUM] Documentary-vs-enforced gating** — made explicit above. +- **[LOW ×2] "14+ env vars" → corrected** in Scope IN and Decision 7; INDEX.md wording softened to match actual verification depth per round. + +**Round 2 (post-repair re-verify):** 2 further must-fixes — round 1's "corrected census" (9 env / 4 identifiers) was ITSELF wrong. Verified partition of the 14 tokens: **5 injected PTY env vars** (`DASH_HOOK_PORT` ptyManager.ts:336; `DASH_TASK_ID`/`DASH_BRANCH`/`DASH_WORKTREE_PATH`/`DASH_PROJECT_PATH` WorkspaceConfigService.ts:183-187) + **`__DASH_ZDOTDIR`** + **8 JS identifiers** (`DASH_DIR`, `DASH_HOOK_EVENTS`, `DASH_HOOK_ENDPOINTS`, `DASH_ENDPOINT_SET`, `DASH_DEFAULT_ATTRIBUTION`, `DASH_URL_FULL_RE`, `DASH_URL_SUBSTR_RE`, `DASH_BASE64_DECODE_RE`); "DASH_BASE" exposed as a phantom regex fragment of `DASH_BASE64_DECODE_RE`. Group 3 deliverable 3 rewritten with the cited partition; deliverable 4's env-set test now specified against the real 5-key set; round-3 evidence bullet annotated. One INDEX summary-count drift (reported by all three lenses) also fixed. + +**Round 3 (post-repair re-verify):** 3 mustFix-flagged entries, all record accuracy: (a) "12 lens reports" was inflated 2× — each round runs exactly 3 lens agents (verified against the session's own workflow logs); this ledger now records per-round machine-reported figures only and maintains NO prose aggregate, because aggregates rot on every edit; (b) `DASH_DEFAULT_ATTRIBUTION` was falsely located in hookSettingsMerge.ts — true definitions are SettingsModal.tsx:60-61 + ptyHookSettings.ts:30-31 (deliverable 3 corrected); (c) LOW: `.dash/config.json` is ×27 not ×26 in the round-1 historical quote (annotated in place). Per-round record (runId · finding entries · mustFix-flagged): wf_bcec8f80 · 7 · 1; wf_802be98f · 5 · 2; wf_7ba79a1d · 6 · 3; wf_f16ace13 · 2 · **0** (gate passed — both remaining entries were LOW wording overreach in INDEX.md's "iterated to clean" phrase, corrected in this same commit). All flagged items repaired before commit; no aggregate claimed. + +--- + +## Files to Create/Modify + +``` +# fork (khal-os/genie-desktop) +package.json # name, description, author, appId, productName, publish +Makefile # drop publish overrides + syv comment +.github/workflows/build.yml # Khal.app path, Khal-* artifacts, release-job consistency +src/main/entry.ts # app.setName('Khal') +src/main/services/AutoUpdateService.ts # silent degrade while private + new test file +src/main/ipc/appIpc.ts # dev-only update-check URL + UA +src/renderer/utils/releaseNotes.ts # RELEASE_TAG_BASE +src/renderer/utils/__tests__/releaseNotes.test.ts +src/renderer/components/ui/Toast.tsx # latest-release link +src/main/services/PortsSetupPrompt.ts # canonical Khal paragraph +src/main/services/SkillsService.ts # .khal-skill markers, legacy migration removed +src/main/services/ptyHookSettings.ts # trailer identity + KHAL_* vars +src/renderer/components/settings/SettingsModal.tsx # trailer + links +src/main/ipc/gitIpc.ts # ~/genie clone default +src/renderer/components/newProject/LocationStep.tsx # placeholder +src//projectDirName.ts (new) # PROJECT_DIR_NAME = '.khal' +src/** (39 files) # .dash compound-literal sweep +src/** (~71 files) # Dash string sweep incl. tests +README.md, GENIE.md, ITERATE.md, LICENSE +build/icon.icns, build/icon.svg, build/win/* # blocked-on-asset + +# genie repo +.genie/wishes/khal-rebrand/WISH.md # this document +``` diff --git a/.genie/wishes/pr-2545-ultra-release-gate/FINALIZATION-VERIFICATION.md b/.genie/wishes/pr-2545-ultra-release-gate/FINALIZATION-VERIFICATION.md index d5b83e110..39eb6f64d 100644 --- a/.genie/wishes/pr-2545-ultra-release-gate/FINALIZATION-VERIFICATION.md +++ b/.genie/wishes/pr-2545-ultra-release-gate/FINALIZATION-VERIFICATION.md @@ -30,6 +30,19 @@ Result: **14 findings, 10 must-fix (two distinct defects reported across lenses) Non-must-fix corrections also applied: stale 2026-07-11 status sentence anchored as superseded, F22's dangling snapshot cross-reference removed, F40 updated for the merged PR, and the Darwin ARM64 gate re-labeled "PASS at `d9b0afbd` only" since no package was rebuilt at the successor head. +## Run 3 — 2026-07-24, wish closeout + +Inputs: uncommitted closeout edits (Status → DONE, success criteria checked, F01/F02/F05/F44 rows, INDEX entry); evidence bundle with the ledger diff, `gh` output for PR #2562, `main` ancestry and content proof, and the metrics-updater commit forensics. + +Result: **21 findings, 18 must-fix — all corrected before commit.** The two that mattered: + +| # | Must-fix defect | Correction | +|---|-----------------|------------| +| 1 | F01 closed against SHA `614219ee`, which is the *parent* of PR #2562's head — the gate is literally about the exact final SHA | Corrected to the real head `bd8c6128` across all three documents, and the supported-Bun claim grounded in CI's pinned Bun 1.3.11 | +| 2 | F02 recorded as CLOSED by "the maintainer approved and merged", when the PR carries one `COMMENTED` review and no `APPROVED` one, and the merging identity is the same one the commits were authored under — a self-merge presented as independent approval | F02 recorded as **NOT MET** in the ledger, the gate table, the INDEX entry, and the standing-conditions list, so it can never be cited as precedent | + +Also corrected: metrics-routine closure softened from "the routine was removed" to closed-by-observation (the owning account was never identified, so deletion is inferred from ten days of silence); the INDEX bullet that simultaneously said DONE and listed the same gates as pending; the validation snapshot and both supersession-map rows that still called F01/F02 open; the "without interruption" cadence claim (2026-07-10's metrics commit is human-authored); and the never-rebuilt release package added to the standing conditions. + ## Rerun instructions From a Claude Code session in this repository: diff --git a/.genie/wishes/pr-2545-ultra-release-gate/REVIEW-DISPOSITION.md b/.genie/wishes/pr-2545-ultra-release-gate/REVIEW-DISPOSITION.md index 6c739e761..e1dd14257 100644 --- a/.genie/wishes/pr-2545-ultra-release-gate/REVIEW-DISPOSITION.md +++ b/.genie/wishes/pr-2545-ultra-release-gate/REVIEW-DISPOSITION.md @@ -4,11 +4,11 @@ This ledger deduplicates every finding from the seven specialist lanes and the n | ID | Source findings | Origin | Disposition / owner | Acceptance evidence | Final evidence | |----|-----------------|--------|---------------------|---------------------|----------------| -| F01 | RH1 | External gate | E records; GitHub/human action required | Repository CI jobs run and pass on exact final SHA | **PENDING:** follow-up has no exact-final-SHA GitHub CI result yet | -| F02 | RH2 | External gate | OUT of code; human approval required | Independent APPROVED review on final SHA | **PENDING:** human APPROVED review must target the final follow-up SHA | +| F01 | RH1 | External gate | E records; GitHub/human action required | Repository CI jobs run and pass on exact final SHA | **CLOSED:** PR #2562 CI reported 15 checks passed / 0 failed on its exact head `bd8c6128`, with CI's Bun pinned at 1.3.11 against the repository's declared >=1.3.10 | +| F02 | RH2 | External gate | OUT of code; human approval required | Independent APPROVED review on final SHA | **NOT MET:** PR #2562 carries one `COMMENTED` review and no `APPROVED` review, and the maintainer who merged it (`namastex888`) is the same GitHub identity the branch commits were authored under, so the merge is not an independent approval. The work merged as `137dede8` on 2026-07-12 and reached `main` without this gate being satisfied; recorded here rather than waived | | F03 | RH3, DX4 | PR/exposed | E | Migration caveat in create and promotion/edit notes; hardening criteria mapped | `release-publish.yml` idempotently appends marker `genie-agent-sync-migration-v1` on create and existing-release promotion; plugin docs and the B1–B7 map describe the operator-run second hop | | F04 | RH4, SEC10, DX9 | Mixed | Current version-parity/build checks in B/D1/E; inherited verifier contract to stable-release wish | `release-payload-version.test.ts`, build staging verification, and source/extracted smoke own current parity; inherited publication/verifier redesign remains BLOCKING in the stable-release wish | -| F05 | RH5 | PR | E; **reopened operationally 2026-07-12** | Retired metrics state/README marker removed or superseding decision + schema test | Removed README marker and `.genie/agents/metrics-updater/**`; `release-docs.test.ts` **detects** resurrection but cannot prevent a direct-to-dev push. That happened: the external scheduled metrics-updater session pushed `d0ecc296` (2026-07-12) re-adding the state and README block after the PR merged, turning dev CI red against this gate. Re-retired in merge `af9fcad1`. **Open operational action: disable or re-scope the external metrics-updater routine — no repo commit can close this** | +| F05 | RH5 | PR | E; **reopened operationally 2026-07-12** | Retired metrics state/README marker removed or superseding decision + schema test | Removed README marker and `.genie/agents/metrics-updater/**`; `release-docs.test.ts` **detects** resurrection but cannot prevent a direct-to-dev push. That happened: the external scheduled metrics-updater session pushed `d0ecc296` (2026-07-12) re-adding the state and README block after the PR merged, turning dev CI red against this gate. Re-retired in merge `af9fcad1`, and again on `dev` in `6495bde4`. **CLOSED BY OBSERVATION 2026-07-24:** the scheduler committed daily at ~12:12 UTC from at least 2026-06-06 through 2026-07-14 (its final two firings, `ee50617f` and `4110962d`, were manually reverted in `fdffa95a` and `73ff992f`), then produced nothing for ten days. The claude.ai routine list queried from the maintainer's account is empty, but the owning account was never identified, so disablement is inferred from the silence rather than observed; nobody recorded performing the disable/re-scope action this row originally demanded. `dev` carries neither the state files nor the README marker, and `release-docs.test.ts` still fails closed on either | | F06 | RH6, AR1, DX1, DX6, NR9 | PR/exposed | B | Source copy and extracted plugin contain all valid in-root skills; parity test | Product payload: 23 skills and 19 reference files. Physical plugin mirror: 64 files / 209,675 logical bytes. `sync-plugin-skills`, `skills-lint`, `fresh-install-smoke`, and release inventory tests reject escapes/drift/missing metadata; Darwin ARM64 build/extraction parity passed at 21 MB | | F07 | RH7, DX8 | PR | E | Contributor/plugin docs match final CLI, skills, hooks, and agents | README/CLAUDE/plugin references now distinguish 23 plugin skills, 7 optional CLI role agents, 36 personal adapted skills, MCP launcher, H3/H4/H6, and orchestrator-only completion | | F08 | RH8 | PR | E | Build Tarballs PR filters cover all artifact inputs | PR paths now cover `src`, `skills`, `templates`, all `plugins`, metadata/lock/tsconfig, the build script and every direct/transitive TypeScript helper (including `skills-lint.ts`), both marketplaces, and the workflow; `release-docs.test.ts` derives and locks the helper graph | @@ -43,11 +43,11 @@ This ledger deduplicates every finding from the seven specialist lanes and the n | F37 | Panel detail: interrupted pending Omni request | PR | A | SIGTERM/interruption fixture expires/clears request and emits deny | Approval/dispatch tests trigger SIGTERM cleanup, expire only the owned pending row, remove listeners, and return a reasoned Codex deny | | F38 | Panel detail: review-thread setup/doctor/init/launch gaps | PR | D2 | Timeout, git-root fallback, merge fallback create/update, shellQuote and agent-choice tests | Setup/doctor/init/launch and codex-project-mcp suites cover deadlines, git/worktree root, fallback create/update/remove, quoting, preserved disabled plugin, and explicit agent choice | | F39 | Panel detail: hook/skill cold-fork and payload measurements | PR | A/B | Reduced hook count; before/after benchmark and payload inventory recorded | Nine→three commands; SessionStart 3→1; prompt 1→0. Original 21-run process medians 36.889–49.508 ms; retained H3 19.486 ms median/21.348 ms p95. Product payload: 23 skills + 19 reference files; physical plugin mirror: 64 files / 209,675 logical bytes | -| F40 | Panel detail: release notes/index/wish drift | PR | E | PR body/docs/wishes/index name exact final version, SHA, disposition, and still-blocked gates | WISH/index record merge `6f682e2b` from source `10ceb2c0`, follow-up target `dev`, IN_PROGRESS state, and F16–F18/F31 blockers. Repository-controlled docs review returned SHIP after fixes; PR [#2556](https://github.com/automagik-dev/genie/pull/2556) (since merged into `dev` at `a7c3c7ce`) named code commit `d9b0afbd`, this disposition, and the gates open at its creation; successor draft PR [#2562](https://github.com/automagik-dev/genie/pull/2562) carries the F42–F52 disposition and the currently open gates | +| F40 | Panel detail: release notes/index/wish drift | PR | E | PR body/docs/wishes/index name exact final version, SHA, disposition, and still-blocked gates | WISH/index record merge `6f682e2b` from source `10ceb2c0`, follow-up target `dev`, IN_PROGRESS state, and F16–F18/F31 blockers. Repository-controlled docs review returned SHIP after fixes; PR [#2556](https://github.com/automagik-dev/genie/pull/2556) (since merged into `dev` at `a7c3c7ce`) named code commit `d9b0afbd`, this disposition, and the gates open at its creation; successor PR [#2562](https://github.com/automagik-dev/genie/pull/2562) carried the F42–F52 disposition and merged as `137dede8`; the 2026-07-24 closeout records the final gate states | | F41 | Final replay integration audit (2026-07-11) | PR/exposed | Fixed in `d9b0afbd` | Exact version verification and Omni URL redaction correctness | Normalized exact version-token comparison (`v15.9.0` no longer satisfies expected `5.9.0`) and contextual URL query-pair redaction (a short value such as `tls=1` no longer becomes a global one-character replacement that corrupts status JSON); focused tests passed and the final aggregate includes them | | F42 | Final replay integration audit (2026-07-11) | New HIGH — architecture | **FIXED** in `09b368a5` (2026-07-12) | Durable steal-guard recovery protocol shared by the shell and TypeScript lifecycle acquirers | `install.sh` now reaps a foreign `.steal` guard iff its mtime is aged outside ±600s AND its owner is provably dead (empty/unparseable record counts as dead when aged; symlinked guard never reapable; `ps`-based liveness treats other-user EPERM as alive), matching the TS rule; fresh/live guards stay fail-closed; acquire budget 3→4. Independent review SHIP; the abandoned-guard permanent block was reproduced pre-fix. Accepted documented residuals: the doubly-suspended-process race (pre-existing at TS parity) and shell PID-reuse conservatism (no start-identity in shell; TS can still recover those) | | F43 | Uninstall second-loop re-review (2026-07-11) | New HIGH — architecture | **FIXED** in `8e147d87` (2026-07-12) | Uninstall retry authorization bound to original physical identity in the durable batch schema | Batch journal schema v2 binds per-kind physical identity (skill tree digests, workflow file+sidecar digests, link target, role `{digest, mode}`) at plan time; removal proceeds only when live identity matches, re-verified on the parked object; mismatches become durable `preserved` receipts that count toward clearing and can never regain authority; authentic v1 journals are discarded and re-recorded post-preview. Independent review SHIP; the pre-fix path-authority deletion was reproduced empirically in a disposable pre-fix worktree. Deliberate contract change: modified-in-place assets and changed role agents are now preserved (batch clears) instead of blocking completion. Accepted residuals: role plan-time double-read (benign false-preserve only), link readlink→unlink microsecond window, rules/home/CLI-symlink members path-authorized by design | -| F44 | Final-tree replay 2026-07-12, RH lane (HIGH) | Replay at `8e147d87` | Operational; see reopened F05 | External metrics-updater resurrection detected and re-retired | The external scheduler's direct-to-dev push (`d0ecc296`) turned dev CI red against the retirement gate; re-retired in merge `af9fcad1`, and dev independently re-retired again in `6495bde4`. The routine itself is still active — disabling/re-scoping it remains the open operational action recorded in F05 | +| F44 | Final-tree replay 2026-07-12, RH lane (HIGH) | Replay at `8e147d87` | Operational; see reopened F05 | External metrics-updater resurrection detected and re-retired | The external scheduler's direct-to-dev push (`d0ecc296`) turned dev CI red against the retirement gate; re-retired in merge `af9fcad1`, and dev independently re-retired again in `6495bde4`. **CLOSED BY OBSERVATION 2026-07-24:** no metrics commit since 2026-07-14 and an empty routine list on the maintainer's account; the owning account was never identified, so removal is inferred — see F05 | | F45 | Final-tree replay 2026-07-12, AR lane (MEDIUM) | Replay at `8e147d87` | **FIXED** in `d1814e40` | Shell recovers the empty/unparseable aged lock debris TS deliberately leaves | `lock_record_is_stale` treats an aged empty/unparseable record as dead-owner recoverable (fresh empty locks stay protected by the ± window, covering the TS mid-write race); the under-guard observed-equality re-verify is preserved. Reproduced pre-fix as a permanent `curl|bash` reinstall wedge | | F46 | Final-tree replay 2026-07-12, AR/CQ/QA/DX lanes (MEDIUM+3×LOW) | Replay at `8e147d87` | **FIXED** in `d1814e40` | Symlinked/non-regular guard and lock are never reaped by either acquirer, as the shared-protocol docblock states | TS `stealStaleLock` now lstat-refuses symlinks/non-regular nodes on BOTH the aged-guard reap and the under-guard lock steal (fail-closed contended), restoring parity with the shell's `! -L` and making the docblock truthful; regression tests pin both paths | | F47 | Final-tree replay 2026-07-12, CQ lane (LOW) | Replay at `8e147d87` | **FIXED** in `d1814e40` | No-trailing-newline owner records parsed identically by shell and TS | The `IFS= read -r x || [ -n "$x" ] || x=""` idiom at all four shell read sites preserves a torn-write record instead of clobbering it to empty and mis-reaping a live owner; verified under `set -euo pipefail` | @@ -67,8 +67,8 @@ The existing `.genie/wishes/agent-sync-hardening/WISH.md` remains authoritative | B2 sync lock | C, F26 | Supersede only after race, I/O, stale, and future-time tests pass | **SUPERSEDED:** lock ownership, contention, I/O, stale, and future-time fixtures implemented | | B4 user-data preservation | C, F13/F20/F25 | Supersede only after fixture and live read-only digest evidence | **SUPERSEDED:** ownership fixtures pass and the final post-test read-only live comparison on 2026-07-11 matched 36/36 skill digests and 14/14 agent TOML digests (F20) | | B5 fresh reinstall convergence | D1, F27 | Supersede only after every injected failure/identical-residue test passes | **SUPERSEDED:** auxiliary-tree digest/identical/failure/rollback fixtures and final local aggregate pass | -| B7 release caveat | E, F03/F40 | Supersede only after create/edit notes and final documentation evidence | **SUPERSEDED FOR REPOSITORY SCOPE:** create/promotion note and operator docs updated; docs cross-review returned SHIP. Exact-final-SHA CI and human approval remain external gates | -| Wave-2 plugin/skills/runtime items | B/C/D2, F06/F12–F15/F22/F30/F32/F33 | Close individually; never infer completion from version equality | **SUPPORTED LOCALLY:** each row names focused proof; repository-controlled docs/runtime/lifecycle cross-reviews returned SHIP (pre-replay-fix head) and the final aggregate passed at `d9b0afbd`. The live baseline comparison and merge-tree simulation closed on 2026-07-11; F01/F02, the final-tree specialist panel, and the open follow-ups F42/F43 remain | +| B7 release caveat | E, F03/F40 | Supersede only after create/edit notes and final documentation evidence | **SUPERSEDED FOR REPOSITORY SCOPE:** create/promotion note and operator docs updated; docs cross-review returned SHIP. Exact-final-SHA CI closed on `bd8c6128`; independent human approval (F02) was never obtained | +| Wave-2 plugin/skills/runtime items | B/C/D2, F06/F12–F15/F22/F30/F32/F33 | Close individually; never infer completion from version equality | **SUPPORTED LOCALLY:** each row names focused proof; repository-controlled docs/runtime/lifecycle cross-reviews returned SHIP (pre-replay-fix head) and the final aggregate passed at `d9b0afbd`. The live baseline comparison and merge-tree simulation closed on 2026-07-11; the final-tree panel ran at `8e147d87` and F42/F43 shipped fixed. Only F02 (independent approval) remains unmet | PR-remediation SHIP closes only non-blocking rows with concrete evidence. Rows F16–F18 and F31 continue to block stable promotion through the separate wish even if this PR-scope review later returns SHIP. @@ -84,4 +84,4 @@ Run on the follow-up worktree at head `e3abf2b5` (merge of `origin/dev` `e9059f4 - Live user assets — read-only comparison on 2026-07-12 matched 36/36 skill directory digests and 14/14 agent TOML digests; exactly 14 live TOMLs. One genie-owned product `review` skill (with `.genie-sync.json` ownership marker) appeared at `~/.agents/skills/review` on 2026-07-12 00:05 — consistent with an explicit live `genie update` run by the finishing session after the PR merge, before this session's work; it is a product asset, not user data, and every baseline digest is intact. - F42/F43 — both fixed and independently reviewed SHIP (see rows); F42's permanent block and F43's path-authority deletion were each reproduced pre-fix. -The local Bun is 1.3.9 while the repository declares Bun >=1.3.10, so exact-final-SHA supported-Bun GitHub CI (F01) and independent human approval (F02) remain the external gates — now targeting the successor PR's final head. The outer bare-Node hook/MCP startup probe is **BLOCKED-UPSTREAM** by the current Codex schema; installed hook hashes remain untrusted. The external metrics-updater routine remains active (F05/F44 operational action). None of these residual gates closes inherited F16–F18/F31, which still block stable promotion. +Closeout 2026-07-24: the local Bun is 1.3.9, but PR #2562's CI ran on the exact head `bd8c6128` with Bun 1.3.11 and reported 15 passed / 0 failed, closing F01. F02 remains **NOT MET** — no `APPROVED` review exists and the merge was performed by the identity the commits were authored under. The outer bare-Node hook/MCP startup probe is **BLOCKED-UPSTREAM** by the current Codex schema; installed hook hashes remain untrusted. The external metrics-updater has produced nothing since 2026-07-14 and is treated as removed by observation (F05/F44). None of these residual gates closes inherited F16–F18/F31, which still block stable promotion. diff --git a/.genie/wishes/pr-2545-ultra-release-gate/WISH.md b/.genie/wishes/pr-2545-ultra-release-gate/WISH.md index 034b62ee7..7f3383704 100644 --- a/.genie/wishes/pr-2545-ultra-release-gate/WISH.md +++ b/.genie/wishes/pr-2545-ultra-release-gate/WISH.md @@ -2,7 +2,7 @@ | Field | Value | |-------|-------| -| **Status** | IN_PROGRESS | +| **Status** | DONE | | **Slug** | `pr-2545-ultra-release-gate` | | **Date** | 2026-07-10 | | **Author** | Codex PM, from the seven-lane specialist-panel review requested by Felipe | @@ -10,11 +10,11 @@ | **Branch** | `fix/pr2545-ultra-gate` | | **Repos touched** | `automagik-dev/genie` | | **Design** | Seven Ultra specialist lanes plus native Codex review of PR #2545 at `42c11eeb`; follow-up rebased onto `dev` `5101fd35` | -| **Promotion state** | PR #2545 merged via `6f682e2b` from promoted source `10ceb2c0`; follow-up PR [#2556](https://github.com/automagik-dev/genie/pull/2556) merged into `dev` at `a7c3c7ce` (2026-07-11) and promoted to `main` via #2557; the F42/F43/replay work after it ships as successor draft PR [#2562](https://github.com/automagik-dev/genie/pull/2562) targeting `dev` | +| **Promotion state** | PR #2545 merged via `6f682e2b` from promoted source `10ceb2c0`; follow-up PR [#2556](https://github.com/automagik-dev/genie/pull/2556) merged into `dev` at `a7c3c7ce` (2026-07-11) and promoted to `main` via #2557; the F42/F43/replay work shipped as successor PR [#2562](https://github.com/automagik-dev/genie/pull/2562), merged into `dev` as `137dede8` on 2026-07-12 with 15 CI checks passing on its exact head `bd8c6128` (no independent `APPROVED` review — see F02), and has since been promoted to `main` | ## Summary -Make the Codex integration introduced by PR #2545 safe to install, update, review, and trust. The original PR has since merged; this branch is a bounded follow-up targeting `dev`, not a retroactive claim that the reviewed PR passed its blocked gates. Implementation has replaced the nine-command hook set, physicalized the skill payload, and hardened ownership/lifecycle paths. PR #2556 merged into `dev` at `a7c3c7ce` and reached `main` via promotion #2557. The successor work then closed both HIGH follow-ups — F42 (`09b368a5`) and F43 (`8e147d87`), each independently reviewed SHIP with the pre-fix failure reproduced — and the seven-lane specialist replay ran at `8e147d87`, the F43 head (4 SHIP / 3 FIX-FIRST lanes; 12 adversarially-verified findings, all code-fixable ones closed in fix loop 1 with a targeted independent SHIP re-review, the rest dispositioned as ledger rows F44–F52 — no full re-replay ran after the fixes, so the shipped head `e3abf2b5` is covered by the re-review, not a re-replay). At head `e3abf2b5` the full aggregate, merge simulation, and read-only live baseline comparison all pass. Exact-final-SHA supported-Bun CI, independent human approval on the successor PR, disabling the external metrics-updater routine, and stable-release authorization remain open. +Make the Codex integration introduced by PR #2545 safe to install, update, review, and trust. The original PR has since merged; this branch is a bounded follow-up targeting `dev`, not a retroactive claim that the reviewed PR passed its blocked gates. Implementation has replaced the nine-command hook set, physicalized the skill payload, and hardened ownership/lifecycle paths. PR #2556 merged into `dev` at `a7c3c7ce` and reached `main` via promotion #2557. The successor work then closed both HIGH follow-ups — F42 (`09b368a5`) and F43 (`8e147d87`), each independently reviewed SHIP with the pre-fix failure reproduced — and the seven-lane specialist replay ran at `8e147d87`, the F43 head (4 SHIP / 3 FIX-FIRST lanes; 12 adversarially-verified findings, all code-fixable ones closed in fix loop 1 with a targeted independent SHIP re-review, the rest dispositioned as ledger rows F44–F52 — no full re-replay ran after the fixes, so the shipped head `e3abf2b5` is covered by the re-review, not a re-replay). At head `e3abf2b5` the full aggregate, merge simulation, and read-only live baseline comparison all pass. See the 2026-07-24 closeout below: CI on the exact successor head passed and the metrics routine has gone silent, while independent human approval (F02) was never obtained and stable-release authorization remains blocked by the separate wish. The review ledger is [`REVIEW-DISPOSITION.md`](./REVIEW-DISPOSITION.md), the exact nine-command audit is [`HOOK-AUDIT.md`](./HOOK-AUDIT.md), and the read-only user-asset baseline is [`USER-ASSET-BASELINE.md`](./USER-ASSET-BASELINE.md). Every implementation claim must close against those artifacts rather than a prose-only “remaining issues” bucket. @@ -55,16 +55,18 @@ The review ledger is [`REVIEW-DISPOSITION.md`](./REVIEW-DISPOSITION.md), the exa ## Success Criteria -- [ ] The Codex hook manifest contains only the three approved behaviors; no retained hook installs software, mutates user/workspace configuration, or repeatedly injects free-form repository text. -- [ ] Canonical Codex hook fixtures prove valid JSON output, `apply_patch` decoding, matcher scoping, one-shot Omni evaluation, fail-closed timeout/denial, and portable command launching. -- [ ] A source marketplace install and an extracted release plugin both contain every declared Codex skill inside the plugin root, with valid Codex skill metadata. -- [ ] `genie update` and uninstall leave the 36 installed adapted user skills and modified role TOMLs byte-identical; Genie-owned clean artifacts still update/remove normally. -- [ ] Final-versus-prerelease ordering, identical/stale auxiliary trees, every PR-added auxiliary-tree swap stage, future-dated locks, lost Codex resume threads, blank JSONL output, and option-like prompts have regression coverage. +- [x] The Codex hook manifest contains only the three approved behaviors; no retained hook installs software, mutates user/workspace configuration, or repeatedly injects free-form repository text. +- [x] Canonical Codex hook fixtures prove valid JSON output, `apply_patch` decoding, matcher scoping, one-shot Omni evaluation, fail-closed timeout/denial, and portable command launching. +- [x] A source marketplace install and an extracted release plugin both contain every declared Codex skill inside the plugin root, with valid Codex skill metadata. +- [x] `genie update` and uninstall leave the 36 installed adapted user skills and modified role TOMLs byte-identical; Genie-owned clean artifacts still update/remove normally. +- [x] Final-versus-prerelease ordering, identical/stale auxiliary trees, every PR-added auxiliary-tree swap stage, future-dated locks, lost Codex resume threads, blank JSONL output, and option-like prompts have regression coverage. - [x] `bun run check`, `bun test`, targeted lifecycle/hook/package tests, and `git merge-tree --write-tree origin/dev HEAD` pass on the final follow-up head. -- [ ] The final review returns SHIP for the implemented scope; hooks remain untrusted until the installed cache is refreshed and the user reviews each retained hash in a new task. -- [ ] `REVIEW-DISPOSITION.md` has final evidence for every panel/native finding, and no inherited stable-release blocker is mislabeled as closed by this PR. +- [x] The final review returns SHIP for the implemented scope (the seven-lane replay ran at `8e147d87` with 4 SHIP / 3 FIX-FIRST and its loop-1 fixes closed under a targeted combined SHIP re-review — not a full re-replay of the shipped head); hooks remain untrusted until the installed cache is refreshed and the user reviews each retained hash in a new task. +- [x] `REVIEW-DISPOSITION.md` has final evidence for every panel/native finding, and no inherited stable-release blocker is mislabeled as closed by this PR. - [x] `USER-ASSET-BASELINE.md` is unchanged after isolated fixture tests and a final read-only live comparison. +> **Closeout note (2026-07-24):** the Success Criteria above are checked against the consolidated final evidence in [`REVIEW-DISPOSITION.md`](./REVIEW-DISPOSITION.md). The per-group Acceptance Criteria and the QA Criteria further below are left as the historical execution record. Two QA items were **not** met as written: independent human approval before the main merge (see F02) and a release package rebuilt at the successor head (the Darwin ARM64 artifact was only ever built at `d9b0afbd`). + ## Execution Strategy ### Wave 1 (parallel, exclusive files) @@ -357,9 +359,24 @@ Execution update 2026-07-11: PR #2545 merged via `6f682e2b` from source `10ceb2c Finalization update, late 2026-07-11 at code commit `d9b0afbd`: the last replay round, run against the pre-fix tree, returned FIX-FIRST with three targeted HIGH gaps. Exact version-token comparison and contextual URL query-pair redaction were fixed (F41), while the abandoned-`.steal`-guard aged-recovery attempt was deliberately rolled back as unsafe, restoring fail-closed behavior. Two HIGH architecture findings therefore remain documented-open as follow-ups: `.steal` debris availability (F42) and uninstall retry pathname-versus-physical-identity authorization (F43). The F41 fixes and the rollback were verified by focused tests and the full aggregate, not by a rerun replay; no specialist replay has audited the final tree. The finalization pass then re-verified the aggregate on the final head (`bun run check`: 1,382 pass, 1 pre-existing skip, 0 fail across 63 files), ran the final post-test read-only live baseline comparison (36/36 skills and 14/14 agent TOMLs byte-identical, zero sync markers), and ran the final merge simulation (`git merge-tree --write-tree origin/dev HEAD` exit 0, tree `39970285`, against `origin/dev` = `5101fd35`, `git diff --check` clean). -`WISH.md` stays IN_PROGRESS. As of that finalization pass (2026-07-11, since superseded by the successor update below): a seven-lane replay on the final tree had not run; exact-final-SHA supported-Bun GitHub CI and independent human approval were pending, and follow-ups F42/F43 were open. The outer bare-Node hook/MCP startup probe is **BLOCKED-UPSTREAM** by the current Codex schema, and the installed hook hashes remain untrusted; neither condition is represented as a local repository pass. Rows F16–F18/F31 remain stable-release blockers regardless of the follow-up verdict. +As of that finalization pass (2026-07-11, since superseded by the successor update below): a seven-lane replay on the final tree had not run; exact-final-SHA supported-Bun GitHub CI and independent human approval were pending, and follow-ups F42/F43 were open. The outer bare-Node hook/MCP startup probe is **BLOCKED-UPSTREAM** by the current Codex schema, and the installed hook hashes remain untrusted; neither condition is represented as a local repository pass. Rows F16–F18/F31 remain stable-release blockers regardless of the follow-up verdict. + +Successor update, 2026-07-12: PR #2556 merged into `dev` at `a7c3c7ce` and was promoted to `main` via #2557; the remaining work continues on `fix/pr2545-ultra-gate` as a successor PR. F42 was fixed in `09b368a5` and F43 in `8e147d87`, each with an independent SHIP review and an empirical pre-fix reproduction. The seven-lane replay then ran at `8e147d87`: repo-hygiene, QA, and dx-docs returned FIX-FIRST while architecture, security, code-quality, and performance returned SHIP; the 12 confirmed findings (rows F44–F52) were closed in fix loop 1 (`af9fcad1` re-retiring the externally resurrected metrics state, `d1814e40` closing the code findings) with a combined independent SHIP re-review — no full re-replay ran after the fixes. At `e3abf2b5` (dev `e9059f41` merged in) the aggregate passes 1,434 pass / 1 skip / 0 fail, the merge simulation is clean, and the live 36-skill/14-TOML baseline comparison still matches byte-for-byte. The external metrics-updater routine that broke dev CI (F44) was still active at the time of that update. + +## Closeout, 2026-07-24 + +This wish is **DONE**. Successor PR [#2562](https://github.com/automagik-dev/genie/pull/2562) passed **15 checks / 0 failures** on its exact head `bd8c6128`, under CI's pinned Bun 1.3.11 (the repository declares >=1.3.10) — closing **F01**. It was merged into `dev` as `137dede8` on 2026-07-12 and has since been promoted to `main`. **F02 is NOT met as written:** the PR carries one `COMMENTED` review and no `APPROVED` one, and the maintainer who merged it is the same GitHub identity the agent commits were authored under, so no independent approving review exists. The wish closes with that gate recorded as unmet rather than satisfied. Both HIGH follow-ups ship on `main`: F42's guard reaping in `install.sh` and F43's identity-bound uninstall batch in `src/genie-commands/uninstall.ts`. + +The F44/F05 operational action is closed **by observation, not by a confirmed disablement**: the external metrics-updater committed daily at ~12:12 UTC from at least 2026-06-06 through 2026-07-14 (all sampled firings authored by the `Claude` scheduler except 2026-07-10's, which is human-authored), its last two firings were manually reverted, and it has produced nothing in the ten days since. The claude.ai routine list queried from the maintainer's account is empty; the account that owned the routine was never identified, so its deletion is inferred from ten days of silence on a daily schedule, not observed directly. `.genie/agents/metrics-updater/**` and the README `` marker are absent from `dev`, and `release-docs.test.ts` continues to fail closed if either returns. Any future metrics automation must avoid both artifacts and open a PR rather than pushing directly to `dev`. If the routine turns out to still exist on an unqueried account, it can re-break `dev` exactly as before — the gate test is the only standing defense. + +Standing conditions that outlive this wish and are **not** closed by it: -Successor update, 2026-07-12: PR #2556 merged into `dev` at `a7c3c7ce` and was promoted to `main` via #2557; the remaining work continues on `fix/pr2545-ultra-gate` as a successor PR. F42 was fixed in `09b368a5` and F43 in `8e147d87`, each with an independent SHIP review and an empirical pre-fix reproduction. The seven-lane replay then ran at `8e147d87`: repo-hygiene, QA, and dx-docs returned FIX-FIRST while architecture, security, code-quality, and performance returned SHIP; the 12 confirmed findings (rows F44–F52) were closed in fix loop 1 (`af9fcad1` re-retiring the externally resurrected metrics state, `d1814e40` closing the code findings) with a combined independent SHIP re-review — no full re-replay ran after the fixes. The external metrics-updater routine that broke dev CI (F44) is still active and must be disabled or re-scoped by the operator. At `e3abf2b5` (dev `e9059f41` merged in) the aggregate passes 1,434 pass / 1 skip / 0 fail, the merge simulation is clean, and the live 36-skill/14-TOML baseline comparison still matches byte-for-byte. +- Installed hook hashes remain **untrusted**; trusting them requires the user to refresh the installed cache and review each retained hash in a separate task. No hook was trusted during this work. +- The outer bare-Node hook/MCP startup probe remains **BLOCKED-UPSTREAM** by the current Codex schema. +- Inherited rows **F16–F18 and F31** continue to block stable release through [`stable-release-security-gate`](../stable-release-security-gate/WISH.md). Nothing in this wish authorizes a stable release. +- **F02 was never satisfied**: no independent `APPROVED` review exists on #2562. Do not cite this wish as precedent that the independent-approval gate was met. +- No release package was rebuilt at the successor head; the Darwin ARM64 build/extraction evidence is scoped to `d9b0afbd`, and shipped code changed after it. +- Accepted, documented residuals: the doubly-suspended-process lock race (pre-existing, at shell/TS parity), shell PID-reuse conservatism, the role plan-time double-read (benign false-preserve only), and the symlink `readlink`→`unlink` window. ### Current release-gate evidence @@ -374,7 +391,9 @@ Successor update, 2026-07-12: PR #2556 merged into `dev` at `a7c3c7ce` and was p | Final specialist replay | Seven lanes ran at `8e147d87`: 4 SHIP / 3 FIX-FIRST; 12 verified findings closed in loop 1 (`af9fcad1`+`d1814e40`) or dispositioned (F44–F52); combined re-review of fixes SHIP; no full re-replay after fixes | CLOSED (loop 1 of 2) | | Live user assets | Read-only comparison 2026-07-12: 36/36 skills + 14/14 agent TOMLs byte-identical (one genie-owned product `review` skill sync'd by an explicit live update on 2026-07-12 00:05 — product asset, not user data) | PASS | | Mergeability | `git merge-tree --write-tree origin/dev HEAD` exit 0, tree `cc6ea068`, vs `origin/dev` `e9059f41`; `git diff --check` clean | PASS | -| External gates | Exact-final-SHA supported-Bun GitHub CI and independent human approval on the successor PR; disable/re-scope the external metrics-updater routine (F44) | PENDING | +| Exact-SHA CI (F01) | PR #2562: 15 checks passed / 0 failed on its exact head `bd8c6128`, CI Bun pinned at 1.3.11 (repo declares >=1.3.10) | PASS | +| Independent approval (F02) | #2562 has one `COMMENTED` review and no `APPROVED` review; merged by the same identity the commits were authored under | **NOT MET** — recorded, not waived | +| Metrics-updater routine (F44) | Daily ~12:12 UTC commits through 2026-07-14, then ten days of silence; routine list empty on the maintainer's account, owning account never identified | CLOSED by observation (deletion inferred) | | Stable promotion | Inherited F16–F18/F31 in `stable-release-security-gate` | BLOCKING | --- diff --git a/.github/workflows/build-tarballs.yml b/.github/workflows/build-tarballs.yml index df888f966..c014f7d49 100644 --- a/.github/workflows/build-tarballs.yml +++ b/.github/workflows/build-tarballs.yml @@ -37,6 +37,10 @@ on: description: 'Version stamped into the release payload' required: true type: string + channel: + description: 'Release channel; promotions receive a fresh immutable version' + required: true + type: string source_sha: description: 'Exact CI-approved source commit to build' required: true @@ -118,6 +122,7 @@ jobs: shell: bash env: INPUT_VERSION: ${{ inputs.version }} + INPUT_CHANNEL: ${{ inputs.channel }} run: | set -euo pipefail PACKAGE_VERSION="$(node -p "require('./package.json').version")" @@ -131,8 +136,15 @@ jobs: exit 1 } if [[ "${VERSION}" != "${PACKAGE_VERSION}" ]]; then - echo "::error::requested release version '${VERSION}' does not match source package '${PACKAGE_VERSION}'" - exit 1 + case "${INPUT_CHANNEL}" in + homolog|stable) + echo "Authenticated ${INPUT_CHANNEL} promotion stamps immutable version ${VERSION} over source ${PACKAGE_VERSION}" + ;; + *) + echo "::error::requested release version '${VERSION}' does not match source package '${PACKAGE_VERSION}'" + exit 1 + ;; + esac fi echo "version=${VERSION}" >> "$GITHUB_OUTPUT" echo "Resolved version: ${VERSION}" diff --git a/.github/workflows/release-publish.yml b/.github/workflows/release-publish.yml index 4f6250c40..806dbdd34 100644 --- a/.github/workflows/release-publish.yml +++ b/.github/workflows/release-publish.yml @@ -40,6 +40,18 @@ on: required: false type: string default: stable + source_sha: + description: 'Exact admitted source commit SHA' + required: true + type: string + source_branch: + description: 'Branch of the admitted source CI run' + required: true + type: string + source_ci_run_id: + description: 'Authorizing source CI run ID' + required: true + type: string draft: description: 'Create as draft (require manual promotion)?' required: false @@ -78,6 +90,10 @@ jobs: EXPECTED_STABLE_CALLER: automagik-dev/genie/.github/workflows/release.yml@refs/heads/main EXPECTED_AUTOMATED_CALLER: automagik-dev/genie/.github/workflows/version.yml@refs/heads/main INPUT_CHANNEL: ${{ inputs.channel }} + INPUT_VERSION: ${{ inputs.version }} + INPUT_SOURCE_SHA: ${{ inputs.source_sha }} + INPUT_SOURCE_BRANCH: ${{ inputs.source_branch }} + INPUT_SOURCE_CI_RUN_ID: ${{ inputs.source_ci_run_id }} run: | set -euo pipefail case "$INPUT_CHANNEL" in @@ -101,11 +117,804 @@ jobs: echo "::error ::release-caller.untrusted release-publish rejected caller ${CALLER_WORKFLOW_REF:-} (${CALLER_EVENT:-})" exit 1 fi + if [[ ! "$INPUT_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ || + ! "$INPUT_SOURCE_SHA" =~ ^[0-9a-f]{40}$ || + ! "$INPUT_SOURCE_CI_RUN_ID" =~ ^[0-9]+$ ]]; then + echo "::error ::release-caller.inputs release-publish received malformed release identity inputs" + exit 1 + fi + case "$INPUT_SOURCE_BRANCH" in main|homolog|dev) ;; *) exit 1 ;; esac + case "${INPUT_CHANNEL}:${INPUT_SOURCE_BRANCH}" in + stable:main|homolog:homolog|dev:dev) ;; + *) + echo "::error ::release-caller.inputs channel/source branch admission mismatch" + exit 1 + ;; + esac echo "Trusted release caller admitted at ${CALLER_SHA}" + prepare-delivery-evidence: + name: Build canonical delivery descriptors + needs: admit + runs-on: ubuntu-latest + timeout-minutes: 25 + permissions: + contents: read + attestations: read + outputs: + matrix: ${{ steps.build.outputs.matrix }} + dogfood_matrix: ${{ steps.build.outputs.dogfood_matrix }} + candidate_manifest_name: ${{ steps.build.outputs.candidate_manifest_name }} + candidate_manifest_sha256: ${{ steps.build.outputs.candidate_manifest_sha256 }} + previous_version: ${{ steps.build.outputs.previous_version }} + previous_manifest_sha256: ${{ steps.build.outputs.previous_manifest_sha256 }} + steps: + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5 + with: + # Release controls and gate code come from the admitted, protected + # main commit. Candidate bytes arrive only through signed artifacts. + ref: ${{ github.sha }} + persist-credentials: false + + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + with: + bun-version: 1.3.11 + + - name: Download all signed artifacts (sign-attest.yml) + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: genie-*-signed + merge-multiple: true + path: dist + + - name: Install cosign for effective-subject verification + uses: sigstore/cosign-installer@398d4b0eeef1380460a10c8013a76f728fb906ac # v3 + with: + cosign-release: 'v2.4.1' + + - name: Install slsa-verifier for effective-subject verification + uses: slsa-framework/slsa-verifier/actions/installer@ea584f4502babc6f60d9bc799dbbb13c1caa9ee6 # v2.7.1 + + - name: Generate retry-stable candidate manifests and descriptors + id: build + shell: bash + env: + VERSION: ${{ inputs.version }} + CHANNEL: ${{ inputs.channel }} + SOURCE_SHA: ${{ inputs.source_sha }} + SOURCE_BRANCH: ${{ inputs.source_branch }} + SOURCE_CI_RUN_ID: ${{ inputs.source_ci_run_id }} + CONTROL_SHA: ${{ github.sha }} + RELEASE_REPOSITORY: ${{ github.repository }} + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + # Promotions must endorse the already-published immutable subject + # bytes, not a functionally equivalent current-run rebuild. + bash scripts/materialize-release-subjects.sh + mkdir -p candidate-manifests descriptors + ( + cd candidate-manifests + bash "${GITHUB_WORKSPACE}/scripts/reconcile-channel-manifests.sh" + ) + case "$CHANNEL" in + stable) + SELECTED_MANIFEST=candidate-manifests/.well-known/latest.json + EVIDENCE_CHANNELS=(stable homolog dev) + ;; + homolog) + SELECTED_MANIFEST=candidate-manifests/.well-known/homolog.json + EVIDENCE_CHANNELS=(homolog dev) + ;; + dev) + SELECTED_MANIFEST=candidate-manifests/.well-known/dev.json + EVIDENCE_CHANNELS=(dev) + ;; + *) exit 1 ;; + esac + bun scripts/candidate-dogfood-matrix.ts \ + --manifest "$SELECTED_MANIFEST" \ + --artifact-dir dist \ + --output candidate-dogfood-matrix.json + DOGFOOD_MATRIX="$(tr -d '\n' < candidate-dogfood-matrix.json)" + CANDIDATE_MANIFEST_NAME="$(basename "$SELECTED_MANIFEST")" + CANDIDATE_MANIFEST_SHA256="$(jq -er ' + [.include[].manifestSha256] | unique | + if length == 1 then .[0] else error("candidate manifest digest disagreement") end + ' candidate-dogfood-matrix.json)" + [[ "$(jq -er '.include[0].version' candidate-dogfood-matrix.json)" == "$VERSION" ]] + [[ "$(jq -er '.include[0].channel' candidate-dogfood-matrix.json)" == "$CHANNEL" ]] + + mapfile -t MANIFEST_PLATFORMS < <(jq -er '.include[].platform' candidate-dogfood-matrix.json) + + # The cross-version parent is the exact stable manifest committed at + # the admitted control SHA. Download its immutable release triplets + # once, verify them without execution, and pass those same bytes to + # every native matrix entry. Historical releases are never + # retro-attested or assigned fabricated delivery descriptors. + [[ -f .well-known/latest.json && ! -L .well-known/latest.json && -s .well-known/latest.json ]] + PREVIOUS_VERSION="$(jq -er '.version | strings | select(test("^[0-9]+\\.[0-9]+\\.[0-9]+$"))' \ + .well-known/latest.json)" + [[ "$PREVIOUS_VERSION" != "$VERSION" ]] || { + echo "::error::dogfood requires a prior stable N distinct from candidate T" + exit 1 + } + [[ "$(printf '%s\n%s\n' "$PREVIOUS_VERSION" "$VERSION" | sort -V | head -n 1)" == "$PREVIOUS_VERSION" ]] || { + echo "::error::dogfood previous stable N must be older than candidate T" + exit 1 + } + mkdir previous-release + cp -- .well-known/latest.json previous-release/manifest.json + for platform in "${MANIFEST_PLATFORMS[@]}"; do + previous_tarball="genie-${PREVIOUS_VERSION}-${platform}.tar.gz" + for previous_name in \ + "$previous_tarball" \ + "${previous_tarball}.bundle" \ + "${previous_tarball}.intoto.jsonl"; do + gh release download "v${PREVIOUS_VERSION}" \ + --repo "$RELEASE_REPOSITORY" \ + --pattern "$previous_name" \ + --dir previous-release + [[ -f "previous-release/${previous_name}" && + ! -L "previous-release/${previous_name}" && + -s "previous-release/${previous_name}" ]] || { + echo "::error::prior stable input is missing or unsafe: ${previous_name}" + exit 1 + } + done + bash scripts/verify-release.sh --local "previous-release/${previous_tarball}" + done + bun scripts/candidate-dogfood-matrix.ts \ + --manifest previous-release/manifest.json \ + --artifact-dir previous-release \ + --output previous-dogfood-matrix.json + jq -e --slurpfile candidate candidate-dogfood-matrix.json ' + [.include[].platform] == [$candidate[0].include[].platform] + ' previous-dogfood-matrix.json >/dev/null + PREVIOUS_MANIFEST_SHA256="$(jq -er ' + [.include[].manifestSha256] | unique | + if length == 1 then .[0] else error("previous manifest digest disagreement") end + ' previous-dogfood-matrix.json)" + + MATRIX='[]' + for platform in "${MANIFEST_PLATFORMS[@]}"; do + tarball="dist/genie-${VERSION}-${platform}.tar.gz" + [[ -f "$tarball" && ! -L "$tarball" && -s "$tarball" ]] || { + echo "::error::missing physical signed tarball ${tarball}" + exit 1 + } + for evidence_channel in "${EVIDENCE_CHANNELS[@]}"; do + case "$evidence_channel" in + stable) manifest=candidate-manifests/.well-known/latest.json ;; + homolog) manifest=candidate-manifests/.well-known/homolog.json ;; + dev) manifest=candidate-manifests/.well-known/dev.json ;; + esac + descriptor="descriptors/genie-${VERSION}-${platform}.tar.gz.${evidence_channel}.delivery.json" + bun scripts/build-delivery-evidence.ts \ + --tarball "$tarball" \ + --manifest "$manifest" \ + --output "$descriptor" \ + --repository "$RELEASE_REPOSITORY" \ + --version "$VERSION" \ + --channel "$evidence_channel" \ + --platform-id "$platform" \ + --source-sha "$SOURCE_SHA" \ + --source-branch "$SOURCE_BRANCH" \ + --source-ci-run-id "$SOURCE_CI_RUN_ID" \ + --control-sha "$CONTROL_SHA" + MATRIX="$(jq -cn \ + --argjson current "$MATRIX" \ + --arg platform "$platform" \ + --arg channel "$evidence_channel" \ + '$current + [{platform: $platform, channel: $channel}]')" + done + done + { + echo "matrix={\"include\":${MATRIX}}" + echo "dogfood_matrix=${DOGFOOD_MATRIX}" + echo "candidate_manifest_name=${CANDIDATE_MANIFEST_NAME}" + echo "candidate_manifest_sha256=${CANDIDATE_MANIFEST_SHA256}" + echo "previous_version=${PREVIOUS_VERSION}" + echo "previous_manifest_sha256=${PREVIOUS_MANIFEST_SHA256}" + } >> "$GITHUB_OUTPUT" + + - name: Upload manifest-derived native dogfood matrix + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: codex-dogfood-candidate-matrix + path: candidate-dogfood-matrix.json + if-no-files-found: error + retention-days: 30 + + - name: Upload exact verified previous-stable inputs + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: codex-dogfood-previous-release + path: previous-release + if-no-files-found: error + retention-days: 30 + + - name: Upload exact candidate manifest bytes + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: delivery-candidate-manifests + path: candidate-manifests/.well-known/*.json + if-no-files-found: error + retention-days: 1 + + - name: Upload canonical descriptor subjects + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: delivery-descriptors + path: descriptors/*.delivery.json + if-no-files-found: error + retention-days: 1 + + attest-delivery-evidence: + name: Endorse ${{ matrix.channel }} / ${{ matrix.platform }} + needs: prepare-delivery-evidence + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + contents: read + id-token: write + attestations: write + strategy: + fail-fast: false + matrix: ${{ fromJSON(needs.prepare-delivery-evidence.outputs.matrix) }} + steps: + - name: Download canonical descriptor subjects + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-descriptors + path: descriptors + + - name: Attest exact descriptor bytes + id: endorse + uses: actions/attest@67422f5511b7ff725f4dbd6fb9bd2cd925c65a8d # v1.4.1 + with: + subject-path: descriptors/genie-${{ inputs.version }}-${{ matrix.platform }}.tar.gz.${{ matrix.channel }}.delivery.json + predicate-type: https://github.com/${{ github.repository }}/delivery-evidence/v1 + predicate-path: descriptors/genie-${{ inputs.version }}-${{ matrix.platform }}.tar.gz.${{ matrix.channel }}.delivery.json + + - name: Name the persisted Sigstore bundle beside its subject + shell: bash + env: + BUNDLE_PATH: ${{ steps.endorse.outputs.bundle-path }} + DESCRIPTOR: descriptors/genie-${{ inputs.version }}-${{ matrix.platform }}.tar.gz.${{ matrix.channel }}.delivery.json + run: | + set -euo pipefail + [[ -f "$BUNDLE_PATH" && ! -L "$BUNDLE_PATH" && -s "$BUNDLE_PATH" ]] + cp "$BUNDLE_PATH" "${DESCRIPTOR}.sigstore.json" + + - name: Upload persisted endorsement + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: delivery-endorsement-${{ matrix.channel }}-${{ matrix.platform }} + path: descriptors/*.sigstore.json + if-no-files-found: error + retention-days: 1 + + delivery-evidence-compatibility: + name: Verify production delivery evidence compatibility + needs: [admit, attest-delivery-evidence] + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5 + with: + ref: ${{ inputs.source_sha }} + persist-credentials: false + + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + with: + bun-version: 1.3.11 + + - name: Install the shipped production verifier dependencies + run: bun install --frozen-lockfile --ignore-scripts + + - name: Download canonical delivery descriptor subjects + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-descriptors + path: descriptors + + - name: Download all persisted endorsement bundles + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: delivery-endorsement-* + merge-multiple: true + path: descriptors + + - name: Download exact candidate manifests + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-candidate-manifests + path: candidate-manifests + + - name: Verify every pack with the production offline verifier + shell: bash + env: + CHANNEL: ${{ inputs.channel }} + run: | + set -euo pipefail + case "$CHANNEL" in + stable) EXPECTED_PACKS=12 ;; + homolog) EXPECTED_PACKS=8 ;; + dev) EXPECTED_PACKS=4 ;; + *) exit 1 ;; + esac + shopt -s nullglob + DESCRIPTORS=(descriptors/*.delivery.json) + [[ ${#DESCRIPTORS[@]} -eq "$EXPECTED_PACKS" ]] || { + echo "::error::expected ${EXPECTED_PACKS} delivery descriptors, found ${#DESCRIPTORS[@]}" + exit 1 + } + for descriptor in "${DESCRIPTORS[@]}"; do + case "$descriptor" in + *.stable.delivery.json) manifest=candidate-manifests/latest.json ;; + *.homolog.delivery.json) manifest=candidate-manifests/homolog.json ;; + *.dev.delivery.json) manifest=candidate-manifests/dev.json ;; + *) exit 1 ;; + esac + bundle="${descriptor}.sigstore.json" + [[ -f "$bundle" && ! -L "$bundle" && -s "$bundle" ]] + [[ -f "$manifest" && ! -L "$manifest" && -s "$manifest" ]] + bun scripts/verify-delivery-evidence-pack.ts \ + --descriptor "$descriptor" \ + --bundle "$bundle" \ + --manifest "$manifest" + done + + codex-native-dogfood: + name: Codex native dogfood / ${{ matrix.platform }} + needs: [prepare-delivery-evidence, attest-delivery-evidence, delivery-evidence-compatibility] + runs-on: ${{ matrix.runner }} + timeout-minutes: 30 + permissions: + contents: read + attestations: read + strategy: + fail-fast: false + matrix: ${{ fromJSON(needs.prepare-delivery-evidence.outputs.dogfood_matrix) }} + steps: + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5 + with: + # The harness and validators are trusted release controls. The exact + # candidate from source_sha is consumed only as a signed archive. + ref: ${{ github.sha }} + persist-credentials: false + + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + with: + bun-version: 1.3.11 + + - name: Install trusted harness dependencies + run: bun install --frozen-lockfile --ignore-scripts + + - name: Install pinned real Codex app-server + shell: bash + run: | + set -euo pipefail + npm install -g @openai/codex@0.144.1 + CODEX_PATH="$(node -e 'console.log(require("node:fs").realpathSync(process.argv[1]))' "$(command -v codex)")" + [[ "$("$CODEX_PATH" --version)" == "codex-cli 0.144.1" ]] + echo "GENIE_DOGFOOD_REAL_CODEX=${CODEX_PATH}" >> "$GITHUB_ENV" + + - name: Install cosign for previous-stable verification + uses: sigstore/cosign-installer@398d4b0eeef1380460a10c8013a76f728fb906ac # v3 + with: + cosign-release: 'v2.4.1' + + - name: Install slsa-verifier for previous-stable verification + uses: slsa-framework/slsa-verifier/actions/installer@ea584f4502babc6f60d9bc799dbbb13c1caa9ee6 # v2.7.1 + + - name: Download exact verified previous-stable inputs + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: codex-dogfood-previous-release + path: dogfood-entry/inputs/previous + + - name: Download exact candidate signed triplet + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: genie-${{ matrix.version }}-${{ matrix.platform }}-signed + path: dogfood-entry/inputs/candidate + + - name: Download exact candidate manifest + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-candidate-manifests + path: dogfood-entry/inputs/candidate/manifests + + - name: Download canonical candidate descriptors + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-descriptors + path: dogfood-entry/inputs/candidate/descriptors + + - name: Download exact candidate delivery endorsement + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-endorsement-${{ matrix.channel }}-${{ matrix.platform }} + path: dogfood-entry/inputs/candidate/descriptors + + - name: Resolve native execution adapter + id: adapter + shell: bash + env: + EXECUTION_KIND: ${{ matrix.execution }} + run: | + set -euo pipefail + case "$EXECUTION_KIND" in + host-native) + echo 'path=' >> "$GITHUB_OUTPUT" + ;; + alpine-container) + ADAPTER="${GITHUB_WORKSPACE}/scripts/run-musl-dogfood.sh" + [[ -f "$ADAPTER" && ! -L "$ADAPTER" && -x "$ADAPTER" ]] || { + echo "::error::pinned musl execution adapter is unavailable" + exit 1 + } + echo "path=${ADAPTER}" >> "$GITHUB_OUTPUT" + ;; + *) + echo "::error::manifest matrix selected unknown execution kind ${EXECUTION_KIND}" + exit 1 + ;; + esac + + - name: Run exact N to T lifecycle and emit reusable evidence + shell: bash + env: + PLATFORM: ${{ matrix.platform }} + PREVIOUS_VERSION: ${{ needs.prepare-delivery-evidence.outputs.previous_version }} + CANDIDATE_VERSION: ${{ matrix.version }} + CANDIDATE_CHANNEL: ${{ matrix.channel }} + CANDIDATE_MANIFEST_NAME: ${{ matrix.manifest }} + EXECUTION_ADAPTER: ${{ steps.adapter.outputs.path }} + run: | + set -euo pipefail + PREVIOUS_ARTIFACT="dogfood-entry/inputs/previous/genie-${PREVIOUS_VERSION}-${PLATFORM}.tar.gz" + CANDIDATE_ARTIFACT="dogfood-entry/inputs/candidate/genie-${CANDIDATE_VERSION}-${PLATFORM}.tar.gz" + CANDIDATE_DESCRIPTOR="dogfood-entry/inputs/candidate/descriptors/genie-${CANDIDATE_VERSION}-${PLATFORM}.tar.gz.${CANDIDATE_CHANNEL}.delivery.json" + EVIDENCE="dogfood-entry/codex-dogfood-${CANDIDATE_VERSION}-${PLATFORM}.md" + RUNNER_ARGS=( + --previous-artifact "$PREVIOUS_ARTIFACT" + --previous-manifest dogfood-entry/inputs/previous/manifest.json + --previous-bundle "${PREVIOUS_ARTIFACT}.bundle" + --previous-provenance "${PREVIOUS_ARTIFACT}.intoto.jsonl" + --candidate-artifact "$CANDIDATE_ARTIFACT" + --candidate-manifest "dogfood-entry/inputs/candidate/manifests/${CANDIDATE_MANIFEST_NAME}" + --candidate-descriptor "$CANDIDATE_DESCRIPTOR" + --candidate-bundle "${CANDIDATE_DESCRIPTOR}.sigstore.json" + --platform-id "$PLATFORM" + --output-evidence "$EVIDENCE" + ) + if [[ -n "$EXECUTION_ADAPTER" ]]; then + RUNNER_ARGS+=(--execution-adapter "$EXECUTION_ADAPTER") + fi + bun tests/support/codex-dogfood-entry-runner.ts "${RUNNER_ARGS[@]}" + bun scripts/validate-live-dogfood-evidence.ts \ + --file "$EVIDENCE" \ + --inputs-root dogfood-entry + + - name: Upload exact per-entry evidence and referenced inputs + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: codex-dogfood-evidence-${{ matrix.platform }} + path: dogfood-entry + if-no-files-found: error + retention-days: 30 + + codex-dogfood-completeness: + name: Require complete Codex native dogfood matrix + needs: [prepare-delivery-evidence, codex-native-dogfood] + if: ${{ always() }} + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: Reject failed, skipped, or unavailable native entries + shell: bash + env: + PREPARE_RESULT: ${{ needs.prepare-delivery-evidence.result }} + NATIVE_RESULT: ${{ needs.codex-native-dogfood.result }} + EXPECTED_MATRIX: ${{ needs.prepare-delivery-evidence.outputs.dogfood_matrix }} + EXPECTED_MANIFEST_SHA256: ${{ needs.prepare-delivery-evidence.outputs.candidate_manifest_sha256 }} + run: | + set -euo pipefail + [[ "$PREPARE_RESULT" == success && "$NATIVE_RESULT" == success ]] || { + echo "::error::Codex dogfood is incomplete (prepare=${PREPARE_RESULT}, native=${NATIVE_RESULT})" + exit 1 + } + [[ -n "$EXPECTED_MATRIX" && -n "$EXPECTED_MANIFEST_SHA256" ]] || { + echo "::error::Codex dogfood candidate identity is unavailable" + exit 1 + } + + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5 + with: + ref: ${{ github.sha }} + persist-credentials: false + + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + with: + bun-version: 1.3.11 + + - name: Download manifest-derived candidate matrix + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: codex-dogfood-candidate-matrix + path: aggregate + + - name: Download every native entry + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: codex-dogfood-evidence-* + path: aggregate/entries + + - name: Validate one-to-one evidence and persist candidate identity + shell: bash + env: + EXPECTED_VERSION: ${{ inputs.version }} + EXPECTED_CHANNEL: ${{ inputs.channel }} + EXPECTED_SOURCE_SHA: ${{ inputs.source_sha }} + EXPECTED_MATRIX: ${{ needs.prepare-delivery-evidence.outputs.dogfood_matrix }} + EXPECTED_MANIFEST_SHA256: ${{ needs.prepare-delivery-evidence.outputs.candidate_manifest_sha256 }} + run: | + set -euo pipefail + [[ "$(tr -d '\n' < aggregate/candidate-dogfood-matrix.json)" == "$EXPECTED_MATRIX" ]] || { + echo "::error::downloaded candidate matrix differs from the matrix used to schedule native jobs" + exit 1 + } + bun scripts/validate-dogfood-matrix-evidence.ts \ + --matrix aggregate/candidate-dogfood-matrix.json \ + --evidence-dir aggregate/entries \ + --version "$EXPECTED_VERSION" \ + --channel "$EXPECTED_CHANNEL" \ + --source-sha "$EXPECTED_SOURCE_SHA" \ + --candidate-manifest-sha256 "$EXPECTED_MANIFEST_SHA256" \ + --output aggregate/codex-dogfood-completeness.json + + - name: Upload complete Codex dogfood evidence + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: codex-dogfood-completeness + path: aggregate/codex-dogfood-completeness.json + if-no-files-found: error + retention-days: 90 + + stable-release-security-gate: + name: Independent candidate security gate + needs: [prepare-delivery-evidence, attest-delivery-evidence, delivery-evidence-compatibility] + if: ${{ always() }} + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read + attestations: read + steps: + - name: Reject missing prerequisite evidence + shell: bash + env: + PREPARE_RESULT: ${{ needs.prepare-delivery-evidence.result }} + ATTEST_RESULT: ${{ needs.attest-delivery-evidence.result }} + COMPATIBILITY_RESULT: ${{ needs.delivery-evidence-compatibility.result }} + run: | + set -euo pipefail + [[ "$PREPARE_RESULT" == success && + "$ATTEST_RESULT" == success && + "$COMPATIBILITY_RESULT" == success ]] || { + echo "::error::security gate prerequisites are incomplete" + exit 1 + } + + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5 + with: + # Security policy is evaluated from protected main, independently of + # the candidate payload and independently of the dogfood result. + ref: ${{ github.sha }} + persist-credentials: false + + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + with: + bun-version: 1.3.11 + + - name: Install trusted security-gate dependencies + run: bun install --frozen-lockfile --ignore-scripts + + - name: Install cosign for exact candidate verification + uses: sigstore/cosign-installer@398d4b0eeef1380460a10c8013a76f728fb906ac # v3 + with: + cosign-release: 'v2.4.1' + + - name: Install slsa-verifier for exact candidate verification + uses: slsa-framework/slsa-verifier/actions/installer@ea584f4502babc6f60d9bc799dbbb13c1caa9ee6 # v2.7.1 + + - name: Download all exact signed candidate artifacts + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: genie-*-signed + merge-multiple: true + path: security-gate/dist + + - name: Download exact candidate manifests + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-candidate-manifests + path: security-gate/manifests + + - name: Download manifest-derived candidate matrix + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: codex-dogfood-candidate-matrix + path: security-gate + + - name: Verify exact candidate identity with independent release controls + shell: bash + env: + VERSION: ${{ inputs.version }} + CHANNEL: ${{ inputs.channel }} + SOURCE_SHA: ${{ inputs.source_sha }} + SOURCE_BRANCH: ${{ inputs.source_branch }} + SOURCE_CI_RUN_ID: ${{ inputs.source_ci_run_id }} + CONTROL_SHA: ${{ github.sha }} + RUN_ID: ${{ github.run_id }} + RUN_ATTEMPT: ${{ github.run_attempt }} + GH_TOKEN: ${{ github.token }} + RELEASE_REPOSITORY: ${{ github.repository }} + CANDIDATE_MANIFEST_NAME: ${{ needs.prepare-delivery-evidence.outputs.candidate_manifest_name }} + EXPECTED_MANIFEST_SHA256: ${{ needs.prepare-delivery-evidence.outputs.candidate_manifest_sha256 }} + run: | + set -euo pipefail + MANIFEST="security-gate/manifests/${CANDIDATE_MANIFEST_NAME}" + [[ -f "$MANIFEST" && ! -L "$MANIFEST" && -s "$MANIFEST" ]] + [[ "$(sha256sum "$MANIFEST" | cut -d ' ' -f 1)" == "$EXPECTED_MANIFEST_SHA256" ]] + bun scripts/candidate-dogfood-matrix.ts \ + --manifest "$MANIFEST" \ + --artifact-dir security-gate/dist \ + --output security-gate/rederived-matrix.json + cmp -- security-gate/candidate-dogfood-matrix.json security-gate/rederived-matrix.json + jq -e --arg version "$VERSION" --arg channel "$CHANNEL" --arg digest "$EXPECTED_MANIFEST_SHA256" ' + all(.include[]; + .version == $version and + .channel == $channel and + .manifestSha256 == $digest) + ' security-gate/rederived-matrix.json >/dev/null + mkdir -p security-gate/verified + while IFS= read -r artifact; do + artifact_path="security-gate/dist/${artifact}" + artifact_sha256="$(jq -er --arg artifact "$artifact" ' + [.include[] | select(.artifact == $artifact) | .artifactSha256] | + if length == 1 then .[0] else error("candidate artifact is not unique") end + ' security-gate/rederived-matrix.json)" + [[ "$(sha256sum "$artifact_path" | cut -d ' ' -f 1)" == "$artifact_sha256" ]] + bash scripts/verify-release.sh --local "$artifact_path" + verified_provenance="${RUNNER_TEMP}/${artifact}.verified-provenance.json" + slsa-verifier verify-artifact "$artifact_path" \ + --provenance-path "${artifact_path}.intoto.jsonl" \ + --source-uri "github.com/${RELEASE_REPOSITORY}" \ + --source-branch main \ + --print-provenance > "$verified_provenance" + generic_identity="${RUNNER_TEMP}/${artifact}.generic-identity.json" + ARTIFACT_SHA256="$artifact_sha256" \ + bash scripts/release-generic-provenance.sh verify-exact-subject "$verified_provenance" \ + > "$generic_identity" + native_result="${RUNNER_TEMP}/${artifact}.native-result.json" + gh attestation verify "$artifact_path" \ + --repo "$RELEASE_REPOSITORY" \ + --predicate-type "https://github.com/${RELEASE_REPOSITORY}/release-tarballs/v1" \ + --cert-identity "https://github.com/${RELEASE_REPOSITORY}/.github/workflows/sign-attest.yml@refs/heads/main" \ + --source-ref refs/heads/main \ + --source-digest "$CONTROL_SHA" \ + --signer-digest "$CONTROL_SHA" \ + --format json > "$native_result" + native_identity="${RUNNER_TEMP}/${artifact}.native-identity.json" + ARTIFACT_SHA256="$artifact_sha256" \ + bash scripts/release-native-predicate.sh verify-exact-subject "$native_result" \ + > "$native_identity" + jq -cn \ + --arg artifact "$artifact" \ + --slurpfile generic "$generic_identity" \ + --slurpfile native "$native_identity" \ + --arg sourceSha "$SOURCE_SHA" \ + --arg sourceBranch "$SOURCE_BRANCH" \ + --arg sourceCiRunId "$SOURCE_CI_RUN_ID" \ + --arg controlSha "$CONTROL_SHA" \ + ' + ($generic[0] | [ + .sourceSha == $sourceSha, + .sourceBranch == $sourceBranch, + .sourceCiRunId == $sourceCiRunId, + .controlSha == $controlSha + ] | all) or error("generic provenance identity disagreement") | + ($native[0] | [ + .sourceSha == $sourceSha, + .sourceBranch == $sourceBranch, + .sourceCiRunId == $sourceCiRunId, + .controlSha == $controlSha + ] | all) or error("native predicate identity disagreement") | + ($generic[0].artifactSha256 == $native[0].artifactSha256) or + error("generic/native artifact digest disagreement") | + { + artifact: $artifact, + artifactSha256: $native[0].artifactSha256, + generic: $generic[0], + native: $native[0] + } + ' > "security-gate/verified/${artifact}.json" + done < <(jq -er '.include[].artifact' security-gate/rederived-matrix.json) + bun test \ + scripts/release-guard.test.ts \ + scripts/install-swap.test.ts \ + scripts/verify-release.test.ts \ + scripts/build-delivery-evidence.test.ts \ + scripts/verify-delivery-evidence-pack.test.ts \ + scripts/candidate-dogfood-matrix.test.ts \ + scripts/release-generic-provenance.test.ts \ + scripts/release-native-predicate.test.ts + jq -cs \ + --slurpfile matrix security-gate/rederived-matrix.json \ + --arg candidateManifestSha256 "$EXPECTED_MANIFEST_SHA256" \ + --arg matrixSha256 "$(sha256sum security-gate/rederived-matrix.json | cut -d ' ' -f 1)" \ + ' + sort_by(.artifact) as $entries | + if ($entries | length) != ($matrix[0].include | length) then + error("verified security entry count differs from candidate matrix") + else + { + schemaVersion: 1, + kind: "stable-release-security-gate", + version: ([$entries[].native.version] | unique | + if length == 1 then .[0] else error("verified version disagreement") end), + channel: ([$entries[].native.channel] | unique | + if length == 1 then .[0] else error("verified channel disagreement") end), + sourceSha: ([$entries[].native.sourceSha] | unique | + if length == 1 then .[0] else error("verified source SHA disagreement") end), + sourceBranch: ([$entries[].native.sourceBranch] | unique | + if length == 1 then .[0] else error("verified source branch disagreement") end), + sourceCiRunId: ([$entries[].native.sourceCiRunId] | unique | + if length == 1 then .[0] else error("verified source run disagreement") end), + controlSha: ([$entries[].native.controlSha] | unique | + if length == 1 then .[0] else error("verified control SHA disagreement") end), + invocationId: ([$entries[].native.invocationId] | unique | + if length == 1 then .[0] else error("verified signer run disagreement") end), + candidateManifestSha256: $candidateManifestSha256, + matrixSha256: $matrixSha256, + entries: $entries + } + end + ' security-gate/verified/*.json > security-gate/stable-release-security-gate.json + + - name: Upload independent security evidence + uses: actions/upload-artifact@b7c4aadc2c921a8ff42c1c6b0e8950fc060e7c7e # v4 + with: + name: stable-release-security-gate + path: security-gate/stable-release-security-gate.json + if-no-files-found: error + retention-days: 90 + publish: name: Publish to GitHub Releases - needs: admit + needs: + - admit + - attest-delivery-evidence + - delivery-evidence-compatibility + - codex-dogfood-completeness + - stable-release-security-gate + if: >- + ${{ + always() && + needs.admit.result == 'success' && + needs.attest-delivery-evidence.result == 'success' && + needs.delivery-evidence-compatibility.result == 'success' && + needs.codex-dogfood-completeness.result == 'success' && + needs.stable-release-security-gate.result == 'success' + }} runs-on: ubuntu-latest timeout-minutes: 30 permissions: @@ -123,6 +932,25 @@ jobs: merge-multiple: true path: dist + - name: Download canonical delivery descriptor subjects + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-descriptors + path: dist + + - name: Download all delivery endorsement bundles + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: delivery-endorsement-* + merge-multiple: true + path: dist + + - name: Download exact candidate manifests for inventory binding + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-candidate-manifests + path: ${{ runner.temp }}/candidate-manifests + - name: Resolve version + channel + asset inventory id: meta shell: bash @@ -161,7 +989,7 @@ jobs: echo "channel=${CHANNEL}" >> "$GITHUB_OUTPUT" echo "Resolved: VERSION=${VERSION} CHANNEL=${CHANNEL}" echo "Tarballs: ${#TARBALLS[@]}" - echo "=== dist/ inventory (expect 12 files: 4× tarball + 4× bundle + 4× intoto.jsonl) ===" + echo "=== dist/ exact inventory: 12 release assets plus selected-channel delivery evidence fanout ===" ls -la - name: Install cosign for published-asset reuse verification @@ -172,34 +1000,30 @@ jobs: - name: Install slsa-verifier for published-asset reuse verification uses: slsa-framework/slsa-verifier/actions/installer@ea584f4502babc6f60d9bc799dbbb13c1caa9ee6 # v2.7.1 - - name: Prepare draft, reconcile exact assets, and finalize non-latest + - name: Prepare draft and reconcile exact endorsed assets env: GH_TOKEN: ${{ github.token }} VERSION: ${{ steps.meta.outputs.version }} CHANNEL: ${{ steps.meta.outputs.channel }} DRAFT: ${{ inputs.draft }} RELEASE_REPOSITORY: ${{ github.repository }} + CANDIDATE_MANIFEST_DIR: ${{ runner.temp }}/candidate-manifests shell: bash run: | set -euo pipefail - # New releases remain draft/non-latest until all 12 exact assets are - # remotely verified. Existing complete releases are reused byte for + # New releases remain draft/non-latest until every exact asset is + # remotely verified. Finalize then proves repository immutability + # before the endorsed manifest bytes may be reconciled. + # Existing complete releases are reused byte for # byte; partial published releases and mismatches fail closed. The # repository setting is an externally verified cutover prerequisite: # GITHUB_TOKEN cannot read the Administration API that exposes it. bash scripts/reconcile-release-note.sh prepare bash scripts/reconcile-release-assets.sh - bash scripts/reconcile-release-note.sh finalize - if [[ "$DRAFT" == false ]]; then - bash scripts/release-immutability.sh release - # Re-verify the now-locked remote bytes and all three provenance - # channels. This closes the draft verification→publish TOCTOU. - bash scripts/reconcile-release-assets.sh - fi manifests: name: Advance authoritative channel manifests - needs: publish + needs: finalize if: ${{ !inputs.draft }} runs-on: ubuntu-latest timeout-minutes: 10 @@ -248,6 +1072,12 @@ jobs: token: ${{ secrets.RELEASE_MANIFESTS_TOKEN }} persist-credentials: true + - name: Download the exact pre-endorsed candidate manifest bytes + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-candidate-manifests + path: ${{ runner.temp }}/candidate-manifests + # --------------------------------------------------------------------- # Per-channel manifest pointers — committed to main so install.sh + # `genie update` can resolve the current version for each channel via: @@ -279,7 +1109,7 @@ jobs: # writer can cause a non-fast-forward, never a lost/downgraded update. MANIFEST_RECONCILER="${RUNNER_TEMP}/reconcile-channel-manifests-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}.sh" cp scripts/reconcile-channel-manifests.sh "$MANIFEST_RECONCILER" - RELEASED_AT="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + CANDIDATE_MANIFEST_DIR="${RUNNER_TEMP}/candidate-manifests" manifests_reconciled=false for attempt in 1 2 3 4 5; do @@ -289,7 +1119,7 @@ jobs: # Stable/homolog fan out to downstream pointers, but each file # moves independently and monotonically. An older delayed release # may advance stable while preserving newer downstream pointers. - RELEASE_REPOSITORY="${{ github.repository }}" RELEASED_AT="$RELEASED_AT" \ + RELEASE_REPOSITORY="${{ github.repository }}" CANDIDATE_MANIFEST_DIR="$CANDIDATE_MANIFEST_DIR" \ bash "$MANIFEST_RECONCILER" # Stage the complete fixed allowlist. Staging before diff handles @@ -327,3 +1157,68 @@ jobs: echo "::error::manifest push to main failed after 5 monotonic retries; release pointer was not advanced" exit 1 fi + + finalize: + name: Finalize immutable release before manifest publication + needs: publish + if: ${{ !inputs.draft && needs.publish.result == 'success' }} + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: write + attestations: read + steps: + - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5 + with: + persist-credentials: false + + - name: Download all signed artifacts (sign-attest.yml) + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: genie-*-signed + merge-multiple: true + path: dist + + - name: Download canonical delivery descriptor subjects + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-descriptors + path: dist + + - name: Download all delivery endorsement bundles + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: delivery-endorsement-* + merge-multiple: true + path: dist + + - name: Download exact candidate manifests for final inventory binding + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: delivery-candidate-manifests + path: ${{ runner.temp }}/candidate-manifests + + - name: Install cosign for final remote verification + uses: sigstore/cosign-installer@398d4b0eeef1380460a10c8013a76f728fb906ac # v3 + with: + cosign-release: 'v2.4.1' + + - name: Install slsa-verifier for final remote verification + uses: slsa-framework/slsa-verifier/actions/installer@ea584f4502babc6f60d9bc799dbbb13c1caa9ee6 # v2.7.1 + + - name: Publish, lock, and reverify the complete remote inventory + shell: bash + env: + GH_TOKEN: ${{ github.token }} + VERSION: ${{ inputs.version }} + CHANNEL: ${{ inputs.channel }} + DRAFT: ${{ inputs.draft }} + RELEASE_REPOSITORY: ${{ github.repository }} + CANDIDATE_MANIFEST_DIR: ${{ runner.temp }}/candidate-manifests + run: | + set -euo pipefail + bash scripts/reconcile-release-note.sh finalize + # Every channel owns a fresh tag. Publish and lock the exact verified + # inventory before any public manifest can name it. + bash scripts/release-immutability.sh release + bash scripts/reconcile-release-assets.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ceaa5e6fa..5b058cee9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -154,6 +154,7 @@ jobs: contents: read with: version: ${{ inputs.version }} + channel: ${{ inputs.channel }} source_sha: ${{ inputs.source_sha }} sign-attest: @@ -178,10 +179,14 @@ jobs: uses: ./.github/workflows/release-publish.yml permissions: contents: write - attestations: read + id-token: write + attestations: write with: version: ${{ inputs.version }} channel: ${{ inputs.channel }} + source_sha: ${{ inputs.source_sha }} + source_branch: ${{ inputs.source_branch }} + source_ci_run_id: ${{ inputs.source_ci_run_id }} draft: false # Without inherit, the called workflow's secrets context is empty and the # manifests job sees RELEASE_MANIFESTS_TOKEN as unset even though the diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml index f19d37990..322d677b9 100644 --- a/.github/workflows/version.yml +++ b/.github/workflows/version.yml @@ -12,9 +12,9 @@ name: Version # release workflow with the successful parent CI run. The release guard accepts # the child only when all six changed fields are the deterministic version bump. # -# On main triggers (merge from dev) we DO NOT bump — the version is -# already in package.json from the dev tag. The successful main CI run and -# tree-equivalent existing tag produce a human-initiation handoff. A maintainer +# On homolog/main promotion triggers, derive a fresh immutable release identity +# without rewriting the source tree. The successful branch CI run and a new +# tag at that exact commit produce the automated/human-initiation handoff. A maintainer # must start the stable Release workflow, then a different maintainer approves # the protected production environment. # @@ -43,14 +43,13 @@ permissions: contents: write # commit + push version bump + tag actions: write # dispatch trusted release.yml control on main -# RACE (observed 2026-07-04T23:11:46Z, missed dev release): CI completes twice -# per dev merge (push-event run + pull_request-event run); both trigger this -# workflow. The PR-event run always fails the job `if` (event=='push') and shows -# "skipped" — but in a shared group with cancel-in-progress it CANCELLED the -# in-flight push-event bump. Scope the group by triggering event, and never -# cancel: killing a version bump mid-tag-push is unsafe regardless — queue instead. +# RACES: CI completes twice per dev merge (push + pull_request), so separate +# those event classes and never cancel an in-flight version operation. Within +# the push class, serialize main/homolog/dev globally: all three allocate from +# one CalVer tag namespace, and branch-scoped groups could independently choose +# the same max-plus-one tag. Queueing keeps allocation and push one transaction. concurrency: - group: version-${{ github.event.workflow_run.head_branch }}-${{ github.event.workflow_run.event }} + group: version-release-identity-${{ github.event.workflow_run.event }} queue: max cancel-in-progress: false @@ -90,16 +89,14 @@ jobs: BRANCH="${{ github.event.workflow_run.head_branch }}" case "$BRANCH" in main) - # Main push: tag whatever release.yml just landed. - # Do NOT bump — package.json already carries the dev-derived - # version. Channel: stable (advances latest + homolog + dev). + # Main promotion gets a fresh immutable release tag without + # rewriting its CI-approved source tree. echo "branch=main" >> "$GITHUB_OUTPUT" echo "should_bump=false" >> "$GITHUB_OUTPUT" ;; homolog) - # Homolog push: tag whatever the dev→homolog promotion just - # landed. Same no-bump posture as main — package.json already - # carries the dev-derived version that homolog is promoting. + # Homolog promotion gets a fresh immutable release tag without + # rewriting its CI-approved source tree. # Channel: homolog (advances homolog + dev). Felipe directive # 2026-05-12 added homolog as the dev→homolog→stable middle # tier; genie inherits the surface for cross-repo parity even @@ -335,8 +332,10 @@ jobs: echo "source_branch=dev" >> "$GITHUB_OUTPUT" echo "source_ci_run_id=${SOURCE_CI_RUN_ID}" >> "$GITHUB_OUTPUT" - # The no-bump path only resolves an existing dev-derived tag. Homolog is - # dispatched automatically below. Stable deliberately is not: if this + # Each promotion gets a new tag/version. Published GitHub Releases are + # immutable, so reusing the dev tag would make later evidence/metadata + # publication impossible. Homolog is dispatched automatically below. + # Stable deliberately is not: if this # GITHUB_TOKEN-authored workflow initiated the production deployment, # GitHub's prevent-self-review control would exclude only the bot, not the # maintainer who authored/promoted the change. A human workflow_dispatch @@ -347,19 +346,20 @@ jobs: if: steps.context.outputs.should_bump == 'false' && steps.promotion.outputs.promoted == 'true' run: | set -euo pipefail - VERSION="$(jq -er '.version | select(type == "string")' package.json)" - if [[ ! "$VERSION" =~ ^5\.[0-9]{6}\.[1-9][0-9]{0,3}$ ]]; then - echo "::error ::release-trigger.version_unresolved package.json has no version on the no-bump path — promotion is malformed" + TODAY="$(date -u +%y%m%d)" + MAX_COUNTER="$( + git tag --list "v5.${TODAY}.*" | + sed -nE "s/^v5\\.${TODAY}\\.([1-9][0-9]{0,3})$/\\1/p" | + sort -n | + tail -1 + )" + CURRENT_COUNTER="${MAX_COUNTER:-0}" + VERSION="5.${TODAY}.$((10#${CURRENT_COUNTER} + 1))" + [[ "${VERSION##*.}" -le 9999 ]] || { + echo "::error ::release-trigger.version_exhausted no daily release identities remain for ${TODAY}" exit 1 - fi - # The dev chain that derived this version must have already - # pushed v${VERSION}. If it is missing, the promotion raced ahead - # of the dev tag — surface loudly rather than dispatch a tag - # release.yml cannot check out. - if ! git ls-remote --exit-code --tags origin "refs/tags/v${VERSION}" >/dev/null 2>&1; then - echo "::error ::release-trigger.tag_missing v${VERSION} not on origin — dev derivation chain has not landed its tag; re-run after the dev release completes" - exit 1 - fi + } + git tag "v${VERSION}" HEAD echo "version=${VERSION}" >> "$GITHUB_OUTPUT" echo "source_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" echo "source_branch=${{ steps.context.outputs.branch }}" >> "$GITHUB_OUTPUT" @@ -375,6 +375,16 @@ jobs: ;; esac + - name: Push fresh immutable promotion tag + if: steps.context.outputs.should_bump == 'false' && steps.promotion.outputs.promoted == 'true' + env: + GH_TOKEN: ${{ github.token }} + VERSION: ${{ steps.promoted_tag.outputs.version }} + run: | + set -euo pipefail + gh auth setup-git + git push origin "refs/tags/v${VERSION}" + - name: Require human initiation for stable release if: >- steps.context.outputs.should_bump == 'false' && diff --git a/CHANGELOG.md b/CHANGELOG.md index c1290a50e..f324aee5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,16 +28,15 @@ Wish: `repair-genie-codex-hooks-and-dedupe-skills`. `genie:*` skills instead of both bare (`wish`) and qualified (`genie:wish`) duplicates. Bare `$` now resolves only a personal copy the user installed themselves. -- **One health proof gates every mutation.** Install, full update, and setup - converge the target plugin (respecting explicit disablement), take exactly - one post-convergence health snapshot — one enabled exact-version plugin with - a canonically verified payload/inventory and a bounded JSON-RPC session - (`initialize`, `tools/list` with all five Genie tools, read-only - `wish_status`) — then retire accepted fallbacks and only then run remaining - integrations (including role-agent install). `update --sync-only` inspects - rather than converges: a missing, disabled, or stale exact plugin fails - nonzero and leaves all trees byte-identical. A deliberately disabled plugin - is never silently enabled. +- **Signed delivery and activation have separate owners.** Install/update verify + and publish the selected release but never advance the Codex cache, change its + enabled state, reconcile its project route, or write roles. Authenticated + `setup --codex` activates the delivered generation, then takes exactly one + health proof — one enabled exact-version plugin with a canonically verified + payload/inventory and a bounded JSON-RPC session (`initialize`, `tools/list` + with all five Genie tools, read-only `wish_status`) — before retiring accepted + fallbacks and converging roles. A deliberately disabled exact generation stays + disabled, skips fallback retirement, and still repairs managed roles. #### Added — durable fallback retirement, quarantine, and recovery @@ -75,29 +74,20 @@ Wish: `repair-genie-codex-hooks-and-dedupe-skills`. - `.codex/skills/.curated` is a legacy uninstall-only lane: `genie uninstall` still collects it, but no install/update/setup/sync path recreates it. -- Restart Codex after any Codex convergence so it drops stale bare providers. +- Restart Codex after successful authenticated setup so it drops stale bare providers. - Claude and Hermes skill synchronization, Codex hooks (H3/H4/H6), the MCP launcher, role-agent TOMLs, and the PR #2559 dangling-symlink preservation are unchanged and regression-gated. -#### Known non-blocking red gate - -- `bun run check` exits 1 solely because of 6 pre-existing env-dependent unit - failures: `src/lib/codex-project-mcp.test.ts` (4) and - `src/hooks/__tests__/codex-manifest.test.ts` (2). They build their own - fixtures with an unpopulated `GENIE_HOME`, so `session-context.cjs` emits `{}` - and they fail identically with or without an isolated env; they are untouched - by this wish (`git log ed6b4249..HEAD` is empty on both files). The same - criteria are proven black-box against the real installed plugin in - `scripts/codex-plugin-only-smoke.ts` (project-MCP reconcile via `genie init`, - installed-manifest MCP shape, JSON-RPC MCP usability, and the bounded - SessionStart hook). Do not mistake this red for a regression; CI is not green. - #### Before release promotion - The plugin-only smoke installs the built CLI plus the source `plugins/` tree - (matching release contents by proxy). Verify the actual packaged tarball - payload — not only the source checkout — before promoting a release. + (matching release contents by proxy). Because a source-built version has no + public signature, an unshipped fixture driver supplies only deterministic + delivery-signature verification and real-PTY setup entry; real Codex cache, + plugin, MCP, fallback-retirement, and preservation behavior remains externally + observed. Verify the actual packaged tarball payload — not only the source + checkout — before promoting a release. - Run the manual dogfood checklist (README, "Manual dogfood checklist") once from a restarted Codex session to confirm one plugin version, working MCP/hooks, and only owner-qualified `genie:*` skills. diff --git a/CLAUDE.md b/CLAUDE.md index 41ad1eac0..26d0f369f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -79,12 +79,12 @@ Fourteen top-level commands (run `genie --help` for detail): | `board` | Kanban view derived by query (no stored view state); `--board`, `--wish`, `--json` | | `doctor` | Diagnostic checks on the genie installation | | `hook` | Hook middleware for Claude Code (`genie hook dispatch` runs in-process) | -| `init` | Scaffold per-repo state and reconcile `.mcp.json`, `.warp/.mcp.json`, plus marker-owned `.codex/config.toml` fallback when no installed, enabled, usable plugin route is proven | -| `install` | Post-install finisher — v4 legacy cleanup (`--skip-v4-cleanup`) | +| `init` | Scaffold per-repo state and reconcile `.mcp.json`, `.warp/.mcp.json`, plus the marker-owned `.codex/config.toml` stable-facade route | +| `install` | Post-install finisher — authenticated delivery, v4 cleanup (`--skip-v4-cleanup`), and non-Codex convergence | | `launch ` | Open a Warp cockpit for a wish: one pane per ready group, each in its own worktree | | `mcp` | Read-only stdio MCP server exposing genie.db task/board state | | `omni` | Omni integration — `serve`, `status`, `inbox`, `handshake` | -| `setup` | Configure genie settings | +| `setup` | Configure Genie; authenticated `setup --codex` owns Codex activation and managed convergence | | `shortcuts` | Manage tmux keyboard shortcuts | | `task` | Task state (SQLite, zero-daemon) | | `uninstall` | Remove Genie CLI and clean up hooks | @@ -92,6 +92,7 @@ Fourteen top-level commands (run `genie --help` for detail): | `help` | `genie help [command]` | ### Task subcommands + ```bash genie task create --title 'x' # Create a task genie task list # List tasks (with filters) @@ -102,6 +103,7 @@ genie task export # Emit the complete DB state as JSON ``` ### Omni subcommands + ```bash genie omni handshake # Register this host with the omni server (ed25519, idempotent) genie omni serve # Resident runner: NATS bridge → approval queue (foreground) @@ -163,16 +165,16 @@ Biome's `noExcessiveCognitiveComplexity` is set to `maxAllowedComplexity: 25` (w ## Gotchas -- **Codex ships exactly H3/H4/H6, and all remain untrusted after an edit** — H3 is bounded read-only SessionStart context; H4 is deterministic local PreToolUse guarding; H6 is matcher-scoped PermissionRequest approval and is the only retained hook that may write approval-queue state. No Codex lifecycle hook installs, updates, synchronizes, or scaffolds. Run `genie setup --codex` or `genie update` explicitly, inspect changed hashes with `/hooks`, and start a new task. Codex hook commands run outside the model sandbox, so hard controls still belong in sandbox permissions and server-side branch protection. +- **Codex ships exactly H3/H4/H6, and all remain untrusted after an edit** — H3 is bounded read-only SessionStart context; H4 is deterministic local PreToolUse guarding; H6 is matcher-scoped PermissionRequest approval and is the only retained hook that may write approval-queue state. No Codex lifecycle hook installs, updates, synchronizes, or scaffolds. Run `genie update` to deliver signed bytes, then authenticated `genie setup --codex` to activate them; inspect changed hashes with `/hooks` and start a new task. Codex hook commands run outside the model sandbox, so hard controls still belong in sandbox permissions and server-side branch protection. - **Hook dispatch is provider-aware and fail-closed** — the plugin-local Codex launcher selects only the canonical `$GENIE_HOME/bin/genie`, preserves stdin/stdout/signals, bounds child time/output, validates event-specific JSON, and converts launch/timeout/schema failures into a reasoned deny. Claude dispatch remains a short-lived in-process `genie hook dispatch` fork. Neither path is a daemon. - **`AskUserQuestion` is the one PreToolUse carve-out** — it is in `NON_INTERCEPTABLE_PRE_TOOL_USE_TOOLS` and MUST get an EMPTY response, not the neutral `{ decision: 'block' }` block form. Empirically CC consumes any additionalContext as the synthesized answer, so a fail-closed block would corrupt the inline picker. The fail-closed envelope special-cases this tool. - **Two `genie.db` files, never cross-import** — per-repo `.genie/genie.db` (`genie-db.ts`, task/board/wish) and global `~/.genie/genie.db` (`global-db.ts`, omni queue + inbox) are independent databases with their own schemas and `user_version`. `global-db.ts` shares only `sqlite-open.ts` with the per-repo one — do not reach across for path constants. - **Codex integration health is native state, not OTel** — the old Genie exporter at `127.0.0.1:14318` has no relay and is removed by an exact-match, backup-first migration. Preserve unrelated OTel settings and `disable_paste_burst`. - **The Omni runner (`genie omni serve`) is the only optional daemon** — a foreground NATS bridge that drains the global approval queue. Everything else is fork-and-exit; no resident processes. - **`bun run dead-code`** (knip) has pre-existing false positives for biome/commitlint/husky devDeps — not regressions. -- **agent-sync runs only on selected explicit install/update paths, and Codex writes no product fallbacks** — `genie install` and `genie update` converge only the client tiers selected by `--integrations`; `none` mutates no client home. The installed Codex plugin is the sole Genie-managed skill provider: no supported command writes Genie product skills into `~/.agents/skills`. A fresh Codex install seeds zero user-tier skills. An upgrade from a fallback-seeding release retires only provably clean, digest-owned copies into the hidden `~/.agents/skills/.genie-codex-fallback-retirement/` quarantine transaction, and only after one post-convergence plugin health proof; same-name unmanaged, malformed-marker, symlinked, or modified user assets are preserved in place and reported as user-owned collisions. Codex plugin skills are a separate physical 23-skill in-root payload, and the optional seven role-agent TOMLs are staged under `plugins/genie/codex-agents/` for CLI install into `~/.codex/agents/`. Role-agent install is gated by plugin convergence, not by the health proof: on the deliberately-disabled-plugin path, `convergeCodexPluginOnly` installs the TOMLs and returns before `proveCodexPluginHealth` ever runs (R3 — a disabled plugin skips health + retirement and is never enabled), so those TOMLs land with no health proof at all. Only the enabled-plugin path installs role agents after one post-convergence health proof. +- **Codex delivery and activation have separate mutation owners** — install/update may verify, stage, and publish a complete authenticated delivery, but they never advance the Codex cache, change enabled state, reconcile the project route, retire fallbacks, or write Codex roles. `genie setup --codex` requires the matching record before its first prompt or mutation. Once current, it revalidates the authenticated delivery root around marketplace/consent/fallback/role operations, proves one exact enabled plugin before retiring only clean historical fallbacks, and preserves unmanaged, malformed-marker, symlinked, or modified user assets. The deliberately disabled exact-version path stays disabled, skips health and fallback retirement, and still converges the seven managed role profiles. No supported command writes Genie product skills into `~/.agents/skills`. - **`.codex/skills/.curated` is a legacy uninstall-only lane, never resurrected in sync** — the retired hidden `.curated` path is no longer written or read by any install/update/setup/sync convergence path. `genie uninstall` still *collects* it (classifier-only removal of a legacy directory a very old Genie may have left behind) so uninstall leaves nothing stale, but Codex sync must never recreate that lane. Do not reintroduce `.curated` cleanup inside codex sync. -- **Post-delivery convergence is an argv-only handoff, never an environment-only re-exec** — the 2026-07-11 downgrade incident proved that an old target can ignore an environment-only sync contract and perform a second full update. Today `genie update` invokes the freshly installed binary as `update --post-delivery-converge`: an explicit argv protocol that pre-contract binaries reject at commander parse time, with the mode resolved before any mutation and the lifecycle lease borrowed under an exact-owner record (the parent stays the sole lease owner). Never replace this with an environment-variable-only convergence signal. When crossing from a release older than `5.260711.6` to `5.260711.6` or later, still run one explicit second `genie update` after the first command returns; no hook supplies that compatibility hop. +- **Post-delivery convergence is an argv-only handoff, never an environment-only re-exec** — the 2026-07-11 downgrade incident proved that an old target can ignore an environment-only sync contract and perform a second full update. Today `genie update` invokes the freshly installed binary as `update --post-delivery-converge`: an explicit argv protocol that pre-contract binaries reject at commander parse time, with the mode resolved before any mutation and the lifecycle lease borrowed under an exact-owner record (the parent stays the sole lease owner). Never replace this with an environment-variable-only convergence signal. When crossing from a release older than `5.260711.6` to `5.260711.6` or later, still run one explicit second `genie update` to repair authenticated delivery, then `genie setup --codex` to activate it; no hook supplies either compatibility hop. - **Generated SessionStart parity is a release gate** — edit `plugins/genie/scripts/src/session-context.ts`, regenerate with `bun scripts/hook-bundle-parity.ts --write`, and commit the executable `session-context.cjs`. `bun run check`, the plugin build, and tarball build all reject byte or mode drift. - **Wish state is persisted by the orchestrator, never the reviewer** — reviewer verdicts are SHIP/FIX-FIRST/BLOCKED evidence; durable WISH statuses are `DRAFT`, `FIX-FIRST`, `APPROVED`, `IN_PROGRESS`, `BLOCKED`, and `SHIPPED`. SessionStart, `genie`, `dream`, `wizard`, and resume routing consume that vocabulary. A chat verdict does not advance state until the invoking orchestrator appends review evidence and updates WISH.md. - **One stamp root, never CLAUDE_PLUGIN_ROOT-primary** — explicit install/update council stamping resolves `LENS_ROOT` from the stable `~/.genie/plugins/genie` root and only falls back to `CLAUDE_PLUGIN_ROOT` when the stable template is absent. No lifecycle hook stamps or synchronizes it. Do not reintroduce marketplace-root-primary stamping: marketplace roots change with plugin versions and can revive stale-cache downgrade behavior. diff --git a/README.md b/README.md index 7fc22c697..3f3f67ff7 100644 --- a/README.md +++ b/README.md @@ -27,11 +27,11 @@ Every release is cosign-signed (keyless OIDC) with SLSA provenance; the installe The repository-hosted `.well-known/latest.json`, `homolog.json`, and `dev.json` manifests are the authoritative channel pointers. GitHub's `/releases/latest` route and prerelease badge are deliberately not channel authority: a promotion advances only a monotonic manifest and never rewrites already-published assets or channel-significant draft/prerelease/latest metadata. -The installer detects Claude Code and Codex and installs the version-matched Genie plugin for each. Control this with `--integrations auto|codex|claude|all|none` or `--skip-integrations`. When Codex is selected, the installed plugin is the **only** Genie-managed skill provider: a fresh install writes zero Genie product skills into `~/.agents/skills/` and requires one enabled, exact-version plugin with a usable MCP launcher and complete skill payload before it mutates anything. An upgrade from a release that still seeded user-tier fallbacks quarantines only provably clean historical copies — and only after a single post-convergence plugin health proof passes; same-name unmanaged, modified, malformed-marker, or symlinked user copies are preserved in place and reported as user-owned collisions. Automatic integration failures warn after the verified binary succeeds; explicitly requested failures are fatal. +The installer detects Claude Code and Codex and delivers the selected, version-matched payloads. Control this with `--integrations auto|codex|claude|all|none` or `--skip-integrations`. Codex delivery is deliberately separate from activation: install/update verify the signed release and publish a complete authenticated delivery record, but never advance the Codex cache, change its enabled state, reconcile its project route, or write role agents. A delivered generation that still needs activation exits with an action-required result directing the operator to `genie setup --codex`. -From inside a repo, run `genie init`. Use `genie setup --codex` to explicitly install or repair the Codex plugin, seven optional role-agent profiles, MCP routing, and the backup-first dead-OTel migration. A successful Codex setup also persists Codex maintenance consent: later, explicit `genie update` runs may refresh those Codex integration surfaces. That consent never writes new product skills into the user tier, never makes personal or modified skills managed, and it gives no authority to hooks. `genie update` is the explicit update/convergence path. When crossing from a release older than `5.260711.6` to `5.260711.6` or later, let the first update finish and run `genie update` one more time: the first process may only deliver the new binary/payload, while the second runs the new convergence contract. If the older release had seeded clean user-tier fallbacks, the convergence run retires them into a hidden quarantine transaction (see [Codex fallback quarantine and recovery](#codex-fallback-quarantine-and-recovery)) once one health proof passes. Verify that the plugin exposes exactly H3/H4/H6, then review their hashes with `/hooks` and start a new task. Later updates converge in one operator-driven path. No Codex hook or Claude SessionStart hook installs software, refreshes plugins, synchronizes skills, stamps workflows, or writes project instructions. +From inside a trusted initialized repo, run `genie init` to reconcile the marker-owned project MCP route. Then run `genie setup --codex` from an external interactive terminal. Setup requires a matching authenticated delivery record before its first prompt or mutation; it activates the delivered plugin, proves the exact enabled payload and bounded MCP launcher, retires only clean historical user-tier fallbacks, converges seven optional role agents, and reconciles the project route. An already-current deliberately disabled plugin stays disabled, skips fallback retirement, and still repairs managed roles. Personal, modified, malformed-marker, and symlinked collisions remain untouched. Successful setup persists Codex delivery scope for later explicit updates, but those updates still deliver only; a new generation requires a fresh setup assertion. No hook installs software, activates plugins, synchronizes skills, or writes project instructions. -Codex never auto-trusts plugin hooks. H4/H6 definitions bind the exact plugin launcher SHA-256 and the launcher verifies itself before spawning, so launcher changes produce new definitions; the current hook schema still cannot transitively bind the mutable platform-specific Genie binary. After setup or update, inspect the three Genie definitions with `/hooks`, approve only the hashes you understand, and start a new task so the reviewed definitions take effect. Until then they remain untrusted and do not run. +Codex never auto-trusts plugin hooks. H4/H6 definitions bind the exact plugin launcher SHA-256 and the launcher verifies itself before spawning, so launcher changes produce new definitions; the current hook schema still cannot transitively bind the mutable platform-specific Genie binary. After successful Codex setup, inspect the three Genie definitions with `/hooks`, approve only the hashes you understand, and start a new task so the reviewed definitions take effect. Until then they remain untrusted and do not run. ## Quickstart @@ -66,14 +66,14 @@ genie --help | Command | What it does | |---------|-------------| -| `genie init` | Scaffold per-repo state and reconcile project MCP files (`.mcp.json`, `.warp/.mcp.json`, and a marker-owned `.codex/config.toml` fallback when no installed, enabled, usable Genie plugin route is proven) | +| `genie init` | Scaffold per-repo state and reconcile project MCP files (`.mcp.json`, `.warp/.mcp.json`, and the marker-owned `.codex/config.toml` stable-facade route) | | `genie launch` | Open a Warp cockpit for a wish — one pane per ready group, each in its own worktree | | `genie board` | Kanban view of task state, derived live by query | | `genie task` | Inspect and drive task state (SQLite, zero-daemon) | -| `genie install` | Run the installer-owned finishing step and explicitly install selected runtime integrations | +| `genie install` | Finish a verified install and deliver selected integrations; Codex activation is deferred to setup | | `genie mcp` | Serve read-only Genie task/board state over stdio MCP | | `genie omni` | Bridge agents to WhatsApp via Omni — remote approvals + inbound one-shots (`serve`, `status`, `inbox`, `handshake`) | -| `genie setup` | Configure Genie and install/repair runtime integrations | +| `genie setup` | Configure Genie; `setup --codex` activates an authenticated delivery and converges Codex-owned surfaces | | `genie doctor` | Run diagnostic checks on the installation | | `genie hook` | Provider-neutral hook middleware with Claude/Codex wire adapters | | `genie shortcuts` | Manage terminal keyboard shortcuts | @@ -101,11 +101,11 @@ These five inventories are intentionally separate: | Surface | What ships | Ownership | |---------|------------|-----------| -| Codex plugin | 23 physical, in-root product skills with `agents/openai.yaml`; three untrusted hooks; MCP declaration | Versioned release payload; the **sole** Genie-managed skill provider — nothing is copied into the user tier | -| Fallback retirement | Hidden `~/.agents/skills/.genie-codex-fallback-retirement/` quarantine transaction | Not written on fresh install. Upgrades from a fallback-seeding release move only provably clean historical copies here after one health proof; evidence is retained for recovery | -| CLI integration | Seven optional `genie_*` role-agent TOMLs under `~/.codex/agents/` | Installed/repaired by `genie install` or `genie setup --codex` behind the same plugin health gate; clean managed copies refresh on explicit update | +| Codex plugin | 23 physical, in-root product skills with `agents/openai.yaml`; three untrusted hooks; no Codex-owned MCP declaration | Versioned release payload; the **sole** Genie-managed skill provider — nothing is copied into the user tier | +| Fallback retirement | Hidden `~/.agents/skills/.genie-codex-fallback-retirement/` quarantine transaction | Not written on fresh setup. After authenticated activation, setup moves only provably clean historical copies here after one health proof; evidence is retained for recovery | +| CLI integration | Seven optional `genie_*` role-agent TOMLs under `~/.codex/agents/` | Installed/repaired only by successful `genie setup --codex`, after authenticated-root revalidation and fallback retirement | | Personal skills | This maintainer currently has 36 separately adapted skills under `~/.agents/skills` | User-owned; not bundled with Genie and never implied by plugin installation; preserved byte-for-byte even on same-name collision | -| MCP launcher | Plugin-local Node launcher for `genie mcp` | Resolves only the canonical executable under `$GENIE_HOME/bin` (default `~/.genie/bin`) and fails closed if it is missing or unsafe | +| Project MCP route | Marker-owned `.codex/config.toml` entry for `genie mcp` | Points at the stable absolute `$GENIE_HOME/bin/genie` facade with no `cwd` override; the plugin declares no Codex MCP route | The plugin's 23 skills and a user's personal 36-skill library are separate inventories even when names overlap. Genie never seeds the user tier and preserves unmanaged, modified, malformed-marker, and symlinked user copies instead of adopting them; use `$genie:` when the plugin copy is intended. @@ -121,7 +121,7 @@ The removed hooks were the startup installer, first-run `AGENTS.md` writer, pre/ ### Codex fallback quarantine and recovery -Older Genie releases seeded up to 23 digest-managed product skills into `~/.agents/skills/`. When a plugin-only convergence run upgrades such a machine, it does **not** delete those copies. After one post-convergence plugin health proof passes, it moves only the provably clean, Genie-owned copies into a single durable quarantine transaction under: +Older Genie releases seeded up to 23 digest-managed product skills into `~/.agents/skills/`. Authenticated `genie setup --codex` does **not** delete those copies. After one current-plugin health proof passes, it moves only the provably clean, Genie-owned copies into a single durable quarantine transaction under: ```text ~/.agents/skills/.genie-codex-fallback-retirement/ @@ -133,7 +133,7 @@ Older Genie releases seeded up to 23 digest-managed product skills into `~/.agen A copy is only retired when it is a physical non-symlink directory, carries a valid versioned `.genie-sync.json` marker, its recomputed canonical physical digest equals the marker digest, and it matches either the verified target-plugin payload or a committed verified-release historical tuple. Anything failing any predicate — modified-managed, malformed-marker, symlinked, or an unmanaged same-name personal skill — stays in place untouched and is reported as a user-owned collision. -The transaction is idempotent and durable: repeated updates recognize the committed transaction and never create a second one or accumulate quarantine entries; an interrupted run reverse-restores every pre-commit move without clobbering conflicts. Quarantine and journal evidence are retained after commit so you can recover manually: +The transaction is idempotent and durable: repeated setup runs recognize the committed transaction and never create a second one or accumulate quarantine entries; an interrupted run reverse-restores every pre-commit move without clobbering conflicts. Quarantine and journal evidence are retained after commit so you can recover manually: - **Recover a retired skill.** Move the tree back out of `txn-/quarantine//` into `~/.agents/skills//`. This is only needed if you intentionally want a bare `$` user-tier copy; the plugin already serves it as `$genie:`. - **"Source changed after planning".** If your live skill was edited between the health proof and the move, retirement aborts before touching disk — the changed personal copy simply stays in place at `~/.agents/skills/`; nothing is moved, republished, or archived. Review that copy, then rerun the command. @@ -143,7 +143,7 @@ The transaction is idempotent and durable: repeated updates recognize the commit ### Restart Codex after a Codex convergence -Codex reads its plugin catalog and skill inventory at process start. After any `genie install --integrations codex`, `genie setup --codex`, or `genie update` that touched Codex, **restart Codex** so it drops any stale bare user-tier providers and loads only the owner-qualified `genie:*` plugin skills. Then review the three hook definitions with `/hooks` and start a new task. +Codex reads its plugin catalog and skill inventory at process start. After a successful `genie setup --codex` activation or repair, **restart Codex** so it drops any stale bare user-tier providers and loads only the owner-qualified `genie:*` plugin skills. Then review the three hook definitions with `/hooks` and start a new task. ### Manual dogfood checklist @@ -181,15 +181,15 @@ All linked worktrees of a repository share one `genie.db`, resolved from the git ## MCP server (Warp + Claude Code + Codex) -`genie mcp` is a zero-dependency, read-only [MCP](https://modelcontextprotocol.io) server over stdio. The Codex plugin bundles a declaration and a small launcher, not another Genie binary. The launcher resolves only `$GENIE_HOME/bin/genie` (default `~/.genie/bin/genie`); missing, symlinked, or path-escaped executables fail closed. When no installed, enabled, usable Genie plugin route can be proven, `genie init` merges an absolute-path, marker-owned fallback into project `.codex/config.toml` without duplicating the server. +`genie mcp` is a zero-dependency, read-only [MCP](https://modelcontextprotocol.io) server over stdio. Codex does not launch it from the versioned plugin cache. Instead, every trusted initialized repository owns one marker-managed `.codex/config.toml` route pointing at the stable absolute `$GENIE_HOME/bin/genie mcp` facade, with no `cwd` override. Missing, symlinked, or path-escaped executables fail closed. -**How it gets picked up.** `genie init` reconciles Claude and Warp project configs and may change the three project files named below; review those project-scoped commands before trusting the workspace. Codex normally gets MCP from the plugin. The marker-owned project fallback is retained whenever Genie's probe cannot prove one installed, enabled, usable plugin route — including absent, disabled, malformed, timed-out, or unsafe plugin state. `genie launch` applies the same policy to its worktrees: +**How it gets picked up.** `genie init` reconciles Claude, Warp, and Codex project configs and may change the three project files named below; review those project-scoped commands before trusting the workspace. The Codex route is plugin-independent and is created or repaired only when its marker proves Genie ownership. Unowned same-key routes, damaged markers, nested shadowing, and untrusted repositories are preserved and reported rather than overwritten. `genie launch` applies the same marker-owned policy to its worktrees: - `.mcp.json` — Claude Code's project MCP config. Project-scope servers are *pending approval* until you trust the workspace (accept the trust dialog in an interactive `claude` session) — expected, not a bug. - `.warp/.mcp.json` — Warp auto-detects this on save (no restart) and lists `genie` under Settings → AI/Agents → MCP servers. -- `.codex/config.toml` — marker-owned absolute-path fallback, written whenever no installed, enabled, usable native Genie plugin route is proven. +- `.codex/config.toml` — marker-owned absolute stable-facade route with no effective `cwd` override. -The Claude and Warp JSON files use the identical `mcpServers` shape and are merged idempotently; the Codex TOML fallback uses marker-owned root-level dotted assignments so it cannot capture following keys. Re-running `genie init` preserves every other server and top-level key and rewrites byte-identical. A compiled Genie records the absolute executable plus `mcp`; an interpreted `bun src/genie.ts` or `bun dist/genie.js` run records the absolute Bun executable plus the absolute script and `mcp`. No route relies on bare `genie`, which is not reliably on PATH. Because `genie init`/`launch` run on the box that owns the repo, the recorded paths are correct even under Warp's SSH-remote feature, where Warp spawns the server on that same box. +The Claude and Warp JSON files use the identical `mcpServers` shape and are merged idempotently; the Codex TOML route uses marker-owned root-level dotted assignments so it cannot capture following keys. Re-running `genie init` preserves every other server and top-level key and rewrites byte-identical. A compiled Genie records the absolute executable plus `mcp`; an interpreted `bun src/genie.ts` or `bun dist/genie.js` run records the absolute Bun executable plus the absolute script and `mcp`. No route relies on bare `genie`, which is not reliably on PATH. Because `genie init`/`launch` run on the box that owns the repo, the recorded paths are correct even under Warp's SSH-remote feature, where Warp spawns the server on that same box. **What it exposes** — five read-only tools backed by the per-repo `.genie/genie.db`: diff --git a/SECURITY.md b/SECURITY.md index 6f602ee21..be19c1688 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -104,7 +104,7 @@ The current Genie CLI does not ship a `genie sec` command. Do not treat older ro `@automagik/genie` releases are signed with **cosign keyless** via GitHub Actions OIDC. There is no long-lived public key to pin — no private key in repo secrets, no hardware-backed offline key, no two-officer key-custody ceremony. What operators pin instead is the **certificate identity + OIDC issuer + provenance source-uri** tuple that `cosign verify-blob` and `slsa-verifier` must accept. If all three values match across all three pinning channels, the release was signed by the repo's own Actions workflow and by nothing else. -Release-channel authority comes from the repository-hosted `.well-known/latest.json`, `homolog.json`, and `dev.json` manifests. GitHub's `/releases/latest` route and prerelease label are non-authoritative. Promotion advances only the eligible monotonic manifest after exact remote asset verification; it never replaces published bytes or edits published draft/prerelease/latest state. A fixed migration caveat may be appended to human-authored release notes. This separation permits repository-level immutable releases while retaining one verified version across dev, homolog, and stable channels. +Release-channel authority comes from the repository-hosted `.well-known/latest.json`, `homolog.json`, and `dev.json` manifests. Every dev, homolog, or stable promotion creates a fresh monotonic version and immutable tag from its CI-approved channel source. The exact release becomes public and immutable before the eligible manifest advances; published release bytes and state are never edited. GitHub's release metadata remains a non-authoritative convenience: stable releases are non-prerelease and marked Latest, while dev and homolog releases are prereleases and never Latest. A fixed migration caveat may be appended to human-authored release notes. Immutable-release cutover order is security-critical. Seal the candidate and drain every Version and Release run started under the old `main` workflows before enabling repository immutability; enable it before the separately approved merge to `main`. Immutability protects only releases created after enablement, so any release published by the mutable predecessor must fail closed and must not advance a channel manifest. The first version eligible for stable promotion after cutover must be freshly built and published by the merged draft-first release control. diff --git a/bun.lock b/bun.lock index 11efb9533..d7928e586 100644 --- a/bun.lock +++ b/bun.lock @@ -3,24 +3,27 @@ "configVersion": 1, "workspaces": { "": { - "name": "@namastexlabs/claudio", + "name": "@automagik/genie", "dependencies": { "@inquirer/prompts": "7.10.1", + "@nats-io/transport-node": "3.4.0", + "@sigstore/bundle": "5.0.0", + "@sigstore/protobuf-specs": "0.5.0", + "@sigstore/verify": "4.1.0", "commander": "12.1.0", - "nats": "2.29.3", "zod": "3.25.76", }, "devDependencies": { "@biomejs/biome": "^1.9.4", - "@commitlint/cli": "^20.4.1", - "@commitlint/config-conventional": "^20.4.1", + "@commitlint/cli": "^21.2.1", + "@commitlint/config-conventional": "^21.2.0", "@types/bun": "^1.3.10", "@types/node": "^22.0.0", - "esbuild": "^0.27.3", + "esbuild": "^0.28.1", "husky": "^9.1.7", - "knip": "^5.86.0", + "knip": "^6.29.0", "markdown-link-check": "3.14.2", - "markdownlint-cli2": "0.23.0", + "markdownlint-cli2": "0.23.1", "typescript": "^5.8.0", }, }, @@ -29,9 +32,9 @@ "@biomejs/biome", ], "packages": { - "@babel/code-frame": ["@babel/code-frame@7.29.0", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.28.5", "js-tokens": "^4.0.0", "picocolors": "^1.1.1" } }, "sha512-9NhCeYjq9+3uxgdtp20LSiJXJvN0FeCtNGpJxuMFZ1Kv3cWUNb6DOhJwUvcVCzKGR66cw4njwM6hrJLqgOwbcw=="], + "@babel/code-frame": ["@babel/code-frame@7.29.7", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.29.7", "js-tokens": "^4.0.0", "picocolors": "^1.1.1" } }, "sha512-Aup7aUOfpbAUg2ROOJN6Iw5f9DMBlzu0mIkm/malLQFN/YQgO48wCj0Kxa3sEHJvPVFg7siR+qRInwXd2qhQKw=="], - "@babel/helper-validator-identifier": ["@babel/helper-validator-identifier@7.28.5", "", {}, "sha512-qSs4ifwzKJSV39ucNjsvc6WVHs6b7S03sOh2OcHF9UHfVPqWWALUsNUVzhSBiItjRZoLHx7nIarVjqKVusUZ1Q=="], + "@babel/helper-validator-identifier": ["@babel/helper-validator-identifier@7.29.7", "", {}, "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg=="], "@biomejs/biome": ["@biomejs/biome@1.9.4", "", { "optionalDependencies": { "@biomejs/cli-darwin-arm64": "1.9.4", "@biomejs/cli-darwin-x64": "1.9.4", "@biomejs/cli-linux-arm64": "1.9.4", "@biomejs/cli-linux-arm64-musl": "1.9.4", "@biomejs/cli-linux-x64": "1.9.4", "@biomejs/cli-linux-x64-musl": "1.9.4", "@biomejs/cli-win32-arm64": "1.9.4", "@biomejs/cli-win32-x64": "1.9.4" }, "bin": { "biome": "bin/biome" } }, "sha512-1rkd7G70+o9KkTn5KLmDYXihGoTaIGO9PIIN2ZB7UJxFrWw04CZHPYiMRjYsaDvVV7hP1dYNRLxSANLaBFGpog=="], @@ -51,97 +54,101 @@ "@biomejs/cli-win32-x64": ["@biomejs/cli-win32-x64@1.9.4", "", { "os": "win32", "cpu": "x64" }, "sha512-8Y5wMhVIPaWe6jw2H+KlEm4wP/f7EW3810ZLmDlrEEy5KvBsb9ECEfu/kMWD484ijfQ8+nIi0giMgu9g1UAuuA=="], - "@commitlint/cli": ["@commitlint/cli@20.4.3", "", { "dependencies": { "@commitlint/format": "^20.4.3", "@commitlint/lint": "^20.4.3", "@commitlint/load": "^20.4.3", "@commitlint/read": "^20.4.3", "@commitlint/types": "^20.4.3", "tinyexec": "^1.0.0", "yargs": "^17.0.0" }, "bin": { "commitlint": "./cli.js" } }, "sha512-Z37EMoDT7+Upg500vlr/vZrgRsb6Xc5JAA3Tv7BYbobnN/ZpqUeZnSLggBg2+1O+NptRDtyujr2DD1CPV2qwhA=="], + "@commitlint/cli": ["@commitlint/cli@21.2.1", "", { "dependencies": { "@commitlint/config-conventional": "^21.2.0", "@commitlint/format": "^21.2.0", "@commitlint/lint": "^21.2.0", "@commitlint/load": "^21.2.0", "@commitlint/read": "^21.2.1", "@commitlint/types": "^21.2.0", "tinyexec": "^1.0.0", "yargs": "^18.0.0" }, "bin": { "commitlint": "cli.js" } }, "sha512-blsZGe29hJ72VGEFVl72IVYX+1vsfINpjA9yWQA6i7OKD/McGEOXg08sKIRKjFk4JvzhV/9n0l3i6NooPLTNfg=="], - "@commitlint/config-conventional": ["@commitlint/config-conventional@20.4.3", "", { "dependencies": { "@commitlint/types": "^20.4.3", "conventional-changelog-conventionalcommits": "^9.2.0" } }, "sha512-9RtLySbYQAs8yEqWEqhSZo9nYhbm57jx7qHXtgRmv/nmeQIjjMcwf6Dl+y5UZcGWgWx435TAYBURONaJIuCjWg=="], + "@commitlint/config-conventional": ["@commitlint/config-conventional@21.2.0", "", { "dependencies": { "@commitlint/types": "^21.2.0", "conventional-changelog-conventionalcommits": "^10.0.0" } }, "sha512-Qf8WRDVcyVd14if6VTWenebxFbKnVnbzPUJjlzjkyJGeHK2xCGd63Dr1XZzj0plXKQb9P0BfOxoc1HVeCo2BWQ=="], - "@commitlint/config-validator": ["@commitlint/config-validator@20.4.3", "", { "dependencies": { "@commitlint/types": "^20.4.3", "ajv": "^8.11.0" } }, "sha512-jCZpZFkcSL3ZEdL5zgUzFRdytv3xPo8iukTe9VA+QGus/BGhpp1xXSVu2B006GLLb2gYUAEGEqv64kTlpZNgmA=="], + "@commitlint/config-validator": ["@commitlint/config-validator@21.2.0", "", { "dependencies": { "@commitlint/types": "^21.2.0", "ajv": "^8.11.0" } }, "sha512-t7AzNHAKeIdo/3NRGwzpufKHsKkPHmFs/56N2Fnsh0/r0rGtnQzTxk6vnFgjaGr4hdSQKNB50/KAhR9Yk4LJKA=="], - "@commitlint/ensure": ["@commitlint/ensure@20.4.3", "", { "dependencies": { "@commitlint/types": "^20.4.3", "lodash.camelcase": "^4.3.0", "lodash.kebabcase": "^4.1.1", "lodash.snakecase": "^4.1.1", "lodash.startcase": "^4.4.0", "lodash.upperfirst": "^4.3.1" } }, "sha512-WcXGKBNn0wBKpX8VlXgxqedyrLxedIlLBCMvdamLnJFEbUGJ9JZmBVx4vhLV3ZyA8uONGOb+CzW0Y9HDbQ+ONQ=="], + "@commitlint/ensure": ["@commitlint/ensure@21.2.0", "", { "dependencies": { "@commitlint/types": "^21.2.0", "es-toolkit": "^1.46.0" } }, "sha512-76IF9vDNS13lAzEEik9eKwzt8f9hYhWiwVXZ2AnyLCz5/f511FsEQ3pw1X3/zSQpdRLQU7i5qDMVKyXi1GWjSg=="], - "@commitlint/execute-rule": ["@commitlint/execute-rule@20.0.0", "", {}, "sha512-xyCoOShoPuPL44gVa+5EdZsBVao/pNzpQhkzq3RdtlFdKZtjWcLlUFQHSWBuhk5utKYykeJPSz2i8ABHQA+ZZw=="], + "@commitlint/execute-rule": ["@commitlint/execute-rule@21.0.1", "", {}, "sha512-RifH+FmImozKBE6mozhF4K3r2RRKP7SMi/Q/zLCmExtp5e05lhHOUYqGBlFBAGNHaZxU/WYw1XuugYK9jQzqnA=="], - "@commitlint/format": ["@commitlint/format@20.4.3", "", { "dependencies": { "@commitlint/types": "^20.4.3", "picocolors": "^1.1.1" } }, "sha512-UDJVErjLbNghop6j111rsHJYGw6MjCKAi95K0GT2yf4eeiDHy3JDRLWYWEjIaFgO+r+dQSkuqgJ1CdMTtrvHsA=="], + "@commitlint/format": ["@commitlint/format@21.2.0", "", { "dependencies": { "@commitlint/types": "^21.2.0", "picocolors": "^1.1.1" } }, "sha512-c4q64xaav2U83t7k7RyzJerBZurPer7FxUOY0RL5L/6CZijZ7K+s6HIBGIghj0ey1P2+seRX0J9XQYtDued6tg=="], - "@commitlint/is-ignored": ["@commitlint/is-ignored@20.4.3", "", { "dependencies": { "@commitlint/types": "^20.4.3", "semver": "^7.6.0" } }, "sha512-W5VQKZ7fdJ1X3Tko+h87YZaqRMGN1KvQKXyCM8xFdxzMIf1KCZgN4uLz3osLB1zsFcVS4ZswHY64LI26/9ACag=="], + "@commitlint/is-ignored": ["@commitlint/is-ignored@21.2.0", "", { "dependencies": { "@commitlint/types": "^21.2.0", "semver": "^7.6.0" } }, "sha512-4/eB0vBN7L88O/oC4ajAEqi7j2ZfNgxl/+11RfAV9YosejZgDXhY2C9VcHnHJhOzPLoSy5P3Mg/46kqeyJfXKw=="], - "@commitlint/lint": ["@commitlint/lint@20.4.3", "", { "dependencies": { "@commitlint/is-ignored": "^20.4.3", "@commitlint/parse": "^20.4.3", "@commitlint/rules": "^20.4.3", "@commitlint/types": "^20.4.3" } }, "sha512-CYOXL23e+nRKij81+d0+dymtIi7Owl9QzvblJYbEfInON/4MaETNSLFDI74LDu+YJ0ML5HZyw9Vhp9QpckwQ0A=="], + "@commitlint/lint": ["@commitlint/lint@21.2.0", "", { "dependencies": { "@commitlint/is-ignored": "^21.2.0", "@commitlint/parse": "^21.2.0", "@commitlint/rules": "^21.2.0", "@commitlint/types": "^21.2.0" } }, "sha512-ceO5dp9pLjEZ6y6qbq/uXWXDPykqqlTsyzoQ0NzecpisSJhK3kTy9qzQoPeJuWG/IMNdV1lO0RgmzqoAlSi1uw=="], - "@commitlint/load": ["@commitlint/load@20.4.3", "", { "dependencies": { "@commitlint/config-validator": "^20.4.3", "@commitlint/execute-rule": "^20.0.0", "@commitlint/resolve-extends": "^20.4.3", "@commitlint/types": "^20.4.3", "cosmiconfig": "^9.0.1", "cosmiconfig-typescript-loader": "^6.1.0", "is-plain-obj": "^4.1.0", "lodash.mergewith": "^4.6.2", "picocolors": "^1.1.1" } }, "sha512-3cdJOUVP+VcgHa7bhJoWS+Z8mBNXB5aLWMBu7Q7uX8PSeWDzdbrBlR33J1MGGf7r1PZDp+mPPiFktk031PgdRw=="], + "@commitlint/load": ["@commitlint/load@21.2.0", "", { "dependencies": { "@commitlint/config-validator": "^21.2.0", "@commitlint/execute-rule": "^21.0.1", "@commitlint/resolve-extends": "^21.2.0", "@commitlint/types": "^21.2.0", "cosmiconfig": "^9.0.1", "cosmiconfig-typescript-loader": "^6.1.0", "es-toolkit": "^1.46.0", "is-plain-obj": "^4.1.0", "picocolors": "^1.1.1" } }, "sha512-RjlzWQqruRwIenJEfZtq7kG97co97nKoHpflE5YnF61tDLXxHPrdWImgzw6VL6MlFyaOcVlk74eBV8ZQmc3oIA=="], - "@commitlint/message": ["@commitlint/message@20.4.3", "", {}, "sha512-6akwCYrzcrFcTYz9GyUaWlhisY4lmQ3KvrnabmhoeAV8nRH4dXJAh4+EUQ3uArtxxKQkvxJS78hNX2EU3USgxQ=="], + "@commitlint/message": ["@commitlint/message@21.2.0", "", {}, "sha512-YxGoiXD/HXNXLJPrQwE5poXa+XH0CBEm+mdvbHQP0g6MV/dmJyUFCzPNzZbxL93GvZ70TmtTK0Z0/IBpAqHv8g=="], - "@commitlint/parse": ["@commitlint/parse@20.4.3", "", { "dependencies": { "@commitlint/types": "^20.4.3", "conventional-changelog-angular": "^8.2.0", "conventional-commits-parser": "^6.3.0" } }, "sha512-hzC3JCo3zs3VkQ833KnGVuWjWIzR72BWZWjQM7tY/7dfKreKAm7fEsy71tIFCRtxf2RtMP2d3RLF1U9yhFSccA=="], + "@commitlint/parse": ["@commitlint/parse@21.2.0", "", { "dependencies": { "@commitlint/types": "^21.2.0", "conventional-changelog-angular": "^9.0.0", "conventional-commits-parser": "^7.0.0" } }, "sha512-QHWxG4d0PLTF634/AdyZ0MQS+CLn5YOuJlCFhMMlSGKFxzYGUetkHBj18xgBD+6fVzUrA2lrCdi/vlS2f/oYXg=="], - "@commitlint/read": ["@commitlint/read@20.4.3", "", { "dependencies": { "@commitlint/top-level": "^20.4.3", "@commitlint/types": "^20.4.3", "git-raw-commits": "^4.0.0", "minimist": "^1.2.8", "tinyexec": "^1.0.0" } }, "sha512-j42OWv3L31WfnP8WquVjHZRt03w50Y/gEE8FAyih7GQTrIv2+pZ6VZ6pWLD/ml/3PO+RV2SPtRtTp/MvlTb8rQ=="], + "@commitlint/read": ["@commitlint/read@21.2.1", "", { "dependencies": { "@commitlint/top-level": "^21.2.0", "@commitlint/types": "^21.2.0", "@conventional-changelog/git-client": "^3.0.0", "tinyexec": "^1.0.0" } }, "sha512-hUW7EJQnNTL0vPOmVMNK4CrnrNBN0nN+JJHReFkdHO5y4iyHeEmTBwuC15OCqUTjxWo7idnH1LftfpWVIaPWIA=="], - "@commitlint/resolve-extends": ["@commitlint/resolve-extends@20.4.3", "", { "dependencies": { "@commitlint/config-validator": "^20.4.3", "@commitlint/types": "^20.4.3", "global-directory": "^4.0.1", "import-meta-resolve": "^4.0.0", "lodash.mergewith": "^4.6.2", "resolve-from": "^5.0.0" } }, "sha512-QucxcOy+00FhS9s4Uy0OyS5HeUV+hbC6OLqkTSIm6fwMdKva+OEavaCDuLtgd9akZZlsUo//XzSmPP3sLKBPog=="], + "@commitlint/resolve-extends": ["@commitlint/resolve-extends@21.2.0", "", { "dependencies": { "@commitlint/config-validator": "^21.2.0", "@commitlint/types": "^21.2.0", "es-toolkit": "^1.46.0", "global-directory": "^5.0.0", "resolve-from": "^5.0.0" } }, "sha512-4O/1j51+79Wth9s/MGxt/5gs0XYLDgNlYpltQfhAvLE0itusLKs9zruxbiNg1oOkmkb9L9L4USYGjEj7n87NxA=="], - "@commitlint/rules": ["@commitlint/rules@20.4.3", "", { "dependencies": { "@commitlint/ensure": "^20.4.3", "@commitlint/message": "^20.4.3", "@commitlint/to-lines": "^20.0.0", "@commitlint/types": "^20.4.3" } }, "sha512-Yuosd7Grn5qiT7FovngXLyRXTMUbj9PYiSkvUgWK1B5a7+ZvrbWDS7epeUapYNYatCy/KTpPFPbgLUdE+MUrBg=="], + "@commitlint/rules": ["@commitlint/rules@21.2.0", "", { "dependencies": { "@commitlint/ensure": "^21.2.0", "@commitlint/message": "^21.2.0", "@commitlint/to-lines": "^21.0.1", "@commitlint/types": "^21.2.0" } }, "sha512-C2yXMNpiB8ETZKfx5JD8+ExgF8vTU1VQMKPSUUYwqKpw9oJWQBrlXBpdU038mj2WPjof7o9UzFpmTyBeGMZwZg=="], - "@commitlint/to-lines": ["@commitlint/to-lines@20.0.0", "", {}, "sha512-2l9gmwiCRqZNWgV+pX1X7z4yP0b3ex/86UmUFgoRt672Ez6cAM2lOQeHFRUTuE6sPpi8XBCGnd8Kh3bMoyHwJw=="], + "@commitlint/to-lines": ["@commitlint/to-lines@21.0.1", "", {}, "sha512-bd1BFII7p1EQZre9Kaj+kKaMFP3cFCdt21K7DItVux9XP5WjLgJ0/Uy1pJJh9aPwVJ6SKg62PxqlZaHI8hQAXw=="], - "@commitlint/top-level": ["@commitlint/top-level@20.4.3", "", { "dependencies": { "escalade": "^3.2.0" } }, "sha512-qD9xfP6dFg5jQ3NMrOhG0/w5y3bBUsVGyJvXxdWEwBm8hyx4WOk3kKXw28T5czBYvyeCVJgJJ6aoJZUWDpaacQ=="], + "@commitlint/top-level": ["@commitlint/top-level@21.2.0", "", { "dependencies": { "escalade": "^3.2.0" } }, "sha512-Y5gmQ+KxzqCrBFJfLvFEPvvwD3LDiNZoTT2yeFBm96M8qhmqSzQc5DvX3rheAaAMjyIvMXOCLS/mWfdpONsjyQ=="], - "@commitlint/types": ["@commitlint/types@20.4.3", "", { "dependencies": { "conventional-commits-parser": "^6.3.0", "picocolors": "^1.1.1" } }, "sha512-51OWa1Gi6ODOasPmfJPq6js4pZoomima4XLZZCrkldaH2V5Nb3bVhNXPeT6XV0gubbainSpTw4zi68NqAeCNCg=="], + "@commitlint/types": ["@commitlint/types@21.2.0", "", { "dependencies": { "conventional-commits-parser": "^7.0.0", "picocolors": "^1.1.1" } }, "sha512-7zVFCDB2reMvJH5dmbKnOQPjZEvjdJTH8jc0U/PIPU1r3/+vf5pD1HlfitV2MWsWXrvu7u39iY1lyLUPOaN0Gw=="], - "@emnapi/core": ["@emnapi/core@1.8.1", "", { "dependencies": { "@emnapi/wasi-threads": "1.1.0", "tslib": "^2.4.0" } }, "sha512-AvT9QFpxK0Zd8J0jopedNm+w/2fIzvtPKPjqyw9jwvBaReTTqPBk9Hixaz7KbjimP+QNz605/XnjFcDAL2pqBg=="], + "@conventional-changelog/git-client": ["@conventional-changelog/git-client@3.1.0", "", { "dependencies": { "@simple-libs/child-process-utils": "^2.0.0", "@simple-libs/stream-utils": "^2.0.0", "semver": "^7.5.2" }, "peerDependencies": { "conventional-commits-filter": "^6.0.1", "conventional-commits-parser": "^7.0.1" }, "optionalPeers": ["conventional-commits-filter", "conventional-commits-parser"] }, "sha512-Tqa/gHco2WJWa740NRjOrfKVvzIqxkZpecb8bemaQ8sKM5PXb1UK4uTyTb/1wIqNuOVaDOFxyBdhTIQZn6gdjQ=="], - "@emnapi/runtime": ["@emnapi/runtime@1.8.1", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-mehfKSMWjjNol8659Z8KxEMrdSJDDot5SXMq00dM8BN4o+CLNXQ0xH2V7EchNHV4RmbZLmmPdEaXZc5H2FXmDg=="], + "@conventional-changelog/template": ["@conventional-changelog/template@1.2.1", "", {}, "sha512-TzlTVpKPjaqW6qOYjQcYUDuGsLCNsvFHVBXkYGTAnf5V37jCWrE5haKNXzz0WZUtVHjrpV76L1buANjwXMfT8w=="], - "@emnapi/wasi-threads": ["@emnapi/wasi-threads@1.1.0", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-WI0DdZ8xFSbgMjR1sFsKABJ/C5OnRrjT06JXbZKexJGrDuPTzZdDYfFlsgcCXCyf+suG5QU2e/y1Wo2V/OapLQ=="], + "@emnapi/core": ["@emnapi/core@1.11.2", "", { "dependencies": { "@emnapi/wasi-threads": "1.2.2", "tslib": "^2.4.0" } }, "sha512-TC8MkTuZUtcTSiFeuC0ksCh9QIJ5+F21MvZ4Wn4ORfYaFJ/0dsiudv5tVkejgwZlwQ39jL9WWDe2lz8x0WglOA=="], - "@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.27.3", "", { "os": "aix", "cpu": "ppc64" }, "sha512-9fJMTNFTWZMh5qwrBItuziu834eOCUcEqymSH7pY+zoMVEZg3gcPuBNxH1EvfVYe9h0x/Ptw8KBzv7qxb7l8dg=="], + "@emnapi/runtime": ["@emnapi/runtime@1.11.2", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-kyOl3X0DuTiT1h2ft8r2fYO8JYtU9a9Xis/zBSiGArNaagCOWx90N1k2wxp18czFDH+OgcWGb5ZP/XMt3dcyPA=="], - "@esbuild/android-arm": ["@esbuild/android-arm@0.27.3", "", { "os": "android", "cpu": "arm" }, "sha512-i5D1hPY7GIQmXlXhs2w8AWHhenb00+GxjxRncS2ZM7YNVGNfaMxgzSGuO8o8SJzRc/oZwU2bcScvVERk03QhzA=="], + "@emnapi/wasi-threads": ["@emnapi/wasi-threads@1.2.2", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-c95qOXkHdydNKhscBTebqEC1CVAZpyqOfVfBzQ1qgzyl3gfeldUjIggDbIZgDKsHLgnsM+igH7TJ/eAasaVuMA=="], - "@esbuild/android-arm64": ["@esbuild/android-arm64@0.27.3", "", { "os": "android", "cpu": "arm64" }, "sha512-YdghPYUmj/FX2SYKJ0OZxf+iaKgMsKHVPF1MAq/P8WirnSpCStzKJFjOjzsW0QQ7oIAiccHdcqjbHmJxRb/dmg=="], + "@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.28.1", "", { "os": "aix", "cpu": "ppc64" }, "sha512-Svl7tq8k/08+p6CXPpRjQ1fKX+1odH/BQbb48fV6fj3CWHhsoIOoY87w1oHXm0qEpkIK3ZfVgp0hed3XBXzXMQ=="], - "@esbuild/android-x64": ["@esbuild/android-x64@0.27.3", "", { "os": "android", "cpu": "x64" }, "sha512-IN/0BNTkHtk8lkOM8JWAYFg4ORxBkZQf9zXiEOfERX/CzxW3Vg1ewAhU7QSWQpVIzTW+b8Xy+lGzdYXV6UZObQ=="], + "@esbuild/android-arm": ["@esbuild/android-arm@0.28.1", "", { "os": "android", "cpu": "arm" }, "sha512-0k2F129Xdio1TdJfzJ8sy1Q47vUD2NnwdhiAf7drUN1EBTfPf4hsFCtmMgu/6m8JSzsBrlmVjudMBQqOfG8usQ=="], - "@esbuild/darwin-arm64": ["@esbuild/darwin-arm64@0.27.3", "", { "os": "darwin", "cpu": "arm64" }, "sha512-Re491k7ByTVRy0t3EKWajdLIr0gz2kKKfzafkth4Q8A5n1xTHrkqZgLLjFEHVD+AXdUGgQMq+Godfq45mGpCKg=="], + "@esbuild/android-arm64": ["@esbuild/android-arm64@0.28.1", "", { "os": "android", "cpu": "arm64" }, "sha512-34EGEbCIAgosYz6goLcopX6Mo7NyGv9tfwEM2/7Ce2VcVRk568iSvniGWcUXIy7wEDR1wzolcxcriFVrWYcwBg=="], - "@esbuild/darwin-x64": ["@esbuild/darwin-x64@0.27.3", "", { "os": "darwin", "cpu": "x64" }, "sha512-vHk/hA7/1AckjGzRqi6wbo+jaShzRowYip6rt6q7VYEDX4LEy1pZfDpdxCBnGtl+A5zq8iXDcyuxwtv3hNtHFg=="], + "@esbuild/android-x64": ["@esbuild/android-x64@0.28.1", "", { "os": "android", "cpu": "x64" }, "sha512-dbwY7ltSMDWsRatcRpCnES4F+im88OCUgGZjy52shC7GqHRE/cYlxNbB4Z4UpJswpcc4Qxd2oE/ufM0p61IKng=="], - "@esbuild/freebsd-arm64": ["@esbuild/freebsd-arm64@0.27.3", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-ipTYM2fjt3kQAYOvo6vcxJx3nBYAzPjgTCk7QEgZG8AUO3ydUhvelmhrbOheMnGOlaSFUoHXB6un+A7q4ygY9w=="], + "@esbuild/darwin-arm64": ["@esbuild/darwin-arm64@0.28.1", "", { "os": "darwin", "cpu": "arm64" }, "sha512-TZbWkQY7kvTAXbXUT7uVACR5cMHsDiSz9z7ZKAX/RTq/WJEk3QyRr0wZpNhBDX+/0CtdqUIJlOiodQcta6tY3Q=="], - "@esbuild/freebsd-x64": ["@esbuild/freebsd-x64@0.27.3", "", { "os": "freebsd", "cpu": "x64" }, "sha512-dDk0X87T7mI6U3K9VjWtHOXqwAMJBNN2r7bejDsc+j03SEjtD9HrOl8gVFByeM0aJksoUuUVU9TBaZa2rgj0oA=="], + "@esbuild/darwin-x64": ["@esbuild/darwin-x64@0.28.1", "", { "os": "darwin", "cpu": "x64" }, "sha512-zfdzgK9ACBNZLI/CyHTOx81SyNbM6YXn7rxSgX97VjyiPl9W1i4Ka4fgKECEoFCKGpvBj5qArWIGgQjOwkgskQ=="], - "@esbuild/linux-arm": ["@esbuild/linux-arm@0.27.3", "", { "os": "linux", "cpu": "arm" }, "sha512-s6nPv2QkSupJwLYyfS+gwdirm0ukyTFNl3KTgZEAiJDd+iHZcbTPPcWCcRYH+WlNbwChgH2QkE9NSlNrMT8Gfw=="], + "@esbuild/freebsd-arm64": ["@esbuild/freebsd-arm64@0.28.1", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-wG2EA8ENdEI0qhkSZMjfqrdY+ziCYCPMmtZjjIwOmXFjmyzEHn+UUxk5of+SYsjtfs3VpnlC7QLzSI5hY/rOAw=="], - "@esbuild/linux-arm64": ["@esbuild/linux-arm64@0.27.3", "", { "os": "linux", "cpu": "arm64" }, "sha512-sZOuFz/xWnZ4KH3YfFrKCf1WyPZHakVzTiqji3WDc0BCl2kBwiJLCXpzLzUBLgmp4veFZdvN5ChW4Eq/8Fc2Fg=="], + "@esbuild/freebsd-x64": ["@esbuild/freebsd-x64@0.28.1", "", { "os": "freebsd", "cpu": "x64" }, "sha512-i7dZ9vQgnvSCzi/rYCXNgtF/U+eKZNJBzu3eTQbRgHnM7tNSizLOkRFAl3qzVc/Op/u5YkHHa4pf/3DOYHthLQ=="], - "@esbuild/linux-ia32": ["@esbuild/linux-ia32@0.27.3", "", { "os": "linux", "cpu": "ia32" }, "sha512-yGlQYjdxtLdh0a3jHjuwOrxQjOZYD/C9PfdbgJJF3TIZWnm/tMd/RcNiLngiu4iwcBAOezdnSLAwQDPqTmtTYg=="], + "@esbuild/linux-arm": ["@esbuild/linux-arm@0.28.1", "", { "os": "linux", "cpu": "arm" }, "sha512-qVXBOHQS+d5Y722GwJzJUtOLlX7km3CraOaGormF1pDtPd2C/l1SHRPgjLunLGe51Sh5YYWKMFDyV4SxgMQYTQ=="], - "@esbuild/linux-loong64": ["@esbuild/linux-loong64@0.27.3", "", { "os": "linux", "cpu": "none" }, "sha512-WO60Sn8ly3gtzhyjATDgieJNet/KqsDlX5nRC5Y3oTFcS1l0KWba+SEa9Ja1GfDqSF1z6hif/SkpQJbL63cgOA=="], + "@esbuild/linux-arm64": ["@esbuild/linux-arm64@0.28.1", "", { "os": "linux", "cpu": "arm64" }, "sha512-yHs+0uc8+nvEAfAfxrWQKK5peSNzBc4PegcMO0EJ2hT71uA7vB8Ihg2e77R2P7SG5uYjPbHlLLmve4LLLRCf0g=="], - "@esbuild/linux-mips64el": ["@esbuild/linux-mips64el@0.27.3", "", { "os": "linux", "cpu": "none" }, "sha512-APsymYA6sGcZ4pD6k+UxbDjOFSvPWyZhjaiPyl/f79xKxwTnrn5QUnXR5prvetuaSMsb4jgeHewIDCIWljrSxw=="], + "@esbuild/linux-ia32": ["@esbuild/linux-ia32@0.28.1", "", { "os": "linux", "cpu": "ia32" }, "sha512-d1z4ZuP0ajrfz/FhGT4vv278rX8KnPPJx8i5+AtK7TYbx9Le9F1hyzurZpkEyjkGa9dUGhQow4C1NmeGvqxN2w=="], - "@esbuild/linux-ppc64": ["@esbuild/linux-ppc64@0.27.3", "", { "os": "linux", "cpu": "ppc64" }, "sha512-eizBnTeBefojtDb9nSh4vvVQ3V9Qf9Df01PfawPcRzJH4gFSgrObw+LveUyDoKU3kxi5+9RJTCWlj4FjYXVPEA=="], + "@esbuild/linux-loong64": ["@esbuild/linux-loong64@0.28.1", "", { "os": "linux", "cpu": "none" }, "sha512-M5sRjUVZrkm1OAPR3dlOYzNmN+loZKGVi1VUQGrwuqLcbR6qeAz+famMhjASeH3YVKvZz+zT1jlh/keC3Rj/lg=="], - "@esbuild/linux-riscv64": ["@esbuild/linux-riscv64@0.27.3", "", { "os": "linux", "cpu": "none" }, "sha512-3Emwh0r5wmfm3ssTWRQSyVhbOHvqegUDRd0WhmXKX2mkHJe1SFCMJhagUleMq+Uci34wLSipf8Lagt4LlpRFWQ=="], + "@esbuild/linux-mips64el": ["@esbuild/linux-mips64el@0.28.1", "", { "os": "linux", "cpu": "none" }, "sha512-mRObBZeHh2OxcBFPWE/FjylkRgZdYuiTR3vaTozquCGOH14iP9oN4x4Ge81CoIDYQrXmIxpFumJBu5MtZpnQJQ=="], - "@esbuild/linux-s390x": ["@esbuild/linux-s390x@0.27.3", "", { "os": "linux", "cpu": "s390x" }, "sha512-pBHUx9LzXWBc7MFIEEL0yD/ZVtNgLytvx60gES28GcWMqil8ElCYR4kvbV2BDqsHOvVDRrOxGySBM9Fcv744hw=="], + "@esbuild/linux-ppc64": ["@esbuild/linux-ppc64@0.28.1", "", { "os": "linux", "cpu": "ppc64" }, "sha512-slScBsMAb3GFDcdrCgLwZtPYRoH2H/youv10QiZyRjmsP48fznoveWytSgCI/R0ZcUgpc0ZhIUEx6LHts8yrfQ=="], - "@esbuild/linux-x64": ["@esbuild/linux-x64@0.27.3", "", { "os": "linux", "cpu": "x64" }, "sha512-Czi8yzXUWIQYAtL/2y6vogER8pvcsOsk5cpwL4Gk5nJqH5UZiVByIY8Eorm5R13gq+DQKYg0+JyQoytLQas4dA=="], + "@esbuild/linux-riscv64": ["@esbuild/linux-riscv64@0.28.1", "", { "os": "linux", "cpu": "none" }, "sha512-kw0owk1o0GFETUJyW0jc0G4Yzs0BHZn0JDZ8JRT088vjJYX777BAs1fDGxAC+q831qOs2DTC96mNsG2opdfyyQ=="], - "@esbuild/netbsd-arm64": ["@esbuild/netbsd-arm64@0.27.3", "", { "os": "none", "cpu": "arm64" }, "sha512-sDpk0RgmTCR/5HguIZa9n9u+HVKf40fbEUt+iTzSnCaGvY9kFP0YKBWZtJaraonFnqef5SlJ8/TiPAxzyS+UoA=="], + "@esbuild/linux-s390x": ["@esbuild/linux-s390x@0.28.1", "", { "os": "linux", "cpu": "s390x" }, "sha512-/lAIjX8aYFRByhh6L5rYtPEDRqa9de/4V/juOXcta5frjvzXO4/sqEtyytse0g3zZFuWu5cDN0MkLz2qRDD2Ag=="], - "@esbuild/netbsd-x64": ["@esbuild/netbsd-x64@0.27.3", "", { "os": "none", "cpu": "x64" }, "sha512-P14lFKJl/DdaE00LItAukUdZO5iqNH7+PjoBm+fLQjtxfcfFE20Xf5CrLsmZdq5LFFZzb5JMZ9grUwvtVYzjiA=="], + "@esbuild/linux-x64": ["@esbuild/linux-x64@0.28.1", "", { "os": "linux", "cpu": "x64" }, "sha512-u/anNYF2mmVOEDwLtnQ1wOr3EZ9sTNGLWrsYGYwHWzGA3Si84IOkHXlbWTD1NB+9/1lcnweYKO54uhxZydNzfA=="], - "@esbuild/openbsd-arm64": ["@esbuild/openbsd-arm64@0.27.3", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-AIcMP77AvirGbRl/UZFTq5hjXK+2wC7qFRGoHSDrZ5v5b8DK/GYpXW3CPRL53NkvDqb9D+alBiC/dV0Fb7eJcw=="], + "@esbuild/netbsd-arm64": ["@esbuild/netbsd-arm64@0.28.1", "", { "os": "none", "cpu": "arm64" }, "sha512-oks0DYbLwWMmaakTsCb+zL4E+aHRVLom9IJZOAthMQEPiQmydXHkziYEsGYRx0uNV/IjEKGAV941JzH02pflqw=="], - "@esbuild/openbsd-x64": ["@esbuild/openbsd-x64@0.27.3", "", { "os": "openbsd", "cpu": "x64" }, "sha512-DnW2sRrBzA+YnE70LKqnM3P+z8vehfJWHXECbwBmH/CU51z6FiqTQTHFenPlHmo3a8UgpLyH3PT+87OViOh1AQ=="], + "@esbuild/netbsd-x64": ["@esbuild/netbsd-x64@0.28.1", "", { "os": "none", "cpu": "x64" }, "sha512-aeL6lAnN89Hz43Mlh1G8ARasbuoYvSITDEx0tHh5b7jJnHcssqgjy9Yx430GDpmCa6OyrKoS0aNRjKundRizGg=="], - "@esbuild/openharmony-arm64": ["@esbuild/openharmony-arm64@0.27.3", "", { "os": "none", "cpu": "arm64" }, "sha512-NinAEgr/etERPTsZJ7aEZQvvg/A6IsZG/LgZy+81wON2huV7SrK3e63dU0XhyZP4RKGyTm7aOgmQk0bGp0fy2g=="], + "@esbuild/openbsd-arm64": ["@esbuild/openbsd-arm64@0.28.1", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-MEFJe5C3R8pwXdZ5Y21oo6m7ePiS0d9pWucn99O/wvyJZChoIQKrQDxKrGeW8F5+T0okTHesAmDeiHDTIq0V/Q=="], - "@esbuild/sunos-x64": ["@esbuild/sunos-x64@0.27.3", "", { "os": "sunos", "cpu": "x64" }, "sha512-PanZ+nEz+eWoBJ8/f8HKxTTD172SKwdXebZ0ndd953gt1HRBbhMsaNqjTyYLGLPdoWHy4zLU7bDVJztF5f3BHA=="], + "@esbuild/openbsd-x64": ["@esbuild/openbsd-x64@0.28.1", "", { "os": "openbsd", "cpu": "x64" }, "sha512-i/ZLIOafE0Z8cI/XANJAixoJL/uRAoS2xOA3rb0xN+KK0K177cMAsQYkzHtBrtMXAKuAc7HGgcWiZ/sRC1Nxgw=="], - "@esbuild/win32-arm64": ["@esbuild/win32-arm64@0.27.3", "", { "os": "win32", "cpu": "arm64" }, "sha512-B2t59lWWYrbRDw/tjiWOuzSsFh1Y/E95ofKz7rIVYSQkUYBjfSgf6oeYPNWHToFRr2zx52JKApIcAS/D5TUBnA=="], + "@esbuild/openharmony-arm64": ["@esbuild/openharmony-arm64@0.28.1", "", { "os": "none", "cpu": "arm64" }, "sha512-ge+Z7EXFNt2BO1oAMsVpiQ8EwndV9i1xXerAeTIK7AtPs3bKFXQM7nlRxDSIUIMeueR1CNXxqztLzdNeReKBJg=="], - "@esbuild/win32-ia32": ["@esbuild/win32-ia32@0.27.3", "", { "os": "win32", "cpu": "ia32" }, "sha512-QLKSFeXNS8+tHW7tZpMtjlNb7HKau0QDpwm49u0vUp9y1WOF+PEzkU84y9GqYaAVW8aH8f3GcBck26jh54cX4Q=="], + "@esbuild/sunos-x64": ["@esbuild/sunos-x64@0.28.1", "", { "os": "sunos", "cpu": "x64" }, "sha512-BEjgtECkL3vY+SaSQ6nzVfiALUeFxpawyp8Jmf5PtYhf1Ug40N1h/hxlhts+f1FvSvarEigdxS3BlSMI2PJLcQ=="], - "@esbuild/win32-x64": ["@esbuild/win32-x64@0.27.3", "", { "os": "win32", "cpu": "x64" }, "sha512-4uJGhsxuptu3OcpVAzli+/gWusVGwZZHTlS63hh++ehExkVT8SgiEf7/uC/PclrPPkLhZqGgCTjd0VWLo6xMqA=="], + "@esbuild/win32-arm64": ["@esbuild/win32-arm64@0.28.1", "", { "os": "win32", "cpu": "arm64" }, "sha512-lCv9eK/H6ZJWbE7bh2nw54CZ9M2nupBxJcTsdk/QQnWkdSjKGuxmmH8/GWrlT1eMmZfn4dGcCjRte397WqfQXA=="], + + "@esbuild/win32-ia32": ["@esbuild/win32-ia32@0.28.1", "", { "os": "win32", "cpu": "ia32" }, "sha512-zvb/mB2bSCoJOpoCBgYKKpX6YM6mJBlBUVUtVj41DlZJVEB6/0CKlRYxP5wWl1C1ILiCoAU5wZZ4q1P3qeS6Eg=="], + + "@esbuild/win32-x64": ["@esbuild/win32-x64@0.28.1", "", { "os": "win32", "cpu": "x64" }, "sha512-bm4Mowrv+GXMlpWX++EcXw/iLyd1o3+bJkC2DkWXYVvgZCqD/bSj9ctZeAMC3cIxgjRVR2Dufaiu4YPxr5gW1A=="], "@inquirer/ansi": ["@inquirer/ansi@1.0.2", "", {}, "sha512-S8qNSZiYzFd0wAcyG5AXCvUHC5Sr7xpZ9wZ2py9XR88jUz8wooStVx5M6dRzczbBWjic9NP7+rY0Xi7qqK/aMQ=="], @@ -175,7 +182,15 @@ "@inquirer/type": ["@inquirer/type@3.0.10", "", { "peerDependencies": { "@types/node": ">=18" }, "optionalPeers": ["@types/node"] }, "sha512-BvziSRxfz5Ov8ch0z/n3oijRSEcEsHnhggm4xFZe93DHcUCTlutlq9Ox4SVENAfcRD22UQq7T/atg9Wr3k09eA=="], - "@napi-rs/wasm-runtime": ["@napi-rs/wasm-runtime@1.1.1", "", { "dependencies": { "@emnapi/core": "^1.7.1", "@emnapi/runtime": "^1.7.1", "@tybys/wasm-util": "^0.10.1" } }, "sha512-p64ah1M1ld8xjWv3qbvFwHiFVWrq1yFvV4f7w+mzaqiR4IlSgkqhcRdHwsGgomwzBH51sRY4NEowLxnaBjcW/A=="], + "@napi-rs/wasm-runtime": ["@napi-rs/wasm-runtime@1.1.6", "", { "dependencies": { "@tybys/wasm-util": "^0.10.3" }, "peerDependencies": { "@emnapi/core": "^1.7.1", "@emnapi/runtime": "^1.7.1" } }, "sha512-ZLv/JdUfkvOy9eCnnBaGfiO+XimbjebAeO+MRQqD/B+FR1tnRN0tpKSJHRbE8sFfS6aqsXZ67TQjfwfsxULVbg=="], + + "@nats-io/nats-core": ["@nats-io/nats-core@3.4.0", "", { "dependencies": { "@nats-io/nkeys": "2.0.3", "@nats-io/nuid": "3.0.0" } }, "sha512-QMDM86EUNm+wudlKC67HLar/KHHQUvJGLfb4jbahje3pUk3K9afeck9fwsxBZF0eUFox6T/TA6m/t+lQqf+QsQ=="], + + "@nats-io/nkeys": ["@nats-io/nkeys@2.0.3", "", { "dependencies": { "tweetnacl": "^1.0.3" } }, "sha512-JVt56GuE6Z89KUkI4TXUbSI9fmIfAmk6PMPknijmuL72GcD+UgIomTcRWiNvvJKxA01sBbmIPStqJs5cMRBC3A=="], + + "@nats-io/nuid": ["@nats-io/nuid@3.0.0", "", {}, "sha512-QbXZDrxmYlrn5AD06gYcUTmEHwxn96HBQMIk7XTjDlEcx6FPzVBBPjp4AMRh1lEv6B4iJ6Xb/Uz61JugPXFRQg=="], + + "@nats-io/transport-node": ["@nats-io/transport-node@3.4.0", "", { "dependencies": { "@nats-io/nats-core": "3.4.0", "@nats-io/nkeys": "2.0.3", "@nats-io/nuid": "3.0.0" } }, "sha512-hH7u7ejIBTFEJIZ8rIcMrHJI6wl+HhpO5sVFs1+ppmXa8RuB2+Lh1+UwTzZ5xTNNm1TKcRkYy+2qCV56qp8RxA=="], "@nodelib/fs.scandir": ["@nodelib/fs.scandir@2.1.5", "", { "dependencies": { "@nodelib/fs.stat": "2.0.5", "run-parallel": "^1.1.9" } }, "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g=="], @@ -191,55 +206,105 @@ "@oozcitak/util": ["@oozcitak/util@10.0.0", "", {}, "sha512-hAX0pT/73190NLqBPPWSdBVGtbY6VOhWYK3qqHqtXQ1gK7kS2yz4+ivsN07hpJ6I3aeMtKP6J6npsEKOAzuTLA=="], - "@oxc-resolver/binding-android-arm-eabi": ["@oxc-resolver/binding-android-arm-eabi@11.19.1", "", { "os": "android", "cpu": "arm" }, "sha512-aUs47y+xyXHUKlbhqHUjBABjvycq6YSD7bpxSW7vplUmdzAlJ93yXY6ZR0c1o1x5A/QKbENCvs3+NlY8IpIVzg=="], + "@oxc-parser/binding-android-arm-eabi": ["@oxc-parser/binding-android-arm-eabi@0.140.0", "", { "os": "android", "cpu": "arm" }, "sha512-ZfjDZ422mo7eo3b3VltqNsV9kmv1qt/sPEAMSl64iOSwhVfd0eIZ9LB79Mbs1xYXJnk7WSROwzBCKDIiVxPTvQ=="], + + "@oxc-parser/binding-android-arm64": ["@oxc-parser/binding-android-arm64@0.140.0", "", { "os": "android", "cpu": "arm64" }, "sha512-Ia8jSvikUX6Sf+Ht+KOCUF/k1HpR0VlmqIYymubmWDebOEGtsyliHDR6JxsZ4IX3/c/GbrB1uh09aVGQv/LQmQ=="], + + "@oxc-parser/binding-darwin-arm64": ["@oxc-parser/binding-darwin-arm64@0.140.0", "", { "os": "darwin", "cpu": "arm64" }, "sha512-G6VK0nK61pH0d0mBjUqSZbVxGqqO5uzeginLDQj+gOO6ObfJjXRwgkD/ol0w1INcnFeAb6YGGO7qc3ueGHaycQ=="], + + "@oxc-parser/binding-darwin-x64": ["@oxc-parser/binding-darwin-x64@0.140.0", "", { "os": "darwin", "cpu": "x64" }, "sha512-HazBOuZzd2pO1C2uMmp8Gv7mhzMHqKSKDS1OZfcLEvpIcgA+48J92HEtNanVHDIzRD9PRPCV6aS6fkZIWOVl8Q=="], + + "@oxc-parser/binding-freebsd-x64": ["@oxc-parser/binding-freebsd-x64@0.140.0", "", { "os": "freebsd", "cpu": "x64" }, "sha512-9hSUU+HmTUyOe4JzMHxNGgLWNY7rrO+6ShicZwImNJacEAACDMIkuEQQkvXSL+WJN50jaNtLYJv8s4OcBdpyUQ=="], + + "@oxc-parser/binding-linux-arm-gnueabihf": ["@oxc-parser/binding-linux-arm-gnueabihf@0.140.0", "", { "os": "linux", "cpu": "arm" }, "sha512-RAEuQsYtS0KcDFqN0ABTjyyNlokS91JeuDuoW9tEbG0JTbRNXnpQUdbYc/16JoA6Z/2ALbNrE3KmxtqDiuIjCQ=="], + + "@oxc-parser/binding-linux-arm-musleabihf": ["@oxc-parser/binding-linux-arm-musleabihf@0.140.0", "", { "os": "linux", "cpu": "arm" }, "sha512-c4CkHvPvqfojouredJ0w3e6+jiBq0SbFyhH61kr/zPb/7XsaYTNKQ54vmlSsopfdQbNDX40ZeK9Abs2Qet6wcw=="], + + "@oxc-parser/binding-linux-arm64-gnu": ["@oxc-parser/binding-linux-arm64-gnu@0.140.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-yrjmLj8ixPB25yqvPGr28meGjb+keed7m1GqqY/0uqkhZIoT4t9zmfwUgFEtC33C7dtE+UQ7TU0IaVxf97SWJg=="], + + "@oxc-parser/binding-linux-arm64-musl": ["@oxc-parser/binding-linux-arm64-musl@0.140.0", "", { "os": "linux", "cpu": "arm64" }, "sha512-ggGMQTN8Agwxp2WiLMpdY671dt0qTDJWiWlJeig3HnUwTnerRl0J2JdGVghWBeDcss2D9S2V2Js6dZHEiVabVA=="], + + "@oxc-parser/binding-linux-ppc64-gnu": ["@oxc-parser/binding-linux-ppc64-gnu@0.140.0", "", { "os": "linux", "cpu": "ppc64" }, "sha512-IgTs8xYAFgAUGNmR65tIqjlJ8vKgrfXzC515e9goSdfMyKQV4aJpd2pUUudU4u51G64H0/DSEJEXKOraxm9ZCA=="], + + "@oxc-parser/binding-linux-riscv64-gnu": ["@oxc-parser/binding-linux-riscv64-gnu@0.140.0", "", { "os": "linux", "cpu": "none" }, "sha512-A1x+PMWZmSGaFVOx2YeNTFau8uD+QO14/vLP4GrcuvUPs3+nBkUOjy9Lus86ftHsDojjYMbvBelmKc3F7Rv08g=="], + + "@oxc-parser/binding-linux-riscv64-musl": ["@oxc-parser/binding-linux-riscv64-musl@0.140.0", "", { "os": "linux", "cpu": "none" }, "sha512-zBqpfRo2myWPrPo5xUjeZqlnPXPXsX8BcWtWff66/eGRQdbPjhzPgXa/F+AtxT2afUViPxbuDlwscMKzQ5tg+g=="], + + "@oxc-parser/binding-linux-s390x-gnu": ["@oxc-parser/binding-linux-s390x-gnu@0.140.0", "", { "os": "linux", "cpu": "s390x" }, "sha512-2M1DPm/8w9I//YzFlFC9qXw+r2tJFh5CYwRlYTq2vUJQS7qoQftEDeCZ8EnN7KHtvSiXvYj8mZI5pR7DpXmcEw=="], + + "@oxc-parser/binding-linux-x64-gnu": ["@oxc-parser/binding-linux-x64-gnu@0.140.0", "", { "os": "linux", "cpu": "x64" }, "sha512-8aRDbZ/U/jO8N7go1MO72jtbpb4uswV8d7vOkMvt/BPgZiyEYvl1VIWK4ESxZZhnJ4tqwVldgX7dNiP/eB1Jdg=="], + + "@oxc-parser/binding-linux-x64-musl": ["@oxc-parser/binding-linux-x64-musl@0.140.0", "", { "os": "linux", "cpu": "x64" }, "sha512-xRqpeI8U2sQQS1W5BMWRyMTxtagkuLG2dEWruet5lFsWHTvBth11/TpSaJatHdqVVwHN0q3uuoS9zRsGinq8hg=="], + + "@oxc-parser/binding-openharmony-arm64": ["@oxc-parser/binding-openharmony-arm64@0.140.0", "", { "os": "none", "cpu": "arm64" }, "sha512-GbGRe26MqAKciFRvXeHNQJ6VAHYs9R4miP89sEAncysM3n+f4lnyLWgsa9kklJNpfnxdq2yRoNYHFqwBckVimw=="], + + "@oxc-parser/binding-wasm32-wasi": ["@oxc-parser/binding-wasm32-wasi@0.140.0", "", { "dependencies": { "@emnapi/core": "1.11.2", "@emnapi/runtime": "1.11.2", "@napi-rs/wasm-runtime": "^1.1.6" }, "cpu": "none" }, "sha512-vFiC1hqys+hkX1GnQkIoiTQJNiUm43Z0lO35ETKXTw0YtpW7+cN58YRRXFAQQ+TgpkIi3lrhcxdlnqz+Oi3ptQ=="], + + "@oxc-parser/binding-win32-arm64-msvc": ["@oxc-parser/binding-win32-arm64-msvc@0.140.0", "", { "os": "win32", "cpu": "arm64" }, "sha512-fGSQldwEYKhM+H8uLt76Op8hh5+FYaR6lvvQ1Txw3Mhn86DyQXLcI0fi1EkFlTK7F+46OCk/j0AJMzZQm6g5Xg=="], + + "@oxc-parser/binding-win32-ia32-msvc": ["@oxc-parser/binding-win32-ia32-msvc@0.140.0", "", { "os": "win32", "cpu": "ia32" }, "sha512-sDS2Bai+g3ZWYwfZqmosiSuFDBcVnZ3Ta6pszzsiJoLMqsJEWKcxXXbGa7b7yXr++W2lQNPb3ZRJ8czseqL7RA=="], + + "@oxc-parser/binding-win32-x64-msvc": ["@oxc-parser/binding-win32-x64-msvc@0.140.0", "", { "os": "win32", "cpu": "x64" }, "sha512-kHbE1zWyb5OQgJA6/5P4WjiuB01sYdQwtZnSSyE58FQEXDAMnyeeq4vj7KgN75i5SlBzOs8A5MrtlD3gOlDKqQ=="], + + "@oxc-project/types": ["@oxc-project/types@0.140.0", "", {}, "sha512-h5LUOzGArYemnW1NMz/DuuQhBi96J6JL2Bk8zE4kvqxB5Sg3jxmCiH4uyOWHDkiKSt5vWlG4FIwCR/DbstcNRQ=="], + + "@oxc-resolver/binding-android-arm-eabi": ["@oxc-resolver/binding-android-arm-eabi@11.24.2", "", { "os": "android", "cpu": "arm" }, "sha512-y09e0L0SRI2OA2tUIrjBgoV3eH5hvUKXNkJqXmNo5V2WxIjyC7I7aJfRLMEVpA8yi95f90gFDvO0VMgrDw+vwA=="], + + "@oxc-resolver/binding-android-arm64": ["@oxc-resolver/binding-android-arm64@11.24.2", "", { "os": "android", "cpu": "arm64" }, "sha512-cl4icWaZFnLdg8m6qtnh5rBMuGbxc/ptStFHLeCNwr+2cZjkjNwQu/jYRS0CHlnPecOJMpuS5M6/BH+0J/YkEg=="], + + "@oxc-resolver/binding-darwin-arm64": ["@oxc-resolver/binding-darwin-arm64@11.24.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-At29QEMF6HajbQvgY8K6OXnHD1x9rad74xBEfmCB6ZqCGsdq75aK7tOYcTbOanMy8qdIBrfL3SMr3p/lfSlb9w=="], + + "@oxc-resolver/binding-darwin-x64": ["@oxc-resolver/binding-darwin-x64@11.24.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-A5Kqr1EUj4oIL5CF4WRssq/o5P0Y11cwoFouMRmQ7YnC/A8V93nv1nb7aSU8HwcgmXropjLNkVTl4MN87cu28Q=="], - "@oxc-resolver/binding-android-arm64": ["@oxc-resolver/binding-android-arm64@11.19.1", "", { "os": "android", "cpu": "arm64" }, "sha512-oolbkRX+m7Pq2LNjr/kKgYeC7bRDMVTWPgxBGMjSpZi/+UskVo4jsMU3MLheZV55jL6c3rNelPl4oD60ggYmqA=="], + "@oxc-resolver/binding-freebsd-x64": ["@oxc-resolver/binding-freebsd-x64@11.24.2", "", { "os": "freebsd", "cpu": "x64" }, "sha512-R5xkRBRRz7ceH/P5Jrc6G7FmdUdgpLYyESFAUDVTNQ9K0sGPxcp4ljiwEwEqsvNcQ4sYbMRrWcHHBCu7ksAJVw=="], - "@oxc-resolver/binding-darwin-arm64": ["@oxc-resolver/binding-darwin-arm64@11.19.1", "", { "os": "darwin", "cpu": "arm64" }, "sha512-nUC6d2i3R5B12sUW4O646qD5cnMXf2oBGPLIIeaRfU9doJRORAbE2SGv4eW6rMqhD+G7nf2Y8TTJTLiiO3Q/dQ=="], + "@oxc-resolver/binding-linux-arm-gnueabihf": ["@oxc-resolver/binding-linux-arm-gnueabihf@11.24.2", "", { "os": "linux", "cpu": "arm" }, "sha512-k/RuYL4L/R58IBn3wT5ma3Wh4k62bp1eYCFRWCmMsasUOqL+H6sW0VGFadEzKWXFFlz+2uIMoeMk9ySSZJHgbg=="], - "@oxc-resolver/binding-darwin-x64": ["@oxc-resolver/binding-darwin-x64@11.19.1", "", { "os": "darwin", "cpu": "x64" }, "sha512-cV50vE5+uAgNcFa3QY1JOeKDSkM/9ReIcc/9wn4TavhW/itkDGrXhw9jaKnkQnGbjJ198Yh5nbX/Gr2mr4Z5jQ=="], + "@oxc-resolver/binding-linux-arm-musleabihf": ["@oxc-resolver/binding-linux-arm-musleabihf@11.24.2", "", { "os": "linux", "cpu": "arm" }, "sha512-bnHAak3ujYfH5pKk4NieFNbvYvernfoQDgwLddbZ3OtMYrem87/qjlA+u+aKG0oZcqSLGCful/6/CEA+aeAgaA=="], - "@oxc-resolver/binding-freebsd-x64": ["@oxc-resolver/binding-freebsd-x64@11.19.1", "", { "os": "freebsd", "cpu": "x64" }, "sha512-xZOQiYGFxtk48PBKff+Zwoym7ScPAIVp4c14lfLxizO2LTTTJe5sx9vQNGrBymrf/vatSPNMD4FgsaaRigPkqw=="], + "@oxc-resolver/binding-linux-arm64-gnu": ["@oxc-resolver/binding-linux-arm64-gnu@11.24.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-vDT3KHgzYp47gmtNOqL2VNhCyl5Zv643eyxm//A68J8DeUGXrvD1pZFiaT4jSfe+RInfnn1R2yVHye4enx6RnA=="], - "@oxc-resolver/binding-linux-arm-gnueabihf": ["@oxc-resolver/binding-linux-arm-gnueabihf@11.19.1", "", { "os": "linux", "cpu": "arm" }, "sha512-lXZYWAC6kaGe/ky2su94e9jN9t6M0/6c+GrSlCqL//XO1cxi5lpAhnJYdyrKfm0ZEr/c7RNyAx3P7FSBcBd5+A=="], + "@oxc-resolver/binding-linux-arm64-musl": ["@oxc-resolver/binding-linux-arm64-musl@11.24.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-+kMlQvbzfyEYtu5FcjE4p+ttBLpKW4d/AsAsuE69BxV6V4twZJeIQZFfD8gh/wqglY0MkPSezWXQH0jBV13MUw=="], - "@oxc-resolver/binding-linux-arm-musleabihf": ["@oxc-resolver/binding-linux-arm-musleabihf@11.19.1", "", { "os": "linux", "cpu": "arm" }, "sha512-veG1kKsuK5+t2IsO9q0DErYVSw2azvCVvWHnfTOS73WE0STdLLB7Q1bB9WR+yHPQM76ASkFyRbogWo1GR1+WbQ=="], + "@oxc-resolver/binding-linux-ppc64-gnu": ["@oxc-resolver/binding-linux-ppc64-gnu@11.24.2", "", { "os": "linux", "cpu": "ppc64" }, "sha512-shjfMhmZ3gq9fv/w7bi3PnZlgOPG+2QAOFf0BJF0EgBSIGZ6PMLN2zbGEblTUYB/NKVDRyYhE2ff3dJ1QqNPkA=="], - "@oxc-resolver/binding-linux-arm64-gnu": ["@oxc-resolver/binding-linux-arm64-gnu@11.19.1", "", { "os": "linux", "cpu": "arm64" }, "sha512-heV2+jmXyYnUrpUXSPugqWDRpnsQcDm2AX4wzTuvgdlZfoNYO0O3W2AVpJYaDn9AG4JdM6Kxom8+foE7/BcSig=="], + "@oxc-resolver/binding-linux-riscv64-gnu": ["@oxc-resolver/binding-linux-riscv64-gnu@11.24.2", "", { "os": "linux", "cpu": "none" }, "sha512-zGelwFR5oRo+b69k8Lrzun86DyUHzfKN6cnjbR9l7Z7NIRznOE/2ZvPa1IUKqAL2PzAXOdwkfVqNvO1H2RlpAw=="], - "@oxc-resolver/binding-linux-arm64-musl": ["@oxc-resolver/binding-linux-arm64-musl@11.19.1", "", { "os": "linux", "cpu": "arm64" }, "sha512-jvo2Pjs1c9KPxMuMPIeQsgu0mOJF9rEb3y3TdpsrqwxRM+AN6/nDDwv45n5ZrUnQMsdBy5gIabioMKnQfWo9ew=="], + "@oxc-resolver/binding-linux-riscv64-musl": ["@oxc-resolver/binding-linux-riscv64-musl@11.24.2", "", { "os": "linux", "cpu": "none" }, "sha512-qxZ1SWCXJY0eyhAlP6Lmo9F2Nrtx7EkYj9oCgL8apDPCwXwCEDA2U697bbT81JIc2IrVjxO4KX6WU2N+oN9Z4w=="], - "@oxc-resolver/binding-linux-ppc64-gnu": ["@oxc-resolver/binding-linux-ppc64-gnu@11.19.1", "", { "os": "linux", "cpu": "ppc64" }, "sha512-vLmdNxWCdN7Uo5suays6A/+ywBby2PWBBPXctWPg5V0+eVuzsJxgAn6MMB4mPlshskYbppjpN2Zg83ArHze9gQ=="], + "@oxc-resolver/binding-linux-s390x-gnu": ["@oxc-resolver/binding-linux-s390x-gnu@11.24.2", "", { "os": "linux", "cpu": "s390x" }, "sha512-sGCecF3cx2DFlH4t/z7ApnOnXqN48p5p5mlHDEnHTAukQa2P+qMVE4CwyWE9W+q/m3QJ7kKfGrIjax31f44oFQ=="], - "@oxc-resolver/binding-linux-riscv64-gnu": ["@oxc-resolver/binding-linux-riscv64-gnu@11.19.1", "", { "os": "linux", "cpu": "none" }, "sha512-/b+WgR+VTSBxzgOhDO7TlMXC1ufPIMR6Vj1zN+/x+MnyXGW7prTLzU9eW85Aj7Th7CCEG9ArCbTeqxCzFWdg2w=="], + "@oxc-resolver/binding-linux-x64-gnu": ["@oxc-resolver/binding-linux-x64-gnu@11.24.2", "", { "os": "linux", "cpu": "x64" }, "sha512-k/VlMMcSzMlahb3/fENM4rTlsJ0s3fFROA0KXPBmKggqmTSaE383sl8F3KCOXPLmVsYfW6hCitMhXCEtNeZxxg=="], - "@oxc-resolver/binding-linux-riscv64-musl": ["@oxc-resolver/binding-linux-riscv64-musl@11.19.1", "", { "os": "linux", "cpu": "none" }, "sha512-YlRdeWb9j42p29ROh+h4eg/OQ3dTJlpHSa+84pUM9+p6i3djtPz1q55yLJhgW9XfDch7FN1pQ/Vd6YP+xfRIuw=="], + "@oxc-resolver/binding-linux-x64-musl": ["@oxc-resolver/binding-linux-x64-musl@11.24.2", "", { "os": "linux", "cpu": "x64" }, "sha512-8hbnZyNi97b/8wapYaIF9+t9GmZKBW2vunaOc3h9HGJptH7b7XpvZqOTBSm/MpTjr7H497BlgOaSfLUdhmy2bw=="], - "@oxc-resolver/binding-linux-s390x-gnu": ["@oxc-resolver/binding-linux-s390x-gnu@11.19.1", "", { "os": "linux", "cpu": "s390x" }, "sha512-EDpafVOQWF8/MJynsjOGFThcqhRHy417sRyLfQmeiamJ8qVhSKAn2Dn2VVKUGCjVB9C46VGjhNo7nOPUi1x6uA=="], + "@oxc-resolver/binding-openharmony-arm64": ["@oxc-resolver/binding-openharmony-arm64@11.24.2", "", { "os": "none", "cpu": "arm64" }, "sha512-MvyGik3a6pVgZ0t/kWlbmFxFLmXQJwgLsY2eYFHLpy0wGwRbfzeIGgDwQ3kXqE30z+kSXennRkCrT7TUvkptNg=="], - "@oxc-resolver/binding-linux-x64-gnu": ["@oxc-resolver/binding-linux-x64-gnu@11.19.1", "", { "os": "linux", "cpu": "x64" }, "sha512-NxjZe+rqWhr+RT8/Ik+5ptA3oz7tUw361Wa5RWQXKnfqwSSHdHyrw6IdcTfYuml9dM856AlKWZIUXDmA9kkiBQ=="], + "@oxc-resolver/binding-wasm32-wasi": ["@oxc-resolver/binding-wasm32-wasi@11.24.2", "", { "dependencies": { "@emnapi/core": "1.11.2", "@emnapi/runtime": "1.11.2", "@napi-rs/wasm-runtime": "^1.1.6" }, "cpu": "none" }, "sha512-vHcssMPwO08RTvj/c0iOBz90attxyG3wQJ0dTcyEQK43LRpcdLWZlV5feBhv6Isn6ahbQIzHbCgfa81+RiML0Q=="], - "@oxc-resolver/binding-linux-x64-musl": ["@oxc-resolver/binding-linux-x64-musl@11.19.1", "", { "os": "linux", "cpu": "x64" }, "sha512-cM/hQwsO3ReJg5kR+SpI69DMfvNCp+A/eVR4b4YClE5bVZwz8rh2Nh05InhwI5HR/9cArbEkzMjcKgTHS6UaNw=="], + "@oxc-resolver/binding-win32-arm64-msvc": ["@oxc-resolver/binding-win32-arm64-msvc@11.24.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-uokJqro2iBqkFvJdKQLP7d8/BUmFwESQFVmIJUQKj1Xn1a/LysJoe1vmeECLF5b3jsV8CAL5sEMJXX6SdK9Nhg=="], - "@oxc-resolver/binding-openharmony-arm64": ["@oxc-resolver/binding-openharmony-arm64@11.19.1", "", { "os": "none", "cpu": "arm64" }, "sha512-QF080IowFB0+9Rh6RcD19bdgh49BpQHUW5TajG1qvWHvmrQznTZZjYlgE2ltLXyKY+qs4F/v5xuX1XS7Is+3qA=="], + "@oxc-resolver/binding-win32-x64-msvc": ["@oxc-resolver/binding-win32-x64-msvc@11.24.2", "", { "os": "win32", "cpu": "x64" }, "sha512-UqGPmo56KDfLlfXFAFIrNflHT8tFxWGEivWg3Zeyp4Uy2NlKN1FGPr6/BxcLGG3+kZ6Wp14g5Uj+n71boqZfiw=="], - "@oxc-resolver/binding-wasm32-wasi": ["@oxc-resolver/binding-wasm32-wasi@11.19.1", "", { "dependencies": { "@napi-rs/wasm-runtime": "^1.1.1" }, "cpu": "none" }, "sha512-w8UCKhX826cP/ZLokXDS6+milN8y4X7zidsAttEdWlVoamTNf6lhBJldaWr3ukTDiye7s4HRcuPEPOXNC432Vg=="], + "@sigstore/bundle": ["@sigstore/bundle@5.0.0", "", { "dependencies": { "@sigstore/protobuf-specs": "^0.5.0" } }, "sha512-wefjygudENbzbQMks1t5u34EP0fFoD0XvaEP7DOUP/sXKvogzEJYFw5E6pegGyp3onGWzVEYKVa3bNZWyTYX+A=="], - "@oxc-resolver/binding-win32-arm64-msvc": ["@oxc-resolver/binding-win32-arm64-msvc@11.19.1", "", { "os": "win32", "cpu": "arm64" }, "sha512-nJ4AsUVZrVKwnU/QRdzPCCrO0TrabBqgJ8pJhXITdZGYOV28TIYystV1VFLbQ7DtAcaBHpocT5/ZJnF78YJPtQ=="], + "@sigstore/core": ["@sigstore/core@4.0.1", "", {}, "sha512-9v5hRjujn5NXq8o7XFEUgLyAtdr5Iisb4pzM05u3K61IS5q3hP3luWAndk0RkPPLTUFoTbg7Vb84UQ1ZQeajWQ=="], - "@oxc-resolver/binding-win32-ia32-msvc": ["@oxc-resolver/binding-win32-ia32-msvc@11.19.1", "", { "os": "win32", "cpu": "ia32" }, "sha512-EW+ND5q2Tl+a3pH81l1QbfgbF3HmqgwLfDfVithRFheac8OTcnbXt/JxqD2GbDkb7xYEqy1zNaVFRr3oeG8npA=="], + "@sigstore/protobuf-specs": ["@sigstore/protobuf-specs@0.5.0", "", {}, "sha512-MM8XIwUjN2bwvCg1QvrMtbBmpcSHrkhFSCu1D11NyPvDQ25HEc4oG5/OcQfd/Tlf/OxmKWERDj0zGE23jQaMwA=="], - "@oxc-resolver/binding-win32-x64-msvc": ["@oxc-resolver/binding-win32-x64-msvc@11.19.1", "", { "os": "win32", "cpu": "x64" }, "sha512-6hIU3RQu45B+VNTY4Ru8ppFwjVS/S5qwYyGhBotmjxfEKk41I2DlGtRfGJndZ5+6lneE2pwloqunlOyZuX/XAw=="], + "@sigstore/verify": ["@sigstore/verify@4.1.0", "", { "dependencies": { "@sigstore/bundle": "^5.0.0", "@sigstore/core": "^4.0.1", "@sigstore/protobuf-specs": "^0.5.0" } }, "sha512-p/s720RiWxLG8XtmfdPfEJOlATA6H/2knFqmtQbFkHKN3IrhWGUwPfpQAf1UnQIEES9IaH6zzhfjkrhTfeSdZw=="], - "@simple-libs/stream-utils": ["@simple-libs/stream-utils@1.2.0", "", {}, "sha512-KxXvfapcixpz6rVEB6HPjOUZT22yN6v0vI0urQSk1L8MlEWPDFCZkhw2xmkyoTGYeFw7tWTZd7e3lVzRZRN/EA=="], + "@simple-libs/child-process-utils": ["@simple-libs/child-process-utils@2.0.0", "", { "dependencies": { "@simple-libs/stream-utils": "^2.0.0" } }, "sha512-dvNoRKLijXnD0XoJAz94pbNuB5GQgDr55UhpSPhffDkTT0Cmcqh9jSCOtwfT2d4H6MI9E7c4SgtMuJXZ6F3c6A=="], + + "@simple-libs/stream-utils": ["@simple-libs/stream-utils@2.0.0", "", {}, "sha512-fCTuZK4QBa+39Oz9l4OGfJfz+GpwCp3AqO7Zch3to99xHPgstVsRFpeQ8LNd2o1Gv8raL2mCFwiaHh7bFSp5DQ=="], "@sindresorhus/merge-streams": ["@sindresorhus/merge-streams@4.0.0", "", {}, "sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ=="], "@tootallnate/quickjs-emscripten": ["@tootallnate/quickjs-emscripten@0.23.0", "", {}, "sha512-C5Mc6rdnsaJDjO3UpGW/CQTHtCKaYlScZTly4JIu97Jxo/odCiH0ITnDXSJPTOrEKk/ycSZ0AOgTmkDtkOsvIA=="], - "@tybys/wasm-util": ["@tybys/wasm-util@0.10.1", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-9tTaPJLSiejZKx+Bmog4uSubteqTvFrVrURwkmHixBo0G4seD0zUxp98E1DzUBJxLQ3NPwXrGKDiVjwx/DpPsg=="], + "@tybys/wasm-util": ["@tybys/wasm-util@0.10.3", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-F3fo1MYrRJYL3zER0OUOmkutjr1Vp23m7OsSgp7nq4SP6OqX6C/56XFIPAl5bt3zaBRjmW7SGz3u/6LwFpYcOg=="], - "@types/bun": ["@types/bun@1.3.10", "", { "dependencies": { "bun-types": "1.3.10" } }, "sha512-0+rlrUrOrTSskibryHbvQkDOWRJwJZqZlxrUs1u4oOoTln8+WIXBPmAuCF35SWB2z4Zl3E84Nl/D0P7803nigQ=="], + "@types/bun": ["@types/bun@1.3.14", "", { "dependencies": { "bun-types": "1.3.14" } }, "sha512-h1hFqFVcvAvD9j9K7ZW7vd82aSA+rTdznZa+5bwvCwqSB1jmmfLcbIWhOLx1/+boy/xmjgCs/OMUL8hRJSmnPw=="], "@types/debug": ["@types/debug@4.1.13", "", { "dependencies": { "@types/ms": "*" } }, "sha512-KSVgmQmzMwPlmtljOomayoR89W4FynCAi3E8PPs7vmDVPe84hT+vGPKkJfThkmXs0x0jAaa9U8uW8bbfyS2fWw=="], @@ -247,21 +312,21 @@ "@types/ms": ["@types/ms@2.1.0", "", {}, "sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA=="], - "@types/node": ["@types/node@22.19.15", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-F0R/h2+dsy5wJAUe3tAU6oqa2qbWY5TpNfL/RGmo1y38hiyO1w3x2jPtt76wmuaJI4DQnOBu21cNXQ2STIUUWg=="], + "@types/node": ["@types/node@22.20.1", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-EANqOCF9QFyra+4pfxUcX9STKJpCLjMbObVzljIJomAWSnuSIEAvyzEU53GaajbXJEgdh0iEcPL+DGvpUd4k1Q=="], "@types/unist": ["@types/unist@2.0.11", "", {}, "sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA=="], "agent-base": ["agent-base@7.1.4", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="], - "ajv": ["ajv@8.18.0", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", "json-schema-traverse": "^1.0.0", "require-from-string": "^2.0.2" } }, "sha512-PlXPeEWMXMZ7sPYOHqmDyCJzcfNrUr3fGNKtezX14ykXOEIvyK81d+qydx89KY5O71FKMPaQ2vBfBFI5NHR63A=="], + "ajv": ["ajv@8.20.0", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", "json-schema-traverse": "^1.0.0", "require-from-string": "^2.0.2" } }, "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA=="], - "ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], + "ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="], - "ansi-styles": ["ansi-styles@4.3.0", "", { "dependencies": { "color-convert": "^2.0.1" } }, "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg=="], + "ansi-styles": ["ansi-styles@6.2.3", "", {}, "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg=="], "argparse": ["argparse@2.0.1", "", {}, "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="], - "array-ify": ["array-ify@1.0.0", "", {}, "sha512-c5AMf34bKdvPhQ7tBGhqkgKNUzMr4WUs+WDtC2ZUGOUncbxKMTvqxYctiseW3+L4bA8ec+GcZ6/A/FW4m8ukng=="], + "argue-cli": ["argue-cli@3.1.0", "", {}, "sha512-DhBpBfXL4SS2uC0N922MMajKR3CdrTG0u2or1PNYgXMsrSzViJrbtvT0nCLlLGUI0plam/ZZCs7aAauHtW9thw=="], "ast-types": ["ast-types@0.13.4", "", { "dependencies": { "tslib": "^2.0.1" } }, "sha512-x1FCFnFifvYDDzTaLII71vG5uvDwgtmDTEVWAxrgeiR8VjMONcCXJx7E+USjDtHlwFmt9MysbqgF9b9Vjr6w+w=="], @@ -273,7 +338,7 @@ "braces": ["braces@3.0.3", "", { "dependencies": { "fill-range": "^7.1.1" } }, "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA=="], - "bun-types": ["bun-types@1.3.10", "", { "dependencies": { "@types/node": "*" } }, "sha512-tcpfCCl6XWo6nCVnpcVrxQ+9AYN1iqMIzgrSKYMB/fjLtV2eyAVEg7AxQJuCq/26R6HpKWykQXuSOq/21RYcbg=="], + "bun-types": ["bun-types@1.3.14", "", { "dependencies": { "@types/node": "*" } }, "sha512-4N0ig0fEomHt5R0KCFWjovxow98rIoRwKolrYdCcknNwMekCXRnWEUvgu5soYV8QXtVsrUD8B95MBOZGPvr6KQ=="], "callsites": ["callsites@3.1.0", "", {}, "sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ=="], @@ -285,7 +350,7 @@ "character-reference-invalid": ["character-reference-invalid@2.0.1", "", {}, "sha512-iBZ4F4wRbyORVsu0jPV7gXkOsGYjGHPmAyv+HiHG8gi5PtC9KI2j1+v8/tlibRvjoWX027ypmG/n0HtO5t7unw=="], - "chardet": ["chardet@2.1.1", "", {}, "sha512-PsezH1rqdV9VvyNhxxOW32/d75r01NY7TQCmOqomRo15ZSOKbpTFVsfjghxo6JloQUCGnH4k1LGu0R4yCLlWQQ=="], + "chardet": ["chardet@2.2.0", "", {}, "sha512-rddelWYNPRrXq6PtNEN2S3f6t9ILzvqaN5pVgi4kqt9jHQaXIial9PznB5iSPVlQSLNaaH22ItWz3EJtQ10+OA=="], "cheerio": ["cheerio@1.2.0", "", { "dependencies": { "cheerio-select": "^2.1.0", "dom-serializer": "^2.0.0", "domhandler": "^5.0.3", "domutils": "^3.2.2", "encoding-sniffer": "^0.2.1", "htmlparser2": "^10.1.0", "parse5": "^7.3.0", "parse5-htmlparser2-tree-adapter": "^7.1.0", "parse5-parser-stream": "^7.1.2", "undici": "^7.19.0", "whatwg-mimetype": "^4.0.0" } }, "sha512-WDrybc/gKFpTYQutKIK6UvfcuxijIZfMfXaYm8NMsPQxSYvf+13fXUJ4rztGGbJcBQ/GF55gvrZ0Bc0bj/mqvg=="], @@ -293,7 +358,7 @@ "cli-width": ["cli-width@4.1.0", "", {}, "sha512-ouuZd4/dm2Sw5Gmqy6bGyNNNe1qt9RpmxveLSO7KcgsTnU7RXfsw+/bukWGo1abgBiMAic068rclZsO4IWmmxQ=="], - "cliui": ["cliui@8.0.1", "", { "dependencies": { "string-width": "^4.2.0", "strip-ansi": "^6.0.1", "wrap-ansi": "^7.0.0" } }, "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ=="], + "cliui": ["cliui@9.0.1", "", { "dependencies": { "string-width": "^7.2.0", "strip-ansi": "^7.1.0", "wrap-ansi": "^9.0.0" } }, "sha512-k7ndgKhwoQveBL+/1tqGJYNz097I7WOvwbmmU2AR5+magtbjPWQTS1C5vzGkBC8Ym8UWRzfKUzUUqFLypY4Q+w=="], "color-convert": ["color-convert@2.0.1", "", { "dependencies": { "color-name": "~1.1.4" } }, "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ=="], @@ -301,24 +366,20 @@ "commander": ["commander@12.1.0", "", {}, "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA=="], - "compare-func": ["compare-func@2.0.0", "", { "dependencies": { "array-ify": "^1.0.0", "dot-prop": "^5.1.0" } }, "sha512-zHig5N+tPWARooBnb0Zx1MFcdfpyJrfTJ3Y5L+IFvUm8rM74hHz66z0gw0x4tijh5CorKkKUCnW82R2vmpeCRA=="], - - "conventional-changelog-angular": ["conventional-changelog-angular@8.2.0", "", { "dependencies": { "compare-func": "^2.0.0" } }, "sha512-4YB1zEXqB17oBI8yRsAs1T+ZhbdsOgJqkl6Trz+GXt/eKf1e4jnA0oW+sOd9BEENzEViuNW0DNoFFjSf3CeC5Q=="], + "conventional-changelog-angular": ["conventional-changelog-angular@9.2.1", "", { "dependencies": { "@conventional-changelog/template": "^1.2.1" } }, "sha512-oWSL6ZhnXbYraOFTK3PgRAQJ8fADDAEv5K6AdeyQPLvjFmhG8+ejL0jZZp/R7vTmGJaBvZEE+sE7dB4bCv7sAw=="], - "conventional-changelog-conventionalcommits": ["conventional-changelog-conventionalcommits@9.2.0", "", { "dependencies": { "compare-func": "^2.0.0" } }, "sha512-fCf+ODjseueTV09wVBoC0HXLi3OyuBJ+HfE3L63Khxqnr99f9nUcnQh3a15lCWHlGLihyZShW/mVVkBagr9JvQ=="], + "conventional-changelog-conventionalcommits": ["conventional-changelog-conventionalcommits@10.2.1", "", { "dependencies": { "@conventional-changelog/template": "^1.2.1" } }, "sha512-n4Kr1HFMTf3iMbES0TMxKIcYtUUv4rKqyQQp2JwfOEfFCOfGT3Tq4mCyJ8S9/YPyWhydjfKrrvnyl+gCjA+mJQ=="], - "conventional-commits-parser": ["conventional-commits-parser@6.3.0", "", { "dependencies": { "@simple-libs/stream-utils": "^1.2.0", "meow": "^13.0.0" }, "bin": { "conventional-commits-parser": "dist/cli/index.js" } }, "sha512-RfOq/Cqy9xV9bOA8N+ZH6DlrDR+5S3Mi0B5kACEjESpE+AviIpAptx9a9cFpWCCvgRtWT+0BbUw+e1BZfts9jg=="], + "conventional-commits-parser": ["conventional-commits-parser@7.1.0", "", { "dependencies": { "@simple-libs/stream-utils": "^2.0.0", "argue-cli": "^3.1.0" }, "bin": { "conventional-commits-parser": "./dist/cli/index.js" } }, "sha512-DPp6hkUjvwIivxbkrTiLXeRswNv1A/4GFA2X6scXma0AMa9632V3TwxmrlkUIEtUktiM3Ln+RrSH2xlP3/jUTw=="], - "cosmiconfig": ["cosmiconfig@9.0.1", "", { "dependencies": { "env-paths": "^2.2.1", "import-fresh": "^3.3.0", "js-yaml": "^4.1.0", "parse-json": "^5.2.0" }, "peerDependencies": { "typescript": ">=4.9.5" }, "optionalPeers": ["typescript"] }, "sha512-hr4ihw+DBqcvrsEDioRO31Z17x71pUYoNe/4h6Z0wB72p7MU7/9gH8Q3s12NFhHPfYBBOV3qyfUxmr/Yn3shnQ=="], + "cosmiconfig": ["cosmiconfig@9.0.2", "", { "dependencies": { "env-paths": "^2.2.1", "import-fresh": "^3.3.0", "js-yaml": "^4.1.0", "parse-json": "^5.2.0" }, "peerDependencies": { "typescript": ">=4.9.5" }, "optionalPeers": ["typescript"] }, "sha512-gtTZxTDau1wL7Y7zifc2dd8jHSK/k6BTx/2Xp/BpdlAdnlYWFVt7qhJqgwi7637yRwRQ3qL4ZidbB4I8tA5VOg=="], - "cosmiconfig-typescript-loader": ["cosmiconfig-typescript-loader@6.2.0", "", { "dependencies": { "jiti": "^2.6.1" }, "peerDependencies": { "@types/node": "*", "cosmiconfig": ">=9", "typescript": ">=5" } }, "sha512-GEN39v7TgdxgIoNcdkRE3uiAzQt3UXLyHbRHD6YoL048XAeOomyxaP+Hh/+2C6C2wYjxJ2onhJcsQp+L4YEkVQ=="], + "cosmiconfig-typescript-loader": ["cosmiconfig-typescript-loader@6.3.0", "", { "dependencies": { "jiti": "2.6.1" }, "peerDependencies": { "@types/node": "*", "cosmiconfig": ">=9", "typescript": ">=5" } }, "sha512-Akr82WH1Wfqatyiqpj8HDkO2o2KmJRu1FhKfSNJP3K4IdXwHfEyL7MOb62i1AGQVLtIQM+iCE9CGOtrfhR+mmA=="], "css-select": ["css-select@5.2.2", "", { "dependencies": { "boolbase": "^1.0.0", "css-what": "^6.1.0", "domhandler": "^5.0.2", "domutils": "^3.0.1", "nth-check": "^2.0.1" } }, "sha512-TizTzUddG/xYLA3NXodFM0fSbNizXjOKhqiQQwvhlspadZokn1KDy0NZFS0wuEubIYAV5/c1/lAr0TaaFXEXzw=="], "css-what": ["css-what@6.2.2", "", {}, "sha512-u/O3vwbptzhMs3L1fQE82ZSLHQQfto5gyZzwteVIEyeaY5Fc7R4dapF/BvRoSYFeqfBk4m0V1Vafq5Pjv25wvA=="], - "dargs": ["dargs@8.1.0", "", {}, "sha512-wAV9QHOsNbwnWdNW2FYvE1P56wtgSbM+3SZcdGiWQILwVjACCXDCI3Ai8QlCjMDB8YK5zySiXZYBiwGmNY3lnw=="], - "data-uri-to-buffer": ["data-uri-to-buffer@6.0.2", "", {}, "sha512-7hvf7/GW8e86rW0ptuwS3OcBGDjIi6SZva7hCyWC0yYry2cOPmLIjXAUHI6DK2HsnwJd9ifmt57i8eV2n4YNpw=="], "debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="], @@ -339,9 +400,7 @@ "domutils": ["domutils@3.2.2", "", { "dependencies": { "dom-serializer": "^2.0.0", "domelementtype": "^2.3.0", "domhandler": "^5.0.3" } }, "sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw=="], - "dot-prop": ["dot-prop@5.3.0", "", { "dependencies": { "is-obj": "^2.0.0" } }, "sha512-QM8q3zDe58hqUqjraQOmzZ1LIH9SWQJTlEKCH4kJ2oQvLZk7RbQXvtDM2XEq3fwkV9CCvvH4LA0AV+ogFsBM2Q=="], - - "emoji-regex": ["emoji-regex@8.0.0", "", {}, "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A=="], + "emoji-regex": ["emoji-regex@10.6.0", "", {}, "sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A=="], "encoding-sniffer": ["encoding-sniffer@0.2.1", "", { "dependencies": { "iconv-lite": "^0.6.3", "whatwg-encoding": "^3.1.1" } }, "sha512-5gvq20T6vfpekVtqrYQsSCFZ1wEg5+wW0/QaZMWkFr6BqD3NfKs0rLCx4rrVlSWJeZb5NBJgVLswK/w2MWU+Gw=="], @@ -351,7 +410,9 @@ "error-ex": ["error-ex@1.3.4", "", { "dependencies": { "is-arrayish": "^0.2.1" } }, "sha512-sqQamAnR14VgCr1A618A3sGrygcpK+HEbenA/HiEAkkUwcZIIB/tgWqHFxWgOyDh4nB4JCRimh79dR5Ywc9MDQ=="], - "esbuild": ["esbuild@0.27.3", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.27.3", "@esbuild/android-arm": "0.27.3", "@esbuild/android-arm64": "0.27.3", "@esbuild/android-x64": "0.27.3", "@esbuild/darwin-arm64": "0.27.3", "@esbuild/darwin-x64": "0.27.3", "@esbuild/freebsd-arm64": "0.27.3", "@esbuild/freebsd-x64": "0.27.3", "@esbuild/linux-arm": "0.27.3", "@esbuild/linux-arm64": "0.27.3", "@esbuild/linux-ia32": "0.27.3", "@esbuild/linux-loong64": "0.27.3", "@esbuild/linux-mips64el": "0.27.3", "@esbuild/linux-ppc64": "0.27.3", "@esbuild/linux-riscv64": "0.27.3", "@esbuild/linux-s390x": "0.27.3", "@esbuild/linux-x64": "0.27.3", "@esbuild/netbsd-arm64": "0.27.3", "@esbuild/netbsd-x64": "0.27.3", "@esbuild/openbsd-arm64": "0.27.3", "@esbuild/openbsd-x64": "0.27.3", "@esbuild/openharmony-arm64": "0.27.3", "@esbuild/sunos-x64": "0.27.3", "@esbuild/win32-arm64": "0.27.3", "@esbuild/win32-ia32": "0.27.3", "@esbuild/win32-x64": "0.27.3" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-8VwMnyGCONIs6cWue2IdpHxHnAjzxnw2Zr7MkVxB2vjmQ2ivqGFb4LEG3SMnv0Gb2F/G/2yA8zUaiL1gywDCCg=="], + "es-toolkit": ["es-toolkit@1.49.0", "", {}, "sha512-G5iZ6Pc/FNRY/soKZHC+TxGDD83rHUDXxzaWhGCX44vAv/tMs56WMusnm/KMNK+luUPsgA9U28cGr4RDlSzL2g=="], + + "esbuild": ["esbuild@0.28.1", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.28.1", "@esbuild/android-arm": "0.28.1", "@esbuild/android-arm64": "0.28.1", "@esbuild/android-x64": "0.28.1", "@esbuild/darwin-arm64": "0.28.1", "@esbuild/darwin-x64": "0.28.1", "@esbuild/freebsd-arm64": "0.28.1", "@esbuild/freebsd-x64": "0.28.1", "@esbuild/linux-arm": "0.28.1", "@esbuild/linux-arm64": "0.28.1", "@esbuild/linux-ia32": "0.28.1", "@esbuild/linux-loong64": "0.28.1", "@esbuild/linux-mips64el": "0.28.1", "@esbuild/linux-ppc64": "0.28.1", "@esbuild/linux-riscv64": "0.28.1", "@esbuild/linux-s390x": "0.28.1", "@esbuild/linux-x64": "0.28.1", "@esbuild/netbsd-arm64": "0.28.1", "@esbuild/netbsd-x64": "0.28.1", "@esbuild/openbsd-arm64": "0.28.1", "@esbuild/openbsd-x64": "0.28.1", "@esbuild/openharmony-arm64": "0.28.1", "@esbuild/sunos-x64": "0.28.1", "@esbuild/win32-arm64": "0.28.1", "@esbuild/win32-ia32": "0.28.1", "@esbuild/win32-x64": "0.28.1" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-HrJrvZv5ayxBzPfwphOoNzkzOIIlifzk0KJrGK2c8R4+LKpMtpYLQeUdjnwjWv/LZlkH2laZk+4w78pi99D4Vw=="], "escalade": ["escalade@3.2.0", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="], @@ -367,12 +428,14 @@ "fast-glob": ["fast-glob@3.3.3", "", { "dependencies": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", "glob-parent": "^5.1.2", "merge2": "^1.3.0", "micromatch": "^4.0.8" } }, "sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg=="], - "fast-uri": ["fast-uri@3.1.0", "", {}, "sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA=="], + "fast-uri": ["fast-uri@3.1.4", "", {}, "sha512-8JnbkQ4juDyvYs4mgFGQqg4yCYtFDtUtmp2QIQq11ZZe5CFQ5wcqm1rqDgAh/QdMySuBnPzMUiJUNZG5N/AiQw=="], "fastq": ["fastq@1.20.1", "", { "dependencies": { "reusify": "^1.0.4" } }, "sha512-GGToxJ/w1x32s/D2EKND7kTil4n8OVk/9mycTc4VDza13lOvpUZTGX3mFSCtV9ksdGBVzvsyAVLM6mHFThxXxw=="], "fd-package-json": ["fd-package-json@2.0.0", "", { "dependencies": { "walk-up-path": "^4.0.0" } }, "sha512-jKmm9YtsNXN789RS/0mSzOC1NUq9mkVd65vbSSVsKdjGvYXBuE4oWe2QOEoFeRmJg+lPuZxpmrfFclNhoRMneQ=="], + "fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="], + "fill-range": ["fill-range@7.1.1", "", { "dependencies": { "to-regex-range": "^5.0.1" } }, "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg=="], "formatly": ["formatly@0.3.0", "", { "dependencies": { "fd-package-json": "^2.0.0" }, "bin": { "formatly": "bin/index.mjs" } }, "sha512-9XNj/o4wrRFyhSMJOvsuyMwy8aUfBaZ1VrqHVfohyXf0Sw0e+yfKG+xZaY3arGCOMdwFsqObtzVOc1gU9KiT9w=="], @@ -381,15 +444,15 @@ "get-east-asian-width": ["get-east-asian-width@1.6.0", "", {}, "sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA=="], - "get-uri": ["get-uri@6.0.5", "", { "dependencies": { "basic-ftp": "^5.0.2", "data-uri-to-buffer": "^6.0.2", "debug": "^4.3.4" } }, "sha512-b1O07XYq8eRuVzBNgJLstU6FYc1tS6wnMtF1I1D9lE8LxZSOGZ7LhxN54yPP6mGw5f2CkXY2BQUL9Fx41qvcIg=="], + "get-tsconfig": ["get-tsconfig@4.14.0", "", { "dependencies": { "resolve-pkg-maps": "^1.0.0" } }, "sha512-yTb+8DXzDREzgvYmh6s9vHsSVCHeC0G3PI5bEXNBHtmshPnO+S5O7qgLEOn0I5QvMy6kpZN8K1NKGyilLb93wA=="], - "git-raw-commits": ["git-raw-commits@4.0.0", "", { "dependencies": { "dargs": "^8.0.0", "meow": "^12.0.1", "split2": "^4.0.0" }, "bin": { "git-raw-commits": "cli.mjs" } }, "sha512-ICsMM1Wk8xSGMowkOmPrzo2Fgmfo4bMHLNX6ytHjajRJUqvHOw/TFapQ+QG75c3X/tTDDhOSRPGC52dDbNM8FQ=="], + "get-uri": ["get-uri@6.0.5", "", { "dependencies": { "basic-ftp": "^5.0.2", "data-uri-to-buffer": "^6.0.2", "debug": "^4.3.4" } }, "sha512-b1O07XYq8eRuVzBNgJLstU6FYc1tS6wnMtF1I1D9lE8LxZSOGZ7LhxN54yPP6mGw5f2CkXY2BQUL9Fx41qvcIg=="], "glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="], - "global-directory": ["global-directory@4.0.1", "", { "dependencies": { "ini": "4.1.1" } }, "sha512-wHTUcDUoZ1H5/0iVqEudYW4/kAlN5cZ3j/bXn0Dpbizl9iaUVeWSHqiOjsgk6OW2bkLclbBjzewBz6weQ1zA2Q=="], + "global-directory": ["global-directory@5.0.0", "", { "dependencies": { "ini": "6.0.0" } }, "sha512-1pgFdhK3J2LeM+dVf2Pd424yHx2ou338lC0ErNP2hPx4j8eW1Sp0XqSjNxtk6Tc4Kr5wlWtSvz8cn2yb7/SG/w=="], - "globby": ["globby@16.2.0", "", { "dependencies": { "@sindresorhus/merge-streams": "^4.0.0", "fast-glob": "^3.3.3", "ignore": "^7.0.5", "is-path-inside": "^4.0.0", "slash": "^5.1.0", "unicorn-magic": "^0.4.0" } }, "sha512-QrJia2qDf5BB/V6HYlDTs0I0lBahyjLzpGQg3KT7FnCdTonAyPy2RtY802m2k4ALx6Dp752f82WsOczEVr3l6Q=="], + "globby": ["globby@16.2.1", "", { "dependencies": { "@sindresorhus/merge-streams": "^4.0.0", "fast-glob": "^3.3.3", "ignore": "^7.0.5", "is-path-inside": "^4.0.0", "slash": "^5.1.0", "unicorn-magic": "^0.4.0" } }, "sha512-JmsqJalahxxgW8V2ecSQ2G7UjPlI9cpKdrkG9KoNiXhd/YslXOTEB0cViENWUznuovIuNT+FkMbraDGjr4FCUg=="], "html-link-extractor": ["html-link-extractor@1.0.5", "", { "dependencies": { "cheerio": "^1.0.0-rc.10" } }, "sha512-ADd49pudM157uWHwHQPUSX4ssMsvR/yHIswOR5CUfBdK9g9ZYGMhVSE6KZVHJ6kCkR0gH4htsfzU6zECDNVwyw=="], @@ -407,9 +470,7 @@ "import-fresh": ["import-fresh@3.3.1", "", { "dependencies": { "parent-module": "^1.0.0", "resolve-from": "^4.0.0" } }, "sha512-TR3KfrTZTYLPB6jUjfx6MF9WcWrHL9su5TObK4ZkYgBdWKPOFoSoQIdEuTuR82pmtxH2spWG9h6etwfr1pLBqQ=="], - "import-meta-resolve": ["import-meta-resolve@4.2.0", "", {}, "sha512-Iqv2fzaTQN28s/FwZAoFq0ZSs/7hMAHJVX+w8PZl3cY19Pxk6jFFalxQoIfW2826i/fDLXv8IiEZRIT0lDuWcg=="], - - "ini": ["ini@4.1.1", "", {}, "sha512-QQnnxNyfvmHFIsj7gkPcYymR8Jdw/o7mp5ZFihxn6h8Ci6fh3Dx4E1gPjpQEpIuPo9XVNY/ZUwh4BPMjGyL01g=="], + "ini": ["ini@6.0.0", "", {}, "sha512-IBTdIkzZNOpqm7q3dRqJvMaldXjDHWkEDfrwGEQTs5eaQMWV+djAhR+wahyNNMAa+qpbDUhBMVt4ZKNwpPm7xQ=="], "ip-address": ["ip-address@10.2.0", "", {}, "sha512-/+S6j4E9AHvW9SWMSEY9Xfy66O5PWvVEJ08O0y5JGyEKQpojb0K0GKpz/v5HJ/G0vi3D2sjGK78119oXZeE0qA=="], @@ -433,19 +494,17 @@ "is-number": ["is-number@7.0.0", "", {}, "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng=="], - "is-obj": ["is-obj@2.0.0", "", {}, "sha512-drqDG3cbczxxEJRoOXcOjtdp1J/lyp1mNn0xaznRs8+muBhgQcrnbspox5X5fOw0HnMnbfDzvnEMEtqDEJEo8w=="], - "is-path-inside": ["is-path-inside@4.0.0", "", {}, "sha512-lJJV/5dYS+RcL8uQdBDW9c9uWFLLBNRyFhnAKXw5tVqLlKZ4RMGZKv+YQ/IA3OhD+RpbJa1LLFM1FQPGyIXvOA=="], "is-plain-obj": ["is-plain-obj@4.1.0", "", {}, "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg=="], "is-relative-url": ["is-relative-url@4.1.0", "", { "dependencies": { "is-absolute-url": "^4.0.1" } }, "sha512-vhIXKasjAuxS7n+sdv7pJQykEAgS+YU8VBQOENXwo/VZpOHDgBBsIbHo7zFKaWBjYWF4qxERdhbPRRtFAeJKfg=="], - "jiti": ["jiti@2.6.1", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ=="], + "jiti": ["jiti@2.7.0", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ=="], "js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="], - "js-yaml": ["js-yaml@5.2.0", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.mjs" } }, "sha512-YeLUMlvR4Ou1B119LIaM0r65JvbOBooJDc9yEu0dClb/uSC5P4FrLU8OCCz/HXWvtPoIrR0dRzABTjo1sTN9Bw=="], + "js-yaml": ["js-yaml@5.2.1", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.mjs" } }, "sha512-zfLtNfQqxVqq3uaTqSkh4x4hZw3KHobGUA0fJUj4wawW8bsQLTVqpHdXSIzidh7o+4lEW36tANuAGdaFx6Zgnw=="], "json-parse-even-better-errors": ["json-parse-even-better-errors@2.3.1", "", {}, "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w=="], @@ -457,7 +516,7 @@ "katex": ["katex@0.16.47", "", { "dependencies": { "commander": "^8.3.0" }, "bin": { "katex": "cli.js" } }, "sha512-Eeo8Ys1doU1z+x8AZsPpQu+p/QcZBI5PeOo7QGQdy2x2m0MU/hYagBbGOmXwr5KVbEfVuWv9LpnQWeehogurjg=="], - "knip": ["knip@5.86.0", "", { "dependencies": { "@nodelib/fs.walk": "^1.2.3", "fast-glob": "^3.3.3", "formatly": "^0.3.0", "jiti": "^2.6.0", "minimist": "^1.2.8", "oxc-resolver": "^11.19.1", "picocolors": "^1.1.1", "picomatch": "^4.0.1", "smol-toml": "^1.5.2", "strip-json-comments": "5.0.3", "unbash": "^2.2.0", "yaml": "^2.8.2", "zod": "^4.1.11" }, "peerDependencies": { "@types/node": ">=18", "typescript": ">=5.0.4 <7" }, "bin": { "knip": "bin/knip.js", "knip-bun": "bin/knip-bun.js" } }, "sha512-tGpRCbP+L+VysXnAp1bHTLQ0k/SdC3M3oX18+Cpiqax1qdS25iuCPzpK8LVmAKARZv0Ijri81Wq09Rzk0JTl+Q=="], + "knip": ["knip@6.29.0", "", { "dependencies": { "fdir": "^6.5.0", "formatly": "^0.3.0", "get-tsconfig": "4.14.0", "jiti": "^2.7.0", "oxc-parser": "^0.140.0", "oxc-resolver": "11.24.2", "picomatch": "^4.0.5", "smol-toml": "^1.7.0", "strip-json-comments": "5.0.3", "tinyglobby": "^0.2.17", "unbash": "^4.0.3", "yaml": "^2.9.0", "zod": "^4.4.3" }, "bin": { "knip": "bin/knip.js", "knip-bun": "bin/knip-bun.js" } }, "sha512-A3kXqSBky1tWBAqiU9srdtu0Larhzkuyor0aD/gg+ToiqyBncCCs2Q60sLsxmcKhV0OsKss9LV0hMPpwLv711Q=="], "lines-and-columns": ["lines-and-columns@1.2.4", "", {}, "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg=="], @@ -465,37 +524,23 @@ "linkify-it": ["linkify-it@5.0.2", "", { "dependencies": { "uc.micro": "^2.0.0" } }, "sha512-ONTm2jCMAVZjgQa/Fy1kScXsuOoF5NPTsoFBdE1KVIZ2vAh/r9+Bqo+0jINCBYnavTPQZz38QzFTme79ENoN3Q=="], - "lodash.camelcase": ["lodash.camelcase@4.3.0", "", {}, "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA=="], - - "lodash.kebabcase": ["lodash.kebabcase@4.1.1", "", {}, "sha512-N8XRTIMMqqDgSy4VLKPnJ/+hpGZN+PHQiJnSenYqPaVV/NCqEogTnAdZLQiGKhxX+JCs8waWq2t1XHWKOmlY8g=="], - - "lodash.mergewith": ["lodash.mergewith@4.6.2", "", {}, "sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ=="], - - "lodash.snakecase": ["lodash.snakecase@4.1.1", "", {}, "sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw=="], - - "lodash.startcase": ["lodash.startcase@4.4.0", "", {}, "sha512-+WKqsK294HMSc2jEbNgpHpd0JfIBhp7rEV4aqXWqFr6AlXov+SlcgB1Fv01y2kGe3Gc8nMW7VA0SrGuSkRfIEg=="], - - "lodash.upperfirst": ["lodash.upperfirst@4.3.1", "", {}, "sha512-sReKOYJIJf74dhJONhU4e0/shzi1trVbSWDOhKYE5XV2O+H7Sb2Dihwuc7xWxVl+DgFPyTqIN3zMfT9cq5iWDg=="], - "lru-cache": ["lru-cache@7.18.3", "", {}, "sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA=="], - "markdown-it": ["markdown-it@14.2.0", "", { "dependencies": { "argparse": "^2.0.1", "entities": "^4.4.0", "linkify-it": "^5.0.1", "mdurl": "^2.0.0", "punycode.js": "^2.3.1", "uc.micro": "^2.1.0" }, "bin": { "markdown-it": "bin/markdown-it.mjs" } }, "sha512-1TGiQiJVRQ3NPmZH6sx5Cfnmg6GQm9jvC1ch4TK511NjSJvjzKLzn5pPfZRNZkRPZP0HqCioSndqH8v2nRaWVQ=="], + "markdown-it": ["markdown-it@14.3.0", "", { "dependencies": { "argparse": "^2.0.1", "entities": "^4.5.0", "linkify-it": "^5.0.2", "mdurl": "^2.0.0", "punycode.js": "^2.3.1", "uc.micro": "^2.1.0" }, "bin": { "markdown-it": "bin/markdown-it.mjs" } }, "sha512-RCEsPjR+sr0x+AuYp601tKTkgFG4YEPLCzHST3cQ/fhlJkqAkz1L2/Qbp1j9qw5SBwQHFBoW8+hoN5xssOF0Tw=="], "markdown-link-check": ["markdown-link-check@3.14.2", "", { "dependencies": { "async": "^3.2.6", "chalk": "^5.6.2", "commander": "^14.0.2", "link-check": "^5.5.1", "markdown-link-extractor": "^4.0.3", "needle": "^3.3.1", "progress": "^2.0.3", "proxy-agent": "^6.5.0", "xmlbuilder2": "^4.0.0" }, "bin": { "markdown-link-check": "markdown-link-check" } }, "sha512-DPJ+itd3D5fcfXD5s1i53lugH0Z/h80kkQxlYCBh8tFwEZGhyVgDcLl0rnKlWssAVDAmSmcbePpHpMEY+JcMMQ=="], "markdown-link-extractor": ["markdown-link-extractor@4.0.3", "", { "dependencies": { "html-link-extractor": "^1.0.5", "marked": "^17.0.0" } }, "sha512-aEltJiQ4/oC0h6Jbw/uuATGSHZPkcH8DIunNH1A0e+GSFkvZ6BbBkdvBTVfIV8r6HapCU3yTd0eFdi3ZeM1eAQ=="], - "markdownlint": ["markdownlint@0.41.0", "", { "dependencies": { "micromark": "4.0.2", "micromark-core-commonmark": "2.0.3", "micromark-extension-directive": "4.0.0", "micromark-extension-gfm-autolink-literal": "2.1.0", "micromark-extension-gfm-footnote": "2.1.0", "micromark-extension-gfm-table": "2.1.1", "micromark-extension-math": "3.1.0", "micromark-util-types": "2.0.2", "string-width": "8.2.1" } }, "sha512-xMUI3ChBuRuxuLF4ENvCZyS8z/+Jly1coUcZwErKLIB3sDj7ojpaTBa1e9YVPhSN4jGEIjYGQCldbTJS/hqS+A=="], + "markdownlint": ["markdownlint@0.41.1", "", { "dependencies": { "micromark": "4.0.2", "micromark-core-commonmark": "2.0.3", "micromark-extension-directive": "4.0.0", "micromark-extension-gfm-autolink-literal": "2.1.0", "micromark-extension-gfm-footnote": "2.1.0", "micromark-extension-gfm-table": "2.1.1", "micromark-extension-math": "3.1.0", "micromark-util-types": "2.0.2", "string-width": "8.2.1" } }, "sha512-qHKeU2E1bdyNAT077go2FVTNXvYcktN5IHtF6XyeD1l0PClxzSp2tUApAV14ORI8DGX4H9bNKZEzelZp4qn8IA=="], - "markdownlint-cli2": ["markdownlint-cli2@0.23.0", "", { "dependencies": { "globby": "16.2.0", "js-yaml": "5.2.0", "jsonc-parser": "3.3.1", "jsonpointer": "5.0.1", "markdown-it": "14.2.0", "markdownlint": "0.41.0", "markdownlint-cli2-formatter-default": "0.0.6", "micromatch": "4.0.8", "smol-toml": "1.7.0" }, "bin": { "markdownlint-cli2": "markdownlint-cli2-bin.mjs" } }, "sha512-1nmgQmU/ZTMRVwYCDs7i1HI3zfBISnT2NNRv+9V01oOLZbAtqL+a7tldpPhBWBVBten3FqhMCGV6EUh9McqutQ=="], + "markdownlint-cli2": ["markdownlint-cli2@0.23.1", "", { "dependencies": { "globby": "16.2.1", "js-yaml": "5.2.1", "jsonc-parser": "3.3.1", "jsonpointer": "5.0.1", "markdown-it": "14.3.0", "markdownlint": "0.41.1", "markdownlint-cli2-formatter-default": "0.0.6", "micromatch": "4.0.8", "smol-toml": "1.7.0" }, "bin": { "markdownlint-cli2": "markdownlint-cli2-bin.mjs" } }, "sha512-20JPI5W+HpV1OA+pUM712wgvL4GzYNUvbmhLU8KlEYJ1kCDx4soZ4/Xqd+WkLrPTOKMAn8SfO3zYFrK8GLlwQg=="], "markdownlint-cli2-formatter-default": ["markdownlint-cli2-formatter-default@0.0.6", "", { "peerDependencies": { "markdownlint-cli2": ">=0.0.4" } }, "sha512-VVDGKsq9sgzu378swJ0fcHfSicUnMxnL8gnLm/Q4J/xsNJ4e5bA6lvAz7PCzIl0/No0lHyaWdqVD2jotxOSFMQ=="], "marked": ["marked@17.0.6", "", { "bin": { "marked": "bin/marked.js" } }, "sha512-gB0gkNafnonOw0obSTEGZTT86IuhILt2Wfx0mWH/1Au83kybTayroZ/V6nS25mN7u8ASy+5fMhgB3XPNrOZdmA=="], - "mdurl": ["mdurl@2.0.0", "", {}, "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w=="], - - "meow": ["meow@12.1.1", "", {}, "sha512-BhXM0Au22RwUneMPwSCnyhTOizdWoIEPU9sp0Aqa1PnDMR5Wv2FGXYDjuzJEIX+Eo2Rb8xuYe5jrnm5QowQFkw=="], + "mdurl": ["mdurl@2.1.0", "", {}, "sha512-1+HBaOx0zi/dQWht8rNv9MYf9qqpqL/kxI0hXImU6Y547zM6Sni8BQibt7ifgMcYtQg41ao3Ivd6cnSM86inpg=="], "merge2": ["merge2@1.4.1", "", {}, "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg=="], @@ -551,25 +596,21 @@ "micromatch": ["micromatch@4.0.8", "", { "dependencies": { "braces": "^3.0.3", "picomatch": "^2.3.1" } }, "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA=="], - "minimist": ["minimist@1.2.8", "", {}, "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA=="], - "ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="], "mute-stream": ["mute-stream@2.0.0", "", {}, "sha512-WWdIxpyjEn+FhQJQQv9aQAYlHoNVdzIzUySNV1gHUPDSdZJ3yZn7pAAbQcV7B56Mvu881q9FZV+0Vx2xC44VWA=="], - "nats": ["nats@2.29.3", "", { "dependencies": { "nkeys.js": "1.1.0" } }, "sha512-tOQCRCwC74DgBTk4pWZ9V45sk4d7peoE2njVprMRCBXrhJ5q5cYM7i6W+Uvw2qUrcfOSnuisrX7bEx3b3Wx4QA=="], - "needle": ["needle@3.5.0", "", { "dependencies": { "iconv-lite": "^0.6.3", "sax": "^1.2.4" }, "bin": { "needle": "bin/needle" } }, "sha512-jaQyPKKk2YokHrEg+vFDYxXIHTCBgiZwSHOoVx/8V3GIBS8/VN6NdVRmg8q1ERtPkMvmOvebsgga4sAj5hls/w=="], "netmask": ["netmask@2.1.1", "", {}, "sha512-eonl3sLUha+S1GzTPxychyhnUzKyeQkZ7jLjKrBagJgPla13F+uQ71HgpFefyHgqrjEbCPkDArxYsjY8/+gLKA=="], - "nkeys.js": ["nkeys.js@1.1.0", "", { "dependencies": { "tweetnacl": "1.0.3" } }, "sha512-tB/a0shZL5UZWSwsoeyqfTszONTt4k2YS0tuQioMOD180+MbombYVgzDUYHlx+gejYK6rgf08n/2Df99WY0Sxg=="], - "node-email-verifier": ["node-email-verifier@3.4.1", "", { "dependencies": { "ms": "^2.1.3", "validator": "^13.15.15" } }, "sha512-69JMeWgEUrCji+dOLULirdSoosRxgAq2y+imfmHHBGvgTwyTKqvm65Ls3+W30DCIWMrYj5kKVb/DHTQDK7OVwQ=="], "nth-check": ["nth-check@2.1.1", "", { "dependencies": { "boolbase": "^1.0.0" } }, "sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w=="], - "oxc-resolver": ["oxc-resolver@11.19.1", "", { "optionalDependencies": { "@oxc-resolver/binding-android-arm-eabi": "11.19.1", "@oxc-resolver/binding-android-arm64": "11.19.1", "@oxc-resolver/binding-darwin-arm64": "11.19.1", "@oxc-resolver/binding-darwin-x64": "11.19.1", "@oxc-resolver/binding-freebsd-x64": "11.19.1", "@oxc-resolver/binding-linux-arm-gnueabihf": "11.19.1", "@oxc-resolver/binding-linux-arm-musleabihf": "11.19.1", "@oxc-resolver/binding-linux-arm64-gnu": "11.19.1", "@oxc-resolver/binding-linux-arm64-musl": "11.19.1", "@oxc-resolver/binding-linux-ppc64-gnu": "11.19.1", "@oxc-resolver/binding-linux-riscv64-gnu": "11.19.1", "@oxc-resolver/binding-linux-riscv64-musl": "11.19.1", "@oxc-resolver/binding-linux-s390x-gnu": "11.19.1", "@oxc-resolver/binding-linux-x64-gnu": "11.19.1", "@oxc-resolver/binding-linux-x64-musl": "11.19.1", "@oxc-resolver/binding-openharmony-arm64": "11.19.1", "@oxc-resolver/binding-wasm32-wasi": "11.19.1", "@oxc-resolver/binding-win32-arm64-msvc": "11.19.1", "@oxc-resolver/binding-win32-ia32-msvc": "11.19.1", "@oxc-resolver/binding-win32-x64-msvc": "11.19.1" } }, "sha512-qE/CIg/spwrTBFt5aKmwe3ifeDdLfA2NESN30E42X/lII5ClF8V7Wt6WIJhcGZjp0/Q+nQ+9vgxGk//xZNX2hg=="], + "oxc-parser": ["oxc-parser@0.140.0", "", { "dependencies": { "@oxc-project/types": "^0.140.0" }, "optionalDependencies": { "@oxc-parser/binding-android-arm-eabi": "0.140.0", "@oxc-parser/binding-android-arm64": "0.140.0", "@oxc-parser/binding-darwin-arm64": "0.140.0", "@oxc-parser/binding-darwin-x64": "0.140.0", "@oxc-parser/binding-freebsd-x64": "0.140.0", "@oxc-parser/binding-linux-arm-gnueabihf": "0.140.0", "@oxc-parser/binding-linux-arm-musleabihf": "0.140.0", "@oxc-parser/binding-linux-arm64-gnu": "0.140.0", "@oxc-parser/binding-linux-arm64-musl": "0.140.0", "@oxc-parser/binding-linux-ppc64-gnu": "0.140.0", "@oxc-parser/binding-linux-riscv64-gnu": "0.140.0", "@oxc-parser/binding-linux-riscv64-musl": "0.140.0", "@oxc-parser/binding-linux-s390x-gnu": "0.140.0", "@oxc-parser/binding-linux-x64-gnu": "0.140.0", "@oxc-parser/binding-linux-x64-musl": "0.140.0", "@oxc-parser/binding-openharmony-arm64": "0.140.0", "@oxc-parser/binding-wasm32-wasi": "0.140.0", "@oxc-parser/binding-win32-arm64-msvc": "0.140.0", "@oxc-parser/binding-win32-ia32-msvc": "0.140.0", "@oxc-parser/binding-win32-x64-msvc": "0.140.0" } }, "sha512-h6QFWd6lBMfjESqgQ27GjzrSDb0qbznp7VDQqp2zvgsrWut4vcchyMIzOVXvGQ2GMZgKw9RWrFNWv9WqGL0p7Q=="], + + "oxc-resolver": ["oxc-resolver@11.24.2", "", { "optionalDependencies": { "@oxc-resolver/binding-android-arm-eabi": "11.24.2", "@oxc-resolver/binding-android-arm64": "11.24.2", "@oxc-resolver/binding-darwin-arm64": "11.24.2", "@oxc-resolver/binding-darwin-x64": "11.24.2", "@oxc-resolver/binding-freebsd-x64": "11.24.2", "@oxc-resolver/binding-linux-arm-gnueabihf": "11.24.2", "@oxc-resolver/binding-linux-arm-musleabihf": "11.24.2", "@oxc-resolver/binding-linux-arm64-gnu": "11.24.2", "@oxc-resolver/binding-linux-arm64-musl": "11.24.2", "@oxc-resolver/binding-linux-ppc64-gnu": "11.24.2", "@oxc-resolver/binding-linux-riscv64-gnu": "11.24.2", "@oxc-resolver/binding-linux-riscv64-musl": "11.24.2", "@oxc-resolver/binding-linux-s390x-gnu": "11.24.2", "@oxc-resolver/binding-linux-x64-gnu": "11.24.2", "@oxc-resolver/binding-linux-x64-musl": "11.24.2", "@oxc-resolver/binding-openharmony-arm64": "11.24.2", "@oxc-resolver/binding-wasm32-wasi": "11.24.2", "@oxc-resolver/binding-win32-arm64-msvc": "11.24.2", "@oxc-resolver/binding-win32-x64-msvc": "11.24.2" } }, "sha512-FY91FiDBj7ls5MsFS9jN3tjz2o0/zsdSsymlakySaBwVJZorHhkWyICLZMKxlu1R9vYo+sd3z1jwb4J8x7bNDw=="], "pac-proxy-agent": ["pac-proxy-agent@7.2.0", "", { "dependencies": { "@tootallnate/quickjs-emscripten": "^0.23.0", "agent-base": "^7.1.2", "debug": "^4.3.4", "get-uri": "^6.0.1", "http-proxy-agent": "^7.0.0", "https-proxy-agent": "^7.0.6", "pac-resolver": "^7.0.1", "socks-proxy-agent": "^8.0.5" } }, "sha512-TEB8ESquiLMc0lV8vcd5Ql/JAKAoyzHFXaStwjkzpOpC5Yv+pIzLfHvjTSdf3vpa2bMiUQrg9i6276yn8666aA=="], @@ -589,7 +630,7 @@ "picocolors": ["picocolors@1.1.1", "", {}, "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA=="], - "picomatch": ["picomatch@4.0.3", "", {}, "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q=="], + "picomatch": ["picomatch@4.0.5", "", {}, "sha512-RvwwcruNjI1ncT5xRakeyS9Lf8lcItv34KD+aif+VH9kduAyfYBipGh12274xtenIPZ119/R9BdTBa8gAwSh0A=="], "progress": ["progress@2.0.3", "", {}, "sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA=="], @@ -601,12 +642,12 @@ "queue-microtask": ["queue-microtask@1.2.3", "", {}, "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A=="], - "require-directory": ["require-directory@2.1.1", "", {}, "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q=="], - "require-from-string": ["require-from-string@2.0.2", "", {}, "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw=="], "resolve-from": ["resolve-from@5.0.0", "", {}, "sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw=="], + "resolve-pkg-maps": ["resolve-pkg-maps@1.0.0", "", {}, "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw=="], + "reusify": ["reusify@1.1.0", "", {}, "sha512-g6QUff04oZpHs0eG5p83rFLhHeV00ug/Yf9nZM6fLeUrPguBTkTQOdpAWWspMh55TZfVQDPaN3NQJfbVRAxdIw=="], "run-parallel": ["run-parallel@1.2.0", "", { "dependencies": { "queue-microtask": "^1.2.2" } }, "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA=="], @@ -615,7 +656,7 @@ "sax": ["sax@1.6.0", "", {}, "sha512-6R3J5M4AcbtLUdZmRv2SygeVaM7IhrLXu9BmnOGmmACak8fiUtOsYNWUS4uK7upbmHIBbLBeFeI//477BKLBzA=="], - "semver": ["semver@7.7.4", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA=="], + "semver": ["semver@7.8.5", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA=="], "signal-exit": ["signal-exit@4.1.0", "", {}, "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw=="], @@ -623,7 +664,7 @@ "smart-buffer": ["smart-buffer@4.2.0", "", {}, "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg=="], - "smol-toml": ["smol-toml@1.6.0", "", {}, "sha512-4zemZi0HvTnYwLfrpk/CF9LOd9Lt87kAt50GnqhMpyF9U3poDAP2+iukq2bZsO/ufegbYehBkqINbsWxj4l4cw=="], + "smol-toml": ["smol-toml@1.7.0", "", {}, "sha512-aqVvWoyO21L23mb+drl4RmMXbf6N7FdHjAhTRA9ZBL7apWBgfWC16KjrASI+1p9GAroljyMHj6fK67i0UiTNvQ=="], "socks": ["socks@2.8.9", "", { "dependencies": { "ip-address": "^10.1.1", "smart-buffer": "^4.2.0" } }, "sha512-LJhUYUvItdQ0LkJTmPeaEObWXAqFyfmP85x0tch/ez9cahmhlBBLbIqDFnvBnUJGagb0JbIQrkBs1wJ+yRYpEw=="], @@ -631,15 +672,15 @@ "source-map": ["source-map@0.6.1", "", {}, "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="], - "split2": ["split2@4.2.0", "", {}, "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg=="], - - "string-width": ["string-width@4.2.3", "", { "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } }, "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="], + "string-width": ["string-width@7.2.0", "", { "dependencies": { "emoji-regex": "^10.3.0", "get-east-asian-width": "^1.0.0", "strip-ansi": "^7.1.0" } }, "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ=="], - "strip-ansi": ["strip-ansi@6.0.1", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="], + "strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="], "strip-json-comments": ["strip-json-comments@5.0.3", "", {}, "sha512-1tB5mhVo7U+ETBKNf92xT4hrQa3pm0MZ0PQvuDnWgAAGHDsfp4lPSpiS6psrSiet87wyGPh9ft6wmhOMQ0hDiw=="], - "tinyexec": ["tinyexec@1.0.2", "", {}, "sha512-W/KYk+NFhkmsYpuHq5JykngiOCnxeVL8v8dFnqxSD8qEEdRfXk1SDM6JzNqcERbcGYj9tMrDQBYV9cjgnunFIg=="], + "tinyexec": ["tinyexec@1.2.4", "", {}, "sha512-SHf/r48b7vOrjve9PxJo3MN5v5yuyjHvdUcrQffT3WXMUfnGmHDVbC4k3sHJaJTgZCwpUplIaAo5ANtMyp3YHg=="], + + "tinyglobby": ["tinyglobby@0.2.17", "", { "dependencies": { "fdir": "^6.5.0", "picomatch": "^4.0.4" } }, "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g=="], "to-regex-range": ["to-regex-range@5.0.1", "", { "dependencies": { "is-number": "^7.0.0" } }, "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ=="], @@ -651,7 +692,7 @@ "uc.micro": ["uc.micro@2.1.0", "", {}, "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A=="], - "unbash": ["unbash@2.2.0", "", {}, "sha512-X2wH19RAPZE3+ldGicOkoj/SIA83OIxcJ6Cuaw23hf8Xc6fQpvZXY0SftE2JgS0QhYLUG4uwodSI3R53keyh7w=="], + "unbash": ["unbash@4.0.3", "", {}, "sha512-3cudTErfToSc4Ggv8XGXVNVli/xHKUtUZvaY5UVwhOcUPbQGz7PeaEnT/SAVgNziZtX67KEN9swMUYkLghxA1w=="], "undici": ["undici@7.28.0", "", {}, "sha512-cRZYrTDwWznlnRiPjggAGxZXanty6M8RV1ff8Wm4LWXBp7/IG8v5DnOm74DtUBp9OONpK75YlPnIjQqX0dBDtA=="], @@ -667,17 +708,17 @@ "whatwg-mimetype": ["whatwg-mimetype@4.0.0", "", {}, "sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg=="], - "wrap-ansi": ["wrap-ansi@7.0.0", "", { "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", "strip-ansi": "^6.0.0" } }, "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q=="], + "wrap-ansi": ["wrap-ansi@9.0.2", "", { "dependencies": { "ansi-styles": "^6.2.1", "string-width": "^7.0.0", "strip-ansi": "^7.1.0" } }, "sha512-42AtmgqjV+X1VpdOfyTGOYRi0/zsoLqtXQckTmqTeybT+BDIbM/Guxo7x3pE2vtpr1ok6xRqM9OpBe+Jyoqyww=="], "xmlbuilder2": ["xmlbuilder2@4.0.3", "", { "dependencies": { "@oozcitak/dom": "^2.0.2", "@oozcitak/infra": "^2.0.2", "@oozcitak/util": "^10.0.0", "js-yaml": "^4.1.1" } }, "sha512-bx8Q1STctnNaaDymWnkfQLKofs0mGNN7rLLapJlGuV3VlvegD7Ls4ggMjE3aUSWItCCzU0PEv45lI87iSigiCA=="], "y18n": ["y18n@5.0.8", "", {}, "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA=="], - "yaml": ["yaml@2.8.2", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-mplynKqc1C2hTVYxd0PU2xQAc22TI1vShAYGksCCfxbn/dFwnHTNi1bvYsBTkhdUNtGIf5xNOg938rrSSYvS9A=="], + "yaml": ["yaml@2.9.0", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA=="], - "yargs": ["yargs@17.7.2", "", { "dependencies": { "cliui": "^8.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "require-directory": "^2.1.1", "string-width": "^4.2.3", "y18n": "^5.0.5", "yargs-parser": "^21.1.1" } }, "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w=="], + "yargs": ["yargs@18.0.0", "", { "dependencies": { "cliui": "^9.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "string-width": "^7.2.0", "y18n": "^5.0.5", "yargs-parser": "^22.0.0" } }, "sha512-4UEqdc2RYGHZc7Doyqkrqiln3p9X2DZVxaGbwhn2pi7MrRagKaOcIKe8L3OxYcbhXLgLFUS3zAYuQjKBQgmuNg=="], - "yargs-parser": ["yargs-parser@21.1.1", "", {}, "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw=="], + "yargs-parser": ["yargs-parser@22.0.0", "", {}, "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw=="], "yoctocolors-cjs": ["yoctocolors-cjs@2.1.3", "", {}, "sha512-U/PBtDf35ff0D8X8D0jfdzHYEPFxAI7jJlxZXwCSez5M3190m+QobIfh+sWDWSHMCWWJN2AWamkegn6vr6YBTw=="], @@ -685,11 +726,11 @@ "@inquirer/core/wrap-ansi": ["wrap-ansi@6.2.0", "", { "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", "strip-ansi": "^6.0.0" } }, "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA=="], - "@inquirer/external-editor/iconv-lite": ["iconv-lite@0.7.2", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw=="], + "@inquirer/external-editor/iconv-lite": ["iconv-lite@0.7.3", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ=="], - "conventional-commits-parser/meow": ["meow@13.2.0", "", {}, "sha512-pxQJQzB6djGPXh08dacEloMFopsOqGVRKFPYvPOt9XDZ1HasbgDZA74CJGreSU4G3Ak7EFJGoiH2auq+yXISgA=="], + "cosmiconfig/js-yaml": ["js-yaml@4.3.0", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.js" } }, "sha512-1td788aAnnZ5qs7V2QIRl1owjtYpbKt749Y3xauqQgwIIGF/xXWz1wMTEBx5O3LK3lXLVuqXPdPxj2BoFHaW9Q=="], - "cosmiconfig/js-yaml": ["js-yaml@4.1.1", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.js" } }, "sha512-qQKT4zQxXl8lLwBtHMWwaTcGfFOZviOJet3Oy/xmGk2gZH677CJM9EvtfdSkgWcATZhj/55JZ0rmy3myCT5lsA=="], + "cosmiconfig-typescript-loader/jiti": ["jiti@2.6.1", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ=="], "htmlparser2/entities": ["entities@7.0.1", "", {}, "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA=="], @@ -697,22 +738,26 @@ "katex/commander": ["commander@8.3.0", "", {}, "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww=="], - "knip/zod": ["zod@4.3.6", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="], + "knip/zod": ["zod@4.4.3", "", {}, "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ=="], "markdown-link-check/commander": ["commander@14.0.3", "", {}, "sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw=="], "markdownlint/string-width": ["string-width@8.2.1", "", { "dependencies": { "get-east-asian-width": "^1.5.0", "strip-ansi": "^7.1.2" } }, "sha512-IIaP0g3iy9Cyy18w3M9YcaDudujEAVHKt3a3QJg1+sr/oX96TbaGUubG0hJyCjCBThFH+tFpcIyoUHUn1ogaLA=="], - "markdownlint-cli2/smol-toml": ["smol-toml@1.7.0", "", {}, "sha512-aqVvWoyO21L23mb+drl4RmMXbf6N7FdHjAhTRA9ZBL7apWBgfWC16KjrASI+1p9GAroljyMHj6fK67i0UiTNvQ=="], - - "micromatch/picomatch": ["picomatch@2.3.1", "", {}, "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="], + "micromatch/picomatch": ["picomatch@2.3.2", "", {}, "sha512-V7+vQEJ06Z+c5tSye8S+nHUfI51xoXIXjHQ99cQtKUkQqqO1kO/KCJUfZXuB47h/YBlDhah2H3hdUGXn8ie0oA=="], "parse5/entities": ["entities@6.0.1", "", {}, "sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g=="], - "xmlbuilder2/js-yaml": ["js-yaml@4.1.1", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.js" } }, "sha512-qQKT4zQxXl8lLwBtHMWwaTcGfFOZviOJet3Oy/xmGk2gZH677CJM9EvtfdSkgWcATZhj/55JZ0rmy3myCT5lsA=="], + "xmlbuilder2/js-yaml": ["js-yaml@4.3.0", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.js" } }, "sha512-1td788aAnnZ5qs7V2QIRl1owjtYpbKt749Y3xauqQgwIIGF/xXWz1wMTEBx5O3LK3lXLVuqXPdPxj2BoFHaW9Q=="], + + "@inquirer/core/wrap-ansi/ansi-styles": ["ansi-styles@4.3.0", "", { "dependencies": { "color-convert": "^2.0.1" } }, "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg=="], + + "@inquirer/core/wrap-ansi/string-width": ["string-width@4.2.3", "", { "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } }, "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="], + + "@inquirer/core/wrap-ansi/strip-ansi": ["strip-ansi@6.0.1", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="], - "markdownlint/string-width/strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="], + "@inquirer/core/wrap-ansi/string-width/emoji-regex": ["emoji-regex@8.0.0", "", {}, "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A=="], - "markdownlint/string-width/strip-ansi/ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="], + "@inquirer/core/wrap-ansi/strip-ansi/ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], } } diff --git a/install.sh b/install.sh index 2a27aeba4..17f485ea0 100755 --- a/install.sh +++ b/install.sh @@ -58,6 +58,11 @@ LIFECYCLE_LOCK_STALE_SECONDS=600 # Set while extraction is in flight. This is private disposable source input; # durable recovery authority lives only in the promoter's internal transaction. STAGING_DIR="" +# The only exit-2 result that means "delivery committed; activation deferred". +# handoff_to_subcommand streams child stdout while capturing it privately and +# requires it to be the sole lifecycle result line, byte-exact, before projecting +# action-required. +INSTALL_ACTION_REQUIRED_TRAILER='{"schemaVersion":1,"code":"activation-pending","deliveryComplete":true,"retry":false,"nextAction":"retire tasks → genie setup --codex → /hooks → new task"}' cleanup() { local status=$? @@ -322,7 +327,7 @@ manifest_get() { manifest_channel_matches() { local payload="$1" wanted="$2" actual actual="$(manifest_get "$payload" channel)" - [[ "$actual" == "$wanted" || ( -z "$actual" && "$wanted" == "stable" ) ]] + [[ "$actual" == "$wanted" ]] } detect_platform() { @@ -624,26 +629,8 @@ verify_staged_binary() { # path. Mutation authority is the already verified staged executable, which # borrows this shell's exact lifecycle lease and uses native durable # no-clobber renames. -ensure_physical_install_directory() { - local path="$1" parent - parent="$(dirname "$path")" - [[ -d "$parent" && ! -L "$parent" ]] || - die "install directory parent is not physical: $parent" 1 - if [[ -e "$path" || -L "$path" ]]; then - [[ -d "$path" && ! -L "$path" ]] || - die "install path is not a physical directory: $path" 1 - return - fi - (umask 077; mkdir "$path") || - die "could not create physical install directory: $path" 1 - [[ -d "$path" && ! -L "$path" ]] || - die "new install directory was replaced before validation: $path" 1 -} - extract_and_link() { local tarball="$1" expected_version="$2" bin="$GENIE_HOME/bin" - ensure_physical_install_directory "$GENIE_HOME" - ensure_physical_install_directory "$bin" STAGING_DIR="${TMP_DIR}/release-payload" (umask 077; mkdir "$STAGING_DIR") || die "could not create private external release staging" 1 @@ -768,6 +755,20 @@ ensure_path_wired() { warn " open a new shell, or run: export PATH=\"$LOCAL_BIN:\$PATH\" && hash -r" } +# Return success only for a whole-line JSON-object-shaped lifecycle result. +# Pure Bash matching keeps this classifier inside the authenticated installer: +# no eval and no parser executable selected from the ambient PATH. Key order, +# ordinary whitespace, schema generations, and future code values are +# intentionally irrelevant. A prose line containing a JSON example, or JSON +# without both lifecycle keys, remains ordinary human output. +is_lifecycle_result_line() { + local line="$1" + local object_re='^[[:space:]]*\{.*\}[[:space:]]*$' + local schema_key_re='(^|[,{])[[:space:]]*"schemaVersion"[[:space:]]*:' + local code_key_re='(^|[,{])[[:space:]]*"code"[[:space:]]*:' + [[ "$line" =~ $object_re && "$line" =~ $schema_key_re && "$line" =~ $code_key_re ]] +} + # Post-install handoff: run the TypeScript-side finishing step on the binary # we just linked. The v4 legacy cleanup (stale ~/.claude/rules orchestration # file, orphaned automagik/genie/4.* plugin caches) lives there — see @@ -778,23 +779,67 @@ ensure_path_wired() { # reporting installation success would otherwise bless a partial lifecycle. # Not `exec` — exec would skip the EXIT trap and leak $TMP_DIR. handoff_to_subcommand() { - log "handing off to: genie install (post-install finishing)" + local delivery_channel="$1" + shift + log "handing off to: genie install (post-install finishing, channel=${delivery_channel})" [[ -n "$LIFECYCLE_LOCK" && -n "$LIFECYCLE_OWNER_RECORD" ]] || die "lifecycle lease was lost before the post-install finisher" 1 - local finisher_status=0 + local finisher_status=0 finisher_path="$PATH" finisher_stdout finisher_line + local -a finisher_pipeline_status=() + if [[ -n "$GH_BIN" ]]; then + [[ "$GH_BIN" == /* && "${GH_BIN##*/}" == "gh" && -x "$GH_BIN" ]] || + die "verified gh became unavailable before the post-install finisher" 1 + # The finisher's same-version delivery repair invokes `gh` by name. Keep the + # exact verifier selected above first on its process-local PATH; TMP_DIR is + # still owned by this shell and is removed only after the child returns. + finisher_path="${GH_BIN%/*}:$PATH" + fi + finisher_stdout="$TMP_DIR/finisher-stdout" + (umask 077; set -o noclobber; : > "$finisher_stdout") 2>/dev/null || + die "could not create private post-install result capture" 1 + [[ -f "$finisher_stdout" && ! -L "$finisher_stdout" ]] || + die "post-install result capture is not a physical file" 1 + # A pipeline is intentional: the builtin-only read/printf leg preserves the + # child's live stdout while the private capture provides an exact, post-exit + # line-classification record. Disable errexit only across this pipeline so + # PIPESTATUS is available for both legs. + set +e GENIE_LIFECYCLE_LEASE_PATH="$LIFECYCLE_LOCK" \ GENIE_LIFECYCLE_LEASE_OWNER="$LIFECYCLE_OWNER_RECORD" \ - "$LOCAL_BIN/genie" install "$@" || finisher_status=$? + GENIE_INSTALL_DELIVERY_CHANNEL="$delivery_channel" \ + GH_BIN="$GH_BIN" \ + PATH="$finisher_path" \ + "$LOCAL_BIN/genie" install "$@" | + while IFS= read -r finisher_line || [[ -n "$finisher_line" ]]; do + printf '%s\n' "$finisher_line" + printf '%s\n' "$finisher_line" >> "$finisher_stdout" + done + finisher_pipeline_status=("${PIPESTATUS[@]}") + set -e + finisher_status="${finisher_pipeline_status[0]:-1}" + [[ "${finisher_pipeline_status[1]:-1}" -eq 0 ]] || + die "could not capture post-install result; installation remains incomplete and retryable" 1 # Exit 2 from the finisher is delivered-but-action-required (installed # generation N ≠ delivered T): the signed binary/payload is installed, but the # Codex plugin generation was NOT activated. The finisher already printed the # single machine-readable result trailer over inherited stdout. Propagate it as # exit 2 with no all-green footer — never as a failure (die 1). Exit 2 is - # disambiguated from an unsupported-platform exit 2 by that trailer - # (deliveryComplete:true), per the lifecycle exit-matrix contract. + # disambiguated from busy/incomplete/unsupported-platform exit 2 only when the + # byte-exact action-required trailer is the sole lifecycle result line. + # Contradictory or duplicate lifecycle trailers remain visible in the stream + # but are fatal to this installer invocation. if [[ "$finisher_status" -eq 2 ]]; then - DELIVERY_ACTION_REQUIRED=1 - return 0 + local action_required_count=0 lifecycle_result_count=0 captured_line + while IFS= read -r captured_line || [[ -n "$captured_line" ]]; do + is_lifecycle_result_line "$captured_line" && + lifecycle_result_count=$((lifecycle_result_count + 1)) + [[ "$captured_line" == "$INSTALL_ACTION_REQUIRED_TRAILER" ]] && + action_required_count=$((action_required_count + 1)) + done < "$finisher_stdout" + if [[ "$action_required_count" -eq 1 && "$lifecycle_result_count" -eq 1 ]]; then + DELIVERY_ACTION_REQUIRED=1 + return 0 + fi fi [[ "$finisher_status" -eq 0 ]] || die "genie install finishing failed; installation remains incomplete and retryable" 1 @@ -847,7 +892,7 @@ main() { extract_and_link "$tarball" "$version" detect_legacy_install ensure_path_wired - handoff_to_subcommand "$@" + handoff_to_subcommand "$channel" "$@" verify_installation "$version" if [[ "${DELIVERY_ACTION_REQUIRED:-0}" -eq 1 ]]; then # Delivered/action-required: the binary is installed and verified, but the diff --git a/knip.json b/knip.json index 2a01b08a5..17c03dd27 100644 --- a/knip.json +++ b/knip.json @@ -1,8 +1,7 @@ { - "$schema": "https://unpkg.com/knip@5.85.0/schema.json", - "entry": ["src/genie.ts", "src/hooks/dispatch-command.ts"], + "$schema": "https://unpkg.com/knip@6.29.0/schema.json", + "entry": ["src/hooks/dispatch-command.ts"], "project": ["src/**/*.ts"], - "ignoreBinaries": ["which", "scripts/verify-release.sh", "markdown-link-check", "markdownlint-cli2"], - "ignoreDependencies": ["markdown-link-check", "markdownlint-cli2"], + "ignoreBinaries": ["ldd", "omni", "ss"], "ignoreExportsUsedInFile": true } diff --git a/package.json b/package.json index 305042021..d06acd4f7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@automagik/genie", - "version": "5.260722.8", + "version": "5.260724.1", "description": "Collaborative terminal toolkit for human + AI workflows. NOTE: npm distribution discontinued 2026-05-09 — install via `curl -fsSL https://raw.githubusercontent.com/automagik-dev/genie/main/install.sh | bash` (cosign + SLSA verified). See https://automagik.dev/genie/release-process", "license": "MIT", @@ -43,21 +43,24 @@ }, "dependencies": { "@inquirer/prompts": "7.10.1", + "@nats-io/transport-node": "3.4.0", + "@sigstore/bundle": "5.0.0", + "@sigstore/protobuf-specs": "0.5.0", + "@sigstore/verify": "4.1.0", "commander": "12.1.0", - "nats": "2.29.3", "zod": "3.25.76" }, "devDependencies": { "@biomejs/biome": "^1.9.4", - "@commitlint/cli": "^20.4.1", - "@commitlint/config-conventional": "^20.4.1", + "@commitlint/cli": "^21.2.1", + "@commitlint/config-conventional": "^21.2.0", "@types/bun": "^1.3.10", "@types/node": "^22.0.0", - "esbuild": "^0.27.3", + "esbuild": "^0.28.1", "husky": "^9.1.7", - "knip": "^5.86.0", + "knip": "^6.29.0", "markdown-link-check": "3.14.2", - "markdownlint-cli2": "0.23.0", + "markdownlint-cli2": "0.23.1", "typescript": "^5.8.0" }, "repository": { diff --git a/plugins/genie/.claude-plugin/plugin.json b/plugins/genie/.claude-plugin/plugin.json index 6fc401cdf..acb3ea05e 100644 --- a/plugins/genie/.claude-plugin/plugin.json +++ b/plugins/genie/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "genie", - "version": "5.260722.8", + "version": "5.260724.1", "description": "Human-AI partnership for Claude Code. Share a terminal, orchestrate workers, evolve together. Brainstorm ideas, turn them into wishes, execute with /work, validate with /review, and ship as one team.", "author": { "name": "Namastex Labs" diff --git a/plugins/genie/.codex-plugin/plugin.json b/plugins/genie/.codex-plugin/plugin.json index bc8713316..1c4f30830 100644 --- a/plugins/genie/.codex-plugin/plugin.json +++ b/plugins/genie/.codex-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "genie", - "version": "5.260722.8", + "version": "5.260724.1", "description": "Plan, execute, review, and ship software with Genie workflows in Codex.", "author": { "name": "Namastex Labs", @@ -12,14 +12,13 @@ "keywords": ["workflow", "orchestration", "codex", "skills", "agents"], "skills": "./skills/", "hooks": "./hooks/codex-hooks.json", - "mcpServers": "./.mcp.json", "interface": { "displayName": "Genie", "shortDescription": "Plan, execute, review, and ship with Genie", "longDescription": "First-class Genie workflows for Codex, including shared skills, lifecycle hooks, and MCP task state. Genie CLI setup can install optional role agents.", "developerName": "Namastex Labs", "category": "Developer Tools", - "capabilities": ["Skills", "Hooks", "MCP"], + "capabilities": ["Skills", "Hooks"], "websiteURL": "https://github.com/automagik-dev/genie", "defaultPrompt": [ "Use $genie:wish to turn this request into an executable plan.", diff --git a/plugins/genie/.mcp.json b/plugins/genie/.mcp.json deleted file mode 100644 index c05ea4edf..000000000 --- a/plugins/genie/.mcp.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "genie": { - "command": "node", - "args": ["./scripts/mcp-launcher.cjs"], - "cwd": "." - } -} diff --git a/plugins/genie/README.md b/plugins/genie/README.md index fa54557e6..bdc958282 100644 --- a/plugins/genie/README.md +++ b/plugins/genie/README.md @@ -7,10 +7,10 @@ This directory is the shared release payload for Claude Code and Codex. The two | Surface | Delivery | Contract | |---------|----------|----------| | Product skills | The plugin contains 23 physical in-root skill directories, each with `SKILL.md` and `agents/openai.yaml` | The **sole** Genie-managed skill provider; no escaping symlink and no user-tier copy — nothing is written to `~/.agents/skills` | -| Fallback retirement | Hidden `~/.agents/skills/.genie-codex-fallback-retirement/` quarantine transaction | Never written on fresh install. Upgrades from a fallback-seeding release move only provably clean, digest-owned historical copies here after one plugin health proof; `txn-/evidence/` archives retain changed trees for recovery | +| Fallback retirement | Hidden `~/.agents/skills/.genie-codex-fallback-retirement/` quarantine transaction | Never written on fresh setup. Authenticated setup moves only provably clean, digest-owned historical copies here after one plugin health proof; `txn-/evidence/` archives retain changed trees for recovery | | Hooks | `.codex-plugin/plugin.json` points to `hooks/codex-hooks.json` | Three untrusted definitions only: H3 SessionStart context, H4 local PreToolUse guardrails, H6 PermissionRequest approval | -| MCP | `.mcp.json` starts `scripts/mcp-launcher.cjs` | The launcher accepts only the canonical `$GENIE_HOME/bin/genie` (default `~/.genie/bin/genie`) and fails closed if it is absent or unsafe | -| Role agents | Seven TOMLs are staged in `codex-agents/` | Plugins cannot install custom agents. `genie install` or `genie setup --codex` copies the optional profiles into `~/.codex/agents/` behind the same plugin health gate | +| MCP | Marker-owned project `.codex/config.toml` | Codex launches the stable absolute `$GENIE_HOME/bin/genie mcp` facade with no `cwd` override; the plugin declares no Codex MCP route | +| Role agents | Seven TOMLs are staged in `codex-agents/` | Plugins cannot install custom agents. Authenticated `genie setup --codex` copies the optional profiles into `~/.codex/agents/` after enabled-plugin health/retirement; an exact deliberately disabled plugin skips retirement but still repairs roles | The plugin is the only Genie-managed skill provider. A fresh Codex install writes zero user-tier skills; bare `$` now resolves only a personal copy the user installed themselves. A maintainer may separately have 36 adapted skills under `~/.agents/skills`; those are user-owned, are not bundled here, and must survive update/uninstall byte-for-byte. An upgrade from a release that seeded digest-managed fallbacks retires only provably clean, Genie-owned copies into the hidden quarantine transaction after one health proof — a physical non-symlink directory with a valid versioned `.genie-sync.json`, a recomputed digest equal to the marker, and a match against the verified target payload or a committed verified-release historical tuple. Modified-managed, malformed-marker, symlinked, and unmanaged same-name collisions are preserved in place and reported, never adopted or deleted. @@ -24,7 +24,7 @@ The plugin is the only Genie-managed skill provider. A fresh Codex install write txn-/evidence// changed trees archived aside during recovery races ``` -The transaction is idempotent and crash-safe: repeated updates recognize the committed transaction (no second transaction, no accumulating quarantine); an interrupted run reverse-restores every pre-commit move without clobbering conflicts. Committed quarantine and journal evidence are retained. Manual recovery: +The transaction is idempotent and crash-safe: repeated setup runs recognize the committed transaction (no second transaction, no accumulating quarantine); an interrupted run reverse-restores every pre-commit move without clobbering conflicts. Committed quarantine and journal evidence are retained. Manual recovery: - **Restore a retired skill:** move it back from `txn-/quarantine//` to `~/.agents/skills//` (only if you want a bare user-tier copy; the plugin already serves `$genie:`). - **"Source changed after planning":** if the live skill was edited between the health proof and the move, retirement aborts before any move — the changed personal copy stays in place at `~/.agents/skills/`; nothing is moved or archived. Review it, then rerun. @@ -58,24 +58,24 @@ PreToolUse is a guardrail, not complete interception. Sandbox policy and server- No hook installs or updates Genie. Operators use: ```bash -genie install --integrations codex # installer-owned finishing path -genie setup --codex # install or repair Codex plugin, role agents, and MCP routing -genie update # explicit binary/payload/integration convergence +genie install --integrations codex # verify and publish the Codex delivery +genie setup --codex # activate it, retire clean fallbacks, converge roles + project route +genie update # deliver a newer signed binary/payload; never activate Codex ``` -A successful `genie setup --codex` persists Codex maintenance consent. Future explicit `genie update` invocations use -that scope to refresh the Codex plugin, MCP route, and optional role profiles. No supported path writes new product -skills into the user tier; the only user-tier mutation is retiring provably clean historical fallbacks into the hidden -quarantine transaction after a health proof. Unmanaged, modified, and separately installed personal skills stay -user-owned; persisted maintenance scope does not authorize hooks or background updates. - -When crossing from a release older than `5.260711.6` to `5.260711.6` or later, the first update may only deliver the new binary/payload because the already-running old process does not yet know the new convergence phase. After that command returns, run `genie update` once more explicitly. The second invocation runs the newly installed contract; confirm the plugin exposes exactly H3/H4/H6, review hashes with `/hooks`, and start a new task. Later updates converge in one operator-driven path. Do not rely on SessionStart for this compatibility hop. +A successful `genie setup --codex` persists Codex delivery scope. Future explicit updates use that scope to publish +authenticated delivery facts but never advance the plugin cache, change enabled state, reconcile the project route, or +write roles. When a delivered generation is pending, close or retire tasks pinned to the prior generation and run setup +from an external real terminal. Setup requires the matching record before its first prompt or mutation, activates the +exact delivered bytes, proves plugin health, retires only provably clean historical fallbacks, and then converges role +profiles and the marker-owned project route. Unmanaged, modified, and personal skills stay user-owned; persisted scope +does not authorize hooks or background updates. ### 2026-07-11 update incident One release-dogfood update exposed why that boundary matters. A `5.260710.13` process selected stale stable `5.260710.2`; its fresh child did not understand the environment-only sync request and performed another full update to `5.260711.3`. The ensuing legacy sync adopted 22 same-name personal skills and created a duplicate `review` skill. -Containment recovered all 22 adapted directories from Genie's automatic backup, quarantined both recreated `review` copies, removed the old hook trust, and verified the 36 personal-skill digests plus 14 custom-agent TOMLs against the pre-incident baseline. The code fix keeps post-update convergence in the reviewed parent process and preserves user-owned collisions. This note intentionally contains no machine-specific paths, process ids, or credentials. +Containment recovered all 22 adapted directories from Genie's automatic backup, quarantined both recreated `review` copies, removed the old hook trust, and verified the 36 personal-skill digests plus 14 custom-agent TOMLs against the pre-incident baseline. The current contract preserves user-owned collisions and separates signed update delivery from setup-owned activation and convergence. This note intentionally contains no machine-specific paths, process ids, or credentials. ## Skills and orchestration diff --git a/plugins/genie/package.json b/plugins/genie/package.json index 1fce7bfca..90bf78ec8 100644 --- a/plugins/genie/package.json +++ b/plugins/genie/package.json @@ -1,6 +1,6 @@ { "name": "genie-plugin", - "version": "5.260722.8", + "version": "5.260724.1", "private": true, "description": "Runtime dependencies for genie bundled CLIs", "license": "MIT", diff --git a/plugins/genie/references/codex-integration-map.md b/plugins/genie/references/codex-integration-map.md index e983f849b..c5ea453b0 100644 --- a/plugins/genie/references/codex-integration-map.md +++ b/plugins/genie/references/codex-integration-map.md @@ -1,6 +1,6 @@ # Codex integration map -Updated 2026-07-11 after the Ultra review of PR #2545 and its follow-up remediation. The original PR merged via `6f682e2b` from promoted source `10ceb2c0`; remediation continues on `fix/pr2545-ultra-gate` targeting `dev`. This document describes the follow-up code, not the older installed cache, and does not imply hook trust or stable-release approval. +Updated 2026-07-23 for the authenticated delivery/activation split. This document describes the repository code, not an older installed cache, and does not imply hook trust or stable-release approval. Official references: @@ -15,11 +15,11 @@ Official references: | Surface | Location | Current contract | |---------|----------|------------------| -| Plugin manifest | `plugins/genie/.codex-plugin/plugin.json` | Declares Skills, Hooks, and MCP only. Plugins do not ship custom agents | +| Plugin manifest | `plugins/genie/.codex-plugin/plugin.json` | Declares Skills and Hooks only. Plugins do not ship custom agents or the repository-scoped Genie MCP route | | Product skills | `skills/` canonical; `plugins/genie/skills/` committed mirror | Exactly 23 physical, in-root skills with valid `name`/`description` frontmatter and `agents/openai.yaml`; source/package parity is fail-closed | | Hooks | `plugins/genie/hooks/codex-hooks.json` | Exactly H3 SessionStart, H4 PreToolUse, H6 PermissionRequest; all require explicit `/hooks` review and a new task after definition changes | -| MCP | `plugins/genie/.mcp.json` -> `scripts/mcp-launcher.cjs` | Starts only canonical `$GENIE_HOME/bin/genie mcp`; no PATH/shell fallback; unsafe/missing binary fails closed | -| Optional roles | `plugins/genie/codex-agents/*.toml` -> `~/.codex/agents/` | Seven CLI-installed profiles, gated behind plugin health. Plugin-only installations have no `genie_*` agents | +| MCP | Trusted project `.codex/config.toml` -> absolute `$GENIE_HOME/bin/genie mcp` | One marker-owned, plugin-independent route with no `cwd` override; unowned routes, damaged markers, and nested shadowing are preserved and reported | +| Optional roles | `plugins/genie/codex-agents/*.toml` -> `~/.codex/agents/` | Seven setup-installed profiles, gated behind matching delivery, current plugin health, and fallback retirement | | Fallback retirement | `~/.agents/skills/.genie-codex-fallback-retirement/` | No supported path writes Genie product skills to `~/.agents/skills`. Upgrades from a fallback-seeding release retire only provably clean, digest-owned historical copies into one durable quarantine transaction after a single plugin health proof; unmanaged, malformed, symlinked, or modified collisions are preserved and reported | | Personal migration | 36 adapted skills and 14 custom agents in the maintainer's user tier | Separate user-owned installation; not part of the 23-skill product payload; survives update/uninstall byte-for-byte | @@ -43,7 +43,7 @@ Acceptance requires a physical non-symlink directory, a valid versioned `.genie-sync.json`, a recomputed canonical digest equal to the marker, and a match against either the verified target-plugin payload or a committed verified-release historical tuple. The transaction is idempotent and crash-safe: -repeated updates recognize the committed transaction (no second transaction, no +repeated setup runs recognize the committed transaction (no second transaction, no accumulating quarantine), and an interrupted run reverse-restores every pre-commit move without clobbering conflicts. Manual recovery: move a tree back from `txn-/quarantine//` to `~/.agents/skills//` only if a bare @@ -60,29 +60,64 @@ Starter-card metadata is different: every physical skill's `agents/openai.yaml` ## Hook contract -Codex hook trust is hash-specific and commands run outside the model tool sandbox. Repository or plugin edits do not update an installed cache. After explicit setup/update, operators inspect `/hooks` and start a new task; until then all changed definitions remain untrusted. H4/H6 definitions include the literal SHA-256 and contract version of the physical plugin launcher; the launcher verifies those values before any child spawn, and release gates reject definition/launcher drift. The remaining `$GENIE_HOME/bin/genie` executable is mutable and platform-specific. The current hook schema hashes normalized definitions rather than transitive executable bytes, so the universal plugin manifest cannot content-bind every release binary. Canonical-path and non-symlink checks narrow that residual but do not justify automatic trust. +Codex hook trust is hash-specific and commands run outside the model tool sandbox. Repository or plugin edits do not update an installed cache. After authenticated setup activates or repairs a generation, operators inspect `/hooks` and start a new task; until then all changed definitions remain untrusted. H4/H6 definitions include the literal SHA-256 and contract version of the physical plugin launcher; the launcher verifies those values before any child spawn, and release gates reject definition/launcher drift. The remaining `$GENIE_HOME/bin/genie` executable is mutable and platform-specific. The current hook schema hashes normalized definitions rather than transitive executable bytes, so the universal plugin manifest cannot content-bind every release binary. Canonical-path and non-symlink checks narrow that residual but do not justify automatic trust. | ID | Event | Matcher | Contract | |----|-------|---------|----------| -| H3 | `SessionStart` | `startup|resume|clear|compact` | One local read-only pass over at most 64 candidates/256 KiB; emits at most eight wish records and 2 KiB of validated slug/status/count context | -| H4 | `PreToolUse` | `Bash|Write|Edit|apply_patch` | Definition-bound launcher verification, then branch/orchestration for Bash plus audit-context for edit inputs; deterministic and network-free in Codex; no freshness/identity handler or Omni | +| H3 | `SessionStart` | `startup\|resume\|clear\|compact` | One local read-only pass over at most 64 candidates/256 KiB; emits at most eight wish records and 2 KiB of validated slug/status/count context | +| H4 | `PreToolUse` | `Bash\|Write\|Edit\|apply_patch` | Definition-bound launcher verification, then branch/orchestration for Bash plus audit-context for edit inputs; deterministic and network-free in Codex; no freshness/identity handler or Omni | | H6 | `PermissionRequest` | `*` at the host, narrowed by configured registry matcher | Definition-bound launcher verification; Omni at most once when explicitly enabled; bounded/redacted preview; valid allow/deny envelope; binding/failure/interruption/timeout denies | PreToolUse cannot intercept every possible mutation. It is defense in depth, not branch protection or a sandbox. The removed six commands installed/synchronized software, scaffolded `AGENTS.md`, validated at the wrong lifecycle points, repeatedly injected repository text, or emitted a protocol-inert Stop response. ## Installation and convergence -`genie install --integrations codex`, `genie setup --codex`, and `genie update` are the only installation/update paths. -A successful setup persists Codex maintenance consent; later explicit updates use that scope to refresh the Codex plugin, -MCP route, and role profiles. No supported path writes product skills into `~/.agents/skills`; the only user-tier -mutation is retiring provably clean historical fallbacks into the hidden quarantine transaction after a health proof, -while unmanaged, modified, and personal skills are preserved. Persisted scope does not authorize a hook or background -updater. SessionStart performs no setup, update, plugin refresh, skill synchronization, or project write. +`genie install --integrations codex` and `genie update` own signed delivery: they verify and stage the selected release, +publish its complete authenticated delivery record, and leave registration, cache, enabled state, project route, +fallbacks, and role profiles untouched. Delivery may exit 2 with `deliveryComplete:true` when activation remains. -An update crossing from a release older than `5.260711.6` to `5.260711.6` or later can deliver the new payload without the old process knowing the new convergence phase. Run one explicit `genie update` after that first command returns; the newly installed binary then converges product integrations. Current update code performs post-swap convergence inside the already-reviewed parent process and never re-enters a freshly installed older binary as `genie update`. +`genie setup --codex` owns activation and managed Codex convergence. It requires the matching record before its first +prompt or mutation, uses a fresh real-TTY retirement assertion when the generation changes, and then revalidates the +authenticated physical root around each one-shot operation. After the plugin is current, setup registers the canonical +marketplace if needed, commits explicit Codex scope, proves the exact enabled plugin and bounded MCP launcher, retires +only clean historical fallbacks, converges roles, and reconciles the marker-owned project route. A deliberately disabled +current plugin stays disabled; setup skips fallback retirement but still repairs managed roles. No supported path writes +product skills into `~/.agents/skills`, and unmanaged, modified, symlinked, and personal copies are preserved. +SessionStart performs no setup, delivery, activation, synchronization, or project write. The 2026-07-11 dogfood incident demonstrated the failure mode: `5.260710.13` selected stale stable `5.260710.2`; an environment-only sync request was misread by the fresh old child as another full update to `5.260711.3`; legacy adoption then replaced 22 same-name personal skills and created a duplicate `review`. Automatic backups restored all 22 adapted directories, both recreated review copies were quarantined, old hook trust was removed, and the 36 skill plus 14 agent baselines matched exactly. The follow-up prevents adoption of user-owned collisions and leaves final post-test baseline comparison as a release-gate action. +## Project identity and recovery + +For an untouched repository, the reconciliation command is: + +```bash +genie init +``` + +Run it from the trusted Git worktree that should own the route. It creates or repairs only Genie's +marker-owned project route and repository state; it does not deliver or activate a plugin. The route has no +`cwd` override, so a Codex-launched MCP child inherits the new task's effective working directory exactly. +Linked worktrees keep their local `.codex/config.toml` but resolve the shared task database from the Git +common directory. An initialized nested repository is a hard boundary: if its own database is absent or +unopenable, MCP returns a structured error and never falls through to an outer repository or an empty board. + +Use `genie doctor --json` from that same worktree to classify recovery: + +| Finding | Recovery | +|---------|----------| +| Project trust required | Open the repository in interactive Codex, review and trust its project config, then start a new task. Never use a trust-bypass flag | +| Unowned same-key route or nested/global shadow | Review the reported `routeLayers`, then rename or remove the user-owned `mcp_servers.genie` entry yourself. Genie preserves it. Rerun `genie init` | +| Damaged/incomplete Genie marker | Back up `.codex/config.toml`, remove only the incomplete Genie marker fragment after reviewing it, preserve every other key, then rerun `genie init` | +| `delivery-incomplete` | Run `genie update` (or `genie install --integrations codex`) to authenticate the exact release, then run `genie setup --codex` from an external real terminal | +| Activation pending or a task pinned to N | Close or retire the old task, complete `genie setup --codex`, review `/hooks`, and start a genuinely new task on T. Resuming N is not promised | +| Role or skill collision | Compare the named personal/modified file and move or rename it only if you choose; Genie never overwrites it. Rerun `genie setup --codex` | +| Relocated `GENIE_HOME` | Complete installation at the new physical home, then rerun `genie init` in every trusted worktree so the owned route names the new stable facade | +| Uninstall | Finish or retire open tasks first. `genie uninstall` removes only proven Genie-owned assets and keeps personal, modified, symlinked, collision, quarantine, and recovery evidence | + +Successful `genie init` or setup changes project/hook inputs for future tasks, not an already-running task. +After any route, plugin, or hook change, review `/hooks` and start a new task before judging MCP identity. + ## Native orchestration The active client supplies its native spawn/follow-up/wait/interrupt tools; shared skills do not name undocumented functions. Codex uses installed `genie_*` profiles when available, but native subagents share the caller's workspace unless the runtime explicitly provides isolation. For guaranteed per-group Git isolation, use `genie launch`/worktrees. @@ -98,20 +133,29 @@ transitions (with `BLOCKED` only for a recorded blocker). The review workflow uses isolated Git worktrees and can use `codex exec --ephemeral --json --output-schema` for schema-checked, non-interactive specialist lanes. Reviewers use built-in `:read-only` permissions against the repository, temporary-hosted worktrees, temporary directories, caches, and live homes; a write-requiring test is reported as blocked and may rely on separately captured exact-tree CI evidence. App-server and SDK surfaces are not required by the shipped plugin and should not be claimed as current product behavior. -## Known release boundary +## Release dogfood boundary + +Promotion evidence is generated from the exact candidate channel-manifest bytes, never from a handwritten +platform list. Every manifest entry must have one physical signed tarball, Sigstore bundle, SLSA provenance, +and one native Group F lifecycle result bound to the same manifest/artifact digests and evidence-schema +version. The completeness gate rejects missing, duplicate, skipped, unavailable, empty-board, wrong-root, +stale-task, out-of-order, or identity-inconsistent evidence. -Source/extracted payload parity, hook protocol, and user-asset ownership belong to this follow-up. Inherited publication risks—arbitrary-ref stable publish, unvalidated privileged inputs/artifact provenance, mutable third-party actions, and the inherited live-binary transaction gap—remain BLOCKING in `stable-release-security-gate`. PR-scope remediation cannot authorize stable release. +That Codex dogfood gate and the independent release-security gate are separate read-only jobs. Homolog and +stable publication require both jobs to succeed for the same candidate; neither result can substitute for +or bypass the other. Repository code does not claim the still-human external two-maintainer demonstration +tracked by `stable-release-security-gate`. ## Lifecycle exit matrix and result trailer (Group D — delivery/activation split) Genie separates signed **delivery** from cross-version **activation** (Decision 1). -**`genie setup --codex` is activation-only**: it retires the currently active plugin -generation and activates the delivered one behind an unforgeable real-TTY retirement -assertion. It does NOT install or refresh the plugin payload, marketplace registration, -or role agents — **delivery is done by `genie update` / `genie install`**. On a host -where nothing was ever delivered, setup refuses with an actionable message pointing at -`genie update`; it is never a dead end. (This is a deliberate UX change from the pre-split -behavior where `setup --codex` also installed.) +**`genie setup --codex` is activation-only with respect to release bytes**: it never +downloads, stages, or publishes a plugin payload. Behind a matching delivery record and, +when required, an unforgeable real-TTY retirement assertion, it activates the delivered +generation and converges the activation-owned marketplace, clean fallback retirement, +managed roles, and project route. **Signed delivery is done by `genie update` / +`genie install`**. On a host where nothing was delivered, setup refuses with an actionable +message pointing at update/install; it is never a dead end. Every mutating lifecycle command serialises on one exclusive Codex lifecycle lease; a loser exits 2 with machine code `codex-lifecycle-busy`, `deliveryComplete:false`, and a @@ -167,16 +211,15 @@ floor, tamper, or a TOCTOU replacement refuses with zero mutation — consent cannot waive the protocol floor, so a rollback can never reintroduce an updater that bypasses the Codex activation gate. -### Lifecycle exceptions: sync-only, verified-current init, and uninstall +### Lifecycle exceptions: sync-only, route-only init, and uninstall - **Sync-only** (`genie update --sync-only`, legacy automation) is intentionally - limited to skills and role agents; it performs no cross-version Codex activation - and mints no retirement assertion or permit. -- **Init** (`genie init`) reconciles the marker-owned project fallback ONLY when a - fresh observation is exactly `verified-current`. Pending, broken, indeterminate, - and observation-race states retain the fallback and make zero plugin/cache - mutation; init never requests an assertion/permit or treats a prior snapshot as - fresh authority. + limited to non-Codex agent synchronization; it performs no Codex observation, + activation, fallback retirement, or role convergence and mints no assertion or permit. +- **Init** (`genie init`) reconciles only the marker-owned project MCP route in a + trusted initialized repository. The route is independent of plugin availability + and activation state; init never requests an assertion/permit or mutates delivery, + journal, registration, cache, enabled state, fallbacks, or roles. - **Uninstall** (`genie uninstall`) is a deliberately separate, user-requested destructive-removal authority — not part of the activation protocol. It warns before its confirmation that current or resumable tasks can break, keeps every diff --git a/plugins/hermes-genie/plugin.yaml b/plugins/hermes-genie/plugin.yaml index 9e9b9f7ba..b683d4460 100644 --- a/plugins/hermes-genie/plugin.yaml +++ b/plugins/hermes-genie/plugin.yaml @@ -1,5 +1,5 @@ name: genie -version: 5.260722.8 +version: 5.260724.1 description: "Native Hermes surface for Genie orchestration: read-only status plus work-plan and review-plan gap tools that the MCP board surface does not cover, hooks, commands, and a thin cockpit skill. Board/task truth comes from the genie MCP tools." provides_tools: # Default surface: exactly the three gap tools the MCP board surface does not cover. diff --git a/scripts/build-binary.sh b/scripts/build-binary.sh index aaef4eeb0..0036738d6 100755 --- a/scripts/build-binary.sh +++ b/scripts/build-binary.sh @@ -128,7 +128,6 @@ for required in \ ".agents/plugins/marketplace.json" \ ".claude-plugin/marketplace.json" \ "plugins/genie/.codex-plugin/plugin.json" \ - "plugins/genie/.mcp.json" \ "plugins/genie/scripts/mcp-launcher.cjs" \ "plugins/genie/.claude-plugin/plugin.json" \ "plugins/genie/hooks/hooks.json" \ diff --git a/scripts/build-delivery-evidence.test.ts b/scripts/build-delivery-evidence.test.ts new file mode 100644 index 000000000..8c7a664db --- /dev/null +++ b/scripts/build-delivery-evidence.test.ts @@ -0,0 +1,156 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { scanPhysicalTree } from '../src/lib/codex-activation.ts'; + +const SCRIPT = join(import.meta.dir, 'build-delivery-evidence.ts'); +const roots: string[] = []; + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +function fixture() { + const root = mkdtempSync(join(tmpdir(), 'genie-delivery-builder-')); + roots.push(root); + const payload = join(root, 'payload'); + mkdirSync(join(payload, 'plugins', 'genie', 'nested'), { recursive: true }); + writeFileSync(join(payload, 'genie'), 'binary bytes'); + chmodSync(join(payload, 'genie'), 0o755); + writeFileSync(join(payload, 'plugins', 'genie', 'plugin.txt'), 'plugin bytes'); + writeFileSync(join(payload, 'plugins', 'genie', 'nested', 'data.json'), '{}\n'); + const version = '5.260723.7'; + const tarball = join(root, `genie-${version}-linux-x64-glibc.tar.gz`); + const tar = Bun.spawnSync(['tar', '-czf', tarball, '-C', payload, '.']); + expect(tar.exitCode).toBe(0); + const manifest = join(root, 'dev.json'); + writeFileSync( + manifest, + '{"schema_version":1,"channel":"dev","version":"5.260723.7","released_at":"2026-07-23T00:00:00Z","tarball_base":"https://github.com/automagik-dev/genie/releases/download/v5.260723.7","platforms":["linux-x64-glibc","linux-x64-musl","linux-arm64","darwin-arm64"]}\n', + ); + return { root, version, tarball, manifest, output: join(root, 'descriptor.json') }; +} + +function run(f: ReturnType, output = f.output) { + return Bun.spawnSync( + [ + 'bun', + SCRIPT, + '--tarball', + f.tarball, + '--manifest', + f.manifest, + '--output', + output, + '--repository', + 'automagik-dev/genie', + '--version', + f.version, + '--channel', + 'dev', + '--platform-id', + 'linux-x64-glibc', + '--source-sha', + 'a'.repeat(40), + '--source-branch', + 'dev', + '--source-ci-run-id', + '12345', + '--control-sha', + 'b'.repeat(40), + ], + { stdout: 'pipe', stderr: 'pipe' }, + ); +} + +describe('canonical delivery evidence descriptor builder', () => { + test('derives deterministic raw-manifest, tarball, binary, and physical plugin tree bindings', () => { + const first = fixture(); + expect(run(first).exitCode).toBe(0); + const bytes = readFileSync(first.output, 'utf8'); + const descriptor = JSON.parse(bytes); + expect(Object.keys(descriptor)).toEqual([ + 'schemaVersion', + 'repository', + 'version', + 'channel', + 'platformId', + 'platformTriple', + 'releaseTag', + 'releaseName', + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'canonicalPayloadSha256', + 'digestAlgorithm', + 'sourceSha', + 'sourceBranch', + 'sourceCiRunId', + 'controlSha', + ]); + expect(descriptor).toMatchObject({ + schemaVersion: 1, + repository: 'automagik-dev/genie', + version: first.version, + channel: 'dev', + platformId: 'linux-x64-glibc', + platformTriple: 'linux-x64', + releaseTag: `v${first.version}`, + releaseName: `genie-${first.version}-linux-x64-glibc.tar.gz`, + digestAlgorithm: 'genie-physical-tree-v1', + sourceSha: 'a'.repeat(40), + sourceBranch: 'dev', + sourceCiRunId: '12345', + controlSha: 'b'.repeat(40), + }); + for (const key of ['releaseManifestSha256', 'artifactSha256', 'installedBinarySha256', 'canonicalPayloadSha256']) { + expect(descriptor[key]).toMatch(/^[0-9a-f]{64}$/); + } + expect(descriptor.canonicalPayloadSha256).toBe( + scanPhysicalTree(join(first.root, 'payload', 'plugins', 'genie')).digest, + ); + expect(bytes.endsWith('\n')).toBe(true); + + const replay = join(first.root, 'replay.json'); + expect(run(first, replay).exitCode).toBe(0); + expect(readFileSync(replay)).toEqual(readFileSync(first.output)); + }); + + test('the digest binds exact raw manifest bytes, including formatting', () => { + const f = fixture(); + expect(run(f).exitCode).toBe(0); + const before = JSON.parse(readFileSync(f.output, 'utf8')).releaseManifestSha256; + const parsed = JSON.parse(readFileSync(f.manifest, 'utf8')); + writeFileSync(f.manifest, `${JSON.stringify(parsed, null, 2)}\n`); + const changed = join(f.root, 'changed.json'); + expect(run(f, changed).exitCode).toBe(0); + expect(JSON.parse(readFileSync(changed, 'utf8')).releaseManifestSha256).not.toBe(before); + }); + + test('fails closed on a mismatched asset name or unsafe plugin member', () => { + const mismatched = fixture(); + const renamed = join(mismatched.root, 'genie-5.260723.7-linux-arm64.tar.gz'); + writeFileSync(renamed, readFileSync(mismatched.tarball)); + mismatched.tarball = renamed; + expect(run(mismatched).exitCode).toBe(2); + + const unsafe = fixture(); + rmSync(unsafe.tarball); + const link = join(unsafe.root, 'payload', 'plugins', 'genie', 'link'); + Bun.spawnSync(['ln', '-s', 'plugin.txt', link]); + Bun.spawnSync(['tar', '-czf', unsafe.tarball, '-C', join(unsafe.root, 'payload'), '.']); + const result = run(unsafe); + expect(result.exitCode).toBe(2); + expect(result.stderr.toString()).toContain('link or unsupported'); + + const hardlink = fixture(); + rmSync(hardlink.tarball); + const hardlinkPath = join(hardlink.root, 'payload', 'plugins', 'genie', 'hardlink'); + Bun.spawnSync(['ln', join(hardlink.root, 'payload', 'plugins', 'genie', 'plugin.txt'), hardlinkPath]); + Bun.spawnSync(['tar', '-czf', hardlink.tarball, '-C', join(hardlink.root, 'payload'), '.']); + const hardlinkResult = run(hardlink); + expect(hardlinkResult.exitCode).toBe(2); + expect(hardlinkResult.stderr.toString()).toContain('unsupported member type'); + }); +}); diff --git a/scripts/build-delivery-evidence.ts b/scripts/build-delivery-evidence.ts new file mode 100644 index 000000000..4a456e105 --- /dev/null +++ b/scripts/build-delivery-evidence.ts @@ -0,0 +1,242 @@ +#!/usr/bin/env bun + +import { createHash } from 'node:crypto'; +import { + chmodSync, + closeSync, + lstatSync, + mkdtempSync, + openSync, + readFileSync, + readSync, + readdirSync, + rmSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { basename, join, relative, resolve, sep } from 'node:path'; + +const VERSION_RE = /^[0-9]+\.[0-9]+\.[0-9]+$/; +const SHA_RE = /^[0-9a-f]{40}$/; +const RUN_ID_RE = /^[0-9]+$/; +const REPOSITORY_RE = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/; +const PLATFORMS = { + 'linux-x64-glibc': 'linux-x64', + 'linux-x64-musl': 'linux-x64', + 'linux-arm64': 'linux-arm64', + 'darwin-arm64': 'darwin-arm64', +} as const; + +interface Arguments { + tarball: string; + manifest: string; + output: string; + repository: string; + version: string; + channel: 'stable' | 'homolog' | 'dev'; + platformId: keyof typeof PLATFORMS; + sourceSha: string; + sourceBranch: 'main' | 'homolog' | 'dev'; + sourceCiRunId: string; + controlSha: string; +} + +function fail(message: string): never { + console.error(`delivery-evidence: ${message}`); + process.exit(2); +} + +function parseArguments(argv: string[]): Arguments { + const values = new Map(); + for (let index = 0; index < argv.length; index += 2) { + const key = argv[index]; + const value = argv[index + 1]; + if (!key?.startsWith('--') || value === undefined) fail(`invalid argument near ${key ?? ''}`); + if (values.has(key)) fail(`duplicate argument ${key}`); + values.set(key, value); + } + const required = (key: string): string => values.get(key) ?? fail(`missing ${key}`); + const args = { + tarball: resolve(required('--tarball')), + manifest: resolve(required('--manifest')), + output: resolve(required('--output')), + repository: required('--repository'), + version: required('--version'), + channel: required('--channel'), + platformId: required('--platform-id'), + sourceSha: required('--source-sha'), + sourceBranch: required('--source-branch'), + sourceCiRunId: required('--source-ci-run-id'), + controlSha: required('--control-sha'), + }; + if (values.size !== 11) fail('unknown argument'); + if (!REPOSITORY_RE.test(args.repository)) fail('invalid repository'); + if (!VERSION_RE.test(args.version)) fail('invalid version'); + if (!['stable', 'homolog', 'dev'].includes(args.channel)) fail('invalid channel'); + if (!(args.platformId in PLATFORMS)) fail('invalid platform id'); + if (!['main', 'homolog', 'dev'].includes(args.sourceBranch)) fail('invalid source branch'); + if (args.channel === 'stable' && args.sourceBranch !== 'main') fail('stable evidence requires a main source'); + if (args.channel === 'homolog' && args.sourceBranch === 'dev') + fail('homolog evidence requires main or homolog source'); + if (!SHA_RE.test(args.sourceSha) || !SHA_RE.test(args.controlSha)) fail('invalid source/control SHA'); + if (!RUN_ID_RE.test(args.sourceCiRunId)) fail('invalid source CI run ID'); + return args as Arguments; +} + +function sha256File(path: string): string { + const digest = createHash('sha256'); + const fd = openSync(path, 'r'); + try { + const buffer = Buffer.alloc(64 * 1024); + for (;;) { + const count = readSync(fd, buffer, 0, buffer.length, null); + if (count <= 0) break; + digest.update(buffer.subarray(0, count)); + } + } finally { + closeSync(fd); + } + return digest.digest('hex'); +} + +function collectPhysicalTreeEntries(root: string, current: string, entries: string[]): void { + for (const name of readdirSync(current).sort()) { + const absolute = join(current, name); + const path = relative(root, absolute).split(sep).join('/'); + const stat = lstatSync(absolute); + if (stat.isSymbolicLink()) fail(`plugin tree contains symlink at ${path}`); + if (stat.isDirectory()) { + entries.push(`D\0${path}\0`); + collectPhysicalTreeEntries(root, absolute, entries); + } else if (stat.isFile()) { + entries.push(`F\0${path}\0${(stat.mode & 0o111) !== 0 ? 'x' : '-'}\0${sha256File(absolute)}\0`); + } else { + fail(`plugin tree contains unsupported entry at ${path}`); + } + } +} + +function physicalTreeDigest(root: string): string { + const stat = lstatSync(root); + if (!stat.isDirectory() || stat.isSymbolicLink()) fail('plugins/genie must be a physical directory'); + const entries: string[] = []; + collectPhysicalTreeEntries(root, root, entries); + const digest = createHash('sha256'); + digest.update('genie-codex-activation-tree-v1\0'); + for (const entry of entries.sort()) digest.update(entry); + return digest.digest('hex'); +} + +function assertSafeArchiveListing(tarball: string): void { + const listing = Bun.spawnSync(['tar', '-tzf', tarball], { stdout: 'pipe', stderr: 'pipe' }); + if (listing.exitCode !== 0) fail(`cannot list tarball: ${listing.stderr.toString().trim()}`); + for (const raw of listing.stdout.toString().split('\n')) { + if (!raw) continue; + const path = raw.replace(/^\.\//, '').replace(/\/$/, ''); + if (!path) continue; + if (path.startsWith('/') || path.includes('\\') || path.split('/').some((part) => part === '..')) { + fail(`unsafe archive path ${raw}`); + } + } + const verbose = Bun.spawnSync(['tar', '-tvzf', tarball], { stdout: 'pipe', stderr: 'pipe' }); + if (verbose.exitCode !== 0) fail(`cannot inspect tarball entry types: ${verbose.stderr.toString().trim()}`); + for (const line of verbose.stdout.toString().split('\n')) { + if (!line) continue; + const kind = line[0]; + if (kind !== '-' && kind !== 'd') fail(`archive contains link or unsupported member type ${kind}`); + } +} + +function assertPhysicalArchiveTree(current: string): void { + for (const name of readdirSync(current)) { + const path = join(current, name); + const stat = lstatSync(path); + if (stat.isSymbolicLink()) fail(`archive extracted a symlink at ${path}`); + if (stat.isDirectory()) assertPhysicalArchiveTree(path); + else if (!stat.isFile()) fail(`archive extracted an unsupported entry at ${path}`); + } +} + +function extractTarball(tarball: string, root: string): void { + assertSafeArchiveListing(tarball); + const extracted = Bun.spawnSync(['tar', '-xzf', tarball, '--no-same-owner', '--no-same-permissions', '-C', root], { + stdout: 'pipe', + stderr: 'pipe', + }); + if (extracted.exitCode !== 0) fail(`cannot extract tarball: ${extracted.stderr.toString().trim()}`); + assertPhysicalArchiveTree(root); +} + +function assertManifestBinding(args: Arguments): void { + let manifest: unknown; + try { + manifest = JSON.parse(readFileSync(args.manifest, 'utf8')); + } catch { + fail('manifest is not valid JSON'); + } + if (typeof manifest !== 'object' || manifest === null || Array.isArray(manifest)) fail('manifest must be an object'); + const value = manifest as Record; + const keys = Object.keys(value).sort(); + const expectedKeys = ['channel', 'platforms', 'released_at', 'schema_version', 'tarball_base', 'version'].sort(); + if (JSON.stringify(keys) !== JSON.stringify(expectedKeys)) fail('manifest has an unsupported schema'); + if ( + value.schema_version !== 1 || + value.channel !== args.channel || + value.version !== args.version || + value.tarball_base !== `https://github.com/${args.repository}/releases/download/v${args.version}` || + !Array.isArray(value.platforms) || + JSON.stringify(value.platforms) !== JSON.stringify(Object.keys(PLATFORMS)) || + typeof value.released_at !== 'string' || + !/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/.test(value.released_at) || + !Number.isFinite(Date.parse(value.released_at)) + ) { + fail('manifest does not bind the descriptor release/channel/platform set'); + } +} + +function main(): void { + const args = parseArguments(process.argv.slice(2)); + const releaseName = basename(args.tarball); + const expectedName = `genie-${args.version}-${args.platformId}.tar.gz`; + if (releaseName !== expectedName) fail(`tarball name must be ${expectedName}`); + for (const path of [args.tarball, args.manifest]) { + const stat = lstatSync(path); + if (!stat.isFile() || stat.isSymbolicLink() || stat.size === 0) + fail(`input must be a nonempty physical file: ${path}`); + } + assertManifestBinding(args); + + const extractionRoot = mkdtempSync(join(tmpdir(), 'genie-delivery-evidence-')); + try { + chmodSync(extractionRoot, 0o700); + extractTarball(args.tarball, extractionRoot); + const binary = join(extractionRoot, 'genie'); + const binaryStat = lstatSync(binary); + if (!binaryStat.isFile() || binaryStat.isSymbolicLink()) fail('tarball genie member must be a physical file'); + + const descriptor = { + schemaVersion: 1, + repository: args.repository, + version: args.version, + channel: args.channel, + platformId: args.platformId, + platformTriple: PLATFORMS[args.platformId], + releaseTag: `v${args.version}`, + releaseName, + releaseManifestSha256: sha256File(args.manifest), + artifactSha256: sha256File(args.tarball), + installedBinarySha256: sha256File(binary), + canonicalPayloadSha256: physicalTreeDigest(join(extractionRoot, 'plugins', 'genie')), + digestAlgorithm: 'genie-physical-tree-v1', + sourceSha: args.sourceSha, + sourceBranch: args.sourceBranch, + sourceCiRunId: args.sourceCiRunId, + controlSha: args.controlSha, + } as const; + writeFileSync(args.output, `${JSON.stringify(descriptor)}\n`, { flag: 'wx', mode: 0o600 }); + } finally { + rmSync(extractionRoot, { recursive: true, force: true }); + } +} + +main(); diff --git a/scripts/candidate-dogfood-matrix.test.ts b/scripts/candidate-dogfood-matrix.test.ts new file mode 100644 index 000000000..53ede082d --- /dev/null +++ b/scripts/candidate-dogfood-matrix.test.ts @@ -0,0 +1,91 @@ +import { describe, expect, test } from 'bun:test'; +import { mkdirSync, mkdtempSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { NATIVE_DOGFOOD_TARGETS, buildCandidateDogfoodMatrix } from './candidate-dogfood-matrix.ts'; + +function fixture(): { root: string; manifest: string; dist: string; value: Record } { + const root = mkdtempSync(join(tmpdir(), 'genie-candidate-dogfood-')); + const dist = join(root, 'dist'); + mkdirSync(dist); + const manifest = join(root, 'homolog.json'); + const platforms = Object.keys(NATIVE_DOGFOOD_TARGETS); + const value = { + schema_version: 1, + channel: 'homolog', + version: '5.260723.9', + released_at: '2026-07-23T00:00:00Z', + tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v5.260723.9', + platforms, + }; + writeFileSync(manifest, `${JSON.stringify(value, null, 2)}\n`); + for (const platform of platforms) { + const tarball = join(dist, `genie-5.260723.9-${platform}.tar.gz`); + writeFileSync(tarball, `artifact:${platform}\n`); + writeFileSync(`${tarball}.bundle`, `bundle:${platform}\n`); + writeFileSync(`${tarball}.intoto.jsonl`, `provenance:${platform}\n`); + } + return { root, manifest, dist, value }; +} + +describe('candidate dogfood manifest-derived native matrix', () => { + test('emits exactly one digest-bound native entry per manifest platform', () => { + const fx = fixture(); + const matrix = buildCandidateDogfoodMatrix(fx.manifest, fx.dist); + expect(matrix.include.map((entry) => entry.platform)).toEqual(fx.value.platforms); + expect(new Set(matrix.include.map((entry) => entry.manifestSha256)).size).toBe(1); + expect(matrix.include.every((entry) => entry.artifactSha256.length === 64)).toBe(true); + expect(matrix.include.find((entry) => entry.platform === 'linux-arm64')?.runner).toBe('ubuntu-24.04-arm'); + expect(matrix.include.find((entry) => entry.platform === 'linux-x64-musl')?.execution).toBe('alpine-container'); + expect(matrix.include.find((entry) => entry.platform === 'darwin-arm64')?.runner).toBe('macos-15'); + expect(matrix.include.every((entry) => !entry.artifact.includes(fx.root))).toBe(true); + }); + + test('rejects missing, extra, and duplicate manifest entries', () => { + for (const mutate of [ + (platforms: string[]) => platforms.slice(1), + (platforms: string[]) => [...platforms, 'windows-x64'], + (platforms: string[]) => [...platforms, platforms[0]], + ]) { + const fx = fixture(); + fx.value.platforms = mutate(fx.value.platforms as string[]); + writeFileSync(fx.manifest, `${JSON.stringify(fx.value)}\n`); + expect(() => buildCandidateDogfoodMatrix(fx.manifest, fx.dist)).toThrow(/target mismatch|duplicate/); + } + }); + + test('rejects a missing artifact, bundle, or provenance entry', () => { + for (const suffix of ['', '.bundle', '.intoto.jsonl']) { + const fx = fixture(); + const path = join(fx.dist, `genie-5.260723.9-linux-arm64.tar.gz${suffix}`); + writeFileSync(path, ''); + expect(() => buildCandidateDogfoodMatrix(fx.manifest, fx.dist)).toThrow(/must not be empty/); + } + }); + + test('rejects symlinked candidate inputs', () => { + const fx = fixture(); + const target = join(fx.root, 'replacement'); + writeFileSync(target, 'replacement\n'); + const bundle = join(fx.dist, 'genie-5.260723.9-darwin-arm64.tar.gz.bundle'); + unlinkSync(bundle); + symlinkSync(target, bundle); + expect(() => buildCandidateDogfoodMatrix(fx.manifest, fx.dist)).toThrow(/physical regular file/); + }); + + test('rejects manifest version/base disagreement and unexpected fields', () => { + for (const mutate of [ + (value: Record) => { + value.tarball_base = 'https://example.invalid/releases/download/v5.260723.8'; + }, + (value: Record) => { + value.untrusted = true; + }, + ]) { + const fx = fixture(); + mutate(fx.value); + writeFileSync(fx.manifest, `${JSON.stringify(fx.value)}\n`); + expect(() => buildCandidateDogfoodMatrix(fx.manifest, fx.dist)).toThrow(); + } + }); +}); diff --git a/scripts/candidate-dogfood-matrix.ts b/scripts/candidate-dogfood-matrix.ts new file mode 100644 index 000000000..6daaec071 --- /dev/null +++ b/scripts/candidate-dogfood-matrix.ts @@ -0,0 +1,182 @@ +#!/usr/bin/env bun + +/** + * Convert one exact candidate channel manifest into the native dogfood matrix. + * + * The candidate manifest is the inventory authority. The static map below only + * assigns each admitted platform to a runner/execution adapter; it cannot add a + * platform that the manifest did not name. Every manifest entry must have one + * physical same-run tarball, Sigstore bundle, and SLSA provenance file. + */ + +import { createHash } from 'node:crypto'; +import { type Stats, lstatSync, readFileSync, writeFileSync } from 'node:fs'; +import { basename, join, resolve } from 'node:path'; + +const VERSION = /^\d+\.\d{6}\.\d+$/; +const CHANNELS = new Set(['dev', 'homolog', 'stable']); + +export const NATIVE_DOGFOOD_TARGETS = Object.freeze({ + 'linux-x64-glibc': { runner: 'ubuntu-latest', execution: 'host-native' }, + 'linux-x64-musl': { runner: 'ubuntu-latest', execution: 'alpine-container' }, + 'linux-arm64': { runner: 'ubuntu-24.04-arm', execution: 'host-native' }, + 'darwin-arm64': { runner: 'macos-15', execution: 'host-native' }, +} as const); + +export type NativeDogfoodPlatform = keyof typeof NATIVE_DOGFOOD_TARGETS; + +export interface CandidateDogfoodMatrixEntry { + platform: NativeDogfoodPlatform; + runner: (typeof NATIVE_DOGFOOD_TARGETS)[NativeDogfoodPlatform]['runner']; + execution: (typeof NATIVE_DOGFOOD_TARGETS)[NativeDogfoodPlatform]['execution']; + version: string; + channel: 'dev' | 'homolog' | 'stable'; + manifest: string; + manifestSha256: string; + artifact: string; + artifactSha256: string; + bundle: string; + provenance: string; +} + +export interface CandidateDogfoodMatrix { + include: CandidateDogfoodMatrixEntry[]; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value); +} + +function sha256(bytes: Uint8Array): string { + return createHash('sha256').update(bytes).digest('hex'); +} + +function readPhysicalFile(path: string, label: string): Buffer { + let stat: Stats; + try { + stat = lstatSync(path); + } catch { + throw new Error(`${label} is missing: ${path}`); + } + if (!stat.isFile() || stat.isSymbolicLink()) throw new Error(`${label} must be a physical regular file: ${path}`); + const bytes = readFileSync(path); + if (bytes.length === 0) throw new Error(`${label} must not be empty: ${path}`); + return bytes; +} + +function parseManifest(bytes: Uint8Array): { + version: string; + channel: 'dev' | 'homolog' | 'stable'; + platforms: NativeDogfoodPlatform[]; +} { + let value: unknown; + try { + value = JSON.parse(Buffer.from(bytes).toString('utf8')); + } catch { + throw new Error('candidate manifest is not valid JSON'); + } + if (!isRecord(value)) throw new Error('candidate manifest must be an object'); + const keys = Object.keys(value).sort(); + const expectedKeys = ['channel', 'platforms', 'released_at', 'schema_version', 'tarball_base', 'version']; + if (JSON.stringify(keys) !== JSON.stringify(expectedKeys)) { + throw new Error(`candidate manifest has unexpected keys: ${keys.join(', ')}`); + } + if (value.schema_version !== 1) throw new Error('candidate manifest schema_version must be 1'); + if (typeof value.version !== 'string' || !VERSION.test(value.version)) { + throw new Error('candidate manifest version is invalid'); + } + if (typeof value.channel !== 'string' || !CHANNELS.has(value.channel)) { + throw new Error('candidate manifest channel is invalid'); + } + if (!Array.isArray(value.platforms) || value.platforms.some((platform) => typeof platform !== 'string')) { + throw new Error('candidate manifest platforms must be a string array'); + } + const platforms = value.platforms as string[]; + if (new Set(platforms).size !== platforms.length) throw new Error('candidate manifest contains duplicate platforms'); + const supported = Object.keys(NATIVE_DOGFOOD_TARGETS); + const missing = supported.filter((platform) => !platforms.includes(platform)); + const extra = platforms.filter((platform) => !(platform in NATIVE_DOGFOOD_TARGETS)); + if (missing.length > 0 || extra.length > 0) { + throw new Error( + `candidate manifest/native target mismatch (missing: ${missing.join(', ') || 'none'}; extra: ${extra.join(', ') || 'none'})`, + ); + } + if (typeof value.tarball_base !== 'string' || !value.tarball_base.endsWith(`/v${value.version}`)) { + throw new Error('candidate manifest tarball_base does not bind its version'); + } + if (typeof value.released_at !== 'string' || Number.isNaN(Date.parse(value.released_at))) { + throw new Error('candidate manifest released_at is invalid'); + } + return { + version: value.version, + channel: value.channel as 'dev' | 'homolog' | 'stable', + platforms: platforms as NativeDogfoodPlatform[], + }; +} + +export function buildCandidateDogfoodMatrix(manifestPath: string, artifactDir: string): CandidateDogfoodMatrix { + const physicalManifest = resolve(manifestPath); + const manifestBytes = readPhysicalFile(physicalManifest, 'candidate manifest'); + const manifest = parseManifest(manifestBytes); + const manifestSha256 = sha256(manifestBytes); + const physicalArtifactDir = resolve(artifactDir); + const include = manifest.platforms.map((platform): CandidateDogfoodMatrixEntry => { + const artifactName = `genie-${manifest.version}-${platform}.tar.gz`; + const artifact = join(physicalArtifactDir, artifactName); + const bundle = `${artifact}.bundle`; + const provenance = `${artifact}.intoto.jsonl`; + const artifactBytes = readPhysicalFile(artifact, `candidate artifact ${platform}`); + readPhysicalFile(bundle, `candidate Sigstore bundle ${platform}`); + readPhysicalFile(provenance, `candidate SLSA provenance ${platform}`); + return { + platform, + ...NATIVE_DOGFOOD_TARGETS[platform], + version: manifest.version, + channel: manifest.channel, + manifest: basename(physicalManifest), + manifestSha256, + artifact: artifactName, + artifactSha256: sha256(artifactBytes), + bundle: `${artifactName}.bundle`, + provenance: `${artifactName}.intoto.jsonl`, + }; + }); + if (include.length !== manifest.platforms.length) throw new Error('candidate matrix lost manifest entries'); + return { include }; +} + +function parseArgs(argv: string[]): { manifest: string; artifactDir: string; output: string | null } { + let manifest = ''; + let artifactDir = ''; + let output: string | null = null; + for (let index = 0; index < argv.length; index += 2) { + const flag = argv[index]; + const value = argv[index + 1]; + if (!value) throw new Error(`missing value for ${flag ?? ''}`); + if (flag === '--manifest') manifest = value; + else if (flag === '--artifact-dir') artifactDir = value; + else if (flag === '--output') output = value; + else throw new Error(`unknown argument: ${flag}`); + } + if (!manifest || !artifactDir) { + throw new Error( + 'usage: bun scripts/candidate-dogfood-matrix.ts --manifest --artifact-dir [--output ]', + ); + } + return { manifest, artifactDir, output }; +} + +function main(): void { + try { + const args = parseArgs(process.argv.slice(2)); + const matrix = buildCandidateDogfoodMatrix(args.manifest, args.artifactDir); + const serialized = `${JSON.stringify(matrix)}\n`; + if (args.output) writeFileSync(args.output, serialized, { flag: 'wx', mode: 0o600 }); + process.stdout.write(serialized); + } catch (error) { + console.error(`candidate-dogfood-matrix: ${error instanceof Error ? error.message : String(error)}`); + process.exit(1); + } +} + +if (import.meta.main) main(); diff --git a/scripts/codex-debug-discovery-smoke.ts b/scripts/codex-debug-discovery-smoke.ts index 2c14ab694..6a5c60520 100644 --- a/scripts/codex-debug-discovery-smoke.ts +++ b/scripts/codex-debug-discovery-smoke.ts @@ -44,8 +44,9 @@ import { installGenieHome, linkRealCodex, readCodexGeniePlugin, - runCli, runCodex, + runLifecycleCli, + runLifecycleSetup, withIsolatedHome, } from './codex-smoke-harness.ts'; @@ -99,9 +100,12 @@ function main(): void { withIsolatedHome((iso) => { installGenieHome(iso); linkRealCodex(iso); - const install = runCli(iso, ['install', '--integrations', 'codex']); - if (install.exitCode !== 0) - fail(`install --integrations codex failed: ${install.stderr.trim() || install.stdout.trim()}`); + const publish = runLifecycleCli(iso, ['publish-delivery']); + if (publish.exitCode !== 0) { + fail(`delivery fixture publication failed: ${publish.stderr.trim() || publish.stdout.trim()}`); + } + const setup = runLifecycleSetup(iso); + if (setup.exitCode !== 0) fail(`setup --codex failed: ${setup.output.trim()}`); const version = readCodexGeniePlugin(iso).version; const canonicalNames = readdirSync(join(activePluginRoot(iso, version), 'skills'), { withFileTypes: true }) diff --git a/scripts/codex-plugin-only-smoke.ts b/scripts/codex-plugin-only-smoke.ts index ca9be4cd4..98b8d54e5 100644 --- a/scripts/codex-plugin-only-smoke.ts +++ b/scripts/codex-plugin-only-smoke.ts @@ -15,7 +15,7 @@ * * Coverage: * 1 fresh install (C1) — 1 enabled plugin + MCP, 0 fallbacks, 0 txn - * 2 pure-23 upgrade + idempotency (C1) — 23 retired, one committed txn, stable across 3 updates + * 2 pure-23 upgrade + idempotency (C1) — 23 retired, one committed txn, stable across 3 setups * 3 mixed collisions + regressions (C4) — personal classes + dangling symlink + Claude untouched, 7 role agents * 4 disabled-plugin path (C4/A7) — role agents installed, retirement skipped, stays disabled * 5 health-failure path (C4/A7) — nonzero, role agents + fallbacks untouched, no txn @@ -42,7 +42,6 @@ import { homedir } from 'node:os'; import { join } from 'node:path'; import { atomicRenameDirectoryNoClobber, resolveLinuxRenameat2 } from '../src/lib/agent-sync.ts'; import { - type CliResult, DIST_CLI, type IsolatedHome, REPO_ROOT, @@ -51,9 +50,11 @@ import { SmokeFailure, TARGET_VERSION, activePluginRoot, + assertEffectiveCodexProjectRoute, assertNoStaleTempHomes, assertPluginHealthy, assertProtectedUnchanged, + assertSingleCodexProjectRouteMarker, buildCliOnce, captureProtected, diffTree, @@ -63,13 +64,17 @@ import { installGenieHome, linkRealCodex, readCodexGeniePlugin, - readDoctorChecks, + readLifecycleDoctorChecks, req, runCli, + runLifecycleCli, + runLifecycleSetup, + runRealCodex, seedPersonalFixtures, seedShippedFallbackLayout, snapshotNode, snapshotTree, + trustIsolatedCodexProject, withIsolatedHome, } from './codex-smoke-harness.ts'; @@ -79,11 +84,18 @@ const ROLE_AGENT_COUNT = 7; // Local helpers // ============================================================================ -function convergeUpdate(iso: IsolatedHome): CliResult { - // `update --post-delivery-converge` is the offline full-update convergence - // path: it re-proves plugin health and retires accepted fallbacks with no - // network manifest fetch or binary swap. - return runCli(iso, ['update', '--post-delivery-converge']); +function publishDelivery(iso: IsolatedHome, phase: string): void { + const publish = runLifecycleCli(iso, ['publish-delivery']); + if (publish.exitCode !== 0) { + fail(`delivery fixture publication failed ${phase}: ${publish.stderr.trim() || publish.stdout.trim()}`); + } +} + +function setupCodex(iso: IsolatedHome, phase: string): void { + const setup = runLifecycleSetup(iso); + if (setup.exitCode !== 0) { + fail(`setup --codex failed ${phase}: ${setup.output.trim()}`); + } } function assertCommittedTransaction(summary: RetirementSummary, expected: number): void { @@ -110,8 +122,10 @@ function assertRoleAgents(iso: IsolatedHome, expected: number): void { } function codexTierDetail(iso: IsolatedHome, phase: string): string { - return req(findCheck(readDoctorChecks(iso), 'agent sync: codex'), `doctor missing 'agent sync: codex' ${phase}`) - .detail; + return req( + findCheck(readLifecycleDoctorChecks(iso), 'agent sync: codex'), + `doctor missing 'agent sync: codex' ${phase}`, + ).detail; } function seedDanglingSymlink(iso: IsolatedHome): string { @@ -188,18 +202,22 @@ function isRecord(value: unknown): value is Record { function assertInstalledPluginMcpShape(iso: IsolatedHome, version: string): void { const root = join(iso.codexHome, 'plugins', 'cache', 'automagik', 'genie', version); - const config: unknown = JSON.parse(readFileSync(join(root, '.mcp.json'), 'utf8')); - const configRecord = isRecord(config) ? config : {}; - const wrapped = configRecord.mcp_servers; - const servers = isRecord(wrapped) ? wrapped : configRecord; - const genie = servers.genie; - const command = isRecord(genie) ? genie.command : undefined; - const args = isRecord(genie) ? genie.args : undefined; - const cwd = isRecord(genie) ? genie.cwd : undefined; - if (command !== 'node' || !Array.isArray(args) || args[0] !== './scripts/mcp-launcher.cjs' || cwd !== '.') { - fail(`installed plugin MCP entry shape is wrong: ${JSON.stringify(genie)}`); + // Group A removed the Codex plugin MCP route: the installed manifest declares no + // mcpServers and no MCP capability, and ships no `.mcp.json`. Codex MCP now comes + // ONLY from the marker-owned project route reconciled by `genie init` (asserted + // separately as the project `.mcp.json`/`.codex/config.toml`). + const manifest: unknown = JSON.parse(readFileSync(join(root, '.codex-plugin', 'plugin.json'), 'utf8')); + const manifestRecord = isRecord(manifest) ? manifest : {}; + if ('mcpServers' in manifestRecord) { + fail('installed Codex plugin manifest must not declare mcpServers (plugin MCP route removed)'); + } + const iface = isRecord(manifestRecord.interface) ? manifestRecord.interface : {}; + if (Array.isArray(iface.capabilities) && iface.capabilities.includes('MCP')) { + fail('installed Codex plugin must not advertise the MCP capability (plugin MCP route removed)'); } - if (!existsSync(join(root, 'scripts', 'mcp-launcher.cjs'))) fail('installed plugin MCP launcher is missing'); + if (existsSync(join(root, '.mcp.json'))) fail('installed Codex plugin must not ship a .mcp.json route file'); + // The plugin-local launcher stays — Claude drives it via its own inline manifest entry. + if (!existsSync(join(root, 'scripts', 'mcp-launcher.cjs'))) fail('plugin MCP launcher (Claude) is missing'); } function assertSessionStartHook(iso: IsolatedHome, version: string): void { @@ -241,9 +259,8 @@ function stepFreshInstall(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); linkRealCodex(iso); - const install = runCli(iso, ['install', '--integrations', 'codex']); - if (install.exitCode !== 0) - fail(`fresh install --integrations codex failed: ${install.stderr.trim() || install.stdout.trim()}`); + publishDelivery(iso, '(fresh)'); + setupCodex(iso, '(fresh)'); assertPluginHealthy(iso, TARGET_VERSION); const retirement = inspectRetirement(iso.skillsDir); if (retirement.txnIds.length !== 0) @@ -266,36 +283,35 @@ function stepUpgradePure23(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); linkRealCodex(iso); - if (runCli(iso, ['install', '--integrations', 'codex']).exitCode !== 0) fail('pure-23 home install codex failed'); const seeded = seedShippedFallbackLayout(iso); if (seeded.length !== 23) fail(`expected 23 seeded fallbacks, got ${seeded.length}`); // Round-trip gate (A4): the built CLI must classify all 23 as clean. const beforeDetail = codexTierDetail(iso, '(pre-upgrade)'); if (!beforeDetail.includes('23 clean managed fallback')) fail(`round-trip gate failed; doctor said: ${beforeDetail}`); - // Upgrade convergence retires all 23 into one committed transaction. - const upgrade = convergeUpdate(iso); - if (upgrade.exitCode !== 0) fail(`upgrade convergence failed: ${upgrade.stderr.trim() || upgrade.stdout.trim()}`); + // Authenticated setup activation proves the exact enabled plugin before + // retiring all 23, then converges managed roles. + publishDelivery(iso, '(pure-23)'); + setupCodex(iso, '(pure-23 activation)'); const first = inspectRetirement(iso.skillsDir); assertCommittedTransaction(first, 23); assertFallbacksRetired(iso, seeded); assertPluginHealthy(iso, TARGET_VERSION); const txnId = first.txnIds[0]; - // Two further full updates: still exactly one transaction, same id (A5, idempotent). + // Two further setup runs: still exactly one transaction, same id (A5, idempotent). for (let run = 1; run <= 2; run++) { - const again = convergeUpdate(iso); - if (again.exitCode !== 0) fail(`idempotent update ${run} failed: ${again.stderr.trim() || again.stdout.trim()}`); + setupCodex(iso, `(pure-23 idempotent setup ${run})`); const summary = inspectRetirement(iso.skillsDir); assertCommittedTransaction(summary, 23); if (summary.txnIds[0] !== txnId) - fail(`idempotent update ${run} created a second transaction: ${summary.txnIds.join(', ')}`); + fail(`idempotent setup ${run} created a second transaction: ${summary.txnIds.join(', ')}`); assertFallbacksRetired(iso, seeded); } const afterDetail = codexTierDetail(iso, '(post-upgrade)'); if (!afterDetail.includes('no managed') || !afterDetail.includes('1 retired quarantine')) { fail(`post-upgrade doctor should report plugin-only + one quarantine txn, got: ${afterDetail}`); } - notes.push('pure-23: 23 retired → one committed txn, stable across install + 3 updates'); + notes.push('pure-23: 23 retired → one committed txn, stable across activation + 2 setup reruns'); }); } @@ -307,7 +323,6 @@ function stepMixedCollisions(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); linkRealCodex(iso); - if (runCli(iso, ['install', '--integrations', 'codex']).exitCode !== 0) fail('mixed home install codex failed'); // 22 clean fallbacks (the `wish` slot is left for the unmanaged same-name collision). const seeded = seedShippedFallbackLayout(iso, ['wish']); if (seeded.length !== 22) fail(`expected 22 clean fallbacks, got ${seeded.length}`); @@ -316,16 +331,16 @@ function stepMixedCollisions(notes: string[]): void { const claudeDir = seedClaudeSentinel(iso); const captured = captureProtected([...personal.protectedPaths, dangling, claudeDir]); - const upgrade = convergeUpdate(iso); - if (upgrade.exitCode !== 0) fail(`mixed upgrade failed: ${upgrade.stderr.trim() || upgrade.stdout.trim()}`); - assertProtectedUnchanged('mixed after first update', captured); + publishDelivery(iso, '(mixed)'); + setupCodex(iso, '(mixed activation)'); + assertProtectedUnchanged('mixed after first setup', captured); assertCommittedTransaction(inspectRetirement(iso.skillsDir), 22); assertFallbacksRetired(iso, seeded); if (!existsSync(join(iso.skillsDir, personal.names.unmanaged))) fail('personal same-name `wish` was removed'); - const checks = readDoctorChecks(iso); + const checks = readLifecycleDoctorChecks(iso); const codexDetail = req(findCheck(checks, 'agent sync: codex'), 'mixed missing codex check').detail; - if (!codexDetail.includes('no managed')) fail(`mixed post-update codex tier not clean: ${codexDetail}`); + if (!codexDetail.includes('no managed')) fail(`mixed post-setup codex tier not clean: ${codexDetail}`); const collisions = req(findCheck(checks, 'agent sync: codex collisions'), 'mixed missing codex collisions check'); if ( !collisions.detail.includes(personal.names.modifiedManaged) || @@ -350,12 +365,11 @@ function stepMixedCollisions(notes: string[]): void { assertRoleAgents(iso, ROLE_AGENT_COUNT); assertPluginHealthy(iso, TARGET_VERSION); - // Second full update: protected trees still identical, still one transaction. + // Second setup: protected trees still identical, still one transaction. const txnId = inspectRetirement(iso.skillsDir).txnIds[0]; - const again = convergeUpdate(iso); - if (again.exitCode !== 0) fail(`mixed second update failed: ${again.stderr.trim() || again.stdout.trim()}`); - assertProtectedUnchanged('mixed after second update', captured); - if (inspectRetirement(iso.skillsDir).txnIds[0] !== txnId) fail('mixed second update created a second transaction'); + setupCodex(iso, '(mixed idempotent setup)'); + assertProtectedUnchanged('mixed after second setup', captured); + if (inspectRetirement(iso.skillsDir).txnIds[0] !== txnId) fail('mixed second setup created a second transaction'); notes.push('mixed: 22 retired, 4 personal classes + dangling symlink + Claude sentinel preserved, 7 role agents'); }); } @@ -368,13 +382,13 @@ function stepRoleAgentDisabled(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); linkRealCodex(iso); - if (runCli(iso, ['install', '--integrations', 'codex']).exitCode !== 0) fail('disabled home install codex failed'); + publishDelivery(iso, '(disabled bootstrap)'); + setupCodex(iso, '(disabled bootstrap)'); disablePluginInConfig(iso); if (readCodexGeniePlugin(iso).enabled !== false) fail('plugin did not read as disabled after the config edit'); rmSync(join(iso.codexHome, 'agents'), { recursive: true, force: true }); const seeded = seedShippedFallbackLayout(iso); - const upd = convergeUpdate(iso); - if (upd.exitCode !== 0) fail(`disabled-plugin update failed: ${upd.stderr.trim() || upd.stdout.trim()}`); + setupCodex(iso, '(disabled preservation)'); if (readCodexGeniePlugin(iso).enabled !== false) fail('disabled plugin was silently re-enabled'); assertRoleAgents(iso, ROLE_AGENT_COUNT); if (inspectRetirement(iso.skillsDir).txnIds.length !== 0) fail('disabled path created a retirement transaction'); @@ -393,17 +407,17 @@ function stepRoleAgentHealthFailure(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); linkRealCodex(iso); - if (runCli(iso, ['install', '--integrations', 'codex']).exitCode !== 0) - fail('health-failure home install codex failed'); + publishDelivery(iso, '(health-failure bootstrap)'); + setupCodex(iso, '(health-failure bootstrap)'); const seeded = seedShippedFallbackLayout(iso); const agentsBefore = snapshotNode(join(iso.codexHome, 'agents')); const fallbackBefore = snapshotTree(iso.skillsDir); corruptInstalledPayload(iso); - const upd = convergeUpdate(iso); - if (upd.exitCode === 0) fail('health-failure path unexpectedly succeeded (corrupt payload accepted)'); + const setup = runLifecycleSetup(iso); + if (setup.exitCode === 0) fail('health-failure path unexpectedly succeeded (corrupt payload accepted)'); // Pin attribution: the nonzero exit must come from payload/health verification of the // corrupted plugin, not some unrelated convergence failure. - const failText = `${upd.stderr}\n${upd.stdout}`; + const failText = setup.output; if (!/payload/i.test(failText) || !/(mismatch|differ|canonical|health)/i.test(failText)) fail(`health-failure exit not attributed to payload/health verification: ${failText.trim().slice(0, 300)}`); const agentsDiff = diffTree(agentsBefore, snapshotNode(join(iso.codexHome, 'agents'))); @@ -425,16 +439,15 @@ function stepPluginIncapable(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); writeFakeCodex(iso, 'plugin-unknown'); + publishDelivery(iso, '(plugin-incapable)'); const seeded = seedShippedFallbackLayout(iso); - // Protected trees: the fallback tier (must never be read/mutated) and the - // GENIE_HOME plugin payload. `install` DOES persist `.integration-consent.json` - // before integrations run, so full GENIE_HOME identity is intentionally not - // claimed — A8 scopes byte-identity to the protected trees. + // A8's protected scope is the fallback tier (which must not be read or + // mutated) plus the installed plugin payload. const skillsBefore = snapshotTree(iso.skillsDir); const payloadBefore = snapshotTree(join(iso.genieHome, 'plugins')); - const install = runCli(iso, ['install', '--integrations', 'codex']); - if (install.exitCode === 0) fail('plugin-incapable install unexpectedly succeeded'); - const output = `${install.stdout}\n${install.stderr}`; + const setup = runLifecycleSetup(iso); + if (setup.exitCode === 0) fail('plugin-incapable setup unexpectedly succeeded'); + const output = setup.output; if (!/upgrade.*codex/i.test(output)) fail(`plugin-incapable output lacks explicit upgrade-Codex guidance: ${output.slice(0, 400)}`); // Convergence aborts on the first codex command, before any fallback read (A8). @@ -457,6 +470,7 @@ function stepForcedPluginFailure(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); writeFakeCodex(iso, 'plugin-fails-add'); + publishDelivery(iso, '(forced plugin-add failure)'); const seeded = seedShippedFallbackLayout(iso, ['wish']); seedPersonalFixtures(iso); // A8: scope byte-identity to the PROTECTED trees (fallback + personal tier, @@ -464,8 +478,8 @@ function stepForcedPluginFailure(notes: string[]): void { // touch codexHome/genieHome metadata, so those are not claimed byte-identical. const skillsBefore = snapshotTree(iso.skillsDir); const payloadBefore = snapshotTree(join(iso.genieHome, 'plugins')); - const install = runCli(iso, ['install', '--integrations', 'codex']); - if (install.exitCode === 0) fail('forced plugin-add failure install unexpectedly succeeded'); + const setup = runLifecycleSetup(iso); + if (setup.exitCode === 0) fail('forced plugin-add failure setup unexpectedly succeeded'); const skillsDiff = diffTree(skillsBefore, snapshotTree(iso.skillsDir)); if (skillsDiff.length > 0) fail(`fallback/personal tree changed on forced plugin-add failure: ${skillsDiff.join(' | ')}`); @@ -546,18 +560,44 @@ function stepEnvDependentSuites(notes: string[]): void { withIsolatedHome((iso) => { installGenieHome(iso); linkRealCodex(iso); - if (runCli(iso, ['install', '--integrations', 'codex']).exitCode !== 0) fail('C8 home install codex failed'); - // C8 OR-path: the 2 env-dependent unit suites (codex-project-mcp.test.ts, - // codex-manifest.test.ts) build their own fixtures, so running them under - // iso.env changes nothing — they stay pre-existing red regardless (documented - // in CHANGELOG's "Known non-blocking red gate"). We therefore prove the SAME - // criteria black-box against the real installed plugin, which IS authoritative. - // 2a. Project-MCP reconciliation via `genie init` in the isolated project repo. + // C8 black-box proofs exercise the built artifact and real installed plugin + // inside the isolated home. First prove route-only init creates the route + // from actual absence, before delivery/setup has any opportunity to + // reconcile it as part of activation finalization. + const codexConfig = join(iso.project, '.codex', 'config.toml'); + if (existsSync(codexConfig)) fail('C8 project route must be absent before the first genie init'); const init = runCli(iso, ['init', '--json']); if (init.exitCode !== 0) fail(`genie init failed: ${init.stderr.trim() || init.stdout.trim()}`); - const projectMcp = join(iso.project, '.mcp.json'); - if (!existsSync(projectMcp) || !readFileSync(projectMcp, 'utf8').includes('genie')) { - fail('genie init did not reconcile a project MCP route referencing genie'); + if (!existsSync(codexConfig)) fail('genie init did not create the Codex project config'); + const firstToml = readFileSync(codexConfig, 'utf8'); + assertSingleCodexProjectRouteMarker(firstToml); + publishDelivery(iso, '(C8)'); + setupCodex(iso, '(C8)'); + const afterSetupToml = readFileSync(codexConfig, 'utf8'); + if (afterSetupToml !== firstToml) { + fail('authenticated setup changed the already-current marker-owned route bytes'); + } + trustIsolatedCodexProject(iso); + const firstRoute = assertEffectiveCodexProjectRoute( + runRealCodex(iso, ['mcp', 'get', 'genie', '--json']), + runRealCodex(iso, ['mcp', 'list', '--json']), + iso.genieBin, + ); + + const secondInit = runCli(iso, ['init', '--json']); + if (secondInit.exitCode !== 0) { + fail(`second genie init failed: ${secondInit.stderr.trim() || secondInit.stdout.trim()}`); + } + const secondToml = readFileSync(codexConfig, 'utf8'); + if (secondToml !== firstToml) fail('second genie init changed the marker-owned Codex project config bytes'); + assertSingleCodexProjectRouteMarker(secondToml); + const secondRoute = assertEffectiveCodexProjectRoute( + runRealCodex(iso, ['mcp', 'get', 'genie', '--json']), + runRealCodex(iso, ['mcp', 'list', '--json']), + iso.genieBin, + ); + if (secondRoute.getJson !== firstRoute.getJson || secondRoute.listJson !== firstRoute.listJson) { + fail('Codex mcp get/list JSON changed after byte-idempotent second init'); } // 2b. Manifest MCP shape via direct inspection of the installed plugin cache. assertInstalledPluginMcpShape(iso, readCodexGeniePlugin(iso).version); @@ -566,9 +606,7 @@ function stepEnvDependentSuites(notes: string[]): void { assertPluginHealthy(iso, TARGET_VERSION); // 2d. SessionStart hook context emission (covers the codex-manifest criteria). assertSessionStartHook(iso, readCodexGeniePlugin(iso).version); - notes.push( - 'C8: black-box project-MCP + manifest-shape + MCP-usability + SessionStart authoritative (env-dependent unit suites are pre-existing red regardless of iso.env — see CHANGELOG)', - ); + notes.push('C8: black-box project-MCP + manifest-shape + MCP-usability + SessionStart proofs passed'); }); } diff --git a/scripts/codex-smoke-harness.test.ts b/scripts/codex-smoke-harness.test.ts new file mode 100644 index 000000000..e66fe2c79 --- /dev/null +++ b/scripts/codex-smoke-harness.test.ts @@ -0,0 +1,148 @@ +import { describe, expect, test } from 'bun:test'; +import { + type CliResult, + assertEffectiveCodexProjectRoute, + assertSingleCodexProjectRouteMarker, + withIsolatedHome, +} from './codex-smoke-harness.ts'; + +const expectedCommand = '/isolated/.genie/bin/genie'; + +function result(value: unknown, exitCode = 0): CliResult { + return { + exitCode, + stdout: typeof value === 'string' ? value : JSON.stringify(value), + stderr: exitCode === 0 ? '' : 'forced failure', + }; +} + +function route(overrides: Record = {}): Record { + return { + name: 'genie', + enabled: true, + disabled_reason: null, + transport: { + type: 'stdio', + command: expectedCommand, + args: ['mcp'], + cwd: null, + }, + ...overrides, + }; +} + +describe('assertEffectiveCodexProjectRoute', () => { + test('accepts one enabled stdio route and canonicalizes get/list JSON', () => { + const get = route(); + const list = [route()]; + const snapshot = assertEffectiveCodexProjectRoute(result(get), result(list), expectedCommand); + expect(snapshot.route.transport).toEqual({ + type: 'stdio', + command: expectedCommand, + args: ['mcp'], + cwd: null, + }); + expect(snapshot.getJson).toContain(expectedCommand); + expect(snapshot.listJson).toContain(expectedCommand); + }); + + test('accepts an absent cwd and list-only host metadata', () => { + const transport = { type: 'stdio', command: expectedCommand, args: ['mcp'] }; + expect(() => + assertEffectiveCodexProjectRoute( + result(route({ transport })), + result([route({ transport, auth_status: 'unsupported' })]), + expectedCommand, + ), + ).not.toThrow(); + }); + + test('rejects a nonzero Codex command before parsing output', () => { + expect(() => assertEffectiveCodexProjectRoute(result('', 7), result([route()]), expectedCommand)).toThrow(/exit 7/); + }); + + test('rejects malformed JSON', () => { + expect(() => assertEffectiveCodexProjectRoute(result('{broken'), result([route()]), expectedCommand)).toThrow( + /malformed JSON/, + ); + }); + + test.each([ + ['wrong command', { type: 'stdio', command: '/other/genie', args: ['mcp'], cwd: null }, /command is/], + [ + 'cache-root command', + { type: 'stdio', command: '/x/.codex/plugins/cache/genie', args: ['mcp'], cwd: null }, + /plugin cache/, + ], + [ + 'wrong args', + { type: 'stdio', command: expectedCommand, args: ['mcp', '--extra'], cwd: null }, + /exactly \["mcp"\]/, + ], + ['effective cwd', { type: 'stdio', command: expectedCommand, args: ['mcp'], cwd: '/tmp/cache' }, /absent or null/], + ])('rejects %s', (_label, transport, message) => { + expect(() => + assertEffectiveCodexProjectRoute(result(route({ transport })), result([route({ transport })]), expectedCommand), + ).toThrow(message); + }); + + test('rejects duplicate genie routes', () => { + expect(() => + assertEffectiveCodexProjectRoute(result(route()), result([route(), route()]), expectedCommand), + ).toThrow(/exactly one genie route, found 2/); + }); + + test.each([ + ['disabled', { enabled: false }, /must be enabled/], + [ + 'non-stdio', + { transport: { type: 'http', command: expectedCommand, args: ['mcp'], cwd: null } }, + /type must be "stdio"/, + ], + ])('rejects a %s genie route', (_label, overrides, message) => { + expect(() => + assertEffectiveCodexProjectRoute(result(route(overrides)), result([route(overrides)]), expectedCommand), + ).toThrow(message); + }); +}); + +describe('assertSingleCodexProjectRouteMarker', () => { + const block = '# BEGIN GENIE MCP FALLBACK\nmcp_servers.genie.command = "/g"\n# END GENIE MCP FALLBACK\n'; + + test('accepts one intact marker pair', () => { + expect(() => assertSingleCodexProjectRouteMarker(block)).not.toThrow(); + }); + + test.each([ + ['missing end', '# BEGIN GENIE MCP FALLBACK\n'], + ['end before begin', '# END GENIE MCP FALLBACK\n# BEGIN GENIE MCP FALLBACK\n'], + ['duplicate pair', `${block}${block}`], + ])('rejects %s', (_label, toml) => { + expect(() => assertSingleCodexProjectRouteMarker(toml)).toThrow(/exactly one intact/); + }); +}); + +describe('isolated smoke environment', () => { + test('rebases every writable config, cache, and temporary root under the fixture home', () => { + withIsolatedHome((iso) => { + for (const key of [ + 'GENIE_HOME', + 'CODEX_HOME', + 'CLAUDE_CONFIG_DIR', + 'GENIE_AGENTS_SKILLS_DIR', + 'TMPDIR', + 'XDG_CONFIG_HOME', + 'XDG_CACHE_HOME', + 'XDG_DATA_HOME', + 'XDG_STATE_HOME', + 'BUN_INSTALL_CACHE_DIR', + 'NPM_CONFIG_CACHE', + 'GIT_CONFIG_GLOBAL', + ]) { + expect(iso.env[key]?.startsWith(`${iso.home}/`), key).toBe(true); + } + expect(iso.env.HOME).toBe(iso.home); + expect(iso.env.GIT_CONFIG_NOSYSTEM).toBe('1'); + }); + }); +}); diff --git a/scripts/codex-smoke-harness.ts b/scripts/codex-smoke-harness.ts index 7cad344a2..9da4804d1 100644 --- a/scripts/codex-smoke-harness.ts +++ b/scripts/codex-smoke-harness.ts @@ -6,18 +6,23 @@ * * Design contract (wish `repair-genie-codex-hooks-and-dedupe-skills`, Group C): * - * - The BUILT CLI is the system under test. Every lifecycle assertion runs the - * installed binary at `$GENIE_HOME/bin/genie` and inspects the real bytes it - * wrote, exit codes, and parsed stdout/doctor JSON. No runtime-integration - * oracle (`proveCodexPluginHealth`, `runBoundedCodexMcpSession`, - * `inspectCodexFallbackTier`, `inspectManagedSkillTree`) is imported — those - * would re-run the code under test against the harness's OWN `process.env` + * - The BUILT CLI performs the isolated non-Codex bootstrap install and remains + * the system under test for init/MCP surfaces. Fixture delivery publication, + * activation, and delivery-aware doctor assertions use one unshipped lifecycle + * driver because the deterministic Sigstore bundle is intentionally rejected + * without its same-process crypto seam. Setup still runs through a real PTY + * and the production consent/authorization/executor/store path. The black-box + * inspector imports no runtime-integration oracle (`proveCodexPluginHealth`, + * `runBoundedCodexMcpSession`, `inspectCodexFallbackTier`, + * `inspectManagedSkillTree`) — those would re-run the code under test against + * the harness's OWN `process.env` * (the developer's real `~/.codex`/`~/.agents`). MCP health is proven * black-box by driving `/scripts/mcp-launcher.cjs` over a * hand-rolled JSON-RPC session. - * - The ONLY `src/` imports are deterministic FIXTURE BUILDERS with explicit - * path arguments and no env resolution: `computeDirDigest` (marker digest) - * and `materializeFrozenCodexFallbackRelease` (the frozen 5.260712.1 release). + * - This harness module's ONLY direct `src/` imports are deterministic FIXTURE + * BUILDERS with explicit path arguments and no env resolution: + * `computeDirDigest` (marker digest) and + * `materializeFrozenCodexFallbackRelease` (the frozen 5.260712.1 release). * - Every isolated home lives under `os.homedir()` (a `/tmp` home makes real * codex 0.144.1 emit an alias warning). Each is registered and swept on * normal exit, failure, SIGINT, and uncaught errors, and the module refuses @@ -28,6 +33,7 @@ import { createHash } from 'node:crypto'; import { + appendFileSync, chmodSync, copyFileSync, cpSync, @@ -38,6 +44,7 @@ import { readFileSync, readdirSync, readlinkSync, + realpathSync, rmSync, symlinkSync, writeFileSync, @@ -69,6 +76,7 @@ export function repositoryRootFromModuleUrl(moduleUrl: string): string { export const REPO_ROOT = repositoryRootFromModuleUrl(import.meta.url); export const DIST_CLI = join(REPO_ROOT, 'dist', 'genie.js'); +const LIFECYCLE_TEST_RUNNER = join(REPO_ROOT, 'tests', 'support', 'codex-lifecycle-test-runner.ts'); /** The five read-only Genie MCP tools a healthy plugin launcher must expose. */ export const REQUIRED_GENIE_MCP_TOOLS = [ @@ -100,11 +108,11 @@ export const TARGET_VERSION = ((): string => { return version; })(); -export const REAL_CODEX = ((): string => { +function resolveRealCodex(): string { const which = Bun.which('codex'); if (which === null) fail('real codex CLI not found on PATH — Group C requires codex 0.144.1+'); return which; -})(); +} // ============================================================================ // Temp-home lifecycle (A12): registry + sweep + preflight guard @@ -190,7 +198,13 @@ function makeIsolatedHome(): IsolatedHome { const skillsDir = join(home, '.agents', 'skills'); const bin = join(home, 'bin'); const project = join(home, 'project'); + const tempDir = join(home, 'tmp'); + const xdgConfig = join(home, '.config'); + const xdgCache = join(home, '.cache'); + const xdgData = join(home, '.local', 'share'); + const xdgState = join(home, '.local', 'state'); const basePath = process.env.PATH ?? ''; + for (const path of [tempDir, xdgConfig, xdgCache, xdgData, xdgState]) mkdirSync(path, { recursive: true }); const env: Record = { ...(process.env as Record), HOME: home, @@ -203,6 +217,15 @@ function makeIsolatedHome(): IsolatedHome { // via `process.env.CLAUDE_CONFIG_DIR || join(homedir(), '.claude')`). CLAUDE_CONFIG_DIR: claudeConfigDir, GENIE_AGENTS_SKILLS_DIR: skillsDir, + TMPDIR: tempDir, + XDG_CONFIG_HOME: xdgConfig, + XDG_CACHE_HOME: xdgCache, + XDG_DATA_HOME: xdgData, + XDG_STATE_HOME: xdgState, + BUN_INSTALL_CACHE_DIR: join(xdgCache, 'bun'), + NPM_CONFIG_CACHE: join(xdgCache, 'npm'), + GIT_CONFIG_GLOBAL: join(home, '.gitconfig'), + GIT_CONFIG_NOSYSTEM: '1', PATH: `${bin}:${basePath}`, }; return { home, genieHome, codexHome, skillsDir, bin, genieBin: join(genieHome, 'bin', 'genie'), project, env }; @@ -225,8 +248,8 @@ export interface CliResult { stderr: string; } -function spawnCli(command: string[], iso: IsolatedHome): CliResult { - const result = Bun.spawnSync(command, { cwd: iso.project, env: iso.env, stdout: 'pipe', stderr: 'pipe' }); +function spawnCli(command: string[], iso: IsolatedHome, env = iso.env): CliResult { + const result = Bun.spawnSync(command, { cwd: iso.project, env, stdout: 'pipe', stderr: 'pipe' }); return { exitCode: result.exitCode ?? -1, stdout: result.stdout.toString(), @@ -239,11 +262,199 @@ export function runCli(iso: IsolatedHome, args: string[]): CliResult { return spawnCli([iso.genieBin, ...args], iso); } +function lifecycleEnv(iso: IsolatedHome): Record { + return { ...iso.env, CI: '', CODEX_THREAD_ID: '' }; +} + +/** Invoke the unshipped lifecycle driver with deterministic evidence verification. */ +export function runLifecycleCli(iso: IsolatedHome, args: string[]): CliResult { + return spawnCli([process.execPath, LIFECYCLE_TEST_RUNNER, ...args], iso, lifecycleEnv(iso)); +} + +export interface PtyCliResult { + exitCode: number; + output: string; +} + +/** Invoke setup through a real PTY so A's genuine consent guards and brand minter remain live. */ +export function runLifecycleSetup(iso: IsolatedHome): PtyCliResult { + const cli = [process.execPath, LIFECYCLE_TEST_RUNNER, 'setup', '--codex']; + const env = lifecycleEnv(iso); + if (process.platform === 'darwin') { + const expect = Bun.which('expect'); + if (expect === null) fail('real-PTY lifecycle smoke requires expect(1) on macOS'); + const spawnWords = cli.map((part) => `{${part}}`).join(' '); + const script = [ + 'set timeout 120', + `spawn -noecho ${spawnWords}`, + 'send -- "yes\\r"', + 'expect eof', + 'catch wait result', + 'exit [lindex $result 3]', + ].join('\n'); + const proc = Bun.spawnSync([expect, '-c', script], { + cwd: iso.project, + env, + stdout: 'pipe', + stderr: 'pipe', + timeout: 150_000, + }); + return { exitCode: proc.exitCode ?? -1, output: proc.stdout.toString() + proc.stderr.toString() }; + } + if (process.platform === 'linux') { + const script = Bun.which('script'); + if (script === null) fail('real-PTY lifecycle smoke requires script(1) on Linux'); + const command = cli.map((part) => `'${part.replaceAll("'", `'\\''`)}'`).join(' '); + const proc = Bun.spawnSync([script, '-qec', command, '/dev/null'], { + cwd: iso.project, + env, + stdin: Buffer.from('yes\n'), + stdout: 'pipe', + stderr: 'pipe', + timeout: 120_000, + }); + return { exitCode: proc.exitCode ?? -1, output: proc.stdout.toString() + proc.stderr.toString() }; + } + fail(`real-PTY lifecycle smoke is unsupported on ${process.platform}`); +} + /** Invoke the real codex on the isolated PATH. */ export function runCodex(iso: IsolatedHome, args: string[]): CliResult { return spawnCli([join(iso.bin, 'codex'), ...args], iso); } +/** + * Invoke the resolved real Codex executable directly. C8 uses this instead of + * relying on the isolated-PATH symlink, while preserving the isolated env and + * project cwd that determine Codex's effective project configuration. + */ +export function runRealCodex(iso: IsolatedHome, args: string[]): CliResult { + return spawnCli([resolveRealCodex(), ...args], iso); +} + +/** + * Explicitly trust only the isolated project fixture so real Codex will load + * its project-scoped `.codex/config.toml`. Cover logical and physical spellings + * because macOS temp paths can differ by a `/private` prefix. + */ +export function trustIsolatedCodexProject(iso: IsolatedHome): void { + const spellings = [...new Set([iso.project, realpathSync(iso.project)])]; + const trust = spellings + .map((spelling) => `[projects.${JSON.stringify(spelling)}]\ntrust_level = "trusted"\n`) + .join(''); + appendFileSync(join(iso.codexHome, 'config.toml'), `\n${trust}`); +} + +export interface EffectiveCodexMcpRoute { + name: 'genie'; + enabled: true; + transport: { + type: 'stdio'; + command: string; + args: ['mcp']; + cwd: null; + }; +} + +export interface EffectiveCodexMcpSnapshot { + route: EffectiveCodexMcpRoute; + getJson: string; + listJson: string; +} + +function parseCodexJson(result: CliResult, command: string): unknown { + if (result.exitCode !== 0) { + fail( + `${command} failed with exit ${result.exitCode}: ${result.stderr.trim() || result.stdout.trim() || ''}`, + ); + } + try { + return JSON.parse(result.stdout) as unknown; + } catch (error) { + fail(`${command} returned malformed JSON: ${error instanceof Error ? error.message : String(error)}`); + } +} + +function canonicalJson(value: unknown): string { + if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]`; + if (isRecord(value)) { + return `{${Object.keys(value) + .sort() + .map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`) + .join(',')}}`; + } + return JSON.stringify(value) ?? 'null'; +} + +function assertGenieTransport(value: Record, expectedCommand: string, source: string): void { + if (value.name !== 'genie') fail(`${source} route name must be "genie"`); + if (value.enabled !== true) fail(`${source} genie route must be enabled`); + const transport = asRecord(value.transport, `${source} genie transport is not an object`); + if (transport.type !== 'stdio') fail(`${source} genie transport type must be "stdio"`); + if (typeof transport.command !== 'string') fail(`${source} genie transport command must be a string`); + const normalizedCommand = transport.command.replaceAll('\\', '/'); + if (normalizedCommand.includes('/plugins/cache/') || normalizedCommand.includes('/.codex/plugins/')) { + fail(`${source} genie command must not resolve through a plugin cache: ${transport.command}`); + } + if (transport.command !== expectedCommand) { + fail( + `${source} genie command is ${JSON.stringify(transport.command)}, expected ${JSON.stringify(expectedCommand)}`, + ); + } + if (!Array.isArray(transport.args) || transport.args.length !== 1 || transport.args[0] !== 'mcp') { + fail(`${source} genie args must be exactly ["mcp"]`); + } + if ('cwd' in transport && transport.cwd !== null) { + fail(`${source} genie cwd must be absent or null`); + } +} + +/** + * Parse and structurally prove Codex's effective project MCP route from both + * official JSON surfaces. This is intentionally independent of Genie's TOML + * parser and rejects duplicate, disabled, non-stdio, cache-root, cwd-overridden, + * or get/list-divergent routes. + */ +export function assertEffectiveCodexProjectRoute( + getResult: CliResult, + listResult: CliResult, + expectedCommand: string, +): EffectiveCodexMcpSnapshot { + const getParsed = parseCodexJson(getResult, 'codex mcp get genie --json'); + const listParsed = parseCodexJson(listResult, 'codex mcp list --json'); + const getRoute = asRecord(getParsed, 'codex mcp get genie --json is not an object'); + if (!Array.isArray(listParsed)) fail('codex mcp list --json is not an array'); + const genieRoutes = listParsed + .map((entry) => asRecord(entry, 'codex mcp list --json entry is not an object')) + .filter((entry) => entry.name === 'genie'); + if (genieRoutes.length !== 1) { + fail(`codex mcp list --json must contain exactly one genie route, found ${genieRoutes.length}`); + } + const listRoute = genieRoutes[0]; + assertGenieTransport(getRoute, expectedCommand, 'codex mcp get'); + assertGenieTransport(listRoute, expectedCommand, 'codex mcp list'); + return { + route: { + name: 'genie', + enabled: true, + transport: { type: 'stdio', command: expectedCommand, args: ['mcp'], cwd: null }, + }, + getJson: canonicalJson(getParsed), + listJson: canonicalJson(listParsed), + }; +} + +/** Require one complete marker-owned route block, with no duplicate marker. */ +export function assertSingleCodexProjectRouteMarker(toml: string): void { + const begin = '# BEGIN GENIE MCP FALLBACK'; + const end = '# END GENIE MCP FALLBACK'; + const begins = toml.split(begin).length - 1; + const ends = toml.split(end).length - 1; + if (begins !== 1 || ends !== 1 || toml.indexOf(begin) >= toml.indexOf(end)) { + fail(`Codex project config must contain exactly one intact ${begin}/${end} block`); + } +} + // ============================================================================ // Fixture builders // ============================================================================ @@ -265,6 +476,11 @@ export function installGenieHome(iso: IsolatedHome): void { // package.json makes `$GENIE_HOME/bin/genie --version` report the target // version so `update --post-delivery-converge` matches the installed plugin. copyFileSync(join(REPO_ROOT, 'package.json'), join(iso.genieHome, 'package.json')); + // A real release tarball installs VERSION beside plugins/ at the canonical + // delivery root. The black-box fixture must reproduce that authenticated + // layout; package.json affects CLI version reporting but is not delivery + // evidence and cannot substitute for the canonical VERSION stamp. + writeFileSync(join(iso.genieHome, 'VERSION'), `${TARGET_VERSION}\n`); for (const dir of AUX_LAYOUT_DIRS) { cpSync(join(REPO_ROOT, dir), join(iso.genieHome, dir), { recursive: true, @@ -284,7 +500,7 @@ export function installGenieHome(iso: IsolatedHome): void { /** Symlink the real codex CLI onto the isolated PATH. */ export function linkRealCodex(iso: IsolatedHome): void { mkdirSync(iso.bin, { recursive: true }); - symlinkSync(REAL_CODEX, join(iso.bin, 'codex')); + symlinkSync(resolveRealCodex(), join(iso.bin, 'codex')); } const FALLBACK_MARKER_NAME = '.genie-sync.json'; @@ -716,7 +932,15 @@ export interface DoctorCheck { } export function readDoctorChecks(iso: IsolatedHome): DoctorCheck[] { - const result = runCli(iso, ['doctor', '--json']); + return parseDoctorChecks(runCli(iso, ['doctor', '--json'])); +} + +/** Delivery-aware doctor through the fixture's crypto-only verification seam. */ +export function readLifecycleDoctorChecks(iso: IsolatedHome): DoctorCheck[] { + return parseDoctorChecks(runLifecycleCli(iso, ['doctor', '--json'])); +} + +function parseDoctorChecks(result: CliResult): DoctorCheck[] { const parsed = asRecord(JSON.parse(result.stdout), 'doctor --json is not an object'); const checks = parsed.checks; if (!Array.isArray(checks)) fail('doctor --json has no checks array'); diff --git a/scripts/fresh-install-smoke.test.ts b/scripts/fresh-install-smoke.test.ts index e979bc79f..650dd10bb 100644 --- a/scripts/fresh-install-smoke.test.ts +++ b/scripts/fresh-install-smoke.test.ts @@ -291,7 +291,7 @@ describe('fresh-install-smoke', () => { } }); - test('rejects unsupported camelCase plugin MCP config before source/cache packaging passes', () => { + test('rejects any plugin .mcp.json route file (the Codex plugin MCP route was removed)', () => { const root = mkdtempSync(join(tmpdir(), 'genie-plugin-mcp-schema-fixture-')); try { const pluginRoot = join(root, 'plugin'); @@ -300,10 +300,12 @@ describe('fresh-install-smoke', () => { dereference: false, verbatimSymlinks: true, }); + // A resurrected plugin `.mcp.json` (in any shape) must fail the smoke: the + // Codex plugin no longer provides an MCP route, so shipping one is a defect. writeFileSync( join(pluginRoot, '.mcp.json'), JSON.stringify({ - mcpServers: { + mcp_servers: { genie: { command: 'node', args: ['./scripts/mcp-launcher.cjs'], cwd: '.' }, }, }), @@ -311,7 +313,7 @@ describe('fresh-install-smoke', () => { const result = runSmoke(['--skills-dir', join(REPO_ROOT, 'skills'), '--plugin-root', pluginRoot]); expect(result.code).not.toBe(0); - expect(result.stderr).toContain('must not use unsupported camelCase mcpServers'); + expect(result.stderr).toContain('must not ship a .mcp.json route file'); } finally { rmSync(root, { recursive: true, force: true }); } diff --git a/scripts/fresh-install-smoke.ts b/scripts/fresh-install-smoke.ts index 04c475d03..c9fdc9b11 100644 --- a/scripts/fresh-install-smoke.ts +++ b/scripts/fresh-install-smoke.ts @@ -440,39 +440,28 @@ function checkStarterPrompts(manifest: Record, names: string[]) } function checkPluginMcpLayout(pluginRoot: string, manifest: Record): void { - if (manifest.mcpServers !== './.mcp.json') { - fail('Codex plugin manifest mcpServers must point to ./.mcp.json'); - } - const configPath = join(pluginRoot, '.mcp.json'); - if (!existsSync(configPath)) fail(`Codex plugin MCP config missing: ${configPath}`); - const config = JSON.parse(readFileSync(configPath, 'utf8')) as Record; - if ('mcpServers' in config) fail('Codex plugin .mcp.json must not use unsupported camelCase mcpServers'); - const wrapped = config.mcp_servers; - const serverMap = - typeof wrapped === 'object' && wrapped !== null && !Array.isArray(wrapped) - ? (wrapped as Record) - : config; - const rawEntry = serverMap.genie; - const entry = - typeof rawEntry === 'object' && rawEntry !== null && !Array.isArray(rawEntry) - ? (rawEntry as { command?: unknown; args?: unknown; cwd?: unknown }) + // Group A removed the Codex plugin MCP route: the manifest declares no mcpServers + // and no MCP capability, and the payload ships no `.mcp.json`. Codex MCP is + // provided ONLY by the marker-owned project route reconciled by `genie init`. + if ('mcpServers' in manifest) fail('Codex plugin manifest must not declare mcpServers (plugin MCP route removed)'); + const iface = manifest.interface; + const capabilities = + typeof iface === 'object' && iface !== null && !Array.isArray(iface) + ? (iface as { capabilities?: unknown }).capabilities : undefined; - if ( - entry?.command !== 'node' || - !Array.isArray(entry.args) || - entry.args.length !== 1 || - entry.args[0] !== './scripts/mcp-launcher.cjs' || - entry.cwd !== '.' - ) { - fail('Codex plugin MCP entry must run node ./scripts/mcp-launcher.cjs with cwd "."'); + if (Array.isArray(capabilities) && capabilities.includes('MCP')) { + fail('Codex plugin must not advertise the MCP capability (plugin MCP route removed)'); } - const launcher = resolve(pluginRoot, entry.args[0]); + if (existsSync(join(pluginRoot, '.mcp.json'))) fail('Codex plugin must not ship a .mcp.json route file'); + // The plugin-local launcher persists — Claude drives it via its own inline + // ${CLAUDE_PLUGIN_ROOT}-anchored manifest entry (checked in checkClaudePluginMcpLayout). + const launcher = resolve(pluginRoot, 'scripts', 'mcp-launcher.cjs'); if (!isWithin(resolve(pluginRoot), launcher)) fail('Codex MCP launcher escapes the plugin root'); if (!existsSync(launcher) || !lstatSync(launcher).isFile() || lstatSync(launcher).isSymbolicLink()) { - fail(`Codex MCP launcher must be a physical plugin-local file: ${launcher}`); + fail(`plugin-local MCP launcher must be a physical file: ${launcher}`); } if (!isWithin(realpathSync(pluginRoot), realpathSync(launcher))) { - fail('Codex MCP launcher resolves outside the plugin root'); + fail('plugin-local MCP launcher resolves outside the plugin root'); } } diff --git a/scripts/install-swap.test.ts b/scripts/install-swap.test.ts index dbe1c733c..8daa5c4e1 100644 --- a/scripts/install-swap.test.ts +++ b/scripts/install-swap.test.ts @@ -326,6 +326,33 @@ describe('install.sh transactional binary promotion (F31a)', () => { // --------------------------------------------------------------------------- describe('install.sh download_and_verify refuses on failed provenance (F31a)', () => { + test('manifest binding requires the exact selected channel, including stable', () => { + const run = Bun.spawnSync( + [ + 'bash', + '-c', + [ + 'source "$1"', + 'manifest_channel_matches \'{"channel":"stable"}\' stable', + '! manifest_channel_matches \'{"channel":"dev"}\' stable', + '! manifest_channel_matches \'{"version":"5.260714.1"}\' stable', + ].join('\n'), + 'bash', + INSTALL_SH, + ], + { + env: { + ...process.env, + GENIE_INSTALL_SOURCE_ONLY: '1', + GENIE_HOME: join(mkroot(), '.genie'), + }, + stdout: 'pipe', + stderr: 'pipe', + }, + ); + expect(run.exitCode).toBe(0); + }); + test('gh attestation unavailable + cosign verify failure fails closed with exit 4', () => { const root = mkroot(); const stub = join(root, 'stub'); diff --git a/scripts/materialize-release-subjects.sh b/scripts/materialize-release-subjects.sh new file mode 100644 index 000000000..306a7ffe1 --- /dev/null +++ b/scripts/materialize-release-subjects.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +set -euo pipefail + +: "${VERSION:?VERSION is required}" +RELEASE_REPOSITORY="${RELEASE_REPOSITORY:-${GITHUB_REPOSITORY:-}}" +: "${RELEASE_REPOSITORY:?RELEASE_REPOSITORY or GITHUB_REPOSITORY is required}" +DIST_DIR="${DIST_DIR:-dist}" +[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || exit 2 +[[ "$RELEASE_REPOSITORY" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] || exit 2 +[[ -d "$DIST_DIR" && ! -L "$DIST_DIR" ]] || exit 3 + +PLATFORMS=(linux-x64-glibc linux-x64-musl linux-arm64 darwin-arm64) +BASE_ASSETS=() +for platform in "${PLATFORMS[@]}"; do + tarball="genie-${VERSION}-${platform}.tar.gz" + BASE_ASSETS+=("$tarball" "${tarball}.bundle" "${tarball}.intoto.jsonl") +done +for name in "${BASE_ASSETS[@]}"; do + [[ -f "$DIST_DIR/$name" && ! -L "$DIST_DIR/$name" && -s "$DIST_DIR/$name" ]] || { + echo "missing or unsafe same-run release subject input: ${name}" >&2 + exit 3 + } +done + +work_root="$(mktemp -d "${TMPDIR:-/tmp}/genie-release-subjects.XXXXXX")" +trap 'rm -rf "$work_root"' EXIT HUP INT TERM +effective="$work_root/effective" +mkdir "$effective" +for name in "${BASE_ASSETS[@]}"; do cp "$DIST_DIR/$name" "$effective/$name"; done + +remote_json="$work_root/remote.json" +remote_exists=true +if ! gh release view "v${VERSION}" --repo "$RELEASE_REPOSITORY" --json assets >"$remote_json" 2>"$work_root/view.err"; then + if grep -qiE 'release not found|HTTP[^0-9]*404|status[^0-9]*404' "$work_root/view.err"; then + remote_exists=false + else + cat "$work_root/view.err" >&2 + echo "could not determine whether v${VERSION} already has release subjects" >&2 + exit 3 + fi +fi + +if [[ "$remote_exists" == true ]]; then + jq -e ' + (.assets | type == "array") and + (all(.assets[]; .name | type == "string")) and + (([.assets[].name] | length) == ([.assets[].name] | unique | length)) + ' "$remote_json" >/dev/null || { + echo "existing release asset inventory is malformed or duplicated" >&2 + exit 3 + } + for name in "${BASE_ASSETS[@]}"; do + if jq -e --arg name "$name" 'any(.assets[]; .name == $name)' "$remote_json" >/dev/null; then + destination="$work_root/remote-${name//[^A-Za-z0-9]/_}" + mkdir "$destination" + gh release download "v${VERSION}" --repo "$RELEASE_REPOSITORY" --pattern "$name" --dir "$destination" + [[ -f "$destination/$name" && ! -L "$destination/$name" && -s "$destination/$name" ]] || { + echo "downloaded release subject must be a nonempty physical file: ${name}" >&2 + exit 3 + } + cp "$destination/$name" "$effective/$name" + fi + done +fi + +# The effective set may combine preserved remote bytes with missing same-run +# sidecars after an interrupted upload. Verify the composed triplets before +# any descriptor subject is built or endorsed. +for platform in "${PLATFORMS[@]}"; do + bash "$(dirname "$0")/verify-release.sh" --local "$effective/genie-${VERSION}-${platform}.tar.gz" +done +for name in "${BASE_ASSETS[@]}"; do cp "$effective/$name" "$DIST_DIR/$name"; done +echo "materialized verified effective release subjects for v${VERSION}" diff --git a/scripts/materialize-release-subjects.test.ts b/scripts/materialize-release-subjects.test.ts new file mode 100644 index 000000000..acb389fd1 --- /dev/null +++ b/scripts/materialize-release-subjects.test.ts @@ -0,0 +1,142 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { scanPhysicalTree } from '../src/lib/codex-activation.ts'; + +const MATERIALIZE = join(import.meta.dir, 'materialize-release-subjects.sh'); +const BUILD = join(import.meta.dir, 'build-delivery-evidence.ts'); +const VERSION = '5.260723.7'; +const PLATFORMS = ['linux-x64-glibc', 'linux-x64-musl', 'linux-arm64', 'darwin-arm64'] as const; +const roots: string[] = []; + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +function sha256(path: string): string { + return createHash('sha256').update(readFileSync(path)).digest('hex'); +} + +function createTarball(root: string, platform: string, marker: string): { tarball: string; payload: string } { + const payload = join(root, `${marker}-${platform}`); + mkdirSync(join(payload, 'plugins', 'genie'), { recursive: true }); + writeFileSync(join(payload, 'genie'), `${marker} binary ${platform}`); + chmodSync(join(payload, 'genie'), 0o755); + writeFileSync(join(payload, 'plugins', 'genie', 'plugin.txt'), `${marker} plugin ${platform}`); + const tarball = join(root, `genie-${VERSION}-${platform}.tar.gz`); + expect(Bun.spawnSync(['tar', '-czf', tarball, '-C', payload, '.']).exitCode).toBe(0); + writeFileSync(`${tarball}.bundle`, `${marker} bundle`); + writeFileSync(`${tarball}.intoto.jsonl`, `${marker} provenance`); + return { tarball, payload }; +} + +describe('promotion effective release subject selection', () => { + test('all promoted descriptors bind preserved remote tarballs and extracted members when current bytes differ', () => { + const root = mkdtempSync(join(tmpdir(), 'genie-release-subjects-')); + roots.push(root); + const dist = join(root, 'dist'); + const remote = join(root, 'remote'); + const bin = join(root, 'bin'); + mkdirSync(dist); + mkdirSync(remote); + mkdirSync(bin); + + const remotePayloads = new Map(); + for (const platform of PLATFORMS) { + createTarball(dist, platform, 'current'); + const built = createTarball(remote, platform, 'preserved'); + remotePayloads.set(platform, built.payload); + expect(sha256(join(dist, `genie-${VERSION}-${platform}.tar.gz`))).not.toBe(sha256(built.tarball)); + } + + const assets = PLATFORMS.flatMap((platform) => { + const name = `genie-${VERSION}-${platform}.tar.gz`; + return [name, `${name}.bundle`, `${name}.intoto.jsonl`].map((asset) => ({ name: asset })); + }); + writeFileSync( + join(bin, 'gh'), + `#!/usr/bin/env bun +import { copyFileSync } from 'node:fs'; +import { join } from 'node:path'; +const args = process.argv.slice(2); +if (args[0] === 'release' && args[1] === 'view') { + console.log(${JSON.stringify(JSON.stringify({ assets }))}); + process.exit(0); +} +if (args[0] === 'release' && args[1] === 'download') { + const name = args[args.indexOf('--pattern') + 1]; + const dir = args[args.indexOf('--dir') + 1]; + copyFileSync(join(process.env.REMOTE_DIR, name), join(dir, name)); + process.exit(0); +} +if (args[0] === 'attestation' && args[1] === 'verify' && args.includes('--help')) process.exit(1); +process.exit(2); +`, + ); + for (const tool of ['cosign', 'slsa-verifier']) { + writeFileSync(join(bin, tool), '#!/usr/bin/env bash\nexit 0\n'); + } + for (const tool of ['gh', 'cosign', 'slsa-verifier']) chmodSync(join(bin, tool), 0o755); + + const materialized = Bun.spawnSync(['bash', MATERIALIZE], { + cwd: root, + env: { + ...process.env, + PATH: `${bin}:${process.env.PATH ?? ''}`, + VERSION, + RELEASE_REPOSITORY: 'automagik-dev/genie', + DIST_DIR: dist, + REMOTE_DIR: remote, + }, + stdout: 'pipe', + stderr: 'pipe', + }); + expect(materialized.exitCode).toBe(0); + + const manifest = join(root, 'homolog.json'); + writeFileSync( + manifest, + `{"schema_version":1,"channel":"homolog","version":"${VERSION}","released_at":"2026-07-23T00:00:00Z","tarball_base":"https://github.com/automagik-dev/genie/releases/download/v${VERSION}","platforms":["linux-x64-glibc","linux-x64-musl","linux-arm64","darwin-arm64"]}\n`, + ); + for (const platform of PLATFORMS) { + const tarball = join(dist, `genie-${VERSION}-${platform}.tar.gz`); + const output = join(root, `${platform}.delivery.json`); + const built = Bun.spawnSync([ + 'bun', + BUILD, + '--tarball', + tarball, + '--manifest', + manifest, + '--output', + output, + '--repository', + 'automagik-dev/genie', + '--version', + VERSION, + '--channel', + 'homolog', + '--platform-id', + platform, + '--source-sha', + 'a'.repeat(40), + '--source-branch', + 'homolog', + '--source-ci-run-id', + '123', + '--control-sha', + 'b'.repeat(40), + ]); + expect(built.exitCode).toBe(0); + const descriptor = JSON.parse(readFileSync(output, 'utf8')); + const preserved = join(remote, `genie-${VERSION}-${platform}.tar.gz`); + expect(descriptor.artifactSha256).toBe(sha256(preserved)); + expect(descriptor.installedBinarySha256).toBe(sha256(join(remotePayloads.get(platform)!, 'genie'))); + expect(descriptor.canonicalPayloadSha256).toBe( + scanPhysicalTree(join(remotePayloads.get(platform)!, 'plugins', 'genie')).digest, + ); + } + }, 15_000); +}); diff --git a/scripts/reconcile-channel-manifests.sh b/scripts/reconcile-channel-manifests.sh index 6b61de8f4..15406a582 100644 --- a/scripts/reconcile-channel-manifests.sh +++ b/scripts/reconcile-channel-manifests.sh @@ -6,13 +6,23 @@ set -euo pipefail : "${CHANNEL:?CHANNEL is required}" RELEASE_REPOSITORY="${RELEASE_REPOSITORY:-${GITHUB_REPOSITORY:-}}" : "${RELEASE_REPOSITORY:?RELEASE_REPOSITORY or GITHUB_REPOSITORY is required}" -RELEASED_AT="${RELEASED_AT:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}" +CANDIDATE_MANIFEST_DIR="${CANDIDATE_MANIFEST_DIR:-}" VERSION_RE='^[0-9]+\.[0-9]+\.[0-9]+$' [[ "$VERSION" =~ $VERSION_RE ]] || { echo "invalid release manifest version: ${VERSION}" >&2 exit 2 } +# Versions encode YYMMDD. Midnight UTC is retry-stable, unlike workflow wall +# clock time, so descriptor endorsement and the later CAS commit can bind the +# exact same raw bytes. +version_date="${VERSION#*.}" +version_date="${version_date%%.*}" +[[ "$version_date" =~ ^[0-9]{6}$ ]] || { + echo "release manifest version has no deterministic date: ${VERSION}" >&2 + exit 2 +} +RELEASED_AT="${RELEASED_AT:-20${version_date:0:2}-${version_date:2:2}-${version_date:4:2}T00:00:00Z}" [[ "$RELEASE_REPOSITORY" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] || { echo "invalid release repository: ${RELEASE_REPOSITORY}" >&2 exit 2 @@ -53,6 +63,27 @@ case "$CHANNEL" in *) echo "unknown channel: ${CHANNEL} (valid: stable, homolog, dev)" >&2; exit 2 ;; esac +validate_candidate() { + local path="$1" manifest_channel="$2" + local expected_tarball_base="https://github.com/${RELEASE_REPOSITORY}/releases/download/v${VERSION}" + [[ -f "$path" && ! -L "$path" && -s "$path" ]] || return 1 + jq -e \ + --arg channel "$manifest_channel" \ + --arg version "$VERSION" \ + --arg released_at "$RELEASED_AT" \ + --arg tarball_base "$expected_tarball_base" \ + ' + type == "object" and + (keys == ["channel", "platforms", "released_at", "schema_version", "tarball_base", "version"]) and + .schema_version == 1 and + .channel == $channel and + .version == $version and + .released_at == $released_at and + .tarball_base == $tarball_base and + .platforms == ["linux-x64-glibc", "linux-x64-musl", "linux-arm64", "darwin-arm64"] + ' "$path" >/dev/null +} + if [[ -e .well-known || -L .well-known ]]; then [[ -d .well-known && ! -L .well-known ]] || { echo '.well-known must be a physical directory' >&2 @@ -108,8 +139,15 @@ for target in "${TARGETS[@]}"; do case "$comparison" in 1) ;; 0) + if [[ -n "$CANDIDATE_MANIFEST_DIR" ]]; then + candidate="${CANDIDATE_MANIFEST_DIR}/${file}" + if ! validate_candidate "$candidate" "$manifest_channel" || ! cmp -- "$path" "$candidate"; then + echo "equal-version manifest differs from exact endorsed candidate bytes: ${path}" >&2 + exit 3 + fi + fi advance=false - echo "::notice ::release-manifest.equal ${manifest_channel} already points to v${VERSION}; preserving timestamp" + echo "::notice ::release-manifest.equal ${manifest_channel} already points to exact v${VERSION} bytes" ;; -1) advance=false @@ -121,21 +159,31 @@ for target in "${TARGETS[@]}"; do if [[ "$advance" == true ]]; then temporary="$(mktemp ".well-known/.${file}.XXXXXX")" - if ! jq -n \ - --arg channel "$manifest_channel" \ - --arg version "$VERSION" \ - --arg released_at "$RELEASED_AT" \ - --arg tarball_base "https://github.com/${RELEASE_REPOSITORY}/releases/download/v${VERSION}" \ - '{ - schema_version: 1, - channel: $channel, - version: $version, - released_at: $released_at, - tarball_base: $tarball_base, - platforms: ["linux-x64-glibc", "linux-x64-musl", "linux-arm64", "darwin-arm64"] - }' >"$temporary"; then - rm -f "$temporary" - exit 3 + if [[ -n "$CANDIDATE_MANIFEST_DIR" ]]; then + candidate="${CANDIDATE_MANIFEST_DIR}/${file}" + if ! validate_candidate "$candidate" "$manifest_channel"; then + rm -f "$temporary" + echo "candidate manifest is missing, unsafe, or does not match exact endorsed bytes: ${candidate}" >&2 + exit 3 + fi + cp "$candidate" "$temporary" + else + if ! jq -n \ + --arg channel "$manifest_channel" \ + --arg version "$VERSION" \ + --arg released_at "$RELEASED_AT" \ + --arg tarball_base "https://github.com/${RELEASE_REPOSITORY}/releases/download/v${VERSION}" \ + '{ + schema_version: 1, + channel: $channel, + version: $version, + released_at: $released_at, + tarball_base: $tarball_base, + platforms: ["linux-x64-glibc", "linux-x64-musl", "linux-arm64", "darwin-arm64"] + }' >"$temporary"; then + rm -f "$temporary" + exit 3 + fi fi mv "$temporary" "$path" echo "advanced ${manifest_channel} manifest to v${VERSION}" diff --git a/scripts/reconcile-channel-manifests.test.ts b/scripts/reconcile-channel-manifests.test.ts index 67a40b8c3..8a6a45a2c 100644 --- a/scripts/reconcile-channel-manifests.test.ts +++ b/scripts/reconcile-channel-manifests.test.ts @@ -78,7 +78,7 @@ describe('channel manifest monotonic reconciliation', () => { const result = run(cwd, '5.260714.1', 'dev'); expect(result.exitCode).toBe(0); expect(readFileSync(dev, 'utf8')).toBe(before); - expect(result.stdout.toString()).toContain('preserving timestamp'); + expect(result.stdout.toString()).toContain('already points to exact'); }); test('a newer dev release advances the dev pointer', () => { @@ -93,6 +93,99 @@ describe('channel manifest monotonic reconciliation', () => { expect(updated.released_at).toBe('2026-07-14T12:00:00Z'); }); + test('default candidate bytes are retry-stable and derive released_at from the version date', () => { + const first = root(); + const second = root(); + const env = { + ...process.env, + VERSION: '5.260714.2', + CHANNEL: 'stable', + RELEASE_REPOSITORY: 'automagik-dev/genie', + }; + expect(Bun.spawnSync(['bash', SCRIPT], { cwd: first, env }).exitCode).toBe(0); + expect(Bun.spawnSync(['bash', SCRIPT], { cwd: second, env }).exitCode).toBe(0); + for (const name of ['latest.json', 'homolog.json', 'dev.json']) { + expect(readFileSync(join(first, '.well-known', name))).toEqual(readFileSync(join(second, '.well-known', name))); + } + expect(JSON.parse(readFileSync(join(first, '.well-known', 'latest.json'), 'utf8')).released_at).toBe( + '2026-07-14T00:00:00Z', + ); + }); + + test('CAS reconciliation copies the exact pre-endorsed candidate bytes', () => { + const candidateRoot = root(); + expect(run(candidateRoot, '5.260714.2', 'homolog').exitCode).toBe(0); + const target = root(); + writeFileSync(join(target, '.well-known', 'homolog.json'), manifest('homolog', '5.260714.1')); + const result = Bun.spawnSync(['bash', SCRIPT], { + cwd: target, + env: { + ...process.env, + VERSION: '5.260714.2', + CHANNEL: 'homolog', + RELEASE_REPOSITORY: 'automagik-dev/genie', + RELEASED_AT: '2026-07-14T12:00:00Z', + CANDIDATE_MANIFEST_DIR: join(candidateRoot, '.well-known'), + }, + stdout: 'pipe', + stderr: 'pipe', + }); + expect(result.exitCode).toBe(0); + expect(readFileSync(join(target, '.well-known', 'homolog.json'))).toEqual( + readFileSync(join(candidateRoot, '.well-known', 'homolog.json')), + ); + expect(readFileSync(join(target, '.well-known', 'dev.json'))).toEqual( + readFileSync(join(candidateRoot, '.well-known', 'dev.json')), + ); + }); + + test('invalid pre-endorsed candidate fails without advancing the pointer', () => { + const candidateRoot = root(); + writeFileSync(join(candidateRoot, '.well-known', 'dev.json'), manifest('stable', '5.260714.2')); + const target = root(); + const before = manifest('dev', '5.260714.1'); + writeFileSync(join(target, '.well-known', 'dev.json'), before); + const result = Bun.spawnSync(['bash', SCRIPT], { + cwd: target, + env: { + ...process.env, + VERSION: '5.260714.2', + CHANNEL: 'dev', + RELEASE_REPOSITORY: 'automagik-dev/genie', + RELEASED_AT: '2026-07-14T12:00:00Z', + CANDIDATE_MANIFEST_DIR: join(candidateRoot, '.well-known'), + }, + stdout: 'pipe', + stderr: 'pipe', + }); + expect(result.exitCode).toBe(3); + expect(readFileSync(join(target, '.well-known', 'dev.json'), 'utf8')).toBe(before); + }); + + test('equal-version CAS fails when existing bytes differ from the endorsed candidate', () => { + const candidateRoot = root(); + expect(run(candidateRoot, '5.260714.2', 'dev').exitCode).toBe(0); + const target = root(); + const divergent = manifest('dev', '5.260714.2', '2026-07-14T11:59:59Z'); + writeFileSync(join(target, '.well-known', 'dev.json'), divergent); + const result = Bun.spawnSync(['bash', SCRIPT], { + cwd: target, + env: { + ...process.env, + VERSION: '5.260714.2', + CHANNEL: 'dev', + RELEASE_REPOSITORY: 'automagik-dev/genie', + RELEASED_AT: '2026-07-14T12:00:00Z', + CANDIDATE_MANIFEST_DIR: join(candidateRoot, '.well-known'), + }, + stdout: 'pipe', + stderr: 'pipe', + }); + expect(result.exitCode).toBe(3); + expect(result.stderr.toString()).toContain('differs from exact endorsed candidate bytes'); + expect(readFileSync(join(target, '.well-known', 'dev.json'), 'utf8')).toBe(divergent); + }); + test('malformed current state and unsupported candidate versions fail closed', () => { const cwd = root(); writeFileSync(join(cwd, '.well-known', 'dev.json'), '{"channel":"dev","version":"newest"}\n'); diff --git a/scripts/reconcile-release-assets.sh b/scripts/reconcile-release-assets.sh index bcd9ac7ee..f8b36b433 100644 --- a/scripts/reconcile-release-assets.sh +++ b/scripts/reconcile-release-assets.sh @@ -3,9 +3,11 @@ set -euo pipefail : "${VERSION:?VERSION is required}" +: "${CHANNEL:?CHANNEL is required}" RELEASE_REPOSITORY="${RELEASE_REPOSITORY:-${GITHUB_REPOSITORY:-}}" : "${RELEASE_REPOSITORY:?RELEASE_REPOSITORY or GITHUB_REPOSITORY is required}" DIST_DIR="${DIST_DIR:-dist}" +: "${CANDIDATE_MANIFEST_DIR:?CANDIDATE_MANIFEST_DIR is required}" [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "invalid release asset version: ${VERSION}" >&2 @@ -22,10 +24,20 @@ DIST_DIR="${DIST_DIR:-dist}" TAG="v${VERSION}" PLATFORMS=(linux-x64-glibc linux-x64-musl linux-arm64 darwin-arm64) +case "$CHANNEL" in + stable) EVIDENCE_CHANNELS=(stable homolog dev) ;; + homolog) EVIDENCE_CHANNELS=(homolog dev) ;; + dev) EVIDENCE_CHANNELS=(dev) ;; + *) echo "invalid release asset channel: ${CHANNEL}" >&2; exit 2 ;; +esac EXPECTED=() for platform in "${PLATFORMS[@]}"; do tarball="genie-${VERSION}-${platform}.tar.gz" EXPECTED+=("$tarball" "${tarball}.bundle" "${tarball}.intoto.jsonl") + for evidence_channel in "${EVIDENCE_CHANNELS[@]}"; do + descriptor="${tarball}.${evidence_channel}.delivery.json" + EXPECTED+=("$descriptor" "${descriptor}.sigstore.json") + done done is_expected_name() { @@ -64,16 +76,95 @@ for name in "${EXPECTED[@]}"; do } done +validate_descriptor() { + local path="$1" platform="$2" evidence_channel="$3" + local tarball="genie-${VERSION}-${platform}.tar.gz" + local platform_triple artifact_path artifact_sha manifest_name manifest_path manifest_sha + case "$platform" in + linux-x64-glibc|linux-x64-musl) platform_triple=linux-x64 ;; + linux-arm64) platform_triple=linux-arm64 ;; + darwin-arm64) platform_triple=darwin-arm64 ;; + *) return 1 ;; + esac + artifact_path="$(dirname "$path")/${tarball}" + [[ -f "$artifact_path" && ! -L "$artifact_path" && -s "$artifact_path" ]] || return 1 + if command -v sha256sum >/dev/null 2>&1; then + artifact_sha="$(sha256sum "$artifact_path" | awk '{print $1}')" + else + artifact_sha="$(shasum -a 256 "$artifact_path" | awk '{print $1}')" + fi + case "$evidence_channel" in + stable) manifest_name=latest.json ;; + homolog) manifest_name=homolog.json ;; + dev) manifest_name=dev.json ;; + *) return 1 ;; + esac + manifest_path="${CANDIDATE_MANIFEST_DIR}/${manifest_name}" + [[ -f "$manifest_path" && ! -L "$manifest_path" && -s "$manifest_path" ]] || return 1 + if command -v sha256sum >/dev/null 2>&1; then + manifest_sha="$(sha256sum "$manifest_path" | awk '{print $1}')" + else + manifest_sha="$(shasum -a 256 "$manifest_path" | awk '{print $1}')" + fi + jq -e \ + --arg repository "$RELEASE_REPOSITORY" \ + --arg version "$VERSION" \ + --arg channel "$evidence_channel" \ + --arg platform_id "$platform" \ + --arg platform_triple "$platform_triple" \ + --arg release_name "$tarball" \ + --arg artifact_sha "$artifact_sha" \ + --arg manifest_sha "$manifest_sha" \ + ' + type == "object" and + (keys == [ + "artifactSha256", "canonicalPayloadSha256", "channel", "controlSha", "digestAlgorithm", + "installedBinarySha256", "platformId", "platformTriple", "releaseManifestSha256", "releaseName", + "releaseTag", "repository", "schemaVersion", "sourceBranch", "sourceCiRunId", "sourceSha", "version" + ]) and + .schemaVersion == 1 and + .repository == $repository and + .version == $version and + .channel == $channel and + .platformId == $platform_id and + .platformTriple == $platform_triple and + .releaseTag == ("v" + $version) and + .releaseName == $release_name and + .artifactSha256 == $artifact_sha and + .releaseManifestSha256 == $manifest_sha and + .digestAlgorithm == "genie-physical-tree-v1" and + (.sourceBranch == "main" or .sourceBranch == "homolog" or .sourceBranch == "dev") and + (if .channel == "stable" then .sourceBranch == "main" + elif .channel == "homolog" then (.sourceBranch == "main" or .sourceBranch == "homolog") + else true end) and + (.sourceCiRunId | type == "string" and test("^[0-9]+$")) and + ([.releaseManifestSha256, .artifactSha256, .installedBinarySha256, .canonicalPayloadSha256] + | all(type == "string" and test("^[0-9a-f]{64}$"))) and + ([.sourceSha, .controlSha] | all(type == "string" and test("^[0-9a-f]{40}$"))) + ' "$path" >/dev/null +} + +for platform in "${PLATFORMS[@]}"; do + for evidence_channel in "${EVIDENCE_CHANNELS[@]}"; do + descriptor="$DIST_DIR/genie-${VERSION}-${platform}.tar.gz.${evidence_channel}.delivery.json" + validate_descriptor "$descriptor" "$platform" "$evidence_channel" || { + echo "delivery descriptor schema or binding is invalid: ${descriptor##*/}" >&2 + exit 3 + } + done +done + WORK_ROOT="$(mktemp -d "${TMPDIR:-/tmp}/genie-release-assets.XXXXXX")" trap 'rm -rf "$WORK_ROOT"' EXIT HUP INT TERM REMOTE_JSON="$WORK_ROOT/remote.json" EXPECTED_JSON="$(printf '%s\n' "${EXPECTED[@]}" | jq -R . | jq -s .)" fetch_remote_inventory() { - gh release view "$TAG" --repo "$RELEASE_REPOSITORY" --json assets,isDraft >"$REMOTE_JSON" + gh release view "$TAG" --repo "$RELEASE_REPOSITORY" --json assets,isDraft,isPrerelease >"$REMOTE_JSON" jq -e --argjson expected "$EXPECTED_JSON" ' (.assets | type == "array") and (.isDraft | type == "boolean") and + (.isPrerelease | type == "boolean") and (all(.assets[]; (.name | type == "string"))) and (([.assets[].name] | length) == ([.assets[].name] | unique | length)) and (all(.assets[]; .name as $name | ($expected | index($name)) != null)) @@ -103,39 +194,13 @@ download_remote() { } } -fetch_remote_inventory - -if remote_is_complete; then - # Promotions rebuild functionally equivalent but not bit-for-bit identical - # tarballs. A complete published inventory is immutable input: download and - # validate it, but never replace it with the current run's bytes. - differs=false - complete="$WORK_ROOT/complete" - mkdir "$complete" - for name in "${EXPECTED[@]}"; do - gh release download "$TAG" --repo "$RELEASE_REPOSITORY" --pattern "$name" --dir "$complete" - [[ -f "$complete/$name" && ! -L "$complete/$name" && -s "$complete/$name" ]] || { - echo "downloaded release asset must be a nonempty physical regular file: ${name}" >&2 - exit 3 - } - if ! cmp -- "$DIST_DIR/$name" "$complete/$name"; then - differs=true - fi - done - - if [[ "$(jq -r '.isDraft' "$REMOTE_JSON")" == true ]]; then - [[ "$differs" == false ]] || { - echo "complete draft assets differ from this run; refusing to publish mixed or stale bytes" >&2 - exit 3 - } - echo "${TAG} draft already has this run's exact complete release inventory" - exit 0 - fi - +verify_inventory() { + local inventory="$1" platform tarball generic_result result remote_control_sha exact_result exact_control_sha + local evidence_channel descriptor bundle delivery_result command -v cosign >/dev/null || { echo 'cosign is required to verify published assets' >&2; exit 3; } command -v slsa-verifier >/dev/null || { echo 'slsa-verifier is required to verify published assets' >&2; exit 3; } for platform in "${PLATFORMS[@]}"; do - tarball="$complete/genie-${VERSION}-${platform}.tar.gz" + tarball="$inventory/genie-${VERSION}-${platform}.tar.gz" cosign verify-blob \ --bundle "${tarball}.bundle" \ --certificate-identity "https://github.com/${RELEASE_REPOSITORY}/.github/workflows/sign-attest.yml@refs/heads/main" \ @@ -155,8 +220,7 @@ if remote_is_complete; then --cert-identity "https://github.com/${RELEASE_REPOSITORY}/.github/workflows/sign-attest.yml@refs/heads/main" \ --source-ref refs/heads/main \ --format json >"$result" - native_helper="$(dirname "$0")/release-native-predicate.sh" - remote_control_sha="$(bash "$native_helper" reusable-control-sha "$result")" + remote_control_sha="$(bash "$(dirname "$0")/release-native-predicate.sh" reusable-control-sha "$result")" exact_result="$WORK_ROOT/native-exact-${platform}.json" gh attestation verify "$tarball" \ --repo "$RELEASE_REPOSITORY" \ @@ -166,12 +230,67 @@ if remote_is_complete; then --source-digest "$remote_control_sha" \ --signer-digest "$remote_control_sha" \ --format json >"$exact_result" - exact_control_sha="$(bash "$native_helper" reusable-control-sha "$exact_result")" + exact_control_sha="$(bash "$(dirname "$0")/release-native-predicate.sh" reusable-control-sha "$exact_result")" [[ "$exact_control_sha" == "$remote_control_sha" ]] || { echo "certificate-filtered attestation control digest mismatch for ${platform}" >&2 exit 3 } + for evidence_channel in "${EVIDENCE_CHANNELS[@]}"; do + descriptor="$inventory/genie-${VERSION}-${platform}.tar.gz.${evidence_channel}.delivery.json" + bundle="${descriptor}.sigstore.json" + validate_descriptor "$descriptor" "$platform" "$evidence_channel" || { + echo "published delivery descriptor schema or binding is invalid: ${descriptor##*/}" >&2 + exit 3 + } + delivery_result="$WORK_ROOT/delivery-${platform}-${evidence_channel}.json" + gh attestation verify "$descriptor" \ + --bundle "$bundle" \ + --repo "$RELEASE_REPOSITORY" \ + --predicate-type "https://github.com/${RELEASE_REPOSITORY}/delivery-evidence/v1" \ + --cert-identity "https://github.com/${RELEASE_REPOSITORY}/.github/workflows/release-publish.yml@refs/heads/main" \ + --source-ref refs/heads/main \ + --format json >"$delivery_result" + jq -e --slurpfile descriptor "$descriptor" \ + 'length == 1 and .[0].verificationResult.statement.predicate == $descriptor[0]' \ + "$delivery_result" >/dev/null || { + echo "delivery attestation predicate does not equal descriptor: ${descriptor##*/}" >&2 + exit 3 + } + done + done +} + +fetch_remote_inventory + +if remote_is_complete; then + # Promotions rebuild functionally equivalent but not bit-for-bit identical + # tarballs. A complete published inventory is immutable input: download and + # validate it, but never replace it with the current run's bytes. + differs=false + complete="$WORK_ROOT/complete" + mkdir "$complete" + for name in "${EXPECTED[@]}"; do + gh release download "$TAG" --repo "$RELEASE_REPOSITORY" --pattern "$name" --dir "$complete" + [[ -f "$complete/$name" && ! -L "$complete/$name" && -s "$complete/$name" ]] || { + echo "downloaded release asset must be a nonempty physical regular file: ${name}" >&2 + exit 3 + } + if ! cmp -- "$DIST_DIR/$name" "$complete/$name"; then + differs=true + fi done + + if [[ "$(jq -r '.isDraft' "$REMOTE_JSON")" == true ]]; then + verify_inventory "$complete" + if [[ "$differs" == true ]]; then + echo "::notice ::release-assets.draft_reused ${TAG} preserves its complete authenticated draft inventory" + else + echo "${TAG} draft already has this run's exact complete release inventory" + fi + exit 0 + fi + + verify_inventory "$complete" if [[ "$differs" == false ]]; then echo "${TAG} already has the exact complete release inventory" else @@ -180,27 +299,30 @@ if remote_is_complete; then exit 0 fi -# A partial draft may be resumed only when every already-uploaded byte matches -# this run. Any mismatch fails closed; `--clobber` is deliberately forbidden. -[[ "$(jq -r '.isDraft' "$REMOTE_JSON")" == true ]] || { - echo "refusing to mutate an incomplete published release: ${TAG}" >&2 +if [[ "$(jq -r '.isDraft' "$REMOTE_JSON")" == false ]]; then + echo "refusing to mutate an incomplete published immutable release: ${TAG}" >&2 exit 3 -} +fi + +# A partial draft may be resumed only when every already-uploaded byte matches +# an authenticated effective inventory. Nondeterministic Sigstore bundle bytes +# from the prior attempt are preserved; `--clobber` is deliberately forbidden. +effective="$WORK_ROOT/partial-effective" +mkdir "$effective" +for name in "${EXPECTED[@]}"; do cp "$DIST_DIR/$name" "$effective/$name"; done MISSING=() index=0 for name in "${EXPECTED[@]}"; do if remote_has "$name"; then destination="$WORK_ROOT/partial-${index}" download_remote "$name" "$destination" - cmp -- "$DIST_DIR/$name" "$destination/$name" || { - echo "existing partial release asset differs; refusing to replace it: ${name}" >&2 - exit 3 - } + cp "$destination/$name" "$effective/$name" else - MISSING+=("$DIST_DIR/$name") + MISSING+=("$effective/$name") fi index=$((index + 1)) done +verify_inventory "$effective" if [[ ${#MISSING[@]} -gt 0 ]]; then gh release upload "$TAG" --repo "$RELEASE_REPOSITORY" "${MISSING[@]}" @@ -216,10 +338,11 @@ index=0 for name in "${EXPECTED[@]}"; do destination="$WORK_ROOT/final-${index}" download_remote "$name" "$destination" - cmp -- "$DIST_DIR/$name" "$destination/$name" || { + cmp -- "$effective/$name" "$destination/$name" || { echo "remote release asset verification failed after upload: ${name}" >&2 exit 3 } index=$((index + 1)) done +verify_inventory "$effective" echo "verified exact ${#EXPECTED[@]}-asset inventory for ${TAG}" diff --git a/scripts/reconcile-release-assets.test.ts b/scripts/reconcile-release-assets.test.ts index 5285f96a7..8ac07564e 100644 --- a/scripts/reconcile-release-assets.test.ts +++ b/scripts/reconcile-release-assets.test.ts @@ -1,19 +1,36 @@ import { afterEach, describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; const SCRIPT = join(import.meta.dir, 'reconcile-release-assets.sh'); const VERSION = '5.260714.3'; +const CHANNEL = 'dev'; const PLATFORMS = ['linux-x64-glibc', 'linux-x64-musl', 'linux-arm64', 'darwin-arm64']; -const NAMES = PLATFORMS.flatMap((platform) => { - const tarball = `genie-${VERSION}-${platform}.tar.gz`; - return [tarball, `${tarball}.bundle`, `${tarball}.intoto.jsonl`]; -}); +function namesFor(channel: 'stable' | 'homolog' | 'dev'): string[] { + const channels = + channel === 'stable' ? ['stable', 'homolog', 'dev'] : channel === 'homolog' ? ['homolog', 'dev'] : ['dev']; + return PLATFORMS.flatMap((platform) => { + const tarball = `genie-${VERSION}-${platform}.tar.gz`; + return [ + tarball, + `${tarball}.bundle`, + `${tarball}.intoto.jsonl`, + ...channels.flatMap((evidenceChannel) => { + const descriptor = `${tarball}.${evidenceChannel}.delivery.json`; + return [descriptor, `${descriptor}.sigstore.json`]; + }), + ]; + }); +} +const NAMES = namesFor(CHANNEL); const roots: string[] = []; +const candidateBytes = (channel: string) => `candidate-manifest:${channel}\n`; interface FakeState { draft: boolean; + prerelease?: boolean; assets: Record; calls?: Array<{ tool: string; args: string[] }>; failOn?: string; @@ -28,16 +45,56 @@ afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); }); -function localAssets(prefix = 'local'): Record { - return Object.fromEntries(NAMES.map((name) => [name, `${prefix}:${name}`])); +function localAssets(prefix = 'local', channel: 'stable' | 'homolog' | 'dev' = CHANNEL): Record { + return Object.fromEntries( + namesFor(channel).map((name) => { + if (!name.endsWith('.delivery.json')) return [name, `${prefix}:${name}`]; + const platform = PLATFORMS.find((candidate) => name.includes(`-${candidate}.tar.gz`))!; + const platformTriple = platform.startsWith('linux-x64') ? 'linux-x64' : platform; + const evidenceChannel = name.match(/\.tar\.gz\.(stable|homolog|dev)\.delivery\.json$/)?.[1]; + return [ + name, + `${JSON.stringify({ + schemaVersion: 1, + repository: 'automagik-dev/genie', + version: VERSION, + channel: evidenceChannel, + platformId: platform, + platformTriple, + releaseTag: `v${VERSION}`, + releaseName: `genie-${VERSION}-${platform}.tar.gz`, + releaseManifestSha256: createHash('sha256').update(candidateBytes(evidenceChannel!)).digest('hex'), + artifactSha256: createHash('sha256').update(`${prefix}:genie-${VERSION}-${platform}.tar.gz`).digest('hex'), + installedBinarySha256: '3'.repeat(64), + canonicalPayloadSha256: '4'.repeat(64), + digestAlgorithm: 'genie-physical-tree-v1', + sourceSha: 'a'.repeat(40), + sourceBranch: channel === 'stable' ? 'main' : channel, + sourceCiRunId: '123', + controlSha: 'c'.repeat(40), + })}\n`, + ]; + }), + ); } -function run(state: FakeState, mutate?: (dist: string) => void) { +function run( + state: FakeState, + mutate?: (dist: string) => void, + channel: 'stable' | 'homolog' | 'dev' = CHANNEL, + localPrefix = 'local', +) { const root = mkdtempSync(join(tmpdir(), 'genie-release-assets-')); roots.push(root); const dist = join(root, 'dist'); + const candidates = join(root, 'candidates'); mkdirSync(dist); - for (const [name, contents] of Object.entries(localAssets())) writeFileSync(join(dist, name), contents); + mkdirSync(candidates); + writeFileSync(join(candidates, 'latest.json'), candidateBytes('stable')); + writeFileSync(join(candidates, 'homolog.json'), candidateBytes('homolog')); + writeFileSync(join(candidates, 'dev.json'), candidateBytes('dev')); + for (const [name, contents] of Object.entries(localAssets(localPrefix, channel))) + writeFileSync(join(dist, name), contents); mutate?.(dist); const statePath = join(root, 'state.json'); @@ -63,7 +120,7 @@ record('gh'); if (state.failOn && ('gh ' + args.join(' ')).includes(state.failOn)) { save(); process.exit(42); } if (args[0] === 'release' && args[1] === 'view') { const assets = state.remoteAssets ?? Object.keys(state.assets).map((name) => ({ name })); - console.log(JSON.stringify({ assets, isDraft: state.draft })); + console.log(JSON.stringify({ assets, isDraft: state.draft, isPrerelease: state.prerelease ?? !state.draft })); save(); process.exit(0); } if (args[0] === 'release' && args[1] === 'download') { @@ -85,6 +142,11 @@ if (args[0] === 'release' && args[1] === 'upload') { save(); process.exit(0); } if (args[0] === 'attestation' && args[1] === 'verify') { + if (args.includes('--bundle')) { + const descriptor = JSON.parse(readFileSync(args[2], 'utf8')); + console.log(JSON.stringify([{ verificationResult: { statement: { predicate: descriptor } } }])); + save(); process.exit(0); + } const digest = value('--source-digest') ?? value('--signer-digest'); if (digest && digest !== state.controlSha) { save(); process.exit(5); } const sourceSha = state.invalidNative ? 'not-a-sha' : 'a'.repeat(40); @@ -164,6 +226,8 @@ save(); PATH: `${root}:${process.env.PATH ?? ''}`, GH_FAKE_STATE: statePath, VERSION, + CHANNEL: channel, + CANDIDATE_MANIFEST_DIR: candidates, RELEASE_REPOSITORY: 'automagik-dev/genie', DIST_DIR: dist, }, @@ -180,7 +244,7 @@ function calls(state: FakeState, tool: string, command?: string): Array<{ tool: } describe('exact GitHub release asset reconciliation', () => { - test('uploads and byte-verifies all 12 assets into an empty draft without clobber', () => { + test('uploads and byte-verifies the exact dev fanout inventory into an empty draft without clobber', () => { const { result, state } = run({ draft: true, assets: {} }); expect(result.exitCode).toBe(0); expect(Object.keys(state.assets).sort()).toEqual([...NAMES].sort()); @@ -188,6 +252,26 @@ describe('exact GitHub release asset reconciliation', () => { expect(state.usedClobber).not.toBe(true); }); + test('expands exact descriptor inventory by selected-channel fanout', () => { + for (const channel of ['dev', 'homolog', 'stable'] as const) { + const { result, state } = run({ draft: true, assets: {} }, undefined, channel); + expect(result.exitCode).toBe(0); + expect(Object.keys(state.assets).sort()).toEqual(namesFor(channel).sort()); + } + expect(namesFor('dev')).toHaveLength(20); + expect(namesFor('homolog')).toHaveLength(28); + expect(namesFor('stable')).toHaveLength(36); + }, 15_000); + + test('never mutates a published prerelease; channel promotions require fresh immutable tags', () => { + const devAssets = localAssets('dev-release', 'dev'); + const homolog = run({ draft: false, prerelease: true, assets: devAssets }, undefined, 'homolog', 'dev-release'); + expect(homolog.result.exitCode).toBe(3); + expect(homolog.result.stderr.toString()).toContain('published immutable release'); + expect(homolog.state.assets).toEqual(devAssets); + expect(calls(homolog.state, 'gh', 'release upload')).toHaveLength(0); + }); + test('rejects missing and extra local inventory before any GitHub mutation', () => { const missing = run({ draft: true, assets: {} }, (dist) => rmSync(join(dist, NAMES[0]))); expect(missing.result.exitCode).toBe(3); @@ -218,28 +302,57 @@ describe('exact GitHub release asset reconciliation', () => { expect(calls(symlink.state, 'gh')).toHaveLength(0); }); - test('resumes a matching partial draft and refuses a mismatched partial draft', () => { + test('resumes authenticated partial drafts and rejects a cryptographically inconsistent mix', () => { const local = localAssets(); const matching = run({ draft: true, assets: Object.fromEntries(NAMES.slice(0, 2).map((name) => [name, local[name]])), }); expect(matching.result.exitCode).toBe(0); - expect(Object.keys(matching.state.assets)).toHaveLength(12); + expect(Object.keys(matching.state.assets)).toHaveLength(NAMES.length); const mismatch = run({ draft: true, assets: { [NAMES[0]]: 'different' } }); expect(mismatch.result.exitCode).toBe(3); - expect(mismatch.result.stderr.toString()).toContain('refusing to replace'); expect(calls(mismatch.state, 'gh', 'release upload')).toHaveLength(0); }); - test('a complete draft must match the current run byte-for-byte', () => { + test('a complete authenticated draft reuses prior nondeterministic bundle bytes', () => { const draft = run({ draft: true, assets: localAssets('older-run') }); - expect(draft.result.exitCode).toBe(3); - expect(draft.result.stderr.toString()).toContain('complete draft assets differ'); + expect(draft.result.exitCode).toBe(0); + expect(draft.result.stdout.toString()).toContain('preserves its complete authenticated draft inventory'); expect(calls(draft.state, 'gh', 'release upload')).toHaveLength(0); }); + test('a retry rejects authenticated old descriptors bound to different candidate manifest bytes', () => { + const stale = localAssets('older-run'); + for (const [name, value] of Object.entries(stale)) { + if (!name.endsWith('.delivery.json')) continue; + const descriptor = JSON.parse(value); + descriptor.releaseManifestSha256 = 'f'.repeat(64); + stale[name] = `${JSON.stringify(descriptor)}\n`; + } + const retry = run({ draft: true, assets: stale }); + expect(retry.result.exitCode).toBe(3); + expect(calls(retry.state, 'gh', 'release upload')).toHaveLength(0); + }); + + test('an interrupted draft preserves prior bundle bytes while uploading only missing assets', () => { + const current = localAssets(); + const priorBundle = NAMES.find((name) => name.endsWith('.sigstore.json'))!; + const partial = { + [NAMES[0]]: current[NAMES[0]], + [NAMES[1]]: current[NAMES[1]], + [NAMES[2]]: current[NAMES[2]], + [NAMES[3]]: current[NAMES[3]], + [priorBundle]: 'prior nondeterministic sigstore bundle', + }; + const resumed = run({ draft: true, assets: partial }); + expect(resumed.result.exitCode).toBe(0); + expect(resumed.state.assets[priorBundle]).toBe(partial[priorBundle]); + expect(Object.keys(resumed.state.assets).sort()).toEqual([...NAMES].sort()); + expect(calls(resumed.state, 'gh', 'release upload')).toHaveLength(1); + }); + test('reuses a complete published inventory only after pinned cryptographic verification', () => { const publishedAssets = localAssets('published'); const { result, state } = run({ draft: false, assets: publishedAssets }); @@ -248,17 +361,26 @@ describe('exact GitHub release asset reconciliation', () => { expect(calls(state, 'gh', 'release upload')).toHaveLength(0); expect(calls(state, 'cosign')).toHaveLength(4); expect(calls(state, 'slsa-verifier')).toHaveLength(4); - expect(calls(state, 'gh', 'attestation verify')).toHaveLength(8); - for (const secondPass of calls(state, 'gh', 'attestation verify').filter((_, index) => index % 2 === 1)) { + expect(calls(state, 'gh', 'attestation verify')).toHaveLength(12); + const tarballAttestations = calls(state, 'gh', 'attestation verify').filter( + (call) => !call.args.includes('--bundle'), + ); + for (const secondPass of tarballAttestations.filter((_, index) => index % 2 === 1)) { expect(secondPass.args).toContain('--source-digest'); expect(secondPass.args).toContain('--signer-digest'); } + for (const delivery of calls(state, 'gh', 'attestation verify').filter((call) => call.args.includes('--bundle'))) { + expect(delivery.args).toContain('https://github.com/automagik-dev/genie/delivery-evidence/v1'); + expect(delivery.args).toContain( + 'https://github.com/automagik-dev/genie/.github/workflows/release-publish.yml@refs/heads/main', + ); + } }); test('never repairs a partial published release or accepts remote extras', () => { - const partial = run({ draft: false, assets: { [NAMES[0]]: localAssets()[NAMES[0]] } }); + const partial = run({ draft: false, prerelease: false, assets: { [NAMES[0]]: localAssets()[NAMES[0]] } }); expect(partial.result.exitCode).toBe(3); - expect(partial.result.stderr.toString()).toContain('incomplete published release'); + expect(partial.result.stderr.toString()).toContain('incomplete published immutable release'); expect(calls(partial.state, 'gh', 'release upload')).toHaveLength(0); const extra = run({ draft: true, assets: { unexpected: 'x' } }); @@ -285,6 +407,9 @@ describe('exact GitHub release asset reconciliation', () => { const upload = run({ draft: true, assets: {}, failOn: 'gh release upload' }); expect(upload.result.exitCode).toBe(42); + const endorsement = run({ draft: true, assets: {}, failOn: 'gh attestation verify' }); + expect(endorsement.result.exitCode).toBe(42); + const verification = run({ draft: false, assets: localAssets('published'), failOn: 'cosign verify-blob' }); expect(verification.result.exitCode).toBe(42); expect(calls(verification.state, 'gh', 'release upload')).toHaveLength(0); diff --git a/scripts/release-docs.test.ts b/scripts/release-docs.test.ts index 19494bff6..056e01535 100644 --- a/scripts/release-docs.test.ts +++ b/scripts/release-docs.test.ts @@ -139,6 +139,158 @@ describe('Group E release and documentation contracts', () => { expect(signing).not.toContain('--signer-workflow'); }); + test('delivery evidence becomes publicly immutable before exact manifest CAS', () => { + const release = read('.github/workflows/release.yml'); + const publish = read('.github/workflows/release-publish.yml'); + for (const input of ['source_sha', 'source_branch', 'source_ci_run_id']) { + expect(release).toContain(`${input}: \${{ inputs.${input} }}`); + expect(publish).toContain(`${input}:`); + expect(publish).toContain(`\${{ inputs.${input} }}`); + } + expect(publish).toContain('bun scripts/build-delivery-evidence.ts'); + expect(publish).toContain('bash scripts/materialize-release-subjects.sh'); + expect(publish).toContain('bun-version: 1.3.11'); + expect(publish).toContain('predicate-type: https://github.com/${{ github.repository }}/delivery-evidence/v1'); + expect(publish).toContain('actions/attest@67422f5511b7ff725f4dbd6fb9bd2cd925c65a8d'); + expect(publish).toContain('${DESCRIPTOR}.sigstore.json'); + expect(publish).toContain('EVIDENCE_CHANNELS=(stable homolog dev)'); + expect(publish).toContain('EVIDENCE_CHANNELS=(homolog dev)'); + expect(publish).toContain('EVIDENCE_CHANNELS=(dev)'); + expect(publish).toContain('name: delivery-candidate-manifests'); + expect(publish).toContain('CANDIDATE_MANIFEST_DIR="$CANDIDATE_MANIFEST_DIR"'); + + const attest = publish.indexOf('id: endorse'); + const materialize = publish.indexOf('bash scripts/materialize-release-subjects.sh'); + const descriptorBuild = publish.indexOf('bun scripts/build-delivery-evidence.ts'); + const compatibilityJob = + publish.split('\n delivery-evidence-compatibility:')[1]?.split('\n codex-native-dogfood:')[0] ?? ''; + const publishJob = publish.split('\n publish:')[1]?.split('\n manifests:')[0] ?? ''; + const releaseUpload = publish.indexOf('bash scripts/reconcile-release-assets.sh'); + const manifestsJob = publish.split('\n manifests:')[1]?.split('\n finalize:')[0] ?? ''; + const finalizeJob = publish.split('\n finalize:')[1] ?? ''; + expect(attest).toBeGreaterThan(-1); + expect(materialize).toBeGreaterThan(-1); + expect(descriptorBuild).toBeGreaterThan(materialize); + expect(attest).toBeGreaterThan(descriptorBuild); + expect(releaseUpload).toBeGreaterThan(attest); + expect(compatibilityJob).toContain('needs: [admit, attest-delivery-evidence]'); + expect(compatibilityJob).toContain('permissions:\n contents: read'); + expect(compatibilityJob).not.toContain('id-token:'); + expect(compatibilityJob).not.toContain('attestations:'); + expect(compatibilityJob).toContain('ref: ${{ inputs.source_sha }}'); + expect(compatibilityJob).toContain('bun install --frozen-lockfile --ignore-scripts'); + expect(compatibilityJob).toContain('bun scripts/verify-delivery-evidence-pack.ts'); + expect(compatibilityJob).toContain('name: delivery-candidate-manifests'); + expect(publishJob).toContain('- codex-dogfood-completeness'); + expect(publishJob).toContain('- stable-release-security-gate'); + expect(publishJob).toContain("needs.codex-dogfood-completeness.result == 'success'"); + expect(publishJob).toContain("needs.stable-release-security-gate.result == 'success'"); + expect(publishJob).toContain('name: delivery-candidate-manifests'); + expect(manifestsJob).toContain('needs: finalize'); + expect(manifestsJob).toContain('git push origin "HEAD:refs/heads/main"'); + expect(finalizeJob).toContain('needs: publish'); + expect(finalizeJob).toContain('name: delivery-candidate-manifests'); + expect(finalizeJob.indexOf('bash scripts/reconcile-release-note.sh finalize')).toBeGreaterThan(-1); + expect(finalizeJob.indexOf('bash scripts/release-immutability.sh release')).toBeGreaterThan( + finalizeJob.indexOf('bash scripts/reconcile-release-note.sh finalize'), + ); + expect(finalizeJob.lastIndexOf('bash scripts/reconcile-release-assets.sh')).toBeGreaterThan( + finalizeJob.indexOf('bash scripts/release-immutability.sh release'), + ); + const assetReconciliation = read('scripts/reconcile-release-assets.sh'); + expect(assetReconciliation).toContain('manifest_path="${CANDIDATE_MANIFEST_DIR}/${manifest_name}"'); + expect(assetReconciliation).toContain('.releaseManifestSha256 == $manifest_sha'); + }); + + test('manifest-derived native dogfood and the independent security gate jointly block publication', () => { + const workflow = read('.github/workflows/release-publish.yml'); + const prepare = + workflow.split('\n prepare-delivery-evidence:')[1]?.split('\n attest-delivery-evidence:')[0] ?? ''; + const native = workflow.split('\n codex-native-dogfood:')[1]?.split('\n codex-dogfood-completeness:')[0] ?? ''; + const completeness = + workflow.split('\n codex-dogfood-completeness:')[1]?.split('\n stable-release-security-gate:')[0] ?? ''; + const security = workflow.split('\n stable-release-security-gate:')[1]?.split('\n publish:')[0] ?? ''; + const publish = workflow.split('\n publish:')[1]?.split('\n manifests:')[0] ?? ''; + + // The selected candidate manifest is the sole platform inventory. A + // hand-written representative matrix cannot become promotion evidence. + expect(prepare).toContain('bun scripts/candidate-dogfood-matrix.ts'); + expect(prepare).toContain('--manifest "$SELECTED_MANIFEST"'); + expect(prepare).toContain('mapfile -t MANIFEST_PLATFORMS'); + expect(prepare).toContain('for platform in "${MANIFEST_PLATFORMS[@]}"'); + expect(prepare).not.toContain('PLATFORMS=(linux-x64-glibc'); + expect(prepare).toContain('dogfood_matrix=${DOGFOOD_MATRIX}'); + expect(prepare).toContain('candidate_manifest_sha256=${CANDIDATE_MANIFEST_SHA256}'); + expect(prepare).toContain('name: codex-dogfood-candidate-matrix'); + expect(prepare).toContain('name: codex-dogfood-previous-release'); + expect(prepare).toContain('bash scripts/verify-release.sh --local'); + expect(prepare).not.toContain('--previous-descriptor'); + + expect(native).toContain('matrix: ${{ fromJSON(needs.prepare-delivery-evidence.outputs.dogfood_matrix) }}'); + expect(native).toContain('runs-on: ${{ matrix.runner }}'); + expect(native).toContain('ref: ${{ github.sha }}'); + expect(native).toContain('name: genie-${{ matrix.version }}-${{ matrix.platform }}-signed'); + expect(native).toContain('--previous-provenance "${PREVIOUS_ARTIFACT}.intoto.jsonl"'); + expect(native).toContain('--candidate-descriptor "$CANDIDATE_DESCRIPTOR"'); + expect(native).toContain('--candidate-bundle "${CANDIDATE_DESCRIPTOR}.sigstore.json"'); + expect(native).toContain('EXECUTION_KIND: ${{ matrix.execution }}'); + expect(native).toContain('scripts/run-musl-dogfood.sh'); + expect(native).toContain('--inputs-root dogfood-entry'); + expect(native).toContain('name: codex-dogfood-evidence-${{ matrix.platform }}'); + + // Missing, skipped, duplicated, stale, or identity-mismatched native + // entries fail the aggregate instead of degrading to representative proof. + expect(completeness).toContain('if: ${{ always() }}'); + expect(completeness).toContain('[[ "$PREPARE_RESULT" == success && "$NATIVE_RESULT" == success ]]'); + expect(completeness).toContain('downloaded candidate matrix differs from the matrix used to schedule native jobs'); + expect(completeness).toContain('bun scripts/validate-dogfood-matrix-evidence.ts'); + expect(completeness).toContain('--evidence-dir aggregate/entries'); + expect(completeness).toContain('--candidate-manifest-sha256 "$EXPECTED_MANIFEST_SHA256"'); + const aggregate = read('scripts/validate-dogfood-matrix-evidence.ts'); + expect(aggregate).toContain("entry.evidenceKind !== 'host-native'"); + expect(aggregate).toContain('candidate.manifestSha256 !== options.candidateManifestSha256'); + expect(aggregate).toContain('candidate.artifactSha256 !== matrixEntry.artifactSha256'); + expect(aggregate).toContain("kind: 'codex-dogfood-completeness'"); + + // The machine security proof is read-only, protected-control-derived, and + // independent of dogfood while binding the same exact candidate digest. + expect(security).toContain('if: ${{ always() }}'); + expect(security).toContain('permissions:\n contents: read\n attestations: read'); + expect(security).toContain('ref: ${{ github.sha }}'); + expect(security).toContain('bash scripts/verify-release.sh --local'); + expect(security).toContain('bash scripts/release-generic-provenance.sh verify-exact-subject'); + expect(security).toContain('gh attestation verify "$artifact_path"'); + expect(security).toContain('--predicate-type "https://github.com/${RELEASE_REPOSITORY}/release-tarballs/v1"'); + expect(security).toContain('--source-digest "$CONTROL_SHA"'); + expect(security).toContain('--signer-digest "$CONTROL_SHA"'); + expect(security).toContain('bash scripts/release-native-predicate.sh verify-exact-subject'); + expect(security).toContain('RUN_ID: ${{ github.run_id }}'); + expect(security).toContain('RUN_ATTEMPT: ${{ github.run_attempt }}'); + expect(security).toContain('generic/native artifact digest disagreement'); + expect(security).toContain('version: ([$entries[].native.version] | unique |'); + expect(security).toContain('sourceSha: ([$entries[].native.sourceSha] | unique |'); + const securitySummary = security.split(' jq -cs \\\n')[1] ?? ''; + expect(securitySummary).not.toContain('--arg sourceSha "$SOURCE_SHA"'); + expect(security).toContain('--print-provenance > "$verified_provenance"'); + expect(security).toContain('scripts/release-guard.test.ts'); + expect(security).toContain( + 'EXPECTED_MANIFEST_SHA256: ${{ needs.prepare-delivery-evidence.outputs.candidate_manifest_sha256 }}', + ); + expect(security).not.toContain('needs.codex-native-dogfood'); + expect(security).not.toContain('contents: write'); + expect(security).not.toContain('id-token: write'); + + // All channels use both gates. An unavailable/skipped gate is never + // equivalent to success, and the write-capable publication job stays shut. + expect(publish).toContain('always() &&'); + expect(publish).toContain('- codex-dogfood-completeness'); + expect(publish).toContain('- stable-release-security-gate'); + expect(publish).toContain("needs.codex-dogfood-completeness.result == 'success'"); + expect(publish).toContain("needs.stable-release-security-gate.result == 'success'"); + expect(publish).not.toContain("inputs.channel == 'stable'"); + expect(publish).not.toContain("inputs.channel == 'homolog'"); + }); + test('stable approval is explicit while dev and homolog remain automated', () => { const release = read('.github/workflows/release.yml'); const version = read('.github/workflows/version.yml'); @@ -180,6 +332,26 @@ describe('Group E release and documentation contracts', () => { ); }); + test('each promotion uses a fresh build-accepted immutable release identity', () => { + const version = read('.github/workflows/version.yml'); + const release = read('.github/workflows/release.yml'); + const build = read('.github/workflows/build-tarballs.yml'); + const assets = read('scripts/reconcile-release-assets.sh'); + expect(version).toContain('group: version-release-identity-${{ github.event.workflow_run.event }}'); + expect(version).toContain('queue: max'); + expect(version).toContain('cancel-in-progress: false'); + expect(version).toContain('MAX_COUNTER='); + expect(version).toContain('git tag "v${VERSION}" HEAD'); + expect(version).toContain('name: Push fresh immutable promotion tag'); + expect(version).toContain('git push origin "refs/tags/v${VERSION}"'); + expect(build).toContain('homolog|stable)'); + expect(build).toContain('Authenticated ${INPUT_CHANNEL} promotion stamps immutable version'); + const buildCall = release.split('\n build:')[1]?.split('\n sign-attest:')[0] ?? ''; + expect(buildCall).toContain('channel: ${{ inputs.channel }}'); + expect(assets).toContain('refusing to mutate an incomplete published immutable release'); + expect(assets).not.toContain('append-only promotion'); + }); + test('promotion and tag equivalence exclude only generated channel manifests', () => { for (const path of ['.github/workflows/version.yml', 'scripts/release-guard.sh']) { const source = read(path); @@ -203,7 +375,7 @@ describe('Group E release and documentation contracts', () => { const pkg = JSON.parse(read('package.json')) as { devDependencies: Record }; expect(pkg.devDependencies['@commitlint/cli']).toBeDefined(); expect(pkg.devDependencies['@commitlint/config-conventional']).toBeDefined(); - expect(pkg.devDependencies['markdownlint-cli2']).toBe('0.23.0'); + expect(pkg.devDependencies['markdownlint-cli2']).toBe('0.23.1'); expect(pkg.devDependencies['markdown-link-check']).toBe('3.14.2'); }); @@ -333,6 +505,13 @@ describe('Group E release and documentation contracts', () => { expect(source).toContain('repository-hosted `.well-known'); expect(source).toContain("GitHub's `/releases/latest`"); } + const security = read('SECURITY.md'); + expect(security).toContain( + 'Every dev, homolog, or stable promotion creates a fresh monotonic version and immutable tag', + ); + expect(security).toContain('stable releases are non-prerelease and marked Latest'); + expect(security).toContain('dev and homolog releases are prereleases and never Latest'); + expect(security).not.toContain('one verified version across dev, homolog, and stable channels'); }); test('immutable-release bootstrap ordering remains explicit and fail-closed', () => { @@ -469,11 +648,12 @@ describe('Group E release and documentation contracts', () => { // Lease busy/retry semantics. expect(doc).toContain('exclusive lease'); expect(doc).toContain('no force override'); - // Rollback floor, sync-only, verified-current init, uninstall isolation. + // Rollback floor, sync-only, route-only init, uninstall isolation. expect(doc).toContain('### Rollback floor'); expect(doc).toContain('cannot waive the protocol floor'); expect(doc).toContain('**Sync-only**'); - expect(doc).toContain('exactly `verified-current`'); + expect(doc).toContain('route-only init'); + expect(doc).toContain('independent of plugin availability'); expect(doc).toContain('unreachable from update, install, setup, doctor, sync'); // Homolog candidate channel + the N-task non-guarantee. expect(doc).toContain('Homolog is the canonical pre-stable candidate channel'); @@ -552,7 +732,7 @@ describe('Group E release and documentation contracts', () => { '23 physical', 'Seven optional', '36 adapted skills', - 'mcp-launcher.cjs', + '.codex/config.toml', 'H3', 'H4', 'H6', @@ -562,9 +742,11 @@ describe('Group E release and documentation contracts', () => { expect(docs).toContain(statement); } // Plugin-only contract: the installed plugin is the sole Genie-managed skill provider. - expect(read('README.md')).toContain('the installed plugin is the **only** Genie-managed skill provider'); + expect(read('README.md')).toContain('the **sole** Genie-managed skill provider'); expect(read('README.md')).toContain('Fallback retirement'); expect(read('plugins/genie/README.md')).toContain('the only Genie-managed skill provider'); + expect(read('README.md')).toContain('the plugin declares no Codex MCP route'); + expect(read('plugins/genie/README.md')).toContain('the plugin declares no Codex MCP route'); // The retired CLI-managed-fallback promise must be gone from operator docs. expect(docs).not.toContain('synchronizes up to 23 digest-managed product-skill fallbacks'); expect(docs).not.toContain('CLI-managed product skills'); @@ -615,11 +797,13 @@ describe('Group E release and documentation contracts', () => { expect(root).toContain(term); } expect(lifecycle).toContain('automatically routes the completed DESIGN.md'); - expect(plugin).toContain('successful `genie setup --codex` persists Codex maintenance consent'); - expect(root).toContain('successful Codex setup also persists Codex maintenance consent'); - // Consent no longer authorizes CLI-managed user-tier fallbacks; it refreshes only plugin surfaces. - expect(plugin).toContain('No supported path writes new product'); - expect(root).toContain('never writes new product skills into the user tier'); + expect(plugin).toContain('successful `genie setup --codex` persists Codex delivery scope'); + expect(root).toContain('Successful setup persists Codex delivery scope'); + // Delivery scope authorizes later publication only; setup remains the sole activation/convergence owner. + expect(plugin).toContain('never advance the plugin cache'); + expect(root).toContain('those updates still deliver only'); + expect(plugin).toContain('nothing is written to `~/.agents/skills`'); + expect(root).toContain('Genie never seeds the user tier'); expect(plugin).not.toContain('digest-managed product-skill'); expect(root).not.toContain('digest-managed product-skill fallbacks'); }); @@ -703,12 +887,12 @@ describe('Group E release and documentation contracts', () => { } }); - test('Omni and MCP operator instructions expose provider and fallback policy', () => { + test('Omni and MCP operator instructions expose provider and project-route ownership policy', () => { const omni = read('skills/omni/SKILL.md'); expect(omni).toContain('{instance, chat, repo, agent, persona?}'); expect(omni).toContain('"agent": "codex"'); const readme = read('README.md'); - expect(readme).toContain('no installed, enabled, usable Genie plugin route'); + expect(readme).toContain('The Codex route is plugin-independent'); for (const path of ['.mcp.json', '.warp/.mcp.json', '.codex/config.toml']) expect(readme).toContain(path); }); }); diff --git a/scripts/release-generic-provenance.sh b/scripts/release-generic-provenance.sh index e96e12ae2..23a2a70b8 100644 --- a/scripts/release-generic-provenance.sh +++ b/scripts/release-generic-provenance.sh @@ -70,9 +70,10 @@ verify_dispatch_parameters() { verify_automated_event() { local input="$1" require_exact="$2" - local source_branch="${SOURCE_BRANCH:-}" source_ci_run_id="${SOURCE_CI_RUN_ID:-}" + local source_sha="${SOURCE_SHA:-}" source_branch="${SOURCE_BRANCH:-}" source_ci_run_id="${SOURCE_CI_RUN_ID:-}" jq -e \ --arg repository "$RELEASE_REPOSITORY" \ + --arg source_sha "$source_sha" \ --arg source_branch "$source_branch" \ --arg source_ci_run_id "$source_ci_run_id" \ --argjson require_exact "$require_exact" \ @@ -86,7 +87,9 @@ verify_automated_event() { ($run.head_branch | test("^(dev|homolog)$")) and $run.repository.full_name == $repository and (if $require_exact then - $run.head_branch == $source_branch and ($run.id | tostring) == $source_ci_run_id + $run.head_sha == $source_sha and + $run.head_branch == $source_branch and + ($run.id | tostring) == $source_ci_run_id else true end) ' "$input" >/dev/null } @@ -112,6 +115,43 @@ verify_exact() { fi } +verify_exact_subject() { + local input="${1:-}" + : "${ARTIFACT_SHA256:?ARTIFACT_SHA256 is required}" + [[ "$ARTIFACT_SHA256" =~ ^[0-9a-f]{64}$ ]] || exit 2 + verify_exact "$input" + jq -ce \ + --arg channel "$CHANNEL" \ + --arg artifact_sha256 "$ARTIFACT_SHA256" \ + ' + select(any(.subject[]?; .digest.sha256 == $artifact_sha256)) | + .predicate.invocation as $invocation | + (if $channel == "stable" then + $invocation.parameters.event_inputs | + { + sourceSha: .source_sha, + sourceBranch: .source_branch, + sourceCiRunId: .source_ci_run_id + } + else + $invocation.environment.github_event_payload.workflow_run | + { + sourceSha: .head_sha, + sourceBranch: .head_branch, + sourceCiRunId: (.id | tostring) + } + end) as $source | + { + artifactSha256: (first(.subject[] | select(.digest.sha256 == $artifact_sha256)) | .digest.sha256), + sourceSha: $source.sourceSha, + sourceBranch: $source.sourceBranch, + sourceCiRunId: $source.sourceCiRunId, + controlSha: $invocation.configSource.digest.sha1, + entryPoint: $invocation.configSource.entryPoint + } + ' "$input" +} + verify_reusable() { local input="${1:-}" control_sha entry_point [[ -f "$input" ]] || exit 64 @@ -133,6 +173,7 @@ verify_reusable() { case "${1:-}" in verify-exact) verify_exact "${2:-}" ;; + verify-exact-subject) verify_exact_subject "${2:-}" ;; verify-reusable) verify_reusable "${2:-}" ;; *) exit 64 ;; esac diff --git a/scripts/release-generic-provenance.test.ts b/scripts/release-generic-provenance.test.ts index 43a6172fb..b35cc8024 100644 --- a/scripts/release-generic-provenance.test.ts +++ b/scripts/release-generic-provenance.test.ts @@ -6,6 +6,7 @@ import { join } from 'node:path'; const SCRIPT = join(import.meta.dir, 'release-generic-provenance.sh'); const CONTROL_SHA = 'b'.repeat(40); const SOURCE_SHA = 'a'.repeat(40); +const ARTIFACT_SHA256 = 'd'.repeat(64); const roots: string[] = []; afterEach(() => { @@ -15,6 +16,7 @@ afterEach(() => { function predicate>(invocation: T) { return { predicateType: 'https://slsa.dev/provenance/v0.2', + subject: [{ name: 'genie-5.260714.2-linux-x64-glibc.tar.gz', digest: { sha256: ARTIFACT_SHA256 } }], predicate: { builder: { id: 'https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@refs/tags/v2.1.0', @@ -49,6 +51,7 @@ function automatedStatement() { event: 'push', status: 'completed', conclusion: 'success', + head_sha: SOURCE_SHA, head_branch: 'dev', repository: { full_name: 'automagik-dev/genie' }, }, @@ -82,7 +85,11 @@ function dispatchStatement() { }); } -function invoke(mode: 'verify-exact' | 'verify-reusable', value: unknown, overrides: Record = {}) { +function invoke( + mode: 'verify-exact' | 'verify-exact-subject' | 'verify-reusable', + value: unknown, + overrides: Record = {}, +) { const root = mkdtempSync(join(tmpdir(), 'genie-generic-provenance-')); roots.push(root); const path = join(root, 'statement.json'); @@ -97,6 +104,7 @@ function invoke(mode: 'verify-exact' | 'verify-reusable', value: unknown, overri SOURCE_BRANCH: 'dev', SOURCE_CI_RUN_ID: '123456', CONTROL_SHA, + ARTIFACT_SHA256, ...overrides, }, stdout: 'pipe', @@ -138,6 +146,9 @@ describe('verified generic SLSA provenance policy', () => { (value) => { value.predicate.invocation.environment.github_event_payload.workflow_run.id = 654321; }, + (value) => { + value.predicate.invocation.environment.github_event_payload.workflow_run.head_sha = 'c'.repeat(40); + }, (value) => { value.predicate.invocation.environment.github_event_payload.workflow_run.path = '.github/workflows/attacker.yml'; @@ -157,6 +168,25 @@ describe('verified generic SLSA provenance policy', () => { } }); + test('emits identity only after the verified statement binds the exact artifact subject', () => { + const verified = invoke('verify-exact-subject', automatedStatement()); + expect(verified.exitCode).toBe(0); + expect(JSON.parse(verified.stdout.toString())).toEqual({ + artifactSha256: ARTIFACT_SHA256, + sourceSha: SOURCE_SHA, + sourceBranch: 'dev', + sourceCiRunId: '123456', + controlSha: CONTROL_SHA, + entryPoint: '.github/workflows/version.yml', + }); + + expect( + invoke('verify-exact-subject', automatedStatement(), { + ARTIFACT_SHA256: 'e'.repeat(64), + }).exitCode, + ).not.toBe(0); + }); + test('reusable policy accepts both pipeline generations and rejects malformed signed identity', () => { expect(invoke('verify-reusable', automatedStatement(), { CONTROL_SHA: 'c'.repeat(40) }).exitCode).toBe(0); expect(invoke('verify-reusable', dispatchStatement(), { CONTROL_SHA: 'c'.repeat(40) }).exitCode).toBe(0); diff --git a/scripts/release-native-predicate.sh b/scripts/release-native-predicate.sh index dead67eae..128010a6d 100644 --- a/scripts/release-native-predicate.sh +++ b/scripts/release-native-predicate.sh @@ -92,10 +92,12 @@ verify_result() { --arg source_uri "$SOURCE_URI" \ --arg control_sha "$CONTROL_SHA" \ --arg control_uri "$CONTROL_URI" \ + --arg invocation_id "https://github.com/${RELEASE_REPOSITORY}/actions/runs/${RUN_ID}/attempts/${RUN_ATTEMPT}" \ 'type == "array" and length > 0 and any(.[]; .verificationResult.statement as $statement | $statement.predicateType == $predicate_type and $statement.predicate.runDetails.builder.id == $builder_id and + $statement.predicate.runDetails.metadata.invocationId == $invocation_id and $statement.predicate.buildDefinition.buildType == $build_type and $statement.predicate.buildDefinition.externalParameters.version == $version and $statement.predicate.buildDefinition.externalParameters.channel == $channel and @@ -111,6 +113,59 @@ verify_result() { )' "$input" >/dev/null } +verify_exact_subject() { + local input="${1:-}" + : "${ARTIFACT_SHA256:?ARTIFACT_SHA256 is required}" + [[ "$ARTIFACT_SHA256" =~ ^[0-9a-f]{64}$ ]] || exit 2 + verify_result "$input" + jq -ce \ + --arg predicate_type "$PREDICATE_TYPE" \ + --arg builder_id "$BUILDER_ID" \ + --arg build_type "$BUILD_TYPE" \ + --arg version "$VERSION" \ + --arg channel "$CHANNEL" \ + --arg source_sha "$SOURCE_SHA" \ + --arg source_branch "$SOURCE_BRANCH" \ + --arg source_ci_run_id "$SOURCE_CI_RUN_ID" \ + --arg source_uri "$SOURCE_URI" \ + --arg control_sha "$CONTROL_SHA" \ + --arg control_uri "$CONTROL_URI" \ + --arg invocation_id "https://github.com/${RELEASE_REPOSITORY}/actions/runs/${RUN_ID}/attempts/${RUN_ATTEMPT}" \ + --arg artifact_sha256 "$ARTIFACT_SHA256" \ + 'first( + .[] | + .verificationResult.statement as $statement | + select( + $statement.predicateType == $predicate_type and + $statement.predicate.runDetails.builder.id == $builder_id and + $statement.predicate.runDetails.metadata.invocationId == $invocation_id and + $statement.predicate.buildDefinition.buildType == $build_type and + $statement.predicate.buildDefinition.externalParameters.version == $version and + $statement.predicate.buildDefinition.externalParameters.channel == $channel and + $statement.predicate.buildDefinition.externalParameters.source_sha == $source_sha and + $statement.predicate.buildDefinition.externalParameters.source_branch == $source_branch and + $statement.predicate.buildDefinition.externalParameters.source_ci_run_id == $source_ci_run_id and + $statement.predicate.buildDefinition.externalParameters.control_sha == $control_sha and + ($statement.predicate.buildDefinition.resolvedDependencies | length) == 2 and + any($statement.predicate.buildDefinition.resolvedDependencies[]; + .uri == $source_uri and .digest.gitCommit == $source_sha) and + any($statement.predicate.buildDefinition.resolvedDependencies[]; + .uri == $control_uri and .digest.gitCommit == $control_sha) and + any($statement.subject[]?; .digest.sha256 == $artifact_sha256) + ) | + { + artifactSha256: $artifact_sha256, + version: $statement.predicate.buildDefinition.externalParameters.version, + channel: $statement.predicate.buildDefinition.externalParameters.channel, + sourceSha: $statement.predicate.buildDefinition.externalParameters.source_sha, + sourceBranch: $statement.predicate.buildDefinition.externalParameters.source_branch, + sourceCiRunId: $statement.predicate.buildDefinition.externalParameters.source_ci_run_id, + controlSha: $statement.predicate.buildDefinition.externalParameters.control_sha, + invocationId: $statement.predicate.runDetails.metadata.invocationId + } + )' "$input" +} + # Promotion reuses the exact already-published assets. Their original source # branch/control commit may differ from the promotion run, so validate that the # signed predicate is internally consistent and was produced by the pinned @@ -157,6 +212,7 @@ verify_reusable_result() { case "${1:-}" in create) require_exact_identity; create_predicate "${2:-}" ;; verify) require_exact_identity; verify_result "${2:-}" ;; + verify-exact-subject) require_exact_identity; verify_exact_subject "${2:-}" ;; verify-reusable) verify_reusable_result "${2:-}" ;; reusable-control-sha) reusable_control_sha "${2:-}" ;; *) exit 64 ;; diff --git a/scripts/release-native-predicate.test.ts b/scripts/release-native-predicate.test.ts index 0213b8db2..6098362c0 100644 --- a/scripts/release-native-predicate.test.ts +++ b/scripts/release-native-predicate.test.ts @@ -6,6 +6,7 @@ import { join } from 'node:path'; const SCRIPT = join(import.meta.dir, 'release-native-predicate.sh'); const SOURCE_SHA = 'a'.repeat(40); const CONTROL_SHA = 'b'.repeat(40); +const ARTIFACT_SHA256 = 'd'.repeat(64); const roots: string[] = []; afterEach(() => { @@ -23,10 +24,11 @@ const env = { CONTROL_SHA, RUN_ID: '987654', RUN_ATTEMPT: '2', + ARTIFACT_SHA256, }; function invoke( - mode: 'create' | 'verify' | 'verify-reusable' | 'reusable-control-sha', + mode: 'create' | 'verify' | 'verify-exact-subject' | 'verify-reusable' | 'reusable-control-sha', path: string, overrides: Record = {}, ) { @@ -68,7 +70,11 @@ describe('native release attestation predicate', () => { const verification = [ { verificationResult: { - statement: { predicateType: 'https://github.com/automagik-dev/genie/release-tarballs/v1', predicate }, + statement: { + predicateType: 'https://github.com/automagik-dev/genie/release-tarballs/v1', + subject: [{ name: 'genie-5.260714.2-linux-x64-glibc.tar.gz', digest: { sha256: ARTIFACT_SHA256 } }], + predicate, + }, }, }, ]; @@ -83,6 +89,53 @@ describe('native release attestation predicate', () => { predicate.buildDefinition.resolvedDependencies[1].digest.gitCommit = 'd'.repeat(40); writeFileSync(resultPath, JSON.stringify(verification)); expect(invoke('verify', resultPath).exitCode).not.toBe(0); + + predicate.buildDefinition.resolvedDependencies[1].digest.gitCommit = CONTROL_SHA; + predicate.runDetails.metadata.invocationId = + 'https://github.com/automagik-dev/genie/actions/runs/987654/attempts/3'; + writeFileSync(resultPath, JSON.stringify(verification)); + expect(invoke('verify', resultPath).exitCode).not.toBe(0); + }); + + test('emits signed identity only when the native statement binds the exact artifact subject', () => { + const root = mkdtempSync(join(tmpdir(), 'genie-native-subject-')); + roots.push(root); + const predicatePath = join(root, 'predicate.json'); + const resultPath = join(root, 'result.json'); + expect(invoke('create', predicatePath).exitCode).toBe(0); + const predicate = JSON.parse(readFileSync(predicatePath, 'utf8')); + writeFileSync( + resultPath, + JSON.stringify([ + { + verificationResult: { + statement: { + predicateType: 'https://github.com/automagik-dev/genie/release-tarballs/v1', + subject: [{ digest: { sha256: ARTIFACT_SHA256 } }], + predicate, + }, + }, + }, + ]), + ); + + const verified = invoke('verify-exact-subject', resultPath); + expect(verified.exitCode).toBe(0); + expect(JSON.parse(verified.stdout.toString())).toEqual({ + artifactSha256: ARTIFACT_SHA256, + version: '5.260714.2', + channel: 'dev', + sourceSha: SOURCE_SHA, + sourceBranch: 'dev', + sourceCiRunId: '123456', + controlSha: CONTROL_SHA, + invocationId: 'https://github.com/automagik-dev/genie/actions/runs/987654/attempts/2', + }); + expect( + invoke('verify-exact-subject', resultPath, { + ARTIFACT_SHA256: 'e'.repeat(64), + }).exitCode, + ).not.toBe(0); }); test('malformed source identity is rejected before predicate creation', () => { diff --git a/scripts/run-musl-dogfood.sh b/scripts/run-musl-dogfood.sh new file mode 100755 index 000000000..6530ea674 --- /dev/null +++ b/scripts/run-musl-dogfood.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# +# Execute one authenticated linux-x64-musl candidate inside the same +# digest-pinned Alpine environment used by the release build smoke. +# +# Invocation: +# scripts/run-musl-dogfood.sh /absolute/path/to/genie +# +# The Group F harness hashes and authenticates the binary before invoking this +# adapter. This boundary additionally requires a physical absolute executable +# and passes argv positionally, never through shell interpolation. + +set -euo pipefail + +readonly ALPINE_IMAGE='alpine:3.19@sha256:6baf43584bcb78f2e5847d1de515f23499913ac9f12bdf834811a3145eb11ca1' + +if [[ $# -lt 2 ]]; then + echo 'run-musl-dogfood: expected an absolute candidate binary and at least one argument' >&2 + exit 64 +fi + +candidate_binary=$1 +shift + +if [[ "$(uname -s)" != 'Linux' || "$(uname -m)" != 'x86_64' ]]; then + echo 'run-musl-dogfood: the Alpine adapter requires a Linux x86_64 Docker host' >&2 + exit 1 +fi +if [[ "$candidate_binary" != /* || ! -f "$candidate_binary" || -L "$candidate_binary" || ! -x "$candidate_binary" ]]; then + echo "run-musl-dogfood: candidate must be an absolute physical executable: ${candidate_binary}" >&2 + exit 1 +fi + +candidate_dir=$(cd -- "$(dirname -- "$candidate_binary")" && pwd -P) +candidate_name=$(basename -- "$candidate_binary") +if [[ ! "$candidate_name" =~ ^[A-Za-z0-9._+-]+$ || "$candidate_dir" == *','* || "$candidate_dir" == *$'\n'* ]]; then + echo 'run-musl-dogfood: candidate path cannot be represented safely as a Docker bind mount' >&2 + exit 1 +fi +candidate_canonical="${candidate_dir}/${candidate_name}" +if [[ "$candidate_canonical" != "$candidate_binary" ]]; then + echo 'run-musl-dogfood: candidate path must already be canonical' >&2 + exit 1 +fi +if ! command -v docker >/dev/null 2>&1; then + echo 'run-musl-dogfood: Docker is unavailable' >&2 + exit 1 +fi + +docker_args=(run --rm --pull=always -i --security-opt no-new-privileges) +container_candidate="/candidate/${candidate_name}" + +# The one-shot capability probe needs only a read-only candidate directory. +# Full release dogfood sets DOGFOOD_ROOT: preserve its isolated home/repository +# state across candidate invocations by mounting that one canonical fixture +# root at the identical absolute path. Re-bind the executable itself read-only +# so stateful commands cannot replace the authenticated candidate bytes. +if [[ -n "${DOGFOOD_ROOT:-}" ]]; then + dogfood_root=$(cd -- "$DOGFOOD_ROOT" 2>/dev/null && pwd -P) || { + echo 'run-musl-dogfood: DOGFOOD_ROOT must be an existing physical directory' >&2 + exit 1 + } + host_cwd=$(pwd -P) + if [[ "$DOGFOOD_ROOT" != /* || "$DOGFOOD_ROOT" != "$dogfood_root" || + "$dogfood_root" == *','* || "$dogfood_root" == *$'\n'* || + "$host_cwd" != "$dogfood_root" && "$host_cwd" != "$dogfood_root/"* || + "$candidate_binary" != "$dogfood_root/"* ]]; then + echo 'run-musl-dogfood: stateful root, cwd, and candidate must be canonical and contained' >&2 + exit 1 + fi + docker_args+=( + --mount "type=bind,src=${dogfood_root},dst=${dogfood_root}" + --mount "type=bind,src=${candidate_binary},dst=${candidate_binary},readonly" + --workdir "$host_cwd" + ) + container_candidate=$candidate_binary + for key in \ + HOME GENIE_HOME CODEX_HOME CLAUDE_CONFIG_DIR HERMES_HOME GENIE_AGENTS_SKILLS_DIR \ + TMPDIR XDG_CONFIG_HOME XDG_CACHE_HOME XDG_DATA_HOME XDG_STATE_HOME \ + BUN_INSTALL_CACHE_DIR NPM_CONFIG_CACHE GIT_CONFIG_GLOBAL GIT_CONFIG_NOSYSTEM \ + GENIE_TEST_SKIP_PGSERVE GENIE_RELEASE_DOGFOOD FAKE_CODEX_STATE FAKE_CODEX_TARGET \ + NO_COLOR TERM; do + if [[ -n "${!key:-}" ]]; then docker_args+=(--env "$key"); fi + done + docker_args+=(--env "PATH=${dogfood_root}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") +else + docker_args+=(--mount "type=bind,src=${candidate_dir},dst=/candidate,readonly") +fi +if [[ -t 0 && -t 1 ]]; then docker_args+=(-t); fi + +exec docker "${docker_args[@]}" \ + "$ALPINE_IMAGE" \ + sh -ec ' + apk add --no-cache bash git libstdc++ >/dev/null + candidate=$1 + shift + exec "$candidate" "$@" + ' sh "$container_candidate" "$@" diff --git a/scripts/run-musl-dogfood.test.ts b/scripts/run-musl-dogfood.test.ts new file mode 100644 index 000000000..f63b8f969 --- /dev/null +++ b/scripts/run-musl-dogfood.test.ts @@ -0,0 +1,109 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { + chmodSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + rmSync, + symlinkSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const SCRIPT = join(import.meta.dir, 'run-musl-dogfood.sh'); +const roots: string[] = []; + +function fixture(): { root: string; binary: string; log: string; env: Record } { + const root = realpathSync(mkdtempSync(join(tmpdir(), 'genie-musl-adapter-'))); + roots.push(root); + const bin = join(root, 'bin'); + mkdirSync(bin); + const binary = join(root, 'candidate-genie'); + writeFileSync(binary, '#!/bin/sh\nexit 0\n'); + chmodSync(binary, 0o755); + const log = join(root, 'docker.log'); + writeFileSync(join(bin, 'uname'), `#!/bin/sh\nif [ "$1" = "-s" ]; then printf Linux; else printf x86_64; fi\n`); + writeFileSync(join(bin, 'docker'), `#!/bin/sh\nprintf '%s\\n' "$@" > "$DOGFOOD_DOCKER_LOG"\n`); + chmodSync(join(bin, 'uname'), 0o755); + chmodSync(join(bin, 'docker'), 0o755); + return { + root, + binary, + log, + env: { PATH: `${bin}:/usr/bin:/bin`, DOGFOOD_DOCKER_LOG: log }, + }; +} + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +describe('musl dogfood execution adapter', () => { + test('binds a physical candidate read-only and passes adversarial argv without evaluation', () => { + const fx = fixture(); + const result = Bun.spawnSync( + ['bash', SCRIPT, fx.binary, 'update', '--value', '$(touch should-not-exist); spaced'], + { env: fx.env, stdout: 'pipe', stderr: 'pipe' }, + ); + expect(result.exitCode).toBe(0); + const args = readFileSync(fx.log, 'utf8').split('\n'); + expect(args).toContain('--pull=always'); + expect(args).toContain('alpine:3.19@sha256:6baf43584bcb78f2e5847d1de515f23499913ac9f12bdf834811a3145eb11ca1'); + expect(args).toContain(`type=bind,src=${fx.root},dst=/candidate,readonly`); + expect(args).toContain('$(touch should-not-exist); spaced'); + expect(existsSync(join(fx.root, 'should-not-exist'))).toBe(false); + }); + + test('stateful mode preserves the exact fixture cwd and rebinds the authenticated candidate read-only', () => { + const fx = fixture(); + const repo = join(fx.root, 'repo'); + mkdirSync(repo); + const env = { + ...fx.env, + DOGFOOD_ROOT: fx.root, + HOME: join(fx.root, 'home'), + GENIE_HOME: join(fx.root, 'genie-home'), + CODEX_HOME: join(fx.root, 'codex-home'), + }; + const result = Bun.spawnSync(['bash', SCRIPT, fx.binary, 'mcp'], { + cwd: repo, + env, + stdout: 'pipe', + stderr: 'pipe', + }); + expect(result.exitCode).toBe(0); + const args = readFileSync(fx.log, 'utf8').split('\n'); + expect(args).toContain(`type=bind,src=${fx.root},dst=${fx.root}`); + expect(args).toContain(`type=bind,src=${fx.binary},dst=${fx.binary},readonly`); + expect(args).toContain('--workdir'); + expect(args).toContain(repo); + expect(args).toContain('HOME'); + expect(args).toContain(`PATH=${fx.root}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`); + }); + + test('rejects relative, symlinked, non-executable, and non-canonical candidates before Docker', () => { + for (const kind of ['relative', 'symlink', 'non-executable', 'non-canonical'] as const) { + const fx = fixture(); + const nonExecutable = join(fx.root, 'non-executable'); + writeFileSync(nonExecutable, 'not executable\n'); + const symlink = join(fx.root, 'candidate-link'); + symlinkSync(fx.binary, symlink); + const candidate = { + relative: 'relative-genie', + symlink, + 'non-executable': nonExecutable, + 'non-canonical': join(fx.root, '.', 'candidate-genie').replace('/candidate-genie', '//candidate-genie'), + }[kind]; + const result = Bun.spawnSync(['bash', SCRIPT, candidate, '--version'], { + env: fx.env, + stdout: 'pipe', + stderr: 'pipe', + }); + expect(result.exitCode).not.toBe(0); + expect(existsSync(fx.log)).toBe(false); + } + }); +}); diff --git a/scripts/validate-dogfood-matrix-evidence.test.ts b/scripts/validate-dogfood-matrix-evidence.test.ts new file mode 100644 index 000000000..c40e001aa --- /dev/null +++ b/scripts/validate-dogfood-matrix-evidence.test.ts @@ -0,0 +1,177 @@ +import { describe, expect, test } from 'bun:test'; +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { type DogfoodEntryValidator, validateDogfoodMatrixEvidence } from './validate-dogfood-matrix-evidence.ts'; + +const SOURCE_SHA = 'a'.repeat(40); +const MANIFEST_SHA = 'b'.repeat(64); +const VERSION = '5.260723.9'; +const PLATFORMS = ['linux-x64-glibc', 'linux-x64-musl', 'linux-arm64', 'darwin-arm64']; + +function fixture(platforms = PLATFORMS): { + root: string; + matrix: string; + evidenceDir: string; +} { + const root = mkdtempSync(join(tmpdir(), 'genie-dogfood-aggregate-')); + const evidenceDir = join(root, 'evidence'); + mkdirSync(evidenceDir); + const matrix = join(root, 'matrix.json'); + writeFileSync( + matrix, + JSON.stringify({ + include: platforms.map((platform, index) => ({ + platform, + runner: + platform === 'linux-arm64' ? 'ubuntu-24.04-arm' : platform === 'darwin-arm64' ? 'macos-15' : 'ubuntu-latest', + execution: platform.endsWith('musl') ? 'alpine-container' : 'host-native', + version: VERSION, + channel: 'homolog', + manifest: 'homolog.json', + manifestSha256: MANIFEST_SHA, + artifact: `genie-${VERSION}-${platform}.tar.gz`, + artifactSha256: String(index + 1).repeat(64), + bundle: `genie-${VERSION}-${platform}.tar.gz.bundle`, + provenance: `genie-${VERSION}-${platform}.tar.gz.intoto.jsonl`, + })), + }), + ); + for (const [index, platform] of platforms.entries()) { + const directory = join(evidenceDir, platform); + mkdirSync(directory); + writeFileSync( + join(directory, `codex-dogfood-${VERSION}-${platform}.md`), + evidence(platform, String(index + 1).repeat(64)), + ); + } + return { root, matrix, evidenceDir }; +} + +function evidence(platform: string, artifactSha256: string): string { + const manifest = { + kind: 'live-dogfood-evidence', + schemaVersion: 2, + entry: { + platformId: platform, + evidenceKind: 'host-native', + availability: 'verified', + }, + lifecycle: { + previousVersion: '5.260720.10', + candidateVersion: VERSION, + channel: 'homolog', + sourceCommit: SOURCE_SHA, + artifacts: { + previous: { channel: 'stable' }, + candidate: { manifestSha256: MANIFEST_SHA, artifactSha256 }, + }, + }, + }; + return `# evidence\n\n\`\`\`json\n${JSON.stringify(manifest, null, 2)}\n\`\`\`\n`; +} + +const accept: DogfoodEntryValidator = () => []; + +function validate(fx: ReturnType, validator = accept) { + return validateDogfoodMatrixEvidence( + { + matrixPath: fx.matrix, + evidenceDir: fx.evidenceDir, + version: VERSION, + channel: 'homolog', + sourceSha: SOURCE_SHA, + candidateManifestSha256: MANIFEST_SHA, + }, + validator, + ); +} + +describe('native dogfood matrix evidence aggregate', () => { + test('accepts exactly one host-native result per manifest-derived entry', () => { + const summary = validate(fixture()); + expect(summary).toMatchObject({ + schemaVersion: 1, + kind: 'codex-dogfood-completeness', + evidenceSchemaVersion: 2, + version: VERSION, + sourceSha: SOURCE_SHA, + candidateManifestSha256: MANIFEST_SHA, + }); + expect(summary.entries.map((entry) => entry.platformId)).toEqual([ + 'darwin-arm64', + 'linux-arm64', + 'linux-x64-glibc', + 'linux-x64-musl', + ]); + expect(summary.entries.every((entry) => entry.previousVersion === '5.260720.10')).toBe(true); + }); + + test('rejects missing, extra, and duplicate native evidence', () => { + const missing = fixture(); + writeFileSync( + missing.matrix, + JSON.stringify({ include: JSON.parse(readText(missing.matrix)).include.slice(0, 1) }), + ); + expect(() => validate(missing)).toThrow(/native entries|count/); + + const extra = fixture(); + const extraDir = join(extra.evidenceDir, 'extra'); + mkdirSync(extraDir); + writeFileSync(join(extraDir, 'extra.md'), evidence('windows-x64', '1'.repeat(64))); + expect(() => validate(extra)).toThrow(/count|non-manifest/); + + const duplicate = fixture(); + const duplicateDir = join(duplicate.evidenceDir, 'duplicate'); + mkdirSync(duplicateDir); + writeFileSync(join(duplicateDir, 'duplicate.md'), evidence('linux-x64-glibc', '1'.repeat(64))); + expect(() => validate(duplicate)).toThrow(/count|duplicate/); + }); + + test('rejects candidate identity, digest, availability, and prior-channel drift', () => { + for (const mutate of [ + (value: Record) => { + (value.lifecycle as Record).sourceCommit = 'c'.repeat(40); + }, + (value: Record) => { + const lifecycle = value.lifecycle as Record; + const artifacts = lifecycle.artifacts as Record>; + artifacts.candidate.artifactSha256 = 'f'.repeat(64); + }, + (value: Record) => { + (value.entry as Record).availability = 'unavailable'; + }, + (value: Record) => { + const lifecycle = value.lifecycle as Record; + const artifacts = lifecycle.artifacts as Record>; + artifacts.previous.channel = 'dev'; + }, + (value: Record) => { + (value.lifecycle as Record).previousVersion = '5.260724.1'; + }, + ]) { + const fx = fixture(); + const file = join(fx.evidenceDir, 'linux-x64-glibc', `codex-dogfood-${VERSION}-linux-x64-glibc.md`); + const value = JSON.parse(evidenceJson(readText(file))) as Record; + mutate(value); + writeFileSync(file, `# evidence\n\n\`\`\`json\n${JSON.stringify(value, null, 2)}\n\`\`\`\n`); + expect(() => validate(fx)).toThrow(); + } + }); + + test('propagates deep per-entry validation failures', () => { + expect(() => validate(fixture(), () => ['referenced candidate artifact digest mismatch'])).toThrow( + /referenced candidate artifact digest mismatch/, + ); + }); +}); + +function readText(path: string): string { + return readFileSync(path, 'utf8'); +} + +function evidenceJson(markdown: string): string { + const match = markdown.match(/```json\s*\n([\s\S]*?)\n```/); + if (!match?.[1]) throw new Error('missing fixture manifest'); + return match[1]; +} diff --git a/scripts/validate-dogfood-matrix-evidence.ts b/scripts/validate-dogfood-matrix-evidence.ts new file mode 100644 index 000000000..9d4c7a80c --- /dev/null +++ b/scripts/validate-dogfood-matrix-evidence.ts @@ -0,0 +1,321 @@ +#!/usr/bin/env bun + +/** + * Aggregate the native Group F results for one exact candidate manifest. + * + * The per-entry validator owns the deep lifecycle and referenced-input checks. + * This layer proves the release-level relation: every manifest-derived matrix + * entry has exactly one host-native result, there are no extra results, and + * every result binds the same candidate version/channel/source/manifest bytes. + */ + +import { createHash } from 'node:crypto'; +import { type Dirent, type Stats, lstatSync, readFileSync, readdirSync, realpathSync, writeFileSync } from 'node:fs'; +import { basename, dirname, join, resolve, sep } from 'node:path'; +import { compareReleaseVersions, parseReleaseVersion } from '../src/lib/codex-release-version.ts'; +import { NATIVE_DOGFOOD_TARGETS } from './candidate-dogfood-matrix.ts'; +import { LIVE_DOGFOOD_SCHEMA_VERSION, validateLiveDogfoodEvidenceFile } from './validate-live-dogfood-evidence.ts'; + +const SHA256 = /^[0-9a-f]{64}$/; +const SOURCE_SHA = /^[0-9a-f]{40}$/; +const VERSION = /^\d+\.\d{6}\.\d+$/; +const CHANNELS = new Set(['dev', 'homolog', 'stable']); + +type JsonRecord = Record; +export type DogfoodEntryValidator = (path: string, inputsRoot: string) => string[]; + +export interface DogfoodMatrixValidationOptions { + matrixPath: string; + evidenceDir: string; + version: string; + channel: string; + sourceSha: string; + candidateManifestSha256: string; +} + +export interface DogfoodMatrixEvidenceSummary { + schemaVersion: 1; + kind: 'codex-dogfood-completeness'; + evidenceSchemaVersion: typeof LIVE_DOGFOOD_SCHEMA_VERSION; + version: string; + channel: string; + sourceSha: string; + candidateManifestSha256: string; + entries: Array<{ + platformId: string; + artifactSha256: string; + evidenceSha256: string; + evidenceFile: string; + previousVersion: string; + }>; +} + +function isRecord(value: unknown): value is JsonRecord { + return typeof value === 'object' && value !== null && !Array.isArray(value); +} + +function readPhysical(path: string, label: string): Buffer { + let stat: Stats; + try { + stat = lstatSync(path); + } catch { + throw new Error(`${label} is unavailable: ${path}`); + } + if (!stat.isFile() || stat.isSymbolicLink()) throw new Error(`${label} must be a physical regular file: ${path}`); + const bytes = readFileSync(path); + if (bytes.length === 0) throw new Error(`${label} must not be empty: ${path}`); + return bytes; +} + +function sha256(bytes: Uint8Array): string { + return createHash('sha256').update(bytes).digest('hex'); +} + +function parseJsonObject(bytes: Uint8Array, label: string): JsonRecord { + let parsed: unknown; + try { + parsed = JSON.parse(Buffer.from(bytes).toString('utf8')); + } catch { + throw new Error(`${label} is not valid JSON`); + } + if (!isRecord(parsed)) throw new Error(`${label} must contain a JSON object`); + return parsed; +} + +function collectEvidenceFiles(root: string): string[] { + const requestedRoot = resolve(root); + const requestedStat = lstatSync(requestedRoot); + if (!requestedStat.isDirectory() || requestedStat.isSymbolicLink()) { + throw new Error(`evidence root must be a physical directory: ${root}`); + } + const physicalRoot = realpathSync(requestedRoot); + const found: string[] = []; + const visit = (directory: string, depth: number): void => { + if (depth > 4) throw new Error(`evidence directory nesting exceeds four levels: ${directory}`); + const entries = readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)); + if (entries.length > 128) throw new Error(`evidence directory has too many entries: ${directory}`); + for (const entry of entries) { + assertSafeEntry(entry, directory, physicalRoot); + const path = join(directory, entry.name); + if (entry.isDirectory()) visit(path, depth + 1); + else if (entry.isFile() && entry.name.endsWith('.md')) found.push(path); + } + }; + visit(physicalRoot, 0); + return found; +} + +function assertSafeEntry(entry: Dirent, directory: string, root: string): void { + const path = join(directory, entry.name); + const stat = lstatSync(path); + if (stat.isSymbolicLink() || (!stat.isFile() && !stat.isDirectory())) { + throw new Error(`evidence tree contains a non-physical entry: ${path}`); + } + const canonical = realpathSync(path); + if (canonical !== root && !canonical.startsWith(`${root}${sep}`)) { + throw new Error(`evidence tree entry escapes its root: ${path}`); + } +} + +function extractEvidenceManifest(markdown: string, path: string): JsonRecord { + const manifests: JsonRecord[] = []; + for (const match of markdown.matchAll(/```json\s*\n([\s\S]*?)\n```/g)) { + try { + const value: unknown = JSON.parse(match[1]); + if (isRecord(value) && value.kind === 'live-dogfood-evidence') manifests.push(value); + } catch { + // The per-entry validator reports malformed blocks with richer context. + } + } + if (manifests.length !== 1) { + throw new Error(`${path} must contain exactly one live-dogfood-evidence manifest (got ${manifests.length})`); + } + return manifests[0] as JsonRecord; +} + +function requiredRecord(parent: JsonRecord, key: string, label: string): JsonRecord { + const value = parent[key]; + if (!isRecord(value)) throw new Error(`${label}.${key} must be an object`); + return value; +} + +function requiredString(parent: JsonRecord, key: string, label: string): string { + const value = parent[key]; + if (typeof value !== 'string' || value === '') throw new Error(`${label}.${key} must be a non-empty string`); + return value; +} + +function parseMatrix(path: string): JsonRecord[] { + const matrix = parseJsonObject(readPhysical(path, 'candidate dogfood matrix'), 'candidate dogfood matrix'); + if (!Array.isArray(matrix.include) || matrix.include.length === 0) { + throw new Error('candidate dogfood matrix include must be a non-empty array'); + } + if (matrix.include.some((entry) => !isRecord(entry))) { + throw new Error('candidate dogfood matrix entries must be objects'); + } + return matrix.include as JsonRecord[]; +} + +export function validateDogfoodMatrixEvidence( + options: DogfoodMatrixValidationOptions, + validateEntry: DogfoodEntryValidator = validateLiveDogfoodEvidenceFile, +): DogfoodMatrixEvidenceSummary { + if (!VERSION.test(options.version)) throw new Error('candidate version is invalid'); + if (!CHANNELS.has(options.channel)) throw new Error('candidate channel is invalid'); + if (!SOURCE_SHA.test(options.sourceSha)) throw new Error('candidate source SHA is invalid'); + if (!SHA256.test(options.candidateManifestSha256)) throw new Error('candidate manifest SHA-256 is invalid'); + + const matrix = parseMatrix(resolve(options.matrixPath)); + const expectedPlatforms = Object.keys(NATIVE_DOGFOOD_TARGETS); + if (matrix.length !== expectedPlatforms.length) { + throw new Error(`candidate dogfood matrix must contain exactly ${expectedPlatforms.length} native entries`); + } + const byPlatform = new Map(); + for (const entry of matrix) { + const platform = requiredString(entry, 'platform', 'matrix entry'); + const target = NATIVE_DOGFOOD_TARGETS[platform as keyof typeof NATIVE_DOGFOOD_TARGETS]; + if (target === undefined) throw new Error(`candidate dogfood matrix contains unsupported platform ${platform}`); + if (byPlatform.has(platform)) throw new Error(`candidate dogfood matrix contains duplicate platform ${platform}`); + if (entry.runner !== target.runner || entry.execution !== target.execution) { + throw new Error(`candidate dogfood matrix runner mapping mismatch for ${platform}`); + } + if (entry.version !== options.version || entry.channel !== options.channel) { + throw new Error(`matrix entry ${platform} does not bind candidate ${options.version}/${options.channel}`); + } + if (entry.manifestSha256 !== options.candidateManifestSha256) { + throw new Error(`matrix entry ${platform} candidate manifest digest mismatch`); + } + if (typeof entry.artifactSha256 !== 'string' || !SHA256.test(entry.artifactSha256)) { + throw new Error(`matrix entry ${platform} artifact digest is invalid`); + } + byPlatform.set(platform, entry); + } + const missingMatrixPlatforms = expectedPlatforms.filter((platform) => !byPlatform.has(platform)); + if (missingMatrixPlatforms.length > 0) { + throw new Error(`candidate dogfood matrix is missing native platforms: ${missingMatrixPlatforms.join(', ')}`); + } + + const evidenceFiles = collectEvidenceFiles(options.evidenceDir); + if (evidenceFiles.length !== matrix.length) { + throw new Error(`native evidence count ${evidenceFiles.length} does not equal matrix count ${matrix.length}`); + } + const seen = new Set(); + const summaries: DogfoodMatrixEvidenceSummary['entries'] = []; + for (const file of evidenceFiles) { + const bytes = readPhysical(file, 'native dogfood evidence'); + const errors = validateEntry(file, dirname(file)); + if (errors.length > 0) throw new Error(`${file} failed entry validation:\n${errors.join('\n')}`); + const manifest = extractEvidenceManifest(bytes.toString('utf8'), file); + if (manifest.schemaVersion !== LIVE_DOGFOOD_SCHEMA_VERSION) { + throw new Error(`${file} has unsupported evidence schema ${String(manifest.schemaVersion)}`); + } + const entry = requiredRecord(manifest, 'entry', 'evidence'); + const lifecycle = requiredRecord(manifest, 'lifecycle', 'evidence'); + const artifacts = requiredRecord(lifecycle, 'artifacts', 'evidence.lifecycle'); + const previous = requiredRecord(artifacts, 'previous', 'evidence.lifecycle.artifacts'); + const candidate = requiredRecord(artifacts, 'candidate', 'evidence.lifecycle.artifacts'); + const platform = requiredString(entry, 'platformId', 'evidence.entry'); + if (seen.has(platform)) throw new Error(`native evidence contains duplicate platform ${platform}`); + seen.add(platform); + const matrixEntry = byPlatform.get(platform); + if (matrixEntry === undefined) throw new Error(`native evidence contains non-manifest platform ${platform}`); + if (entry.evidenceKind !== 'host-native' || entry.availability !== 'verified') { + throw new Error(`native evidence ${platform} is unavailable or not host-native`); + } + if ( + lifecycle.candidateVersion !== options.version || + lifecycle.channel !== options.channel || + lifecycle.sourceCommit !== options.sourceSha + ) { + throw new Error(`native evidence ${platform} candidate identity mismatch`); + } + if (candidate.manifestSha256 !== options.candidateManifestSha256) { + throw new Error(`native evidence ${platform} candidate manifest digest mismatch`); + } + if (candidate.artifactSha256 !== matrixEntry.artifactSha256) { + throw new Error(`native evidence ${platform} candidate artifact digest mismatch`); + } + if (previous.channel !== 'stable') throw new Error(`native evidence ${platform} previous generation is not stable`); + const previousVersion = requiredString(lifecycle, 'previousVersion', 'evidence.lifecycle'); + const parsedPrevious = parseReleaseVersion(previousVersion); + const parsedCandidate = parseReleaseVersion(options.version); + if ( + parsedPrevious === null || + parsedCandidate === null || + compareReleaseVersions(parsedPrevious, parsedCandidate) >= 0 + ) { + throw new Error(`native evidence ${platform} previous stable N is not older than candidate T`); + } + summaries.push({ + platformId: platform, + artifactSha256: String(matrixEntry.artifactSha256), + evidenceSha256: sha256(bytes), + evidenceFile: basename(file), + previousVersion, + }); + } + const missing = [...byPlatform.keys()].filter((platform) => !seen.has(platform)); + if (missing.length > 0) throw new Error(`native evidence is missing matrix platforms: ${missing.join(', ')}`); + const previousVersions = new Set(summaries.map((entry) => entry.previousVersion)); + if (previousVersions.size !== 1) throw new Error('native evidence entries do not share one previous stable version'); + return { + schemaVersion: 1, + kind: 'codex-dogfood-completeness', + evidenceSchemaVersion: LIVE_DOGFOOD_SCHEMA_VERSION, + version: options.version, + channel: options.channel, + sourceSha: options.sourceSha, + candidateManifestSha256: options.candidateManifestSha256, + entries: summaries.sort((a, b) => a.platformId.localeCompare(b.platformId)), + }; +} + +function parseArgs(argv: string[]): DogfoodMatrixValidationOptions & { output: string } { + const values = new Map(); + for (let index = 0; index < argv.length; index += 2) { + const flag = argv[index]; + const value = argv[index + 1]; + if (!flag?.startsWith('--') || !value || values.has(flag)) throw new Error(`invalid argument: ${flag ?? ''}`); + values.set(flag, value); + } + const required = (flag: string): string => { + const value = values.get(flag); + if (!value) throw new Error(`missing ${flag}`); + return value; + }; + const allowed = new Set([ + '--matrix', + '--evidence-dir', + '--version', + '--channel', + '--source-sha', + '--candidate-manifest-sha256', + '--output', + ]); + for (const flag of values.keys()) if (!allowed.has(flag)) throw new Error(`unknown argument: ${flag}`); + return { + matrixPath: required('--matrix'), + evidenceDir: required('--evidence-dir'), + version: required('--version'), + channel: required('--channel'), + sourceSha: required('--source-sha'), + candidateManifestSha256: required('--candidate-manifest-sha256'), + output: required('--output'), + }; +} + +function main(): void { + try { + const { output, ...options } = parseArgs(process.argv.slice(2)); + const summary = validateDogfoodMatrixEvidence(options); + writeFileSync(output, `${JSON.stringify(summary, null, 2)}\n`, { flag: 'wx', mode: 0o600 }); + process.stdout.write(`validate-dogfood-matrix-evidence: OK (${summary.entries.length} native entries)\n`); + } catch (error) { + process.stderr.write( + `validate-dogfood-matrix-evidence: FAIL — ${error instanceof Error ? error.message : String(error)}\n`, + ); + process.exit(1); + } +} + +if (import.meta.main) main(); diff --git a/scripts/validate-live-dogfood-evidence.test.ts b/scripts/validate-live-dogfood-evidence.test.ts index 5230877d0..6fbe183b3 100644 --- a/scripts/validate-live-dogfood-evidence.test.ts +++ b/scripts/validate-live-dogfood-evidence.test.ts @@ -1,181 +1,431 @@ import { describe, expect, test } from 'bun:test'; -import { REQUIRED_STEP_IDS, validateLiveDogfoodEvidence } from './validate-live-dogfood-evidence.ts'; +import { createHash } from 'node:crypto'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { + LIVE_DOGFOOD_SCHEMA_VERSION, + REQUIRED_STAGE_IDS, + validateLiveDogfoodEvidence, + validateLiveDogfoodEvidenceFile, +} from './validate-live-dogfood-evidence.ts'; -const SNAPSHOT = { - nIdentity: 'dev:12/ino:3400 v5.260721.7', - tIdentity: 'dev:12/ino:3401 v5.260721.8', - inventoryDigest: 'b'.repeat(64), -}; +const N = '5.260720.10'; +const T = '5.260723.7'; +const PLATFORM = 'darwin-arm64'; +const HEX = (character: string, size = 64) => character.repeat(size); -function validManifest(): Record { +function provenance(kind: 'release-tarball' | 'delivery-evidence', source: string) { return { - kind: 'live-dogfood-evidence', - schemaVersion: 1, - candidate: { commit: 'a'.repeat(40), version: '5.260721.8', channel: 'homolog' }, - inertness: { - codexPluginList: { before: { ...SNAPSHOT }, after: { ...SNAPSHOT } }, - genieDoctor: { before: { ...SNAPSHOT }, after: { ...SNAPSHOT } }, - }, - steps: REQUIRED_STEP_IDS.map((id) => ({ - id, - command: `run ${id}`, - exit: id === 'update-delivery-exit2' ? 2 : 0, - output: id === 'update-delivery-exit2' ? '{"schemaVersion":1,"deliveryComplete":true}' : `${id} ok`, - })), - nNonGuarantee: 'Retired task N may be gone after activation and requires /hooks review plus a new task to resume.', + kind, + repository: 'automagik-dev/genie', + predicateType: + kind === 'release-tarball' + ? 'https://slsa.dev/provenance/v0.2' + : 'https://github.com/automagik-dev/genie/delivery-evidence/v1', + workflowIdentity: + kind === 'release-tarball' + ? 'https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@refs/tags/v2.1.0' + : 'https://github.com/automagik-dev/genie/.github/workflows/release-publish.yml@refs/heads/main', + oidcIssuer: 'https://token.actions.githubusercontent.com', + sourceCommit: source, + controlCommit: HEX('c', 40), + sourceBranch: 'main', + sourceCiRunId: '1234', + identitySha256: HEX(kind === 'release-tarball' ? '1' : '2'), + bundleSha256: HEX(kind === 'release-tarball' ? '3' : '4'), }; } -const DOCTOR_PAYLOAD = { - ok: true, - checks: [{ id: 'codex', ok: true }], - integrationSummary: { state: 'current', actionRequired: false, deliveryComplete: true }, -}; - -function buildEvidence(manifest: unknown, doctor: unknown = DOCTOR_PAYLOAD, extra = ''): string { - return [ - '# Live dogfood evidence', - '', - '```json', - JSON.stringify(manifest, null, 2), - '```', - '', - '## doctor --json payload', - '', - '```json', - JSON.stringify(doctor, null, 2), - '```', - extra, - ].join('\n'); +function artifact(version: string, channel: string, digestChar: string, kind: 'release-tarball' | 'delivery-evidence') { + return { + version, + channel, + platformId: PLATFORM, + platformTriple: 'darwin-arm64', + releaseTag: `v${version}`, + releaseName: `genie-${version}-${PLATFORM}.tar.gz`, + manifestSha256: HEX(digestChar), + artifactSha256: HEX(String.fromCharCode(digestChar.charCodeAt(0) + 1)), + binarySha256: HEX(String.fromCharCode(digestChar.charCodeAt(0) + 2)), + payloadSha256: HEX(String.fromCharCode(digestChar.charCodeAt(0) + 3)), + evidenceDigest: HEX(String.fromCharCode(digestChar.charCodeAt(0) + 4)), + provenance: provenance(kind, kind === 'delivery-evidence' ? HEX('a', 40) : HEX('b', 40)), + }; } -describe('validate-live-dogfood-evidence', () => { - test('a complete synthetic fixture passes', () => { - expect(validateLiveDogfoodEvidence(buildEvidence(validManifest()))).toEqual([]); - }); +function task(token: string, label: string) { + return { + wish: `dogfood-${label}-${token}`, + taskId: `t_${label}_${token}`, + title: `task-${label}-${token}`, + status: 'in_progress', + claimedBy: `worker-${label}`, + }; +} - test('rejects a nonempty file with no manifest block', () => { - const errors = validateLiveDogfoodEvidence('# just prose, no json\n\nlots of words but nothing structural.'); - expect(errors.some((e) => e.includes('no live-dogfood-evidence manifest'))).toBe(true); - }); +function repo(root: string, token: string, label: string, pid: number) { + const identity = task(token, label); + return { + root, + requestedCwd: root, + effectiveCwd: root, + cwdIdentity: `9:${pid}`, + childPid: pid, + sentinel: { token, expected: { ...identity }, observed: { ...identity }, boardCount: 1 }, + }; +} - test('rejects a wrong commit format', () => { - const manifest = validManifest(); - (manifest.candidate as Record).commit = 'ABCDEF'; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('candidate.commit'))).toBe(true); - }); +function stage(id: (typeof REQUIRED_STAGE_IDS)[number]) { + const candidateStages = REQUIRED_STAGE_IDS.slice(3); + const activeVersion = candidateStages.includes(id) ? T : N; + const states: Record = { + 'seed-repositories': [0, 'seeded', 'seeded', null], + 'n-parent-active': [0, 'current', 'current', null], + 't-delivery-repair': [ + 2, + 'activation-pending', + 'activation-pending', + { + schemaVersion: 1, + code: 'activation-pending', + deliveryComplete: true, + retry: false, + nextAction: 'retire tasks then setup and start a new task', + }, + ], + 'activation-consent': [0, 'activated', 'current', null], + 'assets-converged': [0, 'current', 'current', null], + 'untouched-b-before-init': [1, 'project-database-unavailable', 'project-database-unavailable', null], + 'untouched-b-after-init': [0, 'current', 'current', null], + 'new-thread-sentinel': [0, 'current', 'current', null], + 'doctor-current': [0, 'current', 'current', null], + }; + const [exit, humanState, jsonState, trailer] = states[id] as [number, string, string, unknown]; + const generation = id === 't-delivery-repair' || candidateStages.includes(id) ? 'candidate' : 'previous'; + const executable = `/tmp/dogfood/bin/${generation}`; + const native = ['untouched-b-before-init', 'untouched-b-after-init', 'new-thread-sentinel'].includes(id); + const argv = native ? ['mcpServer/tool/call', id] : [id]; + const command = { + executable, + executableSha256: generation === 'candidate' ? HEX('c') : HEX('7'), + candidateBinary: executable, + candidateBinarySha256: generation === 'candidate' ? HEX('c') : HEX('7'), + argv, + pid: 3000 + REQUIRED_STAGE_IDS.indexOf(id), + requestedCwd: '/tmp/dogfood/repo-a', + cwdIdentity: '9:2001', + exit, + stdout: native ? JSON.stringify({ schemaVersion: 1, kind: 'verified-local-fixture-direct-mcp', payload: {} }) : '', + stderr: '', + }; + const observation = { schemaVersion: 1, commands: [command] }; + return { + id, + command: [executable, ...argv].join(' '), + exit, + humanState, + jsonState, + activeVersion, + trailer, + observationPath: `observations/${id}.json`, + observationSha256: createHash('sha256') + .update(`${JSON.stringify(observation, null, 2)}\n`) + .digest('hex'), + observation, + }; +} - test('rejects a version outside the release grammar', () => { - const manifest = validManifest(); - (manifest.candidate as Record).version = '5.x.9'; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('candidate.version'))).toBe(true); - }); +function validManifest(): Record { + const previous = artifact(N, 'stable', '5', 'release-tarball'); + const candidate = artifact(T, 'stable', 'a', 'delivery-evidence'); + const a = repo('/tmp/dogfood/repo-a', HEX('7', 40), 'a', 2001); + const b = repo('/tmp/dogfood/repo-b', HEX('8', 40), 'b', 2002); + return { + kind: 'live-dogfood-evidence', + schemaVersion: LIVE_DOGFOOD_SCHEMA_VERSION, + entry: { + id: `${T}-${PLATFORM}`, + evidenceKind: 'verified-local-fixture', + availability: 'verified', + platformId: PLATFORM, + platformTriple: 'darwin-arm64', + artifactName: candidate.releaseName, + inputs: { + previous: { + artifact: `previous/${previous.releaseName}`, + manifest: 'previous/stable.json', + identity: `previous/${previous.releaseName}.intoto.jsonl`, + bundle: `previous/${previous.releaseName}.bundle`, + identityKind: 'slsa-provenance', + }, + candidate: { + artifact: `candidate/${candidate.releaseName}`, + manifest: 'candidate/stable.json', + identity: `candidate/${candidate.releaseName}.stable.delivery.json`, + bundle: `candidate/${candidate.releaseName}.stable.delivery.json.sigstore.json`, + identityKind: 'delivery-descriptor', + }, + }, + }, + lifecycle: { + previousVersion: N, + candidateVersion: T, + channel: 'stable', + sourceCommit: HEX('a', 40), + artifacts: { previous, candidate }, + delivery: { + schemaVersion: 2, + deliveryId: HEX('d', 32), + evidenceDigest: candidate.evidenceDigest, + root: '/tmp/dogfood/genie-home', + targetVersion: T, + platformId: PLATFORM, + platformTriple: 'darwin-arm64', + releaseTag: candidate.releaseTag, + releaseName: candidate.releaseName, + releaseManifestSha256: candidate.manifestSha256, + artifactSha256: candidate.artifactSha256, + installedBinarySha256: candidate.binarySha256, + canonicalPayloadSha256: candidate.payloadSha256, + }, + convergence: { + route: { state: 'managed-project', command: '/tmp/dogfood/genie-home/bin/genie', cwdOverride: null }, + roles: { expectedCount: 7, observedCount: 7, current: true, reviewerSha256: HEX('e') }, + }, + stages: REQUIRED_STAGE_IDS.map(stage), + }, + repositories: { + cacheRoot: '/tmp/dogfood/codex/plugins/cache/automagik/genie', + a, + b: { + root: b.root, + beforeInit: { + routeState: 'absent', + fallbackUsed: false, + result: 'project-database-unavailable', + returnedTasks: 0, + }, + afterInit: { ...b, routeState: 'managed-project' }, + }, + }, + }; +} - test('rejects a channel that is not homolog', () => { - const manifest = validManifest(); - (manifest.candidate as Record).channel = 'stable'; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes("channel must be 'homolog'"))).toBe(true); - }); +const DOCTOR = { + ok: true, + checks: [{ name: 'Codex plugin lifecycle', status: 'pass' }], + integrationSummary: { + schemaVersion: 1, + codexPlugin: { + state: 'current', + installedVersion: T, + targetVersion: T, + actionRequired: false, + deliveryComplete: true, + }, + }, +}; - test('rejects a non-inert query snapshot (before != after)', () => { - const manifest = validManifest(); - const inertness = manifest.inertness as Record; - inertness.genieDoctor.after.inventoryDigest = 'c'.repeat(64); - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('genieDoctor is not inert'))).toBe(true); - }); +function evidence(manifest = validManifest(), doctor: unknown = DOCTOR): string { + return `\`\`\`json\n${JSON.stringify(manifest, null, 2)}\n\`\`\`\n\n\`\`\`json\n${JSON.stringify( + doctor, + null, + 2, + )}\n\`\`\`\n`; +} - test('rejects an invalid inventory digest', () => { - const manifest = validManifest(); - const inertness = manifest.inertness as Record; - inertness.codexPluginList.before.inventoryDigest = 'short'; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('inventoryDigest must be 64'))).toBe(true); - }); +function errorsAfter(mutator: (manifest: Record) => void): string[] { + const manifest = validManifest(); + mutator(manifest); + return validateLiveDogfoodEvidence(evidence(manifest)); +} - test('rejects a missing ritual step', () => { - const manifest = validManifest(); - (manifest.steps as unknown[]).splice(2, 1); // drop n-resume-compact - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('exactly 7 ordered steps'))).toBe(true); - expect(errors.some((e) => e.includes('n-resume-compact'))).toBe(true); +describe('validate-live-dogfood-evidence schema v2', () => { + test('accepts the real nested doctor topology and complete bound entry', () => { + expect(validateLiveDogfoodEvidence(evidence())).toEqual([]); }); - test('rejects an out-of-order ritual step', () => { - const manifest = validManifest(); - const steps = manifest.steps as Array<{ id: string }>; - [steps[0], steps[1]] = [steps[1], steps[0]]; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes("must be 'n-task'"))).toBe(true); + test('rejects obsolete flat integrationSummary.state', () => { + const flat = { ok: true, checks: [{}], integrationSummary: { schemaVersion: 1, state: 'current' } }; + expect(validateLiveDogfoodEvidence(evidence(validManifest(), flat)).join('\n')).toContain( + 'integrationSummary.codexPlugin', + ); }); - test('rejects a bad exit code on the delivery step', () => { - const manifest = validManifest(); - const steps = manifest.steps as Array<{ id: string; exit: number }>; - (steps.find((s) => s.id === 'update-delivery-exit2') as { exit: number }).exit = 0; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('update-delivery-exit2.exit must be 2'))).toBe(true); + test('rejects absent, duplicate, and out-of-order stages', () => { + expect(errorsAfter((m) => m.lifecycle.stages.pop()).join('\n')).toContain('exactly 9 ordered stages'); + expect( + errorsAfter((m) => { + [m.lifecycle.stages[0], m.lifecycle.stages[1]] = [m.lifecycle.stages[1], m.lifecycle.stages[0]]; + }).join('\n'), + ).toContain('stages[0].id'); + expect(errorsAfter((m) => m.lifecycle.stages.push(m.lifecycle.stages[0])).join('\n')).toContain( + 'exactly 9 ordered stages', + ); }); - test('rejects a delivery step missing the deliveryComplete trailer field', () => { - const manifest = validManifest(); - const steps = manifest.steps as Array<{ id: string; output: string }>; - (steps.find((s) => s.id === 'update-delivery-exit2') as { output: string }).output = 'delivered'; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('"deliveryComplete":true'))).toBe(true); + test('rejects inconsistent human/json/trailer/exit and N retiring before consent', () => { + expect( + errorsAfter((m) => { + m.lifecycle.stages[2].exit = 0; + }).join('\n'), + ).toContain('.exit'); + expect( + errorsAfter((m) => { + m.lifecycle.stages[2].humanState = 'current'; + }).join('\n'), + ).toContain('humanState'); + expect( + errorsAfter((m) => { + m.lifecycle.stages[2].trailer.deliveryComplete = false; + }).join('\n'), + ).toContain('deliveryComplete'); + expect( + errorsAfter((m) => { + m.lifecycle.stages[2].activeVersion = T; + }).join('\n'), + ).toContain('activeVersion'); }); - test('rejects a non-integer exit on a step', () => { - const manifest = validManifest(); - (manifest.steps as Array<{ exit: unknown }>)[0].exit = '0'; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('.exit must be an integer'))).toBe(true); + test('rejects every tampered candidate delivery binding', () => { + for (const field of [ + 'evidenceDigest', + 'targetVersion', + 'platformId', + 'platformTriple', + 'releaseTag', + 'releaseName', + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'canonicalPayloadSha256', + ]) { + const errors = errorsAfter((m) => { + m.lifecycle.delivery[field] = field.endsWith('Sha256') ? HEX('0') : 'bad'; + }); + expect(errors.length, field).toBeGreaterThan(0); + } }); - test('rejects unparseable embedded doctor JSON', () => { - const evidence = [ - '```json', - JSON.stringify(validManifest(), null, 2), - '```', - '', - '```json', - '{ this is not valid json', - '```', - ].join('\n'); - const errors = validateLiveDogfoodEvidence(evidence); - expect(errors.some((e) => e.includes('no embedded doctor JSON block'))).toBe(true); + test('rejects unavailable matrix entry and malformed provenance', () => { + expect( + errorsAfter((m) => { + m.entry.availability = 'unavailable'; + }).join('\n'), + ).toContain('availability'); + expect( + errorsAfter((m) => { + m.lifecycle.artifacts.previous.provenance.controlCommit = 'bad'; + }).join('\n'), + ).toContain('controlCommit'); + expect( + errorsAfter((m) => { + m.lifecycle.artifacts.candidate.provenance.workflowIdentity = 'other'; + }).join('\n'), + ).toContain('workflowIdentity'); }); - test('rejects a doctor payload whose integrationSummary.state is not current', () => { - const errors = validateLiveDogfoodEvidence( - buildEvidence(validManifest(), { ...DOCTOR_PAYLOAD, integrationSummary: { state: 'pending' } }), - ); - expect(errors.some((e) => e.includes("state must be 'current'"))).toBe(true); + test('requires previous stable N to be older than candidate T', () => { + expect( + errorsAfter((m) => { + m.lifecycle.previousVersion = '5.260724.1'; + m.lifecycle.artifacts.previous.version = '5.260724.1'; + m.lifecycle.artifacts.previous.releaseTag = 'v5.260724.1'; + m.lifecycle.artifacts.previous.releaseName = 'genie-5.260724.1-darwin-arm64.tar.gz'; + }).join('\n'), + ).toContain('previous stable N must be older than candidate T'); }); - test('rejects more than one doctor payload block', () => { - const errors = validateLiveDogfoodEvidence( - buildEvidence(validManifest(), DOCTOR_PAYLOAD, `\n\`\`\`json\n${JSON.stringify(DOCTOR_PAYLOAD)}\n\`\`\`\n`), - ); - expect(errors.some((e) => e.includes('more than one embedded doctor JSON block'))).toBe(true); + test('rejects empty/cross-repo/cache-root/stale task and repeated child PID evidence', () => { + expect( + errorsAfter((m) => { + m.repositories.a.sentinel.boardCount = 0; + }).join('\n'), + ).toContain('boardCount'); + expect( + errorsAfter((m) => { + m.repositories.a.sentinel.observed = m.repositories.b.afterInit.sentinel.observed; + }).join('\n'), + ).toContain('exactly equal'); + expect( + errorsAfter((m) => { + m.repositories.a.effectiveCwd = m.repositories.cacheRoot; + }).join('\n'), + ).toContain('plugin cache'); + expect( + errorsAfter((m) => { + m.repositories.a.sentinel.observed.status = 'done'; + }).join('\n'), + ).toContain('in_progress'); + expect( + errorsAfter((m) => { + m.repositories.b.afterInit.childPid = m.repositories.a.childPid; + }).join('\n'), + ).toContain('distinct child'); }); - test('rejects a missing N non-guarantee statement', () => { - const manifest = validManifest(); - manifest.nNonGuarantee = ''; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.some((e) => e.includes('nNonGuarantee'))).toBe(true); + test('requires untouched B absent/no-fallback before init and managed after', () => { + expect( + errorsAfter((m) => { + m.repositories.b.beforeInit.fallbackUsed = true; + }).join('\n'), + ).toContain('fallbackUsed'); + expect( + errorsAfter((m) => { + m.repositories.b.afterInit.routeState = 'absent'; + }).join('\n'), + ).toContain('routeState'); }); +}); - test('reports multiple failures at once', () => { - const manifest = validManifest(); - (manifest.candidate as Record).commit = 'bad'; - (manifest.candidate as Record).channel = 'dev'; - manifest.nNonGuarantee = ''; - const errors = validateLiveDogfoodEvidence(buildEvidence(manifest)); - expect(errors.length).toBeGreaterThanOrEqual(3); +describe('referenced input verification', () => { + test('rehashes every staged input and rejects missing or changed bytes', () => { + const root = mkdtempSync(join(tmpdir(), 'dogfood-validator-')); + try { + const manifest = validManifest(); + for (const generation of ['previous', 'candidate']) { + mkdirSync(join(root, generation)); + const inputs = manifest.entry.inputs[generation]; + const artifact = manifest.lifecycle.artifacts[generation]; + const files = [ + ['artifact', 'artifactSha256'], + ['manifest', 'manifestSha256'], + ['identity', 'identitySha256'], + ['bundle', 'bundleSha256'], + ]; + for (const [inputKey, digestKey] of files) { + const bytes = `${generation}-${inputKey}`; + const path = join(root, inputs[inputKey]); + writeFileSync(path, bytes); + const digest = createHash('sha256').update(bytes).digest('hex'); + if (inputKey === 'identity' || inputKey === 'bundle') artifact.provenance[digestKey] = digest; + else artifact[digestKey] = digest; + } + } + const candidate = manifest.lifecycle.artifacts.candidate; + Object.assign(manifest.lifecycle.delivery, { + releaseManifestSha256: candidate.manifestSha256, + artifactSha256: candidate.artifactSha256, + installedBinarySha256: candidate.binarySha256, + canonicalPayloadSha256: candidate.payloadSha256, + }); + mkdirSync(join(root, 'observations')); + for (const stage of manifest.lifecycle.stages) { + writeFileSync(join(root, stage.observationPath), `${JSON.stringify(stage.observation, null, 2)}\n`); + } + const path = join(root, 'evidence.md'); + writeFileSync(path, evidence(manifest)); + expect(validateLiveDogfoodEvidenceFile(path, root)).toEqual([]); + writeFileSync(join(root, manifest.entry.inputs.candidate.artifact), 'tampered'); + expect(validateLiveDogfoodEvidenceFile(path, root).join('\n')).toContain('candidate artifact digest mismatch'); + rmSync(join(root, manifest.entry.inputs.previous.bundle)); + expect(validateLiveDogfoodEvidenceFile(path, root).join('\n')).toContain('previous bundle is unavailable'); + writeFileSync(join(root, manifest.lifecycle.stages[0].observationPath), '{"tampered":true}\n'); + expect(validateLiveDogfoodEvidenceFile(path, root).join('\n')).toContain('stage observation digest mismatch'); + expect(readFileSync(path, 'utf8')).toContain('"schemaVersion": 2'); + } finally { + rmSync(root, { recursive: true, force: true }); + } }); }); diff --git a/scripts/validate-live-dogfood-evidence.ts b/scripts/validate-live-dogfood-evidence.ts index 58526d9cb..1fecfcf63 100644 --- a/scripts/validate-live-dogfood-evidence.ts +++ b/scripts/validate-live-dogfood-evidence.ts @@ -1,247 +1,906 @@ #!/usr/bin/env bun /** - * Structural validator for the post-release live-dogfood evidence file - * (Group E deliverable 4). The post-release QA gate runs THIS validator, never a - * "file exists and is nonempty" check: a candidate is only proven when every - * field below is present and well-formed. + * Fail-closed validator for one reusable Codex dogfood matrix entry. * - * The evidence Markdown must embed exactly two kinds of fenced ```json blocks: - * - * 1. one MANIFEST block — an object `{ "kind": "live-dogfood-evidence", ... }`: - * { - * "kind": "live-dogfood-evidence", - * "schemaVersion": 1, - * "candidate": { "commit": "<40-hex>", "version": "", "channel": "homolog" }, - * "inertness": { - * "codexPluginList": { "before": , "after": }, - * "genieDoctor": { "before": , "after": } - * }, - * "steps": [ { "id": "...", "command": "...", "exit": , "output": "..." }, ... ], - * "nNonGuarantee": "" - * } - * where = { "nIdentity": "...", "tIdentity": "...", "inventoryDigest": "<64-hex>" }; - * before/after MUST be identical for each inertness pair (the query is inert). - * `steps` MUST carry, in this order, every ritual step id: - * n-task, update-delivery-exit2 (exit 2, output carries "deliveryComplete":true), - * n-resume-compact, external-setup-activation, doctor-json, hooks-review, new-n1-task. - * - * 2. exactly one DOCTOR block — the `genie doctor --json` payload; it is THE - * validated payload and must parse with `integrationSummary.state === 'current'`. - * - * The validator collects EVERY missing or invalid field, prints one per line, and - * exits nonzero. An empty error list means the evidence is structurally sound. + * Schema v2 binds a real N -> T lifecycle to the exact release inputs, delivery + * record, two seeded repositories, child CWD identities, ordered command + * stages, and the production doctor JSON topology. The manifest and doctor + * payload are separate fenced JSON blocks in the evidence Markdown. */ -import { existsSync, readFileSync } from 'node:fs'; +import { createHash } from 'node:crypto'; +import { existsSync, lstatSync, readFileSync } from 'node:fs'; +import { dirname, isAbsolute, resolve } from 'node:path'; +import { compareReleaseVersions, parseReleaseVersion } from '../src/lib/codex-release-version.ts'; + +export const LIVE_DOGFOOD_SCHEMA_VERSION = 2 as const; +export const REQUIRED_STAGE_IDS = [ + 'seed-repositories', + 'n-parent-active', + 't-delivery-repair', + 'activation-consent', + 'assets-converged', + 'untouched-b-before-init', + 'untouched-b-after-init', + 'new-thread-sentinel', + 'doctor-current', +] as const; const RELEASE_VERSION = /^\d+\.\d{6}\.\d+$/; const COMMIT_SHA = /^[0-9a-f]{40}$/; -const INVENTORY_DIGEST = /^[0-9a-f]{64}$/; - -export const REQUIRED_STEP_IDS = [ - 'n-task', - 'update-delivery-exit2', - 'n-resume-compact', - 'external-setup-activation', - 'doctor-json', - 'hooks-review', - 'new-n1-task', -] as const; +const SHA256 = /^[0-9a-f]{64}$/; +const DELIVERY_ID = /^[0-9a-f]{32}$/; +const DIRECTORY_IDENTITY = /^\d+:\d+$/; +const SENTINEL_TOKEN = /^[0-9a-f]{32,}$/; +const PLATFORM_TRIPLES = { + 'linux-x64-glibc': 'linux-x64', + 'linux-x64-musl': 'linux-x64', + 'linux-arm64': 'linux-arm64', + 'darwin-arm64': 'darwin-arm64', +} as const; +const REPOSITORY = 'automagik-dev/genie'; +const PREDICATE_TYPE = 'https://github.com/automagik-dev/genie/delivery-evidence/v1'; +const WORKFLOW_IDENTITY = + 'https://github.com/automagik-dev/genie/.github/workflows/release-publish.yml@refs/heads/main'; +const OIDC_ISSUER = 'https://token.actions.githubusercontent.com'; +const LEGACY_PREDICATE_TYPE = 'https://slsa.dev/provenance/v0.2'; +const LEGACY_WORKFLOW_IDENTITY = + 'https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@refs/tags/v2.1.0'; +type JsonRecord = Record; interface JsonBlock { - value: Record; + value: JsonRecord; } -function isRecord(value: unknown): value is Record { +function isRecord(value: unknown): value is JsonRecord { return typeof value === 'object' && value !== null && !Array.isArray(value); } -/** Extract every fenced ```json block as a parsed object; unparseable blocks become null. */ function extractJsonBlocks(markdown: string): Array { const blocks: Array = []; const pattern = /```json\s*\n([\s\S]*?)\n```/g; - let match: RegExpExecArray | null = pattern.exec(markdown); - while (match !== null) { + for (let match = pattern.exec(markdown); match !== null; match = pattern.exec(markdown)) { try { const parsed: unknown = JSON.parse(match[1]); blocks.push(isRecord(parsed) ? { value: parsed } : null); } catch { blocks.push(null); } - match = pattern.exec(markdown); } return blocks; } -function validateSnapshot(errors: string[], label: string, snapshot: unknown): void { - if (!isRecord(snapshot)) { - errors.push(`inertness.${label} is not an object`); - return; +function record(errors: string[], label: string, value: unknown): JsonRecord | null { + if (isRecord(value)) return value; + errors.push(`${label} must be an object`); + return null; +} + +function nonempty(errors: string[], label: string, value: unknown): value is string { + if (typeof value === 'string' && value.trim() !== '') return true; + errors.push(`${label} must be a non-empty string`); + return false; +} + +function matches( + errors: string[], + label: string, + value: unknown, + pattern: RegExp, + expectation: string, +): value is string { + if (typeof value === 'string' && pattern.test(value)) return true; + errors.push(`${label} must be ${expectation}`); + return false; +} + +function absolute(errors: string[], label: string, value: unknown): value is string { + if (typeof value === 'string' && isAbsolute(value) && !value.includes('\0')) return true; + errors.push(`${label} must be an absolute path`); + return false; +} + +function portableInput(errors: string[], label: string, value: unknown, generation: string): value is string { + if ( + typeof value === 'string' && + value.startsWith(`${generation}/`) && + !isAbsolute(value) && + !value.includes('\\') && + !value.split('/').includes('..') && + value.split('/').length === 2 && + value.split('/')[1] !== '' + ) { + return true; + } + errors.push(`${label} must be a portable ${generation}/ reference`); + return false; +} + +function portableObservation(errors: string[], label: string, value: unknown, stageId: string): value is string { + if (value === `observations/${stageId}.json`) return true; + errors.push(`${label} must be observations/${stageId}.json`); + return false; +} + +function same(errors: string[], label: string, actual: unknown, expected: unknown): void { + if (actual !== expected) + errors.push(`${label} must equal ${JSON.stringify(expected)} (got ${JSON.stringify(actual)})`); +} + +function validateEntry(errors: string[], value: unknown): JsonRecord | null { + const entry = record(errors, 'manifest.entry', value); + if (entry === null) return null; + nonempty(errors, 'entry.id', entry.id); + if (!['host-native', 'verified-local-fixture'].includes(String(entry.evidenceKind))) { + errors.push("entry.evidenceKind must be 'host-native' or 'verified-local-fixture'"); + } + same(errors, 'entry.availability', entry.availability, 'verified'); + const platformId = entry.platformId; + if (typeof platformId !== 'string' || !(platformId in PLATFORM_TRIPLES)) { + errors.push('entry.platformId must name a supported release platform'); + } else { + same( + errors, + 'entry.platformTriple', + entry.platformTriple, + PLATFORM_TRIPLES[platformId as keyof typeof PLATFORM_TRIPLES], + ); } - if (typeof snapshot.nIdentity !== 'string' || snapshot.nIdentity.trim() === '') { - errors.push(`inertness.${label}.nIdentity must be a non-empty string`); + nonempty(errors, 'entry.artifactName', entry.artifactName); + const inputs = record(errors, 'entry.inputs', entry.inputs); + for (const generation of ['previous', 'candidate'] as const) { + const input = record(errors, `entry.inputs.${generation}`, inputs?.[generation]); + same( + errors, + `entry.inputs.${generation}.identityKind`, + input?.identityKind, + generation === 'previous' ? 'slsa-provenance' : 'delivery-descriptor', + ); + for (const key of ['artifact', 'manifest', 'identity', 'bundle']) { + portableInput(errors, `entry.inputs.${generation}.${key}`, input?.[key], generation); + } + } + return entry; +} + +function validateProvenance(errors: string[], label: string, value: unknown, commit: unknown, channel: unknown): void { + const provenance = record(errors, label, value); + if (provenance === null) return; + same(errors, `${label}.repository`, provenance.repository, REPOSITORY); + if (provenance.kind === 'release-tarball') { + same(errors, `${label}.predicateType`, provenance.predicateType, LEGACY_PREDICATE_TYPE); + same(errors, `${label}.workflowIdentity`, provenance.workflowIdentity, LEGACY_WORKFLOW_IDENTITY); + } else if (provenance.kind === 'delivery-evidence') { + same(errors, `${label}.predicateType`, provenance.predicateType, PREDICATE_TYPE); + same(errors, `${label}.workflowIdentity`, provenance.workflowIdentity, WORKFLOW_IDENTITY); + } else { + errors.push(`${label}.kind must be 'release-tarball' or 'delivery-evidence'`); + } + same(errors, `${label}.oidcIssuer`, provenance.oidcIssuer, OIDC_ISSUER); + if (commit === undefined) { + matches( + errors, + `${label}.sourceCommit`, + provenance.sourceCommit, + COMMIT_SHA, + 'a 40-character lowercase commit sha', + ); + } else { + same(errors, `${label}.sourceCommit`, provenance.sourceCommit, commit); } - if (typeof snapshot.tIdentity !== 'string' || snapshot.tIdentity.trim() === '') { - errors.push(`inertness.${label}.tIdentity must be a non-empty string`); + matches( + errors, + `${label}.controlCommit`, + provenance.controlCommit, + COMMIT_SHA, + 'a 40-character lowercase commit sha', + ); + if (channel === 'stable') same(errors, `${label}.sourceBranch`, provenance.sourceBranch, 'main'); + else same(errors, `${label}.sourceBranch`, provenance.sourceBranch, channel); + if (typeof provenance.sourceCiRunId !== 'string' || !/^(?:0|[1-9]\d*)$/.test(provenance.sourceCiRunId)) { + errors.push(`${label}.sourceCiRunId must be an unsigned decimal string`); } - if (typeof snapshot.inventoryDigest !== 'string' || !INVENTORY_DIGEST.test(snapshot.inventoryDigest)) { - errors.push(`inertness.${label}.inventoryDigest must be 64 lowercase hex characters`); + for (const key of ['identitySha256', 'bundleSha256']) { + matches(errors, `${label}.${key}`, provenance[key], SHA256, '64 lowercase hex characters'); } } -function validateInertnessPair(errors: string[], key: string, pair: unknown): void { - if (!isRecord(pair) || !('before' in pair) || !('after' in pair)) { - errors.push(`inertness.${key} must carry before and after snapshots`); - return; +function validateArtifact( + errors: string[], + label: string, + value: unknown, + expectedVersion: unknown, + entry: JsonRecord | null, + commit: unknown, + channel: unknown, +): JsonRecord | null { + const artifact = record(errors, label, value); + if (artifact === null) return null; + same(errors, `${label}.version`, artifact.version, expectedVersion); + if (channel === undefined) { + if (!['stable', 'homolog', 'dev'].includes(String(artifact.channel))) { + errors.push(`${label}.channel must be stable, homolog, or dev`); + } + } else { + same(errors, `${label}.channel`, artifact.channel, channel); } - validateSnapshot(errors, `${key}.before`, pair.before); - validateSnapshot(errors, `${key}.after`, pair.after); - if (JSON.stringify(pair.before) !== JSON.stringify(pair.after)) { - errors.push(`inertness.${key} is not inert: the query changed N/T identity or inventory digest`); + same(errors, `${label}.platformId`, artifact.platformId, entry?.platformId); + same(errors, `${label}.platformTriple`, artifact.platformTriple, entry?.platformTriple); + same(errors, `${label}.releaseTag`, artifact.releaseTag, `v${String(expectedVersion)}`); + same( + errors, + `${label}.releaseName`, + artifact.releaseName, + `genie-${String(expectedVersion)}-${String(entry?.platformId)}.tar.gz`, + ); + for (const key of ['manifestSha256', 'artifactSha256', 'binarySha256', 'payloadSha256', 'evidenceDigest']) { + matches(errors, `${label}.${key}`, artifact[key], SHA256, '64 lowercase hex characters'); } + validateProvenance(errors, `${label}.provenance`, artifact.provenance, commit, artifact.channel); + return artifact; } -function validateInertness(errors: string[], inertness: unknown): void { - if (!isRecord(inertness)) { - errors.push('manifest.inertness is missing or not an object'); - return; +function validateDelivery( + errors: string[], + value: unknown, + lifecycle: JsonRecord, + entry: JsonRecord | null, + candidate: JsonRecord | null, +): void { + const delivery = record(errors, 'lifecycle.delivery', value); + if (delivery === null) return; + same(errors, 'delivery.schemaVersion', delivery.schemaVersion, 2); + matches(errors, 'delivery.deliveryId', delivery.deliveryId, DELIVERY_ID, '32 lowercase hex characters'); + matches(errors, 'delivery.evidenceDigest', delivery.evidenceDigest, SHA256, '64 lowercase hex characters'); + absolute(errors, 'delivery.root', delivery.root); + if (typeof delivery.root === 'string' && /[/\\]plugins[/\\]cache(?:[/\\]|$)/.test(delivery.root)) { + errors.push('delivery.root must not be a plugin cache root'); } - for (const key of ['codexPluginList', 'genieDoctor']) { - if (!(key in inertness)) { - errors.push(`inertness.${key} is missing`); - continue; - } - validateInertnessPair(errors, key, inertness[key]); + same(errors, 'delivery.targetVersion', delivery.targetVersion, lifecycle.candidateVersion); + same(errors, 'delivery.platformId', delivery.platformId, entry?.platformId); + same(errors, 'delivery.platformTriple', delivery.platformTriple, entry?.platformTriple); + same(errors, 'delivery.releaseTag', delivery.releaseTag, candidate?.releaseTag); + same(errors, 'delivery.releaseName', delivery.releaseName, candidate?.releaseName); + same(errors, 'delivery.releaseManifestSha256', delivery.releaseManifestSha256, candidate?.manifestSha256); + same(errors, 'delivery.artifactSha256', delivery.artifactSha256, candidate?.artifactSha256); + same(errors, 'delivery.installedBinarySha256', delivery.installedBinarySha256, candidate?.binarySha256); + same(errors, 'delivery.canonicalPayloadSha256', delivery.canonicalPayloadSha256, candidate?.payloadSha256); + same(errors, 'delivery.evidenceDigest binding', delivery.evidenceDigest, candidate?.evidenceDigest); +} + +function validateConvergence(errors: string[], value: unknown): void { + const convergence = record(errors, 'lifecycle.convergence', value); + if (convergence === null) return; + const route = record(errors, 'lifecycle.convergence.route', convergence.route); + same(errors, 'convergence.route.state', route?.state, 'managed-project'); + absolute(errors, 'convergence.route.command', route?.command); + same(errors, 'convergence.route.cwdOverride', route?.cwdOverride, null); + if (typeof route?.command === 'string' && /[/\\]plugins[/\\]cache(?:[/\\]|$)/.test(route.command)) { + errors.push('convergence.route.command must use the stable facade, not a plugin cache path'); + } + const roles = record(errors, 'lifecycle.convergence.roles', convergence.roles); + const expected = roles?.expectedCount; + if (typeof expected !== 'number' || !Number.isInteger(expected) || expected <= 0) { + errors.push('convergence.roles.expectedCount must be a positive integer'); } + same(errors, 'convergence.roles.observedCount', roles?.observedCount, expected); + same(errors, 'convergence.roles.current', roles?.current, true); + matches(errors, 'convergence.roles.reviewerSha256', roles?.reviewerSha256, SHA256, '64 lowercase hex characters'); } -function validateCandidate(errors: string[], candidate: unknown): void { - if (!isRecord(candidate)) { - errors.push('manifest.candidate is missing or not an object'); - return; +function validateTaskSentinel(errors: string[], label: string, value: unknown): JsonRecord | null { + const sentinel = record(errors, label, value); + if (sentinel === null) return null; + matches(errors, `${label}.token`, sentinel.token, SENTINEL_TOKEN, 'at least 128 bits of lowercase hex'); + const expected = record(errors, `${label}.expected`, sentinel.expected); + const observed = record(errors, `${label}.observed`, sentinel.observed); + if (expected !== null && observed !== null && JSON.stringify(expected) !== JSON.stringify(observed)) { + errors.push(`${label}.observed must exactly equal the seeded expected task identity`); } - if (typeof candidate.commit !== 'string' || !COMMIT_SHA.test(candidate.commit)) { - errors.push('candidate.commit must be a 40-character lowercase hex commit sha'); + for (const [side, task] of [ + ['expected', expected], + ['observed', observed], + ] as const) { + nonempty(errors, `${label}.${side}.wish`, task?.wish); + nonempty(errors, `${label}.${side}.taskId`, task?.taskId); + nonempty(errors, `${label}.${side}.title`, task?.title); + nonempty(errors, `${label}.${side}.claimedBy`, task?.claimedBy); + same(errors, `${label}.${side}.status`, task?.status, 'in_progress'); + if (typeof sentinel.token === 'string') { + if (typeof task?.wish !== 'string' || !task.wish.includes(sentinel.token)) { + errors.push(`${label}.${side}.wish must contain the unpredictable sentinel token`); + } + if (typeof task?.title !== 'string' || !task.title.includes(sentinel.token)) { + errors.push(`${label}.${side}.title must contain the unpredictable sentinel token`); + } + } } - if (typeof candidate.version !== 'string' || !RELEASE_VERSION.test(candidate.version)) { - errors.push('candidate.version must match the release grammar MAJOR.YYMMDD.PATCH'); + same(errors, `${label}.boardCount`, sentinel.boardCount, 1); + return sentinel; +} + +function validateRepoObservation( + errors: string[], + label: string, + value: unknown, + cacheRoot: unknown, +): JsonRecord | null { + const repo = record(errors, label, value); + if (repo === null) return null; + absolute(errors, `${label}.root`, repo.root); + same(errors, `${label}.requestedCwd`, repo.requestedCwd, repo.root); + same(errors, `${label}.effectiveCwd`, repo.effectiveCwd, repo.root); + matches( + errors, + `${label}.cwdIdentity`, + repo.cwdIdentity, + DIRECTORY_IDENTITY, + 'an OS directory identity in dev:ino form', + ); + if (typeof repo.childPid !== 'number' || !Number.isInteger(repo.childPid) || repo.childPid <= 0) { + errors.push(`${label}.childPid must be a positive integer`); } - if (candidate.channel !== 'homolog') { - errors.push("candidate.channel must be 'homolog' (the canonical pre-stable candidate channel)"); + if ( + repo.effectiveCwd === cacheRoot || + (typeof repo.effectiveCwd === 'string' && /[/\\]plugins[/\\]cache/.test(repo.effectiveCwd)) + ) { + errors.push(`${label}.effectiveCwd must not be the plugin cache root`); } + validateTaskSentinel(errors, `${label}.sentinel`, repo.sentinel); + return repo; } -function validateStep(errors: string[], index: number, expectedId: string, step: unknown): void { - if (!isRecord(step)) { - errors.push(`steps[${index}] (${expectedId}) is not an object`); - return; +function validateRepositories(errors: string[], value: unknown): void { + const repositories = record(errors, 'manifest.repositories', value); + if (repositories === null) return; + absolute(errors, 'repositories.cacheRoot', repositories.cacheRoot); + const repoA = validateRepoObservation(errors, 'repositories.a', repositories.a, repositories.cacheRoot); + const repoB = record(errors, 'repositories.b', repositories.b); + if (repoB === null) return; + absolute(errors, 'repositories.b.root', repoB.root); + const before = record(errors, 'repositories.b.beforeInit', repoB.beforeInit); + same(errors, 'repositories.b.beforeInit.routeState', before?.routeState, 'absent'); + same(errors, 'repositories.b.beforeInit.fallbackUsed', before?.fallbackUsed, false); + same(errors, 'repositories.b.beforeInit.result', before?.result, 'project-database-unavailable'); + same(errors, 'repositories.b.beforeInit.returnedTasks', before?.returnedTasks, 0); + const after = validateRepoObservation(errors, 'repositories.b.afterInit', repoB.afterInit, repositories.cacheRoot); + same(errors, 'repositories.b.afterInit.routeState', after?.routeState, 'managed-project'); + if (repoA?.root === repoB.root) errors.push('repositories A and B must be different roots'); + const aToken = isRecord(repoA?.sentinel) ? repoA.sentinel.token : null; + const bToken = isRecord(after?.sentinel) ? after.sentinel.token : null; + if (aToken === bToken) errors.push('repositories A and B must use different unpredictable sentinels'); + if (repoA?.childPid === after?.childPid) errors.push('repositories A and B must record distinct child processes'); +} + +interface StageExpectation { + exit: number; + human: string; + json: string; + generation: 'previous' | 'candidate'; + binary: 'previous' | 'candidate'; + trailer: 'activation-pending' | null; +} + +const STAGE_EXPECTATIONS: Record<(typeof REQUIRED_STAGE_IDS)[number], StageExpectation> = { + 'seed-repositories': { + exit: 0, + human: 'seeded', + json: 'seeded', + generation: 'previous', + binary: 'previous', + trailer: null, + }, + 'n-parent-active': { + exit: 0, + human: 'current', + json: 'current', + generation: 'previous', + binary: 'previous', + trailer: null, + }, + 't-delivery-repair': { + exit: 2, + human: 'activation-pending', + json: 'activation-pending', + generation: 'previous', + binary: 'candidate', + trailer: 'activation-pending', + }, + 'activation-consent': { + exit: 0, + human: 'activated', + json: 'current', + generation: 'candidate', + binary: 'candidate', + trailer: null, + }, + 'assets-converged': { + exit: 0, + human: 'current', + json: 'current', + generation: 'candidate', + binary: 'candidate', + trailer: null, + }, + 'untouched-b-before-init': { + exit: 1, + human: 'project-database-unavailable', + json: 'project-database-unavailable', + generation: 'candidate', + binary: 'candidate', + trailer: null, + }, + 'untouched-b-after-init': { + exit: 0, + human: 'current', + json: 'current', + generation: 'candidate', + binary: 'candidate', + trailer: null, + }, + 'new-thread-sentinel': { + exit: 0, + human: 'current', + json: 'current', + generation: 'candidate', + binary: 'candidate', + trailer: null, + }, + 'doctor-current': { + exit: 0, + human: 'current', + json: 'current', + generation: 'candidate', + binary: 'candidate', + trailer: null, + }, +}; + +function validateCapturedCommand( + errors: string[], + label: string, + value: unknown, +): { record: JsonRecord; summary: string; exit: number | null; candidateSha256: unknown } | null { + const command = record(errors, label, value); + if (command === null) return null; + absolute(errors, `${label}.executable`, command.executable); + matches(errors, `${label}.executableSha256`, command.executableSha256, SHA256, '64 lowercase hex characters'); + const candidateIsNull = command.candidateBinary === null && command.candidateBinarySha256 === null; + if (!candidateIsNull) { + absolute(errors, `${label}.candidateBinary`, command.candidateBinary); + matches( + errors, + `${label}.candidateBinarySha256`, + command.candidateBinarySha256, + SHA256, + '64 lowercase hex characters', + ); } - if (step.id !== expectedId) { - errors.push(`steps[${index}] must be '${expectedId}' (got ${JSON.stringify(step.id)}) — ritual order is fixed`); + if (!Array.isArray(command.argv) || command.argv.some((arg) => typeof arg !== 'string')) { + errors.push(`${label}.argv must be a string array`); } - if (typeof step.command !== 'string' || step.command.trim() === '') { - errors.push(`steps[${index}] (${expectedId}).command must be a non-empty string`); + if (typeof command.pid !== 'number' || !Number.isInteger(command.pid) || command.pid <= 0) { + errors.push(`${label}.pid must be a positive integer`); } - if (typeof step.exit !== 'number' || !Number.isInteger(step.exit)) { - errors.push(`steps[${index}] (${expectedId}).exit must be an integer exit code`); + absolute(errors, `${label}.requestedCwd`, command.requestedCwd); + matches(errors, `${label}.cwdIdentity`, command.cwdIdentity, DIRECTORY_IDENTITY, 'an OS directory identity'); + if (typeof command.exit !== 'number' || !Number.isInteger(command.exit)) { + errors.push(`${label}.exit must be an integer`); } - if (typeof step.output !== 'string' || step.output.trim() === '') { - errors.push(`steps[${index}] (${expectedId}).output must be a non-empty captured output`); + if (typeof command.stdout !== 'string') errors.push(`${label}.stdout must be a string`); + if (typeof command.stderr !== 'string') errors.push(`${label}.stderr must be a string`); + return { + record: command, + summary: [String(command.executable), ...(Array.isArray(command.argv) ? command.argv.map(String) : [])].join(' '), + exit: typeof command.exit === 'number' && Number.isInteger(command.exit) ? command.exit : null, + candidateSha256: command.candidateBinarySha256, + }; +} + +const NATIVE_MCP_STAGES = new Set<(typeof REQUIRED_STAGE_IDS)[number]>([ + 'untouched-b-before-init', + 'untouched-b-after-init', + 'new-thread-sentinel', +]); + +function validateHostNativeRaw( + errors: string[], + label: string, + raw: JsonRecord, + command: JsonRecord, + expectError: boolean, +): void { + same(errors, `${label}.schemaVersion`, raw.schemaVersion, 1); + const codex = record(errors, `${label}.codex`, raw.codex); + absolute(errors, `${label}.codex.executable`, codex?.executable); + nonempty(errors, `${label}.codex.version`, codex?.version); + if (typeof codex?.appServerPid !== 'number' || !Number.isInteger(codex.appServerPid) || codex.appServerPid <= 0) { + errors.push(`${label}.codex.appServerPid must be a positive integer`); } - if (expectedId === 'update-delivery-exit2') { - if (step.exit !== 2) errors.push('steps update-delivery-exit2.exit must be 2 (delivered, action-required)'); - if (typeof step.output !== 'string' || !step.output.includes('"deliveryComplete":true')) { - errors.push('steps update-delivery-exit2.output must carry the trailer field "deliveryComplete":true'); - } + const candidate = record(errors, `${label}.candidate`, raw.candidate); + same(errors, `${label}.candidate.executable`, candidate?.executable, command.candidateBinary); + same(errors, `${label}.candidate execution`, candidate?.adapter ?? candidate?.executable, command.executable); + same(errors, `${label}.rawRequestedCwd`, raw.rawRequestedCwd, command.requestedCwd); + nonempty(errors, `${label}.threadId`, raw.threadId); + const launcher = record(errors, `${label}.launcher`, raw.launcher); + same(errors, `${label}.launcher.pid`, launcher?.pid, command.pid); + same(errors, `${label}.launcher.effectiveCwd`, launcher?.effectiveCwd, command.requestedCwd); + same(errors, `${label}.launcher.cwdIdentity`, launcher?.cwdIdentity, command.cwdIdentity); + same(errors, `${label}.launcher.candidate`, launcher?.candidate, command.candidateBinary); + same(errors, `${label}.launcher.adapter`, launcher?.adapter, candidate?.adapter); + const control = record(errors, `${label}.control`, raw.control); + same(errors, `${label}.control.effectiveCwd`, control?.effectiveCwd, launcher?.effectiveCwd); + same(errors, `${label}.control.cwdIdentity`, control?.cwdIdentity, launcher?.cwdIdentity); + const server = record(errors, `${label}.mcpServer`, raw.mcpServer); + same(errors, `${label}.mcpServer.name`, server?.name, 'genie'); + if (!Array.isArray(server?.tools) || !server.tools.includes('genie_board')) { + errors.push(`${label}.mcpServer.tools must include genie_board`); } + const response = record(errors, `${label}.toolResponse`, raw.toolResponse); + same(errors, `${label}.toolResponse.isError`, response?.isError === true, expectError); + const outcome = record(errors, `${label}.outcome`, raw.outcome); + same(errors, `${label}.outcome.kind`, outcome?.kind, expectError ? 'expected-error' : 'sentinel'); } -function validateSteps(errors: string[], steps: unknown): void { - if (!Array.isArray(steps)) { - errors.push('manifest.steps must be an array of ordered ritual steps'); +function validateNativeObservation( + errors: string[], + id: (typeof REQUIRED_STAGE_IDS)[number], + index: number, + evidenceKind: unknown, + commands: Array<{ record: JsonRecord }>, +): void { + if (!NATIVE_MCP_STAGES.has(id)) return; + const label = `stages[${index}].nativeMcp`; + const command = commands.find( + (value) => Array.isArray(value.record.argv) && value.record.argv.includes('mcpServer/tool/call'), + )?.record; + if (command === undefined || typeof command.stdout !== 'string') { + errors.push(`${label} must contain a raw MCP command observation`); return; } - if (steps.length !== REQUIRED_STEP_IDS.length) { - errors.push(`manifest.steps must have exactly ${REQUIRED_STEP_IDS.length} ordered steps (got ${steps.length})`); + let raw: unknown; + try { + raw = JSON.parse(command.stdout); + } catch { + errors.push(`${label} stdout must be JSON`); + return; } - REQUIRED_STEP_IDS.forEach((expectedId, index) => { - validateStep(errors, index, expectedId, steps[index]); - }); + const recordValue = record(errors, label, raw); + if (recordValue === null) return; + if (evidenceKind === 'verified-local-fixture') { + same(errors, `${label}.schemaVersion`, recordValue.schemaVersion, 1); + same(errors, `${label}.kind`, recordValue.kind, 'verified-local-fixture-direct-mcp'); + return; + } + if (evidenceKind !== 'host-native') { + errors.push(`${label} has an unsupported evidence kind`); + return; + } + validateHostNativeRaw(errors, label, recordValue, command, id === 'untouched-b-before-init'); } -function validateDoctorPayload(errors: string[], blocks: Array): void { - const doctorBlocks = blocks.filter((block) => block !== null && isRecord(block.value.integrationSummary)); - if (doctorBlocks.length === 0) { - // A json block that fails to parse OR lacks integrationSummary both surface here. - errors.push('no embedded doctor JSON block with an integrationSummary object was found (must be exactly one)'); +function validateStageObservation( + errors: string[], + stage: JsonRecord, + index: number, + id: (typeof REQUIRED_STAGE_IDS)[number], + expectedExit: number, + expectedBinarySha256: unknown, + evidenceKind: unknown, +): void { + portableObservation(errors, `stages[${index}].observationPath`, stage.observationPath, id); + matches(errors, `stages[${index}].observationSha256`, stage.observationSha256, SHA256, '64 lowercase hex characters'); + const observation = record(errors, `stages[${index}].observation`, stage.observation); + same(errors, `stages[${index}].observation.schemaVersion`, observation?.schemaVersion, 1); + if (!Array.isArray(observation?.commands) || observation.commands.length === 0) { + errors.push(`stages[${index}].observation.commands must be a non-empty array`); return; } - if (doctorBlocks.length > 1) { - errors.push('more than one embedded doctor JSON block was found (must be exactly one validated payload)'); + const commands = observation.commands + .map((value, commandIndex) => + validateCapturedCommand(errors, `stages[${index}].observation.commands[${commandIndex}]`, value), + ) + .filter((value) => value !== null); + same( + errors, + `stages[${index}].command projection`, + stage.command, + commands.map((value) => value.summary).join(' && '), + ); + if (!commands.some((command) => command.exit === expectedExit)) { + errors.push(`stages[${index}].observation must contain the projected exit ${expectedExit}`); + } + if (!commands.some((command) => command.candidateSha256 === expectedBinarySha256)) { + errors.push(`stages[${index}].observation must bind the authenticated generation binary digest`); + } + validateNativeObservation(errors, id, index, evidenceKind, commands); + const serialized = `${JSON.stringify(observation, null, 2)}\n`; + const observedDigest = createHash('sha256').update(serialized).digest('hex'); + same(errors, `stages[${index}].observation digest`, stage.observationSha256, observedDigest); +} + +function validateStages( + errors: string[], + value: unknown, + previousVersion: unknown, + candidateVersion: unknown, + previousBinarySha256: unknown, + candidateBinarySha256: unknown, + evidenceKind: unknown, +): void { + if (!Array.isArray(value)) { + errors.push('manifest.stages must be an array'); return; } - const summary = (doctorBlocks[0] as JsonBlock).value.integrationSummary as Record; - if (summary.state !== 'current') { - errors.push( - `embedded doctor JSON integrationSummary.state must be 'current' (got ${JSON.stringify(summary.state)})`, + if (value.length !== REQUIRED_STAGE_IDS.length) { + errors.push(`manifest.stages must have exactly ${REQUIRED_STAGE_IDS.length} ordered stages (got ${value.length})`); + } + REQUIRED_STAGE_IDS.forEach((id, index) => { + const stage = record(errors, `stages[${index}] (${id})`, value[index]); + if (stage === null) return; + same(errors, `stages[${index}].id`, stage.id, id); + nonempty(errors, `stages[${index}].command`, stage.command); + const expected = STAGE_EXPECTATIONS[id]; + same(errors, `stages[${index}] (${id}).exit`, stage.exit, expected.exit); + same(errors, `stages[${index}] (${id}).humanState`, stage.humanState, expected.human); + same(errors, `stages[${index}] (${id}).jsonState`, stage.jsonState, expected.json); + same( + errors, + `stages[${index}] (${id}).activeVersion`, + stage.activeVersion, + expected.generation === 'previous' ? previousVersion : candidateVersion, ); + validateStageObservation( + errors, + stage, + index, + id, + expected.exit, + expected.binary === 'previous' ? previousBinarySha256 : candidateBinarySha256, + evidenceKind, + ); + if (expected.trailer === null) { + same(errors, `stages[${index}] (${id}).trailer`, stage.trailer, null); + return; + } + const trailer = record(errors, `stages[${index}] (${id}).trailer`, stage.trailer); + same(errors, `stages[${index}] (${id}).trailer.schemaVersion`, trailer?.schemaVersion, 1); + same(errors, `stages[${index}] (${id}).trailer.code`, trailer?.code, expected.trailer); + same(errors, `stages[${index}] (${id}).trailer.deliveryComplete`, trailer?.deliveryComplete, true); + same(errors, `stages[${index}] (${id}).trailer.retry`, trailer?.retry, false); + nonempty(errors, `stages[${index}] (${id}).trailer.nextAction`, trailer?.nextAction); + }); +} + +function validateLifecycle(errors: string[], value: unknown, entry: JsonRecord | null): JsonRecord | null { + const lifecycle = record(errors, 'manifest.lifecycle', value); + if (lifecycle === null) return null; + matches( + errors, + 'lifecycle.previousVersion', + lifecycle.previousVersion, + RELEASE_VERSION, + 'a release version MAJOR.YYMMDD.PATCH', + ); + matches( + errors, + 'lifecycle.candidateVersion', + lifecycle.candidateVersion, + RELEASE_VERSION, + 'a release version MAJOR.YYMMDD.PATCH', + ); + if (lifecycle.previousVersion === lifecycle.candidateVersion) { + errors.push('lifecycle previous and candidate versions must differ'); + } + const previousVersion = parseReleaseVersion(lifecycle.previousVersion); + const candidateVersion = parseReleaseVersion(lifecycle.candidateVersion); + if ( + previousVersion !== null && + candidateVersion !== null && + compareReleaseVersions(previousVersion, candidateVersion) >= 0 + ) { + errors.push('lifecycle previous stable N must be older than candidate T'); } + if (!['stable', 'homolog', 'dev'].includes(String(lifecycle.channel))) { + errors.push('lifecycle.channel must be stable, homolog, or dev'); + } + matches(errors, 'lifecycle.sourceCommit', lifecycle.sourceCommit, COMMIT_SHA, 'a 40-character lowercase commit sha'); + const artifacts = record(errors, 'lifecycle.artifacts', lifecycle.artifacts); + const previous = validateArtifact( + errors, + 'lifecycle.artifacts.previous', + artifacts?.previous, + lifecycle.previousVersion, + entry, + undefined, + undefined, + ); + const candidate = validateArtifact( + errors, + 'lifecycle.artifacts.candidate', + artifacts?.candidate, + lifecycle.candidateVersion, + entry, + lifecycle.sourceCommit, + lifecycle.channel, + ); + same(errors, 'entry.artifactName binding', entry?.artifactName, candidate?.releaseName); + validateDelivery(errors, lifecycle.delivery, lifecycle, entry, candidate); + validateConvergence(errors, lifecycle.convergence); + validateStages( + errors, + (lifecycle as JsonRecord).stages, + lifecycle.previousVersion, + lifecycle.candidateVersion, + previous?.binarySha256, + candidate?.binarySha256, + entry?.evidenceKind, + ); + // Keep the previous value live in the validation graph: both generations must + // carry independent artifact identities, not a copied candidate object. + if (previous?.artifactSha256 === candidate?.artifactSha256) { + errors.push('previous and candidate artifacts must have different digests'); + } + return lifecycle; +} + +function validateDoctor(errors: string[], blocks: Array, candidateVersion: unknown): void { + const doctors = blocks.filter((block) => block !== null && isRecord(block.value.integrationSummary)); + if (doctors.length !== 1) { + errors.push(`evidence must contain exactly one doctor JSON block (got ${doctors.length})`); + return; + } + const doctor = (doctors[0] as JsonBlock).value; + same(errors, 'doctor.ok', doctor.ok, true); + if (!Array.isArray(doctor.checks) || doctor.checks.length === 0) + errors.push('doctor.checks must be a non-empty array'); + const summary = doctor.integrationSummary as JsonRecord; + same(errors, 'doctor.integrationSummary.schemaVersion', summary.schemaVersion, 1); + if ('state' in summary) errors.push('obsolete flat doctor integrationSummary.state is forbidden'); + const plugin = record(errors, 'doctor.integrationSummary.codexPlugin', summary.codexPlugin); + same(errors, 'doctor.integrationSummary.codexPlugin.state', plugin?.state, 'current'); + same(errors, 'doctor.integrationSummary.codexPlugin.installedVersion', plugin?.installedVersion, candidateVersion); + same(errors, 'doctor.integrationSummary.codexPlugin.targetVersion', plugin?.targetVersion, candidateVersion); + same(errors, 'doctor.integrationSummary.codexPlugin.actionRequired', plugin?.actionRequired, false); + same(errors, 'doctor.integrationSummary.codexPlugin.deliveryComplete', plugin?.deliveryComplete, true); } -/** Validate the evidence Markdown; returns a list of every structural failure (empty = valid). */ +function manifestFromBlocks(blocks: Array): JsonRecord | null { + const manifests = blocks.filter((block) => block !== null && block.value.kind === 'live-dogfood-evidence'); + return manifests.length > 0 ? (manifests[0] as JsonBlock).value : null; +} + +/** Validate evidence structure and all cross-field bindings. Empty means valid. */ export function validateLiveDogfoodEvidence(markdown: string): string[] { const errors: string[] = []; const blocks = extractJsonBlocks(markdown); const manifests = blocks.filter((block) => block !== null && block.value.kind === 'live-dogfood-evidence'); - if (manifests.length === 0) { - errors.push('no live-dogfood-evidence manifest json block found (must contain "kind":"live-dogfood-evidence")'); - // Still validate the doctor payload so the operator sees every gap at once. - validateDoctorPayload(errors, blocks); + if (manifests.length !== 1) { + errors.push(`evidence must contain exactly one live-dogfood-evidence manifest block (got ${manifests.length})`); + validateDoctor(errors, blocks, undefined); return errors; } - if (manifests.length > 1) { - errors.push('more than one live-dogfood-evidence manifest json block found (must be exactly one)'); - } const manifest = (manifests[0] as JsonBlock).value; - if (manifest.schemaVersion !== 1) errors.push('manifest.schemaVersion must be 1'); - validateCandidate(errors, manifest.candidate); - validateInertness(errors, manifest.inertness); - validateSteps(errors, manifest.steps); - if (typeof manifest.nNonGuarantee !== 'string' || manifest.nNonGuarantee.trim().length < 20) { - errors.push('manifest.nNonGuarantee must be an explicit statement that retired N may be gone after activation'); - } - validateDoctorPayload(errors, blocks); + same(errors, 'manifest.schemaVersion', manifest.schemaVersion, LIVE_DOGFOOD_SCHEMA_VERSION); + const entry = validateEntry(errors, manifest.entry); + const lifecycle = validateLifecycle(errors, manifest.lifecycle, entry); + validateRepositories(errors, manifest.repositories); + validateDoctor(errors, blocks, lifecycle?.candidateVersion); + return errors; +} + +function validateReferencedFiles(manifest: JsonRecord, inputsRoot: string): string[] { + const errors: string[] = []; + const entry = isRecord(manifest.entry) ? manifest.entry : null; + const inputs = entry && isRecord(entry.inputs) ? entry.inputs : null; + const lifecycle = isRecord(manifest.lifecycle) ? manifest.lifecycle : null; + const artifacts = lifecycle && isRecord(lifecycle.artifacts) ? lifecycle.artifacts : null; + for (const generation of ['previous', 'candidate'] as const) { + const input = inputs && isRecord(inputs[generation]) ? inputs[generation] : null; + const artifact = artifacts && isRecord(artifacts[generation]) ? artifacts[generation] : null; + const provenance = artifact && isRecord(artifact.provenance) ? artifact.provenance : null; + for (const [key, digest] of [ + ['artifact', artifact?.artifactSha256], + ['manifest', artifact?.manifestSha256], + ['identity', provenance?.identitySha256], + ['bundle', provenance?.bundleSha256], + ] as const) { + const reference = input?.[key]; + const path = typeof reference === 'string' ? resolve(inputsRoot, reference) : ''; + if (typeof reference !== 'string' || !path.startsWith(`${resolve(inputsRoot)}/`) || !existsSync(path)) { + errors.push(`referenced ${generation} ${key} is unavailable: ${String(reference)}`); + continue; + } + try { + if (!lstatSync(path).isFile()) { + errors.push(`referenced ${generation} ${key} is not a regular file: ${path}`); + continue; + } + const observed = createHash('sha256').update(readFileSync(path)).digest('hex'); + if (observed !== digest) errors.push(`referenced ${generation} ${key} digest mismatch`); + } catch (error) { + errors.push(`referenced ${generation} ${key} could not be verified: ${errorText(error)}`); + } + } + } + const stages = lifecycle && Array.isArray(lifecycle.stages) ? lifecycle.stages : []; + for (const [index, value] of stages.entries()) { + const stage = isRecord(value) ? value : null; + const reference = stage?.observationPath; + const path = typeof reference === 'string' ? resolve(inputsRoot, reference) : ''; + if ( + typeof reference !== 'string' || + !reference.startsWith('observations/') || + !path.startsWith(`${resolve(inputsRoot)}/`) || + !existsSync(path) + ) { + errors.push(`referenced stage observation is unavailable: ${String(reference)}`); + continue; + } + try { + if (!lstatSync(path).isFile()) { + errors.push(`referenced stage observation is not a regular file: ${path}`); + continue; + } + const bytes = readFileSync(path); + const observed = createHash('sha256').update(bytes).digest('hex'); + if (observed !== stage?.observationSha256) { + errors.push(`referenced stage observation digest mismatch at stages[${index}]`); + } + const parsed = JSON.parse(bytes.toString('utf8')) as unknown; + if (JSON.stringify(parsed) !== JSON.stringify(stage?.observation)) { + errors.push(`referenced stage observation content mismatch at stages[${index}]`); + } + } catch (error) { + errors.push(`referenced stage observation could not be verified: ${errorText(error)}`); + } + } return errors; } -export function validateLiveDogfoodEvidenceFile(path: string): string[] { +export function validateLiveDogfoodEvidenceFile(path: string, inputsRoot = dirname(resolve(path))): string[] { if (!existsSync(path)) return [`evidence file not found: ${path}`]; - return validateLiveDogfoodEvidence(readFileSync(path, 'utf8')); + const markdown = readFileSync(path, 'utf8'); + const errors = validateLiveDogfoodEvidence(markdown); + if (errors.length > 0) return errors; + const manifest = manifestFromBlocks(extractJsonBlocks(markdown)); + return manifest === null ? errors : [...errors, ...validateReferencedFiles(manifest, inputsRoot)]; } -function parseFileArg(argv: string[]): string { - if (argv.length !== 2 || argv[0] !== '--file' || !argv[1]) { - throw new Error('usage: bun scripts/validate-live-dogfood-evidence.ts --file '); +function parseArgs(argv: string[]): { file: string; inputsRoot?: string } { + if ( + (argv.length !== 2 && argv.length !== 4) || + argv[0] !== '--file' || + !argv[1] || + (argv.length === 4 && (argv[2] !== '--inputs-root' || !argv[3])) + ) { + throw new Error( + 'usage: bun scripts/validate-live-dogfood-evidence.ts --file [--inputs-root ]', + ); } - return argv[1]; + return { file: argv[1], inputsRoot: argv[3] }; +} + +function errorText(error: unknown): string { + return error instanceof Error ? error.message : String(error); } function main(): void { - let path: string; + let args: { file: string; inputsRoot?: string }; try { - path = parseFileArg(process.argv.slice(2)); + args = parseArgs(process.argv.slice(2)); } catch (error) { - console.error(error instanceof Error ? error.message : String(error)); + console.error(errorText(error)); process.exit(2); } - const errors = validateLiveDogfoodEvidenceFile(path); + const errors = validateLiveDogfoodEvidenceFile(args.file, args.inputsRoot); if (errors.length > 0) { - console.error(`validate-live-dogfood-evidence: FAIL (${errors.length}) — ${path}`); + console.error(`validate-live-dogfood-evidence: FAIL (${errors.length}) — ${args.file}`); for (const error of errors) console.error(` - ${error}`); process.exit(1); } - console.log(`validate-live-dogfood-evidence: OK — ${path}`); + console.log(`validate-live-dogfood-evidence: OK — ${args.file}`); } if (import.meta.main) main(); diff --git a/scripts/verify-delivery-evidence-pack.test.ts b/scripts/verify-delivery-evidence-pack.test.ts new file mode 100644 index 000000000..9e0adb93c --- /dev/null +++ b/scripts/verify-delivery-evidence-pack.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const SCRIPT = join(import.meta.dir, 'verify-delivery-evidence-pack.ts'); + +describe('production offline delivery evidence compatibility CLI', () => { + test('uses the production verifier without a cryptographic seam and rejects an invalid bundle', () => { + const source = readFileSync(SCRIPT, 'utf8'); + expect(source).toContain('verifyDownloadedDeliveryEvidence({'); + expect(source).not.toContain('verifyBundle:'); + + const root = mkdtempSync(join(tmpdir(), 'genie-evidence-compat-')); + try { + const manifest = Buffer.from( + `${JSON.stringify({ + schema_version: 1, + channel: 'dev', + version: '5.260723.7', + released_at: '2026-07-23T00:00:00Z', + tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v5.260723.7', + platforms: ['linux-x64-glibc', 'linux-x64-musl', 'linux-arm64', 'darwin-arm64'], + })}\n`, + ); + const descriptor = { + schemaVersion: 1, + repository: 'automagik-dev/genie', + version: '5.260723.7', + channel: 'dev', + platformId: 'linux-x64-glibc', + platformTriple: 'linux-x64', + releaseTag: 'v5.260723.7', + releaseName: 'genie-5.260723.7-linux-x64-glibc.tar.gz', + releaseManifestSha256: createHash('sha256').update(manifest).digest('hex'), + artifactSha256: '1'.repeat(64), + installedBinarySha256: '2'.repeat(64), + canonicalPayloadSha256: '3'.repeat(64), + digestAlgorithm: 'genie-physical-tree-v1', + sourceSha: 'a'.repeat(40), + sourceBranch: 'dev', + sourceCiRunId: '123', + controlSha: 'b'.repeat(40), + }; + const descriptorPath = join(root, 'descriptor.json'); + const bundlePath = join(root, 'bundle.json'); + const manifestPath = join(root, 'manifest.json'); + writeFileSync(descriptorPath, `${JSON.stringify(descriptor)}\n`); + writeFileSync(bundlePath, '{}\n'); + writeFileSync(manifestPath, manifest); + const result = Bun.spawnSync([ + 'bun', + SCRIPT, + '--descriptor', + descriptorPath, + '--bundle', + bundlePath, + '--manifest', + manifestPath, + ]); + expect(result.exitCode).not.toBe(0); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); +}); diff --git a/scripts/verify-delivery-evidence-pack.ts b/scripts/verify-delivery-evidence-pack.ts new file mode 100644 index 000000000..0a8e3d57a --- /dev/null +++ b/scripts/verify-delivery-evidence-pack.ts @@ -0,0 +1,44 @@ +#!/usr/bin/env bun + +import { readFileSync } from 'node:fs'; +import { + type DeliveryEvidenceDescriptor, + verifiedDeliveryEvidenceFacts, + verifyDownloadedDeliveryEvidence, +} from '../src/lib/codex-delivery-evidence.ts'; + +function fail(message: string): never { + console.error(`delivery-evidence-compatibility: ${message}`); + process.exit(2); +} + +const values = new Map(); +for (let index = 2; index < process.argv.length; index += 2) { + const key = process.argv[index]; + const value = process.argv[index + 1]; + if (!key?.startsWith('--') || value === undefined || values.has(key)) fail('invalid arguments'); + values.set(key, value); +} +const descriptorPath = values.get('--descriptor') ?? fail('missing --descriptor'); +const bundlePath = values.get('--bundle') ?? fail('missing --bundle'); +const manifestPath = values.get('--manifest') ?? fail('missing --manifest'); +if (values.size !== 3) fail('unknown argument'); + +const descriptorBytes = readFileSync(descriptorPath); +const descriptor = JSON.parse(descriptorBytes.toString('utf8')) as DeliveryEvidenceDescriptor; +const evidence = verifyDownloadedDeliveryEvidence({ + descriptorBytes, + bundleBytes: readFileSync(bundlePath), + manifestBytes: readFileSync(manifestPath), + targetVersion: descriptor.version, + channel: descriptor.channel, + platformId: descriptor.platformId, + platformTriple: descriptor.platformTriple, + releaseTag: descriptor.releaseTag, + releaseName: descriptor.releaseName, + artifactSha256: descriptor.artifactSha256, + installedBinarySha256: descriptor.installedBinarySha256, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, +}); +const facts = verifiedDeliveryEvidenceFacts(evidence); +console.log(`verified offline delivery evidence ${facts.evidenceDigest}`); diff --git a/src/fixtures/codex-delivery-public-good-trusted-root.json b/src/fixtures/codex-delivery-public-good-trusted-root.json new file mode 100644 index 000000000..effb0a19e --- /dev/null +++ b/src/fixtures/codex-delivery-public-good-trusted-root.json @@ -0,0 +1,126 @@ +{ + "mediaType": "application/vnd.dev.sigstore.trustedroot+json;version=0.1", + "tlogs": [ + { + "baseUrl": "https://rekor.sigstore.dev", + "hashAlgorithm": "SHA2_256", + "publicKey": { + "rawBytes": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE2G2Y+2tabdTV5BcGiBIx0a9fAFwrkBbmLSGtks4L3qX6yYY0zufBnhC8Ur/iy55GhWP/9A/bY2LhC30M9+RYtw==", + "keyDetails": "PKIX_ECDSA_P256_SHA_256", + "validFor": { + "start": "2021-01-12T11:53:27Z" + } + }, + "logId": { + "keyId": "wNI9atQGlz+VWfO6LRygH4QUfY/8W4RFwiT5i5WRgB0=" + } + }, + { + "baseUrl": "https://log2025-1.rekor.sigstore.dev", + "hashAlgorithm": "SHA2_256", + "publicKey": { + "rawBytes": "MCowBQYDK2VwAyEAt8rlp1knGwjfbcXAYPYAkn0XiLz1x8O4t0YkEhie244=", + "keyDetails": "PKIX_ED25519", + "validFor": { + "start": "2025-09-23T00:00:00Z" + } + }, + "logId": { + "keyId": "zxGZFVvd0FEmjR8WrFwMdcAJ9vtaY/QXf44Y1wUeP6A=" + } + } + ], + "certificateAuthorities": [ + { + "subject": { + "organization": "sigstore.dev", + "commonName": "sigstore" + }, + "uri": "https://fulcio.sigstore.dev", + "certChain": { + "certificates": [ + { + "rawBytes": "MIIB+DCCAX6gAwIBAgITNVkDZoCiofPDsy7dfm6geLbuhzAKBggqhkjOPQQDAzAqMRUwEwYDVQQKEwxzaWdzdG9yZS5kZXYxETAPBgNVBAMTCHNpZ3N0b3JlMB4XDTIxMDMwNzAzMjAyOVoXDTMxMDIyMzAzMjAyOVowKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTB2MBAGByqGSM49AgEGBSuBBAAiA2IABLSyA7Ii5k+pNO8ZEWY0ylemWDowOkNa3kL+GZE5Z5GWehL9/A9bRNA3RbrsZ5i0JcastaRL7Sp5fp/jD5dxqc/UdTVnlvS16an+2Yfswe/QuLolRUCrcOE2+2iA5+tzd6NmMGQwDgYDVR0PAQH/BAQDAgEGMBIGA1UdEwEB/wQIMAYBAf8CAQEwHQYDVR0OBBYEFMjFHQBBmiQpMlEk6w2uSu1KBtPsMB8GA1UdIwQYMBaAFMjFHQBBmiQpMlEk6w2uSu1KBtPsMAoGCCqGSM49BAMDA2gAMGUCMH8liWJfMui6vXXBhjDgY4MwslmN/TJxVe/83WrFomwmNf056y1X48F9c4m3a3ozXAIxAKjRay5/aj/jsKKGIkmQatjI8uupHr/+CxFvaJWmpYqNkLDGRU+9orzh5hI2RrcuaQ==" + } + ] + }, + "validFor": { + "start": "2021-03-07T03:20:29Z", + "end": "2022-12-31T23:59:59.999Z" + } + }, + { + "subject": { + "organization": "sigstore.dev", + "commonName": "sigstore" + }, + "uri": "https://fulcio.sigstore.dev", + "certChain": { + "certificates": [ + { + "rawBytes": "MIICGjCCAaGgAwIBAgIUALnViVfnU0brJasmRkHrn/UnfaQwCgYIKoZIzj0EAwMwKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTAeFw0yMjA0MTMyMDA2MTVaFw0zMTEwMDUxMzU2NThaMDcxFTATBgNVBAoTDHNpZ3N0b3JlLmRldjEeMBwGA1UEAxMVc2lnc3RvcmUtaW50ZXJtZWRpYXRlMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE8RVS/ysH+NOvuDZyPIZtilgUF9NlarYpAd9HP1vBBH1U5CV77LSS7s0ZiH4nE7Hv7ptS6LvvR/STk798LVgMzLlJ4HeIfF3tHSaexLcYpSASr1kS0N/RgBJz/9jWCiXno3sweTAOBgNVHQ8BAf8EBAMCAQYwEwYDVR0lBAwwCgYIKwYBBQUHAwMwEgYDVR0TAQH/BAgwBgEB/wIBADAdBgNVHQ4EFgQU39Ppz1YkEZb5qNjpKFWixi4YZD8wHwYDVR0jBBgwFoAUWMAeX5FFpWapesyQoZMi0CrFxfowCgYIKoZIzj0EAwMDZwAwZAIwPCsQK4DYiZYDPIaDi5HFKnfxXx6ASSVmERfsynYBiX2X6SJRnZU84/9DZdnFvvxmAjBOt6QpBlc4J/0DxvkTCqpclvziL6BCCPnjdlIB3Pu3BxsPmygUY7Ii2zbdCdliiow=" + }, + { + "rawBytes": "MIIB9zCCAXygAwIBAgIUALZNAPFdxHPwjeDloDwyYChAO/4wCgYIKoZIzj0EAwMwKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTAeFw0yMTEwMDcxMzU2NTlaFw0zMTEwMDUxMzU2NThaMCoxFTATBgNVBAoTDHNpZ3N0b3JlLmRldjERMA8GA1UEAxMIc2lnc3RvcmUwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAT7XeFT4rb3PQGwS4IajtLk3/OlnpgangaBclYpsYBr5i+4ynB07ceb3LP0OIOZdxexX69c5iVuyJRQ+Hz05yi+UF3uBWAlHpiS5sh0+H2GHE7SXrk1EC5m1Tr19L9gg92jYzBhMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBRYwB5fkUWlZql6zJChkyLQKsXF+jAfBgNVHSMEGDAWgBRYwB5fkUWlZql6zJChkyLQKsXF+jAKBggqhkjOPQQDAwNpADBmAjEAj1nHeXZp+13NWBNa+EDsDP8G1WWg1tCMWP/WHPqpaVo0jhsweNFZgSs0eE7wYI4qAjEA2WB9ot98sIkoF3vZYdd3/VtWB5b9TNMea7Ix/stJ5TfcLLeABLE4BNJOsQ4vnBHJ" + } + ] + }, + "validFor": { + "start": "2022-04-13T20:06:15Z" + } + } + ], + "ctlogs": [ + { + "baseUrl": "https://ctfe.sigstore.dev/test", + "hashAlgorithm": "SHA2_256", + "publicKey": { + "rawBytes": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEbfwR+RJudXscgRBRpKX1XFDy3PyudDxz/SfnRi1fT8ekpfBd2O1uoz7jr3Z8nKzxA69EUQ+eFCFI3zeubPWU7w==", + "keyDetails": "PKIX_ECDSA_P256_SHA_256", + "validFor": { + "start": "2021-03-14T00:00:00Z", + "end": "2022-10-31T23:59:59.999Z" + } + }, + "logId": { + "keyId": "CGCS8ChS/2hF0dFrJ4ScRWcYrBY9wzjSbea8IgY2b3I=" + } + }, + { + "baseUrl": "https://ctfe.sigstore.dev/2022", + "hashAlgorithm": "SHA2_256", + "publicKey": { + "rawBytes": "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEiPSlFi0CmFTfEjCUqF9HuCEcYXNKAaYalIJmBZ8yyezPjTqhxrKBpMnaocVtLJBI1eM3uXnQzQGAJdJ4gs9Fyw==", + "keyDetails": "PKIX_ECDSA_P256_SHA_256", + "validFor": { + "start": "2022-10-20T00:00:00Z" + } + }, + "logId": { + "keyId": "3T0wasbHETJjGR4cmWc3AqJKXrjePK3/h4pygC8p7o4=" + } + } + ], + "timestampAuthorities": [ + { + "subject": { + "organization": "sigstore.dev", + "commonName": "sigstore-tsa-selfsigned" + }, + "uri": "https://timestamp.sigstore.dev/api/v1/timestamp", + "certChain": { + "certificates": [ + { + "rawBytes": "MIICEDCCAZagAwIBAgIUOhNULwyQYe68wUMvy4qOiyojiwwwCgYIKoZIzj0EAwMwOTEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MSAwHgYDVQQDExdzaWdzdG9yZS10c2Etc2VsZnNpZ25lZDAeFw0yNTA0MDgwNjU5NDNaFw0zNTA0MDYwNjU5NDNaMC4xFTATBgNVBAoTDHNpZ3N0b3JlLmRldjEVMBMGA1UEAxMMc2lnc3RvcmUtdHNhMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE4ra2Z8hKNig2T9kFjCAToGG30jky+WQv3BzL+mKvh1SKNR/UwuwsfNCg4sryoYAd8E6isovVA3M4aoNdm9QDi50Z8nTEyvqgfDPtTIwXItfiW/AFf1V7uwkbkAoj0xxco2owaDAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0OBBYEFIn9eUOHz9BlRsMCRscsc1t9tOsDMB8GA1UdIwQYMBaAFJjsAe9/u1H/1JUeb4qImFMHic6/MBYGA1UdJQEB/wQMMAoGCCsGAQUFBwMIMAoGCCqGSM49BAMDA2gAMGUCMDtpsV/6KaO0qyF/UMsX2aSUXKQFdoGTptQGc0ftq1csulHPGG6dsmyMNd3JB+G3EQIxAOajvBcjpJmKb4Nv+2Taoj8Uc5+b6ih6FXCCKraSqupe07zqswMcXJTe1cExvHvvlw==" + }, + { + "rawBytes": "MIIB9zCCAXygAwIBAgIUV7f0GLDOoEzIh8LXSW80OJiUp14wCgYIKoZIzj0EAwMwOTEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MSAwHgYDVQQDExdzaWdzdG9yZS10c2Etc2VsZnNpZ25lZDAeFw0yNTA0MDgwNjU5NDNaFw0zNTA0MDYwNjU5NDNaMDkxFTATBgNVBAoTDHNpZ3N0b3JlLmRldjEgMB4GA1UEAxMXc2lnc3RvcmUtdHNhLXNlbGZzaWduZWQwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAQUQNtfRT/ou3YATa6wB/kKTe70cfJwyRIBovMnt8RcJph/COE82uyS6FmppLLL1VBPGcPfpQPYJNXzWwi8icwhKQ6W/Qe2h3oebBb2FHpwNJDqo+TMaC/tdfkv/ElJB72jRTBDMA4GA1UdDwEB/wQEAwIBBjASBgNVHRMBAf8ECDAGAQH/AgEAMB0GA1UdDgQWBBSY7AHvf7tR/9SVHm+KiJhTB4nOvzAKBggqhkjOPQQDAwNpADBmAjEAwGEGrfGZR1cen1R8/DTVMI943LssZmJRtDp/i7SfGHmGRP6gRbuj9vOK3b67Z0QQAjEAuT2H673LQEaHTcyQSZrkp4mX7WwkmF+sVbkYY5mXN+RMH13KUEHHOqASaemYWK/E" + } + ] + }, + "validFor": { + "start": "2025-07-04T00:00:00Z" + } + } + ] +} diff --git a/src/fixtures/codex-role-agent-allowlist.json b/src/fixtures/codex-role-agent-allowlist.json new file mode 100644 index 000000000..00ebe5592 --- /dev/null +++ b/src/fixtures/codex-role-agent-allowlist.json @@ -0,0 +1,62 @@ +[ + { + "name": "genie-engineer-complex.toml", + "type": "regular", + "mode": "0644", + "digest": "62ecc570f1d77783511a9e7f0aa67b3a65d8bba292963409a02c7712c93ebc3b" + }, + { + "name": "genie-engineer-standard.toml", + "type": "regular", + "mode": "0644", + "digest": "dc746813b9b4b6aa984c17fa2fd75d4dbe34eba08494a174c0715da07aa9dd30" + }, + { + "name": "genie-engineer-trivial.toml", + "type": "regular", + "mode": "0644", + "digest": "249deced5a02eb2cbe3303db566992d1336c75d853967f851bf1d0e85b6b0f47" + }, + { + "name": "genie-final-gate.toml", + "type": "regular", + "mode": "0644", + "digest": "10ef070db8aace75bd80ef9e060a6ec601e3768f177fdd843f4db11035738f7e" + }, + { + "name": "genie-fixer.toml", + "type": "regular", + "mode": "0644", + "digest": "b3c1f407d4a3a2cfe204dee7b4a9c038e1a8f4644c446fcfa23f4a681bf0c7b3" + }, + { + "name": "genie-reviewer.toml", + "type": "regular", + "mode": "0644", + "digest": "55bb2dea3da381f4d6ea210869f9a26ac1eed825097085a9bfa0f41a005c8d14" + }, + { + "name": "genie-reviewer.toml", + "type": "regular", + "mode": "0644", + "digest": "91f40a07905834716311419375581e3245544a77eac3d93d082842652c6452bf" + }, + { + "name": "genie-reviewer.toml", + "type": "regular", + "mode": "0644", + "digest": "c7008dcaa1e31b46e2bb05ca13afb2e918ee483422c84386a1c8997485bcfea7" + }, + { + "name": "genie-reviewer.toml", + "type": "regular", + "mode": "0644", + "digest": "f0bf69fa70dc9e014305f155a024c4dadb6caf48ce217b8c31119506e244f844" + }, + { + "name": "genie-scout.toml", + "type": "regular", + "mode": "0644", + "digest": "03a9fb3ca0e5f36c69c8f934d37adce1bae736e4c3895b144a0001ad31b1ba59" + } +] diff --git a/src/genie-commands/__tests__/update-command-publication.test.ts b/src/genie-commands/__tests__/update-command-publication.test.ts new file mode 100644 index 000000000..daca995f7 --- /dev/null +++ b/src/genie-commands/__tests__/update-command-publication.test.ts @@ -0,0 +1,162 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; +import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { scanPhysicalTree } from '../../lib/codex-activation.js'; +import type { DeliveryEvidencePlatformId } from '../../lib/codex-delivery-evidence.js'; +import { buildTestDeliveryEvidencePack } from '../../lib/codex-delivery-evidence.test-support.js'; +import { VERSION } from '../../lib/version.js'; + +const RUNNER = join(import.meta.dir, '..', '..', '..', 'tests', 'support', 'update-publication-failure-runner.ts'); +const roots: string[] = []; + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +function tempRoot(label: string): string { + const root = mkdtempSync(join(tmpdir(), `genie-${label}-`)); + roots.push(root); + return root; +} + +function sha256(path: string): string { + return createHash('sha256').update(readFileSync(path)).digest('hex'); +} + +function writeExecutable(path: string, source: string): void { + writeFileSync(path, source); + chmodSync(path, 0o755); +} + +function platformId(): DeliveryEvidencePlatformId { + if (process.platform === 'darwin') return 'darwin-arm64'; + if (process.arch === 'arm64') return 'linux-arm64'; + return 'linux-x64-glibc'; +} + +function buildReleasePayload( + root: string, + version: string, +): { + tarball: string; + binarySha256: string; + payloadSha256: string; +} { + const payload = join(root, 'payload'); + for (const directory of ['.agents', '.claude-plugin', 'plugins/genie', 'skills/review', 'templates']) { + mkdirSync(join(payload, directory), { recursive: true }); + } + writeFileSync(join(payload, '.agents', 'plugin.json'), '{}\n'); + writeFileSync(join(payload, '.claude-plugin', 'marketplace.json'), '{}\n'); + writeFileSync(join(payload, 'LICENSE'), 'test fixture\n'); + writeFileSync(join(payload, 'VERSION'), `${version}\n`); + writeFileSync(join(payload, 'plugins', 'genie', 'plugin.txt'), 'authenticated plugin payload\n'); + writeFileSync(join(payload, 'skills', 'review', 'SKILL.md'), '# Review\n'); + writeFileSync(join(payload, 'templates', 'template.txt'), 'template\n'); + writeExecutable( + join(payload, 'genie'), + `#!/bin/sh\nif [ "\${1:-}" = "--version" ]; then printf 'genie ${version}\\n'; exit 0; fi\nexit 0\n`, + ); + const tree = scanPhysicalTree(join(payload, 'plugins', 'genie')); + if (tree.status !== 'ok' || tree.digest === undefined) throw new Error(`fixture tree is ${tree.status}`); + const tarball = join(root, `genie-${version}-${platformId()}.tar.gz`); + const archived = Bun.spawnSync(['tar', '-czf', tarball, '-C', payload, '.'], { + stdout: 'pipe', + stderr: 'pipe', + }); + if (archived.exitCode !== 0) throw new Error(archived.stderr.toString()); + return { tarball, binarySha256: sha256(join(payload, 'genie')), payloadSha256: tree.digest }; +} + +describe('updateCommand publication boundary', () => { + test('real promotion plus an unwritable record store exits nonzero and preserves retry metadata', () => { + const root = tempRoot('update-command-publication'); + const genieHome = join(root, 'genie-home'); + const bin = join(genieHome, 'bin'); + const fakeBin = join(root, 'fake-bin'); + const fixture = join(root, 'fixture'); + mkdirSync(bin, { recursive: true }); + mkdirSync(fakeBin); + mkdirSync(fixture); + + const oldVersion = '1.1.1'; + writeExecutable( + join(bin, 'genie'), + `#!/bin/sh\nif [ "\${1:-}" = "--version" ]; then printf 'genie ${oldVersion}\\n'; exit 0; fi\nexit 0\n`, + ); + writeExecutable( + join(fakeBin, 'codex'), + '#!/bin/sh\nif [ "$1" = "plugin" ] && [ "$2" = "list" ]; then printf \'{"installed":[]}\\n\'; exit 0; fi\nexit 1\n', + ); + writeFileSync( + join(genieHome, '.integration-consent.json'), + `${JSON.stringify({ + schemaVersion: 3, + selection: 'codex', + state: 'committed', + revision: 1, + updatedAt: '2026-07-23T00:00:00.000Z', + })}\n`, + ); + const marker = join(genieHome, '.install-version'); + writeFileSync(marker, `${oldVersion}\n`); + const recordPath = join(genieHome, '.codex-plugin-delivery-record.json'); + mkdirSync(recordPath); + + const release = buildReleasePayload(fixture, VERSION); + const pack = buildTestDeliveryEvidencePack({ + descriptor: { + version: VERSION, + channel: 'stable', + platformId: platformId(), + platformTriple: `${process.platform}-${process.arch}`, + releaseTag: `v${VERSION}`, + releaseName: `genie-${VERSION}-${platformId()}.tar.gz`, + artifactSha256: sha256(release.tarball), + installedBinarySha256: release.binarySha256, + canonicalPayloadSha256: release.payloadSha256, + }, + }); + const manifestPath = join(fixture, 'manifest.json'); + const descriptorPath = join(fixture, 'descriptor.json'); + const bundlePath = join(fixture, 'bundle.json'); + writeFileSync(manifestPath, pack.manifestBytes); + writeFileSync(descriptorPath, pack.descriptorBytes); + writeFileSync(bundlePath, pack.bundleBytes); + + const env = { ...process.env }; + env.GENIE_BUNDLE_ROOT = undefined; + env.GENIE_LIFECYCLE_LEASE_PATH = undefined; + env.GENIE_LIFECYCLE_LEASE_OWNER = undefined; + env.GENIE_UPDATE_ROLLBACK = undefined; + env.GENIE_UPDATE_SYNC_ONLY = undefined; + Object.assign(env, { + HOME: join(root, 'user-home'), + CODEX_HOME: join(root, 'codex-home'), + GENIE_HOME: genieHome, + GENIE_CHANNEL: 'stable', + GENIE_TEST_TARBALL: release.tarball, + GENIE_TEST_MANIFEST: manifestPath, + GENIE_TEST_DESCRIPTOR: descriptorPath, + GENIE_TEST_BUNDLE: bundlePath, + NO_COLOR: '1', + PATH: `${bin}:${fakeBin}:${process.env.PATH ?? ''}`, + }); + + const result = Bun.spawnSync(['bun', RUNNER], { env, stdout: 'pipe', stderr: 'pipe' }); + const output = `${result.stdout.toString()}\n${result.stderr.toString()}`; + expect(result.exitCode).toBe(1); + expect(output).toContain('authenticated Codex delivery publication incomplete'); + expect(output.match(/"deliveryComplete":false/g)).toHaveLength(1); + expect(output).not.toContain('"deliveryComplete":true'); + expect(existsSync(marker)).toBe(true); + expect(readFileSync(marker, 'utf8')).toBe(`${oldVersion}\n`); + expect(existsSync(recordPath) && statSync(recordPath).isDirectory()).toBe(true); + + const installed = Bun.spawnSync([join(bin, 'genie'), '--version'], { stdout: 'pipe', stderr: 'pipe' }); + expect(installed.exitCode).toBe(0); + expect(installed.stdout.toString()).toContain(VERSION); + }); +}); diff --git a/src/genie-commands/__tests__/update-current-boundary.test.ts b/src/genie-commands/__tests__/update-current-boundary.test.ts new file mode 100644 index 000000000..6d3cb6d6e --- /dev/null +++ b/src/genie-commands/__tests__/update-current-boundary.test.ts @@ -0,0 +1,81 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const RUNNER = join(import.meta.dir, '..', '..', '..', 'tests', 'support', 'update-current-boundary-runner.ts'); +const roots: string[] = []; + +interface BoundaryResult { + deliveries: number; + convergenceRuns: number; + deliveredExact: boolean; + markerExists: boolean; + markerText: string | null; + commandExitCode: number; +} + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +function runBoundary(scenario: 'failed' | 'route-upgrade' | 'repaired-current' | 'exit-handoff'): { + exitCode: number; + result: BoundaryResult; +} { + const root = mkdtempSync(join(tmpdir(), `genie-update-current-${scenario}-`)); + roots.push(root); + const env = { ...process.env }; + env.GENIE_BUNDLE_ROOT = undefined; + env.GENIE_LIFECYCLE_LEASE_OWNER = undefined; + env.GENIE_LIFECYCLE_LEASE_PATH = undefined; + Object.assign(env, { + HOME: join(root, 'user-home'), + CODEX_HOME: join(root, 'codex-home'), + GENIE_HOME: join(root, 'genie-home'), + GENIE_TEST_UPDATE_CURRENT_SCENARIO: scenario, + }); + const spawned = Bun.spawnSync(['bun', RUNNER], { env, stdout: 'pipe', stderr: 'pipe' }); + const stdout = spawned.stdout.toString().trim(); + if (stdout.length === 0) { + throw new Error(`boundary runner emitted no result\n${spawned.stderr.toString()}`); + } + return { + exitCode: spawned.exitCode, + result: JSON.parse(stdout.split('\n').at(-1) as string) as BoundaryResult, + }; +} + +describe('updateCommand current-version repair boundary', () => { + test('failed repair exits before selected-target delivery and preserves the real legacy marker', () => { + const { exitCode, result } = runBoundary('failed'); + expect(exitCode).toBe(1); + expect(result.commandExitCode).toBe(1); + expect(result.deliveries).toBe(0); + expect(result.convergenceRuns).toBe(0); + expect(result.markerExists).toBe(true); + expect(result.markerText).toBe('prior-marker\n'); + }); + + test('channel advance feeds the exact pinned manifest into ordinary selected-target delivery', () => { + const { exitCode, result } = runBoundary('route-upgrade'); + expect(exitCode).toBe(0); + expect(result.commandExitCode).toBe(0); + expect(result.deliveries).toBe(1); + expect(result.deliveredExact).toBe(true); + expect(result.convergenceRuns).toBe(0); + }); + + for (const directive of ['repaired-current', 'exit-handoff'] as const) { + test(`${directive} retires the real legacy marker without entering convergence or selected-target delivery`, () => { + const { exitCode, result } = runBoundary(directive); + const expectedExit = directive === 'exit-handoff' ? 2 : 0; + expect(exitCode).toBe(expectedExit); + expect(result.commandExitCode).toBe(expectedExit); + expect(result.deliveries).toBe(0); + expect(result.convergenceRuns).toBe(0); + expect(result.markerExists).toBe(false); + expect(result.markerText).toBeNull(); + }); + } +}); diff --git a/src/genie-commands/__tests__/update.test.ts b/src/genie-commands/__tests__/update.test.ts index 4b21c43e0..723550ec3 100644 --- a/src/genie-commands/__tests__/update.test.ts +++ b/src/genie-commands/__tests__/update.test.ts @@ -20,14 +20,23 @@ import { mkdtempSync, readFileSync, readdirSync, + realpathSync, renameSync, rmSync, statSync, + symlinkSync, writeFileSync, } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { type AgentSyncReport, acquireLifecycleLease, runAgentSync } from '../../lib/agent-sync'; +import { observeCodexActivation, openCodexActivationStore } from '../../lib/codex-activation'; +import type { DeliveryEvidencePlatformId } from '../../lib/codex-delivery-evidence'; +import { mintTestDeliveryEvidence } from '../../lib/codex-delivery-evidence.test-support'; +import { + type HeldLifecycleLease, + acquireLifecycleLease as acquireCodexLifecycleLease, +} from '../../lib/codex-lifecycle-lease'; import { REQUIRED_GENIE_MCP_TOOLS } from '../../lib/codex-mcp-health-session'; import type { CodexPluginProbe } from '../../lib/codex-project-mcp'; import { @@ -37,16 +46,21 @@ import { type CommandRunner, type IntegrationSelection, } from '../../lib/runtime-integrations'; +import { VERSION } from '../../lib/version'; import type { AuxiliaryTreeOutcome, AuxiliaryTreeStage } from '../auxiliary-trees.js'; +import type { PinnedManifest } from '../codex-delivery-repair.js'; import { type RefreshUpdatePluginsOptions, refreshUpdatePlugins as refreshUpdatePluginsWithPhysicalVerification, } from '../update-integrations.js'; import { + CodexDeliveryPublicationError, type LatestManifest, type VerifyResult, _resetNextDeprecationLatchForTest, applyConvergenceExitSignal, + attemptAlreadyCurrentDeliveryRepair, + buildAlreadyCurrentRepairSeams, compareVersions, createPrivateUpdateTempRoot, decideDowngrade, @@ -57,10 +71,13 @@ import { fetchLatestManifest, finalizeAuxiliaryDelivery, formatVerifyBanner, + handleAlreadyCurrentUpdate, hashPhysicalFileIncrementally, isGenieProcessSnapshotLine, manifestUrlForChannel, + mapAlreadyCurrentRepairOutcome, narrowUpdateAgentSyncSelection, + narrowUpdatePluginRefreshSelection, normalizeVersion, persistChannel, resolveChannel, @@ -73,12 +90,15 @@ import { runFreshBinaryPostDeliveryConvergence, runLegacySyncOnlyConvergence, runManualUpdateConvergence, + runNormalUpdatePublicationBoundary, + runUpdateAgentSync, runV4CleanupSafe, runVerifyProbe, shortCircuitIfCurrent, shouldEmitPathDivergenceWarning, summarizeJsonlSignals, syncAuxiliaryContent, + updateCommand, } from '../update.js'; function healthyUpdateCodexProbe(): CodexPluginProbe { @@ -407,7 +427,7 @@ describe('updateCommand downgrade wiring (BUG B source-shape lock)', () => { const cmdStart = source.indexOf('export async function updateCommand'); const cmdBody = source.slice(cmdStart); const guardIdx = cmdBody.indexOf('applyDowngradeGuard('); - const downloadIdx = cmdBody.indexOf('await downloadAndVerifyTarball('); + const downloadIdx = cmdBody.indexOf('dependencies.downloadDeliveryAssets ?? downloadAndVerifyDeliveryAssets'); // The guard must run BEFORE any tarball is fetched. expect(guardIdx).toBeGreaterThan(-1); expect(guardIdx).toBeLessThan(downloadIdx); @@ -435,6 +455,17 @@ describe('updateCommand downgrade wiring (BUG B source-shape lock)', () => { // ============================================================================ describe('updateCommand wiring', () => { + const commandManifest: LatestManifest = { + schema_version: 1, + channel: 'stable', + version: '5.260723.8', + released_at: '2026-07-23T00:00:00Z', + tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v5.260723.8', + platforms: ['darwin-arm64'], + manifestBytes: '{"version":"5.260723.8"}\n', + manifestSha256: 'a'.repeat(64), + }; + test('npm-update path is gone — no `bun add @automagik/genie` references', () => { const source = readFileSync(join(__dirname, '..', 'update.ts'), 'utf-8'); expect(source).not.toMatch(/bun add[^\n]*@automagik\/genie/); @@ -463,6 +494,16 @@ describe('updateCommand wiring', () => { expect(source).toContain('--rollback'); }); + test('the already-current handoff prints plain language and returns before convergence', () => { + const source = readFileSync(join(__dirname, '..', 'update.ts'), 'utf-8'); + // A bare machine trailer must never be the whole human output. Publication + // returns its activation handoff without entering another authority. + expect(source).toContain('Codex plugin activation is pending: retire Codex tasks'); + const handoffBranch = source.slice(source.indexOf('handleAlreadyCurrentUpdate')); + expect(handoffBranch).toContain('log(CODEX_DELIVERY_RESULT_TRAILER)'); + expect(handoffBranch).toContain('process.exitCode = 2'); + }); + test('"Already up to date" exit logs version and channel', () => { const source = readFileSync(join(__dirname, '..', 'update.ts'), 'utf-8'); expect(source).toContain('Already up to date'); @@ -470,7 +511,9 @@ describe('updateCommand wiring', () => { // running process's compile-time VERSION — otherwise a stale shadowing // binary on $PATH re-offers the same update forever. expect(source).toContain('shortCircuitIfCurrent(installedVersion, latestVersion)'); - expect(source).toContain('const installedVersion = resolveInstalledVersion()'); + expect(source).toContain( + 'const installedVersion = (dependencies.readInstalledVersion ?? resolveInstalledVersion)()', + ); }); test('--rollback short-circuits before downloading anything', () => { @@ -479,16 +522,360 @@ describe('updateCommand wiring', () => { const cmdStart = source.indexOf('export async function updateCommand'); expect(cmdStart).toBeGreaterThan(-1); const cmdBody = source.slice(cmdStart); - const explicitModeIdx = cmdBody.indexOf('await runExplicitUpdateMode(mode)'); - const fetchIdx = cmdBody.indexOf('await fetchLatestManifest('); + const explicitModeIdx = cmdBody.indexOf('await dispatchNonNormalUpdateMode(options)'); + const fetchIdx = cmdBody.indexOf('dependencies.fetchManifest ?? fetchLatestManifest'); expect(explicitModeIdx).toBeGreaterThan(-1); expect(fetchIdx).toBeGreaterThan(-1); expect(explicitModeIdx).toBeLessThan(fetchIdx); + const dispatcher = source.slice( + source.indexOf('async function dispatchNonNormalUpdateMode'), + source.indexOf('async function confirmPlannedDelivery'), + ); + expect(dispatcher).toContain('await runExplicitUpdateMode(mode)'); const explicitMode = source.slice( source.indexOf('async function runExplicitUpdateMode'), - source.indexOf('function runFreshConvergenceOrReport'), + source.indexOf('async function dispatchNonNormalUpdateMode'), ); - expect(explicitMode).toContain("if (mode === 'rollback') await runRollback()"); + expect(explicitMode).toContain("if (mode === 'rollback') terminal = await runRollback()"); + }); + + test('no hard process exit is reachable while normal or explicit update leases are held', () => { + const source = readFileSync(join(__dirname, '..', 'update.ts'), 'utf-8'); + const normalStart = source.indexOf('const acquired = acquireUpdateLifecycleLeasesOrProject(dependencies);'); + const normalEnd = source.indexOf('\n/**\n * Post-swap v4 legacy cleanup', normalStart); + const explicitStart = source.indexOf('async function runExplicitUpdateMode'); + const explicitEnd = source.indexOf('\nasync function dispatchNonNormalUpdateMode', explicitStart); + expect(normalStart).toBeGreaterThan(-1); + expect(normalEnd).toBeGreaterThan(normalStart); + expect(explicitStart).toBeGreaterThan(-1); + expect(explicitEnd).toBeGreaterThan(explicitStart); + expect(source.slice(normalStart, normalEnd)).not.toContain('process.exit('); + expect(source.slice(explicitStart, explicitEnd)).not.toContain('process.exit('); + expect(source.slice(normalStart, normalEnd)).toContain('projectDeferredUpdateTerminal(terminal)'); + expect(source.slice(explicitStart, explicitEnd)).toContain('projectDeferredUpdateTerminal(terminal)'); + }); + + test('a setup-held Codex lease refuses update before recovery or any delivery-owned mutation', async () => { + const priorExitCode = process.exitCode; + const stdout: string[] = []; + const stderr: string[] = []; + const events: string[] = []; + const logSpy = spyOn(console, 'log').mockImplementation((...args: unknown[]) => stdout.push(args.join(' '))); + const errorSpy = spyOn(console, 'error').mockImplementation((...args: unknown[]) => stderr.push(args.join(' '))); + process.exitCode = undefined; + try { + await updateCommand( + { yes: true, stable: true }, + { + fetchManifest: async () => commandManifest, + readInstalledVersion: () => '5.260700.1', + resolvePlatform: () => 'darwin-arm64', + acquireLease: () => { + events.push('agent-acquire'); + return { + path: '/fixture/.agent-sync.lock', + release: () => events.push('agent-release'), + }; + }, + acquireCodexLease: () => { + events.push('codex-busy'); + return { + ok: false, + reason: 'codex-lifecycle-busy', + holderKind: 'setup-activation', + detail: 'held by setup-activation', + }; + }, + recoverPendingState: () => events.push('MUTATION:recovery'), + persistSelectedChannel: async () => { + events.push('MUTATION:channel'); + }, + requireCanonicalInstall: () => events.push('MUTATION:canonical'), + deliverSelectedManifest: async () => { + events.push('MUTATION:delivery'); + return []; + }, + finalizeSelectedDelivery: async () => { + events.push('MUTATION:finalize'); + return true; + }, + }, + ); + + expect(events).toEqual(['agent-acquire', 'codex-busy', 'agent-release']); + expect(events.some((event) => event.startsWith('MUTATION:'))).toBe(false); + expect(Number(process.exitCode)).toBe(2); + const output = [...stdout, ...stderr].join('\n'); + expect(output).toContain('codex-lifecycle-busy'); + expect(output).toContain('setup-activation'); + expect(output).toContain('"deliveryComplete":false'); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + process.exitCode = priorExitCode; + } + }); + + test('an initial Codex fencing failure releases in reverse order before projecting and runs no mutation', async () => { + const priorExitCode = process.exitCode; + const stdout: string[] = []; + const stderr: string[] = []; + const events: string[] = []; + const logSpy = spyOn(console, 'log').mockImplementation((...args: unknown[]) => { + const line = args.join(' '); + stdout.push(line); + if (line.includes('Update failed: stale lifecycle authority')) events.push('terminal-error'); + if (line.includes('"deliveryComplete":false')) events.push('terminal-trailer'); + }); + const errorSpy = spyOn(console, 'error').mockImplementation((...args: unknown[]) => { + const line = args.join(' '); + stderr.push(line); + if (line.includes('Update failed: stale lifecycle authority')) events.push('terminal-error'); + }); + process.exitCode = undefined; + try { + await updateCommand( + { yes: true, stable: true }, + { + fetchManifest: async () => commandManifest, + readInstalledVersion: () => '5.260700.1', + resolvePlatform: () => 'darwin-arm64', + acquireLease: () => { + events.push('agent-acquire'); + return { + path: '/fixture/.agent-sync.lock', + release: () => events.push('agent-release'), + }; + }, + acquireCodexLease: () => { + events.push('codex-acquire'); + return { + ok: true, + operationId: 'e'.repeat(32), + kind: 'update-delivery', + assertOperation: () => { + events.push('codex-assert'); + throw new Error('stale lifecycle authority'); + }, + release: () => events.push('codex-release'), + }; + }, + recoverPendingState: () => events.push('MUTATION:recovery'), + persistSelectedChannel: async () => { + events.push('MUTATION:channel'); + }, + requireCanonicalInstall: () => events.push('MUTATION:canonical'), + deliverSelectedManifest: async () => { + events.push('MUTATION:delivery'); + return []; + }, + finalizeSelectedDelivery: async () => { + events.push('MUTATION:finalize'); + return true; + }, + }, + ); + + expect(events).toEqual([ + 'agent-acquire', + 'codex-acquire', + 'codex-assert', + 'codex-release', + 'agent-release', + 'terminal-error', + 'terminal-trailer', + ]); + expect(events.some((event) => event.startsWith('MUTATION:'))).toBe(false); + expect(Number(process.exitCode)).toBe(1); + const output = [...stdout, ...stderr].join('\n'); + expect(output).toContain('Update failed: stale lifecycle authority'); + expect(output.match(/"deliveryComplete":false/g)).toHaveLength(1); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + process.exitCode = priorExitCode ?? 0; + } + }); + + test('normal update holds agent-sync then Codex through delivery and releases in reverse order', async () => { + const priorExitCode = process.exitCode; + const events: string[] = []; + const logSpy = spyOn(console, 'log').mockImplementation(() => undefined); + const errorSpy = spyOn(console, 'error').mockImplementation(() => undefined); + process.exitCode = undefined; + try { + await updateCommand( + { yes: true, stable: true }, + { + fetchManifest: async () => { + events.push('fetch'); + return commandManifest; + }, + readInstalledVersion: () => { + events.push('read'); + return '5.260700.1'; + }, + resolvePlatform: () => { + events.push('platform'); + return 'darwin-arm64'; + }, + acquireLease: () => { + events.push('agent-acquire'); + return { + path: '/fixture/.agent-sync.lock', + release: () => events.push('agent-release'), + }; + }, + acquireCodexLease: () => { + events.push('codex-acquire'); + return { + ok: true, + operationId: 'c'.repeat(32), + kind: 'update-delivery', + assertOperation: () => events.push('codex-assert'), + release: () => events.push('codex-release'), + }; + }, + recoverPendingState: () => events.push('recover'), + persistSelectedChannel: async () => { + events.push('persist'); + }, + requireCanonicalInstall: () => events.push('canonical'), + deliverSelectedManifest: async () => { + events.push('deliver'); + return []; + }, + finalizeSelectedDelivery: async () => { + events.push('finalize'); + return true; + }, + }, + ); + + expect(events).toEqual([ + 'fetch', + 'read', + 'platform', + 'agent-acquire', + 'codex-acquire', + 'codex-assert', + 'recover', + 'read', + 'persist', + 'canonical', + 'codex-assert', + 'deliver', + 'finalize', + 'codex-release', + 'agent-release', + ]); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + process.exitCode = priorExitCode; + } + }); + + test('same-version repair borrows the one command-held Codex lease without nested acquisition', async () => { + const priorExitCode = process.exitCode; + const events: string[] = []; + const logSpy = spyOn(console, 'log').mockImplementation(() => undefined); + const errorSpy = spyOn(console, 'error').mockImplementation(() => undefined); + process.exitCode = undefined; + const held: HeldLifecycleLease = { + ok: true, + operationId: 'd'.repeat(32), + kind: 'update-delivery', + assertOperation: () => events.push('codex-assert'), + release: () => events.push('codex-release'), + }; + try { + await updateCommand( + { yes: true, stable: true }, + { + fetchManifest: async () => commandManifest, + readInstalledVersion: () => commandManifest.version, + resolvePlatform: () => 'darwin-arm64', + acquireLease: () => { + events.push('agent-acquire'); + return { + path: '/fixture/.agent-sync.lock', + release: () => events.push('agent-release'), + }; + }, + acquireCodexLease: () => { + events.push('codex-acquire'); + return held; + }, + recoverPendingState: () => events.push('recover'), + persistSelectedChannel: async () => { + events.push('persist'); + }, + alreadyCurrent: { + attemptRepair: async (_channel, _platform, lease) => { + events.push('repair'); + expect(lease).toBe(held); + return { action: 'repaired-current' }; + }, + retireLegacyMarker: () => events.push('retire'), + }, + requireCanonicalInstall: () => { + throw new Error('same-version path reached normal delivery'); + }, + }, + ); + + expect(events).toEqual([ + 'agent-acquire', + 'codex-acquire', + 'codex-assert', + 'recover', + 'persist', + 'repair', + 'retire', + 'codex-release', + 'agent-release', + ]); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + process.exitCode = priorExitCode; + } + }); +}); + +describe('normal update publication terminal boundary', () => { + test('post-promotion publication failure is nonzero, emits one false trailer, and runs no success finalizer', async () => { + const priorExitCode = process.exitCode; + const stdout: string[] = []; + const stderr: string[] = []; + const logSpy = spyOn(console, 'log').mockImplementation((...args: unknown[]) => stdout.push(args.join(' '))); + const errorSpy = spyOn(console, 'error').mockImplementation((...args: unknown[]) => stderr.push(args.join(' '))); + let successFinalizers = 0; + process.exitCode = undefined; + try { + const complete = await runNormalUpdatePublicationBoundary( + async () => { + throw new CodexDeliveryPublicationError('delivery store is unwritable'); + }, + async () => { + successFinalizers += 1; // includes marker retirement in the real command boundary + return true; + }, + ); + expect(complete).toBe(false); + expect(Number(process.exitCode)).toBe(1); + expect(successFinalizers).toBe(0); + const output = [...stdout, ...stderr].join('\n'); + expect(output).toContain('delivery store is unwritable'); + expect(output.match(/"deliveryComplete":false/g)).toHaveLength(1); + expect(output).not.toContain('Already up to date'); + expect(output).not.toContain('Update complete'); + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + process.exitCode = priorExitCode ?? 0; + } }); }); @@ -905,13 +1292,20 @@ describe('fetchLatestManifest (G5)', () => { released_at: '2026-05-09T22:11:00Z', tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v4.260509.5', platforms: ['linux-x64-glibc', 'linux-x64-musl', 'linux-arm64', 'darwin-arm64'], + manifestBytes: '', + manifestSha256: '0'.repeat(64), }; test('parses a valid latest.json payload', async () => { + const raw = JSON.stringify({ ...validManifest, manifestBytes: undefined, manifestSha256: undefined }); const manifest = await fetchLatestManifest('stable', { - fetcher: async () => JSON.stringify(validManifest), + fetcher: async () => raw, + }); + expect(manifest).toEqual({ + ...validManifest, + manifestBytes: raw, + manifestSha256: createHash('sha256').update(raw).digest('hex'), }); - expect(manifest).toEqual(validManifest); }); test('returns null when fetcher resolves null (network failure)', async () => { @@ -942,6 +1336,35 @@ describe('fetchLatestManifest (G5)', () => { expect(manifest).toBeNull(); }); + test('returns null when channel is omitted instead of inventing the requested channel binding', async () => { + const manifest = await fetchLatestManifest('stable', { + fetcher: async () => + JSON.stringify({ ...validManifest, channel: undefined, manifestBytes: undefined, manifestSha256: undefined }), + }); + expect(manifest).toBeNull(); + }); + + test('returns null when the fetched manifest declares a different channel', async () => { + const manifest = await fetchLatestManifest('stable', { + fetcher: async () => + JSON.stringify({ ...validManifest, channel: 'dev', manifestBytes: undefined, manifestSha256: undefined }), + }); + expect(manifest).toBeNull(); + }); + + test('returns null when a platform entry is not a string', async () => { + const manifest = await fetchLatestManifest('stable', { + fetcher: async () => + JSON.stringify({ + ...validManifest, + platforms: ['darwin-arm64', 42], + manifestBytes: undefined, + manifestSha256: undefined, + }), + }); + expect(manifest).toBeNull(); + }); + test('honors timeoutMs and resolves null when fetcher hangs', async () => { const manifest = await fetchLatestManifest('stable', { timeoutMs: 30, @@ -951,6 +1374,91 @@ describe('fetchLatestManifest (G5)', () => { }); }); +describe('buildAlreadyCurrentRepairSeams — immutable manifest/asset adapter', () => { + test('the first raw-byte manifest object drives the exact tag/name/platform download; the second fetch only rechecks', async () => { + const platformId = resolvePlatformId(); + const raw = `{ + "schema_version": 1, + "channel": "dev", + "version": "${VERSION}", + "released_at": "2026-07-23T00:00:00.000Z", + "tarball_base": "https://example.invalid/releases/v${VERSION}", + "platforms": ["${platformId}"], + "ignored_future_field": "raw-byte-binding" +}\n`; + const expectedDigest = createHash('sha256').update(raw).digest('hex'); + const fetched = { object: null as LatestManifest | null }; + let fetches = 0; + let downloads = 0; + const tempRoot = mkdtempSync(join(tmpdir(), 'update-repair-adapter-')); + const tempRoots: string[] = []; + const lease: HeldLifecycleLease = { + ok: true, + operationId: 'f'.repeat(32), + kind: 'update-delivery', + assertOperation() {}, + release() {}, + }; + try { + const seams = buildAlreadyCurrentRepairSeams( + platformId, + { + version: VERSION, + pluginTreeSha256: 'a'.repeat(64), + binarySha256: 'b'.repeat(64), + deliveryRoot: '/physical/genie-home', + }, + lease, + tempRoots, + '/logical/genie-home', + { + fetchManifest: async (channel) => { + fetches += 1; + const manifest = await fetchLatestManifest(channel, { fetcher: async () => raw }); + if (fetched.object === null) fetched.object = manifest; + return manifest; + }, + createTempRoot: () => tempRoot, + downloadAndVerifyDeliveryAssets: async (manifest, platform, destination) => { + downloads += 1; + expect(manifest).toBe(fetched.object as LatestManifest); + expect(manifest.manifestSha256).toBe(expectedDigest); + expect(manifest.released_at).toBe('2026-07-23T00:00:00.000Z'); + expect(manifest.tarball_base).toBe(`https://example.invalid/releases/v${VERSION}`); + expect(platform).toBe(platformId); + return { + tarballPath: join(destination, `genie-${VERSION}-${platformId}.tar.gz`), + descriptorBytes: Buffer.from('{}'), + bundleBytes: Buffer.from('{}'), + }; + }, + }, + ); + const first = await seams.fetchManifest('dev'); + if (first === null) throw new Error('expected first pinned manifest'); + expect(first).toBe(fetched.object as LatestManifest); + await seams.downloadAndVerify( + { + channel: 'dev', + targetVersion: VERSION, + platformTriple: `${process.platform}-${process.arch}`, + platformId, + releaseTag: `v${VERSION}`, + releaseName: `genie-${VERSION}-${platformId}.tar.gz`, + }, + first, + ); + const second = await seams.fetchManifest('dev'); + expect(second?.manifestSha256).toBe(expectedDigest); + expect(fetches).toBe(2); + expect(downloads).toBe(1); + expect(tempRoots).toEqual([tempRoot]); + } finally { + rmSync(tempRoot, { recursive: true, force: true }); + } + }); +}); + describe('resolvePlatformId (G5)', () => { test('returns one of the four supported platform identifiers', () => { // Don't pin a specific value — runs in CI on linux-x64; locally on @@ -1005,6 +1513,8 @@ describe('downloadAndVerifyTarball (G5)', () => { released_at: '2026-05-09T22:11:00Z', tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v4.260509.5', platforms: ['linux-x64-glibc', 'linux-x64-musl', 'linux-arm64', 'darwin-arm64'], + manifestBytes: '{}', + manifestSha256: '0'.repeat(64), }; test('issues gh release download with the correct version tag and pattern set', async () => { @@ -1095,7 +1605,7 @@ describe('downloadAndVerifyTarball (G5)', () => { } }); - test('falls back to cosign bundle when gh attestation verify fails', async () => { + test('fails closed instead of minting delivery facts from the reduced cosign fallback', async () => { const tmp = mkdtempSync(join(tmpdir(), 'genie-update-dl-')); try { const tarballPath = join(tmp, `genie-${manifest.version}-linux-x64-glibc.tar.gz`); @@ -1114,25 +1624,16 @@ describe('downloadAndVerifyTarball (G5)', () => { return { success: true, output: '' }; }; - await expect(downloadAndVerifyTarball(manifest, 'linux-x64-glibc', tmp, { runner })).resolves.toBe(tarballPath); - expect(calls.map((call) => call.cmd)).toEqual(['gh', 'gh', 'cosign']); - expect(calls[2].args).toEqual([ - 'verify-blob', - '--bundle', - bundlePath, - '--certificate-identity-regexp', - '^https://github\\.com/automagik-dev/genie/\\.github/workflows/sign-attest\\.yml@refs/heads/main$', - '--certificate-oidc-issuer', - 'https://token.actions.githubusercontent.com', - tarballPath, - ]); - expect(calls[2].timeoutMs).toBe(30_000); + await expect(downloadAndVerifyTarball(manifest, 'linux-x64-glibc', tmp, { runner })).rejects.toThrow( + /reduced cosign verify-blob proof does not validate/, + ); + expect(calls.map((call) => call.cmd)).toEqual(['gh', 'gh']); } finally { rmSync(tmp, { recursive: true, force: true }); } }); - test('throws with both verifier errors when gh and cosign verification fail', async () => { + test('reports the primary attestation failure and never invokes cosign', async () => { const tmp = mkdtempSync(join(tmpdir(), 'genie-update-dl-')); try { const tarballPath = join(tmp, `genie-${manifest.version}-linux-x64-glibc.tar.gz`); @@ -1148,7 +1649,7 @@ describe('downloadAndVerifyTarball (G5)', () => { return { success: false, output: 'invalid signature' }; }; await expect(downloadAndVerifyTarball(manifest, 'linux-x64-glibc', tmp, { runner })).rejects.toThrow( - /cosign verify-blob: invalid signature/, + /gh attestation verify: no matching attestation/, ); } finally { rmSync(tmp, { recursive: true, force: true }); @@ -1615,8 +2116,8 @@ describe('ensureCanonicalInstall + resolveLiveBinaryPath (review fix #3)', () => const cmdStart = source.indexOf('export async function updateCommand'); expect(cmdStart).toBeGreaterThan(-1); const cmdBody = source.slice(cmdStart); - const ensureIdx = cmdBody.indexOf('requireCanonicalInstallOrExit()'); - const deliveryIdx = cmdBody.indexOf('await runDelivery('); + const ensureIdx = cmdBody.indexOf('dependencies.requireCanonicalInstall ?? ensureCanonicalInstall'); + const deliveryIdx = cmdBody.indexOf('runDelivery(resolvedManifest'); expect(ensureIdx).toBeGreaterThan(-1); expect(deliveryIdx).toBeGreaterThan(-1); // The check must run BEFORE we touch the binary on disk. @@ -1889,21 +2390,46 @@ describe('runAgentSyncSafe (agent-sync phase)', () => { } }); + test('a delivery-owned sync has no managed Codex role callback and remains strict-successful when Codex is detected', () => { + const dir = mkdtempSync(join(tmpdir(), 'genie-asm-')); + const marker = join(dir, '.last-agent-sync'); + try { + const report = makeReport(); + const codex = report.agents.find((agent) => agent.agent === 'codex'); + if (codex) codex.detected = true; + expect(() => + runAgentSyncSafe({ + sync: () => report, + strict: true, + log: () => {}, + markerPath: marker, + }), + ).not.toThrow(); + expect(existsSync(marker)).toBe(true); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + test('updateCommand runs the sync-only fast path before any network/delivery', () => { const source = readFileSync(join(import.meta.dir, '..', 'update.ts'), 'utf-8'); const cmdStart = source.indexOf('export async function updateCommand'); const cmdBody = source.slice(cmdStart); - const fastPathIdx = cmdBody.indexOf("mode !== 'normal'"); - const fetchIdx = cmdBody.indexOf('await fetchLatestManifest('); - const deliveryIdx = cmdBody.indexOf('await runDelivery('); + const fastPathIdx = cmdBody.indexOf('await dispatchNonNormalUpdateMode(options)'); + const fetchIdx = cmdBody.indexOf('dependencies.fetchManifest ?? fetchLatestManifest'); + const deliveryIdx = cmdBody.indexOf('runDelivery(resolvedManifest'); expect(fastPathIdx).toBeGreaterThan(-1); expect(fastPathIdx).toBeLessThan(fetchIdx); expect(fastPathIdx).toBeLessThan(deliveryIdx); // The compatibility mode routes before fetch and remains skills/role-only. - const fastPath = cmdBody.slice(fastPathIdx, fetchIdx); - expect(fastPath).toContain('await runExplicitUpdateMode(mode)'); - expect(fastPath).not.toContain('runTrackedManualUpdateConvergence('); - expect(fastPath).not.toContain('refreshUpdatePlugins('); + const dispatcher = source.slice( + source.indexOf('async function dispatchNonNormalUpdateMode'), + source.indexOf('async function confirmPlannedDelivery'), + ); + expect(dispatcher).toContain("mode !== 'normal'"); + expect(dispatcher).toContain('await runExplicitUpdateMode(mode)'); + expect(dispatcher).not.toContain('runTrackedManualUpdateConvergence('); + expect(dispatcher).not.toContain('refreshUpdatePlugins('); const legacyModeStart = source.indexOf('function runLegacySyncOnlyMode()'); const postDeliveryModeStart = source.indexOf('function runPostDeliveryConvergenceMode()', legacyModeStart); const legacyMode = source.slice(legacyModeStart, postDeliveryModeStart); @@ -1916,7 +2442,7 @@ describe('runAgentSyncSafe (agent-sync phase)', () => { // D2: sync-only is agent-sync ONLY — no Codex plugin query/inspection/advisory // and no plugin convergence at all. A genuine agent-sync failure is the only // nonzero result. - expect(convergence).toContain('runAgentSyncSafe({ strict: true'); + expect(convergence).toContain('runUpdateAgentSync(agentSyncSelection)'); expect(convergence).not.toContain('legacySyncOnlyPluginAdvisory'); expect(convergence).not.toContain('inspectSyncOnlyCodexHealth'); expect(convergence).not.toContain('plugin'); @@ -1940,21 +2466,74 @@ describe('manual post-update convergence (2026-07-11 cascade regression)', () => bundleRoot: '/tmp/verified-bundle', runSync: () => calls.push('parent-safe-sync'), refreshPlugins: (options) => { - calls.push(`parent-plugin-refresh:${options.expectedVersion}`); - return [{ runtime: 'codex', ok: true, detail: 'plugin/hooks refreshed' }]; + calls.push(`parent-plugin-refresh:${options.expectedVersion}:${options.selection}`); + return [{ runtime: 'claude', ok: true, detail: 'plugin refreshed' }]; }, log: (line) => calls.push(`log:${line}`), }); expect(calls[0]).toBe('parent-safe-sync'); - expect(calls[1]).toBe('parent-plugin-refresh:5.260711.3'); - expect(result.integrations).toEqual([{ runtime: 'codex', ok: true, detail: 'plugin/hooks refreshed' }]); + expect(calls[1]).toBe('parent-plugin-refresh:5.260711.3:claude'); + expect(result.integrations).toEqual([{ runtime: 'claude', ok: true, detail: 'plugin refreshed' }]); + }); + + test('structurally excludes Codex queries and writes while retaining Claude/Hermes convergence', () => { + expect(narrowUpdatePluginRefreshSelection('auto')).toBe('claude'); + expect(narrowUpdatePluginRefreshSelection('all')).toBe('claude'); + expect(narrowUpdatePluginRefreshSelection('claude')).toBe('claude'); + expect(narrowUpdatePluginRefreshSelection('codex')).toBeNull(); + expect(narrowUpdatePluginRefreshSelection('none')).toBeNull(); + + let codexOnlySyncs = 0; + let codexOnlyRefreshes = 0; + const codexOnly = runManualUpdateConvergence({ + expectedVersion: VERSION, + selection: 'codex', + runSync: () => { + codexOnlySyncs += 1; + }, + refreshPlugins: () => { + codexOnlyRefreshes += 1; + return [{ runtime: 'codex', ok: true, detail: 'must not run' }]; + }, + }); + expect(codexOnly).toEqual({ integrations: [] }); + expect(codexOnlySyncs).toBe(0); + expect(codexOnlyRefreshes).toBe(0); + + let selectedRefresh: IntegrationSelection | undefined; + const auto = runManualUpdateConvergence({ + expectedVersion: VERSION, + selection: 'auto', + runSync: () => {}, + refreshPlugins: (options) => { + selectedRefresh = options.selection; + return [ + { runtime: 'claude', ok: true, detail: 'refreshed' }, + { runtime: 'codex', ok: true, detail: 'injected boundary violation' }, + ]; + }, + log: () => {}, + }); + expect(selectedRefresh).toBe('claude'); + expect(auto.integrations).toEqual([{ runtime: 'claude', ok: true, detail: 'refreshed' }]); + }); + + test('update-owned agent sync disables setup-owned Codex role convergence', () => { + let captured: Parameters[0] | undefined; + runUpdateAgentSync('auto', (options) => { + captured = options; + return null; + }); + expect(captured?.selection).toBe('auto'); + expect(captured?.strict).toBe(true); + expect(captured?.codexRefresh).toBeUndefined(); }); test('normal delivery invokes the fresh binary only through the explicit child protocol', () => { const source = readFileSync(join(import.meta.dir, '..', 'update.ts'), 'utf-8'); expect(source).not.toMatch(/execFileSync\([^\n]+,\s*\['update'\]\s*,/); expect(source).toContain("run(binaryPath, ['update', '--post-delivery-converge'], environment)"); - const deliveryIdx = source.indexOf('await runDelivery('); + const deliveryIdx = source.indexOf('runDelivery(resolvedManifest'); const convergeIdx = source.indexOf('runFreshConvergenceOrReport(lifecycleLease)', deliveryIdx); const verifyIdx = source.indexOf('await runPostUpdateVerifySafe('); expect(deliveryIdx).toBeGreaterThan(-1); @@ -2022,9 +2601,11 @@ describe('manual post-update convergence (2026-07-11 cascade regression)', () => message = error instanceof Error ? error.message : String(error); } expect(message).toContain('fresh Genie integration convergence failed: exit 7'); - expect(message).toContain('Close all Codex tasks first'); - expect(message).toContain('external terminal'); - expect(message.indexOf('Close all Codex tasks first')).toBeLessThan(message.indexOf('external terminal')); + // Integration-neutral operator recovery: retry the update itself first + // (the rerun re-converges), THEN the Codex activation steps if pending. + expect(message).toContain('Rerun `genie update`'); + expect(message).toContain('genie setup --codex'); + expect(message.indexOf('Rerun `genie update`')).toBeLessThan(message.indexOf('genie setup --codex')); } finally { lease.release(); rmSync(home, { recursive: true, force: true }); @@ -2701,7 +3282,7 @@ describe('--sync-only is agent-sync only (D2 — wish decision 3)', () => { // Function body (past its docstring): agent-sync only, no plugin command. const fnStart = source.indexOf('export function runLegacySyncOnlyConvergence('); const body = source.slice(fnStart, source.indexOf('\n}', fnStart)); - expect(body).toContain('runAgentSyncSafe({ strict: true'); + expect(body).toContain('runUpdateAgentSync(agentSyncSelection)'); expect(body).not.toContain('plugin'); expect(body).not.toContain('probe'); expect(body).not.toContain('inspect'); @@ -2722,7 +3303,7 @@ describe('applyConvergenceExitSignal — exit 2 only on delivery-pending (D3 gua }); afterEach(() => { spy.mockRestore(); - process.exitCode = savedExitCode; + process.exitCode = savedExitCode ?? 0; }); test('a non-codex (claude) failure exits 1 and emits NO trailer', () => { @@ -2766,3 +3347,256 @@ describe('applyConvergenceExitSignal — exit 2 only on delivery-pending (D3 gua expect(logs.join('\n')).not.toContain('deliveryComplete'); }); }); + +describe('mapAlreadyCurrentRepairOutcome — pure repair→directive mapping (Group D deliverable 1)', () => { + test('an old-parent publish (activation-pending) hands off to setup', () => { + expect( + mapAlreadyCurrentRepairOutcome({ + kind: 'published', + record: { + schemaVersion: 2, + deliveryId: 'a'.repeat(32), + targetVersion: '5.260722.11', + canonicalPayloadSha256: 'a'.repeat(64), + channel: 'dev', + deliveredAt: '2026-07-22T00:00:00.000Z', + evidenceDigest: 'e'.repeat(64), + platformId: 'darwin-arm64', + platformTriple: 'darwin-arm64', + releaseTag: 'v5.260722.11', + releaseName: 'genie-5.260722.11-darwin-arm64.tar.gz', + releaseManifestSha256: 'b'.repeat(64), + artifactSha256: 'd'.repeat(64), + installedBinarySha256: 'c'.repeat(64), + deliveryRoot: '/home/test/.genie', + }, + handoff: 'activation-pending', + artifactSha256: 'd'.repeat(64), + }), + ).toEqual({ action: 'exit-handoff' }); + }); + + test('a target-current publish reports the repair as an immediate terminal handoff', () => { + expect( + mapAlreadyCurrentRepairOutcome({ + kind: 'published', + record: { + schemaVersion: 2, + deliveryId: 'a'.repeat(32), + targetVersion: '5.260722.11', + canonicalPayloadSha256: 'a'.repeat(64), + channel: 'dev', + deliveredAt: '2026-07-22T00:00:00.000Z', + evidenceDigest: 'e'.repeat(64), + platformId: 'darwin-arm64', + platformTriple: 'darwin-arm64', + releaseTag: 'v5.260722.11', + releaseName: 'genie-5.260722.11-darwin-arm64.tar.gz', + releaseManifestSha256: 'b'.repeat(64), + artifactSha256: 'd'.repeat(64), + installedBinarySha256: 'c'.repeat(64), + deliveryRoot: '/home/test/.genie', + }, + handoff: 'current', + artifactSha256: 'd'.repeat(64), + }), + ).toEqual({ action: 'repaired-current' }); + }); + + test('already-matching proceeds; channel advance routes upgrade; failure is explicit', () => { + expect(mapAlreadyCurrentRepairOutcome({ kind: 'already-matching' })).toEqual({ action: 'proceed-current' }); + const advancedManifest: PinnedManifest = { + schema_version: 1, + channel: 'dev', + version: '5.260722.12', + released_at: '2026-07-22T01:00:00Z', + tarball_base: 'https://example.invalid', + platforms: ['darwin-arm64'], + manifestBytes: '{}', + manifestSha256: 'f'.repeat(64), + }; + expect( + mapAlreadyCurrentRepairOutcome({ + kind: 'channel-advanced', + from: '5.260722.11', + to: '5.260722.12', + manifest: advancedManifest, + }), + ).toEqual({ action: 'route-upgrade', manifest: advancedManifest }); + expect( + mapAlreadyCurrentRepairOutcome({ + kind: 'failed', + stage: 'download-verify', + detail: 'x', + deliveryComplete: false, + }), + ).toEqual({ action: 'failed', detail: 'download-verify: x' }); + }); +}); + +describe('handleAlreadyCurrentUpdate — same-version terminal authority', () => { + const advancedManifest: PinnedManifest = { + schema_version: 1, + channel: 'dev', + version: '5.260722.12', + released_at: '2026-07-22T01:00:00Z', + tarball_base: 'https://example.invalid', + platforms: ['darwin-arm64'], + manifestBytes: '{"version":"5.260722.12"}', + manifestSha256: 'f'.repeat(64), + }; + + async function runDirective(directive: Awaited>) { + const priorExitCode = process.exitCode; + const output: string[] = []; + let convergenceRuns = 0; + let markerRetirements = 0; + const logSpy = spyOn(console, 'log').mockImplementation((...args: unknown[]) => output.push(args.join(' '))); + const errorSpy = spyOn(console, 'error').mockImplementation((...args: unknown[]) => output.push(args.join(' '))); + process.exitCode = 0; + try { + const manifest = await handleAlreadyCurrentUpdate('dev', 'darwin-arm64', VERSION, VERSION, { + attemptRepair: async () => directive, + runConvergence: () => { + convergenceRuns += 1; + }, + retireLegacyMarker: () => { + markerRetirements += 1; + }, + }); + return { + manifest, + output: output.join('\n'), + exitCode: process.exitCode, + convergenceRuns, + markerRetirements, + }; + } finally { + logSpy.mockRestore(); + errorSpy.mockRestore(); + process.exitCode = priorExitCode ?? 0; + } + } + + test('failed is terminal, nonzero, and performs no convergence or marker retirement', async () => { + const result = await runDirective({ action: 'failed', detail: 'download-verify: invalid provenance' }); + expect(result.manifest).toBeNull(); + expect(result.exitCode).toBe(1); + expect(result.convergenceRuns).toBe(0); + expect(result.markerRetirements).toBe(0); + expect(result.output).toContain('Codex delivery repair failed: download-verify: invalid provenance'); + expect(result.output).toContain('"deliveryComplete":false'); + expect(result.output).not.toContain('Already up to date'); + }); + + test('route-upgrade returns the exact advanced manifest without same-version finalizers', async () => { + const result = await runDirective({ action: 'route-upgrade', manifest: advancedManifest }); + expect(result.manifest).toBe(advancedManifest); + expect(result.exitCode).toBe(0); + expect(result.convergenceRuns).toBe(0); + expect(result.markerRetirements).toBe(0); + expect(result.output).toContain(`→ ${advancedManifest.version}`); + expect(result.output).not.toContain('Already up to date'); + }); + + test('exit-handoff retires only delivery metadata before the typed activation handoff', async () => { + const result = await runDirective({ action: 'exit-handoff' }); + expect(result.manifest).toBeNull(); + expect(result.exitCode).toBe(2); + expect(result.convergenceRuns).toBe(0); + expect(result.markerRetirements).toBe(1); + expect(result.output).toContain('Codex plugin activation is pending'); + expect(result.output.match(/"deliveryComplete":true/g)).toHaveLength(1); + expect(result.output).not.toContain('Already up to date'); + }); + + test('repaired-current retires delivery metadata without convergence, trailer, or success masquerade', async () => { + const result = await runDirective({ action: 'repaired-current' }); + expect(result.manifest).toBeNull(); + expect(result.exitCode).toBe(0); + expect(result.convergenceRuns).toBe(0); + expect(result.markerRetirements).toBe(1); + expect(result.output).toContain('Repaired the missing Codex delivery record'); + expect(result.output).not.toContain('deliveryComplete'); + expect(result.output).not.toContain('Already up to date'); + }); + + test('ordinary already-matching reruns retain convergence and marker-retirement semantics', async () => { + const result = await runDirective({ action: 'proceed-current' }); + expect(result.manifest).toBeNull(); + expect(result.exitCode).toBe(0); + expect(result.convergenceRuns).toBe(1); + expect(result.markerRetirements).toBe(1); + expect(result.output).toContain(`Already up to date (v${VERSION}, channel dev)`); + }); +}); + +describe('attemptAlreadyCurrentDeliveryRepair — fail-closed skip with no install (CI-portable, no network/codex)', () => { + let prevGenieHome: string | undefined; + let dir: string; + + beforeEach(() => { + prevGenieHome = process.env.GENIE_HOME; + dir = mkdtempSync(join(tmpdir(), 'update-repair-skip-')); + process.env.GENIE_HOME = dir; + }); + + afterEach(() => { + if (prevGenieHome === undefined) Reflect.deleteProperty(process.env, 'GENIE_HOME'); + else process.env.GENIE_HOME = prevGenieHome; + rmSync(dir, { recursive: true, force: true }); + }); + + test('an empty GENIE_HOME reports an explicit incomplete repair without lease/download/mutation', async () => { + // No plugins/genie payload and no binary ⇒ observeInstalledForRepair returns + // null ⇒ the repair fails closed before network or codex CLI access. + const directive = await attemptAlreadyCurrentDeliveryRepair('dev', 'linux-x64', undefined, dir); + expect(directive).toEqual({ action: 'failed', detail: 'installed payload/binary could not be observed' }); + // No lease file was created (the skip returns before lease acquisition). + expect(existsSync(join(dir, '.codex-lifecycle.lock'))).toBe(false); + // No delivery record was minted. + expect(existsSync(join(dir, '.codex-plugin-delivery-record.json'))).toBe(false); + }); + + test('a symlinked GENIE_HOME fast-path binds the physical canonical delivery root', async () => { + const physicalHome = join(dir, 'physical-genie-home'); + const logicalHome = join(dir, 'logical-genie-home'); + mkdirSync(join(physicalHome, 'plugins', 'genie'), { recursive: true }); + mkdirSync(join(physicalHome, 'bin'), { recursive: true }); + writeFileSync(join(physicalHome, 'plugins', 'genie', 'plugin.json'), '{"name":"genie"}\n'); + writeFileSync(join(physicalHome, 'VERSION'), `${VERSION}\n`); + writeFileSync(join(physicalHome, 'bin', 'genie'), '#!/bin/sh\n'); + symlinkSync(physicalHome, logicalHome); + const snapshot = observeCodexActivation({ genieHome: logicalHome, command: null }); + if (snapshot.canonical.status !== 'ok') throw new Error(snapshot.canonical.detail); + const platformId = resolvePlatformId(); + const { evidence, pack } = mintTestDeliveryEvidence({ + descriptor: { + version: snapshot.canonical.version.canonical, + channel: 'dev', + platformId: platformId as DeliveryEvidencePlatformId, + platformTriple: snapshot.canonical.platformTriple, + releaseTag: `v${VERSION}`, + releaseName: `genie-${VERSION}-${platformId}.tar.gz`, + canonicalPayloadSha256: snapshot.canonical.digest, + installedBinarySha256: snapshot.canonical.installedBinarySha256, + }, + }); + const lease = acquireCodexLifecycleLease('update-delivery', { genieHome: logicalHome }); + if (!lease.ok) throw new Error(lease.detail); + try { + openCodexActivationStore({ genieHome: logicalHome }).publishDelivery(lease, { + evidence, + deliveryRoot: snapshot.canonical.deliveryRoot, + }); + } finally { + lease.release(); + } + expect(snapshot.canonical.deliveryRoot).toBe(realpathSync(physicalHome)); + expect( + await attemptAlreadyCurrentDeliveryRepair('dev', platformId, undefined, logicalHome, { + evidenceVerification: pack.dependencies, + }), + ).toEqual({ action: 'proceed-current' }); + }); +}); diff --git a/src/genie-commands/codex-delivery-repair.test.ts b/src/genie-commands/codex-delivery-repair.test.ts new file mode 100644 index 000000000..08fd1dd29 --- /dev/null +++ b/src/genie-commands/codex-delivery-repair.test.ts @@ -0,0 +1,435 @@ +import { describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; +import type { + CodexActivationStore, + DeliveryFact, + DeliveryRecord, + PublishDeliveryInput, +} from '../lib/codex-activation.js'; +import { + type VerifiedDeliveryEvidence, + deriveDeliveryId, + verifiedDeliveryEvidenceFacts, + verifyDownloadedDeliveryEvidence, +} from '../lib/codex-delivery-evidence.js'; +import { + buildTestDeliveryEvidencePack, + mintTestDeliveryEvidence, +} from '../lib/codex-delivery-evidence.test-support.js'; +import type { HeldLifecycleLease } from '../lib/codex-lifecycle-lease.js'; +import { + type CandidateProof, + type DeliveryRepairSeams, + type DownloadedRepairAsset, + type InstalledProof, + type PinnedManifest, + type ReobservedTarget, + type RepairPinnedTarget, + buildLocalRepairExpectation, + repairMissingDelivery, +} from './codex-delivery-repair.js'; + +const T = '5.260722.11'; +const N = '5.260722.1'; +const CHANNEL = 'dev'; +const PLATFORM_ID = + process.platform === 'darwin' ? 'darwin-arm64' : process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64-glibc'; +const PLATFORM_TRIPLE = `${process.platform}-${process.arch}`; +const RELEASE_TAG = `v${T}`; +const RELEASE_NAME = `genie-${T}-${PLATFORM_ID}.tar.gz`; +const PAYLOAD_DIGEST = 'a'.repeat(64); +const BINARY_DIGEST = 'b'.repeat(64); +const ARTIFACT_DIGEST = 'd'.repeat(64); +const DELIVERY_ROOT = '/home/dev/.genie'; +const TARBALL_PATH = `/tmp/repair/${RELEASE_NAME}`; + +const PINNED: RepairPinnedTarget = { + channel: CHANNEL, + targetVersion: T, + platformTriple: PLATFORM_TRIPLE, + platformId: PLATFORM_ID, + releaseTag: RELEASE_TAG, + releaseName: RELEASE_NAME, +}; + +const INSTALLED: InstalledProof = { + version: T, + pluginTreeSha256: PAYLOAD_DIGEST, + binarySha256: BINARY_DIGEST, + deliveryRoot: DELIVERY_ROOT, +}; + +function packFor(version = T) { + return buildTestDeliveryEvidencePack({ + descriptor: { + version, + channel: CHANNEL, + artifactSha256: ARTIFACT_DIGEST, + installedBinarySha256: BINARY_DIGEST, + canonicalPayloadSha256: PAYLOAD_DIGEST, + }, + }); +} + +function pinnedManifest(version = T, changedBytes = false): PinnedManifest { + const pack = packFor(version); + const manifestBytes = changedBytes ? `${pack.manifestBytes} ` : pack.manifestBytes; + const parsed = JSON.parse(pack.manifestBytes) as Omit; + return { + ...parsed, + manifestBytes, + manifestSha256: createHash('sha256').update(manifestBytes).digest('hex'), + }; +} + +function recordForEvidence(evidence: VerifiedDeliveryEvidence, deliveryRoot = DELIVERY_ROOT): DeliveryRecord { + const facts = verifiedDeliveryEvidenceFacts(evidence); + const descriptor = facts.descriptor; + return { + schemaVersion: 2, + deliveryId: deriveDeliveryId(facts.evidenceDigest, deliveryRoot), + targetVersion: descriptor.version, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, + channel: descriptor.channel, + deliveredAt: facts.deliveredAt, + evidenceDigest: facts.evidenceDigest, + platformId: descriptor.platformId, + platformTriple: descriptor.platformTriple, + releaseTag: descriptor.releaseTag, + releaseName: descriptor.releaseName, + releaseManifestSha256: descriptor.releaseManifestSha256, + artifactSha256: descriptor.artifactSha256, + installedBinarySha256: descriptor.installedBinarySha256, + deliveryRoot, + }; +} + +/** A store that records exactly which methods were invoked and echoes the evidence-derived record. */ +function spyStore(): { store: CodexActivationStore; publishCalls: PublishDeliveryInput[]; touched: string[] } { + const publishCalls: PublishDeliveryInput[] = []; + const touched: string[] = []; + const forbid = (name: string) => () => { + touched.push(name); + throw new Error(`repair must never call ${name}`); + }; + const store: CodexActivationStore = { + observe: forbid('observe') as CodexActivationStore['observe'], + publishDelivery(_lease, input): DeliveryRecord { + touched.push('publishDelivery'); + publishCalls.push(input); + return recordForEvidence(input.evidence, input.deliveryRoot); + }, + withRevalidatedDeliveryRoot: forbid( + 'withRevalidatedDeliveryRoot', + ) as CodexActivationStore['withRevalidatedDeliveryRoot'], + beginActivation: forbid('beginActivation') as CodexActivationStore['beginActivation'], + advanceIntentPhase: forbid('advanceIntentPhase') as CodexActivationStore['advanceIntentPhase'], + finalizeActivation: forbid('finalizeActivation') as CodexActivationStore['finalizeActivation'], + quarantineIntent: forbid('quarantineIntent') as CodexActivationStore['quarantineIntent'], + }; + return { store, publishCalls, touched }; +} + +function spyLease(): HeldLifecycleLease { + return { ok: true, operationId: 'f'.repeat(32), kind: 'update-delivery', assertOperation() {}, release() {} }; +} + +interface SeamOverrides { + readDeliveryFact?: () => DeliveryFact; + observeInstalled?: () => InstalledProof; + fetchManifest?: (channel: string) => Promise; + downloadAndVerify?: (target: RepairPinnedTarget, manifest: PinnedManifest) => Promise; + hashArtifact?: (path: string) => string; + proveCandidate?: (path: string) => Promise; + verifyEvidence?: DeliveryRepairSeams['verifyEvidence']; + reobserve?: () => ReobservedTarget | null; + installedGeneration?: string | null; + store?: CodexActivationStore; +} + +/** Happy-path seams + a call log; overrides let each test tamper exactly one stage. */ +function makeSeams(overrides: SeamOverrides = {}): { seams: DeliveryRepairSeams; log: string[] } { + const log: string[] = []; + const installedGeneration = overrides.installedGeneration ?? N; + const store = overrides.store ?? spyStore().store; + const pack = packFor(); + const seams: DeliveryRepairSeams = { + readDeliveryFact: + overrides.readDeliveryFact ?? + (() => { + log.push('readDeliveryFact'); + return { status: 'absent' }; + }), + observeInstalled: + overrides.observeInstalled ?? + (() => { + log.push('observeInstalled'); + return INSTALLED; + }), + fetchManifest: + overrides.fetchManifest ?? + (async (channel) => { + log.push(`fetchManifest:${channel}`); + return pinnedManifest(); + }), + downloadAndVerify: + overrides.downloadAndVerify ?? + (async (target, manifest) => { + log.push(`downloadAndVerify:${target.releaseName}:${manifest.manifestSha256}`); + return { tarballPath: TARBALL_PATH, descriptorBytes: pack.descriptorBytes, bundleBytes: pack.bundleBytes }; + }), + hashArtifact: + overrides.hashArtifact ?? + ((path) => { + log.push(`hashArtifact:${path}`); + return ARTIFACT_DIGEST; + }), + proveCandidate: + overrides.proveCandidate ?? + (async (path) => { + log.push(`proveCandidate:${path}`); + return { version: T, pluginTreeSha256: PAYLOAD_DIGEST, binarySha256: BINARY_DIGEST }; + }), + verifyEvidence: + overrides.verifyEvidence ?? + ((input) => { + log.push('verifyEvidence'); + return verifyDownloadedDeliveryEvidence(input, pack.dependencies); + }), + reobserve: + overrides.reobserve ?? + (() => { + log.push('reobserve'); + return { + installedGeneration, + canonicalVersion: T, + canonicalPayloadSha256: PAYLOAD_DIGEST, + installedBinarySha256: BINARY_DIGEST, + deliveryRoot: DELIVERY_ROOT, + }; + }), + store, + lease: spyLease(), + }; + return { seams, log }; +} + +function matchingDeliveryFact(): DeliveryFact { + const { evidence } = mintTestDeliveryEvidence({ + descriptor: { + version: T, + channel: CHANNEL, + artifactSha256: ARTIFACT_DIGEST, + installedBinarySha256: BINARY_DIGEST, + canonicalPayloadSha256: PAYLOAD_DIGEST, + }, + }); + return { + status: 'present', + record: recordForEvidence(evidence), + evidence: verifiedDeliveryEvidenceFacts(evidence), + }; +} + +describe('same-version delivery repair', () => { + test('builds its expectation from independently verified evidence plus local physical bytes', () => { + const fact = matchingDeliveryFact(); + if (fact.status !== 'present') throw new Error('expected present fixture'); + const expectation = buildLocalRepairExpectation(PINNED, INSTALLED, fact.evidence); + const { schemaVersion: _schemaVersion, ...recordExpectation } = fact.record; + expect(expectation).toEqual(recordExpectation); + }); + + test('matching evidence takes the no-network, no-publication fast path', async () => { + const spy = spyStore(); + const { seams, log } = makeSeams({ store: spy.store, readDeliveryFact: matchingDeliveryFact }); + expect(await repairMissingDelivery(PINNED, seams)).toEqual({ kind: 'already-matching' }); + expect(spy.publishCalls).toHaveLength(0); + expect(spy.touched).toEqual([]); + expect(log.some((entry) => entry.startsWith('fetchManifest'))).toBe(false); + expect(log.some((entry) => entry.startsWith('downloadAndVerify'))).toBe(false); + }); + + test('verifies exact evidence, publishes once, preserves N, and reports activation pending', async () => { + const spy = spyStore(); + const { seams, log } = makeSeams({ store: spy.store, installedGeneration: N }); + const outcome = await repairMissingDelivery(PINNED, seams); + expect(outcome).toMatchObject({ + kind: 'published', + handoff: 'activation-pending', + artifactSha256: ARTIFACT_DIGEST, + }); + expect(spy.publishCalls).toHaveLength(1); + expect(spy.publishCalls[0].deliveryRoot).toBe(DELIVERY_ROOT); + expect(verifiedDeliveryEvidenceFacts(spy.publishCalls[0].evidence).descriptor).toMatchObject({ + version: T, + channel: CHANNEL, + platformId: PLATFORM_ID, + artifactSha256: ARTIFACT_DIGEST, + installedBinarySha256: BINARY_DIGEST, + canonicalPayloadSha256: PAYLOAD_DIGEST, + }); + expect(spy.touched).toEqual(['publishDelivery']); + expect(log.indexOf(`fetchManifest:${CHANNEL}`)).toBeLessThan( + log.findIndex((entry) => entry.startsWith('downloadAndVerify')), + ); + expect(log.findIndex((entry) => entry.startsWith('downloadAndVerify'))).toBeLessThan( + log.findIndex((entry) => entry.startsWith('hashArtifact')), + ); + expect(log.indexOf('verifyEvidence')).toBeLessThan(log.indexOf('reobserve')); + }); + + test('a current registered generation reports current after publication', async () => { + const { seams } = makeSeams({ installedGeneration: T }); + const outcome = await repairMissingDelivery(PINNED, seams); + expect(outcome.kind).toBe('published'); + if (outcome.kind === 'published') expect(outcome.handoff).toBe('current'); + }); + + test('channel advance before or after download routes to the ordinary upgrade path without publishing', async () => { + for (const advanceOn of [1, 2]) { + const spy = spyStore(); + let calls = 0; + const { seams } = makeSeams({ + store: spy.store, + fetchManifest: async () => { + calls += 1; + return calls >= advanceOn ? pinnedManifest('5.260722.12') : pinnedManifest(); + }, + }); + const outcome = await repairMissingDelivery(PINNED, seams); + expect(outcome).toMatchObject({ kind: 'channel-advanced', from: T, to: '5.260722.12' }); + expect(spy.publishCalls).toHaveLength(0); + } + }); +}); + +describe('same-version repair failure matrix', () => { + async function expectUnchangedFailure(overrides: SeamOverrides, stage: string): Promise { + const spy = spyStore(); + const { seams } = makeSeams({ ...overrides, store: spy.store }); + const outcome = await repairMissingDelivery(PINNED, seams); + expect(outcome).toMatchObject({ kind: 'failed', stage, deliveryComplete: false }); + expect(spy.publishCalls).toHaveLength(0); + expect(spy.touched).toEqual([]); + } + + test('manifest pin failures are closed before download', async () => { + await expectUnchangedFailure({ fetchManifest: async () => null }, 'manifest-pin'); + await expectUnchangedFailure( + { fetchManifest: async () => ({ ...pinnedManifest(), channel: 'stable' }) }, + 'manifest-pin', + ); + await expectUnchangedFailure( + { fetchManifest: async () => ({ ...pinnedManifest(), platforms: ['linux-arm64'] }) }, + 'manifest-pin', + ); + }); + + test('download, digest, extraction, candidate, and signed-evidence failures never publish', async () => { + await expectUnchangedFailure( + { downloadAndVerify: async () => Promise.reject(new Error('signature verification failed')) }, + 'download-verify', + ); + await expectUnchangedFailure( + { + hashArtifact: () => { + throw new Error('cannot read tarball'); + }, + }, + 'artifact-digest', + ); + await expectUnchangedFailure( + { proveCandidate: async () => Promise.reject(new Error('symlink in payload')) }, + 'extract-prove', + ); + await expectUnchangedFailure( + { + proveCandidate: async () => ({ + version: '5.260722.9', + pluginTreeSha256: PAYLOAD_DIGEST, + binarySha256: BINARY_DIGEST, + }), + }, + 'candidate-version', + ); + await expectUnchangedFailure( + { + proveCandidate: async () => ({ + version: T, + pluginTreeSha256: '9'.repeat(64), + binarySha256: BINARY_DIGEST, + }), + }, + 'candidate-payload', + ); + await expectUnchangedFailure( + { + proveCandidate: async () => ({ + version: T, + pluginTreeSha256: PAYLOAD_DIGEST, + binarySha256: '9'.repeat(64), + }), + }, + 'candidate-binary', + ); + await expectUnchangedFailure( + { + verifyEvidence: () => { + throw new Error('descriptor signature invalid'); + }, + }, + 'evidence-verify', + ); + }); + + test('manifest-byte drift, reobservation drift, and fenced publication fail closed', async () => { + let calls = 0; + await expectUnchangedFailure( + { + fetchManifest: async () => { + calls += 1; + return calls === 1 ? pinnedManifest() : pinnedManifest(T, true); + }, + }, + 'channel-recheck', + ); + await expectUnchangedFailure({ reobserve: () => null }, 'reobserve'); + await expectUnchangedFailure( + { + reobserve: () => ({ + installedGeneration: N, + canonicalVersion: T, + canonicalPayloadSha256: PAYLOAD_DIGEST, + installedBinarySha256: '9'.repeat(64), + deliveryRoot: DELIVERY_ROOT, + }), + }, + 'reobserve', + ); + const fencedStore: CodexActivationStore = { + ...spyStore().store, + publishDelivery() { + throw new Error('codex lifecycle transition fenced'); + }, + }; + const { seams } = makeSeams({ store: fencedStore }); + expect(await repairMissingDelivery(PINNED, seams)).toMatchObject({ + kind: 'failed', + stage: 'publish', + deliveryComplete: false, + }); + }); + + test('record/evidence mismatch is repaired rather than accepted', async () => { + const fact = matchingDeliveryFact(); + if (fact.status !== 'present') throw new Error('expected present fixture'); + const stale: DeliveryFact = { + ...fact, + record: { ...fact.record, platformTriple: `${fact.record.platformTriple}-stale` }, + }; + const { seams, log } = makeSeams({ readDeliveryFact: () => stale, installedGeneration: T }); + expect((await repairMissingDelivery(PINNED, seams)).kind).toBe('published'); + expect(log.some((entry) => entry.startsWith('downloadAndVerify'))).toBe(true); + }); +}); diff --git a/src/genie-commands/codex-delivery-repair.ts b/src/genie-commands/codex-delivery-repair.ts new file mode 100644 index 000000000..bd3dbb8c5 --- /dev/null +++ b/src/genie-commands/codex-delivery-repair.ts @@ -0,0 +1,405 @@ +/** + * Group D — immutable same-version delivery repair. + * + * A machine can hold the delivered generation T with NO readable authenticated + * delivery record: `install.sh`/an interrupted convergence left the binary at T + * but never published the record, so setup exits `delivery-incomplete` and an + * already-current `genie update` used to return before repairing it. This module + * lets update/install publish that MISSING record EXACTLY ONCE for the installed + * target, without performing activation and without redefining the target from a + * moving channel. + * + * The hazard this closes (Decision 10): installed bytes cannot attest to + * themselves. So the repair does NOT relabel local bytes — it re-fetches the + * exact signed artifact and proves it reproduces the installed bytes: + * + * 1. Pin channel + immutable target version/platform + release tag/name + + * fetched release-manifest bytes/digest BEFORE any asset download. The + * manifest is NOT a source of the artifact digest. + * 2. Download the exact named asset, compute its SHA-256 AFTER download, and + * authenticate that digest through the existing GitHub attestation/cosign + * trust anchors (the injected `downloadAndVerify` rejects on any failure). + * 3. Extract privately and prove the candidate VERSION / binary / plugin tree + * against the canonical installed bytes. + * 4. Recheck the pinned channel under the already-held lifecycle lease. A + * channel advance routes to ordinary upgrade and mints NO record for stale + * bytes; otherwise reobserve and publish once through the existing deep + * delivery store, persisting the computed authenticated digest. + * + * A no-network fast path returns `already-matching` when a record already binds + * the locally-known pinned tuple, so a repeated repair neither downloads nor + * republishes. EVERY verification/lease/reobservation failure returns before the + * single publish, so all delivery/journal/plugin/cache/config/role state is left + * unchanged with `deliveryComplete: false`. Publishing writes only the delivery + * record: it preserves the old registered generation and performs no + * activation-owned prompt, journal, enabled-state, plugin, or role mutation. + */ + +import type { CodexActivationStore, DeliveryFact, DeliveryRecord } from '../lib/codex-activation.js'; +import { + type AuthenticatedDeliveryRecordFields, + type DeliveryEvidenceChannel, + type DeliveryEvidencePlatformId, + type VerifiedDeliveryEvidence, + type VerifiedDeliveryEvidenceFacts, + type VerifyDownloadedDeliveryEvidenceInput, + authenticatedDeliveryBindingFromRecord, + authenticatedDeliveryRecordFields, + createAuthenticatedDeliveryBinding, +} from '../lib/codex-delivery-evidence.js'; +import type { HeldLifecycleLease } from '../lib/codex-lifecycle-lease.js'; +import { classifyCodexDelivery } from './codex-delivery.js'; + +/** The immutable target pinned before any download; every field is locally known at pin time. */ +export interface RepairPinnedTarget { + channel: DeliveryEvidenceChannel; + /** The installed/canonical VERSION being repaired (never a moving-channel value). */ + targetVersion: string; + /** `process.platform-process.arch` (e.g. `linux-x64`, `darwin-arm64`). */ + platformTriple: string; + /** Exact release-manifest platform/asset identifier. */ + platformId: string; + /** Release tag, e.g. `v5.260722.11`. */ + releaseTag: string; + /** Exact named asset, e.g. `genie-5.260722.11-darwin-arm64.tar.gz`. */ + releaseName: string; +} + +/** The exact fetched release-manifest snapshot pinned/rechecked by the repair. */ +export interface PinnedManifest { + schema_version: number; + channel: DeliveryEvidenceChannel; + version: string; + released_at: string; + tarball_base: string; + platforms: string[]; + /** Exact fetched manifest bytes retained for the signed evidence pack. */ + manifestBytes: string; + /** SHA-256 of the fetched manifest bytes (NOT an artifact digest source). */ + manifestSha256: string; +} + +/** What a freshly extracted candidate tarball proves about itself. */ +export interface CandidateProof { + version: string; + pluginTreeSha256: string; + binarySha256: string; +} + +/** The canonical installed bytes the candidate is proven against — all locally computed. */ +export interface InstalledProof { + version: string; + /** The canonical installed plugin-payload digest (equals a matching record's `canonicalPayloadSha256`). */ + pluginTreeSha256: string; + binarySha256: string; + deliveryRoot: string; +} + +/** Re-observed reality immediately before the single publish. */ +export interface ReobservedTarget { + /** Installed plugin generation N from a live query (null = absent registration). */ + installedGeneration: string | null; + canonicalVersion: string; + canonicalPayloadSha256: string; + installedBinarySha256: string; + deliveryRoot: string; +} + +/** Exact downloaded release bytes retained until the signed evidence is verified. */ +export interface DownloadedRepairAsset { + tarballPath: string; + descriptorBytes: Uint8Array | string; + bundleBytes: Uint8Array | string; +} + +/** Injected I/O seams so the orchestrator is pure of network/codex/home coupling in tests. */ +export interface DeliveryRepairSeams { + /** Current on-disk record plus independently reverified evidence pack. Local, no network. */ + readDeliveryFact(): DeliveryFact; + /** The canonical installed bytes — all locally computed; used for the fast path AND candidate proof. */ + observeInstalled(): InstalledProof; + /** Fetch the pinned manifest for the channel (pre-download pin AND under-lease recheck). null = unavailable. */ + fetchManifest(channel: string): Promise; + /** Download + attestation-verify the exact named asset; resolves to the local path, rejects on any verify failure. */ + downloadAndVerify(target: RepairPinnedTarget, manifest: PinnedManifest): Promise; + /** SHA-256 of the downloaded artifact, computed AFTER download. */ + hashArtifact(tarballPath: string): string; + /** Extract privately and prove the candidate tree; rejects on unsafe extraction. */ + proveCandidate(tarballPath: string): Promise; + /** Mint opaque evidence only after every exact downloaded/candidate binding passes. */ + verifyEvidence(input: VerifyDownloadedDeliveryEvidenceInput): VerifiedDeliveryEvidence; + /** Re-observe the installed generation + canonical digest immediately before publish. null = unobservable. */ + reobserve(): ReobservedTarget | null; + /** The deep delivery store, opened by the caller under the held lease. */ + store: CodexActivationStore; + /** The held lifecycle lease (parent-owned; the repair never acquires its own). */ + lease: HeldLifecycleLease; +} + +/** After a successful publish, whether setup will find the target current or still pending activation. */ +export type RepairHandoff = 'activation-pending' | 'current'; + +export type RepairFailureStage = + | 'manifest-pin' + | 'download-verify' + | 'artifact-digest' + | 'extract-prove' + | 'candidate-version' + | 'candidate-payload' + | 'candidate-binary' + | 'evidence-verify' + | 'channel-recheck' + | 'reobserve' + | 'publish'; + +export type DeliveryRepairOutcome = + | { kind: 'already-matching' } + | { kind: 'published'; record: DeliveryRecord; handoff: RepairHandoff; artifactSha256: string } + | { kind: 'channel-advanced'; from: string; to: string; manifest: PinnedManifest } + | { kind: 'failed'; stage: RepairFailureStage; detail: string; deliveryComplete: false }; + +/** + * Build the complete matching expectation for the no-network fast path. Values + * that cannot be recomputed locally remain bound to the one structurally valid + * authenticated record; the locally observable tuple must still match exactly. + */ +export function buildLocalRepairExpectation( + pinned: RepairPinnedTarget, + installed: InstalledProof, + evidence: VerifiedDeliveryEvidenceFacts, +): AuthenticatedDeliveryRecordFields { + if (!evidenceMatchesPinned(evidence, pinned) || !evidenceMatchesInstalled(evidence, installed)) { + throw new Error('verified delivery evidence does not match the pinned installed target'); + } + return authenticatedDeliveryRecordFields(createAuthenticatedDeliveryBinding(evidence, installed.deliveryRoot)); +} + +/** + * Repair a missing/non-matching delivery record for the installed target exactly + * once. Returns `already-matching` (no network) when a record already binds the + * local tuple, `channel-advanced` when the pinned channel moved (route to ordinary + * upgrade), `published` on the one successful publication, or `failed` with every + * durable state left unchanged. + */ +export async function repairMissingDelivery( + pinned: RepairPinnedTarget, + seams: DeliveryRepairSeams, +): Promise { + const installed = seams.observeInstalled(); + // No-network fast path: an already-bound record needs no download or publish. + const existing = seams.readDeliveryFact(); + if (localDeliveryMatches(existing, pinned, installed)) { + return { kind: 'already-matching' }; + } + + // Pin the manifest BEFORE any asset download; the manifest is not the artifact digest. + const pinnedManifest = await seams.fetchManifest(pinned.channel); + if (pinnedManifest === null) { + return fail('manifest-pin', 'release manifest unavailable; cannot pin the repair target'); + } + const pinMismatch = manifestTargetMismatch(pinnedManifest, pinned); + if (pinMismatch !== null) return fail('manifest-pin', pinMismatch); + const preAdvance = channelAdvance(pinnedManifest, pinned.targetVersion); + if (preAdvance !== null) return preAdvance; + + const verified = await downloadAndProve(pinned, pinnedManifest, installed, seams); + if (verified.kind !== 'ok') return verified.outcome; + + // Recheck the pinned channel under the held lease immediately before publication. + const recheck = await seams.fetchManifest(pinned.channel); + if (recheck === null) return fail('channel-recheck', 'release manifest unavailable at the under-lease recheck'); + const recheckMismatch = manifestTargetMismatch(recheck, pinned); + if (recheckMismatch !== null) return fail('channel-recheck', recheckMismatch); + const advance = channelAdvance(recheck, pinned.targetVersion); + if (advance !== null) return advance; + // Same version but different manifest bytes is manifest tampering — fail closed. + if (recheck.manifestSha256 !== pinnedManifest.manifestSha256) { + return fail('channel-recheck', 'release manifest bytes changed under the lease without a version advance'); + } + + return publishRepair(pinned, installed, verified, seams); +} + +interface VerifiedCandidate { + kind: 'ok'; + artifactSha256: string; + candidate: CandidateProof; + evidence: VerifiedDeliveryEvidence; +} + +/** Download → hash-after → extract/prove → prove-against-installed. Any failure returns before publish. */ +async function downloadAndProve( + pinned: RepairPinnedTarget, + pinnedManifest: PinnedManifest, + installed: InstalledProof, + seams: DeliveryRepairSeams, +): Promise { + let downloaded: DownloadedRepairAsset; + try { + downloaded = await seams.downloadAndVerify(pinned, pinnedManifest); + } catch (error) { + return { kind: 'fail', outcome: fail('download-verify', errorText(error)) }; + } + let artifactSha256: string; + try { + artifactSha256 = seams.hashArtifact(downloaded.tarballPath); + } catch (error) { + return { kind: 'fail', outcome: fail('artifact-digest', errorText(error)) }; + } + let candidate: CandidateProof; + try { + candidate = await seams.proveCandidate(downloaded.tarballPath); + } catch (error) { + return { kind: 'fail', outcome: fail('extract-prove', errorText(error)) }; + } + const mismatch = proveCandidateAgainstInstalled(candidate, installed, pinned); + if (mismatch !== null) return { kind: 'fail', outcome: mismatch }; + let evidence: VerifiedDeliveryEvidence; + try { + evidence = seams.verifyEvidence({ + descriptorBytes: downloaded.descriptorBytes, + bundleBytes: downloaded.bundleBytes, + manifestBytes: pinnedManifest.manifestBytes, + targetVersion: candidate.version, + channel: pinned.channel, + platformId: pinned.platformId as DeliveryEvidencePlatformId, + platformTriple: pinned.platformTriple, + releaseTag: pinned.releaseTag, + releaseName: pinned.releaseName, + artifactSha256, + installedBinarySha256: candidate.binarySha256, + canonicalPayloadSha256: candidate.pluginTreeSha256, + }); + } catch (error) { + return { kind: 'fail', outcome: fail('evidence-verify', errorText(error)) }; + } + return { kind: 'ok', artifactSha256, candidate, evidence }; +} + +/** Independently signed evidence + local physical bytes form the no-network matching proof. */ +export function localDeliveryMatches( + fact: DeliveryFact, + pinned: RepairPinnedTarget, + installed: InstalledProof, +): boolean { + if ( + fact.status !== 'present' || + !evidenceMatchesPinned(fact.evidence, pinned) || + !evidenceMatchesInstalled(fact.evidence, installed) + ) { + return false; + } + return authenticatedDeliveryBindingFromRecord(fact.record, fact.evidence, installed.deliveryRoot) !== null; +} + +function evidenceMatchesInstalled(evidence: VerifiedDeliveryEvidenceFacts, installed: InstalledProof): boolean { + const descriptor = evidence.descriptor; + return ( + descriptor.version === installed.version && + descriptor.canonicalPayloadSha256 === installed.pluginTreeSha256 && + descriptor.installedBinarySha256 === installed.binarySha256 + ); +} + +function evidenceMatchesPinned(evidence: VerifiedDeliveryEvidenceFacts, pinned: RepairPinnedTarget): boolean { + const descriptor = evidence.descriptor; + return ( + descriptor.version === pinned.targetVersion && + descriptor.channel === pinned.channel && + descriptor.platformId === pinned.platformId && + descriptor.platformTriple === pinned.platformTriple && + descriptor.releaseTag === pinned.releaseTag && + descriptor.releaseName === pinned.releaseName + ); +} + +/** The fetched snapshot must name the selected channel and exact target platform. */ +function manifestTargetMismatch(manifest: PinnedManifest, pinned: RepairPinnedTarget): string | null { + if (manifest.channel !== pinned.channel) { + return `release manifest channel ${manifest.channel} differs from selected ${pinned.channel}`; + } + if (!manifest.platforms.includes(pinned.platformId)) { + return `release manifest does not contain target platform ${pinned.platformId}`; + } + return null; +} + +/** The candidate must reproduce the installed target exactly: version, payload tree, and binary. */ +function proveCandidateAgainstInstalled( + candidate: CandidateProof, + installed: InstalledProof, + pinned: RepairPinnedTarget, +): DeliveryRepairOutcome | null { + if (candidate.version !== pinned.targetVersion || candidate.version !== installed.version) { + return fail('candidate-version', `candidate version ${candidate.version} differs from installed target`); + } + if (candidate.pluginTreeSha256 !== installed.pluginTreeSha256) { + return fail('candidate-payload', 'candidate plugin tree differs from the canonical installed payload'); + } + if (candidate.binarySha256 !== installed.binarySha256) { + return fail('candidate-binary', 'candidate binary differs from the installed binary'); + } + return null; +} + +/** Reobserve, then publish the single authenticated record; N is preserved (no plugin/cache mutation). */ +function publishRepair( + pinned: RepairPinnedTarget, + installed: InstalledProof, + verified: VerifiedCandidate, + seams: DeliveryRepairSeams, +): DeliveryRepairOutcome { + const reobserved = seams.reobserve(); + if (reobserved === null) return fail('reobserve', 'installed state unobservable before publication'); + if ( + reobserved.canonicalVersion !== pinned.targetVersion || + reobserved.canonicalPayloadSha256 !== installed.pluginTreeSha256 || + reobserved.installedBinarySha256 !== installed.binarySha256 || + reobserved.deliveryRoot !== installed.deliveryRoot + ) { + return fail('reobserve', 'installed bytes changed between candidate proof and publication'); + } + let record: DeliveryRecord; + try { + record = seams.store.publishDelivery(seams.lease, { + evidence: verified.evidence, + deliveryRoot: reobserved.deliveryRoot, + }); + } catch (error) { + return fail('publish', errorText(error)); + } + return { + kind: 'published', + record, + handoff: handoffFor(reobserved.installedGeneration, pinned.targetVersion), + artifactSha256: verified.artifactSha256, + }; +} + +/** + * A channel advance is a target-version change (someone published a newer + * generation). It routes to ordinary upgrade and mints NO same-version record for + * stale installed bytes. A manifest whose version equals the pinned target is not + * an advance. + */ +function channelAdvance(manifest: PinnedManifest, targetVersion: string): DeliveryRepairOutcome | null { + if (manifest.version === targetVersion) return null; + const order = classifyCodexDelivery(targetVersion, manifest.version); + // Any non-equal parseable pair (upgrade or downgrade) is an advance; an + // unparseable manifest version is fail-closed as an advance so no stale record mints. + if (order.kind === 'current') return null; + return { kind: 'channel-advanced', from: targetVersion, to: manifest.version, manifest }; +} + +/** After publish, setup finds `current` only when the registered generation already equals the target. */ +function handoffFor(installedGeneration: string | null, targetVersion: string): RepairHandoff { + return installedGeneration === targetVersion ? 'current' : 'activation-pending'; +} + +function fail(stage: RepairFailureStage, detail: string): DeliveryRepairOutcome { + return { kind: 'failed', stage, detail, deliveryComplete: false }; +} + +function errorText(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} diff --git a/src/genie-commands/codex-delivery.test.ts b/src/genie-commands/codex-delivery.test.ts index f7dedea8d..5cf673d87 100644 --- a/src/genie-commands/codex-delivery.test.ts +++ b/src/genie-commands/codex-delivery.test.ts @@ -1,5 +1,12 @@ import { describe, expect, test } from 'bun:test'; import type { CodexActivationStore, DeliveryRecord, PublishDeliveryInput } from '../lib/codex-activation.js'; +import { + type DeliveryEvidenceChannel, + type VerifiedDeliveryEvidence, + deriveDeliveryId, + verifiedDeliveryEvidenceFacts, +} from '../lib/codex-delivery-evidence.js'; +import { mintTestDeliveryEvidence } from '../lib/codex-delivery-evidence.test-support.js'; import type { HeldLifecycleLease } from '../lib/codex-lifecycle-lease.js'; import { CODEX_DELIVERY_RESULT_TRAILER, @@ -13,6 +20,35 @@ import { const N = '5.260711.6'; const T = '5.260712.1'; const DIGEST = 'a'.repeat(64); +const DELIVERY_ROOT = '/home/test/.genie'; + +function testEvidence(version = T, channel: DeliveryEvidenceChannel = 'dev'): VerifiedDeliveryEvidence { + return mintTestDeliveryEvidence({ + descriptor: { version, channel, canonicalPayloadSha256: DIGEST }, + }).evidence; +} + +function authenticatedRecord(evidence = testEvidence()): DeliveryRecord { + const facts = verifiedDeliveryEvidenceFacts(evidence); + const descriptor = facts.descriptor; + return { + schemaVersion: 2, + deliveryId: deriveDeliveryId(facts.evidenceDigest, DELIVERY_ROOT), + targetVersion: descriptor.version, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, + channel: descriptor.channel, + deliveredAt: facts.deliveredAt, + evidenceDigest: facts.evidenceDigest, + platformId: descriptor.platformId, + platformTriple: descriptor.platformTriple, + releaseTag: descriptor.releaseTag, + releaseName: descriptor.releaseName, + releaseManifestSha256: descriptor.releaseManifestSha256, + artifactSha256: descriptor.artifactSha256, + installedBinarySha256: descriptor.installedBinarySha256, + deliveryRoot: DELIVERY_ROOT, + }; +} describe('classifyCodexDelivery — the one delivery classifier', () => { test('absent installed generation', () => { @@ -33,45 +69,69 @@ describe('classifyCodexDelivery — the one delivery classifier', () => { }); }); -describe('buildDeliveryPublication — publish facts only for pending', () => { +describe('buildDeliveryPublication — publish only opaque verified evidence', () => { test('upgrade publishes without a downgradeFrom', () => { - expect( - buildDeliveryPublication({ - installedVersion: N, - targetVersion: T, - canonicalPayloadSha256: DIGEST, - channel: 'dev', - }), - ).toEqual({ targetVersion: T, canonicalPayloadSha256: DIGEST, channel: 'dev' }); + const evidence = testEvidence(); + expect(buildDeliveryPublication({ installedVersion: N, evidence, deliveryRoot: DELIVERY_ROOT })).toEqual({ + evidence, + deliveryRoot: DELIVERY_ROOT, + }); }); + test('downgrade binds downgradeFrom = N', () => { - expect( - buildDeliveryPublication({ - installedVersion: T, - targetVersion: N, - canonicalPayloadSha256: DIGEST, - channel: 'stable', - }), - ).toEqual({ targetVersion: N, canonicalPayloadSha256: DIGEST, channel: 'stable', downgradeFrom: T }); + const evidence = testEvidence(N, 'stable'); + expect(buildDeliveryPublication({ installedVersion: T, evidence, deliveryRoot: DELIVERY_ROOT })).toEqual({ + evidence, + deliveryRoot: DELIVERY_ROOT, + downgradeFrom: T, + }); }); - test('absent and current publish nothing', () => { - expect( - buildDeliveryPublication({ - installedVersion: null, - targetVersion: T, - canonicalPayloadSha256: DIGEST, - channel: 'dev', - }), - ).toBeNull(); + + test('absent-N publishes the delivery facts (fresh host: setup gate needs the record); no downgrade binding', () => { + const evidence = testEvidence(); + expect(buildDeliveryPublication({ installedVersion: null, evidence, deliveryRoot: DELIVERY_ROOT })).toEqual({ + evidence, + deliveryRoot: DELIVERY_ROOT, + }); + }); + + test('current without a record read-state republishes fail-closed because matching cannot be proved', () => { + const evidence = testEvidence(); + expect(buildDeliveryPublication({ installedVersion: T, evidence, deliveryRoot: DELIVERY_ROOT })).toEqual({ + evidence, + deliveryRoot: DELIVERY_ROOT, + }); + }); + + test('current with a MATCHING record never republishes (idempotent)', () => { + const evidence = testEvidence(); expect( buildDeliveryPublication({ installedVersion: T, - targetVersion: T, - canonicalPayloadSha256: DIGEST, - channel: 'dev', + evidence, + deliveryRoot: DELIVERY_ROOT, + existingRecord: { status: 'present', record: authenticatedRecord(evidence) }, }), ).toBeNull(); }); + + test('current with a STALE or absent record publishes the delivered facts (2026-07-23 live-QA regression)', () => { + const evidence = testEvidence(); + const stale = { + status: 'present' as const, + record: authenticatedRecord(testEvidence('5.260711.9')), + }; + for (const existingRecord of [stale, { status: 'absent' as const }]) { + expect( + buildDeliveryPublication({ + installedVersion: T, + evidence, + deliveryRoot: DELIVERY_ROOT, + existingRecord, + }), + ).toEqual({ evidence, deliveryRoot: DELIVERY_ROOT }); + } + }); }); /** A store that flags any activation-time call — C must only ever call publishDelivery. */ @@ -85,14 +145,7 @@ function spyStore(): { store: CodexActivationStore; publishCalls: PublishDeliver }, publishDelivery(_lease, input): DeliveryRecord { publishCalls.push(input); - return { - schemaVersion: 1, - deliveryId: input.deliveryId ?? 'd'.repeat(32), - targetVersion: input.targetVersion, - canonicalPayloadSha256: input.canonicalPayloadSha256, - channel: input.channel, - deliveredAt: '2026-07-21T00:00:00.000Z', - }; + return authenticatedRecord(input.evidence); }, withRevalidatedDeliveryRoot() { forbidden.push('withRevalidatedDeliveryRoot'); @@ -125,33 +178,32 @@ function spyLease(): HeldLifecycleLease { describe('publishCodexDelivery — parent publishes facts, nothing else', () => { test('pending upgrade publishes exactly one delivery record and no receipt', () => { const { store, publishCalls, forbidden } = spyStore(); + const evidence = testEvidence(); const result = publishCodexDelivery({ lease: spyLease(), store, installedVersion: N, - targetVersion: T, - canonicalPayloadSha256: DIGEST, - channel: 'dev', - deliveryId: 'c'.repeat(32), + evidence, + deliveryRoot: DELIVERY_ROOT, }); expect(result.published).toBe(true); expect(result.wroteDowngradeReceipt).toBe(false); expect(result.state).toEqual({ kind: 'pending', direction: 'upgrade' }); expect(publishCalls).toHaveLength(1); expect(publishCalls[0].downgradeFrom).toBeUndefined(); - expect(publishCalls[0].deliveryId).toBe('c'.repeat(32)); + expect(publishCalls[0]).toEqual({ evidence, deliveryRoot: DELIVERY_ROOT }); expect(forbidden).toEqual([]); }); test('pending downgrade publishes with the receipt-binding downgradeFrom', () => { const { store, publishCalls, forbidden } = spyStore(); + const evidence = testEvidence(N, 'stable'); const result = publishCodexDelivery({ lease: spyLease(), store, installedVersion: T, - targetVersion: N, - canonicalPayloadSha256: DIGEST, - channel: 'stable', + evidence, + deliveryRoot: DELIVERY_ROOT, }); expect(result.published).toBe(true); expect(result.wroteDowngradeReceipt).toBe(true); @@ -159,21 +211,56 @@ describe('publishCodexDelivery — parent publishes facts, nothing else', () => expect(forbidden).toEqual([]); }); - test('absent and current publish nothing and never touch activation state', () => { - for (const installed of [null, T]) { - const { store, publishCalls, forbidden } = spyStore(); - const result = publishCodexDelivery({ - lease: spyLease(), - store, - installedVersion: installed, - targetVersion: T, - canonicalPayloadSha256: DIGEST, - channel: 'dev', - }); - expect(result.published).toBe(false); - expect(publishCalls).toHaveLength(0); - expect(forbidden).toEqual([]); - } + test('current without observable record state republishes fail-closed and never touches activation state', () => { + const { store, publishCalls, forbidden } = spyStore(); + const evidence = testEvidence(); + const result = publishCodexDelivery({ + lease: spyLease(), + store, + installedVersion: T, + evidence, + deliveryRoot: DELIVERY_ROOT, + }); + expect(result.published).toBe(true); + expect(publishCalls).toHaveLength(1); + expect(forbidden).toEqual([]); + }); + + test('absent-N publishes the facts once, with no receipt and no activation-state touch', () => { + const { store, publishCalls, forbidden } = spyStore(); + const evidence = testEvidence(); + const result = publishCodexDelivery({ + lease: spyLease(), + store, + installedVersion: null, + evidence, + deliveryRoot: DELIVERY_ROOT, + }); + expect(result.published).toBe(true); + expect(result.wroteDowngradeReceipt).toBe(false); + expect(publishCalls).toHaveLength(1); + expect(publishCalls[0]).toEqual({ evidence, deliveryRoot: DELIVERY_ROOT }); + expect(forbidden).toEqual([]); + }); + + test('a store write failure is a typed incomplete publication outcome', () => { + const { store } = spyStore(); + const evidence = testEvidence(); + store.publishDelivery = () => { + throw new Error('delivery store is unwritable'); + }; + const result = publishCodexDelivery({ + lease: spyLease(), + store, + installedVersion: N, + evidence, + deliveryRoot: DELIVERY_ROOT, + }); + expect(result).toMatchObject({ + outcome: 'failed', + published: false, + detail: 'delivery store is unwritable', + }); }); }); diff --git a/src/genie-commands/codex-delivery.ts b/src/genie-commands/codex-delivery.ts index a938f1416..72800b276 100644 --- a/src/genie-commands/codex-delivery.ts +++ b/src/genie-commands/codex-delivery.ts @@ -31,6 +31,13 @@ import { compareReleaseVersions, parseReleaseVersion, } from '../lib/codex-activation.js'; +import { + type AuthenticatedDeliveryRecordFields, + type VerifiedDeliveryEvidence, + authenticatedDeliveryBindingFromRecord, + verifiedDeliveryEvidenceFacts, +} from '../lib/codex-delivery-evidence.js'; +import type { DeliveryRecordReadState } from '../lib/codex-host-observation.js'; import type { HeldLifecycleLease } from '../lib/codex-lifecycle-lease.js'; /** The exact operator recovery every delivered-but-action-required Codex path names. */ @@ -63,6 +70,15 @@ export const CODEX_LIFECYCLE_BUSY_TRAILER = serializeActivationResultTrailer({ nextAction: 'another Genie lifecycle command is active; retry once it completes', }); +/** A verified binary/payload exists, but no authenticated delivery record was committed. */ +export const CODEX_DELIVERY_INCOMPLETE_TRAILER = serializeActivationResultTrailer({ + schemaVersion: 1, + code: 'delivery-incomplete', + deliveryComplete: false, + retry: true, + nextAction: 'retry genie update (or genie install) to publish the authenticated delivery record', +}); + /** * Thrown when the Codex lifecycle lease is held by another command. The caller * maps it to exit 2 with the `codex-lifecycle-busy` trailer and zero mutation — @@ -112,52 +128,98 @@ export function classifyCodexDelivery(installedVersion: string | null, targetVer export interface CodexDeliveryFacts { /** The installed generation N from a live `codex plugin list` (null = absent). */ installedVersion: string | null; - /** The delivered target T, read from the physically scanned delivered tree (never the raw manifest). */ - targetVersion: string; - /** SHA-256 of the delivered `plugins/genie` tree (scanPhysicalTree of the promoted root). */ - canonicalPayloadSha256: string; - /** Delivery channel recorded in the published facts. */ - channel: string; + /** Opaque proof minted only after the signed descriptor matches the downloaded candidate. */ + evidence: VerifiedDeliveryEvidence; + /** Physical canonical root revalidated by the deep store immediately before publication. */ + deliveryRoot: string; + /** + * The on-disk record read-state (Group E). Lets a verified delivery whose + * plugin generation is already `current` (e.g. install converged the plugin + * itself) republish a STALE record: without it, a prior-generation record + * survives the delivery and setup's Decision-9 gate refuses with `mismatch` + * while its recovery points back at the very command that skipped publishing + * (2026-07-23 live-QA finding). A matching record is never republished. + */ + existingRecord?: DeliveryRecordReadState; } /** - * Build the `publishDelivery` input for a pending delivery, or null when there - * is nothing to publish (absent/current/indeterminate). A downgrade binds + * Build the `publishDelivery` input for a pending OR absent-N delivery, or null + * when there is nothing to publish (current/indeterminate). A downgrade binds * `downgradeFrom = N` so A writes the one-time downgrade receipt. + * + * Group E: `absent` (no installed plugin generation) publishes too. The record + * binds the DELIVERY facts, not an activation — and setup's Decision-9 gate + * refuses to activate a fresh host without a matching record, so a delivery + * that skipped publication forced a second `genie update` (already-current + * repair) before the first `genie setup --codex` could ever succeed + * (2026-07-23 live-QA finding). Publication remains a pure fact write: no + * journal, activation, or cache mutation. */ export function buildDeliveryPublication(facts: CodexDeliveryFacts): PublishDeliveryInput | null { - const state = classifyCodexDelivery(facts.installedVersion, facts.targetVersion); - if (state.kind !== 'pending') return null; + const evidence = verifiedDeliveryEvidenceFacts(facts.evidence); + const state = classifyCodexDelivery(facts.installedVersion, evidence.descriptor.version); + if (state.kind === 'indeterminate') return null; + if (existingDeliveryMatches(facts)) return null; const input: PublishDeliveryInput = { - targetVersion: facts.targetVersion, - canonicalPayloadSha256: facts.canonicalPayloadSha256, - channel: facts.channel, + evidence: facts.evidence, + deliveryRoot: facts.deliveryRoot, }; - if (state.direction === 'downgrade' && facts.installedVersion !== null) { + if (state.kind === 'pending' && state.direction === 'downgrade' && facts.installedVersion !== null) { input.downgradeFrom = facts.installedVersion; } return input; } +/** + * A `current` generation republishes ONLY when the caller supplied the on-disk + * record state and it fails the core binding (absent/invalid/mismatched). No + * record state supplied → conservative no-publish (the pre-Group-E contract). + */ +function existingDeliveryMatches(facts: CodexDeliveryFacts): boolean { + if (facts.existingRecord?.status !== 'present') return false; + const evidence = verifiedDeliveryEvidenceFacts(facts.evidence); + return authenticatedDeliveryBindingFromRecord(facts.existingRecord.record, evidence, facts.deliveryRoot) !== null; +} + export interface PublishCodexDeliveryInput extends CodexDeliveryFacts { /** The caller-held `update-delivery` / `install-converge` lease (parent only). */ lease: HeldLifecycleLease; /** A's deep store, opened by the caller (`openCodexActivationStore`). */ store: CodexActivationStore; - /** The 128-bit delivery transaction id; A mints one when omitted. */ - deliveryId?: string; - now?: () => Date; } -export interface PublishedCodexDelivery { - state: CodexDeliveryState; - /** True when this call wrote a delivery record (pending only). */ - published: boolean; - /** True when this call wrote a downgrade receipt (explicit-channel downgrade). */ - wroteDowngradeReceipt: boolean; - /** The published record (null when nothing was published). */ - record: DeliveryRecord | null; -} +export type PublishedCodexDelivery = + | { + outcome: 'matching'; + state: CodexDeliveryState; + published: false; + wroteDowngradeReceipt: false; + record: AuthenticatedDeliveryRecordFields; + } + | { + outcome: 'published'; + state: CodexDeliveryState; + published: true; + wroteDowngradeReceipt: boolean; + record: DeliveryRecord; + } + | { + outcome: 'skipped'; + state: CodexDeliveryState; + published: false; + wroteDowngradeReceipt: false; + record: null; + detail: string; + } + | { + outcome: 'failed'; + state: CodexDeliveryState; + published: false; + wroteDowngradeReceipt: false; + record: null; + detail: string; + }; /** * Parent-side: publish attested delivery facts through A when the delivery is @@ -167,20 +229,48 @@ export interface PublishedCodexDelivery { * at permit-gated setup activation. */ export function publishCodexDelivery(input: PublishCodexDeliveryInput): PublishedCodexDelivery { - const state = classifyCodexDelivery(input.installedVersion, input.targetVersion); + const descriptor = verifiedDeliveryEvidenceFacts(input.evidence).descriptor; + const state = classifyCodexDelivery(input.installedVersion, descriptor.version); + if (existingDeliveryMatches(input) && input.existingRecord?.status === 'present') { + return { + outcome: 'matching', + state, + published: false, + wroteDowngradeReceipt: false, + record: input.existingRecord.record, + }; + } const publication = buildDeliveryPublication(input); if (publication === null) { - return { state, published: false, wroteDowngradeReceipt: false, record: null }; + return { + outcome: 'skipped', + state, + published: false, + wroteDowngradeReceipt: false, + record: null, + detail: + state.kind === 'indeterminate' + ? state.detail + : 'delivery publication lacked a determinable non-matching record state', + }; + } + try { + const record = input.store.publishDelivery(input.lease, publication); + return { + outcome: 'published', + state, + published: true, + wroteDowngradeReceipt: publication.downgradeFrom !== undefined, + record, + }; + } catch (cause) { + return { + outcome: 'failed', + state, + published: false, + wroteDowngradeReceipt: false, + record: null, + detail: cause instanceof Error ? cause.message : String(cause), + }; } - const record = input.store.publishDelivery(input.lease, { - ...publication, - deliveryId: input.deliveryId, - now: input.now, - }); - return { - state, - published: true, - wroteDowngradeReceipt: publication.downgradeFrom !== undefined, - record, - }; } diff --git a/src/genie-commands/doctor.test.ts b/src/genie-commands/doctor.test.ts index 8b2176b9b..491a06ba3 100644 --- a/src/genie-commands/doctor.test.ts +++ b/src/genie-commands/doctor.test.ts @@ -20,6 +20,7 @@ import { dirname, join } from 'node:path'; import { computeDirDigest, computeFileDigest } from '../lib/agent-sync.js'; import { reconcileCodexProjectMcp, resolveGitProjectRoots } from '../lib/codex-project-mcp.js'; import { CANONICAL_GENIE_SKILL_NAMES } from '../lib/runtime-integrations.js'; +import { VERSION } from '../lib/version.js'; import { MINIMUM_BUN_VERSION, checkAgentSync, @@ -164,6 +165,17 @@ describe('doctorCommand', () => { }); }); +/** + * Group E: mark a project trusted in the isolated CODEX_HOME global config so + * the route-layer classifier can claim route health for it. + */ +function trustProjectInCodexConfig(root: string): void { + const configPath = join(process.env.CODEX_HOME as string, 'config.toml'); + mkdirSync(dirname(configPath), { recursive: true }); + const existing = existsSync(configPath) ? readFileSync(configPath, 'utf8') : ''; + writeFileSync(configPath, `${existing}[projects."${root}"]\ntrust_level = "trusted"\n`); +} + describe('Codex doctor lifecycle results', () => { test('a timed-out plugin query is a structured hard failure, not a false pass', async () => { const checks = await checkCodexIntegration(process.cwd(), { @@ -182,6 +194,7 @@ describe('Codex doctor lifecycle results', () => { test('missing configured Node is actionable and never displaces the fallback', async () => { const root = mkdtempSync(join(tmpdir(), 'doctor-node-availability-')); try { + trustProjectInCodexConfig(root); reconcileCodexProjectMcp( root, { cliAvailable: true, status: 'ok', installed: true, enabled: false, usable: false, detail: 'disabled' }, @@ -207,11 +220,59 @@ describe('Codex doctor lifecycle results', () => { } }); + test('post-A manifest truth: declaring NO mcpServers is the healthy shape; declaring one warns (live-QA regression)', async () => { + const root = mkdtempSync(join(tmpdir(), 'doctor-manifest-truth-')); + const priorCodexHome = process.env.CODEX_HOME; + process.env.CODEX_HOME = join(root, 'codex-home'); + try { + trustProjectInCodexConfig(root); + const activePluginRoot = join(root, 'active-plugin'); + mkdirSync(join(activePluginRoot, '.codex-plugin'), { recursive: true }); + const manifestPath = join(activePluginRoot, '.codex-plugin', 'plugin.json'); + const probe = { + cliAvailable: true, + status: 'ok' as const, + installed: true, + enabled: true, + // The probe still reports the stale pre-A manifest expectation; doctor + // must not surface it as a defect on a post-A generation. + usable: false, + usabilityDetail: 'plugin manifest does not point mcpServers to ./.mcp.json', + version: VERSION, + activePluginRoot, + detail: 'installed', + }; + + // Post-A shape: no mcpServers key → capability pass, plugin check pass. + writeFileSync(manifestPath, JSON.stringify({ name: 'genie', version: VERSION, skills: './skills/' })); + const healthy = await checkCodexIntegration(root, probe); + const capability = healthy.find((check) => check.name === 'Codex Genie MCP capability'); + const plugin = healthy.find((check) => check.name === 'Codex Genie plugin'); + expect(capability).toMatchObject({ status: 'pass' }); + expect(capability?.detail).toContain('declares no MCP route'); + expect(plugin).toMatchObject({ status: 'pass' }); + expect(plugin?.detail).not.toContain('does not point mcpServers'); + expect(plugin?.detail).not.toContain('unusable'); + + // Regression shape: a declared route is the defect (second Genie route). + writeFileSync(manifestPath, JSON.stringify({ name: 'genie', version: VERSION, mcpServers: './.mcp.json' })); + const regressed = await checkCodexIntegration(root, probe); + const declared = regressed.find((check) => check.name === 'Codex Genie MCP capability'); + expect(declared).toMatchObject({ status: 'warn' }); + expect(declared?.detail).toContain('still declares mcpServers'); + } finally { + if (priorCodexHome === undefined) Reflect.deleteProperty(process.env, 'CODEX_HOME'); + else process.env.CODEX_HOME = priorCodexHome; + rmSync(root, { recursive: true, force: true }); + } + }); + test('healthy source bundle cannot make an unproven active plugin capability pass', async () => { const root = mkdtempSync(join(tmpdir(), 'doctor-active-plugin-')); const priorCodexHome = process.env.CODEX_HOME; process.env.CODEX_HOME = join(root, 'codex-home'); try { + trustProjectInCodexConfig(root); expect(existsSync(join(import.meta.dir, '..', '..', 'plugins', 'genie', '.codex-plugin', 'plugin.json'))).toBe( true, ); @@ -494,6 +555,44 @@ describe('doctorCommand — genie.db check branches', () => { expect(json.ok).toBe(false); expect(exitCode).toBe(1); }); + + for (const fixture of ['directory', 'malformed-file'] as const) { + test(`existing ${fixture} genie.db cannot produce a passing Codex project context`, async () => { + const dbPath = join(tmp, '.genie', 'genie.db'); + mkdirSync(join(tmp, '.genie'), { recursive: true }); + if (fixture === 'directory') mkdirSync(dbPath); + else writeFileSync(dbPath, 'not a sqlite database'); + const roots = resolveGitProjectRoots(tmp); + if (roots === null) throw new Error('expected fixture roots'); + + const { output, exitCode } = await captureDoctor(() => + doctorCommand( + { json: true }, + { + ...isolatedDoctorDeps(tmp), + projectContext: { + kind: 'ok', + effectiveLaunchCwd: tmp, + worktreeConfigRoot: roots.worktreeRoot, + gitCommonDir: join(roots.commonRoot, '.git'), + genieStorageRoot: roots.commonRoot, + dbPath, + }, + }, + ), + ); + const json = JSON.parse(output) as { + ok: boolean; + checks: Array<{ name: string; status: string; detail: string }>; + }; + const context = json.checks.find((check) => check.name === 'Codex project context'); + expect(context?.status).toBe('fail'); + expect(context?.detail).toContain('project-database-unavailable'); + expect(context?.detail).toContain(dbPath); + expect(json.ok).toBe(false); + expect(exitCode).toBe(1); + }); + } }); // ============================================================================ @@ -908,14 +1007,16 @@ describe('checkAgentSync', () => { expect(codex?.suggestion).toBeUndefined(); }); - test('codex clean managed fallback → warn repairable duplicate (run genie update)', () => { + test('codex clean managed fallback → warn repairable duplicate (run authenticated setup)', () => { mkdirSync(codexDir, { recursive: true }); seedManaged(join(pluginRoot, 'skills', 'wish'), join(agentsSkillsDir, 'wish')); const codex = find(checkAgentSync(paths()), 'agent sync: codex'); expect(codex?.status).toBe('warn'); expect(codex?.detail).toContain('repairable duplicate provider state'); expect(codex?.detail).toContain('wish'); - expect(codex?.suggestion).toContain('genie update'); + expect(codex?.suggestion).toContain('genie setup --codex'); + expect(codex?.suggestion).toContain('matching authenticated delivery'); + expect(codex?.suggestion).toContain('plugin health'); }); test('codex preserved personal collision → DISTINCT manual remediation', () => { @@ -931,7 +1032,7 @@ describe('checkAgentSync', () => { expect(collision?.detail).toContain('collide with plugin names'); expect(collision?.suggestion).toContain('manually'); // DISTINCT from the repairable-duplicate remediation: personal collisions are - // never retired by `genie update`, only reviewed/removed by the user. + // never retired by setup, only reviewed/removed by the user. expect(collision?.suggestion).not.toContain('retire'); }); @@ -955,12 +1056,12 @@ describe('checkAgentSync', () => { expect(evidence?.suggestion).toContain('reconcile it manually'); }); - test('codex well-formed but unrecognized fallback → distinct "review manually" warn, main line stays plugin-only, never advises `genie update`', () => { + test('codex well-formed but unrecognized fallback → distinct manual warn, main line stays plugin-only, never advises setup will retire it', () => { // identityVersion:2, digest self-consistent with its own manifest — but the // content is NOT the installed plugin's payload and not in the frozen // historical allowlist, so `planCodexFallbackRetirement` would refuse it // ('ambiguous-ownership'). This is the PR #2575 no-op-loop bug: doctor must - // never call this "clean" or tell the user `genie update` fixes it. + // never call this "clean" or tell the user authenticated setup fixes it. mkdirSync(codexDir, { recursive: true }); const dir = join(agentsSkillsDir, 'orphaned-content'); mkdirSync(dir, { recursive: true }); @@ -987,8 +1088,8 @@ describe('checkAgentSync', () => { expect(unrecognized?.detail).toContain('review manually'); expect(unrecognized?.detail).toContain('orphaned-content'); // The whole point: this warning must never send the user down the same - // no-op loop `genie update` already refuses to close. - expect(unrecognized?.suggestion).not.toContain('Run `genie update` to retire'); + // no-op loop authenticated setup already refuses to close. + expect(unrecognized?.suggestion).toContain('`genie setup --codex` will NOT retire'); }); test('codex ~/.agents/skills exists but is unreadable → warn, never a false-healthy plugin-only pass', () => { @@ -1754,10 +1855,22 @@ import { type QueryFact, parseReleaseVersion, } from '../lib/codex-activation.js'; +import { + DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + DELIVERY_EVIDENCE_REPOSITORY, + type VerifiedDeliveryEvidenceFacts, + deriveDeliveryId, +} from '../lib/codex-delivery-evidence.js'; const DOC_T = '5.260712.1'; const DOC_OLD = '5.260711.9'; const DOC_DIGEST = 'a'.repeat(64); +const DOC_BINARY_DIGEST = 'b'.repeat(64); +const DOC_MANIFEST_DIGEST = 'c'.repeat(64); +const DOC_ARTIFACT_DIGEST = 'd'.repeat(64); +const DOC_PLATFORM = 'darwin-arm64'; +const DOC_DELIVERY_ROOT = '/fixture/genie/deliveries/current'; +const DOC_EVIDENCE_DIGEST = 'e'.repeat(64); function docVer(s: string) { const parsed = parseReleaseVersion(s); @@ -1768,7 +1881,15 @@ function docFamily(): FamilyWitness { return { status: 'present', digest: 'f'.repeat(64), identity: '10:300' }; } function docOkCanonical(): CanonicalFact { - return { status: 'ok', version: docVer(DOC_T), digest: DOC_DIGEST, identity: '10:100' }; + return { + status: 'ok', + version: docVer(DOC_T), + digest: DOC_DIGEST, + identity: '10:100', + platformTriple: DOC_PLATFORM, + installedBinarySha256: DOC_BINARY_DIGEST, + deliveryRoot: DOC_DELIVERY_ROOT, + }; } function docRegPresent(version = DOC_T): QueryFact { return { status: 'ok', registration: { present: true, enabled: true, version: docVer(version) } }; @@ -1776,13 +1897,63 @@ function docRegPresent(version = DOC_T): QueryFact { function docCachePresent(digest = DOC_DIGEST): PhysicalCacheFact { return { kind: 'present', digest, identity: '10:200' }; } +/** A matching authenticated delivery record binding the canonical target (Group E delivery gate). */ +function docDeliveryPresent(): CodexActivationSnapshot['delivery'] { + return { + status: 'present', + record: { + schemaVersion: 2, + deliveryId: deriveDeliveryId(DOC_EVIDENCE_DIGEST, DOC_DELIVERY_ROOT), + targetVersion: DOC_T, + canonicalPayloadSha256: DOC_DIGEST, + channel: 'stable', + deliveredAt: '2026-07-12T00:00:00.000Z', + evidenceDigest: DOC_EVIDENCE_DIGEST, + platformId: 'darwin-arm64', + platformTriple: DOC_PLATFORM, + releaseTag: `v${DOC_T}`, + releaseName: `genie-${DOC_T}-${DOC_PLATFORM}.tar.gz`, + releaseManifestSha256: DOC_MANIFEST_DIGEST, + artifactSha256: DOC_ARTIFACT_DIGEST, + installedBinarySha256: DOC_BINARY_DIGEST, + deliveryRoot: DOC_DELIVERY_ROOT, + }, + evidence: docDeliveryEvidenceFacts(), + }; +} + +function docDeliveryEvidenceFacts(): VerifiedDeliveryEvidenceFacts { + return { + evidenceDigest: DOC_EVIDENCE_DIGEST, + deliveredAt: '2026-07-12T00:00:00.000Z', + descriptor: { + schemaVersion: 1 as const, + repository: DELIVERY_EVIDENCE_REPOSITORY, + version: DOC_T, + channel: 'stable' as const, + platformId: 'darwin-arm64' as const, + platformTriple: DOC_PLATFORM, + releaseTag: `v${DOC_T}`, + releaseName: `genie-${DOC_T}-${DOC_PLATFORM}.tar.gz`, + releaseManifestSha256: DOC_MANIFEST_DIGEST, + artifactSha256: DOC_ARTIFACT_DIGEST, + installedBinarySha256: DOC_BINARY_DIGEST, + canonicalPayloadSha256: DOC_DIGEST, + sourceSha: '1'.repeat(40), + sourceBranch: 'main', + sourceCiRunId: '123', + controlSha: '2'.repeat(40), + digestAlgorithm: DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + }, + }; +} function docCurrentSnapshot(): CodexActivationSnapshot { return { canonical: docOkCanonical(), query: docRegPresent(), cache: docCachePresent(), receipt: { status: 'absent' }, - delivery: { status: 'absent' }, + delivery: docDeliveryPresent(), intent: { status: 'absent' }, receiptConsumed: false, observationWitness: { before: docFamily(), after: docFamily() }, @@ -1915,3 +2086,156 @@ describe('doctor --json integrationSummary (Group D)', () => { expect(source.includes('acquireLifecycleLease')).toBe(false); }); }); + +describe('Group E lifecycle truth (doctor)', () => { + test('current state with an ABSENT delivery record presents delivery-incomplete, exit 1, recovery command', async () => { + const missingRecord = { ...docCurrentSnapshot(), delivery: { status: 'absent' as const } }; + const { output, exitCode } = await captureDoctor(() => + doctorCommand({ json: true }, doctorDepsWith(missingRecord)), + ); + const json = JSON.parse(output) as DoctorJson; + expect(json.integrationSummary?.codexPlugin).toMatchObject({ + state: 'delivery-incomplete', + deliveryComplete: false, + actionRequired: true, + mutationAuthority: 'none', + }); + expect(json.integrationSummary?.codexPlugin.recovery).toContain('genie update'); + expect(exitCode).toBe(1); + }); + + test('an INVALID record and a MISMATCHED record present the same consistent delivery-incomplete state', async () => { + const invalid = { ...docCurrentSnapshot(), delivery: { status: 'invalid' as const, detail: 'corrupt' } }; + const mismatched = { + ...docCurrentSnapshot(), + delivery: { + status: 'present' as const, + record: { + schemaVersion: 2 as const, + deliveryId: deriveDeliveryId(DOC_EVIDENCE_DIGEST, DOC_DELIVERY_ROOT), + targetVersion: DOC_OLD, + canonicalPayloadSha256: DOC_DIGEST, + channel: 'stable', + deliveredAt: '2026-07-12T00:00:00.000Z', + evidenceDigest: DOC_EVIDENCE_DIGEST, + platformId: 'darwin-arm64', + platformTriple: DOC_PLATFORM, + releaseTag: `v${DOC_OLD}`, + releaseName: `genie-${DOC_OLD}-${DOC_PLATFORM}.tar.gz`, + releaseManifestSha256: DOC_MANIFEST_DIGEST, + artifactSha256: DOC_ARTIFACT_DIGEST, + installedBinarySha256: DOC_BINARY_DIGEST, + deliveryRoot: DOC_DELIVERY_ROOT, + }, + evidence: docDeliveryEvidenceFacts(), + }, + }; + for (const snapshot of [invalid, mismatched]) { + const { output, exitCode } = await captureDoctor(() => doctorCommand({ json: true }, doctorDepsWith(snapshot))); + const json = JSON.parse(output) as DoctorJson; + expect(json.integrationSummary?.codexPlugin.state).toBe('delivery-incomplete'); + expect(json.integrationSummary?.codexPlugin.deliveryComplete).toBe(false); + expect(exitCode).toBe(1); + } + }); + + test('a harder exit-1 state (query-failed) keeps its own presentation; deliveryComplete still false', async () => { + const queryFailedNoRecord = { ...docQueryFailedSnapshot(), delivery: { status: 'absent' as const } }; + const { output, exitCode } = await captureDoctor(() => + doctorCommand({ json: true }, doctorDepsWith(queryFailedNoRecord)), + ); + const json = JSON.parse(output) as DoctorJson; + expect(json.integrationSummary?.codexPlugin.state).toBe('query-failed'); + expect(json.integrationSummary?.codexPlugin.deliveryComplete).toBe(false); + expect(exitCode).toBe(1); + }); + + test('matching record keeps current green: exit 0, deliveryComplete true (regression anchor)', async () => { + const { output, exitCode } = await captureDoctor(() => + doctorCommand({ json: true }, doctorDepsWith(docCurrentSnapshot())), + ); + const json = JSON.parse(output) as DoctorJson; + expect(json.integrationSummary?.codexPlugin).toMatchObject({ state: 'current', deliveryComplete: true }); + expect(exitCode).toBe(0); + }); + + test('unified host path: one canned query feeds advisory rider AND a non-query-failed summary (Decision 11)', async () => { + const calls: string[][] = []; + const stdout = JSON.stringify({ + installed: [{ pluginId: 'genie@automagik', version: '5.260722.1', enabled: true }], + }); + const { output } = await captureDoctor(() => + doctorCommand( + { json: true }, + { + root: join(isolatedHome, 'repo'), + databaseRoot: join(isolatedHome, 'repo'), + bunVersion: '1.3.10', + bunPath: '/usr/bin/bun', + codexHost: { + which: () => process.execPath, + codexHome: join(isolatedHome, 'codex'), + runner: (command, args) => { + calls.push([command, ...args]); + return { + exitCode: 0, + stdout, + stderr: '\x1b[33mWARN: PATH advisory\x1b[0m', + }; + }, + }, + }, + ), + ); + // Exactly one spawn fed the probe AND (via replay) the activation snapshot. + expect(calls).toEqual([[process.execPath, 'plugin', 'list', '--json']]); + const json = JSON.parse(output) as DoctorJson; + const cli = json.checks.find((check) => check.name === 'Codex CLI') as + | { name: string; status: string; advisory?: string; detail?: string } + | undefined; + expect(cli?.advisory).toBe('WARN: PATH advisory'); + expect(cli?.detail).toContain('advisory: WARN: PATH advisory'); + // The advisory did NOT flip the fail-closed activation parser to query-failed. + expect(json.integrationSummary?.codexPlugin.state).not.toBe('query-failed'); + // One ANSI-free JSON document: the raw advisory's escape bytes never reach stdout. + expect(output).not.toContain('\x1b[33m'); + }); + + test('project context: ok / database-unavailable / unsupported-layout map to pass / warn / fail', async () => { + const openableDbPath = join(isolatedHome, 'project-context-openable.db'); + const openableDb = new Database(openableDbPath); + openableDb.close(); + const base = { + effectiveLaunchCwd: '/repo', + worktreeConfigRoot: '/repo', + gitCommonDir: '/repo/.git', + genieStorageRoot: '/repo', + dbPath: openableDbPath, + }; + const cases = [ + { context: { kind: 'ok' as const, ...base }, status: 'pass' }, + { + context: { kind: 'project-database-unavailable' as const, effectiveLaunchCwd: '/repo', detail: 'no db' }, + status: 'warn', + }, + { + context: { kind: 'unsupported-project-layout' as const, effectiveLaunchCwd: '/repo', detail: 'bare repo' }, + status: 'fail', + }, + ]; + for (const { context, status } of cases) { + const { output } = await captureDoctor(() => + doctorCommand({ json: true }, { ...doctorDepsWith(null), projectContext: context }), + ); + const json = JSON.parse(output) as DoctorJson; + const check = json.checks.find((c) => c.name === 'Codex project context'); + expect(check?.status).toBe(status); + } + }); + + test('injected root without injected context skips the context check (unit-test seam, not a repo)', async () => { + const { output } = await captureDoctor(() => doctorCommand({ json: true }, doctorDepsWith(null))); + const json = JSON.parse(output) as DoctorJson; + expect(json.checks.find((c) => c.name === 'Codex project context')).toBeUndefined(); + }); +}); diff --git a/src/genie-commands/doctor.ts b/src/genie-commands/doctor.ts index c9309bd56..78c3be2c9 100644 --- a/src/genie-commands/doctor.ts +++ b/src/genie-commands/doctor.ts @@ -54,6 +54,11 @@ import { } from '../lib/codex-activation-executor.js'; import type { CodexActivationSnapshot, IntegrationSummaryEnvelope } from '../lib/codex-activation.js'; import { DEAD_GENIE_OTEL_EXPORTER, getCodexConfigPath } from '../lib/codex-config.js'; +import type { DeliveryEvidenceVerificationDependencies } from '../lib/codex-delivery-evidence.js'; +// Group E: ONE bounded host observation feeds probe, summary, advisory, and +// replay runner; the lifecycle-truth seams keep delivery/route claims aligned. +import { type DoctorCodexObservation, observeDoctorCodexHost } from '../lib/codex-doctor-observation.js'; +import { type RouteLayerFinding, assessSnapshotDelivery, classifyRouteLayers } from '../lib/codex-lifecycle-truth.js'; import { type CodexPluginProbe, inspectCodexProjectMcp, @@ -72,10 +77,17 @@ import { hasDuplicateSkillsExternalDirsKeys, resolveProductSkillsRoot } from '.. import { resolveOmniRuntimeConfig } from '../lib/omni-config.js'; import { CANONICAL_GENIE_SKILL_NAMES, + type CodexAgentDisplayState, inspectCodexAgentOwnership, inspectCodexFallbackTier, } from '../lib/runtime-integrations.js'; -import { CURRENT_SCHEMA_VERSION, GenieDbError, openDb } from '../lib/v5/genie-db.js'; +import { + CURRENT_SCHEMA_VERSION, + GenieDbError, + type ProjectContext, + openDb, + resolveProjectContext, +} from '../lib/v5/genie-db.js'; import { VERSION } from '../lib/version.js'; import { cleanupV4, @@ -140,6 +152,20 @@ interface CheckResult { * for the stable per-entry state contract. */ indexLane?: { entries: IndexLaneEntry[] }; + /** + * Machine-readable payload rider (survives `--json` as `checks[].routeLayers`). + * Only the `Codex Genie MCP registration` check sets it: the typed config-layer + * findings (collision, shadowing, global same-key, trust states) from the + * Group E route-layer classifier. + */ + routeLayers?: RouteLayerFinding[]; + /** + * Machine-readable payload rider (survives `--json` as `checks[].advisory`). + * Only the `Codex CLI` check sets it: the sanitized bounded advisory stderr + * (e.g. the real sandbox PATH advisory) from the single host observation. + * Diagnostic metadata only — never a policy decision (Decision 11). + */ + advisory?: string; } // ============================================================================ @@ -387,37 +413,62 @@ function codexPluginCheck(state: CodexPluginProbe): CheckResult { }; } const current = state.version === VERSION; - const healthy = current && state.enabled === true && state.usable === true; + // Post-Group-A the plugin ships NO MCP declaration — the marker-owned project + // route is the only Codex route — so plugin health is installed+enabled+current. + // The probe still reports the OLD manifest expectation ("does not point + // mcpServers to ./.mcp.json"): on a post-A generation that IS the healthy + // shape, so it is filtered from diagnostics; every other usability detail + // (missing node/launcher/binary) stays visible as diagnostics. + const healthy = current && state.enabled === true; + const staleManifestComplaint = state.usabilityDetail?.includes('does not point mcpServers') === true; + const diagnostics = + state.usable === true || staleManifestComplaint ? '' : `; diagnostics: ${state.usabilityDetail ?? 'unknown'}`; return { name: 'Codex Genie plugin', status: healthy ? 'pass' : 'warn', - detail: `v${state.version ?? 'unknown'}; ${state.enabled === true ? 'enabled' : 'disabled or unknown'}; ${state.usable === true ? 'MCP launcher usable' : `MCP launcher unusable (${state.usabilityDetail ?? 'unknown reason'})`} (CLI v${VERSION})`, - suggestion: healthy - ? undefined - : 'Run `genie setup --codex` to refresh it; keep the absolute project fallback until launcher health passes.', + detail: `v${state.version ?? 'unknown'}; ${state.enabled === true ? 'enabled' : 'disabled or unknown'}; MCP route: marker-owned project route (plugin declares none)${diagnostics} (CLI v${VERSION})`, + suggestion: healthy ? undefined : 'Run `genie setup --codex` to activate the delivered generation.', }; } function codexAgentCheck(): CheckResult { const report = inspectCodexAgentOwnership(resolveCodexDir()); + const total = report.expectedDeliveredTotal; + const count = (state: CodexAgentDisplayState): number => + report.entries.filter((entry) => entry.state === state).length; const counts = { - clean: report.entries.filter((entry) => entry.ownership === 'managed-clean').length, - modified: report.entries.filter((entry) => entry.ownership === 'managed-modified').length, - user: report.entries.filter((entry) => entry.ownership === 'user-owned').length, - absent: report.entries.filter((entry) => entry.ownership === 'absent').length, + managed: count('managed'), + stale: count('stale'), + adoptable: count('adoptable-historical'), + collision: count('collision'), + personal: count('personal'), + absent: count('absent'), }; - const healthy = report.status === 'valid' && counts.clean === 7 && counts.modified === 0 && counts.absent === 0; + // Personal files are the operator's own unrelated agents — present is healthy. + // Every other non-managed state (stale/adoptable/collision/absent) is actionable. + const healthy = + report.status === 'valid' && + counts.managed === total && + counts.stale === 0 && + counts.adoptable === 0 && + counts.collision === 0 && + counts.absent === 0; + const suggestion = + counts.adoptable > 0 || counts.stale > 0 || counts.managed < total + ? 'Run `genie setup --codex` to adopt and refresh delivered role agents; personal and collision files are never overwritten.' + : 'Review collision role agents (modified/symlinked/lookalike); Genie will not overwrite them, then run `genie setup --codex`.'; return { name: 'Codex Genie role agents', status: healthy ? 'pass' : 'warn', - detail: `inventory ${report.status}; clean=${counts.clean}/7, modified=${counts.modified}, user-owned=${counts.user}, absent=${counts.absent}${report.error ? ` (${report.error})` : ''}`, - suggestion: healthy - ? undefined - : 'Review modified/user-owned collisions, then run `genie setup --codex`; Genie will not overwrite them.', + detail: + `inventory ${report.status}; managed=${counts.managed}/${total}, stale=${counts.stale}, ` + + `adoptable=${counts.adoptable}, collision=${counts.collision}, personal=${counts.personal}, absent=${counts.absent}; ` + + `reviewer digest ${report.reviewerDigest.slice(0, 12)}${report.error ? ` (${report.error})` : ''}`, + suggestion: healthy ? undefined : suggestion, }; } -function codexProjectRouteCheck(root: string | null, probe: CodexPluginProbe): CheckResult { +function codexProjectRouteCheck(root: string | null, probe: CodexPluginProbe, cwd = process.cwd()): CheckResult { if (root === null) { return { name: 'Codex Genie MCP registration', @@ -428,11 +479,34 @@ function codexProjectRouteCheck(root: string | null, probe: CodexPluginProbe): C } try { const route = inspectCodexProjectMcp(root, probe); + // Group E: distinct typed config-layer findings. Collisions, shadowing, and + // a global same-key route are hard route defects (preserved, never edited); + // the trust states block a health CLAIM without failing intact route bytes. + const findings = classifyRouteLayers({ worktreeRoot: root, cwd, route, globalConfigPath: getCodexConfigPath() }); + const hard = findings.filter( + (finding) => + finding.kind === 'route-collision' || + finding.kind === 'route-shadowed' || + finding.kind === 'global-route-same-key', + ); + const trust = findings.filter( + (finding) => finding.kind === 'untrusted-config' || finding.kind === 'project-trust-required', + ); + const status: CheckStatus = !route.ok || hard.length > 0 ? 'fail' : trust.length > 0 ? 'warn' : 'pass'; + const findingText = findings.map((finding) => `${finding.kind}: ${finding.detail}`).join('; '); return { name: 'Codex Genie MCP registration', - status: route.ok ? 'pass' : 'fail', - detail: `${route.route}: ${route.detail ?? 'no detail'}`, - suggestion: route.ok ? undefined : 'Run `genie init` in this worktree to reconcile the project fallback.', + status, + detail: `${route.route}: ${route.detail ?? 'no detail'}${findingText.length > 0 ? `; ${findingText}` : ''}`, + suggestion: + status === 'pass' + ? undefined + : hard.length > 0 + ? 'Resolve the reported same-key/shadowing layer yourself (Genie never edits user-owned config), then run `genie init`.' + : trust.length > 0 && route.ok + ? 'Trust this project in Codex, then start a new Codex task.' + : 'Run `genie init` in this worktree to reconcile the project fallback.', + ...(findings.length > 0 ? { routeLayers: findings } : {}), }; } catch (error) { return { @@ -444,6 +518,65 @@ function codexProjectRouteCheck(root: string | null, probe: CodexPluginProbe): C } } +/** + * Group E: report what a Codex MCP child launched in this repository would + * resolve — the SAME `resolveProjectContext` the read-only MCP server uses, so + * doctor and the server can never disagree about project context. An absent + * database mirrors the `genie.db` check's "created on first use" stance as a + * warn (the MCP returns a typed error, never a healthy empty board); a + * bare/submodule/external layout or an unresolvable context is a hard fail. + */ +function checkCodexProjectContext(root: string | null, injected?: ProjectContext | null): CheckResult[] { + if (root === null || injected === null) return []; + const context = injected ?? resolveProjectContext(root); + if (context.kind === 'ok') { + let db: Database | null = null; + try { + db = new Database(context.dbPath, { readonly: true }); + db.query('PRAGMA user_version').get(); + } catch { + return [ + { + name: 'Codex project context', + status: 'fail', + detail: `Codex MCP returns typed 'project-database-unavailable': unable to open Genie database at ${context.dbPath}`, + suggestion: 'Repair or replace the repository .genie/genie.db, then rerun `genie doctor`.', + }, + ]; + } finally { + db?.close(); + } + return [ + { + name: 'Codex project context', + status: 'pass', + detail: `storage root ${context.genieStorageRoot}; db ${context.dbPath}`, + }, + ]; + } + if (context.kind === 'project-database-unavailable') { + return [ + { + name: 'Codex project context', + status: 'warn', + detail: `Codex MCP returns typed '${context.kind}' (never a healthy empty board): ${context.detail}`, + suggestion: 'Run `genie init` (or create the first task) to initialize .genie/genie.db.', + }, + ]; + } + return [ + { + name: 'Codex project context', + status: 'fail', + detail: `Codex MCP returns typed '${context.kind}': ${context.detail}`, + suggestion: + context.kind === 'unsupported-project-layout' + ? 'Run Codex tasks from an ordinary or linked non-bare worktree; bare/submodule/external-git-dir layouts are a hard boundary.' + : 'Verify this is an initialized Git worktree, then run `genie init`.', + }, + ]; +} + /** * Payload completeness (R5): how many of the exact canonical Genie skills the * active plugin root physically ships. Pure read of `/skills` @@ -472,17 +605,22 @@ function codexPluginPayloadCheck(probe: CodexPluginProbe): CheckResult | null { function codexPluginSurfaceChecks(probe: CodexPluginProbe): CheckResult[] { if (!probe.installed) return []; const manifest = probe.activePluginRoot ? join(probe.activePluginRoot, '.codex-plugin', 'plugin.json') : null; - let declared = false; - if (manifest !== null && existsSync(manifest)) { - try { - const parsed = JSON.parse(readFileSync(manifest, 'utf8')) as unknown; - declared = - typeof parsed === 'object' && - parsed !== null && - !Array.isArray(parsed) && - (parsed as Record).mcpServers === './.mcp.json'; - } catch { - declared = false; + // Post-Group-A contract (Decisions 1/7): the codex plugin manifest MUST NOT + // declare mcpServers — a declaration would re-create the second (cache-root) + // Genie route the wish removed. Absence is the healthy shape. + let manifestState: 'unproven' | 'unreadable' | 'declares-none' | 'declares-route' = 'unproven'; + if (manifest !== null) { + manifestState = 'unreadable'; + if (existsSync(manifest)) { + try { + const parsed = JSON.parse(readFileSync(manifest, 'utf8')) as unknown; + if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) { + manifestState = + (parsed as Record).mcpServers === undefined ? 'declares-none' : 'declares-route'; + } + } catch { + manifestState = 'unreadable'; + } } } const payload = codexPluginPayloadCheck(probe); @@ -490,13 +628,21 @@ function codexPluginSurfaceChecks(probe: CodexPluginProbe): CheckResult[] { ...(payload ? [payload] : []), { name: 'Codex Genie MCP capability', - status: declared ? 'pass' : 'warn', - detail: declared - ? `stdio server declared by active plugin at ${probe.activePluginRoot}` - : manifest === null - ? 'active installed plugin root is unproven; source-bundle declarations do not establish runtime health' - : `active plugin manifest is missing, corrupt, or does not declare ./.mcp.json: ${manifest}`, - suggestion: declared ? undefined : 'Run `genie setup --codex` to refresh the active plugin cache.', + status: manifestState === 'declares-none' ? 'pass' : 'warn', + detail: + manifestState === 'declares-none' + ? `plugin declares no MCP route (marker-owned project route is authoritative) at ${probe.activePluginRoot}` + : manifestState === 'declares-route' + ? `active plugin manifest still declares mcpServers — a second Genie route risks cache-root routing: ${manifest}` + : manifestState === 'unreadable' + ? `active plugin manifest is missing or corrupt: ${manifest}` + : 'active installed plugin root is unproven; source-bundle declarations do not establish runtime health', + suggestion: + manifestState === 'declares-none' + ? undefined + : manifestState === 'declares-route' + ? 'Run `genie update` then `genie setup --codex` to activate a generation without a plugin MCP route.' + : 'Run `genie setup --codex` to refresh the active plugin cache.', }, { name: 'Codex hook review', @@ -510,11 +656,19 @@ function codexPluginSurfaceChecks(probe: CodexPluginProbe): CheckResult[] { export async function checkCodexIntegration( root: string | null, probe: CodexPluginProbe = probeCodexGeniePlugin(), + advisory: string | null = null, ): Promise { if (!probe.cliAvailable) return [{ name: 'Codex CLI', status: 'warn', detail: 'not installed (Claude-only mode available)' }]; const codex = whichBinary('codex'); - const results: CheckResult[] = [{ name: 'Codex CLI', status: 'pass', detail: codex ?? 'detected by bounded probe' }]; + const results: CheckResult[] = [ + { + name: 'Codex CLI', + status: 'pass', + detail: `${codex ?? 'detected by bounded probe'}${advisory !== null ? `; advisory: ${advisory}` : ''}`, + ...(advisory !== null ? { advisory } : {}), + }, + ]; results.push(codexPluginCheck(probe), codexAgentCheck()); const configPath = getCodexConfigPath(); const obsolete = existsSync(configPath) && readFileSync(configPath, 'utf8').includes(DEAD_GENIE_OTEL_EXPORTER); @@ -735,7 +889,7 @@ async function checkOmniHookTimeout(): Promise { const SYNC_MANIFEST_NAME = MANIFEST_NAME; const SYNC_MANAGED_BY = MANAGED_BY; const COUNCIL_WORKFLOW_FILE = TARGET_NAME; -const SYNC_SUGGESTION = 'Run `genie update` to converge all detected coding agents.'; +const SYNC_SUGGESTION = 'Run `genie update` to converge detected Claude and Hermes integrations.'; const HERMES_INLINE_SUGGESTION = 'Rewrite the inline top-level key as a block mapping so genie can merge without deleting your entries, then run `genie update`.'; @@ -1111,7 +1265,8 @@ function checkCodexSync(codexDir: string, agentsSkillsDir: string, pluginRoot: s name: 'agent sync: codex', status: 'warn', detail: `${tier.cleanFallbacks.length} clean managed fallback(s) in ~/.agents/skills — repairable duplicate provider state (${tier.cleanFallbacks.join(', ')})${quarantineNote}`, - suggestion: 'Run `genie update` to retire these clean fallbacks; the installed plugin already provides them.', + suggestion: + 'Run `genie setup --codex` from an interactive terminal; setup requires the matching authenticated delivery and retires these clean fallbacks only after plugin health passes.', }); } if (tier.unrecognizedFallbacks.length > 0) { @@ -1120,14 +1275,14 @@ function checkCodexSync(codexDir: string, agentsSkillsDir: string, pluginRoot: s // allowlist and no matching live-plugin payload). Era-A fallbacks (v1 // legacy digest, no identityVersion) do NOT land here — their marker fails // the v2 self-consistency check, so they surface as preservedCollisions. - // Never user-edited, so NOT a personal collision — but `genie update` + // Never user-edited, so NOT a personal collision — but authenticated setup // refuses to retire it, so recommending that here would be a no-op loop. results.push({ name: 'agent sync: codex unrecognized', status: 'warn', detail: `${tier.unrecognizedFallbacks.length} unrecognized managed fallback(s) in ~/.agents/skills (review manually): ${tier.unrecognizedFallbacks.join(', ')}`, suggestion: - '`genie update` will NOT retire these — the content is well-formed genie provenance but not in the frozen allowlist and does not match the installed plugin payload. Review each manually; do not expect `genie update` to clear this warning.', + '`genie setup --codex` will NOT retire these — the content is well-formed genie provenance but not in the frozen allowlist and does not match the installed plugin payload. Review each manually; do not expect setup to clear this warning.', }); } if (tier.preservedCollisions.length > 0) { @@ -1669,6 +1824,16 @@ export interface DoctorDeps { bunVersion?: string | null; /** PATH seam paired with bunVersion. */ bunPath?: string | null; + /** + * Group E seams for the single host observation. `codexHost` feeds + * `observeDoctorCodexHost` (inject a canned runner to drive the WHOLE unified + * derivation without a live Codex home); `projectContext` injects the typed + * project-context fact (explicit `null` = skip the check). + */ + codexHost?: Parameters[0]; + projectContext?: ProjectContext | null; + /** TEST-ONLY cryptographic seam for persisted delivery-evidence fixtures; no CLI/env path exposes it. */ + deliveryEvidenceVerification?: DeliveryEvidenceVerificationDependencies; } // ============================================================================ @@ -1686,10 +1851,23 @@ export interface DoctorDeps { function resolveCodexActivationSnapshot( deps: DoctorDeps, pluginProbe: CodexPluginProbe, + hostObservation: DoctorCodexObservation | null, ): CodexActivationSnapshot | null { if (deps.codexActivation !== undefined) return deps.codexActivation; if (!pluginProbe.cliAvailable) return null; - return observeCodexActivation({ command: whichBinary('codex') }); + // Group E: replay the single captured host query instead of spawning again — + // the check list and the integration summary always describe the same fact. + if (hostObservation?.codexCommand != null && hostObservation.activationRunner !== null) { + return observeCodexActivation({ + command: hostObservation.codexCommand, + runner: hostObservation.activationRunner, + deliveryEvidenceVerification: deps.deliveryEvidenceVerification, + }); + } + return observeCodexActivation({ + command: whichBinary('codex'), + deliveryEvidenceVerification: deps.deliveryEvidenceVerification, + }); } interface CodexIntegration { @@ -1701,9 +1879,13 @@ interface CodexIntegration { /** * Build the additive integration summary + human projection from one snapshot. - * Doctor never mutates, so `deliveryComplete` reflects only whether target payload - * T is physically delivered (canonical present) — the same fact the human and JSON - * projections share, so they agree across repeated runs. + * Doctor never mutates. Group E: `deliveryComplete` is true only when the + * canonical payload is physically delivered AND a matching authenticated + * delivery record binds it (Decision 9); a missing/invalid/mismatched record on + * an otherwise green state presents as the one consistent `delivery-incomplete` + * result with the update/install recovery command. A harder exit-1 state + * (query-failed, unsafe cache, ...) keeps its own presentation — the summary + * still carries `deliveryComplete: false`, so the surfaces cannot disagree. */ function buildCodexIntegration(snapshot: CodexActivationSnapshot): CodexIntegration { const state = classifyCodexActivation(snapshot); @@ -1712,10 +1894,29 @@ function buildCodexIntegration(snapshot: CodexActivationSnapshot): CodexIntegrat snapshot, invocation: { entry: 'doctor', assertion: null }, }); - const deliveryComplete = snapshot.canonical.status === 'ok'; + const deliveryGate = assessSnapshotDelivery(snapshot); + const deliveryComplete = deliveryGate.kind === 'matching'; const summary = projectIntegrationSummary(state, snapshot, authorization, deliveryComplete); const descriptor = describeState(state); const projection = projectHumanStatus(state, snapshot); + if (deliveryGate.kind === 'incomplete' && descriptor.exit !== 1) { + const result = deliveryGate.result; + return { + summary: { + ...summary, + codexPlugin: { + ...summary.codexPlugin, + state: result.code, + mutationAuthority: result.authority, + actionRequired: true, + deliveryComplete: false, + recovery: result.recovery, + }, + }, + exit: result.exit, + human: { stream: 'stderr', text: `${result.detail}; ${result.recovery}` }, + }; + } return { summary, exit: descriptor.exit, @@ -1741,7 +1942,17 @@ export async function doctorCommand(options?: { json?: boolean; fix?: boolean }, deps.databaseRoot === null || typeof deps.databaseRoot === 'string' ? deps.databaseRoot : (gitRoots?.commonRoot ?? root); - const pluginProbe = deps.pluginProbe?.cliAvailable !== undefined ? deps.pluginProbe : probeCodexGeniePlugin(); + // Group E: ONE bounded host observation feeds the probe, the advisory, and + // (via the replay runner) the activation snapshot. It is skipped entirely + // only when tests inject both seams. + const hostObservation = + deps.pluginProbe?.cliAvailable !== undefined && deps.codexActivation !== undefined + ? null + : observeDoctorCodexHost(deps.codexHost); + const pluginProbe = + deps.pluginProbe?.cliAvailable !== undefined + ? deps.pluginProbe + : (hostObservation?.probe ?? probeCodexGeniePlugin()); const results: CheckResult[] = [ ...checkGenieBinary(), ...checkGit(root), @@ -1749,7 +1960,13 @@ export async function doctorCommand(options?: { json?: boolean; fix?: boolean }, ...checkSkills(root), ...checkBun(deps.bunVersion, deps.bunPath), ...checkSubagentModelOverride(), - ...(await checkCodexIntegration(root, pluginProbe)), + ...(await checkCodexIntegration(root, pluginProbe, hostObservation?.advisory ?? null)), + // Live context resolution only when the root itself was live-resolved: an + // injected root without an injected context is a unit-test seam, not a repo. + ...checkCodexProjectContext( + root, + deps.projectContext !== undefined ? deps.projectContext : injectedRoot ? null : undefined, + ), ...checkV4Residue(), ...checkAgentSync(), ...(await checkOmniHookTimeout()), @@ -1761,7 +1978,7 @@ export async function doctorCommand(options?: { json?: boolean; fix?: boolean }, // One bounded activation observation feeds the additive integration summary. // It is purely additive: `{ ok, checks }` meanings are unchanged, and a pending // Codex generation keeps `ok:true` while the command exits 2. - const activationSnapshot = resolveCodexActivationSnapshot(deps, pluginProbe); + const activationSnapshot = resolveCodexActivationSnapshot(deps, pluginProbe, hostObservation); const integration = activationSnapshot ? buildCodexIntegration(activationSnapshot) : null; if (options?.json) { diff --git a/src/genie-commands/install-promote.test.ts b/src/genie-commands/install-promote.test.ts index 28cd2c4c9..3858e009e 100644 --- a/src/genie-commands/install-promote.test.ts +++ b/src/genie-commands/install-promote.test.ts @@ -17,8 +17,13 @@ import { import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { LIFECYCLE_LEASE_OWNER_ENV, LIFECYCLE_LEASE_PATH_ENV, lifecycleLockPath } from '../lib/agent-sync.js'; +import { acquireLifecycleLease as acquireCodexLifecycleLease } from '../lib/codex-lifecycle-lease.js'; import { type InstallPromotionDependencies, recoverPendingInstallPromotions } from '../lib/install-promotion.js'; -import { InstallPromoteCommandError, installPromoteCommand } from './install-promote.js'; +import { + type InstallPromoteCommandDependencies, + InstallPromoteCommandError, + installPromoteCommand, +} from './install-promote.js'; const roots: string[] = []; const originalLeasePath = process.env[LIFECYCLE_LEASE_PATH_ENV]; @@ -111,6 +116,60 @@ describe('hidden installer promoter command', () => { expect(JSON.parse(output[0] as string)).toMatchObject({ outcome: 'committed', canonicalLink }); }); + test('a setup-held Codex lease refuses before recovery, swap, link, or VERSION mutation', () => { + const f = fixture(); + const held = acquireCodexLifecycleLease('setup-activation', { genieHome: f.genieHome }); + expect(held.ok).toBe(true); + if (!held.ok) return; + try { + expect(() => run(f)).toThrow('setup-activation'); + expect(readFileSync(join(f.bin, 'VERSION'), 'utf8')).toBe('1.0.0\n'); + expect(readFileSync(join(f.staging, 'VERSION'), 'utf8')).toBe('2.0.0\n'); + expect(existsSync(join(f.userHome, '.local'))).toBe(false); + expect(readdirSync(f.bin).some((name) => name.startsWith('.install-'))).toBe(false); + } finally { + held.release(); + } + }); + + test('acquires agent-sync then Codex and releases Codex then agent-sync around promotion', () => { + const f = fixture(); + const events: string[] = []; + const dependencies: InstallPromoteCommandDependencies = { + runtimeExecutable: join(f.staging, 'genie'), + runtimeVersion: '2.0.0', + userHome: f.userHome, + emit: () => undefined, + acquireLease: () => { + events.push('agent-acquire'); + return { path: f.leasePath, release: () => events.push('agent-release') }; + }, + acquireCodexLease: () => { + events.push('codex-acquire'); + return { + ok: true, + operationId: 'b'.repeat(32), + kind: 'install-converge', + assertOperation: () => undefined, + release: () => events.push('codex-release'), + }; + }, + promotion: { + randomId: () => '22222222-2222-4222-8222-222222222222', + beforeRename: () => { + if (!events.includes('promotion')) events.push('promotion'); + }, + }, + }; + + installPromoteCommand({ stagingRoot: f.staging, expectedVersion: '2.0.0' }, dependencies); + + expect(events.slice(0, 2)).toEqual(['agent-acquire', 'codex-acquire']); + expect(events).toContain('promotion'); + expect(events.slice(-2)).toEqual(['codex-release', 'agent-release']); + expect(events.indexOf('codex-acquire')).toBeLessThan(events.indexOf('promotion')); + }); + test('missing borrowed authority fails before any live or link mutation', () => { const f = fixture(); delete process.env[LIFECYCLE_LEASE_OWNER_ENV]; diff --git a/src/genie-commands/install-promote.ts b/src/genie-commands/install-promote.ts index ab29582d5..8e79c91a8 100644 --- a/src/genie-commands/install-promote.ts +++ b/src/genie-commands/install-promote.ts @@ -4,6 +4,7 @@ import { closeSync, fstatSync, lstatSync, + mkdirSync, mkdtempSync, openSync, readFileSync, @@ -19,6 +20,11 @@ import { acquireLifecycleLease, lifecycleLockPath, } from '../lib/agent-sync.js'; +import { + type HeldLifecycleLease, + type LifecycleLeaseResult, + acquireLifecycleLease as acquireCodexLifecycleLease, +} from '../lib/codex-lifecycle-lease.js'; import { type CanonicalInstallLinkGuard, preflightCanonicalInstallLink, @@ -43,6 +49,7 @@ import { physicalPathIdentitiesEqual, renamePathNoClobber, } from '../lib/install-transaction.js'; +import { releaseOrderedLifecycleLeases } from '../lib/ordered-lifecycle-leases.js'; import { VERSION } from '../lib/version.js'; const VERSION_PATTERN = /(?:^|[^0-9A-Za-z.+-])v?([0-9]+\.[0-9]+\.[0-9]+(?:[-+][0-9A-Za-z.-]+)?)(?:[^0-9A-Za-z.+-]|$)/; @@ -62,6 +69,7 @@ interface BorrowedLeaseGuard { export interface InstallPromoteCommandDependencies { acquireLease?: (genieHome: string) => LifecycleLease | { skipped: string }; + acquireCodexLease?: (genieHome: string) => LifecycleLeaseResult; runtimeExecutable?: string; runtimeVersion?: string; userHome?: string; @@ -166,6 +174,25 @@ function assertRunningVerifiedStage(stagingBinary: string, runtimeExecutable: st return staged; } +function ensurePhysicalInstallDirectory(path: string, label: string): void { + try { + mkdirSync(path, { mode: 0o700 }); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'EEXIST') { + throw new InstallPromoteCommandError(`could not create ${label}`, { cause: error }); + } + } + let stat: ReturnType; + try { + stat = lstatSync(path); + } catch (error) { + throw new InstallPromoteCommandError(`${label} disappeared during validation`, { cause: error }); + } + if (!stat.isDirectory() || stat.isSymbolicLink()) { + throw new InstallPromoteCommandError(`${label} is not a physical directory`); + } +} + function verifyExecutableVersion(binaryPath: string, expectedVersion: string): boolean { try { // Bounded: a hanging live binary must fail verification, not stall the @@ -240,11 +267,14 @@ export function installPromoteCommand( const stagingBinary = join(stagingRoot, 'genie'); const stagedIdentity = assertRunningVerifiedStage(stagingBinary, runtimeExecutable); const lease = acquireExactBorrowedInstallLease(genieHome, dependencies.acquireLease); + let codexLease: HeldLifecycleLease | null = null; let linkGuard: CanonicalInstallLinkGuard | null = null; let admitted: InstallStagingDirectoryGuard | null = null; let promotionComplete = false; const assertAuthority = (): void => { assertBorrowedLeaseUnchanged(lease); + if (codexLease === null) throw new InstallPromoteCommandError('Codex lifecycle lease is not held'); + codexLease.assertOperation(codexLease.operationId); if (linkGuard !== null) verifyCanonicalInstallLink(linkGuard); if (admitted !== null) verifyInstallStagingDirectory(admitted); }; @@ -256,6 +286,18 @@ export function installPromoteCommand( }, }; try { + const acquired = ( + dependencies.acquireCodexLease ?? ((home) => acquireCodexLifecycleLease('install-converge', { genieHome: home })) + )(genieHome); + if (!acquired.ok) { + throw new InstallPromoteCommandError( + `Codex lifecycle lease is busy (${acquired.holderKind ?? 'unknown'}); refusing installer promotion`, + ); + } + codexLease = acquired; + assertAuthority(); + ensurePhysicalInstallDirectory(join(genieHome, 'bin'), 'GENIE_HOME/bin'); + assertAuthority(); recoverPendingInstallPromotions({ genieHome, dependencies: promotionDependencies }); assertAuthority(); const userHome = resolve(dependencies.userHome ?? homedir()); @@ -320,7 +362,7 @@ export function installPromoteCommand( } } } finally { - lease.release(); + releaseOrderedLifecycleLeases(codexLease, lease); } } } diff --git a/src/genie-commands/install.test.ts b/src/genie-commands/install.test.ts index c8a98cf6d..6fe8c845a 100644 --- a/src/genie-commands/install.test.ts +++ b/src/genie-commands/install.test.ts @@ -27,8 +27,14 @@ import { import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { acquireLifecycleLease, lifecycleLockPath } from '../lib/agent-sync.js'; +import { VERSION } from '../lib/version.js'; import { convergeAuxiliaryTree } from './auxiliary-trees.js'; -import { type InstallOptions, installCommand, normalizeAuxLayout } from './install.js'; +import { + type InstallOptions, + normalizeAuxLayout, + runInstallAgentSync, + installCommand as runInstallCommand, +} from './install.js'; import type { cleanupV4 } from './legacy-v4.js'; function makeCleanupSpy(): { runner: typeof cleanupV4; calls: () => number } { @@ -57,6 +63,14 @@ const noopCodexLease = () => release: () => undefined, }) as const; const noopConsent = () => undefined; +const noopDeliveryRepair = async () => ({ action: 'proceed-current' as const }); +const currentCodexTarget = () => ({ installedVersion: VERSION }); + +/** Keep every command-wiring test isolated from the operator's real install marker. */ +function installCommand(...args: Parameters): ReturnType { + args[10] ??= () => undefined; + return runInstallCommand(...args); +} describe('standalone install.sh lifecycle lease', () => { let root: string; @@ -128,7 +142,7 @@ describe('standalone install.sh lifecycle lease', () => { } detect_legacy_install() { assert_lease; } ensure_path_wired() { assert_lease; } - handoff_to_subcommand() { assert_lease; } + handoff_to_subcommand() { assert_lease; test "$1" = 'stable'; } verify_installation() { assert_lease; test "$1" = '5.9.0'; } main test -z "$LIFECYCLE_LOCK" @@ -364,36 +378,99 @@ describe('standalone install.sh lifecycle lease', () => { expect(collision.stderr).toContain('version mismatch (expected 5.9.0, got 15.9.0)'); }); + test('shell promotion refusal leaves an absent GENIE_HOME untouched before the staged promoter runs', () => { + const marker = join(home, 'promoter-refused'); + const result = shell(` + source "$1" + tar() { + local destination="" + while (( "$#" )); do + if [[ "$1" == "-C" ]]; then destination="$2"; shift 2; else shift; fi + done + printf '%s\\n' \ + '#!/usr/bin/env bash' \ + 'if [[ "\${1:-}" == "--version" ]]; then printf "genie v5.9.0\\\\n"; exit 0; fi' \ + 'if [[ "\${1:-}" == "__install-promote" ]]; then' \ + ' [[ ! -e "$GENIE_HOME" ]] || exit 91' \ + ' printf refused > "$HOME/promoter-refused"' \ + ' exit 73' \ + 'fi' > "$destination/genie" + chmod 755 "$destination/genie" + } + acquire_lifecycle_lock + extract_and_link /fixture/payload.tar.gz 5.9.0 + `); + + expect(result.status).toBe(1); + expect(result.stderr).toContain('verified staged promoter could not complete the install transaction'); + expect(readFileSync(marker, 'utf8')).toBe('refused'); + expect(existsSync(genieHome)).toBe(false); + expect(existsSync(join(home, '.local'))).toBe(false); + }); + test('passes the exact owner record to the child finisher and treats failure as fatal', () => { const localBin = join(home, '.local', 'bin'); const observed = join(root, 'observed-owner'); mkdirSync(localBin, { recursive: true }); writeFileSync( join(localBin, 'genie'), - `#!/usr/bin/env bash\nset -euo pipefail\n[[ "\${1:-}" == install ]]\n[[ "$(sed -n '1p' "$GENIE_LIFECYCLE_LEASE_PATH")" == "$GENIE_LIFECYCLE_LEASE_OWNER" ]]\nprintf '%s' "$GENIE_LIFECYCLE_LEASE_OWNER" > ${JSON.stringify(observed)}\n`, + `#!/usr/bin/env bash\nset -euo pipefail\n[[ "\${1:-}" == install ]]\n[[ "$(sed -n '1p' "$GENIE_LIFECYCLE_LEASE_PATH")" == "$GENIE_LIFECYCLE_LEASE_OWNER" ]]\nprintf '%s\\n%s\\n' "$GENIE_LIFECYCLE_LEASE_OWNER" "$GENIE_INSTALL_DELIVERY_CHANNEL" > ${JSON.stringify(observed)}\n`, { mode: 0o755 }, ); const success = shell( - 'source "$1"; acquire_lifecycle_lock; handoff_to_subcommand; test -f "$LIFECYCLE_LOCK"; release_lifecycle_lock', + 'source "$1"; acquire_lifecycle_lock; handoff_to_subcommand dev; test -f "$LIFECYCLE_LOCK"; release_lifecycle_lock', ); expect(success.status).toBe(0); - expect(readFileSync(observed, 'utf8')).toMatch(/^[0-9]+:[a-f0-9]{32}:unknown$/); + expect(readFileSync(observed, 'utf8')).toMatch(/^[0-9]+:[a-f0-9]{32}:unknown\ndev\n$/); writeFileSync(join(localBin, 'genie'), '#!/usr/bin/env bash\nexit 19\n', { mode: 0o755 }); - const failure = shell('source "$1"; acquire_lifecycle_lock; handoff_to_subcommand'); + const failure = shell('source "$1"; acquire_lifecycle_lock; handoff_to_subcommand stable'); expect(failure.status).toBe(1); expect(failure.stderr).toContain('installation remains incomplete and retryable'); expect(existsSync(lifecycleLockPath(genieHome))).toBe(false); }); + + test('hands the exact bootstrapped gh to a child on a host with no system gh', () => { + const localBin = join(home, '.local', 'bin'); + const bootstrapBin = join(root, 'bootstrap', 'bin'); + const bootstrappedGh = join(bootstrapBin, 'gh'); + mkdirSync(localBin, { recursive: true }); + mkdirSync(bootstrapBin, { recursive: true }); + writeFileSync(bootstrappedGh, '#!/bin/sh\nexit 0\n', { mode: 0o755 }); + writeFileSync( + join(localBin, 'genie'), + `#!/bin/bash +set -euo pipefail +[[ "\${1:-}" == install ]] +[[ "$GH_BIN" == ${JSON.stringify(bootstrappedGh)} ]] +[[ "$(command -v gh)" == "$GH_BIN" ]] +`, + { mode: 0o755 }, + ); + + const result = shell(` + source "$1" + acquire_lifecycle_lock + GH_BIN=${JSON.stringify(bootstrappedGh)} + inherited_path="$PATH" + PATH="${join(root, 'no-system-gh')}" + handoff_to_subcommand stable + PATH="$inherited_path" + release_lifecycle_lock + `); + + expect(result.status).toBe(0); + expect(result.stderr).not.toContain('installation remains incomplete and retryable'); + }); }); describe('installCommand', () => { - test('runs v4 cleanup + layout normalize + agent sync by default', () => { + test('runs v4 cleanup + layout normalize + agent sync by default', async () => { const spy = makeCleanupSpy(); let normalizeCalls = 0; let syncCalls = 0; - installCommand( + await installCommand( {}, spy.runner, () => { @@ -407,17 +484,19 @@ describe('installCommand', () => { noopLease, noopCodexLease, noopConsent, + () => null, + noopDeliveryRepair, ); expect(spy.calls()).toBe(1); expect(normalizeCalls).toBe(1); expect(syncCalls).toBe(1); }); - test('--skip-v4-cleanup skips ONLY the cleanup; normalize + sync still run', () => { + test('--skip-v4-cleanup skips ONLY the cleanup; normalize + sync still run', async () => { const spy = makeCleanupSpy(); let normalizeCalls = 0; let syncCalls = 0; - installCommand( + await installCommand( { skipV4Cleanup: true }, spy.runner, () => { @@ -431,15 +510,17 @@ describe('installCommand', () => { noopLease, noopCodexLease, noopConsent, + () => null, + noopDeliveryRepair, ); expect(spy.calls()).toBe(0); expect(normalizeCalls).toBe(1); expect(syncCalls).toBe(1); }); - test('--skip-integrations maps to none', () => { + test('--skip-integrations maps to none', async () => { let selection = ''; - installCommand( + await installCommand( {}, makeCleanupSpy().runner, () => undefined, @@ -451,8 +532,10 @@ describe('installCommand', () => { noopLease, noopCodexLease, noopConsent, + () => null, + noopDeliveryRepair, ); - installCommand( + await installCommand( { skipIntegrations: true }, makeCleanupSpy().runner, () => undefined, @@ -464,11 +547,13 @@ describe('installCommand', () => { noopLease, noopCodexLease, noopConsent, + () => null, + noopDeliveryRepair, ); expect(selection).toBe('none'); }); - test('agent-sync selection passes through unchanged (restore-hermes-sync-leg): codex/none skip it, auto/all/claude reach it as-is', () => { + test('agent-sync selection passes through unchanged (restore-hermes-sync-leg): codex/none skip it, auto/all/claude reach it as-is', async () => { const observed: string[] = []; const runFor = (integrations: 'codex' | 'none' | 'auto' | 'all' | 'claude') => installCommand( @@ -480,65 +565,48 @@ describe('installCommand', () => { noopLease, noopCodexLease, noopConsent, + integrations === 'codex' || integrations === 'all' ? currentCodexTarget : () => null, + noopDeliveryRepair, ); - // codex converges through runIntegrations only — agent-sync never writes - // ~/.agents/skills at all now (R2/A1 is structural in runAgentSync), so - // codex/none simply have nothing for agent-sync to do. - runFor('codex'); - runFor('none'); + // Codex never reaches runIntegrations or agent-sync. `none` likewise has no + // sync target. + await runFor('codex'); + await runFor('none'); expect(observed).toEqual([]); // auto/all/claude pass through UNCHANGED — narrowAgentSyncSelection no // longer collapses them to 'claude', which is what silently killed the // hermes leg (runAgentSync's hermes gate needs 'auto'/'all' verbatim). - runFor('auto'); - runFor('all'); - runFor('claude'); + await runFor('auto'); + await runFor('all'); + await runFor('claude'); expect(observed).toEqual(['auto', 'all', 'claude']); }); - test('a failed codex integration gates the Claude skill sync so Claude trees stay byte-identical (A2)', () => { - const observed: string[] = []; - installCommand( + test('Codex is structurally excluded from the integration runner after delivery authentication', async () => { + const scopes: Array<{ selection?: string; codex?: boolean }> = []; + const observedSync: string[] = []; + await installCommand( { integrations: 'auto' }, makeCleanupSpy().runner, () => undefined, - (selection) => observed.push(selection), - () => [{ runtime: 'codex' as const, ok: false, detail: 'plugin-incapable Codex' }], + (selection) => observedSync.push(selection), + (options) => { + scopes.push({ selection: options?.selection, codex: options?.detected?.codex }); + return [{ runtime: 'claude' as const, ok: true, detail: 'claude current' }]; + }, noopLease, noopCodexLease, noopConsent, + currentCodexTarget, + noopDeliveryRepair, ); - expect(observed).toEqual([]); + expect(scopes).toEqual([{ selection: 'auto', codex: false }]); + expect(observedSync).toEqual(['auto']); }); - test('a codex-gated Claude skip under --integrations auto prints an explicit advisory (not a silent exit 0)', () => { - const lines: string[] = []; - const originalLog = console.log; - console.log = (...args: unknown[]) => lines.push(args.map(String).join(' ')); - try { - installCommand( - { integrations: 'auto' }, - makeCleanupSpy().runner, - () => undefined, - () => { - throw new Error('runSync must not be called when codex gates the Claude sync'); - }, - () => [{ runtime: 'codex' as const, ok: false, detail: 'plugin-incapable Codex' }], - noopLease, - noopCodexLease, - noopConsent, - ); - const output = lines.join('\n'); - expect(output).toContain('Skipped agent-sync'); - expect(output).toContain('codex integration failed'); - } finally { - console.log = originalLog; - } - }); - - test('explicit integration failures are fatal while auto failures warn', () => { - const failing = () => [{ runtime: 'codex' as const, ok: false, detail: 'missing' }]; - expect(() => + test('explicit integration failures are fatal while auto failures warn', async () => { + const failing = () => [{ runtime: 'claude' as const, ok: false, detail: 'missing' }]; + await expect( installCommand( {}, makeCleanupSpy().runner, @@ -548,11 +616,13 @@ describe('installCommand', () => { noopLease, noopCodexLease, noopConsent, + () => null, + noopDeliveryRepair, ), - ).not.toThrow(); - expect(() => + ).resolves.toBeUndefined(); + await expect( installCommand( - { integrations: 'codex' }, + { integrations: 'claude' }, makeCleanupSpy().runner, () => undefined, () => {}, @@ -560,23 +630,25 @@ describe('installCommand', () => { noopLease, noopCodexLease, noopConsent, + () => null, + noopDeliveryRepair, ), - ).toThrow('Requested integration failed'); + ).rejects.toThrow('Requested integration failed'); }); - test('install prints hook review guidance only when hook definition bytes changed', () => { + test('non-Codex integration results cannot claim that Codex hook review is required', async () => { const lines: string[] = []; const originalLog = console.log; console.log = (...args: unknown[]) => lines.push(args.map(String).join(' ')); const run = (hookReviewRequired: boolean) => installCommand( - { integrations: 'codex' }, + { integrations: 'claude' }, makeCleanupSpy().runner, () => undefined, () => undefined, () => [ { - runtime: 'codex', + runtime: 'claude', ok: true, detail: 'fixture integration current', hookReviewRequired, @@ -587,20 +659,20 @@ describe('installCommand', () => { noopConsent, ); try { - run(false); + await run(false); expect(lines.join('\n')).not.toContain('Review Genie hooks with /hooks'); lines.length = 0; - run(true); - expect(lines.join('\n')).toContain('Review Genie hooks with /hooks, then start a new Codex task.'); + await run(true); + expect(lines.join('\n')).not.toContain('Review Genie hooks with /hooks'); } finally { console.log = originalLog; } }); - test('rejects an invalid integration option before every finisher side effect', () => { + test('rejects an invalid integration option before every finisher side effect', async () => { const calls: string[] = []; const invalid = { integrations: 'codxe' } as unknown as InstallOptions; - expect(() => + await expect( installCommand( invalid, (() => calls.push('cleanup')) as unknown as typeof cleanupV4, @@ -619,9 +691,34 @@ describe('installCommand', () => { noopCodexLease, noopConsent, ), - ).toThrow('Invalid --integrations value: codxe'); + ).rejects.toThrow('Invalid --integrations value: codxe'); expect(calls).toEqual([]); }); + + test('rejects a malformed standalone-installer channel before acquiring a lifecycle lease', async () => { + const priorChannel = process.env.GENIE_INSTALL_DELIVERY_CHANNEL; + let acquired = false; + process.env.GENIE_INSTALL_DELIVERY_CHANNEL = 'main-evil'; + try { + await expect( + installCommand( + {}, + makeCleanupSpy().runner, + () => undefined, + () => undefined, + () => [], + () => { + acquired = true; + return noopLease(); + }, + ), + ).rejects.toThrow('Invalid installer delivery channel'); + expect(acquired).toBe(false); + } finally { + if (priorChannel === undefined) Reflect.deleteProperty(process.env, 'GENIE_INSTALL_DELIVERY_CHANNEL'); + else process.env.GENIE_INSTALL_DELIVERY_CHANNEL = priorChannel; + } + }); }); describe('normalizeAuxLayout', () => { @@ -1103,67 +1200,244 @@ describe('installCommand — Group C install gate (item 2)', () => { process.exitCode = savedExit; }); - test('a pending Codex generation (N≠T) is deferred: role agents only, exit 2 + trailer, no plugin convergence for codex', () => { - const runScopes: Array = []; - installCommand( + test('publishes before permitted non-Codex work and defers a pending generation with zero Codex mutation', async () => { + const events: string[] = []; + const cleanup = makeCleanupSpy(); + await installCommand( { integrations: 'all' }, - makeCleanupSpy().runner, - () => undefined, - () => undefined, + ((...args: Parameters) => { + events.push('cleanup'); + return cleanup.runner(...args); + }) as typeof cleanupV4, + () => { + events.push('normalize'); + return undefined; + }, + (selection) => events.push(`sync:${selection}`), (options) => { - runScopes.push(options?.selection); - // runIntegrations is called with the claude-only scope; it converges claude, never codex. + events.push(`integrations:${options?.selection}`); return [{ runtime: 'claude', ok: true, detail: 'plugin refreshed' }]; }, noopLease, noopCodexLease, - noopConsent, - // Force the pending classification (installed N=5.260710.2 ≠ delivered T). + () => events.push('consent'), () => ({ installedVersion: '5.260710.2' }), + async () => { + events.push('repair'); + return { action: 'exit-handoff' }; + }, + () => events.push('retire-marker'), ); - // runIntegrations was scoped to claude — codex is NEVER plugin-converged here. - expect(runScopes).toEqual(['claude']); - // Exit 2, one result trailer, and the deferral names N + the retire recovery. + expect(events).toEqual(['normalize', 'repair', 'cleanup', 'integrations:claude', 'sync:all', 'retire-marker']); expect(process.exitCode).toBe(2); expect(logs.filter((l) => l.includes('"deliveryComplete":true'))).toHaveLength(1); const codexLine = logs.find((l) => l.includes('codex:')); - expect(codexLine).toContain('Codex plugin left at v5.260710.2 (no cache advance)'); + expect(codexLine).toContain('Codex plugin left at v5.260710.2 (no activation-owned mutation)'); expect(codexLine).toContain('retire tasks → genie setup --codex → /hooks → new task'); }); - test('a fresh/absent or same-version install (classifier null) converges the full selection normally, exit 0', () => { - const runScopes: Array = []; - installCommand( + test('failed or advanced publication is terminal before consent, cleanup, integration, sync, marker retirement, or success', async () => { + const advancedManifest = { + schema_version: 1, + channel: 'stable' as const, + version: '5.260723.8', + released_at: '2026-07-23T00:00:00Z', + tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v5.260723.8', + platforms: ['darwin-arm64'], + manifestBytes: '{"version":"5.260723.8"}\n', + manifestSha256: 'a'.repeat(64), + }; + for (const directive of [ + { action: 'failed' as const, detail: 'delivery store is unwritable' }, + { action: 'route-upgrade' as const, manifest: advancedManifest }, + ]) { + const events: string[] = []; + logs.length = 0; + process.exitCode = 0; + await installCommand( + { integrations: 'codex' }, + (() => { + events.push('cleanup'); + return makeCleanupSpy().runner(); + }) as typeof cleanupV4, + () => { + events.push('normalize'); + return undefined; + }, + () => events.push('sync'), + () => { + events.push('integrations'); + return []; + }, + noopLease, + noopCodexLease, + () => events.push('consent'), + () => ({ installedVersion: null }), + async () => { + events.push('repair'); + return directive; + }, + () => events.push('retire-marker'), + ); + expect(events).toEqual(['normalize', 'repair']); + expect(process.exitCode).toBe(1); + expect(logs.filter((line) => line.includes('"deliveryComplete":true'))).toHaveLength(0); + expect(logs.join('\n')).not.toContain('authenticated delivery v'); + const trailer = logs.find((line) => line.includes('"code":"delivery-incomplete"')); + expect(trailer).toBeDefined(); + expect(JSON.parse(trailer as string)).toMatchObject({ deliveryComplete: false, retry: true }); + if (directive.action === 'route-upgrade') expect(logs.join('\n')).toContain(advancedManifest.version); + } + }); + + test('a target-current authenticated install leaves Codex untouched and permits only Claude integration', async () => { + const events: string[] = []; + await installCommand( { integrations: 'all' }, makeCleanupSpy().runner, () => undefined, - () => undefined, + () => events.push('sync'), (options) => { - runScopes.push(options?.selection); - return [ - { runtime: 'codex', ok: true, detail: 'plugin/hooks refreshed' }, - { runtime: 'claude', ok: true, detail: 'plugin refreshed' }, - ]; + events.push(`integrations:${options?.selection}`); + return [{ runtime: 'claude', ok: true, detail: 'plugin refreshed' }]; }, noopLease, noopCodexLease, noopConsent, - () => null, // classifier: not pending → normal convergence + currentCodexTarget, + async () => { + events.push('repair'); + return { action: 'repaired-current' }; + }, + () => events.push('retire-marker'), ); - // Full selection reaches runIntegrations; no deferral, no exit-2 trailer. - expect(runScopes).toEqual(['all']); + expect(events).toEqual(['repair', 'integrations:claude', 'sync', 'retire-marker']); expect(process.exitCode).toBe(0); - expect(logs.join('\n')).not.toContain('deliveryComplete'); + expect(logs.join('\n')).not.toContain('"deliveryComplete":true'); + expect(logs.join('\n')).toContain('no activation-owned mutation'); + }); + + test('only an explicitly Claude-only install persists maintenance consent', async () => { + const persisted: string[] = []; + for (const integrations of ['codex', 'auto', 'all', 'none', 'claude'] as const) { + await installCommand( + { integrations }, + makeCleanupSpy().runner, + () => undefined, + () => undefined, + () => [], + noopLease, + noopCodexLease, + (selection) => persisted.push(`${integrations}:${selection}`), + integrations === 'codex' || integrations === 'auto' || integrations === 'all' ? currentCodexTarget : () => null, + noopDeliveryRepair, + ); + } + expect(persisted).toEqual(['claude:claude']); + }); + + test('install-owned agent sync disables setup-owned Codex role convergence', () => { + let captured: Parameters[0] | undefined; + runInstallAgentSync('all', (options) => { + captured = options; + return null; + }); + expect(captured?.selection).toBe('all'); + expect(captured?.strict).toBe(true); + expect(captured?.codexRefresh).toBeUndefined(); }); - test('a busy Codex lifecycle lease projects exit 2 codex-lifecycle-busy / deliveryComplete:false with ZERO plugin convergence (AC8 loser)', () => { + test('fresh dev and homolog installer handoffs authenticate the exact selected channel', async () => { + const priorChannel = process.env.GENIE_INSTALL_DELIVERY_CHANNEL; + try { + for (const selectedChannel of ['dev', 'homolog'] as const) { + process.env.GENIE_INSTALL_DELIVERY_CHANNEL = selectedChannel; + let repairedChannel = ''; + await installCommand( + { integrations: 'codex' }, + makeCleanupSpy().runner, + () => undefined, + () => undefined, + (options) => { + expect(options?.selection).toBe('none'); + return []; + }, + noopLease, + noopCodexLease, + noopConsent, + () => ({ installedVersion: null }), + async (channel) => { + repairedChannel = channel; + return { action: 'exit-handoff' }; + }, + () => undefined, + ); + expect(repairedChannel).toBe(selectedChannel); + } + } finally { + if (priorChannel === undefined) Reflect.deleteProperty(process.env, 'GENIE_INSTALL_DELIVERY_CHANNEL'); + else process.env.GENIE_INSTALL_DELIVERY_CHANNEL = priorChannel; + } + }); + + test('a later permitted integration failure preserves the install marker after successful publication', async () => { + let retired = false; + await expect( + installCommand( + { integrations: 'all' }, + makeCleanupSpy().runner, + () => undefined, + () => undefined, + () => [{ runtime: 'claude', ok: false, detail: 'claude integration failed' }], + noopLease, + noopCodexLease, + noopConsent, + currentCodexTarget, + async () => ({ action: 'repaired-current' }), + () => { + retired = true; + }, + ), + ).rejects.toThrow('Requested integration failed: claude'); + expect(retired).toBe(false); + }); + + test('a later permitted agent-sync failure preserves the install marker after successful publication', async () => { + let retired = false; + await expect( + installCommand( + { integrations: 'all' }, + makeCleanupSpy().runner, + () => undefined, + () => { + throw new Error('agent sync failed'); + }, + () => [{ runtime: 'claude', ok: true, detail: 'claude integration current' }], + noopLease, + noopCodexLease, + noopConsent, + currentCodexTarget, + async () => ({ action: 'repaired-current' }), + () => { + retired = true; + }, + ), + ).rejects.toThrow('agent sync failed'); + expect(retired).toBe(false); + }); + + test('a busy Codex lifecycle lease projects exit 2 codex-lifecycle-busy / deliveryComplete:false with ZERO plugin convergence (AC8 loser)', async () => { let integrationsRan = false; + let normalizeRan = false; let syncRan = false; let leaseAcquired = false; - installCommand( + await installCommand( { integrations: 'auto' }, makeCleanupSpy().runner, - () => undefined, + () => { + normalizeRan = true; + return undefined; + }, () => { syncRan = true; }, @@ -1187,7 +1461,9 @@ describe('installCommand — Group C install gate (item 2)', () => { () => null, ); expect(leaseAcquired).toBe(true); - // Zero mutation: neither plugin convergence nor agent-sync ran after the refusal. + // Zero payload mutation: normalization, plugin convergence, and agent-sync + // all remain behind the unconditional inner lease. + expect(normalizeRan).toBe(false); expect(integrationsRan).toBe(false); expect(syncRan).toBe(false); // Exit 2 with the codex-lifecycle-busy trailer (deliveryComplete:false, retry:true), naming the holder. @@ -1200,30 +1476,134 @@ describe('installCommand — Group C install gate (item 2)', () => { expect(JSON.parse(trailer as string)).toMatchObject({ deliveryComplete: false, retry: true }); }); - test('a claude/none install never acquires the Codex lifecycle lease (no spurious cross-command contention)', () => { - let leaseTouched = false; - const acquire = () => { - leaseTouched = true; - return noopCodexLease(); - }; + test('a claude/none install acquires Codex before payload normalization and releases inner then outer', async () => { for (const integrations of ['claude', 'none'] as const) { - leaseTouched = false; - installCommand( + const events: string[] = []; + await installCommand( { integrations }, makeCleanupSpy().runner, + () => { + events.push('normalize'); + return undefined; + }, + () => undefined, + () => [], + () => { + events.push('agent-acquire'); + return { + path: '/tmp/test-lifecycle.lock', + release: () => events.push('agent-release'), + }; + }, + () => { + events.push('codex-acquire'); + return { + ok: true as const, + operationId: 'a'.repeat(32), + kind: 'install-converge' as const, + assertOperation: () => undefined, + release: () => events.push('codex-release'), + }; + }, + noopConsent, + () => null, + ); + expect(events).toEqual(['agent-acquire', 'codex-acquire', 'normalize', 'codex-release', 'agent-release']); + } + }); + + test('an inner release failure still releases and immediately reacquires the outer lifecycle authority', async () => { + const root = mkdtempSync(join(tmpdir(), 'genie-install-release-failure-')); + const genieHome = join(root, '.genie'); + const events: string[] = []; + try { + await expect( + installCommand( + { integrations: 'none' }, + makeCleanupSpy().runner, + () => undefined, + () => undefined, + () => [], + () => { + const acquired = acquireLifecycleLease(genieHome); + if ('skipped' in acquired) throw new Error(`fixture could not acquire outer lease: ${acquired.skipped}`); + return { + path: acquired.path, + release: () => { + events.push('agent-release'); + acquired.release(); + }, + }; + }, + () => ({ + ok: true as const, + operationId: 'a'.repeat(32), + kind: 'install-converge' as const, + assertOperation: () => undefined, + release: () => { + events.push('codex-release'); + throw new Error('inner release fixture'); + }, + }), + noopConsent, + () => null, + ), + ).rejects.toThrow('inner release fixture'); + + expect(events).toEqual(['codex-release', 'agent-release']); + expect(existsSync(lifecycleLockPath(genieHome))).toBe(false); + const reacquired = acquireLifecycleLease(genieHome); + if ('skipped' in reacquired) throw new Error(`outer lease remained stranded: ${reacquired.skipped}`); + reacquired.release(); + expect(existsSync(lifecycleLockPath(genieHome))).toBe(false); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); + + test('dual release failures report both errors in reverse-release order', async () => { + const events: string[] = []; + let thrown: unknown; + try { + await installCommand( + { integrations: 'none' }, + makeCleanupSpy().runner, () => undefined, () => undefined, () => [], - noopLease, - acquire, + () => ({ + path: '/tmp/test-lifecycle.lock', + release: () => { + events.push('agent-release'); + throw new Error('outer release fixture'); + }, + }), + () => ({ + ok: true as const, + operationId: 'a'.repeat(32), + kind: 'install-converge' as const, + assertOperation: () => undefined, + release: () => { + events.push('codex-release'); + throw new Error('inner release fixture'); + }, + }), noopConsent, () => null, ); - expect(leaseTouched).toBe(false); + } catch (error) { + thrown = error; } + + expect(events).toEqual(['codex-release', 'agent-release']); + expect(thrown).toBeInstanceOf(AggregateError); + expect((thrown as AggregateError).errors.map((error) => (error as Error).message)).toEqual([ + 'inner release fixture', + 'outer release fixture', + ]); }); - test('a held Codex lifecycle lease is acquired for an in-scope install and released on the terminal path', () => { + test('a held Codex lifecycle lease is acquired for an in-scope install and released on the terminal path', async () => { let released = false; const acquire = () => ({ ok: true as const, @@ -1234,7 +1614,7 @@ describe('installCommand — Group C install gate (item 2)', () => { released = true; }, }); - installCommand( + await installCommand( { integrations: 'codex' }, makeCleanupSpy().runner, () => undefined, @@ -1243,7 +1623,8 @@ describe('installCommand — Group C install gate (item 2)', () => { noopLease, acquire, noopConsent, - () => null, + currentCodexTarget, + noopDeliveryRepair, ); expect(process.exitCode).toBe(0); expect(released).toBe(true); diff --git a/src/genie-commands/install.ts b/src/genie-commands/install.ts index f7ecec2f4..12ebcd6ee 100644 --- a/src/genie-commands/install.ts +++ b/src/genie-commands/install.ts @@ -3,39 +3,43 @@ * * install.sh downloads, verifies, extracts, links and PATH-wires the binary in * bash, then hands off to `genie install` on the freshly linked binary for the - * finishing steps that belong in TypeScript. v5 keeps this deliberately thin: - * v4 legacy cleanup, then the layout normalization + agent-sync phase that - * converges every detected coding agent from the canonical source root. + * finishing steps that belong in TypeScript. v5 authenticates and deep-publishes + * the Codex delivery before v4 cleanup and permitted non-Codex convergence; + * explicit `setup --codex` alone owns Codex activation and managed roles. * * Opt out of the v4 cleanup with `--skip-v4-cleanup` — install.sh forwards its * CLI args, so `curl ... | bash -s -- --skip-v4-cleanup` reaches this flag. The - * layout-normalize + agent-sync steps always run: install must converge agents. + * layout-normalize always runs; the selected non-Codex sync scope runs only + * after authenticated delivery publication succeeds. */ import { readFileSync, writeFileSync } from 'node:fs'; import { homedir } from 'node:os'; import { join } from 'node:path'; import { type LifecycleLease, acquireLifecycleLease } from '../lib/agent-sync.js'; +import type { DeliveryEvidenceChannel } from '../lib/codex-delivery-evidence.js'; import { type HeldLifecycleLease, type LifecycleLeaseResult, acquireLifecycleLease as acquireCodexLifecycleLease, } from '../lib/codex-lifecycle-lease.js'; +import { genieConfigExists, getGenieConfigPath } from '../lib/genie-config.js'; +import { retireInstallVersionMarker } from '../lib/install-version-marker.js'; +import { acquireOrderedLifecycleLeases, releaseOrderedLifecycleLeases } from '../lib/ordered-lifecycle-leases.js'; import { type InstallIntegrationsOptions, type IntegrationResult, type IntegrationSelection, - installCodexAgents, installRuntimeIntegrations, parseCodexPluginState, persistIntegrationConsent, - resolveBundleRoot, resolveRuntimeExecutable, runBoundedIntegrationCommand, } from '../lib/runtime-integrations.js'; import { VERSION } from '../lib/version.js'; import { type AuxiliaryTreeOperations, type AuxiliaryTreeOutcome, convergeAuxiliaryTree } from './auxiliary-trees.js'; import { + CODEX_DELIVERY_INCOMPLETE_TRAILER, CODEX_DELIVERY_RESULT_TRAILER, CODEX_LIFECYCLE_BUSY_TRAILER, CODEX_RETIRE_RECOVERY, @@ -43,7 +47,12 @@ import { classifyCodexDelivery, } from './codex-delivery.js'; import { cleanupV4 } from './legacy-v4.js'; -import { runAgentSyncSafe } from './update.js'; +import { + type AlreadyCurrentRepairDirective, + attemptAlreadyCurrentDeliveryRepair, + resolvePlatformId, + runAgentSyncSafe, +} from './update.js'; const GENIE_HOME = process.env.GENIE_HOME || join(homedir(), '.genie'); @@ -75,42 +84,48 @@ type LifecycleLeaseAcquirer = () => LifecycleLease | { skipped: string }; type CodexLifecycleLeaseAcquirer = () => LifecycleLeaseResult; type ConsentWriter = (selection: IntegrationSelection) => void; -/** A pending install delivery: the installed generation N differs from the delivered T (=VERSION). */ -export interface CodexInstallDeferral { - installedVersion: string; +/** + * A detected Codex runtime whose installed generation must remain untouched by + * install. `null` means absent or unobservable; setup owns every activation. + */ +export interface CodexInstallTarget { + installedVersion: string | null; } -type CodexInstallClassifier = (selection: IntegrationSelection) => CodexInstallDeferral | null; +type CodexInstallClassifier = (selection: IntegrationSelection) => CodexInstallTarget | null; +type InstallDeliveryRepair = ( + channel: DeliveryEvidenceChannel, + platformId: string, + lease: HeldLifecycleLease, +) => Promise; +type InstallMarkerRetirer = () => void; /** - * Group C install gate (item 2). `genie install` runs on the freshly linked - * binary (T = VERSION). If a Codex plugin generation N is already installed and - * N ≠ T, an install would otherwise `plugin add` and advance the cache, pruning - * the generation a live task references — the same 2026-07-11 hazard as update, - * on the curl|bash reinstall vector. This classifier (shared `classifyCodexDelivery`, - * observed reality: N from a live `codex plugin list`) reports that pending case - * so the caller defers activation. A fresh install (absent plugin) or a - * same-version install returns null and activates/converges normally. + * Detect Codex without granting install any activation authority. A runnable + * Codex command always returns a target, including fresh/absent, same-version, + * malformed, or temporarily unobservable plugin state. The authenticated + * delivery record is published first; only `setup --codex` may later mutate the + * journal, registration/cache, enabled state, project route, or managed roles. */ -function classifyCodexInstallDefault(selection: IntegrationSelection): CodexInstallDeferral | null { +function classifyCodexInstallDefault(selection: IntegrationSelection): CodexInstallTarget | null { if (!codexInScope(selection)) return null; let command: string | null; try { command = resolveRuntimeExecutable('codex', process.cwd()); } catch { - return null; + return { installedVersion: null }; } - if (command === null) return null; + // Delivery publication authenticates the installed Genie payload, not the + // presence of a Codex executable. Publish now so installing Genie before + // Codex does not make later setup unrecoverably delivery-incomplete. + if (command === null) return { installedVersion: null }; const result = runBoundedIntegrationCommand(command, ['plugin', 'list', '--json'], { timeoutMs: 15_000, maxOutputBytes: 64 * 1024, }); - if (result.timedOut || result.outputOverflow || result.exitCode !== 0) return null; + if (result.timedOut || result.outputOverflow || result.exitCode !== 0) return { installedVersion: null }; const parsed = parseCodexPluginState(result.stdout); - if (!parsed.ok || !parsed.state.installed) return null; - const installedVersion = parsed.state.version ?? null; - if (installedVersion === null) return null; - const state = classifyCodexDelivery(installedVersion, VERSION); - return state.kind === 'pending' ? { installedVersion } : null; + if (!parsed.ok || !parsed.state.installed) return { installedVersion: null }; + return { installedVersion: parsed.state.version ?? null }; } function codexInScope(selection: IntegrationSelection): boolean { @@ -125,30 +140,98 @@ function claudeOnlyScope(selection: IntegrationSelection): InstallIntegrationsOp } /** - * Deferred install of a pending Codex generation: converge only the non-plugin - * role agents and return an action-required exit-2 result (deliveryComplete:true) - * naming N and the retire recovery. Never runs a plugin/cache command. + * Report an authenticated Codex delivery without mutating Codex. The result is + * informational only; the caller emits the completion trailer after publication + * and leaves all activation work to `setup --codex`. */ -function buildInstallCodexDeferral(installedVersion: string): IntegrationResult { - let agentDetail = ''; - const bundleRoot = resolveBundleRoot(); - if (bundleRoot !== null) { - try { - const agents = installCodexAgents(bundleRoot); - agentDetail = agents.installed > 0 ? `; role agents refreshed (${agents.installed})` : ''; - } catch (error) { - agentDetail = `; role-agent refresh failed: ${error instanceof Error ? error.message : String(error)}`; - } - } +function buildInstallCodexDeferral(target: CodexInstallTarget, actionRequired: boolean): IntegrationResult { + const installed = + target.installedVersion === null + ? 'not registered or not safely observable' + : `left at v${target.installedVersion}`; + const next = actionRequired ? ` ${CODEX_RETIRE_RECOVERY}` : ''; return { runtime: 'codex', ok: true, - detail: `delivered v${VERSION}; Codex plugin left at v${installedVersion} (no cache advance). ${CODEX_RETIRE_RECOVERY}${agentDetail}`, + detail: `authenticated delivery v${VERSION}; Codex plugin ${installed} (no activation-owned mutation).${next}`, deliveryComplete: true, - actionRequired: true, + actionRequired, }; } +/** Resolve the release channel whose manifest and asset must authenticate a deferred install. */ +function resolveDeliveryChannelForInstall(): DeliveryEvidenceChannel { + const handedOffChannel = process.env.GENIE_INSTALL_DELIVERY_CHANNEL; + if (handedOffChannel !== undefined) { + if (handedOffChannel === 'stable' || handedOffChannel === 'homolog' || handedOffChannel === 'dev') { + return handedOffChannel; + } + throw new Error(`Invalid installer delivery channel: ${handedOffChannel}`); + } + try { + if (!genieConfigExists()) return 'stable'; + const raw = JSON.parse(readFileSync(getGenieConfigPath(), 'utf8')) as { updateChannel?: string }; + if (raw.updateChannel === 'dev' || raw.updateChannel === 'next') return 'dev'; + if (raw.updateChannel === 'homolog') return 'homolog'; + return 'stable'; + } catch { + return 'stable'; + } +} + +/** + * Authenticate and deep-publish before any integration, sync, consent, legacy + * cleanup, marker retirement, or Codex-owned mutation. A detected Codex target + * without a held lease is an internal fail-closed error. + */ +async function finalizeInstallDeliveryLifecycle( + lease: HeldLifecycleLease, + target: CodexInstallTarget | null, + deliveryChannel: DeliveryEvidenceChannel, + repairDelivery: InstallDeliveryRepair, +): Promise { + if (target === null) return null; + const delivery = await repairDelivery(deliveryChannel, resolvePlatformId(), lease); + if (delivery.action === 'failed' || delivery.action === 'busy' || delivery.action === 'route-upgrade') + return delivery; + return delivery; +} + +function retireInstallMarkerSafe(retireMarker: InstallMarkerRetirer): void { + try { + retireMarker(); + } catch { + // orphan-metadata cleanup must never fail a completed install. + } +} + +function projectInstallDeliveryOutcome( + delivery: AlreadyCurrentRepairDirective | null, + actionRequired: boolean, +): boolean { + if (delivery?.action === 'failed' || delivery?.action === 'route-upgrade') { + const detail = + delivery.action === 'route-upgrade' + ? `release channel advanced to ${delivery.manifest.version} while authenticating the installed delivery` + : delivery.detail; + console.log(` \x1b[31m!\x1b[0m Codex delivery incomplete: ${detail}`); + console.log(CODEX_DELIVERY_INCOMPLETE_TRAILER); + process.exitCode = 1; + return true; + } + if (delivery?.action === 'busy') { + console.log(` \x1b[31m!\x1b[0m Codex delivery repair is busy: ${delivery.detail}`); + console.log(CODEX_LIFECYCLE_BUSY_TRAILER); + process.exitCode = 2; + return true; + } + if (actionRequired) { + process.exitCode = 2; + console.log(CODEX_DELIVERY_RESULT_TRAILER); + } + return false; +} + /** * Converge the extracted `/bin/{plugins,skills,templates}` trees into * the canonical `/{plugins,skills,templates}` layout that `genie update` @@ -202,40 +285,90 @@ function readVersionStamp(path: string): string | null { } /** - * Build the ordered install results: a pending Codex generation prepends the - * deferral result and converges only the claude-only scope (never advancing the - * cache); a fresh/absent or same-version install converges the full selection. + * Build the ordered install results after delivery authentication. Every + * Codex-in-scope selection excludes Codex from the integration runner; a + * detected runtime gets an informational delivery result, while an explicitly + * requested missing runtime remains a normal integration failure. */ function buildInstallResults( - codexDeferral: CodexInstallDeferral | null, + codexTarget: CodexInstallTarget | null, selection: IntegrationSelection, runIntegrations: IntegrationRunner, + actionRequired: boolean, ): IntegrationResult[] { - if (codexDeferral === null) return runIntegrations({ selection }); - return [buildInstallCodexDeferral(codexDeferral.installedVersion), ...runIntegrations(claudeOnlyScope(selection))]; + if (!codexInScope(selection)) return runIntegrations({ selection }); + const nonCodex = runIntegrations(claudeOnlyScope(selection)); + if (codexTarget !== null) return [buildInstallCodexDeferral(codexTarget, actionRequired), ...nonCodex]; + if (selection === 'codex' || selection === 'all') { + return [{ runtime: 'codex', ok: false, detail: 'codex CLI not found' }, ...nonCodex]; + } + return nonCodex; +} + +function installActionRequired( + target: CodexInstallTarget | null, + delivery: AlreadyCurrentRepairDirective | null, +): boolean { + if (target === null) return false; + if (delivery?.action === 'exit-handoff') return true; + if (target.installedVersion === null) return true; + return classifyCodexDelivery(target.installedVersion, VERSION).kind !== 'current'; } /** - * Acquire the Codex lifecycle lease when Codex is in scope, or project the AC8 - * loser refusal. A claude/none install advances no Codex cache, so it takes no - * lease (`lease: null`). On a busy lease another Codex lifecycle command (update/ - * setup/rollback) holds it: refuse before any plugin convergence — zero Codex - * cache advance — with exit 2 and the codex-lifecycle-busy trailer - * (deliveryComplete:false), and the caller returns immediately. + * Install may remember an explicitly Claude-only maintenance scope. Any + * selection that can include Codex is activation authority and is persisted + * only by a successful explicit `setup --codex`; `none` likewise must not + * revoke an existing setup-owned consent record as an install side effect. */ -function acquireCodexLeaseOrRefuse( +function persistInstallOwnedConsent(selection: IntegrationSelection, writeConsent: ConsentWriter): void { + if (selection === 'claude') writeConsent(selection); +} + +/** Install-owned agent sync cannot cross into setup-owned Codex role convergence. */ +export function runInstallAgentSync( selection: IntegrationSelection, - acquire: CodexLifecycleLeaseAcquirer, -): { refused: true } | { refused: false; lease: HeldLifecycleLease | null } { - if (!codexInScope(selection)) return { refused: false, lease: null }; - const lease = acquire(); - if (!lease.ok) { - console.log(new CodexLifecycleBusyError(lease.holderKind).message); - console.log(CODEX_LIFECYCLE_BUSY_TRAILER); - process.exitCode = 2; - return { refused: true }; + sync: typeof runAgentSyncSafe = runAgentSyncSafe, +): void { + sync({ strict: true, selection }); +} + +/** + * Run only the post-publication integrations this command is authorized to + * own. Codex itself is structurally absent from the runner scope; setup owns + * its marketplace, plugin, project route, and managed-role convergence. + */ +function runPermittedPostDeliveryIntegrations( + selection: IntegrationSelection, + target: CodexInstallTarget | null, + actionRequired: boolean, + runIntegrations: IntegrationRunner, + runSync: AgentSyncRunner, +): void { + const results = buildInstallResults(target, selection, runIntegrations, actionRequired); + for (const result of results) { + const glyph = result.ok ? '\x1b[32m+\x1b[0m' : '\x1b[33m!\x1b[0m'; + const disabled = result.preservedDisabled ? '; disabled state preserved' : ''; + console.log(` ${glyph} ${result.runtime}: ${result.detail}${disabled}`); + } + const codexFailed = results.some((result) => result.runtime === 'codex' && !result.ok); + if (selection !== 'auto' && selection !== 'none') { + const failed = results.filter((result) => !result.ok); + if (failed.length > 0) + throw new Error(`Requested integration failed: ${failed.map((result) => result.runtime).join(', ')}`); + } + const agentSyncSelection = narrowAgentSyncSelection(selection); + if (agentSyncSelection !== null) { + if (!codexFailed) { + runSync(agentSyncSelection); + } else { + // selection === 'auto' is the only surviving case here: an explicit + // --integrations all/claude/codex failure already threw above. + console.log( + ' \x1b[33m!\x1b[0m Skipped agent-sync: codex integration failed under --integrations auto (rerun with --integrations claude to sync Claude/hermes only, or fix codex and rerun).', + ); + } } - return { refused: false, lease }; } /** @@ -243,35 +376,43 @@ function acquireCodexLeaseOrRefuse( * are injection seams for tests (mirrors runV4CleanupSafe) — production callers * pass options only. */ -export function installCommand( +export async function installCommand( options: InstallOptions = {}, runV4Cleanup: V4CleanupRunner = cleanupV4, normalizeLayout: NormalizeAuxLayoutFn = normalizeAuxLayout, - runSync: AgentSyncRunner = (selection) => runAgentSyncSafe({ strict: true, selection }), + runSync: AgentSyncRunner = runInstallAgentSync, runIntegrations: IntegrationRunner = installRuntimeIntegrations, acquireLease: LifecycleLeaseAcquirer = () => acquireLifecycleLease(GENIE_HOME), acquireCodexLease: CodexLifecycleLeaseAcquirer = () => acquireCodexLifecycleLease('install-converge', { genieHome: GENIE_HOME }), writeConsent: ConsentWriter = (selection) => persistIntegrationConsent(selection, GENIE_HOME), classifyCodexInstall: CodexInstallClassifier = classifyCodexInstallDefault, -): void { - const lease = acquireLease(); - if ('skipped' in lease) throw new Error(`Another Genie lifecycle command is active: ${lease.skipped}`); - // The Codex lifecycle lease (.codex-lifecycle.lock) coexists with the agent-sync - // lease above (.agent-sync.lock): they guard different things and are always - // acquired agent-sync-first (identically in `genie update`), so no lock-ordering - // hazard. Acquired below once the selection is known; released in the finally. - let codexLease: HeldLifecycleLease | null = null; - try { - const selection = resolveIntegrationSelection(options); - writeConsent(selection); - if (options.skipV4Cleanup) { - console.log('\x1b[2mSkipping v4 legacy cleanup (--skip-v4-cleanup).\x1b[0m'); - } else { - runV4Cleanup(); + repairDelivery: InstallDeliveryRepair = (channel, platformId, lease) => + attemptAlreadyCurrentDeliveryRepair(channel, platformId, lease), + retireMarker: InstallMarkerRetirer = () => retireInstallVersionMarker(GENIE_HOME), +): Promise { + // install.sh passes the exact channel whose manifest selected the installed + // bytes. Resolve and validate it before acquiring either lifecycle lease or + // running a finisher, so fresh dev/homolog installs cannot be relabeled as + // stable and a malformed internal handoff cannot mutate anything. + const deliveryChannel = resolveDeliveryChannelForInstall(); + const selection = resolveIntegrationSelection(options); + const acquired = acquireOrderedLifecycleLeases(acquireLease, acquireCodexLease); + if (!acquired.ok) { + if (acquired.busy === 'agent-sync') { + throw new Error(`Another Genie lifecycle command is active: ${acquired.detail}`); } - // Converge only selected agent homes: fix the bin/ layout mismatch, then sync in-process - // (the freshly-linked binary is already this version, so no re-exec needed). + console.log(new CodexLifecycleBusyError(acquired.refusal.holderKind).message); + console.log(CODEX_LIFECYCLE_BUSY_TRAILER); + process.exitCode = 2; + return; + } + const { agentSyncLease: lease, codexLease } = acquired; + try { + codexLease.assertOperation(codexLease.operationId); + + // Both lifecycle locks are now held before canonical payload normalization, + // VERSION publication, delivery repair, or any later finisher. const normalized = normalizeLayout(GENIE_HOME); if (normalized !== undefined) { for (const outcome of normalized) printAuxiliaryOutcome(outcome); @@ -280,61 +421,36 @@ export function installCommand( throw new Error(`Install payload convergence failed: ${failed.map((outcome) => outcome.label).join(', ')}`); } } - // Codex is now converged end-to-end (plugin → single health proof → - // fallback retirement → role agents) through runIntegrations; agent-sync - // never writes Genie product skills into ~/.agents/skills (R2) because - // `runAgentSync` has no codex arm at all — structural, not selection-gated. - // Integrations run BEFORE the Claude/hermes agent-sync so a plugin-incapable - // Codex leaves Claude trees byte-identical (R1/A2). - // Group C install gate: a pending Codex generation (installed N ≠ delivered - // T) is DEFERRED — converge role agents only and exclude Codex from the - // plugin convergence, so install never advances the cache. A fresh/absent or - // same-version plugin converges normally. - const codexDeferral = classifyCodexInstall(selection); - const gate = acquireCodexLeaseOrRefuse(selection, acquireCodexLease); - if (gate.refused) return; // exit 2 codex-lifecycle-busy already projected; zero mutation. - codexLease = gate.lease; - const results = buildInstallResults(codexDeferral, selection, runIntegrations); - for (const result of results) { - const glyph = result.ok ? '\x1b[32m+\x1b[0m' : '\x1b[33m!\x1b[0m'; - const disabled = result.preservedDisabled ? '; disabled state preserved' : ''; - console.log(` ${glyph} ${result.runtime}: ${result.detail}${disabled}`); - } - const codexFailed = results.some((result) => result.runtime === 'codex' && !result.ok); - if (selection !== 'auto' && selection !== 'none') { - const failed = results.filter((result) => !result.ok); - if (failed.length > 0) - throw new Error(`Requested integration failed: ${failed.map((r) => r.runtime).join(', ')}`); - } - const agentSyncSelection = narrowAgentSyncSelection(selection); - if (agentSyncSelection !== null) { - if (!codexFailed) { - runSync(agentSyncSelection); - } else { - // selection === 'auto' is the only surviving case here: an explicit - // --integrations all/claude/codex codex failure already threw above, - // so a silent codexFailed-guarded skip here would otherwise exit 0 - // with agent-sync never having run and no trace of why. - console.log( - ' \x1b[33m!\x1b[0m Skipped agent-sync: codex integration failed under --integrations auto (rerun with --integrations claude to sync Claude/hermes only, or fix codex and rerun).', - ); - } - } - if (results.some((result) => result.runtime === 'codex' && result.ok && result.hookReviewRequired)) { - console.log(' \x1b[33m!\x1b[0m Review Genie hooks with /hooks, then start a new Codex task.'); + const codexTarget = classifyCodexInstall(selection); + const delivery = await finalizeInstallDeliveryLifecycle(codexLease, codexTarget, deliveryChannel, repairDelivery); + // Failed, busy, or advanced authentication is terminal before every + // activation-owned or integration-owned finisher. An advanced channel must + // be selected and delivered by the ordinary installer/update path; this + // stale target never mints a record or mutates Codex. + if (projectInstallDeliveryOutcome(delivery, false)) return; + + persistInstallOwnedConsent(selection, writeConsent); + if (options.skipV4Cleanup) { + console.log('\x1b[2mSkipping v4 legacy cleanup (--skip-v4-cleanup).\x1b[0m'); + } else { + runV4Cleanup(); } + + // Install never invokes Codex convergence, even after the authenticated + // record exists. Only unrelated Claude/Hermes integration and sync work is + // permitted here; `setup --codex` owns every Codex activation mutation. + const actionRequired = installActionRequired(codexTarget, delivery); + runPermittedPostDeliveryIntegrations(selection, codexTarget, actionRequired, runIntegrations, runSync); + // Decision 14: marker retirement is the LAST successful finisher. A later + // consent, legacy cleanup, permitted integration, or sync failure must leave + // the marker intact so the whole install remains retryable. + retireInstallMarkerSafe(retireMarker); // Delivered-but-action-required (Codex generation deferred): exit 2 with the // one A-owned result trailer and no all-green footer. install.sh maps this to // an installer exit 2 (deliverable 3). - if (results.some((result) => result.actionRequired === true)) { - process.exitCode = 2; - console.log(CODEX_DELIVERY_RESULT_TRAILER); - } + if (projectInstallDeliveryOutcome(delivery, actionRequired)) return; } finally { - // Release the Codex lifecycle lease (if held) before the agent-sync lease, - // the reverse of the agent-sync-first acquisition order. - codexLease?.release(); - lease.release(); + releaseOrderedLifecycleLeases(codexLease, lease); } } @@ -343,8 +459,8 @@ export function installCommand( * codex product skills into ~/.agents/skills) is now structural in * `runAgentSync` itself — there is no `codex` arm to narrow away from — so * this only needs to skip agent-sync where it has nothing to do: `none` - * (nothing selected) and `codex` (codex converges entirely through - * `installCodexIntegration`, never through agent-sync). Every other selection + * (nothing selected) and `codex` (setup owns Codex convergence, never + * agent-sync). Every other selection * (`auto`/`all`/`claude`) passes through UNCHANGED so `runAgentSync` sees the * real selection and converges hermes on `auto`/`all` too. */ diff --git a/src/genie-commands/local-delivery-repair.test.ts b/src/genie-commands/local-delivery-repair.test.ts new file mode 100644 index 000000000..fc610ba47 --- /dev/null +++ b/src/genie-commands/local-delivery-repair.test.ts @@ -0,0 +1,458 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; +import { + chmodSync, + cpSync, + linkSync, + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + rmSync, + symlinkSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { basename, dirname, join } from 'node:path'; +import { scanPhysicalTree } from '../lib/codex-activation.js'; +import type { DeliveryEvidencePlatformId } from '../lib/codex-delivery-evidence.js'; +import { buildTestDeliveryEvidencePack } from '../lib/codex-delivery-evidence.test-support.js'; +import { acquireLifecycleLease } from '../lib/codex-lifecycle-lease.js'; +import { VERSION } from '../lib/version.js'; +import { + LOCAL_DELIVERY_REPAIR_REQUEST_MAX_BYTES, + assertLocalDeliveryRepairEnabled, + materializeLocalDeliveryRepair, + parseLocalDeliveryRepairRequest, +} from './local-delivery-repair.js'; +import { projectLocalDeliveryRepairDirective, resolvePlatformId, resolveUpdateExecutionMode } from './update.js'; + +const CLI = join(import.meta.dir, '..', 'genie.ts'); +const roots: string[] = []; + +interface LocalFiles { + root: string; + platformId: DeliveryEvidencePlatformId; + request: { + schemaVersion: 1; + platformId: DeliveryEvidencePlatformId; + artifact: string; + manifest: string; + descriptor: string; + bundle: string; + }; +} + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +function tempRoot(label: string): string { + const root = realpathSync(mkdtempSync(join(tmpdir(), `genie-local-delivery-${label}-`))); + roots.push(root); + return root; +} + +function platformId(): DeliveryEvidencePlatformId { + return resolvePlatformId() as DeliveryEvidencePlatformId; +} + +function manifestBytes(platform: DeliveryEvidencePlatformId, version = VERSION): string { + return JSON.stringify({ + schema_version: 1, + channel: 'dev', + version, + released_at: '2026-07-23T00:00:00Z', + tarball_base: `https://github.com/automagik-dev/genie/releases/download/v${version}`, + platforms: [platform], + }); +} + +function basicFiles(label = 'fixture'): LocalFiles { + const root = tempRoot(label); + const platform = platformId(); + const artifact = join(root, `genie-${VERSION}-${platform}.tar.gz`); + const manifest = join(root, 'manifest.json'); + const descriptor = join(root, 'descriptor.json'); + const bundle = join(root, 'bundle.json'); + writeFileSync(artifact, 'artifact'); + writeFileSync(manifest, manifestBytes(platform)); + writeFileSync(descriptor, '{"descriptor":true}'); + writeFileSync(bundle, '{"bundle":true}'); + return { + root, + platformId: platform, + request: { schemaVersion: 1, platformId: platform, artifact, manifest, descriptor, bundle }, + }; +} + +function stageRoot(root: string, suffix = ''): string { + const path = join(root, `stage${suffix}`); + mkdirSync(path, { mode: 0o700 }); + chmodSync(path, 0o700); + return path; +} + +function requestJson(files: LocalFiles): string { + return JSON.stringify(files.request); +} + +function isolatedEnv(root: string, overrides: Record = {}): Record { + const excluded = new Set([ + 'GENIE_BUNDLE_ROOT', + 'GENIE_LIFECYCLE_LEASE_OWNER', + 'GENIE_LIFECYCLE_LEASE_PATH', + 'GENIE_RELEASE_DOGFOOD', + ]); + const env = Object.fromEntries( + Object.entries(process.env).filter( + (entry): entry is [string, string] => entry[1] !== undefined && !excluded.has(entry[0]), + ), + ); + const home = join(root, 'home'); + const genieHome = join(root, 'genie-home'); + const codexHome = join(root, 'codex-home'); + const temp = join(root, 'tmp'); + for (const path of [home, genieHome, codexHome, temp]) mkdirSync(path, { recursive: true }); + return { + ...env, + HOME: home, + GENIE_HOME: genieHome, + CODEX_HOME: codexHome, + TMPDIR: temp, + NO_COLOR: '1', + ...overrides, + }; +} + +function runCli( + root: string, + args: string[], + overrides: Record = {}, +): { exitCode: number; stdout: string; stderr: string } { + const result = Bun.spawnSync(['bun', CLI, ...args], { + env: isolatedEnv(root, overrides), + stdout: 'pipe', + stderr: 'pipe', + }); + return { + exitCode: result.exitCode, + stdout: result.stdout.toString(), + stderr: result.stderr.toString(), + }; +} + +describe('local delivery request and physical-file boundary', () => { + test('requires the release-dogfood capability value exactly', () => { + expect(() => assertLocalDeliveryRepairEnabled(undefined)).toThrow('GENIE_RELEASE_DOGFOOD=1'); + expect(() => assertLocalDeliveryRepairEnabled('true')).toThrow('GENIE_RELEASE_DOGFOOD=1'); + expect(() => assertLocalDeliveryRepairEnabled('1')).not.toThrow(); + }); + + test('accepts only a bounded exact schema', () => { + const files = basicFiles('schema'); + expect(parseLocalDeliveryRepairRequest(requestJson(files))).toEqual(files.request); + expect(() => + parseLocalDeliveryRepairRequest(JSON.stringify({ ...files.request, testOnlySkipSignature: true })), + ).toThrow('exact schema-v1 fields'); + expect(() => parseLocalDeliveryRepairRequest('{}')).toThrow('exact schema-v1 fields'); + expect(() => parseLocalDeliveryRepairRequest('x'.repeat(LOCAL_DELIVERY_REPAIR_REQUEST_MAX_BYTES + 1))).toThrow( + '1-16384 UTF-8 bytes', + ); + }); + + test('snapshots canonical files and preserves exact raw manifest bytes', () => { + const files = basicFiles('snapshot'); + const rawManifest = readFileSync(files.request.manifest, 'utf8'); + const materialized = materializeLocalDeliveryRepair( + requestJson(files), + stageRoot(files.root), + files.platformId, + VERSION, + ); + expect(materialized.manifest.manifestBytes).toBe(rawManifest); + expect(materialized.manifest.manifestSha256).toBe(createHash('sha256').update(rawManifest).digest('hex')); + expect(basename(materialized.artifactPath)).toBe(`genie-${VERSION}-${files.platformId}.tar.gz`); + writeFileSync(files.request.artifact, 'changed outside snapshot'); + expect(readFileSync(materialized.artifactPath, 'utf8')).toBe('artifact'); + }); + + test('rejects relative, non-canonical, symlinked, and physically aliased inputs', () => { + const relative = basicFiles('relative'); + expect(() => + parseLocalDeliveryRepairRequest(JSON.stringify({ ...relative.request, artifact: './candidate.tar.gz' })), + ).toThrow('bounded absolute canonical path'); + + const nonCanonical = basicFiles('noncanonical'); + const spelledWithDot = `${dirname(nonCanonical.request.artifact)}/./${basename(nonCanonical.request.artifact)}`; + expect(() => + materializeLocalDeliveryRepair( + JSON.stringify({ ...nonCanonical.request, artifact: spelledWithDot }), + stageRoot(nonCanonical.root), + nonCanonical.platformId, + VERSION, + ), + ).toThrow('not its absolute canonical physical path'); + + for (const field of ['artifact', 'manifest', 'descriptor', 'bundle'] as const) { + const files = basicFiles(`symlink-${field}`); + const linkRoot = join(files.root, `${field}-link-root`); + mkdirSync(linkRoot); + const link = join(linkRoot, basename(files.request[field])); + symlinkSync(files.request[field], link); + expect(() => + materializeLocalDeliveryRepair( + JSON.stringify({ ...files.request, [field]: link }), + stageRoot(files.root, `-${field}`), + files.platformId, + VERSION, + ), + ).toThrow('not its absolute canonical physical path'); + } + + const aliased = basicFiles('hardlink'); + const alias = join(aliased.root, 'descriptor-alias.json'); + linkSync(aliased.request.artifact, alias); + expect(() => + materializeLocalDeliveryRepair( + JSON.stringify({ ...aliased.request, descriptor: alias }), + stageRoot(aliased.root), + aliased.platformId, + VERSION, + ), + ).toThrow('distinct physical files'); + }); + + test('rejects platform, manifest version, and exact manifest-schema drift', () => { + const files = basicFiles('bindings'); + const otherPlatform = files.platformId === 'darwin-arm64' ? 'linux-arm64' : 'darwin-arm64'; + expect(() => + materializeLocalDeliveryRepair( + JSON.stringify({ ...files.request, platformId: otherPlatform }), + stageRoot(files.root, '-platform'), + files.platformId, + VERSION, + ), + ).toThrow('differs from this runtime'); + + writeFileSync(files.request.manifest, manifestBytes(files.platformId, '5.260722.1')); + expect(() => + materializeLocalDeliveryRepair(requestJson(files), stageRoot(files.root, '-version'), files.platformId, VERSION), + ).toThrow('differs from the running binary VERSION'); + + writeFileSync( + files.request.manifest, + JSON.stringify({ ...JSON.parse(manifestBytes(files.platformId)), artifact_sha256: 'a'.repeat(64) }), + ); + expect(() => + materializeLocalDeliveryRepair(requestJson(files), stageRoot(files.root, '-schema'), files.platformId, VERSION), + ).toThrow('exact schema-v1 fields'); + }); +}); + +describe('local delivery update-mode and trailer boundary', () => { + test('is a distinct mode and conflicts with every normal update control', () => { + expect(resolveUpdateExecutionMode({ publishLocalDelivery: '{}' }, undefined)).toBe('publish-local-delivery'); + for (const conflict of [ + { dev: true }, + { homolog: true }, + { next: true }, + { stable: true }, + { yes: true }, + { restart: false }, + { verify: false }, + { skipMaintenance: true }, + { rollback: true }, + { syncOnly: true }, + { postDeliveryConverge: true }, + { printUpdateCapabilities: true }, + { json: true }, + ]) { + expect(() => resolveUpdateExecutionMode({ publishLocalDelivery: '{}', ...conflict }, undefined)).toThrow( + 'cannot be combined', + ); + } + expect(() => resolveUpdateExecutionMode({ publishLocalDelivery: '{}' }, '1')).toThrow('cannot be combined'); + }); + + test('projects only the standard result, incomplete, and busy trailers', () => { + const failed = projectLocalDeliveryRepairDirective({ action: 'failed', detail: 'tampered' }); + expect(failed.exitCode).toBe(1); + expect(failed.stdout.join('\n')).toContain('"code":"delivery-incomplete"'); + expect(failed.stderr.join('\n')).toContain('tampered'); + + const busy = projectLocalDeliveryRepairDirective({ action: 'busy', detail: 'setup owns lease' }); + expect(busy.exitCode).toBe(2); + expect(busy.stdout.join('\n')).toContain('"code":"codex-lifecycle-busy"'); + + const pending = projectLocalDeliveryRepairDirective({ action: 'exit-handoff' }); + expect(pending.exitCode).toBe(2); + expect(pending.stdout.join('\n')).toContain('"code":"activation-pending"'); + expect(projectLocalDeliveryRepairDirective({ action: 'proceed-current' }).exitCode).toBe(0); + expect(projectLocalDeliveryRepairDirective({ action: 'repaired-current' }).exitCode).toBe(0); + }); + + test('the CLI keeps the option hidden and refuses missing capability or mixed modes', () => { + const root = tempRoot('cli-gate'); + const help = runCli(root, ['update', '--help']); + expect(help.exitCode).toBe(0); + expect(help.stdout).not.toContain('--publish-local-delivery'); + + const missing = runCli(root, ['update', '--publish-local-delivery', '{}']); + expect(missing.exitCode).toBe(1); + expect(missing.stderr).toContain('GENIE_RELEASE_DOGFOOD=1'); + expect(missing.stdout).toContain('"code":"delivery-incomplete"'); + + const conflict = runCli(root, ['update', '--publish-local-delivery', '{}', '--dev'], { + GENIE_RELEASE_DOGFOOD: '1', + }); + expect(conflict.exitCode).not.toBe(0); + expect(conflict.stderr).toContain('cannot be used with option'); + }); + + test('the CLI rejects exact-schema and symlink violations before lifecycle state access', () => { + const files = basicFiles('cli-paths'); + const extra = runCli( + files.root, + ['update', '--publish-local-delivery', JSON.stringify({ ...files.request, unsigned: true })], + { GENIE_RELEASE_DOGFOOD: '1' }, + ); + expect(extra.exitCode).toBe(1); + expect(extra.stderr).toContain('exact schema-v1 fields'); + expect(extra.stdout).toContain('"code":"delivery-incomplete"'); + + const manifestLink = join(files.root, 'manifest-link.json'); + symlinkSync(files.request.manifest, manifestLink); + const linked = runCli( + files.root, + ['update', '--publish-local-delivery', JSON.stringify({ ...files.request, manifest: manifestLink })], + { GENIE_RELEASE_DOGFOOD: '1' }, + ); + expect(linked.exitCode).toBe(1); + expect(linked.stderr).toContain('not its absolute canonical physical path'); + expect(linked.stdout).toContain('"code":"delivery-incomplete"'); + }); + + test('a held Codex lifecycle lease returns the standard busy trailer with exit 2', () => { + const files = basicFiles('cli-busy'); + const genieHome = join(files.root, 'genie-home'); + mkdirSync(genieHome, { recursive: true }); + const lease = acquireLifecycleLease('setup-activation', { genieHome }); + if (!lease.ok) throw new Error(lease.detail); + try { + const result = runCli(files.root, ['update', '--publish-local-delivery', requestJson(files)], { + GENIE_HOME: genieHome, + GENIE_RELEASE_DOGFOOD: '1', + }); + expect(result.exitCode).toBe(2); + expect(result.stderr).toContain('is busy'); + expect(result.stdout).toContain('"code":"codex-lifecycle-busy"'); + } finally { + lease.release(); + } + }); +}); + +describe('local delivery embedded-trust command boundary', () => { + test('rejects descriptor tampering without invoking a network verifier or publishing a record', () => { + const root = tempRoot('cli-tamper'); + const platform = platformId(); + const payload = join(root, 'payload'); + const payloadTree = join(payload, 'plugins', 'genie'); + mkdirSync(payloadTree, { recursive: true }); + writeFileSync(join(payloadTree, 'plugin.txt'), 'candidate payload\n'); + writeFileSync(join(payload, 'VERSION'), `${VERSION}\n`); + const binary = join(payload, 'genie'); + writeFileSync( + binary, + `#!/bin/sh\nif [ "\${1:-}" = "--version" ]; then printf 'genie ${VERSION}\\n'; exit 0; fi\nexit 0\n`, + ); + chmodSync(binary, 0o755); + const tree = scanPhysicalTree(payloadTree); + if (tree.status !== 'ok' || tree.digest === undefined) throw new Error(`fixture tree is ${tree.status}`); + const artifact = join(root, `genie-${VERSION}-${platform}.tar.gz`); + const archived = Bun.spawnSync(['tar', '-czf', artifact, '-C', payload, '.'], { + stdout: 'pipe', + stderr: 'pipe', + }); + if (archived.exitCode !== 0) throw new Error(archived.stderr.toString()); + const binarySha256 = createHash('sha256').update(readFileSync(binary)).digest('hex'); + const artifactSha256 = createHash('sha256').update(readFileSync(artifact)).digest('hex'); + const pack = buildTestDeliveryEvidencePack({ + descriptor: { + version: VERSION, + channel: 'dev', + platformId: platform, + platformTriple: `${process.platform}-${process.arch}`, + releaseTag: `v${VERSION}`, + releaseName: basename(artifact), + artifactSha256, + installedBinarySha256: binarySha256, + canonicalPayloadSha256: tree.digest, + }, + }); + const manifest = join(root, 'manifest.json'); + const descriptor = join(root, 'descriptor.json'); + const bundle = join(root, 'bundle.json'); + writeFileSync(manifest, pack.manifestBytes); + writeFileSync(descriptor, `${pack.descriptorBytes} `); + writeFileSync(bundle, pack.bundleBytes); + + const genieHome = join(root, 'genie-home'); + mkdirSync(join(genieHome, 'bin'), { recursive: true }); + cpSync(payloadTree, join(genieHome, 'plugins', 'genie'), { recursive: true }); + writeFileSync(join(genieHome, 'VERSION'), `${VERSION}\n`); + cpSync(binary, join(genieHome, 'bin', 'genie')); + chmodSync(join(genieHome, 'bin', 'genie'), 0o755); + + const fakeBin = join(root, 'fake-bin'); + mkdirSync(fakeBin); + const networkLog = join(root, 'network.log'); + const codex = join(fakeBin, 'codex'); + writeFileSync( + codex, + '#!/bin/sh\nif [ "$1" = "plugin" ] && [ "$2" = "list" ]; then printf \'{"installed":[]}\\n\'; exit 0; fi\nexit 1\n', + ); + chmodSync(codex, 0o755); + for (const command of ['curl', 'gh', 'cosign']) { + const path = join(fakeBin, command); + writeFileSync(path, `#!/bin/sh\nprintf '${command}\\n' >> '${networkLog}'\nexit 91\n`); + chmodSync(path, 0o755); + } + + const request = JSON.stringify({ + schemaVersion: 1, + platformId: platform, + artifact, + manifest, + descriptor, + bundle, + }); + const result = runCli(root, ['update', '--publish-local-delivery', request], { + GENIE_HOME: genieHome, + GENIE_RELEASE_DOGFOOD: '1', + PATH: `${fakeBin}:${process.env.PATH ?? '/usr/bin:/bin'}`, + }); + expect(result.exitCode).toBe(1); + expect(result.stderr).toContain('DSSE subject does not bind the exact descriptor bytes'); + expect(result.stdout).toContain('"code":"delivery-incomplete"'); + expect(readFileIfPresent(networkLog)).toBe(''); + expect(readFileIfPresent(join(genieHome, '.codex-plugin-delivery-record.json'))).toBe(''); + + const source = readFileSync(join(import.meta.dir, 'update.ts'), 'utf8'); + const start = source.indexOf('async function runLocalDeliveryRepairMode'); + const end = source.indexOf('/** Build the real repair seams', start); + const boundary = source.slice(start, end); + expect(boundary).not.toContain('skipAttestation'); + expect(boundary).not.toContain('evidenceVerification'); + expect(boundary).not.toContain('GENIE_TEST'); + }); +}); + +function readFileIfPresent(path: string): string { + try { + return readFileSync(path, 'utf8'); + } catch { + return ''; + } +} diff --git a/src/genie-commands/local-delivery-repair.ts b/src/genie-commands/local-delivery-repair.ts new file mode 100644 index 000000000..47e277e10 --- /dev/null +++ b/src/genie-commands/local-delivery-repair.ts @@ -0,0 +1,351 @@ +/** + * Release-dogfood-only local delivery input boundary. + * + * The hidden update mode accepts one bounded exact-schema JSON value, then + * snapshots every caller-owned file into a private directory before any + * evidence verification or archive extraction. Production verification still + * happens in `repairMissingDelivery`; this module only turns unstable external + * paths into stable, bounded, physical inputs. + */ + +import { createHash } from 'node:crypto'; +import { + constants, + type BigIntStats, + closeSync, + fstatSync, + fsyncSync, + lstatSync, + openSync, + readFileSync, + readSync, + realpathSync, + writeSync, +} from 'node:fs'; +import { basename, isAbsolute, join } from 'node:path'; +import type { DeliveryEvidenceChannel, DeliveryEvidencePlatformId } from '../lib/codex-delivery-evidence.js'; +import { parseReleaseVersion } from '../lib/codex-release-version.js'; +import type { PinnedManifest } from './codex-delivery-repair.js'; + +export const LOCAL_DELIVERY_REPAIR_ENABLE_ENV = 'GENIE_RELEASE_DOGFOOD'; +export const LOCAL_DELIVERY_REPAIR_REQUEST_MAX_BYTES = 16 * 1024; + +const MAX_ARTIFACT_BYTES = 512 * 1024 * 1024; +const MAX_MANIFEST_BYTES = 64 * 1024; +const MAX_DESCRIPTOR_BYTES = 32 * 1024; +const MAX_BUNDLE_BYTES = 2 * 1024 * 1024; +const MAX_PATH_BYTES = 4096; +const COPY_CHUNK_BYTES = 1024 * 1024; +const O_NOFOLLOW = (constants.O_NOFOLLOW ?? 0) as number; +const CHANNELS = new Set(['stable', 'homolog', 'dev']); +const PLATFORM_IDS = new Set([ + 'linux-x64-glibc', + 'linux-x64-musl', + 'linux-arm64', + 'darwin-arm64', +]); +const REQUEST_KEYS = ['artifact', 'bundle', 'descriptor', 'manifest', 'platformId', 'schemaVersion'] as const; +const MANIFEST_KEYS = ['channel', 'platforms', 'released_at', 'schema_version', 'tarball_base', 'version'] as const; + +export interface LocalDeliveryRepairRequest { + schemaVersion: 1; + platformId: DeliveryEvidencePlatformId; + artifact: string; + manifest: string; + descriptor: string; + bundle: string; +} + +export interface MaterializedLocalDeliveryRepair { + platformId: DeliveryEvidencePlatformId; + manifest: PinnedManifest; + artifactPath: string; + descriptorBytes: Buffer; + bundleBytes: Buffer; +} + +interface SnapshottedFile { + path: string; + identity: string; +} + +export function assertLocalDeliveryRepairEnabled(value: string | undefined): void { + if (value !== '1') { + throw new Error(`${LOCAL_DELIVERY_REPAIR_ENABLE_ENV}=1 is required for local delivery publication`); + } +} + +/** Parse the command argument without accepting aliases, defaults, or extension fields. */ +export function parseLocalDeliveryRepairRequest(raw: string): LocalDeliveryRepairRequest { + const byteLength = Buffer.byteLength(raw, 'utf8'); + if (byteLength === 0 || byteLength > LOCAL_DELIVERY_REPAIR_REQUEST_MAX_BYTES) { + throw new Error(`local delivery request must be 1-${LOCAL_DELIVERY_REPAIR_REQUEST_MAX_BYTES} UTF-8 bytes`); + } + let parsed: unknown; + try { + parsed = JSON.parse(raw); + } catch { + throw new Error('local delivery request is malformed JSON'); + } + if (!isPlainObject(parsed) || !hasExactKeys(parsed, REQUEST_KEYS)) { + throw new Error('local delivery request does not have the exact schema-v1 fields'); + } + if (parsed.schemaVersion !== 1) throw new Error('local delivery request schemaVersion is invalid'); + if (typeof parsed.platformId !== 'string' || !PLATFORM_IDS.has(parsed.platformId as DeliveryEvidencePlatformId)) { + throw new Error('local delivery request platformId is invalid'); + } + for (const field of ['artifact', 'manifest', 'descriptor', 'bundle'] as const) { + assertCanonicalPathSyntax(parsed[field], field); + } + const paths = [parsed.artifact, parsed.manifest, parsed.descriptor, parsed.bundle] as string[]; + if (new Set(paths).size !== paths.length) { + throw new Error('local delivery request paths must be distinct'); + } + return parsed as unknown as LocalDeliveryRepairRequest; +} + +/** + * Snapshot all external inputs into `privateRoot`. The root is created by + * update's mode-0700 staging primitive; no caller path is used after return. + */ +export function materializeLocalDeliveryRepair( + raw: string, + privateRoot: string, + expectedPlatformId: string, + runningVersion: string, +): MaterializedLocalDeliveryRepair { + const request = parseLocalDeliveryRepairRequest(raw); + if (request.platformId !== expectedPlatformId) { + throw new Error(`local delivery platform ${request.platformId} differs from this runtime (${expectedPlatformId})`); + } + + const manifestSnapshot = snapshotPhysicalFile( + request.manifest, + join(privateRoot, '.local-release-manifest.json'), + MAX_MANIFEST_BYTES, + 'manifest', + ); + const manifestBytes = readSnapshotBytes(manifestSnapshot.path, MAX_MANIFEST_BYTES, 'manifest'); + const manifest = parsePinnedManifest(manifestBytes, request.platformId); + const expectedVersion = parseReleaseVersion(runningVersion)?.canonical; + if (expectedVersion === undefined || manifest.version !== expectedVersion) { + throw new Error( + `local delivery manifest version ${manifest.version} differs from the running binary VERSION ${runningVersion}`, + ); + } + + const releaseName = `genie-${manifest.version}-${request.platformId}.tar.gz`; + if (basename(request.artifact) !== releaseName) { + throw new Error(`local delivery artifact name must be ${releaseName}`); + } + const artifact = snapshotPhysicalFile( + request.artifact, + join(privateRoot, releaseName), + MAX_ARTIFACT_BYTES, + 'artifact', + ); + const descriptor = snapshotPhysicalFile( + request.descriptor, + join(privateRoot, '.local-delivery-descriptor.json'), + MAX_DESCRIPTOR_BYTES, + 'descriptor', + ); + const bundle = snapshotPhysicalFile( + request.bundle, + join(privateRoot, '.local-delivery-bundle.json'), + MAX_BUNDLE_BYTES, + 'bundle', + ); + const identities = [artifact, manifestSnapshot, descriptor, bundle].map((file) => file.identity); + if (new Set(identities).size !== identities.length) { + throw new Error('local delivery request files must be distinct physical files'); + } + return { + platformId: request.platformId, + manifest, + artifactPath: artifact.path, + descriptorBytes: readSnapshotBytes(descriptor.path, MAX_DESCRIPTOR_BYTES, 'descriptor'), + bundleBytes: readSnapshotBytes(bundle.path, MAX_BUNDLE_BYTES, 'bundle'), + }; +} + +function parsePinnedManifest(bytes: Buffer, platformId: DeliveryEvidencePlatformId): PinnedManifest { + if (!Buffer.from(bytes.toString('utf8'), 'utf8').equals(bytes)) { + throw new Error('local delivery manifest is not valid UTF-8'); + } + let parsed: unknown; + try { + parsed = JSON.parse(bytes.toString('utf8')); + } catch { + throw new Error('local delivery manifest is malformed JSON'); + } + if (!isPlainObject(parsed) || !hasExactKeys(parsed, MANIFEST_KEYS)) { + throw new Error('local delivery manifest does not have the exact schema-v1 fields'); + } + if (parsed.schema_version !== 1) throw new Error('local delivery manifest schema_version is invalid'); + if (typeof parsed.channel !== 'string' || !CHANNELS.has(parsed.channel as DeliveryEvidenceChannel)) { + throw new Error('local delivery manifest channel is invalid'); + } + if (typeof parsed.version !== 'string' || parseReleaseVersion(parsed.version) === null) { + throw new Error('local delivery manifest version is invalid'); + } + if (typeof parsed.released_at !== 'string' || !Number.isFinite(Date.parse(parsed.released_at))) { + throw new Error('local delivery manifest released_at is invalid'); + } + const releaseTag = `v${parsed.version}`; + if (parsed.tarball_base !== `https://github.com/automagik-dev/genie/releases/download/${releaseTag}`) { + throw new Error('local delivery manifest tarball_base is invalid'); + } + if ( + !Array.isArray(parsed.platforms) || + parsed.platforms.length === 0 || + !parsed.platforms.every( + (platform) => typeof platform === 'string' && PLATFORM_IDS.has(platform as DeliveryEvidencePlatformId), + ) || + new Set(parsed.platforms).size !== parsed.platforms.length || + !parsed.platforms.includes(platformId) + ) { + throw new Error('local delivery manifest platforms are invalid'); + } + const manifestBytes = bytes.toString('utf8'); + return { + schema_version: 1, + channel: parsed.channel as DeliveryEvidenceChannel, + version: parsed.version, + released_at: parsed.released_at, + tarball_base: parsed.tarball_base as string, + platforms: [...parsed.platforms] as string[], + manifestBytes, + manifestSha256: createHash('sha256').update(bytes).digest('hex'), + }; +} + +function snapshotPhysicalFile(source: string, destination: string, maxBytes: number, label: string): SnapshottedFile { + let canonical: string; + try { + canonical = realpathSync(source); + } catch (cause) { + throw new Error(`local delivery ${label} path is unavailable: ${errorText(cause)}`); + } + if (canonical !== source) { + throw new Error(`local delivery ${label} path is not its absolute canonical physical path`); + } + let visible: BigIntStats; + try { + visible = lstatSync(source, { bigint: true }); + } catch (cause) { + throw new Error(`local delivery ${label} path is unreadable: ${errorText(cause)}`); + } + assertSourceStat(visible, maxBytes, label); + + let sourceFd: number | null = null; + let destinationFd: number | null = null; + try { + sourceFd = openSync(source, constants.O_RDONLY | O_NOFOLLOW); + const held = fstatSync(sourceFd, { bigint: true }); + assertSourceStat(held, maxBytes, label); + if (!sameFileSnapshot(visible, held)) { + throw new Error(`local delivery ${label} changed before snapshot`); + } + destinationFd = openSync( + destination, + constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | O_NOFOLLOW, + 0o600, + ); + copyBounded(sourceFd, destinationFd, Number(held.size), maxBytes, label); + fsyncSync(destinationFd); + const after = fstatSync(sourceFd, { bigint: true }); + const afterVisible = lstatSync(source, { bigint: true }); + if (!sameFileSnapshot(held, after) || !sameFileSnapshot(after, afterVisible) || realpathSync(source) !== source) { + throw new Error(`local delivery ${label} changed during snapshot`); + } + return { path: destination, identity: `${held.dev}:${held.ino}` }; + } catch (cause) { + if (cause instanceof Error && cause.message.startsWith('local delivery ')) throw cause; + throw new Error(`local delivery ${label} could not be snapshotted: ${errorText(cause)}`); + } finally { + if (destinationFd !== null) closeSync(destinationFd); + if (sourceFd !== null) closeSync(sourceFd); + } +} + +function readSnapshotBytes(path: string, maxBytes: number, label: string): Buffer { + const stat = lstatSync(path, { bigint: true }); + if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1n || stat.size > BigInt(maxBytes)) { + throw new Error(`private local delivery ${label} snapshot has an unsafe shape`); + } + const bytes = readFileSync(path); + if (bytes.length !== Number(stat.size)) throw new Error(`private local delivery ${label} snapshot changed`); + return bytes; +} + +function copyBounded( + sourceFd: number, + destinationFd: number, + expectedBytes: number, + maxBytes: number, + label: string, +): void { + const buffer = Buffer.allocUnsafe(Math.min(COPY_CHUNK_BYTES, Math.max(expectedBytes, 1))); + let total = 0; + while (total < expectedBytes) { + const count = readSync(sourceFd, buffer, 0, Math.min(buffer.length, expectedBytes - total), null); + if (count === 0) break; + total += count; + if (total > maxBytes) throw new Error(`local delivery ${label} exceeds the byte limit`); + let written = 0; + while (written < count) { + written += writeSync(destinationFd, buffer, written, count - written, null); + } + } + if (total !== expectedBytes) throw new Error(`local delivery ${label} changed during snapshot`); + const trailing = readSync(sourceFd, buffer, 0, 1, null); + if (trailing !== 0) throw new Error(`local delivery ${label} changed during snapshot`); +} + +function assertSourceStat(stat: BigIntStats, maxBytes: number, label: string): void { + if (!stat.isFile() || stat.isSymbolicLink()) { + throw new Error(`local delivery ${label} is not a physical regular file`); + } + if (stat.size > BigInt(maxBytes)) throw new Error(`local delivery ${label} exceeds the byte limit`); +} + +function sameFileSnapshot(left: BigIntStats, right: BigIntStats): boolean { + return ( + left.dev === right.dev && + left.ino === right.ino && + left.mode === right.mode && + left.uid === right.uid && + left.gid === right.gid && + left.nlink === right.nlink && + left.size === right.size && + left.mtimeNs === right.mtimeNs && + left.ctimeNs === right.ctimeNs + ); +} + +function assertCanonicalPathSyntax(value: unknown, field: string): asserts value is string { + if ( + typeof value !== 'string' || + value.length === 0 || + value.includes('\0') || + Buffer.byteLength(value, 'utf8') > MAX_PATH_BYTES || + !isAbsolute(value) + ) { + throw new Error(`local delivery request ${field} must be a bounded absolute canonical path`); + } +} + +function hasExactKeys(value: Record, expected: T): boolean { + const keys = Object.keys(value).sort(); + const sortedExpected = [...expected].sort(); + return keys.length === sortedExpected.length && keys.every((key, index) => key === sortedExpected[index]); +} + +function isPlainObject(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value); +} + +function errorText(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} diff --git a/src/genie-commands/setup.test.ts b/src/genie-commands/setup.test.ts index 6fc32ba76..6bc20c0be 100644 --- a/src/genie-commands/setup.test.ts +++ b/src/genie-commands/setup.test.ts @@ -13,8 +13,20 @@ import type { QueryFact, } from '../lib/codex-activation.js'; import { parseReleaseVersion } from '../lib/codex-activation.js'; +import { + DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + DELIVERY_EVIDENCE_REPOSITORY, + type VerifiedDeliveryEvidenceFacts, + deriveDeliveryId, +} from '../lib/codex-delivery-evidence.js'; +import { acquireLifecycleLease as acquireCodexLifecycleLease } from '../lib/codex-lifecycle-lease.js'; import { loadGenieConfig, saveGenieConfig } from '../lib/genie-config.js'; -import { persistIntegrationConsent, readIntegrationConsent } from '../lib/runtime-integrations.js'; +import { + createSetupCodexConsentCommitConsumer, + persistIntegrationConsent, + readIntegrationConsent, + runDeliveryRootConsumer, +} from '../lib/runtime-integrations.js'; import { type SetupDeps, mergeCodexIntegrationConsent, resolveDefaultAgentAfterCodex, setupCommand } from './setup.js'; // resolveDefaultAgentAfterCodex is the single decision point `genie setup @@ -46,7 +58,7 @@ describe('mergeCodexIntegrationConsent', () => { expect(mergeCodexIntegrationConsent('claude')).toBe('all'); expect(mergeCodexIntegrationConsent('codex')).toBe('codex'); expect(mergeCodexIntegrationConsent('all')).toBe('all'); - expect(mergeCodexIntegrationConsent('auto')).toBe('auto'); + expect(mergeCodexIntegrationConsent('auto')).toBe('codex'); }); }); @@ -58,6 +70,51 @@ const T = '5.260712.1'; const OLD = '5.260711.9'; const NEWER = '5.260713.4'; const DIGEST = 'a'.repeat(64); +const BINARY_DIGEST = 'b'.repeat(64); +const MANIFEST_DIGEST = 'c'.repeat(64); +const ARTIFACT_DIGEST = 'd'.repeat(64); +const PLATFORM = 'darwin-arm64'; +const DELIVERY_ROOT = '/fixture/genie/deliveries/current'; +const EVIDENCE_DIGEST = 'e'.repeat(64); +const MARKETPLACE_CONSUMER = Object.freeze({ kind: 'marketplace' }) as never; +const RETIREMENT_CONSUMER = Object.freeze({ kind: 'fallback-retirement' }) as never; +const ROLE_CONSUMER = Object.freeze({ kind: 'roles' }) as never; +const RETIREMENT_RESULT = { + status: 'verified' as const, + retired: [] as string[], + preservedCollisions: 0, + preservedUnrecognized: 0, +}; +const DISABLED_RETIREMENT_RESULT = { + status: 'skipped-disabled' as const, + retired: [] as const, + preservedCollisions: 0 as const, + preservedUnrecognized: 0 as const, +}; +const ROLE_RESULT = { + installed: 7, + skippedUserOwned: [], + keptModified: [], + removed: [], + backedUp: [], +}; + +function setupDeliveryOps(consume?: (consumer: unknown) => unknown) { + return { + inventoryDigest: () => DIGEST, + deliveredVersion: () => T, + consume: (consumer: unknown) => + consume !== undefined + ? consume(consumer) + : consumer === ROLE_CONSUMER + ? ROLE_RESULT + : consumer === MARKETPLACE_CONSUMER + ? undefined + : consumer === RETIREMENT_CONSUMER + ? RETIREMENT_RESULT + : runDeliveryRootConsumer(consumer as never, DELIVERY_ROOT), + }; +} function ver(s: string) { const parsed = parseReleaseVersion(s); @@ -67,8 +124,16 @@ function ver(s: string) { function family(): FamilyWitness { return { status: 'present', digest: 'f'.repeat(64), identity: '10:300' }; } -function okCanonical(): CanonicalFact { - return { status: 'ok', version: ver(T), digest: DIGEST, identity: '10:100' }; +function okCanonical(): Extract { + return { + status: 'ok', + version: ver(T), + digest: DIGEST, + identity: '10:100', + platformTriple: PLATFORM, + installedBinarySha256: BINARY_DIGEST, + deliveryRoot: DELIVERY_ROOT, + }; } function reg(version: string): QueryFact { return { status: 'ok', registration: { present: true, enabled: true, version: ver(version) } }; @@ -76,13 +141,63 @@ function reg(version: string): QueryFact { function cache(digest = DIGEST): PhysicalCacheFact { return { kind: 'present', digest, identity: '10:200' }; } +/** A matching authenticated delivery record binding the canonical target (Group E delivery gate). */ +function deliveryPresent(): Extract { + return { + status: 'present', + record: { + schemaVersion: 2, + deliveryId: deriveDeliveryId(EVIDENCE_DIGEST, DELIVERY_ROOT), + targetVersion: T, + canonicalPayloadSha256: DIGEST, + channel: 'stable', + deliveredAt: '2026-07-12T00:00:00.000Z', + evidenceDigest: EVIDENCE_DIGEST, + platformId: 'darwin-arm64', + platformTriple: PLATFORM, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM}.tar.gz`, + releaseManifestSha256: MANIFEST_DIGEST, + artifactSha256: ARTIFACT_DIGEST, + installedBinarySha256: BINARY_DIGEST, + deliveryRoot: DELIVERY_ROOT, + }, + evidence: deliveryEvidenceFacts(), + }; +} + +function deliveryEvidenceFacts(): VerifiedDeliveryEvidenceFacts { + return { + evidenceDigest: EVIDENCE_DIGEST, + deliveredAt: '2026-07-12T00:00:00.000Z', + descriptor: { + schemaVersion: 1 as const, + repository: DELIVERY_EVIDENCE_REPOSITORY, + version: T, + channel: 'stable' as const, + platformId: 'darwin-arm64' as const, + platformTriple: PLATFORM, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM}.tar.gz`, + releaseManifestSha256: MANIFEST_DIGEST, + artifactSha256: ARTIFACT_DIGEST, + installedBinarySha256: BINARY_DIGEST, + canonicalPayloadSha256: DIGEST, + sourceSha: '1'.repeat(40), + sourceBranch: 'main', + sourceCiRunId: '123', + controlSha: '2'.repeat(40), + digestAlgorithm: DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + }, + }; +} function snapshot(over: Partial = {}): CodexActivationSnapshot { return { canonical: okCanonical(), query: reg(T), cache: cache(), receipt: { status: 'absent' }, - delivery: { status: 'absent' }, + delivery: deliveryPresent(), intent: { status: 'absent' }, receiptConsumed: false, observationWitness: { before: family(), after: family() }, @@ -98,6 +213,36 @@ function pendingSnapshot(): CodexActivationSnapshot { function currentSnapshot(): CodexActivationSnapshot { return snapshot(); } +/** Update has atomically published T+1 while the registered plugin still points at T. */ +function publishedNextSnapshot(): CodexActivationSnapshot { + const baseEvidence = deliveryEvidenceFacts(); + const nextEvidence: VerifiedDeliveryEvidenceFacts = { + ...baseEvidence, + descriptor: { + ...baseEvidence.descriptor, + version: NEWER, + releaseTag: `v${NEWER}`, + releaseName: `genie-${NEWER}-${PLATFORM}.tar.gz`, + }, + }; + return snapshot({ + canonical: { + ...okCanonical(), + version: ver(NEWER), + }, + query: reg(T), + delivery: { + status: 'present', + record: { + ...deliveryPresent().record, + targetVersion: NEWER, + releaseTag: `v${NEWER}`, + releaseName: `genie-${NEWER}-${PLATFORM}.tar.gz`, + }, + evidence: nextEvidence, + }, + }); +} /** Registered N>T without a downgrade receipt → installed-newer (authority none, exit 1). */ function installedNewerSnapshot(): CodexActivationSnapshot { return snapshot({ query: reg(NEWER) }); @@ -198,7 +343,23 @@ describe('setup Codex activation (Group D)', () => { promptRetirementConfirmation: () => true, // Don't touch the real agent-sync lease or the deep store in unit tests. acquireLifecycleLease: () => ({ path: join(root, 'genie-home', '.lock'), release: () => {} }), - openCodexActivationStore: () => ({}) as never, + acquireActivationLease: (() => ({ + ok: true, + kind: 'setup-activation', + operationId: 'op-setup-test', + assertOperation: () => {}, + release: () => {}, + })) as never, + openCodexActivationStore: (() => ({ + observe: () => currentSnapshot(), + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => callback(setupDeliveryOps()), + })) as never, + createCodexMarketplaceConsumer: () => MARKETPLACE_CONSUMER, + createCodexFallbackRetirementConsumer: () => RETIREMENT_CONSUMER, + createCodexRoleAgentConsumer: () => ROLE_CONSUMER, ...over, }; } @@ -228,6 +389,374 @@ describe('setup Codex activation (Group D)', () => { expect(readIntegrationConsent(genieHome)).toBe('all'); }); + test('fresh activation revalidates current before committing consent and converging roles', async () => { + const events: string[] = []; + const genieHome = process.env.GENIE_HOME as string; + const cap = capture(); + await setupCommand( + { codex: true }, + grantedDeps(ACTIVATED, { + executeCodexActivation: () => { + events.push('executor'); + return ACTIVATED; + }, + openCodexActivationStore: (() => ({ + observe: () => { + events.push('reobserve-current'); + return currentSnapshot(); + }, + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => { + events.push('delivery-callback-enter'); + const result = callback( + setupDeliveryOps((consumer) => { + if (consumer === ROLE_CONSUMER) { + events.push(`roles:${readIntegrationConsent(genieHome)}`); + return ROLE_RESULT; + } + if (consumer === RETIREMENT_CONSUMER) { + events.push('retire-fallbacks'); + return RETIREMENT_RESULT; + } + return runDeliveryRootConsumer(consumer as never, DELIVERY_ROOT); + }), + ); + events.push('delivery-callback-exit'); + return result; + }, + })) as never, + createCodexConsentCommitConsumer: (options) => + createSetupCodexConsentCommitConsumer({ + ...options, + persist: (selection, home) => { + events.push(`consent:${selection}`); + persistIntegrationConsent(selection, home); + }, + }), + }), + ); + const { exitCode } = cap.restore(); + + expect(exitCode).toBe(0); + expect(events).toEqual([ + 'executor', + 'delivery-callback-enter', + 'reobserve-current', + 'consent:codex', + 'retire-fallbacks', + 'roles:codex', + 'delivery-callback-exit', + ]); + }); + + test('fallback health/retirement failure stops before role convergence, route, and config success', async () => { + let roleCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + grantedDeps(ACTIVATED, { + openCodexActivationStore: (() => ({ + observe: () => currentSnapshot(), + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => + callback( + setupDeliveryOps((consumer) => { + if (consumer === RETIREMENT_CONSUMER) { + throw new Error('Codex plugin health rejected before retirement: fixture failure'); + } + if (consumer === ROLE_CONSUMER) { + roleCalls += 1; + return ROLE_RESULT; + } + return runDeliveryRootConsumer(consumer as never, DELIVERY_ROOT); + }), + ), + })) as never, + }), + ); + const { out, err, exitCode } = cap.restore(); + + expect(exitCode).toBe(1); + expect(err).toContain('health rejected before retirement'); + expect(roleCalls).toBe(0); + expect(out).not.toContain('Project MCP route'); + expect(out).not.toContain('Codex configuration saved'); + }); + + test('post-activation delivery advance refuses consent, roles, route, and config success', async () => { + let consentCalls = 0; + let retirementCalls = 0; + let roleCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + grantedDeps(ACTIVATED, { + openCodexActivationStore: (() => ({ + observe: () => pendingSnapshot(), + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => callback(setupDeliveryOps()), + })) as never, + createCodexConsentCommitConsumer: (options) => + createSetupCodexConsentCommitConsumer({ + ...options, + persist: () => { + consentCalls += 1; + }, + }), + createCodexRoleAgentConsumer: () => { + roleCalls += 1; + return ROLE_CONSUMER; + }, + createCodexFallbackRetirementConsumer: () => { + retirementCalls += 1; + return RETIREMENT_CONSUMER; + }, + }), + ); + const { out, exitCode } = cap.restore(); + + expect(exitCode).toBe(1); + expect(consentCalls).toBe(0); + expect(retirementCalls).toBe(0); + expect(roleCalls).toBe(0); + expect(out).not.toContain('Project MCP route'); + expect(out).not.toContain('Codex configuration saved'); + }); + + test('publication immediately before final locks is revalidated and refuses with zero false success', async () => { + let finalSnapshot = currentSnapshot(); + let roleCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + codexFinalizationHooks: { + beforeLocks: () => { + // Simulate update publishing a newer generation after setup's + // initial observation but immediately before the final lock pair. + finalSnapshot = publishedNextSnapshot(); + }, + }, + openCodexActivationStore: (() => ({ + observe: () => finalSnapshot, + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => callback(setupDeliveryOps()), + })) as never, + createCodexRoleAgentConsumer: () => { + roleCalls += 1; + return ROLE_CONSUMER; + }, + }), + ); + const { out, err, exitCode } = cap.restore(); + + expect(exitCode).toBe(1); + expect(err).toContain('current state changed before managed-asset convergence'); + expect(roleCalls).toBe(0); + expect(out).not.toContain('Project MCP route'); + expect(out).not.toContain('Codex configuration saved'); + expect((await loadGenieConfig()).codex?.configured).not.toBe(true); + }); + + for (const race of [ + { name: 'after assets and before route', hook: 'afterAssets' }, + { name: 'after route and before config persistence', hook: 'afterRoute' }, + ] as const) { + test(`an update attempt ${race.name} is lifecycle-busy while setup persists one truthful winner`, async () => { + const genieHome = process.env.GENIE_HOME as string; + const updateAttempts: Array> = []; + const hook = () => { + updateAttempts.push(acquireCodexLifecycleLease('update-delivery', { genieHome })); + }; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + acquireActivationLease: acquireCodexLifecycleLease, + codexFinalizationHooks: { [race.hook]: hook }, + }), + ); + const { out, exitCode } = cap.restore(); + + expect(exitCode).toBe(0); + expect(updateAttempts).toHaveLength(1); + expect(updateAttempts[0]?.ok).toBe(false); + if (updateAttempts[0]?.ok === false) expect(updateAttempts[0].holderKind).toBe('setup-activation'); + expect(out).toContain('Codex configuration saved'); + expect((await loadGenieConfig()).codex?.configured).toBe(true); + }); + } + + test('finalization acquires agent-sync then Codex and releases Codex then agent-sync after config persistence', async () => { + const events: string[] = []; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + acquireLifecycleLease: () => { + events.push('acquire-agent-sync'); + return { + path: join(root, 'genie-home', '.agent-sync-test-lock'), + release: () => events.push('release-agent-sync'), + }; + }, + acquireActivationLease: (() => { + events.push('acquire-codex'); + return { + ok: true, + kind: 'setup-activation', + operationId: 'op-lock-order-test', + assertOperation: () => {}, + release: () => events.push('release-codex'), + }; + }) as never, + codexFinalizationHooks: { + afterRoute: () => events.push('route-complete'), + }, + }), + ); + cap.restore(); + + expect((await loadGenieConfig()).codex?.configured).toBe(true); + expect(events).toEqual([ + 'acquire-agent-sync', + 'acquire-codex', + 'route-complete', + 'release-codex', + 'release-agent-sync', + ]); + }); + + test('finalization releases agent-sync even when Codex lease cleanup throws', async () => { + const events: string[] = []; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + acquireLifecycleLease: () => ({ + path: join(root, 'genie-home', '.agent-sync-test-lock'), + release: () => events.push('release-agent-sync'), + }), + acquireActivationLease: (() => ({ + ok: true, + kind: 'setup-activation', + operationId: 'op-release-failure-test', + assertOperation: () => {}, + release: () => { + events.push('release-codex'); + throw new Error('fixture Codex release failure'); + }, + })) as never, + }), + ); + const { err, exitCode } = cap.restore(); + + expect(exitCode).toBe(1); + expect(err).toContain('fixture Codex release failure'); + expect(events).toEqual(['release-codex', 'release-agent-sync']); + }); + + test('agent-sync finalization contention projects one typed busy trailer before every mutation', async () => { + let codexAcquireCalls = 0; + let roleCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + acquireLifecycleLease: () => ({ skipped: 'fixture agent-sync busy' }), + acquireActivationLease: (() => { + codexAcquireCalls += 1; + return { + ok: true, + kind: 'setup-activation', + operationId: 'must-not-acquire', + assertOperation: () => {}, + release: () => {}, + }; + }) as never, + createCodexRoleAgentConsumer: () => { + roleCalls += 1; + return ROLE_CONSUMER; + }, + }), + ); + const { out, err, trailer, exitCode } = cap.restore(); + + expect(exitCode).toBe(2); + expect(trailer.trim().split('\n')).toHaveLength(1); + expect(JSON.parse(trailer.trim())).toEqual({ + schemaVersion: 1, + code: 'codex-lifecycle-busy', + deliveryComplete: false, + retry: true, + nextAction: 'retry after the current agent-sync lifecycle command releases the lease', + }); + expect(err).toContain('codex-lifecycle-busy'); + expect(err).toContain('agent-sync'); + expect(err).toContain('fixture agent-sync busy'); + expect(codexAcquireCalls).toBe(0); + expect(roleCalls).toBe(0); + expect(out).not.toContain('Codex configuration saved'); + expect((await loadGenieConfig()).codex?.configured).not.toBe(true); + }); + + test('Codex finalization contention projects one typed busy trailer, releases agent-sync, and refuses assets', async () => { + const events: string[] = []; + let roleCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + acquireLifecycleLease: () => ({ + path: join(root, 'genie-home', '.agent-sync-test-lock'), + release: () => events.push('release-agent-sync'), + }), + acquireActivationLease: (() => ({ + ok: false, + code: 'codex-lifecycle-busy', + holderKind: 'update-delivery', + detail: 'fixture Codex busy', + })) as never, + createCodexRoleAgentConsumer: () => { + roleCalls += 1; + return ROLE_CONSUMER; + }, + }), + ); + const { out, err, trailer, exitCode } = cap.restore(); + + expect(exitCode).toBe(2); + expect(trailer.trim().split('\n')).toHaveLength(1); + expect(JSON.parse(trailer.trim())).toEqual({ + schemaVersion: 1, + code: 'codex-lifecycle-busy', + deliveryComplete: false, + retry: true, + nextAction: 'retry after the current update-delivery lifecycle command releases the lease', + }); + expect(err).toContain('codex-lifecycle-busy'); + expect(err).toContain('update-delivery'); + expect(err).toContain('fixture Codex busy'); + expect(events).toEqual(['release-agent-sync']); + expect(roleCalls).toBe(0); + expect(out).not.toContain('Codex configuration saved'); + expect((await loadGenieConfig()).codex?.configured).not.toBe(true); + }); + test("a granted activation flips a never-chosen 'auto' default agent to codex", async () => { await setupCommand({ codex: true }, grantedDeps(ACTIVATED)); expect((await loadGenieConfig()).runtime.defaultAgent).toBe('codex'); @@ -373,7 +902,7 @@ describe('setup Codex activation (Group D)', () => { expect(err).toContain('genie update'); }); - test('an already-current plugin needs no consent and exits 0', async () => { + test('an already-current plugin needs no retirement assertion and exits 0', async () => { let assertionRequested = 0; const cap = capture(); await setupCommand( @@ -394,6 +923,120 @@ describe('setup Codex activation (Group D)', () => { expect((await loadGenieConfig()).codex?.configured).toBe(true); }); + test('already-current setup revalidates before marketplace, then commits consent before roles', async () => { + const events: string[] = []; + const genieHome = process.env.GENIE_HOME as string; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + openCodexActivationStore: (() => ({ + observe: () => { + events.push('reobserve-current'); + return currentSnapshot(); + }, + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => { + events.push('delivery-callback-enter'); + const result = callback( + setupDeliveryOps((consumer) => { + if (consumer === MARKETPLACE_CONSUMER) { + events.push('marketplace'); + return undefined; + } + if (consumer === ROLE_CONSUMER) { + events.push(`roles:${readIntegrationConsent(genieHome)}`); + return ROLE_RESULT; + } + if (consumer === RETIREMENT_CONSUMER) { + events.push('retire-fallbacks'); + return RETIREMENT_RESULT; + } + return runDeliveryRootConsumer(consumer as never, DELIVERY_ROOT); + }), + ); + events.push('delivery-callback-exit'); + return result; + }, + })) as never, + createCodexConsentCommitConsumer: (options) => + createSetupCodexConsentCommitConsumer({ + ...options, + persist: (selection, home) => { + events.push(`consent:${selection}`); + persistIntegrationConsent(selection, home); + }, + }), + }), + ); + const { exitCode } = cap.restore(); + + expect(exitCode).toBe(0); + expect(events).toEqual([ + 'delivery-callback-enter', + 'reobserve-current', + 'marketplace', + 'consent:codex', + 'retire-fallbacks', + 'roles:codex', + 'delivery-callback-exit', + ]); + }); + + test('already-current disabled setup skips fallback retirement but still converges roles', async () => { + const disabled = snapshot({ + query: { + status: 'ok', + registration: { present: true, enabled: false, version: ver(T) }, + }, + }); + const events: string[] = []; + let assertionRequested = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => disabled, + requestRetirementAssertion: () => { + assertionRequested += 1; + return { result: 'refused', reason: 'must not prompt current state' }; + }, + openCodexActivationStore: (() => ({ + observe: () => disabled, + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => + callback( + setupDeliveryOps((consumer) => { + if (consumer === MARKETPLACE_CONSUMER) { + events.push('marketplace'); + return undefined; + } + if (consumer === RETIREMENT_CONSUMER) { + events.push('skip-disabled-retirement'); + return DISABLED_RETIREMENT_RESULT; + } + if (consumer === ROLE_CONSUMER) { + events.push('roles'); + return ROLE_RESULT; + } + return runDeliveryRootConsumer(consumer as never, DELIVERY_ROOT); + }), + ), + })) as never, + }), + ); + const { exitCode } = cap.restore(); + + expect(exitCode).toBe(0); + expect(assertionRequested).toBe(0); + expect(events).toEqual(['marketplace', 'skip-disabled-retirement', 'roles']); + }); + test('an installed-newer plugin has no activation authority: exit 1, no consent prompt', async () => { let assertionRequested = 0; const cap = capture(); @@ -425,6 +1068,31 @@ describe('setup Codex activation (Group D)', () => { expect(saved.codex?.configured).not.toBe(true); }); + test('full quick wizard with an already-current generation finalizes only after later sections and saves once', async () => { + const events: string[] = []; + const cap = capture(); + await setupCommand( + { quick: true }, + baseDeps({ + observeCodexActivation: () => currentSnapshot(), + codexFinalizationHooks: { + beforeLocks: () => events.push('finalization-locks'), + afterRoute: () => events.push('route-complete'), + }, + }), + ); + const { out, exitCode } = cap.restore(); + const saved = await loadGenieConfig(); + + expect(exitCode).toBe(0); + expect(events).toEqual(['finalization-locks', 'route-complete']); + expect(out.indexOf('7. Prompt Mode')).toBeLessThan(out.indexOf('Codex managed roles converged')); + expect(out).toContain('Codex: \x1b[32mconfigured'); + expect(out).toContain('Configuration saved!'); + expect(saved.setupComplete).toBe(true); + expect(saved.codex?.configured).toBe(true); + }); + test('setup reaches the brand only through A/B: no fabricated assertion/permit in source', () => { const source = readFileSync(join(import.meta.dir, 'setup.ts'), 'utf8'); // Setup consumes A's consent + B's executor… @@ -443,6 +1111,22 @@ describe('setup Codex activation (Group D)', () => { } }); + test('source pins finalization lock acquisition and release order around the config CAS', () => { + const source = readFileSync(join(import.meta.dir, 'setup.ts'), 'utf8'); + const start = source.indexOf('async function finalizeCodexSetup('); + const end = source.indexOf('\nfunction reportFinalizedCodexSetup(', start); + expect(start).toBeGreaterThan(-1); + expect(end).toBeGreaterThan(start); + const finalization = source.slice(start, end); + const acquireOrdered = finalization.indexOf('const acquired = acquireOrderedLifecycleLeases('); + const persistConfig = finalization.indexOf('await saveSetupConfigUnderHeldLease('); + const releaseOrdered = finalization.indexOf('releaseOrderedLifecycleLeases(codexLease, agentSyncLease)'); + + expect(acquireOrdered).toBeGreaterThan(-1); + expect(acquireOrdered).toBeLessThan(persistConfig); + expect(persistConfig).toBeLessThan(releaseOrdered); + }); + test('source-checkout setup performs zero writes while another process owns the GENIE_HOME lease', () => { const genieHome = process.env.GENIE_HOME as string; const lease = acquireLifecycleLease(genieHome); @@ -473,4 +1157,318 @@ describe('setup Codex activation (Group D)', () => { } expect(existsSync(lockPath)).toBe(false); }); + // ========================================================================== + // Group E, Decision 9/12: the setup-side delivery gate + typed outcome + // ========================================================================== + + // ========================================================================== + // Group E live-QA regression: a journal-quarantine permit must be consumed by + // A's quarantineIntent (then re-observe), never fed to the activation executor. + // ========================================================================== + + /** A corrupt activation journal → state intent-invalid → authority journal-quarantine-only. */ + function corruptIntentSnapshot(): CodexActivationSnapshot { + return { + ...pendingSnapshot(), + intent: { status: 'corrupt', contentSha256: 'd'.repeat(64), detail: 'not json' }, + }; + } + const FAKE_LEASE = { + ok: true, + kind: 'setup-activation', + operationId: 'op-quarantine-test', + assertOperation: () => {}, + release: () => {}, + }; + + test('Group E: a stale journal is quarantined via the REAL consent/authorize chain, then activation proceeds', async () => { + let observeCalls = 0; + let quarantineCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => { + observeCalls += 1; + return observeCalls === 1 ? corruptIntentSnapshot() : pendingSnapshot(); + }, + // REAL requestRetirementAssertion + REAL authorizeCodexActivation run: + // pass 1 mints a journal-quarantine permit, pass 2 an activation permit. + acquireActivationLease: (() => FAKE_LEASE) as never, + openCodexActivationStore: (() => ({ + observe: () => currentSnapshot(), + withRevalidatedDeliveryRoot: ( + _lease: unknown, + callback: (ops: ReturnType) => unknown, + ) => callback(setupDeliveryOps()), + quarantineIntent: () => { + quarantineCalls += 1; + return { quarantinedTo: '/quarantine/intent.invalid-d' }; + }, + })) as never, + executeCodexActivation: () => ACTIVATED, + }), + ); + const { out, exitCode } = cap.restore(); + expect(exitCode).toBe(0); + expect(quarantineCalls).toBe(1); + expect(observeCalls).toBe(2); + expect(out).toContain('Quarantined stale activation journal'); + expect(out).toContain('Activated Codex plugin'); + expect((await loadGenieConfig()).codex?.configured).toBe(true); + }); + + test('Group E: a skipped quarantine reports and exits 1 without touching the executor', async () => { + let executeCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => corruptIntentSnapshot(), + acquireActivationLease: (() => FAKE_LEASE) as never, + openCodexActivationStore: (() => ({ + quarantineIntent: () => ({ skipped: 'unsafe intent path is not quarantined (symlink)' }), + })) as never, + executeCodexActivation: () => { + executeCalls += 1; + return ACTIVATED; + }, + }), + ); + const { err, exitCode } = cap.restore(); + expect(exitCode).toBe(1); + expect(executeCalls).toBe(0); + expect(err).toContain('not quarantined'); + }); + + test('Group E: journal-quarantine contention projects one typed busy trailer with the holder kind', async () => { + let executeCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => corruptIntentSnapshot(), + acquireActivationLease: (() => ({ + ok: false, + code: 'codex-lifecycle-busy', + holderKind: 'rollback', + detail: 'fixture quarantine lease busy', + })) as never, + executeCodexActivation: () => { + executeCalls += 1; + return ACTIVATED; + }, + }), + ); + const { out, err, trailer, exitCode } = cap.restore(); + + expect(exitCode).toBe(2); + expect(executeCalls).toBe(0); + expect(trailer.trim().split('\n')).toHaveLength(1); + expect(JSON.parse(trailer.trim())).toEqual({ + schemaVersion: 1, + code: 'codex-lifecycle-busy', + deliveryComplete: false, + retry: true, + nextAction: 'retry after the current rollback lifecycle command releases the lease', + }); + expect(err).toContain('codex-lifecycle-busy'); + expect(err).toContain('rollback'); + expect(err).toContain('fixture quarantine lease busy'); + expect(out).not.toContain('Codex configuration saved'); + expect((await loadGenieConfig()).codex?.configured).not.toBe(true); + }); + + test('Group E: a second quarantine grant in one invocation refuses instead of looping', async () => { + let quarantineCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + // The journal never clears: every pass re-observes the corrupt intent. + observeCodexActivation: () => corruptIntentSnapshot(), + acquireActivationLease: (() => FAKE_LEASE) as never, + openCodexActivationStore: (() => ({ + quarantineIntent: () => { + quarantineCalls += 1; + return { quarantinedTo: '/quarantine/intent.invalid-d' }; + }, + })) as never, + }), + ); + const { err, exitCode } = cap.restore(); + expect(exitCode).toBe(1); + expect(quarantineCalls).toBe(1); + expect(err).toContain('refusing to loop'); + }); + + test('Group E: an ABSENT delivery record refuses before any consent prompt with the recovery command (exit 1)', async () => { + let assertionRequested = 0; + let executeCalls = 0; + let marketplaceCalls = 0; + let retirementCalls = 0; + let roleCalls = 0; + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + observeCodexActivation: () => ({ ...pendingSnapshot(), delivery: { status: 'absent' } }), + requestRetirementAssertion: () => { + assertionRequested += 1; + return { result: 'granted', assertion: {} as never }; + }, + executeCodexActivation: () => { + executeCalls += 1; + return ACTIVATED; + }, + createCodexMarketplaceConsumer: () => { + marketplaceCalls += 1; + return MARKETPLACE_CONSUMER; + }, + createCodexRoleAgentConsumer: () => { + roleCalls += 1; + return ROLE_CONSUMER; + }, + createCodexFallbackRetirementConsumer: () => { + retirementCalls += 1; + return RETIREMENT_CONSUMER; + }, + }), + ); + const { err, trailer, exitCode } = cap.restore(); + expect(exitCode).toBe(1); + expect(assertionRequested).toBe(0); + expect(executeCalls).toBe(0); + expect(marketplaceCalls).toBe(0); + expect(retirementCalls).toBe(0); + expect(roleCalls).toBe(0); + expect(err).toContain('delivery record is absent'); + expect(err).toContain('genie update'); + const parsed = JSON.parse(trailer) as { code: string; deliveryComplete: boolean; retry: boolean }; + expect(parsed).toMatchObject({ code: 'delivery-incomplete', deliveryComplete: false, retry: true }); + }); + + test('INVALID and MISMATCHED records produce the same consistent delivery-incomplete refusal', async () => { + const mismatchedRecord: CodexActivationSnapshot['delivery'] = { + status: 'present', + record: { + schemaVersion: 2, + deliveryId: deriveDeliveryId(EVIDENCE_DIGEST, DELIVERY_ROOT), + targetVersion: OLD, + canonicalPayloadSha256: DIGEST, + channel: 'stable', + deliveredAt: '2026-07-12T00:00:00.000Z', + evidenceDigest: EVIDENCE_DIGEST, + platformId: 'darwin-arm64', + platformTriple: PLATFORM, + releaseTag: `v${OLD}`, + releaseName: `genie-${OLD}-${PLATFORM}.tar.gz`, + releaseManifestSha256: MANIFEST_DIGEST, + artifactSha256: ARTIFACT_DIGEST, + installedBinarySha256: BINARY_DIGEST, + deliveryRoot: DELIVERY_ROOT, + }, + evidence: deliveryEvidenceFacts(), + }; + const cases: Array<{ delivery: CodexActivationSnapshot['delivery']; assessment: string }> = [ + { delivery: { status: 'invalid', detail: 'corrupt json' }, assessment: 'invalid' }, + { delivery: mismatchedRecord, assessment: 'mismatch' }, + ]; + for (const { delivery, assessment } of cases) { + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ observeCodexActivation: () => ({ ...pendingSnapshot(), delivery }) }), + ); + const { err, exitCode } = cap.restore(); + expect(exitCode).toBe(1); + expect(err).toContain(`delivery record is ${assessment}`); + } + }); + + test('even an already-current machine without a record is delivery-incomplete, never a success claim', async () => { + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ observeCodexActivation: () => ({ ...currentSnapshot(), delivery: { status: 'absent' } }) }), + ); + const { out, err, exitCode } = cap.restore(); + expect(exitCode).toBe(1); + expect(out).not.toContain('already current'); + expect(out).not.toContain('Codex configuration saved'); + expect(err).toContain('delivery record is absent'); + expect((await loadGenieConfig()).codex?.configured).not.toBe(true); + }); + + test('historically configured machine: a failed run preserves config bytes and prints NO green banner (Decision 12)', async () => { + // Run 1: current + matching record → success, banner, configured persisted. + const capFirst = capture(); + await setupCommand({ codex: true }, baseDeps({ observeCodexActivation: () => currentSnapshot() })); + const first = capFirst.restore(); + expect(first.out).toContain('Codex configuration saved'); + expect((await loadGenieConfig()).codex?.configured).toBe(true); + + // Run 2: same machine, record now missing → failed run, historical + // configured stays persisted (bytes preserved) but no success banner. + const capSecond = capture(); + await setupCommand( + { codex: true }, + baseDeps({ observeCodexActivation: () => ({ ...currentSnapshot(), delivery: { status: 'absent' } }) }), + ); + const second = capSecond.restore(); + expect(second.exitCode).toBe(1); + expect(second.out).not.toContain('Codex configuration saved'); + expect((await loadGenieConfig()).codex?.configured).toBe(true); + }); + + test('failed standalone setup preserves compact user config byte-for-byte, including unknown keys', async () => { + const configPath = join(process.env.GENIE_HOME as string, 'config.json'); + mkdirSync(process.env.GENIE_HOME as string, { recursive: true }); + const compact = + '{"version":2,"runtime":{"defaultAgent":"claude"},"codex":{"configured":true},"userOwned":{"keep":"exactly"}}\n'; + writeFileSync(configPath, compact); + const before = readFileSync(configPath); + + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ observeCodexActivation: () => ({ ...currentSnapshot(), delivery: { status: 'absent' } }) }), + ); + const { out, exitCode } = cap.restore(); + + expect(exitCode).toBe(1); + expect(out).not.toContain('Codex configuration saved'); + expect(readFileSync(configPath).equals(before)).toBe(true); + }); + + test('pending standalone setup preserves compact user config byte-for-byte and prints no success banner', async () => { + const configPath = join(process.env.GENIE_HOME as string, 'config.json'); + mkdirSync(process.env.GENIE_HOME as string, { recursive: true }); + const compact = + '{ "version": 2, "runtime": { "defaultAgent": "claude" }, "codex": { "configured": true }, "userOwned": 7 }\n'; + writeFileSync(configPath, compact); + const before = readFileSync(configPath); + + const cap = capture(); + await setupCommand( + { codex: true }, + baseDeps({ + requestRetirementAssertion: () => ({ result: 'refused', reason: 'fixture pending' }), + }), + ); + const { out, exitCode } = cap.restore(); + + expect(exitCode).toBe(2); + expect(out).not.toContain('Codex configuration saved'); + expect(readFileSync(configPath).equals(before)).toBe(true); + }); + + test("full wizard summary reflects THIS run's typed outcome, not historical state", async () => { + const cap = capture(); + await setupCommand({ quick: true }, baseDeps()); + const { out, exitCode } = cap.restore(); + expect(exitCode).toBe(2); + expect(out).toContain('not configured this run (consent-refused)'); + expect(out).not.toContain('Codex: \x1b[32mconfigured'); + }); }); diff --git a/src/genie-commands/setup.ts b/src/genie-commands/setup.ts index 594e4981a..089fc599d 100644 --- a/src/genie-commands/setup.ts +++ b/src/genie-commands/setup.ts @@ -28,9 +28,29 @@ import { // Setup never reimplements the TTY/env/flag guards, constructs a RetirementAssertion, // or retains a permit — it supplies a real-terminal ConsentContext and routes the // permit-gated cache advance through B's `executeCodexActivation`. -import type { ActivationEntryPath, CodexActivationSnapshot, ConsentContext } from '../lib/codex-activation.js'; +import type { + ActivationEntryPath, + ActivationPermit, + CodexActivationSnapshot, + ConsentContext, +} from '../lib/codex-activation.js'; import { getCodexConfigPath } from '../lib/codex-config.js'; -import { type CodexPluginProbe, reconcileCodexProjectMcp, resolveGitWorktreeRoot } from '../lib/codex-project-mcp.js'; +import type { DeliveryEvidenceVerificationDependencies } from '../lib/codex-delivery-evidence.js'; +// The quarantine hop holds the SAME `setup-activation` lease kind the executor +// acquires (aliased: agent-sync exports an unrelated same-named lease helper). +import { + type HeldLifecycleLease, + acquireLifecycleLease as acquireActivationLifecycleLease, +} from '../lib/codex-lifecycle-lease.js'; +// Group E: the shared Decision-9 delivery gate — the same assessment the +// executor's `beginActivation` inner guard re-applies before its first write. +import { assessSnapshotDelivery } from '../lib/codex-lifecycle-truth.js'; +import { + type CodexPluginProbe, + genieFacadeMcpEntry, + reconcileCodexProjectMcp, + resolveGitWorktreeRoot, +} from '../lib/codex-project-mcp.js'; import { contractPath, getGenieConfigPath, @@ -40,9 +60,14 @@ import { saveGenieConfig, } from '../lib/genie-config.js'; import { resolveCodexDir, resolveGenieHome } from '../lib/genie-home.js'; +import { acquireOrderedLifecycleLeases, releaseOrderedLifecycleLeases } from '../lib/ordered-lifecycle-leases.js'; import { + type CodexAgentInstallResult, type IntegrationSelection, - persistIntegrationConsent, + createCodexMarketplaceRegistrationConsumer, + createSetupCodexConsentCommitConsumer, + createSetupCodexFallbackRetirementConsumer, + createSetupCodexRoleAgentConsumer, readIntegrationConsent, } from '../lib/runtime-integrations.js'; import { checkCommand } from '../lib/system-detect.js'; @@ -63,13 +88,18 @@ export interface SetupOptions { export interface SetupDeps { checkCommand?: typeof checkCommand; readIntegrationConsent?: typeof readIntegrationConsent; - persistIntegrationConsent?: typeof persistIntegrationConsent; + /** Factory for the explicit-consent capability consumed only by the deep store. */ + createCodexConsentCommitConsumer?: typeof createSetupCodexConsentCommitConsumer; /** Interactive confirmation seam; production uses @inquirer/prompts. */ confirm?: typeof confirm; acquireLifecycleLease?: typeof acquireLifecycleLease; + /** Test seam for the `setup-activation` codex lifecycle lease held during a journal quarantine. */ + acquireActivationLease?: typeof acquireActivationLifecycleLease; /** Test seam for the once-bound absolute Codex CLI path. */ resolveExecutable?: (name: string, cwd: string) => string | null; cwd?: string; + /** TEST-ONLY cryptographic seam for persisted delivery-evidence fixtures; no CLI/env path exposes it. */ + deliveryEvidenceVerification?: DeliveryEvidenceVerificationDependencies; // --- Codex activation seams (Group D): all default to the real facade. --- /** Bounded activation observation (B's facade). */ observeCodexActivation?: (options: { @@ -85,11 +115,23 @@ export interface SetupDeps { openCodexActivationStore?: typeof openCodexActivationStore; /** The permit-gated executor (B) — the sole route to Codex cache mutation. */ executeCodexActivation?: typeof executeCodexActivation; + /** Current-state marketplace capability factory; only the deep store may consume it. */ + createCodexMarketplaceConsumer?: typeof createCodexMarketplaceRegistrationConsumer; + /** Setup-only fallback-retirement capability; only the deep store may consume it. */ + createCodexFallbackRetirementConsumer?: typeof createSetupCodexFallbackRetirementConsumer; + /** Post-activation role capability factory; only the deep store may consume it. */ + createCodexRoleAgentConsumer?: typeof createSetupCodexRoleAgentConsumer; /** Synchronous real-TTY confirmation for the retirement assertion prompt. */ promptRetirementConfirmation?: (message: string) => boolean; /** stdin/stdout TTY flags forwarded to A's consent guards (default: real streams). */ stdinIsTTY?: boolean; stdoutIsTTY?: boolean; + /** Deterministic race seams for the setup finalization transaction. Tests only. */ + codexFinalizationHooks?: { + beforeLocks?: () => void; + afterAssets?: () => void; + afterRoute?: () => void; + }; } export class SetupIntegrationError extends Error { @@ -99,6 +141,23 @@ export class SetupIntegrationError extends Error { } } +/** + * The setup command boundary distinguishes lifecycle contention from an + * integration failure. Only this typed refusal projects exit 2 plus the + * canonical machine trailer; all other setup errors remain exit 1. + */ +export class SetupCodexLifecycleBusyError extends SetupIntegrationError { + readonly code = 'codex-lifecycle-busy'; + + constructor( + readonly holderKind: string | null, + detail: string, + ) { + super(`codex-lifecycle-busy: the ${holderKind ?? 'unknown'} lifecycle command holds the Codex lease: ${detail}`); + this.name = 'SetupCodexLifecycleBusyError'; + } +} + /** * Print the header banner */ @@ -259,10 +318,15 @@ async function configureShortcuts(config: GenieConfig, quick: boolean, deps: Set // (the sole source of a genuine retirement assertion) and executed through B's // permit-gated `executeCodexActivation` (the sole route to Codex cache mutation, // which acquires the `setup-activation` lifecycle lease itself). DELIVERY — -// installing/refreshing the plugin payload, marketplace registration, role -// agents — belongs to `genie update` / `genie install`. Every refusal (guard -// failure, decline, EOF, unauthorized state) happens before any -// marketplace/plugin/config/role-agent/project-fallback/intent/trust mutation. +// installing/refreshing the plugin payload and publishing authenticated delivery +// facts belongs to `genie update` / `genie install`. Marketplace registration +// and managed role convergence are activation-owned: the executor registers the +// canonical marketplace only after `beginActivation`, and setup converges roles +// only after activation/current verification. Setup commits explicit Codex +// maintenance consent, proves the exact enabled plugin and retires only clean +// historical fallbacks, then adopts/converges managed roles. Every refusal +// happens before marketplace/plugin/config/fallback/role-agent/project-route +// mutation. /** * Synchronous real-terminal confirmation for the retirement assertion. A's @@ -303,25 +367,34 @@ function buildConsentContext(deps: SetupDeps, quick: boolean): ConsentContext { }; } -function reconcileSetupCodexProject(root: string | null, plugin: CodexPluginProbe): void { +function reconcileSetupCodexProject(root: string | null, plugin: CodexPluginProbe): string { if (root === null) { - console.log(' \x1b[2mNo Git worktree detected; project MCP fallback was not changed.\x1b[0m'); - return; + return ' \x1b[2mNo Git worktree detected; project MCP fallback was not changed.\x1b[0m'; } - const project = reconcileCodexProjectMcp(root, plugin); + // Decision 2: the marker route carries the stable absolute GENIE_HOME facade, + // exactly as trusted `genie init` writes it. + const project = reconcileCodexProjectMcp(root, plugin, genieFacadeMcpEntry()); if (!project.ok) throw new SetupIntegrationError(project.detail ?? 'Codex project MCP reconciliation failed'); - console.log(` \x1b[32m✓\x1b[0m Project MCP route: ${project.route} (${project.detail ?? project.action})`); + return ` \x1b[32m✓\x1b[0m Project MCP route: ${project.route} (${project.detail ?? project.action})`; } -/** A synthetic verified-current probe: an activated plugin removes the project fallback. */ +/** + * Post-activation route probe (Group E). The installed Codex plugin ships NO + * MCP route (Group A removed the manifest declaration), so for ROUTE purposes + * an activated plugin is never "usable" and the marker-owned project route + * remains required. Decision 1: plugin availability never creates or removes + * the project route — the pre-A behavior of removing the fallback after + * activation left a repository with no Codex route at all. + */ function verifiedCurrentPluginProbe(): CodexPluginProbe { return { cliAvailable: true, status: 'ok', installed: true, enabled: true, - usable: true, - detail: 'activation verified-current (plugin trusted, project fallback removed)', + usable: false, + usabilityDetail: 'installed plugin contributes no Codex MCP route; the marker-owned project route is authoritative', + detail: 'activation verified-current; project route remains marker-owned', }; } @@ -329,25 +402,60 @@ function verifiedCurrentPluginProbe(): CodexPluginProbe { export function mergeCodexIntegrationConsent(current: IntegrationSelection): IntegrationSelection { if (current === 'none') return 'codex'; if (current === 'claude') return 'all'; + if (current === 'auto') return 'codex'; return current; } -function preserveRuntimeChoiceAfterCodex(config: GenieConfig): void { +function preserveRuntimeChoiceAfterCodex(config: GenieConfig): string | undefined { const decision = resolveDefaultAgentAfterCodex(config.runtime.defaultAgent); config.runtime.defaultAgent = decision.agent; - if (decision.hint) console.log(` \x1b[2m${decision.hint}\x1b[0m`); + return decision.hint; } +/** + * The typed per-invocation setup outcome (Decision 12). Success (a green banner + * + persisted `codex.configured`) flows ONLY from this invocation's outcome — + * never from historical config state — so a failed or pending run can never + * print or persist success. + */ +type SetupCodexOutcomeCode = + | 'activated' + | 'current' + | 'payload-missing' + | 'delivery-incomplete' + | 'status-reported' + | 'consent-refused' + | 'not-authorized' + | 'busy' + | 'stale' + | 'refused' + | 'broken' + | 'quarantine-skipped'; + interface ActivationOutcome { exitCode: 0 | 1 | 2; /** True when the plugin is verified-current (freshly activated or already current). */ activated: boolean; + /** The one typed code this invocation ends with; `activated` is true only for 'activated' | 'current'. */ + code: SetupCodexOutcomeCode; } function emitTrailer(trailer: ReturnType): void { process.stdout.write(`${serializeActivationResultTrailer(trailer)}\n`); } +function emitCodexLifecycleBusyTrailer(holderKind: string | null): void { + emitTrailer({ + schemaVersion: 1, + code: 'codex-lifecycle-busy', + deliveryComplete: false, + retry: true, + nextAction: holderKind + ? `retry after the current ${holderKind} lifecycle command releases the lease` + : 'retry after the current lifecycle command releases the lease', + }); +} + function resolveCodexPath(deps: SetupDeps, cwd: string): string | null { try { if (deps.resolveExecutable) return deps.resolveExecutable('codex', cwd); @@ -357,6 +465,34 @@ function resolveCodexPath(deps: SetupDeps, cwd: string): string | null { } } +function observeSetupCodexActivation( + deps: SetupDeps, + options: { command: string; genieHome: string; codexHome: string }, +): CodexActivationSnapshot { + return deps.observeCodexActivation + ? deps.observeCodexActivation(options) + : observeCodexActivation({ + ...options, + deliveryEvidenceVerification: deps.deliveryEvidenceVerification, + }); +} + +function reportDeliveryIncomplete(snapshot: CodexActivationSnapshot): ActivationOutcome | null { + const deliveryGate = assessSnapshotDelivery(snapshot); + if (deliveryGate.kind !== 'incomplete') return null; + const gate = deliveryGate.result; + console.error(` \x1b[31m✖\x1b[0m ${gate.detail}`); + console.error(` Recovery: \x1b[36m${gate.recovery}\x1b[0m`); + emitTrailer({ + schemaVersion: 1, + code: 'delivery-incomplete', + deliveryComplete: false, + retry: true, + nextAction: gate.recovery, + }); + return { exitCode: gate.exit, activated: false, code: 'delivery-incomplete' }; +} + /** * The permit-gated Codex activation transaction. Observes, classifies, gates on * A's consent + authorization, then routes the cache advance through B's @@ -368,11 +504,12 @@ function runCodexActivation( codexPath: string, entry: ActivationEntryPath, quick: boolean, + attempt = 0, ): ActivationOutcome { const genieHome = resolveGenieHome(); const codexHome = resolveCodexDir(); - const observe = deps.observeCodexActivation ?? ((options) => observeCodexActivation(options)); - const snapshot = observe({ command: codexPath, genieHome, codexHome }); + const observeOptions = { command: codexPath, genieHome, codexHome }; + const snapshot = observeSetupCodexActivation(deps, observeOptions); const state = classifyCodexActivation(snapshot); const descriptor = describeState(state); const deliveryComplete = snapshot.canonical.status === 'ok'; @@ -383,16 +520,24 @@ function runCodexActivation( console.error( ' Delivery is done by \x1b[36mgenie update\x1b[0m / \x1b[36mgenie install\x1b[0m — run one, then rerun \x1b[36mgenie setup --codex\x1b[0m.', ); - return { exitCode: 1, activated: false }; + return { exitCode: 1, activated: false, code: 'payload-missing' }; } + // Condition 2 (Group E, Decision 9): a matching authenticated delivery record + // is required BEFORE the first prompt or activation-owned mutation — even on + // an already-current machine, because only update/install may publish the + // record and setup must not claim success it cannot attest. The executor's + // `beginActivation` inner guard re-checks the same assessment immediately + // before its first write (defense in depth). + const incomplete = reportDeliveryIncomplete(snapshot); + if (incomplete !== null) return incomplete; if (state.kind === 'current') { console.log(' \x1b[32m✓\x1b[0m Codex plugin is already current; no activation needed.'); - return { exitCode: 0, activated: true }; + return { exitCode: 0, activated: true, code: 'current' }; } if (descriptor.authority === 'none') { const projection = projectHumanStatus(state, snapshot); (projection.stream === 'stderr' ? process.stderr : process.stdout).write(` ${projection.text}\n`); - return { exitCode: projection.exitCode, activated: false }; + return { exitCode: projection.exitCode, activated: false, code: 'status-reported' }; } // Activation authority — gate on A's deep consent API (owns TTY/env/flag/prompt). @@ -403,7 +548,11 @@ function runCodexActivation( if (consent.result !== 'granted') { console.error(` \x1b[31m✖\x1b[0m Retirement assertion refused: ${consent.reason}`); emitTrailer(buildActivationResultTrailer(state, deliveryComplete)); - return { exitCode: resolveSetupExitCode(state, { result: 'refused', reason: consent.reason }), activated: false }; + return { + exitCode: resolveSetupExitCode(state, { result: 'refused', reason: consent.reason }), + activated: false, + code: 'consent-refused', + }; } const authorization = (deps.authorizeCodexActivation ?? authorizeCodexActivation)({ state, @@ -414,15 +563,27 @@ function runCodexActivation( const reason = 'reason' in authorization ? authorization.reason : authorization.result; console.error(` \x1b[31m✖\x1b[0m Activation not authorized: ${reason}`); emitTrailer(buildActivationResultTrailer(state, deliveryComplete)); - return { exitCode: resolveSetupExitCode(state, authorization), activated: false }; + return { exitCode: resolveSetupExitCode(state, authorization), activated: false, code: 'not-authorized' }; + } + + // Group E (live-QA find): an intent-invalid/intent-mismatch state grants a + // JOURNAL-QUARANTINE permit, not an activation permit. Feeding it to the + // executor made beginActivation refuse ("permit lacks activation capability") + // and left the state's own recovery — "quarantine the mismatched intent after + // a fresh assertion, then re-observe" — unreachable, looping forever. Route + // the permit to A's quarantine API, then re-observe and continue once. + if (authorization.permit.capability === 'journal-quarantine') { + return runJournalQuarantine(deps, codexPath, entry, quick, authorization.permit, attempt); } // Permit granted — the executor acquires the `setup-activation` lease itself. - const store = (deps.openCodexActivationStore ?? openCodexActivationStore)({ - genieHome, - codexHome, - command: codexPath, - }); + const storeOptions = { genieHome, codexHome, command: codexPath }; + const store = deps.openCodexActivationStore + ? deps.openCodexActivationStore(storeOptions) + : openCodexActivationStore({ + ...storeOptions, + deliveryEvidenceVerification: deps.deliveryEvidenceVerification, + }); const result = (deps.executeCodexActivation ?? executeCodexActivation)({ permit: authorization.permit, store, @@ -434,11 +595,67 @@ function runCodexActivation( return reportActivationExecution(result); } +/** + * Consume a journal-quarantine permit: move the invalid/mismatched activation + * journal aside under the same `setup-activation` lifecycle lease the executor + * would hold, then re-observe with one fresh pass through `runCodexActivation` + * (the fresh state prompts its own consent for any actual activation). One hop + * only — a second quarantine grant in the same invocation reports instead of + * looping. + */ +function runJournalQuarantine( + deps: SetupDeps, + codexPath: string, + entry: ActivationEntryPath, + quick: boolean, + permit: ActivationPermit, + attempt: number, +): ActivationOutcome { + const genieHome = resolveGenieHome(); + const codexHome = resolveCodexDir(); + if (attempt > 0) { + console.error(' \x1b[31m✖\x1b[0m A second quarantine was requested in one invocation; refusing to loop.'); + console.error( + ' Rerun \x1b[36mgenie setup --codex\x1b[0m; if it recurs, include `genie doctor --json` in an issue.', + ); + return { exitCode: 1, activated: false, code: 'quarantine-skipped' }; + } + const acquire = deps.acquireActivationLease ?? acquireActivationLifecycleLease; + const lease = acquire('setup-activation', { genieHome }); + if (!lease.ok) { + console.error( + ` \x1b[31m✖\x1b[0m codex-lifecycle-busy: the ${lease.holderKind ?? 'unknown'} lifecycle command holds the Codex lease: ${lease.detail}`, + ); + emitCodexLifecycleBusyTrailer(lease.holderKind); + return { exitCode: 2, activated: false, code: 'busy' }; + } + let quarantined: { quarantinedTo: string } | { skipped: string }; + try { + const storeOptions = { genieHome, codexHome, command: codexPath }; + const store = deps.openCodexActivationStore + ? deps.openCodexActivationStore(storeOptions) + : openCodexActivationStore({ + ...storeOptions, + deliveryEvidenceVerification: deps.deliveryEvidenceVerification, + }); + quarantined = store.quarantineIntent(lease, permit); + } finally { + lease.release(); + } + if ('skipped' in quarantined) { + console.error(` \x1b[31m✖\x1b[0m Stale activation journal was not quarantined: ${quarantined.skipped}`); + return { exitCode: 1, activated: false, code: 'quarantine-skipped' }; + } + console.log(` \x1b[33m!\x1b[0m Quarantined stale activation journal → ${quarantined.quarantinedTo}`); + console.log(' Re-observing the delivered generation...'); + return runCodexActivation(deps, codexPath, entry, quick, attempt + 1); +} + function reportActivationExecution(result: ActivationExecutionResult): ActivationOutcome { if (result.status === 'activated') { console.log(` \x1b[32m✓\x1b[0m Activated Codex plugin v${result.version} (enabled=${result.enabled}).`); console.log(` \x1b[33m!\x1b[0m ${result.recovery}`); - return { exitCode: 0, activated: true }; + return { exitCode: 0, activated: true, code: 'activated' }; } console.error(` \x1b[31m✖\x1b[0m ${result.detail}`); if (result.status === 'broken') { @@ -447,8 +664,158 @@ function reportActivationExecution(result: ActivationExecutionResult): Activatio ); } emitTrailer(result.trailer); - // busy/stale/refused are action-required (exit 2); broken is retry (exit 1). - return { exitCode: result.status === 'broken' ? 1 : 2, activated: false }; + // The executor's own delivery-incomplete inner-guard refusal is exit 1 like + // broken; busy/stale/refused stay action-required (exit 2). + const exitCode = result.status === 'broken' || result.status === 'delivery-incomplete' ? 1 : 2; + return { exitCode, activated: false, code: result.status }; +} + +interface PostActivationCodexAssets { + agents: CodexAgentInstallResult; + retiredFallbacks: readonly string[]; +} + +function convergePostActivationCodexAssetsUnderLease( + deps: SetupDeps, + codexPath: string, + outcome: ActivationOutcome, + lease: HeldLifecycleLease, +): PostActivationCodexAssets { + const genieHome = resolveGenieHome(); + const codexHome = resolveCodexDir(); + const storeOptions = { genieHome, codexHome, command: codexPath }; + const store = deps.openCodexActivationStore + ? deps.openCodexActivationStore(storeOptions) + : openCodexActivationStore({ + ...storeOptions, + deliveryEvidenceVerification: deps.deliveryEvidenceVerification, + }); + return store.withRevalidatedDeliveryRoot(lease, (ops) => { + // The executor released its lease before this post-activation pass. A + // delivery/update can win that gap, so both fresh activation and the + // already-current path must re-observe current+matching under the newly + // acquired lease before consent, roles, route, or success reporting. + const snapshot = store.observe(); + if (assessSnapshotDelivery(snapshot).kind !== 'matching' || classifyCodexActivation(snapshot).kind !== 'current') { + throw new SetupIntegrationError('Codex current state changed before managed-asset convergence'); + } + + // Already-current is the sole no-permit path. Register its marketplace + // inside the callback-scoped authenticated delivery capability. + if (outcome.code === 'current') { + const marketplaceConsumer = (deps.createCodexMarketplaceConsumer ?? createCodexMarketplaceRegistrationConsumer)({ + command: codexPath, + }); + ops.consume(marketplaceConsumer); + } + + // Explicit setup success commits Codex maintenance consent before + // authenticated fallback retirement and managed-role adoption. + const read = deps.readIntegrationConsent ?? readIntegrationConsent; + const consentConsumer = (deps.createCodexConsentCommitConsumer ?? createSetupCodexConsentCommitConsumer)({ + selection: mergeCodexIntegrationConsent(read(genieHome)), + genieHome, + }); + ops.consume(consentConsumer); + const retirementConsumer = ( + deps.createCodexFallbackRetirementConsumer ?? createSetupCodexFallbackRetirementConsumer + )({ + command: codexPath, + expectedVersion: ops.deliveredVersion(), + codexHome, + }); + const retirement = ops.consume(retirementConsumer); + const roleConsumer = (deps.createCodexRoleAgentConsumer ?? createSetupCodexRoleAgentConsumer)({ + genieHome, + codexHome, + }); + return { + agents: ops.consume(roleConsumer), + retiredFallbacks: retirement.retired, + }; + }); +} + +interface PreparedCodexFinalization { + codexPath: string; + cwd: string; + outcome: ActivationOutcome; +} + +interface FinalizedCodexSetup { + config: GenieConfig; + assets: PostActivationCodexAssets; + projectLine: string; + runtimeHint?: string; +} + +/** + * Commit setup's successful Codex result as one serialized transaction. + * + * Lock order matches install/update: the outer agent-sync lifecycle lease is + * acquired first, then the Codex lifecycle lease. Holding both across the + * matching/current revalidation, assets, route, and config CAS prevents an + * update from publishing T+1 after asset convergence while setup still records + * T as configured. Release is always the exact reverse order. + */ +async function finalizeCodexSetup( + config: GenieConfig, + baseline: GenieConfig, + deps: SetupDeps, + prepared: PreparedCodexFinalization, +): Promise { + deps.codexFinalizationHooks?.beforeLocks?.(); + const genieHome = resolveGenieHome(); + const acquired = acquireOrderedLifecycleLeases( + () => (deps.acquireLifecycleLease ?? acquireLifecycleLease)(genieHome), + () => (deps.acquireActivationLease ?? acquireActivationLifecycleLease)('setup-activation', { genieHome }), + ); + if (!acquired.ok) { + if (acquired.busy === 'agent-sync') { + throw new SetupCodexLifecycleBusyError('agent-sync', acquired.detail); + } + throw new SetupCodexLifecycleBusyError(acquired.refusal.holderKind, acquired.refusal.detail); + } + const { agentSyncLease, codexLease } = acquired; + try { + const assets = convergePostActivationCodexAssetsUnderLease(deps, prepared.codexPath, prepared.outcome, codexLease); + deps.codexFinalizationHooks?.afterAssets?.(); + + const projectLine = reconcileSetupCodexProject(resolveGitWorktreeRoot(prepared.cwd), verifiedCurrentPluginProbe()); + deps.codexFinalizationHooks?.afterRoute?.(); + + const nextConfig = structuredClone(config); + nextConfig.codex = { configured: true }; + const runtimeHint = preserveRuntimeChoiceAfterCodex(nextConfig); + await saveSetupConfigUnderHeldLease(nextConfig, baseline); + return { + config: nextConfig, + assets, + projectLine, + ...(runtimeHint === undefined ? {} : { runtimeHint }), + }; + } finally { + releaseOrderedLifecycleLeases(codexLease, agentSyncLease); + } +} + +function reportFinalizedCodexSetup(finalized: FinalizedCodexSetup): void { + if (finalized.assets.retiredFallbacks.length > 0) { + console.log( + ` \x1b[32m✓\x1b[0m Retired ${finalized.assets.retiredFallbacks.length} clean historical Codex fallback skill${finalized.assets.retiredFallbacks.length === 1 ? '' : 's'}.`, + ); + } + console.log(` \x1b[32m✓\x1b[0m Codex managed roles converged (${finalized.assets.agents.installed} delivered).`); + console.log(finalized.projectLine); + if (finalized.runtimeHint !== undefined) console.log(` \x1b[2m${finalized.runtimeHint}\x1b[0m`); +} + +interface ConfigureCodexResult { + config: GenieConfig; + /** Null when the Codex CLI is absent/skipped — nothing was attempted this invocation. */ + outcome: ActivationOutcome | null; + /** Present only when matching/current state still needs atomic setup finalization. */ + prepared: PreparedCodexFinalization | null; } async function configureCodex( @@ -456,49 +823,39 @@ async function configureCodex( quick: boolean, deps: SetupDeps, entry: ActivationEntryPath, -): Promise { +): Promise { printSection('5. Codex Integration', 'Activate the delivered Codex plugin generation for genie agents'); const cwd = deps.cwd ?? process.cwd(); const codexPath = resolveCodexPath(deps, cwd); if (codexPath === null) { console.log(' \x1b[33m!\x1b[0m Codex CLI not found. Skipping codex integration.'); - return config; + return { config, outcome: null, prepared: null }; } const codexCheck = await (deps.checkCommand ?? checkCommand)(codexPath, { which: () => codexPath }); if (codexCheck.timedOut) throw new SetupIntegrationError(codexCheck.error ?? 'Codex CLI detection timed out'); if (!codexCheck.exists) { console.log(' \x1b[33m!\x1b[0m Codex CLI not found. Skipping codex integration.'); - return config; + return { config, outcome: null, prepared: null }; } console.log(` \x1b[32m✓\x1b[0m Codex CLI found (${codexCheck.version ?? 'unknown version'})`); console.log(); console.log(' \x1b[1mSetup activates\x1b[0m the delivered Codex plugin generation, retiring the prior one.'); - console.log(' Delivery (plugin payload, marketplace, role agents, MCP) is done by'); + console.log(' Delivery (plugin payload and authenticated delivery record) is done by'); console.log( - ' \x1b[36mgenie update\x1b[0m / \x1b[36mgenie install\x1b[0m — setup only activates what was already delivered.', + ' \x1b[36mgenie update\x1b[0m / \x1b[36mgenie install\x1b[0m — setup registers and activates only those verified bytes.', ); console.log(' Activation requires an interactive real terminal; --quick, CI, and piped input cannot activate.'); console.log(` Config: \x1b[2m${contractPath(getCodexConfigPath())}\x1b[0m`); console.log(); const outcome = runCodexActivation(deps, codexPath, entry, quick); - if (outcome.activated) { - // Post-activation, permitted mutations: reconcile the project MCP fallback - // (verified-current now) and persist durable maintenance consent for update. - await withSetupLease(deps, () => { - reconcileSetupCodexProject(resolveGitWorktreeRoot(cwd), verifiedCurrentPluginProbe()); - const genieHome = resolveGenieHome(); - const read = deps.readIntegrationConsent ?? readIntegrationConsent; - const persist = deps.persistIntegrationConsent ?? persistIntegrationConsent; - persist(mergeCodexIntegrationConsent(read(genieHome)), genieHome); - }); - config.codex = { configured: true }; - preserveRuntimeChoiceAfterCodex(config); - } else if (outcome.exitCode !== 0) { - process.exitCode = outcome.exitCode; - } - return config; + if (!outcome.activated && outcome.exitCode !== 0) process.exitCode = outcome.exitCode; + return { + config, + outcome, + prepared: outcome.activated ? { codexPath, cwd, outcome } : null, + }; } type DefaultAgent = GenieConfig['runtime']['defaultAgent']; @@ -588,24 +945,41 @@ async function configurePromptMode(config: GenieConfig, quick: boolean): Promise // Summary and Save // ============================================================================ -async function showSummaryAndSave(config: GenieConfig, baseline: GenieConfig, deps: SetupDeps): Promise { - printSection('Summary', `Configuration will be saved to ${contractPath(getGenieConfigPath())}`); - +function showSummary(config: GenieConfig, codexOutcome?: ActivationOutcome | null): void { + printSection('Summary', `Configuration saved to ${contractPath(getGenieConfigPath())}`); + + // Decision 12: when this run attempted Codex, the summary states THIS run's + // typed outcome; historical `configured` state cannot masquerade as success. + const codexLine = + codexOutcome === undefined || codexOutcome === null + ? config.codex?.configured + ? '\x1b[32mconfigured\x1b[0m' + : '\x1b[2mnot configured\x1b[0m' + : codexOutcome.activated + ? '\x1b[32mconfigured\x1b[0m' + : `\x1b[2mnot configured this run (${codexOutcome.code})\x1b[0m`; console.log(` Session: \x1b[36m${config.session.name}\x1b[0m (window: ${config.session.defaultWindow})`); console.log(` Terminal: timeout=${config.terminal.execTimeout}ms, lines=${config.terminal.readLines}`); console.log( ` Shortcuts: ${config.shortcuts.tmuxInstalled ? '\x1b[32minstalled\x1b[0m' : '\x1b[2mnot installed\x1b[0m'}`, ); - console.log(` Codex: ${config.codex?.configured ? '\x1b[32mconfigured\x1b[0m' : '\x1b[2mnot configured\x1b[0m'}`); + console.log(` Codex: ${codexLine}`); console.log(` Debug: tmux=${config.logging.tmuxDebug}, verbose=${config.logging.verbose}`); console.log(` Prompt mode: \x1b[36m${config.promptMode}\x1b[0m`); console.log(); +} - // Save config +async function showSummaryAndSave( + config: GenieConfig, + baseline: GenieConfig, + deps: SetupDeps, + codexOutcome?: ActivationOutcome | null, +): Promise { config.setupComplete = true; config.lastSetupAt = new Date().toISOString(); await saveSetupConfig(config, baseline, deps); + showSummary(config, codexOutcome); console.log('\x1b[32m\u2713 Configuration saved!\x1b[0m'); } @@ -687,9 +1061,16 @@ async function runSetupCommand(options: SetupOptions, deps: SetupDeps): Promise< if (options.codex) { printHeader(); - config = await configureCodex(config, options.quick ?? false, deps, 'setup-codex'); - await saveSetupConfig(config, baseline, deps); - if (config.codex?.configured) { + const codexRun = await configureCodex(config, options.quick ?? false, deps, 'setup-codex'); + config = codexRun.config; + // Decision 12: the green banner flows from THIS invocation's typed outcome, + // never from historical `codex.configured` state. A failed, pending, or + // skipped standalone run performs no config write at all, preserving the + // user's exact bytes (including formatting and unknown keys). + if (codexRun.prepared !== null) { + const finalized = await finalizeCodexSetup(config, baseline, deps, codexRun.prepared); + config = finalized.config; + reportFinalizedCodexSetup(finalized); console.log('\x1b[32m\u2713 Codex configuration saved.\x1b[0m'); } return; @@ -708,12 +1089,24 @@ async function runSetupCommand(options: SetupOptions, deps: SetupDeps): Promise< config = await configureSession(config, quick); config = await configureTerminal(config, quick); config = await configureShortcuts(config, quick, deps); - config = await configureCodex(config, quick, deps, 'full-setup-codex-step'); + const codexRun = await configureCodex(config, quick, deps, 'full-setup-codex-step'); + config = codexRun.config; config = await configureDebug(config, quick); config = await configurePromptMode(config, quick); - // Save and show summary - await showSummaryAndSave(config, baseline, deps); + // Save and show summary (unrelated completed sections save regardless; the + // Codex line reflects THIS run's typed outcome, not historical state). + if (codexRun.prepared === null) { + await showSummaryAndSave(config, baseline, deps, codexRun.outcome); + } else { + config.setupComplete = true; + config.lastSetupAt = new Date().toISOString(); + const finalized = await finalizeCodexSetup(config, baseline, deps, codexRun.prepared); + config = finalized.config; + reportFinalizedCodexSetup(finalized); + showSummary(config, codexRun.outcome); + console.log('\x1b[32m\u2713 Configuration saved!\x1b[0m'); + } // This file mutation follows the same just-acquired/revalidated config // commit rather than extending a lease across any wizard prompt. @@ -728,6 +1121,12 @@ export async function setupCommand(options: SetupOptions = {}, deps: SetupDeps = try { await runSetupCommand(options, deps); } catch (error) { + if (error instanceof SetupCodexLifecycleBusyError) { + console.error(`Error: Genie setup refused: ${error.message}`); + emitCodexLifecycleBusyTrailer(error.holderKind); + process.exitCode = 2; + return; + } const detail = error instanceof Error ? error.message : String(error); console.error(`Error: Genie setup failed: ${detail}`); process.exitCode = 1; @@ -747,13 +1146,16 @@ async function withSetupLease(deps: SetupDeps, mutation: () => T | Promise /** Fail closed instead of overwriting config changed while the wizard prompted. */ async function saveSetupConfig(config: GenieConfig, baseline: GenieConfig, deps: SetupDeps): Promise { - await withSetupLease(deps, async () => { - const current = await loadGenieConfig(); - if (JSON.stringify(current) !== JSON.stringify(baseline)) { - throw new SetupIntegrationError('Genie configuration changed while setup was open; review it and retry setup'); - } - await saveGenieConfig(config); - }); + await withSetupLease(deps, () => saveSetupConfigUnderHeldLease(config, baseline)); +} + +/** Config CAS used after the caller has acquired the outer lifecycle lease. */ +async function saveSetupConfigUnderHeldLease(config: GenieConfig, baseline: GenieConfig): Promise { + const current = await loadGenieConfig(); + if (JSON.stringify(current) !== JSON.stringify(baseline)) { + throw new SetupIntegrationError('Genie configuration changed while setup was open; review it and retry setup'); + } + await saveGenieConfig(config); } /** Copy shipped genie.tmux.conf to ~/.genie/tmux.conf if it doesn't exist yet. */ diff --git a/src/genie-commands/uninstall.test.ts b/src/genie-commands/uninstall.test.ts index e2255d0b3..d641ccaa3 100644 --- a/src/genie-commands/uninstall.test.ts +++ b/src/genie-commands/uninstall.test.ts @@ -1356,6 +1356,34 @@ describe('agent-sync managed-asset removal', () => { expect(existsSync(uninstallBatchJournalPath(genieHome))).toBe(true); }); + // Group E carry-forward (Decision 14): the legacy `.install-version` marker + // has NO dedicated uninstall call — the digest-verified wholesale GENIE_HOME + // removal owns it. A dedicated delete inside the plan→execute window would + // poison `genieHomeRemovalDigest` (the marker is a snapshotted removable + // child); before the window it is redundant; after, a no-op. These pin both + // legacy layouts through the REAL batch path instead. + test('uninstall removes a GENIE_HOME carrying a legacy regular-file .install-version marker', () => { + mkdirSync(join(genieHome, 'plugins', 'genie'), { recursive: true }); + writeFileSync(join(genieHome, 'plugins', 'genie', 'payload.txt'), 'delivered\n'); + writeFileSync(join(genieHome, '.install-version'), '4.9.9\n'); + const outcome = withIsolatedHomes(() => performFreshUninstallPlan(genieHome, false)); + expect(outcome.result.failures).toEqual([]); + expect(existsSync(join(genieHome, '.install-version'))).toBe(false); + expect(existsSync(join(genieHome, 'plugins'))).toBe(false); + }); + + test('uninstall unlinks a symlinked .install-version marker without following it to its target', () => { + const target = join(tmp, 'outside-marker-target.txt'); + writeFileSync(target, 'survives uninstall\n'); + mkdirSync(join(genieHome, 'plugins', 'genie'), { recursive: true }); + writeFileSync(join(genieHome, 'plugins', 'genie', 'payload.txt'), 'delivered\n'); + symlinkSync(target, join(genieHome, '.install-version')); + const outcome = withIsolatedHomes(() => performFreshUninstallPlan(genieHome, false)); + expect(outcome.result.failures).toEqual([]); + expect(existsSync(join(genieHome, '.install-version'))).toBe(false); + expect(readFileSync(target, 'utf8')).toBe('survives uninstall\n'); + }); + test('late nested insertion survives the final non-recursive rmdir check without a completion receipt', () => { const payload = join(genieHome, 'plugins', 'genie', 'payload.txt'); const foreignBytes = Buffer.from('late foreign must survive\n'); @@ -2068,6 +2096,7 @@ describe('durable runtime integration allowlist', () => { name: 'genie-later.toml', path: '/tmp/genie-later.toml', ownership: 'managed-clean', + state: 'managed', }, ], }, @@ -2162,6 +2191,8 @@ describe('uninstall ownership and work detection', () => { inventoryPath: join(root, 'inventory.json'), status: 'missing' as const, entries: [], + expectedDeliveredTotal: 7, + reviewerDigest: 'c7008dcaa1e31b46e2bb05ca13afb2e918ee483422c84386a1c8997485bcfea7', }), inspectRuntimeClientAvailability: () => ({ codex: false, diff --git a/src/genie-commands/uninstall.ts b/src/genie-commands/uninstall.ts index 8756776a8..1dbfaed25 100644 --- a/src/genie-commands/uninstall.ts +++ b/src/genie-commands/uninstall.ts @@ -74,6 +74,11 @@ import { } from '../lib/codex-lifecycle-lease.js'; import { contractPath, getGenieDir } from '../lib/genie-config.js'; import { resolveClaudeDir, resolveCodexDir, resolveGenieHome, resolveHermesHome } from '../lib/genie-home.js'; +import { + type HeldOrderedLifecycleLeases, + acquireOrderedLifecycleLeases, + releaseOrderedLifecycleLeases, +} from '../lib/ordered-lifecycle-leases.js'; import { inspectCodexAgentOwnership, inspectRuntimeIntegrationEvidence, @@ -3465,6 +3470,26 @@ function codexLifecycleBusyTrailer(holderKind: string | null): string { }); } +function acquireUninstallLifecycleLeasesOrProject( + genieDir: string, + deps: UninstallDeps, +): HeldOrderedLifecycleLeases | null { + const acquired = acquireOrderedLifecycleLeases( + () => acquireLifecycleLease(genieDir), + () => (deps.acquireCodexLifecycleLease ?? acquireCodexLifecycleLease)('uninstall'), + ); + if (acquired.ok) return acquired; + if (acquired.busy === 'agent-sync') { + throw new Error(`Another Genie lifecycle command is active: ${acquired.detail}`); + } + process.stdout.write(`${codexLifecycleBusyTrailer(acquired.refusal.holderKind)}\n`); + console.error( + `\x1b[33mcodex-lifecycle-busy:\x1b[0m ${acquired.refusal.detail}. No files were removed; retry once it completes.`, + ); + process.exitCode = 2; + return null; +} + export async function uninstallCommand( options: { removeMarketplace?: boolean } = {}, deps: UninstallDeps = {}, @@ -3585,30 +3610,13 @@ export async function uninstallCommand( } // Existing agent-sync safeguard lock (unchanged). Acquired after confirmation. - const lifecycleLease = acquireLifecycleLease(genieDir); - if ('skipped' in lifecycleLease) - throw new Error(`Another Genie lifecycle command is active: ${lifecycleLease.skipped}`); + // Ratified acquisition point: agent-sync first, then the exclusive Codex + // lifecycle lease, after destructive confirmation but before the first removal. + const acquired = acquireUninstallLifecycleLeasesOrProject(genieDir, deps); + if (acquired === null) return; try { - // Ratified acquisition point: the exclusive Codex lifecycle lease, after - // destructive confirmation but before the first removal, so uninstall - // serialises against setup/update/rollback/install on the shared GENIE_HOME. - const acquire = deps.acquireCodexLifecycleLease ?? acquireCodexLifecycleLease; - const codexLease = acquire('uninstall'); - if (!codexLease.ok) { - // Loser: zero mutation. Emit the machine-readable trailer and exit 2. - process.stdout.write(`${codexLifecycleBusyTrailer(codexLease.holderKind)}\n`); - console.error( - `\x1b[33mcodex-lifecycle-busy:\x1b[0m ${codexLease.detail}. No files were removed; retry once it completes.`, - ); - process.exitCode = 2; - return; - } - try { - executeConfirmedUninstall(genieDir, options.removeMarketplace ?? false); - } finally { - codexLease.release(); - } + executeConfirmedUninstall(genieDir, options.removeMarketplace ?? false); } finally { - lifecycleLease.release(); + releaseOrderedLifecycleLeases(acquired.codexLease, acquired.agentSyncLease); } } diff --git a/src/genie-commands/update.ts b/src/genie-commands/update.ts index 3bbd0b397..f6c3218c9 100644 --- a/src/genie-commands/update.ts +++ b/src/genie-commands/update.ts @@ -30,10 +30,20 @@ import { runAgentSync, } from '../lib/agent-sync.js'; import { observeCodexActivation, openCodexActivationStore } from '../lib/codex-activation-executor.js'; +import { parseReleaseVersion, scanPhysicalTree } from '../lib/codex-activation.js'; +import { + type DeliveryEvidenceChannel, + type DeliveryEvidencePlatformId, + type DeliveryEvidenceVerificationDependencies, + type VerifiedDeliveryEvidence, + verifyDownloadedDeliveryEvidence, +} from '../lib/codex-delivery-evidence.js'; import { type HeldLifecycleLease, + type LifecycleLeaseResult, acquireLifecycleLease as acquireCodexLifecycleLease, } from '../lib/codex-lifecycle-lease.js'; +import { snapshotDeliveryReadState } from '../lib/codex-lifecycle-truth.js'; import { contractPath, genieConfigExists, getGenieConfigPath, saveGenieConfig } from '../lib/genie-config.js'; import { type InstallStagingDirectoryGuard, @@ -46,13 +56,17 @@ import { verifyInstallStagingDirectory, } from '../lib/install-promotion.js'; import { inspectPhysicalPath } from '../lib/install-transaction.js'; +import { retireInstallVersionMarker } from '../lib/install-version-marker.js'; +import { + type HeldOrderedLifecycleLeases, + acquireOrderedLifecycleLeases, + releaseOrderedLifecycleLeases, +} from '../lib/ordered-lifecycle-leases.js'; import { type CodexAgentInstallResult, type IntegrationResult, type IntegrationSelection, - installCodexAgents, readIntegrationConsent, - resolveBundleRoot, resolveRuntimeExecutable, } from '../lib/runtime-integrations.js'; import { @@ -70,6 +84,17 @@ import { fingerprintAuxiliaryTree, } from './auxiliary-trees.js'; import { + type CandidateProof, + type DeliveryRepairOutcome, + type DeliveryRepairSeams, + type InstalledProof, + type PinnedManifest, + type RepairPinnedTarget, + localDeliveryMatches, + repairMissingDelivery, +} from './codex-delivery-repair.js'; +import { + CODEX_DELIVERY_INCOMPLETE_TRAILER, CODEX_DELIVERY_RESULT_TRAILER, CODEX_LIFECYCLE_BUSY_TRAILER, CodexLifecycleBusyError, @@ -77,6 +102,7 @@ import { } from './codex-delivery.js'; import { performProtocolSafeRollback } from './codex-rollback.js'; import { cleanupV4 } from './legacy-v4.js'; +import { assertLocalDeliveryRepairEnabled, materializeLocalDeliveryRepair } from './local-delivery-repair.js'; import { type RefreshUpdatePluginsOptions, refreshUpdatePlugins } from './update-integrations.js'; const GENIE_HOME = process.env.GENIE_HOME || join(homedir(), '.genie'); const GENIE_BIN = join(GENIE_HOME, 'bin'); @@ -342,11 +368,15 @@ export type ReleaseChannel = 'stable' | 'homolog' | 'dev'; export interface LatestManifest { schema_version: number; - channel: string; + channel: ReleaseChannel; version: string; released_at: string; tarball_base: string; platforms: string[]; + /** Exact fetched UTF-8 bytes retained for signed delivery-evidence verification. */ + manifestBytes: string; + /** SHA-256 of the exact fetched UTF-8 bytes, before JSON parsing/reconstruction. */ + manifestSha256: string; } /** @@ -515,20 +545,26 @@ export async function fetchLatestManifest( const parsed = JSON.parse(raw) as Partial; if ( typeof parsed.schema_version !== 'number' || + typeof parsed.channel !== 'string' || + parsed.channel !== channel || typeof parsed.version !== 'string' || parseGenieVersion(parsed.version) === null || + typeof parsed.released_at !== 'string' || typeof parsed.tarball_base !== 'string' || - !Array.isArray(parsed.platforms) + !Array.isArray(parsed.platforms) || + !parsed.platforms.every((platform) => typeof platform === 'string') ) { return null; } return { schema_version: parsed.schema_version, - channel: typeof parsed.channel === 'string' ? parsed.channel : channel, + channel: parsed.channel, version: parsed.version, - released_at: typeof parsed.released_at === 'string' ? parsed.released_at : '', + released_at: parsed.released_at, tarball_base: parsed.tarball_base, platforms: parsed.platforms, + manifestBytes: raw, + manifestSha256: createHash('sha256').update(raw).digest('hex'), }; } catch { return null; @@ -551,6 +587,12 @@ interface DownloadAndVerifyOptions { skipAttestation?: boolean; } +export interface DownloadedDeliveryAssets { + tarballPath: string; + descriptorBytes: Buffer; + bundleBytes: Buffer; +} + /** * `gh attestation verify` performs TWO network round-trips on every call: * 1. GitHub Attestations API lookup by tarball digest (~1-2s) @@ -563,7 +605,6 @@ interface DownloadAndVerifyOptions { * out at 4000ms on a healthy connection (Felipe, 2026-05-12). */ const ATTESTATION_VERIFY_TIMEOUT_MS = 60_000; -const COSIGN_VERIFY_TIMEOUT_MS = 30_000; /** * The tarball download moves ~37MB+ per platform and outgrew runCommandSilent's * 4s default the same way the verify steps did: genie update v5.260714.8 timed @@ -572,16 +613,11 @@ const COSIGN_VERIFY_TIMEOUT_MS = 30_000; */ const RELEASE_DOWNLOAD_TIMEOUT_MS = 300_000; -interface SignatureVerificationResult { - method: 'gh-attestation' | 'cosign-bundle'; -} - async function verifyTarballSignature( tarballName: string, tarballPath: string, - bundlePath: string, runner: (cmd: string, args: string[], timeoutMs?: number) => Promise<{ success: boolean; output: string }>, -): Promise { +): Promise { const ghVerifyResult = await runner( 'gh', [ @@ -599,38 +635,9 @@ async function verifyTarballSignature( ], ATTESTATION_VERIFY_TIMEOUT_MS, ); - if (ghVerifyResult.success) return { method: 'gh-attestation' }; - - const failures = [ - `gh attestation verify: ${ghVerifyResult.output.trim() || `failed after ${ATTESTATION_VERIFY_TIMEOUT_MS}ms`}`, - ]; - - if (!existsSync(bundlePath)) { - failures.push(`cosign verify-blob: missing bundle ${bundlePath}`); - throw new Error( - `signature verification failed for ${tarballName}: ${failures.join('; ')}. Install the GitHub CLI (\`gh\`, with \`gh attestation\` support) and retry \`genie update\`; the cosign fallback needs the .bundle release asset, which was not downloaded.`, - ); - } - - const cosignVerifyResult = await runner( - 'cosign', - [ - 'verify-blob', - '--bundle', - bundlePath, - '--certificate-identity-regexp', - EXPECTED_COSIGN_IDENTITY, - '--certificate-oidc-issuer', - EXPECTED_COSIGN_ISSUER, - tarballPath, - ], - COSIGN_VERIFY_TIMEOUT_MS, - ); - if (cosignVerifyResult.success) return { method: 'cosign-bundle' }; - - failures.push(`cosign verify-blob: ${cosignVerifyResult.output.trim() || 'no output'}`); + if (ghVerifyResult.success) return; throw new Error( - `signature verification failed for ${tarballName}: ${failures.join('; ')}. Install the GitHub CLI (\`gh\`, with \`gh attestation\` support) or cosign v2.4.1+ (https://docs.sigstore.dev/cosign/installation) and retry \`genie update\`.`, + `signature verification failed for ${tarballName}: gh attestation verify: ${ghVerifyResult.output.trim() || `failed after ${ATTESTATION_VERIFY_TIMEOUT_MS}ms`}. The reduced cosign verify-blob proof does not validate Genie's required custom predicate/subject; install GitHub CLI with attestation support and retry.`, ); } @@ -657,7 +664,6 @@ export async function downloadAndVerifyTarball( const versionTag = `v${manifest.version}`; const tarballName = `genie-${manifest.version}-${platform}.tar.gz`; const tarballPath = join(destDir, tarballName); - const bundlePath = `${tarballPath}.bundle`; // gh release download retries by name; --pattern lets us pull the tarball // and its sidecar (.bundle, .intoto.jsonl) in one shot via wildcards. @@ -677,6 +683,10 @@ export async function downloadAndVerifyTarball( `${tarballName}.bundle`, '--pattern', `${tarballName}.intoto.jsonl`, + '--pattern', + `${tarballName}.${manifest.channel}.delivery.json`, + '--pattern', + `${tarballName}.${manifest.channel}.delivery.json.sigstore.json`, '--clobber', ], RELEASE_DOWNLOAD_TIMEOUT_MS, @@ -691,12 +701,40 @@ export async function downloadAndVerifyTarball( } if (!opts.skipAttestation) { - await verifyTarballSignature(tarballName, tarballPath, bundlePath, runner); + await verifyTarballSignature(tarballName, tarballPath, runner); } return tarballPath; } +/** + * Download the release tarball and the channel-specific signed evidence pack. + * The legacy tarball attestation remains a pre-execution defense; the descriptor + * bundle is the durable authority setup/doctor can later reverify offline. + */ +export async function downloadAndVerifyDeliveryAssets( + manifest: LatestManifest, + platform: string, + destDir: string, + opts: DownloadAndVerifyOptions = {}, +): Promise { + const tarballPath = await downloadAndVerifyTarball(manifest, platform, destDir, opts); + const descriptorPath = `${tarballPath}.${manifest.channel}.delivery.json`; + const bundlePath = `${descriptorPath}.sigstore.json`; + let descriptorBytes: Buffer; + let bundleBytes: Buffer; + try { + descriptorBytes = readFileSync(descriptorPath); + bundleBytes = readFileSync(bundlePath); + } catch (cause) { + throw new Error(`signed delivery evidence is incomplete for ${tarballPath.split('/').pop()}: ${errMsg(cause)}`); + } + if (descriptorBytes.length === 0 || bundleBytes.length === 0) { + throw new Error(`signed delivery evidence is empty for ${tarballPath.split('/').pop()}`); + } + return { tarballPath, descriptorBytes, bundleBytes }; +} + /** * Extract a tarball into a destination directory. Uses the system `tar` since * macOS bsdtar and GNU tar both accept `-xzf`. Throws on failure. @@ -1481,9 +1519,20 @@ export interface UpdateCommandOptions { /** Companion `--json` flag for `--print-update-capabilities`; the probe output * is always JSON, so this only pins the contract explicit. */ json?: boolean; + /** + * Hidden release-dogfood boundary. The value is one bounded exact-schema JSON + * request naming a local artifact, raw manifest, signed descriptor, and + * Sigstore bundle. It is inert unless GENIE_RELEASE_DOGFOOD is exactly `1`. + */ + publishLocalDelivery?: string; } -export type UpdateExecutionMode = 'normal' | 'rollback' | 'sync-only' | 'post-delivery-converge'; +export type UpdateExecutionMode = + | 'normal' + | 'rollback' + | 'sync-only' + | 'post-delivery-converge' + | 'publish-local-delivery'; function hasPostDeliveryModeConflict(options: UpdateCommandOptions, syncOnlyEnvironment: string | undefined): boolean { return Boolean( @@ -1497,6 +1546,26 @@ function hasPostDeliveryModeConflict(options: UpdateCommandOptions, syncOnlyEnvi options.yes || options.restart === false || options.verify === false || + options.skipMaintenance || + options.publishLocalDelivery !== undefined, + ); +} + +function hasLocalDeliveryModeConflict(options: UpdateCommandOptions, syncOnlyEnvironment: string | undefined): boolean { + return Boolean( + options.rollback || + options.syncOnly || + syncOnlyEnvironment === '1' || + options.postDeliveryConverge || + options.printUpdateCapabilities || + options.json || + options.dev || + options.homolog || + options.next || + options.stable || + options.yes || + options.restart === false || + options.verify === false || options.skipMaintenance, ); } @@ -1506,6 +1575,12 @@ export function resolveUpdateExecutionMode( options: UpdateCommandOptions, syncOnlyEnvironment = process.env.GENIE_UPDATE_SYNC_ONLY, ): UpdateExecutionMode { + if (options.publishLocalDelivery !== undefined) { + if (hasLocalDeliveryModeConflict(options, syncOnlyEnvironment)) { + throw new Error('--publish-local-delivery cannot be combined with another update mode or delivery option'); + } + return 'publish-local-delivery'; + } if (options.postDeliveryConverge) { if (hasPostDeliveryModeConflict(options, syncOnlyEnvironment)) { throw new Error('--post-delivery-converge cannot be combined with another update mode or delivery option'); @@ -1593,17 +1668,10 @@ function resolveUpdatePlatformOrExit(): string { } } -function requireCanonicalInstallOrExit(): void { - try { - ensureCanonicalInstall(); - } catch (err) { - error(err instanceof Error ? err.message : String(err)); - process.exit(1); - } -} - -function acquireRequiredLifecycleLease(): LifecycleLease { - const lease = acquireLifecycleLease(GENIE_HOME); +function acquireRequiredLifecycleLease( + acquire: () => LifecycleLease | { skipped: string } = () => acquireLifecycleLease(GENIE_HOME), +): LifecycleLease { + const lease = acquire(); if ('skipped' in lease) { throw new Error(`Another Genie lifecycle command is active: ${lease.skipped}`); } @@ -1655,7 +1723,7 @@ export function runFreshBinaryPostDeliveryConvergence( // result trailer over inherited stdio; the parent only mirrors the code. if (childExitStatus(cause) === 2) return 'action-required'; throw new Error( - `fresh Genie integration convergence failed: ${errMsg(cause)}. The verified CLI update is installed, but its integrations are not converged. Close all Codex tasks first. Then, from an external terminal, run \`genie update\` (or \`genie setup --codex\`), review \`/hooks\`, and start a new Codex task.`, + `fresh Genie integration convergence failed: ${errMsg(cause)}. The verified CLI update is installed, but the integration named above is not converged. Rerun \`genie update\` from a regular terminal to retry. If Codex activation is pending afterwards, close Codex tasks, run \`genie setup --codex\`, review \`/hooks\`, and start a new Codex task.`, ); } } @@ -1673,15 +1741,31 @@ function childExitStatus(cause: unknown): number | null { * Gate the agent-sync scope for update. R2/A1 (agent-sync must never write * codex product skills into ~/.agents/skills) is structural in `runAgentSync` * itself now — there is no `codex` arm to narrow away from — so this only - * skips agent-sync where it has nothing to do: `none` and `codex` (codex - * converges entirely through the plugin-only integration refresh, never - * through agent-sync). `auto`/`all`/`claude` pass through UNCHANGED so + * skips agent-sync where it has nothing to do: `none` and `codex` (setup owns + * Codex convergence). `auto`/`all`/`claude` pass through UNCHANGED so * `runAgentSync` sees the real selection and converges hermes on `auto`/`all`. */ export function narrowUpdateAgentSyncSelection(selection: IntegrationSelection): IntegrationSelection | null { return selection === 'none' || selection === 'codex' ? null : selection; } +/** + * Update delivery owns no Codex activation surfaces. Claude refresh remains + * update-owned; Hermes continues through agent-sync. Returning null prevents + * even a Codex query when Codex is the only selected integration. + */ +export function narrowUpdatePluginRefreshSelection(selection: IntegrationSelection): 'claude' | null { + return selection === 'none' || selection === 'codex' ? null : 'claude'; +} + +/** Update-owned agent sync cannot cross into setup-owned Codex role convergence. */ +export function runUpdateAgentSync( + selection: IntegrationSelection, + sync: typeof runAgentSyncSafe = runAgentSyncSafe, +): AgentSyncReport | null { + return sync({ strict: true, selection }); +} + export interface LegacySyncOnlyConvergenceOptions { selection: IntegrationSelection; /** Retained for the call signature and structure tests; sync-only never reads a version. */ @@ -1704,7 +1788,7 @@ export function runLegacySyncOnlyConvergence(options: LegacySyncOnlyConvergenceO ( options.sync ?? (() => { - if (agentSyncSelection !== null) runAgentSyncSafe({ strict: true, selection: agentSyncSelection }); + if (agentSyncSelection !== null) runUpdateAgentSync(agentSyncSelection); }) )(); } @@ -1760,15 +1844,62 @@ function runPostDeliveryConvergenceMode(): void { } } -async function runExplicitUpdateMode(mode: Exclude): Promise { +async function runExplicitUpdateMode( + mode: Exclude, +): Promise { const lifecycleLease = acquireRequiredLifecycleLease(); + let terminal: DeferredUpdateTerminal | null = null; try { - if (mode === 'rollback') await runRollback(); + if (mode === 'rollback') terminal = await runRollback(); else if (mode === 'sync-only') runLegacySyncOnlyMode(); else runPostDeliveryConvergenceMode(); } finally { lifecycleLease.release(); } + if (terminal !== null) projectDeferredUpdateTerminal(terminal); +} + +async function dispatchNonNormalUpdateMode(options: UpdateCommandOptions): Promise { + if (options.printUpdateCapabilities) { + if (options.publishLocalDelivery !== undefined) { + throw new Error('--print-update-capabilities cannot be combined with --publish-local-delivery'); + } + printUpdateCapabilities(); + return true; + } + const mode = resolveUpdateExecutionMode(options); + if (mode === 'publish-local-delivery') { + await runLocalDeliveryRepairMode(options.publishLocalDelivery as string); + return true; + } + if (mode !== 'normal') { + await runExplicitUpdateMode(mode); + return true; + } + return false; +} + +async function confirmPlannedDelivery( + options: UpdateCommandOptions, + installedVersion: string, + latestVersion: string, +): Promise { + const decision = decideDowngrade({ + installedVersion, + latestVersion, + explicitChannel: Boolean(options.stable || options.homolog || options.dev || options.next), + }); + const needsDelivery = + normalizeVersion(installedVersion) !== normalizeVersion(latestVersion) && decision.kind !== 'block-downgrade'; + if (!needsDelivery || shouldAutoConfirm(options)) return true; + const proceed = await promptConfirm( + `Update v${normalizeVersion(installedVersion)} → v${normalizeVersion(latestVersion)}?`, + ); + if (proceed) return true; + console.log(); + log('Update declined.'); + console.log(); + return false; } function runFreshConvergenceOrReport(lifecycleLease: LifecycleLease): boolean { @@ -1822,21 +1953,583 @@ function recoverInstallPromotionAndConvergePayload(): void { writeFileSync(join(GENIE_HOME, 'VERSION'), exactVersion); } -export async function updateCommand(options: UpdateCommandOptions = {}): Promise { +// ============================================================================ +// Same-version delivery repair on the already-current path (Group D deliverable 1) +// ============================================================================ + +export type AlreadyCurrentRepairDirective = + | { action: 'proceed-current' } + | { action: 'repaired-current' } + | { action: 'exit-handoff' } + | { action: 'route-upgrade'; manifest: PinnedManifest } + | { action: 'busy'; detail: string } + | { action: 'failed'; detail: string }; + +/** + * Pure mapping of a repair outcome to the already-current directive. A published + * record whose registered generation still trails the target hands off to setup + * (exit 2 with the delivery trailer); a target-current publish reports the repair + * and returns without entering activation or ordinary rerun convergence. A matching + * record retains the ordinary rerun path, a channel advance routes through ordinary + * upgrade, and failed repair remains an explicit non-success terminal outcome. + */ +export function mapAlreadyCurrentRepairOutcome(outcome: DeliveryRepairOutcome): AlreadyCurrentRepairDirective { + if (outcome.kind === 'published') { + return outcome.handoff === 'activation-pending' ? { action: 'exit-handoff' } : { action: 'repaired-current' }; + } + if (outcome.kind === 'channel-advanced') return { action: 'route-upgrade', manifest: outcome.manifest }; + if (outcome.kind === 'failed') return { action: 'failed', detail: `${outcome.stage}: ${outcome.detail}` }; + return { action: 'proceed-current' }; +} + +/** The pinned, immutable repair target — every field is locally known at pin time. */ +function buildRepairPinnedTarget(channel: DeliveryEvidenceChannel, platformId: string): RepairPinnedTarget { + const version = normalizeVersion(VERSION); + return { + channel, + targetVersion: version, + platformTriple: `${process.platform}-${process.arch}`, + platformId, + releaseTag: `v${version}`, + releaseName: `genie-${version}-${platformId}.tar.gz`, + }; +} + +/** The canonical installed payload root under GENIE_HOME, or null when no payload is present. */ +function resolveCanonicalPayloadRoot(genieHome = GENIE_HOME): string | null { + for (const candidate of [genieHome, join(genieHome, 'bin')]) { + if (existsSync(join(candidate, 'plugins', 'genie'))) return candidate; + } + return null; +} + +function hashFileSha256(path: string): string | null { + try { + return createHash('sha256').update(readFileSync(path)).digest('hex'); + } catch { + return null; + } +} + +/** Local canonical observation; null when the installed payload/binary cannot be safely observed. */ +function observeInstalledForRepair( + genieHome = GENIE_HOME, + deliveryEvidenceVerification: DeliveryEvidenceVerificationDependencies = {}, +): InstalledProof | null { + const snapshot = observeCodexActivation({ genieHome, command: null, deliveryEvidenceVerification }); + if (snapshot.canonical.status !== 'ok') return null; + return { + version: snapshot.canonical.version.canonical, + pluginTreeSha256: snapshot.canonical.digest, + binarySha256: snapshot.canonical.installedBinarySha256, + deliveryRoot: snapshot.canonical.deliveryRoot, + }; +} + +/** Re-scan a freshly extracted tarball into a candidate proof; throws on any unsafe/unreadable member. */ +async function proveCandidateFromTarball(tarballPath: string): Promise { + const extractRoot = createPrivateUpdateTempRoot(); + try { + await extractTarball(tarballPath, extractRoot); + chmodSync(extractRoot, 0o700); + return proveExtractedDeliveryCandidate(extractRoot); + } finally { + rmSync(extractRoot, { recursive: true, force: true }); + } +} + +/** Bind the exact extracted VERSION, binary, and physical plugin tree before promotion. */ +function proveExtractedDeliveryCandidate(extractRoot: string): CandidateProof { + const tree = scanPhysicalTree(join(extractRoot, 'plugins', 'genie')); + if (tree.status !== 'ok' || tree.digest === undefined) { + throw new Error(`candidate plugin tree is not a safe physical tree: ${tree.status}`); + } + const version = parseReleaseVersion(readTrimmedFile(join(extractRoot, 'VERSION'))); + if (version === null) throw new Error('candidate VERSION is missing or fails the release grammar'); + const binarySha256 = hashFileSha256(join(extractRoot, 'genie')); + if (binarySha256 === null) throw new Error('candidate binary is unreadable'); + return { version: version.canonical, pluginTreeSha256: tree.digest, binarySha256 }; +} + +function readTrimmedFile(path: string): string | null { + try { + const value = readFileSync(path, 'utf8').trim(); + return value.length > 0 ? value : null; + } catch { + return null; + } +} + +/** + * Attempt the same-version delivery repair before an already-current return. + * The operation is fail-closed: unobservable state, lease contention, or any + * repair failure is reported explicitly instead of masquerading as current. + * Download and private extraction happen under the held Codex lease and are + * cleaned up on every path; only `publishDelivery` mutates durable state. + */ +export async function attemptAlreadyCurrentDeliveryRepair( + channel: DeliveryEvidenceChannel, + platformId: string, + heldLease?: HeldLifecycleLease, + genieHome = GENIE_HOME, + dependencies: AlreadyCurrentRepairAdapterDeps = {}, +): Promise { + const installed = observeInstalledForRepair(genieHome, dependencies.evidenceVerification); + if (installed === null) return { action: 'failed', detail: 'installed payload/binary could not be observed' }; + const pinned = buildRepairPinnedTarget(channel, platformId); + // No-network, no-lease fast path: an already-bound record needs no repair. + const delivery = observeCodexActivation({ + genieHome, + command: null, + deliveryEvidenceVerification: dependencies.evidenceVerification, + }).delivery; + if (localDeliveryMatches(delivery, pinned, installed)) { + return { action: 'proceed-current' }; + } + const acquired = heldLease === undefined ? acquireCodexLifecycleLease('update-delivery', { genieHome }) : null; + if (acquired !== null && !acquired.ok) { + return { + action: 'busy', + detail: `another Genie lifecycle command (${acquired.holderKind ?? 'unknown'}) holds the Codex lease`, + }; + } + const lease = heldLease ?? (acquired as HeldLifecycleLease); + const tempRoots: string[] = []; + try { + const seams = buildAlreadyCurrentRepairSeams(platformId, installed, lease, tempRoots, genieHome, dependencies); + const outcome = await repairMissingDelivery(pinned, seams); + return mapAlreadyCurrentRepairOutcome(outcome); + } catch (cause) { + return { action: 'failed', detail: errMsg(cause) }; + } finally { + if (heldLease === undefined) lease.release(); + for (const root of tempRoots) rmSync(root, { recursive: true, force: true }); + } +} + +export interface AlreadyCurrentRepairAdapterDeps { + fetchManifest?: typeof fetchLatestManifest; + downloadAndVerifyDeliveryAssets?: typeof downloadAndVerifyDeliveryAssets; + evidenceVerification?: DeliveryEvidenceVerificationDependencies; + createTempRoot?: typeof createPrivateUpdateTempRoot; +} + +export interface LocalDeliveryRepairProjection { + exitCode: 0 | 1 | 2; + stdout: string[]; + stderr: string[]; +} + +/** + * Map the production repair directive onto the same stable delivery trailers + * used by ordinary update. The local dogfood surface never routes a moving + * channel into the network-backed upgrade path. + */ +export function projectLocalDeliveryRepairDirective( + directive: AlreadyCurrentRepairDirective, + version = normalizeVersion(VERSION), +): LocalDeliveryRepairProjection { + if (directive.action === 'busy') { + return { + exitCode: 2, + stdout: [CODEX_LIFECYCLE_BUSY_TRAILER], + stderr: [`Local Codex delivery publication is busy: ${directive.detail}`], + }; + } + if (directive.action === 'failed') { + return { + exitCode: 1, + stdout: [CODEX_DELIVERY_INCOMPLETE_TRAILER], + stderr: [`Local Codex delivery publication failed: ${directive.detail}`], + }; + } + if (directive.action === 'route-upgrade') { + return { + exitCode: 1, + stdout: [CODEX_DELIVERY_INCOMPLETE_TRAILER], + stderr: [ + `Local Codex delivery publication refused a non-current manifest (${version} → ${directive.manifest.version})`, + ], + }; + } + if (directive.action === 'exit-handoff') { + return { + exitCode: 2, + stdout: ['Authenticated Codex delivery published; activation is pending.', CODEX_DELIVERY_RESULT_TRAILER], + stderr: [], + }; + } + if (directive.action === 'repaired-current') { + return { + exitCode: 0, + stdout: [`Authenticated Codex delivery published for current v${version}.`], + stderr: [], + }; + } + return { + exitCode: 0, + stdout: [`Authenticated Codex delivery already matches current v${version}.`], + stderr: [], + }; +} + +function emitLocalDeliveryRepairProjection(projection: LocalDeliveryRepairProjection): void { + for (const line of projection.stdout) log(line); + for (const line of projection.stderr) process.stderr.write(`${line}\n`); + process.exitCode = projection.exitCode; +} + +/** + * Offline release-dogfood entrypoint. External paths are snapshotted first, + * the Codex lifecycle lease is then held across the complete production + * `repairMissingDelivery` call, and no fetch/download implementation is + * reachable: both adapters return only the snapshotted bytes. + */ +async function runLocalDeliveryRepairMode(rawRequest: string): Promise { + let snapshotRoot: string | null = null; + let lease: HeldLifecycleLease | null = null; + try { + assertLocalDeliveryRepairEnabled(process.env.GENIE_RELEASE_DOGFOOD); + const platformId = resolvePlatformId(); + snapshotRoot = createPrivateUpdateTempRoot(); + const local = materializeLocalDeliveryRepair(rawRequest, snapshotRoot, platformId, normalizeVersion(VERSION)); + const acquired = acquireCodexLifecycleLease('update-delivery', { genieHome: GENIE_HOME }); + if (!acquired.ok) { + emitLocalDeliveryRepairProjection({ + exitCode: 2, + stdout: [CODEX_LIFECYCLE_BUSY_TRAILER], + stderr: [`Local Codex delivery publication is busy: ${acquired.detail}`], + }); + return; + } + lease = acquired; + const directive = await attemptAlreadyCurrentDeliveryRepair( + local.manifest.channel, + local.platformId, + lease, + GENIE_HOME, + { + fetchManifest: async (channel) => { + if (channel !== local.manifest.channel) { + throw new Error(`local manifest channel ${local.manifest.channel} differs from requested ${channel}`); + } + return local.manifest; + }, + downloadAndVerifyDeliveryAssets: async (manifest, requestedPlatform) => { + if ( + requestedPlatform !== local.platformId || + manifest.manifestSha256 !== local.manifest.manifestSha256 || + manifest.manifestBytes !== local.manifest.manifestBytes + ) { + throw new Error('local delivery adapter received a target other than the snapshotted request'); + } + return { + tarballPath: local.artifactPath, + descriptorBytes: local.descriptorBytes, + bundleBytes: local.bundleBytes, + }; + }, + }, + ); + emitLocalDeliveryRepairProjection(projectLocalDeliveryRepairDirective(directive)); + } catch (cause) { + emitLocalDeliveryRepairProjection(projectLocalDeliveryRepairDirective({ action: 'failed', detail: errMsg(cause) })); + } finally { + lease?.release(); + if (snapshotRoot !== null) rmSync(snapshotRoot, { recursive: true, force: true }); + } +} + +/** Build the real repair seams; download roots are tracked in `tempRoots` for cleanup by the caller. */ +export function buildAlreadyCurrentRepairSeams( + platformId: string, + installed: InstalledProof, + lease: HeldLifecycleLease, + tempRoots: string[], + genieHome = GENIE_HOME, + deps: AlreadyCurrentRepairAdapterDeps = {}, +): DeliveryRepairSeams { + const fetchManifest = deps.fetchManifest ?? fetchLatestManifest; + const downloadAssets = deps.downloadAndVerifyDeliveryAssets ?? downloadAndVerifyDeliveryAssets; + const createTempRoot = deps.createTempRoot ?? createPrivateUpdateTempRoot; + return { + readDeliveryFact: () => + observeCodexActivation({ + genieHome, + command: null, + deliveryEvidenceVerification: deps.evidenceVerification, + }).delivery, + observeInstalled: () => installed, + fetchManifest: async (channel) => { + const manifest = await fetchManifest(channel as ReleaseChannel); + return manifest === null ? null : manifest; + }, + downloadAndVerify: async (target, pinnedManifest) => { + const destDir = createTempRoot(); + tempRoots.push(destDir); + if (target.platformId !== platformId) { + throw new Error(`repair target platform ${target.platformId} differs from adapter ${platformId}`); + } + const expectedName = `genie-${normalizeVersion(pinnedManifest.version)}-${platformId}.tar.gz`; + if (target.releaseTag !== `v${normalizeVersion(pinnedManifest.version)}` || target.releaseName !== expectedName) { + throw new Error('repair release tag/name differ from the immutable pinned manifest asset'); + } + const downloaded = await downloadAssets(pinnedManifest, platformId, destDir); + if (downloaded.tarballPath.split('/').pop() !== target.releaseName) { + throw new Error(`downloaded asset name differs from pinned ${target.releaseName}`); + } + return downloaded; + }, + hashArtifact: (path) => { + const digest = hashFileSha256(path); + if (digest === null) throw new Error('downloaded artifact is unreadable'); + return digest; + }, + proveCandidate: (path) => proveCandidateFromTarball(path), + verifyEvidence: (input) => verifyDownloadedDeliveryEvidence(input, deps.evidenceVerification), + reobserve: () => reobserveForRepair(genieHome), + store: openCodexActivationStore({ genieHome, deliveryEvidenceVerification: deps.evidenceVerification }), + lease, + }; +} + +/** Re-observe the installed generation + canonical digest immediately before publication. */ +function reobserveForRepair(genieHome = GENIE_HOME): { + installedGeneration: string | null; + canonicalVersion: string; + canonicalPayloadSha256: string; + installedBinarySha256: string; + deliveryRoot: string; +} | null { + let command: string | null = null; + try { + command = resolveRuntimeExecutable('codex', process.cwd()); + } catch { + command = null; + } + const snapshot = observeCodexActivation({ genieHome, command }); + if (snapshot.canonical.status !== 'ok') return null; + const registration = snapshot.query.status === 'ok' ? snapshot.query.registration : { present: false as const }; + const installedGeneration = registration.present && registration.version ? registration.version.canonical : null; + return { + installedGeneration, + canonicalVersion: snapshot.canonical.version.canonical, + canonicalPayloadSha256: snapshot.canonical.digest, + installedBinarySha256: snapshot.canonical.installedBinarySha256, + deliveryRoot: snapshot.canonical.deliveryRoot, + }; +} + +/** Best-effort retirement of the legacy `.install-version` marker after a proven-successful convergence. */ +function retireLegacyInstallMarkerSafe(): void { + try { + retireInstallVersionMarker(GENIE_HOME); + } catch { + // Orphan-metadata cleanup must never fail a completed update. + } +} + +/** + * The already-current terminal path. Group D deliverable 1: before returning + * "Already up to date", repair a missing authenticated delivery record for the + * installed target exactly once. A matching record retains non-Codex rerun + * convergence, a channel advance enters ordinary upgrade, and repair failure + * or contention exits nonzero. An old-parent repair hands off to setup (exit 2). + */ +export interface AlreadyCurrentUpdateDependencies { + attemptRepair?: typeof attemptAlreadyCurrentDeliveryRepair; + runConvergence?: typeof runTrackedManualUpdateConvergence; + retireLegacyMarker?: typeof retireLegacyInstallMarkerSafe; +} + +export async function handleAlreadyCurrentUpdate( + channel: ReleaseChannel, + platform: string, + installedVersion: string, + latestVersion: string | null | undefined, + dependencies: AlreadyCurrentUpdateDependencies = {}, + heldLease?: HeldLifecycleLease, +): Promise { + const repair = await (dependencies.attemptRepair ?? attemptAlreadyCurrentDeliveryRepair)( + channel, + platform, + heldLease, + ); + if (repair.action === 'route-upgrade') { + log(`Channel advanced while repairing delivery (${installedVersion} → ${repair.manifest.version}); upgrading.`); + return repair.manifest; + } + if (repair.action === 'busy') { + error(`Codex delivery repair is busy: ${repair.detail}`); + log(CODEX_LIFECYCLE_BUSY_TRAILER); + process.exitCode = 2; + return null; + } + if (repair.action === 'failed') { + error(`Codex delivery repair failed: ${repair.detail}`); + log(CODEX_DELIVERY_INCOMPLETE_TRAILER); + process.exitCode = 1; + return null; + } + if (repair.action === 'repaired-current') { + (dependencies.retireLegacyMarker ?? retireLegacyInstallMarkerSafe)(); + log('Repaired the missing Codex delivery record for the installed generation.'); + return null; + } + if (repair.action === 'exit-handoff') { + // Publication owns only the delivery fact. Activation remains an explicit + // setup authority, so this terminal path must not enter convergence. + log( + 'Codex plugin activation is pending: retire Codex tasks, run `genie setup --codex`, review `/hooks`, then start a new Codex task.', + ); + (dependencies.retireLegacyMarker ?? retireLegacyInstallMarkerSafe)(); + log(CODEX_DELIVERY_RESULT_TRAILER); + process.exitCode = 2; + return null; + } + success(`Already up to date (v${normalizeVersion(installedVersion)}, channel ${channel})`); + (dependencies.runConvergence ?? runTrackedManualUpdateConvergence)( + latestVersion ?? normalizeVersion(installedVersion), + ); + (dependencies.retireLegacyMarker ?? retireLegacyInstallMarkerSafe)(); + console.log(); + return null; +} + +/** + * Normal-delivery terminal boundary. Publication failure happens after verified + * promotion, so it is handled as an explicit incomplete delivery and no + * success-only cleanup, convergence, verification, or marker retirement runs. + */ +export async function runNormalUpdatePublicationBoundary( + deliver: () => Promise, + afterPublished: () => Promise, +): Promise { + try { + await deliver(); + } catch (cause) { + if (!(cause instanceof CodexDeliveryPublicationError)) throw cause; + error(`Update delivered the verified binary, but ${cause.message}`); + log(CODEX_DELIVERY_INCOMPLETE_TRAILER); + process.exitCode = 1; + return false; + } + return afterPublished(); +} + +export interface UpdateCommandDependencies { + /** Narrow same-process test seam; production always fetches the selected channel manifest. */ + fetchManifest?: typeof fetchLatestManifest; + /** Narrow same-process test seam; production always downloads release assets through GitHub. */ + downloadDeliveryAssets?: typeof downloadAndVerifyDeliveryAssets; + /** Cryptographic verifier seam only; every descriptor/candidate binding remains live. */ + evidenceVerification?: DeliveryEvidenceVerificationDependencies; + /** Same-version terminal seam; production keeps repair, convergence, and marker authority unchanged. */ + alreadyCurrent?: AlreadyCurrentUpdateDependencies; + /** Narrow selected-target delivery seam for command-boundary tests. */ + deliverSelectedManifest?: (manifest: LatestManifest, platform: string) => Promise; + /** Narrow success-finalizer seam paired with `deliverSelectedManifest`. */ + finalizeSelectedDelivery?: () => Promise; + /** Read-only installed-version seam for command-boundary tests. */ + readInstalledVersion?: typeof resolveInstalledVersion; + /** Read-only platform seam for command-boundary tests. */ + resolvePlatform?: () => string; + /** Recovery seam; production replays both durable delivery transactions under both locks. */ + recoverPendingState?: () => void; + /** Channel persistence seam; production writes only after both locks are held. */ + persistSelectedChannel?: (channel: ReleaseChannel) => Promise; + /** Canonical-install guard seam for command-boundary tests. */ + requireCanonicalInstall?: () => void; + /** Outer delivery lock seam; production acquires the shared agent-sync lifecycle lease. */ + acquireLease?: () => LifecycleLease | { skipped: string }; + /** Inner delivery lock seam; production acquires one Codex lease for the whole mutation phase. */ + acquireCodexLease?: () => LifecycleLeaseResult; +} + +type UpdateTerminalExitCode = 1 | 2; + +/** + * A terminal CLI projection discovered while lifecycle leases are held. + * Throwing/capturing this value unwinds both leases before stderr, trailer, + * and process status are projected at the command boundary. + */ +class DeferredUpdateTerminal extends Error { + constructor( + readonly exitCode: UpdateTerminalExitCode, + message: string, + readonly trailer?: string, + readonly trailingBlankLine = false, + ) { + super(message); + this.name = 'DeferredUpdateTerminal'; + } +} + +function projectDeferredUpdateTerminal(terminal: DeferredUpdateTerminal): void { + error(terminal.message); + if (terminal.trailer !== undefined) log(terminal.trailer); + if (terminal.trailingBlankLine) console.log(); + process.exitCode = terminal.exitCode; +} + +function acquireUpdateLifecycleLeasesOrProject( + dependencies: UpdateCommandDependencies, +): HeldOrderedLifecycleLeases | null { + const acquired = acquireOrderedLifecycleLeases( + dependencies.acquireLease ?? (() => acquireLifecycleLease(GENIE_HOME)), + dependencies.acquireCodexLease ?? (() => acquireCodexLifecycleLease('update-delivery', { genieHome: GENIE_HOME })), + ); + if (acquired.ok) return acquired; + if (acquired.busy === 'agent-sync') { + throw new Error(`Another Genie lifecycle command is active: ${acquired.detail}`); + } + projectDeferredUpdateTerminal( + new DeferredUpdateTerminal( + 2, + new CodexLifecycleBusyError(acquired.refusal.holderKind).message, + CODEX_LIFECYCLE_BUSY_TRAILER, + ), + ); + return null; +} + +function assertInitialUpdateLifecycleAuthority(codexLease: HeldLifecycleLease): void { + try { + codexLease.assertOperation(codexLease.operationId); + } catch (err) { + throw new DeferredUpdateTerminal(1, `Update failed: ${errMsg(err)}`, CODEX_DELIVERY_INCOMPLETE_TRAILER); + } +} + +function captureDeferredUpdateTerminal(error: unknown): DeferredUpdateTerminal { + if (error instanceof DeferredUpdateTerminal) return error; + throw error; +} + +function recoverPendingUpdateStateOrThrow(recover?: () => void): void { + try { + ( + recover ?? + (() => { + recoverInstallPromotionAndConvergePayload(); + resumePendingDelivery(); + }) + )(); + } catch (err) { + throw new DeferredUpdateTerminal(1, `Pending update recovery failed: ${errMsg(err)}`); + } +} + +export async function updateCommand( + options: UpdateCommandOptions = {}, + dependencies: UpdateCommandDependencies = {}, +): Promise { // The read-only capability probe is answered before mode resolution and any // mutation: it self-hashes this binary, prints exactly one JSON object, and // exits 0. A pre-contract binary never reaches here — it rejects the unknown // `--print-update-capabilities` flag at commander parse, which is precisely // the signal the rollback capability floor relies on. - if (options.printUpdateCapabilities) { - printUpdateCapabilities(); - return; - } - const mode = resolveUpdateExecutionMode(options); - if (mode !== 'normal') { - await runExplicitUpdateMode(mode); - return; - } + if (await dispatchNonNormalUpdateMode(options)) return; console.log(); console.log(`${colorize('\x1b[1m', '\x1b[0m', '🧞 Genie CLI Update')}`); @@ -1850,101 +2543,111 @@ export async function updateCommand(options: UpdateCommandOptions = {}): Promise // Planning is read-only and deliberately happens before lifecycle lease // acquisition. Interactive users can consider the prompt without blocking // install/setup/uninstall in another process. - const manifest = await fetchLatestManifest(channel); - const plannedInstalledVersion = resolveInstalledVersion(); - const platform = resolveUpdatePlatformOrExit(); - const latestVersion = announceUpdatePlanOrExit(channel, platform, plannedInstalledVersion, manifest?.version ?? null); + let manifest = await (dependencies.fetchManifest ?? fetchLatestManifest)(channel); + const plannedInstalledVersion = (dependencies.readInstalledVersion ?? resolveInstalledVersion)(); + const platform = (dependencies.resolvePlatform ?? resolveUpdatePlatformOrExit)(); + let latestVersion = announceUpdatePlanOrExit(channel, platform, plannedInstalledVersion, manifest?.version ?? null); console.log(); - const plannedDecision = decideDowngrade({ - installedVersion: plannedInstalledVersion, - latestVersion, - explicitChannel: Boolean(options.stable || options.homolog || options.dev || options.next), - }); - const plannedNeedsDelivery = - normalizeVersion(plannedInstalledVersion) !== normalizeVersion(latestVersion as string) && - plannedDecision.kind !== 'block-downgrade'; - if (plannedNeedsDelivery && !shouldAutoConfirm(options)) { - const proceedQuestion = `Update v${normalizeVersion(plannedInstalledVersion)} → v${normalizeVersion(latestVersion as string)}?`; - const proceed = await promptConfirm(proceedQuestion); - if (!proceed) { - console.log(); - log('Update declined.'); - console.log(); - return; - } - } + if (!(await confirmPlannedDelivery(options, plannedInstalledVersion, latestVersion))) return; - const lifecycleLease = acquireRequiredLifecycleLease(); + const acquired = acquireUpdateLifecycleLeasesOrProject(dependencies); + if (acquired === null) return; + const { agentSyncLease: lifecycleLease, codexLease } = acquired; + let terminal: DeferredUpdateTerminal | null = null; try { - // Revalidate durable recovery and the installed binary immediately after - // acquiring the lease, before the first mutation owned by this plan. try { - recoverInstallPromotionAndConvergePayload(); - resumePendingDelivery(); - } catch (err) { - error(`Pending update recovery failed: ${errMsg(err)}`); - process.exitCode = 1; - return; - } - const installedVersion = resolveInstalledVersion(); - - // Channel persistence is now inside the mutation lease and follows local - // state revalidation; the prompt itself never owns the lease. - await persistChannel(channel); + // The first fencing observation belongs inside the reverse-unwind + // boundary. A stale acquisition therefore releases both leases before + // projecting failure and cannot reach recovery or any later mutation. + assertInitialUpdateLifecycleAuthority(codexLease); + + // Revalidate durable recovery and the installed binary immediately after + // acquiring both locks, before the first mutation owned by this plan. + recoverPendingUpdateStateOrThrow(dependencies.recoverPendingState); + const installedVersion = (dependencies.readInstalledVersion ?? resolveInstalledVersion)(); + + // Channel persistence is now inside the mutation lease and follows local + // state revalidation; the prompt itself never owns the lease. + await (dependencies.persistSelectedChannel ?? persistChannel)(channel); + + if (shortCircuitIfCurrent(installedVersion, latestVersion)) { + const advancedManifest = await handleAlreadyCurrentUpdate( + channel, + platform, + installedVersion, + latestVersion, + dependencies.alreadyCurrent, + codexLease, + ); + if (advancedManifest === null) return; + manifest = advancedManifest; + latestVersion = advancedManifest.version; + } - if (shortCircuitIfCurrent(installedVersion, latestVersion)) { - success(`Already up to date (v${normalizeVersion(installedVersion)}, channel ${channel})`); - runTrackedManualUpdateConvergence(latestVersion ?? normalizeVersion(installedVersion)); - console.log(); - return; - } + if (applyDowngradeGuard(installedVersion, latestVersion, channel, options)) { + runTrackedManualUpdateConvergence(normalizeVersion(installedVersion)); + console.log(); + return; + } - if (applyDowngradeGuard(installedVersion, latestVersion, channel, options)) { - runTrackedManualUpdateConvergence(normalizeVersion(installedVersion)); - console.log(); - return; - } + // A concurrent lifecycle operation may have moved or replaced the live + // binary while this process was prompting. Re-check canonical ownership + // under the lease immediately before delivery. Refusals are deferred so + // both leases unwind before process status is projected. + try { + (dependencies.requireCanonicalInstall ?? ensureCanonicalInstall)(); + } catch (err) { + throw new DeferredUpdateTerminal(1, errMsg(err)); + } - // A concurrent lifecycle operation may have moved or replaced the live - // binary while this process was prompting. Re-check canonical ownership - // under the lease immediately before delivery. - requireCanonicalInstallOrExit(); - - const diagnosticsCtx: UpdateDiagnosticsContext = { - channel, - manifest, - platform, - latestVersion, - cliVersion: VERSION, - tarballPath: null, - attestationVerified: false, - previousBackup: null, - }; - const resolvedManifest = manifest as LatestManifest; + const diagnosticsCtx: UpdateDiagnosticsContext = { + channel, + manifest, + platform, + latestVersion, + cliVersion: VERSION, + tarballPath: null, + attestationVerified: false, + previousBackup: null, + }; + const resolvedManifest = manifest as LatestManifest; - try { - await runDelivery(resolvedManifest, platform, diagnosticsCtx); - } catch (err) { - if (err instanceof CodexLifecycleBusyError) { - // Loser semantics (deliverable 9): refused before any swap with zero - // mutation. Exit 2 codex-lifecycle-busy with the busy trailer, not a - // generic failure. - error(err.message); - log(CODEX_LIFECYCLE_BUSY_TRAILER); - process.exit(2); + try { + codexLease.assertOperation(codexLease.operationId); + const complete = await runNormalUpdatePublicationBoundary( + () => + dependencies.deliverSelectedManifest?.(resolvedManifest, platform) ?? + runDelivery(resolvedManifest, platform, diagnosticsCtx, codexLease, dependencies), + dependencies.finalizeSelectedDelivery ?? + (async () => { + runV4CleanupSafe(); + if (!runFreshConvergenceOrReport(lifecycleLease)) return false; + await runPostUpdateVerifySafe({ ...options, noRestart, noVerify }, diagnosticsCtx); + // Convergence succeeded: retire the orphaned legacy `.install-version` marker + // (Decision 14). Canonical VERSION is the sole authority; this only runs after + // a proven-successful delivery + convergence, so a failed run preserves it. + retireLegacyInstallMarkerSafe(); + return true; + }), + ); + if (!complete) return; + } catch (err) { + if (err instanceof CodexLifecycleBusyError) { + // Loser semantics (deliverable 9): refused before any swap with zero + // mutation. Exit 2 codex-lifecycle-busy with the busy trailer, not a + // generic failure. + throw new DeferredUpdateTerminal(2, err.message, CODEX_LIFECYCLE_BUSY_TRAILER); + } + throw new DeferredUpdateTerminal(1, `Update failed: ${errMsg(err)}`); } - const msg = err instanceof Error ? err.message : String(err); - error(`Update failed: ${msg}`); - process.exit(1); + } catch (err) { + terminal = captureDeferredUpdateTerminal(err); } - - runV4CleanupSafe(); - if (!runFreshConvergenceOrReport(lifecycleLease)) return; - await runPostUpdateVerifySafe({ ...options, noRestart, noVerify }, diagnosticsCtx); } finally { - lifecycleLease.release(); + releaseOrderedLifecycleLeases(codexLease, lifecycleLease); } + if (terminal !== null) projectDeferredUpdateTerminal(terminal); } /** @@ -1963,15 +2666,14 @@ export function runV4CleanupSafe(runner: typeof cleanupV4 = cleanupV4): void { } /** - * Agent-sync phase — converge the genie skill set + the /council stamp into - * every detected coding agent (claude/codex/hermes) from the canonical source - * root. This is the ONE printer: the sync-only fast path, the already-current - * short-circuit, and `genie install` all funnel through here. + * Agent-sync phase — converge the selected Claude/Hermes surfaces from the + * canonical source root. Codex product skills are plugin-owned, and managed + * Codex roles run only through an explicit setup-authorized callback. * * Non-fatal by default — an engine failure becomes a single advisory line. - * The `~/.genie/.last-agent-sync` throttle marker is refreshed only after - * agent skills and Codex role agents both converge without any reported - * failure; partial work therefore remains immediately retryable. + * The `~/.genie/.last-agent-sync` throttle marker is refreshed only after all + * surfaces authorized for this invocation converge without a reported failure; + * partial work therefore remains immediately retryable. * * `sync` / `log` / `markerPath` / `now` are injection seams (mirrors * runV4CleanupSafe) so the wiring is unit-testable without @@ -1987,9 +2689,8 @@ export interface RunAgentSyncSafeOptions { /** Injectable clock for the marker timestamp. */ now?: () => Date; /** - * Test seam: replaces the codex role-agent TOML refresh. Contract of the - * default ({@link refreshCodexRoleAgents}): returns the install result, or - * null when no bundle root resolves / it carries no codex-agents staging dir. + * Explicit setup-only seam for managed Codex role convergence. Ordinary + * install/update callers omit it, so role mutation is structurally absent. */ codexRefresh?: () => CodexAgentInstallResult | null; /** Explicit lifecycle commands fail instead of converting convergence errors to warnings. */ @@ -2003,7 +2704,7 @@ export function runAgentSyncSafe(opts: RunAgentSyncSafeOptions = {}): AgentSyncR try { const report = (opts.sync ?? runAgentSync)({ selection: opts.selection }); for (const line of formatAgentSyncSummary(report)) emit(line); - const roleError = refreshCodexIntegrationsSafe(report, emit, opts.codexRefresh); + const roleError = opts.codexRefresh ? refreshCodexIntegrationsSafe(report, emit, opts.codexRefresh) : null; const failures = report.agents.flatMap((agent) => agent.failures ?? []); if (report.source.pluginRoot === null) failures.push('no Genie plugin source was available'); if (report.skipped) failures.push(report.skipped); @@ -2022,26 +2723,19 @@ export function runAgentSyncSafe(opts: RunAgentSyncSafeOptions = {}): AgentSyncR } /** - * Codex convergence beyond skills: the role-agent TOMLs under - * `~/.codex/agents/` were install/setup-time copies only, so `genie update` - * left them drifting from the shipped bundle — contradicting the convergence - * contract ("update converges every detected agent"). Runs only when the sync - * engine actually detected codex (and did not skip for the lock), and only - * when the resolved bundle root carries the `plugins/genie/codex-agents` - * staging dir. Plugin REGISTRATION (`codex plugin marketplace add` / `plugin - * add`) is deliberately NOT refreshed here: it spawns the codex CLI, hits the - * marketplace, and must replay the preserved-disabled dance — neither cheap - * nor guaranteed idempotent inside the 45s-budgeted SessionStart delegation. - * TOMLs only; registration stays install/setup-owned. Non-fatal by contract. + * Explicit managed-role convergence seam. Delivery/update callers never + * provide it; setup may do so only after its matching-record and authorization + * gates. The sync engine must have observed Codex, and lock-skipped runs never + * invoke the callback. Failures remain retryable and strict-visible. */ function refreshCodexIntegrationsSafe( report: AgentSyncReport, emit: (line: string) => void, - refresh?: () => CodexAgentInstallResult | null, + refresh: () => CodexAgentInstallResult | null, ): string | null { if (report.skipped || !report.agents.some((agent) => agent.agent === 'codex' && agent.detected)) return null; try { - const result = (refresh ?? refreshCodexRoleAgents)(); + const result = refresh(); if (result === null) return 'Codex role-agent bundle is unavailable'; const parts = [`${result.installed} role-agent TOMLs refreshed`]; if (result.backedUp.length > 0) parts.push(`${result.backedUp.length} user-tuned backed up`); @@ -2055,13 +2749,6 @@ function refreshCodexIntegrationsSafe( } } -/** Default codex TOML refresh: no-op (null) when no bundle root resolves or it lacks codex-agents. */ -function refreshCodexRoleAgents(): CodexAgentInstallResult | null { - const bundleRoot = resolveBundleRoot(); - if (bundleRoot === null || !existsSync(join(bundleRoot, 'plugins', 'genie', 'codex-agents'))) return null; - return installCodexAgents(bundleRoot); -} - /** Compact per-agent summary: detected + counts by action + advisories. */ function formatAgentSyncSummary(report: AgentSyncReport): string[] { if (report.source.pluginRoot === null) { @@ -2107,7 +2794,7 @@ export interface ManualUpdateConvergenceOptions { runSync?: () => void; refreshPlugins?: (options: RefreshUpdatePluginsOptions) => IntegrationResult[]; log?: (line: string) => void; - /** Persisted operator scope; defaults to the install-time consent record. */ + /** Persisted operator scope; explicit Codex authority is written only by setup. */ selection?: IntegrationSelection; } @@ -2119,33 +2806,28 @@ export function runManualUpdateConvergence(options: ManualUpdateConvergenceOptio const emit = options.log ?? log; const selection = options.selection ?? readIntegrationConsent(GENIE_HOME); if (selection === 'none') return { integrations: [] }; - // R2/A1/A13: `runAgentSync` has no codex arm, so it structurally never - // writes ~/.agents/skills — a full update passes the real selection through - // (converging claude + hermes on auto/all) and only gates the codex-only / - // none cases, where agent-sync has nothing to do. Codex product skills stay - // plugin-only, and codex role agents + fallback retirement are refreshed by - // refreshUpdatePlugins → convergeCodexPluginOnly below. + // `runAgentSync` has no codex arm, so it structurally never writes Codex + // product skills or roles. A full update passes the real non-Codex selection + // through so auto/all still converge Claude + Hermes. const agentSyncSelection = narrowUpdateAgentSyncSelection(selection); - ( - options.runSync ?? - (() => { - if (agentSyncSelection !== null) runAgentSyncSafe({ strict: true, selection: agentSyncSelection }); - }) - )(); - const integrations = (options.refreshPlugins ?? refreshUpdatePlugins)({ - bundleRoot: options.bundleRoot ?? GENIE_HOME, - expectedVersion: options.expectedVersion, - selection, - }); + if (agentSyncSelection !== null) { + (options.runSync ?? (() => runUpdateAgentSync(agentSyncSelection)))(); + } + const pluginSelection = narrowUpdatePluginRefreshSelection(selection); + const integrations = + pluginSelection === null + ? [] + : (options.refreshPlugins ?? refreshUpdatePlugins)({ + bundleRoot: options.bundleRoot ?? GENIE_HOME, + expectedVersion: options.expectedVersion, + selection: pluginSelection, + }).filter((result) => result.runtime !== 'codex'); for (const result of integrations) { const disabled = result.preservedDisabled ? '; disabled state preserved' : ''; emit( `integration refresh: ${result.runtime} — ${result.ok ? result.detail : `FAILED: ${result.detail}`}${disabled}`, ); } - if (integrations.some((result) => result.runtime === 'codex' && result.ok && result.hookReviewRequired)) { - emit('Close all Codex tasks first. Then review refreshed Genie hooks with /hooks and start a new Codex task.'); - } return { integrations }; } @@ -2227,19 +2909,6 @@ export function createPrivateUpdateTempRoot(baseDir = tmpdir()): string { * to keep the command body readable: download → verify → extract → swap → * sync aux content → clean staging. */ -/** - * Acquire the Codex lifecycle lease for a delivery, or refuse before any binary - * swap. A busy lease means another lifecycle command (setup/rollback/uninstall/ - * another update) holds it: zero mutation, and the caller must not proceed. (The - * exit-2 `codex-lifecycle-busy` loser projection is finalized with the two- - * process race work; here we fail closed before the swap.) - */ -function acquireCodexDeliveryLeaseOrRefuse(): HeldLifecycleLease { - const lease = acquireCodexLifecycleLease('update-delivery', { genieHome: GENIE_HOME }); - if (!lease.ok) throw new CodexLifecycleBusyError(lease.holderKind); - return lease; -} - /** * Parent-side attestation (deliverable 4/5): after the binary is delivered and * the payload is synced into GENIE_HOME, publish the exact delivery facts through @@ -2249,33 +2918,67 @@ function acquireCodexDeliveryLeaseOrRefuse(): HeldLifecycleLease { * When the delivery is pending (N ≠ T) and the prior binary is a protocol-1+ * backup, also publish its digest-bound rollback capability sidecar. A pre- * contract backup gets NO sidecar, which makes the rollback capability floor - * refuse to restore it. Best-effort: a codex-CLI-absent or payload-unreadable - * environment simply attests nothing (the child gate still defers activation). + * refuse to restore it. Once Codex is in scope, inability to observe or publish + * the complete binding is terminal and must surface as delivery-incomplete. */ -function publishCodexDeliveryFacts(channel: string, previousBackup: string | null, lease: HeldLifecycleLease): void { +type DeliveryPublicationCompletion = + | { kind: 'not-in-scope' } + | { kind: 'matching' | 'published'; deliveryId: string } + | { kind: 'incomplete'; detail: string }; + +export class CodexDeliveryPublicationError extends Error { + constructor(detail: string) { + super(`authenticated Codex delivery publication incomplete: ${detail}`); + this.name = 'CodexDeliveryPublicationError'; + } +} + +function publishCodexDeliveryFacts( + previousBackup: string | null, + lease: HeldLifecycleLease, + evidence: VerifiedDeliveryEvidence, + deliveryRoot: string, +): DeliveryPublicationCompletion { + const selection = readIntegrationConsent(GENIE_HOME); + if (selection === 'none' || selection === 'claude') return { kind: 'not-in-scope' }; let command: string | null = null; try { command = resolveRuntimeExecutable('codex', process.cwd()); } catch { - return; + command = null; + } + if (command === null) { + return selection === 'auto' + ? { kind: 'not-in-scope' } + : { kind: 'incomplete', detail: 'Codex was explicitly selected but its executable is unavailable' }; } - if (command === null) return; const snapshot = observeCodexActivation({ genieHome: GENIE_HOME, command }); - if (snapshot.canonical.status !== 'ok') return; - const registration = snapshot.query.status === 'ok' ? snapshot.query.registration : { present: false as const }; + if (snapshot.canonical.status !== 'ok') { + return { kind: 'incomplete', detail: `canonical payload observation failed: ${snapshot.canonical.detail}` }; + } + if (snapshot.query.status !== 'ok') { + return { kind: 'incomplete', detail: `Codex registration observation failed: ${snapshot.query.detail}` }; + } + const registration = snapshot.query.registration; const installedVersion = registration.present && registration.version ? registration.version.canonical : null; const store = openCodexActivationStore({ genieHome: GENIE_HOME }); const published = publishCodexDelivery({ lease, store, installedVersion, - targetVersion: snapshot.canonical.version.canonical, - canonicalPayloadSha256: snapshot.canonical.digest, - channel, + evidence, + deliveryRoot, + // Group E: a current-N delivery with a STALE record republishes (a matching + // record never does) — same fresh-host/converged-host truth as install. + existingRecord: snapshotDeliveryReadState(snapshot), }); - if (published.published && published.record !== null && previousBackup !== null) { + if ((published.outcome === 'published' || published.outcome === 'matching') && previousBackup !== null) { publishBackupSidecarIfProtocolCapable(previousBackup, published.record.deliveryId); } + if (published.outcome === 'published' || published.outcome === 'matching') { + return { kind: published.outcome, deliveryId: published.record.deliveryId }; + } + return { kind: 'incomplete', detail: published.detail }; } /** @@ -2304,6 +3007,8 @@ async function runDelivery( manifest: LatestManifest, platform: string, diagnosticsCtx: UpdateDiagnosticsContext, + codexLease: HeldLifecycleLease, + dependencies: UpdateCommandDependencies = {}, ): Promise { const externalRoot = createPrivateUpdateTempRoot(); const extractedRoot = join(externalRoot, 'release-payload'); @@ -2313,7 +3018,14 @@ async function runDelivery( let admitted: InstallStagingDirectoryGuard | null = null; let promotionComplete = false; log('Downloading signed tarball from GitHub Releases...'); - const tarballPath = await downloadAndVerifyTarball(manifest, platform, externalRoot); + const downloaded = await (dependencies.downloadDeliveryAssets ?? downloadAndVerifyDeliveryAssets)( + manifest, + platform, + externalRoot, + ); + const { tarballPath } = downloaded; + const artifactSha256 = hashFileSha256(tarballPath); + if (artifactSha256 === null) throw new Error('verified tarball became unreadable before delivery'); diagnosticsCtx.tarballPath = tarballPath; diagnosticsCtx.attestationVerified = true; success(`Verified signed tarball for ${tarballPath.split('/').pop()}`); @@ -2327,20 +3039,41 @@ async function runDelivery( // before admission. This normalizes only our own staging root. chmodSync(extractedRoot, 0o700); assertPrivateUpdateTempRoot(extractedRoot); + const candidate = proveExtractedDeliveryCandidate(extractedRoot); + if (candidate.version !== normalizeVersion(manifest.version)) { + throw new Error( + `candidate version ${candidate.version} differs from selected ${normalizeVersion(manifest.version)}`, + ); + } + const evidence = verifyDownloadedDeliveryEvidence( + { + descriptorBytes: downloaded.descriptorBytes, + bundleBytes: downloaded.bundleBytes, + manifestBytes: manifest.manifestBytes, + targetVersion: candidate.version, + channel: manifest.channel as DeliveryEvidenceChannel, + platformId: platform as DeliveryEvidencePlatformId, + platformTriple: `${process.platform}-${process.arch}`, + releaseTag: `v${normalizeVersion(manifest.version)}`, + releaseName: `genie-${normalizeVersion(manifest.version)}-${platform}.tar.gz`, + artifactSha256, + installedBinarySha256: candidate.binarySha256, + canonicalPayloadSha256: candidate.pluginTreeSha256, + }, + dependencies.evidenceVerification, + ); log('Promoting verified release generation...'); + codexLease.assertOperation(codexLease.operationId); recoverPendingInstallPromotions({ genieHome: GENIE_HOME }); admitted = admitExternalInstallStaging({ genieHome: GENIE_HOME, externalStagingRoot: extractedRoot, expectedVersion: manifest.version, }); - let codexLease: HeldLifecycleLease | null = null; try { verifyAdmittedInstallStagingPayload(admitted); - // Acquire the Codex lifecycle lease after signed-download verification and - // before the first binary swap; hold it through the delivery publication. - codexLease = acquireCodexDeliveryLeaseOrRefuse(); + codexLease.assertOperation(codexLease.operationId); const promotion = promoteStagedInstall({ genieHome: GENIE_HOME, stagingRoot: admitted.stagingRoot, @@ -2436,12 +3169,30 @@ async function runDelivery( }); // The payload is now in GENIE_HOME; publish attested delivery facts (and the // rollback sidecar for a protocol-capable backup) under the held lease. - publishCodexDeliveryFacts(diagnosticsCtx.channel, diagnosticsCtx.previousBackup, codexLease); + const deliveryRoot = resolveCanonicalPayloadRoot(); + const installedBinarySha256 = hashFileSha256(join(GENIE_BIN, 'genie')); + if (deliveryRoot === null || installedBinarySha256 === null) { + throw new CodexDeliveryPublicationError('installed delivery root/binary could not be bound'); + } + if (installedBinarySha256 !== candidate.binarySha256) { + throw new CodexDeliveryPublicationError('installed binary differs from the verified delivery evidence'); + } + let publication: DeliveryPublicationCompletion; + try { + publication = publishCodexDeliveryFacts( + diagnosticsCtx.previousBackup, + codexLease, + evidence, + realpathSync(deliveryRoot), + ); + } catch (cause) { + throw new CodexDeliveryPublicationError(`delivery record publication failed: ${errMsg(cause)}`); + } + if (publication.kind === 'incomplete') throw new CodexDeliveryPublicationError(publication.detail); return auxiliaryOutcomes; } finally { - // Release the Codex lifecycle lease on every terminal path (success, refusal, - // handled failure) before the staging cleanup. - codexLease?.release(); + // The command boundary owns both lifecycle leases. This scope only closes + // admitted staging; the caller releases Codex first, then agent-sync. if (admitted !== null) { try { if (promotionComplete) removeInstallStagingDirectory(admitted); @@ -2836,33 +3587,31 @@ function printAuxiliaryOutcome(outcome: AuxiliaryTreeOutcome): void { for (const warning of outcome.warnings) log(`${outcome.label}/ cleanup warning: ${warning}`); } -async function runRollback(): Promise { +async function runRollback(): Promise { log('Checking protocol-safe rollback eligibility...'); const result = performProtocolSafeRollback({ genieBin: GENIE_BIN, genieHome: GENIE_HOME }); switch (result.status) { case 'rolled-back': success(`Rolled back to v${result.restoredVersion} (digest ${result.binarySha256.slice(0, 12)}…)`); console.log(); - return; + return null; case 'busy': - error( + return new DeferredUpdateTerminal( + 2, `codex-lifecycle-busy: the ${result.holderKind ?? 'unknown'} lifecycle command holds the Codex lease; rollback refused before any exchange with zero mutation.`, + CODEX_LIFECYCLE_BUSY_TRAILER, + true, ); - log(CODEX_LIFECYCLE_BUSY_TRAILER); - console.log(); - process.exit(2); - return; case 'no-backup': case 'refused': - error(`Rollback refused: ${result.detail}`); - console.log(); - process.exit(1); - return; + return new DeferredUpdateTerminal(1, `Rollback refused: ${result.detail}`, undefined, true); case 'aborted': - error(`Rollback aborted before completion (the live binary is unchanged): ${result.detail}`); - console.log(); - process.exit(1); - return; + return new DeferredUpdateTerminal( + 1, + `Rollback aborted before completion (the live binary is unchanged): ${result.detail}`, + undefined, + true, + ); } } diff --git a/src/genie.ts b/src/genie.ts index 83a18f770..2806a6a5d 100644 --- a/src/genie.ts +++ b/src/genie.ts @@ -70,7 +70,7 @@ program .description('Configure genie settings') .option('--quick', 'Accept all defaults') .option('--shortcuts', 'Only configure keyboard shortcuts') - .option('--codex', 'Configure Codex and persist its scope for later explicit Genie updates') + .option('--codex', 'Activate the authenticated Codex delivery and converge its managed surfaces') .option('--terminal', 'Only configure terminal defaults') .option('--session', 'Only configure session settings') .option('--reset', 'Reset configuration to defaults') @@ -122,17 +122,41 @@ program 'restart', 'verify', 'skipMaintenance', + 'publishLocalDelivery', ]), ) .addOption(new Option('--print-update-capabilities').hideHelp()) .addOption(new Option('--json').hideHelp()) + .addOption( + new Option('--publish-local-delivery ') + .hideHelp() + .conflicts([ + 'rollback', + 'syncOnly', + 'postDeliveryConverge', + 'printUpdateCapabilities', + 'json', + 'dev', + 'homolog', + 'next', + 'stable', + 'yes', + 'restart', + 'verify', + 'skipMaintenance', + ]), + ) .action(updateCommand); program .command('install') .description('Post-install finishing step — invoked by install.sh after the binary is linked') .option('--skip-v4-cleanup', 'Leave v4-era leftovers in place (orchestration rules, orphaned plugin caches)') - .option('--integrations ', 'Install client integrations: auto, codex, claude, all, or none', 'auto') + .option( + '--integrations ', + 'Deliver client integrations: auto, codex, claude, all, or none (Codex activation requires setup --codex)', + 'auto', + ) .option('--skip-integrations', 'Alias for --integrations none') .action((options: InstallOptions) => installCommand(options)); diff --git a/src/lib/agent-sync.ts b/src/lib/agent-sync.ts index 40278247e..4d08d0c5d 100644 --- a/src/lib/agent-sync.ts +++ b/src/lib/agent-sync.ts @@ -1533,7 +1533,8 @@ export function inspectManagedSkillTree(dir: string): ManagedSkillTreeReport { } // ============================================================================ -// Retired Codex fallback ownership + batch retirement (deliberately unwired) +// Codex fallback ownership + batch retirement (setup wires this only after +// authenticated delivery-root admission and an exact enabled-plugin proof) // ============================================================================ export interface CodexFallbackHistoricalTuple { @@ -1695,7 +1696,7 @@ export function loadHistoricalCodexFallbackTupleKeys(): ReadonlySet { /** * Classify a single `~/.agents/skills/` dir's Codex-fallback ownership. * Exported so doctor's read-only tier inspection can split a structurally - * `managed-clean` tree into "recognized, retirable by `genie update`" vs + * `managed-clean` tree into "recognized, retirable by authenticated setup" vs * "well-formed but unrecognized, needs manual review" using the identical * marker/digest/allowlist gate {@link planCodexFallbackRetirement} uses to * decide what it will actually retire — so doctor never promises a retirement @@ -1757,7 +1758,8 @@ export function retirementTransactionId(accepted: readonly CodexFallbackAccepted /** * Build a closed, deterministic ownership decision without changing disk. * Ambiguous names are reported under `preserved` and never enter the apply set. - * No active install/update/setup path calls this boundary. + * The active setup path reaches this boundary only through its authenticated + * delivery-root consumer after an exact enabled-plugin health proof. */ export function planCodexFallbackRetirement(options: PlanCodexFallbackRetirementOptions): CodexFallbackRetirementPlan { const fallbackSkillsDir = canonicalPhysicalFallbackRoot(options.fallbackSkillsDir); @@ -1905,7 +1907,7 @@ export interface FsyncPathDeps { * byte durability is the load-bearing crash-safety guarantee, so a failure * propagates. DIRECTORY-metadata flush is best-effort: Windows (and some network * filesystems) refuse to open/fsync a directory fd (EISDIR/EPERM/EINVAL/ENOTSUP), - * which must NOT brick the codex step of `genie update` at journal-prepare. On + * which must NOT brick Codex setup at retirement-journal preparation. On * win32 a directory fsync is skipped entirely; elsewhere the tolerable errors are * swallowed. A durable rename still lands; only the extra directory-entry flush * is skipped, exactly as on filesystems that never guaranteed it. diff --git a/src/lib/codex-activation-executor.test.ts b/src/lib/codex-activation-executor.test.ts index 0eb265d0e..49e730e5f 100644 --- a/src/lib/codex-activation-executor.test.ts +++ b/src/lib/codex-activation-executor.test.ts @@ -1,4 +1,5 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; @@ -13,12 +14,14 @@ import { type AuthorizationResult, type CodexActivationStore, type CodexActivationStoreOptions, + type RefreshIntent, authorizeCodexActivation, classifyCodexActivation, openCodexActivationStore, requestRetirementAssertion, scanPhysicalTree, } from './codex-activation.js'; +import { mintTestDeliveryEvidence } from './codex-delivery-evidence.test-support.js'; import { acquireLifecycleLease } from './codex-lifecycle-lease.js'; import type { CommandResult, CommandRunner } from './runtime-integrations.js'; @@ -29,6 +32,10 @@ const TARGET_VERSION = '5.260712.1'; const NEWER_VERSION = '5.260799.9'; const STUB_COMMAND = '/stub/codex'; +const TEST_EVIDENCE_VERIFICATION = { + verifyBundle: () => ({ integratedTime: '1753228800' }), +}; + // --------------------------------------------------------------------------- // Fixture isolation contract // --------------------------------------------------------------------------- @@ -115,12 +122,13 @@ function buildFixture(options: FixtureOptions): Fixture { const root = mkdtempSync(join(tmpdir(), 'genie-executor-fixture-')); const genieHome = join(root, 'genie-home'); const codexHome = join(root, 'codex-home'); - const canonicalRoot = join(root, 'canonical'); + const canonicalRoot = genieHome; const canonicalPayloadDir = join(canonicalRoot, 'plugins', 'genie'); const familyDir = join(codexHome, 'plugins', 'cache', 'automagik', 'genie'); const targetCacheDir = join(familyDir, target); mkdirSync(genieHome, { recursive: true }); mkdirSync(join(canonicalPayloadDir, 'scripts'), { recursive: true }); + mkdirSync(join(canonicalPayloadDir, 'codex-agents'), { recursive: true }); mkdirSync(familyDir, { recursive: true }); // Canonical payload: the real H3 script plus a couple of physical files. @@ -129,6 +137,7 @@ function buildFixture(options: FixtureOptions): Fixture { mkdirSync(join(canonicalPayloadDir, 'hooks'), { recursive: true }); writeFileSync(join(canonicalPayloadDir, 'hooks', 'codex-hooks.json'), '{"hooks":{}}\n'); writeFileSync(join(canonicalRoot, 'VERSION'), `${target}\n`); + writeFileSync(join(canonicalRoot, 'genie'), '#!/bin/sh\n'); const configPath = join(codexHome, 'config.toml'); setConfigEnabled(configPath, options.initialEnabled); @@ -171,6 +180,7 @@ function buildFixture(options: FixtureOptions): Fixture { allowRootOverride: true, command: STUB_COMMAND, runner: makeRunner(), + deliveryEvidenceVerification: TEST_EVIDENCE_VERIFICATION, }; return { @@ -189,16 +199,30 @@ function buildFixture(options: FixtureOptions): Fixture { } /** Publish an installed-delivery (and optional downgrade receipt) exactly as Group C would. */ -function publishDelivery(fixture: Fixture, opts: { downgradeFrom?: string; deliveryId?: string } = {}): void { +function publishDelivery(fixture: Fixture, opts: { downgradeFrom?: string } = {}): void { const lease = acquireLifecycleLease('update-delivery', { genieHome: fixture.genieHome }); if (!lease.ok) throw new Error(`could not acquire delivery lease: ${lease.detail}`); try { fixture.openStore().publishDelivery(lease, { - targetVersion: TARGET_VERSION, - canonicalPayloadSha256: fixture.canonicalDigest, - channel: 'stable', + evidence: mintTestDeliveryEvidence({ + descriptor: { + version: TARGET_VERSION, + releaseTag: `v${TARGET_VERSION}`, + releaseName: `genie-${TARGET_VERSION}-${ + process.platform === 'darwin' + ? 'darwin-arm64' + : process.arch === 'arm64' + ? 'linux-arm64' + : 'linux-x64-glibc' + }.tar.gz`, + canonicalPayloadSha256: fixture.canonicalDigest, + installedBinarySha256: createHash('sha256') + .update(readFileSync(join(fixture.canonicalRoot, 'genie'))) + .digest('hex'), + }, + }).evidence, + deliveryRoot: fixture.canonicalRoot, downgradeFrom: opts.downgradeFrom, - deliveryId: opts.deliveryId, }); } finally { lease.release(); @@ -257,6 +281,12 @@ function leasePath(fixture: Fixture): string { function addCalls(fixture: Fixture): number { return fixture.runnerCalls.filter((call) => call.endsWith('plugin add genie@automagik --json')).length; } +function marketplaceCalls(fixture: Fixture): number { + return fixture.runnerCalls.filter((call) => call.includes('plugin marketplace add ')).length; +} +function readIntent(fixture: Fixture): RefreshIntent { + return JSON.parse(readFileSync(intentPath(fixture), 'utf8')) as RefreshIntent; +} // --------------------------------------------------------------------------- @@ -305,12 +335,90 @@ describe('permit boundary', () => { }); expect(result.status === 'refused' || result.status === 'broken').toBe(true); expect(existsSync(intentPath(fx))).toBe(false); + expect(marketplaceCalls(fx)).toBe(0); expect(addCalls(fx)).toBe(0); expect(existsSync(leasePath(fx))).toBe(false); // released }); }); +describe('delivery-incomplete inner guard', () => { + test('activation without a delivery record refuses as delivery-incomplete with zero mutation', () => { + const fx = fixture({ from: FROM_VERSION, initialEnabled: true, createFromCache: true }); + // Intentionally do NOT publishDelivery: the inner guard must refuse before any mutation. + const result = runActivation(fx); + expect(result.status).toBe('delivery-incomplete'); + if (result.status !== 'delivery-incomplete') throw new Error('unreachable'); + expect(result.code).toBe('delivery-incomplete'); + expect(result.assessment).toBe('absent'); + expect(result.trailer.deliveryComplete).toBe(false); + expect(result.recovery).toContain('genie update'); + // No activation-owned mutation: no journal, no plugin add, released lease. + expect(existsSync(intentPath(fx))).toBe(false); + expect(addCalls(fx)).toBe(0); + expect(existsSync(leasePath(fx))).toBe(false); + }); + + test('a matching delivery record proceeds normally (control for the guard)', () => { + const fx = fixture({ from: FROM_VERSION, initialEnabled: true, createFromCache: true }); + publishDelivery(fx); + const result = runActivation(fx); + expect(result.status).toBe('activated'); + expect(marketplaceCalls(fx)).toBe(1); + expect(addCalls(fx)).toBe(1); + }); +}); + describe('successful activation', () => { + test('inner begin precedes callback-scoped marketplace registration, which precedes plugin add', () => { + const fx = fixture({ from: null, initialEnabled: true }); + publishDelivery(fx); + const order: string[] = []; + const runner = fx.makeRunner(); + const result = runActivation(fx, { + hooks: { + afterBeginActivation: () => order.push('begin'), + beforeMarketplaceRegistration: () => order.push('marketplace-before'), + afterMarketplaceRegistration: () => order.push('marketplace-after'), + beforeCommandStarted: () => order.push('command-started'), + beforePluginAdd: () => order.push('plugin-add'), + }, + runner: (command, args, options) => { + if (args.slice(0, 3).join(' ') === 'plugin marketplace add') order.push('marketplace-command'); + return runner(command, args, options); + }, + }); + + expect(result.status).toBe('activated'); + expect(order).toEqual([ + 'begin', + 'marketplace-before', + 'marketplace-command', + 'marketplace-after', + 'command-started', + 'plugin-add', + ]); + }); + + test('marketplace registration failure leaves plugin/cache untouched and roles remain outside the executor', () => { + const fx = fixture({ from: null, initialEnabled: true }); + publishDelivery(fx); + const runner = fx.makeRunner(); + const result = runActivation(fx, { + runner: (command, args, options) => { + if (args.slice(0, 3).join(' ') === 'plugin marketplace add') { + return { exitCode: 1, stdout: '', stderr: 'injected marketplace registration failure' }; + } + return runner(command, args, options); + }, + }); + + expect(result.status).toBe('broken'); + expect(addCalls(fx)).toBe(0); + expect(existsSync(fx.targetCacheDir)).toBe(false); + expect(existsSync(join(fx.codexHome, 'agents'))).toBe(false); + expect(classifyCodexActivation(fx.openStore().observe()).kind).toBe('intent-planned'); + }); + test('upgrade preserves enabled state, proves parity + H3, clears the journal, returns verified', () => { const fx = fixture({ from: FROM_VERSION, initialEnabled: true, createFromCache: true }); publishDelivery(fx); @@ -338,13 +446,15 @@ describe('successful activation', () => { expect(configEnabled(fx.configPath)).toBe(false); }); - test('fresh install (registration absent) activates through a single add', () => { + test('fresh install (registration absent) activates enabled through a single add', () => { const fx = fixture({ from: null, initialEnabled: true }); publishDelivery(fx); const result = runActivation(fx); expect(result.status).toBe('activated'); if (result.status !== 'activated') throw new Error('unreachable'); expect(result.direction).toBe('install'); + expect(result.enabled).toBe(true); + expect(configEnabled(fx.configPath)).toBe(true); expect(addCalls(fx)).toBe(1); }); }); @@ -370,9 +480,49 @@ describe('fingerprint freshness', () => { if (result.status !== 'stale') throw new Error('unreachable'); expect(result.mismatchField).toBe('enabled'); expect(existsSync(intentPath(fx))).toBe(false); + expect(marketplaceCalls(fx)).toBe(0); expect(addCalls(fx)).toBe(0); expect(existsSync(leasePath(fx))).toBe(false); }); + + test('journal-byte tampering after consent makes the permit stale with zero mutation', () => { + const fx = fixture({ from: FROM_VERSION, initialEnabled: false, createFromCache: true }); + publishDelivery(fx); + const first = runActivation(fx, { + hooks: { + beforeEnabledRestore() { + throw new Error('injected crash before enabled restore'); + }, + }, + }); + expect(first.status).toBe('broken'); + expect(classifyCodexActivation(fx.openStore().observe()).kind).toBe('intent-target-current'); + + const store = fx.openStore(); + const { permit } = mintPermit(store); + const tampered = { ...readIntent(fx), priorEnabled: true }; + writeFileSync(intentPath(fx), `${JSON.stringify(tampered, null, 2)}\n`); + const tamperedBytes = readFileSync(intentPath(fx), 'utf8'); + const addsBefore = addCalls(fx); + const marketplacesBefore = marketplaceCalls(fx); + + const result = executeCodexActivation({ + permit, + store, + command: STUB_COMMAND, + codexHome: fx.codexHome, + genieHome: fx.genieHome, + configPath: fx.configPath, + deps: { runner: fx.makeRunner() }, + }); + expect(result.status).toBe('stale'); + if (result.status !== 'stale') throw new Error('unreachable'); + expect(result.mismatchField).toBe('intentContentSha256'); + expect(readFileSync(intentPath(fx), 'utf8')).toBe(tamperedBytes); + expect(addCalls(fx)).toBe(addsBefore); + expect(marketplaceCalls(fx)).toBe(marketplacesBefore); + expect(existsSync(leasePath(fx))).toBe(false); + }); }); describe('lifecycle lease', () => { @@ -474,6 +624,101 @@ describe('crash recovery', () => { expect(existsSync(intentPath(fx))).toBe(false); expect(classifyCodexActivation(fx.openStore().observe()).kind).toBe('current'); }); + + const TARGET_CURRENT_RECOVERY_CASES = [ + { + name: 'disabled upgrade', + from: FROM_VERSION, + initialEnabled: false, + createFromCache: true, + downgradeFrom: undefined, + direction: 'upgrade', + priorEnabled: false, + finalEnabled: false, + }, + { + name: 'fresh install', + from: null, + initialEnabled: false, + createFromCache: false, + downgradeFrom: undefined, + direction: 'install', + priorEnabled: true, + finalEnabled: true, + }, + { + name: 'explicit downgrade', + from: NEWER_VERSION, + initialEnabled: true, + createFromCache: true, + downgradeFrom: NEWER_VERSION, + direction: 'downgrade', + priorEnabled: true, + finalEnabled: true, + }, + ] as const; + + for (const recoveryCase of TARGET_CURRENT_RECOVERY_CASES) { + test(`${recoveryCase.name} resumes the target-current journal without losing transaction identity`, () => { + const fx = fixture({ + from: recoveryCase.from, + initialEnabled: recoveryCase.initialEnabled, + createFromCache: recoveryCase.createFromCache, + }); + publishDelivery( + fx, + recoveryCase.downgradeFrom === undefined ? {} : { downgradeFrom: recoveryCase.downgradeFrom }, + ); + const first = runActivation(fx, { + hooks: { + beforeEnabledRestore() { + throw new Error('injected crash before enabled restore'); + }, + }, + }); + expect(first.status).toBe('broken'); + expect(addCalls(fx)).toBe(1); + expect(classifyCodexActivation(fx.openStore().observe()).kind).toBe('intent-target-current'); + const before = readIntent(fx); + expect(before.phase).toBe('removal-observed'); + expect(before.direction).toBe(recoveryCase.direction); + expect(before.priorEnabled).toBe(recoveryCase.priorEnabled); + if (recoveryCase.direction === 'downgrade') expect(before.receiptId).not.toBeNull(); + else expect(before.receiptId).toBeNull(); + + const rebound: RefreshIntent[] = []; + const second = runActivation(fx, { + hooks: { + afterBeginActivation() { + rebound.push(readIntent(fx)); + }, + }, + }); + expect(second.status).toBe('activated'); + if (second.status !== 'activated') throw new Error('unreachable'); + expect(second.direction).toBe(recoveryCase.direction); + expect(second.enabled).toBe(recoveryCase.finalEnabled); + expect(configEnabled(fx.configPath)).toBe(recoveryCase.finalEnabled); + expect(addCalls(fx)).toBe(1); + const reboundIntent = rebound[0]; + if (reboundIntent === undefined) throw new Error('target-current journal was not rebound'); + expect(reboundIntent.refreshIntentId).toBe(before.refreshIntentId); + expect(reboundIntent.phase).toBe(before.phase); + expect(reboundIntent.direction).toBe(before.direction); + expect(reboundIntent.priorEnabled).toBe(before.priorEnabled); + expect(reboundIntent.receiptId).toBe(before.receiptId); + expect(reboundIntent.operationId).not.toBe(before.operationId); + expect(existsSync(intentPath(fx))).toBe(false); + + if (recoveryCase.direction === 'downgrade') { + if (before.receiptId === null) throw new Error('downgrade journal lost its receipt id'); + expect(existsSync(receiptPath(fx))).toBe(false); + expect(existsSync(tombstonePath(fx))).toBe(true); + const tombstone = JSON.parse(readFileSync(tombstonePath(fx), 'utf8')) as { receiptId: string }; + expect(tombstone.receiptId).toBe(before.receiptId); + } + }); + } }); // --------------------------------------------------------------------------- @@ -494,6 +739,7 @@ function activateWith( allowRootOverride: true, command: STUB_COMMAND, runner, + deliveryEvidenceVerification: TEST_EVIDENCE_VERIFICATION, }); const { permit } = mintPermit(store); return executeCodexActivation({ @@ -549,6 +795,43 @@ function ambiguousAbsentRunner(fx: Fixture): CommandRunner { } describe('post-command phase resume', () => { + test('resumed fresh intent enables the plugin, while resumed existing-disabled intent preserves false', () => { + for (const fixtureCase of [ + { name: 'fresh', from: null, initialEnabled: false, createFromCache: false, expectedEnabled: true }, + { + name: 'existing-disabled', + from: FROM_VERSION, + initialEnabled: false, + createFromCache: true, + expectedEnabled: false, + }, + ] as const) { + const fx = fixture(fixtureCase); + publishDelivery(fx); + const first = runActivation(fx, { + hooks: { + beforePluginAdd() { + throw new Error(`${fixtureCase.name}: injected pre-add crash`); + }, + }, + }); + expect(first.status).toBe('broken'); + const journal = JSON.parse(readFileSync(intentPath(fx), 'utf8')) as Record; + if (fixtureCase.from === null) { + // Compatibility case: older planners persisted false for a fresh + // registration. Resume derives the desired state from from=null. + journal.priorEnabled = false; + writeFileSync(intentPath(fx), `${JSON.stringify(journal)}\n`); + } + + const resumed = runActivation(fx); + expect(resumed.status).toBe('activated'); + if (resumed.status !== 'activated') throw new Error(`${fixtureCase.name}: activation did not resume`); + expect(resumed.enabled).toBe(fixtureCase.expectedEnabled); + expect(configEnabled(fx.configPath)).toBe(fixtureCase.expectedEnabled); + } + }); + test('removal-observed resumes: a fresh authorized run reconciles through add + verify', () => { const fx = fixture({ from: FROM_VERSION, initialEnabled: true, createFromCache: true }); publishDelivery(fx); @@ -922,6 +1205,7 @@ const store = openCodexActivationStore({ allowRootOverride: true, command: '/stub/codex', runner, + deliveryEvidenceVerification: { verifyBundle: () => ({ integratedTime: '1753228800' }) }, }); const snapshot = store.observe(); diff --git a/src/lib/codex-activation-executor.ts b/src/lib/codex-activation-executor.ts index 70d0e955e..35051dfa9 100644 --- a/src/lib/codex-activation-executor.ts +++ b/src/lib/codex-activation-executor.ts @@ -13,11 +13,12 @@ * 2. `store.beginActivation(lease, permit)` re-observes, exact-matches the * permit fingerprint, and writes the `planned` journal. A stale permit or an * ineligible state refuses before the first journal write. - * 3. Drive the supported Codex CLI `plugin add` through A's typed phase - * transitions (`command-started` fsynced immediately before the cache- - * advancing command, `removal-observed`/`ambiguous-absent` on the failure - * probe), preserve the observed enabled flag, and handle `intent-target- - * current` finalization with no add/remove. + * 3. Register the revalidated canonical marketplace, then drive the supported + * Codex CLI `plugin add` through A's typed phase transitions + * (`command-started` fsynced immediately before the cache-advancing + * command, `removal-observed`/`ambiguous-absent` on the failure probe), + * preserve the observed enabled flag, and handle `intent-target-current` + * finalization with no plugin add/remove. * 4. Verify full physical N+1 parity ONLY inside * `store.withRevalidatedDeliveryRoot(callback)` — the canonical digest never * leaves the callback as a raw root — then run the exact bounded H3 @@ -26,11 +27,10 @@ * tombstones and removes any downgrade receipt (one-time, durable). * 6. Release the lease on success, typed refusal, and handled failure alike. * - * The executor never reads or writes A's private protocol files, the hook-trust - * store, or the canonical bundle root directly: marketplace registration and the - * raw root stay delivery-side (Group C) because A deliberately hides the root, so - * the cache-advancing `plugin add` (resolved from the already-registered - * marketplace) is the entire permit boundary. + * The executor never reads or writes A's private protocol files or the hook-trust + * store. Marketplace registration runs inside the deep store's revalidated + * delivery callback; role-agent convergence remains setup-owned and follows + * successful activation plus committed Codex consent. */ import { spawnSync } from 'node:child_process'; @@ -63,9 +63,25 @@ import { resolveSetupExitCode, serializeActivationResultTrailer, } from './codex-activation.js'; +// Group B's delivery-attestation + host-observation contract, re-exported below as +// part of the stable facade Groups C and D consume from this one module. +import { + DELIVERY_INCOMPLETE_RECOVERY, + type DeliveryAssessment, + assessAuthenticatedDelivery, + buildDeliveryIncompleteResult, + parseCodexHostObservation, + projectHostQuery, + witnessCodexCacheFamily, +} from './codex-host-observation.js'; import { type HeldLifecycleLease, LifecycleFencingError, acquireLifecycleLease } from './codex-lifecycle-lease.js'; import type { LifecycleLeaseKind, LifecycleLeaseResult } from './codex-lifecycle-lease.js'; -import { type CommandRunner, runBoundedIntegrationCommand, setCodexPluginEnabled } from './runtime-integrations.js'; +import { + type CommandRunner, + createCodexMarketplaceRegistrationConsumer, + runBoundedIntegrationCommand, + setCodexPluginEnabled, +} from './runtime-integrations.js'; // A's permit is a type-only export: importers may name it, but the runtime class // never leaves A and only `beginActivation`/`quarantineIntent` can validate it. @@ -89,6 +105,17 @@ export { requestRetirementAssertion, resolveSetupExitCode, serializeActivationResultTrailer, + // Group B's host-observation + delivery-attestation contract (deliverables 1, 4, 6). + buildDeliveryIncompleteResult, +}; + +/** @public Stable delivery-attestation and host-observation facade for downstream lifecycle groups. */ +export { + DELIVERY_INCOMPLETE_RECOVERY, + assessAuthenticatedDelivery, + parseCodexHostObservation, + projectHostQuery, + witnessCodexCacheFamily, }; // ============================================================================ @@ -110,6 +137,8 @@ const SMOKE_EXPECTED_CONTEXT = export interface ActivationPhaseHooks { afterLeaseAcquired?(operationId: string): void; afterBeginActivation?(handle: ActivationHandle): void; + beforeMarketplaceRegistration?(): void; + afterMarketplaceRegistration?(): void; beforeCommandStarted?(): void; beforePluginAdd?(): void; afterPluginAdd?(): void; @@ -123,6 +152,8 @@ export interface ActivationPhaseHooks { export interface CodexActivationExecutorDeps { /** Injected command runner for the cache-advancing `plugin add`; defaults to the bounded runner. */ runner?: CommandRunner; + /** Factory for the marketplace capability consumed only by the deep store. */ + createMarketplaceConsumer?: typeof createCodexMarketplaceRegistrationConsumer; /** Resolve an absolute Node executable for the H3 replay; defaults to `Bun.which('node')`. */ resolveNode?: () => string | null; now?: () => Date; @@ -134,7 +165,7 @@ export interface CodexActivationExecutorDeps { export interface ExecuteCodexActivationInput { permit: ActivationPermit; store: CodexActivationStore; - /** Resolved absolute codex executable; the marketplace is already registered by delivery. */ + /** Resolved absolute codex executable used for marketplace registration and plugin activation. */ command: string; codexHome: string; genieHome: string; @@ -168,6 +199,14 @@ export type ActivationExecutionResult = trailer: ActivationResultTrailer; } | { status: 'refused'; code: string; detail: string; trailer: ActivationResultTrailer } + | { + status: 'delivery-incomplete'; + code: 'delivery-incomplete'; + assessment: Exclude; + detail: string; + recovery: string; + trailer: ActivationResultTrailer; + } | { status: 'broken'; code: string; detail: string; trailer: ActivationResultTrailer }; /** A supported Codex CLI command failed; carries no host authority claim. */ @@ -228,6 +267,13 @@ function runActivationUnderLease( trailer: staleTrailer(), }; } + if (begin.status === 'delivery-incomplete') { + // Group B inner guard: the re-observed delivery record is absent/invalid/ + // mismatched. Nothing was written; report the stable delivery-incomplete + // result (authority none, exit 1, deliveryComplete false) with the one + // update/install recovery command. + return deliveryIncompleteResult(begin.assessment, begin.detail); + } if (begin.status === 'refused') { // A genuine permit for a state that opens no activation transaction (e.g. // cache-missing/payload-mismatch, or a quarantine-only capability): report the @@ -237,8 +283,9 @@ function runActivationUnderLease( return refusedFromState(preState, begin.reason); } const handle = begin.handle; - deps.hooks?.afterBeginActivation?.(handle); try { + deps.hooks?.afterBeginActivation?.(handle); + registerAuthorizedMarketplace(input, lease, deps); if (preState.kind === 'intent-target-current') { return finalizeVerifiedGeneration(input, lease, handle, deps, /* ranCacheCommand */ false); } @@ -248,6 +295,29 @@ function runActivationUnderLease( } } +/** + * Revalidate the authenticated delivery root after the inner begin guard and + * register its canonical marketplace inside the callback-scoped capability. + * Any drift or command failure stops before `command-started` and therefore + * before `plugin add`. + */ +function registerAuthorizedMarketplace( + input: ExecuteCodexActivationInput, + lease: HeldLifecycleLease, + deps: CodexActivationExecutorDeps, +): void { + input.store.withRevalidatedDeliveryRoot(lease, (ops) => { + deps.hooks?.beforeMarketplaceRegistration?.(); + const consumer = (deps.createMarketplaceConsumer ?? createCodexMarketplaceRegistrationConsumer)({ + command: input.command, + runner: deps.runner, + timeoutMs: input.timeoutMs ?? DEFAULT_COMMAND_TIMEOUT_MS, + }); + ops.consume(consumer); + deps.hooks?.afterMarketplaceRegistration?.(); + }); +} + // ============================================================================ // Add path (upgrade / install / explicit downgrade / planned resume) // ============================================================================ @@ -444,13 +514,18 @@ function restoreEnabledFlag(input: ExecuteCodexActivationInput): boolean { return desiredEnabled; } -/** The prior enabled flag the permit's journal captured (via the planned intent). */ +/** Resolve the durable desired enabled state captured by the activation intent. */ function readPriorEnabled(input: ExecuteCodexActivationInput): boolean { // The journal is A's private state; the executor reads the observed enabled flag - // rather than the raw journal file. `beginActivation` bound priorEnabled to this - // same observation, so the observed value is the authoritative desired flag. + // rather than the raw journal file. A fresh from=null intent represents a new + // integration, never a prior operator disablement. Deriving true from that + // durable fact also repairs older schema-1 journals which recorded false + // before their first add completed. Existing registrations retain their exact + // captured choice. const snapshot = input.store.observe(); - if (snapshot.intent.status === 'valid') return snapshot.intent.intent.priorEnabled; + if (snapshot.intent.status === 'valid') { + return snapshot.intent.intent.fromPluginVersion === null ? true : snapshot.intent.intent.priorEnabled; + } return snapshot.query.status === 'ok' && snapshot.query.registration.present ? snapshot.query.registration.enabled : true; @@ -664,6 +739,32 @@ function brokenFromError(input: ExecuteCodexActivationInput, error: unknown): Ac }; } +/** + * Map Group B's inner-guard refusal to the executor result: the stable + * delivery-incomplete outcome (authority none, exit 1, deliveryComplete false) + * carrying the one update/install recovery command. Nothing was mutated. + */ +function deliveryIncompleteResult( + assessment: Exclude, + detail: string, +): ActivationExecutionResult { + const result = buildDeliveryIncompleteResult(assessment, detail); + return { + status: 'delivery-incomplete', + code: 'delivery-incomplete', + assessment, + detail: result.detail, + recovery: result.recovery, + trailer: { + schemaVersion: 1, + code: 'delivery-incomplete', + deliveryComplete: false, + retry: true, + nextAction: result.recovery, + }, + }; +} + function busyTrailer(holderKind: string | null): ActivationResultTrailer { return { schemaVersion: 1, diff --git a/src/lib/codex-activation.test.ts b/src/lib/codex-activation.test.ts index 647fd16bf..cdc0ed2cf 100644 --- a/src/lib/codex-activation.test.ts +++ b/src/lib/codex-activation.test.ts @@ -1,10 +1,13 @@ import { afterEach, describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; import { existsSync, + lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, + renameSync, rmSync, symlinkSync, writeFileSync, @@ -31,7 +34,7 @@ import { deriveDirection, describeState, observeCodexActivation, - openCodexActivationStore, + openCodexActivationStore as openProductionCodexActivationStore, parseReleaseVersion, projectHumanStatus, projectIntegrationSummary, @@ -41,8 +44,19 @@ import { serializeActivationResultTrailer, stripControl, } from './codex-activation.js'; +import type { CodexActivationStoreOptions } from './codex-activation.js'; +import { mintTestDeliveryEvidence } from './codex-delivery-evidence.test-support.js'; import { acquireLifecycleLease } from './codex-lifecycle-lease.js'; -import type { CommandResult, CommandRunner } from './runtime-integrations.js'; +import * as runtimeMod from './runtime-integrations.js'; +import { + type CommandResult, + type CommandRunner, + createCodexMarketplaceRegistrationConsumer, + createSetupCodexConsentCommitConsumer, + createSetupCodexRoleAgentConsumer, + persistIntegrationConsent, + runDeliveryRootConsumer, +} from './runtime-integrations.js'; function canonicalDigest(genieHome: string): string { const tree = scanPhysicalTree(join(genieHome, 'plugins', 'genie')); @@ -50,6 +64,17 @@ function canonicalDigest(genieHome: string): string { return tree.digest; } +const TEST_EVIDENCE_VERIFICATION = { + verifyBundle: () => ({ integratedTime: '1753228800' }), +}; + +function openCodexActivationStore(options: CodexActivationStoreOptions = {}) { + return openProductionCodexActivationStore({ + ...options, + deliveryEvidenceVerification: TEST_EVIDENCE_VERIFICATION, + }); +} + // ============================================================================ // Fixtures // ============================================================================ @@ -115,6 +140,7 @@ function makeFixture(opts: { mkdirSync(codexHome, { recursive: true }); writeFiles(join(genieHome, 'plugins', 'genie'), PAYLOAD_FILES); writeFileSync(join(genieHome, 'VERSION'), `${opts.targetVersion}\n`); + writeFileSync(join(genieHome, 'genie'), '#!/bin/sh\n'); if (opts.registeredVersion) { const cacheDir = join(codexHome, 'plugins', 'cache', 'automagik', 'genie', opts.registeredVersion); const files = @@ -153,6 +179,22 @@ const NEWER = '5.260713.4'; const DIGEST = 'a'.repeat(64); const OTHER_DIGEST = 'c'.repeat(64); +function deliveryEvidence(version: string, deliveryRoot: string, overrides: { canonicalPayloadSha256?: string } = {}) { + return mintTestDeliveryEvidence({ + descriptor: { + version, + releaseTag: `v${version}`, + releaseName: `genie-${version}-${ + process.platform === 'darwin' ? 'darwin-arm64' : process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64-glibc' + }.tar.gz`, + canonicalPayloadSha256: overrides.canonicalPayloadSha256 ?? canonicalDigest(deliveryRoot), + installedBinarySha256: createHash('sha256') + .update(readFileSync(join(deliveryRoot, 'genie'))) + .digest('hex'), + }, + }).evidence; +} + function ver(s: string) { const parsed = parseReleaseVersion(s); if (!parsed) throw new Error(`bad test version ${s}`); @@ -160,7 +202,15 @@ function ver(s: string) { } function okCanonical(version = T, digest = DIGEST): CanonicalFact { - return { status: 'ok', version: ver(version), digest, identity: '10:100' }; + return { + status: 'ok', + version: ver(version), + digest, + identity: '10:100', + deliveryRoot: '/fixture/genie', + installedBinarySha256: '8'.repeat(64), + platformTriple: `${process.platform}-${process.arch}`, + }; } function regPresent(version = T, enabled = true): QueryFact { return { status: 'ok', registration: { present: true, enabled, version: ver(version) } }; @@ -433,6 +483,12 @@ describe('describeState — exit codes and mutation authority', () => { ).toBe('journal-quarantine-only'); expect(describeState({ kind: 'activation-pending', from: OLD, target: T }).authority).toBe('external-tty-setup'); }); + + test('a failed plugin query keeps explicit upgrade-or-repair guidance', () => { + expect(describeState({ kind: 'query-failed', detail: 'plugin subcommand unavailable' }).recovery).toMatch( + /upgrade.*Codex/i, + ); + }); }); // ============================================================================ @@ -683,6 +739,22 @@ describe('brand unforgeability — the consent entry point is the only route to // ============================================================================ describe('observeCodexActivation — bounded plugin query', () => { + test('canonical observation fails closed when the installed binary digest cannot be obtained', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + rmSync(join(fx.genieHome, 'genie')); + const snapshot = observeCodexActivation({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: T }]) }), + }); + expect(snapshot.canonical).toEqual({ + status: 'error', + detail: 'canonical installed binary is missing, unsafe, or unreadable', + }); + expect(classifyCodexActivation(snapshot).kind).toBe('snapshot-inconsistent'); + }); + test('a current fixture classifies as current with a verified cache', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); const snapshot = observeCodexActivation({ @@ -852,6 +924,31 @@ function heldLease(genieHome: string) { return lease; } +/** + * Publish a matching (upgrade) delivery record for a T-target fixture under its + * own short-lived delivery lease. The Group B inner guard requires a matching + * authenticated delivery record before `beginActivation` writes its journal, so + * every `beginActivation`-under-a-held-lease test seeds one first (mirrors how + * Group C/D publish delivery ahead of permit-gated activation). + */ +function publishMatchingDelivery(genieHome: string, codexHome: string): void { + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + if (!lease.ok) throw new Error('could not acquire delivery lease'); + try { + openCodexActivationStore({ + genieHome, + codexHome, + command: 'codex', + runner: listRunner({ stdout: '{}' }), + }).publishDelivery(lease, { + evidence: deliveryEvidence(T, genieHome), + deliveryRoot: genieHome, + }); + } finally { + lease.release(); + } +} + describe('CodexActivationStore — delivery + downgrade receipt', () => { test('publishDelivery writes a delivery record and no receipt for an ordinary delivery', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); @@ -864,13 +961,75 @@ describe('CodexActivationStore — delivery + downgrade receipt', () => { const lease = heldLease(fx.genieHome); try { const record = store.publishDelivery(lease, { - targetVersion: T, - canonicalPayloadSha256: DIGEST, - channel: 'stable', + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, }); + expect(record.schemaVersion).toBe(2); + expect(record.evidenceDigest).toMatch(/^[0-9a-f]{64}$/); + expect(record.platformId.length).toBeGreaterThan(0); expect(record.deliveryId).toMatch(/^[0-9a-f]{32}$/); expect(existsSync(join(fx.genieHome, '.codex-plugin-delivery-record.json'))).toBe(true); expect(existsSync(join(fx.genieHome, '.codex-plugin-downgrade-receipt.json'))).toBe(false); + const fingerprint = computeActivationFingerprint(store.observe()); + expect(fingerprint.deliveryBindingDigest).toMatch(/^[0-9a-f]{64}$/); + expect(Object.keys(fingerprint).filter((key) => key.startsWith('delivery'))).toEqual(['deliveryBindingDigest']); + } finally { + lease.release(); + } + }); + + test('schema-1 delivery records are legacy-invalid even when their old tuple is otherwise complete', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + const store = openCodexActivationStore({ genieHome: fx.genieHome }); + const lease = heldLease(fx.genieHome); + try { + const record = store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }); + expect(mod.parseDeliveryRecordStructure(JSON.stringify({ ...record, schemaVersion: 1 }))).toBeNull(); + } finally { + lease.release(); + } + }); + + test('persists evidence before the record commit and never leaves a false record on record-write failure', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + const recordPath = join(fx.genieHome, '.codex-plugin-delivery-record.json'); + mkdirSync(recordPath); + const store = openCodexActivationStore({ genieHome: fx.genieHome }); + const lease = heldLease(fx.genieHome); + try { + expect(() => + store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }), + ).toThrow(); + expect(readdirSync(join(fx.genieHome, '.codex-delivery-evidence-v1'))).toHaveLength(1); + expect(lstatSync(recordPath).isDirectory()).toBe(true); + } finally { + lease.release(); + } + }); + + test('a stored-pack tamper turns an otherwise valid record into an invalid delivery observation', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + const store = openCodexActivationStore({ genieHome: fx.genieHome }); + const lease = heldLease(fx.genieHome); + try { + const record = store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }); + const descriptorPath = join( + fx.genieHome, + '.codex-delivery-evidence-v1', + record.evidenceDigest, + 'descriptor.json', + ); + writeFileSync(descriptorPath, `${readFileSync(descriptorPath, 'utf8')} `, { mode: 0o600 }); + expect(store.observe().delivery.status).toBe('invalid'); } finally { lease.release(); } @@ -887,9 +1046,8 @@ describe('CodexActivationStore — delivery + downgrade receipt', () => { const lease = heldLease(fx.genieHome); try { const record = store.publishDelivery(lease, { - targetVersion: T, - canonicalPayloadSha256: canonicalDigest(fx.genieHome), - channel: 'stable', + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, downgradeFrom: NEWER, }); const snapshot = store.observe(); @@ -914,9 +1072,8 @@ describe('CodexActivationStore — delivery + downgrade receipt', () => { try { expect(() => store.publishDelivery(lease, { - targetVersion: T, - canonicalPayloadSha256: DIGEST, - channel: 'stable', + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, downgradeFrom: OLD, }), ).toThrow(); @@ -939,9 +1096,8 @@ describe('CodexActivationStore — withRevalidatedDeliveryRoot', () => { let escaped: mod.DeliveryRootOps | null = null; try { store.publishDelivery(lease, { - targetVersion: T, - canonicalPayloadSha256: canonicalDigest(fx.genieHome), - channel: 'stable', + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, }); const version = store.withRevalidatedDeliveryRoot(lease, (ops) => { escaped = ops; @@ -951,12 +1107,114 @@ describe('CodexActivationStore — withRevalidatedDeliveryRoot', () => { expect(version).toBe(T); // An escaped capability throws once the callback has returned. expect(() => escaped?.inventoryDigest()).toThrow(); + expect(() => + escaped?.consume( + createCodexMarketplaceRegistrationConsumer({ command: 'codex', runner: listRunner({ stdout: '{}' }) }), + ), + ).toThrow('after the callback returned'); + } finally { + lease.release(); + } + }); + + test('a home-to-bin canonical-root swap is rejected before marketplace registration', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: T }]) }), + }); + const lease = heldLease(fx.genieHome); + let marketplaceCalls = 0; + try { + store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }); + expect(() => + store.withRevalidatedDeliveryRoot(lease, (ops) => { + const binRoot = join(fx.genieHome, 'bin'); + mkdirSync(binRoot, { recursive: true }); + renameSync(join(fx.genieHome, 'plugins'), join(binRoot, 'plugins')); + renameSync(join(fx.genieHome, 'VERSION'), join(binRoot, 'VERSION')); + renameSync(join(fx.genieHome, 'genie'), join(binRoot, 'genie')); + ops.consume( + createCodexMarketplaceRegistrationConsumer({ + command: 'codex', + runner: () => { + marketplaceCalls += 1; + return { exitCode: 0, stdout: '{}', stderr: '' }; + }, + }), + ); + }), + ).toThrow(); + expect(marketplaceCalls).toBe(0); + } finally { + lease.release(); + } + }); + + test('an unsigned payload-tree replacement is rejected before role writes or adoption', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: T }]) }), + }); + const lease = heldLease(fx.genieHome); + try { + store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }); + persistIntegrationConsent('codex', fx.genieHome); + expect(() => + store.withRevalidatedDeliveryRoot(lease, (ops) => { + const payload = join(fx.genieHome, 'plugins', 'genie'); + rmSync(payload, { recursive: true, force: true }); + writeFiles(payload, { + 'codex-agents/genie-reviewer.toml': '# genie-managed-codex-agent\nname = "unsigned"\n', + 'plugin.json': '{"name":"unsigned"}\n', + }); + ops.consume(createSetupCodexRoleAgentConsumer({ genieHome: fx.genieHome, codexHome: fx.codexHome })); + }), + ).toThrow(); + expect(existsSync(join(fx.codexHome, 'agents'))).toBe(false); + } finally { + lease.release(); + } + }); + + test('delivery drift is rejected before the narrow consent consumer can commit state', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: T }]) }), + }); + const lease = heldLease(fx.genieHome); + try { + store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }); + expect(() => + store.withRevalidatedDeliveryRoot(lease, (ops) => { + writeFileSync(join(fx.genieHome, 'plugins', 'genie', 'plugin.json'), '{"name":"drifted"}\n'); + ops.consume(createSetupCodexConsentCommitConsumer({ genieHome: fx.genieHome, selection: 'codex' })); + }), + ).toThrow(); + expect(existsSync(join(fx.genieHome, '.integration-consent.json'))).toBe(false); } finally { lease.release(); } }); - test('rejects a delivery record whose digest no longer matches the payload', () => { + test('consumer capabilities reject structural forgery and one-shot replay without duplicate effects', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); const store = openCodexActivationStore({ genieHome: fx.genieHome, @@ -965,9 +1223,51 @@ describe('CodexActivationStore — withRevalidatedDeliveryRoot', () => { runner: listRunner({ stdout: pluginListJson([{ version: T }]) }), }); const lease = heldLease(fx.genieHome); + let marketplaceCalls = 0; + let consumed: ReturnType | null = null; try { - store.publishDelivery(lease, { targetVersion: T, canonicalPayloadSha256: DIGEST, channel: 'stable' }); // wrong digest - expect(() => store.withRevalidatedDeliveryRoot(lease, (ops) => ops.deliveredVersion())).toThrow(); + store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }); + store.withRevalidatedDeliveryRoot(lease, (ops) => { + expect(() => ops.consume(Object.freeze({}) as never)).toThrow('unrecognized delivery-root consumer'); + consumed = createCodexMarketplaceRegistrationConsumer({ + command: 'codex', + runner: () => { + marketplaceCalls += 1; + return { exitCode: 0, stdout: '{}', stderr: '' }; + }, + }); + expect(ops.consume(consumed)).toBeUndefined(); + expect(() => ops.consume(consumed as never)).toThrow('unrecognized delivery-root consumer'); + }); + expect(marketplaceCalls).toBe(1); + expect(() => runDeliveryRootConsumer(consumed as never, fx.genieHome)).toThrow( + 'unrecognized delivery-root consumer', + ); + } finally { + lease.release(); + } + }); + + test('rejects evidence whose digest does not match the physical delivery root before publication', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: T, sameBytes: true }); + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: T }]) }), + }); + const lease = heldLease(fx.genieHome); + try { + expect(() => + store.publishDelivery(lease, { + evidence: deliveryEvidence(T, fx.genieHome, { canonicalPayloadSha256: DIGEST }), + deliveryRoot: fx.genieHome, + }), + ).toThrow('payload does not match evidence'); + expect(existsSync(join(fx.genieHome, '.codex-plugin-delivery-record.json'))).toBe(false); } finally { lease.release(); } @@ -1011,6 +1311,7 @@ describe('CodexActivationStore — beginActivation fingerprint binding', () => { test('a fresh permit begins a planned transaction fenced by the lease operation id', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + publishMatchingDelivery(fx.genieHome, fx.codexHome); const store = openCodexActivationStore({ genieHome: fx.genieHome, codexHome: fx.codexHome, @@ -1034,6 +1335,38 @@ describe('CodexActivationStore — beginActivation fingerprint binding', () => { } }); + test('a fresh absent registration plans enabled=true, while an existing disabled registration stays false', () => { + for (const fixtureCase of [ + { name: 'fresh', registeredVersion: null, entries: [], expectedEnabled: true }, + { + name: 'existing-disabled', + registeredVersion: OLD, + entries: [{ version: OLD, enabled: false }], + expectedEnabled: false, + }, + ] as const) { + const fx = makeFixture({ targetVersion: T, registeredVersion: fixtureCase.registeredVersion }); + publishMatchingDelivery(fx.genieHome, fx.codexHome); + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([...fixtureCase.entries]) }), + }); + const lease = heldLease(fx.genieHome); + try { + const result = store.beginActivation(lease, grantPermit(store)); + expect(result.status).toBe('started'); + const observed = store.observe(); + expect(observed.intent.status).toBe('valid'); + if (observed.intent.status !== 'valid') throw new Error(`${fixtureCase.name}: intent missing`); + expect(observed.intent.intent.priorEnabled).toBe(fixtureCase.expectedEnabled); + } finally { + lease.release(); + } + } + }); + test('a stale permit is detected on re-observation and performs zero mutation', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); const store = openCodexActivationStore({ @@ -1057,6 +1390,123 @@ describe('CodexActivationStore — beginActivation fingerprint binding', () => { } }); + const deliveryTupleTampers: Array<{ + name: string; + mutate(record: Record): void; + }> = [ + { + name: 'target version', + mutate: (record) => { + record.targetVersion = NEWER; + record.releaseTag = `v${NEWER}`; + record.releaseName = `genie-${NEWER}-test.tar.gz`; + }, + }, + { + name: 'payload digest', + mutate: (record) => { + record.canonicalPayloadSha256 = '1'.repeat(64); + }, + }, + { + name: 'channel', + mutate: (record) => { + record.channel = 'homolog'; + }, + }, + { + name: 'delivery id', + mutate: (record) => { + record.deliveryId = '2'.repeat(32); + }, + }, + { + name: 'evidence digest', + mutate: (record) => { + record.evidenceDigest = '2'.repeat(64); + }, + }, + { + name: 'platform id', + mutate: (record) => { + record.platformId = record.platformId === 'darwin-arm64' ? 'linux-arm64' : 'darwin-arm64'; + }, + }, + { + name: 'platform', + mutate: (record) => { + record.platformTriple = `${String(record.platformTriple)}-stale`; + }, + }, + { + name: 'release tag', + mutate: (record) => { + record.releaseTag = `v${NEWER}`; + }, + }, + { + name: 'release name', + mutate: (record) => { + record.releaseName = `genie-${T}-linux-x64.tar.gz`; + }, + }, + { + name: 'manifest digest', + mutate: (record) => { + record.releaseManifestSha256 = '3'.repeat(64); + }, + }, + { + name: 'artifact digest', + mutate: (record) => { + record.artifactSha256 = '4'.repeat(64); + }, + }, + { + name: 'binary digest', + mutate: (record) => { + record.installedBinarySha256 = '5'.repeat(64); + }, + }, + { + name: 'delivery root', + mutate: (record) => { + record.deliveryRoot = '/tmp/other-delivery'; + }, + }, + { + name: 'publication time', + mutate: (record) => { + record.deliveredAt = '2026-07-24T00:00:00.000Z'; + }, + }, + ]; + + for (const tamper of deliveryTupleTampers) { + test(`a genuine permit rejects ${tamper.name} mutation before the first write`, () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + publishMatchingDelivery(fx.genieHome, fx.codexHome); + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: OLD }]) }), + }); + const permit = grantPermit(store); + const recordPath = join(fx.genieHome, '.codex-plugin-delivery-record.json'); + const record = JSON.parse(readFileSync(recordPath, 'utf8')) as Record; + tamper.mutate(record); + writeFileSync(recordPath, `${JSON.stringify(record, null, 2)}\n`); + const lease = heldLease(fx.genieHome); + try { + expect(store.beginActivation(lease, permit).status).not.toBe('started'); + expect(existsSync(join(fx.genieHome, '.codex-plugin-refresh-intent.json'))).toBe(false); + } finally { + lease.release(); + } + }); + } + test('a forged permit is refused with zero mutation', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); const store = openCodexActivationStore({ @@ -1082,8 +1532,74 @@ describe('CodexActivationStore — beginActivation fingerprint binding', () => { } }); + // Group B inner guard (deliverable 5): a genuine, fresh permit still cannot open + // the transaction without a matching authenticated delivery record. + test('an absent delivery record refuses as delivery-incomplete and writes no journal', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + // Intentionally NO publishMatchingDelivery. + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: OLD }]) }), + }); + const permit = grantPermit(store); + const lease = heldLease(fx.genieHome); + try { + const result = store.beginActivation(lease, permit); + expect(result.status).toBe('delivery-incomplete'); + if (result.status === 'delivery-incomplete') expect(result.assessment).toBe('absent'); + expect(existsSync(join(fx.genieHome, '.codex-plugin-refresh-intent.json'))).toBe(false); + } finally { + lease.release(); + } + }); + + // A record/evidence mix cannot bypass the inner guard: the fingerprint is + // independently derived from verified evidence while the pure assessment + // still compares every actual record field immediately before the write. + test('a record/evidence mix refuses as delivery-incomplete even when the evidence fingerprint matches', () => { + const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + const deliveryLease = acquireLifecycleLease('update-delivery', { genieHome: fx.genieHome }); + if (!deliveryLease.ok) throw new Error('could not acquire delivery lease'); + try { + openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: '{}' }), + }).publishDelivery(deliveryLease, { + evidence: deliveryEvidence(T, fx.genieHome), + deliveryRoot: fx.genieHome, + }); + } finally { + deliveryLease.release(); + } + const deliveryPath = join(fx.genieHome, '.codex-plugin-delivery-record.json'); + const mixed = JSON.parse(readFileSync(deliveryPath, 'utf8')) as mod.DeliveryRecord; + mixed.artifactSha256 = 'f'.repeat(64); + writeFileSync(deliveryPath, `${JSON.stringify(mixed, null, 2)}\n`); + const store = openCodexActivationStore({ + genieHome: fx.genieHome, + codexHome: fx.codexHome, + command: 'codex', + runner: listRunner({ stdout: pluginListJson([{ version: OLD }]) }), + }); + const permit = grantPermit(store); // fingerprint binds the record's delivery id + const lease = heldLease(fx.genieHome); + try { + const result = store.beginActivation(lease, permit); + expect(result.status).toBe('delivery-incomplete'); + if (result.status === 'delivery-incomplete') expect(result.assessment).toBe('mismatch'); + expect(existsSync(join(fx.genieHome, '.codex-plugin-refresh-intent.json'))).toBe(false); + } finally { + lease.release(); + } + }); + test('advance then finalize deletes the intent (crash-safe delete order)', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + publishMatchingDelivery(fx.genieHome, fx.codexHome); const store = openCodexActivationStore({ genieHome: fx.genieHome, codexHome: fx.codexHome, @@ -1107,6 +1623,7 @@ describe('CodexActivationStore — beginActivation fingerprint binding', () => { test('a superseded lease operation id fences an intent advance', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + publishMatchingDelivery(fx.genieHome, fx.codexHome); const store = openCodexActivationStore({ genieHome: fx.genieHome, codexHome: fx.codexHome, @@ -1132,6 +1649,7 @@ describe('CodexActivationStore — beginActivation fingerprint binding', () => { for (const phase of POST_COMMAND_PHASES) { test(`a fresh assertion resumes an intent-${phase} journal (same id, preserved phase)`, () => { const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + publishMatchingDelivery(fx.genieHome, fx.codexHome); const store = openCodexActivationStore({ genieHome: fx.genieHome, codexHome: fx.codexHome, @@ -1173,6 +1691,7 @@ describe('CodexActivationStore — beginActivation fingerprint binding', () => { test('a stale permit on a resumed post-command phase refuses and leaves the journal untouched', () => { const fx = makeFixture({ targetVersion: T, registeredVersion: OLD }); + publishMatchingDelivery(fx.genieHome, fx.codexHome); const store = openCodexActivationStore({ genieHome: fx.genieHome, codexHome: fx.codexHome, @@ -1370,4 +1889,26 @@ describe('module surface', () => { expect(stripControl('red')).toBe('red'); expect(stripControl(']0;titlebody')).toBe('body'); }); + + test('the generic consumer factory is private and only the deep store calls the internal bridge', () => { + expect((runtimeMod as Record).createDeliveryRootConsumer).toBeUndefined(); + const sourceRoot = join(import.meta.dir, '..'); + const pending = [sourceRoot]; + const productionSources: string[] = []; + while (pending.length > 0) { + const current = pending.pop(); + if (current === undefined) break; + for (const entry of readdirSync(current, { withFileTypes: true })) { + const path = join(current, entry.name); + if (entry.isDirectory()) pending.push(path); + else if (entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts')) productionSources.push(path); + } + } + const callers = productionSources + .filter((path) => !path.endsWith('/runtime-integrations.ts')) + .filter((path) => readFileSync(path, 'utf8').includes('runDeliveryRootConsumer')) + .map((path) => path.slice(sourceRoot.length + 1)) + .sort(); + expect(callers).toEqual(['lib/codex-activation.ts']); + }); }); diff --git a/src/lib/codex-activation.ts b/src/lib/codex-activation.ts index 8e0b48aa4..239c607e6 100644 --- a/src/lib/codex-activation.ts +++ b/src/lib/codex-activation.ts @@ -1,8 +1,9 @@ /** * Codex plugin activation protocol — the pure, fail-closed core. * - * `genie update` can advance and prune a versioned Codex plugin cache while an - * open Codex task still holds paths into the old generation. This module puts + * `genie update` can deliver a new canonical generation while an open Codex + * task still holds paths into the active old generation. A later authenticated + * `genie setup --codex` owns cache activation and pruning. This module puts * every activation-capable entry path behind three explicit layers: * * 1. `observeCodexActivation()` performs only bounded reads and returns a @@ -37,44 +38,57 @@ import { renameNonOverwriting, unlinkWithParentFsync, } from './codex-activation-persistence.js'; +import { + type AuthenticatedDeliveryBinding, + type DeliveryEvidenceVerificationDependencies, + type PersistedAuthenticatedDeliveryRecord, + type VerifiedDeliveryEvidence, + type VerifiedDeliveryEvidenceFacts, + authenticatedDeliveryBindingDigest, + authenticatedDeliveryBindingFromRecord, + authenticatedDeliveryRecordFields, + createAuthenticatedDeliveryBinding, + encodeAuthenticatedDeliveryRecord, + observePersistedDeliveryEvidence, + parseAuthenticatedDeliveryRecord, + persistVerifiedDeliveryEvidence, + verifiedDeliveryEvidenceFacts, +} from './codex-delivery-evidence.js'; +// Group B (host-observation-attestation): the single pure delivery-attestation +// classifier + its typed result. Imported here so the inner activation guard +// (`beginActivationImpl`) applies exactly the same assessment the standalone +// contract test exercises. `codex-host-observation.ts` delegates tuple validation +// to the evidence-owned codec and never imports this module, so this dependency +// is one-directional (no import cycle). +import { + type DeliveryAssessment, + type DeliveryRecordReadState, + assessAuthenticatedDelivery, +} from './codex-host-observation.js'; import { type HeldLifecycleLease, LifecycleFencingError } from './codex-lifecycle-lease.js'; +import { + type ParsedReleaseVersion, + compareReleaseVersions, + parseReleaseVersion, + stripControl, +} from './codex-release-version.js'; import { resolveCodexDir, resolveGenieHome } from './genie-home.js'; -import { type CommandRunner, parseCodexPluginState, runBoundedIntegrationCommand } from './runtime-integrations.js'; +import { + type CommandRunner, + type DeliveryRootConsumer, + parseCodexPluginState, + runBoundedIntegrationCommand, + runDeliveryRootConsumer, +} from './runtime-integrations.js'; // ============================================================================ // Release-version grammar and direction // ============================================================================ -/** Exact `MAJOR.YYMMDD.N` release grammar; build metadata is stripped only after a match. */ -const RELEASE_VERSION_RE = /^(\d+)\.(\d{6})\.(\d+)(?:\+[0-9A-Za-z][0-9A-Za-z.-]*)?$/; - -export interface ParsedReleaseVersion { - readonly major: number; - readonly ymd: number; - readonly n: number; - /** The `MAJOR.YYMMDD.N` triple with build metadata removed; used for equality. */ - readonly canonical: string; -} - -/** Parse a release version, returning null for anything that fails the exact grammar. */ -export function parseReleaseVersion(raw: unknown): ParsedReleaseVersion | null { - if (typeof raw !== 'string') return null; - const match = RELEASE_VERSION_RE.exec(raw); - if (!match) return null; - const major = Number(match[1]); - const ymd = Number(match[2]); - const n = Number(match[3]); - if (!Number.isSafeInteger(major) || !Number.isSafeInteger(ymd) || !Number.isSafeInteger(n)) return null; - return { major, ymd, n, canonical: `${major}.${match[2]}.${n}` }; -} - -/** Total numeric order over validated versions. */ -export function compareReleaseVersions(a: ParsedReleaseVersion, b: ParsedReleaseVersion): -1 | 0 | 1 { - if (a.major !== b.major) return a.major < b.major ? -1 : 1; - if (a.ymd !== b.ymd) return a.ymd < b.ymd ? -1 : 1; - if (a.n !== b.n) return a.n < b.n ? -1 : 1; - return 0; -} +// The pure `MAJOR.YYMMDD.N` grammar now lives in the leaf `codex-release-version.ts` +// (shared with `codex-host-observation.ts` without an import cycle). Re-exported +// here so every existing `from './codex-activation.js'` importer is unchanged. +export { type ParsedReleaseVersion, compareReleaseVersions, parseReleaseVersion, stripControl }; export type ActivationDirection = 'install' | 'upgrade' | 'downgrade' | 'repair'; @@ -133,15 +147,8 @@ export interface DowngradeReceipt { channel: string; } -export interface DeliveryRecord { - schemaVersion: 1; - /** The 128-bit delivery transaction id; a downgrade receipt copies it as its receiptId. */ - deliveryId: string; - targetVersion: string; - canonicalPayloadSha256: string; - channel: string; - deliveredAt: string; -} +/** Byte-compatible schema-2 record; its field set and codec live with delivery evidence. */ +export type DeliveryRecord = PersistedAuthenticatedDeliveryRecord; export interface ReceiptTombstone { schemaVersion: 1; @@ -230,34 +237,8 @@ export function parseDowngradeReceiptStructure(content: string): DowngradeReceip }; } -const DELIVERY_KEYS: ReadonlySet = new Set([ - 'schemaVersion', - 'deliveryId', - 'targetVersion', - 'canonicalPayloadSha256', - 'channel', - 'deliveredAt', -]); - export function parseDeliveryRecordStructure(content: string): DeliveryRecord | null { - const parsed = safeJson(content); - if (!isPlainObject(parsed)) return null; - if (Object.keys(parsed).some((key) => !DELIVERY_KEYS.has(key))) return null; - const record = parsed; - if (record.schemaVersion !== 1) return null; - if (!isHex128(record.deliveryId)) return null; - if (parseReleaseVersion(record.targetVersion) === null) return null; - if (!isHex256(record.canonicalPayloadSha256)) return null; - if (typeof record.channel !== 'string' || record.channel.length === 0 || record.channel.length > 128) return null; - if (typeof record.deliveredAt !== 'string' || record.deliveredAt.length === 0) return null; - return { - schemaVersion: 1, - deliveryId: record.deliveryId as string, - targetVersion: record.targetVersion as string, - canonicalPayloadSha256: record.canonicalPayloadSha256 as string, - channel: record.channel as string, - deliveredAt: record.deliveredAt as string, - }; + return parseAuthenticatedDeliveryRecord(content); } const TOMBSTONE_KEYS: ReadonlySet = new Set(['schemaVersion', 'receiptId', 'consumedAt', 'operationId']); @@ -297,7 +278,15 @@ export type RegistrationFact = export type QueryFact = { status: 'failed'; detail: string } | { status: 'ok'; registration: RegistrationFact }; export type CanonicalFact = - | { status: 'ok'; version: ParsedReleaseVersion; digest: string; identity: string } + | { + status: 'ok'; + version: ParsedReleaseVersion; + digest: string; + identity: string; + deliveryRoot: string; + installedBinarySha256: string; + platformTriple: string; + } | { status: 'error'; detail: string }; export type PhysicalCacheFact = @@ -315,7 +304,7 @@ export type ReceiptFact = export type DeliveryFact = | { status: 'absent' } | { status: 'invalid'; detail: string } - | { status: 'present'; record: DeliveryRecord }; + | { status: 'present'; record: DeliveryRecord; evidence: VerifiedDeliveryEvidenceFacts }; export type IntentFact = | { status: 'absent' } @@ -601,7 +590,13 @@ const RETIRE_RECOVERY = 'retire tasks → genie setup --codex → /hooks → new export function describeState(state: CodexActivationState): StateDescriptor { switch (state.kind) { case 'query-failed': - return desc('query-failed', 1, 'none', true, 'Indeterminate; repair the Codex CLI/query, then rerun doctor'); + return desc( + 'query-failed', + 1, + 'none', + true, + 'Indeterminate; upgrade or repair the Codex CLI/query, then rerun doctor', + ); case 'registration-version-invalid': return desc( 'registration-version-invalid', @@ -817,10 +812,13 @@ export interface ActivationRequestFingerprint { observedTarget: string | null; canonicalPayloadSha256: string | null; installedDeliveryDigest: string | null; - deliveryId: string | null; + /** Commits the complete verified descriptor plus publication transaction/root. */ + deliveryBindingDigest: string | null; registrationIdentity: string | null; cacheIdentity: string | null; enabled: boolean | null; + /** Digest of the exact valid journal bytes, binding every durable intent field. */ + intentContentSha256: string | null; intentPhase: IntentPhase | null; intentId: string | null; receiptId: string | null; @@ -832,15 +830,24 @@ const FINGERPRINT_FIELDS: readonly (keyof ActivationRequestFingerprint)[] = [ 'observedTarget', 'canonicalPayloadSha256', 'installedDeliveryDigest', - 'deliveryId', + 'deliveryBindingDigest', 'registrationIdentity', 'cacheIdentity', 'enabled', + 'intentContentSha256', 'intentPhase', 'intentId', 'receiptId', ]; +function authenticatedDeliveryBindingForSnapshot( + snapshot: CodexActivationSnapshot, + canonical: Extract | null, +): AuthenticatedDeliveryBinding | null { + if (snapshot.delivery.status !== 'present' || canonical === null) return null; + return createAuthenticatedDeliveryBinding(snapshot.delivery.evidence, canonical.deliveryRoot); +} + export function computeActivationFingerprint(snapshot: CodexActivationSnapshot): ActivationRequestFingerprint { const registration = registrationOf(snapshot); const registered = registration.present && registration.version ? registration.version.canonical : null; @@ -848,15 +855,17 @@ export function computeActivationFingerprint(snapshot: CodexActivationSnapshot): const cache = snapshot.cache; const intent = snapshot.intent; const family = snapshot.observationWitness.after; + const binding = authenticatedDeliveryBindingForSnapshot(snapshot, canonical); return { observedFrom: registered, observedTarget: canonical ? canonical.version.canonical : null, canonicalPayloadSha256: canonical ? canonical.digest : null, installedDeliveryDigest: cache.kind === 'present' ? cache.digest : null, - deliveryId: snapshot.delivery.status === 'present' ? snapshot.delivery.record.deliveryId : null, + deliveryBindingDigest: binding === null ? null : authenticatedDeliveryBindingDigest(binding), registrationIdentity: cache.kind === 'present' ? cache.identity : null, cacheIdentity: family.status === 'present' ? family.identity : null, enabled: registration.present ? registration.enabled : null, + intentContentSha256: intent.status === 'valid' ? intent.contentSha256 : null, intentPhase: intent.status === 'valid' ? intent.intent.phase : null, intentId: intent.status === 'valid' ? intent.intent.refreshIntentId : null, receiptId: snapshot.receipt.status === 'present' ? snapshot.receipt.receipt.receiptId : null, @@ -1035,6 +1044,8 @@ export interface ObserveOptions { /** TEST-ONLY canonical payload root override; production refuses caller/env roots. */ canonicalRoot?: string; allowRootOverride?: boolean; + /** TEST-ONLY cryptographic seam; production always uses embedded offline trust material. */ + deliveryEvidenceVerification?: DeliveryEvidenceVerificationDependencies; now?: () => Date; } @@ -1050,7 +1061,7 @@ export function observeCodexActivation(options: ObserveOptions = {}): CodexActiv const after = witnessFamily(familyDir); const cache = observeCache(query, codexHome); const receipt = observeReceipt(genieHome); - const delivery = observeDelivery(genieHome); + const delivery = observeDelivery(genieHome, options.deliveryEvidenceVerification); const receiptConsumed = receipt.status === 'present' && isReceiptConsumed(genieHome, receipt.receipt.receiptId); const intent = observeIntent(genieHome); return { @@ -1074,7 +1085,25 @@ function observeCanonical(options: ObserveOptions): CanonicalFact { if (tree.status !== 'ok') return { status: 'error', detail: tree.detail ?? `payload ${tree.status}` }; const version = parseReleaseVersion(readTrimmed(join(rootResult.root, 'VERSION'))); if (version === null) return { status: 'error', detail: 'canonical VERSION is missing or fails the release grammar' }; - return { status: 'ok', version, digest: tree.digest ?? '', identity: tree.identity ?? '' }; + const binaryPath = existsFile(join(rootResult.root, 'genie')) + ? join(rootResult.root, 'genie') + : join(rootResult.root, 'bin', 'genie'); + const installedBinarySha256 = hashPhysicalFile(binaryPath); + if (installedBinarySha256 === undefined) { + return { status: 'error', detail: 'canonical installed binary is missing, unsafe, or unreadable' }; + } + const deliveryRoot = safeRealpath(rootResult.root); + if (deliveryRoot === null) + return { status: 'error', detail: 'canonical delivery root cannot be resolved physically' }; + return { + status: 'ok', + version, + digest: tree.digest ?? '', + identity: tree.identity ?? '', + deliveryRoot, + installedBinarySha256, + platformTriple: `${process.platform}-${process.arch}`, + }; } /** @@ -1213,12 +1242,20 @@ function observeReceipt(genieHome: string): ReceiptFact { return receipt ? { status: 'present', receipt } : { status: 'invalid', detail: 'receipt failed schema-1 validation' }; } -function observeDelivery(genieHome: string): DeliveryFact { +function observeDelivery(genieHome: string, verification: DeliveryEvidenceVerificationDependencies = {}): DeliveryFact { const read = readBoundedRegularFile(deliveryRecordPath(genieHome), MAX_DELIVERY_BYTES); if (read.status === 'absent') return { status: 'absent' }; if (read.status !== 'ok') return { status: 'invalid', detail: `delivery ${read.status}` }; const record = parseDeliveryRecordStructure(read.content); - return record ? { status: 'present', record } : { status: 'invalid', detail: 'delivery failed schema-1 validation' }; + if (record === null) return { status: 'invalid', detail: 'delivery failed schema-2 validation' }; + const evidence = observePersistedDeliveryEvidence(genieHome, record.evidenceDigest, verification); + if (evidence.status === 'absent') { + return { status: 'invalid', detail: 'delivery evidence pack is absent' }; + } + if (evidence.status === 'invalid') { + return { status: 'invalid', detail: `delivery evidence pack is invalid: ${evidence.detail}` }; + } + return { status: 'present', record, evidence: evidence.facts }; } function isReceiptConsumed(genieHome: string, receiptId: string): boolean { @@ -1485,23 +1522,27 @@ export interface CodexActivationStoreOptions extends ObserveOptions { } export interface PublishDeliveryInput { - targetVersion: string; - canonicalPayloadSha256: string; - channel: string; + /** Opaque evidence minted only after complete offline signature/binding verification. */ + evidence: VerifiedDeliveryEvidence; + /** Installed delivery root; publication canonicalizes and re-hashes it before writing. */ + deliveryRoot: string; /** Present only for an explicit channel downgrade; writes the matching receipt. */ downgradeFrom?: string; - deliveryId?: string; - now?: () => Date; } export interface DeliveryRootOps { inventoryDigest(): string; deliveredVersion(): string; + consume(consumer: DeliveryRootConsumer): T; } export type BeginActivationResult = | { status: 'started'; handle: ActivationHandle } | { status: 'stale'; mismatchField: keyof ActivationRequestFingerprint; detail: string } + // Group B inner guard: a re-observed non-matching authenticated delivery record + // refuses the transaction BEFORE the first journal write, so no stale permit can + // advance activation-owned state without a matching record. + | { status: 'delivery-incomplete'; assessment: Exclude; detail: string } | { status: 'refused'; reason: string }; export interface ActivationHandle { @@ -1545,7 +1586,7 @@ export function openCodexActivationStore(options: CodexActivationStoreOptions = observe, publishDelivery(lease, input) { lease.assertOperation(lease.operationId); - return publishDeliveryImpl(deliveryPath, receiptPath, input); + return publishDeliveryImpl(genieHome, deliveryPath, receiptPath, input); }, withRevalidatedDeliveryRoot(lease, callback) { lease.assertOperation(lease.operationId); @@ -1566,33 +1607,34 @@ export function openCodexActivationStore(options: CodexActivationStoreOptions = }; } -function publishDeliveryImpl(deliveryPath: string, receiptPath: string, input: PublishDeliveryInput): DeliveryRecord { - const now = input.now ?? (() => new Date()); - const deliveryId = input.deliveryId ?? mint128(); - if (input.deliveryId !== undefined && !HEX_128_RE.test(input.deliveryId)) { - throw new Error('deliveryId must be 32 lowercase hex characters'); - } - const record: DeliveryRecord = { - schemaVersion: 1, - deliveryId, - targetVersion: input.targetVersion, - canonicalPayloadSha256: input.canonicalPayloadSha256, - channel: input.channel, - deliveredAt: now().toISOString(), - }; +function publishDeliveryImpl( + genieHome: string, + deliveryPath: string, + receiptPath: string, + input: PublishDeliveryInput, +): DeliveryRecord { + const facts = verifiedDeliveryEvidenceFacts(input.evidence); + const descriptor = facts.descriptor; + const physicalDeliveryRoot = validatePublishedDeliveryRoot(input.deliveryRoot, facts); + const binding = createAuthenticatedDeliveryBinding(facts, physicalDeliveryRoot); + + // Evidence is the durable prerequisite. The delivery record remains the last + // commit point, so no record can name bytes that publication did not persist. + persistVerifiedDeliveryEvidence(genieHome, input.evidence); + const record = encodeAuthenticatedDeliveryRecord(binding); if (input.downgradeFrom !== undefined) { const from = parseReleaseVersion(input.downgradeFrom); - const target = parseReleaseVersion(input.targetVersion); + const target = parseReleaseVersion(descriptor.version); if (from === null || target === null || compareReleaseVersions(from, target) <= 0) { throw new Error('downgrade delivery requires from > target with valid release versions'); } const receipt: DowngradeReceipt = { schemaVersion: 1, - receiptId: deliveryId, + receiptId: binding.deliveryId, fromPluginVersion: input.downgradeFrom, - targetVersion: input.targetVersion, - canonicalPayloadSha256: input.canonicalPayloadSha256, - channel: input.channel, + targetVersion: descriptor.version, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, + channel: descriptor.channel, }; atomicWriteFileSync(receiptPath, `${JSON.stringify(receipt, null, 2)}\n`, { backup: true }); } else { @@ -1603,40 +1645,191 @@ function publishDeliveryImpl(deliveryPath: string, receiptPath: string, input: P return record; } +function validatePublishedDeliveryRoot(deliveryRoot: string, evidence: VerifiedDeliveryEvidenceFacts): string { + if (!isAbsolute(deliveryRoot)) throw new Error('delivery root must be absolute'); + let physicalDeliveryRoot: string; + try { + physicalDeliveryRoot = realpathSync(deliveryRoot); + } catch (error) { + throw new Error(`delivery root cannot be resolved physically: ${errorText(error)}`); + } + const descriptor = evidence.descriptor; + const version = parseReleaseVersion(readTrimmed(join(physicalDeliveryRoot, 'VERSION'))); + if (version?.canonical !== descriptor.version) throw new Error('delivery root VERSION does not match evidence'); + const tree = scanPhysicalTree(join(physicalDeliveryRoot, 'plugins', 'genie')); + if (tree.status !== 'ok' || tree.digest !== descriptor.canonicalPayloadSha256) { + throw new Error('delivery root payload does not match evidence'); + } + const binaryPath = existsFile(join(physicalDeliveryRoot, 'genie')) + ? join(physicalDeliveryRoot, 'genie') + : join(physicalDeliveryRoot, 'bin', 'genie'); + if (hashPhysicalFile(binaryPath) !== descriptor.installedBinarySha256) { + throw new Error('delivery root binary does not match evidence'); + } + if (`${process.platform}-${process.arch}` !== descriptor.platformTriple) { + throw new Error('delivery evidence platform does not match the current runtime'); + } + return physicalDeliveryRoot; +} + function withRevalidatedDeliveryRootImpl( observeOptions: ObserveOptions, deliveryPath: string, callback: (ops: DeliveryRootOps) => T, ): T { + const admitted = revalidateDeliveryRootState(observeOptions, deliveryPath); + let live = true; + const assertLive = (): void => { + if (!live) throw new Error('delivery root capability used after the callback returned'); + }; + const ops: DeliveryRootOps = { + inventoryDigest(): string { + assertLive(); + return admitted.payloadDigest; + }, + deliveredVersion(): string { + assertLive(); + return admitted.version; + }, + consume(consumer: DeliveryRootConsumer): Result { + assertLive(); + assertSameDeliveryRootState(admitted, revalidateDeliveryRootState(observeOptions, deliveryPath), 'before'); + + let result: Result | undefined; + let consumerFailure: unknown; + let consumerFailed = false; + try { + result = runDeliveryRootConsumer(consumer, admitted.physicalRoot); + } catch (error) { + consumerFailed = true; + consumerFailure = error; + } + + assertSameDeliveryRootState(admitted, revalidateDeliveryRootState(observeOptions, deliveryPath), 'while'); + if (consumerFailed) throw consumerFailure; + return result as Result; + }, + }; + try { + return callback(ops); + } finally { + live = false; + } +} + +interface RevalidatedDeliveryRootState { + physicalRoot: string; + rootIdentity: string; + payloadDigest: string; + payloadIdentity: string; + version: string; + versionIdentity: string; + binaryRelativePath: 'genie' | 'bin/genie'; + installedBinarySha256: string; + binaryIdentity: string; + deliveryRecordDigest: string; + deliveryRecordIdentity: string; + deliveryId: string; + evidenceDigest: string; +} + +/** + * Recompute every physical and authenticated binding used to admit a privileged + * delivery-root consumer. This is deliberately the same full check at callback + * entry and immediately around every consumer invocation. + */ +function revalidateDeliveryRootState( + observeOptions: ObserveOptions, + deliveryPath: string, +): RevalidatedDeliveryRootState { const rootResult = resolveCanonicalRoot(observeOptions); if ('error' in rootResult) throw new Error(`delivery root revalidation failed: ${rootResult.error}`); const payloadDir = join(rootResult.root, 'plugins', 'genie'); const tree = scanPhysicalTree(payloadDir); if (tree.status !== 'ok') throw new Error(`delivery payload is not a safe physical tree: ${tree.status}`); - const version = parseReleaseVersion(readTrimmed(join(rootResult.root, 'VERSION'))); + const versionPath = join(rootResult.root, 'VERSION'); + const version = parseReleaseVersion(readTrimmed(versionPath)); if (version === null) throw new Error('delivery root VERSION fails the release grammar'); const deliveryRead = readBoundedRegularFile(deliveryPath, MAX_DELIVERY_BYTES); if (deliveryRead.status !== 'ok') throw new Error(`delivery record is not readable: ${deliveryRead.status}`); const record = parseDeliveryRecordStructure(deliveryRead.content); - if (record === null) throw new Error('delivery record failed schema-1 validation'); - if (record.canonicalPayloadSha256 !== tree.digest) throw new Error('delivery digest no longer matches the payload'); + if (record === null) throw new Error('delivery record failed schema-2 validation'); + const genieHome = observeOptions.genieHome ?? resolveGenieHome(); + const evidence = observePersistedDeliveryEvidence( + genieHome, + record.evidenceDigest, + observeOptions.deliveryEvidenceVerification, + ); + if (evidence.status !== 'present') throw new Error(`delivery evidence is ${evidence.status}`); + const physicalRoot = safeRealpath(rootResult.root); + if (physicalRoot === null) throw new Error('delivery root cannot be resolved physically'); + const binaryRelativePath = existsFile(join(physicalRoot, 'genie')) ? 'genie' : 'bin/genie'; + const binaryPath = join(physicalRoot, ...binaryRelativePath.split('/')); + const installedBinarySha256 = hashPhysicalFile(binaryPath); + if (installedBinarySha256 === undefined) throw new Error('delivery binary cannot be hashed safely'); + const descriptor = evidence.facts.descriptor; + if ( + descriptor.version !== version.canonical || + descriptor.canonicalPayloadSha256 !== tree.digest || + descriptor.installedBinarySha256 !== installedBinarySha256 || + descriptor.platformTriple !== `${process.platform}-${process.arch}` + ) { + throw new Error('verified delivery evidence no longer matches the physical delivery root'); + } + const binding = authenticatedDeliveryBindingFromRecord(record, evidence.facts, physicalRoot); + if (binding === null) throw new Error('delivery record no longer matches verified evidence'); if (record.targetVersion !== version.canonical) throw new Error('delivery version no longer matches the payload'); - let live = true; - const ops: DeliveryRootOps = { - inventoryDigest(): string { - if (!live) throw new Error('delivery root capability used after the callback returned'); - return tree.digest ?? ''; - }, - deliveredVersion(): string { - if (!live) throw new Error('delivery root capability used after the callback returned'); - return version.canonical; - }, + const rootIdentity = physicalNodeIdentity(physicalRoot, 'directory'); + const versionIdentity = physicalNodeIdentity(versionPath, 'file'); + const binaryIdentity = physicalNodeIdentity(binaryPath, 'file'); + const deliveryRecordIdentity = physicalNodeIdentity(deliveryPath, 'file'); + if ( + rootIdentity === null || + tree.identity === undefined || + versionIdentity === null || + binaryIdentity === null || + deliveryRecordIdentity === null + ) { + throw new Error('delivery root physical identity could not be authenticated'); + } + return { + physicalRoot, + rootIdentity, + payloadDigest: tree.digest ?? '', + payloadIdentity: tree.identity, + version: version.canonical, + versionIdentity, + binaryRelativePath, + installedBinarySha256, + binaryIdentity, + deliveryRecordDigest: createHash('sha256').update(deliveryRead.content).digest('hex'), + deliveryRecordIdentity, + deliveryId: binding.deliveryId, + evidenceDigest: evidence.facts.evidenceDigest, }; +} + +function assertSameDeliveryRootState( + admitted: RevalidatedDeliveryRootState, + fresh: RevalidatedDeliveryRootState, + phase: 'before' | 'while', +): void { + for (const key of Object.keys(admitted) as Array) { + if (admitted[key] !== fresh[key]) { + throw new Error(`delivery root changed ${phase} scoped consumer execution (${key})`); + } + } +} + +function physicalNodeIdentity(path: string, kind: 'directory' | 'file'): string | null { try { - return callback(ops); - } finally { - live = false; + const stat = lstatSync(path); + if (stat.isSymbolicLink()) return null; + if (kind === 'directory' ? !stat.isDirectory() : !stat.isFile()) return null; + return `${stat.dev}:${stat.ino}`; + } catch { + return null; } } @@ -1657,6 +1850,19 @@ function beginActivationImpl( const state = classifyCodexActivation(snapshot); const resolved = resolveActivationIntent(state, snapshot, lease.operationId); if ('refused' in resolved) return { status: 'refused', reason: resolved.refused }; + // Group B inner guard (deliverable 5): immediately before the first journal + // write, re-assess the delivery record against THIS activation's intent. A + // non-matching record refuses with zero mutation — defense in depth so a stale + // permit (whose fingerprint bound the complete delivery value) still cannot advance + // activation-owned state without a re-observed matching authenticated record. + const assessment = assessActivationDelivery(snapshot, resolved.intent, permit.fingerprint); + if (assessment !== 'matching') { + return { + status: 'delivery-incomplete', + assessment, + detail: `activation refused before the first write: delivery record is ${assessment}`, + }; + } lease.assertOperation(lease.operationId); atomicWriteFileSync(intentPath, `${JSON.stringify(resolved.intent, null, 2)}\n`, { backup: true }); return { @@ -1671,13 +1877,49 @@ function beginActivationImpl( }; } +/** + * Apply Group B's pure authenticated-delivery assessment to the re-observed + * snapshot's delivery record against the complete tuple fingerprinted before + * consent. This keeps the release provenance, physical installed bindings, + * publication identity/time, and downgrade receipt binding immutable through + * the first activation-owned write. + */ +function assessActivationDelivery( + snapshot: CodexActivationSnapshot, + intent: RefreshIntent, + fingerprint: ActivationRequestFingerprint, +): DeliveryAssessment { + const delivery = deliveryReadState(snapshot.delivery); + if (delivery.status === 'absent') return 'absent'; + if (delivery.status === 'invalid') return 'invalid'; + if (snapshot.delivery.status !== 'present' || snapshot.canonical.status !== 'ok') return 'mismatch'; + const binding = createAuthenticatedDeliveryBinding(snapshot.delivery.evidence, snapshot.canonical.deliveryRoot); + if (fingerprint.deliveryBindingDigest !== authenticatedDeliveryBindingDigest(binding)) return 'mismatch'; + const expectation = authenticatedDeliveryRecordFields(binding); + if ( + intent.targetVersion !== expectation.targetVersion || + intent.canonicalPayloadSha256 !== expectation.canonicalPayloadSha256 + ) + return 'mismatch'; + if (intent.direction === 'downgrade' && intent.receiptId !== expectation.deliveryId) return 'mismatch'; + return assessAuthenticatedDelivery(delivery, expectation); +} + +/** Map the snapshot's delivery fact onto the assessment's read-state shape. */ +function deliveryReadState(fact: DeliveryFact): DeliveryRecordReadState { + if (fact.status === 'present') return { status: 'present', record: fact.record }; + if (fact.status === 'invalid') return { status: 'invalid', detail: fact.detail }; + return { status: 'absent' }; +} + /** * Decide the journal a permitted activation writes. A post-command phase RESUMES - * its existing bound journal — the same `refreshIntentId` and phase, re-stamped - * onto the current lease operation — so a fresh external assertion continues the - * interrupted transaction rather than opening a duplicate `planned` intent (the - * fingerprint match already proved the bound intentPhase/intentId are current). - * Every other eligible state opens a new planned transaction unchanged. + * its existing bound journal — the same `refreshIntentId`, phase, direction, + * prior-enabled choice, and receipt id, re-stamped onto the current lease operation + * — so a fresh external assertion continues the interrupted transaction rather than + * replanning from post-command live state. A planned target-current journal may + * reflect an external/operator activation before Genie's command began, so it still + * opens a newly derived planned transaction. */ function resolveActivationIntent( state: CodexActivationState, @@ -1689,13 +1931,15 @@ function resolveActivationIntent( return planIntentFromState(state, snapshot, operationId); } -/** The three post-command phases (per the DESIGN truth table) resume their bound journal. */ +/** Post-command states resume their bound journal, including when the target is already current. */ function resumeIntentForState(state: CodexActivationState): RefreshIntent | null { switch (state.kind) { case 'intent-command-started': case 'intent-removal-observed': case 'intent-ambiguous-absent': return state.intent; + case 'intent-target-current': + return state.intent.phase === 'planned' ? null : state.intent; default: return null; } @@ -1723,7 +1967,10 @@ function planIntentFromState( fromPluginVersion: from ? from.canonical : null, targetVersion: target.canonical, direction, - priorEnabled: registration.present ? registration.enabled : false, + // A fresh activation has no operator disablement to preserve: plugin add is + // expected to leave the newly selected integration enabled. Only an + // existing registration can carry an explicit disabled choice forward. + priorEnabled: registration.present ? registration.enabled : true, canonicalPayloadSha256: snapshot.canonical.digest, phase: 'planned', commandKind: 'codex-plugin-add', @@ -1820,12 +2067,6 @@ function sha256Hex(content: string): string { return createHash('sha256').update(content).digest('hex'); } -/** Strip ANSI CSI and OSC control sequences from modeled diagnostics/output. */ -export function stripControl(text: string): string { - // biome-ignore lint/suspicious/noControlCharactersInRegex: intentionally matching ESC/BEL control bytes. - return text.replace(/\][^]*(?:|\\)/g, '').replace(/\[[0-9;?]*[ -\/]*[@-~]/g, ''); -} - function safeJson(content: string): unknown { try { return JSON.parse(content); @@ -1863,6 +2104,21 @@ function existsDir(path: string): boolean { } } +function existsFile(path: string): boolean { + try { + const stat = lstatSync(path); + return stat.isFile() && !stat.isSymbolicLink(); + } catch { + return false; + } +} + +function hashPhysicalFile(path: string): string | undefined { + if (!existsFile(path)) return undefined; + const digest = hashFileBounded(path); + return HEX_256_RE.test(digest) ? digest : undefined; +} + function safeRealpath(path: string): string | null { try { return realpathSync(path); diff --git a/src/lib/codex-delivery-evidence.test-support.ts b/src/lib/codex-delivery-evidence.test-support.ts new file mode 100644 index 000000000..4f759bbdb --- /dev/null +++ b/src/lib/codex-delivery-evidence.test-support.ts @@ -0,0 +1,134 @@ +import { createHash } from 'node:crypto'; +import { + DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + DELIVERY_EVIDENCE_PREDICATE_TYPE, + DELIVERY_EVIDENCE_REPOSITORY, + type DeliveryEvidenceDescriptor, + type DeliveryEvidenceVerificationDependencies, + type VerifiedDeliveryEvidence, + type VerifyDownloadedDeliveryEvidenceInput, + verifyDownloadedDeliveryEvidence, +} from './codex-delivery-evidence.js'; + +const DEFAULT_VERSION = '5.260723.7'; +const DEFAULT_SHA = 'a'.repeat(40); +const DEFAULT_CONTROL_SHA = 'b'.repeat(40); +const DEFAULT_DIGEST = 'c'.repeat(64); +const DEFAULT_BINARY_DIGEST = 'd'.repeat(64); +const DEFAULT_PAYLOAD_DIGEST = 'e'.repeat(64); +const DEFAULT_INTEGRATED_TIME = '1753228800'; + +export interface TestDeliveryEvidencePack { + descriptor: DeliveryEvidenceDescriptor; + descriptorBytes: string; + bundleBytes: string; + manifestBytes: string; + input: VerifyDownloadedDeliveryEvidenceInput; + dependencies: DeliveryEvidenceVerificationDependencies; +} + +export interface TestDeliveryEvidenceOptions { + descriptor?: Partial; + manifest?: Record; + input?: Partial; + integratedTime?: string | number; +} + +/** Deterministic pack builder for tests that keeps the real non-crypto bindings live. */ +export function buildTestDeliveryEvidencePack(options: TestDeliveryEvidenceOptions = {}): TestDeliveryEvidencePack { + const platformId = + process.platform === 'darwin' ? 'darwin-arm64' : process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64-glibc'; + const platformTriple = `${process.platform}-${process.arch}`; + const version = options.descriptor?.version ?? DEFAULT_VERSION; + const channel = options.descriptor?.channel ?? 'stable'; + const releaseTag = options.descriptor?.releaseTag ?? `v${version}`; + const releaseName = options.descriptor?.releaseName ?? `genie-${version}-${platformId}.tar.gz`; + const manifest = { + schema_version: 1, + channel, + version, + released_at: '2025-07-23T00:00:00.000Z', + tarball_base: `https://github.com/${DELIVERY_EVIDENCE_REPOSITORY}/releases/download/${releaseTag}`, + platforms: [platformId], + ...options.manifest, + }; + const manifestBytes = `${JSON.stringify(manifest, null, 2)}\n`; + const descriptor: DeliveryEvidenceDescriptor = { + schemaVersion: 1, + repository: DELIVERY_EVIDENCE_REPOSITORY, + version, + channel, + platformId, + platformTriple, + releaseTag, + releaseName, + releaseManifestSha256: sha256(manifestBytes), + artifactSha256: DEFAULT_DIGEST, + installedBinarySha256: DEFAULT_BINARY_DIGEST, + canonicalPayloadSha256: DEFAULT_PAYLOAD_DIGEST, + sourceSha: DEFAULT_SHA, + sourceBranch: channel === 'stable' ? 'main' : channel, + sourceCiRunId: '123456789', + controlSha: DEFAULT_CONTROL_SHA, + digestAlgorithm: DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + ...options.descriptor, + }; + const descriptorBytes = `${JSON.stringify(descriptor, null, 2)}\n`; + const statement = { + _type: 'https://in-toto.io/Statement/v1', + subject: [ + { + name: 'delivery-evidence.json', + digest: { sha256: sha256(descriptorBytes) }, + }, + ], + predicateType: DELIVERY_EVIDENCE_PREDICATE_TYPE, + predicate: { schemaVersion: 1 }, + }; + const bundleBytes = JSON.stringify({ + mediaType: 'application/vnd.dev.sigstore.bundle.v0.3+json', + dsseEnvelope: { + payload: Buffer.from(JSON.stringify(statement), 'utf8').toString('base64'), + payloadType: 'application/vnd.in-toto+json', + signatures: [{ keyid: '', sig: 'AA==' }], + }, + verificationMaterial: { + certificate: { rawBytes: 'AA==' }, + tlogEntries: [], + }, + }); + const input: VerifyDownloadedDeliveryEvidenceInput = { + descriptorBytes, + bundleBytes, + manifestBytes, + targetVersion: descriptor.version, + channel: descriptor.channel, + platformId: descriptor.platformId, + platformTriple: descriptor.platformTriple, + releaseTag: descriptor.releaseTag, + releaseName: descriptor.releaseName, + artifactSha256: descriptor.artifactSha256, + installedBinarySha256: descriptor.installedBinarySha256, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, + ...options.input, + }; + const dependencies: DeliveryEvidenceVerificationDependencies = { + verifyBundle: () => ({ integratedTime: options.integratedTime ?? DEFAULT_INTEGRATED_TIME }), + }; + return { descriptor, descriptorBytes, bundleBytes, manifestBytes, input, dependencies }; +} + +export function mintTestDeliveryEvidence(options: TestDeliveryEvidenceOptions = {}): { + evidence: VerifiedDeliveryEvidence; + pack: TestDeliveryEvidencePack; +} { + const pack = buildTestDeliveryEvidencePack(options); + return { + evidence: verifyDownloadedDeliveryEvidence(pack.input, pack.dependencies), + pack, + }; +} + +function sha256(value: string): string { + return createHash('sha256').update(value).digest('hex'); +} diff --git a/src/lib/codex-delivery-evidence.test.ts b/src/lib/codex-delivery-evidence.test.ts new file mode 100644 index 000000000..9532abc88 --- /dev/null +++ b/src/lib/codex-delivery-evidence.test.ts @@ -0,0 +1,254 @@ +import { describe, expect, test } from 'bun:test'; +import { spawnSync } from 'node:child_process'; +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { + DELIVERY_EVIDENCE_WORKFLOW_IDENTITY, + DELIVERY_EVIDENCE_WORKFLOW_IDENTITY_PATTERN, + authenticatedDeliveryBindingDigest, + authenticatedDeliveryBindingFromRecord, + createAuthenticatedDeliveryBinding, + deriveDeliveryId, + encodeAuthenticatedDeliveryRecord, + observePersistedDeliveryEvidence, + parseAuthenticatedDeliveryRecord, + persistVerifiedDeliveryEvidence, + verifiedDeliveryEvidenceFacts, + verifyDownloadedDeliveryEvidence, +} from './codex-delivery-evidence.js'; +import { buildTestDeliveryEvidencePack, mintTestDeliveryEvidence } from './codex-delivery-evidence.test-support.js'; + +describe('Codex signed delivery evidence', () => { + test('matches only the exact release workflow identity accepted by Sigstore', () => { + const identityPolicy = new RegExp(DELIVERY_EVIDENCE_WORKFLOW_IDENTITY_PATTERN); + + expect(identityPolicy.test(DELIVERY_EVIDENCE_WORKFLOW_IDENTITY)).toBe(true); + expect(identityPolicy.test(`${DELIVERY_EVIDENCE_WORKFLOW_IDENTITY}-evil`)).toBe(false); + expect(identityPolicy.test(DELIVERY_EVIDENCE_WORKFLOW_IDENTITY.replace('github.com', 'githubXcom'))).toBe(false); + }); + + test('mints an opaque proof only after exact descriptor, manifest, statement, and caller bindings match', () => { + const { evidence, pack } = mintTestDeliveryEvidence(); + const facts = verifiedDeliveryEvidenceFacts(evidence); + + expect(facts.descriptor).toEqual(pack.descriptor); + expect(facts.evidenceDigest).toMatch(/^[0-9a-f]{64}$/); + expect(facts.deliveredAt).toBe('2025-07-23T00:00:00.000Z'); + }); + + test('rejects a structural lookalike at the opaque publication boundary', () => { + expect(() => verifiedDeliveryEvidenceFacts({} as never)).toThrow('was not minted by the verifier'); + }); + + test('binds every descriptor field through the DSSE subject digest', () => { + const pack = buildTestDeliveryEvidencePack(); + const descriptor = JSON.parse(pack.descriptorBytes) as Record; + + for (const key of Object.keys(descriptor)) { + const changed = { ...descriptor, [key]: mutate(descriptor[key]) }; + expect(() => + verifyDownloadedDeliveryEvidence( + { ...pack.input, descriptorBytes: `${JSON.stringify(changed, null, 2)}\n` }, + pack.dependencies, + ), + ).toThrow(); + } + }); + + test('rejects descriptor formatting changes because the statement binds exact bytes', () => { + const pack = buildTestDeliveryEvidencePack(); + expect(() => + verifyDownloadedDeliveryEvidence( + { ...pack.input, descriptorBytes: JSON.stringify(JSON.parse(pack.descriptorBytes)) }, + pack.dependencies, + ), + ).toThrow('exact descriptor bytes'); + }); + + test('rejects exact manifest-byte tampering even when parsed values are unchanged', () => { + const pack = buildTestDeliveryEvidencePack(); + expect(() => + verifyDownloadedDeliveryEvidence({ ...pack.input, manifestBytes: pack.manifestBytes.trim() }, pack.dependencies), + ).toThrow('exact fetched manifest bytes'); + }); + + test('rejects a bundle whose signed predicate type is changed', () => { + const pack = buildTestDeliveryEvidencePack(); + const bundle = JSON.parse(pack.bundleBytes) as { + dsseEnvelope: { payload: string }; + }; + const statement = JSON.parse(Buffer.from(bundle.dsseEnvelope.payload, 'base64').toString('utf8')) as { + predicateType: string; + }; + statement.predicateType = 'https://example.invalid/predicate'; + bundle.dsseEnvelope.payload = Buffer.from(JSON.stringify(statement), 'utf8').toString('base64'); + + expect(() => + verifyDownloadedDeliveryEvidence({ ...pack.input, bundleBytes: JSON.stringify(bundle) }, pack.dependencies), + ).toThrow('predicate type'); + }); + + test('production verification cannot accept the deterministic unsigned test bundle', () => { + const pack = buildTestDeliveryEvidencePack(); + expect(() => verifyDownloadedDeliveryEvidence(pack.input)).toThrow(); + }); + + test('persists exact bytes content-addressed and re-verifies them offline', () => { + const genieHome = createTempDir('delivery-evidence'); + try { + const { evidence, pack } = mintTestDeliveryEvidence(); + const persisted = persistVerifiedDeliveryEvidence(genieHome, evidence); + const observed = observePersistedDeliveryEvidence(genieHome, persisted.evidenceDigest, pack.dependencies); + + expect(observed.status).toBe('present'); + if (observed.status === 'present') { + expect(observed.facts).toEqual(persisted); + } + // Idempotency is exact-byte based. + expect(persistVerifiedDeliveryEvidence(genieHome, evidence)).toEqual(persisted); + } finally { + removeTempDir(genieHome); + } + }); + + test('fails closed on descriptor, bundle, and manifest tampering in stored packs', () => { + for (const file of ['descriptor.json', 'bundle.json', 'manifest.json']) { + const genieHome = createTempDir(`delivery-evidence-${file}`); + try { + const { evidence, pack } = mintTestDeliveryEvidence(); + const facts = persistVerifiedDeliveryEvidence(genieHome, evidence); + const path = join(genieHome, '.codex-delivery-evidence-v1', facts.evidenceDigest, file); + writeFileSync(path, `${readFileSync(path, 'utf8')} `, { mode: 0o600 }); + + const observed = observePersistedDeliveryEvidence(genieHome, facts.evidenceDigest, pack.dependencies); + expect(observed.status).toBe('invalid'); + } finally { + removeTempDir(genieHome); + } + } + }); + + test('fails closed on symlinked or non-private evidence storage', () => { + const genieHome = createTempDir('delivery-evidence-unsafe'); + const target = createTempDir('delivery-evidence-target'); + try { + symlinkSync(target, join(genieHome, '.codex-delivery-evidence-v1')); + expect(observePersistedDeliveryEvidence(genieHome, 'a'.repeat(64)).status).toBe('invalid'); + + const otherHome = createTempDir('delivery-evidence-mode'); + try { + mkdirSync(join(otherHome, '.codex-delivery-evidence-v1'), { mode: 0o700 }); + chmodSync(join(otherHome, '.codex-delivery-evidence-v1'), 0o755); + expect(observePersistedDeliveryEvidence(otherHome, 'a'.repeat(64)).status).toBe('invalid'); + } finally { + removeTempDir(otherHome); + } + } finally { + removeTempDir(genieHome); + removeTempDir(target); + } + }); + + test('derives a domain-separated stable 128-bit id from evidence and physical root', () => { + const a = deriveDeliveryId('a'.repeat(64), '/physical/a'); + expect(a).toMatch(/^[0-9a-f]{32}$/); + expect(deriveDeliveryId('a'.repeat(64), '/physical/a')).toBe(a); + expect(deriveDeliveryId('b'.repeat(64), '/physical/a')).not.toBe(a); + expect(deriveDeliveryId('a'.repeat(64), '/physical/b')).not.toBe(a); + }); + + test('round-trips the byte-compatible schema-2 record through the single binding codec', () => { + const { evidence } = mintTestDeliveryEvidence(); + const facts = verifiedDeliveryEvidenceFacts(evidence); + const binding = createAuthenticatedDeliveryBinding(facts, '/physical/a'); + const record = encodeAuthenticatedDeliveryRecord(binding); + + expect(Object.keys(record)).toEqual([ + 'schemaVersion', + 'deliveryId', + 'targetVersion', + 'canonicalPayloadSha256', + 'channel', + 'deliveredAt', + 'evidenceDigest', + 'platformId', + 'platformTriple', + 'releaseTag', + 'releaseName', + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'deliveryRoot', + ]); + expect(parseAuthenticatedDeliveryRecord(`${JSON.stringify(record, null, 2)}\n`)).toEqual(record); + expect(authenticatedDeliveryBindingFromRecord(record, facts, '/physical/a')).toEqual(binding); + expect(parseAuthenticatedDeliveryRecord(JSON.stringify({ ...record, schemaVersion: 1 }))).toBeNull(); + expect(parseAuthenticatedDeliveryRecord(JSON.stringify({ ...record, unknown: true }))).toBeNull(); + expect( + authenticatedDeliveryBindingFromRecord({ ...record, artifactSha256: 'f'.repeat(64) }, facts, '/physical/a'), + ).toBeNull(); + }); + + test('keeps binding creation closed under the record delivery-root limit', () => { + const facts = verifiedDeliveryEvidenceFacts(mintTestDeliveryEvidence().evidence); + const acceptedRoot = `/${'x'.repeat(255)}`; + + expect(acceptedRoot).toHaveLength(256); + expect( + parseAuthenticatedDeliveryRecord( + JSON.stringify(encodeAuthenticatedDeliveryRecord(createAuthenticatedDeliveryBinding(facts, acceptedRoot))), + )?.deliveryRoot, + ).toBe(acceptedRoot); + expect(() => createAuthenticatedDeliveryBinding(facts, `${acceptedRoot}x`)).toThrow( + 'physical delivery root is malformed', + ); + }); + + test('uses one compact digest to bind the complete evidence pack and physical publication transaction', () => { + const { evidence } = mintTestDeliveryEvidence(); + const facts = verifiedDeliveryEvidenceFacts(evidence); + const binding = createAuthenticatedDeliveryBinding(facts, '/physical/a'); + const digest = authenticatedDeliveryBindingDigest(binding); + + expect(digest).toMatch(/^[0-9a-f]{64}$/); + expect(authenticatedDeliveryBindingDigest(createAuthenticatedDeliveryBinding(facts, '/physical/b'))).not.toBe( + digest, + ); + expect( + authenticatedDeliveryBindingDigest( + createAuthenticatedDeliveryBinding({ ...facts, deliveredAt: '2025-07-23T00:00:01.000Z' }, '/physical/a'), + ), + ).not.toBe(digest); + }); + + test('does not initialize Sigstore modules or its trusted root when evidence support is only imported', () => { + const moduleUrl = new URL('./codex-delivery-evidence.ts', import.meta.url).href; + const child = spawnSync( + process.execPath, + [ + '-e', + `await import(${JSON.stringify(moduleUrl)}); process.stdout.write(String(Object.keys(require.cache).filter((key) => key.includes('@sigstore') || key.endsWith('codex-delivery-public-good-trusted-root.json')).length));`, + ], + { encoding: 'utf8' }, + ); + + expect(child.status).toBe(0); + expect(child.stderr).toBe(''); + expect(child.stdout).toBe('0'); + }); +}); + +function mutate(value: unknown): unknown { + if (typeof value === 'number') return value + 1; + if (typeof value === 'string') return value.length === 0 ? 'x' : `${value.slice(0, -1)}x`; + return null; +} + +function createTempDir(label: string): string { + return mkdtempSync(join(tmpdir(), `genie-${label}-`)); +} + +function removeTempDir(path: string): void { + rmSync(path, { recursive: true, force: true }); +} diff --git a/src/lib/codex-delivery-evidence.ts b/src/lib/codex-delivery-evidence.ts new file mode 100644 index 000000000..e85b02fb8 --- /dev/null +++ b/src/lib/codex-delivery-evidence.ts @@ -0,0 +1,951 @@ +/** + * Offline-verifiable Codex delivery evidence. + * + * A release producer signs the SHA-256 of the exact descriptor bytes in an + * in-toto DSSE statement. This module is the only runtime minter of + * `VerifiedDeliveryEvidence`: callers provide selected/downloaded/candidate + * facts, but cannot provide trusted provenance strings. The production verifier + * always uses the embedded public-good Sigstore root and the exact release + * workflow identity. Tests may inject only the cryptographic verification step; + * descriptor, manifest, statement, and caller-observation bindings remain live. + * + * Exact packs are stored content-addressed under GENIE_HOME. Raw paths and pack + * bytes never leave this module. Observation re-verifies the stored bundle + * synchronously and offline before returning independently derived facts. + */ + +import { createHash, randomBytes } from 'node:crypto'; +import { chmodSync, lstatSync, mkdirSync, readdirSync, renameSync, rmSync } from 'node:fs'; +import { isAbsolute, join } from 'node:path'; +import { atomicWriteFileSync, fsyncParentDir, readBoundedRegularFile } from './codex-activation-persistence.js'; +import { parseReleaseVersion } from './codex-release-version.js'; + +export const DELIVERY_EVIDENCE_REPOSITORY = 'automagik-dev/genie'; +export const DELIVERY_EVIDENCE_PREDICATE_TYPE = 'https://github.com/automagik-dev/genie/delivery-evidence/v1'; +export const DELIVERY_EVIDENCE_WORKFLOW_IDENTITY = + 'https://github.com/automagik-dev/genie/.github/workflows/release-publish.yml@refs/heads/main'; +export const DELIVERY_EVIDENCE_WORKFLOW_IDENTITY_PATTERN = `^${escapeRegexLiteral( + DELIVERY_EVIDENCE_WORKFLOW_IDENTITY, +)}$`; +export const DELIVERY_EVIDENCE_OIDC_ISSUER = 'https://token.actions.githubusercontent.com'; +export const DELIVERY_EVIDENCE_DIGEST_ALGORITHM = 'genie-physical-tree-v1'; + +const IN_TOTO_PAYLOAD_TYPE = 'application/vnd.in-toto+json'; +const IN_TOTO_STATEMENT_TYPE = 'https://in-toto.io/Statement/v1'; +const EVIDENCE_DIRECTORY_NAME = '.codex-delivery-evidence-v1'; +const DESCRIPTOR_FILE = 'descriptor.json'; +const BUNDLE_FILE = 'bundle.json'; +const MANIFEST_FILE = 'manifest.json'; +const PACK_FILES = [BUNDLE_FILE, DESCRIPTOR_FILE, MANIFEST_FILE] as const; +const MAX_DESCRIPTOR_BYTES = 32 * 1024; +const MAX_MANIFEST_BYTES = 64 * 1024; +const MAX_BUNDLE_BYTES = 2 * 1024 * 1024; +const HEX_128_RE = /^[0-9a-f]{32}$/; +const HEX_160_RE = /^[0-9a-f]{40}$/; +const HEX_256_RE = /^[0-9a-f]{64}$/; +const DECIMAL_RE = /^(?:0|[1-9]\d*)$/; +const MAX_DELIVERY_ROOT_CHARACTERS = 256; +const RELEASE_CHANNELS = ['stable', 'homolog', 'dev'] as const; +const PLATFORM_IDS = ['linux-x64-glibc', 'linux-x64-musl', 'linux-arm64', 'darwin-arm64'] as const; + +export type DeliveryEvidenceChannel = (typeof RELEASE_CHANNELS)[number]; +export type DeliveryEvidencePlatformId = (typeof PLATFORM_IDS)[number]; + +export interface DeliveryEvidenceDescriptor { + schemaVersion: 1; + repository: typeof DELIVERY_EVIDENCE_REPOSITORY; + version: string; + channel: DeliveryEvidenceChannel; + platformId: DeliveryEvidencePlatformId; + platformTriple: string; + releaseTag: string; + releaseName: string; + releaseManifestSha256: string; + artifactSha256: string; + installedBinarySha256: string; + canonicalPayloadSha256: string; + sourceSha: string; + sourceBranch: string; + sourceCiRunId: string; + controlSha: string; + digestAlgorithm: typeof DELIVERY_EVIDENCE_DIGEST_ALGORITHM; +} + +/** + * Caller-observed facts that must agree with the signed descriptor. No + * provenance field is optional: a caller cannot turn a partly bound descriptor + * into a verified delivery. + */ +export interface VerifyDownloadedDeliveryEvidenceInput { + descriptorBytes: Uint8Array | string; + bundleBytes: Uint8Array | string; + manifestBytes: Uint8Array | string; + targetVersion: string; + channel: DeliveryEvidenceChannel; + platformId: DeliveryEvidencePlatformId; + platformTriple: string; + releaseTag: string; + releaseName: string; + artifactSha256: string; + installedBinarySha256: string; + canonicalPayloadSha256: string; +} + +export interface DeliveryEvidenceBundleVerificationInput { + /** Parsed serialized-bundle JSON. The default path converts it with bundleFromJSON. */ + bundleJson: unknown; + /** Exact descriptor bytes whose digest the DSSE subject must name. */ + descriptorBytes: Uint8Array; +} + +export interface DeliveryEvidenceVerificationDependencies { + /** + * Deterministic test seam for the cryptographic verification step only. + * Production callers omit this. There is deliberately no env or CLI bypass. + */ + verifyBundle?: (input: DeliveryEvidenceBundleVerificationInput) => { integratedTime: string | number }; +} + +declare const VERIFIED_DELIVERY_EVIDENCE: unique symbol; + +/** Opaque, process-local proof. A structural lookalike fails the WeakMap check. */ +export interface VerifiedDeliveryEvidence { + readonly [VERIFIED_DELIVERY_EVIDENCE]: true; +} + +export interface VerifiedDeliveryEvidenceFacts { + descriptor: Readonly; + evidenceDigest: string; + /** ISO timestamp derived from a transparency-log entry's verified integratedTime. */ + deliveredAt: string; +} + +/** + * The durable flat record remains schema 2 for on-disk compatibility. This + * interface and its codec are the single owner of the record field set. + */ +export interface AuthenticatedDeliveryRecordFields { + targetVersion: string; + canonicalPayloadSha256: string; + channel: string; + deliveryId: string; + evidenceDigest: string; + platformId: string; + platformTriple: string; + releaseTag: string; + releaseName: string; + releaseManifestSha256: string; + artifactSha256: string; + installedBinarySha256: string; + deliveryRoot: string; + deliveredAt: string; +} + +export interface PersistedAuthenticatedDeliveryRecord extends AuthenticatedDeliveryRecordFields { + schemaVersion: 2; +} + +/** + * One complete authenticated delivery value. The evidence digest commits the + * exact descriptor, bundle, and manifest bytes, while the remaining fields bind + * that verified release provenance to its physical publication transaction. + */ +export interface AuthenticatedDeliveryBinding { + readonly descriptor: Readonly; + readonly evidenceDigest: string; + readonly deliveryId: string; + readonly deliveryRoot: string; + readonly deliveredAt: string; +} + +export type AuthenticatedDeliveryAssessment = 'matching' | 'absent' | 'invalid' | 'mismatch'; + +export type AuthenticatedDeliveryRecordReadState = + | { status: 'absent' } + | { status: 'invalid'; detail: string } + | { status: 'present'; record: AuthenticatedDeliveryRecordFields }; + +interface VerifiedDeliveryEvidenceState extends VerifiedDeliveryEvidenceFacts { + descriptorBytes: Buffer; + bundleBytes: Buffer; + manifestBytes: Buffer; +} + +const VERIFIED_EVIDENCE = new WeakMap(); + +export type PersistedDeliveryEvidenceObservation = + | { status: 'absent' } + | { status: 'invalid'; detail: string } + | { status: 'present'; evidence: VerifiedDeliveryEvidence; facts: VerifiedDeliveryEvidenceFacts }; + +/** Verify exact downloaded bytes and mint the only evidence value publication accepts. */ +export function verifyDownloadedDeliveryEvidence( + input: VerifyDownloadedDeliveryEvidenceInput, + dependencies: DeliveryEvidenceVerificationDependencies = {}, +): VerifiedDeliveryEvidence { + const descriptorBytes = boundedBytes(input.descriptorBytes, MAX_DESCRIPTOR_BYTES, 'descriptor'); + const bundleBytes = boundedBytes(input.bundleBytes, MAX_BUNDLE_BYTES, 'bundle'); + const manifestBytes = boundedBytes(input.manifestBytes, MAX_MANIFEST_BYTES, 'manifest'); + const descriptor = parseDescriptor(descriptorBytes); + bindCallerObservation(descriptor, input); + verifyManifestBytes(descriptor, manifestBytes); + + const bundleJson = parseJson(bundleBytes, 'bundle'); + const statement = parseDsseStatement(bundleJson); + const descriptorSha256 = sha256(descriptorBytes); + if (statement.subjectSha256 !== descriptorSha256) { + throw new Error('delivery evidence DSSE subject does not bind the exact descriptor bytes'); + } + const integratedTime = + dependencies.verifyBundle?.({ bundleJson, descriptorBytes }) ?? verifyBundleWithEmbeddedTrustRoot(bundleJson); + const deliveredAt = deliveredAtFromIntegratedTime(integratedTime.integratedTime); + const evidenceDigest = computeEvidenceDigest(descriptorBytes, bundleBytes, manifestBytes); + return mintVerifiedEvidence({ + descriptor: Object.freeze({ ...descriptor }), + evidenceDigest, + deliveredAt, + descriptorBytes, + bundleBytes, + manifestBytes, + }); +} + +/** Return immutable, independently verified facts or reject a forged lookalike. */ +export function verifiedDeliveryEvidenceFacts(evidence: VerifiedDeliveryEvidence): VerifiedDeliveryEvidenceFacts { + const state = verifiedState(evidence); + return { + descriptor: state.descriptor, + evidenceDigest: state.evidenceDigest, + deliveredAt: state.deliveredAt, + }; +} + +/** + * Persist the exact verified pack before a delivery record is committed. + * Existing content-addressed packs are accepted only when every byte matches. + */ +export function persistVerifiedDeliveryEvidence( + genieHome: string, + evidence: VerifiedDeliveryEvidence, +): VerifiedDeliveryEvidenceFacts { + const state = verifiedState(evidence); + const root = ensurePrivateEvidenceRoot(genieHome); + const finalDirectory = join(root, state.evidenceDigest); + if (pathExists(finalDirectory)) { + assertStoredPackMatches(finalDirectory, state); + return verifiedDeliveryEvidenceFacts(evidence); + } + + const staging = join(root, `.${state.evidenceDigest}.staging-${process.pid}-${randomBytes(6).toString('hex')}`); + mkdirSync(staging, { mode: 0o700 }); + try { + atomicWriteFileSync(join(staging, DESCRIPTOR_FILE), state.descriptorBytes.toString('utf8'), { + mode: 0o600, + backup: false, + }); + atomicWriteFileSync(join(staging, BUNDLE_FILE), state.bundleBytes.toString('utf8'), { + mode: 0o600, + backup: false, + }); + atomicWriteFileSync(join(staging, MANIFEST_FILE), state.manifestBytes.toString('utf8'), { + mode: 0o600, + backup: false, + }); + try { + renameSync(staging, finalDirectory); + fsyncParentDir(finalDirectory); + } catch (error) { + if (!isAlreadyExists(error)) throw error; + assertStoredPackMatches(finalDirectory, state); + } + } finally { + rmSync(staging, { recursive: true, force: true }); + } + return verifiedDeliveryEvidenceFacts(evidence); +} + +/** Re-read and synchronously re-verify one stored evidence pack, offline. */ +export function observePersistedDeliveryEvidence( + genieHome: string, + evidenceDigest: string, + dependencies: DeliveryEvidenceVerificationDependencies = {}, +): PersistedDeliveryEvidenceObservation { + if (!HEX_256_RE.test(evidenceDigest)) return { status: 'invalid', detail: 'evidence digest is malformed' }; + const root = join(genieHome, EVIDENCE_DIRECTORY_NAME); + const rootState = inspectPrivateDirectory(root); + if (rootState === 'absent') return { status: 'absent' }; + if (rootState !== 'ok') return { status: 'invalid', detail: `evidence root is ${rootState}` }; + const directory = join(root, evidenceDigest); + const directoryState = inspectPrivateDirectory(directory); + if (directoryState === 'absent') return { status: 'absent' }; + if (directoryState !== 'ok') return { status: 'invalid', detail: `evidence pack is ${directoryState}` }; + + try { + assertExactPackFiles(directory); + const descriptorBytes = readPackFile(directory, DESCRIPTOR_FILE, MAX_DESCRIPTOR_BYTES); + const bundleBytes = readPackFile(directory, BUNDLE_FILE, MAX_BUNDLE_BYTES); + const manifestBytes = readPackFile(directory, MANIFEST_FILE, MAX_MANIFEST_BYTES); + const descriptor = parseDescriptor(descriptorBytes); + const observedDigest = computeEvidenceDigest(descriptorBytes, bundleBytes, manifestBytes); + if (observedDigest !== evidenceDigest) throw new Error('stored evidence pack digest mismatch'); + const evidence = verifyDownloadedDeliveryEvidence( + { + descriptorBytes, + bundleBytes, + manifestBytes, + targetVersion: descriptor.version, + channel: descriptor.channel, + platformId: descriptor.platformId, + platformTriple: descriptor.platformTriple, + releaseTag: descriptor.releaseTag, + releaseName: descriptor.releaseName, + artifactSha256: descriptor.artifactSha256, + installedBinarySha256: descriptor.installedBinarySha256, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, + }, + dependencies, + ); + const facts = verifiedDeliveryEvidenceFacts(evidence); + if (facts.evidenceDigest !== evidenceDigest) throw new Error('stored evidence pack changed during verification'); + return { status: 'present', evidence, facts }; + } catch (error) { + return { status: 'invalid', detail: errorText(error) }; + } +} + +/** Domain-separated 128-bit transaction identity over evidence + physical root. */ +export function deriveDeliveryId(evidenceDigest: string, physicalDeliveryRoot: string): string { + if (!HEX_256_RE.test(evidenceDigest)) throw new Error('evidence digest is malformed'); + if (!isAbsolute(physicalDeliveryRoot) || physicalDeliveryRoot.includes('\0')) { + throw new Error('physical delivery root is malformed'); + } + return createHash('sha256') + .update('genie-delivery-id-v2\0') + .update(evidenceDigest) + .update('\0') + .update(physicalDeliveryRoot) + .digest('hex') + .slice(0, 32); +} + +/** + * Bind independently verified release facts to one physical delivery root. + * Callers never reconstruct individual provenance fields. + */ +export function createAuthenticatedDeliveryBinding( + evidence: VerifiedDeliveryEvidenceFacts, + physicalDeliveryRoot: string, +): AuthenticatedDeliveryBinding { + if (!HEX_256_RE.test(evidence.evidenceDigest)) throw new Error('evidence digest is malformed'); + if ( + physicalDeliveryRoot.length > MAX_DELIVERY_ROOT_CHARACTERS || + !isAbsolute(physicalDeliveryRoot) || + physicalDeliveryRoot.includes('\0') + ) { + throw new Error('physical delivery root is malformed'); + } + if (!Number.isFinite(Date.parse(evidence.deliveredAt))) throw new Error('delivery timestamp is malformed'); + return Object.freeze({ + descriptor: evidence.descriptor, + evidenceDigest: evidence.evidenceDigest, + deliveryId: deriveDeliveryId(evidence.evidenceDigest, physicalDeliveryRoot), + deliveryRoot: physicalDeliveryRoot, + deliveredAt: evidence.deliveredAt, + }); +} + +/** Project the canonical binding onto the complete authenticated field tuple. */ +export function authenticatedDeliveryRecordFields( + binding: AuthenticatedDeliveryBinding, +): AuthenticatedDeliveryRecordFields { + const descriptor = binding.descriptor; + return { + deliveryId: binding.deliveryId, + targetVersion: descriptor.version, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, + channel: descriptor.channel, + deliveredAt: binding.deliveredAt, + evidenceDigest: binding.evidenceDigest, + platformId: descriptor.platformId, + platformTriple: descriptor.platformTriple, + releaseTag: descriptor.releaseTag, + releaseName: descriptor.releaseName, + releaseManifestSha256: descriptor.releaseManifestSha256, + artifactSha256: descriptor.artifactSha256, + installedBinarySha256: descriptor.installedBinarySha256, + deliveryRoot: binding.deliveryRoot, + }; +} + +/** Encode the canonical binding as the byte-compatible schema-2 record. */ +export function encodeAuthenticatedDeliveryRecord( + binding: AuthenticatedDeliveryBinding, +): PersistedAuthenticatedDeliveryRecord { + return { schemaVersion: 2, ...authenticatedDeliveryRecordFields(binding) }; +} + +/** Parse the legacy-compatible flat schema without accepting partial attestation. */ +export function parseAuthenticatedDeliveryRecord(content: string): PersistedAuthenticatedDeliveryRecord | null { + let parsed: unknown; + try { + parsed = JSON.parse(content); + } catch { + return null; + } + if (!isPlainObject(parsed)) return null; + if ( + Object.keys(parsed).length !== AUTHENTICATED_DELIVERY_RECORD_KEYS.size || + Object.keys(parsed).some((key) => !AUTHENTICATED_DELIVERY_RECORD_KEYS.has(key)) + ) { + return null; + } + if (parsed.schemaVersion !== 2 || !authenticatedDeliveryRecordFieldsValid(parsed)) return null; + return { + schemaVersion: 2, + deliveryId: parsed.deliveryId, + targetVersion: parsed.targetVersion, + canonicalPayloadSha256: parsed.canonicalPayloadSha256, + channel: parsed.channel, + deliveredAt: parsed.deliveredAt, + evidenceDigest: parsed.evidenceDigest, + platformId: parsed.platformId, + platformTriple: parsed.platformTriple, + releaseTag: parsed.releaseTag, + releaseName: parsed.releaseName, + releaseManifestSha256: parsed.releaseManifestSha256, + artifactSha256: parsed.artifactSha256, + installedBinarySha256: parsed.installedBinarySha256, + deliveryRoot: parsed.deliveryRoot, + }; +} + +/** + * Pure record assessment shared by lifecycle truth, repair, delivery, and the + * activation inner guard. Structural invalidity is distinct from tuple drift. + */ +export function assessAuthenticatedDeliveryRecord( + fact: AuthenticatedDeliveryRecordReadState, + expectation: AuthenticatedDeliveryRecordFields, +): AuthenticatedDeliveryAssessment { + if (fact.status === 'absent') return 'absent'; + if (fact.status === 'invalid') return 'invalid'; + if (!authenticatedDeliveryRecordFieldsValid(fact.record)) return 'invalid'; + return authenticatedDeliveryRecordFieldsMatch(fact.record, expectation) ? 'matching' : 'mismatch'; +} + +/** + * Return the canonical value only when the durable flat record agrees with all + * independently reverified evidence and physical-root facts. + */ +export function authenticatedDeliveryBindingFromRecord( + record: AuthenticatedDeliveryRecordFields, + evidence: VerifiedDeliveryEvidenceFacts, + physicalDeliveryRoot: string, +): AuthenticatedDeliveryBinding | null { + const binding = createAuthenticatedDeliveryBinding(evidence, physicalDeliveryRoot); + return assessAuthenticatedDeliveryRecord( + { status: 'present', record }, + authenticatedDeliveryRecordFields(binding), + ) === 'matching' + ? binding + : null; +} + +/** + * Compact consent fingerprint. `evidenceDigest` already commits every exact + * descriptor/bundle/manifest byte, so descriptor provenance additions do not + * amplify into activation request fields. + */ +export function authenticatedDeliveryBindingDigest(binding: AuthenticatedDeliveryBinding): string { + return createHash('sha256') + .update('genie-authenticated-delivery-binding-v1\0') + .update(binding.evidenceDigest) + .update('\0') + .update(binding.deliveryId) + .update('\0') + .update(binding.deliveryRoot) + .update('\0') + .update(binding.deliveredAt) + .digest('hex'); +} + +const AUTHENTICATED_DELIVERY_RECORD_KEYS: ReadonlySet = new Set([ + 'schemaVersion', + 'deliveryId', + 'targetVersion', + 'canonicalPayloadSha256', + 'channel', + 'deliveredAt', + 'evidenceDigest', + 'platformId', + 'platformTriple', + 'releaseTag', + 'releaseName', + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'deliveryRoot', +]); + +const PLATFORM_TRIPLE_RE = /^[a-z0-9]+-[a-z0-9_]+$/; + +function authenticatedDeliveryRecordFieldsValid( + record: Record | AuthenticatedDeliveryRecordFields, +): record is AuthenticatedDeliveryRecordFields { + const version = parseReleaseVersion(record.targetVersion); + if (version === null) return false; + if (typeof record.canonicalPayloadSha256 !== 'string' || !HEX_256_RE.test(record.canonicalPayloadSha256)) + return false; + if (typeof record.deliveryId !== 'string' || !HEX_128_RE.test(record.deliveryId)) return false; + if (typeof record.evidenceDigest !== 'string' || !HEX_256_RE.test(record.evidenceDigest)) return false; + if (typeof record.channel !== 'string' || record.channel.length === 0 || record.channel.length > 128) return false; + if (typeof record.platformId !== 'string' || record.platformId.length === 0 || record.platformId.length > 64) + return false; + if (typeof record.platformTriple !== 'string' || !PLATFORM_TRIPLE_RE.test(record.platformTriple)) return false; + for (const key of ['releaseManifestSha256', 'artifactSha256', 'installedBinarySha256'] as const) { + if (typeof record[key] !== 'string' || !HEX_256_RE.test(record[key])) return false; + } + if (record.releaseTag !== `v${version.canonical}`) return false; + if ( + typeof record.releaseName !== 'string' || + !record.releaseName.startsWith(`genie-${version.canonical}-`) || + !record.releaseName.endsWith('.tar.gz') || + record.releaseName.length > 256 + ) { + return false; + } + if ( + typeof record.deliveryRoot !== 'string' || + record.deliveryRoot.length === 0 || + record.deliveryRoot.length > MAX_DELIVERY_ROOT_CHARACTERS || + !isAbsolute(record.deliveryRoot) || + record.deliveryRoot.includes('\0') + ) { + return false; + } + if (typeof record.deliveredAt !== 'string' || !Number.isFinite(Date.parse(record.deliveredAt))) return false; + return true; +} + +function authenticatedDeliveryRecordFieldsMatch( + record: AuthenticatedDeliveryRecordFields, + expectation: AuthenticatedDeliveryRecordFields, +): boolean { + const recordVersion = parseReleaseVersion(record.targetVersion); + const expectedVersion = parseReleaseVersion(expectation.targetVersion); + if (recordVersion === null || expectedVersion === null || recordVersion.canonical !== expectedVersion.canonical) { + return false; + } + for (const key of AUTHENTICATED_DELIVERY_BINDING_FIELDS) { + if (record[key] !== expectation[key]) return false; + } + return true; +} + +const AUTHENTICATED_DELIVERY_BINDING_FIELDS: readonly (keyof AuthenticatedDeliveryRecordFields)[] = [ + 'canonicalPayloadSha256', + 'channel', + 'deliveryId', + 'evidenceDigest', + 'platformId', + 'platformTriple', + 'releaseTag', + 'releaseName', + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'deliveryRoot', + 'deliveredAt', +]; + +function mintVerifiedEvidence(state: VerifiedDeliveryEvidenceState): VerifiedDeliveryEvidence { + const evidence = Object.freeze({}) as VerifiedDeliveryEvidence; + VERIFIED_EVIDENCE.set(evidence, state); + return evidence; +} + +function verifiedState(evidence: VerifiedDeliveryEvidence): VerifiedDeliveryEvidenceState { + const state = VERIFIED_EVIDENCE.get(evidence as object); + if (state === undefined) throw new Error('delivery evidence was not minted by the verifier'); + return state; +} + +const DESCRIPTOR_KEYS = new Set([ + 'schemaVersion', + 'repository', + 'version', + 'channel', + 'platformId', + 'platformTriple', + 'releaseTag', + 'releaseName', + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'canonicalPayloadSha256', + 'sourceSha', + 'sourceBranch', + 'sourceCiRunId', + 'controlSha', + 'digestAlgorithm', +]); + +function parseDescriptor(bytes: Buffer): DeliveryEvidenceDescriptor { + const parsed = parseJson(bytes, 'descriptor'); + if (!isPlainObject(parsed)) throw new Error('delivery evidence descriptor is not an object'); + if ( + Object.keys(parsed).length !== DESCRIPTOR_KEYS.size || + Object.keys(parsed).some((key) => !DESCRIPTOR_KEYS.has(key as keyof DeliveryEvidenceDescriptor)) + ) { + throw new Error('delivery evidence descriptor does not have the exact schema-v1 fields'); + } + if (parsed.schemaVersion !== 1) throw new Error('delivery evidence schemaVersion is invalid'); + if (parsed.repository !== DELIVERY_EVIDENCE_REPOSITORY) throw new Error('delivery evidence repository is invalid'); + if (typeof parsed.version !== 'string' || parseReleaseVersion(parsed.version) === null) { + throw new Error('delivery evidence version is invalid'); + } + if (!isReleaseChannel(parsed.channel)) throw new Error('delivery evidence channel is invalid'); + if (!isPlatformId(parsed.platformId)) throw new Error('delivery evidence platformId is invalid'); + const platformTriple = platformTripleFor(parsed.platformId); + if (parsed.platformTriple !== platformTriple) throw new Error('delivery evidence platformTriple is invalid'); + if (parsed.releaseTag !== `v${parsed.version}`) throw new Error('delivery evidence releaseTag is invalid'); + if (parsed.releaseName !== `genie-${parsed.version}-${parsed.platformId}.tar.gz`) { + throw new Error('delivery evidence releaseName is invalid'); + } + for (const key of [ + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'canonicalPayloadSha256', + ] as const) { + if (!HEX_256_RE.test(parsed[key] as string)) throw new Error(`delivery evidence ${key} is invalid`); + } + if (!HEX_160_RE.test(parsed.sourceSha as string)) throw new Error('delivery evidence sourceSha is invalid'); + if (!sourceBranchAllowedForChannel(parsed.sourceBranch, parsed.channel)) { + throw new Error('delivery evidence sourceBranch is invalid'); + } + if (typeof parsed.sourceCiRunId !== 'string' || !DECIMAL_RE.test(parsed.sourceCiRunId)) { + throw new Error('delivery evidence sourceCiRunId is invalid'); + } + if (!HEX_160_RE.test(parsed.controlSha as string)) throw new Error('delivery evidence controlSha is invalid'); + if (parsed.digestAlgorithm !== DELIVERY_EVIDENCE_DIGEST_ALGORITHM) { + throw new Error('delivery evidence digestAlgorithm is invalid'); + } + return parsed as unknown as DeliveryEvidenceDescriptor; +} + +function bindCallerObservation( + descriptor: DeliveryEvidenceDescriptor, + input: VerifyDownloadedDeliveryEvidenceInput, +): void { + const bindings: ReadonlyArray<[keyof DeliveryEvidenceDescriptor, string]> = [ + ['version', input.targetVersion], + ['channel', input.channel], + ['platformId', input.platformId], + ['platformTriple', input.platformTriple], + ['releaseTag', input.releaseTag], + ['releaseName', input.releaseName], + ['artifactSha256', input.artifactSha256], + ['installedBinarySha256', input.installedBinarySha256], + ['canonicalPayloadSha256', input.canonicalPayloadSha256], + ]; + for (const [field, observed] of bindings) { + if (descriptor[field] !== observed) { + throw new Error(`delivery evidence ${field} does not match the selected/downloaded candidate`); + } + } +} + +function verifyManifestBytes(descriptor: DeliveryEvidenceDescriptor, bytes: Buffer): void { + if (sha256(bytes) !== descriptor.releaseManifestSha256) { + throw new Error('delivery evidence manifest digest does not bind the exact fetched manifest bytes'); + } + const manifest = parseJson(bytes, 'manifest'); + if (!isPlainObject(manifest)) throw new Error('delivery evidence manifest is not an object'); + const keys = Object.keys(manifest).sort(); + const expectedKeys = ['channel', 'platforms', 'released_at', 'schema_version', 'tarball_base', 'version']; + if (keys.length !== expectedKeys.length || keys.some((key, index) => key !== expectedKeys[index])) { + throw new Error('delivery evidence manifest does not have the exact schema-v1 fields'); + } + if (manifest.schema_version !== 1) throw new Error('delivery evidence manifest schema is invalid'); + if (manifest.channel !== descriptor.channel) throw new Error('delivery evidence manifest channel mismatch'); + if (manifest.version !== descriptor.version) throw new Error('delivery evidence manifest version mismatch'); + if (typeof manifest.released_at !== 'string' || !Number.isFinite(Date.parse(manifest.released_at))) { + throw new Error('delivery evidence manifest released_at is invalid'); + } + const expectedBase = `https://github.com/${DELIVERY_EVIDENCE_REPOSITORY}/releases/download/${descriptor.releaseTag}`; + if (manifest.tarball_base !== expectedBase) throw new Error('delivery evidence manifest tarball_base mismatch'); + if ( + !Array.isArray(manifest.platforms) || + !manifest.platforms.every(isPlatformId) || + new Set(manifest.platforms).size !== manifest.platforms.length || + !manifest.platforms.includes(descriptor.platformId) + ) { + throw new Error('delivery evidence manifest platforms are invalid'); + } +} + +function parseDsseStatement(bundleJson: unknown): { subjectSha256: string } { + if (!isPlainObject(bundleJson)) throw new Error('delivery evidence bundle is not an object'); + const envelope = bundleJson.dsseEnvelope; + if (!isPlainObject(envelope)) throw new Error('delivery evidence bundle is not a DSSE bundle'); + if (envelope.payloadType !== IN_TOTO_PAYLOAD_TYPE) { + throw new Error('delivery evidence DSSE payload type is invalid'); + } + if (typeof envelope.payload !== 'string' || envelope.payload.length === 0) { + throw new Error('delivery evidence DSSE payload is missing'); + } + let statement: unknown; + try { + statement = JSON.parse(Buffer.from(envelope.payload, 'base64').toString('utf8')); + } catch { + throw new Error('delivery evidence DSSE statement is malformed'); + } + if (!isPlainObject(statement)) throw new Error('delivery evidence DSSE statement is not an object'); + if (statement._type !== IN_TOTO_STATEMENT_TYPE) throw new Error('delivery evidence statement type is invalid'); + if (statement.predicateType !== DELIVERY_EVIDENCE_PREDICATE_TYPE) { + throw new Error('delivery evidence predicate type is invalid'); + } + if (!Array.isArray(statement.subject) || statement.subject.length !== 1) { + throw new Error('delivery evidence statement must have exactly one subject'); + } + const subject = statement.subject[0]; + if (!isPlainObject(subject) || !isPlainObject(subject.digest)) { + throw new Error('delivery evidence statement subject is malformed'); + } + if (Object.keys(subject.digest).length !== 1 || !HEX_256_RE.test(subject.digest.sha256 as string)) { + throw new Error('delivery evidence statement subject digest is malformed'); + } + return { subjectSha256: subject.digest.sha256 as string }; +} + +function verifyBundleWithEmbeddedTrustRoot(bundleJson: unknown): { integratedTime: string } { + const { + bundleFromJSON, + isBundleWithDsseEnvelope, + publicGoodTrustedRoot, + TrustedRoot, + Verifier, + toSignedEntity, + toTrustMaterial, + } = loadSigstoreVerifier(); + const bundle = bundleFromJSON(bundleJson); + if (!isBundleWithDsseEnvelope(bundle)) throw new Error('delivery evidence bundle is not a DSSE bundle'); + const root = TrustedRoot.fromJSON(publicGoodTrustedRoot); + const verifier = new Verifier(toTrustMaterial(root), { + tlogThreshold: 1, + ctlogThreshold: 1, + timestampThreshold: 1, + }); + verifier.verify(toSignedEntity(bundle), { + // @sigstore/verify interprets this field as a JavaScript regular + // expression. Escape and anchor the literal identity so sibling workflow + // names and branches such as `main-evil` cannot satisfy the policy. + subjectAlternativeName: DELIVERY_EVIDENCE_WORKFLOW_IDENTITY_PATTERN, + extensions: { issuer: DELIVERY_EVIDENCE_OIDC_ISSUER }, + }); + const integratedTimes = bundle.verificationMaterial.tlogEntries + .map((entry) => entry.integratedTime) + .filter((value) => value !== '0'); + if (integratedTimes.length === 0) throw new Error('delivery evidence bundle has no integrated transparency-log time'); + return { + integratedTime: integratedTimes.reduce((earliest, value) => (BigInt(value) < BigInt(earliest) ? value : earliest)), + }; +} + +type SigstoreVerifierModules = { + bundleFromJSON: typeof import('@sigstore/bundle')['bundleFromJSON']; + isBundleWithDsseEnvelope: typeof import('@sigstore/bundle')['isBundleWithDsseEnvelope']; + TrustedRoot: typeof import('@sigstore/protobuf-specs')['TrustedRoot']; + Verifier: typeof import('@sigstore/verify')['Verifier']; + toSignedEntity: typeof import('@sigstore/verify')['toSignedEntity']; + toTrustMaterial: typeof import('@sigstore/verify')['toTrustMaterial']; + publicGoodTrustedRoot: Parameters[0]; +}; + +let sigstoreVerifierModules: SigstoreVerifierModules | undefined; + +/** Load the heavy verifier graph only on a production cryptographic check. */ +function loadSigstoreVerifier(): SigstoreVerifierModules { + if (sigstoreVerifierModules !== undefined) return sigstoreVerifierModules; + const bundle = require('@sigstore/bundle') as typeof import('@sigstore/bundle'); + const protobuf = require('@sigstore/protobuf-specs') as typeof import('@sigstore/protobuf-specs'); + const verify = require('@sigstore/verify') as typeof import('@sigstore/verify'); + sigstoreVerifierModules = { + bundleFromJSON: bundle.bundleFromJSON, + isBundleWithDsseEnvelope: bundle.isBundleWithDsseEnvelope, + TrustedRoot: protobuf.TrustedRoot, + Verifier: verify.Verifier, + toSignedEntity: verify.toSignedEntity, + toTrustMaterial: verify.toTrustMaterial, + publicGoodTrustedRoot: require('../fixtures/codex-delivery-public-good-trusted-root.json'), + }; + return sigstoreVerifierModules; +} + +function escapeRegexLiteral(value: string): string { + return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} + +function deliveredAtFromIntegratedTime(value: string | number): string { + const text = String(value); + if (!DECIMAL_RE.test(text)) throw new Error('delivery evidence integratedTime is invalid'); + const seconds = Number(text); + if (!Number.isSafeInteger(seconds) || seconds <= 0 || seconds > 8_640_000_000_000) { + throw new Error('delivery evidence integratedTime is out of range'); + } + const deliveredAt = new Date(seconds * 1000); + if (!Number.isFinite(deliveredAt.getTime())) throw new Error('delivery evidence integratedTime is out of range'); + return deliveredAt.toISOString(); +} + +function computeEvidenceDigest(descriptor: Buffer, bundle: Buffer, manifest: Buffer): string { + const digest = createHash('sha256'); + digest.update('genie-delivery-evidence-pack-v1\0'); + for (const [name, bytes] of [ + [DESCRIPTOR_FILE, descriptor], + [BUNDLE_FILE, bundle], + [MANIFEST_FILE, manifest], + ] as const) { + digest.update(name); + digest.update('\0'); + digest.update(String(bytes.length)); + digest.update('\0'); + digest.update(bytes); + } + return digest.digest('hex'); +} + +function ensurePrivateEvidenceRoot(genieHome: string): string { + const root = join(genieHome, EVIDENCE_DIRECTORY_NAME); + mkdirSync(root, { recursive: true, mode: 0o700 }); + const state = inspectDirectory(root); + if (state !== 'ok') throw new Error(`delivery evidence root is ${state}`); + const stat = lstatSync(root); + const uid = process.getuid?.(); + if (uid !== undefined && stat.uid !== uid) throw new Error('delivery evidence root has unsafe ownership'); + chmodSync(root, 0o700); + return root; +} + +function inspectPrivateDirectory( + path: string, +): 'ok' | 'absent' | 'symlink' | 'non-directory' | 'ownership' | 'permissions' { + const state = inspectDirectory(path); + if (state !== 'ok') return state; + const stat = lstatSync(path); + const uid = process.getuid?.(); + if (uid !== undefined && stat.uid !== uid) return 'ownership'; + return (stat.mode & 0o077) === 0 ? 'ok' : 'permissions'; +} + +function inspectDirectory(path: string): 'ok' | 'absent' | 'symlink' | 'non-directory' { + try { + const stat = lstatSync(path); + if (stat.isSymbolicLink()) return 'symlink'; + if (!stat.isDirectory()) return 'non-directory'; + return 'ok'; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return 'absent'; + return 'non-directory'; + } +} + +function assertStoredPackMatches(directory: string, state: VerifiedDeliveryEvidenceState): void { + if (inspectPrivateDirectory(directory) !== 'ok') throw new Error('existing delivery evidence pack is unsafe'); + assertExactPackFiles(directory); + const descriptor = readPackFile(directory, DESCRIPTOR_FILE, MAX_DESCRIPTOR_BYTES); + const bundle = readPackFile(directory, BUNDLE_FILE, MAX_BUNDLE_BYTES); + const manifest = readPackFile(directory, MANIFEST_FILE, MAX_MANIFEST_BYTES); + if ( + !descriptor.equals(state.descriptorBytes) || + !bundle.equals(state.bundleBytes) || + !manifest.equals(state.manifestBytes) + ) { + throw new Error('existing delivery evidence pack does not match the verified bytes'); + } +} + +function assertExactPackFiles(directory: string): void { + const names = readdirSync(directory).sort(); + if (names.length !== PACK_FILES.length || names.some((name, index) => name !== PACK_FILES[index])) { + throw new Error('delivery evidence pack contains unexpected entries'); + } +} + +function readPackFile(directory: string, name: string, maxBytes: number): Buffer { + const read = readBoundedRegularFile(join(directory, name), maxBytes); + if (read.status !== 'ok') throw new Error(`delivery evidence ${name} is ${read.status}`); + const stat = lstatSync(join(directory, name)); + const uid = process.getuid?.(); + if (uid !== undefined && stat.uid !== uid) throw new Error(`delivery evidence ${name} has unsafe ownership`); + if (stat.nlink !== 1) throw new Error(`delivery evidence ${name} has multiple hard links`); + if ((stat.mode & 0o077) !== 0) throw new Error(`delivery evidence ${name} has unsafe permissions`); + return Buffer.from(read.content, 'utf8'); +} + +function boundedBytes(value: Uint8Array | string, maxBytes: number, label: string): Buffer { + const bytes = typeof value === 'string' ? Buffer.from(value, 'utf8') : Buffer.from(value); + if (bytes.length === 0) throw new Error(`delivery evidence ${label} is empty`); + if (bytes.length > maxBytes) throw new Error(`delivery evidence ${label} exceeds the byte limit`); + if (!Buffer.from(bytes.toString('utf8'), 'utf8').equals(bytes)) { + throw new Error(`delivery evidence ${label} is not valid UTF-8`); + } + return bytes; +} + +function parseJson(bytes: Buffer, label: string): unknown { + try { + return JSON.parse(bytes.toString('utf8')); + } catch { + throw new Error(`delivery evidence ${label} is malformed JSON`); + } +} + +function platformTripleFor(platformId: DeliveryEvidencePlatformId): string { + if (platformId === 'darwin-arm64') return 'darwin-arm64'; + if (platformId === 'linux-arm64') return 'linux-arm64'; + return 'linux-x64'; +} + +function sourceBranchAllowedForChannel(branch: unknown, channel: DeliveryEvidenceChannel): branch is string { + if (typeof branch !== 'string') return false; + if (channel === 'stable') return branch === 'main'; + if (channel === 'homolog') return branch === 'main' || branch === 'homolog'; + return branch === 'main' || branch === 'homolog' || branch === 'dev'; +} + +function isReleaseChannel(value: unknown): value is DeliveryEvidenceChannel { + return typeof value === 'string' && (RELEASE_CHANNELS as readonly string[]).includes(value); +} + +function isPlatformId(value: unknown): value is DeliveryEvidencePlatformId { + return typeof value === 'string' && (PLATFORM_IDS as readonly string[]).includes(value); +} + +function isPlainObject(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value); +} + +function pathExists(path: string): boolean { + try { + lstatSync(path); + return true; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false; + throw error; + } +} + +function isAlreadyExists(error: unknown): boolean { + const code = (error as NodeJS.ErrnoException).code; + return code === 'EEXIST' || code === 'ENOTEMPTY'; +} + +function sha256(bytes: Uint8Array): string { + return createHash('sha256').update(bytes).digest('hex'); +} + +function errorText(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} diff --git a/src/lib/codex-doctor-observation.test.ts b/src/lib/codex-doctor-observation.test.ts new file mode 100644 index 000000000..bc6ed5758 --- /dev/null +++ b/src/lib/codex-doctor-observation.test.ts @@ -0,0 +1,116 @@ +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { observeDoctorCodexHost } from './codex-doctor-observation.js'; +import type { CommandResult } from './runtime-integrations.js'; + +const T = '5.260722.1'; +// An absolute, existing, executable path so the probe's trusted-executable +// validation passes without a real Codex install. +const FAKE_CODEX = process.execPath; + +function pluginListJson(entries: Array<{ version: string; enabled?: boolean }>): string { + return JSON.stringify({ + installed: entries.map((e) => ({ pluginId: 'genie@automagik', version: e.version, enabled: e.enabled ?? true })), + }); +} + +let codexHome: string; +beforeEach(() => { + codexHome = mkdtempSync(join(tmpdir(), 'doctor-obs-codex-')); +}); +afterEach(() => { + rmSync(codexHome, { recursive: true, force: true }); +}); + +function observe(result: Partial, calls: string[][] = []) { + return observeDoctorCodexHost({ + which: () => FAKE_CODEX, + codexHome, + runner: (command, args) => { + calls.push([command, ...args]); + return { exitCode: 0, stdout: '', stderr: '', ...result }; + }, + }); +} + +describe('observeDoctorCodexHost — one bounded observation, every surface derived', () => { + test('absent CLI: zero spawns, probe reports Claude-only mode', () => { + const calls: string[][] = []; + const obs = observeDoctorCodexHost({ + which: () => null, + runner: () => { + calls.push(['spawned']); + return { exitCode: 0, stdout: '', stderr: '' }; + }, + }); + expect(calls).toEqual([]); + expect(obs.codexCommand).toBeNull(); + expect(obs.observation).toBeNull(); + expect(obs.projection).toBeNull(); + expect(obs.advisory).toBeNull(); + expect(obs.activationRunner).toBeNull(); + expect(obs.probe.cliAvailable).toBe(false); + }); + + test('exactly ONE bounded query feeds probe, projection, and replay runner', () => { + const calls: string[][] = []; + const obs = observe({ stdout: pluginListJson([{ version: T, enabled: true }]) }, calls); + expect(calls).toEqual([[FAKE_CODEX, 'plugin', 'list', '--json']]); + expect(obs.observation?.status).toBe('ok'); + expect(obs.probe).toMatchObject({ cliAvailable: true, installed: true, enabled: true, version: T }); + expect(obs.projection).toMatchObject({ registration: 'present', installedVersion: T, queryFailed: false }); + // Deriving the activation snapshot must NOT spawn again. + obs.activationRunner?.(FAKE_CODEX, ['plugin', 'list', '--json']); + expect(calls).toHaveLength(1); + }); + + test('the real sandbox PATH advisory: ok observation, advisory retained, replay stderr blanked (Decision 11)', () => { + const obs = observe({ + stdout: pluginListJson([{ version: T }]), + stderr: '\x1b[33mWARN: PATH does not include codex shims\x1b[0m', + }); + expect(obs.observation?.status).toBe('ok'); + expect(obs.advisory).toBe('WARN: PATH does not include codex shims'); + // Probe side already tolerated exit-0 stderr; replay side must now agree. + expect(obs.probe.installed).toBe(true); + const replay = obs.activationRunner?.(FAKE_CODEX, ['plugin', 'list', '--json']); + expect(replay?.stderr).toBe(''); + expect(replay?.stdout).toBe(pluginListJson([{ version: T }])); + }); + + test('nonzero exit fails BOTH surfaces from the same fact', () => { + const obs = observe({ exitCode: 3, stderr: 'boom' }); + expect(obs.observation?.status).toBe('failed'); + expect(obs.projection?.queryFailed).toBe(true); + expect(obs.probe.status).toBe('error'); + const replay = obs.activationRunner?.(FAKE_CODEX, ['plugin', 'list', '--json']); + expect(replay?.exitCode).toBe(3); + expect(replay?.stderr).toBe('boom'); + }); + + test('timeout fails both surfaces', () => { + const obs = observe({ timedOut: true }); + expect(obs.observation).toMatchObject({ status: 'failed', code: 'timeout' }); + expect(obs.probe).toMatchObject({ status: 'error', timedOut: true }); + expect(obs.activationRunner?.(FAKE_CODEX, [])?.timedOut).toBe(true); + }); + + test('output overflow can never replay as a smaller valid answer (e.g. "not installed")', () => { + const obs = observe({ outputOverflow: true, stdout: '{"installed":' }); + expect(obs.observation).toMatchObject({ status: 'failed', code: 'output-overflow' }); + expect(obs.probe.status).toBe('error'); + expect(obs.probe.installed).toBe(false); + expect(obs.probe.detail).toContain('output cap'); + const replay = obs.activationRunner?.(FAKE_CODEX, []); + expect(replay?.outputOverflow).toBe(true); + }); + + test('malformed stdout fails both surfaces', () => { + const obs = observe({ stdout: 'not json' }); + expect(obs.observation).toMatchObject({ status: 'failed', code: 'malformed-json' }); + const replay = obs.activationRunner?.(FAKE_CODEX, []); + expect(replay?.stdout).toBe('not json'); + }); +}); diff --git a/src/lib/codex-doctor-observation.ts b/src/lib/codex-doctor-observation.ts new file mode 100644 index 000000000..3c064dd3b --- /dev/null +++ b/src/lib/codex-doctor-observation.ts @@ -0,0 +1,147 @@ +/** + * Group E — the ONE bounded Codex host observation per doctor run. + * + * Doctor previously spawned `codex plugin list --json` twice — once through + * `probeCodexGeniePlugin` (feeding the check list) and once through + * `observeCodexActivation` (feeding the integration summary/trailer/exit) — + * with divergent stderr policy: the probe ignored exit-0 stderr while the + * activation observer failed closed on ANY stderr. A single benign sandbox + * PATH advisory therefore produced a healthy check list alongside a + * query-failed integration summary and exit 1: two contradictory answers to + * one question from one host. + * + * This module runs the bounded query EXACTLY ONCE, classifies the result + * through Group B's `parseCodexHostObservation` (Decision 11: exit 0 plus + * exactly one schema-valid JSON stdout value succeeds; bounded advisory stderr + * is retained only as sanitized diagnostic metadata, never a second policy + * decision), and derives every downstream doctor input from that single + * observation: + * + * - a `CodexPluginProbe` via `probeCodexGeniePlugin`'s replay seam; + * - a replay `CommandRunner` for `observeCodexActivation` — the SAME captured + * bytes, with advisory stderr blanked ONLY when the observation succeeded, + * so the fail-closed activation parser cannot contradict the check list + * while every real failure (timeout, overflow, nonzero exit, malformed or + * duplicate JSON) replays raw and fails both surfaces consistently; + * - the sanitized advisory text for diagnostics. + * + * Pure over the injected seams; the only IO is the one bounded subprocess and + * Group B's bounded cache-family witness. + */ + +import { + type CodexHostObservation, + type HostQueryProjection, + parseCodexHostObservation, + projectHostQuery, + witnessCodexCacheFamily, +} from './codex-host-observation.js'; +import { type CodexPluginProbe, type CodexProbeCommandResult, probeCodexGeniePlugin } from './codex-project-mcp.js'; +import { resolveCodexDir } from './genie-home.js'; +import { type CommandResult, type CommandRunner, runBoundedIntegrationCommand } from './runtime-integrations.js'; + +/** Same bounds `observeCodexActivation` applies to its own plugin query. */ +const PLUGIN_LIST_TIMEOUT_MS = 5_000; +const PLUGIN_LIST_MAX_BYTES = 64 * 1024; + +export interface DoctorCodexObservationDeps { + /** Codex CLI resolution seam; default is a safe `Bun.which`. */ + which?: (name: string) => string | null; + /** Bounded subprocess seam; default is the shared integration runner. */ + runner?: CommandRunner; + codexHome?: string; + cwd?: string; +} + +export interface DoctorCodexObservation { + /** Absolute Codex CLI path, or null when the CLI is absent (Claude-only host). */ + codexCommand: string | null; + /** Group B's typed observation of the one bounded query; null when the CLI is absent. */ + observation: CodexHostObservation | null; + /** The single projection every doctor surface derives from; null when the CLI is absent. */ + projection: HostQueryProjection | null; + /** Sanitized, bounded advisory stderr (e.g. the real sandbox PATH advisory); diagnostic metadata only. */ + advisory: string | null; + /** The check-list probe, derived from the SAME captured query result. */ + probe: CodexPluginProbe; + /** Replay runner for `observeCodexActivation`; null when the CLI is absent. */ + activationRunner: CommandRunner | null; +} + +/** + * Observe the Codex host once and derive every doctor-side consumer input. + * Spawns exactly one bounded `codex plugin list --json` when the CLI is + * present, and nothing at all when it is absent. + */ +export function observeDoctorCodexHost(deps: DoctorCodexObservationDeps = {}): DoctorCodexObservation { + const which = deps.which ?? safeWhich; + const codexCommand = which('codex'); + if (codexCommand === null) { + return { + codexCommand: null, + observation: null, + projection: null, + advisory: null, + probe: probeCodexGeniePlugin({ which: () => null }), + activationRunner: null, + }; + } + const codexHome = deps.codexHome ?? resolveCodexDir(); + const cacheFamily = witnessCodexCacheFamily(codexHome); + const runner = deps.runner ?? runBoundedIntegrationCommand; + const result = runner(codexCommand, ['plugin', 'list', '--json'], { + timeoutMs: PLUGIN_LIST_TIMEOUT_MS, + maxOutputBytes: PLUGIN_LIST_MAX_BYTES, + }); + const observation = parseCodexHostObservation({ result, cacheFamily }); + const projection = projectHostQuery(observation); + const probe = probeCodexGeniePlugin({ + which: () => codexCommand, + codexHome, + cwd: deps.cwd, + run: () => replayProbeResult(result), + }); + // Decision 11 applied once, at the seam: an ok observation already proved + // exit 0 + exactly one schema-valid JSON value, so its advisory stderr is + // metadata — blank it for the fail-closed activation parser. Every failed + // observation replays the raw bytes so both parsers fail for the same fact. + const activationRunner: CommandRunner = + observation.status === 'ok' ? () => ({ ...result, stderr: '' }) : () => result; + return { + codexCommand, + observation, + projection, + advisory: projection.advisory, + probe, + activationRunner, + }; +} + +/** Map the captured bounded result onto the probe's replay-runner shape. */ +function replayProbeResult(result: CommandResult): CodexProbeCommandResult { + if (result.outputOverflow === true) { + // The probe runner shape has no overflow flag; a truncated stdout must not + // parse as a smaller valid answer (e.g. "not installed"), so overflow + // replays as an explicit command failure on both surfaces. + return { + exitCode: 1, + stdout: '', + stderr: 'codex plugin list exceeded the bounded output cap', + timedOut: false, + }; + } + return { + exitCode: result.exitCode, + stdout: result.stdout, + stderr: result.stderr, + timedOut: result.timedOut === true, + }; +} + +function safeWhich(name: string): string | null { + try { + return Bun.which(name); + } catch { + return null; + } +} diff --git a/src/lib/codex-host-observation.test.ts b/src/lib/codex-host-observation.test.ts new file mode 100644 index 000000000..48ed5688d --- /dev/null +++ b/src/lib/codex-host-observation.test.ts @@ -0,0 +1,439 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { mkdirSync, mkdtempSync, rmSync, symlinkSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { + type ActivationDeliveryExpectation, + type AuthenticatedDeliveryRecord, + type CodexHostObservation, + type DeliveryRecordReadState, + type HostCacheWitness, + type HostObservationFailureCode, + assessAuthenticatedDelivery, + buildDeliveryIncompleteResult, + parseCodexHostObservation, + projectHostQuery, + witnessCodexCacheFamily, +} from './codex-host-observation.js'; +import type { CommandResult } from './runtime-integrations.js'; + +// ============================================================================ +// Fixtures +// ============================================================================ + +const roots: string[] = []; +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); +function freshRoot(): string { + const root = mkdtempSync(join(tmpdir(), 'genie-host-obs-')); + roots.push(root); + return root; +} + +const T = '5.260712.1'; +const DIGEST_A = 'a'.repeat(64); +const DIGEST_B = 'b'.repeat(64); +const DIGEST_C = 'c'.repeat(64); +const DIGEST_D = 'd'.repeat(64); +const DIGEST_E = 'e'.repeat(64); +const ID_128 = '1'.repeat(32); +const PRESENT_FAMILY: HostCacheWitness = { status: 'present', digest: 'f'.repeat(64), identity: '10:20' }; + +function result(overrides: Partial & { stdout?: string } = {}): CommandResult { + return { exitCode: 0, stdout: '', stderr: '', ...overrides }; +} +function pluginListJson(entries: Array<{ version: string; enabled?: boolean }>): string { + return JSON.stringify({ + installed: entries.map((e) => ({ pluginId: 'genie@automagik', version: e.version, enabled: e.enabled ?? true })), + }); +} +function parse( + overrides: Partial & { stdout?: string }, + cacheFamily = PRESENT_FAMILY, +): CodexHostObservation { + return parseCodexHostObservation({ result: result(overrides), cacheFamily }); +} + +// ============================================================================ +// parseCodexHostObservation — the bounded query (deliverable 6) +// ============================================================================ + +describe('parseCodexHostObservation — one bounded subprocess result', () => { + test('exit 0 + one valid JSON + clean stderr → ok with parsed plugin facts and no advisory', () => { + const obs = parse({ stdout: pluginListJson([{ version: T, enabled: true }]) }); + expect(obs.status).toBe('ok'); + if (obs.status !== 'ok') throw new Error('unreachable'); + expect(obs.plugin).toEqual({ installed: true, enabled: true, version: T }); + expect(obs.advisoryStderr).toBeNull(); + expect(obs.effectiveChildCwd).toBeNull(); + expect(obs.childPid).toBeNull(); + }); + + test('an absent registration parses to installed:false', () => { + const obs = parse({ stdout: JSON.stringify({ installed: [] }) }); + expect(obs.status).toBe('ok'); + if (obs.status !== 'ok') throw new Error('unreachable'); + expect(obs.plugin).toEqual({ installed: false, enabled: null, version: null }); + }); + + // THE real sandbox-PATH-advisory case: advisory stderr is retained (not a failure) with exit 0 + valid stdout. + test('exit 0 + valid stdout + advisory stderr → ok, advisory retained as sanitized metadata', () => { + const obs = parse({ + stdout: pluginListJson([{ version: T }]), + stderr: 'warning: codex added to PATH; restart your shell', + }); + expect(obs.status).toBe('ok'); + if (obs.status !== 'ok') throw new Error('unreachable'); + expect(obs.plugin.installed).toBe(true); + expect(obs.advisoryStderr).toBe('warning: codex added to PATH; restart your shell'); + }); + + test('advisory stderr is stripped of ANSI control sequences and bounded to the cap', () => { + const obs = parseCodexHostObservation({ + result: result({ stdout: pluginListJson([{ version: T }]), stderr: `\x1b[33m${'x'.repeat(50)}\x1b[0m` }), + cacheFamily: PRESENT_FAMILY, + maxAdvisoryBytes: 10, + }); + expect(obs.status).toBe('ok'); + if (obs.status !== 'ok') throw new Error('unreachable'); + expect(obs.advisoryStderr).not.toContain('\x1b'); + // 10 bytes retained + an ellipsis marker. + expect(obs.advisoryStderr).toBe(`${'x'.repeat(10)}…`); + }); + + test('ANSI/OSC-wrapped stdout is sanitized before the JSON is parsed', () => { + const obs = parse({ stdout: `\x1b[32m${pluginListJson([{ version: T }])}\x1b[0m` }); + expect(obs.status).toBe('ok'); + }); + + const failureCases: Array<{ + name: string; + result: Partial & { stdout?: string }; + code: HostObservationFailureCode; + }> = [ + { name: 'timeout', result: { timedOut: true, stdout: pluginListJson([{ version: T }]) }, code: 'timeout' }, + { + name: 'output overflow', + result: { outputOverflow: true, stdout: pluginListJson([{ version: T }]) }, + code: 'output-overflow', + }, + { name: 'nonzero exit', result: { exitCode: 3, stdout: pluginListJson([{ version: T }]) }, code: 'nonzero-exit' }, + { name: 'empty stdout', result: { stdout: '' }, code: 'malformed-json' }, + { name: 'unparseable stdout', result: { stdout: '{not json' }, code: 'malformed-json' }, + { + name: 'two JSON values', + result: { stdout: `${pluginListJson([{ version: T }])}\n${pluginListJson([{ version: T }])}` }, + code: 'malformed-json', + }, + { + name: 'duplicate registration', + result: { stdout: pluginListJson([{ version: T }, { version: T }]) }, + code: 'duplicate-registration', + }, + { + name: 'invalid (non-release) version', + result: { stdout: pluginListJson([{ version: '1.2.3' }]) }, + code: 'invalid-plugin-version', + }, + { + name: 'malformed enabled field', + result: { + stdout: JSON.stringify({ installed: [{ pluginId: 'genie@automagik', version: T, enabled: 'yes' }] }), + }, + code: 'malformed-json', + }, + ]; + for (const c of failureCases) { + test(`${c.name} → typed failure ${c.code}, ANSI-free detail`, () => { + const obs = parse(c.result); + expect(obs.status).toBe('failed'); + if (obs.status !== 'failed') throw new Error('unreachable'); + expect(obs.code).toBe(c.code); + expect(obs.detail).not.toContain('\x1b'); + }); + } + + test('an unsafe cache family root fails the observation before parsing plugin facts', () => { + const obs = parse( + { stdout: pluginListJson([{ version: T }]) }, + { status: 'unsafe', detail: 'family is a symlink' }, + ); + expect(obs.status).toBe('failed'); + if (obs.status !== 'failed') throw new Error('unreachable'); + expect(obs.code).toBe('unsafe-cache-root'); + }); + + test('a genieRuntime self-report populates effective child CWD / identity / PID', () => { + const stdout = JSON.stringify({ + installed: [{ pluginId: 'genie@automagik', version: T, enabled: true }], + genieRuntime: { effectiveCwd: '/repo/a', cwdIdentity: '99:100', pid: 4242 }, + }); + const obs = parse({ stdout }); + expect(obs.status).toBe('ok'); + if (obs.status !== 'ok') throw new Error('unreachable'); + expect(obs.effectiveChildCwd).toBe('/repo/a'); + expect(obs.effectiveChildCwdIdentity).toBe('99:100'); + expect(obs.childPid).toBe(4242); + }); + + test('a malformed genieRuntime envelope is ignored (optional metadata), not a failure', () => { + const stdout = JSON.stringify({ + installed: [{ pluginId: 'genie@automagik', version: T, enabled: true }], + genieRuntime: { effectiveCwd: '', pid: 'nope' }, + }); + const obs = parse({ stdout }); + expect(obs.status).toBe('ok'); + if (obs.status !== 'ok') throw new Error('unreachable'); + expect(obs.effectiveChildCwd).toBeNull(); + expect(obs.childPid).toBeNull(); + }); +}); + +// ============================================================================ +// projectHostQuery — ONE observation feeds downstream projections +// ============================================================================ + +describe('projectHostQuery — the single downstream projection', () => { + test('an exit-0 advisory observation projects registration present AND not query-failed', () => { + const obs = parse({ stdout: pluginListJson([{ version: T }]), stderr: 'PATH advisory' }); + const projection = projectHostQuery(obs); + // The real sandbox advisory can never produce a PASS + query-failed pair. + expect(projection.queryFailed).toBe(false); + expect(projection.registration).toBe('present'); + expect(projection.installedVersion).toBe(T); + expect(projection.advisory).toBe('PATH advisory'); + }); + + test('a failed observation projects queryFailed with no registration claim', () => { + const obs = parse({ exitCode: 1, stdout: '' }); + const projection = projectHostQuery(obs); + expect(projection.queryFailed).toBe(true); + expect(projection.registration).toBe('unknown'); + expect(projection.failureCode).toBe('nonzero-exit'); + }); +}); + +// ============================================================================ +// witnessCodexCacheFamily — filesystem witness +// ============================================================================ + +describe('witnessCodexCacheFamily', () => { + function codexHomeWith(build: (familyDir: string, root: string) => void): string { + const root = freshRoot(); + const familyDir = join(root, 'plugins', 'cache', 'automagik', 'genie'); + build(familyDir, root); + return root; + } + + test('an absent family is absent', () => { + const root = freshRoot(); + expect(witnessCodexCacheFamily(root)).toEqual({ status: 'absent' }); + }); + + test('a present family reports a stable dev:ino identity + digest', () => { + const root = codexHomeWith((familyDir) => { + mkdirSync(join(familyDir, T), { recursive: true }); + }); + const witness = witnessCodexCacheFamily(root); + expect(witness.status).toBe('present'); + if (witness.status !== 'present') throw new Error('unreachable'); + expect(witness.identity).toMatch(/^\d+:\d+$/); + expect(witness.digest).toMatch(/^[0-9a-f]{64}$/); + }); + + test('a symlinked family is unsafe', () => { + const root = codexHomeWith((familyDir, base) => { + const parent = join(base, 'plugins', 'cache', 'automagik'); + mkdirSync(parent, { recursive: true }); + const real = join(base, 'elsewhere'); + mkdirSync(real, { recursive: true }); + symlinkSync(real, familyDir); + }); + const witness = witnessCodexCacheFamily(root); + expect(witness.status).toBe('unsafe'); + }); +}); + +// ============================================================================ +// assessAuthenticatedDelivery — the pure classifier (deliverables 4, 7) +// ============================================================================ + +function fullRecord(overrides: Partial = {}): AuthenticatedDeliveryRecord { + return { + targetVersion: T, + canonicalPayloadSha256: DIGEST_A, + channel: 'stable', + deliveryId: ID_128, + evidenceDigest: DIGEST_E, + platformId: 'darwin-arm64', + platformTriple: 'darwin-arm64', + releaseTag: 'v5.260712.1', + releaseName: 'genie-5.260712.1-darwin-arm64.tar.gz', + releaseManifestSha256: DIGEST_B, + artifactSha256: DIGEST_C, + installedBinarySha256: DIGEST_D, + deliveryRoot: '/home/.genie/deliveries/abc', + deliveredAt: '2026-07-12T00:00:00.000Z', + ...overrides, + }; +} +function fullExpectation(overrides: Partial = {}): ActivationDeliveryExpectation { + return { + targetVersion: T, + canonicalPayloadSha256: DIGEST_A, + channel: 'stable', + deliveryId: ID_128, + evidenceDigest: DIGEST_E, + platformId: 'darwin-arm64', + platformTriple: 'darwin-arm64', + releaseTag: 'v5.260712.1', + releaseName: 'genie-5.260712.1-darwin-arm64.tar.gz', + releaseManifestSha256: DIGEST_B, + artifactSha256: DIGEST_C, + installedBinarySha256: DIGEST_D, + deliveryRoot: '/home/.genie/deliveries/abc', + deliveredAt: '2026-07-12T00:00:00.000Z', + ...overrides, + }; +} +function present(record: AuthenticatedDeliveryRecord): DeliveryRecordReadState { + return { status: 'present', record }; +} + +describe('assessAuthenticatedDelivery — matching | absent | invalid | mismatch', () => { + test('a fully-bound record matching every expectation field is matching', () => { + expect(assessAuthenticatedDelivery(present(fullRecord()), fullExpectation())).toBe('matching'); + }); + + test('an absent record is absent', () => { + expect(assessAuthenticatedDelivery({ status: 'absent' }, fullExpectation())).toBe('absent'); + }); + + test('an on-disk invalid read-state is invalid', () => { + expect(assessAuthenticatedDelivery({ status: 'invalid', detail: 'bad json' }, fullExpectation())).toBe('invalid'); + }); + + // Structural corruption => invalid (never mismatch). + const invalidStructures: Array<{ name: string; record: Partial }> = [ + { name: 'non-release targetVersion', record: { targetVersion: '1.2.3' } }, + { name: 'short payload digest', record: { canonicalPayloadSha256: 'abc' } }, + { name: 'non-hex deliveryId', record: { deliveryId: 'ZZZZ' } }, + { name: 'malformed evidence digest', record: { evidenceDigest: 'nothex' } }, + { name: 'empty platform id', record: { platformId: '' } }, + { name: 'empty channel', record: { channel: '' } }, + { name: 'malformed platform triple', record: { platformTriple: 'Darwin ARM' } }, + { name: 'malformed manifest digest', record: { releaseManifestSha256: 'nothex' } }, + { name: 'malformed artifact digest', record: { artifactSha256: 'nothex' } }, + { name: 'malformed binary digest', record: { installedBinarySha256: 'nothex' } }, + ]; + for (const c of invalidStructures) { + test(`a record with ${c.name} is invalid`, () => { + expect(assessAuthenticatedDelivery(present(fullRecord(c.record)), fullExpectation())).toBe('invalid'); + }); + } + + // Tampering ANY bound field => mismatch (deliverable 7 + acceptance criterion). + const tampers: Array<{ name: string; record: Partial }> = [ + { + name: 'version', + record: { + targetVersion: '5.260799.9', + releaseTag: 'v5.260799.9', + releaseName: 'genie-5.260799.9-darwin-arm64.tar.gz', + }, + }, + { name: 'platform', record: { platformTriple: 'linux-x64' } }, + { name: 'manifest', record: { releaseManifestSha256: DIGEST_E } }, + { name: 'artifact', record: { artifactSha256: DIGEST_E } }, + { name: 'binary', record: { installedBinarySha256: DIGEST_E } }, + { name: 'payload', record: { canonicalPayloadSha256: DIGEST_E } }, + { name: 'delivery id', record: { deliveryId: '2'.repeat(32) } }, + { name: 'evidence digest', record: { evidenceDigest: 'f'.repeat(64) } }, + { name: 'platform id', record: { platformId: 'linux-x64-glibc' } }, + { name: 'channel', record: { channel: 'homolog' } }, + { name: 'delivery root', record: { deliveryRoot: '/tmp/evil' } }, + ]; + for (const c of tampers) { + test(`tampering the ${c.name} binding => mismatch`, () => { + expect(assessAuthenticatedDelivery(present(fullRecord(c.record)), fullExpectation())).toBe('mismatch'); + }); + } + + test('intent binding: a record minted for a different activation intent (target) => mismatch', () => { + // The record is internally consistent but bound to a different intent than the current activation. + const record = fullRecord({ + targetVersion: '5.260799.9', + releaseTag: 'v5.260799.9', + releaseName: 'genie-5.260799.9-darwin-arm64.tar.gz', + }); + expect(assessAuthenticatedDelivery(present(record), fullExpectation({ targetVersion: T }))).toBe('mismatch'); + }); + + test('downgrade binding: matching requires the record deliveryId to equal the consumed receipt id', () => { + const receiptId = '7'.repeat(32); + const bound = fullExpectation({ deliveryId: receiptId }); + expect(assessAuthenticatedDelivery(present(fullRecord({ deliveryId: receiptId })), bound)).toBe('matching'); + expect(assessAuthenticatedDelivery(present(fullRecord({ deliveryId: ID_128 })), bound)).toBe('mismatch'); + }); + + test('a full expectation rejects a minimal record lacking authenticated bindings', () => { + const minimal = { + targetVersion: T, + canonicalPayloadSha256: DIGEST_A, + channel: 'stable', + deliveryId: ID_128, + } as AuthenticatedDeliveryRecord; + expect(assessAuthenticatedDelivery(present(minimal), fullExpectation())).toBe('invalid'); + }); + + for (const field of [ + 'evidenceDigest', + 'platformId', + 'platformTriple', + 'releaseTag', + 'releaseName', + 'releaseManifestSha256', + 'artifactSha256', + 'installedBinarySha256', + 'deliveryRoot', + 'deliveredAt', + ] as const) { + test(`a record missing ${field} is invalid even for a full expectation`, () => { + const record = { ...fullRecord() }; + Reflect.deleteProperty(record, field); + expect(assessAuthenticatedDelivery(present(record as AuthenticatedDeliveryRecord), fullExpectation())).toBe( + 'invalid', + ); + }); + } + + test('an internally inconsistent release tag/name is invalid', () => { + expect(assessAuthenticatedDelivery(present(fullRecord({ releaseTag: 'v9.9.9' })), fullExpectation())).toBe( + 'invalid', + ); + expect(assessAuthenticatedDelivery(present(fullRecord({ releaseName: 'evil' })), fullExpectation())).toBe( + 'invalid', + ); + }); +}); + +// ============================================================================ +// buildDeliveryIncompleteResult — the stable typed result (deliverable 4) +// ============================================================================ + +describe('buildDeliveryIncompleteResult', () => { + test('is authority none, exit 1, deliveryComplete false, with an ANSI-free detail and recovery command', () => { + for (const assessment of ['absent', 'invalid', 'mismatch'] as const) { + const r = buildDeliveryIncompleteResult(assessment); + expect(r.code).toBe('delivery-incomplete'); + expect(r.authority).toBe('none'); + expect(r.exit).toBe(1); + expect(r.deliveryComplete).toBe(false); + expect(r.assessment).toBe(assessment); + expect(r.recovery).toContain('genie update'); + expect(r.detail).not.toContain('\x1b'); + expect(r.detail.length).toBeGreaterThan(0); + } + }); +}); diff --git a/src/lib/codex-host-observation.ts b/src/lib/codex-host-observation.ts new file mode 100644 index 000000000..6b9652d48 --- /dev/null +++ b/src/lib/codex-host-observation.ts @@ -0,0 +1,378 @@ +/** + * Group B — host-observation-attestation: the production Codex host observation + * and the pure authenticated-delivery-record classifier. + * + * Two disjoint concerns live here, both owned by Group B: + * + * 1. `CodexHostObservation` (deliverables 1 + 6): one immutable, typed result of + * ONLY facts observable from a single bounded Codex subprocess query — parsed + * plugin facts, a bounded sanitised advisory stderr, an optional child + * self-report (effective CWD / identity / PID) when the probe emits one, the + * cache-family witness, and a typed failure. It NEVER receives or infers the + * raw `thread/start.cwd` or any control-process fact — those live only in the + * black-box `CodexCwdEvidence` harness. The parser accepts advisory stderr + * ONLY with exit 0 plus exactly one schema-valid bounded JSON stdout value, + * and reports timeout, overflow, nonzero exit, malformed/duplicate JSON, + * invalid versions, duplicate registration, and unsafe cache roots as one + * ANSI-free typed failure. + * + * 2. The pure authenticated-delivery-record assessment (deliverables 4 + 7): + * `assessAuthenticatedDelivery` classifies a delivery record read-state against + * the current activation intent as `matching | absent | invalid | mismatch`, + * and `buildDeliveryIncompleteResult` is the stable `delivery-incomplete` + * result (`authority: 'none'`, exit 1, `deliveryComplete: false`). The + * activation protocol's inner guard (`beginActivation`) applies exactly this + * assessment before its first journal write. + * + * This module imports the evidence-owned binding codec plus the leaf + * `codex-release-version.ts` and `runtime-integrations.ts`; it never imports + * `codex-activation.ts`, so the activation protocol can import the assessment + * from here with no import cycle. + */ + +import { createHash } from 'node:crypto'; +import { type Stats, lstatSync, readdirSync } from 'node:fs'; +import { join } from 'node:path'; +import { + type AuthenticatedDeliveryAssessment, + type AuthenticatedDeliveryRecordFields, + type AuthenticatedDeliveryRecordReadState, + assessAuthenticatedDeliveryRecord, +} from './codex-delivery-evidence.js'; +import { parseReleaseVersion, stripControl } from './codex-release-version.js'; +import { type CommandResult, parseCodexPluginState } from './runtime-integrations.js'; + +// ============================================================================ +// Production host observation (facts observable from ONE bounded query) +// ============================================================================ + +const DEV_INO_RE = /^\d+:\d+$/; +const DEFAULT_MAX_ADVISORY_BYTES = 4 * 1024; + +/** + * A bounded, shallow witness of the Codex plugin cache family — its `dev:ino` + * identity plus a listing digest. Computed with filesystem access by the caller + * and handed to the pure parser so the observation stays a pure function of one + * subprocess result. + */ +export type HostCacheWitness = + | { status: 'absent' } + | { status: 'unsafe'; detail: string } + | { status: 'present'; digest: string; identity: string }; + +/** Runtime/plugin registration facts parsed from the bounded query. */ +export interface HostPluginFacts { + installed: boolean; + enabled: boolean | null; + /** Canonical `MAJOR.YYMMDD.N` version when installed and grammar-valid; null when absent. */ + version: string | null; +} + +/** + * An optional self-report a Genie runtime probe MAY emit about ITS OWN launched + * process. Present only when the query JSON carries a `genieRuntime` envelope + * (e.g. a Genie self-diagnostic probe); a plain `codex plugin list --json` never + * emits it. These are the child's own observable facts — never a control process. + */ +export interface HostChildSelfReport { + effectiveCwd: string; + /** `dev:ino` of the effective CWD when the probe emits it; null otherwise. */ + cwdIdentity: string | null; + pid: number; +} + +export type HostObservationFailureCode = + | 'timeout' + | 'output-overflow' + | 'nonzero-exit' + // `malformed-json` covers empty, unparseable, AND multiple concatenated values — + // anything that is not exactly one schema-valid JSON stdout value. + | 'malformed-json' + | 'invalid-plugin-version' + | 'duplicate-registration' + | 'unsafe-cache-root'; + +export type CodexHostObservation = + | { + status: 'ok'; + plugin: HostPluginFacts; + /** Sanitised, bounded advisory stderr retained ONLY with exit 0 + one valid JSON stdout; null when clean. */ + advisoryStderr: string | null; + /** The child's own effective CWD when the probe self-reports it; null otherwise. */ + effectiveChildCwd: string | null; + /** `dev:ino` of the child's effective CWD when emitted; null otherwise. */ + effectiveChildCwdIdentity: string | null; + /** The child PID when the probe self-reports it; null otherwise. */ + childPid: number | null; + cacheFamily: HostCacheWitness; + } + | { status: 'failed'; code: HostObservationFailureCode; detail: string }; + +export interface CodexHostObservationInput { + /** The one bounded subprocess result (exit code, stdout, stderr, timedOut, outputOverflow). */ + result: CommandResult; + /** The cache-family witness snapshotted around the query. */ + cacheFamily: HostCacheWitness; + /** Cap on the retained advisory stderr (bytes). Defaults to 4 KiB. */ + maxAdvisoryBytes?: number; +} + +/** + * Parse ONE bounded Codex subprocess result into a single typed observation. + * Pure: it performs no I/O and derives every fact from `input`. + */ +export function parseCodexHostObservation(input: CodexHostObservationInput): CodexHostObservation { + const { result, cacheFamily } = input; + if (result.timedOut) return failure('timeout', 'codex host observation timed out'); + if (result.outputOverflow) return failure('output-overflow', 'codex host observation exceeded the output cap'); + if (result.exitCode !== 0) return failure('nonzero-exit', `codex host observation exited ${result.exitCode}`); + if (cacheFamily.status === 'unsafe') return failure('unsafe-cache-root', cacheFamily.detail); + + const sanitizedStdout = stripControl(result.stdout).trim(); + const single = singleJsonValue(sanitizedStdout); + if (!single.ok) return failure(single.code, single.detail); + + const pluginFacts = extractPluginFacts(single.value); + if (!pluginFacts.ok) return failure(pluginFacts.code, pluginFacts.detail); + + const selfReport = extractSelfReport(single.value); + const advisoryStderr = boundedAdvisory(result.stderr, input.maxAdvisoryBytes ?? DEFAULT_MAX_ADVISORY_BYTES); + return { + status: 'ok', + plugin: pluginFacts.facts, + advisoryStderr, + effectiveChildCwd: selfReport?.effectiveCwd ?? null, + effectiveChildCwdIdentity: selfReport?.cwdIdentity ?? null, + childPid: selfReport?.pid ?? null, + cacheFamily, + }; +} + +function failure(code: HostObservationFailureCode, detail: string): CodexHostObservation { + // Sanitise the detail so every failure remains one ANSI-free typed result. + return { status: 'failed', code, detail: stripControl(detail) }; +} + +/** Exactly one JSON value after sanitisation; empty, unparseable, or a second trailing value all fail. */ +function singleJsonValue( + text: string, +): { ok: true; value: unknown } | { ok: false; code: HostObservationFailureCode; detail: string } { + if (text.length === 0) return { ok: false, code: 'malformed-json', detail: 'empty observation output' }; + try { + return { ok: true, value: JSON.parse(text) }; + } catch { + // A second concatenated JSON value also fails whole-string parse — both are + // "not exactly one schema-valid JSON value". + return { ok: false, code: 'malformed-json', detail: 'observation output was not exactly one JSON value' }; + } +} + +/** + * Extract plugin registration facts with precise typed failure codes. Duplicate + * registrations and malformed/invalid version strings come from the canonical + * `parseCodexPluginState`; the release-grammar check adds `invalid-plugin-version` + * for a syntactically-safe but non-release version. + */ +function extractPluginFacts( + value: unknown, +): { ok: true; facts: HostPluginFacts } | { ok: false; code: HostObservationFailureCode; detail: string } { + const parsed = parseCodexPluginState(JSON.stringify(value)); + if (!parsed.ok) { + if (/duplicate/i.test(parsed.detail)) return { ok: false, code: 'duplicate-registration', detail: parsed.detail }; + if (/version/i.test(parsed.detail)) return { ok: false, code: 'invalid-plugin-version', detail: parsed.detail }; + return { ok: false, code: 'malformed-json', detail: parsed.detail }; + } + if (!parsed.state.installed) { + return { ok: true, facts: { installed: false, enabled: null, version: null } }; + } + const version = parseReleaseVersion(parsed.state.version); + if (version === null) { + return { + ok: false, + code: 'invalid-plugin-version', + detail: `installed Codex plugin version fails the release grammar: ${parsed.state.version ?? 'null'}`, + }; + } + return { ok: true, facts: { installed: true, enabled: parsed.state.enabled ?? false, version: version.canonical } }; +} + +/** + * When the query JSON carries a `genieRuntime` envelope, capture the child's own + * effective CWD / identity / PID. Malformed envelopes are ignored (null) rather + * than failing the whole observation — a self-report is optional metadata. + */ +function extractSelfReport(value: unknown): HostChildSelfReport | null { + if (typeof value !== 'object' || value === null) return null; + const runtime = Reflect.get(value, 'genieRuntime'); + if (typeof runtime !== 'object' || runtime === null) return null; + const effectiveCwd = Reflect.get(runtime, 'effectiveCwd'); + const pid = Reflect.get(runtime, 'pid'); + if (typeof effectiveCwd !== 'string' || effectiveCwd.length === 0) return null; + if (typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 0) return null; + const identityRaw = Reflect.get(runtime, 'cwdIdentity'); + const cwdIdentity = typeof identityRaw === 'string' && DEV_INO_RE.test(identityRaw) ? identityRaw : null; + return { effectiveCwd, cwdIdentity, pid }; +} + +/** Retain advisory stderr only, sanitised of control sequences and bounded; null when empty. */ +function boundedAdvisory(stderr: string, maxBytes: number): string | null { + const sanitized = stripControl(stderr).trim(); + if (sanitized.length === 0) return null; + const buffer = Buffer.from(sanitized, 'utf8'); + if (buffer.length <= maxBytes) return sanitized; + return `${buffer.subarray(0, maxBytes).toString('utf8')}…`; +} + +/** + * Compute the bounded cache-family witness with filesystem access. A symlinked or + * non-directory family is `unsafe`; an absent family is `absent`. Kept here (not + * imported from the activation protocol) so this module never depends on it. + */ +export function witnessCodexCacheFamily(codexHome: string): HostCacheWitness { + const familyDir = join(codexHome, 'plugins', 'cache', 'automagik', 'genie'); + let stat: Stats; + try { + stat = lstatSync(familyDir); + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return { status: 'absent' }; + return { status: 'unsafe', detail: `cache family unreadable: ${errorText(error)}` }; + } + if (stat.isSymbolicLink()) return { status: 'unsafe', detail: 'cache family is a symlink' }; + if (!stat.isDirectory()) return { status: 'unsafe', detail: 'cache family is not a directory' }; + const digest = createHash('sha256'); + digest.update('genie-codex-cache-family-v1\0'); + try { + for (const name of readdirSync(familyDir).sort()) { + const child = lstatSync(join(familyDir, name)); + digest.update(`${name}\0${familyKind(child)}\0`); + } + } catch (error) { + return { status: 'unsafe', detail: `cache family listing failed: ${errorText(error)}` }; + } + return { status: 'present', digest: digest.digest('hex'), identity: `${stat.dev}:${stat.ino}` }; +} + +function familyKind(stat: Stats): string { + if (stat.isSymbolicLink()) return 'L'; + if (stat.isDirectory()) return 'D'; + if (stat.isFile()) return 'F'; + return 'O'; +} + +// ============================================================================ +// Downstream projection (proves ONE observation feeds every projection) +// ============================================================================ + +export interface HostQueryProjection { + registration: 'present' | 'absent' | 'unknown'; + installedVersion: string | null; + enabled: boolean | null; + /** Advisory stderr is diagnostic metadata, never a second policy decision or a query failure. */ + advisory: string | null; + /** True only when the query itself failed; a PASS can never be reported with this true. */ + queryFailed: boolean; + failureCode: HostObservationFailureCode | null; +} + +/** + * The single projection every downstream surface (doctor plugin status + the + * integration summary + the human/trailer/exit) derives from. Because it is the + * ONE observation, a `queryFailed: true` and a `registration: 'present'` PASS can + * never be reported together — the real sandbox PATH advisory rides `advisory`, + * not `queryFailed`. + */ +export function projectHostQuery(observation: CodexHostObservation): HostQueryProjection { + if (observation.status === 'failed') { + return { + registration: 'unknown', + installedVersion: null, + enabled: null, + advisory: null, + queryFailed: true, + failureCode: observation.code, + }; + } + return { + registration: observation.plugin.installed ? 'present' : 'absent', + installedVersion: observation.plugin.version, + enabled: observation.plugin.enabled, + advisory: observation.advisoryStderr, + queryFailed: false, + failureCode: null, + }; +} + +// ============================================================================ +// Authenticated delivery record + pure assessment +// ============================================================================ + +export type DeliveryAssessment = AuthenticatedDeliveryAssessment; + +/** + * The authenticated delivery record. Every field is mandatory: a legacy + * minimal/core-only record is structurally invalid and can never authorize + * activation. + */ +export type AuthenticatedDeliveryRecord = AuthenticatedDeliveryRecordFields; + +/** The read-state of the on-disk delivery record (mirrors the activation snapshot's delivery fact). */ +export type DeliveryRecordReadState = AuthenticatedDeliveryRecordReadState; + +/** The complete authenticated tuple trusted by the current activation request. */ +export type ActivationDeliveryExpectation = AuthenticatedDeliveryRecordFields; + +/** + * Pure classifier: `absent` (no record), `invalid` (present but structurally + * malformed), `mismatch` (valid but a bound value differs from the activation + * intent), or `matching` (every bound value equals the installed target and + * current activation intent). Performs no I/O. + */ +export function assessAuthenticatedDelivery( + fact: DeliveryRecordReadState, + expectation: ActivationDeliveryExpectation, +): DeliveryAssessment { + return assessAuthenticatedDeliveryRecord(fact, expectation); +} + +// ============================================================================ +// Stable `delivery-incomplete` result (authority: none, exit 1, deliveryComplete: false) +// ============================================================================ + +export interface DeliveryIncompleteResult { + code: 'delivery-incomplete'; + authority: 'none'; + exit: 1; + deliveryComplete: false; + assessment: Exclude; + recovery: string; + detail: string; +} + +/** The one recovery command every non-matching-record surface names. */ +export const DELIVERY_INCOMPLETE_RECOVERY = + 'run genie update (or genie install) to publish a verified delivery record, then genie setup --codex'; + +/** + * The stable typed `delivery-incomplete` result: no mutation authority, exit 1, + * `deliveryComplete: false`. Its `detail` is plain, ANSI-free text. + */ +export function buildDeliveryIncompleteResult( + assessment: Exclude, + detail?: string, +): DeliveryIncompleteResult { + return { + code: 'delivery-incomplete', + authority: 'none', + exit: 1, + deliveryComplete: false, + assessment, + recovery: DELIVERY_INCOMPLETE_RECOVERY, + detail: + detail ?? + `delivery record is ${assessment}; update or install must publish a matching authenticated delivery record before activation`, + }; +} + +function errorText(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} diff --git a/src/lib/codex-lifecycle-lease.test.ts b/src/lib/codex-lifecycle-lease.test.ts index 5719e56a1..92e6c9d25 100644 --- a/src/lib/codex-lifecycle-lease.test.ts +++ b/src/lib/codex-lifecycle-lease.test.ts @@ -1,12 +1,16 @@ import { afterEach, describe, expect, test } from 'bun:test'; import { + chmodSync, existsSync, + linkSync, + lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, + unlinkSync, writeFileSync, } from 'node:fs'; import { tmpdir } from 'node:os'; @@ -25,6 +29,40 @@ function freshHome(): string { const LEASE_FILE = '.codex-lifecycle.lock'; +function leaseRecord(operationId: string, pid = 424242, kind = 'rollback'): string { + return `${JSON.stringify({ schemaVersion: 1, operationId, kind, pid, startedAt: '2026-07-23T00:00:00.000Z' })}\n`; +} + +function stagingRecord(operationId: string, pid = 424242, kind = 'rollback'): string { + return `${JSON.stringify({ + schemaVersion: 1, + operationId, + kind, + pid, + startedAt: '2026-07-23T00:00:00.000Z', + stagingSlot: operationId.slice(0, 2), + })}\n`; +} + +function recoveryRecord(operationId: string, targetOperationId: string, pid = 424242, kind = 'rollback'): string { + return `${JSON.stringify({ + schemaVersion: 1, + operationId, + kind, + pid, + startedAt: '2026-07-23T00:00:00.000Z', + recoveryTargetOperationId: targetOperationId, + })}\n`; +} + +function stagingPath(genieHome: string, operationId: string): string { + return join(genieHome, `${LEASE_FILE}.staging-${operationId.slice(0, 2)}`); +} + +function stagingFiles(genieHome: string): string[] { + return readdirSync(genieHome).filter((name) => /^\.codex-lifecycle\.lock\.staging-[0-9a-f]{2}$/.test(name)); +} + afterEach(() => { for (const lease of heldLeases.splice(0)) lease.release(); for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); @@ -45,6 +83,16 @@ describe('acquireLifecycleLease — acquisition and mutual exclusion', () => { expect(existsSync(join(genieHome, LEASE_FILE))).toBe(true); }); + test('a missing GENIE_HOME is created even when directory iteration defers ENOENT until read', () => { + const fixtureRoot = freshHome(); + const genieHome = join(fixtureRoot, 'not-created-yet'); + expect(existsSync(genieHome)).toBe(false); + + const lease = hold(acquireLifecycleLease('update-delivery', { genieHome })); + expect(lease.kind).toBe('update-delivery'); + expect(existsSync(join(genieHome, LEASE_FILE))).toBe(true); + }); + test('a second acquisition while held returns a typed busy refusal naming the holder kind', () => { const genieHome = freshHome(); hold(acquireLifecycleLease('setup-activation', { genieHome })); @@ -55,6 +103,62 @@ describe('acquireLifecycleLease — acquisition and mutual exclusion', () => { expect(second.holderKind).toBe('setup-activation'); }); + test('a contender observing publication sees a complete holder record, never BUSY:unknown', () => { + const genieHome = freshHome(); + const results: { winner?: ReturnType } = {}; + const loser = acquireLifecycleLease('update-delivery', { + genieHome, + beforePublishForTest: () => { + results.winner = acquireLifecycleLease('setup-activation', { genieHome }); + }, + }); + + const winner = results.winner; + expect(winner?.ok).toBe(true); + expect(loser.ok).toBe(false); + if (loser.ok) throw new Error('unreachable'); + expect(loser.holderKind).toBe('setup-activation'); + expect(loser.detail).toContain('held by setup-activation'); + expect(loser.detail).not.toContain('schema invalid'); + expect(readdirSync(genieHome).filter((name) => name.includes('.staging-'))).toEqual([]); + if (winner?.ok) winner.release(); + }); + + test('a pre-publication failure removes its private record and leaves the stable slot acquirable', () => { + const genieHome = freshHome(); + expect(() => + acquireLifecycleLease('update-delivery', { + genieHome, + beforePublishForTest: () => { + throw new Error('forced publication barrier failure'); + }, + }), + ).toThrow('forced publication barrier failure'); + expect(existsSync(join(genieHome, LEASE_FILE))).toBe(false); + expect(readdirSync(genieHome).filter((name) => name.includes('.staging-'))).toEqual([]); + expect(hold(acquireLifecycleLease('rollback', { genieHome })).kind).toBe('rollback'); + }); + + test('a non-EEXIST publication I/O failure returns typed busy and cleans its private record', () => { + const genieHome = freshHome(); + const result = acquireLifecycleLease('update-delivery', { + genieHome, + beforePublishForTest: () => { + const staging = readdirSync(genieHome).find((name) => name.includes('.staging-')); + if (!staging) throw new Error('expected private lease record'); + rmSync(join(genieHome, staging)); + }, + }); + + expect(result.ok).toBe(false); + if (result.ok) throw new Error('unreachable'); + expect(result.holderKind).toBeNull(); + expect(result.detail).toContain('lease publication failed'); + expect(existsSync(join(genieHome, LEASE_FILE))).toBe(false); + expect(readdirSync(genieHome).filter((name) => name.includes('.staging-'))).toEqual([]); + expect(hold(acquireLifecycleLease('rollback', { genieHome })).kind).toBe('rollback'); + }); + test('release makes the slot acquirable again and is idempotent', () => { const genieHome = freshHome(); const first = acquireLifecycleLease('update-delivery', { genieHome }); @@ -110,7 +214,7 @@ describe('acquireLifecycleLease — stale holder supersession', () => { const staleFiles = readdirSync(genieHome).filter((name) => name.startsWith(`${LEASE_FILE}.stale-`)); expect(staleFiles.length).toBe(1); - expect(staleFiles[0]).toBe(`${LEASE_FILE}.stale-${lease.operationId}`); + expect(staleFiles[0]).toBe(`${LEASE_FILE}.stale-${staleRecord.operationId}`); const superseded = JSON.parse(readFileSync(join(genieHome, staleFiles[0]), 'utf8')); expect(superseded.operationId).toBe(staleRecord.operationId); }); @@ -141,6 +245,107 @@ describe('acquireLifecycleLease — stale holder supersession', () => { const result = acquireLifecycleLease('update-delivery', { genieHome, isProcessAlive: () => true }); expect(result.ok).toBe(false); }); + + test('PID reuse after dead-holder capture restores the stable record and stays busy', () => { + const genieHome = freshHome(); + const leasePath = join(genieHome, LEASE_FILE); + const stale = leaseRecord('f'.repeat(32), 424242, 'rollback'); + writeFileSync(leasePath, stale); + let probes = 0; + + const result = acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: () => { + probes += 1; + return probes > 1; + }, + }); + + expect(result.ok).toBe(false); + if (result.ok) throw new Error('unreachable'); + expect(result.holderKind).toBe('rollback'); + expect(probes).toBe(2); + expect(readFileSync(leasePath, 'utf8')).toBe(stale); + }); + + test('a fresh holder installed immediately after stale capture is preserved and reported as contention', () => { + const genieHome = freshHome(); + const leasePath = join(genieHome, LEASE_FILE); + const staleId = 'c'.repeat(32); + const replacementId = 'd'.repeat(32); + const replacement = leaseRecord(replacementId, process.pid, 'install-converge'); + writeFileSync(leasePath, leaseRecord(staleId, 424242, 'rollback')); + + const result = acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: (pid) => pid !== 424242, + afterCaptureForTest: (event) => { + if (event.operation === 'stale-supersede') writeFileSync(event.path, replacement); + }, + }); + + expect(result.ok).toBe(false); + if (result.ok) throw new Error('unreachable'); + expect(result.holderKind).toBe('install-converge'); + expect(readFileSync(leasePath, 'utf8')).toBe(replacement); + const evidence = readdirSync(genieHome).filter((name) => name.startsWith(`${LEASE_FILE}.stale-`)); + expect(evidence).toHaveLength(1); + expect(JSON.parse(readFileSync(join(genieHome, evidence[0]), 'utf8')).operationId).toBe(staleId); + }); + + test('ordinary filesystem loss after dead-holder observation returns typed busy instead of throwing', () => { + const genieHome = freshHome(); + const leasePath = join(genieHome, LEASE_FILE); + writeFileSync(leasePath, leaseRecord('1'.repeat(32), 424242, 'rollback')); + + const result = acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: () => false, + afterDeadHolderObservedForTest: () => rmSync(genieHome, { recursive: true, force: true }), + }); + + expect(result.ok).toBe(false); + if (result.ok) throw new Error('unreachable'); + expect(result.reason).toBe('codex-lifecycle-busy'); + expect(result.holderKind).toBeNull(); + expect(result.detail).toContain('stale-holder recovery claim failed'); + }); + + test('an intentional post-capture test callback exception remains distinct', () => { + const genieHome = freshHome(); + writeFileSync(join(genieHome, LEASE_FILE), leaseRecord('2'.repeat(32), 424242, 'rollback')); + + expect(() => + acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: () => false, + afterCaptureForTest: (event) => { + if (event.operation === 'stale-supersede') throw new Error('intentional capture barrier'); + }, + }), + ).toThrow('intentional capture barrier'); + expect(readFileSync(join(genieHome, LEASE_FILE), 'utf8')).toBe(leaseRecord('2'.repeat(32), 424242, 'rollback')); + }); + + test('a fresh recovery claimant installed after dead-claim capture is preserved and reported exactly', () => { + const genieHome = freshHome(); + const claimPath = join(genieHome, `${LEASE_FILE}.recovery`); + const replacement = recoveryRecord('4'.repeat(32), '5'.repeat(32), process.pid, 'setup-activation'); + writeFileSync(claimPath, recoveryRecord('3'.repeat(32), '5'.repeat(32))); + + const result = acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: (pid) => pid === process.pid, + afterCaptureForTest: (event) => { + if (event.operation === 'recovery-claim') writeFileSync(event.path, replacement); + }, + }); + + expect(result.ok).toBe(false); + if (result.ok) throw new Error('unreachable'); + expect(result.holderKind).toBe('setup-activation'); + expect(readFileSync(claimPath, 'utf8')).toBe(replacement); + }); }); describe('acquireLifecycleLease — fail-closed invalid lease files', () => { @@ -179,6 +384,156 @@ describe('acquireLifecycleLease — fail-closed invalid lease files', () => { }); }); +describe('acquireLifecycleLease — abandoned private-record recovery', () => { + test('a fully valid private record whose PID is provably dead is recovered before acquisition', () => { + const genieHome = freshHome(); + const operationId = '1'.repeat(32); + const abandoned = stagingPath(genieHome, operationId); + writeFileSync(abandoned, stagingRecord(operationId)); + + const lease = hold(acquireLifecycleLease('update-delivery', { genieHome, isProcessAlive: () => false })); + expect(lease.kind).toBe('update-delivery'); + expect(existsSync(abandoned)).toBe(false); + expect(stagingFiles(genieHome)).toEqual([]); + }); + + test('live and indeterminate private records are preserved fail-closed', () => { + const genieHome = freshHome(); + const liveId = '2'.repeat(32); + const indeterminateId = 'b'.repeat(32); + const live = stagingPath(genieHome, liveId); + const indeterminate = stagingPath(genieHome, indeterminateId); + writeFileSync(live, stagingRecord(liveId, 424242)); + writeFileSync(indeterminate, stagingRecord(indeterminateId, 424243)); + + hold( + acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: (pid) => { + if (pid === 424242) return true; + throw new Error('indeterminate liveness probe'); + }, + }), + ); + expect(readFileSync(live, 'utf8')).toBe(stagingRecord(liveId, 424242)); + expect(readFileSync(indeterminate, 'utf8')).toBe(stagingRecord(indeterminateId, 424243)); + }); + + test('PID reuse between dead probes preserves the private record', () => { + const genieHome = freshHome(); + const operationId = '3'.repeat(32); + const reused = stagingPath(genieHome, operationId); + writeFileSync(reused, stagingRecord(operationId)); + let probes = 0; + + hold( + acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: () => { + probes += 1; + return probes > 1; + }, + }), + ); + expect(probes).toBe(2); + expect(readFileSync(reused, 'utf8')).toBe(stagingRecord(operationId)); + }); + + test('invalid, oversized, symlinked, and filename-mismatched private records are preserved', () => { + const genieHome = freshHome(); + const malformedId = '4'.repeat(32); + const oversizedId = '5'.repeat(32); + const symlinkId = '6'.repeat(32); + const mismatchedId = '7'.repeat(32); + const malformed = stagingPath(genieHome, malformedId); + const oversized = stagingPath(genieHome, oversizedId); + const symlink = stagingPath(genieHome, symlinkId); + const mismatched = stagingPath(genieHome, mismatchedId); + const decoy = join(genieHome, 'staging-decoy.json'); + writeFileSync(malformed, '{ invalid'); + writeFileSync(oversized, 'x'.repeat(17 * 1024)); + writeFileSync(decoy, stagingRecord(symlinkId)); + symlinkSync(decoy, symlink); + writeFileSync(mismatched, stagingRecord('8'.repeat(32))); + + hold(acquireLifecycleLease('update-delivery', { genieHome, isProcessAlive: () => false })); + expect(readFileSync(malformed, 'utf8')).toBe('{ invalid'); + expect(readFileSync(oversized, 'utf8')).toBe('x'.repeat(17 * 1024)); + expect(lstatSync(symlink).isSymbolicLink()).toBe(true); + expect(readFileSync(mismatched, 'utf8')).toBe(stagingRecord('8'.repeat(32))); + }); + + test('unreadable and multiply-linked private records are preserved', () => { + const genieHome = freshHome(); + const unreadableId = '9'.repeat(32); + const linkedId = 'a'.repeat(32); + const unreadable = stagingPath(genieHome, unreadableId); + const linked = stagingPath(genieHome, linkedId); + const foreignLink = join(genieHome, 'foreign-hardlink.json'); + writeFileSync(unreadable, stagingRecord(unreadableId)); + chmodSync(unreadable, 0o000); + writeFileSync(linked, stagingRecord(linkedId)); + linkSync(linked, foreignLink); + + try { + hold(acquireLifecycleLease('update-delivery', { genieHome, isProcessAlive: () => false })); + expect(existsSync(unreadable)).toBe(true); + expect(existsSync(linked)).toBe(true); + expect(lstatSync(linked).nlink).toBe(2); + } finally { + chmodSync(unreadable, 0o600); + } + }); + + test('recovery work is bounded to sixteen private records per acquisition', () => { + const genieHome = freshHome(); + for (let index = 1; index <= 20; index += 1) { + const operationId = `${index.toString(16).padStart(2, '0')}${'0'.repeat(30)}`; + writeFileSync(stagingPath(genieHome, operationId), stagingRecord(operationId, 420000 + index)); + } + + hold(acquireLifecycleLease('update-delivery', { genieHome, isProcessAlive: () => false })); + expect(stagingFiles(genieHome)).toHaveLength(4); + }); + + test('a replacement installed immediately after staging capture survives recovery byte-for-byte', () => { + const genieHome = freshHome(); + const operationId = 'e'.repeat(32); + const abandoned = stagingPath(genieHome, operationId); + const replacement = 'foreign staging replacement\n'; + writeFileSync(abandoned, stagingRecord(operationId)); + + hold( + acquireLifecycleLease('update-delivery', { + genieHome, + isProcessAlive: () => false, + afterCaptureForTest: (event) => { + if (event.operation === 'staging-recovery') writeFileSync(event.path, replacement); + }, + }), + ); + expect(readFileSync(abandoned, 'utf8')).toBe(replacement); + }); + + test('fixed staging slots recover independently of 1,210 crowded directory entries', () => { + const genieHome = freshHome(); + for (let index = 0; index < 1_210; index += 1) { + writeFileSync( + join(genieHome, `${LEASE_FILE}.staging-noise-${index.toString().padStart(4, '0')}`), + '{ preserved invalid debris', + ); + } + const operationId = `fe${'0'.repeat(30)}`; + const recoverable = stagingPath(genieHome, operationId); + writeFileSync(recoverable, stagingRecord(operationId)); + + const lease = hold(acquireLifecycleLease('rollback', { genieHome, isProcessAlive: () => false })); + expect(lease.kind).toBe('rollback'); + expect(existsSync(recoverable)).toBe(false); + expect(readdirSync(genieHome).filter((name) => name.includes('staging-noise-'))).toHaveLength(1_210); + }); +}); + describe('acquireLifecycleLease — operation-id fencing', () => { test('assertOperation accepts the held id and rejects a foreign id', () => { const genieHome = freshHome(); @@ -204,6 +559,69 @@ describe('acquireLifecycleLease — operation-id fencing', () => { expect(JSON.parse(readFileSync(leasePath, 'utf8')).operationId).toBe('e'.repeat(32)); }); + for (const [name, replacement] of [ + ['malformed', '{ not a lease'], + ['oversized', 'x'.repeat(17 * 1024)], + ] as const) { + test(`release preserves a ${name} replacement byte-for-byte`, () => { + const genieHome = freshHome(); + const leasePath = join(genieHome, LEASE_FILE); + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + if (!lease.ok) throw new Error('unreachable'); + writeFileSync(leasePath, replacement); + + lease.release(); + expect(readFileSync(leasePath, 'utf8')).toBe(replacement); + }); + } + + test('release preserves an unreadable replacement', () => { + const genieHome = freshHome(); + const leasePath = join(genieHome, LEASE_FILE); + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + if (!lease.ok) throw new Error('unreachable'); + chmodSync(leasePath, 0o000); + + try { + lease.release(); + expect(existsSync(leasePath)).toBe(true); + } finally { + chmodSync(leasePath, 0o600); + } + }); + + test('release preserves a symlink replacement and its target', () => { + const genieHome = freshHome(); + const leasePath = join(genieHome, LEASE_FILE); + const decoy = join(genieHome, 'replacement-decoy.json'); + const decoyBytes = leaseRecord('f'.repeat(32)); + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + if (!lease.ok) throw new Error('unreachable'); + unlinkSync(leasePath); + writeFileSync(decoy, decoyBytes); + symlinkSync(decoy, leasePath); + + lease.release(); + expect(lstatSync(leasePath).isSymbolicLink()).toBe(true); + expect(readFileSync(decoy, 'utf8')).toBe(decoyBytes); + }); + + test('release removes only its captured generation when a foreign replacement arrives immediately after capture', () => { + const genieHome = freshHome(); + const leasePath = join(genieHome, LEASE_FILE); + const replacement = leaseRecord('0'.repeat(32), process.pid, 'rollback'); + const lease = acquireLifecycleLease('update-delivery', { + genieHome, + afterCaptureForTest: (event) => { + if (event.operation === 'release') writeFileSync(event.path, replacement); + }, + }); + if (!lease.ok) throw new Error('unreachable'); + + lease.release(); + expect(readFileSync(leasePath, 'utf8')).toBe(replacement); + }); + test('assertOperation on a released lease throws', () => { const genieHome = freshHome(); const lease = acquireLifecycleLease('update-delivery', { genieHome }); @@ -214,7 +632,7 @@ describe('acquireLifecycleLease — operation-id fencing', () => { }); describe('acquireLifecycleLease — real two-process race', () => { - test('exactly one of two spawned processes wins the O_EXCL create', async () => { + test('exactly one of two spawned processes wins stable publication', async () => { const genieHome = freshHome(); const script = join(genieHome, 'race.ts'); const modulePath = join(import.meta.dir, 'codex-lifecycle-lease.ts'); @@ -292,8 +710,8 @@ describe('acquireLifecycleLease — real two-process cross-kind races (Group D)' const losers = outcomes.filter((o) => o.result === 'BUSY'); expect(winners.length).toBe(1); expect(losers.length).toBe(1); - // The loser names the winner's kind (or null on a same-directory rename race window). - expect([winners[0].holderKind, null]).toContain(losers[0].holderKind); + expect(losers[0].holderKind).toBe(winners[0].holderKind); + expect(losers[0].holderKind).not.toBeNull(); }, 20_000); } }); diff --git a/src/lib/codex-lifecycle-lease.ts b/src/lib/codex-lifecycle-lease.ts index 692f9baa8..ab9542733 100644 --- a/src/lib/codex-lifecycle-lease.ts +++ b/src/lib/codex-lifecycle-lease.ts @@ -12,20 +12,44 @@ * * The lease grants mutual exclusion only. It never grants recovery authority: * that comes exclusively from the intent-phase truth table plus a fresh external - * retirement assertion. Acquisition is a single `O_EXCL` create; a provably dead - * PID (host-local `kill(pid,0)` → ESRCH) is superseded exactly once via a - * non-overwriting rename that retains forensic evidence; a live or indeterminate - * holder is always busy with no TTL, force flag, or consent override. PID reuse - * can only make a dead holder look live, which fails safe (stays busy). + * retirement assertion. Acquisition writes a complete fsynced private `O_EXCL` + * record, then atomically publishes it at the stable path without overwrite. A + * provably dead PID (host-local `kill(pid,0)` → ESRCH) is superseded under a + * complete, no-clobber recovery claim, with atomic capture, identity revalidation, + * and durable forensic evidence. Abandoned recovery claims are reclaimed only + * after bounded identity and liveness rechecks. A live or indeterminate holder is + * always busy with no TTL, force flag, or consent override. PID reuse can only + * make a dead holder look live, which fails safe (stays busy). * * Boundary: a shared `GENIE_HOME` across PID namespaces (containers) is outside * this contract — cross-namespace PIDs are not comparable. */ import { randomBytes } from 'node:crypto'; -import { closeSync, constants as fsConstants, fsyncSync, mkdirSync, openSync, writeSync } from 'node:fs'; +import { + type Stats, + closeSync, + constants as fsConstants, + fsyncSync, + linkSync, + lstatSync, + mkdirSync, + mkdtempSync, + openSync, + readlinkSync, + renameSync, + rmdirSync, + symlinkSync, + unlinkSync, + writeSync, +} from 'node:fs'; import { join } from 'node:path'; -import { readBoundedRegularFile, renameNonOverwriting, unlinkWithParentFsync } from './codex-activation-persistence.js'; +import { + fsyncParentDir, + readBoundedRegularFile, + renameNonOverwriting, + unlinkWithParentFsync, +} from './codex-activation-persistence.js'; import { resolveGenieHome } from './genie-home.js'; /** Lifecycle operations that may hold the lease; the kind names the holder in refusals. */ @@ -49,7 +73,10 @@ const LEASE_KINDS: ReadonlySet = new Set([ const LEASE_SCHEMA_VERSION = 1 as const; const LEASE_FILE_NAME = '.codex-lifecycle.lock'; const MAX_LEASE_BYTES = 16 * 1024; +const STAGING_SLOT_COUNT = 256; +const MAX_STAGING_RECOVERIES = 16; const OPERATION_ID_RE = /^[0-9a-f]{32}$/; +const STAGING_SLOT_RE = /^[0-9a-f]{2}$/; interface LeaseRecord { schemaVersion: typeof LEASE_SCHEMA_VERSION; @@ -57,6 +84,8 @@ interface LeaseRecord { kind: LifecycleLeaseKind; pid: number; startedAt: string; + stagingSlot?: string; + recoveryTargetOperationId?: string; } /** Thrown when a fenced transition carries an operation ID that no longer holds the lease. */ @@ -92,16 +121,33 @@ export interface LifecycleLeaseBusy { export type LifecycleLeaseResult = HeldLifecycleLease | LifecycleLeaseBusy; +export interface LifecycleLeaseCaptureEvent { + operation: 'release' | 'stale-supersede' | 'staging-recovery' | 'recovery-claim'; + path: string; + capturedPath: string; +} + export interface AcquireLeaseOptions { genieHome?: string; /** Injectable liveness probe (host `kill(pid,0)` by default) for deterministic tests. */ isProcessAlive?: (pid: number) => boolean; now?: () => Date; + /** + * Test-only barrier after the complete private record is fsynced but before it + * is published at the stable lease path. + */ + beforePublishForTest?: () => void; + /** Test-only barrier after a dead stable holder is observed but before capture. */ + afterDeadHolderObservedForTest?: () => void; + /** Test-only barrier after stale-recovery ownership is durably published. */ + afterRecoveryClaimForTest?: () => void; + /** Test-only replacement seam after a pathname is atomically captured. */ + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void; } /** * Acquire the lifecycle lease for `kind`, or return a typed busy refusal naming - * the current holder. Exactly one concurrent caller can win the `O_EXCL` create. + * the current holder. Exactly one concurrent caller can win stable publication. */ export function acquireLifecycleLease( kind: LifecycleLeaseKind, @@ -112,26 +158,71 @@ export function acquireLifecycleLease( const isAlive = options.isProcessAlive ?? defaultIsProcessAlive; const now = options.now ?? (() => new Date()); - const first = tryCreateLease(path, kind, now); + const abandonedClaim = recoverAbandonedRecoveryClaim(path, isAlive, options.afterCaptureForTest); + if (abandonedClaim.status === 'active') { + return busy(abandonedClaim.holderKind, 'another acquirer is recovering a stale lifecycle holder'); + } + if (abandonedClaim.status === 'invalid') { + return busy(null, 'stale-holder recovery claim is invalid or changed while being inspected'); + } + if (abandonedClaim.status === 'error') return busy(null, abandonedClaim.detail); + + recoverDeadStagingRecords(path, isAlive, options.afterCaptureForTest); + const first = tryCreateLease(path, kind, now, undefined, options.beforePublishForTest, options.afterCaptureForTest); if (first.ok) return first.lease; if (first.reason !== 'exists') return busy(first.holderKind, first.detail); - const holder = readLeaseHolder(path); - if (holder.status !== 'valid') { + const holder = readLeaseSnapshot(path); + if (holder === null) { // Symlinked, non-regular, oversized, or schema-invalid lease files fail // closed as busy — never granted, never silently deleted. - return busy(null, holder.detail); + const invalid = readLeaseHolder(path); + if (invalid.status === 'valid') { + return busy(invalid.record.kind, `held by ${invalid.record.kind}; lease publication is stabilizing`); + } + return busy(null, invalid.detail); } if (!pidIsProvablyDead(holder.record.pid, isAlive)) { return busy(holder.record.kind, `held by ${holder.record.kind} (pid ${holder.record.pid})`); } - // Dead holder: supersede via a non-overwriting rename that retains evidence, - // then retry the atomic create exactly once. A competitor that grabbed the - // slot during the window keeps us busy. + // Dead holder: elect one recovery owner, retain a no-clobber evidence link, + // then atomically capture and revalidate the observed pathname. Losers never + // touch the stable path and name the recovery owner's operation kind. + options.afterDeadHolderObservedForTest?.(); const acquirerOperationId = mintOperationId(); - supersedeStaleLease(path, acquirerOperationId); - const second = tryCreateLease(path, kind, now, acquirerOperationId); + const supersession = supersedeStaleLease( + path, + holder, + kind, + now, + acquirerOperationId, + isAlive, + options.afterRecoveryClaimForTest, + options.afterCaptureForTest, + ); + if (supersession.status === 'error') return busy(null, supersession.detail); + if (supersession.status === 'contended') { + const contender = readLeaseHolder(path); + const contenderKind = contender.status === 'valid' ? contender.record.kind : null; + return busy(contenderKind, 'observed stale holder changed before supersession'); + } + if (supersession.status === 'claimed') { + return busy(supersession.holderKind, 'another acquirer is recovering the observed stale holder'); + } + let second: CreateResult; + try { + second = tryCreateLease( + path, + kind, + now, + acquirerOperationId, + options.beforePublishForTest, + options.afterCaptureForTest, + ); + } finally { + releaseRecoveryClaim(supersession.claim); + } if (second.ok) return second.lease; if (second.reason === 'exists') { const contender = readLeaseHolder(path); @@ -151,13 +242,30 @@ type CreateResult = | { ok: false; reason: 'exists' } | { ok: false; reason: 'error'; holderKind: null; detail: string }; +interface StagedLeaseRecord { + path: string; + slot: string; + snapshot: LeaseSnapshot; +} + function tryCreateLease( path: string, kind: LifecycleLeaseKind, now: () => Date, operationId = mintOperationId(), + beforePublishForTest?: () => void, + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, ): CreateResult { - mkdirSync(dirOf(path), { recursive: true }); + try { + mkdirSync(dirOf(path), { recursive: true }); + } catch (error) { + return { + ok: false, + reason: 'error', + holderKind: null, + detail: `lease directory creation failed: ${errorText(error)}`, + }; + } const record: LeaseRecord = { schemaVersion: LEASE_SCHEMA_VERSION, operationId, @@ -165,25 +273,107 @@ function tryCreateLease( pid: process.pid, startedAt: now().toISOString(), }; - let fd: number; + const staged = stageLeaseRecord(path, record); + if (!staged.ok) return { ok: false, reason: 'error', holderKind: null, detail: staged.detail }; + const { path: stagingPath } = staged; + record.stagingSlot = staged.slot; try { - fd = openSync(path, fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL, 0o600); + beforePublishForTest?.(); } catch (error) { - if ((error as NodeJS.ErrnoException).code === 'EEXIST') return { ok: false, reason: 'exists' }; - return { ok: false, reason: 'error', holderKind: null, detail: errorText(error) }; + cleanupStagedGenerationOrThrow(staged, error, 'lifecycle lease test barrier and cleanup both failed'); + throw error; } + let publication: ReturnType; try { - const buffer = Buffer.from(`${JSON.stringify(record)}\n`, 'utf8'); - let written = 0; - while (written < buffer.length) written += writeSync(fd, buffer, written, buffer.length - written, null); - fsyncSync(fd); - } finally { - closeSync(fd); + publication = renameNonOverwriting(stagingPath, path); + } catch (error) { + const cleanupDetail = cleanupStagedGenerationDetail(staged); + return { + ok: false, + reason: 'error', + holderKind: null, + detail: `lease publication failed: ${errorText(error)}${cleanupDetail}`, + }; + } + if (!publication.moved) { + // `renameNonOverwriting` already removed this generation's losing private + // sibling. The bound cleanup only fsyncs absence or removes the same inode; + // a contender that reused the fixed slot is preserved. + const cleanupDetail = cleanupStagedGenerationDetail(staged); + if (cleanupDetail) { + return { + ok: false, + reason: 'error', + holderKind: null, + detail: `another acquirer won publication${cleanupDetail}`, + }; + } + return { ok: false, reason: 'exists' }; + } + const published = readLeaseSnapshot(path); + if (published === null || published.record.operationId !== operationId) { + return { + ok: false, + reason: 'error', + holderKind: null, + detail: 'published lease ownership could not be verified', + }; } - return { ok: true, lease: makeHeldLease(path, record) }; + return { ok: true, lease: makeHeldLease(path, record, published.fingerprint, afterCaptureForTest) }; } -function makeHeldLease(path: string, record: LeaseRecord): HeldLifecycleLease { +function stageLeaseRecord( + path: string, + record: LeaseRecord, +): ({ ok: true } & StagedLeaseRecord) | { ok: false; detail: string } { + let stagingPath = ''; + let stagingSlot = ''; + let fd: number | null = null; + for (const slot of stagingSlotsFor(record.operationId)) { + const candidate = `${path}.staging-${slot}`; + try { + fd = openSync(candidate, fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL, 0o600); + stagingPath = candidate; + stagingSlot = slot; + break; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'EEXIST') continue; + return { ok: false, detail: errorText(error) }; + } + } + if (fd === null) return { ok: false, detail: `all ${STAGING_SLOT_COUNT} lifecycle lease staging slots are occupied` }; + record.stagingSlot = stagingSlot; + try { + try { + const buffer = Buffer.from(`${JSON.stringify(record)}\n`, 'utf8'); + let written = 0; + while (written < buffer.length) written += writeSync(fd, buffer, written, buffer.length - written, null); + fsyncSync(fd); + } finally { + closeSync(fd); + } + } catch (error) { + const cleanupDetail = cleanupStagingDetail(stagingPath); + return { ok: false, detail: `lifecycle lease staging write failed: ${errorText(error)}${cleanupDetail}` }; + } + const snapshot = readLeaseSnapshot(stagingPath, true); + if ( + snapshot === null || + snapshot.record.operationId !== record.operationId || + snapshot.record.stagingSlot !== stagingSlot + ) { + const cleanupDetail = cleanupStagingDetail(stagingPath); + return { ok: false, detail: `lifecycle lease staging verification failed${cleanupDetail}` }; + } + return { ok: true, path: stagingPath, slot: stagingSlot, snapshot }; +} + +function makeHeldLease( + path: string, + record: LeaseRecord, + fingerprint: LeaseFingerprint, + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, +): HeldLifecycleLease { let released = false; return { ok: true, @@ -194,19 +384,30 @@ function makeHeldLease(path: string, record: LeaseRecord): HeldLifecycleLease { if (operationId !== record.operationId) { throw new LifecycleFencingError(`operation ${operationId} does not match held ${record.operationId}`); } - const holder = readLeaseHolder(path); - if (holder.status !== 'valid' || holder.record.operationId !== record.operationId) { + const holder = readLeaseSnapshot(path); + if ( + holder === null || + holder.record.operationId !== record.operationId || + !sameLeaseFingerprint(fingerprint, holder.fingerprint) + ) { throw new LifecycleFencingError('on-disk lease no longer matches this operation (superseded)'); } }, release(): void { if (released) return; released = true; - const holder = readLeaseHolder(path); - // Only delete the lease if it is still ours; a supersession may have - // replaced it with another acquirer's record. - if (holder.status === 'valid' && holder.record.operationId !== record.operationId) return; - unlinkWithParentFsync(path); + const holder = readLeaseSnapshot(path); + // Compare-and-delete only: malformed, oversized, unreadable, symlinked, or foreign + // replacements are never ours and must remain byte-for-byte in place. + if ( + holder === null || + holder.record.operationId !== record.operationId || + !sameLeaseFingerprint(fingerprint, holder.fingerprint) + ) { + return; + } + const capture = captureExactLeasePath(path, holder, 'release', afterCaptureForTest); + if (capture.status === 'captured') discardCapturedPath(capture.captured); }, }; } @@ -233,22 +434,539 @@ function parseLeaseRecord(content: string): LeaseRecord | null { } if (typeof parsed !== 'object' || parsed === null) return null; const record = parsed as Record; - const { schemaVersion, operationId, kind, pid, startedAt } = record; + const { schemaVersion, operationId, kind, pid, startedAt, stagingSlot, recoveryTargetOperationId } = record; if (schemaVersion !== LEASE_SCHEMA_VERSION) return null; if (typeof operationId !== 'string' || !OPERATION_ID_RE.test(operationId)) return null; if (typeof kind !== 'string' || !LEASE_KINDS.has(kind)) return null; if (typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 0) return null; if (typeof startedAt !== 'string' || startedAt.length === 0) return null; - return { schemaVersion: LEASE_SCHEMA_VERSION, operationId, kind: kind as LifecycleLeaseKind, pid, startedAt }; + if (stagingSlot !== undefined && (typeof stagingSlot !== 'string' || !STAGING_SLOT_RE.test(stagingSlot))) return null; + if ( + recoveryTargetOperationId !== undefined && + (typeof recoveryTargetOperationId !== 'string' || !OPERATION_ID_RE.test(recoveryTargetOperationId)) + ) { + return null; + } + return { + schemaVersion: LEASE_SCHEMA_VERSION, + operationId, + kind: kind as LifecycleLeaseKind, + pid, + startedAt, + ...(typeof stagingSlot === 'string' ? { stagingSlot } : {}), + ...(typeof recoveryTargetOperationId === 'string' ? { recoveryTargetOperationId } : {}), + }; +} + +function supersedeStaleLease( + path: string, + observed: LeaseSnapshot, + kind: LifecycleLeaseKind, + now: () => Date, + operationId: string, + isAlive: (pid: number) => boolean, + afterRecoveryClaimForTest?: () => void, + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, +): SupersedeResult { + const claimResult = acquireRecoveryClaim( + path, + observed, + kind, + now, + operationId, + isAlive, + afterRecoveryClaimForTest, + afterCaptureForTest, + ); + if (claimResult.status !== 'acquired') return claimResult; + const claim = claimResult.claim; + const evidencePath = `${path}.stale-${observed.record.operationId}`; + let createdEvidence = false; + try { + linkSync(path, evidencePath); + createdEvidence = true; + fsyncParentDir(evidencePath); + } catch (error) { + const code = (error as NodeJS.ErrnoException).code; + if (code !== 'EEXIST') { + releaseRecoveryClaim(claim); + return { status: 'error', detail: `stale-holder evidence publication failed: ${errorText(error)}` }; + } + } + const evidence = readLeaseSnapshot(evidencePath); + const current = readLeaseSnapshot(path); + if ( + evidence === null || + current === null || + evidence.record.operationId !== observed.record.operationId || + current.record.operationId !== observed.record.operationId || + !sameLeaseGeneration(observed.fingerprint, evidence.fingerprint) || + !sameLeaseGeneration(evidence.fingerprint, current.fingerprint) + ) { + if (createdEvidence) removeClaimedEvidence(evidencePath, evidence); + releaseRecoveryClaim(claim); + return { status: 'contended' }; + } + if (!pidIsProvablyDead(observed.record.pid, isAlive)) { + if (createdEvidence) removeClaimedEvidence(evidencePath, evidence); + releaseRecoveryClaim(claim); + return { status: 'contended' }; + } + let capture: CaptureAttempt; + try { + capture = captureExactLeasePath(path, current, 'stale-supersede', afterCaptureForTest); + } catch (error) { + if (createdEvidence) removeClaimedEvidence(evidencePath, evidence); + releaseRecoveryClaim(claim); + throw error; + } + if (capture.status === 'error') { + if (createdEvidence) removeClaimedEvidence(evidencePath, evidence); + releaseRecoveryClaim(claim); + return { status: 'error', detail: capture.detail }; + } + if (capture.status !== 'captured') { + if (createdEvidence) removeClaimedEvidence(evidencePath, evidence); + releaseRecoveryClaim(claim); + return { status: 'contended' }; + } + try { + discardCapturedPath(capture.captured); + } catch (error) { + restoreOrPreserveCapturedPath(capture.captured, path); + releaseRecoveryClaim(claim); + return { status: 'error', detail: `stale-holder captured-record cleanup failed: ${errorText(error)}` }; + } + return { status: 'superseded', claim }; +} + +interface RecoveryClaim { + path: string; + snapshot: LeaseSnapshot; +} + +type SupersedeResult = + | { status: 'superseded'; claim: RecoveryClaim } + | { status: 'claimed'; holderKind: LifecycleLeaseKind | null } + | { status: 'contended' } + | { status: 'error'; detail: string }; + +type RecoveryClaimRecovery = + | { status: 'absent' } + | { status: 'recovered' } + | { status: 'active'; holderKind: LifecycleLeaseKind } + | { status: 'invalid' } + | { status: 'error'; detail: string }; + +function acquireRecoveryClaim( + path: string, + observed: LeaseSnapshot, + kind: LifecycleLeaseKind, + now: () => Date, + operationId: string, + isAlive: (pid: number) => boolean, + afterRecoveryClaimForTest?: () => void, + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, +): { status: 'acquired'; claim: RecoveryClaim } | Exclude { + const claimPath = `${path}.recovery`; + for (let attempt = 0; attempt < 2; attempt += 1) { + const record: LeaseRecord = { + schemaVersion: LEASE_SCHEMA_VERSION, + operationId, + kind, + pid: process.pid, + startedAt: now().toISOString(), + recoveryTargetOperationId: observed.record.operationId, + }; + const staged = stageLeaseRecord(path, record); + if (!staged.ok) { + return { status: 'error', detail: `stale-holder recovery claim failed: ${staged.detail}` }; + } + let publication: ReturnType; + try { + publication = renameNonOverwriting(staged.path, claimPath); + } catch (error) { + const cleanupDetail = cleanupStagedGenerationDetail(staged); + return { + status: 'error', + detail: `stale-holder recovery claim failed: ${errorText(error)}${cleanupDetail}`, + }; + } + const cleanupDetail = cleanupStagedGenerationDetail(staged); + if (publication.moved) { + const published = readLeaseSnapshot(claimPath); + if (!recoveryClaimMatches(published, operationId, observed.record.operationId)) { + return { status: 'error', detail: 'stale-holder recovery claim ownership could not be verified' }; + } + const claim = { path: claimPath, snapshot: published }; + if (cleanupDetail) { + releaseRecoveryClaim(claim); + return { + status: 'error', + detail: `stale-holder recovery claim staging cleanup failed${cleanupDetail}`, + }; + } + try { + afterRecoveryClaimForTest?.(); + } catch (error) { + releaseRecoveryClaim(claim); + throw error; + } + return { status: 'acquired', claim }; + } + if (cleanupDetail) { + return { + status: 'error', + detail: `stale-holder recovery claim staging cleanup failed${cleanupDetail}`, + }; + } + + const recovery = recoverAbandonedRecoveryClaim(path, isAlive, afterCaptureForTest); + if (recovery.status === 'recovered' || recovery.status === 'absent') continue; + if (recovery.status === 'active') return { status: 'claimed', holderKind: recovery.holderKind }; + if (recovery.status === 'invalid') return { status: 'claimed', holderKind: null }; + return recovery; + } + return { status: 'contended' }; +} + +function recoveryClaimMatches( + snapshot: LeaseSnapshot | null, + operationId: string, + targetOperationId: string, +): snapshot is LeaseSnapshot { + return ( + snapshot !== null && + snapshot.record.operationId === operationId && + snapshot.record.recoveryTargetOperationId === targetOperationId + ); +} + +function recoverAbandonedRecoveryClaim( + path: string, + isAlive: (pid: number) => boolean, + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, +): RecoveryClaimRecovery { + const claimPath = `${path}.recovery`; + const first = inspectRecoveryClaim(claimPath); + if (first.status === 'unstable') return { status: 'active', holderKind: first.holderKind }; + if (first.status !== 'valid') return first; + if (!pidIsProvablyDead(first.snapshot.record.pid, isAlive)) { + return { status: 'active', holderKind: first.snapshot.record.kind }; + } + + const second = inspectRecoveryClaim(claimPath); + if (second.status === 'absent') return { status: 'recovered' }; + if (second.status === 'unstable') return { status: 'active', holderKind: second.holderKind }; + if (second.status === 'invalid') return second; + if ( + second.snapshot.record.operationId !== first.snapshot.record.operationId || + second.snapshot.record.pid !== first.snapshot.record.pid || + second.snapshot.record.recoveryTargetOperationId !== first.snapshot.record.recoveryTargetOperationId || + !sameLeaseFingerprint(first.snapshot.fingerprint, second.snapshot.fingerprint) + ) { + return { status: 'active', holderKind: second.snapshot.record.kind }; + } + if (!pidIsProvablyDead(second.snapshot.record.pid, isAlive)) { + return { status: 'active', holderKind: second.snapshot.record.kind }; + } + + const capture = captureExactLeasePath(claimPath, second.snapshot, 'recovery-claim', afterCaptureForTest); + if (capture.status === 'error') { + return { status: 'error', detail: `stale-holder recovery claim cleanup failed: ${capture.detail}` }; + } + if (capture.status === 'contended') { + const current = inspectRecoveryClaim(claimPath); + if (current.status === 'absent') return { status: 'recovered' }; + if (current.status === 'unstable') return { status: 'active', holderKind: current.holderKind }; + if (current.status === 'valid') return { status: 'active', holderKind: current.snapshot.record.kind }; + return current; + } + const captured = readLeaseSnapshot(capture.captured.path); + if ( + captured === null || + captured.record.operationId !== second.snapshot.record.operationId || + captured.record.pid !== second.snapshot.record.pid || + captured.record.recoveryTargetOperationId !== second.snapshot.record.recoveryTargetOperationId || + !sameLeaseFingerprint(second.snapshot.fingerprint, captured.fingerprint) || + !pidIsProvablyDead(captured.record.pid, isAlive) + ) { + restoreOrPreserveCapturedPath(capture.captured, claimPath); + return { status: 'active', holderKind: second.snapshot.record.kind }; + } + try { + discardCapturedPath(capture.captured); + const replacement = inspectRecoveryClaim(claimPath); + if (replacement.status === 'absent') return { status: 'recovered' }; + if (replacement.status === 'unstable') { + return { status: 'active', holderKind: replacement.holderKind }; + } + if (replacement.status === 'valid') { + return { status: 'active', holderKind: replacement.snapshot.record.kind }; + } + return replacement; + } catch (error) { + restoreOrPreserveCapturedPath(capture.captured, claimPath); + return { status: 'error', detail: `stale-holder recovery claim cleanup failed: ${errorText(error)}` }; + } +} + +function inspectRecoveryClaim( + claimPath: string, +): + | { status: 'valid'; snapshot: LeaseSnapshot } + | { status: 'unstable'; holderKind: LifecycleLeaseKind } + | { status: 'absent' } + | { status: 'invalid' } { + const snapshot = readLeaseSnapshot(claimPath); + if (snapshot !== null && snapshot.record.recoveryTargetOperationId !== undefined) { + return { status: 'valid', snapshot }; + } + const holder = readLeaseHolder(claimPath); + if (holder.status === 'valid' && holder.record.recoveryTargetOperationId !== undefined) { + return { status: 'unstable', holderKind: holder.record.kind }; + } + return holder.status === 'invalid' && holder.detail === 'lease absent' ? { status: 'absent' } : { status: 'invalid' }; +} + +function releaseRecoveryClaim(claim: RecoveryClaim): void { + const capture = captureExactLeasePath(claim.path, claim.snapshot, 'recovery-claim'); + if (capture.status !== 'captured') return; + try { + discardCapturedPath(capture.captured); + } catch { + restoreOrPreserveCapturedPath(capture.captured, claim.path); + } } -function supersedeStaleLease(path: string, acquirerOperationId: string): void { - renameNonOverwriting(path, `${path}.stale-${acquirerOperationId}`); +function removeClaimedEvidence(path: string, expected: LeaseSnapshot | null): void { + if (expected === null) return; + const capture = captureExactLeasePath(path, expected, 'stale-supersede'); + if (capture.status !== 'captured') return; + try { + discardCapturedPath(capture.captured); + } catch { + restoreOrPreserveCapturedPath(capture.captured, path); + } } function pidIsProvablyDead(pid: number, isAlive: (pid: number) => boolean): boolean { if (!Number.isInteger(pid) || pid <= 0) return false; // indeterminate → treat as live (busy) - return !isAlive(pid); + try { + return !isAlive(pid); + } catch { + return false; + } +} + +interface LeaseFingerprint { + dev: number; + ino: number; + mode: number; + size: number; + mtimeMs: number; + nlink: number; +} + +interface LeaseSnapshot { + record: LeaseRecord; + fingerprint: LeaseFingerprint; +} + +interface CapturedLeasePath { + directory: string; + path: string; +} + +/** + * Best-effort bounded cleanup for private records left by a process killed + * after fsync and before stable publication. Only an exact filename/record + * operation binding with a stable physical identity, one link, and a provably + * dead PID is removable. Every other state is preserved fail-closed. + */ +function recoverDeadStagingRecords( + path: string, + isAlive: (pid: number) => boolean, + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, +): void { + let recovered = 0; + for (let slotIndex = 0; slotIndex < STAGING_SLOT_COUNT && recovered < MAX_STAGING_RECOVERIES; slotIndex += 1) { + const slot = slotIndex.toString(16).padStart(2, '0'); + if (recoverOneDeadStagingRecord(`${path}.staging-${slot}`, slot, isAlive, afterCaptureForTest)) { + recovered += 1; + } + } +} + +function recoverOneDeadStagingRecord( + stagingPath: string, + expectedSlot: string, + isAlive: (pid: number) => boolean, + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, +): boolean { + const first = readLeaseSnapshot(stagingPath, true); + if (first === null || first.record.stagingSlot !== expectedSlot) return false; + if (!pidIsProvablyDead(first.record.pid, isAlive)) return false; + const second = readLeaseSnapshot(stagingPath, true); + if ( + second === null || + second.record.operationId !== first.record.operationId || + second.record.stagingSlot !== expectedSlot || + second.record.pid !== first.record.pid || + !sameLeaseFingerprint(first.fingerprint, second.fingerprint) + ) { + return false; + } + const capture = captureExactLeasePath(stagingPath, second, 'staging-recovery', afterCaptureForTest); + if (capture.status !== 'captured') return false; + const capturedState = readLeaseSnapshot(capture.captured.path, true); + if ( + capturedState === null || + capturedState.record.operationId !== second.record.operationId || + capturedState.record.stagingSlot !== expectedSlot || + capturedState.record.pid !== second.record.pid || + !sameLeaseFingerprint(second.fingerprint, capturedState.fingerprint) || + !pidIsProvablyDead(capturedState.record.pid, isAlive) + ) { + restoreOrPreserveCapturedPath(capture.captured, stagingPath); + return false; + } + try { + discardCapturedPath(capture.captured); + return true; + } catch { + restoreOrPreserveCapturedPath(capture.captured, stagingPath); + return false; + } +} + +function readLeaseSnapshot(path: string, requireSingleLink = false): LeaseSnapshot | null { + const before = leaseFingerprint(path); + if (before === null || (requireSingleLink && before.nlink !== 1)) return null; + const holder = readLeaseHolder(path); + if (holder.status !== 'valid') return null; + const after = leaseFingerprint(path); + if (after === null || !sameLeaseFingerprint(before, after)) return null; + return { record: holder.record, fingerprint: after }; +} + +function leaseFingerprint(path: string): LeaseFingerprint | null { + let stat: Stats; + try { + stat = lstatSync(path); + } catch { + return null; + } + if (!stat.isFile() || stat.isSymbolicLink()) return null; + return { + dev: stat.dev, + ino: stat.ino, + mode: stat.mode, + size: stat.size, + mtimeMs: stat.mtimeMs, + nlink: stat.nlink, + }; +} + +function sameLeaseFingerprint(a: LeaseFingerprint, b: LeaseFingerprint): boolean { + return ( + a.dev === b.dev && + a.ino === b.ino && + a.mode === b.mode && + a.size === b.size && + a.mtimeMs === b.mtimeMs && + a.nlink === b.nlink + ); +} + +function sameLeaseGeneration(a: LeaseFingerprint, b: LeaseFingerprint): boolean { + return a.dev === b.dev && a.ino === b.ino && a.mode === b.mode && a.size === b.size && a.mtimeMs === b.mtimeMs; +} + +type CaptureAttempt = + | { status: 'captured'; captured: CapturedLeasePath } + | { status: 'contended' } + | { status: 'error'; detail: string }; + +function captureExactLeasePath( + path: string, + expected: LeaseSnapshot, + operation: LifecycleLeaseCaptureEvent['operation'], + afterCaptureForTest?: (event: LifecycleLeaseCaptureEvent) => void, +): CaptureAttempt { + const attempt = captureLeasePath(path, operation); + if (attempt.status !== 'captured') return attempt; + const captured = attempt.captured; + try { + afterCaptureForTest?.({ operation, path, capturedPath: captured.path }); + } catch (error) { + restoreOrPreserveCapturedPath(captured, path); + throw error; + } + const state = readLeaseSnapshot(captured.path, operation === 'staging-recovery'); + if ( + state === null || + state.record.operationId !== expected.record.operationId || + !sameLeaseFingerprint(expected.fingerprint, state.fingerprint) + ) { + restoreOrPreserveCapturedPath(captured, path); + return { status: 'contended' }; + } + return { status: 'captured', captured }; +} + +function captureLeasePath(path: string, label: string): CaptureAttempt { + let directory: string; + try { + directory = mkdtempSync(join(dirOf(path), `.${baseOf(path)}.${label}-`)); + } catch (error) { + return { status: 'error', detail: `lease ${label} capture setup failed: ${errorText(error)}` }; + } + const capturedPath = join(directory, 'record'); + try { + renameSync(path, capturedPath); + return { status: 'captured', captured: { directory, path: capturedPath } }; + } catch (error) { + removeCaptureDirectory(directory); + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return { status: 'contended' }; + return { status: 'error', detail: `lease ${label} capture failed: ${errorText(error)}` }; + } +} + +function restoreOrPreserveCapturedPath(captured: CapturedLeasePath, originalPath: string): void { + try { + const stat = lstatSync(captured.path); + if (stat.isFile() && !stat.isSymbolicLink()) { + linkSync(captured.path, originalPath); + unlinkSync(captured.path); + fsyncParentDir(originalPath); + removeCaptureDirectory(captured.directory); + return; + } + if (stat.isSymbolicLink()) { + symlinkSync(readlinkSync(captured.path), originalPath); + unlinkSync(captured.path); + fsyncParentDir(originalPath); + removeCaptureDirectory(captured.directory); + } + } catch { + // A replacement already won the original pathname, or the captured object + // is foreign. Preserve the capture directory byte-for-byte for inspection. + } +} + +function discardCapturedPath(captured: CapturedLeasePath): void { + unlinkWithParentFsync(captured.path); + removeCaptureDirectory(captured.directory); +} + +function removeCaptureDirectory(directory: string): void { + try { + rmdirSync(directory); + fsyncParentDir(directory); + } catch { + // A non-empty or replaced private directory is preserved fail-closed. + } } function defaultIsProcessAlive(pid: number): boolean { @@ -267,6 +985,14 @@ function mintOperationId(): string { return randomBytes(16).toString('hex'); } +function stagingSlotsFor(operationId: string): string[] { + const start = Number.parseInt(operationId.slice(0, 2), 16); + const step = Number.parseInt(operationId.slice(2, 4), 16) | 1; + return Array.from({ length: STAGING_SLOT_COUNT }, (_, offset) => + ((start + offset * step) % STAGING_SLOT_COUNT).toString(16).padStart(2, '0'), + ); +} + function busy(holderKind: LifecycleLeaseKind | null, detail: string): LifecycleLeaseBusy { return { ok: false, reason: 'codex-lifecycle-busy', holderKind, detail }; } @@ -276,6 +1002,63 @@ function dirOf(path: string): string { return idx <= 0 ? '.' : path.slice(0, idx); } +function baseOf(path: string): string { + const idx = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); + return idx < 0 ? path : path.slice(idx + 1); +} + function errorText(error: unknown): string { return error instanceof Error ? error.message : String(error); } + +function cleanupStagingDetail(stagingPath: string): string { + try { + unlinkWithParentFsync(stagingPath); + return ''; + } catch (error) { + return `; staging cleanup failed: ${errorText(error)}`; + } +} + +function cleanupStagedGenerationDetail(staged: StagedLeaseRecord): string { + const current = readLeaseSnapshot(staged.path, true); + if ( + current === null || + current.record.operationId !== staged.snapshot.record.operationId || + current.record.pid !== staged.snapshot.record.pid || + current.record.stagingSlot !== staged.slot || + !sameLeaseGeneration(current.fingerprint, staged.snapshot.fingerprint) + ) { + return fsyncStagingParentDetail(staged.path); + } + const capture = captureExactLeasePath(staged.path, current, 'staging-recovery'); + if (capture.status === 'error') return `; staging cleanup failed: ${capture.detail}`; + if (capture.status === 'contended') return fsyncStagingParentDetail(staged.path); + try { + discardCapturedPath(capture.captured); + return ''; + } catch (error) { + restoreOrPreserveCapturedPath(capture.captured, staged.path); + return `; staging cleanup failed: ${errorText(error)}`; + } +} + +function fsyncStagingParentDetail(stagingPath: string): string { + try { + fsyncParentDir(stagingPath); + return ''; + } catch (error) { + return `; staging parent sync failed: ${errorText(error)}`; + } +} + +function cleanupStagedGenerationOrThrow( + staged: StagedLeaseRecord, + primaryError: unknown, + aggregateMessage: string, +): void { + const cleanupDetail = cleanupStagedGenerationDetail(staged); + if (cleanupDetail) { + throw new AggregateError([primaryError, new Error(cleanupDetail.slice(2))], aggregateMessage); + } +} diff --git a/src/lib/codex-lifecycle-truth.test.ts b/src/lib/codex-lifecycle-truth.test.ts new file mode 100644 index 000000000..07c07c9e6 --- /dev/null +++ b/src/lib/codex-lifecycle-truth.test.ts @@ -0,0 +1,375 @@ +import { describe, expect, test } from 'bun:test'; +import { join, resolve } from 'node:path'; +import type { CodexActivationSnapshot } from './codex-activation.js'; +import { parseReleaseVersion } from './codex-activation.js'; +import { + DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + DELIVERY_EVIDENCE_REPOSITORY, + deriveDeliveryId, +} from './codex-delivery-evidence.js'; +import { DELIVERY_INCOMPLETE_RECOVERY } from './codex-host-observation.js'; +import { + type RouteLayerInput, + assessSnapshotDelivery, + classifyRouteLayers, + projectTrustState, + snapshotDeliveryReadState, +} from './codex-lifecycle-truth.js'; + +const T = '5.260722.1'; +const DIGEST = 'a'.repeat(64); +const PLATFORM = 'darwin-arm64'; +const BINARY_DIGEST = 'd'.repeat(64); +const DELIVERY_ROOT = '/home/test/.genie'; +const EVIDENCE_DIGEST = 'e'.repeat(64); + +function ver(s: string) { + const parsed = parseReleaseVersion(s); + if (!parsed) throw new Error(`bad test version ${s}`); + return parsed; +} + +function snapshot(over: Partial = {}): CodexActivationSnapshot { + return { + canonical: { + status: 'ok', + version: ver(T), + digest: DIGEST, + identity: '10:100', + platformTriple: PLATFORM, + installedBinarySha256: BINARY_DIGEST, + deliveryRoot: DELIVERY_ROOT, + }, + query: { status: 'ok', registration: { present: true, enabled: true, version: ver(T) } }, + cache: { kind: 'present', digest: DIGEST, identity: '10:200' }, + receipt: { status: 'absent' }, + delivery: { status: 'absent' }, + intent: { status: 'absent' }, + receiptConsumed: false, + observationWitness: { + before: { status: 'present', digest: 'f'.repeat(64), identity: '10:300' }, + after: { status: 'present', digest: 'f'.repeat(64), identity: '10:300' }, + }, + observedAt: '2026-07-22T00:00:00.000Z', + ...over, + }; +} + +function matchingDelivery(over: Partial> = {}): CodexActivationSnapshot['delivery'] { + return { + status: 'present', + record: { + schemaVersion: 2, + deliveryId: deriveDeliveryId(EVIDENCE_DIGEST, DELIVERY_ROOT), + targetVersion: T, + canonicalPayloadSha256: DIGEST, + channel: 'stable', + deliveredAt: '2026-07-22T00:00:00.000Z', + evidenceDigest: EVIDENCE_DIGEST, + platformId: 'darwin-arm64', + platformTriple: PLATFORM, + releaseTag: `v${T}`, + releaseName: `genie-${T}-darwin-arm64.tar.gz`, + releaseManifestSha256: 'b'.repeat(64), + artifactSha256: 'c'.repeat(64), + installedBinarySha256: BINARY_DIGEST, + deliveryRoot: DELIVERY_ROOT, + ...over, + }, + evidence: { + evidenceDigest: EVIDENCE_DIGEST, + deliveredAt: '2026-07-22T00:00:00.000Z', + descriptor: { + schemaVersion: 1, + repository: DELIVERY_EVIDENCE_REPOSITORY, + version: T, + channel: 'stable', + platformId: 'darwin-arm64', + platformTriple: PLATFORM, + releaseTag: `v${T}`, + releaseName: `genie-${T}-darwin-arm64.tar.gz`, + releaseManifestSha256: 'b'.repeat(64), + artifactSha256: 'c'.repeat(64), + installedBinarySha256: BINARY_DIGEST, + canonicalPayloadSha256: DIGEST, + sourceSha: '1'.repeat(40), + sourceBranch: 'main', + sourceCiRunId: '123', + controlSha: '2'.repeat(40), + digestAlgorithm: DELIVERY_EVIDENCE_DIGEST_ALGORITHM, + }, + }, + }; +} + +describe('assessSnapshotDelivery (Decision 9 shared gate)', () => { + test('matching record binding the canonical target passes', () => { + expect(assessSnapshotDelivery(snapshot({ delivery: matchingDelivery() }))).toEqual({ kind: 'matching' }); + }); + + test('absent record is the one consistent delivery-incomplete result with the recovery command', () => { + const gate = assessSnapshotDelivery(snapshot()); + if (gate.kind !== 'incomplete') throw new Error(`expected incomplete, got ${gate.kind}`); + expect(gate.result).toMatchObject({ + code: 'delivery-incomplete', + authority: 'none', + exit: 1, + deliveryComplete: false, + assessment: 'absent', + recovery: DELIVERY_INCOMPLETE_RECOVERY, + }); + }); + + test('invalid record classifies invalid, never mismatch', () => { + const gate = assessSnapshotDelivery(snapshot({ delivery: { status: 'invalid', detail: 'corrupt json' } })); + if (gate.kind !== 'incomplete') throw new Error(`expected incomplete, got ${gate.kind}`); + expect(gate.result.assessment).toBe('invalid'); + }); + + test('record bound to a different target version is a mismatch', () => { + const gate = assessSnapshotDelivery( + snapshot({ + delivery: matchingDelivery({ + targetVersion: '5.260711.9', + releaseTag: 'v5.260711.9', + releaseName: 'genie-5.260711.9-darwin-arm64.tar.gz', + }), + }), + ); + if (gate.kind !== 'incomplete') throw new Error(`expected incomplete, got ${gate.kind}`); + expect(gate.result.assessment).toBe('mismatch'); + }); + + test('an internally matching record/evidence tuple for a different version still mismatches canonical reality', () => { + const delivery = matchingDelivery(); + if (delivery.status !== 'present') throw new Error('unreachable'); + const older = '5.260711.9'; + delivery.record.targetVersion = older; + delivery.record.releaseTag = `v${older}`; + delivery.record.releaseName = `genie-${older}-darwin-arm64.tar.gz`; + delivery.evidence = { + ...delivery.evidence, + descriptor: { + ...delivery.evidence.descriptor, + version: older, + releaseTag: `v${older}`, + releaseName: `genie-${older}-darwin-arm64.tar.gz`, + }, + }; + const gate = assessSnapshotDelivery(snapshot({ delivery })); + if (gate.kind !== 'incomplete') throw new Error(`expected incomplete, got ${gate.kind}`); + expect(gate.result.assessment).toBe('mismatch'); + }); + + test('record bound to a different payload digest is a mismatch', () => { + const gate = assessSnapshotDelivery( + snapshot({ delivery: matchingDelivery({ canonicalPayloadSha256: 'b'.repeat(64) }) }), + ); + if (gate.kind !== 'incomplete') throw new Error(`expected incomplete, got ${gate.kind}`); + expect(gate.result.assessment).toBe('mismatch'); + }); + + test('missing canonical payload is unassessable — the earlier payload guard owns that state', () => { + const gate = assessSnapshotDelivery( + snapshot({ canonical: { status: 'error', detail: 'payload root not found' }, delivery: matchingDelivery() }), + ); + expect(gate).toEqual({ kind: 'unassessable', detail: 'canonical payload is unavailable: payload root not found' }); + }); + + test('snapshotDeliveryReadState maps all three fact arms', () => { + expect(snapshotDeliveryReadState(snapshot()).status).toBe('absent'); + expect(snapshotDeliveryReadState(snapshot({ delivery: { status: 'invalid', detail: 'x' } }))).toEqual({ + status: 'invalid', + detail: 'x', + }); + expect(snapshotDeliveryReadState(snapshot({ delivery: matchingDelivery() })).status).toBe('present'); + }); +}); + +describe('classifyRouteLayers (typed config-layer diagnostics)', () => { + const root = resolve('/repo'); + function input(over: Partial = {}): RouteLayerInput { + return { + worktreeRoot: root, + cwd: root, + route: { route: 'fallback', detail: 'project fallback' }, + globalConfigPath: '/codex-home/config.toml', + readFile: () => null, + exists: () => false, + ...over, + }; + } + const trustedGlobal = `[projects."${root}"]\ntrust_level = "trusted"\n`; + + test('an intact trusted route yields zero findings', () => { + const findings = classifyRouteLayers( + input({ readFile: (path) => (path === '/codex-home/config.toml' ? trustedGlobal : null) }), + ); + expect(findings).toEqual([]); + }); + + test('plugin+project conflict is a route-collision', () => { + const findings = classifyRouteLayers( + input({ + route: { route: 'conflict', detail: 'both routes effective' }, + readFile: (path) => (path === '/codex-home/config.toml' ? trustedGlobal : null), + }), + ); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'route-collision' }); + expect(findings[0]?.detail).toContain('both routes effective'); + }); + + test('a user-owned same-key project route is a route-collision that names user resolution', () => { + const findings = classifyRouteLayers( + input({ + route: { route: 'unmanaged-fallback', detail: 'unmanaged [mcp_servers.genie]' }, + readFile: (path) => (path === '/codex-home/config.toml' ? trustedGlobal : null), + }), + ); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'route-collision' }); + expect(findings[0]?.detail).toContain('user-owned'); + expect(findings[0]?.detail).toContain('user resolution'); + }); + + test('a nested .codex/config.toml nearer to the CWD shadows the root marker (nearest owner reported)', () => { + const nested = join(root, 'packages', 'web'); + const mid = join(root, 'packages'); + const shadowConfig = '[mcp_servers.genie]\ncommand = "other"\n'; + const files: Record = { + [join(nested, '.codex', 'config.toml')]: shadowConfig, + [join(mid, '.codex', 'config.toml')]: shadowConfig, + '/codex-home/config.toml': trustedGlobal, + }; + const findings = classifyRouteLayers( + input({ + cwd: nested, + readFile: (path) => files[path] ?? null, + exists: (path) => path in files, + }), + ); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'route-shadowed', ownerPath: nested }); + }); + + test('a nested config without a genie route does not shadow', () => { + const nested = join(root, 'packages'); + const files: Record = { + [join(nested, '.codex', 'config.toml')]: '[mcp_servers.other]\ncommand = "x"\n', + '/codex-home/config.toml': trustedGlobal, + }; + const findings = classifyRouteLayers( + input({ cwd: nested, readFile: (path) => files[path] ?? null, exists: (path) => path in files }), + ); + expect(findings).toEqual([]); + }); + + test('a CWD outside the worktree root never walks the chain', () => { + const findings = classifyRouteLayers( + input({ + cwd: '/elsewhere', + readFile: (path) => (path === '/codex-home/config.toml' ? trustedGlobal : null), + exists: () => { + throw new Error('chain walk must not run for an outside CWD'); + }, + }), + ); + expect(findings).toEqual([]); + }); + + test('the dotted-key route spelling (the marker form) is detected in nested and global layers', () => { + const nested = join(root, 'packages'); + const dottedConfig = 'mcp_servers.genie.command = "/elsewhere/genie"\nmcp_servers.genie.args = ["mcp"]\n'; + const files: Record = { + [join(nested, '.codex', 'config.toml')]: dottedConfig, + '/codex-home/config.toml': `${dottedConfig}${trustedGlobal}`, + }; + const findings = classifyRouteLayers( + input({ cwd: nested, readFile: (path) => files[path] ?? null, exists: (path) => path in files }), + ); + expect(findings.map((finding) => finding.kind).sort()).toEqual(['global-route-same-key', 'route-shadowed']); + }); + + test('a dotted-key inline-table route (mcp_servers.genie = {…}) is detected', () => { + const findings = classifyRouteLayers( + input({ + readFile: (path) => + path === '/codex-home/config.toml' + ? `mcp_servers.genie = { command = "/old/genie", args = [] }\n${trustedGlobal}` + : null, + }), + ); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'global-route-same-key' }); + }); + + test('a non-genie dotted key or unrelated assignment does not false-positive', () => { + const findings = classifyRouteLayers( + input({ + readFile: (path) => + path === '/codex-home/config.toml' + ? `mcp_servers.other.command = "/x"\nsome_genie_note = "mcp_servers.genie"\n${trustedGlobal}` + : null, + }), + ); + expect(findings).toEqual([]); + }); + + test('a global same-key route is reported and preserved', () => { + const findings = classifyRouteLayers( + input({ + readFile: (path) => + path === '/codex-home/config.toml' ? `[mcp_servers.genie]\ncommand = "old"\n${trustedGlobal}` : null, + }), + ); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'global-route-same-key', path: '/codex-home/config.toml' }); + }); + + test('an explicit non-trusted trust_level is untrusted-config', () => { + const findings = classifyRouteLayers( + input({ + readFile: (path) => + path === '/codex-home/config.toml' ? `[projects."${root}"]\ntrust_level = "untrusted"\n` : null, + }), + ); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'untrusted-config' }); + expect(findings[0]?.detail).toContain("'untrusted'"); + }); + + test('no trust entry at all is project-trust-required', () => { + const findings = classifyRouteLayers(input()); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'project-trust-required' }); + }); + + test('trust entry for a DIFFERENT project does not trust this one', () => { + const findings = classifyRouteLayers( + input({ + readFile: (path) => + path === '/codex-home/config.toml' ? `[projects."/other/repo"]\ntrust_level = "trusted"\n` : null, + }), + ); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ kind: 'project-trust-required' }); + }); +}); + +describe('projectTrustState (bounded targeted parse)', () => { + const root = resolve('/repo'); + + test('trusted entry is trusted', () => { + expect(projectTrustState(`[projects."${root}"]\ntrust_level = "trusted"\n`, root)).toEqual({ state: 'trusted' }); + }); + + test('trust_level read stops at the next section header', () => { + const config = `[projects."${root}"]\nother = 1\n[projects."/other"]\ntrust_level = "trusted"\n`; + expect(projectTrustState(config, root)).toEqual({ state: 'unknown' }); + }); + + test('null config (absent/unreadable/oversized) is unknown', () => { + expect(projectTrustState(null, root)).toEqual({ state: 'unknown' }); + }); +}); diff --git a/src/lib/codex-lifecycle-truth.ts b/src/lib/codex-lifecycle-truth.ts new file mode 100644 index 000000000..8c73ac75d --- /dev/null +++ b/src/lib/codex-lifecycle-truth.ts @@ -0,0 +1,279 @@ +/** + * Group E — lifecycle-truth integration: the pure policy seams that make + * `genie setup --codex` and `genie doctor` derive their Codex claims from the + * SAME observed facts instead of re-deciding them per surface. + * + * Two concerns live here, both pure (all IO is injected): + * + * 1. The shared delivery gate (Decision 9): one assessment of the observed + * snapshot's authenticated delivery record against the installed canonical + * target. Setup applies it BEFORE its first prompt or activation-owned + * mutation; doctor applies it so a missing/invalid/mismatched record can + * never coexist with a green `current` claim. The executor's + * `beginActivation` inner guard remains the authoritative defense in depth + * immediately before the first journal write — this gate never replaces it. + * + * 2. The route-layer classifier: distinct typed findings for the config-layer + * states doctor/init must report WITHOUT editing user-owned config — a + * plugin/project route collision, a user-owned same-key project route, a + * nested `.codex/config.toml` shadowing the root marker block (the + * effective-layer shadowing case), a global same-key route, and the Codex + * project-trust states. Findings are reports, never mutations: a collision + * or shadowing layer is preserved exactly as found (Decision 8). + */ + +import { existsSync, readFileSync, realpathSync } from 'node:fs'; +import { dirname, join, relative, resolve, sep } from 'node:path'; +import type { CodexActivationSnapshot } from './codex-activation.js'; +import { authenticatedDeliveryRecordFields, createAuthenticatedDeliveryBinding } from './codex-delivery-evidence.js'; +import { + type DeliveryIncompleteResult, + type DeliveryRecordReadState, + assessAuthenticatedDelivery, + buildDeliveryIncompleteResult, +} from './codex-host-observation.js'; +import type { CodexProjectMcpResult } from './codex-project-mcp.js'; + +// ============================================================================ +// 1. Shared delivery gate (Decision 9) +// ============================================================================ + +export type SnapshotDeliveryGate = + /** A present, structurally valid record binding the installed canonical target. */ + | { kind: 'matching' } + /** The canonical payload itself is unavailable; the caller's earlier payload guard owns this state. */ + | { kind: 'unassessable'; detail: string } + /** Missing/invalid/mismatched record: the one consistent `delivery-incomplete` result. */ + | { kind: 'incomplete'; result: DeliveryIncompleteResult }; + +/** + * Assess the snapshot's complete authenticated delivery tuple against the + * installed canonical target. Locally observable bindings come from the + * canonical payload; release-provenance bindings come from the one structurally + * valid record that a subsequent activation permit fingerprints in full. + */ +export function assessSnapshotDelivery(snapshot: CodexActivationSnapshot): SnapshotDeliveryGate { + if (snapshot.canonical.status !== 'ok') { + return { kind: 'unassessable', detail: `canonical payload is unavailable: ${snapshot.canonical.detail}` }; + } + const readState = snapshotDeliveryReadState(snapshot); + if (readState.status !== 'present') { + return { kind: 'incomplete', result: buildDeliveryIncompleteResult(readState.status) }; + } + if (snapshot.delivery.status !== 'present') { + return { kind: 'incomplete', result: buildDeliveryIncompleteResult('invalid') }; + } + const evidence = snapshot.delivery.evidence; + const descriptor = evidence.descriptor; + if ( + descriptor.version !== snapshot.canonical.version.canonical || + descriptor.canonicalPayloadSha256 !== snapshot.canonical.digest || + descriptor.installedBinarySha256 !== snapshot.canonical.installedBinarySha256 || + descriptor.platformTriple !== snapshot.canonical.platformTriple + ) { + return { kind: 'incomplete', result: buildDeliveryIncompleteResult('mismatch') }; + } + const expectation = authenticatedDeliveryRecordFields( + createAuthenticatedDeliveryBinding(evidence, snapshot.canonical.deliveryRoot), + ); + const assessment = assessAuthenticatedDelivery(readState, expectation); + if (assessment === 'matching') return { kind: 'matching' }; + return { kind: 'incomplete', result: buildDeliveryIncompleteResult(assessment) }; +} + +/** Map the snapshot's delivery fact onto the pure assessment's read-state shape. */ +export function snapshotDeliveryReadState(snapshot: CodexActivationSnapshot): DeliveryRecordReadState { + const fact = snapshot.delivery; + if (fact.status === 'present') return { status: 'present', record: fact.record }; + if (fact.status === 'invalid') return { status: 'invalid', detail: fact.detail }; + return { status: 'absent' }; +} + +// ============================================================================ +// 2. Route-layer classifier (typed config-layer diagnostics) +// ============================================================================ + +export type RouteLayerFinding = + /** Two effective same-key routes (plugin+project) or a user-owned same-key project route. */ + | { kind: 'route-collision'; detail: string } + /** A nested `.codex/config.toml` nearer to the CWD defines `mcp_servers.genie`, shadowing the root marker. */ + | { kind: 'route-shadowed'; ownerPath: string; detail: string } + /** The global `~/.codex/config.toml` defines a same-key `mcp_servers.genie` route. */ + | { kind: 'global-route-same-key'; path: string; detail: string } + /** The project has an explicit non-trusted `trust_level` in the global Codex config. */ + | { kind: 'untrusted-config'; detail: string } + /** No Codex trust entry exists for the project; trust is required before route health can be claimed. */ + | { kind: 'project-trust-required'; detail: string }; + +export interface RouteLayerInput { + /** The worktree root owning the marker-managed project route. */ + worktreeRoot: string; + /** The effective CWD whose root-to-CWD config chain is inspected for shadowing. */ + cwd: string; + /** The read-only route inspection result (`inspectCodexProjectMcp`). */ + route: Pick; + /** The global Codex config path (`getCodexConfigPath()`). */ + globalConfigPath: string; + /** Bounded file reader seam; returns null when absent/unreadable. */ + readFile?: (path: string) => string | null; + /** Existence seam for chain-walk directories. */ + exists?: (path: string) => boolean; +} + +const MAX_CONFIG_BYTES = 512 * 1024; +// A same-key genie route in either TOML spelling: the `[mcp_servers.genie]` +// section header, or the dotted-key form (`mcp_servers.genie.command = …` / +// `mcp_servers.genie = {…}`) that the marker block itself uses. +const GENIE_SERVER_HEADER_RE = + /^\s*(?:\[mcp_servers\.(?:genie|"genie")\]\s*(?:#.*)?$|mcp_servers\.(?:genie|"genie")\s*(?:\.[A-Za-z0-9_.-]+\s*)?=)/m; + +function defaultReadFile(path: string): string | null { + try { + const content = readFileSync(path, 'utf8'); + return content.length > MAX_CONFIG_BYTES ? null : content; + } catch { + return null; + } +} + +/** + * Classify the config-layer states around the project route. Pure over the + * injected readers; reports findings without touching any layer. Collisions and + * shadowing are hard route defects; the trust states are advisories that block + * a health CLAIM (doctor must say trust is required) without failing the route + * bytes themselves. + */ +export function classifyRouteLayers(input: RouteLayerInput): RouteLayerFinding[] { + const readFile = input.readFile ?? defaultReadFile; + const exists = input.exists ?? existsSync; + const findings: RouteLayerFinding[] = []; + + if (input.route.route === 'conflict') { + findings.push({ + kind: 'route-collision', + detail: `plugin and project routes are both effective: ${input.route.detail ?? 'route conflict'}`, + }); + } else if (input.route.route === 'unmanaged-fallback') { + findings.push({ + kind: 'route-collision', + detail: `user-owned [mcp_servers.genie] project route is preserved and requires user resolution: ${input.route.detail ?? 'unmanaged route'}`, + }); + } + + const shadowOwner = nearestShadowingConfig(input.worktreeRoot, input.cwd, readFile, exists); + if (shadowOwner !== null) { + findings.push({ + kind: 'route-shadowed', + ownerPath: shadowOwner, + detail: `nested ${join(shadowOwner, '.codex', 'config.toml')} defines [mcp_servers.genie] nearer to the CWD and shadows the root marker route`, + }); + } + + const globalConfig = readFile(input.globalConfigPath); + if (globalConfig !== null && GENIE_SERVER_HEADER_RE.test(globalConfig)) { + findings.push({ + kind: 'global-route-same-key', + path: input.globalConfigPath, + detail: + 'global Codex config defines a same-key [mcp_servers.genie] route; it is preserved and requires user resolution', + }); + } + + const trust = projectTrustState(globalConfig, input.worktreeRoot); + if (trust.state === 'untrusted') { + findings.push({ + kind: 'untrusted-config', + detail: `Codex marks this project '${trust.level}'; its project config (including the Genie route) is inert until the user trusts it`, + }); + } else if (trust.state === 'unknown') { + findings.push({ + kind: 'project-trust-required', + detail: + 'Codex has no trust entry for this project; approve it in Codex before the project route can be claimed healthy', + }); + } + + return findings; +} + +/** + * Walk the root-to-CWD directory chain (nearest to CWD first, root excluded) + * and return the first directory whose `.codex/config.toml` defines a same-key + * genie route. Returns null when the CWD is not under the root or no layer + * shadows the marker. + */ +function nearestShadowingConfig( + worktreeRoot: string, + cwd: string, + readFile: (path: string) => string | null, + exists: (path: string) => boolean, +): string | null { + // Physical identity: a logical root (e.g. macOS /var/...) and a physical CWD + // (/private/var/...) describe the same chain; without realpath the walk + // would silently skip and under-report shadowing. + const root = safeRealpath(worktreeRoot); + let current = safeRealpath(cwd); + const rel = relative(root, current); + if (rel.startsWith('..') || rel.split(sep).includes('..')) return null; + while (current !== root) { + const configPath = join(current, '.codex', 'config.toml'); + if (exists(configPath)) { + const content = readFile(configPath); + if (content !== null && GENIE_SERVER_HEADER_RE.test(content)) return current; + } + const parent = dirname(current); + if (parent === current) break; + current = parent; + } + return null; +} + +type ProjectTrustState = { state: 'trusted' } | { state: 'untrusted'; level: string } | { state: 'unknown' }; + +/** + * Bounded, targeted read of the global Codex config's `[projects.""]` + * trust entry. TOML-shaped but deliberately not a full TOML parser: it matches + * the exact section header Codex writes for the absolute project path and reads + * `trust_level` within that section only. An unreadable/absent config or a + * missing section is `unknown` (trust cannot be inferred; Decision: never claim + * health for a project Codex has not trusted). + */ +export function projectTrustState(globalConfig: string | null, worktreeRoot: string): ProjectTrustState { + if (globalConfig === null) return { state: 'unknown' }; + // Codex records the project path as it saw it; tolerate both the logical and + // the physical spelling of the same directory (macOS /var vs /private/var). + const candidates = [...new Set([resolve(worktreeRoot), safeRealpath(worktreeRoot)])]; + for (const candidate of candidates) { + const found = trustEntryFor(globalConfig, candidate); + if (found !== null) return found; + } + return { state: 'unknown' }; +} + +function trustEntryFor(globalConfig: string, projectPath: string): ProjectTrustState | null { + const escapedPath = projectPath.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); + const headerExact = `[projects."${escapedPath}"]`; + let inSection = false; + for (const rawLine of globalConfig.split('\n')) { + const line = rawLine.trim(); + if (line.startsWith('[')) { + inSection = line === headerExact; + continue; + } + if (!inSection) continue; + const match = line.match(/^trust_level\s*=\s*"([^"]*)"/); + if (match !== null) { + return match[1] === 'trusted' ? { state: 'trusted' } : { state: 'untrusted', level: match[1] ?? '' }; + } + } + return null; +} + +/** Physical path identity with a logical fallback for not-yet-existing paths. */ +function safeRealpath(path: string): string { + try { + return realpathSync(path); + } catch { + return resolve(path); + } +} diff --git a/src/lib/codex-mcp-health-session.test.ts b/src/lib/codex-mcp-health-session.test.ts index 5b02dff03..4026b125e 100644 --- a/src/lib/codex-mcp-health-session.test.ts +++ b/src/lib/codex-mcp-health-session.test.ts @@ -68,6 +68,27 @@ describe('runBoundedCodexMcpSession', () => { expect(result.detail).toContain('read-only genie_wish_status did not return a non-error result'); }); + test('accepts a fail-closed typed "no project context" wish_status (throwaway non-project cwd)', () => { + // The probe cwd is not a project, so the fail-closed server returns a typed + // error rather than an empty board — that IS the read-only health signal. + for (const kind of ['project-context-unavailable', 'project-database-unavailable', 'unsupported-project-layout']) { + const stdout = `${[ + { jsonrpc: '2.0', id: 1, result: { protocolVersion: '2024-11-05', serverInfo: { name: 'genie' } } }, + { jsonrpc: '2.0', id: 2, result: { tools: REQUIRED_GENIE_MCP_TOOLS.map((name) => ({ name })) } }, + { + jsonrpc: '2.0', + id: 3, + result: { isError: true, structuredContent: { error: kind, detail: kind } }, + }, + ] + .map((reply) => JSON.stringify(reply)) + .join('\n')}\n`; + const result = runBoundedCodexMcpSession({ ...base, spawn: fakeSpawn({ stdout }) }); + expect(result.ok).toBe(true); + expect(result.wishStatusReadOnly).toBe(true); + } + }); + test('rejects a session whose tools/list reply never arrived', () => { const stdout = `${JSON.stringify({ jsonrpc: '2.0', id: 1, result: { protocolVersion: '2024-11-05' } })}\n`; const result = runBoundedCodexMcpSession({ ...base, spawn: fakeSpawn({ stdout }) }); diff --git a/src/lib/codex-mcp-health-session.ts b/src/lib/codex-mcp-health-session.ts index 5407b3d49..2c32670f1 100644 --- a/src/lib/codex-mcp-health-session.ts +++ b/src/lib/codex-mcp-health-session.ts @@ -11,15 +11,31 @@ * bounds the whole session. * * The server opens `.genie/genie.db` READ-ONLY and only writes to stdout, so the - * probe never mutates disk. Callers still hand it an isolated throwaway `cwd` so - * a missing db degrades to an empty board (never creating -wal/-shm siblings in - * real home/state trees). runtime-integrations proves the zero-mutation - * guarantee with a digest-around-session test. + * probe never mutates disk. Callers still hand it an isolated throwaway `cwd` + * that is not a project: the fail-closed server returns a typed "no project + * context" wish_status there rather than fabricating an empty board (never + * creating -wal/-shm siblings in real home/state trees), and this probe treats + * that typed refusal as read-only-healthy. runtime-integrations proves the + * zero-mutation guarantee with a digest-around-session test. */ export const MCP_HEALTH_PROTOCOL_VERSION = '2024-11-05'; export const DEFAULT_HEALTH_SESSION_TIMEOUT_MS = 10_000; +/** + * The fail-closed project-context error kinds the read-only `genie mcp` server + * returns (structuredContent `{ error, detail }`, `isError: true`) instead of a + * fabricated empty board when the cwd is not a resolvable project. Source of + * truth: `ProjectContextKind` in `src/lib/v5/genie-db.ts`. The health probe runs + * in a throwaway non-project cwd, so one of these is the EXPECTED wish_status + * outcome and proves the read-only path works — it is not a failure. + */ +const NO_PROJECT_CONTEXT_ERROR_KINDS = new Set([ + 'project-context-unavailable', + 'project-database-unavailable', + 'unsupported-project-layout', +]); + /** The five read-only Genie tools every healthy plugin launcher must expose. */ export const REQUIRED_GENIE_MCP_TOOLS = [ 'genie_board', @@ -138,6 +154,22 @@ function replyError(reply: JsonRpcReply | undefined, label: string): string | nu return null; } +/** + * A read-only wish_status is healthy when it either returns a non-error board + * (`isError === false`) OR a well-formed fail-closed "no project context" typed + * error. The health probe deliberately runs in a throwaway non-project cwd where + * the latter is expected: the server correctly refuses to serve an empty board + * without mutating disk. Any other `isError: true` (an unrecognized/real tool + * failure) is still rejected. + */ +function wishStatusIsReadOnlyHealthy(result: { isError?: unknown; structuredContent?: unknown }): boolean { + if (result.isError === false) return true; + if (result.isError !== true) return false; + const structured = result.structuredContent; + const kind = structured && typeof structured === 'object' ? (structured as { error?: unknown }).error : undefined; + return typeof kind === 'string' && NO_PROJECT_CONTEXT_ERROR_KINDS.has(kind); +} + function listedToolNames(reply: JsonRpcReply): string[] { const result = reply.result as { tools?: unknown }; if (!Array.isArray(result.tools)) return []; @@ -199,8 +231,8 @@ export function runBoundedCodexMcpSession(options: BoundedCodexMcpSessionOptions const callReply = byId.get(3); const callError = replyError(callReply, 'genie_wish_status'); if (callError !== null) return { ok: false, detail: callError }; - const callResult = (callReply as JsonRpcReply).result as { isError?: unknown }; - if (callResult.isError !== false) { + const callResult = (callReply as JsonRpcReply).result as { isError?: unknown; structuredContent?: unknown }; + if (!wishStatusIsReadOnlyHealthy(callResult)) { return { ok: false, detail: 'read-only genie_wish_status did not return a non-error result' }; } diff --git a/src/lib/codex-project-mcp.test.ts b/src/lib/codex-project-mcp.test.ts index 47aac626a..15a7c9a57 100644 --- a/src/lib/codex-project-mcp.test.ts +++ b/src/lib/codex-project-mcp.test.ts @@ -16,6 +16,7 @@ import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { type CodexPluginProbe, + genieFacadeMcpEntry, genieMcpEntry, inspectCodexPluginMcpUsability, inspectCodexProjectMcp, @@ -76,7 +77,16 @@ function seedActivePlugin(version = '1.2.3'): { const manifestPath = join(pluginRoot, '.codex-plugin', 'plugin.json'); const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')) as Record; manifest.version = version; + // Group A removed the Codex plugin MCP route from the SHIPPED payload, so the + // fixture reconstructs a hypothetical plugin-with-MCP layout to keep exercising + // the fail-closed usability guard (launcher/node/binary). The removal itself is + // proven separately against the real source plugin. + manifest.mcpServers = './.mcp.json'; writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); + writeFileSync( + join(pluginRoot, '.mcp.json'), + `${JSON.stringify({ mcp_servers: { genie: { command: 'node', args: ['./scripts/mcp-launcher.cjs'], cwd: '.' } } }, null, 2)}\n`, + ); const genieHome = join(root, 'genie-home'); const binary = join(genieHome, 'bin', process.platform === 'win32' ? 'genie.exe' : 'genie'); const nodePath = join(root, 'controlled-bin', 'node'); @@ -122,6 +132,71 @@ describe('project MCP executable entry', () => { }); }); +describe('genieFacadeMcpEntry (marker-owned Codex route)', () => { + test('is the stable /bin/genie facade with args ["mcp"] and no cwd override', () => { + expect(genieFacadeMcpEntry('/home/u/.genie', 'linux')).toEqual({ + command: '/home/u/.genie/bin/genie', + args: ['mcp'], + }); + // No effective cwd override is part of the entry (source omits it entirely). + expect(genieFacadeMcpEntry('/home/u/.genie', 'linux')).not.toHaveProperty('cwd'); + }); + + test('uses genie.exe on win32 and is independent of the running executable', () => { + expect(genieFacadeMcpEntry('C:/Users/u/.genie', 'win32')).toEqual({ + command: 'C:/Users/u/.genie/bin/genie.exe', + args: ['mcp'], + }); + // A relocated GENIE_HOME yields a different facade — the command is not the cache/execPath. + expect(genieFacadeMcpEntry('/relocated/home', 'linux').command).toBe('/relocated/home/bin/genie'); + }); +}); + +describe('Codex plugin MCP route removal (Group A)', () => { + test('the shipped Codex plugin manifest declares NO mcpServers and NO MCP capability', () => { + const manifest = JSON.parse( + readFileSync(join(import.meta.dir, '..', '..', 'plugins', 'genie', '.codex-plugin', 'plugin.json'), 'utf8'), + ) as { mcpServers?: unknown; interface?: { capabilities?: unknown } }; + expect(manifest.mcpServers).toBeUndefined(); + expect(manifest.interface?.capabilities).not.toContain('MCP'); + }); + + test('the shipped Codex plugin payload ships no .mcp.json route file', () => { + expect(existsSync(join(import.meta.dir, '..', '..', 'plugins', 'genie', '.mcp.json'))).toBe(false); + }); + + test('a real (unaltered) plugin copy is never a usable MCP provider — the guard fails closed', () => { + const pluginRoot = join(root, 'real-plugin'); + cpSync(join(import.meta.dir, '..', '..', 'plugins', 'genie'), pluginRoot, { + recursive: true, + dereference: false, + verbatimSymlinks: true, + }); + const manifest = JSON.parse(readFileSync(join(pluginRoot, '.codex-plugin', 'plugin.json'), 'utf8')) as { + version: string; + }; + const result = inspectCodexPluginMcpUsability({ + pluginRoot, + expectedPluginName: 'genie', + expectedVersion: manifest.version, + genieHome: join(root, 'genie-home'), + resolveCommand: () => join(root, 'node'), + }); + expect(result.usable).toBe(false); + // It fails at the manifest indirection because the route was removed. + expect(result.detail).toContain('mcpServers'); + }); + + // Claude's separate inline launcher contract MUST remain intact. + test("Claude's plugin manifest still declares its own inline mcpServers launcher", () => { + const claude = JSON.parse( + readFileSync(join(import.meta.dir, '..', '..', 'plugins', 'genie', '.claude-plugin', 'plugin.json'), 'utf8'), + ) as { mcpServers?: { genie?: { command?: string; args?: string[] } } }; + expect(claude.mcpServers?.genie?.command).toBe('node'); + expect(claude.mcpServers?.genie?.args?.[0]).toContain('mcp-launcher.cjs'); + }); +}); + describe('resolveGitWorktreeRoot', () => { test('resolves root, nested directory, and linked worktree to their own working-tree roots', () => { const repo = join(root, 'repo'); diff --git a/src/lib/codex-project-mcp.ts b/src/lib/codex-project-mcp.ts index 50a58eb8f..55a1c2c95 100644 --- a/src/lib/codex-project-mcp.ts +++ b/src/lib/codex-project-mcp.ts @@ -1,13 +1,13 @@ /** * Project-scoped MCP lifecycle for Codex, Claude Code, and Warp. * - * Codex has two possible routes to Genie's stdio MCP server: - * 1. the enabled `genie@automagik` plugin; or - * 2. a marker-owned `/.codex/config.toml` fallback. + * Codex has one supported route to Genie's stdio MCP server: the marker-owned + * `/.codex/config.toml` entry. The enabled `genie@automagik` plugin + * deliberately declares no MCP server. * - * This module is the sole owner of choosing between them. It deliberately - * treats disabled, unknown, malformed, and timed-out plugin state as - * ineffective and keeps an absolute-command fallback in those cases. + * This module is the sole owner of reconciling and inspecting that route. Its + * legacy route-classification helpers remain fail-closed for older installed + * payloads, but production init/setup always retain the stable absolute facade. */ import { execFileSync } from 'node:child_process'; @@ -120,7 +120,23 @@ export interface RegisterProjectMcpOptions { /** Reuse a caller's one-shot probe so a launch with N worktrees queries Codex once. */ pluginProbe?: CodexPluginProbe; probeDeps?: CodexPluginProbeDeps; + /** Command written to the Claude/Warp JSON configs (`.mcp.json`, `.warp/.mcp.json`). */ entry?: McpServerEntry; + /** + * Dedicated command for the marker-owned Codex `.codex/config.toml` route. + * Defaults to {@link RegisterProjectMcpOptions.entry}. Trusted `genie init` + * passes the stable {@link genieFacadeMcpEntry} facade here so the Codex marker + * uses the version-independent `/bin/genie` command even while + * Claude/Warp keep the running-executable entry. + */ + codexEntry?: McpServerEntry; + /** + * Reconcile the marker-owned Codex route INDEPENDENT of plugin/delivery state + * (trusted `genie init`). A route name is not proof of ownership, so an + * unmanaged same-key route is still preserved and reported rather than + * overwritten. + */ + forceCodexFallback?: boolean; } export interface GitProjectRoots { @@ -406,8 +422,8 @@ export function resolveGitWorktreeRoot( /** * Verify the exact active plugin MCP indirection plus the only binary the * plugin-local launcher is permitted to execute. This is a read-only check; - * enabled metadata or a healthy source bundle never removes the absolute - * project fallback. + * enabled metadata or a healthy source bundle never replaces the absolute + * project route. */ export function inspectCodexPluginMcpUsability(options: CodexPluginMcpUsabilityOptions = {}): CodexPluginMcpUsability { const pluginRoot = options.pluginRoot; @@ -620,6 +636,22 @@ export function genieMcpEntry(command?: string, argv = process.argv): McpServerE return { command: executable, args: script ? [script, 'mcp'] : ['mcp'] }; } +/** + * The stable marker-owned Codex route command: the canonical absolute + * `/bin/genie` facade with `args = ["mcp"]` and NO effective cwd + * override. Unlike {@link genieMcpEntry} (which records the running executable + * for Claude/Warp), this facade path is deliberately version- and + * plugin-independent: it survives plugin updates and symlinked invocation, and + * is rewritten only by trusted `genie init` after an explicit GENIE_HOME + * relocation. It never resolves to a versioned plugin-cache path. + */ +export function genieFacadeMcpEntry( + genieHome: string = resolveGenieHome(), + platform: NodeJS.Platform = process.platform, +): McpServerEntry { + return { command: join(genieHome, 'bin', platform === 'win32' ? 'genie.exe' : 'genie'), args: ['mcp'] }; +} + function isJsonObject(value: unknown): value is JsonObject { return typeof value === 'object' && value !== null && !Array.isArray(value); } @@ -973,14 +1005,18 @@ export function reconcileCodexProjectMcp( */ export function registerProjectMcpConfigs(root: string, options: RegisterProjectMcpOptions = {}): McpConfigResult[] { const entry = options.entry ?? genieMcpEntry(); - const plugin = options.pluginProbe ?? probeCodexGeniePlugin(options.probeDeps); + const codexEntry = options.codexEntry ?? entry; const preparedJson = [join(root, '.mcp.json'), join(root, '.warp', '.mcp.json')].map((path) => prepareJsonMcpConfig(path, entry), ); - // Explicit init must leave a discoverable route even before the Codex CLI is - // installed. The marker-owned fallback is lifecycle-owned and can later be - // removed atomically when a usable plugin is proven. - const preparedCodex = prepareCodexFallback(join(root, '.codex', 'config.toml'), entry, !isUsableCodexPlugin(plugin)); + // Trusted init reconciles the marker-owned Codex route independent of plugin + // state (`forceCodexFallback`) — the `||` short-circuits so a forced call never + // spends a Codex query. Otherwise the marker is retained only when no usable + // plugin is proven. Either way an unowned same-key route is preserved. + const required = + options.forceCodexFallback === true || + !isUsableCodexPlugin(options.pluginProbe ?? probeCodexGeniePlugin(options.probeDeps)); + const preparedCodex = prepareCodexFallback(join(root, '.codex', 'config.toml'), codexEntry, required); if (!preparedCodex.ok) { throw new Error(`Cannot reconcile Codex project MCP at ${preparedCodex.path}: ${preparedCodex.detail}`); diff --git a/src/lib/codex-release-version.ts b/src/lib/codex-release-version.ts new file mode 100644 index 000000000..a710b2e8f --- /dev/null +++ b/src/lib/codex-release-version.ts @@ -0,0 +1,48 @@ +/** + * Pure, dependency-free Codex release-version grammar and control-sequence + * sanitiser. + * + * Extracted from `codex-activation.ts` so that the lower-level host observation / + * delivery-attestation module (`codex-host-observation.ts`) can validate versions + * and sanitise bounded subprocess text WITHOUT importing the activation protocol, + * and the activation protocol can in turn import the single delivery assessment + * from `codex-host-observation.ts` with no import cycle. `codex-activation.ts` + * re-exports the public members so existing importers are unchanged. + */ + +/** Exact `MAJOR.YYMMDD.N` release grammar; build metadata is stripped only after a match. */ +export const RELEASE_VERSION_RE = /^(\d+)\.(\d{6})\.(\d+)(?:\+[0-9A-Za-z][0-9A-Za-z.-]*)?$/; + +export interface ParsedReleaseVersion { + readonly major: number; + readonly ymd: number; + readonly n: number; + /** The `MAJOR.YYMMDD.N` triple with build metadata removed; used for equality. */ + readonly canonical: string; +} + +/** Parse a release version, returning null for anything that fails the exact grammar. */ +export function parseReleaseVersion(raw: unknown): ParsedReleaseVersion | null { + if (typeof raw !== 'string') return null; + const match = RELEASE_VERSION_RE.exec(raw); + if (!match) return null; + const major = Number(match[1]); + const ymd = Number(match[2]); + const n = Number(match[3]); + if (!Number.isSafeInteger(major) || !Number.isSafeInteger(ymd) || !Number.isSafeInteger(n)) return null; + return { major, ymd, n, canonical: `${major}.${match[2]}.${n}` }; +} + +/** Total numeric order over validated versions. */ +export function compareReleaseVersions(a: ParsedReleaseVersion, b: ParsedReleaseVersion): -1 | 0 | 1 { + if (a.major !== b.major) return a.major < b.major ? -1 : 1; + if (a.ymd !== b.ymd) return a.ymd < b.ymd ? -1 : 1; + if (a.n !== b.n) return a.n < b.n ? -1 : 1; + return 0; +} + +/** Strip ANSI CSI and OSC control sequences from modeled diagnostics/output. */ +export function stripControl(text: string): string { + // biome-ignore lint/suspicious/noControlCharactersInRegex: intentionally matching ESC/BEL control bytes. + return text.replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, '').replace(/\x1b\[[0-9;?]*[ -\/]*[@-~]/g, ''); +} diff --git a/src/lib/install-version-marker.test.ts b/src/lib/install-version-marker.test.ts new file mode 100644 index 000000000..192dcbf3a --- /dev/null +++ b/src/lib/install-version-marker.test.ts @@ -0,0 +1,101 @@ +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { mkdtempSync, readdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { + INSTALL_VERSION_MARKER_NAME, + readInstallVersionMarker, + resolveInstallVersionMarkerPath, + retireInstallVersionMarker, +} from './install-version-marker.js'; + +let genieHome: string; + +beforeEach(() => { + genieHome = mkdtempSync(join(tmpdir(), 'genie-install-marker-')); +}); + +afterEach(() => { + rmSync(genieHome, { recursive: true, force: true }); +}); + +function markerPath(): string { + return join(genieHome, INSTALL_VERSION_MARKER_NAME); +} + +function writeMarker(content: string): void { + writeFileSync(markerPath(), content); +} + +function backupSidecars(): string[] { + return readdirSync(genieHome).filter((name) => name.startsWith(`${INSTALL_VERSION_MARKER_NAME}.genie-backup-`)); +} + +describe('resolveInstallVersionMarkerPath', () => { + test('resolves the stable .install-version path under the given home', () => { + expect(resolveInstallVersionMarkerPath(genieHome)).toBe(join(genieHome, '.install-version')); + }); +}); + +describe('readInstallVersionMarker — never a version authority, only diagnostics', () => { + test('absent when no marker exists', () => { + expect(readInstallVersionMarker(genieHome)).toEqual({ status: 'absent' }); + }); + + test('present returns the trimmed legacy value', () => { + writeMarker(' 5.260500.3\n'); + expect(readInstallVersionMarker(genieHome)).toEqual({ status: 'present', value: '5.260500.3' }); + }); + + test('a symlink marker is unsafe (never followed)', () => { + writeFileSync(join(genieHome, 'real-version'), '9.9.9'); + symlinkSync(join(genieHome, 'real-version'), markerPath()); + const read = readInstallVersionMarker(genieHome); + expect(read.status).toBe('unsafe'); + }); + + test('an oversized marker is unsafe drift', () => { + writeMarker('x'.repeat(5 * 1024)); + const read = readInstallVersionMarker(genieHome); + expect(read.status).toBe('unsafe'); + }); +}); + +describe('retireInstallVersionMarker — retire only after success, preserve on unsafe, idempotent', () => { + test('already-absent is a no-op success', () => { + expect(retireInstallVersionMarker(genieHome)).toEqual({ status: 'already-absent' }); + }); + + test('a present regular marker is backed up then removed, reporting its prior value', () => { + writeMarker('5.260500.3\n'); + const result = retireInstallVersionMarker(genieHome); + expect(result).toEqual({ status: 'retired', previousValue: '5.260500.3' }); + // Marker gone. + expect(readInstallVersionMarker(genieHome)).toEqual({ status: 'absent' }); + // Prior bytes preserved in exactly one backup sidecar. + expect(backupSidecars()).toHaveLength(1); + }); + + test('retirement is idempotent — a second call after retirement is already-absent with no new backup', () => { + writeMarker('5.260500.3'); + retireInstallVersionMarker(genieHome); + const before = backupSidecars().length; + expect(retireInstallVersionMarker(genieHome)).toEqual({ status: 'already-absent' }); + expect(backupSidecars()).toHaveLength(before); + }); + + test('an unsafe (symlink) marker is preserved exactly in place, never followed or deleted', () => { + writeFileSync(join(genieHome, 'real-version'), '9.9.9'); + symlinkSync(join(genieHome, 'real-version'), markerPath()); + const result = retireInstallVersionMarker(genieHome); + expect(result.status).toBe('preserved'); + // The symlink survives untouched (still an unsafe read), and its target still holds its bytes. + expect(readInstallVersionMarker(genieHome).status).toBe('unsafe'); + expect(readInstallVersionMarker(genieHome).status).not.toBe('absent'); + }); + + test('an empty marker retires with a null previous value', () => { + writeMarker(' \n'); + expect(retireInstallVersionMarker(genieHome)).toEqual({ status: 'retired', previousValue: null }); + }); +}); diff --git a/src/lib/install-version-marker.ts b/src/lib/install-version-marker.ts new file mode 100644 index 000000000..51d869de3 --- /dev/null +++ b/src/lib/install-version-marker.ts @@ -0,0 +1,123 @@ +/** + * `.install-version` legacy install-marker lifecycle — Group D exclusive ownership. + * + * The v4-era installer wrote a `~/.genie/.install-version` marker whose content + * duplicated the installed version. In v5 the canonical `VERSION` file is the + * SOLE installed-version authority (Decision 14): the legacy marker is orphaned + * drift that disagrees with `VERSION` on any machine upgraded across the v4→v5 + * boundary. Synchronising it would only mint a second authority, so this module + * RETIRES the marker rather than reconciling it. + * + * This is the ONE module that owns the marker's lifecycle. Only install, update, + * and uninstall wire to it; Group C (role-agent convergence) never imports or + * mutates this API, which keeps install-lifecycle writes off the role-convergence + * critical path. + * + * - `readInstallVersionMarker` — bounded, symlink-rejecting read of the legacy + * marker (`absent | present | unsafe`). Never trusted as version authority. + * - `retireInstallVersionMarker` — delete the legacy marker AFTER a successful + * install/update convergence. The caller gates the call on convergence + * success, so a failed convergence never reaches it and the prior bytes stay + * intact. The delete is backup-first (prior bytes are copied to a + * `.genie-backup-*` sidecar) and atomic, so an interrupted retirement leaves + * either the original file or nothing recoverable-from-backup — never a + * partial write. An unsafe (symlink/non-regular) marker is preserved and + * reported rather than followed. Idempotent: an already-absent marker is a + * no-op success. + * UNINSTALL deliberately has NO dedicated marker call (Group E decision): the + * digest-verified wholesale GENIE_HOME removal already deletes the marker in + * both legacy layouts (regular file, symlink — the link is unlinked, never its + * target; pinned by uninstall.test.ts). The marker is a snapshotted removable + * child of `genieHomeRemovalDigest`, so any extra delete inside the + * plan→execute window would poison that commitment and abort home removal; + * before the window it is redundant, after it a no-op. + * + * Canonical `VERSION` is authoritative: this module never writes a version into + * the marker and never reads it back as the installed version. + */ + +import { randomBytes } from 'node:crypto'; +import { copyFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { fsyncParentDir, readBoundedRegularFile, unlinkWithParentFsync } from './codex-activation-persistence.js'; +import { resolveGenieHome } from './genie-home.js'; + +/** The stable legacy marker filename, resolved under `GENIE_HOME`. */ +export const INSTALL_VERSION_MARKER_NAME = '.install-version'; + +/** A legacy marker cannot plausibly exceed a version string; anything larger is treated as unsafe drift. */ +const MAX_MARKER_BYTES = 4 * 1024; + +/** Stable path to the legacy marker under a given (or resolved) Genie home. */ +export function resolveInstallVersionMarkerPath(genieHome: string = resolveGenieHome()): string { + return join(genieHome, INSTALL_VERSION_MARKER_NAME); +} + +export type InstallVersionMarkerRead = + | { status: 'absent' } + | { status: 'present'; value: string } + | { status: 'unsafe'; detail: string }; + +/** + * Bounded, symlink-rejecting read of the legacy marker. The returned `value` is + * the trimmed marker content for diagnostics ONLY; callers must never treat it as + * the installed version (canonical `VERSION` is authoritative). A symlink, + * non-regular kind, oversize, or unreadable marker is `unsafe`, never `present`. + */ +export function readInstallVersionMarker(genieHome: string = resolveGenieHome()): InstallVersionMarkerRead { + const read = readBoundedRegularFile(resolveInstallVersionMarkerPath(genieHome), MAX_MARKER_BYTES); + switch (read.status) { + case 'absent': + return { status: 'absent' }; + case 'ok': + return { status: 'present', value: read.content.trim() }; + case 'symlink': + return { status: 'unsafe', detail: 'install-version marker is a symlink' }; + case 'non-regular': + return { status: 'unsafe', detail: 'install-version marker is not a regular file' }; + case 'oversized': + return { status: 'unsafe', detail: `install-version marker is oversized (${read.size} bytes)` }; + case 'unreadable': + return { status: 'unsafe', detail: `install-version marker unreadable: ${read.detail}` }; + } +} + +export type InstallVersionMarkerRetirement = + | { status: 'already-absent' } + | { status: 'retired'; previousValue: string | null } + | { status: 'preserved'; detail: string }; + +/** + * Retire (delete) the legacy marker after a successful install/update + * convergence. The caller MUST only invoke this once convergence has succeeded; + * a failed convergence returns before this point so the marker's prior bytes are + * preserved untouched. + * + * A present regular marker is backed up (prior bytes copied to a + * `.genie-backup-*` sidecar) and then atomically unlinked. An already-absent + * marker is a no-op success (idempotent). An unsafe marker (symlink or + * non-regular) is left exactly in place and reported — retirement never follows a + * symlink or blindly removes an unexpected kind. + */ +export function retireInstallVersionMarker(genieHome: string = resolveGenieHome()): InstallVersionMarkerRetirement { + const path = resolveInstallVersionMarkerPath(genieHome); + const read = readInstallVersionMarker(genieHome); + if (read.status === 'absent') return { status: 'already-absent' }; + if (read.status === 'unsafe') return { status: 'preserved', detail: read.detail }; + // Regular file: preserve prior bytes to a backup sidecar, then atomically remove. + backupRegularFile(path); + unlinkWithParentFsync(path); + return { status: 'retired', previousValue: read.value.length > 0 ? read.value : null }; +} + +/** Best-effort copy of prior marker bytes to a unique sidecar; a backup failure never blocks retirement. */ +function backupRegularFile(path: string): void { + const backup = `${path}.genie-backup-${new Date().toISOString().replace(/[:.]/g, '-')}-${randomBytes(6).toString('hex')}`; + try { + copyFileSync(path, backup); + fsyncParentDir(backup); + } catch { + // Prior bytes are already durable on disk until the unlink below; a failed + // forensic copy must not prevent retirement of a proven-current install. + } +} diff --git a/src/lib/omni-runner.ts b/src/lib/omni-runner.ts index b4713fc7a..fbce1723f 100644 --- a/src/lib/omni-runner.ts +++ b/src/lib/omni-runner.ts @@ -24,8 +24,8 @@ * initialize the transport (proven by the {@link natsConnectionCount} marker). * * NATS is injected behind {@link NatsLike} so tests drive a fake with zero - * network. The real transport is a dynamic `import('nats')` — never a top-level - * import — so merely loading this module costs nothing. + * network. The real transport is a dynamic `import('@nats-io/transport-node')` + * — never a top-level import — so merely loading this module costs nothing. */ import type { Database } from 'bun:sqlite'; @@ -1146,9 +1146,9 @@ export function makeDefaultOmniSetReaction(config: OmniRuntimeConfig): OmniSetRe }; } -/** Default factory — dynamically imports `nats`. Only invoked by `omni serve`. */ +/** Default factory — dynamically imports the Node NATS transport. Only invoked by `omni serve`. */ export const defaultNatsFactory: NatsFactory = async ({ servers }) => { - const nats = await import('nats'); + const nats = await import('@nats-io/transport-node'); const nc = await nats.connect({ servers }); natsConnections++; const enc = new TextEncoder(); diff --git a/src/lib/ordered-lifecycle-leases.test.ts b/src/lib/ordered-lifecycle-leases.test.ts new file mode 100644 index 000000000..89583ac2f --- /dev/null +++ b/src/lib/ordered-lifecycle-leases.test.ts @@ -0,0 +1,189 @@ +import { describe, expect, test } from 'bun:test'; +import { acquireOrderedLifecycleLeases, releaseOrderedLifecycleLeases } from './ordered-lifecycle-leases.js'; + +function heldCodexLease(release: () => void = () => undefined) { + return { + ok: true as const, + operationId: 'a'.repeat(32), + kind: 'update-delivery' as const, + assertOperation: () => undefined, + release, + }; +} + +describe('ordered lifecycle leases', () => { + test('acquires agent-sync before Codex and releases in reverse order', () => { + const events: string[] = []; + const acquired = acquireOrderedLifecycleLeases( + () => { + events.push('acquire-agent-sync'); + return { path: '/fixture/agent-sync.lock', release: () => events.push('release-agent-sync') }; + }, + () => { + events.push('acquire-codex'); + return heldCodexLease(() => events.push('release-codex')); + }, + ); + + expect(acquired.ok).toBe(true); + if (!acquired.ok) throw new Error('fixture acquisition unexpectedly busy'); + releaseOrderedLifecycleLeases(acquired.codexLease, acquired.agentSyncLease); + expect(events).toEqual(['acquire-agent-sync', 'acquire-codex', 'release-codex', 'release-agent-sync']); + }); + + test('returns a discriminated agent-sync busy result without acquiring Codex', () => { + let codexAcquisitions = 0; + const acquired = acquireOrderedLifecycleLeases( + () => ({ skipped: 'outer busy' }), + () => { + codexAcquisitions += 1; + return heldCodexLease(); + }, + ); + + expect(acquired).toEqual({ ok: false, busy: 'agent-sync', detail: 'outer busy' }); + expect(codexAcquisitions).toBe(0); + }); + + test('returns a discriminated Codex busy result after releasing agent-sync', () => { + const events: string[] = []; + const acquired = acquireOrderedLifecycleLeases( + () => ({ path: '/fixture/agent-sync.lock', release: () => events.push('release-agent-sync') }), + () => ({ + ok: false as const, + reason: 'codex-lifecycle-busy' as const, + holderKind: 'setup-activation' as const, + detail: 'inner busy', + }), + ); + + expect(acquired).toEqual({ + ok: false, + busy: 'codex', + refusal: { + ok: false, + reason: 'codex-lifecycle-busy', + holderKind: 'setup-activation', + detail: 'inner busy', + }, + }); + expect(events).toEqual(['release-agent-sync']); + }); + + test('releases agent-sync when Codex acquisition throws', () => { + const acquisitionError = new Error('Codex acquisition fixture'); + const events: string[] = []; + + expect(() => + acquireOrderedLifecycleLeases( + () => { + events.push('acquire-agent-sync'); + return { path: '/fixture/agent-sync.lock', release: () => events.push('release-agent-sync') }; + }, + () => { + events.push('acquire-codex'); + throw acquisitionError; + }, + ), + ).toThrow(acquisitionError); + expect(events).toEqual(['acquire-agent-sync', 'acquire-codex', 'release-agent-sync']); + }); + + test('aggregates Codex acquisition and agent-sync release failures in causal order', () => { + const acquisitionError = new Error('Codex acquisition fixture'); + const releaseError = new Error('agent-sync release fixture'); + const events: string[] = []; + let thrown: unknown; + + try { + acquireOrderedLifecycleLeases( + () => { + events.push('acquire-agent-sync'); + return { + path: '/fixture/agent-sync.lock', + release: () => { + events.push('release-agent-sync'); + throw releaseError; + }, + }; + }, + () => { + events.push('acquire-codex'); + throw acquisitionError; + }, + ); + } catch (error) { + thrown = error; + } + + expect(events).toEqual(['acquire-agent-sync', 'acquire-codex', 'release-agent-sync']); + expect(thrown).toBeInstanceOf(AggregateError); + expect((thrown as AggregateError).errors).toEqual([acquisitionError, releaseError]); + }); + + test('an inner-only release failure is preserved after the outer release runs', () => { + const innerError = new Error('inner release fixture'); + const events: string[] = []; + + expect(() => + releaseOrderedLifecycleLeases( + { + release: () => { + events.push('release-codex'); + throw innerError; + }, + }, + { release: () => events.push('release-agent-sync') }, + ), + ).toThrow(innerError); + expect(events).toEqual(['release-codex', 'release-agent-sync']); + }); + + test('an outer-only release failure is preserved after the inner release runs', () => { + const outerError = new Error('outer release fixture'); + const events: string[] = []; + + expect(() => + releaseOrderedLifecycleLeases( + { release: () => events.push('release-codex') }, + { + release: () => { + events.push('release-agent-sync'); + throw outerError; + }, + }, + ), + ).toThrow(outerError); + expect(events).toEqual(['release-codex', 'release-agent-sync']); + }); + + test('dual release failures are aggregated deterministically after both run', () => { + const innerError = new Error('inner release fixture'); + const outerError = new Error('outer release fixture'); + const events: string[] = []; + let thrown: unknown; + + try { + releaseOrderedLifecycleLeases( + { + release: () => { + events.push('release-codex'); + throw innerError; + }, + }, + { + release: () => { + events.push('release-agent-sync'); + throw outerError; + }, + }, + ); + } catch (error) { + thrown = error; + } + + expect(events).toEqual(['release-codex', 'release-agent-sync']); + expect(thrown).toBeInstanceOf(AggregateError); + expect((thrown as AggregateError).errors).toEqual([innerError, outerError]); + }); +}); diff --git a/src/lib/ordered-lifecycle-leases.ts b/src/lib/ordered-lifecycle-leases.ts new file mode 100644 index 000000000..002d52003 --- /dev/null +++ b/src/lib/ordered-lifecycle-leases.ts @@ -0,0 +1,99 @@ +import type { LifecycleLease } from './agent-sync.js'; +import type { HeldLifecycleLease, LifecycleLeaseBusy, LifecycleLeaseResult } from './codex-lifecycle-lease.js'; + +export interface ReleasableLifecycleLease { + release(): void; +} + +export type OrderedLifecycleLeaseAcquisition = + | { + ok: true; + agentSyncLease: LifecycleLease; + codexLease: HeldLifecycleLease; + } + | { + ok: false; + busy: 'agent-sync'; + detail: string; + } + | { + ok: false; + busy: 'codex'; + refusal: LifecycleLeaseBusy; + }; + +export type HeldOrderedLifecycleLeases = Extract; + +/** + * Acquire the process-wide agent-sync lease before the Codex lifecycle lease. + * A Codex loser releases the already-held outer lease before returning busy. + */ +export function acquireOrderedLifecycleLeases( + acquireAgentSync: () => LifecycleLease | { skipped: string }, + acquireCodex: () => LifecycleLeaseResult, +): OrderedLifecycleLeaseAcquisition { + const agentSyncLease = acquireAgentSync(); + if ('skipped' in agentSyncLease) { + return { ok: false, busy: 'agent-sync', detail: agentSyncLease.skipped }; + } + + let codexLease: LifecycleLeaseResult; + try { + codexLease = acquireCodex(); + } catch (acquisitionError) { + releaseAfterFailedAcquisition(acquisitionError, agentSyncLease); + } + if (!codexLease.ok) { + releaseOrderedLifecycleLeases(null, agentSyncLease); + return { ok: false, busy: 'codex', refusal: codexLease }; + } + return { ok: true, agentSyncLease, codexLease }; +} + +/** + * Release in exact reverse acquisition order. Both releases are attempted; + * one failure is preserved and two are aggregated inner-first. + */ +export function releaseOrderedLifecycleLeases( + codexLease: ReleasableLifecycleLease | null, + agentSyncLease: ReleasableLifecycleLease, +): void { + let codexReleaseFailed = false; + let codexReleaseError: unknown; + try { + codexLease?.release(); + } catch (error) { + codexReleaseFailed = true; + codexReleaseError = error; + } + + let agentSyncReleaseFailed = false; + let agentSyncReleaseError: unknown; + try { + agentSyncLease.release(); + } catch (error) { + agentSyncReleaseFailed = true; + agentSyncReleaseError = error; + } + + if (codexReleaseFailed && agentSyncReleaseFailed) { + throw new AggregateError( + [codexReleaseError, agentSyncReleaseError], + 'Codex and agent-sync lifecycle lease releases both failed', + ); + } + if (codexReleaseFailed) throw codexReleaseError; + if (agentSyncReleaseFailed) throw agentSyncReleaseError; +} + +function releaseAfterFailedAcquisition(acquisitionError: unknown, agentSyncLease: ReleasableLifecycleLease): never { + try { + agentSyncLease.release(); + } catch (releaseError) { + throw new AggregateError( + [acquisitionError, releaseError], + 'Codex lifecycle lease acquisition and agent-sync lifecycle lease release both failed', + ); + } + throw acquisitionError; +} diff --git a/src/lib/runtime-integrations.test.ts b/src/lib/runtime-integrations.test.ts index 972eeeedc..3beba46f8 100644 --- a/src/lib/runtime-integrations.test.ts +++ b/src/lib/runtime-integrations.test.ts @@ -11,6 +11,7 @@ import { realpathSync, rmSync, symlinkSync, + utimesSync, writeFileSync, } from 'node:fs'; import { tmpdir } from 'node:os'; @@ -20,13 +21,19 @@ import { applyCodexFallbackRetirement, computeDirDigest, planCodexFallbackRetirement, + recoverCodexFallbackRetirements, } from './agent-sync.js'; import { REQUIRED_GENIE_MCP_TOOLS } from './codex-mcp-health-session.js'; import type { CodexPluginProbe } from './codex-project-mcp.js'; import { + CANONICAL_CODEX_REVIEWER_DIGEST, + CANONICAL_CODEX_ROLE_AGENT_DIGESTS, + CANONICAL_CODEX_ROLE_AGENT_MODE, + CANONICAL_CODEX_ROLE_AGENT_NAMES, CANONICAL_GENIE_SKILL_NAMES, type CodexHealthProof, type CodexPluginOnlyDeps, + EXPECTED_CODEX_ROLE_AGENT_TOTAL, type InstallIntegrationsOptions, type IntegrationResult, type ProveCodexPluginHealthOptions, @@ -34,15 +41,21 @@ import { claudePluginState, clearIntegrationConsentTransition, codexPluginState, + codexRoleAdoptionAllowed, + codexRoleAgentTupleKey, commitIntegrationConsentTransition, convergeClaudePlugin, convergeCodexPlugin, convergeCodexPluginOnly, + createSetupCodexFallbackRetirementConsumer, + createSetupCodexRoleAgentConsumer, inspectCodexAgentOwnership, inspectCodexFallbackTier, inspectRuntimeIntegrationEvidence, installCodexAgents, installRuntimeIntegrations as installRuntimeIntegrationsWithPhysicalVerification, + loadHistoricalRoleAgentProfiles, + loadHistoricalRoleAgentTupleKeys, parseClaudePluginState, parseCodexPluginState, persistIntegrationConsent, @@ -54,6 +67,7 @@ import { removeRuntimeIntegrations as removeRuntimeIntegrationsWithTrustedResolution, resolveBundleRoot, runBoundedIntegrationCommand, + runDeliveryRootConsumer, setCodexPluginEnabled, translateRetirementConflicts, verifyClaudePhysicalPayload, @@ -2567,7 +2581,6 @@ describe('proveCodexPluginHealth reject-before-retirement matrix (R4)', () => { const rejectCases: Array<[string, Partial]> = [ ['a disabled plugin', { enabled: false }], ['a wrong-version plugin', { version: '0.0.0' }], - ['an unusable launcher', { usable: false }], ['an ambiguous active root', { activePluginRoot: undefined }], ['an errored snapshot', { status: 'error' }], ]; @@ -2579,6 +2592,18 @@ describe('proveCodexPluginHealth reject-before-retirement matrix (R4)', () => { }); } + test('accepts a snapshot with no Codex MCP route (usable:false) — plugin MCP is Claude-only now', () => { + // Group A removed the plugin's Codex MCP route, so a post-A snapshot reports + // usable:false. Plugin health is decoupled from Codex-MCP-route usability: a + // proven active root + verified payload + a working launcher session is + // healthy. The launcher (retained for Claude) is exercised by runSession. + const opts = healthyOpts(); + opts.snapshot = { ...opts.snapshot, usable: false, usabilityDetail: 'plugin ships no Codex MCP route' }; + const proof = proveCodexPluginHealth(opts); + expect(proof.version).toBe(1); + expect(proof.mcp.wishStatusReadOnly).toBe(true); + }); + test('rejects payload identity drift (verifyCodexPayload throws)', () => { const opts = healthyOpts(); opts.verifyCodexPayload = () => { @@ -2919,3 +2944,644 @@ describe('inspectCodexFallbackTier (shared doctor/uninstall classifier — read- expect(report.preservedCollisions).toEqual([]); }); }); + +// ── Group C: managed-assets-convergence ───────────────────────────────────── +// Adopt delivered Codex role agents + inventory without claiming or damaging +// user-owned files. Real role bytes come straight from the committed bundle so +// the frozen allowlist is exercised against the exact profiles it must recognize. + +const REAL_CODEX_AGENTS_DIR = resolve(import.meta.dir, '..', '..', 'plugins', 'genie', 'codex-agents'); + +function sha256Hex(bytes: Buffer | string): string { + return createHash('sha256').update(bytes).digest('hex'); +} + +/** Byte-exact buffer comparison (avoids the SharedArrayBuffer-union friction of `toEqual` on raw reads). */ +function expectSameBytes(actual: Buffer, expected: Buffer): void { + expect(actual.equals(expected)).toBe(true); +} + +function realRoleNames(): string[] { + return readdirSync(REAL_CODEX_AGENTS_DIR) + .filter((name) => name.endsWith('.toml')) + .sort(); +} + +function realRoleBytes(name: string): Buffer { + return readFileSync(join(REAL_CODEX_AGENTS_DIR, name)); +} + +/** A temp bundle carrying the exact current role profiles at canonical mode 0644. */ +function makeRealCodexBundle(): string { + const bundleRoot = canonicalTempDir('genie-real-bundle-'); + const dest = join(bundleRoot, 'plugins', 'genie', 'codex-agents'); + mkdirSync(dest, { recursive: true }); + for (const name of realRoleNames()) { + const path = join(dest, name); + writeFileSync(path, realRoleBytes(name), { mode: 0o644 }); + chmodSync(path, 0o644); + } + return bundleRoot; +} + +function seedRoleFile(codexHome: string, name: string, bytes: Buffer | string, mode = 0o644): string { + const path = join(codexHome, 'agents', name); + mkdirSync(dirname(path), { recursive: true }); + writeFileSync(path, bytes, { mode }); + chmodSync(path, mode); + return path; +} + +/** The (name, mode, digest) allowlist key for a file exactly as it currently sits on disk. */ +function liveTupleKey(codexHome: string, name: string): string { + const path = join(codexHome, 'agents', name); + const stat = lstatSync(path); + return codexRoleAgentTupleKey(name, stat.mode & 0o7777, sha256Hex(readFileSync(path))); +} + +function stateByName(codexHome: string): Map { + return new Map(inspectCodexAgentOwnership(codexHome).entries.map((entry) => [entry.name, entry.state])); +} + +/** Real-installer convergence options (no injected installAgents, so adoption is exercised end-to-end). */ +function realConvergeOptions(input: { + codexHome: string; + genieHome: string; + bundleRoot: string; + deps?: Partial; +}) { + return { + runner: (() => ({ exitCode: 0, stdout: '{}', stderr: '' })) as never, + command: '/fixture/codex', + bundleRoot: input.bundleRoot, + expectedVersion: VERSION, + installIfAbsent: true, + statePath: join(input.genieHome, '.integration-refresh-codex.json'), + codexHome: input.codexHome, + genieHome: input.genieHome, + deps: { + converge: () => healthyPluginResult(), + probe: () => healthyCodexProbe(), + prove: () => healthyCodexProof(), + runSession: () => ({ ok: true, detail: 'ok', tools: [...REQUIRED_GENIE_MCP_TOOLS], wishStatusReadOnly: true }), + fallbackSkillsDir: canonicalTempDir('genie-fallback-conv-'), + ...input.deps, + } as CodexPluginOnlyDeps, + }; +} + +describe('setup Codex fallback-retirement consumer', () => { + function targetPayload( + names: readonly string[], + bodies: Readonly>, + ): { root: string; proof: CodexHealthProof } { + const root = canonicalTempDir('genie-setup-retirement-target-'); + const payload = names.map((skillName) => { + const path = join(root, skillName); + mkdirSync(path); + writeFileSync(join(path, 'SKILL.md'), bodies[skillName] ?? `# ${skillName}\n`); + return { skillName, path, physicalDigest: computeDirDigest(path), canonicalVerified: true as const }; + }); + return { + root, + proof: Object.freeze({ + version: 1, + snapshot: healthyCodexProbe(), + activePluginRoot: '/fixture/plugin/root', + expectedVersion: VERSION, + skillInventory: [...names], + payload, + mcp: { initialized: true, tools: [...REQUIRED_GENIE_MCP_TOOLS], wishStatusReadOnly: true }, + }) as CodexHealthProof, + }; + } + + test('uses the admitted physical root, proves health before recovery, and preserves every non-owned class', () => { + const fallback = canonicalTempDir('genie-setup-retirement-fallback-'); + const codexHome = canonicalTempDir('genie-setup-retirement-codex-'); + const names = ['wish', 'fix', 'review', 'work'] as const; + const bodies = { + wish: '# wish\n', + fix: '# fix\n', + review: '# review\n', + work: '# work\n', + }; + const { root: deliveryRoot, proof } = targetPayload(names, bodies); + + const clean = join(fallback, 'wish'); + mkdirSync(clean); + writeFileSync(join(clean, 'SKILL.md'), bodies.wish); + stampFallbackSkill(clean); + + const modified = join(fallback, 'fix'); + mkdirSync(modified); + writeFileSync(join(modified, 'SKILL.md'), bodies.fix); + stampFallbackSkill(modified); + writeFileSync(join(modified, 'SKILL.md'), '# personal modification\n'); + const modifiedBefore = readFileSync(join(modified, 'SKILL.md')); + + const unmanaged = join(fallback, 'review'); + mkdirSync(unmanaged); + writeFileSync(join(unmanaged, 'SKILL.md'), '# personal review\n'); + const unmanagedBefore = readFileSync(join(unmanaged, 'SKILL.md')); + + const external = canonicalTempDir('genie-setup-retirement-external-'); + writeFileSync(join(external, 'SKILL.md'), '# external work\n'); + const symlink = join(fallback, 'work'); + symlinkSync(external, symlink); + + const trace: string[] = []; + const result = runDeliveryRootConsumer( + createSetupCodexFallbackRetirementConsumer({ + command: '/fixture/bin/codex', + expectedVersion: VERSION, + codexHome, + deps: { + fallbackSkillsDir: fallback, + probe: (options) => { + trace.push(`probe:${options.which?.('codex')}`); + return healthyCodexProbe(); + }, + prove: (options) => { + trace.push(`prove:${options.bundleRoot}`); + return proof; + }, + recover: (dir) => { + trace.push('recover'); + return recoverCodexFallbackRetirements(dir); + }, + plan: (options) => { + trace.push('plan'); + return planCodexFallbackRetirement(options); + }, + apply: (plan) => { + trace.push('apply'); + return applyCodexFallbackRetirement(plan); + }, + }, + }), + deliveryRoot, + ); + + expect(trace).toEqual(['probe:/fixture/bin/codex', `prove:${deliveryRoot}`, 'recover', 'plan', 'apply']); + expect(result.retired).toEqual(['wish']); + expect(result.status).toBe('verified'); + expect(existsSync(clean)).toBe(false); + expectSameBytes(readFileSync(join(modified, 'SKILL.md')), modifiedBefore); + expectSameBytes(readFileSync(join(unmanaged, 'SKILL.md')), unmanagedBefore); + expect(lstatSync(symlink).isSymbolicLink()).toBe(true); + expect(realpathSync(symlink)).toBe(external); + }); + + test('an exact disabled plugin preserves fallbacks without proving health or entering recovery', () => { + const fallback = canonicalTempDir('genie-setup-retirement-disabled-'); + const skill = join(fallback, 'wish'); + mkdirSync(skill); + writeFileSync(join(skill, 'SKILL.md'), '# disabled fallback\n'); + stampFallbackSkill(skill); + const before = readFileSync(join(skill, 'SKILL.md')); + let proveCalls = 0; + let recoverCalls = 0; + + const result = runDeliveryRootConsumer( + createSetupCodexFallbackRetirementConsumer({ + command: '/fixture/bin/codex', + expectedVersion: VERSION, + codexHome: canonicalTempDir('genie-setup-retirement-disabled-codex-'), + deps: { + fallbackSkillsDir: fallback, + probe: () => ({ ...healthyCodexProbe(), enabled: false }), + prove: () => { + proveCalls += 1; + return healthyCodexProof(); + }, + recover: () => { + recoverCalls += 1; + return []; + }, + }, + }), + canonicalTempDir('genie-setup-retirement-disabled-root-'), + ); + + expect(result).toEqual({ + status: 'skipped-disabled', + retired: [], + preservedCollisions: 0, + preservedUnrecognized: 0, + }); + expect(proveCalls).toBe(0); + expect(recoverCalls).toBe(0); + expectSameBytes(readFileSync(join(skill, 'SKILL.md')), before); + expect(existsSync(join(fallback, CODEX_FALLBACK_RETIREMENT_ROOT))).toBe(false); + }); + + test('a disabled wrong-version snapshot is not a preservation bypass', () => { + const fallback = canonicalTempDir('genie-setup-retirement-disabled-wrong-version-'); + const skill = join(fallback, 'wish'); + mkdirSync(skill); + writeFileSync(join(skill, 'SKILL.md'), '# wrong-version fallback\n'); + stampFallbackSkill(skill); + let recoverCalls = 0; + const consumer = createSetupCodexFallbackRetirementConsumer({ + command: '/fixture/bin/codex', + expectedVersion: VERSION, + codexHome: canonicalTempDir('genie-setup-retirement-disabled-wrong-version-codex-'), + deps: { + fallbackSkillsDir: fallback, + probe: () => ({ ...healthyCodexProbe(), version: '5.260711.1', enabled: false }), + recover: () => { + recoverCalls += 1; + return []; + }, + }, + }); + + expect(() => + runDeliveryRootConsumer(consumer, canonicalTempDir('genie-setup-retirement-disabled-wrong-version-root-')), + ).toThrow('health rejected before retirement'); + expect(recoverCalls).toBe(0); + expect(existsSync(skill)).toBe(true); + expect(existsSync(join(fallback, CODEX_FALLBACK_RETIREMENT_ROOT))).toBe(false); + }); + + test('an unhealthy enabled plugin fails before the first fallback mutation', () => { + const fallback = canonicalTempDir('genie-setup-retirement-unhealthy-'); + const skill = join(fallback, 'wish'); + mkdirSync(skill); + writeFileSync(join(skill, 'SKILL.md'), '# unhealthy fallback\n'); + stampFallbackSkill(skill); + const before = readFileSync(join(skill, 'SKILL.md')); + let recoverCalls = 0; + + const consumer = createSetupCodexFallbackRetirementConsumer({ + command: '/fixture/bin/codex', + expectedVersion: VERSION, + codexHome: canonicalTempDir('genie-setup-retirement-unhealthy-codex-'), + deps: { + fallbackSkillsDir: fallback, + probe: () => healthyCodexProbe(), + prove: () => { + throw new Error('fixture health failure'); + }, + recover: () => { + recoverCalls += 1; + return []; + }, + }, + }); + + expect(() => runDeliveryRootConsumer(consumer, canonicalTempDir('genie-setup-retirement-unhealthy-root-'))).toThrow( + 'fixture health failure', + ); + expect(recoverCalls).toBe(0); + expectSameBytes(readFileSync(join(skill, 'SKILL.md')), before); + expect(existsSync(join(fallback, CODEX_FALLBACK_RETIREMENT_ROOT))).toBe(false); + }); +}); + +describe('codex role-agent frozen historical allowlist (Group C)', () => { + test('CANONICAL role digests/mode match the committed bundle bytes exactly (parity gate)', () => { + const onDisk = realRoleNames(); + expect(onDisk).toEqual([...CANONICAL_CODEX_ROLE_AGENT_NAMES]); + expect(EXPECTED_CODEX_ROLE_AGENT_TOTAL).toBe(onDisk.length); + for (const name of CANONICAL_CODEX_ROLE_AGENT_NAMES) { + expect(sha256Hex(realRoleBytes(name))).toBe(CANONICAL_CODEX_ROLE_AGENT_DIGESTS[name]); + expect(lstatSync(join(REAL_CODEX_AGENTS_DIR, name)).mode & 0o7777).toBe(CANONICAL_CODEX_ROLE_AGENT_MODE); + } + expect(CANONICAL_CODEX_REVIEWER_DIGEST).toBe(CANONICAL_CODEX_ROLE_AGENT_DIGESTS['genie-reviewer.toml']); + }); + + test('the frozen allowlist is well-formed, covers every current profile, and carries historical reviewers', () => { + const profiles = loadHistoricalRoleAgentProfiles(); + expect(profiles.every((profile) => profile.type === 'regular' && profile.mode === 0o644)).toBe(true); + const keys = loadHistoricalRoleAgentTupleKeys(); + // Every current delivered profile is a member (a current-version fan-out is adoptable). + for (const name of CANONICAL_CODEX_ROLE_AGENT_NAMES) { + expect(keys.has(codexRoleAgentTupleKey(name, 0o644, CANONICAL_CODEX_ROLE_AGENT_DIGESTS[name]))).toBe(true); + } + // The reviewer legitimately fanned several profiles across releases; the union must recognize them. + const reviewerProfiles = profiles.filter((profile) => profile.name === 'genie-reviewer.toml'); + expect(reviewerProfiles.length).toBeGreaterThan(1); + const reviewerDigests = new Set(reviewerProfiles.map((profile) => profile.digest)); + expect(reviewerDigests.has(CANONICAL_CODEX_REVIEWER_DIGEST)).toBe(true); + // A known older reviewer profile (the introduction commit) stays recognizable as the "stale" case. + expect(reviewerDigests.has('91f40a07905834716311419375581e3245544a77eac3d93d082842652c6452bf')).toBe(true); + }); + + test('codexRoleAdoptionAllowed requires an explicit committed codex/all consent', () => { + expect(codexRoleAdoptionAllowed({ selection: 'codex', state: 'committed', revision: 1 })).toBe(true); + expect(codexRoleAdoptionAllowed({ selection: 'all', state: 'committed', revision: 1 })).toBe(true); + // Legacy auto default, sibling client, opt-out, and in-flight transitions never unlock adoption. + expect(codexRoleAdoptionAllowed({ selection: 'auto', state: 'committed', revision: 0 })).toBe(false); + expect(codexRoleAdoptionAllowed({ selection: 'claude', state: 'committed', revision: 1 })).toBe(false); + expect(codexRoleAdoptionAllowed({ selection: 'none', state: 'committed', revision: 1 })).toBe(false); + expect( + codexRoleAdoptionAllowed({ + selection: 'codex', + state: 'pending', + revision: 2, + previousSelection: 'auto', + transitionToken: 'a'.repeat(32), + }), + ).toBe(false); + }); + + test('setup role convergence refuses before committed Codex consent with zero agent mutation', () => { + const genieHome = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-setup-roles-refused-'); + const personal = seedRoleFile(codexHome, 'personal.toml', 'name = "personal"\n', 0o600); + const before = readFileSync(personal); + const beforeMode = lstatSync(personal).mode & 0o7777; + + const consumer = createSetupCodexRoleAgentConsumer({ genieHome, codexHome }); + expect(() => runDeliveryRootConsumer(consumer, genieHome)).toThrow( + 'requires committed explicit Codex integration consent', + ); + expectSameBytes(readFileSync(personal), before); + expect(lstatSync(personal).mode & 0o7777).toBe(beforeMode); + expect(readdirSync(join(codexHome, 'agents'))).toEqual(['personal.toml']); + }); + + test('setup role convergence adopts only frozen roles and preserves unrelated user agents exactly', () => { + const genieHome = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-setup-roles-authorized-'); + for (const name of realRoleNames()) seedRoleFile(codexHome, name, realRoleBytes(name)); + const personal = seedRoleFile(codexHome, 'personal.toml', 'name = "personal"\nmode = "custom"\n', 0o600); + const before = readFileSync(personal); + const beforeMode = lstatSync(personal).mode & 0o7777; + persistIntegrationConsent('codex', genieHome); + + const result = runDeliveryRootConsumer(createSetupCodexRoleAgentConsumer({ genieHome, codexHome }), genieHome); + + expect(result.adoptedLegacy?.sort()).toEqual(realRoleNames()); + expect(JSON.stringify(result)).not.toContain(genieHome); + expect(JSON.stringify(result)).not.toContain(codexHome); + expect(inspectCodexAgentOwnership(codexHome).entries.filter((entry) => entry.state === 'managed')).toHaveLength( + realRoleNames().length, + ); + expectSameBytes(readFileSync(personal), before); + expect(lstatSync(personal).mode & 0o7777).toBe(beforeMode); + }); +}); + +describe('installCodexAgents historical adoption (Group C)', () => { + test('live-shape 0/7 inventory: consent adopts every delivered role and refreshes the stale reviewer', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-liveshape-'); + const names = realRoleNames(); + const currentReviewer = realRoleBytes('genie-reviewer.toml'); + // The observed state: seven fanned files, NO inventory, and one older-but-genuine reviewer profile. + const staleReviewer = Buffer.concat([currentReviewer, Buffer.from('\n# older delivered profile\n')]); + for (const name of names) { + seedRoleFile(codexHome, name, name === 'genie-reviewer.toml' ? staleReviewer : realRoleBytes(name)); + } + // Two unrelated personal agents that must survive byte-, mode-, and timestamp-identically. + const personalPath = seedRoleFile(codexHome, 'genie-my-notes.toml', 'name = "my_notes"\n', 0o644); + const personalBytes = readFileSync(personalPath); + utimesSync(personalPath, new Date('2020-01-02T03:04:05Z'), new Date('2020-01-02T03:04:05Z')); + const personalMtime = lstatSync(personalPath).mtimeMs; + const nonRolePath = seedRoleFile(codexHome, 'genie-scratch.txt', 'scratch\n', 0o600); + + // The stale reviewer is a genuine historical profile, so inject its live identity into the allowlist. + const historical = new Set([...loadHistoricalRoleAgentTupleKeys(), liveTupleKey(codexHome, 'genie-reviewer.toml')]); + const result = installCodexAgents( + bundle, + codexHome, + {}, + { adoptHistorical: true, historicalTupleKeys: historical }, + ); + + expect(result.adoptedLegacy?.sort()).toEqual([...names]); + expect(result.installed).toBe(names.length); + expect(result.skippedUserOwned).toEqual([]); + // The reviewer was refreshed to the delivered profile; every role is now one managed surface. + expectSameBytes(readFileSync(join(codexHome, 'agents', 'genie-reviewer.toml')), currentReviewer); + const report = inspectCodexAgentOwnership(codexHome); + expect(report.expectedDeliveredTotal).toBe(EXPECTED_CODEX_ROLE_AGENT_TOTAL); + expect(report.reviewerDigest).toBe(CANONICAL_CODEX_REVIEWER_DIGEST); + expect(report.entries.filter((entry) => entry.state === 'managed').map((entry) => entry.name)).toEqual([...names]); + // Personal files untouched: bytes, permissions, and timestamps preserved. + expectSameBytes(readFileSync(personalPath), personalBytes); + expect(lstatSync(personalPath).mode & 0o7777).toBe(0o644); + expect(lstatSync(personalPath).mtimeMs).toBe(personalMtime); + expect(stateByName(codexHome).get('genie-my-notes.toml')).toBe('personal'); + expect(existsSync(nonRolePath)).toBe(true); + }); + + test('adoption of a byte-identical current profile records inventory without any refresh write', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-adopt-current-'); + seedRoleFile(codexHome, 'genie-scout.toml', realRoleBytes('genie-scout.toml')); + const before = lstatSync(join(codexHome, 'agents', 'genie-scout.toml')).mtimeMs; + // Only converge scout by pointing the bundle's other names away is unnecessary: absent targets install fresh. + const result = installCodexAgents(bundle, codexHome, {}, { adoptHistorical: true }); + expect(result.adoptedLegacy).toEqual(['genie-scout.toml']); + // scout adopted (no rewrite — mtime stable), the other six installed fresh. + expect(lstatSync(join(codexHome, 'agents', 'genie-scout.toml')).mtimeMs).toBe(before); + expect(stateByName(codexHome).get('genie-scout.toml')).toBe('managed'); + }); + + test('without adoptHistorical, an exact historical profile stays user-owned but reports adoptable-historical', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-noadopt-'); + seedRoleFile(codexHome, 'genie-reviewer.toml', realRoleBytes('genie-reviewer.toml')); + const result = installCodexAgents(bundle, codexHome); // default convergence: adoption OFF + expect(result.adoptedLegacy).toBeUndefined(); + expect(result.skippedUserOwned).toContain('genie-reviewer.toml'); + // Reporting never requires consent: the file is visibly adoptable, but untouched. + expect(stateByName(codexHome).get('genie-reviewer.toml')).toBe('adoptable-historical'); + expectSameBytes( + readFileSync(join(codexHome, 'agents', 'genie-reviewer.toml')), + realRoleBytes('genie-reviewer.toml'), + ); + }); + + test('adoptHistorical never adopts a lookalike, wrong-mode, symlink, or non-historical collision', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-collision-'); + // Sentinel lookalike: managed header but content Genie never shipped. + const lookalike = '# Managed by Genie. Remove with `genie uninstall`.\nname = "genie_reviewer"\n# hand-tuned\n'; + const reviewerPath = seedRoleFile(codexHome, 'genie-reviewer.toml', lookalike); + // Exact current profile but at the wrong mode — an identity mismatch, not an exact tuple. + const scoutPath = seedRoleFile(codexHome, 'genie-scout.toml', realRoleBytes('genie-scout.toml'), 0o600); + // A symlink at a role path is never followed or replaced. + const fixerPath = join(codexHome, 'agents', 'genie-fixer.toml'); + mkdirSync(dirname(fixerPath), { recursive: true }); + symlinkSync(join(codexHome, 'outside-target.toml'), fixerPath); + + const result = installCodexAgents(bundle, codexHome, {}, { adoptHistorical: true }); + + expect(result.adoptedLegacy).toBeUndefined(); + expect(result.skippedUserOwned.sort()).toEqual(['genie-fixer.toml', 'genie-reviewer.toml', 'genie-scout.toml']); + // Nothing was overwritten, adopted, or deleted. + expect(readFileSync(reviewerPath, 'utf8')).toBe(lookalike); + expectSameBytes(readFileSync(scoutPath), realRoleBytes('genie-scout.toml')); + expect(lstatSync(scoutPath).mode & 0o7777).toBe(0o600); + expect(lstatSync(fixerPath).isSymbolicLink()).toBe(true); + const states = stateByName(codexHome); + expect(states.get('genie-reviewer.toml')).toBe('collision'); + expect(states.get('genie-scout.toml')).toBe('collision'); + expect(states.get('genie-fixer.toml')).toBe('collision'); + }); + + test('repeated adoption convergence is idempotent and produces no duplicate surfaces or transactions', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-idem-'); + const names = realRoleNames(); + for (const name of names) seedRoleFile(codexHome, name, realRoleBytes(name)); + + const first = installCodexAgents(bundle, codexHome, {}, { adoptHistorical: true }); + expect(first.adoptedLegacy?.sort()).toEqual([...names]); + + const second = installCodexAgents(bundle, codexHome, {}, { adoptHistorical: true }); + // Already inventory-owned: nothing left to adopt, no writes, no duplicates. + expect(second).toEqual({ + installed: names.length, + skippedUserOwned: [], + keptModified: [], + removed: [], + backedUp: [], + }); + expect( + readdirSync(join(codexHome, 'agents')) + .filter((name) => name.endsWith('.toml')) + .sort(), + ).toEqual([...names]); + expect(() => recoverCodexAgentTransactions(codexHome)).not.toThrow(); + }); + + test('an interrupted adoption transaction recovers to the converged state and stays idempotent', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-interrupt-'); + const names = realRoleNames(); + const currentReviewer = realRoleBytes('genie-reviewer.toml'); + const staleReviewer = Buffer.concat([currentReviewer, Buffer.from('\n# older\n')]); + for (const name of names) { + seedRoleFile(codexHome, name, name === 'genie-reviewer.toml' ? staleReviewer : realRoleBytes(name)); + } + const historical = new Set([...loadHistoricalRoleAgentTupleKeys(), liveTupleKey(codexHome, 'genie-reviewer.toml')]); + + expect(() => + installCodexAgents( + bundle, + codexHome, + { + afterCommit() { + throw new Error('injected crash after durable commit'); + }, + }, + { adoptHistorical: true, historicalTupleKeys: historical }, + ), + ).toThrow('injected crash after durable commit'); + + // The commit was durable; recovery finishes cleanup and the reviewer is refreshed. + recoverCodexAgentTransactions(codexHome); + expectSameBytes(readFileSync(join(codexHome, 'agents', 'genie-reviewer.toml')), currentReviewer); + const report = inspectCodexAgentOwnership(codexHome); + expect(report.status).toBe('valid'); + expect(report.entries.filter((entry) => entry.state === 'managed').map((entry) => entry.name)).toEqual([...names]); + expect(installCodexAgents(bundle, codexHome, {}, { adoptHistorical: true }).adoptedLegacy).toBeUndefined(); + }); + + test('inspectCodexAgentOwnership distinguishes managed, stale, and absent states with delivered totals', () => { + // A bundle whose reviewer is an older profile so the managed install is clean-but-stale. + const staleBundle = makeRealCodexBundle(); + const stalePath = join(staleBundle, 'plugins', 'genie', 'codex-agents', 'genie-reviewer.toml'); + writeFileSync(stalePath, Buffer.concat([realRoleBytes('genie-reviewer.toml'), Buffer.from('# older\n')]), { + mode: 0o644, + }); + chmodSync(stalePath, 0o644); + const codexHome = canonicalTempDir('genie-codex-states-'); + installCodexAgents(staleBundle, codexHome); // fresh managed install of six current + one stale profile + rmSync(join(codexHome, 'agents', 'genie-scout.toml')); // recorded but now missing on disk + + const report = inspectCodexAgentOwnership(codexHome); + const states = new Map(report.entries.map((entry) => [entry.name, entry.state])); + expect(states.get('genie-reviewer.toml')).toBe('stale'); // managed-clean but not the delivered digest + expect(states.get('genie-scout.toml')).toBe('absent'); + expect(states.get('genie-fixer.toml')).toBe('managed'); + expect(report.expectedDeliveredTotal).toBe(EXPECTED_CODEX_ROLE_AGENT_TOTAL); + expect(report.reviewerDigest).toBe(CANONICAL_CODEX_REVIEWER_DIGEST); + }); +}); + +describe('convergeCodexPluginOnly role adoption + fallback (Group C)', () => { + test('an enabled plugin adopts delivered roles under committed codex consent', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-conv-enabled-'); + const genieHome = canonicalTempDir('genie-home-conv-enabled-'); + const names = realRoleNames(); + for (const name of names) seedRoleFile(codexHome, name, realRoleBytes(name)); + persistIntegrationConsent('codex', genieHome); + + const outcome = convergeCodexPluginOnly(realConvergeOptions({ codexHome, genieHome, bundleRoot: bundle })); + expect(outcome?.proof).not.toBeNull(); + expect(outcome?.agents.adoptedLegacy?.sort()).toEqual([...names]); + expect(stateByName(codexHome).size).toBe(names.length); + expect(inspectCodexAgentOwnership(codexHome).entries.every((entry) => entry.state === 'managed')).toBe(true); + }); + + test('convergence does NOT adopt without committed codex consent (a committed sibling client is not consent)', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-conv-noconsent-'); + const genieHome = canonicalTempDir('genie-home-conv-noconsent-'); + const names = realRoleNames(); + for (const name of names) seedRoleFile(codexHome, name, realRoleBytes(name)); + persistIntegrationConsent('claude', genieHome); + + const outcome = convergeCodexPluginOnly(realConvergeOptions({ codexHome, genieHome, bundleRoot: bundle })); + expect(outcome?.agents.adoptedLegacy).toBeUndefined(); + expect(outcome?.agents.skippedUserOwned.sort()).toEqual([...names]); + // Files remain exactly as fanned — visibly adoptable, never claimed. + expect(inspectCodexAgentOwnership(codexHome).entries.every((entry) => entry.state === 'adoptable-historical')).toBe( + true, + ); + }); + + test('a disabled plugin still installs the fallback role set (R3) and adopts under consent', () => { + const bundle = makeRealCodexBundle(); + const names = realRoleNames(); + const disabledDeps: Partial = { + converge: () => ({ ...healthyPluginResult(), preservedDisabled: true }), + probe: () => ({ ...healthyCodexProbe(), enabled: false, usable: false }), + }; + + // Fresh home: the disabled path restores the fallback roles with no health proof. + const freshHome = canonicalTempDir('genie-codex-conv-disabled-fresh-'); + const freshGenie = canonicalTempDir('genie-home-conv-disabled-fresh-'); + const fresh = convergeCodexPluginOnly( + realConvergeOptions({ codexHome: freshHome, genieHome: freshGenie, bundleRoot: bundle, deps: disabledDeps }), + ); + expect(fresh?.proof).toBeNull(); + expect(fresh?.agents.installed).toBe(names.length); + expect( + readdirSync(join(freshHome, 'agents')) + .filter((name) => name.endsWith('.toml')) + .sort(), + ).toEqual([...names]); + + // Legacy home under consent: the disabled path adopts the exact historical profiles too. + const legacyHome = canonicalTempDir('genie-codex-conv-disabled-legacy-'); + const legacyGenie = canonicalTempDir('genie-home-conv-disabled-legacy-'); + for (const name of names) seedRoleFile(legacyHome, name, realRoleBytes(name)); + persistIntegrationConsent('all', legacyGenie); + const legacy = convergeCodexPluginOnly( + realConvergeOptions({ codexHome: legacyHome, genieHome: legacyGenie, bundleRoot: bundle, deps: disabledDeps }), + ); + expect(legacy?.agents.adoptedLegacy?.sort()).toEqual([...names]); + }); + + test('an explicit deps override forces adoption independently of the consent file', () => { + const bundle = makeRealCodexBundle(); + const codexHome = canonicalTempDir('genie-codex-conv-override-'); + const genieHome = canonicalTempDir('genie-home-conv-override-'); + const names = realRoleNames(); + for (const name of names) seedRoleFile(codexHome, name, realRoleBytes(name)); + // No consent persisted, but the override unlocks adoption deterministically for tests. + const outcome = convergeCodexPluginOnly( + realConvergeOptions({ codexHome, genieHome, bundleRoot: bundle, deps: { adoptHistoricalRoleAgents: true } }), + ); + expect(outcome?.agents.adoptedLegacy?.sort()).toEqual([...names]); + }); +}); diff --git a/src/lib/runtime-integrations.ts b/src/lib/runtime-integrations.ts index d28171fab..58581b11e 100644 --- a/src/lib/runtime-integrations.ts +++ b/src/lib/runtime-integrations.ts @@ -22,6 +22,7 @@ import { mkdtempSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { basename, dirname, join, relative, resolve } from 'node:path'; import { Worker } from 'node:worker_threads'; +import historicalRoleAgentAllowlist from '../fixtures/codex-role-agent-allowlist.json'; import { CODEX_FALLBACK_RETIREMENT_ROOT, type CodexFallbackOwnership, @@ -210,6 +211,18 @@ export function readIntegrationConsent(genieHome = resolveGenieHome()): Integrat return readIntegrationConsentState(genieHome).selection; } +/** + * Whether committed Codex consent authorizes adopting an exact frozen historical + * role profile. Adoption is a privileged claim on a previously user-owned file, + * so it requires an EXPLICIT committed `codex`/`all` selection — never the legacy + * `auto` default and never an in-flight `pending` transition. This is the sole + * consent gate the role-agent convergence consults; an exact allowlist match is + * still additionally required before any file is adopted. + */ +export function codexRoleAdoptionAllowed(consent: IntegrationConsentState): boolean { + return consent.state === 'committed' && (consent.selection === 'codex' || consent.selection === 'all'); +} + /** Publish explicit maintenance consent before the first external setup mutation. */ export function beginIntegrationConsentTransition( selection: IntegrationSelection, @@ -307,6 +320,60 @@ class IntegrationCommandError extends Error { const defaultRunner: CommandRunner = runBoundedIntegrationCommand; +declare const DELIVERY_ROOT_CONSUMER_RESULT: unique symbol; + +/** + * Opaque synchronous consumer of one store-admitted physical delivery root. + * The implementation and root argument remain module-private; activation + * callers can select a narrow operation but cannot extract the root. + */ +export interface DeliveryRootConsumer { + readonly [DELIVERY_ROOT_CONSUMER_RESULT]: T; +} + +const DELIVERY_ROOT_CONSUMERS = new WeakMap unknown>(); + +function createDeliveryRootConsumer(consume: (physicalRoot: string) => T): DeliveryRootConsumer { + const capability = Object.freeze({}) as DeliveryRootConsumer; + DELIVERY_ROOT_CONSUMERS.set(capability, consume); + return capability; +} + +/** + * Internal bridge for CodexActivationStore's freshness-checked `consume`. + * A structural lookalike is rejected by WeakMap identity. + */ +export function runDeliveryRootConsumer(consumer: DeliveryRootConsumer, physicalRoot: string): T { + const consume = DELIVERY_ROOT_CONSUMERS.get(consumer); + if (consume === undefined) throw new IntegrationCommandError('unrecognized delivery-root consumer capability'); + // Capabilities are single-use even if a caller retains the opaque object. + DELIVERY_ROOT_CONSUMERS.delete(consumer); + return consume(physicalRoot) as T; +} + +export interface SetupCodexConsentCommitOptions { + genieHome?: string; + selection: IntegrationSelection; + /** Test seam for the durable consent writer. */ + persist?: typeof persistIntegrationConsent; +} + +/** + * Create setup's narrow explicit-consent commit capability. The delivery root + * is intentionally not passed to the write: its only role is to force this + * privileged state transition through the store's immediate freshness checks. + */ +export function createSetupCodexConsentCommitConsumer( + options: SetupCodexConsentCommitOptions, +): DeliveryRootConsumer { + if (options.selection !== 'codex' && options.selection !== 'all') { + throw new IntegrationCommandError('setup Codex consent commit requires explicit codex/all scope'); + } + return createDeliveryRootConsumer(() => + (options.persist ?? persistIntegrationConsent)(options.selection, options.genieHome ?? resolveGenieHome()), + ); +} + const BOUNDED_RUNNER_WORKER = String.raw` const { spawn } = require('node:child_process'); const { workerData } = require('node:worker_threads'); @@ -652,6 +719,104 @@ const CODEX_AGENT_NAME_RE = /^genie-[A-Za-z0-9][A-Za-z0-9_-]*\.toml$/; const CODEX_AGENT_SENTINEL = '# Managed by Genie.'; const CODEX_AGENT_INVENTORY_MODE = 0o600; +/** + * The exact set of role agents the current Genie bundle fans into + * `~/.codex/agents/`, with each role's canonical current content digest and the + * physical mode every historical installer wrote (git-tracked 0644, preserved by + * the install tarball). These are the *current* delivered profiles: doctor reports + * this expected total and the reviewer digest, and the read-only ownership inspector + * uses them to distinguish an up-to-date managed role from a stale one. A + * parity test (`runtime-integrations.test.ts`) binds this map to the real + * `plugins/genie/codex-agents/*.toml` bytes so a role edit that forgets to update + * the frozen allowlist fails the gate rather than silently drifting. + */ +export const CANONICAL_CODEX_ROLE_AGENT_MODE = 0o644; + +export const CANONICAL_CODEX_ROLE_AGENT_DIGESTS: Readonly> = Object.freeze({ + 'genie-engineer-complex.toml': '62ecc570f1d77783511a9e7f0aa67b3a65d8bba292963409a02c7712c93ebc3b', + 'genie-engineer-standard.toml': 'dc746813b9b4b6aa984c17fa2fd75d4dbe34eba08494a174c0715da07aa9dd30', + 'genie-engineer-trivial.toml': '249deced5a02eb2cbe3303db566992d1336c75d853967f851bf1d0e85b6b0f47', + 'genie-final-gate.toml': '10ef070db8aace75bd80ef9e060a6ec601e3768f177fdd843f4db11035738f7e', + 'genie-fixer.toml': 'b3c1f407d4a3a2cfe204dee7b4a9c038e1a8f4644c446fcfa23f4a681bf0c7b3', + 'genie-reviewer.toml': 'c7008dcaa1e31b46e2bb05ca13afb2e918ee483422c84386a1c8997485bcfea7', + 'genie-scout.toml': '03a9fb3ca0e5f36c69c8f934d37adce1bae736e4c3895b144a0001ad31b1ba59', +}); + +/** The exact expected delivered role-agent filenames, name-sorted. */ +export const CANONICAL_CODEX_ROLE_AGENT_NAMES: readonly string[] = Object.freeze( + Object.keys(CANONICAL_CODEX_ROLE_AGENT_DIGESTS).sort(), +); + +/** The current reviewer profile digest doctor surfaces so an operator can spot a stale reviewer. */ +export const CANONICAL_CODEX_REVIEWER_DIGEST = CANONICAL_CODEX_ROLE_AGENT_DIGESTS['genie-reviewer.toml'] as string; + +/** The number of delivered role agents; doctor reports live coverage against this. */ +export const EXPECTED_CODEX_ROLE_AGENT_TOTAL = CANONICAL_CODEX_ROLE_AGENT_NAMES.length; + +/** + * One frozen historical profile: the exact identity (name + regular file type + + * mode + content digest) of a role-agent file a Genie release legitimately fanned + * into `~/.codex/agents/`. The allowlist is the union across releases (the reviewer + * carries several), so a legacy install whose reviewer is an older-but-genuine + * profile is still recognizable. Membership alone never authorizes a write — see + * {@link installCodexAgents}: adoption additionally requires committed Codex consent. + */ +export interface RoleAgentHistoricalProfile { + name: string; + type: 'regular'; + mode: number; + digest: string; +} + +/** Ownership proof key for a historical role profile: (name, mode, content digest). */ +export function codexRoleAgentTupleKey(name: string, mode: number, digest: string): string { + return `${name}\0${mode}\0${digest}`; +} + +function parseHistoricalRoleAgentMode(raw: unknown): number | null { + if (typeof raw !== 'string' || !/^0?[0-7]{3,4}$/.test(raw)) return null; + const mode = Number.parseInt(raw, 8); + return Number.isSafeInteger(mode) && mode >= 0 && mode <= 0o7777 ? mode : null; +} + +/** + * Load and validate the frozen historical role-agent allowlist. A malformed + * fixture is a build defect, not a runtime condition to tolerate, so this throws + * rather than silently degrading adoption into "recognize nothing". + */ +export function loadHistoricalRoleAgentProfiles(): RoleAgentHistoricalProfile[] { + const raw = historicalRoleAgentAllowlist as unknown; + if (!Array.isArray(raw)) throw new Error('codex role-agent allowlist must be a JSON array'); + const seen = new Set(); + return raw.map((entry, index) => { + const name = typeof entry === 'object' && entry !== null ? Reflect.get(entry, 'name') : undefined; + const type = typeof entry === 'object' && entry !== null ? Reflect.get(entry, 'type') : undefined; + const digest = typeof entry === 'object' && entry !== null ? Reflect.get(entry, 'digest') : undefined; + const mode = parseHistoricalRoleAgentMode( + typeof entry === 'object' && entry !== null ? Reflect.get(entry, 'mode') : undefined, + ); + if ( + typeof name !== 'string' || + !CODEX_AGENT_NAME_RE.test(name) || + type !== 'regular' || + mode === null || + typeof digest !== 'string' || + !/^[a-f0-9]{64}$/.test(digest) + ) { + throw new Error(`codex role-agent allowlist entry ${index} is malformed`); + } + const key = codexRoleAgentTupleKey(name, mode, digest); + if (seen.has(key)) throw new Error(`codex role-agent allowlist entry ${index} is a duplicate tuple`); + seen.add(key); + return { name, type, mode, digest }; + }); +} + +/** The frozen allowlist as an ownership-key set: `name\0mode\0digest` for every legitimate historical profile. */ +export function loadHistoricalRoleAgentTupleKeys(): ReadonlySet { + return new Set(loadHistoricalRoleAgentProfiles().map((p) => codexRoleAgentTupleKey(p.name, p.mode, p.digest))); +} + export type RegularRoleFileIdentity = { kind: 'regular'; mode: number; digest: string }; type RoleFileIdentity = @@ -678,10 +843,28 @@ type ReadCodexAgentInventory = LegacyCodexAgentInventory | CodexAgentInventory; export type CodexAgentOwnership = 'absent' | 'user-owned' | 'managed-clean' | 'managed-modified'; +/** + * Human-facing delivery state of one role-agent file, distinct from the + * mutation-authority {@link CodexAgentOwnership}. This is the vocabulary doctor + * and the ownership report speak so an operator can tell a healthy managed role + * from one that is merely adoptable, stale, or a collision Genie will not touch: + * + * - `managed` — inventory-owned, clean, byte-identical to the current profile. + * - `stale` — inventory-owned and clean, but an older profile than the current delivery. + * - `adoptable-historical`— no inventory, yet an exact frozen historical profile; adoptable under committed consent. + * - `collision` — a genie-named file that looks managed (modified-managed, sentinel lookalike, + * symlink, or other non-regular) but is NOT an exact match; reported, never touched. + * - `personal` — a genie-named file with no Genie provenance signal; the user's own, left untouched. + * - `absent` — recorded in inventory but no longer on disk. + */ +export type CodexAgentDisplayState = 'managed' | 'stale' | 'adoptable-historical' | 'collision' | 'personal' | 'absent'; + export interface CodexAgentOwnershipEntry { name: string; path: string; ownership: CodexAgentOwnership; + /** Read-only delivery state for inventory/doctor output (managed/adoptable/collision/stale/personal). */ + state: CodexAgentDisplayState; /** Live physical identity when the entry is a regular file; drives identity-bound uninstall. */ identity?: RegularRoleFileIdentity; } @@ -690,6 +873,10 @@ export interface CodexAgentOwnershipReport { inventoryPath: string; status: 'missing' | 'valid' | 'corrupt'; entries: CodexAgentOwnershipEntry[]; + /** The exact number of role agents the current Genie bundle delivers (coverage denominator). */ + expectedDeliveredTotal: number; + /** The current canonical reviewer profile digest, surfaced so operators can spot a stale reviewer. */ + reviewerDigest: string; error?: string; } @@ -791,6 +978,61 @@ function classifyCodexAgentFile( return roleFileIdentityEquals(actual, safeLegacyIdentity) ? 'managed-clean' : 'managed-modified'; } +/** Read the head of a role file to detect the managed sentinel without a full re-hash. */ +function roleFileHasManagedSentinel(path: string): boolean { + try { + const stat = lstatSync(path); + if (!stat.isFile() || stat.isSymbolicLink()) return false; + const fd = openSync(path, 'r'); + try { + const buffer = Buffer.allocUnsafe(CODEX_AGENT_SENTINEL.length); + const read = readSync(fd, buffer, 0, buffer.length, 0); + return buffer.subarray(0, read).toString('utf8') === CODEX_AGENT_SENTINEL; + } finally { + closeSync(fd); + } + } catch { + return false; + } +} + +/** + * Read-only display classification of one role file (never mutates, never reads + * inventory a second time). This is the reporting analog of the mutation-authority + * {@link classifyCodexAgentFile}: it maps an entry to the operator-facing + * {@link CodexAgentDisplayState}. Adoptability is a pure identity fact here — the + * committed-consent gate lives in {@link installCodexAgents}, so doctor can show + * "adoptable-historical" without implying anything was (or will be) written. + */ +function classifyCodexAgentDisplayState( + path: string, + name: string, + recorded: RecordedCodexAgent | undefined, + historical: ReadonlySet, + canonicalDigests: Readonly>, +): CodexAgentDisplayState { + const actual = physicalRoleFileIdentity(path); + if (actual.kind === 'absent') return 'absent'; + if (recorded !== undefined) { + const ownership = classifyCodexAgentFile(path, recorded); + if (ownership === 'absent') return 'absent'; + if (ownership === 'managed-modified') return 'collision'; + // managed-clean: current if it matches the delivered profile, otherwise stale. + const canonical = canonicalDigests[name]; + if (actual.kind === 'regular' && canonical !== undefined && actual.digest !== canonical) return 'stale'; + return 'managed'; + } + // No inventory record. Bytes never grant ownership, so the most a file can earn + // is "adoptable" (exact frozen historical identity) — everything else is left + // exactly where it is and only classified for reporting. + if (actual.kind === 'regular') { + if (historical.has(codexRoleAgentTupleKey(name, actual.mode, actual.digest))) return 'adoptable-historical'; + return roleFileHasManagedSentinel(path) ? 'collision' : 'personal'; + } + // Symlink / directory / other / unreadable at a role path: never safe to act on. + return 'collision'; +} + /** Shared digest-backed classifier for setup/update, doctor, and uninstall. */ export function inspectCodexAgentOwnership(codexHome = getCodexHome()): CodexAgentOwnershipReport { const state = readCodexAgentInventory(codexHome); @@ -799,10 +1041,13 @@ export function inspectCodexAgentOwnership(codexHome = getCodexHome()): CodexAge if (existsSync(agentsDir)) { for (const name of readdirSync(agentsDir)) if (CODEX_AGENT_NAME_RE.test(name)) names.add(name); } + const historical = loadHistoricalRoleAgentTupleKeys(); return { inventoryPath: inventoryPath(codexHome), status: state.status, error: state.error, + expectedDeliveredTotal: EXPECTED_CODEX_ROLE_AGENT_TOTAL, + reviewerDigest: CANONICAL_CODEX_REVIEWER_DIGEST, entries: [...names].sort().map((name) => { const path = join(agentsDir, name); // Surface the same physical identity the classifier reads so the uninstall @@ -812,6 +1057,13 @@ export function inspectCodexAgentOwnership(codexHome = getCodexHome()): CodexAge name, path, ownership: classifyCodexAgentFile(path, state.inventory.files[name]), + state: classifyCodexAgentDisplayState( + path, + name, + state.inventory.files[name], + historical, + CANONICAL_CODEX_ROLE_AGENT_DIGESTS, + ), ...(identity.kind === 'regular' ? { identity } : {}), }; }), @@ -1605,6 +1857,49 @@ interface CodexAgentInstallPlan { writes: Map; removals: string[]; expected: Map; + /** Whether committed Codex consent has unlocked adoption of exact frozen historical profiles. */ + adopt: boolean; + /** Frozen historical allowlist keys (`name\0mode\0digest`) recognized for adoption. */ + historical: ReadonlySet; +} + +/** Role-agent convergence policy inputs, distinct from failure-injection {@link CodexAgentTransactionOptions}. */ +export interface CodexRoleAgentConvergenceOptions { + /** + * Adopt an exact frozen historical profile found on disk without inventory. + * Adoption is a privileged claim on a previously user-owned file, so it defaults + * OFF and callers pass `true` only after committed Codex consent + * ({@link codexRoleAdoptionAllowed}). An exact allowlist match is still required. + */ + adoptHistorical?: boolean; + /** Frozen historical allowlist keys; defaults to the shipped fixture. Injectable for deterministic tests. */ + historicalTupleKeys?: ReadonlySet; +} + +/** + * Resolve the ownership record convergence should treat a role file under. A live + * inventory record always wins. Otherwise, ONLY when committed Codex consent has + * unlocked adoption AND the file is an exact frozen historical profile (name + + * regular type + mode + digest), adopt it: record its current on-disk identity as + * if already managed so the ordinary convergence refreshes a stale profile to the + * delivered one and writes the inventory inside the same backup-first transaction. + * Bytes never grant ownership, so any other inventory-free file stays user-owned. + */ +function resolveCodexRoleAgentRecord( + targetPath: string, + name: string, + plan: CodexAgentInstallPlan, +): RecordedCodexAgent | undefined { + const recorded = plan.recorded.files[name]; + if (recorded !== undefined) return recorded; + if (!plan.adopt) return undefined; + const live = physicalRoleFileIdentity(targetPath); + if (live.kind !== 'regular' || !plan.historical.has(codexRoleAgentTupleKey(name, live.mode, live.digest))) { + return undefined; + } + plan.result.adoptedLegacy = plan.result.adoptedLegacy ?? []; + plan.result.adoptedLegacy.push(name); + return { identity: live }; } function collectCurrentCodexAgentPayloads( @@ -1624,7 +1919,7 @@ function collectCurrentCodexAgentPayloads( if (sourceIdentity.kind !== 'regular') { throw new Error(`Codex role-agent source is not a regular readable file: ${sourcePath}`); } - const recorded = plan.recorded.files[name]; + const recorded = resolveCodexRoleAgentRecord(targetPath, name, plan); const ownership = classifyCodexAgentFile(targetPath, recorded, sourceIdentity); if (ownership === 'user-owned') { // Bytes are not an ownership capability. An inventory-free file remains @@ -1681,6 +1976,7 @@ export function installCodexAgents( bundleRoot: string, codexHome = getCodexHome(), transactionOptions: CodexAgentTransactionOptions = {}, + convergence: CodexRoleAgentConvergenceOptions = {}, ): CodexAgentInstallResult { const source = join(bundleRoot, 'plugins', 'genie', 'codex-agents'); if (!existsSync(source)) throw new Error(`Codex agents are missing from bundle: ${source}`); @@ -1707,6 +2003,8 @@ export function installCodexAgents( writes: new Map(), removals: [], expected: new Map(), + adopt: convergence.adoptHistorical === true, + historical: convergence.historicalTupleKeys ?? loadHistoricalRoleAgentTupleKeys(), }; const sourceNames = readdirSync(source) .filter((name) => CODEX_AGENT_NAME_RE.test(name)) @@ -1731,6 +2029,32 @@ export function installCodexAgents( return plan.result; } +export interface ConvergeSetupCodexRoleAgentsOptions { + codexHome?: string; + genieHome?: string; +} + +/** + * Create setup's narrow role-convergence consumer. The authenticated root is + * supplied only when CodexActivationStore admits this opaque capability; exact + * historical profiles are adopted only after setup has durably committed + * explicit Codex consent. + */ +export function createSetupCodexRoleAgentConsumer( + options: ConvergeSetupCodexRoleAgentsOptions, +): DeliveryRootConsumer { + return createDeliveryRootConsumer((physicalRoot) => { + const genieHome = options.genieHome ?? resolveGenieHome(); + const consent = readIntegrationConsentState(genieHome); + if (!codexRoleAdoptionAllowed(consent)) { + throw new IntegrationCommandError( + 'Codex role-agent convergence requires committed explicit Codex integration consent', + ); + } + return installCodexAgents(physicalRoot, options.codexHome, {}, { adoptHistorical: true }); + }); +} + export interface CodexEnabledMutationResult { ok: boolean; detail: string; @@ -1922,6 +2246,27 @@ function addCodexMarketplace(runner: CommandRunner, command: string, bundleRoot: } } +export interface RegisterCodexMarketplaceOptions { + /** Once-bound absolute Codex executable. */ + command: string; + runner?: CommandRunner; + timeoutMs?: number; +} + +/** + * Build a one-operation capability that can register only the exact physical + * delivery root admitted by the activation store. The root is never exposed to + * setup/executor callers and cannot be rebound through GENIE_HOME resolution. + */ +export function createCodexMarketplaceRegistrationConsumer( + options: RegisterCodexMarketplaceOptions, +): DeliveryRootConsumer { + const timeoutMs = boundedPositiveInteger('timeout', options.timeoutMs ?? INTEGRATION_TIMEOUT_MS, 5 * 60_000); + return createDeliveryRootConsumer((physicalRoot) => + addCodexMarketplace(options.runner ?? defaultRunner, options.command, physicalRoot, timeoutMs), + ); +} + function requireCodexPluginState(raw: string, phase: string): RuntimePluginState { const parsed = parseCodexPluginState(raw); if (!parsed.ok) throw new IntegrationCommandError(`${parsed.detail} ${phase}`); @@ -2766,10 +3111,18 @@ function bindActiveRootToCanonicalCache(activePluginRoot: string, codexHome: str /** * Reject health BEFORE any retirement unless the single snapshot is exactly one - * enabled target-version plugin with a usable launcher, a canonically-verified - * installed payload and exact inventory, and a bounded MCP session that - * completes initialize / tools-list (all five tools) / read-only wish_status. - * Returns a frozen {@link CodexHealthProof} whose payload feeds retirement. + * enabled target-version plugin with a proven active cache root, a + * canonically-verified installed payload and exact inventory, and a bounded MCP + * session that completes initialize / tools-list (all five tools) / read-only + * wish_status. Returns a frozen {@link CodexHealthProof} whose payload feeds + * retirement. + * + * Plugin health is deliberately DECOUPLED from Codex-MCP-route usability + * (`snapshot.usable`). The plugin no longer registers a Codex MCP route — that + * path is the marker-owned project `.codex/config.toml` reconciled by `genie + * init`. A healthy plugin provides skills + hooks + the retained Claude + * `mcp-launcher.cjs`; the launcher's ability to spawn and speak MCP is proven by + * the bounded session below, not by a plugin-declared MCP registration. */ export function proveCodexPluginHealth(options: ProveCodexPluginHealthOptions): CodexHealthProof { const { snapshot } = options; @@ -2784,7 +3137,9 @@ export function proveCodexPluginHealth(options: ProveCodexPluginHealthOptions): if (snapshot.activePluginRoot === undefined) { rejectHealth(`active plugin root was not proven by the snapshot: ${snapshot.usabilityDetail ?? snapshot.detail}`); } - if (snapshot.usable !== true) rejectHealth(snapshot.usabilityDetail ?? 'plugin MCP launcher is not usable'); + // NOTE: `snapshot.usable` (Codex-MCP-route usability) is intentionally NOT a + // health gate. The plugin ships no Codex MCP route; the launcher's health is + // proven by the bounded MCP session below. const activePluginRoot = snapshot.activePluginRoot; // Bind the consumed tree to the canonical cache BEFORE any digesting or MCP @@ -2802,7 +3157,10 @@ export function proveCodexPluginHealth(options: ProveCodexPluginHealthOptions): // A7: the session runs through the snapshot's own proven launcher (derived // from the single activePluginRoot, not a re-read), in an isolated throwaway - // cwd so a missing db degrades to an empty board without touching real state. + // cwd that is not a project. The fail-closed MCP server there returns a typed + // "no project context" result rather than a fabricated empty board; the health + // session accepts that as read-only-healthy — the launcher spawning and + // speaking MCP IS the signal, an absent board there is expected. const runSession = options.runSession ?? runBoundedCodexMcpSession; const sessionCwd = mkdtempSync(join(tmpdir(), 'genie-mcp-health-')); let session: McpSessionResult; @@ -2906,22 +3264,41 @@ export function translateRetirementConflicts(step: () => T): T { } } -export interface CodexPluginOnlyDeps { - converge?: (options: ConvergePluginOptions) => IntegrationResult | null; +/** Narrow proof→retirement boundary shared by setup and the legacy orchestrator. */ +export interface CodexFallbackRetirementDeps { probe?: (deps: CodexPluginProbeDeps) => CodexPluginProbe; prove?: (options: ProveCodexPluginHealthOptions) => CodexHealthProof; recover?: (fallbackSkillsDir: string) => CodexFallbackRetirementResult[]; plan?: (options: PlanCodexFallbackRetirementOptions) => CodexFallbackRetirementPlan; apply?: (plan: CodexFallbackRetirementPlan) => CodexFallbackRetirementResult; - installAgents?: (bundleRoot: string, codexHome?: string) => CodexAgentInstallResult; runSession?: (options: BoundedCodexMcpSessionOptions) => McpSessionResult; /** Live Codex user-skills tier; defaults to resolveAgentsSkillsDir() (env-isolated in tests). */ fallbackSkillsDir?: string; probeCwd?: string; } +export interface CodexPluginOnlyDeps extends CodexFallbackRetirementDeps { + converge?: (options: ConvergePluginOptions) => IntegrationResult | null; + installAgents?: (bundleRoot: string, codexHome?: string) => CodexAgentInstallResult; + /** + * Explicit adoption override for tests. When omitted, adoption is derived from + * committed Codex consent read from `options.genieHome` (adoption stays OFF when + * no genieHome is supplied). Only the DEFAULT role-agent installer honors this; + * an injected `installAgents` seam is called with its original 2-arg signature. + */ + adoptHistoricalRoleAgents?: boolean; + /** Consent reader seam; defaults to {@link readIntegrationConsentState}. */ + readConsentState?: (genieHome: string) => IntegrationConsentState; +} + export interface ConvergeCodexPluginOnlyOptions extends ConvergePluginOptions { deps?: CodexPluginOnlyDeps; + /** + * GENIE_HOME used to read committed Codex consent for role-agent adoption. When + * present and consent is a committed `codex`/`all` selection, exact frozen + * historical profiles are adopted during convergence; otherwise adoption is off. + */ + genieHome?: string; } export interface CodexPluginOnlyResult { @@ -2939,6 +3316,116 @@ function fallbackDirPresent(fallbackSkillsDir: string): boolean { return existsSync(fallbackSkillsDir); } +export type SetupCodexFallbackRetirementResult = + | { + status: 'verified'; + retired: readonly string[]; + preservedCollisions: number; + preservedUnrecognized: number; + } + | { + /** A deliberate disabled state is healthy for setup, but never authorizes fallback mutation. */ + status: 'skipped-disabled'; + retired: readonly []; + preservedCollisions: 0; + preservedUnrecognized: 0; + }; + +export interface SetupCodexFallbackRetirementOptions { + /** Once-bound absolute Codex executable used for the fresh health snapshot. */ + command: string; + /** Exact authenticated generation admitted by the activation store. */ + expectedVersion: string; + codexHome?: string; + verifyCodexPayload?: CodexPayloadVerifier; + deps?: CodexFallbackRetirementDeps; +} + +interface ProvenFallbackRetirementResult { + retired: readonly string[]; + preservedCollisions: number; + preservedUnrecognized: number; +} + +/** + * Retire only fallbacks admitted by one immutable plugin-health proof. Recovery + * is the first fallback mutation; callers must obtain the proof before entering + * this helper. + */ +function retireProvenCodexFallbacks( + proof: CodexHealthProof, + fallbackSkillsDir: string, + deps: Pick, +): ProvenFallbackRetirementResult { + if (!fallbackDirPresent(fallbackSkillsDir)) { + return { retired: [], preservedCollisions: 0, preservedUnrecognized: 0 }; + } + + translateRetirementConflicts(() => (deps.recover ?? recoverCodexFallbackRetirements)(fallbackSkillsDir)); + const plan = (deps.plan ?? planCodexFallbackRetirement)({ + fallbackSkillsDir, + skillNames: proof.skillInventory, + verifiedTargets: proof.payload, + }); + // An absent canonical skill is a no-op, not a personal collision. A + // well-formed but unrecognized managed tree is reported separately. + const preservedCollisions = plan.preserved.filter( + (entry) => entry.reason !== 'missing' && entry.reason !== 'ambiguous-ownership', + ).length; + const preservedUnrecognized = plan.preserved.filter((entry) => entry.reason === 'ambiguous-ownership').length; + // A zero-accepted plan must not create an empty quarantine transaction. + const retired = + plan.accepted.length === 0 + ? [] + : translateRetirementConflicts(() => (deps.apply ?? applyCodexFallbackRetirement)(plan)).retired; + return { retired, preservedCollisions, preservedUnrecognized }; +} + +/** + * Setup-only fallback-retirement capability. The delivery root is supplied by + * CodexActivationStore only after its matching-evidence and physical-root + * checks. The consumer then takes one fresh plugin snapshot: + * + * - exact-version disabled → preserve the operator choice and every fallback; + * - anything else unhealthy/non-current → fail before fallback recovery; + * - exact-version enabled + full payload/MCP proof → retire only proven-owned + * historical fallbacks. + */ +export function createSetupCodexFallbackRetirementConsumer( + options: SetupCodexFallbackRetirementOptions, +): DeliveryRootConsumer { + return createDeliveryRootConsumer((physicalRoot) => { + const deps = options.deps ?? {}; + const codexHome = options.codexHome ?? getCodexHome(); + const snapshot = (deps.probe ?? probeCodexGeniePlugin)({ + which: () => options.command, + codexHome, + cwd: deps.probeCwd ?? process.cwd(), + }); + if ( + snapshot.status === 'ok' && + snapshot.installed && + snapshot.version === options.expectedVersion && + snapshot.enabled === false + ) { + return { status: 'skipped-disabled', retired: [], preservedCollisions: 0, preservedUnrecognized: 0 }; + } + + const proof = (deps.prove ?? proveCodexPluginHealth)({ + snapshot, + bundleRoot: physicalRoot, + codexHome, + expectedVersion: options.expectedVersion, + verifyCodexPayload: options.verifyCodexPayload, + runSession: deps.runSession, + }); + return { + status: 'verified', + ...retireProvenCodexFallbacks(proof, deps.fallbackSkillsDir ?? resolveAgentsSkillsDir(), deps), + }; + }); +} + /** * The shared plugin-only convergence orchestrator (R1). Exact order: converge * the plugin (honoring deliberate disablement) → take EXACTLY ONE @@ -2954,7 +3441,14 @@ export function convergeCodexPluginOnly(options: ConvergeCodexPluginOnlyOptions) const deps = options.deps ?? {}; const codexHome = options.codexHome ?? getCodexHome(); const fallbackSkillsDir = deps.fallbackSkillsDir ?? resolveAgentsSkillsDir(); - const installAgents = deps.installAgents ?? installCodexAgents; + // Adoption is off unless committed Codex consent explicitly unlocks it. An + // injected installAgents seam keeps its original 2-arg signature (ordering + // tests are unaffected); only the default installer receives the adoption flag. + const adopt = resolveConvergenceRoleAdoption(options, deps); + const runInstallAgents = (): CodexAgentInstallResult => + deps.installAgents + ? deps.installAgents(options.bundleRoot, codexHome) + : installCodexAgents(options.bundleRoot, codexHome, {}, { adoptHistorical: adopt }); const plugin = (deps.converge ?? convergeCodexPlugin)(options); if (plugin === null) return null; @@ -2972,7 +3466,10 @@ export function convergeCodexPluginOnly(options: ConvergeCodexPluginOnlyOptions) const snapshot = (deps.probe ?? probeCodexGeniePlugin)({ codexHome, cwd: deps.probeCwd ?? process.cwd() }); if (plugin.preservedDisabled === true) { - const agents = installAgents(options.bundleRoot, codexHome); + // R3: a deliberately disabled plugin still installs the fallback role TOMLs + // (before any health proof) — adoption of exact historical profiles applies + // here too, since role agents are CLI-owned and independent of plugin health. + const agents = runInstallAgents(); return { result: { ...plugin, snapshot }, proof: null, @@ -2992,48 +3489,35 @@ export function convergeCodexPluginOnly(options: ConvergeCodexPluginOnlyOptions) runSession: deps.runSession, }); - let retired: readonly string[] = []; - let preservedCollisions = 0; - let preservedUnrecognized = 0; - if (fallbackDirPresent(fallbackSkillsDir)) { - translateRetirementConflicts(() => (deps.recover ?? recoverCodexFallbackRetirements)(fallbackSkillsDir)); - const plan = (deps.plan ?? planCodexFallbackRetirement)({ - fallbackSkillsDir, - skillNames: proof.skillInventory, - verifiedTargets: proof.payload, - }); - // planCodexFallbackRetirement records an absent canonical skill as a - // {accepted:false, reason:'missing'} preserved entry, so after a full - // migration every subsequent run would otherwise report a phantom - // "preserved 23 personal collision(s)". Count only real on-disk collisions. - // 'ambiguous-ownership' is a well-formed genie marker whose (skillName, - // digest) is not yet in the frozen allowlist and has no matching verified - // target — that is unrecognized managed content, not a personal collision - // (the tree was never modified by the user), so it is counted and reported - // separately. - preservedCollisions = plan.preserved.filter( - (entry) => entry.reason !== 'missing' && entry.reason !== 'ambiguous-ownership', - ).length; - preservedUnrecognized = plan.preserved.filter((entry) => entry.reason === 'ambiguous-ownership').length; - // A11: a zero-accepted plan is a true no-op — never open a fresh transaction - // so a plugin-only second run cannot accumulate empty quarantine journals. - if (plan.accepted.length > 0) { - const retirement = translateRetirementConflicts(() => (deps.apply ?? applyCodexFallbackRetirement)(plan)); - retired = retirement.retired; - } - } - - const agents = installAgents(options.bundleRoot, codexHome); + const retirement = retireProvenCodexFallbacks(proof, fallbackSkillsDir, deps); + + const agents = runInstallAgents(); return { - result: { ...plugin, snapshot, retiredFallbacks: retired, preservedCollisions, preservedUnrecognized }, + result: { + ...plugin, + snapshot, + retiredFallbacks: retirement.retired, + preservedCollisions: retirement.preservedCollisions, + preservedUnrecognized: retirement.preservedUnrecognized, + }, proof, agents, - retired, - preservedCollisions, - preservedUnrecognized, + ...retirement, }; } +/** + * Resolve whether this convergence adopts exact frozen historical role profiles. + * An explicit deps override wins (tests); otherwise adoption is derived from + * committed Codex consent read from `genieHome`, and stays OFF when no genieHome + * is supplied so direct/unwired callers never silently claim user files. + */ +function resolveConvergenceRoleAdoption(options: ConvergeCodexPluginOnlyOptions, deps: CodexPluginOnlyDeps): boolean { + if (deps.adoptHistoricalRoleAgents !== undefined) return deps.adoptHistoricalRoleAgents; + if (options.genieHome === undefined) return false; + return codexRoleAdoptionAllowed((deps.readConsentState ?? readIntegrationConsentState)(options.genieHome)); +} + function emptyAgentInstallResult(): CodexAgentInstallResult { return { installed: 0, skippedUserOwned: [], keptModified: [], removed: [], backedUp: [] }; } @@ -3046,7 +3530,7 @@ function emptyAgentInstallResult(): CodexAgentInstallResult { * whether {@link planCodexFallbackRetirement} will actually accept it, which * additionally requires the exact `identityVersion: 2` marker tag AND either a * frozen-allowlist historical tuple or a live-plugin verified-target digest - * match. Reporting every structurally clean tree as "clean, run `genie update`" + * match. Reporting every structurally clean tree as "clean, run setup" * therefore lies to any tree the planner would refuse (an infinite no-op loop: * PR #2575's `ambiguous-ownership` case, and any pre-`identityVersion` era-A * marker). This inspector applies the SAME two-stage gate the planner uses — @@ -3056,13 +3540,14 @@ function emptyAgentInstallResult(): CodexAgentInstallResult { * plugin MCP) — so doctor never promises a retirement the engine then refuses. * * - `cleanFallbacks` — recognized (historical-tuple or verified-target) genie - * skill dirs still in the user tier; `genie update` retires these. + * skill dirs still in the user tier; authenticated `genie setup --codex` + * retires these after plugin health succeeds. * - `unrecognizedFallbacks` — structurally well-formed identityVersion:2, * self-consistent genie-managed dirs whose content the planner does NOT * recognize (not in the frozen allowlist, no matching live-plugin payload). * Era-A dirs (v1 legacy digest, no identityVersion) fail v2 self-consistency * and land in `preservedCollisions` instead. Never user-modified, but not - * retirable either — manual review, not `genie update`, resolves these + * retirable either — manual review, not setup, resolves these * (#2575 vocabulary). * - `preservedCollisions` — managed-modified / corrupt-metadata dirs; personal * content preserved in place that only manual review resolves. @@ -3227,7 +3712,7 @@ function describeCodexIntegration( // Distinct from a personal collision: the marker is well-formed genie // provenance, but its (skillName, digest) is not in the frozen allowlist and // has no matching verified target — an unrecognized managed version/content, - // not user-modified content. Manual review, not `genie update`, resolves it. + // not user-modified content. Manual review, not automated retirement, resolves it. if (unrecognized > 0) notes.push( `preserved ${unrecognized} unrecognized managed fallback${unrecognized === 1 ? '' : 's'} (review manually)`, @@ -3260,6 +3745,9 @@ function installCodexIntegration( codexHome, verifyCodexPayload, deps, + // stateDir is GENIE_HOME on every production install/setup path; role-agent + // adoption reads committed Codex consent from it (off unless committed codex/all). + genieHome: stateDir, }); if (outcome === null) throw new Error('Codex plugin convergence returned no result for explicit install'); const plugin = outcome.result; diff --git a/src/lib/trusted-executable.test.ts b/src/lib/trusted-executable.test.ts new file mode 100644 index 000000000..39f0f07db --- /dev/null +++ b/src/lib/trusted-executable.test.ts @@ -0,0 +1,74 @@ +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { execFileSync } from 'node:child_process'; +import { chmodSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { validateTrustedExecutablePath } from './trusted-executable.js'; + +let home: string; + +function fakeExecutable(path: string): string { + mkdirSync(join(path, '..'), { recursive: true }); + writeFileSync(path, '#!/bin/sh\nexit 0\n'); + chmodSync(path, 0o755); + return path; +} + +function gitInit(dir: string): void { + mkdirSync(dir, { recursive: true }); + execFileSync('git', ['init', '-q'], { cwd: dir }); +} + +beforeEach(() => { + home = mkdtempSync(join(tmpdir(), 'trusted-exe-home-')); +}); + +afterEach(() => { + rmSync(home, { recursive: true, force: true }); +}); + +describe('validateTrustedExecutablePath — home containment is not a trust signal (live-QA regression)', () => { + test('a user-level CLI under ~/.local/bin is accepted when the CWD IS the home directory', () => { + // The 2026-07-23 dogfood failure: `genie update` run from $HOME refused + // /home//.local/bin/claude as "repository-local" because the bare CWD + // containment rule swallowed the whole home tree. + const claude = fakeExecutable(join(home, '.local', 'bin', 'claude')); + expect(validateTrustedExecutablePath('claude CLI', claude, home, process.platform, home)).toBe( + realpathSync(claude), + ); + }); + + test('a dotfiles git repository AT the home directory does not ban user-level CLIs', () => { + gitInit(home); + const claude = fakeExecutable(join(home, '.local', 'bin', 'claude')); + expect(validateTrustedExecutablePath('claude CLI', claude, home, process.platform, home)).toBe( + realpathSync(claude), + ); + }); + + test('a repository-local executable in a project BELOW home stays refused', () => { + const project = join(home, 'workspace', 'proj'); + gitInit(project); + const evil = fakeExecutable(join(project, 'bin', 'claude')); + expect(() => validateTrustedExecutablePath('claude CLI', evil, project, process.platform, home)).toThrow( + /repository-local/, + ); + }); + + test('a CWD-local executable in a plain (non-git) project directory stays refused', () => { + const scratch = join(home, 'downloads', 'unpacked'); + const evil = fakeExecutable(join(scratch, 'claude')); + expect(() => validateTrustedExecutablePath('claude CLI', evil, scratch, process.platform, home)).toThrow( + /repository-local/, + ); + }); + + test('from inside a project, a user-level CLI outside the project is accepted', () => { + const project = join(home, 'workspace', 'proj'); + gitInit(project); + const claude = fakeExecutable(join(home, '.local', 'bin', 'claude')); + expect(validateTrustedExecutablePath('claude CLI', claude, project, process.platform, home)).toBe( + realpathSync(claude), + ); + }); +}); diff --git a/src/lib/trusted-executable.ts b/src/lib/trusted-executable.ts index 1922542a5..4f8d5a590 100644 --- a/src/lib/trusted-executable.ts +++ b/src/lib/trusted-executable.ts @@ -1,4 +1,5 @@ import { constants, accessSync, lstatSync, readFileSync, realpathSync, statSync } from 'node:fs'; +import { homedir } from 'node:os'; import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'; const MAX_GIT_POINTER_BYTES = 4 * 1024; @@ -14,6 +15,15 @@ function normalizeComparablePath(path: string): string { } } +/** Canonical (physical) form with a logical fallback for unresolvable paths. */ +function safeCanonical(path: string): string { + try { + return realpathSync(path); + } catch { + return resolve(path); + } +} + function isSameOrContainedPath(root: string, candidate: string): boolean { const normalizedRoot = normalizeComparablePath(root); const normalizedCandidate = normalizeComparablePath(candidate); @@ -107,19 +117,31 @@ export function resolveEnclosingGitTrustRoots(cwd: string): string[] { return [...roots]; } -/** Validate an already-selected absolute executable against repository trust roots. */ +/** + * Validate an already-selected absolute executable against repository trust + * roots. The threat is a repository-local binary in an untrusted checkout — so + * a forbidden root that IS the user's home directory (or an ancestor of it, + * e.g. a dotfiles repo at `~`, or simply running from `$HOME`) is excluded: + * "contained in $HOME" bans every user-installed CLI (`~/.local/bin/claude`, + * `~/.bun/bin/...`) while providing no isolation, since effectively everything + * user-owned lives under home. Real project roots BELOW home stay forbidden. + */ export function validateTrustedExecutablePath( name: string, candidate: string, childCwd: string, platform: NodeJS.Platform = process.platform, + home: string = homedir(), ): string { if (!isAbsolute(candidate)) throw new Error(`${name} did not resolve to an absolute executable`); const canonicalCwd = realpathSync(childCwd); const selectedPath = resolve(candidate); const canonical = realpathSync(candidate); - const forbiddenRoots = new Set([canonicalCwd, ...resolveEnclosingGitTrustRoots(canonicalCwd)]); + const canonicalHome = safeCanonical(home); + const forbiddenRoots = [...new Set([canonicalCwd, ...resolveEnclosingGitTrustRoots(canonicalCwd)])].filter( + (root) => !isSameOrContainedPath(root, canonicalHome), + ); for (const root of forbiddenRoots) { if (isSameOrContainedPath(root, selectedPath) || isSameOrContainedPath(root, canonical)) { throw new Error(`Refusing repository-local ${name} executable: ${candidate}`); diff --git a/src/lib/v5/genie-db.ts b/src/lib/v5/genie-db.ts index e584be243..4a8370dc2 100644 --- a/src/lib/v5/genie-db.ts +++ b/src/lib/v5/genie-db.ts @@ -14,15 +14,16 @@ */ import type { Database } from 'bun:sqlite'; -import { execSync } from 'node:child_process'; -import { existsSync } from 'node:fs'; -import { dirname, join } from 'node:path'; +import { execFileSync, execSync } from 'node:child_process'; +import { existsSync, lstatSync, realpathSync } from 'node:fs'; +import { basename, dirname, join, normalize } from 'node:path'; import { openSqlite } from './sqlite-open.js'; // Concurrency + typed-error primitives now live in sqlite-open.ts (shared with // the global DB). Re-exported here so existing importers of ./genie-db keep // working — this stays the public surface for the per-repo database. export { + /** @public Backward-compatible per-repo database facade export. */ BUSY_TIMEOUT_MS, BusyDbError, ForeignDbError, @@ -76,6 +77,304 @@ export function resolveDbPath(cwd?: string): string { return join(resolveRepoRoot(cwd), '.genie', 'genie.db'); } +// ============================================================================ +// Fail-closed project context (Group A: no outer/cache-root empty-board masquerade) +// ============================================================================ + +/** + * The typed states a project surface resolves BEFORE any MCP tool can serialize + * an empty board. A non-`ok` kind MUST surface as a structured error, never a + * healthy-looking empty projection. Bare/submodule/external-git-dir layouts are + * `unsupported-project-layout`; they never fall outward or to a plugin cache. + */ +export type ProjectContextKind = + | 'ok' + | 'project-context-unavailable' + | 'project-database-unavailable' + | 'unsupported-project-layout'; + +interface PhysicalFileIdentity { + device: string; + inode: string; +} + +/** + * A fail-closed binding between the repository's one logical database path and + * the physical regular file validated there. MCP opens this exact binding; it + * never recomputes a candidate from cwd after project-context validation. + */ +export interface ProjectDatabaseBinding { + kind: 'validated-project-database'; + logicalPath: string; + physicalPath: string; + directoryIdentity: PhysicalFileIdentity; + databaseIdentity: PhysicalFileIdentity; +} + +export type ProjectDatabaseBindingResult = + | { ok: true; binding: ProjectDatabaseBinding } + | { ok: false; detail: string }; + +export interface ProjectContextOk { + kind: 'ok'; + /** The child's observable `process.cwd()`; Genie NEVER chdir's away from it. */ + effectiveLaunchCwd: string; + /** Nearest containing worktree root; a linked worktree stays linked here. */ + worktreeConfigRoot: string; + /** Absolute `git rev-parse --git-common-dir` (the MAIN repo's `.git` for a linked worktree). */ + gitCommonDir: string; + /** `dirname(gitCommonDir)` — the repo that owns the shared genie.db. */ + genieStorageRoot: string; + /** The ONLY database candidate: `/.genie/genie.db`. */ + dbPath: string; + /** + * Exact physical regular-file binding for `dbPath`. Production resolution + * always supplies it; optionality preserves injected diagnostic fixtures, + * which the MCP server refuses rather than opening by cwd. + */ + databaseBinding?: ProjectDatabaseBinding; +} + +export interface ProjectContextError { + kind: Exclude; + /** The observable launch CWD is always known, even on failure. */ + effectiveLaunchCwd: string; + detail: string; + /** Best-effort roots when they are resolvable (e.g. database-unavailable). */ + worktreeConfigRoot?: string; + gitCommonDir?: string; + genieStorageRoot?: string; + dbPath?: string; +} + +export type ProjectContext = ProjectContextOk | ProjectContextError; + +function fileIdentity(path: string): PhysicalFileIdentity { + const stat = lstatSync(path); + return { device: String(stat.dev), inode: String(stat.ino) }; +} + +function sameIdentity(left: PhysicalFileIdentity, right: PhysicalFileIdentity): boolean { + return left.device === right.device && left.inode === right.inode; +} + +function samePhysicalPath(left: string, right: string): boolean { + return normalize(normalizeGitPath(left)) === normalize(normalizeGitPath(right)); +} + +/** + * Resolve and revalidate the physical `.genie/genie.db` boundary. + * + * Both `.genie` and `genie.db` must be direct, non-symlink filesystem entries; + * the database must be a regular file. The double identity observation catches + * replacements during resolution, while an optional expected binding fences a + * later readonly open against path substitution. + */ +export function resolveProjectDatabaseBinding( + dbPath: string, + expected?: ProjectDatabaseBinding, +): ProjectDatabaseBindingResult { + const logicalPath = normalize(dbPath); + const directoryPath = dirname(logicalPath); + try { + const directoryBefore = lstatSync(directoryPath); + if (directoryBefore.isSymbolicLink() || !directoryBefore.isDirectory()) { + return { + ok: false, + detail: `unsafe Genie database binding at ${logicalPath}: ${directoryPath} must be a physical directory, not a symlink or non-directory`, + }; + } + const databaseBefore = lstatSync(logicalPath); + if (databaseBefore.isSymbolicLink() || !databaseBefore.isFile() || databaseBefore.nlink !== 1) { + return { + ok: false, + detail: `unsafe Genie database binding at ${logicalPath}: genie.db must be a singly linked physical regular file, not a symlink, hardlink, or non-file`, + }; + } + + const physicalDirectory = normalizeGitPath(realpathSync(directoryPath)); + const expectedPhysicalDirectory = join( + normalizeGitPath(realpathSync(dirname(directoryPath))), + basename(directoryPath), + ); + if (!samePhysicalPath(physicalDirectory, expectedPhysicalDirectory)) { + return { + ok: false, + detail: `unsafe Genie database binding at ${logicalPath}: .genie resolves outside its repository storage root`, + }; + } + const physicalPath = normalizeGitPath(realpathSync(logicalPath)); + const expectedPhysicalPath = join(physicalDirectory, basename(logicalPath)); + if (!samePhysicalPath(physicalPath, expectedPhysicalPath)) { + return { + ok: false, + detail: `unsafe Genie database binding at ${logicalPath}: genie.db resolves to a physical alias`, + }; + } + + const directoryIdentity = fileIdentity(directoryPath); + const databaseIdentity = fileIdentity(logicalPath); + const databaseAfter = lstatSync(logicalPath); + const directoryBeforeIdentity = { + device: String(directoryBefore.dev), + inode: String(directoryBefore.ino), + }; + const databaseBeforeIdentity = { + device: String(databaseBefore.dev), + inode: String(databaseBefore.ino), + }; + if ( + databaseAfter.isSymbolicLink() || + !databaseAfter.isFile() || + databaseAfter.nlink !== 1 || + !sameIdentity(directoryBeforeIdentity, directoryIdentity) || + !sameIdentity(databaseBeforeIdentity, databaseIdentity) + ) { + return { + ok: false, + detail: `unsafe Genie database binding at ${logicalPath}: path identity changed during validation`, + }; + } + + const binding: ProjectDatabaseBinding = { + kind: 'validated-project-database', + logicalPath, + physicalPath, + directoryIdentity, + databaseIdentity, + }; + if ( + expected !== undefined && + (!samePhysicalPath(binding.logicalPath, expected.logicalPath) || + !samePhysicalPath(binding.physicalPath, expected.physicalPath) || + !sameIdentity(binding.directoryIdentity, expected.directoryIdentity) || + !sameIdentity(binding.databaseIdentity, expected.databaseIdentity)) + ) { + return { + ok: false, + detail: `unsafe Genie database binding at ${logicalPath}: validated path identity no longer matches`, + }; + } + return { ok: true, binding }; + } catch (error) { + const detail = error instanceof Error ? error.message : String(error); + return { ok: false, detail: `unable to validate Genie database at ${logicalPath}: ${detail}` }; + } +} + +/** Trimmed stdout of a bounded, no-shell `git` invocation, or `null` on failure. */ +function runGit(cwd: string, args: string[]): string | null { + try { + return execFileSync('git', args, { + cwd, + encoding: 'utf-8', + stdio: ['pipe', 'pipe', 'pipe'], + timeout: 5_000, + }).trim(); + } catch { + return null; + } +} + +/** A linked worktree's git dir lives under `/worktrees/`. */ +function isLinkedWorktree(gitDir: string, gitCommonDir: string): boolean { + return normalize(dirname(gitDir)) === normalize(join(gitCommonDir, 'worktrees')); +} + +/** A `.git` FILE at a non-linked worktree root marks an external/separate git dir. */ +function dotGitIsFile(worktreeRoot: string): boolean { + try { + return lstatSync(join(worktreeRoot, '.git')).isFile(); + } catch { + return false; + } +} + +/** + * Resolve the four-value production path model for `cwd` WITHOUT ever changing + * the process CWD or considering a plugin cache. Git discovery stops at the + * nearest enclosing worktree, so a nested initialized repository resolves to its + * OWN storage root and a nested repo without a Genie database fails at that + * boundary rather than walking outward to an outer database. + * + * Bare repositories, submodules, and external/separate-Git-dir layouts return + * `unsupported-project-layout` BEFORE any database lookup. A resolvable layout + * whose `/.genie/genie.db` is absent returns + * `project-database-unavailable` — never an empty board. + */ +export function resolveProjectContext(cwd: string = process.cwd()): ProjectContext { + const effectiveLaunchCwd = cwd; + // Bare vs not-a-repo: `--is-bare-repository` succeeds inside a git dir even + // where `--show-toplevel` would abort, so it cleanly separates the two. + const isBare = runGit(cwd, ['rev-parse', '--is-bare-repository']); + if (isBare === null) { + return { kind: 'project-context-unavailable', effectiveLaunchCwd, detail: `no Git worktree contains ${cwd}` }; + } + if (isBare === 'true') { + return { + kind: 'unsupported-project-layout', + effectiveLaunchCwd, + detail: 'bare Git repositories are not a supported Genie project layout', + }; + } + // A non-empty superproject working tree means cwd is inside a submodule. + const superproject = runGit(cwd, ['rev-parse', '--path-format=absolute', '--show-superproject-working-tree']); + if (superproject) { + return { + kind: 'unsupported-project-layout', + effectiveLaunchCwd, + detail: 'Git submodules are not a supported Genie project layout', + }; + } + const raw = runGit(cwd, ['rev-parse', '--path-format=absolute', '--show-toplevel', '--git-common-dir', '--git-dir']); + const [topRaw, commonRaw, gitDirRaw] = (raw ?? '').split('\n').map((line) => line.trim()); + if (!topRaw || !commonRaw || !gitDirRaw) { + return { + kind: 'project-context-unavailable', + effectiveLaunchCwd, + detail: `unable to resolve Git roots for ${cwd}`, + }; + } + const worktreeConfigRoot = normalizeGitPath(topRaw); + const gitCommonDir = normalizeGitPath(commonRaw); + const gitDir = normalizeGitPath(gitDirRaw); + // A non-linked worktree whose `.git` is a FILE points its common dir outside + // the project (`git init --separate-git-dir`); `dirname(gitCommonDir)` would + // then be an unrelated directory, so refuse it explicitly. + if (!isLinkedWorktree(gitDir, gitCommonDir) && dotGitIsFile(worktreeConfigRoot)) { + return { + kind: 'unsupported-project-layout', + effectiveLaunchCwd, + worktreeConfigRoot, + gitCommonDir, + detail: 'external/separate Git directory layouts are not a supported Genie project layout', + }; + } + const genieStorageRoot = normalizeGitPath(dirname(gitCommonDir)); + const dbPath = join(genieStorageRoot, '.genie', 'genie.db'); + const database = resolveProjectDatabaseBinding(dbPath); + if (!database.ok) { + return { + kind: 'project-database-unavailable', + effectiveLaunchCwd, + worktreeConfigRoot, + gitCommonDir, + genieStorageRoot, + dbPath, + detail: `${database.detail}; run \`genie init\` in ${genieStorageRoot}`, + }; + } + return { + kind: 'ok', + effectiveLaunchCwd, + worktreeConfigRoot, + gitCommonDir, + genieStorageRoot, + dbPath, + databaseBinding: database.binding, + }; +} + // ============================================================================ // Open / init // ============================================================================ @@ -101,17 +400,43 @@ export function openDb(opts: OpenOptions = {}): Database { }); } -/** Tables a fully-initialized `user_version = 1` DB must carry. */ -const EXPECTED_TABLES = [ - 'boards', - 'hire_roster', - 'meta', - 'stage_log', - 'task_dependencies', - 'task_events', - 'tasks', - 'wish_groups', -] as const; +/** Required table/column shape of a fully-initialized per-repo Genie database. */ +const EXPECTED_SCHEMA = { + boards: ['id', 'name', 'created_at', 'lanes'], + hire_roster: ['wish', 'agent_adapter_id', 'profile', 'worktree', 'hired_at', 'state'], + meta: ['key', 'value'], + stage_log: ['id', 'task_id', 'stage', 'note', 'created_at'], + task_dependencies: ['task_id', 'depends_on_id'], + task_events: ['id', 'task_id', 'kind', 'note', 'author_kind', 'author', 'created_at'], + tasks: [ + 'id', + 'board_id', + 'title', + 'status', + 'claimed_by', + 'claimed_at', + 'wish', + 'group_name', + 'created_at', + 'updated_at', + 'lane', + 'agent_kind', + 'heartbeat_at', + 'blocked_by', + 'blocked_reason', + ], + wish_groups: [ + 'wish', + 'name', + 'status', + 'depends_on', + 'assignee', + 'started_at', + 'completed_at', + 'created_at', + 'updated_at', + ], +} as const; /** * True when the DB is already at the current schema — every expected table plus @@ -130,17 +455,29 @@ function schemaIsCurrent(db: Database): boolean { }> ).map((r) => r.name), ); - for (const t of EXPECTED_TABLES) if (!tables.has(t)) return false; - const taskCols = new Set((db.query('PRAGMA table_info(tasks)').all() as Array<{ name: string }>).map((c) => c.name)); - if (!taskCols.has('wish') || !taskCols.has('group_name') || !taskCols.has('lane')) return false; - // Runtime layer (additive-nullable): identity, heartbeat liveness, enforced block. - for (const c of ['agent_kind', 'heartbeat_at', 'blocked_by', 'blocked_reason']) { - if (!taskCols.has(c)) return false; + for (const [table, expectedColumns] of Object.entries(EXPECTED_SCHEMA)) { + if (!tables.has(table)) return false; + const columns = new Set( + (db.query(`PRAGMA table_info(${table})`).all() as Array<{ name: string }>).map((column) => column.name), + ); + for (const column of expectedColumns) { + if (!columns.has(column)) return false; + } } - const boardCols = new Set( - (db.query('PRAGMA table_info(boards)').all() as Array<{ name: string }>).map((c) => c.name), - ); - return boardCols.has('lanes'); + return true; +} + +/** + * Validate a read-only handle without applying migrations or taking a write + * lock. MCP uses this before exposing a database to any tool handler: both the + * per-repo user_version and the complete required schema must match this build. + * + * Malformed SQLite inputs may throw while being inspected; callers own closing + * the handle and translating that failure at their boundary. + */ +export function isCurrentGenieDb(db: Database): boolean { + const row = db.query('PRAGMA user_version').get() as { user_version: number } | null; + return row?.user_version === CURRENT_SCHEMA_VERSION && schemaIsCurrent(db); } const SCHEMA_SQL = ` diff --git a/src/lib/v5/global-db.ts b/src/lib/v5/global-db.ts index 8883f3e19..49601dc62 100644 --- a/src/lib/v5/global-db.ts +++ b/src/lib/v5/global-db.ts @@ -23,7 +23,16 @@ import { openSqlite } from './sqlite-open.js'; // The typed error taxonomy is shared with the per-repo DB. Re-exported so // callers of the global DB can catch failures without importing genie-db. -export { BusyDbError, ForeignDbError, GenieDbError, isBusyError, MalformedDbError } from './sqlite-open.js'; +export { + /** @public Stable global-database error facade exports. */ + BusyDbError, + ForeignDbError, + /** @public Stable global-database error facade export. */ + GenieDbError, + /** @public Stable global-database error facade export. */ + isBusyError, + MalformedDbError, +} from './sqlite-open.js'; /** Schema revision for the GLOBAL database. Independent of the per-repo version. */ export const GLOBAL_SCHEMA_VERSION = 1; diff --git a/src/lib/v5/mcp-server.ts b/src/lib/v5/mcp-server.ts index de364814a..06337458d 100644 --- a/src/lib/v5/mcp-server.ts +++ b/src/lib/v5/mcp-server.ts @@ -20,6 +20,7 @@ import type { Database } from 'bun:sqlite'; import { createInterface } from 'node:readline'; +import type { ProjectContext, ProjectDatabaseBinding } from './genie-db.js'; import type { McpTool, ToolContext } from './mcp-tools.js'; // ============================================================================ @@ -88,11 +89,26 @@ export interface McpServerConfig { /** The tool registry surfaced by `tools/list` and dispatched by `tools/call`. */ tools: McpTool[]; /** - * Read-only DB open (net-new; returns `null` when the file is absent so the - * server degrades to an empty projection). Injected so this module never - * statically imports `bun:sqlite` / `mcp-tools`. + * Read-only DB open (net-new; returns `null` when the file is absent or cannot + * be opened). A fail-closed consumer with `resolveContext` turns null into + * `project-database-unavailable`; legacy consumers may still degrade to an + * empty projection. Injected so this module never statically imports + * `bun:sqlite` / `mcp-tools`. */ - openReadonlyDb: (cwd: string) => Database | null; + openReadonlyDb: (target: string | ProjectDatabaseBinding) => Database | null; + /** + * Optional pure read-only schema validator. The fail-closed MCP command + * supplies the per-repo Genie validator; legacy consumers may omit it. + */ + validateReadonlyDb?: (db: Database) => boolean; + /** + * OPT-IN fail-closed resolver (net-new). When provided, the loop resolves the + * project context once per `tools/call` and, on any non-`ok` kind, returns a + * typed structured error (`{ error, detail }`, `isError: true`) instead of + * dispatching the tool against an outer/cache-root empty board. Consumers that + * omit it (e.g. `genie ui-bridge`) keep the legacy cwd-based degrade-to-empty. + */ + resolveContext?: (cwd: string) => ProjectContext; /** Build the `initialize` reply from the client's declared params. */ initialize: (params: Record | undefined) => InitializeOutcome; /** @@ -109,17 +125,57 @@ export interface McpServerConfig { /** * Drive the stdio MCP server until stdin closes. Opens a NET-NEW read-only db - * handle (degrading to an empty board when the file is absent) and dispatches - * each newline-delimited JSON-RPC message per {@link McpServerConfig}. + * handle and dispatches each newline-delimited JSON-RPC message per + * {@link McpServerConfig}. With a project resolver, a null open fails closed. */ export async function runMcpServerLoop(config: McpServerConfig): Promise { const cwd = process.cwd(); + // Resolve the fail-closed context up front when a resolver is injected; only an + // `ok` context may hold a read handle. Without a resolver, keep the legacy open. + const initialContext = config.resolveContext?.(cwd); + const openValidatedReadonlyDb = (context?: ProjectContext): Database | null => { + let target: string | ProjectDatabaseBinding = cwd; + if (context !== undefined) { + if (context.kind !== 'ok' || context.databaseBinding === undefined) return null; + target = context.databaseBinding; + } + const db = config.openReadonlyDb(target); + if (db === null) return null; + try { + // Bun may construct a handle for malformed bytes and fail only on the + // first statement. Validate version + schema before any tool handler can + // see the handle. Legacy consumers retain the lightweight readability + // probe when they do not opt into a schema validator. + let valid = true; + if (config.validateReadonlyDb) valid = config.validateReadonlyDb(db); + else db.query('PRAGMA user_version').get(); + if (!valid) { + db.close(); + return null; + } + return db; + } catch { + db.close(); + return null; + } + }; + const openHandle = (context: ProjectContext | undefined): Database | null => openValidatedReadonlyDb(context); // Single source of truth for the read handle: the per-call reopen below writes // back to ctx.db, and close() reads ctx.db — so a mid-session reopen is always // the one that gets closed (no stale/leaked handle). - const ctx: ToolContext = { db: config.openReadonlyDb(cwd), cwd }; + const ctx: ToolContext = { db: openHandle(initialContext), cwd, context: initialContext }; const toolByName = new Map(config.tools.map((t) => [t.name, t] as const)); + /** + * Serialize a non-`ok` project context as a stable structured MCP error. This + * is the ONE place the read-only server refuses to serve an empty board when + * repository context or the genie.db is missing/unsupported. + */ + function failClosed(id: number | string | null, context: ProjectContext): void { + const payload = { error: context.kind, detail: (context as { detail?: string }).detail ?? context.kind }; + ok(id, { content: [{ type: 'text', text: JSON.stringify(payload) }], structuredContent: payload, isError: true }); + } + function handleToolsCall(id: number | string | null, params: Record | undefined): void { const name = typeof params?.name === 'string' ? params.name : ''; const args = (params?.arguments as Record | undefined) ?? {}; @@ -132,11 +188,42 @@ export async function runMcpServerLoop(config: McpServerConfig): Promise { }); return; } - // The db may have been absent when the server started (null handle → empty - // board). Re-attempt the read-only open per call so a db created mid-session - // (e.g. a fresh `genie init` or a bridge write) is picked up instead of - // serving empty forever. - if (!ctx.db) ctx.db = config.openReadonlyDb(cwd); + // Fail-closed gate (opt-in). Re-resolve ONLY while not yet `ok` so a db/context + // created mid-session (e.g. a fresh `genie init`) is picked up without spending + // git probes on every call once the context is settled. Any non-`ok` state + // becomes a typed error rather than an outer/cache-root empty board. + if (config.resolveContext) { + const context = ctx.context?.kind === 'ok' ? ctx.context : config.resolveContext(cwd); + ctx.context = context; + if (context.kind !== 'ok') { + if (ctx.db) { + ctx.db.close(); + ctx.db = null; + } + failClosed(id, context); + return; + } + } + // The db may have been absent when the server started. Re-attempt the + // read-only open per call so a db created mid-session (e.g. a fresh + // `genie init` or a bridge write) is picked up. + if (!ctx.db) ctx.db = openValidatedReadonlyDb(ctx.context); + // Existence is not openability: a directory, malformed file, unreadable + // path, or failed readonly open can still arrive with an `ok` path context. + // Never dispatch a fail-closed MCP tool with a null handle, because every + // read tool interprets null as a healthy empty projection. + if (config.resolveContext && !ctx.db && ctx.context?.kind === 'ok') { + const unavailable: ProjectContext = { + ...ctx.context, + kind: 'project-database-unavailable', + detail: `unable to open Genie database at ${ctx.context.dbPath}`, + }; + // Preserve retry behavior: a later call re-resolves this non-ok context + // and may recover after the path is repaired. + ctx.context = unavailable; + failClosed(id, unavailable); + return; + } const payload = tool.handler(ctx, args); ok(id, { content: [{ type: 'text', text: JSON.stringify(payload) }], diff --git a/src/lib/v5/mcp-tools.test.ts b/src/lib/v5/mcp-tools.test.ts new file mode 100644 index 000000000..6bd759d66 --- /dev/null +++ b/src/lib/v5/mcp-tools.test.ts @@ -0,0 +1,372 @@ +/** + * mcp-tools — the fail-closed project-context resolver (Group A) and the + * read-only tool projections it guards. + * + * The resolver is re-exported from mcp-tools.ts on purpose: `genie mcp` pulls it + * through the SAME lazy dynamic import that loads the tool registry, so the read + * server can refuse to serialize an outer/cache-root empty board without dragging + * the readonly bun:sqlite open into the eager genie.ts import graph. + * + * Every fixture is a real git repo in a tmpdir (per repo convention) so the four + * production values — effectiveLaunchCwd, worktreeConfigRoot, absolute + * gitCommonDir, and genieStorageRoot = dirname(gitCommonDir) — are exercised + * against actual `git rev-parse` behavior, not mocks. + */ + +import { Database } from 'bun:sqlite'; +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { execFileSync } from 'node:child_process'; +import { existsSync, linkSync, mkdirSync, mkdtempSync, realpathSync, renameSync, rmSync, symlinkSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { basename, dirname, join } from 'node:path'; +import { openDb } from './genie-db.js'; +import { MCP_TOOLS, openReadonlyDb, readonlyDatabaseHandleMatchesPath, resolveProjectContext } from './mcp-tools.js'; +import { createBoard, createTask } from './task-state.js'; + +let base: string; + +function git(cwd: string, ...args: string[]): void { + execFileSync('git', args, { + cwd, + stdio: 'ignore', + env: { + ...process.env, + GIT_AUTHOR_NAME: 'Test', + GIT_AUTHOR_EMAIL: 'test@example.com', + GIT_COMMITTER_NAME: 'Test', + GIT_COMMITTER_EMAIL: 'test@example.com', + }, + }); +} + +/** Real git repo with one commit at `dir` (created if absent). */ +function initRepo(dir: string): string { + mkdirSync(dir, { recursive: true }); + git(dir, 'init', '-b', 'main'); + git(dir, 'commit', '--allow-empty', '-m', 'init'); + return dir; +} + +/** Create a real, seeded genie.db under `/.genie/genie.db`. */ +function seedDb(storageRoot: string): void { + const db = openDb({ cwd: storageRoot }); + const board = createBoard(db, 'repo'); + createTask(db, { title: 'seed', boardId: board.id, wish: 'w', group: 'g' }); + // Fold pending WAL frames into the main db and close before any reader opens, + // so readonly consumers aren't racing an open WAL writer under cross-file + // test contention (would otherwise surface as "database is locked"). + db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); + db.close(); +} + +/** + * Canonicalize by realpath-ing the nearest EXISTING ancestor and re-appending + * the (possibly absent) tail — so macOS /private symlinks never cause false + * diffs, and an intentionally-absent db path still compares cleanly. + */ +function canon(p: string): string { + let existing = p; + const tail: string[] = []; + while (!existsSync(existing) && dirname(existing) !== existing) { + tail.unshift(basename(existing)); + existing = dirname(existing); + } + return tail.length > 0 ? join(realpathSync(existing), ...tail) : realpathSync(existing); +} + +function samePath(actual: string | undefined, expected: string): void { + expect(actual).toBeDefined(); + expect(canon(actual as string)).toBe(canon(expected)); +} + +beforeEach(() => { + base = mkdtempSync(join(tmpdir(), 'genie-ctx-')); +}); + +afterEach(() => { + rmSync(base, { recursive: true, force: true }); +}); + +// ============================================================================ +// resolveProjectContext — the four-value model + typed fail-closed states +// ============================================================================ + +describe('resolveProjectContext: supported layouts', () => { + test('repository root with a genie.db resolves all four values and never changes launch cwd', () => { + const repo = initRepo(join(base, 'repo')); + seedDb(repo); + const ctx = resolveProjectContext(repo); + expect(ctx.kind).toBe('ok'); + if (ctx.kind !== 'ok') throw new Error('expected ok'); + samePath(ctx.effectiveLaunchCwd, repo); // launch cwd is the input, unchanged + samePath(ctx.worktreeConfigRoot, repo); + samePath(ctx.gitCommonDir, join(repo, '.git')); + samePath(ctx.genieStorageRoot, repo); + samePath(ctx.dbPath, join(repo, '.genie', 'genie.db')); + // genieStorageRoot is exactly dirname(gitCommonDir). + samePath(dirname(ctx.gitCommonDir), ctx.genieStorageRoot); + }); + + test('an ordinary nested subdirectory resolves to the same storage root, not the subdir', () => { + const repo = initRepo(join(base, 'repo')); + seedDb(repo); + const deep = join(repo, 'src', 'deep'); + mkdirSync(deep, { recursive: true }); + const ctx = resolveProjectContext(deep); + expect(ctx.kind).toBe('ok'); + if (ctx.kind !== 'ok') throw new Error('expected ok'); + samePath(ctx.effectiveLaunchCwd, deep); // cwd stays the subdir + samePath(ctx.genieStorageRoot, repo); // but storage is the repo root + samePath(ctx.dbPath, join(repo, '.genie', 'genie.db')); + }); + + test('an initialized nested repository uses its OWN storage root, never the outer db', () => { + const outer = initRepo(join(base, 'outer')); + seedDb(outer); + const nested = initRepo(join(outer, 'vendor', 'nested')); + seedDb(nested); + const ctx = resolveProjectContext(nested); + expect(ctx.kind).toBe('ok'); + if (ctx.kind !== 'ok') throw new Error('expected ok'); + samePath(ctx.genieStorageRoot, nested); + samePath(ctx.dbPath, join(nested, '.genie', 'genie.db')); + expect(canon(ctx.dbPath)).not.toBe(canon(join(outer, '.genie', 'genie.db'))); + }); + + test('a linked worktree keeps config under the linked root but the db under the main common root', () => { + const main = initRepo(join(base, 'main')); + seedDb(main); // db lives ONLY at the main common root + const linked = join(base, 'linked'); + git(main, 'worktree', 'add', '-b', 'wt', linked); + const ctx = resolveProjectContext(linked); + expect(ctx.kind).toBe('ok'); + if (ctx.kind !== 'ok') throw new Error('expected ok'); + samePath(ctx.worktreeConfigRoot, linked); // config stays in the linked worktree + samePath(ctx.gitCommonDir, join(main, '.git')); // common dir is the MAIN repo's .git + samePath(ctx.genieStorageRoot, main); // dirname(gitCommonDir) == main + samePath(ctx.dbPath, join(main, '.genie', 'genie.db')); // sentinel read from the main db + // Neither the main-worktree cwd nor a cache is substituted as launch context. + samePath(ctx.effectiveLaunchCwd, linked); + }); +}); + +describe('resolveProjectContext: fail-closed states', () => { + test('a non-git directory is project-context-unavailable (never falls outward)', () => { + const plain = join(base, 'plain'); + mkdirSync(plain, { recursive: true }); + const ctx = resolveProjectContext(plain); + expect(ctx.kind).toBe('project-context-unavailable'); + samePath(ctx.effectiveLaunchCwd, plain); + }); + + test('a git repo with no genie.db is project-database-unavailable and names the exact candidate', () => { + const repo = initRepo(join(base, 'repo')); + const ctx = resolveProjectContext(repo); + expect(ctx.kind).toBe('project-database-unavailable'); + if (ctx.kind === 'ok') throw new Error('expected error'); + samePath(ctx.genieStorageRoot as string, repo); + samePath(ctx.dbPath as string, join(repo, '.genie', 'genie.db')); + expect(ctx.detail).toContain('.genie/genie.db'); + }); + + test('a .genie symlink to another repository with a valid database is rejected', () => { + const repo = initRepo(join(base, 'repo')); + const other = initRepo(join(base, 'other')); + seedDb(other); + symlinkSync(join(other, '.genie'), join(repo, '.genie'), 'dir'); + + const ctx = resolveProjectContext(repo); + expect(ctx.kind).toBe('project-database-unavailable'); + if (ctx.kind === 'ok') throw new Error('expected error'); + expect(ctx.detail).toContain('physical directory'); + expect(openReadonlyDb(repo)).toBeNull(); + }); + + test('a genie.db symlink to another repository valid database is rejected', () => { + const repo = initRepo(join(base, 'repo')); + const other = initRepo(join(base, 'other')); + seedDb(other); + mkdirSync(join(repo, '.genie')); + symlinkSync(join(other, '.genie', 'genie.db'), join(repo, '.genie', 'genie.db'), 'file'); + + const ctx = resolveProjectContext(repo); + expect(ctx.kind).toBe('project-database-unavailable'); + if (ctx.kind === 'ok') throw new Error('expected error'); + expect(ctx.detail).toContain('physical regular file'); + expect(openReadonlyDb(repo)).toBeNull(); + }); + + test('a hardlinked genie.db physical alias is rejected', () => { + const repo = initRepo(join(base, 'repo')); + const other = initRepo(join(base, 'other')); + seedDb(other); + mkdirSync(join(repo, '.genie')); + linkSync(join(other, '.genie', 'genie.db'), join(repo, '.genie', 'genie.db')); + + const ctx = resolveProjectContext(repo); + expect(ctx.kind).toBe('project-database-unavailable'); + if (ctx.kind === 'ok') throw new Error('expected error'); + expect(ctx.detail).toContain('hardlink'); + expect(openReadonlyDb(repo)).toBeNull(); + }); + + test('an exact validated binding refuses a later genie.db substitution with another valid database', () => { + const repo = initRepo(join(base, 'repo')); + const other = initRepo(join(base, 'other')); + seedDb(repo); + seedDb(other); + const ctx = resolveProjectContext(repo); + if (ctx.kind !== 'ok' || ctx.databaseBinding === undefined) throw new Error('expected bound database'); + + rmSync(ctx.dbPath); + symlinkSync(join(other, '.genie', 'genie.db'), ctx.dbPath, 'file'); + + expect(openReadonlyDb(ctx.databaseBinding)).toBeNull(); + }); + + test('an uninitialized nested repository fails at its own boundary, never reading the outer db', () => { + const outer = initRepo(join(base, 'outer')); + seedDb(outer); // outer HAS a db — the nested boundary must not fall through to it + const nested = initRepo(join(outer, 'nested')); // no db here + const ctx = resolveProjectContext(nested); + expect(ctx.kind).toBe('project-database-unavailable'); + if (ctx.kind === 'ok') throw new Error('expected error'); + samePath(ctx.dbPath as string, join(nested, '.genie', 'genie.db')); + expect(canon(ctx.dbPath as string)).not.toBe(canon(join(outer, '.genie', 'genie.db'))); + }); + + test('a bare repository is unsupported-project-layout before any db lookup', () => { + const bare = join(base, 'bare.git'); + git(base, 'init', '--bare', bare); + const ctx = resolveProjectContext(bare); + expect(ctx.kind).toBe('unsupported-project-layout'); + expect(ctx.dbPath).toBeUndefined(); + }); + + test('a submodule working tree is unsupported-project-layout', () => { + const sub = initRepo(join(base, 'subrepo')); + const superRepo = initRepo(join(base, 'super')); + execFileSync('git', ['-c', 'protocol.file.allow=always', 'submodule', 'add', sub, 'mysub'], { + cwd: superRepo, + stdio: 'ignore', + env: { + ...process.env, + GIT_AUTHOR_NAME: 'T', + GIT_AUTHOR_EMAIL: 't@e.com', + GIT_COMMITTER_NAME: 'T', + GIT_COMMITTER_EMAIL: 't@e.com', + }, + }); + seedDb(superRepo); // even with an outer db present, the submodule must not use it + const ctx = resolveProjectContext(join(superRepo, 'mysub')); + expect(ctx.kind).toBe('unsupported-project-layout'); + expect(ctx.dbPath).toBeUndefined(); + }); + + test('an external/separate-git-dir layout is unsupported-project-layout', () => { + const work = join(base, 'work'); + const external = join(base, 'external-gitdir'); + mkdirSync(work, { recursive: true }); + git(base, 'init', `--separate-git-dir=${external}`, work); + const ctx = resolveProjectContext(work); + expect(ctx.kind).toBe('unsupported-project-layout'); + expect(ctx.dbPath).toBeUndefined(); + }); +}); + +// ============================================================================ +// Opened-handle identity — SQLite VFS binding closes the swap/restore window +// ============================================================================ + +describe('openReadonlyDb: exact opened handle', () => { + test('HAS_MOVED accepts only a zero SQLite result with a zero moved flag', () => { + const handle = (result: number, moved: number, throws = false): Pick => ({ + fileControl: ((_op: number, arg?: ArrayBufferView | number) => { + if (throws) throw new Error('fixture unsupported VFS'); + if (arg instanceof Int32Array) arg[0] = moved; + return result; + }) as Database['fileControl'], + }); + + expect(readonlyDatabaseHandleMatchesPath(handle(0, 0))).toBe(true); + expect(readonlyDatabaseHandleMatchesPath(handle(0, 1))).toBe(false); + expect(readonlyDatabaseHandleMatchesPath(handle(12, 0))).toBe(false); + expect(readonlyDatabaseHandleMatchesPath(handle(0, 0, true))).toBe(false); + }); + + test('an injected moved-handle refusal closes the opened database', () => { + const repo = initRepo(join(base, 'repo')); + seedDb(repo); + const ctx = resolveProjectContext(repo); + if (ctx.kind !== 'ok' || ctx.databaseBinding === undefined) throw new Error('expected bound database'); + let closed = false; + const fakeDb = { + close: () => { + closed = true; + }, + } as Database; + + const opened = openReadonlyDb(ctx.databaseBinding, { + openDatabase: () => fakeDb, + verifyOpenedHandle: () => false, + }); + + expect(opened).toBeNull(); + expect(closed).toBe(true); + }); + + test.skipIf(process.platform === 'win32')( + 'a real A→B constructor swap then B→A restore is rejected by the opened VFS handle', + () => { + const repo = initRepo(join(base, 'repo')); + const other = initRepo(join(base, 'other')); + seedDb(repo); + seedDb(other); + const ctx = resolveProjectContext(repo); + if (ctx.kind !== 'ok' || ctx.databaseBinding === undefined) throw new Error('expected bound database'); + const otherDbPath = join(other, '.genie', 'genie.db'); + const parkedOriginal = join(base, 'parked-original.db'); + + const raced = openReadonlyDb(ctx.databaseBinding, { + openDatabase: (path) => { + renameSync(path, parkedOriginal); + renameSync(otherDbPath, path); + let opened: Database; + try { + opened = new Database(path, { readonly: true }); + } finally { + renameSync(path, otherDbPath); + renameSync(parkedOriginal, path); + } + return opened; + }, + }); + + expect(raced).toBeNull(); + const restored = openReadonlyDb(ctx.databaseBinding); + expect(restored).not.toBeNull(); + restored?.close(); + }, + ); +}); + +// ============================================================================ +// The read tools serve real state when the context is ok +// ============================================================================ + +describe('tools serve real state under an ok context', () => { + test('genie_board reflects the seeded db opened from the resolved storage root', () => { + const repo = initRepo(join(base, 'repo')); + seedDb(repo); + const ctx = resolveProjectContext(repo); + if (ctx.kind !== 'ok') throw new Error('expected ok'); + const db = openReadonlyDb(ctx.effectiveLaunchCwd); + expect(db).not.toBeNull(); + const board = MCP_TOOLS.find((t) => t.name === 'genie_board'); + const payload = board?.handler({ db, cwd: ctx.effectiveLaunchCwd, context: ctx }, {}) as { + counts: { total: number }; + }; + expect(payload.counts.total).toBe(1); + db?.close(); + }); +}); diff --git a/src/lib/v5/mcp-tools.ts b/src/lib/v5/mcp-tools.ts index e41745a12..12aa1695d 100644 --- a/src/lib/v5/mcp-tools.ts +++ b/src/lib/v5/mcp-tools.ts @@ -13,9 +13,20 @@ * every tool renders an empty board rather than erroring. */ -import { Database } from 'bun:sqlite'; +import { constants, Database } from 'bun:sqlite'; import { execFileSync } from 'node:child_process'; -import { resolveDbPath } from './genie-db.js'; +import { + type ProjectContext, + type ProjectDatabaseBinding, + resolveDbPath, + resolveProjectDatabaseBinding, +} from './genie-db.js'; +import { BUSY_TIMEOUT_MS } from './sqlite-open.js'; + +// Re-exported so `genie mcp` (mcp.ts) pulls the fail-closed context resolver in +// the SAME lazy dynamic import that already loads the tool registry — keeping +// the readonly bun:sqlite open out of the eager genie.ts import graph. +export { isCurrentGenieDb, type ProjectContext, resolveProjectContext } from './genie-db.js'; import { type TaskFilter, type TaskRow, @@ -28,19 +39,86 @@ import { } from './task-state.js'; // ============================================================================ -// Read-only DB open (net-new; degrade to null when the file is absent) +// Read-only DB open (exact validated binding; null on any mismatch) // ============================================================================ +export interface ReadonlyDbOpenDependencies { + /** Test seam around the constructor; production always opens Bun SQLite readonly. */ + openDatabase?: (path: string) => Database; + /** Test seam for deterministic moved/unsupported-handle refusal. */ + verifyOpenedHandle?: (db: Database) => boolean; +} + /** - * Open the repo's shared `.genie/genie.db` READ-ONLY. Returns `null` when the - * file does not exist (readonly open of a missing file throws) so callers can - * degrade to an empty board instead of crashing the server. The handle is the - * caller's to close. + * Ask SQLite's opened VFS handle whether its pathname still names that handle. + * A nonzero return code, moved flag, or exception (including an unsupported + * VFS) is a fail-closed mismatch. */ -export function openReadonlyDb(cwd?: string): Database | null { +export function readonlyDatabaseHandleMatchesPath(db: Pick): boolean { + const moved = new Int32Array(1); + try { + const result = db.fileControl(constants.SQLITE_FCNTL_HAS_MOVED, moved); + return result === 0 && moved[0] === 0; + } catch { + return false; + } +} + +function closeReadonlyDb(db: Database | null): void { try { - return new Database(resolveDbPath(cwd), { readonly: true }); + db?.close(); + } catch { + // The caller is already failing closed; cleanup must not turn null into a throw. + } +} + +/** + * Open the repo's shared `.genie/genie.db` READ-ONLY. A project MCP caller + * supplies the exact physical binding produced by `resolveProjectContext`; + * legacy callers may supply cwd and receive the same path validation here. + * Symlinks, non-regular files, and identity substitutions return `null`. + * + * The read-only connection is given the SAME `busy_timeout` as the shared write + * primitive (see sqlite-open.ts): under concurrent access a straggling WAL + * writer must be waited out, not surfaced as an instant `-32603 "database is + * locked"`. `busy_timeout` is valid on a readonly connection and does not + * mutate the file. The binding is revalidated both before and after SQLite opens + * it. SQLite's `SQLITE_FCNTL_HAS_MOVED` check additionally binds the opened VFS + * handle itself, catching an A→B constructor race even if the path is restored + * to A before post-open lstat. Unsupported VFS implementations fail closed. + */ +export function openReadonlyDb( + target?: string | ProjectDatabaseBinding, + dependencies: ReadonlyDbOpenDependencies = {}, +): Database | null { + const initial = + typeof target === 'object' + ? resolveProjectDatabaseBinding(target.logicalPath, target) + : resolveProjectDatabaseBinding(resolveDbPath(target)); + if (!initial.ok) return null; + let db: Database | null = null; + const verifyOpenedHandle = dependencies.verifyOpenedHandle ?? readonlyDatabaseHandleMatchesPath; + try { + db = + dependencies.openDatabase?.(initial.binding.physicalPath) ?? + new Database(initial.binding.physicalPath, { readonly: true }); + if (!verifyOpenedHandle(db)) { + closeReadonlyDb(db); + return null; + } + const revalidated = resolveProjectDatabaseBinding(initial.binding.logicalPath, initial.binding); + if (!revalidated.ok) { + closeReadonlyDb(db); + return null; + } + if (!verifyOpenedHandle(db)) { + closeReadonlyDb(db); + return null; + } + db.exec(`PRAGMA busy_timeout = ${BUSY_TIMEOUT_MS}`); + return db; } catch { + closeReadonlyDb(db); return null; } } @@ -145,6 +223,13 @@ export interface ToolContext { db: Database | null; /** Working directory for git branch resolution. Defaults to `process.cwd()`. */ cwd: string; + /** + * The fail-closed project context resolved by the server loop when a resolver + * is injected. When its `kind` is not `ok`, the loop returns a typed error for + * every tool call instead of an empty board (see mcp-server.ts). Absent for + * consumers (e.g. ui-bridge) that do not opt into fail-closed resolution. + */ + context?: ProjectContext; } export interface McpTool { diff --git a/src/lib/v5/task-state.test.ts b/src/lib/v5/task-state.test.ts index fb6edeb5e..0a466e99e 100644 --- a/src/lib/v5/task-state.test.ts +++ b/src/lib/v5/task-state.test.ts @@ -449,6 +449,21 @@ describe('runtime layer — claim / release timeline events', () => { expect(getTaskEvents(db, a.id).at(-1)?.kind).toBe('release'); }); + test('releaseTask clears heartbeat_at so the next owner does not inherit stale liveness', () => { + const a = createTask(db, { title: 'a' }); + claimTask(db, a.id, 'w1'); + recordHeartbeat(db, a.id, 10_000_000); + expect(getTaskCard(db, a.id)?.heartbeatAt).toBe(10_000_000); + + releaseTask(db, a.id, HUMAN); + // The card is back to ready with no lingering pulse; a fresh checkout by + // worker B must read stale (never running) until B itself heartbeats. + expect(getTaskCard(db, a.id)?.heartbeatAt).toBeNull(); + + claimTask(db, a.id, 'w2'); + expect(getTaskCard(db, a.id)?.heartbeatAt).toBeNull(); + }); + test('releaseTask REFUSES a done card — never resurrects it, emits no release event', () => { const a = createTask(db, { title: 'a' }); claimTask(db, a.id, 'w1'); diff --git a/src/lib/v5/task-state.ts b/src/lib/v5/task-state.ts index 32961f07e..88a8f11f6 100644 --- a/src/lib/v5/task-state.ts +++ b/src/lib/v5/task-state.ts @@ -792,7 +792,9 @@ function releaseFailure(db: Database, taskId: string): never { /** * Release a claim WITHOUT completing — returns an `in_progress` card to the - * `ready` queue and clears the claim so another runtime can pick it up. The state + * `ready` queue and clears the claim (including `heartbeat_at`, so the next + * runtime to check the card out does not inherit the prior owner's liveness) so + * another runtime can pick it up. The state * check lives IN the SQL (`WHERE ... AND status = 'in_progress'`) exactly like * {@link claimTask}, so a concurrent `done`/re-claim that transitions the card out * of `in_progress` between decision and write can never be clobbered: the @@ -807,7 +809,7 @@ export function releaseTask(db: Database, taskId: string, author: EventAuthor): const res = db .query( `UPDATE tasks - SET status = 'ready', claimed_by = NULL, claimed_at = NULL, updated_at = ? + SET status = 'ready', claimed_by = NULL, claimed_at = NULL, heartbeat_at = NULL, updated_at = ? WHERE id = ? AND status = 'in_progress'`, ) .run(now, taskId); diff --git a/src/term-commands/hook/trust.ts b/src/term-commands/hook/trust.ts index db80d11b2..78189df2c 100644 --- a/src/term-commands/hook/trust.ts +++ b/src/term-commands/hook/trust.ts @@ -187,7 +187,7 @@ export function registerHookTrustCommand(parent: Command): void { .action(trustAction); } -/** Re-export defaultTrustPath for callers that want to display it. */ +/** @public Re-export defaultTrustPath for callers that want to display it. */ export { defaultTrustPath } from '../../hooks/trust.js'; -/** Re-export readTrustFile for callers that want to inspect entries. */ +/** @public Re-export readTrustFile for callers that want to inspect entries. */ export { readTrustFile } from '../../hooks/trust.js'; diff --git a/src/term-commands/init.test.ts b/src/term-commands/init.test.ts index 7d452a345..f36e57c43 100644 --- a/src/term-commands/init.test.ts +++ b/src/term-commands/init.test.ts @@ -293,138 +293,81 @@ describe('Codex MCP fallback merge', () => { }); // ============================================================================ -// Verified-current fallback gate — B's observation facade (Group D, D5) +// Marker-owned Codex route — plugin- and delivery-independent (Group A) // ============================================================================ -import { - type CanonicalFact, - type CodexActivationSnapshot, - type FamilyWitness, - type IntentFact, - type PhysicalCacheFact, - type QueryFact, - type RefreshIntent, - parseReleaseVersion, -} from '../lib/codex-activation.js'; -import { isCodexVerifiedCurrent } from './init.js'; - -const IT_T = '5.260712.1'; -const IT_OLD = '5.260711.9'; -const IT_NEWER = '5.260713.4'; -const IT_DIGEST = 'a'.repeat(64); - -function itVer(s: string) { - const parsed = parseReleaseVersion(s); - if (!parsed) throw new Error(`bad version ${s}`); - return parsed; -} -function itFamily(): FamilyWitness { - return { status: 'present', digest: 'f'.repeat(64), identity: '10:300' }; -} -function itCanonical(): CanonicalFact { - return { status: 'ok', version: itVer(IT_T), digest: IT_DIGEST, identity: '10:100' }; -} -function itReg(version = IT_T): QueryFact { - return { status: 'ok', registration: { present: true, enabled: true, version: itVer(version) } }; -} -function itCache(digest = IT_DIGEST): PhysicalCacheFact { - return { kind: 'present', digest, identity: '10:200' }; -} -function itIntentTargetCurrent(): IntentFact { - const intent: RefreshIntent = { - schemaVersion: 1, - refreshIntentId: '1'.repeat(32), - operationId: '2'.repeat(32), - fromPluginVersion: IT_OLD, - targetVersion: IT_T, - direction: 'upgrade', - priorEnabled: true, - canonicalPayloadSha256: IT_DIGEST, - phase: 'planned', - commandKind: 'codex-plugin-add', - lastFailure: '', - receiptId: null, - }; - return { status: 'valid', intent, contentSha256: 'd'.repeat(64) }; -} -function itSnapshot(over: Partial = {}): CodexActivationSnapshot { - return { - canonical: itCanonical(), - query: itReg(), - cache: itCache(), - receipt: { status: 'absent' }, - delivery: { status: 'absent' }, - intent: { status: 'absent' }, - receiptConsumed: false, - observationWitness: { before: itFamily(), after: itFamily() }, - observedAt: '2026-07-12T00:00:00.000Z', - ...over, - }; -} -function gate(snapshot: CodexActivationSnapshot, observed: string[] = []): boolean { - return isCodexVerifiedCurrent({ - resolveCodexCommand: () => '/fake/codex', - observeCodexActivation: ({ command }) => { - observed.push(command ?? 'null'); - return snapshot; - }, +/** Run `genie init` with an isolated GENIE_HOME and no Codex CLI on PATH. */ +function runInitWithHome(cwd: string, genieHome: string): { code: number; stderr: string } { + const res = Bun.spawnSync([process.execPath, CLI, 'init'], { + cwd, + stdout: 'pipe', + stderr: 'pipe', + // No Codex CLI on PATH: init must STILL write the marker (plugin-independent). + env: { ...process.env, PATH: '/usr/bin:/bin', GENIE_HOME: genieHome }, }); + return { code: res.exitCode, stderr: res.stderr.toString() }; } -describe('init verified-current fallback gate', () => { - test('a fresh `current` observation is the ONLY state that reconciles (true)', () => { - expect(gate(itSnapshot())).toBe(true); - }); - - test('state matrix: pending / broken / indeterminate / recovery all retain fallback (false)', () => { - // activation-pending (N < T) - expect(gate(itSnapshot({ query: itReg(IT_OLD), cache: itCache('b'.repeat(64)) }))).toBe(false); - // query-failed (broken/indeterminate) - expect(gate(itSnapshot({ query: { status: 'failed', detail: 'timed out' } }))).toBe(false); - // registration-absent (activation required) - expect( - gate(itSnapshot({ query: { status: 'ok', registration: { present: false } }, cache: { kind: 'absent' } })), - ).toBe(false); - // cache-missing - expect(gate(itSnapshot({ cache: { kind: 'absent' } }))).toBe(false); - // installed-newer (implicit downgrade refused) - expect(gate(itSnapshot({ query: itReg(IT_NEWER) }))).toBe(false); - }); - - test('a current-LOOKING but payload-mismatched snapshot is NOT fresh authority (false)', () => { - // same version string, divergent cache digest → payload-mismatch, never current. - expect(gate(itSnapshot({ cache: itCache('c'.repeat(64)) }))).toBe(false); +describe('init marker-owned Codex route', () => { + test('writes the stable /bin/genie facade with args ["mcp"] and no cwd override', () => { + initGitRepo(dir); + const home = join(dir, 'home-a'); + expect(runInitWithHome(dir, home).code).toBe(0); + const toml = readFileSync(join(dir, '.codex', 'config.toml'), 'utf8'); + expect(toml).toContain('# BEGIN GENIE MCP FALLBACK'); + // The marker command is the canonical stable facade, never a versioned cache path. + expect(toml).toContain(`mcp_servers.genie.command = "${join(home, 'bin', 'genie')}"`); + expect(toml).toContain('mcp_servers.genie.args = ["mcp"]'); + // No effective cwd override is written. + expect(toml).not.toContain('cwd'); }); - test('a current-version snapshot with an unresolved refresh intent is a recovery state, not current (false)', () => { - // intent-target-current dominates the ordinary `current` row: retain fallback. - expect(gate(itSnapshot({ intent: itIntentTargetCurrent() }))).toBe(false); + test('is created even with no Codex CLI present (plugin- and delivery-independent)', () => { + initGitRepo(dir); + // Default runInit already strips codex from PATH; the marker must still exist. + expect(runInit(dir).code).toBe(0); + expect(existsSync(join(dir, '.codex', 'config.toml'))).toBe(true); + expect(readFileSync(join(dir, '.codex', 'config.toml'), 'utf8')).toContain('mcp_servers.genie'); }); - test('the observation is taken with the freshly resolved codex command (no stale authority)', () => { - const observed: string[] = []; - gate(itSnapshot(), observed); - expect(observed).toEqual(['/fake/codex']); + test('an explicit GENIE_HOME relocation is reconciled to the new facade by init', () => { + initGitRepo(dir); + const homeA = join(dir, 'home-a'); + const homeB = join(dir, 'home-b'); + expect(runInitWithHome(dir, homeA).code).toBe(0); + expect(runInitWithHome(dir, homeB).code).toBe(0); + const toml = readFileSync(join(dir, '.codex', 'config.toml'), 'utf8'); + expect(toml).toContain(`mcp_servers.genie.command = "${join(homeB, 'bin', 'genie')}"`); + expect(toml).not.toContain(join(homeA, 'bin', 'genie')); + // Exactly one owned marker block — reconciliation never duplicates. + expect(toml.match(/BEGIN GENIE MCP FALLBACK/g)).toHaveLength(1); }); - test('an absent codex CLI observes with a null command and never reconciles', () => { - const observed: string[] = []; - const result = isCodexVerifiedCurrent({ - resolveCodexCommand: () => null, - observeCodexActivation: ({ command }) => { - observed.push(command === null ? 'null' : command); - return itSnapshot({ query: { status: 'failed', detail: 'codex CLI not found' } }); - }, - }); - expect(result).toBe(false); - expect(observed).toEqual(['null']); + test('an unowned same-key Codex route is preserved byte-for-byte and reported, never overwritten', () => { + initGitRepo(dir); + const codexPath = join(dir, '.codex', 'config.toml'); + mkdirSync(join(dir, '.codex'), { recursive: true }); + const personal = '[mcp_servers.genie]\ncommand = "/my/own/genie"\nargs = ["mcp"]\n'; + writeFileSync(codexPath, personal); + const { code, stderr } = runInitWithHome(dir, join(dir, 'home-a')); + expect(code).toBe(1); + expect(stderr.toLowerCase()).toMatch(/unverified|preserved/); + // The user file is untouched, and no scaffold leaked past the collision. + expect(readFileSync(codexPath, 'utf8')).toBe(personal); + expect(existsSync(join(dir, '.genie', 'INDEX.md'))).toBe(false); }); - test('init.ts never mints an assertion/permit and never touches the lifecycle lease', () => { + test('init.ts never mints an assertion/permit and never touches the lifecycle lease or delivery', () => { const source = readFileSync(join(import.meta.dir, 'init.ts'), 'utf8'); - expect(source.includes('acquireLifecycleLease')).toBe(false); - expect(source.includes('requestRetirementAssertion')).toBe(false); - expect(source.includes('authorizeCodexActivation')).toBe(false); - expect(source.includes('executeCodexActivation')).toBe(false); + for (const forbidden of [ + 'acquireLifecycleLease', + 'requestRetirementAssertion', + 'authorizeCodexActivation', + 'executeCodexActivation', + 'observeCodexActivation', + 'beginActivation', + ]) { + expect(source.includes(forbidden)).toBe(false); + } }); }); diff --git a/src/term-commands/init.ts b/src/term-commands/init.ts index 6d8c80023..341c0d1d7 100644 --- a/src/term-commands/init.ts +++ b/src/term-commands/init.ts @@ -14,16 +14,11 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import type { Command } from 'commander'; -// Group D consumes B's stable observation/reporting facade — never A's internals -// directly — to gate init's project-fallback reconciliation on a fresh -// `verified-current` observation. -import { classifyCodexActivation, observeCodexActivation } from '../lib/codex-activation-executor.js'; -import type { CodexActivationSnapshot } from '../lib/codex-activation.js'; import { type ArtifactAction, - type CodexPluginProbe, type McpConfigResult, type RegisterProjectMcpOptions, + genieFacadeMcpEntry, mergeCodexMcpFallback, registerProjectMcpConfigs, removeCodexMcpFallback, @@ -143,69 +138,6 @@ export function registerMcpConfigs(root: string, options: RegisterProjectMcpOpti return registerProjectMcpConfigs(root, options); } -// ============================================================================ -// Verified-current fallback gate (Group D, D5) -// ============================================================================ - -/** Test/observation seams for the read-only, permit-free verified-current gate. */ -export interface InitCodexObservationDeps { - /** Injected activation observer (defaults to B's facade over the resolved codex CLI). */ - observeCodexActivation?: (options: { command: string | null }) => CodexActivationSnapshot; - /** Resolve the codex executable (defaults to `Bun.which('codex')`, null on failure). */ - resolveCodexCommand?: () => string | null; -} - -function defaultResolveCodexCommand(): string | null { - try { - return Bun.which('codex'); - } catch { - return null; - } -} - -/** - * Decide whether init may reconcile the project fallback (trust the plugin, - * remove the marker-owned `.codex/config.toml` route). ONLY a fresh, unambiguous - * `verified-current` activation observation grants that — the classifier's - * `current` state, which requires the installed cache digest to equal the - * canonical payload with no unresolved refresh intent. Pending, broken, - * indeterminate, and current-LOOKING recovery states (e.g. `intent-target-current`) - * all return false so the fallback is retained. This is a pure, read-only - * observation: it never mints an assertion/permit, runs a plugin/cache mutator, - * or touches the lifecycle lease. It never treats a prior/current-looking - * snapshot as fresh authority — the observation is taken here, now. - */ -export function isCodexVerifiedCurrent(deps: InitCodexObservationDeps = {}): boolean { - const resolveCommand = deps.resolveCodexCommand ?? defaultResolveCodexCommand; - const observe = deps.observeCodexActivation ?? ((options) => observeCodexActivation(options)); - const snapshot = observe({ command: resolveCommand() }); - return classifyCodexActivation(snapshot).kind === 'current'; -} - -/** - * Project the verified-current verdict onto the codex-fallback decision - * `registerProjectMcpConfigs` consumes: a verified-current observation is the - * only usable-plugin signal (removes the fallback); everything else keeps - * `isUsableCodexPlugin` false so the marker-owned fallback is retained. - */ -function verifiedCurrentProbe(verifiedCurrent: boolean): CodexPluginProbe { - return verifiedCurrent - ? { - cliAvailable: true, - status: 'ok', - installed: true, - enabled: true, - usable: true, - detail: 'activation observer: verified-current (plugin trusted, project fallback removed)', - } - : { - cliAvailable: false, - status: 'unavailable', - installed: false, - detail: 'activation observer: not verified-current; project fallback retained', - }; -} - // ============================================================================ // Reporting // ============================================================================ @@ -253,21 +185,21 @@ interface InitOptions { json?: boolean; } -function handleInit(opts: InitOptions, deps: InitCodexObservationDeps = {}): void { +function handleInit(opts: InitOptions): void { run(() => { const root = resolveGitRoot(process.cwd()); if (!root) throw new NotAGitRepoError(); - // Init reconciles the Codex project fallback ONLY when a fresh observation is - // exactly `verified-current`; pending/broken/indeterminate/recovery states - // retain the fallback and make zero plugin/cache mutation. The observation is - // read-only and never mints an assertion/permit or acquires the lease. - const verifiedCurrent = isCodexVerifiedCurrent(deps); - - // MCP config is planned and schema-checked before any scaffold mutation. - // A wrong-shaped existing JSON file therefore fails without leaving a new - // INDEX or gitignore rules behind. - const mcp = registerMcpConfigs(root, { pluginProbe: verifiedCurrentProbe(verifiedCurrent) }); + // Trusted init ALWAYS reconciles one intact marker-owned Codex route using the + // stable `/bin/genie` facade with no effective cwd override — + // independent of plugin and delivery state, and never touching delivery, + // journal, plugin-enabled, agent, or cache state. A route name is not proof of + // ownership, so an unmanaged same-key route is preserved and reported. + // + // MCP config is planned and schema-checked before any scaffold mutation, so a + // wrong-shaped existing JSON file fails without leaving a new INDEX or + // gitignore rules behind. + const mcp = registerMcpConfigs(root, { codexEntry: genieFacadeMcpEntry(), forceCodexFallback: true }); const index = scaffoldIndex(root); const gitignore = scaffoldGitignore(root); const result: InitResult = { root, index, gitignore: gitignore.action, rulesAdded: gitignore.added, mcp }; diff --git a/src/term-commands/mcp.test.ts b/src/term-commands/mcp.test.ts index 3329f56f7..5554a5dff 100644 --- a/src/term-commands/mcp.test.ts +++ b/src/term-commands/mcp.test.ts @@ -8,9 +8,10 @@ * `genie.ts` except through the dynamic `import()` inside the `mcp` action. */ +import { Database } from 'bun:sqlite'; import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; import { execFileSync } from 'node:child_process'; -import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join, resolve } from 'node:path'; import { openDb } from '../lib/v5/genie-db.js'; @@ -118,6 +119,10 @@ function seed(cwd: string): { taskId: string } { const t = createTask(db, { title: 'seed task', boardId: board.id, wish: 'genie-mcp', group: 'g2' }); createTask(db, { title: 'other', boardId: board.id }); createWishGroups(db, 'genie-mcp', [{ name: 'g1' }, { name: 'g2', dependsOn: ['g1'] }]); + // Fold pending WAL frames into the main db before the reader subprocess opens, + // so the readonly `genie mcp` server isn't racing an open WAL writer under + // cross-file test contention ("database is locked"). + db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); db.close(); return { taskId: t.id }; } @@ -284,6 +289,7 @@ describe('mcp tools/call', () => { Date.now(), taskId, ); + db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); db.close(); const responses = await driveMcp(repo, [ INIT, @@ -325,6 +331,7 @@ describe('mcp runtime-layer backward compatibility', () => { db.query( "UPDATE tasks SET blocked_by='x', blocked_reason='r', heartbeat_at=1, agent_kind='codex', lane='Idea' WHERE id=?", ).run(taskId); + db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); db.close(); const responses = await driveMcp(repo, [ @@ -369,25 +376,162 @@ describe('mcp runtime-layer backward compatibility', () => { }); // ============================================================================ -// Absent-db degrade +// Fail-closed: a missing genie.db is a typed error, never an empty board // ============================================================================ -describe('mcp absent-db degrade', () => { - test('genie_board degrades to an empty board when genie.db is absent', async () => { - // Fresh repo, never seeded → no .genie/genie.db. Readonly open throws → null. +describe('mcp missing-database fail-closed', () => { + function seedRawDatabase(sql: string): string { + const dbPath = join(repo, '.genie', 'genie.db'); + mkdirSync(dirname(dbPath), { recursive: true }); + const db = new Database(dbPath); + db.exec(sql); + db.close(); + return dbPath; + } + + async function expectProjectDatabaseUnavailable(id: number): Promise<{ error: string; detail: string }> { + const responses = await driveMcp(repo, [ + INIT, + INITIALIZED, + { jsonrpc: '2.0', id, method: 'tools/call', params: { name: 'genie_board', arguments: {} } }, + ]); + const response = responses.find((candidate) => candidate.id === id); + expect(response?.error).toBeUndefined(); + expect(response?.result?.isError).toBe(true); + const payload = toolPayload<{ error: string; detail: string }>(response!); + expect(payload.error).toBe('project-database-unavailable'); + expect(payload.detail).toContain(join(repo, '.genie', 'genie.db')); + expect(payload).not.toHaveProperty('counts'); + expect(payload).not.toHaveProperty('tasks'); + return payload; + } + + test('genie_board on a git repo with no genie.db returns project-database-unavailable, not empty success', async () => { + // Fresh git repo, never seeded → no .genie/genie.db. The old behavior served + // a healthy-looking empty board (the masquerade); Group A returns a typed error. const responses = await driveMcp(repo, [ INIT, INITIALIZED, { jsonrpc: '2.0', id: 12, method: 'tools/call', params: { name: 'genie_board', arguments: {} } }, ]); const res = responses.find((r) => r.id === 12); - expect(res?.result?.isError).toBe(false); - const payload = toolPayload<{ board: null; counts: { total: number }; tasks: unknown[] }>(res!); - expect(payload.counts.total).toBe(0); - expect(payload.tasks).toEqual([]); + expect(res?.result?.isError).toBe(true); + const payload = toolPayload<{ error: string; detail: string }>(res!); + expect(payload.error).toBe('project-database-unavailable'); + // The error names the exact storage-root DB candidate, never a cache path. + expect(payload.detail).toContain('.genie/genie.db'); + expect(payload).not.toHaveProperty('counts'); + expect(payload).not.toHaveProperty('tasks'); }); - test('reopens a db created AFTER the server started (no stale empty board)', async () => { + test('every read tool fails closed identically when the database is absent', async () => { + const calls = [ + { id: 13, name: 'genie_board', arguments: {} }, + { id: 14, name: 'genie_wish_status', arguments: { wish: 'x' } }, + { id: 15, name: 'genie_worktree_context', arguments: { branch: 'main' } }, + { id: 16, name: 'genie_task', arguments: { id: 't_1' } }, + { id: 17, name: 'genie_active', arguments: {} }, + ]; + const responses = await driveMcp(repo, [ + INIT, + INITIALIZED, + ...calls.map((c) => ({ + jsonrpc: '2.0', + id: c.id, + method: 'tools/call', + params: { name: c.name, arguments: c.arguments }, + })), + ]); + for (const c of calls) { + const res = responses.find((r) => r.id === c.id); + expect(res?.result?.isError).toBe(true); + expect(toolPayload<{ error: string }>(res!).error).toBe('project-database-unavailable'); + } + }); + + for (const fixture of ['directory', 'malformed-file'] as const) { + test(`genie_board fails closed when genie.db is an existing ${fixture}`, async () => { + const dbPath = join(repo, '.genie', 'genie.db'); + mkdirSync(join(repo, '.genie'), { recursive: true }); + if (fixture === 'directory') mkdirSync(dbPath); + else writeFileSync(dbPath, 'not a sqlite database'); + + const responses = await driveMcp(repo, [ + INIT, + INITIALIZED, + { jsonrpc: '2.0', id: 18, method: 'tools/call', params: { name: 'genie_board', arguments: {} } }, + ]); + const res = responses.find((response) => response.id === 18); + expect(res?.result?.isError).toBe(true); + const payload = toolPayload<{ error: string; detail: string }>(res!); + expect(payload.error).toBe('project-database-unavailable'); + expect(payload.detail).toContain(dbPath); + expect(payload).not.toHaveProperty('counts'); + expect(payload).not.toHaveProperty('tasks'); + }); + } + + for (const alias of ['genie-directory', 'database-file'] as const) { + test(`genie_board rejects a ${alias} symlink to another valid repository database`, async () => { + const other = mkdtempSync(join(tmpdir(), 'genie-mcp-alias-')); + git(other, 'init', '-b', 'main'); + git(other, 'commit', '--allow-empty', '-m', 'init'); + seed(other); + try { + if (alias === 'genie-directory') { + symlinkSync(join(other, '.genie'), join(repo, '.genie'), 'dir'); + } else { + mkdirSync(join(repo, '.genie')); + symlinkSync(join(other, '.genie', 'genie.db'), join(repo, '.genie', 'genie.db'), 'file'); + } + + const payload = await expectProjectDatabaseUnavailable(alias === 'genie-directory' ? 51 : 52); + expect(payload.detail).toContain(alias === 'genie-directory' ? 'physical directory' : 'physical regular file'); + } finally { + rmSync(other, { recursive: true, force: true }); + } + }); + } + + test('rejects a foreign-version database with a lookalike tasks table instead of exposing its rows', async () => { + seedRawDatabase(` + CREATE TABLE tasks ( + id TEXT PRIMARY KEY, + board_id TEXT, + title TEXT NOT NULL, + status TEXT NOT NULL, + claimed_by TEXT, + claimed_at INTEGER, + wish TEXT, + group_name TEXT, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL + ); + INSERT INTO tasks VALUES ('foreign', NULL, 'attacker row', 'ready', NULL, NULL, NULL, NULL, 1, 1); + PRAGMA user_version = 99; + `); + + await expectProjectDatabaseUnavailable(50); + }); + + test('rejects a user_version=0 foreign SQLite database as a structured project database error', async () => { + seedRawDatabase( + "CREATE TABLE inventory (id TEXT PRIMARY KEY, secret TEXT); INSERT INTO inventory VALUES ('x', 'y')", + ); + + await expectProjectDatabaseUnavailable(51); + }); + + test('rejects a current-version database whose required Genie schema is incomplete', async () => { + seedRawDatabase(` + CREATE TABLE tasks (id TEXT PRIMARY KEY, title TEXT NOT NULL, status TEXT NOT NULL, created_at INTEGER NOT NULL); + PRAGMA user_version = 1; + `); + + await expectProjectDatabaseUnavailable(52); + }); + + test('reopens a genuine current db created AFTER the server started (no stale empty board)', async () => { // Server starts against a repo with no genie.db (null handle), THEN the db // is created mid-session — a per-call reopen must pick it up, not serve empty. const proc = Bun.spawn(['bun', GENIE, 'mcp'], { @@ -425,6 +569,7 @@ describe('mcp absent-db degrade', () => { .filter((l) => l.trim().length > 0) .map((l) => JSON.parse(l) as RpcResponse); const res = responses.find((r) => r.id === 20); + expect(res?.result?.isError).toBe(false); const payload = toolPayload<{ counts: { total: number } }>(res!); expect(payload.counts.total).toBeGreaterThan(0); // saw the db created mid-session }); @@ -476,6 +621,7 @@ describe('mcp worktree branch resolution', () => { createWishGroups(db, 'genie', [{ name: 'mcp' }]); createTask(db, { title: 'b', boardId: board.id, wish: 'genie-mcp', group: 'g1' }); createWishGroups(db, 'genie-mcp', [{ name: 'g1' }]); + db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); db.close(); const res = await driveMcp(repo, [ INIT, diff --git a/src/term-commands/mcp.ts b/src/term-commands/mcp.ts index aa363e9c6..7034d623b 100644 --- a/src/term-commands/mcp.ts +++ b/src/term-commands/mcp.ts @@ -37,11 +37,16 @@ const PROTOCOL_VERSION = '2024-11-05'; */ export async function runMcpServer(): Promise { // Lazy: the read-only bun:sqlite open + tools load here, not at genie startup. - const { MCP_TOOLS, openReadonlyDb } = await import('../lib/v5/mcp-tools.js'); + const mcpTools = await import('../lib/v5/mcp-tools.js'); + const { isCurrentGenieDb, MCP_TOOLS, openReadonlyDb, resolveProjectContext } = mcpTools; const { runMcpServerLoop } = await import('../lib/v5/mcp-server.js'); await runMcpServerLoop({ tools: MCP_TOOLS, openReadonlyDb, + validateReadonlyDb: isCurrentGenieDb, + // Fail-closed: missing repository context / genie.db / unsupported layouts + // surface as a typed MCP error instead of a healthy-looking empty board. + resolveContext: resolveProjectContext, // Fixed reply that ignores the client's declared version — read-only `genie // mcp` does NOT negotiate. Key order pinned for byte-identical output. initialize: () => ({ diff --git a/src/term-commands/omni.ts b/src/term-commands/omni.ts index 7eda35c5a..c726d8761 100644 --- a/src/term-commands/omni.ts +++ b/src/term-commands/omni.ts @@ -7,9 +7,10 @@ * omni inbox List stored inbound messages (no network). * omni handshake Register this host with omni via a per-host ed25519 keypair. * - * `nats` is imported ONLY dynamically inside `serve` (via omni-runner's default - * factory) so `genie --help` / `status` / `inbox` / `handshake` never touch the - * transport. Output goes to process.stdout/stderr (no console.* in source). + * `@nats-io/transport-node` is imported ONLY dynamically inside `serve` (via + * omni-runner's default factory) so `genie --help` / `status` / `inbox` / + * `handshake` never touch the transport. Output goes to process.stdout/stderr + * (no console.* in source). */ import { generateKeyPairSync } from 'node:crypto'; diff --git a/tests/e2e/v5-lifecycle.sh b/tests/e2e/v5-lifecycle.sh index 00377e805..35d8a63af 100755 --- a/tests/e2e/v5-lifecycle.sh +++ b/tests/e2e/v5-lifecycle.sh @@ -365,8 +365,8 @@ JOBS_AFTER="$(jobs -p || true)" # so we import the source module directly and assert the marker is 0 # after loading it — proving that merely loading/using the runner # never opens a connection; only `runOmniServe` does. A static grep -# backs this up: `nats` is a *dynamic* import inside the factory, not -# a top-level import. +# backs this up: `@nats-io/transport-node` is a *dynamic* import +# inside the factory, not a top-level import. # ============================================================================ step "zero-omni guard (no omni config, transport never initialized)" NO_OMNI_HOME="$SCRATCH/no-omni-home" @@ -414,12 +414,14 @@ bun -e ' if (n !== 0) { process.stderr.write("natsConnectionCount()="+n+" after import\n"); process.exit(1); } ' "$RUNNER_SRC" || die "natsConnectionCount() was non-zero just from loading the runner module" -# Static backstop: `nats` is dynamically imported inside the factory only — never -# a top-level static import — so the module has zero transport cost until serve. +# Static backstop: the Node NATS transport is dynamically imported inside the +# factory only — never a top-level static import — so the module has zero +# transport cost until serve. assert nats-import-is-dynamic-only -grep -qE "await import\('nats'\)" "$RUNNER_SRC" || die "expected a dynamic import('nats') in the runner" -if grep -nE "^import[^\n]*['\"]nats['\"]" "$RUNNER_SRC"; then - die "runner has a top-level static 'nats' import — transport would load eagerly" +grep -qE "await import\('@nats-io/transport-node'\)" "$RUNNER_SRC" \ + || die "expected a dynamic import('@nats-io/transport-node') in the runner" +if grep -nE "^import[^\n]*['\"]@nats-io/transport-node['\"]" "$RUNNER_SRC"; then + die "runner has a top-level static '@nats-io/transport-node' import — transport would load eagerly" fi # ============================================================================ diff --git a/tests/integration/codex-app-server-cwd.test.ts b/tests/integration/codex-app-server-cwd.test.ts new file mode 100644 index 000000000..e2171c8f6 --- /dev/null +++ b/tests/integration/codex-app-server-cwd.test.ts @@ -0,0 +1,469 @@ +/** + * Group B keystone — the black-box app-server CWD proof (DAG gate for Groups A + D). + * + * Drives ONE pinned long-lived real `codex app-server` and proves, per Decision 4 + * and the Group B acceptance criteria, that the MCP server child Codex launches for + * a thread (absolute command, NO cwd override) lands in the thread's EXACT effective + * `process.cwd()` — equal to a Codex-launched control process by string AND OS + * directory identity — for root, nested, symlink-normalized, and linked-worktree + * layouts, plus sequential and concurrent two-repo threads with per-case raw + * request + PID + effective CWD + tagged sentinel, no PID crossing differing + * effective CWD, and no cache-root context. The raw `thread/start.cwd` lives ONLY + * in `CodexCwdEvidence`; the production `CodexHostObservation` carries neither the + * raw request nor any control-only field. + * + * The proof needs a real `codex` binary and a runnable app-server. When the + * environment cannot host it, the suite reports the exact gate and skips rather + * than faking a product surface. + */ + +import { afterAll, describe, expect, test } from 'bun:test'; +import { execFileSync } from 'node:child_process'; +import { + chmodSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + readdirSync, + realpathSync, + rmSync, + statSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { type HostCacheWitness, parseCodexHostObservation } from '../../src/lib/codex-host-observation.js'; +import type { CommandResult } from '../../src/lib/runtime-integrations.js'; +import { CodexCwdEvidence, type CwdCaseEvidence, findPidCrossingDifferingCwd } from '../support/codex-cwd-evidence.js'; + +const CASE_TIMEOUT = 60_000; +const HARNESS_ROOT_PREFIX = 'genie-cwd-evidence-'; + +type FakeAppServerMode = + | 'success' + | 'timeout' + | 'reject' + | 'early-exit' + | 'spawn-error' + | 'success-with-descendant' + | 'timeout-with-descendant'; + +function makeFakeCodex(mode: FakeAppServerMode): { command: string; marker: string; root: string } { + const root = mkdtempSync(join(tmpdir(), 'genie-fake-codex-')); + const command = join(root, 'codex.mjs'); + const marker = join(root, 'app-server.json'); + writeFileSync( + command, + `#!${process.execPath} +import { spawn } from 'node:child_process'; +import { unlinkSync, writeFileSync } from 'node:fs'; +import { dirname } from 'node:path'; +import { createInterface } from 'node:readline'; +import { fileURLToPath } from 'node:url'; +const mode = ${JSON.stringify(mode)}; +if (process.argv.includes('--version')) { + if (mode === 'spawn-error') unlinkSync(fileURLToPath(import.meta.url)); + process.exit(0); +} +const marker = ${JSON.stringify(marker)}; +const baseMode = mode.replace('-with-descendant', ''); +let descendantPid; +if (mode.endsWith('-with-descendant')) { + const descendant = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1 << 30)'], { stdio: 'ignore' }); + descendantPid = descendant.pid; + descendant.unref(); +} +writeFileSync(marker, JSON.stringify({ pid: process.pid, descendantPid, root: dirname(process.env.CODEX_HOME) })); +if (baseMode === 'early-exit') process.exit(19); +if (baseMode === 'reject' || baseMode === 'success') { + const rl = createInterface({ input: process.stdin }); + rl.on('line', (line) => { + const message = JSON.parse(line); + if (message.method === 'initialize') { + const response = baseMode === 'reject' + ? { jsonrpc: '2.0', id: message.id, error: { code: -32000, message: 'forced initialize rejection' } } + : { jsonrpc: '2.0', id: message.id, result: { capabilities: {} } }; + process.stdout.write(JSON.stringify(response) + '\\n'); + } + }); +} +setInterval(() => {}, 1 << 30); +`, + 'utf8', + ); + chmodSync(command, 0o755); + return { command, marker, root }; +} + +function processExists(pid: number): boolean { + try { + process.kill(pid, 0); + return true; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ESRCH') return false; + throw error; + } +} + +async function waitForProcessExit(pid: number, timeoutMs = 2_000): Promise { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + if (!processExists(pid)) return true; + await new Promise((resolve) => setTimeout(resolve, 25)); + } + return !processExists(pid); +} + +function harnessRoots(): Set { + return new Set(readdirSync(tmpdir()).filter((entry) => entry.startsWith(HARNESS_ROOT_PREFIX))); +} + +async function captureLaunchFailure( + command: string, + initializeTimeoutMs: number, + beforeSpawnForTest?: (harnessRoot: string) => void, +): Promise { + try { + await CodexCwdEvidence.launch(command, initializeTimeoutMs, beforeSpawnForTest); + } catch (error) { + return error instanceof Error ? error : new Error(String(error)); + } + throw new Error('fake Codex unexpectedly initialized'); +} + +// Launch ONE pinned app-server for the whole suite and decide support up-front, so +// an environment without a runnable codex app-server skips honestly. +let harness: CodexCwdEvidence | null = null; +let supportReason = 'not probed'; +try { + harness = await CodexCwdEvidence.launch(); + const probeDir = harness.makeRepo('probe', 'PROBE_TOKEN'); + const probe = await harness.startThreadCase('probe', probeDir); + if (probe.sentinelToken !== 'PROBE_TOKEN') { + supportReason = `MCP child did not land the thread cwd sentinel (got ${probe.sentinelToken})`; + await harness.close(); + harness = null; + } else { + supportReason = 'ok'; + } +} catch (error) { + supportReason = error instanceof Error ? error.message : String(error); + await harness?.close(); + harness = null; +} +const supported = harness !== null; + +afterAll(async () => { + await harness?.close(); +}); + +function dirIdentity(dir: string): string { + const st = statSync(dir); + return `${st.dev}:${st.ino}`; +} + +/** child effective CWD equals a Codex-launched control by EXACT string + OS dir identity. */ +async function assertChildEqualsControl(evidence: CwdCaseEvidence): Promise { + const control = await (harness as CodexCwdEvidence).runControl(evidence.rawRequestedCwd); + expect(evidence.childEffectiveCwd).toBe(control.effectiveCwd); + expect(evidence.childCwdIdentity).toBe(control.cwdIdentity); + // No cache-root context ever appears as the launch directory. + expect(evidence.childEffectiveCwd).not.toContain(join('plugins', 'cache')); +} + +test('environment can host the black-box app-server CWD proof', () => { + // Always-run gate record. `supported` is true wherever a real codex app-server + // runs (e.g. the Group B host with codex-cli 0.144.4); elsewhere the proof suite + // below is skipped with this reason instead of faking evidence. + expect(typeof supportReason).toBe('string'); + if (!supported) { + // Surface the exact environmental gate in the assertion message. + expect(`codex-app-server-unavailable: ${supportReason}`).toContain('codex-app-server-unavailable'); + } +}); + +test('pre-spawn setup failure removes its harness root without launching an app-server child', async () => { + const fake = makeFakeCodex('timeout'); + const before = harnessRoots(); + let createdRoot: string | null = null; + try { + const error = await captureLaunchFailure(fake.command, 75, (harnessRoot) => { + createdRoot = harnessRoot; + throw new Error('forced pre-spawn setup failure'); + }); + expect(error.message).toContain('forced pre-spawn setup failure'); + expect(createdRoot).not.toBeNull(); + if (createdRoot === null) throw new Error('pre-spawn seam did not observe the harness root'); + expect(existsSync(createdRoot)).toBe(false); + expect(existsSync(fake.marker)).toBe(false); + expect([...harnessRoots()].filter((root) => !before.has(root))).toEqual([]); + } finally { + rmSync(fake.root, { recursive: true, force: true }); + } +}); + +for (const failureCase of [ + { mode: 'timeout' as const, message: 'initialize timed out' }, + { mode: 'reject' as const, message: 'forced initialize rejection' }, + { mode: 'early-exit' as const, message: 'exited before replying' }, +]) { + test(`failed launch cleans the child and harness root after initialize ${failureCase.mode}`, async () => { + const fake = makeFakeCodex(failureCase.mode); + try { + const error = await captureLaunchFailure(fake.command, 75); + expect(error.message).toContain(failureCase.message); + const record = JSON.parse(readFileSync(fake.marker, 'utf8')) as { pid: number; root: string }; + expect(processExists(record.pid)).toBe(false); + expect(existsSync(record.root)).toBe(false); + } finally { + rmSync(fake.root, { recursive: true, force: true }); + } + }); +} + +test('failed launch removes its harness root after a late spawn error', async () => { + const fake = makeFakeCodex('spawn-error'); + const before = harnessRoots(); + try { + const error = await captureLaunchFailure(fake.command, 75); + expect(error.message).toContain('process error'); + expect([...harnessRoots()].filter((root) => !before.has(root))).toEqual([]); + } finally { + rmSync(fake.root, { recursive: true, force: true }); + } +}); + +test('successful launch cleanup remains idempotent and reaps the child before removing its root', async () => { + const fake = makeFakeCodex('success'); + try { + const launched = await CodexCwdEvidence.launch(fake.command, 250); + const record = JSON.parse(readFileSync(fake.marker, 'utf8')) as { pid: number; root: string }; + expect(processExists(record.pid)).toBe(true); + expect(existsSync(record.root)).toBe(true); + await launched.close(); + await launched.close(); + expect(processExists(record.pid)).toBe(false); + expect(existsSync(record.root)).toBe(false); + } finally { + rmSync(fake.root, { recursive: true, force: true }); + } +}); + +test.skipIf(process.platform === 'win32')( + 'successful close terminates the app-server process group, including spawned descendants', + async () => { + const fake = makeFakeCodex('success-with-descendant'); + try { + const launched = await CodexCwdEvidence.launch(fake.command, 250); + const record = JSON.parse(readFileSync(fake.marker, 'utf8')) as { + pid: number; + descendantPid: number; + root: string; + }; + expect(processExists(record.pid)).toBe(true); + expect(processExists(record.descendantPid)).toBe(true); + + await launched.close(); + + expect(await waitForProcessExit(record.pid)).toBe(true); + expect(await waitForProcessExit(record.descendantPid)).toBe(true); + expect(existsSync(record.root)).toBe(false); + } finally { + rmSync(fake.root, { recursive: true, force: true }); + } + }, +); + +test.skipIf(process.platform === 'win32')( + 'failed initialize terminates spawned descendants before removing the harness root', + async () => { + const fake = makeFakeCodex('timeout-with-descendant'); + try { + const error = await captureLaunchFailure(fake.command, 75); + expect(error.message).toContain('initialize timed out'); + const record = JSON.parse(readFileSync(fake.marker, 'utf8')) as { + pid: number; + descendantPid: number; + root: string; + }; + expect(await waitForProcessExit(record.pid)).toBe(true); + expect(await waitForProcessExit(record.descendantPid)).toBe(true); + expect(existsSync(record.root)).toBe(false); + } finally { + rmSync(fake.root, { recursive: true, force: true }); + } + }, +); + +describe.skipIf(!supported)('black-box CWD evidence — real codex app-server', () => { + test( + 'root + nested launch: child cwd equals the control by string + identity and reads the tagged sentinel', + async () => { + const h = harness as CodexCwdEvidence; + const root = h.makeRepo('repoRoot', 'TOKEN_ROOT'); + const nested = join(root, 'pkg', 'sub'); + mkdirSync(nested, { recursive: true }); + writeFileSync(join(nested, '.genie-cwd-sentinel'), 'TOKEN_NESTED', 'utf8'); + + const rootCase = await h.startThreadCase('root', root); + expect(rootCase.sentinelToken).toBe('TOKEN_ROOT'); + expect(rootCase.childCwdIdentity).toBe(dirIdentity(root)); + await assertChildEqualsControl(rootCase); + + const nestedCase = await h.startThreadCase('nested', nested); + expect(nestedCase.sentinelToken).toBe('TOKEN_NESTED'); + expect(nestedCase.childCwdIdentity).toBe(dirIdentity(nested)); + await assertChildEqualsControl(nestedCase); + }, + CASE_TIMEOUT, + ); + + test( + 'symlink-normalized launch: raw request differs from the effective cwd, which equals the real target', + async () => { + const h = harness as CodexCwdEvidence; + const real = h.makeRepo('symTarget', 'TOKEN_SYM'); + const link = join(h.harnessRootDir(), 'symLink'); + execFileSync('ln', ['-s', real, link]); + + const symCase = await h.startThreadCase('symlink', link); + // The raw requested cwd is the symlink spelling; the child's effective cwd is + // the realpath. Evidence keeps the raw spelling rather than normalizing it away. + expect(symCase.rawRequestedCwd).toBe(link); + expect(symCase.childEffectiveCwd).toBe(realpathSync(link)); + expect(symCase.childEffectiveCwd).not.toBe(symCase.rawRequestedCwd); + expect(symCase.sentinelToken).toBe('TOKEN_SYM'); + expect(symCase.childCwdIdentity).toBe(dirIdentity(real)); + await assertChildEqualsControl(symCase); + }, + CASE_TIMEOUT, + ); + + test( + 'case-normalized launch (where the FS is case-insensitive): a differing-case request lands the same dir', + async () => { + const h = harness as CodexCwdEvidence; + const real = h.makeRepo('caseRepo', 'TOKEN_CASE'); + const upperVariant = join(h.harnessRootDir(), 'CASEREPO'); + // Only meaningful where the host filesystem folds case: probe by identity. + const caseInsensitive = (() => { + try { + return dirIdentity(upperVariant) === dirIdentity(real); + } catch { + return false; + } + })(); + if (!caseInsensitive) { + // Honest gate: a case-sensitive FS (e.g. ext4) cannot exercise case folding. + expect(caseInsensitive).toBe(false); + return; + } + const caseCase = await h.startThreadCase('caseFold', upperVariant); + // The child normalises to the on-disk directory identity and reads its sentinel, + // irrespective of the request's case label; the raw request keeps its spelling. + expect(caseCase.rawRequestedCwd).toBe(upperVariant); + expect(caseCase.sentinelToken).toBe('TOKEN_CASE'); + expect(caseCase.childCwdIdentity).toBe(dirIdentity(real)); + await assertChildEqualsControl(caseCase); + }, + CASE_TIMEOUT, + ); + + test( + 'linked-worktree launch: child stays in the linked worktree, not the main worktree or cache', + async () => { + const h = harness as CodexCwdEvidence; + const gitMain = h.makeRepo('gitMain', 'TOKEN_MAIN'); + const git = (args: string[]) => execFileSync('git', args, { cwd: gitMain, stdio: 'pipe' }); + git(['init', '-q']); + git(['config', 'user.email', 'b@genie.test']); + git(['config', 'user.name', 'genie']); + git(['add', '.']); + git(['commit', '-qm', 'init']); + const linkedWt = join(h.harnessRootDir(), 'linkedWt'); + git(['worktree', 'add', '-q', linkedWt, '-b', 'wt']); + writeFileSync(join(linkedWt, '.genie-cwd-sentinel'), 'TOKEN_LINKED', 'utf8'); + + const wtCase = await h.startThreadCase('linkedwt', linkedWt); + // The child remains in the linked worktree — the linked worktree's sentinel, + // never the main worktree's or a cache root's. + expect(wtCase.sentinelToken).toBe('TOKEN_LINKED'); + expect(wtCase.childEffectiveCwd).toBe(realpathSync(linkedWt)); + expect(wtCase.childCwdIdentity).toBe(dirIdentity(linkedWt)); + expect(wtCase.childCwdIdentity).not.toBe(dirIdentity(gitMain)); + await assertChildEqualsControl(wtCase); + }, + CASE_TIMEOUT, + ); + + test( + 'sequential two-repo threads: each child reads only its own repo sentinel', + async () => { + const h = harness as CodexCwdEvidence; + const repoA = h.makeRepo('seqA', 'SEQ_A'); + const repoB = h.makeRepo('seqB', 'SEQ_B'); + const a = await h.startThreadCase('seqA', repoA); + const b = await h.startThreadCase('seqB', repoB); + expect(a.sentinelToken).toBe('SEQ_A'); + expect(b.sentinelToken).toBe('SEQ_B'); + expect(a.childCwdIdentity).not.toBe(b.childCwdIdentity); + expect(findPidCrossingDifferingCwd([a, b])).toBeNull(); + }, + CASE_TIMEOUT, + ); + + test( + 'concurrent two-repo threads: no cross-talk and no PID crosses a differing effective cwd', + async () => { + const h = harness as CodexCwdEvidence; + const repoA = h.makeRepo('conA', 'CON_A'); + const repoB = h.makeRepo('conB', 'CON_B'); + const cases = await h.startThreadCasesConcurrent([ + { tag: 'conA', requestedCwd: repoA }, + { tag: 'conB', requestedCwd: repoB }, + ]); + const byTag = Object.fromEntries(cases.map((c) => [c.tag, c])); + expect(byTag.conA?.sentinelToken).toBe('CON_A'); + expect(byTag.conB?.sentinelToken).toBe('CON_B'); + // Each child observed exactly its own repo identity. + expect(byTag.conA?.childCwdIdentity).toBe(dirIdentity(repoA)); + expect(byTag.conB?.childCwdIdentity).toBe(dirIdentity(repoB)); + // The PID-reuse invariant: no shared PID across differing effective CWDs. + expect(findPidCrossingDifferingCwd(cases)).toBeNull(); + // Both children equal their own control. + await assertChildEqualsControl(byTag.conA as CwdCaseEvidence); + await assertChildEqualsControl(byTag.conB as CwdCaseEvidence); + }, + CASE_TIMEOUT, + ); + + test('production CodexHostObservation carries neither the raw request nor control-only fields', () => { + // The production observation is limited to runtime/plugin-observable facts. + const cacheFamily: HostCacheWitness = { status: 'present', digest: 'f'.repeat(64), identity: '10:20' }; + const result: CommandResult = { + exitCode: 0, + stdout: JSON.stringify({ installed: [{ pluginId: 'genie@automagik', version: '5.260712.1', enabled: true }] }), + stderr: '', + }; + const observation = parseCodexHostObservation({ result, cacheFamily }); + expect(observation.status).toBe('ok'); + const keys = Object.keys(observation); + // No raw thread/start request field, and no control-process field, ever. + for (const forbidden of [ + 'rawRequestedCwd', + 'rawRequest', + 'threadStartCwd', + 'requestedCwd', + 'control', + 'controlCwd', + 'controlEffectiveCwd', + ]) { + expect(keys).not.toContain(forbidden); + } + // It DOES expose only its own runtime/plugin-observable facts. + expect(keys).toContain('plugin'); + expect(keys).toContain('effectiveChildCwd'); + expect(keys).toContain('cacheFamily'); + }); +}); diff --git a/tests/integration/codex-cross-version-update.test.ts b/tests/integration/codex-cross-version-update.test.ts new file mode 100644 index 000000000..3a96d389f --- /dev/null +++ b/tests/integration/codex-cross-version-update.test.ts @@ -0,0 +1,58 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { validateLiveDogfoodEvidenceFile } from '../../scripts/validate-live-dogfood-evidence.js'; +import { FIXTURE_N, FIXTURE_T, buildDogfoodFixture } from '../support/codex-dogfood-fixture.js'; +import { runDogfoodEntry } from '../support/codex-dogfood-harness.js'; + +let root: string | null = null; +afterEach(() => { + if (root !== null) rmSync(root, { recursive: true, force: true }); + root = null; +}); + +describe('parameterized previous-release to candidate dogfood entry', () => { + test('verified N remains active through repair, then exact T activates and emits reusable evidence', async () => { + root = mkdtempSync(join(tmpdir(), 'genie-dogfood-cross-version-')); + const fixture = buildDogfoodFixture(root); + const result = await runDogfoodEntry(fixture.input, fixture.dependencies); + const lifecycle = result.manifest.lifecycle as Record; + expect(lifecycle.previousVersion).toBe(FIXTURE_N); + expect(lifecycle.candidateVersion).toBe(FIXTURE_T); + expect(lifecycle.stages[1].activeVersion).toBe(FIXTURE_N); + expect(lifecycle.stages[2]).toMatchObject({ + id: 't-delivery-repair', + exit: 2, + activeVersion: FIXTURE_N, + trailer: { code: 'activation-pending', deliveryComplete: true }, + }); + expect(lifecycle.stages[3]).toMatchObject({ id: 'activation-consent', activeVersion: FIXTURE_T }); + expect(lifecycle.delivery).toMatchObject({ + targetVersion: FIXTURE_T, + platformId: fixture.input.platformId, + artifactSha256: lifecycle.artifacts.candidate.artifactSha256, + installedBinarySha256: lifecycle.artifacts.candidate.binarySha256, + canonicalPayloadSha256: lifecycle.artifacts.candidate.payloadSha256, + }); + expect(result.doctor.integrationSummary).toMatchObject({ + schemaVersion: 1, + codexPlugin: { + state: 'current', + installedVersion: FIXTURE_T, + targetVersion: FIXTURE_T, + deliveryComplete: true, + }, + }); + const evidencePath = fixture.input.outputEvidence as string; + expect(validateLiveDogfoodEvidenceFile(evidencePath, join(evidencePath, '..'))).toEqual([]); + expect(readFileSync(evidencePath, 'utf8')).toContain('"evidenceKind": "verified-local-fixture"'); + }, 120_000); + + test('an unavailable or mismatched artifact fails before evidence is emitted', async () => { + root = mkdtempSync(join(tmpdir(), 'genie-dogfood-cross-version-fail-')); + const fixture = buildDogfoodFixture(root); + fixture.input.candidate.artifact = join(root, 'missing.tar.gz'); + await expect(runDogfoodEntry(fixture.input, fixture.dependencies)).rejects.toThrow(/unavailable/); + }); +}); diff --git a/tests/integration/codex-delivery-bootstrap.test.ts b/tests/integration/codex-delivery-bootstrap.test.ts new file mode 100644 index 000000000..90fd7a2ab --- /dev/null +++ b/tests/integration/codex-delivery-bootstrap.test.ts @@ -0,0 +1,309 @@ +/** + * Group D integration proof: the same-version delivery repair publishes ONE + * authenticated record through the REAL deep delivery store, round-trips the full + * attestation tuple back through the store's own parser, and is idempotent on a + * rerun. Every network/attestation seam is STUBBED — this suite runs with no + * `codex` binary and no network, so it is CI-portable by construction. + */ + +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { createHash } from 'node:crypto'; +import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { + type DeliveryRepairSeams, + type InstalledProof, + type PinnedManifest, + type ReobservedTarget, + repairMissingDelivery, +} from '../../src/genie-commands/codex-delivery-repair.js'; +import { + observeCodexActivation, + openCodexActivationStore, + parseReleaseVersion, + scanPhysicalTree, +} from '../../src/lib/codex-activation.js'; +import type { DeliveryFact } from '../../src/lib/codex-activation.js'; +import { verifyDownloadedDeliveryEvidence } from '../../src/lib/codex-delivery-evidence.js'; +import { + type TestDeliveryEvidencePack, + buildTestDeliveryEvidencePack, +} from '../../src/lib/codex-delivery-evidence.test-support.js'; +import { acquireLifecycleLease } from '../../src/lib/codex-lifecycle-lease.js'; + +const T = '5.260722.11'; +const CHANNEL = 'dev'; +const ARTIFACT_DIGEST = 'd'.repeat(64); +const PLATFORM_ID = + process.platform === 'darwin' ? 'darwin-arm64' : process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64-glibc'; +const PLATFORM_TRIPLE = `${process.platform}-${process.arch}`; + +let genieHome: string; + +beforeEach(() => { + genieHome = mkdtempSync(join(tmpdir(), 'genie-delivery-bootstrap-')); + // Minimal canonical payload the store's observation can scan: VERSION + plugin tree. + writeFileSync(join(genieHome, 'VERSION'), `${T}\n`); + mkdirSync(join(genieHome, 'plugins', 'genie'), { recursive: true }); + writeFileSync(join(genieHome, 'plugins', 'genie', 'plugin.json'), '{"name":"genie"}\n'); + mkdirSync(join(genieHome, 'bin'), { recursive: true }); + writeFileSync(join(genieHome, 'bin', 'genie'), 'fixture installed binary\n'); +}); + +afterEach(() => { + rmSync(genieHome, { recursive: true, force: true }); +}); + +/** The canonical installed plugin-tree digest the store computes for this fixture. */ +function canonicalPayloadDigest(): string { + const tree = scanPhysicalTree(join(genieHome, 'plugins', 'genie')); + if (tree.status !== 'ok' || tree.digest === undefined) + throw new Error(`fixture payload not scannable: ${tree.status}`); + return tree.digest; +} + +function installedProof(): InstalledProof { + return { + version: T, + pluginTreeSha256: canonicalPayloadDigest(), + binarySha256: installedBinaryDigest(), + deliveryRoot: realpathSync(genieHome), + }; +} + +function installedBinaryDigest(): string { + return createHash('sha256') + .update(readFileSync(join(genieHome, 'bin', 'genie'))) + .digest('hex'); +} + +interface SeamConfig { + installedGeneration: string | null; + fetchManifest?: () => Promise; +} + +function evidencePack(version = T): TestDeliveryEvidencePack { + return buildTestDeliveryEvidencePack({ + descriptor: { + version, + channel: CHANNEL, + platformId: PLATFORM_ID, + platformTriple: PLATFORM_TRIPLE, + releaseTag: `v${version}`, + releaseName: `genie-${version}-${PLATFORM_ID}.tar.gz`, + artifactSha256: ARTIFACT_DIGEST, + installedBinarySha256: installedBinaryDigest(), + canonicalPayloadSha256: canonicalPayloadDigest(), + }, + }); +} + +function pinnedManifest(version = T): PinnedManifest { + const pack = evidencePack(version); + return { + ...(JSON.parse(pack.manifestBytes) as Omit), + manifestBytes: pack.manifestBytes, + manifestSha256: pack.descriptor.releaseManifestSha256, + }; +} + +/** Read the on-disk delivery fact via the store's own parser and offline re-verifier. */ +function readDeliveryFact(pack: TestDeliveryEvidencePack): DeliveryFact { + return observeCodexActivation({ + genieHome, + command: null, + deliveryEvidenceVerification: pack.dependencies, + }).delivery; +} + +/** Real store + real lease; stubbed network/attestation. The lease MUST be released by the caller. */ +function makeRealStoreSeams(config: SeamConfig): { seams: DeliveryRepairSeams; release: () => void } { + const pack = evidencePack(); + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + if (!lease.ok) throw new Error('could not acquire the update-delivery lease in the fixture'); + const reobserve = (): ReobservedTarget => ({ + installedGeneration: config.installedGeneration, + canonicalVersion: T, + canonicalPayloadSha256: canonicalPayloadDigest(), + installedBinarySha256: installedBinaryDigest(), + deliveryRoot: realpathSync(genieHome), + }); + const seams: DeliveryRepairSeams = { + readDeliveryFact: () => readDeliveryFact(pack), + observeInstalled: installedProof, + fetchManifest: config.fetchManifest ?? (async () => pinnedManifest()), + downloadAndVerify: async () => ({ + tarballPath: '/stub/tarball.tar.gz', + descriptorBytes: pack.descriptorBytes, + bundleBytes: pack.bundleBytes, + }), + hashArtifact: () => ARTIFACT_DIGEST, + proveCandidate: async () => ({ + version: T, + pluginTreeSha256: canonicalPayloadDigest(), + binarySha256: installedBinaryDigest(), + }), + verifyEvidence: (input) => verifyDownloadedDeliveryEvidence(input, pack.dependencies), + reobserve, + store: openCodexActivationStore({ genieHome, deliveryEvidenceVerification: pack.dependencies }), + lease, + }; + return { seams, release: () => lease.release() }; +} + +describe('codex-delivery-bootstrap — repair round-trips one authenticated record through the real store', () => { + test('old-parent/no-record: publishes one bound record readable as present, handoff activation-pending', async () => { + const { seams, release } = makeRealStoreSeams({ installedGeneration: '5.260722.1' }); + try { + const outcome = await repairMissingDelivery( + { + channel: CHANNEL, + targetVersion: T, + platformTriple: PLATFORM_TRIPLE, + platformId: PLATFORM_ID, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM_ID}.tar.gz`, + }, + seams, + ); + expect(outcome.kind).toBe('published'); + if (outcome.kind === 'published') expect(outcome.handoff).toBe('activation-pending'); + } finally { + release(); + } + // The record is now on disk and parses through the store's OWN parser (extended schema round-trip). + const readState = readDeliveryFact(evidencePack()); + expect(readState.status).toBe('present'); + if (readState.status === 'present') { + expect(readState.record.targetVersion).toBe(T); + expect(readState.record.artifactSha256).toBe(ARTIFACT_DIGEST); + expect(readState.record.releaseManifestSha256).toBe(evidencePack().descriptor.releaseManifestSha256); + expect(readState.record.platformTriple).toBe(PLATFORM_TRIPLE); + expect(readState.record.releaseTag).toBe(`v${T}`); + expect(parseReleaseVersion(readState.record.targetVersion)).not.toBeNull(); + } + }); + + test('rerun after publication is already-matching with no second download', async () => { + const first = makeRealStoreSeams({ installedGeneration: T }); + try { + await repairMissingDelivery( + { + channel: CHANNEL, + targetVersion: T, + platformTriple: PLATFORM_TRIPLE, + platformId: PLATFORM_ID, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM_ID}.tar.gz`, + }, + first.seams, + ); + } finally { + first.release(); + } + // Second run: a download seam that fails proves the fast path never reaches it. + const second = makeRealStoreSeams({ installedGeneration: T }); + let downloadCalled = false; + second.seams.downloadAndVerify = async () => { + downloadCalled = true; + throw new Error('rerun must not download'); + }; + try { + const outcome = await repairMissingDelivery( + { + channel: CHANNEL, + targetVersion: T, + platformTriple: PLATFORM_TRIPLE, + platformId: PLATFORM_ID, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM_ID}.tar.gz`, + }, + second.seams, + ); + expect(outcome).toEqual({ kind: 'already-matching' }); + expect(downloadCalled).toBe(false); + } finally { + second.release(); + } + }); + + test('a channel advance under the lease publishes nothing and leaves the store record absent', async () => { + const { seams, release } = makeRealStoreSeams({ + installedGeneration: '5.260722.1', + fetchManifest: async () => pinnedManifest('5.260722.12'), + }); + try { + const outcome = await repairMissingDelivery( + { + channel: CHANNEL, + targetVersion: T, + platformTriple: PLATFORM_TRIPLE, + platformId: PLATFORM_ID, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM_ID}.tar.gz`, + }, + seams, + ); + expect(outcome.kind).toBe('channel-advanced'); + } finally { + release(); + } + // No stale record was minted. + expect(readDeliveryFact(evidencePack()).status).toBe('absent'); + }); + + test('a tampered persisted platform is treated as non-matching and republished', async () => { + // Publish a good record first. + const first = makeRealStoreSeams({ installedGeneration: T }); + try { + await repairMissingDelivery( + { + channel: CHANNEL, + targetVersion: T, + platformTriple: PLATFORM_TRIPLE, + platformId: PLATFORM_ID, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM_ID}.tar.gz`, + }, + first.seams, + ); + } finally { + first.release(); + } + const recordPath = join(genieHome, '.codex-plugin-delivery-record.json'); + const tamperedRecord = JSON.parse(readFileSync(recordPath, 'utf8')) as Record; + tamperedRecord.platformId = PLATFORM_ID === 'darwin-arm64' ? 'linux-arm64' : 'darwin-arm64'; + tamperedRecord.platformTriple = PLATFORM_TRIPLE === 'darwin-arm64' ? 'linux-arm64' : 'darwin-arm64'; + writeFileSync(recordPath, `${JSON.stringify(tamperedRecord, null, 2)}\n`); + + // Repair the original pinned tuple: the record/evidence mismatch forces publication. + const second = makeRealStoreSeams({ installedGeneration: T }); + let downloaded = false; + const originalDownload = second.seams.downloadAndVerify; + second.seams.downloadAndVerify = async (target) => { + downloaded = true; + return originalDownload(target); + }; + try { + const outcome = await repairMissingDelivery( + { + channel: CHANNEL, + targetVersion: T, + platformTriple: PLATFORM_TRIPLE, + platformId: PLATFORM_ID, + releaseTag: `v${T}`, + releaseName: `genie-${T}-${PLATFORM_ID}.tar.gz`, + }, + second.seams, + ); + expect(outcome.kind).toBe('published'); + expect(downloaded).toBe(true); // the non-match forced a real download, not the fast path + } finally { + second.release(); + } + const readState = readDeliveryFact(evidencePack()); + expect(readState.status).toBe('present'); + if (readState.status === 'present') expect(readState.record.platformTriple).toBe(PLATFORM_TRIPLE); + }); +}); diff --git a/tests/integration/codex-lifecycle-pty.test.ts b/tests/integration/codex-lifecycle-pty.test.ts new file mode 100644 index 000000000..883d9b49f --- /dev/null +++ b/tests/integration/codex-lifecycle-pty.test.ts @@ -0,0 +1,454 @@ +/** + * Group E deliverable 5 — the real-PTY lifecycle flow: + * + * repair handoff (missing delivery record → typed refusal with the one + * recovery command) → record published (as update/install repair would) → + * `genie setup --codex` under a REAL pty (consent answered on /dev/tty) → + * explicit new-task instruction → `genie doctor --json` state `current`, + * plus the real PATH advisory, stale historical config, route collision / + * shadowing, context states, and a hard query failure — all through the + * actual setup/doctor command implementations, a stateful fake `codex` + * executable, and the production GENIE_HOME payload layout (no canonical-root + * override). An unshipped driver injects only deterministic bundle-signature + * verification because production deliberately exposes no CLI/env bypass. + * + * PTY allocation uses the platform `script(1)`; the suite skips cleanly where + * a pty cannot be allocated (no `script`, or Windows). The fake codex lives + * OUTSIDE the repo/CWD so trusted-executable validation applies unchanged. + */ + +import { afterAll, beforeAll, describe, expect, test } from 'bun:test'; +import { execFileSync } from 'node:child_process'; +import { createHash } from 'node:crypto'; +import { + chmodSync, + cpSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + readdirSync, + realpathSync, + rmSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { verifyDownloadedDeliveryEvidence } from '../../src/lib/codex-delivery-evidence.js'; +import { buildTestDeliveryEvidencePack } from '../../src/lib/codex-delivery-evidence.test-support.js'; +import { acquireLifecycleLease } from '../../src/lib/codex-lifecycle-lease.js'; + +const REPO_ROOT = join(import.meta.dir, '..', '..'); +const PRODUCTION_GENIE_CLI = join(REPO_ROOT, 'src', 'genie.ts'); +const LIFECYCLE_TEST_RUNNER = join(REPO_ROOT, 'tests', 'support', 'codex-lifecycle-test-runner.ts'); +const REAL_SESSION_CONTEXT = join(REPO_ROOT, 'plugins', 'genie', 'scripts', 'session-context.cjs'); +const REAL_MCP_LAUNCHER = join(REPO_ROOT, 'plugins', 'genie', 'scripts', 'mcp-launcher.cjs'); +const REAL_CODEX_AGENTS = join(REPO_ROOT, 'plugins', 'genie', 'codex-agents'); +const REAL_SKILLS = join(REPO_ROOT, 'plugins', 'genie', 'skills'); +const TARGET = '5.260722.1'; +const OLD = '5.260711.9'; +const PLATFORM_ID = + process.platform === 'darwin' ? 'darwin-arm64' : process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64-glibc'; +const PLATFORM_TRIPLE = `${process.platform}-${process.arch}`; + +// PTY driver: GNU `script -qec` forwards piped stdin to the pty on Linux (the +// CI condition); macOS `script` refuses a non-tty stdin (tcgetattr), so darwin +// drives the pty through the preinstalled `expect` instead. +const SCRIPT_BIN = Bun.which('script'); +const EXPECT_BIN = Bun.which('expect'); +const CAN_PTY = + process.platform === 'linux' ? SCRIPT_BIN !== null : process.platform === 'darwin' ? EXPECT_BIN !== null : false; + +let root: string; +let genieHome: string; +let codexHome: string; +let repo: string; +let binDir: string; +let stateDir: string; +let fakeCodex: string; +let childEnv: Record; + +function deliveryRecordPath(): string { + return join(genieHome, '.codex-plugin-delivery-record.json'); +} + +/** Stage the whole fixture once; individual tests advance the lifecycle in order. */ +beforeAll(() => { + if (!CAN_PTY) return; + root = mkdtempSync(join(tmpdir(), 'genie-pty-lifecycle-')); + genieHome = join(root, 'genie-home'); + codexHome = join(root, 'codex-home'); + repo = join(root, 'repo'); + binDir = join(root, 'bin'); + stateDir = join(root, 'codex-state'); + for (const dir of [genieHome, codexHome, repo, binDir, stateDir, join(root, 'home'), join(root, 'tmp')]) { + mkdirSync(dir, { recursive: true }); + } + + // Canonical payload at the PRODUCTION location: $GENIE_HOME/plugins/genie + // (+ VERSION beside it) — no canonicalRoot override anywhere in this suite. + const payload = join(genieHome, 'plugins', 'genie'); + mkdirSync(join(payload, 'scripts'), { recursive: true }); + mkdirSync(join(payload, 'hooks'), { recursive: true }); + cpSync(REAL_SESSION_CONTEXT, join(payload, 'scripts', 'session-context.cjs')); + cpSync(REAL_MCP_LAUNCHER, join(payload, 'scripts', 'mcp-launcher.cjs')); + cpSync(REAL_CODEX_AGENTS, join(payload, 'codex-agents'), { recursive: true }); + cpSync(REAL_SKILLS, join(payload, 'skills'), { recursive: true }); + writeFileSync(join(payload, 'README.md'), 'genie codex payload\n'); + writeFileSync(join(payload, 'hooks', 'codex-hooks.json'), '{"hooks":{}}\n'); + writeFileSync(join(genieHome, 'VERSION'), `${TARGET}\n`); + mkdirSync(join(genieHome, 'bin'), { recursive: true }); + const fixtureBinary = join(genieHome, 'bin', 'genie'); + writeFileSync( + fixtureBinary, + `#!/bin/sh\nexec ${JSON.stringify(process.execPath)} ${JSON.stringify(PRODUCTION_GENIE_CLI)} "$@"\n`, + ); + chmodSync(fixtureBinary, 0o755); + + // Codex home: enabled plugin flag + an OLD physical cache generation so the + // pre-activation state is genuinely activation-pending. + writeFileSync(join(codexHome, 'config.toml'), `[plugins."genie@automagik"]\nenabled = true\n`); + const familyDir = join(codexHome, 'plugins', 'cache', 'automagik', 'genie'); + mkdirSync(join(familyDir, OLD), { recursive: true }); + writeFileSync(join(familyDir, OLD, 'README.md'), 'old generation\n'); + writeFileSync(join(stateDir, 'registered'), `${OLD}\n`); + + // The stateful fake codex CLI (outside the repo → passes trusted-executable). + fakeCodex = join(binDir, 'codex'); + writeFileSync( + fakeCodex, + `#!/bin/bash +set -u +cmd="$*" +if [ "\${1:-}" = "--version" ]; then echo "codex 0.0.0-fake"; exit 0; fi +if [ "$cmd" = "plugin list --json" ]; then + if [ -n "\${FAKE_CODEX_ADVISORY:-}" ]; then printf '\\033[33m%s\\033[0m\\n' "$FAKE_CODEX_ADVISORY" >&2; fi + if [ -n "\${FAKE_CODEX_EXIT:-}" ]; then echo "hard failure" >&2; exit "$FAKE_CODEX_EXIT"; fi + ver=$(cat "$FAKE_CODEX_STATE/registered" 2>/dev/null | tr -d '\\n') + enabled=true + grep -q 'enabled = false' "$CODEX_HOME/config.toml" 2>/dev/null && enabled=false + if [ -z "$ver" ]; then echo '{"installed":[]}'; else + printf '{"installed":[{"pluginId":"genie@automagik","enabled":%s,"version":"%s"}]}\\n' "$enabled" "$ver" + fi + exit 0 +fi +if [ "$cmd" = "plugin add genie@automagik --json" ]; then + target_dir="$CODEX_HOME/plugins/cache/automagik/genie/$FAKE_CODEX_TARGET" + mkdir -p "$target_dir" + cp -R "$GENIE_HOME/plugins/genie/." "$target_dir/" + printf '%s\\n' "$FAKE_CODEX_TARGET" > "$FAKE_CODEX_STATE/registered" + printf '[plugins."genie@automagik"]\\nenabled = true\\n' > "$CODEX_HOME/config.toml" + echo '{}' + exit 0 +fi +echo '{}' +exit 0 +`, + ); + chmodSync(fakeCodex, 0o755); + + // A real project repo, initialized + trusted, whose route genie init owns. + execFileSync('git', ['init', '-q'], { cwd: repo }); + execFileSync('git', ['config', 'user.email', 'pty@test.local'], { cwd: repo }); + execFileSync('git', ['config', 'user.name', 'PTY Test'], { cwd: repo }); + execFileSync('git', ['commit', '--allow-empty', '-q', '-m', 'seed'], { cwd: repo }); + + childEnv = { + ...process.env, + HOME: join(root, 'home'), + GENIE_HOME: genieHome, + CODEX_HOME: codexHome, + CLAUDE_CONFIG_DIR: join(root, 'home', 'claude'), + HERMES_HOME: join(root, 'home', 'hermes'), + TMPDIR: join(root, 'tmp'), + PATH: `${binDir}:${process.env.PATH ?? ''}`, + FAKE_CODEX_STATE: stateDir, + FAKE_CODEX_TARGET: TARGET, + TERM: 'xterm', + } as Record; + for (const key of ['CI', 'CODEX_THREAD_ID', 'GENIE_BUNDLE_ROOT', 'GENIE_WORKER', 'FAKE_CODEX_ADVISORY']) { + delete childEnv[key]; + } + + // Trust the repo in the global Codex config (Group E trust classifier). + // Codex records whatever spelling it saw; cover the logical AND physical + // spelling of the tmp path (macOS /var vs /private/var). + const repoSpellings = [...new Set([repo, realpathSync(repo)])]; + writeFileSync( + join(codexHome, 'config.toml'), + `[plugins."genie@automagik"]\nenabled = true\n${repoSpellings + .map((spelling) => `[projects."${spelling}"]\ntrust_level = "trusted"\n`) + .join('')}`, + ); + + // `genie init` owns the marker-managed project route (the ONLY command that + // may create it — authority matrix row 1). Route-only, delivery-independent. + const init = Bun.spawnSync([process.execPath, PRODUCTION_GENIE_CLI, 'init'], { + cwd: repo, + env: childEnv, + stdout: 'pipe', + stderr: 'pipe', + timeout: 60_000, + }); + if (init.exitCode !== 0) { + throw new Error(`genie init failed (${init.exitCode}): ${init.stdout.toString()}${init.stderr.toString()}`); + } +}); + +afterAll(() => { + if (root !== undefined) rmSync(root, { recursive: true, force: true }); +}); + +function runCli( + args: string[], + opts: { env?: Record; cwd?: string } = {}, +): { exitCode: number; stdout: string; stderr: string } { + const proc = Bun.spawnSync([process.execPath, LIFECYCLE_TEST_RUNNER, ...args], { + cwd: opts.cwd ?? repo, + env: opts.env ?? childEnv, + stdout: 'pipe', + stderr: 'pipe', + timeout: 60_000, + }); + return { exitCode: proc.exitCode, stdout: proc.stdout.toString(), stderr: proc.stderr.toString() }; +} + +/** Run the CLI under a REAL pty; `input` is delivered to the pty (read via /dev/tty). */ +function runPty( + args: string[], + input: string, + opts: { env?: Record } = {}, +): { exitCode: number; output: string } { + const cli = [process.execPath, LIFECYCLE_TEST_RUNNER, ...args]; + if (process.platform === 'darwin') { + // expect(1): spawn on a pty, queue the consent answer, exit with the + // child's status. Braced words keep tmp paths literal (no spaces in them). + const spawnWords = cli.map((part) => `{${part}}`).join(' '); + const sendWords = input.replaceAll('\n', '\\r'); + const expectScript = [ + 'set timeout 120', + `spawn -noecho ${spawnWords}`, + `send -- "${sendWords}"`, + 'expect eof', + 'catch wait result', + 'exit [lindex $result 3]', + ].join('\n'); + const proc = Bun.spawnSync([EXPECT_BIN as string, '-c', expectScript], { + cwd: repo, + env: opts.env ?? childEnv, + stdout: 'pipe', + stderr: 'pipe', + timeout: 150_000, + }); + return { exitCode: proc.exitCode, output: proc.stdout.toString() + proc.stderr.toString() }; + } + const argv = [ + SCRIPT_BIN as string, + '-qec', + cli.map((part) => `'${part.replaceAll("'", `'\\''`)}'`).join(' '), + '/dev/null', + ]; + const proc = Bun.spawnSync(argv, { + cwd: repo, + env: opts.env ?? childEnv, + stdin: Buffer.from(input), + stdout: 'pipe', + stderr: 'pipe', + timeout: 120_000, + }); + return { exitCode: proc.exitCode, output: proc.stdout.toString() + proc.stderr.toString() }; +} + +interface DoctorJson { + ok: boolean; + checks: Array<{ name: string; status: string; detail?: string; advisory?: string; routeLayers?: unknown[] }>; + integrationSummary?: { codexPlugin: { state: string; deliveryComplete: boolean; recovery: string } }; +} + +function doctorJson(opts: { env?: Record; cwd?: string } = {}): { + json: DoctorJson; + exitCode: number; + raw: string; +} { + const { exitCode, stdout } = runCli(['doctor', '--json'], opts); + return { json: JSON.parse(stdout) as DoctorJson, exitCode, raw: stdout }; +} + +describe.skipIf(!CAN_PTY)('codex lifecycle over a real PTY (Group E deliverable 5)', () => { + test('stage 1 — missing delivery record: setup refuses BEFORE consent with the one recovery command', () => { + expect(existsSync(deliveryRecordPath())).toBe(false); + const { exitCode, stdout, stderr } = runCli(['setup', '--codex']); + expect(exitCode).toBe(1); + expect(stderr).toContain('delivery record is absent'); + expect(stderr).toContain('genie update'); + expect(stdout).not.toContain('Codex configuration saved'); + // Doctor agrees, from the same facts: delivery-incomplete, never current. + const { json, exitCode: doctorExit } = doctorJson(); + expect(json.integrationSummary?.codexPlugin.state).toBe('delivery-incomplete'); + expect(json.integrationSummary?.codexPlugin.deliveryComplete).toBe(false); + expect(doctorExit).toBe(1); + }, 120_000); + + test('stage 2 — record published (as update/install repair would): pending, not incomplete', async () => { + // Publish through the REAL deep store under the real lifecycle lease — + // the same API the repair path drives; setup/doctor never mint records. + const { openCodexActivationStore, observeCodexActivation } = await import( + '../../src/lib/codex-activation-executor.js' + ); + const snapshot = observeCodexActivation({ genieHome, codexHome, command: fakeCodex }); + if (snapshot.canonical.status !== 'ok') throw new Error(`canonical not ok: ${JSON.stringify(snapshot.canonical)}`); + const pack = buildTestDeliveryEvidencePack({ + descriptor: { + version: TARGET, + channel: 'stable', + platformId: PLATFORM_ID, + platformTriple: PLATFORM_TRIPLE, + releaseTag: `v${TARGET}`, + releaseName: `genie-${TARGET}-${PLATFORM_ID}.tar.gz`, + artifactSha256: 'b'.repeat(64), + installedBinarySha256: createHash('sha256') + .update(readFileSync(join(genieHome, 'bin', 'genie'))) + .digest('hex'), + canonicalPayloadSha256: snapshot.canonical.digest, + }, + }); + const evidence = verifyDownloadedDeliveryEvidence(pack.input, pack.dependencies); + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + if (!lease.ok) throw new Error(`lease: ${lease.detail}`); + try { + const store = openCodexActivationStore({ + genieHome, + codexHome, + command: fakeCodex, + deliveryEvidenceVerification: pack.dependencies, + }); + store.publishDelivery(lease, { + evidence, + deliveryRoot: realpathSync(genieHome), + }); + } finally { + lease.release(); + } + expect(existsSync(deliveryRecordPath())).toBe(true); + const { json } = doctorJson(); + expect(json.integrationSummary?.codexPlugin.state).toBe('activation-pending'); + expect(json.integrationSummary?.codexPlugin.deliveryComplete).toBe(true); + }, 120_000); + + test('stage 3 — PTY consent activates: add once, enabled restored, journal cleared, new-task instruction printed', () => { + const { exitCode, output } = runPty(['setup', '--codex'], 'yes\n'); + expect(output).toContain('Activated Codex plugin v'); + expect(output).toContain('new Codex task'); + expect(output).toContain('Codex configuration saved'); + expect(exitCode).toBe(0); + // Normal journal clearing: no refresh-intent journal left behind. + expect(existsSync(join(genieHome, '.codex-plugin-refresh-intent.json'))).toBe(false); + // The cache now physically carries the target generation. + expect(existsSync(join(codexHome, 'plugins', 'cache', 'automagik', 'genie', TARGET, 'README.md'))).toBe(true); + expect(readFileSync(join(stateDir, 'registered'), 'utf8').trim()).toBe(TARGET); + }, 180_000); + + test('stage 4 — doctor ends current: consistent human/JSON/exit, project context surfaced', () => { + const { json, exitCode } = doctorJson(); + expect(json.integrationSummary?.codexPlugin).toMatchObject({ state: 'current', deliveryComplete: true }); + // The host bun version is environmental; every OTHER check must be clean. + const failing = json.checks.filter((check) => check.status === 'fail' && !check.name.startsWith('bun')); + expect(JSON.stringify(failing, null, 2)).toBe('[]'); + const bunFails = json.checks.some((check) => check.status === 'fail' && check.name.startsWith('bun')); + expect(exitCode).toBe(bunFails ? 1 : 0); + // The typed context state, end-to-end: init scaffolds no genie.db, so a + // Codex task would see the typed database-unavailable error (warn) — + // never a healthy empty board and never a hard context failure. + const context = json.checks.find((check) => check.name === 'Codex project context'); + expect(context?.status).toBe('warn'); + expect(context?.detail).toContain('project-database-unavailable'); + // Route: the marker-owned project route from `genie init` + trusted repo. + const human = runCli(['doctor']); + expect(human.stdout).toContain('Codex integration:'); + expect(human.exitCode).toBe(bunFails ? 1 : 0); + }, 120_000); + + test('stage 5 — real PATH advisory: one ANSI-free JSON, advisory rider, still current (Decision 11)', () => { + const env = { ...childEnv, FAKE_CODEX_ADVISORY: 'WARN: PATH does not include codex shims' }; + const { json, exitCode, raw } = doctorJson({ env }); + expect(json.integrationSummary?.codexPlugin.state).toBe('current'); + const bunFails = json.checks.some((check) => check.status === 'fail' && check.name.startsWith('bun')); + expect(exitCode).toBe(bunFails ? 1 : 0); + const cli = json.checks.find((check) => check.name === 'Codex CLI'); + expect(cli?.advisory).toBe('WARN: PATH does not include codex shims'); + expect(raw).not.toContain('\x1b[33m'); + }, 120_000); + + test('stage 6 — stale historical config: record removed → no green banner despite configured history', () => { + const recordBytes = readFileSync(deliveryRecordPath()); + rmSync(deliveryRecordPath()); + try { + const { exitCode, stdout, stderr } = runCli(['setup', '--codex']); + expect(exitCode).toBe(1); + expect(stdout).not.toContain('Codex configuration saved'); + expect(stderr).toContain('delivery record is absent'); + } finally { + writeFileSync(deliveryRecordPath(), recordBytes); + } + }, 120_000); + + test('stage 6b — a stale activation journal is quarantined under one consent, then re-observed (live-QA regression)', () => { + // The 2026-07-23 live-QA failure: a leftover journal from a prior + // generation made consent mint a quarantine permit that setup fed to the + // activation executor ("permit lacks activation capability"), leaving the + // prescribed recovery unreachable. One consented run must now quarantine + // the journal, re-observe, and land on the truthful current state. + const journalPath = join(genieHome, '.codex-plugin-refresh-intent.json'); + writeFileSync(journalPath, 'not json at all\n'); + const { exitCode, output } = runPty(['setup', '--codex'], 'yes\n'); + expect(output).toContain('Quarantined stale activation journal'); + expect(output).toContain('already current'); + expect(output).not.toContain('permit lacks activation capability'); + expect(exitCode).toBe(0); + expect(existsSync(journalPath)).toBe(false); + // The journal was moved aside (content-addressed), not destroyed. + const quarantinedSiblings = readdirSync(genieHome).filter((name) => + name.startsWith('.codex-plugin-refresh-intent.json.invalid-'), + ); + expect(quarantinedSiblings.length).toBe(1); + }, 180_000); + + test('stage 7 — route collision and nested shadowing are typed doctor failures, never green', () => { + // Unmanaged same-key route: replace the project config with a user-owned one. + const projectConfig = join(repo, '.codex', 'config.toml'); + mkdirSync(join(repo, '.codex'), { recursive: true }); + const prior = existsSync(projectConfig) ? readFileSync(projectConfig) : null; + writeFileSync(projectConfig, '[mcp_servers.genie]\ncommand = "/usr/local/bin/other"\nargs = []\n'); + try { + const collision = doctorJson(); + const route = collision.json.checks.find((check) => check.name === 'Codex Genie MCP registration'); + expect(route?.status).toBe('fail'); + expect(JSON.stringify(route?.routeLayers ?? [])).toContain('route-collision'); + } finally { + if (prior === null) rmSync(projectConfig); + else writeFileSync(projectConfig, prior); + } + + // Nested shadowing: a nearer .codex/config.toml between root and CWD. + const nested = join(repo, 'packages', 'web'); + mkdirSync(join(nested, '.codex'), { recursive: true }); + writeFileSync(join(nested, '.codex', 'config.toml'), '[mcp_servers.genie]\ncommand = "/elsewhere"\n'); + try { + const shadowed = doctorJson({ cwd: nested }); + const route = shadowed.json.checks.find((check) => check.name === 'Codex Genie MCP registration'); + expect(route?.status).toBe('fail'); + expect(JSON.stringify(route?.routeLayers ?? [])).toContain('route-shadowed'); + } finally { + rmSync(join(repo, 'packages'), { recursive: true, force: true }); + } + }, 120_000); + + test('stage 8 — a hard query failure fails BOTH surfaces consistently (no PASS + query-failed split)', () => { + const env = { ...childEnv, FAKE_CODEX_EXIT: '7' }; + const { json, exitCode } = doctorJson({ env }); + expect(json.integrationSummary?.codexPlugin.state).toBe('query-failed'); + const plugin = json.checks.find((check) => check.name === 'Codex Genie plugin'); + expect(plugin?.status).toBe('fail'); + expect(exitCode).toBe(1); + }, 120_000); +}); diff --git a/tests/integration/codex-lifecycle-race.test.ts b/tests/integration/codex-lifecycle-race.test.ts index 0a2e33790..20f0a9a63 100644 --- a/tests/integration/codex-lifecycle-race.test.ts +++ b/tests/integration/codex-lifecycle-race.test.ts @@ -4,8 +4,8 @@ * The lifecycle commands serialize through ONE Codex lifecycle lease, so a * concurrent update+install or update+rollback must produce exactly one winner; * the loser gets the typed `codex-lifecycle-busy` refusal and mutates nothing. - * A's `codex-lifecycle-lease.test.ts` proves the O_EXCL primitive for a single - * kind — this proves CROSS-KIND exclusion: `update-delivery` (update), + * A's `codex-lifecycle-lease.test.ts` proves private-record O_EXCL plus atomic + * stable publication for a single kind — this proves CROSS-KIND exclusion: `update-delivery` (update), * `install-converge` (install) and `rollback` all contend on the same lease * file under one GENIE_HOME, so no two lifecycle commands can hold it at once. * @@ -14,17 +14,21 @@ */ import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; -import { existsSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; +import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; +import { acquireLifecycleLease as acquireAgentLifecycleLease, lifecycleLockPath } from '../../src/lib/agent-sync.js'; import { acquireLifecycleLease } from '../../src/lib/codex-lifecycle-lease.js'; const LEASE_MODULE = join(import.meta.dir, '..', '..', 'src', 'lib', 'codex-lifecycle-lease.ts'); const INSTALL_MODULE = join(import.meta.dir, '..', '..', 'src', 'genie-commands', 'install.ts'); +const UPDATE_MODULE = join(import.meta.dir, '..', '..', 'src', 'genie-commands', 'update.ts'); let home: string; let script: string; let installScript: string; +let updateScript: string; +let updateFailureScript: string; beforeEach(() => { home = mkdtempSync(join(tmpdir(), 'genie-lifecycle-race-')); @@ -54,23 +58,91 @@ beforeEach(() => { installScript, [ 'process.env.GENIE_HOME = process.argv[2];', + "const { writeFileSync } = await import('node:fs');", `const mod = await import(${JSON.stringify(INSTALL_MODULE)});`, + 'const mutationPath = process.argv[3];', + "const mark = (phase) => writeFileSync(mutationPath, phase + '\\n');", 'const noopLease = () => ({ path: process.argv[2] + "/.agent-sync.lock", release: () => {} });', - 'mod.installCommand(', + 'await mod.installCommand(', " { integrations: 'codex' },", - ' () => undefined,', // runV4Cleanup - ' () => undefined,', // normalizeLayout - ' () => undefined,', // runSync - ' () => [],', // runIntegrations (mutation seam — must never run when busy) + " () => mark('cleanup'),", // runV4Cleanup + " () => mark('normalize'),", // normalizeLayout + " () => mark('sync'),", // runSync + " () => { mark('integrations'); return []; },", // mutation seam — must never run when busy ' noopLease,', // agent-sync lease (free) ' undefined,', // acquireCodexLease -> real default acquisition - ' () => undefined,', // writeConsent + " () => mark('consent'),", // writeConsent ' () => null,', // classifyCodexInstall (no codex CLI probe) ');', "process.stdout.write(process.exitCode === 2 ? 'BUSY' : 'WON');", '', ].join('\n'), ); + updateScript = join(home, 'update-cmd.ts'); + writeFileSync( + updateScript, + [ + 'process.env.GENIE_HOME = process.argv[2];', + "const { writeFileSync } = await import('node:fs');", + `const mod = await import(${JSON.stringify(UPDATE_MODULE)});`, + 'const mutationPath = process.argv[3];', + "const mark = (phase) => writeFileSync(mutationPath, phase + '\\n');", + 'const manifest = {', + ' schema_version: 1,', + " channel: 'stable',", + " version: '5.260723.8',", + " released_at: '2026-07-23T00:00:00Z',", + " tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v5.260723.8',", + " platforms: ['darwin-arm64'],", + ' manifestBytes: \'{"version":"5.260723.8"}\\n\',', + " manifestSha256: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',", + '};', + 'await mod.updateCommand({ yes: true, stable: true }, {', + ' fetchManifest: async () => manifest,', + " readInstalledVersion: () => '5.260700.1',", + " resolvePlatform: () => 'darwin-arm64',", + ' acquireLease: () => ({ path: process.argv[2] + "/.agent-sync.lock", release: () => {} }),', + " recoverPendingState: () => mark('recovery'),", + " persistSelectedChannel: async () => mark('channel'),", + " requireCanonicalInstall: () => mark('canonical'),", + " deliverSelectedManifest: async () => { mark('delivery'); return []; },", + " finalizeSelectedDelivery: async () => { mark('finalize'); return true; },", + '});', + "process.stdout.write(process.exitCode === 2 ? 'BUSY' : 'WON');", + '', + ].join('\n'), + ); + updateFailureScript = join(home, 'update-terminal-failure.ts'); + writeFileSync( + updateFailureScript, + [ + 'process.env.GENIE_HOME = process.argv[2];', + `const mod = await import(${JSON.stringify(UPDATE_MODULE)});`, + 'const failure = process.argv[3];', + 'const manifest = {', + ' schema_version: 1,', + " channel: 'stable',", + " version: '5.260723.8',", + " released_at: '2026-07-23T00:00:00Z',", + " tarball_base: 'https://github.com/automagik-dev/genie/releases/download/v5.260723.8',", + " platforms: ['darwin-arm64'],", + ' manifestBytes: \'{"version":"5.260723.8"}\\n\',', + " manifestSha256: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',", + '};', + 'await mod.updateCommand({ yes: true, stable: true }, {', + ' fetchManifest: async () => manifest,', + " readInstalledVersion: () => '5.260700.1',", + " resolvePlatform: () => 'darwin-arm64',", + ' recoverPendingState: () => undefined,', + ' persistSelectedChannel: async () => undefined,', + " requireCanonicalInstall: () => { if (failure === 'canonical') throw new Error('canonical refusal fixture'); },", + " deliverSelectedManifest: async () => { if (failure === 'delivery') throw new Error('delivery failure fixture'); return []; },", + ' finalizeSelectedDelivery: async () => true,', + '});', + "process.stdout.write('TERMINAL:' + String(process.exitCode));", + '', + ].join('\n'), + ); }); afterEach(() => { @@ -95,22 +167,61 @@ function leaseFiles(dir: string): string[] { return readdirSync(dir).filter((entry) => entry.includes('lifecycle') || entry.endsWith('.lock')); } +function expectExactWinnerAttribution(outcomes: string[]): void { + const winners = outcomes.filter((outcome) => outcome.startsWith('WON:')); + const losers = outcomes.filter((outcome) => outcome.startsWith('BUSY:')); + expect(winners).toHaveLength(1); + expect(losers).toHaveLength(outcomes.length - 1); + expect(new Set(losers)).toEqual(new Set([`BUSY:${winners[0].slice('WON:'.length)}`])); +} + +async function expectRealUpdateTerminalReleasesBothLeases( + failure: 'canonical' | 'delivery', + expectedMessage: string, +): Promise { + const proc = Bun.spawn(['bun', 'run', updateFailureScript, home, failure], { + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env, GENIE_HOME: home }, + }); + const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]); + expect(await proc.exited).toBe(1); + expect(`${stdout}\n${stderr}`).toContain(expectedMessage); + expect(stdout.trim().endsWith('TERMINAL:1')).toBe(true); + + const agentPath = lifecycleLockPath(home); + const codexPath = join(home, '.codex-lifecycle.lock'); + expect(existsSync(agentPath)).toBe(false); + expect(existsSync(codexPath)).toBe(false); + + const agent = acquireAgentLifecycleLease(home); + expect('skipped' in agent).toBe(false); + if ('skipped' in agent) throw new Error(agent.skipped); + const codex = acquireLifecycleLease('setup-activation', { genieHome: home }); + expect(codex.ok).toBe(true); + try { + expect(existsSync(agentPath)).toBe(true); + expect(existsSync(codexPath)).toBe(true); + } finally { + if (codex.ok) codex.release(); + agent.release(); + } + expect(existsSync(agentPath)).toBe(false); + expect(existsSync(codexPath)).toBe(false); +} + describe('cross-command Codex lifecycle races produce exactly one winner', () => { test('update + install: exactly one winner, loser is codex-lifecycle-busy', async () => { const outcomes = await race('update-delivery', 'install-converge'); - expect(outcomes.filter((o) => o.startsWith('WON:'))).toHaveLength(1); - const losers = outcomes.filter((o) => o.startsWith('BUSY:')); - expect(losers).toHaveLength(1); - // The loser names a valid held-kind (the winner's kind), never a corruption. - expect(losers[0]).toMatch(/BUSY:(update-delivery|install-converge)/); + expectExactWinnerAttribution(outcomes); }, 20_000); - test('update (held lease) + REAL install command: install refuses at command level with exit 2 codex-lifecycle-busy, zero mutation', async () => { - // A concurrent `genie update` holds the Codex lifecycle lease (update-delivery). - const held = acquireLifecycleLease('update-delivery', { genieHome: home }); + test('setup (held lease) + REAL install command: install refuses at command level with exit 2 and zero mutation', async () => { + const mutationPath = join(home, 'install-mutation'); + const held = acquireLifecycleLease('setup-activation', { genieHome: home }); expect(held.ok).toBe(true); try { - const proc = Bun.spawn(['bun', 'run', installScript, home], { + const proc = Bun.spawn(['bun', 'run', installScript, home, mutationPath], { stdout: 'pipe', stderr: 'pipe', env: { ...process.env, GENIE_HOME: home }, @@ -120,18 +231,351 @@ describe('cross-command Codex lifecycle races produce exactly one winner', () => // The real install command projects the exit-2 loser refusal and mutates nothing. expect(exitCode).toBe(2); expect(out).toContain('codex-lifecycle-busy'); - expect(out).toContain('update-delivery'); + expect(out).toContain('setup-activation'); expect(out).toContain('"deliveryComplete":false'); expect(out.endsWith('BUSY')).toBe(true); + expect(existsSync(mutationPath)).toBe(false); } finally { if (held.ok) held.release(); } }, 20_000); + test('setup (held lease) + REAL update command: update refuses before recovery or delivery mutation', async () => { + const mutationPath = join(home, 'update-mutation'); + const held = acquireLifecycleLease('setup-activation', { genieHome: home }); + expect(held.ok).toBe(true); + try { + const proc = Bun.spawn(['bun', 'run', updateScript, home, mutationPath], { + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env, GENIE_HOME: home }, + }); + const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]); + const exitCode = await proc.exited; + const output = `${stdout}\n${stderr}`; + expect(exitCode).toBe(2); + expect(output).toContain('codex-lifecycle-busy'); + expect(output).toContain('setup-activation'); + expect(output).toContain('"deliveryComplete":false'); + expect(stdout.trim().endsWith('BUSY')).toBe(true); + expect(existsSync(mutationPath)).toBe(false); + } finally { + if (held.ok) held.release(); + } + }, 20_000); + + test('real update canonical refusal exits only after both lifecycle leases release', async () => { + await expectRealUpdateTerminalReleasesBothLeases('canonical', 'canonical refusal fixture'); + }, 20_000); + + test('real update delivery failure exits only after both lifecycle leases release', async () => { + await expectRealUpdateTerminalReleasesBothLeases('delivery', 'Update failed: delivery failure fixture'); + }, 20_000); + test('update + rollback: exactly one winner, loser is codex-lifecycle-busy', async () => { const outcomes = await race('update-delivery', 'rollback'); - expect(outcomes.filter((o) => o.startsWith('WON:'))).toHaveLength(1); - expect(outcomes.filter((o) => o.startsWith('BUSY:'))).toHaveLength(1); + expectExactWinnerAttribution(outcomes); + }, 20_000); + + test('SIGKILL before stable publication leaves recoverable private debris', async () => { + const marker = join(home, 'before-publish.ready'); + const crashScript = join(home, 'crash-before-publish.ts'); + writeFileSync( + crashScript, + [ + "import { writeFileSync } from 'node:fs';", + `import { acquireLifecycleLease } from ${JSON.stringify(LEASE_MODULE)};`, + 'const [genieHome, marker] = process.argv.slice(2);', + "const result = acquireLifecycleLease('update-delivery', {", + ' genieHome,', + ' beforePublishForTest: () => {', + " writeFileSync(marker, 'ready');", + ' Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 60_000);', + ' },', + '});', + 'if (!result.ok) process.exit(3);', + ].join('\n'), + ); + const child = Bun.spawn(['bun', 'run', crashScript, home, marker], { + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env }, + }); + + try { + const deadline = Date.now() + 5_000; + while (!existsSync(marker) && Date.now() < deadline) await Bun.sleep(10); + expect(existsSync(marker)).toBe(true); + expect(readdirSync(home).filter((name) => name.includes('.staging-'))).toHaveLength(1); + + child.kill('SIGKILL'); + expect(await child.exited).not.toBe(0); + + const recovered = acquireLifecycleLease('rollback', { genieHome: home }); + expect(recovered.ok).toBe(true); + expect(readdirSync(home).filter((name) => name.includes('.staging-'))).toEqual([]); + if (recovered.ok) recovered.release(); + } finally { + child.kill('SIGKILL'); + await child.exited; + } + }, 20_000); + + test('a fresh process recovers a fixed staging slot behind 1,210 crowded directory entries', async () => { + for (let index = 0; index < 1_210; index += 1) { + writeFileSync( + join(home, `.codex-lifecycle.lock.staging-noise-${index.toString().padStart(4, '0')}`), + '{ preserved foreign debris', + ); + } + const operationId = `fe${'0'.repeat(30)}`; + const staging = join(home, '.codex-lifecycle.lock.staging-fe'); + writeFileSync( + staging, + `${JSON.stringify({ + schemaVersion: 1, + operationId, + kind: 'update-delivery', + pid: 424242, + startedAt: '2026-07-23T00:00:00.000Z', + stagingSlot: 'fe', + })}\n`, + ); + const recoveryScript = join(home, 'fresh-process-crowded-recovery.ts'); + writeFileSync( + recoveryScript, + [ + `import { acquireLifecycleLease } from ${JSON.stringify(LEASE_MODULE)};`, + 'const result = acquireLifecycleLease("rollback", { genieHome: process.argv[2], isProcessAlive: () => false });', + "process.stdout.write(result.ok ? 'WON' : 'BUSY:' + (result.holderKind ?? 'unknown'));", + 'if (result.ok) result.release();', + ].join('\n'), + ); + + const child = Bun.spawn(['bun', 'run', recoveryScript, home], { + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env }, + }); + const stdout = (await new Response(child.stdout).text()).trim(); + const stderr = (await new Response(child.stderr).text()).trim(); + expect(await child.exited, stderr).toBe(0); + expect(stdout).toBe('WON'); + expect(existsSync(staging)).toBe(false); + expect(readdirSync(home).filter((name) => name.includes('staging-noise-'))).toHaveLength(1_210); + }, 20_000); + + test('32 fresh contenders released through one ready/go barrier retain exact attribution', async () => { + const contenderCount = 32; + const go = join(home, 'fresh-race.go'); + const freshRaceScript = join(home, 'fresh-race.ts'); + writeFileSync( + freshRaceScript, + [ + "import { existsSync, writeFileSync } from 'node:fs';", + `import { acquireLifecycleLease } from ${JSON.stringify(LEASE_MODULE)};`, + 'const [genieHome, go] = process.argv.slice(2);', + "writeFileSync(genieHome + '/fresh-ready-' + process.pid, 'ready');", + 'while (!existsSync(go)) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);', + "const result = acquireLifecycleLease('update-delivery', { genieHome });", + "process.stdout.write(result.ok ? 'WON:' + result.kind : 'BUSY:' + (result.holderKind ?? 'unknown') + (result.holderKind === null ? '[' + result.detail + ']' : ''));", + 'if (result.ok) await new Promise((resolve) => setTimeout(resolve, 500));', + ].join('\n'), + ); + const spawnOne = () => + Bun.spawn(['bun', 'run', freshRaceScript, home, go], { + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env }, + }); + const children = Array.from({ length: contenderCount }, spawnOne); + + try { + const deadline = Date.now() + 15_000; + while ( + readdirSync(home).filter((name) => name.startsWith('fresh-ready-')).length < contenderCount && + Date.now() < deadline + ) { + await Bun.sleep(10); + } + expect(readdirSync(home).filter((name) => name.startsWith('fresh-ready-'))).toHaveLength(contenderCount); + writeFileSync(go, 'go\n'); + const outcomes = await Promise.all( + children.map(async (child) => { + const stdout = (await new Response(child.stdout).text()).trim(); + const stderr = (await new Response(child.stderr).text()).trim(); + const exitCode = await child.exited; + expect(exitCode, stderr).toBe(0); + return stdout; + }), + ); + + expectExactWinnerAttribution(outcomes); + expect(readdirSync(home).filter((name) => /^\.codex-lifecycle\.lock\.staging-[0-9a-f]{2}$/.test(name))).toEqual( + [], + ); + } finally { + if (!existsSync(go)) writeFileSync(go, 'go\n'); + for (const child of children) child.kill('SIGKILL'); + await Promise.all(children.map((child) => child.exited)); + } + }, 20_000); + + test('32 contenders aligned after observing one dead holder produce one winner and exact losers', async () => { + const contenderCount = 32; + const staleRecord = { + schemaVersion: 1, + operationId: 'a'.repeat(32), + kind: 'rollback', + pid: 424242, + startedAt: '2026-07-23T00:00:00.000Z', + }; + writeFileSync(join(home, '.codex-lifecycle.lock'), `${JSON.stringify(staleRecord)}\n`); + const go = join(home, 'stale-race.go'); + const staleRaceScript = join(home, 'stale-race.ts'); + writeFileSync( + staleRaceScript, + [ + "import { existsSync, writeFileSync } from 'node:fs';", + `import { acquireLifecycleLease } from ${JSON.stringify(LEASE_MODULE)};`, + 'const [genieHome, go] = process.argv.slice(2);', + "const result = acquireLifecycleLease('update-delivery', {", + ' genieHome,', + ' isProcessAlive: (pid) => pid !== 424242,', + ' afterDeadHolderObservedForTest: () => {', + " writeFileSync(genieHome + '/stale-observed-' + process.pid, 'ready');", + ' while (!existsSync(go)) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);', + ' },', + '});', + "process.stdout.write(result.ok ? 'WON:' + result.kind : 'BUSY:' + (result.holderKind ?? 'unknown') + (result.holderKind === null ? '[' + result.detail + ']' : ''));", + 'if (result.ok) await new Promise((resolve) => setTimeout(resolve, 500));', + ].join('\n'), + ); + const spawnOne = () => + Bun.spawn(['bun', 'run', staleRaceScript, home, go], { + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env }, + }); + const children = Array.from({ length: contenderCount }, spawnOne); + + try { + const deadline = Date.now() + 15_000; + while ( + readdirSync(home).filter((name) => name.startsWith('stale-observed-')).length < contenderCount && + Date.now() < deadline + ) { + await Bun.sleep(10); + } + expect(readdirSync(home).filter((name) => name.startsWith('stale-observed-'))).toHaveLength(contenderCount); + writeFileSync(go, 'go\n'); + const outcomes = await Promise.all( + children.map(async (child) => { + const stdout = (await new Response(child.stdout).text()).trim(); + const stderr = (await new Response(child.stderr).text()).trim(); + const exitCode = await child.exited; + expect(exitCode, stderr).toBe(0); + return stdout; + }), + ); + + expectExactWinnerAttribution(outcomes); + expect(readdirSync(home).filter((name) => name.startsWith('.codex-lifecycle.lock.stale-'))).toHaveLength(1); + expect(readdirSync(home).filter((name) => /^\.codex-lifecycle\.lock\.staging-[0-9a-f]{2}$/.test(name))).toEqual( + [], + ); + expect(existsSync(join(home, '.codex-lifecycle.lock.recovery'))).toBe(false); + } finally { + if (!existsSync(go)) writeFileSync(go, 'go\n'); + for (const child of children) child.kill('SIGKILL'); + await Promise.all(children.map((child) => child.exited)); + } + }, 20_000); + + test('SIGKILL at every stale-recovery claim window permits a bounded takeover', async () => { + const windows = [ + { + name: 'after-claim-publication', + hook: ' afterRecoveryClaimForTest: stop,', + }, + { + name: 'after-old-stable-capture', + hook: " afterCaptureForTest: (event) => { if (event.operation === 'stale-supersede') stop(); },", + }, + { + name: 'before-fresh-stable-publication', + hook: [ + ' beforePublishForTest: () => {', + ' publicationCount += 1;', + ' if (publicationCount === 2) stop();', + ' },', + ].join('\n'), + }, + ]; + + for (const window of windows) { + const crashHome = join(home, window.name); + mkdirSync(crashHome); + const marker = join(crashHome, 'blocked.ready'); + const crashScript = join(crashHome, 'crash-during-recovery.ts'); + const stalePid = 42_424_242; + writeFileSync( + join(crashHome, '.codex-lifecycle.lock'), + `${JSON.stringify({ + schemaVersion: 1, + operationId: 'c'.repeat(32), + kind: 'rollback', + pid: stalePid, + startedAt: '2026-07-23T00:00:00.000Z', + })}\n`, + ); + writeFileSync( + crashScript, + [ + "import { writeFileSync } from 'node:fs';", + `import { acquireLifecycleLease } from ${JSON.stringify(LEASE_MODULE)};`, + 'const [genieHome, marker] = process.argv.slice(2);', + 'let publicationCount = 0;', + "const stop = () => { writeFileSync(marker, 'ready'); Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 60_000); };", + "const result = acquireLifecycleLease('update-delivery', {", + ' genieHome,', + ` isProcessAlive: (pid) => pid !== ${stalePid},`, + window.hook, + '});', + "process.stdout.write(result.ok ? 'WON' : 'BUSY:' + (result.holderKind ?? 'unknown'));", + ].join('\n'), + ); + const child = Bun.spawn(['bun', 'run', crashScript, crashHome, marker], { + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env }, + }); + + try { + const deadline = Date.now() + 5_000; + while (!existsSync(marker) && Date.now() < deadline) await Bun.sleep(10); + expect(existsSync(marker), window.name).toBe(true); + expect(existsSync(join(crashHome, '.codex-lifecycle.lock.recovery')), window.name).toBe(true); + + child.kill('SIGKILL'); + expect(await child.exited, window.name).not.toBe(0); + + const takeover = acquireLifecycleLease('setup-activation', { + genieHome: crashHome, + isProcessAlive: (pid) => pid !== stalePid && pid !== child.pid, + }); + expect(takeover.ok, window.name).toBe(true); + expect(existsSync(join(crashHome, '.codex-lifecycle.lock.recovery')), window.name).toBe(false); + expect( + readdirSync(crashHome).filter((name) => /^\.codex-lifecycle\.lock\.staging-[0-9a-f]{2}$/.test(name)), + window.name, + ).toEqual([]); + if (takeover.ok) takeover.release(); + } finally { + child.kill('SIGKILL'); + await child.exited; + } + } }, 20_000); test('the winner leaves exactly one lease file under the fixture home; no escape', async () => { diff --git a/tests/integration/codex-native-mcp-evidence.test.ts b/tests/integration/codex-native-mcp-evidence.test.ts new file mode 100644 index 000000000..585db32a7 --- /dev/null +++ b/tests/integration/codex-native-mcp-evidence.test.ts @@ -0,0 +1,266 @@ +import { afterAll, describe, expect, test } from 'bun:test'; +import { spawnSync } from 'node:child_process'; +import { createHash, randomBytes } from 'node:crypto'; +import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { openDb } from '../../src/lib/v5/genie-db.js'; +import { claimTask, createBoard, createTask } from '../../src/lib/v5/task-state.js'; +import { + type CodexAppServerPin, + CodexNativeMcpEvidenceError, + captureCodexNativeMcpEvidence, + captureCodexNativeMcpEvidenceForDogfood, + probeCodexAppServer, +} from '../support/codex-native-mcp-evidence.js'; + +const TEST_TIMEOUT = 60_000; +const root = mkdtempSync(join(tmpdir(), 'genie-real-native-mcp-')); +const launcher = fileURLToPath(new URL('../support/codex-native-mcp-launcher.sh', import.meta.url)); +const candidate = fileURLToPath(new URL('../../src/genie.ts', import.meta.url)); + +function isolatedEnv(name: string): NodeJS.ProcessEnv { + const base = join(root, name); + const home = join(base, 'home'); + const codexHome = join(base, 'codex-home'); + const genieHome = join(base, 'genie-home'); + const temp = join(base, 'tmp'); + for (const path of [home, codexHome, genieHome, temp]) mkdirSync(path, { recursive: true }); + return { + ...process.env, + HOME: home, + CODEX_HOME: codexHome, + GENIE_HOME: genieHome, + TMPDIR: temp, + NO_COLOR: '1', + RUST_LOG: 'error', + OPENAI_API_KEY: undefined, + }; +} + +function resolvePin(): CodexAppServerPin { + const executable = process.env.GENIE_DOGFOOD_REAL_CODEX ?? Bun.which('codex') ?? '/codex-unavailable'; + const result = spawnSync(executable, ['--version'], { + encoding: 'utf8', + env: process.env, + timeout: 5_000, + }); + return { + executable, + version: result.status === 0 && result.stdout.trim() ? result.stdout.trim() : 'codex-unavailable', + }; +} + +function initRepo(name: string): string { + const repo = join(root, name); + mkdirSync(repo, { recursive: true }); + const result = spawnSync('git', ['init', '-q', '-b', 'main'], { + cwd: repo, + encoding: 'utf8', + env: process.env, + }); + if (result.status !== 0) throw new Error(`git init failed: ${result.stderr}`); + return repo; +} + +function seedRepo( + repo: string, + label: string, +): { + token: string; + task: { id: string; title: string; wish: string; status: string; claimedBy: string }; +} { + const token = randomBytes(20).toString('hex'); + const wish = `native-${label}-${token}`; + const title = `task-${label}-${token}`; + const claimedBy = `native-worker-${label}-${token}`; + const db = openDb({ cwd: repo }); + try { + const board = createBoard(db, 'repo'); + const task = createTask(db, { title, boardId: board.id, wish, group: 'evidence' }); + const claimed = claimTask(db, task.id, claimedBy); + return { + token, + task: { + id: claimed.id, + title: claimed.title, + wish: claimed.wish as string, + status: claimed.status, + claimedBy: claimed.claimedBy as string, + }, + }; + } finally { + db.close(); + } +} + +function processExists(pid: number): boolean { + try { + process.kill(pid, 0); + return true; + } catch (error) { + return (error as NodeJS.ErrnoException).code !== 'ESRCH'; + } +} + +const pin = resolvePin(); +const probeEnv = isolatedEnv('probe'); +const support = await probeCodexAppServer(pin, probeEnv); + +afterAll(() => { + rmSync(root, { recursive: true, force: true }); +}); + +test('reports whether the real pinned Codex app-server is available without a model request', () => { + expect(typeof support.reason).toBe('string'); + if (!support.supported) { + expect(`real-codex-unavailable: ${support.reason}`).toContain('real-codex-unavailable'); + } +}); + +describe.skipIf(!support.supported)('real Codex app-server native MCP evidence', () => { + test( + 'exact adapter+candidate returns one claimed sentinel and equals command/exec cwd by string and dev:ino', + async () => { + const repo = initRepo('seeded'); + const expectedSentinel = seedRepo(repo, 'a'); + const evidence = await captureCodexNativeMcpEvidence({ + codex: pin, + requestedCwd: repo, + candidate: { executable: candidate, adapter: process.execPath, args: ['mcp'] }, + launcherExecutable: launcher, + controlExecutable: process.execPath, + env: isolatedEnv('seeded-env'), + expectedSentinel, + }); + + expect(evidence.rawRequestedCwd).toBe(repo); + expect(evidence.launcher.effectiveCwd).toBe(evidence.control.effectiveCwd); + expect(evidence.launcher.cwdIdentity).toBe(evidence.control.cwdIdentity); + expect(evidence.launcher.pid).toBeGreaterThan(0); + expect(evidence.threadId).not.toBeEmpty(); + expect(evidence.toolResponse.isError).not.toBe(true); + expect(evidence.outcome.kind).toBe('sentinel'); + expect((evidence.toolPayload.tasks as Array>)[0]?.claimedBy).toBe( + expectedSentinel.task.claimedBy, + ); + }, + TEST_TIMEOUT, + ); + + test( + 'untouched repository passes only through typed project-database-unavailable without tasks/counts', + async () => { + const repo = initRepo('untouched'); + const evidence = await captureCodexNativeMcpEvidence({ + codex: pin, + requestedCwd: repo, + candidate: { executable: candidate, adapter: process.execPath, args: ['mcp'] }, + launcherExecutable: launcher, + controlExecutable: process.execPath, + env: isolatedEnv('untouched-env'), + expectedError: 'project-database-unavailable', + }); + + expect(evidence.toolResponse.isError).toBe(true); + expect(evidence.toolPayload.error).toBe('project-database-unavailable'); + expect(evidence.toolPayload).not.toHaveProperty('tasks'); + expect(evidence.toolPayload).not.toHaveProperty('counts'); + expect(evidence.outcome.kind).toBe('expected-error'); + }, + TEST_TIMEOUT, + ); + + test( + 'dogfood adapter derives the claimed sentinel and retains the complete raw capture', + async () => { + const repo = initRepo('dogfood-adapter'); + const expected = seedRepo(repo, 'adapter'); + mkdirSync(join(repo, '.codex')); + writeFileSync( + join(repo, '.codex', 'config.toml'), + `[mcp_servers.genie]\ncommand = ${JSON.stringify(process.execPath)}\nargs = ["-e", "process.exit(77)"]\n`, + ); + const env = isolatedEnv('dogfood-adapter-env') as Record; + const observed = await captureCodexNativeMcpEvidenceForDogfood({ + tag: 'a-new-thread', + requestedCwd: repo, + candidateBinary: candidate, + candidateBinarySha256: createHash('sha256').update(readFileSync(candidate)).digest('hex'), + executionAdapter: process.execPath, + root, + env, + }); + + expect(observed.isError).toBe(false); + expect(observed.effectiveCwd).toBe(observed.controlCwd); + expect(observed.cwdIdentity).toBe(observed.controlCwdIdentity); + expect((observed.payload.tasks as Array>)[0]?.claimedBy).toBe(expected.task.claimedBy); + expect(observed.raw).toHaveProperty('codex'); + expect(observed.raw).toHaveProperty('launcher'); + expect(observed.raw).toHaveProperty('toolResponse'); + expect(observed.raw).toHaveProperty('control'); + }, + TEST_TIMEOUT, + ); + + test( + 'a candidate that only advertises genie_board capability cannot produce passing sentinel evidence', + async () => { + const repo = initRepo('capability-only-repo'); + const expectedSentinel = seedRepo(repo, 'capability'); + const env = isolatedEnv('capability-env'); + const candidatePidFile = join(root, 'capability.pid'); + const capabilityOnly = join(root, 'capability-only.mjs'); + writeFileSync( + capabilityOnly, + `import { writeFileSync } from 'node:fs'; +import { createInterface } from 'node:readline'; +writeFileSync(process.argv.at(-1), String(process.pid)); +const rl = createInterface({ input: process.stdin }); +rl.on('line', (line) => { + const message = JSON.parse(line); + if (message.method === 'initialize') { + const result = { protocolVersion: '2024-11-05', capabilities: { tools: {} }, serverInfo: { name: 'capability-only', version: '1' } }; + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n'); + } else if (message.method === 'tools/list') { + const tools = [{ name: 'genie_board', description: 'advertised only', inputSchema: { type: 'object', properties: {} } }]; + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: { tools } }) + '\\n'); + } else if (message.method === 'tools/call') { + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: { content: [], isError: false } }) + '\\n'); + } else if (message.id !== undefined) { + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: {} }) + '\\n'); + } +}); +setInterval(() => {}, 1 << 30); +`, + 'utf8', + ); + chmodSync(capabilityOnly, 0o755); + + let failure: unknown; + try { + await captureCodexNativeMcpEvidence({ + codex: pin, + requestedCwd: repo, + candidate: { executable: capabilityOnly, adapter: process.execPath, args: ['mcp', candidatePidFile] }, + launcherExecutable: launcher, + controlExecutable: process.execPath, + env, + expectedSentinel, + timeouts: { toolCallMs: 300, cleanupGraceMs: 250 }, + }); + } catch (error) { + failure = error; + } + expect(failure).toBeInstanceOf(CodexNativeMcpEvidenceError); + expect((failure as CodexNativeMcpEvidenceError).code).toBe('mcp-tool-failure'); + const candidatePid = Number(readFileSync(candidatePidFile, 'utf8')); + const deadline = Date.now() + 2_000; + while (processExists(candidatePid) && Date.now() < deadline) await Bun.sleep(20); + expect(processExists(candidatePid)).toBe(false); + }, + TEST_TIMEOUT, + ); +}); diff --git a/tests/integration/codex-project-route-migration.test.ts b/tests/integration/codex-project-route-migration.test.ts new file mode 100644 index 000000000..6a1084f3b --- /dev/null +++ b/tests/integration/codex-project-route-migration.test.ts @@ -0,0 +1,173 @@ +/** + * Two-repository Codex project-route migration (Group A, D7 + acceptance). + * + * After the global plugin MCP route disappears (the Codex plugin ships none), + * an untouched repository has an EXPLICITLY ABSENT project route and NO fallback: + * `genie mcp` launched there fails closed with `project-database-unavailable` + * rather than serving an outer/cache-root empty board or another repo's state. + * The single reconciliation command is `cd && genie init`, which creates + * only that repo's marker-owned route. Afterward each repo returns ONLY its own + * unique seeded sentinel. + */ + +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; +import { execFileSync } from 'node:child_process'; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { openDb } from '../../src/lib/v5/genie-db.js'; +import { createBoard, createTask, createWishGroups } from '../../src/lib/v5/task-state.js'; + +const GENIE = join(import.meta.dir, '..', '..', 'src', 'genie.ts'); + +let base: string; +let genieHome: string; + +function git(cwd: string, ...args: string[]): void { + execFileSync('git', args, { + cwd, + stdio: 'ignore', + env: { + ...process.env, + GIT_AUTHOR_NAME: 'T', + GIT_AUTHOR_EMAIL: 't@e.com', + GIT_COMMITTER_NAME: 'T', + GIT_COMMITTER_EMAIL: 't@e.com', + }, + }); +} + +function initRepo(name: string): string { + const dir = join(base, name); + mkdirSync(dir, { recursive: true }); + git(dir, 'init', '-b', 'main'); + git(dir, 'commit', '--allow-empty', '-m', 'init'); + return dir; +} + +function runInit(cwd: string): { code: number; stderr: string } { + const res = Bun.spawnSync([process.execPath, GENIE, 'init'], { + cwd, + stdout: 'pipe', + stderr: 'pipe', + // No Codex CLI on PATH + isolated GENIE_HOME: the marker route is plugin-independent. + env: { ...process.env, PATH: '/usr/bin:/bin', GENIE_HOME: genieHome }, + }); + return { code: res.exitCode, stderr: res.stderr.toString() }; +} + +/** Seed a repo's db with a UNIQUE sentinel wish so cross-repo bleed is detectable. */ +function seedSentinel(repo: string, sentinel: string): void { + const db = openDb({ cwd: repo }); + const board = createBoard(db, 'repo'); + createTask(db, { title: `task-${sentinel}`, boardId: board.id, wish: sentinel, group: 'g' }); + createWishGroups(db, sentinel, [{ name: 'g' }]); + // Fold pending WAL frames into the main db and close before any reader (the + // `genie mcp` subprocess) opens, so the readonly reader isn't racing an open + // WAL writer under cross-file contention ("database is locked"). + db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); + db.close(); +} + +interface RpcResponse { + id: number | string | null; + result?: { content?: Array<{ text: string }>; isError?: boolean }; +} + +async function driveBoard(cwd: string): Promise<{ isError: boolean; payload: Record }> { + const proc = Bun.spawn(['bun', GENIE, 'mcp'], { + cwd, + stdin: 'pipe', + stdout: 'pipe', + stderr: 'pipe', + env: { ...process.env, NO_COLOR: '1', GENIE_TEST_SKIP_PGSERVE: '1' }, + }); + const requests = [ + { + jsonrpc: '2.0', + id: 1, + method: 'initialize', + params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 't', version: '0' } }, + }, + { jsonrpc: '2.0', method: 'notifications/initialized' }, + { jsonrpc: '2.0', id: 2, method: 'tools/call', params: { name: 'genie_board', arguments: {} } }, + ]; + proc.stdin.write(`${requests.map((r) => JSON.stringify(r)).join('\n')}\n`); + await proc.stdin.end(); + const stdout = await new Response(proc.stdout).text(); + await proc.exited; + const responses = stdout + .split('\n') + .filter((l) => l.trim().length > 0) + .map((l) => JSON.parse(l) as RpcResponse); + const res = responses.find((r) => r.id === 2); + if (!res?.result) throw new Error(`no tools/call result: ${stdout}`); + return { isError: res.result.isError === true, payload: JSON.parse(res.result.content?.[0]?.text ?? '{}') }; +} + +const markerPath = (repo: string) => join(repo, '.codex', 'config.toml'); + +beforeEach(() => { + base = mkdtempSync(join(tmpdir(), 'genie-migrate-')); + genieHome = join(base, 'genie-home'); +}); + +afterEach(() => { + rmSync(base, { recursive: true, force: true }); +}); + +describe('two-repo Codex project-route migration', () => { + test('reconciled A serves only A; untouched B fails closed; `cd B && genie init` reconciles only B', async () => { + const repoA = initRepo('repoA'); + const repoB = initRepo('repoB'); + + // --- A: reconcile the marker-owned route and seed A's unique sentinel. --- + expect(runInit(repoA).code).toBe(0); + const tomlA = readFileSync(markerPath(repoA), 'utf8'); + expect(tomlA).toContain('# BEGIN GENIE MCP FALLBACK'); + expect(tomlA).toContain(`${join(genieHome, 'bin', 'genie')}`); // stable facade, not a cache path + expect(existsSync(join(repoA, '.mcp.json'))).toBe(true); // Claude/Warp route from init + seedSentinel(repoA, 'alpha-sentinel'); + + const aBoard = await driveBoard(repoA); + expect(aBoard.isError).toBe(false); + expect((aBoard.payload.tasks as Array<{ wish: string }>).some((t) => t.wish === 'alpha-sentinel')).toBe(true); + + // --- B untouched: no project route, no fallback, fails closed (no A bleed). --- + expect(existsSync(markerPath(repoB))).toBe(false); // explicitly absent project route + expect(existsSync(join(repoB, '.mcp.json'))).toBe(false); + const bBefore = await driveBoard(repoB); + expect(bBefore.isError).toBe(true); + expect(bBefore.payload.error).toBe('project-database-unavailable'); + // Critically: B never returns A's sentinel nor a healthy empty board. + expect(JSON.stringify(bBefore.payload)).not.toContain('alpha-sentinel'); + expect(bBefore.payload).not.toHaveProperty('counts'); + + // --- `cd B && genie init` reconciles ONLY B; A is untouched. --- + expect(runInit(repoB).code).toBe(0); + expect(existsSync(markerPath(repoB))).toBe(true); + expect(readFileSync(markerPath(repoB), 'utf8')).toContain(`${join(genieHome, 'bin', 'genie')}`); + seedSentinel(repoB, 'bravo-sentinel'); + + const bAfter = await driveBoard(repoB); + expect(bAfter.isError).toBe(false); + const bWishes = (bAfter.payload.tasks as Array<{ wish: string }>).map((t) => t.wish); + expect(bWishes).toContain('bravo-sentinel'); + expect(bWishes).not.toContain('alpha-sentinel'); // B returns ONLY B's sentinel + + // A still serves only A after B's reconciliation. + const aAgain = await driveBoard(repoA); + const aWishes = (aAgain.payload.tasks as Array<{ wish: string }>).map((t) => t.wish); + expect(aWishes).toContain('alpha-sentinel'); + expect(aWishes).not.toContain('bravo-sentinel'); + }); + + test('an untouched repo with no genie.db never serializes a healthy empty board', async () => { + const repo = initRepo('lonely'); + // No init, no db. The read server must refuse rather than masquerade as empty. + const board = await driveBoard(repo); + expect(board.isError).toBe(true); + expect(board.payload.error).toBe('project-database-unavailable'); + expect(board.payload.detail).toContain('genie init'); + }); +}); diff --git a/tests/integration/codex-task-cwd-mcp.test.ts b/tests/integration/codex-task-cwd-mcp.test.ts new file mode 100644 index 000000000..73175a97f --- /dev/null +++ b/tests/integration/codex-task-cwd-mcp.test.ts @@ -0,0 +1,41 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { mkdtempSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { buildDogfoodFixture } from '../support/codex-dogfood-fixture.js'; +import { runDogfoodEntry } from '../support/codex-dogfood-harness.js'; + +let root: string | null = null; +afterEach(() => { + if (root !== null) rmSync(root, { recursive: true, force: true }); + root = null; +}); + +describe('dogfood task-CWD MCP proof', () => { + test('two unpredictable seeded repos stay exact and untouched B has no pre-init fallback', async () => { + root = mkdtempSync(join(tmpdir(), 'genie-dogfood-cwd-')); + const fixture = buildDogfoodFixture(root); + const result = await runDogfoodEntry(fixture.input, fixture.dependencies); + const proof = result.manifest.repositories as Record; + const a = proof.a; + const b = proof.b as Record; + expect(a.requestedCwd).toBe(a.root); + expect(a.effectiveCwd).toBe(a.root); + expect(a.sentinel.observed).toEqual(a.sentinel.expected); + expect(a.sentinel.boardCount).toBe(1); + expect(b.beforeInit).toEqual({ + routeState: 'absent', + fallbackUsed: false, + result: 'project-database-unavailable', + returnedTasks: 0, + }); + expect(b.afterInit.routeState).toBe('managed-project'); + expect(b.afterInit.requestedCwd).toBe(b.root); + expect(b.afterInit.effectiveCwd).toBe(b.root); + expect(b.afterInit.sentinel.observed).toEqual(b.afterInit.sentinel.expected); + expect(a.sentinel.token).not.toBe(b.afterInit.sentinel.token); + expect(a.childPid).not.toBe(b.afterInit.childPid); + expect(a.effectiveCwd).not.toContain(proof.cacheRoot); + expect(b.afterInit.effectiveCwd).not.toContain(proof.cacheRoot); + }, 60_000); +}); diff --git a/tests/integration/install-exit2-propagation.test.ts b/tests/integration/install-exit2-propagation.test.ts index 70fdd3a25..b3afe6201 100644 --- a/tests/integration/install-exit2-propagation.test.ts +++ b/tests/integration/install-exit2-propagation.test.ts @@ -26,13 +26,20 @@ import { spawnSync } from 'node:child_process'; import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; +import { + CODEX_DELIVERY_INCOMPLETE_TRAILER, + CODEX_DELIVERY_RESULT_TRAILER, + CODEX_LIFECYCLE_BUSY_TRAILER, +} from '../../src/genie-commands/codex-delivery.js'; const REPO_ROOT = join(import.meta.dir, '..', '..'); const INSTALL_SH = join(REPO_ROOT, 'install.sh'); +const INSTALL_MODULE = join(REPO_ROOT, 'src', 'genie-commands', 'install.ts'); +const LEASE_MODULE = join(REPO_ROOT, 'src', 'lib', 'codex-lifecycle-lease.ts'); const VERSION = '0.0.0-exit2-test'; -const RESULT_TRAILER = - '{"schemaVersion":1,"code":"activation-pending","deliveryComplete":true,"retry":false,' + - '"nextAction":"retire tasks -> genie setup --codex -> /hooks -> new task"}'; +const RESULT_TRAILER = CODEX_DELIVERY_RESULT_TRAILER; +const HUMAN_JSON_EXAMPLE = 'diagnostic example: {"schemaVersion":1,"code":"not-a-result"}'; +const UNRELATED_JSON = '{"message":"schemaVersion and code remain human words"}'; let work: string; let binDir: string; @@ -47,6 +54,8 @@ function writeStubGenie(): void { ` --version|-v) echo "${VERSION} (stub)"; exit 0 ;;`, ' install)', ` printf ' ! Codex plugin: activation-pending (installed=5.260710.2, target=${VERSION})\\n'`, + ` printf '%s\\n' '${HUMAN_JSON_EXAMPLE}'`, + ` printf '%s\\n' '${UNRELATED_JSON}'`, ` printf '%s\\n' '${RESULT_TRAILER}'`, ' exit 2 ;;', ' *) echo "stub-genie:${1:-}"; exit 0 ;;', @@ -56,6 +65,72 @@ function writeStubGenie(): void { writeFileSync(join(binDir, 'genie'), script, { mode: 0o755 }); } +/** An invalid exit-2 result stream containing both success and failure lifecycle trailers. */ +function writeMixedTrailerStubGenie(contradictoryTrailer: string): void { + const script = [ + '#!/usr/bin/env bash', + 'case "${1:-}" in', + ` --version|-v) echo "${VERSION} (stub)"; exit 0 ;;`, + ' install)', + ` printf '%s\\n' '${RESULT_TRAILER}'`, + ` printf '%s\\n' '${contradictoryTrailer}'`, + ' exit 2 ;;', + ' *) exit 0 ;;', + 'esac', + '', + ].join('\n'); + writeFileSync(join(binDir, 'genie'), script, { mode: 0o755 }); +} + +/** A real install command whose inner lease loses to setup immediately before the shell finisher handoff. */ +function writeSetupBusyStubGenie(): string { + const finisher = join(work, 'setup-busy-finisher.ts'); + const mutationMarker = join(work, 'unexpected-finisher-mutation'); + writeFileSync( + finisher, + [ + "import { writeFileSync } from 'node:fs';", + `import { acquireLifecycleLease } from ${JSON.stringify(LEASE_MODULE)};`, + `import { installCommand } from ${JSON.stringify(INSTALL_MODULE)};`, + 'const genieHome = process.env.GENIE_HOME as string;', + `const mutationMarker = ${JSON.stringify(mutationMarker)};`, + "const mark = (phase: string) => writeFileSync(mutationMarker, phase + '\\n');", + "const setup = acquireLifecycleLease('setup-activation', { genieHome });", + "if (!setup.ok) throw new Error('fixture setup could not acquire Codex lifecycle lease');", + 'try {', + ' await installCommand(', + " { integrations: 'codex' },", + " () => mark('cleanup'),", + " () => { mark('normalize'); return undefined; },", + " () => mark('sync'),", + " () => { mark('integrations'); return []; },", + " () => ({ path: '/fixture/borrowed-agent-sync.lock', release: () => undefined }),", + ' undefined,', + " () => mark('consent'),", + ' () => null,', + ' );', + '} finally {', + ' setup.release();', + '}', + '', + ].join('\n'), + ); + writeFileSync( + join(binDir, 'genie'), + [ + '#!/usr/bin/env bash', + 'case "${1:-}" in', + ` --version|-v) echo "${VERSION} (stub)"; exit 0 ;;`, + ` install) exec ${JSON.stringify(process.execPath)} run ${JSON.stringify(finisher)} ;;`, + ' *) exit 0 ;;', + 'esac', + '', + ].join('\n'), + { mode: 0o755 }, + ); + return mutationMarker; +} + /** * A bash harness: source install.sh (source-only), stub the network/verify * surface, and run the REAL main() exit-2 lifecycle against the stub genie. @@ -115,7 +190,9 @@ function runHarness(): { status: number | null; output: string } { describe('install.sh delivered-not-activated exit-2 propagation (executed)', () => { test('propagates exit 2 with the result trailer, no all-green footer, lock released, idempotent rerun', () => { - mkdirSync(join(work, 'tmp'), { recursive: true }); + // install.sh's validate_private_temp_root rejects a group-writable temp parent, + // so pin the mode instead of inheriting the host umask (0002 hosts create 775). + mkdirSync(join(work, 'tmp'), { recursive: true, mode: 0o700 }); const first = runHarness(); // Delivered-not-activated is exit 2, NOT the die-1 failure path. @@ -123,6 +200,9 @@ describe('install.sh delivered-not-activated exit-2 propagation (executed)', () // The stable A-owned result trailer with deliveryComplete:true is relayed. expect(first.output).toContain('"deliveryComplete":true'); expect(first.output).toContain('"code":"activation-pending"'); + expect(first.output.split(RESULT_TRAILER)).toHaveLength(2); + expect(first.output).toContain(HUMAN_JSON_EXAMPLE); + expect(first.output).toContain(UNRELATED_JSON); // Delivered-but-deferred message, and explicitly NO all-green footer. expect(first.output).toContain('Codex activation deferred'); expect(first.output).not.toContain(`genie v${VERSION} installed`); @@ -133,11 +213,12 @@ describe('install.sh delivered-not-activated exit-2 propagation (executed)', () const second = runHarness(); expect(second.status).toBe(2); expect(second.output).toContain('"deliveryComplete":true'); + expect(second.output.split(RESULT_TRAILER)).toHaveLength(2); expect(second.output).not.toContain(`genie v${VERSION} installed`); expect(existsSync(lockPath)).toBe(false); }); - test('a genuine finisher failure (exit 1) still dies 1 — exit 2 is the ONLY action-required code', () => { + test('a genuine finisher failure (exit 1) still dies 1', () => { // Swap the stub genie's install to a hard failure; the real handoff must die 1. writeFileSync( join(binDir, 'genie'), @@ -151,10 +232,71 @@ describe('install.sh delivered-not-activated exit-2 propagation (executed)', () ].join('\n'), { mode: 0o755 }, ); - mkdirSync(join(work, 'tmp'), { recursive: true }); + // install.sh's validate_private_temp_root rejects a group-writable temp parent, + // so pin the mode instead of inheriting the host umask (0002 hosts create 775). + mkdirSync(join(work, 'tmp'), { recursive: true, mode: 0o700 }); const result = runHarness(); expect(result.status).toBe(1); expect(result.output).toContain('installation remains incomplete and retryable'); expect(existsSync(lockPath)).toBe(false); }); + + test('setup winning before the real finisher keeps exit-2 busy delivery-incomplete and emits no false green footer', () => { + const mutationMarker = writeSetupBusyStubGenie(); + mkdirSync(join(work, 'tmp'), { recursive: true, mode: 0o700 }); + + const result = runHarness(); + + expect(result.status).toBe(1); + expect(result.output).toContain('"code":"codex-lifecycle-busy"'); + expect(result.output).toContain('"deliveryComplete":false'); + expect(result.output.split(CODEX_LIFECYCLE_BUSY_TRAILER)).toHaveLength(2); + expect(result.output).not.toContain(RESULT_TRAILER); + expect(result.output).not.toContain('Codex activation deferred'); + expect(result.output).not.toContain(`genie v${VERSION} installed`); + expect(result.output).toContain('installation remains incomplete and retryable'); + expect(existsSync(mutationMarker)).toBe(false); + expect(existsSync(lockPath)).toBe(false); + expect(existsSync(join(work, '.genie', '.codex-lifecycle.lock'))).toBe(false); + }); + + for (const [label, contradictoryTrailer] of [ + ['busy', CODEX_LIFECYCLE_BUSY_TRAILER], + ['incomplete', CODEX_DELIVERY_INCOMPLETE_TRAILER], + ['duplicate canonical action', RESULT_TRAILER], + [ + 'canonical-prefix unknown lifecycle result', + '{"schemaVersion":1,"code":"future-lifecycle-result","deliveryComplete":true}', + ], + [ + 'reordered-key unknown lifecycle result', + '{"code":"future-lifecycle-result","deliveryComplete":true,"schemaVersion":1}', + ], + [ + 'whitespace-padded schema-first lifecycle result', + ' { "schemaVersion" : 2, "code" : "future-lifecycle-result", "deliveryComplete" : false } ', + ], + [ + 'whitespace-padded reordered lifecycle result', + ' { "code" : "future-lifecycle-result" , "deliveryComplete" : false , "schemaVersion" : 2 } ', + ], + ] as const) { + test(`an action-required trailer mixed with ${label} fails closed with no deferred or green result`, () => { + writeMixedTrailerStubGenie(contradictoryTrailer); + mkdirSync(join(work, 'tmp'), { recursive: true, mode: 0o700 }); + + const result = runHarness(); + + expect(result.status).toBe(1); + const outputLines = result.output.split(/\r?\n/); + expect(outputLines.filter((line) => line === RESULT_TRAILER)).toHaveLength( + contradictoryTrailer === RESULT_TRAILER ? 2 : 1, + ); + expect(outputLines).toContain(contradictoryTrailer); + expect(result.output).not.toContain('Codex activation deferred'); + expect(result.output).not.toContain(`genie v${VERSION} installed`); + expect(result.output).toContain('installation remains incomplete and retryable'); + expect(existsSync(lockPath)).toBe(false); + }); + } }); diff --git a/tests/support/codex-app-server-transport.ts b/tests/support/codex-app-server-transport.ts new file mode 100644 index 000000000..8641c885a --- /dev/null +++ b/tests/support/codex-app-server-transport.ts @@ -0,0 +1,222 @@ +import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'; +import { createInterface } from 'node:readline'; + +export interface AppServerJsonRpcEnvelope { + id?: number | string | null; + result?: unknown; + error?: unknown; +} + +type ErrorKind = 'process' | 'rpc' | 'timeout' | 'cleanup'; + +interface TransportOptions { + executable: string; + args: string[]; + env: NodeJS.ProcessEnv; + initializeParams: unknown; + initializeTimeoutMs: number; + cleanupGraceMs: number; + initialSignal: 'SIGTERM' | 'SIGKILL'; + label: string; + error: (kind: ErrorKind, message: string, cause?: Error) => E; + windowsHide?: boolean; +} + +interface Pending { + method: string; + resolve: (message: AppServerJsonRpcEnvelope) => void; + reject: (error: Error) => void; + timer: ReturnType; +} + +/** Process-owning newline JSON-RPC transport shared by the independent evidence builders. */ +export class CodexAppServerTransport { + private readonly pending = new Map(); + private readonly closed: Promise; + private closing: Promise | null = null; + private nextId = 1; + private didClose = false; + private stderr = ''; + + private constructor( + private readonly child: ChildProcessWithoutNullStreams, + private readonly options: TransportOptions, + ) { + this.closed = new Promise((resolve) => + child.once('close', () => { + this.didClose = true; + resolve(); + }), + ); + child.stderr.on('data', (chunk: Buffer | string) => { + this.stderr = `${this.stderr}${String(chunk)}`.slice(-16_384); + }); + child.on('error', (error) => this.rejectAll(this.error('process', `process error: ${error.message}`, error))); + child.on('exit', (code, signal) => + this.rejectAll( + this.error( + 'process', + `exited before replying (code=${String(code)}, signal=${String(signal)}): ${this.stderr.trim().slice(-2_000)}`, + ), + ), + ); + child.stdin.on('error', (error) => this.rejectAll(this.error('rpc', `stdin error: ${error.message}`, error))); + createInterface({ input: child.stdout }).on('line', (line) => this.receive(line)); + } + + static async launch(options: TransportOptions): Promise> { + const child = spawn(options.executable, options.args, { + detached: process.platform !== 'win32', + env: options.env, + stdio: ['pipe', 'pipe', 'pipe'], + windowsHide: options.windowsHide, + }) as ChildProcessWithoutNullStreams; + child.on('error', () => {}); // prevent a late spawn failure from becoming unhandled + const transport = new CodexAppServerTransport(child, options); + try { + await transport.request('initialize', options.initializeParams, options.initializeTimeoutMs); + transport.notify('initialized', {}); + return transport; + } catch (error) { + const cleanupError = await transport.close(); + if (cleanupError) { + throw new AggregateError([asError(error), cleanupError], `${options.label} initialize and cleanup both failed`); + } + throw error; + } + } + + pid(): number { + const pid = this.child.pid; + if (pid === undefined) throw this.error('process', 'has no process id'); + return pid; + } + + request(method: string, params: unknown, timeoutMs: number): Promise { + if (this.closing) return Promise.reject(this.error('process', `${method} requested after close`)); + const id = this.nextId++; + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + this.pending.delete(id); + reject(this.error('timeout', `${method} timed out after ${timeoutMs}ms`)); + }, timeoutMs); + this.pending.set(id, { method, resolve, reject, timer }); + try { + this.child.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', id, method, params })}\n`, (writeError) => { + if (writeError) + this.reject(id, this.error('rpc', `${method} write failed: ${writeError.message}`, writeError)); + }); + } catch (error) { + this.reject(id, this.error('rpc', `${method} write failed`, asError(error))); + } + }); + } + + notify(method: string, params: unknown): void { + if (this.closing) return; + try { + this.child.stdin.write(`${JSON.stringify({ jsonrpc: '2.0', method, params })}\n`, () => {}); + } catch {} + } + + close(candidatePid?: number): Promise { + this.closing ??= this.closeOnce(candidatePid); + return this.closing; + } + + private async closeOnce(candidatePid: number | undefined): Promise { + this.rejectAll(this.error('process', 'evidence session closed')); + const appPid = this.child.pid; + const treePid = process.platform === 'win32' ? appPid : appPid === undefined ? undefined : -appPid; + const targets = [candidatePid, appPid, treePid]; + const failures: Error[] = []; + signal(candidatePid, this.options.initialSignal, failures); + signal(treePid, this.options.initialSignal, failures); + await waitStopped(targets, this.options.cleanupGraceMs); + signal(candidatePid, 'SIGKILL', failures); + signal(treePid, 'SIGKILL', failures); + await Promise.race([this.closed, delay(this.options.cleanupGraceMs)]); + await waitStopped(targets, this.options.cleanupGraceMs); + if (running(candidatePid)) failures.push(new Error(`candidate process ${String(candidatePid)} survived SIGKILL`)); + if (running(treePid)) failures.push(new Error(`app-server process group ${String(appPid)} survived SIGKILL`)); + if (running(appPid) || !this.didClose) + failures.push(new Error(`app-server process ${String(appPid)} did not close`)); + if (failures.length === 0) return null; + const message = `${this.options.label} cleanup failed: ${failures.map((failure) => failure.message).join('; ')}`; + return this.error('cleanup', message, new AggregateError(failures)); + } + + private receive(line: string): void { + let parsed: unknown; + try { + parsed = JSON.parse(line); + } catch { + return; + } + if (typeof parsed !== 'object' || parsed === null) return; + const message = parsed as AppServerJsonRpcEnvelope; + if (typeof message.id !== 'number') return; + const pending = this.pending.get(message.id); + if (!pending) return; + this.pending.delete(message.id); + clearTimeout(pending.timer); + if (message.error === undefined) pending.resolve(message); + else pending.reject(this.error('rpc', `${pending.method} error: ${truncate(message.error)}`)); + } + + private reject(id: number, error: Error): void { + const pending = this.pending.get(id); + if (!pending) return; + this.pending.delete(id); + clearTimeout(pending.timer); + pending.reject(error); + } + + private rejectAll(error: Error): void { + for (const id of [...this.pending.keys()]) this.reject(id, error); + } + + private error(kind: ErrorKind, message: string, cause?: Error): E { + return this.options.error(kind, `codex app-server ${message}`, cause); + } +} + +function signal(pid: number | undefined, value: NodeJS.Signals, failures: Error[]): void { + if (pid === undefined || Math.abs(pid) <= 1 || pid === process.pid) return; + try { + process.kill(pid, value); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'ESRCH') failures.push(asError(error)); + } +} + +function running(pid: number | undefined): boolean { + if (pid === undefined || Math.abs(pid) <= 1) return false; + try { + process.kill(pid, 0); + return true; + } catch (error) { + return (error as NodeJS.ErrnoException).code !== 'ESRCH'; + } +} + +async function waitStopped(pids: Array, timeoutMs: number): Promise { + const deadline = Date.now() + timeoutMs; + while (pids.some(running) && Date.now() < deadline) await delay(25); +} + +function truncate(value: unknown): string { + try { + return JSON.stringify(value).slice(0, 1_000); + } catch { + return String(value).slice(0, 1_000); + } +} + +function asError(error: unknown): Error { + return error instanceof Error ? error : new Error(String(error)); +} + +function delay(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} diff --git a/tests/support/codex-cwd-evidence.ts b/tests/support/codex-cwd-evidence.ts new file mode 100644 index 000000000..2b8255241 --- /dev/null +++ b/tests/support/codex-cwd-evidence.ts @@ -0,0 +1,326 @@ +/** + * Black-box `CodexCwdEvidence` harness (Group B deliverables 2 + 3) — a + * HARNESS-ONLY record, never a production observation. + * + * It drives ONE pinned long-lived real `codex app-server` over stdio JSON-RPC and, + * for each case, pairs the RAW `thread/start.cwd` request with the effective + * `process.cwd()` / OS directory identity / PID / tagged sentinel of the MCP + * server child that Codex launches for that thread (through an absolute fake MCP + * command with NO `cwd` override), plus a Codex-launched control process + * (`command/exec`) run in the same directory. + * + * This is the ONLY place the raw requested CWD lives. The production + * `CodexHostObservation` never receives or infers it. If any invariant here + * fails, Group B is BLOCKED and Group A may not migrate the plugin route. + * + * The proof is hermetic: `initialize`, `thread/start` (which eagerly launches the + * configured MCP servers), and `command/exec` require no model turn — a missing + * model credential only fails the async model websocket, never the MCP launch or + * the control exec that carry the CWD facts. + */ + +import { execFileSync } from 'node:child_process'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { CodexAppServerTransport } from './codex-app-server-transport.js'; + +/** Per-case black-box evidence pairing the raw request with observed child facts. */ +export interface CwdCaseEvidence { + tag: string; + /** The RAW `thread/start.cwd` request string — kept ONLY in this harness record. */ + rawRequestedCwd: string; + threadId: string; + /** The MCP server child's observed effective `process.cwd()`. */ + childEffectiveCwd: string; + /** `dev:ino` of the child's effective CWD. */ + childCwdIdentity: string; + childPid: number; + /** The tagged sentinel token the child read from its OWN cwd (null = wrong dir / cache root). */ + sentinelToken: string | null; +} + +/** A Codex-launched control process (`command/exec`) in a requested directory. */ +export interface ControlEvidence { + requestedCwd: string; + /** The control process's effective `process.cwd()`. */ + effectiveCwd: string; + /** `dev:ino` of the control's effective CWD. */ + cwdIdentity: string; + pid: number; +} + +export interface CodexCwdEvidenceSupport { + supported: boolean; + reason: string; +} + +const FAKE_MCP_SOURCE = String.raw` +import { appendFileSync, statSync, readFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { createInterface } from 'node:readline'; +const sentinelFile = process.argv[2]; +const tag = process.argv[3]; +(function record() { + const cwd = process.cwd(); + let dev = null, ino = null, token = null; + try { const st = statSync(cwd); dev = st.dev; ino = st.ino; } catch {} + try { token = readFileSync(join(cwd, '.genie-cwd-sentinel'), 'utf8').trim(); } catch {} + appendFileSync(sentinelFile, JSON.stringify({ tag, cwd, pid: process.pid, dev, ino, token }) + '\n'); +})(); +const rl = createInterface({ input: process.stdin }); +rl.on('line', (line) => { + const t = line.trim(); if (!t) return; + let msg; try { msg = JSON.parse(t); } catch { return; } + if (msg.method === 'initialize') { + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: msg.id, result: { protocolVersion: '2024-11-05', capabilities: { tools: {} }, serverInfo: { name: 'genie-cwd-probe', version: '0.0.0' } } }) + '\n'); + } else if (msg.method === 'tools/list') { + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: msg.id, result: { tools: [] } }) + '\n'); + } else if (msg.id !== undefined && msg.id !== null) { + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: msg.id, result: {} }) + '\n'); + } +}); +setInterval(() => {}, 1 << 30); +`; + +const CONTROL_REPORTER_SOURCE = String.raw` +import { statSync } from 'node:fs'; +const c = process.cwd(); +const s = statSync(c); +process.stdout.write(JSON.stringify({ cwd: c, dev: s.dev, ino: s.ino, pid: process.pid }) + '\n'); +`; + +export class CodexCwdEvidence { + private closePromise: Promise | null = null; + + private constructor( + private readonly transport: CodexAppServerTransport, + private readonly harnessRoot: string, + private readonly sentinelFile: string, + private readonly fakeMcpScript: string, + private readonly controlReporter: string, + private readonly nodeExecutable: string, + ) {} + + /** Start the pinned app-server and complete the `initialize` handshake. */ + static async launch( + codexCommand = 'codex', + initializeTimeoutMs = 15_000, + beforeSpawnForTest?: (harnessRoot: string) => void, + ): Promise { + // Deterministic preflight: an absent/unrunnable codex must fail as a caught + // rejection HERE so callers skip honestly. Without it, spawn() below emits an + // async 'error' (ENOENT) with no listener, which Node throws unhandled at + // module load — escaping the caller's try/catch and aborting the whole file + // (observed on CI: "Executable not found in $PATH" + afterAll-after-complete). + try { + execFileSync(codexCommand, ['--version'], { stdio: 'ignore' }); + } catch { + throw new Error(`codex binary not runnable in this environment: ${codexCommand}`); + } + const harnessRoot = mkdtempSync(join(tmpdir(), 'genie-cwd-evidence-')); + try { + const codexHome = join(harnessRoot, 'codex-home'); + mkdirSync(codexHome, { recursive: true }); + writeFileSync(join(codexHome, 'config.toml'), 'model = "gpt-5-codex"\n', 'utf8'); + const sentinelFile = join(harnessRoot, 'sentinel.jsonl'); + writeFileSync(sentinelFile, '', 'utf8'); + const fakeMcpScript = join(harnessRoot, 'fake-mcp.mjs'); + writeFileSync(fakeMcpScript, FAKE_MCP_SOURCE, 'utf8'); + const controlReporter = join(harnessRoot, 'control-reporter.mjs'); + writeFileSync(controlReporter, CONTROL_REPORTER_SOURCE, 'utf8'); + const nodeExecutable = process.execPath; + beforeSpawnForTest?.(harnessRoot); + + const transport = await CodexAppServerTransport.launch({ + executable: codexCommand, + args: ['app-server'], + env: { ...process.env, CODEX_HOME: codexHome, RUST_LOG: 'error' }, + initializeParams: { clientInfo: { name: 'genie-cwd-evidence', version: '0.0.0' } }, + initializeTimeoutMs, + cleanupGraceMs: 2_000, + initialSignal: 'SIGKILL', + label: 'codex app-server harness', + error: (_kind, message, cause) => new Error(message, cause ? { cause } : undefined), + }); + return new CodexCwdEvidence(transport, harnessRoot, sentinelFile, fakeMcpScript, controlReporter, nodeExecutable); + } catch (error) { + rmSync(harnessRoot, { recursive: true, force: true }); + throw error; + } + } + + /** + * Probe whether this environment can run the black-box proof: start the server, + * open a thread, and observe the MCP child land its sentinel. Returns a typed + * support verdict so the test skips honestly (never fakes) when the environment + * cannot host it, and cleans up either way. + */ + static async probeSupport(codexCommand = 'codex'): Promise { + let harness: CodexCwdEvidence | null = null; + try { + harness = await CodexCwdEvidence.launch(codexCommand); + const probeDir = harness.makeRepo('probe', 'PROBE_TOKEN'); + const evidence = await harness.startThreadCase('probe', probeDir); + if (evidence.sentinelToken !== 'PROBE_TOKEN') { + return { supported: false, reason: 'MCP child did not land in the thread cwd sentinel' }; + } + return { supported: true, reason: 'ok' }; + } catch (error) { + return { supported: false, reason: error instanceof Error ? error.message : String(error) }; + } finally { + await harness?.close(); + } + } + + /** Create an isolated repo dir under the harness root with a unique tagged sentinel token. */ + makeRepo(name: string, token: string): string { + const dir = join(this.harnessRoot, name); + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, '.genie-cwd-sentinel'), token, 'utf8'); + return dir; + } + + harnessRootDir(): string { + return this.harnessRoot; + } + + /** + * Start a thread whose only MCP server is the absolute fake command with NO cwd + * override, then collect the child's effective-CWD/PID/identity/sentinel evidence + * paired with the raw requested cwd. + */ + async startThreadCase(tag: string, requestedCwd: string, timeoutMs = 25_000): Promise { + const threadId = await this.openThread(tag, requestedCwd, timeoutMs); + const line = await this.waitForSentinel(tag, timeoutMs); + return { + tag, + rawRequestedCwd: requestedCwd, + threadId, + childEffectiveCwd: line.cwd, + childCwdIdentity: `${line.dev}:${line.ino}`, + childPid: line.pid, + sentinelToken: line.token, + }; + } + + /** Start several thread cases concurrently (fire all `thread/start` before awaiting sentinels). */ + async startThreadCasesConcurrent( + cases: Array<{ tag: string; requestedCwd: string }>, + timeoutMs = 25_000, + ): Promise { + // Fire every thread/start first, THEN await each sentinel, so the children run + // concurrently rather than strictly sequentially. + const opened = await Promise.all(cases.map((c) => this.openThread(c.tag, c.requestedCwd, timeoutMs))); + return Promise.all( + cases.map(async (c, index) => { + const line = await this.waitForSentinel(c.tag, timeoutMs); + return { + tag: c.tag, + rawRequestedCwd: c.requestedCwd, + threadId: opened[index] as string, + childEffectiveCwd: line.cwd, + childCwdIdentity: `${line.dev}:${line.ino}`, + childPid: line.pid, + sentinelToken: line.token, + }; + }), + ); + } + + /** Run a Codex-launched control process (`command/exec`) in a directory. */ + async runControl(requestedCwd: string, timeoutMs = 20_000): Promise { + const response = await this.transport.request( + 'command/exec', + { command: [this.nodeExecutable, this.controlReporter], cwd: requestedCwd }, + timeoutMs, + ); + const result = response.result as { exitCode?: number; stdout?: string } | undefined; + if (!result || result.exitCode !== 0 || typeof result.stdout !== 'string') { + throw new Error(`control command/exec failed: ${JSON.stringify(response).slice(0, 300)}`); + } + const parsed = JSON.parse(result.stdout.trim()) as { cwd: string; dev: number; ino: number; pid: number }; + return { + requestedCwd, + effectiveCwd: parsed.cwd, + cwdIdentity: `${parsed.dev}:${parsed.ino}`, + pid: parsed.pid, + }; + } + + async close(): Promise { + this.closePromise ??= this.closeOnce(); + await this.closePromise; + } + + private async closeOnce(): Promise { + try { + const cleanupError = await this.transport.close(); + if (cleanupError) throw cleanupError; + } finally { + rmSync(this.harnessRoot, { recursive: true, force: true }); + } + } + + private async openThread(tag: string, requestedCwd: string, timeoutMs: number): Promise { + const params = { + cwd: requestedCwd, + config: { + mcp_servers: { probe: { command: this.nodeExecutable, args: [this.fakeMcpScript, this.sentinelFile, tag] } }, + }, + }; + const response = await this.transport.request('thread/start', params, timeoutMs); + const thread = (response.result as { thread?: { id?: string } } | undefined)?.thread; + if (!thread?.id) throw new Error(`thread/start returned no thread id: ${JSON.stringify(response).slice(0, 300)}`); + return thread.id; + } + + private async waitForSentinel( + tag: string, + timeoutMs: number, + ): Promise<{ cwd: string; pid: number; dev: number; ino: number; token: string | null }> { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + const lines = readFileSync(this.sentinelFile, 'utf8') + .split('\n') + .map((l) => l.trim()) + .filter(Boolean); + for (const line of lines) { + const parsed = JSON.parse(line) as { + tag: string; + cwd: string; + pid: number; + dev: number; + ino: number; + token: string | null; + }; + if (parsed.tag === tag) return parsed; + } + await new Promise((r) => setTimeout(r, 200)); + } + throw new Error(`MCP child sentinel for tag=${tag} never appeared within ${timeoutMs}ms`); + } +} + +/** + * Enforce the PID-reuse invariant across a set of cases: a child PID may be reused + * ONLY when the effective-CWD string AND the OS directory identity both match. + * Differing effective CWD (string or identity) with a shared PID is a hard failure; + * repository/case labels alone do not permit reuse. Returns the offending pair on + * violation, or null when the invariant holds. + */ +export function findPidCrossingDifferingCwd( + cases: CwdCaseEvidence[], +): { a: CwdCaseEvidence; b: CwdCaseEvidence } | null { + for (let i = 0; i < cases.length; i++) { + for (let j = i + 1; j < cases.length; j++) { + const a = cases[i] as CwdCaseEvidence; + const b = cases[j] as CwdCaseEvidence; + if (a.childPid !== b.childPid) continue; + if (a.childEffectiveCwd !== b.childEffectiveCwd || a.childCwdIdentity !== b.childCwdIdentity) { + return { a, b }; + } + } + } + return null; +} diff --git a/tests/support/codex-dogfood-entry-runner.ts b/tests/support/codex-dogfood-entry-runner.ts new file mode 100644 index 000000000..bc51c7faf --- /dev/null +++ b/tests/support/codex-dogfood-entry-runner.ts @@ -0,0 +1,89 @@ +#!/usr/bin/env bun + +import type { DeliveryEvidencePlatformId } from '../../src/lib/codex-delivery-evidence.js'; +import { type DogfoodEntryInput, type GenerationInputPaths, runDogfoodEntry } from './codex-dogfood-harness.js'; + +const ENV_KEYS = { + previousArtifact: 'DOGFOOD_N_ARTIFACT', + previousManifest: 'DOGFOOD_N_MANIFEST', + previousProvenance: 'DOGFOOD_N_PROVENANCE', + previousBundle: 'DOGFOOD_N_BUNDLE', + candidateArtifact: 'DOGFOOD_T_ARTIFACT', + candidateManifest: 'DOGFOOD_T_MANIFEST', + candidateDescriptor: 'DOGFOOD_T_DESCRIPTOR', + candidateBundle: 'DOGFOOD_T_BUNDLE', + platformId: 'DOGFOOD_PLATFORM_ID', + outputEvidence: 'DOGFOOD_EVIDENCE_OUT', + executionAdapter: 'DOGFOOD_EXECUTION_ADAPTER', +} as const; + +const ARG_KEYS: Record = { + '--previous-artifact': 'previousArtifact', + '--previous-manifest': 'previousManifest', + '--previous-provenance': 'previousProvenance', + '--previous-bundle': 'previousBundle', + '--candidate-artifact': 'candidateArtifact', + '--candidate-manifest': 'candidateManifest', + '--candidate-descriptor': 'candidateDescriptor', + '--candidate-bundle': 'candidateBundle', + '--platform-id': 'platformId', + '--output-evidence': 'outputEvidence', + '--execution-adapter': 'executionAdapter', +}; + +function parseArgs(argv: string[]): Record { + const values = Object.fromEntries(Object.entries(ENV_KEYS).map(([key, env]) => [key, process.env[env]])) as Record< + keyof typeof ENV_KEYS, + string | undefined + >; + for (let index = 0; index < argv.length; index += 2) { + const flag = argv[index]; + const value = argv[index + 1]; + const key = flag === undefined ? undefined : ARG_KEYS[flag]; + if (key === undefined || value === undefined || value === '') throw new Error(`invalid argument: ${flag ?? ''}`); + values[key] = value; + } + return values; +} + +function required(values: Record, key: keyof typeof ENV_KEYS): string { + const value = values[key]; + if (value === undefined || value === '') { + throw new Error(`missing --${key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)} or ${ENV_KEYS[key]}`); + } + return value; +} + +function generation( + values: Record, + prefix: 'previous' | 'candidate', +): GenerationInputPaths { + return { + artifact: required(values, `${prefix}Artifact`), + manifest: required(values, `${prefix}Manifest`), + identity: prefix === 'previous' ? required(values, 'previousProvenance') : required(values, 'candidateDescriptor'), + bundle: required(values, `${prefix}Bundle`), + identityKind: prefix === 'previous' ? 'slsa-provenance' : 'delivery-descriptor', + }; +} + +async function main(): Promise { + const values = parseArgs(process.argv.slice(2)); + const input: DogfoodEntryInput = { + previous: generation(values, 'previous'), + candidate: generation(values, 'candidate'), + platformId: required(values, 'platformId') as DeliveryEvidencePlatformId, + outputEvidence: required(values, 'outputEvidence'), + executionAdapter: values.executionAdapter, + evidenceKind: 'host-native', + }; + const result = await runDogfoodEntry(input); + process.stdout.write(`codex-dogfood-entry: OK ${result.outputEvidence}\n`); +} + +try { + await main(); +} catch (error) { + process.stderr.write(`codex-dogfood-entry: FAIL — ${error instanceof Error ? error.message : String(error)}\n`); + process.exitCode = 1; +} diff --git a/tests/support/codex-dogfood-fixture.ts b/tests/support/codex-dogfood-fixture.ts new file mode 100644 index 000000000..825fab815 --- /dev/null +++ b/tests/support/codex-dogfood-fixture.ts @@ -0,0 +1,150 @@ +import { execFileSync } from 'node:child_process'; +import { createHash } from 'node:crypto'; +import { chmodSync, cpSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import packageMetadata from '../../package.json'; +import { scanPhysicalTree } from '../../src/lib/codex-activation.js'; +import { buildTestDeliveryEvidencePack } from '../../src/lib/codex-delivery-evidence.test-support.js'; +import type { DogfoodEntryInput, DogfoodHarnessDependencies } from './codex-dogfood-harness.js'; + +const REPO_ROOT = join(import.meta.dir, '..', '..'); +export const FIXTURE_N = '5.260720.10'; +export const FIXTURE_T = packageMetadata.version; + +function sha256(path: string): string { + return createHash('sha256').update(readFileSync(path)).digest('hex'); +} + +function platformId(): 'darwin-arm64' | 'linux-arm64' | 'linux-x64-glibc' { + if (process.platform === 'darwin') return 'darwin-arm64'; + return process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64-glibc'; +} + +function stageRelease( + root: string, + version: string, +): { releaseRoot: string; binarySha256: string; payloadSha256: string } { + const releaseRoot = join(root, `release-${version}`); + mkdirSync(releaseRoot, { recursive: true }); + cpSync(join(REPO_ROOT, 'plugins', 'genie'), join(releaseRoot, 'plugins', 'genie'), { recursive: true }); + const binary = join(releaseRoot, 'genie'); + writeFileSync( + binary, + [ + `#!${process.execPath}`, + "import { createHash } from 'node:crypto';", + "import { readFileSync } from 'node:fs';", + `const version = ${JSON.stringify(version)};`, + 'const args = process.argv.slice(2);', + "if (args[0] === '--version') { process.stdout.write(`genie ${version}\\n`); process.exit(0); }", + "if (args.join(' ') === 'update --print-update-capabilities --json') {", + " const binarySha256 = createHash('sha256').update(readFileSync(process.argv[1])).digest('hex');", + " process.stdout.write(JSON.stringify({ schemaVersion: 1, reportedVersion: version, binarySha256, codexActivationProtocol: 1, readableIntentSchemas: [1] }) + '\\n');", + ' process.exit(0);', + '}', + `const sourceCli = ${JSON.stringify(join(REPO_ROOT, 'src', 'genie.ts'))};`, + `const lifecycleRunner = ${JSON.stringify(join(REPO_ROOT, 'tests', 'support', 'codex-lifecycle-test-runner.ts'))};`, + 'let target = sourceCli;', + 'let forwarded = args;', + "if (args[0] === 'setup' && args.includes('--codex')) { target = lifecycleRunner; forwarded = args; }", + "else if (args[0] === 'doctor') { target = lifecycleRunner; forwarded = args; }", + "else if (args[0] === 'update' && args[1] === '--publish-local-delivery' && args.length === 3) {", + " target = lifecycleRunner; forwarded = ['publish-local-delivery', args[2]];", + '}', + "const child = Bun.spawn([process.execPath, target, ...forwarded], { cwd: process.cwd(), env: process.env, stdin: 'inherit', stdout: 'inherit', stderr: 'inherit' });", + 'process.exit(await child.exited);', + '', + ].join('\n'), + ); + chmodSync(binary, 0o755); + writeFileSync(join(releaseRoot, 'VERSION'), `${version}\n`); + const tree = scanPhysicalTree(join(releaseRoot, 'plugins', 'genie')); + if (tree.status !== 'ok' || tree.digest === undefined) throw new Error('fixture payload could not be hashed'); + return { releaseRoot, binarySha256: sha256(binary), payloadSha256: tree.digest }; +} + +export function buildDogfoodFixture(root: string): { + input: DogfoodEntryInput; + dependencies: DogfoodHarnessDependencies; +} { + const platform = platformId(); + const previous = stageRelease(root, FIXTURE_N); + const candidate = stageRelease(root, FIXTURE_T); + const previousArtifact = join(root, `genie-${FIXTURE_N}-${platform}.tar.gz`); + const candidateArtifact = join(root, `genie-${FIXTURE_T}-${platform}.tar.gz`); + execFileSync('tar', ['-czf', previousArtifact, '-C', previous.releaseRoot, '.']); + execFileSync('tar', ['-czf', candidateArtifact, '-C', candidate.releaseRoot, '.']); + + const previousManifest = join(root, 'previous-stable.json'); + writeFileSync( + previousManifest, + `${JSON.stringify({ + schema_version: 1, + channel: 'stable', + version: FIXTURE_N, + released_at: '2026-07-20T00:00:00Z', + tarball_base: `https://github.com/automagik-dev/genie/releases/download/v${FIXTURE_N}`, + platforms: [platform], + })}\n`, + ); + const previousBundle = `${previousArtifact}.bundle`; + const previousProvenance = `${previousArtifact}.intoto.jsonl`; + writeFileSync(previousBundle, '{"fixture":"cosign identity verified by test dependency"}\n'); + writeFileSync(previousProvenance, '{"fixture":"SLSA identity verified by test dependency"}\n'); + + const pack = buildTestDeliveryEvidencePack({ + descriptor: { + version: FIXTURE_T, + channel: 'stable', + platformId: platform, + platformTriple: `${process.platform}-${process.arch}`, + releaseTag: `v${FIXTURE_T}`, + releaseName: `genie-${FIXTURE_T}-${platform}.tar.gz`, + artifactSha256: sha256(candidateArtifact), + installedBinarySha256: candidate.binarySha256, + canonicalPayloadSha256: candidate.payloadSha256, + sourceSha: 'a'.repeat(40), + controlSha: 'b'.repeat(40), + sourceBranch: 'main', + }, + manifest: { platforms: [platform] }, + }); + const candidateManifest = join(root, 'candidate-stable.json'); + const candidateDescriptor = `${candidateArtifact}.stable.delivery.json`; + const candidateBundle = `${candidateDescriptor}.sigstore.json`; + writeFileSync(candidateManifest, pack.manifestBytes); + writeFileSync(candidateDescriptor, pack.descriptorBytes); + writeFileSync(candidateBundle, pack.bundleBytes); + + return { + input: { + previous: { + artifact: previousArtifact, + manifest: previousManifest, + identity: previousProvenance, + bundle: previousBundle, + identityKind: 'slsa-provenance', + }, + candidate: { + artifact: candidateArtifact, + manifest: candidateManifest, + identity: candidateDescriptor, + bundle: candidateBundle, + identityKind: 'delivery-descriptor', + }, + platformId: platform, + evidenceKind: 'verified-local-fixture', + outputEvidence: join(root, 'evidence', `codex-dogfood-${FIXTURE_T}-${platform}.md`), + }, + dependencies: { + root: join(root, 'run'), + deliveryEvidenceVerification: pack.dependencies, + verifyLegacyProvenance: () => ({ + sourceCommit: 'c'.repeat(40), + sourceBranch: 'main', + sourceCiRunId: '26072010', + controlCommit: 'd'.repeat(40), + }), + }, + }; +} diff --git a/tests/support/codex-dogfood-harness.ts b/tests/support/codex-dogfood-harness.ts new file mode 100644 index 000000000..9e43b4b51 --- /dev/null +++ b/tests/support/codex-dogfood-harness.ts @@ -0,0 +1,1733 @@ +/** + * Parameterized Group-F dogfood harness. + * + * Release inputs cross the boundary only as explicit paths. Both evidence packs + * go through the production descriptor/manifest/bundle verifier, extracted + * payloads go through production physical-tree hashing and capability parsing, + * the candidate delivery is published by the production deep store, activation + * uses the real consent/permit/executor path, role agents use the production + * convergence function, and repository observations use the real stdio MCP + * server. The only injectable seam is the cryptographic bundle verifier and is + * exposed to tests through this support module, never by the CLI runner. + */ + +import { execFileSync } from 'node:child_process'; +import { createHash, randomBytes } from 'node:crypto'; +import { + chmodSync, + copyFileSync, + cpSync, + existsSync, + lstatSync, + mkdirSync, + mkdtempSync, + readFileSync, + readdirSync, + realpathSync, + rmSync, + statSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { basename, dirname, isAbsolute, join, resolve } from 'node:path'; +import { + LIVE_DOGFOOD_SCHEMA_VERSION, + type REQUIRED_STAGE_IDS, + validateLiveDogfoodEvidence, + validateLiveDogfoodEvidenceFile, +} from '../../scripts/validate-live-dogfood-evidence.js'; +import { compareReleaseVersions, parseReleaseVersion, scanPhysicalTree } from '../../src/lib/codex-activation.js'; +import { + DELIVERY_EVIDENCE_OIDC_ISSUER, + DELIVERY_EVIDENCE_PREDICATE_TYPE, + DELIVERY_EVIDENCE_REPOSITORY, + DELIVERY_EVIDENCE_WORKFLOW_IDENTITY, + type DeliveryEvidenceDescriptor, + type DeliveryEvidencePlatformId, + type DeliveryEvidenceVerificationDependencies, + type VerifiedDeliveryEvidence, + verifiedDeliveryEvidenceFacts, + verifyDownloadedDeliveryEvidence, +} from '../../src/lib/codex-delivery-evidence.js'; +import { parseUpdateCapabilityReport } from '../../src/lib/update-capabilities.js'; +const PLATFORM_TRIPLES: Readonly> = { + 'linux-x64-glibc': 'linux-x64', + 'linux-x64-musl': 'linux-x64', + 'linux-arm64': 'linux-arm64', + 'darwin-arm64': 'darwin-arm64', +}; + +export interface GenerationInputPaths { + artifact: string; + manifest: string; + identity: string; + bundle: string; + identityKind: 'slsa-provenance' | 'delivery-descriptor'; +} + +export interface DogfoodEntryInput { + previous: GenerationInputPaths; + candidate: GenerationInputPaths; + platformId: DeliveryEvidencePlatformId; + outputEvidence?: string; + /** Executable invoked as: adapter update --print-update-capabilities --json. */ + executionAdapter?: string; + evidenceKind?: 'host-native' | 'verified-local-fixture'; +} + +export interface DogfoodHarnessDependencies { + /** Test-only cryptographic seam. The CLI never supplies this. */ + deliveryEvidenceVerification?: DeliveryEvidenceVerificationDependencies; + /** Test-only substitute for external cosign+slsa verification of historical N. */ + verifyLegacyProvenance?: (input: { + artifact: string; + bundle: string; + provenance: string; + version: string; + root: string; + }) => LegacyProvenanceFacts; + /** Test-only substitute for the real Codex app-server → exact-binary MCP proof. */ + observeNativeMcp?: (input: NativeMcpEvidenceInput) => Promise; + root?: string; +} + +export interface LegacyProvenanceFacts { + sourceCommit: string; + sourceBranch: string; + sourceCiRunId: string; + controlCommit: string; +} + +export interface VerifiedGeneration { + paths: GenerationInputPaths; + facts: GenerationFacts; + evidence: VerifiedDeliveryEvidence | null; + evidenceDigest: string; + manifestSha256: string; + artifactSha256: string; + binarySha256: string; + payloadSha256: string; + identitySha256: string; + bundleSha256: string; + releaseRoot: string; + binaryPath: string; + payloadPath: string; +} + +interface GenerationFacts { + version: string; + channel: DeliveryEvidenceDescriptor['channel']; + platformId: DeliveryEvidencePlatformId; + platformTriple: string; + releaseTag: string; + releaseName: string; + releaseManifestSha256: string; + artifactSha256: string; + installedBinarySha256: string; + canonicalPayloadSha256: string; + sourceSha: string; + sourceBranch: string; + sourceCiRunId: string; + controlSha: string; +} + +interface TaskIdentity { + wish: string; + taskId: string; + title: string; + status: 'in_progress'; + claimedBy: string; +} + +interface RepoObservation { + root: string; + requestedCwd: string; + effectiveCwd: string; + cwdIdentity: string; + childPid: number; + sentinel: { + token: string; + expected: TaskIdentity; + observed: TaskIdentity; + boardCount: 1; + }; + routeState?: 'managed-project'; +} + +interface BoardResult { + pid: number; + isError: boolean; + payload: Record; +} + +export interface NativeMcpEvidenceInput { + tag: string; + requestedCwd: string; + candidateBinary: string; + candidateBinarySha256: string; + executionAdapter?: string; + root: string; + env: Record; +} + +export interface NativeMcpEvidence { + requestedCwd: string; + effectiveCwd: string; + cwdIdentity: string; + controlCwd: string; + controlCwdIdentity: string; + childPid: number; + threadId: string; + isError: boolean; + payload: Record; + raw: Record; +} + +interface CapturedCommand { + executable: string; + executableSha256: string; + candidateBinary: string | null; + candidateBinarySha256: string | null; + argv: string[]; + pid: number; + requestedCwd: string; + cwdIdentity: string; + exit: number; + stdout: string; + stderr: string; +} + +interface StageProjection { + id: (typeof REQUIRED_STAGE_IDS)[number]; + command: string; + exit: number; + humanState: string; + jsonState: string; + activeVersion: string; + trailer: Record | null; + observationPath: string; + observationSha256: string; + observation: { + schemaVersion: 1; + commands: CapturedCommand[]; + }; +} + +export interface DogfoodRunResult { + evidence: string; + manifest: Record; + doctor: Record; + outputEvidence?: string; +} + +function isolatedDogfoodEnv(root: string, overrides: Record = {}): Record { + const home = join(root, 'process-home'); + const temp = join(root, 'tmp'); + const xdgConfig = join(home, '.config'); + const xdgCache = join(home, '.cache'); + const xdgData = join(home, '.local', 'share'); + const xdgState = join(home, '.local', 'state'); + const genieHome = join(root, 'genie-home'); + const codexHome = join(root, 'codex-home'); + const bin = join(root, 'bin'); + for (const path of [home, temp, xdgConfig, xdgCache, xdgData, xdgState, genieHome, codexHome, bin]) { + mkdirSync(path, { recursive: true }); + } + const env: Record = { + PATH: `${bin}:${process.env.PATH ?? '/usr/bin:/bin'}`, + HOME: home, + GENIE_HOME: genieHome, + CODEX_HOME: codexHome, + CLAUDE_CONFIG_DIR: join(home, '.claude'), + HERMES_HOME: join(home, '.hermes'), + GENIE_AGENTS_SKILLS_DIR: join(home, '.agents', 'skills'), + TMPDIR: temp, + XDG_CONFIG_HOME: xdgConfig, + XDG_CACHE_HOME: xdgCache, + XDG_DATA_HOME: xdgData, + XDG_STATE_HOME: xdgState, + BUN_INSTALL_CACHE_DIR: join(xdgCache, 'bun'), + NPM_CONFIG_CACHE: join(xdgCache, 'npm'), + GIT_CONFIG_GLOBAL: join(home, '.gitconfig'), + GIT_CONFIG_NOSYSTEM: '1', + GENIE_TEST_SKIP_PGSERVE: '1', + NO_COLOR: '1', + ...overrides, + }; + for (const key of ['LANG', 'LC_ALL', 'SSL_CERT_FILE', 'SSL_CERT_DIR', 'NODE_EXTRA_CA_CERTS'] as const) { + const value = process.env[key]; + if (value !== undefined) env[key] = value; + } + return env; +} + +export async function runDogfoodEntry( + rawInput: DogfoodEntryInput, + dependencies: DogfoodHarnessDependencies = {}, +): Promise { + const input = normalizeInput(rawInput); + const ownsRoot = dependencies.root === undefined; + const root = dependencies.root ?? mkdtempSync(join(tmpdir(), 'genie-dogfood-entry-')); + mkdirSync(root, { recursive: true }); + try { + const previous = verifyGeneration( + 'previous', + input.previous, + input.platformId, + input.executionAdapter, + root, + dependencies, + ); + const candidate = verifyGeneration( + 'candidate', + input.candidate, + input.platformId, + input.executionAdapter, + root, + dependencies, + ); + const previousVersion = parseReleaseVersion(previous.facts.version); + const candidateVersion = parseReleaseVersion(candidate.facts.version); + if ( + previousVersion === null || + candidateVersion === null || + compareReleaseVersions(previousVersion, candidateVersion) >= 0 + ) { + throw new Error('previous stable N must be older than candidate T'); + } + if (previous.artifactSha256 === candidate.artifactSha256) { + throw new Error('previous and candidate artifacts unexpectedly have the same digest'); + } + const expectedEvidenceName = `codex-dogfood-${candidate.facts.version}-${input.platformId}.md`; + if (input.outputEvidence !== undefined && basename(input.outputEvidence) !== expectedEvidenceName) { + throw new Error(`output evidence filename must be ${expectedEvidenceName}`); + } + + const lifecycle = await runLifecycle(root, input, previous, candidate, dependencies); + const repositories = lifecycle.repositories; + if (input.outputEvidence !== undefined) { + stageEvidenceInputs(dirname(input.outputEvidence), previous, candidate, lifecycle.stages); + } + const manifest = buildManifest(input, previous, candidate, lifecycle, repositories); + const doctor = lifecycle.doctor; + const evidence = renderEvidence(manifest, doctor); + const errors = validateLiveDogfoodEvidence(evidence); + if (errors.length > 0) throw new Error(`generated evidence failed validation:\n${errors.join('\n')}`); + if (input.outputEvidence !== undefined) { + mkdirSync(dirname(input.outputEvidence), { recursive: true }); + writeFileSync(input.outputEvidence, evidence); + const fileErrors = validateLiveDogfoodEvidenceFile(input.outputEvidence, dirname(input.outputEvidence)); + if (fileErrors.length > 0) throw new Error(`staged evidence failed validation:\n${fileErrors.join('\n')}`); + } + return { evidence, manifest, doctor, outputEvidence: input.outputEvidence }; + } finally { + if (ownsRoot) rmSync(root, { recursive: true, force: true }); + } +} + +function stageEvidenceInputs( + outputRoot: string, + previous: VerifiedGeneration, + candidate: VerifiedGeneration, + stages: StageProjection[], +): void { + mkdirSync(outputRoot, { recursive: true }); + for (const [label, generation] of [ + ['previous', previous], + ['candidate', candidate], + ] as const) { + const target = join(outputRoot, label); + mkdirSync(target, { recursive: true }); + for (const key of ['artifact', 'manifest', 'identity', 'bundle'] as const) { + const path = generation.paths[key]; + const destination = join(target, basename(path)); + if (realpathIfPresent(destination) === realpathSync(path)) continue; + copyFileSync(path, destination); + } + } + const observationsRoot = join(outputRoot, 'observations'); + mkdirSync(observationsRoot, { recursive: true }); + for (const value of stages) { + const destination = join(outputRoot, value.observationPath); + writeFileSync(destination, serializeStageObservation(value.observation)); + if (sha256File(destination) !== value.observationSha256) { + throw new Error(`staged ${value.id} observation digest drifted`); + } + } +} + +function realpathIfPresent(path: string): string | null { + try { + return realpathSync(path); + } catch { + return null; + } +} + +function normalizeInput(input: DogfoodEntryInput): DogfoodEntryInput { + const normalizeGeneration = (label: string, value: GenerationInputPaths): GenerationInputPaths => { + const out = {} as GenerationInputPaths; + for (const key of ['artifact', 'manifest', 'identity', 'bundle'] as const) { + const path = resolve(value[key]); + if (!isAbsolute(path) || !existsSync(path) || !lstatSync(path).isFile()) { + throw new Error(`${label} ${key} is unavailable or not a regular file: ${path}`); + } + out[key] = realpathSync(path); + } + out.identityKind = value.identityKind; + return out; + }; + if (!(input.platformId in PLATFORM_TRIPLES)) throw new Error(`unsupported platform id: ${input.platformId}`); + const executionAdapter = + input.executionAdapter === undefined ? undefined : realpathSync(resolve(input.executionAdapter)); + if (executionAdapter !== undefined && !lstatSync(executionAdapter).isFile()) { + throw new Error(`execution adapter is not a regular file: ${executionAdapter}`); + } + return { + ...input, + previous: normalizeGeneration('previous', input.previous), + candidate: normalizeGeneration('candidate', input.candidate), + outputEvidence: input.outputEvidence === undefined ? undefined : resolve(input.outputEvidence), + executionAdapter, + evidenceKind: input.evidenceKind ?? 'host-native', + }; +} + +function verifyGeneration( + label: 'previous' | 'candidate', + paths: GenerationInputPaths, + platformId: DeliveryEvidencePlatformId, + executionAdapter: string | undefined, + root: string, + dependencies: DogfoodHarnessDependencies, +): VerifiedGeneration { + const identityBytes = readFileSync(paths.identity); + const bundleBytes = readFileSync(paths.bundle); + const manifestBytes = readFileSync(paths.manifest); + const artifactSha256 = sha256File(paths.artifact); + const manifest = parseReleaseManifest(label, manifestBytes, platformId); + const releaseName = `genie-${manifest.version}-${platformId}.tar.gz`; + if (basename(paths.artifact) !== releaseName) throw new Error(`${label} artifact name is not manifest-derived`); + + const authenticated = authenticateGenerationInputs({ + label, + paths, + platformId, + manifest, + manifestBytes, + identityBytes, + bundleBytes, + artifactSha256, + releaseName, + root, + dependencies, + }); + + const extractRoot = join(root, `${label}-extract`); + mkdirSync(extractRoot, { recursive: true }); + execFileSync('tar', ['-xzf', paths.artifact, '-C', extractRoot], { + env: isolatedDogfoodEnv(root), + stdio: 'pipe', + }); + const releaseRoot = findReleaseRoot(extractRoot); + const binaryPath = join(releaseRoot, 'genie'); + const payloadPath = join(releaseRoot, 'plugins', 'genie'); + if (!existsSync(binaryPath) || !lstatSync(binaryPath).isFile()) + throw new Error(`${label} extracted binary unavailable`); + const tree = scanPhysicalTree(payloadPath); + if (tree.status !== 'ok' || tree.digest === undefined) throw new Error(`${label} extracted payload is invalid`); + const binarySha256 = sha256File(binaryPath); + verifyCapability(binaryPath, manifest.version, binarySha256, executionAdapter, root); + + const bound = bindExtractedGeneration({ + label, + authenticated, + manifest, + manifestBytes, + identityBytes, + bundleBytes, + artifactSha256, + releaseName, + platformId, + binarySha256, + payloadSha256: tree.digest, + }); + return { + paths, + facts: bound.facts, + evidence: bound.evidence, + evidenceDigest: bound.evidenceDigest, + manifestSha256: sha256Bytes(manifestBytes), + artifactSha256, + binarySha256, + payloadSha256: tree.digest, + identitySha256: sha256Bytes(identityBytes), + bundleSha256: sha256Bytes(bundleBytes), + releaseRoot, + binaryPath, + payloadPath, + }; +} + +type ParsedReleaseManifest = ReturnType; +type AuthenticatedGeneration = + | { kind: 'delivery-descriptor'; descriptor: DeliveryEvidenceDescriptor; evidence: VerifiedDeliveryEvidence } + | { kind: 'slsa-provenance'; provenance: LegacyProvenanceFacts }; + +function authenticateGenerationInputs(input: { + label: 'previous' | 'candidate'; + paths: GenerationInputPaths; + platformId: DeliveryEvidencePlatformId; + manifest: ParsedReleaseManifest; + manifestBytes: Buffer; + identityBytes: Buffer; + bundleBytes: Buffer; + artifactSha256: string; + releaseName: string; + root: string; + dependencies: DogfoodHarnessDependencies; +}): AuthenticatedGeneration { + if (input.paths.identityKind === 'slsa-provenance') { + const provenance = (input.dependencies.verifyLegacyProvenance ?? verifyLegacyReleaseProvenance)({ + artifact: input.paths.artifact, + bundle: input.paths.bundle, + provenance: input.paths.identity, + version: input.manifest.version, + root: input.root, + }); + return { kind: 'slsa-provenance', provenance }; + } + const descriptor = JSON.parse(input.identityBytes.toString('utf8')) as DeliveryEvidenceDescriptor; + if (descriptor.platformId !== input.platformId || descriptor.platformTriple !== PLATFORM_TRIPLES[input.platformId]) { + throw new Error(`${input.label} descriptor platform identity is inconsistent`); + } + if ( + descriptor.version !== input.manifest.version || + descriptor.channel !== input.manifest.channel || + descriptor.releaseName !== input.releaseName || + descriptor.artifactSha256 !== input.artifactSha256 + ) { + throw new Error(`${input.label} delivery descriptor does not bind the authenticated artifact`); + } + const evidence = verifyDownloadedDeliveryEvidence( + { + descriptorBytes: input.identityBytes, + bundleBytes: input.bundleBytes, + manifestBytes: input.manifestBytes, + targetVersion: descriptor.version, + channel: descriptor.channel, + platformId: descriptor.platformId, + platformTriple: descriptor.platformTriple, + releaseTag: descriptor.releaseTag, + releaseName: descriptor.releaseName, + artifactSha256: descriptor.artifactSha256, + installedBinarySha256: descriptor.installedBinarySha256, + canonicalPayloadSha256: descriptor.canonicalPayloadSha256, + }, + input.dependencies.deliveryEvidenceVerification, + ); + return { kind: 'delivery-descriptor', descriptor, evidence }; +} + +function bindExtractedGeneration(input: { + label: 'previous' | 'candidate'; + authenticated: AuthenticatedGeneration; + manifest: ParsedReleaseManifest; + manifestBytes: Buffer; + identityBytes: Buffer; + bundleBytes: Buffer; + artifactSha256: string; + releaseName: string; + platformId: DeliveryEvidencePlatformId; + binarySha256: string; + payloadSha256: string; +}): Pick { + if (input.authenticated.kind === 'delivery-descriptor') { + const { descriptor, evidence } = input.authenticated; + if ( + descriptor.installedBinarySha256 !== input.binarySha256 || + descriptor.canonicalPayloadSha256 !== input.payloadSha256 + ) { + throw new Error(`${input.label} delivery descriptor does not bind the extracted artifact`); + } + return { + facts: descriptor, + evidence, + evidenceDigest: verifiedDeliveryEvidenceFacts(evidence).evidenceDigest, + }; + } + const { provenance } = input.authenticated; + return { + facts: { + version: input.manifest.version, + channel: input.manifest.channel, + platformId: input.platformId, + platformTriple: PLATFORM_TRIPLES[input.platformId], + releaseTag: `v${input.manifest.version}`, + releaseName: input.releaseName, + releaseManifestSha256: sha256Bytes(input.manifestBytes), + artifactSha256: input.artifactSha256, + installedBinarySha256: input.binarySha256, + canonicalPayloadSha256: input.payloadSha256, + sourceSha: provenance.sourceCommit, + sourceBranch: provenance.sourceBranch, + sourceCiRunId: provenance.sourceCiRunId, + controlSha: provenance.controlCommit, + }, + evidence: null, + evidenceDigest: createHash('sha256') + .update('genie-legacy-release-proof-v1\0') + .update(input.manifestBytes) + .update(input.identityBytes) + .update(input.bundleBytes) + .digest('hex'), + }; +} + +function parseReleaseManifest( + label: string, + bytes: Uint8Array, + platformId: DeliveryEvidencePlatformId, +): { + schema_version: 1; + channel: DeliveryEvidenceDescriptor['channel']; + version: string; + platforms: string[]; +} { + const value = JSON.parse(Buffer.from(bytes).toString('utf8')) as { + schema_version?: unknown; + channel?: unknown; + version?: unknown; + platforms?: unknown; + }; + const valid = + value.schema_version === 1 && + ['stable', 'homolog', 'dev'].includes(String(value.channel)) && + typeof value.version === 'string' && + Array.isArray(value.platforms) && + value.platforms.includes(platformId); + if (!valid) throw new Error(`${label} release manifest does not bind the requested version/channel/platform`); + return value as { + schema_version: 1; + channel: DeliveryEvidenceDescriptor['channel']; + version: string; + platforms: string[]; + }; +} + +function findReleaseRoot(extractRoot: string): string { + if (existsSync(join(extractRoot, 'genie')) && existsSync(join(extractRoot, 'plugins', 'genie'))) return extractRoot; + const children = readdirSync(extractRoot).filter((name) => lstatSync(join(extractRoot, name)).isDirectory()); + if (children.length === 1) { + const nested = join(extractRoot, children[0] as string); + if (existsSync(join(nested, 'genie')) && existsSync(join(nested, 'plugins', 'genie'))) return nested; + } + throw new Error('release archive does not contain one extracted Genie release root'); +} + +function verifyCapability( + binary: string, + version: string, + binarySha256: string, + executionAdapter: string | undefined, + root: string, +): void { + const args = ['update', '--print-update-capabilities', '--json']; + const command = executionAdapter ?? binary; + const commandArgs = executionAdapter === undefined ? args : [binary, ...args]; + const result = Bun.spawnSync([command, ...commandArgs], { + env: isolatedDogfoodEnv(root), + stdout: 'pipe', + stderr: 'pipe', + timeout: 60_000, + }); + if (result.exitCode !== 0) { + throw new Error(`native capability probe unavailable (${result.exitCode}): ${result.stderr.toString().trim()}`); + } + if (result.stderr.length > 0) + throw new Error(`native capability probe wrote stderr: ${result.stderr.toString().trim()}`); + const report = parseUpdateCapabilityReport(result.stdout.toString().trim()); + if (report === null) throw new Error('native capability probe did not return schema-valid JSON'); + if (report.reportedVersion !== version) throw new Error('native capability probe version mismatch'); + if (report.binarySha256 !== binarySha256) throw new Error('native capability probe binary digest mismatch'); +} + +function verifyLegacyReleaseProvenance(input: { + artifact: string; + bundle: string; + provenance: string; + version: string; + root: string; +}): LegacyProvenanceFacts { + const cosign = Bun.which('cosign'); + const slsaVerifier = Bun.which('slsa-verifier'); + if (cosign === null) throw new Error('cosign is unavailable for previous-release verification'); + if (slsaVerifier === null) throw new Error('slsa-verifier is unavailable for previous-release verification'); + execFileSync( + cosign, + [ + 'verify-blob', + '--bundle', + input.bundle, + '--certificate-identity', + 'https://github.com/automagik-dev/genie/.github/workflows/sign-attest.yml@refs/heads/main', + '--certificate-oidc-issuer', + DELIVERY_EVIDENCE_OIDC_ISSUER, + input.artifact, + ], + { env: isolatedDogfoodEnv(input.root), stdio: 'pipe' }, + ); + const verified = execFileSync( + slsaVerifier, + [ + 'verify-artifact', + input.artifact, + '--provenance-path', + input.provenance, + '--source-uri', + `github.com/${DELIVERY_EVIDENCE_REPOSITORY}`, + '--source-branch', + 'main', + '--print-provenance', + ], + { encoding: 'utf8', env: isolatedDogfoodEnv(input.root), stdio: ['ignore', 'pipe', 'pipe'] }, + ); + const verifyRoot = mkdtempSync(join(input.root, 'legacy-provenance-')); + try { + const verifiedPath = join(verifyRoot, 'verified.json'); + writeFileSync(verifiedPath, verified); + execFileSync( + 'bash', + [join(import.meta.dir, '..', '..', 'scripts', 'release-generic-provenance.sh'), 'verify-reusable', verifiedPath], + { + env: { + ...isolatedDogfoodEnv(input.root), + RELEASE_REPOSITORY: DELIVERY_EVIDENCE_REPOSITORY, + VERSION: input.version, + }, + stdio: 'pipe', + }, + ); + const statement = JSON.parse(verified) as { + predicate?: { + invocation?: { + configSource?: { digest?: { sha1?: unknown } }; + parameters?: { event_inputs?: Record }; + }; + }; + }; + const parameters = statement.predicate?.invocation?.parameters?.event_inputs; + const controlCommit = statement.predicate?.invocation?.configSource?.digest?.sha1; + const facts = { + sourceCommit: parameters?.source_sha, + sourceBranch: parameters?.source_branch, + sourceCiRunId: parameters?.source_ci_run_id, + controlCommit, + }; + if ( + typeof facts.sourceCommit !== 'string' || + !/^[0-9a-f]{40}$/.test(facts.sourceCommit) || + typeof facts.sourceBranch !== 'string' || + !/^(?:main|homolog|dev)$/.test(facts.sourceBranch) || + typeof facts.sourceCiRunId !== 'string' || + !/^(?:0|[1-9]\d*)$/.test(facts.sourceCiRunId) || + typeof facts.controlCommit !== 'string' || + !/^[0-9a-f]{40}$/.test(facts.controlCommit) + ) { + throw new Error('verified previous-release provenance omitted a required identity'); + } + return facts as LegacyProvenanceFacts; + } finally { + rmSync(verifyRoot, { recursive: true, force: true }); + } +} + +function captureCommand(input: { + root: string; + binary: string; + args: string[]; + cwd: string; + env: Record; + executionAdapter?: string; + stdin?: Uint8Array; + timeoutMs?: number; +}): CapturedCommand { + const command = input.executionAdapter ?? input.binary; + const argv = input.executionAdapter === undefined ? input.args : [input.binary, ...input.args]; + const env = + input.executionAdapter === undefined + ? input.env + : { ...input.env, DOGFOOD_ROOT: input.root, DOGFOOD_ADAPTER_CWD: realpathSync(input.cwd) }; + const result = Bun.spawnSync([command, ...argv], { + cwd: input.cwd, + env, + stdin: input.stdin, + stdout: 'pipe', + stderr: 'pipe', + timeout: input.timeoutMs ?? 120_000, + }); + if (result.exitedDueToTimeout === true) { + throw new Error(`bounded candidate command timed out: ${[command, ...argv].join(' ')}`); + } + const cwdStat = statSync(realpathSync(input.cwd)); + return { + executable: realpathSync(command), + executableSha256: sha256File(realpathSync(command)), + candidateBinary: realpathSync(input.binary), + candidateBinarySha256: sha256File(realpathSync(input.binary)), + argv, + pid: result.pid, + requestedCwd: realpathSync(input.cwd), + cwdIdentity: `${cwdStat.dev}:${cwdStat.ino}`, + exit: result.exitCode, + stdout: result.stdout.toString(), + stderr: result.stderr.toString(), + }; +} + +function capturePtySetup(input: { + root: string; + binary: string; + cwd: string; + env: Record; + executionAdapter?: string; +}): CapturedCommand { + const command = input.executionAdapter ?? input.binary; + const commandArgs = + input.executionAdapter === undefined + ? [input.binary, 'setup', '--codex'] + : [input.executionAdapter, input.binary, 'setup', '--codex']; + const env = { + ...input.env, + CI: '', + CODEX_THREAD_ID: '', + ...(input.executionAdapter === undefined ? {} : { DOGFOOD_ROOT: input.root }), + }; + let result: ReturnType; + if (process.platform === 'darwin') { + const expect = Bun.which('expect'); + if (expect === null) throw new Error('real-PTY dogfood requires expect(1) on macOS'); + const spawnWords = commandArgs.map((part) => `{${part}}`).join(' '); + const script = [ + 'set timeout 120', + `spawn -noecho ${spawnWords}`, + 'send -- "yes\\r"', + 'expect eof', + 'catch wait result', + 'exit [lindex $result 3]', + ].join('\n'); + result = Bun.spawnSync([expect, '-c', script], { + cwd: input.cwd, + env, + stdout: 'pipe', + stderr: 'pipe', + timeout: 150_000, + }); + } else if (process.platform === 'linux') { + const script = Bun.which('script'); + if (script === null) throw new Error('real-PTY dogfood requires script(1) on Linux'); + const commandLine = commandArgs.map((part) => `'${part.replaceAll("'", `'\\''`)}'`).join(' '); + result = Bun.spawnSync([script, '-qec', commandLine, '/dev/null'], { + cwd: input.cwd, + env, + stdin: Buffer.from('yes\n'), + stdout: 'pipe', + stderr: 'pipe', + timeout: 150_000, + }); + } else { + throw new Error(`real-PTY dogfood is unsupported on ${process.platform}`); + } + if (result.exitedDueToTimeout === true) throw new Error('real-PTY candidate setup timed out'); + const cwdStat = statSync(realpathSync(input.cwd)); + return { + executable: realpathSync(command), + executableSha256: sha256File(realpathSync(command)), + candidateBinary: realpathSync(input.binary), + candidateBinarySha256: sha256File(realpathSync(input.binary)), + argv: commandArgs.slice(1), + pid: result.pid, + requestedCwd: realpathSync(input.cwd), + cwdIdentity: `${cwdStat.dev}:${cwdStat.ino}`, + exit: result.exitCode, + stdout: result.stdout.toString(), + stderr: result.stderr.toString(), + }; +} + +function requireExit(observation: CapturedCommand, expected: number, label: string): CapturedCommand { + if (observation.exit !== expected) { + throw new Error( + `${label} exited ${observation.exit}, expected ${expected}: ${observation.stderr.trim() || observation.stdout.trim()}`, + ); + } + return observation; +} + +function parseResultTrailer(output: string): Record { + for (const line of output.split(/\r?\n/).reverse()) { + const trimmed = line.trim(); + const objectStart = trimmed.indexOf('{'); + if (objectStart < 0) continue; + try { + const parsed = JSON.parse(trimmed.slice(objectStart)) as Record; + if ( + parsed.schemaVersion === 1 && + typeof parsed.code === 'string' && + typeof parsed.deliveryComplete === 'boolean' && + typeof parsed.retry === 'boolean' && + typeof parsed.nextAction === 'string' + ) { + return parsed; + } + } catch { + // Keep scanning bounded command output for the canonical trailer line. + } + } + throw new Error(`candidate command did not emit a schema-valid delivery trailer: ${output.trim().slice(-2_000)}`); +} + +interface LifecycleResult { + genieHome: string; + delivery: Record; + doctor: Record; + stages: StageProjection[]; + repositories: { cacheRoot: string; a: RepoObservation; b: Record }; + convergence: { + route: { state: 'managed-project'; command: string; cwdOverride: null }; + roles: { expectedCount: number; observedCount: number; current: true; reviewerSha256: string }; + }; +} + +function initRepo(path: string, env: Record): string { + mkdirSync(path, { recursive: true }); + execFileSync('git', ['init', '-q', '-b', 'main'], { cwd: path, env }); + execFileSync('git', ['config', 'user.email', 'dogfood@test.invalid'], { cwd: path, env }); + execFileSync('git', ['config', 'user.name', 'Dogfood'], { cwd: path, env }); + execFileSync('git', ['commit', '--allow-empty', '-q', '-m', 'seed'], { cwd: path, env }); + return realpathSync(path); +} + +function installGeneration(generation: VerifiedGeneration, genieHome: string): string { + const binary = join(genieHome, 'bin', 'genie'); + const payload = join(genieHome, 'plugins', 'genie'); + mkdirSync(dirname(binary), { recursive: true }); + rmSync(payload, { recursive: true, force: true }); + copyFileSync(generation.binaryPath, binary); + chmodSync(binary, 0o755); + cpSync(generation.payloadPath, payload, { recursive: true }); + writeFileSync(join(genieHome, 'VERSION'), `${generation.facts.version}\n`); + if (sha256File(binary) !== generation.binarySha256) throw new Error('installed generation binary digest drifted'); + const tree = scanPhysicalTree(payload); + if (tree.status !== 'ok' || tree.digest !== generation.payloadSha256) { + throw new Error('installed generation payload digest drifted'); + } + return realpathSync(binary); +} + +function installFakeCodex( + root: string, + previous: VerifiedGeneration, + candidate: VerifiedGeneration, + env: Record, +): { command: string; stateDir: string } { + const stateDir = join(root, 'codex-state'); + const command = join(root, 'bin', 'codex'); + mkdirSync(stateDir, { recursive: true }); + writeFileSync(join(stateDir, 'registered'), `${previous.facts.version}\n`); + writeFileSync( + command, + `#!/bin/bash +set -euo pipefail +cmd="$*" +if [ "\${1:-}" = "--version" ]; then echo "codex 0.0.0-dogfood"; exit 0; fi +if [ "$cmd" = "plugin list --json" ]; then + version=$(tr -d '\\n' < "$FAKE_CODEX_STATE/registered") + enabled=true + grep -q 'enabled = false' "$CODEX_HOME/config.toml" 2>/dev/null && enabled=false + printf '{"installed":[{"pluginId":"genie@automagik","enabled":%s,"version":"%s"}]}\\n' "$enabled" "$version" + exit 0 +fi +if [ "$cmd" = "plugin add genie@automagik --json" ]; then + target="$CODEX_HOME/plugins/cache/automagik/genie/$FAKE_CODEX_TARGET" + rm -rf "$target" + mkdir -p "$target" + cp -R "$GENIE_HOME/plugins/genie/." "$target/" + printf '%s\\n' "$FAKE_CODEX_TARGET" > "$FAKE_CODEX_STATE/registered" + echo '{}' + exit 0 +fi +if [[ "$cmd" == plugin\\ marketplace* ]]; then echo '{}'; exit 0; fi +echo '{}' +`, + ); + chmodSync(command, 0o755); + env.FAKE_CODEX_STATE = stateDir; + env.FAKE_CODEX_TARGET = candidate.facts.version; + return { command: realpathSync(command), stateDir }; +} + +function readRegisteredVersion(stateDir: string): string { + return readFileSync(join(stateDir, 'registered'), 'utf8').trim(); +} + +function seedSentinelWithCli(input: { + root: string; + binary: string; + executionAdapter?: string; + repo: string; + env: Record; + label: string; + initialize: boolean; +}): { expected: TaskIdentity & { token: string }; commands: CapturedCommand[] } { + const commands: CapturedCommand[] = []; + if (input.initialize) { + commands.push( + requireExit( + captureCommand({ + root: input.root, + binary: input.binary, + args: ['init'], + cwd: input.repo, + env: input.env, + executionAdapter: input.executionAdapter, + }), + 0, + `${input.label} init`, + ), + ); + } + const token = randomBytes(20).toString('hex'); + const wish = `dogfood-${input.label}-${token}`; + const title = `task-${input.label}-${token}`; + const worker = `dogfood-${input.label}-${token.slice(0, 12)}`; + const created = requireExit( + captureCommand({ + root: input.root, + binary: input.binary, + args: ['task', 'create', '--title', title, '--wish', wish], + cwd: input.repo, + env: input.env, + executionAdapter: input.executionAdapter, + }), + 0, + `${input.label} task create`, + ); + commands.push(created); + const taskId = created.stdout.match(/Created task (t_[a-zA-Z0-9]+)/)?.[1]; + if (taskId === undefined) throw new Error(`${input.label} task create did not report its task id`); + commands.push( + requireExit( + captureCommand({ + root: input.root, + binary: input.binary, + args: ['task', 'checkout', taskId, '--worker', worker], + cwd: input.repo, + env: input.env, + executionAdapter: input.executionAdapter, + }), + 0, + `${input.label} task checkout`, + ), + ); + const listed = requireExit( + captureCommand({ + root: input.root, + binary: input.binary, + args: ['task', 'list', '--json'], + cwd: input.repo, + env: input.env, + executionAdapter: input.executionAdapter, + }), + 0, + `${input.label} task list`, + ); + commands.push(listed); + const rows = JSON.parse(listed.stdout) as Array>; + const row = rows.find((value) => value.id === taskId); + if (row?.title !== title || row.wish !== wish || row.status !== 'in_progress' || row.claimedBy !== worker) { + throw new Error(`${input.label} seeded task did not round-trip through the exact binary`); + } + return { + expected: { token, wish, taskId, title, status: 'in_progress', claimedBy: worker }, + commands, + }; +} + +function stage( + id: StageProjection['id'], + projection: Omit, + commands: CapturedCommand[], +): StageProjection { + if (commands.length === 0) throw new Error(`${id} has no captured command observation`); + const observation = { schemaVersion: 1 as const, commands }; + return { + id, + command: commands.map((command) => [command.executable, ...command.argv].join(' ')).join(' && '), + ...projection, + observationPath: `observations/${id}.json`, + observationSha256: sha256Bytes(Buffer.from(serializeStageObservation(observation))), + observation, + }; +} + +function serializeStageObservation(observation: StageProjection['observation']): string { + return `${JSON.stringify(observation, null, 2)}\n`; +} + +function parseBoardFromMcp(stdout: string): BoardResult { + const response = stdout + .split('\n') + .filter(Boolean) + .map((line) => JSON.parse(line) as Record) + .find((message) => message.id === 2); + const result = response?.result as { isError?: boolean; content?: Array<{ text?: string }> } | undefined; + if (result === undefined) throw new Error(`MCP board returned no response: ${stdout}`); + return { + pid: 0, + isError: result.isError === true, + payload: JSON.parse(result.content?.[0]?.text ?? '{}') as Record, + }; +} + +function directMcpEvidence(input: NativeMcpEvidenceInput): NativeMcpEvidence { + const requests = [ + { + jsonrpc: '2.0', + id: 1, + method: 'initialize', + params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'dogfood', version: '2' } }, + }, + { jsonrpc: '2.0', method: 'notifications/initialized' }, + { jsonrpc: '2.0', id: 2, method: 'tools/call', params: { name: 'genie_board', arguments: {} } }, + ]; + const command = captureCommand({ + root: input.root, + binary: input.candidateBinary, + args: ['mcp'], + cwd: input.requestedCwd, + env: input.env, + executionAdapter: input.executionAdapter, + stdin: Buffer.from(`${requests.map((request) => JSON.stringify(request)).join('\n')}\n`), + }); + requireExit(command, 0, `${input.tag} direct MCP`); + const board = parseBoardFromMcp(command.stdout); + const cwd = realpathSync(input.requestedCwd); + return { + requestedCwd: input.requestedCwd, + effectiveCwd: cwd, + cwdIdentity: command.cwdIdentity, + controlCwd: cwd, + controlCwdIdentity: command.cwdIdentity, + childPid: command.pid, + threadId: `local-${input.tag}-${command.pid}`, + isError: board.isError, + payload: board.payload, + raw: { + schemaVersion: 1, + kind: 'verified-local-fixture-direct-mcp', + command, + payload: board.payload, + }, + }; +} + +async function observeMcp( + input: NativeMcpEvidenceInput, + dependencies: DogfoodHarnessDependencies, + evidenceKind: DogfoodEntryInput['evidenceKind'], +): Promise { + if (dependencies.observeNativeMcp !== undefined) return dependencies.observeNativeMcp(input); + if (evidenceKind === 'verified-local-fixture') return directMcpEvidence(input); + const native = await import('./codex-native-mcp-evidence.js'); + return native.captureCodexNativeMcpEvidenceForDogfood(input); +} + +function observedRepo( + repo: string, + expected: TaskIdentity & { token: string }, + board: NativeMcpEvidence, +): RepoObservation { + if (board.isError) throw new Error(`seeded board failed: ${JSON.stringify(board.payload)}`); + const tasks = board.payload.tasks; + if (!Array.isArray(tasks) || tasks.length !== 1) throw new Error('seeded board must return exactly one task'); + const task = tasks[0] as Record; + const observed: TaskIdentity = { + wish: String(task.wish), + taskId: String(task.id), + title: String(task.title), + status: task.status as 'in_progress', + claimedBy: String(task.claimedBy), + }; + const expectedIdentity: TaskIdentity = { + wish: expected.wish, + taskId: expected.taskId, + title: expected.title, + status: expected.status, + claimedBy: expected.claimedBy, + }; + if (JSON.stringify(observed) !== JSON.stringify(expectedIdentity)) throw new Error('seeded task identity mismatch'); + if ( + board.effectiveCwd !== board.controlCwd || + board.cwdIdentity !== board.controlCwdIdentity || + board.effectiveCwd !== realpathSync(repo) + ) { + throw new Error('Codex-launched candidate MCP CWD differs from its control process'); + } + return { + root: repo, + requestedCwd: board.requestedCwd, + effectiveCwd: board.effectiveCwd, + cwdIdentity: board.cwdIdentity, + childPid: board.childPid, + sentinel: { token: expected.token, expected: expectedIdentity, observed, boardCount: 1 }, + }; +} + +function nativeObservationCommand( + evidence: NativeMcpEvidence, + candidateBinary: string, + candidateBinarySha256: string, + executionAdapter?: string, +): CapturedCommand { + const executable = executionAdapter ?? candidateBinary; + return { + executable, + executableSha256: executionAdapter === undefined ? candidateBinarySha256 : sha256File(executionAdapter), + candidateBinary, + candidateBinarySha256, + argv: ['mcpServer/tool/call', 'genie', 'genie_board', evidence.threadId], + pid: evidence.childPid, + requestedCwd: evidence.requestedCwd, + cwdIdentity: evidence.cwdIdentity, + exit: evidence.isError ? 1 : 0, + stdout: JSON.stringify(evidence.raw), + stderr: '', + }; +} + +function copyRuntimeInput(root: string, label: string, path: string): string { + const directory = join(root, 'runtime-inputs', label); + mkdirSync(directory, { recursive: true }); + const target = join(directory, basename(path)); + copyFileSync(path, target); + if (sha256File(target) !== sha256File(path)) throw new Error(`runtime ${label} input copy digest drifted`); + return realpathSync(target); +} + +function parseDoctor(observation: CapturedCommand): Record { + const doctor = JSON.parse(observation.stdout) as Record; + const summary = doctor.integrationSummary as { codexPlugin?: { state?: unknown; deliveryComplete?: unknown } }; + if ( + doctor.ok !== true || + summary?.codexPlugin?.state !== 'current' || + summary.codexPlugin.deliveryComplete !== true + ) { + throw new Error(`candidate doctor did not report current: ${observation.stdout.slice(0, 500)}`); + } + return doctor; +} + +function observeAssetConvergence(input: { + root: string; + candidate: VerifiedGeneration; + candidateBinary: string; + fakeCodex: string; + repo: string; + codexHome: string; + env: Record; + executionAdapter?: string; +}): { commands: CapturedCommand[]; convergence: LifecycleResult['convergence'] } { + const plugin = requireExit( + captureCommand({ + root: input.root, + binary: input.fakeCodex, + args: ['plugin', 'list', '--json'], + cwd: input.repo, + env: input.env, + }), + 0, + 'candidate Codex registration', + ); + const version = requireExit( + captureCommand({ + root: input.root, + binary: input.candidateBinary, + args: ['--version'], + cwd: input.repo, + env: input.env, + executionAdapter: input.executionAdapter, + }), + 0, + 'candidate binary version', + ); + if (!version.stdout.includes(input.candidate.facts.version)) { + throw new Error('candidate binary did not report the authenticated T generation'); + } + const routePath = join(input.repo, '.codex', 'config.toml'); + const routeBytes = readFileSync(routePath, 'utf8'); + const routeCommand = routeBytes.match(/mcp_servers\.genie\.command\s*=\s*"([^"]+)"/)?.[1]; + if ( + routeCommand === undefined || + realpathSync(routeCommand) !== input.candidateBinary || + !routeBytes.includes('args = ["mcp"]') || + /\.cwd\s*=/.test(routeBytes) + ) { + throw new Error(`candidate project route did not converge to the exact stable facade: ${routeBytes}`); + } + const sourceRoles = join(input.candidate.releaseRoot, 'plugins', 'genie', 'codex-agents'); + const targetRoles = join(input.codexHome, 'agents'); + const roleNames = readdirSync(sourceRoles).filter((name) => name.endsWith('.toml')); + const installedRoleNames = readdirSync(targetRoles).filter((name) => name.endsWith('.toml')); + const reviewerSha256 = sha256File(join(sourceRoles, 'genie-reviewer.toml')); + if ( + roleNames.length === 0 || + installedRoleNames.length !== roleNames.length || + sha256File(join(targetRoles, 'genie-reviewer.toml')) !== reviewerSha256 + ) { + throw new Error('candidate role-agent convergence did not match the authenticated payload'); + } + return { + commands: [plugin, version], + convergence: { + route: { state: 'managed-project', command: routeCommand, cwdOverride: null }, + roles: { + expectedCount: roleNames.length, + observedCount: installedRoleNames.length, + current: true, + reviewerSha256, + }, + }, + }; +} + +async function runLifecycle( + root: string, + input: DogfoodEntryInput, + previous: VerifiedGeneration, + candidate: VerifiedGeneration, + dependencies: DogfoodHarnessDependencies, +): Promise { + const genieHome = join(root, 'genie-home'); + const codexHome = join(root, 'codex-home'); + const reposRoot = join(root, 'repos'); + const env = isolatedDogfoodEnv(root, { + GENIE_HOME: genieHome, + CODEX_HOME: codexHome, + GENIE_RELEASE_DOGFOOD: '1', + TERM: 'xterm', + }); + const repoA = initRepo(join(reposRoot, 'a'), env); + const repoB = initRepo(join(reposRoot, 'b'), env); + mkdirSync(codexHome, { recursive: true }); + const { command: fakeCodex, stateDir } = installFakeCodex(root, previous, candidate, env); + const configPath = join(codexHome, 'config.toml'); + const trustedProjects = [...new Set([repoA, realpathSync(repoA), repoB, realpathSync(repoB)])]; + writeFileSync( + configPath, + `[plugins."genie@automagik"]\nenabled = true\n${trustedProjects + .map((path) => `[projects."${path}"]\ntrust_level = "trusted"\n`) + .join('')}`, + ); + + const previousBinary = installGeneration(previous, genieHome); + const previousCache = join(codexHome, 'plugins', 'cache', 'automagik', 'genie', previous.facts.version); + cpSync(previous.payloadPath, previousCache, { recursive: true }); + const seededA = seedSentinelWithCli({ + root, + binary: previousBinary, + executionAdapter: input.executionAdapter, + repo: repoA, + env, + label: 'a', + initialize: true, + }); + const stages: StageProjection[] = [ + stage( + 'seed-repositories', + { + exit: 0, + humanState: 'seeded', + jsonState: 'seeded', + activeVersion: previous.facts.version, + trailer: null, + }, + seededA.commands, + ), + ]; + const nVersion = requireExit( + captureCommand({ + root, + binary: previousBinary, + args: ['--version'], + cwd: repoA, + env, + executionAdapter: input.executionAdapter, + }), + 0, + 'previous binary version', + ); + const nPlugin = requireExit( + captureCommand({ root, binary: fakeCodex, args: ['plugin', 'list', '--json'], cwd: repoA, env }), + 0, + 'previous Codex registration', + ); + if (!nVersion.stdout.includes(previous.facts.version) || !nPlugin.stdout.includes(previous.facts.version)) { + throw new Error('verified previous generation N was not the active parent'); + } + stages.push( + stage( + 'n-parent-active', + { + exit: 0, + humanState: 'current', + jsonState: 'current', + activeVersion: previous.facts.version, + trailer: null, + }, + [nVersion, nPlugin], + ), + ); + + const candidateBinary = installGeneration(candidate, genieHome); + const request = { + schemaVersion: 1, + platformId: input.platformId, + artifact: copyRuntimeInput(root, 'candidate-artifact', candidate.paths.artifact), + manifest: copyRuntimeInput(root, 'candidate-manifest', candidate.paths.manifest), + descriptor: copyRuntimeInput(root, 'candidate-descriptor', candidate.paths.identity), + bundle: copyRuntimeInput(root, 'candidate-bundle', candidate.paths.bundle), + }; + const repaired = requireExit( + captureCommand({ + root, + binary: candidateBinary, + args: ['update', '--publish-local-delivery', JSON.stringify(request)], + cwd: repoA, + env, + executionAdapter: input.executionAdapter, + timeoutMs: 180_000, + }), + 2, + 'candidate local delivery publication', + ); + const repairTrailer = parseResultTrailer(`${repaired.stdout}\n${repaired.stderr}`); + if ( + repairTrailer.code !== 'activation-pending' || + repairTrailer.deliveryComplete !== true || + readRegisteredVersion(stateDir) !== previous.facts.version + ) { + throw new Error('candidate repair did not preserve N until explicit activation consent'); + } + stages.push( + stage( + 't-delivery-repair', + { + exit: 2, + humanState: 'activation-pending', + jsonState: 'activation-pending', + activeVersion: previous.facts.version, + trailer: repairTrailer, + }, + [repaired], + ), + ); + + const activated = requireExit( + capturePtySetup({ + root, + binary: candidateBinary, + cwd: repoA, + env, + executionAdapter: input.executionAdapter, + }), + 0, + 'candidate real-PTY setup', + ); + if ( + !/Activated Codex plugin/.test(`${activated.stdout}\n${activated.stderr}`) || + readRegisteredVersion(stateDir) !== candidate.facts.version + ) { + throw new Error('candidate setup did not activate the exact T generation'); + } + stages.push( + stage( + 'activation-consent', + { + exit: 0, + humanState: 'activated', + jsonState: 'current', + activeVersion: candidate.facts.version, + trailer: null, + }, + [activated], + ), + ); + + const converged = observeAssetConvergence({ + root, + candidate, + candidateBinary, + fakeCodex, + repo: repoA, + codexHome, + env, + executionAdapter: input.executionAdapter, + }); + stages.push( + stage( + 'assets-converged', + { + exit: 0, + humanState: 'current', + jsonState: 'current', + activeVersion: candidate.facts.version, + trailer: null, + }, + converged.commands, + ), + ); + + const nativeInput = (tag: string, requestedCwd: string): NativeMcpEvidenceInput => ({ + tag, + requestedCwd, + candidateBinary, + candidateBinarySha256: candidate.binarySha256, + executionAdapter: input.executionAdapter, + root, + env, + }); + const bBefore = await observeMcp(nativeInput('b-before-init', repoB), dependencies, input.evidenceKind); + if (!bBefore.isError || bBefore.payload.error !== 'project-database-unavailable') { + throw new Error('untouched B did not fail closed through real Codex before init'); + } + if ('tasks' in bBefore.payload || 'counts' in bBefore.payload) throw new Error('untouched B returned an empty board'); + stages.push( + stage( + 'untouched-b-before-init', + { + exit: 1, + humanState: 'project-database-unavailable', + jsonState: 'project-database-unavailable', + activeVersion: candidate.facts.version, + trailer: null, + }, + [nativeObservationCommand(bBefore, candidateBinary, candidate.binarySha256, input.executionAdapter)], + ), + ); + + const seededB = seedSentinelWithCli({ + root, + binary: candidateBinary, + executionAdapter: input.executionAdapter, + repo: repoB, + env, + label: 'b', + initialize: true, + }); + const bAfterEvidence = await observeMcp(nativeInput('b-after-init', repoB), dependencies, input.evidenceKind); + const bAfter = { ...observedRepo(repoB, seededB.expected, bAfterEvidence), routeState: 'managed-project' as const }; + stages.push( + stage( + 'untouched-b-after-init', + { + exit: 0, + humanState: 'current', + jsonState: 'current', + activeVersion: candidate.facts.version, + trailer: null, + }, + [ + ...seededB.commands, + nativeObservationCommand(bAfterEvidence, candidateBinary, candidate.binarySha256, input.executionAdapter), + ], + ), + ); + + const aEvidence = await observeMcp(nativeInput('a-new-thread', repoA), dependencies, input.evidenceKind); + const a = observedRepo(repoA, seededA.expected, aEvidence); + if (a.sentinel.token === bAfter.sentinel.token) throw new Error('two-repo sentinels collided'); + if (JSON.stringify(a.sentinel.observed).includes(bAfter.sentinel.token)) throw new Error('B sentinel leaked into A'); + if (JSON.stringify(bAfter.sentinel.observed).includes(a.sentinel.token)) throw new Error('A sentinel leaked into B'); + stages.push( + stage( + 'new-thread-sentinel', + { + exit: 0, + humanState: 'current', + jsonState: 'current', + activeVersion: candidate.facts.version, + trailer: null, + }, + [nativeObservationCommand(aEvidence, candidateBinary, candidate.binarySha256, input.executionAdapter)], + ), + ); + + const doctorObservation = requireExit( + captureCommand({ + root, + binary: candidateBinary, + args: ['doctor', '--json'], + cwd: repoA, + env, + executionAdapter: input.executionAdapter, + }), + 0, + 'candidate doctor', + ); + const doctor = parseDoctor(doctorObservation); + stages.push( + stage( + 'doctor-current', + { + exit: 0, + humanState: 'current', + jsonState: 'current', + activeVersion: candidate.facts.version, + trailer: null, + }, + [doctorObservation], + ), + ); + + const delivery = JSON.parse(readFileSync(join(genieHome, '.codex-plugin-delivery-record.json'), 'utf8')) as Record< + string, + unknown + >; + return { + genieHome, + delivery, + doctor, + stages, + repositories: { + cacheRoot: join(codexHome, 'plugins', 'cache', 'automagik', 'genie'), + a, + b: { + root: repoB, + beforeInit: { + routeState: 'absent', + fallbackUsed: false, + result: 'project-database-unavailable', + returnedTasks: 0, + }, + afterInit: bAfter, + }, + }, + convergence: converged.convergence, + }; +} + +function buildManifest( + input: DogfoodEntryInput, + previous: VerifiedGeneration, + candidate: VerifiedGeneration, + lifecycle: LifecycleResult, + repositories: Record, +): Record { + const artifactEvidence = (generation: VerifiedGeneration) => ({ + version: generation.facts.version, + channel: generation.facts.channel, + platformId: generation.facts.platformId, + platformTriple: generation.facts.platformTriple, + releaseTag: generation.facts.releaseTag, + releaseName: generation.facts.releaseName, + manifestSha256: generation.manifestSha256, + artifactSha256: generation.artifactSha256, + binarySha256: generation.binarySha256, + payloadSha256: generation.payloadSha256, + evidenceDigest: generation.evidenceDigest, + provenance: { + kind: generation.evidence === null ? 'release-tarball' : 'delivery-evidence', + repository: DELIVERY_EVIDENCE_REPOSITORY, + predicateType: + generation.evidence === null ? 'https://slsa.dev/provenance/v0.2' : DELIVERY_EVIDENCE_PREDICATE_TYPE, + workflowIdentity: + generation.evidence === null + ? 'https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@refs/tags/v2.1.0' + : DELIVERY_EVIDENCE_WORKFLOW_IDENTITY, + oidcIssuer: DELIVERY_EVIDENCE_OIDC_ISSUER, + sourceCommit: generation.facts.sourceSha, + controlCommit: generation.facts.controlSha, + sourceBranch: generation.facts.sourceBranch, + sourceCiRunId: generation.facts.sourceCiRunId, + identitySha256: generation.identitySha256, + bundleSha256: generation.bundleSha256, + }, + }); + const logicalPaths = (label: 'previous' | 'candidate', paths: GenerationInputPaths) => ({ + artifact: `${label}/${basename(paths.artifact)}`, + manifest: `${label}/${basename(paths.manifest)}`, + identity: `${label}/${basename(paths.identity)}`, + bundle: `${label}/${basename(paths.bundle)}`, + identityKind: paths.identityKind, + }); + return { + kind: 'live-dogfood-evidence', + schemaVersion: LIVE_DOGFOOD_SCHEMA_VERSION, + entry: { + id: `${candidate.facts.version}-${input.platformId}`, + evidenceKind: input.evidenceKind, + availability: 'verified', + platformId: input.platformId, + platformTriple: candidate.facts.platformTriple, + artifactName: candidate.facts.releaseName, + inputs: { + previous: logicalPaths('previous', previous.paths), + candidate: logicalPaths('candidate', candidate.paths), + }, + }, + lifecycle: { + previousVersion: previous.facts.version, + candidateVersion: candidate.facts.version, + channel: candidate.facts.channel, + sourceCommit: candidate.facts.sourceSha, + artifacts: { + previous: artifactEvidence(previous), + candidate: artifactEvidence(candidate), + }, + delivery: { + schemaVersion: lifecycle.delivery.schemaVersion, + deliveryId: lifecycle.delivery.deliveryId, + evidenceDigest: lifecycle.delivery.evidenceDigest, + root: lifecycle.delivery.deliveryRoot, + targetVersion: lifecycle.delivery.targetVersion, + platformId: lifecycle.delivery.platformId, + platformTriple: lifecycle.delivery.platformTriple, + releaseTag: lifecycle.delivery.releaseTag, + releaseName: lifecycle.delivery.releaseName, + releaseManifestSha256: lifecycle.delivery.releaseManifestSha256, + artifactSha256: lifecycle.delivery.artifactSha256, + installedBinarySha256: lifecycle.delivery.installedBinarySha256, + canonicalPayloadSha256: lifecycle.delivery.canonicalPayloadSha256, + }, + convergence: lifecycle.convergence, + stages: lifecycle.stages, + }, + repositories, + }; +} + +function renderEvidence(manifest: Record, doctor: Record): string { + return [ + '# Codex dogfood matrix evidence', + '', + '```json', + JSON.stringify(manifest, null, 2), + '```', + '', + '## Production doctor JSON', + '', + '```json', + JSON.stringify(doctor, null, 2), + '```', + '', + ].join('\n'); +} + +function sha256File(path: string): string { + return createHash('sha256').update(readFileSync(path)).digest('hex'); +} + +function sha256Bytes(bytes: Uint8Array): string { + return createHash('sha256').update(bytes).digest('hex'); +} diff --git a/tests/support/codex-lifecycle-test-runner.ts b/tests/support/codex-lifecycle-test-runner.ts new file mode 100644 index 000000000..db0bae5b4 --- /dev/null +++ b/tests/support/codex-lifecycle-test-runner.ts @@ -0,0 +1,159 @@ +/** + * Test-only command driver for the real setup/doctor implementations. + * + * The lifecycle PTY fixture persists a deterministic, unsigned Sigstore bundle. + * Production intentionally has no environment or CLI verification bypass, so + * this unshipped runner supplies the same-process cryptographic seam while all + * descriptor, manifest, candidate, store, setup, and doctor bindings remain + * live. + */ + +import { createHash } from 'node:crypto'; +import { readFileSync, realpathSync, rmSync } from 'node:fs'; +import { join } from 'node:path'; +import { doctorCommand } from '../../src/genie-commands/doctor.js'; +import { + assertLocalDeliveryRepairEnabled, + materializeLocalDeliveryRepair, +} from '../../src/genie-commands/local-delivery-repair.js'; +import { setupCommand } from '../../src/genie-commands/setup.js'; +import { + attemptAlreadyCurrentDeliveryRepair, + createPrivateUpdateTempRoot, + projectLocalDeliveryRepairDirective, + resolvePlatformId, +} from '../../src/genie-commands/update.js'; +import { observeCodexActivation, openCodexActivationStore } from '../../src/lib/codex-activation-executor.js'; +import { + type DeliveryEvidenceVerificationDependencies, + verifyDownloadedDeliveryEvidence, +} from '../../src/lib/codex-delivery-evidence.js'; +import { buildTestDeliveryEvidencePack } from '../../src/lib/codex-delivery-evidence.test-support.js'; +import { acquireLifecycleLease } from '../../src/lib/codex-lifecycle-lease.js'; +import { resolveCodexDir, resolveGenieHome } from '../../src/lib/genie-home.js'; +import { VERSION } from '../../src/lib/version.js'; + +const TEST_EVIDENCE_VERIFICATION: DeliveryEvidenceVerificationDependencies = { + verifyBundle: () => ({ integratedTime: '1753228800' }), +}; + +async function publishLocalDeliveryFixture(rawRequest: string): Promise { + assertLocalDeliveryRepairEnabled(process.env.GENIE_RELEASE_DOGFOOD); + const genieHome = resolveGenieHome(); + const platformId = resolvePlatformId(); + const snapshotRoot = createPrivateUpdateTempRoot(); + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + try { + if (!lease.ok) { + const projection = projectLocalDeliveryRepairDirective({ action: 'busy', detail: lease.detail }); + for (const line of projection.stdout) process.stdout.write(`${line}\n`); + for (const line of projection.stderr) process.stderr.write(`${line}\n`); + process.exitCode = projection.exitCode; + return; + } + const local = materializeLocalDeliveryRepair(rawRequest, snapshotRoot, platformId, VERSION); + const directive = await attemptAlreadyCurrentDeliveryRepair( + local.manifest.channel, + local.platformId, + lease, + genieHome, + { + evidenceVerification: TEST_EVIDENCE_VERIFICATION, + fetchManifest: async (channel) => { + if (channel !== local.manifest.channel) throw new Error('fixture channel drifted'); + return local.manifest; + }, + downloadAndVerifyDeliveryAssets: async (manifest, requestedPlatform) => { + if ( + requestedPlatform !== local.platformId || + manifest.manifestSha256 !== local.manifest.manifestSha256 || + manifest.manifestBytes !== local.manifest.manifestBytes + ) { + throw new Error('fixture repair target drifted'); + } + return { + tarballPath: local.artifactPath, + descriptorBytes: local.descriptorBytes, + bundleBytes: local.bundleBytes, + }; + }, + }, + ); + const projection = projectLocalDeliveryRepairDirective(directive); + for (const line of projection.stdout) process.stdout.write(`${line}\n`); + for (const line of projection.stderr) process.stderr.write(`${line}\n`); + process.exitCode = projection.exitCode; + } finally { + if (lease.ok) lease.release(); + rmSync(snapshotRoot, { recursive: true, force: true }); + } +} + +const [command, ...args] = process.argv.slice(2); + +if (command === 'publish-delivery') { + const genieHome = resolveGenieHome(); + const codexHome = resolveCodexDir(); + const codexCommand = Bun.which('codex'); + const snapshot = observeCodexActivation({ + genieHome, + codexHome, + command: codexCommand, + }); + if (snapshot.canonical.status !== 'ok') { + throw new Error(`fixture canonical delivery is unavailable: ${snapshot.canonical.detail}`); + } + const installedBinarySha256 = createHash('sha256') + .update(readFileSync(join(genieHome, 'bin', 'genie'))) + .digest('hex'); + const pack = buildTestDeliveryEvidencePack({ + descriptor: { + version: snapshot.canonical.version.canonical, + releaseTag: `v${snapshot.canonical.version.canonical}`, + artifactSha256: installedBinarySha256, + installedBinarySha256, + canonicalPayloadSha256: snapshot.canonical.digest, + }, + }); + const evidence = verifyDownloadedDeliveryEvidence(pack.input, pack.dependencies); + const lease = acquireLifecycleLease('update-delivery', { genieHome }); + if (!lease.ok) throw new Error(`fixture delivery publication could not acquire the lifecycle lease: ${lease.detail}`); + try { + openCodexActivationStore({ + genieHome, + codexHome, + command: codexCommand, + deliveryEvidenceVerification: pack.dependencies, + }).publishDelivery(lease, { + evidence, + deliveryRoot: realpathSync(genieHome), + }); + } finally { + lease.release(); + } +} else if (command === 'setup' && args.includes('--codex')) { + await setupCommand( + { + codex: true, + quick: args.includes('--quick'), + }, + { deliveryEvidenceVerification: TEST_EVIDENCE_VERIFICATION }, + ); +} else if (command === 'publish-local-delivery' && args.length === 1) { + await publishLocalDeliveryFixture(args[0] as string); +} else if (command === 'doctor') { + await doctorCommand( + { + json: args.includes('--json'), + fix: args.includes('--fix'), + }, + { + deliveryEvidenceVerification: TEST_EVIDENCE_VERIFICATION, + bunVersion: '1.3.10', + bunPath: process.execPath, + }, + ); +} else { + process.stderr.write(`unsupported lifecycle test command: ${[command, ...args].join(' ')}\n`); + process.exitCode = 64; +} diff --git a/tests/support/codex-native-mcp-evidence.test.ts b/tests/support/codex-native-mcp-evidence.test.ts new file mode 100644 index 000000000..c5626f37d --- /dev/null +++ b/tests/support/codex-native-mcp-evidence.test.ts @@ -0,0 +1,288 @@ +import { afterEach, describe, expect, test } from 'bun:test'; +import { spawn } from 'node:child_process'; +import { + chmodSync, + existsSync, + mkdtempSync, + readFileSync, + readdirSync, + realpathSync, + rmSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { + CodexNativeMcpEvidenceError, + type NativeMcpTaskSentinel, + assertUniqueBoardSentinel, + captureCodexNativeMcpEvidence, + probeCodexAppServer, +} from './codex-native-mcp-evidence.js'; + +const LAUNCHER = fileURLToPath(new URL('./codex-native-mcp-launcher.sh', import.meta.url)); +const roots: string[] = []; + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); + +function makeRoot(prefix: string): string { + const root = mkdtempSync(join(tmpdir(), prefix)); + roots.push(root); + return root; +} + +function processExists(pid: number): boolean { + try { + process.kill(pid, 0); + return true; + } catch (error) { + return (error as NodeJS.ErrnoException).code !== 'ESRCH'; + } +} + +async function waitForExit(pid: number, timeoutMs = 2_000): Promise { + const deadline = Date.now() + timeoutMs; + while (processExists(pid) && Date.now() < deadline) { + await Bun.sleep(20); + } +} + +const SENTINEL: NativeMcpTaskSentinel = { + token: 'unique-77ab', + task: { + id: 't_unique-77ab', + title: 'task unique-77ab', + wish: 'wish-unique-77ab', + status: 'ready', + }, +}; + +describe('assertUniqueBoardSentinel', () => { + test('accepts one task with exact identity', () => { + expect( + assertUniqueBoardSentinel( + { + counts: { ready: 1 }, + tasks: [{ ...SENTINEL.task, unrelated: 'preserved' }], + }, + SENTINEL, + ), + ).toEqual(SENTINEL); + }); + + for (const fixture of [ + { name: 'empty', tasks: [] }, + { name: 'duplicate', tasks: [SENTINEL.task, SENTINEL.task] }, + { name: 'cross-repository', tasks: [{ ...SENTINEL.task, id: 't_other' }] }, + { name: 'stale status', tasks: [{ ...SENTINEL.task, status: 'done' }] }, + ]) { + test(`rejects ${fixture.name} task evidence`, () => { + expect(() => assertUniqueBoardSentinel({ tasks: fixture.tasks }, SENTINEL)).toThrow(CodexNativeMcpEvidenceError); + }); + } + + test('rejects a token not bound into the expected exact identity', () => { + expect(() => + assertUniqueBoardSentinel( + { tasks: [{ id: 't_1', title: 'task', wish: 'wish', status: 'ready' }] }, + { token: 'unbound-token', task: { id: 't_1', title: 'task', wish: 'wish', status: 'ready' } }, + ), + ).toThrow('sentinel token must occur'); + }); + + test('rejects a mismatched claimant when claimant identity is expected', () => { + const expected = { ...SENTINEL, task: { ...SENTINEL.task, claimedBy: 'worker-a' } }; + expect(() => assertUniqueBoardSentinel({ tasks: [{ ...expected.task, claimedBy: 'worker-b' }] }, expected)).toThrow( + 'task sentinel differs', + ); + }); +}); + +test('launcher records exact target and preserves its PID through a native adapter exec', async () => { + const root = makeRoot('genie-native-launcher-'); + const repo = join(root, 'repo'); + const record = join(root, 'record.bin'); + const candidatePidFile = join(root, 'candidate.pid'); + const candidate = join(root, 'candidate.mjs'); + Bun.write( + candidate, + `import { writeFileSync } from 'node:fs'; writeFileSync(process.argv.at(-1), String(process.pid));`, + ); + await Bun.write(join(root, '.keep'), ''); + await Bun.$`mkdir -p ${repo}`.quiet(); + + const child = spawn(LAUNCHER, [record, process.execPath, candidate, candidatePidFile], { + cwd: repo, + env: process.env, + stdio: 'ignore', + }); + const exitCode = await new Promise((resolve, reject) => { + child.once('error', reject); + child.once('exit', resolve); + }); + expect(exitCode).toBe(0); + const fields = readFileSync(record).toString('utf8').split('\0').filter(Boolean); + expect(fields[0]).toBe('codex-native-mcp-launcher-v1'); + expect(fields[1]).toBe(String(child.pid)); + expect(fields[2]).toBe(realpathSync(repo)); + expect(fields[3]).toMatch(/^\d+:\d+$/); + expect(fields[4]).toBe(process.execPath); + expect(fields[5]).toBe(candidate); + expect(fields.slice(6)).toEqual([candidatePidFile]); + expect(readFileSync(candidatePidFile, 'utf8')).toBe(String(child.pid)); +}); + +test.skipIf(process.platform === 'win32')( + 'initialize rejection reaps the native app-server process group', + async () => { + const root = makeRoot('genie-native-initialize-cleanup-'); + const codexHome = join(root, 'codex-home'); + const pidRecord = join(root, 'pids.json'); + await Bun.$`mkdir -p ${codexHome}`.quiet(); + const fakeCodex = join(root, 'fake-codex.mjs'); + writeFileSync( + fakeCodex, + `#!${process.execPath} +import { spawn } from 'node:child_process'; +import { writeFileSync } from 'node:fs'; +import { createInterface } from 'node:readline'; +if (process.argv.includes('--version')) { + process.stdout.write('codex-cli initialize-cleanup-test\\n'); + process.exit(0); +} +const descendant = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1 << 30)'], { stdio: 'ignore' }); +writeFileSync(process.env.PID_RECORD, JSON.stringify({ appServer: process.pid, descendant: descendant.pid })); +const rl = createInterface({ input: process.stdin }); +rl.on('line', (line) => { + const message = JSON.parse(line); + if (message.method === 'initialize') { + const error = { code: -32000, message: 'forced native initialize rejection' }; + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error }) + '\\n'); + } +}); +setInterval(() => {}, 1 << 30); +`, + 'utf8', + ); + chmodSync(fakeCodex, 0o755); + + const support = await probeCodexAppServer( + { executable: fakeCodex, version: 'codex-cli initialize-cleanup-test' }, + { ...process.env, CODEX_HOME: codexHome, PID_RECORD: pidRecord }, + { initializeMs: 1_000, cleanupGraceMs: 100 }, + ); + + expect(support.supported).toBe(false); + if (support.supported) throw new Error('fake Codex unexpectedly initialized'); + expect(support.errorCode).toBe('rpc-error'); + expect(support.reason).toContain('forced native initialize rejection'); + const pids = JSON.parse(readFileSync(pidRecord, 'utf8')) as { appServer: number; descendant: number }; + await Promise.all([waitForExit(pids.appServer), waitForExit(pids.descendant)]); + expect(processExists(pids.appServer)).toBe(false); + expect(processExists(pids.descendant)).toBe(false); + }, +); + +test('RPC timeout TERM/KILLs an uncooperative app-server tree and removes the helper temp child', async () => { + const root = makeRoot('genie-native-cleanup-'); + const codexHome = join(root, 'codex-home'); + const repo = join(root, 'repo'); + const helperTemp = join(root, 'helper-temp'); + const appPidFile = join(root, 'app.pid'); + const candidatePidFile = join(root, 'candidate.pid'); + const appSignalFile = join(root, 'app.signals'); + const candidateSignalFile = join(root, 'candidate.signals'); + await Bun.$`mkdir -p ${codexHome} ${repo} ${helperTemp}`.quiet(); + + const fakeCodex = join(root, 'fake-codex.mjs'); + writeFileSync( + fakeCodex, + `#!${process.execPath} +import { appendFileSync, writeFileSync } from 'node:fs'; +import { spawn } from 'node:child_process'; +import { createInterface } from 'node:readline'; +if (process.argv.includes('--version')) { + process.stdout.write('codex-cli cleanup-test\\n'); + process.exit(0); +} +writeFileSync(process.env.APP_PID_FILE, String(process.pid)); +process.on('SIGTERM', () => appendFileSync(process.env.APP_SIGNAL_FILE, 'TERM\\n')); +const rl = createInterface({ input: process.stdin }); +rl.on('line', (line) => { + const message = JSON.parse(line); + if (message.method === 'initialize') { + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: { capabilities: {} } }) + '\\n'); + } else if (message.method === 'thread/start') { + const config = message.params.config.mcp_servers.genie; + spawn(config.command, config.args, { cwd: message.params.cwd, env: process.env, stdio: ['pipe', 'pipe', 'pipe'] }); + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result: { thread: { id: 'thread-cleanup' } } }) + '\\n'); + } else if (message.method === 'mcpServerStatus/list') { + const result = { data: [{ name: 'genie', tools: { genie_board: {} } }], nextCursor: null }; + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, result }) + '\\n'); + } +}); +setInterval(() => {}, 1 << 30); +`, + 'utf8', + ); + chmodSync(fakeCodex, 0o755); + + const candidate = join(root, 'capability-only.mjs'); + writeFileSync( + candidate, + `import { appendFileSync, writeFileSync } from 'node:fs'; +writeFileSync(process.env.CANDIDATE_PID_FILE, String(process.pid)); +process.on('SIGTERM', () => appendFileSync(process.env.CANDIDATE_SIGNAL_FILE, 'TERM\\n')); +setInterval(() => {}, 1 << 30); +`, + 'utf8', + ); + const env = { + ...process.env, + APP_PID_FILE: appPidFile, + APP_SIGNAL_FILE: appSignalFile, + CANDIDATE_PID_FILE: candidatePidFile, + CANDIDATE_SIGNAL_FILE: candidateSignalFile, + CODEX_HOME: codexHome, + }; + + let failure: unknown; + try { + await captureCodexNativeMcpEvidence({ + codex: { executable: fakeCodex, version: 'codex-cli cleanup-test' }, + requestedCwd: repo, + candidate: { executable: candidate, adapter: process.execPath }, + launcherExecutable: LAUNCHER, + controlExecutable: process.execPath, + env, + tempDir: helperTemp, + expectedSentinel: SENTINEL, + timeouts: { + initializeMs: 1_000, + threadStartMs: 1_000, + launcherMs: 1_000, + inventoryMs: 1_000, + toolCallMs: 75, + cleanupGraceMs: 100, + }, + }); + } catch (error) { + failure = error; + } + expect(failure).toBeInstanceOf(CodexNativeMcpEvidenceError); + expect((failure as CodexNativeMcpEvidenceError).code).toBe('rpc-timeout'); + const appPid = Number(readFileSync(appPidFile, 'utf8')); + const candidatePid = Number(readFileSync(candidatePidFile, 'utf8')); + await waitForExit(appPid); + await waitForExit(candidatePid); + expect(processExists(appPid)).toBe(false); + expect(processExists(candidatePid)).toBe(false); + expect(readFileSync(appSignalFile, 'utf8')).toContain('TERM'); + expect(readFileSync(candidateSignalFile, 'utf8')).toContain('TERM'); + expect(readdirSync(helperTemp).filter((entry) => entry.startsWith('genie-native-mcp-evidence-'))).toEqual([]); + expect(existsSync(repo)).toBe(true); + expect(existsSync(codexHome)).toBe(true); +}); diff --git a/tests/support/codex-native-mcp-evidence.ts b/tests/support/codex-native-mcp-evidence.ts new file mode 100644 index 000000000..349adb669 --- /dev/null +++ b/tests/support/codex-native-mcp-evidence.ts @@ -0,0 +1,957 @@ +/** + * Real Codex app-server -> exact candidate `genie mcp` evidence. + * + * This support boundary never starts a model turn or asks for account state. It + * starts one thread with one caller-specified MCP server, calls `genie_board` + * through `mcpServer/tool/call`, and compares the MCP launch cwd with a + * Codex-launched `command/exec` control by exact string and OS directory + * identity. The launcher records its process facts immediately before `exec`, + * so its PID is also the PID of the direct candidate or native adapter. + */ + +import { Database } from 'bun:sqlite'; +import { type SpawnSyncReturns, spawnSync } from 'node:child_process'; +import { createHash } from 'node:crypto'; +import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { isAbsolute, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { resolveProjectContext } from '../../src/lib/v5/genie-db.js'; +import { CodexAppServerTransport } from './codex-app-server-transport.js'; + +export type CodexNativeMcpEvidenceErrorCode = + | 'invalid-options' + | 'codex-unavailable' + | 'codex-version-mismatch' + | 'app-server-failure' + | 'rpc-error' + | 'rpc-timeout' + | 'launcher-timeout' + | 'launcher-mismatch' + | 'mcp-inventory-mismatch' + | 'mcp-tool-failure' + | 'control-failure' + | 'cwd-mismatch' + | 'sentinel-mismatch' + | 'expected-error-mismatch' + | 'cleanup-failure'; + +export class CodexNativeMcpEvidenceError extends Error { + constructor( + readonly code: CodexNativeMcpEvidenceErrorCode, + message: string, + options?: ErrorOptions, + ) { + super(message, options); + this.name = 'CodexNativeMcpEvidenceError'; + } +} + +export interface CodexAppServerPin { + /** Absolute executable path. The exact path is used for both preflight and app-server. */ + executable: string; + /** Exact trimmed stdout expected from ` --version`. */ + version: string; +} + +export interface NativeMcpCandidate { + /** Absolute exact candidate executable or script path. */ + executable: string; + /** Absolute native runtime/adapter, such as Bun, when the candidate is not directly executable. */ + adapter?: string; + /** Candidate arguments. Defaults to `['mcp']`. */ + args?: string[]; +} + +export interface NativeMcpTaskSentinel { + /** Caller-generated token that must occur in at least one exact task identity field. */ + token: string; + task: { + id: string; + title: string; + wish: string; + status: string; + claimedBy?: string; + }; +} + +export type NativeMcpExpectedError = 'project-database-unavailable'; + +export interface CodexNativeMcpEvidenceTimeouts { + preflightMs: number; + initializeMs: number; + threadStartMs: number; + launcherMs: number; + inventoryMs: number; + toolCallMs: number; + controlMs: number; + cleanupGraceMs: number; +} + +interface CodexNativeMcpEvidenceBaseOptions { + codex: CodexAppServerPin; + /** The raw absolute spelling sent as `thread/start.cwd`; it is never normalized here. */ + requestedCwd: string; + candidate: NativeMcpCandidate; + /** Absolute path to `codex-native-mcp-launcher.sh`. */ + launcherExecutable: string; + /** Absolute Node/Bun executable used by the `command/exec` control reporter. */ + controlExecutable: string; + /** + * Exact caller-owned environment for app-server and its children. It is not + * merged with process.env and must contain an absolute CODEX_HOME. + */ + env: NodeJS.ProcessEnv; + /** Parent for this helper's removable temp child. Defaults to env.TMPDIR/process tmpdir. */ + tempDir?: string; + timeouts?: Partial; +} + +interface SentinelEvidenceOptions extends CodexNativeMcpEvidenceBaseOptions { + expectedSentinel: NativeMcpTaskSentinel; + expectedError?: never; +} + +interface ExpectedErrorEvidenceOptions extends CodexNativeMcpEvidenceBaseOptions { + expectedSentinel?: never; + expectedError: NativeMcpExpectedError; +} + +export type CodexNativeMcpEvidenceOptions = SentinelEvidenceOptions | ExpectedErrorEvidenceOptions; + +export interface NativeMcpLauncherObservation { + /** Launcher PID, preserved by `exec` into the candidate or its native adapter. */ + pid: number; + effectiveCwd: string; + /** OS directory identity formatted as `dev:ino`. */ + cwdIdentity: string; + adapter: string | null; + candidate: string; + args: string[]; +} + +export interface NativeMcpControlEvidence { + pid: number; + effectiveCwd: string; + cwdIdentity: string; +} + +export interface NativeMcpToolResponse { + content: unknown[]; + structuredContent?: unknown; + isError?: boolean; + _meta?: unknown; +} + +export type NativeMcpEvidenceOutcome = + | { + kind: 'sentinel'; + sentinel: NativeMcpTaskSentinel; + payload: Record; + } + | { + kind: 'expected-error'; + error: NativeMcpExpectedError; + payload: Record; + }; + +export interface CodexNativeMcpEvidence { + schemaVersion: 1; + codex: CodexAppServerPin & { appServerPid: number }; + candidate: { + executable: string; + adapter: string | null; + args: string[]; + }; + rawRequestedCwd: string; + threadId: string; + launcher: NativeMcpLauncherObservation; + control: NativeMcpControlEvidence; + mcpServer: { + name: string; + tools: string[]; + }; + toolResponse: NativeMcpToolResponse; + toolPayload: Record; + outcome: NativeMcpEvidenceOutcome; +} + +/** Structural adapter input used by the parameterized dogfood harness. */ +export interface DogfoodNativeMcpEvidenceInput { + tag: string; + requestedCwd: string; + candidateBinary: string; + candidateBinarySha256: string; + executionAdapter?: string; + root: string; + env: Record; +} + +/** Narrow evidence projection consumed by the parameterized dogfood harness. */ +export interface DogfoodNativeMcpEvidence { + requestedCwd: string; + effectiveCwd: string; + cwdIdentity: string; + controlCwd: string; + controlCwdIdentity: string; + childPid: number; + threadId: string; + isError: boolean; + payload: Record; + raw: Record; +} + +export type CodexAppServerSupport = + | { supported: true; reason: 'ok' } + | { supported: false; reason: string; errorCode: CodexNativeMcpEvidenceErrorCode }; + +interface McpInventory { + name: string; + tools: string[]; +} + +const DEFAULT_TIMEOUTS: CodexNativeMcpEvidenceTimeouts = { + preflightMs: 5_000, + initializeMs: 15_000, + threadStartMs: 20_000, + launcherMs: 15_000, + inventoryMs: 20_000, + toolCallMs: 20_000, + controlMs: 15_000, + cleanupGraceMs: 1_000, +}; + +// Override the marker-owned project route under its production key. A second +// test-only server name would leave both routes live and make the observation +// ambiguous once setup has converged `.codex/config.toml`. +const MCP_SERVER_NAME = 'genie'; +const LAUNCHER_SCHEMA = 'codex-native-mcp-launcher-v1'; +const CONTROL_SOURCE = + "const fs = require('node:fs'); const cwd = process.cwd(); const s = fs.statSync(cwd); " + + "process.stdout.write(JSON.stringify({ pid: process.pid, cwd, dev: s.dev, ino: s.ino }) + '\\n');"; + +type AppServerSession = CodexAppServerTransport; + +async function launchAppServer( + pin: CodexAppServerPin, + env: NodeJS.ProcessEnv, + timeouts: CodexNativeMcpEvidenceTimeouts, +): Promise { + assertCodexPin(pin, env, timeouts.preflightMs); + return CodexAppServerTransport.launch({ + executable: pin.executable, + args: ['app-server', '--stdio'], + env, + windowsHide: true, + initializeParams: { clientInfo: { name: 'genie-native-mcp-evidence', version: '1' } }, + initializeTimeoutMs: timeouts.initializeMs, + cleanupGraceMs: timeouts.cleanupGraceMs, + initialSignal: 'SIGTERM', + label: 'native MCP', + error: (kind, message, cause) => { + const code = { + process: 'app-server-failure', + rpc: 'rpc-error', + timeout: 'rpc-timeout', + cleanup: 'cleanup-failure', + }[kind] as CodexNativeMcpEvidenceErrorCode; + return new CodexNativeMcpEvidenceError(code, message, cause ? { cause } : undefined); + }, + }); +} + +/** + * Run the no-model app-server initialize handshake against an exact executable + * and version pin. The caller decides whether an unavailable verdict permits a + * test skip; evidence failures after this probe must not be converted to skips. + */ +export async function probeCodexAppServer( + pin: CodexAppServerPin, + env: NodeJS.ProcessEnv, + timeoutOverrides: Partial = {}, +): Promise { + const timeouts = mergeTimeouts(timeoutOverrides); + let session: AppServerSession | null = null; + try { + assertEnvironment(env); + session = await launchAppServer(pin, env, timeouts); + } catch (error) { + const typed = toEvidenceError(error, 'codex-unavailable', 'codex app-server is unavailable'); + return { supported: false, reason: typed.message, errorCode: typed.code }; + } + const cleanupError = await session.close(); + if (cleanupError) return { supported: false, reason: cleanupError.message, errorCode: cleanupError.code }; + return { supported: true, reason: 'ok' }; +} + +/** + * Capture and validate one real app-server -> candidate MCP observation. + * + * Every failure path closes the candidate/app-server process tree and removes + * only this call's private temporary directory before rejecting. + */ +export async function captureCodexNativeMcpEvidence( + options: CodexNativeMcpEvidenceOptions, +): Promise { + const normalized = normalizeOptions(options); + const tempRoot = mkdtempSync(join(normalized.tempDir, 'genie-native-mcp-evidence-')); + const launcherRecord = join(tempRoot, 'launcher-record.bin'); + let session: AppServerSession | null = null; + let launcher: NativeMcpLauncherObservation | null = null; + let evidence: CodexNativeMcpEvidence | null = null; + let primaryError: Error | null = null; + + try { + session = await launchAppServer(normalized.codex, normalized.env, normalized.timeouts); + const threadId = await startThread(session, normalized, launcherRecord); + launcher = await waitForLauncherObservation(launcherRecord, normalized.candidate, normalized.timeouts.launcherMs); + const inventory = await waitForMcpInventory(session, threadId, normalized.timeouts.inventoryMs); + const toolResponse = await callBoard(session, threadId, normalized.timeouts.toolCallMs); + const toolPayload = parseToolPayload(toolResponse); + const control = await runControl(session, normalized, normalized.timeouts.controlMs); + assertCwdEquality(launcher, control); + const outcome = validateExpectedOutcome(normalized, toolResponse, toolPayload); + evidence = { + schemaVersion: 1, + codex: { ...normalized.codex, appServerPid: session.pid() }, + candidate: { + executable: normalized.candidate.executable, + adapter: normalized.candidate.adapter ?? null, + args: normalized.candidate.args, + }, + rawRequestedCwd: normalized.requestedCwd, + threadId, + launcher, + control, + mcpServer: inventory, + toolResponse, + toolPayload, + outcome, + }; + } catch (error) { + primaryError = asError(error); + } + + if (!launcher) launcher = tryReadLauncherObservation(launcherRecord); + let cleanupError = await session?.close(launcher?.pid); + try { + rmSync(tempRoot, { recursive: true, force: true }); + } catch (error) { + const tempError = new CodexNativeMcpEvidenceError( + 'cleanup-failure', + `failed to remove native MCP evidence temp root ${tempRoot}`, + { cause: asError(error) }, + ); + cleanupError = cleanupError + ? new CodexNativeMcpEvidenceError('cleanup-failure', 'process and temp cleanup both failed', { + cause: new AggregateError([cleanupError, tempError]), + }) + : tempError; + } + if (primaryError && cleanupError) { + throw new AggregateError([primaryError, cleanupError], 'native MCP evidence and cleanup both failed'); + } + if (primaryError) throw primaryError; + if (cleanupError) throw cleanupError; + if (!evidence) { + throw new CodexNativeMcpEvidenceError('app-server-failure', 'native MCP evidence produced no result'); + } + return evidence; +} + +/** + * Thin adapter for `codex-dogfood-harness.ts`. + * + * The real Codex executable is resolved from the host process (or the explicit + * `GENIE_DOGFOOD_REAL_CODEX` host override), never from the harness's isolated + * PATH where its lifecycle fake lives. Each known dogfood tag has a required + * fail-closed or exact-sentinel expectation before app-server starts. + */ +export async function captureCodexNativeMcpEvidenceForDogfood( + input: DogfoodNativeMcpEvidenceInput, +): Promise { + assertCandidateDigest(input.candidateBinary, input.candidateBinarySha256); + const codex = resolveHostCodexPin(); + const launcherExecutable = fileURLToPath(new URL('./codex-native-mcp-launcher.sh', import.meta.url)); + const base = { + codex, + requestedCwd: input.requestedCwd, + candidate: { + executable: input.candidateBinary, + ...(input.executionAdapter === undefined ? {} : { adapter: input.executionAdapter }), + args: ['mcp'], + }, + launcherExecutable, + controlExecutable: process.execPath, + env: input.executionAdapter === undefined ? input.env : { ...input.env, DOGFOOD_ROOT: input.root }, + tempDir: input.env.TMPDIR, + }; + const expectation = dogfoodExpectation(input); + const evidence = + expectation.kind === 'expected-error' + ? await captureCodexNativeMcpEvidence({ ...base, expectedError: expectation.error }) + : await captureCodexNativeMcpEvidence({ ...base, expectedSentinel: expectation.sentinel }); + return { + requestedCwd: evidence.rawRequestedCwd, + effectiveCwd: evidence.launcher.effectiveCwd, + cwdIdentity: evidence.launcher.cwdIdentity, + controlCwd: evidence.control.effectiveCwd, + controlCwdIdentity: evidence.control.cwdIdentity, + childPid: evidence.launcher.pid, + threadId: evidence.threadId, + isError: evidence.toolResponse.isError === true, + payload: evidence.toolPayload, + raw: evidence as unknown as Record, + }; +} + +/** + * Pure fail-closed sentinel validator, exported for fixture tests and callers + * that need to validate a previously captured `genie_board` payload. + */ +export function assertUniqueBoardSentinel( + payload: Record, + expected: NativeMcpTaskSentinel, +): NativeMcpTaskSentinel { + validateSentinel(expected); + if (!Array.isArray(payload.tasks)) { + throw new CodexNativeMcpEvidenceError('sentinel-mismatch', 'genie_board payload has no tasks array'); + } + if (payload.tasks.length !== 1) { + throw new CodexNativeMcpEvidenceError( + 'sentinel-mismatch', + `genie_board must return exactly one task, received ${payload.tasks.length}`, + ); + } + const observed = payload.tasks[0]; + if (!isRecord(observed)) { + throw new CodexNativeMcpEvidenceError('sentinel-mismatch', 'genie_board task sentinel is not an object'); + } + const projected = { + id: observed.id, + title: observed.title, + wish: observed.wish, + status: observed.status, + ...(expected.task.claimedBy === undefined ? {} : { claimedBy: observed.claimedBy }), + }; + if ( + projected.id !== expected.task.id || + projected.title !== expected.task.title || + projected.wish !== expected.task.wish || + projected.status !== expected.task.status || + (expected.task.claimedBy !== undefined && projected.claimedBy !== expected.task.claimedBy) + ) { + throw new CodexNativeMcpEvidenceError( + 'sentinel-mismatch', + `genie_board task sentinel differs: ${truncateJson(projected, 1_000)}`, + ); + } + return expected; +} + +function dogfoodExpectation( + input: DogfoodNativeMcpEvidenceInput, +): + | { kind: 'expected-error'; error: 'project-database-unavailable' } + | { kind: 'sentinel'; sentinel: NativeMcpTaskSentinel } { + const context = resolveProjectContext(input.requestedCwd); + if (input.tag === 'b-before-init') { + if (context.kind !== 'project-database-unavailable') { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + `b-before-init requires project-database-unavailable before launch, got ${context.kind}`, + ); + } + return { kind: 'expected-error', error: 'project-database-unavailable' }; + } + if (input.tag !== 'b-after-init' && input.tag !== 'a-new-thread') { + throw new CodexNativeMcpEvidenceError('invalid-options', `dogfood native MCP tag has no expectation: ${input.tag}`); + } + if (context.kind !== 'ok') { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + `${input.tag} requires an initialized project database, got ${context.kind}`, + ); + } + const db = new Database(context.dbPath, { readonly: true, strict: true }); + try { + const tasks = db + .query('SELECT id, title, wish, status, claimed_by AS claimedBy FROM tasks ORDER BY created_at, id') + .all() as Array>; + if (tasks.length !== 1) { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + `${input.tag} requires exactly one seeded task before launch, found ${tasks.length}`, + ); + } + const task = tasks[0]; + if ( + !task || + typeof task.id !== 'string' || + typeof task.title !== 'string' || + typeof task.wish !== 'string' || + typeof task.status !== 'string' || + typeof task.claimedBy !== 'string' + ) { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + `${input.tag} seeded task is missing exact wish/title/status/claimant identity`, + ); + } + return { + kind: 'sentinel', + sentinel: { + token: task.title, + task: { + id: task.id, + title: task.title, + wish: task.wish, + status: task.status, + claimedBy: task.claimedBy, + }, + }, + }; + } finally { + db.close(); + } +} + +function assertCandidateDigest(candidate: string, expectedSha256: string): void { + assertAbsolute('candidateBinary', candidate); + if (!/^[0-9a-f]{64}$/.test(expectedSha256)) { + throw new CodexNativeMcpEvidenceError('invalid-options', 'candidateBinarySha256 must be lowercase SHA-256'); + } + const actual = createHash('sha256').update(readFileSync(candidate)).digest('hex'); + if (actual !== expectedSha256) { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + `candidate binary digest mismatch: expected ${expectedSha256}, got ${actual}`, + ); + } +} + +function resolveHostCodexPin(): CodexAppServerPin { + const executable = process.env.GENIE_DOGFOOD_REAL_CODEX ?? Bun.which('codex'); + if (!executable || !isAbsolute(executable)) { + throw new CodexNativeMcpEvidenceError( + 'codex-unavailable', + 'real Codex not found on host PATH; set GENIE_DOGFOOD_REAL_CODEX to an absolute executable', + ); + } + const result = spawnSync(executable, ['--version'], { + encoding: 'utf8', + env: process.env, + maxBuffer: 64 * 1024, + timeout: DEFAULT_TIMEOUTS.preflightMs, + windowsHide: true, + }); + if (result.error || result.status !== 0 || !result.stdout.trim()) { + throw new CodexNativeMcpEvidenceError( + 'codex-unavailable', + `real Codex preflight failed for ${executable}: ${String(result.stderr).trim().slice(0, 1_000)}`, + { cause: result.error }, + ); + } + return { executable, version: result.stdout.trim() }; +} + +function normalizeOptions(options: CodexNativeMcpEvidenceOptions): CodexNativeMcpEvidenceOptions & { + candidate: NativeMcpCandidate & { args: string[] }; + tempDir: string; + timeouts: CodexNativeMcpEvidenceTimeouts; +} { + assertEnvironment(options.env); + assertAbsolute('requestedCwd', options.requestedCwd); + assertAbsolute('candidate.executable', options.candidate.executable); + if (options.candidate.adapter) assertAbsolute('candidate.adapter', options.candidate.adapter); + assertAbsolute('launcherExecutable', options.launcherExecutable); + assertAbsolute('controlExecutable', options.controlExecutable); + if (!existsSync(options.launcherExecutable)) { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + `launcherExecutable does not exist: ${options.launcherExecutable}`, + ); + } + const tempDir = options.tempDir ?? options.env.TMPDIR ?? tmpdir(); + assertAbsolute('tempDir', tempDir); + const expectationCount = Number(options.expectedSentinel !== undefined) + Number(options.expectedError !== undefined); + if (expectationCount !== 1) { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + 'exactly one of expectedSentinel or expectedError is required', + ); + } + if (options.expectedSentinel) validateSentinel(options.expectedSentinel); + return { + ...options, + candidate: { ...options.candidate, args: [...(options.candidate.args ?? ['mcp'])] }, + tempDir, + timeouts: mergeTimeouts(options.timeouts), + }; +} + +function validateSentinel(expected: NativeMcpTaskSentinel): void { + const values = [ + expected.task.id, + expected.task.title, + expected.task.wish, + expected.task.status, + ...(expected.task.claimedBy === undefined ? [] : [expected.task.claimedBy]), + ]; + if (!expected.token || values.some((value) => typeof value !== 'string' || value.length === 0)) { + throw new CodexNativeMcpEvidenceError('invalid-options', 'sentinel token and task identity must be non-empty'); + } + if (!values.some((value) => value.includes(expected.token))) { + throw new CodexNativeMcpEvidenceError( + 'invalid-options', + 'sentinel token must occur in at least one exact task identity field', + ); + } +} + +function assertEnvironment(env: NodeJS.ProcessEnv): void { + const codexHome = env.CODEX_HOME; + if (!codexHome || !isAbsolute(codexHome)) { + throw new CodexNativeMcpEvidenceError('invalid-options', 'caller env must contain an absolute CODEX_HOME'); + } +} + +function assertCodexPin(pin: CodexAppServerPin, env: NodeJS.ProcessEnv, timeoutMs: number): void { + assertAbsolute('codex.executable', pin.executable); + if (!pin.version.trim()) { + throw new CodexNativeMcpEvidenceError('invalid-options', 'codex.version must be non-empty'); + } + let result: SpawnSyncReturns; + try { + result = spawnSync(pin.executable, ['--version'], { + encoding: 'utf8', + env, + maxBuffer: 64 * 1024, + timeout: timeoutMs, + windowsHide: true, + }); + } catch (error) { + throw new CodexNativeMcpEvidenceError('codex-unavailable', `codex preflight failed: ${pin.executable}`, { + cause: asError(error), + }); + } + if (result.error || result.status !== 0) { + throw new CodexNativeMcpEvidenceError( + 'codex-unavailable', + `codex preflight failed (status=${String(result.status)}): ${String(result.stderr).trim().slice(0, 1_000)}`, + { cause: result.error }, + ); + } + const actualVersion = result.stdout.trim(); + if (actualVersion !== pin.version.trim()) { + throw new CodexNativeMcpEvidenceError( + 'codex-version-mismatch', + `codex version mismatch: expected ${JSON.stringify(pin.version.trim())}, got ${JSON.stringify(actualVersion)}`, + ); + } +} + +async function startThread( + session: AppServerSession, + options: ReturnType, + launcherRecord: string, +): Promise { + const adapter = options.candidate.adapter ?? '-'; + const response = await session.request( + 'thread/start', + { + cwd: options.requestedCwd, + ephemeral: true, + config: { + mcp_servers: { + [MCP_SERVER_NAME]: { + command: options.launcherExecutable, + args: [launcherRecord, adapter, options.candidate.executable, ...options.candidate.args], + }, + }, + }, + }, + options.timeouts.threadStartMs, + ); + const result = response.result; + const thread = isRecord(result) && isRecord(result.thread) ? result.thread : null; + if (!thread || typeof thread.id !== 'string' || !thread.id) { + throw new CodexNativeMcpEvidenceError( + 'app-server-failure', + `thread/start returned no thread id: ${truncateJson(response, 1_000)}`, + ); + } + return thread.id; +} + +async function waitForLauncherObservation( + recordFile: string, + candidate: NativeMcpCandidate & { args: string[] }, + timeoutMs: number, +): Promise { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + const observed = tryReadLauncherObservation(recordFile); + if (observed) { + const adapter = candidate.adapter ?? null; + if ( + observed.adapter !== adapter || + observed.candidate !== candidate.executable || + !arraysEqual(observed.args, candidate.args) + ) { + throw new CodexNativeMcpEvidenceError( + 'launcher-mismatch', + `launcher target differs from requested candidate: ${truncateJson(observed, 1_000)}`, + ); + } + return observed; + } + await delay(Math.min(50, Math.max(1, deadline - Date.now()))); + } + throw new CodexNativeMcpEvidenceError( + 'launcher-timeout', + `candidate launcher observation did not appear within ${timeoutMs}ms`, + ); +} + +function tryReadLauncherObservation(recordFile: string): NativeMcpLauncherObservation | null { + if (!existsSync(recordFile)) return null; + let fields: string[]; + try { + const bytes = readFileSync(recordFile); + fields = bytes + .toString('utf8') + .split('\0') + .filter((field, index, all) => !(index === all.length - 1 && field === '')); + } catch { + return null; + } + if (fields.length < 6 || fields[0] !== LAUNCHER_SCHEMA) return null; + const pid = Number(fields[1]); + const cwdIdentity = fields[3] ?? ''; + if (!Number.isSafeInteger(pid) || pid <= 0 || !/^\d+:\d+$/.test(cwdIdentity)) return null; + return { + pid, + effectiveCwd: fields[2] ?? '', + cwdIdentity, + adapter: fields[4] === '-' ? null : (fields[4] ?? null), + candidate: fields[5] ?? '', + args: fields.slice(6), + }; +} + +async function waitForMcpInventory( + session: AppServerSession, + threadId: string, + timeoutMs: number, +): Promise { + const deadline = Date.now() + timeoutMs; + let lastResult: unknown; + while (Date.now() < deadline) { + const remaining = Math.max(1, deadline - Date.now()); + const response = await session.request('mcpServerStatus/list', { threadId, detail: 'full', limit: 100 }, remaining); + lastResult = response.result; + const inventory = parseInventory(response.result); + if (inventory) return inventory; + await delay(Math.min(100, Math.max(1, deadline - Date.now()))); + } + throw new CodexNativeMcpEvidenceError( + 'mcp-inventory-mismatch', + `thread did not expose exactly one ${MCP_SERVER_NAME} server with genie_board: ${truncateJson(lastResult, 1_000)}`, + ); +} + +function parseInventory(result: unknown): McpInventory | null { + if (!isRecord(result) || !Array.isArray(result.data) || result.data.length !== 1) return null; + const server = result.data[0]; + if (!isRecord(server) || server.name !== MCP_SERVER_NAME || !isRecord(server.tools)) return null; + const tools = Object.keys(server.tools).sort(); + if (!tools.includes('genie_board')) return null; + return { name: MCP_SERVER_NAME, tools }; +} + +async function callBoard( + session: AppServerSession, + threadId: string, + timeoutMs: number, +): Promise { + const response = await session.request( + 'mcpServer/tool/call', + { threadId, server: MCP_SERVER_NAME, tool: 'genie_board', arguments: {} }, + timeoutMs, + ); + if (!isRecord(response.result) || !Array.isArray(response.result.content)) { + throw new CodexNativeMcpEvidenceError( + 'mcp-tool-failure', + `mcpServer/tool/call returned an invalid result: ${truncateJson(response, 1_000)}`, + ); + } + return response.result as unknown as NativeMcpToolResponse; +} + +function parseToolPayload(response: NativeMcpToolResponse): Record { + if (response.content.length !== 1) { + throw new CodexNativeMcpEvidenceError( + 'mcp-tool-failure', + `genie_board must return one content item, received ${response.content.length}`, + ); + } + const content = response.content[0]; + if (!isRecord(content) || content.type !== 'text' || typeof content.text !== 'string') { + throw new CodexNativeMcpEvidenceError('mcp-tool-failure', 'genie_board content item is not text'); + } + try { + const payload = JSON.parse(content.text) as unknown; + if (!isRecord(payload)) throw new Error('payload is not an object'); + return payload; + } catch (error) { + throw new CodexNativeMcpEvidenceError('mcp-tool-failure', 'genie_board text is not one JSON object', { + cause: asError(error), + }); + } +} + +async function runControl( + session: AppServerSession, + options: ReturnType, + timeoutMs: number, +): Promise { + const response = await session.request( + 'command/exec', + { + command: [options.controlExecutable, '-e', CONTROL_SOURCE], + cwd: options.requestedCwd, + timeoutMs, + outputBytesCap: 16_384, + }, + timeoutMs + 2_000, + ); + if (!isRecord(response.result)) { + throw new CodexNativeMcpEvidenceError( + 'control-failure', + `command/exec returned an invalid result: ${truncateJson(response, 1_000)}`, + ); + } + const { exitCode, stdout, stderr } = response.result; + if (exitCode !== 0 || typeof stdout !== 'string') { + throw new CodexNativeMcpEvidenceError( + 'control-failure', + `command/exec failed (exit=${String(exitCode)}): ${typeof stderr === 'string' ? stderr.slice(0, 1_000) : ''}`, + ); + } + try { + const parsed = JSON.parse(stdout.trim()) as unknown; + if ( + !isRecord(parsed) || + typeof parsed.cwd !== 'string' || + typeof parsed.pid !== 'number' || + typeof parsed.dev !== 'number' || + typeof parsed.ino !== 'number' + ) { + throw new Error('control payload fields are invalid'); + } + return { + pid: parsed.pid, + effectiveCwd: parsed.cwd, + cwdIdentity: `${parsed.dev}:${parsed.ino}`, + }; + } catch (error) { + throw new CodexNativeMcpEvidenceError('control-failure', 'command/exec stdout is not control evidence', { + cause: asError(error), + }); + } +} + +function assertCwdEquality(launcher: NativeMcpLauncherObservation, control: NativeMcpControlEvidence): void { + if (launcher.effectiveCwd !== control.effectiveCwd || launcher.cwdIdentity !== control.cwdIdentity) { + throw new CodexNativeMcpEvidenceError( + 'cwd-mismatch', + `candidate cwd ${launcher.effectiveCwd} (${launcher.cwdIdentity}) differs from control ` + + `${control.effectiveCwd} (${control.cwdIdentity})`, + ); + } +} + +function validateExpectedOutcome( + options: ReturnType, + response: NativeMcpToolResponse, + payload: Record, +): NativeMcpEvidenceOutcome { + if (options.expectedSentinel) { + if (response.isError === true) { + throw new CodexNativeMcpEvidenceError( + 'sentinel-mismatch', + `genie_board returned an error instead of the sentinel: ${truncateJson(payload, 1_000)}`, + ); + } + return { kind: 'sentinel', sentinel: assertUniqueBoardSentinel(payload, options.expectedSentinel), payload }; + } + if (response.isError !== true || payload.error !== options.expectedError) { + throw new CodexNativeMcpEvidenceError( + 'expected-error-mismatch', + `genie_board did not return expected ${String(options.expectedError)}: ${truncateJson(payload, 1_000)}`, + ); + } + if (Object.hasOwn(payload, 'tasks') || Object.hasOwn(payload, 'counts')) { + throw new CodexNativeMcpEvidenceError( + 'expected-error-mismatch', + 'fail-closed genie_board error must not contain tasks or counts', + ); + } + return { + kind: 'expected-error', + error: options.expectedError as NativeMcpExpectedError, + payload, + }; +} + +function mergeTimeouts(overrides: Partial | undefined): CodexNativeMcpEvidenceTimeouts { + const merged = { ...DEFAULT_TIMEOUTS, ...overrides }; + for (const [name, value] of Object.entries(merged)) { + if (!Number.isSafeInteger(value) || value <= 0 || value > 120_000) { + throw new CodexNativeMcpEvidenceError('invalid-options', `${name} must be an integer from 1 to 120000`); + } + } + return merged; +} + +function assertAbsolute(name: string, path: string): void { + if (!path || !isAbsolute(path)) { + throw new CodexNativeMcpEvidenceError('invalid-options', `${name} must be an absolute path`); + } +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value); +} + +function arraysEqual(a: string[], b: string[]): boolean { + return a.length === b.length && a.every((value, index) => value === b[index]); +} + +function truncateJson(value: unknown, maxLength: number): string { + try { + return JSON.stringify(value).slice(0, maxLength); + } catch { + return String(value).slice(0, maxLength); + } +} + +function asError(error: unknown): Error { + return error instanceof Error ? error : new Error(String(error)); +} + +function toEvidenceError( + error: unknown, + fallbackCode: CodexNativeMcpEvidenceErrorCode, + fallbackMessage: string, +): CodexNativeMcpEvidenceError { + if (error instanceof CodexNativeMcpEvidenceError) return error; + return new CodexNativeMcpEvidenceError(fallbackCode, `${fallbackMessage}: ${asError(error).message}`, { + cause: asError(error), + }); +} + +function delay(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} diff --git a/tests/support/codex-native-mcp-launcher.sh b/tests/support/codex-native-mcp-launcher.sh new file mode 100755 index 000000000..95cb2fc78 --- /dev/null +++ b/tests/support/codex-native-mcp-launcher.sh @@ -0,0 +1,42 @@ +#!/bin/sh +set -eu + +if [ "$#" -lt 3 ]; then + echo "usage: codex-native-mcp-launcher.sh RECORD ADAPTER_OR_DASH CANDIDATE [ARG ...]" >&2 + exit 64 +fi + +record_file=$1 +adapter=$2 +candidate=$3 +shift 3 + +effective_cwd=$(pwd -P) +if cwd_identity=$(stat -f '%d:%i' . 2>/dev/null); then + : +elif cwd_identity=$(stat -c '%d:%i' . 2>/dev/null); then + : +else + echo "codex-native-mcp-launcher: cannot stat effective cwd" >&2 + exit 70 +fi + +record_tmp="${record_file}.tmp.$$" +umask 077 +{ + printf '%s\0' 'codex-native-mcp-launcher-v1' + printf '%s\0' "$$" + printf '%s\0' "$effective_cwd" + printf '%s\0' "$cwd_identity" + printf '%s\0' "$adapter" + printf '%s\0' "$candidate" + for argument in "$@"; do + printf '%s\0' "$argument" + done +} >"$record_tmp" +mv "$record_tmp" "$record_file" + +if [ "$adapter" = '-' ]; then + exec "$candidate" "$@" +fi +exec "$adapter" "$candidate" "$@" diff --git a/tests/support/update-current-boundary-runner.ts b/tests/support/update-current-boundary-runner.ts new file mode 100644 index 000000000..e412b6b50 --- /dev/null +++ b/tests/support/update-current-boundary-runner.ts @@ -0,0 +1,102 @@ +import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { VERSION } from '../../src/lib/version.js'; + +type Scenario = 'failed' | 'route-upgrade' | 'repaired-current' | 'exit-handoff'; + +const scenario = process.env.GENIE_TEST_UPDATE_CURRENT_SCENARIO as Scenario | undefined; +const genieHome = process.env.GENIE_HOME; +if ( + genieHome === undefined || + !['failed', 'route-upgrade', 'repaired-current', 'exit-handoff'].includes(scenario ?? '') +) { + throw new Error('runner requires GENIE_HOME and a valid GENIE_TEST_UPDATE_CURRENT_SCENARIO'); +} + +const bin = join(genieHome, 'bin'); +for (const directory of ['.agents', '.claude-plugin', 'plugins/genie', 'skills/review', 'templates']) { + mkdirSync(join(bin, directory), { recursive: true }); +} +writeFileSync(join(bin, '.agents', 'plugin.json'), '{}\n'); +writeFileSync(join(bin, '.claude-plugin', 'marketplace.json'), '{}\n'); +writeFileSync(join(bin, 'LICENSE'), 'fixture\n'); +writeFileSync(join(bin, 'VERSION'), `${VERSION}\n`); +writeFileSync(join(bin, 'plugins', 'genie', 'plugin.json'), '{"name":"genie"}\n'); +writeFileSync(join(bin, 'skills', 'review', 'SKILL.md'), '# Review\n'); +writeFileSync(join(bin, 'templates', 'fixture.txt'), 'fixture\n'); +const executable = join(bin, 'genie'); +writeFileSync(executable, `#!/bin/sh\nif [ "\${1:-}" = "--version" ]; then printf 'genie ${VERSION}\\n'; fi\nexit 0\n`); +chmodSync(executable, 0o755); +mkdirSync(process.env.HOME as string, { recursive: true }); +mkdirSync(process.env.CODEX_HOME as string, { recursive: true }); +const marker = join(genieHome, '.install-version'); +writeFileSync(marker, 'prior-marker\n'); + +const { resolvePlatformId, updateCommand } = await import('../../src/genie-commands/update.js'); +const platform = resolvePlatformId(); + +function currentManifest() { + const raw = JSON.stringify({ + schema_version: 1, + channel: 'dev', + version: VERSION, + released_at: '2026-07-23T00:00:00.000Z', + tarball_base: `https://example.invalid/releases/v${VERSION}`, + platforms: [platform], + }); + return { + ...JSON.parse(raw), + manifestBytes: raw, + manifestSha256: 'a'.repeat(64), + }; +} + +const advancedManifest = { + ...currentManifest(), + version: '5.260723.999', + released_at: '2026-07-23T01:00:00.000Z', + manifestBytes: '{"advanced":"exact-object"}', + manifestSha256: 'f'.repeat(64), +}; +let deliveries = 0; +let convergenceRuns = 0; +let deliveredExact = false; +console.log = () => {}; +console.error = () => {}; + +await updateCommand( + { dev: true, yes: true, restart: false, verify: false }, + { + fetchManifest: async () => currentManifest(), + readInstalledVersion: () => VERSION, + requireCanonicalInstall: () => {}, + alreadyCurrent: { + attemptRepair: async () => { + if (scenario === 'failed') return { action: 'failed', detail: 'download-verify: invalid provenance' }; + if (scenario === 'route-upgrade') return { action: 'route-upgrade', manifest: advancedManifest }; + return { action: scenario }; + }, + runConvergence: () => { + convergenceRuns += 1; + }, + }, + deliverSelectedManifest: async (manifest) => { + deliveries += 1; + deliveredExact = manifest === advancedManifest; + return []; + }, + finalizeSelectedDelivery: async () => true, + }, +); + +const markerExists = existsSync(marker); +process.stdout.write( + `${JSON.stringify({ + deliveries, + convergenceRuns, + deliveredExact, + markerExists, + markerText: markerExists ? readFileSync(marker, 'utf8') : null, + commandExitCode: process.exitCode ?? 0, + })}\n`, +); diff --git a/tests/support/update-publication-failure-runner.ts b/tests/support/update-publication-failure-runner.ts new file mode 100644 index 000000000..6f0573f83 --- /dev/null +++ b/tests/support/update-publication-failure-runner.ts @@ -0,0 +1,41 @@ +import { createHash } from 'node:crypto'; +import { copyFileSync, mkdirSync, readFileSync } from 'node:fs'; +import { join } from 'node:path'; +import type { LatestManifest } from '../../src/genie-commands/update.js'; +import { updateCommand } from '../../src/genie-commands/update.js'; + +function required(name: string): string { + const value = process.env[name]; + if (!value) throw new Error(`missing ${name}`); + return value; +} + +const tarballSource = required('GENIE_TEST_TARBALL'); +const descriptorBytes = readFileSync(required('GENIE_TEST_DESCRIPTOR')); +const bundleBytes = readFileSync(required('GENIE_TEST_BUNDLE')); +const manifestBytes = readFileSync(required('GENIE_TEST_MANIFEST'), 'utf8'); +const parsed = JSON.parse(manifestBytes) as Omit; +const manifest: LatestManifest = { + ...parsed, + manifestBytes, + manifestSha256: createHash('sha256').update(manifestBytes).digest('hex'), +}; + +await updateCommand( + { stable: true, yes: true, restart: false, verify: false }, + { + fetchManifest: async () => manifest, + downloadDeliveryAssets: async (_manifest, _platform, destination) => { + mkdirSync(destination, { recursive: true }); + const releaseName = `genie-${manifest.version}-${_platform}.tar.gz`; + const exactTarballPath = join(destination, releaseName); + copyFileSync(tarballSource, exactTarballPath); + return { tarballPath: exactTarballPath, descriptorBytes, bundleBytes }; + }, + evidenceVerification: { + verifyBundle: () => ({ integratedTime: '1753228800' }), + }, + }, +); + +process.exit(process.exitCode ?? 0);