Crucible.Context (CrucibleFramework v0.5.2)
View SourceRuntime context threaded through experiment pipeline stages.
This is a simplified, generic context struct that doesn't assume any
specific domain (training, CNS, etc.). Domain-specific data should
be stored in assigns.
Fields
experiment_id- Unique identifier for the experimentrun_id- Unique identifier for this runexperiment- TheCrucibleIR.Experimentstructoutputs- List of outputs generated by stagesmetrics- Map of metrics collected during executionartifacts- Map of artifacts (files, reports, etc.)trace- Optional trace chain for observabilitytelemetry_context- Telemetry metadataassigns- Extension point for domain-specific data
Helper Functions
Metrics Management
put_metric/3- Add or update a metricget_metric/3- Get a metric valueupdate_metric/3- Update metric using functionmerge_metrics/2- Merge multiple metricshas_metric?/2- Check if metric exists
Output Management
add_output/2- Add single outputadd_outputs/2- Add multiple outputs
Artifact Management
put_artifact/3- Store an artifactget_artifact/3- Retrieve an artifacthas_artifact?/2- Check if artifact exists
Assigns Management (Phoenix-style)
Stage Tracking
mark_stage_complete/2- Mark stage as completedstage_completed?/2- Check if stage completedcompleted_stages/1- List all completed stages
Examples
# Create a context with required fields
ctx = %Crucible.Context{
experiment_id: "exp1",
run_id: "run1",
experiment: %CrucibleIR.Experiment{id: "exp1"}
}
# Add metrics
ctx = Crucible.Context.put_metric(ctx, :accuracy, 0.95)
Crucible.Context.get_metric(ctx, :accuracy)
# => 0.95
# Phoenix-style assigns for domain-specific data
ctx = Crucible.Context.assign(ctx, user: "alice", priority: :high)
ctx.assigns.user
# => "alice"
# Training stages store their data in assigns
ctx = Crucible.Context.assign(ctx, :dataset, my_dataset)
ctx = Crucible.Context.assign(ctx, :backend_session, session)
# Track stage completion
ctx = Crucible.Context.mark_stage_complete(ctx, :data_load)
Crucible.Context.stage_completed?(ctx, :data_load)
# => true
Summary
Functions
Adds a single output to the context.
Adds multiple outputs to the context.
Assigns multiple values to the context assigns.
Assigns a single key-value pair to the context assigns.
Returns a list of all completed stages.
Retrieves an artifact from the context.
Gets a metric from the context, returning a default if not found.
Checks if an artifact exists in the context.
Checks if a metric exists in the context.
Marks a stage as completed in the context.
Merges multiple metrics into the context.
Stores an artifact in the context.
Puts a metric into the context.
Checks if a stage has been completed.
Updates a metric using a function.
Types
Functions
Adds a single output to the context.
Examples
ctx = add_output(ctx, %{result: "success"})
length(ctx.outputs)
# => 1
Adds multiple outputs to the context.
Examples
ctx = add_outputs(ctx, [%{result: "a"}, %{result: "b"}])
length(ctx.outputs)
# => 2
Assigns multiple values to the context assigns.
Examples
ctx = assign(ctx, user: "alice", priority: :high)
ctx.assigns.user
# => "alice"
Assigns a single key-value pair to the context assigns.
Examples
ctx = assign(ctx, :user, "alice")
ctx.assigns.user
# => "alice"
Returns a list of all completed stages.
Examples
completed_stages(ctx)
# => [:data_load, :backend_call, :bench]
Retrieves an artifact from the context.
Examples
get_artifact(ctx, :report)
# => "report.html"
get_artifact(ctx, :missing, :not_found)
# => :not_found
Gets a metric from the context, returning a default if not found.
Examples
get_metric(ctx, :accuracy)
# => 0.95
get_metric(ctx, :missing, :default)
# => :default
Checks if an artifact exists in the context.
Examples
has_artifact?(ctx, :report)
# => true
Checks if a metric exists in the context.
Examples
has_metric?(ctx, :accuracy)
# => true
has_metric?(ctx, :missing)
# => false
Marks a stage as completed in the context.
Examples
ctx = mark_stage_complete(ctx, :data_load)
stage_completed?(ctx, :data_load)
# => true
Merges multiple metrics into the context.
Examples
ctx = merge_metrics(ctx, %{accuracy: 0.95, loss: 0.05})
get_metric(ctx, :accuracy)
# => 0.95
Stores an artifact in the context.
Examples
ctx = put_artifact(ctx, :report, "report.html")
get_artifact(ctx, :report)
# => "report.html"
Puts a metric into the context.
Examples
ctx = put_metric(ctx, :accuracy, 0.95)
ctx.metrics.accuracy
# => 0.95
Checks if a stage has been completed.
Examples
stage_completed?(ctx, :data_load)
# => true
stage_completed?(ctx, :not_run)
# => false
Updates a metric using a function.
Examples
ctx = put_metric(ctx, :count, 1)
ctx = update_metric(ctx, :count, &(&1 + 1))
get_metric(ctx, :count)
# => 2