Skip to content
Closed
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
14 changes: 7 additions & 7 deletions crates/nemoclaw-authoring/examples/print_journey_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let capabilities = Capabilities::from_catalog(&catalog);

let minimum = PartialDocument::from_yaml(
b"apiVersion: nemoclaw.nvidia.com/v1alpha1\nkind: NemoClawConfig\nspec:\n sandboxes:\n - {}\n",
b"apiVersion: nemoclaw.nvidia.com/v1alpha2\nkind: NemoClawConfig\nspec:\n sandboxes:\n assistant: {}\n",
)?;
let minimum = JourneyDefinition::new("minimum viable values", minimum)
.ask(["/metadata/name"])
Expand All @@ -41,19 +41,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut guided_values: serde_json::Value =
serde_saphyr::from_slice(include_bytes!("../../../examples/onboarding/openclaw.yaml"))?;
guided_values
.pointer_mut("/spec/sandboxes/0/agent/inference/routes/0/overrides")
.pointer_mut("/spec/sandboxes/assistant/agent/inference/routes/0/overrides")
.and_then(serde_json::Value::as_object_mut)
.expect("bundled example has one model route")
.remove("model");
let guided = PartialDocument::from_yaml(guided_values.to_string().as_bytes())?;
let guided = JourneyDefinition::new("guided preview", guided)
.ask([
"/metadata/name",
"/spec/sandboxes/0/harness/kind",
"/spec/gateway/runtime/provider",
"/spec/inferenceProviders/0/provider",
"/spec/inferenceProviders/0/api",
"/spec/sandboxes/0/agent/inference/routes/0/overrides/model",
"/spec/sandboxes/assistant/harness/kind",
"/spec/gateway/target",
"/spec/inferenceProviders/nvidia-prod/provider",
"/spec/inferenceProviders/nvidia-prod/api",
"/spec/sandboxes/assistant/agent/inference/routes/0/overrides/model",
])
.omit([JourneyScope::ActiveAdapterSettings]);
println!();
Expand Down
138 changes: 100 additions & 38 deletions crates/nemoclaw-authoring/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use crate::{
Diagnostics,
diagnostics::diagnostic,
journey_definition::sandbox_path,
pointer::{escape, mask_entry_keys, replace_entry_keys},
sdk_schema::{finite_choices, sdk_field_schema_for},
settings::SettingQuestion,
};
Expand All @@ -17,45 +19,61 @@ pub(crate) fn deployment_questions_for_values(
active_routes: Option<&str>,
) -> Result<Vec<SettingQuestion>, Diagnostics> {
let mut questions = Vec::new();
collect(values, values, "", &mut questions, 0)?;
let sandbox = sandbox_path(values);
collect(values, values, "", sandbox.as_deref(), &mut questions, 0)?;
questions.retain(|question| {
(!question.path.contains("/execution/")
let fields = fields(&question.path);
(!fields.contains("/execution/")
|| active_harness
.is_some_and(|base| question.path.starts_with(&format!("{base}/execution/"))))
&& (!question.path.contains("/routes/")
&& (!fields.contains("/routes/")
|| active_routes.is_some_and(|base| question.path.starts_with(&format!("{base}/"))))
});
Ok(questions)
}

fn escaped(part: &str) -> String {
part.replace('~', "~0").replace('/', "~1")
/// `path` with the names of sandboxes, services, harnesses, and other named
/// definitions replaced by `*`, so a check for a field never matches a name.
fn fields(path: &str) -> String {
replace_entry_keys(path, |_| "*".into())
}

/// `path` relative to `sandbox`, the pointer to the journey's only sandbox, or
/// `None` when `path` is outside that sandbox.
fn in_sandbox<'a>(path: &'a str, sandbox: Option<&str>) -> Option<&'a str> {
path.strip_prefix(sandbox?)
.filter(|rest| rest.is_empty() || rest.starts_with('/'))
}

// These are deployment-owned editing regions, not a second description of their fields
// or constraints. Native settings and model/provider choices have separate owner views.
fn region(path: &str) -> bool {
fn region(path: &str, sandbox: Option<&str>) -> bool {
let fields = fields(path);
path.starts_with("/spec/gateway/")
|| path.starts_with("/spec/services/")
|| path.starts_with("/spec/sandboxes/0/image/")
|| path.starts_with("/spec/sandboxes/0/network/")
|| path.starts_with("/spec/sandboxes/0/agent/auth/")
|| path.starts_with("/spec/sandboxes/0/harness/execution/")
|| (path.contains("/routes/") && path.ends_with("/overrides/maxTokens"))
|| (path.contains("/harnesses/") && path.contains("/execution/"))
|| in_sandbox(path, sandbox).is_some_and(|rest| {
rest.starts_with("/image/")
|| rest.starts_with("/network/")
|| rest.starts_with("/agent/auth/")
|| rest.starts_with("/harness/execution/")
})
|| (fields.contains("/routes/") && fields.ends_with("/overrides/maxTokens"))
|| (fields.contains("/harnesses/") && fields.contains("/execution/"))
}
fn excluded(path: &str) -> bool {
path.ends_with("/management")
|| (path.starts_with("/spec/services/") && path.ends_with("/kind"))
let fields = fields(path);
fields.ends_with("/management")
|| (path.starts_with("/spec/services/") && fields.ends_with("/kind"))
}
fn complex(path: &str) -> bool {
(path.starts_with("/spec/services/") && path.ends_with("/recipe"))
|| path == "/spec/sandboxes/0/network/policy"
fn complex(path: &str, sandbox: Option<&str>) -> bool {
(path.starts_with("/spec/services/") && fields(path).ends_with("/recipe"))
|| in_sandbox(path, sandbox) == Some("/network/policy")
}
fn collect(
root: &Value,
value: &Value,
path: &str,
sandbox: Option<&str>,
out: &mut Vec<SettingQuestion>,
depth: usize,
) -> Result<(), Diagnostics> {
Expand All @@ -65,12 +83,12 @@ fn collect(
"Deployment question depth exceeded.",
));
}
if value.is_null() || !interested(path) {
if value.is_null() || !interested(path, sandbox) {
return Ok(());
}
if region(path)
if region(path, sandbox)
&& !excluded(path)
&& (!value.is_object() && !value.is_array() || complex(path) || value.is_array())
&& (!value.is_object() && !value.is_array() || complex(path, sandbox) || value.is_array())
{
let Some((schema, required)) = sdk_field_schema_for(root, path) else {
// A `false` schema accepts no value, so the only answer is to omit
Expand Down Expand Up @@ -109,14 +127,22 @@ fn collect(
collect(
root,
child,
&format!("{path}/{}", escaped(key)),
&format!("{path}/{}", escape(key)),
sandbox,
out,
depth + 1,
)?;
}
} else if let Some(array) = value.as_array() {
for (index, child) in array.iter().enumerate() {
collect(root, child, &format!("{path}/{index}"), out, depth + 1)?;
collect(
root,
child,
&format!("{path}/{index}"),
sandbox,
out,
depth + 1,
)?;
}
}
Ok(())
Expand All @@ -135,6 +161,7 @@ fn title(path: &str) -> String {
}
result
}
let path = mask_entry_keys(path);
let parts: Vec<_> = path.split('/').filter(|part| !part.is_empty()).collect();
let (prefix, rest) = if parts.get(1) == Some(&"gateway") {
("Gateway".to_owned(), &parts[2..])
Expand All @@ -158,25 +185,60 @@ fn title(path: &str) -> String {
)
}

fn interested(path: &str) -> bool {
[
fn interested(path: &str, sandbox: Option<&str>) -> bool {
let fields = fields(path);
// A path is inside a root or an ancestor that leads to one.
let near = |root: &str| {
path == root
|| path.starts_with(&format!("{root}/"))
|| root.starts_with(&format!("{path}/"))
};
([
"/spec/gateway",
"/spec/services",
"/spec/sandboxes/0/image",
"/spec/sandboxes/0/network",
"/spec/sandboxes/0/agent/auth",
"/spec/sandboxes/0/harness/execution",
"/spec/sandboxes/0/agent/inference",
"/spec/inferences",
"/spec/sandboxes/0/inferences",
"/spec/harnesses",
"/spec/sandboxes/0/harnesses",
]
.iter()
.any(|root| {
path == *root
|| path.starts_with(&format!("{root}/"))
|| root.starts_with(&format!("{path}/"))
}) && !path.contains("/settings")
&& !path.contains("/config")
.into_iter()
.any(near)
|| sandbox.is_some_and(|sandbox| {
[
"/image",
"/network",
"/agent/auth",
"/harness/execution",
"/agent/inference",
"/inferences",
"/harnesses",
]
.into_iter()
.any(|region| near(&format!("{sandbox}{region}")))
}))
&& !fields.contains("/settings")
&& !fields.contains("/config")
}

#[cfg(test)]
mod tests {
use super::title;

#[test]
fn a_title_shows_a_definition_key_only_when_it_is_a_name() {
for (path, expected) in [
(
"/spec/services/qwen/serving/port",
"Service qwen: serving / port",
),
(
"/spec/services/PRIVATE INVALID NAME/image",
"Service [entry]: image",
),
(
"/spec/harnesses/PRIVATE INVALID NAME/execution/timeoutSeconds",
"Deployment: harnesses / [entry] / execution / timeout Seconds",
),
] {
assert_eq!(title(path), expected);
}
}
}
4 changes: 2 additions & 2 deletions crates/nemoclaw-authoring/src/discovery_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn inference_request_for_document(
document: &Document,
route_name: Option<&str>,
) -> Result<Option<EndpointRequest>, Diagnostics> {
let [sandbox] = document.spec.sandboxes.as_slice() else {
let Some(sandbox) = crate::journey_definition::sole_sandbox(document) else {
return Err(crate::diagnostics::diagnostic(
"sandbox",
"Inference discovery requires one sandbox.",
Expand All @@ -33,7 +33,7 @@ pub fn inference_request_for_document(
let provider = document
.sandbox_route_provider(sandbox, route)
.map_err(|error| crate::diagnostics::diagnostic("provider", &error.to_string()))?;
EndpointRequest::for_external_provider(provider)
EndpointRequest::for_external_provider(provider.definition)
.map_err(|error| crate::diagnostics::diagnostic("provider", &error.to_string()))
}

Expand Down
88 changes: 63 additions & 25 deletions crates/nemoclaw-authoring/src/journey_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,49 @@

//! Sparse values and deliberate question guidance for one onboarding journey.

use nemoclaw_sdk::config::{Document, valid_name};
use serde_json::Value;
use std::collections::BTreeSet;

use crate::{
Capabilities, Diagnostics, PartialDocument,
diagnostics::diagnostic,
pointer::entry_key,
sdk_schema::{sdk_field_possible, sdk_field_schema_for},
};

pub(crate) const NAME: &str = "/metadata/name";
pub(crate) const HARNESS: &str = "/spec/sandboxes/0/harness/kind";
pub(crate) const SETTINGS: &str = "/spec/sandboxes/0/harness/settings";
pub(crate) const INFERENCE_PRESET: &str = "inference:preset";
/// The journey edits exactly one sandbox, which its key names.
pub(crate) const ONE_SANDBOX: &str =
"The v1 journey requires exactly one sandbox keyed by a lowercase name.";

/// The pointer to the journey's only sandbox, `/spec/sandboxes/<name>`, when
/// `/spec/sandboxes` holds exactly one object and its key is a valid name.
/// Questions address the sandbox by this key, so a key the SDK rejects never
/// becomes part of a question ID.
pub(crate) fn sandbox_path(values: &Value) -> Option<String> {
let sandboxes = values.pointer("/spec/sandboxes")?.as_object()?;
let [(name, sandbox)] = sandboxes.iter().collect::<Vec<_>>()[..] else {
return None;
};
(sandbox.is_object() && valid_name(name)).then(|| format!("/spec/sandboxes/{name}"))
}

/// The pointer to the inline harness kind of the journey's only sandbox,
/// which is also the ID of the harness question.
pub(crate) fn harness_field(values: &Value) -> Option<String> {
sandbox_path(values).map(|sandbox| format!("{sandbox}/harness/kind"))
}

/// The name of a one-sandbox document's sandbox; `None` for any other count.
pub(crate) fn sole_sandbox(document: &Document) -> Option<&str> {
let mut names = document.spec.sandboxes.keys();
match (names.next(), names.next()) {
(Some(name), None) => Some(name),
_ => None,
}
}

/// A schema-discovered family of applicable questions. The schema determines
/// which fields exist and whether they are required; this only asks to review
Expand Down Expand Up @@ -134,19 +164,10 @@ impl JourneyDefinition {

/// Start mutable resolution over the sparse v1 single-sandbox envelope.
pub fn start(&self, capabilities: &Capabilities) -> Result<crate::JourneyState, Diagnostics> {
self.validate_guidance(capabilities)?;
if !self
.base
.supplied()
.pointer("/spec/sandboxes")
.and_then(Value::as_array)
.is_some_and(|sandboxes| sandboxes.len() == 1 && sandboxes[0].is_object())
{
return Err(diagnostic(
"journey",
"The v1 journey requires exactly one sandbox object.",
));
if sandbox_path(self.base.supplied()).is_none() {
return Err(diagnostic("journey", ONE_SANDBOX));
}
self.validate_guidance(capabilities)?;
crate::JourneyState::new(self.clone())
}

Expand All @@ -173,10 +194,33 @@ impl JourneyDefinition {
&format!("scope {scope:?} cannot be omitted in this preview"),
));
}
let supplied = self.base.supplied();
let sandbox = sandbox_path(supplied);
let harness = harness_field(supplied);
for field in &self.ask {
if field == NAME || field == HARNESS || field == INFERENCE_PRESET {
if field == NAME || Some(field) == harness.as_ref() || field == INFERENCE_PRESET {
continue;
}
// A field of another sandbox or of an absent provider would add
// that entry when answered.
if let Some(key) = entry_key(field, "/spec/sandboxes")
&& sandbox.as_ref() != Some(&format!("/spec/sandboxes/{key}"))
{
return Err(diagnostic(
"journey",
"asked fields must belong to the journey's sandbox",
));
}
if let Some(key) = entry_key(field, "/spec/inferenceProviders")
&& supplied
.pointer(&format!("/spec/inferenceProviders/{key}"))
.is_none()
{
return Err(diagnostic(
"journey",
"asked provider fields must belong to a supplied provider",
));
}
if sdk_field_schema_for(self.base.supplied(), field).is_some()
|| sdk_field_possible(field)
{
Expand Down Expand Up @@ -231,16 +275,10 @@ impl JourneyDefinition {
"required adapter settings alternatives cannot be omitted",
));
}
if self
.base
.supplied()
.pointer(HARNESS)
.and_then(Value::as_str)
== Some(adapter)
&& self
.base
.supplied()
.pointer(&format!("{SETTINGS}{pointer}"))
if let (Some(sandbox), Some(harness)) = (&sandbox, &harness)
&& supplied.pointer(harness).and_then(Value::as_str) == Some(adapter)
&& supplied
.pointer(&format!("{sandbox}/harness/settings{pointer}"))
.is_some()
{
return Err(diagnostic(
Expand Down
Loading