Skip to content

Commit b57fdc7

Browse files
committed
OpenCode 2.x plugin support: dual v1/v2 adapter
Issue #15: opencode 2.0 rewrote the plugin system; the v1-shaped plugin fails to load on v2. # Project opencode v2 support (issue #15) # Changes Dual-shape plugin entry: a merged default export ({ id, setup, server }) loads on opencode 1.x (reads default.server) and 2.x (reads default.setup, validates the id, strips excess keys). Each host adapter (src/opencode/v1.ts, src/opencode/v2.ts) adapts its SDK to a shared HostCapabilities seam (src/capabilities.ts); all behavior lives in the shared runtime (src/runtime.ts), so both versions get identical features where the host API allows: tools, memory, nudges, chat, watchers, extraction pipeline, wrap-up commands. V2-specific work: tool schemas pre-converted to JSON Schema (v2's zod detection fails on ours); nudges injected through the context hook (the generate hook never fires for normal prompts - probed live); plugin-state persistence (runtime_state table: extraction buffers, child bookkeeping, wrap-up arms, watcher definitions) rehydrated on same-process reload and pruned on restart except the resumed session; refcounted embedding-model pool shared across the daemon's location instances; chat poller re-hosts its journaled session set after a reload so pending mail wakes the session; child events forwarded by ID for below-root launches; wrap-up commands registered in code with $ARGUMENTS substitution and stale-file removal. V2 degrades (documented in README + docs/dev/features/opencode-plugin.md): toasts, compaction trigger, app/tab exit (upstream anomalyco/opencode#50984), noReply deliveries, -c listing, extraction-child deletion. Chat hardening from the same review effort: the reaper leaves name claims so a crash-resumed session reclaims its identity; the chat list CLI gained a session id column, active-only default, and --stale [N|all]; publish.yml runs the full CI gate across the dual-Bun matrix; CI gained a bare-named test summary job to satisfy the main-branch ruleset's required "test" status context. The design/plan documents (docs/plans/opencode-v2-plugin.md, plugin-state-persistence.md) graduated into docs/dev/features/opencode-plugin.md and are removed per the plans convention; this squash commit is their git archive.
1 parent 60ece01 commit b57fdc7

56 files changed

Lines changed: 4518 additions & 1470 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,29 @@ on:
77
branches: [main]
88

99
jobs:
10-
test:
10+
# The matrix legs carry the real gate (typecheck, tests, markdownlint) per
11+
# Bun runtime: opencode v1 embeds Bun 1.3.14; v2 embeds 1.4.2. The plugin
12+
# runs inside the host's embedded runtime, so both must pass.
13+
test-matrix:
1114
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
bun-version: ["1.3.14", "1.4.2"]
1218
steps:
1319
- uses: actions/checkout@v4
1420
- uses: oven-sh/setup-bun@v2
1521
with:
16-
bun-version: "1.3.14"
22+
bun-version: ${{ matrix.bun-version }}
1723
- run: bun install
1824
- run: bunx tsc -p tsconfig.check.json
1925
- run: bun test tests/*.test.ts
2026
- run: bunx markdownlint-cli2
27+
28+
# Bare-named summary job: the main branch ruleset requires a status check
29+
# with context "test", and matrix legs report as "test (1.3.14)" etc.,
30+
# which never satisfies a bare "test" requirement.
31+
test:
32+
needs: test-matrix
33+
runs-on: ubuntu-latest
34+
steps:
35+
- run: echo "matrix legs green"

‎.github/workflows/publish.yml‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,33 @@ permissions:
1313
issues: write
1414

1515
jobs:
16+
# Release gate: the same checks CI runs (the tag push itself does not
17+
# trigger ci.yml), across the dual-runtime matrix a release must pass on
18+
# (opencode v1 embeds Bun 1.3.14; v2 embeds 1.4.2).
19+
test:
20+
runs-on: ubuntu-latest
21+
strategy:
22+
matrix:
23+
bun-version: ["1.3.14", "1.4.2"]
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: oven-sh/setup-bun@v2
27+
with:
28+
bun-version: ${{ matrix.bun-version }}
29+
- run: bun install
30+
- run: bunx tsc -p tsconfig.check.json
31+
- run: bun test tests/*.test.ts
32+
- run: bunx markdownlint-cli2
33+
1634
publish:
35+
needs: test
1736
runs-on: ubuntu-latest
1837
steps:
1938
- uses: actions/checkout@v4
2039
- uses: oven-sh/setup-bun@v2
2140
with:
2241
bun-version: "1.3.14"
2342
- run: bun install
24-
- run: bun test tests/*.test.ts
2543
# OIDC publish needs npm >= 11.5.1. setup-node installs into the
2644
# user-writable tool cache (the runner's stock node in /usr/local is
2745
# root-owned, so npm can't self-upgrade there). Deliberately NO

‎README.md‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ everything runs on your machine.
2020
{ "plugin": ["@jeffober/thatch"] }
2121
```
2222

23-
> **OpenCode 2.0 is not yet supported.** OpenCode 2.0 replaced the plugin API
24-
> (plugins now need a default export with `id`/`setup` instead of the v1
25-
> `server` hook object). Thatch currently targets the 1.18.x plugin API and
26-
> will fail to load on 2.0. Upgrade to 2.0 only after a thatch release adds
27-
> v2 support.
28-
2923
On next start, OpenCode npm-installs thatch and its tools are available
30-
immediately. For async extraction (child sessions run in the background):
24+
immediately. Works with both opencode 1.x and 2.x (the same package supports
25+
both plugin APIs). Until the next release ships, npm's `latest` still
26+
targets 1.x only. For async extraction (child sessions run in the
27+
background):
3128

3229
```bash
3330
export OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS=true
@@ -36,6 +33,32 @@ export OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS=true
3633
Without this env var, extraction still works - the child session runs
3734
synchronously (fire-and-forget) instead of asynchronously.
3835

36+
### What works in opencode 2.x
37+
38+
The v2 plugin API is missing a few surfaces the v1 API had. On 2.x:
39+
40+
- **No toasts.** Toasts are dropped on v2 (no publish surface). The
41+
model-facing nudges they accompanied - watcher and chat wake nudges -
42+
still arrive; the extraction-result and other TUI-only toasts have no
43+
replacement.
44+
- **Plugin state survives reloads.** Editing the plugin (or `opencode
45+
plugin update`) reloads it, but extraction buffers, watcher
46+
registrations, and armed wrap-ups are journaled and restored; a process
47+
restart restores the state of a resumed (`-c`/`-s`) session and prunes
48+
the rest.
49+
- **Extraction children are visible.** The fact-extractor child sessions are
50+
top-level sessions the host cannot delete, so they accumulate in the
51+
session picker, and "continue last session" (`-c`) can land in one after
52+
any session that triggered extraction.
53+
- **`/thatch/compact` does not auto-compact.** The checklist and memory
54+
flush run, but the compaction itself cannot be triggered from the plugin
55+
API - run `/compact` yourself after the wrap-up completes. `/thatch/exit`
56+
cannot auto-exit for the same reason.
57+
- **No `-c` session listing.** The chat resume listing degrades.
58+
59+
Everything else - tools, memory, nudges, chat, watchers - behaves the same
60+
on both versions.
61+
3962
Then **prime your project memory** by running `thatch prime` in your project directory.
4063
This launches an `opencode` session to build an initial map of the code base
4164
and seed the memory.

‎bin/thatch‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ function usage(): never {
4949
Full conversation as OpenAI chat-completions JSONL
5050
thatch session search <query> [--session <id>] [--regex] [--limit N]
5151
JSONL of matching parts across sessions (decoded content)
52-
thatch chat list [--stale N|all] Registered chat sessions, grouped Active/Stale:
53-
name, age, status, project, topic.
54-
--stale N (days) hides stale sessions
55-
older than N; default 1. --stale all
56-
shows every stale session.
52+
thatch chat list [--stale N|all] Registered chat sessions. Default shows
53+
only active (fresh-heartbeat) sessions:
54+
name, session id, age, status, project,
55+
topic. --stale adds the stale section:
56+
N bounds it to sessions seen in the
57+
last N days (default 1); all shows
58+
every stale session.
5759
thatch chat tail [--once] [--limit N|all] [--match RE]
5860
[--from NAME] [--to NAME] [--since DT] [--until DT]
5961
Follow cross-session chat as a JSONL
@@ -415,7 +417,7 @@ async function sessionCommand(rest: string[]): Promise<void> {
415417
// ---------------------------------------------------------------------------
416418

417419
/** Column headers for the `chat list` roster, in display order. */
418-
const CHAT_ROSTER_HEADER = ["NAME", "AGE", "STATUS", "PROJECT", "TOPIC"];
420+
const CHAT_ROSTER_HEADER = ["NAME", "SESSION ID", "AGE", "STATUS", "PROJECT", "TOPIC"];
419421

420422
/**
421423
* Renders the `chat list` roster as two sections - Active and Stale - each
@@ -436,6 +438,7 @@ function formatChatRoster(sessions: ChatSessionRow[], staleCapMs: number | null
436438
const renderTable = (rows: ChatSessionRow[]) => {
437439
const cells = (r: ChatSessionRow) => [
438440
r.name,
441+
r.session_id,
439442
humanAge(r.last_seen),
440443
chatLiveness(r),
441444
r.project ?? "unknown",
@@ -447,9 +450,10 @@ function formatChatRoster(sessions: ChatSessionRow[], staleCapMs: number | null
447450
const align = (line: string[]) => line.map((c, i) => (i < line.length - 1 ? c.padEnd(widths[i]) : c));
448451
const header = align(CHAT_ROSTER_HEADER).map((h) => paint("1", h)).join(" ");
449452
const body = rows.map((r) => {
450-
const [name, age, status, project, topic] = align(cells(r));
453+
const [name, sessionID, age, status, project, topic] = align(cells(r));
451454
return [
452455
paint("32", name),
456+
paint("2", sessionID),
453457
paint("2", age),
454458
paint(status === "stale" ? "31" : "32", status),
455459
project,
@@ -491,21 +495,32 @@ async function chatCommand(rest: string[]): Promise<void> {
491495
// sessions from burying the fresh signal. --stale all restores the
492496
// unbounded view. A bad value dies loudly rather than silently
493497
// shrinking the roster.
494-
const staleArg = flagValue(restArgs, "--stale");
498+
// Default: active sessions only. --stale [N|all] opts into the
499+
// stale section - bare --stale keeps the one-day display cap, N
500+
// bounds it, all removes it. The value is parsed by hand (not
501+
// flagValue) because bare --stale is meaningful.
502+
const showStale = restArgs.includes("--stale");
503+
const staleIdx = restArgs.indexOf("--stale");
504+
const staleNext = showStale ? restArgs[staleIdx + 1] : undefined;
495505
let staleCapMs: number | null = 24 * 60 * 60 * 1000;
496-
if (staleArg !== undefined) {
497-
if (staleArg === "all") staleCapMs = null;
498-
else if (!/^\d+(\.\d+)?$/.test(staleArg) || Number(staleArg) <= 0) {
499-
console.error(`error: --stale expects a positive number of days or "all", got "${staleArg}"`);
506+
if (staleNext !== undefined) {
507+
if (staleNext === "all") staleCapMs = null;
508+
else if (!/^\d+(\.\d+)?$/.test(staleNext) || Number(staleNext) <= 0) {
509+
console.error(`error: --stale expects a positive number of days or "all", got "${staleNext}"`);
500510
process.exit(1);
501-
} else staleCapMs = Number(staleArg) * 24 * 60 * 60 * 1000;
511+
} else staleCapMs = Number(staleNext) * 24 * 60 * 60 * 1000;
502512
}
503-
const sessions = db.listChatSessions();
513+
let sessions = db.listChatSessions();
514+
if (!showStale) sessions = sessions.filter((r) => chatLiveness(r) === "fresh");
504515
if (sessions.length === 0) {
505-
console.log("No sessions registered. Sessions join the directory automatically on their first idle (opencode), or via chat_register.");
516+
console.log(
517+
showStale
518+
? "No sessions registered. Sessions join the directory automatically on their first idle (opencode), or via chat_register."
519+
: "No active sessions. (Stale sessions are hidden; run with --stale to include them.)",
520+
);
506521
break;
507522
}
508-
console.log(formatChatRoster(sessions, staleCapMs));
523+
console.log(formatChatRoster(sessions, showStale ? staleCapMs : null));
509524
break;
510525
}
511526

0 commit comments

Comments
 (0)