Skip to content

OpenCode compaction detection not implemented in reader #148

@willwashburn

Description

@willwashburn

Summary

Burn's OpenCode session reader (packages/reader/src/opencode.ts) does not detect context compaction events, while Claude Code and Codex readers already support this. This means burn waste --kind compaction cannot report compaction losses for OpenCode sessions.

Background

OpenCode emits compaction events as message parts with type: "compaction" (see OpenCode compaction.ts) and fires a session.compacted bus event. The CompactionEvent type (packages/reader/src/types.ts:307-318) is already defined and used by other readers.

Existing implementations

  • Claude Code: Detects compact_boundary system markers (claude.ts:330)
  • Codex: Detects type: "compacted" session records (codex.ts:471)

Implementation Steps

  1. Detect compaction parts in parseOpenCodeSession and parseOpenCodeSessionIncremental:

    • Parse OpenCode session JSONL for message parts with type: "compaction"
    • Extract sessionID, messageID, auto flag from the compaction part
  2. Anchor to preceding turn:

    • Match each compaction event to the immediately preceding assistant turn (store precedingMessageId)
    • Capture tokensBeforeCompact from the preceding turn's cache read tokens (same pattern as Claude/Codex)
  3. Emit CompactionEvent:

    • Push CompactionEvent objects to the events array returned by the parser
    • Follow the existing CompactionEvent schema (types.ts:307-318)
  4. Wire through ledger pipeline:

    • Existing appendCompactionsCompactionLinequeryCompactionsburn waste --kind compaction will automatically pick up OpenCode compaction events once emitted by the reader

References

  • OpenCode CompactionPart type: SDK types
  • Burn CompactionEvent interface: packages/reader/src/types.ts:307-318
  • Claude compaction detection: packages/reader/src/claude.ts:330
  • Codex compaction detection: packages/reader/src/codex.ts:471
  • Ledger compaction writer: packages/ledger/src/writer.ts:83

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions