+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
167 lines
6.6 KiB
TypeScript
167 lines
6.6 KiB
TypeScript
import { afterEach, describe, expect } from "bun:test"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { Effect, Layer } from "effect"
|
|
import { HttpClientResponse } from "effect/unstable/http"
|
|
import { eq } from "drizzle-orm"
|
|
import { Database } from "@opencode-ai/core/database/database"
|
|
|
|
import { Session } from "@/session/session"
|
|
import { SessionPaths } from "../../src/server/routes/instance/httpapi/groups/session"
|
|
import { SyncPaths } from "../../src/server/routes/instance/httpapi/groups/sync"
|
|
import { MessageID, PartID } from "../../src/session/schema"
|
|
import { PartTable } from "@opencode-ai/core/session/sql"
|
|
import { resetDatabase } from "../fixture/db"
|
|
import { disposeAllInstances, TestInstance } from "../fixture/fixture"
|
|
import { testEffect } from "../lib/effect"
|
|
import { ProviderV2 } from "@opencode-ai/core/provider"
|
|
import { ModelV2 } from "@opencode-ai/core/model"
|
|
import { httpApiLayer, requestInDirectory } from "./httpapi-layer"
|
|
|
|
const it = testEffect(Layer.mergeAll(LayerNode.compile(LayerNode.group([Session.node, Database.node])), httpApiLayer))
|
|
|
|
const text = (response: HttpClientResponse.HttpClientResponse) => response.text
|
|
|
|
afterEach(async () => {
|
|
await disposeAllInstances()
|
|
await resetDatabase()
|
|
})
|
|
|
|
const seedCorruptStepFinishPart = Effect.gen(function* () {
|
|
const session = yield* Session.Service
|
|
const info = yield* session.create({})
|
|
const message = yield* session.updateMessage({
|
|
id: MessageID.ascending(),
|
|
role: "user",
|
|
sessionID: info.id,
|
|
agent: "build",
|
|
model: { providerID: ProviderV2.ID.make("test"), modelID: ModelV2.ID.make("test") },
|
|
time: { created: Date.now() },
|
|
})
|
|
const partID = PartID.ascending()
|
|
yield* session.updatePart({
|
|
id: partID,
|
|
sessionID: info.id,
|
|
messageID: message.id,
|
|
type: "step-finish",
|
|
reason: "stop",
|
|
cost: 0,
|
|
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
})
|
|
// Schema.Finite still rejects NaN at encode: exact mirror of the corrupt row
|
|
// that broke the user's session in the OMO/Windows bug.
|
|
const { db } = yield* Database.Service
|
|
yield* db
|
|
.update(PartTable)
|
|
.set({
|
|
data: {
|
|
type: "step-finish",
|
|
reason: "stop",
|
|
cost: 0,
|
|
tokens: { input: 0, output: NaN, reasoning: 0, cache: { read: 0, write: 0 } },
|
|
} as never, // drizzle's .set() can't narrow the discriminated union
|
|
})
|
|
.where(eq(PartTable.id, partID))
|
|
.run()
|
|
.pipe(Effect.orDie)
|
|
return info.id
|
|
})
|
|
|
|
describe("schema-rejection wire shape", () => {
|
|
it.instance(
|
|
"Payload schema rejection returns NamedError-shaped JSON, not empty",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const res = yield* requestInDirectory(SyncPaths.history, test.directory, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ aggregate: -1 }),
|
|
})
|
|
const body = yield* text(res)
|
|
expect(res.status).toBe(400)
|
|
expect(res.headers["content-type"] ?? "").toContain("application/json")
|
|
const parsed = JSON.parse(body)
|
|
expect(parsed).toMatchObject({
|
|
name: "BadRequest",
|
|
data: { kind: expect.stringMatching(/^(Body|Payload)$/) },
|
|
})
|
|
expect(parsed.data.message).toEqual(expect.any(String))
|
|
expect(parsed.data.message.length).toBeGreaterThan(0)
|
|
}),
|
|
{ git: true, config: { formatter: false, lsp: false } },
|
|
)
|
|
|
|
it.instance(
|
|
"Query schema rejection returns NamedError-shaped JSON",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
// /find/file?limit=999999 violates the limit constraint check.
|
|
const url = `/find/file?query=foo&limit=999999&directory=${encodeURIComponent(test.directory)}`
|
|
const res = yield* requestInDirectory(url, test.directory)
|
|
const body = yield* text(res)
|
|
expect(res.status).toBe(400)
|
|
const parsed = JSON.parse(body)
|
|
expect(parsed).toMatchObject({ name: "BadRequest", data: { kind: "Query" } })
|
|
}),
|
|
{ git: true, config: { formatter: false, lsp: false } },
|
|
)
|
|
|
|
it.instance(
|
|
"v2 query schema rejection returns InvalidRequestError JSON",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const res = yield* requestInDirectory("/api/session?limit=0", test.directory)
|
|
const parsed = JSON.parse(yield* text(res))
|
|
expect(res.status).toBe(400)
|
|
expect(parsed).toMatchObject({ _tag: "InvalidRequestError", kind: "Query" })
|
|
expect(parsed.message).toEqual(expect.any(String))
|
|
}),
|
|
{ git: true, config: { formatter: false, lsp: false } },
|
|
)
|
|
|
|
it.instance(
|
|
"rejected request body never echoes back unbounded — message is capped",
|
|
// Defense against DoS-amplification + secret-echo: Effect's Issue formatter
|
|
// dumps the rejected `actual` verbatim. A multi-MB invalid array would
|
|
// become a multi-MB 400 response and log line. Cap kicks in around 1KB.
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const huge = "X".repeat(50_000)
|
|
const res = yield* requestInDirectory(SyncPaths.history, test.directory, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ aggregate: huge }),
|
|
})
|
|
const body = yield* text(res)
|
|
expect(res.status).toBe(400)
|
|
// 1 KB cap + small JSON envelope ≈ <2 KB — never tens of KB.
|
|
expect(body.length).toBeLessThan(2 * 1024)
|
|
const parsed = JSON.parse(body)
|
|
expect(parsed.data.message).not.toContain(huge)
|
|
}),
|
|
{ git: true, config: { formatter: false, lsp: false } },
|
|
)
|
|
|
|
it.instance(
|
|
"response-encode failure: corrupted stored row returns NamedError-shaped JSON with field path",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const sessionID = yield* seedCorruptStepFinishPart
|
|
const url = `${SessionPaths.messages.replace(":sessionID", sessionID)}?limit=80&directory=${encodeURIComponent(test.directory)}`
|
|
const res = yield* requestInDirectory(url, test.directory)
|
|
const body = yield* text(res)
|
|
expect(res.status).toBe(400)
|
|
expect(res.headers["content-type"] ?? "").toContain("application/json")
|
|
const parsed = JSON.parse(body)
|
|
expect(parsed).toMatchObject({ name: "BadRequest", data: { kind: "Body" } })
|
|
// Field path in data.message — what made this PR worth shipping.
|
|
expect(parsed.data.message).toMatch(/output/)
|
|
}),
|
|
{ config: { formatter: false, lsp: false } },
|
|
)
|
|
})
|