crysk 0.4.0 → 0.5.1
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.
- package/README.md +26 -17
- package/config/models/anthropic.json +1 -1
- package/dist/SKILL-k4k0zn1w.md +143 -0
- package/dist/SKILL-vhb4c3vv.md +553 -0
- package/dist/SKILL-xpf6kmdw.md +74 -0
- package/dist/ci-admission-0ken6ehx.md +128 -0
- package/dist/cli.js +3056 -1623
- package/dist/index.js +1744 -938
- package/dist/opencode-ci-ac5zksdn.md +106 -0
- package/dist/workflow-4pkyck01.yml +160 -0
- package/package.json +1 -1
- package/prompts/default/PROVENANCE.md +1 -0
- package/skills/crysk/SKILL.md +16 -16
- package/skills/crysk-ci-setup/SKILL.md +41 -38
- package/skills/crysk-ci-setup/assets/workflow.yml +31 -22
- package/skills/crysk-ci-setup/references/ci-admission.md +1 -1
- package/skills/crysk-ci-setup/references/opencode-ci.md +53 -88
- package/skills/crysk-loop/SKILL.md +1 -1
- package/src/asset-manifest.ts +0 -4
- package/src/assets.ts +1 -1
- package/src/ci/admission-ledger.ts +4 -0
- package/src/ci/classify-error.ts +21 -24
- package/src/ci/cli-record.ts +53 -1
- package/src/ci/crysk-secrets.ts +409 -0
- package/src/ci/force-comment.ts +14 -1
- package/src/ci/gates.ts +86 -57
- package/src/ci/outcome.ts +421 -330
- package/src/ci/platform-config.ts +2 -2
- package/src/ci/platform-estimate.ts +111 -0
- package/src/ci/platform-report.ts +274 -41
- package/src/ci/platform-trace.ts +581 -0
- package/src/ci/platform.ts +58 -12
- package/src/ci/publish.ts +19 -6
- package/src/ci/reporter.ts +14 -4
- package/src/ci/review-levels.ts +8 -7
- package/src/ci/run.ts +88 -0
- package/src/ci/setup.ts +485 -118
- package/src/ci/trace-bundle.ts +1486 -0
- package/src/ci/trace-transcripts.ts +480 -0
- package/src/ci/trigger.ts +176 -33
- package/src/cli.ts +11 -3
- package/src/commands/ci-setup-questions.ts +3 -3
- package/src/commands/ci-setup.ts +152 -6
- package/src/commands/config.ts +3 -0
- package/src/commands/doctor.ts +15 -6
- package/src/config/config.ts +16 -3
- package/src/diversity/clustering.ts +0 -1
- package/src/diversity/identity.ts +4 -5
- package/src/diversity/pipeline-integration.ts +1 -9
- package/src/doctor.ts +3 -3
- package/src/execution/attempt-evidence.ts +10 -5
- package/src/execution/claude-session-trace.ts +480 -0
- package/src/execution/contracts.ts +21 -4
- package/src/execution/harness.ts +105 -8
- package/src/execution/model-fallback.ts +239 -0
- package/src/execution/usage-normalized.ts +6 -4
- package/src/home-preflight.ts +1 -1
- package/src/index.ts +1 -1
- package/src/model/catalog.ts +13 -37
- package/src/model/free-discovery.ts +117 -8
- package/src/model/model-name.ts +404 -0
- package/src/model/provider-capabilities.ts +22 -7
- package/src/model/routing.ts +346 -467
- package/src/pr/admission.ts +18 -0
- package/src/pr/ci-admission-gate.ts +2 -0
- package/src/pr/discovery.ts +18 -0
- package/src/pr/plan.ts +55 -5
- package/src/pr/preflight.ts +1 -1
- package/src/pr/publish-outcome.ts +1 -1
- package/src/pr/review-pr.ts +26 -1
- package/src/pr/target.ts +63 -11
- package/src/production-runtime.ts +760 -331
- package/src/review/pipeline.ts +277 -101
- package/src/review/preflight.ts +201 -294
- package/src/review/report.ts +30 -26
- package/src/review/review.ts +20 -3
- package/src/review/route-preflight.ts +79 -11
- package/src/review/run.ts +44 -6
- package/src/review/scout.ts +1 -1
- package/src/review/spec.ts +3 -3
- package/src/review/step-runner.ts +70 -1
- package/src/runner-authority.ts +127 -9
- package/src/security/binding-policy.ts +1 -2
- package/src/security/claude-credential.ts +355 -0
- package/src/security/credential-broker.ts +56 -40
- package/src/security/evidence-redaction.ts +179 -4
- package/src/security/opencode-secret-manifest.ts +114 -0
- package/src/security/redact.ts +45 -0
- package/src/system-tools.ts +48 -21
- package/src/transport-registry.ts +12 -2
- package/src/transports/claude-code-cli.ts +21 -2
- package/src/transports/opencode-sdk.ts +11 -9
- package/src/transports/opencode-server.ts +7 -10
- package/src/ui/config-edit.ts +9 -20
- package/src/ui/config.ts +32 -27
- package/src/ui/plan.ts +84 -50
- package/src/updater.ts +10 -7
- package/prompts/default/deep-review-lifecycle.md +0 -362
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import { homedir } from "node:os";
|
|
2
|
+
import { opencodeSecretToken } from "#ci/crysk-secrets";
|
|
2
3
|
import {
|
|
3
4
|
createFreeModelProbe,
|
|
5
|
+
createModelVariantsProbe,
|
|
4
6
|
type FreeModelProbe,
|
|
5
7
|
freeVerdictKey,
|
|
8
|
+
type ModelVariantsProbe,
|
|
6
9
|
} from "#model/free-discovery";
|
|
10
|
+
import type { ModelsConfig } from "#model/model-name";
|
|
7
11
|
import type { ProviderCapabilityReport } from "#model/provider-capabilities";
|
|
8
12
|
import {
|
|
9
13
|
type CapabilityGateDecision,
|
|
10
14
|
exactBindingCapabilityGate,
|
|
11
15
|
} from "#model/provider-capabilities";
|
|
12
|
-
import type {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
import type { ResolvedRoutePlan, ResolvedStepRoute } from "#model/routing";
|
|
17
|
+
import {
|
|
18
|
+
buildResolvedRoutePlan,
|
|
19
|
+
createResolvedRoutePlan,
|
|
20
|
+
freezeRoutePlan,
|
|
21
|
+
planRoutes,
|
|
22
|
+
resolveModelEntryRoute,
|
|
16
23
|
} from "#model/routing";
|
|
17
|
-
import { buildResolvedRoutePlan, freezeRoutePlan } from "#model/routing";
|
|
18
24
|
import {
|
|
19
25
|
ClaudeCodeRunner,
|
|
20
26
|
type StepResult,
|
|
@@ -41,14 +47,17 @@ import type {
|
|
|
41
47
|
VerifiedExecutable,
|
|
42
48
|
} from "./execution/contracts";
|
|
43
49
|
import { StepExecutionHarness } from "./execution/harness";
|
|
50
|
+
import { runWithFallback } from "./execution/model-fallback";
|
|
44
51
|
import {
|
|
45
52
|
InMemorySpendLedger,
|
|
46
53
|
type SpendLedger,
|
|
47
54
|
} from "./execution/spend-limiter";
|
|
48
55
|
import {
|
|
49
56
|
type BindingAuthorityResolution,
|
|
57
|
+
createOpenCodeBrokerPool,
|
|
50
58
|
credentialKindBillsMetered,
|
|
51
59
|
credentialKindForRoute,
|
|
60
|
+
openCodeBrokerForRoute,
|
|
52
61
|
openCodeCredentialBrokerForKind,
|
|
53
62
|
prepareProductionRunnerAuthority,
|
|
54
63
|
type ResolvedBindingAuthority,
|
|
@@ -61,8 +70,17 @@ import {
|
|
|
61
70
|
validateBindingAdmission,
|
|
62
71
|
validateRouteDrift,
|
|
63
72
|
} from "./security/binding-policy";
|
|
64
|
-
import {
|
|
73
|
+
import {
|
|
74
|
+
lookupOpenCodeApiRecord,
|
|
75
|
+
lookupOpenCodeOAuthRecord,
|
|
76
|
+
OPENCODE_OAUTH_PROVIDER,
|
|
77
|
+
OpenCodeApiTokenBroker,
|
|
78
|
+
OpenCodeAuthBroker,
|
|
79
|
+
OpenCodeFreeBroker,
|
|
80
|
+
resolveOpenCodeAuthPath,
|
|
81
|
+
} from "./security/credential-broker";
|
|
65
82
|
import { authorizeWorkspaceCwd } from "./security/execution-authority";
|
|
83
|
+
import { readOpenCodeSecretNames } from "./security/opencode-secret-manifest";
|
|
66
84
|
import {
|
|
67
85
|
type AdmitRoutePlanOptions,
|
|
68
86
|
type AdmittedRoutePlanResult,
|
|
@@ -124,7 +142,9 @@ export interface ProductionRuntime {
|
|
|
124
142
|
}
|
|
125
143
|
|
|
126
144
|
interface ActiveTransportLeaseTracker {
|
|
127
|
-
|
|
145
|
+
// U4: the transport is remembered so the lease that ends LAST closes it (and
|
|
146
|
+
// with it the route's OpenCode server), whichever path ended it.
|
|
147
|
+
register(routeKey: string, transport?: ProviderTransport): void;
|
|
128
148
|
/** Returns true when the last active lease for this routeKey was released. */
|
|
129
149
|
unregister(routeKey: string): boolean;
|
|
130
150
|
activeCount(routeKey: string): number;
|
|
@@ -136,7 +156,19 @@ interface ActiveTransportLeaseTracker {
|
|
|
136
156
|
routeKey: string;
|
|
137
157
|
registry: { release?(routeFingerprint: string): void };
|
|
138
158
|
}): Promise<void>;
|
|
139
|
-
releaseAll(registry: {
|
|
159
|
+
releaseAll(registry: {
|
|
160
|
+
release?(routeFingerprint: string): void;
|
|
161
|
+
}): Promise<void>;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// U4: close a transport that owns a process (the OpenCode SDK transport's
|
|
165
|
+
// server). Transports without one (the Claude CLI, test fakes) have no dispose.
|
|
166
|
+
// A failure to close is swallowed like every other teardown here: it must
|
|
167
|
+
// never replace the step's own outcome.
|
|
168
|
+
async function disposeTransport(transport: ProviderTransport): Promise<void> {
|
|
169
|
+
if ("dispose" in transport && typeof transport.dispose === "function") {
|
|
170
|
+
await (transport as { dispose(): Promise<void> }).dispose().catch(() => {});
|
|
171
|
+
}
|
|
140
172
|
}
|
|
141
173
|
|
|
142
174
|
class DefaultActiveTransportLeaseTracker
|
|
@@ -144,9 +176,13 @@ class DefaultActiveTransportLeaseTracker
|
|
|
144
176
|
{
|
|
145
177
|
private readonly refcounts = new Map<string, number>();
|
|
146
178
|
private readonly gates = new Map<string, Promise<void>>();
|
|
179
|
+
private readonly transports = new Map<string, ProviderTransport>();
|
|
147
180
|
|
|
148
|
-
register(routeKey: string): void {
|
|
181
|
+
register(routeKey: string, transport?: ProviderTransport): void {
|
|
149
182
|
this.refcounts.set(routeKey, (this.refcounts.get(routeKey) ?? 0) + 1);
|
|
183
|
+
if (transport !== undefined && !this.transports.has(routeKey)) {
|
|
184
|
+
this.transports.set(routeKey, transport);
|
|
185
|
+
}
|
|
150
186
|
}
|
|
151
187
|
|
|
152
188
|
unregister(routeKey: string): boolean {
|
|
@@ -171,6 +207,12 @@ class DefaultActiveTransportLeaseTracker
|
|
|
171
207
|
if (this.activeCount(input.routeKey) !== 1) {
|
|
172
208
|
return;
|
|
173
209
|
}
|
|
210
|
+
// U4: closed here, so the lease's end (endLease) does not close it a
|
|
211
|
+
// second time. A later lease on the same cached transport registers it
|
|
212
|
+
// again.
|
|
213
|
+
if (this.transports.get(input.routeKey) === input.transport) {
|
|
214
|
+
this.transports.delete(input.routeKey);
|
|
215
|
+
}
|
|
174
216
|
if (
|
|
175
217
|
"dispose" in input.transport &&
|
|
176
218
|
typeof input.transport.dispose === "function"
|
|
@@ -186,16 +228,33 @@ class DefaultActiveTransportLeaseTracker
|
|
|
186
228
|
}): Promise<void> {
|
|
187
229
|
await this.exclusive(input.routeKey, async () => {
|
|
188
230
|
if (this.unregister(input.routeKey)) {
|
|
231
|
+
// U4: the last lease on a route closes its transport. The harness's
|
|
232
|
+
// own teardown (teardownTransportIfLast) runs before the step's
|
|
233
|
+
// credential projection is destroyed, but two concurrent steps on one
|
|
234
|
+
// route can both pass it while the other still holds a lease, and
|
|
235
|
+
// then nobody closes the server; this cannot be raced, since only one
|
|
236
|
+
// lease is ever last. A transport the harness already closed was
|
|
237
|
+
// forgotten there, so it is not closed twice.
|
|
238
|
+
const transport = this.transports.get(input.routeKey);
|
|
239
|
+
this.transports.delete(input.routeKey);
|
|
240
|
+
if (transport !== undefined) await disposeTransport(transport);
|
|
189
241
|
input.registry.release?.(input.routeKey);
|
|
190
242
|
}
|
|
191
243
|
});
|
|
192
244
|
}
|
|
193
245
|
|
|
194
|
-
|
|
246
|
+
// U4: a lease never returned (a step torn down mid-flight) still has its
|
|
247
|
+
// transport closed when the runtime is disposed, so no server outlives it.
|
|
248
|
+
async releaseAll(registry: {
|
|
249
|
+
release?(routeFingerprint: string): void;
|
|
250
|
+
}): Promise<void> {
|
|
251
|
+
const transports = [...this.transports.values()];
|
|
252
|
+
this.transports.clear();
|
|
195
253
|
for (const routeKey of [...this.refcounts.keys()]) {
|
|
196
254
|
registry.release?.(routeKey);
|
|
197
255
|
}
|
|
198
256
|
this.refcounts.clear();
|
|
257
|
+
await Promise.all(transports.map((t) => disposeTransport(t)));
|
|
199
258
|
}
|
|
200
259
|
|
|
201
260
|
private async exclusive<T>(
|
|
@@ -497,11 +556,22 @@ class FrozenRuntimeBinding implements RuntimeBinding {
|
|
|
497
556
|
? {
|
|
498
557
|
openCodeBinaryPath: this.executable.absolutePath,
|
|
499
558
|
observedOpenCodeIdentity: this.observedOpenCodeIdentity,
|
|
559
|
+
// U4 (S9): the broker travels with its kind, for the same reason.
|
|
560
|
+
// The server this route's transport launches is projected by
|
|
561
|
+
// `openCodeLaunchServerFor(merged)` from `merged.credentialBroker`,
|
|
562
|
+
// which used to be only the registry-wide default: one broker for
|
|
563
|
+
// every OpenCode route, so a plan naming two providers would
|
|
564
|
+
// project one provider's record into the other's server. This is
|
|
565
|
+
// the binding's own instance, so the server, the binding and the
|
|
566
|
+
// capability report share one credential per route (#149).
|
|
567
|
+
...(this.credential.broker === undefined
|
|
568
|
+
? {}
|
|
569
|
+
: { credentialBroker: this.credential.broker }),
|
|
500
570
|
}
|
|
501
571
|
: {}),
|
|
502
572
|
});
|
|
503
573
|
const routeKey = this.key;
|
|
504
|
-
this.leaseTracker.register(routeKey);
|
|
574
|
+
this.leaseTracker.register(routeKey, transport);
|
|
505
575
|
return {
|
|
506
576
|
transport,
|
|
507
577
|
dispose: async () => {
|
|
@@ -526,7 +596,9 @@ function uniqueRoutesByFingerprint(
|
|
|
526
596
|
plan: ResolvedRoutePlan,
|
|
527
597
|
): readonly ResolvedStepRoute[] {
|
|
528
598
|
const seen = new Map<string, ResolvedStepRoute>();
|
|
529
|
-
|
|
599
|
+
// Fallbacks included (S4): a fallback is bound up front with the rest, so
|
|
600
|
+
// one that cannot run fails before the run spends, not when its turn comes.
|
|
601
|
+
for (const step of planRoutes(plan)) {
|
|
530
602
|
if (!seen.has(step.routeFingerprint)) {
|
|
531
603
|
seen.set(step.routeFingerprint, step);
|
|
532
604
|
}
|
|
@@ -665,9 +737,13 @@ async function resolveFrozenBindings(
|
|
|
665
737
|
step.route.modelSnapshot,
|
|
666
738
|
);
|
|
667
739
|
if (free) {
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
740
|
+
// U4: the run's pool when there is one, so every route on this free
|
|
741
|
+
// credential shares one broker with the server it launches.
|
|
742
|
+
const freeBroker = openCodeBrokerForRoute(
|
|
743
|
+
options,
|
|
744
|
+
"provider_free",
|
|
745
|
+
step.route.provider,
|
|
746
|
+
);
|
|
671
747
|
authority = {
|
|
672
748
|
...authority,
|
|
673
749
|
credentialKind: "provider_free",
|
|
@@ -793,26 +869,44 @@ export async function probeBindingsReadiness(
|
|
|
793
869
|
bindings,
|
|
794
870
|
registry,
|
|
795
871
|
dispose: async () => {
|
|
796
|
-
leaseTracker.releaseAll(registry);
|
|
872
|
+
await leaseTracker.releaseAll(registry);
|
|
797
873
|
},
|
|
798
874
|
};
|
|
799
875
|
}
|
|
800
876
|
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
877
|
+
// The plan `crysk doctor` probes: the two default models a zero-config review
|
|
878
|
+
// runs (a sonnet hunter, an opus refuter), plus one step per distinct route
|
|
879
|
+
// `models` names, fallbacks included, so every binding a configured review
|
|
880
|
+
// could open is checked before a review needs it. Distinct by route, not by
|
|
881
|
+
// entry: an OpenCode effort is the route's variant and so a binding of its own,
|
|
882
|
+
// while a Claude effort is not part of the route and is probed once.
|
|
883
|
+
export function buildDoctorRoutePlan(models?: ModelsConfig): ResolvedRoutePlan {
|
|
884
|
+
const base = buildResolvedRoutePlan({
|
|
805
885
|
agents: [
|
|
806
886
|
{ key: "reliability", role: "hunter", model: "sonnet" },
|
|
807
887
|
{ key: "refuter", role: "refuter", model: "opus" },
|
|
808
888
|
],
|
|
809
|
-
...(routingConfig === undefined ? {} : { routingConfig }),
|
|
810
889
|
});
|
|
890
|
+
if (models === undefined) return base;
|
|
891
|
+
const distinct = new Map<string, ResolvedStepRoute>();
|
|
892
|
+
for (const entry of Object.values(models).flat()) {
|
|
893
|
+
const step = resolveModelEntryRoute("doctor-model", "other", entry);
|
|
894
|
+
if (!distinct.has(step.routeFingerprint)) {
|
|
895
|
+
distinct.set(step.routeFingerprint, step);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
return createResolvedRoutePlan([
|
|
899
|
+
...base.steps,
|
|
900
|
+
...[...distinct.values()].map((step, i) => ({
|
|
901
|
+
...step,
|
|
902
|
+
stepKey: `doctor-model-${i + 1}`,
|
|
903
|
+
})),
|
|
904
|
+
]);
|
|
811
905
|
}
|
|
812
906
|
|
|
813
907
|
export async function collectDoctorExactBindingReports(input: {
|
|
814
908
|
readonly workspaceRoot: string;
|
|
815
|
-
readonly
|
|
909
|
+
readonly models?: ModelsConfig;
|
|
816
910
|
readonly authorityDeps?: ResolveRunnerAuthorityDeps;
|
|
817
911
|
readonly env?: RunnerAuthorityOptions["env"];
|
|
818
912
|
readonly observeOpenCodeIdentity?: OpenCodeIdentityObserver;
|
|
@@ -820,12 +914,22 @@ export async function collectDoctorExactBindingReports(input: {
|
|
|
820
914
|
import("./transports/opencode-client").OpenCodeSdkLike
|
|
821
915
|
>;
|
|
822
916
|
}): Promise<readonly ExactBindingCapabilityReport[]> {
|
|
823
|
-
const plan = buildDoctorRoutePlan(input.
|
|
917
|
+
const plan = buildDoctorRoutePlan(input.models);
|
|
918
|
+
// U3: the doctor binds OpenCode routes the way a review's admission does, so
|
|
919
|
+
// it reads the setup step's CRYSK_* manifest too.
|
|
920
|
+
const openCodeSecretNames = planRoutes(plan).some(
|
|
921
|
+
(step) => step.route.backend === "opencode",
|
|
922
|
+
)
|
|
923
|
+
? readOpenCodeSecretNames(input.env ?? process.env)
|
|
924
|
+
: undefined;
|
|
824
925
|
const authority = await prepareProductionRunnerAuthority(
|
|
825
926
|
input.workspaceRoot,
|
|
826
927
|
plan,
|
|
827
928
|
input.authorityDeps,
|
|
828
|
-
|
|
929
|
+
{
|
|
930
|
+
...(input.env === undefined ? {} : { env: input.env }),
|
|
931
|
+
...(openCodeSecretNames === undefined ? {} : { openCodeSecretNames }),
|
|
932
|
+
},
|
|
829
933
|
);
|
|
830
934
|
if ("error" in authority) {
|
|
831
935
|
throw new Error(authority.error);
|
|
@@ -891,47 +995,360 @@ export interface ProductionAdmissionContext {
|
|
|
891
995
|
readonly freeModelProbe?: FreeModelProbe;
|
|
892
996
|
}
|
|
893
997
|
|
|
894
|
-
//
|
|
895
|
-
//
|
|
896
|
-
//
|
|
897
|
-
//
|
|
898
|
-
//
|
|
899
|
-
//
|
|
998
|
+
// U4 (S9, L12 "Several providers"): the OpenCode credentials a plan runs on,
|
|
999
|
+
// one per (provider, kind). It replaces `soleOpenCodeCredential`, which refused
|
|
1000
|
+
// any plan naming two providers, or mixing a free and a metered route, because
|
|
1001
|
+
// one broker served every OpenCode route. Each route now carries its own broker
|
|
1002
|
+
// to the server its transport launches (FrozenRuntimeBinding.acquire), and the
|
|
1003
|
+
// registry caches one transport, so one server, per route: two routes on two
|
|
1004
|
+
// credentials never share a server, and each server projects only its route's
|
|
1005
|
+
// record (credential-broker.ts, buildOpenCodeProjection).
|
|
900
1006
|
//
|
|
901
|
-
//
|
|
902
|
-
//
|
|
903
|
-
//
|
|
904
|
-
//
|
|
1007
|
+
// A route's kind is `credentialKindForRoute` over the CRYSK_* names (U3), then
|
|
1008
|
+
// `provider_free` when the run's free verdict for (provider, model) says so,
|
|
1009
|
+
// the same upgrade `resolveFrozenBindings` applies to the binding, so the list
|
|
1010
|
+
// and the bindings cannot disagree. Fallbacks count (S4): every model a role may
|
|
1011
|
+
// run is bound up front.
|
|
1012
|
+
export interface OpenCodeRouteCredential {
|
|
1013
|
+
readonly provider: string;
|
|
1014
|
+
readonly kind: CredentialKind;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
export function planOpenCodeCredentials(
|
|
1018
|
+
plan: ResolvedRoutePlan,
|
|
1019
|
+
openCodeSecretNames: readonly string[] | undefined,
|
|
1020
|
+
freeVerdicts: ReadonlyMap<string, boolean> = new Map(),
|
|
1021
|
+
): readonly OpenCodeRouteCredential[] {
|
|
1022
|
+
const credentials = new Map<string, OpenCodeRouteCredential>();
|
|
1023
|
+
for (const step of planRoutes(plan)) {
|
|
1024
|
+
if (step.route.backend !== "opencode") continue;
|
|
1025
|
+
const kind = openCodeRouteKind(step, openCodeSecretNames, freeVerdicts);
|
|
1026
|
+
credentials.set(JSON.stringify([step.route.provider, kind]), {
|
|
1027
|
+
provider: step.route.provider,
|
|
1028
|
+
kind,
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
return [...credentials.values()].sort(
|
|
1032
|
+
(a, b) =>
|
|
1033
|
+
a.provider.localeCompare(b.provider) || a.kind.localeCompare(b.kind),
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// The kind one OpenCode route binds: planOpenCodeCredentials's rule, shared with
|
|
1038
|
+
// admission's credential check (openCodeCredentialRefusal) so the two cannot
|
|
1039
|
+
// disagree about which routes need a record.
|
|
1040
|
+
function openCodeRouteKind(
|
|
1041
|
+
step: ResolvedStepRoute,
|
|
1042
|
+
openCodeSecretNames: readonly string[] | undefined,
|
|
1043
|
+
freeVerdicts: ReadonlyMap<string, boolean>,
|
|
1044
|
+
): CredentialKind {
|
|
1045
|
+
const base = credentialKindForRoute(
|
|
1046
|
+
"opencode",
|
|
1047
|
+
step.route.provider,
|
|
1048
|
+
{},
|
|
1049
|
+
false,
|
|
1050
|
+
openCodeSecretNames,
|
|
1051
|
+
);
|
|
1052
|
+
return base === "provider_api_token" &&
|
|
1053
|
+
freeVerdicts.get(
|
|
1054
|
+
freeVerdictKey(step.route.provider, step.route.modelSnapshot),
|
|
1055
|
+
) === true
|
|
1056
|
+
? "provider_free"
|
|
1057
|
+
: base;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
// The plan-wide credential, when there is exactly one. A single-credential plan
|
|
1061
|
+
// keeps everything it had before U4: admission seeds that one broker into
|
|
1062
|
+
// `credentialBrokers.opencode` and the registry's default broker and kind, so
|
|
1063
|
+
// the bindings, the probe registry and the server all share one instance. A plan
|
|
1064
|
+
// with no OpenCode route keeps the pre-#133 OAuth default (nothing reads it). A
|
|
1065
|
+
// plan with several has no plan-wide answer: `undefined`, and every route
|
|
1066
|
+
// brings its own.
|
|
1067
|
+
export function soleOpenCodeCredentialOf(
|
|
1068
|
+
credentials: readonly OpenCodeRouteCredential[],
|
|
1069
|
+
): OpenCodeRouteCredential | undefined {
|
|
1070
|
+
if (credentials.length === 0) {
|
|
1071
|
+
return {
|
|
1072
|
+
provider: OPENCODE_OAUTH_PROVIDER,
|
|
1073
|
+
kind: "opencode_chatgpt_oauth",
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1076
|
+
return credentials.length === 1 ? credentials[0] : undefined;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// One broker cannot project several credentials: injected as
|
|
1080
|
+
// `credentialBrokers.opencode`, it would serve every route, and a metered
|
|
1081
|
+
// broker for one provider projects that provider's record into another
|
|
1082
|
+
// provider's server, which no type lock catches when both kinds are metered.
|
|
1083
|
+
// Refused by name, before anything runs.
|
|
1084
|
+
function sharedOpenCodeBrokerRefusal(
|
|
1085
|
+
credentials: readonly OpenCodeRouteCredential[],
|
|
1086
|
+
): string {
|
|
1087
|
+
return `a single OpenCode credential broker (credentialBrokers.opencode) cannot serve a plan on ${credentials.length} OpenCode credentials (${credentials.map((c) => `${c.provider} ${c.kind}`).join(", ")}); pass openCodeBrokerFor instead`;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
// The free verdict of every metered OpenCode route in the plan, through the
|
|
1091
|
+
// run's one memoised probe. OAuth routes never probe (a subscription model is
|
|
1092
|
+
// not free by declaration), exactly as before U4.
|
|
1093
|
+
async function openCodeFreeVerdicts(
|
|
1094
|
+
plan: ResolvedRoutePlan,
|
|
1095
|
+
openCodeSecretNames: readonly string[] | undefined,
|
|
1096
|
+
probe: FreeModelProbe,
|
|
1097
|
+
): Promise<Map<string, boolean>> {
|
|
1098
|
+
const pairs = new Map<string, { provider: string; model: string }>();
|
|
1099
|
+
for (const step of planRoutes(plan)) {
|
|
1100
|
+
if (step.route.backend !== "opencode") continue;
|
|
1101
|
+
const kind = credentialKindForRoute(
|
|
1102
|
+
"opencode",
|
|
1103
|
+
step.route.provider,
|
|
1104
|
+
{},
|
|
1105
|
+
false,
|
|
1106
|
+
openCodeSecretNames,
|
|
1107
|
+
);
|
|
1108
|
+
if (kind !== "provider_api_token") continue;
|
|
1109
|
+
pairs.set(freeVerdictKey(step.route.provider, step.route.modelSnapshot), {
|
|
1110
|
+
provider: step.route.provider,
|
|
1111
|
+
model: step.route.modelSnapshot,
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
const verdicts = new Map<string, boolean>();
|
|
1115
|
+
await Promise.all(
|
|
1116
|
+
[...pairs.entries()].map(async ([key, { provider, model }]) => {
|
|
1117
|
+
verdicts.set(key, await probe(provider, model));
|
|
1118
|
+
}),
|
|
1119
|
+
);
|
|
1120
|
+
return verdicts;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// S7 (E-g, E2): an OpenCode `models` entry's effort is sent as the model's
|
|
1124
|
+
// variant, and OpenCode accepts a variant the model does not have without an
|
|
1125
|
+
// error and runs it at the default level (L34). So every variant a plan would
|
|
1126
|
+
// send, fallbacks included, is checked against the variants OpenCode lists for
|
|
1127
|
+
// that model before the run starts, and refused by name: an unknown variant,
|
|
1128
|
+
// a model with no variants, and a listing that could not be read all stop the
|
|
1129
|
+
// review here, before the cost confirmation. A plan sending no variant (no
|
|
1130
|
+
// OpenCode entry sets an effort, D1) checks nothing and spawns nothing.
|
|
905
1131
|
//
|
|
906
|
-
//
|
|
907
|
-
//
|
|
908
|
-
// the
|
|
909
|
-
export function
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1132
|
+
// The listing is `opencode models <provider> --verbose --refresh`, the same
|
|
1133
|
+
// read the free check makes (model/free-discovery.ts); admission launches no
|
|
1134
|
+
// server, so the server's own model list is not reachable here.
|
|
1135
|
+
export async function openCodeVariantRefusal(
|
|
1136
|
+
plan: ResolvedRoutePlan,
|
|
1137
|
+
probe: ModelVariantsProbe | undefined,
|
|
1138
|
+
): Promise<string | undefined> {
|
|
1139
|
+
const checks = new Map<
|
|
1140
|
+
string,
|
|
1141
|
+
{ readonly step: ResolvedStepRoute; readonly stepKeys: string[] }
|
|
1142
|
+
>();
|
|
1143
|
+
for (const step of planRoutes(plan)) {
|
|
1144
|
+
if (
|
|
1145
|
+
step.route.backend !== "opencode" ||
|
|
1146
|
+
step.route.modelVariant === undefined
|
|
1147
|
+
) {
|
|
1148
|
+
continue;
|
|
914
1149
|
}
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
1150
|
+
const key = JSON.stringify([
|
|
1151
|
+
step.route.provider,
|
|
1152
|
+
step.route.modelSnapshot,
|
|
1153
|
+
step.route.modelVariant,
|
|
1154
|
+
]);
|
|
1155
|
+
const found = checks.get(key);
|
|
1156
|
+
if (found === undefined) {
|
|
1157
|
+
checks.set(key, { step, stepKeys: [step.stepKey] });
|
|
1158
|
+
} else if (!found.stepKeys.includes(step.stepKey)) {
|
|
1159
|
+
found.stepKeys.push(step.stepKey);
|
|
924
1160
|
}
|
|
925
1161
|
}
|
|
926
|
-
if (
|
|
1162
|
+
if (checks.size === 0) return undefined;
|
|
1163
|
+
const refusals = await Promise.all(
|
|
1164
|
+
[...checks.values()].map(async ({ step, stepKeys }) => {
|
|
1165
|
+
const variant = step.route.modelVariant ?? "";
|
|
1166
|
+
const what = `effort "${variant}" for ${step.logicalIdentity}`;
|
|
1167
|
+
const where = `(${stepKeys.join(", ")})`;
|
|
1168
|
+
const listing =
|
|
1169
|
+
probe === undefined
|
|
1170
|
+
? {
|
|
1171
|
+
kind: "unavailable" as const,
|
|
1172
|
+
reason: "no OpenCode binary to list them with",
|
|
1173
|
+
}
|
|
1174
|
+
: await probe(step.route.provider, step.route.modelSnapshot);
|
|
1175
|
+
if (listing.kind === "unavailable") {
|
|
1176
|
+
return `${what} could not be checked against its OpenCode variants (${listing.reason}); OpenCode runs a variant the model does not have at its default level without an error, so the review does not start on an unchecked one: fix that, or remove the effort ${where}`;
|
|
1177
|
+
}
|
|
1178
|
+
if (listing.variants.length === 0) {
|
|
1179
|
+
return `${what} cannot apply: OpenCode lists no variants for this model, so it would run at its default level: remove the effort ${where}`;
|
|
1180
|
+
}
|
|
1181
|
+
if (!listing.variants.includes(variant)) {
|
|
1182
|
+
return `${what} is not one of its OpenCode variants (${listing.variants.join(", ")}), and OpenCode would run it at the model's default level without an error: write one of those, or remove the effort ${where}`;
|
|
1183
|
+
}
|
|
1184
|
+
return undefined;
|
|
1185
|
+
}),
|
|
1186
|
+
);
|
|
1187
|
+
const refused = refusals.filter(
|
|
1188
|
+
(refusal): refusal is string => refusal !== undefined,
|
|
1189
|
+
);
|
|
1190
|
+
return refused.length === 0 ? undefined : `models: ${refused.join("; ")}`;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
// A plan refused because an OpenCode route has no credential record. `message`
|
|
1194
|
+
// is the refusal; `failureClass` and `params` let the CI record classify it
|
|
1195
|
+
// (CRYSK-CRED-UNUSABLE) and name the route's fixes. `params` describes the first
|
|
1196
|
+
// route refused; the message names every one.
|
|
1197
|
+
export interface OpenCodeCredentialRefusal {
|
|
1198
|
+
readonly message: string;
|
|
1199
|
+
readonly failureClass:
|
|
1200
|
+
| "missing_provider_record"
|
|
1201
|
+
| "missing_subscription_record";
|
|
1202
|
+
readonly params: {
|
|
1203
|
+
readonly backend: "opencode";
|
|
1204
|
+
readonly provider: string;
|
|
1205
|
+
readonly model: string;
|
|
1206
|
+
readonly step: string;
|
|
1207
|
+
readonly secret?: string;
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// The auth file's text, "{}" when there is no file (no record at all), or
|
|
1212
|
+
// undefined when it exists and cannot be read. WHY a missing file is not an
|
|
1213
|
+
// unreadable one: a CI run whose crysk-secrets built no OpenCode record writes no
|
|
1214
|
+
// auth file (setup.ts, assembleOpenCodeAuth), and that is exactly the run this
|
|
1215
|
+
// check exists to stop.
|
|
1216
|
+
async function readOpenCodeAuthText(
|
|
1217
|
+
authPath: string,
|
|
1218
|
+
): Promise<string | undefined> {
|
|
1219
|
+
try {
|
|
1220
|
+
return await Bun.file(authPath).text();
|
|
1221
|
+
} catch (error) {
|
|
1222
|
+
const code =
|
|
1223
|
+
typeof error === "object" && error !== null && "code" in error
|
|
1224
|
+
? error.code
|
|
1225
|
+
: undefined;
|
|
1226
|
+
return code === "ENOENT" ? "{}" : undefined;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
// The refusal for one route's missing record, in CI and locally. In CI it also names
|
|
1231
|
+
// the models input: setup builds the auth file only from the providers `models`
|
|
1232
|
+
// names, so a secret passed for a model given through the model input never lands.
|
|
1233
|
+
function missingRecordSentence(
|
|
1234
|
+
provider: string,
|
|
1235
|
+
kind: CredentialKind,
|
|
1236
|
+
stepKeys: readonly string[],
|
|
1237
|
+
inCi: boolean,
|
|
1238
|
+
): { readonly text: string; readonly secret?: string } {
|
|
1239
|
+
const where = `models name OpenCode provider ${provider} (${stepKeys.join(", ")})`;
|
|
1240
|
+
const oauth = kind === "opencode_chatgpt_oauth";
|
|
1241
|
+
const record = oauth
|
|
1242
|
+
? `${provider} subscription record`
|
|
1243
|
+
: `${provider} record`;
|
|
1244
|
+
if (!inCi) {
|
|
927
1245
|
return {
|
|
928
|
-
|
|
1246
|
+
text: oauth
|
|
1247
|
+
? `${where}, but the OpenCode auth file has no ${record}: run \`opencode auth login\` and choose ChatGPT`
|
|
1248
|
+
: `${where}, but the OpenCode auth file has no ${record}: run \`opencode auth login\` and add ${provider}'s API key`,
|
|
929
1249
|
};
|
|
930
1250
|
}
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1251
|
+
const token = opencodeSecretToken(provider);
|
|
1252
|
+
if (token === undefined) {
|
|
1253
|
+
return {
|
|
1254
|
+
text: `${where}, but the OpenCode auth file has no ${record}, and no CRYSK_OPENCODE_* secret name can carry this provider id`,
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1257
|
+
const apiKey = `CRYSK_OPENCODE_${token}_API_KEY`;
|
|
1258
|
+
if (oauth) {
|
|
1259
|
+
const secret = `CRYSK_OPENCODE_${token}_OAUTH`;
|
|
1260
|
+
return {
|
|
1261
|
+
text: `${where}, but no ${secret} secret reached the run (the OpenCode auth file has no ${record}): set it, or ${apiKey} to bill per token, pass it in the crysk-secrets input, and name the model in the models input (the model input builds no OpenCode auth file)`,
|
|
1262
|
+
secret,
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
return {
|
|
1266
|
+
text: `${where}, but no ${apiKey} secret reached the run (the OpenCode auth file has no ${record}): set it, pass it in the crysk-secrets input, and name the model in the models input (the model input builds no OpenCode auth file)`,
|
|
1267
|
+
secret: apiKey,
|
|
1268
|
+
};
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
// Every OpenCode route that will need a credential record, fallbacks included
|
|
1272
|
+
// (admission binds every model a role may run), is checked against the auth file
|
|
1273
|
+
// the run's env points to, the file the route's broker reads when it runs. A route
|
|
1274
|
+
// whose kind is free needs none (OpenCodeFreeBroker runs on an empty tree). The
|
|
1275
|
+
// check asks the brokers' own lookups, so it refuses exactly what the first step on
|
|
1276
|
+
// that route would fail on (CRYSK-CRED-UNUSABLE, credential-broker.ts), only before
|
|
1277
|
+
// anything is spent. It does not depend on the route's effort: a model with no
|
|
1278
|
+
// effort used to pass admission and fail at its first hunter, one with an effort
|
|
1279
|
+
// failed listing its variants with no word about the key.
|
|
1280
|
+
//
|
|
1281
|
+
// Not refused here: an auth file that exists and cannot be read, or is not a JSON
|
|
1282
|
+
// object. The broker's own error names those when the route runs.
|
|
1283
|
+
export async function openCodeCredentialRefusal(
|
|
1284
|
+
plan: ResolvedRoutePlan,
|
|
1285
|
+
openCodeSecretNames: readonly string[] | undefined,
|
|
1286
|
+
freeVerdicts: ReadonlyMap<string, boolean>,
|
|
1287
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
1288
|
+
): Promise<OpenCodeCredentialRefusal | undefined> {
|
|
1289
|
+
const needs = new Map<
|
|
1290
|
+
string,
|
|
1291
|
+
{
|
|
1292
|
+
readonly step: ResolvedStepRoute;
|
|
1293
|
+
readonly kind: CredentialKind;
|
|
1294
|
+
readonly stepKeys: string[];
|
|
1295
|
+
}
|
|
1296
|
+
>();
|
|
1297
|
+
for (const step of planRoutes(plan)) {
|
|
1298
|
+
if (step.route.backend !== "opencode") continue;
|
|
1299
|
+
const kind = openCodeRouteKind(step, openCodeSecretNames, freeVerdicts);
|
|
1300
|
+
if (kind === "provider_free") continue;
|
|
1301
|
+
const key = JSON.stringify([step.route.provider, kind]);
|
|
1302
|
+
const found = needs.get(key);
|
|
1303
|
+
if (found === undefined) {
|
|
1304
|
+
needs.set(key, { step, kind, stepKeys: [step.stepKey] });
|
|
1305
|
+
} else if (!found.stepKeys.includes(step.stepKey)) {
|
|
1306
|
+
found.stepKeys.push(step.stepKey);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
if (needs.size === 0) return undefined;
|
|
1310
|
+
const text = await readOpenCodeAuthText(resolveOpenCodeAuthPath(env));
|
|
1311
|
+
if (text === undefined) return undefined;
|
|
1312
|
+
const reader = async () => text;
|
|
1313
|
+
const inCi = env.GITHUB_ACTIONS === "true";
|
|
1314
|
+
const refused: {
|
|
1315
|
+
readonly sentence: string;
|
|
1316
|
+
readonly refusal: Omit<OpenCodeCredentialRefusal, "message">;
|
|
1317
|
+
}[] = [];
|
|
1318
|
+
for (const { step, kind, stepKeys } of needs.values()) {
|
|
1319
|
+
const provider = step.route.provider;
|
|
1320
|
+
const lookup =
|
|
1321
|
+
kind === "opencode_chatgpt_oauth"
|
|
1322
|
+
? await lookupOpenCodeOAuthRecord(reader)
|
|
1323
|
+
: await lookupOpenCodeApiRecord(reader, provider);
|
|
1324
|
+
if (lookup.ok) continue;
|
|
1325
|
+
if (
|
|
1326
|
+
lookup.failureClass !== "missing_provider_record" &&
|
|
1327
|
+
lookup.failureClass !== "missing_subscription_record"
|
|
1328
|
+
) {
|
|
1329
|
+
continue;
|
|
1330
|
+
}
|
|
1331
|
+
const sentence = missingRecordSentence(provider, kind, stepKeys, inCi);
|
|
1332
|
+
refused.push({
|
|
1333
|
+
sentence: sentence.text,
|
|
1334
|
+
refusal: {
|
|
1335
|
+
failureClass: lookup.failureClass,
|
|
1336
|
+
params: {
|
|
1337
|
+
backend: "opencode",
|
|
1338
|
+
provider,
|
|
1339
|
+
model: step.route.modelSnapshot,
|
|
1340
|
+
step: stepKeys[0] ?? step.stepKey,
|
|
1341
|
+
...(sentence.secret === undefined ? {} : { secret: sentence.secret }),
|
|
1342
|
+
},
|
|
1343
|
+
},
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
1346
|
+
const first = refused[0];
|
|
1347
|
+
if (first === undefined) return undefined;
|
|
1348
|
+
return {
|
|
1349
|
+
...first.refusal,
|
|
1350
|
+
message: refused.map((r) => r.sentence).join("; "),
|
|
1351
|
+
};
|
|
935
1352
|
}
|
|
936
1353
|
|
|
937
1354
|
export async function prepareProductionAdmissionContext(input: {
|
|
@@ -943,31 +1360,47 @@ export async function prepareProductionAdmissionContext(input: {
|
|
|
943
1360
|
import("./transports/opencode-client").OpenCodeSdkLike
|
|
944
1361
|
>;
|
|
945
1362
|
readonly env?: RunnerAuthorityOptions["env"];
|
|
1363
|
+
readonly claudeCredential?: RunnerAuthorityOptions["claudeCredential"];
|
|
1364
|
+
// U3: given (tests), or read here from the setup step's manifest beside the
|
|
1365
|
+
// OpenCode auth file `env` resolves to, once, when the plan names OpenCode.
|
|
1366
|
+
readonly openCodeSecretNames?: RunnerAuthorityOptions["openCodeSecretNames"];
|
|
946
1367
|
readonly credentialBrokers?: RunnerAuthorityOptions["credentialBrokers"];
|
|
947
|
-
//
|
|
948
|
-
//
|
|
949
|
-
|
|
950
|
-
//
|
|
951
|
-
//
|
|
1368
|
+
// U4: the run's per-credential broker pool (tests inject one over a fake
|
|
1369
|
+
// store); created here when absent and carried in the authority options.
|
|
1370
|
+
readonly openCodeBrokerFor?: RunnerAuthorityOptions["openCodeBrokerFor"];
|
|
1371
|
+
// False skips the missing-credential check: a local dry run authenticates
|
|
1372
|
+
// nothing, so it is never refused for a credential (src/review/run.ts).
|
|
1373
|
+
readonly checkCredentials?: boolean;
|
|
1374
|
+
// #182: see ProductionRuntimeOptions.freeModelProbe. U4: each metered
|
|
1375
|
+
// OpenCode route is asked about its own (provider, model) and binds free on a
|
|
1376
|
+
// true verdict, so a plan may mix free and metered routes, each billed by its
|
|
1377
|
+
// own kind. The memoised wrapper is returned so the runtime built from this
|
|
1378
|
+
// admission reuses its verdicts.
|
|
952
1379
|
readonly freeModelProbe?: FreeModelProbe;
|
|
953
|
-
|
|
954
|
-
//
|
|
955
|
-
//
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
//
|
|
966
|
-
//
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
1380
|
+
// S7: lists a model's OpenCode variants (openCodeVariantRefusal). Tests
|
|
1381
|
+
// inject one; otherwise it is built from the authority's OpenCode binary,
|
|
1382
|
+
// and only when the plan sends a variant.
|
|
1383
|
+
readonly modelVariantsProbe?: ModelVariantsProbe;
|
|
1384
|
+
}): Promise<
|
|
1385
|
+
| ProductionAdmissionContext
|
|
1386
|
+
| {
|
|
1387
|
+
readonly error: string;
|
|
1388
|
+
// Set when the error is a route with no credential record (S1).
|
|
1389
|
+
readonly credentialRefusal?: OpenCodeCredentialRefusal;
|
|
1390
|
+
}
|
|
1391
|
+
> {
|
|
1392
|
+
// U3: the CRYSK_OPENCODE_* names are read ONCE, here, and travel in the
|
|
1393
|
+
// authority options below, which the runtime and the binding probe are built
|
|
1394
|
+
// from, so the server's kind and every binding's kind follow the same names.
|
|
1395
|
+
const openCodeSecretNames =
|
|
1396
|
+
input.openCodeSecretNames ??
|
|
1397
|
+
(planRoutes(input.plan).some((step) => step.route.backend === "opencode")
|
|
1398
|
+
? readOpenCodeSecretNames(input.env ?? process.env)
|
|
1399
|
+
: undefined);
|
|
1400
|
+
// U4: one broker per credential for the whole run, shared by the bindings
|
|
1401
|
+
// below, the runtime built from this admission and each route's server.
|
|
1402
|
+
const openCodeBrokerFor =
|
|
1403
|
+
input.openCodeBrokerFor ?? createOpenCodeBrokerPool();
|
|
971
1404
|
// #182 follow-up, CLI threading: the authority is resolved BEFORE probe
|
|
972
1405
|
// evaluation (not after) so the probe can be built from the
|
|
973
1406
|
// AUTHORITY-RESOLVED openCodeBinaryPath — never a second binary lookup for
|
|
@@ -980,9 +1413,14 @@ export async function prepareProductionAdmissionContext(input: {
|
|
|
980
1413
|
input.authorityDeps,
|
|
981
1414
|
{
|
|
982
1415
|
...(input.env === undefined ? {} : { env: input.env }),
|
|
1416
|
+
...(input.claudeCredential === undefined
|
|
1417
|
+
? {}
|
|
1418
|
+
: { claudeCredential: input.claudeCredential }),
|
|
1419
|
+
...(openCodeSecretNames === undefined ? {} : { openCodeSecretNames }),
|
|
983
1420
|
...(input.credentialBrokers === undefined
|
|
984
1421
|
? {}
|
|
985
1422
|
: { credentialBrokers: input.credentialBrokers }),
|
|
1423
|
+
openCodeBrokerFor,
|
|
986
1424
|
},
|
|
987
1425
|
);
|
|
988
1426
|
if ("error" in preliminaryAuthority) {
|
|
@@ -1000,122 +1438,114 @@ export async function prepareProductionAdmissionContext(input: {
|
|
|
1000
1438
|
if (
|
|
1001
1439
|
probeInput === undefined &&
|
|
1002
1440
|
preliminaryAuthority.openCodeBinaryPath !== undefined &&
|
|
1003
|
-
input.plan.
|
|
1441
|
+
planRoutes(input.plan).some((step) => step.route.backend === "opencode")
|
|
1004
1442
|
) {
|
|
1005
1443
|
probeInput = createFreeModelProbe({
|
|
1006
1444
|
binaryPath: preliminaryAuthority.openCodeBinaryPath,
|
|
1007
1445
|
});
|
|
1008
1446
|
}
|
|
1009
1447
|
let sharedFreeProbe: FreeModelProbe | undefined;
|
|
1448
|
+
let freeVerdicts = new Map<string, boolean>();
|
|
1010
1449
|
if (probeInput !== undefined) {
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
if (
|
|
1035
|
-
|
|
1036
|
-
for (const step of input.plan.steps) {
|
|
1037
|
-
if (step.route.backend !== "opencode") continue;
|
|
1038
|
-
pairs.set(
|
|
1039
|
-
freeVerdictKey(step.route.provider, step.route.modelSnapshot),
|
|
1040
|
-
{
|
|
1041
|
-
provider: step.route.provider,
|
|
1042
|
-
model: step.route.modelSnapshot,
|
|
1043
|
-
},
|
|
1044
|
-
);
|
|
1045
|
-
}
|
|
1046
|
-
if (pairs.size > 0) {
|
|
1047
|
-
const results = await Promise.all(
|
|
1048
|
-
[...pairs.values()].map(async ({ provider, model }) => ({
|
|
1049
|
-
provider,
|
|
1050
|
-
model,
|
|
1051
|
-
free: await memoised(provider, model),
|
|
1052
|
-
})),
|
|
1053
|
-
);
|
|
1054
|
-
const freeCount = results.filter((r) => r.free).length;
|
|
1055
|
-
if (freeCount > 0 && freeCount < results.length) {
|
|
1056
|
-
const freeNames = results
|
|
1057
|
-
.filter((r) => r.free)
|
|
1058
|
-
.map((r) => `${r.provider}/${r.model}`)
|
|
1059
|
-
.sort();
|
|
1060
|
-
const paidNames = results
|
|
1061
|
-
.filter((r) => !r.free)
|
|
1062
|
-
.map((r) => `${r.provider}/${r.model}`)
|
|
1063
|
-
.sort();
|
|
1064
|
-
return {
|
|
1065
|
-
error: `plan mixes provider_free (${freeNames.join(", ")}) and metered (${paidNames.join(", ")}) OpenCode routes; the OpenCode server holds one credential for its whole life, so a mixed free/metered plan is not supported yet`,
|
|
1066
|
-
};
|
|
1067
|
-
}
|
|
1068
|
-
if (freeCount === results.length) {
|
|
1069
|
-
openCodeCredential = {
|
|
1070
|
-
kind: "provider_free",
|
|
1071
|
-
provider: openCodeCredential.provider,
|
|
1072
|
-
};
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1450
|
+
sharedFreeProbe = memoiseFreeModelProbe(probeInput);
|
|
1451
|
+
freeVerdicts = await openCodeFreeVerdicts(
|
|
1452
|
+
input.plan,
|
|
1453
|
+
openCodeSecretNames,
|
|
1454
|
+
sharedFreeProbe,
|
|
1455
|
+
);
|
|
1456
|
+
}
|
|
1457
|
+
// S1 (missing credential): after the free check, which decides which routes
|
|
1458
|
+
// need a record, and before the variants check, which lists the model through
|
|
1459
|
+
// the provider and fails with no word about the key when there is none. Skipped
|
|
1460
|
+
// on a dry run, and when the caller injected an OpenCode broker: the routes then
|
|
1461
|
+
// run on that broker, not on the auth file.
|
|
1462
|
+
if (
|
|
1463
|
+
input.checkCredentials !== false &&
|
|
1464
|
+
input.openCodeBrokerFor === undefined &&
|
|
1465
|
+
input.credentialBrokers?.opencode === undefined
|
|
1466
|
+
) {
|
|
1467
|
+
const credentialRefusal = await openCodeCredentialRefusal(
|
|
1468
|
+
input.plan,
|
|
1469
|
+
openCodeSecretNames,
|
|
1470
|
+
freeVerdicts,
|
|
1471
|
+
input.env ?? process.env,
|
|
1472
|
+
);
|
|
1473
|
+
if (credentialRefusal !== undefined) {
|
|
1474
|
+
return { error: credentialRefusal.message, credentialRefusal };
|
|
1075
1475
|
}
|
|
1076
1476
|
}
|
|
1077
|
-
//
|
|
1078
|
-
//
|
|
1079
|
-
//
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1477
|
+
// S7: after the credential check, before the bindings and the cost
|
|
1478
|
+
// confirmation. The probe is built only when a route sends a variant, so a plan
|
|
1479
|
+
// without one spawns nothing new.
|
|
1480
|
+
const sendsVariant = planRoutes(input.plan).some(
|
|
1481
|
+
(step) =>
|
|
1482
|
+
step.route.backend === "opencode" &&
|
|
1483
|
+
step.route.modelVariant !== undefined,
|
|
1484
|
+
);
|
|
1485
|
+
if (sendsVariant) {
|
|
1486
|
+
const variantsProbe =
|
|
1487
|
+
input.modelVariantsProbe ??
|
|
1488
|
+
(preliminaryAuthority.openCodeBinaryPath === undefined
|
|
1489
|
+
? undefined
|
|
1490
|
+
: createModelVariantsProbe({
|
|
1491
|
+
binaryPath: preliminaryAuthority.openCodeBinaryPath,
|
|
1492
|
+
}));
|
|
1493
|
+
const refusal = await openCodeVariantRefusal(input.plan, variantsProbe);
|
|
1494
|
+
if (refusal !== undefined) return { error: refusal };
|
|
1495
|
+
}
|
|
1496
|
+
const openCodeCredentials = planOpenCodeCredentials(
|
|
1497
|
+
input.plan,
|
|
1498
|
+
openCodeSecretNames,
|
|
1499
|
+
freeVerdicts,
|
|
1500
|
+
);
|
|
1501
|
+
const sole = soleOpenCodeCredentialOf(openCodeCredentials);
|
|
1502
|
+
if (sole === undefined && input.credentialBrokers?.opencode !== undefined) {
|
|
1503
|
+
return { error: sharedOpenCodeBrokerRefusal(openCodeCredentials) };
|
|
1504
|
+
}
|
|
1505
|
+
// #149: with ONE OpenCode credential the broker is resolved ONCE, here, and
|
|
1506
|
+
// seeded into the authority so binding resolution and the registry that
|
|
1507
|
+
// launches the server are handed the SAME object (#133: matched to the
|
|
1508
|
+
// plan's provider; #182: to its kind, so a free plan gets the free broker
|
|
1509
|
+
// everywhere). With several (U4) there is no plan-wide broker to seed: each
|
|
1510
|
+
// binding takes its credential's broker from the pool and carries it to its
|
|
1511
|
+
// own route's server (FrozenRuntimeBinding.acquire), which keeps the same
|
|
1512
|
+
// one-instance rule per credential.
|
|
1513
|
+
const credentialBrokers =
|
|
1514
|
+
sole === undefined
|
|
1515
|
+
? { ...input.credentialBrokers }
|
|
1516
|
+
: {
|
|
1517
|
+
...input.credentialBrokers,
|
|
1518
|
+
opencode:
|
|
1519
|
+
input.credentialBrokers?.opencode ??
|
|
1520
|
+
openCodeBrokerFor(sole.kind, sole.provider),
|
|
1521
|
+
};
|
|
1101
1522
|
// The preliminary authority above, with only the decided broker overlaid —
|
|
1102
1523
|
// re-resolving here would be a second binary lookup for the same binaries.
|
|
1103
1524
|
const authorityOptions: RunnerAuthorityOptions = {
|
|
1104
1525
|
...preliminaryAuthority,
|
|
1105
1526
|
credentialBrokers,
|
|
1106
1527
|
};
|
|
1528
|
+
// The registry-wide default credential: only a one-credential plan has one.
|
|
1529
|
+
// It is the fallback for a transport built without a binding; every binding
|
|
1530
|
+
// forwards its own broker and kind on acquire().
|
|
1531
|
+
const registryCredential =
|
|
1532
|
+
sole === undefined
|
|
1533
|
+
? {}
|
|
1534
|
+
: {
|
|
1535
|
+
// #149: the SAME instance seeded into the binding authority above.
|
|
1536
|
+
credentialBroker: credentialBrokers.opencode,
|
|
1537
|
+
// #133: and the kind it was resolved FOR. The launcher pairs the
|
|
1538
|
+
// two; sending the broker without its kind would leave the launcher
|
|
1539
|
+
// defaulting to the OAuth kind over an api-token broker.
|
|
1540
|
+
credentialKind: sole.kind,
|
|
1541
|
+
};
|
|
1107
1542
|
|
|
1108
1543
|
const probeRegistry = createDefaultTransportRegistry({
|
|
1109
1544
|
mode: "conformance",
|
|
1110
1545
|
binaryPath: authorityOptions.binaryPath,
|
|
1111
1546
|
openCodeBinaryPath: authorityOptions.openCodeBinaryPath,
|
|
1112
1547
|
env: authorityOptions.env,
|
|
1113
|
-
|
|
1114
|
-
credentialBroker: credentialBrokers.opencode,
|
|
1115
|
-
// #133: and the kind it was resolved FOR. The launcher pairs the two;
|
|
1116
|
-
// sending the broker without its kind would leave the launcher defaulting
|
|
1117
|
-
// to the OAuth kind over an api-token broker.
|
|
1118
|
-
credentialKind: openCodeCredential.kind,
|
|
1548
|
+
...registryCredential,
|
|
1119
1549
|
...(input.loadSdk !== undefined ? { loadSdk: input.loadSdk } : {}),
|
|
1120
1550
|
});
|
|
1121
1551
|
const probe = await probeBindingsReadiness({
|
|
@@ -1126,8 +1556,8 @@ export async function prepareProductionAdmissionContext(input: {
|
|
|
1126
1556
|
observeOpenCodeIdentity: input.observeOpenCodeIdentity,
|
|
1127
1557
|
mode: "conformance",
|
|
1128
1558
|
authorityDeps: input.authorityDeps,
|
|
1129
|
-
// #182: the SAME memoised instance the
|
|
1130
|
-
//
|
|
1559
|
+
// #182: the SAME memoised instance the credentials above were decided
|
|
1560
|
+
// with — bindings reuse its verdicts instead of re-spawning.
|
|
1131
1561
|
...(sharedFreeProbe === undefined
|
|
1132
1562
|
? {}
|
|
1133
1563
|
: { freeModelProbe: sharedFreeProbe }),
|
|
@@ -1153,11 +1583,26 @@ export async function prepareProductionAdmissionContext(input: {
|
|
|
1153
1583
|
return { error: `OpenCode SDK pre-confirm failed: ${message}` };
|
|
1154
1584
|
}
|
|
1155
1585
|
}
|
|
1586
|
+
// U4: the backend's D1-11 evidence holds only when it holds for EVERY
|
|
1587
|
+
// OpenCode binding. It used to keep the last binding's, harmless while
|
|
1588
|
+
// every route shared one credential and wrong once they do not.
|
|
1589
|
+
let combined: D1_11ReadinessEvidence | undefined;
|
|
1156
1590
|
for (const binding of probe.bindings.values()) {
|
|
1157
1591
|
if (binding.route.backend !== "opencode") continue;
|
|
1158
1592
|
const report = await binding.capabilities();
|
|
1159
|
-
|
|
1593
|
+
const next = d1_11EvidenceFromExactBinding(report, true);
|
|
1594
|
+
combined =
|
|
1595
|
+
combined === undefined
|
|
1596
|
+
? next
|
|
1597
|
+
: {
|
|
1598
|
+
sdkAvailable: combined.sdkAvailable && next.sdkAvailable,
|
|
1599
|
+
credentialAuthority:
|
|
1600
|
+
combined.credentialAuthority && next.credentialAuthority,
|
|
1601
|
+
workspaceBroker: combined.workspaceBroker && next.workspaceBroker,
|
|
1602
|
+
pricingReady: combined.pricingReady && next.pricingReady,
|
|
1603
|
+
};
|
|
1160
1604
|
}
|
|
1605
|
+
if (combined !== undefined) evidence.set("opencode", combined);
|
|
1161
1606
|
}
|
|
1162
1607
|
await probe.dispose();
|
|
1163
1608
|
|
|
@@ -1173,10 +1618,7 @@ export async function prepareProductionAdmissionContext(input: {
|
|
|
1173
1618
|
binaryPath: authorityOptions.binaryPath,
|
|
1174
1619
|
openCodeBinaryPath: authorityOptions.openCodeBinaryPath,
|
|
1175
1620
|
env: authorityOptions.env,
|
|
1176
|
-
|
|
1177
|
-
credentialBroker: credentialBrokers.opencode,
|
|
1178
|
-
// #133: and the kind it was resolved FOR. The launcher pairs the two;
|
|
1179
|
-
credentialKind: openCodeCredential.kind,
|
|
1621
|
+
...registryCredential,
|
|
1180
1622
|
...(input.loadSdk !== undefined ? { loadSdk: input.loadSdk } : {}),
|
|
1181
1623
|
}) as DefaultTransportRegistry;
|
|
1182
1624
|
|
|
@@ -1190,6 +1632,31 @@ export async function prepareProductionAdmissionContext(input: {
|
|
|
1190
1632
|
};
|
|
1191
1633
|
}
|
|
1192
1634
|
|
|
1635
|
+
// #182: one verdict per (provider, model) for the whole run. Memoised so the
|
|
1636
|
+
// probe spawns at most once per pair however many steps name it, and fail
|
|
1637
|
+
// closed: a throwing probe is an unknown price, not a free one.
|
|
1638
|
+
//
|
|
1639
|
+
// WHY freeVerdictKey, not `${provider}/${model}`: model ids may contain "/"
|
|
1640
|
+
// (model/free-discovery.ts header test), so the join collides ("a","b/c") vs
|
|
1641
|
+
// ("a/b","c") — one verdict for two models.
|
|
1642
|
+
function memoiseFreeModelProbe(probe: FreeModelProbe): FreeModelProbe {
|
|
1643
|
+
const verdicts = new Map<string, Promise<boolean>>();
|
|
1644
|
+
return (provider: string, model: string) => {
|
|
1645
|
+
const key = freeVerdictKey(provider, model);
|
|
1646
|
+
const cached = verdicts.get(key);
|
|
1647
|
+
if (cached !== undefined) return cached;
|
|
1648
|
+
const verdict = (async () => {
|
|
1649
|
+
try {
|
|
1650
|
+
return await probe(provider, model);
|
|
1651
|
+
} catch {
|
|
1652
|
+
return false;
|
|
1653
|
+
}
|
|
1654
|
+
})();
|
|
1655
|
+
verdicts.set(key, verdict);
|
|
1656
|
+
return verdict;
|
|
1657
|
+
};
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1193
1660
|
function createImmutableBindingsMap(
|
|
1194
1661
|
bindings: Map<string, RuntimeBinding>,
|
|
1195
1662
|
): ReadonlyMap<string, RuntimeBinding> {
|
|
@@ -1220,6 +1687,10 @@ export class MultiProviderRunner implements StepRunner {
|
|
|
1220
1687
|
private readonly attemptAdmissionGate?: AttemptAdmissionGate;
|
|
1221
1688
|
private readonly graceMarginMs?: number;
|
|
1222
1689
|
private readonly leaseTracker?: ActiveTransportLeaseTracker;
|
|
1690
|
+
// S5: the run env (one Claude credential, chosen at run start), handed to every
|
|
1691
|
+
// step's harness as its `childEnv`. Absent keeps the harness's own default, the
|
|
1692
|
+
// process environment.
|
|
1693
|
+
private readonly childEnv?: Readonly<Record<string, string | undefined>>;
|
|
1223
1694
|
// 2026-09-02: ONE ledger for the whole run, and note the shape — it is
|
|
1224
1695
|
// injectable but NOT optional-off, unlike `attemptAdmissionGate` beside it.
|
|
1225
1696
|
// That difference is the point: the concurrency gate is an opt-in policy, a
|
|
@@ -1247,6 +1718,7 @@ export class MultiProviderRunner implements StepRunner {
|
|
|
1247
1718
|
readonly attemptAdmissionGate?: AttemptAdmissionGate;
|
|
1248
1719
|
readonly graceMarginMs?: number;
|
|
1249
1720
|
readonly leaseTracker?: ActiveTransportLeaseTracker;
|
|
1721
|
+
readonly childEnv?: Readonly<Record<string, string | undefined>>;
|
|
1250
1722
|
// Injected only by tests that need to observe the CAS calls; production
|
|
1251
1723
|
// takes the default, which is the whole point of defaulting rather than
|
|
1252
1724
|
// requiring a caller to remember.
|
|
@@ -1261,6 +1733,7 @@ export class MultiProviderRunner implements StepRunner {
|
|
|
1261
1733
|
this.attemptAdmissionGate = options.attemptAdmissionGate;
|
|
1262
1734
|
this.graceMarginMs = options.graceMarginMs;
|
|
1263
1735
|
this.leaseTracker = options.leaseTracker;
|
|
1736
|
+
this.childEnv = options.childEnv;
|
|
1264
1737
|
this.spendLedger = options.spendLedger ?? new InMemorySpendLedger();
|
|
1265
1738
|
}
|
|
1266
1739
|
|
|
@@ -1275,8 +1748,7 @@ export class MultiProviderRunner implements StepRunner {
|
|
|
1275
1748
|
binding.route.provider === step.route.provider &&
|
|
1276
1749
|
binding.route.modelFamily === step.route.modelFamily &&
|
|
1277
1750
|
binding.route.modelSnapshot === step.route.modelSnapshot &&
|
|
1278
|
-
binding.route.modelVariant === step.route.modelVariant
|
|
1279
|
-
(binding.route.gateway ?? "") === (step.route.gateway ?? "")
|
|
1751
|
+
binding.route.modelVariant === step.route.modelVariant
|
|
1280
1752
|
) {
|
|
1281
1753
|
return binding;
|
|
1282
1754
|
}
|
|
@@ -1292,7 +1764,27 @@ export class MultiProviderRunner implements StepRunner {
|
|
|
1292
1764
|
return undefined;
|
|
1293
1765
|
}
|
|
1294
1766
|
|
|
1767
|
+
// A step whose role lists several models tries them in order (S4,
|
|
1768
|
+
// model-fallback.ts); every model's binding was frozen with the rest, so a
|
|
1769
|
+
// fallback runs through exactly the admission a first model does.
|
|
1770
|
+
//
|
|
1771
|
+
// "The same credential", for a refused login (`auth_invalid`), is the
|
|
1772
|
+
// binding's spend bucket: provider plus credential kind and ref
|
|
1773
|
+
// (`bindingBucketId`). Every Claude model shares one, so a Claude login
|
|
1774
|
+
// failure skips the other Claude models and moves to an OpenCode one, and
|
|
1775
|
+
// the reverse.
|
|
1295
1776
|
async run(step: StepSpec): Promise<StepResult> {
|
|
1777
|
+
return runWithFallback({
|
|
1778
|
+
step,
|
|
1779
|
+
runOn: (single) => this.runOnBinding(single),
|
|
1780
|
+
credentialOf: (route) =>
|
|
1781
|
+
this.resolveBinding({ ...step, route, routeKey: undefined })?.credential
|
|
1782
|
+
.bucketId,
|
|
1783
|
+
cancelled: () => this.signal?.aborted === true,
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
private async runOnBinding(step: StepSpec): Promise<StepResult> {
|
|
1296
1788
|
const binding = this.resolveBinding(step);
|
|
1297
1789
|
if (binding === undefined) {
|
|
1298
1790
|
return {
|
|
@@ -1467,6 +1959,10 @@ export class MultiProviderRunner implements StepRunner {
|
|
|
1467
1959
|
],
|
|
1468
1960
|
binaryPath: binding.executable.absolutePath,
|
|
1469
1961
|
credentialBroker: binding.credential.broker,
|
|
1962
|
+
// S5: the same env the binding's credential kind was decided from
|
|
1963
|
+
// (`options.env`, resolveBindingAuthority), so the child runs on the one
|
|
1964
|
+
// credential the run chose and on nothing the process happens to hold.
|
|
1965
|
+
...(this.childEnv === undefined ? {} : { childEnv: this.childEnv }),
|
|
1470
1966
|
transport: lease.transport,
|
|
1471
1967
|
spawnFn: this.spawnFn,
|
|
1472
1968
|
signal: this.signal,
|
|
@@ -1528,6 +2024,51 @@ export class MultiProviderRunner implements StepRunner {
|
|
|
1528
2024
|
}
|
|
1529
2025
|
}
|
|
1530
2026
|
|
|
2027
|
+
// #182 -> U4: never project a credential under a route of another kind. Before
|
|
2028
|
+
// U4 one server credential served every route, so the guard compared it with
|
|
2029
|
+
// the bindings; each route now carries its own broker to its own server, so
|
|
2030
|
+
// the check is per binding: a free route on a non-free broker (or the reverse)
|
|
2031
|
+
// is a wiring divergence (an admission and a runtime that decided the free
|
|
2032
|
+
// verdict differently, typically a runtime built without the admission's
|
|
2033
|
+
// probe), refused here by name instead of failing later at projection.
|
|
2034
|
+
//
|
|
2035
|
+
// A caller's single `credentialBrokers.opencode` on a plan with several
|
|
2036
|
+
// OpenCode credentials is refused at admission (sharedOpenCodeBrokerRefusal),
|
|
2037
|
+
// which is what every review builds its runtime from. A caller building a
|
|
2038
|
+
// runtime directly (tests, probes) keeps its injected broker for every route,
|
|
2039
|
+
// exactly as before U4.
|
|
2040
|
+
function refuseOpenCodeBrokerMismatch(
|
|
2041
|
+
bindings: ReadonlyMap<string, RuntimeBinding>,
|
|
2042
|
+
): void {
|
|
2043
|
+
const openCode = [...bindings.values()].filter(
|
|
2044
|
+
(b) => b.route.backend === "opencode",
|
|
2045
|
+
);
|
|
2046
|
+
// Only the production broker classes say which kind they project. Any other
|
|
2047
|
+
// broker (a caller's fake) is not judged here, as before U4; a real broker
|
|
2048
|
+
// handed the wrong kind still refuses it by name at projection.
|
|
2049
|
+
const mismatched = openCode
|
|
2050
|
+
.filter((b) => {
|
|
2051
|
+
const broker = b.credential.broker;
|
|
2052
|
+
const free = b.credential.kind === "provider_free";
|
|
2053
|
+
return (
|
|
2054
|
+
(broker instanceof OpenCodeFreeBroker && !free) ||
|
|
2055
|
+
((broker instanceof OpenCodeApiTokenBroker ||
|
|
2056
|
+
broker instanceof OpenCodeAuthBroker) &&
|
|
2057
|
+
free)
|
|
2058
|
+
);
|
|
2059
|
+
})
|
|
2060
|
+
.map(
|
|
2061
|
+
(b) =>
|
|
2062
|
+
`${b.route.provider}/${b.route.modelSnapshot} ${b.credential.kind}`,
|
|
2063
|
+
)
|
|
2064
|
+
.sort();
|
|
2065
|
+
if (mismatched.length > 0) {
|
|
2066
|
+
throw new ProductionRuntimeError(
|
|
2067
|
+
`${mismatched.length} OpenCode binding(s) resolved a credential kind their broker does not project (${mismatched.join(", ")}); refusing to serve a free route from a metered projection or a metered route from an empty one (forward the admission's freeModelProbe so both share one verdict)`,
|
|
2068
|
+
);
|
|
2069
|
+
}
|
|
2070
|
+
}
|
|
2071
|
+
|
|
1531
2072
|
export async function createProductionRuntime(
|
|
1532
2073
|
options: ProductionRuntimeOptions,
|
|
1533
2074
|
): Promise<ProductionRuntime> {
|
|
@@ -1543,81 +2084,20 @@ export async function createProductionRuntime(
|
|
|
1543
2084
|
|
|
1544
2085
|
const plan = freezeRoutePlan(options.plan);
|
|
1545
2086
|
// #182: when a probe is supplied but no registry is, decide the free
|
|
1546
|
-
//
|
|
1547
|
-
// one verdict. The memoised wrapper is what bindings
|
|
1548
|
-
// one spawn per (provider, model), never two that could
|
|
2087
|
+
// verdicts BEFORE building the fallback so the registry's default credential
|
|
2088
|
+
// and the bindings share one verdict. The memoised wrapper is what bindings
|
|
2089
|
+
// resolve with below — one spawn per (provider, model), never two that could
|
|
2090
|
+
// disagree.
|
|
1549
2091
|
let effectiveOptions = options;
|
|
1550
|
-
let
|
|
1551
|
-
| { readonly kind: CredentialKind; readonly provider: string }
|
|
1552
|
-
| undefined;
|
|
2092
|
+
let decidedCredentials: readonly OpenCodeRouteCredential[] | undefined;
|
|
1553
2093
|
if (options.freeModelProbe !== undefined && options.registry === undefined) {
|
|
1554
|
-
const
|
|
1555
|
-
const verdicts = new Map<string, Promise<boolean>>();
|
|
1556
|
-
const memoised: FreeModelProbe = (provider, model) => {
|
|
1557
|
-
// WHY freeVerdictKey, not `${provider}/${model}`: model ids may contain
|
|
1558
|
-
// "/" (model/free-discovery.ts header test), so the join collides
|
|
1559
|
-
// ("a","b/c") vs ("a/b","c") — one verdict for two models. The display
|
|
1560
|
-
// joins below stay human-readable on purpose: they never key a map.
|
|
1561
|
-
const key = freeVerdictKey(provider, model);
|
|
1562
|
-
const cached = verdicts.get(key);
|
|
1563
|
-
if (cached !== undefined) return cached;
|
|
1564
|
-
const verdict = (async () => {
|
|
1565
|
-
try {
|
|
1566
|
-
return await rawProbe(provider, model);
|
|
1567
|
-
} catch {
|
|
1568
|
-
return false;
|
|
1569
|
-
}
|
|
1570
|
-
})();
|
|
1571
|
-
verdicts.set(key, verdict);
|
|
1572
|
-
return verdict;
|
|
1573
|
-
};
|
|
2094
|
+
const memoised = memoiseFreeModelProbe(options.freeModelProbe);
|
|
1574
2095
|
effectiveOptions = { ...options, freeModelProbe: memoised };
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
const pairs = new Map<string, { provider: string; model: string }>();
|
|
1581
|
-
for (const step of plan.steps) {
|
|
1582
|
-
if (step.route.backend !== "opencode") continue;
|
|
1583
|
-
pairs.set(
|
|
1584
|
-
freeVerdictKey(step.route.provider, step.route.modelSnapshot),
|
|
1585
|
-
{
|
|
1586
|
-
provider: step.route.provider,
|
|
1587
|
-
model: step.route.modelSnapshot,
|
|
1588
|
-
},
|
|
1589
|
-
);
|
|
1590
|
-
}
|
|
1591
|
-
if (pairs.size > 0) {
|
|
1592
|
-
const results = await Promise.all(
|
|
1593
|
-
[...pairs.values()].map(async ({ provider, model }) => ({
|
|
1594
|
-
provider,
|
|
1595
|
-
model,
|
|
1596
|
-
free: await memoised(provider, model),
|
|
1597
|
-
})),
|
|
1598
|
-
);
|
|
1599
|
-
const freeCount = results.filter((r) => r.free).length;
|
|
1600
|
-
if (freeCount > 0 && freeCount < results.length) {
|
|
1601
|
-
const freeNames = results
|
|
1602
|
-
.filter((r) => r.free)
|
|
1603
|
-
.map((r) => `${r.provider}/${r.model}`)
|
|
1604
|
-
.sort();
|
|
1605
|
-
const paidNames = results
|
|
1606
|
-
.filter((r) => !r.free)
|
|
1607
|
-
.map((r) => `${r.provider}/${r.model}`)
|
|
1608
|
-
.sort();
|
|
1609
|
-
throw new ProductionRuntimeError(
|
|
1610
|
-
`plan mixes provider_free (${freeNames.join(", ")}) and metered (${paidNames.join(", ")}) OpenCode routes; the OpenCode server holds one credential for its whole life, so a mixed free/metered plan is not supported yet`,
|
|
1611
|
-
);
|
|
1612
|
-
}
|
|
1613
|
-
if (freeCount === results.length) {
|
|
1614
|
-
soleOverride = {
|
|
1615
|
-
kind: "provider_free",
|
|
1616
|
-
provider: base.provider,
|
|
1617
|
-
};
|
|
1618
|
-
}
|
|
1619
|
-
}
|
|
1620
|
-
}
|
|
2096
|
+
decidedCredentials = planOpenCodeCredentials(
|
|
2097
|
+
plan,
|
|
2098
|
+
options.openCodeSecretNames,
|
|
2099
|
+
await openCodeFreeVerdicts(plan, options.openCodeSecretNames, memoised),
|
|
2100
|
+
);
|
|
1621
2101
|
}
|
|
1622
2102
|
const registry =
|
|
1623
2103
|
effectiveOptions.registry ??
|
|
@@ -1627,10 +2107,11 @@ export async function createProductionRuntime(
|
|
|
1627
2107
|
binaryPath: effectiveOptions.binaryPath,
|
|
1628
2108
|
env: effectiveOptions.env,
|
|
1629
2109
|
credentialBrokers: effectiveOptions.credentialBrokers,
|
|
2110
|
+
openCodeSecretNames: effectiveOptions.openCodeSecretNames,
|
|
1630
2111
|
plan,
|
|
1631
|
-
...(
|
|
2112
|
+
...(decidedCredentials === undefined
|
|
1632
2113
|
? {}
|
|
1633
|
-
: {
|
|
2114
|
+
: { openCodeCredentials: decidedCredentials }),
|
|
1634
2115
|
});
|
|
1635
2116
|
|
|
1636
2117
|
const leaseTracker = new DefaultActiveTransportLeaseTracker();
|
|
@@ -1640,56 +2121,7 @@ export async function createProductionRuntime(
|
|
|
1640
2121
|
registry,
|
|
1641
2122
|
leaseTracker,
|
|
1642
2123
|
);
|
|
1643
|
-
|
|
1644
|
-
// a non-free server (or a free server with no free binding) is a wiring
|
|
1645
|
-
// divergence, not a fallback — refuse loudly. The precedent is the existing
|
|
1646
|
-
// mixed-provider refusal above: one server, one credential, no silent pick.
|
|
1647
|
-
if (registry instanceof DefaultTransportRegistry) {
|
|
1648
|
-
const serverKind = registry.openCodeCredentialKind;
|
|
1649
|
-
const hasFreeBinding = [...bindings.values()].some(
|
|
1650
|
-
(b) => b.credential.kind === "provider_free",
|
|
1651
|
-
);
|
|
1652
|
-
const hasOpenCodeBinding = [...bindings.values()].some(
|
|
1653
|
-
(b) => b.route.backend === "opencode",
|
|
1654
|
-
);
|
|
1655
|
-
if (hasFreeBinding && serverKind !== "provider_free") {
|
|
1656
|
-
throw new ProductionRuntimeError(
|
|
1657
|
-
`free OpenCode binding resolved but the server credential is ${serverKind ?? "unset"}; refusing to project a non-free credential under a free route (forward the admission's freeModelProbe so both share one verdict)`,
|
|
1658
|
-
);
|
|
1659
|
-
}
|
|
1660
|
-
if (
|
|
1661
|
-
serverKind === "provider_free" &&
|
|
1662
|
-
hasOpenCodeBinding &&
|
|
1663
|
-
!hasFreeBinding
|
|
1664
|
-
) {
|
|
1665
|
-
throw new ProductionRuntimeError(
|
|
1666
|
-
`OpenCode server holds a free credential but no binding resolved free; refusing to serve metered routes from an empty projection (forward the admission's freeModelProbe so both share one verdict)`,
|
|
1667
|
-
);
|
|
1668
|
-
}
|
|
1669
|
-
// #182 follow-up: the partial mix — a free server with SOME free and SOME
|
|
1670
|
-
// metered opencode bindings, reachable when the runtime probes with a
|
|
1671
|
-
// different instance than the admission (or none). Without this branch the
|
|
1672
|
-
// slipped metered binding fails closed LATER at projection with
|
|
1673
|
-
// missing_provider_record, and the harness fails the step pre-spawn — a
|
|
1674
|
-
// confusing failure for a wiring divergence. This branch upgrades it into
|
|
1675
|
-
// the actionable refusal the guard exists for, in the same shape as its
|
|
1676
|
-
// neighbors.
|
|
1677
|
-
if (serverKind === "provider_free" && hasFreeBinding) {
|
|
1678
|
-
const meteredNames = [...bindings.values()]
|
|
1679
|
-
.filter(
|
|
1680
|
-
(b) =>
|
|
1681
|
-
b.route.backend === "opencode" &&
|
|
1682
|
-
b.credential.kind !== "provider_free",
|
|
1683
|
-
)
|
|
1684
|
-
.map((b) => `${b.route.provider}/${b.route.modelSnapshot}`)
|
|
1685
|
-
.sort();
|
|
1686
|
-
if (meteredNames.length > 0) {
|
|
1687
|
-
throw new ProductionRuntimeError(
|
|
1688
|
-
`OpenCode server holds a free credential but ${meteredNames.length} binding(s) resolved metered (${meteredNames.join(", ")}); refusing to serve metered routes from an empty projection (forward the admission's freeModelProbe so both share one verdict)`,
|
|
1689
|
-
);
|
|
1690
|
-
}
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
2124
|
+
refuseOpenCodeBrokerMismatch(bindings);
|
|
1693
2125
|
// Backend-wide compatibility admission must consume the same observations
|
|
1694
2126
|
// as exact bindings, not reconstruct an unobserved factory beside them.
|
|
1695
2127
|
const compatibilityReports = new Map<
|
|
@@ -1730,6 +2162,7 @@ export async function createProductionRuntime(
|
|
|
1730
2162
|
attemptAdmissionGate: options.attemptAdmissionGate,
|
|
1731
2163
|
graceMarginMs: options.graceMarginMs,
|
|
1732
2164
|
leaseTracker,
|
|
2165
|
+
...(options.env === undefined ? {} : { childEnv: options.env }),
|
|
1733
2166
|
});
|
|
1734
2167
|
|
|
1735
2168
|
return {
|
|
@@ -1739,7 +2172,7 @@ export async function createProductionRuntime(
|
|
|
1739
2172
|
admitted,
|
|
1740
2173
|
evidence: options.evidence,
|
|
1741
2174
|
dispose: async () => {
|
|
1742
|
-
leaseTracker.releaseAll(registry);
|
|
2175
|
+
await leaseTracker.releaseAll(registry);
|
|
1743
2176
|
},
|
|
1744
2177
|
};
|
|
1745
2178
|
}
|
|
@@ -1795,6 +2228,8 @@ export function productionFallbackRegistry(options: {
|
|
|
1795
2228
|
// function is fed `options`/`authorityOptions` (both that type) directly.
|
|
1796
2229
|
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
1797
2230
|
readonly credentialBrokers?: RunnerAuthorityOptions["credentialBrokers"];
|
|
2231
|
+
// U3: the same names the bindings read, so the server's kind follows them too.
|
|
2232
|
+
readonly openCodeSecretNames?: RunnerAuthorityOptions["openCodeSecretNames"];
|
|
1798
2233
|
// #133: optional because this function is also called with a bare
|
|
1799
2234
|
// `{ mode }` by callers that never reach an opencode route. When it IS
|
|
1800
2235
|
// present it decides the credential KIND, and dropping it here is how this
|
|
@@ -1803,31 +2238,25 @@ export function productionFallbackRegistry(options: {
|
|
|
1803
2238
|
// reviewing its own fix.
|
|
1804
2239
|
readonly plan?: ResolvedRoutePlan;
|
|
1805
2240
|
// #182: async callers (createProductionRuntime with a freeModelProbe) decide
|
|
1806
|
-
// the free
|
|
1807
|
-
// spawn — and hand the decided
|
|
1808
|
-
// bindings share one verdict instead of two spawns that could
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
readonly provider: string;
|
|
1812
|
-
};
|
|
2241
|
+
// the free verdicts BEFORE building the fallback — this sync function cannot
|
|
2242
|
+
// spawn — and hand the decided credentials here so the registry's default
|
|
2243
|
+
// and the bindings share one verdict instead of two spawns that could
|
|
2244
|
+
// disagree.
|
|
2245
|
+
readonly openCodeCredentials?: readonly OpenCodeRouteCredential[];
|
|
1813
2246
|
}): TransportRegistry {
|
|
2247
|
+
// U4: the registry-wide default exists only when the plan runs on exactly one
|
|
2248
|
+
// OpenCode credential (or none: the pre-#133 OAuth default). With several,
|
|
2249
|
+
// there is no default, and nothing is refused: every binding forwards its own
|
|
2250
|
+
// broker and kind to the transport it acquires.
|
|
1814
2251
|
const credential =
|
|
1815
|
-
options.
|
|
1816
|
-
? options.
|
|
2252
|
+
options.openCodeCredentials !== undefined
|
|
2253
|
+
? soleOpenCodeCredentialOf(options.openCodeCredentials)
|
|
1817
2254
|
: options.plan === undefined
|
|
1818
2255
|
? undefined
|
|
1819
|
-
:
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
(credential as { error?: string }).error !== undefined
|
|
1824
|
-
) {
|
|
1825
|
-
throw new ProductionRuntimeError((credential as { error: string }).error);
|
|
1826
|
-
}
|
|
1827
|
-
const kind =
|
|
1828
|
-
credential === undefined
|
|
1829
|
-
? undefined
|
|
1830
|
-
: (credential as { kind: CredentialKind }).kind;
|
|
2256
|
+
: soleOpenCodeCredentialOf(
|
|
2257
|
+
planOpenCodeCredentials(options.plan, options.openCodeSecretNames),
|
|
2258
|
+
);
|
|
2259
|
+
const kind = credential?.kind;
|
|
1831
2260
|
// #182: when the decided kind is free and no broker was supplied, default to
|
|
1832
2261
|
// the free broker — NOT the OAuth default in openCodeLaunchServerFor. That
|
|
1833
2262
|
// default pairs an OAuth broker with whatever kind travels, and pairing it
|
|
@@ -1860,7 +2289,7 @@ export function productionFallbackRegistry(options: {
|
|
|
1860
2289
|
? {
|
|
1861
2290
|
credentialBroker: openCodeCredentialBrokerForKind(
|
|
1862
2291
|
"provider_free",
|
|
1863
|
-
(credential as
|
|
2292
|
+
(credential as OpenCodeRouteCredential).provider,
|
|
1864
2293
|
),
|
|
1865
2294
|
}
|
|
1866
2295
|
: {}
|