2026-11-26 04:25:47 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-30 01:17:33 +00:00
|
|
|
"errors"
|
2026-11-26 04:25:47 +00:00
|
|
|
"fmt"
|
2026-11-27 16:45:46 +00:00
|
|
|
"path/filepath"
|
2026-11-26 04:25:47 +00:00
|
|
|
|
2026-11-27 16:45:46 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
2026-11-30 01:17:33 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
2026-11-26 04:25:47 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/preflight"
|
2026-11-27 16:45:46 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
2026-11-26 04:25:47 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-27 16:45:46 +00:00
|
|
|
func inspectInvocationRequirements(runtime *invocationRuntime, start int) error {
|
2026-11-30 01:17:33 +00:00
|
|
|
if !stagesContainAny(runtime.stages[start:], "polish", "trim", "extract", "analyze") {
|
2026-11-27 17:00:16 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-11-27 16:45:46 +00:00
|
|
|
remaining := runtime.plan
|
|
|
|
|
remaining.stages = append([]stage.Stage(nil), runtime.stages[start:]...)
|
|
|
|
|
projection, err := projectSelectedWork(runtime.ctx, runtime.cfg, remaining, runtime.opts.Force, runtime.opts.SelectedArtifacts, runtime.effectiveArtifacts, runtime.artifactStore, runtime.manifest)
|
2026-11-26 04:25:47 +00:00
|
|
|
if err != nil {
|
2026-11-27 16:45:46 +00:00
|
|
|
return fmt.Errorf("project downstream preflight work: %w", err)
|
2026-11-26 04:25:47 +00:00
|
|
|
}
|
2026-11-27 16:45:46 +00:00
|
|
|
targets, err := executionDownstreamTargets(runtime.cfg, runtime.identity.SessionID, projection)
|
2026-11-26 04:25:47 +00:00
|
|
|
if err != nil {
|
2026-11-27 16:45:46 +00:00
|
|
|
return fmt.Errorf("select downstream preflight targets: %w", err)
|
2026-11-26 04:25:47 +00:00
|
|
|
}
|
2026-11-30 01:17:33 +00:00
|
|
|
if !targets.Polish && !targets.Notarius && len(targets.Scriptorium) == 0 {
|
2026-11-26 04:25:47 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-11-27 16:45:46 +00:00
|
|
|
if len(targets.Scriptorium) > 0 && runtime.env.Scriptorium == nil {
|
2026-11-26 04:25:47 +00:00
|
|
|
runtime.env.Scriptorium = runtime.dependencies.newScriptorium()
|
|
|
|
|
}
|
2026-11-27 16:45:46 +00:00
|
|
|
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")
|
|
|
|
|
}
|
2026-11-30 01:17:33 +00:00
|
|
|
root := artifacts.SessionInvocationProbeDir(runtime.paths, runtime.identity.RunID, "audita")
|
2026-11-27 16:45:46 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-11-26 04:25:47 +00:00
|
|
|
}
|
2026-11-27 16:45:46 +00:00
|
|
|
result, err := preflight.CheckDownstream(runtime.ctx, runtime.env.Scriptorium, checker, targets, request)
|
2026-11-30 01:17:33 +00:00
|
|
|
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
|
|
|
|
|
}
|
2026-11-26 04:25:47 +00:00
|
|
|
for _, finding := range result.Findings {
|
|
|
|
|
if finding.Severity == preflight.SeverityWarning {
|
2026-11-30 01:17:33 +00:00
|
|
|
message := formatPromptFinding(finding)
|
|
|
|
|
if finding.Stage == "extract" {
|
|
|
|
|
message = "Notarius: " + finding.Message
|
|
|
|
|
}
|
|
|
|
|
runtime.env.Warn(message, "stage", finding.Stage, "artifact", finding.Artifact, "prompt", finding.PromptID, "issue", finding.IssueCode, "input", finding.InputName, "variable", finding.VariableName)
|
|
|
|
|
} else if finding.Severity == preflight.SeverityInfo && finding.Stage == "extract" {
|
|
|
|
|
runtime.env.Info(finding.Message)
|
2026-11-26 04:25:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-11-27 16:45:46 +00:00
|
|
|
return fmt.Errorf("downstream preflight: %w", err)
|
2026-11-26 04:25:47 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-11-26 04:29:18 +00:00
|
|
|
|
2026-11-30 01:17:33 +00:00
|
|
|
func currentTranscriptUsableForNotarius(projection *workProjection) bool {
|
|
|
|
|
for _, item := range projection.Stages {
|
|
|
|
|
if item.Name == "extract" {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if item.Decision.action == stageActionRun && (item.Name == "transcribe" || item.Name == "merge" || item.Name == "polish" || item.Name == "normalize" || item.Name == "trim") {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-26 04:29:18 +00:00
|
|
|
func formatPromptFinding(item preflight.Finding) string {
|
|
|
|
|
context := fmt.Sprintf("%s prompt %q", item.Stage, item.PromptID)
|
|
|
|
|
if item.Artifact != "" {
|
|
|
|
|
context = fmt.Sprintf("%s artifact %q prompt %q", item.Stage, item.Artifact, item.PromptID)
|
|
|
|
|
}
|
|
|
|
|
if item.IssueCode != "" {
|
|
|
|
|
return fmt.Sprintf("%s: %s (issue=%s input=%q variable=%q)", context, item.Message, item.IssueCode, item.InputName, item.VariableName)
|
|
|
|
|
}
|
|
|
|
|
return context + ": " + item.Message
|
|
|
|
|
}
|