mod agent;
mod fs_util;
mod gate;
mod hybrid;
mod isolate;
mod resolution;
mod run_md;
mod scope;
mod sections;
mod settings;
mod split;
mod support;
mod types;
pub use resolution::resolve_node_capabilities;
pub use types::{
ContextInjection, ContextInjectionMode, ContextInjectionReport, HybridRealization,
RenderFlavor, DEFAULT_CONTEXT_INJECTION, DEFAULT_RENDER_FLAVOR,
};
use crate::error::DispatchError;
use crate::interactive::{
native_analysis_prompt_fragment, native_analysis_terminal_presentation_instructions,
native_answer_persistence_instructions, native_context_enrichment_prompt_fragment,
native_context_enrichment_terminal_presentation_instructions,
native_dashboard_save_instructions, prepare_interactive_output,
setup_bootstrap_authority_instructions, setup_recovery_instructions, NativeMcpDescriptor,
NativePurpose, NativeSessionScope,
};
use crate::ir::{validate_ir_version, WarbleIr};
use crate::models::{ModelConfig, ANTHROPIC_PROVIDER};
use crate::provider::{compose_target, ProviderFragment, ToolMap};
use crate::resolve::ResolutionReport;
use crate::targets::{CapabilityOutcome, TargetId};
use std::path::Path;
use agent::build_agent_markdown;
use fs_util::{mkdir_all, write_file, write_json};
use hybrid::{
any_local_provider, build_hybrid_disclosure, build_hybrid_run_md, emit_hybrid_file_target,
emit_hybrid_file_target_mcp,
};
use isolate::{
build_isolated_child_markdown, build_isolating_parent_markdown, isolated_agent_name,
should_isolate,
};
use resolution::{print_resolution_summary, resolve_node_with_shared_binding};
use run_md::{build_interactive_run_md, build_profile_run_md};
use scope::{build_scope_prompt, merge_scope_settings, scope_denies_destructive_bash};
use settings::{build_settings, wren_config};
use split::{
build_driver_markdown, build_split_settings, build_subagent_markdown,
should_split_per_step_tier, subagent_name,
};
use support::{outcome_supported, realization_supported, trigger_supported, unsupported};
fn is_native_dashboard_component(node: &crate::ir::ComponentNode) -> bool {
node.required_capabilities
.iter()
.any(|capability| capability == "genbi_build")
&& node
.required_capabilities
.iter()
.any(|capability| capability == "artifact_write")
}
fn is_native_persisted_answer_component(node: &crate::ir::ComponentNode) -> bool {
let blocks = &node.effect.render_blocks;
let table = blocks
.iter()
.filter(|block| block.block_type == "table")
.count();
let definition = blocks
.iter()
.filter(|block| block.block_type == "definition")
.count();
table == 1
&& definition <= 1
&& blocks.len() == table + definition
&& blocks
.iter()
.find(|block| block.block_type == "table")
.is_some_and(|block| {
block
.fields
.get("columns")
.is_some_and(|field| field == "string[]")
&& block
.fields
.get("rows")
.is_some_and(|field| field == "row[]")
})
&& blocks
.iter()
.filter(|block| block.block_type == "definition")
.all(|block| {
block
.fields
.get("sql")
.is_some_and(|field| field == "string")
&& block
.fields
.get("source_tables")
.is_some_and(|field| field == "string[]")
&& block
.fields
.get("filters")
.is_some_and(|field| field == "string[]")
})
}
fn native_setup_settings(
mut settings: serde_json::Value,
purpose: Option<NativePurpose>,
native_scope: Option<&NativeSessionScope>,
) -> Result<serde_json::Value, DispatchError> {
if purpose != Some(NativePurpose::Setup) {
return Ok(settings);
}
let scope = native_scope.ok_or_else(|| {
DispatchError("Setup permissions require a server-derived native scope".to_string())
})?;
let scoped_permissions = scope.claude_setup_write_permissions()?;
let allow = settings
.pointer_mut("/permissions/allow")
.and_then(serde_json::Value::as_array_mut)
.ok_or_else(|| DispatchError("native Setup permissions are incompatible".to_string()))?;
allow.retain(|entry| !matches!(entry.as_str(), Some("Edit" | "Write")));
for permission in scoped_permissions {
allow.push(serde_json::Value::String(permission));
}
let note = "Setup write authority is server-sealed: only Edit/Write paths below the host-provided WARBLE_SETUP_BOOTSTRAP_ROOT are allowed; the native cwd is private and read-only.";
match settings.get_mut("$comment") {
Some(comment) => {
let existing = comment.as_str().ok_or_else(|| {
DispatchError("native Setup permissions are incompatible".to_string())
})?;
*comment = serde_json::Value::String(format!("{existing} {note}"));
}
None => {
settings["$comment"] = serde_json::Value::String(note.to_string());
}
}
Ok(settings)
}
pub fn emit_claude_code(
ir: &WarbleIr,
out_dir: &Path,
target_id: &str,
render_flavor: RenderFlavor,
) -> Result<(), DispatchError> {
emit_claude_code_with_models(
ir,
out_dir,
target_id,
render_flavor,
&ModelConfig::default(),
)
}
fn require_per_step_provider_support(
ir: &WarbleIr,
target_id: &str,
models: &ModelConfig,
) -> Result<(), DispatchError> {
let supported = TargetId::parse(target_id)
.map(|t| t.profile())
.and_then(|p| p.get("llm:per_step_provider").map(|e| e.outcome))
.is_some_and(|outcome| outcome != CapabilityOutcome::Fail);
if supported {
return Ok(());
}
for node in &ir.components {
for call in &node.llm_calls {
let binding = models.binding(&call.tier)?;
if binding.provider != ANTHROPIC_PROVIDER {
return Err(DispatchError(format!(
"llm:per_step_provider: fail on {target_id} — the binding routes step '{}.{}' \
(tier '{}') to provider '{}', but this target is whole-session single-provider and does not support \
per-step provider routing (hybrid). Use an all-cloud binding for this target, or dispatch to a target \
that realizes llm:per_step_provider.",
node.verb,
call.name,
call.tier,
binding.provider.as_str()
)));
}
}
}
Ok(())
}
pub fn emit_claude_code_with_models(
ir: &WarbleIr,
out_dir: &Path,
target_id: &str,
render_flavor: RenderFlavor,
models: &ModelConfig,
) -> Result<(), DispatchError> {
emit_claude_code_with_realization(
ir,
out_dir,
target_id,
render_flavor,
models,
HybridRealization::default(),
)
}
pub fn emit_claude_code_with_realization(
ir: &WarbleIr,
out_dir: &Path,
target_id: &str,
render_flavor: RenderFlavor,
models: &ModelConfig,
hybrid: HybridRealization,
) -> Result<(), DispatchError> {
let context = ContextInjection::from_ir(ir, DEFAULT_CONTEXT_INJECTION, None);
emit_claude_code_with_context(
ir,
out_dir,
target_id,
render_flavor,
models,
hybrid,
&context,
)
}
#[allow(clippy::too_many_arguments)]
pub fn emit_claude_code_with_context(
ir: &WarbleIr,
out_dir: &Path,
target_id: &str,
render_flavor: RenderFlavor,
models: &ModelConfig,
hybrid: HybridRealization,
context: &ContextInjection,
) -> Result<(), DispatchError> {
emit_claude_code_with_providers(
ir,
out_dir,
target_id,
render_flavor,
models,
hybrid,
context,
&[],
)
}
#[allow(clippy::too_many_arguments)]
pub fn emit_claude_code_with_providers(
ir: &WarbleIr,
out_dir: &Path,
target_id: &str,
render_flavor: RenderFlavor,
models: &ModelConfig,
hybrid: HybridRealization,
context: &ContextInjection,
providers: &[ProviderFragment],
) -> Result<(), DispatchError> {
emit_claude_code_with_native_purpose(
ir,
out_dir,
target_id,
render_flavor,
models,
hybrid,
context,
providers,
None,
None,
None,
)
}
#[allow(clippy::too_many_arguments)]
pub fn emit_claude_code_with_native_purpose(
ir: &WarbleIr,
out_dir: &Path,
target_id: &str,
render_flavor: RenderFlavor,
models: &ModelConfig,
hybrid: HybridRealization,
context: &ContextInjection,
providers: &[ProviderFragment],
purpose: Option<NativePurpose>,
native_scope: Option<NativeSessionScope>,
native_mcp: Option<NativeMcpDescriptor>,
) -> Result<(), DispatchError> {
validate_ir_version(ir)?;
if let Some(purpose) = purpose {
if target_id != "claude-code:interactive" {
return Err(DispatchError(
"--purpose is supported only by native interactive targets".to_string(),
));
}
purpose.validate_profile(ir)?;
}
if native_mcp.is_some() && purpose.is_none() {
return Err(DispatchError(
"--native-mcp requires a native Sessions --purpose".to_string(),
));
}
let include_setup_recovery_instructions =
purpose == Some(NativePurpose::Setup) && native_mcp.is_some();
if target_id == "codex:interactive" {
return Err(DispatchError("codex:interactive must use the native Codex materializer, never the Claude file emitter".to_string()));
}
let filtered_ir;
let ir = if target_id == "claude-code:interactive" {
filtered_ir = WarbleIr {
components: ir
.components
.iter()
.filter(|node| {
!node
.required_capabilities
.iter()
.any(|capability| capability == "enrichment_apply:deterministic")
})
.cloned()
.collect(),
..ir.clone()
};
if filtered_ir.components.is_empty() {
return Err(DispatchError("apply_enrichment cannot be dispatched on claude-code:interactive: native materialization has no enforceable human-approval apply channel (wall-hit)".to_string()));
}
&filtered_ir
} else {
ir
};
models.validate(ir)?;
if ir
.components
.iter()
.any(|n| should_split_per_step_tier(n) || should_isolate(n))
{
models.orchestrator()?;
}
require_per_step_provider_support(ir, target_id, models)?;
for node in &ir.components {
if !realization_supported(node.realization_kind) {
return Err(unsupported(
"realization_kind",
node.realization_kind.as_str(),
));
}
if !trigger_supported(node.trigger.kind) {
return Err(unsupported("trigger.kind", node.trigger.kind.as_str()));
}
if !outcome_supported(node.effect.outcome.kind) {
return Err(unsupported(
"outcome.kind",
node.effect.outcome.kind.as_str(),
));
}
}
let target = TargetId::parse(target_id).ok_or_else(|| {
DispatchError(format!(
"target '{target_id}' has no capability profile (known targets: {})",
crate::targets::known_target_names().join(", ")
))
})?;
let composed = compose_target(target.profile(), ToolMap::new(), providers, target)?;
let tool_map = &composed.tool_map;
let mut reports: Vec<(String, ResolutionReport)> = Vec::with_capacity(ir.components.len());
for node in &ir.components {
reports.push((
node.id.clone(),
resolve_node_with_shared_binding(
node,
&ir.context_binding,
target_id,
&composed.profile,
)?,
));
}
let report_for = |id: &str| -> &ResolutionReport {
&reports
.iter()
.find(|(nid, _)| nid == id)
.expect("report exists")
.1
};
if any_local_provider(ir, models)? {
if native_mcp.is_some() {
return Err(DispatchError(
"native MCP discovery requires an all-cloud native interactive materialization; hybrid MCP configuration is a separate producer contract"
.to_string(),
));
}
let scope_components = ir
.components
.iter()
.map(|node| (node, report_for(&node.id)))
.collect::<Vec<_>>();
let disclosure = build_hybrid_disclosure(ir, models, hybrid)?;
let scope_prompt = format!(
"{}{}",
build_scope_prompt(ir, &scope_components, render_flavor, true),
disclosure
);
let run = build_hybrid_run_md(ir, target_id, &disclosure);
match hybrid {
HybridRealization::BashScript => {
emit_hybrid_file_target(ir, out_dir, target_id, models, context)?
}
HybridRealization::McpServer => {
emit_hybrid_file_target_mcp(ir, out_dir, target_id, models, context)?
}
}
write_file(&out_dir.join(".claude/CLAUDE.md"), &scope_prompt)?;
write_file(&out_dir.join("RUN.md"), &run)?;
return Ok(());
}
let interactive = if target_id == "claude-code:interactive" {
let signature = ir
.components
.iter()
.map(|node| node.id.as_str())
.collect::<Vec<_>>()
.join(",");
let mut paths = vec![
std::path::PathBuf::from("RUN.md"),
std::path::PathBuf::from("context-report.json"),
std::path::PathBuf::from("capability-report.json"),
std::path::PathBuf::from(".claude/CLAUDE.md"),
std::path::PathBuf::from(".claude/settings.json"),
std::path::PathBuf::from(".wren/config.json"),
];
if native_mcp.is_some() {
paths.push(std::path::PathBuf::from(".mcp.json"));
}
for node in &ir.components {
paths.push(std::path::PathBuf::from(format!(
".claude/agents/{}.md",
node.verb
)));
if should_split_per_step_tier(node) {
for call in &node.llm_calls {
paths.push(std::path::PathBuf::from(format!(
".claude/agents/{}.md",
subagent_name(&node.verb, call)
)));
}
}
}
Some(prepare_interactive_output(
out_dir,
target_id,
"claude",
&signature,
&paths,
purpose,
native_scope.clone(),
native_mcp.clone(),
)?)
} else {
None
};
let out_dir = interactive
.as_ref()
.map_or(out_dir, |output| output.root.as_path());
mkdir_all(out_dir)?;
write_json(
&out_dir.join("context-report.json"),
&serde_json::to_value(context.report()).expect("context report serializes"),
)?;
let include_session_dashboard_save_tool = purpose == Some(NativePurpose::Analysis)
&& native_mcp.is_some()
&& ir.components.iter().any(is_native_dashboard_component);
let include_session_persist_answer_tool = purpose == Some(NativePurpose::Analysis)
&& native_mcp.is_some()
&& ir
.components
.iter()
.any(is_native_persisted_answer_component);
let claude_dir = out_dir.join(".claude");
let agents_dir = claude_dir.join("agents");
let wren_dir = out_dir.join(".wren");
mkdir_all(&agents_dir)?;
mkdir_all(&wren_dir)?;
let mut component_settings: Vec<(String, serde_json::Value)> =
Vec::with_capacity(ir.components.len());
for node in &ir.components {
let include_dashboard_save_tool = purpose == Some(NativePurpose::Analysis)
&& native_mcp.is_some()
&& (is_native_dashboard_component(node) || is_native_persisted_answer_component(node));
let include_persist_answer_tool = purpose == Some(NativePurpose::Analysis)
&& native_mcp.is_some()
&& is_native_persisted_answer_component(node);
let include_native_terminal_presentation = matches!(
purpose,
Some(NativePurpose::Analysis | NativePurpose::ContextEnrichment)
);
let mut rendered_node;
let node = if include_native_terminal_presentation {
rendered_node = node.clone();
rendered_node.prompt_fragment = match purpose {
Some(NativePurpose::Analysis) => {
native_analysis_prompt_fragment(&node.prompt_fragment)
}
Some(NativePurpose::ContextEnrichment) => {
native_context_enrichment_prompt_fragment(&node.prompt_fragment)
}
_ => unreachable!("native terminal presentation requires a native purpose"),
};
&rendered_node
} else {
node
};
let report = report_for(&node.id);
if should_isolate(node) {
write_file(
&agents_dir.join(format!("{}.md", node.verb)),
&build_isolating_parent_markdown(node, report, render_flavor, models)?,
)?;
write_file(
&agents_dir.join(format!("{}.md", isolated_agent_name(&node.verb))),
&build_isolated_child_markdown(
node,
report,
render_flavor,
models,
context,
tool_map,
include_setup_recovery_instructions,
include_dashboard_save_tool,
)?,
)?;
component_settings.push((
node.verb.clone(),
build_split_settings(
node,
report,
render_flavor,
tool_map,
include_session_dashboard_save_tool,
include_session_persist_answer_tool,
),
));
} else if should_split_per_step_tier(node) {
let mut driver = build_driver_markdown(
node,
report,
render_flavor,
models,
context,
include_dashboard_save_tool,
include_persist_answer_tool,
include_native_terminal_presentation,
);
if include_persist_answer_tool {
driver.push('\n');
driver.push_str(native_answer_persistence_instructions());
}
if include_dashboard_save_tool {
driver.push('\n');
driver.push_str(native_dashboard_save_instructions());
}
if include_native_terminal_presentation {
driver.push('\n');
driver.push_str(match purpose {
Some(NativePurpose::Analysis) => {
native_analysis_terminal_presentation_instructions()
}
Some(NativePurpose::ContextEnrichment) => {
native_context_enrichment_terminal_presentation_instructions()
}
_ => unreachable!("native terminal presentation requires a native purpose"),
});
}
if purpose == Some(NativePurpose::Setup) {
driver.push('\n');
driver.push_str(&setup_bootstrap_authority_instructions());
}
write_file(&agents_dir.join(format!("{}.md", node.verb)), &driver)?;
for call in &node.llm_calls {
let mut subagent = build_subagent_markdown(node, call, models, context, tool_map);
if purpose == Some(NativePurpose::Setup) {
subagent.push('\n');
subagent.push_str(&setup_bootstrap_authority_instructions());
}
write_file(
&agents_dir.join(format!("{}.md", subagent_name(&node.verb, call))),
&subagent,
)?;
}
component_settings.push((
node.verb.clone(),
build_split_settings(
node,
report,
render_flavor,
tool_map,
include_session_dashboard_save_tool,
include_session_persist_answer_tool,
),
));
} else {
let mut agent_markdown = build_agent_markdown(
node,
report,
render_flavor,
models,
context,
tool_map,
include_setup_recovery_instructions,
include_dashboard_save_tool,
)?;
if include_dashboard_save_tool {
agent_markdown.push('\n');
agent_markdown.push_str(native_dashboard_save_instructions());
}
if include_native_terminal_presentation {
agent_markdown.push('\n');
agent_markdown.push_str(match purpose {
Some(NativePurpose::Analysis) => {
native_analysis_terminal_presentation_instructions()
}
Some(NativePurpose::ContextEnrichment) => {
native_context_enrichment_terminal_presentation_instructions()
}
_ => unreachable!("native terminal presentation requires a native purpose"),
});
}
if include_setup_recovery_instructions {
agent_markdown.push('\n');
agent_markdown.push_str(setup_recovery_instructions());
}
if purpose == Some(NativePurpose::Setup) {
agent_markdown.push('\n');
agent_markdown.push_str(&setup_bootstrap_authority_instructions());
}
write_file(
&agents_dir.join(format!("{}.md", node.verb)),
&agent_markdown,
)?;
component_settings.push((
node.verb.clone(),
build_settings(
node,
report,
render_flavor,
tool_map,
include_setup_recovery_instructions,
include_session_dashboard_save_tool,
include_session_persist_answer_tool,
),
));
}
}
let scope_components = ir
.components
.iter()
.map(|node| (node, report_for(&node.id)))
.collect::<Vec<_>>();
write_json(
&claude_dir.join("settings.json"),
&native_setup_settings(
merge_scope_settings(&component_settings),
purpose,
native_scope.as_ref(),
)?,
)?;
write_json(&wren_dir.join("config.json"), &wren_config())?;
let scope_prompt = build_scope_prompt(
ir,
&scope_components,
render_flavor,
scope_denies_destructive_bash(&component_settings),
);
write_file(
&claude_dir.join("CLAUDE.md"),
&match interactive.as_ref() {
Some(output) => format!("{}\n{scope_prompt}", output.marker()),
None => scope_prompt,
},
)?;
let run = match interactive.as_ref() {
Some(output) => format!(
"{}\n{}",
output.marker(),
build_interactive_run_md(ir, purpose)
),
None => build_profile_run_md(&ir.profile, &scope_components, render_flavor, models)?,
};
write_file(&out_dir.join("RUN.md"), &run)?;
if let (Some(_), Some(descriptor)) = (&interactive, native_mcp.as_ref()) {
write_file(
&out_dir.join(".mcp.json"),
&descriptor.claude_discovery_config()?,
)?;
}
let capability_report = serde_json::json!({
"target": target_id,
"components": ir.components.iter().map(|node| {
let mut entry = serde_json::json!({
"id": node.id,
"capabilities": report_for(&node.id),
});
let granted: serde_json::Map<String, serde_json::Value> = node
.required_capabilities
.iter()
.filter_map(|c| tool_map.get(c.as_str()).map(|b| (c, b)))
.map(|(capability, binding)| {
(
capability.clone(),
serde_json::json!({ "names": binding.names, "source": binding.source }),
)
})
.collect();
if !granted.is_empty() {
entry["tool_bindings"] = serde_json::Value::Object(granted);
}
if should_isolate(node) {
let collapsed = models.collapsed_model(&node.llm_calls).ok();
entry["isolation"] = serde_json::json!({
"realized_via": "single-child-subagent",
"child_agent": isolated_agent_name(&node.verb),
"per_step_tier": if should_split_per_step_tier(node) {
serde_json::json!({
"outcome": "degrade",
"reason": "the whole component runs in one child, so its steps share one model",
"collapsed_to": collapsed,
})
} else {
serde_json::json!({ "outcome": "not-requested" })
},
});
}
entry
}).collect::<Vec<_>>(),
});
write_json(&out_dir.join("capability-report.json"), &capability_report)?;
for node in &ir.components {
print_resolution_summary(
&format!("{target_id} (component '{}')", node.id),
report_for(&node.id),
);
}
if let Some(output) = interactive {
output.write_ownership()?;
output.write_launch_spec()?;
}
Ok(())
}