fix: the debug log announced a healthy BLE link once a second

noteLinkState's guard was inverted. `state.linkQuiet` holds the INVERSE of
the link being up, so the steady state is `linkQuiet !== linkUp` and an edge
is `linkQuiet === linkUp`. The guard returned on the edge and fell through in
the steady state — the exact opposite of both.

Per-second monitorTick therefore wrote "companion link back" on every tick of
a perfectly healthy link, and swallowed the real transition: linkQuiet never
flipped off its `false` initial value, so "companion link down" could never be
reached at all. A field log from this morning shows 157 of 200 buffered lines
spent on the spam, the whole 200-line ring buffer covering 2m36s, and not one
"link down" line — defeating cc710a3, which added the pair so that a dropped
link would stop reading like an app that had crashed.

The edge decision moves to monitor.js as the pure `linkTransition`, next to
the other per-tick monitor helpers, since app.js cannot be imported under
node --test. Five regression tests cover both steady states, both edges, and a
full drop/return cycle driven tick by tick.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erwin FitenandClaude Opus 5 committed 2026-09-14 09:48:06 +02:00
1 parent cc710a3a11
commit ec55d31bfe
3 files changed
+62 -4

No files matched your search

+4 -3
View File
@@ -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');
}
+16
View File
@@ -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';
}
+42 -1
View File
@@ -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']);
});