diff --git a/reviewer/CONTROLLER.md b/reviewer/CONTROLLER.md index 2d68b236..b39fcd40 100644 --- a/reviewer/CONTROLLER.md +++ b/reviewer/CONTROLLER.md @@ -7,7 +7,10 @@ COMMENT/ACK writes and whole-document reconciliation. The [native discovery reader](GITHUB-DISCOVERY.md) collects request/removal provenance, owner commands and live base revisions with an explicitly supplied read transport. It commits complete discovery before returning Current so cancellation reaches the local -dispatch fence. Actual App consistency remains unqualified. There is no polling +dispatch fence. [Snapshot evidence](SNAPSHOT-EVIDENCE.md) collects a bounded +merge-base diff from trusted immutable Git objects, verifies inline anchors +against their blobs and retains authoritative omissions through review/recovery. +Actual App consistency remains unqualified. There is no polling daemon, production launcher, credential acquisition, installation flow or activation command. The retired SFL runtime remains frozen. Live acceptance is tracked in [#173](https://github.com/hemsoft-dev/set-it-free-loop/issues/173), [#174](https://github.com/hemsoft-dev/set-it-free-loop/issues/174) and diff --git a/reviewer/README.md b/reviewer/README.md index 935e5710..9f81d163 100644 --- a/reviewer/README.md +++ b/reviewer/README.md @@ -61,6 +61,11 @@ limitations, never evidence of clean code. Candidate hooks and instructions are inert data. The reader must run as a separate identity outside the provider worker, without provider/GitHub credentials. +[Snapshot evidence](SNAPSHOT-EVIDENCE.md) builds the trusted changed-path packet +and diff-line map without checkout or fetching. It binds both to the exact +SQLite-admitted packet and carries controller-observed omissions into reader +coverage, fake review, recovery and rendering. + `AuthStore` on Linux/macOS serializes trusted local state updates and per-phase profile leases with a cross-process lock. A durable profile journal survives controller loss, blocks stale seed reuse and commits returned credentials only diff --git a/reviewer/SNAPSHOT-EVIDENCE.md b/reviewer/SNAPSHOT-EVIDENCE.md new file mode 100644 index 00000000..a2e887a4 --- /dev/null +++ b/reviewer/SNAPSHOT-EVIDENCE.md @@ -0,0 +1,93 @@ +# Immutable snapshot evidence + +`CaptureSnapshotEvidence` prepares a bounded evidence packet from an explicitly +supplied trusted `GitStore` and admitted snapshot. It reads committed objects; +it never checks out candidate code, fetches history, loads a provider profile or +starts a reviewer. Repository and fork provenance still come from qualified +discovery and object acquisition. A local object with a matching hash does not +attest installation permissions or which repository supplied it. + +Capture requires all three revisions to be commits and the captured merge base +to be the unique merge base of the base tip and head. Missing or ambiguous +history defers the entire capture. The comparison is merge-base to head, even +when an independently advanced base tip has the same tree as head. + +Git produces the NUL-delimited raw inventory with full object IDs and renames +disabled. A rename appears as deletion and addition. Modes, status, object IDs, +paths and cardinality are validated. Unsafe paths or incomplete inventories +defer capture. Both SHA-1 and SHA-256 object formats are supported. Packet data +includes every changed path, mode, old/new object identity and status within the +128-file cap. No truncated inventory is admitted. + +Regular text blobs and patches share finite bounds. Symlinks, submodules, +binaries, non-UTF-8 objects, unavailable blobs and source/patch size omissions +retain explicit path-specific coverage. Missing commits are not treated as +ordinary blob omissions. There is a two-MiB aggregate blob read budget, a two-MiB +per-command output cap, a 64-KiB JSON packet cap and a one-minute overall capture +deadline. A complete inventory and its coverage must fit; otherwise capture +fails. Patch omission can leave room for later small files. An omitted patch +never supplies inline anchors. + +Fixed Git flags disable external diff drivers, text conversion, rename +detection, color, replacement objects and lazy fetch. `--ignore-submodules=none` overrides candidate `.gitmodules` and repository ignore settings. Git receives no inherited +user environment or global/system configuration. The explicitly selected Git +binary and object repository configuration remain trusted. Their provisioning +must stay outside candidate source. No shell is used by capture. + +Each hunk's context, additions, deletions, counts, coordinates, unchanged gaps +and EOF newline markers are checked against the actual old/new blobs. Metadata +and function labels do not select paths or lines. Only verified right-side +context/addition lines become inline anchors. Source-valid findings elsewhere +remain summary findings under the existing renderer. + +## Bind the admitted packet + +Use the job returned by `Admit`, including on an idempotent retry: + +```go +capture, err := CaptureSnapshotEvidence(ctx, trustedGit, snapshot) +// Handle err before admission. +job, err := store.Admit(ctx, scope, requestID, release, capture.Packet(), now) +// Handle err before binding. +bound, err := capture.Bind(job) +// Handle err before providing a reader or publication anchors. +engine.Reader = bound.Reader +// After stopped-worker execution: +intent, err := publisher.PrepareEvidence(ctx, outcome, bound) +``` + +`Bind` requires the exact admitted snapshot and packet plus a valid job. If a +retry returns a previously stored packet and a fresh capture differs, binding +fails. Recovery must reproduce the original captured packet or retain explicit +uncertainty; it cannot silently replace its omissions or source input. A bound +reader uses the job's configured limits and rejects a different snapshot. +Paths and coverage accessors return copies. Inline maps stay private; use `bound.Render` or `Publisher.PrepareEvidence` so publication revalidates the complete admitted job hash. The low-level map-taking renderer/publisher remain transport test seams and reject captured snapshot packets. + +Supply an independently budgeted bound reader to each adapter as well as the +engine's citation-verification factory. Search skips unreadable paths with recorded coverage and still stops on cancellation or budget exhaustion. Source receipts combine unique controller-owned reasons per allowed path, reserving space for global denial/exhaustion facts. Unknown attempted paths are sanitized rather than stored as source inventory. Initial controller coverage is retained +when the in-process source broker forks the adapter's reader. It consumes no +model read budget. Citation validation and the engine's existing authoritative +coverage merge preserve these omissions even if the model reports none. The +managed source role still requires its separate identity/containment boundary; +this collector does not qualify that boundary. + +## Offline evidence and remaining runtime work + +Tests create real commits and exercise the installed Git producer, including +both object formats, quoted/Unicode paths, deletion, no-newline text, renames, +symlinks, submodules and binaries. Working-tree changes, hooks, configured +external/textconv drivers and inherited Git environment canaries cannot alter +the packet or execute during capture. A local inert promisor trap first proves +that missing objects would trigger fetching without the controls, then proves +capture records an omission without invoking that trap. No network is used. + +Hostile parser fixtures reject forged source, coordinates, counts, hidden edits, +missing newline markers, duplicate paths and partial inventories. The durable +integration test admits this actual Git packet into SQLite, runs four fake +provider phases, recovers completed work without another provider call and +renders authoritative omissions despite empty model coverage declarations. + +This is a library prerequisite for #176. Private polling/backoff, qualified +object acquisition, token attestation, runtime composition and packaging remain +required. It does not install an App, enable Actions, activate the retired +controller or qualify live subscription use. diff --git a/reviewer/publication.go b/reviewer/publication.go index 413808b8..72b63685 100644 --- a/reviewer/publication.go +++ b/reviewer/publication.go @@ -102,7 +102,13 @@ func redactDocument(doc Document, secrets []string) Document { return doc } func (p Publisher) Prepare(ctx context.Context, out Outcome, diff map[string]map[int]bool) (WriteIntent, error) { - doc, err := Render(out, diff) + if snapshotEvidencePacket(out.Job.Packet) { + return WriteIntent{}, ErrFenced + } + return p.prepare(ctx, out, diff) +} +func (p Publisher) prepare(ctx context.Context, out Outcome, diff map[string]map[int]bool) (WriteIntent, error) { + doc, err := renderOutcome(out, diff) if err != nil { return WriteIntent{}, err } diff --git a/reviewer/reader.go b/reviewer/reader.go index d9e321f0..56adf2c4 100644 --- a/reviewer/reader.go +++ b/reviewer/reader.go @@ -30,14 +30,15 @@ type ReadRecord struct { } type Reader struct { - mu sync.Mutex - store ObjectStore - snapshot Snapshot - paths map[string]bool - limits Limits - calls, used int - reads []ReadRecord - coverage []Coverage + mu sync.Mutex + store ObjectStore + snapshot Snapshot + paths map[string]bool + limits Limits + calls, used int + reads []ReadRecord + coverage []Coverage + initialCoverage []Coverage } func validPath(p string) bool { @@ -80,7 +81,7 @@ func (r *Reader) Read(ctx context.Context, revision, p string) (string, error) { defer r.mu.Unlock() fail := func(reason string) (string, error) { loggedPath := p - if !validPath(loggedPath) { + if !validPath(loggedPath) || !r.paths[loggedPath] { loggedPath = "" } if len(r.coverage) < 256 { @@ -121,9 +122,15 @@ func (r *Reader) Read(ctx context.Context, revision, p string) (string, error) { } func (r *Reader) Search(ctx context.Context, revision, needle string, maxResults int) ([]Anchor, error) { + if err := ctx.Err(); err != nil { + return nil, err + } if needle == "" || len(needle) > 256 || strings.ContainsAny(needle, "\r\n") || maxResults < 1 || maxResults > 100 { return nil, errors.New("invalid bounded search") } + if revision != r.snapshot.Head && revision != r.snapshot.Base && revision != r.snapshot.MergeBase { + return nil, errors.New("revision outside job snapshot") + } paths := make([]string, 0, len(r.paths)) for p := range r.paths { paths = append(paths, p) @@ -133,7 +140,18 @@ func (r *Reader) Search(ctx context.Context, revision, needle string, maxResults for _, p := range paths { text, err := r.Read(ctx, revision, p) if err != nil { - return matches, err + if ctx.Err() != nil { + return matches, ctx.Err() + } + r.mu.Lock() + exhausted := r.calls >= r.limits.ReaderCalls || r.used >= r.limits.ReaderBytes + r.mu.Unlock() + if exhausted { + return matches, err + } + // Unreadable material retains its authoritative omission. One binary, + // deleted or oversized path must not disable later text matches. + continue } for i, line := range strings.Split(text, "\n") { if strings.Contains(line, needle) { diff --git a/reviewer/render.go b/reviewer/render.go index b8a1a9ee..64339e64 100644 --- a/reviewer/render.go +++ b/reviewer/render.go @@ -50,6 +50,13 @@ func sourceURL(snapshot Snapshot, a Anchor) string { // findings outside the diff stay in the summary. Incomplete jobs never expose // substantive partial findings, even if prior phases succeeded. func Render(out Outcome, diffLines map[string]map[int]bool) (Document, error) { + if snapshotEvidencePacket(out.Job.Packet) { + return Document{}, ErrFenced + } + return renderOutcome(out, diffLines) +} + +func renderOutcome(out Outcome, diffLines map[string]map[int]bool) (Document, error) { doc := Document{Inline: []InlineComment{}} if err := out.Job.Validate(); err != nil { return doc, err diff --git a/reviewer/snapshot_evidence.go b/reviewer/snapshot_evidence.go new file mode 100644 index 00000000..9deeda02 --- /dev/null +++ b/reviewer/snapshot_evidence.go @@ -0,0 +1,364 @@ +package reviewer + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "regexp" + "strconv" + "strings" + "time" + "unicode/utf8" +) + +const snapshotPacketBytes = 64 << 10 +const snapshotFileLimit = 128 +const snapshotObjectBytes = 2 << 20 + +// SnapshotEvidence is captured by the trusted source reader, never supplied by +// a model. Its methods return copies; packet, reader paths and inline anchors +// all describe the same immutable merge-base-to-head comparison. +type SnapshotEvidence struct { + git GitStore + snapshot Snapshot + packet string + paths []string + coverage []Coverage + lines map[string]map[int]bool +} + +// JobEvidence binds reader coverage and diff anchors to the packet actually +// admitted by SQLite. An idempotent Admit can return an older captured packet; +// a recapture with different omissions must not silently replace its evidence. +type JobEvidence struct { + evidence *SnapshotEvidence + limits Limits + jobHash string +} + +func (e *SnapshotEvidence) Bind(job Job) (*JobEvidence, error) { + if e == nil || job.Snapshot != e.snapshot || job.Packet != e.packet || job.Validate() != nil { + return nil, ErrFenced + } + return &JobEvidence{evidence: e, limits: job.Config.Limits, jobHash: Digest(job)}, nil +} + +func (bound *JobEvidence) validateJob(job Job) error { + if bound == nil || bound.evidence == nil || job.Validate() != nil || bound.jobHash != Digest(job) { + return ErrFenced + } + return nil +} + +func snapshotEvidencePacket(packet string) bool { + var header struct{ Version string } + return json.Unmarshal([]byte(packet), &header) == nil && header.Version == "sfl-snapshot-evidence-1" +} + +// Render retains the admitted-job binding through inline eligibility checks. +func (bound *JobEvidence) Render(out Outcome) (Document, error) { + if err := bound.validateJob(out.Job); err != nil { + return Document{}, err + } + return renderOutcome(out, bound.diffLines()) +} + +func (p Publisher) PrepareEvidence(ctx context.Context, out Outcome, bound *JobEvidence) (WriteIntent, error) { + if err := bound.validateJob(out.Job); err != nil { + return WriteIntent{}, err + } + return p.prepare(ctx, out, bound.diffLines()) +} + +type snapshotFile struct { + Path, Status, OldMode, NewMode, OldObject, NewObject string + Patch string `json:",omitempty"` +} + +type snapshotPacket struct { + Version string + Snapshot Snapshot + Files []snapshotFile + Coverage []Coverage +} + +func (e *SnapshotEvidence) Packet() string { return e.packet } +func (e *SnapshotEvidence) Paths() []string { return append([]string(nil), e.paths...) } +func (e *SnapshotEvidence) Coverage() []Coverage { + return append([]Coverage(nil), e.coverage...) +} +func (bound *JobEvidence) diffLines() map[string]map[int]bool { + e := bound.evidence + out := map[string]map[int]bool{} + for path, lines := range e.lines { + out[path] = map[int]bool{} + for line := range lines { + out[path][line] = true + } + } + return out +} + +// Reader supplies independent budgets while retaining controller-observed +// omissions. It does not grant source access outside this captured path list. +func (bound *JobEvidence) Reader(snapshot Snapshot) (*Reader, error) { + if bound == nil || bound.evidence == nil { + return nil, ErrFenced + } + e := bound.evidence + if snapshot != e.snapshot { + return nil, ErrFenced + } + r, err := NewReader(e.git, snapshot, e.paths, bound.limits) + if err != nil { + return nil, err + } + r.initialCoverage = e.Coverage() + r.coverage = e.Coverage() + return r, nil +} + +// CaptureSnapshotEvidence runs only read-only Git plumbing against an explicitly +// supplied trusted object store. Missing history defers the whole capture. It +// never fetches, checks out a tree, reads the working tree or executes its code. +// Whole changed-path inventories must fit; individual source/patch omissions +// remain explicit. No partially collected packet is returned on failure. +func CaptureSnapshotEvidence(ctx context.Context, git GitStore, snapshot Snapshot) (*SnapshotEvidence, error) { + if snapshot.Validate() != nil { + return nil, ErrIgnored + } + ctx, cancel := context.WithTimeout(ctx, time.Minute) + defer cancel() + for _, id := range []string{snapshot.Base, snapshot.Head, snapshot.MergeBase} { + kind, err := git.command(ctx, 32, "cat-file", "-t", id) + if err != nil || string(kind) != "commit\n" { + return nil, errors.New("snapshot commit history unavailable") + } + } + ancestor, err := git.command(ctx, 256, "merge-base", "--all", snapshot.Base, snapshot.Head) + if err != nil || string(ancestor) != snapshot.MergeBase+"\n" { + return nil, errors.New("snapshot merge-base history unavailable or ambiguous") + } + args := []string{"diff", "--raw", "-z", "--no-abbrev", "--no-renames", "--ignore-submodules=none", "--no-ext-diff", "--no-textconv", "--no-color", snapshot.MergeBase, snapshot.Head, "--"} + raw, err := git.command(ctx, snapshotObjectBytes, args...) + if err != nil { + return nil, errors.New("complete changed-path inventory unavailable") + } + files, err := parseSnapshotChanges(raw, len(snapshot.Head)) + if err != nil { + return nil, err + } + e := &SnapshotEvidence{git: git, snapshot: snapshot, lines: map[string]map[int]bool{}} + p := snapshotPacket{Version: "sfl-snapshot-evidence-1", Snapshot: snapshot, Files: files, Coverage: []Coverage{}} + remaining := snapshotObjectBytes + for i := range p.Files { + if err := ctx.Err(); err != nil { + return nil, err + } + file := &p.Files[i] + e.paths = append(e.paths, file.Path) + omit := func(reason string) { p.Coverage = append(p.Coverage, Coverage{file.Path, reason}) } + if (file.OldMode != "000000" && file.OldMode != "100644" && file.OldMode != "100755") || (file.NewMode != "000000" && file.NewMode != "100644" && file.NewMode != "100755") { + omit("symlink, submodule or non-file diff omitted") + continue + } + old, err := snapshotBlob(ctx, git, file.OldObject, &remaining) + if err != nil { + omit("old source unavailable or source byte limit exhausted") + continue + } + new, err := snapshotBlob(ctx, git, file.NewObject, &remaining) + if err != nil { + omit("new source unavailable or source byte limit exhausted") + continue + } + if bytes.IndexByte(old, 0) >= 0 || bytes.IndexByte(new, 0) >= 0 || !utf8.Valid(old) || !utf8.Valid(new) { + omit("binary or non-UTF-8 diff omitted") + continue + } + patch, err := git.command(ctx, snapshotObjectBytes, "-c", "diff.suppressBlankEmpty=false", "diff", "--no-ext-diff", "--no-textconv", "--no-renames", "--ignore-submodules=none", "--no-color", "--no-prefix", "--text", "--diff-algorithm=myers", "--no-indent-heuristic", "--unified=3", snapshot.MergeBase, snapshot.Head, "--", ":(literal)"+file.Path) + if err != nil { + omit("patch unavailable or patch byte limit exhausted") + continue + } + lines, err := snapshotHunkLines(string(patch), string(old), string(new)) + if err != nil { + return nil, errors.New("patch does not reproduce immutable source") + } + file.Patch = string(patch) + encoded, _ := json.Marshal(p) + if len(encoded) > snapshotPacketBytes { + file.Patch = "" + omit("patch omitted by evidence packet byte limit") + continue + } + e.lines[file.Path] = lines + } + encoded, err := json.Marshal(p) + if err != nil || len(encoded) > snapshotPacketBytes { + return nil, errors.New("complete evidence inventory and coverage exceed packet limit") + } + if err := ctx.Err(); err != nil { + return nil, err + } + e.packet = string(encoded) + e.coverage = append([]Coverage(nil), p.Coverage...) + return e, nil +} + +func snapshotBlob(ctx context.Context, git GitStore, id string, remaining *int) ([]byte, error) { + if strings.Trim(id, "0") == "" { + return nil, nil + } + if *remaining <= 0 { + return nil, ErrDeferred + } + // Inspect size first so an oversized blob cannot repeatedly consume the + // capture's aggregate read budget through capped failed cat-file calls. + raw, err := git.command(ctx, 32, "cat-file", "-s", id) + size, parseErr := strconv.Atoi(strings.TrimSpace(string(raw))) + if err != nil || parseErr != nil || size < 0 || size > *remaining { + return nil, ErrDeferred + } + *remaining -= size + data, err := git.command(ctx, max(1, size), "cat-file", "blob", id) + if err != nil || len(data) != size { + return nil, ErrDeferred + } + return data, nil +} + +func parseSnapshotChanges(raw []byte, hashLength int) ([]snapshotFile, error) { + files := []snapshotFile{} + if len(raw) == 0 { + return files, nil + } + if raw[len(raw)-1] != 0 { + return nil, ErrDeferred + } + parts := bytes.Split(raw[:len(raw)-1], []byte{0}) + if len(parts)%2 != 0 || len(parts)/2 > snapshotFileLimit { + return nil, errors.New("changed-path inventory is malformed or exceeds file limit") + } + seen := map[string]bool{} + for i := 0; i < len(parts); i += 2 { + fields := strings.Split(string(parts[i]), " ") + path := string(parts[i+1]) + if len(fields) != 5 || !strings.HasPrefix(fields[0], ":") || !validPath(path) || seen[path] { + return nil, errors.New("changed-path inventory contains unsafe or duplicate entries") + } + f := snapshotFile{Path: path, OldMode: strings.TrimPrefix(fields[0], ":"), NewMode: fields[1], OldObject: fields[2], NewObject: fields[3], Status: fields[4]} + validMode := func(s string) bool { + return s == "000000" || s == "100644" || s == "100755" || s == "120000" || s == "160000" + } + oldZero, newZero := strings.Trim(f.OldObject, "0") == "", strings.Trim(f.NewObject, "0") == "" + if !validMode(f.OldMode) || !validMode(f.NewMode) || !objectID.MatchString(f.OldObject) || !objectID.MatchString(f.NewObject) || len(f.OldObject) != hashLength || len(f.NewObject) != hashLength || oldZero != (f.OldMode == "000000") || newZero != (f.NewMode == "000000") { + return nil, ErrDeferred + } + if (f.Status == "A" && (!oldZero || newZero)) || (f.Status == "D" && (oldZero || !newZero)) || ((f.Status == "M" || f.Status == "T") && (oldZero || newZero)) || (f.Status != "A" && f.Status != "D" && f.Status != "M" && f.Status != "T") { + return nil, ErrDeferred + } + seen[path] = true + files = append(files, f) + } + return files, nil +} + +var snapshotHunk = regexp.MustCompile(`^@@ -([0-9]+)(?:,([0-9]+))? \+([0-9]+)(?:,([0-9]+))? @@(?: .*)?$`) + +func snapshotSourceLines(s string) []string { + if s == "" { + return nil + } + return strings.Split(strings.TrimSuffix(s, "\n"), "\n") +} + +// The patch's filenames and free-form function labels never select a path or +// line. Every consumed context/add/delete line must match the retained blobs. +func snapshotHunkLines(patch, old, new string) (map[int]bool, error) { + result := map[int]bool{} + oldLines, newLines := snapshotSourceLines(old), snapshotSourceLines(new) + oldAt, newAt, oldEnd, newEnd := 0, 0, 0, 0 + hunks := 0 + needsEOFMarker := false + lines := strings.Split(strings.TrimSuffix(patch, "\n"), "\n") + for _, line := range lines { + if needsEOFMarker { + if line != "\\ No newline at end of file" { + return nil, ErrDeferred + } + needsEOFMarker = false + continue + } + if strings.HasPrefix(line, "@@") { + m := snapshotHunk.FindStringSubmatch(line) + if m == nil || oldAt != oldEnd || newAt != newEnd { + return nil, ErrDeferred + } + values := [4]int{} + for i := range values { + if m[i+1] == "" { + values[i] = 1 + } else { + v, err := strconv.Atoi(m[i+1]) + if err != nil || v < 0 || v > snapshotObjectBytes { + return nil, ErrDeferred + } + values[i] = v + } + } + os, oc, ns, nc := values[0], values[1], values[2], values[3] + if oc > 0 { + os-- + } + if nc > 0 { + ns-- + } + if os < oldEnd || ns < newEnd || os < 0 || ns < 0 || oc > len(oldLines)-os || nc > len(newLines)-ns || (oc == 0 && nc == 0) { + return nil, ErrDeferred + } + // Unchanged gaps must also agree; an omitted real edit cannot hide + // behind a later hunk whose local text happens to be valid. + if strings.Join(oldLines[oldEnd:os], "\n") != strings.Join(newLines[newEnd:ns], "\n") || os-oldEnd != ns-newEnd { + return nil, ErrDeferred + } + oldAt, newAt, oldEnd, newEnd = os, ns, os+oc, ns+nc + hunks++ + continue + } + if hunks == 0 { + continue + } // Git metadata before the first hunk only. + if len(line) == 0 { + return nil, ErrDeferred + } + prefix, text := line[0], line[1:] + if prefix != ' ' && prefix != '+' && prefix != '-' { + return nil, ErrDeferred + } + if prefix == ' ' && oldAt == len(oldLines)-1 && newAt == len(newLines)-1 && strings.HasSuffix(old, "\n") != strings.HasSuffix(new, "\n") { + return nil, ErrDeferred + } + if prefix != '+' { + if oldAt >= oldEnd || oldLines[oldAt] != text { + return nil, ErrDeferred + } + oldAt++ + needsEOFMarker = oldAt == len(oldLines) && !strings.HasSuffix(old, "\n") + } + if prefix != '-' { + if newAt >= newEnd || newLines[newAt] != text { + return nil, ErrDeferred + } + newAt++ + needsEOFMarker = needsEOFMarker || (newAt == len(newLines) && !strings.HasSuffix(new, "\n")) + result[newAt] = true + } + } + if needsEOFMarker || (hunks == 0 && old != new) || (oldEnd < len(oldLines) && strings.HasSuffix(old, "\n") != strings.HasSuffix(new, "\n")) || oldAt != oldEnd || newAt != newEnd || len(oldLines)-oldEnd != len(newLines)-newEnd || strings.Join(oldLines[oldEnd:], "\n") != strings.Join(newLines[newEnd:], "\n") { + return nil, fmt.Errorf("incomplete source patch: %w", ErrDeferred) + } + return result, nil +} diff --git a/reviewer/snapshot_evidence_review_test.go b/reviewer/snapshot_evidence_review_test.go new file mode 100644 index 00000000..e7e31f22 --- /dev/null +++ b/reviewer/snapshot_evidence_review_test.go @@ -0,0 +1,112 @@ +package reviewer + +import ( + "context" + "strconv" + "strings" + "testing" +) + +func TestSnapshotReviewSubmoduleIgnoreCannotHideChangedGitlink(t *testing.T) { + f := newSnapshotGitFixture(t) + old := f.snapshot.Head + f.write(t, ".gitmodules", "[submodule \"submodule\"]\n\tpath = submodule\n\turl = https://example.invalid/inert\n\tignore = all\n") + f.run(t, "config", "submodule.submodule.ignore", "all") + f.run(t, "config", "diff.ignoreSubmodules", "all") + f.run(t, "add", ".gitmodules") + f.run(t, "update-index", "--cacheinfo", "160000,"+old+",submodule") + f.run(t, "commit", "--quiet", "-m", "changed ignored synthetic gitlink") + f.snapshot.Head = f.run(t, "rev-parse", "HEAD") + f.snapshot.Base = old + f.snapshot.MergeBase = old + if baseline := f.run(t, "diff", "--raw", "--no-renames", old, f.snapshot.Head, "--", "submodule"); baseline != "" { + t.Fatal("fixture did not hide gitlink without override", baseline) + } + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + found := false + for _, c := range e.Coverage() { + if c.Path == "submodule" && strings.Contains(c.Reason, "submodule") { + found = true + } + } + if !found { + t.Fatal("ignored submodule absent from authoritative inventory", e.Coverage()) + } +} + +func TestSnapshotReviewSearchContinuesAfterOmittedPaths(t *testing.T) { + f := newSnapshotGitFixture(t) + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + r, err := bindSnapshotEvidence(t, e).Reader(f.snapshot) + if err != nil { + t.Fatal(err) + } + matches, err := r.Search(context.Background(), f.snapshot.Head, "FOUR", 1) + if err != nil || len(matches) != 1 || matches[0].Path != "safe.go" || matches[0].Line != 4 { + t.Fatal("omissions disabled later text search", matches, err) + } + _, coverage := r.Evidence() + if len(coverage) < 3 { + t.Fatal("search erased omissions", coverage) + } + if _, err := r.Search(context.Background(), strings.Repeat("f", 40), "FOUR", 1); err == nil { + t.Fatal("search accepted foreign revision") + } +} + +func TestSnapshotReviewSourceCoverageLeavesRoomForExhaustion(t *testing.T) { + files := map[string]Object{} + paths := []string{} + initial := []Coverage{} + for i := 0; i < 128; i++ { + path := "binary-" + strconv.Itoa(i) + files[path] = Object{"100644", []byte{0}} + paths = append(paths, path) + initial = append(initial, Coverage{path, "controller binary omission"}) + } + r, err := NewReader(&fixtureStore{files: files}, testSnapshot(), paths, DefaultLimits()) + if err != nil { + t.Fatal(err) + } + r.initialCoverage = initial + r.coverage = append([]Coverage{}, initial...) + session, err := NewSession("coverage-test", testSnapshot(), Digest("config"), Codex, 1, "captured inventory", nil, nil, DefaultLimits()) + if err != nil { + t.Fatal(err) + } + broker, err := newSourceBroker(context.Background(), session, r) + if err != nil { + t.Fatal(err) + } + for _, path := range paths { + if _, err := broker.Read(context.Background(), session.Snapshot.Head, path); err == nil { + t.Fatal("binary read succeeded") + } + } + if _, err := broker.Read(context.Background(), session.Snapshot.Head, paths[0]); err == nil { + t.Fatal("tool budget not enforced") + } + a := broker.seal() + if err := a.validate(session); err != nil { + t.Fatal("ordinary exhaustion invalidated source receipt", len(a.Coverage), err) + } + seen := map[string]bool{} + exhaustion := false + for _, c := range a.Coverage { + if c.Path != "" { + seen[c.Path] = true + } + if strings.Contains(c.Reason, "tool-call limit exhausted") { + exhaustion = true + } + } + if len(seen) != 128 || !exhaustion || !a.Exhausted { + t.Fatal("coverage lost inventory or exhaustion", len(seen), exhaustion) + } +} diff --git a/reviewer/snapshot_evidence_test.go b/reviewer/snapshot_evidence_test.go new file mode 100644 index 00000000..96b5fe4a --- /dev/null +++ b/reviewer/snapshot_evidence_test.go @@ -0,0 +1,485 @@ +package reviewer + +import ( + "context" + "encoding/json" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "testing" + "time" +) + +type snapshotGitFixture struct { + dir, git string + snapshot Snapshot +} + +func (f snapshotGitFixture) run(t *testing.T, args ...string) string { + t.Helper() + c := exec.Command(f.git, args...) + c.Dir = f.dir + c.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null") + b, err := c.CombinedOutput() + if err != nil { + t.Fatalf("git %v: %s: %v", args, b, err) + } + return strings.TrimSpace(string(b)) +} +func (f snapshotGitFixture) write(t *testing.T, path, data string) { + t.Helper() + p := filepath.Join(f.dir, path) + if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(data), 0600); err != nil { + t.Fatal(err) + } +} +func (f snapshotGitFixture) store() GitStore { return GitStore{f.git, filepath.Join(f.dir, ".git")} } + +func newSnapshotGitFixture(t *testing.T) snapshotGitFixture { + return snapshotGitFixtureFormat(t, "sha1") +} + +func snapshotGitFixtureFormat(t *testing.T, format string) snapshotGitFixture { + t.Helper() + git, err := exec.LookPath("git") + if err != nil { + t.Skip("Git required") + } + git, err = filepath.Abs(git) + if err != nil { + t.Fatal(err) + } + f := snapshotGitFixture{dir: t.TempDir(), git: git} + f.run(t, "init", "--quiet", "--object-format="+format) + f.run(t, "config", "user.name", "Synthetic fixture") + f.run(t, "config", "user.email", "fixture@example.invalid") + f.write(t, "safe.go", "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\ntwelve\nthirteen\nfourteen\nfifteen\nsixteen\nseventeen\neighteen\nnineteen\ntwenty\n") + f.write(t, "removed.go", "removed without newline") + f.write(t, "renamed.go", "same file\n") + f.run(t, "add", "--all") + f.run(t, "commit", "--quiet", "-m", "old synthetic tree") + old := f.run(t, "rev-parse", "HEAD") + f.write(t, "safe.go", strings.ReplaceAll(strings.ReplaceAll(string(mustSnapshotRead(t, filepath.Join(f.dir, "safe.go"))), "four\n", "FOUR\n"), "seventeen\n", "SEVENTEEN\n")) + if err := os.Remove(filepath.Join(f.dir, "removed.go")); err != nil { + t.Fatal(err) + } + if err := os.Rename(filepath.Join(f.dir, "renamed.go"), filepath.Join(f.dir, "new name.go")); err != nil { + t.Fatal(err) + } + f.write(t, "dir/sp ace[1]\".go", "@@ -0,0 +1 @@\n+++ content\nΔ without final newline") + f.write(t, "binary", "private synthetic\x00bytes") + if err := os.Symlink("/synthetic-unreadable-canary", filepath.Join(f.dir, "link")); err != nil { + t.Fatal(err) + } + f.write(t, ".gitattributes", "*.go diff=hostile\n") + f.run(t, "add", "--all") + f.run(t, "update-index", "--add", "--cacheinfo", "160000,"+old+",submodule") + f.run(t, "commit", "--quiet", "-m", "new synthetic tree") + head := f.run(t, "rev-parse", "HEAD") + // A sibling base commit with the same tree as head makes base-tip-to-head + // empty. The intended merge-base-to-head comparison must still find edits. + base := f.run(t, "commit-tree", f.run(t, "rev-parse", "HEAD^{tree}"), "-p", old, "-m", "independent base advance") + f.snapshot = Snapshot{RepositoryID: RepositoryID, HeadRepositoryID: 5555, PR: 1, Head: head, Base: base, MergeBase: old} + return f +} +func mustSnapshotRead(t *testing.T, path string) []byte { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + return b +} + +func TestSnapshotEvidenceUsesActualGitWithoutCheckoutOrExternalDrivers(t *testing.T) { + f := newSnapshotGitFixture(t) + canary := filepath.Join(f.dir, "EXECUTED") + driver := filepath.Join(f.dir, ".git", "forbidden-driver") + if err := os.WriteFile(driver, []byte("#!/bin/sh\ntouch '"+canary+"'\nexit 1\n"), 0700); err != nil { + t.Fatal(err) + } + f.run(t, "config", "diff.hostile.command", driver) + f.run(t, "config", "diff.hostile.textconv", driver) + if err := os.WriteFile(filepath.Join(f.dir, ".git/hooks/post-checkout"), mustSnapshotRead(t, driver), 0700); err != nil { + t.Fatal(err) + } + f.write(t, "safe.go", "working tree canary must not enter packet\n") + t.Setenv("GIT_EXTERNAL_DIFF", driver) + t.Setenv("GIT_CONFIG_COUNT", "1") + t.Setenv("GIT_CONFIG_KEY_0", "diff.external") + t.Setenv("GIT_CONFIG_VALUE_0", driver) + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + if _, err := os.Stat(canary); !os.IsNotExist(err) { + t.Fatal("candidate hook/driver executed", err) + } + if strings.Contains(e.Packet(), "working tree canary") { + t.Fatal("working tree read") + } + var p snapshotPacket + if err := json.Unmarshal([]byte(e.Packet()), &p); err != nil { + t.Fatal(err) + } + if p.Snapshot != f.snapshot || p.Version != "sfl-snapshot-evidence-1" || len(p.Files) != 9 || len(p.Coverage) != 3 { + t.Fatalf("%+v", p) + } + bound := bindSnapshotEvidence(t, e) + lines := bound.diffLines() + if !lines["safe.go"][4] || !lines["safe.go"][17] || lines["safe.go"][10] || len(lines["removed.go"]) != 0 || !lines["dir/sp ace[1]\".go"][3] { + t.Fatal(lines) + } + if len(lines["binary"]) != 0 || len(lines["link"]) != 0 || len(lines["submodule"]) != 0 { + t.Fatal("omitted object anchors", lines) + } + for _, file := range p.Files { + if file.Path == "new name.go" && file.Status != "A" { + t.Fatal("rename pairing was not disabled") + } + } + // Accessors cannot mutate the capture used later for admission/publication. + lines["safe.go"][10] = true + paths := e.Paths() + paths[0] = "outside" + coverage := e.Coverage() + coverage[0].Reason = "forged" + if bindSnapshotEvidence(t, e).diffLines()["safe.go"][10] || e.Paths()[0] == "outside" || e.Coverage()[0].Reason == "forged" { + t.Fatal("mutable evidence") + } + r, err := bound.Reader(f.snapshot) + if err != nil { + t.Fatal(err) + } + text, err := r.Read(context.Background(), f.snapshot.Head, "safe.go") + if err != nil || !strings.Contains(text, "FOUR\n") { + t.Fatal(text, err) + } + _, c := r.Evidence() + if len(c) != 3 { + t.Fatal(c) + } + s, err := NewSession("fixture", f.snapshot, Digest("config"), Codex, 1, e.Packet(), nil, nil, DefaultLimits()) + if err != nil { + t.Fatal(err) + } + b, err := newSourceBroker(context.Background(), s, r) + if err != nil { + t.Fatal(err) + } + _, c = b.reader.Evidence() + if len(c) != 3 { + t.Fatal("fork lost authoritative omissions", c) + } + if b.reader.calls != 0 || b.reader.used != 0 { + t.Fatal("fork inherited citation budget") + } + wrong := f.snapshot + wrong.HeadRepositoryID++ + if _, err := bound.Reader(wrong); err != ErrFenced { + t.Fatal("snapshot mismatch", err) + } +} + +func TestSnapshotEvidenceRefusesMissingOrAmbiguousHistory(t *testing.T) { + f := newSnapshotGitFixture(t) + for _, field := range []string{"head", "base", "merge-base", "not-commit", "incorrect-merge-base"} { + t.Run(field, func(t *testing.T) { + s := f.snapshot + switch field { + case "head": + s.Head = strings.Repeat("f", 40) + case "base": + s.Base = strings.Repeat("f", 40) + case "merge-base": + s.MergeBase = strings.Repeat("f", 40) + case "not-commit": + s.Head = f.run(t, "rev-parse", "HEAD:safe.go") + case "incorrect-merge-base": + s.MergeBase = s.Head + } + if e, err := CaptureSnapshotEvidence(context.Background(), f.store(), s); err == nil || e != nil { + t.Fatal(e, err) + } + }) + } + s := f.snapshot + s.RepositoryID++ + if _, err := CaptureSnapshotEvidence(context.Background(), GitStore{}, s); err != ErrIgnored { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if e, err := CaptureSnapshotEvidence(ctx, f.store(), f.snapshot); err == nil || e != nil { + t.Fatal(e, err) + } +} + +func TestSnapshotEvidenceRetainsSizeOmissionsAndEnforcesInventoryCap(t *testing.T) { + f := newSnapshotGitFixture(t) + f.write(t, "huge.go", strings.Repeat("x", snapshotObjectBytes+1)) + f.write(t, "patch-large.go", strings.Repeat("unique line\n", 8000)) + f.run(t, "add", "--all") + f.run(t, "commit", "--quiet", "-m", "large synthetic sources") + f.snapshot.Head = f.run(t, "rev-parse", "HEAD") + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + reasons := map[string]string{} + for _, c := range e.Coverage() { + reasons[c.Path] = c.Reason + } + if !strings.Contains(reasons["huge.go"], "source byte limit") || !strings.Contains(reasons["patch-large.go"], "packet byte limit") || len(e.Packet()) > snapshotPacketBytes { + t.Fatal(reasons) + } + if len(bindSnapshotEvidence(t, e).diffLines()["huge.go"]) != 0 || len(bindSnapshotEvidence(t, e).diffLines()["patch-large.go"]) != 0 { + t.Fatal("omitted patch admitted inline") + } + for i := 0; i <= snapshotFileLimit; i++ { + f.write(t, "many/"+strconv.Itoa(i), "x\n") + } + f.run(t, "add", "--all") + f.run(t, "commit", "--quiet", "-m", "too many paths") + f.snapshot.Head = f.run(t, "rev-parse", "HEAD") + if e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot); err == nil || e != nil { + t.Fatal("partial inventory admitted", e, err) + } +} + +func TestSnapshotHunksRejectForgedCoordinatesTextAndMissingEdits(t *testing.T) { + valid := "diff --git a b\n--- a\n+++ b\n@@ -1,2 +1,2 @@ arbitrary metadata\n one\n-two\n+TWO\n" + lines, err := snapshotHunkLines(valid, "one\ntwo\n", "one\nTWO\n") + if err != nil || !lines[2] { + t.Fatal(lines, err) + } + for name, patch := range map[string]string{ + "wrong source": strings.Replace(valid, "+TWO", "+forged", 1), + "wrong deleted source": strings.Replace(valid, "-two", "-forged", 1), + "wrong count": strings.Replace(valid, "-1,2", "-1,3", 1), + "out of bounds": strings.Replace(valid, "+1,2", "+999999999999999999999999,2", 1), + "zero start with content": strings.Replace(valid, "+1,2", "+0,2", 1), + "unfinished": strings.TrimSuffix(valid, "+TWO\n"), + "extra content": valid + "+extra\n", + "overlap": valid + "@@ -1,2 +1,2 @@\n one\n-two\n+TWO\n", + "missing edits": "diff --git a b\n", + } { + t.Run(name, func(t *testing.T) { + if _, err := snapshotHunkLines(patch, "one\ntwo\n", "one\nTWO\n"); err == nil { + t.Fatal("forged patch accepted") + } + }) + } + if _, err := snapshotHunkLines("@@ -3 +3 @@\n c\n", "one\nb\nc\n", "TWO\nb\nc\n"); err == nil { + t.Fatal("hidden edit in unchanged gap") + } +} + +func TestSnapshotRawChangesRejectUnsafeAndIncompleteInventories(t *testing.T) { + a, b := strings.Repeat("a", 40), strings.Repeat("b", 40) + valid := ":100644 100644 " + a + " " + b + " M\x00safe.go\x00" + for _, raw := range []string{strings.TrimSuffix(valid, "\x00"), valid + valid, strings.Replace(valid, "safe.go", "../escape", 1), strings.Replace(valid, " M", " R100", 1), strings.Replace(valid, a, strings.Repeat("0", 40), 1), strings.Replace(valid, "100644", "000000", 1), strings.Replace(valid, a, "bad", 1)} { + if _, err := parseSnapshotChanges([]byte(raw), 40); err == nil { + t.Fatal("malformed raw accepted") + } + } +} + +func TestSnapshotHunksVerifyEndOfFileNewlineMarkers(t *testing.T) { + patch := "@@ -1 +1 @@\n-old\n\\ No newline at end of file\n+new\n\\ No newline at end of file\n" + if lines, err := snapshotHunkLines(patch, "old", "new"); err != nil || !lines[1] { + t.Fatal(lines, err) + } + for _, p := range []string{strings.Replace(patch, "\\ No newline at end of file\n", "", 1), strings.TrimSuffix(patch, "\\ No newline at end of file\n"), "@@ -1 +1 @@\n-old\n+new\n\\ No newline at end of file\n"} { + if _, err := snapshotHunkLines(p, "old", "new"); err == nil { + t.Fatal("missing EOF marker accepted") + } + } + if _, err := snapshotHunkLines(patch, "old\n", "new\n"); err == nil { + t.Fatal("forged EOF marker accepted") + } + if _, err := snapshotHunkLines("diff --git a b\n", "old\n", "old"); err == nil { + t.Fatal("hidden newline edit accepted") + } +} + +func TestSnapshotEvidenceOmissionsSurviveDurableFakeReviewAndRecovery(t *testing.T) { + f := newSnapshotGitFixture(t) + evidence, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + store, _ := openFixtureStore(t) + request := native(1, controlTime) + request.Snapshot = f.snapshot + if err := store.Discover(context.Background(), Discovery{Scope: alphaScope(), Requests: []NativeRequest{request}, Through: controlTime.Add(time.Minute), Complete: true}); err != nil { + t.Fatal(err) + } + job, err := store.Admit(context.Background(), alphaScope(), 1, strings.Repeat("d", 40), evidence.Packet(), controlTime.Add(2*time.Minute)) + if err != nil { + t.Fatal(err) + } + calls := 0 + fake := reviewFunc(func(_ context.Context, session Session) (Report, error) { + calls++ + // No model coverage declaration. Controller observations must survive. + return fakeReport(session), nil + }) + bound, err := evidence.Bind(job) + if err != nil { + t.Fatal(err) + } + engine := Engine{Reviewers: map[Provider]Reviewer{Codex: fake, Claude: fake}, Reader: func(s Snapshot) (*Reader, error) { return bound.Reader(s) }} + out, err := store.RunStopped(context.Background(), job.ID, engine, func() time.Time { return controlTime.Add(3 * time.Minute) }) + if err != nil || out.State != "dual_complete" || calls != 4 { + t.Fatal(out.State, calls, err) + } + for _, artifact := range out.Artifacts { + if len(artifact.Report.Coverage) != 3 { + t.Fatal("model erased authoritative coverage", artifact.Report.Coverage) + } + } + recovered, err := store.RecoverCompleted(context.Background(), job.ID, engine.Reader) + if err != nil || calls != 4 { + t.Fatal("recovery repeated fake sessions", calls, err) + } + doc, err := bound.Render(recovered) + if err != nil { + t.Fatal(err) + } + intent, err := (Publisher{Store: store}).PrepareEvidence(context.Background(), recovered, bound) + if err != nil || intent.JobHash != Digest(job) || intent.Kind != "review" { + t.Fatal("bound evidence did not prepare durable publication", intent, err) + } + for _, path := range []string{"binary", "link", "submodule"} { + if !strings.Contains(doc.Summary, path) { + t.Fatal("omission absent from publication", path) + } + } +} + +func bindSnapshotEvidence(t *testing.T, e *SnapshotEvidence) *JobEvidence { + t.Helper() + job := testJob() + job.Snapshot = e.snapshot + job.Packet = e.Packet() + bound, err := e.Bind(job) + if err != nil { + t.Fatal(err) + } + return bound +} + +func TestSnapshotEvidenceBindingRejectsRecapturedOrSubstitutedJob(t *testing.T) { + f := newSnapshotGitFixture(t) + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + job := testJob() + job.Snapshot = f.snapshot + job.Packet = e.Packet() + for _, change := range []func(*Job){func(j *Job) { j.Packet += " " }, func(j *Job) { j.Snapshot.HeadRepositoryID++ }, func(j *Job) { j.RequesterID++ }, func(j *Job) { j.Config.Limits.ReaderCalls = 0 }} { + bad := job + change(&bad) + if _, err := e.Bind(bad); err != ErrFenced { + t.Fatal("mismatched evidence accepted", err) + } + } +} + +func TestSnapshotEvidenceTypedPublicationRejectsDifferentAdmittedJob(t *testing.T) { + f := newSnapshotGitFixture(t) + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + job := testJob() + job.Snapshot = f.snapshot + job.Packet = e.Packet() + bound, err := e.Bind(job) + if err != nil { + t.Fatal(err) + } + if _, err := Render(Outcome{Job: job, State: "provider_unavailable"}, bound.diffLines()); err != ErrFenced { + t.Fatal("captured packet accepted unbound render map", err) + } + if _, err := (Publisher{}).Prepare(context.Background(), Outcome{Job: job}, bound.diffLines()); err != ErrFenced { + t.Fatal("captured packet accepted unbound publication map", err) + } + for _, change := range []func(*Job){func(j *Job) { j.ID = "different-job" }, func(j *Job) { j.Packet += " " }, func(j *Job) { j.Generation++ }, func(j *Job) { j.Snapshot.HeadRepositoryID++ }} { + other := job + change(&other) + out := Outcome{Job: other, State: "provider_unavailable"} + if _, err := bound.Render(out); err != ErrFenced { + t.Fatal("cross-job render accepted", err) + } + // A nil publisher would panic if the binding check reached storage. + if _, err := (Publisher{}).PrepareEvidence(context.Background(), out, bound); err != ErrFenced { + t.Fatal("cross-job publication reached storage", err) + } + } +} + +func TestSnapshotEvidenceReadsActualSHA256GitObjects(t *testing.T) { + f := snapshotGitFixtureFormat(t, "sha256") + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + if len(f.snapshot.Head) != 64 || !bindSnapshotEvidence(t, e).diffLines()["safe.go"][4] { + t.Fatal("SHA256 object protocol not captured") + } +} + +func TestSnapshotEvidenceMissingPromisorBlobNeverFetches(t *testing.T) { + f := newSnapshotGitFixture(t) + marker, helper := filepath.Join(f.dir, "FETCHED"), filepath.Join(f.dir, ".git", "fetch-trap") + quoted := "'" + strings.ReplaceAll(marker, "'", "'\\''") + "'" + if err := os.WriteFile(helper, []byte("#!/bin/sh\nprintf invoked > "+quoted+"\nexit 1\n"), 0700); err != nil { + t.Fatal(err) + } + blob := f.run(t, "rev-parse", "HEAD:safe.go") + f.run(t, "config", "extensions.partialClone", "origin") + f.run(t, "config", "remote.origin.promisor", "true") + f.run(t, "config", "remote.origin.url", "ext::"+helper) + f.run(t, "config", "protocol.ext.allow", "always") + if err := os.Remove(filepath.Join(f.dir, ".git", "objects", blob[:2], blob[2:])); err != nil { + t.Fatal(err) + } + // A local inert trap proves this setup actually attempts a lazy fetch + // without the production command's no-fetch controls. No network is used. + c := exec.Command(f.git, "cat-file", "-s", blob) + c.Dir = f.dir + c.Env = []string{"GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_NO_LAZY_FETCH=0", "LC_ALL=C"} + if c.Run() == nil { + t.Fatal("missing blob unexpectedly readable") + } + if _, err := os.Stat(marker); err != nil { + t.Fatal("inert fetch trap not reached", err) + } + if err := os.Remove(marker); err != nil { + t.Fatal(err) + } + e, err := CaptureSnapshotEvidence(context.Background(), f.store(), f.snapshot) + if err != nil { + t.Fatal(err) + } + if _, err := os.Stat(marker); !os.IsNotExist(err) { + t.Fatal("implicit fetch executed", err) + } + found := false + for _, coverage := range e.Coverage() { + if coverage.Path == "safe.go" && strings.Contains(coverage.Reason, "new source unavailable") { + found = true + } + } + if !found || len(bindSnapshotEvidence(t, e).diffLines()["safe.go"]) != 0 { + t.Fatal("missing source counted as clean", e.Coverage()) + } +} diff --git a/reviewer/source_broker.go b/reviewer/source_broker.go index 468cd39a..02de744f 100644 --- a/reviewer/source_broker.go +++ b/reviewer/source_broker.go @@ -125,6 +125,8 @@ func newSourceBroker(ctx context.Context, s Session, verifier *Reader) (*sourceB if err != nil { return nil, err } + r.initialCoverage = append([]Coverage(nil), verifier.initialCoverage...) + r.coverage = append([]Coverage(nil), verifier.initialCoverage...) return &sourceBroker{ctx: ctx, reader: r, session: s}, nil } @@ -191,10 +193,36 @@ func (b *sourceBroker) seal() *SourceAccess { b.reader.mu.Lock() calls, used := b.reader.calls, b.reader.used b.reader.mu.Unlock() - coverage = append(coverage, b.denials...) + coverage = compactSourceCoverage(append(coverage, b.denials...)) return &SourceAccess{Version: 1, SessionHash: Digest(b.session), ToolCalls: b.calls, ReaderCalls: calls, Bytes: used, Exhausted: b.exhausted, Reads: append([]ReadRecord{}, reads...), Coverage: append([]Coverage{}, coverage...)} } +// Group controller-owned reasons by allowed path, including global denials. +// Inventory omissions and repeated reads cannot consume separate slots for +// the same path and crowd out the one-time exhaustion fact. Unknown/unsafe +// attempted paths are sanitized to the global path by Reader.Read. +func compactSourceCoverage(records []Coverage) []Coverage { + if len(records) <= 256 { + return records + } + out := []Coverage{} + indices := map[string]int{} + seen := map[Coverage]bool{} + for _, record := range records { + if seen[record] { + continue + } + seen[record] = true + if index, ok := indices[record.Path]; ok { + out[index].Reason += "; " + record.Reason + } else { + indices[record.Path] = len(out) + out = append(out, record) + } + } + return out +} + func (b *sourceBroker) revoke() { b.mu.Lock() defer b.mu.Unlock()