@wildshard/engine 0.1.21 → 0.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/lint/engine-sides.json +5 -0
  3. package/lint/shardfile-boundaries.mjs +4 -2
  4. package/lint/sim-players.json +2 -13
  5. package/lint/wildshard-plugin.js +1 -1
  6. package/package.json +10 -2
  7. package/scripts/apfs-clone.mjs +5 -2
  8. package/scripts/app-build/catalogue.ts +39 -8
  9. package/scripts/app-build/serviceWorker.ts +4 -1
  10. package/scripts/app-build/splitOutput.mjs +33 -11
  11. package/scripts/audio-download-proof.mjs +1 -1
  12. package/scripts/check-spelled-paths.mjs +6 -3
  13. package/scripts/ci-pack.mjs +3 -3
  14. package/scripts/committed-fast-checks.mjs +2 -2
  15. package/scripts/engine-ci-shard.mjs +11 -0
  16. package/scripts/failmode-cases.mjs +1 -1
  17. package/scripts/failmode-proof.mjs +35 -11
  18. package/scripts/fight-page-census.mjs +131 -0
  19. package/scripts/goldens/board.py +44 -11
  20. package/scripts/goldens/native-residency.d.mts +1 -0
  21. package/scripts/goldens/native-residency.mjs +18 -4
  22. package/scripts/grid-crossing-walk-proof.mjs +3 -3
  23. package/scripts/hover-speed-probe.mjs +2 -2
  24. package/scripts/keep-copy.mjs +120 -0
  25. package/scripts/land.mjs +36 -6
  26. package/scripts/lane-live.mjs +136 -0
  27. package/scripts/leftovers-report.mjs +47 -0
  28. package/scripts/loading-card-proof.mjs +42 -9
  29. package/scripts/loading-flow/benchmax.mjs +57 -7
  30. package/scripts/loading-flow/stalls.d.mts +2 -0
  31. package/scripts/loading-flow/stalls.mjs +13 -0
  32. package/scripts/mmo-bots.mjs +11 -5
  33. package/scripts/mmo-grid-proof.mjs +74 -0
  34. package/scripts/mmo-seat-proof.mjs +43 -0
  35. package/scripts/net-lag-proof.mjs +1 -1
  36. package/scripts/nightly-heads.ts +13 -13
  37. package/scripts/nightly-model.ts +26 -12
  38. package/scripts/nightly.proof.ts +18 -5
  39. package/scripts/nightly.ts +13 -8
  40. package/scripts/no-astc-boot.mjs +77 -0
  41. package/scripts/online-rig.mjs +7 -7
  42. package/scripts/parity/boot-smoke.mjs +1 -1
  43. package/scripts/physics-route.json +6 -6
  44. package/scripts/pins-reuse.mjs +1 -0
  45. package/scripts/pins-staging.ts +54 -0
  46. package/scripts/pins.ts +5 -0
  47. package/scripts/release-cook.mjs +6 -2
  48. package/scripts/ring3.proof.ts +71 -0
  49. package/scripts/ring3.ts +100 -0
  50. package/scripts/scratch-dir.mjs +31 -12
  51. package/scripts/scratch-sweep.mjs +15 -23
  52. package/scripts/server-release/items.d.mts +7 -0
  53. package/scripts/server-release/items.mjs +28 -0
  54. package/scripts/server-release/publish.mjs +9 -1
  55. package/scripts/side-parity.mjs +1 -1
  56. package/scripts/test-products.json +1 -21
  57. package/scripts/tsconfig.json +2 -0
  58. package/scripts/vitest-ci-files.ts +5 -1
  59. package/scripts/vps/cook-follow.d.mts +6 -0
  60. package/scripts/vps/cook-follow.mjs +45 -0
  61. package/scripts/vps/harden-apply.sh +10 -1
  62. package/scripts/vps/rebuild.sh +104 -36
  63. package/scripts/vps/wildshard-config +106 -3
  64. package/scripts/warm-native-chrome.mjs +9 -3
  65. package/scripts/worker-client-fights.mjs +9 -3
  66. package/scripts/worktree-reap.mjs +15 -131
  67. package/scripts/worktree.mjs +92 -12
  68. package/src/client/app/entry.ts +2 -3
  69. package/src/client/engine/audio/audioLog.ts +7 -2
  70. package/src/client/engine/boot/contentRead.ts +24 -4
  71. package/src/client/engine/boot/diagnostics.ts +69 -1
  72. package/src/client/engine/boot/downloadGiveUp.ts +34 -0
  73. package/src/client/engine/combat/EquipmentService.ts +2 -0
  74. package/src/client/engine/core/Game.ts +6 -2
  75. package/src/client/engine/core/ktx2.ts +5 -3
  76. package/src/client/engine/core/ktx2Zstd.ts +2 -1
  77. package/src/client/engine/pwa/sw.js +26 -8
  78. package/src/client/engine/render/dataLook.ts +14 -7
  79. package/src/client/engine/render/lookLight.ts +64 -20
  80. package/src/client/engine/ui/Loading.ts +6 -8
  81. package/src/client/engine/ui/downloadFailed.ts +54 -0
  82. package/src/client/engine/ui/lookLightOption.ts +1 -1
  83. package/src/client/game/audio/cueDirector.ts +2 -1
  84. package/src/client/game/boot/spawnDownload.ts +12 -1
  85. package/src/client/game/boss/arenaRoom.ts +4 -3
  86. package/src/client/game/boss/roamingProjection.ts +1 -1
  87. package/src/client/game/devScene/hook.ts +1 -1
  88. package/src/client/game/devScene/liveLook.ts +1 -1
  89. package/src/client/game/grid/copyPrompts.ts +3 -4
  90. package/src/client/game/grid/dataCellKit.ts +99 -0
  91. package/src/client/game/grid/enteredEquipment.ts +11 -2
  92. package/src/client/game/grid/live.ts +9 -299
  93. package/src/client/game/grid/liveSession.ts +31 -8
  94. package/src/client/game/grid/neighbourLife.ts +50 -32
  95. package/src/client/game/grid/products.ts +21 -7
  96. package/src/client/game/grid/reveal.ts +7 -2
  97. package/src/client/game/grid/session.ts +10 -74
  98. package/src/client/game/plugin/weaponFamily.ts +6 -0
  99. package/src/client/game/population/signals.ts +1 -1
  100. package/src/client/game/quest/chapterDirector.ts +1 -0
  101. package/src/client/game/shardfile/clientBossTelegraph.ts +4 -3
  102. package/src/client/game/shardfile/clientWorker.ts +35 -9
  103. package/src/client/game/shardfile/look.ts +1 -1
  104. package/src/client/game/shardfile/runtimeProduct.ts +9 -5
  105. package/src/client/game/shardfile/wirePage.ts +5 -2
  106. package/src/client/game/shardfile/worldSceneTextures.ts +15 -9
  107. package/src/client/host/online/shell.ts +37 -13
  108. package/src/cook/game/cookWorker.ts +8 -5
  109. package/src/cook/game/tierTextures.ts +5 -3
  110. package/src/server/engine/net/simWorld.ts +3 -4
  111. package/src/server/game/grid/cellHome.ts +58 -0
  112. package/src/server/game/grid/room.ts +2 -38
  113. package/src/server/game/grid/simulation.ts +5 -4
  114. package/src/server/game/plugin/mover.ts +18 -14
  115. package/src/server/game/shardfile/declaredKitItems.ts +39 -9
  116. package/src/server/host/bake/headlessWorker.ts +3 -3
  117. package/src/server/host/dedicated/cellHost.ts +2 -2
  118. package/src/server/host/dedicated/cellProtocol.ts +1 -1
  119. package/src/server/host/dedicated/cellWorker.ts +5 -3
  120. package/src/server/host/dedicated/dedicatedRoom.ts +8 -3
  121. package/src/server/host/dedicated/gridCells.ts +5 -3
  122. package/src/server/host/dedicated/main.ts +12 -7
  123. package/src/server/host/dedicated/packReader.ts +15 -2
  124. package/src/server/host/dedicated/playerStore.ts +1 -1
  125. package/src/server/host/dedicated/workerTable.ts +0 -10
  126. package/src/server/host/dedicated/world.ts +39 -5
  127. package/src/server/host/headlessRuntime.ts +11 -6
  128. package/src/server/host/standalone/cellWorld.ts +21 -7
  129. package/src/server/host/standalone/gridServer.ts +3 -1
  130. package/src/server/host/standalone/roadWorld.ts +6 -0
  131. package/src/shared/engine/physics/gridBorders.ts +15 -1
  132. package/src/shared/engine/saves/shape.ts +17 -2
  133. package/src/shared/engine/sim/snapshot.ts +5 -5
  134. package/src/shared/engine/strings.ts +10 -0
  135. package/src/shared/game/grid/placementRows.ts +4 -3
  136. package/src/shared/game/ledger.ts +13 -4
  137. package/src/shared/game/loot/platformCatalogue.ts +14 -0
  138. package/src/shared/game/quest/ledger.ts +3 -3
  139. package/src/shared/game/shardfile/items.ts +9 -1
  140. package/src/shared/host/headlessJourney.ts +3 -3
  141. package/src/shared/host/headlessTape.ts +7 -6
  142. package/src/shared/host/releaseRows.ts +4 -1
  143. package/src/client/game/grid/cellLoading.ts +0 -110
  144. package/src/client/game/grid/cellScreens.ts +0 -48
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env node
2
+ // EG-15 (meta docs/plans/test-coverage/ENGINE.md, TEST-COVERAGE row 32): fight-page-census. What a server says, a page draws.
3
+ // It runs `worker-client-fights` journeys on four full heads (Nalati, Pine, Far Reach, Dunes) and holds, per head:
4
+ // heard every view word the server said to a page (`__workerClient().heard`, counted at the wire);
5
+ // drawn what the page drew of them (`__workerClient().drawn`, counted where a view applies a word);
6
+ // offstage words for a view the page hasn't built, a room nobody entered (`__workerClient().offstage`): not drawn, and not dark;
7
+ // bodies every fight the page shows a bar for has a body drawn (`__workerClientFights().shown`, or an open arena's own body): BUG-28;
8
+ // audio every directed cue and score change that asked for a sound, and whether one played (`window.__audioTally`): N1, N2.
9
+ // A word heard that the page never drew is dark, unless WORDS below says why it draws nothing. A dark word, a bar with no body, a cue that
10
+ // found no sound or a step that failed is red. Digests and verb tapes can't see any of these: they never run a page.
11
+ // scripts/browser-lane.sh node scripts/fight-page-census.mjs --base=http://127.0.0.1:4401/ [--heads=nalati,pine,far,dunes] [--journeys=titan] [--out=tmp/census/census.json]
12
+ import { mkdirSync, writeFileSync } from 'node:fs';
13
+ import { dirname } from 'node:path';
14
+ import { chromium, devices } from 'playwright';
15
+ import { KEYCHAIN_ARGS } from './chromium-keychain.mjs';
16
+ import { fights, pageCensus } from './worker-client-fights.mjs';
17
+
18
+ const arg = (key, fallback) => process.argv.find(value => value.startsWith(`--${key}=`))?.slice(key.length + 3) ?? fallback;
19
+ const base = arg('base', ''), out = arg('out', 'tmp/census/census.json');
20
+ if (base === '') throw new Error('Requires --base');
21
+
22
+ /** Each head and the journeys that fight on it (`fights(…, only)`). Flags the journeys read off argv go on with them. `qara` is left
23
+ * out: its captain comes only after a night patrol is thinned, and some runs it never comes (`sweep`'s Argymaq draws an elite lane). */
24
+ export const HEADS = {
25
+ nalati: { slug: 'nalati-grasslands', journeys: ['titan', 'sweep'], flags: ['--titanlook'] },
26
+ pine: { slug: 'pine-hollow', journeys: ['rig', 'pine'], flags: [] },
27
+ far: { slug: 'far-reach', journeys: ['roc', 'farreach'], flags: [] },
28
+ dunes: { slug: 'sunscar-dunes', journeys: ['sunscar'], flags: [] },
29
+ };
30
+ /** View words a page hears and rightly draws nothing for, and why. Everything else heard must be drawn. */
31
+ export const WORDS = [
32
+ // Exact words, not a prefix: a lane-sheet boss's strike must still be drawn (clientBossTelegraph.ts).
33
+ ['boss.strike.far.roc.stoop', 'the Roc\'s presentation is marked and names no strike: no page view takes it, its body plays the attack off its row'],
34
+ ['boss.strike.sunscar.matriarch.dive', 'the Matriarch\'s presentation is marked and names no strike: no page view takes it, its body plays the attack off its row'],
35
+ ];
36
+ const silent = name => WORDS.find(([word]) => name === word)?.[1] ?? null;
37
+ /** One row per kind of word. A pose is a pose whatever body and field it names (one taken is the census's proof that poses land);
38
+ * an elite's words are drawn each frame under its kind (`Elite.ts`: `elite lane`), so its heard words count there. */
39
+ export const family = name => name.startsWith('pose.') ? 'pose' : name.replace(/^elite\.(\w+):.*$/u, 'elite $1');
40
+
41
+ const picked = arg('heads', Object.keys(HEADS).join(',')).split(','), only = arg('journeys', '').split(',').filter(Boolean);
42
+ for (const name of picked) if (HEADS[name] === undefined) throw new Error(`Unknown head ${name}: ${Object.keys(HEADS).join(', ')}`);
43
+ const { defaultBrowserType: _engine, ...phone } = devices['iPhone 16 Pro'];
44
+ const launched = await chromium.launch({ args: [...KEYCHAIN_ARGS, '--mute-audio', '--use-angle=metal', '--ignore-gpu-blocklist'] });
45
+ const browser = { close: () => launched.close(), newContext: options => launched.newContext(options) };
46
+
47
+ /** One sample of a page's fights: each in progress, whether its bar shows, and whether a body for it is drawn. */
48
+ const sampleFights = page => page.evaluate(() => {
49
+ const shown = selector => [...document.querySelectorAll(selector)].some(node => node.classList.contains('show'));
50
+ const bar = shown('[class*="ws-boss"]') || shown('[class*="ws-elite"]');
51
+ const open = Object.values(window.__wildshard?.shard ?? {}).flatMap(script => { try { const body = script?.openFight?.body; return body === undefined ? [] : [body.visible === true]; } catch { return []; } });
52
+ return (window.__workerClientFights?.() ?? []).filter(row => row.state === 'fight').map(row => ({ id: row.id, bar, body: row.shown ?? open.some(Boolean) }));
53
+ });
54
+ const readPage = page => page.evaluate(() => {
55
+ const report = window.__workerClient();
56
+ return { heard: report.heard ?? {}, drawn: report.drawn, offstage: report.offstage ?? {}, audio: window.__audioTally ?? {}, said: window.__said ?? [] };
57
+ });
58
+
59
+ const census = {};
60
+ for (const head of picked) {
61
+ const { slug, journeys, flags } = HEADS[head];
62
+ const row = census[head] = { slug, steps: [], heard: {}, drawn: {}, offstage: {}, audio: {}, bodies: {}, pages: 0 };
63
+ const sampling = new Map();
64
+ pageCensus.sink = {
65
+ opened(_slug, page) {
66
+ const timer = setInterval(() => {
67
+ void sampleFights(page).then(seen => {
68
+ for (const { id, bar, body } of seen) {
69
+ const fight = row.bodies[id] ??= { samples: 0, bar: 0, bodiless: 0 };
70
+ fight.samples++; if (bar) fight.bar++; if (bar && !body) fight.bodiless++;
71
+ }
72
+ return undefined;
73
+ }, () => undefined);
74
+ }, 1500);
75
+ sampling.set(page, timer);
76
+ },
77
+ async closing(_slug, page) {
78
+ clearInterval(sampling.get(page)); sampling.delete(page);
79
+ const seen = await readPage(page).catch(() => null);
80
+ if (seen === null) return;
81
+ row.pages++;
82
+ for (const [name, count] of Object.entries(seen.heard)) row.heard[family(name)] = (row.heard[family(name)] ?? 0) + count;
83
+ for (const [name, count] of Object.entries(seen.drawn)) row.drawn[family(name)] = (row.drawn[family(name)] ?? 0) + count;
84
+ for (const [name, count] of Object.entries(seen.offstage)) row.offstage[family(name)] = (row.offstage[family(name)] ?? 0) + count;
85
+ for (const [name, { played, silent: quiet }] of Object.entries(seen.audio)) { const sound = row.audio[name] ??= { played: 0, silent: 0 }; sound.played += played; sound.silent += quiet; }
86
+ },
87
+ };
88
+ process.argv.push(...flags);
89
+ for (const journey of only.length === 0 ? journeys : journeys.filter(name => only.includes(name))) {
90
+ await fights(browser, phone, base, journey, async (name, run) => {
91
+ const started = Date.now();
92
+ let result;
93
+ try { result = await run(); } catch (error) { result = { ok: false, error: String(error?.message ?? error).slice(0, 300) }; }
94
+ row.steps.push({ name, ok: result?.ok === true, seconds: Math.round((Date.now() - started) / 1000) });
95
+ console.log(`${result?.ok === true ? 'ok ' : 'FAIL'} ${head} ${name}${result?.ok === true ? '' : ` ${JSON.stringify(result).slice(0, 400)}`}`);
96
+ });
97
+ }
98
+ for (const flag of flags) process.argv.splice(process.argv.indexOf(flag), 1);
99
+ pageCensus.sink = null;
100
+ }
101
+ await browser.close();
102
+
103
+ /** The verdict: each head's words heard against drawn, its bodies and its sounds. */
104
+ const red = [];
105
+ for (const [head, row] of Object.entries(census)) {
106
+ console.log(`\n${head} (${row.slug}): ${row.pages} pages, ${row.steps.filter(step => step.ok).length}/${row.steps.length} steps`);
107
+ console.log(`${' word'.padEnd(44)}${'heard'.padStart(8)}${'drawn'.padStart(8)} verdict`);
108
+ for (const name of [...new Set([...Object.keys(row.heard), ...Object.keys(row.drawn)])].sort()) {
109
+ const heard = row.heard[name] ?? 0, drawn = row.drawn[name] ?? 0, offstage = row.offstage[name] ?? 0, why = silent(name);
110
+ // offstage: every word of it was for a view this page hadn't built (a room nobody entered)
111
+ const verdict = heard === 0 ? 'drawn only' : drawn > 0 ? 'drawn' : offstage === heard ? 'offstage: its room was never built' : why !== null ? `silent: ${why}` : 'DARK';
112
+ if (verdict === 'DARK') red.push(`${head}: ${name} heard ${heard}, drawn 0`);
113
+ console.log(` ${name.slice(0, 41).padEnd(42)}${String(heard).padStart(8)}${String(drawn).padStart(8)} ${verdict}`);
114
+ }
115
+ for (const [id, fight] of Object.entries(row.bodies)) {
116
+ console.log(` fight ${id}: ${fight.samples} samples, bar on ${fight.bar}, bar with no body ${fight.bodiless}`);
117
+ if (fight.bodiless > 0) red.push(`${head}: ${id} showed a bar with no body in ${fight.bodiless} of ${fight.bar} samples`);
118
+ }
119
+ for (const [name, sound] of Object.entries(row.audio).sort(([a], [b]) => a.localeCompare(b))) {
120
+ if (!name.startsWith('cue ') && !name.startsWith('music ')) continue;
121
+ console.log(` audio ${name}: played ${sound.played}, silent ${sound.silent}`);
122
+ if (name.startsWith('cue ') && sound.played === 0) red.push(`${head}: ${name} asked for a sound ${sound.silent} times and none played`);
123
+ }
124
+ // N2: a boss fight the page showed scores it. A sting before the first gesture finds no audio rig yet, so stings only report.
125
+ if (Object.values(row.bodies).some(fight => fight.bar > 0) && (row.audio['music mode:combat']?.played ?? 0) === 0) red.push(`${head}: a boss fight showed its bar and the score never went to combat`);
126
+ for (const step of row.steps) if (!step.ok) red.push(`${head}: step failed: ${step.name}`);
127
+ }
128
+ mkdirSync(dirname(out), { recursive: true });
129
+ writeFileSync(out, `${JSON.stringify({ base, census, red }, null, 1)}\n`);
130
+ console.log(red.length === 0 ? '\ncensus green' : `\ncensus RED (${red.length}):\n${red.map(line => ` ${line}`).join('\n')}`);
131
+ process.exit(red.length === 0 ? 0 : 1);
@@ -10,6 +10,9 @@ A shard (24 cameras). <goldens> is a shard repo's art/goldens/: cameras24.json,
10
10
  An open plot (8 views). <goldens> holds <plot>/cameras.json and <plot>/<n>-<view>.jpg:
11
11
  python3 scripts/goldens/board.py --goldens <goldens> --plot <plot> --now <dir> --out <dir> [...]
12
12
  NOW frames: <now>/<plot>/<n>-<view>.png|.jpg. Columns: GOLDEN | NOW.
13
+ Plot mask: when <n>-<view>-iso.png (other cells hidden) and <n>-<view>-bare.png (every cell hidden) sit beside a NOW
14
+ frame, the pixels where they differ are the plot's own. SSIM and the colour guard count only those, so a neighbour
15
+ the grid layout moved never scores, and a SCORED column shows NOW with everything else dimmed.
13
16
 
14
17
  Writes <out>/<id>.jpg (one big labelled row per camera: read every one) and <out>/board-<slug>.jpg (every camera on one
15
18
  labelled board, <= 500 KB; --red burns a red frame and RED onto those cameras). Prints SSIM per camera: a guide only,
@@ -45,15 +48,32 @@ def blur(a):
45
48
  return sum(k[i] * rows[:, i:i + a.shape[1]] for i in range(11))
46
49
 
47
50
 
48
- def ssim(a_img, b_img):
49
- """Luma SSIM at 402 x 681, the G859 page's measure."""
51
+ def fitted_mask(mask, size):
52
+ """A boolean mask resized to size (w, h)."""
53
+ return np.asarray(Image.fromarray(mask.astype(np.uint8) * 255).resize(size, Image.NEAREST)) > 127
54
+
55
+
56
+ def ssim(a_img, b_img, mask=None):
57
+ """Luma SSIM at 402 x 681, the G859 page's measure; with a mask, the mean over its pixels."""
50
58
  size = (402, 681)
51
59
  a, b = luma(a_img.resize(size, Image.BILINEAR)), luma(b_img.resize(size, Image.BILINEAR))
60
+ if mask is not None: # the same flat fill outside the plot in both, so edge windows never see a neighbour
61
+ inside = fitted_mask(mask, size); fill = b[inside].mean()
62
+ a, b = np.where(inside, a, fill), np.where(inside, b, fill)
52
63
  c1, c2 = (0.01 * 255) ** 2, (0.03 * 255) ** 2
53
64
  ma, mb = blur(a), blur(b)
54
65
  va, vb, cov = blur(a * a) - ma * ma, blur(b * b) - mb * mb, blur(a * b) - ma * mb
55
66
  m = ((2 * ma * mb + c1) * (2 * cov + c2)) / ((ma * ma + mb * mb + c1) * (va + vb + c2))
56
- return float(m.mean())
67
+ return float(m[fitted_mask(mask, size)].mean() if mask is not None else m.mean())
68
+
69
+
70
+ def plot_mask(iso, bare):
71
+ """Where the plot draws: other-cells-hidden against every-cell-hidden, a per-channel step over 24 (sky drift
72
+ and dithering stay under it), smoothed so single noisy pixels drop out. None when the two frames don't pair."""
73
+ if iso is None or bare is None or iso.size != bare.size: return None
74
+ a, b = (np.asarray(x.convert('RGB'), dtype=np.int16) for x in (iso, bare))
75
+ differs = (np.abs(a - b).max(axis=2) > 24).astype(np.float64)
76
+ return blur(differs) > 0.5
57
77
 
58
78
 
59
79
  def font(size):
@@ -98,19 +118,21 @@ def cameras(a):
98
118
  golden = Image.open(os.path.join(g, 'shardfile', f'{cid}.jpg')).convert('RGB')
99
119
  cols.append(('SHARDFILE golden', golden.crop((0, STRIP, golden.width, golden.height))))
100
120
  now = find(os.path.join(a.now, a.shard), (f'{cid}-shardfile.png', f'{cid}.png', f'{cid}.jpg'))
101
- out.append((c['n'], cid, cols, now))
121
+ out.append((c['n'], cid, cols, now, None))
102
122
  else:
103
123
  for v in json.load(open(os.path.join(g, a.plot, 'cameras.json')))['views']:
104
124
  vid = f'{v["n"]}-{v["name"]}'
105
125
  golden = Image.open(os.path.join(g, a.plot, f'{vid}.jpg')).convert('RGB')
106
- now = find(os.path.join(a.now, a.plot), (f'{vid}.png', f'{vid}.jpg'))
107
- out.append((v['n'], vid, [('GOLDEN', golden)], now))
126
+ base = os.path.join(a.now, a.plot)
127
+ now = find(base, (f'{vid}.png', f'{vid}.jpg'))
128
+ mask = plot_mask(find(base, (f'{vid}-iso.png',)), find(base, (f'{vid}-bare.png',)))
129
+ out.append((v['n'], vid, [('GOLDEN', golden)], now, mask))
108
130
  return out
109
131
 
110
132
 
111
133
  def promote(a, cams):
112
134
  if not a.shard or not a.source: raise SystemExit('--promote needs --shard and --source "<sha10> <ruling>"')
113
- for n, cid, _, now in cams:
135
+ for n, cid, _, now, _mask in cams:
114
136
  if n not in a.promote: continue
115
137
  if now is None: raise SystemExit(f'no NOW frame for {cid}')
116
138
  img, f = fit(now, 540), font(12)
@@ -136,20 +158,31 @@ def main():
136
158
  if not a.out: raise SystemExit('--out is required')
137
159
  os.makedirs(a.out, exist_ok=True)
138
160
  big, small, missing = [], [], []
139
- for n, cid, goldens, now in cams:
161
+ for n, cid, goldens, now, mask in cams:
140
162
  if now is None: missing.append(cid); continue
141
- s = ssim(goldens[-1][1], now)
142
- pixels = lambda image: image.convert('RGB').resize((128, 217), Image.BILINEAR).getdata()
163
+ if mask is not None and not mask.any(): mask = None # nothing of the plot in view: score the frame
164
+ s = ssim(goldens[-1][1], now, mask)
165
+ small_px = lambda image: np.asarray(image.convert('RGB').resize((128, 217), Image.BILINEAR)).reshape(-1, 3)
166
+ keep = fitted_mask(mask, (128, 217)).reshape(-1) if mask is not None else slice(None)
167
+ pixels = lambda image: [tuple(p) for p in small_px(image)[keep].tolist()]
143
168
  colour = colour_shift(pixels(goldens[-1][1]), pixels(now))
144
169
  if colour['red']: red.add(n)
145
170
  is_red = n in red
146
171
  shift = f'dY {colour["luma_delta"]:+.3f} dS {colour["saturation_delta"]:+.3f}'
172
+ if mask is not None: shift += f' plot {mask.mean():.0%}'
147
173
  flag = ' RED COLOUR' if colour['red'] else ' RED' if is_red else ''
148
174
  print(f'{cid:28s} {s:.3f} {shift}{flag}')
149
175
  for w, size, sink, title in ((360, 13, big, f'{slug} {cid} SSIM {s:.2f} {shift}'), (120, 9, small, f'{cid} {s:.2f}')):
150
176
  f = font(size)
151
177
  cells = [labelled(fit(img, w), label, INK, w, f) for label, img in goldens]
152
178
  cells.append(labelled(fit(now, w), a.label, RED if is_red else INK, w, f))
179
+ if any(c[4] is not None for c in cams):
180
+ scored = fit(now, w)
181
+ if mask is not None:
182
+ keep_w = fitted_mask(mask, scored.size)[..., None]
183
+ arr = np.asarray(scored, dtype=np.float64)
184
+ scored = Image.fromarray(np.where(keep_w, arr, arr * 0.2 + 8).astype(np.uint8))
185
+ cells.append(labelled(scored, 'SCORED (plot mask)' if mask is not None else 'SCORED (whole frame)', INK, w, f))
153
186
  sink.append(row(cells, title + flag, w, is_red, f))
154
187
  big[-1].save(os.path.join(a.out, f'{cid}.jpg'), quality=82)
155
188
  if missing: print('MISSING NOW frames:', ', '.join(missing))
@@ -157,7 +190,7 @@ def main():
157
190
  cols = 4; tw, th = small[0].width, max(t.height for t in small)
158
191
  f = font(16); head = 2 * f.size + 24
159
192
  board = Image.new('RGB', (cols * (tw + 4) + 4, head + -(-len(small) // cols) * (th + 4) + 4), BG)
160
- columns = ' . '.join([label for label, _ in cams[0][2]] + [a.label])
193
+ columns = ' . '.join([label for label, _ in cams[0][2]] + [a.label] + (['SCORED'] if any(c[4] is not None for c in cams) else []))
161
194
  draw = ImageDraw.Draw(board)
162
195
  draw.text((6, 6), f'GOLDEN RERUN {slug} | {columns}', fill=INK, font=f)
163
196
  status = f'RED: {",".join(map(str, sorted(red)))}' if red else 'No mean colour-shift flags; VISUAL REVIEW REQUIRED'
@@ -3,6 +3,7 @@ export function workingGoldenPreviewFolder(record:string):string;
3
3
  export function goldenRefinementBinding(base:string,source:{build:string;sha:string}):Promise<{
4
4
  initializerExport:string;classExport:string;sha:string;build:string;preview:string;nineIndexSHA256:string;chunk:string;chunkSHA256:string;
5
5
  }>;
6
+ export function buildCodeChunks(root:string,build:string):string[];
6
7
  export function refinementExports(source:string):{initializerExport:string;classExport:string};
7
8
  export function observeRefinement<T extends readonly unknown[]>(proto:{add:(...args:T)=>Promise<boolean>}):{
8
9
  jobs:{id:T[0];at:T[1];state:string;error?:string}[];
@@ -38,17 +38,31 @@ export async function goldenRefinementBinding(base,source) {
38
38
  throw new Error('Native reader requires the served Nine product');
39
39
  const servedIndex=Buffer.from(await(await fetch(new URL('/shardfiles/nine-dragon-stack/shard.json',base))).arrayBuffer());
40
40
  if(!servedIndex.equals(index))throw new Error('Served Nine index differs from exact preview');
41
- const code=`b/${source.build}`,codeFolder=existsSync(join(root,code))?code:'assets',dist=join(root,codeFolder);
42
- const chunks=readdirSync(dist).filter(file=>file.endsWith('.js')).map(file=>({file,bytes:readFileSync(join(dist,file))}))
41
+ const chunks=buildCodeChunks(root,source.build).map(chunk=>({chunk,bytes:readFileSync(join(root,chunk))}))
43
42
  .filter(row=>row.bytes.includes('Invalid or duplicate refinement job'));
44
43
  if (chunks.length!==1) throw new Error('Native refinement chunk is missing or ambiguous');
45
- const {file,bytes}=chunks[0],binding=refinementExports(bytes.toString('utf8'));
46
- const chunk=`/${codeFolder}/${file}`,served=Buffer.from(await(await fetch(new URL(chunk,base))).arrayBuffer());
44
+ const {chunk,bytes}=chunks[0],binding=refinementExports(bytes.toString('utf8'));
45
+ const served=Buffer.from(await(await fetch(new URL(chunk,base))).arrayBuffer());
47
46
  if (!served.equals(bytes)) throw new Error('Served native refinement differs from exact preview');
48
47
  return {...binding,sha:pinned?.sha??source.sha,build:source.build,preview:pinned===undefined?'working-tree':'pinned',
49
48
  nineIndexSHA256:createHash('sha256').update(index).digest('hex'),chunk,chunkSHA256:createHash('sha256').update(served).digest('hex')};
50
49
  }
51
50
 
51
+ /** This build's code chunks, as served paths: its own folder `b/<build>`, plus the
52
+ * content-named chunks in the shared `b/c` that its `importmap.json` names (J217
53
+ * LD6). The shared folder can also hold other builds' chunks, so it is never
54
+ * listed. A build without `b/<build>` keeps everything in `assets/`.
55
+ * @param {string} root the preview's dist @param {string} build */
56
+ export function buildCodeChunks(root,build) {
57
+ const own=`b/${build}`;
58
+ if (!existsSync(join(root,own))) return readdirSync(join(root,'assets')).filter(file=>file.endsWith('.js')).map(file=>`/assets/${file}`);
59
+ const chunks=readdirSync(join(root,own)).filter(file=>file.endsWith('.js')).map(file=>`/${own}/${file}`);
60
+ const map=join(root,'importmap.json');
61
+ if (existsSync(map)) for (const target of Object.values(JSON.parse(readFileSync(map,'utf8')).imports??{}))
62
+ if (typeof target==='string'&&target.startsWith('/b/c/')&&target.endsWith('.js')&&!chunks.includes(target)) chunks.push(target);
63
+ return chunks;
64
+ }
65
+
52
66
  /** Fail closed when the compiler shape changes; no guessed export or copied release binding.
53
67
  * @param {string} source */
54
68
  export function refinementExports(source) {
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  // GC15 (GRID-CROSSING): the first page walk across a cell border online, on this Mac. The rig's server runs WORLD_CELLS=grid with
3
- // WORLD_CROSSING=worker on staging's two workers (Nalati alone; Driftwood, Pine Hollow, Sunscar and Far Reach and their road on the other), its origin carries the grid pack, and a real browser enters Driftwood
3
+ // the always-on road on staging's two workers (Nalati alone; Driftwood, Pine Hollow, Sunscar and Far Reach and their road on the other), its origin carries the grid pack, and a real browser enters Driftwood
4
4
  // from the deck's card, then walks into a same-worker neighbour (Sunscar, or Pine Hollow), boosts back into Driftwood and boosts out again, by
5
5
  // real input. Each leg passes with no stop (no 1.5 s under 0.15 m a sample while the key is held), no page reload, and control in the
6
6
  // target cell (the page inside it, gameplay ready, the player moving). It writes time-to-control per leg beside GC1's baseline.
@@ -69,10 +69,10 @@ if (served !== '' && !/^https:\/\/(staging|preview)\.wildshard\.io\/?$|^http:\/\
69
69
  const workers = Number(process.argv.find(arg => arg.startsWith('--workers='))?.slice(10) ?? 2);
70
70
  if (!Number.isInteger(workers) || workers < 1) throw new Error('--workers takes a whole number from 1');
71
71
  const rig = served !== '' ? { base: served.replace(/\/$/, ''), close: () => Promise.resolve() }
72
- : await openRig({ dist, work, keyFile, pgBin: 'memory:', pgData: '', shard: HOME, more: ['pine-hollow', 'far-reach', 'sunscar-dunes', 'nalati-grasslands'], cells: 'grid', crossing: true, workers });
72
+ : await openRig({ dist, work, keyFile, pgBin: 'memory:', pgData: '', shard: HOME, more: ['pine-hollow', 'far-reach', 'sunscar-dunes', 'nalati-grasslands'], cells: 'grid', workers });
73
73
  let code = 1, browser = /** @type {import('playwright').Browser | null} */ (null);
74
74
  /** @type {Record<string, unknown>} */
75
- const result = { lane: 'opus-gc15c', engine: engineName, layout: phone ? 'phone 402x874 touch' : `desktop ${String(desk[0])}x${String(desk[1])}@${String(desk[2])}`, site: served !== '' ? served : 'local rig, WORLD_CELLS=grid, WORLD_CROSSING=worker, WORLD_WORKERS=2 (staging: Nalati alone, Driftwood with Pine Hollow, Sunscar and Far Reach)' };
75
+ const result = { lane: 'opus-gc15c', engine: engineName, layout: phone ? 'phone 402x874 touch' : `desktop ${String(desk[0])}x${String(desk[1])}@${String(desk[2])}`, site: served !== '' ? served : 'local rig, WORLD_CELLS=grid, WORLD_WORKERS=2 (staging: Nalati alone, Driftwood with Pine Hollow, Sunscar and Far Reach)' };
76
76
  try {
77
77
  let open = served !== '' ? { cells: 25 } : null;
78
78
  for (let tries = 0; tries < 300 && open === null; tries++) { open = await fetch(`http://127.0.0.1:${String(WORLD_PORT)}/health`).then(found => found.ok ? found.json() : null).catch(() => null); if (open === null) await sleep(200); }
@@ -8,7 +8,7 @@
8
8
  // QA 11-15 row 9: `--shots` also shoots the left edge riding with boost off and on (the HOVER tab on a phone, the tag on desktop).
9
9
  // scripts/browser-lane.sh node scripts/hover-speed-probe.mjs --base=<staging, preview or loopback url> --out=<dir> [--layout=phone] [--shots]
10
10
  // scripts/browser-lane.sh node scripts/hover-speed-probe.mjs --rig=<app dist>,<work dir>,<key.json> --out=<dir> (engine's loopback
11
- // world, online-rig.mjs: the built dedicated server, 2 workers, crossing on; the dist stamped for its release file)
11
+ // world, online-rig.mjs: the built dedicated server and 2 workers; the dist stamped for its release file)
12
12
  // J205: staging, preview or a loopback page only, never production. Chromium runs headless and muted; the page is closed at the end.
13
13
  import { mkdirSync, writeFileSync } from 'node:fs';
14
14
  import { join, resolve as absolute } from 'node:path';
@@ -31,7 +31,7 @@ let rig = /** @type {{ base: string, close: () => Promise<unknown> } | null} */
31
31
  if (rigArg !== '') {
32
32
  const [dist = '', work = '', keyFile = ''] = rigArg.split(',');
33
33
  const { openRig, WORLD_PORT } = await import('./online-rig.mjs');
34
- rig = await openRig({ dist, work: absolute(work), keyFile, pgBin: 'memory:', pgData: '', shard: 'driftwood-isle', more: ['pine-hollow', 'far-reach', 'sunscar-dunes', 'nalati-grasslands'], cells: 'grid', crossing: true, workers: 2 });
34
+ rig = await openRig({ dist, work: absolute(work), keyFile, pgBin: 'memory:', pgData: '', shard: 'driftwood-isle', more: ['pine-hollow', 'far-reach', 'sunscar-dunes', 'nalati-grasslands'], cells: 'grid', workers: 2 });
35
35
  for (let tries = 0; tries < 300 && !(await fetch(`http://127.0.0.1:${String(WORLD_PORT)}/health`).then(found => found.ok).catch(() => false)); tries++) await sleep(200);
36
36
  base = rig.base;
37
37
  }
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env node
2
+ // REPO-DISK DK18a/J358: preserve a worktree before a human removes it. This
3
+ // helper only writes a copy; it has no delete operation.
4
+ import { spawnSync } from 'node:child_process';
5
+ import { createHash, randomUUID } from 'node:crypto';
6
+ import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, writeFileSync } from 'node:fs';
7
+ import { homedir } from 'node:os';
8
+ import { basename, join, resolve } from 'node:path';
9
+ import { pathToFileURL } from 'node:url';
10
+
11
+ const MAX_UNTRACKED_BYTES = 50 * 1024 * 1024;
12
+ const DEFAULT_KEEP_ROOT = join(homedir(), '.cache/wildshard/session-keep');
13
+ const RETENTION_DAYS = 14;
14
+
15
+ /** @param {string} cwd @param {string[]} args */
16
+ const run = (cwd, args) => spawnSync('git', ['-C', cwd, ...args], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
17
+ /** @param {string} cwd @param {string[]} args */
18
+ const text = (cwd, args) => {
19
+ const result = run(cwd, args);
20
+ if (result.status !== 0) throw new Error(`keep-copy: git ${args.join(' ')} failed: ${result.stderr.trim()}`);
21
+ return result.stdout;
22
+ };
23
+
24
+ /** @param {string} path @param {string} root @param {Date} now */
25
+ function destination(path, root, now) {
26
+ const day = now.toISOString().slice(0, 10);
27
+ const stamp = now.toISOString().slice(11, 19).replaceAll(':', '-');
28
+ return join(root, day, `${basename(path)}__${process.pid}-${stamp}-${randomUUID()}`);
29
+ }
30
+
31
+ /** @param {string} path @param {string[]} files */
32
+ function bytes(path, files) {
33
+ return files.reduce((total, file) => total + lstatSync(join(path, file)).size, 0);
34
+ }
35
+
36
+ /** Snapshot every recoverable byte before copying and again after stopping
37
+ * writers. Status alone misses edits within an existing untracked folder.
38
+ * @param {string} root */
39
+ export function worktreeState(root) {
40
+ const hash = createHash('sha256');
41
+ for (const file of text(root, ['ls-files', '--others', '--exclude-standard', '-z']).split('\0').filter(Boolean)) {
42
+ const path = join(root, file), info = lstatSync(path);
43
+ if (!info.isFile() && !info.isSymbolicLink()) throw new Error(`cannot snapshot ${file}`);
44
+ hash.update(JSON.stringify([file, info.mode, info.size]));
45
+ hash.update(info.isSymbolicLink() ? readlinkSync(path) : readFileSync(path));
46
+ }
47
+ return JSON.stringify([
48
+ text(root, ['rev-parse', 'HEAD']), text(root, ['symbolic-ref', '--quiet', 'HEAD']),
49
+ text(root, ['status', '--porcelain', '--untracked-files=all']),
50
+ text(root, ['diff', '--binary', 'HEAD']), text(root, ['diff', '--cached', '--binary', 'HEAD']),
51
+ hash.digest('hex'),
52
+ ]);
53
+ }
54
+
55
+ /** Copy recoverable Git worktree state. It never deletes or mutates `path`.
56
+ * @param {string} path @param {{keepRoot?: string, now?: Date}} [options] */
57
+ export function keepCopy(path, { keepRoot = DEFAULT_KEEP_ROOT, now = new Date() } = {}) {
58
+ const root = resolve(path);
59
+ if (!existsSync(root)) throw new Error(`keep-copy: ${root} does not exist`);
60
+ const repo = text(root, ['rev-parse', '--show-toplevel']).trim();
61
+ const files = text(root, ['ls-files', '--others', '--exclude-standard', '-z']).split('\0').filter(Boolean);
62
+ const dir = destination(root, resolve(keepRoot), now);
63
+ mkdirSync(dir, { recursive: true });
64
+ /** @type {{source:string,repository:string,created:string,bundle:boolean,patch:boolean,untracked:string|{files:number,bytes:number}}} */
65
+ const manifest = { source: root, repository: repo, created: now.toISOString(), bundle: false, patch: false, untracked: 'none' };
66
+ const unpublished = run(root, ['log', '--branches', '--tags', '--not', '--remotes', '--format=%H']);
67
+ if (unpublished.status !== 0) throw new Error(`keep-copy: cannot inspect unpublished commits: ${unpublished.stderr.trim()}`);
68
+ if (unpublished.stdout.trim() !== '') {
69
+ text(root, ['bundle', 'create', join(dir, 'unpublished.bundle'), '--branches', '--tags', '--not', '--remotes']);
70
+ manifest.bundle = true;
71
+ }
72
+ const patch = text(root, ['diff', '--binary', 'HEAD']);
73
+ const indexPatch = text(root, ['diff', '--cached', '--binary', 'HEAD']);
74
+ if (indexPatch !== '') writeFileSync(join(dir, 'index.patch'), indexPatch);
75
+ if (patch !== '') {
76
+ writeFileSync(join(dir, 'tracked.patch'), patch);
77
+ manifest.patch = true;
78
+ }
79
+ if (files.length > 0) {
80
+ const total = bytes(root, files);
81
+ if (total <= MAX_UNTRACKED_BYTES) {
82
+ const archive = spawnSync('tar', ['-czf', join(dir, 'untracked.tgz'), '--', ...files], { cwd: root, encoding: 'utf8' });
83
+ if (archive.status !== 0) throw new Error(`keep-copy: tar failed: ${archive.stderr.trim()}`);
84
+ manifest.untracked = { files: files.length, bytes: total };
85
+ } else throw new Error('keep-copy: untracked files exceed 50 MiB; source must be kept');
86
+ }
87
+ writeFileSync(join(dir, 'MANIFEST.json'), `${JSON.stringify(manifest, null, 2)}\n`);
88
+ return { dir, manifest };
89
+ }
90
+
91
+ /** List retention candidates for a human audit. This never removes a copy.
92
+ * @param {{keepRoot?: string, now?: Date}} [options] */
93
+ export function oldKeepCopies({ keepRoot = DEFAULT_KEEP_ROOT, now = new Date() } = {}) {
94
+ if (!existsSync(keepRoot)) return [];
95
+ const rows = [];
96
+ for (const day of readdirSync(keepRoot, { withFileTypes: true }).filter(entry => entry.isDirectory())) {
97
+ const parent = join(keepRoot, day.name);
98
+ for (const child of readdirSync(parent, { withFileTypes: true }).filter(entry => entry.isDirectory())) {
99
+ const manifest = join(parent, child.name, 'MANIFEST.json');
100
+ try {
101
+ const created = new Date(JSON.parse(readFileSync(manifest, 'utf8')).created);
102
+ const age = Math.floor((now.getTime() - created.getTime()) / 86_400_000);
103
+ if (Number.isFinite(age) && age >= RETENTION_DAYS) rows.push({ path: join(parent, child.name), age });
104
+ } catch {/* Unreadable evidence is skipped. */}
105
+ }
106
+ }
107
+ return rows.sort((left, right) => right.age - left.age || left.path.localeCompare(right.path));
108
+ }
109
+
110
+ if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) {
111
+ try {
112
+ const [path] = process.argv.slice(2);
113
+ if (!path || process.argv.length !== 3) throw new Error('usage: node scripts/keep-copy.mjs <worktree>');
114
+ const result = keepCopy(path, { keepRoot: process.env.WS_SESSION_KEEP_ROOT ?? DEFAULT_KEEP_ROOT });
115
+ process.stdout.write(`${result.dir}\n`);
116
+ } catch (error) {
117
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
118
+ process.exitCode = 1;
119
+ }
120
+ }
package/scripts/land.mjs CHANGED
@@ -15,6 +15,7 @@
15
15
  // 8. Delete origin's plan branch and remove this worktree (--keep skips it).
16
16
  // Main's CI runs after every landing; if it goes red, the lane that landed last fixes it forward or reverts it.
17
17
  import {execFileSync,spawn,spawnSync} from 'node:child_process';
18
+ import {keepCopy,worktreeState} from './keep-copy.mjs';
18
19
  import {appendFileSync,existsSync,linkSync,lstatSync,mkdirSync,mkdtempSync,readFileSync,readdirSync,realpathSync,renameSync,rmSync,writeFileSync} from 'node:fs';
19
20
  import {homedir} from 'node:os';
20
21
  import {dirname,join,resolve} from 'node:path';
@@ -159,8 +160,8 @@ function ghApi(repo,path){
159
160
  }
160
161
 
161
162
  /** J193/J200: `quarantine (n)` jobs run quarantined files with their ordinary exit code and are continue-on-error;
162
- * they are red by design until rewritten and never block a landing. So is J186's report-only `boundaries` job (EV3b). */
163
- const NONBLOCKING_JOB=/^(?:quarantine|boundaries)(?: \(\d+\))?$/u;
163
+ * they are red by design until rewritten and never block a landing. Boundaries blocks after TS15's green soak. */
164
+ const NONBLOCKING_JOB=/^quarantine(?: \(\d+\))?$/u;
164
165
  const FAILED=new Set(['failure','timed_out','cancelled','startup_failure']);
165
166
 
166
167
  /** A run's failed jobs, split into the blocking ones and the non-blocking quarantine ones.
@@ -825,16 +826,45 @@ class Landing{
825
826
  const deleted=tryGit(this.root,['push','--quiet',`--force-with-lease=refs/heads/${this.branch}:${this.planTip}`,'origin','--delete',this.branch]);
826
827
  parts.push(deleted.status===0?`origin ${this.branch} deleted`:`origin ${this.branch} kept (${deleted.stderr.trim().split('\n').at(-1)??'push refused'})`);
827
828
  }
828
- const dirty=tryGit(this.root,['status','--porcelain']).stdout.trim();
829
+ const dirtyResult=tryGit(this.root,['status','--porcelain','--untracked-files=all']);
830
+ const dirty=dirtyResult.stdout.trimEnd();
831
+ const generatedOnly=dirty!==''&&dirty.split('\n').every(line=>/^[ MAD?]{2} (?:[^"\n]+\.generated\.ts|public\/assets\/gpu\/[^"\n]+)$/u.test(line));
832
+ const patchBefore=tryGit(this.root,['diff','--binary','HEAD']);
829
833
  const now=tryGit(this.root,['symbolic-ref','--quiet','HEAD']).stdout.trim(),at=tryGit(this.root,['rev-parse','HEAD']).stdout.trim();
830
834
  if(now!==`refs/heads/${this.branch}`||at!==head)parts.push(`worktree kept: HEAD moved off ${this.branch} ${short(head)} after landing`);
831
- else if(dirty!=='')parts.push(`worktree kept: ${dirty.split('\n').length} uncommitted or untracked path(s)`);
835
+ else if(dirtyResult.status!==0||patchBefore.status!==0)parts.push('worktree kept: status unreadable');
836
+ else if(dirty!==''&&!generatedOnly)parts.push(`worktree kept: ${dirty.split('\n').length} uncommitted or untracked path(s)`);
832
837
  else{
838
+ // J358's sole removal exception: this land owns this just-landed worktree.
839
+ // Copy first; any failure retains the source and its local branch.
840
+ let before;
841
+ try{
842
+ before=worktreeState(this.root);
843
+ const saved=keepCopy(this.root,{keepRoot:this.env.WS_SESSION_KEEP_ROOT});
844
+ this.say('copied',`recoverable worktree state saved at ${saved.dir}`);
845
+ }catch(error){
846
+ const line=parts.concat(`worktree kept: copy failed (${error instanceof Error?error.message:String(error)})`).join('; ');
847
+ this.say('cleaned',line);
848
+ return line;
849
+ }
850
+ const unchanged=()=>{try{return worktreeState(this.root)===before;}catch{return false;}};
851
+ if(!unchanged()){
852
+ const line=parts.concat('worktree kept: changed during copy').join('; ');
853
+ this.say('cleaned',line);return line;
854
+ }
833
855
  // TEST-SPEED FL30: nothing started here may outlive the folder (an orphaned Vitest held the CPU slots).
834
856
  let stopped=[];
835
- try{stopped=stopWorktreeProcesses(realpathSync(this.root));}catch{/* Listing failed: remove as before. */}
857
+ try{stopped=stopWorktreeProcesses(realpathSync(this.root));}catch{
858
+ const line=parts.concat('worktree kept: process cleanup failed').join('; ');
859
+ this.say('cleaned',line);return line;
860
+ }
836
861
  if(stopped.length>0)parts.push(`stopped ${String(stopped.length)} process(es) still running there (pid ${stopped.join(', ')})`);
837
- const removed=tryGit(canonical,['worktree','remove',this.root]);
862
+ // Shutdown handlers may write after the copy. Protect their last writes.
863
+ if(!unchanged()){
864
+ const line=parts.concat('worktree kept: changed during process cleanup').join('; ');
865
+ this.say('cleaned',line);return line;
866
+ }
867
+ const removed=tryGit(canonical,['worktree','remove',...(generatedOnly?['--force']:[]),this.root]);
838
868
  if(removed.status===0){
839
869
  tryGitLocked(canonical,['update-ref','-d',`refs/heads/${this.branch}`,head],process.env,this.retry);
840
870
  parts.push(`worktree and ${this.branch} removed; continue from ${canonical}`);