dsh-taskboard 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +15 -0
  2. package/lib/client.js +219 -161
  3. package/lib/host/execution.js +80 -33
  4. package/lib/host/execution.js.map +1 -1
  5. package/lib/host/git.js +49 -5
  6. package/lib/host/git.js.map +1 -1
  7. package/lib/host/routes.js +180 -109
  8. package/lib/host/routes.js.map +1 -1
  9. package/lib/host/scheduler.js +50 -28
  10. package/lib/host/scheduler.js.map +1 -1
  11. package/lib/host/sdk.js +7 -2
  12. package/lib/host/sdk.js.map +1 -1
  13. package/lib/host/store.js +41 -8
  14. package/lib/host/store.js.map +1 -1
  15. package/lib/host/templates.js +10 -3
  16. package/lib/host/templates.js.map +1 -1
  17. package/lib/host/tools.js +124 -93
  18. package/lib/host/tools.js.map +1 -1
  19. package/lib/index.js +3 -1
  20. package/lib/index.js.map +1 -1
  21. package/lib/shared/api.js.map +1 -1
  22. package/lib/shared/protocol.js +23 -2
  23. package/lib/shared/protocol.js.map +1 -1
  24. package/package.json +3 -2
  25. package/src/client/api.ts +19 -9
  26. package/src/client/board/ImportModal.tsx +1 -1
  27. package/src/client/board/TaskBoard.tsx +7 -38
  28. package/src/client/board/TaskCard.tsx +3 -5
  29. package/src/client/board/TaskDetail.tsx +30 -21
  30. package/src/client/board/TaskFormModal.tsx +30 -23
  31. package/src/client/board/format.ts +26 -0
  32. package/src/client/board/labels.ts +44 -0
  33. package/src/client/controller.ts +60 -13
  34. package/src/client/index.ts +7 -5
  35. package/src/client/sidebar-entry.ts +5 -1
  36. package/src/host/execution.ts +90 -16
  37. package/src/host/git.ts +39 -10
  38. package/src/host/routes.ts +227 -126
  39. package/src/host/scheduler.ts +62 -36
  40. package/src/host/sdk.ts +12 -1
  41. package/src/host/store.ts +53 -7
  42. package/src/host/templates.ts +12 -3
  43. package/src/host/tools.ts +180 -123
  44. package/src/index.ts +10 -1
  45. package/src/shared/api.ts +1 -1
  46. package/src/shared/protocol.ts +35 -1
  47. package/src/shared/version.ts +1 -1
  48. package/src/client/board/NewTaskModal.tsx +0 -8
@@ -13,7 +13,7 @@ import type { BoardController } from '../controller.ts'
13
13
  import type { TaskTemplateSpec } from '../../shared/api.ts'
14
14
  import type { ChecklistItem, IsolationMode, Urgency } from '../../shared/protocol.ts'
15
15
  import { MAX_CHECKLIST_ITEMS, defaultIsolationOf, nextCronTime, parseCron } from '../../shared/protocol.ts'
16
- import { fmtTime } from './TaskBoard.tsx'
16
+ import { fmtTime } from './format.ts'
17
17
 
18
18
  /** One row of the configured model catalog (from llm.models). */
19
19
  export interface CatalogModel { provider: string; model: string; name?: string }
@@ -144,6 +144,10 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
144
144
  : (prefill?.checklist ?? []).map(text => ({ text, checked: false })),
145
145
  )
146
146
  const titleRef = useRef<HTMLInputElement>(null)
147
+ // One in-flight write at a time: the foot buttons disable while a
148
+ // create/update/run round-trip is pending — a double click used to fire
149
+ // duplicate creates (and runs) before the first one returned (review P0).
150
+ const [busy, setBusy] = useState(false)
147
151
 
148
152
  // Focus the title and close on Esc while the dialog is open.
149
153
  useEffect(() => {
@@ -155,9 +159,9 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
155
159
  return () => document.removeEventListener('keydown', onKey)
156
160
  }, [controller])
157
161
 
158
- // Model catalog: the plugin face provides it when the runtime is up.
162
+ // Model catalog: the controller exposes the installed face when the runtime is up.
159
163
  useEffect(() => {
160
- const face = (controller as unknown as { modelCatalog?: () => Promise<CatalogModel[]> }).modelCatalog
164
+ const face = controller.modelCatalog
161
165
  if (face === undefined) return
162
166
  void face().then(setCatalog).catch(() => setCatalog([]))
163
167
  }, [controller])
@@ -166,14 +170,18 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
166
170
  // create mode (unless a template pinned one) so executions run with a
167
171
  // real tool set out of the box.
168
172
  useEffect(() => {
169
- const face = (controller as unknown as { presetCatalog?: () => Promise<{ presets: Array<{ id: string; name?: string }>; defaultId?: string }> }).presetCatalog
173
+ const face = controller.presetCatalog
170
174
  if (face === undefined) return
171
175
  void face().then(roster => {
172
176
  setPresets(roster.presets)
173
177
  setPresetDefault(roster.defaultId)
174
- if (task?.presetId === undefined && initialPreset === '' && roster.defaultId !== undefined) setPresetId(roster.defaultId)
178
+ // CREATE mode only (review P1): pre-selecting in edit mode would
179
+ // silently pin the deployment default onto tasks that deliberately
180
+ // follow it. In create mode `task` is undefined, so checking
181
+ // `initialPreset` (template pin) alone is sufficient.
182
+ if (!editing && initialPreset === '' && roster.defaultId !== undefined) setPresetId(roster.defaultId)
175
183
  }).catch(() => setPresets([]))
176
- }, [controller, task?.presetId, initialPreset])
184
+ }, [controller, editing, task?.presetId, initialPreset])
177
185
 
178
186
  // Live cron validation + next-run preview (same math as the host).
179
187
  const cronMatch = mode === 'scheduled' ? parseCron(cron.trim()) : null
@@ -208,13 +216,14 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
208
216
  const filledRows = (): CheckRow[] => checkRows.map(r => ({ ...r, text: r.text.trim() })).filter(r => r.text.length > 0)
209
217
 
210
218
  const submit = (): void => {
211
- if (!valid) return
219
+ if (!valid || busy) return
212
220
  const picked = model !== '' ? (JSON.parse(model) as { provider: string; model: string }) : undefined
213
221
  const isolationOut = isolationPayload()
214
222
  const presetOut = presetPayload()
215
223
  const rows = filledRows()
216
- if (editing) {
217
- void controller.update(task.id, task.version, {
224
+ setBusy(true)
225
+ const action = editing
226
+ ? controller.update(task.id, task.version, {
218
227
  title,
219
228
  description,
220
229
  prompt,
@@ -228,8 +237,7 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
228
237
  // [] clears the checklist (host deletes the field on empty).
229
238
  checklist: rows.length > 0 ? rows : null,
230
239
  })
231
- } else {
232
- void controller.create({
240
+ : controller.create({
233
241
  title,
234
242
  workspaceId,
235
243
  urgency,
@@ -241,18 +249,19 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
241
249
  ...(presetOut !== undefined ? { presetId: presetOut } : {}),
242
250
  ...(rows.length > 0 ? { checklist: rows.map(r => r.text) } : {}),
243
251
  })
244
- }
252
+ void action.catch(() => undefined).finally(() => setBusy(false))
245
253
  }
246
254
 
247
255
  /** Save the form, then immediately trigger a manual run of the task. */
248
256
  const submitAndRun = (): void => {
249
- if (!valid || runBlocked) return
257
+ if (!valid || runBlocked || busy) return
250
258
  const picked = model !== '' ? (JSON.parse(model) as { provider: string; model: string }) : undefined
251
259
  const isolationOut = isolationPayload()
252
260
  const presetOut = presetPayload()
253
261
  const rows = filledRows()
254
- if (editing) {
255
- void (async () => {
262
+ setBusy(true)
263
+ void (async () => {
264
+ if (editing) {
256
265
  const saved = await controller.update(task.id, task.version, {
257
266
  title,
258
267
  description,
@@ -266,9 +275,7 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
266
275
  checklist: rows.length > 0 ? rows : null,
267
276
  })
268
277
  if (saved) await controller.run(task.id)
269
- })()
270
- } else {
271
- void (async () => {
278
+ } else {
272
279
  const id = await controller.create({
273
280
  title,
274
281
  workspaceId,
@@ -282,8 +289,8 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
282
289
  ...(rows.length > 0 ? { checklist: rows.map(r => r.text) } : {}),
283
290
  })
284
291
  if (id !== undefined) await controller.run(id)
285
- })()
286
- }
292
+ }
293
+ })().catch(() => undefined).finally(() => setBusy(false))
287
294
  }
288
295
 
289
296
  const hint = !valid
@@ -450,13 +457,13 @@ export function TaskFormModal({ controller, task }: { controller: BoardControlle
450
457
  <button
451
458
  type="button"
452
459
  className="dsh-atb-btn"
453
- disabled={!valid || runBlocked}
454
- title={runBlocked ? '任务正在执行中,不能重复发起' : '保存后立即发起执行(新会话)'}
460
+ disabled={!valid || runBlocked || busy}
461
+ title={runBlocked ? '任务正在执行中,不能重复发起' : busy ? '正在提交…' : '保存后立即发起执行(新会话)'}
455
462
  onClick={submitAndRun}
456
463
  >
457
464
  ⚡ 立即执行
458
465
  </button>
459
- <button type="button" className="dsh-atb-btn" data-primary="true" disabled={!valid} onClick={submit}>
466
+ <button type="button" className="dsh-atb-btn" data-primary="true" disabled={!valid || busy} onClick={submit}>
460
467
  {editing ? '保存修改' : '创建任务'}
461
468
  </button>
462
469
  </span>
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Pure display helpers shared by the board components.
3
+ *
4
+ * Review P2: these lived in TaskBoard.tsx and were imported back out by
5
+ * TaskCard/TaskDetail/TaskFormModal, forming import cycles with the view
6
+ * root. They have no component dependencies — they belong here.
7
+ *
8
+ * @module dsh-taskboard/client/board/format
9
+ */
10
+ import type { TaskRecord } from '../../shared/protocol.ts'
11
+
12
+ /** Format an epoch ms as a short local stamp. */
13
+ export function fmtTime(ms: number | undefined): string {
14
+ if (ms === undefined) return ''
15
+ const d = new Date(ms)
16
+ const pad = (n: number) => String(n).padStart(2, '0')
17
+ return `${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
18
+ }
19
+
20
+ /** A claim idle for longer than this is highlighted as stale (ms). */
21
+ export const STALE_CLAIM_MS = 30 * 60_000
22
+
23
+ /** Whether the task's claim is stale (in_progress, held, idle too long). */
24
+ export function isStaleClaim(task: TaskRecord, now: number): boolean {
25
+ return task.status === 'in_progress' && task.claimedAt !== undefined && now - task.claimedAt > STALE_CLAIM_MS
26
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Centralized zh-CN display labels for the board UI.
3
+ *
4
+ * Review P2: the same status/urgency/outcome text used to live in three
5
+ * components (TaskBoard / TaskCard / TaskDetail) and drifted — adding a
6
+ * status meant three edits, missing one leaked the raw English key.
7
+ *
8
+ * @module dsh-taskboard/client/board/labels
9
+ */
10
+ import type { TaskStatus, Urgency } from '../../shared/protocol.ts'
11
+
12
+ /** Column headers on the five-column main board (+ secondary tab). */
13
+ export const COLUMN_LABELS: Readonly<Record<TaskStatus, string>> = {
14
+ backlog: '待规划',
15
+ todo: '待办',
16
+ in_progress: '进行中',
17
+ in_review: '待验收',
18
+ done: '已完成',
19
+ canceled: '已取消',
20
+ archived: '已归档',
21
+ }
22
+
23
+ /** Status pill text (detail pane) — historical wording kept verbatim:
24
+ * terminal states read short here, the column headers carry the full forms. */
25
+ export const STATUS_LABEL: Readonly<Record<TaskStatus, string>> = {
26
+ backlog: '待规划', todo: '待办', in_progress: '进行中', in_review: '待验收',
27
+ done: '完成', canceled: '取消', archived: '归档',
28
+ }
29
+
30
+ /** Move-button verbs (shorter than the pill text). */
31
+ export const MOVE_LABEL: Readonly<Record<TaskStatus, string>> = {
32
+ backlog: '待规划', todo: '待办', in_progress: '进行中', in_review: '待验收',
33
+ done: '完成', canceled: '取消', archived: '归档',
34
+ }
35
+
36
+ /** Urgency chip labels. */
37
+ export const URGENCY_LABEL: Readonly<Record<Urgency, string>> = {
38
+ urgent: '紧急', normal: '一般', relaxed: '不急',
39
+ }
40
+
41
+ /** Execution outcome labels. */
42
+ export const OUTCOME_LABEL: Readonly<Record<string, string>> = {
43
+ running: '执行中', succeeded: '成功', failed: '失败', cancelled: '已取消',
44
+ }
@@ -109,7 +109,14 @@ export class BoardController {
109
109
  private disposed = false
110
110
  private disposeStream: (() => void) | undefined
111
111
  private refreshInFlight: Promise<void> | undefined
112
+ /** Newest change-frame revision seen on the SSE stream (S16 refresh chase). */
113
+ private seenRevision: number | undefined
112
114
  private sessionJumper: ((sessionId: string) => Promise<SessionJumpResult>) | undefined
115
+ /** Composer catalog faces, installed formally by the client entry (T13). */
116
+ private readonly catalogFaces: {
117
+ models?: () => Promise<Array<{ provider: string; model: string; name?: string }>>
118
+ presets?: () => Promise<{ presets: Array<{ id: string; name?: string }>; defaultId?: string }>
119
+ } = {}
113
120
 
114
121
  /** @param client - the route client. */
115
122
  constructor(private readonly client: TaskboardClient) {}
@@ -140,8 +147,10 @@ export class BoardController {
140
147
  void this.refresh()
141
148
  this.disposeStream = this.client.stream(
142
149
  (change: ChangeEvent) => {
143
- this.setState({ ledger: { ...this.state.ledger, revision: change.revision } })
150
+ this.seenRevision = change.revision
144
151
  // Any change invalidates the full snapshot; refetch (cheap, local).
152
+ // No intermediate revision-only setState — the refresh result is the
153
+ // single render a frame produces (review P2: frames rendered twice).
145
154
  void this.refresh()
146
155
  },
147
156
  () => { void this.refresh() },
@@ -153,15 +162,23 @@ export class BoardController {
153
162
  if (this.refreshInFlight !== undefined) return this.refreshInFlight
154
163
  this.refreshInFlight = (async () => {
155
164
  try {
156
- const [ledger, workspaces] = await Promise.all([
157
- this.client.state(),
158
- this.client.workspaces(),
159
- ])
160
- let selected: TaskRecord | undefined
161
- if (this.state.selectedId !== undefined) {
162
- selected = ledger.tasks.find(t => t.id === this.state.selectedId)
165
+ // S16: a change frame landing while a fetch is in flight used to
166
+ // strand the board on a stale snapshot forever (the deduped request
167
+ // predates the newest frame and no further event arrives). Chase the
168
+ // newest seen revision — bounded rounds, then give up until the next
169
+ // frame.
170
+ for (let round = 0; round < 3; round++) {
171
+ const [ledger, workspaces] = await Promise.all([
172
+ this.client.state(),
173
+ this.client.workspaces(),
174
+ ])
175
+ let selected: TaskRecord | undefined
176
+ if (this.state.selectedId !== undefined) {
177
+ selected = ledger.tasks.find(t => t.id === this.state.selectedId)
178
+ }
179
+ this.setState({ ledger, workspaces, error: undefined, selectedId: selected === undefined ? undefined : this.state.selectedId })
180
+ if (this.seenRevision === undefined || ledger.revision >= this.seenRevision) break
163
181
  }
164
- this.setState({ ledger, workspaces, error: undefined, selectedId: selected === undefined ? undefined : this.state.selectedId })
165
182
  } catch (error) {
166
183
  this.setState({ error: error instanceof Error ? error.message : String(error) })
167
184
  } finally {
@@ -260,6 +277,26 @@ export class BoardController {
260
277
  this.sessionJumper = jumper
261
278
  }
262
279
 
280
+ /** T13: formal installers for the composer catalog faces (was a monkeypatch from the client entry). */
281
+ installModelCatalog(fn: () => Promise<Array<{ provider: string; model: string; name?: string }>>): void {
282
+ this.catalogFaces.models = fn
283
+ }
284
+
285
+ /** T13: formal installer for the preset roster face. */
286
+ installPresetRoster(fn: () => Promise<{ presets: Array<{ id: string; name?: string }>; defaultId?: string }>): void {
287
+ this.catalogFaces.presets = fn
288
+ }
289
+
290
+ /** The installed model catalog face, when the runtime provides one. */
291
+ get modelCatalog(): (() => Promise<Array<{ provider: string; model: string; name?: string }>>) | undefined {
292
+ return this.catalogFaces.models
293
+ }
294
+
295
+ /** The installed preset roster face, when the runtime provides one. */
296
+ get presetCatalog(): (() => Promise<{ presets: Array<{ id: string; name?: string }>; defaultId?: string }>) | undefined {
297
+ return this.catalogFaces.presets
298
+ }
299
+
263
300
  /**
264
301
  * Jump to an execution's session (open it in the GUI). On success the board
265
302
  * closes so the conversation shows; a deleted-or-archived session reports
@@ -373,13 +410,18 @@ export class BoardController {
373
410
  }
374
411
  }
375
412
 
376
- /** Append a user comment. */
377
- async comment(id: string, body: string): Promise<void> {
413
+ /**
414
+ * Append a user comment. Returns whether it landed — the composer keeps its
415
+ * text on failure (T13: it used to clear unconditionally and lose the draft).
416
+ */
417
+ async comment(id: string, body: string): Promise<boolean> {
378
418
  try {
379
419
  await this.client.comment(id, body)
380
420
  await this.refresh()
421
+ return true
381
422
  } catch (error) {
382
423
  this.setState({ error: error instanceof Error ? error.message : String(error) })
424
+ return false
383
425
  }
384
426
  }
385
427
 
@@ -490,7 +532,8 @@ export class BoardController {
490
532
  async duplicate(task: TaskRecord): Promise<void> {
491
533
  try {
492
534
  await this.client.create({
493
- title: `${task.title}(副本)`,
535
+ // Keep the suffix under the host's 200-char title cap (review P1).
536
+ title: `${task.title.slice(0, 196)}(副本)`,
494
537
  workspaceId: task.workspaceId,
495
538
  urgency: task.urgency,
496
539
  description: task.description.length > 0 ? task.description : undefined,
@@ -620,7 +663,11 @@ export class BoardController {
620
663
  /** Download the task list as a CSV (BOM-prefixed for Excel + Chinese text). */
621
664
  exportCsv(): void {
622
665
  const esc = (v: unknown): string => {
623
- const s = String(v ?? '')
666
+ let s = String(v ?? '')
667
+ // S17: formula-injection guard — title/description are agent-controllable
668
+ // and a cell starting with = + - @ would be EXECUTED as a formula by
669
+ // Excel; neutralize with a leading apostrophe.
670
+ if (/^[=+\-@\t\r]/.test(s)) s = `'${s}`
624
671
  return /[",\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s
625
672
  }
626
673
  const header = ['id', 'title', 'status', 'urgency', 'blocked', 'project', 'claimedBy', 'mode', 'cron', 'nextRunAt', 'model', 'createdAt', 'updatedAt', 'comments', 'executions']
@@ -52,11 +52,13 @@ export function apply(ctx: ClientContextFace): void {
52
52
  const client = createClient()
53
53
  const controller = new BoardController(client)
54
54
 
55
- // Model catalog for the composer: llm.models over the connection RPC.
55
+ // Model catalog for the composer: llm.models over the connection RPC
56
+ // installed through the controller's formal installer (T13: no more
57
+ // monkeypatched instance properties).
56
58
  const connection = ctx.get?.('connection') as ConnectionFace | undefined
57
59
  if (connection !== undefined) {
58
60
  type CatalogRow = { provider: string; model: string; name?: string }
59
- ;(controller as unknown as { modelCatalog?: () => Promise<CatalogRow[]> }).modelCatalog = async (): Promise<CatalogRow[]> => {
61
+ controller.installModelCatalog(async (): Promise<CatalogRow[]> => {
60
62
  const response = await connection.api.llm.models({})
61
63
  if (!response.result.ok) return []
62
64
  const out: CatalogRow[] = []
@@ -66,13 +68,13 @@ export function apply(ctx: ClientContextFace): void {
66
68
  }
67
69
  }
68
70
  return out
69
- }
71
+ })
70
72
 
71
73
  // Preset roster for the composer (0.3.3): agentPreset.list over the
72
74
  // connection RPC — [{id, name}] plus which one is the deployment
73
75
  // default (the form pre-selects it on create).
74
76
  type PresetRow = { id: string; name?: string }
75
- ;(controller as unknown as { presetCatalog?: () => Promise<{ presets: PresetRow[]; defaultId?: string }> }).presetCatalog = async (): Promise<{ presets: PresetRow[]; defaultId?: string }> => {
77
+ controller.installPresetRoster(async (): Promise<{ presets: PresetRow[]; defaultId?: string }> => {
76
78
  const list = connection.api.agentPresets
77
79
  if (list === undefined) return { presets: [] }
78
80
  const response = await list.list({})
@@ -80,7 +82,7 @@ export function apply(ctx: ClientContextFace): void {
80
82
  const presets = response.result.value.presets.map((p: { id: string; name?: string }) => ({ id: p.id, name: p.name }))
81
83
  const def = response.result.value.presets.find((p: { id: string; isDefault: boolean }) => p.isDefault)
82
84
  return { presets, ...(def !== undefined ? { defaultId: def.id } : {}) }
83
- }
85
+ })
84
86
  }
85
87
 
86
88
  // Session navigation for execution rows: resolved LAZILY on every jump —
@@ -196,7 +196,11 @@ interface AtbDebug { attempts: number; found: boolean; placed: boolean }
196
196
  export function mountSidebarEntry(controller: BoardController): () => void {
197
197
  const entry = createEntry(controller)
198
198
  const debug: AtbDebug = { attempts: 0, found: false, placed: false }
199
- ;(window as unknown as { __atbDebug?: AtbDebug }).__atbDebug = debug
199
+ // Debug handle only where the GUI runs locally; never on remote origins.
200
+ const host = globalThis.location?.hostname
201
+ if (host === 'localhost' || host === '127.0.0.1') {
202
+ ;(window as unknown as { __atbDebug?: AtbDebug }).__atbDebug = debug
203
+ }
200
204
  let root: HTMLElement | undefined
201
205
  let placed = false
202
206
 
@@ -119,10 +119,8 @@ function isErrorTurnEnd(data: unknown): { message: string } | undefined {
119
119
  const kind = (reason as { kind?: unknown }).kind
120
120
  if (kind !== 'error') return undefined
121
121
  const error = (reason as { error?: { message?: unknown } }).error
122
- const detail = JSON.stringify(error) ?? ''
123
122
  const message = typeof error?.message === 'string' ? error.message : 'turn failed'
124
- console.error('[dsh-taskboard] turn error detail:', detail.slice(0, 2000))
125
- void detail
123
+ console.error('[dsh-taskboard] turn error detail:', JSON.stringify(error)?.slice(0, 2000) ?? '')
126
124
  return { message }
127
125
  }
128
126
 
@@ -161,15 +159,32 @@ export class ExecutionService {
161
159
  /** Live executions by execution id (settles and cancels remove entries). */
162
160
  private readonly runs = new Map<string, RunEntry>()
163
161
 
162
+ /** Detaches the turn/end listener (plugin teardown — review P1). */
163
+ private readonly unsubscribeEvents: () => void
164
+
164
165
  /** @param deps - store + agents + workspaces + events + clock. */
165
166
  constructor(private readonly deps: ExecutionDeps) {
166
- deps.events.onSessionEvent((sessionId, event) => {
167
+ this.unsubscribeEvents = deps.events.onSessionEvent((sessionId, event) => {
167
168
  if (event.type !== 'turn/end') return
169
+ // S7 (open question): ANY turn/end with an error reason fails the whole
170
+ // execution and hands the task back. Whether the DSH session loop can
171
+ // produce recoverable per-turn errors (and keep the session alive) needs
172
+ // host-side confirmation; if it can, this should count consecutive
173
+ // errors or wait for an explicit termination signal instead.
168
174
  const failure = isErrorTurnEnd(event.data)
169
- if (failure !== undefined) this.noteFailure(sessionId, failure.message)
175
+ if (failure !== undefined) {
176
+ this.noteFailure(sessionId, failure.message).catch(error => {
177
+ console.error('[dsh-taskboard] failure settlement error:', error)
178
+ })
179
+ }
170
180
  })
171
181
  }
172
182
 
183
+ /** Detach the settlement listener; safe to call once at plugin teardown. */
184
+ dispose(): void {
185
+ this.unsubscribeEvents()
186
+ }
187
+
173
188
  /**
174
189
  * Best-effort evidence collection for a prepared run (fail-soft: undefined
175
190
  * on any git problem — settlement NEVER blocks on git).
@@ -195,13 +210,19 @@ export class ExecutionService {
195
210
  if (facts.diffStat !== undefined) execution.diffStat = facts.diffStat
196
211
  }
197
212
 
198
- /** Record a turn failure against the running execution of that session and give the task back. */
199
- private noteFailure(sessionId: string, message: string): void {
213
+ /**
214
+ * Record a turn failure against the running execution of that session and
215
+ * give the task back. Resolves once the failure settlement has COMMITTED —
216
+ * R2: the whenIdle rejection path awaits this (and only this) before
217
+ * releasing its run entry, so a success settlement can never race it into
218
+ * the ledger and record a failed run as succeeded.
219
+ */
220
+ private noteFailure(sessionId: string, message: string): Promise<void> {
200
221
  // The failed session may already have committed work — collect the
201
222
  // evidence (best effort) BEFORE marking the execution failed (0.3.1).
202
223
  const entry = [...this.runs.values()].find(e => e.sessionId === sessionId)
203
- void this.collectEvidence(entry?.prepared).then(facts => {
204
- void this.deps.store.mutate('execution-recorded', (ledger) => {
224
+ return this.collectEvidence(entry?.prepared).then(facts =>
225
+ this.deps.store.mutate('execution-recorded', (ledger) => {
205
226
  for (const task of ledger.tasks) {
206
227
  for (const execution of task.executions) {
207
228
  if (execution.sessionId === sessionId && execution.outcome === 'running') {
@@ -229,16 +250,22 @@ export class ExecutionService {
229
250
  }
230
251
  }
231
252
  return undefined
232
- })
233
- })
253
+ }),
254
+ ).then(() => { /* failure settlement committed */ })
234
255
  }
235
256
 
236
- /** Patch one task's execution record in the ledger. */
257
+ /**
258
+ * Patch one task's execution record in the ledger. R3 depth: a record that
259
+ * already settled (cancelled/failed/succeeded) is never resurrected — the
260
+ * startup path patches sessionId long after the gate opened, and a cancel
261
+ * may have committed in between.
262
+ */
237
263
  private async patchExecution(executionId: string, patch: Partial<ExecutionRecord>): Promise<void> {
238
264
  await this.deps.store.mutate('execution-recorded', (ledger) => {
239
265
  for (const task of ledger.tasks) {
240
266
  const execution = task.executions.find(e => e.id === executionId)
241
267
  if (execution !== undefined) {
268
+ if (execution.outcome !== 'running') return undefined
242
269
  Object.assign(execution, patch)
243
270
  return [task]
244
271
  }
@@ -296,6 +323,14 @@ export class ExecutionService {
296
323
  gate = 'task is already in progress'
297
324
  return undefined
298
325
  }
326
+ // S4: authoritative capacity check INSIDE the gate — counts ledger-wide
327
+ // running executions, immune to the startup window (`runs` registers
328
+ // only after agent creation, seconds later).
329
+ const running = ledger.tasks.reduce((n, t) => n + t.executions.filter(e => e.outcome === 'running').length, 0)
330
+ if (running >= max) {
331
+ gate = `execution concurrency limit reached (${running}/${max} running)`
332
+ return undefined
333
+ }
299
334
  target.executions.push({
300
335
  id: executionId,
301
336
  trigger,
@@ -305,7 +340,7 @@ export class ExecutionService {
305
340
  })
306
341
  target.status = 'in_progress'
307
342
  target.updatedAt = this.deps.now()
308
- target.updatedBy = { kind: 'user' }
343
+ target.updatedBy = { kind: 'system' }
309
344
  target.claimedBy = sessionId
310
345
  target.claimedAt = this.deps.now()
311
346
  return [target]
@@ -354,6 +389,10 @@ export class ExecutionService {
354
389
  const message = error instanceof Error ? error.message : String(error)
355
390
  await this.patchExecution(executionId, { outcome: 'failed', error: `preset 组合失败:${message.slice(0, 400)}`, endedAt: this.deps.now() })
356
391
  await this.revertProgress(taskId)
392
+ // S1: a run that never started must not leave its worktree behind.
393
+ if (prepared !== undefined && this.deps.git !== undefined) {
394
+ try { await this.deps.git.removeWorktree(workspace.path, prepared.worktreePath) } catch { /* best effort (dirty worktrees are kept) */ }
395
+ }
357
396
  return { ok: false, error: `preset composition failed: ${message}` }
358
397
  }
359
398
  let handle: Awaited<ReturnType<AgentsFace['create']>>
@@ -372,9 +411,31 @@ export class ExecutionService {
372
411
  const message = error instanceof Error ? error.message : String(error)
373
412
  await this.patchExecution(executionId, { outcome: 'failed', error: message.slice(0, 500), endedAt: this.deps.now() })
374
413
  await this.revertProgress(taskId)
414
+ // S1: a run that never started must not leave its worktree behind.
415
+ if (prepared !== undefined && this.deps.git !== undefined) {
416
+ try { await this.deps.git.removeWorktree(workspace.path, prepared.worktreePath) } catch { /* best effort (dirty worktrees are kept) */ }
417
+ }
375
418
  return { ok: false, error: message }
376
419
  }
377
420
 
421
+ // R3: the startup path above awaited seconds of git + agent work. A
422
+ // cancel() that landed inside that window already settled the execution
423
+ // (cancelled + task back to todo) — with nothing registered in `runs`,
424
+ // it could not dispose the agent this path was about to create. Re-verify
425
+ // INSIDE the queue (after any enqueued cancel committed) BEFORE injecting:
426
+ // a cancelled card must not gain a zombie session that burns tokens and
427
+ // edits files while the task sits in todo, re-runnable by anyone.
428
+ const stillRunning = await this.deps.store.read(ledger =>
429
+ ledger.tasks.some(t => t.executions.some(e => e.id === executionId && e.outcome === 'running')))
430
+ if (!stillRunning) {
431
+ await handle.dispose().catch(() => { /* best effort */ })
432
+ // S1: do not leave the startup artifacts behind a cancelled run either.
433
+ if (prepared !== undefined && this.deps.git !== undefined) {
434
+ try { await this.deps.git.removeWorktree(workspace.path, prepared.worktreePath) } catch { /* best effort */ }
435
+ }
436
+ return { ok: false, error: 'cancelled during startup' }
437
+ }
438
+
378
439
  // 3. Attach the session to the workspace (GUI project session list).
379
440
  await this.deps.workspaces.attach(task.workspaceId, sessionId).catch(() => { /* cosmetic */ })
380
441
 
@@ -418,9 +479,16 @@ export class ExecutionService {
418
479
  void this.settleExecution(executionId, sessionId, prepared)
419
480
  }
420
481
  this.runs.set(executionId, { sessionId, ...(prepared !== undefined ? { prepared } : {}), settle, dispose: () => handle.dispose() })
482
+ // R2: the rejection path owns its state transition EXCLUSIVELY — the old
483
+ // code also called settle() here, racing two evidence collections whose
484
+ // mutations both checked outcome === 'running': whoever committed first
485
+ // won, so a run that never reached quiescence could be recorded as
486
+ // succeeded (and auto-moved to in_review). Now only the failure
487
+ // settlement writes, and the run entry is released after it commits.
421
488
  void handle.agent.whenIdle().then(settle, () => {
422
489
  this.noteFailure(sessionId, 'agent did not reach quiescence')
423
- settle()
490
+ .then(() => { this.runs.delete(executionId) })
491
+ .catch(() => { this.runs.delete(executionId) })
424
492
  })
425
493
 
426
494
  return { ok: true, executionId, sessionId }
@@ -461,7 +529,7 @@ export class ExecutionService {
461
529
  })
462
530
  t.status = 'in_review'
463
531
  t.updatedAt = now
464
- t.updatedBy = { kind: 'user' }
532
+ t.updatedBy = { kind: 'system' }
465
533
  }
466
534
  return [t]
467
535
  }
@@ -554,11 +622,13 @@ export class ExecutionService {
554
622
  // The cancelled session may already have committed work — keep the
555
623
  // evidence (best effort) so the user can inspect or 续跑 (0.3.1).
556
624
  const facts = await this.collectEvidence(entry?.prepared)
625
+ let settled = false
557
626
  await this.deps.store.mutate('execution-recorded', (ledger) => {
558
627
  const target = ledger.tasks.find(t => t.id === taskId)
559
628
  if (target === undefined) return undefined
560
629
  const execution = target.executions.find(e => e.id === running.id)
561
630
  if (execution === undefined || execution.outcome !== 'running') return undefined
631
+ settled = true
562
632
  execution.outcome = 'cancelled'
563
633
  execution.endedAt = this.deps.now()
564
634
  this.applyFacts(execution, facts)
@@ -570,6 +640,10 @@ export class ExecutionService {
570
640
  }
571
641
  return [target]
572
642
  })
643
+ // The execution may have settled (succeeded/failed) between the stale
644
+ // read above and this mutation — a no-op cancel must NOT report success
645
+ // (the GUI used to show 取消成功 for an already-succeeded run, review P1).
646
+ if (!settled) return { ok: false, error: 'execution already settled' }
573
647
  return { ok: true, executionId: running.id }
574
648
  }
575
649
 
@@ -656,7 +730,7 @@ export class ExecutionService {
656
730
  const lastExec = [...task.executions].reverse().find(e => e.outcome !== 'running')
657
731
  const lastExecText = lastExec === undefined
658
732
  ? '(无)'
659
- : `${lastExec.trigger} · ${lastExec.outcome}${lastExec.error !== undefined ? ` · ${lastExec.error.slice(0, 200)}` : ''} · ${new Date(lastExec.startedAt ?? 0).toISOString()}`
733
+ : `${lastExec.trigger} · ${lastExec.outcome}${lastExec.error !== undefined ? ` · ${lastExec.error.slice(0, 200)}` : ''} · ${lastExec.startedAt !== undefined ? new Date(lastExec.startedAt).toISOString() : '?'}`
660
734
  const lastCommentsText = task.comments.slice(-3)
661
735
  .map(c => `[${c.threadId !== undefined ? 'agent' : 'user'}] ${c.body}`)
662
736
  .join('\n') || '(无)'