crysk 0.0.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +609 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/cli.js +42292 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +122 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +496 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +923 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +901 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +847 -0
- package/src/pr/ci-admission-gate.ts +665 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +787 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { OPENCODE_OAUTH_PROVIDER } from "../security/credential-broker";
|
|
2
|
+
|
|
3
|
+
// #182: live free-model discovery. OpenCode free models (backend `opencode`,
|
|
4
|
+
// provider `opencode`, e.g. `muse-spark-1.3-contributor-free`) authenticate
|
|
5
|
+
// with NOTHING, but without a liveness check they are refused at credential
|
|
6
|
+
// projection — there is no kind for "no credential".
|
|
7
|
+
//
|
|
8
|
+
// NO hardcoded model lists here or in any JSON: free-model identities rotate
|
|
9
|
+
// constantly, so discovery is LIVE — `<binaryPath> models <provider>
|
|
10
|
+
// --verbose --refresh`, parsed at runtime. The discriminator is what the
|
|
11
|
+
// provider itself declares: exact model id present AND status active AND
|
|
12
|
+
// input===0 AND output===0 AND cache.read===0 AND cache.write===0.
|
|
13
|
+
//
|
|
14
|
+
// WHY `--refresh`: a stale price is a CONFIDENT WRONG NUMBER, which is worse
|
|
15
|
+
// than no price — the gate exists to refuse billing an unknown amount, and a
|
|
16
|
+
// stale quote defeats it by making the unknown look known. That doctrine used
|
|
17
|
+
// to live in src/pricing-catalog.ts, whose bundled rate tables #197 deleted
|
|
18
|
+
// for failing it (they aged on our release cadence and then refused the very
|
|
19
|
+
// routes they existed to price); it is restated here rather than pointed at,
|
|
20
|
+
// because it is this function's reason and now has no other home. A cached
|
|
21
|
+
// all-zero cost that has since become metered would admit a paid run as free;
|
|
22
|
+
// offline failure must refuse loudly downstream (fail closed →
|
|
23
|
+
// provider_api_token), not silently pass.
|
|
24
|
+
//
|
|
25
|
+
// SCOPE NOTE (residual now guarded, #182 follow-up): between the probe and
|
|
26
|
+
// the attempt the provider could flip a model from free to metered
|
|
27
|
+
// (stale-cache window). The attempt then runs under an empty projection, and
|
|
28
|
+
// the flip is observable at settlement — the transport accumulates
|
|
29
|
+
// provider-reported cost per message (`cashCostUsd`, `costSource: "provider"`,
|
|
30
|
+
// opencode-sdk.ts), stamped with the route's billing mode ("free",
|
|
31
|
+
// transport-registry.ts). `settlementFromUsage`'s free-nonzero rule
|
|
32
|
+
// (spend-limiter.ts) routes complete usage with cash > 0 on a free-declared
|
|
33
|
+
// route to unresolved WITH reason `free_nonzero_cost` (carrying the provider's
|
|
34
|
+
// stated figure), the spend ledger fences the bucket, and the harness fails
|
|
35
|
+
// the attempt closed with no retry (re-probe is the remedy). Measured first:
|
|
36
|
+
// a legitimate free attempt reports cash 0 (Step-0 probe, "Say OK" on
|
|
37
|
+
// muse-spark-1.3-contributor-free), so the rule cannot misfire on list-price
|
|
38
|
+
// reporting. Free usage with NO cash figure releases WITHOUT fencing
|
|
39
|
+
// (harness.ts free-route fence scope) — no cash figure means no evidence of
|
|
40
|
+
// billing. Cancelled/undefined-usage and non-free incomplete usage still
|
|
41
|
+
// fence: no usage mode to exonerate them, so fail closed.
|
|
42
|
+
|
|
43
|
+
export type FreeModelProbe = (
|
|
44
|
+
provider: string,
|
|
45
|
+
model: string,
|
|
46
|
+
) => Promise<boolean>;
|
|
47
|
+
|
|
48
|
+
export interface FreeModelDiscoveryRunResult {
|
|
49
|
+
readonly exitCode: number;
|
|
50
|
+
readonly stdout: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface IsFreeModelInput {
|
|
54
|
+
readonly binaryPath: string;
|
|
55
|
+
readonly provider: string;
|
|
56
|
+
readonly model: string;
|
|
57
|
+
// Injectable for offline tests (repo doctrine: `bun test` is fully offline
|
|
58
|
+
// with fake spawn). Production defaults to Bun.spawn. Never throws — any
|
|
59
|
+
// failure returns false through the predicate below.
|
|
60
|
+
readonly run?: (
|
|
61
|
+
argv: readonly string[],
|
|
62
|
+
) => Promise<FreeModelDiscoveryRunResult>;
|
|
63
|
+
// Bounds the live spawn below. `models --verbose --refresh` hits the
|
|
64
|
+
// network, so without this a hung binary hangs admission forever — and the
|
|
65
|
+
// memoised probe caches the PENDING promise, so one hang poisons every later
|
|
66
|
+
// caller sharing the instance. With it the memoised promise always settles
|
|
67
|
+
// (true/false, never pending forever). Applies to the default spawn only;
|
|
68
|
+
// an injected `run` is the test's own clock. Fail-closed on timeout.
|
|
69
|
+
readonly timeoutMs?: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const DEFAULT_PROBE_TIMEOUT_MS = 30_000;
|
|
73
|
+
|
|
74
|
+
// Exported for the timeout test below only: production goes through
|
|
75
|
+
// isFreeModel (a test IS a real consumer, same precedent as cli.ts main).
|
|
76
|
+
export async function defaultRun(
|
|
77
|
+
argv: readonly string[],
|
|
78
|
+
timeoutMs: number = DEFAULT_PROBE_TIMEOUT_MS,
|
|
79
|
+
): Promise<FreeModelDiscoveryRunResult> {
|
|
80
|
+
const proc = Bun.spawn([...argv], {
|
|
81
|
+
stdout: "pipe",
|
|
82
|
+
stderr: "ignore",
|
|
83
|
+
stdin: "ignore",
|
|
84
|
+
}) as unknown as {
|
|
85
|
+
readonly stdout: unknown;
|
|
86
|
+
readonly exited: Promise<number>;
|
|
87
|
+
kill(): void;
|
|
88
|
+
};
|
|
89
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
90
|
+
try {
|
|
91
|
+
const run = (async () => {
|
|
92
|
+
const stdout = await new Response(proc.stdout as never).text();
|
|
93
|
+
const exitCode = await proc.exited;
|
|
94
|
+
return { exitCode, stdout };
|
|
95
|
+
})();
|
|
96
|
+
const timeout = new Promise<never>((_, reject) => {
|
|
97
|
+
timer = setTimeout(
|
|
98
|
+
() => reject(new Error("free-model probe timed out")),
|
|
99
|
+
timeoutMs,
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
return await Promise.race([run, timeout]);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
// Kill the hung child so no `models --refresh` outlives the admission
|
|
105
|
+
// that gave up on it; the predicate below turns this into fail-closed
|
|
106
|
+
// false either way.
|
|
107
|
+
try {
|
|
108
|
+
proc.kill();
|
|
109
|
+
} catch {
|
|
110
|
+
// Best-effort: the throw below is what the caller observes.
|
|
111
|
+
}
|
|
112
|
+
throw error;
|
|
113
|
+
} finally {
|
|
114
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function isHeaderLine(line: string): boolean {
|
|
119
|
+
if (line.length === 0) return false;
|
|
120
|
+
const first = line[0];
|
|
121
|
+
if (first === " " || first === "\t") return false;
|
|
122
|
+
const trimmed = line.trim();
|
|
123
|
+
if (trimmed.length === 0) return false;
|
|
124
|
+
if (
|
|
125
|
+
trimmed.startsWith("{") ||
|
|
126
|
+
trimmed.startsWith("}") ||
|
|
127
|
+
trimmed.startsWith('"') ||
|
|
128
|
+
trimmed.startsWith("[") ||
|
|
129
|
+
trimmed.startsWith("]")
|
|
130
|
+
) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return trimmed.includes("/");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The header is `<provider>/<id>`: split on the FIRST "/" so a model id that
|
|
137
|
+
// itself contains "/" still matches exactly (remainder may contain "/").
|
|
138
|
+
function headerMatches(
|
|
139
|
+
header: string,
|
|
140
|
+
provider: string,
|
|
141
|
+
model: string,
|
|
142
|
+
): boolean {
|
|
143
|
+
const slash = header.indexOf("/");
|
|
144
|
+
if (slash <= 0 || slash === header.length - 1) return false;
|
|
145
|
+
return (
|
|
146
|
+
header.slice(0, slash) === provider && header.slice(slash + 1) === model
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function parseCostBlock(
|
|
151
|
+
stdout: string,
|
|
152
|
+
provider: string,
|
|
153
|
+
model: string,
|
|
154
|
+
): unknown {
|
|
155
|
+
const lines = stdout.split("\n");
|
|
156
|
+
let headerIndex = -1;
|
|
157
|
+
for (let i = 0; i < lines.length; i++) {
|
|
158
|
+
const line = lines[i] as string;
|
|
159
|
+
if (!isHeaderLine(line)) continue;
|
|
160
|
+
if (headerMatches(line.trim(), provider, model)) {
|
|
161
|
+
headerIndex = i;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (headerIndex === -1) return undefined;
|
|
166
|
+
const blockLines: string[] = [];
|
|
167
|
+
for (let i = headerIndex + 1; i < lines.length; i++) {
|
|
168
|
+
const line = lines[i] as string;
|
|
169
|
+
if (isHeaderLine(line)) break;
|
|
170
|
+
blockLines.push(line);
|
|
171
|
+
}
|
|
172
|
+
const block = blockLines.join("\n").trim();
|
|
173
|
+
if (block.length === 0) return undefined;
|
|
174
|
+
try {
|
|
175
|
+
return JSON.parse(block);
|
|
176
|
+
} catch {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Strict predicate: exact `provider/model` header, JSON parses,
|
|
182
|
+
// status === "active", all four cost leaves strictly === 0. ANY failure,
|
|
183
|
+
// ambiguity, missing field, non-zero leaf, or spawn error returns FALSE
|
|
184
|
+
// (fail closed, never throws).
|
|
185
|
+
export async function isFreeModel(input: IsFreeModelInput): Promise<boolean> {
|
|
186
|
+
try {
|
|
187
|
+
if (
|
|
188
|
+
input.binaryPath.length === 0 ||
|
|
189
|
+
!input.binaryPath.startsWith("/") ||
|
|
190
|
+
input.provider.length === 0 ||
|
|
191
|
+
input.model.length === 0
|
|
192
|
+
) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
const run = input.run ?? ((argv) => defaultRun(argv, input.timeoutMs));
|
|
196
|
+
let result: FreeModelDiscoveryRunResult;
|
|
197
|
+
try {
|
|
198
|
+
result = await run([
|
|
199
|
+
input.binaryPath,
|
|
200
|
+
"models",
|
|
201
|
+
input.provider,
|
|
202
|
+
"--verbose",
|
|
203
|
+
"--refresh",
|
|
204
|
+
]);
|
|
205
|
+
} catch {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
if (result.exitCode !== 0) return false;
|
|
209
|
+
const parsed = parseCostBlock(result.stdout, input.provider, input.model);
|
|
210
|
+
if (typeof parsed !== "object" || parsed === null) return false;
|
|
211
|
+
const record = parsed as Record<string, unknown>;
|
|
212
|
+
if (record.status !== "active") return false;
|
|
213
|
+
const cost = record.cost;
|
|
214
|
+
if (typeof cost !== "object" || cost === null) return false;
|
|
215
|
+
const costRecord = cost as Record<string, unknown>;
|
|
216
|
+
if (costRecord.input !== 0 || costRecord.output !== 0) return false;
|
|
217
|
+
const cache = costRecord.cache;
|
|
218
|
+
if (typeof cache !== "object" || cache === null) return false;
|
|
219
|
+
const cacheRecord = cache as Record<string, unknown>;
|
|
220
|
+
if (cacheRecord.read !== 0 || cacheRecord.write !== 0) return false;
|
|
221
|
+
return true;
|
|
222
|
+
} catch {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Collision-free memo key for (provider, model) verdicts. RouteMapping
|
|
228
|
+
// provider/modelSnapshot are unconstrained strings and model ids may contain
|
|
229
|
+
// "/" (proven by our own header test) — so `${provider}/${model}` collides:
|
|
230
|
+
// ("a","b/c") and ("a/b","c") share one key and one verdict. JSON.stringify
|
|
231
|
+
// of the pair cannot collide: distinct pairs serialize distinctly.
|
|
232
|
+
export function freeVerdictKey(provider: string, model: string): string {
|
|
233
|
+
return JSON.stringify([provider, model]);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// One probe instance per admission: memoised per (provider, model) so the
|
|
237
|
+
// plan-level server credential and the per-binding upgrades share ONE verdict
|
|
238
|
+
// (the #149 anti-drift rule — two spawns could disagree across a provider
|
|
239
|
+
// flip, and the server would then hold a different truth than the binding).
|
|
240
|
+
export function createFreeModelProbe(options: {
|
|
241
|
+
readonly binaryPath: string;
|
|
242
|
+
readonly run?: IsFreeModelInput["run"];
|
|
243
|
+
readonly timeoutMs?: number;
|
|
244
|
+
}): FreeModelProbe {
|
|
245
|
+
const cache = new Map<string, Promise<boolean>>();
|
|
246
|
+
return (provider: string, model: string) => {
|
|
247
|
+
const key = freeVerdictKey(provider, model);
|
|
248
|
+
const cached = cache.get(key);
|
|
249
|
+
if (cached !== undefined) return cached;
|
|
250
|
+
const verdict = isFreeModel({
|
|
251
|
+
binaryPath: options.binaryPath,
|
|
252
|
+
provider,
|
|
253
|
+
model,
|
|
254
|
+
...(options.run === undefined ? {} : { run: options.run }),
|
|
255
|
+
...(options.timeoutMs === undefined
|
|
256
|
+
? {}
|
|
257
|
+
: { timeoutMs: options.timeoutMs }),
|
|
258
|
+
});
|
|
259
|
+
cache.set(key, verdict);
|
|
260
|
+
return verdict;
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// The plan line's billing half: how many steps of an ADMITTED route plan the
|
|
265
|
+
// free verdict covers. Reads only the verdicts admission already decided, so
|
|
266
|
+
// it never spawns — `probe` is the memoised instance admission returned, and a
|
|
267
|
+
// pair admission never probed would spawn here, which is why the gate below
|
|
268
|
+
// is the same one admission applies. Only OpenCode api-token routes can be
|
|
269
|
+
// free: an OAuth provider (OPENCODE_OAUTH_PROVIDER) never probes, and a
|
|
270
|
+
// claude-code step has no free kind at all. A missing probe, or a throwing
|
|
271
|
+
// one, counts as not free: an unknown price is never presented as free.
|
|
272
|
+
export interface FreeRouteBilling {
|
|
273
|
+
readonly free: number;
|
|
274
|
+
readonly total: number;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export async function countFreeRouteSteps(input: {
|
|
278
|
+
readonly steps: readonly {
|
|
279
|
+
readonly route: {
|
|
280
|
+
readonly backend: string;
|
|
281
|
+
readonly provider: string;
|
|
282
|
+
readonly modelSnapshot: string;
|
|
283
|
+
};
|
|
284
|
+
}[];
|
|
285
|
+
readonly probe: FreeModelProbe | undefined;
|
|
286
|
+
}): Promise<FreeRouteBilling> {
|
|
287
|
+
const total = input.steps.length;
|
|
288
|
+
if (input.probe === undefined) return { free: 0, total };
|
|
289
|
+
const probe = input.probe;
|
|
290
|
+
const verdicts = await Promise.all(
|
|
291
|
+
input.steps.map(async ({ route }) => {
|
|
292
|
+
if (route.backend !== "opencode") return false;
|
|
293
|
+
if (route.provider === OPENCODE_OAUTH_PROVIDER) return false;
|
|
294
|
+
try {
|
|
295
|
+
return await probe(route.provider, route.modelSnapshot);
|
|
296
|
+
} catch {
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
return { free: verdicts.filter((free) => free).length, total };
|
|
302
|
+
}
|