claude-code-session-manager 0.97.0 → 0.100.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (430) hide show
  1. package/README.md +3 -3
  2. package/bin/cli.cjs +16 -4
  3. package/bin/node-floor.cjs +41 -0
  4. package/dist/assets/{DataModel-D93FoaOm.js → DataModel-HdR0AxFq.js} +1 -1
  5. package/dist/assets/{History-CXxxqNP1.js → History-Dr4Jr_5z.js} +2 -2
  6. package/dist/assets/{Hooks-CzeYFQjq.js → Hooks-NhrvH3JP.js} +3 -3
  7. package/dist/assets/{Library-D2zPkwOC.js → Library-Dv_EtIMr.js} +1 -1
  8. package/dist/assets/{MarkdownEditor-DbinP78A.js → MarkdownEditor-DTKDWV_n.js} +1 -1
  9. package/dist/assets/{McpServers-Bu_sVGLr.js → McpServers-M034G9B3.js} +2 -2
  10. package/dist/assets/{Memory-BgiI6LZ_.js → Memory-DzF1B4gx.js} +6 -6
  11. package/dist/assets/{Permissions-C1MMzZRm.js → Permissions-DrgbSGWl.js} +3 -3
  12. package/dist/assets/{Plugins-Dhu6wAZi.js → Plugins-Bd3ef35w.js} +2 -2
  13. package/dist/assets/{ProvenanceBadge-C3AcWKMG.js → ProvenanceBadge-DcBHT1Iy.js} +1 -1
  14. package/dist/assets/{SaveBar-plwYm0iO.js → SaveBar-rimJem-Y.js} +1 -1
  15. package/dist/assets/Scheduler-DjgFJl2w.js +16 -0
  16. package/dist/assets/{ScopeSwitcher-DFy0vv_o.js → ScopeSwitcher-CnAzTBrl.js} +1 -1
  17. package/dist/assets/Settings-Dc1F_sSg.js +3 -0
  18. package/dist/assets/{SkillReferenceGraph-DsI7hRPB.js → SkillReferenceGraph-B9aZDecM.js} +1 -1
  19. package/dist/assets/{Skills-3E748UfB.js → Skills-DRXd18gu.js} +2 -2
  20. package/dist/assets/{SystemPrompt-BqmfUJOd.js → SystemPrompt-ahy5-Fwe.js} +1 -1
  21. package/dist/assets/{TagLibrary-CPz6fO8Q.js → TagLibrary-C_tToDeL.js} +1 -1
  22. package/dist/assets/{TiptapBody-D9EoBQ1P.js → TiptapBody-DvdefS6K.js} +1 -1
  23. package/dist/assets/{Toggle-BHAmZNDn.js → Toggle-6Jl5Njc6.js} +1 -1
  24. package/dist/assets/{index-CKH5Uxik.css → index-Bta-hwud.css} +1 -1
  25. package/dist/assets/{index-tn5JIVPj.js → index-vx8O73l8.js} +497 -502
  26. package/dist/assets/{settingsSchema-Crqy_aNz.js → settingsSchema-D6iRguNV.js} +1 -1
  27. package/dist/index.html +2 -2
  28. package/package.json +26 -29
  29. package/plugins/CLAUDE.md +6 -6
  30. package/plugins/session-manager-dev/skills/builder/4-manual/SKILL.md +23 -19
  31. package/plugins/session-manager-dev/skills/builder/SKILL.md +3 -3
  32. package/plugins/session-manager-dev/skills/develop/SKILL.md +254 -510
  33. package/plugins/session-manager-dev/skills/develop/standards.md +17 -22
  34. package/scripts/README.md +3 -11
  35. package/scripts/audit-ops-hygiene.cjs +3 -3
  36. package/scripts/hooks/lib/guard-prd-writes-policy.cjs +1 -1
  37. package/scripts/hooks/lib/guard-self-schedule-policy.cjs +1 -1
  38. package/scripts/mint-epic.cjs +2 -3
  39. package/scripts/scheduler-mcp-server.cjs +36 -7
  40. package/src/main/agentLibrary.cjs +22 -2
  41. package/src/main/build-info.json +4 -4
  42. package/src/main/config.cjs +41 -38
  43. package/src/main/docEdit.cjs +4 -1
  44. package/src/main/files.cjs +2 -5
  45. package/src/main/health.cjs +1 -1
  46. package/src/main/index.cjs +23 -10
  47. package/src/main/ipcSchemas.cjs +28 -35
  48. package/src/main/lib/agentPersonaSchema.cjs +13 -2
  49. package/src/main/lib/atomicFs.cjs +116 -0
  50. package/src/main/lib/branchSweep.cjs +13 -12
  51. package/src/main/lib/buildTarget.cjs +3 -3
  52. package/src/main/lib/claudeCliCaps.cjs +129 -0
  53. package/src/main/lib/credentials.cjs +2 -4
  54. package/src/main/lib/crossProjectFeedback.cjs +3 -3
  55. package/src/main/lib/cwdClassify.cjs +15 -3
  56. package/src/main/lib/definitionOfDone.cjs +207 -125
  57. package/src/main/lib/dodDrainHook.cjs +11 -5
  58. package/src/main/lib/epicMint.cjs +2 -4
  59. package/src/main/lib/epicStatusMirror.cjs +8 -10
  60. package/src/main/lib/epicWorktreeProjectConfig.cjs +2 -4
  61. package/src/main/lib/gateAuthority.cjs +96 -0
  62. package/src/main/lib/gitExec.cjs +69 -0
  63. package/src/main/lib/gitWorktree.cjs +656 -92
  64. package/src/main/lib/instanceLock.cjs +5 -8
  65. package/src/main/lib/launchFailure.cjs +2 -3
  66. package/src/main/lib/macroLibrary.cjs +386 -0
  67. package/src/main/lib/mcpToolCatalog.cjs +26 -17
  68. package/src/main/lib/needsReviewLedger.cjs +1 -1
  69. package/src/main/lib/opsErrorLog.cjs +11 -1
  70. package/src/main/lib/opsOwnership.cjs +0 -5
  71. package/src/main/lib/pidAlive.cjs +32 -0
  72. package/src/main/lib/prdCreate.cjs +153 -19
  73. package/src/main/lib/prdDisposition.cjs +2 -2
  74. package/src/main/lib/prdGateFiles.cjs +284 -0
  75. package/src/main/lib/prdLocations.cjs +61 -0
  76. package/src/main/lib/prdMigration.cjs +2 -3
  77. package/src/main/lib/prdSizing.cjs +2 -1
  78. package/src/main/lib/promptSessionSchema.cjs +10 -2
  79. package/src/main/lib/rcaReport.cjs +4 -6
  80. package/src/main/lib/reaperHelpers.cjs +8 -1
  81. package/src/main/lib/reservationExpiry.cjs +6 -1
  82. package/src/main/lib/reviewNotice.cjs +263 -0
  83. package/src/main/lib/runClaudeP.cjs +4 -1
  84. package/src/main/lib/schedulerPaths.cjs +22 -2
  85. package/src/main/lib/sessionSlots.cjs +2 -4
  86. package/src/main/lib/shippedPersonaSeeds.cjs +179 -0
  87. package/src/main/lib/timeoutShim.cjs +123 -0
  88. package/src/main/lib/timeoutShimScript.cjs +275 -0
  89. package/src/main/lib/upgradeDrain.cjs +4 -10
  90. package/src/main/lib/watchdogHelpers.cjs +33 -30
  91. package/src/main/lib/workTypeLibrary.cjs +7 -1
  92. package/src/main/promptSessionEvents.cjs +37 -13
  93. package/src/main/queueOps.cjs +17 -6
  94. package/src/main/scheduler.cjs +975 -203
  95. package/src/main/seedAgentPersonas.cjs +278 -23
  96. package/src/main/supervisor.cjs +4 -1
  97. package/src/main/templates/PRD_AUTHORING.md +126 -406
  98. package/src/preload/__tests__/preload-surface.test.cjs +68 -0
  99. package/src/preload/api.d.ts +41 -94
  100. package/src/preload/index.cjs +14 -9
  101. package/src/seed/agents/architect.md +1 -0
  102. package/src/seed/agents/dev-lead.md +17 -18
  103. package/src/seed/agents/project-home-builder.md +1 -0
  104. package/src/seed/agents/validator.md +10 -4
  105. package/dist/assets/HostBilko-nESrTRIg.js +0 -1
  106. package/dist/assets/Scheduler-BVYTeh39.js +0 -16
  107. package/dist/assets/Settings-DIhGgdfs.js +0 -3
  108. package/src/main/__tests__/activeIndexMerge.test.cjs +0 -235
  109. package/src/main/__tests__/agentEffortResolve.test.cjs +0 -117
  110. package/src/main/__tests__/agentLibrary.test.cjs +0 -276
  111. package/src/main/__tests__/agentModelResolve.test.cjs +0 -311
  112. package/src/main/__tests__/agentOverlayWrite.test.cjs +0 -95
  113. package/src/main/__tests__/bilkoHost-deriveSlug.test.cjs +0 -26
  114. package/src/main/__tests__/bilkoHost-integration.test.cjs +0 -118
  115. package/src/main/__tests__/bilkoHostCore.test.cjs +0 -72
  116. package/src/main/__tests__/broadcastCoalescer.test.cjs +0 -122
  117. package/src/main/__tests__/chat-cancel-terminal.test.cjs +0 -120
  118. package/src/main/__tests__/chat-dead-channels.test.cjs +0 -63
  119. package/src/main/__tests__/chat-exit-close-race.test.cjs +0 -146
  120. package/src/main/__tests__/chat-mcp-consent-notice.test.cjs +0 -139
  121. package/src/main/__tests__/chat-preamble-anchors.test.cjs +0 -100
  122. package/src/main/__tests__/chat-queue.test.cjs +0 -97
  123. package/src/main/__tests__/chat-stop-signal.test.cjs +0 -89
  124. package/src/main/__tests__/chatRunner-epic-worktree-execcwd.test.cjs +0 -125
  125. package/src/main/__tests__/chatRunner-session-flag-retry.test.cjs +0 -252
  126. package/src/main/__tests__/classifyPromptTicket.test.cjs +0 -101
  127. package/src/main/__tests__/classifyTranscriptLine.test.cjs +0 -201
  128. package/src/main/__tests__/computeDepHistorySatisfaction.test.cjs +0 -66
  129. package/src/main/__tests__/config-readText-bounded.test.cjs +0 -84
  130. package/src/main/__tests__/configWriteBoundaryOwners.test.cjs +0 -59
  131. package/src/main/__tests__/crossProjectFeedback.test.cjs +0 -334
  132. package/src/main/__tests__/crossProjectFeedbackRoutes.test.cjs +0 -161
  133. package/src/main/__tests__/dep-orphan-archive-health.test.cjs +0 -77
  134. package/src/main/__tests__/develop-skill-failure-modes.test.cjs +0 -70
  135. package/src/main/__tests__/docEdit.test.cjs +0 -244
  136. package/src/main/__tests__/dod-batchkey.test.cjs +0 -183
  137. package/src/main/__tests__/dod-drain-hook.test.cjs +0 -302
  138. package/src/main/__tests__/dod-report.test.cjs +0 -304
  139. package/src/main/__tests__/dod-reverify.test.cjs +0 -285
  140. package/src/main/__tests__/epicContextDigest.test.cjs +0 -174
  141. package/src/main/__tests__/epicMint.test.cjs +0 -332
  142. package/src/main/__tests__/epicMintTelemetryTap.test.cjs +0 -64
  143. package/src/main/__tests__/epicStatusMirror.test.cjs +0 -110
  144. package/src/main/__tests__/epicValidationHook.test.cjs +0 -291
  145. package/src/main/__tests__/exchanges.test.cjs +0 -122
  146. package/src/main/__tests__/exchangesPromptId.test.cjs +0 -61
  147. package/src/main/__tests__/extractJson.test.cjs +0 -51
  148. package/src/main/__tests__/files-reject-credentials.test.cjs +0 -40
  149. package/src/main/__tests__/fixtures/1218-fo-01-move-scripts-lib-into-src-main-lib.log +0 -556
  150. package/src/main/__tests__/flatPrdTickSweep.test.cjs +0 -110
  151. package/src/main/__tests__/health-build-freshness.test.cjs +0 -39
  152. package/src/main/__tests__/health-claude-md-budget.test.cjs +0 -57
  153. package/src/main/__tests__/health-credentials.test.cjs +0 -81
  154. package/src/main/__tests__/health-delegation-chain.test.cjs +0 -124
  155. package/src/main/__tests__/health-per-project-stall.test.cjs +0 -84
  156. package/src/main/__tests__/health-prd-migration.test.cjs +0 -37
  157. package/src/main/__tests__/health-queue-dispatch.test.cjs +0 -135
  158. package/src/main/__tests__/health-starve-escalation.test.cjs +0 -94
  159. package/src/main/__tests__/health-tick-liveness.test.cjs +0 -179
  160. package/src/main/__tests__/health-usage-poller.test.cjs +0 -144
  161. package/src/main/__tests__/health-worktree-cap-blocked.test.cjs +0 -65
  162. package/src/main/__tests__/heapSnapshot.test.cjs +0 -121
  163. package/src/main/__tests__/historyAggregatorIntraday.test.cjs +0 -313
  164. package/src/main/__tests__/historyDashboard.test.cjs +0 -163
  165. package/src/main/__tests__/historyRollup.test.cjs +0 -333
  166. package/src/main/__tests__/intradayRefresh.test.cjs +0 -39
  167. package/src/main/__tests__/ipcSchemas-dependsOn.test.cjs +0 -39
  168. package/src/main/__tests__/kg-augment.test.cjs +0 -195
  169. package/src/main/__tests__/loadGateDetailTick.test.cjs +0 -31
  170. package/src/main/__tests__/machineProfile.test.cjs +0 -152
  171. package/src/main/__tests__/mcpStatus.test.cjs +0 -61
  172. package/src/main/__tests__/memoryAggregate.test.cjs +0 -109
  173. package/src/main/__tests__/memoryStale.test.cjs +0 -88
  174. package/src/main/__tests__/needsReviewLedger.test.cjs +0 -162
  175. package/src/main/__tests__/openExternalApp-spawn-error.test.cjs +0 -25
  176. package/src/main/__tests__/opsErrorLog.test.cjs +0 -109
  177. package/src/main/__tests__/opsErrorLogTelemetryTap.test.cjs +0 -173
  178. package/src/main/__tests__/personaMerge.test.cjs +0 -169
  179. package/src/main/__tests__/planValidator.test.cjs +0 -125
  180. package/src/main/__tests__/pollLoop-dispatch-on-failure.test.cjs +0 -176
  181. package/src/main/__tests__/prd-group-allocator.test.cjs +0 -119
  182. package/src/main/__tests__/prdAdminRouteParity.test.cjs +0 -70
  183. package/src/main/__tests__/prdAdminRoutes.test.cjs +0 -718
  184. package/src/main/__tests__/prdAgentType.test.cjs +0 -103
  185. package/src/main/__tests__/prdAuthoringSeed.test.cjs +0 -39
  186. package/src/main/__tests__/prdCreate.test.cjs +0 -979
  187. package/src/main/__tests__/prdCreateDisposition.test.cjs +0 -201
  188. package/src/main/__tests__/prdCreatePlanId.test.cjs +0 -132
  189. package/src/main/__tests__/prdFrontmatterAgentType.test.cjs +0 -117
  190. package/src/main/__tests__/prdFrontmatterDependsOn.test.cjs +0 -136
  191. package/src/main/__tests__/prdFrontmatterDisposition.test.cjs +0 -125
  192. package/src/main/__tests__/prdFrontmatterQuietMachine.test.cjs +0 -108
  193. package/src/main/__tests__/prdLocations.test.cjs +0 -195
  194. package/src/main/__tests__/prdLocationsArchived.test.cjs +0 -201
  195. package/src/main/__tests__/prdMigration.test.cjs +0 -349
  196. package/src/main/__tests__/prdMigrationLegacyAdopt.test.cjs +0 -91
  197. package/src/main/__tests__/prdParserHighWater.test.cjs +0 -74
  198. package/src/main/__tests__/prdParserSourcePromptId.test.cjs +0 -65
  199. package/src/main/__tests__/prdSetDisposition.test.cjs +0 -222
  200. package/src/main/__tests__/prdSizing.test.cjs +0 -106
  201. package/src/main/__tests__/prdSourcePromptIdBackfill.test.cjs +0 -118
  202. package/src/main/__tests__/prdUpdateDependsOn.test.cjs +0 -160
  203. package/src/main/__tests__/proc-role-env.test.cjs +0 -125
  204. package/src/main/__tests__/procname-claude-spawn-sites.test.cjs +0 -304
  205. package/src/main/__tests__/procname-sm-processes.test.cjs +0 -127
  206. package/src/main/__tests__/projectHomeAdminRoutes.test.cjs +0 -177
  207. package/src/main/__tests__/projectPages.test.cjs +0 -137
  208. package/src/main/__tests__/promptSessionEvents.test.cjs +0 -234
  209. package/src/main/__tests__/promptSessionSchema.test.cjs +0 -101
  210. package/src/main/__tests__/promptSessionTranscript.test.cjs +0 -0
  211. package/src/main/__tests__/promptSessionsCreateEpicHandler.test.cjs +0 -159
  212. package/src/main/__tests__/pty-epic-worktree-spawn-cwd.test.cjs +0 -282
  213. package/src/main/__tests__/pty-session-open-telemetry.test.cjs +0 -96
  214. package/src/main/__tests__/pty-write-result.test.cjs +0 -46
  215. package/src/main/__tests__/queue-health-verdict.test.cjs +0 -170
  216. package/src/main/__tests__/queue-starvation-dispatch-driver.test.cjs +0 -286
  217. package/src/main/__tests__/queue-starvation-per-project.test.cjs +0 -147
  218. package/src/main/__tests__/queueHistory.test.cjs +0 -355
  219. package/src/main/__tests__/queueOps-interactive-ac-lint.test.cjs +0 -65
  220. package/src/main/__tests__/queueOpsArchiveDestination.test.cjs +0 -65
  221. package/src/main/__tests__/queueOpsAutoArchive.test.cjs +0 -154
  222. package/src/main/__tests__/rateLimitPollerStreak.test.cjs +0 -128
  223. package/src/main/__tests__/rcaReport.test.cjs +0 -266
  224. package/src/main/__tests__/reconcileFlatPrdSweep.test.cjs +0 -119
  225. package/src/main/__tests__/reconcileTiming.test.cjs +0 -135
  226. package/src/main/__tests__/runLogRetention.test.cjs +0 -489
  227. package/src/main/__tests__/runVerify-atomic-verdicts.test.cjs +0 -26
  228. package/src/main/__tests__/runVerify-blocked-by-foreign-wip.test.cjs +0 -58
  229. package/src/main/__tests__/runVerify-landed-commit-outranks.test.cjs +0 -191
  230. package/src/main/__tests__/runVerify-policy-denial.test.cjs +0 -89
  231. package/src/main/__tests__/runVerify-transcript-commit-evidence.test.cjs +0 -225
  232. package/src/main/__tests__/runVerify.test.cjs +0 -1784
  233. package/src/main/__tests__/scheduleJobSchema.test.cjs +0 -127
  234. package/src/main/__tests__/scheduleJobStatusDrift.test.cjs +0 -65
  235. package/src/main/__tests__/scheduleJobTransitions.test.cjs +0 -277
  236. package/src/main/__tests__/scheduleJobTransitionsGrep.test.cjs +0 -59
  237. package/src/main/__tests__/scheduleJobTransitionsTelemetryTap.test.cjs +0 -72
  238. package/src/main/__tests__/scheduler-admin-routes.test.cjs +0 -199
  239. package/src/main/__tests__/scheduler-adopted-run-supervision.test.cjs +0 -143
  240. package/src/main/__tests__/scheduler-already-satisfied-on-main.test.cjs +0 -105
  241. package/src/main/__tests__/scheduler-archive-completed-prd.test.cjs +0 -102
  242. package/src/main/__tests__/scheduler-archived-twin-guard.test.cjs +0 -155
  243. package/src/main/__tests__/scheduler-autofix-outcome.test.cjs +0 -188
  244. package/src/main/__tests__/scheduler-autofix-select.test.cjs +0 -439
  245. package/src/main/__tests__/scheduler-autopromote.test.cjs +0 -51
  246. package/src/main/__tests__/scheduler-bash-timeout-env.test.cjs +0 -101
  247. package/src/main/__tests__/scheduler-blocked-by-foreign-wip.test.cjs +0 -107
  248. package/src/main/__tests__/scheduler-boot-orphans.test.cjs +0 -153
  249. package/src/main/__tests__/scheduler-broadcast-reconcile.test.cjs +0 -121
  250. package/src/main/__tests__/scheduler-clear-queue-history.test.cjs +0 -134
  251. package/src/main/__tests__/scheduler-commit-guard-noop.test.cjs +0 -244
  252. package/src/main/__tests__/scheduler-committed-in-window.test.cjs +0 -182
  253. package/src/main/__tests__/scheduler-cross-project-batch.test.cjs +0 -43
  254. package/src/main/__tests__/scheduler-default-eligible-heal.test.cjs +0 -168
  255. package/src/main/__tests__/scheduler-dispatch-loop.test.cjs +0 -58
  256. package/src/main/__tests__/scheduler-effective-concurrency.test.cjs +0 -81
  257. package/src/main/__tests__/scheduler-epic-digest.test.cjs +0 -227
  258. package/src/main/__tests__/scheduler-failed-autoreset.test.cjs +0 -121
  259. package/src/main/__tests__/scheduler-finalize-dispatch-guards.test.cjs +0 -229
  260. package/src/main/__tests__/scheduler-find-prd-dir.test.cjs +0 -75
  261. package/src/main/__tests__/scheduler-fix-plan-path.test.cjs +0 -119
  262. package/src/main/__tests__/scheduler-force-tick-outcome.test.cjs +0 -46
  263. package/src/main/__tests__/scheduler-foreign-wip-manifest.test.cjs +0 -78
  264. package/src/main/__tests__/scheduler-gate-shadow.test.cjs +0 -119
  265. package/src/main/__tests__/scheduler-guard-verdict-autoresolve.test.cjs +0 -390
  266. package/src/main/__tests__/scheduler-heal-refusal.test.cjs +0 -61
  267. package/src/main/__tests__/scheduler-heartbeat-payload.test.cjs +0 -80
  268. package/src/main/__tests__/scheduler-inplace-salvage.test.cjs +0 -323
  269. package/src/main/__tests__/scheduler-integration-failure-stamp.test.cjs +0 -41
  270. package/src/main/__tests__/scheduler-investigation-clean-skip.test.cjs +0 -63
  271. package/src/main/__tests__/scheduler-investigation-prompt.test.cjs +0 -123
  272. package/src/main/__tests__/scheduler-job-budget.test.cjs +0 -172
  273. package/src/main/__tests__/scheduler-job-overrun.test.cjs +0 -175
  274. package/src/main/__tests__/scheduler-launch-failure.test.cjs +0 -199
  275. package/src/main/__tests__/scheduler-leftover-fields.test.cjs +0 -52
  276. package/src/main/__tests__/scheduler-leftover-quarantine.test.cjs +0 -199
  277. package/src/main/__tests__/scheduler-looks-done.test.cjs +0 -537
  278. package/src/main/__tests__/scheduler-manual-pause.test.cjs +0 -118
  279. package/src/main/__tests__/scheduler-mechanical-recovery.test.cjs +0 -245
  280. package/src/main/__tests__/scheduler-meta-code-sha.test.cjs +0 -46
  281. package/src/main/__tests__/scheduler-needs-review-autoresolve.test.cjs +0 -197
  282. package/src/main/__tests__/scheduler-never-stop.test.cjs +0 -157
  283. package/src/main/__tests__/scheduler-no-dead-end-status.test.cjs +0 -152
  284. package/src/main/__tests__/scheduler-no-orphan-run-dir.test.cjs +0 -81
  285. package/src/main/__tests__/scheduler-notify-originating-tab-transcript.test.cjs +0 -87
  286. package/src/main/__tests__/scheduler-notify-originating-tab.test.cjs +0 -343
  287. package/src/main/__tests__/scheduler-periodic-reverify-guard.test.cjs +0 -217
  288. package/src/main/__tests__/scheduler-porcelain-rename.test.cjs +0 -164
  289. package/src/main/__tests__/scheduler-prd-missing-skip.test.cjs +0 -161
  290. package/src/main/__tests__/scheduler-prd-persona-spawn.test.cjs +0 -178
  291. package/src/main/__tests__/scheduler-quarantine-autoresolve.test.cjs +0 -165
  292. package/src/main/__tests__/scheduler-quiet-machine-lease.test.cjs +0 -257
  293. package/src/main/__tests__/scheduler-rate-limit-cooldown-freshness.test.cjs +0 -123
  294. package/src/main/__tests__/scheduler-rate-limit-pause.test.cjs +0 -152
  295. package/src/main/__tests__/scheduler-rate-limit-spin-guard.test.cjs +0 -156
  296. package/src/main/__tests__/scheduler-reap-dead-running-jobs.test.cjs +0 -754
  297. package/src/main/__tests__/scheduler-reaper-helpers-basics.test.cjs +0 -87
  298. package/src/main/__tests__/scheduler-reconcile-cwd-preserve.test.cjs +0 -100
  299. package/src/main/__tests__/scheduler-reconcile-history-backfill.test.cjs +0 -105
  300. package/src/main/__tests__/scheduler-reconcile-invalid-repair.test.cjs +0 -203
  301. package/src/main/__tests__/scheduler-reconcile-quarantine.test.cjs +0 -247
  302. package/src/main/__tests__/scheduler-reset-job-fields-guard.test.cjs +0 -77
  303. package/src/main/__tests__/scheduler-resume-recovery.test.cjs +0 -254
  304. package/src/main/__tests__/scheduler-shard-quarantine.test.cjs +0 -115
  305. package/src/main/__tests__/scheduler-shared-tree-guard.test.cjs +0 -380
  306. package/src/main/__tests__/scheduler-sigterm-commit.test.cjs +0 -43
  307. package/src/main/__tests__/scheduler-stall-per-project.test.cjs +0 -126
  308. package/src/main/__tests__/scheduler-starve-escalation.test.cjs +0 -154
  309. package/src/main/__tests__/scheduler-stranded-autofix-park.test.cjs +0 -252
  310. package/src/main/__tests__/scheduler-stranded-investigation.test.cjs +0 -185
  311. package/src/main/__tests__/scheduler-stuck-failed-escalation.test.cjs +0 -136
  312. package/src/main/__tests__/scheduler-supervisor-record.test.cjs +0 -81
  313. package/src/main/__tests__/scheduler-tick-cancel-token.test.cjs +0 -54
  314. package/src/main/__tests__/scheduler-tick-wedge.test.cjs +0 -172
  315. package/src/main/__tests__/scheduler-transient-failure.test.cjs +0 -141
  316. package/src/main/__tests__/scheduler-unreadable-queue-guard.test.cjs +0 -62
  317. package/src/main/__tests__/scheduler-utilization-hold.test.cjs +0 -89
  318. package/src/main/__tests__/scheduler-verify-prd-path.test.cjs +0 -109
  319. package/src/main/__tests__/scheduler-worktree-cap-defer.test.cjs +0 -234
  320. package/src/main/__tests__/scheduler-worktree-exec-cwd.test.cjs +0 -120
  321. package/src/main/__tests__/scheduler-writeprd-epic-rollback.test.cjs +0 -105
  322. package/src/main/__tests__/schedulerBatchRootBlocker.test.cjs +0 -117
  323. package/src/main/__tests__/schedulerStateSidecarRestore.test.cjs +0 -110
  324. package/src/main/__tests__/seedAgentPersonas.test.cjs +0 -184
  325. package/src/main/__tests__/seedSchedulerMcp.test.cjs +0 -211
  326. package/src/main/__tests__/seedStatus.test.cjs +0 -100
  327. package/src/main/__tests__/seedValidatorPersona.test.cjs +0 -116
  328. package/src/main/__tests__/stop-signal-anchor.test.cjs +0 -75
  329. package/src/main/__tests__/telemetryClient.test.cjs +0 -1055
  330. package/src/main/__tests__/telemetryContract.test.cjs +0 -930
  331. package/src/main/__tests__/telemetrySettings.test.cjs +0 -210
  332. package/src/main/__tests__/transcripts-batch-flush.test.cjs +0 -249
  333. package/src/main/__tests__/transcripts-doFlush-array.test.cjs +0 -124
  334. package/src/main/__tests__/transcripts-paged-reads.test.cjs +0 -241
  335. package/src/main/__tests__/transcripts-worktree-epic-path.test.cjs +0 -154
  336. package/src/main/__tests__/transcriptsUsageFor.test.cjs +0 -206
  337. package/src/main/__tests__/uniquePrdNumbers.test.cjs +0 -153
  338. package/src/main/__tests__/usageSingleFlight.test.cjs +0 -169
  339. package/src/main/__tests__/validationSentinels.test.cjs +0 -84
  340. package/src/main/__tests__/workTypeLibrary.test.cjs +0 -89
  341. package/src/main/bilkoHost.cjs +0 -314
  342. package/src/main/bilkoHostCore.cjs +0 -89
  343. package/src/main/lib/__tests__/active-sessions.test.cjs +0 -251
  344. package/src/main/lib/__tests__/activeIndexRebuild.test.cjs +0 -179
  345. package/src/main/lib/__tests__/agentPersonaSchema.test.cjs +0 -67
  346. package/src/main/lib/__tests__/auditLog.test.cjs +0 -38
  347. package/src/main/lib/__tests__/bootSelfHeal.test.cjs +0 -107
  348. package/src/main/lib/__tests__/branchSweep.test.cjs +0 -164
  349. package/src/main/lib/__tests__/buildIdentity.test.cjs +0 -121
  350. package/src/main/lib/__tests__/buildTarget.test.cjs +0 -52
  351. package/src/main/lib/__tests__/childWithLog.test.cjs +0 -321
  352. package/src/main/lib/__tests__/coldBootPromptSessionsWrite.test.cjs +0 -87
  353. package/src/main/lib/__tests__/crashTelemetry.test.cjs +0 -103
  354. package/src/main/lib/__tests__/credentials-futile-refresh.test.cjs +0 -115
  355. package/src/main/lib/__tests__/cwdClassify.test.cjs +0 -111
  356. package/src/main/lib/__tests__/definitionOfDoneSequence.test.cjs +0 -95
  357. package/src/main/lib/__tests__/delegationReadiness.test.cjs +0 -1175
  358. package/src/main/lib/__tests__/dispatchLoop.test.cjs +0 -63
  359. package/src/main/lib/__tests__/effectiveModelInfo.test.cjs +0 -244
  360. package/src/main/lib/__tests__/ephemeralCwd.test.cjs +0 -91
  361. package/src/main/lib/__tests__/epicDelegationStats.test.cjs +0 -137
  362. package/src/main/lib/__tests__/epicSpawnCwd.test.cjs +0 -283
  363. package/src/main/lib/__tests__/epicSpawnPlan.test.cjs +0 -196
  364. package/src/main/lib/__tests__/epicTranscriptPath.test.cjs +0 -163
  365. package/src/main/lib/__tests__/epicWorktreeBoot.test.cjs +0 -136
  366. package/src/main/lib/__tests__/epicWorktreeMerge.test.cjs +0 -130
  367. package/src/main/lib/__tests__/epicWorktreeMint.test.cjs +0 -117
  368. package/src/main/lib/__tests__/epicWorktreeProjectConfig.test.cjs +0 -137
  369. package/src/main/lib/__tests__/fixChainDepth.test.cjs +0 -40
  370. package/src/main/lib/__tests__/fixtures/204-mercury-steam-horse.log.txt +0 -13
  371. package/src/main/lib/__tests__/fixtures/scheduler-machine.json.corrupt-1789147548 +0 -34
  372. package/src/main/lib/__tests__/gateFixtures.json +0 -20
  373. package/src/main/lib/__tests__/gitCacheBound.test.cjs +0 -69
  374. package/src/main/lib/__tests__/gitWorktree.test.cjs +0 -1478
  375. package/src/main/lib/__tests__/gitWorktreeSalvage.test.cjs +0 -107
  376. package/src/main/lib/__tests__/gitWorktreeSalvageDelta.test.cjs +0 -153
  377. package/src/main/lib/__tests__/guardShims.test.cjs +0 -158
  378. package/src/main/lib/__tests__/importReferences.spec.cjs +0 -56
  379. package/src/main/lib/__tests__/instanceLock.test.cjs +0 -173
  380. package/src/main/lib/__tests__/jobSupervisorRecord.test.cjs +0 -78
  381. package/src/main/lib/__tests__/jobWorktree.test.cjs +0 -199
  382. package/src/main/lib/__tests__/jobWorktreeBootLive.test.cjs +0 -82
  383. package/src/main/lib/__tests__/landedSinceRun.test.cjs +0 -133
  384. package/src/main/lib/__tests__/launchFailure.test.cjs +0 -220
  385. package/src/main/lib/__tests__/loadGate.test.cjs +0 -303
  386. package/src/main/lib/__tests__/localAdminHttp.test.cjs +0 -214
  387. package/src/main/lib/__tests__/loopDelay.test.cjs +0 -68
  388. package/src/main/lib/__tests__/mcpToolCatalog.test.cjs +0 -107
  389. package/src/main/lib/__tests__/modelCatalog.test.cjs +0 -202
  390. package/src/main/lib/__tests__/opsOwnership.test.cjs +0 -113
  391. package/src/main/lib/__tests__/opsRootAbsoluteCwd.test.cjs +0 -328
  392. package/src/main/lib/__tests__/opsRootNestedWrite.test.cjs +0 -51
  393. package/src/main/lib/__tests__/opsRootResolve.test.cjs +0 -149
  394. package/src/main/lib/__tests__/prdDeclaredPaths.test.cjs +0 -82
  395. package/src/main/lib/__tests__/prdDisposition.test.cjs +0 -224
  396. package/src/main/lib/__tests__/procIdentity.test.cjs +0 -119
  397. package/src/main/lib/__tests__/procName.test.cjs +0 -92
  398. package/src/main/lib/__tests__/projectBriefCore.test.cjs +0 -216
  399. package/src/main/lib/__tests__/projectRootResolve.test.cjs +0 -148
  400. package/src/main/lib/__tests__/queueHealth.test.cjs +0 -58
  401. package/src/main/lib/__tests__/queueStoreAtomicWrite.test.cjs +0 -88
  402. package/src/main/lib/__tests__/queueStoreMachineStateRecovery.test.cjs +0 -190
  403. package/src/main/lib/__tests__/quietMachineLease.test.cjs +0 -39
  404. package/src/main/lib/__tests__/rateLimitWindow.test.cjs +0 -88
  405. package/src/main/lib/__tests__/reaperHelpers.test.cjs +0 -577
  406. package/src/main/lib/__tests__/schedulerBatchDepends.test.cjs +0 -312
  407. package/src/main/lib/__tests__/schedulerBatchFairness.test.cjs +0 -213
  408. package/src/main/lib/__tests__/schedulerBatchLaunchHold.test.cjs +0 -125
  409. package/src/main/lib/__tests__/schedulerBatchProjectCap.test.cjs +0 -127
  410. package/src/main/lib/__tests__/schedulerBatchQuietMachine.test.cjs +0 -109
  411. package/src/main/lib/__tests__/schedulerMcpServerHeadlessRefusal.test.cjs +0 -71
  412. package/src/main/lib/__tests__/schedulerMcpServerHelp.test.cjs +0 -216
  413. package/src/main/lib/__tests__/schedulerMcpServerProjectHome.test.cjs +0 -183
  414. package/src/main/lib/__tests__/schedulerPaths.test.cjs +0 -226
  415. package/src/main/lib/__tests__/schedulerPathsWorktree.test.cjs +0 -133
  416. package/src/main/lib/__tests__/schedulerRuntimeState.test.cjs +0 -56
  417. package/src/main/lib/__tests__/sessionSlots.test.cjs +0 -144
  418. package/src/main/lib/__tests__/telemetryBacklog.test.cjs +0 -626
  419. package/src/main/lib/__tests__/telemetryBoot.test.cjs +0 -134
  420. package/src/main/lib/__tests__/telemetryConsent.test.cjs +0 -136
  421. package/src/main/lib/__tests__/telemetryCounters.test.cjs +0 -57
  422. package/src/main/lib/__tests__/telemetryCountersMetadataColumn.test.cjs +0 -98
  423. package/src/main/lib/__tests__/terminalRunOutcome.test.cjs +0 -200
  424. package/src/main/lib/__tests__/toolUseClassify.test.cjs +0 -53
  425. package/src/main/lib/__tests__/updateCheck.test.cjs +0 -63
  426. package/src/main/lib/__tests__/upgradeDrain.test.cjs +0 -130
  427. package/src/main/lib/__tests__/usageCircuit.test.cjs +0 -354
  428. package/src/main/lib/__tests__/watchdog-helpers.test.cjs +0 -375
  429. package/src/main/lib/__tests__/watchdog-relaunch.test.cjs +0 -266
  430. package/src/main/lib/kgExchangePairing.cjs +0 -75
@@ -1,63 +0,0 @@
1
- 'use strict';
2
- import { describe, it, expect, vi } from 'vitest';
3
- const fs = require('node:fs');
4
- const os = require('node:os');
5
- const path = require('node:path');
6
- const { getUpdateStatus, isBehind } = require('../updateCheck.cjs');
7
-
8
- function mk(over = {}) {
9
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'upd-'));
10
- const fetchJson = vi.fn().mockResolvedValue({ latest: '0.91.1' });
11
- const writeJson = vi.fn(async (f, data) => { fs.writeFileSync(f, JSON.stringify(data)); });
12
- const deps = {
13
- app: { getVersion: () => '0.89.0', getPath: () => dir },
14
- env: {}, now: () => 1_000_000, packageName: () => 'pkg-x', fetchJson, writeJson, ...over,
15
- };
16
- return { dir, fetchJson, writeJson, deps, cache: path.join(dir, 'update-check.json') };
17
- }
18
-
19
- describe('getUpdateStatus', () => {
20
- it('fresh fetch populates the cache and reports behind', async () => {
21
- const t = mk();
22
- expect(await getUpdateStatus({ deps: t.deps })).toEqual({ current: '0.89.0', latest: '0.91.1', behind: true });
23
- expect(t.fetchJson).toHaveBeenCalledWith('https://registry.npmjs.org/-/package/pkg-x/dist-tags', 3000);
24
- expect(JSON.parse(fs.readFileSync(t.cache, 'utf8'))).toEqual({ checkedAt: 1_000_000, latest: '0.91.1' });
25
- });
26
- it('cache hit inside 6h makes no network call', async () => {
27
- const t = mk();
28
- fs.writeFileSync(t.cache, JSON.stringify({ checkedAt: 1_000_000 - 1000, latest: '0.91.1' }));
29
- expect((await getUpdateStatus({ deps: t.deps })).behind).toBe(true);
30
- expect(t.fetchJson).not.toHaveBeenCalled();
31
- });
32
- it('expired cache refetches', async () => {
33
- const t = mk({ now: () => 1_000_000 + 6 * 3600_000 + 1 });
34
- fs.writeFileSync(t.cache, JSON.stringify({ checkedAt: 1_000_000, latest: '0.90.0' }));
35
- expect((await getUpdateStatus({ deps: t.deps })).latest).toBe('0.91.1');
36
- expect(t.fetchJson).toHaveBeenCalledTimes(1);
37
- });
38
- it('corrupt cache is a miss', async () => {
39
- const t = mk();
40
- fs.writeFileSync(t.cache, '{nope');
41
- expect((await getUpdateStatus({ deps: t.deps })).latest).toBe('0.91.1');
42
- });
43
- it('offline falls back to latest null', async () => {
44
- const t = mk({ fetchJson: vi.fn().mockRejectedValue(new Error('ENOTFOUND')) });
45
- expect(await getUpdateStatus({ deps: t.deps })).toEqual({ current: '0.89.0', latest: null, behind: false });
46
- });
47
- it('SM_UPDATE_CHECK=0 and E2E short-circuit with no network call', async () => {
48
- for (const env of [{ SM_UPDATE_CHECK: '0' }, { SM_E2E: '1' }]) {
49
- const t = mk({ env });
50
- expect(await getUpdateStatus({ deps: t.deps })).toEqual({ current: '0.89.0', latest: null, behind: false });
51
- expect(t.fetchJson).not.toHaveBeenCalled();
52
- }
53
- });
54
- });
55
-
56
- describe('isBehind', () => {
57
- it('compares numeric triples', () => {
58
- expect(isBehind('0.89.0', '0.91.1')).toBe(true);
59
- expect(isBehind('0.91.1', '0.91.1')).toBe(false);
60
- expect(isBehind('0.100.0', '0.91.1')).toBe(false);
61
- expect(isBehind('0.92.0', '0.91.1')).toBe(false);
62
- });
63
- });
@@ -1,130 +0,0 @@
1
- 'use strict';
2
-
3
- // Run: timeout 120 npx vitest run src/main/lib/__tests__/upgradeDrain.test.cjs
4
-
5
- const assert = require('node:assert/strict');
6
- const fs = require('node:fs');
7
- const os = require('node:os');
8
- const path = require('node:path');
9
- const ud = require('../upgradeDrain.cjs');
10
-
11
- const T0 = Date.parse('2026-09-18T00:00:00Z');
12
- const req = (extra = {}) => ({ reason: 'r', requestedBy: 'u', requestedAt: new Date(T0).toISOString(), ...extra });
13
- const ev = (o) => ud.evaluateDrain({ now: T0 + 1000, ...o }).action;
14
-
15
- function tmp() { return fs.mkdtempSync(path.join(os.tmpdir(), 'upgrade-drain-')); }
16
-
17
- describe('evaluateDrain state machine', () => {
18
- test('no request → none', () => {
19
- assert.equal(ev({ request: null, queueSnapshot: { running: 2 }, drainState: null }), 'none');
20
- });
21
- test('pending request, drain not active → pause (even when idle)', () => {
22
- assert.equal(ev({ request: req(), queueSnapshot: { running: 0, investigating: 0 }, drainState: null }), 'pause');
23
- assert.equal(ev({ request: req(), queueSnapshot: { running: 3 }, drainState: null }), 'pause');
24
- });
25
- test('drain active + running or investigating → wait', () => {
26
- assert.equal(ev({ request: req(), queueSnapshot: { running: 1, investigating: 0 }, drainState: { active: true } }), 'wait');
27
- assert.equal(ev({ request: req(), queueSnapshot: { running: 0, investigating: 1 }, drainState: { active: true } }), 'wait');
28
- });
29
- test('drain active + zero busy → restart', () => {
30
- assert.equal(ev({ request: req(), queueSnapshot: { running: 0, investigating: 0 }, drainState: { active: true } }), 'restart');
31
- });
32
- test('deadline converts to abort, default 4 h', () => {
33
- const now = T0 + ud.DEFAULT_DRAIN_DEADLINE_MS;
34
- assert.equal(ud.DEFAULT_DRAIN_DEADLINE_MS, 4 * 60 * 60_000);
35
- assert.equal(ud.evaluateDrain({ request: req(), queueSnapshot: { running: 1 }, drainState: { active: true }, now }).action, 'abort');
36
- assert.equal(ud.evaluateDrain({ request: req(), queueSnapshot: { running: 1 }, drainState: { active: true }, now: now - 1 }).action, 'wait');
37
- assert.equal(ud.evaluateDrain({ request: req(), queueSnapshot: {}, drainState: { active: true }, now: T0 + 500, deadlineMs: 100 }).action, 'abort');
38
- });
39
- test('completed request → none; stale drain without request → abort', () => {
40
- assert.equal(ev({ request: req({ drainCompletedAt: 'x' }), queueSnapshot: {}, drainState: { active: true } }), 'none');
41
- assert.equal(ev({ request: null, queueSnapshot: {}, drainState: { active: true } }), 'abort');
42
- });
43
- test('a rate-limit pause during a drain does not change the verdict: drain is its own field', () => {
44
- // evaluateDrain never sees state.paused; a rate_limit pause overwriting
45
- // `paused` leaves the drain field (and the verdict) untouched.
46
- const state = { paused: { reason: 'rate_limit' }, drain: { active: true } };
47
- assert.equal(ev({ request: req(), queueSnapshot: { running: 0, investigating: 0 }, drainState: state.drain }), 'restart');
48
- assert.equal(ud.effectivePaused(state).reason, 'rate_limit');
49
- assert.equal(ud.effectivePaused({ paused: null, drain: { active: true } }).reason, 'drain-for-upgrade');
50
- assert.equal(ud.effectivePaused({ paused: null, drain: null }), null);
51
- });
52
- });
53
-
54
- describe('request file lifecycle', () => {
55
- test('requestRestart writes atomically and is idempotent while pending', () => {
56
- const dir = tmp();
57
- try {
58
- const file = path.join(dir, 'restart-request.json');
59
- const a = ud.requestRestart({ reason: 'x', requestedBy: 'user' }, { file, now: T0 });
60
- const b = ud.requestRestart({ reason: 'y', requestedBy: 'auto' }, { file, now: T0 + 5000 });
61
- assert.deepEqual(b, a, 'a second request must not reset the deadline');
62
- assert.deepEqual(fs.readdirSync(dir), ['restart-request.json'], 'no tmp file left behind');
63
- assert.equal(ud.readRestartRequest(file).requestedBy, 'user');
64
- } finally { fs.rmSync(dir, { recursive: true, force: true }); }
65
- });
66
- test('stampDrainCompleted marks it; a new request after completion starts fresh', () => {
67
- const dir = tmp();
68
- try {
69
- const file = path.join(dir, 'restart-request.json');
70
- ud.requestRestart({ reason: 'x', requestedBy: 'u' }, { file, now: T0 });
71
- assert.ok(ud.stampDrainCompleted(T0 + 1, file).drainCompletedAt);
72
- const fresh = ud.requestRestart({ reason: 'again', requestedBy: 'u' }, { file, now: T0 + 9 });
73
- assert.equal(fresh.reason, 'again');
74
- assert.equal(fresh.drainCompletedAt, undefined);
75
- } finally { fs.rmSync(dir, { recursive: true, force: true }); }
76
- });
77
- test('abort retires the request file', () => {
78
- const dir = tmp();
79
- try {
80
- const file = path.join(dir, 'restart-request.json');
81
- ud.requestRestart({ reason: 'x', requestedBy: 'u' }, { file, now: T0 });
82
- ud.retireRestartRequest(file);
83
- assert.equal(ud.readRestartRequest(file), null);
84
- ud.retireRestartRequest(file); // absent → no throw
85
- // With the request gone, a re-evaluation is 'none' — no abort flap.
86
- assert.equal(ev({ request: ud.readRestartRequest(file), queueSnapshot: {}, drainState: null }), 'none');
87
- } finally { fs.rmSync(dir, { recursive: true, force: true }); }
88
- });
89
- });
90
-
91
- describe('boot', () => {
92
- test('clears a leftover drain whose request is complete or gone; keeps a pending one', () => {
93
- assert.equal(ud.bootDrainAction({ drainState: { active: true }, request: req({ drainCompletedAt: 'x' }) }), 'clear');
94
- assert.equal(ud.bootDrainAction({ drainState: { active: true }, request: null }), 'clear');
95
- assert.equal(ud.bootDrainAction({ drainState: { active: true }, request: req() }), 'keep');
96
- assert.equal(ud.bootDrainAction({ drainState: null, request: req() }), 'none');
97
- });
98
- });
99
-
100
- describe('automatic trigger', () => {
101
- test('only when both shas are known and differ — never from npm', () => {
102
- assert.equal(ud.installedBuildDiffers({ running: 'aaa', installed: 'bbb' }), true);
103
- assert.equal(ud.installedBuildDiffers({ running: 'aaa', installed: 'aaa' }), false);
104
- assert.equal(ud.installedBuildDiffers({ running: null, installed: 'bbb' }), false);
105
- assert.equal(ud.installedBuildDiffers({ running: 'aaa', installed: null }), false);
106
- });
107
- });
108
-
109
- describe('queueBusyCount', () => {
110
- test('counts running + investigating only', () => {
111
- const jobs = [{ status: 'running' }, { status: 'investigating' }, { status: 'pending' }, { status: 'needs_review' }];
112
- assert.equal(ud.queueBusyCount(jobs), 2);
113
- assert.equal(ud.queueBusyCount(jobs, 1), 3);
114
- });
115
- });
116
-
117
- describe('restarting marker', () => {
118
- test('active until expiry, then lapses', () => {
119
- const dir = tmp();
120
- try {
121
- const file = path.join(dir, 'restarting.json');
122
- assert.equal(ud.isRestartingMarkerActive({ file, now: T0 }), false);
123
- ud.markRestarting({ file, now: T0, ttlMs: 1000 });
124
- assert.equal(ud.isRestartingMarkerActive({ file, now: T0 + 999 }), true);
125
- assert.equal(ud.isRestartingMarkerActive({ file, now: T0 + 1000 }), false);
126
- ud.clearRestartingMarker(file);
127
- assert.equal(ud.isRestartingMarkerActive({ file, now: T0 }), false);
128
- } finally { fs.rmSync(dir, { recursive: true, force: true }); }
129
- });
130
- });
@@ -1,354 +0,0 @@
1
- /**
2
- * usageCircuit.test.cjs — breaker transitions, jitter bounds, bindingWindow
3
- * fallback, reset freshness, degraded-budget floor/cap, singleFlight dedup.
4
- *
5
- * Run: timeout 300 npx vitest run src/main/lib/__tests__/usageCircuit.test.cjs
6
- */
7
-
8
- 'use strict';
9
-
10
- import { test } from 'vitest';
11
- const assert = require('node:assert/strict');
12
- const {
13
- createUsageCircuit,
14
- backoffWithJitter,
15
- isResetFresh,
16
- bindingWindow,
17
- degradedBudget,
18
- singleFlight,
19
- } = require('../usageCircuit.cjs');
20
-
21
- test('state machine: closed -> open after 3 consecutive failures', () => {
22
- let nowMs = 0;
23
- const circuit = createUsageCircuit({ now: () => nowMs });
24
- assert.strictEqual(circuit.state(), 'closed');
25
- circuit.recordFailure('meter_rate_limited');
26
- assert.strictEqual(circuit.state(), 'closed');
27
- circuit.recordFailure('meter_rate_limited');
28
- assert.strictEqual(circuit.state(), 'closed');
29
- circuit.recordFailure('meter_rate_limited');
30
- assert.strictEqual(circuit.state(), 'open');
31
- });
32
-
33
- test('open -> half_open after the backoff interval elapses, exactly one probe granted', () => {
34
- let nowMs = 0;
35
- const circuit = createUsageCircuit({ now: () => nowMs });
36
- circuit.recordFailure('a');
37
- circuit.recordFailure('a');
38
- circuit.recordFailure('a');
39
- assert.strictEqual(circuit.state(), 'open');
40
-
41
- nowMs += 10_000; // still inside the (>=15s) minimum backoff window
42
- assert.strictEqual(circuit.state(), 'open');
43
-
44
- nowMs += 60 * 60 * 1000; // well past any possible backoff interval
45
- assert.strictEqual(circuit.state(), 'half_open');
46
- // Re-querying before the probe's result is recorded must not grant a second probe.
47
- assert.strictEqual(circuit.state(), 'half_open');
48
- });
49
-
50
- test('half_open -> closed on a successful probe, resetting backoff', () => {
51
- let nowMs = 0;
52
- const circuit = createUsageCircuit({ now: () => nowMs });
53
- circuit.recordFailure('a');
54
- circuit.recordFailure('a');
55
- circuit.recordFailure('a');
56
- nowMs += 60 * 60 * 1000;
57
- assert.strictEqual(circuit.state(), 'half_open');
58
- circuit.recordSuccess({ five_hour: { utilization: 10, resets_at: null } });
59
- assert.strictEqual(circuit.state(), 'closed');
60
- });
61
-
62
- test('half_open -> open on a failed probe, with backoff doubled (probe takes longer to reopen)', () => {
63
- let nowMs = 0;
64
- const circuit = createUsageCircuit({ now: () => nowMs });
65
- circuit.recordFailure('a');
66
- circuit.recordFailure('a');
67
- circuit.recordFailure('a');
68
- nowMs += 60 * 60 * 1000;
69
- assert.strictEqual(circuit.state(), 'half_open');
70
- circuit.recordFailure('a');
71
- assert.strictEqual(circuit.state(), 'open');
72
- nowMs += 25_000; // below the doubled 2nd-stage backoff's own 30s floor
73
- assert.strictEqual(circuit.state(), 'open');
74
- nowMs += 60 * 60 * 1000;
75
- assert.strictEqual(circuit.state(), 'half_open');
76
- });
77
-
78
- test('backoffWithJitter: 1000 samples stay within [0.5x, 1.0x] of the doubled base, floor 30s / cap 8min', () => {
79
- for (let i = 0; i < 1000; i++) {
80
- const first = backoffWithJitter(0);
81
- assert.ok(first >= 15_000 && first <= 30_000, `first=${first}`);
82
-
83
- const second = backoffWithJitter(30_000);
84
- assert.ok(second >= 30_000 && second <= 60_000, `second=${second}`);
85
-
86
- const atCap = backoffWithJitter(8 * 60 * 1000);
87
- assert.ok(atCap >= 4 * 60 * 1000 && atCap <= 8 * 60 * 1000, `atCap=${atCap}`);
88
-
89
- const beyondCap = backoffWithJitter(20 * 60 * 1000); // doubling would exceed the 8min cap
90
- assert.ok(beyondCap >= 4 * 60 * 1000 && beyondCap <= 8 * 60 * 1000, `beyondCap=${beyondCap}`);
91
- }
92
- });
93
-
94
- test('isResetFresh: past -> false, future -> true, null -> false', () => {
95
- const now = Date.parse('2026-09-13T12:00:00Z');
96
- assert.strictEqual(isResetFresh('2026-09-13T11:59:59Z', now), false); // past
97
- assert.strictEqual(isResetFresh('2026-09-13T12:00:00Z', now), false); // at now
98
- assert.strictEqual(isResetFresh('2026-09-13T12:00:01Z', now), true); // future
99
- assert.strictEqual(isResetFresh(null, now), false);
100
- assert.strictEqual(isResetFresh(undefined, now), false);
101
- assert.strictEqual(isResetFresh('not-a-date', now), false);
102
- });
103
-
104
- const REAL_LIMITS = [
105
- { kind: 'session', group: 'session', percent: 6, severity: 'normal', resets_at: '2026-09-22T00:40:00.280514+00:00', scope: null, is_active: false },
106
- { kind: 'weekly_all', group: 'weekly', percent: 64, severity: 'normal', resets_at: '2026-09-24T17:00:00.280532+00:00', scope: null, is_active: true },
107
- { kind: 'weekly_scoped', group: 'weekly', percent: 18, severity: 'normal', resets_at: '2026-09-24T17:00:00.280763+00:00', scope: { model: { id: null, display_name: 'Fable' }, surface: null }, is_active: false },
108
- ];
109
-
110
- test('bindingWindow: verbatim real payload selects weekly_all 64', () => {
111
- const payload = {
112
- five_hour: { utilization: 6, resets_at: '2026-09-22T00:40:00.280514+00:00' },
113
- seven_day: { utilization: 64, resets_at: '2026-09-24T17:00:00.280532+00:00' },
114
- limits: REAL_LIMITS,
115
- };
116
- assert.deepStrictEqual(bindingWindow(payload), {
117
- name: 'weekly_all',
118
- utilization: 64,
119
- resets_at: '2026-09-24T17:00:00.280532+00:00',
120
- });
121
- });
122
-
123
- test('bindingWindow: max percent wins over is_active (session 95 beats weekly_all 64)', () => {
124
- const win = bindingWindow({
125
- limits: [
126
- { kind: 'session', percent: 95, resets_at: '2026-09-22T00:40:00Z', scope: null, is_active: false },
127
- { kind: 'weekly_all', percent: 64, resets_at: '2026-09-24T17:00:00Z', scope: null, is_active: true },
128
- ],
129
- });
130
- assert.strictEqual(win.name, 'session');
131
- assert.strictEqual(win.utilization, 95);
132
- });
133
-
134
- test('bindingWindow: is_active breaks a percent tie', () => {
135
- const win = bindingWindow({
136
- limits: [
137
- { kind: 'session', percent: 50, resets_at: null, scope: null, is_active: false },
138
- { kind: 'weekly_all', percent: 50, resets_at: null, scope: null, is_active: true },
139
- ],
140
- });
141
- assert.strictEqual(win.name, 'weekly_all');
142
- });
143
-
144
- test('bindingWindow: a high weekly_scoped entry is never selected', () => {
145
- const win = bindingWindow({
146
- limits: [
147
- { kind: 'session', percent: 6, resets_at: null, scope: null, is_active: false },
148
- { kind: 'weekly_all', percent: 10, resets_at: null, scope: null, is_active: true },
149
- { kind: 'weekly_scoped', percent: 99, resets_at: null, scope: { model: { display_name: 'Fable' } }, is_active: false },
150
- ],
151
- });
152
- assert.strictEqual(win.name, 'weekly_all');
153
- assert.strictEqual(win.utilization, 10);
154
- });
155
-
156
- test('bindingWindow: percent over 100 is returned raw, not clamped', () => {
157
- const win = bindingWindow({ limits: [{ kind: 'session', percent: 112, resets_at: null, scope: null }] });
158
- assert.strictEqual(win.utilization, 112);
159
- });
160
-
161
- test('bindingWindow: null resets_at backfills from the matching flat window', () => {
162
- const win = bindingWindow({
163
- five_hour: { utilization: 40, resets_at: '2026-09-22T00:40:00Z' },
164
- seven_day: { utilization: 5, resets_at: '2026-09-24T17:00:00Z' },
165
- limits: [
166
- { kind: 'session', percent: 40, resets_at: null, scope: null },
167
- { kind: 'weekly_all', percent: 5, resets_at: null, scope: null },
168
- ],
169
- });
170
- assert.strictEqual(win.name, 'session');
171
- assert.strictEqual(win.resets_at, '2026-09-22T00:40:00Z');
172
- });
173
-
174
- test('bindingWindow: limits[] absent falls back to five_hour', () => {
175
- const payload = { five_hour: { utilization: 42, resets_at: '2026-09-13T15:00:00Z' } };
176
- const win = bindingWindow(payload);
177
- assert.strictEqual(win.name, 'five_hour');
178
- assert.strictEqual(win.utilization, 42);
179
- });
180
-
181
- test('bindingWindow: limits[] with only scoped / non-finite entries falls back to five_hour', () => {
182
- const payload = {
183
- five_hour: { utilization: 12, resets_at: null },
184
- limits: [
185
- { kind: 'weekly_scoped', percent: 81, resets_at: null, scope: { model: { display_name: 'Fable' } } },
186
- { kind: 'session', resets_at: null, scope: null },
187
- ],
188
- };
189
- const win = bindingWindow(payload);
190
- assert.strictEqual(win.name, 'five_hour');
191
- assert.strictEqual(win.utilization, 12);
192
- });
193
-
194
- test('degradedBudget: a last-good binding window that genuinely read 0% carries forward as 0, not 100', () => {
195
- const payload = { five_hour: { utilization: 0, resets_at: null } };
196
- assert.strictEqual(degradedBudget(payload, {}).utilization, 0);
197
- });
198
-
199
- test('degradedBudget: no payload ever received (null/absent) still yields 100, never a blind 0', () => {
200
- assert.strictEqual(degradedBudget(null, {}).utilization, 100);
201
- assert.strictEqual(degradedBudget(undefined, {}).utilization, 100);
202
- });
203
-
204
- test('degradedBudget: a degraded cycle after a real-shape poll carries forward the real percent (64), not 100', () => {
205
- const payload = {
206
- limits: [
207
- { kind: 'session', percent: 6, resets_at: null, scope: null, is_active: false },
208
- { kind: 'weekly_all', percent: 64, resets_at: '2026-09-24T17:00:00Z', scope: null, is_active: true },
209
- ],
210
- };
211
- assert.strictEqual(degradedBudget(payload, {}).utilization, 64);
212
- });
213
-
214
- test('degradedBudget: carries forward the real last-good BINDING (weekly_all) utilization, e.g. 81%, never 0', () => {
215
- const payload = {
216
- five_hour: { utilization: 0, resets_at: null },
217
- limits: [
218
- { kind: 'session', is_active: false, percent: 0, resets_at: null, scope: null },
219
- { kind: 'weekly_all', is_active: true, percent: 81, resets_at: '2026-09-19T00:00:00Z', scope: null },
220
- ],
221
- };
222
- const { utilization } = degradedBudget(payload, {});
223
- assert.strictEqual(utilization, 81);
224
- });
225
-
226
- test('degradedBudget: caps concurrency at 2 regardless of a higher configured cap', () => {
227
- const payload = { five_hour: { utilization: 30, resets_at: null } };
228
- const { concurrencyCap } = degradedBudget(payload, { configuredCap: 5 });
229
- assert.strictEqual(concurrencyCap, 2);
230
- });
231
-
232
- test('degradedBudget: respects a configured cap below 2', () => {
233
- const payload = { five_hour: { utilization: 30, resets_at: null } };
234
- const { concurrencyCap } = degradedBudget(payload, { configuredCap: 1 });
235
- assert.strictEqual(concurrencyCap, 1);
236
- });
237
-
238
- test('degradedBudget: an executor-observed 429 whose window has not passed pins utilization to 100', () => {
239
- const payload = { five_hour: { utilization: 5, resets_at: null } };
240
- const now = Date.parse('2026-09-13T12:00:00Z');
241
- const { utilization } = degradedBudget(payload, {
242
- observed429: true,
243
- resetsAt: '2026-09-13T13:00:00Z', // still in the future
244
- now,
245
- });
246
- assert.strictEqual(utilization, 100);
247
- });
248
-
249
- test('degradedBudget: once the 429 window passes, it stops pinning to 100 and falls back to real utilization', () => {
250
- const payload = { five_hour: { utilization: 5, resets_at: null } };
251
- const now = Date.parse('2026-09-13T12:00:00Z');
252
- const { utilization } = degradedBudget(payload, {
253
- observed429: true,
254
- resetsAt: '2026-09-13T11:00:00Z', // already passed
255
- now,
256
- });
257
- assert.strictEqual(utilization, 5);
258
- });
259
-
260
- test('singleFlight: concurrent callers share the same in-flight promise, fn invoked once', async () => {
261
- let calls = 0;
262
- let resolveFn;
263
- const fn = () => new Promise((resolve) => { resolveFn = resolve; calls += 1; });
264
- const wrapped = singleFlight(fn);
265
-
266
- const p1 = wrapped();
267
- const p2 = wrapped();
268
- assert.strictEqual(p1, p2);
269
- assert.strictEqual(calls, 1);
270
- resolveFn('done');
271
- assert.strictEqual(await p1, 'done');
272
-
273
- const p3 = wrapped();
274
- assert.notStrictEqual(p3, p1);
275
- assert.strictEqual(calls, 2);
276
- resolveFn('done2');
277
- await p3;
278
- });
279
-
280
- test('openedAt: null while closed, stamped the moment it opens, stays stamped through a failed half_open probe, clears on success', () => {
281
- let nowMs = 1000;
282
- const circuit = createUsageCircuit({ now: () => nowMs });
283
- assert.strictEqual(circuit.openedAt(), null);
284
- circuit.recordFailure('a');
285
- circuit.recordFailure('a');
286
- assert.strictEqual(circuit.openedAt(), null); // still closed, 2 of 3
287
- circuit.recordFailure('a');
288
- assert.strictEqual(circuit.state(), 'open');
289
- assert.strictEqual(circuit.openedAt(), 1000);
290
-
291
- nowMs += 60 * 60 * 1000;
292
- assert.strictEqual(circuit.state(), 'half_open');
293
- assert.strictEqual(circuit.openedAt(), 1000); // unchanged through the probe
294
-
295
- circuit.recordFailure('a'); // failed probe -> reopens
296
- assert.strictEqual(circuit.state(), 'open');
297
- assert.strictEqual(circuit.openedAt(), 1000); // still the ORIGINAL open time, not re-stamped
298
-
299
- circuit.recordSuccess({});
300
- assert.strictEqual(circuit.openedAt(), null);
301
- });
302
-
303
- test('getConsecutiveFailures: tracks failures, resets on success', () => {
304
- const circuit = createUsageCircuit({ now: () => 0 });
305
- assert.strictEqual(circuit.getConsecutiveFailures(), 0);
306
- circuit.recordFailure('a');
307
- circuit.recordFailure('a');
308
- assert.strictEqual(circuit.getConsecutiveFailures(), 2);
309
- circuit.recordSuccess({});
310
- assert.strictEqual(circuit.getConsecutiveFailures(), 0);
311
- });
312
-
313
- test('SM_USAGE_CIRCUIT=0 bypasses the breaker entirely: state stays closed no matter how many failures', () => {
314
- const prev = process.env.SM_USAGE_CIRCUIT;
315
- process.env.SM_USAGE_CIRCUIT = '0';
316
- try {
317
- const circuit = createUsageCircuit({ now: () => 0 });
318
- circuit.recordFailure('a');
319
- circuit.recordFailure('a');
320
- circuit.recordFailure('a');
321
- circuit.recordFailure('a');
322
- assert.strictEqual(circuit.state(), 'closed');
323
- assert.strictEqual(circuit.openedAt(), null);
324
- } finally {
325
- if (prev === undefined) delete process.env.SM_USAGE_CIRCUIT; else process.env.SM_USAGE_CIRCUIT = prev;
326
- }
327
- });
328
-
329
- test('SM_USAGE_DEGRADED_CAP overrides degradedBudget concurrencyCap outright, bypassing min(cap, 2)', () => {
330
- const prev = process.env.SM_USAGE_DEGRADED_CAP;
331
- process.env.SM_USAGE_DEGRADED_CAP = '7';
332
- try {
333
- const payload = { five_hour: { utilization: 30, resets_at: null } };
334
- const { concurrencyCap } = degradedBudget(payload, { configuredCap: 2 });
335
- assert.strictEqual(concurrencyCap, 7);
336
- } finally {
337
- if (prev === undefined) delete process.env.SM_USAGE_DEGRADED_CAP; else process.env.SM_USAGE_DEGRADED_CAP = prev;
338
- }
339
- });
340
-
341
- test('singleFlight: de-dupes even when fn rejects, next call gets a fresh attempt', async () => {
342
- let calls = 0;
343
- const fn = () => (calls++ === 0 ? Promise.reject(new Error('boom')) : Promise.resolve('ok'));
344
- const wrapped = singleFlight(fn);
345
-
346
- const p1 = wrapped();
347
- const p2 = wrapped();
348
- assert.strictEqual(p1, p2);
349
- await assert.rejects(p1, /boom/);
350
-
351
- const p3 = wrapped();
352
- assert.strictEqual(await p3, 'ok');
353
- assert.strictEqual(calls, 2);
354
- });