Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Add persisted thread artifact models (#40509)

joeytrasatti-openai9b2ef38f548f
diff --git a/codex-rs/state/migrations/0051_thread_artifacts.sql b/codex-rs/state/migrations/0051_thread_artifacts.sql
new file mode 100644
index 0000000000..8114a7db35
--- /dev/null
+++ b/codex-rs/state/migrations/0051_thread_artifacts.sql
@@ -0,0 +1,12 @@
+CREATE TABLE thread_artifacts (
+ id TEXT PRIMARY KEY,
+ thread_id TEXT NOT NULL REFERENCES threads(id) ON DELETE CASCADE,
+ artifact_type TEXT NOT NULL,
+ identity_key TEXT NOT NULL,
+ payload TEXT NOT NULL,
+ created_at INTEGER NOT NULL,
+ UNIQUE (thread_id, artifact_type, identity_key)
+);
+
+CREATE INDEX idx_thread_artifacts_thread_created_id
+ ON thread_artifacts(thread_id, created_at, id);
diff --git a/codex-rs/state/src/lib.rs b/codex-rs/state/src/lib.rs
index 0352333a97..8176aed59c 100644
--- a/codex-rs/state/src/lib.rs
+++ b/codex-rs/state/src/lib.rs
@@ -54,6 +54,10 @@ pub use model::Stage1JobClaim;
pub use model::Stage1JobClaimOutcome;
pub use model::Stage1Output;
pub use model::Stage1StartupClaimParams;
+pub use model::ThreadArtifact;
+pub use model::ThreadArtifactAttachmentOutcome;
+pub use model::ThreadArtifactPage;
+pub use model::ThreadArtifactRemovalOutcome;
pub use model::ThreadGoal;
pub use model::ThreadGoalStatus;
pub use model::ThreadMetadata;
diff --git a/codex-rs/state/src/migrations_tests.rs b/codex-rs/state/src/migrations_tests.rs
index 78c6929807..c80e851d18 100644
--- a/codex-rs/state/src/migrations_tests.rs
+++ b/codex-rs/state/src/migrations_tests.rs
@@ -188,6 +188,67 @@ INSERT INTO threads (
pool.close().await;
}
+#[tokio::test]
+async fn thread_artifact_migration_preserves_existing_section_metadata() {
+ let sqlite_home = crate::runtime::test_support::unique_temp_dir();
+ tokio::fs::create_dir_all(&sqlite_home)
+ .await
+ .expect("sqlite home should be created");
+ let _cleanup = scopeguard::guard(sqlite_home.clone(), |sqlite_home| {
+ let _ = std::fs::remove_dir_all(sqlite_home);
+ });
+ let sqlite = crate::SqliteConfig::new_for_testing(sqlite_home.as_path().abs());
+ let pool = sqlite
+ .open_read_write_pool(&sqlite.state_db_path())
+ .await
+ .expect("sqlite database should open");
+ migrator_through(/*version*/ 50)
+ .run(&pool)
+ .await
+ .expect("released thread migrations should apply");
+ sqlx::query("UPDATE thread_sections SET appearance = ? WHERE id = ?")
+ .bind(r#"{"icon":"pin"}"#)
+ .bind(PINNED_THREAD_SECTION_ID)
+ .execute(&pool)
+ .await
+ .expect("released section appearance should remain writable");
+
+ STATE_MIGRATOR
+ .run(&pool)
+ .await
+ .expect("artifact migration should apply without rewriting released migrations");
+ let section = sqlx::query_as::<_, (String, String, Option<String>)>(
+ "SELECT id, name, appearance FROM thread_sections WHERE id = ?",
+ )
+ .bind(PINNED_THREAD_SECTION_ID)
+ .fetch_one(&pool)
+ .await
+ .expect("existing section metadata should remain available");
+ assert_eq!(
+ section,
+ (
+ PINNED_THREAD_SECTION_ID.to_string(),
+ PINNED_THREAD_SECTION_NAME.to_string(),
+ Some(r#"{"icon":"pin"}"#.to_string()),
+ )
+ );
+
+ let artifact_tables = sqlx::query_scalar::<_, String>(
+ "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'thread_artifacts'",
+ )
+ .fetch_all(&pool)
+ .await
+ .expect("artifact table should exist");
+ assert_eq!(artifact_tables, vec!["thread_artifacts"]);
+
+ let mut released_migrator = migrator_through(/*version*/ 50);
+ released_migrator.ignore_missing = true;
+ released_migrator
+ .run(&pool)
+ .await
+ .expect("released binaries should tolerate the additive artifact migration");
+}
+
#[tokio::test]
async fn thread_section_order_migration_backfills_stably() {
let sqlite_home = crate::runtime::test_support::unique_temp_dir();
diff --git a/codex-rs/state/src/model/mod.rs b/codex-rs/state/src/model/mod.rs
index d46000d66c..90d0988ea4 100644
--- a/codex-rs/state/src/model/mod.rs
+++ b/codex-rs/state/src/model/mod.rs
@@ -5,6 +5,7 @@ mod memories;
mod project;
mod queued_item;
mod rollout_migration_state;
+mod thread_artifact;
mod thread_goal;
mod thread_metadata;
@@ -27,6 +28,10 @@ pub use queued_item::QueuedUserSubmissionRecord;
pub use rollout_migration_state::RolloutMigrationCursor;
pub use rollout_migration_state::RolloutMigrationSkippedRollout;
pub use rollout_migration_state::RolloutMigrationState;
+pub use thread_artifact::ThreadArtifact;
+pub use thread_artifact::ThreadArtifactAttachmentOutcome;
+pub use thread_artifact::ThreadArtifactPage;
+pub use thread_artifact::ThreadArtifactRemovalOutcome;
pub use thread_goal::ThreadGoal;
pub use thread_goal::ThreadGoalStatus;
pub use thread_metadata::Anchor;
diff --git a/codex-rs/state/src/model/thread_artifact.rs b/codex-rs/state/src/model/thread_artifact.rs
new file mode 100644
index 0000000000..cf04445de2
--- /dev/null
+++ b/codex-rs/state/src/model/thread_artifact.rs
@@ -0,0 +1,46 @@
+use codex_protocol::ThreadId;
+use serde_json::Value;
+
+/// A bounded artifact durably associated with one thread.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ThreadArtifact {
+ /// Stable, server-assigned UUIDv7 artifact identity.
+ pub id: String,
+ /// Thread that owns this artifact.
+ pub thread_id: ThreadId,
+ /// Client-defined artifact category.
+ pub artifact_type: String,
+ /// Client-defined stable identity within the owning thread and artifact category.
+ pub identity_key: String,
+ /// Bounded, client-defined artifact metadata.
+ pub payload: Value,
+ /// Integer Unix timestamp in seconds when the artifact was attached.
+ pub created_at: i64,
+}
+
+/// Result of attaching one uniquely identified thread artifact.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ThreadArtifactAttachmentOutcome {
+ /// A new durable artifact was created.
+ Created(ThreadArtifact),
+ /// The artifact was already attached; its payload and creation time are unchanged.
+ Existing(ThreadArtifact),
+}
+
+/// Result of removing a thread artifact.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum ThreadArtifactRemovalOutcome {
+ /// An attached artifact was removed.
+ Removed(ThreadArtifact),
+ /// No artifact with the requested identity was attached.
+ NotFound,
+}
+
+/// One deterministically ordered page of artifacts across selected threads.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ThreadArtifactPage {
+ /// Artifacts ordered by thread identity, creation time, and artifact identity.
+ pub artifacts: Vec<ThreadArtifact>,
+ /// Opaque cursor for the next page, or `None` when the selection is exhausted.
+ pub next_cursor: Option<String>,
+}