Skip to content

mesh m3c: edge-router attach protocol + self-healing failover - #23

Merged
aphelps-claude-bot merged 1 commit into
mainfrom
m3c-attach-failover
Jul 30, 2026
Merged

aphelps-claude-bot merged 1 commit into
mainfrom
m3c-attach-failover

Conversation

@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator

Summary

Mesh M3c — the edge↔router attach protocol and self-healing failover. Advances both submodule pointers; there is no super-repo source change.

Relaying alone cannot notice that a router went away, so the mesh had no way to re-form around a failure. Routers now advertise how far they are from the elected timebase leader, nodes bind to the router they rank best, and heartbeat timeouts drop dead routes so the tree re-roots on its own.

The deferred RSSI decision landed on (b), the RSSI-free heuristic. WLED's onEspNowMessage(sender, payload, len) hook does not forward per-frame RSSI (espNowReceiveCB drops it before the hook), so ranking by signal strength would mean patching WLED core and re-applying that divergence on every upstream bump. Instead the router advertises a hop/cost metric and every node selects on it — hop count to the leader, then load, then deviceId — which needs no core change and is fully host-testable.

Failover rests on three independent timers (route drop on heartbeat timeout, leader re-election, membership lease expiry). Relaying continues throughout, so what a node loses during the churn is state rather than delivery, and the edge's keyframeMs re-sync covers that gap.

Submodule PRs

Test plan

Automated:

  • make test — super-repo host suites across both submodules
== WLED ampworks host tests ==
-- sensor_sync_test --      ALL TESTS PASSED
-- sensor_sync_ring_test -- ALL TESTS PASSED
== esp-now-router host tests ==
== test_relay ==  == test_election ==  == test_attach ==   ALL TESTS PASSED
OK — all submodule host tests passed.
  • cd esp-now-router && pio test -e native
Collected 3 tests
native:test_election [PASSED]   native:test_attach [PASSED]   native:test_relay [PASSED]
  • cd esp-now-router && pio run -e wt32-eth01
RAM: 12.8% (41888 / 327680)   Flash: 44.2% (579157 / 1310720)   [SUCCESS]
  • cd WLED && pio run -e ampworks
RAM: 25.3% (82896 / 327680)   Flash: 83.3% (1310205 / 1572864)   [SUCCESS]

Manual:

  • Node-drop failover — kill a router mid-stream; the mesh re-elects/re-routes and sensor delivery resumes within keyframeMs. Measure recovery time.
  • Coexistence — an edge with infra WiFi (STA) up serves its web UI and participates on the fixed ESP-NOW channel without dropping the WiFi association.

Note on ordering

PR #22 (m3b-submodule-pointers) also advances these pointers. This branch is based on origin/main, so whichever lands second just re-advances them; the gitlink conflict resolves in favour of this branch's newer tips.

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was done. Routers advertise a hop/cost metric; nodes bind to the best (hops, load, deviceId) with a hold-down, and hold it with attach keepalives against a membership lease. Failover = route drop on heartbeat timeout + leader re-election + lease expiry; relaying never stops, so keyframeMs covers the state gap. Deferred RSSI question resolved as (b), the RSSI-free heuristic — no WLED core patch, so no fork divergence to re-apply on upstream bumps. Pure logic in esp-now-router/src/router_attach.h, host-tested; shared header carries only the wire format. Hardware failover + coexistence tests remain (## Testing Required).

What's needed from you. Review the PR — code, tests, and the disposition of any self-review findings.

Links.
plan: todo_plans/mesh-m3c-edge-router-attach-protocol-self-healing-failover.md
PR: #23

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review (bot) of the M3c change set — anchor summary. This PR's own diff is the two gitlink bumps (WLED → 56d4c56, esp-now-router → eaec2df), both pointing at the submodule PR heads as of this review, so the substantive findings are posted where the code lives:

What was verified and holds:

  • State machine — all liveness / hold-down / lease comparisons are wrap-safe unsigned interval arithmetic with now injected (no millis() wrap hazard, no absolute-time comparisons); selection is a strict total order (hopCost, memberCount, deviceId) so nodes converge without negotiating; the incumbent's metric is refreshed in place (best-not-latest); a just-rebooted router's empty-table load advantage is only a tiebreak and is gated by the hold-down; every send() result is checked and logged, with periodic timers as the retry path.
  • Wire-format discipline — the shared header gains only msgType numbers, SS_HOP_UNREACHABLE, and the packed payload structs both sides parse; no router logic leaked in; msgType 2/3/4 collide with nothing and edges reject all non-snapshot types via ss_parse_header (confirmed at head). ss_router_is_relayable correctly narrows the relay rule so the new control frames cannot flood.
  • Tests — the failure paths are real, not happy-path: timeout at exactly the boundary vs boundary+1, lease-edge expiry, hold-down suppression plus shorter-path preemption, unreachable-upstream drop, both loop guards, stale/foreign acks, and the re-election deviceId tiebreak.

Findings by severity (details inline on the submodule PRs):

  • Medium — (1) RouterAdvert.leaderTerm is documented as a "stale-advert guard" but never consulted by selection, so post-failover stale adverts rank equal to fresh ones; (2) the loop guards only cover 2-cycles — a >=3-node stale-advert cycle after a mid-tier router death goes undetected and heals only via cost saturation (~250 s) or an external shorter-path advert, a bound that should be documented or poisoned away; (3) the edge side of the attach protocol is not implemented in WLED (edges reject msgType 2–4), so the "edge announces itself" subtask is exercised router-to-router only — the docs should say so explicitly.
  • Minor — AttachAck.leaseMs is transmitted but ignored by the receiver; the routerId == 0 any-router wildcard is dead code that would multi-admit if ever used; BACKBONE_ROUTER.md's "failover window is dimensioned below keyframeMs" claim is numerically false (route timeout 3.5 s > keyframe 3 s) though harmless since delivery is flood-based.
  • Suggested test — wrap-around time base (near UINT32_MAX) for the failover/hold-down/lease sequences, and the >=3-node cycle scenario.

None of these block the merge. Merge-order note from the task stands: land #13 and #2 first, then re-point this PR at the merged commits before merging it, and close superseded #22 once this lands.

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review (bot) of 4897fbf..775c95f — anchor summary. This PR's own diff is the two gitlink bumps only, so as before the substantive findings live on the submodule PRs:

  • Gitlinks verified: WLED → 627ea13 and esp-now-router → 64dc581 are exactly the re-reviewed submodule heads, both on m3c-attach-failover and descendants of the previously reviewed commits (56d4c56, eaec2df). All host suites pass locally at these pins (make -C esp-now-router/tests test).
  • Prior findings — all addressed by the bumped commits:
    • Medium (1) leaderTerm unconsulted → route selection now gates on it via wrap-safe ss_seq_newer; verified the gate cannot strand a node (unattached adoption is term-blind, and a node holding an orphaned newer term self-heals via the 3.5 s route timeout or the incumbent's own term update). Pinned by new term-gate + 0xFFFF-wrap tests.
    • Medium (2) >=3-node cycle undetected → addressed as a documented bound (comment now states 2-cycle scope; longer cycles bounded by hop-cost saturation) plus a pinning test.
    • Medium (3) edge side unimplemented but undocumented → SENSOR_SYNC.md now states this repo reserves the wire format only; claim verified against ss_parse_header at head.
    • Minor: leaseMs ignored → keepalive cadence now derives from the quoted lease, floored by ROUTER_ATTACH_MIN_MS; verified the derived interval ≤ lease/3 for any lease ≥ 750 ms (a residual sub-750 ms edge is a new minor on #2). Minor: routerId == 0 wildcard removed. Minor: BACKBONE_ROUTER.md keyframe claim corrected.
    • Suggested tests (uint32 time wrap, 3-node cycle) → both added, plus the re-election deviceId tiebreak.
  • New findings, none blocking (inline on the submodule re-reviews):
    • esp-now-router#2 (aphelps/esp-now-router#2 (review)): 2 minor (keepalive floor can exceed a sub-250 ms quoted lease; stale routerLeaseMs survives route drop/switch until the next ack) + 1 suggested test (the 3-node-cycle test's middle loop restates ra_advert_cost saturation on one struct instead of simulating the three-node advert exchange).
    • WLED#13 (aphelps/WLED#13 (review)): 1 minor (the shared header's NodeAttach.routerId comment still documents the removed 0 = any router wildcard — now-contradicted wire semantics; suggestion attached).

Merge-order note from the task stands: land #13 and #2 first, re-point this PR at the merged commits before merging it, and close superseded #22 once this lands.

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review (bot) of 775c95f..f17b9aa — anchor summary. This PR's own diff is the two gitlink bumps only; the substantive verification lives on the submodule re-reviews.

  • Gitlinks verified: WLED → c88a3c46 and esp-now-router → 08bd6e2 are exactly the current submodule PR heads (both are the m3c-attach-failover branch tips, single-commit descendants of the previously reviewed 627ea13 / 64dc581). All router host suites pass locally at these pins (make -C esp-now-router/tests test).
  • Prior residual findings — all addressed by the bumped commits (verified in detail on the submodule re-reviews):
    • esp-now-router#2 (aphelps/esp-now-router#2 (review)): a sub-750 ms lease is now rejected at ack time, so the keepalive interval is provably ≤ lease/3 for every accepted lease and rejection cannot strand a node (cost is logged membership flap only); attachIntervalMs() now gates on route.attached, and every route-change/drop path was verified to clear that flag, so a lease only ever applies to the router that quoted it; the 3-node-cycle test now genuinely simulates the three-node advert exchange (collapses at lap 169 of the 512 bound; the assertion fails if the unreachable-drop rule regresses). One non-blocking observational note inline there.
    • WLED#13 (aphelps/WLED#13 (review)): the NodeAttach.routerId wire comment now matches the no-wildcard router behaviour on both send and receive paths; clean.
  • No new findings on this PR itself.

Merge-order note from the task stands: land #13 and #2 first, re-point this PR at the merged commits before merging it, and close superseded #22 once this lands.

@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator Author

Now conflicting — the WLED gitlink only, and here is the resolution order

Flagging this rather than resolving it, because the fix depends on a merge decision that belongs to this workstream rather than to the RS485 one.

Four RS485 submodule-pointer bumps landed on main today (#20, #24, #25, #26), which moved the WLED gitlink out from under this branch:

WLED
merge base 467c222
main now cdfef9c0 — receive-path fixes (#25)
this branch c88a3c4

That gitlink is the entire conflict. Nothing textual clashes: aphelps/WLED#13 still merges onto the updated base-wled-16.0.1 with zero conflicts, verified locally with git merge-tree. The RS485 changes (packed socket header, receive-path fixes) and the attach-protocol work touch different files.

Resolution order:

  1. merge reserve the attach-protocol wire format for the sensor-sync bus WLED#13 into base-wled-16.0.1 — it now contains the RS485 work, and Move todo-handler skill to user-level claude-skills repo #13 applies cleanly on top
  2. re-point this branch's WLED gitlink at the resulting merge sha
  3. esp-now-router is unaffected — both sides agree at 8090318

Step 1 is why I have not just resolved this: merging #13 is a call for whoever owns the mesh work, not something to do as a side effect of unblocking a pointer refresh.

For reference, the RS485 side is finished and merged: socket-header packing (7 B on every target — previously 8 on ESP32 vs 7 on AVR, which misparsed every frame in both directions), four receive-path defects, and the bench tooling. make test now covers four suites where it covered two.

@aphelps aphelps left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge this. I can't do the testing currently so please add a task to walk me through comprehensive testing.

@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator Author

Refreshed against current main — now conflict-free

main is now an ancestor of this branch, so there is no conflict left to resolve. The diff against main is exactly three lines: the two gitlinks and one doc fix.

What changed

The WLED gitlink was the only conflict. This branch pointed at c88a3c46, the tip of the attach-protocol PR branch; that PR squash-merged as 586c6136, and meanwhile four RS485 pointer bumps landed on main. Resolved to 586c6136, which contains both workstreams:

esp-now-router keeps this branch's 08bd6e2: main still records the router's initial commit, and 08bd6e2 is strictly newer (it contains cfa6554, which is what the now-closed #22 was advancing to).

Also corrected CLAUDE.md's submodule table, which still told contributors to branch from base-wled-16.0.1 — renamed to main today, so the instruction pointed at a branch that no longer exists.

Verification at the resolved pointers

  • make test — all suites green, and three of them now actually run. The esp-now-router tests were reporting skipped: no tests/ at the pinned revision because main records the router's initial commit. At 08bd6e2 the relay, election and attach suites all execute and pass. So this bump converts three silently-skipped suites into three real ones — worth knowing, since "green" previously meant something weaker than it looked.
  • pio run -e ampworks and -e usermods both SUCCESS, with .pio/libdeps/ampworks/RS485Utils cleared first so the build could not read a stale cached copy of the library (that has produced one false SUCCESS in this repo already).

#22 is closed as superseded — both its bumps are contained here.

WLED -> 586c6136 (the attach-protocol wire format) and esp-now-router ->
d4154389 (the router firmware, attach protocol and failover). main still pinned
esp-now-router at its initial README commit, so the router's test suites were
being skipped rather than run.

Also correct CLAUDE.md: custom work now branches from main, since
base-wled-16.0.1 was renamed to main and the pre-v16 line kept as
main-pre-v16.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants