crysk 0.0.0-stage → 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 +610 -2
- 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 +82 -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,596 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import type {
|
|
5
|
+
ClaudeBinaryResolutionDeps,
|
|
6
|
+
ExecutableAllowlistEntry,
|
|
7
|
+
} from "#model/provider-capabilities";
|
|
8
|
+
import {
|
|
9
|
+
resolveClaudeCanonicalBinary,
|
|
10
|
+
verifyExecutableBinding,
|
|
11
|
+
} from "#model/provider-capabilities";
|
|
12
|
+
import type { ResolvedRoutePlan } from "#model/routing";
|
|
13
|
+
import type { CredentialKind, RunnerBackend } from "./execution/contracts";
|
|
14
|
+
// #161: the SAME predicate the Claude CLI transport files usage on
|
|
15
|
+
// (`claudeCliCostBasis` -> `envBillsMetered`) is the one `credentialKindForRoute`
|
|
16
|
+
// reads below, so admission and usage filing cannot answer "does this
|
|
17
|
+
// environment carry a per-token credential?" differently for one attempt. See
|
|
18
|
+
// the ADMISSION WIRING block on `envBillsMetered` itself for the full history.
|
|
19
|
+
import { envBillsMetered } from "./execution/usage-normalized";
|
|
20
|
+
import {
|
|
21
|
+
type CredentialBroker,
|
|
22
|
+
KeychainCredentialBroker,
|
|
23
|
+
OPENCODE_OAUTH_PROVIDER,
|
|
24
|
+
OpenCodeApiTokenBroker,
|
|
25
|
+
OpenCodeAuthBroker,
|
|
26
|
+
OpenCodeFreeBroker,
|
|
27
|
+
} from "./security/credential-broker";
|
|
28
|
+
|
|
29
|
+
export interface RunnerAuthorityOptions {
|
|
30
|
+
readonly binaryPath?: string;
|
|
31
|
+
readonly openCodeBinaryPath?: string;
|
|
32
|
+
readonly workspaceRoot: string;
|
|
33
|
+
// Widened from `{ PATH?: string }` (#161): this is now also the env
|
|
34
|
+
// `credentialKindForRoute` reads to decide a claude-code route's credential
|
|
35
|
+
// kind, so it has to be able to carry ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKEN,
|
|
36
|
+
// not just PATH.
|
|
37
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
38
|
+
readonly credentialBrokers?: Partial<Record<RunnerBackend, CredentialBroker>>;
|
|
39
|
+
/** Independent executable authority — never derived from the candidate binary. */
|
|
40
|
+
readonly executableAllowlists?: Partial<
|
|
41
|
+
Record<RunnerBackend, readonly ExecutableAllowlistEntry[]>
|
|
42
|
+
>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ResolvedRunnerOptions {
|
|
46
|
+
readonly binaryPath: string;
|
|
47
|
+
readonly workspaceRoot: string;
|
|
48
|
+
readonly executableAllowlist: readonly ExecutableAllowlistEntry[];
|
|
49
|
+
readonly credentialBroker?: CredentialBroker;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type RunnerAuthorityResolution =
|
|
53
|
+
| {
|
|
54
|
+
readonly runnerOptions: ResolvedRunnerOptions;
|
|
55
|
+
readonly error?: undefined;
|
|
56
|
+
}
|
|
57
|
+
| { readonly error: string; readonly runnerOptions?: undefined };
|
|
58
|
+
|
|
59
|
+
export interface ResolveRunnerAuthorityDeps
|
|
60
|
+
extends ClaudeBinaryResolutionDeps {}
|
|
61
|
+
|
|
62
|
+
export interface ResolvedBindingAuthority {
|
|
63
|
+
readonly backend: RunnerBackend;
|
|
64
|
+
readonly binaryPath: string;
|
|
65
|
+
readonly workspaceRoot: string;
|
|
66
|
+
readonly canonicalCwd: string;
|
|
67
|
+
readonly executableAllowlist: readonly ExecutableAllowlistEntry[];
|
|
68
|
+
// #133: the full CredentialKind. This field used to re-declare a NARROWER
|
|
69
|
+
// union of its own, which was the single compile-time reason a metered
|
|
70
|
+
// provider route could not be bound — everything downstream (the harness,
|
|
71
|
+
// the projection, the capability report) already spoke the full type.
|
|
72
|
+
readonly credentialKind: CredentialKind;
|
|
73
|
+
readonly credentialRef: string;
|
|
74
|
+
readonly credentialBroker?: CredentialBroker;
|
|
75
|
+
readonly bucketId: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type BindingAuthorityResolution =
|
|
79
|
+
| { readonly binding: ResolvedBindingAuthority; readonly error?: undefined }
|
|
80
|
+
| { readonly error: string; readonly binding?: undefined };
|
|
81
|
+
|
|
82
|
+
const OPENCODE_BINARY_NAME = "opencode";
|
|
83
|
+
|
|
84
|
+
async function resolveOpenCodeCanonicalBinary(
|
|
85
|
+
lookup: {
|
|
86
|
+
readonly binaryPath?: string;
|
|
87
|
+
readonly env?: { PATH?: string };
|
|
88
|
+
},
|
|
89
|
+
deps: ClaudeBinaryResolutionDeps = {},
|
|
90
|
+
): Promise<
|
|
91
|
+
| { readonly canonicalPath: string; readonly sha256: string }
|
|
92
|
+
| { readonly error: string }
|
|
93
|
+
> {
|
|
94
|
+
const exists = deps.existsFn ?? existsSync;
|
|
95
|
+
const toRealpath = deps.realpathFn ?? realpath;
|
|
96
|
+
const readBytes =
|
|
97
|
+
deps.readFileFn ?? ((p: string) => readFile(p) as Promise<Uint8Array>);
|
|
98
|
+
|
|
99
|
+
let candidate: string;
|
|
100
|
+
if (lookup.binaryPath !== undefined) {
|
|
101
|
+
if (!path.isAbsolute(lookup.binaryPath)) {
|
|
102
|
+
return {
|
|
103
|
+
error: `binary override must be an absolute path: ${lookup.binaryPath}`,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
candidate = lookup.binaryPath;
|
|
107
|
+
} else {
|
|
108
|
+
const searchDirs = (lookup.env?.PATH ?? process.env.PATH ?? "")
|
|
109
|
+
.split(path.delimiter)
|
|
110
|
+
.filter((dir) => dir.length > 0);
|
|
111
|
+
candidate = "";
|
|
112
|
+
for (const dir of searchDirs) {
|
|
113
|
+
const probe = path.join(dir, OPENCODE_BINARY_NAME);
|
|
114
|
+
if (exists(probe)) {
|
|
115
|
+
candidate = probe;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (candidate === "") {
|
|
120
|
+
return { error: "opencode binary not found on PATH" };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
const canonical = await toRealpath(candidate);
|
|
126
|
+
const bytes = await readBytes(canonical);
|
|
127
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
128
|
+
hasher.update(bytes);
|
|
129
|
+
return { canonicalPath: canonical, sha256: hasher.digest("hex") };
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return {
|
|
132
|
+
error: `failed to resolve opencode binary ${candidate}: ${(error as Error).message}`,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function claudeCredentialBroker(): KeychainCredentialBroker | undefined {
|
|
138
|
+
return process.platform === "darwin" && existsSync("/usr/bin/security")
|
|
139
|
+
? new KeychainCredentialBroker()
|
|
140
|
+
: undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function verifyConfiguredExecutable(
|
|
144
|
+
backend: RunnerBackend,
|
|
145
|
+
candidatePath: string,
|
|
146
|
+
options: RunnerAuthorityOptions,
|
|
147
|
+
deps: ResolveRunnerAuthorityDeps = {},
|
|
148
|
+
): Promise<
|
|
149
|
+
| { readonly allowlist: readonly ExecutableAllowlistEntry[] }
|
|
150
|
+
| { readonly error: string }
|
|
151
|
+
> {
|
|
152
|
+
const configuredAllowlist = options.executableAllowlists?.[backend];
|
|
153
|
+
if (configuredAllowlist === undefined || configuredAllowlist.length === 0) {
|
|
154
|
+
return {
|
|
155
|
+
error: `executable allowlist required for ${backend} binding authority`,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// #303: verify-only. Binding resolution needs the allowlist and hash
|
|
160
|
+
// verdict, never a runnable copy; the full authority snapshotted the binary
|
|
161
|
+
// here and dropped the result, leaking one copy per route per run. The
|
|
162
|
+
// harness still verifies (and snapshots) again right before each spawn.
|
|
163
|
+
const verification = await verifyExecutableBinding(
|
|
164
|
+
{
|
|
165
|
+
candidatePath,
|
|
166
|
+
allowlist: configuredAllowlist,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
// Binding resolution already canonicalized the candidate path.
|
|
170
|
+
realpathFn: async (p) => p,
|
|
171
|
+
readFileFn: deps.readFileFn,
|
|
172
|
+
statFn: deps.statFn,
|
|
173
|
+
},
|
|
174
|
+
);
|
|
175
|
+
if (!verification.approved) {
|
|
176
|
+
return {
|
|
177
|
+
error:
|
|
178
|
+
verification.reason ??
|
|
179
|
+
`executable ${candidatePath} is not in configured allowlist for ${backend}`,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return { allowlist: configuredAllowlist };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export async function withOpenCodeDiscoveryAllowlist(
|
|
187
|
+
options: RunnerAuthorityOptions,
|
|
188
|
+
deps: ResolveRunnerAuthorityDeps = {},
|
|
189
|
+
): Promise<RunnerAuthorityOptions | { readonly error: string }> {
|
|
190
|
+
const configured = options.executableAllowlists?.opencode;
|
|
191
|
+
if (configured !== undefined && configured.length > 0) {
|
|
192
|
+
return options;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const resolved = await resolveOpenCodeCanonicalBinary(
|
|
196
|
+
{
|
|
197
|
+
binaryPath: options.openCodeBinaryPath ?? options.binaryPath,
|
|
198
|
+
env: options.env,
|
|
199
|
+
},
|
|
200
|
+
deps,
|
|
201
|
+
);
|
|
202
|
+
if ("error" in resolved) {
|
|
203
|
+
return { error: resolved.error };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
...options,
|
|
208
|
+
openCodeBinaryPath: resolved.canonicalPath,
|
|
209
|
+
executableAllowlists: {
|
|
210
|
+
...options.executableAllowlists,
|
|
211
|
+
opencode: [
|
|
212
|
+
{ absolutePath: resolved.canonicalPath, sha256: resolved.sha256 },
|
|
213
|
+
],
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function planUsesBackend(
|
|
219
|
+
plan: ResolvedRoutePlan,
|
|
220
|
+
backend: RunnerBackend,
|
|
221
|
+
): boolean {
|
|
222
|
+
const seen = new Set<RunnerBackend>();
|
|
223
|
+
for (const step of plan.steps) {
|
|
224
|
+
if (!seen.has(step.route.backend)) {
|
|
225
|
+
seen.add(step.route.backend);
|
|
226
|
+
if (step.route.backend === backend) {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Resolves independent executable allowlists for every backend the frozen
|
|
235
|
+
// plan would execute. Claude discovery stays the default; OpenCode is added
|
|
236
|
+
// only when the plan names it — never as a fallback.
|
|
237
|
+
export async function prepareProductionRunnerAuthority(
|
|
238
|
+
workspaceRoot: string,
|
|
239
|
+
plan: ResolvedRoutePlan,
|
|
240
|
+
deps: ResolveRunnerAuthorityDeps = {},
|
|
241
|
+
seed: Partial<RunnerAuthorityOptions> = {},
|
|
242
|
+
): Promise<RunnerAuthorityOptions | { readonly error: string }> {
|
|
243
|
+
let options: RunnerAuthorityOptions = { workspaceRoot, ...seed };
|
|
244
|
+
if (planUsesBackend(plan, "claude-code")) {
|
|
245
|
+
const withClaude = await withClaudeDiscoveryAllowlist(options, deps);
|
|
246
|
+
if ("error" in withClaude) {
|
|
247
|
+
return withClaude;
|
|
248
|
+
}
|
|
249
|
+
options = withClaude;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (!planUsesBackend(plan, "opencode")) {
|
|
253
|
+
return options;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const withOpenCode = await withOpenCodeDiscoveryAllowlist(options, deps);
|
|
257
|
+
if ("error" in withOpenCode) {
|
|
258
|
+
return withOpenCode;
|
|
259
|
+
}
|
|
260
|
+
return withOpenCode;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export async function withClaudeDiscoveryAllowlist(
|
|
264
|
+
options: RunnerAuthorityOptions,
|
|
265
|
+
deps: ResolveRunnerAuthorityDeps = {},
|
|
266
|
+
): Promise<RunnerAuthorityOptions | { readonly error: string }> {
|
|
267
|
+
const configured = options.executableAllowlists?.["claude-code"];
|
|
268
|
+
if (configured !== undefined && configured.length > 0) {
|
|
269
|
+
return options;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const resolved = await resolveClaudeCanonicalBinary(options, deps);
|
|
273
|
+
if (resolved.error !== undefined) {
|
|
274
|
+
return { error: resolved.error };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
...options,
|
|
279
|
+
executableAllowlists: {
|
|
280
|
+
...options.executableAllowlists,
|
|
281
|
+
"claude-code": [
|
|
282
|
+
{ absolutePath: resolved.canonicalPath, sha256: resolved.sha256 },
|
|
283
|
+
],
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// #133. WHICH credential a route runs on, decided from the backend AND the
|
|
289
|
+
// provider. Backend alone is not enough: OpenCode serves exactly one provider
|
|
290
|
+
// from a subscription (OPENCODE_OAUTH_PROVIDER) and everything else from a
|
|
291
|
+
// metered API token, and those are different credentials with different
|
|
292
|
+
// billing, different projection payloads and different rate-limit buckets.
|
|
293
|
+
//
|
|
294
|
+
// This is deliberately PROVIDER-KEYED and deliberately does NOT read the
|
|
295
|
+
// credential store. The bounded, known limit that buys: an `openai` entry
|
|
296
|
+
// holding an API key rather than an OAuth record still resolves to
|
|
297
|
+
// `opencode_chatgpt_oauth` here, and is then refused by the OAuth broker's
|
|
298
|
+
// existing `type !== "oauth"` lock (credential-broker.ts). That is a loud
|
|
299
|
+
// failure with a one-line fix, not a silent metered run. Reading the store
|
|
300
|
+
// here to disambiguate would put a credential read inside a pure routing
|
|
301
|
+
// decision, and would make the answer depend on machine state the frozen
|
|
302
|
+
// plan cannot record.
|
|
303
|
+
//
|
|
304
|
+
// WHY the default is `provider_api_token` rather than the subscription kind:
|
|
305
|
+
// metered is the FAIL-CLOSED direction. A metered route with no pricing is
|
|
306
|
+
// refused at the exact-binding gate (`pricing_table_missing`, blocking), so
|
|
307
|
+
// the cost of guessing metered is a refusal. A route wrongly marked
|
|
308
|
+
// subscription EXECUTES and reports `cashCostUsd: 0` over real spend — the
|
|
309
|
+
// cost of guessing subscription is an unmetered bill nobody sees.
|
|
310
|
+
//
|
|
311
|
+
// #161 (corrected 2026-09-24 by Juanma — the first cut of this inverted
|
|
312
|
+
// precedence and is wrong; read this in full before touching the
|
|
313
|
+
// claude-code branch again). The kind must answer "what will the CHILD
|
|
314
|
+
// actually run on?", not "does env carry a key?" in isolation — those two
|
|
315
|
+
// questions only agree when NOTHING will project a credential.
|
|
316
|
+
//
|
|
317
|
+
// When a broker IS attached and its projection SUCCEEDS, the harness strips
|
|
318
|
+
// ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and CLAUDE_CODE_OAUTH_TOKEN from
|
|
319
|
+
// the child's env, whatever the ambient environment carries
|
|
320
|
+
// (`PROJECTION_OWNED_KEYS`, execution/harness.ts — "a projection owns the
|
|
321
|
+
// CREDENTIAL IDENTITY, not just the home"). So a macOS developer with an
|
|
322
|
+
// exported `ANTHROPIC_API_KEY`, running through the darwin Keychain default,
|
|
323
|
+
// spawns a child that never sees that key — it runs on the projected
|
|
324
|
+
// subscription OAuth record. Calling that route metered (the first cut's
|
|
325
|
+
// mistake) would silently rebill a subscription developer as per-token for a
|
|
326
|
+
// key the child structurally cannot spend.
|
|
327
|
+
//
|
|
328
|
+
// KNOWN EXCEPTION — #279 (fixed 2026-09-24 by Juanma, option 2: fence at the
|
|
329
|
+
// harness, admission stays unchanged). `KeychainCredentialBroker.project` can
|
|
330
|
+
// throw `missing_subscription_record` when the CLI moved its OAuth record
|
|
331
|
+
// out of the Keychain item; the harness DEGRADES that one failure class
|
|
332
|
+
// instead of killing the step (harness.ts ~756-778) — no projection runs,
|
|
333
|
+
// `buildChildEnv` returns the env UNSTRIPPED, and an ambient key reaches the
|
|
334
|
+
// child after all. On that path THIS FUNCTION still says subscription (a
|
|
335
|
+
// broker WAS attached) — that answer is deliberately left unchanged, because
|
|
336
|
+
// admission cannot know at bind time whether a projection will degrade — but
|
|
337
|
+
// the spend is no longer unaccounted for: `StepExecutionHarness.run`
|
|
338
|
+
// (harness.ts) now recognizes its OWN degraded projection, re-applies this
|
|
339
|
+
// SAME `envBillsMetered` predicate to the SAME unstripped child env usage
|
|
340
|
+
// filing reads, and reserves+fences that one attempt against the spend
|
|
341
|
+
// ledger as if the route had bound metered — without changing the kind this
|
|
342
|
+
// function returns, which credential runs, or who pays. Pre-existing: before
|
|
343
|
+
// #161 every claude-code route said subscription regardless, so this exact
|
|
344
|
+
// admission/filing mismatch already existed on every degraded projection;
|
|
345
|
+
// #161 did not create it, and this function still does not resolve it — the
|
|
346
|
+
// harness does, downstream of here.
|
|
347
|
+
//
|
|
348
|
+
// `hasBroker` carries "will a broker ATTEMPT to project" — a plain boolean
|
|
349
|
+
// the CALLER already resolved (`resolveBindingAuthority` below constructs
|
|
350
|
+
// the broker first, unconditionally, exactly as before #161 existed). This
|
|
351
|
+
// function stays pure: no platform check, no fs/network probe of whether
|
|
352
|
+
// that attempt will actually SUCCEED — #279 is precisely the runtime state
|
|
353
|
+
// this function cannot see and does not try to.
|
|
354
|
+
//
|
|
355
|
+
// `env` is the raw, PRE-strip environment (`options.env ?? process.env` at
|
|
356
|
+
// the call site), not the post-strip child env. On a SUCCESSFUL projection,
|
|
357
|
+
// `!hasBroker && envBillsMetered(env)` equals `envBillsMetered(postStripEnv)`
|
|
358
|
+
// — matching usage filing's own `envBillsMetered(request.isolation.env)`
|
|
359
|
+
// (claude-code-cli.ts) by construction. #279 is the one path where that
|
|
360
|
+
// equality breaks.
|
|
361
|
+
//
|
|
362
|
+
// Both arguments default to the "nothing is known" case (`env: {}`,
|
|
363
|
+
// `hasBroker: false`) so a caller that omits them — every existing call site
|
|
364
|
+
// that never threaded either through, and every test that does not care
|
|
365
|
+
// about this dimension — keeps today's answer: subscription.
|
|
366
|
+
export function credentialKindForRoute(
|
|
367
|
+
backend: RunnerBackend,
|
|
368
|
+
provider: string,
|
|
369
|
+
env: Readonly<Record<string, string | undefined>> = {},
|
|
370
|
+
hasBroker = false,
|
|
371
|
+
): CredentialKind {
|
|
372
|
+
if (backend === "claude-code") {
|
|
373
|
+
return !hasBroker && envBillsMetered(env)
|
|
374
|
+
? "provider_api_token"
|
|
375
|
+
: "claude_subscription_oauth";
|
|
376
|
+
}
|
|
377
|
+
if (backend === "opencode") {
|
|
378
|
+
return provider === OPENCODE_OAUTH_PROVIDER
|
|
379
|
+
? "opencode_chatgpt_oauth"
|
|
380
|
+
: "provider_api_token";
|
|
381
|
+
}
|
|
382
|
+
// Exhaustive on purpose: a backend with no credential authority must not
|
|
383
|
+
// fall into the metered default by accident. It has no binding at all —
|
|
384
|
+
// resolveBindingAuthority refuses it a few lines below — and inventing a
|
|
385
|
+
// kind for it here would make that refusal look like a pricing problem.
|
|
386
|
+
throw new Error(`No credential authority is bound for backend "${backend}"`);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// 2026-09-02. THE one place that says "a provider API token is a metered
|
|
390
|
+
// credential". Two callers need it and they must not each carry their own
|
|
391
|
+
// copy: `FrozenRuntimeBinding.capabilities()` derives the exact binding's
|
|
392
|
+
// effective billing mode from it (#133), and the OpenCode transport factory
|
|
393
|
+
// derives the billing mode STAMPED ON EVERY USAGE RECORD from it
|
|
394
|
+
// (transport-registry.ts). Two copies would be two chances for the capability
|
|
395
|
+
// report and the usage records to disagree about how one attempt bills — and
|
|
396
|
+
// the metered-zero rule in `settlementFromUsage` reads the usage record, so
|
|
397
|
+
// the disagreement would be silently exploitable rather than merely untidy.
|
|
398
|
+
//
|
|
399
|
+
// A boolean, not a mode: the binding's rule is UPGRADE-ONLY (an OAuth or
|
|
400
|
+
// subscription kind keeps whatever the transport reported, `unknown`
|
|
401
|
+
// included), and returning a mode here would invite a caller to overwrite
|
|
402
|
+
// that instead of asking the question this answers.
|
|
403
|
+
export function credentialKindBillsMetered(kind: CredentialKind): boolean {
|
|
404
|
+
return kind === "provider_api_token";
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// The DEFAULT broker for an opencode route, derived from the same provider
|
|
408
|
+
// that decided the kind. Exported because production-runtime.ts resolves the
|
|
409
|
+
// plan's one shared broker (#149 keeps it a single instance) and must reach
|
|
410
|
+
// the same answer: two copies of this pairing would be two chances to hand a
|
|
411
|
+
// route a broker that refuses its kind — a failure that surfaces only at
|
|
412
|
+
// projection time, inside a live run.
|
|
413
|
+
//
|
|
414
|
+
// #182: kind-based selection — the single derivation (the #149 anti-drift
|
|
415
|
+
// rule). Callers that already resolved the kind pass it here and never derive
|
|
416
|
+
// it twice; `openCodeCredentialBroker` below is the thin wrapper for callers
|
|
417
|
+
// holding only a provider.
|
|
418
|
+
export function openCodeCredentialBrokerForKind(
|
|
419
|
+
kind: CredentialKind,
|
|
420
|
+
provider: string,
|
|
421
|
+
): CredentialBroker {
|
|
422
|
+
if (kind === "provider_free") {
|
|
423
|
+
// #280: bound to the provider, same as the metered branch below — the
|
|
424
|
+
// free broker now peeks at this provider's OWN api record before
|
|
425
|
+
// falling back to the empty tree, so it needs to know which provider it
|
|
426
|
+
// is.
|
|
427
|
+
return new OpenCodeFreeBroker(provider);
|
|
428
|
+
}
|
|
429
|
+
if (kind === "provider_api_token") {
|
|
430
|
+
return new OpenCodeApiTokenBroker(provider);
|
|
431
|
+
}
|
|
432
|
+
return new OpenCodeAuthBroker();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export function openCodeCredentialBroker(provider: string): CredentialBroker {
|
|
436
|
+
return openCodeCredentialBrokerForKind(
|
|
437
|
+
credentialKindForRoute("opencode", provider),
|
|
438
|
+
provider,
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export async function resolveBindingAuthority(
|
|
443
|
+
backend: RunnerBackend,
|
|
444
|
+
// Required, not optional: an optional provider would default silently at
|
|
445
|
+
// every call site, and a silent default here picks the BILLING MODE. tsc
|
|
446
|
+
// finding all five call sites is the point — `bun test` would not.
|
|
447
|
+
provider: string,
|
|
448
|
+
options: RunnerAuthorityOptions,
|
|
449
|
+
deps: ResolveRunnerAuthorityDeps = {},
|
|
450
|
+
): Promise<BindingAuthorityResolution> {
|
|
451
|
+
if (backend === "claude-code") {
|
|
452
|
+
const resolved = await resolveClaudeCanonicalBinary(options, deps);
|
|
453
|
+
if (resolved.error !== undefined) {
|
|
454
|
+
return { error: resolved.error };
|
|
455
|
+
}
|
|
456
|
+
const verified = await verifyConfiguredExecutable(
|
|
457
|
+
backend,
|
|
458
|
+
resolved.canonicalPath,
|
|
459
|
+
options,
|
|
460
|
+
deps,
|
|
461
|
+
);
|
|
462
|
+
if ("error" in verified) {
|
|
463
|
+
return { error: verified.error };
|
|
464
|
+
}
|
|
465
|
+
// #161 (corrected 2026-09-24 by Juanma). Resolve the broker FIRST,
|
|
466
|
+
// exactly as this line read before #161 ever touched it — unconditional,
|
|
467
|
+
// never gated on a credential decision that has not been made yet. The
|
|
468
|
+
// broker's PRESENCE is the INPUT to that decision now, not the other way
|
|
469
|
+
// around: see the WHY block on `credentialKindForRoute` above for the
|
|
470
|
+
// full reasoning, including the one known exception where a broker is
|
|
471
|
+
// present but its projection degrades (#279).
|
|
472
|
+
const broker =
|
|
473
|
+
options.credentialBrokers?.["claude-code"] ?? claudeCredentialBroker();
|
|
474
|
+
// Falls back to `process.env` exactly like the opencode PATH lookup a few
|
|
475
|
+
// lines below (`resolveOpenCodeCanonicalBinary`'s
|
|
476
|
+
// `lookup.env?.PATH ?? process.env.PATH`) — nothing in the real CLI
|
|
477
|
+
// threads `env` through this far, so this is the one place that has to
|
|
478
|
+
// read the operator's actual environment for the decision to fire in a
|
|
479
|
+
// real run. Tests inject `options.env` explicitly (including `{}`) to
|
|
480
|
+
// stay offline and deterministic; the default only ever applies when a
|
|
481
|
+
// caller has not opted into a specific answer.
|
|
482
|
+
const credentialKind = credentialKindForRoute(
|
|
483
|
+
backend,
|
|
484
|
+
provider,
|
|
485
|
+
options.env ?? process.env,
|
|
486
|
+
broker !== undefined,
|
|
487
|
+
);
|
|
488
|
+
const metered = credentialKindBillsMetered(credentialKind);
|
|
489
|
+
return {
|
|
490
|
+
binding: {
|
|
491
|
+
backend,
|
|
492
|
+
binaryPath: resolved.canonicalPath,
|
|
493
|
+
workspaceRoot: options.workspaceRoot,
|
|
494
|
+
canonicalCwd: options.workspaceRoot,
|
|
495
|
+
executableAllowlist: verified.allowlist,
|
|
496
|
+
credentialKind,
|
|
497
|
+
credentialRef: "claude-code-credentials",
|
|
498
|
+
...(broker ? { credentialBroker: broker } : {}),
|
|
499
|
+
// #161: a separate bucket for the metered route so its API rate
|
|
500
|
+
// limit is never pooled with the subscription's quota. This is
|
|
501
|
+
// belt-and-suspenders — the REAL rate-limit bucket downstream
|
|
502
|
+
// (`bindingBucketId`, production-runtime.ts) already derives from a
|
|
503
|
+
// fingerprint that embeds `credentialKind`, so the two kinds land in
|
|
504
|
+
// different HMAC buckets regardless of this field — but this
|
|
505
|
+
// `ResolvedBindingAuthority.bucketId` is still part of the public
|
|
506
|
+
// shape and leaving it identical for both kinds would be exactly the
|
|
507
|
+
// kind of unobservable-but-wrong invariant this repo has been burned
|
|
508
|
+
// by before.
|
|
509
|
+
bucketId: metered ? "claude-code-api" : "claude-code",
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (backend === "opencode") {
|
|
515
|
+
const resolved = await resolveOpenCodeCanonicalBinary(
|
|
516
|
+
{
|
|
517
|
+
binaryPath: options.openCodeBinaryPath ?? options.binaryPath,
|
|
518
|
+
env: options.env,
|
|
519
|
+
},
|
|
520
|
+
deps,
|
|
521
|
+
);
|
|
522
|
+
if ("error" in resolved) {
|
|
523
|
+
return { error: resolved.error };
|
|
524
|
+
}
|
|
525
|
+
const verified = await verifyConfiguredExecutable(
|
|
526
|
+
backend,
|
|
527
|
+
resolved.canonicalPath,
|
|
528
|
+
options,
|
|
529
|
+
deps,
|
|
530
|
+
);
|
|
531
|
+
if ("error" in verified) {
|
|
532
|
+
return { error: verified.error };
|
|
533
|
+
}
|
|
534
|
+
const credentialKind = credentialKindForRoute(backend, provider);
|
|
535
|
+
return {
|
|
536
|
+
binding: {
|
|
537
|
+
backend,
|
|
538
|
+
binaryPath: resolved.canonicalPath,
|
|
539
|
+
workspaceRoot: options.workspaceRoot,
|
|
540
|
+
canonicalCwd: options.workspaceRoot,
|
|
541
|
+
executableAllowlist: verified.allowlist,
|
|
542
|
+
credentialKind,
|
|
543
|
+
// #133: per-PROVIDER, because two providers on this one backend are
|
|
544
|
+
// two different credentials. `credentialRef` is half of
|
|
545
|
+
// `credentialFingerprint` (production-runtime.ts), which is what
|
|
546
|
+
// separates rate-limit buckets — a shared ref would pool an OAuth
|
|
547
|
+
// subscription's quota and a metered token's quota into one bucket
|
|
548
|
+
// and make both wrong.
|
|
549
|
+
credentialRef: `opencode-auth:${provider}`,
|
|
550
|
+
credentialBroker:
|
|
551
|
+
options.credentialBrokers?.opencode ??
|
|
552
|
+
// #182: the kind above, not a second derivation — deriving again
|
|
553
|
+
// here is how the broker and the kind drift apart unobserved.
|
|
554
|
+
openCodeCredentialBrokerForKind(credentialKind, provider),
|
|
555
|
+
bucketId: "opencode",
|
|
556
|
+
},
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return {
|
|
561
|
+
error: `unsupported production backend "${backend}" in PR1`,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
export async function resolveRunnerAuthority(
|
|
566
|
+
options: RunnerAuthorityOptions,
|
|
567
|
+
deps: ResolveRunnerAuthorityDeps = {},
|
|
568
|
+
): Promise<RunnerAuthorityResolution> {
|
|
569
|
+
const resolvedOptions = await withClaudeDiscoveryAllowlist(options, deps);
|
|
570
|
+
if ("error" in resolvedOptions) {
|
|
571
|
+
return { error: resolvedOptions.error };
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const result = await resolveBindingAuthority(
|
|
575
|
+
"claude-code",
|
|
576
|
+
// The CLI-compatibility path is Claude-only by construction, and
|
|
577
|
+
// credentialKindForRoute ignores the provider for that backend.
|
|
578
|
+
"anthropic",
|
|
579
|
+
resolvedOptions,
|
|
580
|
+
deps,
|
|
581
|
+
);
|
|
582
|
+
if (result.error !== undefined || result.binding === undefined) {
|
|
583
|
+
return { error: result.error ?? "claude binding unavailable" };
|
|
584
|
+
}
|
|
585
|
+
const binding = result.binding;
|
|
586
|
+
return {
|
|
587
|
+
runnerOptions: {
|
|
588
|
+
binaryPath: binding.binaryPath,
|
|
589
|
+
workspaceRoot: binding.workspaceRoot,
|
|
590
|
+
executableAllowlist: binding.executableAllowlist,
|
|
591
|
+
...(binding.credentialBroker
|
|
592
|
+
? { credentialBroker: binding.credentialBroker }
|
|
593
|
+
: {}),
|
|
594
|
+
},
|
|
595
|
+
};
|
|
596
|
+
}
|