mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
fix(codemode): explain a stalled program and point at the await that never settles (#59749)
This commit is contained in:
committed by
GitHub
Unverified
parent
3a409f42f5
commit
2ed57439da
@@ -415,8 +415,8 @@ export class Interpreter<R> {
|
||||
}
|
||||
|
||||
/** Await: suspends the innermost async body until `value` settles. */
|
||||
*awaitValue(value: Value): Eval<Value> {
|
||||
return (yield new Await(value)) as Value
|
||||
*awaitValue(value: Value, node?: AstNode): Eval<Value> {
|
||||
return (yield new Await(value, node)) as Value
|
||||
}
|
||||
|
||||
iterate(value: Value) {
|
||||
@@ -510,8 +510,8 @@ class Frame<R> {
|
||||
)) as Value
|
||||
}
|
||||
|
||||
awaitValue(value: Value): Eval<Value> {
|
||||
return this.ctx.awaitValue(value)
|
||||
awaitValue(value: Value, node?: AstNode): Eval<Value> {
|
||||
return this.ctx.awaitValue(value, node)
|
||||
}
|
||||
|
||||
private *evaluateStatement(node: Statement | ModuleDeclaration): Eval<StatementResult> {
|
||||
@@ -581,7 +581,7 @@ class Frame<R> {
|
||||
const evaluated = this.evaluate(argumentNode)
|
||||
const value = isEval(evaluated) ? yield* evaluated : evaluated
|
||||
// An async generator awaits what it returns, as it awaits what it yields.
|
||||
return { kind: "return", value: this.generatorAsync ? yield* this.awaitValue(value) : value }
|
||||
return { kind: "return", value: this.generatorAsync ? yield* this.awaitValue(value, node) : value }
|
||||
}
|
||||
|
||||
private *evaluateBlock(node: BlockStatement): Eval<StatementResult> {
|
||||
@@ -1063,7 +1063,7 @@ class Frame<R> {
|
||||
private *nextIteratorResult(iterator: CustomIterator, node: AstNode | undefined): Eval<Step> {
|
||||
const called = yield* this.call(iterator.next, iterator.iterator, [], node)
|
||||
const object = this.requireIteratorObject(
|
||||
iterator.asynchronous ? yield* this.awaitValue(called) : called,
|
||||
iterator.asynchronous ? yield* this.awaitValue(called, node) : called,
|
||||
"Iterator next() result",
|
||||
node,
|
||||
)
|
||||
@@ -1076,7 +1076,7 @@ class Frame<R> {
|
||||
const method = this.requireIteratorMethod(close, "Iterator return", node)
|
||||
const called = yield* this.call(method, iterator.iterator, [], node)
|
||||
this.requireIteratorObject(
|
||||
iterator.asynchronous ? yield* this.awaitValue(called) : called,
|
||||
iterator.asynchronous ? yield* this.awaitValue(called, node) : called,
|
||||
"Iterator return() result",
|
||||
node,
|
||||
)
|
||||
@@ -1533,7 +1533,7 @@ class Frame<R> {
|
||||
|
||||
private *evaluateAwaitExpression(node: AwaitExpression): Eval<Value> {
|
||||
// Await always suspends, including for plain values.
|
||||
return yield* this.awaitValue(yield* this.evaluateExpression(node.argument))
|
||||
return yield* this.awaitValue(yield* this.evaluateExpression(node.argument), node)
|
||||
}
|
||||
|
||||
private *evaluateNewExpression(node: NewExpression): Eval<Value> {
|
||||
@@ -2026,7 +2026,7 @@ class Frame<R> {
|
||||
if (!this.generator) throw typeError("yield is only valid inside a generator.", node)
|
||||
const value = argument ? yield* this.evaluateExpression(argument) : undefined
|
||||
if (node.delegate) return yield* this.delegateYield(value, node)
|
||||
return yield* this.suspendGenerator(this.generatorAsync ? yield* this.awaitValue(value) : value)
|
||||
return yield* this.suspendGenerator(this.generatorAsync ? yield* this.awaitValue(value, node) : value)
|
||||
}
|
||||
|
||||
// Yield `value` and continue with the request that resumes the body: `next` supplies the value of the `yield`,
|
||||
@@ -2069,7 +2069,8 @@ class Frame<R> {
|
||||
while (true) {
|
||||
const method = kind === "next" ? iterator.next : get(iterator.iterator, kind)
|
||||
if (method === undefined || method === null) {
|
||||
if (kind === "return") throw new GeneratorReturn(this.generatorAsync ? yield* this.awaitValue(input) : input)
|
||||
if (kind === "return")
|
||||
throw new GeneratorReturn(this.generatorAsync ? yield* this.awaitValue(input, node) : input)
|
||||
yield* this.closeIterator(iterator, node)
|
||||
throw typeError("The delegated iterator does not provide a throw() method.", node)
|
||||
}
|
||||
@@ -2080,7 +2081,7 @@ class Frame<R> {
|
||||
node,
|
||||
)
|
||||
const result = this.requireIteratorObject(
|
||||
iterator.asynchronous ? yield* this.awaitValue(called) : called,
|
||||
iterator.asynchronous ? yield* this.awaitValue(called, node) : called,
|
||||
`Iterator ${kind}() result`,
|
||||
node,
|
||||
)
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { Diagnostic } from "../codemode.js"
|
||||
import { locate, normalizeError } from "./errors.js"
|
||||
import type { Interpreter } from "./interpreter.js"
|
||||
import { MAX_PENDING_PROMISES } from "./limits.js"
|
||||
import { type AstNode, type GeneratorRequestKind, rangeError, Throw, typeError } from "./model.js"
|
||||
import { type AstNode, type GeneratorRequestKind, PendingThrow, rangeError, Throw, typeError } from "./model.js"
|
||||
import { fn } from "./native.js"
|
||||
import { type Callable, get, Obj, PromiseObj, type Value } from "./objects.js"
|
||||
import { typeofValue } from "./references.js"
|
||||
@@ -38,9 +38,12 @@ export class Enter {
|
||||
) {}
|
||||
}
|
||||
|
||||
/** Suspend the innermost async body until `value` settles. */
|
||||
/** Suspend the innermost async body until `value` settles; a stall while it waits is reported at `node`. */
|
||||
export class Await {
|
||||
constructor(readonly value: Value) {}
|
||||
constructor(
|
||||
readonly value: Value,
|
||||
readonly node?: AstNode,
|
||||
) {}
|
||||
}
|
||||
|
||||
/** Suspend the innermost generator body, producing `value`; it continues with the next request. */
|
||||
@@ -90,7 +93,7 @@ export class AsyncBoundary implements Boundary {
|
||||
readonly generator = false
|
||||
constructor(
|
||||
private readonly settle: (completion: Completion) => void,
|
||||
private readonly result: Value,
|
||||
readonly result: Value,
|
||||
) {}
|
||||
finish(completion: Completion): Completion {
|
||||
this.settle(completion)
|
||||
@@ -152,6 +155,8 @@ export class Runner<R> {
|
||||
private nextID = 0
|
||||
/** Rejections nothing handled yet, described when they happened so reporting them at the end is cheap. */
|
||||
private readonly unhandled = new Map<PromiseObj, Diagnostic>()
|
||||
/** Bodies suspended at an `await` and the promise each waits on, so a stall can point at the one that is stuck. */
|
||||
private readonly parked = new Map<Task, { readonly promise: PromiseObj; readonly node: AstNode | undefined }>()
|
||||
private readonly fibers = new Set<Fiber.Fiber<unknown, unknown>>()
|
||||
private readonly inbox: Array<() => void> = []
|
||||
private wake: (() => void) | undefined
|
||||
@@ -206,8 +211,10 @@ export class Runner<R> {
|
||||
continue
|
||||
}
|
||||
return abrupt(
|
||||
new Error(
|
||||
"Execution stalled: the program is waiting for a promise that nothing can settle (no job is queued and no tool call is running).",
|
||||
new PendingThrow(
|
||||
"Error",
|
||||
"The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program.",
|
||||
self.stuckAwait(),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -462,7 +469,11 @@ export class Runner<R> {
|
||||
}
|
||||
const boundary = stack[index]!.boundary!
|
||||
const continuation = new Task(stack.splice(index))
|
||||
this.then(promise, (settled) => ({ task: continuation, input: settled }))
|
||||
this.parked.set(continuation, { promise, node: signal.node })
|
||||
this.then(promise, (settled) => {
|
||||
this.parked.delete(continuation)
|
||||
return { task: continuation, input: settled }
|
||||
})
|
||||
completion = boundary.suspended()
|
||||
continue
|
||||
}
|
||||
@@ -483,6 +494,18 @@ export class Runner<R> {
|
||||
return undefined
|
||||
}
|
||||
|
||||
// The innermost stuck `await`: an outer one waits on the result of a parked async body, so prefer one that does not.
|
||||
// Bodies that wait on each other in a cycle leave no such await; any located one is then as good as another.
|
||||
private stuckAwait(): AstNode | undefined {
|
||||
const results = new Set<Value>()
|
||||
for (const task of this.parked.keys()) {
|
||||
const boundary = task.stack[0]?.boundary
|
||||
if (boundary instanceof AsyncBoundary) results.add(boundary.result)
|
||||
}
|
||||
const located = [...this.parked.values()].filter((parked) => parked.node !== undefined)
|
||||
return (located.find((parked) => !results.has(parked.promise)) ?? located[0])?.node
|
||||
}
|
||||
|
||||
private dequeue(): Job | undefined {
|
||||
if (this.head === this.queue.length) {
|
||||
if (this.head > 0) {
|
||||
|
||||
@@ -1560,10 +1560,40 @@ describe("job order matches V8", () => {
|
||||
})
|
||||
|
||||
describe("a program that can never finish", () => {
|
||||
test("fails at once when it awaits a promise nothing can settle, without a timeout", async () => {
|
||||
const failure = await error(`await new Promise(() => {}); return 1`)
|
||||
expect(failure.kind).toBe("ExecutionFailure")
|
||||
expect(failure.message).toStartWith("Execution stalled")
|
||||
const stalled =
|
||||
"Error: The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program."
|
||||
|
||||
// Each program stalls at the await on line `line`, column `column`; an outer await only waits on that one.
|
||||
test.each([
|
||||
["the program's own await", "await new Promise((r) => r)", 1, 1],
|
||||
[
|
||||
"an async function whose result the program returns",
|
||||
["async function wait() {", " await new Promise(() => {})", "}", "return wait()"].join("\n"),
|
||||
2,
|
||||
3,
|
||||
],
|
||||
[
|
||||
"an async function the program awaits",
|
||||
["async function wait() {", " await new Promise(() => {})", "}", "await wait()"].join("\n"),
|
||||
2,
|
||||
3,
|
||||
],
|
||||
[
|
||||
"a later await in an async body the program awaits",
|
||||
[
|
||||
"const { promise } = Promise.withResolvers()",
|
||||
"const p = (async () => { await null; await promise })()",
|
||||
"await p",
|
||||
].join("\n"),
|
||||
2,
|
||||
38,
|
||||
],
|
||||
])("fails at once, without a timeout, at the stuck await in %s", async (_, code, line, column) => {
|
||||
expect(await error(code)).toEqual({
|
||||
kind: "ExecutionFailure",
|
||||
message: `${stalled} (line ${line}, col ${column})`,
|
||||
location: { line, column },
|
||||
})
|
||||
})
|
||||
|
||||
test("fails when the unsettleable promise is deep inside other async work", async () => {
|
||||
@@ -1571,7 +1601,26 @@ describe("a program that can never finish", () => {
|
||||
const never = async () => { await null; await new Promise(() => {}) }
|
||||
return await Promise.all([never(), Promise.resolve(1)])
|
||||
`)
|
||||
expect(failure.message).toStartWith("Execution stalled")
|
||||
expect(failure.message).toStartWith(stalled)
|
||||
})
|
||||
|
||||
test("cannot be caught by the program", async () => {
|
||||
expect(await error(`try { await new Promise(() => {}) } catch { return "caught" }`)).toEqual({
|
||||
kind: "ExecutionFailure",
|
||||
message: `${stalled} (line 1, col 7)`,
|
||||
location: { line: 1, column: 7 },
|
||||
})
|
||||
})
|
||||
|
||||
test("is not stalled while a tool call that settles a hand-made promise is still running", async () => {
|
||||
expect(
|
||||
await value(`
|
||||
const { promise, resolve } = Promise.withResolvers()
|
||||
tools.host.gated({ id: 1 }).then(resolve)
|
||||
tools.host.open({ id: 1 })
|
||||
return await promise
|
||||
`),
|
||||
).toBe(1)
|
||||
})
|
||||
|
||||
test("is not stalled while a tool call it waits on is still running", async () => {
|
||||
|
||||
@@ -70,7 +70,7 @@ export type Inventory = {
|
||||
// Invariant model-facing guidance; the changing tool catalog is delivered through Instructions.
|
||||
const description = [
|
||||
"Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.",
|
||||
"`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable; all other external access goes through `tools`.",
|
||||
"`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable: there is no `setTimeout` or sleep. All other external access goes through `tools`.",
|
||||
"Within `{ code }`, the only callable tools are those explicitly listed in the Code Mode catalog instructions or returned by the `search` function. Inside `{ code }`, ignore tools shown outside the Code Mode catalog. They are not available in the Code Mode runtime.",
|
||||
'Call tools through `tools` using only exact paths and signatures from the catalog. Do not infer or normalize tool names; preserve bracket notation such as `tools.<namespace>["tool-name"](input)`.',
|
||||
"Prefer an explicit `return`; if omitted, the final top-level expression becomes the result.",
|
||||
|
||||
@@ -23,7 +23,7 @@ test("execute describes invariant Code Mode behavior", () => {
|
||||
expect(createCodeMode(new Map()).description).toBe(
|
||||
[
|
||||
"Run JavaScript in a confined Code Mode runtime to script tool calls and HTTP requests and compose their results.",
|
||||
"`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable; all other external access goes through `tools`.",
|
||||
"`fetch` is available for HTTP requests. Imports, direct filesystem access, and timers are unavailable: there is no `setTimeout` or sleep. All other external access goes through `tools`.",
|
||||
"Within `{ code }`, the only callable tools are those explicitly listed in the Code Mode catalog instructions or returned by the `search` function. Inside `{ code }`, ignore tools shown outside the Code Mode catalog. They are not available in the Code Mode runtime.",
|
||||
'Call tools through `tools` using only exact paths and signatures from the catalog. Do not infer or normalize tool names; preserve bracket notation such as `tools.<namespace>["tool-name"](input)`.',
|
||||
"Prefer an explicit `return`; if omitted, the final top-level expression becomes the result.",
|
||||
|
||||
Reference in New Issue
Block a user