feat(m4): cap map images at Full HD and shrink on import (#85)

The owner fixed the map cap at 1920 px on the long side and 1080 px on the
short side (either orientation). fff-vtt import now shrinks a larger source
with ImageMagick Lanczos before tiling (alpha kept, proportions preserved,
no upscaling), prints `scaled WxH -> WxH`, and records the shrunk w/h; a
source at or below the cap is untouched, so the pixel-identity tests still
hold. A scene row already above the cap is not rewritten: the server logs
`map_above_cap` at startup and serve prints the re-import advice.

Docs: ADR-0023, DESIGN 3.3/5.3/8/11.1/13/14, TESTING 5.3/13, README,
HANDOFF-M4, client README. The M4 phone gate and the fogbench measurement
now use the Full HD maximum; the 16384 px / 262144-cell bench and test rows
stay as labelled server stress references. import-scale now proves a 16384²
input is shrunk to 1080x1080 (289 tiles at 64 px) quickly and bounded.

Part of #63, #85. #77 is blocked by this issue.
This commit is contained in:
2026-11-21 22:37:34 +01:00
parent 52ab50d765
commit a79d809394
22 changed files with 515 additions and 193 deletions
+27 -18
View File
@@ -1,10 +1,13 @@
// #83: the map import at the real target scale — a 16384x16384 source at tile size 64, i.e. 65,536
// tiles. The one-shot `-crop ... +adjoin` this replaces died here with "Too many open files" after
// minutes at 4-8 GB RSS, so this gate is the regression proof: bounded memory, bounded file
// descriptors (it runs under a low `ulimit -n`), a streamed progress trail, and the full tile tree.
// #83/#85: the map import at the old 16384 px input scale, now under the Full HD cap: the source is
// shrunk to 1080x1080 before tiling (289 tiles at 64 px, not 65,536). The one-shot
// `-crop ... +adjoin` this replaces died here with "Too many open files" after minutes at 4-8 GB
// RSS, so this gate is the regression proof: the huge input is shrunk quickly, memory and file
// descriptors stay bounded (it runs under a low `ulimit -n`), the streamed progress trail appears,
// and the output tile tree follows the shrunk scene.
//
// It is deliberately NOT part of `devbox run test`: generating the source and writing 65,536 PNGs
// takes ~40 s on a 16-core box. `devbox run import-scale` runs it and leaves results/import-scale.log.
// It is deliberately NOT part of `devbox run test`: generating the 16384 px source takes seconds,
// and the shrink is real ImageMagick work. `devbox run import-scale` runs it and leaves
// results/import-scale.log.
import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
@@ -17,8 +20,9 @@ import { pngSize } from './tilefixture.mjs'
const enabled = process.env.IMPORT_SCALE === '1'
const SIDE = 16384
const OUT = 1080
const TILE = 64
const TILE_COUNT = (SIDE / TILE) ** 2
const TILE_COUNT = Math.ceil(OUT / TILE) ** 2
const CRC_TABLE = (() => {
const table = new Int32Array(256)
@@ -134,7 +138,7 @@ async function runMeasured(args, ulimitN) {
peakFds, peakProcs }
}
test('map import CLI: 16384 px source at 64 px stays memory- and fd-bounded (#83)',
test('map import CLI: a 16384 px source is shrunk to Full HD and stays memory- and fd-bounded (#83, #85)',
{ skip: enabled ? false : 'scale test: run `devbox run import-scale`' }, async () => {
const stateDir = makeStateDir()
const img = path.join(stateDir, 'map16384.png')
@@ -154,23 +158,28 @@ test('map import CLI: 16384 px source at 64 px stays memory- and fd-bounded (#83
`peakFds=${run.peakFds} peakProcs=${run.peakProcs} tiles=${tiles.length}`)
assert.equal(run.status, 0, run.stderr)
assert.equal(tiles.length, TILE_COUNT, 'every tile of the 256x256 grid exists')
assert.equal(tiles.length, TILE_COUNT, 'the shrunk 1080x1080 scene has all 289 tiles at 64 px')
assert.match(fs.readFileSync(`${dir}/0_0.png`).slice(0, 8).toString('latin1'), /^\x89PNG/,
'tiles are PNGs')
assert.deepEqual(pngSize(`${dir}/255_255.png`), { width: TILE, height: TILE })
assert.deepEqual(pngSize(`${dir}/255_0.png`), { width: TILE, height: TILE })
assert.deepEqual(pngSize(`${dir}/15_15.png`), { width: TILE, height: TILE })
assert.deepEqual(pngSize(`${dir}/15_0.png`), { width: TILE, height: TILE })
assert.deepEqual(pngSize(`${dir}/16_16.png`),
{ width: OUT - 16 * TILE, height: OUT - 16 * TILE }, 'the last edge tile is 56x56')
assert.equal(fs.existsSync(`${dir}/255_255.png`), false,
'the 16384 px grid is gone: the scene is the Full HD shrink')
// Progress must be visible while it runs, not only at the end.
assert.match(run.stderr, /slicing 16384x16384 into 65536 tiles at 64 px/)
assert.match(run.stderr, /rows 12\/256, tiles 3072\/65536/)
assert.match(run.stderr, /rows 256\/256, tiles 65536\/65536/)
assert.match(run.stdout, /16384x16384 px, tile 64 px, 256x256 tiles/)
// The shrink itself is reported, then the slice of the shrunk image.
assert.match(run.stdout, /scaled 16384×16384 → 1080×1080/)
assert.match(run.stdout, /1080x1080 px, tile 64 px, 17x17 tiles/)
assert.match(run.stderr, /slicing 1080x1080 into 289 tiles at 64 px/)
assert.match(run.stderr, /rows 17\/17, tiles 289\/289/)
// The numbers are the point of this gate: 4-8 GB and 1024 fds were the old failure.
// The numbers are the point of this gate: 4-8 GB and 1024 fds were the old failure, and a
// 16384 px input used to mean minutes and 65k tiles.
assert.ok(run.peakRssMiB < 1024, `peak RSS ${run.peakRssMiB.toFixed(0)} MiB must stay under 1 GiB`)
assert.ok(run.peakFds < 256, `peak fds ${run.peakFds} must stay far under the 256 ulimit`)
assert.ok(run.peakProcs <= 12, `only the importer, one decoder and <=8 children may run (saw ${run.peakProcs})`)
assert.ok(run.ms < 600_000, `a 16384/64 import must finish in minutes, took ${run.ms} ms`)
assert.ok(run.ms < 180_000, `the huge input must be shrunk and tiled in minutes, took ${run.ms} ms`)
assert.equal(fs.readdirSync(path.join(stateDir, 'tiles', '1'))
.some(n => n.startsWith('.import-')), false, 'no temporary directory is left behind')