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

@@ -12,7 +12,6 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
)
func TestRestoreHoldsTransitionLockBeforePlanning(t *testing.T) {
@@ -21,20 +20,20 @@ func TestRestoreHoldsTransitionLockBeforePlanning(t *testing.T) {
fake := &storage.FakeBackend{}
cfg, _, _, _ := seedRestoreCommittedState(t, fake, pipelinePath, campaignPath, sessionPath)
current, err := discoverRemoteCurrentState(context.Background(), cfg, fake)
current, err := discoverRemotePublication(context.Background(), cfg, fake)
if err != nil {
t.Fatalf("discoverRemoteCurrentState() error = %v", err)
t.Fatalf("discoverRemotePublication() error = %v", err)
}
planning := make(chan struct{})
releasePlanning := make(chan struct{})
deps := restoreCommandDependencies(fake)
deps.discoverRemoteCurrentState = func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error) {
deps.discoverRemotePublication = func(context.Context, *config.Config, storage.ObjectStore) (*RemoteCurrentState, error) {
return current, nil
}
deps.buildRestorePlan = func(context.Context, *config.Config, *RemoteCurrentState, storage.ObjectStore, RestorePlanOptions) (*RestorePlan, error) {
deps.buildInventoryRestorePlan = func(ctx context.Context, cfg *config.Config, current *RemoteCurrentState, store storage.ObjectStore, opts RestorePlanOptions) (*RestorePlan, error) {
close(planning)
<-releasePlanning
return &RestorePlan{}, nil
return buildInventoryRestorePlan(ctx, cfg, current, store, opts)
}
restoreDone := make(chan error, 1)
@@ -73,17 +72,18 @@ func TestExecuteStagesRefusesIncompleteRestore(t *testing.T) {
func TestForcedRestoreRetryClearsIncompleteMarker(t *testing.T) {
workspaceRoot := t.TempDir()
pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot)
pipelinePath, campaignPath, sessionPath := writeValidConfigFilesWithScriptoriumArtifacts(t, workspaceRoot)
fake := &storage.FakeBackend{}
cfg, sessionPrefix, _, _ := seedRestoreCommittedState(t, fake, pipelinePath, campaignPath, sessionPath)
seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{
current := seedCommittedRestoreSnapshot(t, cfg, fake, "20260519T010203Z-a1b2c3d4", map[string][]byte{
"artifacts/session_recap.md": []byte("remote recap\n"),
"transcripts/full.json": []byte("remote transcript\n"),
"transcripts/full.json": []byte(`{"segments":[]}`),
})
setRestoreFixtureSources(t, fake, current, map[string]string{"artifacts/session_recap.md": artifacts.ConfiguredArtifactSourceID("session_recap"), "transcripts/full.json": artifacts.ArtifactTranscriptFinalTrimmed})
sessionRoot := artifacts.SessionWorkDirForCampaign(workspaceRoot, cfg.Session.Campaign, cfg.Session.SessionID)
mustWriteTestFile(t, filepath.Join(sessionRoot, "artifacts", "session_recap.md"), "local recap\n")
mustWriteTestFile(t, filepath.Join(sessionRoot, "transcripts", "full.json"), "local transcript\n")
mustWriteTestFile(t, filepath.Join(sessionRoot, "transcripts", "final.trimmed.json"), "local transcript\n")
committedTranscriptKey := artifacts.S3RunRelativeDestinationKey(artifacts.S3RunPrefix(sessionPrefix, "20260519T010203Z-a1b2c3d4"), "published/transcripts/full.json")
fake.ReadHook = func(call storage.FakeReadCall) error {
if call.Key == committedTranscriptKey {
@@ -114,68 +114,5 @@ func TestForcedRestoreRetryClearsIncompleteMarker(t *testing.T) {
if _, err := os.Stat(markerPath); !os.IsNotExist(err) {
t.Fatalf("incomplete restore marker should be cleared after retry; stat err=%v", err)
}
mustReadEquals(t, filepath.Join(sessionRoot, "transcripts", "full.json"), "remote transcript\n")
}
func TestRebaseRestoredManifestPathsRejectsUnsafeReferencesAndRebasesPortablePaths(t *testing.T) {
for _, test := range []struct {
name string
workDir string
output string
wantPath string
wantError string
}{
{
name: "unix absolute path within producer root",
workDir: "/producer/work/sample-campaign/2026-05-03/runs/20260519T010203Z-a1b2c3d4",
output: "/producer/work/sample-campaign/2026-05-03/artifacts/recap.md",
wantPath: "artifacts/recap.md",
},
{
name: "windows absolute path within producer root",
workDir: `C:\producer\work\sample-campaign\2026-05-03\runs\20260519T010203Z-a1b2c3d4`,
output: `C:\producer\work\sample-campaign\2026-05-03\artifacts\recap.md`,
wantPath: "artifacts/recap.md",
},
{
name: "absolute path outside producer root",
workDir: "/producer/work/sample-campaign/2026-05-03/runs/20260519T010203Z-a1b2c3d4",
output: "/another-machine/recap.md",
wantError: "outside the producer session root",
},
{
name: "relative traversal",
workDir: "/producer/work/sample-campaign/2026-05-03/runs/20260519T010203Z-a1b2c3d4",
output: "../recap.md",
wantError: "unsafe relative path",
},
} {
t.Run(test.name, func(t *testing.T) {
cfg := testConfig(t)
m := manifest.New(cfg.Session.SessionID, nowUTC())
m.Campaign = cfg.Session.Campaign
m.RunID = "20260519T010203Z-a1b2c3d4"
m.LocalWorkDir = test.workDir
m.Stages["analyze"] = &manifest.StageRecord{Outputs: []manifest.ArtifactRecord{{Kind: "recap", LocalPath: test.output}}}
m.MarkStageFailed("cleanup", nowUTC(), "retry local deletion")
destination := artifacts.SessionWorkDirForCampaign(cfg.Pipeline.Workspace.Root, cfg.Session.Campaign, cfg.Session.SessionID)
err := rebaseRestoredManifestPaths(cfg, m, destination)
if test.wantError != "" {
if err == nil || !strings.Contains(err.Error(), test.wantError) {
t.Fatalf("rebaseRestoredManifestPaths() error = %v, want %q", err, test.wantError)
}
return
}
if err != nil {
t.Fatalf("rebaseRestoredManifestPaths() error = %v", err)
}
if got, want := m.Stages["analyze"].Outputs[0].LocalPath, filepath.Join(destination, test.wantPath); got != want {
t.Fatalf("rebased local path = %q, want %q", got, want)
}
if got := m.Stages["cleanup"].Status; got != manifest.StatusFailed {
t.Fatalf("cleanup status = %q, want preserved failed state", got)
}
})
}
mustReadEquals(t, filepath.Join(sessionRoot, "transcripts", "final.trimmed.json"), `{"segments":[]}`)
}