Reject unsupported session state before execution and restore
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
@@ -59,6 +60,10 @@ func restoreSessionState(ctx context.Context, cfg *config.Config, opts RestorePl
|
||||
if err != nil {
|
||||
return diagnostic, fmt.Errorf("restore: %w", err)
|
||||
}
|
||||
manifestPath := artifacts.SessionManifestPathForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if _, _, err := loadManifestAtPathIfPresent(ctx, &manifest.LocalStore{}, manifestPath); err != nil {
|
||||
return diagnostic, fmt.Errorf("restore: %w", err)
|
||||
}
|
||||
if opts.DryRun {
|
||||
plan, err := deps.buildRestorePlan(ctx, cfg, current, objectStore, RestorePlanOptions{
|
||||
IncludeAudio: opts.IncludeAudio,
|
||||
@@ -97,6 +102,9 @@ func restoreSessionState(ctx context.Context, cfg *config.Config, opts RestorePl
|
||||
}
|
||||
}
|
||||
}()
|
||||
if _, _, err := loadManifestAtPathIfPresent(ctx, &manifest.LocalStore{}, manifestPath); err != nil {
|
||||
return diagnostic, fmt.Errorf("restore: %w", err)
|
||||
}
|
||||
|
||||
// Classification and installation share the same transition lock as a
|
||||
// runner. This prevents a runner from making reuse decisions against state
|
||||
|
||||
@@ -21,7 +21,7 @@ func TestDiscoverRemoteCurrentStateRequiresCommittedPointer(t *testing.T) {
|
||||
func restoreManifestJSON(t *testing.T, sessionID, campaign string) []byte {
|
||||
t.Helper()
|
||||
now := time.Date(2026, 5, 19, 23, 0, 0, 0, time.UTC).Format(time.RFC3339Nano)
|
||||
data, err := json.Marshal(map[string]any{"schema_version": "narratio.session-manifest.v1", "session_id": sessionID, "campaign": campaign, "run_id": "20260519T010203Z-a1b2c3d4", "created_at": now, "updated_at": now, "stages": map[string]any{}})
|
||||
data, err := json.Marshal(map[string]any{"schema_version": "narratio.session-manifest.v2", "session_id": sessionID, "campaign": campaign, "run_id": "20260519T010203Z-a1b2c3d4", "created_at": now, "updated_at": now, "stages": map[string]any{}})
|
||||
if err != nil {
|
||||
t.Fatalf("marshal manifest: %v", err)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,91 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
func TestExecuteRestoreRejectsUnsupportedRemoteState(t *testing.T) {
|
||||
for _, marker := range []string{"narratio.session-manifest.v1", "", "narratio.session-manifest.v99"} {
|
||||
t.Run(marker, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, root)
|
||||
fake := &storage.FakeBackend{}
|
||||
cfg, _, _, _ := seedRestoreCommittedState(t, fake, pipeline, campaign, session)
|
||||
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", nil)
|
||||
old := fake.Objects[current.CurrentManifestKey].Data
|
||||
data := bytes.Replace(old, []byte("narratio.session-manifest.v2"), []byte(marker), 1)
|
||||
if marker == "" {
|
||||
data = bytes.Replace(data, []byte(`"schema_version":"",`), nil, 1)
|
||||
}
|
||||
fake.SeedObject(storage.FakeObject{Key: current.CurrentManifestKey, Data: data})
|
||||
for i := range current.Commit.Artifacts {
|
||||
if current.Commit.Artifacts[i].Type == artifacts.RemoteArtifactTypeSessionManifest {
|
||||
current.Commit.Artifacts[i] = remoteRestoreArtifact(fake, artifacts.RemoteArtifactTypeSessionManifest, "session.manifest", current.CurrentManifestKey)
|
||||
}
|
||||
}
|
||||
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})
|
||||
pointerData, 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: pointerData})
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := restoreCommandExecutor(fake)([]string{"session", "restore", cfg.Session.SessionID, "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--force"}, &stdout, &stderr)
|
||||
if code == 0 || !strings.Contains(stderr.String(), "schema_version") {
|
||||
t.Fatalf("exit %d, stderr %q", code, stderr.String())
|
||||
}
|
||||
path := artifacts.SessionWorkDirForCampaign(root, cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Fatalf("session root exists after failed discovery: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteRestoreRejectsUnsupportedLocalStateEvenWithForce(t *testing.T) {
|
||||
for _, marker := range []string{"narratio.session-manifest.v1", "", "narratio.session-manifest.v99"} {
|
||||
t.Run(marker, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, root)
|
||||
fake := &storage.FakeBackend{}
|
||||
cfg, _, _, _ := seedRestoreCommittedState(t, fake, pipeline, campaign, session)
|
||||
seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", nil)
|
||||
path := artifacts.SessionManifestPathForCampaign(root, cfg.Session.Campaign, cfg.Session.SessionID)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data := bytes.Replace(restoreManifestJSON(t, cfg.Session.SessionID, cfg.Session.Campaign), []byte("narratio.session-manifest.v2"), []byte(marker), 1)
|
||||
if marker == "" {
|
||||
data = bytes.Replace(data, []byte(`,"schema_version":""`), nil, 1)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := restoreCommandExecutor(fake)([]string{"session", "restore", cfg.Session.SessionID, "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--force"}, &stdout, &stderr)
|
||||
if code == 0 {
|
||||
t.Fatal("expected restore failure")
|
||||
}
|
||||
after, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(after, data) {
|
||||
t.Fatal("manifest changed")
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Dir(path))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 || entries[0].Name() != "manifest.json" {
|
||||
t.Fatalf("session files changed: %v", entries)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteRestoreNonDryRunRestoresDurableFiles(t *testing.T) {
|
||||
workspaceRoot := t.TempDir()
|
||||
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
|
||||
|
||||
@@ -51,6 +51,11 @@ func executePlanWithDependencies(ctx context.Context, cfg *config.Config, plan B
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Reject unsupported existing state before creating or normalizing layout.
|
||||
// The locked initialization reads it again to protect against concurrent work.
|
||||
if _, _, err := loadManifestAtPathIfPresent(ctx, &manifest.LocalStore{}, runtime.manifestPath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := prepareInvocationLayout(runtime); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -887,6 +888,43 @@ func TestExecuteStagesFailureUpdatesManifest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteStagesRejectsUnsupportedSessionWithoutMutation(t *testing.T) {
|
||||
for _, marker := range []string{"narratio.session-manifest.v1", "", "narratio.session-manifest.v99"} {
|
||||
t.Run(marker, func(t *testing.T) {
|
||||
cfg := testConfig(t)
|
||||
path := manifestPathFor(cfg)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data := []byte(fmt.Sprintf(`{"schema_version":%q,"session_id":"%s","created_at":"2026-05-03T10:00:00Z","updated_at":"2026-05-03T10:00:00Z","stages":{}}`, marker, cfg.Session.SessionID))
|
||||
if marker == "" {
|
||||
data = bytes.Replace(data, []byte(`"schema_version":"",`), nil, 1)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := executeStages(context.Background(), cfg, []stage.Stage{BuildFullPlan()[0]}, RunOptions{Force: true})
|
||||
if err == nil {
|
||||
t.Fatal("expected unsupported state error")
|
||||
}
|
||||
after, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(after, data) {
|
||||
t.Fatal("manifest changed")
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Dir(path))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 || entries[0].Name() != "manifest.json" {
|
||||
t.Fatalf("session files changed: %v", entries)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteStagesLoadsExistingManifest(t *testing.T) {
|
||||
cfg := testConfig(t)
|
||||
manifestPath := manifestPathFor(cfg)
|
||||
|
||||
Reference in New Issue
Block a user