Merge imported products into local session state

This commit is contained in:
2026-12-02 16:24:33 +00:00
parent ad8a5df97b
commit e7fada4372
26 changed files with 638 additions and 1254 deletions

View File

@@ -7,7 +7,6 @@ import (
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
var (
@@ -27,14 +26,11 @@ func (e *CurrentCommitPointerMissingError) Unwrap() error {
}
type CurrentState struct {
SessionPrefix string
CurrentManifestKey string
CurrentPointerKey string
RunID string
Manifest *manifest.Manifest
ManifestData []byte
Commit *RemoteCommitManifest
Pointer *CurrentCommitPointer
SessionPrefix string
CurrentPointerKey string
RunID string
Commit *RemoteCommitManifest
Pointer *CurrentCommitPointer
}
type CurrentStateValidation struct {
@@ -44,19 +40,6 @@ type CurrentStateValidation struct {
ValidateRunID bool
}
func LoadCurrentState(
ctx context.Context,
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(
@@ -83,62 +66,3 @@ func LoadCurrentInventory(
}
return loadCommittedInventory(ctx, store, prefix, pointerKey, validation)
}
func ValidateCurrentStateIdentity(state *CurrentState, validation CurrentStateValidation) error {
if state == nil || state.Manifest == nil {
return fmt.Errorf("current state with manifest is required")
}
expectedSessionID := strings.TrimSpace(validation.ExpectedSessionID)
expectedCampaign := strings.TrimSpace(validation.ExpectedCampaign)
expectedRunID := strings.TrimSpace(validation.ExpectedRunID)
manifestSessionID := strings.TrimSpace(state.Manifest.SessionID)
manifestCampaign := strings.TrimSpace(state.Manifest.Campaign)
manifestRunID := strings.TrimSpace(state.Manifest.RunID)
if manifestCampaign != "" {
if err := ValidateSessionIdentity(manifestCampaign, manifestSessionID); err != nil {
return fmt.Errorf("current manifest contains unsafe identities: %w", err)
}
}
if manifestRunID != "" {
if err := ValidateRunIdentity(manifestRunID); err != nil {
return fmt.Errorf("current manifest contains an unsafe run id: %w", err)
}
}
if expectedSessionID != "" && manifestSessionID != expectedSessionID {
return fmt.Errorf(
"current manifest session_id %q does not match expected session_id %q",
manifestSessionID,
expectedSessionID,
)
}
if expectedCampaign != "" {
if manifestCampaign == "" {
return fmt.Errorf("current manifest campaign is required")
}
if manifestCampaign != expectedCampaign {
return fmt.Errorf(
"current manifest campaign %q does not match expected campaign %q",
manifestCampaign,
expectedCampaign,
)
}
}
if expectedRunID == "" && validation.ValidateRunID {
expectedRunID = strings.TrimSpace(state.RunID)
}
if expectedRunID != "" {
if manifestRunID == "" {
return fmt.Errorf("current manifest run_id is required")
}
if manifestRunID != expectedRunID {
return fmt.Errorf(
"current run pointer %q references run %q but current manifest run_id is %q",
state.CurrentPointerKey,
expectedRunID,
manifestRunID,
)
}
}
return nil
}

View File

@@ -1,13 +1,11 @@
package artifacts
import (
"bytes"
"context"
"fmt"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
const (
@@ -15,8 +13,6 @@ const (
MaxCurrentCommitPointerBytes int64 = 64 << 10
// MaxRemoteCommitManifestBytes bounds an immutable commit manifest.
MaxRemoteCommitManifestBytes int64 = 4 << 20
// MaxRemoteSessionManifestBytes bounds the selected session manifest.
MaxRemoteSessionManifestBytes int64 = 8 << 20
)
func loadCommittedInventory(
@@ -64,32 +60,6 @@ func loadCommittedInventory(
return &CurrentState{SessionPrefix: sessionPrefix, CurrentPointerKey: pointerKey, RunID: commit.RunID, Commit: commit, Pointer: pointer}, nil
}
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")
}
manifestData, err := readVerifiedRemoteObject(ctx, store, sessionManifest.DestinationKey, sessionManifest.SHA256, sessionManifest.Size, sessionManifest.Generation, "committed session manifest", MaxRemoteSessionManifestBytes)
if err != nil {
return nil, fmt.Errorf("read committed session manifest %q: %w", sessionManifest.DestinationKey, err)
}
m, err := decodeCommittedManifest(ctx, manifestData)
if err != nil {
return nil, err
}
if err := validateCommitManifestIdentity(state.Commit, m); err != nil {
return nil, err
}
state.CurrentManifestKey = sessionManifest.DestinationKey
state.Manifest = m
state.ManifestData = append([]byte(nil), manifestData...)
if err := ValidateCurrentStateIdentity(state, validation); err != nil {
return nil, err
}
return state, nil
}
func validatePointerCommitIdentity(pointer *CurrentCommitPointer, commit *RemoteCommitManifest) error {
if pointer == nil || commit == nil {
return fmt.Errorf("current commit pointer and remote commit are required")
@@ -100,18 +70,6 @@ func validatePointerCommitIdentity(pointer *CurrentCommitPointer, commit *Remote
return nil
}
func validateCommitManifestIdentity(commit *RemoteCommitManifest, m *manifest.Manifest) error {
if commit == nil || m == nil {
return fmt.Errorf("remote commit and committed session manifest are required")
}
if commit.Campaign != strings.TrimSpace(m.Campaign) ||
commit.SessionID != strings.TrimSpace(m.SessionID) ||
commit.RunID != strings.TrimSpace(m.RunID) {
return fmt.Errorf("selected remote commit identity does not match committed session manifest")
}
return nil
}
func readVerifiedRemoteObject(
ctx context.Context,
store storage.ObjectStore,
@@ -148,11 +106,3 @@ func readCurrentStateControlObject(ctx context.Context, store storage.ObjectStor
}
return info, data, nil
}
func decodeCommittedManifest(ctx context.Context, data []byte) (*manifest.Manifest, error) {
m, err := (&manifest.LocalStore{}).LoadReader(ctx, bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("committed session manifest decode failed: %w; republish this session to replace unsupported remote state", err)
}
return m, nil
}

View File

@@ -13,10 +13,10 @@ import (
func TestLoadCurrentStateRequiresCommitPointer(t *testing.T) {
store := &storage.FakeBackend{}
prefix := S3SessionPrefix("dnd", "sample-campaign", "2026-05-03")
_, err := LoadCurrentState(context.Background(), store, prefix, CurrentStateValidation{})
_, err := LoadCurrentInventory(context.Background(), store, prefix, CurrentStateValidation{})
var missing *CurrentCommitPointerMissingError
if !errors.As(err, &missing) || !errors.Is(err, ErrCurrentCommitPointerMissing) {
t.Fatalf("LoadCurrentState() error = %v, want missing commit pointer", err)
t.Fatalf("LoadCurrentInventory() error = %v, want missing commit pointer", err)
}
if missing.Key != S3CurrentCommitPointerKey(prefix) {
t.Fatalf("missing pointer key = %q", missing.Key)

View File

@@ -163,13 +163,13 @@ func TestLoadCurrentStateReadsVerifiedRemoteCommit(t *testing.T) {
commit := testRemoteCommit(t, "20260519T010203Z-a1b2c3d4")
seedRemoteCommit(t, store, testCurrentSessionPrefix(), commit, "commit-generation", "manifest-generation")
state, err := LoadCurrentState(context.Background(), store, testCurrentSessionPrefix(), CurrentStateValidation{
state, err := LoadCurrentInventory(context.Background(), store, testCurrentSessionPrefix(), CurrentStateValidation{
ExpectedCampaign: commit.Campaign,
ExpectedSessionID: commit.SessionID,
ValidateRunID: true,
})
if err != nil {
t.Fatalf("LoadCurrentState() error = %v", err)
t.Fatalf("LoadCurrentInventory() error = %v", err)
}
if state.Commit == nil || state.Pointer == nil {
t.Fatalf("state does not expose selected immutable commit: %#v", state)
@@ -177,11 +177,11 @@ func TestLoadCurrentStateReadsVerifiedRemoteCommit(t *testing.T) {
if state.CurrentPointerKey != S3CurrentCommitPointerKey(testCurrentSessionPrefix()) {
t.Fatalf("CurrentPointerKey = %q", state.CurrentPointerKey)
}
if state.Manifest.RunID != commit.RunID {
t.Fatalf("manifest run ID = %q, want %q", state.Manifest.RunID, commit.RunID)
if state.RunID != commit.RunID {
t.Fatalf("inventory run ID = %q, want %q", state.RunID, commit.RunID)
}
wantReads := []string{state.CurrentPointerKey, state.Pointer.CommitKey, state.CurrentManifestKey}
wantReads := []string{state.CurrentPointerKey, state.Pointer.CommitKey}
if len(store.Reads) != len(wantReads) {
t.Fatalf("committed current-state reads = %#v, want %q", store.Reads, wantReads)
}
@@ -197,21 +197,8 @@ func TestLoadCurrentStateUsesMetadataFromOpenedObjectVersion(t *testing.T) {
commit := testRemoteCommit(t, "20260519T010203Z-a1b2c3d4")
seedRemoteCommit(t, store, testCurrentSessionPrefix(), commit, "commit-generation", "manifest-generation")
if _, err := LoadCurrentState(context.Background(), store, testCurrentSessionPrefix(), CurrentStateValidation{}); err != nil {
t.Fatalf("LoadCurrentState() error = %v, want verification from Read metadata", err)
}
}
func TestDecodeCommittedManifestGuidesRemoteRecovery(t *testing.T) {
_, err := decodeCommittedManifest(context.Background(), []byte(`{"session_id":"2026-05-03"}`))
if err == nil {
t.Fatal("decodeCommittedManifest() error = nil, want unsupported schema failure")
}
if !strings.Contains(err.Error(), "schema_version is required") || !strings.Contains(err.Error(), "republish this session") {
t.Fatalf("decodeCommittedManifest() error = %v, want schema and republish guidance", err)
}
if strings.Contains(err.Error(), "local state") {
t.Fatalf("decodeCommittedManifest() error = %v, must not describe remote state as local", err)
if _, err := LoadCurrentInventory(context.Background(), store, testCurrentSessionPrefix(), CurrentStateValidation{}); err != nil {
t.Fatalf("LoadCurrentInventory() error = %v, want verification from Read metadata", err)
}
}
@@ -241,44 +228,12 @@ func TestLoadCurrentStateRejectsPointerCommitIdentityMismatch(t *testing.T) {
}
store.SeedObject(storage.FakeObject{Key: S3CurrentCommitPointerKey(prefix), Data: pointerData})
_, err = LoadCurrentState(context.Background(), store, prefix, CurrentStateValidation{})
_, err = LoadCurrentInventory(context.Background(), store, prefix, CurrentStateValidation{})
if err == nil || !strings.Contains(err.Error(), "does not match selected remote commit") {
t.Fatalf("error = %v, want pointer/commit identity mismatch", err)
}
}
func TestLoadCurrentStateRejectsCommitManifestIdentityMismatch(t *testing.T) {
store := &storage.FakeBackend{}
commit := testRemoteCommit(t, "20260519T010203Z-a1b2c3d4")
pointer := seedRemoteCommit(t, store, testCurrentSessionPrefix(), commit, "commit-generation", "manifest-generation")
wrongManifest := testManifestJSON(t, "2026-05-03", "wrong-campaign", commit.RunID)
store.SeedObject(storage.FakeObject{
Key: commit.Artifacts[0].DestinationKey,
Data: wrongManifest,
ETag: "manifest-generation",
})
commit.Artifacts[0].SHA256 = remoteObjectSHA256(wrongManifest)
commit.Artifacts[0].Size = int64(len(wrongManifest))
commitData, err := EncodeRemoteCommitManifest(commit)
if err != nil {
t.Fatalf("EncodeRemoteCommitManifest() error = %v", err)
}
store.SeedObject(storage.FakeObject{Key: pointer.CommitKey, Data: commitData, ETag: "commit-generation"})
pointer.CommitSHA256 = remoteObjectSHA256(commitData)
pointer.CommitSize = int64(len(commitData))
pointerData, err := EncodeCurrentCommitPointer(*pointer)
if err != nil {
t.Fatalf("EncodeCurrentCommitPointer() error = %v", err)
}
store.SeedObject(storage.FakeObject{Key: S3CurrentCommitPointerKey(testCurrentSessionPrefix()), Data: pointerData})
_, err = LoadCurrentState(context.Background(), store, testCurrentSessionPrefix(), CurrentStateValidation{})
if err == nil || !strings.Contains(err.Error(), "does not match committed session manifest") {
t.Fatalf("error = %v, want commit/manifest identity mismatch", err)
}
}
func TestLoadCurrentStateRejectsCommitChecksumOrGenerationMismatch(t *testing.T) {
tests := []struct {
name string
@@ -313,7 +268,7 @@ func TestLoadCurrentStateRejectsCommitChecksumOrGenerationMismatch(t *testing.T)
}
store.SeedObject(storage.FakeObject{Key: S3CurrentCommitPointerKey(testCurrentSessionPrefix()), Data: pointerData})
_, err = LoadCurrentState(context.Background(), store, testCurrentSessionPrefix(), CurrentStateValidation{})
_, err = LoadCurrentInventory(context.Background(), store, testCurrentSessionPrefix(), CurrentStateValidation{})
if err == nil || !strings.Contains(err.Error(), test.want) {
t.Fatalf("error = %v, want %q", err, test.want)
}
@@ -435,7 +390,7 @@ func TestCurrentInventoryDoesNotReadExecutionSnapshot(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if current.Manifest != nil || current.Commit.RunID != runID {
if current.Commit.RunID != runID {
t.Fatalf("inventory %+v", current)
}
for _, read := range store.Reads {