@wildshard/engine 0.1.19 → 0.1.21

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 (125) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/lint/engine-sides.json +5 -0
  3. package/package.json +5 -2
  4. package/scripts/app-build/gen.ts +11 -0
  5. package/scripts/cell-mem-bench.mjs +2 -0
  6. package/scripts/chaos-proxy-core.mjs +14 -1
  7. package/scripts/chaos-proxy-proof.mjs +22 -0
  8. package/scripts/check-devserver.mjs +1 -1
  9. package/scripts/failmode-cases.d.mts +8 -0
  10. package/scripts/failmode-cases.mjs +97 -0
  11. package/scripts/failmode-proof.mjs +532 -0
  12. package/scripts/grid-crossing-nightly.mjs +5 -4
  13. package/scripts/grid-crossing-report.mjs +5 -2
  14. package/scripts/grid-crossing-tour.mjs +36 -12
  15. package/scripts/grid-crossing-walk-proof.mjs +4 -2
  16. package/scripts/hover-speed-probe.mjs +136 -0
  17. package/scripts/land.mjs +2 -1
  18. package/scripts/loading-flow/benchmax.mjs +34 -11
  19. package/scripts/loading-flow/deskbench.mjs +276 -0
  20. package/scripts/loading-flow/stalls.d.mts +17 -3
  21. package/scripts/loading-flow/stalls.mjs +82 -3
  22. package/scripts/net-lag-proof.mjs +3 -2
  23. package/scripts/nightly-model.ts +9 -6
  24. package/scripts/offline-proof.mjs +7 -1
  25. package/scripts/online-rig.mjs +9 -7
  26. package/scripts/parity/boot-smoke.mjs +13 -2
  27. package/scripts/pin-format.ts +5 -2
  28. package/scripts/pins-reuse.d.mts +1 -0
  29. package/scripts/pins-reuse.mjs +22 -0
  30. package/scripts/release-cook.mjs +109 -0
  31. package/scripts/scratch-dir.mjs +95 -0
  32. package/scripts/scratch-sweep.mjs +182 -0
  33. package/scripts/tsconfig.json +2 -12
  34. package/scripts/vps/harden-prometheus.sh +3 -0
  35. package/scripts/vps/rebuild.sh +35 -9
  36. package/scripts/vps/wildshard-apply.mjs +5 -1
  37. package/scripts/vps/wildshard-config +117 -1
  38. package/scripts/worktree-reap.mjs +54 -8
  39. package/scripts/worktree.mjs +19 -5
  40. package/src/client/engine/anim/rigParser.ts +2 -2
  41. package/src/client/engine/boot/contentRead.ts +54 -5
  42. package/src/client/engine/boot/gpuFiles.ts +3 -3
  43. package/src/client/engine/boot/leaveAfterPaint.ts +4 -1
  44. package/src/client/engine/core/Game.ts +63 -19
  45. package/src/client/engine/core/gpuOnly.ts +10 -0
  46. package/src/client/engine/core/renderBudget.ts +134 -0
  47. package/src/client/engine/core/shadowLayer.ts +18 -0
  48. package/src/client/engine/explore/pick.ts +13 -1
  49. package/src/client/engine/net/predictor.ts +12 -4
  50. package/src/client/engine/net/redial.ts +40 -5
  51. package/src/client/engine/player/TouchControls.ts +3 -2
  52. package/src/client/engine/render/dataLook.ts +70 -7
  53. package/src/client/engine/render/families/pbr.ts +2 -0
  54. package/src/client/engine/render/families/registry.ts +11 -2
  55. package/src/client/engine/render/families/sceneSurface.ts +118 -0
  56. package/src/client/engine/render/look.ts +7 -1
  57. package/src/client/engine/render/precompile.ts +4 -2
  58. package/src/client/engine/render/tiers.ts +4 -1
  59. package/src/client/engine/telemetry/browserErrors.ts +3 -1
  60. package/src/client/engine/ui/ErrorModal.ts +1 -1
  61. package/src/client/engine/ui/Loading.ts +14 -2
  62. package/src/client/engine/ui/errorScreen.ts +51 -9
  63. package/src/client/engine/ui/hoverBoost.ts +31 -4
  64. package/src/client/engine/ui/styles/touch.css +6 -0
  65. package/src/client/engine/world/declaredProps.ts +5 -1
  66. package/src/client/engine/world/skyRig.ts +4 -1
  67. package/src/client/engine/world/terrainTileView.ts +64 -23
  68. package/src/client/game/boot/spawnDownload.ts +44 -6
  69. package/src/client/game/devScene/hook.ts +149 -12
  70. package/src/client/game/devScene/liveLook.ts +21 -0
  71. package/src/client/game/grid/live.ts +47 -10
  72. package/src/client/game/grid/liveSession.ts +25 -6
  73. package/src/client/game/grid/pageEntry.ts +12 -1
  74. package/src/client/game/grid/productInputs.ts +22 -11
  75. package/src/client/game/grid/session.ts +15 -4
  76. package/src/client/game/grid/shardfileFar.ts +13 -2
  77. package/src/client/game/session/finish.ts +4 -1
  78. package/src/client/game/shardfile/client.ts +24 -7
  79. package/src/client/game/shardfile/clientPropMaterials.ts +4 -1
  80. package/src/client/game/shardfile/clientSource.ts +7 -3
  81. package/src/client/game/shardfile/clientViews.ts +36 -8
  82. package/src/client/game/shardfile/clientWorker.ts +4 -4
  83. package/src/client/game/shardfile/constructionCheckpoint.ts +2 -2
  84. package/src/client/game/shardfile/groundMerge.ts +59 -5
  85. package/src/client/game/shardfile/hybrid.ts +2 -0
  86. package/src/client/game/shardfile/look.ts +1 -1
  87. package/src/client/game/shardfile/slabLibrary.ts +115 -49
  88. package/src/client/game/shardfile/tileContent.ts +1 -1
  89. package/src/client/game/shardfile/tileDriver.ts +38 -11
  90. package/src/client/game/shardfile/wirePage.ts +7 -3
  91. package/src/client/game/shardfile/worldScenes.ts +17 -10
  92. package/src/client/host/online/doctor.ts +20 -7
  93. package/src/client/host/online/doctorView.ts +2 -0
  94. package/src/client/host/online/linkScreens.ts +95 -11
  95. package/src/client/host/online/reconnectStrip.ts +3 -2
  96. package/src/client/host/online/release.ts +87 -16
  97. package/src/client/host/online/releaseScreens.ts +108 -0
  98. package/src/client/host/online/shell.ts +24 -5
  99. package/src/client/host/online/voice.ts +45 -1
  100. package/src/client/host/online/voiceUi.ts +24 -6
  101. package/src/client/host/standalone/shell.ts +37 -1
  102. package/src/server/engine/sim/playerSim.ts +1 -1
  103. package/src/server/game/grid/simulation.ts +12 -6
  104. package/src/server/game/quest/eliteDrops.ts +7 -3
  105. package/src/server/game/riding/nativeTaming.ts +13 -2
  106. package/src/server/host/dedicated/cellHost.ts +21 -2
  107. package/src/server/host/dedicated/cellProtocol.ts +2 -0
  108. package/src/server/host/dedicated/cellWorker.ts +1 -1
  109. package/src/server/host/dedicated/dedicatedRoom.ts +38 -8
  110. package/src/server/host/dedicated/roadRoom.ts +3 -0
  111. package/src/server/host/dedicated/serverMetrics.ts +9 -0
  112. package/src/server/host/dedicated/workerTable.ts +31 -24
  113. package/src/server/host/standalone/gridServer.ts +1 -1
  114. package/src/server/host/standalone/serverHost.ts +56 -10
  115. package/src/server/host/standalone/serverProduct.ts +9 -7
  116. package/src/server/host/standalone/serverRuntime.ts +1 -1
  117. package/src/shared/engine/boot/loadingWaits.ts +10 -2
  118. package/src/shared/engine/core/config.ts +3 -0
  119. package/src/shared/engine/core/framePacing.ts +12 -1
  120. package/src/shared/engine/physics/terrainTiles.ts +20 -4
  121. package/src/shared/engine/player/hoverSpeed.ts +3 -3
  122. package/src/shared/engine/strings.ts +12 -1
  123. package/src/shared/game/devColliders.ts +60 -0
  124. package/src/shared/game/devGameplay.ts +41 -0
  125. package/src/shared/game/grid/rules.ts +12 -0
@@ -7,20 +7,24 @@
7
7
  # 4. The cook worker (C4): an engine checkout at a tag, its user, its AppArmor allowance and its unit. It needs cook.env.
8
8
  # sudo bash rebuild.sh do what is missing. Exit 0 is done; exit 2 is waiting for one of Jake's inputs
9
9
  # bash rebuild.sh --check change nothing and say what it would do; protected state says `cannot verify`
10
+ # sudo bash rebuild.sh --cook step 4 alone (AUTO-RELEASE AR3): `wildshard-config cook <tag>` runs it as root after
11
+ # writing cook.settings, whose COOK_ENGINE_REF and WILDSHARD_BUILD win over cook.env's
10
12
  # Keep this beside the other scripts/vps files. docs/vps-setup.md, "Rebuild from nothing", has the steps around it.
11
13
  # HARD STOP (Jake, 2026-11-09): this never runs on the old VPS. Nothing there is rebuilt until OPENCLAW-EXTRACT's OX10
12
14
  # is signed, so a host that still holds OpenClaw, Coolify, clawdbot or Ollama state is refused in both modes.
13
15
  # Port 443 and `rooms.wildshard.io` are Jake's, after VPS-HARDENING VH16 (J179): Caddy is installed and left off.
14
16
  set -euo pipefail
15
- CHECK=0
16
- if [ "${1:-}" = --check ]; then CHECK=1
17
- elif [ "$#" != 0 ]; then printf 'stop Expected no arguments or --check\n'; exit 1; fi
17
+ CHECK=0 COOK_ONLY=0
18
+ if [ "$#" = 1 ] && [ "$1" = --check ]; then CHECK=1
19
+ elif [ "$#" = 1 ] && [ "$1" = --cook ]; then COOK_ONLY=1
20
+ elif [ "$#" != 0 ]; then printf 'stop Expected no arguments, --check or --cook\n'; exit 1; fi
18
21
  HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
19
22
  ETC=/etc/wildshard HOME_DIR=/var/lib/wildshard SYSTEMD=/etc/systemd/system
20
23
  PG_VERSION=16 NODE=/usr/bin/node NODE_MAJOR=24 PNPM=pnpm@10.34.9
21
24
  KEY_DIR=/home/raynos/wildshard-e7 KEY_FILE=/home/raynos/wildshard-e7/release-public-key.hex
22
25
  COOK_ENV=/etc/wildshard/cook.env COOK_HOME=/var/lib/wildshard-cook COOK_SRC=/opt/wildshard/engine COOK_LIB=/usr/local/lib/wildshard-cook
23
26
  COOK_GROUP=wildshard-cook
27
+ COOK_SETTINGS=/etc/wildshard/cook.settings
24
28
  # 8790 and 8791 are the game's loopback ports; the cook worker's own default was 8791 too.
25
29
  COOK_PORT=8792
26
30
  ENGINE_GIT=https://github.com/project-wildshard/engine.git
@@ -80,9 +84,15 @@ env_value() { sed -n "s/^$2=//p" "$1" 2>/dev/null | tail -n 1; }
80
84
  # The cook worker starts only once its env file names a build, an API, a token and one store (R2's four rows or a folder).
81
85
  cook_env_complete() {
82
86
  local file=$1 name
83
- for name in COOK_ENGINE_REF WILDSHARD_BUILD WILDSHARD_API_URL COOK_NOTIFY_TOKEN; do [ -n "$(env_value "$file" "$name")" ] || return 1; done
84
- if [ -n "$(env_value "$file" COOK_STORE_DIR)" ]; then return 0; fi
85
- for name in R2_ACCOUNT_ID R2_BUCKET_PACKS R2_PACKS_ACCESS_KEY_ID R2_PACKS_SECRET_ACCESS_KEY; do [ -n "$(env_value "$file" "$name")" ] || return 1; done
87
+ for name in COOK_ENGINE_REF WILDSHARD_BUILD WILDSHARD_API_URL COOK_NOTIFY_TOKEN; do [ -n "$(cook_value "$file" "$name")" ] || return 1; done
88
+ if [ -n "$(cook_value "$file" COOK_STORE_DIR)" ]; then return 0; fi
89
+ for name in R2_ACCOUNT_ID R2_BUCKET_PACKS R2_PACKS_ACCESS_KEY_ID R2_PACKS_SECRET_ACCESS_KEY; do [ -n "$(cook_value "$file" "$name")" ] || return 1; done
90
+ }
91
+ # AUTO-RELEASE AR3 (J355): `wildshard-config cook <tag>` writes COOK_ENGINE_REF and WILDSHARD_BUILD to cook.settings, which
92
+ # the unit reads after cook.env. A key there wins, blank too, exactly as systemd reads the two files; otherwise cook.env's.
93
+ cook_value() {
94
+ if [ -n "${COOK_SETTINGS:-}" ] && [ -r "$COOK_SETTINGS" ] && grep -q "^$2=" "$COOK_SETTINGS"; then env_value "$COOK_SETTINGS" "$2"
95
+ else env_value "$1" "$2"; fi
86
96
  }
87
97
  # Caddy's package ships a page on port 80. Only that file, byte for byte as dpkg recorded it, or this script's own
88
98
  # placeholder is replaced; any other file is VH9's and is never touched.
@@ -106,6 +116,9 @@ wait_http() {
106
116
 
107
117
  # --- Before anything
108
118
  if [ "$CHECK" = 0 ] && [ "$(id -u)" != 0 ]; then say stop 'Run with sudo, or pass --check'; exit 1; fi
119
+ # AUTO-RELEASE AR3: one rebuild at a time, whichever way it was started (a full run, --cook from wildshard-config cook,
120
+ # wildshard-apply <tag> rebuild), so two never move the cook's checkout or install into it at once.
121
+ if [ "$CHECK" = 0 ]; then exec 9>/run/wildshard-rebuild.lock; flock 9; fi
109
122
  for name in setup.sh setup-state.mjs pull.mjs dump.sh restore-check.sh uptime.sh health-stub.mjs wildshard-config wildshard-apply.mjs wildshard-agent.pub; do
110
123
  if [ ! -f "$HERE/$name" ]; then say stop "$name must sit beside rebuild.sh"; exit 1; fi
111
124
  done
@@ -121,6 +134,9 @@ if [ "$(uname -m)" != x86_64 ]; then say warn "The VPS is x86_64, and this is $(
121
134
  CPUS=$(nproc) SHARED_CPUS='AllowedCPUs=13-15'
122
135
  if [ "$CPUS" -lt 16 ]; then say warn "The units' AllowedCPUs assume 16 cores, and this has $CPUS"; SHARED_CPUS="# AllowedCPUs=13-15 needs 16 cores; this box has $CPUS"; fi
123
136
 
137
+ # AUTO-RELEASE AR3: --cook is step 4 alone; the box already has what steps 1 to 3 make.
138
+ PHASE2=0
139
+ if [ "$COOK_ONLY" = 0 ]; then
124
140
  # --- 1. Packages
125
141
  APT_UPDATED=0
126
142
  apt_update() { if [ "$APT_UPDATED" = 0 ]; then apt-get update -qq; APT_UPDATED=1; fi; }
@@ -375,6 +391,7 @@ else
375
391
  bash "$HERE/setup.sh"
376
392
  PHASE2=1
377
393
  fi
394
+ fi # COOK_ONLY: --cook skips steps 1 to 3
378
395
 
379
396
  # --- 4. The cook worker (C4, J28): its own user, an engine checkout it can read and not write, and its unit.
380
397
  have_cook_user() { id wildshard-cook >/dev/null 2>&1 && getent group wildshard-cook >/dev/null; }
@@ -426,7 +443,7 @@ EOF
426
443
  else say ok 'unprivileged user namespaces are not restricted here; no AppArmor profile needed'; fi
427
444
 
428
445
  COOK_REF='' COOK_READY=0
429
- if [ -r "$COOK_ENV" ] && cook_env_complete "$COOK_ENV"; then COOK_READY=1; COOK_REF=$(env_value "$COOK_ENV" COOK_ENGINE_REF); fi
446
+ if [ -r "$COOK_ENV" ] && cook_env_complete "$COOK_ENV"; then COOK_READY=1; COOK_REF=$(cook_value "$COOK_ENV" COOK_ENGINE_REF); fi
430
447
  if [ "$COOK_READY" = 0 ]; then
431
448
  if [ -r "$COOK_ENV" ] || [ "$CHECK" = 0 ]; then wait_for "Fill $COOK_ENV (engine tag, build, notify token, and R2 or a folder), then run rebuild.sh again"
432
449
  elif [ "$(id -u)" = 0 ]; then say would "wait for $COOK_ENV to be filled"
@@ -450,6 +467,10 @@ make_cook_source() {
450
467
  fi
451
468
  git -c safe.directory="$COOK_SRC" -C "$COOK_SRC" fetch --quiet --tags origin
452
469
  git -c safe.directory="$COOK_SRC" -C "$COOK_SRC" checkout --quiet --force --detach "$COOK_REF^{commit}"
470
+ # --cook (AR3): the tag must still be the build wildshard-config verified, or the worker would run other code than it reports.
471
+ if [ "$COOK_ONLY" = 1 ] && [ "$(cook_commit)" != "$(cook_value "$COOK_ENV" WILDSHARD_BUILD)" ]; then
472
+ say stop "$COOK_REF is $(cook_commit) on GitHub now, not the verified WILDSHARD_BUILD"; exit 1
473
+ fi
453
474
  rm -f "$COOK_SRC/.wildshard-installed"
454
475
  (cd "$COOK_SRC" && org_git env CI=1 HOME=/root COREPACK_ENABLE_DOWNLOAD_PROMPT=0 corepack "$PNPM" install --frozen-lockfile --ignore-scripts --config.confirmModulesPurge=false)
455
476
  printf '%s %s\n' "$COOK_REF" "$(cook_commit)" > "$COOK_SRC/.wildshard-installed"
@@ -474,8 +495,11 @@ if [ "$COOK_READY" = 1 ]; then
474
495
  if [ "$CHECK" = 0 ] && ! have_cook_read; then COOK_SOURCE_CHANGED=1; fi
475
496
  step "cook source readable by $COOK_GROUP" have_cook_read make_cook_read
476
497
  fi
477
- # What the running worker last read of cook.env, as a hash in its root-owned checkout: a changed file restarts it.
478
- cook_env_hash() { sha256sum < "$COOK_ENV" | cut -d' ' -f1; }
498
+ # What the running worker last read of cook.env and cook.settings, as a hash in its root-owned checkout: a changed file
499
+ # restarts it. With no cook.settings the hash is cook.env's alone, as it was before AR3.
500
+ cook_env_hash() {
501
+ { cat "$COOK_ENV"; cat "$COOK_SETTINGS" 2>/dev/null || true; } | sha256sum | cut -d' ' -f1
502
+ }
479
503
 
480
504
  if put "$SYSTEMD/wildshard-cook.service" 644 root:root <<EOF
481
505
  [Unit]
@@ -490,6 +514,8 @@ User=wildshard-cook
490
514
  Group=wildshard-cook
491
515
  WorkingDirectory=$COOK_SRC
492
516
  EnvironmentFile=$COOK_ENV
517
+ # AUTO-RELEASE AR3: the engine tag and build wildshard-config cook sets, read after the secrets so they win. Optional.
518
+ EnvironmentFile=-$COOK_SETTINGS
493
519
  # MS2.4b: the checkout is root's and the worker runs git in it as wildshard-cook, which git refuses ("dubious ownership")
494
520
  # unless the path is a safe.directory. Set here, not in a gitconfig the worker's home would have to hold.
495
521
  Environment=GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=safe.directory GIT_CONFIG_VALUE_0=$COOK_SRC
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/node
2
2
  // MMO-SERVER-PLAN MS3.5 (J266): the agents' way to rerun harden.sh or setup.sh as root, from a signed, landed release only.
3
3
  // wildshard-apply <tag> harden|setup|rebuild
4
+ // wildshard-apply <tag> verify AUTO-RELEASE AR2 (J355): print `commit <sha>` for a signed tag GitHub agrees on; runs nothing
4
5
  // <tag> is a release tag a signed channel file names: `vX.Y.Z` (production) or `server-staging-<sha>` (staging). The file is
5
6
  // checked exactly as pull.mjs checks it (Ed25519 over `body`, the release public key embedded below and in setup.sh), and its
6
7
  // `bundle.name` must sit under that tag. Its `engineBuild` commit is fetched from GitHub by hash, the tag must point at the
@@ -89,7 +90,7 @@ async function main() {
89
90
  { stdio: 'inherit', env: { PATH: '/usr/sbin:/usr/bin:/sbin:/bin', HOME: '/root', LANG: 'C.UTF-8', WILDSHARD_APPLY_LOCKED: '1' } });
90
91
  process.exit(locked.status ?? 1);
91
92
  }
92
- if (tag === undefined || which === undefined || rest.length > 0 || !(which in SCRIPTS)) stop('usage: wildshard-apply <tag> harden|setup|rebuild');
93
+ if (tag === undefined || which === undefined || rest.length > 0 || !(Object.hasOwn(SCRIPTS, which) || which === 'verify')) stop('usage: wildshard-apply <tag> harden|setup|rebuild|verify');
93
94
  if (!TAG.test(tag)) stop(`${tag} is not a release tag (vX.Y.Z or server-staging-<sha>)`);
94
95
  const key = test !== undefined ? process.env.WILDSHARD_APPLY_TEST_KEY ?? '' : RELEASE_PUBLIC_KEY;
95
96
  const commits = new Set((await signedFiles()).map(text => signedCommit(text, tag, key)).filter(commit => commit !== null));
@@ -97,12 +98,15 @@ async function main() {
97
98
  if (commits.size > 1) stop(`signed releases name ${tag} with different commits`);
98
99
  const [commit] = commits;
99
100
  say('ok', `${tag} is signed: engine ${commit}`);
101
+ if (test !== undefined && which === 'verify') { say('commit', commit); return; }
100
102
  if (test !== undefined) { say('would', `fetch ${commit} and run scripts/vps/${SCRIPTS[/** @type {'harden' | 'setup' | 'rebuild'} */ (which)]}`); return; }
101
103
 
102
104
  // The tag on GitHub must point at the signed commit (a peeled annotated tag, or a lightweight one).
103
105
  const refs = git(['ls-remote', ENGINE_GIT, `refs/tags/${tag}`, `refs/tags/${tag}^{}`]).trim().split('\n').filter(Boolean).map(row => row.split('\t'));
104
106
  const pointed = (refs.find(([, ref]) => ref === `refs/tags/${tag}^{}`) ?? refs.find(([, ref]) => ref === `refs/tags/${tag}`))?.[0];
105
107
  if (pointed !== commit) stop(`GitHub's ${tag} points at ${pointed ?? 'nothing'}, not the signed ${commit}`);
108
+ // verify: wildshard-config's cook verb reads this line and moves the cook to exactly this commit (AUTO-RELEASE AR4).
109
+ if (which === 'verify') { say('commit', commit); return; }
106
110
 
107
111
  // A fresh root-only folder for this commit: fetched by hash, unpacked from git's own objects, never a working copy.
108
112
  mkdirSync(WORK, { recursive: true, mode: 0o700 });
@@ -5,6 +5,9 @@
5
5
  # write the settings, restart the channel once, roll back if
6
6
  # it does not come up healthy
7
7
  # wildshard-config split <channel> setup.sh's: move the settings out of <channel>.env, once
8
+ # wildshard-config cook <tag> AUTO-RELEASE AR4 (J355): move the cook worker to vX.Y.Z and
9
+ # the build a signed release names for it; roll back on failure
10
+ # wildshard-config show cook print the cook worker's tag and build
8
11
  # Settings live in /etc/wildshard/<channel>.settings (640 root:wildshard-<channel>), which the units read after <channel>.env.
9
12
  # Secrets stay in the root-only .env, and this never prints one or writes one (it reads only the placement rows there, for
10
13
  # the pair check below): only the keys in SETTINGS below, each
@@ -167,11 +170,124 @@ def restart(channel):
167
170
  return healthy(channel)
168
171
 
169
172
 
173
+ # AUTO-RELEASE AR4 (J355): the cook worker's two non-secret keys. They go to /etc/wildshard/cook.settings (640
174
+ # root:wildshard-cook), which its unit and rebuild.sh read after the root-only cook.env (the notify token and R2 writer keys,
175
+ # never read here), so they win. The build is never the caller's: wildshard-apply's verify reads it from a signed channel
176
+ # release that names the tag, after checking GitHub's tag points at it. rebuild.sh --cook then moves the checkout, installs,
177
+ # restarts the worker and waits for its /health.
178
+ COOK_KEYS = ('COOK_ENGINE_REF', 'WILDSHARD_BUILD')
179
+ COOK_TAG = r'v[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}'
180
+ COOK_HEADER = ['# AUTO-RELEASE AR4 (J355): the cook worker\'s engine tag and build, read after cook.env. Change them with',
181
+ '# `sudo wildshard-config cook <tag>`; the secrets stay in cook.env.']
182
+ CLEAN = {'PATH': '/usr/sbin:/usr/bin:/sbin:/bin', 'HOME': '/root', 'LANG': 'C.UTF-8'}
183
+
184
+
185
+ def helper(name):
186
+ """A root-owned helper, or the test's stand-in (never as root)."""
187
+ if TEST:
188
+ return os.environ.get(f'WILDSHARD_CONFIG_TEST_{name.upper()}', '/nonexistent')
189
+ return {'apply': '/usr/local/sbin/wildshard-apply', 'rebuild': '/opt/wildshard-vps/rebuild.sh'}[name]
190
+
191
+
192
+ def helper_env():
193
+ if not TEST:
194
+ return dict(CLEAN)
195
+ return {**CLEAN, 'PATH': os.environ.get('PATH', CLEAN['PATH']),
196
+ **{key: value for key, value in os.environ.items() if key.startswith(('WILDSHARD_APPLY_TEST', 'WILDSHARD_CONFIG_TEST'))}}
197
+
198
+
199
+ def cook_rebuild():
200
+ """rebuild.sh's cook step alone, as root, from the root-owned copy. It exits non-zero unless the worker answers /health.
201
+ It runs in its own session with SIGPIPE and SIGHUP ignored (restore_signals=False keeps them so), so a dropped ssh
202
+ caller can't kill it halfway through a checkout or an install."""
203
+ return subprocess.run(['bash' if TEST else '/usr/bin/bash', helper('rebuild'), '--cook'], cwd='/', env=helper_env(),
204
+ restore_signals=False, start_new_session=True).returncode == 0
205
+
206
+
207
+ def say(line, error=False):
208
+ """Print, but never let a caller that went away stop a move or its rollback."""
209
+ try:
210
+ print(line, file=sys.stderr if error else sys.stdout, flush=True)
211
+ except OSError:
212
+ pass
213
+
214
+
215
+ def show_cook():
216
+ rows = [row for row in read_rows(f'{ETC}/cook.settings') if key_of(row) in COOK_KEYS]
217
+ for row in rows:
218
+ print(row)
219
+ if not rows:
220
+ print('note no cook.settings: the cook worker runs what cook.env names')
221
+ return 0
222
+
223
+
224
+ def cook(args):
225
+ if len(args) != 2 or re.fullmatch(COOK_TAG, args[1]) is None:
226
+ stop('usage: wildshard-config cook vX.Y.Z')
227
+ tag, rebuild = args[1], helper('rebuild')
228
+ try:
229
+ with open(rebuild, encoding='utf-8') as handle:
230
+ knows = '[ "$1" = --cook ]' in handle.read()
231
+ except OSError:
232
+ knows = False
233
+ if not knows:
234
+ stop(f'{rebuild} has no --cook yet: apply a signed tag that carries it first (wildshard-apply <tag> harden)')
235
+ checked = subprocess.run([helper('apply'), tag, 'verify'], cwd='/', env=helper_env(), capture_output=True, text=True)
236
+ found = re.search(r'^commit\s+([0-9a-f]{40})$', checked.stdout, re.M)
237
+ if checked.returncode != 0 or found is None:
238
+ reason = ((checked.stderr or checked.stdout).strip().splitlines() or ['no commit'])[-1]
239
+ stop(f'{tag} is not a tag a signed release names: {reason}')
240
+ commit = found.group(1)
241
+ # One move at a time, held through the rebuild and any rollback, as `set` holds its channel.
242
+ import fcntl
243
+ lock = os.fdopen(os.open(f'{ETC}/.cook.settings.lock', os.O_WRONLY | os.O_CREAT | os.O_NOFOLLOW | os.O_CLOEXEC, 0o600), 'w')
244
+ fcntl.flock(lock, fcntl.LOCK_EX)
245
+ # From here on a dropped caller changes nothing: no hangup, no broken pipe, and any failure rolls back.
246
+ import signal
247
+ signal.signal(signal.SIGHUP, signal.SIG_IGN)
248
+ signal.signal(signal.SIGPIPE, signal.SIG_IGN)
249
+ path = f'{ETC}/cook.settings'
250
+ before = read_rows(path)
251
+ rows = [row for row in (list(before) or list(COOK_HEADER)) if key_of(row) not in COOK_KEYS]
252
+ rows += [f'COOK_ENGINE_REF={tag}', f'WILDSHARD_BUILD={commit}']
253
+ try:
254
+ if rows == before:
255
+ say(f'ok cook.settings already names {tag}')
256
+ else:
257
+ write_rows(path, rows, 'cook')
258
+ say(f'doing cook {tag} at {commit}')
259
+ if cook_rebuild():
260
+ say(f'ok the cook worker runs {tag} ({commit[:8]})')
261
+ return 0
262
+ except BaseException:
263
+ pass
264
+ if rows == before:
265
+ say(f'stop the cook worker is not healthy on {tag}: look at journalctl -u wildshard-cook', error=True)
266
+ return 2
267
+ # It did not come back: the old tag goes back and is rebuilt, so a bad move never leaves the cook down.
268
+ if before:
269
+ write_rows(path, before, 'cook')
270
+ elif os.path.exists(path):
271
+ os.unlink(path)
272
+ say(f'undo the cook worker did not come up on {tag}; the old tag is back', error=True)
273
+ if not cook_rebuild():
274
+ say('stop the cook worker is not healthy on its old tag either: look at journalctl -u wildshard-cook', error=True)
275
+ return 2
276
+ return 1
277
+
278
+
170
279
  def main(argv):
171
280
  production = '--production' in argv
172
281
  args = [arg for arg in argv if arg != '--production']
282
+ if args and args[0] == 'cook':
283
+ return cook(args)
284
+ if args[:2] == ['show', 'cook'] and len(args) == 2:
285
+ return show_cook()
286
+ if args[:2] == ['set', 'cook']:
287
+ stop('the cook worker moves with: wildshard-config cook vX.Y.Z')
173
288
  if not args or args[0] not in ('show', 'set', 'split'):
174
- stop('usage: wildshard-config show <channel> | set <channel> <KEY> <value> [<KEY> <value>]... [--production] | split <channel>')
289
+ stop('usage: wildshard-config show <channel> | set <channel> <KEY> <value> [<KEY> <value>]... [--production] | split <channel> '
290
+ '| cook vX.Y.Z | show cook')
175
291
  verb = args[0]
176
292
  if len(args) < 2 or args[1] not in CHANNELS:
177
293
  stop('the channel is production or staging')
@@ -13,23 +13,28 @@
13
13
  // commit, and `origin plan/<slug>` deleted (`git push --delete`, which the pre-push lets through without a gate)
14
14
  // only if the remote branch is landed too, under a lease on the SHA it had. A worktree that fails any check is kept
15
15
  // and named with the reason; an unlanded branch, local or remote, is never touched. Every run appends one receipt.
16
+ // REPO-DISK DK11: a removed worktree takes its tmp/ and build output with it (they are ignored files inside it), and
17
+ // a real run's receipt records the `df` bytes it freed (other writers move df too, so it is a measure, not a ledger).
18
+ // A worktree kept because it is unlanded or dirty and untouched for --stale hours (default 72) is named in the
19
+ // receipt with its `du` size, so the coordinator's brief can put it to its owner.
16
20
  import {execFileSync,spawnSync} from 'node:child_process';
17
- import {appendFileSync,existsSync,mkdirSync,readdirSync,readlinkSync,realpathSync,statSync} from 'node:fs';
21
+ import {appendFileSync,existsSync,mkdirSync,readdirSync,readlinkSync,realpathSync,statSync,statfsSync} from 'node:fs';
18
22
  import {homedir} from 'node:os';
19
23
  import {basename,dirname,join,resolve,sep} from 'node:path';
20
24
  import {pidLock} from './pid-lock.mjs';
21
25
  import {WORKTREES} from './worktree-ignore.mjs';
22
26
 
23
- const REAP_USAGE='usage: node scripts/worktree.mjs reap [--dry-run] [--grace=HOURS] [--no-remote]';
27
+ const REAP_USAGE='usage: node scripts/worktree.mjs reap [--dry-run] [--grace=HOURS] [--stale=HOURS] [--no-remote]';
24
28
 
25
- /** @param {string[]} argv the arguments after `reap` @returns {{dryRun:boolean,graceHours:number,remote:boolean}} */
29
+ /** @param {string[]} argv the arguments after `reap` @returns {{dryRun:boolean,graceHours:number,staleHours:number,remote:boolean}} */
26
30
  export function parseReapArgs(argv){
27
- const options={dryRun:false,graceHours:24,remote:true};
31
+ const options={dryRun:false,graceHours:24,staleHours:72,remote:true};
28
32
  for(const arg of argv){
29
33
  const [name,value]=arg.split('=',2);
30
34
  if(arg==='--dry-run')options.dryRun=true;
31
35
  else if(arg==='--no-remote')options.remote=false;
32
36
  else if(name==='--grace'&&value!==undefined&&/^\d+(?:\.\d+)?$/u.test(value))options.graceHours=Number(value);
37
+ else if(name==='--stale'&&value!==undefined&&/^\d+(?:\.\d+)?$/u.test(value))options.staleHours=Number(value);
33
38
  else throw new Error(`unknown option ${arg}\n${REAP_USAGE}`);
34
39
  }
35
40
  return options;
@@ -109,6 +114,24 @@ export function lastActivity(tree,gitDir){
109
114
  return newest;
110
115
  }
111
116
 
117
+ /** Free bytes on the volume holding `path` (what `df` shows as available). @param {string} path */
118
+ export function freeBytes(path){
119
+ const volume=statfsSync(path);
120
+ return volume.bavail*volume.bsize;
121
+ }
122
+
123
+ /** `du -sk` of `path` as GiB text, within `budgetMs` (all of a run's `du` calls share one budget), or why not.
124
+ * @param {string} path @param {number} budgetMs */
125
+ function duSize(path,budgetMs){
126
+ if(budgetMs<1000)return 'size not measured (du budget spent)';
127
+ const listed=spawnSync('du',['-sk',path],{encoding:'utf8',timeout:budgetMs});
128
+ const kib=Number((listed.stdout??'').split('\t')[0]);
129
+ return listed.status===0&&Number.isFinite(kib)&&kib>0?`${(kib/1_048_576).toFixed(1)} GiB`:'size unknown';
130
+ }
131
+
132
+ /** Signed MiB text for a byte count; the sign is taken after rounding, so a tiny change reads +0. @param {number} bytes */
133
+ const mib=bytes=>{const size=Math.round(bytes/1_048_576);return `${size<0?'-':'+'}${Math.abs(size).toLocaleString('en-US')} MiB`;};
134
+
112
135
  /** Is `sha` on origin/main itself? @param {string} canonical @param {string} sha */
113
136
  export const onMain=(canonical,sha)=>sha!==''&&tryGit(canonical,['merge-base','--is-ancestor',sha,'origin/main']).status===0;
114
137
  /** Commits of `sha` that origin/main lacks. @param {string} canonical @param {string} sha */
@@ -141,9 +164,30 @@ export function reap(canonical,options,env,say){
141
164
  const fetched=tryGit(canonical,['fetch','--quiet','origin','+refs/heads/main:refs/remotes/origin/main']);
142
165
  if(fetched.status!==0){say(`reap STOP cannot fetch origin main: ${fetched.stderr.trim()}`);return 1;}
143
166
  const directories=processDirectories();
144
- const realRoot=realpathSync(canonical),now=Date.now();
167
+ const realRoot=realpathSync(canonical),now=Date.now(),freeBefore=freeBytes(canonical);
145
168
  /** @type {string[]} */const reaped=[];
146
169
  /** @type {string[]} */const kept=[];
170
+ /** @type {string[]} */const stale=[];
171
+ const duUntil=now+60_000;
172
+ /** DK11: an unlanded or dirty worktree untouched for --stale hours goes on the receipt with its size. Untouched
173
+ * means no Git activity, no change to a dirty path, and no process inside.
174
+ * @param {string} slug @param {string} path @param {string} why @param {string[]} [dirty] porcelain lines */
175
+ const staleCheck=(slug,path,why,dirty=[])=>{
176
+ let gitDir='',physical='';
177
+ try{gitDir=git(path,['rev-parse','--absolute-git-dir']).trim();physical=realpathSync(path);}catch{return;}
178
+ if(directories===undefined||[...directories.values()].some(cwd=>cwd===physical||cwd.startsWith(`${physical}${sep}`)))return;
179
+ let newest=0;
180
+ try{newest=lastActivity(path,gitDir);}catch{return;}
181
+ for(const line of dirty.slice(0,500)){
182
+ const name=line.slice(3).split(' -> ').at(-1)??'';
183
+ try{newest=Math.max(newest,statSync(join(path,name.replaceAll(/^"|"$/gu,''))).mtimeMs);}catch{/* Deleted: no mtime. */}
184
+ }
185
+ const idle=(now-newest)/3_600_000;
186
+ if(idle<options.staleHours)return;
187
+ const entry=`${slug} (${duSize(path,duUntil-Date.now())}, idle ${(idle/24).toFixed(1)} d, ${why})`;
188
+ stale.push(entry);
189
+ say(`stale ${entry}`);
190
+ };
147
191
  /** @type {string[]} */const remotes=[];
148
192
  for(const tree of linkedWorktrees(canonical)){
149
193
  const slug=basename(tree.path),branch=`refs/heads/plan/${slug}`;
@@ -156,9 +200,9 @@ export function reap(canonical,options,env,say){
156
200
  if(tree.branch!==branch){keep(`on ${tree.branch===''?'a detached HEAD':tree.branch}, not plan/${slug}`);continue;}
157
201
  if(!existsSync(tree.path)){keep('its folder is gone; `git worktree prune` clears it');continue;}
158
202
  // Landed means the tip is on main itself (land fast-forwards): \`git cherry\` would miss a merge's own changes.
159
- if(!onMain(canonical,tree.head)){keep(`${String(aheadOfMain(canonical,tree.head))} commit(s) not on main`);continue;}
203
+ if(!onMain(canonical,tree.head)){const why=`${String(aheadOfMain(canonical,tree.head))} commit(s) not on main`;keep(why);staleCheck(slug,tree.path,why);continue;}
160
204
  const dirty=git(tree.path,['--no-optional-locks','status','--porcelain','--untracked-files=all']).split('\n').filter(Boolean);
161
- if(dirty.length>0){keep(`${String(dirty.length)} uncommitted path(s), first ${dirty[0]?.slice(3)}`);continue;}
205
+ if(dirty.length>0){const why=`${String(dirty.length)} uncommitted path(s)`;keep(`${why}, first ${dirty[0]?.slice(3)}`);staleCheck(slug,tree.path,why,dirty);continue;}
162
206
  const gitDir=git(tree.path,['rev-parse','--absolute-git-dir']).trim();
163
207
  const busy=(/** @type {Map<number,string>|undefined} */ seen)=>seen===undefined?'cannot list process working directories'
164
208
  :[...seen].filter(([,path])=>path===physical||path.startsWith(`${physical}${sep}`)).map(([pid])=>String(pid)).join(', ');
@@ -206,8 +250,10 @@ export function reap(canonical,options,env,say){
206
250
  else{remotes.push(`${slug} delete failed`);say(` ${slug}: origin plan/${slug} not deleted: ${pushed.stderr.trim()}`);}
207
251
  }
208
252
  if(!options.dryRun)tryGit(canonical,['worktree','prune']);
253
+ const freed=options.dryRun?'':`; df ${mib(freeBytes(canonical)-freeBefore)}`;
254
+ if(freed!=='')say(`df free changed ${freed.slice(5)} during the run`);
209
255
  const lane=/^[a-z][a-z0-9_-]*$/u.test(env.WS_LANE_NAME??'')?String(env.WS_LANE_NAME):'reaper';
210
- const line=`${new Date().toISOString().slice(11,16)} UTC ${lane} worktree reap ${basename(canonical)}${options.dryRun?' (dry run)':''}: ${reaped.length===0?'none':`${options.dryRun?'would reap':'reaped'} ${reaped.join(', ')}`}; kept ${kept.length===0?'none':kept.join(', ')}${remotes.length===0?'':`; origin ${remotes.join(', ')}`}`;
256
+ const line=`${new Date().toISOString().slice(11,16)} UTC ${lane} worktree reap ${basename(canonical)}${options.dryRun?' (dry run)':''}: ${reaped.length===0?'none':`${options.dryRun?'would reap':'reaped'} ${reaped.join(', ')}`}; kept ${kept.length===0?'none':kept.join(', ')}${remotes.length===0?'':`; origin ${remotes.join(', ')}`}${freed}${stale.length===0?'':`; stale ${String(options.staleHours)} h+: ${stale.join(', ')}`}`;
211
257
  const file=resolve(env.WS_LANE_RECEIPTS??join(homedir(),'.cache/wildshard/lane-receipts.log'));
212
258
  try{mkdirSync(dirname(file),{recursive:true});appendFileSync(file,`${line}\n`);}catch(error){say(`reap receipt unavailable: ${String(error)}`);}
213
259
  return 0;
@@ -13,7 +13,9 @@
13
13
  //
14
14
  // Never copied: .env*, tmp/, .review/, .cache/, browser profiles, leases, ports, push proofs; the worktree gets
15
15
  // only tracked files and its own install. Each step's seconds go to <worktree>/tmp/worktree-setup.json and, with
16
- // WS_LANE_NAME set, one line in ~/.cache/wildshard/lane-receipts.log. Removal is DS13's reaper: `reap`
16
+ // WS_LANE_NAME set, one line in ~/.cache/wildshard/lane-receipts.log. REPO-DISK DK10: the record and the receipt also
17
+ // carry the volume's `df` free bytes before and after and the worktree's `du`: build output inside a worktree costs
18
+ // nothing until written (clones), so `df` shows the real cost and `du` the apparent one. Removal is DS13's reaper: `reap`
17
19
  // (scripts/worktree-reap.mjs) removes landed, clean, idle worktrees with their branches, local and on origin.
18
20
  import {execFileSync,spawn,spawnSync} from 'node:child_process';
19
21
  import {randomBytes} from 'node:crypto';
@@ -22,7 +24,7 @@ import {homedir} from 'node:os';
22
24
  import {basename,dirname,join,resolve} from 'node:path';
23
25
  import {pathToFileURL} from 'node:url';
24
26
  import {WORKTREES} from './worktree-ignore.mjs';
25
- import {parseReapArgs,reap} from './worktree-reap.mjs';
27
+ import {freeBytes,parseReapArgs,reap} from './worktree-reap.mjs';
26
28
 
27
29
  export const SLUG=/^[a-z0-9][a-z0-9._-]{0,63}$/u;
28
30
  const REGULAR=/^100(?:644|755)$/u;
@@ -247,12 +249,22 @@ function run(cwd,command,args,env){
247
249
  if(result.status!==0)throw new StopError(`${command} ${args.join(' ')} exited ${String(result.status)}`);
248
250
  }
249
251
 
252
+ /** DK10: what the new worktree cost the volume (`df` free before minus after; other writers move it too) and what
253
+ * `du` counts in it (clones in full). @param {string} tree @param {number} freeBefore free bytes before setup */
254
+ export function diskCost(tree,freeBefore){
255
+ const freeAfter=freeBytes(tree),listed=spawnSync('du',['-sk',tree],{encoding:'utf8',timeout:120_000});
256
+ const kib=Number((listed.stdout??'').split('\t')[0]),duKib=listed.status===0&&Number.isFinite(kib)?kib:undefined;
257
+ const cost=freeBefore-freeAfter,costMib=Math.round(cost/1_048_576);
258
+ const text=`df ${costMib<0?'gained':'cost'} ${Math.abs(costMib).toLocaleString('en-US')} MiB, du ${duKib===undefined?'unknown':`${(duKib/1_048_576).toFixed(2)} GiB`}, free ${(freeAfter/2**30).toFixed(1)} GiB`;
259
+ return {freeBefore,freeAfter,dfCostBytes:cost,duKib,text};
260
+ }
261
+
250
262
  /** @param {string[]} argv @param {string} [cwd] @param {NodeJS.ProcessEnv} [env] @returns {number} */
251
263
  export function worktreeNew(argv,cwd=process.cwd(),env=process.env){
252
264
  const options=parseArgs(argv),canonical=canonicalRoot(cwd),common=join(canonical,'.git');
253
265
  const relative=`${WORKTREES}/${options.slug}`,tree=join(canonical,relative),branch=`plan/${options.slug}`;
254
266
  /** @type {{step:string,seconds:number,detail?:string}[]} */const steps=[];
255
- const began=performance.now();
267
+ const began=performance.now(),freeBefore=freeBytes(canonical);
256
268
  /** @param {string} step @param {() => (string|void)} work */
257
269
  const timed=(step,work)=>{
258
270
  const started=performance.now(),detail=work();
@@ -307,13 +319,15 @@ export function worktreeNew(argv,cwd=process.cwd(),env=process.env){
307
319
  catch(error){process.stderr.write(`worktree: timings refresh not started (${error instanceof Error?error.message:String(error)})\n`);}
308
320
  }
309
321
  const total=Number(((performance.now()-began)/1000).toFixed(2));
310
- const record={slug:options.slug,branch,base,tree,total,steps,finished:new Date().toISOString()};
322
+ const disk=diskCost(tree,freeBefore);
323
+ process.stdout.write(`worktree disk ${disk.text}\n`);
324
+ const record={slug:options.slug,branch,base,tree,total,steps,disk,finished:new Date().toISOString()};
311
325
  mkdirSync(join(tree,'tmp'),{recursive:true});
312
326
  writeFileSync(join(tree,'tmp/worktree-setup.json'),`${JSON.stringify(record,null,2)}\n`);
313
327
  const lane=/^[a-z][a-z0-9_-]*$/u.test(env.WS_LANE_NAME??'')?String(env.WS_LANE_NAME):'';
314
328
  if(lane!==''){
315
329
  const file=resolve(env.WS_LANE_RECEIPTS??join(homedir(),'.cache/wildshard/lane-receipts.log'));
316
- try{mkdirSync(dirname(file),{recursive:true});appendFileSync(file,`${new Date().toISOString().slice(11,16)} UTC ${lane} worktree new ${basename(canonical)}/${relative} on ${branch} from ${base.slice(0,10)} in ${total.toFixed(1)} s (${steps.map(row=>`${row.step} ${row.seconds.toFixed(1)}`).join(', ')})\n`);}
330
+ try{mkdirSync(dirname(file),{recursive:true});appendFileSync(file,`${new Date().toISOString().slice(11,16)} UTC ${lane} worktree new ${basename(canonical)}/${relative} on ${branch} from ${base.slice(0,10)} in ${total.toFixed(1)} s (${steps.map(row=>`${row.step} ${row.seconds.toFixed(1)}`).join(', ')}); ${disk.text}\n`);}
317
331
  catch(error){process.stderr.write(`worktree receipt unavailable: ${String(error)}\n`);}
318
332
  }
319
333
  const name=lane===''?'<lane>':lane;
@@ -5,7 +5,7 @@ import { installGltfKtx2 } from '../core/ktx2';
5
5
  import { pageScope } from '../../../shared/engine/app/resources';
6
6
  import { waitFor } from '../../../shared/engine/boot/waits';
7
7
  import { buildYield, WORK_SLICE_MS, workSlice, type WorkSlice } from '../../../shared/engine/core/workSlice';
8
- import { buildHeld, frameBudgetSpent, frameDue, presenting } from '../../../shared/engine/core/framePacing';
8
+ import { buildHeld, buildUrgent, frameBudgetSpent, frameDue, presenting } from '../../../shared/engine/core/framePacing';
9
9
 
10
10
  // FileLoader starts every network read immediately. Only the parser's temporary
11
11
  // meshopt/image/geometry allocations share this page-wide decode slot.
@@ -38,7 +38,7 @@ class BoundedRigLoader extends GLTFLoader implements RigLoader {
38
38
  // The previous read/consumer can span drawn frames. Its wall clock
39
39
  // cannot charge that wait to this parse's current live CPU budget.
40
40
  const pause = presenting()
41
- ? (buildHeld() || !frameDue()) && !frameBudgetSpent(WORK_SLICE_MS) ? undefined : buildYield(pageScope)
41
+ ? (buildHeld() || buildUrgent() || !frameDue()) && !frameBudgetSpent(WORK_SLICE_MS) ? undefined : buildYield(pageScope)
42
42
  : (decodeSlice ??= workSlice(undefined, pageScope)).check();
43
43
  await waitFor('GLTF decode pacing', Promise.resolve(pause), 'work');
44
44
  }
@@ -3,17 +3,43 @@ import { captureBrowserError, engineScreenTags } from '../telemetry/browserError
3
3
  import { beginLoadingWait, finishLoadingWait } from '../../../shared/engine/boot/loadingWaits';
4
4
  import { currentLoadingTimeline } from '../../../shared/engine/boot/timeline';
5
5
  import { bootProgress } from '../../../shared/engine/boot/timing';
6
+ import { diagnosticNow } from '../../../shared/engine/core/clock';
6
7
 
7
8
  /** A missing or interrupted transport, never a parsed-content or admission refusal. */
8
9
  export class ContentReadError extends Error {
9
10
  readonly url: string;
10
11
  /** RL8: the server answered that the file is not there (404, or the HTML fallback): a retry of the same URL can't help */
11
12
  readonly missing: boolean;
12
- constructor(url: string, detail: string, cause?: unknown, missing = false) {
13
+ /** FM6: the HTTP status the server answered, its `Retry-After` in ms (seconds or a date, at most 10 min), and whether the bytes
14
+ * arrived but failed their length check (mode 44). Null and false when not measured. */
15
+ readonly status: number | null;
16
+ readonly retryAfterMs: number | null;
17
+ readonly broken: boolean;
18
+ constructor(url: string, detail: string, cause?: unknown, missing = false, measured: { status?: number; retryAfterMs?: number | null; broken?: boolean } = {}) {
13
19
  super(`Read failed: ${new URL(url, 'https://content.invalid').pathname} · ${detail}`, { cause });
14
20
  this.name = 'ContentReadError'; this.url = url; this.missing = missing;
21
+ this.status = measured.status ?? null; this.retryAfterMs = measured.retryAfterMs ?? null; this.broken = measured.broken ?? false;
15
22
  }
16
23
  }
24
+ /** FM6 mode 34: a `Retry-After` of seconds or an HTTP date, in ms from `now`; anything else (or past 10 min) is no answer. */
25
+ export function contentRetryAfterMs(header: string | null, now: number = Date.now()): number | null {
26
+ if (header === null) return null;
27
+ const text = header.trim(), ms = /^\d{1,6}$/u.test(text) ? Number(text) * 1000 : /[a-z]/iu.test(text) ? Date.parse(text) - now : Number.NaN;
28
+ return Number.isFinite(ms) && ms >= 0 && ms <= 600_000 ? Math.round(ms) : null;
29
+ }
30
+ /** The spawn book (`assets/spawn/packs-<build>.json`): mode 35 names it apart from the packs it lists. */
31
+ const BOOK = /(?:^|\/)assets\/spawn\/packs-[^/]+\.json$/u;
32
+ /** FM6: the catalogue's ids for a failed read, and the line a player reads (meta MMO-FAILURE-MODES-PLAN modes 33 to 35, 44). The
33
+ * address and raw detail stay in the error for Copy diagnostics. */
34
+ export function contentFailure(error: ContentReadError): { readonly failmode: string; readonly line: string } {
35
+ const book = BOOK.test(new URL(error.url, 'https://content.invalid').pathname);
36
+ if (book) return { failmode: 'book-5xx', line: 'The world\'s book didn\'t arrive' };
37
+ if (error.status === 429) return { failmode: 'packs-429', line: 'Download paused · the server is busy' };
38
+ if (error.broken) return { failmode: 'packs-5xx pack-broken', line: 'Download paused · a file didn\'t verify' };
39
+ if (error.missing) return { failmode: 'packs-5xx', line: 'Download paused · a file isn\'t there' };
40
+ if (error.status !== null) return { failmode: 'packs-5xx', line: 'Download paused · the server had an error' };
41
+ return { failmode: 'packs-5xx', line: 'Download paused · the connection dropped' };
42
+ }
17
43
  export interface ContentReadOptions {
18
44
  fetch: (url: string, init: RequestInit) => Promise<Response>;
19
45
  signal?: AbortSignal;
@@ -38,10 +64,11 @@ function html(bytes: Uint8Array): boolean {
38
64
  async function body(response: Response, url: string, maximum: number, read?: (bytes: number) => void, expected?: number,
39
65
  received?: (response: Response) => void): Promise<Uint8Array<ArrayBuffer>> {
40
66
  const sizeError = (): Error => expected === undefined ? new Error('Content wire size exceeds cap')
41
- : new ContentReadError(url, 'byte length differs from manifest');
67
+ : new ContentReadError(url, 'byte length differs from manifest', undefined, false, { broken: true });
42
68
  if (!response.ok || /\btext\/html\b/iu.test(response.headers.get('content-type') ?? '')) {
43
69
  await response.body?.cancel().catch(() => undefined);
44
- throw new ContentReadError(url, response.ok ? 'missing content (HTML response)' : `HTTP ${response.status}`, undefined, response.ok || response.status === 404);
70
+ throw new ContentReadError(url, response.ok ? 'missing content (HTML response)' : `HTTP ${response.status}`, undefined, response.ok || response.status === 404,
71
+ response.ok ? {} : { status: response.status, retryAfterMs: contentRetryAfterMs(response.headers.get('retry-after')) });
45
72
  }
46
73
  const length = response.headers.get('content-length');
47
74
  if (length !== null && (!/^\d+$/u.test(length) || Number(length) > maximum)) {
@@ -94,7 +121,13 @@ function shellRetry(scope: Scope, error: ContentReadError, retry: () => void): v
94
121
  const button = document.querySelector<HTMLButtonElement>('.ws-load .ws-load-retry');
95
122
  if (button === null) return;
96
123
  const foot = document.querySelector<HTMLElement>('.ws-load [data-el="foot"]');
97
- if (foot !== null) foot.textContent = error.message;
124
+ // FM6: the shell's foot says what failed in a player's words with the catalogue's id, as Loading's does (the FM3 proof caught
125
+ // START's boot tables showing their address and HTTP status here); the address stays in the error for Copy diagnostics
126
+ if (foot !== null) {
127
+ const failure = contentFailure(error);
128
+ foot.textContent = failure.line; foot.dataset['failmode'] = failure.failmode;
129
+ scope.onDispose(() => { if (foot.dataset['failmode'] === failure.failmode) delete foot.dataset['failmode']; });
130
+ }
98
131
  button.hidden = false;
99
132
  scope.listen(button, 'click', retry);
100
133
  }
@@ -122,7 +155,7 @@ async function retryFromCard(error: ContentReadError, signal?: AbortSignal): Pro
122
155
  reportStall(target, error, timeline?.snapshot().current ?? null);
123
156
  id = beginLoadingWait(error.message, { kind: 'file', resource: error.url,
124
157
  owner: timeline?.waitOwner ?? null, step: timeline?.snapshot().current ?? null,
125
- retry: () => {for (const retry of pending) retry();}, retryNavigation: false });
158
+ retry: () => {for (const retry of pending) retry();}, retryNavigation: false, ...contentFailure(error) });
126
159
  shellRetry(scope, error, () => {for (const retry of pending) retry();});
127
160
  });
128
161
  } finally {
@@ -133,6 +166,20 @@ async function retryFromCard(error: ContentReadError, signal?: AbortSignal): Pro
133
166
  }
134
167
  }
135
168
 
169
+ /** FM6 mode 34: the server's own wait, on the card as `packs-429` with its countdown; the read's signal cancels it. */
170
+ async function busyWait(error: ContentReadError, ms: number, signal?: AbortSignal): Promise<void> {
171
+ const scope = new Scope('content-read-busy'), timeline = currentLoadingTimeline();
172
+ const id = typeof document === 'undefined' ? undefined : beginLoadingWait(error.message, { kind: 'file', resource: error.url, owner: timeline?.waitOwner ?? null,
173
+ step: timeline?.snapshot().current ?? null, ...contentFailure(error), dueAt: diagnosticNow() + ms });
174
+ try {
175
+ await new Promise<void>((resolve, reject) => {
176
+ if (signal?.aborted === true) { reject(abortReason(signal)); return; }
177
+ if (signal !== undefined) scope.listen(signal, 'abort', () => { reject(abortReason(signal)); }, { once: true });
178
+ scope.timeout(ms, resolve);
179
+ });
180
+ } finally { if (id !== undefined) finishLoadingWait(id); scope.dispose(); }
181
+ }
182
+
136
183
  /** Read a complete bounded file. Retry one confirmed lost/missing transport immediately,
137
184
  * then leave the same load pending on RETRY. An optional first-party manifest
138
185
  * length shares this recovery; ordinary caps, parsing and hashes stay strict. */
@@ -151,6 +198,8 @@ export async function readContent(url: string, maximum: number, options: Content
151
198
  options.signal?.throwIfAborted();
152
199
  if (!(error instanceof ContentReadError)) throw error;
153
200
  if (error.missing && options.missingThrows === true) throw error;
201
+ // FM6 mode 34: a 429 that says when waits that long before the retry the budget already allows; the count is unchanged
202
+ if (error.retryAfterMs !== null && error.retryAfterMs > 0) await busyWait(error, error.retryAfterMs, options.signal);
154
203
  if (!retried) { retried = true; }
155
204
  else {
156
205
  if (options.instead !== undefined && await options.instead(error)) throw error;
@@ -44,7 +44,7 @@
44
44
  import { GPU_FILES as ENGINE_GPU_FILES } from './ktx2.generated';
45
45
  import { TIER } from '../core/tier';
46
46
  import { pageURL } from './contentBase';
47
- import { CONTENT_CAPS, PAGE_LEVEL } from '../../../shared/engine/core/config';
47
+ import { PAGE_LEVEL, PHONE_PLAYING_BYTES } from '../../../shared/engine/core/config';
48
48
  import { harnessPins } from '../../../shared/engine/app/identity';
49
49
  import { ktx2Probe, ktx2ProbeResult, type Ktx2Probe } from '../render/ktx2Probe';
50
50
 
@@ -106,8 +106,8 @@ export function setDeviceTexForm(memory: DeviceTexForm | null): void { deviceFor
106
106
  /** G188's automatic tier policy: measured over-cap phone builds use KTX2 before the first visit, never desktop. */
107
107
  export function autoTexturePolicy(mode: TexMode | undefined, imagesFirstPlayingBytes?: number): { mode: TexMode; why: string } | undefined {
108
108
  if (imagesFirstPlayingBytes !== undefined && (!Number.isSafeInteger(imagesFirstPlayingBytes) || imagesFirstPlayingBytes < 0)) throw new RangeError('Invalid images-first playing estimate');
109
- if (TIER === 'phone' && imagesFirstPlayingBytes !== undefined && imagesFirstPlayingBytes > CONTENT_CAPS.playing) {
110
- return { mode: 'ktx2', why: `auto: images-first playing estimate ${imagesFirstPlayingBytes} exceeds ${CONTENT_CAPS.playing}` };
109
+ if (TIER === 'phone' && imagesFirstPlayingBytes !== undefined && imagesFirstPlayingBytes > PHONE_PLAYING_BYTES) {
110
+ return { mode: 'ktx2', why: `auto: images-first playing estimate ${imagesFirstPlayingBytes} exceeds ${PHONE_PLAYING_BYTES}` };
111
111
  }
112
112
  return mode === undefined ? undefined : { mode, why: 'auto: level tier texture policy' };
113
113
  }