Record debug evidence in one run-scoped capture session
This commit is contained in:
@@ -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"}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user