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
17 changes: 17 additions & 0 deletions crates/tinycomputer-bus/src/agent/agent_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,23 @@ fn host_context_debug_output_hides_the_opaque_value() {
assert!(!format!("{context:?}").contains("secret-capability"));
}

#[test]
fn a_do_turn_limit_pins_its_wire_form() {
let request: StartTaskRequest = serde_json::from_value(json!({
"task": "buy a pack of dried fruit",
"budget": {"max_do_turns": 16}
}))
.unwrap();
assert_eq!(request.budget.max_do_turns, Some(16));
assert_eq!(
serde_json::to_value(request.budget).unwrap()["max_do_turns"],
16
);
let unset: StartTaskRequest =
serde_json::from_value(json!({"task": "buy a pack of dried fruit"})).unwrap();
assert_eq!(unset.budget.max_do_turns, None);
}

#[test]
fn rescues_pin_their_wire_form() {
let request: StartTaskRequest = serde_json::from_value(json!({
Expand Down
3 changes: 3 additions & 0 deletions crates/tinycomputer-bus/src/agent/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ pub struct TaskBudget {
/// How many ways each decision is asked before its answers are averaged;
/// the module's default when unset.
pub votes: Option<u32>,
/// Turns each `do` step may spend before it fails as not accomplished;
/// the module's default (8) when unset, at most 32.
pub max_do_turns: Option<u32>,
/// How each flow run asks its decisions; the narrow strategy when unset.
pub strategy: Option<crate::FlowStrategy>,
/// How much each flow run deliberates before acting; the deep level
Expand Down
18 changes: 18 additions & 0 deletions crates/tinycomputer-bus/src/flow/flow_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ fn a_brief_pins_its_wire_form_and_defaults_empty() {
}))
.unwrap();
assert_eq!((request.votes, request.brief.goal.as_str()), (3, "g"));
assert_eq!(
request.max_do_turns, None,
"unset keeps the module's default"
);
assert!(
serde_json::to_value(&request)
.unwrap()
.get("max_do_turns")
.is_none(),
"an unset turn limit is left off the wire"
);
let request: RunFlowRequest = serde_json::from_value(json!({
"flow": {"app": "Mail", "steps": ["x"]},
"max_do_turns": 12
}))
.unwrap();
assert_eq!(request.max_do_turns, Some(12));
assert_eq!(serde_json::to_value(&request).unwrap()["max_do_turns"], 12);
for (flow_loop, wire) in [
(FlowLoop::Vote, "vote"),
(FlowLoop::PageKind, "page_kind"),
Expand Down
6 changes: 6 additions & 0 deletions crates/tinycomputer-bus/src/flow/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ pub struct RunFlowRequest {
/// answers are averaged: 1 asks once. Jev calls are cheap, so accuracy
/// is bought with more of them. Capped by the module at 9.
pub votes: u32,
/// Turns a `do` step may spend before it fails as not accomplished; the
/// module's default (8) when unset, capped by the module at 32. A shop's
/// checkout can need more moves in one step than a desktop app does.
#[serde(skip_serializing_if = "Option::is_none")]
pub max_do_turns: Option<u32>,
/// Who the run is for and what it is after, shown to Jev with every
/// question so each small decision is made knowing the whole task.
pub brief: FlowBrief,
Expand Down Expand Up @@ -241,6 +246,7 @@ impl Default for RunFlowRequest {
max_actions: 60,
max_model_calls: 3000,
votes: 7,
max_do_turns: None,
brief: FlowBrief::default(),
disabled_loops: Vec::new(),
memory: Vec::new(),
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, 27);
pub const CONTRACT_VERSION: (u32, u32) = (2, 28);

/// 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, 27);
/// ```
/// # use tinycomputer_bus::{is_compatible, CONTRACT_VERSION};
/// assert!(is_compatible(CONTRACT_VERSION));
/// assert!(is_compatible((2, 27)));
/// assert!(is_compatible((2, 28)));
/// assert!(!is_compatible((2, 11)));
/// assert!(!is_compatible((1, 8)));
/// ```
Expand Down
7 changes: 5 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, 27));
assert_eq!(CONTRACT_VERSION, (2, 28));
}

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

#[test]
fn a_newer_minor_on_the_module_side_binds() {
assert!(is_compatible((2, 27)));
assert!(is_compatible((2, 28)));
assert!(is_compatible((2, 97)));
// 2.24 adds same-task flow-to-planner handoff to StartTask.
// 2.22 adds optional host-held secret handles to StartTask and RunFlow.
Expand Down Expand Up @@ -44,6 +44,9 @@ fn a_newer_minor_on_the_module_side_binds() {
// 2.26 adds TraceSinkHost, so a 2.25 module lacks that callback.
// 2.27 relays opaque host context to ConsequenceHost reviews. A 2.26
// module would drop that context, so a host requiring it cannot bind.
// 2.28 adds `max_do_turns` to TaskBudget and RunFlowRequest. A 2.27
// module ignores it and keeps every `do` step at 8 turns.
assert!(!is_compatible((2, 27)));
assert!(!is_compatible((2, 26)));
assert!(!is_compatible((2, 25)));
assert!(!is_compatible((2, 24)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,43 @@ async fn an_action_judged_unhelpful_is_undone_and_not_tried_again() {
assert_eq!(run.result.stop, FlowStopReason::Completed);
}

#[tokio::test]
async fn a_do_step_spends_the_turns_its_run_allows() {
let ended_after = |max_do_turns: Option<u32>| async move {
let run = run_with(
App::quirky(Quirk::Frozen),
json!({"app": "Mail", "steps": ["open the search results"]}),
|request| {
request.max_do_turns = max_do_turns;
request.max_actions = 120;
},
|id, question, _| (id == "move").then(|| pick(question, "wait", 0.9)),
)
.await;
assert_eq!(run.result.stop, FlowStopReason::StepFailed);
let step = &run.result.steps[0];
(step.turns, step.note.clone())
};

let (turns, note) = ended_after(None).await;
assert_eq!(turns, 8, "the default is unchanged");
assert!(note.contains("not accomplished after 8 turns"), "{note}");

let (turns, note) = ended_after(Some(3)).await;
assert_eq!(turns, 3);
assert!(note.contains("not accomplished after 3 turns"), "{note}");

let (turns, note) = ended_after(Some(12)).await;
assert_eq!(turns, 12);
assert!(note.contains("not accomplished after 12 turns"), "{note}");

let (_, note) = ended_after(Some(500)).await;
assert!(
note.contains("not accomplished after 32 turns"),
"capped by the module: {note}"
);
}

#[tokio::test]
async fn waits_that_change_nothing_are_not_a_stall_and_stop_being_offered() {
let run = run_with(
Expand Down
5 changes: 5 additions & 0 deletions crates/tinycomputer-engine/src/agentic/flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ const MAX_ACTIONS: u32 = 120;
/// Upper bound on [`RunFlowRequest::max_model_calls`]. Jev is cheap, and
/// every framing of a voted decision is one evaluation.
const MAX_CALLS: u32 = 10_000;
/// Upper bound on [`RunFlowRequest::max_do_turns`]: a step stuck past this
/// many turns is better rescued than tried again.
const MAX_DO_TURNS: u32 = 32;
/// Choices, picks, and entries remembered for the brief's `so_far`.
const MAX_SO_FAR: usize = 12;
/// Longest goal the brief carries, in characters.
Expand Down Expand Up @@ -272,6 +275,8 @@ pub(super) struct FlowRun<'r, B> {
pub(super) page: Option<String>,
/// How many ways each decision is asked.
votes: u32,
/// Turns a `do` step may spend.
pub(super) do_turns: u32,
/// The secret values, to mask every request with.
secrets: Facts,
/// Opaque host handles, never substituted into flow text.
Expand Down
5 changes: 4 additions & 1 deletion crates/tinycomputer-engine/src/agentic/flow/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tinycomputer_bus::{
use tinycomputer_core::Facts;

use super::{
Ended, FlowRun, Halt, MAX_ACTIONS, MAX_CALLS, StepLog,
Ended, FlowRun, Halt, MAX_ACTIONS, MAX_CALLS, MAX_DO_TURNS, StepLog,
backend::AgentBackend,
front::Front,
ledger, steps,
Expand Down Expand Up @@ -106,6 +106,9 @@ impl<'r, B: AgentBackend + Sync> FlowRun<'r, B> {
so_far: Vec::new(),
page: None,
votes: request.votes.clamp(1, vote::MAX_VOTES),
do_turns: request
.max_do_turns
.map_or(steps::DO_TURNS, |turns| turns.clamp(1, MAX_DO_TURNS)),
secrets,
fact_handles: request.fact_handles.clone(),
secret_host,
Expand Down
7 changes: 4 additions & 3 deletions crates/tinycomputer-engine/src/agentic/flow/steps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ use tinycomputer_bus::FlowAction;

use super::{Ended, FlowRun, Halt, StepLog, backend::AgentBackend};

/// Turns a `do` step may spend.
const DO_TURNS: u32 = 8;
/// Turns a `do` step may spend when the run does not set
/// `RunFlowRequest::max_do_turns`.
pub(super) const DO_TURNS: u32 = 8;
/// Turns spent opening the thing a `choose` step picks from.
pub(super) const REVEAL_TURNS: u32 = 3;
/// Times `open` checks for a readable window, waiting between checks.
Expand Down Expand Up @@ -99,7 +100,7 @@ pub(super) async fn run<B: AgentBackend + Sync>(
// then be typed as the caller's card number.
FlowAction::Do(intent) => match typing::typing(intent) {
Some(slot) => run.enter(log, &[slot]).await,
None => run.accomplish(log, text, DO_TURNS).await,
None => run.accomplish(log, text, run.do_turns).await,
},
FlowAction::Enter(slots) => run.enter(log, &slots.0).await,
FlowAction::Choose(choose) => run.choose(log, choose).await,
Expand Down
1 change: 1 addition & 0 deletions crates/tinycomputer-engine/src/task/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ pub(super) fn run_request(
max_actions,
max_model_calls,
votes: state.budget.votes.unwrap_or(DEFAULT_VOTES),
max_do_turns: state.budget.max_do_turns,
strategy: state.budget.strategy.unwrap_or_default(),
deliberation: state.budget.deliberation.unwrap_or_default(),
brief: brief(&state),
Expand Down
6 changes: 6 additions & 0 deletions crates/tinycomputer-engine/src/task/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ fn start_task_input() -> Value {
"max_actions": {"type": "integer"},
"max_model_calls": {"type": "integer"},
"votes": {"type": "integer"},
"max_do_turns": {
"type": "integer",
"minimum": 1,
"maximum": 32,
"description": "turns each do step may spend before it fails as not accomplished; 8 when unset"
},
"strategy": {"enum": ["narrow", "wide"], "default": "narrow"},
"deliberation": {"enum": ["off", "standard", "deep"], "default": "deep"},
"max_elapsed_ms": {"type": "integer"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ async fn describe_schemas_name_every_request_field() {
max_actions: Some(1),
max_model_calls: Some(1),
votes: Some(1),
max_do_turns: Some(1),
strategy: Some(tinycomputer_bus::FlowStrategy::default()),
deliberation: Some(tinycomputer_bus::Deliberation::default()),
max_elapsed_ms: Some(1),
Expand Down
27 changes: 27 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 @@ -66,6 +66,7 @@ async fn a_finished_flow_is_done_with_its_reads_and_no_fact_values() {
(request.max_actions, request.max_model_calls, request.votes),
(120, 6000, 7)
);
assert_eq!(request.max_do_turns, None, "the module's own default");
assert!(request.facts.is_empty(), "an email is shared, not secret");
assert_eq!(request.brief.details["email"], "asha@example.com");
assert!(
Expand Down Expand Up @@ -630,3 +631,29 @@ async fn a_run_gets_the_callers_values_and_the_flow_keeps_its_own_definitions()
);
assert_eq!(request.flow.vars["first_name"], "${first name}");
}

#[tokio::test]
async fn a_budget_turn_limit_reaches_every_run() {
let (tasks, script) = controller(vec![finished_run(
FlowStopReason::Completed,
vec![step("1", "do", "add it to the cart", StepOutcome::Done, "")],
&[],
None,
)]);
let view = tasks
.start(&StartTaskRequest {
flow: Some(
serde_json::from_value(json!({"app": "browser", "steps": ["add it to the cart"]}))
.unwrap(),
),
budget: tinycomputer_bus::agent::TaskBudget {
max_do_turns: Some(16),
..tinycomputer_bus::agent::TaskBudget::default()
},
..StartTaskRequest::default()
})
.data
.unwrap();
settle(&tasks, &view.id).await;
assert_eq!(script.requests.lock().unwrap()[0].max_do_turns, Some(16));
}
1 change: 1 addition & 0 deletions crates/tinycomputer-skills/schemas/start_task.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"max_model_calls": {"type": "integer"},
"max_elapsed_ms": {"type": "integer"},
"votes": {"type": "integer", "description": "How many ways each decision is asked before its answers are averaged."},
"max_do_turns": {"type": "integer", "minimum": 1, "maximum": 32, "description": "How many turns each do step may spend before it fails as not accomplished (default 8, at most 32)."},
"strategy": {"type": "string", "enum": ["narrow", "wide"], "description": "How each decision is asked: many small requests (narrow, the default), or one request per turn over a digest of the screen with a working memory (wide)."},
"deliberation": {"type": "string", "enum": ["off", "standard", "deep"], "description": "How much each decision is deliberated before acting: deep (the default) escalates close calls with more framings, pairwise duels, and contrasting questions, checks every action's effect, and undoes and retries mistakes; standard does less; off uses single thresholds."},
"max_rescues": {"type": "integer", "minimum": 0, "maximum": 5, "description": "How many failed steps a reasoning model may rescue with guidance before the task fails (default and most 5; 0 turns rescues off). Needs the module's planner configuration."}
Expand Down
2 changes: 1 addition & 1 deletion crates/tinycomputer/tests/public_api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn the_crate_re_exports_the_contract_rather_than_redefining_it() {
#[test]
fn the_shipped_contract_binds_to_itself() {
assert!(is_compatible(CONTRACT_VERSION));
assert_eq!(CONTRACT_VERSION, (2, 27));
assert_eq!(CONTRACT_VERSION, (2, 28));
assert!(!is_compatible((2, 25)));
}

Expand Down
3 changes: 2 additions & 1 deletion docs/crates/tinycomputer-bus/agent-and-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ for the fuller picture of what does and does not get automated here.
`TaskBudget` mirrors the levers `RunFlowRequest` exposes (see
[Writing flows](flows.md)) but every field is `Option`, so an unset one takes
the module's own default rather than the crate baking one in twice:
`max_actions`, `max_model_calls`, `votes`, `strategy`, `deliberation`,
`max_actions`, `max_model_calls`, `votes`, `max_do_turns` (turns each `do`
step may spend, 8 when unset, at most 32), `strategy`, `deliberation`,
`max_elapsed_ms` (excludes time spent waiting on the caller), and
`max_rescues`, how many times a failed step may be handed to a reasoning
model for guidance before the task gives up on it. Absent means the module's
Expand Down
2 changes: 1 addition & 1 deletion docs/technical/decision-thresholds.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Change a constant and its row together.
| `CAP` | 20 | `ask/mod.rs` | most options in one Choice |
| `HEDGED` | 0.10 | `ask/answers.rs` | distance from an even chance within which a condition's calibrated yes/no says nothing either way, and defers to a crisp coverage |
| `CRISP_TOP` | 0.85 | `ask/answers.rs` | probability on a condition's top coverage level at which it stands alone for a hedged yes/no |
| `DO_TURNS` | 8 | `steps/mod.rs` | turns a `do` step may spend |
| `DO_TURNS` / `MAX_DO_TURNS` | 8 / 32 | `steps/mod.rs`, `mod.rs` | turns a `do` step may spend when `max_do_turns` is unset, and the most a run may set |
| `REFLECT_FLOOR` | 0.50 | `reflect.rs` | belief that a pressed `choose` left its choice, below which it is repaired, and failed if the repair does not take |
| `REPAIR_TURNS` | 4 | `reflect.rs` | turns one reflection repair may spend |
| `MAX_ACTIONS` / `MAX_CALLS` | 120 / 10000 | `mod.rs` | per-run caps on actions and Jev calls |
Expand Down
5 changes: 5 additions & 0 deletions docs/technical/specs/desktop-module-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ serves the interface and fails to answer, turns the approval into a decline, so
the task never takes the action. A host that serves nothing leaves the pause as
the only gate. A payment checkpoint is never continuable and needs no review.

Do-step turns (2.28): `TaskBudget` and `RunFlowRequest` take an optional
`max_do_turns`, the turns each `do` step may spend before it fails as not
accomplished. Unset keeps the module's default of 8; the module caps it at
32. A 2.27 module ignores the field.

Host context relay (2.27): `ContinueTaskRequest` takes an optional opaque
`host_context`. When the module requests consequence review, it copies that
value unchanged into `ReviewActionRequest.host_context`; the module does not
Expand Down
3 changes: 3 additions & 0 deletions docs/technical/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ is acted on: `deep` (the default), `standard`, or `off`
([`specs/jev-deliberation.md`](specs/jev-deliberation.md)). Deliberation
spends calls only where the evidence is thin, and degrades rung by rung when
the budget runs short rather than failing the run.
`budget.max_do_turns` sets how many turns each `do` step may spend before it
fails as not accomplished: 8 when unset, at most 32. A long step on a busy
page, such as a shop's cart or checkout, may need more than 8 moves.
`budget.max_rescues` caps how many failed steps are rescued (below).
The flow runtime has no clock, so `max_elapsed_ms` is enforced by
the controller, which times out a run that would go past what is left. Time
Expand Down
Loading