Read selected remote sessions directly without listing run history

This commit is contained in:
2026-11-18 04:12:56 +00:00
parent f85f93bdfb
commit 35eaefd05d
5 changed files with 35 additions and 37 deletions

View File

@@ -300,6 +300,11 @@ Retain contributor accuracy, ordering, and secret-free inspection output.
## 9. P3: Read the selected remote session without enumerating run history ## 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. Stage: 7.
Location: `internal/app/config_loader.go:194–228`, `findRemoteSessionConfig`. Location: `internal/app/config_loader.go:194–228`, `findRemoteSessionConfig`.

View File

@@ -13,7 +13,6 @@ type commandDependencies struct {
diagnostics io.Writer diagnostics io.Writer
pipelineSearch *configkit.Search pipelineSearch *configkit.Search
loadPipelineConfig func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) loadPipelineConfig func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error)
downloadObjectToTemp func(context.Context, storage.ObjectStore, string, string) (string, error)
loadSecretsDir func(string) (*configkit.SecretsReport, error) loadSecretsDir func(string) (*configkit.SecretsReport, error)
secretsBootstrap *secretsBootstrap secretsBootstrap *secretsBootstrap
newObjectStore func(context.Context, *config.Config) (storage.ObjectStore, error) newObjectStore func(context.Context, *config.Config) (storage.ObjectStore, error)
@@ -29,9 +28,6 @@ func normalizeCommandDependencies(deps commandDependencies) commandDependencies
if deps.loadPipelineConfig == nil { if deps.loadPipelineConfig == nil {
deps.loadPipelineConfig = config.LoadPipelineWithOptions deps.loadPipelineConfig = config.LoadPipelineWithOptions
} }
if deps.downloadObjectToTemp == nil {
deps.downloadObjectToTemp = storage.DownloadObjectToTemp
}
if deps.loadSecretsDir == nil { if deps.loadSecretsDir == nil {
deps.loadSecretsDir = configkit.LoadSecretsIntoEnvironment deps.loadSecretsDir = configkit.LoadSecretsIntoEnvironment
} }

View File

@@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "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)) 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 { if err != nil {
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error()) return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error())
} }
defer body.Close()
remoteKey = sessionInfo.Key remoteKey = sessionInfo.Key
sessionTempPath, err := deps.downloadObjectToTemp(ctx, store, remoteKey, "narratio-session-*.yml") temporary, err := os.CreateTemp("", "narratio-session-*.yml")
if err != nil { if err != nil {
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err)) return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
} }
sessionTempPath := temporary.Name()
cleanup = func() error { cleanup = func() error {
if err := fileops.RemoveAllUnderRoot(filepath.Dir(sessionTempPath), sessionTempPath); err != nil { if err := fileops.RemoveAllUnderRoot(filepath.Dir(sessionTempPath), sessionTempPath); err != nil {
return fmt.Errorf("remove downloaded remote session config: %w", err) return fmt.Errorf("remove downloaded remote session config: %w", err)
} }
return nil 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) sessionBytes, err := os.ReadFile(sessionTempPath)
if err != nil { if err != nil {
return nil, fmt.Errorf("read downloaded remote session config: %w", err) 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 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) authored, err := store.Exists(ctx, remoteKey)
if err != nil { 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 var selected *artifacts.RemoteArtifact
if !authored { 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" { if item.Type == artifacts.RemoteArtifactTypeRunFile && item.Source == "run:prepare/inputs/session.yml" {
identity, err := artifacts.ParseImmutableRunObjectKey(sessionPrefix, item.DestinationKey) identity, err := artifacts.ParseImmutableRunObjectKey(sessionPrefix, item.DestinationKey)
if err != nil || "run:"+identity.RelativePath != item.Source { 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 copy := item
selected = &copy selected = &copy
@@ -213,25 +221,24 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses
} }
} }
} else if !errors.Is(err, artifacts.ErrCurrentCommitPointerMissing) { } 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 { if err != nil {
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q list failed: %w", remoteKey, err) 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)
} }
for _, obj := range objects {
if obj.Key == remoteKey {
if selected != nil { if selected != nil {
if obj.Size != selected.Size || strings.TrimSpace(obj.ETag) != selected.Generation { if info.Size != selected.Size || strings.TrimSpace(info.ETag) != selected.Generation {
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q differs from committed object identity", remoteKey) _ = body.Close()
return storage.ObjectInfo{}, "", nil, fmt.Errorf("remote session %q differs from committed object identity", remoteKey)
} }
return obj, selected.SHA256, nil return info, selected.SHA256, body, nil
} }
return obj, "", nil return info, "", body, nil
}
}
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q not found", remoteKey)
} }
func s3BucketName(cfg *config.PipelineConfig) string { func s3BucketName(cfg *config.PipelineConfig) string {

View File

@@ -209,6 +209,7 @@ func TestRemoteSessionConfigCloseIsIdempotent(t *testing.T) {
workspaceRoot := t.TempDir() workspaceRoot := t.TempDir()
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot) pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)
fake := &storage.FakeBackend{} 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 seedRemoteSessionConfig(t, fake, "2026-05-03", `session_id: 2026-05-03
inputs: inputs:
audio_s3: audio_s3:
@@ -217,12 +218,11 @@ inputs:
setSessionConfigSearchPaths(t, []string{filepath.Join(t.TempDir(), "session.yml")}) setSessionConfigSearchPaths(t, []string{filepath.Join(t.TempDir(), "session.yml")})
deps, _ := remoteSessionLoaderDependencies(fake) 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) loaded, err := loadCommandConfigWithDependencies(context.Background(), pipelinePath, "", campaignPath, "", config.SessionLoadOptions{SessionID: "2026-05-03"}, nil, deps)
if err != nil { if err != nil {
t.Fatalf("loadCommandConfig() error = %v", err) t.Fatalf("loadCommandConfig() error = %v", err)
} }
downloadedPath := loaded.Config.SessionSource.LocalPath
if err := loaded.Close(); err != nil { if err := loaded.Close(); err != nil {
t.Fatalf("first Close() error = %v", err) 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) { func TestExecuteRemoteSessionFallbackLoadsSecretsBeforeObjectStoreInit(t *testing.T) {
workspaceRoot := t.TempDir() workspaceRoot := t.TempDir()
pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot) pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot)

View File

@@ -133,14 +133,14 @@ inputs:
t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String()) t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String())
} }
downloaded := false downloaded := false
for _, call := range fake.Downloads { for _, call := range fake.Reads {
if call.Key == remoteKey { if call.Key == remoteKey {
downloaded = true downloaded = true
break break
} }
} }
if !downloaded { 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 { if storeInitCalls == 0 {
t.Fatal("object store was not initialized") t.Fatal("object store was not initialized")