-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskDetail.tsx
More file actions
651 lines (622 loc) · 25.2 KB
/
TaskDetail.tsx
File metadata and controls
651 lines (622 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
'use client';
import React, { useCallback, useState } from 'react';
import { useRouter } from 'next/navigation';
import {
CCard,
CCardBody,
CCardHeader,
CAlert,
CBadge,
CButton,
CTable,
CTableHead,
CTableRow,
CTableHeaderCell,
CTableBody,
CTableDataCell,
CFormLabel,
CFormTextarea,
CFormCheck,
CFormSelect,
CRow,
CCol,
} from '@coreui/react-pro';
import { AgentLog } from './AgentLog';
import { TaskLog } from './TaskLog';
import { MaxTurnsInput } from './MaxTurnsInput';
import { VendorModelPicker } from './VendorModelPicker';
import { PatchesPanel, MessagesPanel } from './pro-loader';
import type { Task, TaskRun, TaskEvent, TaskStatus, GhostJobInfo, GitFlow, AgentVendor, ClaudeMode } from '@/lib/types';
import { STATUS_COLORS } from '@/lib/types';
interface TaskDetailProps {
task: Task;
runs: TaskRun[];
events: TaskEvent[];
ghost?: GhostJobInfo | null;
}
export function TaskDetail({ task, runs, events, ghost }: TaskDetailProps) {
const router = useRouter();
// Description / acceptance editable state
const [description, setDescription] = useState(task.description ?? '');
// Clean acceptance criteria from JSON formatting if present
const cleanAcceptance = (acceptance: string | null): string => {
if (!acceptance) return '';
// Remove JSON brackets and quotes if the content is wrapped in {"..."}
const trimmed = acceptance.trim();
if (trimmed.startsWith('{"') && trimmed.endsWith('"}')) {
return trimmed.slice(2, -2);
}
return trimmed;
};
const [acceptance, setAcceptance] = useState(cleanAcceptance(task.acceptance));
const [descSaving, setDescSaving] = useState(false);
const [descError, setDescError] = useState('');
const [descSaved, setDescSaved] = useState(false);
const handleSaveDesc = useCallback(async () => {
setDescSaving(true);
setDescError('');
setDescSaved(false);
try {
const res = await fetch(`/api/tasks/${task.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ description: description || null, acceptance: acceptance || null }),
});
if (!res.ok) {
const d = await res.json();
throw new Error(d.error || 'Save failed');
}
setDescSaved(true);
router.refresh();
} catch (err: unknown) {
setDescError(err instanceof Error ? err.message : 'Save failed');
} finally {
setDescSaving(false);
}
}, [task.id, description, acceptance, router]);
// Agent settings local edit state
const [agentMaxTurns, setAgentMaxTurns] = useState<number | null>(task.max_turns ?? 50);
const [agentVendor, setAgentVendor] = useState<AgentVendor>((task.agent_vendor ?? 'anthropic') as AgentVendor);
const [agentModel, setAgentModel] = useState<string>(task.claude_model ?? '');
const [agentBilling, setAgentBilling] = useState<ClaudeMode>((task.claude_mode ?? 'max') as ClaudeMode);
const [agentGitFlow, setAgentGitFlow] = useState<GitFlow>((task.git_flow ?? 'branch') as GitFlow);
const [backupVendor, setBackupVendor] = useState<AgentVendor>((task.backup_vendor ?? 'anthropic') as AgentVendor);
const [backupModel, setBackupModel] = useState<string>(task.backup_model ?? 'claude-sonnet-4-6');
const [agentSaving, setAgentSaving] = useState(false);
const [agentError, setAgentError] = useState('');
const [agentSaved, setAgentSaved] = useState(false);
const handleSaveAgent = useCallback(async () => {
setAgentSaving(true);
setAgentError('');
setAgentSaved(false);
try {
const res = await fetch(`/api/tasks/${task.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
max_turns: agentMaxTurns,
agent_vendor: agentVendor,
claude_model: agentModel.trim() || null,
claude_mode: agentBilling,
git_flow: agentGitFlow,
backup_vendor: backupVendor !== agentVendor ? backupVendor : null,
backup_model: backupModel.trim() || null,
}),
});
if (!res.ok) {
const d = await res.json();
throw new Error(d.error || 'Save failed');
}
setAgentSaved(true);
router.refresh();
} catch (err: unknown) {
setAgentError(err instanceof Error ? err.message : 'Save failed');
} finally {
setAgentSaving(false);
}
}, [task.id, agentMaxTurns, agentVendor, agentModel, agentBilling, agentGitFlow, backupVendor, backupModel, router]);
const handleDelete = useCallback(async () => {
if (!confirm(`Delete task "${task.task_key}"? This cannot be undone.`)) return;
await fetch(`/api/tasks/${task.id}`, { method: 'DELETE' });
router.push('/tasks');
}, [task.id, task.task_key, router]);
const handleFixGhost = useCallback(async () => {
await fetch('/api/worker', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'restart' }) });
router.refresh();
}, [router]);
const handleForceReopen = useCallback(async () => {
if (!confirm('This will reset the task to pending, clear all locks, and restart the worker. Continue?')) return;
// 1. Reset task status and clear job reference
await fetch(`/api/tasks/${task.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status: 'pending', queue_job_id: null }),
});
// 2. Clean locks + restart worker
await fetch('/api/worker', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'restart' }),
});
router.refresh();
}, [task.id, router]);
const handleEnqueue = useCallback(async () => {
await fetch(`/api/tasks/${task.id}/enqueue`, { method: 'POST' });
router.refresh();
}, [task.id, router]);
const handleCancel = useCallback(async () => {
await fetch(`/api/tasks/${task.id}/cancel`, { method: 'POST' });
router.refresh();
}, [task.id, router]);
const [continuing, setContinuing] = useState(false);
const handleContinue = useCallback(async () => {
if (!confirm('Continue this task with the current primary model? The in-flight run will be stopped and a new session will resume from where it left off.')) return;
setContinuing(true);
try {
const res = await fetch(`/api/tasks/${task.id}/continue`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model: null, mode: null }),
});
if (!res.ok) {
const d = await res.json().catch(() => ({}));
alert(d.error || 'Continue failed');
}
router.refresh();
} finally {
setContinuing(false);
}
}, [task.id, router]);
const [compacting, setCompacting] = useState(false);
const handleCompact = useCallback(async () => {
if (
!confirm(
"Summarise this task's prior transcript via the system LLM, replacing the per-retry context with a distilled summary? The next attempt will start with a fresh session.",
)
)
return;
setCompacting(true);
try {
const res = await fetch(`/api/tasks/${task.id}/compact`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ reason: 'manual_ui' }),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
alert(data.error || 'Compaction failed');
} else {
alert(
`Compacted ${data.chars_in ?? '?'} chars → ${data.chars_out ?? '?'} chars. Use "Continue" to resume with the summary.`,
);
}
router.refresh();
} finally {
setCompacting(false);
}
}, [task.id, router]);
const handleRetire = useCallback(async () => {
await fetch(`/api/tasks/${task.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status: 'retired' }),
});
router.refresh();
}, [task.id, router]);
const handleReopen = useCallback(async () => {
await fetch(`/api/tasks/${task.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ status: 'pending' }),
});
router.refresh();
}, [task.id, router]);
const [refreshing, setRefreshing] = useState(false);
const [refreshMsg, setRefreshMsg] = useState<{ type: 'success' | 'danger'; text: string } | null>(null);
const handleRefreshGit = useCallback(async () => {
if (!task.repo_id) return;
setRefreshing(true);
setRefreshMsg(null);
try {
const res = await fetch(`/api/repos/${task.repo_id}/refresh-git`, { method: 'POST' });
if (!res.ok) {
const data = await res.json().catch(() => null);
throw new Error(data?.error || 'Refresh failed');
}
const data = await res.json();
setRefreshMsg({ type: 'success', text: data.message });
} catch (err) {
setRefreshMsg({ type: 'danger', text: err instanceof Error ? err.message : 'Refresh failed' });
} finally {
setRefreshing(false);
}
}, [task.repo_id]);
const [filling, setFilling] = useState(false);
const [fillError, setFillError] = useState('');
const handleFillTask = useCallback(async () => {
if (!description.trim()) return;
setFilling(true);
setFillError('');
try {
const res = await fetch('/api/tasks/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ description }),
});
if (!res.ok) {
const data = await res.json().catch(() => null);
throw new Error(data?.error || 'devtask skill failed');
}
const generated = await res.json();
if (generated.description) setDescription(generated.description);
if (generated.acceptance) setAcceptance(generated.acceptance);
// Patch remaining fields on server (never overwrite title/key on existing tasks)
await fetch(`/api/tasks/${task.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
description: generated.description || undefined,
acceptance: generated.acceptance || undefined,
claude_model: generated.claude_model,
max_turns: generated.max_turns,
claude_mode: generated.claude_mode || undefined,
skip_verify: generated.skip_verify,
}),
});
router.refresh();
} catch (err) {
setFillError(err instanceof Error ? err.message : 'devtask skill failed');
} finally {
setFilling(false);
}
}, [description, task.id, router]);
return (
<>
{ghost?.detected && (
<CAlert color="warning" className="mb-4">
<strong>Ghost job detected.</strong> This task is stuck in the queue but the worker is not processing it
{ghost.lock_held ? ' (repo lock is held)' : ''}.
This happens when the worker crashes mid-task.
<CButton color="warning" size="sm" className="ms-3" onClick={handleFixGhost}>
Fix: Restart Worker
</CButton>
</CAlert>
)}
<div className="mb-4">
<h2 className="mb-1">{task.task_key}: {task.title}</h2>
<div className="mb-2">
<CBadge color={STATUS_COLORS[task.status as TaskStatus]}>
{task.status}
</CBadge>
{task.repo_name && (
<span className="ms-2 text-body-secondary">{task.repo_name}</span>
)}
</div>
<div className="d-flex flex-wrap gap-2">
<CButton
color="outline-secondary"
onClick={() => router.push('/tasks')}
>
Back to Tasks
</CButton>
{task.status === 'pending' && (
<CButton color="success" onClick={handleEnqueue}>
Enqueue
</CButton>
)}
{task.status === 'test' && (
<CButton color="info" onClick={handleReopen}>
Reopen
</CButton>
)}
{(task.status === 'running' || task.status === 'queued') && (
<CButton color="danger" onClick={handleCancel}>
Cancel
</CButton>
)}
{(['running', 'queued', 'failed', 'blocked', 'cancelled', 'test'].includes(task.status)) && (
<CButton
color="info"
disabled={continuing}
onClick={handleContinue}
title={
task.status === 'test'
? 'Resume this task with the existing session & branch to address a follow-up request from the Messages panel'
: 'Resume this task from where it left off'
}
>
{continuing ? 'Continuing…' : 'Continue'}
</CButton>
)}
{(['running', 'failed', 'blocked', 'cancelled'].includes(task.status)) && (
<CButton
color="secondary"
variant="outline"
disabled={compacting}
onClick={handleCompact}
title="Summarise prior attempts via the system LLM — next retry starts fresh with the summary"
>
{compacting ? 'Compacting…' : 'Compact'}
</CButton>
)}
{(task.status === 'running' || task.status === 'verifying' || task.status === 'failed' || task.status === 'queued') && (
<CButton color="warning" onClick={handleForceReopen}>
Force Reopen
</CButton>
)}
{(task.status === 'test' || task.status === 'failed' || task.status === 'cancelled') && (
<CButton color="dark" variant="outline" onClick={handleRetire}>
Retire
</CButton>
)}
<CButton
color="outline-danger"
onClick={handleDelete}
>
Delete
</CButton>
{task.repo_clone_url && (
<CButton
color="outline-primary"
href={task.repo_clone_url.replace(/\.git$/, '')}
target="_blank"
rel="noopener noreferrer"
as="a"
>
Repo Link
</CButton>
)}
{task.repo_id && (
<CButton
color="outline-info"
disabled={refreshing}
onClick={handleRefreshGit}
>
{refreshing ? 'Refreshing...' : 'Refresh Git'}
</CButton>
)}
{refreshMsg && (
<span className={`small text-${refreshMsg.type} align-self-center`}>{refreshMsg.text}</span>
)}
</div>
</div>
<CRow>
<CCol lg={8}>
{/* Description + Acceptance — editable */}
<CCard className="mb-4">
<CCardHeader><strong>Description & Acceptance Criteria</strong></CCardHeader>
<CCardBody>
{descError && <CAlert color="danger" className="py-2">{descError}</CAlert>}
{descSaved && <CAlert color="success" className="py-2">Saved.</CAlert>}
<div className="mb-3">
<div className="d-flex align-items-center gap-2 mb-1">
<CFormLabel className="mb-0">Description</CFormLabel>
{task.status !== 'retired' && (
<CButton
type="button"
color="info"
variant="outline"
size="sm"
disabled={filling || !description.trim()}
onClick={handleFillTask}
>
{filling ? 'Filling...' : 'Fill Task'}
</CButton>
)}
{fillError && (
<span className="text-danger small">{fillError}</span>
)}
</div>
<CFormTextarea
value={description}
onChange={(e) => setDescription(e.target.value)}
rows={5}
placeholder="Detailed task description…"
/>
</div>
<div className="mb-3">
<CFormLabel className="mb-1">Acceptance Criteria</CFormLabel>
<CFormTextarea
value={acceptance}
onChange={(e) => setAcceptance(e.target.value)}
rows={3}
placeholder="What conditions must be met for this task to be considered done?"
/>
</div>
<CButton color="primary" size="sm" disabled={descSaving} onClick={handleSaveDesc}>
{descSaving ? 'Saving…' : 'Save'}
</CButton>
</CCardBody>
</CCard>
{/* Messages — two-way chat between operator and agent via task_messages.
Placed above the Event Log so the operator can easily talk to the
agent without scrolling the right-hand sidebar. */}
<MessagesPanel taskId={task.id} taskKey={task.task_key} />
{/* Live Log */}
<CCard className="mb-4">
<CCardHeader><strong>Event Log</strong></CCardHeader>
<CCardBody>
<AgentLog events={events} maxHeight="400px" />
</CCardBody>
</CCard>
{/* File-based Task Log */}
<CCard className="mb-4">
<CCardHeader><strong>Task Log</strong></CCardHeader>
<CCardBody>
<TaskLog taskKey={task.task_key} maxHeight="600px" />
</CCardBody>
</CCard>
</CCol>
<CCol lg={4}>
{/* Task Info */}
<CCard className="mb-4">
<CCardHeader><strong>Details</strong></CCardHeader>
<CCardBody>
<dl className="mb-0">
<dt>Billing</dt>
<dd>{task.claude_mode === 'max' ? 'Max (subscription)' : 'API Platform'}</dd>
<dt>Queue Job ID</dt>
<dd className="text-break">{task.queue_job_id || '-'}</dd>
<dt>Created</dt>
<dd suppressHydrationWarning>{new Date(task.created_at).toLocaleString()}</dd>
</dl>
</CCardBody>
</CCard>
{/* Agent Settings */}
<CCard className="mb-4">
<CCardHeader><strong>Agent Settings</strong></CCardHeader>
<CCardBody>
{agentError && <CAlert color="danger" className="py-2">{agentError}</CAlert>}
{agentSaved && <CAlert color="success" className="py-2">Saved.</CAlert>}
<div className="mb-3">
<CFormLabel className="mb-1">Max Turns</CFormLabel>
<MaxTurnsInput
value={agentMaxTurns}
onChange={setAgentMaxTurns}
/>
<small className="text-body-secondary">
Current: {task.max_turns === null ? 'Unlimited' : (task.max_turns ?? 50)}
</small>
</div>
<div className="mb-3">
<CFormLabel className="mb-1">Billing</CFormLabel>
<CFormSelect
value={agentBilling}
onChange={(e) => setAgentBilling(e.target.value as ClaudeMode)}
>
<option value="api">API Platform</option>
<option value="max">Max (subscription)</option>
</CFormSelect>
<small className="text-body-secondary">
{agentBilling === 'max'
? 'Uses vendor OAuth / subscription login (Claude Max, ChatGPT Plus, Google OAuth, Qwen OAuth).'
: 'Uses the vendor API-key env var from .env.'}
</small>
</div>
<div className="mb-3">
<CFormLabel className="mb-1">Agent vendor & model</CFormLabel>
<VendorModelPicker
vendor={agentVendor}
model={agentModel}
onVendorChange={setAgentVendor}
onModelChange={setAgentModel}
modelLabel="Model"
modelPlaceholder="Leave blank to use repo default"
/>
{task.claude_model && (
<small className="text-body-secondary">
Current: {task.agent_vendor ?? 'anthropic'} · {task.claude_model}
</small>
)}
</div>
<div className="mb-3">
<CFormLabel className="mb-1">Backup model</CFormLabel>
<VendorModelPicker
vendor={backupVendor}
model={backupModel}
onVendorChange={setBackupVendor}
onModelChange={setBackupModel}
modelLabel="Backup Model"
modelPlaceholder="Auto-fallback model (default: claude-sonnet-4-6)"
vendorName="backup_vendor"
modelName="backup_model"
/>
<small className="text-body-secondary">
Auto-failover if primary vendor/model fails after all retries.
{task.backup_vendor || task.backup_model
? ` Current: ${task.backup_vendor ? `${task.backup_vendor}/` : ''}${task.backup_model || '(default model)'}`
: ''}
</small>
</div>
<div className="mb-3">
<CFormLabel className="mb-1">Git flow</CFormLabel>
<CFormSelect
value={agentGitFlow}
onChange={(e) => setAgentGitFlow(e.target.value as GitFlow)}
>
<option value="branch">Branch + PR</option>
<option value="commit">Direct commit</option>
<option value="patch">Patch only</option>
</CFormSelect>
</div>
<div className="mb-3">
<CFormLabel className="mb-1">Verification</CFormLabel>
<CFormCheck
id="skip_verify"
label="Skip verification"
checked={task.skip_verify}
onChange={async (e) => {
await fetch(`/api/tasks/${task.id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ skip_verify: e.target.checked }),
});
router.refresh();
}}
/>
</div>
<CButton
color="primary"
size="sm"
disabled={agentSaving}
onClick={handleSaveAgent}
>
{agentSaving ? 'Saving…' : 'Save'}
</CButton>
</CCardBody>
</CCard>
{/* Patches (Option A — downloadable format-patch output for manual
application to a production / mirror repo). */}
<PatchesPanel taskKey={task.task_key} taskStatus={task.status} />
{/* Run History */}
<CCard className="mb-4">
<CCardHeader><strong>Run History ({runs.length})</strong></CCardHeader>
<CCardBody className="p-0">
{runs.length === 0 ? (
<p className="p-3 mb-0 text-body-secondary">No runs yet.</p>
) : (
<CTable small hover responsive className="mb-0">
<CTableHead>
<CTableRow>
<CTableHeaderCell>Time</CTableHeaderCell>
<CTableHeaderCell>Status</CTableHeaderCell>
<CTableHeaderCell>Duration</CTableHeaderCell>
<CTableHeaderCell>Cost</CTableHeaderCell>
</CTableRow>
</CTableHead>
<CTableBody>
{[...runs].sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()).map((run) => (
<CTableRow key={run.id}>
<CTableDataCell suppressHydrationWarning>
{new Date(run.created_at).toLocaleDateString(undefined, { day: 'numeric', month: 'short' })}{' '}
<small className="text-body-secondary">
{new Date(run.created_at).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })}
</small>
</CTableDataCell>
<CTableDataCell>
<CBadge color={
run.status === 'passed' ? 'success' :
run.status === 'failed' ? 'danger' :
run.status === 'running' ? 'info' : 'secondary'
}>
{run.status}
</CBadge>
</CTableDataCell>
<CTableDataCell>
{run.duration_ms ? `${(run.duration_ms / 1000).toFixed(1)}s` : '-'}
</CTableDataCell>
<CTableDataCell>
{run.cost_usd ? `$${Number(run.cost_usd).toFixed(2)}` : '-'}
</CTableDataCell>
</CTableRow>
))}
</CTableBody>
</CTable>
)}
</CCardBody>
</CCard>
</CCol>
</CRow>
</>
);
}