Publish and restore prepared session documents

This commit is contained in:
2026-11-17 22:27:24 +00:00
parent bcfca7a044
commit ed7e722077
6 changed files with 238 additions and 7 deletions

View File

@@ -2,6 +2,8 @@ package app
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
@@ -91,10 +93,11 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
}
sessionInfo, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey)
sessionInfo, checksum, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey, config.CampaignID(base.Campaign), sessionID)
if err != nil {
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error())
}
remoteKey = sessionInfo.Key
sessionTempPath, err := deps.downloadObjectToTemp(ctx, store, remoteKey, "narratio-session-*.yml")
if err != nil {
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
@@ -109,6 +112,12 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
if err != nil {
return nil, fmt.Errorf("read downloaded remote session config: %w", err)
}
if checksum != "" {
digest := sha256.Sum256(sessionBytes)
if !strings.EqualFold(hex.EncodeToString(digest[:]), checksum) || int64(len(sessionBytes)) != sessionInfo.Size {
return nil, fmt.Errorf("remote session %q does not match its committed checksum or size", remoteKey)
}
}
sessionCfg, err := config.LoadSessionBytesWithOptions("s3://"+s3BucketName(base.Pipeline)+"/"+remoteKey, sessionBytes, sessionOpts)
if err != nil {
return nil, err
@@ -182,17 +191,41 @@ func loadPipelineConfigWithDependencies(pipelineFlag string, opts config.Pipelin
return resolvedPipelinePath, pipelineCfg, nil
}
func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, sessionPrefix, remoteKey string) (storage.ObjectInfo, error) {
func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, sessionPrefix, remoteKey, campaign, sessionID string) (storage.ObjectInfo, string, error) {
current, err := artifacts.LoadCurrentState(ctx, store, sessionPrefix, artifacts.CurrentStateValidation{ExpectedCampaign: campaign, ExpectedSessionID: sessionID, ValidateRunID: true})
var selected *artifacts.RemoteArtifact
if err == nil {
for _, item := range current.Commit.Artifacts {
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")
}
copy := item
selected = &copy
remoteKey = item.DestinationKey
break
}
}
} else if !errors.Is(err, artifacts.ErrCurrentCommitPointerMissing) {
return storage.ObjectInfo{}, "", fmt.Errorf("load remote session selection: %w", err)
}
objects, err := store.List(ctx, sessionPrefix)
if err != nil {
return storage.ObjectInfo{}, fmt.Errorf("remote session %q list failed: %w", remoteKey, err)
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q list failed: %w", remoteKey, err)
}
for _, obj := range objects {
if obj.Key == remoteKey {
return obj, nil
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
}
}
return storage.ObjectInfo{}, fmt.Errorf("remote session %q not found", remoteKey)
return storage.ObjectInfo{}, "", fmt.Errorf("remote session %q not found", remoteKey)
}
func s3BucketName(cfg *config.PipelineConfig) string {