From 94e5604e770d6589f786ea71e51738e21492f301 Mon Sep 17 00:00:00 2001 From: soryu Date: Wed, 21 Jan 2026 17:31:46 +0000 Subject: Add task branching feature (#15) --- makima/src/db/models.rs | 45 +++++++++++++++++++++++++++++++++++-- makima/src/db/repository.rs | 54 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 87 insertions(+), 12 deletions(-) (limited to 'makima/src/db') diff --git a/makima/src/db/models.rs b/makima/src/db/models.rs index 291fad7..bf95a3a 100644 --- a/makima/src/db/models.rs +++ b/makima/src/db/models.rs @@ -519,6 +519,12 @@ pub struct Task { /// When the task was last interrupted due to daemon disconnect #[serde(skip_serializing_if = "Option::is_none")] pub interrupted_at: Option>, + + // Task branching + /// Source task ID when this task was branched from another task's conversation. + /// Used to track the origin of "what if" explorations. + #[serde(skip_serializing_if = "Option::is_none")] + pub branched_from_task_id: Option, } impl Task { @@ -598,8 +604,8 @@ pub struct TaskListResponse { #[derive(Debug, Deserialize, ToSchema)] #[serde(rename_all = "camelCase")] pub struct CreateTaskRequest { - /// Contract this task belongs to (required) - pub contract_id: Uuid, + /// Contract this task belongs to (optional for branched/anonymous tasks) + pub contract_id: Option, /// Name of the task pub name: String, /// Optional description @@ -633,6 +639,10 @@ pub struct CreateTaskRequest { pub copy_files: Option>, /// Checkpoint SHA to branch from (optional) pub checkpoint_sha: Option, + /// Source task ID when branching from another task's conversation + pub branched_from_task_id: Option, + /// Conversation history to initialize the task with (JSON array of messages) + pub conversation_history: Option, } /// Request payload for updating a task @@ -681,6 +691,37 @@ pub struct SendMessageRequest { pub message: String, } +/// Default for include_conversation field in BranchTaskRequest +fn default_include_conversation() -> bool { + true +} + +/// Request to branch a task from an existing task's conversation. +/// Creates a new anonymous task that continues from the source task's state. +#[derive(Debug, Deserialize, ToSchema)] +#[serde(rename_all = "camelCase")] +pub struct BranchTaskRequest { + /// The initial message/instructions for the branched task + pub message: String, + /// Optional name for the branched task (auto-generated if not provided) + pub name: Option, + /// Whether to include conversation history from the source task (default: true) + #[serde(default = "default_include_conversation")] + pub include_conversation: bool, +} + +/// Response from branching a task. +#[derive(Debug, Serialize, ToSchema)] +#[serde(rename_all = "camelCase")] +pub struct BranchTaskResponse { + /// The newly created branched task + pub task: Task, + /// Number of conversation messages included from source task + pub message_count: usize, + /// Daemon ID if the task was started (None if no daemon available) + pub daemon_id: Option, +} + // ============================================================================= // Daemon Types // ============================================================================= diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs index 536bc9b..7387735 100644 --- a/makima/src/db/repository.rs +++ b/makima/src/db/repository.rs @@ -654,8 +654,8 @@ pub async fn create_task(pool: &PgPool, req: CreateTaskRequest) -> Result Result Result Result Result, sqlx::Error> { get_supervisor_state(pool, contract_id).await } + +// ============================================================================= +// Anonymous Task Cleanup Functions +// ============================================================================= + +/// Delete stale anonymous tasks (tasks with contract_id = NULL) that: +/// - Are in a terminal state (done, failed, merged) +/// - Are older than the specified number of days +/// +/// Returns the number of deleted tasks. +pub async fn cleanup_stale_anonymous_tasks( + pool: &PgPool, + max_age_days: i32, +) -> Result { + let result = sqlx::query( + r#" + DELETE FROM tasks + WHERE contract_id IS NULL + AND status IN ('done', 'failed', 'merged') + AND created_at < NOW() - INTERVAL '1 day' * $1 + "#, + ) + .bind(max_age_days) + .execute(pool) + .await?; + + Ok(result.rows_affected() as i64) +} -- cgit v1.2.3