release-skill 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/.agents/plugins/marketplace.json +23 -0
  2. package/.claude-plugin/marketplace.json +16 -0
  3. package/.claude-plugin/plugin.json +10 -0
  4. package/.codex-plugin/plugin.json +26 -0
  5. package/CHANGELOG.md +68 -0
  6. package/CODE_OF_CONDUCT.md +76 -0
  7. package/CONTRIBUTING.md +49 -0
  8. package/INSTALL.md +182 -0
  9. package/LICENSE +21 -0
  10. package/NOTICE +25 -0
  11. package/README.md +501 -0
  12. package/README.zh-CN.md +463 -0
  13. package/SECURITY.md +48 -0
  14. package/adapters/claude/.claude-plugin/marketplace.json +16 -0
  15. package/adapters/claude/.claude-plugin/plugin.json +10 -0
  16. package/adapters/claude/skills/release-assess/SKILL.md +52 -0
  17. package/adapters/claude/skills/release-help/SKILL.md +60 -0
  18. package/adapters/claude/skills/release-prepare/SKILL.md +71 -0
  19. package/adapters/claude/skills/release-publish/SKILL.md +55 -0
  20. package/adapters/claude/skills/release-reconcile/SKILL.md +73 -0
  21. package/adapters/claude/skills/release-verify/SKILL.md +70 -0
  22. package/adapters/codex/.codex-plugin/plugin.json +26 -0
  23. package/adapters/codex/skills/release-assess/SKILL.md +52 -0
  24. package/adapters/codex/skills/release-help/SKILL.md +60 -0
  25. package/adapters/codex/skills/release-prepare/SKILL.md +71 -0
  26. package/adapters/codex/skills/release-publish/SKILL.md +55 -0
  27. package/adapters/codex/skills/release-reconcile/SKILL.md +73 -0
  28. package/adapters/codex/skills/release-verify/SKILL.md +70 -0
  29. package/bin/release-skill.mjs +743 -0
  30. package/native/safe-write/binding.gyp +40 -0
  31. package/native/safe-write/prebuilds.json +4 -0
  32. package/native/safe-write/src/safe_write.cc +2023 -0
  33. package/package.json +75 -0
  34. package/references/.render-manifest.json +33 -0
  35. package/references/00-target-state.md +124 -0
  36. package/references/01-state-machine.md +155 -0
  37. package/references/02-project-config.md +217 -0
  38. package/references/03-readme-quality.md +136 -0
  39. package/references/04-supply-chain.md +147 -0
  40. package/references/05-evidence-and-errors.md +164 -0
  41. package/references/06-adapter-contract.md +178 -0
  42. package/schemas/.render-manifest.json +37 -0
  43. package/schemas/approval-record.schema.json +115 -0
  44. package/schemas/artifact-lock.schema.json +111 -0
  45. package/schemas/artifact-plan.schema.json +52 -0
  46. package/schemas/artifact-policy.schema.json +76 -0
  47. package/schemas/evidence-event.schema.json +89 -0
  48. package/schemas/release-plan.schema.json +369 -0
  49. package/schemas/release-project.schema.json +359 -0
  50. package/schemas/release-run.schema.json +195 -0
  51. package/skills/release-assess/SKILL.md +52 -0
  52. package/skills/release-help/SKILL.md +60 -0
  53. package/skills/release-prepare/SKILL.md +71 -0
  54. package/skills/release-publish/SKILL.md +55 -0
  55. package/skills/release-reconcile/SKILL.md +73 -0
  56. package/skills/release-verify/SKILL.md +70 -0
  57. package/skills-src/release-assess/SKILL.md +52 -0
  58. package/skills-src/release-help/SKILL.md +60 -0
  59. package/skills-src/release-prepare/SKILL.md +71 -0
  60. package/skills-src/release-publish/SKILL.md +55 -0
  61. package/skills-src/release-reconcile/SKILL.md +73 -0
  62. package/skills-src/release-verify/SKILL.md +70 -0
  63. package/src/adapters/contract.mjs +214 -0
  64. package/src/adapters/git-github.mjs +214 -0
  65. package/src/adapters/npm.mjs +947 -0
  66. package/src/adapters/plugin-marketplace.mjs +1365 -0
  67. package/src/adapters/push-snapshot.mjs +216 -0
  68. package/src/artifacts/adoption.mjs +743 -0
  69. package/src/artifacts/artifact-plan.mjs +162 -0
  70. package/src/artifacts/entry.mjs +240 -0
  71. package/src/artifacts/git-authority.mjs +637 -0
  72. package/src/artifacts/graph.mjs +189 -0
  73. package/src/artifacts/inspect.mjs +520 -0
  74. package/src/artifacts/inventory.mjs +192 -0
  75. package/src/artifacts/merge/binary.mjs +77 -0
  76. package/src/artifacts/merge/entry-merge.mjs +228 -0
  77. package/src/artifacts/merge/json.mjs +641 -0
  78. package/src/artifacts/merge/markdown.mjs +246 -0
  79. package/src/artifacts/merge/regions.mjs +156 -0
  80. package/src/artifacts/merge/text.mjs +432 -0
  81. package/src/artifacts/merge/tree.mjs +202 -0
  82. package/src/artifacts/merge/yaml.mjs +669 -0
  83. package/src/artifacts/path-key.mjs +94 -0
  84. package/src/artifacts/policy.mjs +319 -0
  85. package/src/artifacts/producer-registry.mjs +439 -0
  86. package/src/artifacts/project-lock.mjs +732 -0
  87. package/src/artifacts/resolution.mjs +658 -0
  88. package/src/artifacts/safe-fs-backend-internal.mjs +680 -0
  89. package/src/artifacts/safe-fs.mjs +72 -0
  90. package/src/artifacts/state.mjs +495 -0
  91. package/src/artifacts/transaction-journal.mjs +983 -0
  92. package/src/artifacts/transaction.mjs +1361 -0
  93. package/src/commands/approve.mjs +280 -0
  94. package/src/commands/artifacts.mjs +627 -0
  95. package/src/commands/assess.mjs +838 -0
  96. package/src/commands/prepare.mjs +1377 -0
  97. package/src/commands/publish.mjs +883 -0
  98. package/src/commands/reconcile.mjs +1255 -0
  99. package/src/commands/verify.mjs +915 -0
  100. package/src/core/approval.mjs +332 -0
  101. package/src/core/baseline.mjs +272 -0
  102. package/src/core/blackbox-hard-gates.mjs +142 -0
  103. package/src/core/config.mjs +448 -0
  104. package/src/core/digest.mjs +90 -0
  105. package/src/core/errors.mjs +113 -0
  106. package/src/core/evidence.mjs +167 -0
  107. package/src/core/hooks.mjs +241 -0
  108. package/src/core/node-version.mjs +64 -0
  109. package/src/core/plan.mjs +735 -0
  110. package/src/core/previous-public-baseline.mjs +204 -0
  111. package/src/core/run.mjs +681 -0
  112. package/src/core/state-machine.mjs +76 -0
  113. package/src/core/version-consistency.mjs +111 -0
  114. package/src/producers/build-adapters.mjs +231 -0
  115. package/src/producers/render-public-assets.mjs +152 -0
  116. package/src/producers/sync-skills.mjs +96 -0
  117. package/src/readme/contract.mjs +297 -0
  118. package/src/readme/examples.mjs +288 -0
  119. package/src/readme/parity.mjs +122 -0
  120. package/src/snapshot/export.mjs +99 -0
  121. package/src/snapshot/frozen.mjs +401 -0
  122. package/src/snapshot/manifest.mjs +207 -0
  123. package/src/snapshot/public-map.mjs +1459 -0
  124. package/src/snapshot/public-path.mjs +110 -0
  125. package/src/snapshot/scan.mjs +419 -0
@@ -0,0 +1,743 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { basename, dirname, join, resolve } from 'node:path';
4
+ import { execFile as execFileCb } from 'node:child_process';
5
+ import { promisify } from 'node:util';
6
+ import { parseNodeMajor, meetsMinimum, computeReadinessStatus } from '../src/core/node-version.mjs';
7
+
8
+ const execFile = promisify(execFileCb);
9
+
10
+ const COMMANDS = new Set(['help', 'assess', 'prepare', 'approve', 'publish', 'reconcile', 'verify', 'artifacts']);
11
+
12
+ /**
13
+ * Check if a command is available and get its version.
14
+ *
15
+ * @param {string} command - The command to check.
16
+ * @param {string[]} versionArgs - Arguments to get version (e.g., ['--version']).
17
+ * @returns {Promise<{available: boolean, version: string|null, required: boolean, diagnostic: string}>}
18
+ */
19
+ async function checkDependency(command, versionArgs = ['--version']) {
20
+ try {
21
+ const { stdout } = await execFile(command, versionArgs, {
22
+ shell: false,
23
+ encoding: 'utf8',
24
+ timeout: 5000,
25
+ });
26
+ const version = stdout.trim().split('\n')[0];
27
+ return {
28
+ available: true,
29
+ version,
30
+ required: command === 'node',
31
+ diagnostic: 'ok',
32
+ };
33
+ } catch (err) {
34
+ return {
35
+ available: false,
36
+ version: null,
37
+ required: command === 'node',
38
+ diagnostic: err.code === 'ENOENT' ? 'not found' : err.message,
39
+ };
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Perform environment and dependency checks.
45
+ *
46
+ * @returns {Promise<object>} Environment check results.
47
+ */
48
+ async function performEnvironmentChecks() {
49
+ const checks = {};
50
+
51
+ // Node.js
52
+ const nodeCheck = await checkDependency('node', ['--version']);
53
+ checks.node = {
54
+ ...nodeCheck,
55
+ required: true,
56
+ minimumVersion: '22.0.0',
57
+ meetsMinimum: nodeCheck.available ? meetsMinimum(parseNodeMajor(nodeCheck.version), 22) : false,
58
+ };
59
+
60
+ // Git
61
+ const gitCheck = await checkDependency('git', ['--version']);
62
+ checks.git = {
63
+ ...gitCheck,
64
+ required: true,
65
+ usage: '版本控制和 baseline 捕获',
66
+ };
67
+
68
+ // pnpm
69
+ const pnpmCheck = await checkDependency('pnpm', ['--version']);
70
+ checks.pnpm = {
71
+ ...pnpmCheck,
72
+ required: false,
73
+ usage: '包管理(推荐)',
74
+ };
75
+
76
+ // npm
77
+ const npmCheck = await checkDependency('npm', ['--version']);
78
+ checks.npm = {
79
+ ...npmCheck,
80
+ required: false,
81
+ usage: '包发布',
82
+ };
83
+
84
+ // GitHub CLI
85
+ const ghCheck = await checkDependency('gh', ['--version']);
86
+ checks.gh = {
87
+ ...ghCheck,
88
+ required: false,
89
+ usage: 'GitHub 操作',
90
+ };
91
+
92
+ const claudeCheck = await checkDependency('claude', ['--version']);
93
+ checks.claude = {
94
+ ...claudeCheck,
95
+ required: false,
96
+ usage: '仅当计划声明 claude-plugin distribution 时用于消费者安装验证',
97
+ };
98
+
99
+ const codexCheck = await checkDependency('codex', ['--version']);
100
+ checks.codex = {
101
+ ...codexCheck,
102
+ required: false,
103
+ usage: '仅当计划声明 codex-plugin distribution 时用于消费者安装验证',
104
+ };
105
+
106
+ return checks;
107
+ }
108
+
109
+ /**
110
+ * Get capability maturity information.
111
+ *
112
+ * @returns {object} Capability maturity information.
113
+ */
114
+ function getCapabilityMaturity() {
115
+ return {
116
+ assess: {
117
+ available: true,
118
+ mode: 'read-only',
119
+ description: 'Read-only assessment of project release readiness',
120
+ },
121
+ prepare: {
122
+ available: true,
123
+ mode: 'offline local writes',
124
+ description: 'Freeze a release plan with snapshots and gates',
125
+ },
126
+ publish: {
127
+ available: true,
128
+ mode: 'controlled production (sandbox-verified)',
129
+ description: 'Publishes frozen GitHub/npm artifacts and runs configured Claude/Codex consumer checkpoints with approval and exact digest confirmation',
130
+ },
131
+ reconcile: {
132
+ available: true,
133
+ mode: 'evidence-based recovery (sandbox-verified)',
134
+ description: 'Reconcile PARTIAL runs, retry safe missing checkpoints, and stop for human decisions on conflicts',
135
+ },
136
+ verify: {
137
+ available: true,
138
+ mode: 'fresh consumer verification (sandbox-verified)',
139
+ description: 'Recheck remote state, exact npm installation, CLI help, and configured Claude/Codex installs before VERIFIED',
140
+ },
141
+ };
142
+ }
143
+
144
+ function printHelp() {
145
+ console.log(`release-skill - Release governance Skill family
146
+
147
+ Usage:
148
+ release-skill <command> [options]
149
+
150
+ Commands:
151
+ help Show this help message and exit
152
+ assess Read-only assessment of project release readiness
153
+ prepare Freeze a release plan (release-skill output to .release-skill/; hooks may do remote ops)
154
+ approve Record local approval for a frozen release plan
155
+ publish Publish frozen GitHub/npm artifacts after approval and digest confirmation
156
+ reconcile Resume PARTIAL state from evidence; conflicts require a human
157
+ verify Fresh remote and consumer verification; only this reaches VERIFIED
158
+ artifacts Artifact status, inspect, update/apply, resolution, and diagnostics
159
+
160
+ Options:
161
+ --root <path> Project root directory (default: cwd)
162
+ --plan <path> Path to the release plan file
163
+ --run <path> Path to the release run file (required for reconcile/verify)
164
+ --approval <path> Path to the approval record
165
+ --production Prepare immutable Git/npm production artifacts
166
+ --confirm-production <digest> Confirm the exact production plan digest
167
+ --output <path> Override prepare/approve output path (non-production only)
168
+ --run-dir <path> Override prepare run directory; production requires one direct child of .release-skill/runs
169
+ --json Output results as JSON
170
+ --version Show version and exit
171
+ -h, --help Show this help message and exit
172
+
173
+ Safety:
174
+ Safe default: help -> assess -> prepare --offline -> human review.
175
+ Production happy end: prepare --production -> approve -> publish -> verify.
176
+ prepare copies current public files into a local snapshot; it does not rewrite source files.
177
+ - Default mode is offline (release-skill pipeline does no remote writes)
178
+ - prepare output goes to .release-skill/ directory only
179
+ - User-configured hooks may write anywhere and perform remote operations
180
+ - To ensure zero remote writes, disable hooks or audit them separately
181
+ - publish requires explicit approval and an exact plan-digest confirmation
182
+ - publish consumes frozen Git/npm artifacts, never the live workspace
183
+ - existing remote objects and uncertain checks stop for human intervention
184
+ - production-equivalent protocol sandbox is verified; a real remote canary is not
185
+
186
+ First safe command:
187
+ release-skill help --json # Environment check (read-only)
188
+ release-skill assess --root <path> --offline --json # Project assessment`);
189
+ }
190
+
191
+ const args = process.argv.slice(2);
192
+ const hasJson = args.includes('--json');
193
+ const positional = args.filter(a => !a.startsWith('--'));
194
+ const command = positional[0];
195
+
196
+ if (!command && (args.includes('--version') || args.includes('-v'))) {
197
+ const { createRequire } = await import('node:module');
198
+ const require = createRequire(import.meta.url);
199
+ const pkg = require('../package.json');
200
+ console.log(pkg.version);
201
+ process.exit(0);
202
+ }
203
+
204
+ if (!command || command === 'help') {
205
+ if (hasJson) {
206
+ // Perform environment checks for --json mode
207
+ const checks = await performEnvironmentChecks();
208
+ const capabilities = getCapabilityMaturity();
209
+
210
+ // Compute readiness: Node >=22 and Git are required; pnpm/npm/gh are optional
211
+ const readiness = computeReadinessStatus({
212
+ nodeAvailable: checks.node.available,
213
+ nodeMeetsMinimum: checks.node.meetsMinimum,
214
+ gitAvailable: checks.git.available,
215
+ });
216
+ const missingRequired = [];
217
+ if (!checks.node.available || !checks.node.meetsMinimum) missingRequired.push('node>=22');
218
+ if (!checks.git.available) missingRequired.push('git');
219
+ const productionMissing = [
220
+ ...missingRequired,
221
+ ...(!checks.npm.available ? ['npm'] : []),
222
+ ...(!checks.gh.available ? ['gh'] : []),
223
+ ];
224
+
225
+ const output = {
226
+ command: 'help',
227
+ mode: 'environment-check',
228
+ status: readiness.status,
229
+ missingRequired,
230
+ readiness: {
231
+ localPreparation: {
232
+ status: readiness.status,
233
+ missingRequired,
234
+ },
235
+ productionPublish: {
236
+ status: productionMissing.length > 0 ? 'NOT_READY' : 'AUTH_CHECK_REQUIRED',
237
+ missingRequired: productionMissing,
238
+ authentication: '运行生产发布前还需验证 gh auth、Git HTTPS credential 与 npm auth;help 不发起网络认证检查。',
239
+ conditionalConsumers: {
240
+ claude: '声明 claude-plugin distribution 时必须可用',
241
+ codex: '声明 codex-plugin distribution 时必须可用',
242
+ },
243
+ },
244
+ },
245
+ checks,
246
+ capabilities,
247
+ maturity: {
248
+ assess: 'read-only (default); --output writes local report',
249
+ prepare: 'offline local writes; requires --acknowledge-hook-side-effects when hooks are configured',
250
+ onlinePrepare: 'previous-public-baseline observation available; production mode freezes publish artifacts and fails closed on drift or unknown state',
251
+ publish: 'GitHub/npm plus configured Claude/Codex consumer checkpoints are sandbox-verified; approval and exact digest confirmation required',
252
+ reconcile: 'PARTIAL recovery sandbox-verified; remote conflicts require human intervention',
253
+ verify: 'fresh exact npm and Claude/Codex consumer installation checks are sandbox-verified; success reaches VERIFIED',
254
+ },
255
+ recommendations: [],
256
+ };
257
+
258
+ // Add recommendations based on checks
259
+ if (!checks.node.available) {
260
+ output.recommendations.push('Install Node.js >= 22.0.0');
261
+ } else if (checks.node.available && !checks.node.meetsMinimum) {
262
+ output.recommendations.push('Upgrade Node.js to version 22 or later');
263
+ }
264
+
265
+ if (!checks.git.available) {
266
+ output.recommendations.push('Install Git for version control operations');
267
+ }
268
+
269
+ if (!checks.pnpm.available) {
270
+ output.recommendations.push('Install pnpm for package management (optional)');
271
+ }
272
+
273
+ if (!checks.npm.available) {
274
+ output.recommendations.push('Install npm for package publishing (optional)');
275
+ }
276
+
277
+ if (!checks.gh.available) {
278
+ output.recommendations.push('Install GitHub CLI for GitHub operations (optional)');
279
+ }
280
+
281
+ if (!checks.claude.available) {
282
+ output.recommendations.push('Install Claude CLI before releasing a configured claude-plugin distribution');
283
+ }
284
+
285
+ if (!checks.codex.available) {
286
+ output.recommendations.push('Install Codex CLI before releasing a configured codex-plugin distribution');
287
+ }
288
+
289
+ console.log(JSON.stringify(output, null, 2));
290
+ process.exit(readiness.status === 'READY' ? 0 : 1);
291
+ } else {
292
+ printHelp();
293
+ process.exit(0);
294
+ }
295
+ }
296
+
297
+ if (!COMMANDS.has(command)) {
298
+ if (hasJson) {
299
+ const output = {
300
+ error: 'UNKNOWN_COMMAND',
301
+ message: `Unknown command: ${command}`,
302
+ exitCode: 2
303
+ };
304
+ console.log(JSON.stringify(output));
305
+ } else {
306
+ console.error(`Error: Unknown command '${command}'`);
307
+ console.error('Run "release-skill help" for available commands.');
308
+ }
309
+ process.exit(2);
310
+ }
311
+
312
+ // --- Assess command routing ---
313
+ if (command === 'assess') {
314
+ const { assessProject } = await import('../src/commands/assess.mjs');
315
+
316
+ const rootIdx = args.indexOf('--root');
317
+ const rawRoot = rootIdx !== -1 && args[rootIdx + 1] ? args[rootIdx + 1] : process.cwd();
318
+ const root = resolve(rawRoot);
319
+ const offline = args.includes('--offline') || !args.includes('--online');
320
+ const outputIdx = args.indexOf('--output');
321
+ const output = outputIdx !== -1 && args[outputIdx + 1] ? args[outputIdx + 1] : undefined;
322
+
323
+ try {
324
+ const report = await assessProject({ root, offline, output });
325
+
326
+ if (hasJson) {
327
+ console.log(JSON.stringify(report, null, 2));
328
+ } else {
329
+ console.log(report.summary);
330
+ }
331
+
332
+ process.exit(report.status === 'ASSESSED' ? 0 : 1);
333
+ } catch (err) {
334
+ if (hasJson) {
335
+ const errOutput = {
336
+ error: err.code ?? 'UNKNOWN_ERROR',
337
+ message: err.message,
338
+ exitCode: err.exitCode ?? 1,
339
+ };
340
+ console.log(JSON.stringify(errOutput));
341
+ } else {
342
+ console.error(`Error: ${err.message}`);
343
+ }
344
+ process.exit(err.exitCode ?? 1);
345
+ }
346
+ }
347
+
348
+ // --- Prepare command routing ---
349
+ if (command === 'prepare') {
350
+ const { prepareRelease } = await import('../src/commands/prepare.mjs');
351
+ const { readFile: readFileFs } = await import('node:fs/promises');
352
+
353
+ const rootIdx = args.indexOf('--root');
354
+ const rawRoot = rootIdx !== -1 && args[rootIdx + 1] ? args[rootIdx + 1] : process.cwd();
355
+ const root = resolve(rawRoot);
356
+ const offline = args.includes('--offline') || !args.includes('--online');
357
+
358
+ // Resolve target version from --target-version or --version flag
359
+ let targetVersion;
360
+ for (const flag of ['--target-version', '--version']) {
361
+ const idx = args.indexOf(flag);
362
+ if (idx !== -1 && args[idx + 1]) {
363
+ targetVersion = args[idx + 1];
364
+ break;
365
+ }
366
+ }
367
+
368
+ const hooksAuthorized = args.includes('--acknowledge-hook-side-effects');
369
+ const production = args.includes('--production');
370
+ const outputIdx = args.indexOf('--output');
371
+ const output = outputIdx !== -1 && args[outputIdx + 1] ? resolve(args[outputIdx + 1]) : undefined;
372
+ const runDirIdx = args.indexOf('--run-dir');
373
+ const runDir = runDirIdx !== -1 && args[runDirIdx + 1] ? resolve(args[runDirIdx + 1]) : undefined;
374
+
375
+ try {
376
+ const result = await prepareRelease({
377
+ root,
378
+ version: targetVersion,
379
+ offline,
380
+ hooksAuthorized,
381
+ production,
382
+ output,
383
+ runDir,
384
+ });
385
+
386
+ if (hasJson) {
387
+ // Output the full plan object plus metadata so consumers
388
+ // can inspect status, units, externalActions, planDigest, etc.
389
+ const planContent = await readFileFs(result.planPath, 'utf8');
390
+ const plan = JSON.parse(planContent);
391
+ console.log(JSON.stringify({
392
+ ...plan,
393
+ planPath: result.planPath,
394
+ planDigest: result.planDigest,
395
+ evidenceDir: result.evidenceDir,
396
+ }, null, 2));
397
+ } else {
398
+ console.log(`Plan frozen at: ${result.planPath}`);
399
+ console.log(`Plan digest: ${result.planDigest}`);
400
+ console.log(`Evidence: ${result.evidenceDir}`);
401
+ }
402
+
403
+ process.exit(0);
404
+ } catch (err) {
405
+ if (hasJson) {
406
+ const errOutput = {
407
+ error: err.code ?? 'UNKNOWN_ERROR',
408
+ message: err.message,
409
+ exitCode: err.exitCode ?? 1,
410
+ };
411
+ console.log(JSON.stringify(errOutput));
412
+ } else {
413
+ console.error(`Error: ${err.message}`);
414
+ }
415
+ process.exit(err.exitCode ?? 1);
416
+ }
417
+ }
418
+
419
+ // --- Approve command routing ---
420
+ if (command === 'approve') {
421
+ const { approvePlan } = await import('../src/commands/approve.mjs');
422
+
423
+ const planIdx = args.indexOf('--plan');
424
+ const planPath = planIdx !== -1 && args[planIdx + 1] ? args[planIdx + 1] : undefined;
425
+ const digestIdx = args.indexOf('--digest');
426
+ const expectedDigest = digestIdx !== -1 && args[digestIdx + 1] ? args[digestIdx + 1] : undefined;
427
+ const actorIdx = args.indexOf('--actor');
428
+ const actor = actorIdx !== -1 && args[actorIdx + 1] ? args[actorIdx + 1] : undefined;
429
+ const outputIdx = args.indexOf('--output');
430
+ const outputPath = outputIdx !== -1 && args[outputIdx + 1] ? resolve(args[outputIdx + 1]) : undefined;
431
+
432
+ if (!planPath || !expectedDigest || !actor) {
433
+ const msg = 'approve requires --plan <path>, --digest <sha256>, and --actor <name>';
434
+ if (hasJson) {
435
+ console.log(JSON.stringify({ error: 'MISSING_PARAMETERS', message: msg, exitCode: 1 }));
436
+ } else {
437
+ console.error(`Error: ${msg}`);
438
+ }
439
+ process.exit(1);
440
+ }
441
+
442
+ try {
443
+ const resolvedPlanPath = resolve(planPath);
444
+ const planDir = dirname(resolvedPlanPath);
445
+ const releaseDir = basename(planDir) === 'plans' && basename(resolvedPlanPath) === `${expectedDigest}.json`
446
+ ? dirname(planDir)
447
+ : planDir;
448
+ const approvalPath = outputPath ?? join(releaseDir, 'approval-record.json');
449
+ const record = await approvePlan({ planPath, expectedDigest, actor, outputPath: approvalPath });
450
+
451
+ if (hasJson) {
452
+ console.log(JSON.stringify(record, null, 2));
453
+ } else {
454
+ console.log(`Plan approved by ${record.actor}`);
455
+ console.log(`Approval record: ${record.approvalPath}`);
456
+ console.log(`Expires at: ${record.expiresAt}`);
457
+ }
458
+
459
+ process.exit(0);
460
+ } catch (err) {
461
+ if (hasJson) {
462
+ const errOutput = {
463
+ error: err.code ?? 'UNKNOWN_ERROR',
464
+ message: err.message,
465
+ exitCode: err.exitCode ?? 1,
466
+ };
467
+ console.log(JSON.stringify(errOutput));
468
+ } else {
469
+ console.error(`Error: ${err.message}`);
470
+ }
471
+ process.exit(err.exitCode ?? 1);
472
+ }
473
+ }
474
+
475
+ // --- Reconcile command routing ---
476
+ if (command === 'reconcile') {
477
+ const { reconcileRelease } = await import('../src/commands/reconcile.mjs');
478
+ const { createGitGithubAdapter } = await import('../src/adapters/git-github.mjs');
479
+ const { createNpmAdapter } = await import('../src/adapters/npm.mjs');
480
+ const { createPluginMarketplaceAdapter } = await import('../src/adapters/plugin-marketplace.mjs');
481
+ const { createPushSnapshotAdapter } = await import('../src/adapters/push-snapshot.mjs');
482
+ const { createAdapterRegistry } = await import('../src/adapters/contract.mjs');
483
+
484
+ const rootIdx = args.indexOf('--root');
485
+ const rawRoot = rootIdx !== -1 && args[rootIdx + 1] ? args[rootIdx + 1] : process.cwd();
486
+ const root = resolve(rawRoot);
487
+ const planIdx = args.indexOf('--plan');
488
+ const planPath = planIdx !== -1 && args[planIdx + 1] ? resolve(args[planIdx + 1]) : undefined;
489
+ const runIdx = args.indexOf('--run');
490
+ const runPath = runIdx !== -1 && args[runIdx + 1] ? resolve(args[runIdx + 1]) : undefined;
491
+ const approvalIdx = args.indexOf('--approval');
492
+ const approvalPath = approvalIdx !== -1 && args[approvalIdx + 1] ? resolve(args[approvalIdx + 1]) : undefined;
493
+ const confirmationIdx = args.indexOf('--confirm-production');
494
+ const productionConfirmation = confirmationIdx !== -1 && args[confirmationIdx + 1]
495
+ ? args[confirmationIdx + 1]
496
+ : undefined;
497
+
498
+ if (!planPath || !runPath) {
499
+ const msg = 'reconcile requires --plan <path> and --run <path>';
500
+ if (hasJson) {
501
+ console.log(JSON.stringify({ error: 'MISSING_PARAMETERS', message: msg, exitCode: 1 }));
502
+ } else {
503
+ console.error(`Error: ${msg}`);
504
+ }
505
+ process.exit(1);
506
+ }
507
+
508
+ try {
509
+ const registry = createAdapterRegistry([
510
+ createGitGithubAdapter(),
511
+ createNpmAdapter(),
512
+ createPluginMarketplaceAdapter(),
513
+ createPushSnapshotAdapter(),
514
+ ]);
515
+
516
+ const result = await reconcileRelease({
517
+ planPath,
518
+ sourceRunPath: runPath,
519
+ approvalPath,
520
+ adapterRegistry: registry,
521
+ root,
522
+ productionConfirmation,
523
+ });
524
+
525
+ if (hasJson) {
526
+ console.log(JSON.stringify(result, null, 2));
527
+ } else {
528
+ console.log(`Reconcile status: ${result.status}`);
529
+ for (const cp of result.checkpoints) {
530
+ console.log(` ${cp.actionId}: ${cp.status}`);
531
+ }
532
+ }
533
+
534
+ process.exit(result.status === 'PUBLISHED' ? 0 : 1);
535
+ } catch (err) {
536
+ if (hasJson) {
537
+ const errOutput = {
538
+ error: err.code ?? 'UNKNOWN_ERROR',
539
+ message: err.message,
540
+ exitCode: err.exitCode ?? 1,
541
+ };
542
+ console.log(JSON.stringify(errOutput));
543
+ } else {
544
+ console.error(`Error: ${err.message}`);
545
+ }
546
+ process.exit(err.exitCode ?? 1);
547
+ }
548
+ }
549
+
550
+ // --- Verify command routing ---
551
+ if (command === 'verify') {
552
+ const { verifyRelease } = await import('../src/commands/verify.mjs');
553
+ const { createGitGithubAdapter } = await import('../src/adapters/git-github.mjs');
554
+ const { createNpmAdapter } = await import('../src/adapters/npm.mjs');
555
+ const { createPluginMarketplaceAdapter } = await import('../src/adapters/plugin-marketplace.mjs');
556
+ const { createPushSnapshotAdapter } = await import('../src/adapters/push-snapshot.mjs');
557
+ const { createAdapterRegistry } = await import('../src/adapters/contract.mjs');
558
+
559
+ const rootIdx = args.indexOf('--root');
560
+ const rawRoot = rootIdx !== -1 && args[rootIdx + 1] ? args[rootIdx + 1] : process.cwd();
561
+ const root = resolve(rawRoot);
562
+ const planIdx = args.indexOf('--plan');
563
+ const planPath = planIdx !== -1 && args[planIdx + 1] ? resolve(args[planIdx + 1]) : undefined;
564
+ const runIdx = args.indexOf('--run');
565
+ const runPath = runIdx !== -1 && args[runIdx + 1] ? resolve(args[runIdx + 1]) : undefined;
566
+
567
+ if (!planPath || !runPath) {
568
+ const msg = 'verify requires --plan <path> and --run <path>';
569
+ if (hasJson) {
570
+ console.log(JSON.stringify({ error: 'MISSING_PARAMETERS', message: msg, exitCode: 1 }));
571
+ } else {
572
+ console.error(`Error: ${msg}`);
573
+ }
574
+ process.exit(1);
575
+ }
576
+
577
+ try {
578
+ const registry = createAdapterRegistry([
579
+ createGitGithubAdapter(),
580
+ createNpmAdapter(),
581
+ createPluginMarketplaceAdapter(),
582
+ createPushSnapshotAdapter(),
583
+ ]);
584
+
585
+ const result = await verifyRelease({
586
+ planPath,
587
+ sourceRunPath: runPath,
588
+ adapterRegistry: registry,
589
+ root,
590
+ });
591
+
592
+ if (hasJson) {
593
+ console.log(JSON.stringify(result, null, 2));
594
+ } else {
595
+ console.log(`Verify status: ${result.status}`);
596
+ console.log(`Adapter checks: ${result.adapterChecks.length} passed`);
597
+ console.log(`Smoke test: ${result.smokeTest.passed ? 'PASSED' : 'FAILED'}`);
598
+ }
599
+
600
+ process.exit(result.status === 'VERIFIED' ? 0 : 1);
601
+ } catch (err) {
602
+ if (hasJson) {
603
+ const errOutput = {
604
+ error: err.code ?? 'UNKNOWN_ERROR',
605
+ message: err.message,
606
+ exitCode: err.exitCode ?? 1,
607
+ };
608
+ console.log(JSON.stringify(errOutput));
609
+ } else {
610
+ console.error(`Error: ${err.message}`);
611
+ }
612
+ process.exit(err.exitCode ?? 1);
613
+ }
614
+ }
615
+
616
+ // --- Publish command routing ---
617
+ if (command === 'publish') {
618
+ const { publishRelease } = await import('../src/commands/publish.mjs');
619
+ const { createGitGithubAdapter } = await import('../src/adapters/git-github.mjs');
620
+ const { createNpmAdapter } = await import('../src/adapters/npm.mjs');
621
+ const { createPluginMarketplaceAdapter } = await import('../src/adapters/plugin-marketplace.mjs');
622
+ const { createPushSnapshotAdapter } = await import('../src/adapters/push-snapshot.mjs');
623
+ const { createAdapterRegistry } = await import('../src/adapters/contract.mjs');
624
+
625
+ const rootIdx = args.indexOf('--root');
626
+ const rawRoot = rootIdx !== -1 && args[rootIdx + 1] ? args[rootIdx + 1] : process.cwd();
627
+ const root = resolve(rawRoot);
628
+ const planIdx = args.indexOf('--plan');
629
+ const planPath = planIdx !== -1 && args[planIdx + 1] ? resolve(args[planIdx + 1]) : undefined;
630
+ const approvalIdx = args.indexOf('--approval');
631
+ const approvalPath = approvalIdx !== -1 && args[approvalIdx + 1] ? resolve(args[approvalIdx + 1]) : undefined;
632
+ const confirmationIdx = args.indexOf('--confirm-production');
633
+ const productionConfirmation = confirmationIdx !== -1 && args[confirmationIdx + 1]
634
+ ? args[confirmationIdx + 1]
635
+ : undefined;
636
+
637
+ if (!planPath || !approvalPath || !productionConfirmation) {
638
+ const msg = 'publish requires --plan <path>, --approval <path>, and --confirm-production <plan-digest>';
639
+ if (hasJson) {
640
+ console.log(JSON.stringify({ error: 'MISSING_PARAMETERS', message: msg, exitCode: 1 }));
641
+ } else {
642
+ console.error(`Error: ${msg}`);
643
+ }
644
+ process.exit(1);
645
+ }
646
+
647
+ try {
648
+ const registry = createAdapterRegistry([
649
+ createGitGithubAdapter(),
650
+ createNpmAdapter(),
651
+ createPluginMarketplaceAdapter(),
652
+ createPushSnapshotAdapter(),
653
+ ]);
654
+
655
+ const result = await publishRelease({
656
+ planPath,
657
+ approvalPath,
658
+ adapterRegistry: registry,
659
+ root,
660
+ productionMode: true,
661
+ productionConfirmation,
662
+ });
663
+
664
+ if (hasJson) {
665
+ console.log(JSON.stringify(result, null, 2));
666
+ } else {
667
+ console.log(`Publish status: ${result.status}`);
668
+ for (const cp of result.checkpoints) {
669
+ console.log(` ${cp.actionId}: ${cp.status}`);
670
+ }
671
+ }
672
+
673
+ process.exit(result.status === 'PUBLISHED' ? 0 : 1);
674
+ } catch (err) {
675
+ if (hasJson) {
676
+ const errOutput = {
677
+ error: err.code ?? 'UNKNOWN_ERROR',
678
+ message: err.message,
679
+ exitCode: err.exitCode ?? 1,
680
+ };
681
+ console.log(JSON.stringify(errOutput));
682
+ } else {
683
+ console.error(`Error: ${err.message}`);
684
+ }
685
+ process.exit(err.exitCode ?? 1);
686
+ }
687
+ }
688
+
689
+ // --- Artifacts command routing ---
690
+ if (command === 'artifacts') {
691
+ const { runArtifactsCommand } = await import('../src/commands/artifacts.mjs');
692
+
693
+ const rootIdx = args.indexOf('--root');
694
+ const rawRoot = rootIdx !== -1 && args[rootIdx + 1] ? args[rootIdx + 1] : process.cwd();
695
+ const root = resolve(rawRoot);
696
+ const outputIdx = args.indexOf('--output');
697
+ const output = outputIdx !== -1 && args[outputIdx + 1] ? resolve(args[outputIdx + 1]) : undefined;
698
+
699
+ const subcommand = positional[1] ?? 'status';
700
+
701
+ try {
702
+ const result = await runArtifactsCommand({ subcommand, args, root });
703
+
704
+ if (hasJson) {
705
+ console.log(JSON.stringify(result, null, 2));
706
+ } else {
707
+ console.log(`Status: ${result.status}`);
708
+ console.log(`Safe to write: ${result.safeToWrite}`);
709
+ console.log(`Target unchanged: ${result.targetUnchanged}`);
710
+ if (result.nextAction) {
711
+ console.log(`Next action: ${result.nextAction.command}`);
712
+ }
713
+ }
714
+
715
+ // Exit code: 0 if clean/safe/drift-detected (dry-run), 1 if blocking
716
+ const blockingStatuses = new Set([
717
+ 'BASE_UNAVAILABLE', 'POLICY_INVALID', 'PATH_UNSAFE',
718
+ 'CONFLICT', 'DIRTY_SCOPE_CONFLICT',
719
+ ]);
720
+ process.exit(blockingStatuses.has(result.status) ? 1 : 0);
721
+ } catch (err) {
722
+ if (hasJson) {
723
+ const errOutput = {
724
+ error: err.code ?? 'UNKNOWN_ERROR',
725
+ message: err.message,
726
+ status: err.code ?? 'UNKNOWN_ERROR',
727
+ safeToWrite: false,
728
+ targetUnchanged: true,
729
+ evidenceDir: null,
730
+ nextAction: { command: 'artifacts inspect --root <path>' },
731
+ exitCode: err.exitCode ?? 1,
732
+ };
733
+ console.log(JSON.stringify(errOutput));
734
+ } else {
735
+ console.error(`Error: ${err.message}`);
736
+ }
737
+ process.exit(err.exitCode ?? 1);
738
+ }
739
+ }
740
+
741
+ // Placeholder: remaining commands will be wired in later tasks
742
+ console.error(`Command '${command}' is not yet implemented.`);
743
+ process.exit(1);