different-ai/openwork · error · InterpreterRuntimeError
Failed to parse TypeScript: ${flattenDiagnosticMessageText(d
Error message
Failed to parse TypeScript: ${flattenDiagnosticMessageText(diagnostic.messageText, "\n")} What it means
parseProgram first transpiles the submitted TypeScript with the TypeScript compiler (transpileModule) and inspects diagnostics of category Error. If any parse-level diagnostic exists, it throws InterpreterRuntimeError with code "ParseError" carrying the flattened compiler diagnostic message. This is the library's front-line guard: only syntactically valid TypeScript ever reaches the sandboxed interpreter.
Source
Thrown at packages/codemode/src/interpreter/runtime.ts:127
SandboxPromise,
SandboxRegExp,
SandboxSet,
SandboxURL,
SandboxURLSearchParams,
} from "../values.js"
const parseProgram = (code: string): ProgramNode => {
const transpiled = transpileModule(`async function __codemode__() {\n${code}\n}`, {
reportDiagnostics: true,
compilerOptions: {
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
},
})
const diagnostic = transpiled.diagnostics?.find((item) => item.category === DiagnosticCategory.Error)
if (diagnostic) {
throw new InterpreterRuntimeError(
`Failed to parse TypeScript: ${flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`,
undefined,
"ParseError",
)
}
const bodyStart = transpiled.outputText.indexOf("{") + 1
const bodyEnd = transpiled.outputText.lastIndexOf("}")
const executableCode = transpiled.outputText.slice(bodyStart, bodyEnd)
const parsed = parse(executableCode, {
ecmaVersion: "latest",
sourceType: "script",
allowReturnOutsideFunction: true,
allowAwaitOutsideFunction: true,
locations: true,
}) as unknown
if (!isRecord(parsed) || parsed.type !== "Program" || !Array.isArray(parsed.body)) {View on GitHub (pinned to 2b7df46e8a)
Solutions
- Read the diagnostic text after "Failed to parse TypeScript:" — it names the exact syntax problem and often the position.
- Compile the script locally with `npx tsc --noEmit` (or run it through transpileModule) to reproduce and locate the error.
- Fix the syntax in the generated script string; check for truncated output if the code is produced by a model or template.
- If parsing user-supplied code, wrap the parse call in try/catch and surface the ParseError message to the author of the script.
Example fix
// before const script = "const x: number = ; return x" // after const script = "const x: number = 1; return x"
Defensive patterns
Strategy: try-catch
Validate before calling
import ts from "typescript"
export const validateScript = (script: string): string | null => {
const { diagnostics } = ts.transpileModule(script, { compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ESNext }, reportDiagnostics: true })
const first = diagnostics?.find((d) => d.category === ts.DiagnosticCategory.Error)
return first ? ts.flattenDiagnosticMessageText(first.messageText, "\n") : null
} Try / catch
try {
await program(script)
} catch (e) {
if (e instanceof Error && e.message.startsWith("Failed to parse TypeScript:")) {
console.error("Script syntax error:", e.message.replace("Failed to parse TypeScript: ", ""))
} else throw e
} Prevention
- Validate generated scripts with the TypeScript compiler before handing them to the runtime.
- If scripts come from an LLM, cap/verify output isn't truncated mid-statement.
- Keep template-string quoting simple; prefer building code with tagged templates or builders.
- Surface the ParseError message verbatim to script authors — it contains the exact location.
When it happens
Trigger: Passing a script string with TypeScript syntax errors (unclosed brackets, invalid tokens, bad TS type syntax) to the codemode runtime's program()/parseProgram entry point.
Common situations: Dynamically generated code snippets with template-string quoting mistakes, code copied from a different language, scripts truncated mid-expression, or TS-only syntax emitted by an LLM that doesn't compile.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to parse script as a Program node.
- Unexpected '${result.kind}' outside of a loop.
- for...of supports one declared binding.
- Unsupported for...of binding.
- for...in supports one declared binding.
AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01).
Data as JSON: /api/errors/42d648bcdc4e7b03.
Report an issue: GitHub.