2026-05-23 13:28:25 +00:00
package artifactpolicy
import (
2026-05-23 15:50:29 +00:00
"errors"
2026-05-23 13:28:25 +00:00
"fmt"
"regexp"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
)
const (
SourceBoundsSession = "narratio.bounds.session"
2026-11-27 16:24:08 +00:00
SourceInputPlayers = "narratio.input.players"
SourceInputParty = "narratio.input.party"
SourceInputGlossary = "narratio.input.glossary"
SourceInputGlossaryParty = "narratio.input.glossary.party"
SourceInputGlossaryCampaign = "narratio.input.glossary.campaign"
SourceInputGlossarySetting = "narratio.input.glossary.setting"
SourceInputGlossarySpells = "narratio.input.glossary.spells"
SourceInputSpellCatalog = "narratio.input.spell_catalog"
2026-05-27 09:34:05 -05:00
2026-05-23 13:28:25 +00:00
configuredSourcePrefix = "narratio.artifact."
2026-08-09 23:30:32 +00:00
extractionSourcePrefix = "narratio.extraction."
2026-05-23 13:28:25 +00:00
previousConfiguredSrcPrefix = "narratio.previous_session.artifact."
)
var configuredSourceRE = regexp . MustCompile ( ` ^narratio\.artifact\.([a-z][a-z0-9_]*)$ ` )
2026-08-09 23:30:32 +00:00
var extractionSourceRE = regexp . MustCompile ( ` ^narratio\.extraction\.([a-z][a-z0-9_]*)$ ` )
2026-05-23 13:28:25 +00:00
var previousSourceRE = regexp . MustCompile ( ` ^narratio\.previous_session\.artifact\.([a-z][a-z0-9_]*)$ ` )
2026-08-09 23:30:32 +00:00
var configuredKeyRE = regexp . MustCompile ( ` ^[a-z][a-z0-9_]*$ ` )
2026-05-23 13:28:25 +00:00
2026-05-23 15:50:29 +00:00
var (
ErrUnsupportedScriptoriumInputSource = errors . New ( "unsupported scriptorium input source" )
ErrInvalidPreviousSessionSource = errors . New ( "invalid previous-session source format" )
)
2026-05-23 13:28:25 +00:00
type SourceKind string
const (
SourceKindBuiltIn SourceKind = "built_in"
SourceKindConfiguredArtifact SourceKind = "configured_artifact"
2026-08-09 23:30:32 +00:00
SourceKindExtraction SourceKind = "extraction"
2026-05-23 13:28:25 +00:00
SourceKindPreviousArtifact SourceKind = "previous_session_configured_artifact"
2026-05-27 09:34:05 -05:00
SourceKindStableInput SourceKind = "stable_input"
2026-05-23 13:28:25 +00:00
)
// Source describes one normalized artifact source identifier.
type Source struct {
ID string
Kind SourceKind
ConfiguredKey string
}
2026-05-23 15:50:29 +00:00
// ScriptoriumInputSourceDescriptor describes one validated Scriptorium input source.
type ScriptoriumInputSourceDescriptor struct {
Source Source
PreviousSession * PreviousSessionSourceDescriptor
}
2026-08-29 14:55:24 +00:00
// PreparedInputSourceDescriptor describes a prepared stable input's source,
// manifest kind, and canonical staged filename.
type PreparedInputSourceDescriptor struct {
SourceID string
ManifestKind string
Filename string
}
2026-05-23 15:50:29 +00:00
// PreviousSessionSourceDescriptor describes one canonical previous-session input source.
type PreviousSessionSourceDescriptor struct {
SourceID string
ConfiguredKey string
ConfiguredSourceID string
}
// UnknownConfiguredArtifactError reports a source that references an undefined configured artifact key.
type UnknownConfiguredArtifactError struct {
ConfiguredKey string
}
func ( e * UnknownConfiguredArtifactError ) Error ( ) string {
return fmt . Sprintf ( "references unknown artifact %q" , e . ConfiguredKey )
}
2026-08-09 23:30:32 +00:00
// UnknownExtractionArtifactError reports a source that references an undefined extraction key.
type UnknownExtractionArtifactError struct {
ConfiguredKey string
}
func ( e * UnknownExtractionArtifactError ) Error ( ) string {
return fmt . Sprintf ( "references unknown extraction output %q" , e . ConfiguredKey )
}
// IsConfiguredKey reports whether a key follows the configured-artifact key grammar.
func IsConfiguredKey ( key string ) bool {
return configuredKeyRE . MatchString ( strings . TrimSpace ( key ) )
}
2026-05-23 13:28:25 +00:00
// ConfiguredSourceID converts a configured artifact key into source id form.
func ConfiguredSourceID ( key string ) string {
return configuredSourcePrefix + strings . TrimSpace ( key )
}
2026-08-09 23:30:32 +00:00
// ExtractionSourceID converts an extraction output key into source id form.
func ExtractionSourceID ( key string ) string {
return extractionSourcePrefix + strings . TrimSpace ( key )
}
2026-05-23 13:28:25 +00:00
// PreviousSessionSourceID converts a configured artifact key into previous-session source id form.
func PreviousSessionSourceID ( key string ) string {
return previousConfiguredSrcPrefix + strings . TrimSpace ( key )
}
// ParseConfiguredSource extracts configured key from narratio.artifact.<key>.
func ParseConfiguredSource ( source string ) ( string , bool ) {
matches := configuredSourceRE . FindStringSubmatch ( strings . TrimSpace ( source ) )
if len ( matches ) != 2 {
return "" , false
}
return matches [ 1 ] , true
}
2026-08-09 23:30:32 +00:00
// ParseExtractionSource extracts configured key from narratio.extraction.<key>.
func ParseExtractionSource ( source string ) ( string , bool ) {
matches := extractionSourceRE . FindStringSubmatch ( strings . TrimSpace ( source ) )
if len ( matches ) != 2 {
return "" , false
}
return matches [ 1 ] , true
}
2026-05-23 13:28:25 +00:00
// ParsePreviousSessionSource extracts configured key from narratio.previous_session.artifact.<key>.
func ParsePreviousSessionSource ( source string ) ( string , bool ) {
matches := previousSourceRE . FindStringSubmatch ( strings . TrimSpace ( source ) )
if len ( matches ) != 2 {
return "" , false
}
return matches [ 1 ] , true
}
2026-08-10 22:43:22 +00:00
// ClassifySource classifies a source id as built-in, configured, extraction, or previous-session configured.
2026-05-23 13:28:25 +00:00
func ClassifySource ( source string ) ( Source , error ) {
trimmed := strings . TrimSpace ( source )
if trimmed == "" {
return Source { } , fmt . Errorf ( "artifact source is required" )
}
if _ , ok := artifactmodel . LookupRuntimeTranscriptArtifact ( trimmed ) ; ok {
return Source { ID : trimmed , Kind : SourceKindBuiltIn } , nil
}
if trimmed == SourceBoundsSession {
return Source { ID : trimmed , Kind : SourceKindBuiltIn } , nil
}
if key , ok := ParseConfiguredSource ( trimmed ) ; ok {
return Source { ID : trimmed , Kind : SourceKindConfiguredArtifact , ConfiguredKey : key } , nil
}
2026-08-09 23:30:32 +00:00
if key , ok := ParseExtractionSource ( trimmed ) ; ok {
return Source { ID : trimmed , Kind : SourceKindExtraction , ConfiguredKey : key } , nil
}
2026-05-23 13:28:25 +00:00
if key , ok := ParsePreviousSessionSource ( trimmed ) ; ok {
return Source { ID : trimmed , Kind : SourceKindPreviousArtifact , ConfiguredKey : key } , nil
}
return Source { } , fmt . Errorf ( "unsupported artifact source %q" , source )
}
2026-05-23 15:50:29 +00:00
// DescribeScriptoriumInputSource classifies one input source and returns descriptor
// metadata used by config validation, analyze input resolution, and previous-cache planning.
func DescribeScriptoriumInputSource ( source string ) ( ScriptoriumInputSourceDescriptor , error ) {
trimmed := strings . TrimSpace ( source )
if trimmed == "" {
return ScriptoriumInputSourceDescriptor { } , ErrUnsupportedScriptoriumInputSource
}
2026-11-27 16:24:08 +00:00
if trimmed == SourceInputGlossary {
return ScriptoriumInputSourceDescriptor { } , fmt . Errorf ( "%w: replace %s with an explicit narratio.input.glossary.<party|campaign|setting|spells> source" , ErrUnsupportedScriptoriumInputSource , trimmed )
}
2026-08-29 14:55:24 +00:00
if prepared , ok := DescribePreparedInputSource ( trimmed ) ; ok {
2026-05-27 09:34:05 -05:00
return ScriptoriumInputSourceDescriptor {
2026-08-29 16:19:22 +00:00
Source : Source { ID : prepared . SourceID , Kind : SourceKindStableInput } ,
2026-05-27 09:34:05 -05:00
} , nil
}
2026-05-23 15:50:29 +00:00
if strings . HasPrefix ( trimmed , "narratio.previous_session.artifact" ) {
descriptor , err := DescribePreviousSessionSource ( trimmed )
if err != nil {
return ScriptoriumInputSourceDescriptor { } , err
}
return ScriptoriumInputSourceDescriptor {
Source : Source {
ID : descriptor . SourceID ,
Kind : SourceKindPreviousArtifact ,
ConfiguredKey : descriptor . ConfiguredKey ,
} ,
PreviousSession : & descriptor ,
} , nil
}
classified , err := ClassifySource ( trimmed )
if err != nil {
return ScriptoriumInputSourceDescriptor { } , ErrUnsupportedScriptoriumInputSource
}
return ScriptoriumInputSourceDescriptor { Source : classified } , nil
}
2026-08-29 14:55:24 +00:00
var preparedInputSources = map [ string ] PreparedInputSourceDescriptor {
SourceInputPlayers : {
SourceID : SourceInputPlayers ,
ManifestKind : "players" ,
Filename : "players.yml" ,
} ,
SourceInputParty : {
SourceID : SourceInputParty ,
ManifestKind : "party" ,
Filename : "party.yml" ,
} ,
2026-11-27 16:24:08 +00:00
SourceInputGlossaryParty : { SourceID : SourceInputGlossaryParty , ManifestKind : "glossary_party" , Filename : "glossaries/party.yml" } ,
SourceInputGlossaryCampaign : { SourceID : SourceInputGlossaryCampaign , ManifestKind : "glossary_campaign" , Filename : "glossaries/campaign.yml" } ,
SourceInputGlossarySetting : { SourceID : SourceInputGlossarySetting , ManifestKind : "glossary_setting" , Filename : "glossaries/setting.yml" } ,
SourceInputGlossarySpells : { SourceID : SourceInputGlossarySpells , ManifestKind : "glossary_spells" , Filename : "glossaries/spells.yml" } ,
2026-08-29 14:55:24 +00:00
SourceInputSpellCatalog : {
SourceID : SourceInputSpellCatalog ,
ManifestKind : "spell_catalog" ,
Filename : "spell_catalog.json" ,
} ,
}
2026-11-27 16:31:20 +00:00
// GlossaryInputSources returns the four shared glossary descriptors in consumer order.
func GlossaryInputSources ( ) [ ] PreparedInputSourceDescriptor {
return [ ] PreparedInputSourceDescriptor {
preparedInputSources [ SourceInputGlossaryParty ] , preparedInputSources [ SourceInputGlossaryCampaign ] ,
preparedInputSources [ SourceInputGlossarySetting ] , preparedInputSources [ SourceInputGlossarySpells ] ,
}
}
2026-08-29 14:55:24 +00:00
// DescribePreparedInputSource returns the canonical descriptor for a prepared
// stable input source.
func DescribePreparedInputSource ( source string ) ( PreparedInputSourceDescriptor , bool ) {
descriptor , ok := preparedInputSources [ strings . TrimSpace ( source ) ]
return descriptor , ok
2026-05-27 09:34:05 -05:00
}
2026-05-23 15:50:29 +00:00
// DescribePreviousSessionSource validates a canonical previous-session source id
// and returns both previous and configured-source vocabulary descriptors.
func DescribePreviousSessionSource ( source string ) ( PreviousSessionSourceDescriptor , error ) {
configuredKey , ok := ParsePreviousSessionSource ( source )
if ! ok {
return PreviousSessionSourceDescriptor { } , ErrInvalidPreviousSessionSource
}
return PreviousSessionSourceDescriptor {
SourceID : PreviousSessionSourceID ( configuredKey ) ,
ConfiguredKey : configuredKey ,
ConfiguredSourceID : ConfiguredSourceID ( configuredKey ) ,
} , nil
}
// PreviousSessionSourceDescriptorForConfiguredKey derives a previous-session source descriptor
// from a configured artifact key.
func PreviousSessionSourceDescriptorForConfiguredKey ( configuredKey string ) ( PreviousSessionSourceDescriptor , error ) {
return DescribePreviousSessionSource ( PreviousSessionSourceID ( configuredKey ) )
}
// ValidateInputConfiguredReference checks that configured/previous-session sources
// reference configured artifacts known to the current Scriptorium config.
func ValidateInputConfiguredReference (
descriptor ScriptoriumInputSourceDescriptor ,
configured map [ string ] struct { } ,
2026-08-09 23:30:32 +00:00
) error {
return ValidateInputReference ( descriptor , configured , nil )
}
// ValidateInputReference checks that configured and extraction sources are declared
// by the effective pipeline configuration.
func ValidateInputReference (
descriptor ScriptoriumInputSourceDescriptor ,
configured map [ string ] struct { } ,
extractions map [ string ] struct { } ,
2026-05-23 15:50:29 +00:00
) error {
switch descriptor . Source . Kind {
case SourceKindConfiguredArtifact , SourceKindPreviousArtifact :
if _ , ok := configured [ descriptor . Source . ConfiguredKey ] ; ! ok {
return & UnknownConfiguredArtifactError { ConfiguredKey : descriptor . Source . ConfiguredKey }
}
2026-08-09 23:30:32 +00:00
case SourceKindExtraction :
if _ , ok := extractions [ descriptor . Source . ConfiguredKey ] ; ! ok {
return & UnknownExtractionArtifactError { ConfiguredKey : descriptor . Source . ConfiguredKey }
}
2026-05-23 15:50:29 +00:00
}
return nil
}
2026-05-23 13:28:25 +00:00
// ValidatePublishSource validates that a source is publish-compatible and references a known configured artifact.
func ValidatePublishSource ( source string , configured map [ string ] string ) ( Source , error ) {
2026-08-09 23:30:32 +00:00
return ValidatePublishSourceWithExtractions ( source , configured , nil )
}
// ValidatePublishSourceWithExtractions validates publish sources against the
// configured Scriptorium artifacts and extraction outputs.
func ValidatePublishSourceWithExtractions (
source string ,
configured map [ string ] string ,
extractions map [ string ] struct { } ,
) ( Source , error ) {
2026-05-23 13:28:25 +00:00
classified , err := ClassifySource ( source )
if err != nil {
2026-08-09 23:30:32 +00:00
return Source { } , fmt . Errorf ( "must be a built-in source id, narratio.artifact.<name>, or configured narratio.extraction.<name>" )
2026-05-23 13:28:25 +00:00
}
if classified . Kind == SourceKindPreviousArtifact {
2026-08-09 23:30:32 +00:00
return Source { } , fmt . Errorf ( "must be a built-in source id, narratio.artifact.<name>, or configured narratio.extraction.<name>" )
2026-05-23 13:28:25 +00:00
}
if classified . Kind == SourceKindConfiguredArtifact {
if configured == nil {
return Source { } , fmt . Errorf ( "configured artifact %q is not defined in pipeline.scriptorium.artifacts" , classified . ConfiguredKey )
}
if _ , ok := configured [ classified . ConfiguredKey ] ; ! ok {
return Source { } , fmt . Errorf ( "configured artifact %q is not defined in pipeline.scriptorium.artifacts" , classified . ConfiguredKey )
}
}
2026-08-09 23:30:32 +00:00
if classified . Kind == SourceKindExtraction {
if _ , ok := extractions [ classified . ConfiguredKey ] ; ! ok {
return Source { } , fmt . Errorf ( "extraction output %q is not defined in pipeline.notarius.outputs" , classified . ConfiguredKey )
}
}
2026-05-23 13:28:25 +00:00
return classified , nil
}
// DeriveDefaultPublishedDestination returns the default publish destination for one source.
func DeriveDefaultPublishedDestination ( source Source , configured map [ string ] string ) ( string , error ) {
switch source . Kind {
case SourceKindBuiltIn :
if spec , ok := artifactmodel . LookupRuntimeTranscriptArtifact ( source . ID ) ; ok {
return pathsafe . NormalizeRelativeDestination ( spec . CanonicalRelPath )
}
if source . ID == SourceBoundsSession {
return pathsafe . NormalizeRelativeDestination ( "artifacts/session_bounds.json" )
}
return "" , fmt . Errorf ( "unsupported built-in source %q" , source . ID )
case SourceKindConfiguredArtifact :
if configured == nil {
return "" , fmt . Errorf ( "configured artifact %q is not defined in pipeline.scriptorium.artifacts" , source . ConfiguredKey )
}
outputPath , ok := configured [ source . ConfiguredKey ]
if ! ok {
return "" , fmt . Errorf ( "configured artifact %q is not defined in pipeline.scriptorium.artifacts" , source . ConfiguredKey )
}
if strings . TrimSpace ( outputPath ) == "" {
return "" , fmt . Errorf ( "pipeline.scriptorium.artifacts.%s.output_path is empty" , source . ConfiguredKey )
}
return pathsafe . NormalizeRelativeDestination ( outputPath )
default :
return "" , fmt . Errorf ( "publish destination cannot be derived from source %q" , source . ID )
}
}
// ResolvePublishedDestination validates and normalizes an explicit destination,
// or derives one when omitted.
func ResolvePublishedDestination ( sourceID , explicitDest string , configured map [ string ] string ) ( string , error ) {
2026-08-10 00:32:24 +00:00
return ResolvePublishedDestinationWithExtractions ( sourceID , explicitDest , configured , nil )
}
// ResolvePublishedDestinationWithExtractions validates and normalizes a destination
// while accepting extraction sources declared by the effective Notarius configuration.
func ResolvePublishedDestinationWithExtractions (
sourceID , explicitDest string ,
configured map [ string ] string ,
extractions map [ string ] struct { } ,
) ( string , error ) {
source , err := ValidatePublishSourceWithExtractions ( sourceID , configured , extractions )
2026-05-23 13:28:25 +00:00
if err != nil {
return "" , err
}
if strings . TrimSpace ( explicitDest ) != "" {
return pathsafe . NormalizeRelativeDestination ( explicitDest )
}
return DeriveDefaultPublishedDestination ( source , configured )
}