opencode-agent-orchestration-kit 1.0.32 → 1.0.37

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 (88) hide show
  1. package/README.md +73 -4
  2. package/SECURITY.md +7 -0
  3. package/docs/cli.md +100 -0
  4. package/docs/installation.md +25 -5
  5. package/docs/quickstart.md +1 -1
  6. package/docs/releases/v1.0.33.md +72 -0
  7. package/docs/releases/v1.0.34.md +59 -0
  8. package/docs/releases/v1.0.35.md +62 -0
  9. package/docs/releases/v1.0.36.md +58 -0
  10. package/docs/releases/v1.0.37.md +53 -0
  11. package/docs/security.md +4 -0
  12. package/docs/supply-chain.md +9 -5
  13. package/docs/threat-model.md +244 -0
  14. package/docs/use-cases/README.md +31 -0
  15. package/docs/use-cases/direct-label-change/README.md +76 -0
  16. package/docs/use-cases/direct-label-change/before/package.json +8 -0
  17. package/docs/use-cases/direct-label-change/before/src/settings-label.mjs +1 -0
  18. package/docs/use-cases/direct-label-change/before/test/settings-label.test.mjs +8 -0
  19. package/docs/use-cases/direct-label-change/case.json +118 -0
  20. package/docs/use-cases/direct-label-change/expected/package.json +8 -0
  21. package/docs/use-cases/direct-label-change/expected/src/settings-label.mjs +1 -0
  22. package/docs/use-cases/direct-label-change/expected/test/settings-label.test.mjs +8 -0
  23. package/docs/use-cases/feature-tag-normalizer/README.md +78 -0
  24. package/docs/use-cases/feature-tag-normalizer/before/package.json +8 -0
  25. package/docs/use-cases/feature-tag-normalizer/before/src/normalize-tags.mjs +3 -0
  26. package/docs/use-cases/feature-tag-normalizer/before/test/normalize-tags.test.mjs +13 -0
  27. package/docs/use-cases/feature-tag-normalizer/case.json +119 -0
  28. package/docs/use-cases/feature-tag-normalizer/expected/package.json +8 -0
  29. package/docs/use-cases/feature-tag-normalizer/expected/src/normalize-tags.mjs +14 -0
  30. package/docs/use-cases/feature-tag-normalizer/expected/test/normalize-tags.test.mjs +18 -0
  31. package/docs/workflows.md +6 -0
  32. package/opencode/agents/lead.md +11 -2
  33. package/opencode/agents/review_api.md +26 -9
  34. package/opencode/agents/review_coordinator.md +60 -68
  35. package/opencode/agents/review_quality.md +27 -11
  36. package/opencode/agents/review_security.md +26 -10
  37. package/opencode/agents/review_tests.md +26 -9
  38. package/opencode/agents/reviewer.md +95 -80
  39. package/opencode/commands/autonomous.md +75 -0
  40. package/opencode/commands/evolve.md +3 -1
  41. package/opencode/commands/feature.md +6 -2
  42. package/opencode/commands/loop.md +12 -8
  43. package/opencode/commands/review-orchestrated.md +26 -24
  44. package/opencode/commands/review-preflight.md +8 -7
  45. package/opencode/commands/review.md +27 -10
  46. package/opencode/docs/ai/harness/README.md +5 -0
  47. package/opencode/docs/ai/harness/agents.md +27 -3
  48. package/opencode/docs/ai/harness/checks.md +18 -0
  49. package/opencode/docs/ai/harness/commands.md +44 -0
  50. package/opencode/docs/ai/harness/orchestrated-review.md +8 -1
  51. package/opencode/docs/ai/harness/orchestration-contracts.json +924 -0
  52. package/opencode/package-lock.json +14 -8
  53. package/opencode/package.json +1 -0
  54. package/opencode/plugins/shell-export-guard.ts +23 -0
  55. package/opencode/scripts/adversarial-harness.test.mjs +5 -2
  56. package/opencode/scripts/check-harness.mjs +873 -18
  57. package/opencode/scripts/check-harness.test.mjs +562 -0
  58. package/opencode/scripts/loop-state.mjs +88 -14
  59. package/opencode/scripts/loop-state.test.mjs +82 -5
  60. package/opencode/scripts/review-orchestrated-prepare.mjs +18 -1
  61. package/opencode/scripts/review-orchestrated-prepare.test.mjs +49 -0
  62. package/opencode/scripts/shell-export-policy.d.mts +4 -0
  63. package/opencode/scripts/shell-export-policy.mjs +272 -0
  64. package/opencode/scripts/shell-export-policy.test.mjs +96 -0
  65. package/opencode/skills/code-review-and-quality/SKILL.md +63 -321
  66. package/opencode/skills/code-review-and-quality/references/profiles/backend-clean-ddd-cqrs.md +60 -0
  67. package/opencode/skills/code-review-and-quality/references/profiles/backend.md +54 -0
  68. package/opencode/skills/code-review-and-quality/references/profiles/frontend-clean-layered.md +60 -0
  69. package/opencode/skills/code-review-and-quality/references/profiles/frontend.md +62 -0
  70. package/opencode/skills/code-review-and-quality/references/review-policy.md +258 -0
  71. package/package.json +5 -2
  72. package/scripts/check-supply-chain.mjs +16 -2
  73. package/scripts/check-supply-chain.test.mjs +52 -1
  74. package/scripts/check-threat-model.mjs +421 -0
  75. package/scripts/check-threat-model.test.mjs +304 -0
  76. package/scripts/check-use-cases.mjs +501 -0
  77. package/scripts/check-use-cases.test.mjs +225 -0
  78. package/scripts/check.sh +55 -1
  79. package/scripts/install-smoke.sh +24 -1
  80. package/scripts/manage-installation.mjs +567 -25
  81. package/scripts/manage-installation.test.mjs +418 -0
  82. package/scripts/oak.mjs +246 -0
  83. package/scripts/oak.test.mjs +329 -0
  84. package/scripts/package-smoke.mjs +57 -0
  85. package/scripts/package-smoke.test.mjs +75 -2
  86. package/scripts/version.mjs +1 -0
  87. package/scripts/version.test.mjs +3 -2
  88. package/supply-chain.json +1 -0
@@ -143,6 +143,34 @@ function put(root, relative, bytes, mode = 0o644) {
143
143
  };
144
144
  }
145
145
 
146
+ function snapshotTree(root) {
147
+ const entries = [];
148
+ function walk(directory, prefix = "") {
149
+ for (const name of fs.readdirSync(directory).sort()) {
150
+ const relative = prefix ? `${prefix}/${name}` : name;
151
+ const fullPath = path.join(directory, name);
152
+ const stat = fs.lstatSync(fullPath);
153
+ if (stat.isDirectory()) {
154
+ entries.push({ relative, kind: "directory", mode: stat.mode & 0o777 });
155
+ walk(fullPath, relative);
156
+ } else if (stat.isFile()) {
157
+ entries.push({
158
+ relative,
159
+ kind: "file",
160
+ mode: stat.mode & 0o777,
161
+ sha256: crypto.createHash("sha256").update(fs.readFileSync(fullPath)).digest("hex"),
162
+ });
163
+ } else if (stat.isSymbolicLink()) {
164
+ entries.push({ relative, kind: "symlink", target: fs.readlinkSync(fullPath) });
165
+ } else {
166
+ entries.push({ relative, kind: "other", mode: stat.mode & 0o777 });
167
+ }
168
+ }
169
+ }
170
+ if (fs.existsSync(root)) walk(root);
171
+ return entries;
172
+ }
173
+
146
174
  function persistManifest(targetRoot, manifest) {
147
175
  fs.mkdirSync(path.join(targetRoot, ".oak"), { recursive: true, mode: 0o700 });
148
176
  fs.writeFileSync(path.join(targetRoot, ".oak", "manifest.json"), canonicalManifestBytes(manifest), { mode: 0o600 });
@@ -166,11 +194,401 @@ function managerFixture(sourceRoot, overrides = {}) {
166
194
  return createInstallationManager({
167
195
  sourceRoot,
168
196
  versionProvider: () => "1.0.27",
197
+ compatibilityProvider: () => doctorCompatibility(),
198
+ commandRunner: successfulDoctorCommand,
199
+ nodeVersionProvider: () => "v24.14.1",
169
200
  ...deterministicDeps(),
170
201
  ...overrides,
171
202
  });
172
203
  }
173
204
 
205
+ function doctorCompatibility(overrides = {}) {
206
+ return {
207
+ schema_version: 1,
208
+ node: {
209
+ engines: "^22.9.0 || ^24.0.0",
210
+ blocking_majors: [22, 24],
211
+ canary_major: 26,
212
+ },
213
+ opencode: {
214
+ supported_range: ">=1.14.41 <2.0.0",
215
+ minimum_tested: "1.14.41",
216
+ stable_tested: "1.18.4",
217
+ canary: "latest",
218
+ },
219
+ sdk: {
220
+ opencode_plugin: "1.14.41",
221
+ opentui_core: "0.2.5",
222
+ opentui_solid: "0.2.5",
223
+ },
224
+ ...overrides,
225
+ };
226
+ }
227
+
228
+ function successfulDoctorCommand(command, args) {
229
+ if (command === "opencode" && args.length === 1 && args[0] === "--version") {
230
+ return { status: 0, stdout: "1.18.4\n", stderr: "" };
231
+ }
232
+ return { status: 0, stdout: "", stderr: "" };
233
+ }
234
+
235
+ const privatePathCanary = ["", "Users", "private-example"].join("/");
236
+
237
+ function writeDoctorPayload(sourceRoot, { executable = false } = {}) {
238
+ put(sourceRoot, "AGENTS.md", "# Agent map\n");
239
+ put(sourceRoot, "agents/lead.md", "---\ndescription: Lead\nmode: primary\n---\n");
240
+ put(sourceRoot, "commands/feature.md", "---\ndescription: Feature\nagent: lead\n---\n");
241
+ put(sourceRoot, "opencode.json", `${JSON.stringify({
242
+ default_agent: "lead",
243
+ plugin: ["superpowers@git+https://github.com/example/superpowers.git#d884ae04edebef577e82ff7c4e143debd0bbec99"],
244
+ }, null, 2)}\n`);
245
+ put(sourceRoot, "tui.json", `${JSON.stringify({ plugin: ["./plugins/token-tree-usage.tsx"] }, null, 2)}\n`);
246
+ put(sourceRoot, "plugins/token-tree-usage.tsx", "export default {}\n");
247
+ const dependencies = {
248
+ "@opencode-ai/plugin": "1.14.41",
249
+ "@opentui/core": "0.2.5",
250
+ "@opentui/solid": "0.2.5",
251
+ };
252
+ put(sourceRoot, "package.json", `${JSON.stringify({
253
+ type: "module",
254
+ engines: { node: "^22.9.0 || ^24.0.0" },
255
+ dependencies,
256
+ }, null, 2)}\n`);
257
+ const packages = {
258
+ "": { engines: { node: "^22.9.0 || ^24.0.0" }, dependencies },
259
+ };
260
+ for (const [name, version] of Object.entries(dependencies)) {
261
+ packages[`node_modules/${name}`] = { version };
262
+ }
263
+ put(sourceRoot, "package-lock.json", `${JSON.stringify({
264
+ name: "doctor-fixture",
265
+ lockfileVersion: 3,
266
+ packages,
267
+ }, null, 2)}\n`);
268
+ put(sourceRoot, "scripts/update-skill-registry.mjs", "process.exit(0)\n");
269
+ put(sourceRoot, "docs/ai/harness/skill_registry.md", "# Skill Registry\n");
270
+ put(sourceRoot, "docs/ai/harness/skill_registry.json", '{"schema_version":2,"skills":[]}\n');
271
+ put(sourceRoot, "skills/example/SKILL.md", "---\nname: example\ndescription: Example\n---\n");
272
+ if (executable) put(sourceRoot, "scripts/run.sh", "#!/usr/bin/env bash\nexit 0\n", 0o755);
273
+ }
274
+
275
+ async function installedDoctorFixture(t, options = {}) {
276
+ const fixture = makeFixture(t);
277
+ writeDoctorPayload(fixture.sourceRoot, options);
278
+ const manager = managerFixture(fixture.sourceRoot);
279
+ const installed = await manager.run("install", { targetRoot: fixture.targetRoot });
280
+ assert.equal(installed.exitCode, 0);
281
+ for (const [name, version] of Object.entries(doctorCompatibility().sdk).map(([field, version]) => {
282
+ const names = {
283
+ opencode_plugin: "@opencode-ai/plugin",
284
+ opentui_core: "@opentui/core",
285
+ opentui_solid: "@opentui/solid",
286
+ };
287
+ return [names[field], version];
288
+ })) {
289
+ put(fixture.targetRoot, `node_modules/${name}/package.json`, `${JSON.stringify({ name, version })}\n`);
290
+ }
291
+ return { ...fixture, manager };
292
+ }
293
+
294
+ test("[D001] doctor diagnostic report exposes the closed schema in canonical order", async (t) => {
295
+ const { sourceRoot, targetRoot } = makeFixture(t);
296
+ put(sourceRoot, "agents/lead.md", "lead\n");
297
+ const result = await managerFixture(sourceRoot, {
298
+ compatibilityProvider: () => doctorCompatibility(),
299
+ commandRunner: successfulDoctorCommand,
300
+ nodeVersionProvider: () => "v24.14.1",
301
+ }).run("doctor", { targetRoot });
302
+
303
+ assert.deepEqual(result.report.checks.map((check) => check.id), [
304
+ "opencode-version",
305
+ "node-version",
306
+ "dependencies",
307
+ "installed-files",
308
+ "file-drift",
309
+ "required-configuration",
310
+ "optional-plugins",
311
+ "permissions",
312
+ "executable-scripts",
313
+ "skill-registry",
314
+ "compatibility",
315
+ "legacy-residue",
316
+ ]);
317
+ for (const check of result.report.checks) {
318
+ assert.deepEqual(Object.keys(check).sort(), ["action", "id", "status", "summary"]);
319
+ assert.match(check.id, /^[a-z0-9]+(?:-[a-z0-9]+)*$/);
320
+ assert.match(check.status, /^(pass|info|action-required|not-applicable)$/);
321
+ assert.equal(check.summary.includes("\n"), false);
322
+ if (check.status === "action-required") {
323
+ assert.equal(typeof check.action, "string");
324
+ assert.ok(check.action.length > 0);
325
+ } else {
326
+ assert.equal(check.action, null);
327
+ }
328
+ }
329
+ assert.deepEqual(Object.keys(result.report.summary).sort(), [
330
+ "actionRequired",
331
+ "info",
332
+ "notApplicable",
333
+ "pass",
334
+ ]);
335
+ assert.equal(Object.values(result.report.summary).reduce((sum, value) => sum + value, 0), 12);
336
+ });
337
+
338
+ test("[D002] doctor diagnostic node-version uses the canonical engine", async (t) => {
339
+ for (const [version, expected] of [
340
+ ["v22.9.0", "pass"],
341
+ ["v22.99.0", "pass"],
342
+ ["v24.0.0", "pass"],
343
+ ["v24.14.1", "pass"],
344
+ ["v22.8.9", "action-required"],
345
+ ["v23.0.0", "action-required"],
346
+ ["v26.0.0", "action-required"],
347
+ ]) {
348
+ await t.test(version, async (child) => {
349
+ const { sourceRoot, targetRoot } = makeFixture(child);
350
+ put(sourceRoot, "agents/lead.md", "lead\n");
351
+ const result = await managerFixture(sourceRoot, {
352
+ compatibilityProvider: () => doctorCompatibility(),
353
+ commandRunner: successfulDoctorCommand,
354
+ nodeVersionProvider: () => version,
355
+ }).run("doctor", { targetRoot });
356
+ const check = result.report.checks.find((entry) => entry.id === "node-version");
357
+ assert.equal(check.status, expected);
358
+ });
359
+ }
360
+ });
361
+
362
+ test("[D003] doctor diagnostic opencode-version is bounded and sanitized", async (t) => {
363
+ for (const [version, expected] of [
364
+ ["1.18.4", "pass"],
365
+ ["1.14.41", "pass"],
366
+ ["1.14.40", "action-required"],
367
+ ["2.0.0", "action-required"],
368
+ ["invalid", "action-required"],
369
+ ]) {
370
+ await t.test(version, async (child) => {
371
+ const { sourceRoot, targetRoot } = makeFixture(child);
372
+ put(sourceRoot, "agents/lead.md", "lead\n");
373
+ const calls = [];
374
+ const canary = `${privatePathCanary} provider-canary stderr-canary`;
375
+ const result = await managerFixture(sourceRoot, {
376
+ compatibilityProvider: () => doctorCompatibility(),
377
+ nodeVersionProvider: () => "v24.14.1",
378
+ commandRunner(command, args, options) {
379
+ calls.push({ command, args, options });
380
+ return { status: 0, stdout: `${version}\n`, stderr: canary };
381
+ },
382
+ }).run("doctor", { targetRoot });
383
+ const check = result.report.checks.find((entry) => entry.id === "opencode-version");
384
+ assert.equal(check.status, expected);
385
+ assert.equal(JSON.stringify(result.report).includes(canary), false);
386
+ assert.equal(calls[0].command, "opencode");
387
+ assert.deepEqual(calls[0].args, ["--version"]);
388
+ assert.equal(calls[0].options.shell, false);
389
+ assert.ok(calls[0].options.timeout > 0);
390
+ assert.ok(calls[0].options.maxBuffer > 0);
391
+ });
392
+ }
393
+ });
394
+
395
+ test("[D004] doctor diagnostic dependencies validates direct installed packages without npm", async (t) => {
396
+ const healthy = await installedDoctorFixture(t);
397
+ const passing = await healthy.manager.run("doctor", { targetRoot: healthy.targetRoot });
398
+ assert.equal(passing.report.checks.find((entry) => entry.id === "dependencies").status, "pass");
399
+
400
+ fs.rmSync(path.join(healthy.targetRoot, "node_modules", "@opentui", "solid"), { recursive: true });
401
+ const missing = await healthy.manager.run("doctor", { targetRoot: healthy.targetRoot });
402
+ assert.equal(missing.report.checks.find((entry) => entry.id === "dependencies").status, "action-required");
403
+ });
404
+
405
+ test("[D005] doctor diagnostic required-configuration accepts valid managed configuration", async (t) => {
406
+ const fixture = await installedDoctorFixture(t);
407
+ const passing = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
408
+ assert.equal(passing.report.checks.find((entry) => entry.id === "required-configuration").status, "pass");
409
+
410
+ fs.writeFileSync(path.join(fixture.targetRoot, "opencode.json"), "{bad\n");
411
+ const invalid = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
412
+ assert.equal(invalid.exitCode, 2);
413
+ });
414
+
415
+ test("[D006] doctor diagnostic optional-plugins distinguishes pinned external and broken local references", async (t) => {
416
+ const fixture = await installedDoctorFixture(t);
417
+ const informational = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
418
+ assert.equal(informational.report.checks.find((entry) => entry.id === "optional-plugins").status, "info");
419
+
420
+ fs.rmSync(path.join(fixture.targetRoot, "plugins", "token-tree-usage.tsx"));
421
+ const broken = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
422
+ assert.equal(broken.report.checks.find((entry) => entry.id === "optional-plugins").status, "action-required");
423
+ });
424
+
425
+ test("[D007] doctor diagnostic installed-files and file-drift preserve lifecycle evidence", async (t) => {
426
+ const fixture = await installedDoctorFixture(t);
427
+ const passing = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
428
+ assert.equal(passing.report.checks.find((entry) => entry.id === "installed-files").status, "pass");
429
+ assert.equal(passing.report.checks.find((entry) => entry.id === "file-drift").status, "pass");
430
+
431
+ fs.writeFileSync(path.join(fixture.targetRoot, "agents", "lead.md"), "changed\n");
432
+ const drifted = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
433
+ assert.equal(drifted.report.checks.find((entry) => entry.id === "file-drift").status, "action-required");
434
+ });
435
+
436
+ test("[D008] doctor diagnostic permissions reports managed mode drift", async (t) => {
437
+ const fixture = await installedDoctorFixture(t);
438
+ const passing = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
439
+ assert.equal(passing.report.checks.find((entry) => entry.id === "permissions").status, "pass");
440
+
441
+ fs.chmodSync(path.join(fixture.targetRoot, "agents", "lead.md"), 0o600);
442
+ const drifted = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
443
+ assert.equal(drifted.report.checks.find((entry) => entry.id === "permissions").status, "action-required");
444
+
445
+ const preserved = preservedFixture(t);
446
+ fs.chmodSync(path.join(preserved.targetRoot, "opencode.json"), 0o644);
447
+ const preservedMode = await managerFixture(preserved.sourceRoot).run("doctor", { targetRoot: preserved.targetRoot });
448
+ assert.equal(preservedMode.report.checks.find((entry) => entry.id === "permissions").status, "info");
449
+
450
+ const state = await installedDoctorFixture(t);
451
+ fs.chmodSync(path.join(state.targetRoot, ".oak", "manifest.json"), 0o644);
452
+ const permissiveState = await state.manager.run("doctor", { targetRoot: state.targetRoot });
453
+ assert.equal(permissiveState.report.checks.find((entry) => entry.id === "permissions").status, "action-required");
454
+ });
455
+
456
+ test("[D009] doctor diagnostic executable-scripts follows approved manifest modes", async (t) => {
457
+ const sourceOnly = makeFixture(t);
458
+ put(sourceOnly.sourceRoot, "agents/lead.md", "lead\n");
459
+ put(path.join(sourceOnly.root, "repo"), "package.json", "{}\n");
460
+ for (const relative of [
461
+ "install.sh",
462
+ "uninstall.sh",
463
+ "upgrade.sh",
464
+ "doctor.sh",
465
+ "rollback.sh",
466
+ "scripts/check.sh",
467
+ "scripts/opencode-compat-smoke.sh",
468
+ "scripts/package-smoke.sh",
469
+ ]) {
470
+ put(path.join(sourceOnly.root, "repo"), relative, "#!/usr/bin/env bash\n", 0o755);
471
+ }
472
+ const sourceManager = managerFixture(sourceOnly.sourceRoot, {
473
+ repositoryRoot: path.join(sourceOnly.root, "repo"),
474
+ });
475
+ const sourcePassing = await sourceManager.run("doctor", { targetRoot: sourceOnly.targetRoot });
476
+ assert.equal(sourcePassing.report.checks.find((entry) => entry.id === "executable-scripts").status, "pass");
477
+ fs.chmodSync(path.join(sourceOnly.root, "repo", "doctor.sh"), 0o644);
478
+ const sourceBroken = await sourceManager.run("doctor", { targetRoot: sourceOnly.targetRoot });
479
+ assert.equal(sourceBroken.report.checks.find((entry) => entry.id === "executable-scripts").status, "action-required");
480
+
481
+ const fixture = await installedDoctorFixture(t, { executable: true });
482
+ const passing = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
483
+ assert.equal(passing.report.checks.find((entry) => entry.id === "executable-scripts").status, "pass");
484
+
485
+ fs.chmodSync(path.join(fixture.targetRoot, "scripts", "run.sh"), 0o644);
486
+ const drifted = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
487
+ assert.equal(drifted.report.checks.find((entry) => entry.id === "executable-scripts").status, "action-required");
488
+ });
489
+
490
+ test("[D010] doctor diagnostic skill-registry is bounded and local", async (t) => {
491
+ const fixture = await installedDoctorFixture(t);
492
+ const passing = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
493
+ assert.equal(passing.report.checks.find((entry) => entry.id === "skill-registry").status, "pass");
494
+
495
+ const failingManager = managerFixture(fixture.sourceRoot, {
496
+ commandRunner(command, args) {
497
+ if (command === "opencode") return { status: 0, stdout: "1.18.4\n", stderr: "" };
498
+ assert.equal(command, process.execPath);
499
+ assert.equal(args.at(-1), "--check");
500
+ return { status: 1, stdout: "", stderr: "registry-canary" };
501
+ },
502
+ });
503
+ const failing = await failingManager.run("doctor", { targetRoot: fixture.targetRoot });
504
+ assert.equal(failing.report.checks.find((entry) => entry.id === "skill-registry").status, "action-required");
505
+ assert.equal(JSON.stringify(failing.report).includes("registry-canary"), false);
506
+ });
507
+
508
+ test("[D011] doctor diagnostic legacy-residue recognizes an unmanaged harness without mutation", async (t) => {
509
+ const { sourceRoot, targetRoot } = makeFixture(t);
510
+ put(sourceRoot, "agents/lead.md", "lead\n");
511
+ put(targetRoot, "AGENTS.md", "map\n");
512
+ put(targetRoot, "opencode.json", '{"default_agent":"lead"}\n');
513
+ fs.mkdirSync(path.join(targetRoot, "agents"), { recursive: true });
514
+ fs.mkdirSync(path.join(targetRoot, "commands"), { recursive: true });
515
+ const before = fs.readdirSync(targetRoot).sort();
516
+ const result = await managerFixture(sourceRoot).run("doctor", { targetRoot });
517
+ assert.equal(result.report.checks.find((entry) => entry.id === "legacy-residue").status, "action-required");
518
+ assert.deepEqual(fs.readdirSync(targetRoot).sort(), before);
519
+ assert.equal(fs.existsSync(path.join(targetRoot, ".oak")), false);
520
+ });
521
+
522
+ test("[D012] doctor diagnostic CLI renders ordered findings and immediate actions", async (t) => {
523
+ const fixture = await installedDoctorFixture(t);
524
+ const stdout = capture();
525
+ const stderr = capture();
526
+ const exitCode = await main(["doctor", "--target", fixture.targetRoot], {
527
+ sourceRoot: fixture.sourceRoot,
528
+ versionProvider: () => "1.0.27",
529
+ compatibilityProvider: () => doctorCompatibility(),
530
+ commandRunner: successfulDoctorCommand,
531
+ nodeVersionProvider: () => "v24.14.1",
532
+ stdout: stdout.stream,
533
+ stderr: stderr.stream,
534
+ });
535
+ assert.equal(exitCode, 0);
536
+ assert.match(stdout.read(), /^doctor: healthy;/);
537
+ let previous = -1;
538
+ for (const id of [
539
+ "opencode-version",
540
+ "node-version",
541
+ "dependencies",
542
+ "installed-files",
543
+ "file-drift",
544
+ "required-configuration",
545
+ "optional-plugins",
546
+ "permissions",
547
+ "executable-scripts",
548
+ "skill-registry",
549
+ "compatibility",
550
+ "legacy-residue",
551
+ ]) {
552
+ const index = stdout.read().indexOf(`] ${id}:`);
553
+ assert.ok(index > previous, `${id} must appear in canonical order`);
554
+ previous = index;
555
+ }
556
+ assert.match(stdout.read(), /^summary: pass=\d+ info=\d+ action-required=\d+ not-applicable=\d+$/m);
557
+ assert.equal(stderr.read(), "");
558
+ });
559
+
560
+ test("[D013] doctor diagnostic never renders private subprocess or configuration material", async (t) => {
561
+ const fixture = await installedDoctorFixture(t);
562
+ const configPath = path.join(fixture.targetRoot, "opencode.json");
563
+ const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
564
+ config.unrelated_secret = "config-secret-canary";
565
+ fs.writeFileSync(configPath, `${JSON.stringify(config)}\n`);
566
+ const commandCanary = `${privatePathCanary} provider-canary model-canary stderr-canary`;
567
+ const manager = managerFixture(fixture.sourceRoot, {
568
+ commandRunner(command) {
569
+ return command === "opencode"
570
+ ? { status: 0, stdout: "1.18.4\n", stderr: commandCanary }
571
+ : { status: 1, stdout: commandCanary, stderr: commandCanary };
572
+ },
573
+ });
574
+ const result = await manager.run("doctor", { targetRoot: fixture.targetRoot });
575
+ const serialized = JSON.stringify(result.report);
576
+ for (const canary of ["config-secret-canary", privatePathCanary, "provider-canary", "model-canary", "stderr-canary"]) {
577
+ assert.equal(serialized.includes(canary), false);
578
+ }
579
+ });
580
+
581
+ test("[D014] actionable ordinary doctor leaves the complete target unchanged", async (t) => {
582
+ const fixture = await installedDoctorFixture(t);
583
+ fs.rmSync(path.join(fixture.targetRoot, "node_modules", "@opentui", "solid"), { recursive: true });
584
+ const before = snapshotTree(fixture.targetRoot);
585
+ const result = await fixture.manager.run("doctor", { targetRoot: fixture.targetRoot });
586
+ const after = snapshotTree(fixture.targetRoot);
587
+ assert.equal(result.exitCode, 1);
588
+ assert.deepEqual(after, before);
589
+ assert.equal(fs.existsSync(path.join(fixture.targetRoot, ".oak", "lock.json")), false);
590
+ });
591
+
174
592
  test("[S020] CLI parsing, help, target precedence, and exit codes are deterministic", async (t) => {
175
593
  await t.test("explicit target wins without tilde expansion", () => {
176
594
  const parsed = parseCliArgs("install", ["--dry-run", "--target", "~/oak"]);
@@ -0,0 +1,246 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { spawnSync } from "node:child_process";
6
+ import { fileURLToPath } from "node:url";
7
+
8
+ import { resolveTarget } from "./manage-installation.mjs";
9
+
10
+ export const OAK_COMMANDS = [
11
+ "install",
12
+ "upgrade",
13
+ "doctor",
14
+ "check",
15
+ "replay",
16
+ "state",
17
+ "uninstall",
18
+ "rollback",
19
+ "version",
20
+ ];
21
+
22
+ const SCRIPT_PATH = fileURLToPath(import.meta.url);
23
+ const REPOSITORY_ROOT = path.dirname(path.dirname(SCRIPT_PATH));
24
+
25
+ export const OAK_ENTRYPOINTS = Object.freeze({
26
+ manager: path.join(REPOSITORY_ROOT, "scripts", "manage-installation.mjs"),
27
+ version: path.join(REPOSITORY_ROOT, "scripts", "version.mjs"),
28
+ check: path.join(REPOSITORY_ROOT, "opencode", "scripts", "check-harness.mjs"),
29
+ replay: path.join(REPOSITORY_ROOT, "opencode", "scripts", "replay-routing.mjs"),
30
+ state: path.join(REPOSITORY_ROOT, "opencode", "scripts", "loop-state.mjs"),
31
+ });
32
+
33
+ export const OAK_REPLAY_DEFAULTS = Object.freeze({
34
+ corpus: path.join(
35
+ REPOSITORY_ROOT,
36
+ "opencode",
37
+ "docs",
38
+ "ai",
39
+ "evolution",
40
+ "benchmarks",
41
+ "router-scenarios.jsonl",
42
+ ),
43
+ fixtures: path.join(
44
+ REPOSITORY_ROOT,
45
+ "opencode",
46
+ "docs",
47
+ "ai",
48
+ "evolution",
49
+ "benchmarks",
50
+ "replay-fixtures.jsonl",
51
+ ),
52
+ });
53
+
54
+ const HELP = {
55
+ install: "Usage: oak install [--dry-run] [--force] [--target PATH]",
56
+ upgrade: "Usage: oak upgrade [--dry-run] [--target PATH]",
57
+ doctor: "Usage: oak doctor [--accept-preserved PATH] [--target PATH]",
58
+ check: "Usage: oak check [--target PATH]",
59
+ replay: "Usage: oak replay [--corpus PATH] [--fixtures PATH] [--output PATH]",
60
+ state: "Usage: oak state <init|resume|record|release|inspect|repair|migrate> --root PATH [options]",
61
+ uninstall: "Usage: oak uninstall [--dry-run] [--yes] [--target PATH]",
62
+ rollback: "Usage: oak rollback [--dry-run] [--target PATH]",
63
+ version: "Usage: oak version",
64
+ };
65
+ const LIFECYCLE_COMMANDS = new Set([
66
+ "install",
67
+ "upgrade",
68
+ "doctor",
69
+ "uninstall",
70
+ "rollback",
71
+ ]);
72
+
73
+ export function renderOakHelp(command) {
74
+ if (command) return `${HELP[command]}\n`;
75
+ return `Usage: oak <command> [options]
76
+
77
+ Commands:
78
+ ${OAK_COMMANDS.map((name) => ` ${name}`).join("\n")}
79
+ `;
80
+ }
81
+
82
+ function runNode(entrypoint, args, options, runtime) {
83
+ const result = runtime.run(process.execPath, [entrypoint, ...args], {
84
+ cwd: options.cwd,
85
+ env: options.env,
86
+ stdio: "inherit",
87
+ shell: false,
88
+ });
89
+ if (result?.error || result?.signal || !Number.isInteger(result?.status)) {
90
+ runtime.stderr.write(`oak: unable to run ${options.label}\n`);
91
+ return 2;
92
+ }
93
+ return result.status;
94
+ }
95
+
96
+ function invalid(stderr, message) {
97
+ stderr.write(`oak: ${message}\n`);
98
+ return 2;
99
+ }
100
+
101
+ export function resolveCheckTarget(options, env = process.env) {
102
+ return resolveTarget(options, env);
103
+ }
104
+
105
+ function dispatchCheck(args, runtime) {
106
+ let target;
107
+ if (args.length === 0) {
108
+ target = undefined;
109
+ } else if (args.length === 2 && args[0] === "--target" && args[1] && !args[1].startsWith("--")) {
110
+ target = args[1];
111
+ } else {
112
+ return invalid(runtime.stderr, "invalid check arguments");
113
+ }
114
+ let absoluteTarget;
115
+ let stat;
116
+ try {
117
+ absoluteTarget = path.resolve(resolveCheckTarget({ target }, runtime.env));
118
+ stat = runtime.fsOps.lstatSync(absoluteTarget);
119
+ } catch {
120
+ return invalid(runtime.stderr, "check target is unavailable");
121
+ }
122
+ if (stat.isSymbolicLink() || !stat.isDirectory()) {
123
+ return invalid(runtime.stderr, "check target must be a non-symlink directory");
124
+ }
125
+ return runNode(
126
+ OAK_ENTRYPOINTS.check,
127
+ [],
128
+ { cwd: absoluteTarget, env: runtime.env, label: "check" },
129
+ runtime,
130
+ );
131
+ }
132
+
133
+ function dispatchReplay(args, runtime) {
134
+ const values = new Map();
135
+ const allowed = new Set(["--corpus", "--fixtures", "--output"]);
136
+ for (let index = 0; index < args.length; index += 2) {
137
+ const flag = args[index];
138
+ const value = args[index + 1];
139
+ if (!allowed.has(flag) || values.has(flag) || !value || value.startsWith("--")) {
140
+ return invalid(runtime.stderr, "invalid replay arguments");
141
+ }
142
+ values.set(flag, value);
143
+ }
144
+ const replayArgs = [
145
+ "--corpus",
146
+ values.get("--corpus") ?? OAK_REPLAY_DEFAULTS.corpus,
147
+ "--fixtures",
148
+ values.get("--fixtures") ?? OAK_REPLAY_DEFAULTS.fixtures,
149
+ ];
150
+ if (values.has("--output")) replayArgs.push("--output", values.get("--output"));
151
+ return runNode(
152
+ OAK_ENTRYPOINTS.replay,
153
+ replayArgs,
154
+ { cwd: runtime.cwd, env: runtime.env, label: "replay" },
155
+ runtime,
156
+ );
157
+ }
158
+
159
+ function dispatchState(args, runtime) {
160
+ const [action, ...options] = args;
161
+ const actions = new Set(["init", "resume", "record", "release", "inspect", "repair", "migrate"]);
162
+ const valueFlags = new Set([
163
+ "--root",
164
+ "--slug",
165
+ "--contract",
166
+ "--git-baseline",
167
+ "--session-id",
168
+ "--action-id",
169
+ "--planned-iterations",
170
+ "--iteration",
171
+ "--completed-step",
172
+ "--blocking-cause",
173
+ "--status",
174
+ "--approval-status",
175
+ ]);
176
+ const booleanFlags = new Set(["--release-lock", "--truncate-tail"]);
177
+ if (!actions.has(action)) return invalid(runtime.stderr, "invalid state action");
178
+
179
+ let root;
180
+ for (let index = 0; index < options.length; index += 1) {
181
+ const flag = options[index];
182
+ if (booleanFlags.has(flag)) continue;
183
+ if (!valueFlags.has(flag)) return invalid(runtime.stderr, "invalid state arguments");
184
+ const value = options[index + 1];
185
+ if (!value || value.startsWith("--")) return invalid(runtime.stderr, "invalid state arguments");
186
+ if (flag === "--root") {
187
+ if (root !== undefined) return invalid(runtime.stderr, "invalid state arguments");
188
+ root = value;
189
+ }
190
+ index += 1;
191
+ }
192
+ if (root === undefined) return invalid(runtime.stderr, "state requires --root PATH");
193
+ return runNode(
194
+ OAK_ENTRYPOINTS.state,
195
+ args,
196
+ { cwd: path.resolve(root), env: runtime.env, label: "state" },
197
+ runtime,
198
+ );
199
+ }
200
+
201
+ export function dispatchOak(argv, deps = {}) {
202
+ const runtime = {
203
+ run: deps.run ?? spawnSync,
204
+ stdout: deps.stdout ?? process.stdout,
205
+ stderr: deps.stderr ?? process.stderr,
206
+ env: deps.env ?? process.env,
207
+ cwd: deps.cwd ?? process.cwd(),
208
+ fsOps: deps.fsOps ?? fs,
209
+ };
210
+
211
+ if (argv.length === 1 && ["--help", "help"].includes(argv[0])) {
212
+ runtime.stdout.write(renderOakHelp());
213
+ return 0;
214
+ }
215
+ if (argv.length === 1 && ["--version", "version"].includes(argv[0])) {
216
+ return runNode(
217
+ OAK_ENTRYPOINTS.version,
218
+ [],
219
+ { cwd: runtime.cwd, env: runtime.env, label: "version" },
220
+ runtime,
221
+ );
222
+ }
223
+ const [command, ...rest] = argv;
224
+ if (!OAK_COMMANDS.includes(command)) return invalid(runtime.stderr, "unknown or missing command");
225
+ if (rest.length === 1 && rest[0] === "--help") {
226
+ runtime.stdout.write(renderOakHelp(command));
227
+ return 0;
228
+ }
229
+ if (command === "version") return invalid(runtime.stderr, "version accepts no arguments");
230
+ if (command === "check") return dispatchCheck(rest, runtime);
231
+ if (command === "replay") return dispatchReplay(rest, runtime);
232
+ if (command === "state") return dispatchState(rest, runtime);
233
+ if (LIFECYCLE_COMMANDS.has(command)) {
234
+ return runNode(
235
+ OAK_ENTRYPOINTS.manager,
236
+ [command, ...rest],
237
+ { cwd: runtime.cwd, env: runtime.env, label: command },
238
+ runtime,
239
+ );
240
+ }
241
+ return invalid(runtime.stderr, `${command} is not implemented`);
242
+ }
243
+
244
+ const isEntrypoint = process.argv[1]
245
+ && fs.realpathSync(fileURLToPath(import.meta.url)) === fs.realpathSync(process.argv[1]);
246
+ if (isEntrypoint) process.exitCode = dispatchOak(process.argv.slice(2));