@kabmo-labs/kabmo 0.25.0 → 0.26.0

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 (188) hide show
  1. package/dist/client/index.html +1 -1
  2. package/dist/client/index.js +440 -436
  3. package/dist/client/static/js/{index-ga8cbpt4.js → index-95b3n20k.js} +945 -945
  4. package/dist/client/static/js/index-p2yw9ywf.css +1 -0
  5. package/package.json +1 -1
  6. package/src/client/components/common/session-actions-menu/index.tsx +38 -12
  7. package/src/client/components/common/session-branch/BranchGuides.tsx +93 -0
  8. package/src/client/components/common/session-branch/SubagentRow.tsx +77 -0
  9. package/src/client/components/common/session-branch/index.tsx +169 -0
  10. package/src/client/components/common/session-branch/session-branch.test.tsx +240 -0
  11. package/src/client/components/common/session-branch/style.ts +29 -0
  12. package/src/client/components/common/session-row/StatusSlot.tsx +15 -3
  13. package/src/client/components/common/session-row/index.tsx +24 -25
  14. package/src/client/components/common/session-row/session-row.test.tsx +11 -0
  15. package/src/client/components/common/session-state-provider/index.tsx +1 -1
  16. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +64 -68
  17. package/src/client/components/layout/session-sidebar/SessionItem.batch.test.ts +3 -3
  18. package/src/client/components/layout/session-sidebar/SessionItem.test.ts +25 -2
  19. package/src/client/components/layout/session-sidebar/SessionItem.tsx +33 -30
  20. package/src/client/components/layout/session-sidebar/about-modal/HarnessRow.tsx +261 -0
  21. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +35 -225
  22. package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +36 -16
  23. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +11 -3
  24. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +13 -4
  25. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +28 -13
  26. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +59 -57
  27. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +57 -61
  28. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.test.ts +5 -0
  29. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +33 -30
  30. package/src/client/components/settings/Sidebar.tsx +1 -1
  31. package/src/client/components/settings/categories/NotificationSettings.test.tsx +92 -0
  32. package/src/client/components/settings/categories/NotificationSettings.tsx +112 -2
  33. package/src/client/components/settings/categories/harness-settings/HarnessCard.tsx +1 -1
  34. package/src/client/components/workspace/chat-timeline/AiMessageFooter.fork.test.ts +94 -0
  35. package/src/client/components/workspace/chat-timeline/AiMessageFooter.tsx +31 -5
  36. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +28 -39
  37. package/src/client/components/workspace/chat-timeline/MessageList.tsx +7 -5
  38. package/src/client/components/workspace/chat-timeline/PendingChatView.tsx +10 -0
  39. package/src/client/components/workspace/chat-timeline/RunFooter.tsx +12 -3
  40. package/src/client/components/workspace/chat-timeline/chat-input/AttachmentToolbar.tsx +2 -0
  41. package/src/client/components/workspace/chat-timeline/chat-input/ModeToggles.tsx +15 -1
  42. package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.test.tsx +85 -0
  43. package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.tsx +84 -0
  44. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +2 -0
  45. package/src/client/components/workspace/chat-timeline/chat-input/modes-props.ts +5 -0
  46. package/src/client/components/workspace/chat-timeline/index.tsx +10 -21
  47. package/src/client/components/workspace/chat-timeline/target-picker/DraftTargetRow.tsx +115 -0
  48. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +173 -0
  49. package/src/client/components/workspace/chat-timeline/target-picker/parent-session-options.test.ts +49 -0
  50. package/src/client/hooks/chat/composer/items.test.ts +19 -0
  51. package/src/client/hooks/chat/omp/folder-auto-expand.ts +1 -1
  52. package/src/client/hooks/chat/omp/pending-adoption.ts +51 -0
  53. package/src/client/hooks/chat/omp/pending-child.test.ts +73 -0
  54. package/src/client/hooks/chat/omp/pending-entry.ts +59 -0
  55. package/src/client/hooks/chat/omp/pending-rows.ts +6 -2
  56. package/src/client/hooks/chat/omp/pending-sessions.test.ts +1 -2
  57. package/src/client/hooks/chat/omp/pending-sessions.ts +45 -69
  58. package/src/client/hooks/chat/omp/pending-waiting.ts +1 -1
  59. package/src/client/hooks/chat/omp/run-switch.ts +1 -1
  60. package/src/client/hooks/chat/omp/session-list.tsx +4 -0
  61. package/src/client/hooks/chat/subagents/roster.ts +122 -0
  62. package/src/client/hooks/chat/timeline/child-spawn.test.ts +55 -0
  63. package/src/client/hooks/chat/timeline/child-spawn.ts +45 -0
  64. package/src/client/hooks/chat/timeline/fork-request.test.ts +54 -0
  65. package/src/client/hooks/chat/timeline/fork-request.ts +43 -0
  66. package/src/client/hooks/chat/timeline/mode-availability.test.ts +1 -1
  67. package/src/client/hooks/chat/timeline/mode-availability.ts +33 -0
  68. package/src/client/hooks/chat/timeline/mode-markers.ts +7 -1
  69. package/src/client/hooks/chat/timeline/mode-post.ts +15 -3
  70. package/src/client/hooks/chat/timeline/modes-contract.ts +9 -4
  71. package/src/client/hooks/chat/timeline/modes.test.ts +56 -18
  72. package/src/client/hooks/chat/timeline/modes.ts +42 -29
  73. package/src/client/hooks/chat/timeline/pending-branch.ts +205 -0
  74. package/src/client/hooks/chat/timeline/pending-dispatch.ts +253 -0
  75. package/src/client/hooks/chat/timeline/send.ts +17 -103
  76. package/src/client/hooks/chat/timeline/session-harness.ts +6 -0
  77. package/src/client/hooks/chat/timeline/stop-with-queue.ts +42 -0
  78. package/src/client/hooks/chat/timeline/use-fork-session.ts +40 -31
  79. package/src/client/hooks/models/use-harness-update.test.ts +137 -44
  80. package/src/client/hooks/models/use-harness-update.ts +133 -85
  81. package/src/client/hooks/ui/input-required-alert.ts +12 -5
  82. package/src/client/hooks/ui/notifications.ts +45 -0
  83. package/src/client/hooks/ui/run-outcome-alert.ts +48 -0
  84. package/src/client/hooks/workspace/expanded-sessions.ts +36 -7
  85. package/src/client/lib/native/open-session.ts +33 -0
  86. package/src/client/lib/notifications/os.test.ts +125 -0
  87. package/src/client/lib/notifications/os.ts +130 -0
  88. package/src/client/lib/plugins/sidebar-bridge.ts +2 -20
  89. package/src/client/lib/router/open-session.test.ts +75 -0
  90. package/src/client/lib/router/open-session.ts +34 -0
  91. package/src/client/lib/signals.ts +5 -1
  92. package/src/client/main.tsx +6 -0
  93. package/src/server/lib/db/schema.ts +1 -0
  94. package/src/server/lib/harness/acp/calls.ts +15 -1
  95. package/src/server/lib/harness/acp/client-capabilities.test.ts +55 -0
  96. package/src/server/lib/harness/acp/client-capabilities.ts +61 -0
  97. package/src/server/lib/harness/acp/client-fs.test.ts +135 -0
  98. package/src/server/lib/harness/acp/client-fs.ts +131 -0
  99. package/src/server/lib/harness/acp/client-initialize.test.ts +70 -0
  100. package/src/server/lib/harness/acp/client-initialize.ts +84 -0
  101. package/src/server/lib/harness/acp/client.ts +31 -21
  102. package/src/server/lib/harness/acp/commands.test.ts +5 -5
  103. package/src/server/lib/harness/acp/commands.ts +14 -2
  104. package/src/server/lib/harness/acp/frames-timing.test.ts +68 -0
  105. package/src/server/lib/harness/acp/frames-v2.test.ts +85 -0
  106. package/src/server/lib/harness/acp/frames.ts +71 -158
  107. package/src/server/lib/harness/acp/mapper.test.ts +40 -0
  108. package/src/server/lib/harness/acp/mapper.ts +12 -0
  109. package/src/server/lib/harness/acp/permissions.ts +6 -0
  110. package/src/server/lib/harness/acp/protocol.ts +41 -2
  111. package/src/server/lib/harness/acp/session-facts.ts +1 -1
  112. package/src/server/lib/harness/acp/session-handshake.test.ts +90 -1
  113. package/src/server/lib/harness/acp/session-handshake.ts +83 -6
  114. package/src/server/lib/harness/acp/session-idle.ts +7 -6
  115. package/src/server/lib/harness/acp/session-notifications.ts +41 -2
  116. package/src/server/lib/harness/acp/session-observers.test.ts +63 -0
  117. package/src/server/lib/harness/acp/session-permissions.test.ts +29 -0
  118. package/src/server/lib/harness/acp/session-permissions.ts +36 -0
  119. package/src/server/lib/harness/acp/session-requests.test.ts +92 -0
  120. package/src/server/lib/harness/acp/session-requests.ts +48 -6
  121. package/src/server/lib/harness/acp/session-turns.ts +8 -3
  122. package/src/server/lib/harness/acp/session-usage.ts +4 -0
  123. package/src/server/lib/harness/acp/session-web-state.ts +30 -2
  124. package/src/server/lib/harness/acp/session.test-util.ts +19 -1
  125. package/src/server/lib/harness/acp/session.ts +60 -60
  126. package/src/server/lib/harness/acp/tool-calls.ts +223 -0
  127. package/src/server/lib/harness/acp/turn.test.ts +102 -0
  128. package/src/server/lib/harness/acp/turn.ts +81 -6
  129. package/src/server/lib/harness/acp/v2.test.ts +144 -0
  130. package/src/server/lib/harness/acp/v2.ts +220 -0
  131. package/src/server/lib/harness/acp/verbs.ts +46 -6
  132. package/src/server/lib/harness/launch.test.ts +25 -0
  133. package/src/server/lib/harness/launch.ts +21 -9
  134. package/src/server/lib/harness/session.ts +3 -0
  135. package/src/server/lib/harness/update-job-store.test.ts +37 -12
  136. package/src/server/lib/harness/update-job-store.ts +22 -16
  137. package/src/server/lib/harness/update-job.server.ts +66 -175
  138. package/src/server/lib/harness/update-queue.server.ts +292 -0
  139. package/src/server/lib/harness/update-queue.test.ts +265 -0
  140. package/src/server/lib/harness/update-spawn.server.ts +96 -0
  141. package/src/server/lib/harness/update-worker.ts +8 -3
  142. package/src/server/lib/lifecycle/after-listen.ts +9 -0
  143. package/src/server/lib/lifecycle/paths.ts +14 -9
  144. package/src/server/lib/omp/rpc/run-settle.server.ts +10 -1
  145. package/src/server/lib/omp/rpc/subagent-liveness.ts +5 -5
  146. package/src/server/lib/omp/rpc/terminal-settle.ts +5 -1
  147. package/src/server/lib/omp/session/delete-flow.server.ts +1 -0
  148. package/src/server/lib/omp/session/sidebar-data.server.ts +10 -12
  149. package/src/server/lib/sessions/fork.server.test.ts +68 -0
  150. package/src/server/lib/sessions/fork.server.ts +31 -6
  151. package/src/server/lib/sessions/spawn.server.test.ts +41 -0
  152. package/src/server/lib/sessions/spawn.server.ts +27 -2
  153. package/src/server/routes/agent/harness-command.ts +6 -5
  154. package/src/server/routes/chat/stream.ts +7 -2
  155. package/src/server/routes/harness/update-status.ts +18 -12
  156. package/src/server/routes/harness/update.test.ts +53 -16
  157. package/src/server/routes/harness/update.ts +13 -8
  158. package/src/server/routes/sessions/body-readers.ts +43 -0
  159. package/src/server/routes/sessions/fork.ts +19 -4
  160. package/src/server/routes/sessions/routes.test.ts +1 -2
  161. package/src/server/routes/sessions/session.ts +19 -0
  162. package/src/server/routes/sessions/spawn.ts +16 -13
  163. package/src/shared/lib/chat/omp/agent-events.ts +11 -0
  164. package/src/shared/lib/chat/omp/session-modes-signal.test.ts +41 -0
  165. package/src/shared/lib/harness/mode-signal.ts +20 -0
  166. package/src/shared/lib/harness/session-modes.ts +24 -0
  167. package/src/shared/lib/harness/update-job.ts +53 -6
  168. package/src/shared/lib/icons/tint-hues.generated.ts +34 -34
  169. package/src/shared/lib/notifications/outcomes.test.ts +96 -0
  170. package/src/shared/lib/notifications/outcomes.ts +64 -0
  171. package/src/shared/lib/notifications/settings.test.ts +49 -0
  172. package/src/shared/lib/notifications/settings.ts +34 -0
  173. package/src/shared/lib/omp/session/default-title.ts +5 -4
  174. package/src/shared/lib/omp/session/prewarm-misc.test.ts +3 -2
  175. package/src/shared/lib/omp/session/schema-migrations.ts +4 -0
  176. package/src/shared/lib/omp/session/stream-state.server.test.ts +19 -0
  177. package/src/shared/lib/omp/session/stream-state.server.ts +24 -4
  178. package/src/shared/lib/settings/defaults.ts +3 -0
  179. package/src/shared/types/harness-update.ts +24 -0
  180. package/src/shared/types/realtime.ts +2 -0
  181. package/src/shared/types/settings/state.ts +10 -0
  182. package/src/shared/types/workspace.ts +4 -0
  183. package/src/test-support/harness-table.test-util.ts +25 -9
  184. package/dist/client/static/js/index-bc9aa4mx.css +0 -1
  185. package/src/client/components/common/session-actions-menu/NewChildSessionDialog.tsx +0 -113
  186. package/src/client/components/common/subagent-list/index.tsx +0 -165
  187. package/src/client/components/layout/session-sidebar/ChildSessionList.tsx +0 -66
  188. package/src/client/hooks/workspace/child-expansion.ts +0 -33
@@ -6,9 +6,11 @@
6
6
  /**
7
7
  * The dialog's half of the background harness update.
8
8
  *
9
- * Three properties, and each one is a failure this design exists to prevent:
10
- * a `202` means the install is running in a WORKER (so the card must follow it),
11
- * a `409` must come back as the refusal it is (no second install), and a run
9
+ * Four properties, and each one is a failure this design exists to prevent:
10
+ * a `202` means the install is running in a WORKER (so the card must follow it);
11
+ * a second harness pressed while the first installs is ACCEPTED and QUEUED (it
12
+ * must be followed too, and its own settle reported); a `409` for the SAME
13
+ * harness must come back as the refusal it is (no second install); and a run
12
14
  * already in flight must be ADOPTED when the card opens — otherwise a reload
13
15
  * loses the spinner and the user presses the button again.
14
16
  *
@@ -29,8 +31,10 @@ const realFetch = Bun.fetch;
29
31
  const TEST_POLL_MS = 1;
30
32
  let container: HTMLElement;
31
33
  let calls: string[] = [];
32
- let postPayload: { status: number; body: unknown } = { status: 202, body: {} };
33
- let statusPayload: HarnessUpdateJob | null = null;
34
+ let postReplies: { status: number; body: unknown }[] = [];
35
+ let statusJobs: HarnessUpdateJob[] = [];
36
+ /** Called when the card re-reads the version rows (the probe-arrival signal). */
37
+ let onVersionsRead: (() => void) | null = null;
34
38
 
35
39
  function job(overrides: Partial<HarnessUpdateJob> = {}): HarnessUpdateJob {
36
40
  return {
@@ -38,6 +42,7 @@ function job(overrides: Partial<HarnessUpdateJob> = {}): HarnessUpdateJob {
38
42
  label: 'claude',
39
43
  startedAt: Date.now(),
40
44
  finishedAt: null,
45
+ queuedAt: null,
41
46
  pid: 1234,
42
47
  steps: [{ part: 'adapter', command: 'npm install -g x', argv: ['npm', 'install', '-g', 'x'], state: 'running', code: null }],
43
48
  output: '',
@@ -49,19 +54,30 @@ function job(overrides: Partial<HarnessUpdateJob> = {}): HarnessUpdateJob {
49
54
  };
50
55
  }
51
56
 
57
+ function finished(overrides: Partial<HarnessUpdateJob> = {}): HarnessUpdateJob {
58
+ return job({ pid: null, finishedAt: Date.now(), ok: true, applied: true, ...overrides });
59
+ }
60
+
52
61
  beforeEach(() => {
53
62
  installDomGlobals(new Window({ url: 'http://localhost' }));
54
63
  container = document.createElement('div');
55
64
  document.body.appendChild(container);
56
65
  calls = [];
57
- statusPayload = null;
58
- postPayload = { status: 202, body: {} };
66
+ statusJobs = [];
67
+ postReplies = [];
68
+ onVersionsRead = null;
59
69
  (globalThis as unknown as Record<string, unknown>).fetch = async (input: unknown, init?: { method?: string }) => {
60
70
  calls.push(`${init?.method ?? 'GET'} ${String(input)}`);
61
71
  if (init?.method === 'POST') {
62
- return new Response(JSON.stringify(postPayload.body), { status: postPayload.status });
72
+ const reply = postReplies.length > 1 ? postReplies.shift()! : postReplies[0];
73
+ return new Response(JSON.stringify(reply?.body ?? {}), { status: reply?.status ?? 202 });
74
+ }
75
+ if (String(input).includes('/api/harness/versions')) {
76
+ onVersionsRead?.();
77
+ onVersionsRead = null;
78
+ return new Response(JSON.stringify({ harnesses: [] }), { status: 200 });
63
79
  }
64
- return new Response(JSON.stringify({ job: statusPayload }), { status: 200 });
80
+ return new Response(JSON.stringify({ jobs: statusJobs }), { status: 200 });
65
81
  };
66
82
  });
67
83
 
@@ -74,15 +90,21 @@ afterEach(() => {
74
90
 
75
91
  interface Harness {
76
92
  current: () => ReturnType<typeof useHarnessUpdate>;
77
- /** Resolves with what the card was told when the run settled. */
78
- settled: Promise<{ message: string; ok: boolean }>;
93
+ /** The settled messages, in order. */
94
+ settlements: Array<{ message: string; ok: boolean }>;
95
+ /** Resolves when settlement number `index` has been reported. */
96
+ settlement: (index: number) => Promise<{ message: string; ok: boolean }>;
79
97
  }
80
98
 
81
99
  function mount(): Harness {
82
- const { promise, resolve } = Promise.withResolvers<{ message: string; ok: boolean }>();
100
+ const settlements: Array<{ message: string; ok: boolean }> = [];
101
+ const waiters: Array<() => void> = [];
83
102
  const box = { latest: null as ReturnType<typeof useHarnessUpdate> | null };
84
103
  function Consumer() {
85
- box.latest = useHarnessUpdate((message, ok) => resolve({ message, ok }), { pollMs: TEST_POLL_MS });
104
+ box.latest = useHarnessUpdate((message, ok) => {
105
+ settlements.push({ message, ok });
106
+ waiters.shift()?.();
107
+ }, { pollMs: TEST_POLL_MS });
86
108
  return null;
87
109
  }
88
110
  const host = document.createElement('div');
@@ -93,7 +115,15 @@ function mount(): Harness {
93
115
  if (!box.latest) throw new Error('consumer did not render');
94
116
  return box.latest;
95
117
  },
96
- settled: promise,
118
+ settlements,
119
+ settlement: async (index) => {
120
+ while (settlements.length <= index) {
121
+ const { promise, resolve } = Promise.withResolvers<void>();
122
+ waiters.push(resolve);
123
+ await promise;
124
+ }
125
+ return settlements[index];
126
+ },
97
127
  };
98
128
  }
99
129
 
@@ -105,38 +135,76 @@ async function flush(): Promise<void> {
105
135
  });
106
136
  }
107
137
 
138
+ const GETS = (): string[] => calls.filter((call) => call.startsWith('GET /api/harness/update/status'));
139
+
108
140
  describe('useHarnessUpdate', () => {
109
141
  test('a 202 starts a run and the finished record settles it', async () => {
110
142
  const harness = mount();
111
143
  await flush();
112
- expect(harness.current().updating).toBeNull();
144
+ expect(harness.current().jobs).toEqual([]);
113
145
 
114
- postPayload = { status: 202, body: { started: true, job: job() } };
146
+ postReplies = [{ status: 202, body: { started: true, queued: false, job: job() } }];
115
147
  // The worker is already done by the time the first status read lands: this
116
148
  // is the spinners-then-message path, with no real delay anywhere.
117
- statusPayload = job({ finishedAt: Date.now(), pid: null, ok: true, applied: true, message: 'claude updated (adapter)' });
149
+ statusJobs = [finished({ message: 'claude updated (adapter)' })];
118
150
 
119
151
  const started: { problem: string | null } = { problem: 'unset' };
120
152
  await act(async () => {
121
153
  started.problem = await harness.current().start('claude');
122
154
  });
123
155
  expect(started.problem).toBeNull();
124
- // The spinner's own state is pinned by the adoption case below; here the
125
- // record was already finished, so the run settles inside the same flush.
126
- expect(await harness.settled).toEqual({ message: 'claude updated (adapter)', ok: true });
127
- expect(harness.current().updating).toBeNull();
156
+ expect(await harness.settlement(0)).toEqual({ message: 'claude updated (adapter)', ok: true });
128
157
  // The record outlives the spinner: the log panel reads it AFTER the run, and
129
- // a cleared `job` would close the reader's only view of what was installed.
130
- expect(harness.current().job?.message).toBe('claude updated (adapter)');
158
+ // a cleared list would close the reader's only view of what was installed.
159
+ expect(harness.current().jobs[0]?.message).toBe('claude updated (adapter)');
160
+ });
161
+
162
+ test('a second harness is ACCEPTED as queued, and both settles are reported', async () => {
163
+ // The bug this fixes: pressing the second row while the first installed got
164
+ // a 409 and nothing else. Both presses now return 202 — the second with a
165
+ // `queued` record — and the queue starts it after the first settles.
166
+ const harness = mount();
167
+ await flush();
168
+
169
+ postReplies = [
170
+ { status: 202, body: { started: true, queued: false, job: job() } },
171
+ { status: 202, body: { started: true, queued: true, job: job({ harness: 'codex', label: 'codex', pid: null, queuedAt: Date.now() }) } },
172
+ ];
173
+ statusJobs = [job(), job({ harness: 'codex', label: 'codex', pid: null, queuedAt: Date.now() })];
174
+
175
+ let first: string | null = 'unset';
176
+ let second: string | null = 'unset';
177
+ await act(async () => {
178
+ first = await harness.current().start('claude');
179
+ second = await harness.current().start('codex');
180
+ });
181
+ expect(first).toBeNull();
182
+ expect(second).toBeNull();
183
+ expect(harness.current().jobs.map((entry) => entry.harness).sort()).toEqual(['claude', 'codex']);
184
+
185
+ // Claude finishes, the queue starts codex, codex finishes.
186
+ statusJobs = [
187
+ finished({ message: 'claude updated (adapter)' }),
188
+ job({ harness: 'codex', label: 'codex' }),
189
+ ];
190
+ expect(await harness.settlement(0)).toEqual({ message: 'claude updated (adapter)', ok: true });
191
+
192
+ statusJobs = [
193
+ finished({ message: 'claude updated (adapter)' }),
194
+ finished({ harness: 'codex', label: 'codex', message: 'codex updated (codex-acp)' }),
195
+ ];
196
+ expect(await harness.settlement(1)).toEqual({ message: 'codex updated (codex-acp)', ok: true });
197
+ // One settle each, not one per poll.
198
+ expect(harness.settlements).toHaveLength(2);
131
199
  });
132
200
 
133
201
  test('a 409 comes back as the refusal, and its record is still readable', async () => {
134
- // The refusal carries the RUNNING record (`job`), so the press that lost the
135
- // race can still open the log of the install it collided with.
136
- postPayload = {
202
+ // The refusal carries the unfinished record (`job`), so the press that lost
203
+ // the race can still open the log of the install it collided with.
204
+ postReplies = [{
137
205
  status: 409,
138
206
  body: { error: 'claude update is already running — it continues in the background', job: job({ pid: 999 }) },
139
- };
207
+ }];
140
208
  const harness = mount();
141
209
  await flush();
142
210
 
@@ -145,36 +213,63 @@ describe('useHarnessUpdate', () => {
145
213
  refusal.problem = await harness.current().start('claude');
146
214
  });
147
215
  expect(refusal.problem).toContain('already running');
148
- expect(harness.current().updating).toBeNull();
149
- expect(harness.current().job?.pid).toBe(999);
216
+ expect(harness.current().jobs[0]?.pid).toBe(999);
217
+ expect(harness.settlements).toEqual([]);
150
218
  });
151
219
 
152
220
  test('an in-flight run is adopted when the card opens', async () => {
153
- statusPayload = job();
221
+ statusJobs = [job()];
154
222
  const harness = mount();
155
223
  await flush();
156
224
  // The spinner belongs to a run this page did not start — a second tab, or
157
225
  // this one after a reload.
158
- expect(harness.current().updating).toBe('claude');
226
+ expect(harness.current().jobs[0]?.harness).toBe('claude');
227
+
228
+ statusJobs = [finished({ ok: false, message: 'claude update failed (exit 1)' })];
229
+ expect(await harness.settlement(0)).toEqual({ message: 'claude update failed (exit 1)', ok: false });
230
+ });
231
+
232
+ test('a settled run keeps polling until its PROBE answers, then re-reads the rows', async () => {
233
+ // The row quotes the version the install just replaced until the server has
234
+ // asked the new binary what it is. The poll is what carries that answer to
235
+ // the row; without it, "updated" and a stale number sit side by side and the
236
+ // only way out is pressing Check status.
237
+ statusJobs = [finished({ message: 'claude updated (adapter)', probe: 'pending' })];
238
+ const versionsRead = Promise.withResolvers<void>();
239
+ onVersionsRead = versionsRead.resolve;
240
+ const harness = mount();
241
+ await flush();
242
+ expect(harness.current().jobs[0]?.probe).toBe('pending');
243
+ expect(harness.settlements).toEqual([]);
159
244
 
160
- statusPayload = job({ finishedAt: Date.now(), pid: null, ok: false, applied: true, message: 'claude update failed (exit 1)' });
161
- expect(await harness.settled).toEqual({ message: 'claude update failed (exit 1)', ok: false });
245
+ // The probe answers on a later poll; the row's own read is the proof that it
246
+ // arrived, and it is awaited rather than slept for.
247
+ statusJobs = [finished({ message: 'claude updated (adapter) — it now reports 0.103.0', probe: 'done' })];
248
+ await versionsRead.promise;
249
+ expect(calls).toContain('GET /api/harness/versions');
250
+ expect(harness.current().jobs[0]?.probe).toBe('done');
251
+ // The probe arriving is not a second toast: one run, one report.
252
+ expect(harness.settlements).toEqual([]);
162
253
  });
163
254
 
164
- test('a finished record on open is history, not a run to follow', async () => {
165
- statusPayload = job({ finishedAt: Date.now(), pid: null, ok: true, applied: true, message: 'claude updated (adapter)' });
255
+ test('a finished record on open is history, not a run to toast about', async () => {
256
+ statusJobs = [finished({ message: 'claude updated (adapter)' })];
166
257
  const harness = mount();
167
258
  await flush();
168
- expect(harness.current().updating).toBeNull();
259
+ // The loop stopped after the ONE read: no unfinished record means nothing to
260
+ // follow, so a finished record can never be reported as a fresh settle.
261
+ expect(harness.settlements).toEqual([]);
169
262
  // No spinner, but the row can still offer the log of what ran.
170
- expect(harness.current().job?.harness).toBe('claude');
263
+ expect(harness.current().jobs[0]?.harness).toBe('claude');
264
+ // One read and no follow-up: nothing is unfinished.
265
+ expect(GETS()).toHaveLength(1);
171
266
  });
172
267
 
173
268
  test('"already up to date" is reported without following a job', async () => {
174
- postPayload = {
269
+ postReplies = [{
175
270
  status: 200,
176
271
  body: { success: true, id: 'claude', message: 'claude is already up to date (adapter 0.102.0)' },
177
- };
272
+ }];
178
273
  const harness = mount();
179
274
  await flush();
180
275
 
@@ -183,11 +278,9 @@ describe('useHarnessUpdate', () => {
183
278
  current.problem = await harness.current().start('claude');
184
279
  });
185
280
  expect(current.problem).toBeNull();
186
- expect(harness.current().updating).toBeNull();
187
- // Nothing ran, so there is no record for a row to show a log of.
188
- expect(harness.current().job).toBeNull();
189
- expect(await harness.settled).toEqual({ message: 'claude is already up to date (adapter 0.102.0)', ok: true });
281
+ expect(harness.current().jobs).toEqual([]);
282
+ expect(await harness.settlement(0)).toEqual({ message: 'claude is already up to date (adapter 0.102.0)', ok: true });
190
283
  // One read — the mount's — and no poll for a job that does not exist.
191
- expect(calls.filter((call) => call.startsWith('GET /api/harness/update/status'))).toHaveLength(1);
284
+ expect(GETS()).toHaveLength(1);
192
285
  });
193
286
  });
@@ -4,23 +4,36 @@
4
4
  */
5
5
 
6
6
  /**
7
- * Starting a harness update, and following the background job it becomes.
7
+ * Starting harness updates, and following every background run they became.
8
8
  *
9
9
  * The POST used to hold the request open for the whole install; now it answers
10
- * `202` and the run continues in a detached worker. So the dialog's job has two
10
+ * `202` and each run continues in a detached worker. So the dialog's job has two
11
11
  * halves: start it, then poll `GET /api/harness/update/status` until the record
12
- * says it finished — and, on open, ADOPT a run that is already in flight. That
12
+ * says it finished — and, on open, ADOPT whatever is already in flight. That
13
13
  * second half is what makes the update really background: closing About, or
14
14
  * reloading the page, does not lose the button's state, and a reopened card
15
15
  * shows the spinner for the run that is still installing.
16
16
  *
17
- * The rows are re-read once the run settles (`invalidateHarnessVersions`): the
17
+ * **ONE loop for EVERY run, because two presses are two records.** A second
18
+ * harness is accepted while the first installs: its record carries `queuedAt`
19
+ * and the server's queue starts it when the run ahead settles, so the card has
20
+ * to follow a run it did not start AND one that has not started yet. A finished
21
+ * run is reported once per harness, and a record that was already finished when
22
+ * the card opened is HISTORY — a reopened dialog must not re-toast last week's
23
+ * install.
24
+ *
25
+ * The rows are re-read once a run settles (`invalidateHarnessVersions`): the
18
26
  * update forgot the cached probe, so the next read is the first honest one.
19
27
  */
20
28
 
21
29
  import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
22
30
  import { invalidateHarnessVersions } from '@/client/hooks/models/use-harness-versions';
23
- import { isHarnessUpdateJob, updateJobFrom } from '@/shared/lib/harness/update-job';
31
+ import {
32
+ isHarnessUpdateJob,
33
+ jobMoving,
34
+ jobUnfinished,
35
+ updateJobsFrom,
36
+ } from '@/shared/lib/harness/update-job';
24
37
  import type { HarnessUpdateJob } from '@/shared/types/harness-update';
25
38
 
26
39
  /** One poll per second: the worker flushes at 400ms, and this is a dialog. */
@@ -29,9 +42,10 @@ export const HARNESS_UPDATE_POLL_MS = 1_000;
29
42
  /**
30
43
  * Stop polling after this long and hand the control back.
31
44
  *
32
- * The install itself is bounded per step by the worker; this only stops an
33
- * open dialog from asking forever if the record stopped moving. The run is not
34
- * cancelled — closing the dialog never cancels anything.
45
+ * The install itself is bounded per step by the worker; this only stops an open
46
+ * dialog from asking forever if the server stopped answering. The run is not
47
+ * cancelled — closing the dialog never cancels anything, and a queued run is
48
+ * started by the server's own ticker.
35
49
  */
36
50
  const MAX_FOLLOW_MS = 45 * 60_000;
37
51
 
@@ -47,105 +61,137 @@ export interface HarnessUpdateOptions {
47
61
  }
48
62
 
49
63
  export interface HarnessUpdateController {
50
- /** The harness whose run this dialog is following, or null. */
51
- updating: string | null;
52
- /** The step the worker is on (`adapter`, `Claude Code`, …), when it says. */
53
- progress: string | null;
54
64
  /**
55
- * The record itself — the last one read, running or finished.
65
+ * The last known record per harness — waiting, installing, or the last run
66
+ * that finished.
56
67
  *
57
- * The log dialog renders from this: the worker streams every command's output
58
- * into it, so the panel is the install's own console. It is NOT cleared when a
59
- * run settles, because the interesting moment to read the log is often after
60
- * it failed.
68
+ * The rows render from this: `jobQueued`/`jobRunning`/`runningStepOf` answer
69
+ * what to draw, and the log dialog renders the record's own steps and output.
70
+ * A settled run is NOT cleared, because the interesting moment to read the log
71
+ * is often after it failed.
61
72
  */
62
- job: HarnessUpdateJob | null;
73
+ jobs: HarnessUpdateJob[];
63
74
  /** Start an update; resolves to a refusal, or null when the run was started
64
- * (or there was nothing to move). */
75
+ * (immediately or queued behind the one already installing). */
65
76
  start: (id: string) => Promise<string | null>;
66
77
  }
67
78
 
68
79
  type Settled = (message: string, ok: boolean) => void;
69
80
 
70
- function runningStep(job: HarnessUpdateJob | null): string | null {
71
- const steps = job && Array.isArray(job.steps) ? job.steps : [];
72
- return steps.find((step) => step.state === 'running')?.part ?? null;
73
- }
74
-
75
81
  export function useHarnessUpdate(
76
82
  onSettled: Settled,
77
83
  { pollMs = HARNESS_UPDATE_POLL_MS, enabled = true }: HarnessUpdateOptions = {},
78
84
  ): HarnessUpdateController {
79
- const [updating, setUpdating] = useState<string | null>(null);
80
- const [progress, setProgress] = useState<string | null>(null);
81
- const [job, setJob] = useState<HarnessUpdateJob | null>(null);
85
+ const [jobs, setJobs] = useState<HarnessUpdateJob[]>([]);
82
86
  const alive = useRef(true);
87
+ const polling = useRef(false);
88
+ // Which harnesses this card WATCHED install. A run it only saw as history is
89
+ // not one to toast about, and the same harness must not be reported twice.
90
+ const watched = useRef(new Set<string>());
91
+ const reported = useRef(new Set<string>());
92
+ // Runs whose post-install probe answer has already been reflected in the rows.
93
+ const probed = useRef(new Set<string>());
83
94
  // The callback is read through a ref so an inline arrow from the caller does
84
- // not restart the mount effect (and re-adopt the job) on every render.
95
+ // not restart the mount effect (and re-adopt the records) on every render.
85
96
  const settled = useRef(onSettled);
86
97
  settled.current = onSettled;
87
98
 
88
- const follow = useCallback(async (id: string): Promise<void> => {
89
- const startedAt = Date.now();
90
- while (alive.current) {
91
- let job: HarnessUpdateJob | null = null;
92
- try {
93
- const response = await fetch('/api/harness/update/status', { cache: 'no-store' });
94
- if (!response.ok) throw new Error(String(response.status));
95
- job = updateJobFrom(await response.json());
96
- } catch {
97
- // A transient failure is not the update failing: the record lives on
98
- // disk and the next poll reads it.
99
- }
99
+ /** Fold one harness's record into the list, keeping the newer one. */
100
+ const merge = useCallback((incoming: HarnessUpdateJob | null) => {
101
+ if (!incoming) return;
102
+ setJobs((current) => {
103
+ const index = current.findIndex((job) => job.harness === incoming.harness);
104
+ if (index < 0) return [...current, incoming];
105
+ const next = current.slice();
106
+ next[index] = incoming;
107
+ return next;
108
+ });
109
+ }, []);
100
110
 
101
- if (!alive.current) return;
102
- // Kept even once it finishes: the log stays readable while the panel is
103
- // open, and a failure is the case a reader wants the output for.
104
- if (job) setJob(job);
105
- if (job?.finishedAt != null) {
106
- setUpdating(null);
107
- setProgress(null);
111
+ /**
112
+ * Report the runs that went from unfinished to finished, and re-read the rows
113
+ * once each run's post-install PROBE answers.
114
+ *
115
+ * The probe is what stops the row quoting the version the update replaced, and
116
+ * it lands AFTER the run settles — so the settle report and the version refresh
117
+ * are two separate moments, not one.
118
+ */
119
+ const observe = useCallback((incoming: HarnessUpdateJob[]) => {
120
+ setJobs(incoming);
121
+ for (const job of incoming) {
122
+ if (jobUnfinished(job)) {
123
+ watched.current.add(job.harness);
124
+ reported.current.delete(job.harness);
125
+ continue;
126
+ }
127
+ if (watched.current.has(job.harness) && !reported.current.has(job.harness)) {
128
+ reported.current.add(job.harness);
108
129
  invalidateHarnessVersions();
109
130
  settled.current(job.message ?? (job.ok ? 'Update finished' : 'Update failed'), Boolean(job.ok));
110
- return;
111
131
  }
112
- if (job?.harness === id) setProgress(runningStep(job));
132
+ // A run whose probe settled: the numbers the row reads are now the new
133
+ // ones, so the cached rows must be dropped. `startedAt` identifies the run,
134
+ // because the next update replaces the record under the same harness.
135
+ if (job.probe === 'done') {
136
+ const key = `${job.harness}:${job.startedAt}`;
137
+ if (!probed.current.has(key)) {
138
+ probed.current.add(key);
139
+ invalidateHarnessVersions();
140
+ }
141
+ }
142
+ }
143
+ }, []);
113
144
 
114
- if (Date.now() - startedAt > MAX_FOLLOW_MS) {
115
- setUpdating(null);
116
- setProgress(null);
117
- return;
145
+ /**
146
+ * Follow every unfinished record until they all settle.
147
+ *
148
+ * One loop, not one per harness: a queued run and an installing run are two
149
+ * rows of the same poll, and a loop per press would double the reads for no
150
+ * extra answer. `polling` keeps a second caller (the mount effect and a press
151
+ * landing together) from starting a second loop.
152
+ */
153
+ const follow = useCallback(async (): Promise<void> => {
154
+ if (polling.current) return;
155
+ polling.current = true;
156
+ const startedAt = Date.now();
157
+ try {
158
+ while (alive.current) {
159
+ let next: HarnessUpdateJob[] | null = null;
160
+ try {
161
+ const response = await fetch('/api/harness/update/status', { cache: 'no-store' });
162
+ if (!response.ok) throw new Error(String(response.status));
163
+ next = updateJobsFrom(await response.json());
164
+ } catch {
165
+ // A transient failure is not the update failing: the records live on
166
+ // disk and the next poll reads them.
167
+ }
168
+
169
+ if (!alive.current) return;
170
+ if (next) observe(next);
171
+ // Nothing left to watch — a queued run keeps this loop alive (the
172
+ // server starts it when the install ahead finishes), and so does a run
173
+ // whose post-install probe has not answered yet.
174
+ if (!next || !next.some(jobMoving)) return;
175
+ if (Date.now() - startedAt > MAX_FOLLOW_MS) return;
176
+ const { promise, resolve } = Promise.withResolvers<void>();
177
+ setTimeout(resolve, pollMs);
178
+ await promise;
118
179
  }
119
- const { promise, resolve } = Promise.withResolvers<void>();
120
- setTimeout(resolve, pollMs);
121
- await promise;
180
+ } finally {
181
+ polling.current = false;
122
182
  }
123
- }, [pollMs]);
183
+ }, [observe, pollMs]);
124
184
 
125
- // Adopt a run that is already going: another tab, or this dialog reopened.
185
+ // Adopt whatever is already going: another tab, a run accepted before a
186
+ // reload, or a queued one. A closed card has nothing to show and nothing to
187
+ // poll for; the workers and the queue keep going either way.
126
188
  useEffect(() => {
127
- // A closed card has nothing to show and nothing to poll for; the run keeps
128
- // going in its worker either way.
129
189
  if (!enabled) {
130
190
  alive.current = false;
131
191
  return;
132
192
  }
133
193
  alive.current = true;
134
- void (async () => {
135
- try {
136
- const response = await fetch('/api/harness/update/status', { cache: 'no-store' });
137
- if (!response.ok) return;
138
- const running = updateJobFrom(await response.json());
139
- if (!alive.current || !running) return;
140
- setJob(running);
141
- if (running.finishedAt !== null) return;
142
- setUpdating(running.harness);
143
- setProgress(runningStep(running));
144
- void follow(running.harness);
145
- } catch {
146
- // Nothing running, or no server to ask: the list still renders.
147
- }
148
- })();
194
+ void follow();
149
195
  return () => {
150
196
  alive.current = false;
151
197
  };
@@ -168,7 +214,8 @@ export function useHarnessUpdate(
168
214
  message?: string;
169
215
  success?: boolean;
170
216
  started?: boolean;
171
- job?: HarnessUpdateJob;
217
+ queued?: boolean;
218
+ job?: unknown;
172
219
  };
173
220
 
174
221
  // The POST's own record is trusted only if it IS a record; a payload that
@@ -176,16 +223,17 @@ export function useHarnessUpdate(
176
223
  const posted = isHarnessUpdateJob(payload.job) ? payload.job : null;
177
224
 
178
225
  if (response.status === 202 && payload.started) {
179
- setUpdating(id);
180
- setJob(posted);
181
- setProgress(runningStep(posted));
182
- void follow(id);
226
+ // `started` covers both halves: installing now, or waiting its turn.
227
+ watched.current.add(id);
228
+ reported.current.delete(id);
229
+ merge(posted);
230
+ void follow();
183
231
  return null;
184
232
  }
185
233
  if (!response.ok) {
186
- // A refusal carries the run that caused it: the reader can open ITS log
187
- // instead of only being told to wait.
188
- if (response.status === 409 && posted) setJob(posted);
234
+ // A refusal carries the record that caused it (`job`), so the reader can
235
+ // open ITS log instead of only being told to wait.
236
+ if (posted) merge(posted);
189
237
  return payload.error ?? `Update failed (HTTP ${response.status})`;
190
238
  }
191
239
  if (payload.success) {
@@ -196,7 +244,7 @@ export function useHarnessUpdate(
196
244
  return null;
197
245
  }
198
246
  return payload.error ?? 'Update failed';
199
- }, [follow]);
247
+ }, [follow, merge]);
200
248
 
201
- return { updating, progress, job, start };
249
+ return { jobs, start };
202
250
  }
@@ -15,8 +15,9 @@
15
15
  */
16
16
 
17
17
  import { useEffect, useRef } from 'preact/hooks';
18
- import type { WorkspaceFolderData } from '@/shared/types';
18
+ import type { SessionItemData, WorkspaceFolderData } from '@/shared/types';
19
19
  import { triggerInputRequiredSound } from '@/client/hooks/ui/notification-sound';
20
+ import { sendNotification } from '@/client/hooks/ui/notifications';
20
21
 
21
22
  export function useInputRequiredAlert(folders: WorkspaceFolderData[]): void {
22
23
  // Null until the first snapshot: a page load must not replay a cue for
@@ -24,19 +25,25 @@ export function useInputRequiredAlert(folders: WorkspaceFolderData[]): void {
24
25
  const knownRef = useRef<Set<string> | null>(null);
25
26
 
26
27
  useEffect(() => {
27
- const waiting = new Set<string>();
28
+ const waiting = new Map<string, SessionItemData>();
28
29
  for (const folder of folders) {
29
30
  for (const session of folder.sessions ?? []) {
30
- if (session.awaitingInput) waiting.add(String(session.id));
31
+ if (session.awaitingInput) waiting.set(String(session.id), session);
31
32
  }
32
33
  }
33
34
 
34
35
  const known = knownRef.current;
35
- knownRef.current = waiting;
36
+ knownRef.current = new Set(waiting.keys());
36
37
  if (!known) return;
37
- for (const id of waiting) {
38
+ for (const [id, session] of waiting) {
38
39
  if (known.has(id)) continue;
39
40
  triggerInputRequiredSound();
41
+ sendNotification('ask', {
42
+ title: session.title?.trim() || 'Kabmo',
43
+ body: 'Waiting for your answer.',
44
+ tag: `ask:${id}`,
45
+ sessionId: id,
46
+ });
40
47
  return;
41
48
  }
42
49
  }, [folders]);