{"record":{"id":"fc2ad4b4c4fe451e","repo":"different-ai/openwork","slug":"unexpected-result-kind-outside-of-a-loop","errorCode":null,"errorMessage":"Unexpected '${result.kind}' outside of a loop.","messagePattern":"Unexpected '(.+?)' outside of a loop\\.","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/interpreter/runtime.ts","lineNumber":688,"sourceCode":"    // Run the program body in its own module scope on top of the builtin global scope, so\n    // top-level declarations (`let undefined = 5`, `const Object = ...`) shadow builtins like\n    // JS module scope, instead of colliding with the seeded globals.\n    this.pushScope()\n    return Effect.gen(function* () {\n      self.hoistFunctions(program.body)\n      let value: unknown = undefined\n      let returned = false\n      for (const statement of program.body) {\n        const result = yield* self.evaluateStatement(statement)\n\n        if (result.kind === \"return\") {\n          value = result.value\n          returned = true\n          break\n        }\n\n        if (result.kind === \"break\" || result.kind === \"continue\") {\n          throw new InterpreterRuntimeError(`Unexpected '${result.kind}' outside of a loop.`, statement)\n        }\n\n        if (result.kind === \"value\") {\n          self.lastValue = result.value\n        }\n      }\n      if (!returned) value = self.lastValue\n\n      // The program body runs inside an implicit async function, so a returned promise\n      // resolves before crossing the data boundary - `return tools.ns.tool(...)` works\n      // without an explicit await, exactly as in JS.\n      if (value instanceof SandboxPromise) value = yield* self.settlePromise(value)\n      yield* self.drainPendingSettlements()\n      return value\n    }).pipe(Effect.ensuring(Effect.sync(() => self.popScope())))\n  }\n\n  // Awaits every fiber-backed promise the program abandoned (fire-and-forget tool calls), so","sourceCodeStart":670,"sourceCodeEnd":706,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/interpreter/runtime.ts#L670-L706","documentation":"When the interpreter's run loop finishes executing a statement, a break or continue result escaping to top level (not inside a loop) is invalid control flow. The interpreter detects this and throws, mirroring JavaScript's own SyntaxError for stray break/continue.","triggerScenarios":"A break or continue statement positioned outside any loop body in CodeMode code — e.g. top-level break, or break inside an if that is not enclosed by a for/while the interpreter recognizes, including break inside non-loop constructs like function bodies at top level.","commonSituations":"Hand-written or LLM-generated code with a mis-nested break/continue; refactoring that removed the enclosing loop but left the break; translating code from languages with labeled block breaks.","solutions":["Remove the stray break/continue statement","Ensure the statement is nested inside a for/while loop","Replace the early exit with a conditional or return"],"exampleFix":"// before\nif (done) break;\n// after\nif (done) return; // or restructure inside a loop","handlingStrategy":"validation","validationCode":"// crude check: a break/continue not preceded by a loop keyword in the same block scope\nfor (const m of code.matchAll(/\\b(break|continue)\\b/g)) {\n  const before = code.slice(0, m.index);\n  if (!/(for|while)\\s*\\(/.test(before.split('}').pop() ?? '')) {\n    throw new Error(`'${m[1]}' may be outside of a loop`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  result = invokeCodeMode(code);\n} catch (e) {\n  if (String(e.message).includes(\"outside of a loop\")) {\n    // flag the stray break/continue for rewrite\n  } else throw e;\n}","preventionTips":["Ensure every break/continue sits inside a for/while loop","Prefer return or conditionals over control-flow jumps at top level","Review refactors that remove loops for leftover break/continue"],"tags":["codemode","control-flow","break-continue","syntax"],"backgroundTag":"illegal-break-continue","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}