Skip to content

feat(importers): import opencode sessions - #345

Merged
upsetbit merged 8 commits into
masterfrom
feat/opencode-importer
Dec 4, 2026
Merged

upsetbit merged 8 commits into
masterfrom
feat/opencode-importer

Conversation

@upsetbit

Copy link
Copy Markdown
Contributor

Summary

  • Import OpenCode sessions from opencode.db (and opencode-<channel>.db) into the CLI. Each session is a JSONL projection, so the database — which also holds credentials — is not copied into the raw store.
  • Accept opencode on session push and store the raw object as .jsonl. The panel filter lists the agent as OpenCode.

Test plan

  • go test ./internal/importers/opencode/ ./internal/paths/ ./internal/cli/ -count=1
  • go test ./internal/server/handlers/ -count=1 -run 'TestValidatePushedAgent|TestRawKeyOpenCode'
  • go test ./internal/panel/ -count=1 -run 'TestDisplayAgent|TestSessionsFeedRowSwapsSidePanel'
  • Imported the local ~/.local/share/opencode/opencode.db into a temporary PROSA_HOME and checked that each session's raw artifact is a .jsonl

@upsetbit upsetbit left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by GPT-6.2-Astra (gpt-6.2-astra, reasoning effort max) via CODEX_HOME=~/.codex-mz. Thread 01a22d52-7992-7d72-98de-69bc675a4657. Reviewed 776f5435.

The importer uses the source session IDs, gives v2 rows precedence, maps cache and reasoning tokens correctly, and keeps credential-table rows out of the JSONL accepted by the server. Sync can leave session metadata stale, drop measured requests, and underprice long prompts.

OC-01 major internal/importers/opencode/project.go
projectionLines omits parent_id, directory, model, time_created, and time_updated. Changing only one of those fields leaves the JSONL hash unchanged, so LastHash skips the local update. Even after --overwrite refreshes the local row, the server sees the same raw hash and skips the push. Include the source session metadata in the hashed JSONL.

OC-02 major internal/importers/opencode/project.go
The session-wide choice drops measured usage when one assistant message has a step-finish and another has only message-level tokens. With zero session-row counters, the importer labels the subtotal complete. If the step reports zero and the other message reports positive tokens, it skips the entire session as no_usage. Choose the usage source per assistant message so each measured inference counts once.

OC-03 major internal/importers/opencode/project.go
requestFrom leaves PromptTokens unset even when the cache-inclusive prompt length is known. The pricing engine uses that field to select the long-context rate. An OpenCode gemini-2.5-pro request with 200,001 input tokens receives the short-context rate. Populate PromptTokens from the cache-inclusive input when the breakdown is available.

OC-04 major internal/importers/opencode/parse.go
All v2 messages and parts are already in memory when loadLayout loads the entire legacy history. The duplicate-ID check runs afterward. A migrated database with a large message and part payload in each layout retains both before the first session is written. Filter shadowed legacy IDs in SQL and process one session or a bounded batch at a time.

OC-05 major internal/importers/opencode/parse.go
Dropping a malformed v2 message also removes its ID from the index used by loadParts. Valid text and tool-result parts attached to that message then disappear from both the raw JSONL and the projected turns. The v2 row still supplies the message ID, type, sequence, and timestamp. Retain that envelope so intact parts survive the malformed payload.

OC-06 minor internal/importers/opencode/import_db.go
The marker gives every historical database hash its own state_seen entry. After importing a database, changing a message, and restoring the original database from backup, the old marker short-circuits the import and leaves the newer message in the store. Key the marker by a stable source database identity and compare its most recently imported hash.

OC-07 minor internal/importers/opencode/project.go
Message timestamps override populated time_created and time_updated. A session created May 1 that contains an April 30 message is filed under April. The canonical mapping gives the session-row timestamps precedence. Use the earliest or latest message time only when the corresponding row timestamp is missing.

OC-08 minor internal/importers/opencode/project.go
When the session row has no model, every message is inspected. A user or model-switched record carrying a model can set Session.Model before the first assistant response supplies the model actually used. Restrict this fallback to assistant records and their model.id or modelID fields.

OC-09 minor internal/importers/opencode/import_db.go
If only a credential or event row changes, the database hash changes while every session projection still matches. With zero session writes and zero policy skips, the result is Skipped: false, so prosa sync --json reports an import. Return a skipped result when the database scan writes no sessions.

@upsetbit

Copy link
Copy Markdown
Contributor Author

Addressed the Astra findings in a51b125.

  • OC-01: the hashed JSONL starts with the session row, so a change to parent, directory, model, or timestamps reimports and pushes.
  • OC-02: each assistant message uses its own step-finish tokens, or its message tokens when it has no step-finish.
  • OC-03: PromptTokens is the cache-inclusive input.
  • OC-04: legacy ids already in session_v2 are excluded in SQL, and each session is loaded on its own.
  • OC-05: a malformed message is kept as a null envelope so its parts stay in the raw and in the turns.
  • OC-06: the state_seen marker is keyed by the database path and stores the latest hash.
  • OC-07: time_created and time_updated win when the row has them.
  • OC-08: the model fallback reads assistant messages.
  • OC-09: a scan that writes no session is a skip.

upsetbit and others added 3 commits December 4, 2026 18:58
A compaction record is a summarization call with its own model and
tokens, and the session-row counters include it. Each compaction now
yields a request, so a session with one can reach complete coverage and
a zero session row keeps its compaction tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upsetbit

upsetbit commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Review follow-ups in 0fe0d7f..6fbb13f.

  • compaction records are summarization calls with their own model and tokens, and the session-row counters include them. Each one now yields a request, so a session with a compaction can reach complete coverage, and a zero session row keeps the compaction tokens. Covered by TestImportCountsCompactionUsage.
  • docs/sources/opencode.md and canonical-session.md describe compaction usage.
  • OpenCode is now named in the agent lists in README, INTENT, concepts.md, store.md, importers.md, and the CLI design brief.

@upsetbit
upsetbit merged commit d6208ce into master Dec 4, 2026
5 checks passed
@upsetbit
upsetbit deleted the feat/opencode-importer branch December 4, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant