Persist thread artifacts in runtime store

This commit is contained in:
Jeremy Rose
2026-05-14 16:44:25 -07:00
parent 76814a24be
commit 4a72c41ed3
85 changed files with 1763 additions and 1451 deletions
+49
View File
@@ -14,13 +14,16 @@ use codex_protocol::protocol::SandboxPolicy;
use crate::AppendThreadItemsParams;
use crate::ArchiveThreadParams;
use crate::CreateThreadArtifactParams;
use crate::CreateThreadParams;
use crate::ListThreadArtifactsParams;
use crate::ListThreadsParams;
use crate::LoadThreadHistoryParams;
use crate::ReadThreadByRolloutPathParams;
use crate::ReadThreadParams;
use crate::ResumeThreadParams;
use crate::StoredThread;
use crate::StoredThreadArtifact;
use crate::StoredThreadHistory;
use crate::ThreadPage;
use crate::ThreadStore;
@@ -107,6 +110,8 @@ pub struct InMemoryThreadStoreCalls {
pub read_thread: usize,
pub read_thread_by_rollout_path: usize,
pub list_threads: usize,
pub list_thread_artifacts: usize,
pub create_thread_artifact: usize,
pub update_thread_metadata: usize,
pub archive_thread: usize,
pub unarchive_thread: usize,
@@ -128,6 +133,7 @@ struct InMemoryThreadStoreState {
created_threads: HashMap<ThreadId, CreateThreadParams>,
histories: HashMap<ThreadId, Vec<RolloutItem>>,
names: HashMap<ThreadId, Option<String>>,
artifacts: HashMap<ThreadId, Vec<StoredThreadArtifact>>,
rollout_paths: HashMap<PathBuf, ThreadId>,
}
@@ -265,6 +271,36 @@ impl ThreadStore for InMemoryThreadStore {
})
}
async fn list_thread_artifacts(
&self,
params: ListThreadArtifactsParams,
) -> ThreadStoreResult<Vec<StoredThreadArtifact>> {
let mut state = self.state.lock().await;
state.calls.list_thread_artifacts += 1;
Ok(state
.artifacts
.get(&params.thread_id)
.cloned()
.unwrap_or_default())
}
async fn create_thread_artifact(
&self,
params: CreateThreadArtifactParams,
) -> ThreadStoreResult<StoredThreadArtifact> {
let mut state = self.state.lock().await;
state.calls.create_thread_artifact += 1;
if !state.created_threads.contains_key(&params.thread_id) {
return Err(ThreadStoreError::ThreadNotFound {
thread_id: params.thread_id,
});
}
let artifacts = state.artifacts.entry(params.thread_id).or_default();
let artifact = stored_artifact_from_new(params.artifact, uuid::Uuid::now_v7().to_string());
artifacts.push(artifact.clone());
Ok(artifact)
}
async fn update_thread_metadata(
&self,
params: UpdateThreadMetadataParams,
@@ -334,6 +370,7 @@ fn stored_thread_from_state(
agent_role: None,
agent_path: None,
git_info: None,
artifacts: state.artifacts.get(&thread_id).cloned().unwrap_or_default(),
approval_mode: AskForApproval::Never,
sandbox_policy: SandboxPolicy::new_read_only_policy(),
token_usage: None,
@@ -341,3 +378,15 @@ fn stored_thread_from_state(
history,
})
}
fn stored_artifact_from_new(
artifact: crate::NewThreadArtifact,
id: String,
) -> StoredThreadArtifact {
StoredThreadArtifact {
id,
created_at: Utc::now().timestamp(),
artifact_type: artifact.artifact_type,
payload: artifact.payload,
}
}
+4
View File
@@ -22,19 +22,23 @@ pub use local::LocalThreadStoreConfig;
pub use store::ThreadStore;
pub use types::AppendThreadItemsParams;
pub use types::ArchiveThreadParams;
pub use types::CreateThreadArtifactParams;
pub use types::CreateThreadParams;
pub use types::GitInfoPatch;
pub use types::ItemPage;
pub use types::ListItemsParams;
pub use types::ListThreadArtifactsParams;
pub use types::ListThreadsParams;
pub use types::ListTurnsParams;
pub use types::LoadThreadHistoryParams;
pub use types::NewThreadArtifact;
pub use types::OptionalStringPatch;
pub use types::ReadThreadByRolloutPathParams;
pub use types::ReadThreadParams;
pub use types::ResumeThreadParams;
pub use types::SortDirection;
pub use types::StoredThread;
pub use types::StoredThreadArtifact;
pub use types::StoredThreadHistory;
pub use types::StoredTurn;
pub use types::StoredTurnError;
@@ -139,6 +139,7 @@ pub(super) fn stored_thread_from_rollout_item(
agent_role: item.agent_role,
agent_path: None,
git_info,
artifacts: Vec::new(),
approval_mode: AskForApproval::OnRequest,
sandbox_policy: SandboxPolicy::new_read_only_policy(),
token_usage: None,
@@ -80,6 +80,8 @@ pub(super) async fn list_threads(
.map(|thread| thread.thread_id)
.collect::<HashSet<_>>();
let mut names = HashMap::<ThreadId, String>::with_capacity(thread_ids.len());
let mut artifacts =
HashMap::<ThreadId, Vec<crate::StoredThreadArtifact>>::with_capacity(thread_ids.len());
if let Some(state_db_ctx) = store.state_db().await {
for &thread_id in &thread_ids {
let Ok(Some(metadata)) = state_db_ctx.get_thread(thread_id).await else {
@@ -88,6 +90,14 @@ pub(super) async fn list_threads(
if let Some(title) = distinct_thread_metadata_title(&metadata) {
names.insert(thread_id, title);
}
artifacts.insert(
thread_id,
metadata
.artifacts
.into_iter()
.map(super::thread_artifacts::stored_artifact_from_state)
.collect(),
);
}
}
if names.len() < thread_ids.len()
@@ -102,6 +112,9 @@ pub(super) async fn list_threads(
if let Some(title) = names.get(&thread.thread_id).cloned() {
set_thread_name_from_title(thread, title);
}
if let Some(thread_artifacts) = artifacts.remove(&thread.thread_id) {
thread.artifacts = thread_artifacts;
}
}
Ok(ThreadPage { items, next_cursor })
@@ -193,6 +206,8 @@ mod tests {
use uuid::Uuid;
use super::*;
use crate::CreateThreadArtifactParams;
use crate::NewThreadArtifact;
use crate::ThreadStore;
use crate::local::LocalThreadStore;
use crate::local::test_support::test_config;
@@ -225,7 +240,7 @@ mod tests {
cwd_filters: None,
archived: false,
search_term: None,
use_state_db_only: false,
use_state_db_only: true,
})
.await
.expect("thread listing");
@@ -301,6 +316,78 @@ mod tests {
);
}
#[tokio::test]
async fn list_threads_preserves_sqlite_artifacts_for_rollout_rows() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let uuid = Uuid::from_u128(107);
let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id");
let rollout_path =
write_session_file(home.path(), "2025-01-03T12-00-00", uuid).expect("session file");
let runtime = codex_state::StateRuntime::init(
config.sqlite_home.clone(),
config.default_model_provider_id.clone(),
)
.await
.expect("state db should initialize");
let store = LocalThreadStore::new(config.clone(), Some(runtime.clone()));
runtime
.mark_backfill_complete(/*last_watermark*/ None)
.await
.expect("backfill should be complete");
let mut builder = codex_state::ThreadMetadataBuilder::new(
thread_id,
rollout_path,
Utc::now(),
SessionSource::Cli,
);
builder.model_provider = Some(config.default_model_provider_id.clone());
let mut metadata = builder.build(config.default_model_provider_id.as_str());
metadata.title = "artifact thread".to_string();
metadata.first_user_message = Some("plain preview".to_string());
metadata.preview = metadata.first_user_message.clone();
runtime
.upsert_thread(&metadata)
.await
.expect("state db upsert should succeed");
let artifact = store
.create_thread_artifact(CreateThreadArtifactParams {
thread_id,
artifact: NewThreadArtifact {
artifact_type: "github/pull_request".to_string(),
payload: serde_json::json!({
"repoOwner": "openai",
"repoName": "codex",
"number": 910887,
"canonicalUrl": "https://github.com/openai/codex/pull/910887",
}),
},
})
.await
.expect("artifact should create");
let page = store
.list_threads(ListThreadsParams {
page_size: 10,
cursor: None,
sort_key: ThreadSortKey::CreatedAt,
sort_direction: SortDirection::Desc,
allowed_sources: Vec::new(),
model_providers: None,
cwd_filters: None,
archived: false,
search_term: None,
use_state_db_only: true,
})
.await
.expect("thread listing");
assert_eq!(page.items.len(), 1);
assert_eq!(page.items[0].thread_id, thread_id);
assert_eq!(page.items[0].artifacts.len(), 1);
assert_eq!(page.items[0].artifacts[0].id, artifact.id);
}
#[tokio::test]
async fn list_threads_selects_active_or_archived_collection() {
let home = TempDir::new().expect("temp dir");
+18
View File
@@ -4,6 +4,7 @@ mod helpers;
mod list_threads;
mod live_writer;
mod read_thread;
mod thread_artifacts;
mod unarchive_thread;
mod update_thread_metadata;
@@ -22,13 +23,16 @@ use tokio::sync::Mutex;
use crate::AppendThreadItemsParams;
use crate::ArchiveThreadParams;
use crate::CreateThreadArtifactParams;
use crate::CreateThreadParams;
use crate::ListThreadArtifactsParams;
use crate::ListThreadsParams;
use crate::LoadThreadHistoryParams;
use crate::ReadThreadByRolloutPathParams;
use crate::ReadThreadParams;
use crate::ResumeThreadParams;
use crate::StoredThread;
use crate::StoredThreadArtifact;
use crate::StoredThreadHistory;
use crate::ThreadPage;
use crate::ThreadStore;
@@ -249,6 +253,20 @@ impl ThreadStore for LocalThreadStore {
list_threads::list_threads(self, params).await
}
async fn list_thread_artifacts(
&self,
params: ListThreadArtifactsParams,
) -> ThreadStoreResult<Vec<StoredThreadArtifact>> {
thread_artifacts::list_thread_artifacts(self, params).await
}
async fn create_thread_artifact(
&self,
params: CreateThreadArtifactParams,
) -> ThreadStoreResult<StoredThreadArtifact> {
thread_artifacts::create_thread_artifact(self, params).await
}
async fn update_thread_metadata(
&self,
params: UpdateThreadMetadataParams,
+112 -20
View File
@@ -30,7 +30,8 @@ pub(super) async fn read_thread(
params: ReadThreadParams,
) -> ThreadStoreResult<StoredThread> {
let thread_id = params.thread_id;
if let Some(metadata) = read_sqlite_metadata(store, thread_id).await
let sqlite_metadata = read_sqlite_metadata(store, thread_id).await;
if let Some(metadata) = sqlite_metadata.clone()
&& (params.include_archived
|| (metadata.archived_at.is_none()
&& !rollout_path_is_archived(
@@ -75,6 +76,9 @@ pub(super) async fn read_thread(
message: format!("thread {} is archived", thread.thread_id),
});
}
if let Some(metadata) = sqlite_metadata {
apply_sqlite_git_info(&mut thread, metadata);
}
attach_history_if_requested(&mut thread, params.include_history).await?;
Ok(thread)
}
@@ -109,25 +113,29 @@ pub(super) async fn read_thread_by_rollout_path(
});
}
if let Some(metadata) = read_sqlite_metadata(store, thread.thread_id).await {
let existing_git_info = thread.git_info.take();
let (fallback_sha, fallback_branch, fallback_origin_url) = match existing_git_info {
Some(info) => (
info.commit_hash.map(|sha| sha.0),
info.branch,
info.repository_url,
),
None => (None, None, None),
};
thread.git_info = git_info_from_parts(
metadata.git_sha.or(fallback_sha),
metadata.git_branch.or(fallback_branch),
metadata.git_origin_url.or(fallback_origin_url),
);
apply_sqlite_git_info(&mut thread, metadata);
}
attach_history_if_requested(&mut thread, include_history).await?;
Ok(thread)
}
fn apply_sqlite_git_info(thread: &mut StoredThread, metadata: ThreadMetadata) {
let existing_git_info = thread.git_info.take();
let (fallback_sha, fallback_branch, fallback_origin_url) = match existing_git_info {
Some(info) => (
info.commit_hash.map(|sha| sha.0),
info.branch,
info.repository_url,
),
None => (None, None, None),
};
thread.git_info = git_info_from_parts(
metadata.git_sha.or(fallback_sha),
metadata.git_branch.or(fallback_branch),
metadata.git_origin_url.or(fallback_origin_url),
);
}
fn resolve_requested_rollout_path(
store: &LocalThreadStore,
rollout_path: std::path::PathBuf,
@@ -242,6 +250,8 @@ async fn read_thread_from_rollout_path(
{
set_thread_name_from_title(&mut thread, title);
}
thread.artifacts =
super::thread_artifacts::rollout_thread_artifacts(path.as_path(), thread.thread_id).await?;
Ok(thread)
}
@@ -313,6 +323,11 @@ async fn stored_thread_from_sqlite_metadata(
metadata.git_branch,
metadata.git_origin_url,
),
artifacts: metadata
.artifacts
.into_iter()
.map(super::thread_artifacts::stored_artifact_from_state)
.collect(),
approval_mode: parse_or_default(&metadata.approval_mode, AskForApproval::OnRequest),
sandbox_policy: parse_or_default(
&metadata.sandbox_policy,
@@ -334,9 +349,16 @@ async fn stored_thread_from_session_meta(
message: format!("failed to read thread {}: {err}", path.display()),
})?;
let archived = rollout_path_is_archived(store.config.codex_home.as_path(), path.as_path());
Ok(stored_thread_from_meta_line(
store, meta_line, path, archived,
))
let mut thread = stored_thread_from_meta_line(store, meta_line, path, archived);
thread.artifacts = super::thread_artifacts::rollout_thread_artifacts(
thread
.rollout_path
.as_deref()
.expect("session-meta thread should retain rollout path"),
thread.thread_id,
)
.await?;
Ok(thread)
}
fn stored_thread_from_meta_line(
@@ -375,6 +397,7 @@ fn stored_thread_from_meta_line(
agent_role: meta_line.meta.agent_role,
agent_path: meta_line.meta.agent_path,
git_info: meta_line.git,
artifacts: Vec::new(),
approval_mode: AskForApproval::OnRequest,
sandbox_policy: SandboxPolicy::new_read_only_policy(),
token_usage: None,
@@ -418,6 +441,8 @@ mod tests {
use uuid::Uuid;
use super::*;
use crate::CreateThreadArtifactParams;
use crate::NewThreadArtifact;
use crate::ThreadStore;
use crate::local::LocalThreadStore;
use crate::local::test_support::test_config;
@@ -657,7 +682,6 @@ mod tests {
.upsert_thread(&metadata)
.await
.expect("state db upsert should succeed");
let thread = store
.read_thread(ReadThreadParams {
thread_id,
@@ -670,6 +694,58 @@ mod tests {
assert_eq!(thread.name, Some("Saved title".to_string()));
}
#[tokio::test]
async fn read_thread_preserves_sqlite_artifacts_with_rollout_preview() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let uuid = Uuid::from_u128(225);
let thread_id = ThreadId::from_string(&uuid.to_string()).expect("valid thread id");
let rollout_path =
write_session_file(home.path(), "2025-01-03T12-00-00", uuid).expect("session file");
let runtime = codex_state::StateRuntime::init(
config.sqlite_home.clone(),
config.default_model_provider_id.clone(),
)
.await
.expect("state db should initialize");
let store = LocalThreadStore::new(config.clone(), Some(runtime.clone()));
let mut builder =
ThreadMetadataBuilder::new(thread_id, rollout_path, Utc::now(), SessionSource::Cli);
builder.model_provider = Some(config.default_model_provider_id.clone());
runtime
.upsert_thread(&builder.build(config.default_model_provider_id.as_str()))
.await
.expect("state db upsert should succeed");
let artifact = store
.create_thread_artifact(CreateThreadArtifactParams {
thread_id,
artifact: NewThreadArtifact {
artifact_type: "github/pull_request".to_string(),
payload: serde_json::json!({
"repoOwner": "openai",
"repoName": "codex",
"number": 910887,
"canonicalUrl": "https://github.com/openai/codex/pull/910887",
}),
},
})
.await
.expect("artifact should create");
let thread = store
.read_thread(ReadThreadParams {
thread_id,
include_archived: false,
include_history: false,
})
.await
.expect("read thread");
assert_eq!(thread.preview, "Hello from user");
assert_eq!(thread.artifacts.len(), 1);
assert_eq!(thread.artifacts[0].id, artifact.id);
}
#[tokio::test]
async fn read_thread_preserves_rollout_cwd_when_sqlite_metadata_exists() {
let home = TempDir::new().expect("temp dir");
@@ -868,6 +944,20 @@ mod tests {
.upsert_thread(&metadata)
.await
.expect("state db upsert should succeed");
let artifact = store
.create_thread_artifact(CreateThreadArtifactParams {
thread_id,
artifact: NewThreadArtifact {
artifact_type: "github/pull_request".to_string(),
payload: serde_json::json!({
"repoOwner": "openai",
"repoName": "codex",
"number": 910887,
}),
},
})
.await
.expect("artifact should create");
let thread = store
.read_thread(ReadThreadParams {
@@ -882,9 +972,11 @@ mod tests {
assert_eq!(thread.rollout_path, Some(rollout_path));
assert_eq!(thread.preview, "Hello from user");
assert_eq!(thread.model_provider, config.default_model_provider_id);
assert_eq!(thread.artifacts.len(), 1);
assert_eq!(thread.artifacts[0].id, artifact.id);
let history = thread.history.expect("history should load");
assert_eq!(history.thread_id, thread_id);
assert_eq!(history.items.len(), 2);
assert_eq!(history.items.len(), 3);
}
#[tokio::test]
@@ -0,0 +1,265 @@
use std::collections::HashMap;
use std::path::Path;
use super::LocalThreadStore;
use super::live_writer;
use super::update_thread_metadata::ResolvedRolloutPath;
use super::update_thread_metadata::resolve_rollout_path;
use crate::CreateThreadArtifactParams;
use crate::ListThreadArtifactsParams;
use crate::NewThreadArtifact;
use crate::StoredThreadArtifact;
use crate::ThreadStoreError;
use crate::ThreadStoreResult;
use chrono::Utc;
use codex_protocol::ThreadId;
use codex_protocol::protocol::RolloutItem;
use codex_protocol::protocol::ThreadArtifact as RolloutThreadArtifact;
use codex_rollout::RolloutRecorder;
use codex_rollout::append_rollout_item_to_path;
use codex_rollout::read_session_meta_line;
pub(super) async fn list_thread_artifacts(
store: &LocalThreadStore,
params: ListThreadArtifactsParams,
) -> ThreadStoreResult<Vec<StoredThreadArtifact>> {
let state_db = state_db(store, params.thread_id).await?;
state_db
.get_thread(params.thread_id)
.await
.map(|thread| {
thread
.map(|metadata| metadata.artifacts)
.unwrap_or_default()
.into_iter()
.map(stored_artifact_from_state)
.collect()
})
.map_err(|err| ThreadStoreError::Internal {
message: format!(
"failed to list thread artifacts for {}: {err}",
params.thread_id
),
})
}
pub(super) async fn create_thread_artifact(
store: &LocalThreadStore,
params: CreateThreadArtifactParams,
) -> ThreadStoreResult<StoredThreadArtifact> {
let resolved_rollout_path = writable_rollout_path(store, params.thread_id).await?;
let (artifact_type, payload) = state_artifact_kind(params.artifact);
let artifact_id = uuid::Uuid::now_v7().to_string();
let artifact = RolloutThreadArtifact {
created_at: Utc::now().timestamp(),
artifact_type,
payload,
};
write_rollout_thread_artifacts(
&resolved_rollout_path,
params.thread_id,
HashMap::from([(artifact_id.clone(), Some(artifact.clone()))]),
)
.await?;
let stored_artifact = stored_artifact_from_rollout(artifact_id, artifact);
append_state_thread_artifact(store, params.thread_id, &stored_artifact).await?;
Ok(stored_artifact)
}
async fn state_db(
store: &LocalThreadStore,
thread_id: ThreadId,
) -> ThreadStoreResult<codex_rollout::StateDbHandle> {
store
.state_db()
.await
.ok_or_else(|| ThreadStoreError::Internal {
message: format!("sqlite state db unavailable for thread {thread_id}"),
})
}
async fn append_state_thread_artifact(
store: &LocalThreadStore,
thread_id: ThreadId,
artifact: &StoredThreadArtifact,
) -> ThreadStoreResult<()> {
let state_db = state_db(store, thread_id).await?;
let created_at =
chrono::DateTime::<Utc>::from_timestamp(artifact.created_at, 0).ok_or_else(|| {
ThreadStoreError::Internal {
message: format!(
"failed to cache thread artifact {} for {thread_id}: invalid timestamp {}",
artifact.id, artifact.created_at
),
}
})?;
let updated = state_db
.append_thread_artifact(codex_state::ThreadArtifact {
thread_id,
id: artifact.id.clone(),
created_at,
artifact_type: artifact.artifact_type.clone(),
payload: artifact.payload.clone(),
})
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to cache thread artifact for {thread_id}: {err}"),
})?;
if !updated {
return Err(ThreadStoreError::ThreadNotFound { thread_id });
}
Ok(())
}
pub(super) fn stored_artifact_from_state(
artifact: codex_state::ThreadArtifact,
) -> StoredThreadArtifact {
StoredThreadArtifact {
id: artifact.id,
created_at: artifact.created_at.timestamp(),
artifact_type: artifact.artifact_type,
payload: artifact.payload,
}
}
pub(super) fn stored_artifact_from_rollout(
id: String,
artifact: RolloutThreadArtifact,
) -> StoredThreadArtifact {
StoredThreadArtifact {
id,
created_at: artifact.created_at,
artifact_type: artifact.artifact_type,
payload: artifact.payload,
}
}
fn state_artifact_kind(artifact: NewThreadArtifact) -> (String, serde_json::Value) {
(artifact.artifact_type, artifact.payload)
}
async fn writable_rollout_path(
store: &LocalThreadStore,
thread_id: ThreadId,
) -> ThreadStoreResult<ResolvedRolloutPath> {
if live_writer::rollout_path(store, thread_id).await.is_ok() {
live_writer::persist_thread(store, thread_id).await?;
}
resolve_rollout_path(store, thread_id, /*include_archived*/ false)
.await
.map_err(|err| match err {
ThreadStoreError::InvalidRequest { .. } => {
ThreadStoreError::ThreadNotFound { thread_id }
}
other => other,
})
}
pub(super) async fn rollout_thread_artifacts(
rollout_path: &Path,
thread_id: ThreadId,
) -> ThreadStoreResult<Vec<StoredThreadArtifact>> {
let (items, _, _) = RolloutRecorder::load_rollout_items(rollout_path)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!(
"failed to read thread artifacts for {thread_id} from {}: {err}",
rollout_path.display()
),
})?;
let mut artifacts = Vec::<StoredThreadArtifact>::new();
for item in items {
let RolloutItem::SessionMeta(meta_line) = item else {
continue;
};
if meta_line.meta.id != thread_id {
continue;
}
for (id, artifact) in meta_line.artifacts {
match artifact {
Some(artifact) => {
let updated_artifact = stored_artifact_from_rollout(id.clone(), artifact);
if let Some(existing) = artifacts.iter_mut().find(|existing| existing.id == id)
{
*existing = updated_artifact;
} else {
artifacts.push(updated_artifact);
}
}
None => artifacts.retain(|artifact| artifact.id != id),
}
}
}
Ok(artifacts)
}
async fn write_rollout_thread_artifacts(
resolved_rollout_path: &ResolvedRolloutPath,
thread_id: ThreadId,
artifacts: HashMap<String, Option<RolloutThreadArtifact>>,
) -> ThreadStoreResult<()> {
let mut session_meta = read_session_meta_line(resolved_rollout_path.path.as_path())
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to set thread artifacts for {thread_id}: {err}"),
})?;
if session_meta.meta.id != thread_id {
return Err(ThreadStoreError::Internal {
message: format!(
"failed to set thread artifacts: rollout session metadata id mismatch: expected {thread_id}, found {}",
session_meta.meta.id
),
});
}
session_meta.git = None;
session_meta.artifacts = artifacts;
append_rollout_item_to_path(
resolved_rollout_path.path.as_path(),
&RolloutItem::SessionMeta(session_meta),
)
.await
.map_err(|err| ThreadStoreError::Internal {
message: format!("failed to set thread artifacts for {thread_id}: {err}"),
})?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ThreadStore;
use crate::local::test_support::test_config;
use tempfile::TempDir;
#[tokio::test]
async fn create_thread_artifact_returns_thread_not_found_for_missing_thread() {
let home = TempDir::new().expect("temp dir");
let config = test_config(home.path());
let runtime = codex_state::StateRuntime::init(
config.sqlite_home.clone(),
config.default_model_provider_id.clone(),
)
.await
.expect("state db should initialize");
let store = LocalThreadStore::new(config, Some(runtime));
let thread_id =
ThreadId::from_string("00000000-0000-0000-0000-000000000123").expect("valid thread id");
let err = store
.create_thread_artifact(CreateThreadArtifactParams {
thread_id,
artifact: NewThreadArtifact {
artifact_type: "github/pull_request".to_string(),
payload: serde_json::json!({ "number": 123 }),
},
})
.await
.expect_err("missing thread should fail");
assert!(matches!(
err,
ThreadStoreError::ThreadNotFound { thread_id: missing_thread_id }
if missing_thread_id == thread_id
));
}
}
@@ -23,9 +23,9 @@ use crate::ThreadStoreResult;
use crate::UpdateThreadMetadataParams;
use crate::local::read_thread;
struct ResolvedRolloutPath {
path: PathBuf,
archived: bool,
pub(super) struct ResolvedRolloutPath {
pub(super) path: PathBuf,
pub(super) archived: bool,
}
pub(super) async fn update_thread_metadata(
@@ -114,13 +114,13 @@ pub(super) async fn update_thread_metadata(
apply_thread_git_info_to_rollout(
resolved_rollout_path.path.as_path(),
thread_id,
sha,
branch,
origin_url,
&sha,
&branch,
&origin_url,
memory_mode.as_deref(),
)
.await?;
apply_thread_git_info(store, thread_id, sha, branch, origin_url).await?;
apply_thread_git_info(store, thread_id, &sha, &branch, &origin_url).await?;
}
let mut thread = match read_thread::read_thread(
@@ -228,6 +228,7 @@ async fn apply_thread_git_info_to_rollout(
branch: branch.clone(),
repository_url: origin_url.clone(),
});
session_meta.artifacts.clear();
session_meta.meta.memory_mode = memory_mode.map(str::to_string);
append_rollout_item_to_path(rollout_path, &RolloutItem::SessionMeta(session_meta))
.await
@@ -285,6 +286,7 @@ async fn apply_thread_memory_mode(
// Memory-mode updates should not modify git metadata. The rollout replay
// code will preserve the latest prior git marker when this field is absent.
session_meta.git = None;
session_meta.artifacts.clear();
session_meta.meta.memory_mode = Some(memory_mode_as_str(memory_mode).to_string());
append_rollout_item_to_path(rollout_path, &RolloutItem::SessionMeta(session_meta))
.await
@@ -300,7 +302,7 @@ fn memory_mode_as_str(mode: ThreadMemoryMode) -> &'static str {
}
}
async fn resolve_rollout_path(
pub(super) async fn resolve_rollout_path(
store: &LocalThreadStore,
thread_id: ThreadId,
include_archived: bool,
@@ -362,7 +364,10 @@ mod tests {
use uuid::Uuid;
use super::*;
use crate::CreateThreadArtifactParams;
use crate::GitInfoPatch;
use crate::ListThreadArtifactsParams;
use crate::NewThreadArtifact;
use crate::ResumeThreadParams;
use crate::ThreadEventPersistenceMode;
use crate::ThreadMetadataPatch;
@@ -456,6 +461,17 @@ mod tests {
.expect("state db should initialize");
let store = LocalThreadStore::new(config.clone(), Some(runtime.clone()));
let artifact = store
.create_thread_artifact(CreateThreadArtifactParams {
thread_id,
artifact: NewThreadArtifact {
artifact_type: "github/pull_request".to_string(),
payload: json!({ "number": 910887 }),
},
})
.await
.expect("create artifact");
store
.update_thread_metadata(UpdateThreadMetadataParams {
thread_id,
@@ -491,6 +507,7 @@ mod tests {
assert_eq!(appended["type"], "session_meta");
assert_eq!(appended["payload"]["memory_mode"], "disabled");
assert_eq!(appended["payload"]["git"]["branch"], "feature");
assert!(appended["payload"].get("artifacts").is_none());
codex_rollout::state_db::reconcile_rollout(
Some(runtime.as_ref()),
@@ -507,6 +524,13 @@ mod tests {
.await
.expect("thread memory mode should be readable");
assert_eq!(memory_mode.as_deref(), Some("disabled"));
assert_eq!(
store
.list_thread_artifacts(ListThreadArtifactsParams { thread_id })
.await
.expect("thread artifacts should remain readable"),
vec![artifact]
);
}
#[tokio::test]
+15
View File
@@ -4,9 +4,11 @@ use std::any::Any;
use crate::AppendThreadItemsParams;
use crate::ArchiveThreadParams;
use crate::CreateThreadArtifactParams;
use crate::CreateThreadParams;
use crate::ItemPage;
use crate::ListItemsParams;
use crate::ListThreadArtifactsParams;
use crate::ListThreadsParams;
use crate::ListTurnsParams;
use crate::LoadThreadHistoryParams;
@@ -14,6 +16,7 @@ use crate::ReadThreadByRolloutPathParams;
use crate::ReadThreadParams;
use crate::ResumeThreadParams;
use crate::StoredThread;
use crate::StoredThreadArtifact;
use crate::StoredThreadHistory;
use crate::ThreadPage;
use crate::ThreadStoreError;
@@ -72,6 +75,18 @@ pub trait ThreadStore: Any + Send + Sync {
/// Lists stored threads matching the supplied filters.
async fn list_threads(&self, params: ListThreadsParams) -> ThreadStoreResult<ThreadPage>;
/// Lists artifacts associated with a thread in insertion order.
async fn list_thread_artifacts(
&self,
params: ListThreadArtifactsParams,
) -> ThreadStoreResult<Vec<StoredThreadArtifact>>;
/// Creates an artifact and returns the persisted record.
async fn create_thread_artifact(
&self,
params: CreateThreadArtifactParams,
) -> ThreadStoreResult<StoredThreadArtifact>;
/// Lists turns within a stored thread.
async fn list_turns(&self, _params: ListTurnsParams) -> ThreadStoreResult<TurnPage> {
Err(ThreadStoreError::Unsupported {
+36
View File
@@ -16,6 +16,7 @@ use codex_protocol::protocol::ThreadSource;
use codex_protocol::protocol::TokenUsage;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value as JsonValue;
/// Controls how many event variants should be persisted for future replay.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
@@ -335,6 +336,8 @@ pub struct StoredThread {
pub agent_path: Option<String>,
/// Optional Git metadata captured for the thread.
pub git_info: Option<GitInfo>,
/// Model-managed artifacts attached to the thread.
pub artifacts: Vec<StoredThreadArtifact>,
/// Approval mode captured for the thread.
pub approval_mode: AskForApproval,
/// Sandbox policy captured for the thread.
@@ -347,6 +350,39 @@ pub struct StoredThread {
pub history: Option<StoredThreadHistory>,
}
/// Store-owned model-managed thread artifact.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StoredThreadArtifact {
/// Stable artifact id.
pub id: String,
/// Unix timestamp in seconds when the artifact was created.
pub created_at: i64,
/// Namespaced artifact kind chosen by the client.
pub artifact_type: String,
/// Opaque client-defined artifact payload.
pub payload: JsonValue,
}
/// Parameters for listing thread artifacts.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ListThreadArtifactsParams {
pub thread_id: ThreadId,
}
/// Parameters for creating a thread artifact.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CreateThreadArtifactParams {
pub thread_id: ThreadId,
pub artifact: NewThreadArtifact,
}
/// New artifact payload accepted by the store.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NewThreadArtifact {
pub artifact_type: String,
pub payload: JsonValue,
}
/// Optional field patch where omission leaves a value unchanged and `Some(None)` clears it.
pub type OptionalStringPatch = Option<Option<String>>;