opencode/packages/server/test/space.test.ts
Sebastian 63b7f04555
feat(server): batch writes for experimental spaces (#66668)
Co-authored-by: kommander <335157+kommander@users.noreply.github.com>
2026-11-26 21:52:33 +01:00

346 lines
16 KiB
TypeScript

import path from "node:path"
import { expect } from "bun:test"
import { OpenCode } from "@opencode/client"
import { Effect } from "effect"
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
import { it } from "../../core/test/lib/effect"
import { ServerFetch } from "../src/fetch"
const authorization = `Basic ${btoa("opencode:secret")}`
const client = (handler: (request: Request) => Promise<Response>, headers?: Record<string, string>) =>
OpenCode.make({
baseUrl: "http://opencode.local",
headers,
fetch: Object.assign((input: string | URL | Request, init?: RequestInit) => handler(new Request(input, init)), {
preconnect: fetch.preconnect,
}),
})
// Reads the wire stream directly: each call is its own connection, with its own stream ID and HTTP status.
const subscribe = async (
handler: (request: Request) => Promise<Response>,
query: string,
headers: Record<string, string> = { authorization },
) => {
const response = await handler(new Request(`http://opencode.local/api/event?${query}`, { headers }))
const reader = response.body?.pipeThrough(new TextDecoderStream()).getReader()
const frames: string[] = []
let buffer = ""
return {
status: response.status,
next: async (): Promise<{ readonly type: string; readonly data: unknown }> => {
while (frames.length === 0) {
const chunk = await reader?.read()
if (!chunk || chunk.done) throw new Error("event stream closed")
const parts = (buffer + chunk.value).split("\n\n")
buffer = parts.pop() ?? ""
frames.push(...parts.filter((part) => part.startsWith("data: ")))
}
const event = JSON.parse(frames.shift()?.slice("data: ".length) ?? "")
return { type: event.type, data: event.data }
},
close: () => reader?.cancel(),
}
}
it.live("syncs a space through the API and follows it on the event stream, across a restart", () =>
Effect.gen(function* () {
const dir = (yield* tmpdirScoped("opencode-space-api-")).path
const boot = ServerFetch.make({
app: { version: "test" },
password: "secret",
database: { path: path.join(dir, "opencode.db") },
config: {
directory: dir,
project: false,
claude: false,
content: "{}",
},
models: { fetch: false },
fs: { filewatcher: false },
})
// Issued by the database, so it survives the restart below.
let epoch = ""
let cursor = ""
yield* Effect.scoped(
Effect.gen(function* () {
const handler = yield* boot
const writer = client(handler, { authorization })
yield* Effect.promise(async () => {
// A stream that subscribes to no space still connects at once and gets its other events.
const plain = await subscribe(handler, "types=session.created")
const connected = await plain.next()
expect(connected.type).toBe("server.connected")
const stream = (connected.data as { stream: string }).stream
expect(stream).toMatch(/^[0-9a-f-]{36}$/u)
await writer.session.create({ location: { directory: dir } })
expect((await plain.next()).type).toBe("session.created")
await writer.experimental.space.write({
space: "app",
changes: [{ key: "draft/1", value: '{"text":"hi","n":[1,null]}' }],
})
await writer.experimental.space.write({ space: "other", changes: [{ key: "draft/1", value: "elsewhere" }] })
const reader = await subscribe(handler, "types=session.created")
const readerStream = ((await reader.next()).data as { stream: string }).stream
expect(readerStream).not.toBe(stream)
const page = await writer.experimental.space.sync({ space: "app" })
epoch = page.epoch
expect(page).toEqual({
type: "snapshot",
space: "app",
epoch,
revision: 1,
entries: [{ key: "draft/1", value: '{"text":"hi","n":[1,null]}' }],
cursor: `${epoch}.1`,
})
// Between the sync and the subscription.
await writer.experimental.space.write({ space: "app", changes: [{ key: "draft/2", value: "2" }] })
await writer.experimental.space.subscribe({
stream: readerStream,
spaces: [{ space: "app", cursor: page.cursor }],
})
expect(await reader.next()).toEqual({
type: "experimental.space.updated",
data: { space: "app", key: "draft/2", value: "2", revision: 2 },
})
await writer.experimental.space.write({ space: "other", changes: [{ key: "draft/1", value: "unseen" }] })
await writer.experimental.space.write({ space: "app", changes: [{ key: "draft/1", value: null }] })
await writer.session.create({ location: { directory: dir } })
expect(await reader.next()).toEqual({
type: "experimental.space.deleted",
data: { space: "app", key: "draft/1", revision: 3 },
})
expect((await reader.next()).type).toBe("session.created")
// The stream that subscribed to nothing sees no space events at all.
expect((await plain.next()).type).toBe("session.created")
// Unsubscribing a space the stream does not follow succeeds.
await writer.experimental.space.unsubscribe({ stream, spaces: ["never"] })
await reader.close()
await plain.close()
cursor = `${epoch}.3`
// A closed stream, or one that never existed, cannot be subscribed.
await expect(
writer.experimental.space.subscribe({ stream: readerStream, spaces: [{ space: "app", cursor }] }),
).rejects.toMatchObject({ _tag: "InvalidRequestError", kind: "stream_not_found", field: "stream" })
await expect(
writer.experimental.space.subscribe({ stream: "not-a-stream", spaces: [] }),
).rejects.toMatchObject({ _tag: "InvalidRequestError", kind: "stream_not_found", field: "stream" })
await expect(
writer.experimental.space.unsubscribe({ stream: "not-a-stream", spaces: ["app"] }),
).rejects.toMatchObject({ _tag: "InvalidRequestError", kind: "stream_not_found", field: "stream" })
// Offline, in one write: repeated updates, a key created and deleted, and deletes of already deleted keys.
// It reports the head after its last change that changed a key.
expect(
await writer.experimental.space.write({
space: "app",
changes: [
{ key: "draft/2", value: "3" },
{ key: "draft/2", value: "4" },
{ key: "draft/3", value: "x" },
{ key: "draft/3", value: null },
{ key: "draft/3", value: null },
{ key: "draft/1", value: null },
],
}),
).toEqual({ revision: 7, cursor: `${epoch}.7` })
expect(await writer.experimental.space.sync({ space: "app", cursor })).toEqual({
type: "delta",
space: "app",
epoch,
revision: 7,
entries: [{ key: "draft/2", value: "4" }],
deleted: ["draft/3"],
cursor: `${epoch}.7`,
})
cursor = `${epoch}.7`
const anonymous = client(handler)
for (const call of [
() => anonymous.experimental.space.write({ space: "app", changes: [{ key: "x", value: "1" }] }),
() => anonymous.experimental.space.sync({ space: "app" }),
() => anonymous.experimental.space.subscribe({ stream, spaces: [] }),
() => anonymous.experimental.space.unsubscribe({ stream, spaces: [] }),
])
await expect(call()).rejects.toMatchObject({ _tag: "UnauthorizedError" })
expect((await subscribe(handler, "", {})).status).toBe(401)
// In its own space, so the revisions above are unchanged.
const write = (changes: unknown) =>
handler(
new Request("http://opencode.local/api/experimental/space/limit/write", {
method: "POST",
headers: { authorization, "content-type": "application/json" },
body: JSON.stringify({ changes }),
}),
)
const values = (count: number) =>
Array.from({ length: count }, (_, index) => ({ key: "c", value: `${index}` }))
// A write has 1 to 1024 changes, each value a string or null.
for (const changes of [[], values(1025), [{ key: "c", value: 1 }], [{ key: "c" }]])
expect((await write(changes)).status).toBe(400)
expect(await (await write(values(1024))).json()).toEqual({ revision: 1024, cursor: `${epoch}.1024` })
// The cap is the UTF-8 length of a write's values together, keys aren't counted: exactly 1 MiB is stored, one
// byte more is not.
expect((await write([{ key: "c", value: "x".repeat(1024 * 1024) }])).status).toBe(200)
expect(
(
await write([
{ key: "c", value: "x".repeat(1024 * 1024 - 2) },
{ key: "d", value: "é" },
])
).status,
).toBe(200)
expect(
(
await write([
{ key: "c", value: "x".repeat(1024 * 1024 - 1) },
{ key: "d", value: "é" },
])
).status,
).toBe(400)
// A rejected write commits none of its changes, though each value would fit, and one that changes nothing
// reports the head.
expect(
(
await write([
{ key: "e", value: "x".repeat(512 * 1024) },
{ key: "c", value: "x".repeat(512 * 1024 + 1) },
])
).status,
).toBe(400)
expect(await (await write([{ key: "e", value: null }])).json()).toEqual({
revision: 1027,
cursor: `${epoch}.1027`,
})
})
}),
)
const handler = yield* boot
yield* Effect.promise(async () => {
const writer = client(handler, { authorization })
const reader = await subscribe(handler, "types=session.created")
const stream = ((await reader.next()).data as { stream: string }).stream
await writer.experimental.space.write({ space: "app", changes: [{ key: "draft/4", value: "true" }] })
// A new stream after the restart subscribes with the cursor saved before it.
await writer.experimental.space.subscribe({ stream, spaces: [{ space: "app", cursor }] })
expect(await reader.next()).toEqual({
type: "experimental.space.updated",
data: { space: "app", key: "draft/4", value: "true", revision: 8 },
})
await writer.experimental.space.write({ space: "app", changes: [{ key: "draft/4", value: null }] })
expect(await reader.next()).toEqual({
type: "experimental.space.deleted",
data: { space: "app", key: "draft/4", revision: 9 },
})
await reader.close()
// A reset, as after a restore, makes every saved cursor unusable; only an authenticated client may do it.
await expect(client(handler).experimental.space.reset()).rejects.toMatchObject({ _tag: "UnauthorizedError" })
await writer.experimental.space.reset()
const reset = await writer.experimental.space.sync({ space: "app", cursor })
expect(reset.type).toBe("snapshot")
expect(reset.epoch).not.toBe(epoch)
})
}).pipe(Effect.scoped),
)
it.live("two clients follow one space on their own streams, through unsubscribe and a reconnect", () =>
Effect.gen(function* () {
const dir = (yield* tmpdirScoped("opencode-space-clients-")).path
const handler = yield* ServerFetch.make({
app: { version: "test" },
password: "secret",
database: { path: path.join(dir, "opencode.db") },
config: { directory: dir, project: false, claude: false, content: "{}" },
models: { fetch: false },
fs: { filewatcher: false },
})
yield* Effect.promise(async () => {
const api = client(handler, { authorization })
const updated = (key: string, value: string, revision: number) => ({
type: "experimental.space.updated",
data: { space: "pins", key, value, revision },
})
const deleted = (key: string, revision: number) => ({
type: "experimental.space.deleted",
data: { space: "pins", key, revision },
})
// Each client opens its own stream and reads its ID, then syncs unless it has a saved cursor, and subscribes.
const follow = async (saved?: string) => {
const events = await subscribe(handler, "types=session.created")
const stream = ((await events.next()).data as { stream: string }).stream
const page = saved === undefined ? await api.experimental.space.sync({ space: "pins" }) : undefined
await api.experimental.space.subscribe({
stream,
spaces: [{ space: "pins", cursor: saved ?? page?.cursor ?? "" }],
})
return { events, stream, page }
}
const a = await follow()
const b = await follow()
expect(a.stream).not.toBe(b.stream)
const epoch = a.page?.epoch ?? ""
for (const page of [a.page, b.page])
expect(page).toEqual({ type: "snapshot", space: "pins", epoch, revision: 0, entries: [], cursor: `${epoch}.0` })
await api.experimental.space.write({ space: "pins", changes: [{ key: "x", value: "1" }] })
expect([await a.events.next(), await b.events.next()]).toEqual([updated("x", "1", 1), updated("x", "1", 1)])
await api.experimental.space.write({ space: "pins", changes: [{ key: "x", value: null }] })
expect([await a.events.next(), await b.events.next()]).toEqual([deleted("x", 2), deleted("x", 2)])
await api.experimental.space.write({ space: "pins", changes: [{ key: "y", value: "2" }] })
expect([await a.events.next(), await b.events.next()]).toEqual([updated("y", "2", 3), updated("y", "2", 3)])
// B stops following pins: A still gets z, and B's next event is the session created after it.
await api.experimental.space.unsubscribe({ stream: b.stream, spaces: ["pins"] })
await api.experimental.space.write({ space: "pins", changes: [{ key: "z", value: "3" }] })
await api.session.create({ location: { directory: dir } })
expect(await a.events.next()).toEqual(updated("z", "3", 4))
expect((await a.events.next()).type).toBe("session.created")
expect((await b.events.next()).type).toBe("session.created")
// B goes away; A changes more; B comes back on a new stream with the cursor of the last change it applied.
await b.events.close()
await api.experimental.space.write({
space: "pins",
changes: [
{ key: "a", value: "4" },
{ key: "b", value: "5" },
{ key: "a", value: null },
],
})
const back = await follow(`${epoch}.3`)
expect(back.stream).not.toBe(b.stream)
await api.experimental.space.write({ space: "pins", changes: [{ key: "c", value: "6" }] })
// What it missed, one event per key in revision order, then the live change.
expect([
await back.events.next(),
await back.events.next(),
await back.events.next(),
await back.events.next(),
]).toEqual([updated("z", "3", 4), updated("b", "5", 6), deleted("a", 7), updated("c", "6", 8)])
// A fresh client with no cursor gets the final state as a snapshot.
expect(await api.experimental.space.sync({ space: "pins" })).toEqual({
type: "snapshot",
space: "pins",
epoch,
revision: 8,
entries: [
{ key: "y", value: "2" },
{ key: "z", value: "3" },
{ key: "b", value: "5" },
{ key: "c", value: "6" },
],
cursor: `${epoch}.8`,
})
await a.events.close()
await back.events.close()
})
}).pipe(Effect.scoped),
)