From 35eaefd05d4d68da28e92dd827433a0a30d3f413 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Wed, 18 Nov 2026 04:12:56 +0000 Subject: [PATCH] Read selected remote sessions directly without listing run history --- docs/roadmap/remediation.md | 5 +++ internal/app/command_dependencies.go | 4 -- internal/app/config_loader.go | 45 +++++++++++++---------- internal/app/remote_session_test.go | 14 +------ internal/app/session_oriented_cli_test.go | 4 +- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index b4efef9..e73faf8 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -300,6 +300,11 @@ Retain contributor accuracy, ordering, and secret-free inspection output. ## 9. P3: Read the selected remote session without enumerating run history +Status: Complete. Remote loading opens the selected object once, verifies its +returned identity, and streams into a command-owned temporary file. Remote and +publication round trips pass under race; a rejecting-list fake checks enumeration +is unnecessary, and cleanup remains idempotent. + Stage: 7. Location: `internal/app/config_loader.go:194–228`, `findRemoteSessionConfig`. diff --git a/internal/app/command_dependencies.go b/internal/app/command_dependencies.go index 7ef8a58..1c481cd 100644 --- a/internal/app/command_dependencies.go +++ b/internal/app/command_dependencies.go @@ -13,7 +13,6 @@ type commandDependencies struct { diagnostics io.Writer pipelineSearch *configkit.Search loadPipelineConfig func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) - downloadObjectToTemp func(context.Context, storage.ObjectStore, string, string) (string, error) loadSecretsDir func(string) (*configkit.SecretsReport, error) secretsBootstrap *secretsBootstrap newObjectStore func(context.Context, *config.Config) (storage.ObjectStore, error) @@ -29,9 +28,6 @@ func normalizeCommandDependencies(deps commandDependencies) commandDependencies if deps.loadPipelineConfig == nil { deps.loadPipelineConfig = config.LoadPipelineWithOptions } - if deps.downloadObjectToTemp == nil { - deps.downloadObjectToTemp = storage.DownloadObjectToTemp - } if deps.loadSecretsDir == nil { deps.loadSecretsDir = configkit.LoadSecretsIntoEnvironment } diff --git a/internal/app/config_loader.go b/internal/app/config_loader.go index 54010ae..f57a69e 100644 --- a/internal/app/config_loader.go +++ b/internal/app/config_loader.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "fmt" + "io" "os" "path/filepath" "strings" @@ -93,21 +94,28 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err)) } - sessionInfo, checksum, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey, config.CampaignID(base.Campaign), sessionID) + sessionInfo, checksum, body, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey, config.CampaignID(base.Campaign), sessionID) if err != nil { return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error()) } + defer body.Close() remoteKey = sessionInfo.Key - sessionTempPath, err := deps.downloadObjectToTemp(ctx, store, remoteKey, "narratio-session-*.yml") + temporary, err := os.CreateTemp("", "narratio-session-*.yml") if err != nil { return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err)) } + sessionTempPath := temporary.Name() cleanup = func() error { if err := fileops.RemoveAllUnderRoot(filepath.Dir(sessionTempPath), sessionTempPath); err != nil { return fmt.Errorf("remove downloaded remote session config: %w", err) } return nil } + _, copyErr := io.Copy(temporary, body) + closeErr := temporary.Close() + if err := errors.Join(copyErr, closeErr); err != nil { + return nil, fmt.Errorf("download remote session %q: %w", remoteKey, err) + } sessionBytes, err := os.ReadFile(sessionTempPath) if err != nil { return nil, fmt.Errorf("read downloaded remote session config: %w", err) @@ -191,10 +199,10 @@ func loadPipelineConfigWithDependencies(pipelineFlag string, opts config.Pipelin return resolvedPipelinePath, pipelineCfg, nil } -func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, sessionPrefix, remoteKey, campaign, sessionID string) (storage.ObjectInfo, string, error) { +func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, sessionPrefix, remoteKey, campaign, sessionID string) (storage.ObjectInfo, string, io.ReadCloser, error) { authored, err := store.Exists(ctx, remoteKey) if err != nil { - return storage.ObjectInfo{}, "", fmt.Errorf("check authored session %q: %w", remoteKey, err) + return storage.ObjectInfo{}, "", nil, fmt.Errorf("check authored session %q: %w", remoteKey, err) } var selected *artifacts.RemoteArtifact if !authored { @@ -204,7 +212,7 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses if item.Type == artifacts.RemoteArtifactTypeRunFile && item.Source == "run:prepare/inputs/session.yml" { identity, err := artifacts.ParseImmutableRunObjectKey(sessionPrefix, item.DestinationKey) if err != nil || "run:"+identity.RelativePath != item.Source { - return storage.ObjectInfo{}, "", fmt.Errorf("committed session source does not match its object key") + return storage.ObjectInfo{}, "", nil, fmt.Errorf("committed session source does not match its object key") } copy := item selected = © @@ -213,25 +221,24 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses } } } else if !errors.Is(err, artifacts.ErrCurrentCommitPointerMissing) { - return storage.ObjectInfo{}, "", fmt.Errorf("load remote session selection: %w", err) + return storage.ObjectInfo{}, "", nil, fmt.Errorf("load remote session selection: %w", err) } } - objects, err := store.List(ctx, sessionPrefix) + info, body, err := store.Read(ctx, remoteKey) if err != nil { - return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q list failed: %w", remoteKey, err) - } - for _, obj := range objects { - if obj.Key == remoteKey { - if selected != nil { - if obj.Size != selected.Size || strings.TrimSpace(obj.ETag) != selected.Generation { - return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q differs from committed object identity", remoteKey) - } - return obj, selected.SHA256, nil - } - return obj, "", nil + if errors.Is(err, os.ErrNotExist) { + return storage.ObjectInfo{}, "", nil, fmt.Errorf("remote session %q not found: %w", remoteKey, err) } + return storage.ObjectInfo{}, "", nil, fmt.Errorf("read remote session %q: %w", remoteKey, err) } - return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q not found", remoteKey) + if selected != nil { + if info.Size != selected.Size || strings.TrimSpace(info.ETag) != selected.Generation { + _ = body.Close() + return storage.ObjectInfo{}, "", nil, fmt.Errorf("remote session %q differs from committed object identity", remoteKey) + } + return info, selected.SHA256, body, nil + } + return info, "", body, nil } func s3BucketName(cfg *config.PipelineConfig) string { diff --git a/internal/app/remote_session_test.go b/internal/app/remote_session_test.go index 6683e77..f719d65 100644 --- a/internal/app/remote_session_test.go +++ b/internal/app/remote_session_test.go @@ -209,6 +209,7 @@ func TestRemoteSessionConfigCloseIsIdempotent(t *testing.T) { workspaceRoot := t.TempDir() pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot) fake := &storage.FakeBackend{} + fake.ListErr = errors.New("remote session loading must not enumerate historical objects") seedRemoteSessionConfig(t, fake, "2026-05-03", `session_id: 2026-05-03 inputs: audio_s3: @@ -217,12 +218,11 @@ inputs: setSessionConfigSearchPaths(t, []string{filepath.Join(t.TempDir(), "session.yml")}) deps, _ := remoteSessionLoaderDependencies(fake) - var downloadedPath string - deps.downloadObjectToTemp = captureRemoteSessionTempPath(&downloadedPath) loaded, err := loadCommandConfigWithDependencies(context.Background(), pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: "2026-05-03"}, nil, deps) if err != nil { t.Fatalf("loadCommandConfig() error = %v", err) } + downloadedPath := loaded.Config.SessionSource.LocalPath if err := loaded.Close(); err != nil { t.Fatalf("first Close() error = %v", err) } @@ -237,16 +237,6 @@ inputs: } } -func captureRemoteSessionTempPath(destination *string) func(context.Context, storage.ObjectStore, string, string) (string, error) { - return func(ctx context.Context, store storage.ObjectStore, key, pattern string) (string, error) { - path, err := storage.DownloadObjectToTemp(ctx, store, key, pattern) - if err == nil { - *destination = path - } - return path, err - } -} - func TestExecuteRemoteSessionFallbackLoadsSecretsBeforeObjectStoreInit(t *testing.T) { workspaceRoot := t.TempDir() pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot) diff --git a/internal/app/session_oriented_cli_test.go b/internal/app/session_oriented_cli_test.go index b846395..b3afaf0 100644 --- a/internal/app/session_oriented_cli_test.go +++ b/internal/app/session_oriented_cli_test.go @@ -133,14 +133,14 @@ inputs: t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String()) } downloaded := false - for _, call := range fake.Downloads { + for _, call := range fake.Reads { if call.Key == remoteKey { downloaded = true break } } if !downloaded { - t.Fatalf("remote session %q was not downloaded; downloads=%v", remoteKey, fake.Downloads) + t.Fatalf("remote session %q was not read; reads=%v", remoteKey, fake.Reads) } if storeInitCalls == 0 { t.Fatal("object store was not initialized")