fabro/lib/components/fabro-petri/tests/runs.rs
Scott Werner a1a98c69d0 fix: address review of the in-process sandbox providers
- Keep plugin-era Daytona lease fingerprints: read only DAYTONA_API_URL and
  DAYTONA_ORGANIZATION_ID (no URL alias, no placement target), and stop
  forwarding DAYTONA_SERVER_URL and DAYTONA_TARGET to the worker.
- Take the Docker fingerprint and network from this process's DOCKER_HOST,
  the endpoint the Docker client actually connects to; make the provider
  configuration's fields private.
- Return an error instead of panicking when Petri supplies no Host registry.
- Run deletion reads the Daytona key only for a Daytona run, and a forced
  or restarted delete goes on when the secret store fails, as it does for
  every other prune failure.
- Stop putting DAYTONA_API_KEY in the worker's environment; the worker reads
  it from the vault. Give the worker's Daytona client the shared HTTP client.
- Fork, rewind and retry no longer read the vault: a fork acquires no sandbox.
- Remove the dead worker plugin forwarding and document that runs execute
  only on the built-in providers.
- Build every Petri runtime through providers::standard_runtime or
  bare_runtime, with a Clippy lint against Runtime::standard/bare.
- Share the Docker require-or-skip policy in fabro-test, tighten the Host
  scope assertion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 12:31:37 -04:00

154 lines
5.7 KiB
Rust

//! A Fabro workflow runs through Petri from this crate: the `hello` bundle in
//! memory on the stub registry, and a command-only workflow on the host
//! sandbox through the real step registry.
//!
//! Built-in Host scopes run in process without a plugin executable.
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use fabro_petri::providers::{self, SandboxProviderConfig};
use petri_execution::host::{self, HostRun};
use petri_execution::inspect::{self, RunInspection};
use petri_frontend_fabro::Fabro;
use petri_runtime::executor::Retention;
use petri_runtime::frontend::CompileInputs;
use petri_runtime::ir::RunStatus;
use petri_runtime::{RunOptions, Runtime};
use petri_store::{Access, MemoryRunStore, RunKey, RunStore as _};
use tokio::fs;
/// A command-only workflow: one script stage between start and exit.
const COMMAND_WORKFLOW: &str = r#"digraph Command {
graph [goal="Run one command"]
start [shape=Mdiamond]
exit [shape=Msquare]
say [shape=parallelogram, script="echo hello from petri"]
start -> say -> exit
}"#;
const COMMAND_SETTINGS: &str = "_version = 1\n\n[workflow]\ngraph = \"workflow.fabro\"\n";
/// The `.fabro/workflows/hello` bundle checked into this repository.
fn hello_bundle() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../.fabro/workflows/hello")
}
/// Write a bundle's files into `<root>/.fabro/workflows/<name>` so the
/// frontend sees a bundle root of its own, with no project settings layer
/// above it. Returns the workflow file.
async fn install_bundle(root: &Path, name: &str, files: &[(&str, &str)]) -> PathBuf {
let bundle = root.join(".fabro").join("workflows").join(name);
fs::create_dir_all(&bundle)
.await
.expect("the bundle directory is creatable");
for (file, text) in files {
fs::write(bundle.join(file), text)
.await
.expect("the bundle file is writable");
}
bundle.join("workflow.fabro")
}
fn run_options(run_dir: &Path, key: &str) -> RunOptions {
let mut options = RunOptions::new(run_dir);
options.grace = Duration::from_secs(2);
options.retention = Retention::Never;
options.echo = false;
options.run_key = Some(RunKey::new(key));
options
}
/// Lower `workflow`, run it to completion, and inspect the run through its
/// store.
async fn run_workflow(
rt: &Runtime,
store: &MemoryRunStore,
key: &str,
workflow: &Path,
) -> RunInspection {
let lowered = rt
.check(workflow, None, None, &CompileInputs::new())
.expect("the workflow file loads");
let graph = lowered
.graph
.unwrap_or_else(|| panic!("the workflow lowers: {:?}", lowered.diagnostics));
let host_run = HostRun::new(graph).with_children(lowered.children);
let report = host::run_configured(rt, host_run, |_, _| {})
.await
.expect("the run completes");
assert_eq!(
report.status,
RunStatus::Success,
"errors: {:?}; history: {:#?}",
report.state.errors(),
report.state.history()
);
let logs = store
.open(&RunKey::new(key), Access::Read)
.await
.expect("the run opens for reading");
inspect::inspect_run(&*logs)
.await
.expect("the stored run inspects")
}
/// The `hello` bundle, whose one stage is a prompt, completes on the stub
/// registry with no model, and its record in the memory store says so. The
/// stubbed stages never run a command, but the run still takes its host
/// scope through the in-process provider.
#[tokio::test]
async fn the_hello_bundle_runs_in_memory_on_the_stub_registry() {
let root = tempfile::tempdir().expect("a temp dir");
let bundle = hello_bundle();
let workflow_text = fs::read_to_string(bundle.join("workflow.fabro"))
.await
.expect("the hello workflow is checked in");
let settings_text = fs::read_to_string(bundle.join("workflow.toml"))
.await
.expect("the hello settings are checked in");
let workflow = install_bundle(root.path(), "hello", &[
("workflow.fabro", &workflow_text),
("workflow.toml", &settings_text),
])
.await;
let store = Arc::new(MemoryRunStore::new());
let rt = petri_attractor_steps::register_stubs(
providers::standard_runtime(&SandboxProviderConfig::default()).frontend(Fabro::new()),
)
.store(store.clone())
.options(run_options(&root.path().join("run"), "hello"));
let inspection = run_workflow(&rt, &store, "hello", &workflow).await;
assert!(inspection.complete, "{:?}", inspection.incomplete);
assert_eq!(inspection.status.as_deref(), Some("success"));
assert_eq!(inspection.run_key, RunKey::new("hello"));
assert_eq!(inspection.executions.len(), 1);
}
/// A command-only workflow runs its script on the host sandbox through the
/// real step registry, and its record in the memory store says so.
#[tokio::test]
async fn a_command_workflow_runs_on_the_host_sandbox() {
let root = tempfile::tempdir().expect("a temp dir");
let workflow = install_bundle(root.path(), "command", &[
("workflow.fabro", COMMAND_WORKFLOW),
("workflow.toml", COMMAND_SETTINGS),
])
.await;
let store = Arc::new(MemoryRunStore::new());
let rt = petri_attractor_steps::register(
providers::standard_runtime(&SandboxProviderConfig::default()).frontend(Fabro::new()),
)
.store(store.clone())
.options(run_options(&root.path().join("run"), "command"));
let inspection = run_workflow(&rt, &store, "command", &workflow).await;
assert!(inspection.complete, "{:?}", inspection.incomplete);
assert_eq!(inspection.status.as_deref(), Some("success"));
assert_eq!(inspection.run_key, RunKey::new("command"));
assert_eq!(inspection.executions.len(), 1);
}