feat(app-server): add history_mode to thread (#29927)

## Description

This PR adds a new `historyMode = "legacy" | "paginated"` to `Thread`.
This will be stored in `SessionMeta` in the JSONL rollout file and as a
new column in the SQLite thread_metadata table, and exposed on
`thread/start` and on the `Thread` object in app-server.

## What changed

- Added canonical `ThreadHistoryMode` with `legacy` and `paginated`,
defaulting old and new SessionMeta to `legacy`.
- Carried `history_mode` through core session config, ThreadStore stored
metadata, local/in-memory stores, rollout metadata extraction, and the
existing SQLite `threads` table.
- Added experimental `historyMode` to app-server v2 `Thread` and
`thread/start`.
- Made paginated stored threads metadata-discoverable but unsupported
for legacy full-history reads, `load_history`, live resume, and create
paths.
- Regenerated app-server schema fixtures and added
protocol/state/thread-store/app-server coverage for persistence and
fail-closed behavior.

## Compatibility floor
Because users may be running various versions of Codex binaries on the
same machine (TUI, Codex App, etc.), we will need to establish a
compatibility floor for upcoming paginated threads, which will change
how thread storage reads and writes work.

The overall plan here:
```
Release N:
- Add historyMode to SessionMeta / Thread / SQLite metadata.
- Teach binaries to understand paginated threads.
- If a binary sees `historyMode="paginated"` but does not support the paginated contract, it refuses to resume/mutate the thread.
- Default remains `"legacy"`.

Release N+1:
- First-party clients start opting into paginated threads where appropriate.
- Internal dogfood / staged rollout.
- Measure old-client usage and paginated-thread unsupported errors.

Release N+2:
- Only after Release N+ is overwhelmingly deployed, make paginated the default.
- Accept that a small tail of N-1-or-older binaries may not understand paginated threads.
```

The important behavior change is fail-closed handling for a binary that
encounters a persisted `paginated` thread before it knows how to fully
support paginated history. In app-server, if a thread is `paginated`, we
will:

- allow metadata-only discovery paths like `thread/list` and
`thread/read(includeTurns=false)`, so clients can still see the thread
and inspect its `historyMode`
- reject legacy full-history/live-thread paths like
`thread/read(includeTurns=true)` and `thread/resume` with an unsupported
JSON-RPC error
- avoid silently treating an unknown or future `historyMode` as `legacy`

Under the hood, the ThreadStore layer also rejects legacy operations
that would need to load or replay the full thread history for a
paginated thread. That gives us the behavior we want for Release N:
future paginated threads are visible, but this binary fails closed
instead of trying to operate on them as if they were legacy threads.
This commit is contained in:
Owen Lin authored and GitHub committed 2026-06-26 09:12:42 -07:00
1 parent 2c5bc5e284
commit 5267e805fb
91 files changed
+1385 -39

No files matched your search

+38
View File
@@ -61,6 +61,7 @@ use codex_protocol::protocol::SessionContextWindow;
use codex_protocol::protocol::SessionMeta;
use codex_protocol::protocol::SessionMetaLine;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::ThreadHistoryMode;
use codex_protocol::protocol::ThreadSource;
use codex_state::StateRuntime;
use codex_utils_path as path_utils;
@@ -94,6 +95,7 @@ pub enum RolloutRecorderParams {
dynamic_tools: Vec<DynamicToolSpec>,
selected_capability_roots: Vec<SelectedCapabilityRoot>,
multi_agent_version: Option<MultiAgentVersion>,
history_mode: ThreadHistoryMode,
initial_window_id: Option<String>,
},
Resume {
@@ -186,6 +188,7 @@ impl RolloutRecorderParams {
dynamic_tools,
selected_capability_roots: Vec::new(),
multi_agent_version: None,
history_mode: Default::default(),
initial_window_id: None,
}
}
@@ -225,6 +228,16 @@ impl RolloutRecorderParams {
self
}
pub fn with_history_mode(mut self, history_mode: ThreadHistoryMode) -> Self {
if let Self::Create {
history_mode: mode, ..
} = &mut self
{
*mode = history_mode;
}
self
}
pub fn with_initial_window_id(mut self, initial_window_id: String) -> Self {
if let Self::Create {
initial_window_id: window_id,
@@ -751,6 +764,7 @@ impl RolloutRecorder {
dynamic_tools,
selected_capability_roots,
multi_agent_version,
history_mode,
initial_window_id,
} => {
let log_file_info = precompute_log_file_info(config, conversation_id)?;
@@ -789,6 +803,7 @@ impl RolloutRecorder {
},
selected_capability_roots,
memory_mode: (!config.generate_memories()).then_some("disabled".to_string()),
history_mode,
multi_agent_version,
context_window: initial_window_id.map(SessionContextWindow::new),
};
@@ -956,6 +971,12 @@ impl RolloutRecorder {
items.push(item);
}
Err(e) => {
if thread_id.is_none() {
// The first SessionMeta belongs to this rollout. Later SessionMeta lines
// can be copied from fork history, so only validate unknown history modes
// before we have parsed the rollout's own SessionMeta.
reject_unknown_thread_history_mode(&v)?;
}
trace!("failed to parse rollout line: {e}");
parse_errors = parse_errors.saturating_add(1);
}
@@ -1019,6 +1040,21 @@ impl RolloutRecorder {
}
}
pub(crate) fn reject_unknown_thread_history_mode(value: &Value) -> std::io::Result<()> {
if value.get("type").and_then(Value::as_str) != Some("session_meta") {
return Ok(());
}
let Some(history_mode) = value
.get("payload")
.and_then(|payload| payload.get("history_mode"))
else {
return Ok(());
};
serde_json::from_value::<ThreadHistoryMode>(history_mode.clone())
.map(|_| ())
.map_err(|err| IoError::other(format!("invalid session metadata history_mode: {err}")))
}
fn strip_legacy_ghost_snapshot_rollout_line(value: &mut Value) -> bool {
match value.get("type").and_then(Value::as_str) {
Some("response_item") => value
@@ -1122,6 +1158,7 @@ fn fill_missing_thread_item_metadata(item: &mut ThreadItem, state_item: ThreadIt
git_sha,
git_origin_url,
source,
history_mode: _,
parent_thread_id,
agent_nickname,
agent_role,
@@ -1837,6 +1874,7 @@ fn thread_item_from_state_metadata(
.or_else(|_| serde_json::from_value(Value::String(item.source)))
.unwrap_or(SessionSource::Unknown),
),
history_mode: item.history_mode,
parent_thread_id,
agent_nickname: item.agent_nickname,
agent_role: item.agent_role,