fix(gate): validate finding level and share the blocking threshold
Resolves gate-subsystem-2 and gate-subsystem-1. WIRE: gate.Finding.Level is now a typed gate.Level restricted to LevelError/LevelWarn, and Finding.Validate rejects any other value so a mis-typed or empty level fails the gate closed instead of silently downgrading an intended blocker to a pass -- mirroring the fail-closed standard ReviewFinding.Validate already sets in internal/domain/workflow/review_findings.go. WIRE: StageYaegiGate now delegates the blocking decision to gate.Finding.Blocking instead of re-implementing the raw "error" literal, so the script-facing gate.Blocking helper (exposed by gateextract to interpreted .archie/gate.go scripts) and the rule the daemon actually enforces share one source of truth and cannot drift.
This commit is contained in:
6 files changed
+122
-6
No files matched your search
@@ -163,12 +163,15 @@ func StageYaegiGate() Stage {
|
|||||||
|
|
||||||
var blocking []string
|
var blocking []string
|
||||||
for _, f := range findings {
|
for _, f := range findings {
|
||||||
|
if err := f.Validate(); err != nil {
|
||||||
|
return fmt.Errorf("custom gate: %w", err)
|
||||||
|
}
|
||||||
loc := f.File
|
loc := f.File
|
||||||
if f.Line > 0 {
|
if f.Line > 0 {
|
||||||
loc = fmt.Sprintf("%s:%d", f.File, f.Line)
|
loc = fmt.Sprintf("%s:%d", f.File, f.Line)
|
||||||
}
|
}
|
||||||
msg := strings.TrimSpace(loc + ": " + f.Message)
|
msg := strings.TrimSpace(loc + ": " + f.Message)
|
||||||
if f.Level == "error" {
|
if f.Blocking() {
|
||||||
blocking = append(blocking, msg)
|
blocking = append(blocking, msg)
|
||||||
}
|
}
|
||||||
tc.Log.Info("custom gate finding", "level", f.Level, "file", f.File, "line", f.Line, "message", f.Message)
|
tc.Log.Info("custom gate finding", "level", f.Level, "file", f.File, "line", f.Line, "message", f.Message)
|
||||||
|
|||||||
@@ -139,6 +139,31 @@ func Check(ctx gate.GateContext) []gate.Finding {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStageYaegiGateRejectsUnknownLevel(t *testing.T) {
|
||||||
|
if _, err := exec.LookPath("git"); err != nil {
|
||||||
|
t.Skip("git not installed")
|
||||||
|
}
|
||||||
|
dir := gitRepoWithOriginRef(t, "main")
|
||||||
|
writeGateGo(t, dir, `package gate
|
||||||
|
|
||||||
|
import "github.com/samcharles93/archie-core/internal/gate"
|
||||||
|
|
||||||
|
func Check(ctx gate.GateContext) []gate.Finding {
|
||||||
|
return []gate.Finding{{Level: "fatal", Message: "mis-typed level"}}
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "notes.txt"), []byte("hi\n"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
runGit(t, dir, "add", "-A")
|
||||||
|
runGit(t, dir, "commit", "-m", "add notes")
|
||||||
|
|
||||||
|
tc := newYaegiGateTaskContext(t, dir)
|
||||||
|
if err := StageYaegiGate().Run(context.Background(), tc); err == nil {
|
||||||
|
t.Fatal("StageYaegiGate() = nil, want an error for an unrecognized finding level (fail closed)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestStageRepoStagesNilLoaderIsNoop(t *testing.T) {
|
func TestStageRepoStagesNilLoaderIsNoop(t *testing.T) {
|
||||||
tc := &TaskContext{Log: slog.New(slog.DiscardHandler)}
|
tc := &TaskContext{Log: slog.New(slog.DiscardHandler)}
|
||||||
if err := StageRepoStages().Run(context.Background(), tc); err != nil {
|
if err := StageRepoStages().Run(context.Background(), tc); err != nil {
|
||||||
|
|||||||
+35
-4
@@ -5,6 +5,19 @@
|
|||||||
// the like).
|
// the like).
|
||||||
package gate
|
package gate
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Level is the severity of a gate finding. LevelError blocks the gate;
|
||||||
|
// LevelWarn is advisory and logged only.
|
||||||
|
type Level string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// LevelError blocks the gate: the task is parked instead of opening a PR.
|
||||||
|
LevelError Level = "error"
|
||||||
|
// LevelWarn is advisory and logged only.
|
||||||
|
LevelWarn Level = "warn"
|
||||||
|
)
|
||||||
|
|
||||||
// GateContext carries everything a custom gate function can inspect.
|
// GateContext carries everything a custom gate function can inspect.
|
||||||
type GateContext struct {
|
type GateContext struct {
|
||||||
// Diff is the unified diff of all changes against BaseRef.
|
// Diff is the unified diff of all changes against BaseRef.
|
||||||
@@ -21,8 +34,9 @@ type GateContext struct {
|
|||||||
|
|
||||||
// Finding is one gate violation.
|
// Finding is one gate violation.
|
||||||
type Finding struct {
|
type Finding struct {
|
||||||
// Level is "error" (blocks the gate) or "warn" (advisory, logged only).
|
// Level is the severity: LevelError blocks the gate, LevelWarn is
|
||||||
Level string
|
// advisory and logged only.
|
||||||
|
Level Level
|
||||||
// File is the optional file path the finding applies to.
|
// File is the optional file path the finding applies to.
|
||||||
File string
|
File string
|
||||||
// Line is the optional line number within File.
|
// Line is the optional line number within File.
|
||||||
@@ -30,12 +44,29 @@ type Finding struct {
|
|||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blocking reports whether any finding has Level "error".
|
// Blocking reports whether this finding blocks the gate.
|
||||||
|
func (f Finding) Blocking() bool {
|
||||||
|
return f.Level == LevelError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blocking reports whether any finding blocks the gate.
|
||||||
func Blocking(findings []Finding) bool {
|
func Blocking(findings []Finding) bool {
|
||||||
for _, f := range findings {
|
for _, f := range findings {
|
||||||
if f.Level == "error" {
|
if f.Blocking() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate checks that the finding satisfies contract invariants. An
|
||||||
|
// unrecognized level fails closed: a script that emits a mis-typed or empty
|
||||||
|
// level is an error, never a silently non-blocking finding.
|
||||||
|
func (f Finding) Validate() error {
|
||||||
|
switch f.Level {
|
||||||
|
case LevelError, LevelWarn:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid gate level %q (must be %q or %q)", f.Level, LevelError, LevelWarn)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -99,6 +99,40 @@ func TestBlocking(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindingValidate(t *testing.T) {
|
||||||
|
t.Run("error is valid", func(t *testing.T) {
|
||||||
|
if err := (Finding{Level: LevelError}).Validate(); err != nil {
|
||||||
|
t.Errorf("Validate() = %v, want nil", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("warn is valid", func(t *testing.T) {
|
||||||
|
if err := (Finding{Level: LevelWarn}).Validate(); err != nil {
|
||||||
|
t.Errorf("Validate() = %v, want nil", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("unrecognized level fails closed", func(t *testing.T) {
|
||||||
|
for _, level := range []Level{"fatal", "ERROR", "Error", ""} {
|
||||||
|
if err := (Finding{Level: level}).Validate(); err == nil {
|
||||||
|
t.Errorf("Validate() = nil for level %q, want an error", level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindingBlocking(t *testing.T) {
|
||||||
|
if !(Finding{Level: LevelError}).Blocking() {
|
||||||
|
t.Error("Finding{Level: LevelError}.Blocking() = false, want true")
|
||||||
|
}
|
||||||
|
if (Finding{Level: LevelWarn}).Blocking() {
|
||||||
|
t.Error("Finding{Level: LevelWarn}.Blocking() = true, want false")
|
||||||
|
}
|
||||||
|
if (Finding{Level: ""}).Blocking() {
|
||||||
|
t.Error("Finding{Level: \"\"}.Blocking() = true, want false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGateContextZeroValue(t *testing.T) {
|
func TestGateContextZeroValue(t *testing.T) {
|
||||||
// Zero-value GateContext should not panic when accessed.
|
// Zero-value GateContext should not panic when accessed.
|
||||||
var gctx GateContext
|
var gctx GateContext
|
||||||
|
|||||||
@@ -19,6 +19,15 @@ func TestSymbolsContainsExpectedEntries(t *testing.T) {
|
|||||||
t.Errorf("Blocking kind = %v, want Func", v.Kind())
|
t.Errorf("Blocking kind = %v, want Func", v.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LevelError and LevelWarn must be the exported level constants.
|
||||||
|
for _, name := range []string{"LevelError", "LevelWarn"} {
|
||||||
|
if v, ok := pkg[name]; !ok {
|
||||||
|
t.Errorf("%s not found in Symbols", name)
|
||||||
|
} else if v.Kind() != reflect.String {
|
||||||
|
t.Errorf("%s kind = %v, want String", name, v.Kind())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Finding must be a pointer type.
|
// Finding must be a pointer type.
|
||||||
if v, ok := pkg["Finding"]; !ok {
|
if v, ok := pkg["Finding"]; !ok {
|
||||||
t.Error("Finding not found in Symbols")
|
t.Error("Finding not found in Symbols")
|
||||||
@@ -26,6 +35,14 @@ func TestSymbolsContainsExpectedEntries(t *testing.T) {
|
|||||||
t.Errorf("Finding kind = %v, want Ptr", v.Kind())
|
t.Errorf("Finding kind = %v, want Ptr", v.Kind())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Level must be a pointer to the defined string type, so interpreted
|
||||||
|
// scripts can resolve Finding.Level's named type.
|
||||||
|
if v, ok := pkg["Level"]; !ok {
|
||||||
|
t.Error("Level not found in Symbols")
|
||||||
|
} else if v.Kind() != reflect.Pointer {
|
||||||
|
t.Errorf("Level kind = %v, want Ptr", v.Kind())
|
||||||
|
}
|
||||||
|
|
||||||
// GateContext must be a pointer type.
|
// GateContext must be a pointer type.
|
||||||
if v, ok := pkg["GateContext"]; !ok {
|
if v, ok := pkg["GateContext"]; !ok {
|
||||||
t.Error("GateContext not found in Symbols")
|
t.Error("GateContext not found in Symbols")
|
||||||
@@ -39,8 +56,11 @@ func TestSymbolsNoUnexpectedEntries(t *testing.T) {
|
|||||||
pkg := Symbols[pkgKey]
|
pkg := Symbols[pkgKey]
|
||||||
expected := map[string]bool{
|
expected := map[string]bool{
|
||||||
"Blocking": true,
|
"Blocking": true,
|
||||||
|
"LevelError": true,
|
||||||
|
"LevelWarn": true,
|
||||||
"Finding": true,
|
"Finding": true,
|
||||||
"GateContext": true,
|
"GateContext": true,
|
||||||
|
"Level": true,
|
||||||
}
|
}
|
||||||
for name := range pkg {
|
for name := range pkg {
|
||||||
if !expected[name] {
|
if !expected[name] {
|
||||||
|
|||||||
@@ -10,10 +10,13 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
Symbols["github.com/samcharles93/archie-core/internal/gate/gate"] = map[string]reflect.Value{
|
Symbols["github.com/samcharles93/archie-core/internal/gate/gate"] = map[string]reflect.Value{
|
||||||
// function, constant and variable definitions
|
// function, constant and variable definitions
|
||||||
"Blocking": reflect.ValueOf(gate.Blocking),
|
"Blocking": reflect.ValueOf(gate.Blocking),
|
||||||
|
"LevelError": reflect.ValueOf(gate.LevelError),
|
||||||
|
"LevelWarn": reflect.ValueOf(gate.LevelWarn),
|
||||||
|
|
||||||
// type definitions
|
// type definitions
|
||||||
"Finding": reflect.ValueOf((*gate.Finding)(nil)),
|
"Finding": reflect.ValueOf((*gate.Finding)(nil)),
|
||||||
"GateContext": reflect.ValueOf((*gate.GateContext)(nil)),
|
"GateContext": reflect.ValueOf((*gate.GateContext)(nil)),
|
||||||
|
"Level": reflect.ValueOf((*gate.Level)(nil)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user