Share downstream readiness checks across invocations
This commit is contained in:
@@ -98,7 +98,7 @@ severity/error behavior is preserved.
|
|||||||
|
|
||||||
## Stage 2: Share Downstream Request Construction And Execution
|
## Stage 2: Share Downstream Request Construction And Execution
|
||||||
|
|
||||||
Status: Pending.
|
Status: Complete. Verified with `GOTOOLCHAIN=go1.27.4 go test ./internal/app ./internal/preflight` and `git diff --check`.
|
||||||
|
|
||||||
Read `internal/app/{runner_preflight,preflight_targets}.go`,
|
Read `internal/app/{runner_preflight,preflight_targets}.go`,
|
||||||
`internal/stage/{polish_context,extract_preflight}.go`, and
|
`internal/stage/{polish_context,extract_preflight}.go`, and
|
||||||
|
|||||||
106
internal/app/downstream_readiness.go
Normal file
106
internal/app/downstream_readiness.go
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type readinessCheck struct {
|
||||||
|
Group string `json:"group"`
|
||||||
|
Outcome string `json:"outcome"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type downstreamReadiness struct {
|
||||||
|
Checks []readinessCheck
|
||||||
|
Findings []preflight.Finding
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkDownstreamReadiness checks independent tools using this invocation's
|
||||||
|
// prepared state and retains adapters for subsequent processing.
|
||||||
|
func checkDownstreamReadiness(runtime *invocationRuntime, targets preflight.Targets, prepared, useCurrent, force bool) (downstreamReadiness, error) {
|
||||||
|
var result downstreamReadiness
|
||||||
|
var failures []error
|
||||||
|
record := func(group string, checked preflight.Result, err error) {
|
||||||
|
result.Findings = append(result.Findings, checked.Findings...)
|
||||||
|
outcome, message := "succeeded", ""
|
||||||
|
if err != nil {
|
||||||
|
outcome, message = "failed", err.Error()
|
||||||
|
failures = append(failures, err)
|
||||||
|
}
|
||||||
|
result.Checks = append(result.Checks, readinessCheck{Group: group, Outcome: outcome, Message: message})
|
||||||
|
}
|
||||||
|
blocked := func(group string) {
|
||||||
|
result.Checks = append(result.Checks, readinessCheck{Group: group, Outcome: "blocked", Message: "prepared inputs are unavailable"})
|
||||||
|
}
|
||||||
|
if len(targets.Scriptorium) > 0 && runtime.ctx.Err() == nil {
|
||||||
|
if runtime.env.Scriptorium == nil {
|
||||||
|
runtime.env.Scriptorium = runtime.dependencies.newScriptorium()
|
||||||
|
}
|
||||||
|
checked, err := preflight.Check(runtime.ctx, runtime.env.Scriptorium, targets.Scriptorium)
|
||||||
|
record("prompt requirements", checked, err)
|
||||||
|
}
|
||||||
|
if targets.Polish && runtime.ctx.Err() == nil {
|
||||||
|
if !prepared {
|
||||||
|
blocked("audita")
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
if runtime.env.Audita == nil {
|
||||||
|
runtime.env.Audita, err = runtime.dependencies.newAudita(runtime.cfg)
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("initialize audita runner: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var checked preflight.Result
|
||||||
|
if err == nil {
|
||||||
|
checker, ok := runtime.env.Audita.(preflight.AuditaChecker)
|
||||||
|
if !ok || checker == nil {
|
||||||
|
err = fmt.Errorf("audita runner does not support preflight")
|
||||||
|
} else {
|
||||||
|
root := artifacts.SessionInvocationProbeDir(runtime.paths, runtime.identity.RunID, "audita")
|
||||||
|
request, _, buildErr := stage.BuildPolishRequest(runtime.paths, runtime.manifest, "", filepath.Join(root, "outputs", "transcripts", "polished.json"), filepath.Join(root, "logs"), filepath.Join(root, "debug"))
|
||||||
|
if buildErr != nil {
|
||||||
|
err = fmt.Errorf("build audita preflight: %w", buildErr)
|
||||||
|
} else {
|
||||||
|
checked, err = preflight.CheckAudita(runtime.ctx, checker, request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
record("audita", checked, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if targets.Notarius && runtime.ctx.Err() == nil {
|
||||||
|
if !prepared {
|
||||||
|
blocked("notarius")
|
||||||
|
} else {
|
||||||
|
if runtime.env.Notarius == nil {
|
||||||
|
runtime.env.Notarius = runtime.dependencies.newNotarius()
|
||||||
|
}
|
||||||
|
var checked preflight.Result
|
||||||
|
var err error
|
||||||
|
native, ok := runtime.env.Notarius.(notarius.ReadinessChecker)
|
||||||
|
if !ok || native == nil {
|
||||||
|
err = fmt.Errorf("notarius runner does not support preflight")
|
||||||
|
} else {
|
||||||
|
root := artifacts.SessionInvocationProbeDir(runtime.paths, runtime.identity.RunID, "notarius")
|
||||||
|
probe, buildErr := stage.BuildNotariusReadiness(runtime.cfg, runtime.paths, runtime.manifest, root, useCurrent, force)
|
||||||
|
if buildErr != nil {
|
||||||
|
err = fmt.Errorf("build notarius preflight: %w", buildErr)
|
||||||
|
} else {
|
||||||
|
checked, err = preflight.CheckNotarius(runtime.ctx, native, probe.Request)
|
||||||
|
if verifyErr := probe.VerifyReferences(); verifyErr != nil {
|
||||||
|
err = errors.Join(err, fmt.Errorf("verify Notarius probe references: %w", verifyErr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
record("notarius", checked, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, errors.Join(append(failures, runtime.ctx.Err())...)
|
||||||
|
}
|
||||||
48
internal/app/downstream_readiness_test.go
Normal file
48
internal/app/downstream_readiness_test.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
||||||
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDownstreamReadinessContinuesAfterIndependentFailures(t *testing.T) {
|
||||||
|
cfg := testConfig(t)
|
||||||
|
paths := seedValidationPrepared(t, cfg)
|
||||||
|
m, present, err := loadManifestAtPathIfPresent(context.Background(), &manifest.LocalStore{}, paths.ManifestPath)
|
||||||
|
if err != nil || !present {
|
||||||
|
t.Fatalf("load manifest: %v", err)
|
||||||
|
}
|
||||||
|
scriptFailure := errors.New("requirements unavailable")
|
||||||
|
auditaFailure := errors.New("provider unavailable")
|
||||||
|
inspector := &scriptorium.FakeRunner{RequirementsErr: scriptFailure}
|
||||||
|
provider := &audita.FakeRunner{PreflightErr: auditaFailure}
|
||||||
|
runtime := &invocationRuntime{ctx: context.Background(), cfg: cfg, paths: paths, manifest: m, identity: invocationIdentity{RunID: "probe"}, env: &stage.Env{Scriptorium: inspector, Audita: provider}, dependencies: invocationDependencies{}.normalized()}
|
||||||
|
result, err := checkDownstreamReadiness(runtime, preflight.Targets{Polish: true, Scriptorium: []preflight.Target{{Stage: "trim"}}}, true, false, false)
|
||||||
|
if !errors.Is(err, scriptFailure) || !errors.Is(err, auditaFailure) || len(result.Checks) != 2 || len(result.Findings) != 2 || len(provider.PreflightRequests) != 1 {
|
||||||
|
t.Fatalf("result = %+v, error = %v", result, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDownstreamReadinessReportsRequestFailuresAndBlockedInputs(t *testing.T) {
|
||||||
|
cfg := testConfig(t)
|
||||||
|
configureNotariusProbe(t, cfg)
|
||||||
|
paths := artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root).SessionPathsFor(cfg.Session.Campaign, cfg.Session.SessionID)
|
||||||
|
runtime := &invocationRuntime{ctx: context.Background(), cfg: cfg, paths: paths, identity: invocationIdentity{RunID: "probe"}, env: &stage.Env{Audita: &audita.FakeRunner{}}, dependencies: invocationDependencies{}.normalized()}
|
||||||
|
targets := preflight.Targets{Polish: true, Notarius: true}
|
||||||
|
result, err := checkDownstreamReadiness(runtime, targets, true, false, false)
|
||||||
|
if err == nil || len(result.Checks) != 2 || result.Checks[0].Outcome != "failed" || result.Checks[1].Outcome != "failed" {
|
||||||
|
t.Fatalf("result = %+v, error = %v", result, err)
|
||||||
|
}
|
||||||
|
result, err = checkDownstreamReadiness(runtime, targets, false, false, false)
|
||||||
|
if err != nil || len(result.Checks) != 2 || result.Checks[0].Outcome != "blocked" || result.Checks[1].Outcome != "blocked" {
|
||||||
|
t.Fatalf("result = %+v, error = %v", result, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,8 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||||
)
|
)
|
||||||
@@ -29,53 +24,7 @@ func inspectInvocationRequirements(runtime *invocationRuntime, start int) error
|
|||||||
if !targets.Polish && !targets.Notarius && len(targets.Scriptorium) == 0 {
|
if !targets.Polish && !targets.Notarius && len(targets.Scriptorium) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if len(targets.Scriptorium) > 0 && runtime.env.Scriptorium == nil {
|
result, err := checkDownstreamReadiness(runtime, targets, true, currentTranscriptUsableForNotarius(projection), runtime.opts.Force)
|
||||||
runtime.env.Scriptorium = runtime.dependencies.newScriptorium()
|
|
||||||
}
|
|
||||||
var checker preflight.AuditaChecker
|
|
||||||
var request audita.PolishRequest
|
|
||||||
if targets.Polish {
|
|
||||||
if runtime.env.Audita == nil {
|
|
||||||
runner, err := runtime.dependencies.newAudita(runtime.cfg)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("initialize audita runner: %w", err)
|
|
||||||
}
|
|
||||||
runtime.env.Audita = runner
|
|
||||||
}
|
|
||||||
var ok bool
|
|
||||||
checker, ok = runtime.env.Audita.(preflight.AuditaChecker)
|
|
||||||
if !ok || checker == nil {
|
|
||||||
return fmt.Errorf("audita runner does not support preflight")
|
|
||||||
}
|
|
||||||
root := artifacts.SessionInvocationProbeDir(runtime.paths, runtime.identity.RunID, "audita")
|
|
||||||
output := filepath.Join(root, "outputs", "transcripts", "polished.json")
|
|
||||||
request, _, err = stage.BuildPolishRequest(runtime.paths, runtime.manifest, "", output, filepath.Join(root, "logs"), filepath.Join(root, "debug"))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("build audita preflight: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result, err := preflight.CheckDownstream(runtime.ctx, runtime.env.Scriptorium, checker, targets, request)
|
|
||||||
if err == nil && targets.Notarius {
|
|
||||||
if runtime.env.Notarius == nil {
|
|
||||||
runtime.env.Notarius = runtime.dependencies.newNotarius()
|
|
||||||
}
|
|
||||||
native, ok := runtime.env.Notarius.(notarius.ReadinessChecker)
|
|
||||||
if !ok || native == nil {
|
|
||||||
return fmt.Errorf("notarius runner does not support preflight")
|
|
||||||
}
|
|
||||||
useCurrent := currentTranscriptUsableForNotarius(projection)
|
|
||||||
root := artifacts.SessionInvocationProbeDir(runtime.paths, runtime.identity.RunID, "notarius")
|
|
||||||
probe, buildErr := stage.BuildNotariusReadiness(runtime.cfg, runtime.paths, runtime.manifest, root, useCurrent, runtime.opts.Force)
|
|
||||||
if buildErr != nil {
|
|
||||||
return fmt.Errorf("build notarius preflight: %w", buildErr)
|
|
||||||
}
|
|
||||||
checked, checkErr := preflight.CheckNotarius(runtime.ctx, native, probe.Request)
|
|
||||||
result.Findings = append(result.Findings, checked.Findings...)
|
|
||||||
if verifyErr := probe.VerifyReferences(); verifyErr != nil {
|
|
||||||
checkErr = errors.Join(checkErr, fmt.Errorf("verify Notarius probe references: %w", verifyErr))
|
|
||||||
}
|
|
||||||
err = checkErr
|
|
||||||
}
|
|
||||||
for _, finding := range result.Findings {
|
for _, finding := range result.Findings {
|
||||||
if finding.Severity == preflight.SeverityWarning {
|
if finding.Severity == preflight.SeverityWarning {
|
||||||
message := formatPromptFinding(finding)
|
message := formatPromptFinding(finding)
|
||||||
|
|||||||
@@ -235,7 +235,12 @@ func inspectSessionValidation(ctx context.Context, cfg *config.Config, deps comm
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checked, checkErr := preflight.CheckDownstream(ctx, inspector, checker, targets, request)
|
checked, checkErr := preflight.Check(ctx, inspector, targets.Scriptorium)
|
||||||
|
if targets.Polish && ctx.Err() == nil {
|
||||||
|
auditaResult, auditaErr := preflight.CheckAudita(ctx, checker, request)
|
||||||
|
checked.Findings = append(checked.Findings, auditaResult.Findings...)
|
||||||
|
checkErr = errors.Join(checkErr, auditaErr)
|
||||||
|
}
|
||||||
if targets.Notarius && ctx.Err() == nil {
|
if targets.Notarius && ctx.Err() == nil {
|
||||||
nativeResult, nativeErr := preflight.CheckNotarius(ctx, native, probe.Request)
|
nativeResult, nativeErr := preflight.CheckNotarius(ctx, native, probe.Request)
|
||||||
checked.Findings = append(checked.Findings, nativeResult.Findings...)
|
checked.Findings = append(checked.Findings, nativeResult.Findings...)
|
||||||
|
|||||||
@@ -90,17 +90,3 @@ func CheckNotarius(ctx context.Context, checker notarius.ReadinessChecker, reque
|
|||||||
}
|
}
|
||||||
return result, ctx.Err()
|
return result, ctx.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckDownstream runs the selected checks and retains tool-specific findings.
|
|
||||||
func CheckDownstream(ctx context.Context, inspector Inspector, checker AuditaChecker, targets Targets, polish audita.PolishRequest) (Result, error) {
|
|
||||||
result, scriptErr := Check(ctx, inspector, targets.Scriptorium)
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return result, errors.Join(scriptErr, err)
|
|
||||||
}
|
|
||||||
if !targets.Polish {
|
|
||||||
return result, scriptErr
|
|
||||||
}
|
|
||||||
auditaResult, auditaErr := CheckAudita(ctx, checker, polish)
|
|
||||||
result.Findings = append(result.Findings, auditaResult.Findings...)
|
|
||||||
return result, errors.Join(scriptErr, auditaErr)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
||||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/scriptorium"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type auditaFunc func(context.Context, audita.PolishRequest) (audita.PreflightResult, error)
|
type auditaFunc func(context.Context, audita.PolishRequest) (audita.PreflightResult, error)
|
||||||
@@ -44,33 +43,6 @@ func TestNotariusReadinessRequiresCompleteMatchingEvidence(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDownstreamChecksSelectionAndPreservesFindings(t *testing.T) {
|
|
||||||
calls := 0
|
|
||||||
checker := auditaFunc(func(_ context.Context, r audita.PolishRequest) (audita.PreflightResult, error) {
|
|
||||||
calls++
|
|
||||||
if r.OutputProcessedPath != "output" {
|
|
||||||
t.Fatal(r)
|
|
||||||
}
|
|
||||||
return audita.PreflightResult{}, nil
|
|
||||||
})
|
|
||||||
inspector := inspectorFunc(func(context.Context, scriptorium.RequirementsRequest) (scriptorium.RequirementsReport, error) {
|
|
||||||
return scriptorium.RequirementsReport{Complete: false}, nil
|
|
||||||
})
|
|
||||||
if result, err := CheckDownstream(context.Background(), nil, nil, Targets{}, audita.PolishRequest{}); err != nil || len(result.Findings) != 0 {
|
|
||||||
t.Fatalf("empty: %+v %v", result, err)
|
|
||||||
}
|
|
||||||
targets := Targets{Polish: true, Scriptorium: []Target{{Stage: "trim"}}}
|
|
||||||
result, err := CheckDownstream(context.Background(), inspector, checker, targets, audita.PolishRequest{OutputProcessedPath: "output"})
|
|
||||||
if err != nil || calls != 1 || len(result.Findings) != 1 || result.Findings[0].Severity != SeverityWarning {
|
|
||||||
t.Fatalf("result: %+v %v", result, err)
|
|
||||||
}
|
|
||||||
targets.Polish = false
|
|
||||||
_, err = CheckDownstream(context.Background(), inspector, checker, targets, audita.PolishRequest{})
|
|
||||||
if err != nil || calls != 1 {
|
|
||||||
t.Fatalf("unselected: %d %v", calls, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDownstreamChecksPropagateFailureAndCancellation(t *testing.T) {
|
func TestDownstreamChecksPropagateFailureAndCancellation(t *testing.T) {
|
||||||
failure := errors.New("provider refused synthetic input")
|
failure := errors.New("provider refused synthetic input")
|
||||||
checker := auditaFunc(func(context.Context, audita.PolishRequest) (audita.PreflightResult, error) {
|
checker := auditaFunc(func(context.Context, audita.PolishRequest) (audita.PreflightResult, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user