@deeeed/metamask-harness 0.41.1 → 0.42.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 (38) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +7 -0
  3. package/adapters/manifest.json +8 -0
  4. package/adapters/mobile/bridge-runtime/cdp-bridge.cjs +70 -10
  5. package/adapters/mobile/bridge-runtime/console-forwarder.cjs +115 -15
  6. package/adapters/mobile/bridge-runtime/lib/cdp-broker.cjs +752 -0
  7. package/adapters/mobile/bridge-runtime/lib/devtools-proxy.cjs +177 -0
  8. package/adapters/mobile/bridge-runtime/lib/target-discovery.cjs +13 -3
  9. package/adapters/mobile/reload-app.mjs +67 -0
  10. package/adapters/mobile/start-console-forwarder.sh +17 -2
  11. package/adapters/mobile/start-metro.sh +6 -11
  12. package/adapters/mobile/stop-metro.sh +10 -1
  13. package/adapters/shared/open-debug.mjs +172 -2
  14. package/dist/adapters/extension/network-observer.js +300 -0
  15. package/dist/adapters/mobile/metro-env.js +0 -5
  16. package/dist/adapters/mobile/prepare.js +1 -3
  17. package/dist/adapters/mobile/runtime-decision.js +6 -9
  18. package/dist/adapters.js +14 -1
  19. package/dist/cli-commands.js +6 -3
  20. package/dist/cli.js +4 -0
  21. package/dist/command-contract.js +3 -0
  22. package/dist/commands/call.js +45 -20
  23. package/dist/commands/reload.js +80 -0
  24. package/dist/commands/run.js +49 -22
  25. package/dist/mm-harness-cli.js +17 -1
  26. package/dist/network-observation.js +271 -0
  27. package/docs/NETWORK-CAPTURE.md +98 -0
  28. package/docs/RECIPES.md +10 -0
  29. package/library/actions/mobile/app/network_assert.mjs +14 -0
  30. package/library/actions/mobile/app/network_capture.mjs +72 -0
  31. package/library/actions/mobile/platform/bridge.mjs +7 -2
  32. package/library/actions/shared/app/network-artifact.mjs +10 -0
  33. package/library/actions/shared/app/network-assert.mjs +154 -0
  34. package/library/manifests/extension.action-manifest.json +88 -0
  35. package/library/manifests/mobile.action-manifest.json +107 -0
  36. package/library/recipes/mobile/perps/performance.recipe.json +11 -11
  37. package/package.json +1 -1
  38. package/scripts/completions.sh +2 -1
@@ -0,0 +1,154 @@
1
+ import { constants } from 'node:fs';
2
+ import { open } from 'node:fs/promises';
3
+
4
+ import { resolveNetworkArtifact } from './network-artifact.mjs';
5
+
6
+ export async function assertNetwork(input) {
7
+ const node = input.node ?? {};
8
+ const id = String(node.id ?? '').trim();
9
+ const artifactsDir = input.context?.artifactsDir;
10
+ if (!id || !artifactsDir) {
11
+ throw new Error('app.network_assert requires id and artifactsDir.');
12
+ }
13
+ const artifactPath = String(
14
+ node.artifact_path ?? `network/${id}-summary.json`,
15
+ );
16
+ const artifactFile = resolveNetworkArtifact(artifactsDir, artifactPath);
17
+ const handle = await open(
18
+ artifactFile,
19
+ constants.O_RDONLY | constants.O_NOFOLLOW,
20
+ );
21
+ let summary;
22
+ try {
23
+ const artifactStat = await handle.stat();
24
+ if (!artifactStat.isFile() || artifactStat.size > 5 * 1024 * 1024) {
25
+ throw new Error(
26
+ 'app.network_assert summary is not a bounded regular file.',
27
+ );
28
+ }
29
+ summary = JSON.parse(await handle.readFile('utf8'));
30
+ } finally {
31
+ await handle.close();
32
+ }
33
+ validateSummary(summary, id);
34
+
35
+ const requiredStatus = node.required_status;
36
+ if (requiredStatus && summary.status !== requiredStatus) {
37
+ throw new Error(
38
+ `app.network_assert expected ${requiredStatus}, got ${summary.status}.`,
39
+ );
40
+ }
41
+ const requiredMinRequests = node.required_min_requests;
42
+ const requiredMaxRequests = node.required_max_requests;
43
+ const requiredTypes = node.required_types ?? [];
44
+ const forbiddenTypes = node.forbidden_types ?? [];
45
+ if (
46
+ (requiredTypes.length > 0 || forbiddenTypes.length > 0) &&
47
+ !summary.projectedBodyFields.includes('type')
48
+ ) {
49
+ throw new Error(
50
+ 'app.network_assert type assertions require body_json_fields to include type.',
51
+ );
52
+ }
53
+ if (
54
+ (requiredMaxRequests !== undefined || forbiddenTypes.length > 0) &&
55
+ requiredStatus !== 'complete'
56
+ ) {
57
+ throw new Error(
58
+ 'app.network_assert negative assertions require required_status=complete.',
59
+ );
60
+ }
61
+ for (const [name, value] of [
62
+ ['required_min_requests', requiredMinRequests],
63
+ ['required_max_requests', requiredMaxRequests],
64
+ ]) {
65
+ if (value !== undefined && (!Number.isInteger(value) || value < 0)) {
66
+ throw new Error(`app.network_assert ${name} is invalid.`);
67
+ }
68
+ }
69
+ if (
70
+ Number.isInteger(requiredMinRequests) &&
71
+ Number.isInteger(requiredMaxRequests) &&
72
+ requiredMinRequests > requiredMaxRequests
73
+ ) {
74
+ throw new Error('app.network_assert request assertion range is invalid.');
75
+ }
76
+ if (
77
+ Number.isInteger(requiredMinRequests) &&
78
+ summary.totalRequests < requiredMinRequests
79
+ ) {
80
+ throw new Error(
81
+ `app.network_assert expected at least ${requiredMinRequests} request(s), got ${summary.totalRequests}.`,
82
+ );
83
+ }
84
+ if (
85
+ Number.isInteger(requiredMaxRequests) &&
86
+ summary.totalRequests > requiredMaxRequests
87
+ ) {
88
+ throw new Error(
89
+ `app.network_assert expected at most ${requiredMaxRequests} request(s), got ${summary.totalRequests}.`,
90
+ );
91
+ }
92
+ for (const type of requiredTypes) {
93
+ if (!hasPositiveOwnCount(summary.requestsByType, type)) {
94
+ throw new Error(`app.network_assert did not observe required type ${type}.`);
95
+ }
96
+ }
97
+ for (const type of forbiddenTypes) {
98
+ if (hasPositiveOwnCount(summary.requestsByType, type)) {
99
+ throw new Error(`app.network_assert observed forbidden type ${type}.`);
100
+ }
101
+ }
102
+
103
+ return {
104
+ action: input.action,
105
+ id,
106
+ artifactPath,
107
+ status: summary.status,
108
+ totalRequests: summary.totalRequests,
109
+ };
110
+ }
111
+
112
+ function hasPositiveOwnCount(counts, key) {
113
+ return (
114
+ Object.hasOwn(counts, key) &&
115
+ Number.isInteger(counts[key]) &&
116
+ counts[key] > 0
117
+ );
118
+ }
119
+
120
+ function validateSummary(summary, expectedId) {
121
+ if (
122
+ !summary ||
123
+ typeof summary !== 'object' ||
124
+ Array.isArray(summary) ||
125
+ summary.schemaVersion !== 1 ||
126
+ summary.id !== expectedId ||
127
+ !['complete', 'partial', 'unavailable'].includes(summary.status) ||
128
+ !Number.isInteger(summary.totalRequests) ||
129
+ summary.totalRequests < 0 ||
130
+ !Array.isArray(summary.requests) ||
131
+ summary.requests.length !== summary.totalRequests ||
132
+ !Number.isInteger(summary.uninspectableBodyRequests) ||
133
+ summary.uninspectableBodyRequests < 0 ||
134
+ !Array.isArray(summary.projectedBodyFields) ||
135
+ summary.projectedBodyFields.some((field) => typeof field !== 'string') ||
136
+ !summary.requestsByType ||
137
+ typeof summary.requestsByType !== 'object' ||
138
+ Array.isArray(summary.requestsByType)
139
+ ) {
140
+ throw new Error('app.network_assert summary contract is invalid.');
141
+ }
142
+ const typeCounts = Object.values(summary.requestsByType);
143
+ for (const count of typeCounts) {
144
+ if (!Number.isInteger(count) || count < 1) {
145
+ throw new Error('app.network_assert summary type counts are invalid.');
146
+ }
147
+ }
148
+ if (
149
+ typeCounts.reduce((total, count) => total + count, 0) !==
150
+ summary.totalRequests
151
+ ) {
152
+ throw new Error('app.network_assert summary type counts are inconsistent.');
153
+ }
154
+ }
@@ -1119,6 +1119,94 @@
1119
1119
  }
1120
1120
  ]
1121
1121
  },
1122
+ "app.network_capture": {
1123
+ "description": "Capture redacted HTTP requests between named Recipe Protocol v1 nodes through the adapter's persistent Network observer.",
1124
+ "examples": [
1125
+ {
1126
+ "action": "app.network_capture",
1127
+ "phase": "start",
1128
+ "id": "perps-home",
1129
+ "url_includes": ["api.hyperliquid.xyz/info"],
1130
+ "methods": ["POST"],
1131
+ "body_json_fields": ["type", "req.coin", "dex"],
1132
+ "intent": "Start the redacted request window",
1133
+ "next": "exercise-flow"
1134
+ },
1135
+ {
1136
+ "action": "app.network_capture",
1137
+ "phase": "end",
1138
+ "id": "perps-home",
1139
+ "artifact_path": "network/perps-home.json",
1140
+ "intent": "End and index the redacted request window",
1141
+ "next": "assert-network"
1142
+ }
1143
+ ],
1144
+ "schema": {
1145
+ "type": "object",
1146
+ "properties": {
1147
+ "phase": { "type": "string", "enum": ["start", "end"] },
1148
+ "id": { "type": "string" },
1149
+ "url_includes": {
1150
+ "type": "array",
1151
+ "items": { "type": "string" }
1152
+ },
1153
+ "methods": {
1154
+ "type": "array",
1155
+ "items": { "type": "string" }
1156
+ },
1157
+ "body_json_fields": {
1158
+ "type": "array",
1159
+ "items": { "type": "string" }
1160
+ },
1161
+ "max_requests": { "type": "integer" },
1162
+ "max_duration_ms": { "type": "integer" },
1163
+ "artifact_path": { "type": "string" }
1164
+ },
1165
+ "required": ["phase", "id"],
1166
+ "additionalProperties": false
1167
+ },
1168
+ "execution_capabilities": []
1169
+ },
1170
+ "app.network_assert": {
1171
+ "description": "Assert a previously indexed app.network_capture summary without hiding its diagnostics on failure.",
1172
+ "examples": [
1173
+ {
1174
+ "action": "app.network_assert",
1175
+ "id": "perps-home",
1176
+ "artifact_path": "network/perps-home.json",
1177
+ "required_status": "complete",
1178
+ "required_min_requests": 1,
1179
+ "required_types": ["allMids"],
1180
+ "forbidden_types": ["candleSnapshot"],
1181
+ "intent": "Assert the indexed request summary",
1182
+ "next": "done"
1183
+ }
1184
+ ],
1185
+ "schema": {
1186
+ "type": "object",
1187
+ "properties": {
1188
+ "id": { "type": "string" },
1189
+ "artifact_path": { "type": "string" },
1190
+ "required_status": {
1191
+ "type": "string",
1192
+ "enum": ["complete", "partial", "unavailable"]
1193
+ },
1194
+ "required_min_requests": { "type": "integer" },
1195
+ "required_max_requests": { "type": "integer" },
1196
+ "required_types": {
1197
+ "type": "array",
1198
+ "items": { "type": "string" }
1199
+ },
1200
+ "forbidden_types": {
1201
+ "type": "array",
1202
+ "items": { "type": "string" }
1203
+ }
1204
+ },
1205
+ "required": ["id"],
1206
+ "additionalProperties": false
1207
+ },
1208
+ "execution_capabilities": []
1209
+ },
1122
1210
  "app.status": {
1123
1211
  "description": "Report the adapter's static status — platform, project root, resolved checkout shape, and headless compatibility mode (no live route or account).",
1124
1212
  "examples": [
@@ -1112,6 +1112,113 @@
1112
1112
  },
1113
1113
  "execution_capabilities": ["app-mutation"]
1114
1114
  },
1115
+ "app.network_capture": {
1116
+ "description": "Capture redacted HTTP requests between named Recipe Protocol v1 nodes through the adapter's persistent Network observer.",
1117
+ "examples": [
1118
+ {
1119
+ "action": "app.network_capture",
1120
+ "phase": "start",
1121
+ "id": "perps-home",
1122
+ "url_includes": ["api.hyperliquid.xyz/info"],
1123
+ "methods": ["POST"],
1124
+ "body_json_fields": ["type", "req.coin", "dex"],
1125
+ "intent": "Start the redacted request window",
1126
+ "next": "exercise-flow"
1127
+ },
1128
+ {
1129
+ "action": "app.network_capture",
1130
+ "phase": "end",
1131
+ "id": "perps-home",
1132
+ "artifact_path": "network/perps-home.json",
1133
+ "intent": "End and index the redacted request window",
1134
+ "next": "assert-network"
1135
+ }
1136
+ ],
1137
+ "schema": {
1138
+ "type": "object",
1139
+ "properties": {
1140
+ "phase": {
1141
+ "type": "string",
1142
+ "enum": ["start", "end"]
1143
+ },
1144
+ "id": {
1145
+ "type": "string"
1146
+ },
1147
+ "url_includes": {
1148
+ "type": "array",
1149
+ "items": { "type": "string" }
1150
+ },
1151
+ "methods": {
1152
+ "type": "array",
1153
+ "items": { "type": "string" }
1154
+ },
1155
+ "body_json_fields": {
1156
+ "type": "array",
1157
+ "items": { "type": "string" }
1158
+ },
1159
+ "max_requests": {
1160
+ "type": "integer"
1161
+ },
1162
+ "max_duration_ms": {
1163
+ "type": "integer"
1164
+ },
1165
+ "artifact_path": {
1166
+ "type": "string"
1167
+ }
1168
+ },
1169
+ "required": ["phase", "id"],
1170
+ "additionalProperties": false
1171
+ },
1172
+ "execution_capabilities": []
1173
+ },
1174
+ "app.network_assert": {
1175
+ "description": "Assert a previously indexed app.network_capture summary without hiding its diagnostics on failure.",
1176
+ "examples": [
1177
+ {
1178
+ "action": "app.network_assert",
1179
+ "id": "perps-home",
1180
+ "artifact_path": "network/perps-home.json",
1181
+ "required_status": "complete",
1182
+ "required_min_requests": 1,
1183
+ "required_types": ["allMids"],
1184
+ "forbidden_types": ["candleSnapshot"],
1185
+ "intent": "Assert the indexed request summary",
1186
+ "next": "done"
1187
+ }
1188
+ ],
1189
+ "schema": {
1190
+ "type": "object",
1191
+ "properties": {
1192
+ "id": {
1193
+ "type": "string"
1194
+ },
1195
+ "artifact_path": {
1196
+ "type": "string"
1197
+ },
1198
+ "required_status": {
1199
+ "type": "string",
1200
+ "enum": ["complete", "partial", "unavailable"]
1201
+ },
1202
+ "required_min_requests": {
1203
+ "type": "integer"
1204
+ },
1205
+ "required_max_requests": {
1206
+ "type": "integer"
1207
+ },
1208
+ "required_types": {
1209
+ "type": "array",
1210
+ "items": { "type": "string" }
1211
+ },
1212
+ "forbidden_types": {
1213
+ "type": "array",
1214
+ "items": { "type": "string" }
1215
+ }
1216
+ },
1217
+ "required": ["id"],
1218
+ "additionalProperties": false
1219
+ },
1220
+ "execution_capabilities": []
1221
+ },
1115
1222
  "app.status": {
1116
1223
  "description": "Report the adapter's static status — platform, project root, resolved checkout shape, and headless compatibility mode (no live route or account).",
1117
1224
  "examples": [
@@ -168,25 +168,25 @@
168
168
  "value": "{{params.content_variant}}",
169
169
  "equals": "trending",
170
170
  "cases": {
171
- "match": "assert-no-positions"
171
+ "match": "ensure-no-positions"
172
172
  },
173
173
  "default": "assert-position",
174
- "intent": "Prove either the existing position or the empty account before timing"
174
+ "intent": "Prepare either the existing position or an empty account before timing"
175
175
  },
176
- "assert-no-positions": {
177
- "action": "metamask.perps.assert_positions",
176
+ "ensure-no-positions": {
177
+ "action": "metamask.perps.ensure_positions",
178
178
  "state": "none",
179
179
  "mode": "all",
180
- "timeout_ms": 30000,
181
- "intent": "Prove the market-only account has no open positions",
182
- "next": "assert-no-orders"
180
+ "timeout_ms": 120000,
181
+ "intent": "Close all positions outside timing so the market-only account is empty",
182
+ "next": "ensure-no-orders"
183
183
  },
184
- "assert-no-orders": {
185
- "action": "metamask.perps.assert_orders",
184
+ "ensure-no-orders": {
185
+ "action": "metamask.perps.ensure_orders",
186
186
  "state": "none",
187
187
  "mode": "all",
188
- "timeout_ms": 30000,
189
- "intent": "Prove the market-only account has no open orders",
188
+ "timeout_ms": 120000,
189
+ "intent": "Cancel all orders outside timing so the market-only account is empty",
190
190
  "next": "select-account-switch-preparation"
191
191
  },
192
192
  "assert-position": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deeeed/metamask-harness",
3
- "version": "0.41.1",
3
+ "version": "0.42.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mm-harness": "bin/mm-harness"
@@ -29,7 +29,7 @@ _mmh_bin() {
29
29
  return 1
30
30
  }
31
31
 
32
- _mmh_commands="launch stop logs debug fixtures actions call run doctor check checklist execution-template recipe-quality install verify cleanup completions"
32
+ _mmh_commands="launch stop logs debug reload fixtures actions call run doctor check checklist execution-template recipe-quality install verify cleanup completions"
33
33
 
34
34
  # Per-command flags (static, from the mm-harness surface).
35
35
  _mmh_flags_for() {
@@ -38,6 +38,7 @@ _mmh_flags_for() {
38
38
  stop) printf '%s' "--port --target --json" ;;
39
39
  logs) printf '%s' "--full --events --source --adapter --target --json" ;;
40
40
  debug) printf '%s' "--worker --dev-menu --adapter --target --json" ;;
41
+ reload) printf '%s' "--adapter --platform --target --json" ;;
41
42
  fixtures) printf '%s' "--from --dev --force --fixture --adapter --target --json" ;;
42
43
  checklist) printf '%s' "" ;;
43
44
  execution-template) printf '%s' "--dir --domain-dir --project-worker --project-name --package-templates --package-id --flow --run-mode --platform --domain --id --provenance --include-shadowed --no-include-shadowed --title --force --json" ;;