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

  1. Read the diagnostic text after "Failed to parse TypeScript:" — it names the exact syntax problem and often the position.
  2. Compile the script locally with `npx tsc --noEmit` (or run it through transpileModule) to reproduce and locate the error.
  3. Fix the syntax in the generated script string; check for truncated output if the code is produced by a model or template.
  4. 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

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

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/42d648bcdc4e7b03. Report an issue: GitHub.