package artifacts import ( "context" "fmt" "strings" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" ) const ( // MaxCurrentCommitPointerBytes bounds the mutable commit-selection record. MaxCurrentCommitPointerBytes int64 = 64 << 10 // MaxRemoteCommitManifestBytes bounds an immutable commit manifest. MaxRemoteCommitManifestBytes int64 = 4 << 20 ) func loadCommittedInventory( ctx context.Context, store storage.ObjectStore, sessionPrefix string, pointerKey string, validation CurrentStateValidation, ) (*CurrentState, error) { _, pointerData, err := readCurrentStateControlObject(ctx, store, pointerKey, "current commit pointer", MaxCurrentCommitPointerBytes) if err != nil { return nil, err } pointer, err := DecodeCurrentCommitPointer(pointerData) if err != nil { return nil, err } if err := pointer.ValidateForSessionPrefix(sessionPrefix); err != nil { return nil, err } commitData, err := readVerifiedRemoteObject(ctx, store, pointer.CommitKey, pointer.CommitSHA256, pointer.CommitSize, pointer.CommitGeneration, "remote commit manifest", MaxRemoteCommitManifestBytes) if err != nil { return nil, fmt.Errorf("read selected remote commit %q: %w", pointer.CommitKey, err) } commit, err := DecodeRemoteCommitManifest(commitData) if err != nil { return nil, err } if err := commit.ValidateForSessionPrefix(sessionPrefix); err != nil { return nil, err } 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 } func validatePointerCommitIdentity(pointer *CurrentCommitPointer, commit *RemoteCommitManifest) error { if pointer == nil || commit == nil { return fmt.Errorf("current commit pointer and remote commit are required") } if pointer.Campaign != commit.Campaign || pointer.SessionID != commit.SessionID || pointer.RunID != commit.RunID { return fmt.Errorf("current commit pointer identity does not match selected remote commit") } return nil } func readVerifiedRemoteObject( ctx context.Context, store storage.ObjectStore, key string, wantSHA256 string, wantSize int64, wantGeneration string, category string, maxBytes int64, ) ([]byte, error) { info, data, err := readCurrentStateControlObject(ctx, store, key, category, maxBytes) if err != nil { return nil, err } if int64(len(data)) != wantSize { return nil, fmt.Errorf("size mismatch: got %d, want %d", len(data), wantSize) } if checksum := remoteObjectSHA256(data); checksum != wantSHA256 { return nil, fmt.Errorf("checksum mismatch: got %s, want %s", checksum, wantSHA256) } if info.Size > 0 && info.Size != wantSize { return nil, fmt.Errorf("storage size mismatch: got %d, want %d", info.Size, wantSize) } if strings.TrimSpace(info.ETag) != wantGeneration { return nil, fmt.Errorf("generation mismatch: got %q, want %q", info.ETag, wantGeneration) } return data, nil } func readCurrentStateControlObject(ctx context.Context, store storage.ObjectStore, key, category string, maxBytes int64) (storage.ObjectInfo, []byte, error) { info, data, err := storage.ReadObjectBounded(ctx, store, key, maxBytes) if err != nil { return storage.ObjectInfo{}, nil, fmt.Errorf("read %s control object %q with %d-byte limit: %w", category, key, maxBytes, err) } return info, data, nil }