StageScopeImpl

class StageScopeImpl<Input, Output, InputLabel>(resources: TrainingResources<Input, Output, InputLabel>, logger: KLogger, val record: StageRecord, parentScope: StageScopeImpl<Input, Output, InputLabel>?, onUpdate: (isStageUpdate: Boolean) -> Unit) : StageScope<Input, Output, InputLabel>

Core implementation of StageScope.

Each instance owns a StageRecord whose MetricsMap is recomputed from substages on each substage update, then propagated upward via onUpdate.

Constructors

Link copied to clipboard
constructor(resources: TrainingResources<Input, Output, InputLabel>, logger: KLogger, record: StageRecord, parentScope: StageScopeImpl<Input, Output, InputLabel>?, onUpdate: (isStageUpdate: Boolean) -> Unit)

Properties

Link copied to clipboard
open override val dataset: TrainSet<Input, InputLabel>

The session's dataset, taken from TrainingResources.dataset.

Link copied to clipboard

The record this scope owns and mutates; its metrics are recomputed on each substage update.

Link copied to clipboard
open override val trackedAgent: GraphAIAgent<Input, Output>

The session's tracked agent, taken from TrainingResources.trackedAgent.

Functions

Link copied to clipboard
suspend fun StageScope<*, *, *>.executePrompt(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): Result<List<Message.Response>>

Executes an LLM prompt with consumption tracking. Creates a PromptExecutionRecord as a leaf record, recording elapsed time and consumption. On success, returns the raw LLM response messages.

Link copied to clipboard
suspend fun StageScope<*, *, *>.executePromptOrThrow(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): List<Message.Response>

.getOrThrow() shortcut over executePrompt; throws on a recorded execution failure.

Link copied to clipboard
inline suspend fun <T> StageScope<*, *, *>.executePromptStructured(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): Result<T>

Executes a structured LLM prompt with consumption tracking. Creates a PromptExecutionRecord and returns the deserialized structure.

Link copied to clipboard
inline suspend fun <T> StageScope<*, *, *>.executePromptStructuredOrThrow(prompt: Prompt, model: LLModel, retryPolicy: RetryPolicy? = null): T

.getOrThrow() shortcut over executePromptStructured; throws on a recorded execution failure.

Link copied to clipboard
open suspend override fun <T> executeWithTrackedPromptExecutor(name: String, retryPolicy: RetryPolicy?, execute: suspend (CapturingPromptExecutor) -> T): Result<T>

Runs execute against a fresh consumption-capturing executor and records exactly one leaf entry under the current stage — PromptExecutionRecord.Completed on success, or PromptExecutionRecord.Failed on any failure. Shared by executePrompt and executePromptStructured.

Link copied to clipboard
open suspend override fun iterateDataset(name: String, dataset: TrainSet<Input, InputLabel>, customMetricsToRecord: List<Metric>?, failureRateThreshold: Double, earlyStop: (TrainSetItem<Input, InputLabel>) -> PrematureExecutionStopDecision, processItem: suspend StageScope<Input, Output, InputLabel>.(TrainSetItem<Input, InputLabel>) -> Unit): StageRecord

Thin delegate over runStage with the datasetIterationMetrics preset (plus any customMetricsToRecord). Threshold breach throws inside the block so runStage's catch arm records it on the dataset stage — preserving the documented "marks but does not throw" contract and letting per-item aborts propagate naturally.

Link copied to clipboard
open override fun logAction(json: Json, builder: ActionLogBuilder.() -> Unit)

Builds an action log via the ActionLogBuilder DSL and stores it on the current stage record.

Link copied to clipboard

Convenience wrapper that maps a collection, enclosing it in a dedicated stage. Pros: automatically sets substagesTotal to enable ETC calculation.

Link copied to clipboard
open override fun recordCustomMetric(metric: Metric)

Attaches a custom metric to the records tree of this stage. This method has two uses:

Link copied to clipboard
open suspend override fun runAgent(item: TrainSetItem<Input, InputLabel>, agentToRun: GraphAIAgent<Input, Output>, retryPolicy: RetryPolicy?): Result<CompletedAgentRun<Output>>

Runs agentToRun on item and records exactly one leaf entry under the current stage — AgentRunRecord.Completed on success, or AgentRunRecord.Failed on any failure (regular exception, retry-exhausted transient, or ExecutionAbortException).

Link copied to clipboard
suspend fun <Input, Output, InputLabel> StageScope<Input, Output, InputLabel>.runAgentOrThrow(item: TrainSetItem<Input, InputLabel>, agentToRun: GraphAIAgent<Input, Output> = trackedAgent, retryPolicy: RetryPolicy? = null): CompletedAgentRun<Output>

.getOrThrow() shortcut over StageScope.runAgent; throws on a recorded run failure.

Link copied to clipboard
open suspend override fun runAgentWithRetries(item: TrainSetItem<Input, InputLabel>, maxAttempts: Int, until: RunUntil, agentToRun: GraphAIAgent<Input, Output>, innerRetryPolicy: RetryPolicy?): CompletedAgentRun<Output>?

Re-samples the agent: runs it up to maxAttempts times, returning the first attempt whose outcome satisfies until (default RunUntil.SOLVED). The intended use is intentional re-sampling — e.g. bootstrap demonstration generation — not transient-failure recovery.

open suspend override fun <RunData> runAgentWithRetries(item: TrainSetItem<Input, InputLabel>, maxAttempts: Int, until: RunUntil, innerRetryPolicy: RetryPolicy?, agentProvider: suspend () -> PreparedAgentRun<Input, Output, RunData>): MatchedAgentRun<Output, RunData>?

Runs the agent up to maxAttempts times, creating a fresh agent per attempt via agentProvider.

Link copied to clipboard
suspend fun <Input, Output, InputLabel, T> StageScope<Input, Output, InputLabel>.runIterableStage(collection: Collection<T>, name: String, itemBlock: suspend StageScope<Input, Output, InputLabel>.(T) -> Unit): Result<Unit>

Convenience wrapper that iterates a collection, enclosing it in a dedicated stage. Pros: automatically sets substagesTotal to enable ETC calculation.

Link copied to clipboard
suspend fun <Input, Output, InputLabel, T> StageScope<Input, Output, InputLabel>.runIterableStageOrThrow(collection: Collection<T>, name: String, itemBlock: suspend StageScope<Input, Output, InputLabel>.(T) -> Unit)
Link copied to clipboard
open suspend override fun <T> runStage(name: String, substagesTotal: Int?, metrics: MetricsMap, block: suspend StageScope<Input, Output, InputLabel>.() -> T): Result<T>

Runs block as a named child stage and records the result.

Link copied to clipboard
suspend fun <Input, Output, InputLabel, T> StageScope<Input, Output, InputLabel>.runStageOrThrow(name: String, substagesTotal: Int? = null, metrics: MetricsMap = standardStageMetrics(), block: suspend StageScope<Input, Output, InputLabel>.() -> T): T

.getOrThrow() shortcut over StageScope.runStage. See its KDoc for the metrics preset conventions; the default and parameter semantics are identical.

Link copied to clipboard
inline fun <T> StageScope<*, *, *>.setAdditionalData(data: T)

Serializes data with defaultExperimentsJson and stores it as this stage's additional data via StageScope.setAdditionalDataJson. Overwrites any previously set value.

Link copied to clipboard
open override fun setAdditionalDataJson(additionalData: JsonElement)

Sets some additional data in this stage's record. Overwrites if additional data was already added.