@firstpick/pi-package-webui 0.11.1 → 0.11.2

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 (309) 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 +320 -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/activity-intercom-controller.spec.mjs +457 -0
  93. package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
  94. package/tests/browser/activity-owner-integration.spec.mjs +201 -0
  95. package/tests/browser/activity-queue-controller.spec.mjs +405 -0
  96. package/tests/browser/activity-section-picker.spec.mjs +294 -0
  97. package/tests/browser/activity-todo-view.spec.mjs +60 -0
  98. package/tests/browser/activity-usage-controller.spec.mjs +416 -0
  99. package/tests/browser/compact-layout-integration.spec.mjs +58 -0
  100. package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
  101. package/tests/browser/composer-disclosure.spec.mjs +389 -0
  102. package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
  103. package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
  104. package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
  105. package/tests/browser/control-visibility.spec.mjs +5 -0
  106. package/tests/browser/controls-layout.spec.mjs +21 -2
  107. package/tests/browser/conversation-overview.spec.mjs +251 -0
  108. package/tests/browser/draft-conversations.spec.mjs +276 -0
  109. package/tests/browser/empty-startup-fixture.mjs +135 -0
  110. package/tests/browser/empty-startup.spec.mjs +282 -0
  111. package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
  112. package/tests/browser/focused-composer-more.spec.mjs +127 -0
  113. package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
  114. package/tests/browser/focused-navigation-fixture.mjs +32 -0
  115. package/tests/browser/focused-project-activity.spec.mjs +111 -0
  116. package/tests/browser/focused-shell-integration.spec.mjs +208 -0
  117. package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
  118. package/tests/browser/folder-picker-layout.spec.mjs +302 -0
  119. package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
  120. package/tests/browser/git-setup-cancel.spec.mjs +251 -0
  121. package/tests/browser/guided-git-fallback.spec.mjs +40 -21
  122. package/tests/browser/inspector-header.spec.mjs +221 -0
  123. package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
  124. package/tests/browser/inspector-resize.spec.mjs +227 -0
  125. package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
  126. package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
  127. package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
  128. package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
  129. package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
  130. package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
  131. package/tests/browser/interaction-continuity.spec.mjs +50 -26
  132. package/tests/browser/interaction-model-picker.spec.mjs +86 -0
  133. package/tests/browser/last-terminal-close.spec.mjs +280 -0
  134. package/tests/browser/mobile-foundation.spec.mjs +172 -20
  135. package/tests/browser/model-catalog-groups.spec.mjs +104 -0
  136. package/tests/browser/more-row-menus.spec.mjs +258 -0
  137. package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
  138. package/tests/browser/no-project-conversation.spec.mjs +113 -0
  139. package/tests/browser/optimistic-prompt.spec.mjs +3 -0
  140. package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
  141. package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
  142. package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
  143. package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
  144. package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
  145. package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
  146. package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
  147. package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
  148. package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
  149. package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
  150. package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
  151. package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
  152. package/tests/browser/project-artifact-controller.spec.mjs +324 -0
  153. package/tests/browser/project-artifact-integration.spec.mjs +103 -0
  154. package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
  155. package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
  156. package/tests/browser/session-fixture.mjs +118 -0
  157. package/tests/browser/session-ownership.spec.mjs +175 -0
  158. package/tests/browser/session-shelves-integration.spec.mjs +219 -0
  159. package/tests/browser/session-shelves.spec.mjs +340 -0
  160. package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
  161. package/tests/browser/session-summary.spec.mjs +17 -12
  162. package/tests/browser/settings-complete-home.spec.mjs +463 -0
  163. package/tests/browser/settings-entrypoint.spec.mjs +104 -0
  164. package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
  165. package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
  166. package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
  167. package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
  168. package/tests/browser/shell-ownership.spec.mjs +398 -0
  169. package/tests/browser/shell-rollout-fixture.mjs +259 -0
  170. package/tests/browser/shell-rollout.spec.mjs +357 -0
  171. package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
  172. package/tests/browser/shell-workspace.spec.mjs +433 -0
  173. package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
  174. package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
  175. package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
  176. package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
  177. package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
  178. package/tests/browser/stream-output-isolation.spec.mjs +6 -3
  179. package/tests/browser/themed-selects.spec.mjs +297 -0
  180. package/tests/browser/thread-start-flow.spec.mjs +481 -0
  181. package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
  182. package/tests/browser/ui-polish-features.spec.mjs +85 -0
  183. package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
  184. package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
  185. package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
  186. package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
  187. package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
  188. package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
  189. package/tests/browser/windows-drive-parent.spec.mjs +4 -2
  190. package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
  191. package/tests/codex-fast-mode-static.test.mjs +16 -11
  192. package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
  193. package/tests/codex-usage-source.test.mjs +4 -3
  194. package/tests/component-update-api-static.test.mjs +2 -2
  195. package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
  196. package/tests/control-deck-component-updates-static.test.mjs +9 -4
  197. package/tests/control-visibility-static.test.mjs +3 -3
  198. package/tests/controls-layout-static.test.mjs +5 -5
  199. package/tests/conversation-overview-adapter.test.mjs +100 -0
  200. package/tests/document-artifact-static.test.mjs +2 -1
  201. package/tests/draft-conversations.test.mjs +205 -0
  202. package/tests/empty-startup-launcher.test.mjs +197 -0
  203. package/tests/events-tree-context-menu-static.test.mjs +22 -18
  204. package/tests/fake-pi-session-ownership.test.mjs +302 -0
  205. package/tests/fast-mode-client-static.test.mjs +1 -1
  206. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  207. package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
  208. package/tests/file-viewer-image-static.test.mjs +15 -11
  209. package/tests/file-viewer-search-static.test.mjs +5 -5
  210. package/tests/fixtures/fake-pi.mjs +41 -9
  211. package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
  212. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
  213. package/tests/git-footer-context-menu-static.test.mjs +1 -1
  214. package/tests/git-panel-file-preview-static.test.mjs +41 -48
  215. package/tests/git-panel-live-updates-static.test.mjs +2 -1
  216. package/tests/git-workflow-dispatch.test.mjs +66 -0
  217. package/tests/git-workflow-preferences.test.mjs +9 -1
  218. package/tests/guided-git-activation-static.test.mjs +14 -11
  219. package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
  220. package/tests/guided-git-message-reliability-static.test.mjs +10 -6
  221. package/tests/guided-git-publication-backend-static.test.mjs +16 -11
  222. package/tests/http-endpoints-harness.test.mjs +21 -9
  223. package/tests/interaction-state-stability-static.test.mjs +4 -3
  224. package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
  225. package/tests/intercom-tag-layout-static.test.mjs +13 -12
  226. package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
  227. package/tests/main-output-loading-static.test.mjs +4 -4
  228. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  229. package/tests/mobile-compact-layout.test.mjs +2 -2
  230. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  231. package/tests/mobile-foundation-static.test.mjs +10 -7
  232. package/tests/mobile-phone-experience-static.test.mjs +3 -2
  233. package/tests/mobile-static.test.mjs +173 -135
  234. package/tests/model-catalog-order.test.mjs +103 -0
  235. package/tests/native-parity.test.mjs +6 -4
  236. package/tests/native-session-choice-owner.test.mjs +102 -0
  237. package/tests/native-update-lifecycle.test.mjs +10 -5
  238. package/tests/no-project-http-harness.test.mjs +328 -0
  239. package/tests/no-project-navigation.test.mjs +54 -0
  240. package/tests/no-project-workspaces.test.mjs +77 -0
  241. package/tests/open-issue-wizard-static.test.mjs +1 -1
  242. package/tests/optional-feature-migration-frontend.test.mjs +4 -4
  243. package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
  244. package/tests/package-updates.test.mjs +630 -0
  245. package/tests/persistent-ui-layout-static.test.mjs +3 -3
  246. package/tests/platform-event-stream.test.mjs +253 -0
  247. package/tests/project-file-viewer-controller.test.mjs +652 -0
  248. package/tests/project-git-tools-adapter.test.mjs +103 -0
  249. package/tests/project-git-tools-controller.test.mjs +577 -0
  250. package/tests/project-runners-controller.test.mjs +453 -0
  251. package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
  252. package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
  253. package/tests/questionnaire-dialog.test.mjs +6 -6
  254. package/tests/queue-edit-reorder-static.test.mjs +48 -51
  255. package/tests/rpc-supervisor-host.test.mjs +4 -1
  256. package/tests/safety-guard-auto-review-static.test.mjs +1 -1
  257. package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
  258. package/tests/server-app-runners.test.mjs +536 -0
  259. package/tests/server-codex-fast-mode.test.mjs +346 -0
  260. package/tests/server-conversation-mode.test.mjs +386 -0
  261. package/tests/server-conversation-voice.test.mjs +426 -0
  262. package/tests/server-extension-ui-requests.test.mjs +357 -0
  263. package/tests/server-git-generation.test.mjs +558 -0
  264. package/tests/server-git-mutations.test.mjs +729 -0
  265. package/tests/server-git-reads.test.mjs +381 -0
  266. package/tests/server-git-worktree-sessions.test.mjs +502 -0
  267. package/tests/server-helper-requests.test.mjs +364 -0
  268. package/tests/server-rpc-transports.test.mjs +553 -0
  269. package/tests/server-settings-store.test.mjs +132 -0
  270. package/tests/server-shell-rollout.test.mjs +478 -0
  271. package/tests/server-sse-delivery.test.mjs +410 -0
  272. package/tests/server-workspace-files.test.mjs +546 -0
  273. package/tests/session-attention-scheduler.test.mjs +514 -0
  274. package/tests/session-composer-store.test.mjs +153 -0
  275. package/tests/session-context.test.mjs +134 -0
  276. package/tests/session-extension-requests.test.mjs +193 -0
  277. package/tests/session-ownership-adapter.test.mjs +130 -0
  278. package/tests/session-sampling-ui-static.test.mjs +10 -4
  279. package/tests/session-send-lifetime.test.mjs +140 -0
  280. package/tests/session-shelf-state.test.mjs +348 -0
  281. package/tests/session-shelves-adapter.test.mjs +79 -0
  282. package/tests/session-summary-lifetime.test.mjs +54 -0
  283. package/tests/settings-resource-controller.test.mjs +548 -0
  284. package/tests/shell-action-adapter.test.mjs +169 -0
  285. package/tests/shell-actions.test.mjs +182 -0
  286. package/tests/shell-navigation.test.mjs +348 -0
  287. package/tests/shell-preferences.test.mjs +367 -0
  288. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  289. package/tests/stream-output-isolation-static.test.mjs +1 -1
  290. package/tests/subagent-gate-visibility.test.mjs +1 -1
  291. package/tests/subagent-launch-slot-state.test.mjs +2 -2
  292. package/tests/subagent-launch-slots-static.test.mjs +1 -1
  293. package/tests/subagent-observability-static.test.mjs +2 -2
  294. package/tests/subagent-reliability-integration.test.mjs +13 -30
  295. package/tests/syntax-highlighting-static.test.mjs +3 -3
  296. package/tests/tab-close-continuity-static.test.mjs +1 -1
  297. package/tests/tab-metadata-ordering.test.mjs +99 -0
  298. package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
  299. package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
  300. package/tests/terminal-tab-workspace-static.test.mjs +5 -4
  301. package/tests/terminal-tabs-resize-static.test.mjs +1 -1
  302. package/tests/theme-customizer-static.test.mjs +1 -1
  303. package/tests/thread-display-title.test.mjs +44 -0
  304. package/tests/update-api-harness.test.mjs +15 -14
  305. package/tests/update-tab-restore-harness.test.mjs +3 -0
  306. package/tests/webui-settings-locking.test.mjs +2 -2
  307. package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
  308. package/tests/workspace-choices.test.mjs +114 -0
  309. package/tests/workspace-save-load-static.test.mjs +7 -4
@@ -0,0 +1,132 @@
1
+ import assert from "node:assert/strict";
2
+ import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import path from "node:path";
5
+ import { setTimeout as delay } from "node:timers/promises";
6
+ import * as compatibility from "../lib/git-workflow-preferences.mjs";
7
+ import * as preferenceValues from "../lib/git-workflow-preference-values.mjs";
8
+ import * as store from "../lib/server/settings-store.mjs";
9
+
10
+ const storeExports = [
11
+ "WEBUI_SIDE_PANEL_WIDTH_MIN_PX",
12
+ "WEBUI_SIDE_PANEL_WIDTH_MAX_PX",
13
+ "webuiSettingsFile",
14
+ "legacyWebuiSettingsFile",
15
+ "normalizeResourceDefaults",
16
+ "normalizeInterfacePreferences",
17
+ "normalizeWebuiSettings",
18
+ "withWebuiSettingsLock",
19
+ "migrateLegacyWebuiSettings",
20
+ "validatedShellRolloutCohort",
21
+ "initializeWebuiShellRollout",
22
+ "readWebuiSettings",
23
+ "updateWebuiSettings",
24
+ "writeWebuiSettings",
25
+ ];
26
+ assert.deepEqual(Object.keys(store).sort(), [...storeExports].sort());
27
+ for (const name of storeExports) {
28
+ assert.equal(compatibility[name], store[name], `${name} must be the same binding at both import paths`);
29
+ }
30
+ assert.deepEqual(Object.keys(compatibility).sort(), [
31
+ ...storeExports,
32
+ ...Object.keys(preferenceValues),
33
+ "readGitWorkflowPreferences",
34
+ "writeGitWorkflowPreferences",
35
+ ].sort(), "the compatibility module must preserve the complete existing export surface");
36
+
37
+ const root = await mkdtemp(path.join(tmpdir(), "pi-webui-settings-store-"));
38
+ try {
39
+ const absentFile = path.join(root, "absent.json");
40
+ const defaults = await store.updateWebuiSettings(async () => undefined, absentFile);
41
+ assert.equal(defaults.version, 8);
42
+ assert.equal(defaults.uiLayout.version, 3);
43
+ await assert.rejects(readFile(absentFile), { code: "ENOENT" }, "an undefined updater must not create a settings file");
44
+ await assert.rejects(readdir(`${absentFile}.lock`), { code: "ENOENT" });
45
+
46
+ const settingsFile = path.join(root, "settings.json");
47
+ const rawSettings = '{ "version": 8, "retained": { "keep": true }, "counter": 0 }\n';
48
+ await writeFile(settingsFile, rawSettings, "utf8");
49
+ await compatibility.updateWebuiSettings(async (current) => {
50
+ assert.deepEqual(current.retained, { keep: true });
51
+ return undefined;
52
+ }, settingsFile);
53
+ assert.equal(await readFile(settingsFile, "utf8"), rawSettings, "a no-write update must leave even the original formatting untouched");
54
+
55
+ let releaseFirstUpdate;
56
+ let firstUpdateStarted;
57
+ const started = new Promise((resolve) => { firstUpdateStarted = resolve; });
58
+ const held = new Promise((resolve) => { releaseFirstUpdate = resolve; });
59
+ const firstUpdate = compatibility.updateWebuiSettings(async (current) => {
60
+ firstUpdateStarted();
61
+ await held;
62
+ return { counter: current.counter + 1 };
63
+ }, settingsFile);
64
+ await started;
65
+ let secondUpdateEntered = false;
66
+ const secondUpdate = store.updateWebuiSettings((current) => {
67
+ secondUpdateEntered = true;
68
+ assert.equal(current.counter, 1, "the new import must observe the awaited compatibility updater's write");
69
+ return { counter: current.counter + 1 };
70
+ }, path.join(root, ".", "settings.json"));
71
+ try {
72
+ await new Promise((resolve) => setImmediate(resolve));
73
+ assert.equal(secondUpdateEntered, false, "mixed imports must serialize on the same per-file queue");
74
+ assert.equal((await readdir(`${settingsFile}.lock`)).length, 1, "the queued import must not start a competing lock acquisition");
75
+ assert.equal(await readFile(settingsFile, "utf8"), rawSettings, "the store must await the updater before writing");
76
+ } finally {
77
+ releaseFirstUpdate();
78
+ await Promise.all([firstUpdate, secondUpdate]);
79
+ }
80
+ await Promise.all(Array.from({ length: 8 }, (_, index) => (
81
+ (index % 2 ? compatibility : store).updateWebuiSettings((current) => ({ counter: current.counter + 1 }), settingsFile)
82
+ )));
83
+ assert.equal((await store.readWebuiSettings(settingsFile)).counter, 10, "mixed-import updates must not lose concurrent increments");
84
+ await Promise.all([
85
+ compatibility.writeGitWorkflowPreferences({ generation: { provider: "fake", modelId: "fake-model" } }, settingsFile),
86
+ store.writeWebuiSettings({ interfacePreferences: { sidePanelWidth: 641 } }, settingsFile),
87
+ ]);
88
+ assert.equal((await store.readWebuiSettings(settingsFile)).gitWorkflow.generation.modelId, "fake-model");
89
+ assert.equal((await compatibility.readWebuiSettings(settingsFile)).interfacePreferences.sidePanelWidth, 641);
90
+
91
+ const beforeRejectedUpdate = await readFile(settingsFile, "utf8");
92
+ const updaterError = new Error("updater failed");
93
+ await assert.rejects(store.updateWebuiSettings(async () => { throw updaterError; }, settingsFile), (error) => error === updaterError);
94
+ assert.equal(await readFile(settingsFile, "utf8"), beforeRejectedUpdate, "a rejected updater must leave settings untouched");
95
+ await compatibility.writeWebuiSettings({ counter: 11 }, settingsFile);
96
+ const persisted = JSON.parse(await readFile(settingsFile, "utf8"));
97
+ assert.equal(persisted.counter, 11, "a rejected update must not poison the shared queue");
98
+ assert.deepEqual(persisted.retained, { keep: true });
99
+ assert.equal(Object.hasOwn(persisted, "subagentLaunchSlots"), false, "unrelated writes must not materialize launch-slot settings");
100
+
101
+ const futureLayout = { version: 99, futurePanel: { order: ["retain"] } };
102
+ await writeFile(settingsFile, `${JSON.stringify({ ...persisted, version: 99, uiLayout: futureLayout })}\n`, "utf8");
103
+ await store.writeWebuiSettings({ remoteAuthEnabled: true }, settingsFile);
104
+ const futurePersisted = JSON.parse(await readFile(settingsFile, "utf8"));
105
+ assert.equal(futurePersisted.version, 99);
106
+ assert.deepEqual(futurePersisted.uiLayout, futureLayout);
107
+ assert.deepEqual(futurePersisted.retained, { keep: true });
108
+ assert.equal((await compatibility.readWebuiSettings(settingsFile)).uiLayout.version, 3, "future layout data must still read through the safe current normalizer");
109
+
110
+ const targetFile = path.join(root, "migration-race.json");
111
+ const legacyFile = path.join(root, "legacy.json");
112
+ const winningSettings = '{"version":8,"retained":"new target wins"}\n';
113
+ await writeFile(legacyFile, '{"retained":"legacy"}\n', "utf8");
114
+ let migration;
115
+ await store.withWebuiSettingsLock(targetFile, async () => {
116
+ migration = store.migrateLegacyWebuiSettings(targetFile, legacyFile);
117
+ // Observe the migration's pending lock before creating the competing target.
118
+ const deadline = Date.now() + 1_000;
119
+ while ((await readdir(`${targetFile}.lock`)).length < 2) {
120
+ assert.ok(Date.now() < deadline, "migration must reach its locked absence recheck");
121
+ await delay(5);
122
+ }
123
+ await writeFile(targetFile, winningSettings, "utf8");
124
+ });
125
+ assert.equal(await migration, false, "migration must recheck target absence after acquiring the lock");
126
+ assert.equal(await readFile(targetFile, "utf8"), winningSettings);
127
+
128
+ assert.equal((await readdir(root)).some((name) => name.endsWith(".lock") || name.endsWith(".tmp")), false, "updates and migration must clean up their artifacts");
129
+ console.log("server-settings-store.test.mjs passed");
130
+ } finally {
131
+ await rm(root, { recursive: true, force: true });
132
+ }
@@ -0,0 +1,478 @@
1
+ import assert from "node:assert/strict";
2
+ import { spawn, spawnSync } from "node:child_process";
3
+ import { once } from "node:events";
4
+ import { lstat, mkdir, mkdtemp, readFile, readdir, rm, stat, symlink, writeFile } from "node:fs/promises";
5
+ import { tmpdir } from "node:os";
6
+ import path from "node:path";
7
+ import { setTimeout as delay } from "node:timers/promises";
8
+ import { pathToFileURL } from "node:url";
9
+ import { uiLayoutRevision } from "../lib/ui-layout-settings.mjs";
10
+ import {
11
+ initializeWebuiShellRollout,
12
+ readWebuiSettings,
13
+ updateWebuiSettings,
14
+ validatedShellRolloutCohort,
15
+ webuiSettingsFile,
16
+ } from "../lib/server/settings-store.mjs";
17
+
18
+ const sandbox = await mkdtemp(path.join(tmpdir(), "pi-webui-shell-rollout-"));
19
+ const moduleUrl = pathToFileURL(path.join(import.meta.dirname, "../lib/server/settings-store.mjs")).href;
20
+ const savedEnv = { ...process.env };
21
+ const children = new Set();
22
+ let completed = 0;
23
+
24
+ async function fixture(name) {
25
+ const home = path.join(sandbox, name);
26
+ await mkdir(home);
27
+ const env = {
28
+ ...savedEnv,
29
+ HOME: home,
30
+ XDG_CONFIG_HOME: path.join(home, "config"),
31
+ XDG_STATE_HOME: path.join(home, "state"),
32
+ PI_CODING_AGENT_DIR: path.join(home, "agent"),
33
+ };
34
+ delete env.PI_WEBUI_SETTINGS_FILE;
35
+ process.env.HOME = home;
36
+ process.env.XDG_CONFIG_HOME = env.XDG_CONFIG_HOME;
37
+ delete process.env.PI_WEBUI_SETTINGS_FILE;
38
+ return {
39
+ home, env,
40
+ root: path.join(home, ".pi/webui"),
41
+ target: path.join(home, ".pi/webui/settings.json"),
42
+ legacy: path.join(home, "config/pi-webui/settings.json"),
43
+ outer: path.join(home, ".pi/.webui-shell-rollout-init.lock"),
44
+ };
45
+ }
46
+
47
+ async function put(filename, value) {
48
+ await mkdir(path.dirname(filename), { recursive: true });
49
+ await writeFile(filename, typeof value === "string" ? value : JSON.stringify(value));
50
+ }
51
+ const raw = async (filename) => JSON.parse(await readFile(filename, "utf8"));
52
+ const absent = async (filename) => assert.rejects(lstat(filename), { code: "ENOENT" });
53
+
54
+ async function waitFor(check) {
55
+ const deadline = Date.now() + 5_000;
56
+ while (!await check()) {
57
+ assert.ok(Date.now() < deadline, "timed out waiting for filesystem checkpoint");
58
+ await delay(5);
59
+ }
60
+ }
61
+
62
+ // Faults and pauses are injected at filesystem boundaries in disposable children.
63
+ // Production code has no test hooks, alternate writers or configurable lock policy.
64
+ function child(f, { fault, pause, options = {}, action = "initialize", env = f.env } = {}) {
65
+ const source = `
66
+ import fs from "node:fs/promises";
67
+ import { syncBuiltinESMExports } from "node:module";
68
+ const root = ${JSON.stringify(f.root)};
69
+ const target = ${JSON.stringify(f.target)};
70
+ const fault = ${JSON.stringify(fault)};
71
+ const pause = ${JSON.stringify(pause)};
72
+ const original = { mkdir: fs.mkdir, rename: fs.rename, lstat: fs.lstat, readFile: fs.readFile };
73
+ let renamed = false;
74
+ const checkpoint = async () => {
75
+ process.send({ checkpoint: true });
76
+ await new Promise(resolve => process.once("message", resolve));
77
+ };
78
+ fs.mkdir = async (filename, options) => {
79
+ const result = await original.mkdir(filename, options);
80
+ if (filename === root && options?.recursive !== true) {
81
+ if (fault === "exclusive-race") throw Object.assign(new Error("lost mkdir race"), { code: "EEXIST" });
82
+ if (pause === "root") await checkpoint();
83
+ }
84
+ if (filename === target + ".lock" && pause === "settings-lock") await checkpoint();
85
+ return result;
86
+ };
87
+ fs.rename = async (source, destination) => {
88
+ if (destination === target && fault === "commit") throw Object.assign(new Error("commit denied"), { code: "EACCES" });
89
+ const result = await original.rename(source, destination);
90
+ if (destination === target) renamed = true;
91
+ return result;
92
+ };
93
+ fs.readFile = async (filename, ...args) => {
94
+ if (filename === target && renamed && fault === "readback") throw Object.assign(new Error("readback denied"), { code: "EACCES" });
95
+ return original.readFile(filename, ...args);
96
+ };
97
+ fs.lstat = async filename => {
98
+ if (filename === root && fault === "probe") throw Object.assign(new Error("probe denied"), { code: "EACCES" });
99
+ return original.lstat(filename);
100
+ };
101
+ syncBuiltinESMExports();
102
+ const store = await import(${JSON.stringify(moduleUrl)});
103
+ let result;
104
+ if (${JSON.stringify(action)} === "hold-outer") {
105
+ await store.withWebuiSettingsLock(${JSON.stringify(f.outer.slice(0, -5))}, checkpoint);
106
+ } else if (${JSON.stringify(action)} === "hold-settings") {
107
+ await store.withWebuiSettingsLock(target, checkpoint);
108
+ } else if (${JSON.stringify(action)} === "update") {
109
+ result = await store.updateWebuiSettings(() => ({ concurrent: { retained: true } }), target);
110
+ } else {
111
+ result = await store.initializeWebuiShellRollout(${JSON.stringify(options)});
112
+ }
113
+ process.send({ result: result ?? null });
114
+ process.disconnect();
115
+ `;
116
+ const processChild = spawn(process.execPath, ["--input-type=module", "-e", source], {
117
+ env, stdio: ["ignore", "pipe", "pipe", "ipc"],
118
+ });
119
+ children.add(processChild);
120
+ let stderr = "";
121
+ processChild.stderr.on("data", chunk => { stderr += chunk; });
122
+ const messages = [];
123
+ processChild.on("message", message => messages.push(message));
124
+ const done = once(processChild, "exit").then(([code, signal]) => {
125
+ children.delete(processChild);
126
+ return { code, signal, stderr, messages };
127
+ });
128
+ const watchdog = setTimeout(() => processChild.kill("SIGKILL"), 15_000);
129
+ done.then(() => clearTimeout(watchdog));
130
+ return {
131
+ process: processChild, done,
132
+ checkpoint: () => waitFor(() => messages.some(message => message.checkpoint)),
133
+ release: () => processChild.send("continue"),
134
+ result: async () => {
135
+ const outcome = await done;
136
+ assert.equal(outcome.code, 0, outcome.stderr);
137
+ return outcome.messages.findLast(message => Object.hasOwn(message, "result")).result;
138
+ },
139
+ };
140
+ }
141
+
142
+ async function check(name, run) {
143
+ await run();
144
+ completed += 1;
145
+ console.log(`ok ${completed} - ${name}`);
146
+ }
147
+
148
+ try {
149
+ await check("strict marker validation preserves extension compatibility", async () => {
150
+ for (const value of [undefined, null, [], "fresh", {}, { version: "1", cohort: "fresh" },
151
+ { version: 2, cohort: "fresh" }, { version: 1, cohort: "unknown" }]) {
152
+ assert.equal(validatedShellRolloutCohort(value), null);
153
+ }
154
+ for (const cohort of ["fresh", "established"]) {
155
+ assert.equal(validatedShellRolloutCohort({ version: 1, cohort, extra: true }), cohort);
156
+ }
157
+ });
158
+
159
+ await check("absent default root persists private raw-only fresh marker", async () => {
160
+ const f = await fixture("fresh");
161
+ assert.equal(webuiSettingsFile(f.env), f.target);
162
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: "fresh" });
163
+ assert.deepEqual(await raw(f.target), { shellRollout: { version: 1, cohort: "fresh" } });
164
+ assert.equal((await stat(f.root)).mode & 0o777, 0o700);
165
+ assert.equal((await stat(f.target)).mode & 0o777, 0o600);
166
+ const bytes = await readFile(f.target, "utf8");
167
+ await put(f.legacy, "malformed late legacy");
168
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: "fresh" });
169
+ assert.equal(await readFile(f.target, "utf8"), bytes, "valid marker wins without a write");
170
+ assert.equal((await readWebuiSettings()).version, 8);
171
+ assert.equal((await readWebuiSettings()).uiLayout.version, 3);
172
+ await absent(f.outer);
173
+ assert.deepEqual(await readdir(f.root), ["settings.json"]);
174
+ });
175
+
176
+ await check("existing root and prior-use evidence force established", async () => {
177
+ for (const kind of ["empty-root", "file", "directory", "dangling-link"]) {
178
+ const f = await fixture(`evidence-${kind}`);
179
+ const evidence = path.join(f.home, "prior-use");
180
+ if (kind === "empty-root") await mkdir(f.root, { recursive: true });
181
+ if (kind === "file") await put(evidence, "previous use");
182
+ if (kind === "directory") await mkdir(evidence);
183
+ if (kind === "dangling-link") await symlink(path.join(f.home, "missing"), evidence);
184
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env, additionalEvidencePaths: [evidence] }), { cohort: "established" });
185
+ }
186
+ });
187
+
188
+ await check("custom target defaults resolve supplied env and never prove fresh", async () => {
189
+ const f = await fixture("custom");
190
+ const target = path.join(f.home, "custom/settings.json");
191
+ const env = { ...f.env, PI_WEBUI_SETTINGS_FILE: target, XDG_CONFIG_HOME: path.join(f.home, "other-config") };
192
+ await put(path.join(env.XDG_CONFIG_HOME, "pi-webui/settings.json"), { mustNotImport: true });
193
+ assert.deepEqual(await initializeWebuiShellRollout({ env }), { cohort: "established" });
194
+ assert.deepEqual(await raw(target), { shellRollout: { version: 1, cohort: "established" } });
195
+ await absent(f.root);
196
+ const explicit = path.join(f.home, "explicit/settings.json");
197
+ assert.deepEqual(await initializeWebuiShellRollout({ storageFile: explicit, env: f.env }), { cohort: "established" });
198
+ await put(explicit, { shellRollout: { version: 1, cohort: "fresh", extension: 7 } });
199
+ assert.deepEqual(await initializeWebuiShellRollout({ storageFile: explicit, env }), { cohort: "fresh" });
200
+ const sameDefault = await fixture("overridden-default");
201
+ assert.deepEqual(await initializeWebuiShellRollout({ env: { ...sameDefault.env, PI_WEBUI_SETTINGS_FILE: sameDefault.target } }), { cohort: "established" });
202
+ });
203
+
204
+ await check("invalid, future and null own markers survive init, reads and unrelated writes", async () => {
205
+ const f = await fixture("markers");
206
+ for (const marker of [null, false, 0, "fresh", {}, [], { version: 2, cohort: "fresh" }, { version: 1, cohort: "bad" }]) {
207
+ await put(f.target, { version: 8, shellRollout: marker, retained: true });
208
+ const bytes = await readFile(f.target, "utf8");
209
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: null });
210
+ await readWebuiSettings();
211
+ assert.equal(await readFile(f.target, "utf8"), bytes);
212
+ await updateWebuiSettings(() => ({ unrelated: true }));
213
+ assert.deepEqual((await raw(f.target)).shellRollout, marker);
214
+ }
215
+ });
216
+
217
+ await check("marker-only initialization preserves raw versions, layouts and layout revisions", async () => {
218
+ const f = await fixture("raw-layout");
219
+ for (const version of [1, 2, 3, 99]) {
220
+ const settings = {
221
+ version, retained: { nested: [1, null, "keep"] },
222
+ uiLayout: { version, sidePanel: { placement: "left", panelWidths: { left: 444, right: 555 } }, future: ["retain"] },
223
+ };
224
+ await put(f.target, settings);
225
+ const before = uiLayoutRevision((await readWebuiSettings()).uiLayout);
226
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: "established" });
227
+ assert.deepEqual(await raw(f.target), { ...settings, shellRollout: { version: 1, cohort: "established" } });
228
+ assert.equal(uiLayoutRevision((await readWebuiSettings()).uiLayout), before);
229
+ }
230
+ });
231
+
232
+ await check("legacy migrates before marker and retains data and future layout", async () => {
233
+ const f = await fixture("legacy");
234
+ const legacy = { version: 99, retained: { legacy: true }, uiLayout: { version: 99, future: ["keep"] } };
235
+ await put(f.legacy, legacy);
236
+ const bytes = await readFile(f.legacy, "utf8");
237
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: "established" });
238
+ assert.deepEqual((await raw(f.target)).retained, legacy.retained);
239
+ assert.deepEqual((await raw(f.target)).uiLayout, legacy.uiLayout);
240
+ assert.equal((await raw(f.target)).version, 99);
241
+ assert.equal(await readFile(f.legacy, "utf8"), bytes);
242
+ assert.equal(Object.hasOwn(await raw(f.target), "subagentLaunchSlots"), false);
243
+ });
244
+
245
+ await check("malformed legacy and malformed target are not masked", async () => {
246
+ for (const content of ["{", "[]", "null"]) {
247
+ const f = await fixture(`malformed-${completed}-${content.length}-${content.charCodeAt(0)}`);
248
+ await put(f.legacy, content);
249
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: null });
250
+ await absent(f.target);
251
+ assert.equal(await readFile(f.legacy, "utf8"), content);
252
+ }
253
+ const f = await fixture("malformed-target");
254
+ await put(f.target, "{invalid");
255
+ await put(f.legacy, { legacy: true });
256
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: null });
257
+ assert.equal(await readFile(f.target, "utf8"), "{invalid");
258
+ });
259
+
260
+ await check("ordinary reads and undefined updates never assign a cohort", async () => {
261
+ const f = await fixture("reads");
262
+ await readWebuiSettings();
263
+ await absent(f.root);
264
+ await updateWebuiSettings(() => undefined);
265
+ await absent(f.target);
266
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: "established" }, "undefined update created the root, so freshness is lost");
267
+ const migrated = await fixture("read-migration");
268
+ await put(migrated.legacy, { retained: "legacy read" });
269
+ assert.equal((await readWebuiSettings()).retained, "legacy read");
270
+ assert.equal(Object.hasOwn(await raw(migrated.target), "shellRollout"), false);
271
+ });
272
+
273
+ await check("ambiguous paths and probe errors cannot prove freshness", async () => {
274
+ const f = await fixture("probe-failure");
275
+ assert.deepEqual(await child(f, { fault: "probe" }).result(), { cohort: null });
276
+ await absent(f.root);
277
+ for (const kind of ["file", "symlink", "dangling"]) {
278
+ const state = await fixture(`root-${kind}`);
279
+ await mkdir(path.dirname(state.root));
280
+ if (kind === "file") await writeFile(state.root, "not a directory");
281
+ else {
282
+ const destination = path.join(state.home, "elsewhere");
283
+ if (kind === "symlink") await mkdir(destination);
284
+ await symlink(destination, state.root);
285
+ }
286
+ assert.deepEqual(await initializeWebuiShellRollout({ env: state.env }), { cohort: null });
287
+ }
288
+ const badEvidence = await fixture("evidence-error");
289
+ await put(path.join(badEvidence.home, "file"), "not a directory");
290
+ assert.deepEqual(await initializeWebuiShellRollout({ env: badEvidence.env, additionalEvidencePaths: [path.join(badEvidence.home, "file/child")] }), { cohort: null });
291
+ await absent(badEvidence.root);
292
+ });
293
+
294
+ await check("exclusive mkdir EEXIST loses freshness", async () => {
295
+ const f = await fixture("exclusive-race");
296
+ assert.deepEqual(await child(f, { fault: "exclusive-race" }).result(), { cohort: "established" });
297
+ });
298
+
299
+ await check("target path types are conservative and readable valid markers still win", async () => {
300
+ const directory = await fixture("target-directory");
301
+ await mkdir(directory.target, { recursive: true });
302
+ assert.deepEqual(await initializeWebuiShellRollout({ env: directory.env }), { cohort: null });
303
+ const linked = await fixture("target-link");
304
+ const destination = path.join(linked.home, "settings-source.json");
305
+ await put(destination, { shellRollout: { version: 1, cohort: "fresh", extra: true } });
306
+ await mkdir(linked.root, { recursive: true });
307
+ await symlink(destination, linked.target);
308
+ const bytes = await readFile(destination, "utf8");
309
+ assert.deepEqual(await initializeWebuiShellRollout({ env: linked.env }), { cohort: "fresh" });
310
+ assert.equal(await readFile(destination, "utf8"), bytes);
311
+ if (process.platform === "linux") {
312
+ const fifo = await fixture("target-fifo");
313
+ await mkdir(fifo.root, { recursive: true });
314
+ const made = spawnSync("mkfifo", [fifo.target], { encoding: "utf8", timeout: 2_000 });
315
+ assert.equal(made.status, 0, made.stderr || made.error?.message);
316
+ assert.deepEqual(await child(fifo).result(), { cohort: null });
317
+ assert.equal((await lstat(fifo.target)).isFIFO(), true, "special target must remain untouched");
318
+ }
319
+ });
320
+
321
+ await check("failed commits lose freshness; failed readback returns null", async () => {
322
+ for (const fault of ["commit", "readback"]) {
323
+ const f = await fixture(`failure-${fault}`);
324
+ assert.deepEqual(await child(f, { fault }).result(), { cohort: null });
325
+ assert.equal((await stat(f.root)).isDirectory(), true);
326
+ assert.equal((await readdir(f.root)).some(name => name.endsWith(".tmp") || name.endsWith(".lock")), false);
327
+ await absent(f.outer);
328
+ // Readback failure can leave a successfully committed fresh marker.
329
+ const expected = fault === "commit" ? "established" : "fresh";
330
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: expected });
331
+ }
332
+ });
333
+
334
+ await check("failed marker commit preserves raw settings and failed migration preserves legacy", async () => {
335
+ const f = await fixture("existing-commit-failure");
336
+ await put(f.target, '{ "version": 99, "uiLayout": {"version":99}, "retained":true }');
337
+ const before = await readFile(f.target, "utf8");
338
+ assert.deepEqual(await child(f, { fault: "commit" }).result(), { cohort: null });
339
+ assert.equal(await readFile(f.target, "utf8"), before);
340
+ const legacy = await fixture("migration-commit-failure");
341
+ await put(legacy.legacy, { retained: "legacy" });
342
+ const source = await readFile(legacy.legacy, "utf8");
343
+ assert.deepEqual(await child(legacy, { fault: "commit" }).result(), { cohort: null });
344
+ await absent(legacy.target);
345
+ assert.equal(await readFile(legacy.legacy, "utf8"), source);
346
+ assert.deepEqual(await readdir(legacy.root), []);
347
+ await absent(legacy.outer);
348
+ assert.deepEqual(await initializeWebuiShellRollout({ env: legacy.env }), { cohort: "established" });
349
+ assert.equal((await raw(legacy.target)).retained, "legacy");
350
+ });
351
+
352
+ await check("two processes serialize absent-root initialization while first is paused", async () => {
353
+ const f = await fixture("concurrent-starts");
354
+ const first = child(f, { pause: "root" });
355
+ await first.checkpoint();
356
+ const second = child(f);
357
+ await waitFor(async () => (await readdir(f.outer)).filter(name => name.endsWith(".json")).length === 2);
358
+ await absent(f.target);
359
+ first.release();
360
+ assert.deepEqual(await first.result(), { cohort: "fresh" });
361
+ assert.deepEqual(await second.result(), { cohort: "fresh" });
362
+ assert.deepEqual(await raw(f.target), { shellRollout: { version: 1, cohort: "fresh" } });
363
+ await absent(f.outer);
364
+ });
365
+
366
+ await check("canonical parent aliases share the same outer lock", async () => {
367
+ const f = await fixture("alias");
368
+ const aliasHome = `${f.home}-alias`;
369
+ await symlink(f.home, aliasHome);
370
+ const first = child(f, { pause: "root" });
371
+ await first.checkpoint();
372
+ const second = child(f, { env: { ...f.env, HOME: aliasHome } });
373
+ await waitFor(async () => (await readdir(f.outer)).length === 2);
374
+ first.release();
375
+ assert.deepEqual(await first.result(), { cohort: "fresh" });
376
+ assert.deepEqual(await second.result(), { cohort: "fresh" });
377
+ });
378
+
379
+ await check("concurrent ordinary update retains patch and forces established", async () => {
380
+ const f = await fixture("concurrent-update");
381
+ const first = child(f, { pause: "root" });
382
+ await first.checkpoint();
383
+ await child(f, { action: "update" }).result();
384
+ first.release();
385
+ assert.deepEqual(await first.result(), { cohort: "established" });
386
+ assert.deepEqual((await raw(f.target)).concurrent, { retained: true });
387
+ await updateWebuiSettings(() => ({ after: true }));
388
+ assert.equal((await raw(f.target)).shellRollout.cohort, "established");
389
+ });
390
+
391
+ await check("raw lock recheck preserves a concurrently appeared marker or unmarked file", async () => {
392
+ for (const marker of [undefined, null, { version: 1, cohort: "fresh", extra: true }]) {
393
+ const f = await fixture(`raw-race-${completed}-${String(marker)}`);
394
+ const pending = child(f, { pause: "settings-lock" });
395
+ await pending.checkpoint();
396
+ const settings = { retained: "winner", ...(marker === undefined ? {} : { shellRollout: marker }) };
397
+ await put(f.target, settings);
398
+ pending.release();
399
+ assert.deepEqual(await pending.result(), { cohort: marker === undefined ? "established" : validatedShellRolloutCohort(marker) });
400
+ assert.deepEqual(await raw(f.target), marker === undefined
401
+ ? { ...settings, shellRollout: { version: 1, cohort: "established" } } : settings);
402
+ }
403
+ });
404
+
405
+ await check("crash after root creation recovers dead lock and stays established", async () => {
406
+ const f = await fixture("crash");
407
+ const first = child(f, { pause: "root" });
408
+ await first.checkpoint();
409
+ first.process.kill("SIGKILL");
410
+ assert.equal((await first.done).signal, "SIGKILL");
411
+ await absent(f.target);
412
+ assert.deepEqual(await child(f).result(), { cohort: "established" });
413
+ await absent(f.outer);
414
+ assert.deepEqual(await readdir(f.root), ["settings.json"]);
415
+ });
416
+
417
+ await check("late evidence loses freshness and late legacy waits for migration", async () => {
418
+ for (const kind of ["evidence", "legacy"]) {
419
+ const f = await fixture(`late-${kind}`);
420
+ const evidence = path.join(f.home, "prior-use");
421
+ const pending = child(f, { pause: "root", options: { additionalEvidencePaths: [evidence] } });
422
+ await pending.checkpoint();
423
+ await put(kind === "legacy" ? f.legacy : evidence, kind === "legacy" ? { imported: true } : "prior use");
424
+ pending.release();
425
+ assert.deepEqual(await pending.result(), { cohort: kind === "legacy" ? null : "established" });
426
+ if (kind === "legacy") {
427
+ await absent(f.target);
428
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: "established" });
429
+ assert.equal((await raw(f.target)).imported, true);
430
+ }
431
+ }
432
+ });
433
+
434
+ await check("explicit legacy path is migrated before marker persistence", async () => {
435
+ const f = await fixture("explicit-legacy");
436
+ const source = path.join(f.home, "selected-legacy.json");
437
+ await put(source, { selectedSource: true });
438
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env, legacyFile: source }), { cohort: "established" });
439
+ assert.equal((await raw(f.target)).selectedSource, true);
440
+ await absent(f.legacy);
441
+ });
442
+
443
+ await check("live outer lock times out without creating the root", async () => {
444
+ const f = await fixture("timeout");
445
+ const holder = child(f, { action: "hold-outer" });
446
+ await holder.checkpoint();
447
+ const started = Date.now();
448
+ assert.deepEqual(await child(f).result(), { cohort: null });
449
+ assert.ok(Date.now() - started < 5_000, "initialization must have a bounded wait");
450
+ await absent(f.root);
451
+ assert.equal((await readdir(f.outer)).length, 1, "timeout cleans only its own record");
452
+ holder.release();
453
+ await holder.result();
454
+ await absent(f.outer);
455
+ });
456
+
457
+ await check("ordinary settings lock timeout returns null and cleans outer ownership", async () => {
458
+ const f = await fixture("settings-timeout");
459
+ const holder = child(f, { action: "hold-settings" });
460
+ await holder.checkpoint();
461
+ assert.deepEqual(await child(f).result(), { cohort: null });
462
+ await absent(f.target);
463
+ await absent(f.outer);
464
+ assert.equal((await readdir(`${f.target}.lock`)).length, 1);
465
+ holder.release();
466
+ await holder.result();
467
+ assert.deepEqual(await initializeWebuiShellRollout({ env: f.env }), { cohort: "established" });
468
+ });
469
+
470
+ console.log(`server-shell-rollout.test.mjs passed (${completed} groups)`);
471
+ } finally {
472
+ for (const processChild of children) processChild.kill("SIGKILL");
473
+ for (const key of ["HOME", "XDG_CONFIG_HOME", "PI_WEBUI_SETTINGS_FILE"]) {
474
+ if (savedEnv[key] === undefined) delete process.env[key];
475
+ else process.env[key] = savedEnv[key];
476
+ }
477
+ await rm(sandbox, { recursive: true, force: true });
478
+ }