Crucible.Context (CrucibleFramework v0.5.2)

View Source

Runtime 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 experiment
  • run_id - Unique identifier for this run
  • experiment - The CrucibleIR.Experiment struct
  • outputs - List of outputs generated by stages
  • metrics - Map of metrics collected during execution
  • artifacts - Map of artifacts (files, reports, etc.)
  • trace - Optional trace chain for observability
  • telemetry_context - Telemetry metadata
  • assigns - Extension point for domain-specific data

Helper Functions

Metrics Management

Output Management

Artifact Management

Assigns Management (Phoenix-style)

  • assign/2 - Assign single or multiple values
  • assign/3 - Assign single key-value pair

Stage Tracking

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

t()

@type t() :: %Crucible.Context{
  artifacts: map(),
  assigns: map(),
  experiment: CrucibleIR.Experiment.t(),
  experiment_id: String.t(),
  metrics: map(),
  outputs: list(),
  run_id: String.t(),
  telemetry_context: map(),
  trace: term() | nil
}

Functions

add_output(ctx, output)

@spec add_output(t(), term()) :: t()

Adds a single output to the context.

Examples

ctx = add_output(ctx, %{result: "success"})
length(ctx.outputs)
# => 1

add_outputs(ctx, outputs)

@spec add_outputs(t(), list()) :: t()

Adds multiple outputs to the context.

Examples

ctx = add_outputs(ctx, [%{result: "a"}, %{result: "b"}])
length(ctx.outputs)
# => 2

assign(ctx, assigns)

@spec assign(t(), keyword() | map()) :: t()

Assigns multiple values to the context assigns.

Examples

ctx = assign(ctx, user: "alice", priority: :high)
ctx.assigns.user
# => "alice"

assign(ctx, key, value)

@spec assign(t(), atom(), term()) :: t()

Assigns a single key-value pair to the context assigns.

Examples

ctx = assign(ctx, :user, "alice")
ctx.assigns.user
# => "alice"

completed_stages(ctx)

@spec completed_stages(t()) :: [atom()]

Returns a list of all completed stages.

Examples

completed_stages(ctx)
# => [:data_load, :backend_call, :bench]

get_artifact(ctx, key, default \\ nil)

@spec get_artifact(t(), atom(), term()) :: term()

Retrieves an artifact from the context.

Examples

get_artifact(ctx, :report)
# => "report.html"

get_artifact(ctx, :missing, :not_found)
# => :not_found

get_metric(ctx, key, default \\ nil)

@spec get_metric(t(), atom(), term()) :: term()

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

has_artifact?(ctx, key)

@spec has_artifact?(t(), atom()) :: boolean()

Checks if an artifact exists in the context.

Examples

has_artifact?(ctx, :report)
# => true

has_metric?(ctx, key)

@spec has_metric?(t(), atom()) :: boolean()

Checks if a metric exists in the context.

Examples

has_metric?(ctx, :accuracy)
# => true

has_metric?(ctx, :missing)
# => false

mark_stage_complete(ctx, stage_name)

@spec mark_stage_complete(t(), atom()) :: t()

Marks a stage as completed in the context.

Examples

ctx = mark_stage_complete(ctx, :data_load)
stage_completed?(ctx, :data_load)
# => true

merge_metrics(ctx, metrics)

@spec merge_metrics(t(), map()) :: t()

Merges multiple metrics into the context.

Examples

ctx = merge_metrics(ctx, %{accuracy: 0.95, loss: 0.05})
get_metric(ctx, :accuracy)
# => 0.95

put_artifact(ctx, key, artifact)

@spec put_artifact(t(), atom(), term()) :: t()

Stores an artifact in the context.

Examples

ctx = put_artifact(ctx, :report, "report.html")
get_artifact(ctx, :report)
# => "report.html"

put_metric(ctx, key, value)

@spec put_metric(t(), atom(), term()) :: t()

Puts a metric into the context.

Examples

ctx = put_metric(ctx, :accuracy, 0.95)
ctx.metrics.accuracy
# => 0.95

stage_completed?(ctx, stage_name)

@spec stage_completed?(t(), atom()) :: boolean()

Checks if a stage has been completed.

Examples

stage_completed?(ctx, :data_load)
# => true

stage_completed?(ctx, :not_run)
# => false

update_metric(ctx, key, update_fn)

@spec update_metric(t(), atom(), (term() -> term())) :: t()

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