diff options
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/db.go | 26 | ||||
| -rw-r--r-- | internal/storage/db_test.go | 35 |
2 files changed, 50 insertions, 11 deletions
diff --git a/internal/storage/db.go b/internal/storage/db.go index 01ce902..aaf1e09 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -80,6 +80,7 @@ func (s *DB) migrate() error { `ALTER TABLE tasks ADD COLUMN rejection_comment TEXT`, `ALTER TABLE tasks ADD COLUMN question_json TEXT`, `ALTER TABLE executions ADD COLUMN session_id TEXT`, + `ALTER TABLE executions ADD COLUMN sandbox_dir TEXT`, } for _, m := range migrations { if _, err := s.db.Exec(m); err != nil { @@ -236,7 +237,7 @@ func (s *DB) ResetTaskForRetry(id string) (*task.Task, error) { configJSON, _ := json.Marshal(t.Agent) now := time.Now().UTC() - if _, err := tx.Exec(`UPDATE tasks SET state = ?, config_json = ?, updated_at = ? WHERE id = ?`, + if _, err := tx.Exec(`UPDATE tasks SET state = ?, config_json = ?, question_json = NULL, updated_at = ? WHERE id = ?`, string(task.StateQueued), string(configJSON), now, id); err != nil { return nil, err } @@ -348,7 +349,8 @@ type Execution struct { ArtifactDir string CostUSD float64 ErrorMsg string - SessionID string // claude --session-id; persisted for resume + SessionID string // claude --session-id; persisted for resume + SandboxDir string // preserved sandbox path when task is BLOCKED; resume must run here // In-memory only: set when creating a resume execution, not stored in DB. ResumeSessionID string @@ -358,23 +360,23 @@ type Execution struct { // CreateExecution inserts an execution record. func (s *DB) CreateExecution(e *Execution) error { _, err := s.db.Exec(` - INSERT INTO executions (id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + INSERT INTO executions (id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id, sandbox_dir) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, e.ID, e.TaskID, e.StartTime.UTC(), e.EndTime.UTC(), e.ExitCode, e.Status, - e.StdoutPath, e.StderrPath, e.ArtifactDir, e.CostUSD, e.ErrorMsg, e.SessionID, + e.StdoutPath, e.StderrPath, e.ArtifactDir, e.CostUSD, e.ErrorMsg, e.SessionID, e.SandboxDir, ) return err } // GetExecution retrieves an execution by ID. func (s *DB) GetExecution(id string) (*Execution, error) { - row := s.db.QueryRow(`SELECT id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id FROM executions WHERE id = ?`, id) + row := s.db.QueryRow(`SELECT id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id, sandbox_dir FROM executions WHERE id = ?`, id) return scanExecution(row) } // ListExecutions returns executions for a task. func (s *DB) ListExecutions(taskID string) ([]*Execution, error) { - rows, err := s.db.Query(`SELECT id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id FROM executions WHERE task_id = ? ORDER BY start_time DESC`, taskID) + rows, err := s.db.Query(`SELECT id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id, sandbox_dir FROM executions WHERE task_id = ? ORDER BY start_time DESC`, taskID) if err != nil { return nil, err } @@ -393,7 +395,7 @@ func (s *DB) ListExecutions(taskID string) ([]*Execution, error) { // GetLatestExecution returns the most recent execution for a task. func (s *DB) GetLatestExecution(taskID string) (*Execution, error) { - row := s.db.QueryRow(`SELECT id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id FROM executions WHERE task_id = ? ORDER BY start_time DESC LIMIT 1`, taskID) + row := s.db.QueryRow(`SELECT id, task_id, start_time, end_time, exit_code, status, stdout_path, stderr_path, artifact_dir, cost_usd, error_msg, session_id, sandbox_dir FROM executions WHERE task_id = ? ORDER BY start_time DESC LIMIT 1`, taskID) return scanExecution(row) } @@ -518,10 +520,10 @@ func (s *DB) UpdateTaskQuestion(taskID, questionJSON string) error { func (s *DB) UpdateExecution(e *Execution) error { _, err := s.db.Exec(` UPDATE executions SET end_time = ?, exit_code = ?, status = ?, cost_usd = ?, error_msg = ?, - stdout_path = ?, stderr_path = ?, artifact_dir = ?, session_id = ? + stdout_path = ?, stderr_path = ?, artifact_dir = ?, session_id = ?, sandbox_dir = ? WHERE id = ?`, e.EndTime.UTC(), e.ExitCode, e.Status, e.CostUSD, e.ErrorMsg, - e.StdoutPath, e.StderrPath, e.ArtifactDir, e.SessionID, e.ID, + e.StdoutPath, e.StderrPath, e.ArtifactDir, e.SessionID, e.SandboxDir, e.ID, ) return err } @@ -576,12 +578,14 @@ func scanTaskRows(rows *sql.Rows) (*task.Task, error) { func scanExecution(row scanner) (*Execution, error) { var e Execution var sessionID sql.NullString + var sandboxDir sql.NullString err := row.Scan(&e.ID, &e.TaskID, &e.StartTime, &e.EndTime, &e.ExitCode, &e.Status, - &e.StdoutPath, &e.StderrPath, &e.ArtifactDir, &e.CostUSD, &e.ErrorMsg, &sessionID) + &e.StdoutPath, &e.StderrPath, &e.ArtifactDir, &e.CostUSD, &e.ErrorMsg, &sessionID, &sandboxDir) if err != nil { return nil, err } e.SessionID = sessionID.String + e.SandboxDir = sandboxDir.String return &e, nil } diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index d28a4a8..2956be0 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -628,6 +628,41 @@ func TestDeleteTask_DeepSubtaskCascadeAtomic(t *testing.T) { } } +// TestResetTaskForRetry_ClearsQuestionJSON verifies that restarting a BLOCKED +// or FAILED task via ResetTaskForRetry clears any stale question so the frontend +// does not show a stale "waiting for input" prompt. +func TestResetTaskForRetry_ClearsQuestionJSON(t *testing.T) { + db := testDB(t) + now := time.Now().UTC() + tk := makeTestTask("retry-task", now) + tk.State = task.StatePending + db.CreateTask(tk) + + // Transition to BLOCKED with a question. + db.UpdateTaskState("retry-task", task.StateQueued) + db.UpdateTaskState("retry-task", task.StateRunning) + db.UpdateTaskState("retry-task", task.StateBlocked) + db.UpdateTaskQuestion("retry-task", `{"question":"which branch?"}`) + + // Simulate the task failing and being restarted. + db.UpdateTaskState("retry-task", task.StateFailed) + + if _, err := db.ResetTaskForRetry("retry-task"); err != nil { + t.Fatalf("ResetTaskForRetry: %v", err) + } + + got, err := db.GetTask("retry-task") + if err != nil { + t.Fatalf("GetTask: %v", err) + } + if got.QuestionJSON != "" { + t.Errorf("question_json should be cleared after reset, got %q", got.QuestionJSON) + } + if got.State != task.StateQueued { + t.Errorf("state should be QUEUED, got %q", got.State) + } +} + func TestStorage_GetLatestExecution(t *testing.T) { db := testDB(t) now := time.Now().UTC() |
