claude-code-session-manager 0.141.0 → 0.142.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 (200) hide show
  1. package/README.md +80 -70
  2. package/bin/cli.cjs +11 -0
  3. package/dist/assets/AppSettings-BcZgIvvY.js +2 -0
  4. package/dist/assets/{History-DJMiuXyK.js → History-DpZuJXi1.js} +1 -1
  5. package/dist/assets/HomeClaudeConfig-D32ZJtU-.js +3 -0
  6. package/dist/assets/{Panel-Bf1Cf8UL.js → Panel-BjdJjhqt.js} +1 -1
  7. package/dist/assets/{ProjectClaudeConfig-BWPpypf9.js → ProjectClaudeConfig-BTClYV6i.js} +7 -7
  8. package/dist/assets/Scheduler-DPobKx1Z.js +16 -0
  9. package/dist/assets/{SkillReferenceGraph-Ba4TkX2C.js → SkillReferenceGraph-Cqyu4mef.js} +1 -1
  10. package/dist/assets/{Skills-C1v91GEO.js → Skills-vv0noNVI.js} +5 -5
  11. package/dist/assets/{TiptapBody-BrcWVpiW.js → TiptapBody-CXi8Pvut.js} +1 -1
  12. package/dist/assets/Toggle-CiYCWLjh.js +1 -0
  13. package/dist/assets/{VoiceButton-qivnDV6K.js → VoiceButton-4NKKd1sC.js} +1 -1
  14. package/dist/assets/{historyTotals-CMCPTKqC.js → historyTotals-DDunb8Ch.js} +1 -1
  15. package/dist/assets/index-Cn5E9958.js +3133 -0
  16. package/dist/assets/{index-DKXdXfka.css → index-DVHIHoJf.css} +1 -1
  17. package/dist/index.html +2 -2
  18. package/dist-web/app.css +1 -1
  19. package/dist-web/app.js +496 -484
  20. package/package.json +4 -2
  21. package/plugins/AGENTS.md +4 -3
  22. package/plugins/session-manager-dev/hooks/hooks.json +15 -0
  23. package/plugins/session-manager-dev/hooks/residue-check.cjs +241 -0
  24. package/plugins/session-manager-dev/skills/develop/SKILL.md +52 -18
  25. package/plugins/session-manager-dev/skills/develop/standards.md +3 -2
  26. package/scripts/README.md +1 -0
  27. package/scripts/hooks/guard-tool-risk.cjs +5 -1
  28. package/scripts/hooks/lib/guard-tool-risk-policy.cjs +158 -1
  29. package/scripts/scheduler-mcp-server.cjs +732 -110
  30. package/src/main/adminRouteRegistry.cjs +59 -0
  31. package/src/main/agentLibrary.cjs +72 -4
  32. package/src/main/agentsIpc.cjs +3 -2
  33. package/src/main/build-info.json +4 -4
  34. package/src/main/chatRunner.cjs +231 -16
  35. package/src/main/config.cjs +30 -0
  36. package/src/main/docEdit.cjs +4 -2
  37. package/src/main/health.cjs +91 -0
  38. package/src/main/index.cjs +98 -48
  39. package/src/main/ipcSchemas.cjs +161 -8
  40. package/src/main/lib/AGENTS.md +1 -1
  41. package/src/main/lib/chatBootRecovery.cjs +2 -0
  42. package/src/main/lib/claude/agentModelResolve.cjs +51 -25
  43. package/src/main/lib/claude/agentSnapshot.cjs +186 -0
  44. package/src/main/lib/claude/environmentProfiles.cjs +242 -0
  45. package/src/main/lib/claude/jobSandboxPolicy.cjs +241 -33
  46. package/src/main/lib/claude/personaToolScope.cjs +82 -31
  47. package/src/main/lib/claude/runClaudeP.cjs +4 -2
  48. package/src/main/lib/claude/toolRiskMarks.cjs +41 -1
  49. package/src/main/lib/claude/usageCircuit.cjs +49 -21
  50. package/src/main/lib/claude/vaultRefs.cjs +277 -0
  51. package/src/main/lib/demo-video-kit/capture.cjs +282 -12
  52. package/src/main/lib/demo-video-kit/cast.md +43 -0
  53. package/src/main/lib/demo-video-kit/character.cjs +722 -30
  54. package/src/main/lib/demo-video-kit/checks.cjs +120 -1
  55. package/src/main/lib/demo-video-kit/export.cjs +218 -0
  56. package/src/main/lib/demo-video-kit/frames.cjs +104 -9
  57. package/src/main/lib/demo-video-kit/kind-marketing.md +121 -0
  58. package/src/main/lib/demo-video-kit/kind-tiktok.md +99 -0
  59. package/src/main/lib/demo-video-kit/kokoroSay.cjs +205 -0
  60. package/src/main/lib/demo-video-kit/project-home-html.md +43 -0
  61. package/src/main/lib/demo-video-kit/resumableRun.cjs +298 -0
  62. package/src/main/lib/demo-video-kit/seedKit.cjs +3 -3
  63. package/src/main/lib/demo-video-kit/voiceLane.cjs +233 -0
  64. package/src/main/lib/demo-video-kit/voiceLibrary.cjs +312 -0
  65. package/src/main/lib/epic/artifactAdminRoutes.cjs +211 -0
  66. package/src/main/lib/epic/artifactIpc.cjs +153 -0
  67. package/src/main/lib/epic/artifactNotice.cjs +72 -0
  68. package/src/main/lib/epic/artifactRetention.cjs +310 -0
  69. package/src/main/lib/epic/artifactStore.cjs +392 -0
  70. package/src/main/lib/epic/delegationReadiness.cjs +76 -2
  71. package/src/main/lib/epic/epicAgentRefresh.cjs +97 -0
  72. package/src/main/lib/epic/epicEventLog.cjs +214 -0
  73. package/src/main/lib/epic/epicMint.cjs +43 -1
  74. package/src/main/lib/epic/epicOutsideRead.cjs +1 -0
  75. package/src/main/lib/epic/epicOutsideState.cjs +18 -4
  76. package/src/main/lib/epic/epicPurge.cjs +347 -0
  77. package/src/main/lib/epic/epicReopen.cjs +5 -2
  78. package/src/main/lib/epic/epicSettle.cjs +4 -13
  79. package/src/main/lib/epic/epicSpawnPlan.cjs +57 -4
  80. package/src/main/lib/epic/epicStatusMirror.cjs +34 -1
  81. package/src/main/lib/epic/epicTransitions.cjs +139 -0
  82. package/src/main/lib/epic/epicValidationHook.cjs +81 -2
  83. package/src/main/lib/epic/guardSpawnSettings.cjs +3 -2
  84. package/src/main/lib/externalContract.cjs +37 -4
  85. package/src/main/lib/feedback-remote/crossProjectReply.cjs +389 -126
  86. package/src/main/lib/feedback-remote/customerFeedbackAdminRoutes.cjs +16 -3
  87. package/src/main/lib/feedback-remote/customerFeedbackClient.cjs +11 -6
  88. package/src/main/lib/feedback-remote/customerFeedbackInbox.cjs +26 -1
  89. package/src/main/lib/feedback-remote/customerFeedbackIpc.cjs +5 -2
  90. package/src/main/lib/feedback-remote/feedbackBoxDrain.cjs +409 -0
  91. package/src/main/lib/feedback-remote/feedbackBoxKeys.cjs +218 -0
  92. package/src/main/lib/feedback-remote/feedbackEnvelope.cjs +163 -0
  93. package/src/main/lib/feedback-remote/feedbackRelayClient.cjs +164 -0
  94. package/src/main/lib/feedback-remote/feedbackReport.cjs +234 -0
  95. package/src/main/lib/feedback-remote/feedbackSeal.cjs +217 -0
  96. package/src/main/lib/feedback-remote/localAdminHttp.cjs +108 -2
  97. package/src/main/lib/feedback-remote/macroAdminRoutes.cjs +2 -1
  98. package/src/main/lib/feedback-remote/needsYouIpc.cjs +17 -0
  99. package/src/main/lib/feedback-remote/needsYouStore.cjs +177 -6
  100. package/src/main/lib/feedback-remote/pendingReplies.cjs +189 -0
  101. package/src/main/lib/feedback-remote/projectHomeAdminRoutes.cjs +188 -10
  102. package/src/main/lib/feedback-remote/serviceClient.cjs +429 -0
  103. package/src/main/lib/feedback-remote/serviceMcpSession.cjs +252 -0
  104. package/src/main/lib/feedback-remote/serviceMcpTools.cjs +284 -0
  105. package/src/main/lib/feedback-remote/servicePairings.cjs +313 -0
  106. package/src/main/lib/feedback-remote/serviceStdioBridge.cjs +290 -0
  107. package/src/main/lib/feedback-remote/siteFeedbackAdminRoutes.cjs +295 -0
  108. package/src/main/lib/feedback-remote/siteFeedbackInbox.cjs +598 -0
  109. package/src/main/lib/feedback-remote/siteFeedbackIpc.cjs +86 -0
  110. package/src/main/lib/feedback-remote/siteFeedbackService.cjs +477 -0
  111. package/src/main/lib/git/worktreeDeps.cjs +77 -2
  112. package/src/main/lib/mcpToolCatalog.cjs +304 -28
  113. package/src/main/lib/opsOwnership.cjs +29 -1
  114. package/src/main/lib/opsRetentionSettings.cjs +27 -5
  115. package/src/main/lib/persona-macro/builtinMacros.cjs +7 -2
  116. package/src/main/lib/persona-macro/macroJobDispatch.cjs +52 -8
  117. package/src/main/lib/persona-macro/macroJobIpc.cjs +16 -6
  118. package/src/main/lib/persona-macro/macroLibrary.cjs +49 -7
  119. package/src/main/lib/persona-macro/personaFrontmatterSchema.cjs +83 -0
  120. package/src/main/lib/persona-macro/projectVideoToolCheck.cjs +3 -0
  121. package/src/main/lib/persona-macro/shippedPersonaSeeds.cjs +263 -0
  122. package/src/main/lib/phaseProjection.cjs +103 -0
  123. package/src/main/lib/prd/depSlugResolve.cjs +4 -13
  124. package/src/main/lib/prd/prdBodyPatch.cjs +12 -17
  125. package/src/main/lib/prd/prdCreate.cjs +41 -5
  126. package/src/main/lib/prd/prdCriteria.cjs +225 -0
  127. package/src/main/lib/prd/prdFrontmatter.cjs +43 -3
  128. package/src/main/lib/prd/prdGateFiles.cjs +125 -49
  129. package/src/main/lib/prd/prdSizing.cjs +3 -1
  130. package/src/main/lib/process/cleanEnv.cjs +52 -1
  131. package/src/main/lib/process/leftoverRenice.cjs +178 -0
  132. package/src/main/lib/process/memoryScope.cjs +31 -3
  133. package/src/main/lib/project-pages/pagesLibrary.cjs +72 -6
  134. package/src/main/lib/project-pages/videoReviewIpc.cjs +3 -2
  135. package/src/main/lib/project-pages/voiceAdminRoutes.cjs +175 -0
  136. package/src/main/lib/project-pages/voicePreview.cjs +196 -0
  137. package/src/main/lib/project-pages/voicesIpc.cjs +186 -0
  138. package/src/main/lib/promptSessionSchema.cjs +6 -0
  139. package/src/main/lib/scheduler/cancelledRow.cjs +57 -0
  140. package/src/main/lib/scheduler/cpuSample.cjs +133 -42
  141. package/src/main/lib/scheduler/criteriaChecks.cjs +169 -0
  142. package/src/main/lib/scheduler/definitionOfDone.cjs +44 -5
  143. package/src/main/lib/scheduler/jobMemoryStats.cjs +313 -0
  144. package/src/main/lib/scheduler/loadGate.cjs +82 -4
  145. package/src/main/lib/scheduler/planRecordHarvest.cjs +32 -2
  146. package/src/main/lib/scheduler/prdOutcomeRecord.cjs +133 -0
  147. package/src/main/lib/scheduler/runLogRetention.cjs +42 -1
  148. package/src/main/lib/scheduler/scheduleJobSchema.cjs +5 -0
  149. package/src/main/lib/scheduler/scheduleJobTransitions.cjs +47 -0
  150. package/src/main/lib/scheduler/schedulerConfig.cjs +47 -0
  151. package/src/main/lib/scheduler/schedulerPaths.cjs +0 -3
  152. package/src/main/lib/scheduler/schedulerRuntimeState.cjs +50 -1
  153. package/src/main/lib/scheduler/sessionSlots.cjs +5 -1
  154. package/src/main/lib/scheduler/slotsRoute.cjs +237 -0
  155. package/src/main/lib/scheduler/toolClearance.cjs +258 -0
  156. package/src/main/lib/scheduler/validatorVerdictSchema.cjs +140 -10
  157. package/src/main/lib/tasks/memoryConsolidation.cjs +300 -0
  158. package/src/main/lib/tasks/mitigationReview.cjs +294 -0
  159. package/src/main/lib/tasks/taskAdminRoutes.cjs +312 -22
  160. package/src/main/lib/tasks/taskIpc.cjs +22 -18
  161. package/src/main/lib/tasks/taskLedger.cjs +147 -25
  162. package/src/main/lib/tasks/taskRun.cjs +186 -17
  163. package/src/main/lib/tasks/taskSwitch.cjs +85 -12
  164. package/src/main/lib/tasks/taskSwitchRoute.cjs +172 -0
  165. package/src/main/lib/telemetry/auditEvents.cjs +207 -6
  166. package/src/main/lib/telemetry/eventsAdminRoutes.cjs +198 -6
  167. package/src/main/lib/telemetry/webhookDelivery.cjs +205 -0
  168. package/src/main/lib/telemetry/webhookRegistry.cjs +281 -0
  169. package/src/main/lib/toolUseClassify.cjs +181 -0
  170. package/src/main/lib/util/smfileRange.cjs +77 -0
  171. package/src/main/projectPages.cjs +3 -1
  172. package/src/main/promptSessionEvents.cjs +5 -0
  173. package/src/main/pty.cjs +68 -0
  174. package/src/main/queueOps.cjs +32 -13
  175. package/src/main/runVerify.cjs +74 -9
  176. package/src/main/scheduler/AGENTS.md +1 -0
  177. package/src/main/scheduler/budget.cjs +6 -5
  178. package/src/main/scheduler/prdParser.cjs +46 -13
  179. package/src/main/scheduler/reviewLadder.cjs +9 -25
  180. package/src/main/scheduler/utilizationHold.cjs +101 -0
  181. package/src/main/scheduler.cjs +1414 -189
  182. package/src/main/templates/PRD_AUTHORING.md +5 -2
  183. package/src/main/webRemote.cjs +463 -6
  184. package/src/preload/api.d.ts +529 -38
  185. package/src/preload/apiSurface.json +209 -7
  186. package/src/preload/index.cjs +46 -1
  187. package/src/seed/agents/architect.md +6 -1
  188. package/src/seed/agents/blender-artist.md +2 -1
  189. package/src/seed/agents/competitive-analyst.md +3 -1
  190. package/src/seed/agents/competitive-researcher.md +2 -1
  191. package/src/seed/agents/demo-video-builder.md +87 -83
  192. package/src/seed/agents/dev-lead.md +3 -3
  193. package/src/seed/agents/project-home-builder.md +9 -31
  194. package/src/seed/agents/validator.md +9 -10
  195. package/dist/assets/AppSettings-DExeJhqQ.js +0 -2
  196. package/dist/assets/HomeClaudeConfig-CTp2tkZ4.js +0 -3
  197. package/dist/assets/Scheduler-cyr61UAC.js +0 -16
  198. package/dist/assets/ViewTabs-DXhOWFG0.js +0 -1
  199. package/dist/assets/index-B2TQz3nd.js +0 -3121
  200. package/src/main/lib/epic/epicStateWatcher.cjs +0 -157
@@ -14,6 +14,8 @@ const { COMPLETED_EQUIVALENT_VERDICTS } = require('../lib/scheduler/terminalRunO
14
14
  const { resolveIsFixPlan } = require('../lib/scheduler/fixPlanSlug.cjs');
15
15
  const { GATE_AUTHORITY_VERDICTS } = require('../lib/scheduler/gateAuthority.cjs');
16
16
  const { DEFAULT_PROJECT_CWD } = require('../lib/scheduler/schedulerBatch.cjs');
17
+ // Every automated selector skips a human-cancelled row (incident 2026-10-19 git-viewer: cancelled rows re-queued and merged).
18
+ const { isCancelledRow } = require('../lib/scheduler/cancelledRow.cjs');
17
19
 
18
20
  /**
19
21
  * Resume-first recovery (PRD 1111). A job parked in needs_review with verdict
@@ -217,7 +219,7 @@ function isFinishReviewKillHealEligible(job) {
217
219
  if (typeof job.landedCommit !== 'string' || job.landedCommit.length === 0) return false;
218
220
  if (job.finishReviewHealAttemptedAt) return false;
219
221
  if (job.integrationFailureKind) return false;
220
- if (isCancelledJob(job) || isRefireCapped(job)) return false;
222
+ if (isCancelledRow(job) || isRefireCapped(job)) return false;
221
223
  return true;
222
224
  }
223
225
 
@@ -596,23 +598,6 @@ function healRefusalReason(job, verdict, committedDuringRun) {
596
598
  * gate or a real non-zero-exit error) is excluded — that failure is
597
599
  * evidence, not silence, and must never become a heal candidate (PRD 1102).
598
600
  */
599
- // A human cancelled this row (scheduler_cancel_job). Incident 2026-10-19
600
- // (git-viewer): cancelled jobs were parked as 'failed' (source
601
- // 'remote:cancelJob'), then selectFailedAutoResetTargets re-queued them 10
602
- // minutes later and they ran and merged unwanted commits. Marker is
603
- // `cancelledAt`; the statusHistory source is the fallback for rows cancelled
604
- // before the marker existed. Every automated selector must skip these; only a
605
- // human scheduler_reset_job revives one.
606
- function isCancelledJob(job) {
607
- if (!job) return false;
608
- if (typeof job.cancelledAt === 'string' && job.cancelledAt) return true;
609
- const history = Array.isArray(job.statusHistory) ? job.statusHistory : [];
610
- for (let i = history.length - 1; i >= 0; i--) {
611
- if (history[i] && history[i].to === job.status) return history[i].source === 'remote:cancelJob';
612
- }
613
- return false;
614
- }
615
-
616
601
  function isFailedUnverifiedShaped(job) {
617
602
  if (!job || job.status !== 'failed') return false;
618
603
  if (job.verifierVerdict && RESCANNABLE_VERDICTS.has(job.verifierVerdict)) return true;
@@ -623,7 +608,7 @@ function isFailedUnverifiedShaped(job) {
623
608
  }
624
609
 
625
610
  function isRescanCandidate(job) {
626
- if (!job || isCancelledJob(job) || isRefireCapped(job)) return false;
611
+ if (!job || isCancelledRow(job) || isRefireCapped(job)) return false;
627
612
  // Default-ELIGIBLE: every needs_review row is a heal candidate unless its
628
613
  // verdict is in RESCAN_EXCLUDED_VERDICTS. No runId requirement here — a row
629
614
  // without one still gets the evidence rung and the unresolvable annotation.
@@ -655,7 +640,7 @@ function isTranscriptRescannable(job) {
655
640
  function selectEvidenceScanTargets(jobs, now = Date.now()) {
656
641
  const due = [];
657
642
  for (const j of jobs ?? []) {
658
- if (isCancelledJob(j)) continue;
643
+ if (isCancelledRow(j)) continue;
659
644
  if (j.status !== 'needs_review' || !isRescanCandidate(j)) continue;
660
645
  if (isTranscriptRescannable(j)) continue;
661
646
  if (j.looksDone) continue;
@@ -785,7 +770,7 @@ function failedAutoResetDisabled() {
785
770
  function selectFailedAutoResetTargets(jobs, now, thresholdMs) {
786
771
  const targets = [];
787
772
  for (const j of jobs ?? []) {
788
- if (isCancelledJob(j)) continue;
773
+ if (isCancelledRow(j)) continue;
789
774
  if (j.status !== 'failed') continue;
790
775
  const attempts = j.failedAutoResetAttempts ?? 0;
791
776
  if (attempts >= FAILED_AUTORESET_CAP) continue;
@@ -837,7 +822,7 @@ function selectFailedAutoResetTargets(jobs, now, thresholdMs) {
837
822
  function findStuckFailedJobs(jobs, now, thresholdMs) {
838
823
  const stuck = [];
839
824
  for (const j of jobs ?? []) {
840
- if (isCancelledJob(j)) continue;
825
+ if (isCancelledRow(j)) continue;
841
826
  if (j.status !== 'failed') continue;
842
827
  if (j.stuckFailedNotified === true) continue;
843
828
  if (!isRescanCandidate(j)) continue;
@@ -906,7 +891,7 @@ function needsReviewAutoResolveDisabled() {
906
891
  function selectExhaustedNeedsReviewTargets(jobs, now, thresholdMs) {
907
892
  const targets = [];
908
893
  for (const j of jobs ?? []) {
909
- if (isCancelledJob(j)) continue;
894
+ if (isCancelledRow(j)) continue;
910
895
  if (j.status !== 'needs_review') continue;
911
896
  if (!isEligibleForNeedsReviewAutoResolve(j, jobs)) continue;
912
897
  if ((j.exhaustedResolveAttempts ?? 0) > NEEDS_REVIEW_RESOLVE_CAP) continue;
@@ -949,7 +934,7 @@ function selectExhaustedNeedsReviewTargets(jobs, now, thresholdMs) {
949
934
  function selectAutoFixTargets(jobs, { fixSlugExists, resolveJobRunId = resolveRunId }) {
950
935
  const slugsInQueue = new Set(jobs.map((j) => j.slug));
951
936
  return jobs.filter((job) => {
952
- if (isCancelledJob(job)) return false;
937
+ if (isCancelledRow(job)) return false;
953
938
  if (isRefireCapped(job)) return false;
954
939
  if (job.status !== 'needs_review') return false;
955
940
  // A job parked here because it was blocked by a sibling's foreign WIP
@@ -1117,7 +1102,6 @@ function selectResumeRecoveryBatch(jobs, { saturated = false } = {}) {
1117
1102
  module.exports = {
1118
1103
  selectResumeRecoveryBatch,
1119
1104
  isRefireCapped,
1120
- isCancelledJob,
1121
1105
  selectResumeRecoveryTarget,
1122
1106
  MECHANICALLY_RESOLVABLE_VERDICTS,
1123
1107
  isMechanicalRecoveryFutile,
@@ -0,0 +1,101 @@
1
+ // @ts-check
2
+ 'use strict';
3
+
4
+ /**
5
+ * scheduler/utilizationHold.cjs — the one rule deciding whether an automatic
6
+ * (non-human) scheduler tick may dispatch. Pure: no I/O, no clock (`now` is a
7
+ * parameter), so every caller (when-available, job-finish, dispatch-loop,
8
+ * slot-release ticks) agrees.
9
+ *
10
+ * Why every window: during a meter outage the top window (a 5-hour session
11
+ * at 97%) can reset while weekly_all in the same payload is still at 92%
12
+ * until Thursday. Reading only the binding window drops the hold at the
13
+ * session reset and the queue dispatches into the weekly limit (Self,
14
+ * 2026-11-23). A window whose reset has passed is skipped — never read as
15
+ * "no hold" for the others.
16
+ */
17
+
18
+ /**
19
+ * @typedef {{ window: string | null, percent: number | null, resetsAt: string | null }} UsageWindow
20
+ * @typedef {{
21
+ * percent: number | null,
22
+ * window: string | null,
23
+ * resetsAt: string | null,
24
+ * known: boolean,
25
+ * windows: UsageWindow[] | null,
26
+ * }} UtilizationReading
27
+ * @typedef {{
28
+ * reason: 'manual-fire-policy' | 'utilization-held',
29
+ * window: string | null,
30
+ * percent: number | null,
31
+ * threshold: number | null,
32
+ * resetsAt: string | null,
33
+ * detail: string,
34
+ * }} AutoDispatchHold
35
+ */
36
+
37
+ /**
38
+ * Reset time as ms, or +Infinity when null/unparseable (counts as "resets last").
39
+ * @param {string | null | undefined} resetsAt
40
+ * @returns {number}
41
+ */
42
+ function resetMs(resetsAt) {
43
+ const ms = typeof resetsAt === 'string' ? Date.parse(resetsAt) : NaN;
44
+ return Number.isFinite(ms) ? ms : Infinity;
45
+ }
46
+
47
+ /**
48
+ * @param {{
49
+ * firePolicy: string | null | undefined,
50
+ * threshold: number | null | undefined,
51
+ * reading: UtilizationReading | null | undefined,
52
+ * now: number,
53
+ * }} args
54
+ * @returns {AutoDispatchHold | null}
55
+ */
56
+ function evaluateAutoDispatchHold({ firePolicy, threshold, reading, now }) {
57
+ if (firePolicy === 'manual') {
58
+ return {
59
+ reason: 'manual-fire-policy',
60
+ window: null,
61
+ percent: null,
62
+ threshold: null,
63
+ resetsAt: null,
64
+ detail: 'fire policy is manual: only Run now dispatches',
65
+ };
66
+ }
67
+ if (reading == null || reading.known !== true) return null;
68
+ if (typeof threshold !== 'number' || !Number.isFinite(threshold)) return null;
69
+ const limit = threshold;
70
+
71
+ /** @type {UsageWindow[]} */
72
+ const candidates = Array.isArray(reading.windows) && reading.windows.length > 0
73
+ ? reading.windows
74
+ : [{ window: reading.window, percent: reading.percent, resetsAt: reading.resetsAt }];
75
+
76
+ /** @type {{ window: string | null, percent: number, resetsAt: string | null, resetAt: number } | null} */
77
+ let pick = null;
78
+ for (const c of candidates) {
79
+ if (!c) continue;
80
+ const percent = c.percent;
81
+ if (typeof percent !== 'number' || !Number.isFinite(percent)) continue;
82
+ const resetAt = resetMs(c.resetsAt);
83
+ if (resetAt <= now) continue; // already reset: its old reading no longer applies
84
+ if (percent < limit) continue;
85
+ if (pick === null || resetAt > pick.resetAt || (resetAt === pick.resetAt && percent > pick.percent)) {
86
+ pick = { window: c.window ?? null, percent, resetsAt: c.resetsAt ?? null, resetAt };
87
+ }
88
+ }
89
+ if (pick === null) return null;
90
+
91
+ return {
92
+ reason: 'utilization-held',
93
+ window: pick.window,
94
+ percent: pick.percent,
95
+ threshold: limit,
96
+ resetsAt: pick.resetsAt,
97
+ detail: `${pick.window ?? 'binding'} window at ${String(pick.percent)}% ≥ ${String(limit)}%, resets ${pick.resetsAt ?? 'unknown'}`,
98
+ };
99
+ }
100
+
101
+ module.exports = { evaluateAutoDispatchHold };