@mnaimfaizy/agent-kit 1.0.0-beta.14 → 1.0.0-beta.16

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.
Files changed (180) hide show
  1. package/README.md +22 -4
  2. package/custom-harness.schema.json +243 -0
  3. package/harnesses.json +244 -6
  4. package/package.json +3 -2
  5. package/src/checks/ci-workflow.mjs +105 -0
  6. package/src/checks/claude-app.mjs +11 -2
  7. package/src/checks/custom-harness.mjs +157 -0
  8. package/src/checks/github-app.mjs +214 -0
  9. package/src/checks/rulesets.mjs +9 -8
  10. package/src/checks/secrets-and-variables.mjs +27 -8
  11. package/src/checks/stages.mjs +705 -0
  12. package/src/cli.mjs +583 -36
  13. package/src/deep-scan/agent.mjs +88 -21
  14. package/src/deep-scan/credential.mjs +81 -32
  15. package/src/deep-scan/docker-driver.mjs +105 -42
  16. package/src/deep-scan/harness-text.mjs +99 -0
  17. package/src/deep-scan/lifecycle.mjs +68 -178
  18. package/src/deep-scan/limits.mjs +13 -7
  19. package/src/deep-scan/model-proxy.mjs +21 -10
  20. package/src/deep-scan/preflight.mjs +19 -6
  21. package/src/deep-scan/recipe.mjs +4 -0
  22. package/src/deep-scan/runtime.mjs +54 -6
  23. package/src/deep-scan/sandbox-run.mjs +232 -0
  24. package/src/deep-scan/sbx-driver.mjs +224 -39
  25. package/src/deep-scan/tar.mjs +69 -22
  26. package/src/deep-scan/verify.mjs +6 -1
  27. package/src/deep-scan.mjs +73 -13
  28. package/src/doctor.mjs +166 -5
  29. package/src/exec.mjs +14 -5
  30. package/src/factory/budget/budget.mjs +259 -0
  31. package/src/factory/build/command.mjs +321 -0
  32. package/src/factory/build/factory.mjs +473 -0
  33. package/src/factory/build/findings.mjs +55 -0
  34. package/src/factory/build/host.mjs +230 -0
  35. package/src/factory/build/notes.mjs +191 -0
  36. package/src/factory/build/pull-request.mjs +74 -0
  37. package/src/factory/bundle.mjs +146 -0
  38. package/src/factory/checks/allowed-paths.mjs +50 -0
  39. package/src/factory/checks/plan-header.mjs +296 -0
  40. package/src/factory/checks/qa.mjs +85 -0
  41. package/src/factory/checks/review.mjs +73 -0
  42. package/src/factory/checks/security.mjs +50 -0
  43. package/src/factory/checks/stage-check.mjs +89 -0
  44. package/src/factory/claims/command.mjs +187 -0
  45. package/src/factory/claims/release.mjs +111 -0
  46. package/src/factory/decision/answers.mjs +103 -0
  47. package/src/factory/decision/command.mjs +64 -0
  48. package/src/factory/decision/plan.mjs +99 -0
  49. package/src/factory/decision/request.mjs +116 -0
  50. package/src/factory/doctor/files.mjs +329 -0
  51. package/src/factory/doctor/index.mjs +56 -0
  52. package/src/factory/doctor/local.mjs +497 -0
  53. package/src/factory/doctor/orchestrate.mjs +146 -0
  54. package/src/factory/engine/lease.mjs +146 -0
  55. package/src/factory/engine/local-engine.mjs +177 -0
  56. package/src/factory/engine/lockfile.mjs +64 -0
  57. package/src/factory/engine/workflow-engine.mjs +124 -0
  58. package/src/factory/gate/comment.mjs +88 -0
  59. package/src/factory/gate/config.mjs +57 -0
  60. package/src/factory/gate/gate-core.mjs +250 -0
  61. package/src/factory/gate/guarded.mjs +17 -0
  62. package/src/factory/gate/reads.mjs +105 -0
  63. package/src/factory/gate/task.mjs +193 -0
  64. package/src/factory/gate/trip-reads.mjs +82 -0
  65. package/src/factory/gate/trip.mjs +87 -0
  66. package/src/factory/init/blueprint.mjs +174 -0
  67. package/src/factory/init/index.mjs +337 -0
  68. package/src/factory/labels/evidence.mjs +162 -0
  69. package/src/factory/labels/moves.mjs +32 -0
  70. package/src/factory/labels/reconcile.mjs +267 -0
  71. package/src/factory/labels/revert.mjs +87 -0
  72. package/src/factory/labels/timeline.mjs +72 -0
  73. package/src/factory/labels/voided.mjs +59 -0
  74. package/src/factory/limits/harness.mjs +39 -0
  75. package/src/factory/limits/limits.mjs +123 -0
  76. package/src/factory/merge/command.mjs +172 -0
  77. package/src/factory/outbox/check.mjs +240 -0
  78. package/src/factory/outbox/rulings.mjs +195 -0
  79. package/src/factory/plan/command.mjs +66 -0
  80. package/src/factory/plan/comment.mjs +156 -0
  81. package/src/factory/plan/publish.mjs +149 -0
  82. package/src/factory/plan/risk.mjs +153 -0
  83. package/src/factory/qa/command.mjs +163 -0
  84. package/src/factory/records/engine-record.mjs +133 -0
  85. package/src/factory/records/main.mjs +96 -0
  86. package/src/factory/records/record.mjs +226 -0
  87. package/src/factory/records/sink.mjs +57 -0
  88. package/src/factory/report/build.mjs +92 -0
  89. package/src/factory/report/command.mjs +80 -0
  90. package/src/factory/report/facts.mjs +226 -0
  91. package/src/factory/report/gate-section.mjs +122 -0
  92. package/src/factory/report/metrics.mjs +111 -0
  93. package/src/factory/report/reads.mjs +246 -0
  94. package/src/factory/report/render.mjs +121 -0
  95. package/src/factory/report/rollout.mjs +154 -0
  96. package/src/factory/review/command.mjs +182 -0
  97. package/src/factory/run/awake.mjs +70 -0
  98. package/src/factory/run/collect.mjs +77 -0
  99. package/src/factory/run/command.mjs +490 -0
  100. package/src/factory/run/gh.mjs +46 -0
  101. package/src/factory/run/launch.mjs +47 -0
  102. package/src/factory/run/pipelines.mjs +261 -0
  103. package/src/factory/run/raw.mjs +96 -0
  104. package/src/factory/run/restart.mjs +83 -0
  105. package/src/factory/run/s3.mjs +193 -0
  106. package/src/factory/run/secrets.mjs +142 -0
  107. package/src/factory/run/teardown.mjs +242 -0
  108. package/src/factory/run/token.mjs +68 -0
  109. package/src/factory/security/command.mjs +157 -0
  110. package/src/factory/setup/app.mjs +122 -0
  111. package/src/factory/setup/command.mjs +409 -0
  112. package/src/factory/setup/labels.mjs +25 -0
  113. package/src/factory/setup/reads.mjs +129 -0
  114. package/src/factory/setup/writes.mjs +126 -0
  115. package/src/factory/snapshot/write.mjs +176 -0
  116. package/src/factory/stop/command.mjs +136 -0
  117. package/src/factory/sweep/actions-main.mjs +117 -0
  118. package/src/factory/sweep/command.mjs +65 -0
  119. package/src/factory/sweep/knobs.mjs +78 -0
  120. package/src/factory/sweep/marker-version.mjs +15 -0
  121. package/src/factory/sweep/markers.mjs +112 -0
  122. package/src/factory/sweep/overlap.mjs +24 -0
  123. package/src/factory/sweep/run.mjs +198 -0
  124. package/src/factory/sweep/shell.mjs +394 -0
  125. package/src/factory/sweep/split.mjs +104 -0
  126. package/src/factory/sweep/states.mjs +87 -0
  127. package/src/factory/sweep/sweep-core.mjs +1781 -0
  128. package/src/factory/tracker/github.mjs +674 -0
  129. package/src/factory/tracker/operations.mjs +153 -0
  130. package/src/factory/upgrade/index.mjs +83 -0
  131. package/src/harness/adapters.mjs +30 -0
  132. package/src/harness/antigravity.mjs +187 -0
  133. package/src/harness/claude-code.mjs +8 -1
  134. package/src/harness/codex.mjs +219 -0
  135. package/src/harness/cursor.mjs +199 -0
  136. package/src/harness/custom/registry.mjs +21 -0
  137. package/src/harness/custom/schema.mjs +90 -0
  138. package/src/harness/custom.mjs +299 -0
  139. package/src/harness/facts.mjs +44 -7
  140. package/src/harness/gemini.mjs +164 -0
  141. package/src/harness/install-layer.mjs +86 -3
  142. package/src/harness/opencode.mjs +289 -0
  143. package/src/harness/resolve.mjs +156 -0
  144. package/src/harness/support-levels.mjs +151 -3
  145. package/src/harness-live/assess.mjs +29 -6
  146. package/src/harness-live/cursor-real.mjs +146 -0
  147. package/src/harness-live/cursor-version.mjs +70 -0
  148. package/src/harness-live/cursor.mjs +80 -0
  149. package/src/harness-live/custom.mjs +91 -0
  150. package/src/harness-live/fixture-harness.mjs +45 -0
  151. package/src/harness-live/harnesses.mjs +263 -0
  152. package/src/harness-live/host.mjs +113 -4
  153. package/src/harness-live/opencode.mjs +289 -0
  154. package/src/harness-live/probes.mjs +87 -7
  155. package/src/harness-live/run.mjs +101 -20
  156. package/src/harness-live/setup.mjs +126 -0
  157. package/src/harness-live/stage.mjs +25 -15
  158. package/src/harness-live/suite.mjs +276 -95
  159. package/src/harness-test.mjs +116 -0
  160. package/src/init.mjs +374 -36
  161. package/src/kit-owned-paths.mjs +1 -1
  162. package/src/model-proxy/core.mjs +128 -20
  163. package/src/model-proxy/profiles/anthropic.mjs +54 -10
  164. package/src/model-proxy/profiles/gemini-api.mjs +311 -0
  165. package/src/model-proxy/profiles/openai.mjs +327 -0
  166. package/src/model-proxy/stub-dialects/gemini-api.mjs +178 -0
  167. package/src/model-proxy/stub-dialects/openai.mjs +173 -0
  168. package/src/model-proxy/stub.mjs +9 -1
  169. package/src/stage/audit.mjs +79 -0
  170. package/src/stage/egress-route.mjs +207 -0
  171. package/src/stage/implement/carry.mjs +308 -0
  172. package/src/stage/implement/sandbox.mjs +271 -0
  173. package/src/stage/implement/verify.mjs +70 -0
  174. package/src/stage/implement.mjs +218 -0
  175. package/src/stage/plan.mjs +115 -0
  176. package/src/stage/review-response.mjs +172 -0
  177. package/src/stage/review.mjs +185 -0
  178. package/src/stage/workspace.mjs +274 -0
  179. package/src/stage.mjs +1087 -0
  180. package/src/upgrade.mjs +39 -7
@@ -0,0 +1,289 @@
1
+ // The opencode Harness adapter (ADR 0027): one function that takes a Stage
2
+ // request and returns a launch plan, as plain data. The Launcher does
3
+ // everything with the plan; this module starts nothing and imports nothing.
4
+ //
5
+ // The CLI: opencode 2.x (`@opencode/cli`), one pinned version, in
6
+ // `/opt/agent-kit` of the kit's Harness image (ADR 0029,
7
+ // docker/harness-images/), which is pinned by digest. The updater is off in
8
+ // the system config the image holds, in the run's own and by
9
+ // `OPENCODE_DISABLE_AUTOUPDATE`. Nothing is installed at run time. opencode
10
+ // 1.x (`opencode-ai`) is another harness, which the kit does not run.
11
+ //
12
+ // The provider is the Stage's model's: opencode names a model
13
+ // `provider/model`, and each provider here has a model proxy profile
14
+ // (ADR 0023). The accepted credential is that provider's API key.
15
+ //
16
+ // The launch: `opencode run`, the brief on stdin, always `--standalone`
17
+ // (without it, 2.x attaches to a shared per-user server that read its
18
+ // environment when it started), with every permission approved (`--auto`),
19
+ // since the Launcher's sandbox reaches the Guards. opencode reads the
20
+ // Launcher's config (`OPENCODE_CONFIG`) and a global config folder of the
21
+ // Launcher's own; project config is off, and the Launcher removes it from the
22
+ // copy too. The config names the model, one provider whose base URL is the
23
+ // model proxy over HTTP (`transport: "http"`, since opencode would otherwise
24
+ // open a WebSocket to OpenAI), and denies every other provider. Web search,
25
+ // web fetch and the image and video tools, sharing, the update check, the
26
+ // model catalogue's fetch, automations, snapshots, formatters, language
27
+ // servers and the title agent (a model call of its own) are off. The pins are
28
+ // written twice: as the image's system config, which opencode loads last, so
29
+ // nothing outranks it, and in the run's config.
30
+ //
31
+ // The agent holds only the run's caller key as its provider's key. opencode's
32
+ // step limit ends a run as if it had finished, so it is left unset: the wall
33
+ // clock and the proxy's token cap bound the run.
34
+
35
+ /** The opencode version every sandbox runs, pinned. */
36
+ const VERSION = "2.4.3";
37
+
38
+ /**
39
+ * The Harness image that holds that version. The `harness-images` workflow
40
+ * builds it in the pull request that changes the pin or its Dockerfile, and
41
+ * fails until this holds the digest it pushed
42
+ * (`node docker/harness-images/image.mjs record opencode`).
43
+ */
44
+ const IMAGE = "ghcr.io/mnaimfaizy/agent-kit-sandbox-opencode";
45
+ const IMAGE_DIGEST = "sha256:6f7c304a8aa75ad823d0f345eeab7024f3bd502ece39211723b05a2ed8699450"; // inputs-bada713720af3f1339b1c796d7efa4ec
46
+
47
+ /**
48
+ * The providers opencode runs on, by its own id for each: the model proxy
49
+ * profile that fronts it, the kit secret that holds its key and the variable
50
+ * that key is read from, the package opencode speaks its API with, and where
51
+ * its API's paths begin on the proxy.
52
+ */
53
+ export const PROVIDERS = Object.freeze({
54
+ anthropic: Object.freeze({
55
+ displayName: "Anthropic",
56
+ profile: "anthropic",
57
+ secret: "anthropic_api_key",
58
+ env: "ANTHROPIC_API_KEY",
59
+ package: "@opencode/ai/providers/anthropic",
60
+ path: "/v1",
61
+ whereToGet:
62
+ "For an anthropic/ model: create an API key in a dedicated Anthropic Console workspace with its own spend limit: https://console.anthropic.com/settings/keys.",
63
+ }),
64
+ openai: Object.freeze({
65
+ displayName: "OpenAI",
66
+ profile: "openai",
67
+ secret: "openai_api_key",
68
+ env: "OPENAI_API_KEY",
69
+ package: "@opencode/ai/providers/openai",
70
+ path: "/v1",
71
+ whereToGet:
72
+ "For an openai/ model: create an API key in the OpenAI platform, in a project with its own budget: https://platform.openai.com/api-keys.",
73
+ }),
74
+ google: Object.freeze({
75
+ displayName: "Gemini",
76
+ profile: "gemini-api",
77
+ secret: "gemini_api_key",
78
+ env: "GEMINI_API_KEY",
79
+ package: "@opencode/ai/providers/google",
80
+ path: "/v1beta",
81
+ whereToGet:
82
+ "For a google/ model: create an API key in Google AI Studio, in a project with its own budget: https://aistudio.google.com/apikey.",
83
+ }),
84
+ });
85
+
86
+ /** The model a Stage gets when none is named. */
87
+ export const DEFAULT_MODEL = "anthropic/claude-opus-5-5";
88
+
89
+ /**
90
+ * What a Consumer needs to know about opencode, which `cli/harnesses.json`
91
+ * publishes per tag (ADR 0030): the kit secrets it accepts, one per provider,
92
+ * with where to get each; no effort values, since opencode has no effort
93
+ * setting (a model's variant, `provider/model#variant`, is part of the model);
94
+ * and the model a Stage gets when none is named.
95
+ */
96
+ export const FACTS = Object.freeze({
97
+ displayName: "opencode",
98
+ secrets: Object.freeze(
99
+ Object.entries(PROVIDERS).map(([provider, { secret, whereToGet }]) => ({
100
+ name: secret,
101
+ provider,
102
+ whereToGet,
103
+ })),
104
+ ),
105
+ effortValues: Object.freeze([]),
106
+ defaultModel: DEFAULT_MODEL,
107
+ });
108
+
109
+ /** The tools that reach outside the sandbox, or ask a model API to. */
110
+ export const WEB_TOOLS = Object.freeze([
111
+ "webfetch",
112
+ "websearch",
113
+ "generate_image",
114
+ "generate_video",
115
+ ]);
116
+
117
+ /** The placeholder the sandbox holds in place of the provider's key. */
118
+ const PLACEHOLDER = "agent-kit-deep-scan-placeholder";
119
+
120
+ /** Every variable a provider's key could sit in, removed from the Launcher's commands. */
121
+ const SCRUB = Object.freeze([
122
+ "ANTHROPIC_API_KEY",
123
+ "ANTHROPIC_AUTH_TOKEN",
124
+ "OPENAI_API_KEY",
125
+ "GEMINI_API_KEY",
126
+ "GOOGLE_API_KEY",
127
+ "GOOGLE_GENERATIVE_AI_API_KEY",
128
+ "OPENCODE_API_KEY",
129
+ ]);
130
+
131
+ /** The settings every run holds, in the image's system config and the run's own. */
132
+ const PINS = {
133
+ update: "disable",
134
+ share: "disabled",
135
+ websearch: false,
136
+ automations: false,
137
+ policies: WEB_TOOLS.map((tool) => ({
138
+ action: "tool.use",
139
+ resource: `${tool}:*`,
140
+ effect: "deny",
141
+ })),
142
+ };
143
+
144
+ const CHOOSE = `name the model provider/model, with a provider that has a model proxy profile: ${Object.keys(PROVIDERS).slice(0, -1).join(", ")} or ${Object.keys(PROVIDERS).at(-1)}`;
145
+
146
+ /**
147
+ * The provider and the model's own id that a Stage's model names (none: the
148
+ * default model's), or why it is refused. The refused value is never repeated.
149
+ * @param {string} [model] `provider/model`, with an optional `#variant`
150
+ * @returns {{ provider: string, model: string } | { refused: string }}
151
+ */
152
+ export function providerOf(model = DEFAULT_MODEL) {
153
+ const named = /^([^/#]+)\/([^#]+)(?:#.*)?$/.exec(model);
154
+ if (named === null) {
155
+ return { refused: `This Stage's model names no provider, which opencode needs: ${CHOOSE}.` };
156
+ }
157
+ const [, provider, id] = named;
158
+ if (!Object.hasOwn(PROVIDERS, provider)) {
159
+ return {
160
+ refused: `This Stage's model names a provider with no model proxy profile, so its key could not stay out of the sandbox: ${CHOOSE}.`,
161
+ };
162
+ }
163
+ return { provider, model: id };
164
+ }
165
+
166
+ /** The run's config file. Without a proxy address, no base URL. */
167
+ function configJson({ model, provider, id, proxyUrl }) {
168
+ const { env, package: pkg, path } = PROVIDERS[provider];
169
+ return JSON.stringify(
170
+ {
171
+ $schema: "https://opencode.ai/config.json",
172
+ model,
173
+ providers: {
174
+ [provider]: {
175
+ package: pkg,
176
+ settings: {
177
+ ...(proxyUrl !== undefined ? { baseURL: `${proxyUrl}${path}` } : {}),
178
+ apiKey: `{env:${env}}`,
179
+ transport: "http",
180
+ },
181
+ // Declared, so a model the bundled catalogue does not know still runs.
182
+ models: { [id]: {} },
183
+ },
184
+ },
185
+ ...PINS,
186
+ policies: [
187
+ { action: "provider.use", resource: "*", effect: "deny" },
188
+ { action: "provider.use", resource: provider, effect: "allow" },
189
+ ...PINS.policies,
190
+ ],
191
+ permissions: [
192
+ { action: "*", resource: "*", effect: "allow" },
193
+ ...WEB_TOOLS.map((action) => ({ action, resource: "*", effect: "deny" })),
194
+ ],
195
+ agents: { title: { disabled: true } },
196
+ snapshots: false,
197
+ formatter: false,
198
+ lsp: false,
199
+ },
200
+ null,
201
+ 2,
202
+ );
203
+ }
204
+
205
+ /**
206
+ * The launch plan for `request`.
207
+ * @param {object} [request] a Stage request, as the Claude Code adapter
208
+ * describes it. `model` is `provider/model` and picks the provider; a
209
+ * provider with no profile throws. `effort` and `maxTurns` are ignored:
210
+ * opencode has no effort setting, and a turn limit it would not report. The
211
+ * only `credentialKind` is `api-key`.
212
+ * @returns the nine-part launch plan, in the same shape as Claude Code's.
213
+ */
214
+ export function opencode({
215
+ model = DEFAULT_MODEL,
216
+ credentialKind = "api-key",
217
+ proxyUrl,
218
+ briefPath,
219
+ kitDir = "/agent-kit",
220
+ callerKey,
221
+ } = {}) {
222
+ if (credentialKind !== "api-key") {
223
+ throw new Error(`opencode has no credential kind ${JSON.stringify(credentialKind)}`);
224
+ }
225
+ const chosen = providerOf(model);
226
+ if ("refused" in chosen) throw new Error(chosen.refused);
227
+ const { provider, model: id } = chosen;
228
+ const { profile, secret, env } = PROVIDERS[provider];
229
+ const placeholder = callerKey ?? PLACEHOLDER;
230
+ // The config's folder, which the Launcher empties before it writes the file,
231
+ // and the folders opencode writes to while it runs.
232
+ const home = `${kitDir}/opencode`;
233
+ const state = `${kitDir}/opencode-run`;
234
+ const file = `${home}/opencode.json`;
235
+ return {
236
+ identity: { name: "opencode", displayName: "opencode", version: VERSION, major: 2 },
237
+ installLayer: {
238
+ image: `${IMAGE}@${IMAGE_DIGEST}`,
239
+ env: [],
240
+ run: [
241
+ "ln -sf /opt/agent-kit/bin/opencode /usr/local/bin/opencode \\",
242
+ " && mkdir -p /etc/opencode \\",
243
+ ` && printf '%s\\n' '${JSON.stringify(PINS)}' > /etc/opencode/opencode.json`,
244
+ ],
245
+ },
246
+ modelRoute: { profile },
247
+ credentials: {
248
+ accepted: { "api-key": { secret, env, placeholder } },
249
+ scrub: [...SCRUB],
250
+ },
251
+ launch: {
252
+ argv: [
253
+ "opencode",
254
+ "run",
255
+ // A private server: never the shared one.
256
+ "--standalone",
257
+ // Approves whatever would ask; a denied permission still holds.
258
+ "--auto",
259
+ // Its events go to stdout, the transcript.
260
+ "--format",
261
+ "json",
262
+ ],
263
+ // With no message argument, the prompt is read from stdin.
264
+ stdin: briefPath !== undefined ? { file: briefPath } : null,
265
+ files: [{ path: file, text: configJson({ model, provider, id, proxyUrl }) }],
266
+ env: [
267
+ `${env}=${placeholder}`,
268
+ `OPENCODE_CONFIG=${file}`,
269
+ "OPENCODE_DISABLE_AUTOUPDATE=1",
270
+ "OPENCODE_DISABLE_PROJECT_CONFIG=1",
271
+ "OPENCODE_DISABLE_CLAUDE_CODE=1",
272
+ "OPENCODE_DISABLE_MODELS_FETCH=1",
273
+ `XDG_CONFIG_HOME=${home}/xdg-config`,
274
+ `XDG_DATA_HOME=${state}/data`,
275
+ `XDG_STATE_HOME=${state}/state`,
276
+ `XDG_CACHE_HOME=${state}/cache`,
277
+ // opencode takes its base URL from the config; the shell is told the
278
+ // proxy's address too.
279
+ ...(proxyUrl !== undefined ? [`AGENT_KIT_MODEL_PROXY_URL=${proxyUrl}`] : []),
280
+ ],
281
+ // The Launcher removes `checkoutConfig` from the copy before the launch.
282
+ removeCheckoutConfig: true,
283
+ },
284
+ checkoutConfig: [".opencode", "opencode.json", "opencode.jsonc", ".agents/skills", ".env"],
285
+ instructionFiles: ["AGENTS.md"],
286
+ stops: { turnCap: null },
287
+ transcript: { path: `${kitDir}/logs/agent.log` },
288
+ };
289
+ }
@@ -0,0 +1,156 @@
1
+ // Which harness a Stage runs on (ADR 0022): `--harness`, else the Stage's
2
+ // `AGENT_KIT_<STAGE>_HARNESS`, else `AGENT_KIT_HARNESS`, else Claude Code. An
3
+ // unknown name is refused before anything runs, with the names there are; so
4
+ // is a harness this version has no adapter for, and, on a security Stage, one
5
+ // the model proxy cannot front (ADR 0023). A Custom harness, `custom:<name>`,
6
+ // is then loaded from the trusted commit (ADR 0027).
7
+
8
+ import { firstLine } from "../exec.mjs";
9
+ import { detectDefaultBranch } from "../init.mjs";
10
+ import { CLI_VERSION } from "../tags.mjs";
11
+ import { ADAPTERS, DEFAULT_HARNESS } from "./adapters.mjs";
12
+ import { isCustom } from "./custom/registry.mjs";
13
+ import { loadCustomFile } from "./custom.mjs";
14
+ import { providerOf } from "./opencode.mjs";
15
+ import { SECURITY_STAGES, STAGES } from "./support-levels.mjs";
16
+
17
+ /** Every harness the kit names; `custom:<name>` names a Custom harness beside them. */
18
+ export const HARNESS_NAMES = Object.freeze([
19
+ "claude-code",
20
+ "codex",
21
+ "gemini",
22
+ "antigravity",
23
+ "opencode",
24
+ "cursor",
25
+ ]);
26
+
27
+ const CUSTOM = /^custom:[A-Za-z0-9][A-Za-z0-9._-]*$/;
28
+
29
+ /** opencode 1.x by its package, `opencode-ai`, or by its version. */
30
+ const OPENCODE_1 = /^opencode(-ai(@.*)?|[@-]v?1(\.[0-9x.]*)?)$/;
31
+ const OPENCODE_1_DOCS_URL = `https://github.com/mnaimfaizy/agent-kit/blob/v${CLI_VERSION}/docs/harnesses.md#opencode-1x`;
32
+
33
+ /**
34
+ * @param {{ stage: string, flag?: string, env?: Record<string, string | undefined> }} opts
35
+ * `stage` is the Stage's name, such as `deep-scan`. `flag` is `--harness`'s
36
+ * value, undefined when it was not given. An empty variable is an unset one.
37
+ * @returns {{ harness: string, from: string } | { refused: string, unknown?: true }}
38
+ * `from` is where the name came from; `unknown` marks a name that is no
39
+ * harness at all. The refused value is never repeated.
40
+ */
41
+ export function resolveHarness({ stage, flag, env = {} }) {
42
+ const own = `AGENT_KIT_${stage.toUpperCase().replace(/-/g, "_")}_HARNESS`;
43
+ const found =
44
+ flag !== undefined
45
+ ? { harness: flag, from: "--harness" }
46
+ : [own, "AGENT_KIT_HARNESS"]
47
+ .filter((name) => env[name])
48
+ .map((name) => ({ harness: env[name], from: name }))[0];
49
+ if (found === undefined) return { harness: DEFAULT_HARNESS, from: "the default" };
50
+ const { harness, from } = found;
51
+ if (OPENCODE_1.test(harness)) {
52
+ return {
53
+ refused: `${from} names opencode 1.x (opencode-ai), which the kit does not run: its opencode harness is 2.x (@opencode/cli), named opencode. Keep 1.x by describing it as a Custom harness: ${OPENCODE_1_DOCS_URL}`,
54
+ unknown: true,
55
+ };
56
+ }
57
+ if (!HARNESS_NAMES.includes(harness) && !CUSTOM.test(harness)) {
58
+ return {
59
+ refused: `${from} names no known harness: it is one of ${HARNESS_NAMES.join(", ")} or custom:<name>.`,
60
+ unknown: true,
61
+ };
62
+ }
63
+ if (harness === "cursor" && SECURITY_STAGES.includes(stage)) {
64
+ const runs = stage === "deep-scan" ? "Deep scan" : "static security audit";
65
+ return {
66
+ refused: `${from} names Cursor CLI, which the model proxy cannot front, so it runs no ${runs}: its key would have to sit in the sandbox, and its spend could not be capped.`,
67
+ };
68
+ }
69
+ if (harness === "cursor" && stage === "review-response") {
70
+ return {
71
+ refused: `${from} names Cursor CLI, which runs no Review response: that Stage runs pull request code beside the key, and Cursor CLI's key sits in the sandbox (ADR 0025).`,
72
+ };
73
+ }
74
+ if (harness === "opencode" && stage !== "deep-scan") {
75
+ return {
76
+ refused: `${from} names opencode, which this version runs on the Deep scan alone.`,
77
+ };
78
+ }
79
+ if (!isCustom(harness) && !Object.hasOwn(ADAPTERS, harness)) {
80
+ return {
81
+ refused: `${from} names a harness with no adapter in this version: the Stage runs on ${Object.keys(ADAPTERS).join(" and ")}.`,
82
+ };
83
+ }
84
+ return found;
85
+ }
86
+
87
+ /**
88
+ * Why `harness` cannot run a Stage on `model`, or null. On opencode the model
89
+ * picks the provider, which needs a model proxy profile (ADR 0023). The
90
+ * refused value is never repeated.
91
+ * @param {string} harness
92
+ * @param {string} [model]
93
+ * @returns {string | null}
94
+ */
95
+ export function modelRefusal(harness, model) {
96
+ if (harness !== "opencode") return null;
97
+ const chosen = providerOf(model);
98
+ return "refused" in chosen ? chosen.refused : null;
99
+ }
100
+
101
+ /**
102
+ * Make `harness` ready to run: a Custom harness's file is read from the
103
+ * trusted commit, the one `trustedRef` names, else the default branch's tip as
104
+ * this checkout has it, and never from the checkout under review. A built-in
105
+ * harness needs nothing.
106
+ * @param {{
107
+ * harness: string,
108
+ * runner: import("../exec.mjs").Runner,
109
+ * cwd: string,
110
+ * trustedRef?: string,
111
+ * defaultBranch?: string,
112
+ * flags?: string[],
113
+ * }} opts `flags` are the caller's own options that name where to read from,
114
+ * for a refusal to advise.
115
+ * @returns {{ refused?: string[] } | { text: string, commit: string, at: string }}
116
+ * For a Custom harness that loaded, the file's text and the commit it is
117
+ * from; for a built-in one, nothing.
118
+ */
119
+ export function loadHarness({ harness, runner, cwd, trustedRef, defaultBranch, flags = [] }) {
120
+ if (!isCustom(harness)) return {};
121
+ const top = runner("git", ["-C", cwd, "rev-parse", "--show-toplevel"]);
122
+ if (top.status !== 0) {
123
+ return {
124
+ refused: [
125
+ top.status === null
126
+ ? `Could not run git: ${firstLine(top.stderr)}`
127
+ : `${cwd} is not a git repository; ${harness} is read from its trusted commit.`,
128
+ ],
129
+ };
130
+ }
131
+ const root = top.stdout.trim();
132
+ const branch = trustedRef ?? defaultBranch ?? detectDefaultBranch(runner, root);
133
+ const ref = trustedRef === undefined ? `refs/heads/${branch}` : trustedRef;
134
+ const found = runner("git", [
135
+ "-C",
136
+ root,
137
+ "rev-parse",
138
+ "--verify",
139
+ "--quiet",
140
+ "--end-of-options",
141
+ `${ref}^{commit}`,
142
+ ]);
143
+ if (found.status !== 0) {
144
+ return {
145
+ refused: [
146
+ trustedRef === undefined
147
+ ? `No local branch ${branch} (the default branch) to read ${harness} from: create or fetch it${flags.length ? `, or pass ${flags.join(" or ")}` : ""}.`
148
+ : `The trusted ref ${trustedRef} is not a commit this checkout has: fetch it, or leave it out.`,
149
+ ],
150
+ };
151
+ }
152
+ const commit = found.stdout.trim();
153
+ const at = `${branch} (${commit.slice(0, 12)})`;
154
+ const loaded = loadCustomFile({ harness, runner, root, commit, at, stages: STAGES });
155
+ return "refused" in loaded ? { refused: loaded.refused } : { text: loaded.text, commit, at };
156
+ }
@@ -1,7 +1,8 @@
1
1
  // The Support Level table (ADR 0028): kit-owned, harness by Stage, each cell a
2
2
  // level and a link to the run that earned it. It is published inside
3
3
  // `cli/harnesses.json` (ADR 0030), which `node scripts/gen_harnesses.mjs`
4
- // writes from here and the adapters; preflight and the docs read that file.
4
+ // writes from here and the adapters; preflight, `doctor`, `init` and the docs
5
+ // read that file.
5
6
  //
6
7
  // A pair starts at `documented` when its adapter merges. A level changes only
7
8
  // by a pull request that edits its cell here and cites the run:
@@ -33,17 +34,164 @@ export const SUPPORT_LEVELS = Object.freeze(["documented", "tried", "verified"])
33
34
 
34
35
  const documented = () => ({ level: "documented", run: null });
35
36
 
37
+ /**
38
+ * The Stages this kit runs on each harness: the ones whose reusable workflow's
39
+ * check step does not refuse it as "not wired". A contract test holds the two
40
+ * equal, so the pull request that wires a Stage adds it here.
41
+ * @type {Readonly<Record<string, readonly string[]>>}
42
+ */
43
+ const WIRED = Object.freeze({
44
+ "claude-code": STAGES,
45
+ codex: ["deep-scan", "security-audit", "review", "plan", "implement", "review-response"],
46
+ gemini: ["deep-scan", "review", "plan", "implement", "review-response"],
47
+ antigravity: ["deep-scan", "review", "plan", "implement", "review-response"],
48
+ opencode: ["deep-scan"],
49
+ cursor: ["plan", "implement", "review"],
50
+ });
51
+
52
+ /**
53
+ * The highest level a harness may hold, where its CLI cannot be pinned (ADR
54
+ * 0028): Cursor CLI's backend changes behind the binary, so it stops at
55
+ * `tried`. Building the table fails on a cell above its cap.
56
+ * @type {Readonly<Record<string, string>>}
57
+ */
58
+ export const LEVEL_CAPS = Object.freeze({ cursor: "tried" });
59
+
36
60
  /** @type {Record<string, Record<string, { level: string | null, run: string | null }>>} */
37
- export const SUPPORT_TABLE = Object.freeze({
61
+ const LEVELS = {
38
62
  "claude-code": {
39
63
  "deep-scan": {
40
64
  level: "tried",
41
65
  run: "https://github.com/mnaimfaizy/agent-kit/issues/458#issuecomment-7249921732",
42
66
  },
67
+ "security-audit": {
68
+ level: "tried",
69
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46854989738",
70
+ },
71
+ review: {
72
+ level: "tried",
73
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46853470393",
74
+ },
75
+ plan: {
76
+ level: "tried",
77
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46849892166",
78
+ },
79
+ implement: {
80
+ level: "tried",
81
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46850644168",
82
+ },
83
+ "review-response": {
84
+ level: "tried",
85
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46853902574",
86
+ },
87
+ },
88
+ codex: {
89
+ "deep-scan": documented(),
90
+ "security-audit": {
91
+ level: "tried",
92
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46844164694",
93
+ },
94
+ review: {
95
+ level: "tried",
96
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46835416383",
97
+ },
98
+ plan: {
99
+ level: "tried",
100
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46826363622",
101
+ },
102
+ implement: {
103
+ level: "tried",
104
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46833250256",
105
+ },
106
+ "review-response": {
107
+ level: "tried",
108
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/46837975839",
109
+ },
110
+ },
111
+ gemini: {
112
+ "deep-scan": documented(),
113
+ "security-audit": documented(),
114
+ review: {
115
+ level: "tried",
116
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47025534828",
117
+ },
118
+ plan: {
119
+ level: "tried",
120
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47022825316",
121
+ },
122
+ implement: {
123
+ level: "tried",
124
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47023347347",
125
+ },
126
+ "review-response": {
127
+ level: "tried",
128
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47027665621",
129
+ },
130
+ },
131
+ antigravity: {
132
+ "deep-scan": documented(),
133
+ "security-audit": documented(),
134
+ review: {
135
+ level: "tried",
136
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47069605427",
137
+ },
138
+ plan: {
139
+ level: "tried",
140
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47068109902",
141
+ },
142
+ implement: {
143
+ level: "tried",
144
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47068557808",
145
+ },
146
+ "review-response": {
147
+ level: "tried",
148
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47070131711",
149
+ },
150
+ },
151
+ opencode: {
152
+ "deep-scan": documented(),
43
153
  "security-audit": documented(),
44
154
  review: documented(),
45
155
  plan: documented(),
46
156
  implement: documented(),
47
157
  "review-response": documented(),
48
158
  },
49
- });
159
+ cursor: {
160
+ "deep-scan": documented(),
161
+ "security-audit": documented(),
162
+ review: documented(),
163
+ plan: {
164
+ level: "tried",
165
+ run: "https://github.com/mnaimfaizy/agent-kit/actions/runs/47244849657",
166
+ },
167
+ implement: documented(),
168
+ "review-response": documented(),
169
+ },
170
+ };
171
+
172
+ for (const [harness, cap] of Object.entries(LEVEL_CAPS)) {
173
+ for (const [stage, { level }] of Object.entries(LEVELS[harness])) {
174
+ if (level !== null && SUPPORT_LEVELS.indexOf(level) > SUPPORT_LEVELS.indexOf(cap)) {
175
+ throw new Error(`${harness} on ${stage} is ${level}, above its cap of ${cap} (ADR 0028)`);
176
+ }
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Each pair's cell as `harnesses.json` publishes it: its level and run, and
182
+ * `wired`, whether this kit runs the Stage on the harness at all.
183
+ * @type {Readonly<Record<string, Record<string, { level: string | null, run: string | null, wired: boolean }>>>}
184
+ */
185
+ export const SUPPORT_TABLE = Object.freeze(
186
+ Object.fromEntries(
187
+ Object.entries(LEVELS).map(([harness, cells]) => [
188
+ harness,
189
+ Object.fromEntries(
190
+ Object.entries(cells).map(([stage, cell]) => [
191
+ stage,
192
+ { ...cell, wired: WIRED[harness].includes(stage) },
193
+ ]),
194
+ ),
195
+ ]),
196
+ ),
197
+ );