graphrt-core 0.6.5

Executor-agnostic graph runtime for agentic AI workflows: graph model, variable pool, scheduler, events, snapshots and built-in control nodes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Root-run orchestration: bootstrap the variable pool, run the root frame,
//! enforce the run-wide duration budget and emit the terminal event.

use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use futures::future::Either;
use indexmap::IndexMap;

use super::control::{Control, Stop};
use super::frame::{Frame, FrameOutcome, RunShared};
use super::ids::new_id;
use super::{Engine, ResumeFrom, RunInput};
use crate::dsl::VariableDecl;
use crate::event::{Event, PauseReason, Usage};
use crate::node::{EventSink, NodeError};
use crate::pool::{CONVERSATION, ENV, SYS, Selector, VariablePool};
use crate::redact::SecretSet;
use crate::snapshot::{FrameSnapshot, SNAPSHOT_FORMAT, Snapshot};
use crate::value::Value;

/// A failure before the run starts: the message and its `error_type`.
type StartFailure = (String, &'static str);

/// Which run this is and where it starts.
struct RunOrigin {
    run_id: String,
    /// Run time spent before this segment (the snapshot's, on resume).
    prior_elapsed_ms: u64,
    /// The root frame to restore; `None` for a new run.
    root_snapshot: Option<FrameSnapshot>,
}

/// Counters and resume data a snapshot carries into [`RunShared`] (all empty
/// for a new run).
#[derive(Default)]
struct CarriedState {
    steps: u64,
    budget: crate::budget::BudgetState,
    usage: Usage,
    handled_errors: u32,
    resume_inputs: IndexMap<String, Value>,
    secrets: Vec<(Selector, Value)>,
}

/// The host's values for the run, `env` and `conversation` resolved against
/// the workflow's declarations.
struct HostValues {
    env: Vec<(String, Value)>,
    conversation: Vec<(String, Value)>,
    /// The host's own `conversation` values, re-applied onto a restored frame.
    conversation_overrides: IndexMap<String, Value>,
    sys: IndexMap<String, Value>,
    inputs: IndexMap<String, Value>,
    /// The budget of this run: the engine's limits, lowered by the request's.
    #[cfg(feature = "budgets")]
    max_total_tokens: Option<u64>,
    #[cfg(feature = "budgets")]
    max_cost_micros: Option<u64>,
}

/// The lower of two optional limits (`None`: no limit).
#[cfg(feature = "budgets")]
fn lower(engine: Option<u64>, request: Option<u64>) -> Option<u64> {
    match (engine, request) {
        (Some(a), Some(b)) => Some(a.min(b)),
        (a, b) => a.or(b),
    }
}

/// Drive a whole run, emitting every event through `sink`.
pub(crate) async fn run_root(engine: Engine, input: RunInput, control: Arc<Control>, sink: EventSink) {
    let started_ms = engine.clock.now_ms();
    let (origin, host, carried) = match prepare_run(&engine, input, started_ms) {
        Ok(prepared) => prepared,
        Err((error, error_type)) => return emit_start_failure(&sink, error, error_type),
    };
    let shared = share_run(&engine, &control, &sink, &host, carried);
    let RunOrigin { run_id, prior_elapsed_ms, root_snapshot } = origin;
    sink(Event::RunStarted { run_id: run_id.clone(), resumed: root_snapshot.is_some() });
    let frame = match root_snapshot {
        Some(snapshot) => restored_root(&shared, &host, snapshot),
        None => Frame::fresh(shared.clone(), Arc::from(""), None, 0, fresh_root_pool(&engine, &host, &run_id)),
    };
    let frame = match frame {
        Ok(frame) => frame,
        Err(e) => return emit_frame_failure(&shared, &e),
    };
    // The run-wide duration budget (`Limits::max_duration_ms`, minus the time
    // before a resume) stops the root frame when it runs out.
    let outcome = match duration_budget_ms(&shared, prior_elapsed_ms) {
        Some(ms) => {
            let run = frame.run();
            futures::pin_mut!(run);
            match futures::future::select(run, shared.clock.sleep(Duration::from_millis(ms))).await {
                Either::Left((outcome, _)) => outcome,
                Either::Right(((), _)) => stop_over_duration_limit(&shared),
            }
        }
        None => frame.run().await,
    };
    let elapsed_ms = prior_elapsed_ms.saturating_add(engine.clock.now_ms().saturating_sub(started_ms));
    sink(terminal_event(&shared, outcome, run_id, elapsed_ms));
}

/// Validate the host's values and the resume snapshot, in that order.
///
/// @throws StartFailure - `invalid_input` for a value of the wrong type or a bad
///   resume secret, `snapshot_mismatch` for a snapshot of another format or graph.
fn prepare_run(
    engine: &Engine,
    input: RunInput,
    started_ms: u64,
) -> Result<(RunOrigin, HostValues, CarriedState), StartFailure> {
    let workflow = engine.graph.workflow();
    // Env the host refuses this workflow: the run cannot do what it was written to.
    #[cfg(feature = "bundle-env")]
    if let Some(why) = engine.refused_env.as_deref().and_then(super::RefusedEnv::failure) {
        return Err((why, "env_refused"));
    }
    let env = resolve_decls(&workflow.env, &input.env, true).map_err(invalid_input)?;
    let conversation = resolve_decls(&workflow.conversation, &input.conversation, false).map_err(invalid_input)?;
    let (origin, carried) = match input.resume {
        Some(resume) => resume_origin(engine, resume)?,
        None => {
            let run_id = input.run_id.unwrap_or_else(|| new_id(started_ms));
            (RunOrigin { run_id, prior_elapsed_ms: 0, root_snapshot: None }, CarriedState::default())
        }
    };
    let host = HostValues {
        env,
        conversation,
        conversation_overrides: input.conversation,
        sys: input.sys,
        inputs: input.inputs,
        // The engine's, a resumed run's own (kept in its snapshot) and this
        // request's: the lowest applies, so no request raises a limit.
        #[cfg(feature = "budgets")]
        max_total_tokens: lower(
            lower(engine.limits.max_total_tokens, carried.budget.max_total_tokens),
            input.max_total_tokens,
        ),
        #[cfg(feature = "budgets")]
        max_cost_micros: lower(
            lower(engine.limits.max_cost_micros, carried.budget.max_cost_micros),
            input.max_cost_micros,
        ),
    };
    Ok((origin, host, carried))
}

fn invalid_input(error: String) -> StartFailure {
    (error, "invalid_input")
}

/// Where a resumed run continues: its snapshot, once it matches this engine.
///
/// @throws StartFailure - See [`prepare_run`].
fn resume_origin(engine: &Engine, resume: ResumeFrom) -> Result<(RunOrigin, CarriedState), StartFailure> {
    let snapshot = resume.snapshot;
    check_snapshot(engine, &snapshot)?;
    let secrets = parse_secrets(&resume.secrets).map_err(invalid_input)?;
    let origin = RunOrigin {
        run_id: snapshot.run_id,
        prior_elapsed_ms: snapshot.elapsed_ms,
        root_snapshot: Some(snapshot.root),
    };
    let carried = CarriedState {
        steps: snapshot.steps,
        budget: snapshot.budget,
        usage: snapshot.usage,
        handled_errors: snapshot.handled_errors,
        resume_inputs: resume.inputs,
        secrets,
    };
    Ok((origin, carried))
}

/// @throws StartFailure (`snapshot_mismatch`) - A snapshot of another format,
///   or of a graph other than this engine's.
fn check_snapshot(engine: &Engine, snapshot: &Snapshot) -> Result<(), StartFailure> {
    if snapshot.format != SNAPSHOT_FORMAT {
        return Err((crate::Error::SnapshotFormat(snapshot.format).to_string(), "snapshot_mismatch"));
    }
    if snapshot.graph_fingerprint != engine.fingerprint() {
        let mismatch = crate::Error::SnapshotMismatch {
            expected: engine.fingerprint().to_owned(),
            actual: snapshot.graph_fingerprint.clone(),
        };
        #[allow(unused_mut)]
        let mut message = mismatch.to_string();
        // After `+` come the linked workflows (`len:name=fingerprint`).
        #[cfg(feature = "subagents")]
        if engine.fingerprint().contains('+') || snapshot.graph_fingerprint.contains('+') {
            message.push_str(
                ": the workflow, or a workflow linked to it as a subagent, changed or is not linked as when the run paused",
            );
        }
        return Err((message, "snapshot_mismatch"));
    }
    Ok(())
}

fn emit_start_failure(sink: &EventSink, error: String, error_type: &str) {
    sink(Event::RunFailed { error, error_type: error_type.to_owned(), usage: Usage::default(), elapsed_ms: 0 });
}

fn emit_frame_failure(shared: &RunShared, error: &NodeError) {
    shared.emit(Event::RunFailed {
        error: error.to_string(),
        error_type: error.kind.clone(),
        usage: shared.final_usage(),
        elapsed_ms: 0,
    });
}

/// The state every frame of the run shares, with every secret known up front
/// observed so its plaintext is scrubbed from node error messages. Nodes add
/// the secrets they produce (see `Frame::commit`).
fn share_run(
    engine: &Engine,
    control: &Arc<Control>,
    sink: &EventSink,
    host: &HostValues,
    carried: CarriedState,
) -> Arc<RunShared> {
    let secret_values = Arc::new(SecretSet::new());
    // Includes secret-typed conversation variables, scrubbed like any other secret.
    let initial = host
        .env
        .iter()
        .map(|(_, v)| v)
        .chain(carried.secrets.iter().map(|(_, v)| v))
        .chain(host.inputs.values())
        .chain(host.conversation.iter().map(|(_, v)| v));
    initial.for_each(|value| secret_values.observe(value));
    #[cfg(feature = "budgets")]
    let stopper = Arc::clone(control);
    #[cfg(feature = "budgets")]
    let budget = Arc::new(crate::budget::RunBudget::new(
        host.max_total_tokens,
        host.max_cost_micros,
        (carried.budget.tokens, carried.budget.cost_micros),
        Box::new(move |why| stopper.stop(super::control::Stop::Fail(why))),
    ));
    Arc::new(RunShared {
        #[cfg(feature = "budgets")]
        budget,
        #[cfg(not(feature = "budgets"))]
        budget: carried.budget,
        graph: engine.graph.clone(),
        services: engine.services.clone(),
        clock: engine.clock.clone(),
        limits: engine.limits.clone(),
        control: control.clone(),
        sink: sink.clone(),
        steps: AtomicU64::new(carried.steps),
        handled_errors: AtomicU32::new(carried.handled_errors),
        usage: Mutex::new(carried.usage),
        #[cfg(feature = "budgets")]
        executions: Mutex::new(Vec::new()),
        env: host.env.clone(),
        secrets: carried.secrets,
        resume_inputs: carried.resume_inputs,
        secret_values,
        #[cfg(feature = "subagents")]
        fingerprint: engine.fingerprint().to_owned(),
        #[cfg(feature = "subagents")]
        linked: Arc::clone(&engine.linked),
        #[cfg(feature = "subagents")]
        parents: Mutex::new(std::collections::HashSet::new()),
        #[cfg(feature = "subagents")]
        running_children: Mutex::new(std::collections::HashSet::new()),
    })
}

/// The root frame of a resumed run. Host-supplied `sys` and `conversation`
/// values take precedence over persisted ones.
fn restored_root(shared: &Arc<RunShared>, host: &HostValues, snapshot: FrameSnapshot) -> Result<Frame, NodeError> {
    let mut frame = Frame::restore(shared.clone(), Arc::from(""), None, 0, snapshot)?;
    apply_host_values(&mut frame, &host.sys, &host.conversation_overrides);
    Ok(frame)
}

/// The variable pool of a new run's root frame.
fn fresh_root_pool(engine: &Engine, host: &HostValues, run_id: &str) -> VariablePool {
    // Node namespaces plus `env`, `conversation` and `sys`.
    let mut pool = VariablePool::with_namespace_capacity(engine.graph.nodes().len() + 3);
    for (name, value) in &host.env {
        pool.insert(ENV, name, value.clone());
    }
    for (name, value) in &host.conversation {
        pool.insert(CONVERSATION, name, value.clone());
    }
    for (name, value) in &host.sys {
        pool.insert(SYS, name, value.clone());
    }
    pool.insert(SYS, "run_id", Value::from(run_id));
    // Start inputs live in the entry node's namespace; the start node validates
    // and re-emits them as its outputs. They are never copied into `sys`.
    if let Some(entry) = engine.graph.root_scope().and_then(|s| engine.graph.node(s.entry)) {
        for (name, value) in &host.inputs {
            pool.insert(&entry.id, name, value.clone());
        }
    }
    pool
}

/// Milliseconds the run may still take, when it has a duration limit and the
/// clock can time it.
fn duration_budget_ms(shared: &RunShared, prior_elapsed_ms: u64) -> Option<u64> {
    let budget = shared.limits.max_duration_ms.map(|ms| ms.saturating_sub(prior_elapsed_ms))?;
    shared.clock.supports_timers().then_some(budget)
}

/// Stop a run that exceeded `Limits::max_duration_ms`.
fn stop_over_duration_limit(shared: &RunShared) -> FrameOutcome {
    let limit = shared.limits.max_duration_ms.unwrap_or_default();
    shared.control.stop(Stop::Fail(format!("run exceeded its duration limit of {limit} ms")));
    FrameOutcome::Stopped
}

/// The run's terminal event for the root frame's `outcome`.
fn terminal_event(shared: &RunShared, outcome: FrameOutcome, run_id: String, elapsed_ms: u64) -> Event {
    let usage =
        if matches!(outcome, FrameOutcome::Paused { .. }) { shared.total_usage() } else { shared.final_usage() };
    match outcome {
        FrameOutcome::Completed { outputs, .. } => Event::RunSucceeded {
            outputs,
            usage,
            handled_errors: shared.handled_errors.load(Ordering::Acquire),
            elapsed_ms,
        },
        FrameOutcome::Failed { error, .. } => {
            Event::RunFailed { error: error.to_string(), error_type: error.kind.clone(), usage, elapsed_ms }
        }
        FrameOutcome::Paused { snapshot, .. } => {
            let snapshot = Snapshot {
                format: SNAPSHOT_FORMAT,
                run_id,
                #[cfg(feature = "subagents")]
                graph_fingerprint: shared.fingerprint.clone(),
                #[cfg(not(feature = "subagents"))]
                graph_fingerprint: shared.graph.fingerprint().to_owned(),
                elapsed_ms,
                steps: shared.steps.load(Ordering::Acquire),
                usage,
                handled_errors: shared.handled_errors.load(Ordering::Acquire),
                #[cfg(feature = "budgets")]
                budget: shared.budget.state(),
                // Kept as it came: a build without budgets charges nothing.
                #[cfg(not(feature = "budgets"))]
                budget: shared.budget,
                root: *snapshot,
            };
            Event::RunPaused { reasons: pause_reasons(&shared.control), snapshot: Box::new(snapshot) }
        }
        FrameOutcome::Stopped => stopped_event(&shared.control, usage, elapsed_ms),
    }
}

/// Why the run paused: its nodes' reasons, or an `external` one for a pause
/// requested through the run handle.
fn pause_reasons(control: &Control) -> Vec<PauseReason> {
    let mut reasons = control.reasons();
    if reasons.is_empty() && control.pause_requested() {
        reasons.push(PauseReason {
            frame: Arc::from(""),
            node_id: None,
            kind: "external".to_owned(),
            data: Value::Null,
        });
    }
    reasons
}

/// The terminal event of a stopped run: a limit failure or an abort.
fn stopped_event(control: &Control, usage: Usage, elapsed_ms: u64) -> Event {
    match control.stop_reason() {
        Some(Stop::Fail(error)) => {
            Event::RunFailed { error, error_type: "limit_exceeded".to_owned(), usage, elapsed_ms }
        }
        Some(Stop::Abort(reason)) => Event::RunAborted { reason, elapsed_ms },
        None => Event::RunAborted { reason: "stopped".to_owned(), elapsed_ms },
    }
}

/// Parse `ResumeFrom::secrets` keys into selectors; values become secrets.
fn parse_secrets(secrets: &IndexMap<String, Value>) -> Result<Vec<(Selector, Value)>, String> {
    let mut out = Vec::with_capacity(secrets.len());
    for (key, value) in secrets {
        let selector = Selector::parse(key).map_err(|e| format!("resume secret `{key}`: {e}"))?;
        let plaintext = value.expose_secret().ok_or_else(|| format!("resume secret `{key}` must be a string"))?;
        out.push((selector, Value::Secret(Arc::from(plaintext))));
    }
    Ok(out)
}

/// Re-apply host-provided `sys` and `conversation` values onto a restored root frame.
fn apply_host_values(frame: &mut Frame, sys: &IndexMap<String, Value>, conversation: &IndexMap<String, Value>) {
    for (name, value) in sys {
        frame.set_variable(SYS, name, value.clone());
    }
    for (name, value) in conversation {
        frame.set_variable(CONVERSATION, name, value.clone());
    }
}

/// Merge declared variables with host overrides, coercing to the declared types.
/// Declarations without a value and without an override are omitted when
/// `omit_missing`, otherwise they get their type's zero value.
pub(crate) fn resolve_decls(
    decls: &[VariableDecl],
    overrides: &IndexMap<String, Value>,
    omit_missing: bool,
) -> Result<Vec<(String, Value)>, String> {
    let mut out: Vec<(String, Value)> = Vec::new();
    for decl in decls {
        let raw = overrides.get(&decl.name).cloned().or_else(|| decl.value.clone());
        let value = match raw {
            Some(v) => decl.ty.coerce(v).map_err(|e| format!("variable `{}`: {e}", decl.name))?,
            None if omit_missing => continue,
            None => decl.ty.zero(),
        };
        out.push((decl.name.clone(), value));
    }
    for (name, value) in overrides {
        if !decls.iter().any(|d| &d.name == name) {
            out.push((name.clone(), value.clone()));
        }
    }
    Ok(out)
}