fix(webconfig): tighten CLI failure detection, reboot deferral and refusals

Five findings from review, all confirmed against the source.

Failure classification (P2). Testing replies for an "Err" prefix passed five
other shapes off as success: "Unknown command", "unknown config: x", "??: x",
"Can't find GPS", "(ERR: clock cannot go backwards)" and "File system erase:
Err". They rendered green, and worse, left _batch_all_ok true — so a queued
reboot went ahead after commands that had failed, defeating the gate entirely.

Rather than lengthen one guess, the two questions are now asked separately,
each erring safe:

  - colour asks "does this look like a failure", against every shape CommonCLI
    actually emits, enumerated in WebConfigBatch.h and pinned by a host test
    that uses the literal strings. Getting this wrong is cosmetic.
  - the reboot gate asks something narrower and answerable: "did every setting
    I asked for take". Only `set`/`password` gate it, and only on the "OK"
    prefix every setter keeps. Diagnostics no longer gate a reboot at all, so a
    harmless `memory` cannot strand one and no guess is made about "> value".

Reboot deferral (P2). CommonCLI dispatches on a six-byte prefix, so `reboot
now` and `rebooted` reach Board::reboot() too. Matching exactly meant those
variants skipped both the confirmation and the deferral and took the node down
mid-drain — the precise failure deferral exists to prevent. Both sides now
anchor the way the firmware dispatches, and the UI's risk matcher with them.

Three commands the portal cannot honestly serve are refused at POST with a
reason, and dropped from autocomplete, instead of running and lying:

  - `start ota` builds a second AsyncWebServer on port 80 with no bind check
    and answers "Started" regardless; the portal already holds that port, so it
    could only leak the allocation and inhibit sleep.
  - `clock sync` takes its time from the caller's timestamp, which a web
    request has none of, so CommonCLI always rejected it. `time <epoch>` works
    and remains offered.
  - bare `log` and `get acl` write their real output to Serial and hand back a
    stub the terminal showed as success; `log` also streams a whole file from
    the loop task, stalling the mesh and radio while it does.

The mock now emits the same failure shapes it used to fake as successes, so
these are reproducible off-hardware. 24 batch + 14 keys tests pass; audit
reports 119/119 answered, 0 missing, 4/4 refused with a reason.
This commit is contained in:
agessaman committed 2026-08-08 08:40:49 -07:00
1 parent 0f4c87118a
commit 944caebed7
6 files changed
+284 -47

No files matched your search

+63
View File
@@ -1,5 +1,6 @@
#pragma once
#include <stddef.h> // size_t / NULL for the reply classifiers below
#include <stdint.h>
// Fork-owned, dependency-free spec for the WebConfig "config batch / reboot /
@@ -210,6 +211,68 @@ static inline bool cliRebootAllowed(bool has_reboot, bool all_ok) {
return has_reboot && all_ok;
}
// CommonCLI has no single failure convention. Testing only for an "Err" prefix
// let five other shapes through as success — including "Unknown command" and
// "unknown config: x", the two an operator hits most — which coloured them
// green AND let a queued reboot proceed after them.
//
// Every shape below is a literal from CommonCLI.cpp / CommonCLI_Observer.cpp.
// This list is the fragile part of the design: a new failure string added there
// is silently a success here. Which is exactly why it must not be what decides
// whether to reboot — see cliReplyConfirmsWrite.
static inline bool cliReplyIsFailure(const char* r) {
if (r == NULL || r[0] == 0) return false; // empty is normalised to "OK"
static const char* const kPrefixes[] = {
"Err", "ERR", "err", // "Err - ", "ERR: ", "Error: "
"(ERR", // "(ERR: clock cannot go backwards)"
"Unknown command",
"unknown config",
"??", // "??: <key>" from the get fallthrough
"Can't find", // "Can't find GPS"
};
for (size_t i = 0; i < sizeof(kPrefixes) / sizeof(kPrefixes[0]); i++) {
const char* p = kPrefixes[i];
size_t n = 0;
while (p[n]) n++;
bool match = true;
for (size_t j = 0; j < n; j++) {
if (r[j] != p[j]) { match = false; break; }
}
if (match) return true;
}
// "File system erase: Err" reports the failure at the END of the reply.
for (size_t i = 0; r[i]; i++) {
if (r[i] == ':' && r[i + 1] == ' ' && r[i + 2] == 'E' && r[i + 3] == 'r' &&
r[i + 4] == 'r') return true;
}
return false;
}
// Whether a command's reply is allowed to gate the deferred reboot.
//
// Only writes are, and only writes have a reply convention worth trusting:
// every setter answers with an "OK" prefix. Diagnostics do not — `memory`
// answers "Free: ...", a getter answers "> value" — so letting them gate would
// mean guessing, and guessing wrong here either strands the operator (a
// harmless `memory` blocks their reboot) or reboots into a config that did not
// apply. The question the gate exists to answer is narrower than "did anything
// fail": it is "did every setting I asked for actually take".
static inline bool cliReplyGatesReboot(const char* cmd) {
if (cmd == NULL) return false;
const char* set = "set ";
const char* pwd = "password ";
bool is_set = true, is_pwd = true;
for (int i = 0; i < 4; i++) if (cmd[i] != set[i]) { is_set = false; break; }
for (int i = 0; i < 9; i++) if (cmd[i] != pwd[i]) { is_pwd = false; break; }
return is_set || is_pwd;
}
// A write took effect iff its reply starts with "OK" — the one convention every
// setter in CommonCLI actually keeps.
static inline bool cliWriteSucceeded(const char* reply) {
return reply != NULL && reply[0] == 'O' && reply[1] == 'K';
}
// --------------------------------------------------------------------------
// Reboot fire (.cpp:262-265) and isRebootPending (.cpp:70-74).
// --------------------------------------------------------------------------