carlos / platform Public

C2 Task 2: scoped scan, declaration envelopes, class left

rootReadScope narrows the scanner's output to what root runs; envelopes
hash a directory's reached declarations (doc comments included) and its
in-scope files, so agent and console code beside root code leaves them.
A unit or directory still in the tree that root no longer reaches is
class left, a reviewer approval written like a removal (@removed with
census-ruling-<NAME>;left, @coverage <dir> left); a later row re-admits
the unit. R0's removed and @coverage <dir> - are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 2c069bf5c7d2a0e2be8a30a1bae378340c6165de parent 1e49f4a
4 files changed, +384 −10
  • internal/provision/census_propose_test.go +75 −7
  • internal/provision/census_scope_fixture_test.go +171 −0
  • internal/provision/census_scope_test.go +92 −0
  • internal/provision/plan14_root_reads_test.go +46 −3
diff --git a/internal/provision/census_propose_test.go b/internal/provision/census_propose_test.go
index b73e24b55..b63d64bad 100644
--- a/internal/provision/census_propose_test.go
+++ b/internal/provision/census_propose_test.go
@@ -37,6 +37,7 @@ const (
censusSites = "sites" // more or fewer reads of kinds the unit already makes
censusEnvelope = "envelope" // a package's source fingerprint moved
censusRemoved = "removed" // a unit's declaration, or an envelope's directory, gone from the tree
+ censusLeft = "left" // a unit or directory still in the tree that root no longer reaches (C2)
censusReads = "reads" // a kind of read the unit did not make before
censusNew = "new" // an unclassified unit: needs a disposition
censusQuiet = "quiet" // still declared and reads nothing now, or a directory still there with no scanned source
@@ -166,6 +167,9 @@ func classifyRootReads(got rootReadScan, inv rootReadInventory, seal string) []c
cs = append(cs, censusChange{Key: key, Class: censusBody, Before: w, After: rootReadEntry{File: w.File, Unit: w.Unit, Digest: decl}})
case declared:
cs = append(cs, censusChange{Key: key, Class: censusQuiet, Before: w, After: rootReadEntry{File: w.File, Unit: w.Unit, Digest: decl}})
+ case got.Outside[key]:
+ // Still declared, but root's verbs no longer reach it (C2).
+ cs = append(cs, censusChange{Key: key, Class: censusLeft, Before: w})
case got.Files[w.File] || got.absent(w.File):
// The scan proves the declaration gone, and every site with it:
// its file was read without it, or the file is gone by Lstat.
@@ -184,7 +188,12 @@ func classifyRootReads(got rootReadScan, inv rootReadInventory, seal string) []c
for dir, hash := range inv.Coverage {
if _, ok := got.Coverage[dir]; !ok {
class := censusRemoved
- if !got.absent(dir) {
+ switch {
+ case got.absent(dir):
+ case got.Outside != nil:
+ // A scoped scan: the directory is there, root reaches none of it.
+ class = censusLeft
+ default:
class = censusQuiet
}
cs = append(cs, censusChange{Key: dir, Class: class, OldHash: hash})
@@ -281,7 +290,7 @@ func proposeRootReads(got rootReadScan, inv rootReadInventory, seal, name string
}
removed := map[string]bool{}
for _, c := range cs {
- if c.Class == censusRemoved && c.OldHash == "" {
+ if (c.Class == censusRemoved || c.Class == censusLeft) && c.OldHash == "" {
removed[c.Key] = true
}
}
@@ -315,7 +324,11 @@ func proposeRootReads(got rootReadScan, inv rootReadInventory, seal, name string
cover = append(cover, "@coverage\t"+c.Key+"\t-")
continue
}
- if c.Class == censusRemoved {
+ if c.Class == censusLeft && c.OldHash != "" {
+ cover = append(cover, "@coverage\t"+c.Key+"\t"+censusLeftMark)
+ continue
+ }
+ if c.Class == censusRemoved || c.Class == censusLeft {
b := c.Before
gone = append(gone, "@remove\t"+b.File+"\t"+b.Unit)
if _, isFrozen := inv.Frozen[c.Key]; isFrozen && inv.Removed[c.Key] == "" {
@@ -324,10 +337,17 @@ func proposeRootReads(got rootReadScan, inv rootReadInventory, seal, name string
owner = b.Task
}
claim := strings.TrimSpace(claims[c.Key])
+ mark := censusAbsentMark
+ if c.Class == censusLeft {
+ mark = censusLeftMark
+ if claim == "" {
+ claim = "left scope: still declared, no longer reached from root's verbs"
+ }
+ }
if claim == "" {
claim = "deleted: declaration and sites absent from the tree"
}
- gone = append(gone, "@removed\t"+b.File+"\t"+b.Unit+"\t"+owner+"\t"+ruling+";"+censusAbsentMark+";basis="+claim)
+ gone = append(gone, "@removed\t"+b.File+"\t"+b.Unit+"\t"+owner+"\t"+ruling+";"+mark+";basis="+claim)
}
continue
}
@@ -345,6 +365,9 @@ func proposeRootReads(got rootReadScan, inv rootReadInventory, seal, name string
if counts[censusRemoved] > 0 {
header = append(header, fmt.Sprintf("# %d removed: each unit's declaration, or each envelope's directory, is absent from the scanned tree, so its row goes, a frozen declaration is retired as absent, and an envelope is retired.", counts[censusRemoved]))
}
+ if counts[censusLeft] > 0 {
+ header = append(header, fmt.Sprintf("# %d left scope: still in the tree, no longer reached from root's verbs, so its row goes, a frozen declaration is retired while it stays out, and an envelope leaves.", counts[censusLeft]))
+ }
lines := append(append(append(append(header, frozen...), plain...), gone...), cover...)
overlay := strings.Join(lines, "\n") + "\n"
@@ -384,8 +407,8 @@ func rootReadCard(name string, cs []censusChange, claims map[string]string, seal
var b strings.Builder
fmt.Fprintf(&b, "# Census approval: %s\n\n", name)
var decisions []censusChange
- var envelopes, retired []string
- var units, removed []censusChange
+ var envelopes, retired, leftDirs []string
+ var units, removed, left []censusChange
for _, c := range cs {
switch {
case censusDecision(c.Class):
@@ -396,6 +419,10 @@ func rootReadCard(name string, cs []censusChange, claims map[string]string, seal
retired = append(retired, "`"+c.Key+"`")
case c.Class == censusRemoved:
removed = append(removed, c)
+ case c.Class == censusLeft && c.OldHash != "":
+ leftDirs = append(leftDirs, "`"+c.Key+"`")
+ case c.Class == censusLeft:
+ left = append(left, c)
default:
units = append(units, c)
}
@@ -437,12 +464,29 @@ func rootReadCard(name string, cs []censusChange, claims map[string]string, seal
if len(retired) > 0 {
fmt.Fprintf(&b, "Envelopes retired: %s. Each directory is gone from the tree.\n\n", strings.Join(retired, ", "))
}
- if len(units) > 0 || len(envelopes) > 0 || len(removed) > 0 || len(retired) > 0 {
+ if len(left) > 0 {
+ b.WriteString("Left the census's scope: still declared, no longer reached from root's verbs.\n\n| unit | recorded disposition | basis | what changed |\n|---|---|---|---|\n")
+ for _, c := range left {
+ claim := strings.TrimSpace(claims[c.Key])
+ if claim == "" {
+ claim = "no longer reached"
+ }
+ fmt.Fprintf(&b, "| `%s` `%s` | %s, task %s | %s | %s |\n", c.Before.File, c.Before.Unit, c.Before.Class, c.Before.Task, c.Before.Basis, claim)
+ }
+ b.WriteString("\n")
+ }
+ if len(leftDirs) > 0 {
+ fmt.Fprintf(&b, "Envelopes that left the census's scope: %s. Root reaches nothing in each directory now.\n\n", strings.Join(leftDirs, ", "))
+ }
+ if len(units) > 0 || len(envelopes) > 0 || len(removed) > 0 || len(retired) > 0 || len(left) > 0 || len(leftDirs) > 0 {
b.WriteString(censusReviewQuestion)
}
if len(removed) > 0 || len(retired) > 0 {
b.WriteString(censusRemovalQuestion)
}
+ if len(left) > 0 || len(leftDirs) > 0 {
+ b.WriteString(censusLeftQuestion)
+ }
if len(units) > 0 {
b.WriteString("Recorded dispositions, which each change must still satisfy:\n\n")
for _, c := range units {
@@ -481,6 +525,30 @@ const censusReviewQuestion = "Review: a model from the other family than the aut
// move, and a check root relied on can go with the code that held it.
const censusRemovalQuestion = "A removed unit's recorded disposition and basis are in its table above. For a removed unit or a retired envelope, confirm from the diff that the code is deleted and not moved: a read that moved shows on this card as a change to the unit that took it, and a unit moved whole shows as a new unit, a decision. Escalate if a check, refusal or validation that root relies on goes with the deleted code and its callers' changes on this card do not account for that.\n\n"
+// censusLeftQuestion is what the reviewer answers for a unit or directory
+// that left the census's scope (C2): root no longer reaches it, which can
+// only take reads away from root, unless the call moved somewhere the
+// reference graph cannot see.
+const censusLeftQuestion = "A unit or directory that left the census's scope is still in the tree, but no declaration root's verbs reach refers to it now. Confirm from the diff that the reference that reached it was deleted or changed in code on this card (a unit or an envelope above), not hidden from the graph: a call by reflection, a go:linkname, or a function value or interface implementation set up by code root does not run. Escalate if root may still run it, or if a check root relied on went with it.\n\n"
+
+// censusLeftMark follows the ruling in the basis of an @removed row
+// census-propose writes for a unit that left scope, and is the hash of an
+// envelope that left: "@coverage <dir> left".
+const censusLeftMark = "left"
+
+// rootReadLeftRemoval reports whether a removal is one census-propose wrote
+// for a unit that left scope: basis census-ruling-<name>;left[;...]. It
+// holds while the unit stays out of scope; a later row that classifies the
+// unit again re-admits it.
+func rootReadLeftRemoval(removal string) bool {
+ _, basis, _ := strings.Cut(removal, ";")
+ ruling, rest, ok := strings.Cut(basis, ";")
+ if !ok || !strings.HasPrefix(ruling, "census-ruling-") || !censusName.MatchString(strings.TrimPrefix(ruling, "census-ruling-")) {
+ return false
+ }
+ return rest == censusLeftMark || strings.HasPrefix(rest, censusLeftMark+";")
+}
+
// censusAbsentMark follows the ruling in the basis of an @removed row
// census-propose writes: the declaration must stay out of the tree
// (rootReadAbsentRemoval).
diff --git a/internal/provision/census_scope_fixture_test.go b/internal/provision/census_scope_fixture_test.go
index 9dbdc67e1..86f3b15d8 100644
--- a/internal/provision/census_scope_fixture_test.go
+++ b/internal/provision/census_scope_fixture_test.go
@@ -5,6 +5,8 @@ package provision
// reference graph.
import (
+ "os"
+ "path/filepath"
"strings"
"testing"
)
@@ -220,3 +222,172 @@ func TestCensusScopeUnknownRootVerbFails(t *testing.T) {
t.Fatalf("err = %v, want the missing system verb named", err)
}
}
+
+func scopedTree(t *testing.T, root string) rootReadScan {
+ t.Helper()
+ got, err := rootReadScoped(root, censusGraph)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return got
+}
+
+// editFile replaces old with new in one file of a fixture tree.
+func editFile(t *testing.T, root, file, old, new string) {
+ t.Helper()
+ path := filepath.Join(root, filepath.FromSlash(file))
+ raw, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(raw), old) {
+ t.Fatalf("%s has no %q", file, old)
+ }
+ if err := os.WriteFile(path, []byte(strings.Replace(string(raw), old, new, 1)), 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestCensusScopedScan(t *testing.T) {
+ got := scopedTree(t, scopeTree(t))
+ for _, k := range []string{
+ "internal/host/host.go:readConfig", "internal/host/host.go:bootValue.String",
+ "internal/initpkg/init.go:initRead", "internal/initpkg/init.go:varRead",
+ "internal/host/units/carlos-edge.service:external", "internal/host/units/carlos-adopt.service:external",
+ "internal/host/run.sh:external", "infra/modules/box/main.tf:external",
+ } {
+ if !rootReadScanned(got, k) {
+ t.Errorf("%s not scanned", k)
+ }
+ }
+ for _, k := range []string{
+ "internal/host/host.go:agentRead", "internal/host/host.go:RunStatus", "internal/helper/helper.go:Load",
+ "internal/console/console.go:consoleRead", "internal/console/templates/page.html:external", "scripts/tool.sh:external",
+ } {
+ if rootReadScanned(got, k) {
+ t.Errorf("%s scanned, but root does not run it", k)
+ }
+ }
+ if !got.Outside["internal/host/host.go:agentRead"] || got.Outside["internal/host/host.go:readConfig"] {
+ t.Errorf("outside: %v", got.Outside)
+ }
+ var dirs []string
+ for d := range got.Coverage {
+ dirs = append(dirs, d)
+ }
+ if joined := strings.Join(sortedStrings(dirs), " "); joined != ". infra/modules/box internal/host internal/host/units internal/initpkg" {
+ t.Errorf("envelopes: %s", joined)
+ }
+}
+
+// An envelope hashes the reached declarations of its directory: an edit to
+// agent code beside root code leaves it, an edit to a reached helper with
+// no reads moves it.
+func TestCensusScopedEnvelope(t *testing.T) {
+ root := scopeTree(t)
+ before := scopedTree(t, root).Coverage["internal/host"]
+ editFile(t, root, "internal/host/host.go", `os.ReadFile("/data/agent")`, `os.ReadFile("/data/agent2")`)
+ if after := scopedTree(t, root).Coverage["internal/host"]; after != before {
+ t.Fatal("an agent-only edit moved the root envelope")
+ }
+ editFile(t, root, "internal/host/host.go", "func RunApply() { _ = units }", "func RunApply() { _ = units; _ = 1 }")
+ if after := scopedTree(t, root).Coverage["internal/host"]; after == before {
+ t.Fatal("an edit to a reached helper left the envelope as it was")
+ }
+}
+
+const readConfigUnit = "internal/host/host.go:readConfig"
+
+// A unit root stops reaching, still declared, has left the census's scope:
+// an approval, written like a removal, that holds while it stays out.
+func TestCensusLeftUnitIsAnApproval(t *testing.T) {
+ root := scopeTree(t)
+ inv, seal := inventoryOf(scopedTree(t, root), readConfigUnit)
+ editFile(t, root, "internal/host/host.go", "func RunAdopt() { readConfig() }", "func RunAdopt() {}")
+ got := scopedTree(t, root)
+ classes := classesOf(classifyRootReads(got, inv, seal))
+ if classes[readConfigUnit] != censusLeft || classes["internal/host"] != censusEnvelope {
+ t.Fatalf("classes %v", classes)
+ }
+ if censusDecision(censusLeft) {
+ t.Fatal("left must be an approval")
+ }
+ p, err := proposeRootReads(got, inv, seal, "drop-config", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(p.Overlay, "@removed\t"+strings.Replace(readConfigUnit, ":", "\t", 1)+"\tH3\tcensus-ruling-drop-config;left;basis=") {
+ t.Fatalf("overlay:\n%s", p.Overlay)
+ }
+ if !strings.Contains(p.Card, "Left the census's scope") {
+ t.Fatalf("card:\n%s", p.Card)
+ }
+ after := cloneRootReadInventory(inv)
+ if err := loadRootReadInventory(writeOverlay(t, p.Overlay), &after, true); err != nil {
+ t.Fatal(err)
+ }
+ if errs := censusDiscrepancies(got, after, p.Seal); len(errs) != 0 || p.Seal != seal {
+ t.Fatalf("after the overlay: %v", errs)
+ }
+
+ // Back in scope with no row: a new unit, a decision.
+ editFile(t, root, "internal/host/host.go", "func RunAdopt() {}", "func RunAdopt() { readConfig() }")
+ back := scopedTree(t, root)
+ if classes := classesOf(classifyRootReads(back, after, p.Seal)); classes[readConfigUnit] != censusNew {
+ t.Fatalf("back in scope: %v", classes)
+ }
+ // A hand row re-admits it: unchanged, the row alone, its freeze holds.
+ row := inv.Entries[readConfigUnit]
+ readmit := strings.Join([]string{row.File, row.Unit, row.Class, row.Task, row.Basis, row.Kinds, row.Digest}, "\t") + "\n@coverage\tinternal/host\t" + back.Coverage["internal/host"] + "\n"
+ again := cloneRootReadInventory(after)
+ if err := loadRootReadInventory(writeOverlay(t, readmit), &again, true); err != nil {
+ t.Fatal(err)
+ }
+ if errs := censusDiscrepancies(back, again, p.Seal); len(errs) != 0 {
+ t.Fatalf("re-admitted: %v", errs)
+ }
+}
+
+// A directory root stops reaching, still in the tree: its envelope leaves
+// with "@coverage <dir> left", and coming back is an unpinned envelope.
+func TestCensusLeftEnvelope(t *testing.T) {
+ root := scopeTree(t)
+ inv, seal := inventoryOf(scopedTree(t, root))
+ editFile(t, root, "internal/host/host.go", "\t_ \"fixture/root/internal/initpkg\"\n", "")
+ got := scopedTree(t, root)
+ classes := classesOf(classifyRootReads(got, inv, seal))
+ for _, k := range []string{"internal/initpkg", "internal/initpkg/init.go:initRead", "internal/initpkg/init.go:varRead"} {
+ if classes[k] != censusLeft {
+ t.Fatalf("class of %s = %q (all %v)", k, classes[k], classes)
+ }
+ }
+ p, err := proposeRootReads(got, inv, seal, "drop-init", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(p.Overlay, "@coverage\tinternal/initpkg\tleft\n") {
+ t.Fatalf("overlay:\n%s", p.Overlay)
+ }
+ after := cloneRootReadInventory(inv)
+ if err := loadRootReadInventory(writeOverlay(t, p.Overlay), &after, true); err != nil {
+ t.Fatal(err)
+ }
+ if errs := censusDiscrepancies(got, after, p.Seal); len(errs) != 0 {
+ t.Fatalf("after the overlay: %v", errs)
+ }
+ editFile(t, root, "internal/host/host.go", "\t\"fixture/root/internal/helper\"\n", "\t\"fixture/root/internal/helper\"\n\t_ \"fixture/root/internal/initpkg\"\n")
+ if errs := censusDiscrepancies(scopedTree(t, root), after, p.Seal); !containsPrefix(errs, "changed root source envelope internal/initpkg") {
+ t.Fatalf("back in scope: %v", errs)
+ }
+ for name, body := range map[string]string{"in the base": "@coverage\tinternal/initpkg\tleft\n", "unknown": "@coverage\tinternal/never\tleft\n"} {
+ inv := inventoryOfCoverage(got)
+ if err := loadRootReadInventory(writeOverlay(t, body), &inv, name != "in the base"); err == nil {
+ t.Errorf("%s: loaded", name)
+ }
+ }
+}
+
+func inventoryOfCoverage(scan rootReadScan) rootReadInventory {
+ inv, _ := inventoryOf(scan)
+ return inv
+}
diff --git a/internal/provision/census_scope_test.go b/internal/provision/census_scope_test.go
index 81b08ee47..413b47915 100644
--- a/internal/provision/census_scope_test.go
+++ b/internal/provision/census_scope_test.go
@@ -8,6 +8,8 @@ package provision
// entry points instead of main.go:main.
import (
+ "crypto/sha256"
+ "encoding/hex"
"fmt"
"go/ast"
"go/token"
@@ -501,3 +503,93 @@ func sortedStrings(s []string) []string {
sort.Strings(out)
return out
}
+
+// rootReadScoped scans the tree at root and narrows the scan to what root
+// runs. load builds the linux graph: censusGraphShared for the repository,
+// censusGraph for a fixture tree a test edits.
+func rootReadScoped(root string, load func(root, platform string) (*refGraph, error)) (rootReadScan, error) {
+ raw, err := rootReadProduction(root)
+ if err != nil {
+ return raw, err
+ }
+ g, err := load(root, "linux")
+ if err != nil {
+ return raw, err
+ }
+ s, err := rootScopeFrom(root, g)
+ if err != nil {
+ return raw, err
+ }
+ return rootReadScope(raw, s)
+}
+
+// rootReadScope narrows a scan of the whole tree to the census's scope. A
+// Go unit is in scope when root reaches its declaration. A non-Go file is
+// when a reached declaration embeds it, when it is under infra/ (user_data
+// and the units it installs run as root at boot, and its IAM decides who
+// can write what root reads), or when it sits beside a reached declaration.
+// An envelope is one per directory holding anything in scope, over the
+// reached declarations (doc comments included) and in-scope files there;
+// "." adds go.mod and go.sum, which pick the code root links.
+func rootReadScope(raw rootReadScan, s rootScope) (rootReadScan, error) {
+ out := raw
+ out.Entries = nil
+ out.Declarations = map[string]string{}
+ out.Outside = map[string]bool{}
+ out.Coverage = map[string]string{}
+ goDirs := map[string]bool{}
+ for key := range s.Reached {
+ file, unit, _ := strings.Cut(key, ":")
+ if unit == "external" || !strings.HasSuffix(file, ".go") {
+ continue
+ }
+ if _, ok := raw.Sources[key]; !ok {
+ return out, fmt.Errorf("census scope: root reaches %s, which the scan does not declare", key)
+ }
+ goDirs[filepath.ToSlash(filepath.Dir(file))] = true
+ }
+ external := func(path string) bool {
+ _, embedded := s.Reached[path+":external"]
+ return embedded || strings.HasPrefix(path, "infra/") || goDirs[filepath.ToSlash(filepath.Dir(path))]
+ }
+ inScope := func(key string) bool {
+ file, unit, _ := strings.Cut(key, ":")
+ if unit == "external" {
+ return external(file)
+ }
+ _, ok := s.Reached[key]
+ return ok
+ }
+ for _, e := range raw.Entries {
+ if inScope(e.File + ":" + e.Unit) {
+ out.Entries = append(out.Entries, e)
+ }
+ }
+ for key, digest := range raw.Declarations {
+ if inScope(key) {
+ out.Declarations[key] = digest
+ } else {
+ out.Outside[key] = true
+ }
+ }
+ rows := map[string][]string{}
+ for key, digest := range raw.Sources {
+ if inScope(key) {
+ file, _, _ := strings.Cut(key, ":")
+ dir := filepath.ToSlash(filepath.Dir(file))
+ rows[dir] = append(rows[dir], key+"\x00"+digest)
+ }
+ }
+ for path, digest := range raw.Externals {
+ dir := filepath.ToSlash(filepath.Dir(path))
+ if (path == "go.mod" || path == "go.sum") && goDirs["."] || path != "go.mod" && path != "go.sum" && external(path) {
+ rows[dir] = append(rows[dir], path+"\x00"+digest)
+ }
+ }
+ for dir, lines := range rows {
+ sort.Strings(lines)
+ sum := sha256.Sum256([]byte(strings.Join(lines, "\n")))
+ out.Coverage[dir] = hex.EncodeToString(sum[:])
+ }
+ return out, nil
+}
diff --git a/internal/provision/plan14_root_reads_test.go b/internal/provision/plan14_root_reads_test.go
index de6120c83..81b70603f 100644
--- a/internal/provision/plan14_root_reads_test.go
+++ b/internal/provision/plan14_root_reads_test.go
@@ -34,6 +34,15 @@ type rootReadScan struct {
Dirs map[string]bool
Files map[string]bool
Root string
+ // Outside is every declaration in the tree that root does not run, in
+ // a scan narrowed to the census's scope (rootReadScope); nil in a scan
+ // of the whole tree.
+ Outside map[string]bool
+ // Sources is each Go declaration's digest with its doc comment, and
+ // Externals each non-Go file's (go.mod and go.sum too), for the scoped
+ // envelopes (rootReadScope).
+ Sources map[string]string
+ Externals map[string]string
}
// absent reports whether the scan proves a path (slash-relative) gone: not
@@ -206,7 +215,7 @@ func rootReadTyped(root string) (map[string]string, map[string]bool, error) {
}
func rootReadProduction(root string) (rootReadScan, error) {
- out := rootReadScan{Coverage: map[string]string{}, Declarations: map[string]string{}, Dirs: map[string]bool{}, Files: map[string]bool{}, Root: root}
+ out := rootReadScan{Coverage: map[string]string{}, Declarations: map[string]string{}, Dirs: map[string]bool{}, Files: map[string]bool{}, Root: root, Sources: map[string]string{}, Externals: map[string]string{}}
typed, active, err := rootReadTyped(root)
if err != nil {
return out, err
@@ -380,6 +389,12 @@ func rootReadProduction(root string) (rootReadScan, error) {
})
sum := sha256.Sum256(src[start:end])
out.Declarations[p+":"+name] = hex.EncodeToString(sum[:])
+ from := start
+ if doc := rootReadDeclDoc(decl); doc != nil {
+ from = fset.Position(doc.Pos()).Offset
+ }
+ whole := sha256.Sum256(src[from:end])
+ out.Sources[p+":"+name] = hex.EncodeToString(whole[:])
if len(kinds) == 0 {
continue
}
@@ -395,6 +410,7 @@ func rootReadProduction(root string) (rootReadScan, error) {
sum := sha256.Sum256(body)
dir := filepath.ToSlash(filepath.Dir(p))
coverage[dir] = append(coverage[dir], p+"\x00"+hex.EncodeToString(sum[:]))
+ out.Externals[p] = hex.EncodeToString(sum[:])
// A shell command or unit can read data via a variable. Review the
// complete file as authority, rather than matching only literal /data.
if p != "go.mod" && p != "go.sum" {
@@ -450,6 +466,17 @@ func rootReadDeclName(decl ast.Decl, grouped map[string]int) (string, bool) {
return name, true
}
+// rootReadDeclDoc is a declaration's doc comment, or nil.
+func rootReadDeclDoc(decl ast.Decl) *ast.CommentGroup {
+ switch d := decl.(type) {
+ case *ast.FuncDecl:
+ return d.Doc
+ case *ast.GenDecl:
+ return d.Doc
+ }
+ return nil
+}
+
type classifiedRootRead struct {
rootReadEntry
Class, Task, Basis string
@@ -532,6 +559,18 @@ func loadRootReadInventory(path string, inv *rootReadInventory, delta bool) erro
return fmt.Errorf("%s:%d: coverage", path, i+1)
}
key = "@coverage:" + col[1]
+ if col[2] == censusLeftMark {
+ // The directory left the census's scope (C2): its envelope
+ // goes; back in scope it is an envelope nobody pinned.
+ if !delta {
+ return fmt.Errorf("%s:%d: an envelope leaves scope only by an overlay", path, i+1)
+ }
+ if _, ok := inv.Coverage[col[1]]; !ok {
+ return fmt.Errorf("%s:%d: unknown envelope %s left scope", path, i+1, col[1])
+ }
+ delete(inv.Coverage, col[1])
+ break
+ }
if col[2] == "-" {
// A retired envelope (claims cleanup R0): the directory is
// gone, which compareRootReads checks against the scan.
@@ -1748,13 +1787,17 @@ func checkRootReadDeclarations(scan rootReadScan, inv rootReadInventory) []strin
digest, exists := scan.Declarations[key]
current, classified := inv.Entries[key]
removal := inv.Removed[key]
- if removal != "" {
+ // A unit that left scope (C2) is re-admitted by a later row that
+ // classifies it again; it is then checked as any frozen unit is.
+ if removal != "" && !(classified && rootReadLeftRemoval(removal)) {
// Census ruling zddr3: an H3 census-ruling removal may retire a
// unit that is still declared only when this scan finds no read in
// it. The basis text is never evidence; the scan is.
// A removal census-propose wrote for a deleted unit holds only
// while the declaration stays gone (claims cleanup R0).
- retired := exists && strings.HasPrefix(removal, "H3;census-ruling-") && !rootReadScanned(scan, key) && !rootReadAbsentRemoval(removal)
+ // A removal written for a unit that left scope holds only while
+ // it stays out of scope.
+ retired := exists && strings.HasPrefix(removal, "H3;census-ruling-") && !rootReadScanned(scan, key) && !rootReadAbsentRemoval(removal) && !rootReadLeftRemoval(removal)
if (exists && !retired) || classified {
errs = append(errs, "removed legacy declaration still present "+key)
}