Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/taurus-core/src/handler/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub struct FunctionThunk {
/// caller seeds; and it is handed on to remote nodes as a callable sub
/// flow (and reported) as-is instead of being run eagerly.
pub sub_flow_function: Option<Arc<SubFlowFunctionResult>>,
/// Overrides resolved in the parent context at mint time (parallel to
/// `settings`): `Some` wins over the caller's value for that slot (even
/// `Some(null)`), `None` keeps it. Empty on the local path and for
/// returned functions. See `resolve_function_setting`.
pub resolved_overrides: Vec<Option<Value>>,
}

impl fmt::Debug for FunctionThunk {
Expand All @@ -55,6 +60,9 @@ impl fmt::Debug for FunctionThunk {
pub enum Thunk {
Node {
node_id: i64,
/// Node-range overrides (from `CompiledThunk::Node::settings`); empty
/// for a local consumer. Resolved and applied at mint time.
settings: Vec<SubFlowSetting>,
input_schema: Option<Struct>,
output_schema: Option<Struct>,
name: Option<String>,
Expand Down
15 changes: 14 additions & 1 deletion crates/taurus-core/src/runtime/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,20 @@ impl ExecutionEngine {
},
with_trace,
);
for (input_index, value) in parameters.into_iter().enumerate() {
// Seed the positional input slots, with any override winning over the
// action-supplied value for its slot (even a null). `resolved_overrides`
// is empty for a function target, so that case is plain caller seeding
// (its overrides are applied later in `build_function_thunk_args`).
let slot_count = pending.resolved_overrides.len().max(parameters.len());
for input_index in 0..slot_count {
let value = match pending.resolved_overrides.get(input_index) {
Some(Some(override_value)) => override_value.clone(),
// No override: use the action value, or skip if it gave none.
_ => match parameters.get(input_index) {
Some(value) => value.clone(),
None => continue,
},
};
value_store.insert_input_type(
tucana::shared::InputType {
node_id: pending.caller_node_id,
Expand Down
7 changes: 7 additions & 0 deletions crates/taurus-core/src/runtime/engine/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ fn compile_node_value(
Some(sub_flow::ExecutionReference::StartingNodeId(referenced_node_id)) => {
Ok(CompiledArg::Deferred(CompiledThunk::Node {
node_id: *referenced_node_id,
// Capture overrides like the function variant below, so a
// node-range tool keeps them instead of dropping them.
settings: sub_flow
.parameters
.iter()
.map(|parameter| compile_sub_flow_setting(node_id, parameter_index, parameter))
.collect::<Result<Vec<_>, _>>()?,
input_schema: sub_flow.input_schema.clone(),
output_schema: sub_flow.output_schema.clone(),
name: sub_flow.name.clone(),
Expand Down
117 changes: 98 additions & 19 deletions crates/taurus-core/src/runtime/engine/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ impl<'a> EngineExecutor<'a> {
input_index: index as i64,
};
let value = self
.resolve_function_setting(function, setting, input_type, value_store)
.resolve_function_setting(function, setting, index, input_type, value_store)
.await?;
self.trace_record_arg(
frame_id,
Expand Down Expand Up @@ -1149,11 +1149,19 @@ impl<'a> EngineExecutor<'a> {
// function a remote runtime returned as a node result.
Argument::Thunk(Thunk::Node {
node_id,
settings,
input_schema,
output_schema,
name,
description,
}) => {
// Resolve overrides now, in the parent context (the standalone
// run's store has a null flow input). Empty for a plain consumer.
let settings = std::mem::take(settings);
let resolved_overrides = self
.resolve_overrides(&settings, index as i64, value_store)
.await
.map_err(Signal::Failure)?;
// Not executed, so left exactly as `build_args` already
// recorded it: `eager: false, executed: false`.
match self.sub_flow_registry.mint(
Expand All @@ -1164,6 +1172,7 @@ impl<'a> EngineExecutor<'a> {
Arc::clone(activity),
value_store.get_current_node_id(),
index as i64,
resolved_overrides,
) {
Some(id) => {
minted_ids.push(id.clone());
Expand Down Expand Up @@ -1196,6 +1205,15 @@ impl<'a> EngineExecutor<'a> {
// and `execute_function_standalone` -> `build_function_thunk_args`
// resolves the declared `settings` from them (see `engine.rs`).
Argument::Thunk(Thunk::Function(function)) => {
// Resolve overrides in this node's (parent) store; a returned
// function has no settings to resolve. See the node branch above.
if function.sub_flow_function.is_none() && function.resolved_overrides.is_empty() {
let settings = function.settings.clone();
function.resolved_overrides = self
.resolve_overrides(&settings, function.parameter_index, value_store)
.await
.map_err(Signal::Failure)?;
}
let returned = function.sub_flow_function.clone();
let input_schema = function.input_schema.clone();
let output_schema = function.output_schema.clone();
Expand Down Expand Up @@ -1577,6 +1595,7 @@ impl<'a> EngineExecutor<'a> {
&self,
function: &FunctionThunk,
setting: &SubFlowSetting,
index: usize,
input_type: InputType,
value_store: &mut ValueStore,
) -> Result<Value, RuntimeError> {
Expand All @@ -1585,8 +1604,26 @@ impl<'a> EngineExecutor<'a> {
paths: Vec::new(),
};

// Own the lookup result so the value_store borrow is released before we
// may re-borrow it to resolve the default.
// Minted/tool path: `resolved_overrides` holds overrides pre-resolved
// in the parent context. Rule: a present override wins over the caller
// value (even null); an absent one (`None`) lets the caller value stand
// -- there is no default to fall back on.
if !function.resolved_overrides.is_empty() {
if let Some(Some(value)) = function.resolved_overrides.get(index) {
return Ok(value.clone());
}
return match value_store.get(&reference) {
ValueStoreResult::Success(value) => Ok(value),
ValueStoreResult::Error(err) => Err(err),
ValueStoreResult::NotFound | ValueStoreResult::SubFlowFunction(_) => {
Err(missing_setting_error(function, setting))
}
};
}

// Local consumer path (`for_each`/`map`/`filter`): `default` is a
// fallback, used only when the caller value is absent or null
// (code0-tech/taurus#346).
match value_store.get(&reference) {
ValueStoreResult::Success(value) => {
if is_null_value(&value) && setting.default.is_some() {
Expand All @@ -1597,8 +1634,6 @@ impl<'a> EngineExecutor<'a> {
}
}
ValueStoreResult::Error(err) => Err(err),
// Input slots only ever hold values, so a sub flow function can't
// be found here; treat it like a missing input.
ValueStoreResult::NotFound | ValueStoreResult::SubFlowFunction(_) => {
self.resolve_setting_default(function, setting, value_store)
.await
Expand All @@ -1615,27 +1650,28 @@ impl<'a> EngineExecutor<'a> {
value_store: &mut ValueStore,
) -> Result<Value, RuntimeError> {
let Some(default) = setting.default.as_ref() else {
return Err(RuntimeError::new(
"T-CORE-000107",
"SubFlowSettingValueMissing",
format!(
"Required sub_flow setting {} for function {} is missing",
setting.identifier, function.identifier
),
));
return Err(missing_setting_error(function, setting));
};
self.resolve_override(default, function.parameter_index, value_store)
.await
}

match self.compiled_arg_to_argument(
default,
function.parameter_index as usize,
value_store,
)? {
/// Resolves a compiled override value against the current store, like a
/// node parameter (literal/reference/template/nested sub-flow). Shared by
/// the function-default path and mint-time pre-resolution.
async fn resolve_override(
&self,
default: &CompiledArg,
parameter_index: i64,
value_store: &mut ValueStore,
) -> Result<Value, RuntimeError> {
match self.compiled_arg_to_argument(default, parameter_index as usize, value_store)? {
Argument::Eval(value) => Ok(value),
Argument::Template(template) => {
self.resolve_template_value(&template, value_store, None).await
}
// A reference resolving to a sub flow function, or a nested sub
// flow default, runs to produce the default value.
// flow default, runs to produce the value.
Argument::Thunk(thunk) => match self.execute_thunk(&thunk, value_store).await.signal {
Signal::Success(value) | Signal::Return(value) => Ok(value),
Signal::Failure(err) => Err(err),
Expand All @@ -1645,6 +1681,29 @@ impl<'a> EngineExecutor<'a> {
},
}
}

/// Resolves a sub-flow's overrides in the parent context at mint time,
/// parallel to `settings`: `Some` for a configured value (even null), `None`
/// where there was none. `parameter_index` is the sub-flow's position among
/// the remote node's parameters.
async fn resolve_overrides(
&self,
settings: &[SubFlowSetting],
parameter_index: i64,
value_store: &mut ValueStore,
) -> Result<Vec<Option<Value>>, RuntimeError> {
let mut resolved = Vec::with_capacity(settings.len());
for setting in settings {
let value = match setting.default.as_ref() {
Some(default) => {
Some(self.resolve_override(default, parameter_index, value_store).await?)
}
None => None,
};
resolved.push(value);
}
Ok(resolved)
}
}

fn parameter_results_from_args(args: &[Argument]) -> Vec<NodeValueExecutionResult> {
Expand Down Expand Up @@ -1743,6 +1802,7 @@ fn returned_function_thunk(
name: Some(function.name.clone()),
description: Some(function.description.clone()),
sub_flow_function: Some(function),
resolved_overrides: Vec::new(),
}))
}

Expand Down Expand Up @@ -1839,12 +1899,14 @@ fn compiled_thunk_to_argument(thunk: &CompiledThunk) -> Thunk {
match thunk {
CompiledThunk::Node {
node_id,
settings,
input_schema,
output_schema,
name,
description,
} => Thunk::Node {
node_id: *node_id,
settings: settings.clone(),
input_schema: input_schema.clone(),
output_schema: output_schema.clone(),
name: name.clone(),
Expand All @@ -1869,6 +1931,10 @@ fn compiled_thunk_to_argument(thunk: &CompiledThunk) -> Thunk {
name: name.clone(),
description: description.clone(),
sub_flow_function: None,
// Resolved later, at mint time, if this function is handed to a
// remote action as a tool (see `resolve_remote_argument`); on the
// local path the settings' own defaults are resolved at invoke.
resolved_overrides: Vec::new(),
}),
}
}
Expand All @@ -1894,6 +1960,19 @@ fn is_null_value(value: &Value) -> bool {
matches!(value.kind.as_ref(), None | Some(Kind::NullValue(_)))
}

/// The error for a required sub-flow setting that had neither a configured
/// value/override nor a caller-supplied input.
fn missing_setting_error(function: &FunctionThunk, setting: &SubFlowSetting) -> RuntimeError {
RuntimeError::new(
"T-CORE-000107",
"SubFlowSettingValueMissing",
format!(
"Required sub_flow setting {} for function {} is missing",
setting.identifier, function.identifier
),
)
}

fn null_value() -> Value {
Value {
kind: Some(Kind::NullValue(0)),
Expand Down
5 changes: 5 additions & 0 deletions crates/taurus-core/src/runtime/engine/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ pub struct CompiledTemplateReference {
pub enum CompiledThunk {
Node {
node_id: i64,
/// Configured overrides (`SubFlow.parameters`), compiled like a
/// function's `settings`. Empty for a plain node-range consumer; for a
/// tool, resolved in the parent context at mint time and applied by
/// seeding the sub-flow's input slots (`execute_sub_flow`).
settings: Vec<SubFlowSetting>,
/// Declared shape of the sub-flow's input/output, carried from
/// `shared::SubFlow` so a remote action call that mints this
/// reference into a wire-format `ActionNodeSubFlowValue` can attach
Expand Down
Loading