Read publication inventory without execution snapshots

This commit is contained in:
2026-12-02 15:44:52 +00:00
parent 7ad8deb381
commit ebdf8b522f
14 changed files with 194 additions and 41 deletions

View File

@@ -24,6 +24,7 @@ type commandDependencies struct {
newAudita func(*config.Config) (audita.Runner, error)
newNotarius func() notarius.Runner
discoverRemoteCurrentState func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error)
discoverRemotePublication func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error)
buildRestorePlan func(context.Context, *config.Config, *RemoteCurrentState, storage.ObjectStore, RestorePlanOptions) (*RestorePlan, error)
executeRestorePlan func(context.Context, *config.Config, *RemoteCurrentState, *RestorePlan, *RestoreReport, storage.ObjectStore) (*RestoreExecutionResult, error)
}
@@ -55,6 +56,9 @@ func normalizeCommandDependencies(deps commandDependencies) commandDependencies
if deps.discoverRemoteCurrentState == nil {
deps.discoverRemoteCurrentState = discoverRemoteCurrentState
}
if deps.discoverRemotePublication == nil {
deps.discoverRemotePublication = discoverRemotePublication
}
if deps.buildRestorePlan == nil {
deps.buildRestorePlan = buildRestorePlan
}

View File

@@ -108,7 +108,7 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses
}
var selected *artifacts.RemoteArtifact
if !authored {
current, err := artifacts.LoadCurrentState(ctx, store, sessionPrefix, artifacts.CurrentStateValidation{ExpectedCampaign: campaign, ExpectedSessionID: sessionID, ValidateRunID: true})
current, err := artifacts.LoadCurrentInventory(ctx, store, sessionPrefix, artifacts.CurrentStateValidation{ExpectedCampaign: campaign, ExpectedSessionID: sessionID, ValidateRunID: true})
if err == nil {
for _, item := range current.Commit.Artifacts {
if item.Type == artifacts.RemoteArtifactTypeRunFile && item.Source == "run:prepare/inputs/session.yml" {

View File

@@ -162,7 +162,7 @@ func inspectRemoteCurrentStateWithDependencies(ctx context.Context, cfg *config.
return remoteCurrentStateCheck{}
}
deps = normalizeCommandDependencies(deps)
current, err := deps.discoverRemoteCurrentState(ctx, cfg, store)
current, err := deps.discoverRemotePublication(ctx, cfg, store)
if err != nil {
return remoteCurrentStateCheck{Err: err}
}

View File

@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
@@ -533,3 +534,71 @@ func addSecretsToPipelineConfig(t *testing.T, pipelinePath, secretsDir, accessKe
}
configureTestSecrets(t, pipelinePath, secretsDir)
}
func TestPublicationInspectionAndSessionConfigIgnoreExecutionSnapshot(t *testing.T) {
for _, tc := range []struct {
name string
omit bool
body []byte
}{
{"no snapshot", true, nil}, {"missing snapshot", false, nil}, {"malformed snapshot", false, []byte("not json")}, {"unsupported snapshot", false, []byte(`{"schema_version":"unsupported"}`)},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := restorePlanConfig(t)
fake := &storage.FakeBackend{}
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/final.trimmed.json": []byte(`{"segments":[]}`)})
sessionKey := artifacts.S3RunRelativeDestinationKey(artifacts.S3RunPrefix(current.SessionPrefix, current.RunID), "prepare/inputs/session.yml")
fake.SeedObject(storage.FakeObject{Key: sessionKey, Data: []byte("session_id: 2026-05-03\ntitle: Inventory\n")})
current.Commit.Artifacts = append(current.Commit.Artifacts, remoteRestoreArtifact(fake, artifacts.RemoteArtifactTypeRunFile, "run:prepare/inputs/session.yml", sessionKey))
if tc.omit {
filtered := current.Commit.Artifacts[:0]
for _, item := range current.Commit.Artifacts {
if item.Type != artifacts.RemoteArtifactTypeSessionManifest {
filtered = append(filtered, item)
}
}
current.Commit.Artifacts = filtered
}
if tc.body == nil {
delete(fake.Objects, current.CurrentManifestKey)
} else {
fake.SeedObject(storage.FakeObject{Key: current.CurrentManifestKey, Data: tc.body})
}
commitData, err := artifacts.EncodeRemoteCommitManifest(*current.Commit)
if err != nil {
t.Fatal(err)
}
commitKey := artifacts.S3RunCommitKey(current.SessionPrefix, current.RunID)
fake.SeedObject(storage.FakeObject{Key: commitKey, Data: commitData})
pointer, err := artifacts.EncodeCurrentCommitPointer(artifacts.CurrentCommitPointer{FormatVersion: artifacts.RemoteCommitFormatVersion, Campaign: cfg.Session.Campaign, SessionID: cfg.Session.SessionID, RunID: current.RunID, CommitKey: commitKey, CommitSHA256: restoreCommitSHA256(commitData), CommitSize: int64(len(commitData)), CommitGeneration: fake.Objects[commitKey].ETag})
if err != nil {
t.Fatal(err)
}
fake.SeedObject(storage.FakeObject{Key: artifacts.S3CurrentCommitPointerKey(current.SessionPrefix), Data: pointer})
fake.Reads = nil
publication, err := discoverRemotePublication(context.Background(), cfg, fake)
if err != nil || publication.RunID != current.RunID {
t.Fatalf("publication %+v %v", publication, err)
}
_, _, body, err := findRemoteSessionConfig(context.Background(), fake, current.SessionPrefix, current.SessionPrefix+"session.yml", cfg.Session.Campaign, cfg.Session.SessionID)
if err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(body)
if err != nil {
t.Fatal(err)
}
if err := body.Close(); err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), "title: Inventory") {
t.Fatalf("session %s", data)
}
for _, read := range fake.Reads {
if read.Key == current.CurrentManifestKey {
t.Fatal("execution snapshot was read")
}
}
})
}
}

View File

@@ -225,19 +225,14 @@ func TestCommittedStatusReportsCarriedOutputOriginAndChecksExactKey(t *testing.T
fake.SeedObject(storage.FakeObject{Key: oldKey, Data: []byte("carried\n")})
current.Commit.Artifacts[1].Source = "narratio.transcript.final_trimmed"
current.Commit.Artifacts[1].DestinationKey = oldKey
current.Manifest.Stages["publish"] = &manifest.StageRecord{
Name: "publish", Status: manifest.StatusSucceeded,
Metadata: map[string]any{"publish_output_outcomes": []any{map[string]any{
"source": "narratio.transcript.final_trimmed", "status": "carried_locked",
}}},
}
current.Manifest = nil
outputs, err := inspectPublishedOutputs(context.Background(), cfg, fake, nil, current, nil, true, "available")
if err != nil || len(outputs) != 1 {
t.Fatalf("outputs=%#v err=%v", outputs, err)
}
output := outputs[0]
if output.Outcome != "carried_locked" || output.State != "published" || output.Destination != "transcripts/full.json" || output.DeclaredDestination != "transcripts/original.json" || output.DeclaredKey != oldKey || output.OriginRun != originRun {
if output.State != "published" || output.Destination != "transcripts/full.json" || output.DeclaredDestination != "transcripts/original.json" || output.DeclaredKey != oldKey || output.OriginRun != originRun {
t.Fatalf("carried output=%#v", output)
}
if len(fake.ExistsKeys) == 0 || fake.ExistsKeys[len(fake.ExistsKeys)-1] != oldKey {

View File

@@ -24,6 +24,25 @@ type RemoteCurrentState struct {
Commit *artifacts.RemoteCommitManifest
}
func discoverRemotePublication(ctx context.Context, cfg *config.Config, store storage.ObjectStore) (*RemoteCurrentState, error) {
if cfg == nil || cfg.Pipeline == nil || cfg.Session == nil || store == nil {
return nil, fmt.Errorf("resolved config and remote object store are required")
}
bucket := artifacts.ResolvePublishBucket(cfg, nil)
if strings.TrimSpace(bucket) == "" {
return nil, fmt.Errorf("publish bucket is required")
}
prefix, err := artifacts.ResolvePublishSessionPrefix(cfg, nil)
if err != nil {
return nil, fmt.Errorf("resolve publish session prefix: %w", err)
}
current, err := artifacts.LoadCurrentInventory(ctx, store, prefix, artifacts.CurrentStateValidation{ExpectedSessionID: cfg.Session.SessionID, ExpectedCampaign: cfg.Session.Campaign})
if err != nil {
return nil, fmt.Errorf("remote %w", err)
}
return &RemoteCurrentState{Bucket: bucket, SessionPrefix: prefix, RunID: current.Commit.RunID, SessionID: current.Commit.SessionID, Campaign: current.Commit.Campaign, Commit: current.Commit}, nil
}
func discoverRemoteCurrentState(ctx context.Context, cfg *config.Config, store storage.ObjectStore) (*RemoteCurrentState, error) {
if cfg == nil || cfg.Pipeline == nil || cfg.Session == nil {
return nil, fmt.Errorf("resolved config with pipeline/session is required")

View File

@@ -110,7 +110,7 @@ func TestSessionStateCommandReturnsPartialRemoteFailure(t *testing.T) {
failure := errors.New("synthetic remote failure")
fake := &storage.FakeBackend{}
deps, _ := remoteSessionLoaderDependencies(fake)
deps.discoverRemoteCurrentState = func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error) {
deps.discoverRemotePublication = func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error) {
return nil, failure
}
app, err := newApplication(deps, executePlan)
@@ -150,7 +150,7 @@ func TestPublishedOutputInspectionKeepsCarriedIdentityWithoutPayloadReads(t *tes
fake.SeedObject(storage.FakeObject{Key: key, Data: []byte("carried\n")})
current.Commit.Artifacts[1].Source = "narratio.transcript.final_trimmed"
current.Commit.Artifacts[1].DestinationKey = key
current.Manifest.Stages["publish"] = &manifest.StageRecord{Name: "publish", Status: manifest.StatusSucceeded, Metadata: map[string]any{"publish_output_outcomes": []any{map[string]any{"source": "narratio.transcript.final_trimmed", "status": "carried_locked"}}}}
current.Manifest = nil
reads := len(fake.Reads)
outputs, err := inspectPublishedOutputs(context.Background(), cfg, fake, nil, current, []artifacts.RemoteLock{{Source: "narratio.transcript.final_trimmed"}}, true, "current")
if err != nil {
@@ -160,7 +160,7 @@ func TestPublishedOutputInspectionKeepsCarriedIdentityWithoutPayloadReads(t *tes
t.Fatalf("outputs=%#v", outputs)
}
output := outputs[0]
if output.State != "published" || output.Outcome != "carried_locked" || output.LockState != "locked" || output.DeclaredKey != key || output.DeclaredDestination != "transcripts/original.json" || output.OriginRun != originRun {
if output.State != "published" || output.LockState != "locked" || output.DeclaredKey != key || output.DeclaredDestination != "transcripts/original.json" || output.OriginRun != originRun {
t.Fatalf("output=%#v", output)
}
if len(fake.Reads) != reads || fake.ExistsKeys[len(fake.ExistsKeys)-1] != key {
@@ -247,16 +247,13 @@ func TestPublishedOutputInspectionNormalizesAcceptedSources(t *testing.T) {
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{"transcripts/full.json": []byte("published\n")})
declaration := &current.Commit.Artifacts[1]
declaration.Source = canonical
current.Manifest.Stages["publish"] = &manifest.StageRecord{
Name: "publish", Status: manifest.StatusSucceeded,
Metadata: map[string]any{"publish_output_outcomes": []any{map[string]any{"source": canonical, "status": "carried_locked"}}},
}
current.Manifest = nil
outputs, err := inspectPublishedOutputs(context.Background(), cfg, fake, nil, current, []artifacts.RemoteLock{{Source: canonical}}, true, "current")
if err != nil || len(outputs) != 1 {
t.Fatalf("outputs=%#v err=%v", outputs, err)
}
output := outputs[0]
if output.Source != canonical || output.State != "published" || output.LockState != "locked" || output.Outcome != "carried_locked" || output.DeclaredKey != declaration.DestinationKey || output.OriginRun != current.RunID {
if output.Source != canonical || output.State != "published" || output.LockState != "locked" || output.DeclaredKey != declaration.DestinationKey || output.OriginRun != current.RunID {
t.Fatalf("accepted source lost publication evidence: %#v", output)
}
if cfg.PublicationOutputs()[0].Source != source {

View File

@@ -176,7 +176,6 @@ func inspectSessionState(ctx context.Context, cfg *config.Config, status, remote
} else if current.State != nil {
report.Remote.State = "current"
report.Remote.RunID = current.State.RunID
report.Remote.ManifestKey = current.State.CurrentManifestKey
}
locks := inspectRemoteLocks(ctx, cfg, store)
fail("locks", locks.Err)
@@ -265,9 +264,7 @@ func inspectPublishedOutputs(ctx context.Context, cfg *config.Config, store stor
configuredOutputs := helperConfiguredOutputPathMap(catalog)
extractionOutputs := helperExtractionOutputSet(catalog)
var declarations map[string]artifacts.RemoteArtifact
outcomes := map[string]string{}
if current != nil {
outcomes = committedPublishOutputOutcomes(current.Manifest)
declarations = committedPublishedOutputs(current.Commit)
}
var resultErr error
@@ -275,7 +272,7 @@ func inspectPublishedOutputs(ctx context.Context, cfg *config.Config, store stor
source := strings.TrimSpace(rule.Source)
destination, err := artifactpolicy.ResolvePublishedDestinationWithExtractions(source, rule.Dest, configuredOutputs, extractionOutputs)
_, isLocked := locked[source]
output := sessionPublishedOutput{Source: source, Destination: destination, Required: rule.Required == nil || *rule.Required, LockState: "unknown", State: "missing", Outcome: outcomes[source]}
output := sessionPublishedOutput{Source: source, Destination: destination, Required: rule.Required == nil || *rule.Required, LockState: "unknown", State: "missing"}
if locksKnown {
output.LockState = "unlocked"
if isLocked {

View File

@@ -49,6 +49,21 @@ func LoadCurrentState(
store storage.ObjectStore,
sessionPrefix string,
validation CurrentStateValidation,
) (*CurrentState, error) {
state, err := LoadCurrentInventory(ctx, store, sessionPrefix, validation)
if err != nil {
return nil, err
}
return loadCommittedCurrentState(ctx, store, state, validation)
}
// LoadCurrentInventory reads the selected publication identity and declarations
// without fetching an execution snapshot.
func LoadCurrentInventory(
ctx context.Context,
store storage.ObjectStore,
sessionPrefix string,
validation CurrentStateValidation,
) (*CurrentState, error) {
prefix := strings.TrimSpace(sessionPrefix)
if prefix == "" {
@@ -66,7 +81,7 @@ func LoadCurrentState(
if !pointerExists {
return nil, &CurrentCommitPointerMissingError{Key: pointerKey}
}
return loadCommittedCurrentState(ctx, store, prefix, pointerKey, validation)
return loadCommittedInventory(ctx, store, prefix, pointerKey, validation)
}
func ValidateCurrentStateIdentity(state *CurrentState, validation CurrentStateValidation) error {

View File

@@ -19,7 +19,7 @@ const (
MaxRemoteSessionManifestBytes int64 = 8 << 20
)
func loadCommittedCurrentState(
func loadCommittedInventory(
ctx context.Context,
store storage.ObjectStore,
sessionPrefix string,
@@ -52,8 +52,20 @@ func loadCommittedCurrentState(
if err := validatePointerCommitIdentity(pointer, commit); err != nil {
return nil, err
}
if strings.TrimSpace(validation.ExpectedCampaign) != "" && commit.Campaign != strings.TrimSpace(validation.ExpectedCampaign) {
return nil, fmt.Errorf("remote commit campaign %q does not match expected campaign %q", commit.Campaign, validation.ExpectedCampaign)
}
if strings.TrimSpace(validation.ExpectedSessionID) != "" && commit.SessionID != strings.TrimSpace(validation.ExpectedSessionID) {
return nil, fmt.Errorf("remote commit session_id %q does not match expected session_id %q", commit.SessionID, validation.ExpectedSessionID)
}
if strings.TrimSpace(validation.ExpectedRunID) != "" && commit.RunID != strings.TrimSpace(validation.ExpectedRunID) {
return nil, fmt.Errorf("remote commit run_id %q does not match expected run_id %q", commit.RunID, validation.ExpectedRunID)
}
return &CurrentState{SessionPrefix: sessionPrefix, CurrentPointerKey: pointerKey, RunID: commit.RunID, Commit: commit, Pointer: pointer}, nil
}
sessionManifest, ok := commit.Artifact(RemoteArtifactTypeSessionManifest)
func loadCommittedCurrentState(ctx context.Context, store storage.ObjectStore, state *CurrentState, validation CurrentStateValidation) (*CurrentState, error) {
sessionManifest, ok := state.Commit.Artifact(RemoteArtifactTypeSessionManifest)
if !ok {
return nil, fmt.Errorf("remote commit must declare exactly one session manifest artifact")
}
@@ -65,20 +77,13 @@ func loadCommittedCurrentState(
if err != nil {
return nil, err
}
if err := validateCommitManifestIdentity(commit, m); err != nil {
if err := validateCommitManifestIdentity(state.Commit, m); err != nil {
return nil, err
}
state := &CurrentState{
SessionPrefix: sessionPrefix,
CurrentPointerKey: pointerKey,
CurrentManifestKey: sessionManifest.DestinationKey,
RunID: commit.RunID,
Manifest: m,
ManifestData: append([]byte(nil), manifestData...),
Commit: commit,
Pointer: pointer,
}
state.CurrentManifestKey = sessionManifest.DestinationKey
state.Manifest = m
state.ManifestData = append([]byte(nil), manifestData...)
if err := ValidateCurrentStateIdentity(state, validation); err != nil {
return nil, err
}

View File

@@ -397,3 +397,52 @@ func appendUnknownJSONField(t *testing.T, data []byte) []byte {
}
return append(append([]byte(nil), trimmed[:len(trimmed)-1]...), []byte(`,"unexpected":true}`)...)
}
func TestCurrentInventoryDoesNotReadExecutionSnapshot(t *testing.T) {
for _, tc := range []struct {
name string
snapshot bool
body []byte
}{
{"no snapshot", false, nil}, {"missing snapshot", true, nil}, {"malformed snapshot", true, []byte("not json")}, {"unsupported snapshot", true, []byte(`{"schema_version":"unsupported"}`)},
} {
t.Run(tc.name, func(t *testing.T) {
prefix := testCurrentSessionPrefix()
runID := "20260519T010203Z-a1b2c3d4"
commit := testRemoteCommit(t, runID)
old := commit.Artifacts[0]
commit.Artifacts = []RemoteArtifact{{Type: RemoteArtifactTypePublishedOutput, Source: ArtifactTranscriptFinalTrimmed, DestinationKey: S3RunRelativeDestinationKey(S3RunPrefix(prefix, runID), "published/transcripts/final.trimmed.json"), SHA256: strings.Repeat("a", 64), Size: 1, Generation: "output-generation"}}
if tc.snapshot {
commit.Artifacts = append(commit.Artifacts, old)
}
store := &storage.FakeBackend{}
if tc.body != nil {
store.SeedObject(storage.FakeObject{Key: old.DestinationKey, Data: tc.body, ETag: "irrelevant"})
}
data, err := EncodeRemoteCommitManifest(commit)
if err != nil {
t.Fatal(err)
}
key := S3RunCommitKey(prefix, runID)
store.SeedObject(storage.FakeObject{Key: key, Data: data, ETag: "commit-generation"})
pointer := testCommitPointer(t, prefix, commit)
encoded, err := EncodeCurrentCommitPointer(pointer)
if err != nil {
t.Fatal(err)
}
store.SeedObject(storage.FakeObject{Key: S3CurrentCommitPointerKey(prefix), Data: encoded})
current, err := LoadCurrentInventory(context.Background(), store, prefix, CurrentStateValidation{ExpectedCampaign: commit.Campaign, ExpectedSessionID: commit.SessionID, ExpectedRunID: runID})
if err != nil {
t.Fatal(err)
}
if current.Manifest != nil || current.Commit.RunID != runID {
t.Fatalf("inventory %+v", current)
}
for _, read := range store.Reads {
if read.Key == old.DestinationKey {
t.Fatal("execution snapshot was read")
}
}
})
}
}