Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"typecheck": "tsc --noEmit",
"test": "bun test --timeout 30000 --only-failures",
"test:httpapi": "bun run script/httpapi-exercise.ts --mode coverage --fail-on-missing --fail-on-skip && bun run script/httpapi-exercise.ts --mode auth --fail-on-missing --fail-on-skip --progress && bun run script/httpapi-exercise.ts --mode effect --fail-on-missing --fail-on-skip --progress",
"test:httpapi": "bun run script/httpapi-exercise.ts --mode coverage --fail-on-missing --fail-on-skip && bun run script/httpapi-exercise.ts --mode auth --fail-on-missing --fail-on-skip --progress && bun run script/httpapi-exercise.ts --mode effect --fail-on-missing --fail-on-skip --progress --trace",
"bench:test": "bun run script/bench-test-suite.ts",
"profile:test": "bun run script/profile-test-files.ts",
"build": "bun run script/build.ts",
Expand Down
47 changes: 43 additions & 4 deletions packages/opencode/test/server/httpapi-exercise/runner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout as sleep } from "node:timers/promises"
import { Flag } from "@opencode-ai/core/flag/flag"
import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
import { SessionV1 } from "@opencode-ai/core/v1/session"
Expand All @@ -14,6 +15,38 @@ import type { ActiveScenario, Options, ProjectOptions, Result, Scenario, Scenari
import { ProviderV2 } from "@opencode-ai/core/provider"
import { ModelV2 } from "@opencode-ai/core/model"

const CLEANUP_BOUND_MS = 60_000

// Finalizers run uninterruptibly, so an Effect timeout cannot bound cleanup:
// race each async disposal against a plain timer instead. The timer is
// aborted as soon as the race settles, so a fast success logs nothing
// and leaves no timer pending. Wedges and disposal errors are logged and
// swallowed rather than propagated: these are best-effort resets of
// test-scoped state (scratch db/tmp per run) that must neither hang the run
// nor mask the scenario's own result - resetState runs under
// Effect.ensuring, and in this Effect version a failing finalizer replaces
// the scenario's original cause.
const bounded = (label: string, promise: Promise<unknown>): Promise<void> => {
const cancel = new AbortController()
const bound = sleep(CLEANUP_BOUND_MS, undefined, { signal: cancel.signal })
.then(() => {
console.error(`[reset] ${label} did not finish within ${CLEANUP_BOUND_MS / 1000}s; continuing`)
})
// The finally below aborts this timer once the race settles; the race
// already observes the resulting AbortError, and this catch swallows it
// explicitly so static review sees the rejection as handled.
.catch(() => {})
return Promise.race([
promise.then(
() => {},
(error) => {
console.error(`[reset] ${label} failed; continuing`, error)
},
),
bound,
]).finally(() => cancel.abort())
}

export function runScenario(options: Options) {
return (scenario: Scenario) => {
if (scenario.kind === "todo") return Effect.succeed({ status: "skip", scenario } as Result)
Expand Down Expand Up @@ -79,8 +112,14 @@ function withContext<A, E>(
(ctx) =>
Effect.gen(function* () {
yield* trace(options, scenario, `${label} tmpdir cleanup start`)
// Finalizers run uninterruptibly, so an Effect timeout could not bound
// this either: bounded() races the disposal against a plain timer.
// After a scenario timeout the run must move on even if cleanup is
// wedged. Effect.ignore still guards a synchronous throw from the
// asyncDispose call itself.
yield* Effect.promise(async () => {
await ctx.dir?.[Symbol.asyncDispose]()
const disposal = ctx.dir?.[Symbol.asyncDispose]()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Inconsistent error handling vs bounded helper

The comment claims "Same reasoning as bounded()" but the behavior differs: this tmpdir cleanup always resolves (both fulfillment and rejection of disposal call resolve()), while bounded (line 297) rejects on disposal rejection. If disposal fails here, the error is silently swallowed; in resetState it would propagate. Decide which behavior is intentional and align them, or update the comment.

if (disposal) await bounded(`${label} tmpdir disposal`, disposal)
}).pipe(Effect.ignore)
yield* trace(options, scenario, `${label} tmpdir cleanup done`)
}),
Expand Down Expand Up @@ -270,8 +309,8 @@ const resetState = Effect.promise(async () => {
const modules = await runtime()
Flag.OPENCODE_SERVER_PASSWORD = original.OPENCODE_SERVER_PASSWORD
Flag.OPENCODE_SERVER_USERNAME = original.OPENCODE_SERVER_USERNAME
await disposeApps()
await modules.disposeAllInstances()
await modules.resetDatabase()
await bounded("disposeApps", disposeApps())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: resetState failure masks original scenario error

resetState is run via Effect.ensuring(scenario.reset ? resetState : Effect.void) (line 221, unchanged). If any bounded call rejects (e.g., a disposal throws), resetState fails and Effect.ensuring propagates that failure, replacing the original scenario result (pass/fail). This means a flaky disposal error could hide a genuine test failure. Consider wrapping resetState in Effect.catchAll(() => Effect.void) or similar to keep it best-effort.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: Sequential bounded calls in resetState can take up to 180s total

Three sequential await bounded(...) calls each with a 60s timeout means resetState can take up to 180s. Consider Promise.all([bounded(...), bounded(...), bounded(...)]) for parallel execution, unless sequential order is required for correctness.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

await bounded("disposeAllInstances", modules.disposeAllInstances())
await bounded("resetDatabase", modules.resetDatabase())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: disposeAllInstances called twice during reset

resetState calls bounded("disposeAllInstances", modules.disposeAllInstances()) at line 320, then bounded("resetDatabase", modules.resetDatabase()) here. But resetDatabase (in db.ts:6) also calls disposeAllInstances().catch(() => undefined) internally. This double-disposal is likely harmless but wasteful; consider removing the inner call or the outer one.

await Bun.sleep(25)
})
Loading