liu
4 天以前 aa624335b1ac67222c42eb8b5b81e73b6b96d32d
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
#!/usr/bin/env node
/**
 * Live variant mode server (self-contained, zero dependencies).
 *
 * Serves the browser script (/live.js), the detection overlay (/detect.js),
 * uses Server-Sent Events (SSE) for server→browser push, and HTTP POST for
 * browser→server events. Agent communicates via HTTP long-poll (/poll).
 *
 * Usage:
 *   node <scripts_path>/live-server.mjs              # start
 *   node <scripts_path>/live-server.mjs stop         # stop + remove injected live.js tag
 *   node <scripts_path>/live-server.mjs stop --keep-inject   # stop only
 *   node <scripts_path>/live-server.mjs --help
 */
 
import http from 'node:http';
import { randomUUID } from 'node:crypto';
import { spawn, execFileSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import net from 'node:net';
import { fileURLToPath } from 'node:url';
import { parseDesignMd } from './lib/design-parser.mjs';
import { loadContext } from './context.mjs';
import {
  assembleLiveBrowserScript,
  assertLiveBrowserScriptParts,
  readLiveBrowserScriptParts,
  resolveLiveBrowserScriptParts,
} from './live/browser-script-parts.mjs';
import { createLiveSessionStore, GENERATION_FENCED_PHASES } from './live/session-store.mjs';
import { runGenerationPreflight } from './live/generation-preflight.mjs';
import { validateEvent } from './live/event-validation.mjs';
import { selectAvailablePendingEvent } from './live/poll-lanes.mjs';
import { createManualEditRoutes } from './live/manual-edit-routes.mjs';
import {
  LIVE_COMMANDS,
  VARIANT_PROGRESS_CHECKPOINT_REASONS as VARIANT_PROGRESS_CHECKPOINT_REASON_LIST,
} from './live/vocabulary.mjs';
import {
  getDesignSidecarPath,
  getLiveDir,
  getLiveAnnotationsDir,
  IMPECCABLE_COMMAND_PREFIX,
  readLiveServerInfo,
  removeLiveServerInfo,
  resolveDesignSidecarPath,
  writeLiveServerInfo,
} from './lib/impeccable-paths.mjs';
import { countByPage as countPendingByPage } from './live/manual-edits-buffer.mjs';
import {
  createManualApplyController,
  summarizeManualApplyFailures,
} from './live/manual-apply.mjs';
import {
  applyDeferredSvelteComponentAccepts,
  bumpSvelteComponentPreviewRevision,
  compileCheckVariants,
  removeAllSvelteComponentSessions,
  sweepInactiveSvelteComponentSessions,
} from './live/svelte-component.mjs';
import { enterLiveRoot } from './live/roots.mjs';
 
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Anchor the whole process on the live roots manifest before anything derives
// a path from cwd. A server started from the wrong directory re-roots itself
// onto the appRoot the boot decided on instead of minting a second project.
const LIVE_ROOTS = enterLiveRoot(process.cwd());
 
// PRODUCT.md / DESIGN.md context, resolved lazily and per request so a server
// that outlives an `impeccable document` run (or a context file created after
// boot) reports current truth instead of a boot-time snapshot. The roots
// manifest wins when the ambient resolution misses (nested app inheriting
// repo-level context files).
function resolveProjectContext() {
  const ctx = loadContext(process.cwd());
  const designPath = ctx.designPath
    ? path.resolve(process.cwd(), ctx.designPath)
    : (LIVE_ROOTS?.designPath && fs.existsSync(LIVE_ROOTS.designPath) ? LIVE_ROOTS.designPath : null);
  const hasProduct = ctx.hasProduct
    || !!(LIVE_ROOTS?.productPath && fs.existsSync(LIVE_ROOTS.productPath));
  return {
    ...ctx,
    hasProduct,
    hasDesign: !!designPath,
    resolvedDesignPath: designPath,
    contextDir: ctx.contextDir || LIVE_ROOTS?.contextRoot || process.cwd(),
    designContextDir: ctx.designContextDir
      || (designPath ? path.dirname(designPath) : null),
  };
}
const DEFAULT_POLL_TIMEOUT = 600_000;   // 10 min — agent re-polls on timeout anyway
const SSE_HEARTBEAT_INTERVAL = 30_000;  // keepalive ping every 30s
 
// The browser events allowed to mint a NEW session journal. `generate` starts
// a variant session at Go; `steer` mints its own request id. Every other
// id-carrying event must land on an existing session (see the unknown_session
// gate in the /events handler).
const SESSION_CREATING_EVENT_TYPES = new Set(['generate', 'steer']);
// The browser checkpoints for several unrelated reasons (see checkpointPayload
// in live-browser.js). Only these two report that variant availability changed,
// and only they may drive variant_progress / the *_reviewable phases.
const VARIANT_PROGRESS_CHECKPOINT_REASONS = new Set(VARIANT_PROGRESS_CHECKPOINT_REASON_LIST);
 
// ---------------------------------------------------------------------------
// Port detection
// ---------------------------------------------------------------------------
 
async function findOpenPort(start = 8400) {
  return new Promise((resolve) => {
    const srv = net.createServer();
    srv.listen(start, '127.0.0.1', () => {
      const port = srv.address().port;
      srv.close(() => resolve(port));
    });
    srv.on('error', () => resolve(findOpenPort(start + 1)));
  });
}
 
// ---------------------------------------------------------------------------
// Session state
// ---------------------------------------------------------------------------
 
const state = {
  token: null,
  port: null,
  sseClients: new Set(),   // SSE response objects (server→browser push)
  pendingEvents: [],        // browser events waiting for agent ack ({ event, leaseUntil })
  pendingPolls: [],         // agent poll callbacks waiting for browser events
  nextEventSeq: 1,
  lastAgentPollingBroadcast: null,
  exitTimer: null,
  sessionDir: null,         // per-session tmp dir for annotation screenshots
  sessionStore: null,
  leaseTimer: null,
  manualEditActivity: null,
  nextManualEditSeq: 1,
  // Deferreds for in-flight chat-routed Apply events. Keyed by event id; each
  // entry is resolved when the chat agent POSTs an ack carrying the batch
  // result, or rejected when the hard timeout fires.
  pendingApplyDeferreds: new Map(),
  // Updated whenever a /poll long-poll request arrives or is resolved with an
  // event. Used to detect "a chat agent is likely attached" without requiring
  // a poll to be parked at the exact moment we dispatch.
  lastPollAt: 0,
  timedOutApplyIds: new Map(),
};
 
const CHAT_POLL_FRESHNESS_MS = 60_000;
const POLL_LEASE_EXPIRY_TIMER_GRACE_MS = 2;
const DEBUG_MANUAL_EDIT_EVENTS = /^(1|true|yes)$/i.test(process.env.IMPECCABLE_LIVE_DEBUG_EVENTS || '');
 
const manualApply = createManualApplyController({
  pendingEvents: state.pendingEvents,
  pendingApplyDeferreds: state.pendingApplyDeferreds,
  timedOutApplyIds: state.timedOutApplyIds,
  enqueueEvent,
  acknowledgePendingEvent,
  flushPendingPolls,
  recordManualEditActivity,
  cwd: () => process.cwd(),
});
 
const manualEditRoutes = createManualEditRoutes({
  getToken: () => state.token,
  manualApply,
  recordManualEditActivity,
  getManualEditStatus,
  chatAgentLikelyActive,
  cwd: () => process.cwd(),
  env: () => process.env,
});
 
function chatAgentLikelyActive() {
  if (state.pendingPolls.length > 0) return true;
  if (!state.lastPollAt) return false;
  return Date.now() - state.lastPollAt < CHAT_POLL_FRESHNESS_MS;
}
 
// Cap per-annotation upload size. A full 1920×1080 PNG is typically <1 MB;
// cap at 10 MB to guard against runaway writes from a misbehaving client.
const MAX_ANNOTATION_BYTES = 10 * 1024 * 1024;
 
function enqueueEvent(event) {
  if (!event) return;
  // Dedupe by (session, type), except mount failures, which are per-variant:
  // variant 2 failing must not be swallowed because variant 1's failure is
  // still queued.
  const duplicate = event.id && state.pendingEvents.some((entry) => (
    entry.event?.id === event.id
    && entry.event?.type === event.type
    && (event.type !== 'variant_mount_failed' || entry.event?.variant === event.variant)
  ));
  if (duplicate) return;
  state.pendingEvents.push({ event, leaseUntil: 0, seq: state.nextEventSeq++ });
  flushPendingPolls();
}
 
function restorePendingEventsFromStore() {
  if (!state.sessionStore) return;
  for (const snapshot of state.sessionStore.listActiveSessions()) {
    if (snapshot.pendingEvent) enqueueEvent(snapshot.pendingEvent);
  }
}
 
function findAvailablePendingEvent(now = Date.now(), types = null) {
  return selectAvailablePendingEvent(state.pendingEvents, { now, types });
}
 
async function leaseEvent(entry, leaseMs) {
  // Claim the entry before awaiting anything. prepareGenerateEventForLease
  // yields to the event loop, and selectAvailablePendingEvent only skips
  // entries whose lease is in the future — an unclaimed entry would be handed
  // to a second poll in that window and generated twice.
  entry.leaseUntil = Date.now() + leaseMs;
  await prepareGenerateEventForLease(entry);
  if (!entry.event?.id) {
    const idx = state.pendingEvents.indexOf(entry);
    if (idx !== -1) state.pendingEvents.splice(idx, 1);
    return entry.event;
  }
  // Re-stamp so the lease window starts when the agent actually receives the
  // work, not when scaffolding began.
  entry.leaseUntil = Date.now() + leaseMs;
  recordGenerateDelivery(entry);
  scheduleLeaseFlush();
  broadcastAgentPollingIfChanged();
  return entry.event;
}
 
function recordGenerateDelivery(entry) {
  const event = entry?.event;
  if (!event || event.type !== 'generate' || event.generationReadyAt) return;
  const at = Date.now();
  entry.event = { ...event, generationReadyAt: at };
  state.sessionStore?.appendEvent(entry.event);
  recordAgentPhase(event.id, 'generation_ready', { at });
}
 
async function prepareGenerateEventForLease(entry) {
  const event = entry?.event;
  if (!event || event.type !== 'generate' || event.scaffoldAttempted) return;
 
  recordAgentPhase(event.id, 'picked_up');
  recordAgentPhase(event.id, 'scaffolding');
  const result = await runGenerationPreflight(event, {
    cwd: process.cwd(),
    scriptsDir: __dirname,
  });
  entry.event = {
    ...event,
    scaffoldAttempted: true,
    scaffoldDurationMs: result.durationMs ?? null,
    ...(result.ok ? { scaffold: result.scaffold } : { scaffoldError: result.error || result.reason }),
  };
  state.sessionStore?.appendEvent(entry.event);
  recordAgentPhase(event.id, result.ok ? 'source_ready' : 'scaffold_fallback', {
    durationMs: result.durationMs ?? null,
    previewMode: result.scaffold?.previewMode || 'source',
  });
}
 
function recordAgentPhase(id, phase, details = {}) {
  if (!id) return;
  const event = {
    type: 'agent_phase',
    id,
    phase,
    at: Date.now(),
    ...details,
  };
  state.sessionStore?.appendEvent(event);
  broadcast(event);
}
 
/**
 * Detect a browser that missed the generation `done` broadcast.
 *
 * The preflight no longer writes the scaffold into source for source-preview
 * targets (the agent writes wrapper + variants in one atomic edit), so the old
 * scaffold-write full-reload that opened the "stranded at 0/N" race is gone.
 * This recovery stays as defense in depth: any framework reload that drops the
 * agent's variant write + `done` while the browser is mid-reload leaves the new
 * page in GENERATING at 0/N. That resumed page always checkpoints
 * (`browser_resumed`), so a checkpoint claiming "still generating, variants
 * missing" for a session whose generation already completed is direct
 * evidence of the miss. Rebuild the `done` payload from the snapshot so the
 * caller can re-broadcast it; the browser's done handler is idempotent and
 * falls back to injecting variants from source.
 *
 * Keys on the store's monotone `generationCompletedAt`, not `phase` — the
 * behind checkpoint itself regresses `phase` to `generating`, and a browser
 * that misses the redelivered `done` too (another reload) must still trigger
 * redelivery from its next checkpoint.
 */
function detectMissedGenerationCompletion(event) {
  if (!event?.id || event.type !== 'checkpoint') return null;
  if (event.phase !== 'generating') return null;
  if (!variantCountLooksBehind(event.arrivedVariants, event.expectedVariants)) return null;
  if (!state.sessionStore) return null;
  let snapshot = null;
  try {
    snapshot = state.sessionStore.getSnapshot(event.id);
  } catch {
    return null;
  }
  return missedCompletionFromSnapshot(snapshot);
}
 
function variantCountLooksBehind(arrivedValue, expectedValue) {
  const arrived = Number(arrivedValue) || 0;
  const expected = Number(expectedValue) || 0;
  return arrived <= 0 || (expected > 0 && arrived < expected);
}
 
function missedCompletionFromSnapshot(snapshot) {
  if (!snapshot?.id || !snapshot.generationCompletedAt) return null;
  if (snapshot.generationCanceled) return null;
  // Accept/discard already underway: the browser is no longer waiting on
  // generation, and a late `done` there would collide with teardown.
  if (GENERATION_FENCED_PHASES.has(snapshot.phase)) return null;
  const file = snapshot.sourceFile || snapshot.previewFile;
  if (!file) return null;
  return {
    type: 'done',
    id: snapshot.id,
    file,
    sourceFile: snapshot.sourceFile || undefined,
    previewFile: snapshot.previewFile || undefined,
    previewMode: snapshot.previewMode || undefined,
    redelivered: true,
  };
}
 
function recordGenerationCheckpoint(event) {
  if (!event?.id || event.type !== 'checkpoint') return;
  if (generationIsFenced(event.id)) return;
  // Only checkpoints that report a change in variant availability are
  // generation progress. The browser also checkpoints for durability on Tune
  // slider drags, resumes, and anchor recovery; treating those as progress
  // echoed `variant_progress` straight back to the browser that sent it, which
  // remounts the component preview mid-drag (reverting the user's live param
  // edit and detaching the popover's element), and permanently latched the
  // *_reviewable phases from the wrong trigger, corrupting generation timings.
  if (!VARIANT_PROGRESS_CHECKPOINT_REASONS.has(event.reason)) return;
  const arrived = Number(event.arrivedVariants) || 0;
  const expected = Number(event.expectedVariants) || 0;
  if (arrived <= 0 || expected <= 0) return;
  const previewMode = event.previewMode || 'source';
  const previewFile = event.previewFile || event.file;
  if (previewFile) {
    broadcast({
      type: 'variant_progress',
      id: event.id,
      file: previewFile,
      sourceFile: event.sourceFile || (previewMode === 'source' ? previewFile : undefined),
      previewFile,
      previewMode,
      arrivedVariants: arrived,
      expectedVariants: expected,
      publicationKind: event.publicationKind || 'variants',
    });
  }
  const details = {
    arrivedVariants: arrived,
    expectedVariants: expected,
    checkpointReason: event.reason || null,
  };
  const at = Date.now();
  if (!generationPhaseAlreadyRecorded(event.id, 'first_reviewable')) {
    recordAgentPhase(event.id, 'first_reviewable', { ...details, at });
  }
  if (arrived >= 2 && expected >= 3 && !generationPhaseAlreadyRecorded(event.id, 'second_reviewable')) {
    recordAgentPhase(event.id, 'second_reviewable', { ...details, at });
  }
  if (arrived >= expected && !generationPhaseAlreadyRecorded(event.id, 'all_variants_ready')) {
    recordAgentPhase(event.id, 'all_variants_ready', { ...details, at });
  }
}
 
function generationIsFenced(id) {
  if (!state.sessionStore || !id) return false;
  try {
    const snapshot = state.sessionStore.getSnapshot(id, { includeCompleted: true });
    return snapshot?.generationCanceled === true;
  } catch {
    return false;
  }
}
 
function generationPhaseAlreadyRecorded(id, phase) {
  if (!state.sessionStore) return false;
  try {
    const snapshot = state.sessionStore.getSnapshot(id, { includeCompleted: true });
    return !!snapshot?.generationTimings?.[phase];
  } catch {
    return false;
  }
}
 
function acknowledgePendingEvent(id, sourceEventType) {
  if (!id) return false;
  const idx = state.pendingEvents.findIndex((entry) => (
    entry.event?.id === id
    && (!sourceEventType || entry.event?.type === sourceEventType)
  ));
  if (idx === -1) return false;
  const acknowledged = state.pendingEvents[idx].event;
  state.pendingEvents.splice(idx, 1);
  scheduleLeaseFlush();
  broadcastAgentPollingIfChanged();
  return acknowledged;
}
 
function releasePendingEvent(id, sourceEventType) {
  const entry = state.pendingEvents.find((item) => (
    item.event?.id === id
    && (!sourceEventType || item.event?.type === sourceEventType)
  ));
  if (!entry) return null;
  entry.leaseUntil = 0;
  scheduleLeaseFlush();
  return entry.event;
}
 
function retirePendingGeneration(id) {
  if (!id) return 0;
  let retired = 0;
  for (let index = state.pendingEvents.length - 1; index >= 0; index -= 1) {
    const event = state.pendingEvents[index]?.event;
    if (event?.id !== id || event.type !== 'generate') continue;
    state.pendingEvents.splice(index, 1);
    retired += 1;
  }
  if (retired > 0) {
    scheduleLeaseFlush();
    broadcastAgentPollingIfChanged();
  }
  return retired;
}
 
function findPendingEventById(id, sourceEventType) {
  if (!id) return null;
  const entry = state.pendingEvents.find((item) => (
    item.event?.id === id
    && (!sourceEventType || item.event?.type === sourceEventType)
  ));
  return entry?.event || null;
}
 
function summarizePendingEventForStatus(entry) {
  const event = entry.event || {};
  const summary = {
    id: event.id,
    type: event.type,
    leased: isLeased(entry),
    leaseUntil: entry.leaseUntil || null,
  };
  if (event.type === 'manual_edit_apply') {
    summary.pageUrl = event.pageUrl || null;
    summary.chunk = event.chunk || null;
    summary.repair = event.repair || null;
    summary.evidencePath = event.evidencePath || null;
    summary.agentAction = event.agentAction || manualApply.buildAgentAction(event);
    summary.manualApplySummary = manualApply.summarizeEvent(event, manualApply.getDeferred(event.id)?.batch || event.batch);
  }
  return summary;
}
 
function summarizeActiveSessionForClient(snapshot = {}) {
  return {
    id: snapshot.id,
    phase: snapshot.phase,
    pageUrl: snapshot.pageUrl ?? null,
    sourceFile: snapshot.sourceFile ?? null,
    previewFile: snapshot.previewFile ?? null,
    previewMode: snapshot.previewMode ?? null,
    expectedVariants: snapshot.expectedVariants ?? 0,
    arrivedVariants: snapshot.arrivedVariants ?? 0,
    visibleVariant: snapshot.visibleVariant ?? null,
    checkpointRevision: snapshot.checkpointRevision ?? 0,
    browserCheckpointRevision: snapshot.browserCheckpointRevision ?? snapshot.checkpointRevision ?? 0,
    publicationCheckpointRevision: snapshot.publicationCheckpointRevision ?? 0,
    paramValues: snapshot.paramValues || {},
    generationPhase: snapshot.generationPhase ?? null,
    generationCompletedAt: snapshot.generationCompletedAt ?? null,
    generationCanceled: snapshot.generationCanceled === true,
    cancelReason: snapshot.cancelReason ?? null,
    // Render truth, so a browser with no localStorage can rehydrate to the
    // same comparison the server already knows about.
    mountedVariants: Array.isArray(snapshot.mountedVariants) ? snapshot.mountedVariants : [],
    mountFailures: Array.isArray(snapshot.mountFailures) ? snapshot.mountFailures : [],
    renderState: snapshot.renderState ?? null,
  };
}
 
function activeSessionSummaries() {
  if (!state.sessionStore) return [];
  return state.sessionStore.listActiveSessions().map((snapshot) => summarizeActiveSessionForClient(snapshot));
}
 
function cancelQueuedAnonymousExitEvents() {
  let removed = 0;
  for (let i = state.pendingEvents.length - 1; i >= 0; i -= 1) {
    const event = state.pendingEvents[i]?.event;
    if (event?.type !== 'exit' || event.id) continue;
    state.pendingEvents.splice(i, 1);
    removed += 1;
  }
  if (removed > 0) {
    scheduleLeaseFlush();
    broadcastAgentPollingIfChanged();
  }
  return removed;
}
 
function scheduleLeaseFlush() {
  if (state.leaseTimer) {
    clearTimeout(state.leaseTimer);
    state.leaseTimer = null;
  }
  const now = Date.now();
  const nextLeaseUntil = state.pendingEvents
    .map((entry) => entry.leaseUntil || 0)
    .filter((leaseUntil) => leaseUntil > now)
    .sort((a, b) => a - b)[0];
  if (!nextLeaseUntil) return;
  state.leaseTimer = setTimeout(() => {
    state.leaseTimer = null;
    flushPendingPolls();
    broadcastAgentPollingIfChanged();
  }, Math.max(0, nextLeaseUntil - now + POLL_LEASE_EXPIRY_TIMER_GRACE_MS));
}
 
function flushPendingPolls() {
  let changed = false;
  while (state.pendingPolls.length > 0) {
    let pollIndex = -1;
    let entry = null;
    for (let index = 0; index < state.pendingPolls.length; index += 1) {
      const candidate = findAvailablePendingEvent(Date.now(), state.pendingPolls[index].types);
      if (!candidate) continue;
      pollIndex = index;
      entry = candidate;
      break;
    }
    if (!entry) {
      scheduleLeaseFlush();
      broadcastAgentPollingIfChanged();
      return;
    }
    const [poll] = state.pendingPolls.splice(pollIndex, 1);
    // leaseEvent is async (it may scaffold source), but it claims the entry
    // synchronously, so the next loop iteration will not re-select it. Resolve
    // the poll when the lease settles rather than awaiting here, so one slow
    // scaffold never delays the other parked polls. On the exceptional failure
    // path, answer `timeout` so the agent re-polls; the claim stays until the
    // lease expires, which keeps a deterministic failure from hot-looping.
    leaseEvent(entry, poll.leaseMs).then(poll.resolve, (error) => {
      console.error('[live] lease failed for ' + (entry.event?.id || 'unknown') + ': ' + (error?.message || error));
      poll.resolve({ type: 'timeout' });
    });
    changed = true;
  }
  scheduleLeaseFlush();
  if (changed) broadcastAgentPollingIfChanged();
}
 
function isLeased(entry) {
  return !!(entry?.leaseUntil && entry.leaseUntil > Date.now());
}
 
function agentPollingConnected() {
  // A leased event only proves that a poll returned once. The foreground task
  // may have ended immediately afterward, so only an actively waiting poll is
  // evidence that steering can wake the task right now.
  return state.pendingPolls.length > 0;
}
 
function broadcastAgentPollingIfChanged() {
  const connected = agentPollingConnected();
  if (state.lastAgentPollingBroadcast === connected) return;
  state.lastAgentPollingBroadcast = connected;
  broadcast({ type: 'agent_polling', connected });
}
 
/** Push a message to all connected SSE clients. */
function broadcast(msg) {
  const data = 'data: ' + JSON.stringify(msg) + '\n\n';
  for (const res of state.sseClients) {
    try { res.write(data); } catch { /* client gone */ }
  }
}
 
function recordManualEditActivity(type, details = {}) {
  const entry = {
    seq: state.nextManualEditSeq++,
    type,
    ts: new Date().toISOString(),
    ...details,
  };
  state.manualEditActivity = entry;
  if (DEBUG_MANUAL_EDIT_EVENTS) {
    try {
      const filePath = path.join(getLiveDir(process.cwd()), 'manual-edit-events.jsonl');
      fs.mkdirSync(path.dirname(filePath), { recursive: true });
      fs.appendFileSync(filePath, JSON.stringify(entry) + '\n');
    } catch {
      /* diagnostics are best-effort; never block live mode on observability */
    }
  }
  broadcast(entry);
  return entry;
}
 
function getManualEditStatus() {
  try {
    const { totalCount, perPage } = countPendingByPage(process.cwd());
    return { totalCount, perPage, lastActivity: state.manualEditActivity };
  } catch (err) {
    return {
      totalCount: null,
      perPage: {},
      lastActivity: state.manualEditActivity,
      error: err.message,
    };
  }
}
 
// ---------------------------------------------------------------------------
// Load scripts
// ---------------------------------------------------------------------------
 
function loadBrowserScripts() {
  // Detection script: prefer the skill-bundled detector, then fall back to
  // source/npm package locations for local development and older installs.
  // This one IS cached — detect.js rarely changes during a session.
  const detectPaths = [
    path.join(__dirname, 'detector', 'detect-antipatterns-browser.js'),
    path.join(__dirname, '..', '..', 'cli', 'engine', 'detect-antipatterns-browser.js'),
    path.join(__dirname, '..', '..', '..', '..', 'cli', 'engine', 'detect-antipatterns-browser.js'),
    path.join(process.cwd(), 'node_modules', 'impeccable', 'cli', 'engine', 'detect-antipatterns-browser.js'),
  ];
  let detectScript = '';
  for (const p of detectPaths) {
    try { detectScript = fs.readFileSync(p, 'utf-8'); break; } catch { /* try next */ }
  }
 
  // Browser script parts: DO NOT cache. Return paths so the /live.js handler
  // can re-read every part on each request. Editing browser code during
  // iteration should land on the next tab reload, not require a server restart.
  const liveScriptParts = resolveLiveBrowserScriptParts(__dirname);
  try {
    assertLiveBrowserScriptParts(liveScriptParts);
  } catch (err) {
    process.stderr.write('Error: ' + err.message + '\n');
    process.exit(1);
  }
 
  return { detectScript, liveScriptParts };
}
 
function hasProjectContext() {
  // PRODUCT.md carries brand voice / anti-references — that's what determines
  // whether variants are brand-aware. DESIGN.md (visual tokens) is a separate
  // concern, surfaced by the design panel's own empty state.
  return !!resolveProjectContext().hasProduct;
}
 
function statOrNull(filePath) {
  try { return fs.statSync(filePath); } catch { return null; }
}
 
// Strict loopback-origin test for CORS. Parses the Origin as a URL (never a
// substring match, so `http://localhost.evil.com` and `http://127.0.0.1.evil.com`
// fail) and accepts only http/https on localhost, 127.0.0.1, or the IPv6 loopback.
function isLoopbackOrigin(origin) {
  if (typeof origin !== 'string' || origin.length === 0) return false;
  let parsed;
  try { parsed = new URL(origin); } catch { return false; }
  if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return false;
  const host = parsed.hostname.toLowerCase();
  return host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]';
}
 
// HTTP request handler
// ---------------------------------------------------------------------------
 
function createRequestHandler({ detectScript, liveScriptParts }) {
  return (req, res) => {
    const url = new URL(req.url, `http://localhost:${state.port}`);
    // Token-or-loopback CORS. Reflect the caller's Origin when it is a
    // loopback origin OR the request carries the valid session token, always
    // paired with `Vary: Origin` so an intermediary cache never serves a
    // response authorized for one origin to another. A remote page (e.g.
    // https://evil.example probing the port from a tab open on the same
    // machine) has no token and gets no Access-Control-Allow-Origin, so its
    // JS-initiated fetch cannot read any response. The token branch exists for
    // dev servers on non-localhost loopback aliases (ddev's *.ddev.site,
    // Valet's *.test, hosts-file entries): the injected classic <script src>
    // delivers the token to the page regardless of origin, every overlay
    // request carries it in the query string (preflights included, since
    // OPTIONS hits the same URL), and a token bearer is already fully
    // authorized on every route — the token is the security boundary, not the
    // origin. Requests with no Origin header (script tags, curl, the agent's
    // own fetches) are not subject to CORS and keep working; no ACAO header
    // is needed for them.
    const origin = req.headers.origin;
    if (origin && (isLoopbackOrigin(origin) || url.searchParams.get('token') === state.token)) {
      res.setHeader('Access-Control-Allow-Origin', origin);
      res.setHeader('Vary', 'Origin');
    }
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
    if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
 
    const p = url.pathname;
 
    // --- Scripts ---
    if (p === '/live.js') {
      // Token-gated: the script body embeds state.token, which unlocks every
      // token-guarded route. Serving it unauthenticated let any local page read
      // the token and drive the session. The injected <script src> carries
      // `?token=...` (see live-inject.mjs). A missing/wrong token → 401.
      if (url.searchParams.get('token') !== state.token) {
        res.writeHead(401, { 'Content-Type': 'text/plain' });
        res.end('Unauthorized');
        return;
      }
      // Re-read from disk each request so edits to live-browser.js land on
      // the next tab reload. No-store headers prevent browser caching across
      // sessions — during iteration, a cached old script silently breaks
      // every subsequent session.
      let parts;
      try {
        parts = readLiveBrowserScriptParts(liveScriptParts);
      } catch (err) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('Error reading live browser scripts: ' + err.message);
        return;
      }
      const body = assembleLiveBrowserScript({
        token: state.token,
        port: state.port,
        vocabulary: LIVE_COMMANDS,
        commandPrefix: IMPECCABLE_COMMAND_PREFIX,
        appRoot: process.cwd(),
        parts,
      });
      res.writeHead(200, {
        'Content-Type': 'application/javascript',
        'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
        'Pragma': 'no-cache',
      });
      res.end(body);
      return;
    }
    if (p === '/detect.js' || p === '/') {
      if (!detectScript) { res.writeHead(404); res.end('Not available'); return; }
      res.writeHead(200, { 'Content-Type': 'application/javascript' });
      res.end(detectScript);
      return;
    }
 
    // --- Vendored modern-screenshot (UMD build) ---
    // Lazy-loaded by live.js when the user clicks Go; exposes
    // window.modernScreenshot.domToBlob(...) for capture.
    if (p === '/modern-screenshot.js') {
      const vendorPath = path.join(__dirname, 'modern-screenshot.umd.js');
      try {
        res.writeHead(200, {
          'Content-Type': 'application/javascript',
          'Cache-Control': 'public, max-age=31536000, immutable',
        });
        res.end(fs.readFileSync(vendorPath));
      } catch {
        res.writeHead(404); res.end('Vendor script not found');
      }
      return;
    }
 
    // --- Annotation upload (browser → server, raw PNG body) ---
    // Client generates the eventId, POSTs the PNG, then POSTs the generate
    // event with screenshotPath already set. Keeps bytes out of the SSE/poll
    // bridge and preserves the "one shot from the user's POV" UX.
    if (p === '/annotation' && req.method === 'POST') {
      const token = url.searchParams.get('token');
      if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
      const eventId = url.searchParams.get('eventId');
      if (!eventId || !/^[A-Za-z0-9_-]{1,64}$/.test(eventId)) {
        res.writeHead(400, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ error: 'Invalid eventId' }));
        return;
      }
      if ((req.headers['content-type'] || '').toLowerCase() !== 'image/png') {
        res.writeHead(415, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ error: 'Content-Type must be image/png' }));
        return;
      }
      if (!state.sessionDir) {
        res.writeHead(500, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ error: 'Session dir unavailable' }));
        return;
      }
      const chunks = [];
      let total = 0;
      let aborted = false;
      req.on('data', (c) => {
        if (aborted) return;
        total += c.length;
        if (total > MAX_ANNOTATION_BYTES) {
          aborted = true;
          res.writeHead(413, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'Payload too large' }));
          req.destroy();
          return;
        }
        chunks.push(c);
      });
      req.on('end', () => {
        if (aborted) return;
        const absPath = path.join(state.sessionDir, eventId + '.png');
        try {
          fs.writeFileSync(absPath, Buffer.concat(chunks));
        } catch (err) {
          res.writeHead(500, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'Write failed: ' + err.message }));
          return;
        }
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ ok: true, path: absPath }));
      });
      req.on('error', () => {
        if (!aborted) {
          res.writeHead(500, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'Upload failed' }));
        }
      });
      return;
    }
 
    // --- Health ---
    if (p === '/status') {
      const token = url.searchParams.get('token');
      if (token !== state.token) { res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Unauthorized' })); return; }
      const sessions = activeSessionSummaries();
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        status: 'ok',
        port: state.port,
        connectedClients: state.sseClients.size,
        pendingEvents: state.pendingEvents.map((entry) => summarizePendingEventForStatus(entry)),
        agentPolling: agentPollingConnected(),
        activeSessions: sessions,
        manualEdits: getManualEditStatus(),
      }));
      return;
    }
 
    if (p === '/health') {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        status: 'ok', port: state.port, mode: 'variant',
        hasProjectContext: hasProjectContext(),
        connectedClients: state.sseClients.size,
      }));
      return;
    }
 
    // --- Design system (unified v2 response) + raw ---
    //   /design-system.json    returns both parsed DESIGN.md and .impeccable/design.json
    //                          sidecar when present. Panel merges them:
    //                            { present, parsed, sidecar, hasMd, hasSidecar,
    //                              mdNewerThanJson, parseError?, sidecarError? }
    //                          - parsed: output of parseDesignMd (frontmatter
    //                            + the canonical sections) when DESIGN.md exists.
    //                          - sidecar: .impeccable/design.json contents when present.
    //                            Expected shape: schemaVersion 2, carrying
    //                            extensions + components + narrative.
    //   /design-system/raw     returns DESIGN.md markdown verbatim
    if (p === '/design-system.json' || p === '/design-system/raw') {
      const token = url.searchParams.get('token');
      if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
 
      const projectContext = resolveProjectContext();
      const mdPath = projectContext.resolvedDesignPath;
      const jsonPath = resolveDesignSidecarPath(process.cwd(), projectContext.designContextDir || projectContext.contextDir) || getDesignSidecarPath(process.cwd());
      const mdStat = statOrNull(mdPath);
      const jsonStat = statOrNull(jsonPath);
 
      if (p === '/design-system/raw') {
        if (!mdStat) { res.writeHead(404); res.end('Not found'); return; }
        res.writeHead(200, { 'Content-Type': 'text/markdown; charset=utf-8' });
        res.end(fs.readFileSync(mdPath, 'utf-8'));
        return;
      }
 
      if (!mdStat && !jsonStat) {
        res.writeHead(404, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ present: false }));
        return;
      }
 
      const response = {
        present: true,
        hasMd: !!mdStat,
        hasSidecar: !!jsonStat,
        mdNewerThanJson: !!(mdStat && jsonStat && mdStat.mtimeMs > jsonStat.mtimeMs + 1000),
      };
 
      if (mdStat) {
        try {
          response.parsed = parseDesignMd(fs.readFileSync(mdPath, 'utf-8'));
        } catch (err) {
          response.parseError = err.message;
        }
      }
 
      if (jsonStat) {
        try {
          response.sidecar = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
        } catch (err) {
          response.sidecarError = 'Failed to parse .impeccable/design.json: ' + err.message;
        }
      }
 
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify(response));
      return;
    }
 
    // --- Source file (no-HMR fallback) ---
    if (p === '/source') {
      const token = url.searchParams.get('token');
      if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
      const filePath = url.searchParams.get('path');
      if (!filePath || filePath.includes('..')) { res.writeHead(400); res.end('Bad path'); return; }
      const absPath = path.resolve(process.cwd(), filePath);
      // Confine to the project root. A bare `startsWith(cwd)` string check lets a
      // sibling dir whose name extends the root name (projeto -> projeto-backup)
      // slip through; compare on the relative path instead (same pattern as
      // sessionFileMetadataFromPollReply below). An empty rel means the request
      // resolved to the root directory itself, which this file route never serves.
      const rel = path.relative(process.cwd(), absPath);
      if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) { res.writeHead(403); res.end('Forbidden'); return; }
      let content;
      try { content = fs.readFileSync(absPath, 'utf-8'); }
      catch { res.writeHead(404); res.end('File not found'); return; }
      res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
      res.end(content);
      return;
    }
 
    // --- SSE: server→browser push (replaces WebSocket) ---
    if (p === '/events' && req.method === 'GET') {
      const token = url.searchParams.get('token');
      if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
      clearTimeout(state.exitTimer);
      state.exitTimer = null;
      cancelQueuedAnonymousExitEvents();
      res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
      });
      res.write('data: ' + JSON.stringify({
        type: 'connected',
        hasProjectContext: hasProjectContext(),
        agentPolling: agentPollingConnected(),
        activeSessions: activeSessionSummaries(),
      }) + '\n\n');
 
      state.sseClients.add(res);
 
      // Keepalive: SSE comment every 30s prevents silent connection drops.
      const heartbeat = setInterval(() => {
        try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); }
      }, SSE_HEARTBEAT_INTERVAL);
 
      req.on('close', () => {
        clearInterval(heartbeat);
        state.sseClients.delete(res);
        if (state.sseClients.size === 0) {
          clearTimeout(state.exitTimer);
          state.exitTimer = setTimeout(() => {
            if (state.sseClients.size === 0) enqueueEvent({ type: 'exit' });
          }, 8000);
        }
      });
      return;
    }
 
    if (manualEditRoutes(req, res, url)) return;
 
    // --- Browser→server events (replaces WebSocket messages) ---
    if (p === '/events' && req.method === 'POST') {
      let body = '';
      req.on('data', (c) => { body += c; });
      req.on('end', () => {
        let msg;
        try { msg = JSON.parse(body); } catch {
          res.writeHead(400, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'Invalid JSON' }));
          return;
        }
        if (msg.token !== state.token) {
          res.writeHead(401, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'Unauthorized' }));
          return;
        }
        // Defense in depth: manual copy edits must use the staged stash/apply
        // endpoints. The direct Save event path is disabled in the browser.
        if (msg.type === 'manual_edits') {
          res.writeHead(400, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'manual_edits must POST to /manual-edit-stash, not /events' }));
          return;
        }
        if (msg.type === 'manual_edit_apply') {
          res.writeHead(400, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'manual_edit_apply is disabled; use /manual-edit-stash then /manual-edit-commit' }));
          return;
        }
        const error = validateEvent(msg);
        if (error) {
          res.writeHead(400, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error }));
          return;
        }
        if (msg.type === 'agent_phase') {
          recordAgentPhase(msg.id, msg.phase, {
            ...(Number.isFinite(msg.durationMs) ? { durationMs: msg.durationMs } : {}),
            owner: typeof msg.owner === 'string' ? msg.owner : undefined,
          });
          res.writeHead(200, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ ok: true }));
          return;
        }
        // Only the events that START a session may create its journal.
        // Everything else (checkpoints, mount acks, accept/discard) must
        // reference a session THIS store already knows: appendEvent creates a
        // journal for any id it is handed, so without this gate a browser
        // resuming another project's session from per-origin storage (two
        // apps sharing a localhost port) materializes a ghost session here
        // that keeps reattaching after every discard.
        if (msg.id && state.sessionStore
            && !SESSION_CREATING_EVENT_TYPES.has(msg.type)
            && !state.sessionStore.has(msg.id)) {
          res.writeHead(404, { 'Content-Type': 'application/json' });
          res.end(JSON.stringify({ error: 'unknown_session', id: msg.id }));
          return;
        }
        const missedCompletion = detectMissedGenerationCompletion(msg);
        if (state.sessionStore && msg.id) {
          try {
            state.sessionStore.appendEvent(msg);
          } catch (err) {
            res.writeHead(500, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({ error: 'session_store_append_failed', message: err.message }));
            return;
          }
        }
        if (msg.type === 'accept' || msg.type === 'discard') {
          retirePendingGeneration(msg.id);
        }
        recordGenerationCheckpoint(msg);
        if (missedCompletion) broadcast(missedCompletion);
        if (msg.type === 'exit') {
          cleanupSvelteComponentSessionsBeforeExit();
        }
        // An ORPHANED discard is the browser reporting that the session's
        // wrapper no longer exists in source (edited or regenerated away).
        // There is no cleanup for an agent to perform, and asking one to run
        // the normal discard flow would just fail against the missing
        // scaffolding, so the server terminalizes the session itself and the
        // event stays out of the poll queue.
        const orphanedDiscard = msg.type === 'discard' && msg.orphaned === true;
        if (orphanedDiscard && state.sessionStore && msg.id) {
          try {
            state.sessionStore.appendEvent({ type: 'discarded', id: msg.id, orphaned: true });
          } catch { /* the discard_requested phase already left the resumable set */ }
        }
        // `variant_mounted` is the happy path: it is journaled above so the
        // snapshot carries render truth, but there is nothing for the agent to
        // do about it, so it stays out of the poll queue and off the SSE bus.
        // `variant_mount_failed` is the opposite: the agent published something
        // the browser could not render, and only the agent can fix it, so it
        // goes to the queue as a first-class event.
        if (msg.type !== 'checkpoint' && msg.type !== 'variant_mounted' && !orphanedDiscard) {
          enqueueEvent(msg);
        }
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ ok: true }));
      });
      return;
    }
 
    // --- Stop ---
    if (p === '/stop') {
      const token = url.searchParams.get('token');
      if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('stopping');
      shutdown();
      return;
    }
 
    // --- Agent poll ---
    if (p === '/poll' && req.method === 'GET') {
      handlePollGet(req, res, url);
      return;
    }
    if (p === '/poll' && req.method === 'POST') {
      handlePollPost(req, res);
      return;
    }
 
    res.writeHead(404); res.end('Not found');
  };
}
 
// ---------------------------------------------------------------------------
// Agent poll endpoints (unchanged from WS version)
// ---------------------------------------------------------------------------
 
function parsePollTypes(value) {
  if (!value) return null;
  const types = String(value).split(',').map((type) => type.trim()).filter(Boolean);
  return types.length > 0 ? new Set(types) : null;
}
 
function handlePollGet(req, res, url) {
  const token = url.searchParams.get('token');
  if (token !== state.token) {
    res.writeHead(401, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ error: 'Unauthorized' }));
    return;
  }
  state.lastPollAt = Date.now();
  const timeout = parseInt(url.searchParams.get('timeout') || DEFAULT_POLL_TIMEOUT, 10);
  const leaseMs = parseInt(url.searchParams.get('leaseMs') || '30000', 10);
  const types = parsePollTypes(url.searchParams.get('types'));
  const available = findAvailablePendingEvent(Date.now(), types);
  if (available) {
    // Do not await inline: leaseEvent may scaffold source, and this handler runs
    // on the server's only thread. The client can disconnect during that window,
    // so check the socket before replying.
    leaseEvent(available, leaseMs).then((event) => {
      if (res.writableEnded || res.destroyed) return;
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify(event));
    }, (error) => {
      console.error('[live] lease failed for ' + (available.event?.id || 'unknown') + ': ' + (error?.message || error));
      if (res.writableEnded || res.destroyed) return;
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ type: 'timeout' }));
    });
    return;
  }
  const poll = { resolve, leaseMs, types };
  const timer = setTimeout(() => {
    const idx = state.pendingPolls.indexOf(poll);
    if (idx !== -1) state.pendingPolls.splice(idx, 1);
    broadcastAgentPollingIfChanged();
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ type: 'timeout' }));
  }, timeout);
  function resolve(event) {
    clearTimeout(timer);
    state.lastPollAt = Date.now();
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify(event));
  }
  state.pendingPolls.push(poll);
  broadcastAgentPollingIfChanged();
  scheduleLeaseFlush();
  req.on('close', () => {
    clearTimeout(timer);
    const idx = state.pendingPolls.indexOf(poll);
    if (idx !== -1) state.pendingPolls.splice(idx, 1);
    broadcastAgentPollingIfChanged();
  });
}
 
function sessionFileMetadataFromPollReply(file) {
  if (!file || typeof file !== 'string') return { file };
  const normalized = file.split(path.sep).join('/');
  const base = { file: normalized };
  const metadataFile = normalized;
  if (!metadataFile.endsWith('/manifest.json') && metadataFile !== 'manifest.json') return base;
  if (!metadataFile.includes('.impeccable/live/previews/')
      && !metadataFile.includes('node_modules/.impeccable-live/')
      && !metadataFile.includes('src/lib/impeccable/')
      && !metadataFile.includes('/.impeccable-live/')) return base;
 
  let full;
  try {
    full = path.resolve(process.cwd(), metadataFile);
    const rel = path.relative(process.cwd(), full);
    if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) return base;
  } catch {
    return base;
  }
 
  try {
    const manifest = JSON.parse(fs.readFileSync(full, 'utf-8'));
    if (manifest?.previewMode !== 'svelte-component'
        || !manifest.sourceFile) return base;
    return {
      file: String(manifest.sourceFile).split(path.sep).join('/'),
      sourceFile: String(manifest.sourceFile).split(path.sep).join('/'),
      previewFile: normalized,
      previewMode: manifest.previewMode,
    };
  } catch {
    return base;
  }
}
 
function inferSourceEventType(msg = {}, pendingEvents = state.pendingEvents) {
  const entriesForId = pendingEvents.filter((entry) => entry.event?.id === msg.id);
  const pendingTypes = new Set(entriesForId.map((entry) => entry.event?.type));
  if (msg.type === 'discarded' || msg.type === 'discard') return 'discard';
  if (msg.type === 'complete') {
    if (pendingTypes.has('carbonize_cleanup')) return 'carbonize_cleanup';
    return pendingTypes.has('accept') ? 'accept' : (pendingTypes.has('generate') ? 'generate' : undefined);
  }
  if (msg.type === 'steer_done') return 'steer';
  // `agent_done` can be the automatic acknowledgement for a carbonize Accept.
  // New pollers send sourceEventType explicitly; default to generate only for
  // older callers so a late worker cannot acknowledge a queued Accept.
  if (msg.type === 'agent_done' || msg.type === 'done') {
    // A `done` reply to a mount failure is the republish that unblocks the
    // browser. Without this the ack would look for a `generate` that was
    // already retired, the mount-failure event would stay queued, and the next
    // poll would hand the same failure back to the agent forever.
    if (!pendingTypes.has('generate') && pendingTypes.has('variant_mount_failed')) return 'variant_mount_failed';
    return 'generate';
  }
  // `error` is reference/live.md's documented failure reply, and parseReplyArgs
  // never sets sourceEventType on it (the poller is a fresh process that cannot
  // know what it leased). Returning undefined here makes acknowledgePendingEvent
  // match *any* event for this id: a stale generate worker's failure silently
  // consumed the user's queued Accept, which was then never delivered to any
  // agent and left the browser in SAVING forever. Attribute the failure to the
  // event this agent actually holds a lease on, and otherwise to `generate` —
  // never to a wildcard. If that generate was already retired by an Accept, the
  // ack simply finds no match, which is the correct outcome for a stale reply.
  if (msg.type === 'error') {
    return entriesForId.find(isLeased)?.event?.type || 'generate';
  }
  return undefined;
}
 
function handlePollPost(req, res) {
  let body = '';
  req.on('data', (c) => { body += c; });
  req.on('end', () => {
    let msg;
    try { msg = JSON.parse(body); } catch {
      res.writeHead(400, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ error: 'Invalid JSON' }));
      return;
    }
    if (msg.token !== state.token) {
      res.writeHead(401, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ error: 'Unauthorized' }));
      return;
    }
    const pendingApplyDeferred = manualApply.getDeferred(msg.id);
    if (pendingApplyDeferred) {
      const validation = manualApply.validateResultMessage(msg, pendingApplyDeferred);
      if (!validation.ok) {
        recordManualEditActivity('manual_edit_apply_reply_invalid', {
          id: msg.id,
          pageUrl: pendingApplyDeferred.pageUrl,
          chunk: pendingApplyDeferred.event?.chunk || null,
          repair: pendingApplyDeferred.event?.repair || null,
          reason: validation.body?.reason || validation.body?.error || 'invalid_manual_apply_result',
          status: msg.data?.status || null,
        });
        res.writeHead(400, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify(validation.body));
        return;
      }
      recordManualEditActivity('manual_edit_apply_reply_received', {
        id: msg.id,
        pageUrl: pendingApplyDeferred.pageUrl,
        chunk: pendingApplyDeferred.event?.chunk || null,
        repair: pendingApplyDeferred.event?.repair || null,
        status: validation.result.status,
        appliedCount: validation.result.appliedEntryIds.length,
        failed: summarizeManualApplyFailures(validation.result.failed),
        fileCount: validation.result.files.length,
        noteCount: validation.result.notes.length,
      });
      manualApply.resolveDeferred(msg.id, validation.result);
      acknowledgePendingEvent(msg.id);
      flushPendingPolls();
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ ok: true }));
      return;
    }
    if (manualApply.hasTimedOutId(msg.id)) {
      const rollback = manualApply.rollbackTimedOutReply(msg);
      recordManualEditActivity('manual_edit_apply_stale_reply_rejected', {
        id: msg.id,
        rolledBackFileCount: rollback.rolledBackFiles?.length || 0,
        rollbackFailureCount: rollback.rollbackFailures?.length || 0,
      });
      res.writeHead(409, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ error: 'stale_manual_edit_apply_reply', ...rollback }));
      return;
    }
    const sourceEventType = msg.sourceEventType || inferSourceEventType(msg);
    if (msg.type === 'retry') {
      const releasedEvent = releasePendingEvent(msg.id, sourceEventType);
      if (!releasedEvent) {
        res.writeHead(msg.id ? 404 : 400, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({
          error: msg.id ? 'unknown_poll_retry_id' : 'missing_poll_retry_id',
          id: msg.id,
        }));
        return;
      }
      flushPendingPolls();
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({ ok: true, released: true }));
      return;
    }
    const pendingEventBeforeAck = findPendingEventById(msg.id, sourceEventType);
    if (pendingEventBeforeAck?.type === 'steer' && msg.type === 'steer_done'
        && !msg.file && !(typeof msg.message === 'string' && msg.message.trim())) {
      res.writeHead(400, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        error: 'steer_done_requires_file_or_message',
        hint: 'Reply with --file after writing source, or include a message explaining an intentional no-op.',
      }));
      return;
    }
    const acknowledgedEvent = acknowledgePendingEvent(msg.id, sourceEventType);
    let skipJournalReply = false;
    let existingSession = null;
    if (!acknowledgedEvent && state.sessionStore && msg.id) {
      try {
        existingSession = state.sessionStore.getSnapshot(msg.id, { includeCompleted: true });
        if (!existingSession?.updatedAt) existingSession = null;
        skipJournalReply = existingSession?.phase === 'completed' || existingSession?.phase === 'discarded';
      } catch { /* fall through and record the reply normally */ }
    }
    if (!acknowledgedEvent && !existingSession) {
      recordManualEditActivity('manual_edit_poll_reply_unknown', {
        id: msg.id || null,
        type: msg.type || null,
      });
      res.writeHead(msg.id ? 404 : 400, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        error: msg.id ? 'unknown_poll_reply_id' : 'missing_poll_reply_id',
        id: msg.id,
      }));
      return;
    }
    const replyFileMeta = sessionFileMetadataFromPollReply(msg.file);
    // A publish (done reply carrying a component manifest) snapshots the
    // variant files into a fresh revision dir before the browser is told:
    // the import path changes every publish, so no transform cache can pin a
    // stale compile of a republished module (node_modules is unwatched).
    // Broken variants are bounced HERE, before the browser imports anything:
    // a compile error that reaches the page is a red overlay in the user's
    // face; bounced at publish it is a private fix with file and line.
    if (replyFileMeta.previewMode === 'svelte-component'
        && msg.id
        && (msg.type === 'done' || !msg.type)) {
      let compileCheck = { ok: true, failures: [] };
      try { compileCheck = compileCheckVariants(msg.id, process.cwd()); } catch { /* best-effort */ }
      if (!compileCheck.ok) {
        res.writeHead(422, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({
          error: 'variant_compile_failed',
          id: msg.id,
          failures: compileCheck.failures,
          _instructions: 'The publish was NOT delivered: the listed variant file(s) do not compile, so the browser never saw them. Fix each failure at the given file and line (the most common cause is a second top-level <style> element; Svelte allows exactly one, so merge all rules into the existing block), then send the same --reply done again.',
        }));
        return;
      }
      try { bumpSvelteComponentPreviewRevision(msg.id, process.cwd()); } catch { /* best-effort */ }
    }
    if (state.sessionStore && msg.id && !skipJournalReply) {
      try {
        const eventType = msg.type === 'steer_done'
          ? 'steer_done'
          : msg.type === 'discard' || msg.type === 'discarded'
            ? 'discarded'
            : msg.type === 'complete'
              ? 'complete'
              : msg.type === 'error'
                ? 'agent_error'
                : 'agent_done';
        state.sessionStore.appendEvent({
          type: eventType,
          id: msg.id,
          file: replyFileMeta.file,
          sourceFile: replyFileMeta.sourceFile,
          previewFile: replyFileMeta.previewFile,
          previewMode: replyFileMeta.previewMode,
          message: msg.message,
          sourceEventType: acknowledgedEvent?.type,
          carbonize: msg.data?.carbonize === true,
        });
      } catch { /* keep reply path best-effort; browser still needs SSE */ }
    }
    flushPendingPolls();
    // Forward the reply to the browser via SSE
    broadcast({
      type: msg.type || 'done',
      id: msg.id,
      message: msg.message,
      file: msg.file,
      sourceFile: replyFileMeta.sourceFile,
      previewFile: replyFileMeta.previewFile,
      previewMode: replyFileMeta.previewMode,
      data: msg.data,
    });
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ ok: true }));
  });
}
 
// ---------------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------------
 
let httpServer = null;
 
function shutdown() {
  cleanupSvelteComponentSessionsBeforeExit();
  removeLiveServerInfo(process.cwd());
  if (state.leaseTimer) clearTimeout(state.leaseTimer);
  state.leaseTimer = null;
  if (state.sessionDir) {
    try { fs.rmSync(state.sessionDir, { recursive: true, force: true }); } catch {}
  }
  for (const res of state.sseClients) { try { res.end(); } catch {} }
  state.sseClients.clear();
  for (const poll of state.pendingPolls) poll.resolve({ type: 'exit' });
  state.pendingPolls.length = 0;
  if (httpServer) httpServer.close();
  process.exit(0);
}
 
function cleanupSvelteComponentSessionsBeforeExit() {
  try {
    removeAllSvelteComponentSessions(process.cwd());
  } catch (err) {
    console.warn('[impeccable] Svelte component session cleanup failed:', err.message);
  }
}
 
/**
 * A previous run that died without its shutdown hook leaves preview component
 * dirs behind. Drop the ones whose session the store no longer considers
 * active; anything still active is mid-generation and must survive a restart.
 */
function sweepOrphanSvelteComponentSessionsOnStartup() {
  try {
    const activeIds = (state.sessionStore?.listActiveSessions() || [])
      .map((snapshot) => snapshot?.id)
      .filter(Boolean);
    const result = sweepInactiveSvelteComponentSessions(activeIds, process.cwd());
    if (result.removed.length > 0 || result.removedRoot) {
      console.log('[impeccable] swept orphaned Svelte component sessions:', JSON.stringify(result));
    }
  } catch (err) {
    console.warn('[impeccable] Svelte component session sweep failed:', err.message);
  }
}
 
// Accept receipts are a short-lived idempotency record for a single accept.
// Nothing reads one after the session that wrote it is gone, so they only need
// to outlive a crash-and-retry window.
const ACCEPT_RECEIPT_MAX_AGE_MS = 14 * 24 * 60 * 60 * 1000;
 
function sweepStaleAcceptReceiptsOnStartup() {
  try {
    const dir = path.join(getLiveDir(process.cwd()), 'accept-receipts');
    if (!fs.existsSync(dir)) return;
    const cutoff = Date.now() - ACCEPT_RECEIPT_MAX_AGE_MS;
    let removed = 0;
    for (const name of fs.readdirSync(dir)) {
      if (!name.endsWith('.json') && !name.endsWith('.tmp')) continue;
      const file = path.join(dir, name);
      try {
        if (fs.statSync(file).mtimeMs >= cutoff) continue;
        fs.rmSync(file, { force: true });
        removed++;
      } catch { /* non-fatal */ }
    }
    if (removed > 0) console.log(`[impeccable] removed ${removed} accept receipt(s) older than 14 days`);
  } catch (err) {
    console.warn('[impeccable] accept receipt retention sweep failed:', err.message);
  }
}
 
function applyLegacyDeferredAcceptsOnStartup() {
  try {
    const result = applyDeferredSvelteComponentAccepts(process.cwd());
    if (result.applied > 0 || result.failed > 0) {
      console.log('[impeccable] applied legacy deferred Svelte component accepts:', JSON.stringify(result));
    }
  } catch (err) {
    console.warn('[impeccable] legacy deferred Svelte component accept apply failed:', err.message);
  }
}
 
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
 
const args = process.argv.slice(2);
 
if (args.includes('--help') || args.includes('-h')) {
  console.log(`Usage: node live-server.mjs [options]
 
Start the live variant mode server (zero dependencies).
 
Commands:
  (default)     Start the server (foreground)
  stop          Stop the server and remove the injected live.js script tag
  stop --keep-inject   Stop the server only (leave the script tag in the HTML entry)
 
Options:
  --background  Start detached, print connection JSON to stdout, then exit
  --port=PORT   Use a specific port (default: auto-detect starting at 8400)
  --keep-inject Only with stop: skip live-inject.mjs --remove
  --help        Show this help
 
Endpoints:
  /live.js             Browser script (element picker + variant cycling)
  /detect.js           Detection overlay (backwards compatible)
  /modern-screenshot.js Vendored modern-screenshot UMD build (lazy-loaded by live.js)
  /annotation          POST raw image/png to stage a variant screenshot
  /events              SSE stream (server→browser) + POST (browser→server)
  /poll                Long-poll for agent CLI
  /manual-edit-stash   Stage browser copy edits
  /manual-edit-commit  Apply staged browser copy edits
  /manual-edit-discard Discard staged browser copy edits
  /source              Raw source file reader (no-HMR fallback)
  /status              Durable recovery status (token-protected)
  /health              Health check`);
  process.exit(0);
}
 
if (args.includes('stop')) {
  const keepInject = args.includes('--keep-inject');
  try {
    const { info } = readLiveServerInfo(process.cwd()) || {};
    const res = await fetch(`http://localhost:${info.port}/stop?token=${info.token}`);
    if (res.ok) console.log(`Stopped live server on port ${info.port}.`);
  } catch {
    console.log('No running live server found.');
  }
  if (!keepInject) {
    const injectPath = path.join(__dirname, 'live-inject.mjs');
    try {
      const out = execFileSync(process.execPath, [injectPath, '--remove'], {
        encoding: 'utf-8',
        cwd: process.cwd(),
      });
      const line = out.trim().split('\n').filter(Boolean).pop();
      if (line) {
        try {
          const j = JSON.parse(line);
          if (j.removed === true) {
            console.log(`Removed live script tag from ${j.file}.`);
          }
        } catch {
          /* ignore non-JSON lines */
        }
      }
    } catch (err) {
      const detail = err.stderr?.toString?.().trim?.()
        || err.stdout?.toString?.().trim?.()
        || err.message
        || String(err);
      console.warn(`Note: could not remove live script tag (${detail.split('\n')[0]})`);
    }
  }
  process.exit(0);
}
 
// --background: spawn a detached child server, wait for it to be ready,
// print the connection JSON, then exit.  This keeps the startup command
// simple (no shell backgrounding or chained commands).
if (args.includes('--background')) {
  const childArgs = args.filter(a => a !== '--background');
  const child = spawn(process.execPath, [fileURLToPath(import.meta.url), ...childArgs], {
    detached: true,
    stdio: 'ignore',
    cwd: process.cwd(),
  });
  child.unref();
 
  // Poll for the PID file (the child writes it once the HTTP server is listening).
  const deadline = Date.now() + 10_000;
  while (Date.now() < deadline) {
    try {
      const { info } = readLiveServerInfo(process.cwd()) || {};
      if (info.pid !== process.pid) {
        // Output JSON so the agent can read port + token from stdout.
        console.log(JSON.stringify(info));
        process.exit(0);
      }
    } catch { /* not ready yet */ }
    // The detached child is typically listening in 35-45ms. A 200ms polling
    // floor dominated configured cold Live startup; poll cheaply and return
    // as soon as the child has written its ready record.
    await new Promise(r => setTimeout(r, 5));
  }
  console.error('Timed out waiting for live server to start.');
  process.exit(1);
}
 
// Check for existing session
const existingRecord = readLiveServerInfo(process.cwd());
if (existingRecord?.info) {
  const existing = existingRecord.info;
  try {
    process.kill(existing.pid, 0);
    console.error(`Live server already running on port ${existing.port} (pid ${existing.pid}).`);
    console.error('Stop it first with: node ' + path.basename(fileURLToPath(import.meta.url)) + ' stop');
    process.exit(1);
  } catch {
    try { fs.unlinkSync(existingRecord.path); } catch {}
  }
}
 
state.token = randomUUID();
state.sessionStore = createLiveSessionStore({ cwd: process.cwd() });
manualApply.rollbackTransaction({
  reason: 'manual_edit_server_start_recovered_abandoned_transaction',
});
applyLegacyDeferredAcceptsOnStartup();
sweepOrphanSvelteComponentSessionsOnStartup();
sweepStaleAcceptReceiptsOnStartup();
restorePendingEventsFromStore();
manualApply.pruneStaleEvidence();
const portArg = args.find(a => a.startsWith('--port='));
state.port = portArg ? parseInt(portArg.split('=')[1], 10) : await findOpenPort();
// Annotation screenshots live in the project root so the agent's Read tool
// doesn't trip a per-file permission prompt. Sessioned by token so concurrent
// projects (or quick restarts) don't collide.
const annotRoot = getLiveAnnotationsDir(process.cwd());
fs.mkdirSync(annotRoot, { recursive: true });
state.sessionDir = fs.mkdtempSync(path.join(annotRoot, 'session-'));
 
const { detectScript, liveScriptParts } = loadBrowserScripts();
httpServer = http.createServer(createRequestHandler({ detectScript, liveScriptParts }));
 
httpServer.listen(state.port, '127.0.0.1', () => {
  writeLiveServerInfo(process.cwd(), { pid: process.pid, port: state.port, token: state.token });
  const url = `http://localhost:${state.port}`;
  console.log(`\nImpeccable live server running on ${url}`);
  console.log(`Token: ${state.token}\n`);
  console.log(`Script: ${url}/live.js`);
  console.log('Inject: managed by live-inject.mjs; Astro source tags use is:inline automatically.');
  console.log(`Stop:   node ${path.basename(fileURLToPath(import.meta.url))} stop`);
});
 
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);