Record debug evidence in one run-scoped capture session

This commit is contained in:
2026-11-22 02:49:48 +00:00
parent 584b041e57
commit 8680060400
10 changed files with 700 additions and 676 deletions

View File

@@ -1,4 +1,4 @@
// Package debugbundle owns explicitly requested per-run debug bundles.
// Package debugbundle allocates explicitly requested per-run debug directories.
package debugbundle
import (
@@ -10,38 +10,54 @@ import (
)
type Bundle struct {
path, summaryRoot, traceRoot string
createdAt time.Time
path string
createdAt time.Time
}
// Allocate starts a capture session. A link failure returns the already-created
// bundle as well as the error so callers can identify and retain available data.
func Allocate(parent, runID string, startedAt time.Time) (*Bundle, error) {
parent = strings.TrimSpace(parent)
if parent == "" {
return nil, fmt.Errorf("debug parent must not be empty")
}
if err := validateRunID(runID); err != nil {
return nil, err
}
parent, err := filepath.Abs(parent)
if err != nil {
return nil, fmt.Errorf("resolve debug parent: %w", err)
}
if err := os.MkdirAll(parent, 0o700); err != nil {
return nil, fmt.Errorf("create debug parent %q: %w", parent, err)
}
path := filepath.Join(parent, runID)
if err := os.Mkdir(path, 0o700); err != nil {
if os.IsExist(err) {
return nil, fmt.Errorf("debug bundle %q already exists", path)
}
return nil, fmt.Errorf("create debug bundle %q: %w", path, err)
directory := filepath.Join(parent, runID)
if err := os.Mkdir(directory, 0o700); err != nil {
return nil, fmt.Errorf("create debug bundle %q: %w", directory, err)
}
summary, trace := filepath.Join(path, "summary"), filepath.Join(path, "trace")
if err := os.Mkdir(summary, 0o700); err != nil {
_ = os.Remove(path)
return nil, fmt.Errorf("create debug summary %q: %w", summary, err)
bundle := &Bundle{path: directory, createdAt: startedAt}
if err := updateLatest(parent, runID); err != nil {
return bundle, fmt.Errorf("update debug latest for %q: %w", directory, err)
}
if err := os.Mkdir(trace, 0o700); err != nil {
_ = os.RemoveAll(path)
return nil, fmt.Errorf("create debug trace %q: %w", trace, err)
return bundle, nil
}
func updateLatest(parent, runID string) error {
temporary, err := os.CreateTemp(parent, ".latest-*")
if err != nil {
return err
}
return &Bundle{path: path, summaryRoot: summary, traceRoot: trace, createdAt: startedAt}, nil
name := temporary.Name()
defer os.Remove(name)
if err := temporary.Close(); err != nil {
return err
}
if err := os.Remove(name); err != nil {
return err
}
if err := os.Symlink(runID, name); err != nil {
return err
}
return os.Rename(name, filepath.Join(parent, "latest"))
}
func validateRunID(runID string) error {
@@ -59,18 +75,6 @@ func (b *Bundle) Path() string {
}
return b.path
}
func (b *Bundle) SummaryRoot() string {
if b == nil {
return ""
}
return b.summaryRoot
}
func (b *Bundle) TraceRoot() string {
if b == nil {
return ""
}
return b.traceRoot
}
func (b *Bundle) RunID() string {
if b == nil {
return ""
@@ -83,9 +87,3 @@ func (b *Bundle) CreatedAt() time.Time {
}
return b.createdAt
}
func (b *Bundle) Summary() *SummaryWriter {
if b == nil {
return nil
}
return &SummaryWriter{root: b.summaryRoot, runID: b.RunID(), createdAt: b.createdAt}
}

View File

@@ -1,252 +1,140 @@
package debugbundle
import (
"bytes"
"encoding/json"
"errors"
"io"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
)
const testBundleRunID = "run-42-00000000000000000000000000000001"
func TestAllocateCreatesRestrictiveSummaryAndTrace(t *testing.T) {
parent := t.TempDir()
fixed := time.Unix(0, 42).UTC()
bundle, err := Allocate(parent, testBundleRunID, fixed)
func TestAllocateStartsOneDirectoryWithRelativeLatest(t *testing.T) {
t.Chdir(t.TempDir())
started := time.Unix(42, 0).UTC()
bundle, err := Allocate("debug root ", "run-first", started)
if err != nil {
t.Fatal(err)
}
if bundle.RunID() != testBundleRunID || bundle.CreatedAt() != fixed || bundle.SummaryRoot() != filepath.Join(bundle.Path(), "summary") || bundle.TraceRoot() != filepath.Join(bundle.Path(), "trace") {
if !filepath.IsAbs(bundle.Path()) || bundle.RunID() != "run-first" || bundle.CreatedAt() != started {
t.Fatalf("bundle=%#v", bundle)
}
for _, path := range []string{bundle.Path(), bundle.SummaryRoot(), bundle.TraceRoot()} {
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o700 {
t.Fatalf("%s mode=%#o", path, info.Mode().Perm())
}
names, err := os.ReadDir(bundle.Path())
if err != nil || len(names) != 0 {
t.Fatalf("unexpected summary/trace hierarchy: %v %v", names, err)
}
if err := bundle.Summary().WriteError("failed"); err != nil {
t.Fatal(err)
target, err := os.Readlink(filepath.Join(filepath.Dir(bundle.Path()), "latest"))
if err != nil || target != bundle.RunID() || filepath.IsAbs(target) {
t.Fatalf("latest=%q %v", target, err)
}
info, err := os.Stat(filepath.Join(bundle.SummaryRoot(), ArtifactErrorLog))
}
func TestAllocateRetainsExistingBundleOnCollision(t *testing.T) {
parent := t.TempDir()
bundle, err := Allocate(parent, "first", time.Now())
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o600 {
t.Fatalf("file mode=%#o", info.Mode().Perm())
}
}
func TestAllocateRejectsExistingBundleWithoutChangingIt(t *testing.T) {
parent := t.TempDir()
bundlePath := filepath.Join(parent, testBundleRunID)
if err := os.Mkdir(bundlePath, 0o700); err != nil {
sentinel := filepath.Join(bundle.Path(), "sentinel")
if err := os.WriteFile(sentinel, []byte("existing evidence"), 0o600); err != nil {
t.Fatal(err)
}
sentinelPath := filepath.Join(bundlePath, "sentinel")
sentinel := []byte("existing bundle")
if err := os.WriteFile(sentinelPath, sentinel, 0o600); err != nil {
t.Fatal(err)
if _, err := Allocate(parent, "first", time.Now()); !errors.Is(err, os.ErrExist) {
t.Fatalf("collision=%v", err)
}
if _, err := Allocate(parent, testBundleRunID, time.Unix(0, 42)); err == nil || !strings.Contains(err.Error(), "already exists") {
t.Fatalf("Allocate() error = %v, want collision", err)
got, err := os.ReadFile(sentinel)
if err != nil || string(got) != "existing evidence" {
t.Fatalf("evidence=%q %v", got, err)
}
if got, err := os.ReadFile(sentinelPath); err != nil || !bytes.Equal(got, sentinel) {
t.Fatalf("sentinel = %q, %v", got, err)
target, err := os.Readlink(filepath.Join(parent, "latest"))
if err != nil || target != "first" {
t.Fatalf("collision changed latest: %q %v", target, err)
}
}
func TestAllocateRejectsUnsafeRunIDsBeforeCreatingParent(t *testing.T) {
for _, runID := range []string{"", ".", "..", "../escape", `..\\escape`, "/absolute", " trailing "} {
t.Run(runID, func(t *testing.T) {
for _, id := range []string{"", ".", "..", "../escape", "/absolute", " spaced ", string([]byte{'a', 92, 'b'})} {
t.Run(id, func(t *testing.T) {
parent := filepath.Join(t.TempDir(), "debug")
if _, err := Allocate(parent, runID, time.Time{}); err == nil {
t.Fatalf("Allocate(%q) succeeded", runID)
if _, err := Allocate(parent, id, time.Now()); err == nil {
t.Fatal("unsafe run ID accepted")
}
if _, err := os.Stat(parent); !os.IsNotExist(err) {
t.Fatalf("debug parent exists or stat failed: %v", err)
t.Fatalf("parent exists: %v", err)
}
})
}
}
func TestSummaryWriterWritesEverySummaryArtifact(t *testing.T) {
bundle, err := Allocate(t.TempDir(), testBundleRunID, time.Unix(0, 42))
if err != nil {
t.Fatal(err)
}
summary := bundle.Summary()
if err := summary.WriteInvocation(Invocation{Operation: "run"}); err != nil {
t.Fatal(err)
}
if err := summary.WriteEffectiveConfig(testEffectiveConfigWriter{content: "version: 5\n"}); err != nil {
t.Fatal(err)
}
if err := summary.WriteResolvedPipeline(testRedactedResolvedPipelinePayload{}); err != nil {
t.Fatal(err)
}
if err := summary.WriteResolvedReferences(nil); err != nil {
t.Fatal(err)
}
if err := summary.WriteCheckpointEvents(nil); err != nil {
t.Fatal(err)
}
if err := summary.WriteRunManifest(artifacts.RunManifest{RunID: bundle.RunID()}); err != nil {
t.Fatal(err)
}
if err := summary.WriteChunkPlan(artifacts.ChunkPlanSummary{}); err != nil {
t.Fatal(err)
}
if err := summary.WriteRunReport(RunReport{RunID: bundle.RunID(), PipelineID: "test"}); err != nil {
t.Fatal(err)
}
if err := summary.WriteDiagnostics(contracts.DiagnosticCollection{}); err != nil {
t.Fatal(err)
}
if err := summary.WriteError("failed"); err != nil {
t.Fatal(err)
}
for _, name := range []string{
ArtifactInvocationMetadata,
ArtifactEffectiveConfigYAML,
ArtifactResolvedPipeline,
ArtifactResolvedReferences,
ArtifactCheckpointEvents,
ArtifactRunManifest,
ArtifactChunkPlan,
ArtifactRunReport,
ArtifactDiagnostics,
ArtifactErrorLog,
} {
info, err := os.Stat(filepath.Join(bundle.SummaryRoot(), name))
if err != nil {
t.Fatalf("summary artifact %q: %v", name, err)
func TestAllocateFailureLeavesAvailableDataWithoutFallbackOrDeletion(t *testing.T) {
t.Run("parent is a file", func(t *testing.T) {
parent := filepath.Join(t.TempDir(), "debug")
if err := os.WriteFile(parent, []byte("existing"), 0o600); err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o600 {
t.Fatalf("summary artifact %q mode=%#o", name, info.Mode().Perm())
if bundle, err := Allocate(parent, "run", time.Now()); err == nil || bundle != nil {
t.Fatalf("bundle=%#v error=%v", bundle, err)
}
}
got, err := os.ReadFile(parent)
if err != nil || string(got) != "existing" {
t.Fatalf("parent changed: %q %v", got, err)
}
})
t.Run("latest cannot be replaced", func(t *testing.T) {
parent := t.TempDir()
latest := filepath.Join(parent, "latest")
if err := os.Mkdir(latest, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(latest, "sentinel"), []byte("existing"), 0o600); err != nil {
t.Fatal(err)
}
bundle, err := Allocate(parent, "run", time.Now())
if err == nil || bundle == nil {
t.Fatalf("available bundle lost: %#v %v", bundle, err)
}
if _, err := os.Stat(bundle.Path()); err != nil {
t.Fatal(err)
}
if got, err := os.ReadFile(filepath.Join(latest, "sentinel")); err != nil || string(got) != "existing" {
t.Fatalf("latest directory changed: %q %v", got, err)
}
names, err := os.ReadDir(parent)
if err != nil || len(names) != 2 {
t.Fatalf("temporary link left behind: %v %v", names, err)
}
})
}
func TestWriteEffectiveConfigDoesNotPublishPartialRendering(t *testing.T) {
bundle, err := Allocate(t.TempDir(), testBundleRunID, time.Unix(0, 42))
if err != nil {
t.Fatal(err)
func TestConcurrentAllocationLeavesAllSessionsAndAValidLatestLink(t *testing.T) {
parent := t.TempDir()
const sessions = 8
var wg sync.WaitGroup
failures := make(chan error, sessions)
for i := range sessions {
wg.Add(1)
go func(i int) {
defer wg.Done()
_, err := Allocate(parent, time.Unix(int64(i), 0).Format("150405"), time.Now())
failures <- err
}(i)
}
if err := bundle.Summary().WriteEffectiveConfig(testEffectiveConfigWriter{err: errors.New("render failed")}); err == nil {
t.Fatal("WriteEffectiveConfig() error = nil, want render failure")
}
path := filepath.Join(bundle.SummaryRoot(), ArtifactEffectiveConfigYAML)
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatalf("effective configuration artifact exists after rendering failure: %v", err)
}
}
func TestWriteInvocationPreservesReasoningEffortOverrideStates(t *testing.T) {
replacement := "focused"
cleared := ""
tests := []struct {
name string
override *string
wantValue string
wantSet bool
}{
{name: "inherit"},
{name: "replace", override: &replacement, wantValue: "focused", wantSet: true},
{name: "clear", override: &cleared, wantSet: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bundle, err := Allocate(t.TempDir(), testBundleRunID, time.Unix(0, 42))
if err != nil {
t.Fatal(err)
}
if err := bundle.Summary().WriteInvocation(Invocation{
Operation: "run",
ReasoningEffortOverride: tt.override,
}); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(bundle.SummaryRoot(), ArtifactInvocationMetadata))
if err != nil {
t.Fatal(err)
}
var payload map[string]any
if err := json.Unmarshal(data, &payload); err != nil {
t.Fatal(err)
}
value, found := payload["reasoning_effort_override"]
if found != tt.wantSet || (found && value != tt.wantValue) {
t.Fatalf("reasoning override found=%t value=%#v, want found=%t value=%q; JSON=%s", found, value, tt.wantSet, tt.wantValue, data)
}
})
}
}
func TestWriteInvocationOmitsEmptySessionID(t *testing.T) {
for _, sessionID := range []string{"", "session-123"} {
bundle, err := Allocate(t.TempDir(), testBundleRunID, time.Unix(0, 42))
wg.Wait()
close(failures)
for err := range failures {
if err != nil {
t.Fatal(err)
}
if err := bundle.Summary().WriteInvocation(Invocation{Operation: "run", SessionID: sessionID}); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(bundle.SummaryRoot(), ArtifactInvocationMetadata))
if err != nil {
t.Fatal(err)
}
var payload map[string]any
if err := json.Unmarshal(data, &payload); err != nil {
t.Fatal(err)
}
value, found := payload["session_id"]
if found != (sessionID != "") || (found && value != sessionID) {
t.Fatalf("session found=%t value=%#v, want found=%t value=%q; JSON=%s", found, value, sessionID != "", sessionID, data)
}
}
names, err := os.ReadDir(parent)
if err != nil || len(names) != sessions+1 {
t.Fatalf("sessions=%v %v", names, err)
}
target, err := os.Readlink(filepath.Join(parent, "latest"))
if err != nil || filepath.IsAbs(target) {
t.Fatalf("latest=%q %v", target, err)
}
if info, err := os.Stat(filepath.Join(parent, target)); err != nil || !info.IsDir() {
t.Fatalf("latest target unavailable: %v", err)
}
}
func TestSummaryWriterInternalWritesConfineArtifacts(t *testing.T) {
bundle, err := Allocate(t.TempDir(), testBundleRunID, time.Unix(0, 42))
if err != nil {
t.Fatal(err)
}
if err := bundle.Summary().writeJSON("../outside.json", map[string]any{}); err == nil {
t.Fatal("accepted traversal")
}
if err := bundle.Summary().writeBytes(`trace\\x`, []byte("x")); err == nil {
t.Fatal("accepted backslash")
}
}
type testEffectiveConfigWriter struct {
content string
err error
}
func (writer testEffectiveConfigWriter) WriteEffective(output io.Writer) error {
if writer.err != nil {
return writer.err
}
_, err := io.WriteString(output, writer.content)
return err
}
type testRedactedResolvedPipelinePayload struct{}
func (testRedactedResolvedPipelinePayload) RedactedResolvedPipelinePayload() pipeline.ResolvedPipeline {
return pipeline.ResolvedPipeline{ID: "redacted"}
}

View File

@@ -1,145 +0,0 @@
package debugbundle
import (
"bytes"
"fmt"
"io"
"strings"
"time"
"gitea.maximumdirect.net/eric/notarius/internal/core/artifacts"
"gitea.maximumdirect.net/eric/notarius/internal/core/fileio"
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
)
const (
ArtifactInvocationMetadata = "invocation.json"
ArtifactEffectiveConfigYAML = "effective-config.yml"
ArtifactResolvedPipeline = "resolved-pipeline.json"
ArtifactResolvedReferences = "resolved-references.json"
ArtifactCheckpointEvents = "checkpoint-events.json"
ArtifactRunManifest = "run-manifest.json"
ArtifactChunkPlan = "chunk-plan.json"
ArtifactRunReport = "run-report.json"
ArtifactDiagnostics = "final-diagnostics.json"
ArtifactErrorLog = "error.log"
)
type EffectiveConfigWriter interface{ WriteEffective(io.Writer) error }
type RedactedResolvedPipelinePayload interface {
RedactedResolvedPipelinePayload() pipeline.ResolvedPipeline
}
type Invocation struct {
Operation string `json:"operation"`
PipelineID string `json:"pipeline_id,omitempty"`
PipelineDigest string `json:"pipeline_digest,omitempty"`
Resume bool `json:"resume,omitempty"`
RecomputeStep string `json:"recompute_step,omitempty"`
InputPath string `json:"input_path,omitempty"`
ConfigPath string `json:"config_path,omitempty"`
ConfigSource string `json:"config_source,omitempty"`
ConfigOverlay string `json:"config_overlay,omitempty"`
OnlyLanes []string `json:"only_lanes,omitempty"`
ChunkCacheOverride string `json:"chunk_cache_override,omitempty"`
ReasoningEffortOverride *string `json:"reasoning_effort_override,omitempty"`
SessionID string `json:"session_id,omitempty"`
RunID string `json:"run_id"`
StartedAt time.Time `json:"started_at"`
}
type RunReport struct {
RunID string `json:"run_id"`
PipelineID string `json:"pipeline_id"`
OutputPath string `json:"output_path,omitempty"`
DebugPath string `json:"debug_path,omitempty"`
Succeeded bool `json:"succeeded"`
OutputCount int `json:"output_count"`
RejectedCount int `json:"rejected_count"`
WarningGroupCount int `json:"warning_group_count"`
WarningOccurrenceCount int `json:"warning_occurrence_count"`
DiagnosticGroupCount int `json:"diagnostic_group_count"`
DiagnosticOccurrenceCount int `json:"diagnostic_occurrence_count"`
DiagnosticsTruncated bool `json:"diagnostics_truncated"`
ValidationStatus string `json:"validation_status,omitempty"`
}
type SummaryWriter struct {
root, runID string
createdAt time.Time
}
func (w *SummaryWriter) WriteInvocation(payload Invocation) error {
if w == nil {
return fmt.Errorf("debug summary writer must not be nil")
}
if payload.RunID == "" {
payload.RunID = w.runID
}
if payload.StartedAt.IsZero() {
payload.StartedAt = w.createdAt
}
if payload.ReasoningEffortOverride != nil {
value := *payload.ReasoningEffortOverride
payload.ReasoningEffortOverride = &value
}
return w.writeJSON(ArtifactInvocationMetadata, payload)
}
func (w *SummaryWriter) WriteEffectiveConfig(payload EffectiveConfigWriter) error {
if payload == nil {
return fmt.Errorf("effective configuration writer must not be nil")
}
var rendered bytes.Buffer
if err := payload.WriteEffective(&rendered); err != nil {
return fmt.Errorf("render effective configuration: %w", err)
}
return w.writeBytes(ArtifactEffectiveConfigYAML, rendered.Bytes())
}
func (w *SummaryWriter) WriteResolvedPipeline(payload RedactedResolvedPipelinePayload) error {
if payload == nil {
return fmt.Errorf("redacted resolved pipeline payload must not be nil")
}
return w.writeJSON(ArtifactResolvedPipeline, payload.RedactedResolvedPipelinePayload())
}
func (w *SummaryWriter) WriteResolvedReferences(v []artifacts.ReferenceProvenance) error {
return w.writeJSON(ArtifactResolvedReferences, v)
}
func (w *SummaryWriter) WriteCheckpointEvents(v []pipeline.CheckpointEvent) error {
return w.writeJSON(ArtifactCheckpointEvents, v)
}
func (w *SummaryWriter) WriteRunManifest(v artifacts.RunManifest) error {
return w.writeJSON(ArtifactRunManifest, v)
}
func (w *SummaryWriter) WriteChunkPlan(v artifacts.ChunkPlanSummary) error {
return w.writeJSON(ArtifactChunkPlan, v)
}
func (w *SummaryWriter) WriteRunReport(v RunReport) error { return w.writeJSON(ArtifactRunReport, v) }
func (w *SummaryWriter) WriteDiagnostics(v contracts.DiagnosticCollection) error {
return w.writeJSON(ArtifactDiagnostics, contracts.CloneDiagnosticCollection(v))
}
func (w *SummaryWriter) WriteError(message string) error {
return w.writeBytes(ArtifactErrorLog, []byte(message+"\n"))
}
func (w *SummaryWriter) writeJSON(name string, v any) error {
if w == nil {
return fmt.Errorf("debug summary writer must not be nil")
}
if err := fileio.WriteJSON(w.root, summaryName(name), v, 0o700, 0o600); err != nil {
return fmt.Errorf("write debug summary artifact %q: %w", name, err)
}
return nil
}
func (w *SummaryWriter) writeBytes(name string, v []byte) error {
if w == nil {
return fmt.Errorf("debug summary writer must not be nil")
}
if err := fileio.WriteBytes(w.root, summaryName(name), v, 0o700, 0o600); err != nil {
return fmt.Errorf("write debug summary artifact %q: %w", name, err)
}
return nil
}
func summaryName(name string) string {
name = strings.TrimSpace(name)
if name == "" || strings.ContainsAny(name, "/\\") {
return "../invalid"
}
return name
}