navi-core 0.1.0

Local agentic engine and terminal-first coding agent.
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
#[cfg(test)]
mod tests {
    use crate::goal::types::{GoalId, GoalStatus, SessionGoal};
    use crate::goal::{GoalExtension, GoalRuntimeHandle, GoalService};
    use crate::tool::{Tool, ToolInvocation};
    use serde_json::json;
    use std::sync::Arc;

    // ── types ──────────────────────────────────────────────────

    #[test]
    fn goal_id_is_unique() {
        let a = GoalId::new();
        let b = GoalId::new();
        assert_ne!(a, b);
    }

    #[test]
    fn goal_id_roundtrip_string() {
        let id = GoalId::new();
        let s = id.to_string();
        let id2 = GoalId::from_string(&s);
        assert_eq!(id, id2);
    }

    #[test]
    fn goal_status_terminal() {
        assert!(GoalStatus::Complete.is_terminal());
        assert!(GoalStatus::BudgetLimited.is_terminal());
        assert!(!GoalStatus::Active.is_terminal());
        assert!(!GoalStatus::Paused.is_terminal());
        assert!(!GoalStatus::Blocked.is_terminal());
        assert!(!GoalStatus::UsageLimited.is_terminal());
    }

    #[test]
    fn goal_status_auto_continue() {
        assert!(GoalStatus::Active.should_auto_continue());
        assert!(!GoalStatus::UsageLimited.should_auto_continue());
        assert!(!GoalStatus::Complete.should_auto_continue());
        assert!(!GoalStatus::BudgetLimited.should_auto_continue());
        assert!(!GoalStatus::Paused.should_auto_continue());
        assert!(!GoalStatus::Blocked.should_auto_continue());
    }

    #[test]
    fn session_goal_new_is_active() {
        let goal = SessionGoal::new("s1".into(), "test objective".into(), None);
        assert_eq!(goal.status, GoalStatus::Active);
        assert_eq!(goal.tokens_used, 0);
        assert_eq!(goal.time_used_seconds, 0);
    }

    #[test]
    fn session_goal_record_tokens() {
        let mut goal = SessionGoal::new("s1".into(), "test".into(), Some(100));
        assert!(!goal.record_tokens(50));
        assert_eq!(goal.tokens_used, 50);
        assert_eq!(goal.status, GoalStatus::Active);
        assert!(goal.record_tokens(60)); // 50+60=110 > 100 budget
        assert_eq!(goal.tokens_used, 110);
        assert_eq!(goal.status, GoalStatus::BudgetLimited);
    }

    #[test]
    fn session_goal_no_budget_never_exceeded() {
        let mut goal = SessionGoal::new("s1".into(), "test".into(), None);
        assert!(!goal.record_tokens(1_000_000));
        assert_eq!(goal.status, GoalStatus::Active);
    }

    #[test]
    fn session_goal_blocked_after_three_same_reason() {
        let mut goal = SessionGoal::new("s1".into(), "test".into(), None);
        assert!(!goal.record_blocked_turn("api down"));
        assert_eq!(goal.consecutive_blocked_turns, 1);
        assert!(!goal.record_blocked_turn("api down"));
        assert_eq!(goal.consecutive_blocked_turns, 2);
        assert!(goal.record_blocked_turn("api down"));
        assert_eq!(goal.status, GoalStatus::Blocked);
        assert_eq!(goal.consecutive_blocked_turns, 3);
    }

    #[test]
    fn session_goal_different_reasons_resets_counter() {
        let mut goal = SessionGoal::new("s1".into(), "test".into(), None);
        goal.record_blocked_turn("api down");
        goal.record_blocked_turn("api down");
        assert_eq!(goal.consecutive_blocked_turns, 2);
        assert!(!goal.record_blocked_turn("network error"));
        assert_eq!(goal.consecutive_blocked_turns, 1); // reset
    }

    #[test]
    fn session_goal_remaining_budget() {
        let goal = SessionGoal::new("s1".into(), "test".into(), Some(200));
        assert_eq!(goal.remaining_budget(), Some(200));
    }

    #[test]
    fn session_goal_transition_to() {
        let mut goal = SessionGoal::new("s1".into(), "test".into(), None);
        goal.transition_to(GoalStatus::Paused);
        assert_eq!(goal.status, GoalStatus::Paused);
    }

    // ── runtime ────────────────────────────────────────────────

    #[test]
    fn runtime_get_goal_none_by_default() {
        let runtime = GoalRuntimeHandle::new(None);
        assert!(runtime.get_goal().is_none());
    }

    #[test]
    fn runtime_set_and_get_objective() {
        let runtime = GoalRuntimeHandle::new(None);
        runtime.set_session_id("sess-1");
        let goal = runtime.set_objective("new objective".into(), Some(500));
        assert_eq!(goal.objective, "new objective");
        assert_eq!(goal.session_id, "sess-1");
        assert_eq!(goal.token_budget, Some(500));
        assert_eq!(goal.status, GoalStatus::Active);

        let got = runtime.get_goal().unwrap();
        assert_eq!(got.objective, "new objective");
    }

    #[test]
    fn runtime_clear_goal() {
        let runtime = GoalRuntimeHandle::new(None);
        runtime.set_objective("test".into(), None);
        assert!(runtime.get_goal().is_some());
        runtime.clear_goal();
        assert!(runtime.get_goal().is_none());
    }

    #[test]
    fn runtime_continue_if_idle_returns_prompt() {
        let runtime = GoalRuntimeHandle::new(None);
        runtime.set_objective("do work".into(), None);
        let prompt = runtime.continue_if_idle();
        assert!(prompt.is_some());
        assert!(prompt.unwrap().contains("do work"));
    }

    #[test]
    fn runtime_continue_if_idle_returns_none_when_empty() {
        let runtime = GoalRuntimeHandle::new(None);
        assert!(runtime.continue_if_idle().is_none());
    }

    #[test]
    fn runtime_continue_if_idle_disabled() {
        let runtime = GoalRuntimeHandle::new(None);
        runtime.set_objective("do work".into(), None);
        runtime.set_auto_continue(false);
        assert!(runtime.continue_if_idle().is_none());
    }

    #[test]
    fn runtime_record_tokens_and_budget_limit() {
        let runtime = GoalRuntimeHandle::new(None);
        runtime.set_objective("test".into(), Some(100));
        runtime.start_turn();
        assert!(!runtime.record_tokens(80));
        assert!(runtime.record_tokens(30)); // exceeds 100
        let prompt = runtime.budget_limit_prompt();
        assert!(prompt.is_some());
        assert!(prompt.unwrap().contains("Budget Limit Reached"));
    }

    #[test]
    fn runtime_record_blocked_turn() {
        let runtime = GoalRuntimeHandle::new(None);
        runtime.set_objective("test".into(), None);
        assert!(!runtime.record_blocked_turn("a"));
        assert!(!runtime.record_blocked_turn("a"));
        assert!(runtime.record_blocked_turn("a")); // 3rd
        let goal = runtime.get_goal().unwrap();
        assert_eq!(goal.status, GoalStatus::Blocked);
    }

    #[tokio::test]
    async fn runtime_status_update_survives_turn_finish() {
        let runtime = Arc::new(GoalRuntimeHandle::new(None));
        runtime.set_session_id("sess-1");
        runtime.set_objective("test".into(), None);
        runtime.start_turn();

        let tool = crate::goal::UpdateGoalTool::new(runtime.clone());
        let result = tool
            .invoke(ToolInvocation {
                id: "goal-update-1".to_string(),
                tool_name: "update_goal".to_string(),
                input: json!({ "action": "complete" }),
            })
            .await
            .expect("update goal");
        assert!(result.ok);

        runtime.finish_turn();
        let goal = runtime.get_goal().unwrap();
        assert_eq!(goal.status, GoalStatus::Complete);
    }

    #[tokio::test]
    async fn update_goal_blocked_preserves_consecutive_count() {
        let runtime = Arc::new(GoalRuntimeHandle::new(None));
        runtime.set_session_id("sess-1");
        runtime.set_objective("test".into(), None);
        let tool = crate::goal::UpdateGoalTool::new(runtime.clone());

        for expected_count in 1..=3 {
            let result = tool
                .invoke(ToolInvocation {
                    id: format!("goal-blocked-{expected_count}"),
                    tool_name: "update_goal".to_string(),
                    input: json!({
                        "action": "blocked",
                        "reason": "same blocker"
                    }),
                })
                .await
                .expect("update goal");
            assert!(result.ok);
            assert_eq!(
                result.output["consecutive_blocked_turns"].as_u64(),
                Some(expected_count)
            );
        }

        let goal = runtime.get_goal().unwrap();
        assert_eq!(goal.status, GoalStatus::Blocked);
        assert_eq!(goal.consecutive_blocked_turns, 3);
        assert_eq!(goal.block_reason.as_deref(), Some("same blocker"));
    }

    #[tokio::test]
    async fn update_goal_cannot_resume_terminal_goal() {
        let runtime = Arc::new(GoalRuntimeHandle::new(None));
        runtime.set_session_id("sess-1");
        let mut goal = runtime.set_objective("test".into(), Some(10));
        goal.transition_to(GoalStatus::BudgetLimited);
        runtime.update_goal(goal);

        let tool = crate::goal::UpdateGoalTool::new(runtime.clone());
        let result = tool
            .invoke(ToolInvocation {
                id: "goal-resume-terminal".to_string(),
                tool_name: "update_goal".to_string(),
                input: json!({ "action": "resume" }),
            })
            .await
            .expect("update goal");

        assert!(!result.ok);
        assert_eq!(
            runtime.get_goal().unwrap().status,
            GoalStatus::BudgetLimited
        );
    }

    #[test]
    fn runtime_mark_usage_limited() {
        let runtime = GoalRuntimeHandle::new(None);
        runtime.set_objective("test".into(), None);
        runtime.start_turn();
        runtime.mark_usage_limited();
        let goal = runtime.get_goal().unwrap();
        assert_eq!(goal.status, GoalStatus::UsageLimited);
    }

    // ── accounting ─────────────────────────────────────────────

    #[test]
    fn accounting_basic_flow() {
        let goal = SessionGoal::new("s1".into(), "test".into(), Some(200));
        let acct = crate::goal::GoalAccountingState::new(goal);
        assert!(acct.is_active());

        acct.start_turn();
        assert!(!acct.record_token_usage(100));
        assert!(acct.record_token_usage(150)); // exceeds 200

        let snap = acct.snapshot().unwrap();
        assert_eq!(snap.status, GoalStatus::BudgetLimited);
        assert!(snap.tokens_used >= 200);
    }

    // ── service ────────────────────────────────────────────────

    #[test]
    fn service_set_get_clear_goal() {
        let svc = GoalService::new();
        let goal = svc.set_goal("s1".into(), "obj".into(), Some(500));
        assert_eq!(goal.objective, "obj");

        let got = svc.get_goal("s1");
        assert!(got.is_none()); // runtime not registered

        svc.clear_goal("s1");
    }

    #[test]
    fn service_register_and_use_runtime() {
        let svc = GoalService::new();
        let rt = Arc::new(GoalRuntimeHandle::new(None));

        svc.register_runtime("s1".into(), rt.clone());
        let goal = svc.set_goal("s1".into(), "through_runtime".into(), None);
        assert_eq!(goal.objective, "through_runtime");

        let got = svc.get_goal("s1").unwrap();
        assert_eq!(got.objective, "through_runtime");

        svc.clear_goal("s1");
        assert!(svc.get_goal("s1").is_none());
    }

    // ── steering ───────────────────────────────────────────────

    #[test]
    fn steering_continuation_has_objective() {
        let goal = SessionGoal::new("x".into(), "build feature Y".into(), Some(1000));
        let prompt = crate::goal::steering::build_continuation_prompt(&goal);
        assert!(prompt.contains("build feature Y"));
        assert!(prompt.contains("Active Thread Goal"));
        assert!(prompt.contains("Completion Audit"));
        assert!(prompt.contains("Blocked Audit"));
    }

    #[test]
    fn steering_budget_limit() {
        let mut goal = SessionGoal::new("x".into(), "task".into(), Some(10));
        goal.record_tokens(15);
        let prompt = crate::goal::steering::build_budget_limit_prompt(&goal);
        assert!(prompt.contains("Budget Limit Reached"));
        assert!(prompt.contains("task"));
    }

    #[test]
    fn steering_objective_updated() {
        let goal = SessionGoal::new("x".into(), "new task".into(), None);
        let prompt = crate::goal::steering::build_objective_updated_prompt(&goal);
        assert!(prompt.contains("Objective Updated"));
        assert!(prompt.contains("new task"));
    }

    // ── tools definitions ──────────────────────────────────────

    #[test]
    fn goal_tool_definitions_exist() {
        let defs = crate::goal::goal_tool_definitions();
        assert_eq!(defs.len(), 3);
        let names: Vec<&str> = defs.iter().map(|d| d.name.as_str()).collect();
        assert!(names.contains(&"get_goal"));
        assert!(names.contains(&"create_goal"));
        assert!(names.contains(&"update_goal"));
    }

    #[test]
    fn get_goal_tool_returns_none_when_no_goal() {
        let rt = Arc::new(GoalRuntimeHandle::new(None));
        let tool = crate::goal::GetGoalTool::new(rt);
        let def = tool.definition();
        assert_eq!(def.name, "get_goal");
    }

    #[test]
    fn create_goal_tool_definition() {
        let rt = Arc::new(GoalRuntimeHandle::new(None));
        let tool = crate::goal::CreateGoalTool::new(rt);
        let def = tool.definition();
        assert_eq!(def.name, "create_goal");
        assert_eq!(def.kind, crate::tool::ToolKind::Write);
    }

    #[test]
    fn update_goal_tool_definition() {
        let rt = Arc::new(GoalRuntimeHandle::new(None));
        let tool = crate::goal::UpdateGoalTool::new(rt);
        let def = tool.definition();
        assert_eq!(def.name, "update_goal");
        assert_eq!(def.kind, crate::tool::ToolKind::Write);
    }

    // ── events ─────────────────────────────────────────────────

    #[test]
    fn runtime_event_goal_updated_converts_to_agent_event() {
        use crate::event::{AgentEvent, RuntimeEvent, RuntimeEventKind};
        let event = RuntimeEvent::new(RuntimeEventKind::GoalUpdated {
            session_id: "s1".into(),
            goal_id: "g1".into(),
            objective: "obj".into(),
            status: GoalStatus::Active,
            tokens_used: 42,
            token_budget: Some(100),
        });
        let agent = event.into_agent_event();
        assert!(matches!(agent, Some(AgentEvent::GoalUpdated { .. })));
    }

    #[test]
    fn session_snapshot_has_goal_field() {
        use crate::session::{SessionId, SessionSnapshot};
        let snap = SessionSnapshot {
            version: SessionSnapshot::CURRENT_VERSION,
            id: SessionId::new("test".into()),
            title: None,
            project: std::path::PathBuf::from("/tmp"),
            created_at: 1,
            updated_at: 2,
            events: vec![],
            memory: None,
            goal: Some(SessionGoal::new("s1".into(), "obj".into(), None)),
        };
        assert!(snap.goal.is_some());
        let goal = snap.goal.as_ref().unwrap();
        assert_eq!(goal.objective, "obj");

        let json = serde_json::to_string(&snap).unwrap();
        let loaded: SessionSnapshot = serde_json::from_str(&json).unwrap();
        assert!(loaded.goal.is_some());
    }

    // ── extension ──────────────────────────────────────────────

    #[test]
    fn extension_hooks_basic_flow() {
        let svc = Arc::new(GoalService::new());
        let rt = Arc::new(GoalRuntimeHandle::new(None));
        let ext = GoalExtension::new(svc.clone(), rt.clone());

        ext.on_session_start("sess-1");
        rt.set_objective("work".into(), None);
        ext.on_turn_start("sess-1", "task");
        assert!(!ext.on_token_usage(50, 30));
        ext.on_turn_end("sess-1");
        ext.on_session_end("sess-1");
    }
}