@zyro-sec/server 1.0.0 → 1.0.2

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 (130) hide show
  1. package/package.json +15 -18
  2. package/script/build.ts +28 -0
  3. package/script/pack.ts +5 -0
  4. package/script/workerd-probe.ts +59 -0
  5. package/src/api.ts +12 -0
  6. package/src/auth.ts +70 -0
  7. package/src/content-headers.ts +32 -0
  8. package/src/cors.ts +34 -0
  9. package/src/event-feed.ts +93 -0
  10. package/src/fetch.ts +67 -0
  11. package/src/handlers/agent.ts +23 -0
  12. package/src/handlers/artifact.ts +242 -0
  13. package/src/handlers/automation.ts +63 -0
  14. package/src/handlers/blob.ts +55 -0
  15. package/src/handlers/command.ts +8 -0
  16. package/src/handlers/config.ts +26 -0
  17. package/src/handlers/credential.ts +87 -0
  18. package/src/handlers/debug.ts +57 -0
  19. package/src/handlers/event.ts +38 -0
  20. package/src/handlers/form.ts +15 -0
  21. package/src/handlers/fs.ts +180 -0
  22. package/src/handlers/generate.ts +117 -0
  23. package/src/handlers/integration.ts +310 -0
  24. package/src/handlers/job.ts +29 -0
  25. package/src/handlers/location.ts +33 -0
  26. package/src/handlers/machine.ts +336 -0
  27. package/src/handlers/mcp.ts +160 -0
  28. package/src/handlers/media.ts +44 -0
  29. package/src/handlers/message.ts +82 -0
  30. package/src/handlers/migration.ts +13 -0
  31. package/src/handlers/model.ts +25 -0
  32. package/src/handlers/permission.ts +131 -0
  33. package/src/handlers/persistent-pty.ts +285 -0
  34. package/src/handlers/plugin.ts +97 -0
  35. package/src/handlers/project.ts +23 -0
  36. package/src/handlers/provider.ts +32 -0
  37. package/src/handlers/pty-socket.ts +47 -0
  38. package/src/handlers/pty.ts +181 -0
  39. package/src/handlers/push.ts +67 -0
  40. package/src/handlers/reference.ts +8 -0
  41. package/src/handlers/rpc.ts +31 -0
  42. package/src/handlers/server.ts +58 -0
  43. package/src/handlers/session-error.ts +47 -0
  44. package/src/handlers/session.ts +722 -0
  45. package/src/handlers/shell.ts +72 -0
  46. package/src/handlers/skill.ts +34 -0
  47. package/src/handlers/usage.ts +23 -0
  48. package/src/handlers/vcs.ts +104 -0
  49. package/src/handlers/voice.ts +50 -0
  50. package/src/handlers/websearch.ts +50 -0
  51. package/src/handlers/worktree.ts +60 -0
  52. package/src/handlers.ts +69 -0
  53. package/src/location.ts +86 -0
  54. package/src/media-uri.ts +13 -0
  55. package/src/middleware/authorization.ts +111 -0
  56. package/src/middleware/defect-response.ts +39 -0
  57. package/src/middleware/form-location.ts +44 -0
  58. package/src/middleware/schema-error.ts +49 -0
  59. package/src/middleware/session-location.ts +30 -0
  60. package/src/options.ts +61 -0
  61. package/src/pairing.ts +36 -0
  62. package/src/process.ts +274 -0
  63. package/src/range.ts +16 -0
  64. package/src/request-site.ts +36 -0
  65. package/src/request-tracing.ts +9 -0
  66. package/src/routes.ts +261 -0
  67. package/src/server-info.ts +41 -0
  68. package/src/service-status.ts +30 -0
  69. package/src/workerd.ts +184 -0
  70. package/test/artifact.test.ts +224 -0
  71. package/test/auth.test.ts +24 -0
  72. package/test/automation.test.ts +315 -0
  73. package/test/client-mcp.test.ts +369 -0
  74. package/test/codemode-api.test.ts +88 -0
  75. package/test/config.test.ts +104 -0
  76. package/test/cors.test.ts +24 -0
  77. package/test/defect-response.test.ts +83 -0
  78. package/test/event-feed.test.ts +201 -0
  79. package/test/fetch-option.test.ts +104 -0
  80. package/test/fetch.test.ts +698 -0
  81. package/test/fixture/mcp-starts.cjs +20 -0
  82. package/test/fixture/routes.ts +37 -0
  83. package/test/fixture/server.ts +20 -0
  84. package/test/fixture/worktree-delegate/index.ts +13 -0
  85. package/test/fixture/worktree-plugin/index.ts +47 -0
  86. package/test/flag-channel.test.ts +126 -0
  87. package/test/fs.test.ts +439 -0
  88. package/test/generate.test.ts +132 -0
  89. package/test/handler-policy.test.ts +43 -0
  90. package/test/heap-dump.test.ts +137 -0
  91. package/test/integration-policy.test.ts +93 -0
  92. package/test/job.test.ts +186 -0
  93. package/test/machine-iroh.test.ts +259 -0
  94. package/test/machine-link.test.ts +1234 -0
  95. package/test/machine-routes.test.ts +198 -0
  96. package/test/media.test.ts +343 -0
  97. package/test/message.test.ts +105 -0
  98. package/test/model.test.ts +62 -0
  99. package/test/options.test.ts +42 -0
  100. package/test/permission-saved.test.ts +73 -0
  101. package/test/persistent-pty.test.ts +1007 -0
  102. package/test/process.test.ts +261 -0
  103. package/test/provider.test.ts +88 -0
  104. package/test/pty-socket.property.test.ts +240 -0
  105. package/test/pty-socket.test.ts +89 -0
  106. package/test/pty-transport.test.ts +200 -0
  107. package/test/push-routes.test.ts +157 -0
  108. package/test/request-site.test.ts +71 -0
  109. package/test/request-tracing.test.ts +87 -0
  110. package/test/rpc-handler-errors.test.ts +76 -0
  111. package/test/schema-error.test.ts +68 -0
  112. package/test/service-status.test.ts +34 -0
  113. package/test/session-admit-location.test.ts +92 -0
  114. package/test/session-create.test.ts +65 -0
  115. package/test/session-diff.test.ts +100 -0
  116. package/test/session-form.test.ts +50 -0
  117. package/test/session-import.test.ts +152 -0
  118. package/test/session-instances.test.ts +335 -0
  119. package/test/session-unarchive.test.ts +73 -0
  120. package/test/session-update.test.ts +66 -0
  121. package/test/skill.test.ts +57 -0
  122. package/test/sse-failure.test.ts +71 -0
  123. package/test/usage.test.ts +88 -0
  124. package/test/vcs.test.ts +356 -0
  125. package/test/video-generate.test.ts +131 -0
  126. package/test/voice.test.ts +170 -0
  127. package/test/workerd.test.ts +227 -0
  128. package/test/worktree.test.ts +333 -0
  129. package/tsconfig.build.json +15 -0
  130. package/tsconfig.json +11 -0
@@ -0,0 +1,181 @@
1
+ import { Pty } from "@zyro-sec/core/pty"
2
+ import { PtyProtocol } from "@zyro-sec/core/pty/protocol"
3
+ import { PtyTicket } from "@zyro-sec/core/pty/ticket"
4
+ import { Location } from "@zyro-sec/core/location"
5
+ import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
6
+ import { Effect, Queue } from "effect"
7
+ import { HttpServerRequest, HttpServerResponse } from "effect/http"
8
+ import { HttpApiBuilder, HttpApiSchema } from "effect/http-api"
9
+ import { Socket } from "effect/socket"
10
+ import { Api } from "../api"
11
+ import { CorsConfig, isAllowedRequestOrigin } from "../cors"
12
+ import { ForbiddenError, PtyNotFoundError } from "@zyro-sec/protocol/errors"
13
+ import {
14
+ PTY_CONNECT_TICKET_QUERY,
15
+ PTY_CONNECT_TOKEN_HEADER,
16
+ PTY_CONNECT_TOKEN_HEADER_VALUE,
17
+ } from "@zyro-sec/protocol/groups/pty"
18
+ import { locationErrors, requestRef, response } from "../location"
19
+ import { holdLocation, type Outbound, runPtySocket } from "./pty-socket"
20
+
21
+ const notFound = (ptyID: Pty.ID) => new PtyNotFoundError({ ptyID, message: `PTY session not found: ${ptyID}` })
22
+
23
+ export const PtyHandler = HttpApiBuilder.group(Api, "server.pty", (handlers) =>
24
+ Effect.gen(function* () {
25
+ const locations = yield* LocationServiceMap.Service
26
+ const tickets = yield* PtyTicket.Service
27
+ const cors = yield* CorsConfig
28
+
29
+ return handlers
30
+ .handle(
31
+ "pty.list",
32
+ Effect.fn(function* () {
33
+ const pty = yield* Pty.Service
34
+ return yield* response(pty.list())
35
+ }),
36
+ )
37
+ .handle(
38
+ "pty.create",
39
+ Effect.fn(function* (ctx) {
40
+ const pty = yield* Pty.Service
41
+ const location = yield* Location.Service
42
+ return yield* response(
43
+ pty.create({
44
+ ...ctx.payload,
45
+ cwd: ctx.payload.cwd || location.directory,
46
+ }),
47
+ )
48
+ }),
49
+ )
50
+ .handle(
51
+ "pty.get",
52
+ Effect.fn(function* (ctx) {
53
+ const pty = yield* Pty.Service
54
+ return yield* response(
55
+ pty.get(ctx.params.ptyID).pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID))),
56
+ )
57
+ }),
58
+ )
59
+ .handle(
60
+ "pty.update",
61
+ Effect.fn(function* (ctx) {
62
+ const pty = yield* Pty.Service
63
+ return yield* response(
64
+ pty
65
+ .update(ctx.params.ptyID, ctx.payload)
66
+ .pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID))),
67
+ )
68
+ }),
69
+ )
70
+ .handle(
71
+ "pty.remove",
72
+ Effect.fn(function* (ctx) {
73
+ const pty = yield* Pty.Service
74
+ yield* pty
75
+ .remove(ctx.params.ptyID)
76
+ .pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID)))
77
+ return HttpApiSchema.NoContent.make()
78
+ }),
79
+ )
80
+ .handle(
81
+ "pty.connectToken",
82
+ Effect.fn(function* (ctx) {
83
+ const request = yield* HttpServerRequest.HttpServerRequest
84
+ // The custom header forces a CORS preflight, so cross-origin browser pages cannot
85
+ // mint tickets without passing the server's origin policy.
86
+ if (
87
+ request.headers[PTY_CONNECT_TOKEN_HEADER] !== PTY_CONNECT_TOKEN_HEADER_VALUE ||
88
+ !isAllowedRequestOrigin(request.headers.origin, request.headers.host, cors)
89
+ )
90
+ return yield* new ForbiddenError({ message: "Invalid PTY connect token request" })
91
+ const pty = yield* Pty.Service
92
+ const location = yield* Location.Service
93
+ yield* pty.get(ctx.params.ptyID).pipe(Effect.catchTag("Pty.NotFoundError", () => notFound(ctx.params.ptyID)))
94
+ return yield* response(
95
+ tickets.issue({
96
+ ptyID: ctx.params.ptyID,
97
+ directory: location.directory,
98
+ workspaceID: location.workspaceID,
99
+ }),
100
+ )
101
+ }),
102
+ )
103
+ .handleRaw(
104
+ "pty.connect",
105
+ Effect.fn("PtyHandler.connect")(function* (ctx) {
106
+ if (!isAllowedRequestOrigin(ctx.request.headers.origin, ctx.request.headers.host, cors))
107
+ return HttpServerResponse.empty({ status: 403 })
108
+
109
+ const ref = LocationServiceMap.canonical(requestRef(ctx.request))
110
+ const url = new URL(ctx.request.url, "http://localhost")
111
+ const ticket = url.searchParams.get(PTY_CONNECT_TICKET_QUERY)
112
+ if (
113
+ ticket &&
114
+ !(yield* tickets.consume({
115
+ ticket,
116
+ ptyID: ctx.params.ptyID,
117
+ directory: ref.directory,
118
+ workspaceID: ref.workspaceID,
119
+ }))
120
+ )
121
+ return HttpServerResponse.empty({ status: 403 })
122
+
123
+ return yield* Effect.gen(function* () {
124
+ const pty = yield* Pty.Service
125
+ const exists = yield* pty.get(ctx.params.ptyID).pipe(
126
+ Effect.as(true),
127
+ Effect.catchTag("Pty.NotFoundError", () => Effect.succeed(false)),
128
+ )
129
+ if (!exists) return HttpServerResponse.empty({ status: 404 })
130
+ const parsedCursor = url.searchParams.get("cursor")
131
+ const cursorNumber = parsedCursor === null ? undefined : Number(parsedCursor)
132
+ const cursor =
133
+ cursorNumber !== undefined && Number.isSafeInteger(cursorNumber) && cursorNumber >= -1
134
+ ? cursorNumber
135
+ : undefined
136
+
137
+ const socket = yield* Effect.orDie(ctx.request.upgrade)
138
+ // TODO: Integrate graceful-shutdown socket tracking before clients migrate to this route.
139
+ const outbox = yield* Queue.unbounded<Outbound>()
140
+ yield* holdLocation(outbox)
141
+ const attachment = yield* pty
142
+ .attach(ctx.params.ptyID, {
143
+ cursor,
144
+ onData: (chunk) => Queue.offerUnsafe(outbox, chunk),
145
+ onEnd: () => Queue.offerUnsafe(outbox, new Socket.CloseEvent(1000)),
146
+ })
147
+ .pipe(
148
+ Effect.catchTags({
149
+ "Pty.NotFoundError": () =>
150
+ Effect.sync(() => {
151
+ Queue.offerUnsafe(outbox, new Socket.CloseEvent(4404, "session not found"))
152
+ }),
153
+ "Pty.ExitedError": () =>
154
+ Effect.sync(() => {
155
+ Queue.offerUnsafe(outbox, new Socket.CloseEvent(4404, "session exited"))
156
+ }),
157
+ }),
158
+ )
159
+ if (attachment) {
160
+ for (const chunk of PtyProtocol.chunks(attachment.replay)) Queue.offerUnsafe(outbox, chunk)
161
+ Queue.offerUnsafe(outbox, PtyProtocol.metaFrame(attachment.cursor))
162
+ attachment.activate()
163
+ }
164
+
165
+ yield* runPtySocket({
166
+ socket,
167
+ outbox,
168
+ onMessage: (message) =>
169
+ Effect.sync(() => {
170
+ if (!attachment) return
171
+ const decoded = PtyProtocol.decodeInput(message)
172
+ if (decoded !== undefined) attachment.write(decoded)
173
+ }),
174
+ detach: () => attachment?.detach(),
175
+ })
176
+ return HttpServerResponse.empty()
177
+ }).pipe(Effect.scoped, Effect.provide(locations.get(ref)), locationErrors)
178
+ }),
179
+ )
180
+ }),
181
+ )
@@ -0,0 +1,67 @@
1
+ import { Effect, Option } from "effect"
2
+ import { HttpApiSchema } from "effect/http-api"
3
+ import { PushList } from "@zyro-sec/core/push/list"
4
+ import { InvalidRequestError, PushDeviceNotFoundError } from "@zyro-sec/protocol/errors"
5
+ import { Session } from "@zyro-sec/core/session"
6
+ import type { Push } from "@zyro-sec/schema/push"
7
+
8
+ export const makePush = Effect.gen(function* () {
9
+ const list = yield* PushList.Service
10
+ const sessions = yield* Session.Service
11
+
12
+ const invalid = (error: PushList.InvalidError) => new InvalidRequestError({ message: error.reason })
13
+ const notFound = (error: PushList.NotFoundError) =>
14
+ new PushDeviceNotFoundError({ deviceID: error.id, message: error.message })
15
+ // A subagent's notifications follow its top-level session's bell, so a bell on the subagent would do nothing.
16
+ const rejectSubagent = Effect.fnUntraced(function* (sessionID: Session.ID) {
17
+ const session = Option.getOrUndefined(yield* sessions.get(sessionID).pipe(Effect.option))
18
+ if (session?.parentID)
19
+ return yield* new InvalidRequestError({ message: "Set the bell on the top-level session, not on a subagent" })
20
+ })
21
+
22
+ return {
23
+ get: Effect.fn("PushHandler.get")(function* () {
24
+ return { data: yield* list.info() }
25
+ }),
26
+ register: Effect.fn("PushHandler.register")(function* (id: Push.DeviceID, payload: Push.Register) {
27
+ return { data: yield* list.register(id, payload).pipe(Effect.mapError(invalid)) }
28
+ }),
29
+ remove: Effect.fn("PushHandler.remove")(function* (id: Push.DeviceID) {
30
+ yield* list.remove(id)
31
+ return HttpApiSchema.NoContent.make()
32
+ }),
33
+ update: Effect.fn("PushHandler.update")(function* (id: Push.DeviceID, payload: Push.DevicePatch) {
34
+ return { data: yield* list.update(id, payload).pipe(Effect.mapError(notFound)) }
35
+ }),
36
+ setDeviceBell: Effect.fn("PushHandler.setDeviceBell")(function* (
37
+ id: Push.DeviceID,
38
+ sessionID: Session.ID,
39
+ bell: Push.Bell,
40
+ ) {
41
+ yield* rejectSubagent(sessionID)
42
+ return {
43
+ data: yield* list.setDeviceBell(id, sessionID, bell).pipe(
44
+ Effect.catchTags({
45
+ "PushList.InvalidError": (error) => Effect.fail(invalid(error)),
46
+ "PushList.NotFoundError": (error) => Effect.fail(notFound(error)),
47
+ }),
48
+ ),
49
+ }
50
+ }),
51
+ removeDeviceBell: Effect.fn("PushHandler.removeDeviceBell")(function* (id: Push.DeviceID, sessionID: Session.ID) {
52
+ yield* list.removeDeviceBell(id, sessionID).pipe(Effect.mapError(notFound))
53
+ return HttpApiSchema.NoContent.make()
54
+ }),
55
+ setSessionBell: Effect.fn("PushHandler.setSessionBell")(function* (sessionID: Session.ID, bell: Push.Bell) {
56
+ yield* rejectSubagent(sessionID)
57
+ return { data: yield* list.setSessionBell(sessionID, bell).pipe(Effect.mapError(invalid)) }
58
+ }),
59
+ removeSessionBell: Effect.fn("PushHandler.removeSessionBell")(function* (sessionID: Session.ID) {
60
+ yield* list.removeSessionBell(sessionID)
61
+ return HttpApiSchema.NoContent.make()
62
+ }),
63
+ setSettings: Effect.fn("PushHandler.setSettings")(function* (settings: Push.Settings) {
64
+ return { data: yield* list.setSettings(settings).pipe(Effect.mapError(invalid)) }
65
+ }),
66
+ }
67
+ })
@@ -0,0 +1,8 @@
1
+ import { Reference } from "@zyro-sec/core/reference"
2
+ import { HttpApiBuilder } from "effect/http-api"
3
+ import { Api } from "../api"
4
+ import { response } from "../location"
5
+
6
+ export const ReferenceHandler = HttpApiBuilder.group(Api, "server.reference", (handlers) =>
7
+ handlers.handle("reference.list", () => response(Reference.Service.use((reference) => reference.list()))),
8
+ )
@@ -0,0 +1,31 @@
1
+ import { Rpc } from "@zyro-sec/core/rpc"
2
+ import { RpcError, RpcInternalError } from "@zyro-sec/protocol/errors"
3
+ import { Effect } from "effect"
4
+ import { HttpApiBuilder } from "effect/http-api"
5
+ import { Api } from "../api"
6
+
7
+ export const RpcHandler = HttpApiBuilder.group(Api, "server.rpc", (handlers) =>
8
+ handlers.handle("rpc.call", ({ params, payload }) =>
9
+ Effect.gen(function* () {
10
+ const rpc = yield* Rpc.Service
11
+ const output = yield* rpc.call(params.rpcID, params.method, payload.input)
12
+ return output === undefined ? {} : { output }
13
+ }).pipe(
14
+ Effect.mapError((error) =>
15
+ error.type === "rpc.invalid_output" || error.type === "rpc.internal"
16
+ ? new RpcInternalError({ type: error.type, message: error.message })
17
+ : new RpcError({
18
+ type: error.type,
19
+ message: error.message,
20
+ ...(error.data === undefined ? {} : { data: error.data }),
21
+ }),
22
+ ),
23
+ // Defects outside handler execution are still logged, never echoed to the client.
24
+ Effect.catchDefect((defect) =>
25
+ Effect.logError("rpc call failed", { rpc: params.rpcID, method: params.method, defect }).pipe(
26
+ Effect.andThen(Effect.fail(new RpcInternalError({ type: "rpc.internal", message: "RPC call failed" }))),
27
+ ),
28
+ ),
29
+ ),
30
+ ),
31
+ )
@@ -0,0 +1,58 @@
1
+ import { hostname } from "node:os"
2
+ import { Duration, Effect } from "effect"
3
+ import { HttpServerRequest, HttpServerResponse } from "effect/http"
4
+ import { HttpApiBuilder } from "effect/http-api"
5
+ import { PersistentPty } from "@zyro-sec/core/persistent-pty"
6
+ import { UnauthorizedError } from "@zyro-sec/protocol/errors"
7
+ import { Api } from "../api"
8
+ import { ServerAuth } from "../auth"
9
+ import { ServerInfo } from "../server-info"
10
+ import { ServerPairing } from "../pairing"
11
+
12
+ export const ServerHandler = HttpApiBuilder.group(Api, "server.server", (handlers) =>
13
+ Effect.gen(function* () {
14
+ const pairing = yield* ServerPairing.Service
15
+ const auth = yield* ServerAuth.Config
16
+
17
+ return handlers
18
+ .handle("server.info", () =>
19
+ Effect.gen(function* () {
20
+ const info = yield* ServerInfo.Service
21
+ return {
22
+ version: info.app.version ?? "unknown",
23
+ pid: process.pid ?? 0,
24
+ hostname: hostname() || undefined,
25
+ urls: info.urls(),
26
+ paths: info.paths,
27
+ capabilities: { persistentPty: PersistentPty.available },
28
+ }
29
+ }),
30
+ )
31
+ .handle("server.pair", () => pairing.issue())
32
+ .handle(
33
+ "server.connect",
34
+ Effect.fn(function* (ctx) {
35
+ const request = yield* HttpServerRequest.HttpServerRequest
36
+ // Browser navigations ask for HTML; everything else is an API client that wants the token.
37
+ const browser = request.headers.accept?.includes("text/html") === true
38
+ const token = (yield* pairing.consume(ctx.params.code)) ? ServerAuth.issueSession(auth) : undefined
39
+ if (token === undefined) {
40
+ if (!browser) return yield* new UnauthorizedError({ message: "Pairing link expired or already used" })
41
+ return HttpServerResponse.text(
42
+ "This pairing link expired or was already used. Run `opencode pair` to get a new one.",
43
+ { status: 401 },
44
+ )
45
+ }
46
+ if (!browser) return { token }
47
+ return HttpServerResponse.redirect("/").pipe(
48
+ HttpServerResponse.setCookieUnsafe(ServerAuth.sessionCookieName(request.headers.host), token, {
49
+ path: "/",
50
+ httpOnly: true,
51
+ sameSite: "lax",
52
+ maxAge: Duration.seconds(ServerAuth.SESSION_TTL_SECONDS),
53
+ }),
54
+ )
55
+ }),
56
+ )
57
+ }),
58
+ )
@@ -0,0 +1,47 @@
1
+ import { Session } from "@zyro-sec/core/session"
2
+ import type { Snapshot } from "@zyro-sec/core/snapshot"
3
+ import { Skill } from "@zyro-sec/core/skill"
4
+ import { MessageNotFoundError, SessionNotFoundError, SkillLoadError, UnknownError } from "@zyro-sec/protocol/errors"
5
+ import { Effect } from "effect"
6
+
7
+ export function missingSession(error: Session.NotFoundError) {
8
+ return new SessionNotFoundError({
9
+ sessionID: error.sessionID,
10
+ message: `Session not found: ${error.sessionID}`,
11
+ })
12
+ }
13
+
14
+ export function skillLoadFailed(error: Skill.LoadError) {
15
+ return new SkillLoadError({ skill: Skill.ID.make(error.skill), message: error.message })
16
+ }
17
+
18
+ export function missingMessage(error: Session.MessageNotFoundError) {
19
+ return new MessageNotFoundError({
20
+ sessionID: error.sessionID,
21
+ messageID: error.messageID,
22
+ message: `Message not found: ${error.messageID}`,
23
+ })
24
+ }
25
+
26
+ export function failedMessageDecode(error: Session.MessageDecodeError) {
27
+ const ref = `err_${crypto.randomUUID().slice(0, 8)}`
28
+ return Effect.logError("failed to decode session message").pipe(
29
+ Effect.annotateLogs({ ref, sessionID: error.sessionID, messageID: error.messageID }),
30
+ Effect.andThen(
31
+ Effect.fail(new UnknownError({ message: "Unexpected server error. Check server logs for details.", ref })),
32
+ ),
33
+ )
34
+ }
35
+
36
+ /** Snapshot repositories are host state clients cannot repair, so surface only a log reference. */
37
+ export function failedSnapshot(operation: string, sessionID: Session.ID) {
38
+ return (error: Snapshot.Error) => {
39
+ const ref = `err_${crypto.randomUUID().slice(0, 8)}`
40
+ return Effect.logError(`failed to ${operation}`, { cause: error }).pipe(
41
+ Effect.annotateLogs({ ref, sessionID }),
42
+ Effect.andThen(
43
+ Effect.fail(new UnknownError({ message: "Unexpected server error. Check server logs for details.", ref })),
44
+ ),
45
+ )
46
+ }
47
+ }