@open-cr-agent/runtime-opencode 0.5.0 → 0.7.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.
@@ -1,28 +1,35 @@
1
- import { OcraError } from "@open-cr-agent/core";
2
- import { emptyUsage, errorMessage, parseModel, } from "@open-cr-agent/core/internal";
3
- import { sessionUsage, summarizeSession } from "./session-outcome.js";
1
+ import { emptyUsage, errorMessage, OcraError, parseModel, } from "@open-cr-agent/core";
2
+ import { parseSessionMessages, sessionUsage, summarizeSession, } from "./session-outcome.js";
4
3
  // A session that was cut off has still spent tokens and may have reported
5
4
  // findings; this bounds the one extra request that collects them.
6
- export const HARVEST_TIMEOUT_MS = 5_000;
5
+ const HARVEST_TIMEOUT_MS = 5_000;
7
6
  // A prompt returns only when the agent is done, so silence on the request is
8
7
  // normal; silence in the session is not. A session whose messages have not
9
8
  // changed for this long (no new step, no streamed text, no tool progress) is
10
9
  // stopped and the task moves to the next model instead of waiting for its
11
10
  // full timeout. Generous, so a slow step that is still writing survives.
12
- export const INACTIVITY_MS = 5 * 60_000;
11
+ const INACTIVITY_MS = 5 * 60_000;
13
12
  // Each poll also reports what the session has spent, and a run's spend limit
14
13
  // stops running tasks on those reports: this bounds how far past the limit a
15
14
  // task can get before it is stopped.
16
- export const ACTIVITY_POLL_MS = 10_000;
15
+ const ACTIVITY_POLL_MS = 10_000;
17
16
  // About one review attempt in twelve on Gemini ended after a step or two
18
17
  // with no text, no done tool and steps to spare (2026-09-28), and the task
19
18
  // counted as completed with its files unread. Continuing the same session
20
19
  // keeps what it read and is cheaper than starting over.
21
- export function stoppedEarly(outcome, resume) {
20
+ function stoppedEarly(outcome, resume) {
22
21
  return (!outcome.toolCalls.includes(resume.doneTool) &&
23
22
  outcome.steps < resume.maxSteps &&
24
23
  outcome.text.trim() === "");
25
24
  }
25
+ // OpenCode caps each prompt's steps, so the cap ended an attempt when its
26
+ // last turn alone used them all.
27
+ function capped(outcome, lastTurnSteps, resume) {
28
+ const atCap = !outcome.error &&
29
+ !outcome.toolCalls.includes(resume.doneTool) &&
30
+ lastTurnSteps >= resume.maxSteps;
31
+ return atCap ? { ...outcome, atStepCap: true } : outcome;
32
+ }
26
33
  export async function promptSession(session, input, reportTool, signal, activity = {}) {
27
34
  const created = await session.create({ title: input.title }, { signal });
28
35
  if (!created.data) {
@@ -38,6 +45,7 @@ export async function promptSession(session, input, reportTool, signal, activity
38
45
  sessionID,
39
46
  agent: input.agent,
40
47
  model: parseModel(input.model),
48
+ ...(input.variant ? { variant: input.variant } : {}),
41
49
  system: input.system,
42
50
  tools: input.tools,
43
51
  parts: [{ type: "text", text: input.user }],
@@ -53,7 +61,7 @@ export async function promptSession(session, input, reportTool, signal, activity
53
61
  let outcome;
54
62
  try {
55
63
  const messages = await session.messages({ sessionID }, { signal: attempt });
56
- outcome = summarizeSession((messages.data ?? []), reportTool, input.toolPrefix);
64
+ outcome = summarizeSession(parseSessionMessages(messages.data), reportTool, input.toolPrefix);
57
65
  }
58
66
  catch (error) {
59
67
  // The session finished; running it again on the next model would pay
@@ -66,12 +74,15 @@ export async function promptSession(session, input, reportTool, signal, activity
66
74
  },
67
75
  };
68
76
  }
69
- if (!input.resume || !stoppedEarly(outcome, input.resume))
77
+ if (!input.resume)
70
78
  return outcome;
79
+ if (!stoppedEarly(outcome, input.resume))
80
+ return capped(outcome, outcome.steps, input.resume);
71
81
  const again = await session.prompt({
72
82
  sessionID,
73
83
  agent: input.agent,
74
84
  model: parseModel(input.model),
85
+ ...(input.variant ? { variant: input.variant } : {}),
75
86
  system: input.system,
76
87
  tools: input.tools,
77
88
  parts: [{ type: "text", text: input.resume.message }],
@@ -83,7 +94,7 @@ export async function promptSession(session, input, reportTool, signal, activity
83
94
  };
84
95
  if (again.error)
85
96
  resumed.error = { message: JSON.stringify(again.error), retryable: false };
86
- return resumed;
97
+ return capped(resumed, resumed.steps - outcome.steps, input.resume);
87
98
  }
88
99
  catch (error) {
89
100
  // Aborted or cut off by the transport: OpenCode may still be running the
@@ -120,7 +131,7 @@ function watchActivity(session, sessionID, options) {
120
131
  polling = true;
121
132
  try {
122
133
  const messages = await session.messages({ sessionID }, { signal: AbortSignal.timeout(HARVEST_TIMEOUT_MS) });
123
- const list = (messages.data ?? []);
134
+ const list = parseSessionMessages(messages.data);
124
135
  if (options.onUsage) {
125
136
  const spent = sessionUsage(list);
126
137
  if (spent.costUsd > reported.costUsd || spent.inputTokens > reported.inputTokens) {
@@ -148,7 +159,7 @@ function watchActivity(session, sessionID, options) {
148
159
  return { signal: controller.signal, stop: () => clearInterval(timer) };
149
160
  }
150
161
  // What changes while an agent works: steps, parts, streamed text, tool states.
151
- export function activitySignature(messages) {
162
+ function activitySignature(messages) {
152
163
  return messages
153
164
  .map((m) => m.parts.map((p) => `${p.type}:${p.state?.status ?? ""}:${p.text?.length ?? 0}`).join(","))
154
165
  .join("|");
@@ -156,13 +167,12 @@ export function activitySignature(messages) {
156
167
  async function harvest(session, sessionID, reportTool, toolPrefix) {
157
168
  try {
158
169
  const messages = await session.messages({ sessionID }, { signal: AbortSignal.timeout(HARVEST_TIMEOUT_MS) });
159
- return summarizeSession((messages.data ?? []), reportTool, toolPrefix);
170
+ return summarizeSession(parseSessionMessages(messages.data), reportTool, toolPrefix);
160
171
  }
161
172
  catch {
162
173
  return emptyOutcome();
163
174
  }
164
175
  }
165
- export function emptyOutcome() {
176
+ function emptyOutcome() {
166
177
  return { findings: [], steps: 0, toolCalls: [], text: "", usage: emptyUsage() };
167
178
  }
168
- //# sourceMappingURL=session-prompt.js.map
@@ -5,4 +5,3 @@ export interface ToolServer {
5
5
  close(): Promise<void>;
6
6
  }
7
7
  export declare function startToolServer(tools: readonly ToolDefinition[], context: () => ReviewContext | undefined): Promise<ToolServer>;
8
- //# sourceMappingURL=tool-server.d.ts.map
@@ -3,7 +3,7 @@ import { createServer } from "node:http";
3
3
  import { createRequire } from "node:module";
4
4
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
5
5
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
6
- import { errorMessage } from "@open-cr-agent/core/internal";
6
+ import { errorMessage } from "@open-cr-agent/core";
7
7
  // src/ and dist/ both sit one level below the package root.
8
8
  const VERSION = createRequire(import.meta.url)("../package.json").version;
9
9
  export async function startToolServer(tools, context) {
@@ -60,4 +60,3 @@ function authorized(req, expected) {
60
60
  const actual = Buffer.from(req.headers.authorization ?? "");
61
61
  return actual.length === expected.length && timingSafeEqual(actual, expected);
62
62
  }
63
- //# sourceMappingURL=tool-server.js.map
@@ -1,4 +1,3 @@
1
1
  import { Agent, type Dispatcher } from "undici";
2
2
  export declare function createUntimedDispatcher(): Agent;
3
3
  export declare function untimedFetch(dispatcher: Dispatcher): typeof fetch;
4
- //# sourceMappingURL=transport.d.ts.map
package/dist/transport.js CHANGED
@@ -8,6 +8,10 @@ import { Agent, fetch as undiciFetch } from "undici";
8
8
  export function createUntimedDispatcher() {
9
9
  return new Agent({ headersTimeout: 0, bodyTimeout: 0 });
10
10
  }
11
+ // The adapter from undici's fetch to Node's is a deliberate copy of the one in
12
+ // core's proxiedFetch: a runtime builds on core's public API only (ADR-0006),
13
+ // and sharing it there would put undici's Dispatcher type in core's contract
14
+ // and pass a dispatcher from this package's undici to core's copy.
11
15
  export function untimedFetch(dispatcher) {
12
16
  return async (input, init) => {
13
17
  const request = new Request(input, init);
@@ -22,4 +26,3 @@ export function untimedFetch(dispatcher) {
22
26
  return response;
23
27
  };
24
28
  }
25
- //# sourceMappingURL=transport.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-cr-agent/runtime-opencode",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "OpenCode-based agent runtime for Open-CR-Agent",
5
5
  "keywords": [
6
6
  "code-review",
@@ -33,8 +33,8 @@
33
33
  "!dist/**/*.map"
34
34
  ],
35
35
  "dependencies": {
36
- "@modelcontextprotocol/sdk": "^1.30.1",
37
- "@open-cr-agent/core": "0.5.0",
36
+ "@modelcontextprotocol/sdk": "^1.31.0",
37
+ "@open-cr-agent/core": "0.7.0",
38
38
  "@opencode-ai/sdk": "1.18.32",
39
39
  "opencode-ai": "1.18.32",
40
40
  "undici": "^8.11.2",