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

+22 -3
View File
@@ -136,6 +136,7 @@ WHERE kind = ? AND job_key = ?
/// - starts from `threads` filtered to active threads and allowed sources
/// (`push_thread_filters`)
/// - excludes threads with `memory_mode != 'enabled'`
/// - excludes paginated threads because stage 1 still full-loads rollout JSONL
/// - excludes the current thread id
/// - keeps only threads whose millisecond `updated_at` is in the age window
/// - checks memory staleness against the memories DB
@@ -177,6 +178,7 @@ SELECT
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.history_mode,
threads.thread_source,
threads.agent_path,
threads.agent_nickname,
@@ -213,7 +215,7 @@ FROM threads
},
/*include_thread_id_tiebreaker*/ false,
);
builder.push(" AND threads.memory_mode = 'enabled'");
builder.push(" AND threads.memory_mode = 'enabled' AND threads.history_mode = 'legacy'");
builder
.push(" AND threads.id != ")
.push_bind(current_thread_id.as_str());
@@ -549,6 +551,7 @@ SELECT
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.history_mode,
threads.thread_source,
threads.agent_nickname,
threads.agent_role,
@@ -569,7 +572,7 @@ SELECT
threads.git_branch,
threads.git_origin_url
FROM threads
WHERE threads.id = ? AND threads.memory_mode = 'enabled'
WHERE threads.id = ? AND threads.memory_mode = 'enabled' AND threads.history_mode = 'legacy'
"#,
)
.bind(thread_id.to_string())
@@ -1673,6 +1676,7 @@ mod tests {
use chrono::Duration;
use chrono::Utc;
use codex_protocol::ThreadId;
use codex_protocol::protocol::ThreadHistoryMode;
use pretty_assertions::assert_eq;
use sqlx::Row;
use std::sync::Arc;
@@ -2161,7 +2165,7 @@ mod tests {
}
#[tokio::test]
async fn claim_stage1_jobs_skips_threads_with_disabled_memory_mode() {
async fn claim_stage1_jobs_skips_threads_without_legacy_enabled_memory() {
let codex_home = unique_temp_dir();
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
.await
@@ -2174,6 +2178,8 @@ mod tests {
ThreadId::from_string(&Uuid::new_v4().to_string()).expect("current thread id");
let disabled_thread_id =
ThreadId::from_string(&Uuid::new_v4().to_string()).expect("disabled thread id");
let paginated_thread_id =
ThreadId::from_string(&Uuid::new_v4().to_string()).expect("paginated thread id");
let enabled_thread_id =
ThreadId::from_string(&Uuid::new_v4().to_string()).expect("enabled thread id");
@@ -2200,6 +2206,19 @@ mod tests {
.await
.expect("disable thread memory mode");
let mut paginated = test_thread_metadata(
&codex_home,
paginated_thread_id,
codex_home.join("paginated"),
);
paginated.created_at = eligible_at;
paginated.updated_at = eligible_at;
paginated.history_mode = ThreadHistoryMode::Paginated;
runtime
.upsert_thread(&paginated)
.await
.expect("upsert paginated thread");
let mut enabled =
test_thread_metadata(&codex_home, enabled_thread_id, codex_home.join("enabled"));
enabled.created_at = eligible_at;
@@ -11,6 +11,8 @@ use codex_protocol::protocol::AskForApproval;
#[cfg(test)]
use codex_protocol::protocol::SandboxPolicy;
#[cfg(test)]
use codex_protocol::protocol::ThreadHistoryMode;
#[cfg(test)]
use std::path::Path;
#[cfg(test)]
use std::path::PathBuf;
@@ -49,6 +51,7 @@ pub(super) fn test_thread_metadata(
updated_at: now,
recency_at: now,
source: "cli".to_string(),
history_mode: ThreadHistoryMode::Legacy,
thread_source: None,
agent_nickname: None,
agent_role: None,
+36 -2
View File
@@ -15,6 +15,7 @@ SELECT
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.history_mode,
threads.thread_source,
threads.agent_nickname,
threads.agent_role,
@@ -544,6 +545,7 @@ INSERT INTO threads (
updated_at_ms,
recency_at_ms,
source,
history_mode,
thread_source,
agent_nickname,
agent_role,
@@ -565,7 +567,7 @@ INSERT INTO threads (
git_branch,
git_origin_url,
memory_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING
"#,
)
@@ -578,6 +580,7 @@ ON CONFLICT(id) DO NOTHING
.bind(datetime_to_epoch_millis(updated_at))
.bind(datetime_to_epoch_millis(recency_at))
.bind(metadata.source.as_str())
.bind(metadata.history_mode.as_str())
.bind(
metadata
.thread_source
@@ -796,6 +799,7 @@ INSERT INTO threads (
updated_at_ms,
recency_at_ms,
source,
history_mode,
thread_source,
agent_nickname,
agent_role,
@@ -817,7 +821,7 @@ INSERT INTO threads (
git_branch,
git_origin_url,
memory_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
rollout_path = excluded.rollout_path,
created_at = excluded.created_at,
@@ -827,6 +831,7 @@ ON CONFLICT(id) DO UPDATE SET
updated_at_ms = excluded.updated_at_ms,
recency_at_ms = threads.recency_at_ms,
source = excluded.source,
history_mode = excluded.history_mode,
thread_source = excluded.thread_source,
agent_nickname = excluded.agent_nickname,
agent_role = excluded.agent_role,
@@ -858,6 +863,7 @@ ON CONFLICT(id) DO UPDATE SET
.bind(datetime_to_epoch_millis(updated_at))
.bind(datetime_to_epoch_millis(insert_recency_at))
.bind(metadata.source.as_str())
.bind(metadata.history_mode.as_str())
.bind(
metadata
.thread_source
@@ -1206,6 +1212,7 @@ SELECT
threads.updated_at_ms AS updated_at,
threads.recency_at_ms AS recency_at,
threads.source,
threads.history_mode,
threads.thread_source,
threads.agent_nickname,
threads.agent_role,
@@ -1419,6 +1426,7 @@ mod tests {
use codex_protocol::protocol::SessionMeta;
use codex_protocol::protocol::SessionMetaLine;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::ThreadHistoryMode;
use pretty_assertions::assert_eq;
use serde_json::json;
use std::path::PathBuf;
@@ -1461,6 +1469,30 @@ mod tests {
assert_eq!(memory_mode, "disabled");
}
#[tokio::test]
async fn thread_metadata_round_trips_history_mode() {
let codex_home = unique_temp_dir();
let runtime = StateRuntime::init(codex_home.clone(), "test-provider".to_string())
.await
.expect("state db should initialize");
let thread_id =
ThreadId::from_string("00000000-0000-0000-0000-000000000124").expect("valid thread id");
let mut metadata = test_thread_metadata(&codex_home, thread_id, codex_home.clone());
metadata.history_mode = ThreadHistoryMode::Paginated;
runtime
.upsert_thread(&metadata)
.await
.expect("upsert should succeed");
let metadata = runtime
.get_thread(thread_id)
.await
.expect("thread should load")
.expect("thread should exist");
assert_eq!(metadata.history_mode, ThreadHistoryMode::Paginated);
}
#[tokio::test]
async fn delete_thread_cleans_associated_state() -> Result<()> {
let codex_home = unique_temp_dir();
@@ -2150,6 +2182,7 @@ mod tests {
dynamic_tools: None,
selected_capability_roots: Vec::new(),
memory_mode: Some("polluted".to_string()),
history_mode: Default::default(),
multi_agent_version: None,
context_window: None,
},
@@ -2214,6 +2247,7 @@ mod tests {
dynamic_tools: None,
selected_capability_roots: Vec::new(),
memory_mode: None,
history_mode: Default::default(),
multi_agent_version: None,
context_window: None,
},