Skip to content

fix: make external-analyzer quality gaps deterministic and correctly attributed - #454

Merged
jflowers merged 1 commit into
unbound-force:mainfrom
jflowers:opsx/external-analyzer-quality-gap-order-is-n
Dec 1, 2026
Merged

jflowers merged 1 commit into
unbound-force:mainfrom
jflowers:opsx/external-analyzer-quality-gap-order-is-n

Conversation

@jflowers

@jflowers jflowers commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixed two defects in external-analyzer quality mode (epic #449 Tier 2, issue #448).

In internal/adapter/quality.go BuildQualityFromMappings:

  • Determinism: replaced Go map iteration over distinct targets with a deterministic walk sorted by (package, function), preserving analysis-result order within each target. Extended report-level sorts to total orders: reports by (TestFunction, TestLocation) and WorstCoverageTests by (Percentage, TestFunction, TestLocation). Two runs of gaze report --analyzer on identical input now produce byte-identical JSON.
  • Per-gap attribution: each gap now carries its owning function via new GapTargets []FunctionTarget on taxonomy.ContractCoverage (JSON gap_targets,omitempty, parallel to Gaps, external-analyzer-only). Renderers (markdown gaps-table Target column, text [owner] prefix, HTML owner prefix, compact AI payload) name the per-gap owner with fallback to the report-level label.

Quality scores (Percentage/CoveredCount/TotalContractual) and the Go-native quality path (internal/quality/quality.go, coverage.go) are untouched. Updated internal/report/schema.go, docs (json-schemas.md, quality.md), AGENTS.md Recent Changes, and filed website issue unbound-force/website#497.

Closes #448

How to Test

From openspec/changes/external-analyzer-quality-gap-order-is-n/tasks.md and specs/quality-external-analyzer/spec.md:

  1. Build parity: go build ./cmd/gaze and go build ./... succeed.
  2. Tests: go test -race -count=1 -short -timeout 15m ./... passes (20 packages).
  3. Lint: golangci-lint run ./... reports 0 issues.
  4. Go-native untouched: git diff origin/main...HEAD -- internal/quality/quality.go internal/quality/coverage.go shows no changes; Go-native golden/text tests pass unchanged (GapTargets nil → fallback rendering).
  5. Determinism (QEX-FR-001): go test -run TestBuildQualityFromMappings -count=25 -race ./internal/adapter — synthetic multi-target fixture invoked ≥25 times produces byte-identical marshaled JSON; gaps ordered by sorted (package,function) then analysis-result order; WorstCoverageTests tie-break verified.
  6. Per-gap attribution (QEX-FR-002): go test -run TestGap -race ./internal/aireport ./internal/quality — markdown table rows, text [owner] lines, HTML items, and compact payload gap_targets name the owning function per gap; fallback and escaping verified.
  7. Schema: go test -run TestReportSchema -race ./internal/report — JSON with gap_targets validates.
  8. Session determinism: go test -run TestSessionMultiTarget -race -short ./internal/adapter runs the fake_analyzer with --multi-target-mapping across repeated sessions and asserts byte-identical order/labels.

How to Demo

  1. Run gaze report --analyzer <analyzer> --format=json ./... twice on identical input and diff the outputs — they must be identical.
  2. Inspect the markdown gaps table (--format=markdown) — each gap row's Target column shows its owning function, not the report's first target.
  3. For multi-target test fixtures, verify gap_targets in JSON aligns index-parallel with gaps and carries the correct package/function per gap.

Key Files Changed

  • internal/adapter/quality.go — deterministic target iteration, total-order sorts, per-gap target join
  • internal/taxonomy/types.go — ContractCoverage.GapTargets field
  • internal/report/schema.go — gap_targets property
  • internal/aireport/compact.go, render_markdown.go — per-gap owner rendering + compact payload
  • internal/quality/report.go, html.go, quality.html.tmpl — text/HTML per-gap owner
  • internal/protocol/testdata/fake_analyzer/main.go — --multi-target-mapping flag
  • internal/adapter/quality_determinism_test.go, quality_internal_test.go, session_multi_target_test.go — regression/tie-break/resilience tests
  • internal/adapter/testdata/quality-determinism/* — golden fixtures
  • internal/aireport/compact_test.go, render_markdown_test.go — renderer tests
  • internal/quality/html_test.go, report_internal_test.go — renderer tests
  • internal/report/report_test.go — schema validation
  • docs/reference/cli/quality.md, docs/reference/json-schemas.md — docs
  • AGENTS.md — Recent Changes
  • openspec/changes/external-analyzer-quality-gap-order-is-n/* — spec artifacts

Stats: 29 files changed, 5072 insertions(+), 32 deletions(-) vs upstream/main

Known Issues

  • Code review 816139f2-f33c-4d5b-b46e-0861d4401e07 — APPROVE WITH ADVISORIES (7 reviewers, 0 blocking findings): 1 LOW advisory — duplicated gapOwnerName/gapOwner guard across 3 renderers (design D3) — non-blocking, tracked but not requiring follow-up.

This PR was generated by /uf.finale (AI-assisted).

…attributed

Two defects in external-analyzer quality mode (issue unbound-force#448):
- Determinism: BuildQualityFromMappings unioned effects by iterating a Go map
  of distinct targets (random order), making Gaps/GapHints/AmbiguousEffects
  and report ordering nondeterministic. Now sorts distinct targets by
  (package, function) and preserves analysis-result order within each
  target; total-order tie-breaks on report/summary sorts ensure two runs
  of `gaze report --analyzer` on identical input produce byte-identical JSON.
- Attribution: every gap row was labeled with the report's first target
  even though Gaps unions effects across all referenced targets. Each gap
  now carries its owning function via new GapTargets []FunctionTarget on
  taxonomy.ContractCoverage (JSON gap_targets, external-analyzer-only);
  markdown Target column, text [owner] prefix, HTML owner prefix, and
  compact AI payload render the per-gap owner with fallback to report-level
  label.
- Scores (Percentage/CoveredCount/TotalContractual) and Go-native quality
  path untouched; JSON schema and docs (json-schemas.md, quality.md,
  AGENTS.md) updated.

Closes unbound-force#448

Assisted-by: Muse-Spark-Open-816B-A42B-NVFP4
Generated with AI assistance (Muse-Spark-Open-816B-A42B-NVFP4)

@uf-divisor uf-divisor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Council Review: APPROVE WITH ADVISORIES

7/7 reviewers approve — divisor-adversary, divisor-architect, divisor-curator, divisor-entropy, divisor-gaze-testing, divisor-guard, divisor-testing (5 APPROVE, 2 APPROVE WITH ADVISORIES). Canonical verdict: APPROVE WITH ADVISORIES — zero CRITICAL or HIGH findings, two LOW advisories.

This PR fixes both defects from #448 in the external-analyzer quality path: gap order is now deterministic (union targets iterated in sorted (package, function) order; report-level sorts made total via (TestFunction, TestLocation) and (Percentage, TestFunction, TestLocation)), and each gap carries and renders its owning function through the additive, schema-documented gap_targets field with fallback-guarded rendering on every surface. The fix is pinned by dual-layer regression tests (25 in-process repetitions plus 5 cross-process sessions with golden score fixtures proving scores are unchanged), and the Go-native path is functionally untouched. All CI checks pass; pre-flight (ci-aware) PASS with gates run at the frozen head commit.

Advisories (non-blocking)

  1. LOW (DRY, CS-004) — the per-gap owner guard i < len(GapTargets) && GapTargets[i].Function != "" is duplicated across three renderers (gapOwner in internal/quality/report.go, gapOwnerName in internal/aireport/render_markdown.go, buildQualityHTMLEffects in internal/quality/html.go); a taxonomy.ContractCoverage method would centralize it. Scoped deliberately per design D3.
  2. LOW (test isolation, TC-002) — TestSession_MultiTargetMappingDeterministicGapOrder defers session Close() inside the 5-iteration loop, stacking deferred closes until function return; explicit per-iteration close would release subprocess handles promptly.

This review was generated by /uf.review-pr (AI-assisted).

}
}

// gapOwner returns the qualified name of the function owning gap i when

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The per-gap owner guard i < len(GapTargets) && GapTargets[i].Function != "" appears in three renderers — gapOwner here, gapOwnerName in internal/aireport/render_markdown.go, and buildQualityHTMLEffects in internal/quality/html.go — each with the identical fallback to the report target. Three occurrences reach the CS-004 extraction threshold: a method on taxonomy.ContractCoverage (for example GapOwnerName(i)) would centralize the bounds and emptiness checks and give every renderer one owner-resolution path. Design D3 scopes this duplication deliberately, so this is a LOW advisory rather than a blocker.

(divisor-architect, LOW)

// and any per-process state differ, not just in-process order.
s := adapter.NewSession(fakeBinaryPath, []string{"--stdio", "--multi-target-mapping"}, rootDir, []string{"./..."}, &stderr, nil)
defer func() { _ = s.Close() }()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer func() { _ = s.Close() }() sits inside the 5-iteration session loop, so the five deferred closes stack and run only when the test function returns. Each session's analyzer subprocess and its file descriptors stay open across the remaining iterations, and a teardown failure in an early session cannot surface before later sessions have run. Closing explicitly at iteration end — or wrapping the loop body in a function with its own defer — releases each subprocess promptly and isolates teardown failures per session. LOW: the test passes and the handles are released at function exit.

(divisor-gaze-testing, LOW)

@jflowers
jflowers merged commit 246969e into unbound-force:main Dec 1, 2026
11 checks passed
@jflowers
jflowers deleted the opsx/external-analyzer-quality-gap-order-is-n branch December 1, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

External-analyzer quality: gap order is nondeterministic and gaps are labeled with the first target for multi-target tests

2 participants