@zyro-sec/server 1.0.0 → 1.0.1
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 +15 -21
- package/script/build.ts +28 -0
- package/script/pack.ts +5 -0
- package/script/workerd-probe.ts +59 -0
- package/src/api.ts +12 -0
- package/src/auth.ts +70 -0
- package/src/content-headers.ts +32 -0
- package/src/cors.ts +34 -0
- package/src/event-feed.ts +93 -0
- package/src/fetch.ts +67 -0
- package/src/handlers/agent.ts +23 -0
- package/src/handlers/artifact.ts +242 -0
- package/src/handlers/automation.ts +63 -0
- package/src/handlers/blob.ts +55 -0
- package/src/handlers/command.ts +8 -0
- package/src/handlers/config.ts +26 -0
- package/src/handlers/credential.ts +87 -0
- package/src/handlers/debug.ts +57 -0
- package/src/handlers/event.ts +38 -0
- package/src/handlers/form.ts +15 -0
- package/src/handlers/fs.ts +180 -0
- package/src/handlers/generate.ts +117 -0
- package/src/handlers/integration.ts +310 -0
- package/src/handlers/job.ts +29 -0
- package/src/handlers/location.ts +33 -0
- package/src/handlers/machine.ts +336 -0
- package/src/handlers/mcp.ts +160 -0
- package/src/handlers/media.ts +44 -0
- package/src/handlers/message.ts +82 -0
- package/src/handlers/migration.ts +13 -0
- package/src/handlers/model.ts +25 -0
- package/src/handlers/permission.ts +131 -0
- package/src/handlers/persistent-pty.ts +285 -0
- package/src/handlers/plugin.ts +97 -0
- package/src/handlers/project.ts +23 -0
- package/src/handlers/provider.ts +32 -0
- package/src/handlers/pty-socket.ts +47 -0
- package/src/handlers/pty.ts +181 -0
- package/src/handlers/push.ts +67 -0
- package/src/handlers/reference.ts +8 -0
- package/src/handlers/rpc.ts +31 -0
- package/src/handlers/server.ts +58 -0
- package/src/handlers/session-error.ts +47 -0
- package/src/handlers/session.ts +722 -0
- package/src/handlers/shell.ts +72 -0
- package/src/handlers/skill.ts +34 -0
- package/src/handlers/usage.ts +23 -0
- package/src/handlers/vcs.ts +104 -0
- package/src/handlers/voice.ts +50 -0
- package/src/handlers/websearch.ts +50 -0
- package/src/handlers/worktree.ts +60 -0
- package/src/handlers.ts +69 -0
- package/src/location.ts +86 -0
- package/src/media-uri.ts +13 -0
- package/src/middleware/authorization.ts +111 -0
- package/src/middleware/defect-response.ts +39 -0
- package/src/middleware/form-location.ts +44 -0
- package/src/middleware/schema-error.ts +49 -0
- package/src/middleware/session-location.ts +30 -0
- package/src/options.ts +61 -0
- package/src/pairing.ts +36 -0
- package/src/process.ts +274 -0
- package/src/range.ts +16 -0
- package/src/request-site.ts +36 -0
- package/src/request-tracing.ts +9 -0
- package/src/routes.ts +261 -0
- package/src/server-info.ts +41 -0
- package/src/service-status.ts +30 -0
- package/src/workerd.ts +184 -0
- package/test/artifact.test.ts +224 -0
- package/test/auth.test.ts +24 -0
- package/test/automation.test.ts +315 -0
- package/test/client-mcp.test.ts +369 -0
- package/test/codemode-api.test.ts +88 -0
- package/test/config.test.ts +104 -0
- package/test/cors.test.ts +24 -0
- package/test/defect-response.test.ts +83 -0
- package/test/event-feed.test.ts +201 -0
- package/test/fetch-option.test.ts +104 -0
- package/test/fetch.test.ts +698 -0
- package/test/fixture/mcp-starts.cjs +20 -0
- package/test/fixture/routes.ts +37 -0
- package/test/fixture/server.ts +20 -0
- package/test/fixture/worktree-delegate/index.ts +13 -0
- package/test/fixture/worktree-plugin/index.ts +47 -0
- package/test/flag-channel.test.ts +126 -0
- package/test/fs.test.ts +439 -0
- package/test/generate.test.ts +132 -0
- package/test/handler-policy.test.ts +43 -0
- package/test/heap-dump.test.ts +137 -0
- package/test/integration-policy.test.ts +93 -0
- package/test/job.test.ts +186 -0
- package/test/machine-iroh.test.ts +259 -0
- package/test/machine-link.test.ts +1234 -0
- package/test/machine-routes.test.ts +198 -0
- package/test/media.test.ts +343 -0
- package/test/message.test.ts +105 -0
- package/test/model.test.ts +62 -0
- package/test/options.test.ts +42 -0
- package/test/permission-saved.test.ts +73 -0
- package/test/persistent-pty.test.ts +1007 -0
- package/test/process.test.ts +261 -0
- package/test/provider.test.ts +88 -0
- package/test/pty-socket.property.test.ts +240 -0
- package/test/pty-socket.test.ts +89 -0
- package/test/pty-transport.test.ts +200 -0
- package/test/push-routes.test.ts +157 -0
- package/test/request-site.test.ts +71 -0
- package/test/request-tracing.test.ts +87 -0
- package/test/rpc-handler-errors.test.ts +76 -0
- package/test/schema-error.test.ts +68 -0
- package/test/service-status.test.ts +34 -0
- package/test/session-admit-location.test.ts +92 -0
- package/test/session-create.test.ts +65 -0
- package/test/session-diff.test.ts +100 -0
- package/test/session-form.test.ts +50 -0
- package/test/session-import.test.ts +152 -0
- package/test/session-instances.test.ts +335 -0
- package/test/session-unarchive.test.ts +73 -0
- package/test/session-update.test.ts +66 -0
- package/test/skill.test.ts +57 -0
- package/test/sse-failure.test.ts +71 -0
- package/test/usage.test.ts +88 -0
- package/test/vcs.test.ts +356 -0
- package/test/video-generate.test.ts +131 -0
- package/test/voice.test.ts +170 -0
- package/test/workerd.test.ts +227 -0
- package/test/worktree.test.ts +333 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { expect } from "bun:test"
|
|
4
|
+
import { OpenCode } from "@zyro-sec/client"
|
|
5
|
+
import { Effect } from "effect"
|
|
6
|
+
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
7
|
+
import { it } from "../../core/test/lib/effect"
|
|
8
|
+
import { serveRoutes } from "./fixture/routes"
|
|
9
|
+
|
|
10
|
+
const client = (handler: (request: Request) => Promise<Response>) =>
|
|
11
|
+
OpenCode.make({
|
|
12
|
+
baseUrl: "http://opencode.local",
|
|
13
|
+
fetch: Object.assign((input: string | URL | Request, init?: RequestInit) => handler(new Request(input, init)), {
|
|
14
|
+
preconnect: fetch.preconnect,
|
|
15
|
+
}),
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it.live("manages local and plugin-backed artifacts through /api/experimental/artifacts when enabled", () =>
|
|
19
|
+
Effect.gen(function* () {
|
|
20
|
+
const dir = (yield* tmpdirScoped("opencode-artifact-api-")).path
|
|
21
|
+
const global = path.join(dir, "global")
|
|
22
|
+
const enabled = path.join(dir, "enabled")
|
|
23
|
+
const enabledOther = path.join(dir, "enabled-other")
|
|
24
|
+
const disabled = path.join(dir, "disabled")
|
|
25
|
+
const pluginDir = path.join(dir, "custom-artifact-plugin")
|
|
26
|
+
|
|
27
|
+
yield* Effect.promise(async () => {
|
|
28
|
+
await Promise.all([global, enabled, enabledOther, disabled, pluginDir].map((item) => fs.mkdir(item, { recursive: true })))
|
|
29
|
+
await fs.writeFile(
|
|
30
|
+
path.join(pluginDir, "package.json"),
|
|
31
|
+
JSON.stringify({ name: "custom-artifact-plugin", type: "module", exports: "./index.js" }),
|
|
32
|
+
)
|
|
33
|
+
await fs.writeFile(
|
|
34
|
+
path.join(pluginDir, "index.js"),
|
|
35
|
+
`
|
|
36
|
+
const store = new Map()
|
|
37
|
+
const decoder = new TextDecoder()
|
|
38
|
+
const encoder = new TextEncoder()
|
|
39
|
+
export default {
|
|
40
|
+
id: "test-s3-artifacts",
|
|
41
|
+
async setup(ctx) {
|
|
42
|
+
await ctx.experimental.artifact.transform((editor) => {
|
|
43
|
+
editor.add({
|
|
44
|
+
id: "s3",
|
|
45
|
+
name: "S3 Static",
|
|
46
|
+
capabilities: ["static"],
|
|
47
|
+
prompt: "Artifacts go to a static S3 bucket.",
|
|
48
|
+
async create(input) {
|
|
49
|
+
store.set(input.id, Object.fromEntries(Object.entries(input.files).map(([k, v]) => [k, decoder.decode(v)])))
|
|
50
|
+
return { url: "https://artifacts.example.test/" + input.id + "/" }
|
|
51
|
+
},
|
|
52
|
+
async read(id) {
|
|
53
|
+
const item = store.get(id) ?? {}
|
|
54
|
+
return Object.fromEntries(Object.entries(item).map(([k, v]) => [k, encoder.encode(v)]))
|
|
55
|
+
},
|
|
56
|
+
async update(id, input) {
|
|
57
|
+
store.set(id, Object.fromEntries(Object.entries(input.files).map(([k, v]) => [k, decoder.decode(v)])))
|
|
58
|
+
return { url: "https://artifacts.example.test/" + id + "/" }
|
|
59
|
+
},
|
|
60
|
+
async delete(id) {
|
|
61
|
+
store.delete(id)
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
`,
|
|
68
|
+
)
|
|
69
|
+
await fs.writeFile(
|
|
70
|
+
path.join(enabled, "opencode.json"),
|
|
71
|
+
JSON.stringify({
|
|
72
|
+
plugins: [{ package: pluginDir }],
|
|
73
|
+
experimental: { artifacts: true },
|
|
74
|
+
}),
|
|
75
|
+
)
|
|
76
|
+
await fs.writeFile(
|
|
77
|
+
path.join(enabledOther, "opencode.json"),
|
|
78
|
+
JSON.stringify({
|
|
79
|
+
experimental: { artifacts: true },
|
|
80
|
+
}),
|
|
81
|
+
)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const server = yield* serveRoutes({
|
|
85
|
+
app: { version: "test" },
|
|
86
|
+
database: { path: path.join(dir, "opencode.db") },
|
|
87
|
+
config: { directory: global, project: true, claude: false },
|
|
88
|
+
models: { fetch: false },
|
|
89
|
+
fs: { filewatcher: false },
|
|
90
|
+
})
|
|
91
|
+
const handler = server.handler
|
|
92
|
+
const api = client(handler)
|
|
93
|
+
yield* server.pluginsLoaded(enabled)
|
|
94
|
+
|
|
95
|
+
yield* Effect.promise(async () => {
|
|
96
|
+
// Disabled location returns ForbiddenError
|
|
97
|
+
await expect(
|
|
98
|
+
api.experimental.artifacts.list({ location: { directory: disabled } }),
|
|
99
|
+
).rejects.toMatchObject({ _tag: "ForbiddenError" })
|
|
100
|
+
|
|
101
|
+
// Providers list includes built-in local (default) and plugin-registered s3
|
|
102
|
+
const providers = await api.experimental.artifacts.providers({ location: { directory: enabled } })
|
|
103
|
+
expect(providers.data).toEqual([
|
|
104
|
+
{ id: "local", name: "Local", capabilities: ["static"], default: true },
|
|
105
|
+
{ id: "s3", name: "S3 Static", capabilities: ["static"], default: false },
|
|
106
|
+
])
|
|
107
|
+
|
|
108
|
+
const session = await api.session.create({ location: { directory: enabled } })
|
|
109
|
+
|
|
110
|
+
// Create local multi-file artifact linked to session
|
|
111
|
+
const created = await api.experimental.artifacts.create({
|
|
112
|
+
location: { directory: enabled },
|
|
113
|
+
sessionID: session.id,
|
|
114
|
+
title: "Wait stats",
|
|
115
|
+
files: {
|
|
116
|
+
"index.html": "<!doctype html><h1>v1</h1><script src='./assets/app.js'></script>",
|
|
117
|
+
"assets/app.js": "window.VERSION = 1",
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
expect(created.data).toMatchObject({
|
|
121
|
+
provider: "local",
|
|
122
|
+
sessionID: session.id,
|
|
123
|
+
title: "Wait stats",
|
|
124
|
+
entry: "index.html",
|
|
125
|
+
url: `/api/experimental/artifacts/${encodeURIComponent(created.data.id)}/view/`,
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// Redirects /view -> /view/ with 308 so relative "./assets/*" resolves
|
|
129
|
+
const redirect = await handler(
|
|
130
|
+
new Request(`http://opencode.local/api/experimental/artifacts/${created.data.id}/view`),
|
|
131
|
+
)
|
|
132
|
+
expect(redirect.status).toBe(308)
|
|
133
|
+
expect(redirect.headers.get("location")).toBe(`/api/experimental/artifacts/${created.data.id}/view/`)
|
|
134
|
+
|
|
135
|
+
// Serves entry HTML and relative JS in a sandboxed opaque origin without requiring location query
|
|
136
|
+
const v1Html = await handler(
|
|
137
|
+
new Request(`http://opencode.local/api/experimental/artifacts/${created.data.id}/view/`),
|
|
138
|
+
)
|
|
139
|
+
expect(v1Html.status).toBe(200)
|
|
140
|
+
expect(v1Html.headers.get("content-type")).toContain("text/html")
|
|
141
|
+
expect(v1Html.headers.get("content-security-policy")).toBe("sandbox allow-scripts")
|
|
142
|
+
expect(await v1Html.text()).toContain("<h1>v1</h1>")
|
|
143
|
+
|
|
144
|
+
// Sessionless SDK artifact also resolves its stored directory at /view/
|
|
145
|
+
const sessionless = await api.experimental.artifacts.create({
|
|
146
|
+
location: { directory: enabled },
|
|
147
|
+
title: "Standalone report",
|
|
148
|
+
files: { "index.html": "<h1>no-session</h1>" },
|
|
149
|
+
})
|
|
150
|
+
const sessionlessHtml = await handler(
|
|
151
|
+
new Request(`http://opencode.local${sessionless.data.url}`),
|
|
152
|
+
)
|
|
153
|
+
expect(sessionlessHtml.status).toBe(200)
|
|
154
|
+
expect(await sessionlessHtml.text()).toContain("<h1>no-session</h1>")
|
|
155
|
+
|
|
156
|
+
// Another enabled project cannot list, read, update, or delete this project's artifacts
|
|
157
|
+
const otherList = await api.experimental.artifacts.list({ location: { directory: enabledOther } })
|
|
158
|
+
expect(otherList.data).toEqual([])
|
|
159
|
+
await expect(
|
|
160
|
+
api.experimental.artifacts.get({ artifactID: created.data.id, location: { directory: enabledOther } }),
|
|
161
|
+
).rejects.toMatchObject({ _tag: "ArtifactNotFoundError" })
|
|
162
|
+
await expect(
|
|
163
|
+
api.experimental.artifacts.delete({ artifactID: created.data.id, location: { directory: enabledOther } }),
|
|
164
|
+
).rejects.toMatchObject({ _tag: "ArtifactNotFoundError" })
|
|
165
|
+
|
|
166
|
+
const v1Js = await handler(
|
|
167
|
+
new Request(`http://opencode.local/api/experimental/artifacts/${created.data.id}/view/assets/app.js`),
|
|
168
|
+
)
|
|
169
|
+
expect(v1Js.status).toBe(200)
|
|
170
|
+
expect(await v1Js.text()).toBe("window.VERSION = 1")
|
|
171
|
+
|
|
172
|
+
// Update replaces files at the same ID and URL
|
|
173
|
+
const updated = await api.experimental.artifacts.update({
|
|
174
|
+
artifactID: created.data.id,
|
|
175
|
+
files: {
|
|
176
|
+
"index.html": "<!doctype html><h1>v2</h1>",
|
|
177
|
+
},
|
|
178
|
+
})
|
|
179
|
+
expect(updated.data.url).toBe(created.data.url)
|
|
180
|
+
|
|
181
|
+
const latestHtml = await handler(
|
|
182
|
+
new Request(`http://opencode.local/api/experimental/artifacts/${created.data.id}/view/`),
|
|
183
|
+
)
|
|
184
|
+
expect(await latestHtml.text()).toContain("<h1>v2</h1>")
|
|
185
|
+
|
|
186
|
+
// Per-request override to plugin-registered "s3" provider
|
|
187
|
+
const remote = await api.experimental.artifacts.create({
|
|
188
|
+
location: { directory: enabled },
|
|
189
|
+
sessionID: session.id,
|
|
190
|
+
provider: "s3",
|
|
191
|
+
title: "Shared bundle",
|
|
192
|
+
files: { "index.html": "<p>remote</p>" },
|
|
193
|
+
})
|
|
194
|
+
expect(remote.data).toMatchObject({
|
|
195
|
+
provider: "s3",
|
|
196
|
+
url: `https://artifacts.example.test/${remote.data.id}/`,
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
const fetchedRemote = await api.experimental.artifacts.get({
|
|
200
|
+
artifactID: remote.data.id,
|
|
201
|
+
location: { directory: enabled },
|
|
202
|
+
})
|
|
203
|
+
expect(fetchedRemote.data.files).toEqual({ "index.html": "<p>remote</p>" })
|
|
204
|
+
|
|
205
|
+
const listedS3 = await api.experimental.artifacts.list({
|
|
206
|
+
sessionID: session.id,
|
|
207
|
+
provider: "s3",
|
|
208
|
+
})
|
|
209
|
+
expect(listedS3.data.map((item) => item.id)).toEqual([remote.data.id])
|
|
210
|
+
|
|
211
|
+
// Delete removes record and returns 404 thereafter
|
|
212
|
+
await api.experimental.artifacts.delete({ artifactID: created.data.id, location: { directory: enabled } })
|
|
213
|
+
await expect(
|
|
214
|
+
api.experimental.artifacts.get({ artifactID: created.data.id, location: { directory: enabled } }),
|
|
215
|
+
).rejects.toMatchObject({
|
|
216
|
+
_tag: "ArtifactNotFoundError",
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
}),
|
|
220
|
+
// As the suite's first server boot, this test pays the process's cold load of the Location graph (built-in plugins,
|
|
221
|
+
// the models catalog, lazily imported modules). sdk's first host boot shows that cost: 0.7-4s on Windows CI against
|
|
222
|
+
// 0.25s on Linux. This test then boots three Locations and a local plugin.
|
|
223
|
+
30_000,
|
|
224
|
+
)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { ServerAuth } from "@zyro-sec/server/auth"
|
|
3
|
+
import { Option, Redacted } from "effect"
|
|
4
|
+
|
|
5
|
+
test("accepts only the fixed opencode username", () => {
|
|
6
|
+
const config = { password: Option.some("secret"), username: "opencode" }
|
|
7
|
+
expect(ServerAuth.authorized({ username: "opencode", password: Redacted.make("secret") }, config)).toBe(true)
|
|
8
|
+
expect(ServerAuth.authorized({ username: "custom", password: Redacted.make("secret") }, config)).toBe(false)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test("session tokens expire, resist tampering, and are revoked by changing the password", () => {
|
|
12
|
+
const config = { password: Option.some("secret"), username: "opencode" }
|
|
13
|
+
const now = Date.now()
|
|
14
|
+
const token = ServerAuth.issueSession(config, now)
|
|
15
|
+
if (!token) throw new Error("Expected a session token")
|
|
16
|
+
expect(ServerAuth.verifySession(token, config, now)).toBe(true)
|
|
17
|
+
expect(ServerAuth.authorized({ username: "opencode", password: Redacted.make(token) }, config)).toBe(true)
|
|
18
|
+
expect(ServerAuth.verifySession(token, config, now + ServerAuth.SESSION_TTL_SECONDS * 1000)).toBe(false)
|
|
19
|
+
expect(ServerAuth.verifySession(`${token}x`, config, now)).toBe(false)
|
|
20
|
+
const parts = token.split(".")
|
|
21
|
+
expect(ServerAuth.verifySession(`${Number(parts[0]) + 1}.${parts[1]}`, config, now)).toBe(false)
|
|
22
|
+
expect(ServerAuth.verifySession(token, { ...config, password: Option.some("rotated") }, now)).toBe(false)
|
|
23
|
+
expect(ServerAuth.issueSession({ ...config, password: Option.none() })).toBeUndefined()
|
|
24
|
+
})
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { expect } from "bun:test"
|
|
4
|
+
import { OpenCode } from "@zyro-sec/client"
|
|
5
|
+
import { Effect } from "effect"
|
|
6
|
+
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
7
|
+
import { it } from "../../core/test/lib/effect"
|
|
8
|
+
import { ServerFetch } from "../src/fetch"
|
|
9
|
+
import { ServerProcess } from "../src/process"
|
|
10
|
+
import { startServer } from "./fixture/server"
|
|
11
|
+
|
|
12
|
+
const client = (handler: (request: Request) => Promise<Response>) =>
|
|
13
|
+
OpenCode.make({
|
|
14
|
+
baseUrl: "http://opencode.local",
|
|
15
|
+
fetch: Object.assign((input: string | URL | Request, init?: RequestInit) => handler(new Request(input, init)), {
|
|
16
|
+
preconnect: fetch.preconnect,
|
|
17
|
+
}),
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const hourly = { type: "every" as const, every: "1 hour" }
|
|
21
|
+
|
|
22
|
+
it.live(
|
|
23
|
+
"manages automations through the experimental API when enabled and honors a project automations override",
|
|
24
|
+
() =>
|
|
25
|
+
Effect.gen(function* () {
|
|
26
|
+
const dir = (yield* tmpdirScoped("opencode-automation-api-")).path
|
|
27
|
+
const global = path.join(dir, "global")
|
|
28
|
+
const enabled = path.join(dir, "enabled")
|
|
29
|
+
const ask = path.join(dir, "ask")
|
|
30
|
+
yield* Effect.promise(async () => {
|
|
31
|
+
await Promise.all([global, enabled, ask].map((item) => fs.mkdir(item, { recursive: true })))
|
|
32
|
+
await fs.writeFile(path.join(global, "opencode.json"), JSON.stringify({ automations: true }))
|
|
33
|
+
await fs.writeFile(path.join(enabled, "opencode.json"), JSON.stringify({}))
|
|
34
|
+
await fs.writeFile(path.join(ask, "opencode.json"), JSON.stringify({ permission: { execute: "ask" } }))
|
|
35
|
+
})
|
|
36
|
+
const handler = yield* ServerFetch.make({
|
|
37
|
+
app: { version: "test" },
|
|
38
|
+
database: { path: path.join(dir, "opencode.db") },
|
|
39
|
+
config: { directory: global, project: true, claude: false },
|
|
40
|
+
models: { fetch: false },
|
|
41
|
+
fs: { filewatcher: false },
|
|
42
|
+
})
|
|
43
|
+
const api = client(handler)
|
|
44
|
+
const post = (body: unknown) =>
|
|
45
|
+
handler(
|
|
46
|
+
new Request("http://opencode.local/api/experimental/automation", {
|
|
47
|
+
method: "POST",
|
|
48
|
+
headers: { "content-type": "application/json" },
|
|
49
|
+
body: JSON.stringify(body),
|
|
50
|
+
}),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
yield* Effect.promise(async () => {
|
|
54
|
+
const session = await api.session.create({
|
|
55
|
+
location: { directory: enabled },
|
|
56
|
+
})
|
|
57
|
+
await expect(
|
|
58
|
+
api.experimental.automation.create({
|
|
59
|
+
sessionID: session.id,
|
|
60
|
+
name: "build watch",
|
|
61
|
+
trigger: { type: "event", server: "ci", event: "build.changed" },
|
|
62
|
+
action: { type: "prompt", prompt: "Check the build" },
|
|
63
|
+
}),
|
|
64
|
+
).rejects.toMatchObject({
|
|
65
|
+
_tag: "InvalidRequestError",
|
|
66
|
+
message: expect.stringContaining("tool call"),
|
|
67
|
+
})
|
|
68
|
+
const created = await api.experimental.automation.create({
|
|
69
|
+
sessionID: session.id,
|
|
70
|
+
name: "build",
|
|
71
|
+
trigger: hourly,
|
|
72
|
+
expires: "in 8 hours",
|
|
73
|
+
action: { type: "prompt", prompt: "check the build" },
|
|
74
|
+
})
|
|
75
|
+
expect(created).toMatchObject({
|
|
76
|
+
sessionID: session.id,
|
|
77
|
+
directory: enabled,
|
|
78
|
+
name: "build",
|
|
79
|
+
trigger: hourly,
|
|
80
|
+
action: { type: "prompt", prompt: "check the build", to: { session: session.id } },
|
|
81
|
+
status: "active",
|
|
82
|
+
})
|
|
83
|
+
expect(created.expires).toBeDefined()
|
|
84
|
+
expect(await api.experimental.automation.list({ sessionID: session.id })).toMatchObject([{ id: created.id }])
|
|
85
|
+
expect(await api.experimental.automation.list({ directory: enabled })).toMatchObject([{ id: created.id }])
|
|
86
|
+
expect(await api.experimental.automation.list({})).toMatchObject([{ id: created.id }])
|
|
87
|
+
await expect(api.experimental.automation.list({ directory: "relative" })).rejects.toMatchObject({
|
|
88
|
+
_tag: "InvalidRequestError",
|
|
89
|
+
})
|
|
90
|
+
expect(await api.experimental.automation.get({ automationID: created.id })).toMatchObject({ id: created.id })
|
|
91
|
+
expect(await api.experimental.automation.pause({ automationID: created.id })).toMatchObject({
|
|
92
|
+
status: "paused",
|
|
93
|
+
})
|
|
94
|
+
expect(await api.experimental.automation.resume({ automationID: created.id })).toMatchObject({
|
|
95
|
+
status: "active",
|
|
96
|
+
})
|
|
97
|
+
await api.experimental.automation.delete({ automationID: created.id })
|
|
98
|
+
await expect(api.experimental.automation.get({ automationID: created.id })).rejects.toMatchObject({
|
|
99
|
+
_tag: "AutomationNotFoundError",
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// Without a Session, in a directory, prompting an existing Session.
|
|
103
|
+
const standalone = await api.experimental.automation.create({
|
|
104
|
+
name: "standalone",
|
|
105
|
+
directory: enabled,
|
|
106
|
+
trigger: hourly,
|
|
107
|
+
action: {
|
|
108
|
+
type: "prompt",
|
|
109
|
+
prompt: "hello",
|
|
110
|
+
to: { session: session.id },
|
|
111
|
+
},
|
|
112
|
+
})
|
|
113
|
+
expect(standalone).toMatchObject({
|
|
114
|
+
directory: enabled,
|
|
115
|
+
action: { to: { session: session.id } },
|
|
116
|
+
})
|
|
117
|
+
expect(standalone.sessionID).toBeUndefined()
|
|
118
|
+
// A subagent target needs a creating Session to be the parent of each run's child.
|
|
119
|
+
await expect(
|
|
120
|
+
api.experimental.automation.create({
|
|
121
|
+
name: "orphan",
|
|
122
|
+
directory: enabled,
|
|
123
|
+
trigger: hourly,
|
|
124
|
+
action: { type: "prompt", prompt: "hello", to: { subagent: "general" } },
|
|
125
|
+
}),
|
|
126
|
+
).rejects.toMatchObject({ _tag: "InvalidRequestError" })
|
|
127
|
+
const triage = await api.experimental.automation.create({
|
|
128
|
+
sessionID: session.id,
|
|
129
|
+
name: "triage",
|
|
130
|
+
trigger: hourly,
|
|
131
|
+
action: {
|
|
132
|
+
type: "prompt",
|
|
133
|
+
prompt: "triage",
|
|
134
|
+
to: { subagent: "general" },
|
|
135
|
+
},
|
|
136
|
+
})
|
|
137
|
+
expect(triage).toMatchObject({
|
|
138
|
+
sessionID: session.id,
|
|
139
|
+
action: { to: { subagent: "general" } },
|
|
140
|
+
})
|
|
141
|
+
await api.experimental.automation.delete({ automationID: triage.id })
|
|
142
|
+
|
|
143
|
+
await expect(
|
|
144
|
+
api.experimental.automation.create({
|
|
145
|
+
sessionID: session.id,
|
|
146
|
+
name: "bad",
|
|
147
|
+
trigger: { type: "every", every: "soon" },
|
|
148
|
+
action: { type: "prompt", prompt: "ping" },
|
|
149
|
+
}),
|
|
150
|
+
).rejects.toMatchObject({ _tag: "InvalidRequestError" })
|
|
151
|
+
// A trigger without its type, or a target naming both a Session and an agent, is rejected rather than guessed.
|
|
152
|
+
expect(
|
|
153
|
+
(
|
|
154
|
+
await post({
|
|
155
|
+
sessionID: session.id,
|
|
156
|
+
name: "x",
|
|
157
|
+
trigger: { every: "1 hour" },
|
|
158
|
+
action: { type: "prompt", prompt: "x" },
|
|
159
|
+
})
|
|
160
|
+
).status,
|
|
161
|
+
).toBe(400)
|
|
162
|
+
expect(
|
|
163
|
+
(
|
|
164
|
+
await post({
|
|
165
|
+
sessionID: session.id,
|
|
166
|
+
name: "x",
|
|
167
|
+
trigger: hourly,
|
|
168
|
+
action: { type: "prompt", prompt: "x", to: { session: session.id, subagent: "general" } },
|
|
169
|
+
})
|
|
170
|
+
).status,
|
|
171
|
+
).toBe(400)
|
|
172
|
+
expect(await api.experimental.automation.list({ sessionID: session.id })).toEqual([])
|
|
173
|
+
|
|
174
|
+
// Turn off the directory that still owns the standalone automation. Even unfiltered lists must hide it.
|
|
175
|
+
await fs.writeFile(path.join(enabled, "opencode.json"), JSON.stringify({ automations: false }))
|
|
176
|
+
await api.location.reload()
|
|
177
|
+
expect(await api.experimental.automation.list({})).toEqual([])
|
|
178
|
+
await expect(api.experimental.automation.get({ automationID: standalone.id })).rejects.toMatchObject({
|
|
179
|
+
_tag: "ForbiddenError",
|
|
180
|
+
})
|
|
181
|
+
await expect(
|
|
182
|
+
api.experimental.automation.create({
|
|
183
|
+
sessionID: session.id,
|
|
184
|
+
name: "x",
|
|
185
|
+
trigger: hourly,
|
|
186
|
+
action: { type: "prompt", prompt: "ping" },
|
|
187
|
+
}),
|
|
188
|
+
).rejects.toMatchObject({ _tag: "ForbiddenError" })
|
|
189
|
+
await expect(api.experimental.automation.list({ sessionID: session.id })).rejects.toMatchObject({
|
|
190
|
+
_tag: "ForbiddenError",
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
// A code action that would need an interactive approval is refused over stateless HTTP instead of hanging.
|
|
194
|
+
const askSession = await api.session.create({ location: { directory: ask } })
|
|
195
|
+
await expect(
|
|
196
|
+
api.experimental.automation.create({
|
|
197
|
+
sessionID: askSession.id,
|
|
198
|
+
name: "blocked",
|
|
199
|
+
trigger: hourly,
|
|
200
|
+
action: { type: "code", code: "return" },
|
|
201
|
+
}),
|
|
202
|
+
).rejects.toMatchObject({ _tag: "ForbiddenError" })
|
|
203
|
+
})
|
|
204
|
+
}).pipe(Effect.scoped),
|
|
205
|
+
// Boots three Locations (enabled, enabled again on reload, ask) as one of the suite's first server boots, which
|
|
206
|
+
// still pay the cold Location load on Windows CI (see artifact.test.ts).
|
|
207
|
+
30_000,
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
it.live("runs due automations in standalone ServerProcess.start without lifecycle", () =>
|
|
211
|
+
Effect.gen(function* () {
|
|
212
|
+
const dir = (yield* tmpdirScoped("opencode-automation-serve-")).path
|
|
213
|
+
const server = yield* startServer(dir)
|
|
214
|
+
const api = OpenCode.make({ baseUrl: server.base, headers: server.headers })
|
|
215
|
+
|
|
216
|
+
yield* Effect.promise(async () => {
|
|
217
|
+
const session = await api.session.create({ location: { directory: dir } })
|
|
218
|
+
const created = await api.experimental.automation.create({
|
|
219
|
+
sessionID: session.id,
|
|
220
|
+
name: "models",
|
|
221
|
+
trigger: { type: "at", at: "in 150 millis" },
|
|
222
|
+
action: {
|
|
223
|
+
type: "code",
|
|
224
|
+
code: 'const models = await tools.opencode.models({}); console.log("models=" + models.total); return { state: { total: models.total } }',
|
|
225
|
+
},
|
|
226
|
+
})
|
|
227
|
+
expect(created.status).toBe("active")
|
|
228
|
+
|
|
229
|
+
const deadline = Date.now() + 5_000
|
|
230
|
+
let current = created
|
|
231
|
+
while (current.status === "active" && Date.now() < deadline) {
|
|
232
|
+
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
233
|
+
current = await api.experimental.automation.get({ automationID: created.id })
|
|
234
|
+
}
|
|
235
|
+
expect(current).toMatchObject({
|
|
236
|
+
status: "done",
|
|
237
|
+
runCount: 1,
|
|
238
|
+
lastOutcome: "fired",
|
|
239
|
+
lastOutput: expect.stringContaining("models="),
|
|
240
|
+
state: { total: expect.any(Number) },
|
|
241
|
+
})
|
|
242
|
+
})
|
|
243
|
+
}).pipe(Effect.scoped),
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
it.live("starts one child for a due automation when two servers share a database", () =>
|
|
247
|
+
Effect.gen(function* () {
|
|
248
|
+
const dir = (yield* tmpdirScoped("opencode-automation-leader-")).path
|
|
249
|
+
const dbPath = path.join(dir, "shared.db")
|
|
250
|
+
yield* Effect.promise(() =>
|
|
251
|
+
fs.writeFile(
|
|
252
|
+
path.join(dir, "opencode.json"),
|
|
253
|
+
JSON.stringify({
|
|
254
|
+
// Exercise the child's durable prompt and wake, not inference through a default provider.
|
|
255
|
+
policies: [{ action: "provider.use", resource: "*", effect: "deny" }],
|
|
256
|
+
}),
|
|
257
|
+
),
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
// Seed an active automation in a short-lived scope so it falls due while no server is running.
|
|
261
|
+
const createdID = yield* Effect.scoped(
|
|
262
|
+
Effect.gen(function* () {
|
|
263
|
+
const handler = yield* ServerFetch.make({
|
|
264
|
+
app: { version: "test" },
|
|
265
|
+
database: { path: dbPath },
|
|
266
|
+
config: { directory: dir, project: true, claude: false },
|
|
267
|
+
models: { fetch: false },
|
|
268
|
+
fs: { filewatcher: false },
|
|
269
|
+
})
|
|
270
|
+
const seededApi = client(handler)
|
|
271
|
+
return yield* Effect.promise(async () => {
|
|
272
|
+
const parent = await seededApi.session.create({ location: { directory: dir } })
|
|
273
|
+
const created = await seededApi.experimental.automation.create({
|
|
274
|
+
sessionID: parent.id,
|
|
275
|
+
name: "single leader",
|
|
276
|
+
trigger: { type: "at", at: "in 80 millis" },
|
|
277
|
+
action: { type: "prompt", prompt: "single leader check", to: { subagent: "general" } },
|
|
278
|
+
})
|
|
279
|
+
return created.id
|
|
280
|
+
})
|
|
281
|
+
}),
|
|
282
|
+
)
|
|
283
|
+
yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 120)))
|
|
284
|
+
|
|
285
|
+
const boot = () =>
|
|
286
|
+
ServerProcess.start<never, never>({
|
|
287
|
+
hostname: "127.0.0.1",
|
|
288
|
+
port: 0,
|
|
289
|
+
password: "secret",
|
|
290
|
+
app: { version: "test-version" },
|
|
291
|
+
database: { path: dbPath },
|
|
292
|
+
config: { directory: dir },
|
|
293
|
+
fs: { filewatcher: false },
|
|
294
|
+
models: { fetch: false },
|
|
295
|
+
})
|
|
296
|
+
const [first] = yield* Effect.all([boot(), boot()], { concurrency: 2 })
|
|
297
|
+
const base = `http://127.0.0.1:${"port" in first.address ? first.address.port : 0}`
|
|
298
|
+
const api = OpenCode.make({
|
|
299
|
+
baseUrl: base,
|
|
300
|
+
headers: { authorization: `Basic ${btoa("opencode:secret")}` },
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
yield* Effect.promise(async () => {
|
|
304
|
+
const deadline = Date.now() + 5_000
|
|
305
|
+
let current = await api.experimental.automation.get({ automationID: createdID })
|
|
306
|
+
while (current.status === "active" && Date.now() < deadline) {
|
|
307
|
+
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
308
|
+
current = await api.experimental.automation.get({ automationID: createdID })
|
|
309
|
+
}
|
|
310
|
+
const started = (await api.session.list({})).data.filter((session) => session.title === "single leader")
|
|
311
|
+
expect(current).toMatchObject({ status: "done", runCount: 1 })
|
|
312
|
+
expect(started).toHaveLength(1)
|
|
313
|
+
})
|
|
314
|
+
}).pipe(Effect.scoped),
|
|
315
|
+
)
|