Files
DeepSeek-Reasonix/tools/repolint/baseline.go
T
YHH ee8a85ec04 chore(standards): machine-enforce comment, file-size, and layering rules (#7806)
* chore(standards): gate comments, file size, and layering in CI

The comment and file-size rules lived only in an untracked CLAUDE.md written
for a TypeScript project, so nothing ever checked them, and REASONIX.md told
editors to match the surrounding comment density -- which is what reproduced
the essays. tools/repolint makes the rules machine-checkable and ratchets
them: recorded debt passes, anything new fails the required lint check.

Weights are excess lines rather than counts, so trading a short violation for
a long one still trips the gate. Layering encodes the contract REASONIX.md
already claimed and the import graph already satisfies, with one recorded
exception (capdiag reaching into boot).

* chore(standards): point CLAUDE.md at REASONIX.md

Claude Code loads CLAUDE.md and Reasonix loads REASONIX.md, so a repo with
only the latter leaves Claude Code sessions with no standing instructions.
A committed one-line pointer keeps the single source of truth without the
two files drifting apart again; the negation un-ignores it for contributors
whose global gitignore drops CLAUDE.md.
2026-08-07 11:25:52 +08:00

97 lines
2.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
)
// Ratchet snapshot, in weighted units: no file may exceed its budget and no
// rule may exceed its repo ceiling. Both are relaxed only by hand, in a diff a
// reviewer sees.
type Baseline struct {
Limits map[string]int `json:"limits"`
Files map[string]map[string]int `json:"files"`
}
func loadBaseline(path string) (*Baseline, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var b Baseline
if err := json.Unmarshal(data, &b); err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
if b.Limits == nil {
b.Limits = map[string]int{}
}
if b.Files == nil {
b.Files = map[string]map[string]int{}
}
return &b, nil
}
func baselineFrom(findings []Finding) *Baseline {
b := &Baseline{Limits: map[string]int{}, Files: map[string]map[string]int{}}
for _, rule := range allRules {
b.Limits[rule] = 0
}
for _, f := range findings {
b.Limits[f.Rule] += f.Weight
if b.Files[f.File] == nil {
b.Files[f.File] = map[string]int{}
}
b.Files[f.File][f.Rule] += f.Weight
}
return b
}
func (b *Baseline) write(path string) error {
data, err := json.MarshalIndent(b, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, append(data, '\n'), 0o644)
}
func (b *Baseline) exceeded(findings []Finding) ([]Finding, []string) {
counts := map[string]map[string]int{}
totals := map[string]int{}
for _, f := range findings {
if counts[f.File] == nil {
counts[f.File] = map[string]int{}
}
counts[f.File][f.Rule] += f.Weight
totals[f.Rule] += f.Weight
}
var msgs []string
over := map[string]map[string]bool{}
for _, file := range sortedKeys(counts) {
for _, rule := range sortedKeys(counts[file]) {
allowed := b.Files[file][rule]
if got := counts[file][rule]; got > allowed {
msgs = append(msgs, fmt.Sprintf("%s: %s is %d over its %d budget", file, rule, got, allowed))
if over[file] == nil {
over[file] = map[string]bool{}
}
over[file][rule] = true
}
}
}
for _, rule := range sortedKeys(totals) {
if totals[rule] > b.Limits[rule] {
msgs = append(msgs, fmt.Sprintf("repo total for %s is %d, above the %d ceiling", rule, totals[rule], b.Limits[rule]))
}
}
var shown []Finding
for _, f := range findings {
if over[f.File][f.Rule] {
shown = append(shown, f)
}
}
return shown, msgs
}