vercel/ai · error

OpenCode compaction requires a previous turn or an explicit

Error message

OpenCode compaction requires a previous turn or an explicit model.

What it means

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.

Source

Thrown at packages/harness-opencode/src/bridge/index.ts:832

}: {
  client: OpenCodeClient;
  sessionId: string;
  start: StartMessage;
  turn: BridgeTurn;
  emit: Emit;
}): Promise<void> {
  const eventsAbort = new AbortController();
  const compactionSettled = createDeferred<void>();
  let sawCompaction = false;
  let sawBusy = false;
  let terminalError: string | undefined;
  const model = await resolveCompactionModel({
    client,
    sessionId,
    start,
  });
  if (!model) {
    throw new Error(
      'OpenCode compaction requires a previous turn or an explicit model.',
    );
  }
  const eventLoop = consumeEvents({
    client,
    sessionId,
    permissionMode: start.permissionMode,
    builtinToolFiltering: start.builtinToolFiltering,
    turn,
    state: createTranslationState(),
    emit: msg => {
      if (msg.type === 'compaction') sawCompaction = true;
      emit(msg);
    },
    signal: eventsAbort.signal,
    onEvent: event => {
      if (
        event.type === 'session.next.compaction.ended' ||

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Run at least one normal turn before compacting so the session's model can be inferred.
  2. Pass an explicit model in the compaction/start options (e.g. the model id your OpenCode server is configured with).
  3. Verify the session id points to a session with existing messages.

Example fix

// before
await bridge.compact({ sessionId: freshSessionId }); // throws
// after
await bridge.compact({ sessionId, model: 'anthropic/claude-sonnet-4' });
Defensive patterns

Strategy: validation

Validate before calling

const canCompact = sessionHasPreviousTurn(sessionId) || Boolean(options.model);
if (!canCompact) throw new Error('Provide an explicit model or run a turn before compacting');

Type guard

null

Try / catch

try {
  await bridge.compact({ sessionId });
} catch (e) {
  if (e instanceof Error && e.message.includes('requires a previous turn or an explicit model')) {
    await bridge.compact({ sessionId, model: configuredModelId });
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/0821cb1b1e7575e6. Report an issue: GitHub.