From bd3a22754a2e6504888405c90ae4657f6fabb667 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Fri, 27 Nov 2026 01:10:44 +0000 Subject: [PATCH] Separate output checks from session creation --- docs/roadmap/implementation.md | 2 + internal/core/diagnostics/output_session.go | 53 ++++++++------ .../core/diagnostics/output_session_test.go | 71 +++++++++++-------- .../core/diagnostics/output_stream_test.go | 4 +- internal/framework/preflight/preflight.go | 3 +- 5 files changed, 79 insertions(+), 54 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 591eade..3c4125d 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -63,6 +63,8 @@ ownership and handoff remain as specified: ## Stage 1: Separate checked destinations from output resources +**Completed.** + Work: 1. Extract the checking work in `diagnostics.SelectOutput` into a check operation diff --git a/internal/core/diagnostics/output_session.go b/internal/core/diagnostics/output_session.go index 2210f14..2a1f4f9 100644 --- a/internal/core/diagnostics/output_session.go +++ b/internal/core/diagnostics/output_session.go @@ -68,10 +68,20 @@ type OutputSession struct { stdout io.Writer } -// SelectOutput checks active destinations before any output resources are opened. -func SelectOutput(selection OutputSelection) (*OutputSession, error) { - session := &OutputSession{enabled: selection.ReportingEnabled, stdout: selection.Stdout} - session.replace = session.replaceReport +// CheckedDestinations records destination paths and kinds without opening them. +type CheckedDestinations struct { + transcript string + publishPath string + reports [3]string + reportDestinations [3]string + enabled bool + direct bool + stdout io.Writer +} + +// CheckOutput checks active destinations before any output resources are opened. +func CheckOutput(selection OutputSelection) (*CheckedDestinations, error) { + checked := &CheckedDestinations{enabled: selection.ReportingEnabled, stdout: selection.Stdout} type destination struct { display string path string @@ -89,7 +99,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) { return destination{display: path, path: resolved, info: info}, nil } var resolvedDir string - if session.enabled { + if checked.enabled { if strings.TrimSpace(selection.ReportDir) == "" { return nil, fmt.Errorf("report directory is required") } @@ -115,11 +125,11 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) { 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() + checked.transcript, checked.publishPath = target.display, target.path + checked.direct = target.info != nil && !target.info.Mode().IsRegular() active = append(active, target) } - if session.enabled { + if checked.enabled { label := selection.ReportDir if !os.IsPathSeparator(label[len(label)-1]) { label += string(filepath.Separator) @@ -133,7 +143,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) { 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 + checked.reports[i], checked.reportDestinations[i] = target.display, target.path active = append(active, target) } } @@ -141,7 +151,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) { return a.path == b.path || (a.info != nil && b.info != nil && os.SameFile(a.info, b.info)) } if len(active) == 0 { - return session, nil + return checked, nil } var protected []destination for _, path := range selection.ProtectedPaths { @@ -154,7 +164,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) { } } var input destination - if session.enabled && selection.InputTranscriptPath != "" { + if checked.enabled && selection.InputTranscriptPath != "" { var err error input, err = check(selection.InputTranscriptPath) if err != nil { @@ -167,7 +177,7 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) { return nil, fmt.Errorf("output destination %q conflicts with input %q", target.display, source.display) } } - if (session.transcript == "" || i > 0) && input.path != "" && same(target, input) { + if (checked.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] { @@ -176,19 +186,18 @@ func SelectOutput(selection OutputSelection) (*OutputSession, error) { } } } - return session, nil + return checked, nil } -// PrepareOutput validates destinations and acquires the transcript resource. -func PrepareOutput(selection OutputSelection) (*OutputSession, error) { - session, err := SelectOutput(selection) - if err != nil { - return nil, err +// NewOutputSession constructs a session without acquiring output resources. +func NewOutputSession(checked *CheckedDestinations) *OutputSession { + session := &OutputSession{ + transcript: checked.transcript, publishPath: checked.publishPath, + reports: checked.reports, reportDestinations: checked.reportDestinations, + enabled: checked.enabled, direct: checked.direct, stdout: checked.stdout, } - if err := session.Prepare(); err != nil { - return nil, err - } - return session, nil + session.replace = session.replaceReport + return session } // Prepare opens a sibling temporary file for regular transcript output. diff --git a/internal/core/diagnostics/output_session_test.go b/internal/core/diagnostics/output_session_test.go index bd139f6..5248522 100644 --- a/internal/core/diagnostics/output_session_test.go +++ b/internal/core/diagnostics/output_session_test.go @@ -33,20 +33,20 @@ func TestPrepareOutputAllowsInPlaceTranscriptButRejectsReportCollisions(t *testi t.Fatal(err) } for _, output := range []string{input, alias, hardlink} { - if _, err := PrepareOutput(OutputSelection{TranscriptPath: output, InputTranscriptPath: input}); err != nil { + if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, InputTranscriptPath: input}); err != nil { t.Fatalf("in-place output rejected: %s: %v", output, err) } } - if _, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, ErrorsFile), ReportDir: dir, ReportingEnabled: true}); err == nil { + if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(dir, ErrorsFile), ReportDir: dir, ReportingEnabled: true}); err == nil { t.Fatal("report/transcript collision accepted") } - if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true, ProtectedPaths: []string{filepath.Join(dir, ErrorsFile)}}); err == nil { + if _, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true, ProtectedPaths: []string{filepath.Join(dir, ErrorsFile)}}); err == nil { t.Fatal("report/source collision accepted") } if err := os.Symlink(input, filepath.Join(dir, ErrorsFile)); err != nil { t.Fatal(err) } - if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true, InputTranscriptPath: input}); err == nil { + if _, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true, InputTranscriptPath: input}); err == nil { t.Fatal("report/input alias accepted") } if err := os.Remove(filepath.Join(dir, ErrorsFile)); err != nil { @@ -55,7 +55,7 @@ func TestPrepareOutputAllowsInPlaceTranscriptButRejectsReportCollisions(t *testi if err := os.Mkdir(filepath.Join(dir, MarkdownFile), 0700); err != nil { t.Fatal(err) } - if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}); err == nil { + if _, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true}); err == nil { t.Fatal("directory report target accepted") } if got, err := os.ReadFile(input); err != nil || string(got) != "source" { @@ -69,7 +69,7 @@ func TestPrepareOutputPreservesExistingTranscriptAndChecksParent(t *testing.T) { if err := os.WriteFile(output, []byte("old content"), 0600); err != nil { t.Fatal(err) } - session, err := PrepareOutput(OutputSelection{TranscriptPath: output}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output}) if err != nil { t.Fatal(err) } @@ -85,10 +85,10 @@ func TestPrepareOutputPreservesExistingTranscriptAndChecksParent(t *testing.T) { if info, _ := os.Stat(output); info.Mode().Perm() != 0600 { t.Fatalf("permission changed: %v", info.Mode()) } - if _, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, "missing", "out.json")}); err == nil { + if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(dir, "missing", "out.json")}); err == nil { t.Fatal("absent parent accepted") } - if _, err := PrepareOutput(OutputSelection{TranscriptPath: dir}); err == nil { + if _, err := prepareOutputForTest(OutputSelection{TranscriptPath: dir}); err == nil { t.Fatal("directory target accepted") } entries, err := os.ReadDir(dir) @@ -115,7 +115,7 @@ func TestOutputPathsUseFilesystemParent(t *testing.T) { } parent := alias + string(filepath.Separator) + ".." output := parent + string(filepath.Separator) + "out.json" - session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: parent, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, ReportDir: parent, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -146,7 +146,7 @@ func TestOutputPublishesThroughTranscriptAndReportSymlinks(t *testing.T) { t.Fatal(err) } } - session, err := PrepareOutput(OutputSelection{TranscriptPath: transcript, ReportDir: dir, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: transcript, ReportDir: dir, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -196,7 +196,7 @@ func TestStagingCleansTemporaryResourcesAfterPublicationFailure(t *testing.T) { func TestDisabledReportingIgnoresUnusableDirectory(t *testing.T) { dir := t.TempDir() - session, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, "out.json"), ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: false}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(dir, "out.json"), ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: false}) if err != nil || len(session.ReportPaths()) != 0 { t.Fatalf("disabled reports probed: %+v %v", session, err) } @@ -210,7 +210,7 @@ func TestDisabledReportingIgnoresUnusableDirectory(t *testing.T) { func TestEnabledReportingRequiresExistingDirectory(t *testing.T) { dir := t.TempDir() - if _, err := PrepareOutput(OutputSelection{ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: true}); err == nil { + if _, err := prepareOutputForTest(OutputSelection{ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: true}); err == nil { t.Fatal("missing report directory accepted") } if _, err := os.Stat(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) { @@ -220,7 +220,7 @@ func TestEnabledReportingRequiresExistingDirectory(t *testing.T) { func TestStdoutModeInitializesAndFinalizesReports(t *testing.T) { dir := t.TempDir() - session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -251,7 +251,7 @@ func TestStdoutModeInitializesAndFinalizesReports(t *testing.T) { func TestInitializeFailureMarksPriorReportFailedBeforeModelWork(t *testing.T) { dir := t.TempDir() - session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -286,7 +286,7 @@ func TestInitializeFailureMarksPriorReportFailedBeforeModelWork(t *testing.T) { func TestFinalizationCollectsFailuresAndRetainsPublishedTranscript(t *testing.T) { dir := t.TempDir() output := filepath.Join(dir, "output.json") - session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -330,7 +330,7 @@ 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{ + session, err := prepareOutputForTest(OutputSelection{ Stdout: writerFunc(func([]byte) (int, error) { return 0, delivery }), ReportDir: dir, ReportingEnabled: true, }) @@ -390,7 +390,7 @@ func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { dir := t.TempDir() - session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -430,7 +430,7 @@ func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) { func TestRepeatedFailureOfOneReportCountsOnce(t *testing.T) { dir := t.TempDir() - session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{ReportDir: dir, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -471,7 +471,7 @@ func TestCancellationAfterStreamDeliveryUsesOnlyFailedReports(t *testing.T) { defer cancel() delivery := errors.New("late stream error") var escaped []byte - session, err := PrepareOutput(OutputSelection{ + session, err := prepareOutputForTest(OutputSelection{ Stdout: writerFunc(func(payload []byte) (int, error) { escaped = append(escaped, payload...) cancel() @@ -502,7 +502,7 @@ func TestCancellationAfterStreamDeliveryUsesOnlyFailedReports(t *testing.T) { } func TestCleanupFailureKeepsTemporaryDirectoryForRetry(t *testing.T) { - session, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "output.json")}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "output.json")}) if err != nil { t.Fatal(err) } @@ -600,13 +600,14 @@ func TestSelectionAndAbortPreserveTranscript(t *testing.T) { if err := os.WriteFile(output, []byte("original"), 0600); err != nil { t.Fatal(err) } - session, err := SelectOutput(OutputSelection{TranscriptPath: output}) + checked, err := CheckOutput(OutputSelection{TranscriptPath: output}) if err != nil { t.Fatal(err) } if entries, err := os.ReadDir(dir); err != nil || len(entries) != 1 { t.Fatalf("selection changed directory: %v %v", entries, err) } + session := NewOutputSession(checked) if err := session.Prepare(); err != nil { t.Fatal(err) } @@ -627,7 +628,7 @@ func TestSelectionAndAbortPreserveTranscript(t *testing.T) { func TestStdoutAndDeviceDelivery(t *testing.T) { var stdout bytes.Buffer - session, err := PrepareOutput(OutputSelection{Stdout: &stdout}) + session, err := prepareOutputForTest(OutputSelection{Stdout: &stdout}) if err != nil { t.Fatal(err) } @@ -637,7 +638,7 @@ func TestStdoutAndDeviceDelivery(t *testing.T) { if stdout.String() != "transcript" { t.Fatalf("stdout=%q", stdout.String()) } - session, err = PrepareOutput(OutputSelection{TranscriptPath: os.DevNull}) + session, err = prepareOutputForTest(OutputSelection{TranscriptPath: os.DevNull}) if err != nil { t.Fatal(err) } @@ -655,7 +656,7 @@ func TestFinishRetainsPrimaryFailureAndAttemptsReports(t *testing.T) { if err := os.WriteFile(output, []byte("original"), 0600); err != nil { t.Fatal(err) } - session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true}) if err != nil { t.Fatal(err) } @@ -707,7 +708,7 @@ type shortOutput struct{} func (shortOutput) Write(b []byte) (int, error) { return len(b) / 2, nil } func TestShortStdoutWriteIsFailure(t *testing.T) { - session, err := PrepareOutput(OutputSelection{Stdout: shortOutput{}}) + session, err := prepareOutputForTest(OutputSelection{Stdout: shortOutput{}}) if err != nil { t.Fatal(err) } @@ -723,7 +724,7 @@ func TestTranscriptWriteFailureCleansTemporaryResource(t *testing.T) { if err := os.WriteFile(output, []byte("original"), 0600); err != nil { t.Fatal(err) } - session, err := PrepareOutput(OutputSelection{TranscriptPath: output}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: output}) if err != nil { t.Fatal(err) } @@ -746,7 +747,7 @@ func TestTranscriptWriteFailureCleansTemporaryResource(t *testing.T) { func TestNamedPipeIsNotOpenedDuringPreparation(t *testing.T) { pipe := filepath.Join(t.TempDir(), "output-pipe") makeNamedPipe(t, pipe) - session, err := PrepareOutput(OutputSelection{TranscriptPath: pipe}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: pipe}) if err != nil { t.Fatal(err) } @@ -761,7 +762,7 @@ func TestNamedPipeIsNotOpenedDuringPreparation(t *testing.T) { func TestLateNamedPipeIsPreserved(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "output") - session, err := PrepareOutput(OutputSelection{TranscriptPath: path}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: path}) if err != nil { t.Fatal(err) } @@ -784,7 +785,7 @@ func TestLateNamedPipeIsPreserved(t *testing.T) { func TestStreamChangedToRegularFileIsPreserved(t *testing.T) { path := filepath.Join(t.TempDir(), "output") makeNamedPipe(t, path) - session, err := PrepareOutput(OutputSelection{TranscriptPath: path}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: path}) if err != nil { t.Fatal(err) } @@ -814,3 +815,15 @@ func makeNamedPipe(t *testing.T, path string) { t.Fatalf("mkfifo: %v %s", err, output) } } + +func prepareOutputForTest(selection OutputSelection) (*OutputSession, error) { + checked, err := CheckOutput(selection) + if err != nil { + return nil, err + } + session := NewOutputSession(checked) + if err := session.Prepare(); err != nil { + return nil, err + } + return session, nil +} diff --git a/internal/core/diagnostics/output_stream_test.go b/internal/core/diagnostics/output_stream_test.go index b470ec1..a738bf4 100644 --- a/internal/core/diagnostics/output_stream_test.go +++ b/internal/core/diagnostics/output_stream_test.go @@ -23,7 +23,7 @@ func TestTranscriptStreamDestinations(t *testing.T) { t.Fatal(err) } // Preparation must finish even though the pipe has no reader yet. - session, err := PrepareOutput(OutputSelection{TranscriptPath: path}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: path}) if err != nil { t.Fatal(err) } @@ -59,7 +59,7 @@ func TestTranscriptStreamWriteFailure(t *testing.T) { if _, err := os.Stat("/dev/full"); os.IsNotExist(err) { t.Skip("/dev/full unavailable") } - session, err := PrepareOutput(OutputSelection{TranscriptPath: "/dev/full"}) + session, err := prepareOutputForTest(OutputSelection{TranscriptPath: "/dev/full"}) if err != nil { t.Fatal(err) } diff --git a/internal/framework/preflight/preflight.go b/internal/framework/preflight/preflight.go index 2bd5221..d31f753 100644 --- a/internal/framework/preflight/preflight.go +++ b/internal/framework/preflight/preflight.go @@ -54,13 +54,14 @@ func Prepare(ctx context.Context, input Input) (*Prepared, error) { if err != nil { return nil, err } - session, err := diagnostics.SelectOutput(input.Output) + checked, err := diagnostics.CheckOutput(input.Output) if err != nil { return nil, err } if err := ctx.Err(); err != nil { return nil, err } + session := diagnostics.NewOutputSession(checked) if err := session.Prepare(); err != nil { return nil, err }