Make run identities collision-resistant and outputs exclusive

This commit is contained in:
2026-07-18 14:21:26 +00:00
parent a39eea7ed6
commit 2111e01142
9 changed files with 367 additions and 75 deletions

View File

@@ -37,6 +37,7 @@ type Options struct {
Catalog pipeline.ModuleCatalog
Registries pipeline.Registries
LLMClientFactory LLMClientFactory
RunIDGenerator RunIDGenerator
LookupEnv func(string) (string, bool)
Now func() time.Time
UserCacheDir func() (string, error)
@@ -91,6 +92,9 @@ func normalizeOptions(opts Options) (Options, error) {
if opts.Now == nil {
opts.Now = time.Now
}
if opts.RunIDGenerator == nil {
opts.RunIDGenerator = defaultRunIDGenerator
}
if opts.UserCacheDir == nil {
opts.UserCacheDir = os.UserCacheDir
}
@@ -213,17 +217,25 @@ func runPipelineCommand(args []string, stdout, stderr io.Writer, opts Options) i
}
startedAt := opts.Now().UTC()
runID := fmt.Sprintf("run-%d", startedAt.UnixNano())
runID, err := opts.RunIDGenerator(startedAt)
if err != nil {
fmt.Fprintf(stderr, "notarius: generate run ID: %v\n", err)
return 1
}
if err := validateRunID(runID); err != nil {
fmt.Fprintf(stderr, "notarius: invalid generated run ID: %v\n", err)
return 1
}
var summary *debugbundle.SummaryWriter
debugPath := ""
debugRecorder := pipeline.NoopDebugRecorder()
if *debug {
bundle, err := debugbundle.Allocate(cfg.Debug.Directory)
bundle, err := debugbundle.Allocate(cfg.Debug.Directory, runID, startedAt)
if err != nil {
fmt.Fprintf(stderr, "notarius: %v\n", err)
return 1
}
runID, debugPath, summary = bundle.RunID(), bundle.Path(), bundle.Summary()
debugPath, summary = bundle.Path(), bundle.Summary()
debugRecorder, err = opts.DebugRecorderFactory(bundle.TraceRoot())
if err != nil {
return failPipelineCommand(stderr, summary, debugPath, fmt.Errorf("create debug recorder: %w", err), true)
@@ -515,8 +527,15 @@ func writeOutputFiles(runOutputDir string, files []contracts.OutputFile) error {
targets = append(targets, outputTarget{path: targetPath, file: file})
}
if err := os.MkdirAll(runOutputDir, 0o755); err != nil {
return fmt.Errorf("create output directory %q: %w", runOutputDir, err)
outputParent := filepath.Dir(runOutputDir)
if err := os.MkdirAll(outputParent, 0o755); err != nil {
return fmt.Errorf("create output parent %q: %w", outputParent, err)
}
if err := os.Mkdir(runOutputDir, 0o755); err != nil {
if os.IsExist(err) {
return fmt.Errorf("output run directory %q already exists", runOutputDir)
}
return fmt.Errorf("create output run directory %q: %w", runOutputDir, err)
}
for _, target := range targets {
if err := os.MkdirAll(filepath.Dir(target.path), 0o755); err != nil {