Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions loopx/control_plane/quota/native_context_lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,37 @@ export function updateNativeContextLifecycle(request: JsonObject): JsonObject {
if (same && event.source === "resume" && request.resume_verified === true
&& !state.pending && candidate?.basis === NATIVE_CONTEXT_BASIS
&& candidate.capture_bytes_verified === true) return state;
if (same && event.source === "resume" && fileEpochCurrent
&& !(candidate?.native_resume || (state.candidate && !candidate))) {
// Resuming a session is not compaction. File comparison does not depend
// on model retention; retire that separate assumption when unverified.
// A previously qualified history checkpoint that now fails can include
// an unreported compaction/rollback, so conservatively rotate below.
delete state.candidate;
delete state.pending;
delete state.active_call;
delete state.section_reads;
// Selected outputs can establish their own literal sections on a
// qualified replay. They never establish a whole-input candidate.
const readScope = jsonObject(state.read_scope);
const selectedResume = jsonObject(state.selected_resume);
const readScopeCurrent = readScope?.session_id === session && readScope.context_epoch === state.context_epoch;
const selectedResumeCurrent = readScopeCurrent && typeof selectedResume?.epoch === "string"
&& selectedResume.epoch === state.context_epoch;
if (same && event.source === "resume" && (fileEpochCurrent || selectedResumeCurrent)) {
// A selection has its own replay checkpoint, including after a failed
// full replay leaves the recovery file in the old epoch.
const previous = jsonObject(state.consumed_pointers);
const replayed = jsonObject(request.resume_sections);
const retained = Object.fromEntries(Object.entries(replayed ?? {}).filter(([address, digest]) =>
typeof digest === "string" && previous?.[address] === digest));
if (Object.keys(retained).length > 0) {
if (candidate?.native_resume || (state.candidate && !candidate)) {
// Failed complete replay may conceal compaction. Rotate the file
// epoch, keeping only the independently verified literal selections.
invalidate();
} else {
// Ordinary resume alone is not compaction or proof of model retention.
delete state.candidate;
delete state.pending;
delete state.active_call;
delete state.section_reads;
}
if (readScopeCurrent && Object.keys(retained).length > 0) {
// Only the replay-verified sections move to the new epoch. The next
// projection still matches the rest of the scope and each field's digest.
state.read_scope = {...readScope, context_epoch: state.context_epoch};
state.consumed_pointers = retained;
state.confirmed_read_sections = Object.keys(retained);
if (selectedResumeCurrent)
state.selected_resume = {...selectedResume, scope: state.read_scope, epoch: state.context_epoch};
} else {
delete state.consumed_pointers;
delete state.confirmed_read_sections;
Expand Down
7 changes: 4 additions & 3 deletions loopx/control_plane/turn_driver/codex_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def codex_context_hook(directory: str | None, event: dict[str, Any]) -> dict[str
from ..effect_runtime import EffectRuntimeRemoteError, EffectRuntimeStartupError
from .codex_context_history import (
checkpoint_native_read, checkpoint_native_selection, native_resume_sections, native_resume_verified,
observe_native_read,
observe_completed_native_reread, observe_native_read,
)

if directory is None:
Expand Down Expand Up @@ -414,8 +414,9 @@ def codex_context_hook(directory: str | None, event: dict[str, Any]) -> dict[str
resume_verified=resume_verified,
resume_sections=native_resume_sections(state, event) if name == "SessionStart" and not resume_verified else None)
if name == "PostToolUse" and same:
checkpoint_native_read(read_state, updated, event,
[page for page in pages if "read_sections" not in page])
full_pages = [page for page in pages if "read_sections" not in page]
checkpoint_native_read(read_state, updated, event, full_pages)
observe_completed_native_reread(read_state, updated, event, full_pages)
checkpoint_native_selection(state, updated, event, raw_pages, pages)
previous = updated.get("host_execution_preparation")
ready = previous.get("ready_capture") if isinstance(previous, dict) else None
Expand Down
Loading
Loading