Agent Mesh
Agent Mesh Code Review Style Guide
This file extends the standard Gemini Code Assist review prompt for this repository. It lists checks that are specific to Agent Mesh and that generic review does not catch. Apply them to every pull request that touches Go code. When a rule is violated, quote the offending line and state the concrete fix; do not only describe the principle.
AGENTS.md at the repository root holds the architecture and dependency
rules and is authoritative. This guide adds review-time checks on top of it.
1. Peer IDs must be canonicalized at the boundary
Why
A libp2p peer ID has several valid string encodings. peer.Decode accepts
all of them (base58 multihash 12D3KooW..., CIDv1 base32 bafzaajai...,
and others) and returns the same peer.ID, but peer.ID.String() always
emits the base58 form. Every cache, ban set, storage row, and map[string]
in Agent Mesh is keyed on that canonical form. A raw string copied off the wire and
used as a key is therefore a different key for the same peer, and a ban,
admission or dedup check silently misses. This is a security bug, not a
style issue.
How to spot untrusted input
The repository has a naming convention that makes this reviewable:
PeerId/GetPeerId()(protobuf spelling) is a wire field on anapi.*message:req.PeerId,event.PeerId,ann.GetPeerId(). Treat it as untrusted, non-canonical input.PeerID(Go spelling) is an internal field or variable that is expected to already hold the canonical form.- Other raw sources: URL query parameters (
r.URL.Query().Get("peer_id"),Get("id")), path segments (/mesh/{peer}/...), HTTP headers, MCP tool parameters.
Rules
Flag any of these when the operand comes from a raw source:
- Discarded decode.
if _, err := peer.Decode(s); err != nil { ... }followed by continued use ofs. The decodedpeer.IDmust be kept and used (or its.String()), never the input string. - Raw string as a key.
m[x.PeerId],cache.Add(x.PeerId, ...),cache.Contains(x.PeerId),seen[p.PeerId]. - Raw string stored in a record.
storage.EnrolledNode{PeerID: req.PeerId},storage.RouterLease{PeerID: req.PeerId},discovery.Provider{PeerID: ann.GetPeerId()}. - Raw string passed to a lookup keyed on the canonical form.
store.GetNode(ctx, req.PeerId),store.IsNodeBanned(ctx, req.PeerId),store.GetEnrollmentRequest(ctx, peerIDFromQuery),mesh.PublishEvent(ctx, api.MeshEvent_BANNED, req.PeerId, nil). - Raw string compared for identity.
x.PeerId == selfID,params.PeerID == n.Host.ID().String(). Decode both sides and comparepeer.IDvalues, or compare.String()of two decoded IDs. Emptiness checks (!= "") are fine. - Inconsistent handling inside one handler. A function that decodes
req.PeerIdintopIDand then usesreq.PeerIdfor storage or publishing on a later line. OncepIDexists,req.PeerIdshould only appear in log messages and error strings.
Expected shape
pID, err := peer.Decode(req.PeerId)
if err != nil {
http.Error(w, "Invalid Peer ID", http.StatusBadRequest)
return
}
canonical := pID.String()
// From here on: store, look up, publish and compare with pID / canonical.
Pubsub and libp2p callbacks already hand over a peer.ID
(msg.GetFrom(), conn.RemotePeer()); those are trusted and need no decode.
Tests
A change that fixes a bug MUST have a regression test. Tests MUST NOT shallow errors, those need to be handled and fail the test. Any new feature or codepath MUST have the corresponding testing coverage.
Tests must follow the pyramid of testing. Enforce strict modularity in testing. The repository uses a defined testing pyramid (Unit, Integration, and E2E via Bats). You must adhere to the following testing philosophy:
- Optimize for Test Speed: E2E tests are slow and strictly based on existing Critical User Journeys (CUJs).
- Push Coverage Down: If test coverage for a specific edge case or feature can be added at a lower level (Unit or Integration), it is strictly preferred over E2E for speed.
- No Redundancy: Do not replicate a test in the slower E2E path if it is already sufficiently covered in the Integration path.
Test Domains:
- Unit Tests: Focus on isolated, internal functions.
- Integration Tests (tests/integration/): Verify module interactions and API compliance in Go and those are time bounded, no more than 10 seconds per execution.
- E2E Tests (tests/e2e/*.bats): Use Bats (Bash Automated Testing System) exclusively for high-level, black-box testing of core CUJs.
Ask for tests if they are missing.
2. Time units must be explicit and consistent
Why
Time values cross the wire as bare int64. The proto already mixes units:
- Unix milliseconds:
MeshEvent.timestamp, the enrollment and refresh challengetimestampfields, theX-Mesh-Challenge-Tsheader. - Unix seconds:
ServiceAnnounce.timestamp, everyexpires_at,checked_at,credential_expires_at,biscuit_expires_at. - Explicit-unit names:
latency_ewma_ms,poll_interval_seconds.
A mismatch between producer and consumer is a silent off-by-1000 that
freshness checks and TTLs will not catch in tests that use time.Now() on
both sides.
Rules
- Every new
int64time field inapi/agentmesh.protomust state its unit, either in the field name (_ms,_seconds) or in a comment on the field. Prefer milliseconds for new instant fields; prefer the_mssuffix. - Encoder and decoder must agree.
time.Now().UnixMilli()on the producing side pairs withtime.UnixMilli(x)on the consuming side;time.Now().Unix()pairs withtime.Unix(x, 0). Flag any field written with one and read with the other, including in tests. - One unit per container. A map, cache or struct field must not receive
seconds on one code path and milliseconds on another. Example to flag:
seeding a cache with
time.Now().Unix()in a constructor and later writingevent.Timestamp(milliseconds) into the same cache from an event handler, even if the value is never read. - Comparisons across units are wrong.
event.Timestamp < lastSeenis only valid if both were produced with the sameUnix*()call. Check the origin of both operands. - Durations stay
time.Durationinside Go. Convert to an integer only at the serialization boundary, and name the unit there. - Freshness windows and TTLs in config (
FreshnessThreshold,BiscuitTimeout,LeaseDuration,PolicySyncJitter, etc.) must betime.Duration, not bare integers.
3. Test coverage must follow the pyramid
Why
E2E tests (tests/e2e/*.bats) are slow and reserved for critical user
journeys. Integration tests (tests/integration/) are bounded to 10 seconds
per execution. Coverage belongs at the lowest layer that can express it.
Rules
- Every behavior change ships with a test at the lowest adequate layer.
- Pure logic, parsing, canonicalization, unit conversion, policy
evaluation: unit test in the same package (
*_test.go). - Interaction between two components over the API in
api/agentmesh.proto(node ↔ control plane, router ↔ control plane):tests/integration/. - A full user journey across built binaries:
tests/e2e/*.bats, and only if it is a documented CUJ.
- Pure logic, parsing, canonicalization, unit conversion, policy
evaluation: unit test in the same package (
- Flag coverage that is too high in the pyramid. A new bats test for an edge case that a Go unit or integration test could pin is a request for change: ask for the lower-level test and removal of the e2e one.
- Flag missing regression tests for bug fixes. A PR whose description says "fix", "bypass", "canonicalize", "race", "leak" or similar must add a test that fails on the parent commit. If no test is present, ask for one and suggest the layer.
- Flag duplicate coverage. The same scenario asserted in both integration and e2e paths; keep the faster one.
- Integration tests must be time bounded. Look for unbounded waits,
time.Sleeplonger than a few hundred milliseconds, or polling without a deadline. Prefercontext.WithTimeoutandt.Deadline(). - Security fixes need a negative test. For anything touching authentication, admission, bans, biscuit verification or label gating, the test must include the attacker's input (the forged token, the alternative encoding, the stale timestamp) and assert rejection, not only the happy path.
- Tests must not weaken production checks. Flag test-only flags,
if testingbranches, or exported hooks whose only purpose is to skip a verification step. - Tests are plain Go on the standard
testingpackage. Flag any import ofgithub.com/stretchr/testify,github.com/onsi/ginkgo,github.com/onsi/gomegaor another assertion or matcher library, in test code or ingo.mod; the fix isif got != want { t.Fatalf("...: got %v, want %v", got, want) }. Flag suite objects, fixture hierarchies,Describe/Itstyle nesting, and helpers whose only effect is to make one test shorter: a helper is justified by repetition across tests. Flag a test that logs an error and continues, or that ignores a returned error (_ = f()) on the path under test: the error must fail the test. Flag assertion messages that do not state both the observed and the expected value.
4. Dependencies
Reinforcing AGENTS.md §2: a PR that adds a line to the root go.mod
require block must justify it in the description and must have been
explicitly agreed to. If the dependency only runs inside a sandbox image, it
belongs in that command's own module (cmd/nano-init/go.mod pattern). Flag
any new root dependency that lacks this justification.
5. API surfaces and secrets
Why
Agent Mesh has two API surfaces with different encodings (AGENTS.md §1, "Two API
surfaces"): the mesh protocol between components is protobuf from
api/agentmesh.proto; the operator plane (/admin/*, /users/*) is JSON whose
shapes are Go structs in api/. Shapes defined ad hoc inside a handler, or
borrowed from internal/storage, have no single owner: the console, the CLI
and the tests each re-spell the field names, and a rename breaks one of them
silently. Secrets passed as flag values leak through ps and shell history
regardless of how carefully the rest of the system handles them.
Rules
- Flag wire shapes defined outside
api/. In a handler,var req struct { ... json:"..." },json.NewEncoder(w).Encode(map[string]any{...}), or a client buildingmap[string]any{"ttl_hours": ...}is a request for change: name theapi.*Request/api.*Responsetype that should exist (or the proto message, if a mesh component consumes it). - Flag internal types on the wire.
json.NewEncoder(w).Encode(list)wherelistis[]*storage.X, or acmd//internal/consolepackage importinginternal/storageto decode a response, serializes storage details (hashes, timestamps, column-shaped names) as an API. Propose anapi.XInfotype and quote the fields that must not cross. - Flag the wrong encoding for the surface. A new mesh-protocol message
as JSON, or a new operator endpoint that only the console will call as
protobuf, needs a stated reason. The tell for "mesh protocol" is a
consumer in
internal/node,internal/router,internal/samboxor the FFI. - Flag secrets as flag values. Any
Flags().StringVar(&x, "...token"| "...secret"|"...password", ...)whose value is the credential itself, rather than a--*-pathor an env var name, is a finding; so is a banner or log line that prints a credential the operator supplied.agentmesh-control-plane --admin-token-pathandagentmesh-node --bootstrap-token-pathare the reference shape.
6. Behavior must be auditable from logs and metrics
Why
Agent Mesh runs as a fleet of routers and thousands of members on networks
nobody can attach a debugger to. When a member does not attach, a router
pair does not federate, or a call fails, the operator has the binaries'
logs and /metrics and nothing else. A decision the code takes without a
trace (a continue on a condition, an early return nil, a branch that
skips a handshake because a connection already exists) is correct until it
is not, and then the failure cannot be explained without a reproduction. A
bug whose diagnosis needed a temporary print statement is a bug in the
instrumentation as much as in the logic.
Rules
- Every decision about a peer leaves a trace. Admission, refusal,
closing a connection, sending a peer away, skipping a candidate, giving
up a retry: each has a log line at the level its frequency allows
(
Infofor state changes,Debugfor per-request steps,Warnfor something an operator should act on) or a counter with areasonorresultlabel. Flag acontinue,returnorbreakon an error or a condition that has neither. - State is a gauge. A set the component maintains (attached routers,
authenticated peers, peer routers listed and authenticated, leased
addresses, banned peers, pending retries) is exported as a gauge from the
live data structure, never from a counter that is incremented and
decremented by hand. Flag a new
sync.Map, map or slice that models a peer or member set without a corresponding metric. - Errors are not swallowed.
_ = f()andif err != nil { return }on anything other than a close or a best-effort write need a log line or a counter. Flag the bare form. - Log lines carry the identifier. A line about a peer names the peer ID; a line about a lease names the addresses; a line about a router names the router. Flag lines that say what happened without saying to whom.
- A flaky-test fix adds instrumentation. A flake is a race the
production code has too. A PR that makes a test deterministic by
waiting on a condition must expose that condition in the binary (a
gauge, a
/debugview, a log line) and wait on it there, not on atime.Sleep. Flag a fix whose only change is in the test, and flag a retry loop, a widened timeout or a sleep added to a test that failed in CI: ask for the reproduction (stresswith-race, seeAGENTS.md§3) and the code change it led to. - Safe defaults, zero configuration. A new flag or option needs a
default that is secure without the operator reading the docs: deny, verify,
short-lived, loopback-only, bounded. Flag a default that opens something
(
AllowInsecure*true, an unbounded limit, a disabled check) and a flag whose help text does not name the risk of loosening it. Ask whether the flag is needed at all when the safe default covers every deployment.
7. Review output
- Group findings by the section numbers above so the author can see which rule applies.
- For rule 1, rule 2, rule 5 and rule 6 findings, always propose the corrected code, not just the diagnosis.
- Do not raise generic Go style nits (naming, comment punctuation, import
ordering) that
gofmtandgolangci-lintalready enforce viamake lint.