Add experimental attached thread forks (#56351)

## What changed

- Add `attachToSource` to `thread/fork` to persist a fork attached to its source without emitting `thread/started`. Attached forks require local storage and cannot use `ephemeral` or `deferGoalContinuation`.
- Hide attached threads from ordinary listings and add `attachedToThreadId` to `thread/list` for explicit discovery, including empty threads. Keep this relationship separate from spawned-agent parent and ancestor filters.
- Persist attachment metadata in rollouts and SQLite so it survives restarts and database recovery. Reject source deletion until its attached threads are deleted.

## Testing

Add coverage for legacy and paginated history, restart and missing or corrupt database recovery, filtered listings and pagination, resume, conflicting options, and deletion safeguards across store instances.

GitOrigin-RevId: 589f3174fc36f6054e75d65818360695645a5913
This commit is contained in:
Brent Traut
2026-10-23 04:52:47 +00:00
committed by copyberry
parent 4610b573e5
commit 30587d7885
70 changed files with 1078 additions and 68 deletions
+106 -5
View File
@@ -7,12 +7,15 @@ use codex_protocol::protocol::SessionSource;
use std::sync::atomic::AtomicI64;
use std::sync::atomic::Ordering;
const EXCLUDE_ATTACHED_THREADS: &str = " AND threads.attached_thread_parent_id IS NULL";
impl StateRuntime {
pub async fn get_thread(&self, id: ThreadId) -> anyhow::Result<Option<crate::ThreadMetadata>> {
let row = sqlx::query(
r#"
SELECT
threads.id,
threads.attached_thread_parent_id,
threads.rollout_path,
threads.created_at_ms AS created_at,
threads.updated_at_ms AS updated_at,
@@ -454,6 +457,7 @@ ON CONFLICT(child_thread_id) DO NOTHING
},
/*include_thread_id_tiebreaker*/ false,
);
builder.push(EXCLUDE_ATTACHED_THREADS);
builder.push(" AND threads.title = ");
builder.push_bind(title);
if let Some(cwd) = cwd {
@@ -585,6 +589,7 @@ ON CONFLICT(child_thread_id) DO NOTHING
| crate::SortKey::SectionPosition
),
);
builder.push(EXCLUDE_ATTACHED_THREADS);
push_thread_order_and_limit(
&mut builder,
sort_key,
@@ -663,8 +668,9 @@ INSERT INTO threads (
git_origin_url,
memory_mode,
project_id,
daybreak_enabled
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
daybreak_enabled,
attached_thread_parent_id
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING
"#,
)
@@ -718,6 +724,7 @@ ON CONFLICT(id) DO NOTHING
.bind("enabled")
.bind(metadata.project_id.as_deref())
.bind(metadata.daybreak_enabled)
.bind(metadata.attached_thread_parent_id.map(|id| id.to_string()))
.execute_with_busy_retry(self.pool.as_ref())
.await?;
self.insert_thread_spawn_edge_from_source_if_absent(metadata.id, metadata.source.as_str())
@@ -963,9 +970,11 @@ INSERT INTO threads (
git_origin_url,
memory_mode,
project_id,
daybreak_enabled
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
daybreak_enabled,
attached_thread_parent_id
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
attached_thread_parent_id = COALESCE(excluded.attached_thread_parent_id, threads.attached_thread_parent_id),
rollout_path = excluded.rollout_path,
created_at = excluded.created_at,
updated_at = excluded.updated_at,
@@ -1054,6 +1063,7 @@ ON CONFLICT(id) DO UPDATE SET
.bind(creation_memory_mode.unwrap_or("enabled"))
.bind(metadata.project_id.as_deref())
.bind(metadata.daybreak_enabled)
.bind(metadata.attached_thread_parent_id.map(|id| id.to_string()))
.execute_with_busy_retry(self.pool.as_ref())
.await?;
self.insert_thread_spawn_edge_from_source_if_absent(metadata.id, metadata.source.as_str())
@@ -1272,6 +1282,9 @@ WITH RECURSIVE subtree(child_thread_id, parent_thread_id) AS (
Some(crate::ThreadRelationFilter::DescendantsOf(_)) => builder.push(
", subtree.parent_thread_id AS parent_thread_id\nFROM subtree\nCROSS JOIN threads ON threads.id = subtree.child_thread_id",
),
Some(crate::ThreadRelationFilter::AttachedThreadsOf(_)) => {
builder.push(", NULL AS parent_thread_id\nFROM threads")
}
None => builder.push(" FROM threads"),
};
let include_thread_id_tiebreaker = relation_filter.is_some()
@@ -1285,6 +1298,12 @@ WITH RECURSIVE subtree(child_thread_id, parent_thread_id) AS (
include_thread_id_tiebreaker,
/*include_empty_preview*/ relation_filter.is_some(),
);
if !matches!(
relation_filter,
Some(crate::ThreadRelationFilter::AttachedThreadsOf(_))
) {
builder.push(EXCLUDE_ATTACHED_THREADS);
}
match relation_filter {
Some(crate::ThreadRelationFilter::DirectChildrenOf(parent_thread_id)) => {
builder.push(" AND listed_edge.parent_thread_id = ");
@@ -1294,11 +1313,15 @@ WITH RECURSIVE subtree(child_thread_id, parent_thread_id) AS (
builder.push(" AND subtree.child_thread_id != ");
builder.push_bind(ancestor_thread_id.to_string());
}
Some(crate::ThreadRelationFilter::AttachedThreadsOf(parent)) => {
builder.push(" AND threads.attached_thread_parent_id = ");
builder.push_bind(parent.to_string());
}
None => {}
}
let order_by_index = match (relation_filter, filters.cwd_filters) {
// Relationship listings are expected to be much smaller than the global thread table.
// Prefer the spawn-edge index and sort the matching subtree instead of scanning the
// Prefer the relationship index and sort the matches instead of scanning the
// timestamp index until enough related threads happen to be found.
(Some(_), _) => OrderByIndex::Disabled,
// Multi-cwd listing is supported but at the time of writing has no current use in production.
@@ -1321,6 +1344,7 @@ pub(super) fn push_thread_select_columns(builder: &mut QueryBuilder<Sqlite>) {
r#"
SELECT
threads.id,
threads.attached_thread_parent_id,
threads.rollout_path,
threads.created_at_ms AS created_at,
threads.updated_at_ms AS updated_at,
@@ -1708,6 +1732,81 @@ mod tests {
);
}
#[tokio::test]
async fn attached_thread_discovery_preserves_parent_lifecycle_and_cwd() -> anyhow::Result<()> {
let home = unique_temp_dir();
let config = crate::SqliteConfig::new_for_testing(home.as_path().abs());
let runtime = StateRuntime::init(config, "test-provider".to_string()).await?;
let parent = ThreadId::new();
let child = ThreadId::new();
let mut parent_metadata = test_thread_metadata(&home, parent, home.clone());
parent_metadata.preview = Some("parent".into());
parent_metadata.title = "shared title".into();
let mut child_metadata = test_thread_metadata(&home, child, home.join("original"));
child_metadata.attached_thread_parent_id = Some(parent);
child_metadata.preview = Some("saved child".into());
child_metadata.created_at = Utc::now() + chrono::Duration::seconds(5);
child_metadata.updated_at = child_metadata.created_at;
child_metadata.title = parent_metadata.title.clone();
runtime.upsert_thread(&parent_metadata).await?;
assert!(runtime.insert_thread_if_absent(&child_metadata).await?);
let filters = ThreadFilterOptions {
archived_only: false,
allowed_sources: &[],
model_providers: None,
cwd_filters: None,
section: None,
project_id: None,
anchor: None,
sort_key: SortKey::CreatedAt,
sort_direction: SortDirection::Desc,
search_term: None,
};
let named = runtime
.find_thread_by_exact_title(
"shared title",
&[],
/*model_providers*/ None,
/*archived_only*/ false,
/*cwd*/ None,
)
.await?;
assert_eq!(named.map(|thread| thread.id), Some(parent));
let ids = runtime
.list_thread_ids(
/*limit*/ 1,
/*anchor*/ None,
SortKey::UpdatedAt,
&[],
/*model_providers*/ None,
/*archived_only*/ false,
)
.await?;
assert_eq!(ids.len(), 1);
assert_ne!(ids[0], child);
parent_metadata.cwd = home.join("moved");
runtime.upsert_thread(&parent_metadata).await?;
runtime
.mark_archived(parent, &parent_metadata.rollout_path, Utc::now())
.await?;
let saved = runtime
.list_threads_by_relation(
/*page_size*/ 10,
crate::ThreadRelationFilter::AttachedThreadsOf(parent),
filters,
)
.await?;
assert_eq!(
saved
.items
.iter()
.map(|t| (t.id, t.cwd.clone()))
.collect::<Vec<_>>(),
vec![(child, child_metadata.cwd)]
);
Ok(())
}
#[tokio::test]
async fn archived_threads_without_previews_remain_listed() {
let codex_home = unique_temp_dir();
@@ -2808,6 +2907,7 @@ mod tests {
);
let items = vec![RolloutItem::SessionMeta(Box::new(SessionMetaLine {
meta: SessionMeta {
attached_thread: false,
creator_user_id: None,
creator_account_id: None,
session_id: thread_id.into(),
@@ -2882,6 +2982,7 @@ mod tests {
);
let items = vec![RolloutItem::SessionMeta(Box::new(SessionMetaLine {
meta: SessionMeta {
attached_thread: false,
creator_user_id: None,
creator_account_id: None,
session_id: thread_id.into(),