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

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