Skip to content

Make code comments name the hazard and point, with the design living in docs - #303

Merged
github-actions[bot] merged 4 commits into
developfrom
arashi.li/comment-standard
Aug 19, 2026
Merged

github-actions[bot] merged 4 commits into
developfrom
arashi.li/comment-standard

Conversation

@arasHi87

Copy link
Copy Markdown
Collaborator

Closes

Closes #122

What & why

#122 asked for a comment tidy-up. The scan that opened it overturned three of its four premises, and the convention it recorded got reversed during review — so this lands a different, and I think better, rule than the ticket describes.

The rule. The code is the explanation; a comment exists to stop a wrong edit and point at the document that owns the reasoning. Target ~7 lines. The ticket's convention was the opposite ("self-contained comments, with a trailing pointer as a depth link, not the sole explanation"), which is what produced walls like BuildTrie's 89-line transcription of an algorithm trie-construction.md already owns.

Moving prose out is a two-step edit. Confirm the content is in docs/ first; if it is not, add it there before cutting. Three things turned out to live only in code and are now documented — the device_owner partition table, the WAL schema history (data-structures.md still said v3 while the code was at v7), and the BPF-ABI layering carve-out.

Naming beats shortening. The last commit is the version of this that actually works: is_group_mac() and endpoints_from_vm() delete fifteen lines of comment by giving the operations names, rather than compressing the description of them.

Numbers: blocks of 25+ lines 29 → 0; the 15–24 band 64 → 5 (all package entry points carrying invariant lists). Go comment density 30.9% → 27.0%. bpf/telemetry.c 51% → 43%.

Test plan

  • Unit — task test
  • Integration — task test-integration (Docker, BPF caps)
  • Bench-gate — task bench-gate (hot-path changes only)
  • Scenario — scenariotest run <name>
  • Manual e2e on a live cluster

task test-integration: 40 packages, 0 failures — including the kernel-loading E2E tests, so the verifier accepts the new __always_inline helpers. task bench-gate-integration: every BenchmarkHotpath_ still at zero allocations. The docs guard validates every remaining reference.

Scope / deliberately not touched

  • cmd/scenariotest/scenarios/registry.go and host_route_nexthop.go — untouched throughout, to avoid colliding with in-flight work on develop.
  • Blocks in the 8–14 line band. The pass ran to 25+, then 15+; below 15 the remaining blocks average 11.9 lines across 141 files and are already local hazard notes, so trimming them changes a line count without changing what a reader gets. The ~7-line target stands as guidance for new code.
  • The zone_code and flow_metrics reference numbers were not re-measured; see Notes.

Notes

Contracts. Touches the packet path and the billing-path comments, but no contract behaviour. bpf/telemetry.c is the only functional change and it is a pure extraction: is_group_mac() and endpoints_from_vm() are __always_inline, the directional swap picks the same MAC and IP per hook as before, and the group-MAC test is the same bit. Contract 5's in-band created_ns comparison, Contract 2's single-lock Collect, and Contract 7's fold-forward are unchanged — only the prose around them moved.

One thing for the reviewer to resolve. Writing the two copies side by side surfaced a drift: the subnet_zone_trie comment cited a 37-tenant host at ~14,252 pre-dedup entries, while data-structures.md:109 cites a 27-tenant host at 9,270. Both cannot describe the same measurement. I kept the doc's figures rather than pick a winner — if the 37-tenant number is the newer one, the doc needs the update. This is exactly the drift the convention's argument predicts, and it had already happened.

Follow-up worth filing. The extraction in the last commit is the pattern the whole ticket wanted. There are likely more sites where a comment is describing code that wants a name — settleAttributionChange / SettleRebase and the four-tier fold in Collect are where I would look first.

#122's DoD needs rewriting, not ticking. Two of its bullets no longer describe this work: the doc.go-per-package item (46 of 48 packages already had a proper package comment — it was a file-placement preference, not a gap) and the telemetry.c "one self-contained file-header model block" item (each model fact appears exactly once, adjacent to the code it guards; consolidating would move explanations away from the line being edited).

DoD

  • Linked issue's DoD met, incl. Handbook knowledge update (/bigstack-core:save-to-handbook)

@arasHi87 arasHi87 added the done Mark a PR ready to merge (triggers auto-merge once CI + reviews pass) label Aug 19, 2026
@arasHi87 arasHi87 closed this Aug 19, 2026
@arasHi87 arasHi87 reopened this Aug 19, 2026
…carry the design

The comment rules now lead with a single idea: the code is the
explanation, and a comment exists to stop a wrong edit and point at the
document that owns the reasoning. A ~7-line target replaces the old
open-ended 'self-contained comment' convention, and moving prose out
becomes a two-step edit — confirm the content is in docs/ first, add it
there if not, only then cut. Never delete the only record.

Also records the BPF-ABI layering carve-out, which until now existed
only as a comment in internal/bpf/abi.go: importing the ABI value types
from L2/L3/L4 is a shared data contract, not a layer violation, and must
not be 'fixed' by hand-copying generated struct layouts.

The C placement rules tighten too. No member of a declaration gets a
comment block of its own — C has no doc tool that renders one, so inside
a struct body it is only a visual interruption — and member notes are
one-line trailing, all-or-none across the declaration.

Signed-off-by: arashi.li <arashi.li@bigstack.co>
…AL schema history

Both of these existed only as code comments, so the comment tidy-up
would have deleted the sole copy of each.

The device_owner partition — which owners are infra, which are VM-like,
and the network:floatingip carve-out — is verified ground truth on the
OVN-Yoga target, including cube:mgr and the trunk-subport case. It now
sits under the five-step algorithm, where Step 4 consumes it, with the
prefix-match-not-allowlist rationale and the separate IsKnownVMOwner
allowlist that only drives the drift warning.

The WAL schema history was worse: data-structures.md still described a
v3 envelope while wal.SchemaVersion was at 7, so the code comment was
the only accurate record. The history is now a table stating why each
bump stayed additive and therefore why an older file still loads, plus
the note that adding a version means adding a row.

One discrepancy surfaced by writing them down: the code comment cited a
37-tenant host at ~14252 pre-dedup entries, the doc a 27-tenant host at
9270. Left as the doc's figures pending a re-measure.

Signed-off-by: arashi.li <arashi.li@bigstack.co>
…e comments can go

Two operations in handle_packet were carrying multi-line comments that
described what a well-named function would have said outright.

is_group_mac() replaces a bare 'key.dst_mac[0] & 0x01' and the six lines
explaining the IEEE 802 I/G bit. endpoints_from_vm() replaces the
three-way ternary swap and its nine lines: the returned struct's fields
are vm_mac / peer_mac / remote_ip, which is the whole of what the
comment used to spell out. Both are __always_inline, so the generated
program is unchanged in shape.

What stays on endpoints_from_vm is the part the code still cannot say —
that both hooks sit on the same tap, so reading h_source/daddr
unconditionally makes egress look the VM's own address up as its peer,
and mis-bills silently.

The rest of the file follows the tightened C rules: no comment block
inside a declaration body (the wire-byte-order note moves above the
lpm_key initializer, and all three members take one-line trailing
notes), and no block over seven lines outside the file header. Comment
share drops from 51% to 43%.

Verified with task test-integration — the program loads, so the verifier
accepts the struct-returning helper — and task bench-gate-integration,
which holds the hot path at zero allocations.

Signed-off-by: arashi.li <arashi.li@bigstack.co>
…cs that own it

A repo-wide pass applying the ratified rules. Blocks of 25+ lines are
gone entirely (29 of them), and the 15-24 band drops from 64 to 5 — the
five that remain are package entry points whose length is an invariant
list, which the convention allows.

The pattern throughout: keep the sentence a reader would otherwise get
wrong, drop the paragraph explaining why, point at the document. The
worst case was BuildTrie's 89-line transcription of the five-step
algorithm that trie-construction.md already owns, now 8. state.go went
169 comment lines to 74. Where a block was the only record of something,
the content moved to docs/ first — see the preceding commit.

Also fixed on the way: two file-scope headers had lost the blank line
before their package clause, so godoc had silently promoted them to
package documentation, and internal/scenariotest carried a second
'Package scenariotest' comment alongside its doc.go, which godoc
concatenates. Two literal **bold** markers removed — godoc has no bold,
so they reached the reader as asterisks.

Citations moved to their own trailing line, once per block, never
mid-sentence: an anchored path runs 50-80 characters and broke both the
sentence and the 80-column wrap. That is why the validated-reference
count falls from 321 to 261 — repeats collapsing, not citations lost.

Go comment density 30.9% -> 27.0%. task test and task test-integration
both green; the docs guard validates every remaining reference.

Signed-off-by: arashi.li <arashi.li@bigstack.co>
@arasHi87
arasHi87 force-pushed the arashi.li/comment-standard branch from 718bf09 to dbe6ae6 Compare August 19, 2026 02:04
@arasHi87 arasHi87 added done Mark a PR ready to merge (triggers auto-merge once CI + reviews pass) and removed done Mark a PR ready to merge (triggers auto-merge once CI + reviews pass) labels Aug 19, 2026
@github-actions
github-actions Bot merged commit 6ed2b60 into develop Aug 19, 2026
10 checks passed
@github-actions
github-actions Bot deleted the arashi.li/comment-standard branch August 19, 2026 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

done Mark a PR ready to merge (triggers auto-merge once CI + reviews pass)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Comment tidy-up scan (self-contained comments + doc.go)

1 participant