create-windy 0.5.2 → 0.6.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 (570) hide show
  1. package/dist/cli.js +288 -154
  2. package/package.json +2 -1
  3. package/template/.windy-template.json +2 -2
  4. package/template/apps/agent-server/README.md +25 -6
  5. package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +15 -2
  6. package/template/apps/agent-server/src/southwind_agent_server/app.py +13 -34
  7. package/template/apps/agent-server/src/southwind_agent_server/config.py +49 -0
  8. package/template/apps/agent-server/src/southwind_agent_server/contracts.py +13 -0
  9. package/template/apps/agent-server/src/southwind_agent_server/engine_capacity.py +31 -0
  10. package/template/apps/agent-server/src/southwind_agent_server/fingerprint.py +5 -0
  11. package/template/apps/agent-server/src/southwind_agent_server/health_routes.py +50 -0
  12. package/template/apps/agent-server/src/southwind_agent_server/openai_engine.py +11 -2
  13. package/template/apps/agent-server/src/southwind_agent_server/openai_provider.py +3 -9
  14. package/template/apps/agent-server/src/southwind_agent_server/postgres_store.py +5 -0
  15. package/template/apps/agent-server/src/southwind_agent_server/readiness_pin.py +37 -0
  16. package/template/apps/agent-server/src/southwind_agent_server/run_admission.py +64 -0
  17. package/template/apps/agent-server/src/southwind_agent_server/run_capacity.py +59 -0
  18. package/template/apps/agent-server/src/southwind_agent_server/service.py +42 -43
  19. package/template/apps/agent-server/src/southwind_agent_server/service_errors.py +4 -0
  20. package/template/apps/agent-server/src/southwind_agent_server/service_execution.py +31 -0
  21. package/template/apps/agent-server/src/southwind_agent_server/service_tasks.py +34 -0
  22. package/template/apps/agent-server/src/southwind_agent_server/sqlite_catalog.py +11 -0
  23. package/template/apps/agent-server/src/southwind_agent_server/store.py +6 -0
  24. package/template/apps/agent-server/src/southwind_agent_server/vision_probe.py +87 -0
  25. package/template/apps/agent-server/src/southwind_agent_server/vision_requirement.py +28 -0
  26. package/template/apps/agent-server/src/southwind_agent_server/vlm_readiness.py +247 -0
  27. package/template/apps/agent-server/tests/test_api.py +16 -3
  28. package/template/apps/agent-server/tests/test_openai_provider.py +21 -4
  29. package/template/apps/agent-server/tests/test_postgres_store.py +3 -0
  30. package/template/apps/agent-server/tests/test_run_capacity.py +110 -0
  31. package/template/apps/agent-server/tests/test_runtime_factory_readiness.py +37 -0
  32. package/template/apps/agent-server/tests/test_sqlite_store.py +3 -0
  33. package/template/apps/agent-server/tests/test_vision_readiness_enforcement.py +72 -0
  34. package/template/apps/agent-server/tests/test_vlm_readiness.py +106 -0
  35. package/template/apps/document-server/src/southwind_document_server/adapter.py +117 -205
  36. package/template/apps/document-server/src/southwind_document_server/adapter_errors.py +13 -0
  37. package/template/apps/document-server/src/southwind_document_server/app.py +62 -14
  38. package/template/apps/document-server/src/southwind_document_server/artifacts.py +27 -0
  39. package/template/apps/document-server/src/southwind_document_server/config.py +96 -2
  40. package/template/apps/document-server/src/southwind_document_server/contracts.py +168 -13
  41. package/template/apps/document-server/src/southwind_document_server/fingerprint.py +128 -0
  42. package/template/apps/document-server/src/southwind_document_server/grants.py +81 -0
  43. package/template/apps/document-server/src/southwind_document_server/multipart_output.py +265 -0
  44. package/template/apps/document-server/src/southwind_document_server/output_validation.py +202 -0
  45. package/template/apps/document-server/src/southwind_document_server/progress.py +27 -0
  46. package/template/apps/document-server/src/southwind_document_server/readiness.py +280 -0
  47. package/template/apps/document-server/src/southwind_document_server/readiness_contracts.py +280 -0
  48. package/template/apps/document-server/src/southwind_document_server/readiness_http.py +87 -0
  49. package/template/apps/document-server/src/southwind_document_server/readiness_support.py +120 -0
  50. package/template/apps/document-server/src/southwind_document_server/request_validation.py +48 -0
  51. package/template/apps/document-server/src/southwind_document_server/service.py +297 -96
  52. package/template/apps/document-server/src/southwind_document_server/service_recovery.py +183 -0
  53. package/template/apps/document-server/src/southwind_document_server/sink.py +316 -0
  54. package/template/apps/document-server/src/southwind_document_server/store.py +301 -141
  55. package/template/apps/document-server/src/southwind_document_server/store_events.py +40 -0
  56. package/template/apps/document-server/src/southwind_document_server/store_models.py +62 -0
  57. package/template/apps/document-server/src/southwind_document_server/store_recovery.py +247 -0
  58. package/template/apps/document-server/src/southwind_document_server/store_schema.py +50 -0
  59. package/template/apps/document-server/tests/recovery_support.py +230 -0
  60. package/template/apps/document-server/tests/support.py +210 -0
  61. package/template/apps/document-server/tests/test_adapter.py +251 -0
  62. package/template/apps/document-server/tests/test_config.py +43 -0
  63. package/template/apps/document-server/tests/test_contracts.py +80 -0
  64. package/template/apps/document-server/tests/test_fingerprint.py +125 -0
  65. package/template/apps/document-server/tests/test_grants.py +46 -0
  66. package/template/apps/document-server/tests/test_handshake.py +154 -0
  67. package/template/apps/document-server/tests/test_integration.py +192 -0
  68. package/template/apps/document-server/tests/test_multipart_security.py +259 -0
  69. package/template/apps/document-server/tests/test_readiness.py +311 -0
  70. package/template/apps/document-server/tests/test_readiness_contracts.py +131 -0
  71. package/template/apps/document-server/tests/test_recovery.py +222 -0
  72. package/template/apps/document-server/tests/test_recovery_faults.py +101 -0
  73. package/template/apps/document-server/tests/test_recovery_races.py +283 -0
  74. package/template/apps/document-server/tests/test_service.py +16 -13
  75. package/template/apps/document-server/tests/test_service_artifacts.py +296 -0
  76. package/template/apps/document-server/tests/test_sink.py +253 -0
  77. package/template/apps/document-server/tests/test_store.py +199 -0
  78. package/template/apps/embedding-server/pyproject.toml +5 -0
  79. package/template/apps/embedding-server/src/southwind_embedding_server/adapter.py +38 -2
  80. package/template/apps/embedding-server/src/southwind_embedding_server/app.py +111 -3
  81. package/template/apps/embedding-server/src/southwind_embedding_server/config.py +27 -0
  82. package/template/apps/embedding-server/src/southwind_embedding_server/image_input.py +16 -5
  83. package/template/apps/embedding-server/src/southwind_embedding_server/run_cipher.py +31 -0
  84. package/template/apps/embedding-server/src/southwind_embedding_server/run_service.py +130 -0
  85. package/template/apps/embedding-server/src/southwind_embedding_server/run_store.py +250 -0
  86. package/template/apps/embedding-server/tests/test_adapter.py +25 -0
  87. package/template/apps/embedding-server/tests/test_run_http.py +185 -0
  88. package/template/apps/embedding-server/tests/test_run_service.py +200 -0
  89. package/template/apps/embedding-server/uv.lock +71 -0
  90. package/template/apps/server/Dockerfile +13 -0
  91. package/template/apps/server/package.json +1 -0
  92. package/template/apps/server/src/agent/drizzle-screenshot-intake-repository.integration.test.ts +283 -0
  93. package/template/apps/server/src/agent/drizzle-screenshot-intake-repository.ts +175 -0
  94. package/template/apps/server/src/agent/persistent-screenshot-intake-producer.ts +320 -0
  95. package/template/apps/server/src/agent/remote-runtime.ts +8 -1
  96. package/template/apps/server/src/agent/required-environment.ts +5 -0
  97. package/template/apps/server/src/agent/run-attach.ts +41 -0
  98. package/template/apps/server/src/agent/run-audit.ts +49 -0
  99. package/template/apps/server/src/agent/run-contracts.ts +6 -1
  100. package/template/apps/server/src/agent/run-facade.ts +66 -68
  101. package/template/apps/server/src/agent/run-route-input.ts +26 -1
  102. package/template/apps/server/src/agent/run-routes.ts +1 -0
  103. package/template/apps/server/src/agent/runtime-bootstrap-report.test.ts +1 -1
  104. package/template/apps/server/src/agent/runtime-bootstrap.ts +110 -60
  105. package/template/apps/server/src/agent/runtime-readiness-sources.test.ts +27 -0
  106. package/template/apps/server/src/agent/runtime-readiness-sources.ts +31 -0
  107. package/template/apps/server/src/agent/runtime-reporting-bootstrap.ts +40 -0
  108. package/template/apps/server/src/agent/screenshot-intake-authorization.ts +29 -0
  109. package/template/apps/server/src/agent/screenshot-intake-bootstrap.ts +61 -0
  110. package/template/apps/server/src/agent/screenshot-intake-durable.ts +53 -0
  111. package/template/apps/server/src/agent/screenshot-intake-errors.ts +9 -0
  112. package/template/apps/server/src/agent/screenshot-intake-producer.ts +41 -0
  113. package/template/apps/server/src/agent/screenshot-intake-record.ts +44 -0
  114. package/template/apps/server/src/agent/screenshot-intake-recovery.ts +106 -0
  115. package/template/apps/server/src/agent/screenshot-intake-repository.ts +64 -0
  116. package/template/apps/server/src/agent/screenshot-intake-reservation.ts +24 -0
  117. package/template/apps/server/src/agent/screenshot-intake-routes.ts +171 -0
  118. package/template/apps/server/src/agent/screenshot-intake-runtime-types.ts +6 -0
  119. package/template/apps/server/src/agent/screenshot-intake-sweep.ts +26 -0
  120. package/template/apps/server/src/agent/screenshot-intake-test-support.ts +87 -0
  121. package/template/apps/server/src/agent/screenshot-intake-v2-validation.ts +152 -0
  122. package/template/apps/server/src/agent/screenshot-intake-v2.test.ts +338 -0
  123. package/template/apps/server/src/agent/screenshot-intake-v2.ts +350 -0
  124. package/template/apps/server/src/agent/screenshot-png.ts +15 -0
  125. package/template/apps/server/src/agent/screenshot-run-binding.ts +34 -0
  126. package/template/apps/server/src/agent/unavailable-routes.ts +9 -0
  127. package/template/apps/server/src/agent/vision-facade.test.ts +98 -8
  128. package/template/apps/server/src/agent/vision-input.ts +5 -2
  129. package/template/apps/server/src/agent/vision-readiness-pin.ts +74 -0
  130. package/template/apps/server/src/agent/vision-readiness.ts +58 -0
  131. package/template/apps/server/src/ai-artifact/runtime-reference.test.ts +135 -0
  132. package/template/apps/server/src/ai-artifact/runtime.ts +134 -5
  133. package/template/apps/server/src/ai-output-artifact/cleanup.ts +214 -0
  134. package/template/apps/server/src/ai-output-artifact/contracts.ts +152 -0
  135. package/template/apps/server/src/ai-output-artifact/drizzle-cleanup.ts +37 -0
  136. package/template/apps/server/src/ai-output-artifact/drizzle-complete.ts +71 -0
  137. package/template/apps/server/src/ai-output-artifact/drizzle-grant-cleanup.ts +45 -0
  138. package/template/apps/server/src/ai-output-artifact/drizzle-grants.ts +109 -0
  139. package/template/apps/server/src/ai-output-artifact/drizzle-mapping.ts +77 -0
  140. package/template/apps/server/src/ai-output-artifact/drizzle-readiness.ts +46 -0
  141. package/template/apps/server/src/ai-output-artifact/drizzle-repository.ts +350 -0
  142. package/template/apps/server/src/ai-output-artifact/drizzle-seal.ts +64 -0
  143. package/template/apps/server/src/ai-output-artifact/grant.ts +30 -0
  144. package/template/apps/server/src/ai-output-artifact/host-registry.ts +53 -0
  145. package/template/apps/server/src/ai-output-artifact/memory-cleanup.ts +47 -0
  146. package/template/apps/server/src/ai-output-artifact/memory-grants.ts +98 -0
  147. package/template/apps/server/src/ai-output-artifact/memory-readiness.ts +26 -0
  148. package/template/apps/server/src/ai-output-artifact/memory-repository.ts +348 -0
  149. package/template/apps/server/src/ai-output-artifact/policy.test.ts +67 -0
  150. package/template/apps/server/src/ai-output-artifact/policy.ts +125 -0
  151. package/template/apps/server/src/ai-output-artifact/postgres-test-support.ts +147 -0
  152. package/template/apps/server/src/ai-output-artifact/postgres.integration.test.ts +349 -0
  153. package/template/apps/server/src/ai-output-artifact/reference-codec.ts +128 -0
  154. package/template/apps/server/src/ai-output-artifact/repository-grant-invariants.test-support.ts +165 -0
  155. package/template/apps/server/src/ai-output-artifact/repository-invariants.test-support.ts +239 -0
  156. package/template/apps/server/src/ai-output-artifact/repository-invariants.test.ts +18 -0
  157. package/template/apps/server/src/ai-output-artifact/repository.ts +107 -0
  158. package/template/apps/server/src/ai-output-artifact/route-input.ts +173 -0
  159. package/template/apps/server/src/ai-output-artifact/routes-delete.test.ts +287 -0
  160. package/template/apps/server/src/ai-output-artifact/routes-grant.test.ts +116 -0
  161. package/template/apps/server/src/ai-output-artifact/routes-nonblocking-body.test.ts +136 -0
  162. package/template/apps/server/src/ai-output-artifact/routes-security.test.ts +176 -0
  163. package/template/apps/server/src/ai-output-artifact/routes-storage-error.test.ts +105 -0
  164. package/template/apps/server/src/ai-output-artifact/routes.test-support.ts +84 -0
  165. package/template/apps/server/src/ai-output-artifact/routes.test.ts +198 -0
  166. package/template/apps/server/src/ai-output-artifact/routes.ts +280 -0
  167. package/template/apps/server/src/ai-output-artifact/service-completion.ts +60 -0
  168. package/template/apps/server/src/ai-output-artifact/service-grants.test.ts +169 -0
  169. package/template/apps/server/src/ai-output-artifact/service-grants.ts +217 -0
  170. package/template/apps/server/src/ai-output-artifact/service-input.ts +175 -0
  171. package/template/apps/server/src/ai-output-artifact/service-read.ts +89 -0
  172. package/template/apps/server/src/ai-output-artifact/service-recovery.test.ts +238 -0
  173. package/template/apps/server/src/ai-output-artifact/service-support.ts +240 -0
  174. package/template/apps/server/src/ai-output-artifact/service.test.ts +283 -0
  175. package/template/apps/server/src/ai-output-artifact/service.ts +334 -0
  176. package/template/apps/server/src/ai-output-artifact/storage-recovery.ts +33 -0
  177. package/template/apps/server/src/ai-output-artifact/storage-writer.ts +103 -0
  178. package/template/apps/server/src/ai-output-artifact/test-support.ts +161 -0
  179. package/template/apps/server/src/ai-runtime-readiness/health-response.ts +92 -0
  180. package/template/apps/server/src/ai-runtime-readiness/operation-port-trust.test.ts +66 -0
  181. package/template/apps/server/src/ai-runtime-readiness/operation-port.test-support.ts +143 -0
  182. package/template/apps/server/src/ai-runtime-readiness/operation-port.test.ts +193 -0
  183. package/template/apps/server/src/ai-runtime-readiness/operation-port.ts +172 -0
  184. package/template/apps/server/src/ai-runtime-readiness/trust-store.ts +61 -0
  185. package/template/apps/server/src/app/ai-screenshot-intake-binding.ts +22 -0
  186. package/template/apps/server/src/app/document-output-artifact-bindings.ts +5 -0
  187. package/template/apps/server/src/app/speech-output-artifact-bindings.ts +5 -0
  188. package/template/apps/server/src/application-services.ts +9 -3
  189. package/template/apps/server/src/audit/redaction.ts +1 -0
  190. package/template/apps/server/src/audit/reliable-writer.test.ts +8 -0
  191. package/template/apps/server/src/auth/password-change-license-restriction.test.ts +156 -0
  192. package/template/apps/server/src/document/cancel-response.ts +33 -0
  193. package/template/apps/server/src/document/contracts.ts +6 -0
  194. package/template/apps/server/src/document/errors.ts +11 -0
  195. package/template/apps/server/src/document/facade-cancel.test.ts +226 -0
  196. package/template/apps/server/src/document/facade-cancel.ts +110 -0
  197. package/template/apps/server/src/document/facade-grant-handoff.test.ts +113 -0
  198. package/template/apps/server/src/document/facade-idempotency.test.ts +195 -0
  199. package/template/apps/server/src/document/facade-invocation.ts +46 -0
  200. package/template/apps/server/src/document/facade-output-artifact.test.ts +310 -0
  201. package/template/apps/server/src/document/facade-read-response.ts +116 -0
  202. package/template/apps/server/src/document/facade-readiness.test.ts +95 -0
  203. package/template/apps/server/src/document/facade-support.ts +297 -0
  204. package/template/apps/server/src/document/facade.ts +164 -173
  205. package/template/apps/server/src/document/output-artifact-composition.test.ts +64 -0
  206. package/template/apps/server/src/document/output-artifact-composition.ts +78 -0
  207. package/template/apps/server/src/document/output-artifact-fingerprint.contract.test.ts +110 -0
  208. package/template/apps/server/src/document/output-artifact-fingerprint.ts +150 -0
  209. package/template/apps/server/src/document/output-artifact-host-registry.ts +44 -0
  210. package/template/apps/server/src/document/output-artifact-host.test.ts +332 -0
  211. package/template/apps/server/src/document/output-artifact-host.ts +331 -0
  212. package/template/apps/server/src/document/output-artifact-manifest.test-support.ts +71 -0
  213. package/template/apps/server/src/document/output-artifact-publish.test-support.ts +48 -0
  214. package/template/apps/server/src/document/output-artifact-readiness-probe.ts +347 -0
  215. package/template/apps/server/src/document/output-artifact-readiness-timeout.ts +30 -0
  216. package/template/apps/server/src/document/output-artifact-readiness.test.ts +264 -0
  217. package/template/apps/server/src/document/output-artifact-readiness.ts +87 -0
  218. package/template/apps/server/src/document/output-artifact-route-composition.test.ts +27 -0
  219. package/template/apps/server/src/document/output-artifact-route-composition.ts +14 -0
  220. package/template/apps/server/src/document/output-artifact-runtime.test-support.ts +8 -0
  221. package/template/apps/server/src/document/output-artifact-runtime.test.ts +124 -0
  222. package/template/apps/server/src/document/output-artifact-runtime.ts +275 -0
  223. package/template/apps/server/src/document/output-artifact.test-support.ts +350 -0
  224. package/template/apps/server/src/document/remote-response.ts +162 -0
  225. package/template/apps/server/src/document/remote-runtime.test.ts +100 -4
  226. package/template/apps/server/src/document/remote-runtime.ts +37 -85
  227. package/template/apps/server/src/document/routes-read-response.test.ts +267 -0
  228. package/template/apps/server/src/document/routes.test.ts +109 -0
  229. package/template/apps/server/src/document/routes.ts +23 -14
  230. package/template/apps/server/src/embedding/audit.ts +46 -0
  231. package/template/apps/server/src/embedding/contracts.ts +13 -0
  232. package/template/apps/server/src/embedding/drizzle-run-binding-repository.integration.test.ts +100 -0
  233. package/template/apps/server/src/embedding/drizzle-run-binding-repository.ts +102 -0
  234. package/template/apps/server/src/embedding/facade-types.ts +18 -0
  235. package/template/apps/server/src/embedding/facade.ts +242 -111
  236. package/template/apps/server/src/embedding/operation-authorization.ts +23 -0
  237. package/template/apps/server/src/embedding/remote-runtime.test.ts +18 -0
  238. package/template/apps/server/src/embedding/remote-runtime.ts +77 -0
  239. package/template/apps/server/src/embedding/routes.test.ts +118 -0
  240. package/template/apps/server/src/embedding/routes.ts +98 -0
  241. package/template/apps/server/src/embedding/run-artifact.ts +18 -0
  242. package/template/apps/server/src/embedding/run-authorization.test.ts +133 -0
  243. package/template/apps/server/src/embedding/run-authorization.ts +91 -0
  244. package/template/apps/server/src/embedding/run-binding-repository.ts +75 -0
  245. package/template/apps/server/src/embedding/run-fingerprint.test.ts +52 -0
  246. package/template/apps/server/src/embedding/run-fingerprint.ts +39 -0
  247. package/template/apps/server/src/embedding/terminal-observer.test.ts +26 -0
  248. package/template/apps/server/src/embedding/terminal-observer.ts +44 -0
  249. package/template/apps/server/src/index.ts +38 -31
  250. package/template/apps/server/src/module-host/ai-operation-port.ts +3 -3
  251. package/template/apps/server/src/module-host/durable-context.ts +194 -16
  252. package/template/apps/server/src/module-host/durable-handler-registration.ts +1 -0
  253. package/template/apps/server/src/module-host/durable-storage-filesystem-restart.test.ts +265 -0
  254. package/template/apps/server/src/module-host/durable-storage-write.test.ts +345 -0
  255. package/template/apps/server/src/module-host/http-runtime.ts +25 -0
  256. package/template/apps/server/src/module-host/initialize-contracts.ts +18 -1
  257. package/template/apps/server/src/module-host/initialize.test.ts +18 -0
  258. package/template/apps/server/src/module-host/initialize.ts +42 -2
  259. package/template/apps/server/src/module-host/module-secret-audit-version.test.ts +73 -0
  260. package/template/apps/server/src/module-host/module-secret-binding-resolution.ts +60 -0
  261. package/template/apps/server/src/module-host/module-secret-durable.test.ts +206 -0
  262. package/template/apps/server/src/module-host/module-secret-port.test-support.ts +125 -0
  263. package/template/apps/server/src/module-host/module-secret-port.test.ts +161 -0
  264. package/template/apps/server/src/module-host/module-secret-port.ts +249 -0
  265. package/template/apps/server/src/module-host/module-secret-result.test.ts +324 -0
  266. package/template/apps/server/src/module-host/module-secret-result.ts +274 -0
  267. package/template/apps/server/src/module-host/module-secret-tool-boundary.test.ts +143 -0
  268. package/template/apps/server/src/module-host/project-composition.ts +1 -0
  269. package/template/apps/server/src/module-host/report-staging-port.ts +55 -0
  270. package/template/apps/server/src/module-host/report-staging-registration.test.ts +104 -0
  271. package/template/apps/server/src/module-host/request-context.ts +58 -1
  272. package/template/apps/server/src/module-host/route-declaration.ts +11 -4
  273. package/template/apps/server/src/module-host/route-installer.test-fixture.ts +246 -0
  274. package/template/apps/server/src/module-host/route-installer.test.ts +47 -211
  275. package/template/apps/server/src/module-host/route-installer.ts +192 -21
  276. package/template/apps/server/src/module-host/route-registration.test.ts +44 -4
  277. package/template/apps/server/src/module-host/runtime-composition.ts +2 -0
  278. package/template/apps/server/src/module-host/scoped-host.ts +84 -0
  279. package/template/apps/server/src/module-host/screenshot-artifact-ref.ts +91 -0
  280. package/template/apps/server/src/module-host/screenshot-intake-composition.test.ts +292 -0
  281. package/template/apps/server/src/module-host/screenshot-intake-deadline.ts +38 -0
  282. package/template/apps/server/src/module-host/screenshot-intake-plan.ts +75 -0
  283. package/template/apps/server/src/module-host/screenshot-intake-port.ts +297 -0
  284. package/template/apps/server/src/module-host/screenshot-intake-security.test.ts +311 -0
  285. package/template/apps/server/src/module-host/screenshot-intake-store.ts +76 -0
  286. package/template/apps/server/src/module-host/screenshot-intake-test-support.ts +68 -0
  287. package/template/apps/server/src/module-host/secret-bindings.test.ts +167 -0
  288. package/template/apps/server/src/module-host/secret-bindings.ts +184 -0
  289. package/template/apps/server/src/module-host/storage-authorization-binding.test.ts +242 -0
  290. package/template/apps/server/src/module-host/storage-port.test.ts +8 -1
  291. package/template/apps/server/src/module-host/storage-port.ts +84 -8
  292. package/template/apps/server/src/module-host/storage-restart-fixture.test-support.ts +242 -0
  293. package/template/apps/server/src/module-host/storage-restart-postgres.integration.test.ts +280 -0
  294. package/template/apps/server/src/module-host/storage-scope-drift-http.test.ts +68 -0
  295. package/template/apps/server/src/module-host/storage-scope.ts +52 -1
  296. package/template/apps/server/src/module-host/storage-stream-route.test.ts +282 -0
  297. package/template/apps/server/src/module-host/storage-write-error.test.ts +174 -0
  298. package/template/apps/server/src/module-host/storage-write-error.ts +62 -0
  299. package/template/apps/server/src/module-host/storage-write-port.test.ts +323 -0
  300. package/template/apps/server/src/module-host/storage-write-port.ts +320 -0
  301. package/template/apps/server/src/module-host/storage-write-unavailable.ts +21 -0
  302. package/template/apps/server/src/module-host/storage-writer-retention.test.ts +109 -0
  303. package/template/apps/server/src/module-host/user-directory-composition.test.ts +1 -0
  304. package/template/apps/server/src/module-preflight-secret.test.ts +79 -0
  305. package/template/apps/server/src/module-preflight.ts +5 -0
  306. package/template/apps/server/src/platform-http-runtime.ts +16 -4
  307. package/template/apps/server/src/report-staging/archive-frame-stream.test.ts +209 -0
  308. package/template/apps/server/src/report-staging/archive-frame-stream.ts +341 -0
  309. package/template/apps/server/src/report-staging/archive-process.test.ts +302 -0
  310. package/template/apps/server/src/report-staging/archive-process.ts +257 -0
  311. package/template/apps/server/src/report-staging/archive-volumes.ts +30 -0
  312. package/template/apps/server/src/report-staging/cancellation.ts +13 -0
  313. package/template/apps/server/src/report-staging/contracts.ts +94 -0
  314. package/template/apps/server/src/report-staging/drizzle-lifecycle.ts +278 -0
  315. package/template/apps/server/src/report-staging/drizzle-mapping.ts +140 -0
  316. package/template/apps/server/src/report-staging/drizzle-repository.integration.test.ts +346 -0
  317. package/template/apps/server/src/report-staging/drizzle-repository.ts +255 -0
  318. package/template/apps/server/src/report-staging/drizzle-seal.integration.test.ts +267 -0
  319. package/template/apps/server/src/report-staging/drizzle-seal.ts +177 -0
  320. package/template/apps/server/src/report-staging/filesystem-cleanup.test.ts +68 -0
  321. package/template/apps/server/src/report-staging/filesystem-cleanup.ts +102 -0
  322. package/template/apps/server/src/report-staging/filesystem-errors.ts +15 -0
  323. package/template/apps/server/src/report-staging/filesystem-identifiers.ts +19 -0
  324. package/template/apps/server/src/report-staging/filesystem-io.test.ts +85 -0
  325. package/template/apps/server/src/report-staging/filesystem-io.ts +206 -0
  326. package/template/apps/server/src/report-staging/filesystem-scan.ts +85 -0
  327. package/template/apps/server/src/report-staging/filesystem-store.ts +346 -0
  328. package/template/apps/server/src/report-staging/heartbeat.test.ts +47 -0
  329. package/template/apps/server/src/report-staging/heartbeat.ts +53 -0
  330. package/template/apps/server/src/report-staging/manifest-stream.ts +46 -0
  331. package/template/apps/server/src/report-staging/memory-repository.ts +256 -0
  332. package/template/apps/server/src/report-staging/metadata-catalog.ts +89 -0
  333. package/template/apps/server/src/report-staging/metadata-spool-paths.ts +195 -0
  334. package/template/apps/server/src/report-staging/metadata-spool-schema.ts +54 -0
  335. package/template/apps/server/src/report-staging/metadata-spool.test.ts +214 -0
  336. package/template/apps/server/src/report-staging/metadata-spool.ts +305 -0
  337. package/template/apps/server/src/report-staging/native/archive-frame-helper.c +309 -0
  338. package/template/apps/server/src/report-staging/path-policy.test.ts +127 -0
  339. package/template/apps/server/src/report-staging/path-policy.ts +263 -0
  340. package/template/apps/server/src/report-staging/repository-fence.test.ts +114 -0
  341. package/template/apps/server/src/report-staging/repository.ts +75 -0
  342. package/template/apps/server/src/report-staging/runtime.ts +31 -0
  343. package/template/apps/server/src/report-staging/sealed-file.test.ts +34 -0
  344. package/template/apps/server/src/report-staging/sealed-file.ts +137 -0
  345. package/template/apps/server/src/report-staging/service-input-limits.test.ts +62 -0
  346. package/template/apps/server/src/report-staging/service-input.ts +204 -0
  347. package/template/apps/server/src/report-staging/service-lifecycle.test.ts +132 -0
  348. package/template/apps/server/src/report-staging/service-read-fence.test.ts +37 -0
  349. package/template/apps/server/src/report-staging/service-read.ts +172 -0
  350. package/template/apps/server/src/report-staging/service-support.ts +149 -0
  351. package/template/apps/server/src/report-staging/service.test-fixture.ts +226 -0
  352. package/template/apps/server/src/report-staging/service.test.ts +314 -0
  353. package/template/apps/server/src/report-staging/service.ts +255 -0
  354. package/template/apps/server/src/report-staging/supervised-process.ts +194 -0
  355. package/template/apps/server/src/speech/contracts.ts +8 -0
  356. package/template/apps/server/src/speech/errors.ts +10 -0
  357. package/template/apps/server/src/speech/facade-contract.ts +49 -0
  358. package/template/apps/server/src/speech/facade-output.test.ts +299 -0
  359. package/template/apps/server/src/speech/facade-output.ts +298 -0
  360. package/template/apps/server/src/speech/facade-validation.ts +127 -0
  361. package/template/apps/server/src/speech/facade.ts +185 -151
  362. package/template/apps/server/src/speech/fingerprint.test.ts +42 -0
  363. package/template/apps/server/src/speech/fingerprint.ts +28 -0
  364. package/template/apps/server/src/speech/output-artifact-binding.test.ts +11 -0
  365. package/template/apps/server/src/speech/output-artifact-binding.ts +30 -0
  366. package/template/apps/server/src/speech/output-artifact-error.ts +9 -0
  367. package/template/apps/server/src/speech/output-artifact-host.test-support.ts +73 -0
  368. package/template/apps/server/src/speech/output-artifact-host.test.ts +302 -0
  369. package/template/apps/server/src/speech/output-artifact-host.ts +279 -0
  370. package/template/apps/server/src/speech/output-artifact-policy.ts +114 -0
  371. package/template/apps/server/src/speech/output-artifact-replay.ts +91 -0
  372. package/template/apps/server/src/speech/remote-response.ts +158 -0
  373. package/template/apps/server/src/speech/remote-runtime.test.ts +90 -0
  374. package/template/apps/server/src/speech/remote-runtime.ts +36 -78
  375. package/template/apps/server/src/speech/routes.ts +60 -16
  376. package/template/apps/server/src/startup-finalization.ts +52 -0
  377. package/template/apps/server/src/startup-preflight.ts +2 -0
  378. package/template/apps/server/src/startup-shutdown.test.ts +69 -0
  379. package/template/apps/server/src/startup-shutdown.ts +61 -0
  380. package/template/apps/server/src/storage/drizzle-upload-purge.ts +61 -0
  381. package/template/apps/server/src/storage/drizzle-upload-references.ts +23 -0
  382. package/template/apps/server/src/storage/drizzle-upload-repository.integration.test.ts +103 -0
  383. package/template/apps/server/src/storage/drizzle-upload-repository.ts +109 -14
  384. package/template/apps/server/src/storage/host-runtime.ts +15 -0
  385. package/template/apps/server/src/storage/routes-stream-cancellation.test.ts +154 -0
  386. package/template/apps/server/src/storage/routes.test.ts +13 -2
  387. package/template/apps/server/src/storage/routes.ts +57 -18
  388. package/template/apps/server/src/storage/runtime.ts +17 -4
  389. package/template/apps/server/src/system/route-types.ts +5 -1
  390. package/template/apps/server/src/system/system-resource-monitoring.test.ts +47 -0
  391. package/template/apps/server/src/system/system-resource-monitoring.ts +60 -10
  392. package/template/apps/server/src/work-order/routes.test.ts +1 -0
  393. package/template/apps/speech-server/src/southwind_speech_server/adapter.py +58 -17
  394. package/template/apps/speech-server/src/southwind_speech_server/app.py +24 -17
  395. package/template/apps/speech-server/src/southwind_speech_server/bounded_json.py +60 -0
  396. package/template/apps/speech-server/src/southwind_speech_server/config.py +122 -2
  397. package/template/apps/speech-server/src/southwind_speech_server/contracts.py +58 -0
  398. package/template/apps/speech-server/src/southwind_speech_server/errors.py +8 -0
  399. package/template/apps/speech-server/src/southwind_speech_server/normalizer.py +14 -0
  400. package/template/apps/speech-server/src/southwind_speech_server/output_artifact.py +190 -0
  401. package/template/apps/speech-server/src/southwind_speech_server/output_validation.py +54 -0
  402. package/template/apps/speech-server/src/southwind_speech_server/pin_cli.py +83 -0
  403. package/template/apps/speech-server/src/southwind_speech_server/readiness.py +266 -0
  404. package/template/apps/speech-server/src/southwind_speech_server/readiness_contracts.py +268 -0
  405. package/template/apps/speech-server/src/southwind_speech_server/readiness_http.py +91 -0
  406. package/template/apps/speech-server/src/southwind_speech_server/readiness_local.py +161 -0
  407. package/template/apps/speech-server/src/southwind_speech_server/readiness_support.py +106 -0
  408. package/template/apps/speech-server/src/southwind_speech_server/service.py +118 -13
  409. package/template/apps/speech-server/src/southwind_speech_server/store.py +7 -0
  410. package/template/apps/speech-server/tests/test_bounded_json.py +55 -0
  411. package/template/apps/speech-server/tests/test_config.py +22 -0
  412. package/template/apps/speech-server/tests/test_output_artifact.py +60 -0
  413. package/template/apps/speech-server/tests/test_output_validation.py +44 -0
  414. package/template/apps/speech-server/tests/test_readiness.py +315 -0
  415. package/template/apps/speech-server/tests/test_readiness_http.py +48 -0
  416. package/template/apps/speech-server/tests/test_readiness_local.py +43 -0
  417. package/template/apps/speech-server/tests/test_service.py +211 -36
  418. package/template/apps/web/src/pages/system/components/OperationGpuMonitor.vue +2 -1
  419. package/template/apps/web/src/pages/system/components/OperationGpuMonitor.webtest.ts +29 -0
  420. package/template/apps/web/src/pages/system/components/OperationServiceMonitor.vue +2 -1
  421. package/template/apps/web/src/pages/system/components/OperationServiceMonitor.webtest.ts +4 -1
  422. package/template/docker-compose.yml +38 -1
  423. package/template/docs/architecture/ai-runtime-readiness.md +498 -0
  424. package/template/docs/architecture/ai-runtime.md +20 -4
  425. package/template/docs/architecture/durable-jobs.md +54 -5
  426. package/template/docs/architecture/module-host.md +72 -9
  427. package/template/docs/architecture/module-manifest-composition.md +5 -2
  428. package/template/docs/architecture/module-secret-resolution.md +250 -0
  429. package/template/docs/architecture/object-storage.md +144 -3
  430. package/template/docs/architecture/report-staging.md +158 -0
  431. package/template/docs/architecture/server-module-interface.md +29 -0
  432. package/template/docs/architecture/speech-variants.md +45 -0
  433. package/template/docs/platform/agent-tools.md +16 -0
  434. package/template/docs/platform/document-ai-runtime.md +199 -17
  435. package/template/docs/platform/module-configuration.md +5 -0
  436. package/template/docs/platform/speech-ai-runtime.md +34 -5
  437. package/template/docs/platform/vision-ai-runtime.md +24 -0
  438. package/template/package.json +4 -1
  439. package/template/packages/ai-client/package.json +1 -1
  440. package/template/packages/ai-client/src/agent/client/client.ts +3 -0
  441. package/template/packages/ai-client/src/agent/client/index.ts +1 -0
  442. package/template/packages/ai-client/src/agent/client/screenshot.test.ts +40 -0
  443. package/template/packages/ai-client/src/agent/client/screenshot.ts +146 -0
  444. package/template/packages/ai-client/src/agent/client/types.ts +2 -0
  445. package/template/packages/ai-client/src/agent/contracts/runs.ts +1 -0
  446. package/template/packages/ai-client/src/embedding/client.test.ts +52 -0
  447. package/template/packages/ai-client/src/embedding/client.ts +69 -0
  448. package/template/packages/ai-client/src/embedding/contracts.ts +47 -0
  449. package/template/packages/ai-client/src/embedding/index.ts +1 -0
  450. package/template/packages/ai-client/src/embedding/run.ts +130 -0
  451. package/template/packages/ai-client/src/speech/client.test.ts +26 -0
  452. package/template/packages/ai-client/src/speech/client.ts +25 -1
  453. package/template/packages/ai-client/src/speech/contracts.ts +19 -0
  454. package/template/packages/config/package.json +1 -1
  455. package/template/packages/database/drizzle/0048_illegal_magneto.sql +11 -0
  456. package/template/packages/database/drizzle/0049_right_lenny_balinger.sql +79 -0
  457. package/template/packages/database/drizzle/0050_greedy_sister_grimm.sql +92 -0
  458. package/template/packages/database/drizzle/0051_lame_shockwave.sql +19 -0
  459. package/template/packages/database/drizzle/0052_tiresome_komodo.sql +1 -0
  460. package/template/packages/database/drizzle/0053_calm_victor_mancha.sql +1 -0
  461. package/template/packages/database/drizzle/0054_tough_thanos.sql +29 -0
  462. package/template/packages/database/drizzle/meta/0048_snapshot.json +8852 -0
  463. package/template/packages/database/drizzle/meta/0049_snapshot.json +9410 -0
  464. package/template/packages/database/drizzle/meta/0050_snapshot.json +10115 -0
  465. package/template/packages/database/drizzle/meta/0051_snapshot.json +10257 -0
  466. package/template/packages/database/drizzle/meta/0052_snapshot.json +10263 -0
  467. package/template/packages/database/drizzle/meta/0053_snapshot.json +10270 -0
  468. package/template/packages/database/drizzle/meta/0054_snapshot.json +10468 -0
  469. package/template/packages/database/drizzle/meta/_journal.json +49 -0
  470. package/template/packages/database/package.json +1 -1
  471. package/template/packages/database/src/agent-runtime-schema.test.ts +15 -0
  472. package/template/packages/database/src/ai-output-artifacts.test.ts +57 -0
  473. package/template/packages/database/src/schema/agent-runtime.ts +43 -0
  474. package/template/packages/database/src/schema/ai-output-artifacts.ts +182 -0
  475. package/template/packages/database/src/schema/embedding-runtime.ts +62 -0
  476. package/template/packages/database/src/schema/file-storage.ts +18 -0
  477. package/template/packages/database/src/schema/index.ts +3 -0
  478. package/template/packages/database/src/schema/report-staging.ts +212 -0
  479. package/template/packages/database/src/schema.test.ts +19 -0
  480. package/template/packages/jobs/package.json +1 -1
  481. package/template/packages/modules/index.ts +1 -0
  482. package/template/packages/modules/package.json +1 -1
  483. package/template/packages/modules/src/ai-operation-composition.test.ts +37 -0
  484. package/template/packages/modules/src/ai-operation-composition.ts +2 -0
  485. package/template/packages/modules/src/ai-operation-readiness-contract.test.ts +38 -0
  486. package/template/packages/modules/src/ai-operation-readiness-contract.ts +34 -0
  487. package/template/packages/modules/src/capability-audit-validation.ts +20 -0
  488. package/template/packages/modules/src/capability-guard-validation.ts +41 -0
  489. package/template/packages/modules/src/composition.ts +11 -0
  490. package/template/packages/modules/src/manifest.ts +40 -0
  491. package/template/packages/modules/src/secret-composition.test.ts +308 -0
  492. package/template/packages/modules/src/secret-composition.ts +176 -0
  493. package/template/packages/modules/src/system-api-control-plane.ts +17 -1
  494. package/template/packages/modules/src/system-module-catalog.test.ts +52 -0
  495. package/template/packages/modules/src/system-module-catalog.ts +1 -1
  496. package/template/packages/server-sdk/contracts/ai-runtime-readiness/v1/document-python-parity.json +12 -0
  497. package/template/packages/server-sdk/contracts/ai-runtime-readiness/v1/document.json +70 -0
  498. package/template/packages/server-sdk/contracts/ai-runtime-readiness/v1/embedding.json +68 -0
  499. package/template/packages/server-sdk/contracts/ai-runtime-readiness/v1/speech.json +80 -0
  500. package/template/packages/server-sdk/contracts/ai-runtime-readiness/v1/vlm.json +70 -0
  501. package/template/packages/server-sdk/contracts/document-runtime/v1/fingerprint-fixture.json +101 -0
  502. package/template/packages/server-sdk/index.ts +109 -0
  503. package/template/packages/server-sdk/package.json +22 -3
  504. package/template/packages/server-sdk/src/ai-runtime-readiness-capture.ts +168 -0
  505. package/template/packages/server-sdk/src/ai-runtime-readiness-digest.ts +49 -0
  506. package/template/packages/server-sdk/src/ai-runtime-readiness-identity.ts +16 -0
  507. package/template/packages/server-sdk/src/ai-runtime-readiness-keys.ts +23 -0
  508. package/template/packages/server-sdk/src/ai-runtime-readiness-limits.ts +141 -0
  509. package/template/packages/server-sdk/src/ai-runtime-readiness-projection.ts +326 -0
  510. package/template/packages/server-sdk/src/ai-runtime-readiness-requirement.ts +31 -0
  511. package/template/packages/server-sdk/src/ai-runtime-readiness-time.ts +16 -0
  512. package/template/packages/server-sdk/src/ai-runtime-readiness-trust.ts +84 -0
  513. package/template/packages/server-sdk/src/ai-runtime-readiness-validation-values.ts +147 -0
  514. package/template/packages/server-sdk/src/ai-runtime-readiness-validation.ts +299 -0
  515. package/template/packages/server-sdk/src/ai-runtime-readiness-values.ts +42 -0
  516. package/template/packages/server-sdk/src/ai-runtime-readiness.adversarial-input.contract.test.ts +146 -0
  517. package/template/packages/server-sdk/src/ai-runtime-readiness.contract-support.ts +139 -0
  518. package/template/packages/server-sdk/src/ai-runtime-readiness.contract.test.ts +314 -0
  519. package/template/packages/server-sdk/src/ai-runtime-readiness.digest-safety.contract.test.ts +131 -0
  520. package/template/packages/server-sdk/src/ai-runtime-readiness.fail-closed.contract.test.ts +215 -0
  521. package/template/packages/server-sdk/src/ai-runtime-readiness.identity.contract.test.ts +138 -0
  522. package/template/packages/server-sdk/src/ai-runtime-readiness.input-safety.contract.test.ts +314 -0
  523. package/template/packages/server-sdk/src/ai-runtime-readiness.public-api.test.ts +18 -0
  524. package/template/packages/server-sdk/src/ai-runtime-readiness.python-parity.contract.test.ts +160 -0
  525. package/template/packages/server-sdk/src/ai-runtime-readiness.trust.contract.test.ts +135 -0
  526. package/template/packages/server-sdk/src/ai-runtime-readiness.ts +198 -0
  527. package/template/packages/server-sdk/src/ai-screenshot-intake-port.ts +58 -0
  528. package/template/packages/server-sdk/src/data-governance-registration.ts +1 -3
  529. package/template/packages/server-sdk/src/durable-handler.ts +12 -0
  530. package/template/packages/server-sdk/src/module-adapter.test.ts +10 -0
  531. package/template/packages/server-sdk/src/module-adapter.ts +3 -0
  532. package/template/packages/server-sdk/src/module-host.ts +8 -0
  533. package/template/packages/server-sdk/src/report-staging-context-compatibility.test.ts +25 -0
  534. package/template/packages/server-sdk/src/report-staging-port.ts +190 -0
  535. package/template/packages/server-sdk/src/request-context.ts +27 -1
  536. package/template/packages/server-sdk/src/route-definition.ts +6 -0
  537. package/template/packages/server-sdk/src/secret-binding.test.ts +66 -0
  538. package/template/packages/server-sdk/src/secret-binding.ts +54 -0
  539. package/template/packages/server-sdk/src/secret-context-compatibility.test.ts +77 -0
  540. package/template/packages/server-sdk/src/secret-port.ts +80 -0
  541. package/template/packages/server-sdk/src/storage-port.ts +129 -9
  542. package/template/packages/server-sdk/src/storage-v052-source-compatibility.test.ts +61 -0
  543. package/template/packages/shared/index.ts +2 -0
  544. package/template/packages/shared/package.json +4 -1
  545. package/template/packages/shared/src/ai-operation-readiness.test.ts +75 -0
  546. package/template/packages/shared/src/ai-operation-readiness.ts +170 -0
  547. package/template/packages/shared/src/license-catalog.ts +14 -9
  548. package/template/packages/shared/src/stable-key.ts +26 -0
  549. package/template/packages/shared/tsconfig.json +16 -0
  550. package/template/packages/storage/index.ts +2 -0
  551. package/template/packages/storage/package.json +1 -1
  552. package/template/packages/storage/src/in-memory-upload-repository.ts +105 -2
  553. package/template/packages/storage/src/local-file-blob-store.test.ts +58 -0
  554. package/template/packages/storage/src/local-file-blob-store.ts +5 -71
  555. package/template/packages/storage/src/local-file-blob-writer.ts +96 -0
  556. package/template/packages/storage/src/upload-abort-recovery.test.ts +234 -0
  557. package/template/packages/storage/src/upload-abort.ts +130 -0
  558. package/template/packages/storage/src/upload-cleanup-race.test.ts +207 -0
  559. package/template/packages/storage/src/upload-cleanup.ts +85 -0
  560. package/template/packages/storage/src/upload-completion.ts +52 -0
  561. package/template/packages/storage/src/upload-purge.test.ts +163 -0
  562. package/template/packages/storage/src/upload-purge.ts +261 -0
  563. package/template/packages/storage/src/upload-repository-v052-compatibility.test.ts +31 -0
  564. package/template/packages/storage/src/upload-repository.ts +61 -0
  565. package/template/packages/storage/src/upload-service-recovery.test.ts +296 -0
  566. package/template/packages/storage/src/upload-service.ts +138 -150
  567. package/template/packages/storage/src/upload-streams.ts +126 -30
  568. package/template/packages/storage/src/upload-types.ts +13 -0
  569. package/template/packages/storage/src/upload-validation.ts +127 -0
  570. package/template/scripts/provision-speech-readiness.ts +147 -0
@@ -0,0 +1,60 @@
1
+ import type { StorageRuntime } from "../storage/runtime.js";
2
+ import type {
3
+ AiOutputArtifactRecord,
4
+ AiOutputArtifactScope,
5
+ } from "./contracts.js";
6
+ import type { AiOutputArtifactRepository } from "./repository.js";
7
+ import {
8
+ outputConflict,
9
+ outputNotReady,
10
+ outputStorageError,
11
+ } from "./service-support.js";
12
+ import type { AiOutputArtifactStorageWriterFactory } from "./storage-writer.js";
13
+ import { purgeCompletedOutputUpload } from "./storage-recovery.js";
14
+
15
+ export async function completeOutputArtifact(
16
+ options: {
17
+ readonly repository: AiOutputArtifactRepository;
18
+ readonly storage: StorageRuntime;
19
+ readonly writers: AiOutputArtifactStorageWriterFactory;
20
+ readonly now: () => Date;
21
+ },
22
+ scope: AiOutputArtifactScope,
23
+ artifact: AiOutputArtifactRecord,
24
+ signal: AbortSignal,
25
+ ) {
26
+ if (artifact.status === "completed") return artifact;
27
+ if (artifact.status !== "uploading" || !artifact.uploadSessionId) {
28
+ throw outputNotReady();
29
+ }
30
+ let file;
31
+ try {
32
+ file = await options.writers
33
+ .create(scope, signal)
34
+ .completeUpload(artifact.uploadSessionId);
35
+ } catch (error) {
36
+ throw outputStorageError(error);
37
+ }
38
+ const completed = await options.repository.completeArtifact(
39
+ artifact.artifactId,
40
+ artifact.generation,
41
+ file,
42
+ options.now().toISOString(),
43
+ );
44
+ if (completed) return completed;
45
+
46
+ const purged = await purgeCompletedOutputUpload(
47
+ options.storage,
48
+ scope,
49
+ artifact,
50
+ signal,
51
+ ).catch(() => false);
52
+ await options.repository.transitionArtifact(
53
+ artifact.artifactId,
54
+ artifact.generation,
55
+ ["uploading"],
56
+ purged ? "aborted" : "cleanup-pending",
57
+ options.now().toISOString(),
58
+ );
59
+ throw outputConflict();
60
+ }
@@ -0,0 +1,169 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { createOutputArtifactFixture } from "./test-support.js";
3
+
4
+ describe("AI Output Artifact grant handoff", () => {
5
+ test("pre-ACK rollback 保留空 open scope,不可写且可用下一 credential 重试", async () => {
6
+ const fixture = createOutputArtifactFixture();
7
+ const first = await fixture.prepare("grant-pre-ack");
8
+ const scopeId = first.scope.scopeId;
9
+
10
+ expect(first).toMatchObject({
11
+ descriptor: { grantGeneration: 1 },
12
+ scope: {
13
+ revision: 1,
14
+ grantSequence: 1,
15
+ pendingGrantGeneration: 1,
16
+ },
17
+ });
18
+ expect(first.scope).not.toHaveProperty("grantHash");
19
+ await expect(
20
+ fixture.service.requireWritable(scopeId, first.grant!),
21
+ ).rejects.toMatchObject({ code: "OUTPUT_ARTIFACT_FORBIDDEN" });
22
+
23
+ expect(
24
+ await fixture.service.rollbackPendingGrant(
25
+ scopeId,
26
+ first.descriptor!.grantGeneration,
27
+ first.grant!,
28
+ ),
29
+ ).toBe("rolled-back");
30
+ const empty = await fixture.repository.getScope(scopeId);
31
+ expect(empty).toMatchObject({
32
+ status: "open",
33
+ revision: 2,
34
+ grantSequence: 1,
35
+ });
36
+ expect(empty).not.toHaveProperty("grantHash");
37
+ expect(empty?.pendingGrantHash).toBeUndefined();
38
+ await expect(
39
+ fixture.service.requireWritable(scopeId, first.grant!),
40
+ ).rejects.toMatchObject({ code: "OUTPUT_ARTIFACT_FORBIDDEN" });
41
+
42
+ const retry = await fixture.prepare("grant-pre-ack");
43
+ expect(retry.scope.revision).toBe(3);
44
+ expect(retry.descriptor?.grantGeneration).toBe(2);
45
+ await expect(
46
+ fixture.service.acceptGrant(scopeId, 1, first.grant!),
47
+ ).rejects.toMatchObject({ code: "OUTPUT_ARTIFACT_FORBIDDEN" });
48
+ const accepted = await fixture.service.acceptGrant(
49
+ scopeId,
50
+ retry.descriptor!.grantGeneration,
51
+ retry.grant!,
52
+ );
53
+ const duplicate = await fixture.service.acceptGrant(
54
+ scopeId,
55
+ retry.descriptor!.grantGeneration,
56
+ retry.grant!,
57
+ );
58
+ expect(accepted).toMatchObject({
59
+ activeGrantGeneration: 2,
60
+ grantSequence: 2,
61
+ });
62
+ expect(duplicate.revision).toBe(accepted.revision);
63
+ await expect(
64
+ fixture.service.requireWritable(scopeId, retry.grant!),
65
+ ).resolves.toMatchObject({ activeGrantGeneration: 2 });
66
+ });
67
+
68
+ test("空 open scope 无重试时按 scope TTL 清理", async () => {
69
+ const fixture = createOutputArtifactFixture();
70
+ const prepared = await fixture.prepare("grant-empty-cleanup");
71
+ await fixture.service.rollbackPendingGrant(
72
+ prepared.scope.scopeId,
73
+ prepared.descriptor!.grantGeneration,
74
+ prepared.grant!,
75
+ );
76
+ fixture.setNow(
77
+ new Date(Date.parse(prepared.scope.scopeExpiresAt) + 1).toISOString(),
78
+ );
79
+
80
+ expect(await fixture.cleanup.cleanupExpired()).toMatchObject({
81
+ inspected: 1,
82
+ cleaned: 1,
83
+ pending: 0,
84
+ });
85
+ expect(
86
+ await fixture.repository.getScope(prepared.scope.scopeId),
87
+ ).toMatchObject({ status: "cleaned" });
88
+ await expect(
89
+ fixture.service.requireWritable(prepared.scope.scopeId, prepared.grant!),
90
+ ).rejects.toMatchObject({ code: "OUTPUT_ARTIFACT_FORBIDDEN" });
91
+ });
92
+
93
+ test("并发 replay、pending lease、迟到与重复 ACK 均按 credential generation 隔离", async () => {
94
+ const fixture = createOutputArtifactFixture();
95
+ const attempts = await Promise.allSettled([
96
+ fixture.prepare("grant-concurrent"),
97
+ fixture.prepare("grant-concurrent"),
98
+ ]);
99
+ expect(
100
+ attempts.filter(({ status }) => status === "fulfilled"),
101
+ ).toHaveLength(1);
102
+ const first = attempts.find(
103
+ (
104
+ result,
105
+ ): result is PromiseFulfilledResult<
106
+ Awaited<ReturnType<typeof fixture.prepare>>
107
+ > => result.status === "fulfilled",
108
+ )!.value;
109
+ expect(first.descriptor?.grantGeneration).toBe(1);
110
+
111
+ fixture.setNow(
112
+ new Date(
113
+ Date.parse(first.scope.pendingGrantExpiresAt!) + 1,
114
+ ).toISOString(),
115
+ );
116
+ const replacement = await fixture.prepare("grant-concurrent");
117
+ expect(replacement.descriptor?.grantGeneration).toBe(2);
118
+ await expect(
119
+ fixture.service.acceptGrant(
120
+ first.scope.scopeId,
121
+ first.descriptor!.grantGeneration,
122
+ first.grant!,
123
+ ),
124
+ ).rejects.toMatchObject({ code: "OUTPUT_ARTIFACT_FORBIDDEN" });
125
+ const accepted = await fixture.service.acceptGrant(
126
+ first.scope.scopeId,
127
+ replacement.descriptor!.grantGeneration,
128
+ replacement.grant!,
129
+ );
130
+ const duplicate = await fixture.service.acceptGrant(
131
+ first.scope.scopeId,
132
+ replacement.descriptor!.grantGeneration,
133
+ replacement.grant!,
134
+ );
135
+ expect(accepted.activeGrantGeneration).toBe(2);
136
+ expect(duplicate.revision).toBe(accepted.revision);
137
+ });
138
+
139
+ test("seal 与 cancel 都清除 pending,迟到 ACK 不能复活 credential", async () => {
140
+ for (const terminal of ["seal", "cancel"] as const) {
141
+ const fixture = createOutputArtifactFixture();
142
+ const current = await fixture.begin(`grant-terminal-${terminal}`);
143
+ const pending = await fixture.prepare(`grant-terminal-${terminal}`);
144
+ expect(pending.descriptor?.grantGeneration).toBe(2);
145
+
146
+ if (terminal === "seal") {
147
+ await fixture.service.sealScope(current.scope.scopeId, current.grant!);
148
+ } else {
149
+ expect(
150
+ await fixture.cleanup.abortAuthorized(
151
+ fixture.service,
152
+ current.scope.scopeId,
153
+ current.grant!,
154
+ ),
155
+ ).toBe(true);
156
+ }
157
+ const closed = await fixture.repository.getScope(current.scope.scopeId);
158
+ expect(closed?.status).toBe(terminal === "seal" ? "sealed" : "aborted");
159
+ expect(closed?.pendingGrantHash).toBeUndefined();
160
+ await expect(
161
+ fixture.service.acceptGrant(
162
+ current.scope.scopeId,
163
+ pending.descriptor!.grantGeneration,
164
+ pending.grant!,
165
+ ),
166
+ ).rejects.toMatchObject({ code: "OUTPUT_ARTIFACT_FORBIDDEN" });
167
+ }
168
+ });
169
+ });
@@ -0,0 +1,217 @@
1
+ import type {
2
+ AiOutputArtifactReferenceV1,
3
+ AiOutputArtifactScope,
4
+ AiOutputArtifactSinkDescriptorV1,
5
+ } from "./contracts.js";
6
+ import {
7
+ createAiOutputArtifactGrant,
8
+ hashAiOutputArtifactGrant,
9
+ matchesAiOutputArtifactGrant,
10
+ } from "./grant.js";
11
+ import type { AiOutputArtifactRepository } from "./repository.js";
12
+ import {
13
+ newAiOutputScope,
14
+ type BeginAiOutputArtifactScopeInput,
15
+ } from "./service-input.js";
16
+ import {
17
+ outputArtifactReference,
18
+ outputConflict,
19
+ outputForbidden,
20
+ outputSinkDescriptor,
21
+ } from "./service-support.js";
22
+ import type { AiOutputArtifactReferenceCodec } from "./reference-codec.js";
23
+
24
+ const pendingGrantLeaseMs = 15_000;
25
+
26
+ interface GrantDependencies {
27
+ readonly repository: AiOutputArtifactRepository;
28
+ readonly references: AiOutputArtifactReferenceCodec;
29
+ readonly now: () => Date;
30
+ }
31
+
32
+ export interface PreparedOutputScope {
33
+ readonly scope: AiOutputArtifactScope;
34
+ readonly descriptor?: AiOutputArtifactSinkDescriptorV1;
35
+ readonly grant?: string;
36
+ readonly references?: readonly AiOutputArtifactReferenceV1[];
37
+ readonly created: boolean;
38
+ readonly replayed: boolean;
39
+ }
40
+
41
+ export async function prepareOutputScope(
42
+ dependencies: GrantDependencies,
43
+ input: BeginAiOutputArtifactScopeInput,
44
+ ): Promise<PreparedOutputScope> {
45
+ const now = dependencies.now();
46
+ const grant = createAiOutputArtifactGrant();
47
+ const pendingExpiresAt = pendingLeaseExpiry(now, input.policy.scopeTtlMs);
48
+ const candidate = newAiOutputScope(input, grant.hash, pendingExpiresAt, now);
49
+ const result = await dependencies.repository.createOrGetScope(candidate);
50
+ if (result.scope.fingerprint !== candidate.fingerprint) {
51
+ throw outputConflict();
52
+ }
53
+ if (result.created) {
54
+ return prepared(result.scope, grant.token, true, false);
55
+ }
56
+ if (result.scope.status === "sealed") {
57
+ return sealedReplay(dependencies, result.scope);
58
+ }
59
+ const nowIso = now.toISOString();
60
+ if (
61
+ result.scope.status !== "open" ||
62
+ result.scope.scopeExpiresAt <= nowIso ||
63
+ (result.scope.pendingGrantExpiresAt &&
64
+ result.scope.pendingGrantExpiresAt > nowIso)
65
+ ) {
66
+ throw outputConflict();
67
+ }
68
+ const nextPendingExpiry = new Date(
69
+ Math.min(
70
+ now.getTime() + pendingGrantLeaseMs,
71
+ Date.parse(result.scope.scopeExpiresAt),
72
+ ),
73
+ ).toISOString();
74
+ const preparedScope = await dependencies.repository.prepareGrant(
75
+ result.scope.scopeId,
76
+ result.scope.revision,
77
+ grant.hash,
78
+ nextPendingExpiry,
79
+ nowIso,
80
+ );
81
+ if (!preparedScope) throw outputConflict();
82
+ return prepared(preparedScope, grant.token, false, true);
83
+ }
84
+
85
+ export async function acceptOutputGrant(
86
+ dependencies: GrantDependencies,
87
+ scopeId: string,
88
+ generation: number,
89
+ token: string,
90
+ ): Promise<AiOutputArtifactScope> {
91
+ const current = await dependencies.repository.getScope(scopeId);
92
+ if (isActiveGrant(current, generation, token)) return current!;
93
+ const now = dependencies.now().toISOString();
94
+ if (
95
+ !current ||
96
+ current.status !== "open" ||
97
+ current.pendingGrantGeneration !== generation ||
98
+ !current.pendingGrantHash ||
99
+ !matchesAiOutputArtifactGrant(token, current.pendingGrantHash) ||
100
+ !current.pendingGrantExpiresAt ||
101
+ current.pendingGrantExpiresAt <= now
102
+ ) {
103
+ throw outputForbidden();
104
+ }
105
+ const promoted = await dependencies.repository.promoteGrant(
106
+ scopeId,
107
+ current.revision,
108
+ generation,
109
+ hashAiOutputArtifactGrant(token),
110
+ now,
111
+ );
112
+ if (promoted) return promoted;
113
+ const raced = await dependencies.repository.getScope(scopeId);
114
+ if (isActiveGrant(raced, generation, token)) return raced!;
115
+ throw outputConflict();
116
+ }
117
+
118
+ export async function confirmOutputGrant(
119
+ dependencies: GrantDependencies,
120
+ scopeId: string,
121
+ generation: number,
122
+ token: string,
123
+ ): Promise<boolean> {
124
+ return isActiveGrant(
125
+ await dependencies.repository.getScope(scopeId),
126
+ generation,
127
+ token,
128
+ );
129
+ }
130
+
131
+ export async function rollbackPendingOutputGrant(
132
+ dependencies: GrantDependencies,
133
+ scopeId: string,
134
+ generation: number,
135
+ token: string,
136
+ ): Promise<"rolled-back" | "promoted" | "superseded"> {
137
+ const current = await dependencies.repository.getScope(scopeId);
138
+ if (isActiveGrant(current, generation, token)) return "promoted";
139
+ if (
140
+ current?.status === "open" &&
141
+ current.pendingGrantGeneration === generation &&
142
+ current.pendingGrantHash &&
143
+ matchesAiOutputArtifactGrant(token, current.pendingGrantHash)
144
+ ) {
145
+ const rolledBack = await dependencies.repository.rollbackGrant(
146
+ scopeId,
147
+ current.revision,
148
+ generation,
149
+ hashAiOutputArtifactGrant(token),
150
+ dependencies.now().toISOString(),
151
+ );
152
+ if (rolledBack) return "rolled-back";
153
+ }
154
+ const raced = await dependencies.repository.getScope(scopeId);
155
+ return isActiveGrant(raced, generation, token) ? "promoted" : "superseded";
156
+ }
157
+
158
+ function prepared(
159
+ scope: AiOutputArtifactScope,
160
+ grant: string,
161
+ created: boolean,
162
+ replayed: boolean,
163
+ ): PreparedOutputScope {
164
+ return {
165
+ scope,
166
+ descriptor: outputSinkDescriptor(scope),
167
+ grant,
168
+ created,
169
+ replayed,
170
+ };
171
+ }
172
+
173
+ async function sealedReplay(
174
+ dependencies: GrantDependencies,
175
+ scope: AiOutputArtifactScope,
176
+ ): Promise<PreparedOutputScope> {
177
+ if (
178
+ !scope.grantHash ||
179
+ !scope.activeGrantGeneration ||
180
+ scope.refExpiresAt <= dependencies.now().toISOString()
181
+ ) {
182
+ throw outputConflict();
183
+ }
184
+ const artifacts = await dependencies.repository.listArtifacts(scope.scopeId);
185
+ if (artifacts.some(({ status }) => status !== "completed")) {
186
+ throw outputConflict();
187
+ }
188
+ return {
189
+ scope,
190
+ descriptor: outputSinkDescriptor(scope),
191
+ references: artifacts.map((artifact) =>
192
+ outputArtifactReference(scope, artifact, dependencies.references),
193
+ ),
194
+ created: false,
195
+ replayed: true,
196
+ };
197
+ }
198
+
199
+ function isActiveGrant(
200
+ scope: AiOutputArtifactScope | undefined,
201
+ generation: number,
202
+ token: string,
203
+ ): boolean {
204
+ return Boolean(
205
+ scope &&
206
+ scope.status !== "cleaned" &&
207
+ scope.activeGrantGeneration === generation &&
208
+ scope.grantHash &&
209
+ matchesAiOutputArtifactGrant(token, scope.grantHash),
210
+ );
211
+ }
212
+
213
+ function pendingLeaseExpiry(now: Date, scopeTtlMs: number): string {
214
+ return new Date(
215
+ Math.min(now.getTime() + pendingGrantLeaseMs, now.getTime() + scopeTtlMs),
216
+ ).toISOString();
217
+ }
@@ -0,0 +1,175 @@
1
+ import { createHash, randomBytes } from "node:crypto";
2
+ import { parseAiRuntimeDeploymentIdentityV1 } from "@southwind-ai/server-sdk";
3
+ import { captureAuthorizationScope } from "../data-access/scope-snapshot.js";
4
+ import { isServerScopeContext } from "../data-access/context.js";
5
+ import type { RequestGuardContext } from "../guards.js";
6
+ import type { AiOutputArtifactPolicy } from "./policy.js";
7
+ import type {
8
+ AiOutputArtifactScope,
9
+ AiOutputRuntimeKind,
10
+ } from "./contracts.js";
11
+ import type { NewAiOutputScope } from "./repository.js";
12
+
13
+ export interface BeginAiOutputArtifactScopeInput {
14
+ readonly runId: string;
15
+ readonly moduleName: string;
16
+ readonly operationKey: string;
17
+ readonly runtimeKind: AiOutputRuntimeKind;
18
+ readonly idempotencyKey: string;
19
+ readonly invocationDigest: string;
20
+ readonly dataClassification: AiOutputArtifactScope["dataClassification"];
21
+ readonly deploymentIdentity: string;
22
+ readonly releaseDigest: string;
23
+ readonly offersDigest: string;
24
+ readonly policy: AiOutputArtifactPolicy;
25
+ readonly context: RequestGuardContext;
26
+ }
27
+
28
+ export function newAiOutputScope(
29
+ input: BeginAiOutputArtifactScopeInput,
30
+ pendingGrantHash: string,
31
+ pendingGrantExpiresAt: string,
32
+ now: Date,
33
+ ): NewAiOutputScope {
34
+ const scopeContext = input.context.actor.scopeContext;
35
+ if (
36
+ !/^[A-Za-z0-9][A-Za-z0-9._:-]{0,79}$/.test(input.runId) ||
37
+ !/^[A-Za-z0-9][A-Za-z0-9._:-]{0,159}$/.test(input.idempotencyKey) ||
38
+ !/^[a-f0-9]{64}$/.test(input.invocationDigest) ||
39
+ !/^[A-Za-z0-9._-]{1,128}$/.test(input.context.requestId) ||
40
+ !isServerScopeContext(scopeContext) ||
41
+ scopeContext.actorId !== input.context.actor.id ||
42
+ parseAiRuntimeDeploymentIdentityV1(input.deploymentIdentity) === null ||
43
+ !digest(input.releaseDigest) ||
44
+ !digest(input.offersDigest)
45
+ ) {
46
+ throw new Error("AI Output Scope 治理上下文无效");
47
+ }
48
+ if (
49
+ input.policy.runtimeKind !== input.runtimeKind ||
50
+ input.policy.dataClassification !== input.dataClassification
51
+ ) {
52
+ throw new Error("AI Output Scope 策略不匹配");
53
+ }
54
+ const scope = captureAuthorizationScope(scopeContext);
55
+ const identity = outputScopeIdentity(input);
56
+ const idempotencyHash = identity.idempotencyHash;
57
+ const fingerprint = identity.fingerprint;
58
+ const createdAt = now.toISOString();
59
+ return {
60
+ scopeId: opaque("outscope"),
61
+ runId: input.runId,
62
+ requestId: input.context.requestId,
63
+ actorId: input.context.actor.id,
64
+ tenantId: scopeContext.tenantId,
65
+ moduleName: input.moduleName,
66
+ operationKey: input.operationKey,
67
+ runtimeKind: input.runtimeKind,
68
+ outputKind: input.policy.outputKind,
69
+ purpose: input.policy.purpose,
70
+ dataClassification: input.dataClassification,
71
+ idempotencyHash,
72
+ fingerprint,
73
+ scopeHash: scope.hash,
74
+ scopeSnapshot: scope.snapshot,
75
+ deploymentIdentity: input.deploymentIdentity,
76
+ releaseDigest: input.releaseDigest,
77
+ offersDigest: input.offersDigest,
78
+ policyDigest: input.policy.digest,
79
+ revision: 1,
80
+ grantSequence: 1,
81
+ pendingGrantHash,
82
+ pendingGrantGeneration: 1,
83
+ pendingGrantExpiresAt,
84
+ status: "open",
85
+ allowedMediaTypes: [...input.policy.allowedMediaTypes],
86
+ maxArtifactBytes: input.policy.maxArtifactBytes,
87
+ maxArtifacts: input.policy.maxArtifacts,
88
+ maxTotalBytes: input.policy.maxTotalBytes,
89
+ scopeExpiresAt: plus(now, input.policy.scopeTtlMs),
90
+ fileExpiresAt: plus(
91
+ now,
92
+ input.policy.scopeTtlMs + input.policy.fileRetentionMs,
93
+ ),
94
+ refExpiresAt: plus(
95
+ now,
96
+ input.policy.scopeTtlMs + input.policy.referenceTtlMs,
97
+ ),
98
+ createdAt,
99
+ updatedAt: createdAt,
100
+ };
101
+ }
102
+
103
+ export function outputScopeIdentity(input: BeginAiOutputArtifactScopeInput) {
104
+ const scopeContext = input.context.actor.scopeContext;
105
+ if (!isServerScopeContext(scopeContext)) {
106
+ throw new Error("AI Output Scope 缺少授权范围");
107
+ }
108
+ const scope = captureAuthorizationScope(scopeContext);
109
+ const idempotencyHash = sha256(
110
+ [
111
+ input.context.actor.id,
112
+ input.moduleName,
113
+ input.operationKey,
114
+ input.runId,
115
+ input.policy.outputKind,
116
+ input.idempotencyKey,
117
+ ].join("\u0000"),
118
+ );
119
+ return {
120
+ idempotencyHash,
121
+ fingerprint: sha256(
122
+ canonical({
123
+ actorId: input.context.actor.id,
124
+ tenantId: scopeContext.tenantId,
125
+ moduleName: input.moduleName,
126
+ operationKey: input.operationKey,
127
+ runId: input.runId,
128
+ idempotencyHash,
129
+ invocationDigest: input.invocationDigest,
130
+ runtimeKind: input.runtimeKind,
131
+ outputKind: input.policy.outputKind,
132
+ dataClassification: input.dataClassification,
133
+ scopeHash: scope.hash,
134
+ deploymentIdentity: input.deploymentIdentity,
135
+ releaseDigest: input.releaseDigest,
136
+ offersDigest: input.offersDigest,
137
+ policyDigest: input.policy.digest,
138
+ maxArtifactBytes: input.policy.maxArtifactBytes,
139
+ maxArtifacts: input.policy.maxArtifacts,
140
+ maxTotalBytes: input.policy.maxTotalBytes,
141
+ }),
142
+ ),
143
+ };
144
+ }
145
+
146
+ export function outputArtifactFingerprint(input: {
147
+ outputKey: string;
148
+ outputKind: string;
149
+ filename: string;
150
+ mediaType: string;
151
+ byteSize: number;
152
+ sha256: string;
153
+ }) {
154
+ return sha256(canonical(input));
155
+ }
156
+
157
+ export function opaque(prefix: string) {
158
+ return `${prefix}_${randomBytes(16).toString("hex")}`;
159
+ }
160
+
161
+ function plus(now: Date, milliseconds: number) {
162
+ return new Date(now.getTime() + milliseconds).toISOString();
163
+ }
164
+
165
+ function digest(value: string) {
166
+ return /^sha256:[0-9a-f]{64}$/.test(value);
167
+ }
168
+
169
+ function sha256(value: string) {
170
+ return createHash("sha256").update(value).digest("hex");
171
+ }
172
+
173
+ function canonical(value: object) {
174
+ return JSON.stringify(value, Object.keys(value).sort());
175
+ }
@@ -0,0 +1,89 @@
1
+ import type { StorageRuntime } from "../storage/runtime.js";
2
+ import type {
3
+ AiOutputArtifactRecord,
4
+ AiOutputArtifactScope,
5
+ } from "./contracts.js";
6
+ import type { AiOutputArtifactReferenceCodec } from "./reference-codec.js";
7
+ import type { AiOutputArtifactRepository } from "./repository.js";
8
+ import { outputForbidden, validOutputReference } from "./service-support.js";
9
+
10
+ export interface AiOutputArtifactReadAuthorization {
11
+ readonly actorId?: string;
12
+ readonly scopeHash?: string;
13
+ readonly requestId: string;
14
+ }
15
+
16
+ export interface AiOutputArtifactReadDependencies {
17
+ readonly repository: AiOutputArtifactRepository;
18
+ readonly storage: StorageRuntime;
19
+ readonly references: AiOutputArtifactReferenceCodec;
20
+ readonly authorizeRead: (
21
+ scope: AiOutputArtifactScope,
22
+ signal: AbortSignal,
23
+ ) => Promise<AiOutputArtifactReadAuthorization | undefined>;
24
+ readonly recordRead: (
25
+ scope: AiOutputArtifactScope,
26
+ artifact: AiOutputArtifactRecord,
27
+ outcome: "success" | "denied",
28
+ requestId: string,
29
+ ) => void;
30
+ }
31
+
32
+ export async function openOutputArtifactReference(
33
+ options: AiOutputArtifactReadDependencies,
34
+ token: string,
35
+ now: string,
36
+ signal: AbortSignal,
37
+ ) {
38
+ let claims;
39
+ try {
40
+ claims = options.references.decode(token);
41
+ } catch {
42
+ throw outputForbidden();
43
+ }
44
+ const [scope, artifact] = await Promise.all([
45
+ options.repository.getScope(claims.scopeId),
46
+ options.repository.getArtifact(claims.artifactId),
47
+ ]);
48
+ if (
49
+ !scope ||
50
+ !artifact ||
51
+ !validOutputReference(scope, artifact, claims, now)
52
+ ) {
53
+ throw outputForbidden();
54
+ }
55
+ const authorization = await options.authorizeRead(scope, signal);
56
+ if (
57
+ !authorization ||
58
+ authorization.actorId !== scope.actorId ||
59
+ authorization.scopeHash !== scope.scopeHash
60
+ ) {
61
+ options.recordRead(
62
+ scope,
63
+ artifact,
64
+ "denied",
65
+ authorization?.requestId ?? scope.requestId,
66
+ );
67
+ throw outputForbidden();
68
+ }
69
+ try {
70
+ const opened = await options.storage.files.open(
71
+ claims.fileId,
72
+ authorization.actorId,
73
+ );
74
+ if (
75
+ opened.purpose !== scope.purpose ||
76
+ opened.mimeType !== claims.mediaType ||
77
+ opened.byteSize !== claims.byteSize ||
78
+ opened.contentHash.value !== claims.sha256
79
+ ) {
80
+ await opened.body.cancel().catch(() => undefined);
81
+ throw outputForbidden();
82
+ }
83
+ options.recordRead(scope, artifact, "success", authorization.requestId);
84
+ return opened;
85
+ } catch {
86
+ options.recordRead(scope, artifact, "denied", authorization.requestId);
87
+ throw outputForbidden();
88
+ }
89
+ }