package artifacts import ( "fmt" "path/filepath" "strings" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/pathsafe" ) const sha256HexLength = 64 // SessionPaths contains canonical local paths for one session work directory. type SessionPaths struct { WorkspaceRoot string CampaignID string SessionID string Root string InputsDir string AudioDir string TranscriptsDir string TranscriptsRawDir string ArtifactsDir string ReportsDir string ConfigDir string LogsDir string DebugDir string ReferencesDir string RunsDir string PreviousDir string PreviousManifestPath string PreviousArtifactsDir string ManifestPath string LockPath string RestoreMarkerPath string } // SessionInvocationProbeDir keeps automatic readiness outside actual attempts. func SessionInvocationProbeDir(paths SessionPaths, runID, application string) string { return filepath.Join(paths.RunsDir, ".preflight", runID, application) } // SessionWorkDirForCampaign returns the canonical campaign-aware work directory for one session. func SessionWorkDirForCampaign(rootDir, campaign, sessionID string) string { return filepath.Join(rootDir, config.PathWorkDirSegment, campaign, sessionID) } // SessionManifestPathForCampaign returns the canonical session manifest path. func SessionManifestPathForCampaign(rootDir, campaign, sessionID string) string { return filepath.Join(SessionWorkDirForCampaign(rootDir, campaign, sessionID), config.PathManifestFile) } // SessionRestoreMarkerPathForCampaign returns the durable recovery marker for // a restore that has started changing one session's local state. func SessionRestoreMarkerPathForCampaign(rootDir, campaign, sessionID string) string { return filepath.Join(SessionWorkDirForCampaign(rootDir, campaign, sessionID), ".restore-incomplete.json") } // SessionRunsDirForCampaign returns the canonical runs directory for one session. func SessionRunsDirForCampaign(rootDir, campaign, sessionID string) string { return filepath.Join(SessionWorkDirForCampaign(rootDir, campaign, sessionID), config.PathRunsDirSegment) } // SessionPreviousDirForCampaign returns the canonical previous-session state directory. func SessionPreviousDirForCampaign(rootDir, campaign, sessionID string) string { return filepath.Join(SessionWorkDirForCampaign(rootDir, campaign, sessionID), config.PathInputsDirSegment, config.PathPreviousDirSegment) } // SessionPreviousManifestPathForCampaign returns the canonical previous-session manifest cache path. func SessionPreviousManifestPathForCampaign(rootDir, campaign, sessionID string) string { return filepath.Join(SessionPreviousDirForCampaign(rootDir, campaign, sessionID), config.PathManifestFile) } // SessionPreviousArtifactsDirForCampaign returns the canonical previous-session artifact cache directory. func SessionPreviousArtifactsDirForCampaign(rootDir, campaign, sessionID string) string { return filepath.Join(SessionPreviousDirForCampaign(rootDir, campaign, sessionID), config.PathArtifactsDirSegment) } // SessionPreviousArtifactPathForCampaign returns a confined path under // previous/artifacts for one artifact. func SessionPreviousArtifactPathForCampaign(rootDir, campaign, sessionID, artifactRelativePath string) (string, error) { if err := ValidateSessionIdentity(campaign, sessionID); err != nil { return "", err } return pathsafe.JoinSlashRelativeUnderRoot( SessionPreviousArtifactsDirForCampaign(rootDir, campaign, sessionID), previousArtifactCacheRelativePath(artifactRelativePath), ) } // SessionRunRootForCampaign returns the canonical run root under runs/{run_id}. func SessionRunRootForCampaign(rootDir, campaign, sessionID, runID string) string { return filepath.Join(SessionRunsDirForCampaign(rootDir, campaign, sessionID), runID) } // SessionRunStageDirForCampaign returns the canonical stage directory under runs/{run_id}/{stage}. func SessionRunStageDirForCampaign(rootDir, campaign, sessionID, runID, stageName string) string { return filepath.Join(SessionRunRootForCampaign(rootDir, campaign, sessionID, runID), stageName) } // SessionRunLogsDir and SessionRunDebugDir locate diagnostics for an attempt. func SessionRunLogsDir(paths SessionPaths, runID, stageName string) string { return filepath.Join(paths.RunsDir, runID, stageName, "logs") } func SessionRunDebugDir(paths SessionPaths, runID, stageName string) string { return filepath.Join(paths.RunsDir, runID, stageName, "debug") } // SessionLatestLogsPath and SessionLatestDebugPath locate managed navigation views. func SessionLatestLogsPath(paths SessionPaths, stageName string) string { return filepath.Join(paths.LogsDir, stageName) } func SessionLatestDebugPath(paths SessionPaths, stageName string) string { return filepath.Join(paths.DebugDir, stageName) } // SessionRunExtractDirForCampaign returns the invocation-local extraction directory. func SessionRunExtractDirForCampaign(rootDir, campaign, sessionID, runID string) string { return SessionRunStageDirForCampaign(rootDir, campaign, sessionID, runID, "extract") } // SessionRunNotariusReportPathForCampaign returns the invocation's captured completion report. func SessionRunNotariusReportPathForCampaign(rootDir, campaign, sessionID, runID string) string { return filepath.Join(SessionRunExtractDirForCampaign(rootDir, campaign, sessionID, runID), "logs", "notarius.report.json") } // SessionRunNotariusLogPathForCampaign returns the invocation-local stderr log path. func SessionRunNotariusLogPathForCampaign(rootDir, campaign, sessionID, runID string) string { return filepath.Join(SessionRunExtractDirForCampaign(rootDir, campaign, sessionID, runID), "logs", "notarius.stderr.log") } // SessionRunNotariusOutputRootForCampaign returns the invocation-local Notarius output root. func SessionRunNotariusOutputRootForCampaign(rootDir, campaign, sessionID, runID string) string { return filepath.Join(SessionRunExtractDirForCampaign(rootDir, campaign, sessionID, runID), "notarius-output") } // SessionNotariusReferenceCacheDir returns the stable cache root for verified // reference snapshots supplied to Notarius. func SessionNotariusReferenceCacheDir(paths SessionPaths) string { return paths.ReferencesDir } // SessionNotariusReferenceSnapshotPath returns the content-addressed path for // one verified prepared reference snapshot. func SessionNotariusReferenceSnapshotPath(paths SessionPaths, checksum, preparedBasename string) (string, error) { if len(checksum) != sha256HexLength { return "", fmt.Errorf("reference checksum must be a full lowercase SHA-256 digest") } for _, character := range checksum { if (character < '0' || character > '9') && (character < 'a' || character > 'f') { return "", fmt.Errorf("reference checksum must be a full lowercase SHA-256 digest") } } if err := pathsafe.ValidateOpaqueSegment(preparedBasename); err != nil { return "", fmt.Errorf("prepared reference basename: %w", err) } return pathsafe.JoinSlashRelativeUnderRoot( SessionNotariusReferenceCacheDir(paths), checksum+"/"+preparedBasename, ) } // SessionPreviousDir returns the previous-session state directory for already-resolved session paths. func SessionPreviousDir(paths SessionPaths) string { return paths.PreviousDir } // SessionPreviousManifestPath returns the previous-session manifest path for already-resolved session paths. func SessionPreviousManifestPath(paths SessionPaths) string { return paths.PreviousManifestPath } // SessionPreviousArtifactsDir returns the previous-session artifact directory for already-resolved session paths. func SessionPreviousArtifactsDir(paths SessionPaths) string { return paths.PreviousArtifactsDir } // SessionPreviousArtifactPath returns a confined path under previous/artifacts // for already-resolved session paths. func SessionPreviousArtifactPath(paths SessionPaths, artifactRelativePath string) (string, error) { if err := ValidateSessionIdentity(paths.CampaignID, paths.SessionID); err != nil { return "", err } return pathsafe.JoinSlashRelativeUnderRoot(paths.PreviousArtifactsDir, previousArtifactCacheRelativePath(artifactRelativePath)) } // ValidateSessionIdentity validates the opaque components used in workspace // and object-store session paths. func ValidateSessionIdentity(campaign, sessionID string) error { if err := pathsafe.ValidateOpaqueSegment(campaign); err != nil { return fmt.Errorf("campaign identity: %w", err) } if err := pathsafe.ValidateOpaqueSegment(sessionID); err != nil { return fmt.Errorf("session identity: %w", err) } return nil } // ValidateRunIdentity validates the opaque component used below a session's // run directory and object-store prefix. func ValidateRunIdentity(runID string) error { if err := pathsafe.ValidateOpaqueSegment(runID); err != nil { return fmt.Errorf("run identity: %w", err) } return nil } func previousArtifactCacheRelativePath(artifactRelativePath string) string { rel := filepath.ToSlash(filepath.Clean(filepath.FromSlash(strings.TrimSpace(artifactRelativePath)))) if rel == "." { return "" } const artifactsPrefix = "artifacts/" if strings.HasPrefix(rel, artifactsPrefix) && len(rel) > len(artifactsPrefix) { return strings.TrimPrefix(rel, artifactsPrefix) } return rel } func buildSessionPaths(workspaceRoot, campaign, sessionID string) SessionPaths { root := SessionWorkDirForCampaign(workspaceRoot, campaign, sessionID) return buildSessionPathsFromRoot(workspaceRoot, campaign, sessionID, root) } func buildSessionPathsFromRoot(workspaceRoot, campaign, sessionID, root string) SessionPaths { return SessionPaths{ WorkspaceRoot: workspaceRoot, CampaignID: campaign, SessionID: sessionID, Root: root, InputsDir: filepath.Join(root, config.PathInputsDirSegment), AudioDir: filepath.Join(root, config.PathAudioDirSegment), TranscriptsDir: filepath.Join(root, config.PathTranscriptsSegment), TranscriptsRawDir: filepath.Join(root, filepath.FromSlash(config.PathTranscriptsRaw)), ArtifactsDir: filepath.Join(root, config.PathArtifactsDirSegment), ReportsDir: filepath.Join(root, config.PathReportsDirSegment), ConfigDir: filepath.Join(root, config.PathConfigDirSegment), LogsDir: filepath.Join(root, config.PathLogsDirSegment), DebugDir: filepath.Join(root, "debug"), ReferencesDir: filepath.Join(root, config.PathInputsDirSegment, "references"), RunsDir: filepath.Join(root, config.PathRunsDirSegment), PreviousDir: filepath.Join(root, config.PathInputsDirSegment, config.PathPreviousDirSegment), PreviousManifestPath: filepath.Join(root, config.PathInputsDirSegment, config.PathPreviousDirSegment, config.PathManifestFile), PreviousArtifactsDir: filepath.Join(root, config.PathInputsDirSegment, config.PathPreviousDirSegment, config.PathArtifactsDirSegment), ManifestPath: filepath.Join(root, config.PathManifestFile), LockPath: filepath.Join(root, config.PathLockFile), RestoreMarkerPath: filepath.Join(root, ".restore-incomplete.json"), } }