@firstpick/pi-package-webui 0.11.1 → 0.11.3

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 (310) hide show
  1. package/DEVELOPMENT.md +151 -12
  2. package/README.md +34 -6
  3. package/TECHNICAL.md +75 -8
  4. package/bin/pi-webui.mjs +5390 -11423
  5. package/index.ts +10 -3
  6. package/lib/component-update-state.mjs +2 -2
  7. package/lib/git-workflow-preference-values.mjs +122 -0
  8. package/lib/git-workflow-preferences.mjs +35 -563
  9. package/lib/package-updates.mjs +260 -0
  10. package/lib/rpc-supervisor-client.mjs +8 -4
  11. package/lib/server/app-runners.mjs +1667 -0
  12. package/lib/server/codex-fast-mode.mjs +161 -0
  13. package/lib/server/conversation-mode.mjs +218 -0
  14. package/lib/server/conversation-voice.mjs +318 -0
  15. package/lib/server/extension-ui-requests.mjs +114 -0
  16. package/lib/server/git-generation.mjs +459 -0
  17. package/lib/server/git-mutations.mjs +1827 -0
  18. package/lib/server/git-reads.mjs +608 -0
  19. package/lib/server/git-worktree-sessions.mjs +408 -0
  20. package/lib/server/helper-requests.mjs +82 -0
  21. package/lib/server/no-project-workspaces.mjs +103 -0
  22. package/lib/server/rpc-transports.mjs +394 -0
  23. package/lib/server/settings-store.mjs +544 -0
  24. package/lib/server/sse-delivery.mjs +138 -0
  25. package/lib/server/workspace-files.mjs +563 -0
  26. package/lib/update/supervisor.mjs +4 -0
  27. package/lib/webui-workspaces.mjs +2 -0
  28. package/package.json +4 -4
  29. package/public/activity-agents-controller.mjs +269 -0
  30. package/public/activity-events-controller.mjs +236 -0
  31. package/public/activity-intercom-controller.mjs +627 -0
  32. package/public/activity-queue-controller.mjs +534 -0
  33. package/public/activity-todo-view.mjs +89 -0
  34. package/public/activity-usage-controller.mjs +589 -0
  35. package/public/app.js +3761 -3886
  36. package/public/conversation-overview.css +84 -0
  37. package/public/conversation-overview.mjs +193 -0
  38. package/public/draft-conversations.mjs +135 -0
  39. package/public/focused-composer-controls.css +111 -0
  40. package/public/focused-composer-controls.mjs +143 -0
  41. package/public/focused-composer-disclosure.mjs +223 -0
  42. package/public/focused-shell-adapter.css +264 -0
  43. package/public/focused-shell-adapter.mjs +257 -0
  44. package/public/index.html +137 -80
  45. package/public/model-catalog-order.mjs +63 -0
  46. package/public/platform-event-stream.mjs +64 -0
  47. package/public/project-artifact-controller.mjs +218 -0
  48. package/public/project-file-viewer-controller.mjs +383 -0
  49. package/public/project-git-tools-controller.mjs +181 -0
  50. package/public/project-runners-controller.mjs +220 -0
  51. package/public/service-worker.js +52 -1
  52. package/public/session-attention-scheduler.mjs +182 -0
  53. package/public/session-composer-store.mjs +56 -0
  54. package/public/session-context.mjs +51 -0
  55. package/public/session-extension-requests.mjs +63 -0
  56. package/public/session-shelf-state.mjs +151 -0
  57. package/public/session-shelves.css +171 -0
  58. package/public/session-shelves.mjs +95 -0
  59. package/public/settings-navigation-controller.mjs +249 -0
  60. package/public/settings-navigation.css +61 -0
  61. package/public/settings-resource-controller.mjs +232 -0
  62. package/public/settings-workspace.css +19 -0
  63. package/public/settings-workspace.mjs +175 -0
  64. package/public/shell-actions.mjs +87 -0
  65. package/public/shell-attention-inbox.css +72 -0
  66. package/public/shell-attention-inbox.mjs +198 -0
  67. package/public/shell-context-actions.css +61 -0
  68. package/public/shell-context-actions.mjs +168 -0
  69. package/public/shell-inspector-resize.mjs +124 -0
  70. package/public/shell-navigation-resize.mjs +107 -0
  71. package/public/shell-navigation.mjs +127 -0
  72. package/public/shell-preferences.mjs +108 -0
  73. package/public/shell-surfaces.mjs +99 -0
  74. package/public/shell-tools-launcher.css +97 -0
  75. package/public/shell-tools-launcher.mjs +222 -0
  76. package/public/shell-view-host.mjs +115 -0
  77. package/public/shell-workspace.css +266 -0
  78. package/public/shell-workspace.mjs +402 -0
  79. package/public/sidebar-update-menu.css +190 -0
  80. package/public/sidebar-update-menu.mjs +512 -0
  81. package/public/styles.css +322 -169
  82. package/public/subagent-launch-slot-state.mjs +2 -2
  83. package/public/themed-selects.css +156 -0
  84. package/public/workspace-choices.mjs +104 -0
  85. package/tests/activity-agents-controller.test.mjs +652 -0
  86. package/tests/activity-events-controller.test.mjs +511 -0
  87. package/tests/app-runner-terminal-static.test.mjs +3 -3
  88. package/tests/append-system-http.test.mjs +7 -6
  89. package/tests/append-system-selector-static.test.mjs +6 -6
  90. package/tests/aur-review-widget-static.test.mjs +11 -3
  91. package/tests/boot-failure-diagnostics.test.mjs +8 -3
  92. package/tests/browser/action-feedback-gutter.spec.mjs +251 -0
  93. package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
  94. package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
  95. package/tests/browser/activity-owner-integration.spec.mjs +201 -0
  96. package/tests/browser/activity-queue-controller.spec.mjs +405 -0
  97. package/tests/browser/activity-section-picker.spec.mjs +294 -0
  98. package/tests/browser/activity-todo-view.spec.mjs +60 -0
  99. package/tests/browser/activity-usage-controller.spec.mjs +416 -0
  100. package/tests/browser/compact-layout-integration.spec.mjs +58 -0
  101. package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
  102. package/tests/browser/composer-disclosure.spec.mjs +389 -0
  103. package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
  104. package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
  105. package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
  106. package/tests/browser/control-visibility.spec.mjs +5 -0
  107. package/tests/browser/controls-layout.spec.mjs +21 -2
  108. package/tests/browser/conversation-overview.spec.mjs +251 -0
  109. package/tests/browser/draft-conversations.spec.mjs +276 -0
  110. package/tests/browser/empty-startup-fixture.mjs +135 -0
  111. package/tests/browser/empty-startup.spec.mjs +282 -0
  112. package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
  113. package/tests/browser/focused-composer-more.spec.mjs +127 -0
  114. package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
  115. package/tests/browser/focused-navigation-fixture.mjs +32 -0
  116. package/tests/browser/focused-project-activity.spec.mjs +111 -0
  117. package/tests/browser/focused-shell-integration.spec.mjs +208 -0
  118. package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
  119. package/tests/browser/folder-picker-layout.spec.mjs +302 -0
  120. package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
  121. package/tests/browser/git-setup-cancel.spec.mjs +251 -0
  122. package/tests/browser/guided-git-fallback.spec.mjs +40 -21
  123. package/tests/browser/inspector-header.spec.mjs +221 -0
  124. package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
  125. package/tests/browser/inspector-resize.spec.mjs +227 -0
  126. package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
  127. package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
  128. package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
  129. package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
  130. package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
  131. package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
  132. package/tests/browser/interaction-continuity.spec.mjs +50 -26
  133. package/tests/browser/interaction-model-picker.spec.mjs +86 -0
  134. package/tests/browser/last-terminal-close.spec.mjs +280 -0
  135. package/tests/browser/mobile-foundation.spec.mjs +172 -20
  136. package/tests/browser/model-catalog-groups.spec.mjs +104 -0
  137. package/tests/browser/more-row-menus.spec.mjs +258 -0
  138. package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
  139. package/tests/browser/no-project-conversation.spec.mjs +113 -0
  140. package/tests/browser/optimistic-prompt.spec.mjs +3 -0
  141. package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
  142. package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
  143. package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
  144. package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
  145. package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
  146. package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
  147. package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
  148. package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
  149. package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
  150. package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
  151. package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
  152. package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
  153. package/tests/browser/project-artifact-controller.spec.mjs +324 -0
  154. package/tests/browser/project-artifact-integration.spec.mjs +103 -0
  155. package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
  156. package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
  157. package/tests/browser/session-fixture.mjs +118 -0
  158. package/tests/browser/session-ownership.spec.mjs +175 -0
  159. package/tests/browser/session-shelves-integration.spec.mjs +219 -0
  160. package/tests/browser/session-shelves.spec.mjs +340 -0
  161. package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
  162. package/tests/browser/session-summary.spec.mjs +17 -12
  163. package/tests/browser/settings-complete-home.spec.mjs +463 -0
  164. package/tests/browser/settings-entrypoint.spec.mjs +104 -0
  165. package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
  166. package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
  167. package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
  168. package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
  169. package/tests/browser/shell-ownership.spec.mjs +398 -0
  170. package/tests/browser/shell-rollout-fixture.mjs +259 -0
  171. package/tests/browser/shell-rollout.spec.mjs +357 -0
  172. package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
  173. package/tests/browser/shell-workspace.spec.mjs +433 -0
  174. package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
  175. package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
  176. package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
  177. package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
  178. package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
  179. package/tests/browser/stream-output-isolation.spec.mjs +6 -3
  180. package/tests/browser/themed-selects.spec.mjs +297 -0
  181. package/tests/browser/thread-start-flow.spec.mjs +481 -0
  182. package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
  183. package/tests/browser/ui-polish-features.spec.mjs +85 -0
  184. package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
  185. package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
  186. package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
  187. package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
  188. package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
  189. package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
  190. package/tests/browser/windows-drive-parent.spec.mjs +4 -2
  191. package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
  192. package/tests/codex-fast-mode-static.test.mjs +16 -11
  193. package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
  194. package/tests/codex-usage-source.test.mjs +4 -3
  195. package/tests/component-update-api-static.test.mjs +2 -2
  196. package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
  197. package/tests/control-deck-component-updates-static.test.mjs +9 -4
  198. package/tests/control-visibility-static.test.mjs +3 -3
  199. package/tests/controls-layout-static.test.mjs +5 -5
  200. package/tests/conversation-overview-adapter.test.mjs +100 -0
  201. package/tests/document-artifact-static.test.mjs +2 -1
  202. package/tests/draft-conversations.test.mjs +205 -0
  203. package/tests/empty-startup-launcher.test.mjs +197 -0
  204. package/tests/events-tree-context-menu-static.test.mjs +22 -18
  205. package/tests/fake-pi-session-ownership.test.mjs +302 -0
  206. package/tests/fast-mode-client-static.test.mjs +1 -1
  207. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  208. package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
  209. package/tests/file-viewer-image-static.test.mjs +15 -11
  210. package/tests/file-viewer-search-static.test.mjs +5 -5
  211. package/tests/fixtures/fake-pi.mjs +41 -9
  212. package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
  213. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
  214. package/tests/git-footer-context-menu-static.test.mjs +1 -1
  215. package/tests/git-panel-file-preview-static.test.mjs +41 -48
  216. package/tests/git-panel-live-updates-static.test.mjs +2 -1
  217. package/tests/git-workflow-dispatch.test.mjs +66 -0
  218. package/tests/git-workflow-preferences.test.mjs +9 -1
  219. package/tests/guided-git-activation-static.test.mjs +14 -11
  220. package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
  221. package/tests/guided-git-message-reliability-static.test.mjs +10 -6
  222. package/tests/guided-git-publication-backend-static.test.mjs +16 -11
  223. package/tests/http-endpoints-harness.test.mjs +21 -9
  224. package/tests/interaction-state-stability-static.test.mjs +4 -3
  225. package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
  226. package/tests/intercom-tag-layout-static.test.mjs +13 -12
  227. package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
  228. package/tests/main-output-loading-static.test.mjs +4 -4
  229. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  230. package/tests/mobile-compact-layout.test.mjs +2 -2
  231. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  232. package/tests/mobile-foundation-static.test.mjs +10 -7
  233. package/tests/mobile-phone-experience-static.test.mjs +3 -2
  234. package/tests/mobile-static.test.mjs +173 -135
  235. package/tests/model-catalog-order.test.mjs +103 -0
  236. package/tests/native-parity.test.mjs +6 -4
  237. package/tests/native-session-choice-owner.test.mjs +102 -0
  238. package/tests/native-update-lifecycle.test.mjs +10 -5
  239. package/tests/no-project-http-harness.test.mjs +328 -0
  240. package/tests/no-project-navigation.test.mjs +54 -0
  241. package/tests/no-project-workspaces.test.mjs +77 -0
  242. package/tests/open-issue-wizard-static.test.mjs +1 -1
  243. package/tests/optional-feature-migration-frontend.test.mjs +4 -4
  244. package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
  245. package/tests/package-updates.test.mjs +630 -0
  246. package/tests/persistent-ui-layout-static.test.mjs +3 -3
  247. package/tests/platform-event-stream.test.mjs +253 -0
  248. package/tests/project-file-viewer-controller.test.mjs +652 -0
  249. package/tests/project-git-tools-adapter.test.mjs +103 -0
  250. package/tests/project-git-tools-controller.test.mjs +577 -0
  251. package/tests/project-runners-controller.test.mjs +453 -0
  252. package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
  253. package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
  254. package/tests/questionnaire-dialog.test.mjs +6 -6
  255. package/tests/queue-edit-reorder-static.test.mjs +48 -51
  256. package/tests/rpc-supervisor-host.test.mjs +4 -1
  257. package/tests/safety-guard-auto-review-static.test.mjs +1 -1
  258. package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
  259. package/tests/server-app-runners.test.mjs +536 -0
  260. package/tests/server-codex-fast-mode.test.mjs +346 -0
  261. package/tests/server-conversation-mode.test.mjs +386 -0
  262. package/tests/server-conversation-voice.test.mjs +426 -0
  263. package/tests/server-extension-ui-requests.test.mjs +357 -0
  264. package/tests/server-git-generation.test.mjs +558 -0
  265. package/tests/server-git-mutations.test.mjs +729 -0
  266. package/tests/server-git-reads.test.mjs +381 -0
  267. package/tests/server-git-worktree-sessions.test.mjs +502 -0
  268. package/tests/server-helper-requests.test.mjs +364 -0
  269. package/tests/server-rpc-transports.test.mjs +553 -0
  270. package/tests/server-settings-store.test.mjs +132 -0
  271. package/tests/server-shell-rollout.test.mjs +478 -0
  272. package/tests/server-sse-delivery.test.mjs +410 -0
  273. package/tests/server-workspace-files.test.mjs +546 -0
  274. package/tests/session-attention-scheduler.test.mjs +514 -0
  275. package/tests/session-composer-store.test.mjs +153 -0
  276. package/tests/session-context.test.mjs +134 -0
  277. package/tests/session-extension-requests.test.mjs +193 -0
  278. package/tests/session-ownership-adapter.test.mjs +130 -0
  279. package/tests/session-sampling-ui-static.test.mjs +10 -4
  280. package/tests/session-send-lifetime.test.mjs +140 -0
  281. package/tests/session-shelf-state.test.mjs +348 -0
  282. package/tests/session-shelves-adapter.test.mjs +79 -0
  283. package/tests/session-summary-lifetime.test.mjs +54 -0
  284. package/tests/settings-resource-controller.test.mjs +548 -0
  285. package/tests/shell-action-adapter.test.mjs +169 -0
  286. package/tests/shell-actions.test.mjs +182 -0
  287. package/tests/shell-navigation.test.mjs +348 -0
  288. package/tests/shell-preferences.test.mjs +367 -0
  289. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  290. package/tests/stream-output-isolation-static.test.mjs +1 -1
  291. package/tests/subagent-gate-visibility.test.mjs +1 -1
  292. package/tests/subagent-launch-slot-state.test.mjs +2 -2
  293. package/tests/subagent-launch-slots-static.test.mjs +1 -1
  294. package/tests/subagent-observability-static.test.mjs +2 -2
  295. package/tests/subagent-reliability-integration.test.mjs +13 -30
  296. package/tests/syntax-highlighting-static.test.mjs +3 -3
  297. package/tests/tab-close-continuity-static.test.mjs +1 -1
  298. package/tests/tab-metadata-ordering.test.mjs +99 -0
  299. package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
  300. package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
  301. package/tests/terminal-tab-workspace-static.test.mjs +5 -4
  302. package/tests/terminal-tabs-resize-static.test.mjs +1 -1
  303. package/tests/theme-customizer-static.test.mjs +1 -1
  304. package/tests/thread-display-title.test.mjs +44 -0
  305. package/tests/update-api-harness.test.mjs +15 -14
  306. package/tests/update-tab-restore-harness.test.mjs +3 -0
  307. package/tests/webui-settings-locking.test.mjs +2 -2
  308. package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
  309. package/tests/workspace-choices.test.mjs +114 -0
  310. package/tests/workspace-save-load-static.test.mjs +7 -4
@@ -0,0 +1,652 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+ import { createActivityAgentsController } from "../public/activity-agents-controller.mjs";
4
+
5
+ const tick = () => new Promise((resolve) => setImmediate(resolve));
6
+ const tab = { groupId: "tab:parent-a", tabId: "parent-a" };
7
+ const running = (id = "run-a") => ({ id, status: "running", capabilities: { cancel: true }, agents: [{ id: "agent-a" }] });
8
+ const done = (id = "done-a") => ({ id, status: "done", capabilities: { open: true } });
9
+ const overview = (runs = [], extra = {}) => ({
10
+ version: 2,
11
+ runningAgents: 0,
12
+ tabs: [{ tabId: "parent-a", gates: [] }],
13
+ groups: [{ id: "tab:parent-a", kind: "tab", tabId: "parent-a", runs }],
14
+ ...extra,
15
+ });
16
+
17
+ function fakeTimers() {
18
+ let serial = 0;
19
+ const pending = new Map();
20
+ const history = new Map();
21
+ return {
22
+ pending, history,
23
+ setTimeout(callback, delay) {
24
+ const id = ++serial;
25
+ const timer = { callback, delay };
26
+ pending.set(id, timer);
27
+ history.set(id, timer);
28
+ return id;
29
+ },
30
+ clearTimeout(id) { pending.delete(id); },
31
+ fire(id = [...pending.keys()][0]) {
32
+ const timer = pending.get(id);
33
+ assert.ok(timer, `timer ${id} must be pending`);
34
+ pending.delete(id);
35
+ return timer.callback();
36
+ },
37
+ delay() {
38
+ assert.equal(pending.size, 1);
39
+ return [...pending.values()][0].delay;
40
+ },
41
+ };
42
+ }
43
+
44
+ function fixture(overrides = {}) {
45
+ const calls = [];
46
+ const changes = [];
47
+ const overviews = [];
48
+ const settled = [];
49
+ const events = [];
50
+ const statuses = [];
51
+ const closed = [];
52
+ const policyInputs = [];
53
+ const settings = { visibility: "visible", autoClear: false };
54
+ const timers = fakeTimers();
55
+ let controller;
56
+ controller = createActivityAgentsController({
57
+ api(path, options) {
58
+ let resolve;
59
+ let reject;
60
+ const pending = new Promise((accept, fail) => { resolve = accept; reject = fail; });
61
+ calls.push({ path, options, resolve: (data = null) => resolve({ data }), reject });
62
+ return pending;
63
+ },
64
+ getVisibility: () => settings.visibility,
65
+ getAutoClear: () => settings.autoClear,
66
+ finishedSelections(data) {
67
+ policyInputs.push(data);
68
+ return (data?.groups || []).flatMap((group) => group.runs
69
+ .filter((run) => ["done", "failed", "cancelled"].includes(run.status) && run.capabilities?.open)
70
+ .map((run) => ({ groupId: group.id, runId: run.id })));
71
+ },
72
+ canCancel: (run, agent) => ["queued", "running"].includes(agent?.status || run.status) && (agent?.capabilities || run.capabilities)?.cancel === true,
73
+ canDismiss: (run) => run.attached === true || ["done", "failed", "cancelled"].includes(run.status) && run.capabilities?.open === true,
74
+ isAttachedProjection: (run) => run.attached === true,
75
+ agentCount: (run) => run.agents?.length || 0,
76
+ onOverview: (data) => overviews.push(data),
77
+ onRefreshSettled: () => settled.push(controller.getSnapshot()),
78
+ onChange: () => changes.push(controller.getSnapshot()),
79
+ onEvent: (...args) => events.push(args),
80
+ onCancelStatus: (...args) => statuses.push(args),
81
+ onCancelClosed: (selection) => closed.push(selection),
82
+ setTimeout: timers.setTimeout,
83
+ clearTimeout: timers.clearTimeout,
84
+ ...overrides,
85
+ });
86
+ return {
87
+ controller, calls, changes, overviews, settled, events, statuses, closed, settings, timers, policyInputs,
88
+ async seed(data = overview()) {
89
+ const request = controller.refresh();
90
+ calls.at(-1).resolve(data);
91
+ await request;
92
+ return data;
93
+ },
94
+ };
95
+ }
96
+
97
+ test("construction is inert and snapshots borrow the one overview without cloning", async () => {
98
+ const f = fixture();
99
+ const c = f.controller;
100
+ assert.deepEqual(Object.keys(c).sort(), ["start", "refresh", "schedule", "getSnapshot", "beginCancel", "resetCancel", "submitCancel", "dismiss", "clearFinished", "dispose"].sort());
101
+ assert.deepEqual(c.getSnapshot(), { data: null, error: null, loading: false, clearingFinished: false, cancelSelection: null, cancelSubmitting: false });
102
+ assert.equal(f.calls.length, 0);
103
+ assert.equal(f.timers.pending.size, 0);
104
+ const data = await f.seed(overview([running()]));
105
+ const snapshot = c.getSnapshot();
106
+ assert.ok(Object.isFrozen(snapshot));
107
+ assert.throws(() => { snapshot.data = null; }, TypeError);
108
+ assert.strictEqual(snapshot.data, data);
109
+ assert.strictEqual(c.getSnapshot().data, data);
110
+ assert.equal(f.calls[0].path, "/api/subagents");
111
+ assert.deepEqual(f.calls[0].options, { scoped: false });
112
+ assert.deepEqual(f.overviews, [data]);
113
+ assert.equal(f.settled.length, 1);
114
+ assert.equal(f.changes[0].loading, true);
115
+ assert.equal(f.changes.at(-1).loading, false);
116
+ });
117
+
118
+ test("overlapping refreshes coalesce into one follow-up per in-flight batch", async () => {
119
+ const f = fixture();
120
+ const c = f.controller;
121
+ const first = c.refresh();
122
+ const second = c.refresh();
123
+ const third = c.refresh();
124
+ assert.equal(f.calls.length, 1);
125
+ const early = overview([running()], { updatedAt: 1 });
126
+ f.calls[0].resolve(early);
127
+ await tick();
128
+ assert.equal(f.calls.length, 2);
129
+ assert.equal(c.getSnapshot().loading, true);
130
+ assert.deepEqual(f.overviews, [early]);
131
+ assert.equal(f.settled.length, 0);
132
+ const fourth = c.refresh();
133
+ const fifth = c.refresh();
134
+ f.calls[1].resolve(overview([], { updatedAt: 2 }));
135
+ await tick();
136
+ assert.equal(f.calls.length, 3);
137
+ const last = overview([done()], { updatedAt: 3 });
138
+ f.calls[2].resolve(last);
139
+ await Promise.all([first, second, third, fourth, fifth]);
140
+ assert.strictEqual(c.getSnapshot().data, last);
141
+ assert.equal(c.getSnapshot().loading, false);
142
+ assert.equal(f.settled.length, 1);
143
+ assert.equal(f.overviews.length, 3);
144
+ assert.equal(f.timers.pending.size, 0);
145
+ });
146
+
147
+ test("refresh failure retains the overview and a later success clears the error", async () => {
148
+ const f = fixture();
149
+ const data = await f.seed();
150
+ const failure = new Error("overview unavailable");
151
+ const failed = f.controller.refresh();
152
+ f.calls[1].reject(failure);
153
+ await failed;
154
+ assert.strictEqual(f.controller.getSnapshot().data, data);
155
+ assert.strictEqual(f.controller.getSnapshot().error, failure);
156
+ assert.equal(f.controller.getSnapshot().loading, false);
157
+ assert.equal(f.overviews.length, 1);
158
+ assert.equal(f.settled.length, 2);
159
+ const recovered = f.controller.refresh();
160
+ f.calls[2].resolve();
161
+ await recovered;
162
+ assert.equal(f.controller.getSnapshot().data, null);
163
+ assert.equal(f.controller.getSnapshot().error, null);
164
+ });
165
+
166
+ test("start is idempotent and schedules only after its initial request settles", async () => {
167
+ const f = fixture();
168
+ const initial = f.controller.start();
169
+ f.controller.start();
170
+ f.controller.start();
171
+ assert.equal(f.calls.length, 1);
172
+ assert.equal(f.timers.pending.size, 0);
173
+ f.calls[0].resolve(overview());
174
+ await initial;
175
+ assert.equal(f.timers.delay(), 4000);
176
+ f.controller.start();
177
+ assert.equal(f.calls.length, 1);
178
+ assert.equal(f.timers.pending.size, 1);
179
+ });
180
+
181
+ test("polling retains active, running-gate, idle and hidden cadence with one replaceable timer", async () => {
182
+ const f = fixture();
183
+ const c = f.controller;
184
+ await f.seed(overview([], { runningAgents: 2 }));
185
+ c.schedule();
186
+ assert.equal(f.timers.delay(), 1500);
187
+ await f.seed(overview([], { tabs: [{ tabId: "parent-a", gates: [{ status: "running" }] }] }));
188
+ c.schedule();
189
+ assert.equal(f.timers.delay(), 1500);
190
+ await f.seed(overview());
191
+ c.schedule();
192
+ assert.equal(f.timers.delay(), 4000);
193
+ const requests = f.calls.length;
194
+ f.settings.visibility = "hidden";
195
+ c.schedule();
196
+ assert.equal(f.timers.delay(), 10_000);
197
+ assert.equal(f.calls.length, requests, "hiding does not dismiss or cancel");
198
+ c.schedule(0);
199
+ assert.equal(f.timers.delay(), 0);
200
+ const poll = f.timers.fire();
201
+ f.calls.at(-1).resolve(overview());
202
+ await poll;
203
+ assert.equal(f.timers.delay(), 10_000);
204
+ });
205
+
206
+ test("a newer explicit schedule survives both initial and timer refresh finalizers", async () => {
207
+ const f = fixture();
208
+ const c = f.controller;
209
+ const initial = c.start();
210
+ c.schedule(71);
211
+ f.calls[0].resolve(overview());
212
+ await initial;
213
+ assert.equal(f.timers.delay(), 71);
214
+ const oldId = [...f.timers.pending.keys()][0];
215
+ const poll = f.timers.fire(oldId);
216
+ c.schedule(83);
217
+ f.calls[1].resolve(overview());
218
+ await poll;
219
+ assert.equal(f.timers.delay(), 83);
220
+ const count = f.calls.length;
221
+ await f.timers.history.get(oldId).callback();
222
+ assert.equal(f.calls.length, count, "a superseded callback cannot refresh");
223
+ });
224
+
225
+ test("cancel selection preserves explicit group/run/agent identity and checks supplied policy", () => {
226
+ const policyCalls = [];
227
+ const countCalls = [];
228
+ const f = fixture({
229
+ canCancel: (...args) => { policyCalls.push(args); return args[1]?.allowed === true; },
230
+ agentCount: (run) => { countCalls.push(run); return 3; },
231
+ });
232
+ const run = running();
233
+ const agent = { id: "display-id", instanceId: "instance-id", allowed: true };
234
+ const external = { groupId: "external", tabId: "display-tab" };
235
+ assert.equal(f.controller.beginCancel({}, run, agent), null);
236
+ assert.equal(f.controller.beginCancel(external, {}, agent), null);
237
+ assert.equal(f.controller.beginCancel(external, run), null);
238
+ const selection = f.controller.beginCancel(external, run, agent);
239
+ assert.deepEqual(selection, { groupId: "external", tabId: "display-tab", runId: "run-a", agentId: "instance-id", agentCount: 3 });
240
+ assert.ok(Object.isFrozen(selection));
241
+ assert.strictEqual(f.controller.getSnapshot().cancelSelection, selection);
242
+ assert.deepEqual(policyCalls, [[run, null], [run, agent]]);
243
+ assert.deepEqual(countCalls, [run]);
244
+ assert.equal(f.calls.length, 0);
245
+ const replacement = f.controller.beginCancel(tab, running("run-b"), agent);
246
+ assert.equal(replacement.runId, "run-b");
247
+ assert.notStrictEqual(replacement, selection);
248
+ });
249
+
250
+ test("cancel submits the captured unscoped whole-run request, locks duplicates, then refreshes", async () => {
251
+ const f = fixture();
252
+ const c = f.controller;
253
+ const external = { groupId: "external", tabId: "parent-a" };
254
+ const run = running();
255
+ run.agents.push({ id: "agent-b" });
256
+ const selection = c.beginCancel(external, run, { id: "agent-id", instanceId: "instance-id" });
257
+ const cancel = c.submitCancel({ reason: " duplicate work ", note: " keep artifacts \n" });
258
+ assert.equal(c.getSnapshot().cancelSubmitting, true);
259
+ assert.equal(c.beginCancel(tab, running("replacement")), null);
260
+ assert.equal(await c.submitCancel(), false);
261
+ external.groupId = "changed";
262
+ run.id = "changed";
263
+ assert.deepEqual(f.calls[0].options, {
264
+ method: "POST", scoped: false,
265
+ body: { group: "external", runId: "run-a", agentId: "instance-id", reason: "duplicate work", note: "keep artifacts" },
266
+ });
267
+ assert.equal(f.calls[0].path, "/api/subagents/cancel");
268
+ assert.deepEqual(f.statuses.at(-1), ["Cancelling subagent…"]);
269
+ f.calls[0].resolve();
270
+ await tick();
271
+ assert.deepEqual(f.closed, [selection]);
272
+ assert.deepEqual(f.events, [["cancelled run run-a; the parent agent was notified", "info"]]);
273
+ assert.equal(f.calls[1].path, "/api/subagents");
274
+ f.calls[1].resolve(overview());
275
+ assert.equal(await cancel, true);
276
+ assert.equal(c.getSnapshot().cancelSubmitting, false);
277
+ assert.equal(f.timers.delay(), 4000);
278
+ });
279
+
280
+ test("cancel omits empty optional fields and prefers the agent id when no instance id exists", async () => {
281
+ const f = fixture();
282
+ f.controller.beginCancel(tab, running());
283
+ const first = f.controller.submitCancel({ reason: " \n", note: " " });
284
+ assert.deepEqual(f.calls[0].options.body, { group: "tab:parent-a", runId: "run-a" });
285
+ f.calls[0].reject(new Error("denied"));
286
+ await first;
287
+ f.controller.beginCancel(tab, running(), { id: "agent-id" });
288
+ const second = f.controller.submitCancel();
289
+ assert.deepEqual(f.calls[1].options.body, { group: "tab:parent-a", runId: "run-a", agentId: "agent-id" });
290
+ f.calls[1].reject("offline");
291
+ await second;
292
+ assert.deepEqual(f.statuses.at(-1), ["subagent cancel failed: offline", "error"]);
293
+ });
294
+
295
+ test("cancel failure keeps the selection, reports original status and releases only its lock", async () => {
296
+ const f = fixture();
297
+ const selection = f.controller.beginCancel(tab, running());
298
+ const request = f.controller.submitCancel();
299
+ f.calls[0].reject(new Error("not cancellable"));
300
+ assert.equal(await request, false);
301
+ assert.strictEqual(f.controller.getSnapshot().cancelSelection, selection);
302
+ assert.equal(f.controller.getSnapshot().cancelSubmitting, false);
303
+ assert.deepEqual(f.statuses.at(-1), ["subagent cancel failed: not cancellable", "error"]);
304
+ assert.deepEqual(f.events, [["subagent cancel failed: not cancellable", "error"]]);
305
+ assert.equal(f.closed.length, 0);
306
+ assert.equal(f.calls.length, 1);
307
+ const retry = f.controller.submitCancel();
308
+ assert.equal(f.calls.length, 2);
309
+ f.calls[1].reject(new Error("still denied"));
310
+ await retry;
311
+ });
312
+
313
+ for (const outcome of ["success", "failure"]) {
314
+ test(`reset/reopen rejects an old cancel ${outcome} and its finally cannot unlock a new request`, async () => {
315
+ const f = fixture();
316
+ const c = f.controller;
317
+ const previous = c.beginCancel(tab, running());
318
+ const old = c.submitCancel();
319
+ c.resetCancel();
320
+ assert.equal(c.getSnapshot().cancelSubmitting, false);
321
+ const next = c.beginCancel(tab, running());
322
+ assert.deepEqual(next, previous);
323
+ assert.notStrictEqual(next, previous, "reopening the same run still creates a new selection owner");
324
+ const current = c.submitCancel({ reason: "new reason" });
325
+ const callbacks = [f.statuses.length, f.changes.length];
326
+ if (outcome === "success") f.calls[0].resolve();
327
+ else f.calls[0].reject(new Error("old failure"));
328
+ assert.equal(await old, false);
329
+ assert.strictEqual(c.getSnapshot().cancelSelection, next);
330
+ assert.equal(c.getSnapshot().cancelSubmitting, true);
331
+ assert.deepEqual([f.statuses.length, f.changes.length], callbacks);
332
+ assert.deepEqual(f.closed, []);
333
+ assert.deepEqual(f.events, []);
334
+ assert.equal(f.calls.length, 2);
335
+ f.calls[1].resolve();
336
+ await tick();
337
+ assert.deepEqual(f.closed, [next]);
338
+ f.calls[2].resolve(overview());
339
+ await current;
340
+ assert.equal(c.getSnapshot().cancelSubmitting, false);
341
+ });
342
+ }
343
+
344
+ test("success closes only the selected dialog and its pending refresh cannot unlock a reopened dialog", async () => {
345
+ let f;
346
+ f = fixture({ onCancelClosed(selection) {
347
+ f.closed.push(selection);
348
+ f.controller.resetCancel();
349
+ } });
350
+ const c = f.controller;
351
+ const firstSelection = c.beginCancel(tab, running());
352
+ const old = c.submitCancel();
353
+ f.calls[0].resolve();
354
+ await tick();
355
+ assert.deepEqual(f.closed, [firstSelection]);
356
+ const next = c.beginCancel(tab, running("run-b"));
357
+ const current = c.submitCancel();
358
+ assert.equal(f.calls[1].path, "/api/subagents");
359
+ assert.equal(f.calls[2].path, "/api/subagents/cancel");
360
+ f.calls[1].resolve(overview());
361
+ await old;
362
+ assert.strictEqual(c.getSnapshot().cancelSelection, next);
363
+ assert.equal(c.getSnapshot().cancelSubmitting, true);
364
+ f.calls[2].reject(new Error("new error"));
365
+ await current;
366
+ assert.deepEqual(f.statuses.at(-1), ["subagent cancel failed: new error", "error"]);
367
+ assert.equal(c.getSnapshot().cancelSubmitting, false);
368
+ });
369
+
370
+ test("resetting or closing selection is local and does not clear overview resources", async () => {
371
+ const f = fixture();
372
+ const data = await f.seed(overview([running()]));
373
+ const count = f.calls.length;
374
+ f.controller.beginCancel(tab, running());
375
+ f.controller.resetCancel();
376
+ assert.equal(f.controller.getSnapshot().cancelSelection, null);
377
+ assert.equal(f.controller.getSnapshot().cancelSubmitting, false);
378
+ assert.strictEqual(f.controller.getSnapshot().data, data);
379
+ assert.equal(f.calls.length, count);
380
+ assert.deepEqual(f.closed, []);
381
+ assert.deepEqual(f.events, []);
382
+ assert.deepEqual(f.statuses.at(-1), [""]);
383
+ });
384
+
385
+ test("dismiss validates capability and preserves external group identity and detach wording", async () => {
386
+ const f = fixture();
387
+ const c = f.controller;
388
+ assert.equal(await c.dismiss({}, done()), false);
389
+ assert.equal(await c.dismiss(tab, running()), false);
390
+ assert.equal(f.calls.length, 0);
391
+ const external = { groupId: "external", tabId: "parent-a" };
392
+ const projection = { id: "projection-a", status: "stale", attached: true };
393
+ const request = c.dismiss(external, projection);
394
+ assert.equal(f.calls[0].path, "/api/subagents/dismiss");
395
+ assert.deepEqual(f.calls[0].options, { method: "POST", scoped: false, body: { group: "external", runId: "projection-a" } });
396
+ external.groupId = "changed";
397
+ projection.id = "changed";
398
+ f.calls[0].resolve();
399
+ await tick();
400
+ assert.deepEqual(f.events, [["detached persisted session projection projection-a", "info"]]);
401
+ assert.deepEqual(f.closed, []);
402
+ f.calls[1].resolve(overview());
403
+ assert.equal(await request, true);
404
+ assert.equal(f.timers.delay(), 4000);
405
+ });
406
+
407
+ test("finished dismissal reports its own event, while failure does not refresh or close a dialog", async () => {
408
+ const f = fixture();
409
+ const c = f.controller;
410
+ const selection = c.beginCancel(tab, running());
411
+ const first = c.dismiss(tab, done());
412
+ f.calls[0].resolve();
413
+ await tick();
414
+ f.calls[1].resolve(overview());
415
+ await first;
416
+ assert.deepEqual(f.events, [["dismissed finished subagent run done-a", "info"]]);
417
+ const failed = c.dismiss(tab, done("failed-b"));
418
+ f.calls[2].reject(new Error("cannot dismiss"));
419
+ assert.equal(await failed, false);
420
+ assert.deepEqual(f.events.at(-1), ["subagent dismiss failed: cannot dismiss", "error"]);
421
+ assert.equal(f.calls.length, 3);
422
+ assert.strictEqual(c.getSnapshot().cancelSelection, selection);
423
+ assert.deepEqual(f.closed, []);
424
+ });
425
+
426
+ test("a mutation overlapping an overview fetch requests one authoritative follow-up", async () => {
427
+ const f = fixture();
428
+ const refresh = f.controller.refresh();
429
+ const dismiss = f.controller.dismiss(tab, done());
430
+ f.calls[1].resolve();
431
+ await tick();
432
+ assert.equal(f.calls.length, 2);
433
+ const beforeDismissal = overview([done()]);
434
+ f.calls[0].resolve(beforeDismissal);
435
+ await tick();
436
+ assert.equal(f.calls.length, 3);
437
+ const afterDismissal = overview();
438
+ f.calls[2].resolve(afterDismissal);
439
+ await Promise.all([refresh, dismiss]);
440
+ assert.strictEqual(f.controller.getSnapshot().data, afterDismissal);
441
+ assert.equal(f.settled.length, 1);
442
+ });
443
+
444
+ test("Clear finished snapshots selections, dismisses sequentially, and reports partial failure", async () => {
445
+ const selections = [{ groupId: "tab:parent-a", runId: "done-a" }, { groupId: "external", runId: "done-b" }, { groupId: "external", runId: "done-c" }];
446
+ const f = fixture({ finishedSelections: () => selections });
447
+ const c = f.controller;
448
+ await f.seed(overview([done()]));
449
+ c.schedule();
450
+ const clear = c.clearFinished();
451
+ assert.equal(c.getSnapshot().clearingFinished, true);
452
+ assert.equal(f.timers.pending.size, 0);
453
+ assert.equal(f.calls.length, 2);
454
+ selections[1].groupId = "changed";
455
+ selections.push({ groupId: "external", runId: "new-run" });
456
+ await c.clearFinished();
457
+ assert.equal(f.calls.length, 2);
458
+ f.calls[1].resolve();
459
+ await tick();
460
+ assert.equal(f.calls.length, 3);
461
+ assert.deepEqual(f.calls[2].options.body, { group: "external", runId: "done-b" });
462
+ f.calls[2].reject(new Error("permission denied"));
463
+ await tick();
464
+ assert.equal(f.calls.length, 4);
465
+ f.calls[3].resolve();
466
+ await tick();
467
+ assert.equal(f.calls[4].path, "/api/subagents");
468
+ assert.equal(c.getSnapshot().clearingFinished, true);
469
+ f.calls[4].resolve(overview());
470
+ await clear;
471
+ assert.equal(f.calls.length, 5);
472
+ assert.equal(c.getSnapshot().clearingFinished, false);
473
+ assert.equal(c.getSnapshot().loading, false);
474
+ assert.deepEqual(f.events, [["cleared 2 finished subagent runs", "info"], ["could not clear 1 finished subagent run", "warn"]]);
475
+ assert.deepEqual(f.calls.slice(1, 4).map((call) => call.options), [
476
+ { method: "POST", scoped: false, body: { group: "tab:parent-a", runId: "done-a" } },
477
+ { method: "POST", scoped: false, body: { group: "external", runId: "done-b" } },
478
+ { method: "POST", scoped: false, body: { group: "external", runId: "done-c" } },
479
+ ]);
480
+ assert.equal(f.timers.delay(), 4000);
481
+ });
482
+
483
+ test("Clear finished is inert during loading or without policy-eligible selections", async () => {
484
+ const f = fixture();
485
+ const c = f.controller;
486
+ await c.clearFinished();
487
+ assert.equal(f.calls.length, 0);
488
+ const pending = c.refresh();
489
+ await c.clearFinished();
490
+ assert.equal(f.calls.length, 1);
491
+ f.calls[0].resolve(overview([running(), { id: "stale", status: "stale", attached: true }, { id: "no-open", status: "done" }]));
492
+ await pending;
493
+ await c.clearFinished();
494
+ assert.equal(f.calls.length, 1);
495
+ assert.deepEqual(f.events, []);
496
+ });
497
+
498
+ test("Auto-Clear uses only supplied eligible groups and cannot recurse on its refresh", async () => {
499
+ const f = fixture();
500
+ f.settings.autoClear = true;
501
+ const c = f.controller;
502
+ const data = overview([running(), done(), { id: "retained", status: "done", capabilities: { open: false } }]);
503
+ data.groups.push({ id: "external", kind: "external", runs: [done("external-done")] });
504
+ const initial = c.refresh();
505
+ f.calls[0].resolve(data);
506
+ await tick();
507
+ assert.strictEqual(f.policyInputs[0], data);
508
+ assert.equal(f.calls[1].path, "/api/subagents/dismiss");
509
+ assert.deepEqual(f.calls[1].options.body, { group: "tab:parent-a", runId: "done-a" });
510
+ f.calls[1].resolve();
511
+ await tick();
512
+ assert.deepEqual(f.calls[2].options.body, { group: "external", runId: "external-done" });
513
+ f.calls[2].resolve();
514
+ await tick();
515
+ assert.equal(f.calls[3].path, "/api/subagents");
516
+ f.calls[3].resolve(data);
517
+ await initial;
518
+ assert.equal(f.calls.length, 4, "an unchanged refresh cannot recursively clear again");
519
+ assert.equal(c.getSnapshot().clearingFinished, false);
520
+ assert.deepEqual(f.events, [["auto-cleared", "info", {
521
+ aggregateKey: "subagent-auto-clear", aggregateIncrement: 2,
522
+ aggregateSingular: "finished subagent run", aggregatePlural: "finished subagent runs",
523
+ }]]);
524
+ assert.equal(f.timers.delay(), 4000);
525
+ });
526
+
527
+ test("Auto-Clear partial failure retains aggregate metadata and the original warning", async () => {
528
+ const f = fixture();
529
+ await f.seed(overview([done(), { ...done("failed-b"), status: "failed" }]));
530
+ f.settings.autoClear = true;
531
+ const request = f.controller.refresh();
532
+ f.calls[1].resolve(f.controller.getSnapshot().data);
533
+ await tick();
534
+ f.calls[2].resolve();
535
+ await tick();
536
+ f.calls[3].reject(new Error("busy"));
537
+ await tick();
538
+ f.calls[4].reject(new Error("refresh failed"));
539
+ await request;
540
+ assert.deepEqual(f.events, [["auto-cleared", "info", {
541
+ aggregateKey: "subagent-auto-clear", aggregateIncrement: 1,
542
+ aggregateSingular: "finished subagent run", aggregatePlural: "finished subagent runs",
543
+ }], ["could not auto-clear 1 finished subagent run", "warn"]]);
544
+ assert.equal(f.controller.getSnapshot().error.message, "refresh failed");
545
+ assert.equal(f.controller.getSnapshot().clearingFinished, false);
546
+ assert.equal(f.timers.delay(), 4000);
547
+ });
548
+
549
+ test("a failed refresh alone cannot Auto-Clear retained data and the preference is read at completion", async () => {
550
+ const f = fixture();
551
+ await f.seed(overview([done()]));
552
+ f.settings.autoClear = true;
553
+ const failed = f.controller.refresh();
554
+ f.calls[1].reject(new Error("offline"));
555
+ await failed;
556
+ assert.equal(f.calls.length, 2);
557
+ const next = f.controller.refresh();
558
+ f.settings.autoClear = false;
559
+ f.calls[2].resolve(overview([done()]));
560
+ await next;
561
+ assert.equal(f.calls.length, 3);
562
+ assert.deepEqual(f.events, []);
563
+ });
564
+
565
+ test("Clear finished leaves a newer explicit schedule intact and releases after every failure", async () => {
566
+ const f = fixture();
567
+ const c = f.controller;
568
+ await f.seed(overview([done()]));
569
+ const clear = c.clearFinished();
570
+ c.schedule(123);
571
+ f.calls[1].reject(new Error("dismiss failed"));
572
+ await tick();
573
+ f.calls[2].reject(new Error("refresh failed"));
574
+ await clear;
575
+ assert.deepEqual(f.events, [["could not clear 1 finished subagent run", "warn"]]);
576
+ assert.equal(c.getSnapshot().clearingFinished, false);
577
+ assert.equal(c.getSnapshot().loading, false);
578
+ assert.equal(f.timers.delay(), 123);
579
+ const retry = c.clearFinished();
580
+ assert.equal(f.calls.length, 4);
581
+ f.calls[3].resolve();
582
+ await tick();
583
+ f.calls[4].resolve(overview());
584
+ await retry;
585
+ assert.deepEqual(f.events.at(-1), ["cleared 1 finished subagent run", "info"]);
586
+ });
587
+
588
+ for (const outcome of ["success", "failure"]) {
589
+ test(`dispose rejects a late overview ${outcome}, clears timers and prevents all new work`, async () => {
590
+ const f = fixture();
591
+ const c = f.controller;
592
+ const retained = await f.seed(overview([running()]));
593
+ c.beginCancel(tab, running());
594
+ c.schedule(7);
595
+ const timer = [...f.timers.pending.keys()][0];
596
+ const request = f.timers.fire(timer);
597
+ const queued = c.refresh();
598
+ c.dispose();
599
+ c.dispose();
600
+ const counts = [f.overviews.length, f.settled.length, f.changes.length];
601
+ if (outcome === "success") f.calls[1].resolve(overview());
602
+ else f.calls[1].reject(new Error("late error"));
603
+ await Promise.all([request, queued]);
604
+ assert.strictEqual(c.getSnapshot().data, retained);
605
+ assert.equal(c.getSnapshot().error, null);
606
+ assert.equal(c.getSnapshot().loading, false);
607
+ assert.deepEqual([f.overviews.length, f.settled.length, f.changes.length], counts);
608
+ c.start();
609
+ await c.refresh();
610
+ c.schedule(0);
611
+ await f.timers.history.get(timer).callback();
612
+ assert.equal(c.beginCancel(tab, running()), null);
613
+ c.resetCancel();
614
+ await c.submitCancel();
615
+ await c.dismiss(tab, done());
616
+ await c.clearFinished();
617
+ assert.equal(f.calls.length, 2);
618
+ assert.equal(f.timers.pending.size, 0);
619
+ assert.deepEqual(f.closed, []);
620
+ assert.deepEqual(f.events, []);
621
+ });
622
+ }
623
+
624
+ for (const action of ["cancel", "dismiss", "clear"]) {
625
+ for (const outcome of ["success", "failure"]) {
626
+ test(`dispose during ${action} suppresses a late ${outcome} without further server work`, async () => {
627
+ const f = fixture();
628
+ const c = f.controller;
629
+ const data = await f.seed(overview([done(), done("done-b")]));
630
+ let request;
631
+ if (action === "cancel") {
632
+ c.beginCancel(tab, running());
633
+ request = c.submitCancel();
634
+ } else if (action === "dismiss") {
635
+ request = c.dismiss(tab, done());
636
+ } else {
637
+ request = c.clearFinished();
638
+ }
639
+ c.dispose();
640
+ const counts = [f.changes.length, f.statuses.length, f.settled.length];
641
+ if (outcome === "success") f.calls[1].resolve();
642
+ else f.calls[1].reject(new Error("late error"));
643
+ await request;
644
+ assert.equal(f.calls.length, 2);
645
+ assert.strictEqual(c.getSnapshot().data, data);
646
+ assert.deepEqual([f.changes.length, f.statuses.length, f.settled.length], counts);
647
+ assert.deepEqual(f.events, []);
648
+ assert.deepEqual(f.closed, []);
649
+ assert.equal(f.timers.pending.size, 0);
650
+ });
651
+ }
652
+ }