@piagent/platform 1.3.0 → 1.4.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 (304) hide show
  1. package/CHANGELOG.md +56 -3
  2. package/README.md +3 -3
  3. package/architecture/layers.json +96 -1
  4. package/docs/README.md +1 -0
  5. package/docs/architecture.md +1 -0
  6. package/docs/authority-manifest.md +1 -0
  7. package/docs/auto-delegation-policy.md +1 -0
  8. package/docs/capability-packs.md +10 -2
  9. package/docs/command-reference-vietnamese.md +5 -4
  10. package/docs/context-engine.md +1 -0
  11. package/docs/context-window-policy.md +1 -0
  12. package/docs/distribution-standard.md +8 -7
  13. package/docs/herdr-workflow.md +1 -0
  14. package/docs/mcp-and-tools.md +14 -9
  15. package/docs/memory-policy.md +1 -0
  16. package/docs/model-options.md +1 -0
  17. package/docs/oauth-providers.md +1 -0
  18. package/docs/operator-manual-vietnamese.md +15 -12
  19. package/docs/package-architecture-notes.md +1 -0
  20. package/docs/project-adapters.md +1 -0
  21. package/docs/project-onboarding.md +1 -0
  22. package/docs/publishing-for-teams.md +1 -0
  23. package/docs/quality-benchmark.md +1 -0
  24. package/docs/quickstart-vietnamese.md +3 -2
  25. package/docs/release-install-policy.md +11 -7
  26. package/docs/runtime-harness-standard.md +1 -0
  27. package/docs/runtime-policy-design.md +1 -0
  28. package/docs/runtime-quality-baseline.md +1 -0
  29. package/docs/security-redaction-benchmark.md +1 -0
  30. package/docs/security-threat-model.md +2 -1
  31. package/docs/subagent-orchestration-capabilities.md +1 -0
  32. package/docs/subagents-and-multiagent.md +1 -0
  33. package/docs/task-implementation-contract.md +1 -0
  34. package/docs/task-lifecycle-vietnamese.md +1 -0
  35. package/docs/team-onboarding.md +3 -2
  36. package/docs/third-party-neutrality.md +26 -0
  37. package/docs/usage-observability.md +1 -0
  38. package/docs/vercel-docs-site.md +1 -0
  39. package/docs/workflow-recipes.md +1 -0
  40. package/docs-site/architecture.html +1 -1
  41. package/docs-site/benchmark.html +1 -1
  42. package/docs-site/commands.html +1 -1
  43. package/docs-site/context-engine.html +1 -1
  44. package/docs-site/en/architecture.html +1 -1
  45. package/docs-site/en/benchmark.html +1 -1
  46. package/docs-site/en/commands.html +1 -1
  47. package/docs-site/en/context-engine.html +1 -1
  48. package/docs-site/en/index.html +1 -1
  49. package/docs-site/en/mcp-approval.html +1 -1
  50. package/docs-site/en/mcp-auth.html +1 -1
  51. package/docs-site/en/mcp-servers.html +1 -1
  52. package/docs-site/en/mcp.html +1 -1
  53. package/docs-site/en/permissions.html +1 -1
  54. package/docs-site/en/profiles.html +1 -1
  55. package/docs-site/en/quickstart.html +1 -1
  56. package/docs-site/en/runtime.html +1 -1
  57. package/docs-site/en/scope.html +1 -1
  58. package/docs-site/en/security.html +1 -1
  59. package/docs-site/en/team.html +1 -1
  60. package/docs-site/en/whats-new.html +1 -1
  61. package/docs-site/en/workflows.html +1 -1
  62. package/docs-site/index.html +1 -1
  63. package/docs-site/mcp-approval.html +1 -1
  64. package/docs-site/mcp-auth.html +1 -1
  65. package/docs-site/mcp-servers.html +1 -1
  66. package/docs-site/mcp.html +1 -1
  67. package/docs-site/permissions.html +1 -1
  68. package/docs-site/profiles.html +1 -1
  69. package/docs-site/quickstart.html +1 -1
  70. package/docs-site/runtime.html +1 -1
  71. package/docs-site/scope.html +1 -1
  72. package/docs-site/security.html +1 -1
  73. package/docs-site/team.html +1 -1
  74. package/docs-site/whats-new.html +1 -1
  75. package/docs-site/workflows.html +1 -1
  76. package/evals/fixtures/mutation-provenance-record.invalid.json +17 -0
  77. package/evals/fixtures/mutation-provenance-record.valid.json +27 -0
  78. package/evals/fixtures/piagent-webui/approval-v1.invalid.json +54 -0
  79. package/evals/fixtures/piagent-webui/approval-v1.valid.json +58 -0
  80. package/evals/fixtures/piagent-webui/attachment-v1.invalid.json +25 -0
  81. package/evals/fixtures/piagent-webui/attachment-v1.valid.json +25 -0
  82. package/evals/fixtures/piagent-webui/capabilities-v1.invalid.json +51 -0
  83. package/evals/fixtures/piagent-webui/capabilities-v1.valid.json +122 -0
  84. package/evals/fixtures/piagent-webui/commit-summary-v1.invalid.json +10 -0
  85. package/evals/fixtures/piagent-webui/commit-summary-v1.valid.json +25 -0
  86. package/evals/fixtures/piagent-webui/control-command-v1.invalid.json +37 -0
  87. package/evals/fixtures/piagent-webui/control-command-v1.valid.json +35 -0
  88. package/evals/fixtures/piagent-webui/diff-v1.invalid.json +13 -0
  89. package/evals/fixtures/piagent-webui/diff-v1.valid.json +141 -0
  90. package/evals/fixtures/piagent-webui/gateway-capabilities-v1.invalid.json +35 -0
  91. package/evals/fixtures/piagent-webui/gateway-capabilities-v1.valid.json +35 -0
  92. package/evals/fixtures/piagent-webui/gateway-protocol-v1.invalid.json +14 -0
  93. package/evals/fixtures/piagent-webui/gateway-protocol-v1.valid.json +14 -0
  94. package/evals/fixtures/piagent-webui/handoff-history-v1.invalid.json +16 -0
  95. package/evals/fixtures/piagent-webui/handoff-history-v1.valid.json +16 -0
  96. package/evals/fixtures/piagent-webui/model-catalog-v1.invalid.json +31 -0
  97. package/evals/fixtures/piagent-webui/model-catalog-v1.valid.json +43 -0
  98. package/evals/fixtures/piagent-webui/queue-v1.invalid.json +29 -0
  99. package/evals/fixtures/piagent-webui/queue-v1.valid.json +44 -0
  100. package/evals/fixtures/piagent-webui/recovery-history-v1.invalid.json +17 -0
  101. package/evals/fixtures/piagent-webui/recovery-history-v1.valid.json +20 -0
  102. package/evals/fixtures/piagent-webui/release-monitor-v1.invalid.json +12 -0
  103. package/evals/fixtures/piagent-webui/release-monitor-v1.valid.json +29 -0
  104. package/evals/fixtures/piagent-webui/review-state-v1.invalid.json +25 -0
  105. package/evals/fixtures/piagent-webui/review-state-v1.valid.json +38 -0
  106. package/evals/fixtures/piagent-webui/runtime-event-v2.invalid.json +49 -0
  107. package/evals/fixtures/piagent-webui/runtime-event-v2.valid.json +49 -0
  108. package/evals/fixtures/piagent-webui/session-catalog-v1.invalid.json +17 -0
  109. package/evals/fixtures/piagent-webui/session-catalog-v1.valid.json +58 -0
  110. package/evals/fixtures/piagent-webui/session-command-v1.invalid.json +19 -0
  111. package/evals/fixtures/piagent-webui/session-command-v1.valid.json +19 -0
  112. package/evals/fixtures/piagent-webui/snapshot-v1.invalid.json +5 -0
  113. package/evals/fixtures/piagent-webui/snapshot-v1.valid.json +191 -0
  114. package/evals/fixtures/piagent-webui/source-change-v1.invalid.json +57 -0
  115. package/evals/fixtures/piagent-webui/source-change-v1.valid.json +98 -0
  116. package/evals/fixtures/piagent-webui/source-mutation-v1.invalid.json +19 -0
  117. package/evals/fixtures/piagent-webui/source-mutation-v1.valid.json +38 -0
  118. package/evals/fixtures/piagent-webui/source-revert-v1.invalid.json +20 -0
  119. package/evals/fixtures/piagent-webui/source-revert-v1.valid.json +62 -0
  120. package/evals/fixtures/piagent-webui/subagent-tree-v1.invalid.json +26 -0
  121. package/evals/fixtures/piagent-webui/subagent-tree-v1.valid.json +39 -0
  122. package/evals/fixtures/piagent-webui/task-index-v1.invalid.json +15 -0
  123. package/evals/fixtures/piagent-webui/task-index-v1.valid.json +15 -0
  124. package/evals/fixtures/piagent-webui/task-timeline-v1.invalid.json +14 -0
  125. package/evals/fixtures/piagent-webui/task-timeline-v1.valid.json +16 -0
  126. package/evals/fixtures/piagent-webui/transcript-v1.invalid.json +35 -0
  127. package/evals/fixtures/piagent-webui/transcript-v1.valid.json +55 -0
  128. package/evals/fixtures/task-baseline-manifest.invalid.json +16 -0
  129. package/evals/fixtures/task-baseline-manifest.valid.json +41 -0
  130. package/evals/fixtures/verifier-file-snapshot.invalid.json +21 -0
  131. package/evals/fixtures/verifier-file-snapshot.valid.json +21 -0
  132. package/package.json +30 -3
  133. package/packages/piagent-core/benchmark/benchmark-candidate.d.ts +1 -0
  134. package/packages/piagent-core/benchmark/benchmark-forensics.d.ts +1 -0
  135. package/packages/piagent-core/benchmark/benchmark-ledger.d.ts +4 -0
  136. package/packages/piagent-core/benchmark/benchmark-record-validation.d.ts +1 -0
  137. package/packages/piagent-core/benchmark/benchmark-runtime.d.ts +1 -0
  138. package/packages/piagent-core/capabilities/capability-core.js +2 -2
  139. package/packages/piagent-core/capabilities/capability-verification-cache.js +280 -0
  140. package/packages/piagent-core/capabilities/runtime-integrity.js +153 -1
  141. package/packages/piagent-core/extensions/document-intake.ts +24 -10
  142. package/packages/piagent-core/extensions/piagent-guard.ts +90 -73
  143. package/packages/piagent-core/extensions/redaction-core.d.ts +2 -0
  144. package/packages/piagent-core/extensions/state-retention.js +77 -44
  145. package/packages/piagent-core/extensions/task-journal.js +30 -1
  146. package/packages/piagent-core/extensions/task-state.js +33 -36
  147. package/packages/piagent-core/mcp/mcp-server-catalog.js +5 -5
  148. package/packages/piagent-core/package.json +1 -1
  149. package/packages/piagent-core/runtime/hooks/session-start-hook.ts +2 -2
  150. package/packages/piagent-core/runtime/hooks/tool-call-hook.ts +5 -2
  151. package/packages/piagent-core/runtime/hooks/tool-result-hook.ts +20 -19
  152. package/packages/piagent-core/runtime/hooks/tool-result-metadata.ts +13 -0
  153. package/packages/piagent-core/runtime/input/chat-images.ts +2 -2
  154. package/packages/piagent-core/runtime/inspection/activity-event-adapter.ts +94 -0
  155. package/packages/piagent-core/runtime/inspection/approval-broker.ts +318 -0
  156. package/packages/piagent-core/runtime/inspection/commit-summary-projection.ts +59 -0
  157. package/packages/piagent-core/runtime/inspection/context-telemetry-inspection.ts +37 -0
  158. package/packages/piagent-core/runtime/inspection/criteria-links.ts +295 -0
  159. package/packages/piagent-core/runtime/inspection/diff-projection.ts +499 -0
  160. package/packages/piagent-core/runtime/inspection/git-filter-safety.ts +38 -0
  161. package/packages/piagent-core/runtime/inspection/git-numstat-adapter.ts +71 -0
  162. package/packages/piagent-core/runtime/inspection/git-status-adapter.ts +493 -0
  163. package/packages/piagent-core/runtime/inspection/git-tree-adapter.ts +108 -0
  164. package/packages/piagent-core/runtime/inspection/mcp-control.ts +10 -0
  165. package/packages/piagent-core/runtime/inspection/mutation-provenance-contract.ts +131 -0
  166. package/packages/piagent-core/runtime/inspection/mutation-provenance-recorder.ts +48 -0
  167. package/packages/piagent-core/runtime/inspection/mutation-provenance-store.ts +161 -0
  168. package/packages/piagent-core/runtime/inspection/project-runtime-inspection.ts +36 -0
  169. package/packages/piagent-core/runtime/inspection/review-state-contract.ts +108 -0
  170. package/packages/piagent-core/runtime/inspection/review-state-projection.ts +93 -0
  171. package/packages/piagent-core/runtime/inspection/review-state-store.ts +112 -0
  172. package/packages/piagent-core/runtime/inspection/runtime-event-store.ts +262 -0
  173. package/packages/piagent-core/runtime/inspection/same-process-bridge-proof.ts +103 -0
  174. package/packages/piagent-core/runtime/inspection/source-change-projection.ts +471 -0
  175. package/packages/piagent-core/runtime/inspection/source-evidence-contract.ts +139 -0
  176. package/packages/piagent-core/runtime/inspection/source-evidence-store.ts +315 -0
  177. package/packages/piagent-core/runtime/inspection/source-handoff-store.ts +70 -0
  178. package/packages/piagent-core/runtime/inspection/source-inspection-plan.ts +34 -0
  179. package/packages/piagent-core/runtime/inspection/source-mutation-projection.ts +164 -0
  180. package/packages/piagent-core/runtime/inspection/source-mutation-store.ts +168 -0
  181. package/packages/piagent-core/runtime/inspection/source-open-target.ts +38 -0
  182. package/packages/piagent-core/runtime/inspection/source-revert-projection.ts +147 -0
  183. package/packages/piagent-core/runtime/inspection/task-baseline-start-capture.ts +22 -0
  184. package/packages/piagent-core/runtime/inspection/task-compaction-history.ts +160 -0
  185. package/packages/piagent-core/runtime/inspection/task-control-journal.ts +131 -0
  186. package/packages/piagent-core/runtime/inspection/task-handoff-history.ts +95 -0
  187. package/packages/piagent-core/runtime/inspection/task-provenance-projection.ts +60 -0
  188. package/packages/piagent-core/runtime/inspection/task-recovery-timeline.ts +88 -0
  189. package/packages/piagent-core/runtime/inspection/task-run-index.ts +87 -0
  190. package/packages/piagent-core/runtime/inspection/task-source-projection.ts +401 -0
  191. package/packages/piagent-core/runtime/inspection/task-subagent-tree.ts +101 -0
  192. package/packages/piagent-core/runtime/inspection/text-diff.ts +60 -0
  193. package/packages/piagent-core/runtime/inspection/verifier-snapshot-contract.ts +114 -0
  194. package/packages/piagent-core/runtime/inspection/verifier-snapshot-store.ts +189 -0
  195. package/packages/piagent-core/runtime/inspection/webui-snapshot.ts +320 -0
  196. package/packages/piagent-core/runtime/inspection/workspace-file-reader.ts +132 -0
  197. package/packages/piagent-core/runtime/inspection/workspace-source-projection.ts +96 -0
  198. package/packages/piagent-core/runtime/inspection/zero-turn-conformance.ts +175 -0
  199. package/packages/piagent-core/runtime/orchestration/owned-work-budget.ts +68 -4
  200. package/packages/piagent-core/runtime/policy/source-index-transaction.ts +173 -0
  201. package/packages/piagent-core/runtime/policy/source-mutation-guard-binding.ts +42 -0
  202. package/packages/piagent-core/runtime/policy/source-mutation-guard.ts +184 -0
  203. package/packages/piagent-core/runtime/policy/source-worktree-transaction.ts +105 -0
  204. package/packages/piagent-core/runtime/product/activity-inspector-footer.ts +5 -3
  205. package/packages/piagent-core/runtime/product/activity-inspector.ts +85 -343
  206. package/packages/piagent-core/runtime/recovery/resume-state.ts +38 -1
  207. package/packages/piagent-core/runtime/registration/activity-inspector-command.ts +68 -14
  208. package/packages/piagent-core/runtime/registration/task-start-tool.ts +13 -14
  209. package/packages/piagent-core/runtime/session/model-authorship-state.ts +31 -3
  210. package/packages/piagent-core/runtime/session/runtime-state.ts +11 -1
  211. package/packages/piagent-webui/dist/client/assets/index-DjMntaK5.js +172 -0
  212. package/packages/piagent-webui/dist/client/assets/index-DwKCWbwd.css +1 -0
  213. package/packages/piagent-webui/dist/client/index.html +16 -0
  214. package/packages/piagent-webui/extension/attachment-store.ts +323 -0
  215. package/packages/piagent-webui/extension/held-message-queue.ts +311 -0
  216. package/packages/piagent-webui/extension/lifecycle-controller.ts +408 -0
  217. package/packages/piagent-webui/extension/lifecycle-event-adapter.ts +23 -0
  218. package/packages/piagent-webui/extension/piagent-webui.ts +470 -0
  219. package/packages/piagent-webui/extension/review-controller.ts +160 -0
  220. package/packages/piagent-webui/extension/same-session-bridge.ts +500 -0
  221. package/packages/piagent-webui/extension/session-options-controller.ts +281 -0
  222. package/packages/piagent-webui/extension/session-stream-adapter.ts +249 -0
  223. package/packages/piagent-webui/extension/source-mutation-controller.ts +215 -0
  224. package/packages/piagent-webui/extension/source-open-controller.ts +136 -0
  225. package/packages/piagent-webui/extension/source-revert-controller.ts +189 -0
  226. package/packages/piagent-webui/extension/terminal-session-adapter.ts +73 -0
  227. package/packages/piagent-webui/extension/vscode-handoff.ts +51 -0
  228. package/packages/piagent-webui/gateway/control-socket.ts +92 -0
  229. package/packages/piagent-webui/gateway/gateway-events.ts +60 -0
  230. package/packages/piagent-webui/gateway/gateway-protocol-service.ts +64 -0
  231. package/packages/piagent-webui/gateway/gateway-service.ts +215 -0
  232. package/packages/piagent-webui/gateway/gateway-session-stream.ts +110 -0
  233. package/packages/piagent-webui/gateway/mcp-auth-broker.ts +158 -0
  234. package/packages/piagent-webui/gateway/native-project-picker.ts +29 -0
  235. package/packages/piagent-webui/gateway/pi-host.ts +41 -0
  236. package/packages/piagent-webui/gateway/profile-state.ts +1 -0
  237. package/packages/piagent-webui/gateway/project-registry.ts +108 -0
  238. package/packages/piagent-webui/gateway/provider-auth-broker.ts +172 -0
  239. package/packages/piagent-webui/gateway/session-catalog.ts +128 -0
  240. package/packages/piagent-webui/gateway/session-command-controller.ts +237 -0
  241. package/packages/piagent-webui/gateway/session-command-store.ts +224 -0
  242. package/packages/piagent-webui/gateway/session-inspection-registry.ts +204 -0
  243. package/packages/piagent-webui/gateway/session-lease-store.ts +1 -0
  244. package/packages/piagent-webui/gateway/session-metadata-store.ts +152 -0
  245. package/packages/piagent-webui/gateway/session-runtime-supervisor.ts +485 -0
  246. package/packages/piagent-webui/ownership/gateway-runtime-context.ts +8 -0
  247. package/packages/piagent-webui/ownership/profile-state.ts +117 -0
  248. package/packages/piagent-webui/ownership/session-lease-store.ts +329 -0
  249. package/packages/piagent-webui/ownership/session-refs.ts +8 -0
  250. package/packages/piagent-webui/package.json +33 -0
  251. package/packages/piagent-webui/server/README.md +4 -0
  252. package/packages/piagent-webui/server/benchmark-release-monitor.ts +230 -0
  253. package/packages/piagent-webui/server/core-inspection-provider.ts +403 -0
  254. package/packages/piagent-webui/server/gateway-websocket.ts +143 -0
  255. package/packages/piagent-webui/server/http-security.ts +27 -0
  256. package/packages/piagent-webui/server/ipc-read-model-client.ts +89 -0
  257. package/packages/piagent-webui/server/loopback-server.ts +368 -0
  258. package/packages/piagent-webui/server/read-model-provider.ts +37 -0
  259. package/packages/piagent-webui/server/read-only-router.ts +105 -0
  260. package/packages/piagent-webui/server/session-auth.ts +88 -0
  261. package/packages/piagent-webui/server/sidecar-main.ts +86 -0
  262. package/packages/piagent-webui/server/sse-hub.ts +76 -0
  263. package/packages/piagent-webui/server/static-bundle.ts +40 -0
  264. package/packages/piagent-webui/server/transcript-projection.ts +110 -0
  265. package/schemas/mutation-provenance-record.schema.json +100 -0
  266. package/schemas/piagent-webui/approval-v1.schema.json +654 -0
  267. package/schemas/piagent-webui/attachment-v1.schema.json +267 -0
  268. package/schemas/piagent-webui/capabilities-v1.schema.json +729 -0
  269. package/schemas/piagent-webui/catalog-v1.json +240 -0
  270. package/schemas/piagent-webui/commit-summary-v1.schema.json +63 -0
  271. package/schemas/piagent-webui/common-v1.schema.json +479 -0
  272. package/schemas/piagent-webui/control-command-v1.schema.json +1780 -0
  273. package/schemas/piagent-webui/diff-v1.schema.json +826 -0
  274. package/schemas/piagent-webui/gateway-capabilities-v1.schema.json +136 -0
  275. package/schemas/piagent-webui/gateway-protocol-v1.schema.json +456 -0
  276. package/schemas/piagent-webui/handoff-history-v1.schema.json +92 -0
  277. package/schemas/piagent-webui/model-catalog-v1.schema.json +95 -0
  278. package/schemas/piagent-webui/queue-v1.schema.json +99 -0
  279. package/schemas/piagent-webui/recovery-history-v1.schema.json +144 -0
  280. package/schemas/piagent-webui/release-monitor-v1.schema.json +173 -0
  281. package/schemas/piagent-webui/review-state-v1.schema.json +206 -0
  282. package/schemas/piagent-webui/runtime-event-v2.schema.json +5206 -0
  283. package/schemas/piagent-webui/session-catalog-v1.schema.json +222 -0
  284. package/schemas/piagent-webui/session-command-v1.schema.json +390 -0
  285. package/schemas/piagent-webui/snapshot-v1.schema.json +2622 -0
  286. package/schemas/piagent-webui/source-change-v1.schema.json +763 -0
  287. package/schemas/piagent-webui/source-mutation-v1.schema.json +96 -0
  288. package/schemas/piagent-webui/source-revert-v1.schema.json +130 -0
  289. package/schemas/piagent-webui/subagent-tree-v1.schema.json +88 -0
  290. package/schemas/piagent-webui/task-index-v1.schema.json +74 -0
  291. package/schemas/piagent-webui/task-timeline-v1.schema.json +49 -0
  292. package/schemas/piagent-webui/transcript-v1.schema.json +243 -0
  293. package/schemas/task-baseline-manifest.schema.json +122 -0
  294. package/schemas/verifier-file-snapshot.schema.json +81 -0
  295. package/scripts/check-architecture.mjs +1 -0
  296. package/scripts/check-doc-languages.mjs +19 -2
  297. package/scripts/check-third-party-neutrality.mjs +43 -0
  298. package/scripts/install-global.sh +1 -1
  299. package/scripts/piagent-cli.mjs +17 -3
  300. package/scripts/piagent-dashboard.mjs +208 -0
  301. package/scripts/piagent-webui-launcher.mjs +121 -0
  302. package/scripts/typescript-loader.mjs +26 -1
  303. package/scripts/verify-local.sh +253 -0
  304. package/scripts/verify-release-identity.mjs +4 -1
@@ -0,0 +1,311 @@
1
+ import { createHash, randomBytes } from "node:crypto";
2
+
3
+ import { redactSensitiveText } from "../../piagent-core/extensions/redaction-core.js";
4
+ import {
5
+ chatContentDigest,
6
+ controlActionDigest,
7
+ type BridgeIdentity,
8
+ type BridgeRevisions,
9
+ type ChatReceipt,
10
+ type NewOperationChatCommand,
11
+ type SameSessionPiBridge
12
+ } from "./same-session-bridge.ts";
13
+
14
+ export const WEBUI_QUEUE_ENTRY_TYPE = "piagent-webui-held-queue-v1";
15
+ const REF = /^[A-Za-z0-9][A-Za-z0-9._~-]{0,159}$/;
16
+ const PUBLIC_REF = /^[A-Za-z0-9][A-Za-z0-9._:@~-]{0,159}$/;
17
+ const REVISION = /^[A-Za-z0-9][A-Za-z0-9._:~-]{0,159}$/;
18
+ const DIGEST = /^sha256:[a-f0-9]{64}$/;
19
+ const IDEMPOTENCY = /^[A-Za-z0-9._~-]{32,128}$/;
20
+ const MAX_ITEMS = 100;
21
+
22
+ type QueueAction = "queue.update" | "queue.delete" | "queue.dispatch";
23
+ export type QueueControlReceipt = Omit<ChatReceipt, "action" | "resultCode"> & {
24
+ action: "chat.send" | QueueAction;
25
+ resultCode: ChatReceipt["resultCode"] | "held" | "updated" | "deleted";
26
+ };
27
+ type QueueCommand = {
28
+ schemaVersion: 1; version: "piagent-webui-control-v1"; messageType: "command"; commandId: string; idempotencyKey: string;
29
+ requestedAt: string; expiresAt: string; capabilityScope: "control.chat"; action: "chat.send" | QueueAction; actionDigest: string;
30
+ identity: BridgeIdentity; expectedRevisions: BridgeRevisions & { workspacePreimage: null; indexPreimage: null; patchPreimage: null };
31
+ payload: Record<string, unknown>;
32
+ };
33
+ type QueueItem = {
34
+ queueItemRef: string; messageRequestId: string; text: string; attachmentRefs: string[]; contentDigest: string;
35
+ state: "held" | "quarantined"; createdAt: string; updatedAt: string; reasonCode: string | null;
36
+ };
37
+ type QueueProjectionItem = {
38
+ queueItemRef: string; messageRequestId: string; position: number; state: "held" | "quarantined"; preview: string; redacted: boolean;
39
+ truncated: boolean; attachmentCount: number; previewDigest: string; createdAt: string; updatedAt: string; reasonCode: string | null;
40
+ };
41
+ type QueueProjection = {
42
+ schemaVersion: 1; version: "piagent-webui-queue-v1"; generatedAt: string; identity: BridgeIdentity; revision: BridgeRevisions;
43
+ state: "ready" | "unavailable"; heldCount: number; quarantinedCount: number; items: QueueProjectionItem[];
44
+ reasonCode: string | null;
45
+ };
46
+
47
+ function sha(value: string): string { return `sha256:${createHash("sha256").update(value).digest("hex")}`; }
48
+ function canonical(value: unknown): string {
49
+ if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
50
+ if (value && typeof value === "object") return `{${Object.entries(value as Record<string, unknown>).sort(([a], [b]) => a.localeCompare(b))
51
+ .map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(",")}}`;
52
+ return JSON.stringify(value);
53
+ }
54
+ function copy<T>(value: T): T { return structuredClone(value); }
55
+ function same(left: unknown, right: unknown): boolean { return canonical(left) === canonical(right); }
56
+ function exactKeys(value: unknown, keys: string[]): boolean {
57
+ return Boolean(value && typeof value === "object" && !Array.isArray(value)
58
+ && same(Object.keys(value as object).sort(), [...keys].sort()));
59
+ }
60
+ function timestamp(value: unknown): value is string {
61
+ if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(value)) return false;
62
+ const parsed = Date.parse(value);
63
+ return Number.isFinite(parsed) && new Date(parsed).toISOString() === value;
64
+ }
65
+ function validIdentity(value: unknown): value is BridgeIdentity {
66
+ if (!exactKeys(value, ["projectRef", "runtimeInstanceId", "sessionRef", "taskId", "taskRunId", "agentOperationId", "toolCallId"])) return false;
67
+ const identity = value as BridgeIdentity;
68
+ return [identity.projectRef, identity.runtimeInstanceId, identity.sessionRef].every((item) => typeof item === "string" && REF.test(item))
69
+ && [identity.taskId, identity.taskRunId].every((item) => item === null || PUBLIC_REF.test(item))
70
+ && (identity.agentOperationId === null || REF.test(identity.agentOperationId)) && identity.toolCallId === null
71
+ && (identity.taskRunId === null || identity.taskId !== null);
72
+ }
73
+ function validRevisions(value: unknown): value is QueueCommand["expectedRevisions"] {
74
+ if (!exactKeys(value, ["runtimeRevision", "taskRevision", "controlRevision", "workspaceRevision", "indexRevision", "approvalRevision",
75
+ "sessionOptionRevision", "queueRevision", "workspacePreimage", "indexPreimage", "patchPreimage"])) return false;
76
+ const revisions = value as QueueCommand["expectedRevisions"];
77
+ return REVISION.test(revisions.runtimeRevision) && REVISION.test(revisions.queueRevision ?? "")
78
+ && [revisions.taskRevision, revisions.controlRevision, revisions.workspaceRevision, revisions.indexRevision, revisions.approvalRevision,
79
+ revisions.sessionOptionRevision].every((item) => item === null || REVISION.test(item))
80
+ && revisions.workspacePreimage === null && revisions.indexPreimage === null && revisions.patchPreimage === null;
81
+ }
82
+ function safePreview(text: string): { preview: string; redacted: boolean; truncated: boolean } {
83
+ const cleaned = text.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]/g, "");
84
+ const redacted = redactSensitiveText(cleaned);
85
+ return { preview: redacted.text.slice(0, 4_000), redacted: redacted.redacted, truncated: redacted.text.length > 4_000 };
86
+ }
87
+
88
+ export class HeldMessageQueue {
89
+ readonly #bridge: SameSessionPiBridge;
90
+ readonly #appendEntry: (customType: string, data: unknown) => void;
91
+ readonly #now: () => Date;
92
+ #sessionRef: string | null = null;
93
+ #lastIdentity: BridgeIdentity | null = null;
94
+ #lastRevisions: BridgeRevisions | null = null;
95
+ #items: QueueItem[] = [];
96
+ #receiptsByKey = new Map<string, QueueControlReceipt>();
97
+ #receiptsByCommand = new Map<string, QueueControlReceipt>();
98
+ #serial: Promise<unknown> = Promise.resolve();
99
+
100
+ constructor(options: { bridge: SameSessionPiBridge; appendEntry(customType: string, data: unknown): void; now?: () => Date }) {
101
+ this.#bridge = options.bridge; this.#appendEntry = options.appendEntry; this.#now = options.now ?? (() => new Date());
102
+ }
103
+
104
+ reset(): void {
105
+ this.#sessionRef = null; this.#lastIdentity = null; this.#lastRevisions = null; this.#items = [];
106
+ this.#receiptsByKey.clear(); this.#receiptsByCommand.clear();
107
+ }
108
+
109
+ #sync(): ReturnType<SameSessionPiBridge["snapshot"]> {
110
+ const snapshot = this.#bridge.snapshot();
111
+ if (snapshot.identity && snapshot.revisions) {
112
+ if (this.#sessionRef && this.#sessionRef !== snapshot.identity.sessionRef) this.reset();
113
+ this.#sessionRef = snapshot.identity.sessionRef; this.#lastIdentity = copy(snapshot.identity); this.#lastRevisions = copy(snapshot.revisions);
114
+ }
115
+ return snapshot;
116
+ }
117
+
118
+ #projectItem(item: QueueItem, position: number): QueueProjectionItem {
119
+ const preview = safePreview(item.text);
120
+ return { queueItemRef: item.queueItemRef, messageRequestId: item.messageRequestId, position, state: item.state, ...preview,
121
+ attachmentCount: item.attachmentRefs.length, previewDigest: sha(canonical({ ...preview, attachmentCount: item.attachmentRefs.length })),
122
+ createdAt: item.createdAt, updatedAt: item.updatedAt,
123
+ reasonCode: item.reasonCode };
124
+ }
125
+
126
+ #evidence(command: QueueCommand, receipt: QueueControlReceipt, item: QueueItem, position: number, mutationRef: string): Record<string, unknown> {
127
+ return { schemaVersion: 1, mutationRef, commandId: command.commandId, action: command.action, resultCode: receipt.resultCode,
128
+ queueRevision: receipt.observedRevisionsAfter.queueRevision, item: this.#projectItem(item, position) };
129
+ }
130
+
131
+ projection(): QueueProjection {
132
+ const snapshot = this.#sync(), ready = snapshot.state === "ready" && Boolean(snapshot.identity && snapshot.revisions);
133
+ if (!this.#lastIdentity || !this.#lastRevisions) throw new Error("webui-held-queue-identity-unavailable");
134
+ const items = ready ? this.#items.map((item, position) => this.#projectItem(item, position)) : [];
135
+ return { schemaVersion: 1, version: "piagent-webui-queue-v1", generatedAt: this.#now().toISOString(), identity: copy(snapshot.identity ?? this.#lastIdentity),
136
+ revision: copy(snapshot.revisions ?? this.#lastRevisions), state: ready ? "ready" : "unavailable",
137
+ heldCount: items.filter((item) => item.state === "held").length, quarantinedCount: items.filter((item) => item.state === "quarantined").length,
138
+ items, reasonCode: ready ? null : "bridge-binding-not-ready" };
139
+ }
140
+
141
+ snapshot(): { heldCount: number; quarantinedCount: number; queueRevision: string | null } {
142
+ const projection = this.projection();
143
+ return { heldCount: projection.heldCount, quarantinedCount: projection.quarantinedCount, queueRevision: projection.revision.queueRevision };
144
+ }
145
+
146
+ execute(command: unknown): Promise<QueueControlReceipt> {
147
+ const run = this.#serial.then(() => this.#execute(command), () => this.#execute(command));
148
+ this.#serial = run.catch(() => undefined); return run;
149
+ }
150
+
151
+ async #execute(input: unknown): Promise<QueueControlReceipt> {
152
+ const command = input as QueueCommand;
153
+ if (command?.action === "chat.send" && (command.payload as any)?.delivery !== "hold") return this.#bridge.execute(command as unknown as NewOperationChatCommand);
154
+ const snapshot = this.#sync(), structural = this.#validate(command);
155
+ if (snapshot.state !== "ready" || !snapshot.identity || !snapshot.revisions)
156
+ return this.#reject(command, "resync-required", "bridge-binding-not-ready", "The current Pi session binding is unavailable.");
157
+ if (structural) return this.#reject(command, "invalid-command", structural, "The queue command is invalid.");
158
+ const keyDigest = sha(command.idempotencyKey), old = this.#receiptsByKey.get(keyDigest) ?? this.#receiptsByCommand.get(command.commandId);
159
+ if (old) {
160
+ if (old.commandId !== command.commandId || old.actionDigest !== command.actionDigest || old.idempotencyKeyDigest !== keyDigest)
161
+ return this.#reject(command, "idempotency-payload-mismatch", "idempotency-payload-mismatch", "The command key is already bound to another payload.");
162
+ return { ...copy(old), deduplicated: true };
163
+ }
164
+ const now = this.#now().getTime();
165
+ if (Date.parse(command.requestedAt) > now + 30_000) return this.#reject(command, "invalid-command", "requested-at-in-future", "The command timestamp is not yet valid.");
166
+ if (now > Date.parse(command.expiresAt)) return this.#reject(command, "expired", "command-expired", "The command expired before execution.");
167
+ if (!same(command.identity, snapshot.identity)) return this.#reject(command, "identity-mismatch", "identity-mismatch", "The command does not target this Pi session.");
168
+ if (!["runtimeRevision", "taskRevision", "controlRevision", "queueRevision"].every((key) =>
169
+ (command.expectedRevisions as any)[key] === (snapshot.revisions as any)[key]))
170
+ return this.#reject(command, "stale-revision", "stale-revision", "The queue changed before this command.");
171
+ if (command.action === "chat.send") return this.#hold(command);
172
+ if (command.action === "queue.update") return this.#update(command);
173
+ if (command.action === "queue.delete") return this.#delete(command);
174
+ return this.#dispatch(command);
175
+ }
176
+
177
+ #validate(command: QueueCommand): string | null {
178
+ if (!command || typeof command !== "object" || command.schemaVersion !== 1 || command.version !== "piagent-webui-control-v1"
179
+ || command.messageType !== "command" || command.capabilityScope !== "control.chat"
180
+ || !["chat.send", "queue.update", "queue.delete", "queue.dispatch"].includes(command.action)) return "unsupported-command-shape";
181
+ if (!exactKeys(command, ["schemaVersion", "version", "messageType", "commandId", "idempotencyKey", "requestedAt", "expiresAt", "capabilityScope",
182
+ "action", "actionDigest", "identity", "expectedRevisions", "payload"]) || !validIdentity(command.identity) || !validRevisions(command.expectedRevisions)
183
+ || !REF.test(command.commandId) || !IDEMPOTENCY.test(command.idempotencyKey) || !timestamp(command.requestedAt) || !timestamp(command.expiresAt)
184
+ || Date.parse(command.requestedAt) > Date.parse(command.expiresAt) || Date.parse(command.expiresAt) - Date.parse(command.requestedAt) > 300_000)
185
+ return "invalid-command-metadata";
186
+ const payload = command.payload;
187
+ if (command.action === "chat.send") {
188
+ if (!exactKeys(payload, ["messageRequestId", "capabilityAction", "delivery", "text", "attachmentRefs", "contentDigest"])
189
+ || payload.capabilityAction !== "hold" || payload.delivery !== "hold" || typeof payload.messageRequestId !== "string" || !REF.test(payload.messageRequestId)
190
+ || typeof payload.text !== "string" || payload.text.length < 1 || Buffer.byteLength(payload.text) > 65_536 || payload.text.includes("\0")
191
+ || !Array.isArray(payload.attachmentRefs) || payload.attachmentRefs.length > 0 || payload.contentDigest !== chatContentDigest(payload as any)) return "invalid-hold-payload";
192
+ } else if (command.action === "queue.update") {
193
+ if (!exactKeys(payload, ["queueItemRef", "text", "attachmentRefs", "contentDigest"]) || typeof payload.queueItemRef !== "string" || !REF.test(payload.queueItemRef)
194
+ || typeof payload.text !== "string" || payload.text.length < 1 || Buffer.byteLength(payload.text) > 65_536 || payload.text.includes("\0")
195
+ || !Array.isArray(payload.attachmentRefs) || payload.attachmentRefs.length > 0 || payload.contentDigest !== chatContentDigest(payload as any)) return "invalid-update-payload";
196
+ } else if (command.action === "queue.delete") {
197
+ if (!exactKeys(payload, ["queueItemRef"]) || typeof payload.queueItemRef !== "string" || !REF.test(payload.queueItemRef)) return "invalid-delete-payload";
198
+ } else if (!exactKeys(payload, ["queueItemRef", "messageRequestId"]) || typeof payload.queueItemRef !== "string" || !REF.test(payload.queueItemRef)
199
+ || typeof payload.messageRequestId !== "string" || !REF.test(payload.messageRequestId)) return "invalid-dispatch-payload";
200
+ if (!DIGEST.test(command.actionDigest) || command.actionDigest !== controlActionDigest(command)) return "action-digest-mismatch";
201
+ return null;
202
+ }
203
+
204
+ #hold(command: QueueCommand): QueueControlReceipt {
205
+ if (this.#items.length >= MAX_ITEMS) return this.#reject(command, "capability-unavailable", "held-queue-full", "The held message queue is full.");
206
+ if (this.#items.some((item) => item.messageRequestId === command.payload.messageRequestId))
207
+ return this.#reject(command, "replay", "message-request-replay", "This held message request already exists.");
208
+ const at = this.#now().toISOString(), item: QueueItem = { queueItemRef: this.#opaqueRef("queue-item"),
209
+ messageRequestId: command.payload.messageRequestId as string, text: command.payload.text as string, attachmentRefs: [],
210
+ contentDigest: command.payload.contentDigest as string, state: "held", createdAt: at, updatedAt: at, reasonCode: null };
211
+ let receipt: QueueControlReceipt | null = null;
212
+ const mutationRef = this.#opaqueRef("queue-mutation");
213
+ const committed = this.#bridge.commitQueueMutation((before, after) => {
214
+ receipt = this.#receipt(command, before, after, "accepted", "held", null, null);
215
+ this.#appendEntry(WEBUI_QUEUE_ENTRY_TYPE, this.#evidence(command, receipt, item, this.#items.length, mutationRef));
216
+ });
217
+ if (!committed || !receipt) return this.#reject(command, "capability-unavailable", "queue-evidence-unavailable", "The held message could not be recorded.");
218
+ this.#items.push(item); return this.#remember(receipt);
219
+ }
220
+
221
+ #update(command: QueueCommand): QueueControlReceipt {
222
+ const index = this.#items.findIndex((item) => item.queueItemRef === command.payload.queueItemRef);
223
+ if (index < 0 || this.#items[index].state !== "held") return this.#reject(command, "capability-unavailable", "queue-item-unavailable", "The held message is unavailable.");
224
+ const projection = this.#projectItem(this.#items[index], index);
225
+ if (projection.redacted || projection.truncated)
226
+ return this.#reject(command, "capability-unavailable", "queue-item-preview-not-editable", "A redacted or truncated held message cannot be edited from its preview.");
227
+ const next = { ...this.#items[index], text: command.payload.text as string, attachmentRefs: [], contentDigest: command.payload.contentDigest as string,
228
+ updatedAt: this.#now().toISOString() };
229
+ return this.#commitSettled(command, "updated", next, index, false);
230
+ }
231
+
232
+ #delete(command: QueueCommand): QueueControlReceipt {
233
+ const index = this.#items.findIndex((item) => item.queueItemRef === command.payload.queueItemRef);
234
+ if (index < 0) return this.#reject(command, "capability-unavailable", "queue-item-unavailable", "The held message is unavailable.");
235
+ return this.#commitSettled(command, "deleted", this.#items[index], index, true);
236
+ }
237
+
238
+ #commitSettled(command: QueueCommand, resultCode: "updated" | "deleted", item: QueueItem, index: number, remove: boolean): QueueControlReceipt {
239
+ const mutationRef = this.#opaqueRef("queue-mutation"); let receipt: QueueControlReceipt | null = null;
240
+ const committed = this.#bridge.commitQueueMutation((before, after) => {
241
+ receipt = this.#receipt(command, before, after, "settled", resultCode, null, mutationRef);
242
+ this.#appendEntry(WEBUI_QUEUE_ENTRY_TYPE, this.#evidence(command, receipt, item, index, mutationRef));
243
+ });
244
+ if (!committed || !receipt) return this.#reject(command, "capability-unavailable", "queue-evidence-unavailable", "The queue change could not be recorded.");
245
+ if (remove) this.#items.splice(index, 1); else this.#items[index] = item;
246
+ return this.#remember(receipt);
247
+ }
248
+
249
+ async #dispatch(command: QueueCommand): Promise<QueueControlReceipt> {
250
+ const index = this.#items.findIndex((item) => item.queueItemRef === command.payload.queueItemRef), beforeSnapshot = this.#bridge.snapshot();
251
+ if (index < 0 || this.#items[index].state !== "held" || !beforeSnapshot.identity || !beforeSnapshot.revisions)
252
+ return this.#reject(command, "capability-unavailable", "queue-item-unavailable", "The held message is unavailable.");
253
+ if (beforeSnapshot.liveness === "unknown") return this.#reject(command, "capability-unavailable", "agent-liveness-unknown", "Pi operation state is unknown.");
254
+ const item = this.#items[index], payload = { messageRequestId: command.payload.messageRequestId as string, capabilityAction: "send" as const,
255
+ delivery: beforeSnapshot.liveness === "running" ? "follow-up" as const : "new-operation" as const,
256
+ text: item.text, attachmentRefs: item.attachmentRefs, contentDigest: item.contentDigest };
257
+ const inner: NewOperationChatCommand = { schemaVersion: 1, version: "piagent-webui-control-v1", messageType: "command",
258
+ commandId: this.#ref("queue-dispatch", command.commandId, command.actionDigest), idempotencyKey: `queue-dispatch.${sha(command.idempotencyKey).slice(7)}`,
259
+ requestedAt: command.requestedAt, expiresAt: command.expiresAt, capabilityScope: "control.chat", action: "chat.send", actionDigest: "",
260
+ identity: copy(beforeSnapshot.identity), expectedRevisions: { ...copy(beforeSnapshot.revisions), workspacePreimage: null, indexPreimage: null, patchPreimage: null }, payload };
261
+ inner.actionDigest = controlActionDigest(inner);
262
+ const innerReceipt = await this.#bridge.execute(inner);
263
+ if (innerReceipt.phase === "rejected") return this.#reject(command, innerReceipt.resultCode as ChatReceipt["resultCode"], innerReceipt.error?.code ?? "dispatch-rejected",
264
+ innerReceipt.error?.message ?? "The held message was not dispatched.");
265
+ const uncertain = innerReceipt.phase === "uncertain", removed = !uncertain;
266
+ const nextItem = { ...item, state: "quarantined" as const, updatedAt: this.#now().toISOString(), reasonCode: "dispatch-acknowledgement-ambiguous" };
267
+ const mutationRef = this.#opaqueRef("queue-mutation"); let receipt: QueueControlReceipt | null = null;
268
+ const committed = this.#bridge.commitQueueMutation((before, after) => {
269
+ receipt = this.#receipt(command, copy(beforeSnapshot.revisions!), after, innerReceipt.phase, innerReceipt.resultCode as ChatReceipt["resultCode"],
270
+ innerReceipt.error, innerReceipt.settlementEvidenceRef, innerReceipt.identity);
271
+ this.#appendEntry(WEBUI_QUEUE_ENTRY_TYPE, this.#evidence(command, receipt, uncertain ? nextItem : item, index, mutationRef));
272
+ });
273
+ if (!committed || !receipt) {
274
+ this.#items[index] = nextItem;
275
+ return this.#receipt(command, copy(beforeSnapshot.revisions), this.#bridge.snapshot().revisions ?? copy(beforeSnapshot.revisions), "uncertain", "dispatch-unknown",
276
+ { code: "queue-settlement-evidence-unavailable", message: "Dispatch may have occurred; the queue item was quarantined.", retryable: false }, null,
277
+ innerReceipt.identity);
278
+ }
279
+ if (removed) this.#items.splice(index, 1); else this.#items[index] = nextItem;
280
+ return this.#remember(receipt);
281
+ }
282
+
283
+ #receipt(command: QueueCommand, before: BridgeRevisions, after: BridgeRevisions, phase: ChatReceipt["phase"], resultCode: QueueControlReceipt["resultCode"],
284
+ error: ChatReceipt["error"], settlementEvidenceRef: string | null, identity: BridgeIdentity = command.identity): QueueControlReceipt {
285
+ return { schemaVersion: 1, version: "piagent-webui-control-v1", messageType: "receipt", commandId: command.commandId,
286
+ idempotencyKeyDigest: sha(command.idempotencyKey), action: command.action, actionDigest: command.actionDigest, identity: copy(identity), phase, resultCode,
287
+ requestedAt: command.requestedAt, settledAt: ["requested", "accepted"].includes(phase) ? null : this.#now().toISOString(),
288
+ observedRevisionsBefore: copy(before), observedRevisionsAfter: copy(after), deduplicated: false, auditRef: null,
289
+ settlementEvidenceRef: phase === "settled" ? settlementEvidenceRef : null, error };
290
+ }
291
+
292
+ #reject(command: QueueCommand, resultCode: QueueControlReceipt["resultCode"], code: string, message: string): QueueControlReceipt {
293
+ const snapshot = this.#sync(), fallbackIdentity = snapshot.identity ?? this.#lastIdentity ?? { projectRef: "project.unavailable", runtimeInstanceId: "runtime.unavailable",
294
+ sessionRef: "session.unavailable", taskId: null, taskRunId: null, agentOperationId: null, toolCallId: null };
295
+ const fallbackRevisions = snapshot.revisions ?? this.#lastRevisions ?? { runtimeRevision: "runtime-rev.unavailable", taskRevision: null, controlRevision: null,
296
+ workspaceRevision: null, indexRevision: null, approvalRevision: null, sessionOptionRevision: null, queueRevision: null };
297
+ const safeAction = ["chat.send", "queue.update", "queue.delete", "queue.dispatch"].includes(command?.action) ? command.action : "chat.send";
298
+ return { schemaVersion: 1, version: "piagent-webui-control-v1", messageType: "receipt", commandId: REF.test(command?.commandId ?? "") ? command.commandId : this.#ref("command", code),
299
+ idempotencyKeyDigest: IDEMPOTENCY.test(command?.idempotencyKey ?? "") ? sha(command.idempotencyKey) : sha("invalid-idempotency-key"), action: safeAction,
300
+ actionDigest: DIGEST.test(command?.actionDigest ?? "") ? command.actionDigest : sha(code), identity: validIdentity(command?.identity) ? copy(command.identity) : copy(fallbackIdentity),
301
+ phase: "rejected", resultCode, requestedAt: timestamp(command?.requestedAt) ? command.requestedAt : this.#now().toISOString(), settledAt: this.#now().toISOString(),
302
+ observedRevisionsBefore: copy(fallbackRevisions), observedRevisionsAfter: copy(fallbackRevisions), deduplicated: resultCode === "replay", auditRef: null,
303
+ settlementEvidenceRef: null, error: { code, message, retryable: false } };
304
+ }
305
+
306
+ #remember(receipt: QueueControlReceipt): QueueControlReceipt {
307
+ this.#receiptsByKey.set(receipt.idempotencyKeyDigest, copy(receipt)); this.#receiptsByCommand.set(receipt.commandId, copy(receipt)); return receipt;
308
+ }
309
+ #opaqueRef(prefix: string): string { return `${prefix}.${randomBytes(24).toString("hex")}`; }
310
+ #ref(prefix: string, ...parts: string[]): string { return `${prefix}.${createHash("sha256").update(parts.join("\0")).digest("hex").slice(0, 40)}`; }
311
+ }