Preserve thread context when retrying or editing turns (#33211)

## What changed

- Add experimental `beforeTurnId` support to `thread/fork` for copying history strictly before a turn, including an in-progress turn.
- Use before-turn forks for TUI backtracking and safety retries so the replacement prompt retains thread context instead of recreating the branch from the preceding turn or a new thread.
- Add experimental `deferGoalContinuation` support so safety-retry forks can inherit an active goal while waiting for the replacement turn before automatic continuation resumes.
- Hide duplicated nested-review prompts when replaying history or selecting a prompt to edit.

## Testing

- Cover before-turn truncation, fork validation, active-goal inheritance, TUI safety retries, backtracking, and nested-review replay.

GitOrigin-RevId: 4915d85ae1929a0b8c40f3ed7dca29c7a5bdcdf3
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-07-15 02:35:49 +00:00
1 parent e4711f2a3b
commit d88db19144
25 files changed
+1343 -96

No files matched your search

+88
View File
@@ -65,6 +65,94 @@ WHERE thread_id = ?
row.map(|row| thread_goal_from_row(&row)).transpose()
}
pub async fn replace_thread_goal_snapshot(
&self,
goal: &crate::ThreadGoal,
) -> anyhow::Result<()> {
let mut transaction = self.pool.begin().await?;
sqlx::query(
r#"
INSERT INTO thread_goals (
thread_id,
goal_id,
objective,
status,
token_budget,
tokens_used,
time_used_seconds,
created_at_ms,
updated_at_ms
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(thread_id) DO UPDATE SET
goal_id = excluded.goal_id,
objective = excluded.objective,
status = excluded.status,
token_budget = excluded.token_budget,
tokens_used = excluded.tokens_used,
time_used_seconds = excluded.time_used_seconds,
created_at_ms = excluded.created_at_ms,
updated_at_ms = excluded.updated_at_ms
"#,
)
.bind(goal.thread_id.to_string())
.bind(&goal.goal_id)
.bind(&goal.objective)
.bind(goal.status.as_str())
.bind(goal.token_budget)
.bind(goal.tokens_used)
.bind(goal.time_used_seconds)
.bind(datetime_to_epoch_millis(goal.created_at))
.bind(datetime_to_epoch_millis(goal.updated_at))
.execute(&mut *transaction)
.await?;
sqlx::query(
r#"
INSERT INTO thread_goal_continuation_deferrals (thread_id)
VALUES (?)
ON CONFLICT(thread_id) DO NOTHING
"#,
)
.bind(goal.thread_id.to_string())
.execute(&mut *transaction)
.await?;
transaction.commit().await?;
Ok(())
}
pub async fn has_thread_goal_continuation_deferral(
&self,
thread_id: ThreadId,
) -> anyhow::Result<bool> {
sqlx::query_scalar(
r#"
SELECT EXISTS(
SELECT 1
FROM thread_goal_continuation_deferrals
WHERE thread_id = ?
)
"#,
)
.bind(thread_id.to_string())
.fetch_one(self.pool.as_ref())
.await
.map_err(Into::into)
}
pub async fn clear_thread_goal_continuation_deferral(
&self,
thread_id: ThreadId,
) -> anyhow::Result<()> {
sqlx::query("DELETE FROM thread_goal_continuation_deferrals WHERE thread_id = ?")
.bind(thread_id.to_string())
.execute(self.pool.as_ref())
.await?;
Ok(())
}
pub async fn replace_thread_goal(
&self,
thread_id: ThreadId,