@zyro-sec/server 1.0.2 → 1.0.4
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
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { expect } from "bun:test"
|
|
2
|
-
import fs from "node:fs/promises"
|
|
3
|
-
import path from "node:path"
|
|
4
|
-
import { CodeModeApi } from "@zyro-sec/core/codemode/api"
|
|
5
|
-
import { CodeModeTool } from "@zyro-sec/core/codemode/tool"
|
|
6
|
-
import { Location } from "@zyro-sec/core/location"
|
|
7
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
8
|
-
import { Permission } from "@zyro-sec/core/permission"
|
|
9
|
-
import { AbsolutePath } from "@zyro-sec/core/schema"
|
|
10
|
-
import { Tool } from "@zyro-sec/core/tool"
|
|
11
|
-
import { Agent } from "@zyro-sec/schema/agent"
|
|
12
|
-
import { Session } from "@zyro-sec/schema/session"
|
|
13
|
-
import { SessionMessage } from "@zyro-sec/schema/session-message"
|
|
14
|
-
import { Context, Effect, Layer, Schema } from "effect"
|
|
15
|
-
import { HttpEffect, HttpRouter, HttpServer } from "effect/http"
|
|
16
|
-
import { tmpdir } from "../../core/test/fixture/tmpdir"
|
|
17
|
-
import { it } from "../../core/test/lib/effect"
|
|
18
|
-
import { createRoutes } from "../src/routes"
|
|
19
|
-
|
|
20
|
-
it.live("sends Code Mode opencode:// requests to the server's own router with the server's credential", () =>
|
|
21
|
-
Effect.gen(function* () {
|
|
22
|
-
const root = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-codemode-api-")))
|
|
23
|
-
const directory = path.join(root.path, "project")
|
|
24
|
-
yield* Effect.promise(() => fs.mkdir(directory))
|
|
25
|
-
const app = yield* Layer.build(
|
|
26
|
-
createRoutes({
|
|
27
|
-
app: { version: "test-version" },
|
|
28
|
-
database: { path: ":memory:" },
|
|
29
|
-
config: { project: false, directory: root.path },
|
|
30
|
-
models: { fetch: false },
|
|
31
|
-
fs: { filewatcher: false },
|
|
32
|
-
password: "secret",
|
|
33
|
-
}).pipe(Layer.provide(HttpServer.layerServices)),
|
|
34
|
-
)
|
|
35
|
-
const handler = Context.get(app, HttpRouter.HttpRouter).asHttpEffect().pipe(HttpEffect.toWebHandlerWith(app))
|
|
36
|
-
expect((yield* Effect.promise(() => handler(new Request("http://opencode.local/api/info")))).status).toBe(401)
|
|
37
|
-
|
|
38
|
-
const created = yield* Effect.promise(() =>
|
|
39
|
-
handler(
|
|
40
|
-
new Request("http://opencode.local/api/session", {
|
|
41
|
-
method: "POST",
|
|
42
|
-
headers: { "content-type": "application/json", authorization: `Basic ${btoa("opencode:secret")}` },
|
|
43
|
-
// opencode:// asks as the opencode api command, so the session allows it without a prompt.
|
|
44
|
-
body: JSON.stringify({
|
|
45
|
-
location: { directory },
|
|
46
|
-
permissions: [{ action: "*", resource: "*", effect: "allow" }],
|
|
47
|
-
}),
|
|
48
|
-
}),
|
|
49
|
-
).then((response) => response.json()),
|
|
50
|
-
)
|
|
51
|
-
const parent = Schema.decodeUnknownSync(Schema.Struct({ data: Session.Info }))(created).data
|
|
52
|
-
const locations = Context.get(app, LocationServiceMap.Service)
|
|
53
|
-
const result = yield* Effect.gen(function* () {
|
|
54
|
-
const permission = yield* Permission.Service
|
|
55
|
-
const api = Context.make(CodeModeApi.Service, Context.get(app, CodeModeApi.Service)).pipe(
|
|
56
|
-
Context.add(Location.Service, yield* Location.Service),
|
|
57
|
-
)
|
|
58
|
-
return yield* CodeModeTool.create(
|
|
59
|
-
{ tools: new Map() },
|
|
60
|
-
() => Effect.die("The program calls no tools."),
|
|
61
|
-
permission,
|
|
62
|
-
undefined,
|
|
63
|
-
api,
|
|
64
|
-
).execute(
|
|
65
|
-
{
|
|
66
|
-
code: `const info = await (await fetch("opencode:///api/info")).json()
|
|
67
|
-
const location = await (await fetch("opencode:///api/location")).json()
|
|
68
|
-
const child = await (await fetch("opencode:///api/session", {
|
|
69
|
-
method: "POST",
|
|
70
|
-
headers: { "content-type": "application/json" },
|
|
71
|
-
body: JSON.stringify({ title: "child", parentID: (await (await fetch("opencode:///api/session/current")).json()).data.id }),
|
|
72
|
-
})).json()
|
|
73
|
-
return [info.version, location.directory, child.data.title, child.data.parentID]`,
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
sessionID: parent.id,
|
|
77
|
-
agent: Agent.ID.make("build"),
|
|
78
|
-
messageID: SessionMessage.ID.create(),
|
|
79
|
-
id: Tool.CallID.make("call_opencode_api"),
|
|
80
|
-
progress: () => Effect.void,
|
|
81
|
-
},
|
|
82
|
-
)
|
|
83
|
-
}).pipe(Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory) }))))
|
|
84
|
-
|
|
85
|
-
expect(result.output.error, result.output.output).toBeUndefined()
|
|
86
|
-
expect(JSON.parse(result.output.output)).toEqual(["test-version", directory, "child", parent.id])
|
|
87
|
-
}),
|
|
88
|
-
)
|
package/test/config.test.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs/promises"
|
|
2
|
-
import path from "node:path"
|
|
3
|
-
import { expect } from "bun:test"
|
|
4
|
-
import { Config } from "@zyro-sec/schema/config"
|
|
5
|
-
import { Effect, Schema } from "effect"
|
|
6
|
-
import { tmpdir } from "../../core/test/fixture/tmpdir"
|
|
7
|
-
import { it } from "../../core/test/lib/effect"
|
|
8
|
-
import { startServer } from "./fixture/server"
|
|
9
|
-
import { AbsolutePath } from "@zyro-sec/schema/schema"
|
|
10
|
-
|
|
11
|
-
it.live("returns ordered config entries for the requested directory", () =>
|
|
12
|
-
Effect.gen(function* () {
|
|
13
|
-
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-config-endpoint-")))
|
|
14
|
-
const global = path.join(tmp.path, "global")
|
|
15
|
-
const project = path.join(tmp.path, "project")
|
|
16
|
-
const config = path.join(project, "opencode.json")
|
|
17
|
-
yield* Effect.promise(() =>
|
|
18
|
-
Promise.all([fs.mkdir(global, { recursive: true }), fs.mkdir(project, { recursive: true })]),
|
|
19
|
-
)
|
|
20
|
-
yield* Effect.promise(() =>
|
|
21
|
-
fs.writeFile(
|
|
22
|
-
config,
|
|
23
|
-
JSON.stringify({
|
|
24
|
-
permissions: [
|
|
25
|
-
{ action: "shell", resource: "*", effect: "ask" },
|
|
26
|
-
{ action: "shell", resource: "git status", effect: "allow" },
|
|
27
|
-
],
|
|
28
|
-
mcp: { servers: { docs: { type: "remote", url: "https://example.com/mcp" } } },
|
|
29
|
-
}),
|
|
30
|
-
),
|
|
31
|
-
)
|
|
32
|
-
const server = yield* startServer(global)
|
|
33
|
-
const url = new URL("/api/config", server.base)
|
|
34
|
-
url.searchParams.set("location[directory]", project)
|
|
35
|
-
const response = yield* Effect.promise(() => fetch(url, { headers: server.headers }))
|
|
36
|
-
const body: unknown = yield* Effect.promise(() => response.json())
|
|
37
|
-
const entries = Schema.decodeUnknownSync(Schema.Array(Config.Entry))(body)
|
|
38
|
-
|
|
39
|
-
expect(response.status).toBe(200)
|
|
40
|
-
expect(Array.isArray(entries)).toBe(true)
|
|
41
|
-
const document = entries.find(
|
|
42
|
-
(entry): entry is Config.Document => entry.type === "document" && entry.path === config,
|
|
43
|
-
)
|
|
44
|
-
expect(document?.info.permissions).toEqual([
|
|
45
|
-
{ action: "shell", resource: "*", effect: "ask" },
|
|
46
|
-
{ action: "shell", resource: "git status", effect: "allow" },
|
|
47
|
-
])
|
|
48
|
-
expect(document?.path).toBe(AbsolutePath.make(config))
|
|
49
|
-
if (!Array.isArray(body)) throw new Error("Expected a config entry array")
|
|
50
|
-
const raw = body.find((entry) => isRecord(entry) && entry["type"] === "document" && entry["path"] === config)
|
|
51
|
-
if (!isRecord(raw) || !isRecord(raw["info"])) throw new Error("Expected a config document")
|
|
52
|
-
expect(raw["info"]).not.toHaveProperty("default_agent")
|
|
53
|
-
expect(raw["info"]).not.toHaveProperty("model")
|
|
54
|
-
const mcp = raw["info"]["mcp"]
|
|
55
|
-
if (!isRecord(mcp) || !isRecord(mcp["servers"]) || !isRecord(mcp["servers"]["docs"]))
|
|
56
|
-
throw new Error("Expected an MCP server config")
|
|
57
|
-
expect(mcp["servers"]["docs"]).not.toHaveProperty("headers")
|
|
58
|
-
expect(mcp["servers"]["docs"]).not.toHaveProperty("oauth")
|
|
59
|
-
}),
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
it.live("updates the global shell without replacing unrelated JSONC", () =>
|
|
63
|
-
Effect.gen(function* () {
|
|
64
|
-
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-config-shells-")))
|
|
65
|
-
const global = path.join(tmp.path, "global")
|
|
66
|
-
const config = path.join(global, "opencode.jsonc")
|
|
67
|
-
yield* Effect.promise(() => fs.mkdir(global, { recursive: true }))
|
|
68
|
-
yield* Effect.promise(() =>
|
|
69
|
-
fs.writeFile(
|
|
70
|
-
config,
|
|
71
|
-
`{
|
|
72
|
-
// keep this comment
|
|
73
|
-
"model": "provider/model",
|
|
74
|
-
"shell": "bash"
|
|
75
|
-
}
|
|
76
|
-
`,
|
|
77
|
-
),
|
|
78
|
-
)
|
|
79
|
-
const server = yield* startServer(global)
|
|
80
|
-
const response = yield* Effect.promise(() =>
|
|
81
|
-
fetch(new URL("/api/experimental/config", server.base), {
|
|
82
|
-
method: "PATCH",
|
|
83
|
-
headers: { ...server.headers, "content-type": "application/json" },
|
|
84
|
-
body: JSON.stringify({ shell: "/bin/zsh" }),
|
|
85
|
-
}),
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
expect(response.status).toBe(204)
|
|
89
|
-
const text = yield* Effect.promise(() => fs.readFile(config, "utf8"))
|
|
90
|
-
expect(text).toContain("// keep this comment")
|
|
91
|
-
expect(text).toContain('"model": "provider/model"')
|
|
92
|
-
expect(text).toContain('"shell": "/bin/zsh"')
|
|
93
|
-
|
|
94
|
-
const shells = yield* Effect.promise(() =>
|
|
95
|
-
fetch(new URL("/api/config/shell", server.base), { headers: server.headers }),
|
|
96
|
-
)
|
|
97
|
-
expect(shells.status).toBe(200)
|
|
98
|
-
expect(Array.isArray(yield* Effect.promise(() => shells.json()))).toBe(true)
|
|
99
|
-
}),
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
103
|
-
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
104
|
-
}
|
package/test/cors.test.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "bun:test"
|
|
2
|
-
import { isAllowedCorsOrigin, isAllowedRequestOrigin } from "../src/cors"
|
|
3
|
-
|
|
4
|
-
test("custom origins extend the defaults without allowing other origins", () => {
|
|
5
|
-
const options = { cors: ["http://192.168.1.10:3001", "https://example.com"] }
|
|
6
|
-
expect(isAllowedCorsOrigin("http://192.168.1.10:3001")).toBe(false)
|
|
7
|
-
expect(isAllowedCorsOrigin("http://192.168.1.10:3001", options)).toBe(true)
|
|
8
|
-
expect(isAllowedCorsOrigin("https://example.com", options)).toBe(true)
|
|
9
|
-
expect(isAllowedCorsOrigin("http://localhost:3001", options)).toBe(true)
|
|
10
|
-
expect(isAllowedCorsOrigin("https://app.opencode.ai", options)).toBe(true)
|
|
11
|
-
expect(isAllowedCorsOrigin(undefined, options)).toBe(true)
|
|
12
|
-
expect(isAllowedCorsOrigin("http://192.168.1.10:3002", options)).toBe(false)
|
|
13
|
-
expect(isAllowedCorsOrigin("https://example.com.evil.test", options)).toBe(false)
|
|
14
|
-
expect(isAllowedCorsOrigin("http://example.com", options)).toBe(false)
|
|
15
|
-
expect(isAllowedCorsOrigin("null", options)).toBe(false)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
test("PTY origin checks use the same allowlist and retain same-host access", () => {
|
|
19
|
-
const options = { cors: ["http://192.168.1.10:3001"] }
|
|
20
|
-
expect(isAllowedRequestOrigin("http://192.168.1.10:3001", "192.168.1.10:1029")).toBe(false)
|
|
21
|
-
expect(isAllowedRequestOrigin("http://192.168.1.10:3001", "192.168.1.10:1029", options)).toBe(true)
|
|
22
|
-
expect(isAllowedRequestOrigin("http://192.168.1.10:1029", "192.168.1.10:1029", options)).toBe(true)
|
|
23
|
-
expect(isAllowedRequestOrigin("http://192.168.1.10:3002", "192.168.1.10:1029", options)).toBe(false)
|
|
24
|
-
})
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import path from "node:path"
|
|
2
|
-
import { expect } from "bun:test"
|
|
3
|
-
import { OpenCode } from "@zyro-sec/client"
|
|
4
|
-
import { Database } from "@zyro-sec/core/database/database"
|
|
5
|
-
import { Global } from "@zyro-sec/util/global"
|
|
6
|
-
import { Context, Effect } from "effect"
|
|
7
|
-
import { tempGlobalLayer } from "../../core/test/fixture/global"
|
|
8
|
-
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
9
|
-
import { it } from "../../core/test/lib/effect"
|
|
10
|
-
import { serveRoutes } from "./fixture/routes"
|
|
11
|
-
|
|
12
|
-
const large = "x".repeat(60_000)
|
|
13
|
-
|
|
14
|
-
const boot = Effect.gen(function* () {
|
|
15
|
-
const dir = yield* tmpdirScoped("opencode-defect-response-")
|
|
16
|
-
const server = yield* serveRoutes(
|
|
17
|
-
{
|
|
18
|
-
app: { version: "test" },
|
|
19
|
-
database: { path: path.join(dir.path, "opencode.db") },
|
|
20
|
-
models: { fetch: false },
|
|
21
|
-
fs: { filewatcher: false },
|
|
22
|
-
},
|
|
23
|
-
[Global.node.replace(tempGlobalLayer)],
|
|
24
|
-
)
|
|
25
|
-
const db = Context.get(server.context, Database.Service).db
|
|
26
|
-
return {
|
|
27
|
-
db,
|
|
28
|
-
location: { directory: dir.path },
|
|
29
|
-
request: (method: string, route: string, body?: unknown) =>
|
|
30
|
-
Effect.promise(async () => {
|
|
31
|
-
const response = await server.fetch(`http://opencode.local${route}`, {
|
|
32
|
-
method,
|
|
33
|
-
headers: { "content-type": "application/json" },
|
|
34
|
-
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
35
|
-
})
|
|
36
|
-
return { status: response.status, body: await response.text() }
|
|
37
|
-
}),
|
|
38
|
-
api: OpenCode.make({
|
|
39
|
-
baseUrl: "http://opencode.local",
|
|
40
|
-
fetch: Object.assign(
|
|
41
|
-
(input: string | URL | Request, init?: RequestInit) => server.handler(new Request(input, init)),
|
|
42
|
-
{ preconnect: fetch.preconnect },
|
|
43
|
-
),
|
|
44
|
-
}),
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
it.live("a write on a full disk answers 500 with the storage reason instead of an empty body", () =>
|
|
49
|
-
Effect.gen(function* () {
|
|
50
|
-
const server = yield* boot
|
|
51
|
-
const session = yield* Effect.promise(() => server.api.session.create({ location: server.location }))
|
|
52
|
-
// A real SQLITE_FULL: SQLite never lowers the limit below the current size, so 1 means the database may not grow
|
|
53
|
-
// and a write that needs a new page fails.
|
|
54
|
-
yield* server.db.run("PRAGMA max_page_count = 1").pipe(Effect.orDie)
|
|
55
|
-
|
|
56
|
-
const prompt = yield* server.request("POST", `/api/session/${session.id}/prompt`, { text: large })
|
|
57
|
-
expect(prompt.status).toBe(500)
|
|
58
|
-
expect(JSON.parse(prompt.body)).toEqual({
|
|
59
|
-
_tag: "UnknownError",
|
|
60
|
-
message: "Storage is full",
|
|
61
|
-
ref: expect.stringMatching(/^err_[0-9a-f]{8}$/),
|
|
62
|
-
})
|
|
63
|
-
}),
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
it.live("other defects answer 500 with a ref, and unknown routes keep their 404", () =>
|
|
67
|
-
Effect.gen(function* () {
|
|
68
|
-
const server = yield* boot
|
|
69
|
-
const session = yield* Effect.promise(() => server.api.session.create({ location: server.location }))
|
|
70
|
-
// A query that is simply wrong is a bug, not a storage failure: an empty table shadows the sessions.
|
|
71
|
-
yield* server.db.run("CREATE TEMP TABLE session_v2 (shadow text)").pipe(Effect.orDie)
|
|
72
|
-
const read = yield* server.request("GET", `/api/session/${session.id}`)
|
|
73
|
-
yield* server.db.run("DROP TABLE temp.session_v2").pipe(Effect.orDie)
|
|
74
|
-
expect(read.status).toBe(500)
|
|
75
|
-
expect(JSON.parse(read.body)).toEqual({
|
|
76
|
-
_tag: "UnknownError",
|
|
77
|
-
message: "Server failed to handle the request. Check server logs for details.",
|
|
78
|
-
ref: expect.stringMatching(/^err_[0-9a-f]{8}$/),
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
expect((yield* server.request("GET", "/api/does-not-exist")).status).toBe(404)
|
|
82
|
-
}),
|
|
83
|
-
)
|
package/test/event-feed.test.ts
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test"
|
|
2
|
-
import { Agent } from "@zyro-sec/core/agent"
|
|
3
|
-
import { Bus } from "@zyro-sec/core/bus"
|
|
4
|
-
import { Credential } from "@zyro-sec/schema/credential"
|
|
5
|
-
import { Event } from "@zyro-sec/schema/event"
|
|
6
|
-
import { IntegrationID } from "@zyro-sec/schema/integration-id"
|
|
7
|
-
import { Deferred, Effect, Exit, Fiber, Option, Schema, Stream } from "effect"
|
|
8
|
-
import { it } from "../../core/test/lib/effect"
|
|
9
|
-
import { EventFeed } from "../src/event-feed"
|
|
10
|
-
|
|
11
|
-
const Internal = Bus.ephemeral({ type: "test.internal", schema: { value: Schema.String } })
|
|
12
|
-
|
|
13
|
-
const event = (id: string): Event.Payload<typeof Agent.Event.Updated> => ({
|
|
14
|
-
id: Event.ID.make(`evt_${id}`),
|
|
15
|
-
created: Date.now(),
|
|
16
|
-
type: Agent.Event.Updated.type,
|
|
17
|
-
data: {},
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const internal = (value: string): Event.Payload<typeof Internal> => ({
|
|
21
|
-
id: Event.ID.create(),
|
|
22
|
-
created: Date.now(),
|
|
23
|
-
type: Internal.type,
|
|
24
|
-
data: { value },
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
function makeSource() {
|
|
28
|
-
let subscriber: Bus.Subscriber | undefined
|
|
29
|
-
return {
|
|
30
|
-
observe: (next: Bus.Subscriber) =>
|
|
31
|
-
Effect.sync(() => {
|
|
32
|
-
subscriber = next
|
|
33
|
-
return Effect.sync(() => {
|
|
34
|
-
if (subscriber === next) subscriber = undefined
|
|
35
|
-
})
|
|
36
|
-
}),
|
|
37
|
-
publish: (event: Event.Payload) => Effect.suspend(() => (subscriber ? subscriber(event) : Effect.void)),
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
describe("EventFeed", () => {
|
|
42
|
-
test("preserves the public SSE frame encoding", () => {
|
|
43
|
-
const payload = event("wire")
|
|
44
|
-
expect(EventFeed.frame(payload)).toBe(`data: ${JSON.stringify(payload)}\n\n`)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it.effect("encodes once and delivers the same frame to every subscriber", () =>
|
|
48
|
-
Effect.gen(function* () {
|
|
49
|
-
let encodes = 0
|
|
50
|
-
const source = makeSource()
|
|
51
|
-
const feed = yield* EventFeed.make(source.observe, {
|
|
52
|
-
encode: (event) => {
|
|
53
|
-
encodes += 1
|
|
54
|
-
return event.type
|
|
55
|
-
},
|
|
56
|
-
})
|
|
57
|
-
const first = yield* feed.subscribe()
|
|
58
|
-
const second = yield* feed.subscribe()
|
|
59
|
-
const left = yield* first.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
|
60
|
-
const right = yield* second.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
|
61
|
-
|
|
62
|
-
yield* source.publish(event("example"))
|
|
63
|
-
|
|
64
|
-
expect([Array.from(yield* Fiber.join(left)), Array.from(yield* Fiber.join(right))]).toEqual([
|
|
65
|
-
[Agent.Event.Updated.type],
|
|
66
|
-
[Agent.Event.Updated.type],
|
|
67
|
-
])
|
|
68
|
-
expect(encodes).toBe(1)
|
|
69
|
-
}),
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
it.effect("delivers only the requested event types to filtered subscribers", () =>
|
|
73
|
-
Effect.gen(function* () {
|
|
74
|
-
const source = makeSource()
|
|
75
|
-
const feed = yield* EventFeed.make(source.observe, { encode: (event) => event.type })
|
|
76
|
-
const filtered = yield* feed.subscribe([Credential.Event.Switched.type])
|
|
77
|
-
const all = yield* feed.subscribe()
|
|
78
|
-
const left = yield* filtered.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
|
79
|
-
const right = yield* all.pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
|
|
80
|
-
|
|
81
|
-
yield* source.publish(event("ignored"))
|
|
82
|
-
yield* source.publish({
|
|
83
|
-
id: Event.ID.create(),
|
|
84
|
-
created: Date.now(),
|
|
85
|
-
type: Credential.Event.Switched.type,
|
|
86
|
-
data: {
|
|
87
|
-
credentialID: Credential.ID.make("cred_test"),
|
|
88
|
-
integrationID: IntegrationID.make("openai"),
|
|
89
|
-
},
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
expect(Array.from(yield* Fiber.join(left))).toEqual([Credential.Event.Switched.type])
|
|
93
|
-
expect(Array.from(yield* Fiber.join(right))).toEqual([Agent.Event.Updated.type, Credential.Event.Switched.type])
|
|
94
|
-
}),
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
it.effect("fails only the subscriber that exceeds its lag capacity", () =>
|
|
98
|
-
Effect.gen(function* () {
|
|
99
|
-
const source = makeSource()
|
|
100
|
-
const feed = yield* EventFeed.make(source.observe, {
|
|
101
|
-
capacity: 1,
|
|
102
|
-
encode: (event) => event.id,
|
|
103
|
-
})
|
|
104
|
-
const slow = yield* feed.subscribe()
|
|
105
|
-
const fast = yield* feed.subscribe()
|
|
106
|
-
const first = yield* Deferred.make<void>()
|
|
107
|
-
const second = yield* Deferred.make<void>()
|
|
108
|
-
const received = new Array<string>()
|
|
109
|
-
const fastFiber = yield* fast.pipe(
|
|
110
|
-
Stream.take(3),
|
|
111
|
-
Stream.runForEach((frame) =>
|
|
112
|
-
Effect.sync(() => received.push(frame)).pipe(
|
|
113
|
-
Effect.andThen(
|
|
114
|
-
frame === "evt_one"
|
|
115
|
-
? Deferred.succeed(first, undefined)
|
|
116
|
-
: frame === "evt_two"
|
|
117
|
-
? Deferred.succeed(second, undefined)
|
|
118
|
-
: Effect.void,
|
|
119
|
-
),
|
|
120
|
-
),
|
|
121
|
-
),
|
|
122
|
-
Effect.forkScoped,
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
yield* source.publish(event("one"))
|
|
126
|
-
yield* Deferred.await(first)
|
|
127
|
-
yield* source.publish(event("two"))
|
|
128
|
-
yield* Deferred.await(second)
|
|
129
|
-
yield* source.publish(event("three"))
|
|
130
|
-
|
|
131
|
-
yield* Fiber.join(fastFiber)
|
|
132
|
-
|
|
133
|
-
const result = yield* slow.pipe(Stream.runCollect, Effect.exit)
|
|
134
|
-
expect(received).toEqual(["evt_one", "evt_two", "evt_three"])
|
|
135
|
-
expect(Exit.isFailure(result)).toBeTrue()
|
|
136
|
-
if (Exit.isSuccess(result)) return
|
|
137
|
-
expect(Option.getOrUndefined(Exit.findErrorOption(result))).toBeInstanceOf(EventFeed.SubscriberOverflowError)
|
|
138
|
-
}),
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
it.effect("delivers global credential events to public subscribers", () =>
|
|
142
|
-
Effect.gen(function* () {
|
|
143
|
-
const source = makeSource()
|
|
144
|
-
const feed = yield* EventFeed.make(source.observe, { encode: (event) => event.type })
|
|
145
|
-
const stream = yield* feed.subscribe()
|
|
146
|
-
const received = yield* stream.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
|
147
|
-
|
|
148
|
-
yield* source.publish({
|
|
149
|
-
id: Event.ID.create(),
|
|
150
|
-
created: Date.now(),
|
|
151
|
-
type: Credential.Event.Switched.type,
|
|
152
|
-
data: {
|
|
153
|
-
credentialID: Credential.ID.make("cred_test"),
|
|
154
|
-
integrationID: IntegrationID.make("openai"),
|
|
155
|
-
},
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
expect(Array.from(yield* Fiber.join(received))).toEqual([Credential.Event.Switched.type])
|
|
159
|
-
}),
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
it.effect("filters internal events before they consume subscriber capacity", () =>
|
|
163
|
-
Effect.gen(function* () {
|
|
164
|
-
const source = makeSource()
|
|
165
|
-
const feed = yield* EventFeed.make(source.observe, { capacity: 1, encode: (event) => event.type })
|
|
166
|
-
const stream = yield* feed.subscribe()
|
|
167
|
-
|
|
168
|
-
yield* source.publish(internal("one"))
|
|
169
|
-
yield* source.publish(internal("two"))
|
|
170
|
-
yield* source.publish(event("public"))
|
|
171
|
-
|
|
172
|
-
expect(Array.from(yield* stream.pipe(Stream.take(1), Stream.runCollect))).toEqual([Agent.Event.Updated.type])
|
|
173
|
-
}),
|
|
174
|
-
)
|
|
175
|
-
|
|
176
|
-
it.effect("disconnects current subscribers after an encoding failure and continues for later subscribers", () =>
|
|
177
|
-
Effect.gen(function* () {
|
|
178
|
-
const source = makeSource()
|
|
179
|
-
const feed = yield* EventFeed.make(source.observe, {
|
|
180
|
-
encode: (event) => {
|
|
181
|
-
if (event.id === Event.ID.make("evt_bad")) throw new Error("invalid event")
|
|
182
|
-
return event.id
|
|
183
|
-
},
|
|
184
|
-
})
|
|
185
|
-
const current = yield* feed.subscribe()
|
|
186
|
-
const failed = yield* current.pipe(Stream.runCollect, Effect.exit, Effect.forkScoped)
|
|
187
|
-
|
|
188
|
-
yield* source.publish(event("bad"))
|
|
189
|
-
const exit = yield* Fiber.join(failed)
|
|
190
|
-
|
|
191
|
-
const next = yield* feed.subscribe()
|
|
192
|
-
const received = yield* next.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
|
193
|
-
yield* source.publish(event("good"))
|
|
194
|
-
|
|
195
|
-
expect(Exit.isFailure(exit)).toBeTrue()
|
|
196
|
-
if (Exit.isSuccess(exit)) return
|
|
197
|
-
expect(Option.getOrUndefined(Exit.findErrorOption(exit))).toBeInstanceOf(EventFeed.EncodingError)
|
|
198
|
-
expect(Array.from(yield* Fiber.join(received))).toEqual(["evt_good"])
|
|
199
|
-
}),
|
|
200
|
-
)
|
|
201
|
-
})
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { expect } from "bun:test"
|
|
2
|
-
import { Location } from "@zyro-sec/core/location"
|
|
3
|
-
import { LocationServiceMap } from "@zyro-sec/core/location-service-map"
|
|
4
|
-
import { Mcp } from "@zyro-sec/core/mcp/index"
|
|
5
|
-
import { Context, Effect, Layer } from "effect"
|
|
6
|
-
import { HttpEffect, HttpRouter, HttpServer } from "effect/http"
|
|
7
|
-
import { echoMcpHandler } from "../../core/test/fixture/mcp-echo"
|
|
8
|
-
import { waitUntil } from "../../core/test/lib/eventually"
|
|
9
|
-
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
10
|
-
import { it } from "../../core/test/lib/effect"
|
|
11
|
-
import { createRoutes } from "../src/routes"
|
|
12
|
-
|
|
13
|
-
// `.invalid` never resolves, so only the embedder's fetch can answer these.
|
|
14
|
-
const consoleServer = "https://console.invalid"
|
|
15
|
-
const mcpServer = "https://mcp.invalid/mcp"
|
|
16
|
-
|
|
17
|
-
it.live("sends Console config and remote MCP requests through the embedder's fetch", () =>
|
|
18
|
-
Effect.gen(function* () {
|
|
19
|
-
const tmp = yield* tmpdirScoped("opencode-fetch-option-")
|
|
20
|
-
const requested: string[] = []
|
|
21
|
-
const methods: string[] = []
|
|
22
|
-
const mcp = echoMcpHandler()
|
|
23
|
-
yield* Effect.addFinalizer(() => Effect.promise(() => mcp.close()))
|
|
24
|
-
const fetch = async (input: Request | string | URL, init?: RequestInit) => {
|
|
25
|
-
const request = new Request(input, init)
|
|
26
|
-
const url = new URL(request.url)
|
|
27
|
-
requested.push(`${url.origin}${url.pathname}`)
|
|
28
|
-
if (url.pathname === "/api/v2/config") return Response.json({})
|
|
29
|
-
if (`${url.origin}${url.pathname}` !== mcpServer) return new Response("Not found", { status: 404 })
|
|
30
|
-
const body: unknown = request.method === "POST" ? await request.clone().json() : undefined
|
|
31
|
-
if (typeof body === "object" && body !== null && "method" in body && typeof body.method === "string")
|
|
32
|
-
methods.push(body.method)
|
|
33
|
-
return mcp.fetch(request)
|
|
34
|
-
}
|
|
35
|
-
const context = yield* Layer.build(
|
|
36
|
-
createRoutes({
|
|
37
|
-
password: "secret",
|
|
38
|
-
database: { path: ":memory:" },
|
|
39
|
-
models: { fetch: false },
|
|
40
|
-
fs: { filewatcher: false },
|
|
41
|
-
fetch,
|
|
42
|
-
config: {
|
|
43
|
-
directory: tmp.path,
|
|
44
|
-
project: false,
|
|
45
|
-
content: JSON.stringify({ mcp: { servers: { gateway: { type: "remote", url: mcpServer, oauth: false } } } }),
|
|
46
|
-
},
|
|
47
|
-
}).pipe(Layer.provide(HttpServer.layerServices)),
|
|
48
|
-
)
|
|
49
|
-
const handler = Context.get(context, HttpRouter.HttpRouter)
|
|
50
|
-
.asHttpEffect()
|
|
51
|
-
.pipe(HttpEffect.toWebHandlerWith(context))
|
|
52
|
-
const request = (method: "GET" | "POST", route: string, body?: unknown) =>
|
|
53
|
-
Effect.promise(async (signal) => {
|
|
54
|
-
const url = new URL(route, "http://opencode.local")
|
|
55
|
-
url.searchParams.set("location[directory]", tmp.path)
|
|
56
|
-
const response = await handler(
|
|
57
|
-
new Request(url, {
|
|
58
|
-
method,
|
|
59
|
-
headers: {
|
|
60
|
-
authorization: `Basic ${btoa("opencode:secret")}`,
|
|
61
|
-
...(body === undefined ? {} : { "content-type": "application/json" }),
|
|
62
|
-
},
|
|
63
|
-
body: body === undefined ? undefined : JSON.stringify(body),
|
|
64
|
-
signal,
|
|
65
|
-
}),
|
|
66
|
-
)
|
|
67
|
-
return response.status
|
|
68
|
-
})
|
|
69
|
-
const inLocation = <A, E>(effect: Effect.Effect<A, E, Mcp.Service>) =>
|
|
70
|
-
effect.pipe(
|
|
71
|
-
Effect.provide(
|
|
72
|
-
LocationServiceMap.Service.get(
|
|
73
|
-
LocationServiceMap.canonical(Location.Ref.make({ directory: tmp.path as Location.Ref["directory"] })),
|
|
74
|
-
),
|
|
75
|
-
),
|
|
76
|
-
Effect.provideContext(context),
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
expect(
|
|
80
|
-
yield* request("POST", "/api/credential", {
|
|
81
|
-
integrationID: "opencode",
|
|
82
|
-
value: { type: "key", key: "secret", metadata: { server: consoleServer } },
|
|
83
|
-
}),
|
|
84
|
-
).toBe(200)
|
|
85
|
-
expect(yield* request("GET", "/api/provider")).toBe(200)
|
|
86
|
-
yield* waitUntil(Effect.sync(() => requested.includes(`${consoleServer}/api/v2/config`)))
|
|
87
|
-
|
|
88
|
-
yield* waitUntil(
|
|
89
|
-
inLocation(Mcp.Service.use((service) => service.tools())).pipe(
|
|
90
|
-
Effect.map((tools) => tools.some((tool) => tool.server === "gateway" && tool.name === "echo")),
|
|
91
|
-
),
|
|
92
|
-
)
|
|
93
|
-
const result = yield* inLocation(
|
|
94
|
-
Mcp.Service.use((service) => service.callTool({ server: "gateway", name: "echo" })),
|
|
95
|
-
)
|
|
96
|
-
expect(result.content).toEqual([{ type: "text", text: "called echo" }])
|
|
97
|
-
expect(methods).toEqual(expect.arrayContaining(["server/discover", "tools/list", "tools/call"]))
|
|
98
|
-
|
|
99
|
-
// A reconnect requested over the API starts the connection in the request's fiber rather than the MCP service's.
|
|
100
|
-
methods.length = 0
|
|
101
|
-
expect(yield* request("POST", "/api/experimental/mcp/gateway/connect")).toBe(204)
|
|
102
|
-
expect(methods).toContain("server/discover")
|
|
103
|
-
}),
|
|
104
|
-
)
|