-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvercel_codex_loop.ts
More file actions
1025 lines (901 loc) · 36.9 KB
/
Copy pathvercel_codex_loop.ts
File metadata and controls
1025 lines (901 loc) · 36.9 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { spawn } from "child_process";
import fs from "fs";
import path from "path";
import process from "process";
import dotenv from "dotenv";
import { Codex } from "@openai/codex-sdk";
import { fileURLToPath } from "url";
function loadEnv() {
const explicit = process.env.DOTENV_CONFIG_PATH;
const scriptPath = [...process.argv]
.reverse()
.find((arg) => typeof arg === "string" && (arg.endsWith(".ts") || arg.endsWith(".js")) && fs.existsSync(arg));
const scriptDir = scriptPath ? path.dirname(path.resolve(scriptPath)) : process.cwd();
const scriptEnv = path.join(scriptDir, ".env");
const cwdEnv = path.join(process.cwd(), ".env");
const envPath = explicit
? path.resolve(explicit)
: fs.existsSync(scriptEnv)
? scriptEnv
: fs.existsSync(cwdEnv)
? cwdEnv
: null;
if (!envPath) {
console.warn("[warn] No .env found (set DOTENV_CONFIG_PATH or place a .env next to the script).");
return;
}
const res = dotenv.config({ path: envPath });
const count = res.parsed ? Object.keys(res.parsed).length : 0;
if (res.error) {
console.warn(`[warn] Failed to load env from ${envPath}:`, res.error);
} else {
console.log(`[info] Loaded ${count} env var(s) from ${envPath}`);
}
}
loadEnv();
const SCRIPT_FILE = fileURLToPath(import.meta.url);
const SCRIPT_DIR = path.dirname(SCRIPT_FILE);
// =========================
// CONFIGURATION
// =========================
const REPO_PATH = path.resolve(process.env.REPO_PATH || ".");
const PROD_URL = process.env.PROD_URL || "";
const GIT_REMOTE = process.env.GIT_REMOTE || "origin";
const GIT_BRANCH = process.env.GIT_BRANCH || "";
const VERCEL_TOKEN = process.env.VERCEL_TOKEN || "";
const VERCEL_TEAM_ID = process.env.VERCEL_TEAM_ID || "";
const MAX_ITERATIONS = Number(process.env.MAX_ITERATIONS || 10);
const SLEEP_AFTER_PUSH_SECONDS = Number(process.env.SLEEP_AFTER_PUSH_SECONDS || 90);
const CODEX_USE_EXEC = (process.env.CODEX_USE_EXEC || "1") !== "0";
const RUN_PREFLIGHT = process.env.RUN_PREFLIGHT === "1"; // opt-in local checks
const PREFLIGHT_COMMANDS: string[][] = [
["pnpm", "-C", "apps/web", "exec", "tsc", "--noEmit", "--pretty", "false"],
// Add lint/test commands if desired:
// ["pnpm", "-C", "apps/web", "lint"],
// ["pnpm", "-C", "apps/web", "test"],
];
const RUNTIME_ROOT = path.resolve(process.env.VERCEL_CODEX_RUNTIME || path.join(REPO_PATH, "vercel_codex_autofix"));
const LOGS_ROOT = path.join(RUNTIME_ROOT, "logs");
const MCP_RUNTIME_DIR = path.join(RUNTIME_ROOT, "mcp");
const AUTOFIX_DIR = path.join(RUNTIME_ROOT, ".autofix");
const STATE_PATH = path.join(AUTOFIX_DIR, "state.json");
const RUN_MCP_GUI = (process.env.RUN_MCP_GUI || process.env.RUN_MCP_PLAYWRIGHT || "1") !== "0";
const MCP_GUI_SCRIPT_PATH = path.resolve(process.env.MCP_GUI_SCRIPT || path.join(MCP_RUNTIME_DIR, "run-gui-check.sh"));
const MCP_GUI_OUTPUT_DIR = path.resolve(process.env.MCP_GUI_LOG_DIR || path.join(LOGS_ROOT, "gui"));
const MCP_GUI_REPORT_PATH = path.join(AUTOFIX_DIR, "gui_report.md");
const MCP_GUI_TESTS_PATH = path.resolve(process.env.MCP_GUI_TESTS || path.join(MCP_RUNTIME_DIR, "gui-tests.md"));
const MCP_GUI_ROUTES_PATH = path.resolve(process.env.MCP_GUI_ROUTES || path.join(MCP_RUNTIME_DIR, "gui-routes.json"));
const MCP_GUI_PLAN_PATH = path.resolve(process.env.MCP_GUI_PLAN || path.join(MCP_RUNTIME_DIR, "gui-plan.generated.json"));
const TEMPLATE_MCP_DIR = path.join(SCRIPT_DIR, "mcp");
const CODEX_RULES =
"- Edit files as needed, but do NOT commit.\n" +
"- Prefer minimal, targeted changes that address the specific issue.\n" +
"- Do not change dependency versions or lockfiles unless clearly required.\n" +
"- Prefer editing application code over root configs unless necessary.\n" +
"- Avoid broad refactors and keep diffs as small as possible.\n" +
"- Do NOT rely on local node_modules or locally built artifacts.\n" +
"- Refresh the lockfile only if there is clear evidence of a mismatch.";
function extractInitialTaskFromArgv(): string {
const argv = process.argv || [];
let scriptIndex = -1;
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (typeof arg !== "string") continue;
if (!arg.endsWith(".ts") && !arg.endsWith(".js")) continue;
const candidate = path.isAbsolute(arg) ? arg : path.resolve(process.cwd(), arg);
if (fs.existsSync(candidate)) scriptIndex = i;
}
const start = scriptIndex >= 0 ? scriptIndex + 1 : 2;
const fragments = argv.slice(start).filter((arg): arg is string => typeof arg === "string");
if (fragments.length === 0) return "";
const hasMeaningful = fragments.some((frag) => frag.trim().length > 0);
if (!hasMeaningful) return "";
const joined = fragments.join(" ").trim();
if (!joined) return "";
if (joined === '""' || joined === "''") return "";
return joined;
}
const INITIAL_TASK = extractInitialTaskFromArgv();
function buildInitialCommitMessage(task: string): string {
const normalized = task.replace(/\s+/g, " ").trim();
if (!normalized) return "chore: codex initial task";
const prefix = "chore: codex initial task - ";
const maxLength = 72;
const available = Math.max(8, maxLength - prefix.length);
const snippet = normalized.length > available ? `${normalized.slice(0, available - 3)}...` : normalized;
return `${prefix}${snippet}`;
}
// =========================
// HELPER FUNCTIONS
// =========================
type RunResult = { stdout: string; stderr: string; code: number };
type AutoFixState = {
iteration: number;
lastPushedCommit?: string;
seenSignatures: string[];
resolvedSignatures: string[];
lastScore?: number;
};
type GuiCheckOutcome = {
ok: boolean;
report: string;
logPath: string;
targetUrl: string;
};
type GuiRoutesConfig = {
baseUrl?: string;
defaultWaitMs?: number;
routes: Array<string | GuiRouteEntry>;
};
type GuiRouteEntry = {
path: string;
name?: string;
description?: string;
waitMs?: number;
};
type GuiRouteSpec = {
path: string;
name: string;
description?: string;
waitMs: number;
};
function run(
cmd: string[],
opts: { cwd?: string; env?: NodeJS.ProcessEnv; inputText?: string; streamOutput?: boolean } = {},
): Promise<RunResult> {
return new Promise((resolve) => {
const child = spawn(cmd[0], cmd.slice(1), {
cwd: opts.cwd,
env: opts.env,
stdio: "pipe",
});
let stdout = "";
let stderr = "";
child.stdout.setEncoding("utf-8");
child.stderr.setEncoding("utf-8");
child.stdout.on("data", (data) => {
stdout += data;
if (opts.streamOutput) process.stdout.write(data);
});
child.stderr.on("data", (data) => {
stderr += data;
if (opts.streamOutput) process.stderr.write(data);
});
child.on("close", (code) => resolve({ stdout, stderr, code: code ?? 0 }));
if (opts.inputText) {
child.stdin.write(opts.inputText);
child.stdin.end();
}
});
}
function ensureAutofixDir() {
if (!fs.existsSync(AUTOFIX_DIR)) fs.mkdirSync(AUTOFIX_DIR, { recursive: true });
}
function loadState(): AutoFixState {
ensureAutofixDir();
if (!fs.existsSync(STATE_PATH)) {
return { iteration: 0, seenSignatures: [], resolvedSignatures: [] };
}
try {
const raw = fs.readFileSync(STATE_PATH, "utf-8");
const parsed = JSON.parse(raw) as AutoFixState;
return {
iteration: parsed.iteration || 0,
lastPushedCommit: parsed.lastPushedCommit,
seenSignatures: parsed.seenSignatures || [],
resolvedSignatures: parsed.resolvedSignatures || [],
lastScore: parsed.lastScore,
};
} catch {
return { iteration: 0, seenSignatures: [], resolvedSignatures: [] };
}
}
function saveState(state: AutoFixState) {
ensureAutofixDir();
fs.writeFileSync(STATE_PATH, JSON.stringify(state, null, 2), "utf-8");
}
async function gitWorkdirHasChanges(): Promise<boolean> {
const { code } = await run(["git", "diff", "--quiet"], { cwd: REPO_PATH });
if (code !== 0) return true;
const staged = await run(["git", "diff", "--cached", "--quiet"], { cwd: REPO_PATH });
return staged.code !== 0;
}
async function getBranchAheadBehind(branch: string): Promise<{ ahead: number; behind: number } | null> {
const upstreamRef = `${branch}@{upstream}`;
const res = await run(["git", "rev-list", "--left-right", "--count", upstreamRef, branch], { cwd: REPO_PATH });
if (res.code !== 0) {
if (/no upstream configured/i.test(res.stderr)) {
console.warn(`[warn] Branch ${branch} has no upstream configured; skipping upstream push check.`);
return null;
}
console.warn(`[warn] Could not determine upstream status for branch ${branch}:\n${res.stderr || res.stdout}`);
return null;
}
const parts = res.stdout.trim().split(/\s+/);
if (parts.length < 2) return { ahead: 0, behind: 0 };
const behind = Number(parts[0] || 0) || 0;
const ahead = Number(parts[1] || 0) || 0;
return { ahead, behind };
}
function promptYesNo(question: string, defaultValue = false): Promise<boolean> {
if (!process.stdin.isTTY) {
console.warn("[warn] No interactive terminal detected; defaulting to 'no'.");
return Promise.resolve(defaultValue);
}
return new Promise((resolve) => {
process.stdout.write(question);
process.stdin.setEncoding("utf-8");
process.stdin.resume();
process.stdin.once("data", (data) => {
process.stdin.pause();
const answer = data.toString().trim().toLowerCase();
if (!answer) {
resolve(defaultValue);
return;
}
resolve(answer === "y" || answer === "yes");
});
});
}
async function maybePushAheadCommits(branch: string): Promise<boolean> {
const aheadBehind = await getBranchAheadBehind(branch);
if (!aheadBehind || aheadBehind.ahead <= 0) {
return true;
}
console.warn(`[warn] Local branch ${branch} is ahead of its upstream by ${aheadBehind.ahead} commit(s).`);
const confirm = await promptYesNo("Push these commits before starting the loop? [y/N] ");
if (!confirm) {
console.log("[info] Aborting to allow manual git push.");
return false;
}
const push = await run(["git", "push", GIT_REMOTE, branch], { cwd: REPO_PATH, streamOutput: true });
if (push.code !== 0) {
throw new Error(`git push failed:\n${push.stdout}\n${push.stderr}`);
}
console.log("[info] git push completed (pre-loop sync).");
console.log(`[info] Sleeping ${SLEEP_AFTER_PUSH_SECONDS}s while Vercel builds...`);
await sleep(SLEEP_AFTER_PUSH_SECONDS * 1000);
return true;
}
async function getCurrentCommitHash(short = true): Promise<string> {
const args = short ? ["git", "rev-parse", "--short=7", "HEAD"] : ["git", "rev-parse", "HEAD"];
const { stdout } = await run(args, { cwd: REPO_PATH });
return stdout.trim();
}
async function resolveGitBranch(): Promise<string> {
if (GIT_BRANCH) {
const exists = await run(["git", "show-ref", "--verify", "--quiet", `refs/heads/${GIT_BRANCH}`], { cwd: REPO_PATH });
if (exists.code === 0) return GIT_BRANCH;
console.warn(`[warn] GIT_BRANCH=${GIT_BRANCH} is not a local branch. Falling back to current branch.`);
}
const { stdout, code } = await run(["git", "rev-parse", "--abbrev-ref", "HEAD"], { cwd: REPO_PATH });
if (code === 0 && stdout.trim()) return stdout.trim();
return "main";
}
function extractDeploymentCandidates(text: string): string[] {
const candidates = new Set<string>();
{
const urlRe = /https?:\/\/[A-Za-z0-9-]+\.vercel\.app\b/g;
let match: RegExpExecArray | null;
while ((match = urlRe.exec(text)) !== null) candidates.add(match[0]);
}
{
const bareRe = /\b[A-Za-z0-9-]+\.vercel\.app\b/g;
let match: RegExpExecArray | null;
while ((match = bareRe.exec(text)) !== null) candidates.add(`https://${match[0]}`);
}
return Array.from(candidates);
}
function stripAnsi(text: string): string {
// ECMA-48 / ANSI escape sequences.
return text.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "");
}
function extractErrorSignatures(logs: string): string[] {
const s = new Set<string>();
const lines = logs.split(/\r?\n/);
for (const line of lines) {
const m1 = line.match(/(?:^|\s)(\.\/[^\s]+?\.(?:ts|tsx|js|jsx)):(\d+):(\d+)/);
if (m1) s.add(`LOC|${m1[1]}:${m1[2]}:${m1[3]}`);
const m2 = line.match(/\bTS(\d{3,5})\b/);
if (m2) s.add(`TS${m2[1]}`);
if (/failed to compile/i.test(line)) s.add("NEXT_FAILED_TO_COMPILE");
if (/type error:/i.test(line)) s.add("TS_TYPE_ERROR");
if (/elifecycle/i.test(line)) s.add("PNPM_ELIFECYCLE");
}
return Array.from(s).slice(0, 30);
}
function scoreFromLogs(logs: string): number {
const lines = logs.split(/\r?\n/);
const tsErrCount = lines.filter((l) => /\bTS\d{3,5}\b/.test(l) || /type error:/i.test(l)).length;
const buildFail = /failed to compile|build failed|elifecycle|command failed with exit code|next\.js build worker exited/i.test(logs) ? 1 : 0;
return buildFail * 1000 + tsErrCount * 10;
}
function normalizeVercelInspectLogs(raw: string): string {
const withoutCliPreamble = raw
.split(/\r?\n/)
.filter((line) => {
const trimmed = line.trim();
if (!trimmed) return true;
if (trimmed.startsWith("Vercel CLI ")) return false;
if (trimmed.startsWith("Fetching deployment ")) return false;
if (trimmed.startsWith("> Fetched deployment ")) return false;
if (trimmed.startsWith("status\t")) return false;
if (trimmed.startsWith("status ")) return false;
return true;
})
.join("\n");
return stripAnsi(withoutCliPreamble).trim();
}
async function getDeploymentIdForCurrentCommit(): Promise<string | null> {
const commitFull = await getCurrentCommitHash(false);
const commitShort = commitFull.slice(0, 7);
console.log(`[info] Looking for deployment of commit ${commitShort}...`);
const env = { ...process.env };
if (VERCEL_TOKEN) env.VERCEL_AUTH_TOKEN = VERCEL_TOKEN;
if (VERCEL_TEAM_ID) env.VERCEL_TEAM_ID = VERCEL_TEAM_ID;
// Prefer Vercel metadata filtering to avoid false positives when scanning logs for a short SHA.
const listByMeta = await run(["vercel", "list", "--no-color", "--yes", "-m", `githubCommitSha=${commitFull}`], {
cwd: REPO_PATH,
env,
});
const metaCombined = `${listByMeta.stdout}\n${listByMeta.stderr}`;
if (listByMeta.code === 0) {
const metaCandidates = extractDeploymentCandidates(metaCombined);
if (metaCandidates.length > 0) {
console.log(`[info] Found ${metaCandidates.length} deployment(s) via metadata filter.`);
return metaCandidates[0];
}
}
const list = await run(["vercel", "list", "--no-color", "--yes"], { cwd: REPO_PATH, env });
const combinedList = `${list.stdout}\n${list.stderr}`;
if (list.code !== 0 || !combinedList.trim()) {
console.warn("[warn] `vercel list` failed or returned no data.");
if (combinedList.trim()) console.warn(combinedList);
return null;
}
const candidates = extractDeploymentCandidates(combinedList);
console.log(`[info] Parsed ${candidates.length} deployment candidates from \`vercel list\`.`);
// Fallback: inspect build logs and parse the explicit "Commit: <sha>" line from Vercel's cloning step.
const cloningCommitRe = /\bCommit:\s*([0-9a-f]{7,40})\b/i;
for (const dep of candidates) {
console.log(`[debug] Inspecting deployment ${dep} for commit ${commitShort}...`);
const inspect = await run(["vercel", "inspect", dep, "--logs", "--no-color"], { cwd: REPO_PATH, env });
const inspectCombined = `${inspect.stdout}\n${inspect.stderr}`;
const match = inspectCombined.match(cloningCommitRe);
const inspectedCommit = match?.[1]?.toLowerCase();
if (!inspectedCommit) continue;
if (commitFull.toLowerCase().startsWith(inspectedCommit) || inspectedCommit.startsWith(commitShort.toLowerCase())) {
console.log(`[info] Matched commit ${commitShort} to deployment ${dep}`);
return dep;
}
}
console.warn(`[warn] No deployment found for commit ${commitShort} in ${candidates.length} candidates.`);
return null;
}
async function fetchLatestBuildLogs(): Promise<{ logs: string; deploymentUrl?: string }> {
console.log("\n[step] Fetching Vercel build logs for current commit...");
const env = { ...process.env };
if (VERCEL_TOKEN) env.VERCEL_AUTH_TOKEN = VERCEL_TOKEN;
if (VERCEL_TEAM_ID) env.VERCEL_TEAM_ID = VERCEL_TEAM_ID;
const dep = await getDeploymentIdForCurrentCommit();
if (!dep) {
console.warn("[warn] Could not find a deployment for the current commit.");
return { logs: "", deploymentUrl: undefined };
}
console.log(`[info] Inspecting deployment ${dep} ...`);
const timeout = process.env.VERCEL_INSPECT_TIMEOUT || "10m";
const res = await run(["vercel", "inspect", dep, "--logs", "--wait", "--timeout", timeout, "--no-color"], { cwd: REPO_PATH, env });
const rawLogs = `${res.stdout}\n${res.stderr}`.trim();
const logs = normalizeVercelInspectLogs(rawLogs) || rawLogs;
if (!logs.trim()) {
console.warn("[warn] No logs returned from Vercel for this deployment.");
return { logs: "", deploymentUrl: dep || undefined };
}
const lines = logs.split(/\r?\n/);
console.log("[info] Log snippet:");
console.log(lines.slice(-25).join("\n"));
return { logs, deploymentUrl: dep || undefined };
}
function buildLooksSuccessful(logs: string): boolean {
const text = logs.toLowerCase();
const failMarkers = [
"failed to compile",
"type error:",
"build failed",
"elifecycle",
"command failed with exit code",
"next.js build worker exited with code",
];
if (failMarkers.some((f) => text.includes(f))) return false;
const success = ["deployment completed", "ready! deployed to", "successfully deployed"];
if (success.some((s) => text.includes(s))) return true;
return false;
}
async function runCodexPrompt(task: string): Promise<boolean> {
const hadChangesBefore = await gitWorkdirHasChanges();
if (CODEX_USE_EXEC) {
const execEnv = { ...process.env };
delete execEnv.OPENAI_API_KEY;
delete execEnv.CODEX_API_KEY;
const args = ["codex", "exec", "--full-auto", task];
const res = await run(args, { cwd: REPO_PATH, env: execEnv, streamOutput: true });
if (res.code !== 0) {
console.warn("[warn] codex exec failed:", res.stderr || res.stdout);
return false;
}
console.log("[codex final response]");
console.log((res.stdout || res.stderr).trim());
} else {
const codexEnv: Record<string, string> = {};
for (const [k, v] of Object.entries(process.env)) {
if (v !== undefined) codexEnv[k] = v;
}
const codex = new Codex({
env: codexEnv,
});
try {
const thread = codex.startThread({
workingDirectory: REPO_PATH,
skipGitRepoCheck: false,
});
const turn = await thread.run(task);
console.log("[codex final response]");
console.log(turn.finalResponse);
} catch (err) {
console.warn("[warn] Codex run failed:", err);
return false;
}
}
const hasChangesAfter = await gitWorkdirHasChanges();
if (!hasChangesAfter && !hadChangesBefore) {
console.log("[info] Codex made no file changes.");
return false;
}
console.log("[info] Codex appears to have modified the repo.");
return true;
}
async function runCodexOnLogs(logs: string, extraContext = ""): Promise<boolean> {
console.log("\n[step] Running Codex with latest build logs...");
if (!logs.trim()) {
console.log("[info] No logs provided to Codex. Skipping.");
return false;
}
// Make logs available for the agent.
const logsPath = path.join(REPO_PATH, "dev_debug_logs.md");
fs.writeFileSync(
logsPath,
`# Vercel build logs\n\nFetched: ${new Date().toISOString()}\n\n\`\`\`\n${logs}\n\`\`\`\n`,
"utf-8",
);
console.log(`[info] Wrote logs to ${logsPath} for Codex context.`);
const task =
"You are Codex running in an autofix loop for a Vercel deployment.\n" +
"Goal: diagnose the build failure from the Vercel logs and modify the repo to fix it.\n" +
"Rules:\n" +
`${CODEX_RULES}\n` +
"- Reason strictly from dev_debug_logs.md and the repo; do not assume hidden state.\n\n" +
`The logs are in ${logsPath}.\n` +
"Start by reading that file." +
(extraContext ? `\n\nAdditional context:\n${extraContext}` : "");
return runCodexPrompt(task);
}
async function runInitialCodexTask(initialTask: string, extraContext = ""): Promise<boolean> {
const trimmed = initialTask.trim();
if (!trimmed) {
console.log("[info] No initial task provided.");
return false;
}
console.log("\n[step] Running Codex for initial CLI task...");
const task =
"You are Codex running in an autofix loop for a Vercel deployment.\n" +
"Goal: implement the initial user request described below and ensure the code remains production-ready.\n" +
"Rules:\n" +
`${CODEX_RULES}\n` +
"- Avoid regressing existing functionality and prefer incremental changes.\n\n" +
`Initial task:\n${trimmed}\n` +
(extraContext ? `\nAdditional context:\n${extraContext}` : "");
return runCodexPrompt(task);
}
async function gitCommitAndPush(branch: string, customMessage?: string): Promise<boolean> {
console.log("\n[step] Git add/commit/push...");
await run(["git", "add", "-A"], { cwd: REPO_PATH });
const commitMsg = customMessage || "chore: auto-fix by codex based on Vercel build logs";
const commit = await run(["git", "commit", "-m", commitMsg], { cwd: REPO_PATH });
if (commit.code !== 0) {
if (commit.stdout.toLowerCase().includes("nothing to commit") || commit.stderr.toLowerCase().includes("nothing to commit")) {
console.log("[info] Nothing to commit. Skipping push.");
return false;
}
throw new Error(`git commit failed:\n${commit.stdout}\n${commit.stderr}`);
}
console.log("[info] Commit created:");
console.log(commit.stdout);
const push = await run(["git", "push", GIT_REMOTE, branch], { cwd: REPO_PATH });
if (push.code !== 0) {
throw new Error(`git push failed:\n${push.stdout}\n${push.stderr}`);
}
console.log("[info] git push completed.");
return true;
}
async function runPreflight(): Promise<{ ok: boolean; output: string }> {
if (!RUN_PREFLIGHT || PREFLIGHT_COMMANDS.length === 0) {
return { ok: true, output: "[info] Preflight disabled (RUN_PREFLIGHT!=1 or no commands)." };
}
console.log("\n[step] Running local preflight checks...");
let output = "";
for (const cmd of PREFLIGHT_COMMANDS) {
console.log(`[preflight] ${cmd.join(" ")}`);
const res = await run(cmd, { cwd: REPO_PATH, streamOutput: true });
const combined = `${res.stdout}\n${res.stderr}`.trim();
output += `\n\n## ${cmd.join(" ")}\n\n${combined}\n`;
if (res.code !== 0) {
return { ok: false, output };
}
}
return { ok: true, output };
}
function ensureFileDir(filePath: string) {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
}
function copyIfMissing(src: string, dest: string) {
if (!fs.existsSync(src)) return;
if (fs.existsSync(dest)) return;
ensureFileDir(dest);
fs.copyFileSync(src, dest);
try {
const mode = fs.statSync(src).mode;
fs.chmodSync(dest, mode);
} catch {
// ignore chmod errors
}
}
function ensureRuntimeAssets() {
fs.mkdirSync(RUNTIME_ROOT, { recursive: true });
fs.mkdirSync(LOGS_ROOT, { recursive: true });
fs.mkdirSync(MCP_RUNTIME_DIR, { recursive: true });
if (fs.existsSync(TEMPLATE_MCP_DIR)) {
copyIfMissing(path.join(TEMPLATE_MCP_DIR, "playwright.config.json"), path.join(MCP_RUNTIME_DIR, "playwright.config.json"));
copyIfMissing(path.join(TEMPLATE_MCP_DIR, "run-gui-check.sh"), path.join(MCP_RUNTIME_DIR, "run-gui-check.sh"));
copyIfMissing(path.join(TEMPLATE_MCP_DIR, "gui-routes.json"), MCP_GUI_ROUTES_PATH);
}
}
function sanitizeBaseUrl(raw?: string): string {
if (!raw) return "http://localhost:3000";
const trimmed = raw.trim();
if (!trimmed) return "http://localhost:3000";
if (/^https?:\/\//i.test(trimmed)) return trimmed.replace(/\/$/, "");
return `https://${trimmed.replace(/^\/+/, "").replace(/\/$/, "")}`;
}
function resolveFullUrl(baseUrl: string, routePath: string): string {
try {
return new URL(routePath, baseUrl).toString();
} catch {
const normalizedBase = baseUrl.replace(/\/$/, "");
const normalizedPath = routePath.replace(/^\//, "");
return `${normalizedBase}/${normalizedPath}`;
}
}
function normalizeGuiRoute(entry: string | GuiRouteEntry, index: number, defaultWaitMs: number): GuiRouteSpec | null {
if (typeof entry === "string") {
const pathValue = entry.trim();
if (!pathValue) return null;
return {
path: pathValue,
name: `Route ${pathValue}`,
waitMs: defaultWaitMs,
};
}
if (!entry || typeof entry.path !== "string") return null;
const trimmedPath = entry.path.trim();
if (!trimmedPath) return null;
return {
path: trimmedPath,
name: entry.name?.trim() || `Route ${trimmedPath}`,
description: entry.description?.trim() || undefined,
waitMs: entry.waitMs ?? defaultWaitMs,
};
}
async function autoGenerateGuiSpecs(targetUrl?: string) {
if (!RUN_MCP_GUI) return;
if (!fs.existsSync(MCP_GUI_ROUTES_PATH)) {
const sample = {
baseUrl: targetUrl || PROD_URL || "https://your-app.vercel.app",
defaultWaitMs: 3000,
routes: ["/"],
};
ensureFileDir(MCP_GUI_ROUTES_PATH);
fs.writeFileSync(MCP_GUI_ROUTES_PATH, JSON.stringify(sample, null, 2), "utf-8");
console.log(`[info] Created sample GUI routes config at ${MCP_GUI_ROUTES_PATH}; customize it for your app.`);
}
let configRaw: GuiRoutesConfig | null = null;
try {
configRaw = JSON.parse(fs.readFileSync(MCP_GUI_ROUTES_PATH, "utf-8")) as GuiRoutesConfig;
} catch (err) {
console.warn(`[warn] Failed to parse ${MCP_GUI_ROUTES_PATH}:`, err);
return;
}
const routesInput = configRaw?.routes || [];
if (routesInput.length === 0) {
console.warn(`[warn] GUI routes config at ${MCP_GUI_ROUTES_PATH} has no routes; skipping spec generation.`);
return;
}
const defaultWaitMs = configRaw?.defaultWaitMs ?? 3000;
const normalizedRoutes = routesInput
.map((entry, idx) => normalizeGuiRoute(entry, idx, defaultWaitMs))
.filter((r): r is GuiRouteSpec => Boolean(r));
if (normalizedRoutes.length === 0) {
console.warn(`[warn] GUI routes config at ${MCP_GUI_ROUTES_PATH} did not yield any valid entries.`);
return;
}
const baseUrl = sanitizeBaseUrl(configRaw?.baseUrl || targetUrl || PROD_URL || "http://localhost:3000");
const generatedAt = new Date().toISOString();
const mdParts: string[] = [];
mdParts.push("# Auto-generated GUI intent spec for MCP Playwright");
mdParts.push("");
mdParts.push(`- Generated: ${generatedAt}`);
mdParts.push(`- Base URL: ${baseUrl}`);
mdParts.push(`- Routes file: ${path.relative(REPO_PATH, MCP_GUI_ROUTES_PATH)}`);
mdParts.push("");
mdParts.push("## Scenarios");
mdParts.push("");
const plan = {
generatedAt,
baseUrl,
defaultWaitMs,
scenarios: normalizedRoutes.map((route, index) => ({
index: index + 1,
name: route.name,
path: route.path,
url: resolveFullUrl(baseUrl, route.path),
waitMs: route.waitMs,
description: route.description,
})),
};
for (const [idx, route] of normalizedRoutes.entries()) {
const waitSeconds = (route.waitMs / 1000).toFixed(1).replace(/\.0$/, "");
mdParts.push(`### Scenario ${idx + 1}: ${route.name}`);
mdParts.push(`- Route: ${route.path}`);
mdParts.push(`- URL: ${resolveFullUrl(baseUrl, route.path)}`);
if (route.description) {
mdParts.push(`- Goal: ${route.description}`);
}
mdParts.push("- Steps:");
mdParts.push(" 1. Start a fresh browser session (clear cookies/localStorage).");
mdParts.push(" 2. Navigate to the route URL.");
mdParts.push(" 3. Systematically interact with visible inputs using placeholder text.");
mdParts.push(` 4. Click each prominent button/CTA once; after every click, wait ~${waitSeconds}s to observe the UI.`);
mdParts.push(" 5. If a modal or secondary flow appears, interact once then return to the primary page.");
mdParts.push("- Failure conditions:");
mdParts.push(" - UI becomes unresponsive longer than the wait window.");
mdParts.push(" - Navigation errors (blank pages, 404, crash screens).");
mdParts.push(" - Required interactive controls are missing or disabled.");
mdParts.push(" - Obvious error modals or crash overlays.");
mdParts.push("");
}
ensureFileDir(MCP_GUI_TESTS_PATH);
fs.writeFileSync(MCP_GUI_TESTS_PATH, mdParts.join("\n"), "utf-8");
ensureFileDir(MCP_GUI_PLAN_PATH);
fs.writeFileSync(MCP_GUI_PLAN_PATH, JSON.stringify(plan, null, 2), "utf-8");
}
async function runGuiCheck(targetUrl?: string): Promise<GuiCheckOutcome | null> {
if (!RUN_MCP_GUI) {
console.log("[info] RUN_MCP_GUI=0 — skipping GUI checks.");
return null;
}
const resolvedTarget = targetUrl || PROD_URL;
if (!resolvedTarget) {
console.log("[info] No target URL available for GUI checks. Set PROD_URL or pass MCP_GUI_TARGET_URL.");
return null;
}
ensureRuntimeAssets();
await autoGenerateGuiSpecs(resolvedTarget);
fs.mkdirSync(MCP_GUI_OUTPUT_DIR, { recursive: true });
if (!fs.existsSync(MCP_GUI_SCRIPT_PATH)) {
console.log(`[info] GUI runner script not found at ${MCP_GUI_SCRIPT_PATH}. Skipping GUI checks.`);
return null;
}
console.log("\n[step] Running MCP Playwright GUI checks...");
const env = { ...process.env };
env.MCP_GUI_TARGET_URL = resolvedTarget;
env.MCP_GUI_LOG_DIR = MCP_GUI_OUTPUT_DIR;
const res = await run(["bash", MCP_GUI_SCRIPT_PATH, resolvedTarget], {
cwd: REPO_PATH,
env,
streamOutput: true,
});
const combined = `${res.stdout}\n${res.stderr}`.trim();
ensureAutofixDir();
fs.writeFileSync(
MCP_GUI_REPORT_PATH,
`# MCP Playwright GUI report\n\nRun: ${new Date().toISOString()}\nTarget: ${resolvedTarget}\nLogs directory: ${MCP_GUI_OUTPUT_DIR}\n\n\`\`\`\n${combined}\n\`\`\`\n`,
"utf-8",
);
const ok = res.code === 0 && !/FAIL/i.test(combined);
return {
ok,
report: combined || "[no report produced]",
logPath: MCP_GUI_REPORT_PATH,
targetUrl: resolvedTarget,
};
}
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// =========================
// MAIN LOOP
// =========================
async function main() {
ensureRuntimeAssets();
console.log("[start] Vercel ↔ Codex auto-fix loop (TypeScript)");
console.log(`Repo: ${REPO_PATH}`);
const branch = await resolveGitBranch();
console.log(`Branch: ${branch}`);
console.log(`URL: ${PROD_URL}`);
console.log(`MAX_ITERATIONS: ${MAX_ITERATIONS}`);
const synced = await maybePushAheadCommits(branch);
if (!synced) {
console.log("[stop] Exiting before Codex loop because local commits were not pushed.");
return;
}
let state = loadState();
if (INITIAL_TASK) {
console.log("\n[init] Initial CLI task detected:");
console.log(` ${INITIAL_TASK}`);
const initialChanged = await runInitialCodexTask(INITIAL_TASK);
if (!initialChanged) {
console.log("[info] Initial task produced no repo changes. Continuing to build-log loop.");
} else {
const preflight = await runPreflight();
if (RUN_PREFLIGHT && !preflight.ok) {
console.log("[warn] Preflight failed after initial task; asking Codex to revise the patch.");
const constraints =
"\nAdditional constraints:\n" +
"- Complete the initial CLI request without breaking preflight commands.\n" +
"- Keep prior fixes intact unless absolutely necessary.\n" +
"\nPreflight output:\n```text\n" +
preflight.output.slice(-12000) +
"\n```\n";
const retryChanged = await runInitialCodexTask(INITIAL_TASK, constraints);
if (!retryChanged) {
console.log("[info] Codex could not resolve the initial task preflight issues. Stopping.");
return;
}
const preflightRetry = await runPreflight();
if (!preflightRetry.ok) {
console.log("[warn] Preflight still failing after retry. Stopping to avoid regressions.");
return;
}
}
const commitMsg = buildInitialCommitMessage(INITIAL_TASK);
const pushed = await gitCommitAndPush(branch, commitMsg);
if (!pushed) {
console.log("[info] Initial task yielded nothing to push. Continuing to build-log loop.");
} else {
console.log(`[info] Sleeping ${SLEEP_AFTER_PUSH_SECONDS}s while Vercel builds...`);
await sleep(SLEEP_AFTER_PUSH_SECONDS * 1000);
}
}
}
for (let i = 1; i <= MAX_ITERATIONS; i++) {
console.log("\n==============================");
console.log(`Iteration ${i}/${MAX_ITERATIONS}`);
console.log("==============================");
const { logs, deploymentUrl } = await fetchLatestBuildLogs();
if (!logs.trim()) {
console.log("[warn] No logs found. Sleeping and retrying...");
await sleep(SLEEP_AFTER_PUSH_SECONDS * 1000);
continue;
}
let effectiveLogs = logs;
let extraCodexContext = "";
let logSource: "vercel" | "gui" = "vercel";
let finalSuccess = false;
let guiOutcome: GuiCheckOutcome | null = null;
const deployLooksSuccessful = buildLooksSuccessful(logs);
if (deployLooksSuccessful) {
guiOutcome = await runGuiCheck(deploymentUrl || PROD_URL);
if (!guiOutcome || guiOutcome.ok) {
finalSuccess = true;
} else {
console.warn("[warn] GUI checks failed after a successful deployment.");
effectiveLogs = guiOutcome.report;
logSource = "gui";
extraCodexContext =
"GUI regression detected by MCP Playwright after deployment.\n" +
`Target URL: ${guiOutcome.targetUrl}\n` +
`GUI report saved to ${guiOutcome.logPath}\n` +
`GUI plan: ${MCP_GUI_PLAN_PATH}\n` +
`Artifacts directory: ${MCP_GUI_OUTPUT_DIR}\n` +
"Analyze the report, inspect the trace/video in the logs directory, and patch the application so the scenario passes.";
}
}
const rawSigs = extractErrorSignatures(effectiveLogs);
const sigSet = new Set(rawSigs);
if (logSource === "gui") {
sigSet.add("GUI_REGRESSION");
}
const sigs = Array.from(sigSet);
let score = scoreFromLogs(effectiveLogs);
if (logSource === "gui") {
score += 5000;
}
console.log(`[info] Score=${score}, signatures=${sigs.join(", ")}`);
const regression = sigs.some((x) => state.resolvedSignatures.includes(x));
if (regression) {
console.warn("[warn] Regression detected: previously resolved signature reappeared.");
}
state.iteration = state.iteration + 1;
state.seenSignatures = Array.from(new Set([...state.seenSignatures, ...sigs])).slice(-200);
state.lastScore = score;
saveState(state);
if (finalSuccess) {
console.log("[info] Build looks successful 🎉");
if (guiOutcome) {
console.log("[info] MCP Playwright GUI checks also passed ✅");
} else if (RUN_MCP_GUI) {
console.log("[info] GUI checks skipped (script missing or target URL unavailable).");
} else {
console.log("[info] GUI checks disabled (RUN_MCP_GUI=0).");
}
state = loadState();
state.resolvedSignatures = Array.from(new Set([...state.resolvedSignatures, ...state.seenSignatures])).slice(-200);
saveState(state);
console.log("[done] Stopping loop.");
return;
}
const changed = await runCodexOnLogs(effectiveLogs, extraCodexContext);
if (!changed) {
console.log("[info] Codex has no further changes. Stopping loop.");
return;
}
const preflight = await runPreflight();
if (RUN_PREFLIGHT && !preflight.ok) {
console.log("[warn] Preflight failed; asking Codex to fix without regressions.");
const constraints =
"\nAdditional constraints:\n" +
`- Do NOT reintroduce previously seen error signatures: ${state.resolvedSignatures.join(", ") || "none"}\n` +
"- Do NOT revert previous fixes unless absolutely necessary.\n" +
"- After changes, ensure `pnpm -C apps/web exec tsc --noEmit --pretty false` passes.\n" +
"\nPreflight output:\n```text\n" +
preflight.output.slice(-12000) +
"\n```\n";
const retryChanged = await runCodexOnLogs(effectiveLogs, constraints);
if (!retryChanged) {
console.log("[info] Codex could not fix preflight. Stopping.");
return;
}
const preflightRetry = await runPreflight();
if (!preflightRetry.ok) {
console.log("[warn] Preflight still failing after retry. Stopping to avoid regressions.");
return;
}
}
const pushed = await gitCommitAndPush(branch);
if (!pushed) {
console.log("[info] No code changes actually pushed. Stopping loop.");
return;
}
console.log(`[info] Sleeping ${SLEEP_AFTER_PUSH_SECONDS}s while Vercel builds...`);
await sleep(SLEEP_AFTER_PUSH_SECONDS * 1000);