Skip to content

feat(opencode): support OpenCode 2.x - #106

Open
Akagi201 wants to merge 2 commits into
skillsynchq:mainfrom
akfork:fix/claude2opencode
Open

Akagi201 wants to merge 2 commits into
skillsynchq:mainfrom
akfork:fix/claude2opencode

Conversation

@Akagi201

Copy link
Copy Markdown

Issue: #96

What changed and why?

OpenCode 2.x moved new sessions to session_v2 / session_message (each message is one flat record), renamed opencode import to opencode session import, and flattened the import document. txcript's opencode harness only knew the 1.x tables and command, so discovery found nothing and continue --with opencode failed against a 2.x database.

This PR detects which generation the live database speaks and supports both:

  • Read: 2.x sessions are discovered, loaded, fingerprinted and archived through the new tables; each flat message is expanded back into the same {info, parts} record the 1.x path already understands, so both generations converge on one code path.
  • Write: against a 2.x database the export is flattened into the 2.x document ({type: "user", text, files} / {type: "assistant", content}) and handed to opencode session import --directory …; 1.x databases keep the legacy opencode import path.

Writing also mirrors what 2.x actually does:

  • info.projectID and info.location are schema-required but ignored by the server (it resolves the project from --directory and rewrites the stored row). txcript emits the real project row id when the read-only project table matches — including from a repository subdirectory, since OpenCode stores the canonical git/hg root — and falls back to OpenCode's own global sentinel otherwise.
  • A duplicate session id is reported as an error: 2.x rejects it with a 409 and its CLI exits 0 with empty stdout, so the save used to look successful while changing nothing.
  • Attachment payloads are normalized to the canonical base64 the import schema demands (percent-encoded data URLs and wrapped base64 from 1.x), mirroring OpenCode's own 1.x→2.x migration.
  • Inline [Image N] markers already present in the text are reused instead of duplicated, and mention offsets are written as UTF-16 code units, matching how the composer places extmarks.

Before: txcript list --from opencode → no local opencode sessions found; continue --with opencode → `opencode import` failed.
After: txcript list --from opencode lists the 2.x sessions and continue --with opencode imports them through opencode session import.

This is an independent implementation of issue #96 and overlaps in scope with #97; it additionally covers the project-id fallback, duplicate-id detection, payload normalization and marker/offset edge cases listed above.

Verification

  • cargo test -p txcript --lib --features opencode opencode: 14 passed.
  • cargo test --workspace --all-features: 489 passed, 0 failed.
  • cargo clippy --workspace --all-features and cargo fmt --check clean.

2.x writes new sessions to `session_v2`/`session_message`, renamed `opencode import` to `opencode session import`, and takes a different import document. Detect the live schema and discover, load, fingerprint and archive 2.x sessions through the new tables; saving flattens the export into the 2.x document when the database speaks it.

The import path handles what 2.x actually does: the project id is read from the `project` table (matching worktree ancestors, so repo subdirectories work) and falls back to OpenCode's own `global` sentinel, since the server derives the project from --directory; a duplicate id is reported as an error because the CLI exits 0 after a 409; inline `[Image N]` markers are reused instead of duplicated; mention offsets use UTF-16 code units like the composer; and attachment payloads are normalized to canonical base64 the way OpenCode's own 1.x migration does.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 04:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Absorbs what upstream skillsynchq#97 found against the 2.x source:

- 2.x renamed the built-in tools and input keys (`bash` is `shell` with `timeout`/`background`, `filePath` is `path`, `task` is `subagent`, `Skill` is `skill` with `id`, `WebSearch` is `websearch`, `grep` takes `include`/`limit` and the inverse `caseSensitive`). Map them both ways: a 2.x session reads as the tools txcript knows and writes back with the names OpenCode's UI renders.
- Force tool input to an object; 2.x validates a record, and a freeform Codex code-mode call would otherwise reject the whole import.
- Carry the tool call's timing onto the part and write the full token object the schema validates, instead of zeroed timestamps and a partial 1.x object.
- Choose the import document and command from `opencode --version`, not the database: an upgraded CLI migrates an old database on its next start, so the import can legitimately run first. This replaces the `opencode session list` bootstrap.
- The text form is the 2.x `session export` document and `from_text` reads both layouts, so the WASM converters round-trip with the current CLI.
- Records with no 1.x shape (`compaction`, `idle`, `shell`, `system`) ride through a load and an export unchanged, and synthetic user text becomes its own `synthetic` record.

Verified against sst/opencode (`packages/core/src/tool/plugin/*.ts`, `packages/schema/src/session-message.ts`).
@Akagi201

Copy link
Copy Markdown
Author

Follow-up pushed (d194402) — after reviewing #97 and re-verifying its findings against the OpenCode 2.x source, this branch now also covers:

  • Tool renames. 2.x renamed the built-in tools and input keys (bash → shell with timeout/background, filePath → path, task → subagent, Skill → skill with id, WebSearch → websearch, and grep takes include/limit plus the inverse caseSensitive). Both directions are mapped, so a 2.x session reads back as Bash/Edit/… and writes carry the names the 2.x UI renders.
  • Import command selection from the CLI. save now reads opencode --version rather than the database schema: an upgraded CLI migrates an old database on its next start, so the import can legitimately run before that has happened. This replaces the opencode session list bootstrap.
  • Text form. to_text emits the 2.x session export document and from_text reads both layouts (flat 2.x and the 1.x export), so the WASM converters round-trip with the current CLI.
  • Fidelity. Tool input is forced to an object (a freeform Codex code-mode input would otherwise fail schema validation for the whole document), per-tool timestamps are carried onto the part, per-message token objects are written in the full shape the schema validates, records with no 1.x shape (compaction, idle, shell, system, …) ride through a load and an export unchanged, and synthetic user text becomes its own synthetic record.

Credit to @acaprino — the tool-rename table and the CLI-generation signal came out of #97. Both are verified here against packages/core/src/tool/plugin/*.ts and packages/schema/src/session-message.ts.

Verification: 20 opencode library tests (was 14), 495 workspace tests, cargo clippy --workspace --all-features and cargo fmt --check clean.

This branch has not been deployed

No deployments
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.

2 participants