diff --git a/src/app.js b/src/app.js index 4d1c6df..a4622ba 100644 --- a/src/app.js +++ b/src/app.js @@ -19,7 +19,7 @@ import { createBeeper } from './beeper.js'; import { createLocalMap } from './localmap.js'; import { hexCellAt } from './hexgrid.js'; import { - discoverDecision, isOrganicHeard, snrToPct, decayPeak, pruneTimestamps, + discoverDecision, isOrganicHeard, snrToPct, decayPeak, pruneTimestamps, linkTransition, } from './monitor.js'; import { shareLog } from './sharelog.js'; import { Gps } from './gps.js'; @@ -599,9 +599,10 @@ function bleLinkUp() { // Without it a dropped link reads as a log that simply goes quiet — which is the same // thing as an app that has crashed, an area with no traffic, or a feature turned off. function noteLinkState(linkUp) { - if (linkUp === state.linkQuiet) return; // linkQuiet holds the INVERSE, so this is a change + const edge = linkTransition(state.linkQuiet, linkUp); + if (!edge) return; state.linkQuiet = !linkUp; - if (!linkUp) dbg('companion link down — discover, RF sampling and region asks held until it is back', 'no'); + if (edge === 'down') dbg('companion link down — discover, RF sampling and region asks held until it is back', 'no'); else dbg('companion link back — discover, RF sampling and region asks resumed', 'ok'); } diff --git a/src/monitor.js b/src/monitor.js index d64ae3d..ea7976f 100644 --- a/src/monitor.js +++ b/src/monitor.js @@ -71,3 +71,19 @@ export function pruneTimestamps(times, now, windowMs = 60000) { const cutoff = now - windowMs; return times.filter((t) => t >= cutoff); } + +// --- Companion link edges ----------------------------------------------------- +// linkTransition decides what the per-second tick should ANNOUNCE about the BLE link: +// 'down', 'back', or null while nothing changed. `prevQuiet` is the caller's stored +// flag, which holds the INVERSE of the link being up — so the steady state is +// `prevQuiet !== linkUp`, and an edge is exactly `prevQuiet === linkUp`. +// +// Getting that comparison the wrong way round is not a cosmetic bug: it announces on +// every steady tick and swallows the real edge. A field log (2026-09-14, 08:47 onward) +// shows "companion link back" once a second for three minutes, filling the 200-line +// debug buffer so that the heard/asks/regions lines rolled out of it — and no "link +// down" line anywhere, because the flag never flipped. +export function linkTransition(prevQuiet, linkUp) { + if (prevQuiet !== linkUp) return null; // steady: the flag already holds the inverse + return linkUp ? 'back' : 'down'; +} diff --git a/test/monitor.test.mjs b/test/monitor.test.mjs index 40ac801..971ac1b 100644 --- a/test/monitor.test.mjs +++ b/test/monitor.test.mjs @@ -5,7 +5,7 @@ import { test } from 'node:test'; import assert from 'node:assert'; import { discoverDecision, isOrganicHeard, snrToPct, decayPeak, pruneTimestamps, - DISCOVER_INTERVAL_MS, DISCOVER_BACKOFF_MS } from '../src/monitor.js'; + DISCOVER_INTERVAL_MS, DISCOVER_BACKOFF_MS, linkTransition } from '../src/monitor.js'; test('discover fires immediately when never fired and channel quiet', () => { const d = discoverDecision(1000, null, 0, false); @@ -106,3 +106,44 @@ test('the first tick after the link returns sweeps immediately', () => { test('linkUp defaults to true, so an existing caller keeps sweeping', () => { assert.equal(discoverDecision(1_000_000, null, 0, false).fire, true); }); + +// --- Link up/down edge announcements ------------------------------------------ +// Regression (field log 2026-09-14, 08:47–08:50): "companion link back" was logged on +// EVERY per-second tick while the link was simply up, flooding the 200-line debug ring +// buffer so that heard/asks/regions lines rolled out within minutes — while a real drop +// was never announced at all. + +test('a steady link announces nothing, tick after tick', () => { + let quiet = false; // link up at start + for (let i = 0; i < 200; i++) assert.equal(linkTransition(quiet, true), null); +}); + +test('a steady down link announces nothing either', () => { + for (let i = 0; i < 200; i++) assert.equal(linkTransition(true, false), null); +}); + +test('the drop is announced once, then stays quiet', () => { + let quiet = false; + assert.equal(linkTransition(quiet, false), 'down'); + quiet = true; // caller flips the flag on an edge + assert.equal(linkTransition(quiet, false), null); +}); + +test('the return is announced once, then stays quiet', () => { + let quiet = true; + assert.equal(linkTransition(quiet, true), 'back'); + quiet = false; + assert.equal(linkTransition(quiet, true), null); +}); + +test('a full drop/return cycle over per-second ticks announces exactly two edges', () => { + // Drives the real monitorTick loop: 10 s up, 5 s down, 10 s up again. + const ups = [...Array(10).fill(true), ...Array(5).fill(false), ...Array(10).fill(true)]; + let quiet = false; + const said = []; + for (const up of ups) { + const edge = linkTransition(quiet, up); + if (edge) { said.push(edge); quiet = !up; } + } + assert.deepEqual(said, ['down', 'back']); +});