@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.
- package/package.json +8 -8
- package/script/build.ts +28 -28
- package/script/pack.ts +5 -5
- package/script/workerd-probe.ts +59 -59
- package/src/api.ts +12 -12
- package/src/auth.ts +70 -70
- package/src/content-headers.ts +32 -32
- package/src/cors.ts +34 -34
- package/src/event-feed.ts +93 -93
- package/src/fetch.ts +67 -67
- package/src/handlers/agent.ts +23 -23
- package/src/handlers/artifact.ts +242 -242
- package/src/handlers/automation.ts +63 -63
- package/src/handlers/blob.ts +55 -55
- package/src/handlers/command.ts +8 -8
- package/src/handlers/config.ts +26 -26
- package/src/handlers/credential.ts +87 -87
- package/src/handlers/debug.ts +57 -57
- package/src/handlers/event.ts +38 -38
- package/src/handlers/form.ts +15 -15
- package/src/handlers/fs.ts +180 -180
- package/src/handlers/generate.ts +117 -117
- package/src/handlers/integration.ts +310 -310
- package/src/handlers/job.ts +29 -29
- package/src/handlers/location.ts +33 -33
- package/src/handlers/machine.ts +336 -336
- package/src/handlers/mcp.ts +160 -160
- package/src/handlers/media.ts +44 -44
- package/src/handlers/message.ts +82 -82
- package/src/handlers/migration.ts +13 -13
- package/src/handlers/model.ts +25 -25
- package/src/handlers/permission.ts +131 -131
- package/src/handlers/persistent-pty.ts +285 -285
- package/src/handlers/plugin.ts +97 -97
- package/src/handlers/project.ts +23 -23
- package/src/handlers/provider.ts +32 -32
- package/src/handlers/pty-socket.ts +47 -47
- package/src/handlers/pty.ts +181 -181
- package/src/handlers/push.ts +67 -67
- package/src/handlers/reference.ts +8 -8
- package/src/handlers/rpc.ts +31 -31
- package/src/handlers/server.ts +58 -58
- package/src/handlers/session-error.ts +47 -47
- package/src/handlers/session.ts +722 -722
- package/src/handlers/shell.ts +72 -72
- package/src/handlers/skill.ts +34 -34
- package/src/handlers/usage.ts +23 -23
- package/src/handlers/vcs.ts +104 -104
- package/src/handlers/voice.ts +50 -50
- package/src/handlers/websearch.ts +50 -50
- package/src/handlers/worktree.ts +60 -60
- package/src/handlers.ts +69 -69
- package/src/location.ts +86 -86
- package/src/media-uri.ts +13 -13
- package/src/middleware/authorization.ts +111 -111
- package/src/middleware/defect-response.ts +39 -39
- package/src/middleware/form-location.ts +44 -44
- package/src/middleware/schema-error.ts +49 -49
- package/src/middleware/session-location.ts +30 -30
- package/src/options.ts +61 -61
- package/src/pairing.ts +36 -36
- package/src/process.ts +274 -274
- package/src/range.ts +16 -16
- package/src/request-site.ts +36 -36
- package/src/request-tracing.ts +9 -9
- package/src/routes.ts +261 -261
- package/src/server-info.ts +41 -41
- package/src/service-status.ts +30 -30
- package/src/workerd.ts +184 -184
- package/test/fixture/mcp-starts.cjs +20 -20
- package/test/fixture/routes.ts +37 -37
- package/test/fixture/server.ts +20 -20
- package/test/fixture/worktree-delegate/index.ts +13 -13
- package/test/fixture/worktree-plugin/index.ts +47 -47
- package/tsconfig.build.json +15 -15
- package/tsconfig.json +11 -11
- package/test/artifact.test.ts +0 -224
- package/test/auth.test.ts +0 -24
- package/test/automation.test.ts +0 -315
- package/test/client-mcp.test.ts +0 -369
- package/test/codemode-api.test.ts +0 -88
- package/test/config.test.ts +0 -104
- package/test/cors.test.ts +0 -24
- package/test/defect-response.test.ts +0 -83
- package/test/event-feed.test.ts +0 -201
- package/test/fetch-option.test.ts +0 -104
- package/test/fetch.test.ts +0 -698
- package/test/flag-channel.test.ts +0 -126
- package/test/fs.test.ts +0 -439
- package/test/generate.test.ts +0 -132
- package/test/handler-policy.test.ts +0 -43
- package/test/heap-dump.test.ts +0 -137
- package/test/integration-policy.test.ts +0 -93
- package/test/job.test.ts +0 -186
- package/test/machine-iroh.test.ts +0 -259
- package/test/machine-link.test.ts +0 -1234
- package/test/machine-routes.test.ts +0 -198
- package/test/media.test.ts +0 -343
- package/test/message.test.ts +0 -105
- package/test/model.test.ts +0 -62
- package/test/options.test.ts +0 -42
- package/test/permission-saved.test.ts +0 -73
- package/test/persistent-pty.test.ts +0 -1007
- package/test/process.test.ts +0 -261
- package/test/provider.test.ts +0 -88
- package/test/pty-socket.property.test.ts +0 -240
- package/test/pty-socket.test.ts +0 -89
- package/test/pty-transport.test.ts +0 -200
- package/test/push-routes.test.ts +0 -157
- package/test/request-site.test.ts +0 -71
- package/test/request-tracing.test.ts +0 -87
- package/test/rpc-handler-errors.test.ts +0 -76
- package/test/schema-error.test.ts +0 -68
- package/test/service-status.test.ts +0 -34
- package/test/session-admit-location.test.ts +0 -92
- package/test/session-create.test.ts +0 -65
- package/test/session-diff.test.ts +0 -100
- package/test/session-form.test.ts +0 -50
- package/test/session-import.test.ts +0 -152
- package/test/session-instances.test.ts +0 -335
- package/test/session-unarchive.test.ts +0 -73
- package/test/session-update.test.ts +0 -66
- package/test/skill.test.ts +0 -57
- package/test/sse-failure.test.ts +0 -71
- package/test/usage.test.ts +0 -88
- package/test/vcs.test.ts +0 -356
- package/test/video-generate.test.ts +0 -131
- package/test/voice.test.ts +0 -170
- package/test/workerd.test.ts +0 -227
- package/test/worktree.test.ts +0 -333
package/src/service-status.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
export * as Status from "./service-status"
|
|
2
|
-
|
|
3
|
-
import { Effect, Ref } from "effect"
|
|
4
|
-
|
|
5
|
-
export type State =
|
|
6
|
-
| { readonly type: "starting" }
|
|
7
|
-
| { readonly type: "ready" }
|
|
8
|
-
| { readonly type: "stopping" }
|
|
9
|
-
| { readonly type: "failed" }
|
|
10
|
-
|
|
11
|
-
export interface Interface {
|
|
12
|
-
readonly current: Effect.Effect<State>
|
|
13
|
-
readonly ready: Effect.Effect<void>
|
|
14
|
-
readonly fail: Effect.Effect<void>
|
|
15
|
-
readonly beginStopping: Effect.Effect<void>
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const make = Effect.fnUntraced(function* (options: { readonly initial?: State } = {}) {
|
|
19
|
-
const current = yield* Ref.make(options.initial ?? ({ type: "starting" } satisfies State))
|
|
20
|
-
const beginStopping = Ref.update(current, (status) =>
|
|
21
|
-
status.type === "stopping" ? status : ({ type: "stopping" } satisfies State),
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
current: Ref.get(current),
|
|
26
|
-
ready: Ref.update(current, (status) => (status.type === "starting" ? ({ type: "ready" } satisfies State) : status)),
|
|
27
|
-
fail: Ref.update(current, (status) => (status.type === "starting" ? ({ type: "failed" } satisfies State) : status)),
|
|
28
|
-
beginStopping,
|
|
29
|
-
} satisfies Interface
|
|
30
|
-
})
|
|
1
|
+
export * as Status from "./service-status"
|
|
2
|
+
|
|
3
|
+
import { Effect, Ref } from "effect"
|
|
4
|
+
|
|
5
|
+
export type State =
|
|
6
|
+
| { readonly type: "starting" }
|
|
7
|
+
| { readonly type: "ready" }
|
|
8
|
+
| { readonly type: "stopping" }
|
|
9
|
+
| { readonly type: "failed" }
|
|
10
|
+
|
|
11
|
+
export interface Interface {
|
|
12
|
+
readonly current: Effect.Effect<State>
|
|
13
|
+
readonly ready: Effect.Effect<void>
|
|
14
|
+
readonly fail: Effect.Effect<void>
|
|
15
|
+
readonly beginStopping: Effect.Effect<void>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const make = Effect.fnUntraced(function* (options: { readonly initial?: State } = {}) {
|
|
19
|
+
const current = yield* Ref.make(options.initial ?? ({ type: "starting" } satisfies State))
|
|
20
|
+
const beginStopping = Ref.update(current, (status) =>
|
|
21
|
+
status.type === "stopping" ? status : ({ type: "stopping" } satisfies State),
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
current: Ref.get(current),
|
|
26
|
+
ready: Ref.update(current, (status) => (status.type === "starting" ? ({ type: "ready" } satisfies State) : status)),
|
|
27
|
+
fail: Ref.update(current, (status) => (status.type === "starting" ? ({ type: "failed" } satisfies State) : status)),
|
|
28
|
+
beginStopping,
|
|
29
|
+
} satisfies Interface
|
|
30
|
+
})
|
package/src/workerd.ts
CHANGED
|
@@ -1,184 +1,184 @@
|
|
|
1
|
-
export * as ServerWorkerd from "./workerd"
|
|
2
|
-
|
|
3
|
-
import { Context, Effect, Layer } from "effect"
|
|
4
|
-
import { BlobStore } from "@zyro-sec/core/blob-store"
|
|
5
|
-
import { ConfigPluginSource } from "@zyro-sec/core/config/plugin/source"
|
|
6
|
-
import { Database } from "@zyro-sec/core/database/database"
|
|
7
|
-
import { sqliteLayer } from "@zyro-sec/core/database/sqlite.workerd"
|
|
8
|
-
import type { DurableObjectStorage } from "@zyro-sec/core/database/sqlite.workerd"
|
|
9
|
-
import { EnvironmentUnavailable } from "@zyro-sec/core/environment/unavailable"
|
|
10
|
-
import { Identity } from "@zyro-sec/core/machine/identity"
|
|
11
|
-
import { Pty } from "@zyro-sec/core/pty"
|
|
12
|
-
import { Snapshot } from "@zyro-sec/core/snapshot"
|
|
13
|
-
import { Vcs } from "@zyro-sec/core/vcs"
|
|
14
|
-
import { VideoGeneration } from "@zyro-sec/core/video-generation"
|
|
15
|
-
import { Workspace } from "@zyro-sec/core/workspace"
|
|
16
|
-
import { CrossSpawnSpawner } from "@zyro-sec/util/cross-spawn-spawner"
|
|
17
|
-
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
18
|
-
import { Npm } from "@zyro-sec/util/npm"
|
|
19
|
-
import { ServerFetch } from "./fetch"
|
|
20
|
-
import type { ServerOptions } from "./options"
|
|
21
|
-
import { withoutParentSpan } from "./request-tracing"
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The workerd runtime profile: boots opencode core and server inside a
|
|
25
|
-
* Cloudflare Durable Object, with every intentionally-local service replaced
|
|
26
|
-
* or disabled.
|
|
27
|
-
*
|
|
28
|
-
* - Database runs on the injected `DurableObjectStorage` SQLite.
|
|
29
|
-
* - `Workspace` is configured with `keepWarm: 0`, `pollInterval: null`, and
|
|
30
|
-
* `storage.setAlarm` scheduling so the Durable Object can hibernate at zero
|
|
31
|
-
* idle cost. Callers forward the Durable Object's `alarm()` method to
|
|
32
|
-
* `handler.alarm()` (or `opencode.alarm()` via `@zyro-sec/sdk/workerd`).
|
|
33
|
-
* - Watcher and fff are disabled through their existing option flags; pty, fff,
|
|
34
|
-
* and photon native modules resolve to inert stubs under the `workerd` bundle
|
|
35
|
-
* condition.
|
|
36
|
-
* - Bare locations use a typed no-execution-plane process spawner; FileSystem
|
|
37
|
-
* and FileSystemSearch route through Environment (working on attached remote
|
|
38
|
-
* workspaces and failing cleanly on bare locations). Pty fails with a clear
|
|
39
|
-
* defect; Npm fails package installs with a typed workerd error; Snapshot and
|
|
40
|
-
* Vcs degrade to no-op results.
|
|
41
|
-
* - Blob storage and video generation report themselves unsupported: blobs are files on a local disk.
|
|
42
|
-
* - Config is injected as a string (no filesystem); plugin discovery is
|
|
43
|
-
* precompiled-only, and stdio MCP reports the same no-plane failure as Shell.
|
|
44
|
-
*
|
|
45
|
-
* Bundle with the `workerd` condition, e.g.
|
|
46
|
-
* `bun build src/workerd.ts --conditions=workerd --target=node`
|
|
47
|
-
* (see `script/workerd-probe.ts`).
|
|
48
|
-
*/
|
|
49
|
-
export interface Options {
|
|
50
|
-
/** Durable Object storage whose SQLite database backs the opencode database. */
|
|
51
|
-
readonly storage: DurableObjectStorage
|
|
52
|
-
readonly app?: ServerOptions["app"]
|
|
53
|
-
readonly password?: string
|
|
54
|
-
/** Inline opencode config content (JSON), same as `ServerOptions.config.content`. */
|
|
55
|
-
readonly config?: { readonly content?: string }
|
|
56
|
-
/** models.dev catalog options; the bundled snapshot is the boot-time floor either way. */
|
|
57
|
-
readonly models?: ServerOptions["models"]
|
|
58
|
-
/** Replaces `globalThis.fetch` for OpenCode's own HTTP requests, e.g. to route them through a Service Binding. */
|
|
59
|
-
readonly fetch?: ServerOptions["fetch"]
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface Handler {
|
|
63
|
-
(request: Request): Promise<Response>
|
|
64
|
-
/** Suspends any workspaces past their idle threshold; call from the Durable Object's `alarm()` method. */
|
|
65
|
-
readonly alarm: () => Promise<void>
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Builds the web-standard fetch + alarm handler for a Durable Object. The
|
|
70
|
-
* application layer builds eagerly in the caller's scope, so hold it in the
|
|
71
|
-
* Durable Object instance rather than per request.
|
|
72
|
-
*
|
|
73
|
-
* Callers must forward the Durable Object's `alarm()` lifecycle method to
|
|
74
|
-
* `handler.alarm()`: this profile disables background idle polling so the
|
|
75
|
-
* isolate can hibernate at zero idle cost.
|
|
76
|
-
*/
|
|
77
|
-
export const create = Effect.fn("ServerWorkerd.create")(function* (options: Options) {
|
|
78
|
-
// Eviction can kill the isolate between a turn's Started and terminal events with no
|
|
79
|
-
// teardown. The write-ahead execution claim plus ServerFetch.make's boot-time resume
|
|
80
|
-
// recovers such orphaned turns by replaying the drain from durable history on the next wake.
|
|
81
|
-
const server = yield* ServerFetch.makeWithContext(serverOptions(options), {
|
|
82
|
-
overrides: replacements(options),
|
|
83
|
-
})
|
|
84
|
-
const run = Effect.runPromiseWith(yield* withoutParentSpan(Effect.context()))
|
|
85
|
-
const workspace = Context.get(server.context, Workspace.Service)
|
|
86
|
-
const handler: Handler = Object.assign(server.handler, {
|
|
87
|
-
alarm: () => run(Effect.asVoid(workspace.suspendIdle())),
|
|
88
|
-
})
|
|
89
|
-
return handler
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
export function serverOptions(options: Options): ServerOptions {
|
|
93
|
-
return {
|
|
94
|
-
app: options.app,
|
|
95
|
-
password: options.password,
|
|
96
|
-
fs: { filewatcher: false, fff: false },
|
|
97
|
-
// Durable event history is how a turn orphaned by eviction is recovered:
|
|
98
|
-
// the boot-time resume replays it. A runtime that dies without teardown
|
|
99
|
-
// cannot opt out of it, so this is not exposed as an option.
|
|
100
|
-
events: { persist: true },
|
|
101
|
-
config: { content: options.config?.content },
|
|
102
|
-
models: options.models,
|
|
103
|
-
fetch: options.fetch,
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** The workerd replacement graph, applied after the standard server replacements. */
|
|
108
|
-
export function replacements(options: Options): LayerNode.Replacements {
|
|
109
|
-
return [
|
|
110
|
-
Database.node.replace(Database.configuredClient(sqliteLayer({ storage: options.storage }))),
|
|
111
|
-
Identity.node.replace(Identity.kvNode),
|
|
112
|
-
Workspace.node.replace(
|
|
113
|
-
Workspace.configured({
|
|
114
|
-
keepWarm: 0,
|
|
115
|
-
pollInterval: null,
|
|
116
|
-
onIdleScheduled: (atMs) => Effect.tryPromise(() => options.storage.setAlarm(atMs)),
|
|
117
|
-
}),
|
|
118
|
-
),
|
|
119
|
-
CrossSpawnSpawner.node.replace(EnvironmentUnavailable.layer),
|
|
120
|
-
Snapshot.node.replace(Snapshot.noopLayer),
|
|
121
|
-
Vcs.node.replace(vcsLayer),
|
|
122
|
-
Pty.node.replace(ptyLayer),
|
|
123
|
-
Npm.node.replace(npmLayer),
|
|
124
|
-
// Blobs need a local `blobs/` folder, which a Durable Object doesn't have.
|
|
125
|
-
BlobStore.node.replace(BlobStore.unsupportedLayer),
|
|
126
|
-
VideoGeneration.node.replace(VideoGeneration.unsupportedLayer),
|
|
127
|
-
// Precompiled (internal and SDK) plugins only: no plugin-directory scan, npm
|
|
128
|
-
// install, or import of plugin code from disk.
|
|
129
|
-
ConfigPluginSource.node.replace(ConfigPluginSource.empty),
|
|
130
|
-
]
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const unavailable = (what: string) => Effect.die(new Error(`${what} is unavailable in the workerd profile`))
|
|
134
|
-
|
|
135
|
-
const npmUnavailable = (what: string, pkg: string) =>
|
|
136
|
-
Effect.fail(
|
|
137
|
-
new Npm.InstallFailedError({
|
|
138
|
-
add: [pkg],
|
|
139
|
-
dir: "",
|
|
140
|
-
cause: new Error(`${what} is unavailable in the workerd profile`),
|
|
141
|
-
}),
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
const npmLayer = Layer.succeed(
|
|
145
|
-
Npm.Service,
|
|
146
|
-
Npm.Service.of({
|
|
147
|
-
add: (pkg) => npmUnavailable("Npm.add", pkg),
|
|
148
|
-
resolve: (name) => Effect.succeed({ directory: "", name }),
|
|
149
|
-
check: () => Effect.succeed(false),
|
|
150
|
-
update: (pkg) => npmUnavailable("Npm.update", pkg),
|
|
151
|
-
follows: () => Effect.succeed(false),
|
|
152
|
-
reject: () => Effect.succeed(false),
|
|
153
|
-
which: () => Effect.undefined,
|
|
154
|
-
}),
|
|
155
|
-
)
|
|
156
|
-
|
|
157
|
-
// Vcs degrades to empty results, matching its behavior for locations without a
|
|
158
|
-
// supported VCS, so read-only clients never need to special-case this runtime.
|
|
159
|
-
const vcsLayer = Layer.succeed(
|
|
160
|
-
Vcs.Service,
|
|
161
|
-
Vcs.Service.of({
|
|
162
|
-
base: () => Effect.succeed(null),
|
|
163
|
-
initialize: () => Effect.fail(new Vcs.InitializeError({ kind: "unsupported" })),
|
|
164
|
-
transform: () => Effect.succeed({ dispose: Effect.void }),
|
|
165
|
-
reload: () => Effect.void,
|
|
166
|
-
info: () => Effect.succeed({ branch: {} }),
|
|
167
|
-
branches: () => Effect.succeed([]),
|
|
168
|
-
status: () => Effect.succeed([]),
|
|
169
|
-
diff: () => Effect.succeed([]),
|
|
170
|
-
}),
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
const ptyLayer = Layer.succeed(
|
|
174
|
-
Pty.Service,
|
|
175
|
-
Pty.Service.of({
|
|
176
|
-
list: () => Effect.succeed([]),
|
|
177
|
-
get: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
178
|
-
create: () => unavailable("Pty.create"),
|
|
179
|
-
update: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
180
|
-
remove: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
181
|
-
write: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
182
|
-
attach: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
183
|
-
}),
|
|
184
|
-
)
|
|
1
|
+
export * as ServerWorkerd from "./workerd"
|
|
2
|
+
|
|
3
|
+
import { Context, Effect, Layer } from "effect"
|
|
4
|
+
import { BlobStore } from "@zyro-sec/core/blob-store"
|
|
5
|
+
import { ConfigPluginSource } from "@zyro-sec/core/config/plugin/source"
|
|
6
|
+
import { Database } from "@zyro-sec/core/database/database"
|
|
7
|
+
import { sqliteLayer } from "@zyro-sec/core/database/sqlite.workerd"
|
|
8
|
+
import type { DurableObjectStorage } from "@zyro-sec/core/database/sqlite.workerd"
|
|
9
|
+
import { EnvironmentUnavailable } from "@zyro-sec/core/environment/unavailable"
|
|
10
|
+
import { Identity } from "@zyro-sec/core/machine/identity"
|
|
11
|
+
import { Pty } from "@zyro-sec/core/pty"
|
|
12
|
+
import { Snapshot } from "@zyro-sec/core/snapshot"
|
|
13
|
+
import { Vcs } from "@zyro-sec/core/vcs"
|
|
14
|
+
import { VideoGeneration } from "@zyro-sec/core/video-generation"
|
|
15
|
+
import { Workspace } from "@zyro-sec/core/workspace"
|
|
16
|
+
import { CrossSpawnSpawner } from "@zyro-sec/util/cross-spawn-spawner"
|
|
17
|
+
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
18
|
+
import { Npm } from "@zyro-sec/util/npm"
|
|
19
|
+
import { ServerFetch } from "./fetch"
|
|
20
|
+
import type { ServerOptions } from "./options"
|
|
21
|
+
import { withoutParentSpan } from "./request-tracing"
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The workerd runtime profile: boots opencode core and server inside a
|
|
25
|
+
* Cloudflare Durable Object, with every intentionally-local service replaced
|
|
26
|
+
* or disabled.
|
|
27
|
+
*
|
|
28
|
+
* - Database runs on the injected `DurableObjectStorage` SQLite.
|
|
29
|
+
* - `Workspace` is configured with `keepWarm: 0`, `pollInterval: null`, and
|
|
30
|
+
* `storage.setAlarm` scheduling so the Durable Object can hibernate at zero
|
|
31
|
+
* idle cost. Callers forward the Durable Object's `alarm()` method to
|
|
32
|
+
* `handler.alarm()` (or `opencode.alarm()` via `@zyro-sec/sdk/workerd`).
|
|
33
|
+
* - Watcher and fff are disabled through their existing option flags; pty, fff,
|
|
34
|
+
* and photon native modules resolve to inert stubs under the `workerd` bundle
|
|
35
|
+
* condition.
|
|
36
|
+
* - Bare locations use a typed no-execution-plane process spawner; FileSystem
|
|
37
|
+
* and FileSystemSearch route through Environment (working on attached remote
|
|
38
|
+
* workspaces and failing cleanly on bare locations). Pty fails with a clear
|
|
39
|
+
* defect; Npm fails package installs with a typed workerd error; Snapshot and
|
|
40
|
+
* Vcs degrade to no-op results.
|
|
41
|
+
* - Blob storage and video generation report themselves unsupported: blobs are files on a local disk.
|
|
42
|
+
* - Config is injected as a string (no filesystem); plugin discovery is
|
|
43
|
+
* precompiled-only, and stdio MCP reports the same no-plane failure as Shell.
|
|
44
|
+
*
|
|
45
|
+
* Bundle with the `workerd` condition, e.g.
|
|
46
|
+
* `bun build src/workerd.ts --conditions=workerd --target=node`
|
|
47
|
+
* (see `script/workerd-probe.ts`).
|
|
48
|
+
*/
|
|
49
|
+
export interface Options {
|
|
50
|
+
/** Durable Object storage whose SQLite database backs the opencode database. */
|
|
51
|
+
readonly storage: DurableObjectStorage
|
|
52
|
+
readonly app?: ServerOptions["app"]
|
|
53
|
+
readonly password?: string
|
|
54
|
+
/** Inline opencode config content (JSON), same as `ServerOptions.config.content`. */
|
|
55
|
+
readonly config?: { readonly content?: string }
|
|
56
|
+
/** models.dev catalog options; the bundled snapshot is the boot-time floor either way. */
|
|
57
|
+
readonly models?: ServerOptions["models"]
|
|
58
|
+
/** Replaces `globalThis.fetch` for OpenCode's own HTTP requests, e.g. to route them through a Service Binding. */
|
|
59
|
+
readonly fetch?: ServerOptions["fetch"]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface Handler {
|
|
63
|
+
(request: Request): Promise<Response>
|
|
64
|
+
/** Suspends any workspaces past their idle threshold; call from the Durable Object's `alarm()` method. */
|
|
65
|
+
readonly alarm: () => Promise<void>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Builds the web-standard fetch + alarm handler for a Durable Object. The
|
|
70
|
+
* application layer builds eagerly in the caller's scope, so hold it in the
|
|
71
|
+
* Durable Object instance rather than per request.
|
|
72
|
+
*
|
|
73
|
+
* Callers must forward the Durable Object's `alarm()` lifecycle method to
|
|
74
|
+
* `handler.alarm()`: this profile disables background idle polling so the
|
|
75
|
+
* isolate can hibernate at zero idle cost.
|
|
76
|
+
*/
|
|
77
|
+
export const create = Effect.fn("ServerWorkerd.create")(function* (options: Options) {
|
|
78
|
+
// Eviction can kill the isolate between a turn's Started and terminal events with no
|
|
79
|
+
// teardown. The write-ahead execution claim plus ServerFetch.make's boot-time resume
|
|
80
|
+
// recovers such orphaned turns by replaying the drain from durable history on the next wake.
|
|
81
|
+
const server = yield* ServerFetch.makeWithContext(serverOptions(options), {
|
|
82
|
+
overrides: replacements(options),
|
|
83
|
+
})
|
|
84
|
+
const run = Effect.runPromiseWith(yield* withoutParentSpan(Effect.context()))
|
|
85
|
+
const workspace = Context.get(server.context, Workspace.Service)
|
|
86
|
+
const handler: Handler = Object.assign(server.handler, {
|
|
87
|
+
alarm: () => run(Effect.asVoid(workspace.suspendIdle())),
|
|
88
|
+
})
|
|
89
|
+
return handler
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
export function serverOptions(options: Options): ServerOptions {
|
|
93
|
+
return {
|
|
94
|
+
app: options.app,
|
|
95
|
+
password: options.password,
|
|
96
|
+
fs: { filewatcher: false, fff: false },
|
|
97
|
+
// Durable event history is how a turn orphaned by eviction is recovered:
|
|
98
|
+
// the boot-time resume replays it. A runtime that dies without teardown
|
|
99
|
+
// cannot opt out of it, so this is not exposed as an option.
|
|
100
|
+
events: { persist: true },
|
|
101
|
+
config: { content: options.config?.content },
|
|
102
|
+
models: options.models,
|
|
103
|
+
fetch: options.fetch,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The workerd replacement graph, applied after the standard server replacements. */
|
|
108
|
+
export function replacements(options: Options): LayerNode.Replacements {
|
|
109
|
+
return [
|
|
110
|
+
Database.node.replace(Database.configuredClient(sqliteLayer({ storage: options.storage }))),
|
|
111
|
+
Identity.node.replace(Identity.kvNode),
|
|
112
|
+
Workspace.node.replace(
|
|
113
|
+
Workspace.configured({
|
|
114
|
+
keepWarm: 0,
|
|
115
|
+
pollInterval: null,
|
|
116
|
+
onIdleScheduled: (atMs) => Effect.tryPromise(() => options.storage.setAlarm(atMs)),
|
|
117
|
+
}),
|
|
118
|
+
),
|
|
119
|
+
CrossSpawnSpawner.node.replace(EnvironmentUnavailable.layer),
|
|
120
|
+
Snapshot.node.replace(Snapshot.noopLayer),
|
|
121
|
+
Vcs.node.replace(vcsLayer),
|
|
122
|
+
Pty.node.replace(ptyLayer),
|
|
123
|
+
Npm.node.replace(npmLayer),
|
|
124
|
+
// Blobs need a local `blobs/` folder, which a Durable Object doesn't have.
|
|
125
|
+
BlobStore.node.replace(BlobStore.unsupportedLayer),
|
|
126
|
+
VideoGeneration.node.replace(VideoGeneration.unsupportedLayer),
|
|
127
|
+
// Precompiled (internal and SDK) plugins only: no plugin-directory scan, npm
|
|
128
|
+
// install, or import of plugin code from disk.
|
|
129
|
+
ConfigPluginSource.node.replace(ConfigPluginSource.empty),
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const unavailable = (what: string) => Effect.die(new Error(`${what} is unavailable in the workerd profile`))
|
|
134
|
+
|
|
135
|
+
const npmUnavailable = (what: string, pkg: string) =>
|
|
136
|
+
Effect.fail(
|
|
137
|
+
new Npm.InstallFailedError({
|
|
138
|
+
add: [pkg],
|
|
139
|
+
dir: "",
|
|
140
|
+
cause: new Error(`${what} is unavailable in the workerd profile`),
|
|
141
|
+
}),
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
const npmLayer = Layer.succeed(
|
|
145
|
+
Npm.Service,
|
|
146
|
+
Npm.Service.of({
|
|
147
|
+
add: (pkg) => npmUnavailable("Npm.add", pkg),
|
|
148
|
+
resolve: (name) => Effect.succeed({ directory: "", name }),
|
|
149
|
+
check: () => Effect.succeed(false),
|
|
150
|
+
update: (pkg) => npmUnavailable("Npm.update", pkg),
|
|
151
|
+
follows: () => Effect.succeed(false),
|
|
152
|
+
reject: () => Effect.succeed(false),
|
|
153
|
+
which: () => Effect.undefined,
|
|
154
|
+
}),
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
// Vcs degrades to empty results, matching its behavior for locations without a
|
|
158
|
+
// supported VCS, so read-only clients never need to special-case this runtime.
|
|
159
|
+
const vcsLayer = Layer.succeed(
|
|
160
|
+
Vcs.Service,
|
|
161
|
+
Vcs.Service.of({
|
|
162
|
+
base: () => Effect.succeed(null),
|
|
163
|
+
initialize: () => Effect.fail(new Vcs.InitializeError({ kind: "unsupported" })),
|
|
164
|
+
transform: () => Effect.succeed({ dispose: Effect.void }),
|
|
165
|
+
reload: () => Effect.void,
|
|
166
|
+
info: () => Effect.succeed({ branch: {} }),
|
|
167
|
+
branches: () => Effect.succeed([]),
|
|
168
|
+
status: () => Effect.succeed([]),
|
|
169
|
+
diff: () => Effect.succeed([]),
|
|
170
|
+
}),
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
const ptyLayer = Layer.succeed(
|
|
174
|
+
Pty.Service,
|
|
175
|
+
Pty.Service.of({
|
|
176
|
+
list: () => Effect.succeed([]),
|
|
177
|
+
get: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
178
|
+
create: () => unavailable("Pty.create"),
|
|
179
|
+
update: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
180
|
+
remove: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
181
|
+
write: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
182
|
+
attach: (ptyID) => Effect.fail(new Pty.NotFoundError({ ptyID })),
|
|
183
|
+
}),
|
|
184
|
+
)
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
const fs = require("node:fs")
|
|
2
|
-
const readline = require("node:readline")
|
|
3
|
-
|
|
4
|
-
fs.appendFileSync(process.argv[2], `${process.pid}\n`)
|
|
5
|
-
readline
|
|
6
|
-
.createInterface({ input: process.stdin })
|
|
7
|
-
.on("line", (line) => {
|
|
8
|
-
const request = JSON.parse(line)
|
|
9
|
-
if (request.id === undefined) return
|
|
10
|
-
const result =
|
|
11
|
-
request.method === "initialize"
|
|
12
|
-
? {
|
|
13
|
-
protocolVersion: request.params.protocolVersion,
|
|
14
|
-
capabilities: { tools: {} },
|
|
15
|
-
serverInfo: { name: "filesystem-test", version: "1" },
|
|
16
|
-
}
|
|
17
|
-
: { tools: [] }
|
|
18
|
-
process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: request.id, result }) + "\n")
|
|
19
|
-
})
|
|
20
|
-
.on("close", () => process.exit(0))
|
|
1
|
+
const fs = require("node:fs")
|
|
2
|
+
const readline = require("node:readline")
|
|
3
|
+
|
|
4
|
+
fs.appendFileSync(process.argv[2], `${process.pid}\n`)
|
|
5
|
+
readline
|
|
6
|
+
.createInterface({ input: process.stdin })
|
|
7
|
+
.on("line", (line) => {
|
|
8
|
+
const request = JSON.parse(line)
|
|
9
|
+
if (request.id === undefined) return
|
|
10
|
+
const result =
|
|
11
|
+
request.method === "initialize"
|
|
12
|
+
? {
|
|
13
|
+
protocolVersion: request.params.protocolVersion,
|
|
14
|
+
capabilities: { tools: {} },
|
|
15
|
+
serverInfo: { name: "filesystem-test", version: "1" },
|
|
16
|
+
}
|
|
17
|
+
: { tools: [] }
|
|
18
|
+
process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id: request.id, result }) + "\n")
|
|
19
|
+
})
|
|
20
|
+
.on("close", () => process.exit(0))
|
package/test/fixture/routes.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
2
|
-
import { Plugin } from "@zyro-sec/core/plugin"
|
|
3
|
-
import { Location } from "@zyro-sec/schema/location"
|
|
4
|
-
import { AbsolutePath } from "@zyro-sec/schema/schema"
|
|
5
|
-
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
6
|
-
import { Context, Effect, Layer } from "effect"
|
|
7
|
-
import { HttpEffect, HttpRouter, HttpServer } from "effect/http"
|
|
8
|
-
import type { ServerOptions } from "../../src/options"
|
|
9
|
-
import { createRoutes } from "../../src/routes"
|
|
10
|
-
|
|
11
|
-
// The server never waits for a Location's plugins to load. A test that sets up a Location does, so it can
|
|
12
|
-
// exercise plugin-provided entries without polling.
|
|
13
|
-
export const pluginsLoaded = (context: Context.Context<LocationServiceMap.Service>, directory: string) =>
|
|
14
|
-
Plugin.awaitActivation.pipe(
|
|
15
|
-
Effect.provide(
|
|
16
|
-
Context.get(context, LocationServiceMap.Service).get(
|
|
17
|
-
Location.Ref.make({ directory: AbsolutePath.make(directory) }),
|
|
18
|
-
),
|
|
19
|
-
),
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
// Serves the routes in this process so the test can reach the Location's plugins.
|
|
23
|
-
export const serveRoutes = Effect.fnUntraced(function* (
|
|
24
|
-
options: ServerOptions,
|
|
25
|
-
overrides: LayerNode.Replacements = [],
|
|
26
|
-
) {
|
|
27
|
-
const context = yield* Layer.build(
|
|
28
|
-
createRoutes(options, () => [], overrides).pipe(Layer.provide(HttpServer.layerServices)),
|
|
29
|
-
)
|
|
30
|
-
const handler = Context.get(context, HttpRouter.HttpRouter).asHttpEffect().pipe(HttpEffect.toWebHandlerWith(context))
|
|
31
|
-
return {
|
|
32
|
-
context,
|
|
33
|
-
handler,
|
|
34
|
-
fetch: (input: string | URL, init?: RequestInit) => handler(new Request(input, init)),
|
|
35
|
-
pluginsLoaded: (directory: string) => pluginsLoaded(context, directory),
|
|
36
|
-
}
|
|
37
|
-
})
|
|
1
|
+
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
2
|
+
import { Plugin } from "@zyro-sec/core/plugin"
|
|
3
|
+
import { Location } from "@zyro-sec/schema/location"
|
|
4
|
+
import { AbsolutePath } from "@zyro-sec/schema/schema"
|
|
5
|
+
import type { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
6
|
+
import { Context, Effect, Layer } from "effect"
|
|
7
|
+
import { HttpEffect, HttpRouter, HttpServer } from "effect/http"
|
|
8
|
+
import type { ServerOptions } from "../../src/options"
|
|
9
|
+
import { createRoutes } from "../../src/routes"
|
|
10
|
+
|
|
11
|
+
// The server never waits for a Location's plugins to load. A test that sets up a Location does, so it can
|
|
12
|
+
// exercise plugin-provided entries without polling.
|
|
13
|
+
export const pluginsLoaded = (context: Context.Context<LocationServiceMap.Service>, directory: string) =>
|
|
14
|
+
Plugin.awaitActivation.pipe(
|
|
15
|
+
Effect.provide(
|
|
16
|
+
Context.get(context, LocationServiceMap.Service).get(
|
|
17
|
+
Location.Ref.make({ directory: AbsolutePath.make(directory) }),
|
|
18
|
+
),
|
|
19
|
+
),
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
// Serves the routes in this process so the test can reach the Location's plugins.
|
|
23
|
+
export const serveRoutes = Effect.fnUntraced(function* (
|
|
24
|
+
options: ServerOptions,
|
|
25
|
+
overrides: LayerNode.Replacements = [],
|
|
26
|
+
) {
|
|
27
|
+
const context = yield* Layer.build(
|
|
28
|
+
createRoutes(options, () => [], overrides).pipe(Layer.provide(HttpServer.layerServices)),
|
|
29
|
+
)
|
|
30
|
+
const handler = Context.get(context, HttpRouter.HttpRouter).asHttpEffect().pipe(HttpEffect.toWebHandlerWith(context))
|
|
31
|
+
return {
|
|
32
|
+
context,
|
|
33
|
+
handler,
|
|
34
|
+
fetch: (input: string | URL, init?: RequestInit) => handler(new Request(input, init)),
|
|
35
|
+
pluginsLoaded: (directory: string) => pluginsLoaded(context, directory),
|
|
36
|
+
}
|
|
37
|
+
})
|
package/test/fixture/server.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { Effect } from "effect"
|
|
2
|
-
import { HttpServer } from "effect/http"
|
|
3
|
-
import { ServerProcess } from "../../src/process"
|
|
4
|
-
|
|
5
|
-
export const startServer = Effect.fnUntraced(function* (directory: string) {
|
|
6
|
-
const server = yield* ServerProcess.start<never, never>({
|
|
7
|
-
hostname: "127.0.0.1",
|
|
8
|
-
port: 0,
|
|
9
|
-
password: "secret",
|
|
10
|
-
app: { version: "test-version" },
|
|
11
|
-
database: { path: ":memory:" },
|
|
12
|
-
config: { directory },
|
|
13
|
-
fs: { filewatcher: false },
|
|
14
|
-
models: { fetch: false },
|
|
15
|
-
})
|
|
16
|
-
return {
|
|
17
|
-
base: HttpServer.formatAddress(server.address),
|
|
18
|
-
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
|
|
19
|
-
}
|
|
20
|
-
})
|
|
1
|
+
import { Effect } from "effect"
|
|
2
|
+
import { HttpServer } from "effect/http"
|
|
3
|
+
import { ServerProcess } from "../../src/process"
|
|
4
|
+
|
|
5
|
+
export const startServer = Effect.fnUntraced(function* (directory: string) {
|
|
6
|
+
const server = yield* ServerProcess.start<never, never>({
|
|
7
|
+
hostname: "127.0.0.1",
|
|
8
|
+
port: 0,
|
|
9
|
+
password: "secret",
|
|
10
|
+
app: { version: "test-version" },
|
|
11
|
+
database: { path: ":memory:" },
|
|
12
|
+
config: { directory },
|
|
13
|
+
fs: { filewatcher: false },
|
|
14
|
+
models: { fetch: false },
|
|
15
|
+
})
|
|
16
|
+
return {
|
|
17
|
+
base: HttpServer.formatAddress(server.address),
|
|
18
|
+
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
|
|
19
|
+
}
|
|
20
|
+
})
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Plugin } from "@zyro-sec/plugin"
|
|
2
|
-
|
|
3
|
-
export default Plugin.define({
|
|
4
|
-
id: "test.worktree-delegate",
|
|
5
|
-
async setup(ctx) {
|
|
6
|
-
const projectID = ctx.options.projectID ?? ctx.location.project.id
|
|
7
|
-
if (typeof projectID !== "string") throw new Error("Missing target project")
|
|
8
|
-
await ctx.worktree.create({
|
|
9
|
-
projectID,
|
|
10
|
-
name: "delegated",
|
|
11
|
-
})
|
|
12
|
-
},
|
|
13
|
-
})
|
|
1
|
+
import { Plugin } from "@zyro-sec/plugin"
|
|
2
|
+
|
|
3
|
+
export default Plugin.define({
|
|
4
|
+
id: "test.worktree-delegate",
|
|
5
|
+
async setup(ctx) {
|
|
6
|
+
const projectID = ctx.options.projectID ?? ctx.location.project.id
|
|
7
|
+
if (typeof projectID !== "string") throw new Error("Missing target project")
|
|
8
|
+
await ctx.worktree.create({
|
|
9
|
+
projectID,
|
|
10
|
+
name: "delegated",
|
|
11
|
+
})
|
|
12
|
+
},
|
|
13
|
+
})
|