@open-cr-agent/runtime-opencode 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ import type { OpencodeClient } from "@opencode-ai/sdk/v2";
2
+ export interface DoneToolApi {
3
+ event: Pick<OpencodeClient["event"], "subscribe">;
4
+ permission: Pick<OpencodeClient["permission"], "list" | "reply">;
5
+ }
6
+ export declare function endSessionsAtDoneTool(api: DoneToolApi, doneTool: string, signal: AbortSignal, resubscribeMs?: number): Promise<void>;
@@ -0,0 +1,34 @@
1
+ import { setTimeout as delay } from "node:timers/promises";
2
+ const RESUBSCRIBE_MS = 1_000;
3
+ // OpenCode hands every tool result back to the model, the done tool's too:
4
+ // one more request per task, carrying the whole conversation, to hear that
5
+ // the review is over. A rejected permission is the one way a tool call ends
6
+ // its session with the step's usage recorded (aborting loses it), so the
7
+ // done tool asks for one (opencode-config.ts) and every such request is
8
+ // rejected here. Runs until the signal aborts.
9
+ export async function endSessionsAtDoneTool(api, doneTool, signal, resubscribeMs = RESUBSCRIBE_MS) {
10
+ const reject = (requestID) => api.permission.reply({ requestID, reply: "reject" }, { signal }).catch(() => { });
11
+ while (!signal.aborted) {
12
+ try {
13
+ const { stream } = await api.event.subscribe({}, { signal });
14
+ for await (const event of stream) {
15
+ if (event.type === "permission.asked") {
16
+ if (event.properties.permission === doneTool)
17
+ void reject(event.properties.id);
18
+ }
19
+ else if (event.type === "server.connected") {
20
+ // Whatever was asked while no stream was open, after a reconnect too.
21
+ const pending = await api.permission.list({}, { signal });
22
+ for (const request of pending.data ?? []) {
23
+ if (request.permission === doneTool)
24
+ void reject(request.id);
25
+ }
26
+ }
27
+ }
28
+ }
29
+ catch {
30
+ // The stream broke: subscribe again.
31
+ }
32
+ await delay(resubscribeMs, undefined, { signal }).catch(() => { });
33
+ }
34
+ }
@@ -1,6 +1,7 @@
1
1
  import { type AppliedSampling, type CustomProvider, type Sampling } from "@open-cr-agent/core";
2
2
  import type { ToolServer } from "./tool-server.js";
3
3
  export declare const MCP_SERVER = "ocra";
4
+ export declare const DONE_TOOL: string;
4
5
  export declare const REVIEW_AGENT = "ocra-reviewer";
5
6
  export declare const HELPER_AGENT = "ocra-helper";
6
7
  export declare const WRAP_UP_AGENT = "ocra-wrap-up";
@@ -1,5 +1,7 @@
1
- import { MAX_AGENT_STEPS, OcraError, WRAP_UP_STEPS, WRAP_UP_TOOLS, } from "@open-cr-agent/core";
1
+ import { MAX_AGENT_STEPS, OcraError, REVIEW_TOOLS, WRAP_UP_STEPS, WRAP_UP_TOOLS, } from "@open-cr-agent/core";
2
2
  export const MCP_SERVER = "ocra";
3
+ // As OpenCode names it: the permission it asks for, too (done-tool.ts).
4
+ export const DONE_TOOL = `${MCP_SERVER}_${REVIEW_TOOLS.taskDone}`;
3
5
  export const REVIEW_AGENT = "ocra-reviewer";
4
6
  export const HELPER_AGENT = "ocra-helper";
5
7
  // A review's wrap-up turn (WRAP_UP_MESSAGE): its own agent, since OpenCode
@@ -34,12 +36,13 @@ export const OPENCODE_BUILTIN_TOOLS = [
34
36
  "apply_patch",
35
37
  ];
36
38
  export const DISABLED_BUILTINS = Object.fromEntries(OPENCODE_BUILTIN_TOOLS.map((t) => [t, false]));
37
- // The helper's tools (every one off) with only the reporting tools on.
39
+ // The helper's tools (every one off) with only the reporting tools on. The
40
+ // done tool is neither on nor off: a prompt's tools become the session's
41
+ // permissions, and "on" would allow what the agent must ask for.
38
42
  export function wrapUpTools(helperTools) {
39
- return {
40
- ...helperTools,
41
- ...Object.fromEntries(WRAP_UP_TOOLS.map((t) => [`${MCP_SERVER}_${t}`, true])),
42
- };
43
+ const { [DONE_TOOL]: _asks, ...off } = helperTools;
44
+ const on = WRAP_UP_TOOLS.map((t) => `${MCP_SERVER}_${t}`).filter((t) => t !== DONE_TOOL);
45
+ return { ...off, ...Object.fromEntries(on.map((t) => [t, true])) };
43
46
  }
44
47
  // OpenCode 1.18.32 takes a temperature per agent and sends it when the
45
48
  // model's catalog entry says the model accepts one; a model declared in
@@ -53,6 +56,7 @@ export function openCodeSampling(sampling = {}) {
53
56
  }
54
57
  export function openCodeConfig(tools, helperTools, custom = {}, sampling = {}) {
55
58
  const permission = { edit: "deny", bash: "deny", webfetch: "deny", skill: "deny" };
59
+ const reviewing = { ...permission, [DONE_TOOL]: "ask" };
56
60
  const { temperature } = sampling;
57
61
  const agents = {
58
62
  [REVIEW_AGENT]: {
@@ -60,7 +64,7 @@ export function openCodeConfig(tools, helperTools, custom = {}, sampling = {}) {
60
64
  prompt: REVIEW_AGENT_PROMPT,
61
65
  steps: MAX_AGENT_STEPS,
62
66
  tools: DISABLED_BUILTINS,
63
- permission,
67
+ permission: reviewing,
64
68
  },
65
69
  [HELPER_AGENT]: {
66
70
  mode: "primary",
@@ -74,7 +78,7 @@ export function openCodeConfig(tools, helperTools, custom = {}, sampling = {}) {
74
78
  prompt: REVIEW_AGENT_PROMPT,
75
79
  steps: WRAP_UP_STEPS,
76
80
  tools: wrapUpTools(helperTools),
77
- permission,
81
+ permission: reviewing,
78
82
  },
79
83
  };
80
84
  return {
package/dist/runtime.js CHANGED
@@ -5,9 +5,10 @@ import { join } from "node:path";
5
5
  import { ChainRunner, MAX_AGENT_STEPS, OcraError, parseModel, RESUME_MESSAGE, REVIEW_TOOLS, reviewTools, WRAP_UP_MESSAGE, withoutSecrets, } from "@open-cr-agent/core";
6
6
  import { createOpencodeClient } from "@opencode-ai/sdk/v2";
7
7
  import { resolveOpencodeBinary } from "./binary.js";
8
+ import { endSessionsAtDoneTool } from "./done-tool.js";
8
9
  import { AppliedEfforts } from "./effort.js";
9
10
  import { setUpEfforts } from "./effort-setup.js";
10
- import { DISABLED_BUILTINS, HELPER_AGENT, MCP_SERVER, openCodeConfig, openCodeSampling, REVIEW_AGENT, WITHOUT_SAMPLING, WRAP_UP_AGENT, wrapUpTools, } from "./opencode-config.js";
11
+ import { DISABLED_BUILTINS, DONE_TOOL, HELPER_AGENT, MCP_SERVER, openCodeConfig, openCodeSampling, REVIEW_AGENT, WITHOUT_SAMPLING, WRAP_UP_AGENT, wrapUpTools, } from "./opencode-config.js";
11
12
  import { startOpencodeServer } from "./opencode-server.js";
12
13
  import { credentialValues, missingCredentials, serverEnv } from "./server-env.js";
13
14
  import { promptSession } from "./session-prompt.js";
@@ -65,6 +66,7 @@ export class OpenCodeRuntime {
65
66
  if (!infra)
66
67
  return;
67
68
  process.off("exit", infra.onExit);
69
+ infra.doneTool.abort();
68
70
  await Promise.allSettled([infra.server.close(), infra.tools.close(), infra.dispatcher.close()]);
69
71
  await rm(infra.root, { recursive: true, force: true });
70
72
  }
@@ -193,7 +195,9 @@ export class OpenCodeRuntime {
193
195
  file: join(dirs.config, "opencode.json"),
194
196
  secrets,
195
197
  });
196
- return { root, tools, server, client, efforts, dispatcher, onExit, secrets };
198
+ const doneTool = new AbortController();
199
+ void endSessionsAtDoneTool(client, DONE_TOOL, doneTool.signal);
200
+ return { root, tools, server, client, efforts, dispatcher, doneTool, onExit, secrets };
197
201
  }
198
202
  catch (error) {
199
203
  await Promise.allSettled([tools.close(), started?.close()]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-cr-agent/runtime-opencode",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "OpenCode-based agent runtime for Open-CR-Agent",
5
5
  "keywords": [
6
6
  "code-review",
@@ -34,7 +34,7 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "@modelcontextprotocol/sdk": "^1.32.2",
37
- "@open-cr-agent/core": "0.15.0",
37
+ "@open-cr-agent/core": "0.16.0",
38
38
  "@opencode-ai/sdk": "1.18.32",
39
39
  "opencode-ai": "1.18.32",
40
40
  "undici": "^8.12.4",