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

@@ -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
}

View File

@@ -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 = &copy
@@ -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 {

View File

@@ -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)

View File

@@ -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")