Skip to content
Merged
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
12 changes: 12 additions & 0 deletions crates/tinycomputer-bus/src/agent/agent_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ fn waiting_and_continuing_have_forgiving_defaults() {
assert_eq!(resume.id.to_string(), "t-1");
assert_eq!(resume.inputs["date of birth"], "1990-04-02");
assert_eq!(resume.approve, None);

let private: ContinueTaskRequest = serde_json::from_value(json!({
"id": "t-2",
"fact_handles": {
"email": {"handle": "opaque-email", "mask": "‹email›", "kind": "asked_input"}
}
}))
.unwrap();
assert_eq!(private.fact_handles["email"].handle, "opaque-email");
let wire = serde_json::to_value(private).unwrap();
assert_eq!(wire["fact_handles"]["email"]["mask"], "‹email›");
assert_eq!(wire["inputs"], json!({}));
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions crates/tinycomputer-bus/src/agent/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ pub struct ContinueTaskRequest {
/// Values for a `needs_input` pause, by the field names it listed. Added
/// to the task's facts.
pub inputs: BTreeMap<String, String>,
/// Opaque host handles for secret values the task asked for. The value
/// remains with the host and is resolved only when the task types it.
/// A name cannot be supplied in both `inputs` and `fact_handles`.
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub fact_handles: BTreeMap<String, SecretHandle>,
/// The decision on a `needs_approval` pause, or `true` to go past a
/// non-payment `checkpoint`.
pub approve: Option<bool>,
Expand Down
4 changes: 2 additions & 2 deletions crates/tinycomputer-bus/src/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! release workflow bumps, which tracks the shipped artifact.

/// The wire contract version this crate defines.
pub const CONTRACT_VERSION: (u32, u32) = (2, 22);
pub const CONTRACT_VERSION: (u32, u32) = (2, 23);

/// Returns whether a host holding [`CONTRACT_VERSION`] can bind to a module
/// reporting `module`.
Expand All @@ -22,7 +22,7 @@ pub const CONTRACT_VERSION: (u32, u32) = (2, 22);
/// ```
/// # use tinycomputer_bus::{is_compatible, CONTRACT_VERSION};
/// assert!(is_compatible(CONTRACT_VERSION));
/// assert!(is_compatible((2, 22)));
/// assert!(is_compatible((2, 23)));
/// assert!(!is_compatible((2, 11)));
/// assert!(!is_compatible((1, 8)));
/// ```
Expand Down
6 changes: 4 additions & 2 deletions crates/tinycomputer-bus/src/version/version_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{CONTRACT_VERSION, binds, is_compatible};

#[test]
fn the_shipped_contract_version_is_pinned() {
assert_eq!(CONTRACT_VERSION, (2, 22));
assert_eq!(CONTRACT_VERSION, (2, 23));
}

#[test]
Expand All @@ -16,8 +16,9 @@ fn the_contract_binds_to_itself() {

#[test]
fn a_newer_minor_on_the_module_side_binds() {
assert!(is_compatible((2, 22)));
assert!(is_compatible((2, 23)));
assert!(is_compatible((2, 97)));
// 2.23 adds host-held fact handles to ContinueTask.
// 2.22 adds optional host-held secret handles to StartTask and RunFlow.
// A host-only handle is ignored by older modules, which then refuse it
// instead of trying to type its opaque identifier as a value.
Expand All @@ -38,6 +39,7 @@ fn a_newer_minor_on_the_module_side_binds() {
// 2.19 adds the ConsequenceHost host-callback interface (Review): a 2.19
// host serves it, which a 2.18 module does not call, so its approvals
// would never reach the host's policy.
assert!(!is_compatible((2, 22)));
assert!(!is_compatible((2, 18)));
assert!(!is_compatible((2, 17)));
assert!(!is_compatible((2, 16)));
Expand Down
15 changes: 14 additions & 1 deletion crates/tinycomputer-engine/src/task/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,24 @@ fn members() -> Vec<MemberDoc> {
),
member(
methods::CONTINUE_TASK,
"Answers a paused task: inputs for needs_input, approve for needs_approval.",
"Answers a paused task: values or host-held secret handles for needs_input, approve for needs_approval.",
object(
json!({
"id": {"type": "string"},
"inputs": {"type": "object", "additionalProperties": {"type": "string"}},
"fact_handles": {
"type": "object",
"description": "Host-held secret values by input name. Only opaque handles and safe masks cross the engine boundary.",
"additionalProperties": {
"type": "object",
"properties": {
"handle": {"type": "string"},
"mask": {"type": "string"},
"kind": {"type": "string"}
},
"required": ["handle", "mask", "kind"]
}
},
"approve": {"type": "boolean"},
"answer": {"type": "string"}
}),
Expand Down
30 changes: 28 additions & 2 deletions crates/tinycomputer-engine/src/task/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tinycomputer_bus::agent::{
AgentError, AgentResponse, ContinueTaskRequest, TaskStatus, TaskView,
};
use tinycomputer_bus::{Flow, FlowAction, FlowStep};
use tinycomputer_core::Facts;
use tinycomputer_core::{Facts, SecretFactHandle};

use super::Tasks;
use super::errors::poisoned;
Expand All @@ -27,7 +27,33 @@ impl Tasks {
return poisoned();
};
// A supplied value is secret when its name says so, like any other.
state.facts = state.facts.merged(&Facts::new(request.inputs));
// Handles stay with the host; only their opaque references enter task
// state and the model-facing request.
let supplied = match Facts::with_handles(
request.inputs,
request.fact_handles.into_iter().map(|(name, handle)| {
(
name,
SecretFactHandle {
handle: handle.handle,
mask: handle.mask,
kind: handle.kind,
},
)
}),
std::iter::empty::<String>(),
) {
Ok(facts) => facts,
Err(error) => {
return AgentResponse::err(AgentError::new(
"INVALID_SECRET_FACT",
error.to_string(),
"provide each secret input as a non-empty host handle with a unique name",
true,
));
}
};
state.facts = state.facts.merged(&supplied);
let known = known_names(&state.flow, &state.facts);
let facts = fact_names(&state.facts);
let missing = crate::agentic::missing_inputs(&state.flow, &known, &facts);
Expand Down
26 changes: 20 additions & 6 deletions crates/tinycomputer-engine/src/task/task_tests/describe_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ use tinycomputer_bus::{JevConfiguration, JevProvider};

use super::rescue_tests::Model;

fn continue_with_handle(
id: tinycomputer_bus::agent::TaskId,
) -> tinycomputer_bus::agent::ContinueTaskRequest {
tinycomputer_bus::agent::ContinueTaskRequest {
id,
approve: Some(true),
answer: Some(String::new()),
fact_handles: std::collections::BTreeMap::from([(
"email".to_owned(),
tinycomputer_bus::agent::SecretHandle {
handle: "opaque-email".to_owned(),
mask: "‹email›".to_owned(),
kind: "asked_input".to_owned(),
},
)]),
..tinycomputer_bus::agent::ContinueTaskRequest::default()
}
}

/// A decision model summary, as a configured runtime reports it.
fn jev() -> JevConfiguration {
JevConfiguration {
Expand Down Expand Up @@ -244,12 +263,7 @@ async fn describe_schemas_name_every_request_field() {
),
(
"ContinueTask",
serde_json::to_value(tinycomputer_bus::agent::ContinueTaskRequest {
id: id.clone(),
approve: Some(true),
answer: Some(String::new()),
..tinycomputer_bus::agent::ContinueTaskRequest::default()
}),
serde_json::to_value(continue_with_handle(id.clone())),
),
(
"CancelTask",
Expand Down
146 changes: 146 additions & 0 deletions crates/tinycomputer-engine/src/task/task_tests/start_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,152 @@ async fn a_value_supplied_for_a_missing_fact_still_fails_fast_if_it_leaks() {
);
}

#[tokio::test]
async fn a_missing_sensitive_value_can_be_supplied_as_a_host_handle() {
let (tasks, script) = controller(vec![finished_run(
FlowStopReason::Completed,
vec![step("1", "enter", "passport number", StepOutcome::Done, "")],
&[],
None,
)]);
let view = start(
&tasks,
json!({"app": "Mail", "steps": [
{"enter": {"passport number": "${passport number}"}}
]}),
&[],
);
assert!(matches!(view.status, TaskStatus::NeedsInput { .. }));

let supplied = tasks.continue_task(ContinueTaskRequest {
id: view.id.clone(),
fact_handles: BTreeMap::from([(
"passport number".to_owned(),
tinycomputer_bus::agent::SecretHandle {
handle: "vault-secret-handle".to_owned(),
mask: "••••567".to_owned(),
kind: "identity_document".to_owned(),
},
)]),
..ContinueTaskRequest::default()
});
assert_eq!(supplied.data.unwrap().status, TaskStatus::Running);
assert!(matches!(
settle(&tasks, &view.id).await.status,
TaskStatus::Done { .. }
));

let request = &script.requests.lock().unwrap()[0];
assert_eq!(
request.fact_handles["passport number"].handle,
"vault-secret-handle"
);
assert_eq!(request.fact_handles["passport number"].mask, "••••567");
assert!(request.facts.contains("passport number"));
assert!(!format!("{request:?}").contains("vault-secret-handle"));
}

#[tokio::test]
async fn invalid_continuation_handles_leave_existing_facts_and_pause_unchanged() {
let (tasks, script) = controller(vec![finished_run(
FlowStopReason::Completed,
vec![step(
"1",
"enter",
"passport number and email",
StepOutcome::Done,
"",
)],
&[],
None,
)]);
let view = tasks
.start(&StartTaskRequest {
flow: Some(flow(json!({"app": "Mail", "steps": [
{"enter": {
"passport number": "${passport number}",
"email": "${email}"
}}
]}))),
fact_handles: BTreeMap::from([(
"passport number".to_owned(),
tinycomputer_bus::agent::SecretHandle {
handle: "existing-vault-handle".to_owned(),
mask: "••••567".to_owned(),
kind: "identity_document".to_owned(),
},
)]),
..StartTaskRequest::default()
})
.data
.unwrap();
assert!(matches!(view.status, TaskStatus::NeedsInput { .. }));

for invalid in [
ContinueTaskRequest {
fact_handles: BTreeMap::from([(
"email".to_owned(),
tinycomputer_bus::agent::SecretHandle::default(),
)]),
..ContinueTaskRequest::default()
},
ContinueTaskRequest {
inputs: BTreeMap::from([("email".to_owned(), "person@example.test".to_owned())]),
fact_handles: BTreeMap::from([(
"email".to_owned(),
tinycomputer_bus::agent::SecretHandle {
handle: "opaque".to_owned(),
mask: "‹email›".to_owned(),
kind: "input".to_owned(),
},
)]),
..ContinueTaskRequest::default()
},
] {
let reply = tasks.continue_task(ContinueTaskRequest {
id: view.id.clone(),
..invalid
});
assert_eq!(code(&reply), "INVALID_SECRET_FACT");
let current = tasks
.list()
.data
.unwrap()
.into_iter()
.find(|task| task.id == view.id)
.unwrap();
assert!(matches!(
current.status,
TaskStatus::NeedsInput { ref fields } if fields.len() == 1 && fields[0].name == "email"
));
assert!(script.requests.lock().unwrap().is_empty());
}

let continued = tasks.continue_task(ContinueTaskRequest {
id: view.id.clone(),
fact_handles: BTreeMap::from([(
"email".to_owned(),
tinycomputer_bus::agent::SecretHandle {
handle: "new-private-handle".to_owned(),
mask: "‹email›".to_owned(),
kind: "asked_input".to_owned(),
},
)]),
..ContinueTaskRequest::default()
});
assert_eq!(continued.data.unwrap().status, TaskStatus::Running);
assert!(matches!(
settle(&tasks, &view.id).await.status,
TaskStatus::Done { .. }
));
let request = &script.requests.lock().unwrap()[0];
assert_eq!(
request.fact_handles["passport number"].handle,
"existing-vault-handle"
);
assert_eq!(request.fact_handles["email"].handle, "new-private-handle");
}

#[tokio::test]
async fn constraints_that_cannot_start_a_task_are_refused() {
let (tasks, _) = controller(Vec::new());
Expand Down
Loading