@mostra/runtime 0.9.0 → 0.11.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 (279) hide show
  1. package/dist/bin.js +6628 -9
  2. package/dist/bin.js.map +1 -1
  3. package/dist/builder/child/main.js +2 -2
  4. package/dist/{child-Chiavhdx.js → child-BmNZ4a70.js} +2 -2
  5. package/dist/{child-Chiavhdx.js.map → child-BmNZ4a70.js.map} +1 -1
  6. package/dist/deps/child/install.js +1 -1
  7. package/dist/{directories-mJ1qlVht.js → directories-BLJjeSPD.js} +5 -5
  8. package/dist/directories-BLJjeSPD.js.map +1 -0
  9. package/dist/{files-FygSJIzK.js → files-DGeWuE4I.js} +2 -2
  10. package/dist/{files-FygSJIzK.js.map → files-DGeWuE4I.js.map} +1 -1
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +2 -2
  14. package/dist/{instance-EnaZPs14.js → instance-DCiUHvgZ.js} +3835 -255
  15. package/dist/instance-DCiUHvgZ.js.map +1 -0
  16. package/dist/reference/cli.md +1322 -0
  17. package/dist/reference/diagnostics.json +1552 -0
  18. package/dist/reference/errors/MOSTRA_A11Y_MINOR.md +23 -0
  19. package/dist/reference/errors/MOSTRA_A11Y_SERIOUS.md +23 -0
  20. package/dist/reference/errors/MOSTRA_ACTION_CANCELLED.md +23 -0
  21. package/dist/reference/errors/MOSTRA_ACTION_EVENT_INVALID.md +23 -0
  22. package/dist/reference/errors/MOSTRA_ACTION_EXPORT_INVALID.md +23 -0
  23. package/dist/reference/errors/MOSTRA_ACTION_INTERRUPTED.md +23 -0
  24. package/dist/reference/errors/MOSTRA_ACTION_PAUSED.md +23 -0
  25. package/dist/reference/errors/MOSTRA_ACTION_RUN_NOT_FOUND.md +23 -0
  26. package/dist/reference/errors/MOSTRA_ACTION_THREW.md +23 -0
  27. package/dist/reference/errors/MOSTRA_ACTION_TOO_LARGE.md +23 -0
  28. package/dist/reference/errors/MOSTRA_ACTION_UNKNOWN_FUNCTION.md +23 -0
  29. package/dist/reference/errors/MOSTRA_ACTION_UNSERIALIZABLE.md +23 -0
  30. package/dist/reference/errors/MOSTRA_ARTIFACT_BUNDLE_INVALID.md +23 -0
  31. package/dist/reference/errors/MOSTRA_ARTIFACT_DIR_NOT_EMPTY.md +23 -0
  32. package/dist/reference/errors/MOSTRA_ARTIFACT_ENTRY_AMBIGUOUS.md +23 -0
  33. package/dist/reference/errors/MOSTRA_ARTIFACT_ID_DROPPED.md +23 -0
  34. package/dist/reference/errors/MOSTRA_ARTIFACT_NOT_FOUND.md +23 -0
  35. package/dist/reference/errors/MOSTRA_ARTIFACT_NO_ENTRY.md +23 -0
  36. package/dist/reference/errors/MOSTRA_ARTIFACT_NO_PAGE.md +23 -0
  37. package/dist/reference/errors/MOSTRA_ARTIFACT_PATCH_REJECTED.md +23 -0
  38. package/dist/reference/errors/MOSTRA_ARTIFACT_PATH_INVALID.md +23 -0
  39. package/dist/reference/errors/MOSTRA_ARTIFACT_SOURCE_MISSING.md +23 -0
  40. package/dist/reference/errors/MOSTRA_ARTIFACT_SOURCE_UNREADABLE.md +23 -0
  41. package/dist/reference/errors/MOSTRA_ARTIFACT_TEMPLATE_UNKNOWN.md +23 -0
  42. package/dist/reference/errors/MOSTRA_ARTIFACT_VERSION_COLLECTED.md +23 -0
  43. package/dist/reference/errors/MOSTRA_ARTIFACT_VERSION_NOT_FOUND.md +23 -0
  44. package/dist/reference/errors/MOSTRA_BUILD_CRASHED.md +23 -0
  45. package/dist/reference/errors/MOSTRA_BUILD_FAILED.md +23 -0
  46. package/dist/reference/errors/MOSTRA_BUILD_TIMEOUT.md +23 -0
  47. package/dist/reference/errors/MOSTRA_BUILD_UNRESOLVED.md +23 -0
  48. package/dist/reference/errors/MOSTRA_CLI_CONFIG_UNWRITABLE.md +23 -0
  49. package/dist/reference/errors/MOSTRA_CLI_UNKNOWN_COMMAND.md +23 -0
  50. package/dist/reference/errors/MOSTRA_CLI_USAGE.md +23 -0
  51. package/dist/reference/errors/MOSTRA_CONSOLE_ERROR.md +23 -0
  52. package/dist/reference/errors/MOSTRA_CONSOLE_WARNING.md +23 -0
  53. package/dist/reference/errors/MOSTRA_DEP_INSTALL_FAILED.md +23 -0
  54. package/dist/reference/errors/MOSTRA_DEP_MISSING.md +23 -0
  55. package/dist/reference/errors/MOSTRA_DEP_NOT_FOUND.md +23 -0
  56. package/dist/reference/errors/MOSTRA_DEP_OFFLINE.md +23 -0
  57. package/dist/reference/errors/MOSTRA_DEP_SPEC_INVALID.md +23 -0
  58. package/dist/reference/errors/MOSTRA_DEP_TIMEOUT.md +23 -0
  59. package/dist/reference/errors/MOSTRA_DEP_WARM_CONFLICT.md +23 -0
  60. package/dist/reference/errors/MOSTRA_ENV_BIND_FAILED.md +23 -0
  61. package/dist/reference/errors/MOSTRA_ENV_BROWSER_MISSING.md +23 -0
  62. package/dist/reference/errors/MOSTRA_ENV_BUNDLE_DOWNLOAD_FAILED.md +23 -0
  63. package/dist/reference/errors/MOSTRA_ENV_BUNDLE_INVALID.md +23 -0
  64. package/dist/reference/errors/MOSTRA_ENV_BUNDLE_UNAVAILABLE.md +23 -0
  65. package/dist/reference/errors/MOSTRA_ENV_HELPER_MISSING.md +23 -0
  66. package/dist/reference/errors/MOSTRA_ENV_NODE_UNSUPPORTED.md +23 -0
  67. package/dist/reference/errors/MOSTRA_ENV_PORT_TAKEN.md +23 -0
  68. package/dist/reference/errors/MOSTRA_ENV_RUNTIME_INSTALLING.md +23 -0
  69. package/dist/reference/errors/MOSTRA_ENV_RUNTIME_MISSING.md +23 -0
  70. package/dist/reference/errors/MOSTRA_ENV_SERVICE_FAILED.md +23 -0
  71. package/dist/reference/errors/MOSTRA_ENV_SHELL_MISSING.md +23 -0
  72. package/dist/reference/errors/MOSTRA_ENV_WARM_MISSING.md +23 -0
  73. package/dist/reference/errors/MOSTRA_FIELD_CONFLICT.md +23 -0
  74. package/dist/reference/errors/MOSTRA_FIELD_DUPLICATE_ID.md +23 -0
  75. package/dist/reference/errors/MOSTRA_FIELD_ID_INVALID.md +23 -0
  76. package/dist/reference/errors/MOSTRA_FIELD_ID_UNSTABLE.md +23 -0
  77. package/dist/reference/errors/MOSTRA_FIELD_MUTATION_ID_REUSED.md +23 -0
  78. package/dist/reference/errors/MOSTRA_FIELD_SHARED.md +23 -0
  79. package/dist/reference/errors/MOSTRA_FIELD_TOO_LARGE.md +23 -0
  80. package/dist/reference/errors/MOSTRA_FIELD_UNBOUND.md +23 -0
  81. package/dist/reference/errors/MOSTRA_HOME_DISK_FULL.md +23 -0
  82. package/dist/reference/errors/MOSTRA_HOME_GC_INCOMPLETE.md +23 -0
  83. package/dist/reference/errors/MOSTRA_HOME_GONE.md +23 -0
  84. package/dist/reference/errors/MOSTRA_HOME_INTEGRITY.md +23 -0
  85. package/dist/reference/errors/MOSTRA_HOME_LEDGER_UNREADABLE.md +23 -0
  86. package/dist/reference/errors/MOSTRA_HOME_LOCKED.md +23 -0
  87. package/dist/reference/errors/MOSTRA_HOME_MISMATCH.md +23 -0
  88. package/dist/reference/errors/MOSTRA_HOME_NETWORK_FILESYSTEM.md +23 -0
  89. package/dist/reference/errors/MOSTRA_HOME_NOT_ALWAYS_ON.md +23 -0
  90. package/dist/reference/errors/MOSTRA_HOME_NOT_OWNED.md +23 -0
  91. package/dist/reference/errors/MOSTRA_HOME_SOCKET_TOO_LONG.md +23 -0
  92. package/dist/reference/errors/MOSTRA_HOME_TOO_NEW.md +23 -0
  93. package/dist/reference/errors/MOSTRA_HOST_CONNECTION_CLOSED.md +23 -0
  94. package/dist/reference/errors/MOSTRA_HOST_CURSOR_COMPACTED.md +23 -0
  95. package/dist/reference/errors/MOSTRA_HOST_IDENTITY_UNAVAILABLE.md +23 -0
  96. package/dist/reference/errors/MOSTRA_HOST_INITIALIZE_ORDER.md +23 -0
  97. package/dist/reference/errors/MOSTRA_HOST_INTERNAL_ERROR.md +23 -0
  98. package/dist/reference/errors/MOSTRA_HOST_INVALID_PARAMS.md +23 -0
  99. package/dist/reference/errors/MOSTRA_HOST_MALFORMED.md +23 -0
  100. package/dist/reference/errors/MOSTRA_HOST_MESSAGE_TOO_LARGE.md +23 -0
  101. package/dist/reference/errors/MOSTRA_HOST_METHOD_UNKNOWN.md +23 -0
  102. package/dist/reference/errors/MOSTRA_HOST_NOTIFICATION_UNSUPPORTED.md +23 -0
  103. package/dist/reference/errors/MOSTRA_HOST_PORT_UNSUPPORTED.md +23 -0
  104. package/dist/reference/errors/MOSTRA_HOST_REQUEST_ID_REUSED.md +23 -0
  105. package/dist/reference/errors/MOSTRA_HOST_REVISION_UNSUPPORTED.md +23 -0
  106. package/dist/reference/errors/MOSTRA_HOST_SHUTDOWN_REFUSED.md +23 -0
  107. package/dist/reference/errors/MOSTRA_HOST_SUBSCRIPTION_UNKNOWN.md +23 -0
  108. package/dist/reference/errors/MOSTRA_HOST_TIMEOUT.md +23 -0
  109. package/dist/reference/errors/MOSTRA_HYGIENE_CONTENT_TYPE.md +23 -0
  110. package/dist/reference/errors/MOSTRA_HYGIENE_CSRF_MISSING.md +23 -0
  111. package/dist/reference/errors/MOSTRA_HYGIENE_FRAME_SESSION.md +23 -0
  112. package/dist/reference/errors/MOSTRA_HYGIENE_HEADERS_TOO_LARGE.md +23 -0
  113. package/dist/reference/errors/MOSTRA_HYGIENE_HOST_REFUSED.md +23 -0
  114. package/dist/reference/errors/MOSTRA_HYGIENE_NOT_SERVED.md +23 -0
  115. package/dist/reference/errors/MOSTRA_HYGIENE_ORIGIN_REFUSED.md +23 -0
  116. package/dist/reference/errors/MOSTRA_HYGIENE_RATE_LIMITED.md +23 -0
  117. package/dist/reference/errors/MOSTRA_HYGIENE_REQUEST_MALFORMED.md +23 -0
  118. package/dist/reference/errors/MOSTRA_HYGIENE_SCOPE_DENIED.md +23 -0
  119. package/dist/reference/errors/MOSTRA_HYGIENE_SESSION_CONFLICT.md +23 -0
  120. package/dist/reference/errors/MOSTRA_HYGIENE_SESSION_MISSING.md +23 -0
  121. package/dist/reference/errors/MOSTRA_HYGIENE_TOKEN_INVALID.md +23 -0
  122. package/dist/reference/errors/MOSTRA_HYGIENE_TOKEN_REVOKED.md +23 -0
  123. package/dist/reference/errors/MOSTRA_LIVE_SOURCE_FAILED.md +23 -0
  124. package/dist/reference/errors/MOSTRA_LIVE_TOO_LARGE.md +23 -0
  125. package/dist/reference/errors/MOSTRA_MANIFEST_INVALID.md +23 -0
  126. package/dist/reference/errors/MOSTRA_MANIFEST_NOT_LITERAL.md +23 -0
  127. package/dist/reference/errors/MOSTRA_MANIFEST_UNKNOWN_KEY.md +23 -0
  128. package/dist/reference/errors/MOSTRA_NETWORK_EXTERNAL_FAILED.md +23 -0
  129. package/dist/reference/errors/MOSTRA_NETWORK_REQUEST_FAILED.md +23 -0
  130. package/dist/reference/errors/MOSTRA_PLATFORM_UNSUPPORTED.md +23 -0
  131. package/dist/reference/errors/MOSTRA_PUBLISH_ID_CONFLICT.md +23 -0
  132. package/dist/reference/errors/MOSTRA_PUBLISH_SOURCE_CHANGED.md +23 -0
  133. package/dist/reference/errors/MOSTRA_PUBLISH_VERIFY_FAILED.md +23 -0
  134. package/dist/reference/errors/MOSTRA_REGRESSION_ACTION_REMOVED.md +23 -0
  135. package/dist/reference/errors/MOSTRA_REGRESSION_ARIA.md +23 -0
  136. package/dist/reference/errors/MOSTRA_REGRESSION_FIELD_REMOVED.md +23 -0
  137. package/dist/reference/errors/MOSTRA_REGRESSION_LIVE_REMOVED.md +23 -0
  138. package/dist/reference/errors/MOSTRA_REGRESSION_PAGE_REMOVED.md +23 -0
  139. package/dist/reference/errors/MOSTRA_REGRESSION_PIXELS.md +23 -0
  140. package/dist/reference/errors/MOSTRA_REMOTE_CONFIG_INVALID.md +23 -0
  141. package/dist/reference/errors/MOSTRA_REMOTE_HOME_MOVING.md +23 -0
  142. package/dist/reference/errors/MOSTRA_REMOTE_HOME_SUSPENDED.md +23 -0
  143. package/dist/reference/errors/MOSTRA_REMOTE_LIMIT_REACHED.md +23 -0
  144. package/dist/reference/errors/MOSTRA_REMOTE_LOGIN_FAILED.md +23 -0
  145. package/dist/reference/errors/MOSTRA_REMOTE_NOT_OWNER_INSTANCE.md +23 -0
  146. package/dist/reference/errors/MOSTRA_REMOTE_NO_INSTANCE.md +23 -0
  147. package/dist/reference/errors/MOSTRA_REMOTE_OPERATOR_UNAVAILABLE.md +23 -0
  148. package/dist/reference/errors/MOSTRA_REMOTE_OWNER_OFFLINE.md +23 -0
  149. package/dist/reference/errors/MOSTRA_REMOTE_PAUSED_BY_OPERATOR.md +23 -0
  150. package/dist/reference/errors/MOSTRA_REMOTE_QUEUE_FULL.md +23 -0
  151. package/dist/reference/errors/MOSTRA_REMOTE_RATE_LIMITED.md +23 -0
  152. package/dist/reference/errors/MOSTRA_REMOTE_SIGN_IN_REQUIRED.md +23 -0
  153. package/dist/reference/errors/MOSTRA_REMOTE_UNDER_REVIEW.md +23 -0
  154. package/dist/reference/errors/MOSTRA_REMOTE_UNREACHABLE.md +23 -0
  155. package/dist/reference/errors/MOSTRA_RUNTIME_BLANK.md +23 -0
  156. package/dist/reference/errors/MOSTRA_RUNTIME_NOT_READY.md +23 -0
  157. package/dist/reference/errors/MOSTRA_RUNTIME_PAGE_ERROR.md +23 -0
  158. package/dist/reference/errors/MOSTRA_RUNTIME_WORKER_CRASHED.md +23 -0
  159. package/dist/reference/errors/MOSTRA_RUNTIME_WORKER_PARKED.md +23 -0
  160. package/dist/reference/errors/MOSTRA_SECRET_MISSING.md +23 -0
  161. package/dist/reference/errors/MOSTRA_SHARE_NOT_ENABLED.md +23 -0
  162. package/dist/reference/errors/MOSTRA_SHARE_NOT_FOUND.md +23 -0
  163. package/dist/reference/errors/MOSTRA_SHARE_PORTS_EXHAUSTED.md +23 -0
  164. package/dist/reference/errors/MOSTRA_SHARE_TOOL_MISSING.md +23 -0
  165. package/dist/reference/errors/MOSTRA_SHARE_TOOL_NOT_READY.md +23 -0
  166. package/dist/reference/errors/MOSTRA_SHARE_TTL_INVALID.md +23 -0
  167. package/dist/reference/errors/MOSTRA_SHARE_TUNNEL_FAILED.md +23 -0
  168. package/dist/reference/errors/MOSTRA_SMOKE_BUDGET.md +23 -0
  169. package/dist/reference/errors/MOSTRA_SMOKE_NO_EFFECT.md +23 -0
  170. package/dist/reference/errors/MOSTRA_SUBMIT_ALREADY.md +23 -0
  171. package/dist/reference/errors/MOSTRA_SUBMIT_BLOCKED.md +23 -0
  172. package/dist/reference/errors/MOSTRA_SUBMIT_CHANNEL_DENIED.md +23 -0
  173. package/dist/reference/errors/MOSTRA_SUBMIT_NOT_FOUND.md +23 -0
  174. package/dist/reference/errors/MOSTRA_SUBMIT_PAUSED.md +23 -0
  175. package/dist/reference/errors/MOSTRA_SUBMIT_POLICY_NONE.md +23 -0
  176. package/dist/reference/errors/MOSTRA_SUBMIT_REVISION_MOVED.md +23 -0
  177. package/dist/reference/errors/MOSTRA_SUBMIT_SIGNAL_INVALID.md +23 -0
  178. package/dist/reference/errors/MOSTRA_SUBMIT_VIEW_INVALID.md +23 -0
  179. package/dist/reference/errors/MOSTRA_TARGET_UNAVAILABLE.md +23 -0
  180. package/dist/reference/errors/MOSTRA_TYPE_ERROR.md +23 -0
  181. package/dist/reference/errors/MOSTRA_TYPE_UNTYPED_PACKAGE.md +23 -0
  182. package/dist/reference/errors/MOSTRA_VERIFY_ACTION_SKIPPED.md +23 -0
  183. package/dist/reference/errors/MOSTRA_VERIFY_BASELINE_MISSING.md +23 -0
  184. package/dist/reference/errors/MOSTRA_VERIFY_CRASHED.md +23 -0
  185. package/dist/reference/errors/MOSTRA_VERIFY_LIVE_FAILED.md +23 -0
  186. package/dist/reference/errors/MOSTRA_VERIFY_NO_BASELINE.md +23 -0
  187. package/dist/reference/errors/MOSTRA_VERIFY_SECRET_MISSING.md +23 -0
  188. package/dist/reference/errors/MOSTRA_VERIFY_SKIP_CONSEQUENCE.md +23 -0
  189. package/dist/reference/errors/MOSTRA_VERIFY_TIMEOUT.md +23 -0
  190. package/dist/reference/self-hosting.md +130 -0
  191. package/dist/reference/telemetry.md +84 -0
  192. package/dist/skill/SKILL.md +122 -0
  193. package/dist/skill/assets/examples/dashboard/index.tsx +161 -0
  194. package/dist/skill/assets/examples/dashboard/live.ts +25 -0
  195. package/dist/skill/assets/examples/dashboard/styles.css +125 -0
  196. package/dist/skill/assets/examples/decision/index.tsx +95 -0
  197. package/dist/skill/assets/examples/decision/styles.css +125 -0
  198. package/dist/skill/assets/examples/inbox/actions.ts +23 -0
  199. package/dist/skill/assets/examples/inbox/index.tsx +109 -0
  200. package/dist/skill/assets/examples/inbox/styles.css +125 -0
  201. package/dist/skill/assets/studio.css +125 -0
  202. package/dist/skill/references/answers.md +217 -0
  203. package/dist/skill/references/design.md +154 -0
  204. package/dist/skill/references/libraries.md +167 -0
  205. package/dist/skill/references/long-lived.md +148 -0
  206. package/dist/skill/references/mcp.md +68 -0
  207. package/dist/skill/references/page.md +131 -0
  208. package/dist/skill/references/server.md +146 -0
  209. package/dist/skill/references/sharing.md +123 -0
  210. package/dist/skill/references/verify.md +71 -0
  211. package/package.json +12 -5
  212. package/src/actions/index.ts +163 -10
  213. package/src/bin.ts +78 -1
  214. package/src/gateway/actions.ts +40 -4
  215. package/src/gateway/browser.ts +162 -95
  216. package/src/gateway/capabilities.ts +23 -6
  217. package/src/gateway/http.ts +12 -1
  218. package/src/gateway/index.ts +282 -20
  219. package/src/gateway/listener.ts +31 -6
  220. package/src/gateway/routes.ts +23 -7
  221. package/src/gc/index.ts +19 -0
  222. package/src/instance/index.ts +60 -2
  223. package/src/interaction/ephemeral.ts +32 -23
  224. package/src/interaction/events.ts +21 -1
  225. package/src/interaction/imports.ts +317 -0
  226. package/src/interaction/index.ts +228 -15
  227. package/src/interaction/outbox.ts +9 -0
  228. package/src/interaction/pause.ts +106 -0
  229. package/src/interaction/results.ts +8 -1
  230. package/src/interaction/runs.ts +52 -2
  231. package/src/interaction/submissions.ts +59 -2
  232. package/src/interaction/tally.ts +129 -0
  233. package/src/interaction/viewers.ts +34 -1
  234. package/src/ledger/index.ts +124 -10
  235. package/src/ledger/migrations.ts +234 -0
  236. package/src/ledger/types.ts +8 -0
  237. package/src/link/author.ts +215 -0
  238. package/src/link/binding.ts +289 -0
  239. package/src/link/hosted.ts +101 -0
  240. package/src/link/http.ts +141 -0
  241. package/src/link/index.ts +10 -0
  242. package/src/link/manager.ts +1489 -0
  243. package/src/link/relay.ts +512 -0
  244. package/src/live/index.ts +112 -2
  245. package/src/platform/filesystems.ts +78 -0
  246. package/src/platform/index.ts +1 -0
  247. package/src/protocol-server/client.ts +53 -0
  248. package/src/protocol-server/index.ts +19 -2
  249. package/src/protocol-server/server.ts +38 -7
  250. package/src/protocol-server/websocket.ts +161 -0
  251. package/src/publish/bundle.ts +398 -0
  252. package/src/publish/manifest.ts +9 -0
  253. package/src/publish/methods.ts +12 -1
  254. package/src/publish/publisher.ts +127 -0
  255. package/src/registry/index.ts +35 -14
  256. package/src/server/api.ts +881 -0
  257. package/src/server/auth.ts +1139 -0
  258. package/src/server/authorize.ts +816 -0
  259. package/src/server/clients.ts +235 -0
  260. package/src/server/config.ts +228 -0
  261. package/src/server/data.ts +427 -0
  262. package/src/server/directory.ts +210 -0
  263. package/src/server/feed.ts +191 -0
  264. package/src/server/handle.ts +172 -0
  265. package/src/server/hosted-pages.ts +532 -0
  266. package/src/server/hosted.ts +1323 -0
  267. package/src/server/index.ts +650 -0
  268. package/src/server/mcp.ts +330 -0
  269. package/src/server/metering.ts +156 -0
  270. package/src/server/oauth-clients.ts +510 -0
  271. package/src/server/operator.ts +498 -0
  272. package/src/server/providers.ts +284 -0
  273. package/src/server/relay.ts +1163 -0
  274. package/src/server/remove.ts +148 -0
  275. package/src/server/routing.ts +275 -0
  276. package/src/share/index.ts +110 -6
  277. package/src/verify/runner.ts +23 -2
  278. package/dist/directories-mJ1qlVht.js.map +0 -1
  279. package/dist/instance-EnaZPs14.js.map +0 -1
@@ -17,6 +17,7 @@ import {
17
17
  type CapabilityScope,
18
18
  type CarryOver,
19
19
  type ControlsPolicy,
20
+ type Cursor,
20
21
  createDiagnostic,
21
22
  type DeliveryCounts,
22
23
  type EventFilter,
@@ -30,13 +31,16 @@ import {
30
31
  type FieldState,
31
32
  MutationId,
32
33
  type MutationResult,
34
+ type PausedBy,
33
35
  type Results,
34
36
  RpcError,
35
37
  type RunState,
36
38
  refusal,
37
39
  resultsGetMethod,
38
40
  type StreamSnapshot,
41
+ type SubmissionEnvelope,
39
42
  type SubmissionSummary,
43
+ type SubmissionsDeliverResult,
40
44
  type SubmitChannel,
41
45
  scopeAllows,
42
46
  stateGetMethod,
@@ -57,7 +61,9 @@ import {
57
61
  liveFields,
58
62
  replayedMutation,
59
63
  } from "./fields.ts";
64
+ import { type ImportSource, importSubmission } from "./imports.ts";
60
65
  import { type HostConnections, Outbox } from "./outbox.ts";
66
+ import { type Pause, pauseOf, setPause } from "./pause.ts";
61
67
  import { buildResults, CHOICE, resultsMarkdown, submissionFor } from "./results.ts";
62
68
  import {
63
69
  appendRunOutput,
@@ -81,15 +87,20 @@ import {
81
87
  orphansOf,
82
88
  type Snapshot,
83
89
  type SubmissionRow,
90
+ SubmitConflict,
84
91
  submissionSummary,
85
92
  submit,
93
+ wouldRecord,
86
94
  } from "./submissions.ts";
87
95
  import { Subscriptions } from "./subscriptions.ts";
88
96
  import {
89
97
  localViewerId,
98
+ nameAnonymous,
90
99
  openView,
91
100
  viewControls,
92
101
  viewer,
102
+ viewerForAccount,
103
+ viewerForAnonymous,
93
104
  viewerForHost,
94
105
  viewerForLink,
95
106
  viewerForSpec,
@@ -100,10 +111,23 @@ import { waitFor } from "./wait.ts";
100
111
 
101
112
  export { createEphemeralBus, type EphemeralBus } from "./ephemeral.ts";
102
113
  export { appendEvent, compactEvents, redactEvents } from "./events.ts";
114
+ export { followEvents, type ImportSource, importedDeliveryId } from "./imports.ts";
103
115
  export type { HostConnections } from "./outbox.ts";
104
116
  export { policyInForce } from "./policy.ts";
105
- export type { RunEnd, RunFlush, RunOutput, RunRow } from "./runs.ts";
117
+ export {
118
+ appendRunOutput,
119
+ finishRun,
120
+ markRunning,
121
+ markUnknown,
122
+ markWaiting,
123
+ type RunEnd,
124
+ type RunFlush,
125
+ type RunOutput,
126
+ type RunRow,
127
+ runState,
128
+ } from "./runs.ts";
106
129
  export { SubmitConflict } from "./submissions.ts";
130
+ export { tallyField, tallyState } from "./tally.ts";
107
131
 
108
132
  /** How the interaction core finds an artifact a host names. */
109
133
  export interface InteractionArtifacts {
@@ -132,8 +156,30 @@ export interface InteractionOptions {
132
156
  readonly coverage: (versionId: string) => (fieldId: string) => boolean;
133
157
  /** How long a host has to answer a port call (30 s; tests shorten it). */
134
158
  readonly portTimeoutMs?: number;
159
+ /**
160
+ * Who a submission of this artifact is delivered to, instead of every registered or expected
161
+ * host: on a hosted home, the artifact's owner link alone, connected or not (D165, after the Opus
162
+ * review). `undefined` keeps D105's rule.
163
+ */
164
+ readonly deliveryOwners?: (artifactId: string) => readonly string[] | undefined;
165
+ /** Whether deliveries carry the stored snapshot: a hosted home's do (D165 item 1). */
166
+ readonly deliverSnapshots?: boolean;
167
+ /**
168
+ * Whether pages see only that an answer was sent (D165 item 1a, IN-13 for hosted links): a
169
+ * submission's `delivery` reads `delivered` once it committed, without `deliveries`, and pages
170
+ * get no delivery events. The owner, its agent and hosts keep every delivery state.
171
+ */
172
+ readonly sentOnly?: boolean;
135
173
  }
136
174
 
175
+ /** The delivery events only the owner, its agent and hosts see on a hosted home (D165 item 1a). */
176
+ const DELIVERY_EVENTS = new Set([
177
+ "submission.retrying",
178
+ "submission.delivered",
179
+ "submission.rejected",
180
+ "submission.undeliverable",
181
+ ]);
182
+
137
183
  /** A browser session as the interaction core sees it: who it acts as. */
138
184
  export interface PageIdentity {
139
185
  readonly artifactId: string;
@@ -151,6 +197,17 @@ export interface Interaction {
151
197
  /** Stops delivering; pending deliveries stay in the outbox. */
152
198
  stop(): void;
153
199
 
200
+ /**
201
+ * Pauses or resumes an artifact as its owner or its server's operator (D165 item 14, RM-09);
202
+ * the owner resuming an operator's pause is `MOSTRA_REMOTE_PAUSED_BY_OPERATOR`.
203
+ */
204
+ pause(
205
+ artifactId: string,
206
+ change: { readonly paused: boolean; readonly by: PausedBy; readonly reason?: string },
207
+ ): { readonly paused: boolean; readonly by?: PausedBy };
208
+ /** The artifact's pause, or `undefined` when it isn't paused. */
209
+ pauseOf(artifactId: string): Pause | undefined;
210
+
154
211
  /** The viewer a capability token's sessions act as (D104): its own, or the home's local user. */
155
212
  viewerOf(tokenViewerId: string | undefined): string;
156
213
  /** A viewer by ID (D104). */
@@ -163,6 +220,12 @@ export interface Interaction {
163
220
  viewerForTailnet(spec: ViewerSpec): string;
164
221
  /** A link's own viewer, for a tunnel visitor nobody named (D158 item 2). */
165
222
  viewerForLink(tokenId: string, name: string): string;
223
+ /** A signed-in account on a hosted link (D165 item 8), named as its provider verified it. */
224
+ viewerForAccount(accountId: string, name?: string): string;
225
+ /** An anonymous viewer on a hosted link, by the route's derived browser key (D165 item 8). */
226
+ viewerForAnonymous(key: string): string;
227
+ /** Sets an anonymous viewer's self-chosen name, unverified; `false` for any other viewer. */
228
+ nameAnonymous(viewerId: string, name: string | null): boolean;
166
229
  /** Records a frame load of a version (D104), with the controls in force (D108); returns its ID. */
167
230
  openView(input: {
168
231
  readonly artifactId: string;
@@ -185,6 +248,26 @@ export interface Interaction {
185
248
  readonly ops: readonly FieldOp[];
186
249
  },
187
250
  ): MutationResult;
251
+ /**
252
+ * Stores a submission its hosted home delivered to this owner instance, with its local
253
+ * deliveries, in one transaction (D165 item 1), and answers as a host answers a delivery.
254
+ */
255
+ importSubmission(envelope: SubmissionEnvelope, source: ImportSource): SubmissionsDeliverResult;
256
+ /**
257
+ * A submission as its delivery to `owner` carries it, with its snapshot (a hosted home's
258
+ * `hosted/submission`, FAILURE_MODEL §11.2); `undefined` when `owner` has no delivery of it.
259
+ */
260
+ envelopeFor(submissionId: string, owner: string): SubmissionEnvelope | undefined;
261
+ /** A submission's full Results (§8), built from the immutable submission alone. */
262
+ resultsOf(submissionId: string): Results | undefined;
263
+ /**
264
+ * Whether a page's submit would be judged afresh: not a repeat of a recorded one, not refused by
265
+ * its view or a pause. Only such a submit is admitted by a hosted home's rates and limits.
266
+ */
267
+ wouldRecord(
268
+ page: PageIdentity,
269
+ input: { readonly viewId: string; readonly expectedRevision: number },
270
+ ): boolean;
188
271
  /** A viewer's submit (IN-05); `SubmitConflict` under a closed `once`. */
189
272
  submit(
190
273
  page: PageIdentity,
@@ -288,6 +371,8 @@ export interface Interaction {
288
371
  openRuns(versionId?: string): RunRow[];
289
372
  /** A run's state now. */
290
373
  runState(runId: string): RunState | undefined;
374
+ /** A run's row, for the actions module (a relayed run, D165 item 4). */
375
+ runRow(runId: string): RunRow | undefined;
291
376
  /** `signal()` from server code: `signal.raised`, source `action`, the run as cause (AC-05). */
292
377
  serverSignal(
293
378
  artifactId: string,
@@ -341,6 +426,11 @@ export interface Interaction {
341
426
  * session reports what its preview's submits would deliver (D140 item 12). Returns the stop.
342
427
  */
343
428
  onSubmission(listener: (results: Results) => void): () => void;
429
+ /**
430
+ * Hears, once a submission commits, which of its artifact's tallied fields it counted (D165
431
+ * item 12), so their viewers are sent the new counts. Returns the stop.
432
+ */
433
+ onTallied(listener: (artifactId: string, fields: readonly string[]) => void): () => void;
344
434
  /** How many host subscriptions are open. */
345
435
  subscriptionCount(): number;
346
436
  }
@@ -449,6 +539,24 @@ function runVisible(row: RunRow, page: PageIdentity): boolean {
449
539
  /** Creates the interaction core. */
450
540
 
451
541
  export function createInteraction(options: InteractionOptions): Interaction {
542
+ /** A submission as a page gets it: on a hosted home, sent and nothing about its delivery. */
543
+ const forPage = (summary: SubmissionSummary): SubmissionSummary => {
544
+ if (!options.sentOnly) return summary;
545
+ const { deliveries: _deliveries, ...sent } = summary;
546
+ return { ...sent, delivery: "delivered" };
547
+ };
548
+ /** Runs a page's write, giving a conflict's submission as the page gets it. */
549
+ const asPage = <T>(write: () => T): T => {
550
+ try {
551
+ return write();
552
+ } catch (error) {
553
+ if (options.sentOnly && error instanceof SubmitConflict) {
554
+ throw new SubmitConflict(error.diagnostic.message, forPage(error.submission));
555
+ }
556
+ throw error;
557
+ }
558
+ };
559
+ const tallyListeners = new Set<(artifactId: string, fields: readonly string[]) => void>();
452
560
  const { ledger, log, hosts } = options;
453
561
  const outbox = new Outbox({
454
562
  ledger,
@@ -457,6 +565,7 @@ export function createInteraction(options: InteractionOptions): Interaction {
457
565
  expected: options.expectedHosts,
458
566
  faults: options.faults,
459
567
  ...(options.portTimeoutMs !== undefined ? { portTimeoutMs: options.portTimeoutMs } : {}),
568
+ ...(options.deliverSnapshots ? { snapshots: true } : {}),
460
569
  });
461
570
  const subscriptions = new Subscriptions({
462
571
  ledger,
@@ -512,6 +621,12 @@ export function createInteraction(options: InteractionOptions): Interaction {
512
621
  viewerForHost: (spec) => ledger.write((db) => viewerForHost(db, spec)),
513
622
  viewerForTailnet: (spec) => ledger.write((db) => viewerForTailnet(db, spec)),
514
623
  viewerForLink: (tokenId, name) => ledger.write((db) => viewerForLink(db, tokenId, name)),
624
+ viewerForAccount: (accountId, name) =>
625
+ ledger.write((db) => viewerForAccount(db, accountId, name)),
626
+ viewerForAnonymous: (key) => ledger.write((db) => viewerForAnonymous(db, key)),
627
+ nameAnonymous: (viewerId, name) => ledger.write((db) => nameAnonymous(db, viewerId, name)),
628
+ pause: (artifactId, change) => ledger.write((db) => setPause(db, artifactId, change)),
629
+ pauseOf: (artifactId) => ledger.read((db) => pauseOf(db, artifactId)),
515
630
  openView(input) {
516
631
  return ledger.write((db) => openView(db, input));
517
632
  },
@@ -541,7 +656,7 @@ export function createInteraction(options: InteractionOptions): Interaction {
541
656
  const replay = replayedMutation(db, mutation);
542
657
  if (replay) return { result: replay, changed: false, replayed: true };
543
658
  if (input.scope === "viewer") {
544
- assertDraftOpen(db, page.artifactId, page.viewerId);
659
+ asPage(() => assertDraftOpen(db, page.artifactId, page.viewerId));
545
660
  if (input.ops.some((op) => CHOICE.test(op.id))) {
546
661
  // A choice needs the `review` scope however it is written, as the trusted Choose
547
662
  // does (D97; Claude review finding 7)…
@@ -569,20 +684,98 @@ export function createInteraction(options: InteractionOptions): Interaction {
569
684
  outbox.mirror(page.artifactId, page.viewerId);
570
685
  return result;
571
686
  },
572
- submit(page, input) {
573
- const owners = outbox.owners();
574
- const outcome = ledger.write((db) =>
575
- submit(db, {
687
+ importSubmission(envelope, source) {
688
+ const owners = options.deliveryOwners?.(envelope.artifactId) ?? outbox.owners();
689
+ const answer = ledger.write((db) =>
690
+ importSubmission(db, {
691
+ envelope,
692
+ source,
693
+ owners,
694
+ owned: (inner, artifactId) =>
695
+ inner.get(
696
+ "SELECT 1 FROM hosted_artifact WHERE artifact_id = ? AND server = ?",
697
+ artifactId,
698
+ source.server,
699
+ ) !== undefined,
700
+ }),
701
+ );
702
+ log.info("interaction.imported", {
703
+ artifactId: envelope.artifactId,
704
+ submissionId: envelope.submissionId,
705
+ accepted: answer.accepted,
706
+ });
707
+ return answer;
708
+ },
709
+ resultsOf(submissionId) {
710
+ return ledger.read((db) => {
711
+ const submission = db.get<SubmissionRow>(
712
+ "SELECT * FROM submission WHERE id = ?",
713
+ submissionId,
714
+ );
715
+ return submission ? buildResults(db, submission) : undefined;
716
+ });
717
+ },
718
+ envelopeFor(submissionId, owner) {
719
+ return ledger.read((db) => {
720
+ const delivery = db.get<{ id: string; attempts: number }>(
721
+ "SELECT id, attempts FROM delivery WHERE submission_id = ? AND port_owner = ?",
722
+ submissionId,
723
+ owner,
724
+ );
725
+ const submission = db.get<SubmissionRow>(
726
+ "SELECT * FROM submission WHERE id = ?",
727
+ submissionId,
728
+ );
729
+ if (!delivery || !submission) return undefined;
730
+ return {
731
+ kind: "submission",
732
+ deliveryId: delivery.id,
733
+ submissionId: submission.id,
734
+ artifactId: submission.artifact_id,
735
+ attempt: Math.max(1, delivery.attempts),
736
+ results: buildResults(db, submission),
737
+ snapshot: submission.snapshot_json,
738
+ cursor: String(submission.position) as Cursor,
739
+ };
740
+ });
741
+ },
742
+ wouldRecord(page, input) {
743
+ return ledger.read((db) =>
744
+ wouldRecord(db, {
576
745
  artifactId: page.artifactId,
577
746
  viewerId: page.viewerId,
578
747
  ...(page.tokenId !== undefined ? { tokenId: page.tokenId } : {}),
579
748
  viewId: input.viewId,
580
749
  expectedRevision: input.expectedRevision,
581
- channel: input.channel,
582
- owners,
583
- covers: options.coverage,
584
750
  }),
585
751
  );
752
+ },
753
+
754
+ submit(page, input) {
755
+ const owners = options.deliveryOwners?.(page.artifactId) ?? outbox.owners();
756
+ const outcome = asPage(() =>
757
+ ledger.write((db) =>
758
+ submit(db, {
759
+ artifactId: page.artifactId,
760
+ viewerId: page.viewerId,
761
+ ...(page.tokenId !== undefined ? { tokenId: page.tokenId } : {}),
762
+ viewId: input.viewId,
763
+ expectedRevision: input.expectedRevision,
764
+ channel: input.channel,
765
+ owners,
766
+ covers: options.coverage,
767
+ }),
768
+ ),
769
+ );
770
+ if (outcome.tallied) {
771
+ for (const listener of tallyListeners) {
772
+ try {
773
+ listener(page.artifactId, outcome.tallied);
774
+ } catch (error) {
775
+ log.error("interaction.tally_listener_failed", { error });
776
+ }
777
+ }
778
+ }
586
779
  if (outcome.created) {
587
780
  log.info("interaction.submitted", {
588
781
  artifactId: page.artifactId,
@@ -599,8 +792,8 @@ export function createInteraction(options: InteractionOptions): Interaction {
599
792
  );
600
793
  return row ? submissionSummary(db, row) : undefined;
601
794
  });
602
- if (!fresh) return outcome.submission;
603
- return outcome.submission.duplicate ? { ...fresh, duplicate: true } : fresh;
795
+ if (!fresh) return forPage(outcome.submission);
796
+ return forPage(outcome.submission.duplicate ? { ...fresh, duplicate: true } : fresh);
604
797
  },
605
798
  choose(page, input) {
606
799
  const actor = pageActor(page);
@@ -627,7 +820,7 @@ export function createInteraction(options: InteractionOptions): Interaction {
627
820
  };
628
821
  const replay = replayedMutation(db, mutation);
629
822
  if (replay) return { result: replay, changed: false, replayed: true };
630
- assertDraftOpen(db, page.artifactId, page.viewerId);
823
+ asPage(() => assertDraftOpen(db, page.artifactId, page.viewerId));
631
824
  return applyMutation(db, mutation);
632
825
  });
633
826
  if (outcome.changed) outbox.mirror(page.artifactId, page.viewerId);
@@ -710,7 +903,7 @@ export function createInteraction(options: InteractionOptions): Interaction {
710
903
  const state = documentSnapshot(db, page.artifactId, null);
711
904
  return { ...state, fields: fieldsForPage(state.fields, page.viewerId) };
712
905
  })(),
713
- ...(latestOwn ? { submission: submissionSummary(db, latestOwn) } : {}),
906
+ ...(latestOwn ? { submission: forPage(submissionSummary(db, latestOwn)) } : {}),
714
907
  ...(() => {
715
908
  // The latest version, so a page that missed its `version.published` learns of it.
716
909
  const latest = db.get<{ id: string; number: number }>(
@@ -727,6 +920,15 @@ export function createInteraction(options: InteractionOptions): Interaction {
727
920
  // Read in pages until enough visible events or the end, so other viewers' events never
728
921
  // stall a page.
729
922
  return ledger.read((db) => {
923
+ // A cursor above the head was read from a home since restored to an earlier copy (§18.2):
924
+ // the page starts again from a fresh snapshot, never from an empty tail.
925
+ if (after > latestCursor(db)) {
926
+ throw new RpcError(
927
+ createDiagnostic("MOSTRA_HOST_CURSOR_COMPACTED", {
928
+ message: "The page's cursor is past this home's head; it reads a fresh snapshot.",
929
+ }),
930
+ );
931
+ }
730
932
  const events: EventRecord[] = [];
731
933
  let cursor = after;
732
934
  const runs = new Map<string, RunRow | undefined>();
@@ -743,6 +945,7 @@ export function createInteraction(options: InteractionOptions): Interaction {
743
945
  for (const event of read.events) {
744
946
  if (events.length === limit) return { events, cursor: String(cursor) };
745
947
  cursor = Number(event.cursor);
948
+ if (options.sentOnly && DELIVERY_EVENTS.has(event.type)) continue;
746
949
  if (visibleTo(event, page, runOf)) events.push(eventForPage(event, page.viewerId));
747
950
  }
748
951
  cursor = Math.max(cursor, Number(read.cursor));
@@ -780,7 +983,10 @@ export function createInteraction(options: InteractionOptions): Interaction {
780
983
  ? { baseRevision: Number(params.baseRevision) }
781
984
  : {}),
782
985
  ops: params.ops,
783
- actor: clientActor(session, params.as),
986
+ actor:
987
+ params.causation !== undefined
988
+ ? { kind: "agent", id: params.causation.runId }
989
+ : clientActor(session, params.as),
784
990
  }).result,
785
991
  );
786
992
  }),
@@ -828,7 +1034,7 @@ export function createInteraction(options: InteractionOptions): Interaction {
828
1034
  ],
829
1035
  connected(session) {
830
1036
  if (session.ports.has("submissions") && session.client)
831
- outbox.registered(session.client.name);
1037
+ outbox.registered(session.link?.instanceId ?? session.client.name);
832
1038
  },
833
1039
  disconnected(session) {
834
1040
  subscriptions.closed(session.id);
@@ -900,6 +1106,9 @@ export function createInteraction(options: InteractionOptions): Interaction {
900
1106
  openRuns(versionId) {
901
1107
  return ledger.read((db) => openRuns(db, versionId));
902
1108
  },
1109
+ runRow(runId) {
1110
+ return ledger.read((db) => db.get<RunRow>("SELECT * FROM action_run WHERE id = ?", runId));
1111
+ },
903
1112
  runState(runId) {
904
1113
  return ledger.read((db) => {
905
1114
  const row = db.get<RunRow>("SELECT * FROM action_run WHERE id = ?", runId);
@@ -1016,6 +1225,10 @@ export function createInteraction(options: InteractionOptions): Interaction {
1016
1225
  }
1017
1226
  });
1018
1227
  },
1228
+ onTallied(listener) {
1229
+ tallyListeners.add(listener);
1230
+ return () => tallyListeners.delete(listener);
1231
+ },
1019
1232
  subscriptionCount: () => subscriptions.size,
1020
1233
  };
1021
1234
  }
@@ -8,6 +8,7 @@
8
8
  * delivery the host acknowledged but Mostra didn't record goes out again with the same ID.
9
9
  */
10
10
  import {
11
+ type Cursor,
11
12
  type DeliveryState,
12
13
  type DraftMirror,
13
14
  MAX_LINE_LENGTH,
@@ -61,6 +62,11 @@ export interface OutboxOptions {
61
62
  readonly now?: () => number;
62
63
  /** How long a host has to answer a port call; default {@link PORT_CALL_TIMEOUT_MS}. */
63
64
  readonly portTimeoutMs?: number;
65
+ /**
66
+ * Whether envelopes carry the stored snapshot: a hosted home's do, so its owner instance keeps
67
+ * the same submission (D165 item 1).
68
+ */
69
+ readonly snapshots?: boolean;
64
70
  }
65
71
 
66
72
  interface DeliveryRow {
@@ -260,6 +266,9 @@ export class Outbox {
260
266
  artifactId: submission.artifact_id,
261
267
  attempt,
262
268
  results: buildResults(db, submission),
269
+ ...(this.#options.snapshots
270
+ ? { snapshot: submission.snapshot_json, cursor: String(submission.position) as Cursor }
271
+ : {}),
263
272
  };
264
273
  return envelope;
265
274
  });
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Pausing an artifact (D165 item 14, RM-09): a paused artifact stays viewable and its shell says
3
+ * so; it takes no submissions (the submit transaction refuses them) and runs no actions until it
4
+ * is resumed, and every draft is kept. The owner pauses and resumes its own pause; an operator's
5
+ * pause is resumed only by the operator. One row per party that paused it (`artifact_pause`,
6
+ * DATA_MODEL §7.2), in local and hosted homes alike; each change appends `artifact.paused` or
7
+ * `artifact.resumed`, so open pages learn of it.
8
+ */
9
+ import { createDiagnostic, type PausedBy, RpcError } from "@mostra/protocol";
10
+ import type { LedgerDb } from "../ledger/index.ts";
11
+ import { appendEvent } from "./events.ts";
12
+
13
+ /** An artifact's pause, while it is paused. */
14
+ export interface Pause {
15
+ readonly by: PausedBy;
16
+ readonly reason?: string;
17
+ readonly at: string;
18
+ }
19
+
20
+ /**
21
+ * The artifact's pause, or `undefined` when it isn't paused: the operator's when both paused it,
22
+ * since only the operator lifts that one.
23
+ */
24
+ export function pauseOf(db: LedgerDb, artifactId: string): Pause | undefined {
25
+ const row = db.get<{ by: PausedBy; reason: string | null; paused_at: string }>(
26
+ "SELECT by, reason, paused_at FROM artifact_pause WHERE artifact_id = ? ORDER BY by = 'operator' DESC LIMIT 1",
27
+ artifactId,
28
+ );
29
+ return row
30
+ ? { by: row.by, ...(row.reason !== null ? { reason: row.reason } : {}), at: row.paused_at }
31
+ : undefined;
32
+ }
33
+
34
+ /**
35
+ * Pauses or resumes an artifact, in the caller's write transaction, as `by`. The owner's pause and
36
+ * the operator's are kept apart (one row each): pausing again keeps the earlier pause of the same
37
+ * party, and a resume lifts only its own party's, so the artifact takes answers again once no
38
+ * pause is left. The owner resuming while the operator's pause stands is
39
+ * `MOSTRA_REMOTE_PAUSED_BY_OPERATOR`, and changes nothing.
40
+ */
41
+ export function setPause(
42
+ db: LedgerDb,
43
+ artifactId: string,
44
+ change: { readonly paused: boolean; readonly by: PausedBy; readonly reason?: string },
45
+ ): { readonly paused: boolean; readonly by?: PausedBy } {
46
+ const own = db.get<{ by: PausedBy }>(
47
+ "SELECT by FROM artifact_pause WHERE artifact_id = ? AND by = ?",
48
+ artifactId,
49
+ change.by,
50
+ );
51
+ const actor = change.by === "operator" ? { kind: "mostra" as const } : { kind: "agent" as const };
52
+ if (change.paused) {
53
+ if (!own) {
54
+ const before = pauseOf(db, artifactId);
55
+ db.run(
56
+ "INSERT INTO artifact_pause (artifact_id, by, reason, paused_at) VALUES (?, ?, ?, ?)",
57
+ artifactId,
58
+ change.by,
59
+ change.reason ?? null,
60
+ new Date().toISOString(),
61
+ );
62
+ // Open pages learn of a change they can see: a first pause, or the operator's over the owner's.
63
+ if (before === undefined || change.by === "operator") {
64
+ appendEvent(db, {
65
+ artifactId,
66
+ type: "artifact.paused",
67
+ actor,
68
+ payload: {
69
+ by: change.by,
70
+ ...(change.reason !== undefined ? { reason: change.reason } : {}),
71
+ },
72
+ });
73
+ }
74
+ }
75
+ const now = pauseOf(db, artifactId) as Pause;
76
+ return { paused: true, by: now.by };
77
+ }
78
+ // The owner can't lift a pause while the operator's stands: nothing changes, its own included.
79
+ if (change.by === "owner" && pauseOf(db, artifactId)?.by === "operator") {
80
+ throw new RpcError(
81
+ createDiagnostic("MOSTRA_REMOTE_PAUSED_BY_OPERATOR", {
82
+ message: "The server's operator paused this artifact; only the operator resumes it.",
83
+ }),
84
+ );
85
+ }
86
+ if (own) {
87
+ db.run("DELETE FROM artifact_pause WHERE artifact_id = ? AND by = ?", artifactId, change.by);
88
+ }
89
+ const left = pauseOf(db, artifactId);
90
+ if (left === undefined) {
91
+ if (own) {
92
+ appendEvent(db, { artifactId, type: "artifact.resumed", actor, payload: { by: change.by } });
93
+ }
94
+ return { paused: false };
95
+ }
96
+ // The operator lifted its own pause; the owner's stays, and so does the page's notice.
97
+ if (own) {
98
+ appendEvent(db, {
99
+ artifactId,
100
+ type: "artifact.paused",
101
+ actor: { kind: "agent" },
102
+ payload: { by: left.by, ...(left.reason !== undefined ? { reason: left.reason } : {}) },
103
+ });
104
+ }
105
+ return { paused: true, by: left.by };
106
+ }
@@ -3,7 +3,13 @@
3
3
  * Everything comes from the immutable submission row, its snapshot and the version it names, plus
4
4
  * the deliveries' current states; never from the live draft (IN-06).
5
5
  */
6
- import type { ArtifactLink, ArtifactManifest, DeliveryStatus, Results } from "@mostra/protocol";
6
+ import type {
7
+ ArtifactLink,
8
+ ArtifactManifest,
9
+ DeliveryStatus,
10
+ EventOrigin,
11
+ Results,
12
+ } from "@mostra/protocol";
7
13
  import { createDiagnostic, RpcError } from "@mostra/protocol";
8
14
  import type { LedgerDb } from "../ledger/index.ts";
9
15
  import { storedPolicy } from "./policy.ts";
@@ -164,6 +170,7 @@ export function buildResults(db: LedgerDb, row: SubmissionRow): Results {
164
170
  digest: row.digest,
165
171
  delivery: overallDelivery(deliveries),
166
172
  deliveries,
173
+ ...(row.origin_json ? { origin: JSON.parse(row.origin_json) as EventOrigin } : {}),
167
174
  },
168
175
  answers: nestFields(draftFields),
169
176
  fields: [
@@ -144,9 +144,58 @@ export function createOrJoinRun(db: LedgerDb, input: NewRun): { row: RunRow; joi
144
144
  };
145
145
  }
146
146
 
147
- /** Moves a queued run to `running`, when its worker has it. */
147
+ /** Moves a queued (or, on a remote server, waiting) run to `running`, when its worker has it. */
148
148
  export function markRunning(db: LedgerDb, runId: string): void {
149
- db.run("UPDATE action_run SET status = 'running' WHERE id = ? AND status = 'queued'", runId);
149
+ db.run(
150
+ "UPDATE action_run SET status = 'running' WHERE id = ? AND status IN ('queued', 'waiting')",
151
+ runId,
152
+ );
153
+ }
154
+
155
+ /**
156
+ * A relayed run waits for its owner's instance (D165 item 4): `waiting`, with `action.waiting`.
157
+ * Returns whether it moved (a queued run only).
158
+ */
159
+ export function markWaiting(db: LedgerDb, runId: string): boolean {
160
+ const row = db.get<RunRow>("SELECT * FROM action_run WHERE id = ?", runId);
161
+ if (row?.status !== "queued") return false;
162
+ db.run("UPDATE action_run SET status = 'waiting' WHERE id = ?", runId);
163
+ appendEvent(db, {
164
+ artifactId: row.artifact_id,
165
+ type: "action.waiting",
166
+ actor: { kind: "mostra" },
167
+ causation: { runId },
168
+ payload: { runId, action: row.action },
169
+ });
170
+ return true;
171
+ }
172
+
173
+ /**
174
+ * A relayed run whose owner never reported its end within the offline limit, or no longer knows
175
+ * it (D165 item 4): `unknown`, with `action.unknown`. It may have taken effect, so it is never
176
+ * `failed`; the owner's later report replaces it once (a trigger). Returns whether it moved.
177
+ */
178
+ export function markUnknown(db: LedgerDb, runId: string): boolean {
179
+ const row = db.get<RunRow>("SELECT * FROM action_run WHERE id = ?", runId);
180
+ if (!row || (row.status !== "queued" && row.status !== "running" && row.status !== "waiting"))
181
+ return false;
182
+ db.run(
183
+ "UPDATE action_run SET status = 'unknown', reason = 'owner-unreachable', error_json = ? WHERE id = ?",
184
+ JSON.stringify({
185
+ code: "MOSTRA_REMOTE_OWNER_OFFLINE",
186
+ message:
187
+ "Its owner's machine never said how it ended, so it may have taken effect; it shows here if the owner reports.",
188
+ }),
189
+ runId,
190
+ );
191
+ appendEvent(db, {
192
+ artifactId: row.artifact_id,
193
+ type: "action.unknown",
194
+ actor: { kind: "mostra" },
195
+ causation: { runId },
196
+ payload: { runId, action: row.action },
197
+ });
198
+ return true;
150
199
  }
151
200
 
152
201
  /** What a running run produced since the last flush. */
@@ -238,6 +287,7 @@ export function finishRun(db: LedgerDb, runId: string, end: RunEnd): RunRow | un
238
287
  const row = db.get<RunRow>("SELECT * FROM action_run WHERE id = ?", runId);
239
288
  if (!row || isFinalRunStatus(row.status)) return row;
240
289
  const finishedAt = new Date().toISOString();
290
+ // An `unknown` run's reported end replaces its reason and error too (D165 item 4).
241
291
  db.run(
242
292
  "UPDATE action_run SET status = ?, reason = ?, error_json = ?, finished_at = ? WHERE id = ?",
243
293
  end.status,