vercel/ai · critical

OpenCode compaction failed: ${formatError(compacted.error)}

Error message

OpenCode compaction failed: ${formatError(compacted.error)}

What it means

runCompaction calls the OpenCode summarize API (legacySessionSummarize) with the resolved model. If the API returns an error result, the bridge aborts the event subscription and throws this error containing the server's formatted error. The compaction request itself was rejected or failed server-side.

Source

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

      } else if (sawBusy && status === 'idle') {
        compactionSettled.resolve();
        return true;
      }
      if (event.type === 'session.error') {
        terminalError = formatError(event.properties?.error ?? event);
        compactionSettled.resolve();
        return true;
      }
    },
  });
  const compacted = await legacySessionSummarize({
    client,
    sessionId,
    model,
  });
  if (compacted.error) {
    eventsAbort.abort();
    throw new Error(
      `OpenCode compaction failed: ${formatError(compacted.error)}`,
    );
  }
  await Promise.race([compactionSettled.promise, sleep(250)]);
  eventsAbort.abort();
  await eventLoop.catch(() => {});
  if (terminalError) throw new Error(terminalError);
  if (!sawCompaction) {
    emit({
      type: 'compaction',
      trigger: 'manual',
      summary: '',
      harnessMetadata: {
        opencode: { missingSummary: true },
      },
    });
  }
}

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Read formatError output in the message for the server's reason and fix that (model id, auth, session state).
  2. Confirm the model passed to compaction is configured and available on the OpenCode server.
  3. Ensure the session has enough history to summarize and is not mid-turn.
  4. Retry after transient provider failures.

Example fix

// before: model not configured on server
await bridge.compact({ sessionId, model: 'nonexistent/model' });
// after
await bridge.compact({ sessionId, model: 'anthropic/claude-sonnet-4' });
Defensive patterns

Strategy: try-catch

Validate before calling

const providerModels = await fetch(`${base}/model`).then(r => r.json());
if (!providerModels.includes(model)) throw new Error(`Model ${model} not available on OpenCode server`);

Type guard

function isCompactionError(e: unknown): e is Error {
  return e instanceof Error && e.message.startsWith('OpenCode compaction failed:');
}

Try / catch

try {
  await bridge.compact({ sessionId, model });
} catch (e) {
  if (isCompactionError(e)) {
    console.error('Summarize error detail:', e.message.replace('OpenCode compaction failed: ', ''));
    // fix model/auth per detail, then retry
  } else throw e;
}

Prevention

When it happens

Trigger: Calling compaction when legacySessionSummarize resolves compacted.error — invalid model id for the summarize endpoint, session not in a compactable state, OpenCode server rejecting the request (auth, HTTP 4xx/5xx), or provider failure during summarization.

Common situations: Model id not available on the OpenCode server's provider config; summarizing a session with nothing to summarize; provider API key/quota errors during summarization; OpenCode version where the summarize endpoint changed.

Related errors


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