blun-king-cli 9.1.643 → 9.1.644

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 (266) hide show
  1. package/LIESMICH.txt +18 -0
  2. package/README.md +20 -6
  3. package/bin/blun.js +3 -3
  4. package/bin/king.js +3 -3
  5. package/bin/launcher-mode.js +125 -126
  6. package/bin/launcher-runtime.js +603 -616
  7. package/bin/plugin-bootstrap.js +339 -405
  8. package/bin/rename-private-staging.cjs +2 -2
  9. package/bin/retired-bundled-plugins.cjs +84 -0
  10. package/bin/startup-security-host.cjs +105 -0
  11. package/bin/startup-security.cjs +9 -105
  12. package/bin/startup-warning-policy.cjs +22 -0
  13. package/bin/update-lease-installer.cjs +125 -0
  14. package/bin/update-lease-sockets.cjs +228 -0
  15. package/bin/update-lease.js +22 -327
  16. package/bin/verify-bundled-agent-sources.cjs +2 -4
  17. package/blun.mjs +18 -179
  18. package/bundled-agent-sources.json +1 -552
  19. package/package.json +1 -5
  20. package/tool-liveness642/receipt.json +1 -1
  21. package/worker-host.mjs +3 -119
  22. package/agent-spine-plugin/.claude-plugin/marketplace.json +0 -20
  23. package/agent-spine-plugin/.claude-plugin/plugin.json +0 -14
  24. package/agent-spine-plugin/.codex-plugin/plugin.json +0 -47
  25. package/agent-spine-plugin/.mcp.json +0 -8
  26. package/agent-spine-plugin/CHANGELOG.md +0 -1614
  27. package/agent-spine-plugin/LICENSE +0 -186
  28. package/agent-spine-plugin/README.md +0 -432
  29. package/agent-spine-plugin/assets/agentspine-banner.svg +0 -32
  30. package/agent-spine-plugin/bin/agentspine-mcp.js +0 -4
  31. package/agent-spine-plugin/bin/agentspine.js +0 -7
  32. package/agent-spine-plugin/blun.plugin.json +0 -68
  33. package/agent-spine-plugin/docs/acceptance.md +0 -61
  34. package/agent-spine-plugin/docs/artifact-evaluation.md +0 -93
  35. package/agent-spine-plugin/docs/host-integration.md +0 -193
  36. package/agent-spine-plugin/docs/preflight-recall.md +0 -71
  37. package/agent-spine-plugin/docs/preservation-contract.md +0 -53
  38. package/agent-spine-plugin/docs/quality-gates.md +0 -50
  39. package/agent-spine-plugin/docs/session-timeline.md +0 -112
  40. package/agent-spine-plugin/docs/source-roots.md +0 -113
  41. package/agent-spine-plugin/docs/world-model.md +0 -119
  42. package/agent-spine-plugin/hooks/codex.json +0 -47
  43. package/agent-spine-plugin/hooks/hooks.json +0 -106
  44. package/agent-spine-plugin/hooks/version.json +0 -5
  45. package/agent-spine-plugin/package.json +0 -77
  46. package/agent-spine-plugin/scripts/check-codex-install.js +0 -226
  47. package/agent-spine-plugin/scripts/check-hosts.js +0 -208
  48. package/agent-spine-plugin/scripts/check-install-hook.js +0 -227
  49. package/agent-spine-plugin/scripts/check-install-selfstarter.js +0 -154
  50. package/agent-spine-plugin/scripts/check-install.js +0 -478
  51. package/agent-spine-plugin/scripts/check-line-budget.js +0 -58
  52. package/agent-spine-plugin/scripts/check-syntax.js +0 -29
  53. package/agent-spine-plugin/scripts/github-actions.js +0 -11
  54. package/agent-spine-plugin/scripts/hermetic-process.js +0 -191
  55. package/agent-spine-plugin/scripts/release-check.js +0 -150
  56. package/agent-spine-plugin/scripts/run-acceptance.js +0 -19
  57. package/agent-spine-plugin/scripts/run-checks.js +0 -50
  58. package/agent-spine-plugin/scripts/run-tests-hermetic.js +0 -112
  59. package/agent-spine-plugin/skills/agent-spine/SKILL.md +0 -99
  60. package/agent-spine-plugin/spine-example/1-identity.md +0 -12
  61. package/agent-spine-plugin/spine-example/2-voice.md +0 -6
  62. package/agent-spine-plugin/spine-example/3-conduct.md +0 -8
  63. package/agent-spine-plugin/spine-example/4-history.md +0 -4
  64. package/agent-spine-plugin/src/cli-agent.js +0 -315
  65. package/agent-spine-plugin/src/cli-attention.js +0 -95
  66. package/agent-spine-plugin/src/cli-autonomy.js +0 -36
  67. package/agent-spine-plugin/src/cli-common.js +0 -71
  68. package/agent-spine-plugin/src/cli-continuity.js +0 -116
  69. package/agent-spine-plugin/src/cli-core.js +0 -128
  70. package/agent-spine-plugin/src/cli-diagnostics.js +0 -291
  71. package/agent-spine-plugin/src/cli-host.js +0 -21
  72. package/agent-spine-plugin/src/cli-learning.js +0 -320
  73. package/agent-spine-plugin/src/cli-premortem.js +0 -16
  74. package/agent-spine-plugin/src/cli-sharing.js +0 -230
  75. package/agent-spine-plugin/src/cli.js +0 -181
  76. package/agent-spine-plugin/src/codex-reader-launcher.js +0 -182
  77. package/agent-spine-plugin/src/hook.js +0 -500
  78. package/agent-spine-plugin/src/index.js +0 -108
  79. package/agent-spine-plugin/src/lib/acceptance.js +0 -435
  80. package/agent-spine-plugin/src/lib/action-lesson-recall.js +0 -118
  81. package/agent-spine-plugin/src/lib/attention-context.js +0 -167
  82. package/agent-spine-plugin/src/lib/attention-events.js +0 -113
  83. package/agent-spine-plugin/src/lib/attention-privacy.js +0 -72
  84. package/agent-spine-plugin/src/lib/attention-schema.js +0 -164
  85. package/agent-spine-plugin/src/lib/attention-storage.js +0 -93
  86. package/agent-spine-plugin/src/lib/attention.js +0 -164
  87. package/agent-spine-plugin/src/lib/audit-premortem.js +0 -223
  88. package/agent-spine-plugin/src/lib/audit.js +0 -401
  89. package/agent-spine-plugin/src/lib/authentication.js +0 -515
  90. package/agent-spine-plugin/src/lib/autonomy-policy.js +0 -110
  91. package/agent-spine-plugin/src/lib/autonomy-store.js +0 -202
  92. package/agent-spine-plugin/src/lib/autonomy.js +0 -8
  93. package/agent-spine-plugin/src/lib/briefing.js +0 -493
  94. package/agent-spine-plugin/src/lib/catalog-document-read.js +0 -51
  95. package/agent-spine-plugin/src/lib/catalog.js +0 -167
  96. package/agent-spine-plugin/src/lib/channel-continuity.js +0 -19
  97. package/agent-spine-plugin/src/lib/channel-runtime.js +0 -665
  98. package/agent-spine-plugin/src/lib/codex-installation.js +0 -231
  99. package/agent-spine-plugin/src/lib/codex-skill-installation.js +0 -211
  100. package/agent-spine-plugin/src/lib/context.js +0 -156
  101. package/agent-spine-plugin/src/lib/continuity.js +0 -338
  102. package/agent-spine-plugin/src/lib/coordination.js +0 -577
  103. package/agent-spine-plugin/src/lib/delivery-agent-usage.js +0 -231
  104. package/agent-spine-plugin/src/lib/delivery-assignment.js +0 -220
  105. package/agent-spine-plugin/src/lib/delivery-command-actions.js +0 -462
  106. package/agent-spine-plugin/src/lib/delivery-knowledge.js +0 -78
  107. package/agent-spine-plugin/src/lib/delivery-premortem-binding.js +0 -107
  108. package/agent-spine-plugin/src/lib/delivery-premortem-closure.js +0 -171
  109. package/agent-spine-plugin/src/lib/delivery-premortem-codec.js +0 -45
  110. package/agent-spine-plugin/src/lib/delivery-premortem-correction.js +0 -101
  111. package/agent-spine-plugin/src/lib/delivery-premortem-file.js +0 -21
  112. package/agent-spine-plugin/src/lib/delivery-premortem-index.js +0 -493
  113. package/agent-spine-plugin/src/lib/delivery-premortem-inspection.js +0 -37
  114. package/agent-spine-plugin/src/lib/delivery-premortem-recovery.js +0 -120
  115. package/agent-spine-plugin/src/lib/delivery-premortem-rejection.js +0 -65
  116. package/agent-spine-plugin/src/lib/delivery-premortem-results.js +0 -28
  117. package/agent-spine-plugin/src/lib/delivery-premortem-session-guard.js +0 -46
  118. package/agent-spine-plugin/src/lib/delivery-premortem-write-ledger.js +0 -285
  119. package/agent-spine-plugin/src/lib/delivery-premortem.js +0 -499
  120. package/agent-spine-plugin/src/lib/delivery-shell-heredoc.js +0 -115
  121. package/agent-spine-plugin/src/lib/delivery-shell-substitutions.js +0 -111
  122. package/agent-spine-plugin/src/lib/delivery-shell-wrapper.js +0 -64
  123. package/agent-spine-plugin/src/lib/delivery-target.js +0 -73
  124. package/agent-spine-plugin/src/lib/delivery-verification.js +0 -445
  125. package/agent-spine-plugin/src/lib/documents.js +0 -239
  126. package/agent-spine-plugin/src/lib/feed-transport.js +0 -499
  127. package/agent-spine-plugin/src/lib/filesystem-retry.js +0 -34
  128. package/agent-spine-plugin/src/lib/gateway-common.js +0 -68
  129. package/agent-spine-plugin/src/lib/gateway-control.js +0 -370
  130. package/agent-spine-plugin/src/lib/gateway-delivery.js +0 -103
  131. package/agent-spine-plugin/src/lib/gateway-execution.js +0 -350
  132. package/agent-spine-plugin/src/lib/gateway-group-response.js +0 -128
  133. package/agent-spine-plugin/src/lib/gateway-host-fencing.js +0 -92
  134. package/agent-spine-plugin/src/lib/gateway-host-lifecycle.js +0 -193
  135. package/agent-spine-plugin/src/lib/gateway-inspection.js +0 -85
  136. package/agent-spine-plugin/src/lib/gateway-knowledge.js +0 -129
  137. package/agent-spine-plugin/src/lib/gateway-policy-provenance.js +0 -197
  138. package/agent-spine-plugin/src/lib/gateway-premortem-disposition.js +0 -82
  139. package/agent-spine-plugin/src/lib/gateway-premortem.js +0 -363
  140. package/agent-spine-plugin/src/lib/gateway-prepared-host.js +0 -28
  141. package/agent-spine-plugin/src/lib/gateway-runs.js +0 -345
  142. package/agent-spine-plugin/src/lib/gateway-runtime-identity.js +0 -31
  143. package/agent-spine-plugin/src/lib/gateway-runtime-records.js +0 -21
  144. package/agent-spine-plugin/src/lib/gateway-runtime.js +0 -18
  145. package/agent-spine-plugin/src/lib/gateway-state-transaction.js +0 -356
  146. package/agent-spine-plugin/src/lib/gateway-state.js +0 -344
  147. package/agent-spine-plugin/src/lib/graph.js +0 -337
  148. package/agent-spine-plugin/src/lib/hook-artifact-guards.js +0 -356
  149. package/agent-spine-plugin/src/lib/hook-audit.js +0 -43
  150. package/agent-spine-plugin/src/lib/hook-briefing-use.js +0 -78
  151. package/agent-spine-plugin/src/lib/hook-context.js +0 -441
  152. package/agent-spine-plugin/src/lib/hook-final-message.js +0 -26
  153. package/agent-spine-plugin/src/lib/hook-input.js +0 -27
  154. package/agent-spine-plugin/src/lib/hook-output.js +0 -284
  155. package/agent-spine-plugin/src/lib/hook-premortem.js +0 -257
  156. package/agent-spine-plugin/src/lib/hook-process-advisory.js +0 -36
  157. package/agent-spine-plugin/src/lib/hook-protection.js +0 -95
  158. package/agent-spine-plugin/src/lib/hook-stop-verification.js +0 -84
  159. package/agent-spine-plugin/src/lib/hook-timeline.js +0 -102
  160. package/agent-spine-plugin/src/lib/host-instruction-budget.js +0 -11
  161. package/agent-spine-plugin/src/lib/https-transport.js +0 -392
  162. package/agent-spine-plugin/src/lib/identifier-analysis.js +0 -446
  163. package/agent-spine-plugin/src/lib/indexed-memory-offline.js +0 -40
  164. package/agent-spine-plugin/src/lib/indexed-memory.js +0 -298
  165. package/agent-spine-plugin/src/lib/knowledge-evidence.js +0 -431
  166. package/agent-spine-plugin/src/lib/learning-applications.js +0 -441
  167. package/agent-spine-plugin/src/lib/learning-artifact-evaluator.js +0 -114
  168. package/agent-spine-plugin/src/lib/learning-candidates.js +0 -274
  169. package/agent-spine-plugin/src/lib/learning-context.js +0 -137
  170. package/agent-spine-plugin/src/lib/learning-delivery-contracts.js +0 -310
  171. package/agent-spine-plugin/src/lib/learning-evaluation-contracts.js +0 -350
  172. package/agent-spine-plugin/src/lib/learning-evaluation-registration.js +0 -372
  173. package/agent-spine-plugin/src/lib/learning-evaluation-revocation.js +0 -292
  174. package/agent-spine-plugin/src/lib/learning-evidence-contracts.js +0 -321
  175. package/agent-spine-plugin/src/lib/learning-findings.js +0 -458
  176. package/agent-spine-plugin/src/lib/learning-measurement-contracts.js +0 -285
  177. package/agent-spine-plugin/src/lib/learning-measurements.js +0 -265
  178. package/agent-spine-plugin/src/lib/learning-outcome-contracts.js +0 -165
  179. package/agent-spine-plugin/src/lib/learning-outcomes.js +0 -343
  180. package/agent-spine-plugin/src/lib/learning-reconciliation.js +0 -290
  181. package/agent-spine-plugin/src/lib/learning-retry-contracts.js +0 -188
  182. package/agent-spine-plugin/src/lib/learning-schema.js +0 -229
  183. package/agent-spine-plugin/src/lib/learning-scope-targets.js +0 -424
  184. package/agent-spine-plugin/src/lib/learning-state-upgrade.js +0 -477
  185. package/agent-spine-plugin/src/lib/learning-status-configuration.js +0 -476
  186. package/agent-spine-plugin/src/lib/learning-storage.js +0 -203
  187. package/agent-spine-plugin/src/lib/learning-trial-recovery.js +0 -218
  188. package/agent-spine-plugin/src/lib/learning-validation-contracts.js +0 -375
  189. package/agent-spine-plugin/src/lib/learning-validation-renewal.js +0 -298
  190. package/agent-spine-plugin/src/lib/learning-validation-runtime.js +0 -234
  191. package/agent-spine-plugin/src/lib/learning.js +0 -36
  192. package/agent-spine-plugin/src/lib/lesson-recall-session.js +0 -172
  193. package/agent-spine-plugin/src/lib/mcp-autonomy-tools.js +0 -37
  194. package/agent-spine-plugin/src/lib/mcp-delivery-completion.js +0 -124
  195. package/agent-spine-plugin/src/lib/mcp-delivery-tools.js +0 -45
  196. package/agent-spine-plugin/src/lib/mcp-premortem.js +0 -68
  197. package/agent-spine-plugin/src/lib/mcp-runtime.js +0 -355
  198. package/agent-spine-plugin/src/lib/mcp-source-context.js +0 -61
  199. package/agent-spine-plugin/src/lib/mcp-timeline-tools.js +0 -240
  200. package/agent-spine-plugin/src/lib/mcp-world-tools.js +0 -52
  201. package/agent-spine-plugin/src/lib/object-transport.js +0 -206
  202. package/agent-spine-plugin/src/lib/owned-file-lock.js +0 -182
  203. package/agent-spine-plugin/src/lib/paths.js +0 -109
  204. package/agent-spine-plugin/src/lib/peer-transport.js +0 -283
  205. package/agent-spine-plugin/src/lib/persona-runtime.js +0 -581
  206. package/agent-spine-plugin/src/lib/pre-answer-timeline-recall.js +0 -104
  207. package/agent-spine-plugin/src/lib/preflight-delivery-id.js +0 -27
  208. package/agent-spine-plugin/src/lib/preflight.js +0 -688
  209. package/agent-spine-plugin/src/lib/project-portfolio.js +0 -176
  210. package/agent-spine-plugin/src/lib/runtime.js +0 -13
  211. package/agent-spine-plugin/src/lib/selfstarter-core.js +0 -382
  212. package/agent-spine-plugin/src/lib/selfstarter-jobs.js +0 -149
  213. package/agent-spine-plugin/src/lib/selfstarter-lease.js +0 -204
  214. package/agent-spine-plugin/src/lib/selfstarter-policy.js +0 -90
  215. package/agent-spine-plugin/src/lib/selfstarter-workspace.js +0 -111
  216. package/agent-spine-plugin/src/lib/selfstarter.js +0 -11
  217. package/agent-spine-plugin/src/lib/session-timeline-auth.js +0 -316
  218. package/agent-spine-plugin/src/lib/session-timeline-codex.js +0 -69
  219. package/agent-spine-plugin/src/lib/session-timeline-contract.js +0 -56
  220. package/agent-spine-plugin/src/lib/session-timeline-enrollment-source.js +0 -38
  221. package/agent-spine-plugin/src/lib/session-timeline-enrollment-storage.js +0 -128
  222. package/agent-spine-plugin/src/lib/session-timeline-enrollment-transport.js +0 -15
  223. package/agent-spine-plugin/src/lib/session-timeline-enrollment.js +0 -478
  224. package/agent-spine-plugin/src/lib/session-timeline-event-extract.js +0 -186
  225. package/agent-spine-plugin/src/lib/session-timeline-host-origin.js +0 -73
  226. package/agent-spine-plugin/src/lib/session-timeline-host-receipt.js +0 -117
  227. package/agent-spine-plugin/src/lib/session-timeline-invocation.js +0 -201
  228. package/agent-spine-plugin/src/lib/session-timeline-king.js +0 -93
  229. package/agent-spine-plugin/src/lib/session-timeline-prior.js +0 -57
  230. package/agent-spine-plugin/src/lib/session-timeline-provider.js +0 -36
  231. package/agent-spine-plugin/src/lib/session-timeline-query.js +0 -56
  232. package/agent-spine-plugin/src/lib/session-timeline-results.js +0 -56
  233. package/agent-spine-plugin/src/lib/session-timeline-root.js +0 -11
  234. package/agent-spine-plugin/src/lib/session-timeline-search.js +0 -82
  235. package/agent-spine-plugin/src/lib/session-timeline-sid-acl.js +0 -213
  236. package/agent-spine-plugin/src/lib/session-timeline-source-open.js +0 -30
  237. package/agent-spine-plugin/src/lib/session-timeline-source.js +0 -80
  238. package/agent-spine-plugin/src/lib/session-timeline-state.js +0 -45
  239. package/agent-spine-plugin/src/lib/session-timeline-transport.js +0 -42
  240. package/agent-spine-plugin/src/lib/session-timeline-windows-acl.js +0 -145
  241. package/agent-spine-plugin/src/lib/session-timeline.js +0 -462
  242. package/agent-spine-plugin/src/lib/sharing.js +0 -969
  243. package/agent-spine-plugin/src/lib/source-roots.js +0 -475
  244. package/agent-spine-plugin/src/lib/source-tree-scan.js +0 -178
  245. package/agent-spine-plugin/src/lib/sqlite-transport.js +0 -501
  246. package/agent-spine-plugin/src/lib/task-knowledge-context.js +0 -99
  247. package/agent-spine-plugin/src/lib/telegram-adapter.js +0 -119
  248. package/agent-spine-plugin/src/lib/timeline-continuation-update.js +0 -100
  249. package/agent-spine-plugin/src/lib/timeline-tool-guard.js +0 -225
  250. package/agent-spine-plugin/src/lib/timeline-user-feedback.js +0 -216
  251. package/agent-spine-plugin/src/lib/timeline-world-capture.js +0 -233
  252. package/agent-spine-plugin/src/lib/voice-runtime.js +0 -39
  253. package/agent-spine-plugin/src/lib/world-knowledge.js +0 -306
  254. package/agent-spine-plugin/src/lib/world-model.js +0 -333
  255. package/agent-spine-plugin/src/mcp.js +0 -456
  256. package/agent-spine-plugin/src/version.js +0 -1
  257. package/agent-spine-plugin/src/worker.js +0 -257
  258. package/bin/agentspine-king-goal-inbox.mjs +0 -127
  259. package/bin/agentspine-king-goal-intake.mjs +0 -106
  260. package/bin/agentspine-king-host-runner.mjs +0 -109
  261. package/bin/agentspine-king-snapshot-policy.cjs +0 -80
  262. package/bin/agentspine-king-status-policy.mjs +0 -226
  263. package/bin/agentspine-king-worker-host.mjs +0 -160
  264. package/bin/managed-plugin-selection.cjs +0 -141
  265. package/bin/verify-agent-behavior.cjs +0 -122
  266. package/bin/verify-agent-components.cjs +0 -104
@@ -1,344 +0,0 @@
1
- import { join } from "node:path";
2
- import { retainLegacyExecutionHolds, validExecutionHold } from "./gateway-host-fencing.js";
3
- import { buildCatalog } from "./catalog.js";
4
- import { matchesGatewayLaneLease, validGatewayLeaseExecution } from "./gateway-host-lifecycle.js";
5
- import * as goalPremortem from "./gateway-premortem.js";
6
- import {
7
- readGatewayStateJson, withGatewayStateLock, writeGatewayStateJson
8
- } from "./gateway-state-transaction.js";
9
- import { validStepKnowledgeState } from "./knowledge-evidence.js";
10
- import { projectStateDir } from "./paths.js";
11
- import {
12
- GATEWAY_POLICY_SCHEMA, GATEWAY_RUNTIME_SCHEMA, GOAL_PLAN_SCHEMA, GOAL_STATUSES,
13
- HEALTH_VALUES, ID_RE, OUTBOX_STATUSES, PLAN_STEP_STATUSES, QUEUE_STATUSES,
14
- ROUTE_RE, SECRET_RE, WAKE_KINDS, emptyPolicy, emptyRuntime, exactId,
15
- safeCheckpoint, safeText, sha256
16
- } from "./gateway-common.js";
17
- import {
18
- createExecutionDecision, validExecutionDecision, validExecutionOutcomeSequence,
19
- validGoalTransferProofs
20
- } from "./gateway-execution.js";
21
-
22
- export function validGoalPlan(plan, goal = null, root = null, premortemContractVersion = undefined) {
23
- if (!(plan && plan.schema === GOAL_PLAN_SCHEMA && plan.authority === "context-only-plan"
24
- && Number.isInteger(plan.revision) && plan.revision >= 0 && Array.isArray(plan.steps)
25
- && plan.steps.length > 0 && plan.steps.length <= 32 && /^[a-f0-9]{64}$/.test(plan.definitionsDigest || "")
26
- && (plan.currentStepId === null || ID_RE.test(plan.currentStepId || ""))
27
- && goalPremortem.validPlanPremortemContract(plan, premortemContractVersion))) return false;
28
- const ids = new Set(plan.steps.map((step) => step?.stepId));
29
- if (ids.size !== plan.steps.length || ids.has(undefined)) return false;
30
- for (const step of plan.steps) {
31
- if (!(ID_RE.test(step.stepId || "") && typeof step.title === "string" && step.title.length > 0 && step.title.length <= 500
32
- && (step.agentId === undefined || ID_RE.test(step.agentId || ""))
33
- && (step.resources === undefined || (Array.isArray(step.resources) && step.resources.length <= 16
34
- && new Set(step.resources).size === step.resources.length && step.resources.every((resource) => ID_RE.test(resource || ""))))
35
- && (step.execution === undefined || step.execution === null || validExecutionDecision(step.execution))
36
- && (step.executionOutcomes === undefined || (Array.isArray(step.executionOutcomes) && step.executionOutcomes.length <= 8
37
- && new Set(step.executionOutcomes.map((outcome) => outcome.outcomeId)).size === step.executionOutcomes.length
38
- && (step.executionOutcomes.length === 0 || (step.execution
39
- && validExecutionOutcomeSequence(step.execution, step.executionOutcomes)))))
40
- && typeof step.successCriterion === "string" && step.successCriterion.length > 0 && step.successCriterion.length <= 1000
41
- && Array.isArray(step.dependsOn) && new Set(step.dependsOn).size === step.dependsOn.length
42
- && step.dependsOn.every((dependency) => ids.has(dependency) && dependency !== step.stepId)
43
- && PLAN_STEP_STATUSES.has(step.status)
44
- && (step.checkpoint === null || (() => { try { safeCheckpoint(step.checkpoint); return true; } catch { return false; } })())
45
- && (step.blocker === null || (typeof step.blocker === "string" && step.blocker.length <= 500 && !SECRET_RE.test(step.blocker)))
46
- && (step.completedAt === null || Number.isFinite(new Date(step.completedAt).getTime()))
47
- && (step.completedByQueueId === null || ID_RE.test(step.completedByQueueId || ""))
48
- && goalPremortem.validGoalPremortemAttachments(step, goal, root, plan.definitionsDigest)
49
- && validStepKnowledgeState(step, plan.definitionsDigest)
50
- && Number.isFinite(new Date(step.updatedAt).getTime())
51
- && (step.executionOutcomes || []).every((outcome) => new Date(outcome.observedAt) <= new Date(step.updatedAt)))) return false;
52
- }
53
- const visiting = new Set(); const visited = new Set();
54
- const visit = (stepId) => {
55
- if (visiting.has(stepId)) return false;
56
- if (visited.has(stepId)) return true;
57
- visiting.add(stepId);
58
- const step = plan.steps.find((entry) => entry.stepId === stepId);
59
- if (!step.dependsOn.every(visit)) return false;
60
- visiting.delete(stepId); visited.add(stepId); return true;
61
- };
62
- if (!plan.steps.every((step) => visit(step.stepId))) return false;
63
- const completed = new Set(plan.steps.filter((step) => step.status === "completed").map((step) => step.stepId));
64
- if (plan.steps.some((step) => ["completed", "active", "blocked"].includes(step.status)
65
- && !step.dependsOn.every((dependency) => completed.has(dependency)))) return false;
66
- if (plan.steps.some((step) => (step.status === "completed") !== (step.completedAt !== null && step.completedByQueueId !== null))) return false;
67
- if (plan.steps.some((step) => (step.status === "blocked") !== (step.blocker !== null))) return false;
68
- if (plan.steps.some((step) => (step.knowledgeGaps || []).filter((gap) => gap.status === "open").length > 1)) return false;
69
- if (plan.steps.some((step) => (step.knowledgeGaps || []).some((gap) => gap.status === "open")
70
- && step.status !== "blocked")) return false;
71
- if (plan.steps.some((step) => step.execution && step.status === "completed"
72
- && !(step.executionOutcomes || []).some((outcome) => outcome.passed))) return false;
73
- if (plan.definitionsDigest !== sha256(JSON.stringify(goalPremortem.planDefinitionMaterial(plan.steps)))) return false;
74
- const current = plan.steps.filter((step) => ["active", "blocked"].includes(step.status));
75
- return current.length <= 1 && (plan.currentStepId === null
76
- ? current.length === 0 : current.length === 1 && current[0].stepId === plan.currentStepId);
77
- }
78
- export function activateNextPlanStep(plan, now) {
79
- const completed = new Set(plan.steps.filter((step) => step.status === "completed").map((step) => step.stepId));
80
- const next = plan.steps.find((step) => step.status === "pending" && step.dependsOn.every((dependency) => completed.has(dependency)));
81
- if (!next) { plan.currentStepId = null; return null; }
82
- next.status = "active"; next.updatedAt = now; plan.currentStepId = next.stepId; plan.revision += 1;
83
- return next;
84
- }
85
- export function createGoalPlan(steps, now, defaultAgentId, transferContext) {
86
- if (!Array.isArray(steps) || steps.length === 0 || steps.length > 32) throw new Error("goal plan requires 1-32 steps");
87
- const normalized = steps.map((step, index) => ({
88
- stepId: exactId(step?.stepId ?? step?.id, `steps[${index}].stepId`),
89
- agentId: exactId(step?.agentId ?? defaultAgentId, `steps[${index}].agentId`),
90
- resources: Array.isArray(step?.resources)
91
- ? step.resources.map((resource) => exactId(resource, `steps[${index}].resources`)) : [],
92
- execution: createExecutionDecision(step?.execution, `steps[${index}].execution`, transferContext),
93
- ...(transferContext.premortemContractVersion === 1 ? { premortemContractVersion: 1 } : {}),
94
- title: safeText(step?.title, `steps[${index}].title`, 500),
95
- successCriterion: safeText(step?.successCriterion, `steps[${index}].successCriterion`),
96
- dependsOn: Array.isArray(step?.dependsOn) ? step.dependsOn.map((dependency) => exactId(dependency, `steps[${index}].dependsOn`)) : [],
97
- status: "pending", checkpoint: null, blocker: null, completedAt: null, completedByQueueId: null,
98
- knowledgeGaps: [], selfHelpReports: [], selfHelpRequirements: [], executionOutcomes: [], updatedAt: now
99
- }));
100
- const plan = { schema: GOAL_PLAN_SCHEMA, ...(transferContext.premortemContractVersion === 1
101
- ? { premortemContractVersion: 1, premortemContract: goalPremortem.GOAL_PREMORTEM_CONTRACT } : {}),
102
- revision: 0, currentStepId: null, steps: normalized,
103
- definitionsDigest: sha256(JSON.stringify(goalPremortem.planDefinitionMaterial(normalized))), authority: "context-only-plan" };
104
- if (!validGoalPlan(plan, null, null, transferContext.premortemContractVersion)) throw new Error("goal plan must be an acyclic dependency graph with exact unique step IDs");
105
- activateNextPlanStep(plan, now);
106
- return plan;
107
- }
108
- export function currentPlanStep(goal) {
109
- return goal.plan?.steps.find((step) => step.stepId === goal.plan.currentStepId) || null;
110
- }
111
- export function planStepAgentId(goal, step) {
112
- return step?.agentId ?? goal.agentId;
113
- }
114
- export function planStepResources(step) {
115
- return Array.isArray(step?.resources) ? step.resources : [];
116
- }
117
- export function queuePlanStep(policy, item) {
118
- if (!item.goalStepId) return null;
119
- return policy.goals.find((goal) => goal.goalId === item.goalId)?.plan?.steps
120
- .find((step) => step.stepId === item.goalStepId) || null;
121
- }
122
- export function conflictingResources(policy, candidate, leased) {
123
- const wanted = new Set(planStepResources(queuePlanStep(policy, candidate)));
124
- if (!wanted.size || candidate.projectId !== leased.projectId || candidate.groupId !== leased.groupId) return [];
125
- return planStepResources(queuePlanStep(policy, leased)).filter((resource) => wanted.has(resource));
126
- }
127
- export function effectiveQueuePriority(policy, item) {
128
- if (!item.goalId) return item.priority;
129
- return policy.goals.find((goal) => goal.goalId === item.goalId)?.priority ?? item.priority;
130
- }
131
- export function planQueueKey(goalId, stepId, phase, suffix = "") {
132
- return ["goal", goalId, "step", stepId, phase, suffix].filter(Boolean).join(":");
133
- }
134
-
135
- export function newGoalQueue(goal, step, kind, key, current, availableAt = current) {
136
- return { queueId: "gateway-queue:" + sha256(key).slice(0, 32), dedupeKey: key, kind,
137
- agentId: planStepAgentId(goal, step), projectId: goal.projectId, groupId: goal.groupId, goalId: goal.goalId,
138
- goalStepId: step?.stepId || null, channelEventId: null, priority: goal.priority, status: "pending",
139
- attempts: 0, lease: null, availableAt, createdAt: current, updatedAt: current,
140
- completedAt: null, lastError: null, authority: "execution-state-only" };
141
- }
142
-
143
- export function validGoal(goal, root = null, premortemContractVersion = undefined) {
144
- return goal && ID_RE.test(goal.goalId || "") && ID_RE.test(goal.agentId || "")
145
- && ID_RE.test(goal.ownerSubjectId || "") && ID_RE.test(goal.projectId || "")
146
- && (goal.groupId === null || ID_RE.test(goal.groupId || "")) && GOAL_STATUSES.has(goal.status)
147
- && typeof goal.successCriterion === "string" && goal.successCriterion.length > 0
148
- && typeof goal.nextSafeStep === "string" && goal.nextSafeStep.length > 0
149
- && (goal.deadline === null || Number.isFinite(new Date(goal.deadline).getTime()))
150
- && Number.isInteger(goal.priority) && goal.priority >= 0 && goal.priority <= 100
151
- && (goal.checkpoint === null || (() => { try { safeCheckpoint(goal.checkpoint); return true; } catch { return false; } })())
152
- && (goal.blocker === null || (typeof goal.blocker === "string" && goal.blocker.length <= 500 && !SECRET_RE.test(goal.blocker)))
153
- && (goal.heartbeatAt === null || Number.isFinite(new Date(goal.heartbeatAt).getTime()))
154
- && goal.authority === "authenticated-goal-policy" && Number.isFinite(new Date(goal.createdAt).getTime())
155
- && Number.isFinite(new Date(goal.updatedAt).getTime())
156
- && (goal.plan === undefined || goal.plan === null
157
- || (validGoalPlan(goal.plan, goal, root, premortemContractVersion)
158
- && goal.plan.steps.every((step) => ["knowledgeGaps", "selfHelpReports", "selfHelpRequirements"]
159
- .every((field) => (step[field] || []).every((entry) => entry.goalId === goal.goalId)))
160
- && (goal.status !== "active" || (currentPlanStep(goal)?.status === "active" && goal.nextSafeStep === currentPlanStep(goal).title))
161
- && (goal.status !== "blocked" || currentPlanStep(goal)?.status === "blocked")
162
- && (goal.status !== "completed" || goal.plan.steps.every((step) => step.status === "completed"))));
163
- }
164
-
165
- export function validPolicyHistory(item, root = null) {
166
- if (!item || item.authority !== "authenticated-goal-policy" || !Number.isFinite(new Date(item.at).getTime())) return false;
167
- if (item.kind === "goal") return validGoal(item.value, root);
168
- return item.kind === "control" && item.value && typeof item.value.enabled === "boolean" && typeof item.value.killSwitch === "boolean";
169
- }
170
-
171
- export function validQueue(item) {
172
- return item && ID_RE.test(item.queueId || "") && WAKE_KINDS.has(item.kind)
173
- && ID_RE.test(item.dedupeKey || "") && item.queueId === "gateway-queue:" + sha256(item.dedupeKey).slice(0, 32)
174
- && ID_RE.test(item.agentId || "") && ID_RE.test(item.projectId || "")
175
- && (item.groupId === null || ID_RE.test(item.groupId || ""))
176
- && (item.goalId === null || ID_RE.test(item.goalId || ""))
177
- && (item.goalStepId === undefined || item.goalStepId === null || (item.goalId !== null && ID_RE.test(item.goalStepId || "")))
178
- && (item.channelEventId === null || ID_RE.test(item.channelEventId || ""))
179
- && QUEUE_STATUSES.has(item.status) && Number.isInteger(item.priority) && item.priority >= 0 && item.priority <= 100
180
- && Number.isInteger(item.attempts) && item.attempts >= 0 && item.attempts <= 20
181
- && item.authority === "execution-state-only" && Number.isFinite(new Date(item.createdAt).getTime())
182
- && Number.isFinite(new Date(item.availableAt).getTime()) && Number.isFinite(new Date(item.updatedAt).getTime())
183
- && (item.completedAt === null || Number.isFinite(new Date(item.completedAt).getTime()))
184
- && (item.lastError === null || (typeof item.lastError === "string" && item.lastError.length <= 500 && !SECRET_RE.test(item.lastError)))
185
- && validExecutionHold(item)
186
- && (item.lease === null || (item.status === "leased" && ID_RE.test(item.lease.workerId || "")
187
- && Number.isFinite(new Date(item.lease.claimedAt).getTime())
188
- && Number.isFinite(new Date(item.lease.expiresAt).getTime())
189
- && validGatewayLeaseExecution(item.lease)));
190
- }
191
-
192
- export function validOutbox(item) {
193
- return item && ID_RE.test(item.outboxId || "") && ID_RE.test(item.queueId || "")
194
- && ID_RE.test(item.idempotencyKey || "") && ID_RE.test(item.bindingId || "")
195
- && item.outboxId === "gateway-outbox:" + sha256(item.idempotencyKey).slice(0, 32)
196
- && ID_RE.test(item.eventId || "") && ROUTE_RE.test(item.provider || "") && ROUTE_RE.test(item.tenantId || "")
197
- && ROUTE_RE.test(item.accountId || "") && ROUTE_RE.test(item.chatId || "")
198
- && (item.threadId === null || ROUTE_RE.test(item.threadId || "")) && (item.replyTo === null || ROUTE_RE.test(item.replyTo || ""))
199
- && OUTBOX_STATUSES.has(item.status)
200
- && typeof item.text === "string" && item.text.length > 0 && item.text.length <= 16000 && !SECRET_RE.test(item.text)
201
- && Number.isInteger(item.attempts) && item.attempts >= 0 && item.attempts <= 20
202
- && item.authority === "delivery-state-only" && Number.isFinite(new Date(item.createdAt).getTime())
203
- && Number.isFinite(new Date(item.updatedAt).getTime()) && Number.isFinite(new Date(item.nextAttemptAt).getTime())
204
- && (item.deliveredAt === null || Number.isFinite(new Date(item.deliveredAt).getTime()))
205
- && (item.adapterReceipt === null || (typeof item.adapterReceipt === "string" && item.adapterReceipt.length <= 500))
206
- && (item.lastError === null || (typeof item.lastError === "string" && item.lastError.length <= 500 && !SECRET_RE.test(item.lastError)));
207
- }
208
-
209
- export function validLane(item) {
210
- return item && ID_RE.test(item.agentId || "") && ID_RE.test(item.queueId || "") && ID_RE.test(item.workerId || "")
211
- && new Set(["leased", "completed", "expired"]).has(item.status)
212
- && Number.isFinite(new Date(item.claimedAt).getTime()) && Number.isFinite(new Date(item.expiresAt).getTime())
213
- && Number.isFinite(new Date(item.updatedAt).getTime()) && item.authority === "execution-state-only";
214
- }
215
-
216
- export function validReceipt(item) {
217
- if (!(item && ID_RE.test(item.id || "") && ID_RE.test(item.kind || "") && ID_RE.test(item.objectId || "")
218
- && Number.isFinite(new Date(item.at).getTime()) && item.details && typeof item.details === "object"
219
- && !Array.isArray(item.details) && !SECRET_RE.test(JSON.stringify(item.details))
220
- && item.authority === "execution-state-only" && /^[a-f0-9]{64}$/.test(item.digest || ""))) return false;
221
- const material = { kind: item.kind, objectId: item.objectId, at: item.at, details: item.details, authority: "execution-state-only" };
222
- return item.digest === sha256(JSON.stringify(material)) && item.id === "gateway-receipt:" + item.digest.slice(0, 24);
223
- }
224
-
225
- export function validHistory(item) {
226
- if (!(item && new Set(["queue", "outbox"]).has(item.kind) && ID_RE.test(item.objectId || "")
227
- && ID_RE.test(item.transition || "") && Number.isFinite(new Date(item.at).getTime())
228
- && item.value && item.authority === "execution-state-only")) return false;
229
- return item.kind === "queue" ? validQueue(item.value) && item.objectId === item.value.queueId
230
- : validOutbox(item.value) && item.objectId === item.value.outboxId;
231
- }
232
-
233
- export function validHealth(health) {
234
- return health && ["gateway", "adapter", "scheduler", "queue", "worker", "host"].every((key) => HEALTH_VALUES.has(health[key]))
235
- && (health.lastTickAt === null || Number.isFinite(new Date(health.lastTickAt).getTime()))
236
- && (health.lastReconciledAt === null || Number.isFinite(new Date(health.lastReconciledAt).getTime()));
237
- }
238
-
239
- export function normalizePolicy(value, root, provenance = null, staged = false) {
240
- const contractValid = staged ? value?.schema === GATEWAY_POLICY_SCHEMA
241
- && goalPremortem.ensureGoalPremortemRegistry(value) : goalPremortem.ensureGoalPremortemPolicy(value, GATEWAY_POLICY_SCHEMA, provenance);
242
- if (!value || !contractValid || value.root !== root
243
- || !Number.isInteger(value.revision) || typeof value.enabled !== "boolean" || typeof value.killSwitch !== "boolean"
244
- || !Array.isArray(value.goals) || !Array.isArray(value.history)
245
- || value.goals.some((item) => !validGoal(item, root, goalPremortem.goalPremortemContractVersion(value, item.goalId)))
246
- || value.goals.some((item) => !validGoalTransferProofs(item, value.goals))
247
- || value.history.some((item) => !validPolicyHistory(item, root))) {
248
- throw new Error("gateway policy is invalid; autonomous runtime is disabled");
249
- }
250
- return value;
251
- }
252
-
253
- export function normalizeRuntime(value, root) {
254
- const baseValid = value && value.schema === GATEWAY_RUNTIME_SCHEMA && value.root === root && Number.isInteger(value.revision)
255
- && Array.isArray(value.queue) && Array.isArray(value.lanes) && Array.isArray(value.outbox)
256
- && Array.isArray(value.receipts) && Array.isArray(value.history);
257
- if (!baseValid) throw new Error("gateway runtime structure is invalid; worker is disabled");
258
- const invalid = [];
259
- if (!validHealth(value.health)) invalid.push("health");
260
- const queueIndex = value.queue.findIndex((item) => !validQueue(item));
261
- const laneIndex = value.lanes.findIndex((item) => !validLane(item));
262
- const outboxIndex = value.outbox.findIndex((item) => !validOutbox(item));
263
- const receiptIndex = value.receipts.findIndex((item) => !validReceipt(item));
264
- const historyIndex = value.history.findIndex((item) => !validHistory(item));
265
- const leaseBindingMismatch = value.lanes.some((lane) => lane?.status === "leased"
266
- && !matchesGatewayLaneLease(lane, value.queue.find((item) => item.queueId === lane.queueId)));
267
- if (queueIndex >= 0) invalid.push("queue:" + queueIndex);
268
- if (laneIndex >= 0) invalid.push("lanes:" + laneIndex);
269
- if (leaseBindingMismatch) invalid.push("lane-lease-binding");
270
- if (outboxIndex >= 0) invalid.push("outbox:" + outboxIndex);
271
- if (receiptIndex >= 0) invalid.push("receipts:" + receiptIndex);
272
- if (historyIndex >= 0) invalid.push("history:" + historyIndex);
273
- if (invalid.length) throw new Error("gateway runtime is invalid (" + invalid.join(",") + "); worker is disabled");
274
- retainLegacyExecutionHolds(value);
275
- return value;
276
- }
277
-
278
- export async function pathsFor(root, catalog = null) {
279
- catalog ||= await buildCatalog(root);
280
- const directory = await projectStateDir(catalog.root);
281
- return { catalog, directory, gatewayPolicyPath: join(directory, "gateway-policy.json"), gatewayRuntimePath: join(directory, "gateway-runtime.json") };
282
- }
283
-
284
- export const readJson = readGatewayStateJson;
285
- export const writeJson = writeGatewayStateJson;
286
-
287
- export async function withLock(paths, task) {
288
- return withGatewayStateLock(paths, task, { validatePair: (policy, runtime) => {
289
- normalizePolicy(policy, paths.catalog.root, null, true); normalizeRuntime(runtime, paths.catalog.root);
290
- const findings = gatewayRuntimeFindings(policy, runtime);
291
- if (findings.length) throw new Error("gateway state pair is invalid (" + findings.slice(0, 16).join(",") + ")");
292
- } });
293
- }
294
-
295
-
296
- export function gatewayRuntimeFindings(policy, runtime) {
297
- const findings = [];
298
- const active = new Set();
299
- const goalIds = new Set();
300
- for (const goal of policy.goals) {
301
- if (!validGoal(goal, policy.root)) findings.push("invalid-goal:" + (goal?.goalId || "unknown"));
302
- else if (!validGoalTransferProofs(goal, policy.goals)) findings.push("invalid-strategy-transfer:" + goal.goalId);
303
- if (goalIds.has(goal.goalId)) findings.push("duplicate-goal:" + goal.goalId);
304
- goalIds.add(goal.goalId);
305
- if (goal.status === "active" && active.has(goal.agentId)) findings.push("multiple-active-goals:" + goal.agentId);
306
- if (goal.status === "active") active.add(goal.agentId);
307
- }
308
- for (const item of policy.history) if (!validPolicyHistory(item, policy.root)) findings.push("invalid-gateway-policy-history");
309
- const queueIds = new Set(); const dedupeKeys = new Set();
310
- for (const item of runtime.queue) {
311
- if (!validQueue(item)) findings.push("invalid-queue-item:" + (item?.queueId || "unknown"));
312
- if (queueIds.has(item.queueId)) findings.push("duplicate-queue-item:" + item.queueId);
313
- if (dedupeKeys.has(item.dedupeKey)) findings.push("duplicate-queue-dedupe:" + item.dedupeKey);
314
- queueIds.add(item.queueId); dedupeKeys.add(item.dedupeKey);
315
- if (item.goalStepId) {
316
- const goal = policy.goals.find((entry) => entry.goalId === item.goalId);
317
- const step = goal?.plan?.steps.find((entry) => entry.stepId === item.goalStepId);
318
- if (!step) findings.push("orphan-goal-step:" + item.queueId);
319
- else if (item.agentId !== planStepAgentId(goal, step)) findings.push("goal-step-agent-mismatch:" + item.queueId);
320
- }
321
- }
322
- const outboxIds = new Set(); const idempotencyKeys = new Set();
323
- for (const item of runtime.outbox) {
324
- if (!validOutbox(item)) findings.push("invalid-outbox-item:" + (item?.outboxId || "unknown"));
325
- if (!queueIds.has(item.queueId)) findings.push("orphan-outbox-item:" + item.outboxId);
326
- if (outboxIds.has(item.outboxId)) findings.push("duplicate-outbox-item:" + item.outboxId);
327
- if (idempotencyKeys.has(item.idempotencyKey)) findings.push("duplicate-outbox-idempotency:" + item.idempotencyKey);
328
- outboxIds.add(item.outboxId); idempotencyKeys.add(item.idempotencyKey);
329
- }
330
- for (const item of runtime.receipts) if (!validReceipt(item)
331
- || (!queueIds.has(item.objectId) && !outboxIds.has(item.objectId))) findings.push("invalid-gateway-receipt:" + (item?.id || "unknown"));
332
- for (const item of runtime.history) if (!validHistory(item)) findings.push("invalid-gateway-history:" + (item?.objectId || "unknown"));
333
- const leased = runtime.lanes.filter((item) => item.status === "leased");
334
- for (const lane of runtime.lanes) {
335
- if (!validLane(lane)) findings.push("invalid-agent-lane:" + (lane?.queueId || "unknown"));
336
- const queue = runtime.queue.find((item) => item.queueId === lane.queueId);
337
- if (!queue || (lane.status === "leased" && !matchesGatewayLaneLease(lane, queue))) {
338
- findings.push("agent-lane-queue-mismatch:" + (lane.queueId || "unknown"));
339
- }
340
- }
341
- if (new Set(leased.map((item) => item.agentId)).size !== leased.length) findings.push("duplicate-agent-lane");
342
- if (!validHealth(runtime.health)) findings.push("invalid-gateway-health");
343
- return findings;
344
- }