Let workflows merge or discard their own tasks

This commit is contained in:
Ian Gulliver
2026-10-22 16:46:57 -07:00
parent b2cb15dd2a
commit efa1513568
13 changed files with 489 additions and 398 deletions
+23 -1
View File
@@ -762,6 +762,27 @@ func (s *Store) ClearReady(ctx context.Context, id, actor, reason string) error
})
}
func (s *Store) TakeReady(ctx context.Context, id, ready string) (bool, error) {
taken := false
err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET ready = '' WHERE id = ? AND status = ? AND ready = ? RETURNING project_id`, id, string(api.TaskStatusActive), ready).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return nil
}
if err != nil {
return err
}
taken = true
reason := "merge"
if ready == "discard" {
reason = "discard"
}
return emit(api.Event{Kind: "task.ready_cleared", ProjectId: &projectID, TaskId: &id, Actor: "byre", Payload: map[string]any{"id": id, "reason": reason}})
})
return taken, err
}
func (s *Store) setReady(ctx context.Context, id, actor, ready, kind string, payload map[string]any) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
@@ -1667,7 +1688,8 @@ const taskColumns = `id, project_id, title, merge_title, branch, worktree_path,
// Keep the child lookup separate from the task itself so SQLite can use tasks_parent.
const backgroundExpr = `(audit != '' OR EXISTS (SELECT 1 FROM tasks r WHERE r.id = tasks.parent_task_id AND tasks.workflow_stage != '' AND r.audit != ''))
AND NOT EXISTS (SELECT 1 FROM events WHERE events.task_id IN (SELECT tasks.id UNION ALL SELECT c.id FROM tasks c WHERE c.parent_task_id = tasks.id AND c.parent_task_id != '' AND tasks.workflow_name != '')
AND kind IN ('inbox.created', 'task.ready', 'session.error', 'session.rate_limited'))`
AND (kind IN ('inbox.created', 'task.ready', 'session.error', 'session.rate_limited')
OR (kind = 'task.discard_suggested' AND NOT EXISTS (SELECT 1 FROM tasks r JOIN workflow_definitions d ON d.id = r.workflow_definition_id WHERE r.id IN (tasks.id, tasks.parent_task_id) AND d.auto_discard))))`
const sessionColumns = `id, coalesce(task_id, ''), owner, vendor, model, vendor_session_id, state, role, member, created_by, created_at, forked_from, forked_at, subagents`