@deeeed/metamask-harness 0.17.5 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/CHANGELOG.md +1059 -0
  2. package/README.md +91 -140
  3. package/adapters/core/inject.sh +1 -6
  4. package/adapters/extension/console-tail.mjs +55 -24
  5. package/adapters/extension/ensure-browser.sh +6 -1
  6. package/adapters/extension/inject.mjs +7 -13
  7. package/adapters/extension/launch-browser.cjs +7 -1
  8. package/adapters/extension/launch-webpack.cjs +83 -0
  9. package/adapters/extension/launch.sh +8 -11
  10. package/adapters/extension/lib/chrome-args.cjs +8 -1
  11. package/adapters/extension/lib/macos-focus.cjs +32 -0
  12. package/adapters/extension/live.sh +12 -21
  13. package/adapters/extension/reattach.sh +1 -0
  14. package/adapters/extension/seed-fixture.sh +4 -12
  15. package/adapters/extension/sidepanel-toggle.sh +4 -1
  16. package/adapters/extension/stamp-runtime-title.cjs +58 -0
  17. package/adapters/extension/start-watch.sh +17 -4
  18. package/adapters/extension/stop-viewers.sh +1 -1
  19. package/adapters/extension/sync-webpack-dist.cjs +107 -0
  20. package/adapters/extension/verify.sh +2 -2
  21. package/adapters/manifest.json +53 -37
  22. package/adapters/mobile/bridge-runtime/cdp-bridge.cjs +16 -0
  23. package/adapters/mobile/bridge-runtime/console-forwarder.cjs +1 -1
  24. package/adapters/mobile/bridge-runtime/lib/match-bridge-target.cjs +3 -1
  25. package/adapters/mobile/bridge-runtime/lib/target-discovery.cjs +2 -3
  26. package/adapters/mobile/cleanup.sh +16 -176
  27. package/adapters/mobile/inject.sh +91 -671
  28. package/adapters/mobile/launch-console-forwarder.cjs +39 -0
  29. package/adapters/mobile/open-device.sh +45 -7
  30. package/adapters/mobile/start-console-forwarder.sh +70 -0
  31. package/adapters/mobile/start-metro.sh +0 -41
  32. package/adapters/mobile/verify.sh +34 -83
  33. package/adapters/mobile/wait-for-bridge.sh +22 -8
  34. package/adapters/shared/harness-source-fingerprint.mjs +14 -12
  35. package/adapters/shared/resolve-slot-ports-core.mjs +17 -6
  36. package/adapters/shared/resolve-slot-ports.sh +17 -5
  37. package/adapters/shared/update-check-worker.mjs +43 -0
  38. package/bin/mm-harness +21 -2
  39. package/dist/adapters/core/surface.js +4 -1
  40. package/dist/adapters/extension/console-capture.js +95 -0
  41. package/dist/adapters/extension/product-config.js +110 -0
  42. package/dist/adapters/extension/runtime-decision.js +20 -2
  43. package/dist/adapters/extension/surface.js +22 -3
  44. package/dist/adapters/harness-freshness.js +49 -0
  45. package/dist/adapters/mobile/perps-env.js +43 -3
  46. package/dist/adapters/mobile/prepare.js +39 -7
  47. package/dist/adapters/mobile/surface.js +5 -2
  48. package/dist/adapters/resolve-slot-ports.js +2 -2
  49. package/dist/adapters/slot-ports.js +13 -16
  50. package/dist/adapters.js +56 -16
  51. package/dist/checkout-lock.js +27 -2
  52. package/dist/cli-color.js +19 -0
  53. package/dist/cli-commands.js +1 -1
  54. package/dist/cli.js +7 -18
  55. package/dist/command-contract.js +462 -0
  56. package/dist/command-journal.js +263 -0
  57. package/dist/commands/call.js +252 -57
  58. package/dist/commands/check.js +10 -4
  59. package/dist/commands/completion-candidates.js +17 -22
  60. package/dist/commands/device-target.js +27 -12
  61. package/dist/commands/doctor.js +106 -25
  62. package/dist/commands/fixtures.js +142 -35
  63. package/dist/commands/last.js +60 -0
  64. package/dist/commands/launch/extension.js +40 -17
  65. package/dist/commands/launch/index.js +207 -63
  66. package/dist/commands/launch/mobile.js +2 -0
  67. package/dist/commands/list-executables.js +187 -29
  68. package/dist/commands/logs.js +8 -6
  69. package/dist/commands/manifest.js +286 -35
  70. package/dist/commands/parse-args.js +68 -4
  71. package/dist/commands/provision.js +10 -4
  72. package/dist/commands/run-engine.js +772 -264
  73. package/dist/commands/run-report.js +12 -3
  74. package/dist/commands/run.js +378 -85
  75. package/dist/commands/shared.js +76 -4
  76. package/dist/commands/status-probe.js +4 -1
  77. package/dist/commands/status.js +2 -1
  78. package/dist/commands/stop.js +7 -2
  79. package/dist/commands/update.js +16 -28
  80. package/dist/completions-cache.js +1 -1
  81. package/dist/doctor.js +57 -10
  82. package/dist/harness.js +47 -137
  83. package/dist/heal-bounds.js +7 -2
  84. package/dist/json-stream.js +57 -0
  85. package/dist/live-adapter-contract.js +138 -32
  86. package/dist/manifest.js +161 -1
  87. package/dist/mm-harness-cli.js +146 -73
  88. package/dist/paths.js +2 -5
  89. package/dist/recipe-security.js +178 -0
  90. package/dist/run-diagnostics.js +261 -0
  91. package/dist/run-recording.js +1 -1
  92. package/dist/runner.js +140 -9
  93. package/docs/CONTRIBUTING.md +136 -0
  94. package/docs/QA.md +184 -0
  95. package/docs/RECIPES.md +122 -0
  96. package/docs/SECURITY.md +88 -0
  97. package/library/README.md +7 -3
  98. package/library/actions/core/perps/_controller.mjs +43 -56
  99. package/library/actions/core/perps/assert_orders.mjs +6 -7
  100. package/library/actions/core/perps/assert_positions.mjs +6 -7
  101. package/library/actions/core/perps/close_orders.mjs +2 -0
  102. package/library/actions/core/perps/close_positions.mjs +2 -0
  103. package/library/actions/core/perps/ensure_orders.mjs +4 -2
  104. package/library/actions/core/perps/ensure_positions.mjs +4 -2
  105. package/library/actions/core/perps/place_order.mjs +7 -3
  106. package/library/actions/core/perps/read_account.mjs +2 -2
  107. package/library/actions/core/perps/read_orders.mjs +2 -1
  108. package/library/actions/core/perps/read_positions.mjs +2 -1
  109. package/library/actions/core/wallet/list_accounts.mjs +95 -0
  110. package/library/actions/extension/perps/assert_orders.mjs +2 -1
  111. package/library/actions/extension/perps/assert_positions.mjs +2 -1
  112. package/library/actions/extension/perps/perps.mjs +43 -14
  113. package/library/actions/extension/platform/cdp.mjs +1 -0
  114. package/library/actions/extension/wallet/list_accounts.mjs +41 -0
  115. package/library/actions/mobile/perps/assert_orders.mjs +2 -1
  116. package/library/actions/mobile/perps/assert_positions.mjs +2 -1
  117. package/library/actions/mobile/perps/perps.mjs +40 -12
  118. package/library/actions/mobile/wallet/list_accounts.mjs +37 -0
  119. package/library/library.json +1 -1
  120. package/library/manifests/core.action-manifest.json +1214 -396
  121. package/library/manifests/extension.action-manifest.json +1635 -728
  122. package/library/manifests/mobile.action-manifest.json +1740 -727
  123. package/library/recipes/app/lifecycle.android-smoke.mobile.recipe.json +63 -81
  124. package/library/recipes/perps/clean-market-testnet.core.recipe.json +44 -0
  125. package/library/recipes/perps/clean-market-testnet.recipe.json +49 -0
  126. package/library/recipes/perps/lifecycle.recipe.json +136 -180
  127. package/library/recipes/perps/order-lifecycle.core.recipe.json +71 -67
  128. package/library/recipes/perps/performance.background-resume.mobile.recipe.json +51 -67
  129. package/library/recipes/perps/performance.cold-start.mobile.recipe.json +51 -67
  130. package/library/recipes/perps/performance.mobile.recipe.json +37 -51
  131. package/library/recipes/perps/performance.warm-start.mobile.recipe.json +44 -59
  132. package/library/recipes/perps/read-markets.core.recipe.json +29 -31
  133. package/library/recipes/perps/smoke.core.recipe.json +29 -32
  134. package/library/recipes/perps/smoke.extension.recipe.json +41 -44
  135. package/library/recipes/perps/smoke.mobile.recipe.json +42 -44
  136. package/library/recipes/perps/trading-lifecycle.core.recipe.json +69 -65
  137. package/library/recipes/runner/action-validation.extension.recipe.json +312 -398
  138. package/library/recipes/runner/action-validation.mobile.recipe.json +316 -402
  139. package/library/recipes/runner/smoke.core.recipe.json +25 -0
  140. package/library/recipes/runner/smoke.extension.recipe.json +23 -24
  141. package/library/recipes/runner/smoke.mobile.recipe.json +23 -24
  142. package/library/recipes/wallet/smoke.extension.recipe.json +40 -0
  143. package/library/recipes/wallet/smoke.mobile.recipe.json +40 -0
  144. package/package.json +12 -5
  145. package/scripts/completions.sh +8 -11
  146. package/scripts/validate-human-outcomes.mjs +169 -0
  147. package/adapters/mobile/overlay/app/dev-tools/AgenticService/AgentStepHud.tsx.patch +0 -185
  148. package/adapters/mobile/overlay/app/dev-tools/AgenticService/AgenticService.ts.patch +0 -1662
  149. package/adapters/mobile/overlay/compat/README.md +0 -28
  150. package/adapters/mobile/overlay/compat/rn81-message-event-source.patch +0 -42
  151. package/adapters/shared/resolve-farmslot-ports-core.mjs +0 -3
  152. package/adapters/shared/resolve-farmslot-ports.mjs +0 -5
  153. package/adapters/shared/resolve-farmslot-ports.sh +0 -7
  154. package/dist/adapters/extension/harness-freshness.js +0 -39
  155. package/dist/commands/flows.js +0 -62
  156. package/docs/ADAPTER-SURFACE.md +0 -119
  157. package/docs/CHEATSHEET.md +0 -61
  158. package/docs/CLI-SPEC.md +0 -1098
  159. package/docs/CODE-MAP.md +0 -62
  160. package/docs/DEBUG-HANDOVER.md +0 -36
  161. package/docs/MENTAL-MODEL.md +0 -295
  162. package/docs/UX-PRINCIPLES.md +0 -64
  163. package/docs/architecture.md +0 -398
  164. package/docs/live-adapter-contract.md +0 -188
  165. package/docs/package-boundaries.md +0 -47
  166. package/docs/perps-flow-catalog.md +0 -235
  167. package/docs/recipe-libraries.md +0 -301
  168. package/docs/runtime-file-conventions.md +0 -36
  169. package/library/flows/perps.flows.json +0 -64
@@ -0,0 +1,462 @@
1
+ const bool = () => ({ kind: "boolean" });
2
+ const value = (choices) => ({ kind: "value", ...choices ? { choices } : {} });
3
+ const optionalValue = (choices) => ({ kind: "optional-value", ...choices ? { choices } : {} });
4
+ const options = (...groups) => Object.assign({}, ...groups);
5
+ const HELP = { "--help": bool(), "-h": bool() };
6
+ const JSON = { "--json": bool() };
7
+ const JSON_STREAM = { "--json-stream": bool() };
8
+ const TARGET = { "--target": value() };
9
+ const ADAPTER = { "--adapter": value(["mobile", "extension", "core"]) };
10
+ const MOBILE_PLATFORM = { "--platform": value(["ios", "android"]) };
11
+ const ADAPTER_OR_MOBILE_PLATFORM = { "--platform": value(["mobile", "extension", "core", "ios", "android"]) };
12
+ const DEVICE = { "--device": value() };
13
+ const RUNTIME_PORTS = {
14
+ "--cdp-port": value(),
15
+ "--watcher-port": value(),
16
+ "--metro-port": value(),
17
+ "--runtime-dir": value()
18
+ };
19
+ const RECIPE_RUNTIME = {
20
+ "--artifacts-dir": value(),
21
+ "--action-manifest": value(),
22
+ "--library": value(),
23
+ "--heal": value(["off", "infra-only", "auto"]),
24
+ "--slot": value(),
25
+ "--validation-runtime-dir": value(),
26
+ "--launch-existing-dist": bool(),
27
+ "--record-video": optionalValue(["full-run", "off"]),
28
+ "--source-trust": value(["trusted", "untrusted", "unknown"]),
29
+ "--source-kind": value(["bundled", "operator", "task", "recipe-file", "library", "custom-adapter"]),
30
+ "--source-name": value(),
31
+ "--source-digest": value(),
32
+ "--approve-plan": value()
33
+ };
34
+ const RUNWAY = {
35
+ "--branch": value(),
36
+ "--default-branch": value(),
37
+ "--run": value(),
38
+ "--cache-root": value(),
39
+ "--simulator": value(),
40
+ "--runtime": value(),
41
+ "--device-type": value(),
42
+ "--slot": value(),
43
+ "--watcher-port": value(),
44
+ "--runtime-dir": value(),
45
+ "--force": bool(),
46
+ "--resolve-only": bool()
47
+ };
48
+ const REMOVED_OPTION_REPLACEMENTS = {
49
+ "--project-root": "--target",
50
+ "--record": "--record-video"
51
+ };
52
+ const PUBLIC_COMMAND_CONTRACTS = {
53
+ status: {
54
+ aliases: ["health", "home"],
55
+ options: options(HELP, JSON, TARGET, DEVICE, {
56
+ "--fast": bool(),
57
+ "--all-devices": bool()
58
+ })
59
+ },
60
+ checklist: {
61
+ usage: "mm-harness checklist mark <task-dir> <step> [options]",
62
+ options: options(HELP, {
63
+ "--mark-last": bool(),
64
+ "--already-fixed": bool(),
65
+ "--no-self-review": bool(),
66
+ "--skip-learnings": bool(),
67
+ "--skip-checklist": bool(),
68
+ "--reason": value(),
69
+ "--checklist": value(),
70
+ "--signal": value()
71
+ }),
72
+ positionals: [
73
+ { label: "action", choices: ["mark"] },
74
+ { label: "task-dir" },
75
+ {
76
+ label: "step",
77
+ validate: (step) => {
78
+ if (["start", "complete", "no-change", "blocked"].includes(step)) return true;
79
+ const numeric = Number(step);
80
+ return Number.isInteger(numeric) && numeric >= 1;
81
+ },
82
+ validDescription: "start|complete|no-change|blocked|a positive numeric step"
83
+ }
84
+ ],
85
+ minimumPositionals: 3
86
+ },
87
+ actions: {
88
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, {
89
+ "--action": value(),
90
+ "--categories": bool(),
91
+ "--category": value(),
92
+ "--action-manifest": value(),
93
+ "--library": value(),
94
+ "--raw": bool()
95
+ }),
96
+ positionals: [{ label: "query" }],
97
+ noPositionalsWith: ["--action", "--categories", "--raw"]
98
+ },
99
+ stop: {
100
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, {
101
+ "--port": value(),
102
+ "--watcher-port": value()
103
+ })
104
+ },
105
+ call: {
106
+ options: options(HELP, JSON, TARGET, ADAPTER, MOBILE_PLATFORM, DEVICE, RUNTIME_PORTS, RECIPE_RUNTIME, {
107
+ "--list": bool(),
108
+ "--arg": value()
109
+ }),
110
+ positionals: [{ label: "action" }],
111
+ minimumPositionals: 0,
112
+ requiredUnless: ["--list"],
113
+ noPositionalsWith: ["--list"],
114
+ leadingPositionals: 1,
115
+ variadic: { label: "key=value", pattern: /^[^=\s]+=.*/u }
116
+ },
117
+ run: {
118
+ options: options(HELP, JSON, JSON_STREAM, TARGET, ADAPTER, MOBILE_PLATFORM, DEVICE, RUNTIME_PORTS, RECIPE_RUNTIME, {
119
+ "--list": bool(),
120
+ "--plan": bool(),
121
+ "--describe": bool()
122
+ }),
123
+ positionals: [{ label: "recipe" }],
124
+ minimumPositionals: 1,
125
+ requiredUnless: ["--list"],
126
+ noPositionalsWith: ["--list"],
127
+ variadic: { label: "key=value", pattern: /^[^=\s]+=.*/u }
128
+ },
129
+ last: {
130
+ options: options(HELP, JSON, TARGET, {
131
+ "--runtime-dir": value()
132
+ })
133
+ },
134
+ doctor: {
135
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, DEVICE, RUNTIME_PORTS, {
136
+ "--fix": bool(),
137
+ "--expect-live": bool(),
138
+ "--print-ready": bool(),
139
+ "--all-devices": bool(),
140
+ "--action-manifest": value(),
141
+ "--port": value()
142
+ })
143
+ },
144
+ check: {
145
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, {
146
+ "--base": value(),
147
+ "--profile": value(["fast", "full"]),
148
+ "--fix": bool(),
149
+ "--artifacts-dir": value()
150
+ }),
151
+ positionals: [{ label: "action", choices: ["diff"] }],
152
+ minimumPositionals: 1
153
+ },
154
+ "recipe-quality": {
155
+ options: options(HELP, JSON, {
156
+ "--input": value(),
157
+ "--output": value()
158
+ }),
159
+ positionals: [{ label: "action", choices: ["build"] }]
160
+ },
161
+ provision: {
162
+ options: options(HELP, JSON, TARGET, ADAPTER, DEVICE, RUNWAY, {
163
+ "--platform": value(["ios"]),
164
+ "--device-platform": value(["ios"])
165
+ }),
166
+ positionals: [
167
+ { label: "source", choices: ["runway", "ios"] },
168
+ { label: "platform", choices: ["ios"] }
169
+ ]
170
+ },
171
+ install: {
172
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, DEVICE, RUNWAY, {
173
+ "--runway": bool()
174
+ }),
175
+ allowPassthrough: true
176
+ },
177
+ verify: {
178
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, RUNTIME_PORTS),
179
+ allowPassthrough: true
180
+ },
181
+ cleanup: {
182
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM),
183
+ allowPassthrough: true
184
+ },
185
+ launch: {
186
+ options: options(HELP, JSON, JSON_STREAM, TARGET, ADAPTER, MOBILE_PLATFORM, DEVICE, RUNTIME_PORTS, {
187
+ "--build": bool(),
188
+ "--verify": bool(),
189
+ "--runway": bool(),
190
+ "--watch": bool(),
191
+ "--sidepanel": bool(),
192
+ "--fullscreen": bool(),
193
+ "--url": value(),
194
+ "--heal": value(["off", "infra-only", "auto"])
195
+ }),
196
+ positionals: [{ label: "platform", choices: ["ios", "android"] }]
197
+ },
198
+ logs: {
199
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, {
200
+ "--full": bool(),
201
+ "--window": bool(),
202
+ "--events": value(),
203
+ "--source": value()
204
+ })
205
+ },
206
+ debug: {
207
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, {
208
+ "--worker": bool(),
209
+ "--dev-menu": bool(),
210
+ "--no-open": bool()
211
+ })
212
+ },
213
+ update: {
214
+ options: options(HELP, JSON, { "--check": bool() })
215
+ },
216
+ fixtures: {
217
+ options: options(HELP, JSON, TARGET, ADAPTER, ADAPTER_OR_MOBILE_PLATFORM, DEVICE, {
218
+ "--from": value(),
219
+ "--dev": bool(),
220
+ "--force": bool(),
221
+ "--fixture": value(),
222
+ "--out": value(),
223
+ "--state": value(),
224
+ "--cdp-port": value(),
225
+ "--extension-dir": value(),
226
+ "--extension-id-file": value(),
227
+ "--action-manifest": value()
228
+ }),
229
+ positionals: [{ label: "action", choices: ["init", "sync", "set", "generate", "finalize"] }],
230
+ minimumPositionals: 0
231
+ }
232
+ };
233
+ function validatePublicInvocation(argv, examples = {}) {
234
+ const token = argv[0];
235
+ if (!token || token === "--help" || token === "-h" || token === "--version" || token === "-v") return null;
236
+ const resolved = resolveCommand(token);
237
+ if (!resolved) {
238
+ const names = publicCommandTokens();
239
+ const suggestion = closest(token, names);
240
+ if (token.startsWith("-")) {
241
+ return {
242
+ code: "CLI_UNKNOWN_OPTION",
243
+ command: "mm-harness",
244
+ message: `unknown top-level option '${token}'. Valid top-level options: --help, --version.`,
245
+ userAction: actionFor(token, closest(token, ["--help", "--version"]), "mm-harness --help")
246
+ };
247
+ }
248
+ return {
249
+ code: "CLI_UNKNOWN_COMMAND",
250
+ command: token,
251
+ message: `unknown command '${token}'. Valid commands: ${Object.keys(PUBLIC_COMMAND_CONTRACTS).join(", ")}.`,
252
+ userAction: actionFor(token, suggestion, suggestion ? exampleFor(resolveCommand(suggestion)?.name, examples) : "mm-harness --help")
253
+ };
254
+ }
255
+ const { name, contract } = resolved;
256
+ const tokens = argv.slice(1);
257
+ const positionals = [];
258
+ const seenOptions = /* @__PURE__ */ new Set();
259
+ const validOptions = Object.keys(contract.options);
260
+ for (let index = 0; index < tokens.length; index += 1) {
261
+ const argument = tokens[index] ?? "";
262
+ if (argument === "--") {
263
+ if (!contract.allowPassthrough) {
264
+ return usageFailure("CLI_UNEXPECTED_PASSTHROUGH", name, "this command does not accept `--` passthrough.", contract, examples);
265
+ }
266
+ break;
267
+ }
268
+ if (!argument.startsWith("-") || argument === "-") {
269
+ positionals.push(argument);
270
+ continue;
271
+ }
272
+ const equals = argument.indexOf("=");
273
+ const optionName = equals === -1 ? argument : argument.slice(0, equals);
274
+ const inlineValue = equals === -1 ? void 0 : argument.slice(equals + 1);
275
+ const spec = contract.options[optionName];
276
+ if (!spec) {
277
+ const suggestion = REMOVED_OPTION_REPLACEMENTS[optionName] ?? closest(optionName, validOptions);
278
+ return {
279
+ code: "CLI_UNKNOWN_OPTION",
280
+ command: name,
281
+ message: `unknown option '${optionName}'.`,
282
+ userAction: actionFor(optionName, suggestion, exampleFor(name, examples))
283
+ };
284
+ }
285
+ seenOptions.add(optionName);
286
+ if (spec.kind === "boolean") {
287
+ if (inlineValue !== void 0) {
288
+ return usageFailure("CLI_INVALID_OPTION_VALUE", name, `${optionName} is a flag and does not take a value.`, contract, examples);
289
+ }
290
+ continue;
291
+ }
292
+ if (spec.kind === "optional-value" && inlineValue === void 0) continue;
293
+ const optionValue = inlineValue ?? tokens[index + 1];
294
+ const nextTokenIsOption = inlineValue === void 0 && (optionValue === "-h" || optionValue === "--" || optionValue?.startsWith("--"));
295
+ if (optionValue === void 0 || spec.kind === "value" && optionValue === "" || nextTokenIsOption) {
296
+ return usageFailure("CLI_MISSING_OPTION_VALUE", name, `${optionName} requires a value.`, contract, examples);
297
+ }
298
+ if (inlineValue === void 0) index += 1;
299
+ if (spec.choices && !spec.choices.includes(optionValue)) {
300
+ const suggestion = closest(optionValue, spec.choices);
301
+ return {
302
+ code: "CLI_INVALID_OPTION_VALUE",
303
+ command: name,
304
+ message: `${optionName} must be ${formatChoiceList(spec.choices)}; received '${optionValue}'.`,
305
+ userAction: actionFor(optionValue, suggestion, exampleFor(name, examples))
306
+ };
307
+ }
308
+ }
309
+ if (seenOptions.has("--help") || seenOptions.has("-h")) return null;
310
+ if (contract.leadingPositionals && !contract.requiredUnless?.some((option) => seenOptions.has(option)) && tokens.slice(0, contract.leadingPositionals).some((argument) => !argument || argument.startsWith("-"))) {
311
+ const missing = contract.positionals?.[0]?.label ?? "argument";
312
+ return missingPositionalFailure(name, `${name} requires <${missing}> first.`, contract, examples);
313
+ }
314
+ const forbiddingOption = contract.noPositionalsWith?.find((option) => seenOptions.has(option));
315
+ if (forbiddingOption && positionals.length > 0) {
316
+ return usageFailure(
317
+ "CLI_EXCESS_POSITIONAL",
318
+ name,
319
+ `${forbiddingOption} does not accept positional '${positionals[0]}'.`,
320
+ contract,
321
+ examples
322
+ );
323
+ }
324
+ const minimum = contract.requiredUnless?.some((option) => seenOptions.has(option)) ? 0 : contract.minimumPositionals ?? 0;
325
+ if (positionals.length < minimum) {
326
+ const missingSpec = contract.positionals?.[positionals.length];
327
+ const missing = missingSpec?.label ?? "argument";
328
+ const valid = missingSpec?.choices?.length ? ` Choose one: ${missingSpec.choices.join("|")}.` : "";
329
+ const usage = contract.usage ? ` Usage: ${contract.usage}.` : "";
330
+ return missingPositionalFailure(name, `missing required <${missing}>.${valid}${usage}`, contract, examples);
331
+ }
332
+ const fixed = contract.positionals ?? [];
333
+ for (let index = 0; index < Math.min(positionals.length, fixed.length); index += 1) {
334
+ const invalid = invalidPositional(positionals[index] ?? "", fixed[index]);
335
+ if (invalid) return positionalFailure(name, positionals[index] ?? "", fixed[index], contract, examples);
336
+ }
337
+ if (positionals.length > fixed.length && !contract.variadic) {
338
+ const unexpected = positionals[fixed.length] ?? "";
339
+ return usageFailure("CLI_EXCESS_POSITIONAL", name, `unexpected positional '${unexpected}'; this command accepts ${formatPositionals(contract)}.`, contract, examples);
340
+ }
341
+ if (contract.variadic) {
342
+ for (const positional of positionals.slice(fixed.length)) {
343
+ if (invalidPositional(positional, contract.variadic)) {
344
+ return positionalFailure(name, positional, contract.variadic, contract, examples);
345
+ }
346
+ }
347
+ }
348
+ return null;
349
+ }
350
+ function publicCommandTokens() {
351
+ const tokens = [];
352
+ for (const [name, contract] of Object.entries(PUBLIC_COMMAND_CONTRACTS)) {
353
+ tokens.push(name, ...contract.aliases ?? []);
354
+ }
355
+ return tokens;
356
+ }
357
+ function publicCommandNames() {
358
+ return Object.keys(PUBLIC_COMMAND_CONTRACTS);
359
+ }
360
+ function resolveCommand(token) {
361
+ const direct = PUBLIC_COMMAND_CONTRACTS[token];
362
+ if (direct) return { name: token, contract: direct };
363
+ for (const [name, contract] of Object.entries(PUBLIC_COMMAND_CONTRACTS)) {
364
+ if (contract.aliases?.includes(token)) return { name, contract };
365
+ }
366
+ return null;
367
+ }
368
+ function usageFailure(code, command, message, _contract, examples) {
369
+ return {
370
+ code,
371
+ command,
372
+ message,
373
+ userAction: `Try: ${exampleFor(command, examples)}. Inspect options: mm-harness ${command} --help`
374
+ };
375
+ }
376
+ function missingPositionalFailure(command, message, contract, examples) {
377
+ if (command === "run") {
378
+ return {
379
+ code: "CLI_MISSING_POSITIONAL",
380
+ command,
381
+ message,
382
+ userAction: `List recipes: mm-harness run --list
383
+ Example: ${exampleFor(command, examples)}
384
+ More: mm-harness run --help`
385
+ };
386
+ }
387
+ if (command === "call") {
388
+ return {
389
+ code: "CLI_MISSING_POSITIONAL",
390
+ command,
391
+ message,
392
+ userAction: `List actions: mm-harness call --list
393
+ Example: ${exampleFor(command, examples)}
394
+ More: mm-harness call --help`
395
+ };
396
+ }
397
+ return usageFailure("CLI_MISSING_POSITIONAL", command, message, contract, examples);
398
+ }
399
+ function positionalFailure(command, received, spec, _contract, examples) {
400
+ const valid = spec.validDescription ?? (spec.choices ? spec.choices.join(", ") : spec.pattern || spec.validate ? `a valid <${spec.label}>` : `<${spec.label}>`);
401
+ const suggestion = spec.choices ? closest(received, spec.choices) : void 0;
402
+ return {
403
+ code: "CLI_INVALID_POSITIONAL",
404
+ command,
405
+ message: `invalid <${spec.label}> '${received}'. Valid values: ${valid}.`,
406
+ userAction: actionFor(received, suggestion, exampleFor(command, examples))
407
+ };
408
+ }
409
+ function invalidPositional(value2, spec) {
410
+ if (!spec) return true;
411
+ if (spec.choices && !spec.choices.includes(value2)) return true;
412
+ if (spec.validate && !spec.validate(value2)) return true;
413
+ return Boolean(spec.pattern && !spec.pattern.test(value2));
414
+ }
415
+ function formatChoiceList(choices) {
416
+ if (choices.length === 1) return choices[0] ?? "";
417
+ if (choices.length === 2) return `${choices[0]} or ${choices[1]}`;
418
+ return `${choices.slice(0, -1).join(", ")}, or ${choices[choices.length - 1]}`;
419
+ }
420
+ function formatPositionals(contract) {
421
+ const fixed = (contract.positionals ?? []).map((spec) => `<${spec.label}>`);
422
+ if (contract.variadic) fixed.push(`[<${contract.variadic.label}> ...]`);
423
+ return fixed.length > 0 ? fixed.join(" ") : "no positionals";
424
+ }
425
+ function actionFor(received, suggestion, example) {
426
+ return `${suggestion ? `Did you mean '${suggestion}' instead of '${received}'? ` : ""}Try: ${example}`;
427
+ }
428
+ function exampleFor(command, examples) {
429
+ return command ? examples[command] ?? `mm-harness ${command} --help` : "mm-harness --help";
430
+ }
431
+ function closest(input, candidates) {
432
+ if (candidates.length === 0) return void 0;
433
+ const ranked = candidates.map((candidate) => ({ candidate, distance: levenshtein(input, candidate) })).sort((left, right) => left.distance - right.distance || left.candidate.localeCompare(right.candidate));
434
+ const best = ranked[0];
435
+ if (!best) return void 0;
436
+ const threshold = Math.max(2, Math.floor(Math.max(input.length, best.candidate.length) / 3));
437
+ return best.distance <= threshold ? best.candidate : void 0;
438
+ }
439
+ function levenshtein(left, right) {
440
+ let twoBack;
441
+ let prior = Array.from({ length: right.length + 1 }, (_, index) => index);
442
+ for (let leftIndex = 1; leftIndex <= left.length; leftIndex += 1) {
443
+ const current = [leftIndex];
444
+ for (let rightIndex = 1; rightIndex <= right.length; rightIndex += 1) {
445
+ const substitution = prior[rightIndex - 1] + (left[leftIndex - 1] === right[rightIndex - 1] ? 0 : 1);
446
+ current[rightIndex] = Math.min((current[rightIndex - 1] ?? 0) + 1, (prior[rightIndex] ?? 0) + 1, substitution);
447
+ if (twoBack && leftIndex > 1 && rightIndex > 1 && left[leftIndex - 1] === right[rightIndex - 2] && left[leftIndex - 2] === right[rightIndex - 1]) {
448
+ current[rightIndex] = Math.min(current[rightIndex] ?? 0, (twoBack[rightIndex - 2] ?? 0) + 1);
449
+ }
450
+ }
451
+ twoBack = prior;
452
+ prior = current;
453
+ }
454
+ return prior[right.length] ?? right.length;
455
+ }
456
+ export {
457
+ PUBLIC_COMMAND_CONTRACTS,
458
+ closest,
459
+ publicCommandNames,
460
+ publicCommandTokens,
461
+ validatePublicInvocation
462
+ };