fix(#533): state the flood constraint instead of a branch that never fired

The flood arm read as though the model were consulted:

    if (pathLength < 0) {
      // Flood: trust ML, only enforce firmware formula as floor
      if (mlTimeout < physicsMin) return physicsMin;
    }
    return mlTimeout.clamp(physicsMin, physicsMax);

It never did that. _physicsMinTimeout and _physicsMaxTimeout return the
identical expression for pathLength < 0, mirroring the firmware's
calcFloodTimeoutMillisFor, so a clamp between them cannot preserve a
prediction. Whichever way control went, flood returned 500 + 16 * airtime.
The comment described behaviour the code did not have, which is exactly
the kind of load-bearing false comment that re-causes a bug later.

Replaced with an explicit early return and the constraint written down.
Behaviour is unchanged.

Equivalence is tested, not asserted. A new group attaches a real
TimeoutPredictionService, trains it on 40-56 s delivery times so any
prediction is far from 1300, and checks flood is unmoved while a direct
path is still clamped to its ceiling, which proves the clamp is live
rather than the predictor being absent. That group was run against the
pre-change branch and passes there too, so this is a simplification and
not a behaviour change.

Deliberately NOT decided here: whether flood should ever trust a
prediction. Every round trip measured to date was 0-hop direct, so there
is no flood data to decide it from. The verification needed to answer it
is written up on the issue.

Part of epic #473, stacked on #528, #529, #531 and #532.
release/1.5.0-beta.1
Strycher 4 months ago
parent 99b5ea1251
commit 0f7a920220

@ -5751,12 +5751,22 @@ class MeshCoreConnector extends ChangeNotifier {
secondsSinceLastRx: secSinceRx,
);
if (mlTimeout != null) {
if (pathLength < 0) {
// Flood: trust ML, only enforce firmware formula as floor
if (mlTimeout < physicsMin) {
return physicsMin;
}
}
// Flood is a single firmware value, not a range: _physicsMinTimeout and
// _physicsMaxTimeout both return `500 + (16 * airtime)` for
// pathLength < 0, mirroring the firmware's calcFloodTimeoutMillisFor.
// A clamp between two equal bounds cannot preserve a prediction, so the
// model has never influenced a flood timeout in either direction.
//
// Previously written as an early return guarded by `mlTimeout <
// physicsMin`, above the comment "Flood: trust ML, only enforce firmware
// formula as floor". That described behaviour the code did not have, and
// read as though the model were consulted here. Stating the constraint
// is honest; the branch was not. Behaviour is unchanged (#533).
//
// Whether flood *should* trust a prediction is unanswered and is not
// decided here. Every round trip measured to date was 0-hop direct, so
// there is no flood data to decide it from.
if (pathLength < 0) return physicsMax;
return mlTimeout.clamp(physicsMin, physicsMax);
}

@ -16,6 +16,10 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/services/message_retry_service.dart';
import 'package:meshcore_open/services/path_history_service.dart';
import 'package:meshcore_open/services/storage_service.dart';
import 'package:meshcore_open/services/timeout_prediction_service.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -116,4 +120,109 @@ void main() {
);
});
});
group('flood is a single firmware value, not a range (#533)', () {
// The flood branch used to be written as "trust ML, only enforce the
// firmware formula as floor". It never did that: _physicsMinTimeout and
// _physicsMaxTimeout return the identical expression for pathLength < 0,
// so a clamp between them discards any prediction. The branch was removed
// and the constraint stated instead, with behaviour unchanged.
//
// These tests pin the equality the simplification depends on. If a future
// change makes the flood floor and ceiling differ, the removed branch would
// no longer have been equivalent, and this fails loudly rather than
// silently altering flood timeouts.
// firmware calcFloodTimeoutMillisFor: SEND_TIMEOUT_BASE_MILLIS 500 +
// FLOOD_SEND_TIMEOUT_FACTOR 16 * airtime, with the 50 ms fallback airtime.
const floodFirmwareValue = 500 + (16 * 50); // 1300
test('flood returns the firmware formula exactly', () {
expect(connector.calculateTimeout(pathLength: -1), floodFirmwareValue);
});
test('flood does not scale with hop count the way direct does', () {
// Direct grows per hop; flood has no hop term at all.
expect(
connector.calculateTimeout(pathLength: -1),
isNot(connector.calculateTimeout(pathLength: 0)),
);
expect(
connector.calculateTimeout(pathLength: 1),
greaterThan(connector.calculateTimeout(pathLength: 0)),
);
});
test('the CLI budget still covers flood through both of its legs', () {
// calculateCliTimeout takes the flood branch in the outbound term and in
// the reply term, so no flood-specific handling is needed there.
expect(
connector.calculateCliTimeout(pathLength: -1),
floodFirmwareValue +
cliReplyDelayMs +
floodFirmwareValue +
connector.messageRetrievalBudgetMs,
);
});
});
group('flood ignores the model even when one exists (#533)', () {
// The group above only exercises the no-model path. The actual claim is
// about what happens when predictTimeout returns a value: for flood it can
// never survive, because the floor and the ceiling are the same number.
//
// This attaches a real predictor, trains it on deliberately huge delivery
// times so any prediction is far from the firmware constant, and asserts
// flood is unmoved. Verified to pass against the pre-#533 code as well,
// which is what makes the branch removal a simplification rather than a
// behaviour change.
late TimeoutPredictionService prediction;
setUp(() {
prediction = TimeoutPredictionService.noStorage();
connector.initialize(
retryService: MessageRetryService(),
pathHistoryService: PathHistoryService(StorageService()),
timeoutPredictionService: prediction,
);
// minObservations is 10; vary the features so training does not discard
// them all for zero variance, and make deliveryMs enormous so any
// prediction lands nowhere near 1300.
for (var i = 0; i < 12; i++) {
prediction.recordObservation(
contactKey: 'contact$i',
pathLength: i.isEven ? -1 : (i % 4),
messageBytes: 40 + (i * 12),
tripTimeMs: 40000 + (i * 1500),
);
}
});
test('the predictor is actually trained, so the test is not vacuous', () {
expect(prediction.hasModel, isTrue);
final predicted = prediction.predictTimeout(
pathLength: -1,
messageBytes: 172,
);
expect(predicted, isNotNull);
expect(
predicted,
greaterThan(1300),
reason:
'the prediction must differ from the flood constant, or this '
'group proves nothing',
);
});
test('flood still returns the firmware constant', () {
expect(connector.calculateTimeout(pathLength: -1), 500 + (16 * 50));
});
test('direct is still clamped to its ceiling, so the clamp is live', () {
// Contrast: on a direct path the prediction IS consulted and then
// clamped. If this returned the raw prediction the clamp would be broken.
expect(connector.calculateTimeout(pathLength: 0), 1050);
});
});
}

Loading…
Cancel
Save

Powered by TurnKey Linux.