Location
src/sim/spider.ts lines ~93 and ~109
Description
findHuntTarget encodes and decodes hunt tile keys using a hardcoded 7-bit shift:
const key = (ay << 7) + ax; // encode
bestX = k & 127; // decode ax
bestY = k >> 7; // decode ay
The inline comment acknowledges the assumption: // ay * SURFACE_GRID_WIDTH(128) + ax; 128 = 2^7.
tile-key.ts imports SURFACE_GRID_WIDTH from constants.ts and derives its stride constant from it:
import { SURFACE_GRID_WIDTH } from './constants.js';
const TILE_KEY_STRIDE = SURFACE_GRID_WIDTH;
spider.ts does not — it hardcodes 128 = 2^7 directly.
Impact
Currently benign: SURFACE_GRID_WIDTH = 128 and 128 = 2^7. If SURFACE_GRID_WIDTH is ever changed (e.g. a larger map config), spider.ts silently computes wrong tile coordinates from the packed key. The spider would evaluate ant density at incorrect tiles, misidentifying hunt targets. No compile-time error, no assertion failure, no test failure — silent wrong behaviour.
Fix
Replace the literal shift with a constant derived from SURFACE_GRID_WIDTH, or use the same TILE_KEY_STRIDE approach as tile-key.ts. Add a compile-time assertion (as tile-key.ts does for its own encoding) so future changes to SURFACE_GRID_WIDTH cause a build error here rather than a silent bug.
Severity
Medium — latent bug, currently safe, no behavioural change until map dimensions change.
Location
src/sim/spider.tslines ~93 and ~109Description
findHuntTargetencodes and decodes hunt tile keys using a hardcoded 7-bit shift:The inline comment acknowledges the assumption:
// ay * SURFACE_GRID_WIDTH(128) + ax; 128 = 2^7.tile-key.tsimportsSURFACE_GRID_WIDTHfromconstants.tsand derives its stride constant from it:spider.tsdoes not — it hardcodes 128 = 2^7 directly.Impact
Currently benign:
SURFACE_GRID_WIDTH = 128and128 = 2^7. IfSURFACE_GRID_WIDTHis ever changed (e.g. a larger map config),spider.tssilently computes wrong tile coordinates from the packed key. The spider would evaluate ant density at incorrect tiles, misidentifying hunt targets. No compile-time error, no assertion failure, no test failure — silent wrong behaviour.Fix
Replace the literal shift with a constant derived from
SURFACE_GRID_WIDTH, or use the sameTILE_KEY_STRIDEapproach astile-key.ts. Add a compile-time assertion (astile-key.tsdoes for its own encoding) so future changes toSURFACE_GRID_WIDTHcause a build error here rather than a silent bug.Severity
Medium — latent bug, currently safe, no behavioural change until map dimensions change.