@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
@@ -0,0 +1,289 @@
1
+ /**
2
+ * The owner's side of the network binding (INTERFACES §18.2, D165 item 2): one WebSocket to a
3
+ * remote server's `/host`, dialled out (so it works behind NAT), opened with the access token and
4
+ * the subprotocol `mostra.host`, carrying the host protocol as one JSON-RPC message per text frame.
5
+ * It keeps the heartbeat (a ping and `mostra/ping` every 20 s; dead after 45 s of silence) and
6
+ * dials again with backoff (1 s doubling to 60 s, with jitter) whenever the connection ends, so an
7
+ * edge or server restart is an ordinary reconnect. Each connection opens its session afresh:
8
+ * `mostra/initialize` with the owner link, then the ports registered again.
9
+ */
10
+ import {
11
+ HOST_PROTOCOL_REVISIONS,
12
+ type JsonRpcParams,
13
+ type MethodDefinition,
14
+ type MostraInitializeParams,
15
+ type MostraInitializeResult,
16
+ mostraInitializeMethod,
17
+ RpcPeer,
18
+ } from "@mostra/protocol";
19
+ import { WebSocket } from "ws";
20
+ import type { Log } from "../log.ts";
21
+ import {
22
+ BINDING_SUBPROTOCOL,
23
+ type HeartbeatOptions,
24
+ keepAlive,
25
+ MAX_FRAME_BYTES,
26
+ readFrame,
27
+ } from "../protocol-server/index.ts";
28
+
29
+ /** The first redial's delay; each later one doubles, up to {@link MAX_REDIAL_MS}. */
30
+ export const FIRST_REDIAL_MS = 1_000;
31
+ /** The longest delay between dials (§18.2). */
32
+ export const MAX_REDIAL_MS = 60_000;
33
+
34
+ /** The delay before dial number `attempt` (1 for the first redial), with up to 25% jitter. */
35
+ export function redialMs(attempt: number, random: () => number = Math.random): number {
36
+ const base = Math.min(MAX_REDIAL_MS, FIRST_REDIAL_MS * 2 ** Math.max(0, attempt - 1));
37
+ return Math.round(base * (0.75 + random() * 0.25));
38
+ }
39
+
40
+ /** What a binding needs for each connection it opens. */
41
+ export interface BindingOptions {
42
+ /** The server's API origin; the binding dials `<origin>/host` (`ws:` for `http:`, `wss:` for `https:`). */
43
+ readonly server: string;
44
+ /** A fresh access token for each dial (it may refresh it first). */
45
+ readonly accessToken: () => Promise<string>;
46
+ /** The `mostra/initialize` params for each session (the owner link with its marks, the ports). */
47
+ readonly initialize: () => MostraInitializeParams;
48
+ /** Requests the hosted home sends this instance: its port calls. */
49
+ readonly onRequest: (method: string, params: unknown) => unknown;
50
+ /**
51
+ * Notifications the hosted home sends, with the session they came on: one can arrive while the
52
+ * session's own start (`onOpen`) is still under way, before {@link Binding.session} names it
53
+ * (Opus review of WP-29, 2).
54
+ */
55
+ readonly onNotification?: (method: string, params: unknown, session: BindingSession) => void;
56
+ /** Runs once a session is open, before anything else is sent on it. */
57
+ readonly onOpen?: (session: BindingSession) => void | Promise<void>;
58
+ /** Runs when a connection ends, with why. */
59
+ readonly onClose?: (reason: string) => void;
60
+ /** Told when a dial is refused for its token (401): the caller signs in again or refreshes. */
61
+ readonly onUnauthorized?: () => void;
62
+ readonly log: Log;
63
+ readonly heartbeat?: HeartbeatOptions;
64
+ /** The `Host` to send, for tests that dial a loopback address under a name. */
65
+ readonly connectTo?: { readonly host: string; readonly port?: number | undefined };
66
+ /**
67
+ * Sees every message the binding sends and receives, as its text (tests: the relay's secrets
68
+ * grep, `MOSTRA_TEST_BINDING_TRACE`).
69
+ */
70
+ readonly trace?: (direction: "out" | "in", text: string) => void;
71
+ }
72
+
73
+ /** One open session on a binding. */
74
+ export interface BindingSession {
75
+ readonly result: MostraInitializeResult;
76
+ call<P, R>(
77
+ definition: MethodDefinition<P, R>,
78
+ params: P,
79
+ options?: { timeoutMs?: number },
80
+ ): Promise<R>;
81
+ notify(method: string, params?: JsonRpcParams): void;
82
+ }
83
+
84
+ /** A binding that keeps itself connected until it is stopped. */
85
+ export interface Binding {
86
+ /** The session open now, if any. */
87
+ readonly session: BindingSession | undefined;
88
+ /** Settles with the next open session (now, if one is open). */
89
+ ready(): Promise<BindingSession>;
90
+ /** Drops the current connection; the binding dials again (tests, a token change). */
91
+ reconnect(): void;
92
+ stop(): Promise<void>;
93
+ }
94
+
95
+ /** Starts a binding: it dials at once and keeps dialling until stopped. */
96
+ export function startBinding(options: BindingOptions): Binding {
97
+ const { log } = options;
98
+ let stopped = false;
99
+ let current: { socket: WebSocket; peer: RpcPeer } | undefined;
100
+ let session: BindingSession | undefined;
101
+ let attempt = 0;
102
+ let timer: ReturnType<typeof setTimeout> | undefined;
103
+ let waiters: ((session: BindingSession) => void)[] = [];
104
+
105
+ const url = (): string => {
106
+ const origin = new URL(options.server);
107
+ const scheme = origin.protocol === "https:" ? "wss:" : "ws:";
108
+ if (options.connectTo) {
109
+ const port =
110
+ options.connectTo.port ?? (Number(origin.port) || (scheme === "wss:" ? 443 : 80));
111
+ return `${scheme}//${options.connectTo.host}:${port}/host`;
112
+ }
113
+ return `${scheme}//${origin.host}/host`;
114
+ };
115
+
116
+ const schedule = (): void => {
117
+ if (stopped || timer) return;
118
+ attempt += 1;
119
+ const delay = redialMs(attempt);
120
+ timer = setTimeout(() => {
121
+ timer = undefined;
122
+ void dial();
123
+ }, delay);
124
+ timer.unref?.();
125
+ };
126
+
127
+ const dial = async (): Promise<void> => {
128
+ if (stopped) return;
129
+ let token: string;
130
+ try {
131
+ token = await options.accessToken();
132
+ } catch (error) {
133
+ log.warn("link.token_failed", { server: options.server, error: (error as Error).message });
134
+ options.onClose?.((error as Error).message);
135
+ schedule();
136
+ return;
137
+ }
138
+ if (stopped) return;
139
+ const host = new URL(options.server).host;
140
+ const socket = new WebSocket(url(), BINDING_SUBPROTOCOL, {
141
+ headers: { authorization: `Bearer ${token}`, ...(options.connectTo ? { host } : {}) },
142
+ maxPayload: MAX_FRAME_BYTES,
143
+ perMessageDeflate: false,
144
+ handshakeTimeout: 20_000,
145
+ });
146
+ let opened = false;
147
+ /** This connection's session, from its `mostra/initialize` answer on. */
148
+ let connectionSession: BindingSession | undefined;
149
+ const peer = new RpcPeer({
150
+ send: (message) => {
151
+ if (socket.readyState !== socket.OPEN) return;
152
+ const text = JSON.stringify(message);
153
+ options.trace?.("out", text);
154
+ socket.send(text);
155
+ },
156
+ onRequest: (method, params) => options.onRequest(method, params),
157
+ onNotification: (method, params) => {
158
+ if (method === "mostra/ping") return;
159
+ if (!connectionSession) {
160
+ log.warn("link.notification_before_session", { server: options.server, method });
161
+ return;
162
+ }
163
+ options.onNotification?.(method, params, connectionSession);
164
+ },
165
+ onInternalError: (error, method) => log.error("link.internal_error", { method, error }),
166
+ });
167
+ current = { socket, peer };
168
+ socket.on("unexpected-response", (_request, response) => {
169
+ const status = response.statusCode ?? 0;
170
+ log.warn("link.refused", { server: options.server, status });
171
+ if (status === 401) options.onUnauthorized?.();
172
+ socket.terminate();
173
+ });
174
+ socket.on("message", (data, isBinary) => {
175
+ const text = readFrame(socket, data as Buffer, isBinary, log);
176
+ if (text === undefined) return;
177
+ options.trace?.("in", text);
178
+ peer.receiveLine(text);
179
+ });
180
+ socket.on("error", (error) =>
181
+ log.warn("link.socket_error", { server: options.server, error: error.message }),
182
+ );
183
+ socket.once("close", (code, reason) => {
184
+ peer.close();
185
+ if (current?.socket === socket) current = undefined;
186
+ session = undefined;
187
+ const why = `closed (${code}${reason.length > 0 ? `: ${reason.toString()}` : ""})`;
188
+ if (opened) log.info("link.disconnected", { server: options.server, code });
189
+ options.onClose?.(why);
190
+ if (code === 4401 || code === 4403) options.onUnauthorized?.();
191
+ schedule();
192
+ });
193
+ socket.once("open", () => {
194
+ opened = true;
195
+ keepAlive(socket, options.heartbeat);
196
+ void (async () => {
197
+ try {
198
+ const result = await peer.call(mostraInitializeMethod, options.initialize(), {
199
+ timeoutMs: 30_000,
200
+ });
201
+ const open: BindingSession = {
202
+ result,
203
+ call: (definition, params, callOptions) => peer.call(definition, params, callOptions),
204
+ notify: (method, params) => peer.notify(method, params),
205
+ };
206
+ connectionSession = open;
207
+ attempt = 0;
208
+ await options.onOpen?.(open);
209
+ if (socket.readyState !== socket.OPEN) return;
210
+ session = open;
211
+ log.info("link.connected", { server: options.server });
212
+ const ready = waiters;
213
+ waiters = [];
214
+ for (const resolve of ready) resolve(open);
215
+ } catch (error) {
216
+ log.warn("link.initialize_failed", {
217
+ server: options.server,
218
+ error: (error as Error).message,
219
+ });
220
+ options.onClose?.((error as Error).message);
221
+ socket.close(1008, "initialize failed");
222
+ }
223
+ })();
224
+ });
225
+ };
226
+
227
+ void dial();
228
+
229
+ return {
230
+ get session() {
231
+ return session;
232
+ },
233
+ ready() {
234
+ if (session) return Promise.resolve(session);
235
+ return new Promise((resolve) => waiters.push(resolve));
236
+ },
237
+ reconnect() {
238
+ current?.socket.terminate();
239
+ },
240
+ async stop() {
241
+ stopped = true;
242
+ if (timer) clearTimeout(timer);
243
+ const open = current;
244
+ current = undefined;
245
+ if (open) {
246
+ await new Promise<void>((resolve) => {
247
+ if (open.socket.readyState === open.socket.CLOSED) {
248
+ resolve();
249
+ return;
250
+ }
251
+ open.socket.once("close", () => resolve());
252
+ open.socket.close(1000);
253
+ setTimeout(() => {
254
+ open.socket.terminate();
255
+ resolve();
256
+ }, 2_000).unref();
257
+ });
258
+ }
259
+ },
260
+ };
261
+ }
262
+
263
+ /** The `mostra/initialize` params an owner instance sends on its binding (§18.2). */
264
+ export function ownerInitialize(input: {
265
+ readonly name: string;
266
+ readonly version: string;
267
+ readonly instanceId: string;
268
+ readonly relayMark: number;
269
+ readonly followed?: { readonly incarnation: string; readonly cursor: string };
270
+ }): MostraInitializeParams {
271
+ return {
272
+ client: { name: input.name, version: input.version },
273
+ revisions: [...HOST_PROTOCOL_REVISIONS],
274
+ capabilities: { notifications: ["events/notify"] },
275
+ ports: ["submissions", "relay"],
276
+ link: {
277
+ instanceId: input.instanceId,
278
+ relayMark: input.relayMark,
279
+ ...(input.followed
280
+ ? {
281
+ followed: {
282
+ incarnation: input.followed.incarnation,
283
+ cursor: input.followed.cursor as never,
284
+ },
285
+ }
286
+ : {}),
287
+ },
288
+ };
289
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * A hosted artifact's shared state and live values live on its hosted home (D165 item 1;
3
+ * INTERFACES §18.2): the owner's own `state/get`, `state/patch` and `live/push`, and its server
4
+ * code's `state`, act there through the link, refused with `MOSTRA_REMOTE_UNREACHABLE` when it
5
+ * can't be reached, and are never applied here alone.
6
+ */
7
+ import {
8
+ type ArtifactPauseResult,
9
+ type ArtifactRef,
10
+ artifactPauseMethod,
11
+ livePushMethod,
12
+ MutationId,
13
+ stateGetMethod,
14
+ statePatchMethod,
15
+ } from "@mostra/protocol";
16
+ import type { HostedState } from "../actions/index.ts";
17
+ import { handler, type MethodHandler } from "../protocol-server/index.ts";
18
+ import type { Link } from "./manager.ts";
19
+
20
+ const FORWARDED = new Set([stateGetMethod.method, statePatchMethod.method, livePushMethod.method]);
21
+
22
+ /** Server code's `state` for an artifact hosted somewhere, else `undefined`. */
23
+ export function hostedState(link: Link, artifactId: string): HostedState | undefined {
24
+ const server = link.hostedOn(artifactId);
25
+ if (server === undefined) return undefined;
26
+ const artifact = { artifactId };
27
+ return {
28
+ get: (ids) =>
29
+ link.forward(server, stateGetMethod, {
30
+ artifact,
31
+ ...(ids !== undefined ? { ids: [...ids] } : {}),
32
+ }),
33
+ patch: (input) =>
34
+ link.forward(server, statePatchMethod, {
35
+ artifact,
36
+ ops: input.ops as never,
37
+ mutationId: input.mutationId ?? MutationId.create(),
38
+ ...(input.baseRevision !== undefined ? { baseRevision: input.baseRevision } : {}),
39
+ as: "agent",
40
+ ...(input.runId !== undefined ? { causation: { runId: input.runId } } : {}),
41
+ }),
42
+ };
43
+ }
44
+
45
+ /**
46
+ * Wraps the instance's `state/get`, `state/patch` and `live/push` so that, for a hosted artifact,
47
+ * they act on its hosted home: the same params, the artifact named by its ID there.
48
+ */
49
+ export function forwardHosted(
50
+ link: Link,
51
+ resolve: (ref: ArtifactRef) => string,
52
+ handlers: readonly MethodHandler[],
53
+ ): MethodHandler[] {
54
+ return handlers.map((entry) => {
55
+ if (!FORWARDED.has(entry.definition.method)) return entry;
56
+ return {
57
+ definition: entry.definition,
58
+ handle: (params, session) => {
59
+ const input = params as { readonly artifact: ArtifactRef };
60
+ const artifactId = resolve(input.artifact);
61
+ const server = link.hostedOn(artifactId);
62
+ if (server === undefined) return entry.handle(params, session);
63
+ return link.forward(server, entry.definition, { ...input, artifact: { artifactId } });
64
+ },
65
+ };
66
+ });
67
+ }
68
+
69
+ /**
70
+ * The owner's `artifact/pause` (D165 item 14): pauses or resumes the artifact here and on every
71
+ * server it is hosted on, each as its owner. A server where the operator paused it answers that
72
+ * only the operator resumes it (`MOSTRA_REMOTE_PAUSED_BY_OPERATOR`); one that can't be reached is
73
+ * `MOSTRA_REMOTE_UNREACHABLE`, and the call can be made again, since a pause is idempotent.
74
+ */
75
+ export function pauseHandler(
76
+ link: Link,
77
+ resolve: (ref: ArtifactRef) => string,
78
+ pause: (
79
+ artifactId: string,
80
+ change: { readonly paused: boolean; readonly by: "owner"; readonly reason?: string },
81
+ ) => ArtifactPauseResult,
82
+ ): MethodHandler {
83
+ return handler(artifactPauseMethod, async (params) => {
84
+ const artifactId = resolve(params.artifact);
85
+ let byOperator = false;
86
+ for (const server of link.hostedServers(artifactId)) {
87
+ const answer = await link.forward(server, artifactPauseMethod, {
88
+ artifact: { artifactId },
89
+ paused: params.paused,
90
+ ...(params.reason !== undefined ? { reason: params.reason } : {}),
91
+ });
92
+ if (answer.by === "operator") byOperator = true;
93
+ }
94
+ const local = pause(artifactId, {
95
+ paused: params.paused,
96
+ by: "owner",
97
+ ...(params.reason !== undefined ? { reason: params.reason } : {}),
98
+ });
99
+ return byOperator ? { paused: true, by: "operator" } : local;
100
+ });
101
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Requests from an owner instance to a remote server's API origin over HTTPS (or HTTP for a
3
+ * loopback test server): the device grant, token refreshes and revocations, and version files
4
+ * (INTERFACES §18.2, §18.5). One place, so every request bounds its time and its answer's size and
5
+ * never logs what it sends. `MOSTRA_TEST_SERVER_ADDRESS` (`host:port`) points every request at a
6
+ * test server listening on loopback under the server's own `Host`.
7
+ */
8
+ import { request as httpRequest, type IncomingHttpHeaders } from "node:http";
9
+ import { request as httpsRequest } from "node:https";
10
+ import { createDiagnostic, RpcError } from "@mostra/protocol";
11
+
12
+ /** How long a request to the server may take. */
13
+ export const REMOTE_REQUEST_TIMEOUT_MS = 30_000;
14
+ /** The largest answer the instance reads (the API answers JSON; files go the other way). */
15
+ const MAX_ANSWER_BYTES = 4 * 1024 * 1024;
16
+
17
+ /** Where requests for `server` actually go: the test override, or the server's own host. */
18
+ export interface RemoteAddress {
19
+ readonly host: string;
20
+ /** Absent: the server URL's own port. */
21
+ readonly port?: number | undefined;
22
+ }
23
+
24
+ /** Reads the test override, `host:port`, or nothing. */
25
+ export function testServerAddress(value: string | undefined): RemoteAddress | undefined {
26
+ if (!value) return undefined;
27
+ // A bare address keeps every server URL's own port (tests with several servers).
28
+ if (/^[\d.]+$/.test(value)) return { host: value };
29
+ const at = value.lastIndexOf(":");
30
+ const port = Number(value.slice(at + 1));
31
+ if (at <= 0 || !Number.isInteger(port) || port <= 0 || port > 65535) return undefined;
32
+ return { host: value.slice(0, at).replace(/^\[|\]$/g, ""), port };
33
+ }
34
+
35
+ /** An answer: its status, headers and body. */
36
+ export interface RemoteAnswer {
37
+ readonly status: number;
38
+ readonly headers: IncomingHttpHeaders;
39
+ readonly body: Buffer;
40
+ }
41
+
42
+ /** The diagnostic for a server that can't be reached, naming it (never what was sent). */
43
+ export function unreachable(server: string, why: string): RpcError {
44
+ return new RpcError(
45
+ createDiagnostic("MOSTRA_REMOTE_UNREACHABLE", {
46
+ message: `${server} can't be reached: ${why}.`,
47
+ }),
48
+ );
49
+ }
50
+
51
+ /** Sends one request to `server` (an API origin) and reads its answer. */
52
+ export function remoteRequest(
53
+ server: string,
54
+ path: string,
55
+ init: {
56
+ readonly method?: string;
57
+ readonly headers?: Record<string, string>;
58
+ readonly body?: string | Buffer;
59
+ readonly connectTo?: RemoteAddress;
60
+ readonly timeoutMs?: number;
61
+ } = {},
62
+ ): Promise<RemoteAnswer> {
63
+ const url = new URL(path, server);
64
+ const secure = url.protocol === "https:";
65
+ const send = secure ? httpsRequest : httpRequest;
66
+ const port = Number(url.port) || (secure ? 443 : 80);
67
+ return new Promise((resolve, reject) => {
68
+ const outgoing = send(
69
+ {
70
+ host: init.connectTo?.host ?? url.hostname,
71
+ port: init.connectTo?.port ?? port,
72
+ path: `${url.pathname}${url.search}`,
73
+ method: init.method ?? "GET",
74
+ headers: {
75
+ host: url.host,
76
+ ...(init.body !== undefined
77
+ ? { "content-length": String(Buffer.byteLength(init.body)) }
78
+ : {}),
79
+ ...init.headers,
80
+ },
81
+ ...(secure ? { servername: url.hostname } : {}),
82
+ timeout: init.timeoutMs ?? REMOTE_REQUEST_TIMEOUT_MS,
83
+ },
84
+ (incoming) => {
85
+ const chunks: Buffer[] = [];
86
+ let size = 0;
87
+ incoming.on("data", (chunk: Buffer) => {
88
+ size += chunk.length;
89
+ if (size > MAX_ANSWER_BYTES) {
90
+ incoming.destroy();
91
+ reject(unreachable(server, "its answer was too large"));
92
+ return;
93
+ }
94
+ chunks.push(chunk);
95
+ });
96
+ incoming.on("end", () =>
97
+ resolve({
98
+ status: incoming.statusCode ?? 0,
99
+ headers: incoming.headers,
100
+ body: Buffer.concat(chunks),
101
+ }),
102
+ );
103
+ incoming.on("error", (error) => reject(unreachable(server, error.message)));
104
+ },
105
+ );
106
+ outgoing.on("timeout", () => outgoing.destroy(new Error("no answer in time")));
107
+ outgoing.on("error", (error) => reject(unreachable(server, error.message)));
108
+ if (init.body !== undefined) outgoing.write(init.body);
109
+ outgoing.end();
110
+ });
111
+ }
112
+
113
+ /** Posts a form and reads a JSON answer, whatever its status. */
114
+ export async function postForm(
115
+ server: string,
116
+ path: string,
117
+ fields: Record<string, string>,
118
+ connectTo?: RemoteAddress,
119
+ bearer?: string,
120
+ ): Promise<{ status: number; json: Record<string, unknown> }> {
121
+ const answer = await remoteRequest(server, path, {
122
+ method: "POST",
123
+ headers: {
124
+ "content-type": "application/x-www-form-urlencoded",
125
+ accept: "application/json",
126
+ ...(bearer !== undefined ? { authorization: `Bearer ${bearer}` } : {}),
127
+ },
128
+ body: new URLSearchParams(fields).toString(),
129
+ ...(connectTo ? { connectTo } : {}),
130
+ });
131
+ let json: Record<string, unknown> = {};
132
+ try {
133
+ const parsed = JSON.parse(answer.body.toString("utf8")) as unknown;
134
+ if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) {
135
+ json = parsed as Record<string, unknown>;
136
+ }
137
+ } catch {
138
+ // Not JSON: the caller reads the status alone.
139
+ }
140
+ return { status: answer.status, json };
141
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The owner's side of a remote server (ARCHITECTURE §15.3, INTERFACES §18.2, D165): the network
3
+ * binding, sign-in, the sync cursor and the relay. One public entry, as every runtime module
4
+ * (REPO_STANDARDS §3).
5
+ */
6
+
7
+ export { createOwnerAuthor } from "./author.ts";
8
+ export { type Binding, ownerInitialize, startBinding } from "./binding.ts";
9
+ export { forwardHosted, hostedState, pauseHandler } from "./hosted.ts";
10
+ export { createLink, type Link } from "./manager.ts";