feat(assessment): report planned bank coverage
This commit is contained in:
1 parent
76d32bb79f
commit
a69b1a79bb
10 files changed
+1211
-288
No files matched your search
@@ -0,0 +1,225 @@
|
||||
// Package bankcoverage implements the local assessment bank coverage command.
|
||||
package bankcoverage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
assessment "lincodex/backend/internal/usecase/assessment"
|
||||
)
|
||||
|
||||
// Run reports how valid bundle drafts and implementation paths match the bank manifest.
|
||||
func Run(arguments []string, stdout, stderr io.Writer) error {
|
||||
const location = "bankcoverage.Run"
|
||||
flags := flag.NewFlagSet("assessment-bank-coverage", flag.ContinueOnError)
|
||||
flags.SetOutput(stderr)
|
||||
manifestPath := flags.String("manifest", "testdata/assessment/bank_manifest.json", "path to the planned bank manifest")
|
||||
bundleDir := flags.String("bundles", "testdata/assessment", "directory containing bank bundle JSON files and solution files")
|
||||
strict := flags.Bool("strict", false, "fail when planned families, review declarations, or implementation paths are missing")
|
||||
if err := flags.Parse(arguments); err != nil {
|
||||
return fmt.Errorf("%s - parse arguments: %w", location, err)
|
||||
}
|
||||
if flags.NArg() != 0 {
|
||||
return fmt.Errorf("%s - unexpected positional arguments", location)
|
||||
}
|
||||
content, err := os.ReadFile(*manifestPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - read manifest: %w", location, err)
|
||||
}
|
||||
manifest, err := assessment.ParseBankCoverageManifest(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inventory, err := loadBankCoverageInventory(*bundleDir, *manifestPath, manifest)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - load bundle inventory: %w", location, err)
|
||||
}
|
||||
report, err := assessment.BuildBankCoverageReport(manifest, inventory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - build report: %w", location, err)
|
||||
}
|
||||
if err := printBankCoverageReport(stdout, report); err != nil {
|
||||
return fmt.Errorf("%s - write report: %w", location, err)
|
||||
}
|
||||
if *strict {
|
||||
return report.StrictError()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadBankCoverageInventory(bundleDir, manifestPath string, manifest assessment.BankCoverageManifest) (result assessment.BankCoverageInventory, returnErr error) {
|
||||
const location = "bankcoverage.loadBankCoverageInventory"
|
||||
manifestPath, err := canonicalPath(manifestPath)
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - resolve manifest path: %w", location, err)
|
||||
}
|
||||
bundleRoot, err := os.OpenRoot(bundleDir)
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - open bundle directory: %w", location, err)
|
||||
}
|
||||
defer func() {
|
||||
if err := bundleRoot.Close(); err != nil {
|
||||
returnErr = errors.Join(returnErr, fmt.Errorf("%s - close bundle directory: %w", location, err))
|
||||
}
|
||||
}()
|
||||
inventory := assessment.BankCoverageInventory{}
|
||||
err = fs.WalkDir(bundleRoot.FS(), ".", func(path string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return fmt.Errorf("%s - scan bundle directory: %w", location, walkErr)
|
||||
}
|
||||
if entry.IsDir() || filepath.Ext(path) != ".json" {
|
||||
return nil
|
||||
}
|
||||
candidatePath, err := canonicalPath(filepath.Join(bundleDir, filepath.FromSlash(path)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - resolve candidate path %s: %w", location, filepath.ToSlash(path), err)
|
||||
}
|
||||
if candidatePath == manifestPath {
|
||||
return nil
|
||||
}
|
||||
content, err := bundleRoot.ReadFile(filepath.FromSlash(path))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - read bundle %s: %w", location, filepath.ToSlash(path), err)
|
||||
}
|
||||
bundle, err := assessment.ParseBundle(bytes.NewReader(content))
|
||||
if err != nil {
|
||||
inventory.InvalidBundleFiles = append(inventory.InvalidBundleFiles, fmt.Sprintf("%s: %v", filepath.ToSlash(path), err))
|
||||
return nil
|
||||
}
|
||||
drafts, err := bundle.Drafts()
|
||||
if err != nil {
|
||||
inventory.InvalidBundleFiles = append(inventory.InvalidBundleFiles, fmt.Sprintf("%s: %v", filepath.ToSlash(path), err))
|
||||
return nil
|
||||
}
|
||||
for _, draft := range drafts {
|
||||
core := draft.Core()
|
||||
inventory.Families = append(inventory.Families, assessment.BankCoverageFamilyRecord{
|
||||
Key: string(core.Key), Category: string(core.Category), Version: core.Version,
|
||||
Author: string(core.Author), Reviewer: string(core.Reviewer),
|
||||
ReviewStatus: string(core.ReviewStatus), Source: filepath.ToSlash(path),
|
||||
})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, err
|
||||
}
|
||||
if err := collectImplementationPaths(bundleRoot, manifest, &inventory); err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - collect implementation paths: %w", location, err)
|
||||
}
|
||||
return inventory, nil
|
||||
}
|
||||
|
||||
func collectImplementationPaths(root *os.Root, manifest assessment.BankCoverageManifest, inventory *assessment.BankCoverageInventory) error {
|
||||
const location = "bankcoverage.collectImplementationPaths"
|
||||
for _, category := range manifest.Categories {
|
||||
for _, topic := range category.Topics {
|
||||
for _, key := range topic.FamilyKeys {
|
||||
for _, language := range category.RequiredLanguages {
|
||||
relative := filepath.Join(category.ImplementationsDir, key+implementationExtension(language))
|
||||
info, err := root.Stat(relative)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("%s - inspect %s: %w", location, filepath.ToSlash(relative), err)
|
||||
}
|
||||
if info.Mode().IsRegular() && info.Size() > 0 {
|
||||
inventory.ImplementationFiles = append(inventory.ImplementationFiles, filepath.ToSlash(relative))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func canonicalPath(path string) (string, error) {
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resolvedPath, err := filepath.EvalSymlinks(absPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Clean(resolvedPath), nil
|
||||
}
|
||||
|
||||
func implementationExtension(language string) string {
|
||||
if language == "python" {
|
||||
return ".py"
|
||||
}
|
||||
return ".go"
|
||||
}
|
||||
|
||||
func printBankCoverageReport(writer io.Writer, report assessment.BankCoverageReport) error {
|
||||
const location = "bankcoverage.printBankCoverageReport"
|
||||
write := func(format string, values ...any) error {
|
||||
if _, err := fmt.Fprintf(writer, format, values...); err != nil {
|
||||
return fmt.Errorf("%s - write output: %w", location, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := write("Assessment bank coverage inventory\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := write("Family records are valid drafts from bundle files; this report does not publish or calibrate them.\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, category := range report.Categories {
|
||||
if err := write("%s: %d/%d planned family keys present; %d keys inventoried; %d/%d implementation paths present\n",
|
||||
category.Code, category.PresentFamilies, category.PlannedFamilies, category.InventoriedFamilies,
|
||||
category.PresentImplementationFiles, category.RequiredImplementationFiles); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := write("Review declarations: %d approved, %d pending, %d rejected; %d distinct family keys inventoried\n",
|
||||
report.DeclaredApprovedReviews, report.PendingReviewDeclarations, report.RejectedReviewDeclarations, report.InventoriedFamilies); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, names := range []struct {
|
||||
label string
|
||||
names []string
|
||||
}{
|
||||
{"Missing family keys", report.MissingFamilyKeys},
|
||||
{"Families without an approved independent reviewer declaration", report.MissingIndependentReviews},
|
||||
{"Missing Go/Python implementation paths", report.MissingImplementationFiles},
|
||||
{"Unassigned family keys", report.UnassignedFamilyKeys},
|
||||
{"Unassigned categories", report.UnassignedCategories},
|
||||
{"Category mismatches", report.CategoryMismatches},
|
||||
{"Invalid bundle records", report.InvalidBundleFiles},
|
||||
{"Evidence not verifiable by this check", report.UnverifiableEvidence},
|
||||
} {
|
||||
if err := printNames(writer, names.label, names.names); err != nil {
|
||||
return fmt.Errorf("%s - print %s: %w", location, names.label, err)
|
||||
}
|
||||
}
|
||||
if report.CalibrationMeasurementRequired {
|
||||
if err := write("Calibration thresholds remain external measurement gates; no quality result is inferred.\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printNames(writer io.Writer, label string, names []string) error {
|
||||
if len(names) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := fmt.Fprintf(writer, "%s (%d):\n", label, len(names)); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
if _, err := fmt.Fprintf(writer, " %s\n", name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user