github.com/rotari-dev/rotari/internal/artifact

Overview

package artifact // import "github.com/rotari-dev/rotari/internal/artifact"

Package artifact discovers artifact candidates: file and directory references
that can be found statically in a job's definition. A candidate is not a proven
output of the job; inputs and outputs are not distinguished, and discovery is
best-effort. The package owns the one classifier every discovery source uses.
It never opens the paths it finds and reads no files itself; callers supply the
job definition and the contents of referenced sources.

Constants

const (
	// MaxSourceBytes is the largest source discovery parses. A larger file
	// is not read at all: a configuration file that size is abnormal, and
	// YAML parses at only a few megabytes per second.
	MaxSourceBytes = 256 << 10
	// MaxSourceDepth bounds nesting of mappings and sequences.
	MaxSourceDepth = 64
	// MaxSourceValues bounds the scalar values inspected in one source.
	MaxSourceValues = 50000
)
    Limits on inspecting one configuration source.

const (
	KindArgument    = "argument"
	KindEnvironment = "environment"
	KindOutput      = "output"
	KindError       = "error"
	KindConfig      = "config"
	KindShell       = "shell"
	KindPython      = "python"
	KindDeclared    = "declared"
)
    Source kinds say where a reference was found.

const (
	// BasisAbsolute: the reference was written as an absolute path.
	BasisAbsolute = "absolute"
	// BasisWorkingDirectory: a relative reference joined to the job's
	// effective working directory.
	BasisWorkingDirectory = "working_directory"
	// BasisUnresolved: a relative reference whose working directory is not
	// known yet, such as a queued job without an absolute directory.
	BasisUnresolved = "unresolved"
)
    Resolution bases say how a candidate's Path was obtained.

const DiscoveryVersion = 7
    DiscoveryVersion identifies the discovery rules that produced a Record. Bump
    it when a rule change means older records would be discovered differently
    today; older records are kept as they are, not recomputed.

const MaxCandidates = 1000
    MaxCandidates bounds the candidates one job records. References beyond it
    are dropped with a diagnostic.

Functions

func ConfigFormat(value string) string
    ConfigFormat returns "yaml", "json", or "toml" for a configuration file
    discovery inspects, or "" for any other value.

func Describe(sources []Source) string
    Describe returns a one-line account of where a candidate was found,
    for viewers: the option, variable, or key it appeared under, the redirection
    operator, and the file and line:column of configuration, shell, or Python
    source. Identical accounts are named once, and sources beyond the first few
    are counted.

func IsPathKey(key string) bool
    IsPathKey reports whether key narrowly names a path for PATH-R5: path,
    file, or dir, or a name ending in _path, _file, or _dir, compared
    case-insensitively with hyphens read as underscores. Only the leaf of a
    dotted key counts, and leading option dashes and Hydra override prefixes (+,
    ++, ~) are ignored.

func IsSpecialSink(value string) bool
    IsSpecialSink reports whether value names a device or descriptor that is
    never an artifact, such as /dev/null (PATH-X3).

Types

type Candidate struct {
	// Path is the cleaned reference: absolute unless Basis is unresolved.
	Path    string   `json:"path"`
	Basis   string   `json:"basis"`
	Sources []Source `json:"sources"`
}
    Candidate is one discovered file or directory reference. It says nothing
    about whether the path exists, its type, or whether the job created it.

type ConfigReader func(path string) ([]ConfigReference, error)
    ConfigReader returns the references in the configuration file at an absolute
    path, as ConfigReferences finds them. With an error it may still return
    the references found before a limit was reached. A ConfigReader may cache
    results: they do not depend on the job.

func ParseSources(read SourceReader) ConfigReader
    ParseSources returns a ConfigReader that reads each file through read and
    parses it with ConfigReferences, without caching.

type ConfigReference struct {
	// Value is the text as written; Path is the path it names, which
	// differs only for a local URI (PATH-R6).
	Value    string
	Path     string
	Rule     Rule
	Key      string
	Location string
}
    ConfigReference is one path reference found in a configuration source.

func ConfigReferences(format string, data []byte) ([]ConfigReference, error)
    ConfigReferences parses a configuration source of format "yaml", "json",
    or "toml" structurally and classifies each string value, with its nearest
    mapping key as context. Interpolation, custom YAML tags, and aliases are
    not evaluated: a tagged or aliased value is skipped, as is a string with
    unresolved interpolation. The references found before a limit was reached
    are returned with the error.

type Diagnostic struct {
	Source  string `json:"source,omitempty"`
	Message string `json:"message"`
}
    Diagnostic records a discovery problem. It never changes the job's execution
    or result.

type Job struct {
	Command     []string
	Environment []string
	Output      []string
	Error       []string
	// WorkingDirectory is the effective directory the job runs in. When it
	// is empty or relative, relative references stay unresolved.
	WorkingDirectory string
	// Declared are the paths the user declared with add --artifact.
	Declared []string
	// Variables are values rotari fixes for the attempt, such as
	// ROTARI_ARRAY_TASK_ID and ROTARI_JOB_DIR. With the Environment entries
	// they are the only variables shell source expands (PATH-E1).
	Variables map[string]string
}
    Job is the part of a job definition discovery reads.

type Record struct {
	Version int `json:"version"`
	// WorkingDirectory is the effective directory relative references were
	// resolved on, or "" when it was not known.
	WorkingDirectory string `json:"working_directory,omitempty"`
	Result
}
    Record is the persisted discovery of one job attempt.

type Result struct {
	Candidates  []Candidate  `json:"candidates,omitempty"`
	Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
}
    Result is a job's discovered candidates, in order of first reference.

func Discover(job Job, sources Sources) Result
    Discover finds the candidates FromJob finds, then inspects the shell
    scripts the job runs and the Python files it names through sources.Script,
    then each configuration file (.yaml, .yml, .json, .toml) referenced by a
    command argument, an environment value, shell source, or Python source and
    resolved to an absolute path, through sources.Config. References found in a
    configuration file are not inspected in turn.

func FromJob(job Job) Result
    FromJob discovers the candidates in a job's command arguments, environment
    values, and log destinations. Referenced configuration files are not read;
    see Discover.

type Rule string
    Rule identifies the classifier rule that accepted a reference,
    so a candidate stays explainable. The IDs match the discovery plan in
    development/2026-10-03-artifact-discovery/plan.md.

const (
	// RuleRedirection accepts a literal file-opening shell redirection
	// target. It is reserved for shell inspection.
	RuleRedirection Rule = "PATH-R1"
	// RuleExplicitPath accepts explicit absolute or relative path notation:
	// /work/results, ./results, ../results.
	RuleExplicitPath Rule = "PATH-R2"
	// RuleDirectoryReference accepts a relative reference containing a
	// directory separator: results/metrics, results/.
	RuleDirectoryReference Rule = "PATH-R3"
	// RuleExtension accepts a basename with a recognized filename extension.
	RuleExtension Rule = "PATH-R4"
	// RulePathKey accepts a literal value under a recognized path key or long
	// option: output_dir, --file-path, checkpoint_file, or an output or
	// input key such as --output unless the value is a format name.
	RulePathKey Rule = "PATH-R5"
	// RuleLogDestination accepts a job's own stdout or stderr destination,
	// which rotari writes itself, without classification.
	RuleLogDestination Rule = "PATH-D1"
	// RuleDeclared accepts a path the user declared with add --artifact,
	// without classification.
	RuleDeclared Rule = "PATH-D2"
	// RuleLocalURI accepts a URI that names a local file, file: or sqlite:,
	// as experiment trackers take (MLFLOW_TRACKING_URI=sqlite:///mlflow.db),
	// and records the path it names. Every other URI stays skipped by
	// PATH-X1.
	RuleLocalURI Rule = "PATH-R6"
)
func Classify(value, key string, syntax Syntax) (string, Rule, bool)
    Classify decides whether value is a path reference. key is the context the
    value appears under: a long option name, a key=value key, an environment
    variable name, or a configuration key; empty when none. It returns the path
    the value names, which differs from value only for a local URI (PATH-R6),
    and the accepting rule, or false when the value is skipped.

type Source struct {
	Kind string `json:"kind"`
	// Value is the reference as written.
	Value string `json:"value"`
	Rule  Rule   `json:"rule"`
	// Index is the position in the command, environment, or destination list.
	Index *int `json:"index,omitempty"`
	// Key is the option, key=value key, environment variable, or
	// configuration key the value appeared under.
	Key string `json:"key,omitempty"`
	// Stream is stdout or stderr for a log destination.
	Stream string `json:"stream,omitempty"`
	// File is the configuration source the reference was found in.
	File string `json:"file,omitempty"`
	// Location is the key or index path within File, such as train.paths[1],
	// or the line:column within shell source.
	Location string `json:"location,omitempty"`
	// Direction is the redirection operator of a PATH-R1 target, such as >
	// or <.
	Direction string `json:"direction,omitempty"`
	// Expanded reports that PATH-E1 expanded a variable in the value.
	Expanded bool `json:"expanded,omitempty"`
}
    Source records where one reference to a candidate was found and the evidence
    that hints at its role, without deciding the role.

type SourceReader func(path string) ([]byte, error)
    SourceReader returns the contents of a referenced source file, given its
    absolute path. It returns an error when the file cannot be read safely;
    discovery records the error as a diagnostic and continues.

type Sources struct {
	// Config returns the references of a configuration file.
	Config ConfigReader
	// Script returns the contents of a shell script or Python file.
	Script SourceReader
}
    Sources reads the files discovery inspects. A nil reader inspects no file of
    its kind.

type Syntax int
    Syntax says how a value is written, which decides whether unexpanded
    interpolation and glob characters are literal.

const (
	// Literal values reach the program as written: argv and environment
	// values, which the executors pass quoted and no shell expands.
	Literal Syntax = iota
	// Interpolated values come from configuration or shell source, where an
	// application or shell may expand $NAME, ${...}, globs, or ~. They are
	// skipped when they contain such syntax (PATH-X2).
	Interpolated
)