@zyro-sec/server 1.0.2 → 1.0.3

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 +8 -8
  2. package/script/build.ts +28 -28
  3. package/script/pack.ts +5 -5
  4. package/script/workerd-probe.ts +59 -59
  5. package/src/api.ts +12 -12
  6. package/src/auth.ts +70 -70
  7. package/src/content-headers.ts +32 -32
  8. package/src/cors.ts +34 -34
  9. package/src/event-feed.ts +93 -93
  10. package/src/fetch.ts +67 -67
  11. package/src/handlers/agent.ts +23 -23
  12. package/src/handlers/artifact.ts +242 -242
  13. package/src/handlers/automation.ts +63 -63
  14. package/src/handlers/blob.ts +55 -55
  15. package/src/handlers/command.ts +8 -8
  16. package/src/handlers/config.ts +26 -26
  17. package/src/handlers/credential.ts +87 -87
  18. package/src/handlers/debug.ts +57 -57
  19. package/src/handlers/event.ts +38 -38
  20. package/src/handlers/form.ts +15 -15
  21. package/src/handlers/fs.ts +180 -180
  22. package/src/handlers/generate.ts +117 -117
  23. package/src/handlers/integration.ts +310 -310
  24. package/src/handlers/job.ts +29 -29
  25. package/src/handlers/location.ts +33 -33
  26. package/src/handlers/machine.ts +336 -336
  27. package/src/handlers/mcp.ts +160 -160
  28. package/src/handlers/media.ts +44 -44
  29. package/src/handlers/message.ts +82 -82
  30. package/src/handlers/migration.ts +13 -13
  31. package/src/handlers/model.ts +25 -25
  32. package/src/handlers/permission.ts +131 -131
  33. package/src/handlers/persistent-pty.ts +285 -285
  34. package/src/handlers/plugin.ts +97 -97
  35. package/src/handlers/project.ts +23 -23
  36. package/src/handlers/provider.ts +32 -32
  37. package/src/handlers/pty-socket.ts +47 -47
  38. package/src/handlers/pty.ts +181 -181
  39. package/src/handlers/push.ts +67 -67
  40. package/src/handlers/reference.ts +8 -8
  41. package/src/handlers/rpc.ts +31 -31
  42. package/src/handlers/server.ts +58 -58
  43. package/src/handlers/session-error.ts +47 -47
  44. package/src/handlers/session.ts +722 -722
  45. package/src/handlers/shell.ts +72 -72
  46. package/src/handlers/skill.ts +34 -34
  47. package/src/handlers/usage.ts +23 -23
  48. package/src/handlers/vcs.ts +104 -104
  49. package/src/handlers/voice.ts +50 -50
  50. package/src/handlers/websearch.ts +50 -50
  51. package/src/handlers/worktree.ts +60 -60
  52. package/src/handlers.ts +69 -69
  53. package/src/location.ts +86 -86
  54. package/src/media-uri.ts +13 -13
  55. package/src/middleware/authorization.ts +111 -111
  56. package/src/middleware/defect-response.ts +39 -39
  57. package/src/middleware/form-location.ts +44 -44
  58. package/src/middleware/schema-error.ts +49 -49
  59. package/src/middleware/session-location.ts +30 -30
  60. package/src/options.ts +61 -61
  61. package/src/pairing.ts +36 -36
  62. package/src/process.ts +274 -274
  63. package/src/range.ts +16 -16
  64. package/src/request-site.ts +36 -36
  65. package/src/request-tracing.ts +9 -9
  66. package/src/routes.ts +261 -261
  67. package/src/server-info.ts +41 -41
  68. package/src/service-status.ts +30 -30
  69. package/src/workerd.ts +184 -184
  70. package/test/fixture/mcp-starts.cjs +20 -20
  71. package/test/fixture/routes.ts +37 -37
  72. package/test/fixture/server.ts +20 -20
  73. package/test/fixture/worktree-delegate/index.ts +13 -13
  74. package/test/fixture/worktree-plugin/index.ts +47 -47
  75. package/tsconfig.build.json +15 -15
  76. package/tsconfig.json +11 -11
  77. package/test/artifact.test.ts +0 -224
  78. package/test/auth.test.ts +0 -24
  79. package/test/automation.test.ts +0 -315
  80. package/test/client-mcp.test.ts +0 -369
  81. package/test/codemode-api.test.ts +0 -88
  82. package/test/config.test.ts +0 -104
  83. package/test/cors.test.ts +0 -24
  84. package/test/defect-response.test.ts +0 -83
  85. package/test/event-feed.test.ts +0 -201
  86. package/test/fetch-option.test.ts +0 -104
  87. package/test/fetch.test.ts +0 -698
  88. package/test/flag-channel.test.ts +0 -126
  89. package/test/fs.test.ts +0 -439
  90. package/test/generate.test.ts +0 -132
  91. package/test/handler-policy.test.ts +0 -43
  92. package/test/heap-dump.test.ts +0 -137
  93. package/test/integration-policy.test.ts +0 -93
  94. package/test/job.test.ts +0 -186
  95. package/test/machine-iroh.test.ts +0 -259
  96. package/test/machine-link.test.ts +0 -1234
  97. package/test/machine-routes.test.ts +0 -198
  98. package/test/media.test.ts +0 -343
  99. package/test/message.test.ts +0 -105
  100. package/test/model.test.ts +0 -62
  101. package/test/options.test.ts +0 -42
  102. package/test/permission-saved.test.ts +0 -73
  103. package/test/persistent-pty.test.ts +0 -1007
  104. package/test/process.test.ts +0 -261
  105. package/test/provider.test.ts +0 -88
  106. package/test/pty-socket.property.test.ts +0 -240
  107. package/test/pty-socket.test.ts +0 -89
  108. package/test/pty-transport.test.ts +0 -200
  109. package/test/push-routes.test.ts +0 -157
  110. package/test/request-site.test.ts +0 -71
  111. package/test/request-tracing.test.ts +0 -87
  112. package/test/rpc-handler-errors.test.ts +0 -76
  113. package/test/schema-error.test.ts +0 -68
  114. package/test/service-status.test.ts +0 -34
  115. package/test/session-admit-location.test.ts +0 -92
  116. package/test/session-create.test.ts +0 -65
  117. package/test/session-diff.test.ts +0 -100
  118. package/test/session-form.test.ts +0 -50
  119. package/test/session-import.test.ts +0 -152
  120. package/test/session-instances.test.ts +0 -335
  121. package/test/session-unarchive.test.ts +0 -73
  122. package/test/session-update.test.ts +0 -66
  123. package/test/skill.test.ts +0 -57
  124. package/test/sse-failure.test.ts +0 -71
  125. package/test/usage.test.ts +0 -88
  126. package/test/vcs.test.ts +0 -356
  127. package/test/video-generate.test.ts +0 -131
  128. package/test/voice.test.ts +0 -170
  129. package/test/workerd.test.ts +0 -227
  130. package/test/worktree.test.ts +0 -333
@@ -1,259 +0,0 @@
1
- import { expect } from "bun:test"
2
- import { NodeHttpServer } from "@effect/platform-node"
3
- import { OpenCode } from "@zyro-sec/client"
4
- import { Environment } from "@zyro-sec/core/environment/index"
5
- import { Identity } from "@zyro-sec/core/machine/identity"
6
- import { Iroh } from "@zyro-sec/core/machine/iroh"
7
- import loadBinding from "@zyro-sec/core/machine/iroh-binding"
8
- import { Join } from "@zyro-sec/core/machine/join"
9
- import { Link } from "@zyro-sec/core/machine/link"
10
- import { Machines } from "@zyro-sec/core/machine/machines"
11
- import { Global } from "@zyro-sec/util/global"
12
- import { Context, Deferred, Effect, Exit, Layer, Option, Scope, Stream } from "effect"
13
- import { HttpRouter, HttpServer, type HttpServerRequest, type HttpServerResponse } from "effect/http"
14
- import { randomBytes } from "node:crypto"
15
- import { createServer } from "node:http"
16
- import { makeWorker } from "../../core/test/fixture/machine-worker"
17
- import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
18
- import { it } from "../../core/test/lib/effect"
19
- import { createRoutes } from "../src/routes"
20
-
21
- // The link over Iroh, offline: the home binds on loopback with no relays or address lookup, and a second endpoint of
22
- // the same binding, standing in for the worker, dials the home key at the home's bound address.
23
-
24
- const ALPN = Array.from(new TextEncoder().encode("opencode-link/1"))
25
-
26
- /** Deterministic read boundaries, without a native stream coalescing or splitting the fixture. */
27
- const reading = (chunks: number[][]) => {
28
- const unused = () => {
29
- throw new Error("unexpected stream operation")
30
- }
31
- return Iroh.carrier(
32
- {
33
- recv: {
34
- read: async () => chunks.shift() ?? [],
35
- readExact: unused,
36
- readToEnd: unused,
37
- id: unused,
38
- bytesRead: unused,
39
- stop: unused,
40
- receivedReset: unused,
41
- },
42
- send: {
43
- write: unused,
44
- writeAll: unused,
45
- finish: unused,
46
- reset: unused,
47
- setPriority: unused,
48
- priority: unused,
49
- stopped: unused,
50
- id: unused,
51
- },
52
- },
53
- unused,
54
- )
55
- }
56
-
57
- it.effect("reads many Iroh messages intact and in order from one read", () =>
58
- Effect.gen(function* () {
59
- const messages = Array.from({ length: 900 }, (_, index) => {
60
- const message = new Uint8Array(70).fill(index % 256)
61
- new DataView(message.buffer).setUint16(0, index)
62
- return message
63
- })
64
- const carrier = reading([messages.flatMap((message) => [0, message.length, ...message])])
65
- const received = yield* Effect.forEach(messages, () => carrier.receive)
66
- expect(received).toEqual(messages)
67
- received[0][0] ^= 1
68
- expect(received[1]).toEqual(messages[1])
69
- expect(yield* carrier.receive.pipe(Effect.flip)).toEqual(new Link.Closed())
70
- }),
71
- )
72
-
73
- it.effect("reassembles an Iroh message split across two reads after an earlier message", () =>
74
- Effect.gen(function* () {
75
- const carrier = reading([
76
- [0, 3, 1, 2, 3, 0, 4, 4],
77
- [5, 6, 7],
78
- ])
79
- const first = yield* carrier.receive
80
- expect(yield* carrier.receive).toEqual(Uint8Array.of(4, 5, 6, 7))
81
- expect(first).toEqual(Uint8Array.of(1, 2, 3))
82
- expect(yield* carrier.receive.pipe(Effect.flip)).toEqual(new Link.Closed())
83
- }),
84
- )
85
-
86
- type Endpoint = Effect.Success<ReturnType<typeof Iroh.listen>>
87
-
88
- /** The server's routes on a port of their own, counting every import of the Iroh binding. */
89
- const home = Effect.fnUntraced(function* (machines: boolean) {
90
- const directory = (yield* tmpdirScoped("opencode-machine-iroh-")).path
91
- const server = createServer()
92
- const http = yield* NodeHttpServer.make(() => server, { port: 0, host: "127.0.0.1" })
93
- yield* Effect.addFinalizer(() => Effect.sync(() => server.closeAllConnections()))
94
- const routes =
95
- yield* Deferred.make<
96
- Effect.Effect<HttpServerResponse.HttpServerResponse, unknown, HttpServerRequest.HttpServerRequest | Scope.Scope>
97
- >()
98
- yield* http.serve(Effect.flatten(Deferred.await(routes))).pipe(Effect.provide(NodeHttpServer.layerHttpServices))
99
- const overrides = [
100
- Global.node.replace(
101
- Global.layerWith({
102
- data: directory,
103
- cache: directory,
104
- config: directory,
105
- state: directory,
106
- tmp: directory,
107
- bin: directory,
108
- log: directory,
109
- repos: directory,
110
- }),
111
- ),
112
- ]
113
- const state = { loads: 0, endpoint: undefined as Endpoint | undefined }
114
- const options = {
115
- password: "secret",
116
- app: { version: "test-version" },
117
- database: { path: ":memory:" },
118
- models: { fetch: false },
119
- config: { directory, project: false, content: JSON.stringify({ experimental: { machines } }) },
120
- fs: { filewatcher: false },
121
- }
122
- const context = yield* Layer.build(
123
- createRoutes(options, () => [], overrides).pipe(Layer.provideMerge(NodeHttpServer.layerHttpServices)),
124
- ).pipe(
125
- Effect.provideService(Iroh.Testing, {
126
- offline: true,
127
- load: () => {
128
- state.loads++
129
- return loadBinding()
130
- },
131
- watch: (endpoint) => {
132
- state.endpoint = endpoint
133
- },
134
- }),
135
- )
136
- yield* Deferred.succeed(routes, Context.get(context, HttpRouter.HttpRouter).asHttpEffect())
137
- return {
138
- state,
139
- context,
140
- machines: Context.get(context, Machines.Service),
141
- api: OpenCode.make({
142
- baseUrl: HttpServer.formatAddress(http.address),
143
- headers: { authorization: `Basic ${btoa("opencode:secret")}` },
144
- }),
145
- }
146
- })
147
-
148
- /** A worker's endpoint on loopback, and a dial of the home key over it whose stream is the link's carrier. */
149
- const workerEndpoint = Effect.fnUntraced(function* (home: Endpoint) {
150
- const binding = yield* Effect.promise(() => loadBinding())
151
- if (!binding) return yield* Effect.die(new Error("no Iroh build for this platform"))
152
- const builder = binding.Endpoint.builder()
153
- builder.applyMinimal()
154
- builder.bindAddr("127.0.0.1:0")
155
- const endpoint = yield* Effect.acquireRelease(
156
- Effect.promise(() => builder.bind()),
157
- (endpoint) => Effect.promise(() => endpoint.close()),
158
- )
159
- return (key: Uint8Array) =>
160
- Effect.gen(function* () {
161
- const address = new binding.EndpointAddr(binding.EndpointId.fromBytes(Array.from(key)), null, home.boundSockets())
162
- const connection = yield* Effect.acquireRelease(
163
- Effect.promise(() => endpoint.connect(address, ALPN)),
164
- (connection) => Effect.sync(() => connection.close(0n, [])),
165
- )
166
- const stream = yield* Effect.promise(() => connection.openBi())
167
- return { connection, carrier: Iroh.carrier(stream, () => connection.close(0n, [])) }
168
- })
169
- })
170
-
171
- const until = Effect.fnUntraced(function* (condition: Effect.Effect<boolean>) {
172
- const deadline = Date.now() + 10_000
173
- while (!(yield* condition)) {
174
- if (Date.now() > deadline) return yield* Effect.die(new Error("timed out"))
175
- yield* Effect.sleep("10 millis")
176
- }
177
- })
178
-
179
- it.live("pairs a worker over Iroh, carries its frames, and refuses a bad join with 1008", () =>
180
- Effect.gen(function* () {
181
- const server = yield* home(true)
182
- yield* Effect.promise(() => server.api.experimental.machine.list())
183
- expect(server.state.loads).toBe(0)
184
-
185
- const added = yield* Effect.promise(() => server.api.experimental.machine.add({ name: "gpu", reach: [] }))
186
- expect(server.state.loads).toBe(1)
187
- const join = Option.getOrThrow(Join.decode(added.join))
188
- expect(join.reach).toEqual([{ type: "iroh" }])
189
- expect(added.reach).toEqual(join.reach)
190
- const dial = yield* workerEndpoint(server.state.endpoint!)
191
-
192
- // A stranger with a wrong secret: refused, and the home closes the connection with 1008.
193
- const stranger = yield* dial(join.home)
194
- const refused = yield* Link.connect(stranger.carrier, {
195
- home: join.home,
196
- identity: Identity.fromSeed(randomBytes(32)),
197
- secret: randomBytes(32),
198
- }).pipe(Effect.flip)
199
- expect(refused._tag).toBe("Link.Refused")
200
- expect(yield* Effect.promise(() => stranger.connection.closed())).toContain("1008")
201
-
202
- // The worker pairs with the join secret, dialing the home key, and files go both ways over the link.
203
- const identity = Identity.fromSeed(randomBytes(32))
204
- const fake = yield* makeWorker({
205
- connect: (connection) =>
206
- Effect.scoped(
207
- Effect.gen(function* () {
208
- const dialed = yield* dial(join.home)
209
- const linked = yield* Link.connect(dialed.carrier, { home: join.home, identity, secret: join.secret })
210
- linked.send(connection.hello)
211
- yield* Effect.raceFirst(
212
- Stream.runForEach(linked.messages, (message) => Effect.sync(() => connection.send(message))),
213
- Stream.runForEach(connection.messages, (message) => Effect.sync(() => linked.send(message))),
214
- )
215
- }),
216
- ),
217
- })
218
- yield* fake.restore
219
- yield* until(Effect.map(server.machines.list(), (list) => list.some((machine) => machine.online)))
220
- const listed = yield* Effect.promise(() => server.api.experimental.machine.list())
221
- expect(listed[0].connection).toMatchObject({ kind: "iroh", path: "direct" })
222
- expect(listed[0].connection?.rtt).toBeGreaterThanOrEqual(0)
223
- const files = (yield* server.machines.get(Environment.ID.make("gpu"))).files
224
- const bytes = randomBytes(200_000)
225
- yield* files.write("/note.bin", bytes)
226
- expect(Buffer.compare((yield* files.read("/note.bin")).bytes, bytes)).toBe(0)
227
- expect(server.state.loads).toBe(1)
228
- }),
229
- )
230
-
231
- it.live("refuses an Iroh connection while machines are off", () =>
232
- Effect.gen(function* () {
233
- const server = yield* home(true)
234
- const endpoint = yield* Iroh.listen(Effect.succeed(false)).pipe(
235
- Effect.provide(server.context),
236
- Effect.provideService(Iroh.Testing, { offline: true, load: loadBinding, watch: () => {} }),
237
- )
238
- const identity = Context.get(server.context, Identity.Service)
239
- const dial = yield* workerEndpoint(endpoint)
240
- const dialed = yield* Effect.exit(Effect.scoped(dial(yield* identity.publicKey())))
241
- expect(Exit.isFailure(dialed)).toBe(true)
242
- }),
243
- )
244
-
245
- it.live("never imports Iroh while machines are off, or on with no machines", () =>
246
- Effect.gen(function* () {
247
- const off = yield* home(false)
248
- const results = yield* Effect.promise(() =>
249
- Promise.allSettled([
250
- off.api.experimental.machine.list(),
251
- off.api.experimental.machine.add({ name: "gpu", reach: [] }),
252
- ]),
253
- )
254
- expect(results.map((result) => result.status)).toEqual(["rejected", "rejected"])
255
- const on = yield* home(true)
256
- yield* Effect.promise(() => on.api.experimental.machine.list())
257
- expect([off.state.loads, on.state.loads]).toEqual([0, 0])
258
- }),
259
- )