diff --git a/docs/internal/diagnostics-reporting.md b/docs/internal/diagnostics-reporting.md index 33d135b..5238d62 100644 --- a/docs/internal/diagnostics-reporting.md +++ b/docs/internal/diagnostics-reporting.md @@ -20,16 +20,19 @@ validator entries yield `validation_decisions_filtered` only for a successful operation; they do not count as filtered correction proposals. Failed operations retain their execution counts and never produce a recovery record. -`internal/core/diagnostics.OutputSession` owns active destination checks, -transcript preflight, report initialization, regular-file replacement, and direct -stream publication. Transcript input is protected from early report writes but +`internal/core/diagnostics.OutputSession` owns destination selection, report +initialization, and publication. Transcript and report files share a staging +primitive that writes beside the resolved destination. Streams are opened for +delivery. Transcript input is protected from early report writes but may be the final transcript destination. The LLM runtime records consumed PromptKit sources and effective host message files and supplies those source paths to CLI; no second filename-based discovery rule is used. CLI composes it after configuration, transcript acquisition, and preflight readiness and before dispatching model work. It does not create ordinary report parents. -On a final write error it attempts other enabled reports and one failed-status -pass; a published transcript is never erased by a later report failure. +Final report rendering uses the delivery and cleanup errors already observed. +If a report write first fails, the remaining writes use the failed bundle and +one pass attempts to update the reports still missing failed evidence. A +published transcript remains available after a later report failure. Debug is independent. The shared `--debug` flag enables a unique retained child under `diagnostics.work_dir`. The CLI creates one interaction sink rooted in diff --git a/docs/operations.md b/docs/operations.md index f639e45..ceae8f7 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -34,15 +34,20 @@ untouched until successful publication. Pipes and devices are opened only for delivery, and a pipe can wait for its reader. If a transcript destination changes between a regular file and a pipe or device during processing, Audita can report a delivery failure while preserving the new destination. Parent directories for -required outputs must already exist. +required outputs must already exist. Publication uses the destinations resolved +during preparation. Give concurrent invocations separate destinations, and +choose separate report directories when retaining reports from multiple runs. Enabled reports are initialized as `in_progress` before processing. The transcript is published before final reports; the files do not form a single transaction. A late report failure may therefore leave a published transcript. -Fatal errors and cancellation clean unpublished temporary resources and attempt -failed-status final reports. An uncatchable termination can leave `in_progress` -evidence. Read exit status and reports together rather than treating a file's -existence as proof of success. +After a required write failure or cancellation, Audita attempts to clean +unpublished temporary resources and publish failed-status reports. A cleanup +failure is reported with the earlier error. Cancellation observed after a +stream write still fails the invocation, even when some or all bytes reached +the reader. If failed reports cannot be rendered, available report evidence +remains. An uncatchable termination can leave `in_progress` evidence. Use the +exit status and report status to determine the outcome. ## Debug evidence @@ -50,7 +55,9 @@ The shared `--debug` flag enables retained artifacts in a run directory beneath `diagnostics.work_dir`; selecting a work directory alone does not enable capture. Optional debug preparation or writes can produce warnings without changing correction decisions or the required output result. Debug material can -include transcript content, prompts, responses, and correction decisions. Protect this material according to the sensitivity of the inputs and responses. Repair interactions +include transcript content, prompts, responses, and correction decisions. +Protect this material according to the sensitivity of the inputs and responses +and manage its retention. Repair interactions and revalidation artifacts include proposal and attempt coordinates; the correction ledger connects them to the original edit. diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 710a723..a1bb50a 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -186,6 +186,13 @@ after I/O, cleanup errors, initialized evidence when rendering fails, and write behavior after known failure. Observe publication attempts as well as final files. Leave processing fact and ledger projection to Stage 17. +**Complete.** Required outputs use the destinations resolved during selection +and share regular-file staging. Publication checks cancellation, retains and +reports cleanup failures, and uses failed-status reports when required delivery +cannot complete. Tests verified publication attempts, retained evidence when +rendering failed, symlink destinations, stream cancellation, and Markdown path +escaping. The CLI build and full offline Go suite passed. + ## Stage 5 — Consolidate typed transcript data Audit #13, #31, #51, #52, #54, #62 (file helper). diff --git a/internal/cli/process_inputs.go b/internal/cli/process_inputs.go index c21a36a..0efec3c 100644 --- a/internal/cli/process_inputs.go +++ b/internal/cli/process_inputs.go @@ -5,6 +5,7 @@ import ( "context" "crypto/rand" "encoding/hex" + "errors" "fmt" "io" "path/filepath" @@ -43,7 +44,13 @@ func loadProcessInputs(ctx context.Context, input processInput, mode commandkit. if err != nil { return nil, err } + if err := ctx.Err(); err != nil { + return nil, err + } raw, err := coreio.ReadRequiredFile(input.Transcript, "transcript") + if ctx.Err() != nil { + return nil, errors.Join(err, ctx.Err()) + } if err != nil { return nil, err } @@ -79,7 +86,10 @@ func prepareProcess(ctx context.Context, input processInput, loaded *processInpu if cfg.Reporting.Enabled { reportDir = cfg.Reporting.StdoutDir if input.Output.Value != "" && !reportDirSet { - reportDir = filepath.Dir(input.Output.Value) + reportDir, _ = filepath.Split(input.Output.Value) + if reportDir == "" { + reportDir = "." + } } } runID, err := generateRunID() @@ -88,7 +98,7 @@ func prepareProcess(ctx context.Context, input processInput, loaded *processInpu } started := time.Now().UTC() var initial diagnostics.ReportBundle - var failed func(error, []diagnostics.FileFailure) diagnostics.ReportBundle + var failed diagnostics.ReportRenderer if cfg.Reporting.Enabled { snapshot := reporting.Snapshot{RunID: runID, Status: reporting.StatusInProgress, StartedAt: started, TranscriptPath: input.Transcript, OutputPath: input.Output.Value} for index, item := range cfg.Pipeline.Stages { @@ -98,15 +108,14 @@ func prepareProcess(ctx context.Context, input processInput, loaded *processInpu if err != nil { return nil, err } - failed = func(primary error, failures []diagnostics.FileFailure) diagnostics.ReportBundle { + failed = func(primary error, failures []diagnostics.FileFailure) (diagnostics.ReportBundle, error) { failedSnapshot := snapshot completed := time.Now().UTC() failedSnapshot.CompletedAt = &completed if primary != nil { failedSnapshot.Errors = []reporting.ErrorRecord{{Scope: "run", Phase: "initialization", Reason: "canceled", Message: "Processing was canceled."}} } - bundle, _ := renderReportBundle(failedOutputSnapshot(failedSnapshot, failures)) - return bundle + return renderReportBundle(failedOutputSnapshot(failedSnapshot, failures)) } } var debug *preflight.DebugInput diff --git a/internal/cli/process_output.go b/internal/cli/process_output.go index 539645f..64988d4 100644 --- a/internal/cli/process_output.go +++ b/internal/cli/process_output.go @@ -21,41 +21,22 @@ func finishProcess(inv commandkit.Invocation, input processInput, prepared *prep completed := time.Now().UTC() snapshot.RunID, snapshot.StartedAt, snapshot.CompletedAt = prepared.RunID, prepared.Started, &completed snapshot.TranscriptPath, snapshot.OutputPath = input.Transcript, input.Output.Value - if primary == nil { - primary = inv.Context.Err() - } snapshot.Status = reporting.StatusCompleted - if primary != nil { - snapshot = failedProcessingSnapshot(snapshot, primary, inv.Context.Err()) - } reportPaths := prepared.Prepared.Output.ReportPaths() - var reports diagnostics.ReportBundle - var failed func([]diagnostics.FileFailure) diagnostics.ReportBundle + var render diagnostics.ReportRenderer if len(reportPaths) > 0 { - var err error - reports, err = renderReportBundle(snapshot) - if err != nil { - if primary == nil { - primary = err + render = func(cause error, failures []diagnostics.FileFailure) (diagnostics.ReportBundle, error) { + report := snapshot + if cause != nil { + report = failedProcessingSnapshot(report, cause, inv.Context.Err()) } - snapshot.Status = reporting.StatusFailed - } - failed = func(failures []diagnostics.FileFailure) diagnostics.ReportBundle { - result, bundleErr := renderReportBundle(failedOutputSnapshot(snapshot, failures)) - if bundleErr != nil { - return reports + if len(failures) > 0 { + report = failedOutputSnapshot(report, failures) } - return result + return renderReportBundle(report) } } - if canceled := inv.Context.Err(); primary == nil && canceled != nil { - primary = canceled - snapshot = failedProcessingSnapshot(snapshot, canceled, canceled) - if failed != nil { - reports = failed(nil) - } - } - result := prepared.Prepared.Output.Finish(primary, transcript, reports, failed) + result := prepared.Prepared.Output.Finish(inv.Context, primary, transcript, render) if result != nil { return commandkit.Failure(result.Error()).WithCause(result) } diff --git a/internal/core/diagnostics/output_session.go b/internal/core/diagnostics/output_session.go index cf61dba..beb02e6 100644 --- a/internal/core/diagnostics/output_session.go +++ b/internal/core/diagnostics/output_session.go @@ -1,6 +1,7 @@ package diagnostics import ( + "context" "errors" "fmt" "io" @@ -45,80 +46,123 @@ func (f FileFailure) Unwrap() error { return f.Err } // OutputSession owns checked destinations and per-file publication. Filesystem // failures remain typed so CLI can present contextual diagnostics. type OutputSession struct { - transcript string - reports [3]string - enabled bool - replace func(string, []byte) error - temp *os.File - tempPath string - tempDir string - publishPath string - direct bool - stdout io.Writer + transcript string + reports [3]string + reportDestinations [3]string + enabled bool + replace func(context.Context, string, []byte) error + transcriptFile stagedFile + pendingCleanup []*stagedFile + publishPath string + direct bool + stdout io.Writer } // SelectOutput checks active destinations before any output resources are opened. func SelectOutput(selection OutputSelection) (*OutputSession, error) { - session := &OutputSession{enabled: selection.ReportingEnabled, replace: atomicReplace, stdout: selection.Stdout} - active := make([]string, 0, 4) - if selection.TranscriptPath != "" { - session.transcript = selection.TranscriptPath - active = append(active, session.transcript) + session := &OutputSession{enabled: selection.ReportingEnabled, stdout: selection.Stdout} + session.replace = session.replaceReport + type destination struct { + display string + path string + info os.FileInfo } + check := func(path string) (destination, error) { + resolved, err := canonicalDestination(path) + if err != nil { + return destination{}, err + } + info, err := os.Stat(resolved) + if err != nil && !errors.Is(err, os.ErrNotExist) { + return destination{}, err + } + return destination{display: path, path: resolved, info: info}, nil + } + var resolvedDir string if session.enabled { if strings.TrimSpace(selection.ReportDir) == "" { return nil, fmt.Errorf("report directory is required") } - info, err := os.Stat(selection.ReportDir) + var err error + resolvedDir, err = filepath.EvalSymlinks(selection.ReportDir) + if err != nil { + return nil, fmt.Errorf("report directory: %w", err) + } + info, err := os.Stat(resolvedDir) if err != nil { return nil, fmt.Errorf("report directory: %w", err) } if !info.IsDir() { return nil, fmt.Errorf("report destination is not a directory") } - session.reports = [3]string{ - filepath.Join(selection.ReportDir, ErrorsFile), - filepath.Join(selection.ReportDir, RecoveriesFile), - filepath.Join(selection.ReportDir, MarkdownFile), - } - active = append(active, session.reports[:]...) } - protected := make([]string, 0, len(selection.ProtectedPaths)) + var active []destination + if selection.TranscriptPath != "" { + target, err := check(selection.TranscriptPath) + if err != nil { + return nil, fmt.Errorf("prepare transcript output %q: %w", selection.TranscriptPath, err) + } + if target.info != nil && target.info.IsDir() { + return nil, fmt.Errorf("transcript output %q is a directory", target.display) + } + session.transcript, session.publishPath = target.display, target.path + session.direct = target.info != nil && !target.info.Mode().IsRegular() + active = append(active, target) + } + if session.enabled { + label := selection.ReportDir + if !os.IsPathSeparator(label[len(label)-1]) { + label += string(filepath.Separator) + } + for i, name := range [...]string{ErrorsFile, RecoveriesFile, MarkdownFile} { + target, err := check(filepath.Join(resolvedDir, name)) + if err != nil { + return nil, fmt.Errorf("report output %q: %w", label+name, err) + } + target.display = label + name + if target.info != nil && !target.info.Mode().IsRegular() { + return nil, fmt.Errorf("report output %q is not a regular file", target.display) + } + session.reports[i], session.reportDestinations[i] = target.display, target.path + active = append(active, target) + } + } + same := func(a, b destination) bool { + return a.path == b.path || (a.info != nil && b.info != nil && os.SameFile(a.info, b.info)) + } + if len(active) == 0 { + return session, nil + } + var protected []destination for _, path := range selection.ProtectedPaths { if path != "" { - protected = append(protected, path) + source, err := check(path) + if err != nil { + return nil, fmt.Errorf("protected source %q: %w", path, err) + } + protected = append(protected, source) } } - for i, path := range active { - if path != session.transcript { - if info, err := os.Stat(path); err == nil { - if !info.Mode().IsRegular() { - return nil, fmt.Errorf("report output %q is not a regular file", path) - } - } else if !errors.Is(err, os.ErrNotExist) { - return nil, fmt.Errorf("report output %q: %w", path, err) + var input destination + if session.enabled && selection.InputTranscriptPath != "" { + var err error + input, err = check(selection.InputTranscriptPath) + if err != nil { + return nil, fmt.Errorf("transcript input %q: %w", selection.InputTranscriptPath, err) + } + } + for i, target := range active { + for _, source := range protected { + if same(target, source) { + return nil, fmt.Errorf("output destination %q conflicts with input %q", target.display, source.display) } } - sources := protected - if path != session.transcript && selection.InputTranscriptPath != "" { - sources = append(append([]string(nil), protected...), selection.InputTranscriptPath) - } - for _, source := range sources { - collision, err := sameDestination(path, source) - if err != nil { - return nil, err - } - if collision { - return nil, fmt.Errorf("output destination %q conflicts with input %q", path, source) - } + if (session.transcript == "" || i > 0) && input.path != "" && same(target, input) { + return nil, fmt.Errorf("output destination %q conflicts with input %q", target.display, input.display) } for _, prior := range active[:i] { - collision, err := sameDestination(path, prior) - if err != nil { - return nil, err - } - if collision { - return nil, fmt.Errorf("output destinations %q and %q conflict", path, prior) + if same(target, prior) { + return nil, fmt.Errorf("output destinations %q and %q conflict", target.display, prior.display) } } } @@ -142,40 +186,17 @@ func (s *OutputSession) Prepare() error { if s == nil { return fmt.Errorf("output session is required") } - if s.transcript == "" || s.temp != nil || s.direct { + if s.transcript == "" || s.transcriptFile.file != nil || s.direct { return nil } - info, err := os.Stat(s.transcript) - if err == nil && info.IsDir() { - return fmt.Errorf("transcript output %q is a directory", s.transcript) - } - if err == nil && !info.Mode().IsRegular() { - s.direct = true - return nil - } - if err != nil && !errors.Is(err, os.ErrNotExist) { - return fmt.Errorf("transcript output %q: %w", s.transcript, err) - } - s.publishPath, err = canonicalDestination(s.transcript) - if err != nil { - return err - } - s.tempDir, err = os.MkdirTemp(filepath.Dir(s.publishPath), ".audita-output-*") - if err != nil { - return fmt.Errorf("prepare transcript output %q: %w", s.transcript, err) - } - s.tempPath = filepath.Join(s.tempDir, "payload") - s.temp, err = os.OpenFile(s.tempPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644) - if err != nil { - _ = s.Close() - return fmt.Errorf("prepare transcript output %q: %w", s.transcript, err) - } - if info != nil { - if err := s.temp.Chmod(info.Mode().Perm()); err != nil { - _ = s.Close() - return err + if s.transcriptFile.dir != "" { + if err := s.Close(); err != nil { + return fmt.Errorf("clean unpublished transcript output %q: %w", s.transcript, err) } } + if err := s.transcriptFile.prepare(s.publishPath); err != nil { + return fmt.Errorf("prepare transcript output %q: %w", s.transcript, errors.Join(err, s.Close())) + } return nil } @@ -184,23 +205,20 @@ func (s *OutputSession) Close() error { if s == nil { return nil } + return errors.Join(s.transcriptFile.close(), s.cleanupReports()) +} + +func (s *OutputSession) cleanupReports() error { var errs []error - if s.temp != nil { - errs = append(errs, s.temp.Close()) - s.temp = nil - } - if s.tempPath != "" { - if err := os.Remove(s.tempPath); err != nil && !errors.Is(err, os.ErrNotExist) { - errs = append(errs, err) + remaining := s.pendingCleanup[:0] + for _, file := range s.pendingCleanup { + errs = append(errs, file.close()) + if file.dir != "" { + remaining = append(remaining, file) } - s.tempPath = "" - } - if s.tempDir != "" { - if err := os.Remove(s.tempDir); err != nil && !errors.Is(err, os.ErrNotExist) { - errs = append(errs, err) - } - s.tempDir = "" } + clear(s.pendingCleanup[len(remaining):]) + s.pendingCleanup = remaining return errors.Join(errs...) } @@ -218,39 +236,20 @@ func (s *OutputSession) TranscriptPath() string { return s.transcript } -// Initialize publishes each initial report and attempts failed-status reports if any fail. -func (s *OutputSession) Initialize(initial ReportBundle, failed func([]FileFailure) ReportBundle) error { +// ReportRenderer receives the processing cause and known delivery failures. +type ReportRenderer func(error, []FileFailure) (ReportBundle, error) + +// Initialize publishes reports before model work can start. +func (s *OutputSession) Initialize(ctx context.Context, initial ReportBundle, render ReportRenderer) error { if s == nil || !s.enabled { return nil } - var failures []FileFailure - for i, payload := range bundleParts(initial) { - if err := s.replace(s.reports[i], payload); err != nil { - failures = append(failures, FileFailure{Path: s.reports[i], Kind: "report initialization", Err: err}) - } - } - if len(failures) == 0 { - return nil - } - if err := s.Close(); err != nil { - failures = append(failures, FileFailure{Path: s.transcript, Kind: "output cleanup", Err: err}) - } - if failed != nil { - for i, payload := range bundleParts(failed(failures)) { - if err := s.replace(s.reports[i], payload); err != nil { - failures = append(failures, FileFailure{Path: s.reports[i], Kind: "failed report initialization", Err: err}) - } - } - } - errs := make([]error, 0, len(failures)) - for _, failure := range failures { - errs = append(errs, failure) - } - return errors.Join(errs...) + failures := s.publishReports(ctx, initial, nil, nil, "report initialization", render) + return includeCancellation(ctx, joinOutputFailures(nil, failures)) } // PrepareDebug creates optional initial diagnostics and returns any nonfatal problems. -func (s *OutputSession) PrepareDebug(workDir, runID string, metadata InvocationMetadata, effective []byte) (*RunDirectory, []error) { +func PrepareDebug(workDir, runID string, metadata InvocationMetadata, effective []byte) (*RunDirectory, []error) { run, err := NewRunDirectory(workDir, runID) if err != nil { return nil, []error{err} @@ -267,180 +266,307 @@ func (s *OutputSession) PrepareDebug(workDir, runID string, metadata InvocationM return run, problems } -// Finalize publishes the transcript first, then errors, recoveries, and Markdown. -// An empty transcript payload means processing failed and no transcript is written. -// Every enabled report is attempted even after an earlier write failure. -func (s *OutputSession) Finalize(transcript []byte, reports ReportBundle, failed func([]FileFailure) ReportBundle) []FileFailure { +// Finish publishes a completed transcript before rendering final reports. +func (s *OutputSession) Finish(ctx context.Context, primary error, transcript []byte, render ReportRenderer) error { + return includeCancellation(ctx, joinOutputFailures(primary, s.finalize(ctx, primary, transcript, render))) +} + +func (s *OutputSession) finalize(ctx context.Context, primary error, transcript []byte, render ReportRenderer) []FileFailure { if s == nil { return []FileFailure{{Kind: "output finalization", Err: fmt.Errorf("output session is required")}} } - failures := make([]FileFailure, 0) - if transcript != nil { - if err := s.writeTranscript(transcript); err != nil { + var failures []FileFailure + if primary == nil && ctx.Err() == nil && transcript != nil { + if err := s.writeTranscript(ctx, transcript); err != nil { failures = append(failures, FileFailure{Path: s.transcript, Kind: "transcript write", Err: err}) } } if err := s.Close(); err != nil { failures = append(failures, FileFailure{Path: s.transcript, Kind: "output cleanup", Err: err}) } - if s.enabled { - for i, payload := range bundleParts(reports) { - if err := s.replace(s.reports[i], payload); err != nil { - failures = append(failures, FileFailure{Path: s.reports[i], Kind: "report write", Err: err}) + if !s.enabled || render == nil { + return failures + } + primary = includeCancellation(ctx, primary) + if primary != nil || len(failures) != 0 { + return s.publishReports(ctx, ReportBundle{}, primary, failures, "report write", render) + } + reports, err := render(nil, nil) + if err != nil { + failures = append(failures, FileFailure{Kind: "report rendering", Err: err}) + } + return s.publishReports(ctx, reports, primary, failures, "report write", render) +} + +func (s *OutputSession) publishReports(ctx context.Context, reports ReportBundle, primary error, failures []FileFailure, action string, render ReportRenderer) (result []FileFailure) { + defer func() { + if err := s.cleanupReports(); err != nil { + result = append(result, FileFailure{Kind: "output cleanup", Err: err}) + } + }() + primary = includeCancellation(ctx, primary) + failed := false + var failedWritten [3]bool + write := func(i int, payload []byte) { + writeCtx := ctx + kind := action + if failed { + writeCtx = context.WithoutCancel(ctx) + kind = "failed " + action + } + if err := s.replace(writeCtx, s.reportDestinations[i], payload); err != nil { + failures = append(failures, FileFailure{Path: s.reports[i], Kind: kind, Err: err}) + } else if failed { + failedWritten[i] = true + } + } + renderFailed := func() bool { + if action == "report initialization" { + if err := s.Close(); err != nil { + failures = append(failures, FileFailure{Path: s.transcript, Kind: "output cleanup", Err: err}) } } - if len(failures) > 0 && failed != nil { - for i, payload := range bundleParts(failed(failures)) { - if err := s.replace(s.reports[i], payload); err != nil { - failures = append(failures, FileFailure{Path: s.reports[i], Kind: "failed report write", Err: err}) - } + if render == nil { + return false + } + var err error + reports, err = render(includeCancellation(ctx, primary), failures) + if err != nil { + failures = append(failures, FileFailure{Kind: "failed report rendering", Err: err}) + return false + } + failed = true + return true + } + knownFailure := primary != nil || len(failures) != 0 + if knownFailure && !renderFailed() { + return failures + } + for i := range s.reports { + if !failed && (len(failures) != 0 || ctx.Err() != nil) && !renderFailed() { + return failures + } + write(i, bundleParts(reports)[i]) + } + if !knownFailure && (failed || len(failures) != 0 || ctx.Err() != nil) { + if !failed && !renderFailed() { + return failures + } + for i, payload := range bundleParts(reports) { + if !failedWritten[i] { + write(i, payload) } } } return failures } -// Stream destinations are opened only when delivering the transcript. -// Finish retains an earlier processing error while reporting delivery failures. -func (s *OutputSession) Finish(primary error, transcript []byte, reports ReportBundle, failed func([]FileFailure) ReportBundle) error { - if primary != nil { - transcript = nil - } - failures := s.Finalize(transcript, reports, failed) - errs := make([]error, 0, len(failures)+1) - errs = append(errs, primary) +func joinOutputFailures(primary error, failures []FileFailure) error { + errs := []error{primary} for _, failure := range failures { errs = append(errs, failure) } return errors.Join(errs...) } -func (s *OutputSession) writeTranscript(payload []byte) error { +func includeCancellation(ctx context.Context, err error) error { + if canceled := ctx.Err(); canceled != nil && !errors.Is(err, canceled) { + return errors.Join(err, canceled) + } + return err +} + +func (s *OutputSession) writeTranscript(ctx context.Context, payload []byte) error { + if err := ctx.Err(); err != nil { + return err + } if s.transcript == "" { if s.stdout == nil { return fmt.Errorf("transcript stdout writer is required") } n, err := s.stdout.Write(payload) if err == nil && n != len(payload) { - return io.ErrShortWrite + err = io.ErrShortWrite } - return err + return includeCancellation(ctx, err) } if s.direct { - info, err := os.Stat(s.transcript) + info, err := os.Stat(s.publishPath) if err != nil { return err } if info.IsDir() || info.Mode().IsRegular() { return fmt.Errorf("transcript output is no longer a stream destination") } - file, err := os.OpenFile(s.transcript, os.O_WRONLY, 0) + if err := ctx.Err(); err != nil { + return err + } + file, err := os.OpenFile(s.publishPath, os.O_WRONLY, 0) if err != nil { return err } + if err := ctx.Err(); err != nil { + return errors.Join(err, file.Close()) + } n, writeErr := file.Write(payload) if writeErr == nil && n != len(payload) { writeErr = io.ErrShortWrite } - return errors.Join(writeErr, file.Close()) + return includeCancellation(ctx, errors.Join(writeErr, file.Close())) } - if s.temp == nil { - return fmt.Errorf("transcript output is not prepared") - } - n, writeErr := s.temp.Write(payload) - if writeErr == nil && n != len(payload) { - writeErr = io.ErrShortWrite - } - closeErr := s.temp.Close() - s.temp = nil - if err := errors.Join(writeErr, closeErr); err != nil { - return err - } - info, err := os.Stat(s.publishPath) - if err == nil && !info.Mode().IsRegular() { - return fmt.Errorf("transcript output changed type after preparation") - } - if err != nil && !errors.Is(err, os.ErrNotExist) { - return err - } - if err := os.Rename(s.tempPath, s.publishPath); err != nil { - return err - } - s.tempPath = "" - return nil + return s.transcriptFile.publish(ctx, s.publishPath, payload) } func bundleParts(bundle ReportBundle) [3][]byte { return [3][]byte{bundle.Errors, bundle.Recoveries, bundle.Markdown} } -func sameDestination(a, b string) (bool, error) { - ca, err := canonicalDestination(a) - if err != nil { - return false, err - } - cb, err := canonicalDestination(b) - if err != nil { - return false, err - } - if ca == cb { - return true, nil - } - ai, ae := os.Stat(a) - bi, be := os.Stat(b) - return ae == nil && be == nil && os.SameFile(ai, bi), nil -} - +// Resolve the supplied path before making it absolute so a symlink followed by +// ".." uses the symlink target's parent. A missing leaf uses its resolved parent. func canonicalDestination(path string) (string, error) { - abs, err := filepath.Abs(path) + resolved, err := filepath.EvalSymlinks(path) + if err == nil { + return filepath.Abs(resolved) + } + if !errors.Is(err, os.ErrNotExist) { + return "", err + } + parent, leaf := filepath.Split(path) + if leaf == "" { + return "", err + } + if parent == "" { + parent = "." + } + resolved, err = filepath.EvalSymlinks(parent) if err != nil { return "", err } - abs = filepath.Clean(abs) - if resolved, err := filepath.EvalSymlinks(abs); err == nil { - return resolved, nil + resolved, err = filepath.Abs(resolved) + if err != nil { + return "", err } - parent, err := filepath.EvalSymlinks(filepath.Dir(abs)) + candidate := filepath.Join(resolved, leaf) + // A stream such as a descriptor alias can have a kernel-resolved referent + // without a regular filesystem path to that referent. + if info, statErr := os.Stat(candidate); statErr == nil && !info.IsDir() && !info.Mode().IsRegular() { + return candidate, nil + } + link, err := os.Readlink(candidate) if err == nil { - return filepath.Join(parent, filepath.Base(abs)), nil + if !filepath.IsAbs(link) { + link = resolved + string(filepath.Separator) + link + } + return canonicalDestination(link) } - return abs, nil + return candidate, nil } -func atomicReplace(path string, payload []byte) error { - publishPath, err := canonicalDestination(path) - if err != nil { - return err - } - // A private temporary directory lets ordinary file creation apply the umask - // without reading or changing process-global state. Rename stays on the same - // filesystem, and the incomplete payload is not accessible through the parent. - dir, err := os.MkdirTemp(filepath.Dir(publishPath), ".audita-write-*") - if err != nil { - return err - } - defer os.Remove(dir) - name := filepath.Join(dir, "payload") - temp, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644) - if err != nil { - return err - } - defer os.Remove(name) - if info, err := os.Stat(publishPath); err == nil { - if err := temp.Chmod(info.Mode().Perm()); err != nil { - _ = temp.Close() - return err - } - } else if !errors.Is(err, os.ErrNotExist) { - _ = temp.Close() - return err - } - if _, err := temp.Write(payload); err != nil { - _ = temp.Close() - return err - } - if err := temp.Close(); err != nil { - return err - } - return os.Rename(name, publishPath) +// stagedFile is a regular file staged beside its checked destination. +type stagedFile struct { + file *os.File + path string + dir string +} + +func (f *stagedFile) prepare(target string) error { + info, err := regularFileInfo(target) + if err != nil { + return err + } + // The private directory keeps the incomplete payload inaccessible while + // allowing ordinary creation to apply the umask without changing it. + f.dir, err = os.MkdirTemp(filepath.Dir(target), ".audita-output-*") + if err != nil { + return err + } + f.path = filepath.Join(f.dir, "payload") + f.file, err = os.OpenFile(f.path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644) + if err != nil { + return err + } + if info != nil { + return f.file.Chmod(info.Mode().Perm()) + } + return nil +} + +func (f *stagedFile) publish(ctx context.Context, target string, payload []byte) error { + if err := ctx.Err(); err != nil { + return err + } + if f.file == nil { + return fmt.Errorf("output is not prepared") + } + n, writeErr := f.file.Write(payload) + if writeErr == nil && n != len(payload) { + writeErr = io.ErrShortWrite + } + closeErr := f.file.Close() + f.file = nil + if err := errors.Join(writeErr, closeErr); err != nil { + return err + } + if _, err := regularFileInfo(target); err != nil { + return err + } + if err := ctx.Err(); err != nil { + return err + } + if err := os.Rename(f.path, target); err != nil { + return err + } + f.path = "" + return nil +} + +func regularFileInfo(path string) (os.FileInfo, error) { + info, err := os.Lstat(path) + if errors.Is(err, os.ErrNotExist) { + return nil, nil + } + if err != nil { + return nil, err + } + if !info.Mode().IsRegular() { + return nil, fmt.Errorf("output changed type after selection") + } + return info, nil +} + +func (f *stagedFile) close() error { + var failures []error + if f.file != nil { + failures = append(failures, f.file.Close()) + f.file = nil + } + if f.path != "" { + if err := os.Remove(f.path); err != nil && !errors.Is(err, os.ErrNotExist) { + return errors.Join(append(failures, err)...) + } + f.path = "" + } + if f.dir != "" { + if err := os.Remove(f.dir); err != nil && !errors.Is(err, os.ErrNotExist) { + return errors.Join(append(failures, err)...) + } + f.dir = "" + } + return errors.Join(failures...) +} + +func (s *OutputSession) replaceReport(ctx context.Context, target string, payload []byte) error { + if err := ctx.Err(); err != nil { + return err + } + file := new(stagedFile) + err := file.prepare(target) + if err == nil { + err = file.publish(ctx, target, payload) + } + cleanup := file.close() + if file.dir != "" { + s.pendingCleanup = append(s.pendingCleanup, file) + } + return errors.Join(err, cleanup) } diff --git a/internal/core/diagnostics/output_session_test.go b/internal/core/diagnostics/output_session_test.go index 81091ff..9ee8900 100644 --- a/internal/core/diagnostics/output_session_test.go +++ b/internal/core/diagnostics/output_session_test.go @@ -2,6 +2,7 @@ package diagnostics import ( "bytes" + "context" "encoding/json" "errors" "io" @@ -73,7 +74,7 @@ func TestPrepareOutputPreservesExistingTranscriptAndChecksParent(t *testing.T) { if got, err := os.ReadFile(output); err != nil || string(got) != "old content" { t.Fatalf("preflight truncated output: %s %v", got, err) } - if failures := session.Finalize([]byte("new content"), ReportBundle{}, func([]FileFailure) ReportBundle { return ReportBundle{} }); len(failures) != 0 { + if failures := session.finalize(context.Background(), nil, []byte("new content"), nil); len(failures) != 0 { t.Fatal(failures) } if got, err := os.ReadFile(output); err != nil || string(got) != "new content" { @@ -99,21 +100,93 @@ func TestPrepareOutputPreservesExistingTranscriptAndChecksParent(t *testing.T) { } } -func TestAtomicReplacementCleansTemporaryFileAfterRenameFailure(t *testing.T) { +func TestOutputPathsUseFilesystemParent(t *testing.T) { + dir := t.TempDir() + resolved := filepath.Join(dir, "destination") + child := filepath.Join(resolved, "child") + if err := os.MkdirAll(child, 0o700); err != nil { + t.Fatal(err) + } + alias := filepath.Join(dir, "alias") + if err := os.Symlink(child, alias); err != nil { + t.Fatal(err) + } + parent := alias + string(filepath.Separator) + ".." + output := parent + string(filepath.Separator) + "out.json" + session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: parent, ReportingEnabled: true}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := session.Close(); err != nil { + t.Error(err) + } + }) + if err := session.Finish(context.Background(), nil, []byte("transcript"), func(error, []FileFailure) (ReportBundle, error) { + return ReportBundle{Markdown: []byte("report")}, nil + }); err != nil { + t.Fatal(err) + } + for name, want := range map[string]string{"out.json": "transcript", MarkdownFile: "report"} { + got, err := os.ReadFile(filepath.Join(resolved, name)) + if err != nil || string(got) != want { + t.Fatalf("%s: %s, %v", name, got, err) + } + } +} + +func TestOutputPublishesThroughTranscriptAndReportSymlinks(t *testing.T) { + dir := t.TempDir() + transcript := filepath.Join(dir, "transcript.json") + report := filepath.Join(dir, MarkdownFile) + for path, target := range map[string]string{transcript: "actual.json", report: "actual.md"} { + if err := os.Symlink(target, path); err != nil { + t.Fatal(err) + } + } + session, err := PrepareOutput(OutputSelection{TranscriptPath: transcript, ReportDir: dir, ReportingEnabled: true}) + if err != nil { + t.Fatal(err) + } + err = session.Finish(context.Background(), nil, []byte("transcript"), func(error, []FileFailure) (ReportBundle, error) { + return ReportBundle{Markdown: []byte("report")}, nil + }) + if err != nil { + t.Fatal(err) + } + for path, want := range map[string]string{transcript: "transcript", report: "report"} { + if _, err := os.Readlink(path); err != nil { + t.Errorf("lost symlink at %q: %v", path, err) + } + got, err := os.ReadFile(path) + if err != nil || string(got) != want { + t.Fatalf("%s: %s, %v", path, got, err) + } + } +} + +func TestStagingCleansTemporaryResourcesAfterPublicationFailure(t *testing.T) { dir := t.TempDir() blocked := filepath.Join(dir, "directory-target") + var file stagedFile + if err := file.prepare(blocked); err != nil { + t.Fatal(err) + } if err := os.Mkdir(blocked, 0700); err != nil { t.Fatal(err) } - if err := atomicReplace(blocked, []byte("new content")); err == nil { + if err := file.publish(context.Background(), blocked, []byte("new content")); err == nil { t.Fatal("directory replacement unexpectedly succeeded") } + if err := file.close(); err != nil { + t.Fatal(err) + } entries, err := os.ReadDir(dir) if err != nil { t.Fatal(err) } for _, entry := range entries { - if strings.HasPrefix(entry.Name(), ".audita-write-") { + if strings.HasPrefix(entry.Name(), ".audita-output-") { t.Fatalf("replacement left temporary file: %s", entry.Name()) } } @@ -125,7 +198,7 @@ func TestDisabledReportingIgnoresUnusableDirectory(t *testing.T) { if err != nil || len(session.ReportPaths()) != 0 { t.Fatalf("disabled reports probed: %+v %v", session, err) } - if err := session.Initialize(ReportBundle{}, nil); err != nil { + if err := session.Initialize(context.Background(), ReportBundle{}, nil); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) { @@ -151,7 +224,7 @@ func TestStdoutModeInitializesAndFinalizesReports(t *testing.T) { } initial := ReportBundle{Errors: []byte("in progress errors"), Recoveries: []byte("in progress recoveries"), Markdown: []byte("in progress report")} finished := ReportBundle{Errors: []byte("completed errors"), Recoveries: []byte("completed recoveries"), Markdown: []byte("completed report")} - if err := session.Initialize(initial, nil); err != nil { + if err := session.Initialize(context.Background(), initial, nil); err != nil { t.Fatal(err) } if session.TranscriptPath() != "" { @@ -163,7 +236,7 @@ func TestStdoutModeInitializesAndFinalizesReports(t *testing.T) { t.Fatalf("initial %s: %s %v", path, got, err) } } - if failures := session.Finalize(nil, finished, func([]FileFailure) ReportBundle { return ReportBundle{} }); len(failures) != 0 { + if failures := session.finalize(context.Background(), nil, nil, func(error, []FileFailure) (ReportBundle, error) { return finished, nil }); len(failures) != 0 { t.Fatal(failures) } for i, path := range session.ReportPaths() { @@ -181,18 +254,18 @@ func TestInitializeFailureMarksPriorReportFailedBeforeModelWork(t *testing.T) { t.Fatal(err) } actual := session.replace - session.replace = func(path string, payload []byte) error { + session.replace = func(ctx context.Context, path string, payload []byte) error { if filepath.Base(path) == RecoveriesFile && string(payload) == "initial" { return os.ErrPermission } - return actual(path, payload) + return actual(ctx, path, payload) } modelCalled := false - if err := session.Initialize(ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")}, func(failures []FileFailure) ReportBundle { + if err := session.Initialize(context.Background(), ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")}, func(_ error, failures []FileFailure) (ReportBundle, error) { if len(failures) != 1 || filepath.Base(failures[0].Path) != RecoveriesFile { t.Fatalf("initialization failures: %+v", failures) } - return ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")} + return ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")}, nil }); err == nil { modelCalled = true } @@ -215,22 +288,32 @@ func TestFinalizationCollectsFailuresAndRetainsPublishedTranscript(t *testing.T) if err != nil { t.Fatal(err) } - if err := session.Initialize(ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")}, nil); err != nil { + if err := session.Initialize(context.Background(), ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")}, nil); err != nil { t.Fatal(err) } actual := session.replace - session.replace = func(path string, payload []byte) error { + var attempts []string + session.replace = func(ctx context.Context, path string, payload []byte) error { + attempts = append(attempts, filepath.Base(path)+"="+string(payload)) if filepath.Base(path) == ErrorsFile && string(payload) == "complete" { return os.ErrPermission } - return actual(path, payload) + return actual(ctx, path, payload) } complete := ReportBundle{Errors: []byte("complete"), Recoveries: []byte("complete"), Markdown: []byte("complete")} failed := ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")} - failures := session.Finalize([]byte("transcript"), complete, func([]FileFailure) ReportBundle { return failed }) + failures := session.finalize(context.Background(), nil, []byte("transcript"), func(_ error, failures []FileFailure) (ReportBundle, error) { + if len(failures) > 0 { + return failed, nil + } + return complete, nil + }) if len(failures) != 1 || failures[0].Path != filepath.Join(dir, ErrorsFile) { t.Fatalf("failures: %+v", failures) } + if got := strings.Join(attempts, ","); got != "errors.yml=complete,recoveries.yml=failed,report.md=failed,errors.yml=failed" { + t.Fatalf("unexpected publication attempts: %s", got) + } if got, err := os.ReadFile(output); err != nil || string(got) != "transcript" { t.Fatalf("transcript erased: %s %v", got, err) } @@ -241,6 +324,106 @@ func TestFinalizationCollectsFailuresAndRetainsPublishedTranscript(t *testing.T) } } +func TestFailedRenderingKeepsInitializedEvidenceAfterStreamError(t *testing.T) { + dir := t.TempDir() + delivery := errors.New("stream delivery failed") + rendering := errors.New("failed report rendering failed") + session, err := PrepareOutput(OutputSelection{ + Stdout: writerFunc(func([]byte) (int, error) { return 0, delivery }), + ReportDir: dir, ReportingEnabled: true, + }) + if err != nil { + t.Fatal(err) + } + ctx := context.Background() + initial := ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")} + if err := session.Initialize(ctx, initial, nil); err != nil { + t.Fatal(err) + } + session.replace = func(context.Context, string, []byte) error { + t.Error("attempted to replace initialized report without a failed bundle") + return nil + } + err = session.Finish(ctx, nil, []byte("transcript"), func(primary error, failures []FileFailure) (ReportBundle, error) { + if len(failures) != 1 || !errors.Is(failures[0], delivery) { + t.Fatalf("renderer received %v, %v", primary, failures) + } + return ReportBundle{}, rendering + }) + if !errors.Is(err, delivery) || !errors.Is(err, rendering) { + t.Fatalf("lost failure causes: %v", err) + } + for _, path := range session.ReportPaths() { + raw, err := os.ReadFile(path) + if err != nil || string(raw) != "initial" { + t.Fatalf("report at %s: %s (%v)", path, raw, err) + } + } +} + +type writerFunc func([]byte) (int, error) + +func (write writerFunc) Write(payload []byte) (int, error) { return write(payload) } + +func TestCancellationAfterStreamDeliveryUsesOnlyFailedReports(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + delivery := errors.New("late stream error") + var escaped []byte + session, err := PrepareOutput(OutputSelection{ + Stdout: writerFunc(func(payload []byte) (int, error) { + escaped = append(escaped, payload...) + cancel() + return len(payload), delivery + }), + ReportDir: t.TempDir(), ReportingEnabled: true, + }) + if err != nil { + t.Fatal(err) + } + var attempts int + session.replace = func(ctx context.Context, path string, payload []byte) error { + attempts++ + if ctx.Err() != nil || string(payload) != "failed" { + t.Errorf("unexpected publication for %s: %q, context %v", path, payload, ctx.Err()) + } + return nil + } + err = session.Finish(ctx, nil, []byte("transcript"), func(primary error, failures []FileFailure) (ReportBundle, error) { + if !errors.Is(primary, context.Canceled) || len(failures) != 1 || !errors.Is(failures[0], delivery) { + t.Fatalf("renderer received %v, %v", primary, failures) + } + return ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")}, nil + }) + if string(escaped) != "transcript" || attempts != 3 || !errors.Is(err, context.Canceled) || !errors.Is(err, delivery) { + t.Fatalf("escaped=%q reports=%d error=%v", escaped, attempts, err) + } +} + +func TestCleanupFailureKeepsTemporaryDirectoryForRetry(t *testing.T) { + session, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "output.json")}) + if err != nil { + t.Fatal(err) + } + tempDir := session.transcriptFile.dir + extra := filepath.Join(tempDir, "extra") + if err := os.WriteFile(extra, nil, 0o600); err != nil { + t.Fatal(err) + } + primary := errors.New("processing failed") + err = session.Finish(context.Background(), primary, nil, nil) + var failure FileFailure + if !errors.Is(err, primary) || !errors.As(err, &failure) || failure.Kind != "output cleanup" || session.transcriptFile.dir != tempDir { + t.Fatalf("cleanup error=%v, directory=%q", err, session.transcriptFile.dir) + } + if err := os.Remove(extra); err != nil { + t.Fatal(err) + } + if err := session.Close(); err != nil || session.transcriptFile.dir != "" { + t.Fatalf("retry error=%v, directory=%q", err, session.transcriptFile.dir) + } +} + func TestDebugRunDirectoryUsesSuppliedIDAndIsRetained(t *testing.T) { parent := filepath.Join(t.TempDir(), "debug") if _, err := os.Stat(parent); !errors.Is(err, os.ErrNotExist) { @@ -326,10 +509,10 @@ func TestSelectionAndAbortPreserveTranscript(t *testing.T) { if err := session.Prepare(); err != nil { t.Fatal(err) } - if session.temp == nil { + if session.transcriptFile.file == nil { t.Fatal("temporary output not open") } - temp := session.tempPath + temp := session.transcriptFile.path if err := session.Close(); err != nil { t.Fatal(err) } @@ -347,7 +530,7 @@ func TestStdoutAndDeviceDelivery(t *testing.T) { if err != nil { t.Fatal(err) } - if failures := session.Finalize([]byte("transcript"), ReportBundle{}, nil); len(failures) != 0 { + if failures := session.finalize(context.Background(), nil, []byte("transcript"), nil); len(failures) != 0 { t.Fatal(failures) } if stdout.String() != "transcript" { @@ -357,10 +540,10 @@ func TestStdoutAndDeviceDelivery(t *testing.T) { if err != nil { t.Fatal(err) } - if !session.direct || session.temp != nil { + if !session.direct || session.transcriptFile.file != nil { t.Fatal("device was not deferred") } - if failures := session.Finalize([]byte("discard"), ReportBundle{}, nil); len(failures) != 0 { + if failures := session.finalize(context.Background(), nil, []byte("discard"), nil); len(failures) != 0 { t.Fatal(failures) } } @@ -376,15 +559,15 @@ func TestFinishRetainsPrimaryFailureAndAttemptsReports(t *testing.T) { t.Fatal(err) } originalReplace := session.replace - session.replace = func(path string, data []byte) error { + session.replace = func(ctx context.Context, path string, data []byte) error { if filepath.Base(path) == ErrorsFile { return os.ErrPermission } - return originalReplace(path, data) + return originalReplace(ctx, path, data) } primary := errors.New("processing failed") bundle := ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")} - result := session.Finish(primary, []byte("unwanted"), bundle, func([]FileFailure) ReportBundle { return bundle }) + result := session.Finish(context.Background(), primary, []byte("unwanted"), func(error, []FileFailure) (ReportBundle, error) { return bundle, nil }) if !errors.Is(result, primary) || !errors.Is(result, os.ErrPermission) { t.Fatalf("combined error: %v", result) } @@ -398,18 +581,14 @@ func TestFinishRetainsPrimaryFailureAndAttemptsReports(t *testing.T) { func TestOptionalDebugPreparationReportsProblems(t *testing.T) { dir := t.TempDir() - session, err := SelectOutput(OutputSelection{}) - if err != nil { - t.Fatal(err) - } file := filepath.Join(dir, "not-directory") if err := os.WriteFile(file, []byte("x"), 0600); err != nil { t.Fatal(err) } - if run, problems := session.PrepareDebug(file, "run", InvocationMetadata{}, nil); run != nil || len(problems) == 0 { + if run, problems := PrepareDebug(file, "run", InvocationMetadata{}, nil); run != nil || len(problems) == 0 { t.Fatalf("run=%v problems=%v", run, problems) } - run, problems := session.PrepareDebug(filepath.Join(dir, "debug"), "run", InvocationMetadata{Operation: "process"}, []byte("version: 3\n")) + run, problems := PrepareDebug(filepath.Join(dir, "debug"), "run", InvocationMetadata{Operation: "process"}, []byte("version: 3\n")) if len(problems) != 0 { t.Fatal(problems) } @@ -431,7 +610,7 @@ func TestShortStdoutWriteIsFailure(t *testing.T) { if err != nil { t.Fatal(err) } - failures := session.Finalize([]byte("transcript"), ReportBundle{}, nil) + failures := session.finalize(context.Background(), nil, []byte("transcript"), nil) if len(failures) != 1 || !errors.Is(failures[0], io.ErrShortWrite) { t.Fatalf("failures=%v", failures) } @@ -447,11 +626,11 @@ func TestTranscriptWriteFailureCleansTemporaryResource(t *testing.T) { if err != nil { t.Fatal(err) } - temp := session.tempPath - if err := session.temp.Close(); err != nil { + temp := session.transcriptFile.path + if err := session.transcriptFile.file.Close(); err != nil { t.Fatal(err) } - failures := session.Finalize([]byte("new"), ReportBundle{}, nil) + failures := session.finalize(context.Background(), nil, []byte("new"), nil) if len(failures) == 0 { t.Fatal("closed output accepted") } @@ -470,7 +649,7 @@ func TestNamedPipeIsNotOpenedDuringPreparation(t *testing.T) { if err != nil { t.Fatal(err) } - if !session.direct || session.temp != nil { + if !session.direct || session.transcriptFile.file != nil { t.Fatal("pipe was not deferred") } if err := session.Close(); err != nil { @@ -487,7 +666,7 @@ func TestLateNamedPipeIsPreserved(t *testing.T) { } defer session.Close() makeNamedPipe(t, path) - err = session.Finish(nil, []byte("transcript"), ReportBundle{}, nil) + err = session.Finish(context.Background(), nil, []byte("transcript"), nil) if err == nil || !strings.Contains(err.Error(), "changed type") { t.Fatalf("expected clear delivery failure, got %v", err) } @@ -515,7 +694,7 @@ func TestStreamChangedToRegularFileIsPreserved(t *testing.T) { if err := os.WriteFile(path, []byte("existing content"), 0600); err != nil { t.Fatal(err) } - err = session.Finish(nil, []byte("new"), ReportBundle{}, nil) + err = session.Finish(context.Background(), nil, []byte("new"), nil) if err == nil || !strings.Contains(err.Error(), "no longer a stream") { t.Fatalf("expected clear delivery failure, got %v", err) } diff --git a/internal/core/diagnostics/output_stream_test.go b/internal/core/diagnostics/output_stream_test.go index 64b668d..b470ec1 100644 --- a/internal/core/diagnostics/output_stream_test.go +++ b/internal/core/diagnostics/output_stream_test.go @@ -3,6 +3,7 @@ package diagnostics import ( + "context" "io" "os" "path/filepath" @@ -37,7 +38,7 @@ func TestTranscriptStreamDestinations(t *testing.T) { defer reader.Close() } payload := []byte("transcript") - if failures := session.Finalize(payload, ReportBundle{}, nil); len(failures) != 0 { + if failures := session.finalize(context.Background(), nil, payload, nil); len(failures) != 0 { t.Fatal(failures) } after, err := os.Stat(path) @@ -62,7 +63,7 @@ func TestTranscriptStreamWriteFailure(t *testing.T) { if err != nil { t.Fatal(err) } - failures := session.Finalize([]byte("transcript"), ReportBundle{}, nil) + failures := session.finalize(context.Background(), nil, []byte("transcript"), nil) if len(failures) != 1 || failures[0].Kind != "transcript write" { t.Fatalf("write failure lost: %+v", failures) } diff --git a/internal/core/reporting/markdown.go b/internal/core/reporting/markdown.go index 666f180..45c1279 100644 --- a/internal/core/reporting/markdown.go +++ b/internal/core/reporting/markdown.go @@ -34,12 +34,12 @@ func Markdown(snapshot Snapshot) []byte { fmt.Fprintf(&b, "Recoveries: %s of %s model operations \n", showCount(snapshot.Summary.Recoveries), showCount(snapshot.Summary.LLMOperations)) fmt.Fprintf(&b, "Proposal responses: %s; with filtering: %s\n\n", showCount(snapshot.Summary.ProposalResponses), showCount(snapshot.Summary.ProposalResponsesWithFiltering)) - fmt.Fprintf(&b, "Transcript input: %s \n", escapeMarkdown(snapshot.TranscriptPath)) + fmt.Fprintf(&b, "Transcript input: %s \n", stageCellEscaper.Replace(html.EscapeString(snapshot.TranscriptPath))) output := snapshot.OutputPath if output == "" { output = "stdout" } - fmt.Fprintf(&b, "Transcript output: %s\n\n", escapeMarkdown(output)) + fmt.Fprintf(&b, "Transcript output: %s\n\n", stageCellEscaper.Replace(html.EscapeString(output))) fmt.Fprintln(&b, "## Work-unit errors") fmt.Fprintln(&b) diff --git a/internal/core/reporting/markdown_test.go b/internal/core/reporting/markdown_test.go index 0767b7e..5cc11ec 100644 --- a/internal/core/reporting/markdown_test.go +++ b/internal/core/reporting/markdown_test.go @@ -119,6 +119,22 @@ func TestStageIDIsLiteralInMarkdownAndExactInYAML(t *testing.T) { } } +func TestMarkdownTreatsTranscriptPathsAsLiteralText(t *testing.T) { + snapshot := Snapshot{ + TranscriptPath: "incoming/[raw](https://example.invalid)/_input_ .json", + OutputPath: "out/*file*\\name\nsecond", + } + rendered := string(Markdown(snapshot)) + for _, want := range []string{ + `incoming/\[raw\](https://example.invalid)/\_input\_ <part>.json`, + `out/\*file\*\name
second`, + } { + if !strings.Contains(rendered, want) { + t.Fatalf("missing escaped path %q in:\n%s", want, rendered) + } + } +} + func markdownTableRows(text string) []string { var rows []string for _, line := range strings.Split(text, "\n") { diff --git a/internal/framework/preflight/preflight.go b/internal/framework/preflight/preflight.go index 4a7fd84..ac8c1f2 100644 --- a/internal/framework/preflight/preflight.go +++ b/internal/framework/preflight/preflight.go @@ -3,6 +3,7 @@ package preflight import ( "context" + "errors" "fmt" "slices" @@ -19,7 +20,7 @@ type Input struct { Transcript *schema.SourceTranscript Output diagnostics.OutputSelection InitialReports diagnostics.ReportBundle - FailedReports func(error, []diagnostics.FileFailure) diagnostics.ReportBundle + FailedReports diagnostics.ReportRenderer Debug *DebugInput } @@ -61,30 +62,20 @@ func Prepare(ctx context.Context, input Input) (*Prepared, error) { return nil, err } if err := ctx.Err(); err != nil { - _ = session.Close() - return nil, err + return nil, errors.Join(err, session.Close()) } - failedReports := func(primary error) func([]diagnostics.FileFailure) diagnostics.ReportBundle { - return func(failures []diagnostics.FileFailure) diagnostics.ReportBundle { - if input.FailedReports == nil { - return diagnostics.ReportBundle{} - } - return input.FailedReports(primary, failures) - } - } - if err := session.Initialize(input.InitialReports, failedReports(nil)); err != nil { - return nil, err + if err := session.Initialize(ctx, input.InitialReports, input.FailedReports); err != nil { + return nil, errors.Join(err, session.Close()) } cancelOutput := func(err error) error { - failed := failedReports(err) - return session.Finish(err, nil, failed(nil), failed) + return session.Finish(ctx, err, nil, input.FailedReports) } if err := ctx.Err(); err != nil { return nil, cancelOutput(err) } result := &Prepared{Output: session} if input.Debug != nil { - result.Debug, result.Warnings = session.PrepareDebug(input.Debug.WorkDir, input.Debug.RunID, input.Debug.Metadata, input.Debug.EffectiveConfig) + result.Debug, result.Warnings = diagnostics.PrepareDebug(input.Debug.WorkDir, input.Debug.RunID, input.Debug.Metadata, input.Debug.EffectiveConfig) } if err := ctx.Err(); err != nil { return nil, cancelOutput(err)