diff options
| author | Claudomator Agent <agent@claudomator> | 2026-03-12 01:48:15 +0000 |
|---|---|---|
| committer | Claudomator Agent <agent@claudomator> | 2026-03-12 01:48:15 +0000 |
| commit | f28c22352aa1a8ede7552ee0277f7d60552d9094 (patch) | |
| tree | 02e9085809462e09b7d2a68ebb754e247eaecd22 | |
| parent | 4f83d35fa47bc71b31e0f92a0927bea8910c01b6 (diff) | |
feat: add Resume support for CANCELLED, FAILED, and BUDGET_EXCEEDED tasks
Interrupted tasks (CANCELLED, FAILED, BUDGET_EXCEEDED) now support session
resume in addition to restart. Both buttons are shown on the task card.
- executor: extend resumablePoolStates to include CANCELLED, FAILED, BUDGET_EXCEEDED
- api: extend handleResumeTimedOutTask to accept all resumable states with
state-specific resume messages; replace hard-coded TIMED_OUT check with a
resumableStates map
- web: add RESUME_STATES set; render Resume + Restart buttons for interrupted
states; TIMED_OUT keeps Resume only
- tests: 5 new Go tests (TestResumeInterrupted_*); updated task-actions.test.mjs
with 17 tests covering dual-button behaviour
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
| -rw-r--r-- | internal/api/server.go | 36 | ||||
| -rw-r--r-- | internal/api/server_test.go | 123 | ||||
| -rw-r--r-- | internal/executor/executor.go | 26 | ||||
| -rw-r--r-- | web/app.js | 84 | ||||
| -rw-r--r-- | web/test/task-actions.test.mjs | 89 |
5 files changed, 297 insertions, 61 deletions
diff --git a/internal/api/server.go b/internal/api/server.go index c545253..df35536 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -24,6 +24,7 @@ type questionStore interface { GetLatestExecution(taskID string) (*storage.Execution, error) UpdateTaskQuestion(taskID, questionJSON string) error UpdateTaskState(id string, newState task.State) error + AppendTaskInteraction(taskID string, interaction task.Interaction) error } // Server provides the REST API and WebSocket endpoint for Claudomator. @@ -250,6 +251,25 @@ func (s *Server) handleAnswerQuestion(w http.ResponseWriter, r *http.Request) { return } + // Record the Q&A interaction before clearing the question. + if tk.QuestionJSON != "" { + var qData struct { + Text string `json:"text"` + Options []string `json:"options"` + } + if jsonErr := json.Unmarshal([]byte(tk.QuestionJSON), &qData); jsonErr == nil { + interaction := task.Interaction{ + QuestionText: qData.Text, + Options: qData.Options, + Answer: input.Answer, + AskedAt: tk.UpdatedAt, + } + if appendErr := s.questionStore.AppendTaskInteraction(taskID, interaction); appendErr != nil { + s.logger.Error("failed to append interaction", "taskID", taskID, "error", appendErr) + } + } + } + // Clear the question and transition to QUEUED. if err := s.questionStore.UpdateTaskQuestion(taskID, ""); err != nil { writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to clear question"}) @@ -277,6 +297,14 @@ func (s *Server) handleAnswerQuestion(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]string{"message": "task queued for resume", "task_id": taskID}) } +// resumableStates are the task states from which a session-based resume is valid. +var resumableStates = map[task.State]string{ + task.StateTimedOut: "Your previous execution timed out. Please continue where you left off and complete the task.", + task.StateCancelled: "Your previous execution was cancelled. Please continue where you left off and complete the task.", + task.StateFailed: "Your previous execution failed. Please review what happened and continue from where you left off.", + task.StateBudgetExceeded: "Your previous execution exceeded its budget. Please continue where you left off and complete the task.", +} + func (s *Server) handleResumeTimedOutTask(w http.ResponseWriter, r *http.Request) { taskID := r.PathValue("id") @@ -285,8 +313,10 @@ func (s *Server) handleResumeTimedOutTask(w http.ResponseWriter, r *http.Request writeJSON(w, http.StatusNotFound, map[string]string{"error": "task not found"}) return } - if tk.State != task.StateTimedOut { - writeJSON(w, http.StatusConflict, map[string]string{"error": "task is not timed out"}) + + resumeMsg, resumable := resumableStates[tk.State] + if !resumable { + writeJSON(w, http.StatusConflict, map[string]string{"error": "task is not in a resumable state"}) return } @@ -302,7 +332,7 @@ func (s *Server) handleResumeTimedOutTask(w http.ResponseWriter, r *http.Request ID: uuid.New().String(), TaskID: taskID, ResumeSessionID: latest.SessionID, - ResumeAnswer: "Your previous execution timed out. Please continue where you left off and complete the task.", + ResumeAnswer: resumeMsg, } if err := s.pool.SubmitResume(context.Background(), tk, resumeExec); err != nil { writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": err.Error()}) diff --git a/internal/api/server_test.go b/internal/api/server_test.go index afdc9d2..c90e3b3 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -894,10 +894,11 @@ func TestServer_CancelTask_Completed_Returns409(t *testing.T) { // mockQuestionStore implements questionStore for testing handleAnswerQuestion. type mockQuestionStore struct { - getTaskFn func(id string) (*task.Task, error) - getLatestExecutionFn func(taskID string) (*storage.Execution, error) - updateTaskQuestionFn func(taskID, questionJSON string) error - updateTaskStateFn func(id string, newState task.State) error + getTaskFn func(id string) (*task.Task, error) + getLatestExecutionFn func(taskID string) (*storage.Execution, error) + updateTaskQuestionFn func(taskID, questionJSON string) error + updateTaskStateFn func(id string, newState task.State) error + appendInteractionFn func(taskID string, interaction task.Interaction) error } func (m *mockQuestionStore) GetTask(id string) (*task.Task, error) { @@ -912,6 +913,12 @@ func (m *mockQuestionStore) UpdateTaskQuestion(taskID, questionJSON string) erro func (m *mockQuestionStore) UpdateTaskState(id string, newState task.State) error { return m.updateTaskStateFn(id, newState) } +func (m *mockQuestionStore) AppendTaskInteraction(taskID string, interaction task.Interaction) error { + if m.appendInteractionFn != nil { + return m.appendInteractionFn(taskID, interaction) + } + return nil +} func TestServer_AnswerQuestion_UpdateQuestionFails_Returns500(t *testing.T) { srv, _ := testServer(t) @@ -1178,6 +1185,114 @@ func TestResumeTimedOut_ResponseShape(t *testing.T) { } } +func TestResumeInterrupted_Cancelled_Success_Returns202(t *testing.T) { + srv, store := testServer(t) + createTaskWithState(t, store, "resume-cancelled-1", task.StateCancelled) + + exec := &storage.Execution{ + ID: "exec-cancelled-1", + TaskID: "resume-cancelled-1", + SessionID: "550e8400-e29b-41d4-a716-446655440030", + Status: "CANCELLED", + } + if err := store.CreateExecution(exec); err != nil { + t.Fatalf("create execution: %v", err) + } + + req := httptest.NewRequest("POST", "/api/tasks/resume-cancelled-1/resume", nil) + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + + if w.Code != http.StatusAccepted { + t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) + } + got, _ := store.GetTask("resume-cancelled-1") + if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { + t.Errorf("task state: want QUEUED/RUNNING/READY after resume, got %v", got.State) + } +} + +func TestResumeInterrupted_Failed_Success_Returns202(t *testing.T) { + srv, store := testServer(t) + createTaskWithState(t, store, "resume-failed-1", task.StateFailed) + + exec := &storage.Execution{ + ID: "exec-failed-1", + TaskID: "resume-failed-1", + SessionID: "550e8400-e29b-41d4-a716-446655440031", + Status: "FAILED", + } + if err := store.CreateExecution(exec); err != nil { + t.Fatalf("create execution: %v", err) + } + + req := httptest.NewRequest("POST", "/api/tasks/resume-failed-1/resume", nil) + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + + if w.Code != http.StatusAccepted { + t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) + } + got, _ := store.GetTask("resume-failed-1") + if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { + t.Errorf("task state: want QUEUED/RUNNING/READY after resume, got %v", got.State) + } +} + +func TestResumeInterrupted_BudgetExceeded_Success_Returns202(t *testing.T) { + srv, store := testServer(t) + createTaskWithState(t, store, "resume-budget-1", task.StateBudgetExceeded) + + exec := &storage.Execution{ + ID: "exec-budget-1", + TaskID: "resume-budget-1", + SessionID: "550e8400-e29b-41d4-a716-446655440032", + Status: "BUDGET_EXCEEDED", + } + if err := store.CreateExecution(exec); err != nil { + t.Fatalf("create execution: %v", err) + } + + req := httptest.NewRequest("POST", "/api/tasks/resume-budget-1/resume", nil) + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + + if w.Code != http.StatusAccepted { + t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) + } + got, _ := store.GetTask("resume-budget-1") + if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { + t.Errorf("task state: want QUEUED/RUNNING/READY after resume, got %v", got.State) + } +} + +func TestResumeInterrupted_NoSession_Returns500(t *testing.T) { + srv, store := testServer(t) + createTaskWithState(t, store, "resume-nosess-1", task.StateCancelled) + + // No execution — no session ID available. + req := httptest.NewRequest("POST", "/api/tasks/resume-nosess-1/resume", nil) + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + + if w.Code != http.StatusInternalServerError { + t.Errorf("status: want 500, got %d; body: %s", w.Code, w.Body.String()) + } +} + +func TestResumeInterrupted_WrongState_Returns409(t *testing.T) { + srv, store := testServer(t) + createTaskWithState(t, store, "resume-wrong-1", task.StatePending) + + req := httptest.NewRequest("POST", "/api/tasks/resume-wrong-1/resume", nil) + w := httptest.NewRecorder() + srv.Handler().ServeHTTP(w, req) + + if w.Code != http.StatusConflict { + t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) + } +} + func TestRateLimit_ValidateRejectsExcess(t *testing.T) { srv, _ := testServer(t) srv.elaborateLimiter = newIPRateLimiter(0, 1) diff --git a/internal/executor/executor.go b/internal/executor/executor.go index 76c8ac7..bafacd2 100644 --- a/internal/executor/executor.go +++ b/internal/executor/executor.go @@ -26,6 +26,8 @@ type Store interface { UpdateExecution(e *storage.Execution) error UpdateTaskState(id string, newState task.State) error UpdateTaskQuestion(taskID, questionJSON string) error + UpdateTaskSummary(taskID, summary string) error + AppendTaskInteraction(taskID string, interaction task.Interaction) error } // LogPather is an optional interface runners can implement to provide the log @@ -149,11 +151,20 @@ func (p *Pool) Cancel(taskID string) bool { return true } -// SubmitResume re-queues a blocked task using the provided resume execution. +// resumablePoolStates are the task states that may be submitted for session resume. +var resumablePoolStates = map[task.State]bool{ + task.StateBlocked: true, + task.StateTimedOut: true, + task.StateCancelled: true, + task.StateFailed: true, + task.StateBudgetExceeded: true, +} + +// SubmitResume re-queues a blocked or interrupted task using the provided resume execution. // The execution must have ResumeSessionID and ResumeAnswer set. func (p *Pool) SubmitResume(ctx context.Context, t *task.Task, exec *storage.Execution) error { - if t.State != task.StateBlocked && t.State != task.StateTimedOut { - return fmt.Errorf("task %s must be in BLOCKED or TIMED_OUT state to resume (current: %s)", t.ID, t.State) + if !resumablePoolStates[t.State] { + return fmt.Errorf("task %s must be in a resumable state to resume (current: %s)", t.ID, t.State) } if exec.ResumeSessionID == "" { return fmt.Errorf("resume execution for task %s must have a ResumeSessionID", t.ID) @@ -331,6 +342,15 @@ func (p *Pool) handleRunResult(ctx context.Context, t *task.Task, exec *storage. } } + summary := exec.Summary + if summary == "" && exec.StdoutPath != "" { + summary = extractSummary(exec.StdoutPath) + } + if summary != "" { + if summaryErr := p.store.UpdateTaskSummary(t.ID, summary); summaryErr != nil { + p.logger.Error("failed to update task summary", "taskID", t.ID, "error", summaryErr) + } + } if updateErr := p.store.UpdateExecution(exec); updateErr != nil { p.logger.Error("failed to update execution", "error", updateErr) } @@ -119,8 +119,11 @@ function createTaskCard(task) { } // Footer: action buttons based on state - const RESTART_STATES = new Set(['FAILED', 'CANCELLED', 'BUDGET_EXCEEDED']); - if (task.state === 'PENDING' || task.state === 'RUNNING' || task.state === 'READY' || task.state === 'BLOCKED' || task.state === 'TIMED_OUT' || RESTART_STATES.has(task.state)) { + // Interrupted states (CANCELLED, FAILED, BUDGET_EXCEEDED) show both Resume and Restart. + // TIMED_OUT shows Resume only. Others show a single action. + const RESUME_STATES = new Set(['TIMED_OUT', 'CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']); + const RESTART_STATES = new Set(['CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']); + if (task.state === 'PENDING' || task.state === 'RUNNING' || task.state === 'READY' || task.state === 'BLOCKED' || RESUME_STATES.has(task.state)) { const footer = document.createElement('div'); footer.className = 'task-card-footer'; @@ -161,24 +164,25 @@ function createTaskCard(task) { footer.appendChild(rejectBtn); } else if (task.state === 'BLOCKED') { renderQuestionFooter(task, footer); - } else if (task.state === 'TIMED_OUT') { - const btn = document.createElement('button'); - btn.className = 'btn-resume'; - btn.textContent = 'Resume'; - btn.addEventListener('click', (e) => { + } else if (RESUME_STATES.has(task.state)) { + const resumeBtn = document.createElement('button'); + resumeBtn.className = 'btn-resume'; + resumeBtn.textContent = 'Resume'; + resumeBtn.addEventListener('click', (e) => { e.stopPropagation(); - handleResume(task.id, btn, footer); + handleResume(task.id, resumeBtn, footer); }); - footer.appendChild(btn); - } else if (RESTART_STATES.has(task.state)) { - const btn = document.createElement('button'); - btn.className = 'btn-restart'; - btn.textContent = 'Restart'; - btn.addEventListener('click', (e) => { - e.stopPropagation(); - handleRestart(task.id, btn, footer); - }); - footer.appendChild(btn); + footer.appendChild(resumeBtn); + if (RESTART_STATES.has(task.state)) { + const restartBtn = document.createElement('button'); + restartBtn.className = 'btn-restart'; + restartBtn.textContent = 'Restart'; + restartBtn.addEventListener('click', (e) => { + e.stopPropagation(); + handleRestart(task.id, restartBtn, footer); + }); + footer.appendChild(restartBtn); + } } card.appendChild(footer); @@ -1292,6 +1296,50 @@ function renderTaskPanel(task, executions) { const content = document.getElementById('task-panel-content'); content.innerHTML = ''; + // ── Summary ── + if (task.summary) { + const summarySection = makeSection('Summary'); + const summaryEl = document.createElement('p'); + summaryEl.className = 'task-summary'; + summaryEl.textContent = task.summary; + summarySection.appendChild(summaryEl); + content.appendChild(summarySection); + } + + // ── Q&A History ── + if (task.interactions && task.interactions.length > 0) { + const qaSection = makeSection('Q&A History'); + const qaList = document.createElement('div'); + qaList.className = 'qa-list'; + for (const interaction of task.interactions) { + const qaItem = document.createElement('div'); + qaItem.className = 'qa-item'; + + const qEl = document.createElement('div'); + qEl.className = 'qa-question'; + qEl.textContent = interaction.question_text || '(question)'; + qaItem.appendChild(qEl); + + if (interaction.options && interaction.options.length > 0) { + const opts = document.createElement('div'); + opts.className = 'qa-options'; + opts.textContent = 'Options: ' + interaction.options.join(', '); + qaItem.appendChild(opts); + } + + if (interaction.answer) { + const aEl = document.createElement('div'); + aEl.className = 'qa-answer'; + aEl.textContent = interaction.answer; + qaItem.appendChild(aEl); + } + + qaList.appendChild(qaItem); + } + qaSection.appendChild(qaList); + content.appendChild(qaSection); + } + // ── Overview ── const overview = makeSection('Overview'); const overviewGrid = document.createElement('div'); diff --git a/web/test/task-actions.test.mjs b/web/test/task-actions.test.mjs index c7d666b..a1790fa 100644 --- a/web/test/task-actions.test.mjs +++ b/web/test/task-actions.test.mjs @@ -6,78 +6,101 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; // ── Logic under test ────────────────────────────────────────────────────────── +// +// Interrupted states (CANCELLED, FAILED, BUDGET_EXCEEDED) get BOTH a Resume +// button and a Restart button. TIMED_OUT gets Resume only. + +const RESUME_STATES = new Set(['TIMED_OUT', 'CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']); +const RESTART_STATES = new Set(['CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']); + +function getCardActions(state) { + const actions = []; + if (state === 'PENDING') return ['run']; + if (state === 'RUNNING') return ['cancel']; + if (state === 'READY') return ['approve']; + if (RESUME_STATES.has(state)) actions.push('resume'); + if (RESTART_STATES.has(state)) actions.push('restart'); + return actions.length ? actions : [null]; +} -const RESTART_STATES = new Set(['FAILED', 'CANCELLED', 'BUDGET_EXCEEDED']); - -function getCardAction(state) { - if (state === 'PENDING') return 'run'; - if (state === 'RUNNING') return 'cancel'; - if (state === 'READY') return 'approve'; - if (state === 'TIMED_OUT') return 'resume'; - if (RESTART_STATES.has(state)) return 'restart'; - return null; +function getResumeEndpoint(state) { + return RESUME_STATES.has(state) ? '/resume' : null; } -function getApiEndpoint(state) { - if (state === 'TIMED_OUT') return '/resume'; - if (RESTART_STATES.has(state)) return '/run'; - return null; +function getRestartEndpoint(state) { + return RESTART_STATES.has(state) ? '/run' : null; } // ── Tests ───────────────────────────────────────────────────────────────────── describe('task card action buttons', () => { it('shows Run button for PENDING', () => { - assert.equal(getCardAction('PENDING'), 'run'); + assert.deepEqual(getCardActions('PENDING'), ['run']); }); it('shows Cancel button for RUNNING', () => { - assert.equal(getCardAction('RUNNING'), 'cancel'); + assert.deepEqual(getCardActions('RUNNING'), ['cancel']); }); - it('shows Restart button for FAILED', () => { - assert.equal(getCardAction('FAILED'), 'restart'); + it('shows Resume AND Restart buttons for FAILED', () => { + assert.deepEqual(getCardActions('FAILED'), ['resume', 'restart']); }); - it('shows Resume button for TIMED_OUT', () => { - assert.equal(getCardAction('TIMED_OUT'), 'resume'); + it('shows Resume AND Restart buttons for CANCELLED', () => { + assert.deepEqual(getCardActions('CANCELLED'), ['resume', 'restart']); }); - it('shows Restart button for CANCELLED', () => { - assert.equal(getCardAction('CANCELLED'), 'restart'); + it('shows Resume AND Restart buttons for BUDGET_EXCEEDED', () => { + assert.deepEqual(getCardActions('BUDGET_EXCEEDED'), ['resume', 'restart']); }); - it('shows Restart button for BUDGET_EXCEEDED', () => { - assert.equal(getCardAction('BUDGET_EXCEEDED'), 'restart'); + it('shows Resume button only for TIMED_OUT (no restart)', () => { + assert.deepEqual(getCardActions('TIMED_OUT'), ['resume']); }); it('shows approve buttons for READY', () => { - assert.equal(getCardAction('READY'), 'approve'); + assert.deepEqual(getCardActions('READY'), ['approve']); }); it('shows no button for COMPLETED', () => { - assert.equal(getCardAction('COMPLETED'), null); + assert.deepEqual(getCardActions('COMPLETED'), [null]); }); it('shows no button for QUEUED', () => { - assert.equal(getCardAction('QUEUED'), null); + assert.deepEqual(getCardActions('QUEUED'), [null]); }); }); describe('task action API endpoints', () => { it('TIMED_OUT uses /resume endpoint', () => { - assert.equal(getApiEndpoint('TIMED_OUT'), '/resume'); + assert.equal(getResumeEndpoint('TIMED_OUT'), '/resume'); + }); + + it('CANCELLED uses /resume endpoint for resume', () => { + assert.equal(getResumeEndpoint('CANCELLED'), '/resume'); + }); + + it('FAILED uses /resume endpoint for resume', () => { + assert.equal(getResumeEndpoint('FAILED'), '/resume'); + }); + + it('BUDGET_EXCEEDED uses /resume endpoint for resume', () => { + assert.equal(getResumeEndpoint('BUDGET_EXCEEDED'), '/resume'); + }); + + it('CANCELLED uses /run endpoint for restart', () => { + assert.equal(getRestartEndpoint('CANCELLED'), '/run'); }); - it('FAILED uses /run endpoint', () => { - assert.equal(getApiEndpoint('FAILED'), '/run'); + it('FAILED uses /run endpoint for restart', () => { + assert.equal(getRestartEndpoint('FAILED'), '/run'); }); - it('CANCELLED uses /run endpoint', () => { - assert.equal(getApiEndpoint('CANCELLED'), '/run'); + it('BUDGET_EXCEEDED uses /run endpoint for restart', () => { + assert.equal(getRestartEndpoint('BUDGET_EXCEEDED'), '/run'); }); - it('BUDGET_EXCEEDED uses /run endpoint', () => { - assert.equal(getApiEndpoint('BUDGET_EXCEEDED'), '/run'); + it('TIMED_OUT has no /run restart endpoint', () => { + assert.equal(getRestartEndpoint('TIMED_OUT'), null); }); }); |
