address review findings on voice outlines

- remove the unused Nominatim reserve(); spacing tests now measure the
  actual transport start across processes
- start each queued building's 2 s ground-sampling budget when it
  reaches the head of the queue, and request a frame on promotion
- accept Nominatim and grounds outlines that cross the antimeridian
- let an unnamed school, university, hospital, cemetery, stadium or zoo
  area answer a grounds ask whose head noun names that kind, using only
  the part that holds the point; compare grounds names symmetrically
- charge shared OpenFreeMap consumers their own slice and keep the
  charged budget at the previous 64 MiB
- share one place-name fold, point-in-polygon, line stitching and
  nextTask helper instead of duplicates
- exclude Who's On First sources licensed only as "CC BY (equivalent)"
  or "CC0 (equivalent)"; rebuild the pack from the pinned archives
- whole-word neighbourhood alias guard; correct the outline size-limit
  comment
- restore DATA_SOURCES.md table formatting, keeping only real edits
- test behaviour instead of private engine fields
This commit is contained in:
manjunath committed 2026-10-30 20:47:20 +05:30
1 parent 5a1cfa6800
commit 2e82425d62
37 files changed
+921 -481

No files matched your search

+19 -6
View File
@@ -774,7 +774,8 @@ test('replacement cleanup failure still removes the old route and retains failed
const old = engine.list()[0];
const refresh = engine.annotate([{ type: 'area', target: 'Capitol', label: 'New' }]);
await flushMicrotasks();
const replacement = h.pending.at(-1).anno;
const replacementTask = h.pending.at(-1);
const replacement = replacementTask.anno;
const originalRemove = h.renderer.remove;
const removals = [];
let fail = true;
@@ -785,16 +786,22 @@ test('replacement cleanup failure still removes the old route and retains failed
};
assert.throws(() => engine.clear(), /replacement cleanup failed/);
assert.ok(removals.includes(old), 'old removal is attempted even when replacement removal throws');
assert.equal(old._pendingReplacement, null, 'cleanup failure cannot retain install authority');
assert.ok(old._retiredRoutes.has(replacement));
assert.equal(replacementTask.options.isCurrent(), false, 'cleanup failure cannot retain install authority');
assert.equal(engine.list()[0], old, 'failed cleanup remains reachable');
await flushMicrotasks();
fail = false;
const attempts = removals.filter((anno) => anno === replacement).length;
engine.clear();
assert.ok(
removals.filter((anno) => anno === replacement).length > attempts,
'the failed replacement route is retried on the next clear',
);
await refresh;
assert.equal(engine.count(), 0);
assert.equal(h.live.size, 0);
assert.equal(old._pendingReplacement, null);
replacementTask.finish();
await flushMicrotasks();
assert.equal(h.live.size, 0, 'a revoked install cannot commit after cleanup');
});
@@ -840,8 +847,10 @@ test('successful replacement survives failed old-route retirement and keeps clea
await flushMicrotasks(); h.pending[0].finish(); await initial;
const old = engine.list()[0];
const remove = h.renderer.remove;
const removals = [];
let fail = true;
h.renderer.remove = (anno) => {
removals.push(anno);
if (anno === old && fail) throw new Error('retirement failed');
remove(anno);
};
@@ -853,10 +862,14 @@ test('successful replacement survives failed old-route retirement and keeps clea
assert.equal(completed.results[0].cleanupPending, true);
assert.equal(current.label, 'New');
assert.ok(h.live.has(current.id), 'valid installed replacement is not rolled back by old teardown failure');
assert.equal(current._pendingReplacement, undefined);
assert.ok(current._retiredRoutes.has(old));
assert.equal(h.pending.filter((task) => task.options.isCurrent()).length, 0, 'no install is left pending');
fail = false;
const attempts = removals.filter((anno) => anno === old).length;
engine.clear();
assert.ok(
removals.filter((anno) => anno === old).length > attempts,
'the old route that failed to retire is retried on clear',
);
assert.equal(engine.count(), 0);
assert.equal(h.live.size, 0);
});
@@ -225,7 +225,10 @@ function fakeViewer(lon, lat) {
clampToHeightSupported: true,
clampToHeight: (world) => world,
postRender: frames.event,
requestRender() {},
renderRequests: 0,
requestRender() {
scene.renderRequests += 1;
},
};
return {
dataSources,
@@ -716,6 +719,32 @@ test('buildings share one probe per frame and preserve probe geometry, filtering
assert.equal(viewer.credits.size, 0);
});
test('a building queued behind others gets its full budget on a slow device', async (t) => {
installBrowserGlobals(t);
t.mock.timers.enable({ apis: ['setTimeout'] });
t.after(() => t.mock.timers.reset());
const { viewer, frames } = fakeViewer(-99, 31);
const renderer = createHybridAnnotationRenderer(viewer);
viewer.scene.clampToHeight = (position) => position;
const traces = [workTrace(), workTrace(), workTrace()];
const completions = [0, 0.01, 0.02].map((offset, index) =>
renderer.add(buildingAnnotation(`queued-${index}`, offset), {
pendingVisible: false,
workTrace: traces[index],
}));
// Eight probes per building at one probe per frame; 1.5 s passes while
// each of the first two buildings samples, so the third waits 3 s in line.
for (let building = 0; building < 3; building += 1) {
for (let frame = 0; frame < 8; frame += 1) frames.emit();
if (building < 2) t.mock.timers.tick(1500);
}
const results = await Promise.all(completions);
assert.deepEqual(results.map((result) => result.status), ['installed', 'installed', 'installed']);
assert.deepEqual(traces.map((trace) => trace.samplerStatus), ['installed', 'installed', 'installed']);
assert.ok(traces[2].samplerHeadAt >= traces[1].samplerHeadAt);
assert.ok(traces[2].samplerHeadAt >= traces[2].samplerStart);
});
test('building cancellation, queued deadline and destroy prevent saved callbacks from committing', async (t) => {
installBrowserGlobals(t);
t.mock.timers.enable({ apis: ['setTimeout'] });
@@ -745,10 +774,16 @@ test('building cancellation, queued deadline and destroy prevent saved callbacks
frames.emit();
assert.equal(probeCount, 1);
const savedTimeout = frames.snapshot();
// No further renderer frames arrive: both enqueue deadlines must still fire,
// including the tail task that has never had a chance to sample.
// No further renderer frames arrive: the head's deadline fires, then the
// tail gets its own budget from the moment it reaches the head and still
// times out without frames, even though it never had a chance to sample.
const rendersBefore = viewer.scene.renderRequests;
t.mock.timers.tick(2000);
assert.equal((await headCompletion).status, 'timeout');
assert.ok(viewer.scene.renderRequests > rendersBefore, 'a promoted tail asks for a frame');
assert.equal(tailTrace.samplerStatus, undefined, 'queue wait does not spend the tail budget');
assert.ok(Number.isFinite(tailTrace.samplerHeadAt), 'the tail starts its own budget at the head');
t.mock.timers.tick(2000);
const tailResult = await tailCompletion;
assert.equal(tailResult.status, 'timeout');
assert.equal(tailResult.timing.probes, 0);
+47 -3
View File
@@ -13,6 +13,7 @@ import {
buildingFromTiles,
createOpenFreeMapOutlineSource,
enclosingAreaFromTiles,
groundsClassesFor,
streetFromTiles,
} from '../sources/openFreeMapOutlines.js';
import { createNominatimOutlineClient } from '../sources/nominatimOutlines.js';
@@ -80,6 +81,46 @@ export function nominatimKindFor({ scope, groundsLike, pointLike, around }) {
return null;
}
/**
* Whether polygons contain a point. A part whose outer ring crosses the
* antimeridian ([179.99, …], [-179.99, …]) is read contiguously in its own
* longitude frame, with the point moved into that frame; every other part,
* including one genuinely wider than 180°, is tested as stored. Crossing is
* read from an edge jumping more than 180°, which assumes densely noded rings
* (OSM, Nominatim, tiles); a ring that is both wider than 180° and crossing, or
* a non-crossing ring with a single edge over 180°, is not supported.
*/
export function containsAnchor(polygons, lat, lon) {
if (!Number.isFinite(lon)) return false;
for (const part of polygons || []) {
const outer = part?.[0];
if (!Array.isArray(outer) || !outer.length) continue;
const reference = outer[0][0];
const shift = (x) => x - 360 * Math.round((x - reference) / 360);
const span = (ring, f) => {
let west = Infinity;
let east = -Infinity;
for (const [x] of ring) {
west = Math.min(west, f(x));
east = Math.max(east, f(x));
}
return east - west;
};
// An edge jumping more than 180° wraps the line; a polar ring whose
// edges run along ±180 collapses when reframed and is read as stored.
const jumps = outer.some(
([x], i) => i > 0 && Math.abs(x - outer[i - 1][0]) > 180,
);
const framedSpan = jumps ? span(outer, shift) : 0;
const crosses = framedSpan > 0 && framedSpan < 180;
const framed = crosses
? part.map((ring) => ring.map(([x, y]) => [shift(x), y]))
: part;
if (polygonsContain([framed], lat, crosses ? shift(lon) : lon)) return true;
}
return false;
}
/**
* Parts with the one holding the anchor outside its holes first. A boundary
* can be largest at sea (Tokyo's island municipalities), so size alone does
@@ -90,7 +131,7 @@ export function anchorPartFirst(polygons, points) {
for (const { lat, lon } of points) {
if (!Number.isFinite(lat) || !Number.isFinite(lon)) continue;
const at = polygons.findIndex((polygon) =>
polygonsContain([polygon], lat, lon),
containsAnchor([polygon], lat, lon),
);
if (at > 0) return [polygons[at], ...polygons.filter((_, i) => i !== at)];
if (at === 0) return polygons;
@@ -188,9 +229,12 @@ export function createOutlineRungs({
lat: at.lat,
lon: at.lon,
name: outlineQueryText(ctx.target || ctx.matchName),
// The kind the ask names ("… School grounds") lets unnamed tile
// landuse of that class answer; read before grounds words are cut.
unnamedClasses: groundsClassesFor(ctx.target || ctx.matchName),
minAreaM2: GROUNDS_MIN_AREA_M2,
});
if (!area || !polygonsContain(area.polygons, ctx.lat, ctx.lon)) return null;
if (!area || !containsAnchor(area.polygons, ctx.lat, ctx.lon)) return null;
return {
ring: area.ring,
polygons: area.polygons,
@@ -215,7 +259,7 @@ export function createOutlineRungs({
if (!outline?.polygons?.length) return outline; // null | undefined | unavailable | rateLimited
// Named outlines must retain the resolved place's authority,
// including holes; a nearby same-name polygon is not the selected place.
if (!polygonsContain(outline.polygons, ctx.lat, ctx.lon)) return null;
if (!containsAnchor(outline.polygons, ctx.lat, ctx.lon)) return null;
const polygons = anchorPartFirst(outline.polygons, [
{ lat: ctx.lat, lon: ctx.lon },
outline.center || {},
+100
View File
@@ -11,6 +11,7 @@ import os from 'node:os';
import path from 'node:path';
import { Readable } from 'node:stream';
import {
containsAnchor,
createOutlineRungs,
isNamedAsk,
nominatimKindFor,
@@ -324,6 +325,71 @@ test('a city with no configured Overpass is outlined by Nominatim', async () =>
assert.deepEqual(credits, ['osm']);
});
test('a Nominatim outline across the antimeridian still holds its anchor', async () => {
const ring = [
[179.9, -16.9],
[-179.9, -16.9],
[-179.9, -16.7],
[179.9, -16.7],
[179.9, -16.9],
];
const hole = [
[179.99, -16.81],
[-179.99, -16.81],
[-179.99, -16.79],
[179.99, -16.79],
[179.99, -16.81],
];
assert.ok(containsAnchor([[ring]], -16.8, 179.95));
assert.ok(
containsAnchor([[ring]], -16.8, -179.95),
'either side of the line',
);
assert.ok(!containsAnchor([[ring, hole]], -16.8, 180), 'holes still exclude');
assert.ok(!containsAnchor([[ring]], -16.8, 179.5), 'outside stays outside');
assert.ok(
!containsAnchor([[ring]], -16.8, 0),
'far side of the globe is outside',
);
const steps = [-170, -130, -90, -50, -10, 20];
const wide = [
...steps.map((x) => [x, 10]),
...steps.toReversed().map((x) => [x, 20]),
[-170, 10],
];
assert.ok(
containsAnchor([[wide]], 15, 5),
'a ring wider than 180° is read as stored',
);
const polar = [
[-180, -60],
[180, -60],
[180, -90],
[-180, -90],
[-180, -60],
];
assert.ok(
containsAnchor([[polar]], -80, 30),
'a polar ring spanning every longitude',
);
const nominatim = fakeNominatim({ polygons: [[ring]], class: 'boundary' });
const result = await createOutlineRungs({
tiles: fixtureTiles(),
nominatim,
}).resolve(
ctx({
scope: 'city',
target: 'Dateline Town',
matchName: 'Dateline Town',
lat: -16.8,
lon: 179.95,
}),
ladder(UNAVAILABLE),
);
assert.deepEqual(result.ring, ring, 'the outline is accepted, not refused');
assert.equal(result.outlineCredit, 'osm');
});
test('an empty Nominatim polygon list falls back without throwing', async () => {
const nominatim = fakeNominatim({ polygons: [], class: 'boundary' });
const result = await createOutlineRungs({
@@ -399,6 +465,40 @@ test('named outlines must contain the resolved anchor outside holes', async () =
);
});
test('an unnamed school landuse answers a school grounds ask from tiles', async () => {
const schoolRing = square(CAPITOL.lat, CAPITOL.lon, 0.002);
const tiles = {
fetchBounds: async () => ({
tiles: [
{
areas: [
{
name: null,
rings: [schoolRing],
class: 'school',
clipped: false,
},
],
},
],
}),
};
const ask = (target) =>
createOutlineRungs({ tiles, nominatim: fakeNominatim(null) }).resolve(
ctx({ scope: 'building', target, groundsLike: true }),
ladder(UNAVAILABLE),
);
const school = await ask('Lincoln High School grounds');
assert.equal(school.footprintKind, 'area');
assert.deepEqual(school.polygons, [[schoolRing]]);
assert.equal(school.outlineCredit, 'tiles');
assert.equal(
await ask('Texas Capitol grounds'),
UNAVAILABLE,
'other asks still need a name',
);
});
test('a multi-part city leads with the part holding the anchor, not the largest', async () => {
const sea = square(33.5, 139.5, 0.8); // larger, far from the anchor
const outer = square(CAPITOL.lat, CAPITOL.lon, 0.4);
+3 -2
View File
@@ -1040,11 +1040,12 @@ export function createAnnotationResolver({
// Outside a district ask, an alternate name unrelated to the name is too
// weak: Se in Sao Paulo lists "Sao Paulo" among its names, and "outline
// Sao Paulo" means the city ("Richmond" for Richmond District still counts).
// Whole words: an alias hit on "York" does not keep "Yorkville".
if (
!district &&
hood?.byAlias &&
!normalizePlaceName(hood.name).includes(
normalizePlaceName(stripQualifiers(query)),
!` ${normalizePlaceName(hood.name)} `.includes(
` ${normalizePlaceName(stripQualifiers(query))} `,
)
)
hood = null;
+27 -6
View File
@@ -60,9 +60,12 @@ export function createWorldAnnotationRenderer(viewer) {
function sampleBuilding(anno, options) {
pendingGround.get(anno)?.finish('cancelled');
return new Promise((resolve, reject) => {
const started = performance.now();
const queuedAt = performance.now();
// The 2 s budget starts when this task reaches the head of the queue, so
// waiting behind other buildings on a slow device never spends it.
let started = null;
const workTrace = options.workTrace;
if (workTrace) workTrace.samplerStart = started;
if (workTrace) workTrace.samplerStart = queuedAt;
let groundDetailSlots = 0;
let groundDetailReady = true;
if (workTrace) {
@@ -83,24 +86,42 @@ export function createWorldAnnotationRenderer(viewer) {
maxSliceMs: 0,
lastSliceMs: 0,
probes: 0,
deadlineMs: started + 2000,
queuedMs: 0,
deadlineMs: null,
};
task.current = () =>
!destroyed &&
!options.signal?.aborted &&
pendingGround.get(anno) === task &&
(options.isCurrent?.() ?? true);
task.begin = () => {
if (started !== null) return;
started = performance.now();
timing.queuedMs = started - queuedAt;
timing.deadlineMs = started + 2000;
if (workTrace) workTrace.samplerHeadAt = started;
task.timer = setTimeout(() => task.finish('timeout'), 2000);
// A task promoted outside a frame (its predecessor timed out or was
// cancelled) still needs frames in request-render mode.
try {
viewer.scene.requestRender?.();
} catch {
/* the enqueue path reports render failures */
}
};
task.finish = (status, error) => {
if (pendingGround.get(anno) !== task) return;
pendingGround.delete(anno);
groundQueue.splice(groundQueue.indexOf(task), 1);
const position = groundQueue.indexOf(task);
groundQueue.splice(position, 1);
clearTimeout(task.timer);
if (position === 0) groundQueue[0]?.begin();
options.signal?.removeEventListener('abort', task.abort);
if (!groundQueue.length && observingGround) {
viewer.scene.postRender.removeEventListener(onGroundFrame);
observingGround = false;
}
timing.elapsedMs = performance.now() - started;
timing.elapsedMs = performance.now() - (started ?? queuedAt);
if (workTrace) {
workTrace.probes = timing.probes;
workTrace.samplerStatus = status;
@@ -230,7 +251,7 @@ export function createWorldAnnotationRenderer(viewer) {
};
pendingGround.set(anno, task);
groundQueue.push(task);
task.timer = setTimeout(() => task.finish('timeout'), 2000);
if (groundQueue[0] === task) task.begin();
options.signal?.addEventListener('abort', task.abort, { once: true });
if (!task.current()) return task.finish('cancelled');
if (!observingGround) {