diff --git a/README.md b/README.md index dec87deb..05e8ca08 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ The current spine is real, not aspirational: - verifier-backed promotion through `thread/epiphany/promote` - successful-write notifications through `thread/epiphany/stateUpdated` - durable `jobBindings` as a thin Epiphany-owned launcher seam over runtime job backends +- explicit launch and interrupt authority through `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt`, still backed by runtime `agent_jobs` - live bound-job progress notifications through `thread/epiphany/jobsUpdated` - read-only reflection surfaces through `thread/epiphany/scene`, `jobs`, `freshness`, `context`, and `pressure` - durable investigation checkpointing for compaction-safe planning diff --git a/notes/epiphany-core-harness-surfaces.md b/notes/epiphany-core-harness-surfaces.md index 7cc86e14..11ebb750 100644 --- a/notes/epiphany-core-harness-surfaces.md +++ b/notes/epiphany-core-harness-surfaces.md @@ -46,6 +46,8 @@ The rule is: | `thread/epiphany/propose` | read-only proposal | landed | Drafts graph/frontier/churn candidates from verified evidence-backed observations. | | `thread/epiphany/promote` | verifier gate | landed | Rejects or applies candidates through the durable update path. | | `thread/epiphany/stateUpdated` | notification | landed | Emits updated projected state, source, revision, and changed fields after successful update/promote writes. | +| `thread/epiphany/jobLaunch` | bounded authority write | landed, live-smoked | Creates a launcher-owned durable `jobBinding`, launches the current backend adapter, and emits `stateUpdated` with source `jobLaunch`. | +| `thread/epiphany/jobInterrupt` | bounded authority write | landed, live-smoked | Interrupts the current backend adapter for a bound launcher job, clears backend identity from the durable `jobBinding`, and emits `stateUpdated` with source `jobInterrupt`. | | `thread/epiphany/jobsUpdated` | notification | landed | Emits changed launcher-bound job snapshots for real runtime progress events when the mapped payload actually changes. | | `thread/epiphany/scene` | read-only reflection | landed, live-smoked | Compact client scene derived from authoritative Epiphany state, including checkpoint summary reflection. | | `thread/epiphany/jobs` | read-only reflection | landed, live-smoked | Derived indexing, remap, verification, and specialist-progress slots from typed state and retrieval summaries, with durable launcher metadata plus live backend overlay when a real owner exists. | @@ -62,6 +64,8 @@ Durable Epiphany state may change through: - `thread/epiphany/update` - accepted `thread/epiphany/promote` +- `thread/epiphany/jobLaunch` +- `thread/epiphany/jobInterrupt` - normal rollout persistence of the current live `EpiphanyThreadState` The following must stay read-only: @@ -131,7 +135,6 @@ Metaphor is compression after source context. It is not decoration for guesses. These are not landed yet: -- an explicit launch/interrupt surface over the landed thin launcher seam - richer evidence-range and graph-shard inspection beyond the landed context shard - automatic watcher-driven graph/retrieval/invariant invalidation policy on top of the landed freshness reflection - automatic tool-output observation promotion @@ -153,6 +156,13 @@ scope, backend kind, and backend job id, and the current adapter can overlay that launcher metadata onto live runtime `agent_jobs` snapshots. The surface does not start, schedule, create, or notify jobs. +The first explicit bounded authority surfaces are also landed as +`thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt`. They are +allowed to mutate durable `jobBindings` and talk to the current backend +adapter, but only to create or interrupt explicit launcher-owned work. They +are not a queue, a second scheduler, or permission to smuggle runtime policy +into the reflection surfaces. + The first live bound-runtime progress notification is also landed as `thread/epiphany/jobsUpdated`. It rides existing `agent_job_progress:{json}` background events from the runtime job runner, resolves matching launcher @@ -271,11 +281,12 @@ The pressure signal now exists as `thread/epiphany/pressure`, the freshness signal now exists as `thread/epiphany/freshness`, live watcher-backed invalidation telemetry now exists inside that freshness surface for loaded threads, durable investigation packets now exist in typed state plus -prompt/scene/context reflection, and the first bounded policy verdict now -exists as `thread/epiphany/reorient`. What does not exist yet is the runtime -coordinator that acts on that verdict. Automatic CRRC still needs launch and -interrupt authority over the thin job seam, job owners that can be stopped -cleanly, and explicit runtime policy instead of vibes with a clipboard. +prompt/scene/context reflection, the first bounded policy verdict now exists as +`thread/epiphany/reorient`, and explicit launch/interrupt authority now exists +over the thin job seam. What does not exist yet is the runtime coordinator that +acts on that verdict. Automatic CRRC still needs explicit runtime policy, +bounded job ownership, and clean stopping rules instead of vibes with a +clipboard. Compaction should squeeze scratch, not the map. diff --git a/notes/epiphany-current-algorithmic-map.md b/notes/epiphany-current-algorithmic-map.md index 87326d4f..bb279f0c 100644 --- a/notes/epiphany-current-algorithmic-map.md +++ b/notes/epiphany-current-algorithmic-map.md @@ -10,7 +10,7 @@ The important question for this file is not "what is Epiphany supposed to become Current Epiphany is a forked Codex harness with a typed modeling spine. -The spine exists in seventeen live paths: +The spine exists in eighteen live paths: - protocol state shape: `EpiphanyThreadState`, `RolloutItem::EpiphanyState`, retrieval summaries, and prompt tags live in [protocol.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/protocol/src/protocol.rs:103). - in-memory session state: `SessionState` stores an optional `EpiphanyThreadState`, and `Session` exposes thin async accessors over it in [state/session.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/core/src/state/session.rs:36) and [session/mod.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/core/src/session/mod.rs:1240). @@ -25,6 +25,7 @@ The spine exists in seventeen live paths: - scene projection: app-server declares experimental read-only `thread/epiphany/scene`, reads the same live/stored Epiphany state view used by `thread/read`, and derives a compact client scene with objective, active subgoal, invariant status counts, graph focus/counts, retrieval status, investigation-checkpoint summary counts, observation/evidence summaries, churn, live/stored source, and available control-plane actions without mutating or persisting state in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:340), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:3924), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4115), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:10757). - job/progress reflection: app-server declares experimental read-only `thread/epiphany/jobs`, reads the same thread view as scene, optionally fills retrieval state for loaded threads with no Epiphany state, and derives four non-authoritative progress slots for retrieval indexing, graph remap, invariant verification, and specialist work. Durable `job_bindings` now act as a thin Epiphany-owned launcher seam with launcher id, authority scope, backend kind, and backend job id; the current adapter can overlay those launcher bindings onto live runtime `agent_jobs` snapshots so the surface can show real owner/progress/thread information without scheduling, notifying, mutating, or persisting runtime jobs in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:347), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4142), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4176), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:11998). - bound-job progress notifications: app-server now declares experimental `thread/epiphany/jobsUpdated`, listens for `agent_job_progress:{json}` background events from the runtime job runner, resolves matching launcher bindings against live `agent_jobs` snapshots through the current backend adapter, and emits only the changed bound-job payloads when the mapped job state actually differs from the last notification for that thread. It does not poll in a loop, mutate Epiphany state, schedule work, or make `thread/epiphany/jobs` a writer. +- explicit job control: app-server now declares experimental `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt`, routes them through core launch/interrupt handlers that can open the live or direct sqlite state runtime, create or cancel the current `agent_jobs` backend binding, persist durable `job_bindings` revisions through the authoritative Epiphany state path, interrupt live worker threads when needed, and emit `thread/epiphany/stateUpdated` with source `jobLaunch` or `jobInterrupt` instead of pretending jobs reflection is the scheduler in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:389), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4850), [codex_thread.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/core/src/codex_thread.rs:418), [codex_thread.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/core/src/codex_thread.rs:493), [codex_thread.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/core/src/codex_thread.rs:657), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4992). - freshness reflection: app-server declares experimental read-only `thread/epiphany/freshness`, reads the same live/stored thread view used by scene/jobs/context, fills live retrieval state for loaded threads, and derives one bounded retrieval/graph freshness lens plus, for loaded threads, watcher-backed invalidation inputs with watched root, changed paths, mapped graph-node hits, active-frontier hits, and revision/source identity without mutating, scheduling, notifying, or performing automatic semantic invalidation in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:350), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4167), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4259), [epiphany_invalidation.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/epiphany_invalidation.rs:19), [epiphany_invalidation.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/epiphany_invalidation.rs:67), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4219), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:11073). - targeted context reflection: app-server declares experimental read-only `thread/epiphany/context`, reads the same live/stored Epiphany state view used by scene/jobs, and derives a bounded state shard containing selected graph nodes/edges, links, active frontier, checkpoint, the full durable investigation-checkpoint packet, observations, direct evidence, linked evidence, and missing requested ids without retrieval, proposal, promotion, notification, mutation, or persistence in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:355), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4253), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4261), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:11152). - context-pressure reflection: core token telemetry carries `model_auto_compact_token_limit` beside total/last usage and `model_context_window`; app-server declares experimental read-only `thread/epiphany/pressure`, reads live token usage or the latest stored rollout `TokenCount`, and derives `unknown`/`low`/`elevated`/`high`/`critical` pressure plus a non-acting compaction-prep recommendation without mutating Epiphany state or scheduling compaction in [protocol.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/protocol/src/protocol.rs:2237), [turn_context.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/core/src/session/turn_context.rs:103), [session/mod.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/core/src/session/mod.rs:2716), [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:360), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4361), [token_usage_replay.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor/token_usage_replay.rs:75), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4306), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:11028). @@ -84,6 +85,8 @@ flowchart TD A --> AH["thread/epiphany/propose requires loaded thread"] A --> AD["thread/epiphany/promote requires loaded thread"] A --> Z["thread/epiphany/update requires loaded thread"] + A --> AJL["thread/epiphany/jobLaunch requires loaded thread"] + A --> AJI["thread/epiphany/jobInterrupt requires loaded thread"] AA --> AB["epiphany-core distill_observation"] AB --> AC["Observation/evidence patch proposal"] AC --> AD @@ -98,6 +101,12 @@ flowchart TD R --> T["CodexThread.epiphany_retrieve"] S --> U["CodexThread.epiphany_index"] Z --> ZA["CodexThread.epiphany_update_state"] + AJL --> AJLA["CodexThread.epiphany_launch_job"] + AJLA --> AJLB["Persist launcher-bound jobBinding + create backend job"] + AJLB --> ZD + AJI --> AJIA["CodexThread.epiphany_interrupt_job"] + AJIA --> AJIB["Interrupt backend worker(s) + clear bound backend from jobBinding"] + AJIB --> ZD ZA --> ZB["Apply typed patch and increment revision"] ZB --> B ZB --> ZC["Persist immediate RolloutItem::EpiphanyState"] @@ -1308,7 +1317,7 @@ stored Epiphany state -> successful writes emit thread/epiphany/stateUpdated ``` -The current remaining missing organ is not the red pen, not the first chewing motion, not the first state-change bell, not the first client-shaped scene, not the first job/progress reflection, not the first live bound-job notification, not the first freshness lens, not the first watcher-backed invalidation read, not the first targeted graph/evidence context read, not the first context-pressure gauge, not the first durable investigation checkpoint packet, not the first bounded reorientation verdict, and no longer the first thin launcher seam. Those exist. Proposal now has the first bits of map memory, focus, and selection hygiene: it can auto-select a bounded evidence-backed observation cluster when ids are omitted, requires accepting recent evidence behind selected observations, prioritizes stronger selected observations for proposal wording, reuses existing architecture nodes by concrete code-ref/path/id checks before creating new path nodes, can rescue unanchored graph nodes through strict unique semantic overlap, follows graph links and incident edges into the frontier, and reports match-kind-aware churn pressure from the actual proposal shape. Distillation now also has the first source-output-aware teeth for tool/model summaries. Promotion now notices risky deltas instead of just checking the shape of the envelope, treats graph expansion as risky even if the patch tries to mumble "low pressure" while dragging a new wall into the blueprint, token-matches strong verifier kinds so `contest` cannot sneak past as `test`, and validates investigation checkpoints against known evidence before the packet can be inked. Successful update/promote writes now emit `thread/epiphany/stateUpdated` with `source: "update"` or `source: "promote"`, event-level revision, and typed changed fields so clients no longer need to poll blindly, infer the cause from timing, or diff the whole state to know which top-level sections moved. Runtime `agent_job_progress` background events now also emit `thread/epiphany/jobsUpdated` with changed bound-job payloads so clients no longer need to poll `thread/epiphany/jobs` just to watch one real owner move, and the bound jobs now reflect launcher id, authority scope, and backend kind/job id instead of treating raw runtime pointers as the architecture. The scene, jobs, freshness, context, pressure, and reorient surfaces give clients compact or bounded read-only views without making the GUI/client authoritative, scene/context surface the checkpoint packet back out without becoming writers, freshness surfaces watcher telemetry without becoming a hidden worker, and reorient now turns those signals into an explicit wakeup verdict. The remaining missing organs are an explicit launch/interrupt authority surface over the landed seam, a second backend if `agent_jobs` stops being enough, a runtime consumer that can act on the reorientation verdict without becoming a hidden tyrant, and eventually role-scoped specialist ownership without silently auto-promoting anything. +The current remaining missing organ is not the red pen, not the first chewing motion, not the first state-change bell, not the first client-shaped scene, not the first job/progress reflection, not the first live bound-job notification, not the first freshness lens, not the first watcher-backed invalidation read, not the first targeted graph/evidence context read, not the first context-pressure gauge, not the first durable investigation checkpoint packet, not the first bounded reorientation verdict, not the first thin launcher seam, and no longer the first explicit launch/interrupt authority surface over that seam. Those exist. Proposal now has the first bits of map memory, focus, and selection hygiene: it can auto-select a bounded evidence-backed observation cluster when ids are omitted, requires accepting recent evidence behind selected observations, prioritizes stronger selected observations for proposal wording, reuses existing architecture nodes by concrete code-ref/path/id checks before creating new path nodes, can rescue unanchored graph nodes through strict unique semantic overlap, follows graph links and incident edges into the frontier, and reports match-kind-aware churn pressure from the actual proposal shape. Distillation now also has the first source-output-aware teeth for tool/model summaries. Promotion now notices risky deltas instead of just checking the shape of the envelope, treats graph expansion as risky even if the patch tries to mumble "low pressure" while dragging a new wall into the blueprint, token-matches strong verifier kinds so `contest` cannot sneak past as `test`, and validates investigation checkpoints against known evidence before the packet can be inked. Successful update/promote writes now emit `thread/epiphany/stateUpdated` with `source: "update"` or `source: "promote"`, event-level revision, and typed changed fields so clients no longer need to poll blindly, infer the cause from timing, or diff the whole state to know which top-level sections moved. Runtime `agent_job_progress` background events now also emit `thread/epiphany/jobsUpdated` with changed bound-job payloads so clients no longer need to poll `thread/epiphany/jobs` just to watch one real owner move, the bound jobs now reflect launcher id, authority scope, and backend kind/job id instead of treating raw runtime pointers as the architecture, and the explicit `jobLaunch` / `jobInterrupt` surfaces can now create or tear down those bindings without pretending reflection is authority. The scene, jobs, freshness, context, pressure, and reorient surfaces give clients compact or bounded read-only views without making the GUI/client authoritative, scene/context surface the checkpoint packet back out without becoming writers, freshness surfaces watcher telemetry without becoming a hidden worker, and reorient now turns those signals into an explicit wakeup verdict. The remaining missing organs are a second backend if `agent_jobs` stops being enough, a runtime consumer that can act on the reorientation verdict without becoming a hidden tyrant, and eventually role-scoped specialist ownership without silently auto-promoting anything. ```text model/tool observations @@ -1322,7 +1331,7 @@ model/tool observations The typed write path exists, the first deterministic observation/evidence distillation path exists, source-output-aware distillation now summarizes noisy tool/model output into typed evidence with final result/error/finished lines ahead of generic warnings, the first deterministic map/churn proposal path exists, proposal-quality hardening now auto-selects bounded observation clusters when ids are omitted, requires accepting recent evidence, reuses existing graph nodes by observed code refs, prioritizes stronger selected observations, rescues unanchored graph nodes by strict semantic overlap, focuses linked frontier context, reports match-kind-aware map-delta pressure, and the first verifier-backed promotion gate exists. The first map/churn safety layer also exists: promotion can reject state replacement patches that are not tied to explicit observations/evidence, that contain structurally broken subgoal, invariant, graph, frontier, checkpoint, investigation-checkpoint, or churn fields, that try to promote risky churn without a warning and stronger verifier evidence, that claim a graph expansion is low-pressure to dodge the warning gate, or that rely on substring verifier-kind accidents. The first live state notification layer also exists: accepted writes publish updated typed state plus event-level revision and typed changed fields; rejected promotions stay quiet. -In natural language: current Epiphany can preserve a map, show a map, project a compact scene from the map, project derived job/progress slots from the map and retrieval summary, reflect a thin launcher seam over bound work, emit live bound-job progress notifications from runtime `agent_job_progress` events, project a read-only freshness lens over retrieval and graph staleness, layer watcher-backed invalidation telemetry onto that lens for loaded threads, project a targeted graph/evidence context shard from the map, project context pressure from token telemetry, bank an in-flight investigation packet in durable typed state, reflect that packet back into prompt/scene/context, turn those signals into a read-only resume-versus-regather verdict, retrieve evidence for the map, draft one explicit observation/evidence patch, derive a bounded graph/frontier/churn candidate from verified evidence-backed observations with code refs, choose a small coherent observation set when the caller does not provide one, favor the strongest selected observation when drafting that candidate, reject or accept a verified candidate, apply explicit map/evidence/churn edits, notify clients after successful writes, and sanity-check those edits before they hit the ledger. It still does not invalidate stale graph regions automatically, execute Epiphany-owned long-running jobs beyond the borrowed runtime `agent_jobs` backend, or coordinate specialist ownership. Teeth are installed; chewing is supervised; the first bell rings; the first wall board exists; the first shift board exists; the first freshness clipboard exists; the first door sensor now exists too; the first binder lens exists; the first pressure gauge exists; the first ember packet exists; the first verdict slip now exists too; the next slices are about safe waking and real ownership, not jawbones. +In natural language: current Epiphany can preserve a map, show a map, project a compact scene from the map, project derived job/progress slots from the map and retrieval summary, reflect a thin launcher seam over bound work, explicitly launch or interrupt that bound work through a bounded authority surface, emit live bound-job progress notifications from runtime `agent_job_progress` events, project a read-only freshness lens over retrieval and graph staleness, layer watcher-backed invalidation telemetry onto that lens for loaded threads, project a targeted graph/evidence context shard from the map, project context pressure from token telemetry, bank an in-flight investigation packet in durable typed state, reflect that packet back into prompt/scene/context, turn those signals into a read-only resume-versus-regather verdict, retrieve evidence for the map, draft one explicit observation/evidence patch, derive a bounded graph/frontier/churn candidate from verified evidence-backed observations with code refs, choose a small coherent observation set when the caller does not provide one, favor the strongest selected observation when drafting that candidate, reject or accept a verified candidate, apply explicit map/evidence/churn edits, notify clients after successful writes, and sanity-check those edits before they hit the ledger. It still does not invalidate stale graph regions automatically, execute Epiphany-owned long-running jobs beyond the borrowed runtime `agent_jobs` backend, or coordinate specialist ownership. Teeth are installed; chewing is supervised; the first bell rings; the first wall board exists; the first shift board exists; the first freshness clipboard exists; the first door sensor now exists too; the first binder lens exists; the first pressure gauge exists; the first ember packet exists; the first verdict slip now exists too; the first bounded launch lever and stop switch now exist too; the next slices are about safe waking and real ownership, not jawbones. ## Verification Hooks @@ -1348,6 +1357,7 @@ Current tests cover the landed flows at useful seams: - live app-server smoke for the first Phase 6 context boundary in `tools/epiphany_phase6_context_smoke.py`. - live app-server smoke for the first Phase 6 pressure boundary in `tools/epiphany_phase6_pressure_smoke.py`. - live app-server smoke for the first Phase 6 reorientation policy boundary in `tools/epiphany_phase6_reorient_smoke.py`. +- live app-server smoke for the first Phase 6 job-control boundary in `tools/epiphany_phase6_job_control_smoke.py`. Useful commands: @@ -1364,4 +1374,5 @@ $env:CARGO_TARGET_DIR='C:\Users\Meta\.cargo-target-codex'; cargo test -p codex-a & 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_phase6_context_smoke.py' & 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_phase6_pressure_smoke.py' & 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_phase6_reorient_smoke.py' +& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_phase6_job_control_smoke.py' ``` diff --git a/notes/epiphany-fork-implementation-plan.md b/notes/epiphany-fork-implementation-plan.md index 04905595..7d4f8c97 100644 --- a/notes/epiphany-fork-implementation-plan.md +++ b/notes/epiphany-fork-implementation-plan.md @@ -51,6 +51,7 @@ The landed machine now has: - read-only map/churn proposal through `thread/epiphany/propose` - verifier-backed promotion through `thread/epiphany/promote` - successful-write notification through `thread/epiphany/stateUpdated` +- explicit launch/interrupt authority through `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt`, still backed by runtime `agent_jobs` - thin Epiphany-owned launcher metadata in durable `jobBindings`, with launcher id, authority scope, and backend kind/job id - live bound-runtime progress notification through `thread/epiphany/jobsUpdated` - response-level and notification-level revision and changed-field metadata @@ -71,6 +72,7 @@ The landed machine now has: - durable Phase 6 investigation checkpointing in authoritative typed state, prompt rendering, and scene/context reflection - read-only Phase 6 CRRC reorientation policy through `thread/epiphany/reorient` - live Phase 6 reorientation app-server smoke coverage in `tools/epiphany_phase6_reorient_smoke.py` +- live Phase 6 job-control app-server smoke coverage in `tools/epiphany_phase6_job_control_smoke.py` The current phase is Phase 6: reflection boundary and observable harness state. @@ -81,11 +83,12 @@ These boundaries are more important than the individual method names: - `thread/epiphany/retrieve` is read-only. - `thread/epiphany/distill` is read-only. - `thread/epiphany/propose` is read-only. -- Durable typed state writes go through `thread/epiphany/update` or accepted `thread/epiphany/promote`. +- Durable typed state writes go through `thread/epiphany/update`, accepted `thread/epiphany/promote`, or the bounded `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt` authority surfaces when they mutate `jobBindings`. - `thread/epiphany/index` may update the semantic retrieval catalog, but it is not a hidden Epiphany-state writer. - `thread/epiphany/scene` is a client reflection, not a second source of truth. - `thread/epiphany/jobs` is a derived reflection over retrieval summaries plus typed launcher bindings and optional backend snapshots, not a scheduler or durable runtime job store. - `thread/epiphany/jobsUpdated` is a live notification derived from runtime progress events and launcher-bound job snapshots, not a scheduler, polling daemon, or durable runtime job store. +- `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt` are bounded authority surfaces over durable `jobBindings` plus the current backend adapter, not a hidden scheduler, queue, or second runtime. - `thread/epiphany/freshness` is a derived reflection, not automatic watcher-driven invalidation, a mutation gate, or a hidden refresh scheduler. - `thread/epiphany/context` is a targeted reflection, not a state writer or hidden proposal engine. - `thread/epiphany/pressure` is a context-pressure reflection, not an automatic compactor, scheduler, or CRRC coordinator. @@ -127,9 +130,10 @@ and notify typed state. It can. The next unknowns are: - how the landed thin launcher seam should expose explicit launch and interrupt authority without becoming a secret scheduler +- how the landed read-only reorientation verdict should be consumed by runtime without turning CRRC into ceremony machinery - how the landed watcher-backed invalidation telemetry should be consumed without turning freshness into a secret worker -- how the landed reorientation verdict should be consumed by future automatic CRRC without becoming ceremony machinery - how much automatic CRRC coordination belongs in runtime once pressure, freshness, watcher, checkpoint, and reorientation signals all exist +- whether the first bounded CRRC runtime move should reuse the landed `agent_jobs` backend through the explicit job-control seam or wait for a second backend - what Phase 6 should prove before specialist scheduling begins ## Phase 6 Direction @@ -138,8 +142,8 @@ Phase 6 should grow observable harness state outward from the typed spine. Useful candidates: -1. Add an explicit launch/interrupt authority surface over the landed thin launcher seam while keeping `agent_jobs` as the first backend adapter. -2. Decide how the landed reorientation verdict should be consumed by future automatic CRRC without becoming ceremony machinery. +1. Decide how the landed read-only reorientation verdict should be consumed by runtime without turning CRRC into ceremony machinery. +2. Decide how much automatic CRRC coordination belongs now that pressure, freshness, watcher, checkpoint, reorient, and explicit job control all exist. 3. Add targeted scene/jobs/context/pressure/reorient fields only when a client or smoke exposes a real gap. Do not spend Phase 6 polishing Phase 5 out of anxiety. The Phase 5 smoke harness diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index c751765b..ba372601 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -28,7 +28,7 @@ Do not trust this file for the exact live HEAD. Always check git. - Do not copy exact branch or HEAD from this note. Run `git status --short --branch` and `git log --oneline -5`. - Phase 1 through Phase 5 are complete enough. -- Phase 6 has read-only `thread/epiphany/scene`, `thread/epiphany/jobs`, `thread/epiphany/freshness`, `thread/epiphany/context`, `thread/epiphany/pressure`, and `thread/epiphany/reorient`; durable `jobBindings` now act as a thin Epiphany-owned launcher seam with launcher id, authority scope, and backend kind/job id, jobs can overlay that seam onto live runtime `agent_jobs` progress for loaded threads, and `thread/epiphany/jobsUpdated` now translates live `agent_job_progress:{json}` background events into changed launcher-bound notifications without polling or scheduling. Freshness carries watcher-backed invalidation inputs, and reorient turns checkpoint plus freshness/pressure/watcher signals into a read-only resume-versus-regather verdict. +- Phase 6 has read-only `thread/epiphany/scene`, `thread/epiphany/jobs`, `thread/epiphany/freshness`, `thread/epiphany/context`, `thread/epiphany/pressure`, and `thread/epiphany/reorient`; durable `jobBindings` now act as a thin Epiphany-owned launcher seam with launcher id, authority scope, and backend kind/job id, `thread/epiphany/jobLaunch` / `thread/epiphany/jobInterrupt` now provide explicit bounded authority over that seam, jobs can overlay it onto live runtime `agent_jobs` progress for loaded threads, and `thread/epiphany/jobsUpdated` now translates live `agent_job_progress:{json}` background events into changed launcher-bound notifications without polling or scheduling. Freshness carries watcher-backed invalidation inputs, and reorient turns checkpoint plus freshness/pressure/watcher signals into a read-only resume-versus-regather verdict. - Durable in-flight investigation checkpointing is now landed in authoritative typed state, writable through `thread/epiphany/update` or accepted `thread/epiphany/promote`, rendered into the prompt, and reflected through scene/context. - The repo is an Epiphany fork of Codex, not a Codex preset. - `vendor/codex` is tracked directly, not a submodule. @@ -62,6 +62,7 @@ The current spine: - read-only map/churn proposal through `thread/epiphany/propose` - verifier-backed promotion through `thread/epiphany/promote` - successful-write notification through `thread/epiphany/stateUpdated` +- explicit launch/interrupt authority through `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt` - thin launcher metadata in durable `jobBindings` - live bound-job progress notification through `thread/epiphany/jobsUpdated` - read-only compact reflection through `thread/epiphany/scene` @@ -78,6 +79,7 @@ The current spine: - live context app-server smoke through `tools/epiphany_phase6_context_smoke.py` - live pressure app-server smoke through `tools/epiphany_phase6_pressure_smoke.py` - live reorientation app-server smoke through `tools/epiphany_phase6_reorient_smoke.py` +- live job-control app-server smoke through `tools/epiphany_phase6_job_control_smoke.py` The exact current control flow is documented in `notes/epiphany-current-algorithmic-map.md`. @@ -93,7 +95,7 @@ The exact current control flow is documented in - `thread/epiphany/context` is read-only. - `thread/epiphany/pressure` is read-only. - `thread/epiphany/reorient` is read-only. -- Durable typed state writes go through `thread/epiphany/update` or accepted `thread/epiphany/promote`. +- Durable typed state writes go through `thread/epiphany/update`, accepted `thread/epiphany/promote`, or the bounded `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt` authority surfaces when they mutate `jobBindings`. - `thread/epiphany/index` writes the retrieval catalog, not durable Epiphany understanding. - GUI/client surfaces reflect and steer typed state; they do not become the source of truth. - Do not restart Phase 5 hardening without a concrete regression. @@ -118,6 +120,12 @@ For jobs reflection behavior changes, run: & 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_phase6_jobs_smoke.py' ``` +For launch/interrupt authority changes over the thin job seam, run: + +```powershell +& 'C:\Users\Meta\.cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe' '.\tools\epiphany_phase6_job_control_smoke.py' +``` + For freshness reflection behavior changes, run: ```powershell @@ -214,9 +222,10 @@ automatic CRRC. Runtime coordination and next-action execution are still future work even though read-only job ownership/progress reflection now has a real runtime seam and a thin launcher boundary. -When the user asks to continue, the next likely organ is an explicit -launch/interrupt authority surface over the landed thin launcher seam, still -backed by runtime `agent_jobs` until a second backend deserves to exist. +When the user asks to continue, the next likely organ is the first bounded +runtime consumer for the landed read-only `thread/epiphany/reorient` verdict, +using the now-explicit job-control seam without turning CRRC into a hidden +scheduler. Live `thread/epiphany/scene`, `thread/epiphany/jobs`, `thread/epiphany/freshness`, `thread/epiphany/context`, @@ -230,7 +239,6 @@ guardrails, not the next organs. - specialist-agent scheduling - GUI-as-source-of-truth - automatic runtime CRRC coordinator acting on the landed reorientation verdict -- explicit launch/interrupt authority surface over the landed thin launcher seam - Epiphany-owned long-running job execution beyond the current runtime `agent_jobs` seam - broad event stream beyond the landed state update notification diff --git a/state/evidence.jsonl b/state/evidence.jsonl index eb0a5385..1dd1afe2 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -46,3 +46,4 @@ {"ts": "2026-04-26T14:01:57+00:00", "type": "verification", "status": "ok", "note": "Phase 6 live bound-job progress notification landed as experimental thread/epiphany/jobsUpdated. App-server now translates runtime agent_job_progress:{json} background events into changed bound-job payloads by resolving durable jobBindings against live agent_jobs snapshots, suppressing duplicate unchanged payloads without polling, state mutation, or scheduling. Focused app-server notification coverage, schema fixture checks, and tools/epiphany_phase6_jobs_smoke.py passed."} {"ts": "2026-04-26T14:57:54+00:00", "type": "design", "status": "accepted", "note": "Durable jobBindings now define a thin Epiphany-owned launcher seam: launcher identity, authority scope, backend kind, and backend job id belong to Epiphany even while the first backend adapter still targets runtime agent_jobs. This keeps job ownership and authority explicit without prematurely building a second scheduler or runtime."} {"ts": "2026-04-26T14:57:54+00:00", "type": "verification", "status": "ok", "note": "Phase 6 thin launcher seam landed in durable jobBindings and thread/epiphany/jobs reflection. Jobs and jobsUpdated now surface launcherJobId, authorityScope, backendKind, and backendJobId while the current adapter still resolves runtime agent_jobs progress. Focused codex-core epiphany_state_update tests, app-server-protocol thread_epiphany and schema fixture tests, app-server mapper plus jobsUpdated notification tests, cargo build -p codex-app-server, and tools/epiphany_phase6_jobs_smoke.py passed."} +{"ts": "2026-04-26T15:55:45+00:00", "type": "verification", "status": "ok", "note": "Phase 6 explicit thread/epiphany/jobLaunch and thread/epiphany/jobInterrupt authority landed over the thin launcher seam. The control path now falls back to a direct sqlite state runtime for loaded ephemeral threads, launches or cancels the current agent_jobs backend without turning jobs reflection into a scheduler, and preserves a blocked specialist slot with explicit relaunch reason after interrupt. Focused codex-core epiphany_state_update tests, app-server mapper plus jobsUpdated notification tests, app-server-protocol thread_epiphany and schema fixture checks, cargo build -p codex-app-server, and tools/epiphany_phase6_job_control_smoke.py plus tools/epiphany_phase6_jobs_smoke.py and tools/epiphany_phase6_scene_smoke.py passed."} diff --git a/state/map.yaml b/state/map.yaml index bafb0096..c335d701 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -62,7 +62,7 @@ accepted_design: - Authoritative Epiphany state lives in Codex protocol/core session and rollout state. - The prompt reads typed state through a bounded `` developer fragment. - Clients read typed state through `Thread.epiphanyState`, compact reflections such as `thread/epiphany/scene`, `thread/epiphany/jobs`, and `thread/epiphany/freshness`, targeted state shards through `thread/epiphany/context`, runtime pressure through `thread/epiphany/pressure`, bounded CRRC reorientation verdicts through `thread/epiphany/reorient`, and live bound-job progress through `thread/epiphany/jobsUpdated`. - - Durable typed state writes route through `thread/epiphany/update` or accepted `thread/epiphany/promote`. + - Durable typed state writes route through `thread/epiphany/update`, accepted `thread/epiphany/promote`, or the bounded `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt` authority surfaces when they mutate `job_bindings`. - `thread/epiphany/retrieve`, `thread/epiphany/distill`, `thread/epiphany/propose`, `thread/epiphany/scene`, `thread/epiphany/jobs`, `thread/epiphany/freshness`, `thread/epiphany/context`, `thread/epiphany/pressure`, and `thread/epiphany/reorient` stay read-only. - `thread/epiphany/index` writes the retrieval catalog, not durable Epiphany understanding. - Qdrant is the preferred persistent semantic backend; BM25 remains the bootstrap/fallback/control path. @@ -82,7 +82,7 @@ accepted_design: - `thread/epiphany/reorient` is the first bounded CRRC policy surface: it derives a read-only `resume` versus `regather` verdict from checkpoint, freshness, watcher, and pressure signals without mutating state, compacting, scheduling, or silently continuing work. - Epiphany safety doctrine is to separate cognition from actuation, keep authority explicit, make interruption sacred, stage capability release, and treat operator misuse as a normal design input rather than a rude hypothetical. - Epiphany is built in the open, but publication mode is not one-size-fits-all: use FOSS where viable, source-available licensing where economics or capability risk demand it, and commercial terms where organizations extract real enterprise value. - - Explicit launch/interrupt authority over the thin job seam, Epiphany-owned long-running job execution beyond the current runtime `agent_jobs` backend, and specialist scheduling are still future work. + - `thread/epiphany/jobLaunch` and `thread/epiphany/jobInterrupt` are the first bounded authority surfaces over the thin job seam: they mutate durable `job_bindings`, create or cancel the current `agent_jobs` backend binding, and preserve explicit launcher identity and interruption without turning `thread/epiphany/jobs` into a scheduler. - Handoff, plan, harness-surface, and scratch files must have distinct jobs and must not become duplicate brains. - A machine is perfect when every remaining part visibly deserves to exist; this applies to persistent memory too. - Evidence is not an activity feed; repeated "I just did this" entries should be cut or distilled when git already proves the work. @@ -97,11 +97,12 @@ landed_baseline: - Phase 3: typed client read surface through `Thread.epiphanyState`. - Phase 4: hybrid retrieval plus explicit Qdrant indexing with BM25 fallback. - Phase 5: typed update/distill/propose/promote/read/notification control plane. - - Phase 6: read-only scene, job/progress, freshness with watcher-backed invalidation inputs, targeted context, context-pressure, and reorientation policy surfaces, plus durable investigation checkpointing for compaction-safe planning, live bound-job progress notifications, and a thin launcher seam in durable job bindings. + - Phase 6: read-only scene, job/progress, freshness with watcher-backed invalidation inputs, targeted context, context-pressure, and reorientation policy surfaces, plus durable investigation checkpointing for compaction-safe planning, live bound-job progress notifications, a thin launcher seam in durable job bindings, and explicit launch/interrupt authority over that seam. guardrails: - Use `tools/epiphany_phase5_smoke.py` before modifying update, distill, propose, promote, read-projection, or notification behavior. - Use `tools/epiphany_phase6_scene_smoke.py` before modifying scene projection behavior. - Use `tools/epiphany_phase6_jobs_smoke.py` before modifying job/progress reflection behavior. + - Use `tools/epiphany_phase6_job_control_smoke.py` before modifying launch/interrupt authority over the thin job seam. - Use `tools/epiphany_phase6_freshness_smoke.py` before modifying freshness reflection behavior. - Use `tools/epiphany_phase6_invalidation_smoke.py` before modifying watcher-backed invalidation behavior inside freshness reflection. - Use `tools/epiphany_phase6_context_smoke.py` before modifying targeted context-shard behavior. @@ -125,5 +126,5 @@ rejected_paths: reason: persistent state that accumulates every local truth becomes memory cruft and weakens rehydration current_status: - summary: Phase 1 durable state, Phase 2 prompt integration, Phase 3 typed read, Phase 4 retrieval/indexing, Phase 5 update/distill/propose/promote/read/notification, and Phase 6 read-only scene, jobs, freshness, targeted context, context-pressure reflection, watcher-backed invalidation inputs, durable investigation checkpointing, bounded CRRC reorientation policy, live bound-job progress notifications, and a thin launcher seam in durable `job_bindings` are landed. `job_bindings` now carry Epiphany-owned launcher metadata such as launcher id, authority scope, and backend kind/job id, while still tolerating the older runtime-agent-job pointer as a compatibility alias. `thread/epiphany/jobs` overlays that launcher seam onto real runtime `agent_jobs` progress for bound work, including runtime/backend job id, worker thread ids, and progress counts, without becoming a scheduler. `thread/epiphany/jobsUpdated` now translates runtime `agent_job_progress:{json}` background events into changed launcher-bound payloads only when the mapped job state actually changes, so clients can watch real owners move without polling the jobs read surface. `thread/epiphany/reorient` still consumes checkpoint, freshness, watcher, and pressure signals to return a read-only `resume` versus `regather` verdict with explicit checkpoint-path/frontier reasons, but it does not mutate state, compact, schedule, notify, or silently continue work. Focused core/protocol/app-server tests, schema fixture regeneration/checks, app-server build, and the live jobs smoke cover the seam. Investigation checkpoints still live in authoritative typed state, flow through the prompt, and reflect through scene/context without adding a second writer or fake scheduler. Persistent state was cut down so map, handoff, plan, harness surfaces, scratch, and evidence each carry one job instead of becoming a Jenga tower; evidence is now a distilled ledger rather than an activity feed. The global AGENTS Epiphany core command says persistent state is the agent's mind and explicitly permits the agent to ask for self-modification when memory or workflow makes it worse. The continuity goal is to bank the fire before the dark so compaction becomes less like death and more like sleep. - next_action: After compaction, rehydrate and reorient. If the user asks to continue, the next likely Phase 6 organ is an explicit launch/interrupt authority surface over the landed thin launcher seam, still backed by runtime `agent_jobs` until a second backend deserves to exist. Automatic runtime CRRC remains future work that may eventually consume the landed read-only reorientation verdict. Do not restart the completed pressure, freshness, watcher-invalidation, investigation-checkpoint, reorientation, jobs-binding, jobsUpdated, or thin-launcher-seam slice unless a concrete regression appears. + summary: Phase 1 durable state, Phase 2 prompt integration, Phase 3 typed read, Phase 4 retrieval/indexing, Phase 5 update/distill/propose/promote/read/notification, and Phase 6 read-only scene, jobs, freshness, targeted context, context-pressure reflection, watcher-backed invalidation inputs, durable investigation checkpointing, bounded CRRC reorientation policy, live bound-job progress notifications, a thin launcher seam in durable `job_bindings`, and explicit `thread/epiphany/jobLaunch` / `thread/epiphany/jobInterrupt` authority over that seam are landed. `job_bindings` now carry Epiphany-owned launcher metadata such as launcher id, authority scope, and backend kind/job id, while still tolerating the older runtime-agent-job pointer as a compatibility alias. `thread/epiphany/jobs` overlays that launcher seam onto real runtime `agent_jobs` progress for bound work, including runtime/backend job id, worker thread ids, and progress counts, without becoming a scheduler. `thread/epiphany/jobsUpdated` now translates runtime `agent_job_progress:{json}` background events into changed launcher-bound payloads only when the mapped job state actually changes, so clients can watch real owners move without polling the jobs read surface. The launch/interrupt control path now uses direct state-runtime fallback when a loaded ephemeral thread does not already carry a live sqlite handle, so authoritative bindings and runtime snapshots stay aligned instead of pretending the backend vanished. `thread/epiphany/reorient` still consumes checkpoint, freshness, watcher, and pressure signals to return a read-only `resume` versus `regather` verdict with explicit checkpoint-path/frontier reasons, but it does not mutate state, compact, schedule, notify, or silently continue work. Focused core/protocol/app-server tests, schema fixture regeneration/checks, app-server build, live jobs/job-control/scene smokes, and the targeted jobsUpdated notification test cover the seam. Investigation checkpoints still live in authoritative typed state, flow through the prompt, and reflect through scene/context without adding a second writer or fake scheduler. Persistent state was cut down so map, handoff, plan, harness surfaces, scratch, and evidence each carry one job instead of becoming a Jenga tower; evidence is now a distilled ledger rather than an activity feed. The global AGENTS Epiphany core command says persistent state is the agent's mind and explicitly permits the agent to ask for self-modification when memory or workflow makes it worse. The continuity goal is to bank the fire before the dark so compaction becomes less like death and more like sleep. + next_action: After compaction, rehydrate and reorient. If the user asks to continue, the next likely organ is the first bounded runtime consumer for the landed read-only `thread/epiphany/reorient` verdict, using the now-explicit job-control seam without turning CRRC into a hidden scheduler. Automatic runtime CRRC remains future work; do not restart the completed pressure, freshness, watcher-invalidation, investigation-checkpoint, reorientation, jobs-binding, jobsUpdated, thin-launcher-seam, or job-control slice unless a concrete regression appears. diff --git a/tools/epiphany_phase6_job_control_smoke.py b/tools/epiphany_phase6_job_control_smoke.py new file mode 100644 index 00000000..f805567f --- /dev/null +++ b/tools/epiphany_phase6_job_control_smoke.py @@ -0,0 +1,378 @@ +from __future__ import annotations + +import argparse +import json +from pathlib import Path +import time +from typing import Any + +from epiphany_phase5_smoke import AppServerClient +from epiphany_phase5_smoke import DEFAULT_APP_SERVER +from epiphany_phase5_smoke import ROOT +from epiphany_phase5_smoke import require +from epiphany_phase5_smoke import reset_smoke_paths + + +DEFAULT_CODEX_HOME = ROOT / ".epiphany-smoke" / "phase6-job-control-codex-home" +DEFAULT_RESULT = ROOT / ".epiphany-smoke" / "phase6-job-control-smoke-result.json" +DEFAULT_TRANSCRIPT = ROOT / ".epiphany-smoke" / "phase6-job-control-smoke-transcript.jsonl" +DEFAULT_STDERR = ROOT / ".epiphany-smoke" / "phase6-job-control-smoke-server.stderr.log" + +BINDING_ID = "specialist-work" +LINKED_SUBGOAL_ID = "phase6-job-control" +LINKED_GRAPH_NODE_ID = "job-control-surface" +OWNER_ROLE = "epiphany-harness" +AUTHORITY_SCOPE = "epiphany.specialist" +JOB_SCOPE = "role-scoped specialist work" + + +def job_by_id(jobs: list[dict[str, Any]], job_id: str) -> dict[str, Any]: + for job in jobs: + if job["id"] == job_id: + return job + raise AssertionError(f"missing job {job_id!r}: {jobs!r}") + + +def assert_bound_specialist_job(job: dict[str, Any]) -> None: + require(job["id"] == BINDING_ID, "launched job should preserve binding id") + require(job["kind"] == "specialist", "launched job should remain specialist kind") + require(job["scope"] == JOB_SCOPE, "launched job should preserve scope") + require(job["ownerRole"] == OWNER_ROLE, "launched job should preserve owner role") + require( + job["authorityScope"] == AUTHORITY_SCOPE, + "launched job should surface authority scope", + ) + require( + isinstance(job.get("launcherJobId"), str) + and job["launcherJobId"].startswith("epiphany-launch-"), + "launched job should surface an Epiphany launcher id", + ) + require( + job["backendKind"] == "agentJobs", + "launched job should resolve through the agent_jobs backend", + ) + require( + isinstance(job.get("backendJobId"), str) and job["backendJobId"], + "launched job should expose a backend job id", + ) + require( + job["runtimeAgentJobId"] == job["backendJobId"], + "launched job should keep runtime and backend job ids aligned", + ) + require( + job["linkedSubgoalIds"] == [LINKED_SUBGOAL_ID], + "launched job should preserve linked subgoal ids", + ) + require( + job["linkedGraphNodeIds"] == [LINKED_GRAPH_NODE_ID], + "launched job should preserve linked graph node ids", + ) + require( + job["status"] in {"pending", "running", "completed", "failed", "cancelled"}, + "launched job should bind to a real backend status", + ) + + +def assert_interrupted_specialist_job(job: dict[str, Any]) -> None: + require(job["id"] == BINDING_ID, "interrupted job should preserve binding id") + require(job["kind"] == "specialist", "interrupted job should remain specialist kind") + require(job["scope"] == JOB_SCOPE, "interrupted job should preserve scope") + require(job["ownerRole"] == OWNER_ROLE, "interrupted job should preserve owner role") + require( + job["authorityScope"] == AUTHORITY_SCOPE, + "interrupted job should preserve authority scope", + ) + require(job["status"] == "blocked", "interrupt should leave the slot explicitly blocked") + require( + job.get("launcherJobId") is None, + "interrupt should clear launcher identity from the specialist slot", + ) + require( + job.get("backendKind") is None and job.get("backendJobId") is None, + "interrupt should clear backend identity from the specialist slot", + ) + require( + job.get("runtimeAgentJobId") is None, + "interrupt should clear runtime job identity from the specialist slot", + ) + require( + isinstance(job.get("blockingReason"), str) + and "launch explicitly" in job["blockingReason"].lower(), + "interrupt should leave a bounded relaunch reason behind", + ) + + +def wait_for_jobs_surface( + client: AppServerClient, + thread_id: str, + *, + require_bound_backend: bool, + timeout: float = 20.0, +) -> dict[str, Any]: + deadline = time.time() + timeout + while time.time() < deadline: + response = client.send("thread/epiphany/jobs", {"threadId": thread_id}) + assert response is not None + job = job_by_id(response["jobs"], BINDING_ID) + if require_bound_backend and job.get("backendJobId"): + return response + if not require_bound_backend and job.get("backendJobId") is None: + return response + time.sleep(0.2) + raise TimeoutError("timed out waiting for expected Epiphany jobs surface state") + + +def run_smoke(args: argparse.Namespace) -> dict[str, Any]: + app_server = args.app_server.resolve() + if not app_server.exists(): + raise FileNotFoundError(f"codex app-server binary not found: {app_server}") + + codex_home = args.codex_home.resolve() + result_path = args.result.resolve() + transcript_path = args.transcript.resolve() + stderr_path = args.stderr.resolve() + reset_smoke_paths(codex_home, result_path, transcript_path, stderr_path) + + with AppServerClient(app_server, codex_home, transcript_path, stderr_path) as client: + client.send( + "initialize", + { + "clientInfo": { + "name": "epiphany-phase6-job-control-smoke", + "title": "Epiphany Phase 6 Job Control Smoke", + "version": "0.1.0", + }, + "capabilities": {"experimentalApi": True}, + }, + ) + client.send("initialized", expect_response=False) + started = client.send( + "thread/start", + {"cwd": str(ROOT / "epiphany-core"), "ephemeral": True}, + ) + assert started is not None + thread_id = started["thread"]["id"] + + launch_notification_start = len(client.notifications) + launch = client.send( + "thread/epiphany/jobLaunch", + { + "threadId": thread_id, + "expectedRevision": 0, + "bindingId": BINDING_ID, + "kind": "specialist", + "scope": JOB_SCOPE, + "ownerRole": OWNER_ROLE, + "authorityScope": AUTHORITY_SCOPE, + "linkedSubgoalIds": [LINKED_SUBGOAL_ID], + "linkedGraphNodeIds": [LINKED_GRAPH_NODE_ID], + "instruction": ( + "Acknowledge the smoke row and report a compact JSON object result for it." + ), + "inputJson": { + "task": "phase6-job-control-smoke", + "goal": "exercise explicit launch and interrupt authority", + }, + "outputSchemaJson": { + "type": "object", + "properties": { + "ack": {"type": "boolean"}, + "task": {"type": "string"}, + }, + "required": ["ack", "task"], + "additionalProperties": True, + }, + "maxRuntimeSeconds": 60, + }, + ) + assert launch is not None + require(launch["revision"] == 1, "launch should create revision 1 from missing state") + require( + launch["changedFields"] == ["jobBindings"], + "launch should only mutate job bindings", + ) + require( + launch["epiphanyState"]["revision"] == 1, + "launch response should return the persisted state revision", + ) + assert_bound_specialist_job(launch["job"]) + + launch_notification = client.wait_for_notification( + "thread/epiphany/stateUpdated", + start_index=launch_notification_start, + timeout=15.0, + ) + require( + launch_notification["params"]["threadId"] == thread_id, + "launch notification should identify the thread", + ) + require( + launch_notification["params"]["source"] == "jobLaunch", + "launch notification should report the launch source", + ) + require( + launch_notification["params"]["revision"] == 1, + "launch notification should expose revision 1", + ) + require( + launch_notification["params"]["changedFields"] == ["jobBindings"], + "launch notification should identify the job binding mutation", + ) + + jobs_updated = client.wait_for_notification( + "thread/epiphany/jobsUpdated", + start_index=launch_notification_start, + timeout=20.0, + ) + require( + jobs_updated["params"]["threadId"] == thread_id, + "jobsUpdated should identify the thread", + ) + require( + jobs_updated["params"]["source"] == "runtimeProgress", + "jobsUpdated should identify runtime progress", + ) + require( + jobs_updated["params"]["stateRevision"] == 1, + "jobsUpdated should preserve the launched state revision", + ) + updated_job = job_by_id(jobs_updated["params"]["jobs"], BINDING_ID) + assert_bound_specialist_job(updated_job) + + ready_jobs = wait_for_jobs_surface( + client, + thread_id, + require_bound_backend=True, + ) + require( + ready_jobs["source"] == "live", + "jobs surface should resolve live state for the loaded thread", + ) + require( + ready_jobs["stateRevision"] == 1, + "jobs surface should not invent a new revision after launch", + ) + ready_job = job_by_id(ready_jobs["jobs"], BINDING_ID) + assert_bound_specialist_job(ready_job) + + interrupt_notification_start = len(client.notifications) + interrupt = client.send( + "thread/epiphany/jobInterrupt", + { + "threadId": thread_id, + "expectedRevision": 1, + "bindingId": BINDING_ID, + "reason": "Phase 6 job control smoke requested a clean interrupt.", + }, + ) + assert interrupt is not None + require( + interrupt["revision"] == 2, + "interrupt should advance the durable state revision", + ) + require( + interrupt["changedFields"] == ["jobBindings"], + "interrupt should only mutate job bindings", + ) + require( + interrupt["epiphanyState"]["revision"] == 2, + "interrupt response should return the new persisted state revision", + ) + require( + isinstance(interrupt["cancelRequested"], bool), + "interrupt should report whether runtime cancellation was accepted", + ) + require( + isinstance(interrupt["interruptedThreadIds"], list), + "interrupt should expose any interrupted worker thread ids", + ) + assert_interrupted_specialist_job(interrupt["job"]) + + interrupt_notification = client.wait_for_notification( + "thread/epiphany/stateUpdated", + start_index=interrupt_notification_start, + timeout=15.0, + ) + require( + interrupt_notification["params"]["threadId"] == thread_id, + "interrupt notification should identify the thread", + ) + require( + interrupt_notification["params"]["source"] == "jobInterrupt", + "interrupt notification should report the interrupt source", + ) + require( + interrupt_notification["params"]["revision"] == 2, + "interrupt notification should expose revision 2", + ) + require( + interrupt_notification["params"]["changedFields"] == ["jobBindings"], + "interrupt notification should identify the job binding mutation", + ) + + interrupted_jobs = wait_for_jobs_surface( + client, + thread_id, + require_bound_backend=False, + ) + require( + interrupted_jobs["stateRevision"] == 2, + "jobs surface should reflect the interrupted state revision", + ) + interrupted_job = job_by_id(interrupted_jobs["jobs"], BINDING_ID) + assert_interrupted_specialist_job(interrupted_job) + + final_read = client.send("thread/read", {"threadId": thread_id, "includeTurns": False}) + assert final_read is not None + require( + final_read["thread"]["epiphanyState"]["revision"] == 2, + "thread/read should reflect the interrupted revision", + ) + + result = { + "threadId": thread_id, + "codexHome": str(codex_home), + "launchRevision": launch["revision"], + "launchStatus": launch["job"]["status"], + "launchBackendJobId": launch["job"]["backendJobId"], + "jobsUpdatedStatus": updated_job["status"], + "jobsUpdatedBackendJobId": updated_job["backendJobId"], + "readyJobsStatus": ready_job["status"], + "readyJobsBackendJobId": ready_job["backendJobId"], + "interruptRevision": interrupt["revision"], + "interruptCancelRequested": interrupt["cancelRequested"], + "interruptInterruptedThreadIds": interrupt["interruptedThreadIds"], + "interruptedStatus": interrupted_job["status"], + "interruptedBlockingReason": interrupted_job["blockingReason"], + "jobsUpdatedNotificationCount": client.count_notifications( + "thread/epiphany/jobsUpdated", + start_index=launch_notification_start, + ), + "finalReadRevision": final_read["thread"]["epiphanyState"]["revision"], + } + + result_path.parent.mkdir(parents=True, exist_ok=True) + result_path.write_text( + json.dumps(result, indent=2, ensure_ascii=False) + "\n", + encoding="utf-8", + ) + return result + + +def main() -> int: + parser = argparse.ArgumentParser( + description="Live-smoke the Phase 6 Epiphany job launch/interrupt control surface." + ) + parser.add_argument("--app-server", type=Path, default=DEFAULT_APP_SERVER) + parser.add_argument("--codex-home", type=Path, default=DEFAULT_CODEX_HOME) + parser.add_argument("--result", type=Path, default=DEFAULT_RESULT) + parser.add_argument("--transcript", type=Path, default=DEFAULT_TRANSCRIPT) + parser.add_argument("--stderr", type=Path, default=DEFAULT_STDERR) + args = parser.parse_args() + result = run_smoke(args) + print(json.dumps(result, indent=2, ensure_ascii=False)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/epiphany_phase6_scene_smoke.py b/tools/epiphany_phase6_scene_smoke.py index ea9b3346..7e79ab3c 100644 --- a/tools/epiphany_phase6_scene_smoke.py +++ b/tools/epiphany_phase6_scene_smoke.py @@ -150,6 +150,7 @@ def assert_missing_scene(scene: dict[str, Any]) -> None: "distill", "context", "jobs", + "jobLaunch", "freshness", "pressure", "reorient", @@ -239,10 +240,12 @@ def assert_ready_scene(scene: dict[str, Any], expected_revision: int) -> None: "distill", "context", "jobs", + "jobLaunch", "freshness", "pressure", "reorient", "update", + "jobInterrupt", "propose", "promote", ], diff --git a/vendor/codex/codex-rs/app-server-protocol/schema/json/ServerNotification.json b/vendor/codex/codex-rs/app-server-protocol/schema/json/ServerNotification.json index ab2315d5..56150530 100644 --- a/vendor/codex/codex-rs/app-server-protocol/schema/json/ServerNotification.json +++ b/vendor/codex/codex-rs/app-server-protocol/schema/json/ServerNotification.json @@ -4122,7 +4122,9 @@ "ThreadEpiphanyStateUpdatedSource": { "enum": [ "update", - "promote" + "promote", + "jobLaunch", + "jobInterrupt" ], "type": "string" }, diff --git a/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json b/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json index 5ffe61bf..9a056840 100644 --- a/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json +++ b/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.schemas.json @@ -15980,6 +15980,8 @@ "distill", "context", "jobs", + "jobLaunch", + "jobInterrupt", "freshness", "pressure", "reorient", @@ -16379,7 +16381,9 @@ "ThreadEpiphanyStateUpdatedSource": { "enum": [ "update", - "promote" + "promote", + "jobLaunch", + "jobInterrupt" ], "type": "string" }, diff --git a/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json b/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json index f1833095..6d685ea3 100644 --- a/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json +++ b/vendor/codex/codex-rs/app-server-protocol/schema/json/codex_app_server_protocol.v2.schemas.json @@ -13867,6 +13867,8 @@ "distill", "context", "jobs", + "jobLaunch", + "jobInterrupt", "freshness", "pressure", "reorient", @@ -14266,7 +14268,9 @@ "ThreadEpiphanyStateUpdatedSource": { "enum": [ "update", - "promote" + "promote", + "jobLaunch", + "jobInterrupt" ], "type": "string" }, diff --git a/vendor/codex/codex-rs/app-server-protocol/schema/json/v2/ThreadEpiphanyStateUpdatedNotification.json b/vendor/codex/codex-rs/app-server-protocol/schema/json/v2/ThreadEpiphanyStateUpdatedNotification.json index 680ee138..bc1276fc 100644 --- a/vendor/codex/codex-rs/app-server-protocol/schema/json/v2/ThreadEpiphanyStateUpdatedNotification.json +++ b/vendor/codex/codex-rs/app-server-protocol/schema/json/v2/ThreadEpiphanyStateUpdatedNotification.json @@ -905,7 +905,9 @@ "ThreadEpiphanyStateUpdatedSource": { "enum": [ "update", - "promote" + "promote", + "jobLaunch", + "jobInterrupt" ], "type": "string" } diff --git a/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanySceneAction.ts b/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanySceneAction.ts index a9aeec37..c1b3c0d3 100644 --- a/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanySceneAction.ts +++ b/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanySceneAction.ts @@ -2,4 +2,4 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -export type ThreadEpiphanySceneAction = "index" | "retrieve" | "distill" | "context" | "jobs" | "freshness" | "pressure" | "reorient" | "propose" | "promote" | "update"; +export type ThreadEpiphanySceneAction = "index" | "retrieve" | "distill" | "context" | "jobs" | "jobLaunch" | "jobInterrupt" | "freshness" | "pressure" | "reorient" | "propose" | "promote" | "update"; diff --git a/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanyStateUpdatedSource.ts b/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanyStateUpdatedSource.ts index 6632a504..9cb60223 100644 --- a/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanyStateUpdatedSource.ts +++ b/vendor/codex/codex-rs/app-server-protocol/schema/typescript/v2/ThreadEpiphanyStateUpdatedSource.ts @@ -2,4 +2,4 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -export type ThreadEpiphanyStateUpdatedSource = "update" | "promote"; +export type ThreadEpiphanyStateUpdatedSource = "update" | "promote" | "jobLaunch" | "jobInterrupt"; diff --git a/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs b/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs index 702248aa..b070f4a0 100644 --- a/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs +++ b/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs @@ -386,6 +386,16 @@ client_request_definitions! { params: v2::ThreadEpiphanyPromoteParams, response: v2::ThreadEpiphanyPromoteResponse, }, + #[experimental("thread/epiphany/jobLaunch")] + ThreadEpiphanyJobLaunch => "thread/epiphany/jobLaunch" { + params: v2::ThreadEpiphanyJobLaunchParams, + response: v2::ThreadEpiphanyJobLaunchResponse, + }, + #[experimental("thread/epiphany/jobInterrupt")] + ThreadEpiphanyJobInterrupt => "thread/epiphany/jobInterrupt" { + params: v2::ThreadEpiphanyJobInterruptParams, + response: v2::ThreadEpiphanyJobInterruptResponse, + }, #[experimental("thread/epiphany/update")] ThreadEpiphanyUpdate => "thread/epiphany/update" { params: v2::ThreadEpiphanyUpdateParams, @@ -1696,10 +1706,12 @@ mod tests { v2::ThreadEpiphanySceneAction::Distill, v2::ThreadEpiphanySceneAction::Context, v2::ThreadEpiphanySceneAction::Jobs, + v2::ThreadEpiphanySceneAction::JobLaunch, v2::ThreadEpiphanySceneAction::Freshness, v2::ThreadEpiphanySceneAction::Pressure, v2::ThreadEpiphanySceneAction::Reorient, v2::ThreadEpiphanySceneAction::Update, + v2::ThreadEpiphanySceneAction::JobInterrupt, v2::ThreadEpiphanySceneAction::Propose, v2::ThreadEpiphanySceneAction::Promote, ], @@ -1793,10 +1805,12 @@ mod tests { "distill", "context", "jobs", + "jobLaunch", "freshness", "pressure", "reorient", "update", + "jobInterrupt", "propose", "promote" ] @@ -2486,6 +2500,194 @@ mod tests { Ok(()) } + #[test] + fn serialize_thread_epiphany_job_launch_response() -> Result<()> { + let response = ClientResponse::ThreadEpiphanyJobLaunch { + request_id: RequestId::Integer(12), + response: v2::ThreadEpiphanyJobLaunchResponse { + revision: 3, + changed_fields: vec![v2::ThreadEpiphanyStateUpdatedField::JobBindings], + epiphany_state: codex_protocol::protocol::EpiphanyThreadState { + revision: 3, + job_bindings: vec![codex_protocol::protocol::EpiphanyJobBinding { + id: "specialist-work".to_string(), + kind: codex_protocol::protocol::EpiphanyJobKind::Specialist, + scope: "role-scoped specialist work".to_string(), + owner_role: "epiphany-harness".to_string(), + launcher_job_id: Some("launcher-specialist".to_string()), + authority_scope: Some("epiphany.specialist".to_string()), + backend_kind: Some( + codex_protocol::protocol::EpiphanyJobBackendKind::AgentJobs, + ), + backend_job_id: Some("job-123".to_string()), + runtime_agent_job_id: Some("job-123".to_string()), + linked_subgoal_ids: Vec::new(), + linked_graph_node_ids: Vec::new(), + progress_note: Some("Launched explicitly.".to_string()), + blocking_reason: None, + }], + ..Default::default() + }, + job: v2::ThreadEpiphanyJob { + id: "specialist-work".to_string(), + kind: v2::ThreadEpiphanyJobKind::Specialist, + scope: "role-scoped specialist work".to_string(), + owner_role: "epiphany-harness".to_string(), + launcher_job_id: Some("launcher-specialist".to_string()), + authority_scope: Some("epiphany.specialist".to_string()), + backend_kind: Some(v2::ThreadEpiphanyJobBackendKind::AgentJobs), + backend_job_id: Some("job-123".to_string()), + status: v2::ThreadEpiphanyJobStatus::Pending, + runtime_agent_job_id: Some("job-123".to_string()), + items_processed: None, + items_total: None, + progress_note: Some("Runtime agent job is pending.".to_string()), + last_checkpoint_at_unix_seconds: None, + blocking_reason: None, + active_thread_ids: Vec::new(), + linked_subgoal_ids: Vec::new(), + linked_graph_node_ids: Vec::new(), + }, + }, + }; + + assert_eq!(response.id(), &RequestId::Integer(12)); + assert_eq!(response.method(), "thread/epiphany/jobLaunch"); + assert_eq!( + json!({ + "method": "thread/epiphany/jobLaunch", + "id": 12, + "response": { + "revision": 3, + "changedFields": ["jobBindings"], + "epiphanyState": { + "revision": 3, + "job_bindings": [{ + "id": "specialist-work", + "kind": "specialist", + "scope": "role-scoped specialist work", + "owner_role": "epiphany-harness", + "launcher_job_id": "launcher-specialist", + "authority_scope": "epiphany.specialist", + "backend_kind": "agent_jobs", + "backend_job_id": "job-123", + "runtime_agent_job_id": "job-123", + "progress_note": "Launched explicitly." + }] + }, + "job": { + "id": "specialist-work", + "kind": "specialist", + "scope": "role-scoped specialist work", + "ownerRole": "epiphany-harness", + "launcherJobId": "launcher-specialist", + "authorityScope": "epiphany.specialist", + "backendKind": "agentJobs", + "backendJobId": "job-123", + "status": "pending", + "runtimeAgentJobId": "job-123", + "progressNote": "Runtime agent job is pending." + } + } + }), + serde_json::to_value(&response)?, + ); + Ok(()) + } + + #[test] + fn serialize_thread_epiphany_job_interrupt_response() -> Result<()> { + let response = ClientResponse::ThreadEpiphanyJobInterrupt { + request_id: RequestId::Integer(13), + response: v2::ThreadEpiphanyJobInterruptResponse { + cancel_requested: true, + interrupted_thread_ids: vec!["worker-thread-1".to_string()], + revision: 4, + changed_fields: vec![v2::ThreadEpiphanyStateUpdatedField::JobBindings], + epiphany_state: codex_protocol::protocol::EpiphanyThreadState { + revision: 4, + job_bindings: vec![codex_protocol::protocol::EpiphanyJobBinding { + id: "specialist-work".to_string(), + kind: codex_protocol::protocol::EpiphanyJobKind::Specialist, + scope: "role-scoped specialist work".to_string(), + owner_role: "epiphany-harness".to_string(), + launcher_job_id: None, + authority_scope: Some("epiphany.specialist".to_string()), + backend_kind: None, + backend_job_id: None, + runtime_agent_job_id: None, + linked_subgoal_ids: Vec::new(), + linked_graph_node_ids: Vec::new(), + progress_note: None, + blocking_reason: Some( + "Launch explicitly to resume specialist work.".to_string(), + ), + }], + ..Default::default() + }, + job: v2::ThreadEpiphanyJob { + id: "specialist-work".to_string(), + kind: v2::ThreadEpiphanyJobKind::Specialist, + scope: "role-scoped specialist work".to_string(), + owner_role: "epiphany-harness".to_string(), + launcher_job_id: None, + authority_scope: Some("epiphany.specialist".to_string()), + backend_kind: None, + backend_job_id: None, + status: v2::ThreadEpiphanyJobStatus::Blocked, + runtime_agent_job_id: None, + items_processed: None, + items_total: None, + progress_note: None, + last_checkpoint_at_unix_seconds: None, + blocking_reason: Some( + "Launch explicitly to resume specialist work.".to_string(), + ), + active_thread_ids: Vec::new(), + linked_subgoal_ids: Vec::new(), + linked_graph_node_ids: Vec::new(), + }, + }, + }; + + assert_eq!(response.id(), &RequestId::Integer(13)); + assert_eq!(response.method(), "thread/epiphany/jobInterrupt"); + assert_eq!( + json!({ + "method": "thread/epiphany/jobInterrupt", + "id": 13, + "response": { + "cancelRequested": true, + "interruptedThreadIds": ["worker-thread-1"], + "revision": 4, + "changedFields": ["jobBindings"], + "epiphanyState": { + "revision": 4, + "job_bindings": [{ + "id": "specialist-work", + "kind": "specialist", + "scope": "role-scoped specialist work", + "owner_role": "epiphany-harness", + "authority_scope": "epiphany.specialist", + "blocking_reason": "Launch explicitly to resume specialist work." + }] + }, + "job": { + "id": "specialist-work", + "kind": "specialist", + "scope": "role-scoped specialist work", + "ownerRole": "epiphany-harness", + "authorityScope": "epiphany.specialist", + "status": "blocked", + "blockingReason": "Launch explicitly to resume specialist work." + } + } + }), + serde_json::to_value(&response)?, + ); + Ok(()) + } + #[test] fn serialize_thread_epiphany_update_response() -> Result<()> { let response = ClientResponse::ThreadEpiphanyUpdate { @@ -2558,6 +2760,38 @@ mod tests { Ok(()) } + #[test] + fn serialize_thread_epiphany_state_updated_job_launch_notification() -> Result<()> { + let notification = ServerNotification::ThreadEpiphanyStateUpdated( + v2::ThreadEpiphanyStateUpdatedNotification { + thread_id: "thr_123".to_string(), + source: v2::ThreadEpiphanyStateUpdatedSource::JobLaunch, + revision: 8, + changed_fields: vec![v2::ThreadEpiphanyStateUpdatedField::JobBindings], + epiphany_state: codex_protocol::protocol::EpiphanyThreadState { + revision: 8, + ..Default::default() + }, + }, + ); + assert_eq!( + json!({ + "method": "thread/epiphany/stateUpdated", + "params": { + "threadId": "thr_123", + "source": "jobLaunch", + "revision": 8, + "changedFields": ["jobBindings"], + "epiphanyState": { + "revision": 8 + } + } + }), + serde_json::to_value(¬ification)?, + ); + Ok(()) + } + #[test] fn serialize_thread_epiphany_jobs_updated_notification() -> Result<()> { let notification = ServerNotification::ThreadEpiphanyJobsUpdated( @@ -3277,6 +3511,45 @@ mod tests { assert_eq!(reason, Some("thread/epiphany/promote")); } + #[test] + fn thread_epiphany_job_launch_is_marked_experimental() { + let request = ClientRequest::ThreadEpiphanyJobLaunch { + request_id: RequestId::Integer(1), + params: v2::ThreadEpiphanyJobLaunchParams { + thread_id: "thr_123".to_string(), + expected_revision: Some(2), + binding_id: "specialist-work".to_string(), + kind: codex_protocol::protocol::EpiphanyJobKind::Specialist, + scope: "role-scoped specialist work".to_string(), + owner_role: "epiphany-harness".to_string(), + authority_scope: "epiphany.specialist".to_string(), + linked_subgoal_ids: vec!["phase6".to_string()], + linked_graph_node_ids: vec!["job-surface".to_string()], + instruction: "Summarize the bound specialist task.".to_string(), + input_json: json!({"task": "smoke"}), + output_schema_json: None, + max_runtime_seconds: Some(30), + }, + }; + let reason = crate::experimental_api::ExperimentalApi::experimental_reason(&request); + assert_eq!(reason, Some("thread/epiphany/jobLaunch")); + } + + #[test] + fn thread_epiphany_job_interrupt_is_marked_experimental() { + let request = ClientRequest::ThreadEpiphanyJobInterrupt { + request_id: RequestId::Integer(1), + params: v2::ThreadEpiphanyJobInterruptParams { + thread_id: "thr_123".to_string(), + expected_revision: Some(3), + binding_id: "specialist-work".to_string(), + reason: Some("Stop the specialist smoke job.".to_string()), + }, + }; + let reason = crate::experimental_api::ExperimentalApi::experimental_reason(&request); + assert_eq!(reason, Some("thread/epiphany/jobInterrupt")); + } + #[test] fn thread_epiphany_update_is_marked_experimental() { let request = ClientRequest::ThreadEpiphanyUpdate { diff --git a/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs b/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs index 8e3fae15..1da494ba 100644 --- a/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs +++ b/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs @@ -70,6 +70,7 @@ use codex_protocol::protocol::EpiphanyInvariant as CoreEpiphanyInvariant; use codex_protocol::protocol::EpiphanyInvestigationCheckpoint as CoreEpiphanyInvestigationCheckpoint; use codex_protocol::protocol::EpiphanyInvestigationDisposition as CoreEpiphanyInvestigationDisposition; use codex_protocol::protocol::EpiphanyJobBinding as CoreEpiphanyJobBinding; +use codex_protocol::protocol::EpiphanyJobKind as CoreEpiphanyJobKind; use codex_protocol::protocol::EpiphanyModeState as CoreEpiphanyModeState; use codex_protocol::protocol::EpiphanyObservation as CoreEpiphanyObservation; use codex_protocol::protocol::EpiphanyRetrievalStatus as CoreEpiphanyRetrievalStatus; @@ -3996,6 +3997,8 @@ pub enum ThreadEpiphanySceneAction { Distill, Context, Jobs, + JobLaunch, + JobInterrupt, Freshness, Pressure, Reorient, @@ -4797,6 +4800,8 @@ pub struct ThreadEpiphanyStateUpdatedNotification { pub enum ThreadEpiphanyStateUpdatedSource { Update, Promote, + JobLaunch, + JobInterrupt, } #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema, TS)] @@ -4839,6 +4844,71 @@ pub enum ThreadEpiphanyJobsUpdatedSource { RuntimeProgress, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadEpiphanyJobLaunchParams { + pub thread_id: String, + #[serde(default)] + #[ts(optional = nullable)] + pub expected_revision: Option, + pub binding_id: String, + pub kind: CoreEpiphanyJobKind, + pub scope: String, + pub owner_role: String, + pub authority_scope: String, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub linked_subgoal_ids: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub linked_graph_node_ids: Vec, + pub instruction: String, + #[ts(type = "unknown")] + pub input_json: serde_json::Value, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional = nullable, type = "unknown | null")] + pub output_schema_json: Option, + #[serde(default)] + #[ts(optional = nullable)] + pub max_runtime_seconds: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadEpiphanyJobLaunchResponse { + pub revision: u64, + pub changed_fields: Vec, + pub epiphany_state: CoreEpiphanyThreadState, + pub job: ThreadEpiphanyJob, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadEpiphanyJobInterruptParams { + pub thread_id: String, + #[serde(default)] + #[ts(optional = nullable)] + pub expected_revision: Option, + pub binding_id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional = nullable)] + pub reason: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export_to = "v2/")] +pub struct ThreadEpiphanyJobInterruptResponse { + pub cancel_requested: bool, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub interrupted_thread_ids: Vec, + pub revision: u64, + pub changed_fields: Vec, + pub epiphany_state: CoreEpiphanyThreadState, + pub job: ThreadEpiphanyJob, +} + #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)] #[serde(rename_all = "camelCase")] #[ts(export_to = "v2/")] diff --git a/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs b/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs index 2d0dc4df..b2c8e3fe 100644 --- a/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs +++ b/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs @@ -168,7 +168,11 @@ use codex_app_server_protocol::ThreadEpiphanyInvalidationInput; use codex_app_server_protocol::ThreadEpiphanyInvalidationStatus; use codex_app_server_protocol::ThreadEpiphanyJob; use codex_app_server_protocol::ThreadEpiphanyJobBackendKind; +use codex_app_server_protocol::ThreadEpiphanyJobInterruptParams; +use codex_app_server_protocol::ThreadEpiphanyJobInterruptResponse; use codex_app_server_protocol::ThreadEpiphanyJobKind; +use codex_app_server_protocol::ThreadEpiphanyJobLaunchParams; +use codex_app_server_protocol::ThreadEpiphanyJobLaunchResponse; use codex_app_server_protocol::ThreadEpiphanyJobStatus; use codex_app_server_protocol::ThreadEpiphanyJobsParams; use codex_app_server_protocol::ThreadEpiphanyJobsResponse; @@ -299,6 +303,8 @@ use codex_core::CodexThreadTurnContextOverrides; use codex_core::EPIPHANY_RETRIEVAL_DEFAULT_LIMIT; use codex_core::EPIPHANY_RETRIEVAL_MAX_LIMIT; use codex_core::EpiphanyDistillInput; +use codex_core::EpiphanyJobInterruptRequest; +use codex_core::EpiphanyJobLaunchRequest; use codex_core::EpiphanyMapProposalInput; use codex_core::EpiphanyPromotionInput; use codex_core::EpiphanyRetrieveQuery; @@ -1095,6 +1101,14 @@ impl CodexMessageProcessor { self.thread_epiphany_promote(to_connection_request_id(request_id), params) .await; } + ClientRequest::ThreadEpiphanyJobLaunch { request_id, params } => { + self.thread_epiphany_job_launch(to_connection_request_id(request_id), params) + .await; + } + ClientRequest::ThreadEpiphanyJobInterrupt { request_id, params } => { + self.thread_epiphany_job_interrupt(to_connection_request_id(request_id), params) + .await; + } ClientRequest::ThreadEpiphanyUpdate { request_id, params } => { self.thread_epiphany_update(to_connection_request_id(request_id), params) .await; @@ -4975,6 +4989,229 @@ impl CodexMessageProcessor { .await; } + async fn thread_epiphany_job_launch( + &self, + request_id: ConnectionRequestId, + params: ThreadEpiphanyJobLaunchParams, + ) { + let ThreadEpiphanyJobLaunchParams { + thread_id, + expected_revision, + binding_id, + kind, + scope, + owner_role, + authority_scope, + linked_subgoal_ids, + linked_graph_node_ids, + instruction, + input_json, + output_schema_json, + max_runtime_seconds, + } = params; + + let thread_uuid = match ThreadId::from_string(&thread_id) { + Ok(id) => id, + Err(err) => { + self.send_invalid_request_error(request_id, format!("invalid thread id: {err}")) + .await; + return; + } + }; + + let thread = match self.thread_manager.get_thread(thread_uuid).await { + Ok(thread) => thread, + Err(_) => { + self.send_invalid_request_error( + request_id, + format!("thread not loaded: {thread_uuid}"), + ) + .await; + return; + } + }; + + let changed_fields = vec![ThreadEpiphanyStateUpdatedField::JobBindings]; + let launched = match thread + .epiphany_launch_job(EpiphanyJobLaunchRequest { + expected_revision, + binding_id: binding_id.clone(), + kind, + scope, + owner_role, + authority_scope, + linked_subgoal_ids, + linked_graph_node_ids, + instruction, + input_json, + output_schema_json, + max_runtime_seconds, + }) + .await + { + Ok(launched) => launched, + Err(CodexErr::InvalidRequest(message)) => { + self.send_invalid_request_error(request_id, message).await; + return; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to launch Epiphany job for {thread_uuid}: {err}"), + ) + .await; + return; + } + }; + let epiphany_state = + client_visible_live_thread_epiphany_state(thread.as_ref(), launched.epiphany_state) + .await; + let state_db_ctx = thread.epiphany_state_runtime().await; + let job_resolution = + load_epiphany_job_launcher_snapshots(state_db_ctx.as_ref(), Some(&epiphany_state)) + .await; + let job = map_epiphany_jobs(Some(&epiphany_state), None, &job_resolution) + .into_iter() + .find(|job| job.id == binding_id) + .unwrap_or_else(|| ThreadEpiphanyJob { + id: launched.binding_id.clone(), + kind: map_core_epiphany_job_kind(kind), + scope: "missing launched job projection".to_string(), + owner_role: "epiphany-harness".to_string(), + launcher_job_id: Some(launched.launcher_job_id.clone()), + authority_scope: None, + backend_kind: Some(ThreadEpiphanyJobBackendKind::AgentJobs), + backend_job_id: Some(launched.backend_job_id.clone()), + status: ThreadEpiphanyJobStatus::Pending, + runtime_agent_job_id: Some(launched.backend_job_id.clone()), + items_processed: None, + items_total: None, + progress_note: None, + last_checkpoint_at_unix_seconds: None, + blocking_reason: None, + active_thread_ids: Vec::new(), + linked_subgoal_ids: Vec::new(), + linked_graph_node_ids: Vec::new(), + }); + + self.outgoing + .send_response( + request_id, + ThreadEpiphanyJobLaunchResponse { + revision: epiphany_state.revision, + changed_fields: changed_fields.clone(), + epiphany_state: epiphany_state.clone(), + job, + }, + ) + .await; + self.outgoing + .send_server_notification(ServerNotification::ThreadEpiphanyStateUpdated( + ThreadEpiphanyStateUpdatedNotification { + thread_id: thread_uuid.to_string(), + source: ThreadEpiphanyStateUpdatedSource::JobLaunch, + revision: epiphany_state.revision, + changed_fields, + epiphany_state, + }, + )) + .await; + } + + async fn thread_epiphany_job_interrupt( + &self, + request_id: ConnectionRequestId, + params: ThreadEpiphanyJobInterruptParams, + ) { + let ThreadEpiphanyJobInterruptParams { + thread_id, + expected_revision, + binding_id, + reason, + } = params; + + let thread_uuid = match ThreadId::from_string(&thread_id) { + Ok(id) => id, + Err(err) => { + self.send_invalid_request_error(request_id, format!("invalid thread id: {err}")) + .await; + return; + } + }; + + let thread = match self.thread_manager.get_thread(thread_uuid).await { + Ok(thread) => thread, + Err(_) => { + self.send_invalid_request_error( + request_id, + format!("thread not loaded: {thread_uuid}"), + ) + .await; + return; + } + }; + + let changed_fields = vec![ThreadEpiphanyStateUpdatedField::JobBindings]; + let interrupted = match thread + .epiphany_interrupt_job(EpiphanyJobInterruptRequest { + expected_revision, + binding_id: binding_id.clone(), + reason, + }) + .await + { + Ok(interrupted) => interrupted, + Err(CodexErr::InvalidRequest(message)) => { + self.send_invalid_request_error(request_id, message).await; + return; + } + Err(err) => { + self.send_internal_error( + request_id, + format!("failed to interrupt Epiphany job for {thread_uuid}: {err}"), + ) + .await; + return; + } + }; + let epiphany_state = + client_visible_live_thread_epiphany_state(thread.as_ref(), interrupted.epiphany_state) + .await; + let state_db_ctx = thread.epiphany_state_runtime().await; + let job_resolution = + load_epiphany_job_launcher_snapshots(state_db_ctx.as_ref(), Some(&epiphany_state)) + .await; + let job = map_epiphany_jobs(Some(&epiphany_state), None, &job_resolution) + .into_iter() + .find(|job| job.id == binding_id) + .unwrap_or_else(|| map_epiphany_specialist_job()); + + self.outgoing + .send_response( + request_id, + ThreadEpiphanyJobInterruptResponse { + cancel_requested: interrupted.cancel_requested, + interrupted_thread_ids: interrupted.interrupted_thread_ids.clone(), + revision: epiphany_state.revision, + changed_fields: changed_fields.clone(), + epiphany_state: epiphany_state.clone(), + job, + }, + ) + .await; + self.outgoing + .send_server_notification(ServerNotification::ThreadEpiphanyStateUpdated( + ThreadEpiphanyStateUpdatedNotification { + thread_id: thread_uuid.to_string(), + source: ThreadEpiphanyStateUpdatedSource::JobInterrupt, + revision: epiphany_state.revision, + changed_fields, + epiphany_state, + }, + )) + .await; + } + /// Builds the API view for `thread/read` from persisted metadata plus optional live state. async fn read_thread_view( &self, @@ -11040,12 +11277,14 @@ fn epiphany_scene_available_actions( ThreadEpiphanySceneAction::Distill, ThreadEpiphanySceneAction::Context, ThreadEpiphanySceneAction::Jobs, + ThreadEpiphanySceneAction::JobLaunch, ThreadEpiphanySceneAction::Freshness, ThreadEpiphanySceneAction::Pressure, ThreadEpiphanySceneAction::Reorient, ThreadEpiphanySceneAction::Update, ]; if state_present { + actions.push(ThreadEpiphanySceneAction::JobInterrupt); actions.push(ThreadEpiphanySceneAction::Propose); actions.push(ThreadEpiphanySceneAction::Promote); } @@ -11982,7 +12221,7 @@ pub(crate) async fn thread_epiphany_jobs_updated_notification_for_agent_job_prog return None; } - let state_db_ctx = thread.state_db()?; + let state_db_ctx = thread.epiphany_state_runtime().await?; let retrieval_override = if state.retrieval.is_none() && binding_ids.contains("retrieval-index") { Some(thread.epiphany_retrieval_state().await) @@ -12163,6 +12402,10 @@ fn overlay_epiphany_job_binding( if let Some(blocking_reason) = binding.blocking_reason.clone() { job.blocking_reason = Some(blocking_reason); } + if binding.blocking_reason.is_some() && binding_backend_job_id(binding).is_none() { + job.status = ThreadEpiphanyJobStatus::Blocked; + return job; + } if let Some(snapshot) = snapshot { job.status = map_thread_epiphany_job_status(snapshot.status); @@ -14131,10 +14374,12 @@ mod tests { ThreadEpiphanySceneAction::Distill, ThreadEpiphanySceneAction::Context, ThreadEpiphanySceneAction::Jobs, + ThreadEpiphanySceneAction::JobLaunch, ThreadEpiphanySceneAction::Freshness, ThreadEpiphanySceneAction::Pressure, ThreadEpiphanySceneAction::Reorient, ThreadEpiphanySceneAction::Update, + ThreadEpiphanySceneAction::JobInterrupt, ThreadEpiphanySceneAction::Propose, ThreadEpiphanySceneAction::Promote, ] @@ -15064,6 +15309,62 @@ mod tests { ); } + #[test] + fn map_epiphany_jobs_blocks_binding_after_interrupt_clears_backend() { + let state = codex_protocol::protocol::EpiphanyThreadState { + job_bindings: vec![codex_protocol::protocol::EpiphanyJobBinding { + id: "specialist-work".to_string(), + kind: codex_protocol::protocol::EpiphanyJobKind::Specialist, + scope: "role-scoped specialist work".to_string(), + owner_role: "epiphany-harness".to_string(), + launcher_job_id: None, + authority_scope: Some("epiphany.specialist".to_string()), + backend_kind: None, + backend_job_id: None, + runtime_agent_job_id: None, + linked_subgoal_ids: vec!["phase-6".to_string()], + linked_graph_node_ids: vec!["job-control".to_string()], + progress_note: None, + blocking_reason: Some( + "No active runtime backend is currently bound; launch explicitly to resume specialist work." + .to_string(), + ), + }], + ..Default::default() + }; + + let jobs = map_epiphany_jobs( + Some(&state), + None, + &EpiphanyJobLauncherResolution::default(), + ); + let specialist = jobs + .iter() + .find(|job| job.id == "specialist-work") + .expect("specialist slot should exist"); + + assert_eq!(specialist.status, ThreadEpiphanyJobStatus::Blocked); + assert_eq!( + specialist.authority_scope.as_deref(), + Some("epiphany.specialist") + ); + assert_eq!(specialist.launcher_job_id, None); + assert_eq!(specialist.backend_kind, None); + assert_eq!(specialist.backend_job_id, None); + assert_eq!(specialist.runtime_agent_job_id, None); + assert_eq!(specialist.linked_subgoal_ids, vec!["phase-6".to_string()]); + assert_eq!( + specialist.linked_graph_node_ids, + vec!["job-control".to_string()] + ); + assert!( + specialist + .blocking_reason + .as_deref() + .is_some_and(|reason| reason.contains("launch explicitly")) + ); + } + #[test] fn epiphany_update_patch_changed_fields_reports_patch_surface() { let fields = epiphany_update_patch_changed_fields(&ThreadEpiphanyUpdatePatch { diff --git a/vendor/codex/codex-rs/core/src/codex_thread.rs b/vendor/codex/codex-rs/core/src/codex_thread.rs index c7417071..3e88ba75 100644 --- a/vendor/codex/codex-rs/core/src/codex_thread.rs +++ b/vendor/codex/codex-rs/core/src/codex_thread.rs @@ -10,6 +10,7 @@ use crate::session::Codex; use crate::session::SessionSettingsUpdate; use crate::session::SteerInputError; use codex_features::Feature; +use codex_protocol::ThreadId; use codex_protocol::config_types::ApprovalsReviewer; use codex_protocol::config_types::CollaborationMode; use codex_protocol::config_types::Personality; @@ -34,6 +35,7 @@ use codex_protocol::protocol::EpiphanyInvariant; use codex_protocol::protocol::EpiphanyInvestigationCheckpoint; use codex_protocol::protocol::EpiphanyJobBackendKind; use codex_protocol::protocol::EpiphanyJobBinding; +use codex_protocol::protocol::EpiphanyJobKind; use codex_protocol::protocol::EpiphanyModeState; use codex_protocol::protocol::EpiphanyObservation; use codex_protocol::protocol::EpiphanyRetrievalState; @@ -61,6 +63,8 @@ use tokio::sync::Mutex; use tokio::sync::watch; use codex_rollout::state_db::StateDbHandle; +use serde_json::Value; +use uuid::Uuid; #[derive(Clone, Debug)] pub struct ThreadConfigSnapshot { @@ -121,6 +125,45 @@ pub struct EpiphanyStateUpdate { pub mode: Option, } +#[derive(Debug, Clone)] +pub struct EpiphanyJobLaunchRequest { + pub expected_revision: Option, + pub binding_id: String, + pub kind: EpiphanyJobKind, + pub scope: String, + pub owner_role: String, + pub authority_scope: String, + pub linked_subgoal_ids: Vec, + pub linked_graph_node_ids: Vec, + pub instruction: String, + pub input_json: Value, + pub output_schema_json: Option, + pub max_runtime_seconds: Option, +} + +#[derive(Debug, Clone)] +pub struct EpiphanyJobLaunchResult { + pub epiphany_state: EpiphanyThreadState, + pub binding_id: String, + pub launcher_job_id: String, + pub backend_job_id: String, +} + +#[derive(Debug, Clone)] +pub struct EpiphanyJobInterruptRequest { + pub expected_revision: Option, + pub binding_id: String, + pub reason: Option, +} + +#[derive(Debug, Clone)] +pub struct EpiphanyJobInterruptResult { + pub epiphany_state: EpiphanyThreadState, + pub binding_id: String, + pub cancel_requested: bool, + pub interrupted_thread_ids: Vec, +} + impl EpiphanyStateUpdate { fn is_empty(&self) -> bool { self.objective.is_none() @@ -372,6 +415,18 @@ impl CodexThread { self.codex.state_db() } + pub async fn epiphany_state_runtime(&self) -> Option { + if let Some(state_db) = self.state_db() { + return Some(state_db); + } + + let codex_home = self.codex.session.codex_home().await; + let config = self.codex.thread_config_snapshot().await; + codex_state::StateRuntime::init(codex_home.to_path_buf(), config.model_provider_id) + .await + .ok() + } + pub async fn config_snapshot(&self) -> ThreadConfigSnapshot { self.codex.thread_config_snapshot().await } @@ -435,6 +490,290 @@ impl CodexThread { Ok(next_state) } + pub async fn epiphany_launch_job( + &self, + request: EpiphanyJobLaunchRequest, + ) -> CodexResult { + validate_epiphany_job_launch_request(&request)?; + + let session = self.codex.session.clone(); + let state_db = self.epiphany_state_runtime().await.ok_or_else(|| { + CodexErr::InvalidRequest( + "sqlite state db is unavailable for Epiphany job launch".to_string(), + ) + })?; + let launch_turn = session.new_default_turn().await; + let launch_options = crate::tools::handlers::agent_jobs::build_runner_options( + &session, + &launch_turn, + Some(1), + ) + .await + .map_err(|err| { + CodexErr::InvalidRequest(format!( + "failed to prepare Epiphany agent job runner: {err}" + )) + })?; + + let current_state = self + .codex + .session + .epiphany_state() + .await + .unwrap_or_default(); + if let Some(expected_revision) = request.expected_revision + && current_state.revision != expected_revision + { + return Err(CodexErr::InvalidRequest(format!( + "epiphany state revision mismatch: expected {expected_revision}, found {}", + current_state.revision + ))); + } + validate_epiphany_job_launch_target(¤t_state, &state_db, &request).await?; + + let launcher_job_id = format!("epiphany-launch-{}", Uuid::new_v4()); + let backend_job_id = Uuid::new_v4().to_string(); + let replacement_binding = build_epiphany_job_launch_binding( + &request, + launcher_job_id.as_str(), + backend_job_id.as_str(), + ); + let next_job_bindings = replace_or_append_epiphany_job_binding( + current_state.job_bindings.clone(), + replacement_binding, + ); + + let validation_errors = epiphany_state_update_validation_errors( + ¤t_state, + &EpiphanyStateUpdate { + job_bindings: Some(next_job_bindings.clone()), + ..Default::default() + }, + ); + if !validation_errors.is_empty() { + return Err(CodexErr::InvalidRequest(format!( + "invalid Epiphany job launch patch: {}", + validation_errors.join("; ") + ))); + } + + let output_csv_path = epiphany_agent_job_output_csv_path( + self.codex.session.codex_home().await.as_path(), + backend_job_id.as_str(), + ); + let row_object = request.input_json.as_object().cloned().ok_or_else(|| { + CodexErr::InvalidRequest( + "epiphany job launch input_json must be a JSON object".to_string(), + ) + })?; + let input_headers = sorted_json_object_keys(&row_object); + let job_items = vec![codex_state::AgentJobItemCreateParams { + item_id: request.binding_id.clone(), + row_index: 0, + source_id: Some(request.binding_id.clone()), + row_json: Value::Object(row_object), + }]; + + state_db + .create_agent_job( + &codex_state::AgentJobCreateParams { + id: backend_job_id.clone(), + name: epiphany_agent_job_name(request.binding_id.as_str()), + instruction: request.instruction.clone(), + auto_export: false, + max_runtime_seconds: request.max_runtime_seconds, + output_schema_json: request.output_schema_json.clone(), + input_headers, + input_csv_path: output_csv_path.display().to_string(), + output_csv_path: output_csv_path.display().to_string(), + }, + job_items.as_slice(), + ) + .await + .map_err(|err| { + CodexErr::Fatal(format!("failed to create Epiphany backend job: {err}")) + })?; + + let epiphany_state = match self + .epiphany_update_state(EpiphanyStateUpdate { + expected_revision: request.expected_revision, + job_bindings: Some(next_job_bindings), + ..Default::default() + }) + .await + { + Ok(epiphany_state) => epiphany_state, + Err(err) => { + let _ = state_db + .mark_agent_job_cancelled( + backend_job_id.as_str(), + "Epiphany launch state update failed before the binding could persist.", + ) + .await; + return Err(err); + } + }; + + let runner_session = session.clone(); + let runner_state_db = state_db.clone(); + let runner_job_id = backend_job_id.clone(); + tokio::spawn(async move { + if let Err(err) = runner_state_db + .mark_agent_job_running(runner_job_id.as_str()) + .await + { + let error_message = + format!("failed to transition Epiphany backend job to running: {err}"); + let _ = runner_state_db + .mark_agent_job_failed(runner_job_id.as_str(), error_message.as_str()) + .await; + return; + } + + if let Err(err) = crate::tools::handlers::agent_jobs::run_agent_job_loop( + runner_session, + launch_turn, + runner_state_db.clone(), + runner_job_id.clone(), + launch_options, + ) + .await + { + let error_message = format!("Epiphany backend job runner failed: {err}"); + let _ = runner_state_db + .mark_agent_job_failed(runner_job_id.as_str(), error_message.as_str()) + .await; + } + }); + + Ok(EpiphanyJobLaunchResult { + epiphany_state, + binding_id: request.binding_id, + launcher_job_id, + backend_job_id, + }) + } + + pub async fn epiphany_interrupt_job( + &self, + request: EpiphanyJobInterruptRequest, + ) -> CodexResult { + if request.binding_id.trim().is_empty() { + return Err(CodexErr::InvalidRequest( + "epiphany job interrupt binding_id must be non-empty".to_string(), + )); + } + + let state_db = self.epiphany_state_runtime().await.ok_or_else(|| { + CodexErr::InvalidRequest( + "sqlite state db is unavailable for Epiphany job interrupt".to_string(), + ) + })?; + let current_state = self + .codex + .session + .epiphany_state() + .await + .unwrap_or_default(); + if let Some(expected_revision) = request.expected_revision + && current_state.revision != expected_revision + { + return Err(CodexErr::InvalidRequest(format!( + "epiphany state revision mismatch: expected {expected_revision}, found {}", + current_state.revision + ))); + } + + let Some(binding_index) = current_state + .job_bindings + .iter() + .position(|binding| binding.id == request.binding_id) + else { + return Err(CodexErr::InvalidRequest(format!( + "epiphany job binding {:?} was not found", + request.binding_id + ))); + }; + let binding = current_state.job_bindings[binding_index].clone(); + let agent_job_id = binding_agent_jobs_job_id_core(&binding).ok_or_else(|| { + CodexErr::InvalidRequest(format!( + "epiphany job binding {:?} is not currently backed by the agent_jobs runtime", + request.binding_id + )) + })?; + + let mut interrupted_thread_ids = Vec::new(); + let running_items = state_db + .list_agent_job_items( + agent_job_id, + Some(codex_state::AgentJobItemStatus::Running), + Some(64), + ) + .await + .map_err(|err| { + CodexErr::Fatal(format!( + "failed to inspect running Epiphany backend job items: {err}" + )) + })?; + for running_item in running_items { + let Some(assigned_thread_id) = running_item.assigned_thread_id else { + continue; + }; + let Ok(thread_id) = ThreadId::from_string(assigned_thread_id.as_str()) else { + continue; + }; + match self + .codex + .session + .services + .agent_control + .shutdown_live_agent(thread_id) + .await + { + Ok(_) | Err(CodexErr::ThreadNotFound(_)) | Err(CodexErr::InternalAgentDied) => { + interrupted_thread_ids.push(assigned_thread_id); + } + Err(err) => { + return Err(CodexErr::Fatal(format!( + "failed to interrupt Epiphany worker thread {assigned_thread_id}: {err}" + ))); + } + } + } + + let cancel_requested = state_db + .mark_agent_job_cancelled( + agent_job_id, + request.reason.as_deref().unwrap_or( + "Epiphany launch authority requested an interrupt for the bound backend job.", + ), + ) + .await + .map_err(|err| { + CodexErr::Fatal(format!("failed to cancel Epiphany backend job: {err}")) + })?; + + let next_job_bindings = clear_epiphany_job_binding_backend( + current_state.job_bindings.clone(), + binding_index, + "No active runtime backend is currently bound; launch explicitly to resume specialist work.", + ); + let epiphany_state = self + .epiphany_update_state(EpiphanyStateUpdate { + expected_revision: request.expected_revision, + job_bindings: Some(next_job_bindings), + ..Default::default() + }) + .await?; + + Ok(EpiphanyJobInterruptResult { + epiphany_state, + binding_id: request.binding_id, + cancel_requested, + interrupted_thread_ids, + }) + } + pub async fn epiphany_retrieval_state(&self) -> EpiphanyRetrievalState { let config = self.codex.thread_config_snapshot().await; let workspace_root = config.cwd.to_path_buf(); @@ -558,6 +897,194 @@ impl CodexThread { } } +fn validate_epiphany_job_launch_request(request: &EpiphanyJobLaunchRequest) -> CodexResult<()> { + if request.binding_id.trim().is_empty() { + return Err(CodexErr::InvalidRequest( + "epiphany job launch binding_id must be non-empty".to_string(), + )); + } + if matches!( + request.binding_id.as_str(), + "retrieval-index" | "graph-remap" | "verification" + ) { + return Err(CodexErr::InvalidRequest(format!( + "epiphany job launch binding_id {:?} is reserved for a derived built-in slot", + request.binding_id + ))); + } + if request.kind != EpiphanyJobKind::Specialist { + return Err(CodexErr::InvalidRequest( + "epiphany job launch currently supports only specialist jobs on the agent_jobs backend" + .to_string(), + )); + } + if request.scope.trim().is_empty() { + return Err(CodexErr::InvalidRequest( + "epiphany job launch scope must be non-empty".to_string(), + )); + } + if request.owner_role.trim().is_empty() { + return Err(CodexErr::InvalidRequest( + "epiphany job launch owner_role must be non-empty".to_string(), + )); + } + if request.authority_scope.trim().is_empty() { + return Err(CodexErr::InvalidRequest( + "epiphany job launch authority_scope must be non-empty".to_string(), + )); + } + if request.instruction.trim().is_empty() { + return Err(CodexErr::InvalidRequest( + "epiphany job launch instruction must be non-empty".to_string(), + )); + } + if !request.input_json.is_object() { + return Err(CodexErr::InvalidRequest( + "epiphany job launch input_json must be a JSON object".to_string(), + )); + } + if let Some(max_runtime_seconds) = request.max_runtime_seconds + && max_runtime_seconds == 0 + { + return Err(CodexErr::InvalidRequest( + "epiphany job launch max_runtime_seconds must be >= 1".to_string(), + )); + } + Ok(()) +} + +async fn validate_epiphany_job_launch_target( + state: &EpiphanyThreadState, + state_db: &StateDbHandle, + request: &EpiphanyJobLaunchRequest, +) -> CodexResult<()> { + let Some(existing_binding) = state + .job_bindings + .iter() + .find(|binding| binding.id == request.binding_id) + else { + return Ok(()); + }; + let Some(agent_job_id) = binding_agent_jobs_job_id_core(existing_binding) else { + return Ok(()); + }; + let existing_job = state_db.get_agent_job(agent_job_id).await.map_err(|err| { + CodexErr::Fatal(format!( + "failed to inspect existing Epiphany backend job {:?}: {err}", + agent_job_id + )) + })?; + if existing_job.as_ref().is_some_and(|job| { + matches!( + job.status, + codex_state::AgentJobStatus::Pending | codex_state::AgentJobStatus::Running + ) + }) { + return Err(CodexErr::InvalidRequest(format!( + "epiphany job binding {:?} is already bound to active backend job {:?}; interrupt it before launching a replacement", + request.binding_id, agent_job_id + ))); + } + Ok(()) +} + +fn build_epiphany_job_launch_binding( + request: &EpiphanyJobLaunchRequest, + launcher_job_id: &str, + backend_job_id: &str, +) -> EpiphanyJobBinding { + EpiphanyJobBinding { + id: request.binding_id.clone(), + kind: request.kind, + scope: request.scope.clone(), + owner_role: request.owner_role.clone(), + launcher_job_id: Some(launcher_job_id.to_string()), + authority_scope: Some(request.authority_scope.clone()), + backend_kind: Some(EpiphanyJobBackendKind::AgentJobs), + backend_job_id: Some(backend_job_id.to_string()), + runtime_agent_job_id: Some(backend_job_id.to_string()), + linked_subgoal_ids: request.linked_subgoal_ids.clone(), + linked_graph_node_ids: request.linked_graph_node_ids.clone(), + progress_note: Some( + "Explicitly launched through the Epiphany authority surface onto the agent_jobs backend." + .to_string(), + ), + blocking_reason: None, + } +} + +fn replace_or_append_epiphany_job_binding( + mut bindings: Vec, + replacement: EpiphanyJobBinding, +) -> Vec { + if let Some(existing) = bindings + .iter_mut() + .find(|binding| binding.id == replacement.id) + { + *existing = replacement; + return bindings; + } + bindings.push(replacement); + bindings +} + +fn clear_epiphany_job_binding_backend( + mut bindings: Vec, + binding_index: usize, + blocking_reason: &str, +) -> Vec { + let binding = &mut bindings[binding_index]; + binding.launcher_job_id = None; + binding.backend_kind = None; + binding.backend_job_id = None; + binding.runtime_agent_job_id = None; + binding.progress_note = None; + binding.blocking_reason = Some(blocking_reason.to_string()); + bindings +} + +fn binding_backend_kind_core(binding: &EpiphanyJobBinding) -> Option { + binding.backend_kind.or_else(|| { + if binding.runtime_agent_job_id.is_some() { + Some(EpiphanyJobBackendKind::AgentJobs) + } else { + None + } + }) +} + +fn binding_backend_job_id_core(binding: &EpiphanyJobBinding) -> Option<&str> { + binding + .backend_job_id + .as_deref() + .or(binding.runtime_agent_job_id.as_deref()) +} + +fn binding_agent_jobs_job_id_core(binding: &EpiphanyJobBinding) -> Option<&str> { + match binding_backend_kind_core(binding) { + Some(EpiphanyJobBackendKind::AgentJobs) => binding_backend_job_id_core(binding), + None => None, + } +} + +fn epiphany_agent_job_name(binding_id: &str) -> String { + let suffix = Uuid::new_v4().simple().to_string(); + let short_suffix = &suffix[..8]; + format!("epiphany-{binding_id}-{short_suffix}") +} + +fn epiphany_agent_job_output_csv_path(codex_home: &std::path::Path, job_id: &str) -> PathBuf { + codex_home + .join("epiphany-agent-jobs") + .join(format!("{job_id}.csv")) +} + +fn sorted_json_object_keys(row_object: &serde_json::Map) -> Vec { + let mut keys = row_object.keys().cloned().collect::>(); + keys.sort(); + keys +} + fn epiphany_state_update_validation_errors( state: &EpiphanyThreadState, update: &EpiphanyStateUpdate, diff --git a/vendor/codex/codex-rs/core/src/lib.rs b/vendor/codex/codex-rs/core/src/lib.rs index 7dc6b0c1..9601d833 100644 --- a/vendor/codex/codex-rs/core/src/lib.rs +++ b/vendor/codex/codex-rs/core/src/lib.rs @@ -24,6 +24,8 @@ mod epiphany_retrieval; mod epiphany_rollout; pub use codex_thread::CodexThread; pub use codex_thread::CodexThreadTurnContextOverrides; +pub use codex_thread::EpiphanyJobInterruptRequest; +pub use codex_thread::EpiphanyJobLaunchRequest; pub use codex_thread::EpiphanyStateUpdate; pub use codex_thread::ThreadConfigSnapshot; pub use epiphany_distillation::EpiphanyDistillInput; diff --git a/vendor/codex/codex-rs/core/src/tools/handlers/agent_jobs.rs b/vendor/codex/codex-rs/core/src/tools/handlers/agent_jobs.rs index e3b00960..cd6d5f4a 100644 --- a/vendor/codex/codex-rs/core/src/tools/handlers/agent_jobs.rs +++ b/vendor/codex/codex-rs/core/src/tools/handlers/agent_jobs.rs @@ -98,7 +98,7 @@ struct ReportAgentJobResultToolResult { } #[derive(Debug, Clone)] -struct JobRunnerOptions { +pub(crate) struct JobRunnerOptions { max_concurrency: usize, spawn_config: Config, } @@ -519,7 +519,7 @@ fn required_state_db( }) } -async fn build_runner_options( +pub(crate) async fn build_runner_options( session: &Arc, turn: &Arc, requested_concurrency: Option, @@ -564,7 +564,7 @@ fn normalize_max_runtime_seconds(requested: Option) -> Result, Ok(Some(requested)) } -async fn run_agent_job_loop( +pub(crate) async fn run_agent_job_loop( session: Arc, turn: Arc, db: Arc,