Files
meshcore-analyzer/internal/packetpath/trust.go
T
efitenandClaude Opus 5 176bb53335 fix(#1784): ship pathTrust default 1, not 2 (#1929)
Follows #1841. Moves the pathTrust default from 2 back to 1.

## Why

#1784's first acceptance criterion is **"Default behaviour remains
backward-compatible"**, and its example config shows
`minHashBytesForMapping: 1`. What shipped is 2.

The problem is not the value. It is that **there is no way to undo it
from the UI.** #1841 adds no control for the threshold: it is
`config.json` only, and changing it needs a restart. The customizer
gains a hint that says exactly that. So an instance that upgrades
without touching config switches to the stricter rule, and the only
visible symptom is that the neighbour graph and the resolved paths
quietly get smaller.

The existing "Hide 1-byte path hops" toggle (#1633) is a *display*
filter and does not change what counts as evidence, so it is not an
escape hatch either. The two are easy to confuse.

## How much this actually moves

Measured on a live instance via `/api/analytics/hash-sizes`, not
estimated:

| path-hop observations | count | share |
|---|---|---|
| 1-byte prefix | 116,923 | **56.0%** |
| 2-byte prefix | 86,031 | 41.2% |
| 3-byte prefix | 5,753 | 2.8% |

| repeaters by observed hash size | count |
|---|---|
| 1-byte | **645 (41%)** |
| 2-byte | 865 |
| 3-byte | 63 |

At threshold 2 the 1-byte column stops counting as mapping evidence.
`MeetsPathTrust` also drops the legacy bucket-0 observations with it
(pre-#1638 persisted neighbor edges that carry no per-mode breakdown),
so already-stored edges lose their evidence status on upgrade too.

## What this does not change

The knob works and is untouched. Operators who want the stricter
behaviour set `minHashBytesForMapping` to 2 or 3, which is the opt-in
#1784 describes. Only the default moves. Nothing about storage changes;
packets and paths were never affected either way.

## Also fixes an inconsistency inside #1841

Five frontend consumers already fall back to **1** when
`MC_getPathTrustThreshold()` is unavailable: `analytics.js`, `live.js`,
`map.js`, `nodes.js`, `route-view.js`. Two fell back to **2**:
`hop-filter.js` (the getter itself) and `customize-v2.js`. They now all
agree.

## Tests

- `internal/packetpath`: `TestMeetsPathTrust_ZeroValueOptIn` was
asserting the old default *through behaviour*, so it would need
rewriting on any future default change. It now asserts the property
instead: an absent JSON field resolves to
`DefaultMinHashBytesForMapping`, behaves identically to naming that
value outright, and an explicit stricter setting still wins. Package
tests pass.
- `test-issue-1633-hide-1byte-hops.js`: the case pinning the getter's
default is updated, with the reasoning in a comment so the next person
sees why it is 1. **37 passed, 0 failed** (master baseline: 37 passed, 0
failed).
- `cmd/server` config tests pass.

## One thing I want to flag rather than paper over

The test I changed was named `default is 2 (operator-confirmed)`. I am
overriding something that was confirmed with an operator, and I am not
claiming that confirmation was wrong. My reading is that it was about
the threshold being a *useful* value, which it is, rather than about it
being the default in a build with no UI to change it. If the intent
really was "2 out of the box for everyone", say so and I will close
this.

@Bjorkan as the issue author, @nullrouten0 and @Saarlandpower since you
have touched adjacent code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01Wzwr3eXseyNM7Xj598djjE

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-02 10:27:35 +02:00

71 lines
3.0 KiB
Go

package packetpath
// TrustConfig controls how much confidence a path-hash prefix observation
// must carry before it is used as topology/mapping evidence (issue #1784).
//
// MeshCore path hops are hashed pubkey prefixes of 1, 2, or 3 bytes
// (firmware/src/Packet.cpp:13-18, hash_size = (pathByte>>6)+1). Shorter
// prefixes collide more often — a 1-byte hash has only 256 possible values,
// so on denser meshes a "currently unique candidate" resolution can still
// be a false positive. TrustConfig lets operators require a longer prefix
// before a mapping consumer (neighbor graph, path resolver, neighbor
// builder, path inspector) treats a resolved hop as trustworthy evidence.
//
// This does not affect raw storage: packets and paths are always stored
// exactly as received. It only gates *derived* trust — which observations
// are eligible to become neighbor-graph edges, resolved-path hops, or
// capability inferences. See the #1784 call-site audit for the full list
// of consumers this is meant to gate.
type TrustConfig struct {
MinHashBytesForMapping int `json:"minHashBytesForMapping,omitempty"`
}
// DefaultMinHashBytesForMapping is the backward-compatible default: every
// prefix length counts as mapping evidence, exactly as before #1784.
//
// It is deliberately 1 rather than the stricter 2. There is no UI control for
// this threshold, it can only be changed in config.json and that needs a
// restart, so shipping 2 would tighten every instance on upgrade with nothing
// in the UI explaining why the neighbour graph shrank. Measured on a live
// network, 56% of path-hop observations carry a 1-byte prefix and 41% of
// repeaters use a 1-byte hash, so that is not a marginal change. Issue #1784's
// own first acceptance criterion is that the default stays backward compatible.
//
// Operators who want the stricter behaviour set minHashBytesForMapping: 2 or 3.
const DefaultMinHashBytesForMapping = 1
const MaxHashBytes = 3
func (c *TrustConfig) MinHashBytesOrDefault() int {
if c == nil || c.MinHashBytesForMapping <= 0 {
return DefaultMinHashBytesForMapping
}
if c.MinHashBytesForMapping > MaxHashBytes {
return MaxHashBytes
}
return c.MinHashBytesForMapping
}
// MeetsPathTrust reports whether a path-hash observation of the given
// prefix length (in bytes) should be trusted as mapping/topology evidence
// under cfg's threshold.
//
// prefixBytes == 0 denotes an unknown-length/legacy observation — e.g.
// pre-#1638 persisted neighbor edges with no per-mode breakdown
// (NeighborEdge.CountsByMode[0] in cmd/server/neighbor_graph.go). Per
// #1784's bucket-0 policy: once the threshold requires more than the
// wire-format minimum of 1 byte (minBytes >= 2), an unknown-length
// observation cannot be proven to meet it and is excluded. At the
// trust-all threshold (minBytes <= 1) bucket 0 passes, matching pre-#1784
// behavior exactly.
func MeetsPathTrust(prefixBytes int, cfg *TrustConfig) bool {
minBytes := cfg.MinHashBytesOrDefault()
if minBytes <= 1 {
return true
}
if prefixBytes <= 0 {
return false
}
return prefixBytes >= minBytes
}