@deeeed/metamask-harness 0.79.0 → 0.81.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 (252) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/README.md +1 -1
  3. package/adapters/extension/ensure-browser.sh +2 -2
  4. package/adapters/extension/inject.mjs +0 -1
  5. package/adapters/extension/launch-browser.cjs +39 -613
  6. package/adapters/extension/readiness.mjs +2 -5
  7. package/adapters/extension/reattach.sh +2 -2
  8. package/adapters/extension/stamp-runtime-title.cjs +1 -1
  9. package/adapters/manifest.json +16 -64
  10. package/adapters/mobile/bridge-runtime/cdp-bridge.cjs +57 -1358
  11. package/adapters/mobile/bridge-runtime/teaching.cjs +20 -0
  12. package/adapters/mobile/inject.sh +16 -0
  13. package/adapters/mobile/launch-console-forwarder.cjs +1 -2
  14. package/adapters/mobile/open-device.sh +2 -2
  15. package/adapters/mobile/reload-app.mjs +4 -4
  16. package/adapters/mobile/reset-app-data.sh +1 -1
  17. package/adapters/mobile/start-console-forwarder.sh +3 -2
  18. package/adapters/mobile/start-metro.sh +17 -12
  19. package/adapters/mobile/wait-for-bridge.cjs +2 -2
  20. package/adapters/shared/harness-source-fingerprint.mjs +13 -8
  21. package/adapters/shared/open-debug.mjs +2 -2
  22. package/adapters/shared/sync-wallet-fixture.sh +8 -5
  23. package/adapters/terminal/cleanup.mjs +2 -1
  24. package/adapters/terminal/inject.mjs +2 -1
  25. package/adapters/terminal/launch.mjs +17 -13
  26. package/adapters/terminal/lib/mainnet-guard.mjs +9 -0
  27. package/adapters/terminal/lib/readiness.mjs +20 -12
  28. package/adapters/terminal/policy.mjs +35 -0
  29. package/adapters/terminal/verify.mjs +10 -3
  30. package/adapters/terminal/wallet-host.mjs +43 -225
  31. package/bin/mm-harness +9 -28
  32. package/dist/adapters/checkout-files.js +16 -0
  33. package/dist/adapters/console-capture.js +14 -1
  34. package/dist/adapters/core/actions.js +8 -4
  35. package/dist/adapters/core/overlay.js +1 -1
  36. package/dist/adapters/core/readiness.js +28 -1
  37. package/dist/adapters/core/run.js +2 -2
  38. package/dist/adapters/core/surface.js +16 -1
  39. package/dist/adapters/extension/actions.js +6 -2
  40. package/dist/adapters/extension/browser-pid.js +56 -0
  41. package/dist/adapters/extension/console-capture.js +1 -1
  42. package/dist/adapters/extension/doctor-checks.js +1 -1
  43. package/dist/adapters/extension/ensure-ready.js +5 -4
  44. package/dist/adapters/extension/extension-id.js +1 -1
  45. package/dist/adapters/extension/feature-flags.js +1 -1
  46. package/dist/adapters/extension/fixtures.js +1 -2
  47. package/dist/adapters/extension/launch.js +4 -5
  48. package/dist/adapters/extension/launched-browser.js +1 -2
  49. package/dist/adapters/extension/network-observer.js +14 -1
  50. package/dist/adapters/extension/overlay.js +1 -1
  51. package/dist/adapters/extension/product-config.js +1 -1
  52. package/dist/adapters/extension/readiness.js +1 -1
  53. package/dist/adapters/extension/reload.js +1 -1
  54. package/dist/adapters/extension/run.js +135 -14
  55. package/dist/adapters/extension/runtime-decision.js +2 -1
  56. package/dist/adapters/extension/runtime.js +9 -5
  57. package/dist/adapters/extension/surface.js +15 -2
  58. package/dist/adapters/extension/verify.js +2 -2
  59. package/dist/adapters/harness-freshness.js +2 -1
  60. package/dist/adapters/mobile/actions.js +4 -1
  61. package/dist/adapters/mobile/android-gfxinfo-performance.js +1 -3
  62. package/dist/adapters/mobile/app-identity.js +10 -0
  63. package/dist/adapters/mobile/doctor-checks.js +1 -1
  64. package/dist/adapters/mobile/fixtures.js +1 -1
  65. package/dist/adapters/mobile/launch.js +27 -28
  66. package/dist/adapters/mobile/lifecycle.js +1 -1
  67. package/dist/adapters/mobile/metro-env.js +9 -59
  68. package/dist/adapters/mobile/network-observer.js +68 -0
  69. package/dist/adapters/mobile/performance-observer.js +2 -4
  70. package/dist/adapters/mobile/prepare-target.js +1 -1
  71. package/dist/adapters/mobile/prepare.js +3 -4
  72. package/dist/adapters/mobile/provision.js +1 -1
  73. package/dist/adapters/mobile/readiness.js +1 -1
  74. package/dist/adapters/mobile/recording-target.js +15 -0
  75. package/dist/adapters/mobile/release-artifact-state.js +1 -1
  76. package/dist/adapters/mobile/reload.js +1 -1
  77. package/dist/adapters/mobile/run.js +39 -15
  78. package/dist/adapters/mobile/runtime-decision.js +2 -4
  79. package/dist/adapters/mobile/source-freshness.js +22 -126
  80. package/dist/adapters/mobile/surface.js +26 -3
  81. package/dist/adapters/mobile/ui.js +16 -5
  82. package/dist/adapters/mobile/verify.js +2 -3
  83. package/dist/adapters/performance/cdp-trace.js +1 -1
  84. package/dist/adapters/performance/device-platform.js +1 -1
  85. package/dist/adapters/perps-order-inputs.js +36 -0
  86. package/dist/adapters/proof-failure.js +11 -0
  87. package/dist/adapters/slot-ports.js +1 -1
  88. package/dist/adapters/surface.js +1 -0
  89. package/dist/adapters/terminal/actions.js +4 -1
  90. package/dist/adapters/terminal/console-capture.js +1 -1
  91. package/dist/adapters/terminal/doctor-checks.js +2 -0
  92. package/dist/adapters/terminal/launch.js +4 -1
  93. package/dist/adapters/terminal/overlay.js +8 -1
  94. package/dist/adapters/terminal/run.js +33 -5
  95. package/dist/adapters/terminal/surface.js +9 -1
  96. package/dist/adapters.js +27 -7
  97. package/dist/advisor-policy.js +1 -1
  98. package/dist/checkout-view.js +1 -2
  99. package/dist/cli.js +59 -21
  100. package/dist/command-contract.js +1 -26
  101. package/dist/commands/check.js +33 -10
  102. package/dist/commands/checklist.js +1 -1
  103. package/dist/commands/completion-candidates.js +6 -14
  104. package/dist/commands/config.js +2 -2
  105. package/dist/commands/core-readiness.js +12 -7
  106. package/dist/commands/debug.js +3 -3
  107. package/dist/commands/device-target.js +5 -5
  108. package/dist/commands/doctor.js +2 -14
  109. package/dist/commands/domain.js +2 -2
  110. package/dist/commands/ensure-ready.js +1 -7
  111. package/dist/commands/fixtures.js +5 -6
  112. package/dist/commands/help.js +3 -4
  113. package/dist/commands/launch/device.js +10 -17
  114. package/dist/commands/launch/extension.js +2 -8
  115. package/dist/commands/launch/mobile.js +1 -1
  116. package/dist/commands/logs.js +3 -3
  117. package/dist/commands/mobile-device-view.js +4 -2
  118. package/dist/commands/overlay.js +110 -0
  119. package/dist/commands/pin.js +1 -1
  120. package/dist/commands/pr-body.js +2 -2
  121. package/dist/commands/prepare.js +4 -9
  122. package/dist/commands/provision.js +3 -12
  123. package/dist/commands/recipe-advice.js +1 -2
  124. package/dist/commands/recipe-quality.js +2 -3
  125. package/dist/commands/resolve-extension.js +1 -7
  126. package/dist/commands/review.js +4 -4
  127. package/dist/commands/runtime-decision.js +1 -6
  128. package/dist/commands/runtime-health.js +1 -7
  129. package/dist/commands/runtime-launch.js +1 -13
  130. package/dist/commands/self-test.js +5 -8
  131. package/dist/commands/status-watch.js +1 -2
  132. package/dist/commands/status.js +2 -5
  133. package/dist/commands/task-init.js +2 -3
  134. package/dist/commands/tutorial.js +2 -2
  135. package/dist/commands/update.js +1 -1
  136. package/dist/completions-cache.js +1 -1
  137. package/dist/console-classifier.js +19 -0
  138. package/dist/doctor.js +2 -3
  139. package/dist/engine.js +57 -0
  140. package/dist/funding-execution-context.js +96 -1
  141. package/dist/host.js +32 -0
  142. package/dist/index.js +1 -6
  143. package/dist/live-tsx.js +49 -0
  144. package/dist/mm-harness-cli.js +4 -6
  145. package/dist/paths.js +34 -64
  146. package/dist/performance-observation.js +3 -5
  147. package/dist/progress.js +1 -1
  148. package/dist/recipe-assistance-context.js +6 -9
  149. package/dist/recipe-security.js +2 -59
  150. package/dist/review/knowledge.js +2 -2
  151. package/dist/runner.js +7 -4
  152. package/dist/runtime-context.js +1 -1
  153. package/dist/task-view.js +2 -3
  154. package/docs/CONTRIBUTING.md +5 -3
  155. package/docs/RECIPES.md +31 -3
  156. package/docs/cli-contract.md +2 -2
  157. package/docs/feature-guard.md +90 -0
  158. package/library/README.md +3 -1
  159. package/library/actions/core/perps/_controller.mjs +153 -134
  160. package/library/actions/core/perps/_rewards.mjs +186 -0
  161. package/library/actions/core/perps/close_positions.mjs +1 -1
  162. package/library/actions/core/perps/place_order.mjs +167 -45
  163. package/library/actions/core/perps/quote_fees.mjs +61 -0
  164. package/library/actions/core/wallet/list_accounts.mjs +14 -57
  165. package/library/actions/extension/feature_flags/state.mjs +1 -1
  166. package/library/actions/extension/perps/close_visible_position.mjs +5 -0
  167. package/library/actions/extension/perps/edit_margin.mjs +4 -0
  168. package/library/actions/extension/perps/mutation-receipt.mjs +95 -3
  169. package/library/actions/extension/perps/perps.mjs +320 -3
  170. package/library/actions/extension/perps/place_order.mjs +3 -1
  171. package/library/actions/extension/perps/update_position_tpsl.mjs +4 -0
  172. package/library/actions/extension/platform/cdp.mjs +73 -11
  173. package/library/actions/harness-exports.mjs +1 -6
  174. package/library/actions/mobile/app/network-control.mjs +6 -8
  175. package/library/actions/mobile/perps/perps.mjs +216 -11
  176. package/library/actions/mobile/perps/place_order.mjs +3 -1
  177. package/library/actions/mobile/perps/prepare_local_snapshot_endpoint.mjs +1 -1
  178. package/library/actions/mobile/platform/bridge.mjs +8 -8
  179. package/library/actions/mobile/platform/native-session-name.mjs +21 -1
  180. package/library/actions/mobile/platform/native-session.mjs +3 -4
  181. package/library/actions/mobile/platform/observe-ui.mjs +12 -6
  182. package/library/actions/mobile/ui/native-navigation.mjs +4 -3
  183. package/library/actions/mobile/wallet/native-ui.mjs +3 -2
  184. package/library/actions/shared/analytics/_adapter.mjs +1 -1
  185. package/library/actions/shared/console-findings.mjs +1 -1
  186. package/library/actions/shared/perps/order-policy.mjs +177 -0
  187. package/library/actions/shared/wallet/fixture-accounts.mjs +84 -0
  188. package/library/actions/terminal/app/launch.mjs +3 -2
  189. package/library/actions/terminal/platform/hud.mjs +2 -1
  190. package/library/actions/terminal/platform/page.mjs +2 -1
  191. package/library/actions/terminal/platform/runtime.mjs +2 -1
  192. package/library/actions/terminal/wallet/_signature-log.mjs +40 -161
  193. package/library/actions/terminal/wallet/assert_signatures.mjs +6 -12
  194. package/library/actions/terminal/wallet/read_signatures.mjs +10 -9
  195. package/library/manifests/core.action-manifest.json +87 -13
  196. package/library/manifests/extension.action-manifest.json +4 -0
  197. package/library/manifests/mobile.action-manifest.json +12 -0
  198. package/package.json +8 -9
  199. package/site/assets/help-recipes.json +1 -0
  200. package/adapters/extension/lib/slot-title.cjs +0 -292
  201. package/adapters/mobile/bridge-runtime/console-forwarder.cjs +0 -472
  202. package/adapters/mobile/bridge-runtime/lib/bridge-errors.cjs +0 -125
  203. package/adapters/mobile/bridge-runtime/lib/cdp-broker.cjs +0 -1055
  204. package/adapters/mobile/bridge-runtime/lib/cdp-eval.cjs +0 -110
  205. package/adapters/mobile/bridge-runtime/lib/config.cjs +0 -61
  206. package/adapters/mobile/bridge-runtime/lib/console-format.cjs +0 -53
  207. package/adapters/mobile/bridge-runtime/lib/devtools-proxy.cjs +0 -177
  208. package/adapters/mobile/bridge-runtime/lib/issue-capture.cjs +0 -446
  209. package/adapters/mobile/bridge-runtime/lib/match-bridge-target.cjs +0 -84
  210. package/adapters/mobile/bridge-runtime/lib/target-discovery.cjs +0 -403
  211. package/adapters/mobile/bridge-runtime/lib/ws-client.cjs +0 -116
  212. package/adapters/mobile/coalesce-metro-log.cjs +0 -24
  213. package/adapters/mobile/launch-metro.cjs +0 -77
  214. package/adapters/mobile/metro-config.cjs +0 -98
  215. package/adapters/mobile/metro-log-generation.cjs +0 -106
  216. package/adapters/terminal/lib/origin.mjs +0 -30
  217. package/adapters/terminal/lib/page-script.mjs +0 -246
  218. package/adapters/terminal/lib/strict-wallet.mjs +0 -138
  219. package/dist/adapters/mobile/frame-metrics.js +0 -118
  220. package/dist/adapters/mobile/video-recorder.js +0 -289
  221. package/dist/artifact-files.js +0 -92
  222. package/dist/checkout-lock.js +0 -149
  223. package/dist/cli-color.js +0 -98
  224. package/dist/command-journal.js +0 -308
  225. package/dist/commands/call.js +0 -685
  226. package/dist/commands/last.js +0 -60
  227. package/dist/commands/launch/index.js +0 -109
  228. package/dist/commands/list-executables.js +0 -247
  229. package/dist/commands/manifest.js +0 -488
  230. package/dist/commands/parse-args.js +0 -279
  231. package/dist/commands/reload.js +0 -41
  232. package/dist/commands/run-engine.js +0 -1340
  233. package/dist/commands/run-report.js +0 -241
  234. package/dist/commands/run.js +0 -751
  235. package/dist/commands/shared.js +0 -323
  236. package/dist/commands/stop.js +0 -60
  237. package/dist/devices.js +0 -100
  238. package/dist/discovery.js +0 -15
  239. package/dist/execution-provenance.js +0 -332
  240. package/dist/harness.js +0 -451
  241. package/dist/heal-bounds.js +0 -137
  242. package/dist/json-stream.js +0 -60
  243. package/dist/leaf-invoke.js +0 -19
  244. package/dist/library-provenance.js +0 -33
  245. package/dist/live-adapter-contract.js +0 -380
  246. package/dist/metamask-action-validation.js +0 -68
  247. package/dist/network-observation.js +0 -290
  248. package/dist/recording-target.js +0 -120
  249. package/dist/run-diagnostics.js +0 -364
  250. package/dist/run-recording.js +0 -481
  251. package/dist/runtime-proof.js +0 -90
  252. package/library/actions/mobile/platform/tool-paths.mjs +0 -123
@@ -1,33 +0,0 @@
1
- import { execFile } from "node:child_process";
2
- import { promisify } from "node:util";
3
- const execFileAsync = promisify(execFile);
4
- async function gitLibraryProvenance(root) {
5
- try {
6
- const { stdout: tracked } = await execFileAsync("git", ["-C", root, "ls-files", "--", "."], {
7
- encoding: "utf8",
8
- timeout: 5e3
9
- });
10
- if (!tracked.trim()) return {};
11
- const [{ stdout: revision }, { stdout: status }] = await Promise.all([
12
- execFileAsync("git", ["-C", root, "rev-parse", "HEAD"], {
13
- encoding: "utf8",
14
- timeout: 5e3
15
- }),
16
- execFileAsync(
17
- "git",
18
- ["-C", root, "status", "--porcelain=v1", "--untracked-files=normal", "--", "."],
19
- {
20
- encoding: "utf8",
21
- timeout: 5e3
22
- }
23
- )
24
- ]);
25
- const trimmedRevision = revision.trim();
26
- return trimmedRevision ? { revision: trimmedRevision, dirty: status.trim().length > 0 } : {};
27
- } catch {
28
- return {};
29
- }
30
- }
31
- export {
32
- gitLibraryProvenance
33
- };
@@ -1,380 +0,0 @@
1
- import { recordCommandOutput } from "./command-journal.js";
2
- import { trackCheckoutChild } from "./checkout-lock.js";
3
- import { createHash } from "node:crypto";
4
- import { access, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises";
5
- import { existsSync, readFileSync } from "node:fs";
6
- import os from "node:os";
7
- import path from "node:path";
8
- import { spawn } from "node:child_process";
9
- import { pathToFileURL } from "node:url";
10
- import { build } from "esbuild";
11
- import { init, parse } from "es-module-lexer";
12
- import { resolveRequiredLocalProtocolRoot, runnerDir } from "./paths.js";
13
- import { adapterRegistry } from "./adapters/registry.js";
14
- import { getAdapterSurface } from "./adapters/surface.js";
15
- import { serializeTrustedMutationContext } from "./funding-execution-context.js";
16
- function actionFileStem(action) {
17
- return String(action).replace(/[^a-zA-Z0-9._-]/g, "_");
18
- }
19
- function actionParts(action) {
20
- const parts = String(action).split(".").filter(Boolean);
21
- if (parts.length >= 3 && parts[0] === "metamask") {
22
- return { family: parts[1], localName: parts.slice(2).join(".") };
23
- }
24
- if (parts.length >= 2) {
25
- return { family: parts[0], localName: parts.slice(1).join(".") };
26
- }
27
- return { family: "actions", localName: String(action) };
28
- }
29
- function candidateStems(action) {
30
- const stem = actionFileStem(action);
31
- const { localName } = actionParts(action);
32
- const localStem = actionFileStem(localName);
33
- const stems = [stem];
34
- if (localStem && localStem !== stem) stems.push(localStem);
35
- return stems;
36
- }
37
- function candidateFamilies(action) {
38
- const { family } = actionParts(action);
39
- const families = [family];
40
- return [...new Set(families)];
41
- }
42
- function candidatePaths(platform, action, runtimeEnv = process.env) {
43
- const stems = candidateStems(action);
44
- const families = candidateFamilies(action);
45
- const declaredRoot = declaredActionRoot(
46
- runtimeEnv.METAMASK_RECIPE_ACTION_SOURCE_MAP ?? process.env.METAMASK_RECIPE_ACTION_SOURCE_MAP,
47
- action
48
- );
49
- const roots = [...new Set((declaredRoot ? [declaredRoot] : [
50
- runtimeEnv.METAMASK_RECIPE_LIVE_ADAPTER_DIR ?? process.env.METAMASK_RECIPE_LIVE_ADAPTER_DIR,
51
- path.join(runnerDir, "library/actions")
52
- ]).filter((entry) => Boolean(entry)))];
53
- const files = [];
54
- for (const root of roots) {
55
- for (const family of families) {
56
- for (const candidateStem of stems) {
57
- pushCandidateFiles(files, root, platform, family, candidateStem);
58
- }
59
- pushDomainDispatcherFiles(files, root, platform, family);
60
- }
61
- }
62
- return files;
63
- }
64
- function declaredActionRoot(raw, action) {
65
- if (!raw) return void 0;
66
- try {
67
- const parsed = JSON.parse(raw);
68
- const root = parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed[action] : void 0;
69
- return typeof root === "string" && root.length > 0 ? root : void 0;
70
- } catch {
71
- return void 0;
72
- }
73
- }
74
- function pushCandidateFiles(files, root, platform, family, stem) {
75
- files.push(path.join(root, platform, family, `${stem}.mjs`));
76
- files.push(path.join(root, "shared", family, `${stem}.mjs`));
77
- }
78
- function pushDomainDispatcherFiles(files, root, platform, family) {
79
- files.push(path.join(root, platform, family, `${family}.mjs`));
80
- files.push(path.join(root, "shared", family, `${family}.mjs`));
81
- }
82
- async function firstExecutablePath(paths) {
83
- for (const file of paths) {
84
- try {
85
- await access(file);
86
- return file;
87
- } catch (error) {
88
- if (error.code !== "ENOENT") throw error;
89
- }
90
- }
91
- return null;
92
- }
93
- function runProcess(command, args, options) {
94
- return new Promise((resolve, reject) => {
95
- let settled = false;
96
- const child = spawn(command, args, {
97
- cwd: options.cwd,
98
- env: options.env,
99
- stdio: [options.stdin == null ? "ignore" : "pipe", "pipe", "pipe"]
100
- });
101
- if (child.pid) trackCheckoutChild(options.cwd, child.pid);
102
- if (options.stdin != null) {
103
- child.stdin.on("error", (error) => {
104
- if (error.code === "EPIPE" || settled) return;
105
- settled = true;
106
- reject(error);
107
- });
108
- child.stdin.end(options.stdin);
109
- }
110
- let stdout = "";
111
- let stderr = "";
112
- child.stdout.on("data", (chunk) => {
113
- stdout += chunk;
114
- recordCommandOutput(chunk);
115
- });
116
- child.stderr.on("data", (chunk) => {
117
- stderr += chunk;
118
- recordCommandOutput(chunk);
119
- });
120
- const timeout = options.timeoutMs ? setTimeout(() => {
121
- if (settled) return;
122
- settled = true;
123
- child.kill("SIGTERM");
124
- const killTimer = setTimeout(() => child.kill("SIGKILL"), 1e3);
125
- child.once("close", () => clearTimeout(killTimer));
126
- resolve({ exitCode: null, stdout, stderr, timedOut: true });
127
- }, options.timeoutMs) : void 0;
128
- child.on("error", (error) => {
129
- if (settled) return;
130
- settled = true;
131
- if (timeout) clearTimeout(timeout);
132
- reject(error);
133
- });
134
- child.on("close", (exitCode) => {
135
- if (settled) return;
136
- settled = true;
137
- if (timeout) clearTimeout(timeout);
138
- resolve({ exitCode, stdout, stderr, timedOut: false });
139
- });
140
- });
141
- }
142
- async function prepareLiveAdapterScript({
143
- platform,
144
- action,
145
- implementationRoot,
146
- allowRuntimeImports = false
147
- }) {
148
- const entryPath = await firstExecutablePath(
149
- candidatePaths(platform, action, {
150
- METAMASK_RECIPE_ACTION_SOURCE_MAP: JSON.stringify({ [action]: implementationRoot })
151
- })
152
- );
153
- if (!entryPath) return null;
154
- const rootReal = await realpath(implementationRoot);
155
- const entryReal = await realpath(entryPath);
156
- if (!isPathWithin(rootReal, entryReal)) {
157
- throw new Error(`Live adapter ${entryPath} resolves outside ${implementationRoot}.`);
158
- }
159
- const result = await build({
160
- entryPoints: [entryReal],
161
- bundle: true,
162
- write: false,
163
- platform: "node",
164
- format: "esm",
165
- target: `node${process.versions.node.split(".")[0]}`,
166
- legalComments: "none",
167
- sourcemap: false,
168
- banner: {
169
- js: `import { createRequire as __mmCreateRequire } from 'node:module';const require = __mmCreateRequire(import.meta.url);process.argv[1] = ${JSON.stringify(entryReal)};`
170
- },
171
- plugins: [bindImportMetaUrls()],
172
- logLevel: "silent"
173
- });
174
- const output = result.outputFiles[0];
175
- if (!output) throw new Error(`Bundling live adapter ${entryReal} produced no executable output.`);
176
- const sourceText = output.text;
177
- await init;
178
- const [imports] = parse(sourceText);
179
- if (!allowRuntimeImports && imports.some((entry) => entry.d >= 0)) {
180
- const error = new Error(
181
- `Live adapter ${entryReal} contains a runtime dynamic import that cannot be bound to the approved bundle. Replace it with a static import so its implementation bytes are included in the plan digest.`
182
- );
183
- Object.assign(error, {
184
- code: "RECIPE_TRUST_UNBOUND_IMPORT",
185
- userAction: "replace the computed import with a static import, or promote the adapter into an explicitly trusted engineer-owned library"
186
- });
187
- throw error;
188
- }
189
- return {
190
- action,
191
- platform,
192
- entryPath: entryReal,
193
- sourceText,
194
- sourceDigest: `sha256:${createHash("sha256").update(sourceText).digest("hex")}`
195
- };
196
- }
197
- function bindImportMetaUrls() {
198
- return {
199
- name: "bind-import-meta-urls",
200
- setup(buildContext) {
201
- buildContext.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async (args) => {
202
- const contents = await readFile(args.path, "utf8");
203
- if (!contents.includes("import.meta.url")) return void 0;
204
- const extension = path.extname(args.path);
205
- const loader = extension === ".ts" || extension === ".mts" || extension === ".cts" ? "ts" : extension === ".tsx" ? "tsx" : extension === ".jsx" ? "jsx" : "js";
206
- return {
207
- contents: contents.replaceAll(
208
- "import.meta.url",
209
- JSON.stringify(pathToFileURL(args.path).href)
210
- ),
211
- loader
212
- };
213
- });
214
- }
215
- };
216
- }
217
- function tsxLiveScripts(platform) {
218
- return platform && adapterRegistry.has(platform) ? getAdapterSurface(platform).actions.tsxLiveScripts : void 0;
219
- }
220
- function commandFor(file, projectRoot, platform) {
221
- const needsTsx = Boolean(tsxLiveScripts(platform)) || importsSourceTypescript(file);
222
- if (!needsTsx) {
223
- return { command: process.execPath, args: [file] };
224
- }
225
- const tsxBin = resolveTsxBin(projectRoot);
226
- if (!tsxBin) {
227
- const where = projectRoot ?? "the checkout";
228
- const error = new Error(
229
- `this action runs TypeScript from the checkout but no tsx runtime was found.
230
- Next: run 'yarn install' in ${where} (tsx is a dev dependency of the checkout); if package imports still fail, run 'yarn build' there`
231
- );
232
- error.exitCode = 2;
233
- throw error;
234
- }
235
- return { command: tsxBin, args: [file] };
236
- }
237
- function resolveTsxBin(projectRoot) {
238
- if (process.env.TSX_BIN) return process.env.TSX_BIN;
239
- const candidates = [];
240
- if (projectRoot) candidates.push(path.join(projectRoot, "node_modules/.bin/tsx"));
241
- candidates.push(path.join(runnerDir, "node_modules/.bin/tsx"));
242
- for (const candidate of candidates) {
243
- if (existsSync(candidate)) return candidate;
244
- }
245
- try {
246
- const protocolTsx = path.join(resolveRequiredLocalProtocolRoot("TypeScript live adapter execution"), "node_modules/.bin/tsx");
247
- if (existsSync(protocolTsx)) return protocolTsx;
248
- } catch {
249
- }
250
- return null;
251
- }
252
- function importsSourceTypescript(file) {
253
- return importsSourceTypescriptFrom(file, /* @__PURE__ */ new Set());
254
- }
255
- function importsSourceTypescriptFrom(file, visited) {
256
- const absolute = path.resolve(file);
257
- if (visited.has(absolute)) return false;
258
- visited.add(absolute);
259
- const source = readFileSync(file, "utf8");
260
- const importPattern = /(?:from\s+|import\(\s*)['"]([^'"]+)['"]/gu;
261
- for (const match of source.matchAll(importPattern)) {
262
- const specifier = match[1] ?? "";
263
- if (specifier.endsWith(".ts")) return true;
264
- if (!specifier.startsWith(".")) continue;
265
- if (!specifier.endsWith(".mjs")) continue;
266
- if (importsSourceTypescriptFrom(path.resolve(path.dirname(absolute), specifier), visited)) {
267
- return true;
268
- }
269
- }
270
- return false;
271
- }
272
- async function resolveLiveAdapter(platform, action, runtimeEnv) {
273
- return firstExecutablePath(candidatePaths(platform, action, runtimeEnv));
274
- }
275
- async function runLiveAdapterScript({ platform, action, node, context, prepared }) {
276
- const script = prepared?.entryPath ?? await resolveLiveAdapter(platform, action, context?.env);
277
- if (!script) return null;
278
- const tempDir = await mkdtemp(path.join(os.tmpdir(), "mm-harness-live-adapter-"));
279
- const inputPath = path.join(tempDir, "input.json");
280
- const outputPath = path.join(tempDir, "output.json");
281
- const input = {
282
- schemaVersion: 1,
283
- platform,
284
- action,
285
- node,
286
- context: {
287
- nodeId: context.nodeId,
288
- projectRoot: context.projectRoot,
289
- artifactsDir: context.artifactsDir,
290
- ...context.trustedMutation ? { trustedMutation: serializeTrustedMutationContext(context.trustedMutation) } : {}
291
- },
292
- outputPath
293
- };
294
- await writeFile(inputPath, `${JSON.stringify(input, null, 2)}
295
- `);
296
- const command = prepared ? commandForPrepared(context.projectRoot, platform) : commandFor(script, context.projectRoot, platform);
297
- const platformEnv = await tsxLiveScripts(platform)?.env(context.projectRoot, tempDir) ?? {};
298
- const processTimeoutMs = liveAdapterProcessTimeoutMs(node);
299
- const result = await runProcess(command.command, [...command.args, inputPath], {
300
- cwd: context.projectRoot,
301
- env: {
302
- ...process.env,
303
- ...context.env,
304
- ...platformEnv,
305
- METAMASK_RECIPE_ADAPTER_INPUT: inputPath,
306
- METAMASK_RECIPE_ADAPTER_OUTPUT: outputPath
307
- },
308
- timeoutMs: processTimeoutMs,
309
- ...prepared ? { stdin: prepared.sourceText } : {}
310
- });
311
- try {
312
- if (result.timedOut) {
313
- throw new Error(`Live adapter ${script} timed out after ${processTimeoutMs}ms.`);
314
- }
315
- if (result.exitCode !== 0) {
316
- const failureOutput = await readFile(outputPath, "utf8").then(JSON.parse).catch(() => null);
317
- if (Array.isArray(failureOutput?.artifacts)) {
318
- for (const artifact of failureOutput.artifacts) context.registerArtifact(artifact);
319
- }
320
- throw new Error(`Live adapter ${script} exited ${result.exitCode}: ${result.stderr || result.stdout}`);
321
- }
322
- let parsed = null;
323
- try {
324
- parsed = JSON.parse(await readFile(outputPath, "utf8"));
325
- } catch (_error) {
326
- const stdout = result.stdout.trim();
327
- if (!stdout) throw new Error(`Live adapter ${script} did not write JSON output.`);
328
- parsed = JSON.parse(stdout);
329
- }
330
- return { script, result: parsed };
331
- } finally {
332
- await rm(tempDir, { recursive: true, force: true });
333
- }
334
- }
335
- function liveAdapterProcessTimeoutMs(node) {
336
- if (node.live_adapter_timeout_ms != null) {
337
- const timeoutMs = Number(node.live_adapter_timeout_ms);
338
- return Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 6e4;
339
- }
340
- const settleMs = Number(node.settle_ms);
341
- const settleAllowance = Number.isFinite(settleMs) && settleMs > 0 ? settleMs : 0;
342
- const actionTimeouts = [
343
- node.target_timeout_ms,
344
- node.unlock_timeout_ms,
345
- node.timeout_ms,
346
- node.wait_timeout_ms
347
- ].map(Number).filter((value) => Number.isFinite(value) && value > 0);
348
- if (actionTimeouts.length > 0) {
349
- return Math.max(
350
- 6e4,
351
- actionTimeouts.reduce((total, value) => total + value, 0) + settleAllowance + 5e3
352
- );
353
- }
354
- return 6e4 + settleAllowance;
355
- }
356
- function commandForPrepared(projectRoot, platform) {
357
- if (!tsxLiveScripts(platform)) {
358
- return { command: process.execPath, args: ["--input-type=module", "-"] };
359
- }
360
- const tsxBin = resolveTsxBin(projectRoot);
361
- if (!tsxBin) {
362
- const error = new Error(
363
- `this action imports TypeScript from the checkout but no tsx runtime was found.
364
- Next: run 'yarn install' in ${projectRoot}`
365
- );
366
- error.exitCode = 2;
367
- throw error;
368
- }
369
- return { command: tsxBin, args: ["--input-type=module", "-"] };
370
- }
371
- function isPathWithin(root, candidate) {
372
- const relative = path.relative(root, candidate);
373
- return relative === "" || relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
374
- }
375
- export {
376
- liveAdapterProcessTimeoutMs,
377
- prepareLiveAdapterScript,
378
- resolveLiveAdapter,
379
- runLiveAdapterScript
380
- };
@@ -1,68 +0,0 @@
1
- import { adapterRegistry } from "./adapters/registry.js";
2
- import { getAdapterSurface } from "./adapters/surface.js";
3
- function isRecord(value) {
4
- return Boolean(value) && typeof value === "object" && !Array.isArray(value);
5
- }
6
- function resolveMetaMaskParameterValue(value, params) {
7
- if (typeof value === "string") {
8
- const exact = /^\{\{params\.([A-Za-z0-9_.-]+)\}\}$/u.exec(value);
9
- if (!exact) return value;
10
- let current = params;
11
- for (const segment of exact[1].split(".")) {
12
- if (!isRecord(current) || !Object.hasOwn(current, segment)) return value;
13
- current = current[segment];
14
- }
15
- return current;
16
- }
17
- if (Array.isArray(value)) return value.map((entry) => resolveMetaMaskParameterValue(entry, params));
18
- if (!isRecord(value)) return value;
19
- return Object.fromEntries(
20
- Object.entries(value).map(([key, entry]) => [key, resolveMetaMaskParameterValue(entry, params)])
21
- );
22
- }
23
- function validateMetaMaskActionInputs(recipe, adapter, params) {
24
- if (!isRecord(recipe) || !isRecord(recipe.workflow)) return [];
25
- const nodes = isRecord(recipe.workflow.nodes) ? recipe.workflow.nodes : void 0;
26
- if (!nodes) return [];
27
- const surface = adapter && adapterRegistry.has(adapter) ? getAdapterSurface(adapter) : void 0;
28
- const findings = [];
29
- for (const [nodeId, rawNode] of Object.entries(nodes)) {
30
- const node = params ? resolveMetaMaskParameterValue(rawNode, params) : rawNode;
31
- if (!isRecord(node)) continue;
32
- const action = node.action;
33
- findings.push(...surface?.actions.inputFindings?.(nodeId, node) ?? []);
34
- const state = typeof node.state === "string" ? node.state.toLowerCase() : void 0;
35
- const createsOrder = action === "metamask.perps.place_order" || !surface?.actions.ensureChecksOnly && (action === "metamask.perps.ensure_positions" || action === "metamask.perps.ensure_orders") && (state === "open" || state === "present");
36
- if (!createsOrder) continue;
37
- const requireOneOf = (names, message) => {
38
- const present = names.some((name) => {
39
- const value = node[name];
40
- return value !== void 0 && value !== null && value !== "";
41
- });
42
- if (present) return;
43
- findings.push({
44
- severity: "error",
45
- code: "recipe.missing_param",
46
- path: `workflow.nodes.${nodeId}.${names[0]}`,
47
- message
48
- });
49
- };
50
- requireOneOf(
51
- ["market", "symbol"],
52
- `${action} requires market=<symbol> (or symbol=<symbol>) before it may create an order`
53
- );
54
- requireOneOf(
55
- ["side"],
56
- `${action} requires side=long or side=short before it may create an order`
57
- );
58
- requireOneOf(
59
- ["amount", "notional"],
60
- `${action} requires amount=<usd> or notional=<usd> before it may create an order`
61
- );
62
- }
63
- return findings;
64
- }
65
- export {
66
- resolveMetaMaskParameterValue,
67
- validateMetaMaskActionInputs
68
- };