-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflutter-vm-agentic-loop.mjs
More file actions
300 lines (280 loc) · 8.93 KB
/
flutter-vm-agentic-loop.mjs
File metadata and controls
300 lines (280 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env node
/**
* Full non-invasive Flutter observability loop — proves the VM-service
* integration end to end:
*
* 1. rc_start flutter run -d macos
* 2. rc_flutter_endpoints (wait for URLs)
* 3. rc_flutter_connect (open VM-service WS + subscribe to streams)
* 4. rc_flutter_eval "1+1" — confirm two-way comms
* 5. inject a synthetic build-time exception into main.dart
* 6. rc_flutter_hot_reload ← structured result
* 7. rc_flutter_drain_errors ← MUST see the exception (count > 0)
* 8. restore main.dart, hot-reload again
* 9. rc_flutter_drain_errors ← MUST be empty (count === 0)
* 10. send 'q', wait for clean exit
*
* Cleans up main.dart in a finally block so the project is left untouched.
*
* For UI INTERACTION tests (tap, scroll, text input, screenshots), use
* Marionette MCP — see README.md.
*/
import { spawn } from "node:child_process";
import {
readFileSync,
writeFileSync,
copyFileSync,
unlinkSync,
existsSync,
} from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
const here = dirname(fileURLToPath(import.meta.url));
const serverEntry = join(here, "..", "dist", "index.js");
const flutterCwd = join(here, "..", "flutter_example");
const mainDart = join(flutterCwd, "lib", "main.dart");
const mainDartBak = mainDart + ".agentic-bak";
function injectException() {
const original = readFileSync(mainDart, "utf8");
copyFileSync(mainDart, mainDartBak);
const marker = "Widget build(BuildContext context) {\n // This method is rerun";
if (!original.includes(marker)) {
throw new Error("could not find injection point in main.dart");
}
const patched = original.replace(
marker,
`Widget build(BuildContext context) {
throw Exception('agentic-rc VM demo: synthetic build-time exception');
// This method is rerun`,
);
writeFileSync(mainDart, patched, "utf8");
}
function restoreMainDart() {
if (existsSync(mainDartBak)) {
copyFileSync(mainDartBak, mainDart);
unlinkSync(mainDartBak);
}
}
const child = spawn(process.execPath, [serverEntry], { stdio: ["pipe", "pipe", "pipe"] });
const pending = new Map();
let nextId = 1;
let stdoutBuf = "";
child.stdout.setEncoding("utf8");
child.stdout.on("data", (chunk) => {
stdoutBuf += chunk;
let idx;
while ((idx = stdoutBuf.indexOf("\n")) !== -1) {
const line = stdoutBuf.slice(0, idx);
stdoutBuf = stdoutBuf.slice(idx + 1);
if (!line.trim()) continue;
let msg;
try {
msg = JSON.parse(line);
} catch {
continue;
}
if (msg.id !== undefined && pending.has(msg.id)) {
const { resolve, reject } = pending.get(msg.id);
pending.delete(msg.id);
if (msg.error) reject(new Error(JSON.stringify(msg.error)));
else resolve(msg.result);
}
}
});
child.stderr.setEncoding("utf8");
child.stderr.on("data", (c) => process.stderr.write(`[server] ${c}`));
function send(method, params, timeoutMs = 30_000) {
const id = nextId++;
return new Promise((resolve, reject) => {
pending.set(id, { resolve, reject });
child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n");
setTimeout(() => {
if (pending.has(id)) {
pending.delete(id);
reject(new Error(`Timeout waiting for ${method} (id=${id})`));
}
}, timeoutMs);
});
}
function notify(method, params) {
child.stdin.write(JSON.stringify({ jsonrpc: "2.0", method, params }) + "\n");
}
function unwrap(r) {
return JSON.parse(r.content.find((c) => c.type === "text").text);
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
async function main() {
// 0. init MCP
console.log("→ initialize MCP server");
const init = await send("initialize", {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "vm-agentic-loop", version: "0.0.1" },
});
console.log(` ${init.serverInfo.name} v${init.serverInfo.version}`);
notify("notifications/initialized", {});
// 1. start flutter
console.log("→ rc_start: flutter run -d macos");
const startInfo = unwrap(
await send("tools/call", {
name: "rc_start",
arguments: {
command: "flutter",
args: ["run", "-d", "macos"],
cwd: flutterCwd,
cols: 160,
rows: 60,
},
}),
);
const sid = startInfo.session_id;
console.log(` session=${sid} pid=${startInfo.pid}`);
// 2. wait for endpoints to be auto-detected
console.log("→ rc_flutter_endpoints wait_ms=180000");
const endpoints = unwrap(
await send(
"tools/call",
{
name: "rc_flutter_endpoints",
arguments: { session_id: sid, wait_ms: 180_000 },
},
200_000,
),
);
console.log(" endpoints:");
console.log(` vm_service_ws : ${endpoints.vm_service_ws}`);
console.log(` vm_service_http: ${endpoints.vm_service_http}`);
console.log(` devtools_url : ${endpoints.devtools_url}`);
console.log(` host:port : ${endpoints.host}:${endpoints.port}`);
console.log(` detected_at_ms: ${endpoints.detected_at_ms}`);
// 3. open VM service WS
console.log("→ rc_flutter_connect");
const connectInfo = unwrap(
await send("tools/call", {
name: "rc_flutter_connect",
arguments: { session_id: sid, wait_ms: 30_000, subscribe: true },
}),
);
console.log(` ${JSON.stringify(connectInfo)}`);
// 4. sanity-eval
console.log("→ rc_flutter_eval expression='1+1'");
const evalInfo = unwrap(
await send("tools/call", {
name: "rc_flutter_eval",
arguments: { session_id: sid, expression: "1+1" },
}),
);
console.log(` ${evalInfo.kind} = ${evalInfo.valueAsString}`);
// give the framework a beat in case any errors are still queued from boot
await sleep(500);
console.log("→ rc_flutter_drain_errors (initial baseline)");
const baseline = unwrap(
await send("tools/call", {
name: "rc_flutter_drain_errors",
arguments: { session_id: sid },
}),
);
console.log(` baseline error count: ${baseline.count}`);
// 5. inject the bug
console.log("→ patch main.dart: throw Exception in build()");
injectException();
// 6. programmatic hot reload
console.log("→ rc_flutter_hot_reload");
const reloadInfo = unwrap(
await send(
"tools/call",
{ name: "rc_flutter_hot_reload", arguments: { session_id: sid } },
20_000,
),
);
console.log(` reload: ${JSON.stringify(reloadInfo)}`);
// 7. drain errors AFTER the bad reload
await sleep(1500);
console.log("→ rc_flutter_drain_errors (after-buggy-reload)");
const buggyErrs = unwrap(
await send("tools/call", {
name: "rc_flutter_drain_errors",
arguments: { session_id: sid },
}),
);
console.log(` count=${buggyErrs.count}`);
buggyErrs.errors.slice(0, 3).forEach((e, i) => {
console.log(` [${i}] ${e.stream} ${e.message.slice(0, 200)}`);
});
if (buggyErrs.count === 0) {
throw new Error("ASSERTION FAILED: drain_errors returned 0 after a broken hot-reload");
}
console.log(" ✅ structured error detected via VM service (no PTY grepping)");
// 8. restore + hot reload
console.log("→ restore main.dart");
restoreMainDart();
console.log("→ rc_flutter_hot_reload (after fix)");
const reload2 = unwrap(
await send("tools/call", {
name: "rc_flutter_hot_reload",
arguments: { session_id: sid },
}),
);
console.log(` reload: ${JSON.stringify(reload2)}`);
await sleep(1500);
console.log("→ rc_flutter_drain_errors (after-fix)");
const cleanErrs = unwrap(
await send("tools/call", {
name: "rc_flutter_drain_errors",
arguments: { session_id: sid },
}),
);
console.log(` count=${cleanErrs.count}`);
if (cleanErrs.count !== 0) {
console.log(" ⚠️ expected 0 — but app may still be re-emitting stale errors");
cleanErrs.errors.slice(0, 2).forEach((e, i) =>
console.log(` [${i}] ${e.stream} ${e.message.slice(0, 200)}`),
);
} else {
console.log(" ✅ clean — bug fixed and verified via VM service");
}
// 9. quit
console.log("→ rc_send_keys 'q' (quit)");
await send("tools/call", {
name: "rc_send_keys",
arguments: { session_id: sid, keys: "q" },
});
const deadline = Date.now() + 20_000;
while (Date.now() < deadline) {
const s = unwrap(
await send("tools/call", {
name: "rc_status",
arguments: { session_id: sid },
}),
);
if (s.status !== "running") {
console.log(` EXIT status=${s.status} code=${s.exit_code}`);
break;
}
await sleep(500);
}
console.log("\n=== OBSERVABILITY LOOP PASSED ===");
}
main()
.then(() => {
restoreMainDart();
child.kill("SIGTERM");
process.exit(0);
})
.catch((err) => {
restoreMainDart();
child.kill("SIGKILL");
console.error("\n=== OBSERVABILITY LOOP FAILED ===");
console.error(err);
process.exit(1);
});
process.on("SIGINT", () => {
restoreMainDart();
process.exit(130);
});
process.on("SIGTERM", () => {
restoreMainDart();
process.exit(143);
});