{"record":{"id":"0821cb1b1e7575e6","repo":"vercel/ai","slug":"opencode-compaction-requires-a-previous-turn-or-an","errorCode":null,"errorMessage":"OpenCode compaction requires a previous turn or an explicit model.","messagePattern":"OpenCode compaction requires a previous turn or an explicit model\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/harness-opencode/src/bridge/index.ts","lineNumber":832,"sourceCode":"}: {\n  client: OpenCodeClient;\n  sessionId: string;\n  start: StartMessage;\n  turn: BridgeTurn;\n  emit: Emit;\n}): Promise<void> {\n  const eventsAbort = new AbortController();\n  const compactionSettled = createDeferred<void>();\n  let sawCompaction = false;\n  let sawBusy = false;\n  let terminalError: string | undefined;\n  const model = await resolveCompactionModel({\n    client,\n    sessionId,\n    start,\n  });\n  if (!model) {\n    throw new Error(\n      'OpenCode compaction requires a previous turn or an explicit model.',\n    );\n  }\n  const eventLoop = consumeEvents({\n    client,\n    sessionId,\n    permissionMode: start.permissionMode,\n    builtinToolFiltering: start.builtinToolFiltering,\n    turn,\n    state: createTranslationState(),\n    emit: msg => {\n      if (msg.type === 'compaction') sawCompaction = true;\n      emit(msg);\n    },\n    signal: eventsAbort.signal,\n    onEvent: event => {\n      if (\n        event.type === 'session.next.compaction.ended' ||","sourceCodeStart":814,"sourceCodeEnd":850,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/harness-opencode/src/bridge/index.ts#L814-L850","documentation":"runCompaction resolves a model for the summarize/compaction request via resolveCompactionModel, which derives it from the session's previous turn or an explicit model in start options. If neither is available it throws this error. Compaction therefore cannot run on a fresh session with no prior activity and no model specified.","triggerScenarios":"Triggering compaction on a brand-new OpenCode session that has no prior assistant turn (so no model can be inferred) without passing an explicit model in the start/options message.","commonSituations":"Calling compact/summarize immediately after session creation; resuming with a session id whose history is empty; forgetting to set the model option when driving compaction programmatically.","solutions":["Run at least one normal turn before compacting so the session's model can be inferred.","Pass an explicit model in the compaction/start options (e.g. the model id your OpenCode server is configured with).","Verify the session id points to a session with existing messages."],"exampleFix":"// before\nawait bridge.compact({ sessionId: freshSessionId }); // throws\n// after\nawait bridge.compact({ sessionId, model: 'anthropic/claude-sonnet-4' });","handlingStrategy":"validation","validationCode":"const canCompact = sessionHasPreviousTurn(sessionId) || Boolean(options.model);\nif (!canCompact) throw new Error('Provide an explicit model or run a turn before compacting');","typeGuard":"null","tryCatchPattern":"try {\n  await bridge.compact({ sessionId });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('requires a previous turn or an explicit model')) {\n    await bridge.compact({ sessionId, model: configuredModelId });\n  } else throw e;\n}","preventionTips":["Always pass an explicit model when triggering programmatic compaction.","Only compact sessions with at least one completed turn.","Track session lifecycle so fresh sessions are not compacted immediately."],"tags":["compaction","opencode","configuration","model"],"backgroundTag":"missing-model-config","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}