Skip to content

bug(opencode): total token usage and spend decreases after archiving sessions in OpenCode #1362

Closed

Description

@nmastereal

Summary

In OpenCode, archiving a session is an organizational action that simply sets a timestamp in the session.time_archived column of ~/.local/share/opencode/opencode.db. All session metadata, messages, parts, and token usages remain fully intact in SQLite.

However, Codeburn's discovery query excludes archived sessions (WHERE time_archived IS NULL). Because opencode is treated as a standard non-durable provider, Codeburn interprets the absence of these sessions during discovery as file deletion, evicting them from the session cache (session-cache.v9) and stripping their token usage from historical metrics.

Environment

  • Codeburn Version: 0.9.23
  • Provider: OpenCode (opencode.db)
  • OS: macOS (Apple Silicon)

Steps to Reproduce

  1. Use OpenCode on any project/worktree to accumulate token spend.
  2. Run codeburn overview or codeburn sessions -p all to observe total token usage and cost.
  3. In OpenCode, archive the session (or a parent session with child subagents). This sets time_archived in opencode.db.
  4. Run codeburn overview or codeburn sessions -p all again.

Expected Behavior

Historical token usage and cost should remain unchanged. OpenCode sessions are still present in the SQLite database; archiving them should not reduce historical metrics or evict them from the cache.

Actual Behavior

Total token counts and spend decrease. The archived sessions vanish from session-cache.v9 and from all historical aggregation views.

Root Cause Analysis

  1. Discovery Query Excludes Archived Sessions:
    In src/providers/sqlite-session-parser.ts:

    export async function discoverSqliteSessions(
      config: SqliteProviderConfig,
    ): Promise<SessionSource[]> {
      // ...
      const rows = db.query<SessionRow>(
        'SELECT id, CAST(directory AS BLOB) AS directory, CAST(title AS BLOB) AS title, time_created ' +
        'FROM session WHERE time_archived IS NULL AND parent_id IS NULL ORDER BY time_created DESC',
      )
      // ...
    }

    Archived sessions fail the WHERE time_archived IS NULL clause and are omitted from the returned SessionSource[].

  2. Cache Eviction of Undiscovered Sources:
    In src/parser.ts:

    if (!readOnly && sources.length > 0 && !provider.durableSources) {
      for (const cachedPath of Object.keys(section.files)) {
        if (!allDiscoveredFiles.has(cachedPath)) {
          delete section.files[cachedPath]
          markCacheDirty(diskCache, providerName, cachedPath)
        }
      }
    }

    Because opencode does not have provider.durableSources = true, any session not returned by discoverSqliteSessions() is pruned from section.files in the cache.

  3. Subagent Tree CTE Also Filters Archived Children:
    In src/providers/sqlite-session-parser.ts (createSqliteSessionParser):

    WITH RECURSIVE session_tree(id) AS (
      SELECT id FROM session WHERE id = ?
      UNION
      SELECT child.id
      FROM session child
      JOIN session_tree parent ON child.parent_id = parent.id
      WHERE child.time_archived IS NULL
    )

    If a parent session is active but its subagents were archived (or vice versa), the subagent calls are dropped.

Proposed Fix

  1. In src/providers/sqlite-session-parser.ts:
    • In discoverSqliteSessions(): remove time_archived IS NULL from the query so all root sessions in opencode.db are discovered:
      SELECT id, CAST(directory AS BLOB) AS directory, CAST(title AS BLOB) AS title, time_created 
      FROM session 
      WHERE parent_id IS NULL 
      ORDER BY time_created DESC
    • In createSqliteSessionParser(): remove WHERE child.time_archived IS NULL from the recursive CTE queries for message and part.

Activity

  1. nmastereal commented on Sep 13, 2026

    @nmastereal
    Author

    I retried regular sessions, those dont seem to have the issue. For my use case it definitely happened after archiving sessions within a worktree and then deleting the worktree.

  2. nmastereal commented on Sep 13, 2026

    @nmastereal
    Author

    Thanks for following up! Option A is definitely the right choice here.

    In my workflow (specifically using OpenChamber), it is very typical to archive sessions and then delete the associated worktree once the task is finished. Even though the worktree directory is removed, the sessions and token data are fully preserved in opencode.db. Deleting a temporary worktree shouldn't cause historical spend or usage metrics to drop—broadening discovery to keep archived sessions in totals aligns much better with this real-world lifecycle.

  3. iamtoruk commented on Sep 14, 2026

    @iamtoruk
    Member

    Confirmed, and option A it is. Verified on a copy of a real OpenCode database with an isolated cache: three root sessions flagged archived, fresh parse.

    Cost Calls Sessions
    nothing archived $109.91 4,009 177
    three archived $109.78 3,353 174

    On current main a warm cache keeps the lifetime totals, because the daily cache carries the historical days, but the archived sessions still disappear from the sessions list, from per-project views, and from any cache rebuild. The filter dates from the original OpenCode provider with no stated reason, and OpenCode's archive is organizational, the rows stay.

    @dajiaohuang go ahead with A:

    • drop time_archived IS NULL from the root discovery query in src/providers/sqlite-session-parser.ts and from both recursive session_tree CTEs (message and part);
    • flip the two tests that pin the old behavior, excludes archived sessions (tests/providers/opencode.test.ts:186) and does not include archived child sessions in the root subtree (:760), to assert the opposite;
    • reword the "walks the unarchived subtree" line in docs/providers/opencode.md:45;
    • no parse-version bump: archived sessions return as newly discovered sources and parse from scratch.

    Kilo Code shares this parser, so the change covers it too. Plain commit message, no trailers. One changelog bullet under Unreleased / Fixed.

  4. added
    bug
    area: cli
    provider: opencode
    status: confirmed
    good first issue
    on Sep 14, 2026
  5. added a commit that references this issue on Sep 18, 2026
    f1f275e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: clibuggood first issueprovider: opencodestatus: confirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions