tinyagents 2.1.0

A recursive language-model (RLM) harness for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Unit tests for the thread-goal domain types.

use super::prompt::*;
use super::types::*;

fn goal(status: ThreadGoalStatus, token_budget: Option<u64>, tokens_used: u64) -> ThreadGoal {
    ThreadGoal {
        thread_id: "t".into(),
        goal_id: "goal-0".into(),
        objective: "ship the release".into(),
        status,
        token_budget,
        tokens_used,
        time_used_seconds: 0,
        created_at_ms: 0,
        updated_at_ms: 0,
        continuation_suppressed: false,
    }
}

#[test]
fn status_strings_match_serialized() {
    assert_eq!(ThreadGoalStatus::Active.as_str(), "active");
    assert_eq!(ThreadGoalStatus::Paused.as_str(), "paused");
    assert_eq!(ThreadGoalStatus::BudgetLimited.as_str(), "budget_limited");
    assert_eq!(ThreadGoalStatus::Complete.as_str(), "complete");
}

#[test]
fn active_and_terminal_predicates() {
    assert!(ThreadGoalStatus::Active.is_active());
    assert!(!ThreadGoalStatus::Paused.is_active());
    assert!(ThreadGoalStatus::Complete.is_terminal());
    assert!(ThreadGoalStatus::BudgetLimited.is_terminal());
    assert!(!ThreadGoalStatus::Active.is_terminal());
    assert!(!ThreadGoalStatus::Paused.is_terminal());
}

#[test]
fn budget_helpers() {
    let mut g = goal(ThreadGoalStatus::Active, Some(100), 40);
    assert_eq!(g.budget_remaining(), Some(60));
    assert!(!g.over_budget());
    g.tokens_used = 120;
    assert_eq!(g.budget_remaining(), Some(0));
    assert!(g.over_budget());
    g.token_budget = None;
    assert_eq!(g.budget_remaining(), None);
    assert!(!g.over_budget());
}

#[test]
fn context_block_present_only_for_active_and_budget_limited() {
    let active = goal(ThreadGoalStatus::Active, Some(500), 100);
    let block = active_goal_context_block(&active).expect("active goal has a context block");
    assert!(block.contains("ship the release"));
    assert!(block.contains("goal_complete"));
    assert!(block.contains("400"), "should name remaining budget");

    let limited = goal(ThreadGoalStatus::BudgetLimited, Some(100), 100);
    let block = active_goal_context_block(&limited).expect("budget-limited has a stop block");
    assert!(block.contains("budget"));

    assert!(active_goal_context_block(&goal(ThreadGoalStatus::Paused, None, 0)).is_none());
    assert!(active_goal_context_block(&goal(ThreadGoalStatus::Complete, None, 0)).is_none());
}

#[test]
fn thread_goal_round_trips_through_json() {
    let g = goal(ThreadGoalStatus::Active, Some(1000), 250);
    let json = serde_json::to_value(&g).unwrap();
    // camelCase field names on the wire.
    assert_eq!(json["threadId"], "t");
    assert_eq!(json["goalId"], "goal-0");
    assert_eq!(json["tokenBudget"], 1000);
    let back: ThreadGoal = serde_json::from_value(json).unwrap();
    assert_eq!(back, g);
}

mod store_tests {
    use std::sync::Arc;

    use super::super::store;
    use super::ThreadGoalStatus;
    use crate::harness::store::{InMemoryStore, Store};

    fn store() -> Arc<dyn Store> {
        Arc::new(InMemoryStore::default())
    }

    #[tokio::test]
    async fn set_get_clear_round_trip() {
        let s = store();
        assert!(store::get(&s, "t1").await.unwrap().is_none());

        let g = store::set(&s, "t1", "ship the feature", None)
            .await
            .unwrap();
        assert_eq!(g.objective, "ship the feature");
        assert_eq!(g.status, ThreadGoalStatus::Active);
        assert_eq!(g.tokens_used, 0);

        let loaded = store::get(&s, "t1").await.unwrap().unwrap();
        assert_eq!(loaded.goal_id, g.goal_id);

        assert!(store::clear(&s, "t1").await.unwrap());
        assert!(store::get(&s, "t1").await.unwrap().is_none());
        assert!(!store::clear(&s, "t1").await.unwrap());
    }

    #[tokio::test]
    async fn set_same_objective_preserves_goal_id_and_counters() {
        let s = store();
        let g1 = store::set(&s, "t", "objective A", Some(100)).await.unwrap();
        store::account_usage(&s, "t", &g1.goal_id, 30, 5)
            .await
            .unwrap()
            .unwrap();
        let g2 = store::set(&s, "t", "objective A", Some(200)).await.unwrap();
        assert_eq!(g1.goal_id, g2.goal_id, "same objective keeps goal_id");
        assert_eq!(g2.tokens_used, 30, "counters preserved");
        assert_eq!(g2.token_budget, Some(200), "budget refreshed");
    }

    #[tokio::test]
    async fn set_same_objective_stays_budget_limited_when_over_budget() {
        let s = store();
        let g = store::set(&s, "t", "obj", Some(100)).await.unwrap();
        let limited = store::account_usage(&s, "t", &g.goal_id, 120, 0)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(limited.status, ThreadGoalStatus::BudgetLimited);
        let resed = store::set(&s, "t", "obj", Some(100)).await.unwrap();
        assert_eq!(resed.tokens_used, 120, "same-objective preserves counters");
        assert_eq!(
            resed.status,
            ThreadGoalStatus::BudgetLimited,
            "still over budget → must stay budget_limited, not active"
        );
        let raised = store::set(&s, "t", "obj", Some(1000)).await.unwrap();
        assert_eq!(raised.status, ThreadGoalStatus::Active);
    }

    #[tokio::test]
    async fn set_changed_objective_mints_new_goal_id_and_resets() {
        let s = store();
        let g1 = store::set(&s, "t", "objective A", None).await.unwrap();
        store::account_usage(&s, "t", &g1.goal_id, 30, 5)
            .await
            .unwrap()
            .unwrap();
        let g2 = store::set(&s, "t", "objective B", None).await.unwrap();
        assert_ne!(g1.goal_id, g2.goal_id);
        assert_eq!(g2.tokens_used, 0, "counters reset on new objective");
        assert_eq!(g2.created_at_ms, g1.created_at_ms, "created_at preserved");
    }

    #[tokio::test]
    async fn set_if_absent_only_bootstraps_when_empty() {
        let s = store();
        let created = store::set_if_absent(&s, "t", "scout goal", None)
            .await
            .unwrap();
        assert!(created.is_some());
        let again = store::set_if_absent(&s, "t", "different goal", None)
            .await
            .unwrap();
        assert!(again.is_none());
        assert_eq!(
            store::get(&s, "t").await.unwrap().unwrap().objective,
            "scout goal"
        );
    }

    #[tokio::test]
    async fn account_usage_ignores_stale_goal_id() {
        let s = store();
        let g = store::set(&s, "t", "obj", None).await.unwrap();
        let after = store::account_usage(&s, "t", "not-the-goal-id", 50, 1)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(after.tokens_used, 0);
        let after = store::account_usage(&s, "t", &g.goal_id, 50, 1)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(after.tokens_used, 50);
    }

    #[tokio::test]
    async fn account_usage_trips_budget_limited() {
        let s = store();
        let g = store::set(&s, "t", "obj", Some(100)).await.unwrap();
        let after = store::account_usage(&s, "t", &g.goal_id, 120, 2)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(after.status, ThreadGoalStatus::BudgetLimited);
        assert_eq!(after.budget_remaining(), Some(0));
    }

    #[tokio::test]
    async fn pause_resume_complete_transitions() {
        let s = store();
        store::set(&s, "t", "obj", None).await.unwrap();
        assert_eq!(
            store::pause(&s, "t").await.unwrap().status,
            ThreadGoalStatus::Paused
        );
        assert_eq!(
            store::resume(&s, "t").await.unwrap().status,
            ThreadGoalStatus::Active
        );
        let done = store::complete(&s, "t").await.unwrap();
        assert_eq!(done.status, ThreadGoalStatus::Complete);
        // Resume does not reactivate a completed goal.
        assert_eq!(
            store::resume(&s, "t").await.unwrap().status,
            ThreadGoalStatus::Complete
        );
    }

    #[tokio::test]
    async fn mutators_error_without_a_goal() {
        let s = store();
        assert!(store::complete(&s, "missing").await.is_err());
        assert!(store::pause(&s, "missing").await.is_err());
    }

    #[tokio::test]
    async fn empty_objective_and_blank_thread_id_rejected() {
        let s = store();
        assert!(store::set(&s, "t", "   ", None).await.is_err());
        assert!(store::set(&s, "  ", "obj", None).await.is_err());
    }

    #[tokio::test]
    async fn list_all_returns_every_thread_goal() {
        let s = store();
        store::set(&s, "alpha", "a", None).await.unwrap();
        store::set(&s, "beta", "b", None).await.unwrap();
        let mut ids: Vec<String> = store::list_all(&s)
            .await
            .unwrap()
            .into_iter()
            .map(|g| g.thread_id)
            .collect();
        ids.sort();
        assert_eq!(ids, vec!["alpha".to_string(), "beta".to_string()]);
    }
}

mod tool_tests {
    use std::sync::Arc;

    use serde_json::json;

    use super::super::tool::{GoalTool, GoalToolKind, goal_tools};
    use crate::harness::events::EventSink;
    use crate::harness::ids::{RunId, ThreadId};
    use crate::harness::store::{InMemoryStore, Store};
    use crate::harness::tool::{Tool, ToolCall, ToolExecutionContext};

    fn store() -> Arc<dyn Store> {
        Arc::new(InMemoryStore::default())
    }

    fn ctx(thread_id: Option<&str>) -> ToolExecutionContext {
        ToolExecutionContext {
            run_id: RunId::new("run-1"),
            thread_id: thread_id.map(ThreadId::new),
            depth: 0,
            max_turn_output_tokens: None,
            events: EventSink::new(),
            streaming: false,
            workspace: None,
        }
    }

    fn call(id: &str, name: &str, args: serde_json::Value) -> ToolCall {
        ToolCall {
            id: id.to_string(),
            name: name.to_string(),
            arguments: args,
            invalid: None,
        }
    }

    #[test]
    fn goal_tools_builds_the_model_facing_set() {
        let tools = goal_tools(store());
        let names: Vec<&str> = tools.iter().map(|t| Tool::<()>::name(t.as_ref())).collect();
        assert_eq!(names, vec!["goal_get", "goal_set", "goal_complete"]);
    }

    #[tokio::test]
    async fn set_get_complete_via_tools_in_thread_scope() {
        let s = store();
        let set = GoalTool::new(GoalToolKind::Set, s.clone());
        let res = Tool::<()>::call_with_context(
            &set,
            &(),
            call(
                "c1",
                "goal_set",
                json!({ "objective": "land the PR", "token_budget": 5000 }),
            ),
            ctx(Some("thread-tools")),
        )
        .await
        .unwrap();
        assert!(res.error.is_none(), "{res:?}");
        assert!(res.content.contains("land the PR"));

        let get = GoalTool::new(GoalToolKind::Get, s.clone());
        let res = Tool::<()>::call_with_context(
            &get,
            &(),
            call("c2", "goal_get", json!({})),
            ctx(Some("thread-tools")),
        )
        .await
        .unwrap();
        assert!(res.content.contains("status: active"));

        let done = GoalTool::new(GoalToolKind::Complete, s.clone());
        let res = Tool::<()>::call_with_context(
            &done,
            &(),
            call("c3", "goal_complete", json!({})),
            ctx(Some("thread-tools")),
        )
        .await
        .unwrap();
        assert!(res.content.contains("status: complete"));
    }

    #[tokio::test]
    async fn tools_error_without_thread_scope() {
        let s = store();
        let set = GoalTool::new(GoalToolKind::Set, s);
        // Bare call (no context) errors.
        let res = Tool::<()>::call(
            &set,
            &(),
            call("c1", "goal_set", json!({ "objective": "x" })),
        )
        .await
        .unwrap();
        assert!(res.error.is_some());
        assert!(res.error.unwrap().contains("active thread"));
        // Context with no thread id also errors.
        let set = GoalTool::new(GoalToolKind::Set, store());
        let res = Tool::<()>::call_with_context(
            &set,
            &(),
            call("c2", "goal_set", json!({ "objective": "x" })),
            ctx(None),
        )
        .await
        .unwrap();
        assert!(res.error.is_some());
    }

    #[tokio::test]
    async fn get_reports_absent_goal() {
        let get = GoalTool::new(GoalToolKind::Get, store());
        let res = Tool::<()>::call_with_context(
            &get,
            &(),
            call("c1", "goal_get", json!({})),
            ctx(Some("empty-thread")),
        )
        .await
        .unwrap();
        assert!(res.content.contains("no goal set"));
    }

    #[tokio::test]
    async fn set_missing_objective_is_a_soft_error() {
        let set = GoalTool::new(GoalToolKind::Set, store());
        let res = Tool::<()>::call_with_context(
            &set,
            &(),
            call("c1", "goal_set", json!({})),
            ctx(Some("t")),
        )
        .await
        .unwrap();
        assert!(res.content.contains("missing 'objective'"));
    }
}

mod continuation_tests {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::Duration;

    use super::super::store;
    use super::super::types::{GoalProgress, ThreadGoalStatus, TurnOutcome};
    use super::super::{goal_gate_node, note_user_turn, run_continuation_tick};
    use crate::error::TinyAgentsError;
    use crate::graph::GraphBuilder;
    use crate::graph::command::NodeResult;
    use crate::harness::store::{InMemoryStore, Store};

    fn store() -> Arc<dyn Store> {
        Arc::new(InMemoryStore::default())
    }

    /// State overwritten by each work iteration: the tokens it "spent" and
    /// whether it made progress. The gate's `progress` closure reads these.
    #[derive(Clone, Debug, Default, PartialEq)]
    struct GateState {
        iters: usize,
        tokens: u64,
        progress: bool,
    }

    /// Builds and runs `work_node -> gate` under `thread_id`, looping until the
    /// gate routes to END or the recursion limit trips. `work` is the per-iteration
    /// behavior producing the next [`GateState`].
    async fn run_gate<W, Fut>(
        s: &Arc<dyn Store>,
        thread_id: &str,
        recursion_limit: usize,
        per_iter_tokens: u64,
        made_progress: bool,
        work: W,
    ) -> crate::error::Result<GateState>
    where
        W: Fn(usize) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = ()> + Send + 'static,
    {
        let counter = Arc::new(AtomicUsize::new(0));
        let work = Arc::new(work);
        let work_node = {
            let counter = counter.clone();
            let work = work.clone();
            move |_state: GateState, _ctx| {
                let counter = counter.clone();
                let work = work.clone();
                Box::pin(async move {
                    let n = counter.fetch_add(1, Ordering::SeqCst) + 1;
                    work(n).await;
                    Ok(NodeResult::Update(GateState {
                        iters: n,
                        tokens: per_iter_tokens,
                        progress: made_progress,
                    }))
                }) as crate::graph::NodeFuture<GateState>
            }
        };

        let gate = goal_gate_node::<GateState, GateState>(s.clone(), "work", |st: &GateState| {
            GoalProgress {
                tokens_used: st.tokens,
                elapsed_secs: 0,
                made_progress: st.progress,
            }
        });

        let graph = GraphBuilder::<GateState, GateState>::overwrite()
            .with_recursion_limit(recursion_limit)
            .add_node("work", work_node)
            .add_node("gate", gate)
            .set_entry("work")
            .add_edge("work", "gate")
            .with_command_destinations("gate", ["work", crate::graph::END])
            .compile()?;

        let exec = graph
            .run_with_thread(thread_id, GateState::default())
            .await?;
        Ok(exec.state)
    }

    #[tokio::test]
    async fn gate_loops_while_active_then_stops_on_complete() {
        let s = store();
        store::set(&s, "t", "obj", None).await.unwrap();
        let s2 = s.clone();
        let final_state = run_gate(&s, "t", 100, 10, true, move |n| {
            let s2 = s2.clone();
            async move {
                if n >= 3 {
                    store::complete(&s2, "t").await.unwrap();
                }
            }
        })
        .await
        .unwrap();
        assert_eq!(
            final_state.iters, 3,
            "runs the work node until goal completes"
        );
        let goal = store::get(&s, "t").await.unwrap().unwrap();
        assert_eq!(goal.status, ThreadGoalStatus::Complete);
    }

    #[tokio::test]
    async fn gate_stops_on_budget_cap() {
        let s = store();
        store::set(&s, "t", "obj", Some(100)).await.unwrap();
        let final_state = run_gate(&s, "t", 100, 40, true, |_n| async {})
            .await
            .unwrap();
        assert_eq!(final_state.iters, 3, "40*3 = 120 crosses the 100 budget");
        let goal = store::get(&s, "t").await.unwrap().unwrap();
        assert_eq!(goal.status, ThreadGoalStatus::BudgetLimited);
        assert_eq!(goal.tokens_used, 120);
    }

    #[tokio::test]
    async fn gate_stops_and_suppresses_on_zero_progress() {
        let s = store();
        store::set(&s, "t", "obj", None).await.unwrap();
        let final_state = run_gate(&s, "t", 100, 0, false, |_n| async {})
            .await
            .unwrap();
        assert_eq!(
            final_state.iters, 1,
            "a no-progress iteration stops the loop"
        );
        let goal = store::get(&s, "t").await.unwrap().unwrap();
        assert!(goal.continuation_suppressed, "one-shot suppression set");
    }

    #[tokio::test]
    async fn gate_recursion_limit_is_the_backstop() {
        let s = store();
        store::set(&s, "t", "obj", None).await.unwrap();
        // Never completes, never stalls, no budget → the loop only stops at the
        // recursion limit (work+gate = 2 supersteps per iteration).
        let err = run_gate(&s, "t", 6, 5, true, |_n| async {})
            .await
            .unwrap_err();
        assert!(matches!(err, TinyAgentsError::RecursionLimit(_)));
    }

    #[tokio::test]
    async fn gate_stops_when_thread_has_no_goal() {
        let s = store();
        // No goal set for the thread → gate routes straight to END after one work run.
        let final_state = run_gate(&s, "t", 100, 5, true, |_n| async {})
            .await
            .unwrap();
        assert_eq!(final_state.iters, 1);
    }

    #[tokio::test]
    async fn driver_runs_up_to_max_per_tick() {
        let s = store();
        store::set(&s, "a", "a", None).await.unwrap();
        store::set(&s, "b", "b", None).await.unwrap();
        store::set(&s, "c", "c", None).await.unwrap();
        let ran = run_continuation_tick(&s, Duration::ZERO, 2, |_g| async {
            Ok(TurnOutcome {
                tokens_used: 5,
                elapsed_secs: 0,
                made_progress: true,
            })
        })
        .await
        .unwrap();
        assert_eq!(ran, 2, "capped at max_per_tick");
    }

    #[tokio::test]
    async fn driver_skips_non_idle_goals() {
        let s = store();
        store::set(&s, "fresh", "obj", None).await.unwrap();
        // A 1-hour idle window over a just-created goal → nothing runs.
        let ran = run_continuation_tick(&s, Duration::from_secs(3600), 5, |_g| async {
            Ok(TurnOutcome::default())
        })
        .await
        .unwrap();
        assert_eq!(ran, 0);
    }

    #[tokio::test]
    async fn driver_suppresses_after_a_no_progress_turn() {
        let s = store();
        store::set(&s, "t", "obj", None).await.unwrap();
        let ran = run_continuation_tick(&s, Duration::ZERO, 5, |_g| async {
            Ok(TurnOutcome {
                tokens_used: 3,
                elapsed_secs: 0,
                made_progress: false,
            })
        })
        .await
        .unwrap();
        assert_eq!(ran, 1);
        assert!(
            store::get(&s, "t")
                .await
                .unwrap()
                .unwrap()
                .continuation_suppressed
        );
        // A second tick finds the goal suppressed and runs nothing.
        let ran2 = run_continuation_tick(&s, Duration::ZERO, 5, |_g| async {
            Ok(TurnOutcome::default())
        })
        .await
        .unwrap();
        assert_eq!(ran2, 0);
    }

    #[tokio::test]
    async fn note_user_turn_clears_suppression_and_resumes() {
        let s = store();
        let g = store::set(&s, "t", "obj", None).await.unwrap();
        // Suppress, then a user turn clears it.
        store::set_continuation_suppressed_if(&s, "t", &g.goal_id, true)
            .await
            .unwrap();
        let after = note_user_turn(&s, "t").await.unwrap().unwrap();
        assert!(!after.continuation_suppressed);

        // A paused goal is reactivated by a user turn.
        store::pause(&s, "t").await.unwrap();
        let after = note_user_turn(&s, "t").await.unwrap().unwrap();
        assert_eq!(after.status, ThreadGoalStatus::Active);

        // No goal → None.
        assert!(note_user_turn(&s, "missing").await.unwrap().is_none());
    }
}