Commit Graph
2 Commits
  • fix(node): don't delete clients when a node reports an empty snapshot (#6734)
    * fix(node): don't delete clients when a node reports an empty snapshot
    
    A node snapshot that comes back with zero clients for an inbound the hub
    still has clients on was treated as authoritative: SyncInbound strips
    every link for that inbound, the orphan sweep marks the now-linkless
    clients, and ReapSyncOrphans hard-deletes them once the grace period
    elapses. But a zero-client snapshot is indistinguishable from a degraded
    node — one that was just deleted, reset, restarted, or answered before
    its config loaded. On 2026-10-04 this deleted clients across the whole
    hub when a single node was removed.
    
    Treat a zero-client snapshot for an inbound that still has clients as
    non-authoritative: skip the link rebuild and the orphan sweep for that
    inbound (the same handling a failed SyncInbound already gets) and wait
    for a snapshot that carries clients. Removing a node's last client is
    done from the hub (which updates links and pushes); a node still serving
    other clients prunes a removed one through the existing partial path.
    
    The two orphan tests that drove removal via an empty snapshot now drive
    it via a partial snapshot (node alive, still serving another client),
    the authoritative path. New tests in node_degraded_snapshot_test.go
    cover the guard and its narrowness.
    
    Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
    
    * fix(node): keep hub settings on an empty node snapshot; address review
    
    The empty-snapshot guard skipped only the link rebuild and orphan sweep,
    but Phase A had already adopted the node's `{"clients":[]}` blob into
    `inbounds.settings`. Reconcile builds each push from that blob, so the hub
    kept re-pushing an empty client list and a reset/restarted node never got
    its clients back — the guard fired forever. Phase A now refuses to blank the
    settings of an inbound the hub still populates, so the hub stays
    authoritative and reconcile re-pushes the real clients (recovery). A
    node-side removal of the last client is therefore hub-authoritative by
    design; the partial-snapshot path still prunes an inbound that reports other
    clients.
    
    - test: assert the inbound's settings survive an empty snapshot (fails
      without the Phase A fix).
    - drop TestSetRemoteTraffic_EmptySnapshotSurvivesReap (no branch the core
      test doesn't already cover) and the duplicate orphanMark helper
      (readOrphanMark already exists); trim the comment blocks to the 2-line
      CLAUDE.md limit.
    - fix a pre-existing -race/-shuffle flake: TestGetAmneziaWGLogs owned no DB
      and relied on the ambient global one, which a sibling's dbtest cleanup
      closes under shuffle, panicking on nil in amneziawgLogActivity. It now
      owns a throwaway DB.
    
    Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
    
    * fix(node): re-push hub clients to a node that reports an empty snapshot
    
    23c51074 stopped the empty snapshot from blanking inbounds.settings, but
    nothing re-sent those settings: the node was never marked dirty, and the
    reconcile fingerprint from the last good push still matched, so
    ReconcileInbound skipped the inbound. A reset node stayed empty while the
    hub kept handing out links to it. The ClientStats sweep also deleted the
    node baseline on that tick, so traffic used until recovery was lost.
    
    The empty snapshot is now classified once, before adoption: its settings
    and wire fields are not adopted, the ClientStats sweep, link rebuild and
    orphan sweep skip it, the node is marked dirty and the inbound's pushed
    fingerprint is dropped (Remote.ForgetPushedInbound) so reconcile re-sends
    the hub's clients.
    
    ---------
    
    Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
    Co-authored-by: Yuriy Khachaturian <y.khachaturian@souzmult.ru>
  • fix(api-token): let a node-sync master reach every node endpoint it calls
    Invariant: every request runtime.Remote sends to a node is accepted under
    the node-sync scope, except /server/updatePanel, which #6201 withholds on
    purpose.
    
    The allowlist was written on 2026-08-15 and three Remote calls arrived
    after it without being added: /clients/activeInbounds (#6164, same day),
    /inbounds/:id/subSortIndex (#6179) and /clients/bulkResetTraffic
    (4210a50c). A master enrolled with a node-sync token or mTLS got 403 on
    each: the subscription sort order and multi-client traffic resets never
    reached the node, and online attribution silently fell back to email-only
    because FetchTrafficSnapshot logs that failure at debug level.
    
    TestMasterNodeContract drives every exported Remote method through the
    production router, once per enrollment scope, fails on any 401/403 the
    node returns even when Remote swallows it, and fails when a Remote method
    has no cell. It replaces TestNodeSyncScopeAllowlistMatchesRemoteInventory,
    a hand-copied duplicate of the allowlist that never read Remote and so
    missed all three; its updatePanel rule stays in
    TestNodeSyncScopeUsesFullPathPatterns. It also supersedes
    TestMasterPushWithAdminTokenAppliesClients (its UpdateInbound cell); that
    file's removal landed in dae91276 by a staging slip.