Land Phase 3 Epiphany thread state exposure

This commit is contained in:
Meta Vens 2026-04-24 00:41:16 +01:00
commit 80bc702675
20 changed files with 681 additions and 200 deletions

View file

@ -1,230 +1,165 @@
# Codex Epiphany Phase 2 Implementation Plan
# Codex Epiphany Phase 4 Implementation Plan
## Status
Implemented and verified in vendored Codex on 2026-04-23.
Planned on 2026-04-23 after landing and verifying:
What landed:
- Phase 1 durable Epiphany thread state
- Phase 2 prompt integration
- a minimal Phase 3 typed client read surface via `Thread.epiphanyState`
- `EpiphanyStateInstructions` as a dedicated developer-context fragment
- a bounded `EpiphanyThreadState` renderer wrapped in `<epiphany_state> ... </epiphany_state>`
- prompt injection from `Session::build_initial_context` whenever `SessionState.epiphany_state` is present
- prompt-facing inclusion, omission, resume, bounded-rendering, and snapshot coverage
Next likely slice:
- Phase 3 typed app-server/protocol exposure so GUI clients can read Epiphany thread state directly instead of inferring it from prompt text
This is the actionable implementation note for the second Epiphany patch slice against the real `openai/codex` tree now vendored directly under `vendor/codex`.
Phase 1 landed in parent commit `2042687e3035c5a86d7f6aa66306d87abcc10f2d`. The durable state seam exists. Codex can now persist and replay structured Epiphany thread state. The next job is to make that state actually matter to the turn loop without pretending we already have GUI, retrieval, or operator ergonomics solved.
The next job is to give Epiphany a real repo retrieval organ instead of making every future role rediscover the workspace with shell commands and optimism.
## Summary
Land Phase 2 as a **prompt-integration slice** that derives a compact Epiphany summary from `SessionState.epiphany_state` and injects it into the developer-context path during turn construction.
Land Phase 4 as a **bounded internal retrieval slice**:
- add typed retrieval state and shard/index summaries to Epiphany state
- add one additive app-server retrieval query surface for loaded threads
- support hybrid retrieval from day one:
- exact/path/symbol/lexical results
- semantic chunk results
- keep the slice internal/dev-usable first
After this slice:
- Epiphany state affects model behavior instead of just sitting in rollout
- resumed Epiphany sessions carry their structured understanding back into the prompt path
- normal non-Epiphany sessions still behave exactly as they do now
- Epiphany can ask structured repo questions through one typed surface
- GUI work has a real data source later
- retrieval can evolve without making prompt text or shell transcripts the canonical knowledge path
This slice still does **not** include:
This slice should still avoid:
- GUI surfaces
- app-server notifications or typed read/update RPCs
- retrieval indexing
- watcher-driven semantic invalidation
- observation promotion heuristics
- watcher-driven invalidation
- automatic observation promotion
- GUI implementation
- specialist-agent scheduling
- public operator UX
- user-facing activation flows
## Phase 2 Principle
## Phase 4 Principle
The activation rule for Phase 2 should stay brutally simple:
Do not build "vector search" as a separate novelty organ off to the side.
- if `SessionState.epiphany_state` is `Some`, the session is Epiphany-active
- if it is `None`, nothing new is injected
That is enough for internal/dev use. Do not invent a new public toggle, preset, or protocol field in this slice.
Build a **hybrid repo retrieval subsystem** with one typed query shape and one typed result shape. Exact lookup and semantic lookup should be different modes of the same machine, not rival religions.
## Key Changes
### 1. Add a dedicated Epiphany prompt fragment renderer
### 1. Extend Epiphany state with retrieval metadata
Touch:
- `vendor/codex/codex-rs/core/src/context/mod.rs`
- `vendor/codex/codex-rs/core/src/context/epiphany_state_instructions.rs` (new)
- `vendor/codex/codex-rs/protocol/src/protocol.rs`
Add a new contextual fragment, tentatively:
Add minimal retrieval metadata to `EpiphanyThreadState`, for example:
- `EpiphanyStateInstructions`
- `retrieval`
- `workspace_root`
- `index_revision`
- `status`
- `semantic_available`
- `last_indexed_at`
- `shards`
- `dirty_paths`
Pattern it after other developer-scoped context fragments such as collaboration-mode and permissions instructions.
Keep this metadata summary-focused. Do not dump raw embeddings, giant posting lists, or per-file sludge into thread state.
### 2. Add a core retrieval facade
Touch:
- `vendor/codex/codex-rs/core/src/`
Add a focused retrieval module, tentatively:
- `epiphany_retrieval.rs`
Responsibilities:
- render a compact, deterministic textual summary from `EpiphanyThreadState`
- wrap it in its own clear developer marker block such as `<epiphany_state> ... </epiphany_state>`
- include light instruction text that tells the model how to treat the state:
- use it as the current structured thread understanding
- do not silently redefine it
- surface mismatches or gaps before broad edits
- accept a loaded thread plus query params
- resolve the relevant workspace root
- run exact retrieval and semantic retrieval behind one interface
- return typed ranked results with file paths, line anchors, excerpts, and retrieval mode metadata
Do **not** dump raw JSON into the prompt.
Result types should be explicit and bounded:
### 2. Keep the summary bounded and intentionally selective
- exact hits
- semantic hits
- optional graph-linked ids later
The renderer should include only the parts that help the model stay oriented during a turn.
Do not make the first cut depend on prompt parsing or transcript scraping.
Include:
### 3. Keep the first semantic backend small and local
- `revision`
- `objective`
- `active_subgoal_id`
- a bounded list of nearby `subgoals`
- a bounded list of `invariants`
- a compact `graphs` summary centered on the frontier
- `graph_frontier`
- `graph_checkpoint`
- recent `observations`
- recent `recent_evidence`
- `churn`
- `mode`
- `last_updated_turn_id`
The first semantic backend should be workspace-local, sharded, and incremental-friendly in storage layout, even if the initial indexing policy is still crude.
Default Phase 2 shaping rules:
Requirements:
- prefer frontier and checkpoint over whole-graph dumping
- prefer active and recent records over exhaustive history
- cap list lengths aggressively
- keep code refs line-anchored when rendered, but do not spam them everywhere
- omit empty sections entirely
- do not store everything in one giant JSON blob
- prefer per-workspace or per-subsystem shards
- track index revision and freshness explicitly
- keep chunk text line-anchored and path-aware
Phase 2 should **not** naively dump full scratch content. Scratch is volatile and can turn the prompt back into sludge. If scratch is rendered at all, keep it to a tiny active-summary form.
If the first semantic backend needs to be intentionally narrow, that is fine. A small honest subsystem beats a magical blob with terrible write behavior.
### 3. Inject the Epiphany summary during initial context construction
### 4. Add one additive app-server query surface
Touch:
- `vendor/codex/codex-rs/core/src/session/mod.rs`
- `vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs`
- `vendor/codex/codex-rs/app-server/src/codex_message_processor.rs`
- `vendor/codex/codex-rs/app-server/README.md`
The integration point is:
Add one typed method for retrieval, tentatively:
- `Session::build_initial_context`
- `thread/epiphany/retrieve`
Add:
Suggested request fields:
- a read of `self.epiphany_state().await`
- conditional rendering of `EpiphanyStateInstructions` when the state exists
- insertion into `developer_sections`
- `threadId`
- `query`
- optional `cwd`
- optional `modes` or `preferSemantic`
- optional `limit`
- optional `pathPrefixes`
Recommended placement:
Suggested response fields:
- after collaboration-mode instructions
- before realtime/personality/apps/skills/plugin additions
- `query`
- `indexSummary`
- `results`
That keeps the Epiphany state near the top-level task discipline instead of burying it behind auxiliary capability chatter.
Keep it additive and read-only. No write/update/index-control methods yet unless they turn out to be mechanically unavoidable.
Do **not** change:
### 5. Reuse existing exact-search substrate where practical
- `TurnContext`
- rollout protocol
- tool routing
- token accounting rules
Codex already has fuzzy/exact-ish repo search substrate. Reuse what is useful instead of building a second exact-search toy from scratch.
unless a small mechanical adjustment becomes unavoidable during implementation.
But do not confuse that with the whole answer. Phase 4 exists because exact file search alone is not enough.
### 4. Treat prompt integration as read-only in this slice
### 6. Test the machine, not just the types
Phase 2 is about **consuming** Epiphany state during turns, not teaching the model to rewrite canon by itself.
Add tests for:
So in this slice:
- the model reads Epiphany state
- the existing persistence seam remains the storage mechanism
- no automatic state mutation from assistant output happens yet
If state updates are needed during testing, use the internal/dev hooks that already exist from Phase 1.
### 5. Add prompt-facing tests, not just persistence tests
Touch:
- `vendor/codex/codex-rs/core/src/session/tests.rs`
- `vendor/codex/codex-rs/core/src/session/snapshots/` (if a new snapshot is needed)
- optionally `vendor/codex/codex-rs/core/src/context/epiphany_state_instructions.rs` local tests
Add at least:
1. `build_initial_context_includes_epiphany_state_block_when_present`
- seed `SessionState.epiphany_state`
- call `build_initial_context`
- verify the developer bundle includes the Epiphany block
2. `build_initial_context_omits_epiphany_state_block_when_absent`
- verify normal sessions stay unchanged
3. one bounded-rendering test
- prove the renderer prefers frontier/recent items and omits empty sections
4. one snapshot-style request/context test
- similar in spirit to the existing fork/context snapshot coverage
- confirm Epiphany state appears where expected in the built prompt
If practical, add one resume-oriented assertion:
- reconstruct a session from rollout with Epiphany state
- build initial context
- verify the injected Epiphany block survives replay and resume
## Public Interfaces
Phase 2 should still avoid public surface churn.
No new:
- protocol events
- app-server notifications
- GUI methods
- public activation controls
This is still an internal/dev-usable slice.
1. protocol serde for the new retrieval request/response shapes
2. core ranking/merging behavior for mixed exact + semantic results
3. app-server handling for the new request
4. retrieval metadata inclusion in `EpiphanyThreadState`
5. a smoke test that proves semantic retrieval can find a concept that exact-name search would miss
## Assumptions
- The existence of `SessionState.epiphany_state` is sufficient as the Phase 2 activation signal.
- The Epiphany prompt block belongs in the developer-context path, not as a user message.
- Prompt integration should remain compact and deterministic; this slice is not permission to stuff the entire map back into one giant haunted string.
- Phase 2 is still safe to ship without GUI changes because non-Epiphany sessions remain untouched.
## Test Plan
### Core behavior
- Epiphany-active sessions inject a developer-scoped Epiphany summary
- non-Epiphany sessions do not
- resumed sessions with persisted Epiphany state inject the latest surviving summary
### Rendering discipline
- empty fields do not create empty sections
- bounded sections truncate deterministically
- frontier/gap/evidence ordering is stable
### Compatibility
- existing prompt/context tests continue to pass for non-Epiphany paths
- no new rollout compatibility fallout should appear because Phase 2 consumes existing state only
- Phase 4 is still internal/dev-usable only.
- Retrieval should be thread/workspace aware, not a global cross-repo soup.
- The first retrieval slice can be read-only.
- Watcher-driven invalidation belongs later; this slice only needs enough metadata to admit freshness honestly.
## Immediate Next Step After This Plan
Implement the smallest useful prompt path only:
Implement the smallest useful retrieval baseline:
1. add `EpiphanyStateInstructions`
2. inject it from `Session::build_initial_context` when `epiphany_state` exists
3. add omission/inclusion tests
4. add one snapshot proving the prompt shape
1. extend `EpiphanyThreadState` with retrieval metadata
2. add a core hybrid retrieval facade
3. expose one typed `thread/epiphany/retrieve` read method
4. document it in `app-server/README.md`
5. verify with targeted protocol/core/app-server tests
Do not start with GUI. Do not start with retrieval. Do not start teaching the model to rewrite canon from its own prose.
Do not start with GUI. Do not start with automatic graph invalidation. Do not pretend shell transcripts are a retrieval strategy.

View file

@ -11,7 +11,7 @@ The working idea is simple enough:
- keep evidence durable
- make the harness consult that state instead of pretending the transcript is a brain
The project target is now the vendored Codex core harness itself, not a sidecar wrapper first and not some random GitHub “OpenCodex” contraption held together with hope and string.
The project target is now the vendored Codex core harness itself, not a sidecar wrapper first and not some random GitHub "OpenCodex" contraption held together with hope and string.
## Current Repo State
@ -25,6 +25,7 @@ Recent anchor commits before the current in-flight work:
- `2042687e3035c5a86d7f6aa66306d87abcc10f2d` - vendored Codex and landed the Phase 1 Epiphany core state slice
- `c823815` - persisted the Phase 2 implementation plan and refreshed the project handoff/state
- `efd1420` - landed and pushed the Phase 2 prompt-integration slice
- `640e063` - documented the pre-compaction Epiphany workflow and synced the repo memory before compaction
Do not trust this note for the exact current HEAD; use `git log --oneline -1` if you need the live commit.
@ -66,7 +67,7 @@ Main touched files:
- `vendor/codex/codex-rs/core/src/session/rollout_reconstruction_tests.rs`
- plus rollout/state/app-server compatibility readers
Phase 2 is now done too.
Phase 2 is done too.
The Phase 2 prompt-integration slice was implemented inside vendored Codex:
@ -85,23 +86,48 @@ Main touched files for Phase 2:
- `vendor/codex/codex-rs/core/src/session/snapshots/codex_core__session__build_initial_context_epiphany_state.snap`
- `vendor/codex/codex-rs/protocol/src/protocol.rs`
Phase 3 is landed too, in a conservative read-surface form.
What landed:
- app-server protocol `Thread` payloads now expose optional typed `epiphany_state`
- hydrated thread responses prefer live `CodexThread` state when a thread is loaded
- stored thread reads fall back to rollout reconstruction through a shared core helper that respects rollback and compaction semantics
- the minimal typed client surface is now real without making prompt text the GUI data source
- dedicated Epiphany-specific update RPCs and live state notifications are still deferred
Main touched files for Phase 3:
- `vendor/codex/codex-rs/core/src/epiphany_rollout.rs`
- `vendor/codex/codex-rs/core/src/lib.rs`
- `vendor/codex/codex-rs/core/src/codex_thread.rs`
- `vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs`
- `vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs`
- `vendor/codex/codex-rs/app-server/src/codex_message_processor.rs`
- `vendor/codex/codex-rs/app-server/README.md`
- compatibility fixture/test updates in `analytics`, `exec`, and `tui`
## What Must Be Remembered Before Compaction
If a future session wakes up from compaction and starts bluffing, this is the part to staple to its forehead.
- Phase 1 and Phase 2 are both landed, verified, committed, and pushed.
- The latest pushed implementation anchor before this handoff sync is `efd1420`:
- `Land Phase 2 Epiphany prompt integration`
- Phase 1, Phase 2, and the minimal Phase 3 read surface are all landed and verified.
- The latest pushed implementation anchor before this handoff sync is `640e063`:
- `Document pre-compaction Epiphany workflow`
- `vendor/codex` is ordinary tracked repo content, not a submodule.
- Phase 2 means Codex now **reads** Epiphany state during turn construction:
- `SessionState.epiphany_state` is the internal activation signal
- `Session::build_initial_context` injects a bounded `<epiphany_state>` developer fragment
- resumed sessions reuse the restored Epiphany snapshot in the prompt path
- Phase 3 means clients can now **read** typed Epiphany state directly from hydrated `Thread` payloads:
- `thread/start`, `thread/resume`, `thread/fork`, `thread/read`, `thread/unarchive`, and detached review-thread startup can carry `thread.epiphanyState` when present
- loaded threads use live state
- stored thread reads reconstruct from rollout with the same rollback/compaction semantics as core
- there are still no dedicated Epiphany update RPCs or live `thread/epiphany/*` notifications
- The next phase is **not** GUI work.
- The next phase is **Phase 3 typed state exposure**:
- expose Epiphany thread state to clients over typed app-server/protocol surfaces
- keep it additive and internal/dev-usable first
- do not make prompt text the canonical GUI data source
- The next phase is **Phase 4 repo-local hybrid retrieval**:
- give Epiphany a real repo retrieval subsystem instead of repeated file-by-file shell archaeology
- keep it typed, additive, and GUI-friendly
- Important Windows verification footgun still stands:
- use `CARGO_TARGET_DIR=C:\Users\Meta\.cargo-target-codex` for `codex-core` work on this machine
- Snapshot hygiene note:
@ -138,6 +164,12 @@ Verified:
- targeted new `codex-core` Epiphany persistence/replay tests passed
- targeted `codex-core` `epiphany` tests passed after Phase 2
- broader `codex-core` `build_initial_context_*` coverage passed after Phase 2
- `cargo fmt --all` passed again after Phase 3
- `cargo test -p codex-core -p codex-app-server-protocol -p codex-app-server -p codex-analytics -p codex-exec -p codex-tui --lib --no-run` passed after the new `Thread.epiphanyState` field fallout was patched
- targeted Phase 3 tests passed:
- `cargo test -p codex-core --lib latest_epiphany_state_from_rollout_items`
- `cargo test -p codex-app-server --lib load_epiphany_state_from_rollout_path_reads_latest_snapshot`
- `cargo test -p codex-app-server-protocol --lib serialize_client_response`
Important Windows footgun:
@ -149,39 +181,38 @@ Without that, you get to learn about `symlink_dir failed: ... A required privile
## Recommended Next Implementation
Do **Phase 3 typed state exposure** next.
Do **Phase 4 repo-local hybrid retrieval** next.
The durable state seam exists and the turn loop now reads it. The next clean move is to expose that state to clients without forcing GUI code to scrape prompt text.
The durable state seam exists, the turn loop now reads it, and clients can now load typed Epiphany state directly. The next clean move is to stop making every future agent rediscover the repo with shell spelunking.
Planned slice:
1. add typed app-server/protocol read surfaces for Epiphany thread state
2. keep the new surface additive and internal/dev-usable first
3. avoid making transcript text the canonical source for GUI state
4. leave retrieval, invalidation, and specialist-agent scheduling for later slices
1. define retrieval state and shard/index summaries in typed Epiphany state
2. add a bounded hybrid retrieval surface that combines exact search with semantic chunk lookup
3. keep the first retrieval slice additive and internal/dev-usable first
4. leave watcher-driven invalidation, heavy GUI reflection, and specialist-agent scheduling for later slices
Main files for the next slice:
- `vendor/codex/codex-rs/protocol/src/`
- `vendor/codex/codex-rs/core/src/`
- `vendor/codex/codex-rs/app-server/src/`
- `vendor/codex/codex-rs/app-server-protocol/src/`
- `vendor/codex/codex-rs/protocol/src/`
- `vendor/codex/codex-rs/core/src/session/`
## What Not To Do Next
Not yet:
- GUI work
- protocol event expansion
- retrieval indexing
- watcher-driven semantic invalidation
- automatic observation promotion
- specialist-agent scheduling
- user-facing activation flows
The next slice should stay small and mean:
- expose typed state reads
- keep GUI, retrieval, and mutation logic dark
- add typed retrieval scaffolding
- keep GUI, mutation, and specialist-agent logic dark
## Verification Commands
@ -203,4 +234,4 @@ cmd /c "\"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxi
The repo is in a good state.
Phase 1 and Phase 2 are landed and verified. `vendor/codex` is first-class in the parent repo now. The next clean move is typed state exposure for clients, not GUI paint and not another architectural detour. Also: pre-compaction persistence is now an explicit design rule, not a lucky habit.
Phase 1, Phase 2, and a minimal Phase 3 typed read surface are landed and verified. `vendor/codex` is first-class in the parent repo now. The next clean move is repo-local hybrid retrieval, not GUI paint and not another architectural detour. Also: pre-compaction persistence is now an explicit design rule, not a lucky habit.