From 5a1318694ddc412335268674202e5287a23637be Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 30 Apr 2026 09:31:56 +0100 Subject: [PATCH] Use current context for CRRC pressure --- notes/epiphany-current-algorithmic-map.md | 2 +- notes/epiphany-fork-implementation-plan.md | 7 +- notes/fresh-workspace-handoff.md | 4 +- state/evidence.jsonl | 1 + state/map.yaml | 8 +- .../app-server/src/codex_message_processor.rs | 182 +++++++++++------- 6 files changed, 120 insertions(+), 84 deletions(-) diff --git a/notes/epiphany-current-algorithmic-map.md b/notes/epiphany-current-algorithmic-map.md index 5f3c19d2..731ea004 100644 --- a/notes/epiphany-current-algorithmic-map.md +++ b/notes/epiphany-current-algorithmic-map.md @@ -29,7 +29,7 @@ The spine exists in twenty-five live paths: - 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). - graph traversal reflection: app-server declares experimental read-only `thread/epiphany/graphQuery`, reads the same live Epiphany state view used by context, and derives explicit node/edge lookup, path/symbol matches, bounded neighbors, edge-kind matches, and frontier neighborhoods with architecture/dataflow links plus frontier/checkpoint identity, matched selectors, and missing ids without retrieval, proposal, promotion, indexing, notification, mutation, or scheduling in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:379), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4720), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:5060), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:15201). -- 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). +- 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`, derives current context pressure from `last_token_usage` rather than cumulative thread spend, treats cumulative-only telemetry as unknown, and raises `shouldPrepareCompaction` at 80% of the active auto-compact/context limit 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). - pre-compaction checkpoint intervention: app-server `TokenCount` handling now checks the same pressure mapper, and for loaded Epiphany threads crossing `shouldPrepareCompaction` it steers the active turn once with a bounded CRRC checkpoint directive to bank working context before compaction/reorientation. The per-turn dedupe bit and the pending compact handoff live in listener `ThreadState`; successful steering marks the active turn for a forced compact action at clean turn completion, while aborted turns clear the handoff. The intervention uses existing turn steering and turn-boundary automation rather than a new scheduler in [bespoke_event_handling.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/bespoke_event_handling.rs:1467), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:15405), and [thread_state.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/thread_state.rs:144). - reorientation policy: app-server declares experimental read-only `thread/epiphany/reorient`, reuses the same live/stored thread view plus mapped pressure/freshness/watcher signals, and derives a bounded `resume`/`regather` verdict with checkpoint-path/frontier reasons without mutating, compacting, scheduling, notifying, or continuing work in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:365), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4514), [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4394), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:11424). - CRRC coordinator recommendation: app-server declares experimental read-only `thread/epiphany/crrc`, reuses the same pressure/freshness/watcher/reorientation mapping plus the fixed `reorient-worker` binding and result read-back helper, recognizes already accepted reorientation findings so an accepted result is not recommended for acceptance again, and returns one bounded recommendation such as continue, prepare checkpoint, launch worker, wait, review, accept, or regather manually without mutating, launching, accepting, compacting, notifying, scheduling, or silently continuing work in [common.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/common.rs:370), [v2.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server-protocol/src/protocol/v2.rs:4661), and [codex_message_processor.rs](E:/Projects/EpiphanyAgent/vendor/codex/codex-rs/app-server/src/codex_message_processor.rs:4546). diff --git a/notes/epiphany-fork-implementation-plan.md b/notes/epiphany-fork-implementation-plan.md index 6ded3b86..492c1253 100644 --- a/notes/epiphany-fork-implementation-plan.md +++ b/notes/epiphany-fork-implementation-plan.md @@ -113,7 +113,7 @@ These boundaries are more important than the individual method names: - `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/graphQuery` is bounded graph traversal over authoritative typed state, not retrieval, proposal, promotion, scheduling, indexing, or a state writer. -- `thread/epiphany/pressure` is a context-pressure reflection, not an automatic compactor, scheduler, or CRRC coordinator. +- `thread/epiphany/pressure` is a current-context pressure reflection, not cumulative spend accounting, an automatic compactor, scheduler, or CRRC coordinator. - `thread/epiphany/reorient` is a bounded policy verdict, not an automatic runtime coordinator, scheduler, compactor, or hidden state writer. - `thread/epiphany/crrc` is a read-only coordinator recommendation over existing signals, not a scheduler, launch button, acceptance gate, compactor, or hidden state writer. - `thread/epiphany/coordinator` is a read-only fixed-lane MVP policy projection over existing signals, not a scheduler, launcher, acceptance gate, compactor, or hidden state writer. @@ -227,8 +227,9 @@ pre-compaction checkpoint steering now also latches a pending compact handoff for the completed turn, so the brake becomes a real compaction request instead of a polite suggestion. The first pre-compaction checkpoint intervention is now also landed: token-count pressure -events steer active loaded Epiphany turns once at the `shouldPrepareCompaction` -threshold so working context can be banked before compaction. The next MVP +events steer active loaded Epiphany turns once when current context usage reaches +80% of the active auto-compact/context limit, ignoring cumulative token spend so +CRRC does not yell at clouds. The next MVP question is sharper: dogfood the coordinator/status/pre-compaction loop and fix only concrete blockers. The latest self-dogfood pass found one: the dogfood runner's manifest promised vanilla/comparison artifacts that were not actually diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 89e6616d..71047629 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -32,7 +32,7 @@ Do not trust this file for the exact live HEAD. Always check git. - `tools/epiphany_mvp_status.py` is the first dogfood operator view. It starts or reads a thread through app-server and prints scene, pressure, reorient, jobs, roles, modeling/verification role result read-backs, reorient result, and CRRC recommendation as text or JSON. - `tools/epiphany_mvp_coordinator.py` is the first auditable fixed-lane coordinator runner. It starts or reads a thread through app-server, follows the harness-native coordinator action, can auto-launch modeling, verification, or reorient-worker jobs, keeps semantic findings review-gated by default, and writes summary, steps, rendered snapshots, transcript, stderr, and final next-action artifacts under `.epiphany-dogfood/coordinator` or a caller-provided artifact directory. - Native CRRC automation is now landed only at turn-complete safe boundaries. It may submit `Op::Compact` for coordinator-approved `compactRehydrateReorient` or for a successful pre-compaction checkpoint intervention's pending compact handoff, and it may launch the fixed `reorient-worker` for coordinator-approved `launchReorientWorker`. It does not auto-launch modeling/verification, accept findings, promote evidence, edit implementation code, or keep going after reviewable semantic output. -- Pre-compaction checkpoint intervention is now landed. On token-count events for loaded Epiphany threads, when pressure reaches the existing `shouldPrepareCompaction` threshold, the harness steers the active turn once with a CRRC checkpoint directive so the agent banks working context before compaction/reorientation. A successful steer now latches a turn-scoped compact handoff that is consumed at clean turn completion, preventing the brake from decaying into another implementation turn. This is still bounded steering plus compaction handoff, not automatic semantic acceptance, a broad scheduler, or implementation continuation. +- Pre-compaction checkpoint intervention is now landed. On token-count events for loaded Epiphany threads, when current context usage reaches 80% of the active auto-compact/context limit, the harness steers the active turn once with a CRRC checkpoint directive so the agent banks working context before compaction/reorientation. Pressure ignores cumulative token spend; cumulative-only telemetry reports unknown instead of yelling. A successful steer now latches a turn-scoped compact handoff that is consumed at clean turn completion, preventing the brake from decaying into another implementation turn. This is still bounded steering plus compaction handoff, not automatic semantic acceptance, a broad scheduler, or implementation continuation. - `tools/epiphany_mvp_dogfood.py` is the first auditable dogfood runner. It drives a bounded state/role/CRRC/reorientation loop and writes local artifacts under `.epiphany-dogfood/mvp-loop`, including sealed app-server transcript diagnostics, rendered snapshots, operator-safe final status, vanilla-reference prompt/output, and comparison notes. The runner now writes truthful vanilla/comparison artifacts whether the optional vanilla reference is skipped, fails, or completes. - `tools/epiphany_mvp_live_specialist.py` is the first auditable live-specialist runner. It launches a real role specialist through `thread/epiphany/roleLaunch`, lets the spawned worker report through `report_agent_job_result`, reads it back through `thread/epiphany/roleResult`, and writes local artifacts under `.epiphany-dogfood/live-specialist`. - The first MVP GUI shell now exists under `apps/epiphany-gui`. It is a Tauri v2 + React operator console over the existing status bridge, dogfood artifacts, and GUI action artifacts, not a new source of truth. It can prepare a durable Epiphany checkpoint for a resumable operator thread, run status snapshots and coordinator-plan artifacts, launch/read the fixed modeling and verification lanes, launch/read the fixed reorient-worker, and explicitly accept completed reorient findings after review. @@ -99,7 +99,7 @@ The current spine: - read-only retrieval/graph freshness reflection plus watcher-backed invalidation inputs through `thread/epiphany/freshness` - read-only targeted state-shard reflection through `thread/epiphany/context` - read-only graph traversal through `thread/epiphany/graphQuery` -- read-only context-pressure reflection through `thread/epiphany/pressure` +- read-only current-context pressure reflection through `thread/epiphany/pressure` - read-only CRRC reorientation policy through `thread/epiphany/reorient` - read-only CRRC next-action recommendation through `thread/epiphany/crrc` - read-only fixed-lane MVP action recommendation through `thread/epiphany/coordinator` diff --git a/state/evidence.jsonl b/state/evidence.jsonl index a1525e0c..15807b8a 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -43,3 +43,4 @@ {"ts": "2026-04-30T05:47:56+00:00", "type": "implementation", "status": "ok", "note": "The GUI now treats a newest no-diff implementation artifact as a soft operator gate: Continue Implementation is paused until the operator reviews or runs another lane, preventing immediate repeated no-diff pseudo-progress from the console."} {"ts": "2026-04-30T05:54:22+00:00", "type": "verification", "status": "ok", "note": "Stateful no-diff implementation handling landed and was exercised on the Aetheria dogfood thread: continueImplementation-1777528376766401900-25000 produced no tracked diff, thread/epiphany/update marked the investigation checkpoint regather_required with audit evidence, advanced Epiphany state to revision 17, and a follow-up coordinator plan returned regatherManually with implementation blocked."} {"ts": "2026-04-30T08:18:46+00:00", "type": "implementation", "status": "ok", "note": "CRRC pre-compaction steering now creates a turn-scoped pending compact handoff after successful steer_input, and clean turn completion forces the existing compact automation even if the ordinary coordinator would continue; aborted turns clear the handoff. Focused Rust tests, coordinator smoke, and reorient-launch smoke passed."} +{"ts": "2026-04-30T08:31:23+00:00", "type": "implementation", "status": "ok", "note": "CRRC pressure now uses current context usage from last token telemetry instead of cumulative thread token spend, treats cumulative-only telemetry as unknown, and starts pre-compaction preparation at 80% of the active auto-compact/context limit. Focused pressure/coordinator tests plus pressure and coordinator smokes passed."} diff --git a/state/map.yaml b/state/map.yaml index 75bbb6c6..8dc74a5a 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -90,7 +90,7 @@ accepted_design: - `thread/epiphany/freshness` is the first read-only freshness surface: it derives retrieval and graph staleness from live retrieval summaries plus frontier/churn state, and for loaded threads reflects watcher-backed invalidation inputs with changed paths plus mapped graph/frontier hits, without mutating, scheduling, or performing automatic semantic invalidation. - `thread/epiphany/context` is the first targeted read-only state-shard surface: it selects graph nodes/edges, active frontier, checkpoint, observations, and evidence from authoritative typed state without mutating or notifying. - `thread/epiphany/graphQuery` is the first read-only graph traversal surface over authoritative typed state: it returns explicit nodes, path/symbol matches, bounded neighbors, and frontier neighborhoods with architecture/dataflow links without mutating, notifying, indexing, scheduling, proposing, or promoting. - - `thread/epiphany/pressure` is the first read-only context-pressure surface: it derives pressure from token telemetry and auto-compact/context limits without compacting, scheduling, mutating, or notifying. + - `thread/epiphany/pressure` is the first read-only context-pressure surface: it derives pressure from current context usage, not cumulative token spend, against auto-compact/context limits without compacting, scheduling, mutating, or notifying; compaction prep starts at 80% of the active limit. - `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. - `thread/epiphany/crrc` is the first bounded CRRC coordinator recommendation surface: it composes pressure, the reorientation verdict, fixed reorient-worker binding/result status, finding read-back, and available actions into one read-only recommendation without launching, accepting, compacting, scheduling, or mutating state. - A narrow auditable coordinator is likely central to the MVP because implementation, modeling/checkpoint, verification/review, and CRRC automation depend on each other's signals. CRRC itself is harness workflow automation, not a specialist-agent role: it watches pressure/continuity, ensures persistence, triggers compaction/rehydration/reorientation when needed, and may launch a bounded reorient-worker specialist when semantic regathering is required. @@ -130,7 +130,7 @@ 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, graph traversal, context-pressure, reorientation policy, and CRRC coordinator recommendation surfaces, plus durable investigation checkpointing for compaction-safe planning, live bound-job progress notifications, a thin launcher seam in durable job bindings, explicit launch/interrupt authority over that seam, and a bounded reorient-guided worker launch surface. + - Phase 6: read-only scene, job/progress, freshness with watcher-backed invalidation inputs, targeted context, graph traversal, current-context pressure, reorientation policy, and CRRC coordinator recommendation surfaces, plus durable investigation checkpointing for compaction-safe planning, live bound-job progress notifications, a thin launcher seam in durable job bindings, explicit launch/interrupt authority over that seam, and a bounded reorient-guided worker launch surface. - Phase 6 read-back: read-only reorient-worker result reflection through `thread/epiphany/reorientResult`. - Phase 6 acceptance: explicit reorient-worker finding acceptance through `thread/epiphany/reorientAccept`. - Phase 6 dogfood view: first CLI operator status over scene/pressure/reorient/jobs/result/CRRC. @@ -180,5 +180,5 @@ rejected_paths: reason: persistent state that accumulates every local truth becomes memory cruft and weakens rehydration current_status: - summary: Phase 1 durable state through the Phase 6 GUI/coordinator/specialist/CRRC MVP surfaces are landed. Aetheria dogfood first exposed a supervisor contamination scar, so the quarantine now keeps the supervisor on operator-safe projections and sealed transcripts. The clean supervised pass proved the CRRC compact/reorient/reorientAccept lane can reach state revision 16 and clear to `continueImplementation`; the follow-up implementation pass produced no tracked Aetheria diff, wrote audit artifacts, marked the investigation checkpoint `regather_required`, advanced state to revision 17, and caused the coordinator to block implementation with `regatherManually` instead of fake progress. A follow-up harness repair now turns successful CRRC pre-compaction steering into a pending compact handoff consumed at clean turn completion, so the brake cannot silently decay into another implementation turn. - next_action: Retry Aetheria dogfood from the operator-safe coordinator lanes after the pre-compaction handoff repair. Start with a bounded regather/modeling repair for the `regather_required` checkpoint, then retry implementation through the GUI/coordinator path and require either a tracked Aetheria diff or an explicit reviewable failure artifact. Do not inspect raw worker transcripts or `rawResult` payloads unless the user explicitly requests forensic debugging. + summary: Phase 1 durable state through the Phase 6 GUI/coordinator/specialist/CRRC MVP surfaces are landed. Aetheria dogfood first exposed a supervisor contamination scar, so the quarantine now keeps the supervisor on operator-safe projections and sealed transcripts. The clean supervised pass proved the CRRC compact/reorient/reorientAccept lane can reach state revision 16 and clear to `continueImplementation`; the follow-up implementation pass produced no tracked Aetheria diff, wrote audit artifacts, marked the investigation checkpoint `regather_required`, advanced state to revision 17, and caused the coordinator to block implementation with `regatherManually` instead of fake progress. A follow-up harness repair now turns successful CRRC pre-compaction steering into a pending compact handoff consumed at clean turn completion, and the pressure gauge now ignores cumulative token spend in favor of current context usage with an 80% prep threshold. + next_action: Retry Aetheria dogfood from the operator-safe coordinator lanes after the pre-compaction handoff and pressure-gauge repairs. Start with a bounded regather/modeling repair for the `regather_required` checkpoint, then retry implementation through the GUI/coordinator path and require either a tracked Aetheria diff or an explicit reviewable failure artifact. Do not inspect raw worker transcripts or `rawResult` payloads unless the user explicitly requests forensic debugging. 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 9c309ccb..5b439a76 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 @@ -14899,6 +14899,10 @@ fn pressure_level_label(level: ThreadEpiphanyPressureLevel) -> &'static str { } } +const EPIPHANY_PRESSURE_ELEVATED_PER_MILLE: u32 = 650; +const EPIPHANY_PRESSURE_PREPARE_COMPACTION_PER_MILLE: u32 = 800; +const EPIPHANY_PRESSURE_CRITICAL_PER_MILLE: u32 = 950; + fn retrieval_freshness_status_label( status: ThreadEpiphanyRetrievalFreshnessStatus, ) -> &'static str { @@ -15256,7 +15260,21 @@ fn map_epiphany_pressure(info: Option<&CoreTokenUsageInfo>) -> ThreadEpiphanyPre }; }; - let used_tokens = info.total_token_usage.total_tokens.max(0); + let used_tokens = info.last_token_usage.total_tokens.max(0); + if used_tokens == 0 && info.total_token_usage.total_tokens > 0 { + return ThreadEpiphanyPressure { + status: ThreadEpiphanyPressureStatus::Unknown, + level: ThreadEpiphanyPressureLevel::Unknown, + basis: ThreadEpiphanyPressureBasis::Unknown, + used_tokens: None, + model_context_window: info.model_context_window, + model_auto_compact_token_limit: info.model_auto_compact_token_limit, + remaining_tokens: None, + ratio_per_mille: None, + should_prepare_compaction: false, + note: "Only cumulative token spend is available; CRRC will not infer current context pressure from that.".to_string(), + }; + } let model_context_window = info.model_context_window.filter(|value| *value > 0); let model_auto_compact_token_limit = info .model_auto_compact_token_limit @@ -15280,7 +15298,7 @@ fn map_epiphany_pressure(info: Option<&CoreTokenUsageInfo>) -> ThreadEpiphanyPre remaining_tokens: None, ratio_per_mille: None, should_prepare_compaction: false, - note: "Token usage is known, but no context window or auto-compact threshold is available." + note: "Current context usage is known, but no context window or auto-compact threshold is available." .to_string(), }; }; @@ -15288,21 +15306,28 @@ fn map_epiphany_pressure(info: Option<&CoreTokenUsageInfo>) -> ThreadEpiphanyPre let ratio_per_mille = ((used_tokens.saturating_mul(1000)) / limit).max(0) as u32; let remaining_tokens = limit.saturating_sub(used_tokens); let level = match ratio_per_mille { - ratio if ratio >= 1000 => ThreadEpiphanyPressureLevel::Critical, - ratio if ratio >= 900 => ThreadEpiphanyPressureLevel::High, - ratio if ratio >= 750 => ThreadEpiphanyPressureLevel::Elevated, + ratio if ratio >= EPIPHANY_PRESSURE_CRITICAL_PER_MILLE => { + ThreadEpiphanyPressureLevel::Critical + } + ratio if ratio >= EPIPHANY_PRESSURE_PREPARE_COMPACTION_PER_MILLE => { + ThreadEpiphanyPressureLevel::High + } + ratio if ratio >= EPIPHANY_PRESSURE_ELEVATED_PER_MILLE => { + ThreadEpiphanyPressureLevel::Elevated + } _ => ThreadEpiphanyPressureLevel::Low, }; - let should_prepare_compaction = ratio_per_mille >= 900; + let should_prepare_compaction = + ratio_per_mille >= EPIPHANY_PRESSURE_PREPARE_COMPACTION_PER_MILLE; let note = match basis { ThreadEpiphanyPressureBasis::AutoCompactLimit => { - "Pressure is derived from the model auto-compact token limit.".to_string() + "Pressure is derived from current context usage against the model auto-compact token limit.".to_string() } ThreadEpiphanyPressureBasis::ModelContextWindow => { - "Pressure is derived from the model context window because no auto-compact threshold was recorded.".to_string() + "Pressure is derived from current context usage against the model context window because no auto-compact threshold was recorded.".to_string() } ThreadEpiphanyPressureBasis::Unknown => { - "Token usage is known, but no usable pressure limit was recorded.".to_string() + "Current context usage is known, but no usable pressure limit was recorded.".to_string() } }; @@ -17417,6 +17442,26 @@ mod tests { use std::sync::Arc; use tempfile::TempDir; + fn core_token_usage_info( + cumulative_tokens: i64, + current_context_tokens: i64, + model_context_window: Option, + model_auto_compact_token_limit: Option, + ) -> CoreTokenUsageInfo { + CoreTokenUsageInfo { + total_token_usage: codex_protocol::protocol::TokenUsage { + total_tokens: cumulative_tokens, + ..Default::default() + }, + last_token_usage: codex_protocol::protocol::TokenUsage { + total_tokens: current_context_tokens, + ..Default::default() + }, + model_context_window, + model_auto_compact_token_limit, + } + } + #[test] fn validate_dynamic_tools_rejects_unsupported_input_schema() { let tools = vec![ApiDynamicToolSpec { @@ -18800,15 +18845,12 @@ mod tests { #[test] fn map_epiphany_pressure_prefers_auto_compact_limit() { - let pressure = map_epiphany_pressure(Some(&CoreTokenUsageInfo { - total_token_usage: codex_protocol::protocol::TokenUsage { - total_tokens: 92, - ..Default::default() - }, - last_token_usage: codex_protocol::protocol::TokenUsage::default(), - model_context_window: Some(200), - model_auto_compact_token_limit: Some(100), - })); + let pressure = map_epiphany_pressure(Some(&core_token_usage_info( + 1_000, + 82, + Some(200), + Some(100), + ))); assert_eq!(pressure.status, ThreadEpiphanyPressureStatus::Ready); assert_eq!(pressure.level, ThreadEpiphanyPressureLevel::High); @@ -18816,22 +18858,38 @@ mod tests { pressure.basis, ThreadEpiphanyPressureBasis::AutoCompactLimit ); - assert_eq!(pressure.ratio_per_mille, Some(920)); - assert_eq!(pressure.remaining_tokens, Some(8)); + assert_eq!(pressure.ratio_per_mille, Some(820)); + assert_eq!(pressure.remaining_tokens, Some(18)); assert!(pressure.should_prepare_compaction); } + #[test] + fn map_epiphany_pressure_ignores_cumulative_thread_spend() { + let pressure = + map_epiphany_pressure(Some(&core_token_usage_info(310, 70, Some(200), Some(100)))); + + assert_eq!(pressure.status, ThreadEpiphanyPressureStatus::Ready); + assert_eq!(pressure.level, ThreadEpiphanyPressureLevel::Elevated); + assert_eq!(pressure.used_tokens, Some(70)); + assert_eq!(pressure.ratio_per_mille, Some(700)); + assert!(!pressure.should_prepare_compaction); + } + + #[test] + fn map_epiphany_pressure_refuses_cumulative_only_telemetry() { + let pressure = + map_epiphany_pressure(Some(&core_token_usage_info(310, 0, Some(200), Some(100)))); + + assert_eq!(pressure.status, ThreadEpiphanyPressureStatus::Unknown); + assert_eq!(pressure.level, ThreadEpiphanyPressureLevel::Unknown); + assert_eq!(pressure.ratio_per_mille, None); + assert!(!pressure.should_prepare_compaction); + } + #[test] fn map_epiphany_pressure_falls_back_to_context_window() { - let pressure = map_epiphany_pressure(Some(&CoreTokenUsageInfo { - total_token_usage: codex_protocol::protocol::TokenUsage { - total_tokens: 150, - ..Default::default() - }, - last_token_usage: codex_protocol::protocol::TokenUsage::default(), - model_context_window: Some(200), - model_auto_compact_token_limit: None, - })); + let pressure = + map_epiphany_pressure(Some(&core_token_usage_info(1_000, 150, Some(200), None))); assert_eq!(pressure.status, ThreadEpiphanyPressureStatus::Ready); assert_eq!(pressure.level, ThreadEpiphanyPressureLevel::Elevated); @@ -18845,24 +18903,9 @@ mod tests { #[test] fn pre_compaction_checkpoint_intervention_uses_compaction_prep_threshold() { - let elevated = map_epiphany_pressure(Some(&CoreTokenUsageInfo { - total_token_usage: codex_protocol::protocol::TokenUsage { - total_tokens: 89, - ..Default::default() - }, - last_token_usage: codex_protocol::protocol::TokenUsage::default(), - model_context_window: None, - model_auto_compact_token_limit: Some(100), - })); - let high = map_epiphany_pressure(Some(&CoreTokenUsageInfo { - total_token_usage: codex_protocol::protocol::TokenUsage { - total_tokens: 90, - ..Default::default() - }, - last_token_usage: codex_protocol::protocol::TokenUsage::default(), - model_context_window: None, - model_auto_compact_token_limit: Some(100), - })); + let elevated = + map_epiphany_pressure(Some(&core_token_usage_info(1_000, 79, None, Some(100)))); + let high = map_epiphany_pressure(Some(&core_token_usage_info(1_000, 80, None, Some(100)))); assert!(!should_run_epiphany_pre_compaction_checkpoint_intervention( &elevated @@ -18953,15 +18996,12 @@ mod tests { ), ..Default::default() }; - let pressure = map_epiphany_pressure(Some(&CoreTokenUsageInfo { - total_token_usage: codex_protocol::protocol::TokenUsage { - total_tokens: 40, - ..Default::default() - }, - last_token_usage: codex_protocol::protocol::TokenUsage::default(), - model_context_window: Some(200), - model_auto_compact_token_limit: Some(100), - })); + let pressure = map_epiphany_pressure(Some(&core_token_usage_info( + 1_000, + 40, + Some(200), + Some(100), + ))); let retrieval = ThreadEpiphanyRetrievalFreshness { status: ThreadEpiphanyRetrievalFreshnessStatus::Ready, semantic_available: Some(true), @@ -19603,15 +19643,12 @@ mod tests { #[test] fn map_epiphany_coordinator_compacts_at_pressure_threshold() { - let pressure = map_epiphany_pressure(Some(&CoreTokenUsageInfo { - total_token_usage: codex_protocol::protocol::TokenUsage { - total_tokens: 92, - ..Default::default() - }, - last_token_usage: codex_protocol::protocol::TokenUsage::default(), - model_context_window: Some(200), - model_auto_compact_token_limit: Some(100), - })); + let pressure = map_epiphany_pressure(Some(&core_token_usage_info( + 1_000, + 80, + Some(200), + Some(100), + ))); let recommendation = ThreadEpiphanyCrrcRecommendation { action: ThreadEpiphanyCrrcAction::Continue, recommended_scene_action: Some(ThreadEpiphanySceneAction::Reorient), @@ -19649,15 +19686,12 @@ mod tests { #[test] fn map_epiphany_coordinator_does_not_recompact_after_accepted_resume_reorient() { - let pressure = map_epiphany_pressure(Some(&CoreTokenUsageInfo { - total_token_usage: codex_protocol::protocol::TokenUsage { - total_tokens: 92, - ..Default::default() - }, - last_token_usage: codex_protocol::protocol::TokenUsage::default(), - model_context_window: Some(200), - model_auto_compact_token_limit: Some(100), - })); + let pressure = map_epiphany_pressure(Some(&core_token_usage_info( + 1_000, + 80, + Some(200), + Some(100), + ))); let recommendation = ThreadEpiphanyCrrcRecommendation { action: ThreadEpiphanyCrrcAction::Continue, recommended_scene_action: Some(ThreadEpiphanySceneAction::Reorient),