Add persisted thread artifact models (#40509)

## What changed

- Add a `thread_artifacts` SQLite table with per-thread typed identities,
  JSON payloads, cascade deletion, uniqueness enforcement, and an index for
  ordered per-thread reads.
- Export artifact records, attachment and removal outcomes, and paginated
  result models from `codex-state`.

## Testing

- Add a migration test covering preservation of existing thread-section
  metadata and compatibility with the previous migration set.

GitOrigin-RevId: 4e0a8e5bfa168ae2b8871970466e5055e142a4a6
This commit is contained in:
joeytrasatti-openai
2026-08-25 01:10:10 +00:00
committed by copyberry
parent 41ca88e95c
commit 9b2ef38f54
5 changed files with 128 additions and 0 deletions
@@ -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);
+4
View File
@@ -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;
+61
View File
@@ -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();
+5
View File
@@ -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;
@@ -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>,
}