Repository navigation
Expand file tree
/
Copy pathopencode.ts
More file actions
1428 lines (1363 loc) · 58.6 KB
/
Copy pathopencode.ts
File metadata and controls
1428 lines (1363 loc) · 58.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @ref LLP 0003#opencode-server-lifecycle [implements] — OpenCode server startup, CLI/SDK pinning, model preflight
// @ref LLP 0003#retry-taxonomy [implements] — stall/timeout/backoff handling for OpenCode passes
import { createRequire } from "node:module";
import path from "node:path";
import { createOpencode } from "@opencode-ai/sdk";
import type { LoadedConfig } from "../config/schema.js";
// Type-only: erased at compile time, so it creates NO runtime import cycle. The
// Claude engine is reached at runtime via dynamic import() in the dispatch
// branches below; claude-code.ts statically imports the shared helpers here.
import type { ClaudeCodeHandle, Engine } from "./claude-code.js";
import { pathInside, resolveOnPath } from "./exec.js";
import { RateLimitWatch } from "./throttle.js";
import { OPENCODE_RESEARCH_TOOLS, toolMap } from "./tools.js";
import { errorMessage, sleep } from "./util.js";
import type { ResearchMcpRuntime } from "./research.js";
/** Discriminant for the Claude Code CLI engine (see core/claude-code.ts). */
export const CLAUDE_CODE_ENGINE = "claude-code" as const;
/** Fixed so repo config cannot redirect the Meta credential. */
// @ref LLP 0003#muse-spark-via-meta-model-api [implements] — fixed endpoint, provider routing, and known-model metadata
export const META_MODEL_API_BASE_URL = "https://api.meta.ai/v1";
/** Known public Muse models. Other explicit `meta/...` ids use neutral metadata. */
export const META_MUSE_MODELS: Record<string, Record<string, unknown>> = {
"muse-spark-1.2": {
name: "Muse Spark 1.2",
reasoning: true,
limit: { context: 1_048_576, output: 131_072 },
modalities: {
input: ["text", "image", "pdf", "video"],
output: ["text"],
},
options: {
store: false,
reasoningEffort: "high",
reasoningSummary: "auto",
include: ["reasoning.encrypted_content"],
},
},
"muse-spark-1.2-contributor": {
name: "Muse Spark 1.2 Contributor",
reasoning: true,
limit: { context: 1_048_576, output: 131_072 },
modalities: {
input: ["text", "image", "pdf", "video"],
output: ["text"],
},
options: {
store: false,
reasoningEffort: "high",
reasoningSummary: "auto",
include: ["reasoning.encrypted_content"],
},
},
"muse-spark-1.3": {
name: "Muse Spark 1.3",
reasoning: true,
limit: { context: 1_048_576, output: 1_048_576 },
modalities: {
input: ["text", "image", "pdf", "video"],
output: ["text"],
},
options: {
store: false,
reasoningEffort: "high",
reasoningSummary: "auto",
include: ["reasoning.encrypted_content"],
},
},
"muse-spark-1.3-contributor": {
name: "Muse Spark 1.3 Contributor",
reasoning: true,
limit: { context: 1_048_576, output: 1_048_576 },
modalities: {
input: ["text", "image", "pdf", "video"],
output: ["text"],
},
options: {
store: false,
reasoningEffort: "high",
reasoningSummary: "auto",
include: ["reasoning.encrypted_content"],
},
},
};
/**
* Resolve which engine an agent's pass dispatches to, and (when claude) which
* claude handle to run it against. The per-agent router (engineOf) wins; absent it
* the carrier's own single `engine` decides. When the pass is claude-routed the
* claude handle is the carrier itself (a claude-only run, where the carrier IS the
* claude handle) or its `.claude` field (any run that also drives OpenCode). Pure
* and side-effect-free so the seam's dispatch is unit-testable without spawning.
*/
// @ref LLP 0003#two-engines-per-agent-dispatch [implements] — dynamic-import seam that reaches claude-code.ts at runtime, avoiding a static import cycle
export function resolveEngineDispatch(
handle: OpencodeHandle,
agent: string,
): { engine: "opencode" } | { engine: "claude-code"; claudeHandle: ClaudeCodeHandle } {
const engine = handle.engineOf?.(agent) ?? handle.engine ?? "opencode";
if (engine !== CLAUDE_CODE_ENGINE) {
return { engine };
}
// A claude-only run: the carrier itself IS the claude handle. Cast is safe (and
// still needed) because `handle` is typed OpencodeHandle here regardless.
if (handle.engine === CLAUDE_CODE_ENGINE) {
return { engine, claudeHandle: handle as ClaudeCodeHandle };
}
// Mixed run: engineOf routed this agent to claude-code, so the carrier's `.claude`
// field must be set. If it isn't, that's an invariant violation in how the handle
// was assembled, not a runtime fluke — fail loudly here instead of letting a bad
// cast smuggle `undefined` past the type checker and crash deep inside
// runClaudePrompt with no clue what went wrong.
if (!handle.claude) {
throw new Error(
`Agent "${agent}" is routed to the claude-code engine but this handle has no ` +
`.claude carrier — the handle was assembled inconsistently.`,
);
}
return { engine, claudeHandle: handle.claude };
}
export interface OpencodeHandle {
client: any;
url: string;
close: () => void;
/** Rate-limit evidence from this server's own log (see core/throttle.ts). */
rateLimit: RateLimitWatch;
/** Which review engine this handle drives. undefined ⇒ the OpenCode engine
* (default); "claude-code" ⇒ the Claude Code CLI engine, a superset handle
* (see ClaudeCodeHandle) that the seam functions dispatch to. */
engine?: Engine;
/** Present on the carrier when the run ALSO drives the Claude Code engine: the
* real claude handle, used by the per-agent dispatch below when engineOf routes an
* agent to claude-code and the carrier itself is the opencode handle. */
claude?: ClaudeCodeHandle;
/** Per-agent engine router; undefined ⇒ single-engine run keyed by `engine`. */
engineOf?: (agent: string) => Engine;
}
/** Token usage as reported on an OpenCode assistant message's `info.tokens`. */
export interface TokenUsage {
input?: number;
output?: number;
reasoning?: number;
cache?: { read?: number; write?: number };
}
export interface PromptResult {
text: string;
cost: number;
sessionID: string;
/** True when this reply came from the finalize ("wrap up now") path — i.e. the
* agent ran out of time and returned partial findings rather than converging. */
truncated?: boolean;
/** Token usage for the model request that produced this reply (for cache metrics). */
tokens?: TokenUsage;
/**
* The provider/model that ACTUALLY answered, as reported by the server — not what
* was configured. OpenCode silently substitutes a default when an agent's model id
* is empty or unusable, so without this a run can review with a completely different
* model than the config names and nothing says so. (It did: an empty REVIEWER_MODEL
* wiped every configured id and reviews ran on whatever provider happened to be
* available.) Undefined when the server didn't report it.
*/
model?: string;
}
/** Sum token usage across attempts (for per-task/run totals). */
export function addTokenUsage(into: TokenUsage, from?: TokenUsage): TokenUsage {
if (!from) {
return into;
}
into.input = (into.input ?? 0) + (from.input ?? 0);
into.output = (into.output ?? 0) + (from.output ?? 0);
into.reasoning = (into.reasoning ?? 0) + (from.reasoning ?? 0);
into.cache = {
read: (into.cache?.read ?? 0) + (from.cache?.read ?? 0),
write: (into.cache?.write ?? 0) + (from.cache?.write ?? 0),
};
return into;
}
// The coordinator consolidates findings; it needs no repo tools.
const COORDINATOR_TOOLS = toolMap([]);
// Every tool disabled, passed per-REQUEST (not per-agent) to make a "reply now,
// don't investigate" prompt physically unable to call tools. A prompt-level plea is
// not enough: in eas-cli#4084 the finalize ("stop and return what you have") reply
// opened 7 more files and then blew its window, losing the whole pass.
const NO_TOOLS = toolMap([]);
// Agent id for the single combined cross-cutting pass (see review.ts). It MUST be
// defined here so OpenCode uses this restricted tool set — otherwise the model
// falls back to a default agent with full tools and crawls the whole repo, which
// is why the cross-file pass used to wander for its entire time budget.
export const CROSS_CUTTING_AGENT = "cross-cutting";
// Deliberately NO `glob`/`list`: the cross-file pass is given the changed files'
// patch paths already, and directory crawling is exactly what made it wander into
// unrelated packages. `read` (open a known file) + `grep` (find a cross-reference
// among the changed files) are enough to trace interactions.
const CROSS_CUTTING_TOOLS = toolMap(["read", "grep"]);
// Verifies a finding by re-reading the actual file (adversarial refute pass). Same
// restricted tool set — it opens the cited file and checks the claim.
export const VERIFIER_AGENT = "verifier";
const VERIFIER_TOOLS = toolMap(["read", "grep"]);
// @ref LLP 0010#patch-level-confirmation-v2 [implements] — no-tools agent: the addressing PR's patch is INLINED, never read from disk, so there is no pathInside surface at all
// Confirms a requalification by reading the addressing PR's INLINED patch (v2). It
// gets NO tools: the patch is inlined into the task, so it must never read the disk
// (the untrusted upstack tree is not even materialized) — an empty tool set makes
// that structural, like the coordinator.
export const STACK_VERIFIER_AGENT = "stack-verifier";
/** Build the inline OpenCode config (agents + coordinator) from a repo config. */
export function buildOpencodeConfig(
config: LoadedConfig,
research?: ResearchMcpRuntime,
): Record<string, unknown> {
const agent: Record<string, unknown> = {};
for (const reviewer of config.agents) {
agent[reviewer.id] = {
description: `${reviewer.id} reviewer`,
mode: "all",
model: reviewer.model,
temperature: reviewer.temperature,
prompt: `You are the ${reviewer.id} code reviewer. Follow the user message exactly and return only the requested JSON.`,
tools: {
...reviewer.tools,
...Object.fromEntries(OPENCODE_RESEARCH_TOOLS.map((name) => [name, Boolean(research)])),
},
};
}
agent[CROSS_CUTTING_AGENT] = {
description: "Cross-file reviewer: issues spanning multiple changed files.",
mode: "all",
// Use the default reviewing model (agents share it unless overridden).
model: config.agents[0]?.model ?? config.coordinator.model,
temperature: config.agents[0]?.temperature ?? 0.1,
prompt:
"You are the cross-file code reviewer. Follow the user message exactly and return only the requested JSON.",
tools: {
...CROSS_CUTTING_TOOLS,
...Object.fromEntries(OPENCODE_RESEARCH_TOOLS.map((name) => [name, Boolean(research)])),
},
};
agent[VERIFIER_AGENT] = {
description: "Verifies a finding against the real file (adversarial refute pass).",
mode: "all",
model: config.agents[0]?.model ?? config.coordinator.model,
temperature: config.agents[0]?.temperature ?? 0.1,
prompt:
"You verify code-review findings against the actual source. Follow the user message exactly and return only the requested JSON.",
tools: VERIFIER_TOOLS,
};
agent[STACK_VERIFIER_AGENT] = {
description: "Confirms a requalification against the addressing PR's inlined patch.",
mode: "all",
model: config.agents[0]?.model ?? config.coordinator.model,
temperature: config.agents[0]?.temperature ?? 0.1,
prompt:
"You judge whether a later PR's patch actually addresses a code-review finding. Follow the user message exactly and return only the requested JSON.",
// No tools: the patch is inlined, so it must never read the disk (mirrors NO_TOOLS
// on the coordinator — see STACK_VERIFIER_AGENT).
tools: NO_TOOLS,
};
agent["coordinator"] = {
description: "Consolidates specialist findings into one decision.",
mode: "all",
model: config.coordinator.model,
temperature: config.coordinator.temperature,
prompt:
"You are the review coordinator. Follow the user message exactly and return only the requested JSON.",
tools: COORDINATOR_TOOLS,
};
// Synthesize a provider block for each upstream-alias auth entry, so one
// upstream can be reached with two credentials at once (e.g. "openai" on a
// ChatGPT/Codex subscription for the default models, plus an "openai-api"
// alias holding a metered API key for pro-tier models the subscription
// doesn't offer). The alias's model list is exactly the ids the roster
// references under that provider — OpenCode needs custom providers' models
// declared, and declaring only what's used keeps the preflight meaningful.
const provider: Record<string, unknown> = {};
const referencedModels = [
...config.agents.map((reviewer) => reviewer.model),
config.coordinator.model,
];
for (const entry of config.auth) {
if (
entry.provider === "meta" &&
entry.mode === "api-key" &&
entry.tokenEnv &&
!entry.upstream
) {
// Meta's Model API uses the Responses adapter; openai-compatible uses Chat
// Completions. Register every explicitly referenced `meta/…` model so a new
// Meta family/name does not require an ECR release merely to select the right
// provider. Known Muse ids get their published capabilities; unknown ids stay
// neutral and are validated by Meta on their first request.
const models: Record<string, unknown> = {};
for (const id of referencedModels) {
const slash = id.indexOf("/");
if (slash > 0 && id.slice(0, slash) === "meta" && id.length > slash + 1) {
const model = id.slice(slash + 1);
models[model] = META_MUSE_MODELS[model] ?? {};
}
}
provider.meta = {
npm: "@ai-sdk/openai",
name: "Meta Model API",
options: {
baseURL: META_MODEL_API_BASE_URL,
apiKey: `{env:${entry.tokenEnv}}`,
},
models,
};
continue;
}
if (!entry.upstream || !entry.tokenEnv) {
continue;
}
const models: Record<string, unknown> = {};
for (const id of referencedModels) {
const slash = id.indexOf("/");
if (slash > 0 && id.slice(0, slash) === entry.provider) {
models[id.slice(slash + 1)] = {};
}
}
provider[entry.provider] = {
npm:
entry.upstream === "openai"
? "@ai-sdk/openai"
: entry.upstream === "anthropic"
? "@ai-sdk/anthropic"
: "@ai-sdk/openai-compatible",
name: entry.provider,
options: { apiKey: `{env:${entry.tokenEnv}}` },
models,
};
}
return {
$schema: "https://opencode.ai/config.json",
agent,
...(research
? {
mcp: {
platform_docs: {
type: "local",
command: [research.command, ...research.args],
environment: research.environment,
enabled: true,
timeout: config.research.timeoutMs,
},
},
}
: {}),
...(Object.keys(provider).length > 0 ? { provider } : {}),
};
}
/** hey-api style responses come back as { data, error }; unwrap or throw. */
function unwrap<T>(res: any): T {
if (res && typeof res === "object" && ("data" in res || "error" in res)) {
if (res.error) {
throw new Error(typeof res.error === "string" ? res.error : JSON.stringify(res.error));
}
return res.data as T;
}
return res as T;
}
/**
* Directory holding the `opencode` binary from OUR dependency tree, or null if it
* can't be resolved.
*
* The SDK spawns the server with a bare `launch("opencode", …)`, i.e. whatever comes
* first on PATH. That silently couples every run to the machine's global install:
* in CI, `npx -p @expo/code-review-cli` puts the temp prefix's `.bin` first and the
* pinned version wins, but on a developer machine an older global `opencode` shadows
* it and the pair drifts (a 1.18.1 CLI against a 1.18.4 SDK surfaced as
* `ProviderModelNotFoundError: Model not found: anthropic/claude-opus-4-8` — the CLI
* resolving a model id the SDK considered valid). Prepending this directory to PATH
* makes the version we declare in package.json the version we actually run.
*/
function bundledOpencodeBinDir(): string | null {
try {
const require = createRequire(import.meta.url);
// <…>/node_modules/opencode-ai/package.json → <…>/node_modules/.bin, which holds
// the correctly-named `opencode` shim (the package's own bin file is
// `opencode.exe`, so the package dir itself is NOT a usable PATH entry).
const pkg = require.resolve("opencode-ai/package.json");
return path.join(path.dirname(pkg), "..", ".bin");
} catch {
return null;
}
}
/** The `opencode` the SDK will actually spawn: ours if resolvable, else PATH's. */
export function opencodeBinSource(): { dir: string | null; pinned: boolean } {
const dir = bundledOpencodeBinDir();
return { dir, pinned: dir !== null };
}
/**
* Resolve the `opencode` binary the way we trust it: OUR bundled shim when the
* dependency resolves, else a PATH lookup from a trusted cwd (resolveOnPath, never
* the inherited one) with a refusal of any binary that resolves INSIDE the current
* tree. Null when unresolved or in-tree.
*
* `ecr doctor`/`ecr setup-auth` may run inside a cloned untrusted repo, so a bare
* `opencode` handed to execFile/spawn resolves against the inherited cwd — and Windows
* checks the current directory before PATH, letting a PR-committed `opencode` shim run
* with ambient secrets in its env. Every opencode spawn in those commands goes through
* this, mirroring resolveClaudeCli for the `claude` binary.
*/
export async function resolveOpencodeCli(): Promise<string | null> {
const bundled = bundledOpencodeBinDir();
if (bundled) {
// Our own dependency tree (require.resolve is relative to THIS module, not cwd), so
// it's trusted by construction — and NO in-tree refusal here: ecr's node_modules
// commonly sits under cwd when run from its own repo, which pathInside would then
// wrongly reject.
return path.join(bundled, "opencode");
}
// PATH fallback: resolved from a trusted cwd, and refused if it lands in-tree.
const cliPath = await resolveOnPath("opencode");
if (!cliPath || pathInside(cliPath, process.cwd())) {
return null;
}
return cliPath;
}
// @ref LLP 0003#opencode-server-lifecycle [constrained-by] — port 0 avoids clobbering a dev's already-running opencode session; the SDK's own bare launch("opencode") spawn is a knowingly accepted POSIX-only residual, not to be silently "fixed" into an absolute-path reimplementation
/** Start an in-process OpenCode server with the given inline config. */
export async function startOpencode(config: unknown): Promise<OpencodeHandle> {
// Make our pinned CLI win over any global install (see bundledOpencodeBinDir).
// The SDK takes no `env`, so PATH is the only lever; it spreads `process.env` at
// spawn time, so setting it here reaches the child.
//
// SECURITY residual (accepted, POSIX-only deployment): the SDK spawns a BARE
// `opencode` (cross-spawn `launch("opencode")`), which on Windows resolves the name
// against the current directory before PATH — during a review the cwd is the
// untrusted PR-head tree, so a PR-committed `opencode.exe` at its root could run in
// its place. We deliberately do NOT reimplement the SDK's server bootstrap to inject
// an absolute path here: on POSIX (the supported platform) `execvp` never searches
// the cwd, so the hijack cannot fire, and forking the launch would mean silently
// maintaining our own copy of it against SDK drift. The direct-spawn `opencode`
// callers we own (`ecr doctor`/`ecr setup-auth`) are still hardened via
// resolveOpencodeCli. Revisit if Windows becomes a supported target.
const binDir = bundledOpencodeBinDir();
if (binDir) {
process.env.PATH = `${binDir}${path.delimiter}${process.env.PATH ?? ""}`;
}
const { client, server } = await createOpencode({
hostname: "127.0.0.1",
// Port 0 = let the OS pick a free one. The SDK defaults to a FIXED 4096 and reads
// the real URL back from the server's startup line, so the default meant any
// already-running opencode (a developer's own session is the common case) made
// every local run die with an opaque `ServeError`.
port: 0,
config: config as any,
});
// Watch THIS server's log for provider throttle evidence (prepareAuth has already
// pointed XDG_DATA_HOME at the run's isolated dir when auth is injected).
return { client, url: server.url, close: () => server.close(), rateLimit: new RateLimitWatch() };
}
/**
* A configured `provider/model` string that the running server can't resolve, with
* the closest ids it does know — the whole point is that the error names the fix.
*/
export interface UnknownModel {
model: string;
/**
* `credential` — the provider is the one we supplied a credential for, and the
* server still doesn't offer it: the credential was rejected, which is a very
* different fix from a typo'd id (and the one that actually bit us: a bad
* ANTHROPIC_OAUTH_API_KEY made every anthropic model look nonexistent).
* `provider` — no such provider at all. `model` — provider fine, model isn't.
*/
reason: "credential" | "provider" | "model";
suggestions: string[];
}
/** `provider/model` as the server reported it, or undefined if it reported neither. */
export function formatModel(providerID?: string, modelID?: string): string | undefined {
if (!providerID && !modelID) {
return undefined;
}
return `${providerID ?? "?"}/${modelID ?? "?"}`;
}
/** Providers the server actually has, as `{ providerID: [modelID, …] }`. */
export type ProviderModels = Record<string, string[]>;
/**
* Check configured model ids against what the server can resolve. Pure so the
* matching rules are testable without a live server.
*
* A wrong model id is a CONFIG error: it hits every pass identically, no retry or
* smaller scope can fix it, and the run should say so once instead of reporting N
* indistinguishable "pass failed" gaps (or, worse, burning the whole budget first).
*/
export function findUnknownModels(
models: string[],
available: ProviderModels,
/** Provider(s) we supplied a credential for, if any (see reason: "credential"). */
credentialedProvider?: string | string[],
): UnknownModel[] {
const credentialed = new Set(
typeof credentialedProvider === "string"
? [credentialedProvider]
: (credentialedProvider ?? []),
);
const unknown: UnknownModel[] = [];
for (const model of new Set(models)) {
// OpenCode model ids are `provider/model`; a model id may itself contain slashes
// (e.g. openrouter's `vendor/name`), so only the FIRST segment is the provider.
const slash = model.indexOf("/");
const providerID = slash === -1 ? model : model.slice(0, slash);
const modelID = slash === -1 ? "" : model.slice(slash + 1);
const providerModels = available[providerID];
if (!providerModels) {
unknown.push({
model,
// We configured this provider's credential and the server still doesn't
// offer it ⇒ the credential was refused, not the provider misnamed.
reason: credentialed.has(providerID) ? "credential" : "provider",
suggestions: Object.keys(available).sort(),
});
continue;
}
if (!providerModels.includes(modelID)) {
// Suggest ids that share a prefix with what was asked for, else the whole list.
const stem = modelID.split(/[-/]/)[0] ?? "";
const near = stem ? providerModels.filter((id) => id.startsWith(stem)) : [];
unknown.push({
model,
reason: "model",
suggestions: (near.length > 0 ? near : providerModels).slice(0, 8).sort(),
});
}
}
return unknown;
}
/** Ask the running server which providers/models it can resolve. */
export async function fetchProviderModels(handle: OpencodeHandle): Promise<ProviderModels> {
const data = unwrap<{ providers?: Array<{ id?: string; models?: Record<string, unknown> }> }>(
await handle.client.config.providers(),
);
const available: ProviderModels = {};
for (const provider of data?.providers ?? []) {
if (provider?.id) {
available[provider.id] = Object.keys(provider.models ?? {});
}
}
return available;
}
/** Human-readable, copy-pasteable explanation of unresolvable model ids. */
export function formatUnknownModels(
unknown: UnknownModel[],
auths?:
| { mode: string; provider: string; tokenEnv?: string }
| Array<{ mode: string; provider: string; tokenEnv?: string }>,
): string {
// A refused credential is one fact about the run, not one per model: report it once
// and name the token to check, rather than repeating it for every configured model.
const refused = unknown.filter((entry) => entry.reason === "credential");
if (refused.length > 0) {
const refusedProvider = refused[0]!.model.split("/")[0];
const entries = auths ? (Array.isArray(auths) ? auths : [auths]) : [];
const auth = entries.find((entry) => entry.provider === refusedProvider) ?? entries[0];
const provider = auth?.provider ?? refusedProvider;
const tokenEnv = auth?.tokenEnv;
// Do NOT blame the token alone: the most common causes have nothing to do with
// the credential's validity (see below). An earlier version of this message sent
// us to re-issue two perfectly good tokens.
// No anthropic special case here anymore: anthropic models never reach the
// OpenCode preflight (engineForModel routes every anthropic/… id to the Claude
// Code engine), so this message only ever names non-anthropic providers.
return (
`The OpenCode server does not offer the "${provider}" provider, even though this run ` +
`supplied a ${auth?.mode ?? "configured"} credential for it. OpenCode drops a provider whose ` +
`credential it could not use, which makes every ${provider} model look nonexistent: ` +
`${refused.map((entry) => entry.model).join(", ")}.\n` +
`The credential itself is often FINE. Check these in order:\n` +
(tokenEnv
? ` 1. The credential is wrong for the mode. ` +
`auth.mode "api-key" expects a plain API key for ${provider}; an OAuth/subscription ` +
`token is not an API key. A truncated or half-pasted ${tokenEnv} fails the same way.\n`
: ` 1. The credential is wrong for the configured auth.mode.\n`) +
`Providers the server does offer: ${refused[0]!.suggestions.join(", ") || "(none)"}.`
);
}
const lines = unknown.map((entry) =>
entry.reason === "provider"
? ` ${entry.model} — unknown provider "${entry.model.split("/")[0]}". Configured providers: ${entry.suggestions.join(", ") || "(none — is the credential set?)"}`
: ` ${entry.model} — that provider has no such model. Close matches: ${entry.suggestions.join(", ") || "(none)"}`,
);
return (
`The configured model id(s) do not exist on the running OpenCode server:\n${lines.join("\n")}\n` +
`Fix the model in .expo-code-review/config.jsonc (agents' \`model\`, \`coordinator.model\`) ` +
`or REVIEWER_MODEL. Note that a model id must be "provider/model" (e.g. anthropic/claude-sonnet-5), ` +
`and that an out-of-date \`opencode\` can reject an id a newer one accepts — run \`ecr doctor\`.`
);
}
// @ref LLP 0003#opencode-server-lifecycle [implements] — fail once before any pass runs; distinguishes credential-refused from model-not-found (see UnknownModel.reason)
/**
* Fail fast when a configured model can't be resolved, BEFORE any pass runs. Never
* blocks the run on its own failure: if the providers endpoint can't be read (an
* older server, a transport blip), the review proceeds and any real problem surfaces
* per-pass as before.
*/
export async function assertModelsResolvable(
handle: OpencodeHandle,
models: string[],
auths?: Array<{ mode: string; provider: string; tokenEnv?: string }>,
): Promise<void> {
if (handle.engine === CLAUDE_CODE_ENGINE) {
const { assertClaudeModels } = await import("./claude-code.js");
return assertClaudeModels(handle as ClaudeCodeHandle, models);
}
let available: ProviderModels;
try {
available = await fetchProviderModels(handle);
} catch {
return;
}
if (Object.keys(available).length === 0) {
return;
}
const unknown = findUnknownModels(
models,
available,
auths?.map((entry) => entry.provider),
);
if (unknown.length > 0) {
throw new Error(formatUnknownModels(unknown, auths));
}
}
const POLL_INTERVAL_MS = 1000;
// Emit a "still working" heartbeat if this long passes with no tool activity, so
// a long model-thinking stretch doesn't look hung in the logs.
const HEARTBEAT_MS = 45_000;
// Default per-attempt ceiling. Focused chunk passes finish well under this; the
// cross-cutting pass is given more (see review.ts). Hitting the cap does NOT mean
// "retry" — we first interrupt the run and ask the agent to return whatever
// findings it already has (finalizeOnTimeout), and only fail if that also runs
// over. Callers must treat AgentTimeoutError as "abandon", never "retry".
const DEFAULT_MAX_WAIT_MS = 8 * 60 * 1000;
// Extra budget for the "stop and summarize what you have" finalization prompt.
// Deliberately generous: this is the ONLY chance to salvage a pass that ran out of
// time, and at 90s it was losing that race. Tools are disabled for the request
// (NO_TOOLS), so the reply is a single emit and normally lands in seconds.
const FINALIZE_WAIT_MS = 3 * 60 * 1000;
// ---- stall detection ----
//
// A pass is STALLED when its in-progress assistant message stops changing at all:
// no new tool call, no streamed text or reasoning, no token growth. A model that is
// genuinely thinking still grows that message every few seconds, so a gap this long
// means the provider request is wedged (or stuck in an internal retry we cannot
// see) — not that the work is hard.
//
// Motivating incident (eas-cli#4084, 2026-07-26): the cross-file pass ran 7 `read`
// calls in its first 6 seconds and then sat completely silent for 25 minutes,
// recording ZERO tokens (input, output, reasoning and cache all 0 in the run log),
// until its wall-clock cap fired. The finalize salvage then also went silent, so the
// pass's entire work product was lost and the PR got a coverage gap. Nothing in the
// run distinguished "wedged" from "thinking" — the heartbeat just printed elapsed
// seconds. A wall-clock cap alone cannot fix this: raising it only buys a longer
// silence, which is why the cap is NOT the lever here.
//
// Unlike the wall-clock cap (non-convergence → abandon, never retry), a stall is
// transient, so it earns ONE clean-slate retry inside the pass's existing budget.
const STALL_MS = 4 * 60 * 1000;
// Never let the watchdog outlast the pass it guards: a short pass (the 3m verifier,
// a 4m no-tools fallback) would otherwise hit its deadline before the watchdog could
// fire, and get none of this protection. Half the cap, with a floor that leaves room
// for a slow first token.
const MIN_STALL_MS = 30 * 1000;
// @ref LLP 0003#retry-taxonomy [constrained-by] — watchdog window capped at half the pass's own maxWaitMs so it can never outlast the deadline it protects
/** Exported for tests. */
export function stallWindowMs(maxWaitMs: number): number {
return Math.min(STALL_MS, Math.max(MIN_STALL_MS, Math.floor(maxWaitMs / 2)));
}
// The finalize reply does no investigation and cannot call tools, so it should
// stream within seconds; a much shorter silence already means wedged.
const FINALIZE_STALL_MS = 60 * 1000;
// Breathing room before the retry: if the silence came from provider-side throttling
// or backoff, reconnecting instantly is the worst move.
const STALL_RETRY_BACKOFF_MS = 20 * 1000;
// When the account is provably rate-limited, wait in longer beats: re-sending the
// pass's whole context into a throttled account only deepens the limit. Several
// waits fit inside a pass budget, and each is long enough for a limit window to move.
const RATE_LIMIT_WAIT_MS = 90 * 1000;
// Only retry when enough of the pass's budget remains for the fresh attempt to
// plausibly finish; otherwise go straight to the soft landing.
const STALL_RETRY_MIN_REMAINING_MS = STALL_MS + 60 * 1000;
/**
* What to do about a stalled attempt: WAIT (the account is provably rate-limited —
* see core/throttle.ts — so patience beats re-sending the context; waits don't
* consume the one retry), start over from a clean session, or stop and salvage
* findings. Exactly ONE wedged retry, and only with enough budget left for it to
* land — a second wedged attempt would just spend the rest of the pass's window,
* which is the failure this whole mechanism exists to end. Exported for tests.
*/
export function stallAction(
wedgedRetries: number,
remainingMs: number,
rateLimited = false,
): "wait" | "retry" | "soft-land" {
if (rateLimited && remainingMs > STALL_RETRY_MIN_REMAINING_MS) {
return "wait";
}
return wedgedRetries === 0 && remainingMs > STALL_RETRY_MIN_REMAINING_MS ? "retry" : "soft-land";
}
const FINALIZE_PROMPT =
"You have reached your time budget. STOP investigating now — do NOT read, grep, " +
"glob, list, or open any more files, and do not call any tools. Based ONLY on " +
"what you have already examined, reply with the single JSON object exactly as " +
"specified in your instructions, containing whatever findings you are already " +
"confident about. If you have nothing solid, return an empty findings array.";
/**
* Internal signal that a poll loop passed its deadline. Carries the best-effort
* cost/tokens of the in-progress (never-completed) assistant message so a
* timed-out task's spend isn't dropped from the run's metrics.
*/
class DeadlineReached extends Error {
constructor(
readonly cost: number = 0,
readonly tokens?: TokenUsage,
) {
super("deadline reached");
}
}
/**
* Internal signal that an in-progress reply went silent (see STALL_MS). Distinct
* from DeadlineReached: the pass's time budget is NOT spent, so the caller can spend
* what's left on a fresh attempt instead of abandoning the work.
*/
class NoProgress extends Error {
constructor(
readonly cost: number = 0,
readonly tokens?: TokenUsage,
/** How long the reply had been unchanged when we gave up on it. */
readonly idleMs: number = 0,
) {
super("no progress");
}
}
/**
* A cheap signature of how far along an in-progress reply is: its parts (count,
* type, streamed length, tool status) plus the message's token/cost counters. ANY
* real progress — a new tool call, another chunk of text or reasoning, a tool
* advancing pending → running → completed — changes it; a wedged request leaves it
* byte-identical poll after poll. Exported for tests.
*/
export function progressFingerprint(message: RawMessage): string {
const shape = (message.parts ?? [])
.map((part) => `${part.type ?? ""}:${(part.text ?? "").length}:${part.state?.status ?? ""}`)
.join("|");
const tokens = message.info?.tokens;
return [
shape,
tokens?.input ?? 0,
tokens?.output ?? 0,
tokens?.reasoning ?? 0,
tokens?.cache?.read ?? 0,
tokens?.cache?.write ?? 0,
message.info?.cost ?? 0,
].join("~");
}
const DEADLINE_SENTINEL = Symbol("deadline");
/**
* Race a promise against the poll deadline. Without this, a stalled message fetch
* (a wedged/overloaded OpenCode server) blocks the poll loop past its deadline,
* because the deadline is only re-checked at the top of the loop — so a single
* hung fetch could let a task run minutes past its time cap. Returns the sentinel
* the instant the deadline passes, so the loop enforces the cap even mid-fetch.
*/
async function raceDeadline<T>(
work: Promise<T>,
deadline: number,
): Promise<T | typeof DEADLINE_SENTINEL> {
const remaining = deadline - Date.now();
if (remaining <= 0) {
return DEADLINE_SENTINEL;
}
let timer: ReturnType<typeof setTimeout>;
const timeout = new Promise<typeof DEADLINE_SENTINEL>((resolve) => {
timer = setTimeout(() => resolve(DEADLINE_SENTINEL), remaining);
});
try {
return await Promise.race([work, timeout]);
} finally {
clearTimeout(timer!);
}
}
/**
* Thrown when an agent exceeds its time budget even after being asked to wrap up.
* Callers MUST treat this as "abandon this task" — retrying just repeats the same
* non-convergent run. Carries the cost/tokens burned so the caller can still
* account for the (abandoned) work.
*/
export class AgentTimeoutError extends Error {
readonly cost: number;
readonly tokens?: TokenUsage;
/**
* Why the pass was abandoned: it investigated without converging ("time"), or its
* model request went silent and did not recover after a fresh attempt ("stall").
* The caller's handling is the same (abandon), but the two have different causes —
* a wander is ours to bound, a stall is the provider's — so logs and coverage
* notes must not conflate them.
*/
readonly reason: "time" | "stall";
constructor(
agent: string,
minutes: number,
cost = 0,
tokens?: TokenUsage,
reason: "time" | "stall" = "time",
) {
super(
reason === "stall"
? `Agent "${agent}" stalled: its model request went silent and produced nothing after a retry (${minutes} minutes)`
: `Agent "${agent}" timed out after ${minutes} minutes (including finalize)`,
);
this.name = "AgentTimeoutError";
this.cost = cost;
this.tokens = tokens;
this.reason = reason;
}
}
/**
* Run a single prompt against the named agent in a fresh session and return the
* concatenated assistant text.
*
* Uses the async prompt + polling rather than the synchronous `session.prompt`:
* a large diff can keep an agent busy well past undici's 300s headers timeout,
* which would kill a long-held synchronous request. promptAsync returns
* immediately and we poll the message list (quick GETs) until the assistant
* message completes.
*/
export async function promptAgent(
handle: OpencodeHandle,
args: {
agent: string;
system: string;
text: string;
title: string;
/** Called once per tool the agent runs, for live progress (e.g. "read foo.ts"). */
onActivity?: (line: string) => void;
/** Per-attempt time ceiling. Defaults to DEFAULT_MAX_WAIT_MS. */
maxWaitMs?: number;
/**
* Soft ceiling on the number of distinct tool calls the investigation may make.
* Exceeding it trips the same soft-landing as the wall-clock cap (interrupt +
* ask for findings so far). This bounds an agent that WANDERS — reads/greps
* without converging — which is the root cause of the non-convergent 15-minute
* timeouts. Undefined = no tool-call cap.
*/
maxToolCalls?: number;
/**
* On hitting the ceiling, interrupt the run and ask the agent to return the
* findings it has so far (a soft landing) instead of throwing immediately.
*/
finalizeOnTimeout?: boolean;
},
): Promise<PromptResult> {
// A Claude handle never runs OpenCode's session/polling machinery: consumers
// use promptAndParse, but guard here too so a direct call can't run this code
// against a Claude handle. Dispatch is per-agent (see resolveEngineDispatch).
const dispatch = resolveEngineDispatch(handle, args.agent);
if (dispatch.engine === CLAUDE_CODE_ENGINE) {
const { runClaudePrompt } = await import("./claude-code.js");
return runClaudePrompt(dispatch.claudeHandle, args);
}
const maxWaitMs = args.maxWaitMs ?? DEFAULT_MAX_WAIT_MS;
// ONE deadline for the whole pass, shared by the first attempt and any stall
// retry, so retrying a wedged request can never push the pass past its declared
// cap (the budget math in review.ts depends on that being true).
const deadline = Date.now() + maxWaitMs;
// Spend from abandoned attempts, carried forward so a wedged attempt's cost and
// tokens still land in the run's metrics instead of vanishing.
let carriedCost = 0;
let carriedTokens: TokenUsage | undefined;
const carry = (result: PromptResult): PromptResult => ({
...result,
cost: result.cost + carriedCost,
tokens: addTokenUsage(addTokenUsage({}, carriedTokens), result.tokens),
});
const absorb = (spent: { cost: number; tokens?: TokenUsage }): void => {
carriedCost += spent.cost;
carriedTokens = addTokenUsage(addTokenUsage({}, carriedTokens), spent.tokens);
};
/**
* Last chance to get something out of a pass that hit its ceiling: interrupt the
* run and ask the SAME (context-carrying) session for whatever it already has.
* Tools are disabled for this request, so it cannot resume investigating — the
* only thing it can do is emit.
*/
const softLand = async (
sessionID: string,
reason: "time" | "stall",
reportedTools: Set<string>,
): Promise<PromptResult> => {
await abortQuietly(handle, sessionID);
const minutes = Math.round(maxWaitMs / 60000);
if (!args.finalizeOnTimeout) {
throw new AgentTimeoutError(args.agent, minutes, carriedCost, carriedTokens, reason);
}
// Only messages after this point count as the answer.
const baseline = (await fetchMessages(handle, sessionID)).length;
args.onActivity?.(
reason === "stall"
? "no output after a retry — asking for findings so far"
: "time budget reached — asking for findings so far",
);
await sendSessionPrompt(handle, sessionID, {
agent: args.agent,
system: args.system,
text: FINALIZE_PROMPT,
tools: NO_TOOLS,
});
try {
const result = await pollForCompletion(handle, sessionID, {
agent: args.agent,
fromIndex: baseline,
deadline: Date.now() + FINALIZE_WAIT_MS,
onActivity: args.onActivity,
reportedTools,
stallMs: FINALIZE_STALL_MS,
});
return { ...carry(result), truncated: true };
} catch (finalizeError) {
if (finalizeError instanceof DeadlineReached || finalizeError instanceof NoProgress) {
await abortQuietly(handle, sessionID);
absorb(finalizeError);
throw new AgentTimeoutError(
args.agent,
Math.round((maxWaitMs + FINALIZE_WAIT_MS) / 60000),
carriedCost,
carriedTokens,
reason,
);
}
throw finalizeError;
}
};
let wedgedRetries = 0;
for (let attempt = 0; ; attempt++) {
const session = unwrap<{ id: string }>(
await handle.client.session.create({
body: { title: attempt === 0 ? args.title : `${args.title}-retry${attempt}` },
}),
);
const reportedTools = new Set<string>();
await sendSessionPrompt(handle, session.id, {
agent: args.agent,
system: args.system,
text: args.text,
});
try {
return carry(
await pollForCompletion(handle, session.id, {
agent: args.agent,
fromIndex: 0,
deadline,
onActivity: args.onActivity,
reportedTools,
maxToolCalls: args.maxToolCalls,
stallMs: stallWindowMs(maxWaitMs),
}),
);
} catch (error) {
// Went silent. The request is wedged, not slow, so the first move is a clean
// slate — a fresh session, not the finalize prompt, which would be asking the
// wedged request to answer. Exactly one retry, and only when enough budget
// remains for it to land; after that the finalize is still worth a try as the