Repository navigation
Description
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
- Use OpenCode on any project/worktree to accumulate token spend.
- Run
codeburn overvieworcodeburn sessions -p allto observe total token usage and cost. - In OpenCode, archive the session (or a parent session with child subagents). This sets
time_archivedinopencode.db. - Run
codeburn overvieworcodeburn sessions -p allagain.
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
-
Discovery Query Excludes Archived Sessions:
Insrc/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 NULLclause and are omitted from the returnedSessionSource[]. -
Cache Eviction of Undiscovered Sources:
Insrc/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
opencodedoes not haveprovider.durableSources = true, any session not returned bydiscoverSqliteSessions()is pruned fromsection.filesin the cache. -
Subagent Tree CTE Also Filters Archived Children:
Insrc/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
- In
src/providers/sqlite-session-parser.ts:- In
discoverSqliteSessions(): removetime_archived IS NULLfrom the query so all root sessions inopencode.dbare 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(): removeWHERE child.time_archived IS NULLfrom the recursive CTE queries formessageandpart.
- In
Activity
nmastereal commentedon Sep 13, 2026
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.
nmastereal commentedon Sep 13, 2026
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.
iamtoruk commentedon Sep 14, 2026
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.
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:
time_archived IS NULLfrom the root discovery query insrc/providers/sqlite-session-parser.tsand from both recursivesession_treeCTEs (message and part);excludes archived sessions(tests/providers/opencode.test.ts:186) anddoes not include archived child sessions in the root subtree(:760), to assert the opposite;Kilo Code shares this parser, so the change covers it too. Plain commit message, no trailers. One changelog bullet under Unreleased / Fixed.
fix(opencode): stop dropping archived sessions from discovery and sub…