@the-open-engine/zeroshot 6.38.0 → 6.39.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 (381) hide show
  1. package/cli/event-copy.js +8 -7
  2. package/cli/event-copy.ts +12 -0
  3. package/cli/index.js +7 -31
  4. package/cli/json-export.js +61 -64
  5. package/cli/json-export.ts +112 -0
  6. package/cli/message-formatter-utils.js +68 -56
  7. package/cli/message-formatter-utils.ts +121 -0
  8. package/cli/message-formatters-normal.js +124 -259
  9. package/cli/message-formatters-normal.ts +250 -0
  10. package/cli/message-formatters-watch.js +81 -84
  11. package/cli/message-formatters-watch.ts +189 -0
  12. package/lib/agent-cli-provider/adapters/codex.d.ts.map +1 -1
  13. package/lib/agent-cli-provider/adapters/codex.js +3 -1
  14. package/lib/agent-cli-provider/adapters/codex.js.map +1 -1
  15. package/lib/cluster/generated/protocol-schema.cjs +1 -1
  16. package/lib/cluster/generated/protocol-schema.mjs +1 -1
  17. package/lib/cluster/generated/protocol.d.cts +8 -0
  18. package/lib/cluster/generated/protocol.d.mts +8 -0
  19. package/lib/cluster/generated/protocol.d.ts +8 -0
  20. package/lib/clusters-registry.d.ts +23 -0
  21. package/lib/clusters-registry.js +17 -29
  22. package/lib/completion.d.ts +12 -0
  23. package/lib/completion.js +84 -94
  24. package/lib/compose-utils.d.ts +32 -0
  25. package/lib/compose-utils.js +57 -72
  26. package/lib/detached-startup.d.ts +83 -0
  27. package/lib/detached-startup.js +231 -302
  28. package/lib/docker-config.d.ts +43 -0
  29. package/lib/docker-config.js +150 -325
  30. package/lib/docker-env-auth.d.ts +17 -0
  31. package/lib/docker-env-auth.js +75 -0
  32. package/lib/git-remote-utils.d.ts +48 -0
  33. package/lib/git-remote-utils.js +147 -213
  34. package/lib/id-detector.d.ts +15 -0
  35. package/lib/id-detector.js +37 -41
  36. package/lib/path-check.d.ts +30 -0
  37. package/lib/path-check.js +42 -45
  38. package/lib/process-liveness.d.ts +16 -0
  39. package/lib/process-liveness.js +19 -12
  40. package/lib/provider-credential-path.d.ts +16 -0
  41. package/lib/provider-credential-path.js +31 -29
  42. package/lib/provider-defaults.d.ts +30 -0
  43. package/lib/provider-defaults.js +47 -42
  44. package/lib/provider-detection.d.ts +11 -0
  45. package/lib/provider-detection.js +91 -54
  46. package/lib/provider-names.d.ts +38 -0
  47. package/lib/provider-names.js +56 -64
  48. package/lib/repo-settings.d.ts +28 -0
  49. package/lib/repo-settings.js +83 -59
  50. package/lib/run-mode.d.ts +21 -0
  51. package/lib/run-mode.js +21 -20
  52. package/lib/run-plan.d.ts +28 -0
  53. package/lib/run-plan.js +17 -16
  54. package/lib/settings/claude-auth.d.ts +35 -0
  55. package/lib/settings/claude-auth.js +30 -47
  56. package/lib/settings-defaults.d.ts +20 -0
  57. package/lib/settings-defaults.js +149 -0
  58. package/lib/settings-error.d.ts +7 -0
  59. package/lib/settings-error.js +8 -0
  60. package/lib/settings-issue-providers.d.ts +10 -0
  61. package/lib/settings-issue-providers.js +12 -0
  62. package/lib/settings-models.d.ts +22 -0
  63. package/lib/settings-models.js +93 -0
  64. package/lib/settings-storage.d.ts +23 -0
  65. package/lib/settings-storage.js +205 -0
  66. package/lib/settings-validation.d.ts +7 -0
  67. package/lib/settings-validation.js +182 -0
  68. package/lib/settings.d.ts +29 -0
  69. package/lib/settings.js +41 -736
  70. package/lib/setup-apply-types.d.ts +66 -0
  71. package/lib/setup-apply-types.js +2 -0
  72. package/lib/setup-apply.d.ts +39 -0
  73. package/lib/setup-apply.js +267 -264
  74. package/lib/setup-journal.d.ts +41 -0
  75. package/lib/setup-journal.js +119 -76
  76. package/lib/setup-plan-types.d.ts +102 -0
  77. package/lib/setup-plan-types.js +2 -0
  78. package/lib/setup-plan.d.ts +30 -0
  79. package/lib/setup-plan.js +300 -324
  80. package/lib/setup-undo.d.ts +44 -0
  81. package/lib/setup-undo.js +61 -74
  82. package/lib/start-cluster-config.d.ts +26 -0
  83. package/lib/start-cluster-config.js +56 -0
  84. package/lib/start-cluster-environment.d.ts +40 -0
  85. package/lib/start-cluster-environment.js +134 -0
  86. package/lib/start-cluster-run-options.d.ts +53 -0
  87. package/lib/start-cluster-run-options.js +147 -0
  88. package/lib/start-cluster.d.ts +64 -0
  89. package/lib/start-cluster.js +98 -454
  90. package/lib/stream-json-parser.d.ts +17 -0
  91. package/lib/stream-json-parser.js +51 -52
  92. package/npm-shrinkwrap.json +2 -2
  93. package/package.json +12 -6
  94. package/scripts/postinstall.js +27 -0
  95. package/scripts/rust-distribution.js +23 -14
  96. package/src/agent/agent-command-proofs-context.js +35 -45
  97. package/src/agent/agent-command-proofs-context.ts +87 -0
  98. package/src/agent/agent-config-settings.js +19 -0
  99. package/src/agent/agent-config-settings.ts +41 -0
  100. package/src/agent/agent-config.js +122 -231
  101. package/src/agent/agent-config.ts +285 -0
  102. package/src/agent/agent-context-builder.js +38 -273
  103. package/src/agent/agent-context-builder.ts +51 -0
  104. package/src/agent/agent-context-environment-sections.js +105 -0
  105. package/src/agent/agent-context-environment-sections.ts +134 -0
  106. package/src/agent/agent-context-packs.js +165 -0
  107. package/src/agent/agent-context-packs.ts +296 -0
  108. package/src/agent/agent-context-prompt-sections.js +100 -0
  109. package/src/agent/agent-context-prompt-sections.ts +148 -0
  110. package/src/agent/agent-context-sections.js +25 -334
  111. package/src/agent/agent-context-sections.ts +36 -0
  112. package/src/agent/agent-context-sources.js +96 -149
  113. package/src/agent/agent-context-sources.ts +253 -0
  114. package/src/agent/agent-context-types.js +2 -0
  115. package/src/agent/agent-context-types.ts +132 -0
  116. package/src/agent/agent-context-validation-sections.js +73 -0
  117. package/src/agent/agent-context-validation-sections.ts +129 -0
  118. package/src/agent/agent-input-injector.js +99 -118
  119. package/src/agent/agent-input-injector.ts +195 -0
  120. package/src/agent/agent-liveness-poll.js +74 -78
  121. package/src/agent/agent-liveness-poll.ts +163 -0
  122. package/src/agent/agent-quality-gate-schema.js +77 -86
  123. package/src/agent/agent-quality-gate-schema.ts +135 -0
  124. package/src/agent/agent-quality-gates-context.js +44 -50
  125. package/src/agent/agent-quality-gates-context.ts +91 -0
  126. package/src/agent/agent-stuck-detector.js +171 -239
  127. package/src/agent/agent-stuck-detector.ts +288 -0
  128. package/src/agent/agent-trigger-evaluator.js +29 -46
  129. package/src/agent/agent-trigger-evaluator.ts +98 -0
  130. package/src/agent/context-metrics.js +110 -141
  131. package/src/agent/context-metrics.ts +261 -0
  132. package/src/agent/context-pack-builder.js +72 -358
  133. package/src/agent/context-pack-builder.ts +105 -0
  134. package/src/agent/context-pack-core.js +70 -0
  135. package/src/agent/context-pack-core.ts +96 -0
  136. package/src/agent/context-pack-selection.js +119 -0
  137. package/src/agent/context-pack-selection.ts +187 -0
  138. package/src/agent/context-pack-truncation.js +139 -0
  139. package/src/agent/context-pack-truncation.ts +208 -0
  140. package/src/agent/context-pack-types.js +2 -0
  141. package/src/agent/context-pack-types.ts +98 -0
  142. package/src/agent/context-replay-policy.js +30 -39
  143. package/src/agent/context-replay-policy.ts +71 -0
  144. package/src/agent/critical-agent-policy.js +6 -10
  145. package/src/agent/critical-agent-policy.ts +20 -0
  146. package/src/agent/guidance-queue.js +74 -87
  147. package/src/agent/guidance-queue.ts +166 -0
  148. package/src/agent/output-extraction-error-detail.js +45 -0
  149. package/src/agent/output-extraction-error-detail.ts +49 -0
  150. package/src/agent/output-extraction-failures.js +125 -0
  151. package/src/agent/output-extraction-failures.ts +149 -0
  152. package/src/agent/output-extraction-json.js +234 -0
  153. package/src/agent/output-extraction-json.ts +235 -0
  154. package/src/agent/output-extraction-pi.js +124 -0
  155. package/src/agent/output-extraction-pi.ts +137 -0
  156. package/src/agent/output-extraction-types.js +2 -0
  157. package/src/agent/output-extraction-types.ts +49 -0
  158. package/src/agent/output-extraction.js +17 -617
  159. package/src/agent/output-extraction.ts +30 -0
  160. package/src/agent/output-reformatter-errors.js +83 -0
  161. package/src/agent/output-reformatter-errors.ts +106 -0
  162. package/src/agent/output-reformatter-types.js +2 -0
  163. package/src/agent/output-reformatter-types.ts +86 -0
  164. package/src/agent/output-reformatter.js +140 -185
  165. package/src/agent/output-reformatter.ts +238 -0
  166. package/src/agent/pi-terminal-lifecycle-types.js +2 -0
  167. package/src/agent/pi-terminal-lifecycle-types.ts +41 -0
  168. package/src/agent/pi-terminal-lifecycle.js +186 -206
  169. package/src/agent/pi-terminal-lifecycle.ts +297 -0
  170. package/src/agent/provider-control-plane.js +22 -21
  171. package/src/agent/provider-control-plane.ts +52 -0
  172. package/src/agent/provider-terminal-failure-types.js +2 -0
  173. package/src/agent/provider-terminal-failure-types.ts +60 -0
  174. package/src/agent/provider-terminal-failure.js +194 -191
  175. package/src/agent/provider-terminal-failure.ts +262 -0
  176. package/src/agent/rate-limit-backoff.js +31 -74
  177. package/src/agent/rate-limit-backoff.ts +71 -0
  178. package/src/agent/schema-utils.js +85 -125
  179. package/src/agent/schema-utils.ts +148 -0
  180. package/src/agent/structured-output-error.js +34 -33
  181. package/src/agent/structured-output-error.ts +105 -0
  182. package/src/agent/validation-platform.js +27 -27
  183. package/src/agent/validation-platform.ts +49 -0
  184. package/src/agent-cli-provider/adapters/codex.ts +3 -1
  185. package/src/attach/attach-client.js +232 -405
  186. package/src/attach/attach-client.ts +300 -0
  187. package/src/attach/attach-server-cleanup.js +43 -0
  188. package/src/attach/attach-server-cleanup.ts +39 -0
  189. package/src/attach/attach-server-clients.js +117 -0
  190. package/src/attach/attach-server-clients.ts +122 -0
  191. package/src/attach/attach-server-events.js +38 -0
  192. package/src/attach/attach-server-events.ts +36 -0
  193. package/src/attach/attach-server-pty.js +45 -0
  194. package/src/attach/attach-server-pty.ts +64 -0
  195. package/src/attach/attach-server-runtime.js +73 -0
  196. package/src/attach/attach-server-runtime.ts +62 -0
  197. package/src/attach/attach-server-socket.js +28 -0
  198. package/src/attach/attach-server-socket.ts +23 -0
  199. package/src/attach/attach-server-types.js +2 -0
  200. package/src/attach/attach-server-types.ts +82 -0
  201. package/src/attach/attach-server.js +106 -523
  202. package/src/attach/attach-server.ts +158 -0
  203. package/src/attach/index.js +18 -35
  204. package/src/attach/index.ts +18 -0
  205. package/src/attach/protocol.js +95 -184
  206. package/src/attach/protocol.ts +181 -0
  207. package/src/attach/ring-buffer.js +76 -102
  208. package/src/attach/ring-buffer.ts +109 -0
  209. package/src/attach/send-input.js +64 -75
  210. package/src/attach/send-input.ts +94 -0
  211. package/src/attach/socket-discovery.js +164 -218
  212. package/src/attach/socket-discovery.ts +230 -0
  213. package/src/attach/socket-paths.js +42 -61
  214. package/src/attach/socket-paths.ts +81 -0
  215. package/src/claude-credentials.js +51 -54
  216. package/src/claude-credentials.ts +89 -0
  217. package/src/cluster/generated/protocol-schema.ts +1 -1
  218. package/src/cluster/generated/protocol.ts +2 -1
  219. package/src/command-proofs.js +110 -141
  220. package/src/command-proofs.ts +216 -0
  221. package/src/config-router.js +81 -73
  222. package/src/config-router.ts +131 -0
  223. package/src/copy-worker.js +53 -29
  224. package/src/copy-worker.ts +75 -0
  225. package/src/darwin-keychain-boundary.js +80 -111
  226. package/src/darwin-keychain-boundary.ts +148 -0
  227. package/src/guidance-topics.js +4 -5
  228. package/src/guidance-topics.ts +10 -0
  229. package/src/input-helpers.js +33 -54
  230. package/src/input-helpers.ts +58 -0
  231. package/src/ledger-sequence.js +45 -48
  232. package/src/ledger-sequence.ts +63 -0
  233. package/src/legacy-lib/clusters-registry.ts +40 -0
  234. package/src/legacy-lib/completion.ts +148 -0
  235. package/src/legacy-lib/compose-utils.ts +102 -0
  236. package/src/legacy-lib/detached-startup.ts +449 -0
  237. package/src/legacy-lib/docker-config.ts +282 -0
  238. package/src/legacy-lib/docker-env-auth.ts +116 -0
  239. package/src/legacy-lib/git-remote-utils.ts +244 -0
  240. package/src/legacy-lib/id-detector.ts +57 -0
  241. package/src/legacy-lib/js-yaml.d.ts +3 -0
  242. package/src/legacy-lib/omelette.d.ts +15 -0
  243. package/src/legacy-lib/path-check.ts +89 -0
  244. package/src/legacy-lib/process-liveness.ts +36 -0
  245. package/src/legacy-lib/provider-credential-path.ts +57 -0
  246. package/src/legacy-lib/provider-defaults.ts +114 -0
  247. package/src/legacy-lib/provider-detection.ts +67 -0
  248. package/src/legacy-lib/provider-names.ts +126 -0
  249. package/src/legacy-lib/repo-settings.ts +88 -0
  250. package/src/legacy-lib/run-mode.ts +58 -0
  251. package/src/legacy-lib/run-plan.ts +49 -0
  252. package/src/legacy-lib/settings/claude-auth.ts +80 -0
  253. package/src/legacy-lib/settings-defaults.ts +212 -0
  254. package/src/legacy-lib/settings-error.ts +8 -0
  255. package/src/legacy-lib/settings-issue-providers.ts +18 -0
  256. package/src/legacy-lib/settings-models.ts +124 -0
  257. package/src/legacy-lib/settings-storage.ts +277 -0
  258. package/src/legacy-lib/settings-validation.ts +220 -0
  259. package/src/legacy-lib/settings.ts +87 -0
  260. package/src/legacy-lib/setup-apply-types.ts +70 -0
  261. package/src/legacy-lib/setup-apply.ts +456 -0
  262. package/src/legacy-lib/setup-journal.ts +185 -0
  263. package/src/legacy-lib/setup-plan-types.ts +96 -0
  264. package/src/legacy-lib/setup-plan.ts +500 -0
  265. package/src/legacy-lib/setup-undo.ts +142 -0
  266. package/src/legacy-lib/start-cluster-config.ts +124 -0
  267. package/src/legacy-lib/start-cluster-environment.ts +165 -0
  268. package/src/legacy-lib/start-cluster-run-options.ts +279 -0
  269. package/src/legacy-lib/start-cluster.ts +237 -0
  270. package/src/legacy-lib/stream-json-parser.ts +88 -0
  271. package/src/message-buffer.js +55 -71
  272. package/src/message-buffer.ts +107 -0
  273. package/src/message-bus-bridge.js +92 -133
  274. package/src/message-bus-bridge.ts +149 -0
  275. package/src/name-generator-shared.js +220 -0
  276. package/src/name-generator-shared.ts +231 -0
  277. package/src/name-generator.js +8 -217
  278. package/src/name-generator.ts +27 -0
  279. package/src/omp-blob-root.js +83 -64
  280. package/src/omp-blob-root.ts +116 -0
  281. package/src/omp-config-overlay.js +65 -38
  282. package/src/omp-config-overlay.ts +110 -0
  283. package/src/omp-execution-fingerprint.js +38 -43
  284. package/src/omp-execution-fingerprint.ts +92 -0
  285. package/src/omp-session-limits.js +10 -15
  286. package/src/omp-session-limits.ts +53 -0
  287. package/src/omp-session-partition.js +2 -2
  288. package/src/omp-session-verifier.js +1 -1
  289. package/src/orchestrator.js +19 -4
  290. package/src/providers/anthropic/index.js +3 -1
  291. package/src/providers/anthropic/index.ts +13 -0
  292. package/src/providers/capabilities.js +19 -27
  293. package/src/providers/capabilities.ts +44 -0
  294. package/src/providers/google/index.js +3 -1
  295. package/src/providers/google/index.ts +13 -0
  296. package/src/providers/openai/index.js +3 -1
  297. package/src/providers/openai/index.ts +13 -0
  298. package/src/providers/opencode/index.js +3 -1
  299. package/src/providers/opencode/index.ts +13 -0
  300. package/src/quality-gates.js +67 -127
  301. package/src/quality-gates.ts +149 -0
  302. package/src/repo-settings-access.js +40 -0
  303. package/src/repo-settings-access.ts +64 -0
  304. package/src/schemas/sub-cluster.js +127 -183
  305. package/src/schemas/sub-cluster.ts +238 -0
  306. package/src/state-snapshot-normalization.js +224 -0
  307. package/src/state-snapshot-normalization.ts +222 -0
  308. package/src/state-snapshot.js +192 -359
  309. package/src/state-snapshot.ts +291 -0
  310. package/src/state-snapshotter.js +118 -130
  311. package/src/state-snapshotter.ts +167 -0
  312. package/src/sub-cluster-wrapper.js +4 -13
  313. package/src/task-run-model-args.js +87 -124
  314. package/src/task-run-model-args.ts +204 -0
  315. package/src/task-runner.js +5 -25
  316. package/src/task-runner.ts +28 -0
  317. package/src/task-startup-error.js +73 -58
  318. package/src/task-startup-error.ts +126 -0
  319. package/src/template-validation/consensus-gate-contracts.js +2 -0
  320. package/src/template-validation/consensus-gate-contracts.ts +70 -0
  321. package/src/template-validation/consensus-gate-scenarios.js +76 -0
  322. package/src/template-validation/consensus-gate-scenarios.ts +130 -0
  323. package/src/template-validation/consensus-gate-stage.js +34 -0
  324. package/src/template-validation/consensus-gate-stage.ts +56 -0
  325. package/src/template-validation/random-topology-contracts.js +2 -0
  326. package/src/template-validation/random-topology-contracts.ts +54 -0
  327. package/src/template-validation/random-topology-dispatch.js +25 -0
  328. package/src/template-validation/random-topology-dispatch.ts +33 -0
  329. package/src/template-validation/random-topology-message-loop.js +53 -0
  330. package/src/template-validation/random-topology-message-loop.ts +73 -0
  331. package/src/template-validation/random-topology-operations.js +79 -0
  332. package/src/template-validation/random-topology-operations.ts +101 -0
  333. package/src/template-validation/random-topology-runtime.js +25 -0
  334. package/src/template-validation/random-topology-runtime.ts +43 -0
  335. package/src/template-validation/random-topology-scenario.js +52 -0
  336. package/src/template-validation/random-topology-scenario.ts +57 -0
  337. package/src/template-validation/random-topology-schema.js +159 -0
  338. package/src/template-validation/random-topology-schema.ts +190 -0
  339. package/src/template-validation/random-topology-state.js +28 -0
  340. package/src/template-validation/random-topology-state.ts +30 -0
  341. package/src/template-validation/random-topology-trigger-evaluation.js +45 -0
  342. package/src/template-validation/random-topology-trigger-evaluation.ts +65 -0
  343. package/src/template-validation/random-topology-trigger.js +75 -0
  344. package/src/template-validation/random-topology-trigger.ts +88 -0
  345. package/src/template-validation/report-formatter.js +40 -46
  346. package/src/template-validation/report-formatter.ts +101 -0
  347. package/src/template-validation/simulate-consensus-gates.js +111 -302
  348. package/src/template-validation/simulate-consensus-gates.ts +196 -0
  349. package/src/template-validation/simulate-random-topology.js +26 -519
  350. package/src/template-validation/simulate-random-topology.ts +57 -0
  351. package/src/template-validation/simulate-two-stage-validation.js +41 -265
  352. package/src/template-validation/simulate-two-stage-validation.ts +55 -0
  353. package/src/template-validation/simulation-agent-runtime.js +25 -0
  354. package/src/template-validation/simulation-agent-runtime.ts +40 -0
  355. package/src/template-validation/simulation-agent.js +29 -0
  356. package/src/template-validation/simulation-agent.ts +73 -0
  357. package/src/template-validation/simulation-runtime.js +34 -0
  358. package/src/template-validation/simulation-runtime.ts +72 -0
  359. package/src/template-validation/two-stage-contracts.js +2 -0
  360. package/src/template-validation/two-stage-contracts.ts +41 -0
  361. package/src/template-validation/two-stage-inputs.js +21 -0
  362. package/src/template-validation/two-stage-inputs.ts +29 -0
  363. package/src/template-validation/two-stage-results.js +30 -0
  364. package/src/template-validation/two-stage-results.ts +40 -0
  365. package/src/template-validation/two-stage-scenario.js +86 -0
  366. package/src/template-validation/two-stage-scenario.ts +120 -0
  367. package/src/worktree-claude-config.js +182 -220
  368. package/src/worktree-claude-config.ts +272 -0
  369. package/src/worktree-tooling-env.js +103 -125
  370. package/src/worktree-tooling-env.ts +142 -0
  371. package/task-lib/completion.js +2 -3
  372. package/task-lib/completion.ts +9 -0
  373. package/task-lib/config.js +1 -4
  374. package/task-lib/config.ts +11 -0
  375. package/task-lib/name-generator.js +4 -226
  376. package/task-lib/name-generator.ts +11 -0
  377. package/task-lib/omp-session-cleanup.js +1 -1
  378. package/task-lib/omp-storage-root.js +8 -12
  379. package/task-lib/omp-storage-root.ts +62 -0
  380. package/task-lib/process-termination.js +143 -166
  381. package/task-lib/process-termination.ts +268 -0
@@ -0,0 +1,96 @@
1
+ import contextMetrics = require('./context-metrics');
2
+
3
+ import type {
4
+ ContextPack,
5
+ ContextPackPriority,
6
+ ContextPackVariant,
7
+ NormalizedContextPack,
8
+ RenderedVariant,
9
+ } from './context-pack-types';
10
+
11
+ const { estimateTokensFromChars } = contextMetrics;
12
+
13
+ const PRIORITY_RANK: Readonly<Record<ContextPackPriority, number>> = {
14
+ required: 0,
15
+ high: 1,
16
+ medium: 2,
17
+ low: 3,
18
+ };
19
+
20
+ const DEFAULT_PRIORITY: ContextPackPriority = 'medium';
21
+
22
+ function isContextPackPriority(priority: string): priority is ContextPackPriority {
23
+ return (
24
+ priority === 'required' || priority === 'high' || priority === 'medium' || priority === 'low'
25
+ );
26
+ }
27
+
28
+ function normalizePriority(
29
+ priority: string | undefined,
30
+ required: boolean | undefined
31
+ ): ContextPackPriority {
32
+ if (required) return 'required';
33
+ if (priority && isContextPackPriority(priority)) return priority;
34
+ return DEFAULT_PRIORITY;
35
+ }
36
+
37
+ function normalizePack(pack: ContextPack, index: number): NormalizedContextPack {
38
+ const priority = normalizePriority(pack.priority, pack.required);
39
+ return {
40
+ ...pack,
41
+ priority,
42
+ required: pack.required || priority === 'required',
43
+ order: pack.order ?? index,
44
+ };
45
+ }
46
+
47
+ function renderVariant(
48
+ pack: NormalizedContextPack,
49
+ variant: ContextPackVariant,
50
+ cache: Map<string, RenderedVariant>
51
+ ): RenderedVariant {
52
+ const cacheKey = `${pack.id}:${variant}`;
53
+ const cached = cache.get(cacheKey);
54
+ if (cached) return cached;
55
+
56
+ let text: unknown = '';
57
+ if (variant === 'full') {
58
+ text = typeof pack.render === 'function' ? pack.render() : '';
59
+ } else if (variant === 'compact') {
60
+ text = typeof pack.compact === 'function' ? pack.compact() : '';
61
+ }
62
+
63
+ const safeText = typeof text === 'string' ? text : '';
64
+ const chars = safeText.length;
65
+ const estimatedTokens = estimateTokensFromChars(chars);
66
+ const rendered = { text: safeText, chars, estimatedTokens };
67
+ cache.set(cacheKey, rendered);
68
+ return rendered;
69
+ }
70
+
71
+ function sortByPriorityThenOrder(a: NormalizedContextPack, b: NormalizedContextPack): number {
72
+ const priorityDelta = PRIORITY_RANK[a.priority] - PRIORITY_RANK[b.priority];
73
+ if (priorityDelta !== 0) return priorityDelta;
74
+ return a.order - b.order;
75
+ }
76
+
77
+ function sortByOrder(a: NormalizedContextPack, b: NormalizedContextPack): number {
78
+ return a.order - b.order;
79
+ }
80
+
81
+ function sortByPriorityDescThenOrderDesc(
82
+ a: NormalizedContextPack,
83
+ b: NormalizedContextPack
84
+ ): number {
85
+ const priorityDelta = PRIORITY_RANK[b.priority] - PRIORITY_RANK[a.priority];
86
+ if (priorityDelta !== 0) return priorityDelta;
87
+ return b.order - a.order;
88
+ }
89
+
90
+ export = {
91
+ normalizePack,
92
+ renderVariant,
93
+ sortByPriorityThenOrder,
94
+ sortByOrder,
95
+ sortByPriorityDescThenOrderDesc,
96
+ };
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ const contextPackCore = require("./context-pack-core");
3
+ const { renderVariant, sortByPriorityThenOrder } = contextPackCore;
4
+ function includedSelection(variant, rendered) {
5
+ return {
6
+ status: 'included',
7
+ variant,
8
+ chars: rendered.chars,
9
+ estimatedTokens: rendered.estimatedTokens,
10
+ text: rendered.text,
11
+ };
12
+ }
13
+ function skippedSelection(reason) {
14
+ return {
15
+ status: 'skipped',
16
+ variant: null,
17
+ reason,
18
+ chars: 0,
19
+ estimatedTokens: 0,
20
+ };
21
+ }
22
+ function shouldCompactRequired(full, compact, remainingTokens) {
23
+ return (Number.isFinite(remainingTokens) &&
24
+ compact !== null &&
25
+ compact.chars > 0 &&
26
+ full.estimatedTokens > remainingTokens &&
27
+ (compact.estimatedTokens <= remainingTokens || compact.estimatedTokens < full.estimatedTokens));
28
+ }
29
+ function selectRequiredVariant(full, compact, remainingTokens) {
30
+ if (full.chars === 0 && compact && compact.chars > 0) {
31
+ return includedSelection('compact', compact);
32
+ }
33
+ if (shouldCompactRequired(full, compact, remainingTokens)) {
34
+ return includedSelection('compact', compact);
35
+ }
36
+ return includedSelection('full', full);
37
+ }
38
+ function selectOptionalVariant(full, compact, remainingTokens) {
39
+ if (!Number.isFinite(remainingTokens) || full.estimatedTokens <= remainingTokens) {
40
+ return includedSelection('full', full);
41
+ }
42
+ if (compact && compact.chars > 0 && compact.estimatedTokens <= remainingTokens) {
43
+ return includedSelection('compact', compact);
44
+ }
45
+ return skippedSelection('budget');
46
+ }
47
+ function selectVariant(pack, remainingTokens, cache) {
48
+ const full = renderVariant(pack, 'full', cache);
49
+ const compact = pack.compact ? renderVariant(pack, 'compact', cache) : null;
50
+ if (full.chars === 0 && (!compact || compact.chars === 0)) {
51
+ return skippedSelection('empty');
52
+ }
53
+ return pack.required
54
+ ? selectRequiredVariant(full, compact, remainingTokens)
55
+ : selectOptionalVariant(full, compact, remainingTokens);
56
+ }
57
+ function buildDecision(pack, selection) {
58
+ return {
59
+ id: pack.id,
60
+ section: pack.section || null,
61
+ priority: pack.priority,
62
+ required: pack.required,
63
+ status: selection.status,
64
+ variant: selection.variant,
65
+ chars: selection.chars,
66
+ estimatedTokens: selection.estimatedTokens,
67
+ order: pack.order,
68
+ reason: 'reason' in selection ? selection.reason : null,
69
+ };
70
+ }
71
+ function consumeSelectionBudget(selection, budget) {
72
+ if (!Number.isFinite(budget.remainingTokens)) {
73
+ return budget;
74
+ }
75
+ if (selection.estimatedTokens > budget.remainingTokens) {
76
+ return {
77
+ remainingTokens: 0,
78
+ overBudgetTokens: budget.overBudgetTokens + selection.estimatedTokens - budget.remainingTokens,
79
+ };
80
+ }
81
+ return {
82
+ remainingTokens: budget.remainingTokens - selection.estimatedTokens,
83
+ overBudgetTokens: budget.overBudgetTokens,
84
+ };
85
+ }
86
+ function selectPacks(normalized, maxTokens) {
87
+ const renderCache = new Map();
88
+ const decisions = new Map();
89
+ const selected = new Map();
90
+ let budget = {
91
+ remainingTokens: typeof maxTokens === 'number' && Number.isFinite(maxTokens) ? maxTokens : Infinity,
92
+ overBudgetTokens: 0,
93
+ };
94
+ const selectionOrder = normalized.slice().sort(sortByPriorityThenOrder);
95
+ for (const pack of selectionOrder) {
96
+ const selection = selectVariant(pack, budget.remainingTokens, renderCache);
97
+ decisions.set(pack.id, buildDecision(pack, selection));
98
+ if (selection.status !== 'included') {
99
+ continue;
100
+ }
101
+ selected.set(pack.id, {
102
+ text: selection.text,
103
+ chars: selection.chars,
104
+ estimatedTokens: selection.estimatedTokens,
105
+ variant: selection.variant,
106
+ });
107
+ budget = consumeSelectionBudget(selection, budget);
108
+ }
109
+ return {
110
+ renderCache,
111
+ decisions,
112
+ selected,
113
+ remainingTokens: budget.remainingTokens,
114
+ overBudgetTokens: budget.overBudgetTokens,
115
+ };
116
+ }
117
+ module.exports = {
118
+ selectPacks,
119
+ };
@@ -0,0 +1,187 @@
1
+ import contextPackCore = require('./context-pack-core');
2
+
3
+ import type {
4
+ IncludedSelection,
5
+ MutableContextPackDecision,
6
+ NormalizedContextPack,
7
+ PackSelection,
8
+ PackSelectionState,
9
+ RenderedVariant,
10
+ SelectedVariant,
11
+ } from './context-pack-types';
12
+
13
+ type SkippedSelection = Extract<PackSelection, { status: 'skipped' }>;
14
+
15
+ interface TokenBudgetState {
16
+ remainingTokens: number;
17
+ overBudgetTokens: number;
18
+ }
19
+
20
+ const { renderVariant, sortByPriorityThenOrder } = contextPackCore;
21
+
22
+ function includedSelection(
23
+ variant: IncludedSelection['variant'],
24
+ rendered: RenderedVariant
25
+ ): IncludedSelection {
26
+ return {
27
+ status: 'included',
28
+ variant,
29
+ chars: rendered.chars,
30
+ estimatedTokens: rendered.estimatedTokens,
31
+ text: rendered.text,
32
+ };
33
+ }
34
+
35
+ function skippedSelection(reason: SkippedSelection['reason']): SkippedSelection {
36
+ return {
37
+ status: 'skipped',
38
+ variant: null,
39
+ reason,
40
+ chars: 0,
41
+ estimatedTokens: 0,
42
+ };
43
+ }
44
+
45
+ function shouldCompactRequired(
46
+ full: RenderedVariant,
47
+ compact: RenderedVariant | null,
48
+ remainingTokens: number
49
+ ): compact is RenderedVariant {
50
+ return (
51
+ Number.isFinite(remainingTokens) &&
52
+ compact !== null &&
53
+ compact.chars > 0 &&
54
+ full.estimatedTokens > remainingTokens &&
55
+ (compact.estimatedTokens <= remainingTokens || compact.estimatedTokens < full.estimatedTokens)
56
+ );
57
+ }
58
+
59
+ function selectRequiredVariant(
60
+ full: RenderedVariant,
61
+ compact: RenderedVariant | null,
62
+ remainingTokens: number
63
+ ): IncludedSelection {
64
+ if (full.chars === 0 && compact && compact.chars > 0) {
65
+ return includedSelection('compact', compact);
66
+ }
67
+
68
+ if (shouldCompactRequired(full, compact, remainingTokens)) {
69
+ return includedSelection('compact', compact);
70
+ }
71
+
72
+ return includedSelection('full', full);
73
+ }
74
+
75
+ function selectOptionalVariant(
76
+ full: RenderedVariant,
77
+ compact: RenderedVariant | null,
78
+ remainingTokens: number
79
+ ): PackSelection {
80
+ if (!Number.isFinite(remainingTokens) || full.estimatedTokens <= remainingTokens) {
81
+ return includedSelection('full', full);
82
+ }
83
+
84
+ if (compact && compact.chars > 0 && compact.estimatedTokens <= remainingTokens) {
85
+ return includedSelection('compact', compact);
86
+ }
87
+
88
+ return skippedSelection('budget');
89
+ }
90
+
91
+ function selectVariant(
92
+ pack: NormalizedContextPack,
93
+ remainingTokens: number,
94
+ cache: Map<string, RenderedVariant>
95
+ ): PackSelection {
96
+ const full = renderVariant(pack, 'full', cache);
97
+ const compact = pack.compact ? renderVariant(pack, 'compact', cache) : null;
98
+
99
+ if (full.chars === 0 && (!compact || compact.chars === 0)) {
100
+ return skippedSelection('empty');
101
+ }
102
+
103
+ return pack.required
104
+ ? selectRequiredVariant(full, compact, remainingTokens)
105
+ : selectOptionalVariant(full, compact, remainingTokens);
106
+ }
107
+
108
+ function buildDecision(
109
+ pack: NormalizedContextPack,
110
+ selection: PackSelection
111
+ ): MutableContextPackDecision {
112
+ return {
113
+ id: pack.id,
114
+ section: pack.section || null,
115
+ priority: pack.priority,
116
+ required: pack.required,
117
+ status: selection.status,
118
+ variant: selection.variant,
119
+ chars: selection.chars,
120
+ estimatedTokens: selection.estimatedTokens,
121
+ order: pack.order,
122
+ reason: 'reason' in selection ? selection.reason : null,
123
+ };
124
+ }
125
+
126
+ function consumeSelectionBudget(
127
+ selection: IncludedSelection,
128
+ budget: TokenBudgetState
129
+ ): TokenBudgetState {
130
+ if (!Number.isFinite(budget.remainingTokens)) {
131
+ return budget;
132
+ }
133
+
134
+ if (selection.estimatedTokens > budget.remainingTokens) {
135
+ return {
136
+ remainingTokens: 0,
137
+ overBudgetTokens:
138
+ budget.overBudgetTokens + selection.estimatedTokens - budget.remainingTokens,
139
+ };
140
+ }
141
+
142
+ return {
143
+ remainingTokens: budget.remainingTokens - selection.estimatedTokens,
144
+ overBudgetTokens: budget.overBudgetTokens,
145
+ };
146
+ }
147
+
148
+ function selectPacks(
149
+ normalized: readonly NormalizedContextPack[],
150
+ maxTokens: number | undefined
151
+ ): PackSelectionState {
152
+ const renderCache = new Map<string, RenderedVariant>();
153
+ const decisions = new Map<string, MutableContextPackDecision>();
154
+ const selected = new Map<string, SelectedVariant>();
155
+ let budget: TokenBudgetState = {
156
+ remainingTokens:
157
+ typeof maxTokens === 'number' && Number.isFinite(maxTokens) ? maxTokens : Infinity,
158
+ overBudgetTokens: 0,
159
+ };
160
+ const selectionOrder = normalized.slice().sort(sortByPriorityThenOrder);
161
+ for (const pack of selectionOrder) {
162
+ const selection = selectVariant(pack, budget.remainingTokens, renderCache);
163
+ decisions.set(pack.id, buildDecision(pack, selection));
164
+ if (selection.status !== 'included') {
165
+ continue;
166
+ }
167
+ selected.set(pack.id, {
168
+ text: selection.text,
169
+ chars: selection.chars,
170
+ estimatedTokens: selection.estimatedTokens,
171
+ variant: selection.variant,
172
+ });
173
+ budget = consumeSelectionBudget(selection, budget);
174
+ }
175
+
176
+ return {
177
+ renderCache,
178
+ decisions,
179
+ selected,
180
+ remainingTokens: budget.remainingTokens,
181
+ overBudgetTokens: budget.overBudgetTokens,
182
+ };
183
+ }
184
+
185
+ export = {
186
+ selectPacks,
187
+ };
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ const contextMetrics = require("./context-metrics");
3
+ const contextPackCore = require("./context-pack-core");
4
+ const { estimateTokensFromChars } = contextMetrics;
5
+ const { renderVariant, sortByPriorityDescThenOrderDesc } = contextPackCore;
6
+ const TRUNCATION_SUFFIX = '\n\n[Context truncated to fit limit]\n';
7
+ function truncateText(text, targetChars) {
8
+ if (text.length <= targetChars) {
9
+ return { text, truncated: false };
10
+ }
11
+ if (targetChars <= 0) {
12
+ return { text: '', truncated: true };
13
+ }
14
+ if (targetChars <= TRUNCATION_SUFFIX.length) {
15
+ return { text: text.slice(0, targetChars), truncated: true };
16
+ }
17
+ const sliceLength = targetChars - TRUNCATION_SUFFIX.length;
18
+ return {
19
+ text: text.slice(0, sliceLength) + TRUNCATION_SUFFIX,
20
+ truncated: true,
21
+ };
22
+ }
23
+ function compactOptionalPacks(packs, { selected, decisions, cache, maxChars }, startingChars) {
24
+ let currentChars = startingChars;
25
+ for (const pack of packs) {
26
+ if (currentChars <= maxChars)
27
+ break;
28
+ const decision = decisions.get(pack.id);
29
+ if (!decision || decision.variant === 'compact' || !pack.compact)
30
+ continue;
31
+ const compact = renderVariant(pack, 'compact', cache);
32
+ if (compact.chars === 0 || compact.chars >= decision.chars)
33
+ continue;
34
+ const previousChars = decision.chars;
35
+ selected.set(pack.id, { ...compact, variant: 'compact' });
36
+ decision.variant = 'compact';
37
+ decision.chars = compact.chars;
38
+ decision.estimatedTokens = compact.estimatedTokens;
39
+ decision.reason = decision.reason || 'max_chars';
40
+ currentChars -= previousChars - compact.chars;
41
+ currentChars = Math.max(0, currentChars);
42
+ }
43
+ return currentChars;
44
+ }
45
+ function dropOptionalPacks(packs, { selected, decisions, maxChars }, startingChars) {
46
+ let currentChars = startingChars;
47
+ for (const pack of packs) {
48
+ if (currentChars <= maxChars)
49
+ break;
50
+ if (!selected.has(pack.id))
51
+ continue;
52
+ const decision = decisions.get(pack.id);
53
+ currentChars -= decision?.chars || 0;
54
+ selected.delete(pack.id);
55
+ if (decision) {
56
+ decision.status = 'skipped';
57
+ decision.reason = decision.reason || 'max_chars';
58
+ decision.chars = 0;
59
+ decision.estimatedTokens = 0;
60
+ }
61
+ }
62
+ return currentChars;
63
+ }
64
+ function sortRequiredTruncationCandidates(packs, selected) {
65
+ return packs
66
+ .filter((pack) => selected.has(pack.id) && pack.required)
67
+ .sort((a, b) => {
68
+ const preserveDelta = (a.preserve ? 1 : 0) - (b.preserve ? 1 : 0);
69
+ if (preserveDelta !== 0)
70
+ return preserveDelta;
71
+ const sizeDelta = (selected.get(b.id)?.chars || 0) - (selected.get(a.id)?.chars || 0);
72
+ if (sizeDelta !== 0)
73
+ return sizeDelta;
74
+ return b.order - a.order;
75
+ });
76
+ }
77
+ function truncateRequiredPacks({ packs, selected, decisions, currentChars, maxChars, }) {
78
+ let overage = currentChars - maxChars;
79
+ const requiredCandidates = sortRequiredTruncationCandidates(packs, selected);
80
+ for (const pack of requiredCandidates) {
81
+ if (overage <= 0)
82
+ break;
83
+ const decision = decisions.get(pack.id);
84
+ const selectedPack = selected.get(pack.id);
85
+ if (!decision || !selectedPack)
86
+ continue;
87
+ const targetChars = Math.max(0, selectedPack.chars - overage);
88
+ const truncated = truncateText(selectedPack.text, targetChars);
89
+ if (truncated.text.length === selectedPack.chars)
90
+ continue;
91
+ const newChars = truncated.text.length;
92
+ overage -= selectedPack.chars - newChars;
93
+ selected.set(pack.id, {
94
+ text: truncated.text,
95
+ chars: newChars,
96
+ estimatedTokens: estimateTokensFromChars(newChars),
97
+ variant: selectedPack.variant,
98
+ });
99
+ decision.chars = newChars;
100
+ decision.estimatedTokens = estimateTokensFromChars(newChars);
101
+ decision.truncated = true;
102
+ decision.reason = decision.reason || 'max_chars';
103
+ }
104
+ }
105
+ function selectedChars(selected) {
106
+ return Array.from(selected.values()).reduce((sum, item) => sum + item.chars, 0);
107
+ }
108
+ function applyMaxCharsGuard({ packs, selected, decisions, cache, maxChars, totalChars, }) {
109
+ if (typeof maxChars !== 'number' || !Number.isFinite(maxChars) || totalChars <= maxChars) {
110
+ return {
111
+ applied: false,
112
+ beforeChars: totalChars,
113
+ afterChars: totalChars,
114
+ };
115
+ }
116
+ const includedOptional = packs
117
+ .filter((pack) => selected.has(pack.id) && !pack.required)
118
+ .sort(sortByPriorityDescThenOrderDesc);
119
+ const mutationParams = { selected, decisions, cache, maxChars };
120
+ let currentChars = compactOptionalPacks(includedOptional, mutationParams, totalChars);
121
+ currentChars = dropOptionalPacks(includedOptional, mutationParams, currentChars);
122
+ if (currentChars > maxChars) {
123
+ truncateRequiredPacks({
124
+ packs,
125
+ selected,
126
+ decisions,
127
+ currentChars,
128
+ maxChars,
129
+ });
130
+ }
131
+ return {
132
+ applied: true,
133
+ beforeChars: totalChars,
134
+ afterChars: selectedChars(selected),
135
+ };
136
+ }
137
+ module.exports = {
138
+ applyMaxCharsGuard,
139
+ };