coding-agents: opencode v2 — fix `.` export entry and skip subagent sessions (branch ready for import)
Hi — two small fixes for hindsight-integrations/coding-agents (the opencode v2 adapter), ready as a 3-commit branch.
Process note: this repo has pull_request_creation_policy: collaborators_only, so I can't open the PR myself (cross-fork creation 404s). The branch is pushed and rebased on current main (e68ff798a):
lehneres:fix/coding-agents-opencode2-exports-subagents — https://github.com/lehneres/hindsight/tree/fix/coding-agents-opencode2-exports-subagents
If someone with access could import the branch and open the PR (or grant me collaborator access), that would be ideal. The full diff is appended below in case you'd rather apply it directly.
1. Point exports["."] at the v2 entry
Problem. When a host resolves the plugin through the package's . export — by package name, or (as opencode v2's part resolver does) from a directory whose package.json carries a name and an exports map — it landed on ./dist/index.js, the v1 entry. The v1 entry fails opencode v2's schema check:
Plugin must export a default definition with an id and an effect or setup function
Fix. exports["."].default now points at ./index.js, the v2 entry (which re-exports dist/opencode2.js). main is untouched and still names dist/index.js for v1 hosts that follow main (see the index.js header comment), so the single registered package path keeps loading the right harness on both hosts.
2. Skip subagent sessions in recall and write-back
opencode v2 has no explicit subagent marker, but it does record parentage: a subagent session is exactly a session with a parentID. Such a session is a child of a conversation the parent already tracks, so both halves of the pipeline should leave it alone — injecting into it pays for a second recall per delegation, and retaining it files a fragment of a session the parent already stores in full.
isSubagentSession()probessession.get().parentIDand fails open (retains) if the probe throws — a probe that cannot answer must not silently stop memory working.forkis deliberately NOT treated as subagent: a fork is a continuation the user chose to branch, and its conversation is as much theirs as the original's. OnlyparentIDcounts.- A single gate in the prompt hook covers both halves: a session that is never admitted also never gets an injection built, and never joins
ownSessions(the bank-isolation guard for the global event loop) for the idle write-back. - Mirrors the call the dsh adapter makes from its host's
origin === "subagent"marker.
Verification (rebased branch, opencode v2.2.0 host environment)
tsc --noEmit: clean;tsupbuild: cleannpm test(vitest, e2e excluded): 1402/1403 — the one failure (src/core/hindsight.test.ts > reflect > is bounded by its own deadline,InvalidArgumentError: Argument dispatcher must implement dispatch) also fails on cleanmain(e68ff798a) in the same environment, so it is pre-existing and unrelated.src/harness/opencode2.test.ts: 16/16, including the new subagent-skip coverage (third commit).
No version bump — left to the release process.
Full diff (e68ff798a → branch tip)
diff --git a/hindsight-integrations/coding-agents/package.json b/hindsight-integrations/coding-agents/package.json
index af547186a..0218fc97a 100644
--- a/hindsight-integrations/coding-agents/package.json
+++ b/hindsight-integrations/coding-agents/package.json
@@ -54,7 +54,7 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
- "default": "./dist/index.js"
+ "default": "./index.js"
},
"./server": "./index.js",
"./dsh": "./dist/dsh.js",
diff --git a/hindsight-integrations/coding-agents/src/harness/opencode2.test.ts b/hindsight-integrations/coding-agents/src/harness/opencode2.test.ts
index 750020292..04dd193fd 100644
--- a/hindsight-integrations/coding-agents/src/harness/opencode2.test.ts
+++ b/hindsight-integrations/coding-agents/src/harness/opencode2.test.ts
@@ -22,7 +22,7 @@ type ToolDraft = { add(tool: Record<string, unknown>): void };
* ids the prompt hook admitted, so a test that cannot say "these hooks ran, THEN this event
* arrived" would be asserting on a race rather than on the guard.
*/
-function fakeContext() {
+function fakeContext(opts: { parentOf?: (id: string) => string | undefined; getThrows?: boolean } = {}) {
const hooks: Record<string, unknown> = {};
const tools: Record<string, any>[] = [];
const skills: Record<string, any>[] = [];
@@ -57,6 +57,11 @@ function fakeContext() {
hooks[name] = cb;
return { dispose: async () => {} };
},
+ // The parentage probe: `parentID` set means a subagent session (see isSubagentSession).
+ get: async ({ sessionID }: { sessionID: string }) => {
+ if (opts.getThrows) throw new Error("session lookup unavailable");
+ return { id: sessionID, parentID: opts.parentOf?.(sessionID) };
+ },
context: async ({ sessionID }: { sessionID: string }) => {
contextCalls.push(sessionID);
return [{ type: "user", text: "hi" }];
@@ -233,6 +238,52 @@ describe("opencode2 adapter", () => {
expect(core.onSessionIdle.mock.calls).toEqual([["ses_run"], ["ses_tui"]]);
});
+ it("skips a subagent session entirely — no recall, no write-back", async () => {
+ // A subagent session is a child of a conversation we already track. Injecting into it would pay
+ // for a second recall per delegation, and retaining it would file a fragment of a session the
+ // parent stores in full — the same rule the dsh adapter applies from its own host's marker.
+ const fake = fakeContext({ parentOf: (id) => (id === "ses_child" ? "ses_parent" : undefined) });
+ const core = fakeCore();
+ await wireOpencode2Runtime(core, fake.ctx);
+ await fake.prompt()({ sessionID: "ses_child", prompt: { text: "subagent work" } });
+ // Never admitted, so the idle loop must ignore it even though the event does arrive.
+ await fake.emit({ type: "session.idle", data: { sessionID: "ses_child" } });
+ expect(core.onPrompt).not.toHaveBeenCalled();
+ expect(core.onSessionIdle).not.toHaveBeenCalled();
+ });
+
+ it("still recalls and writes back a primary session alongside a subagent one", async () => {
+ const fake = fakeContext({ parentOf: (id) => (id === "ses_child" ? "ses_parent" : undefined) });
+ const core = fakeCore();
+ await wireOpencode2Runtime(core, fake.ctx);
+ await fake.prompt()({ sessionID: "ses_child", prompt: { text: "subagent work" } });
+ await fake.prompt()({ sessionID: "ses_main", prompt: { text: "user work" } });
+ await fake.emit({ type: "session.idle", data: { sessionID: "ses_child" } });
+ await fake.emit({ type: "session.idle", data: { sessionID: "ses_main" } });
+ expect(core.onPrompt.mock.calls).toEqual([["ses_main", "user work"]]);
+ expect(core.onSessionIdle.mock.calls).toEqual([["ses_main"]]);
+ });
+
+ it("treats a fork as a primary session, not a subagent one", async () => {
+ // A fork is a continuation the user chose to branch, so its conversation is as much theirs as
+ // the original's. Only `parentID` marks a subagent, and a fork carries none.
+ const fake = fakeContext();
+ const core = fakeCore();
+ await wireOpencode2Runtime(core, fake.ctx);
+ await fake.prompt()({ sessionID: "ses_fork", prompt: { text: "branched work" } });
+ expect(core.onPrompt).toHaveBeenCalledWith("ses_fork", "branched work");
+ });
+
+ it("fails OPEN when the parentage probe throws", async () => {
+ // A probe that cannot answer must not be able to silently stop memory working. A visible loss
+ // of recall is diagnosable; one that leaves no trace is not.
+ const fake = fakeContext({ getThrows: true });
+ const core = fakeCore();
+ await wireOpencode2Runtime(core, fake.ctx);
+ await fake.prompt()({ sessionID: "ses_1", prompt: { text: "still recalls" } });
+ expect(core.onPrompt).toHaveBeenCalledWith("ses_1", "still recalls");
+ });
+
it("never writes back a session another project's plugin instance owns", async () => {
// Bank isolation, not tidiness. v2's background service hosts every open project at once and
// `event.subscribe()` is GLOBAL — a plugin loaded for project A really does receive
diff --git a/hindsight-integrations/coding-agents/src/harness/opencode2.ts b/hindsight-integrations/coding-agents/src/harness/opencode2.ts
index 89dafc0f2..7698fce7a 100644
--- a/hindsight-integrations/coding-agents/src/harness/opencode2.ts
+++ b/hindsight-integrations/coding-agents/src/harness/opencode2.ts
@@ -143,6 +143,36 @@ async function registerCompanionSkill(ctx: Opencode2Context, harness: string): P
}
}
+/**
+ * Is this session a SUBAGENT's, rather than one the user is actually working in?
+ *
+ * A subagent session is a child of a conversation we already track, so both halves of the
+ * pipeline should leave it alone: injecting into it would pay for a second recall per
+ * delegation, and retaining it would file a fragment of a session the parent already stores
+ * in full. The dsh adapter makes the same call from its own host's `origin === "subagent"`
+ * marker (src/dsh.ts); opencode2 has no such marker, but it does record parentage, and a
+ * subagent session is exactly a session with a `parentID`.
+ *
+ * `fork` is deliberately NOT treated as subagent: a fork is a continuation the user chose to
+ * branch, and its conversation is as much theirs as the original's. Only `parentID` counts.
+ *
+ * Fails OPEN (retains) if the lookup throws. A probe that cannot answer must not be able to
+ * silently stop memory working — the failure mode we can see is better than the one we cannot.
+ */
+async function isSubagentSession(
+ ctx: Opencode2Context,
+ sessionID: string,
+ harness: string
+): Promise<boolean> {
+ try {
+ const info = await ctx.session.get({ sessionID: sessionID as never });
+ return info?.parentID != null;
+ } catch (e) {
+ diag(harness, "subagent_probe_failed", { session: sessionID, error: describeError(e) });
+ return false;
+ }
+}
+
/**
* Wire a RuntimeCore onto one opencode2 plugin context and return the teardown.
*
@@ -181,7 +211,14 @@ export async function wireOpencode2Runtime(
// repeats it byte for byte. It used to be pushed into the system prompt from the `context` hook,
// which made the system prompt change between turns and broke the provider's prefix cache
// (#5914).
+ // Subagent sessions are dropped here, before they are ever admitted — the single gate covers
+ // both halves, because a session that never reaches `onPrompt` also never gets an injection
+ // built for it, and never joins `ownSessions` for the event loop below to retain.
await ctx.session.hook("prompt", async (input) => {
+ if (await isSubagentSession(ctx, input.sessionID, harness)) {
+ diag(harness, "subagent_session_skipped", { session: input.sessionID });
+ return;
+ }
ownSessions.add(input.sessionID);
await core.onPrompt(input.sessionID, input.prompt?.text ?? "");
const inj = core.getInjection(input.sessionID);
Thanks for the branch. The load half is fixed in 0.8.3 a different way: exports["./server"] → ./index.js (#6003, and verified on OpenCode 2.2.1 in #6170). OpenCode 2.2 reads ./server before ., so exports["."] stays on the v1 plugin for v1 hosts that resolve by name. The subagent skip (parentID) is still open; I'm keeping this issue open for that part.