saya-cli 0.3.2

Database-aware AI agent for the terminal: full-screen TUI, schema discovery, and bounded read-only SQL over PostgreSQL, MySQL, SQLite, DuckDB, and Snowflake.
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
use crate::{RenderFormat, TerminalEvent, render::Rendered, render_event};
use async_trait::async_trait;
use saya_agent::{AgentEvent, AgentEventSink};
use std::{
    io::{self, Write},
    sync::Mutex,
};

pub(crate) struct TerminalSink {
    format: RenderFormat,
    text_open: Mutex<bool>,
}
impl TerminalSink {
    pub(crate) fn new(format: RenderFormat) -> Self {
        Self {
            format,
            text_open: Mutex::new(false),
        }
    }
}

#[async_trait]
impl AgentEventSink for TerminalSink {
    async fn emit(&self, event: AgentEvent) {
        let rendered = render_agent(
            event,
            self.format,
            &mut self.text_open.lock().expect("terminal state"),
        );
        print!("{}", rendered.stdout);
        eprint!("{}", rendered.stderr);
        let _ = io::stdout().flush();
        let _ = io::stderr().flush();
    }
}

fn render_agent(event: AgentEvent, format: RenderFormat, text_open: &mut bool) -> Rendered {
    let Some(event) = terminal_event(event) else {
        // Nothing to print, and nothing to close: a progress signal must not
        // terminate an open text block the way a real event would.
        return Rendered {
            stdout: String::new(),
            stderr: String::new(),
        };
    };
    let close = matches!(format, RenderFormat::Text)
        && *text_open
        && !matches!(event, TerminalEvent::AssistantText { .. });
    let mut rendered = render_event(&event, format);
    if close && !matches!(event, TerminalEvent::Complete) {
        rendered.stdout.insert(0, '\n');
        *text_open = false;
    }
    match event {
        TerminalEvent::AssistantText { ref text } if matches!(format, RenderFormat::Text) => {
            *text_open = !text.is_empty()
        }
        TerminalEvent::Complete if matches!(format, RenderFormat::Text) => {
            *text_open = false;
            if !close {
                rendered.stdout.clear();
            }
        }
        _ => {}
    }
    rendered
}

/// Maps one agent event to what the headless renderer should print, or `None`
/// when the event is a pure progress signal with nothing to say here.
///
/// The `None` case exists because the catch-all below is deliberately loud: a
/// variant this renderer does not understand must surface as
/// `NotImplemented` rather than silently end the stream. That is right for an
/// event carrying content, and wrong for one that carries none — a spinner
/// label is meaningful to the TUI and meaningless to a pipe. Without this
/// distinction, adding a progress event prints `unrecognized agent event` at
/// the user, which has now happened three times (see the arms below).
pub(crate) fn terminal_event(event: AgentEvent) -> Option<TerminalEvent> {
    Some(match event {
        AgentEvent::AssistantText { text } => TerminalEvent::AssistantText { text },
        AgentEvent::ToolRequested { name, arguments } => {
            let detail = crate::agent::tools::tool_call_detail(&name, &arguments);
            TerminalEvent::ToolRequested { name, detail }
        }
        AgentEvent::ToolCompleted { name, summary } => {
            TerminalEvent::ToolCompleted { name, summary }
        }
        AgentEvent::ToolDenied { name, reason } => TerminalEvent::ToolDenied { name, reason },
        AgentEvent::KnowledgeSupplied {
            outcome,
            contracts,
            dropped_by_bounds,
        } => TerminalEvent::KnowledgeSupplied {
            outcome,
            contracts,
            dropped_by_bounds,
        },
        AgentEvent::KnowledgeOverridden { findings } => {
            TerminalEvent::KnowledgeOverridden { findings }
        }
        // Extraction timed out or errored after the turn succeeded — surface it
        // rather than fall through to the `unrecognized agent event` catch-all
        // (spec packet-54 decision 4: an event with no renderer previously
        // printed that, and repeating it would be worse than the bug being
        // fixed).
        AgentEvent::KnowledgeLearningSkipped { reason } => {
            TerminalEvent::KnowledgeLearningSkipped { reason }
        }
        // Learning used to fall through to the catch-all below and print
        // `unrecognized agent event` — an error string at the exact moment the
        // product did the thing it is for.
        AgentEvent::KnowledgeProposed { claim } => TerminalEvent::KnowledgeLearned { claim },
        // Progress only: the TUI labels its spinner with this, and a pipe has
        // no spinner to label. Dropped rather than rendered — not forgotten,
        // which is what the catch-all would make of it.
        AgentEvent::KnowledgeLearningStarted => return None,
        AgentEvent::Complete => TerminalEvent::Complete,
        // AgentEvent is #[non_exhaustive]; a future variant this renderer does not
        // yet understand must not silently terminate the stream (Complete) — surface
        // it as an unimplemented event instead.
        _ => TerminalEvent::NotImplemented {
            feature: "unrecognized agent event".into(),
        },
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::render::{RenderFormat, render_event};
    use saya_agent::{
        KnowledgeOutcome, LearningSkipReason, OverrideFindingDto, SuppliedClaimDto,
        SuppliedContractDto,
    };
    use saya_types::{ClaimId, ClaimStatus};

    fn dto_claim(id: &str, kind: &str, value: &str, status: ClaimStatus) -> SuppliedClaimDto {
        SuppliedClaimDto {
            claim_id: ClaimId::parse(id).unwrap(),
            kind: kind.into(),
            value: value.into(),
            column: None,
            status,
        }
    }

    fn dto_contract(claims: Vec<SuppliedClaimDto>) -> SuppliedContractDto {
        SuppliedContractDto {
            profile: "analytics".into(),
            object: "catalog.public.orders".into(),
            schema_state: "current".into(),
            claims,
        }
    }

    /// KnowledgeSupplied maps to a real TerminalEvent variant (not
    /// NotImplemented) and renders through the text adapter (spec P1c §5).
    #[test]
    fn knowledge_supplied_renders_through_the_text_adapter() {
        let event = AgentEvent::knowledge_supplied(
            KnowledgeOutcome::Ran {
                store_unavailable: false,
            },
            vec![dto_contract(vec![
                dto_claim("c-1", "table_alias", "orders", ClaimStatus::Confirmed),
                dto_claim(
                    "c-2",
                    "default_time_column",
                    "created_at",
                    ClaimStatus::Candidate,
                ),
            ])],
            0,
        );
        let rendered = render_agent(event, RenderFormat::Text, &mut false);
        // The compact header, the unconfirmed count, and the per-claim lines all
        // reach stdout through the adapter.
        assert!(
            rendered
                .stdout
                .contains("memory supplied · 2 claims (1 unconfirmed)"),
            "{:?}",
            rendered.stdout
        );
        assert!(
            rendered.stdout.contains("table_alias  orders  confirmed"),
            "{:?}",
            rendered.stdout
        );
        assert!(
            rendered.stdout.contains("candidate  (unconfirmed)"),
            "{:?}",
            rendered.stdout
        );
        assert_eq!(rendered.stderr, "");
    }

    /// The three outcomes render distinguishably through the text adapter, and
    /// Ran-and-found-nothing is silent (spec §5 / §4).
    #[test]
    fn text_adapter_distinguishes_the_three_outcomes() {
        let off = render_agent(
            AgentEvent::knowledge_supplied(KnowledgeOutcome::Off, Vec::new(), 0),
            RenderFormat::Text,
            &mut false,
        );
        let skipped = render_agent(
            AgentEvent::knowledge_supplied(KnowledgeOutcome::Skipped, Vec::new(), 0),
            RenderFormat::Text,
            &mut false,
        );
        let ran_empty = render_agent(
            AgentEvent::knowledge_supplied(
                KnowledgeOutcome::Ran {
                    store_unavailable: false,
                },
                Vec::new(),
                0,
            ),
            RenderFormat::Text,
            &mut false,
        );
        assert_eq!(off.stdout, "memory off · recall disabled\n");
        assert_eq!(
            skipped.stdout,
            "memory skipped · not permitted to read saved claims\n"
        );
        assert_eq!(ran_empty.stdout, "", "Ran-and-found-nothing is silent");
        assert_ne!(off.stdout, skipped.stdout);
    }

    /// A non-zero dropped count is visible through the text adapter (spec §5).
    #[test]
    fn text_adapter_shows_a_nonzero_dropped_count() {
        let rendered = render_agent(
            AgentEvent::knowledge_supplied(
                KnowledgeOutcome::Ran {
                    store_unavailable: false,
                },
                vec![dto_contract(vec![dto_claim(
                    "c-1",
                    "table_alias",
                    "orders",
                    ClaimStatus::Confirmed,
                )])],
                30,
            ),
            RenderFormat::Text,
            &mut false,
        );
        assert!(
            rendered.stdout.contains("· 30 more dropped by bounds"),
            "{:?}",
            rendered.stdout
        );
    }

    /// The JSON/NDJSON adapter carries the event under its type tag rather than
    /// the NotImplemented fallback (spec §2: every adapter that renders events).
    #[test]
    fn json_adapter_carries_the_event_under_its_type_tag() {
        let event = AgentEvent::knowledge_supplied(
            KnowledgeOutcome::Ran {
                store_unavailable: false,
            },
            vec![dto_contract(vec![dto_claim(
                "c-1",
                "table_alias",
                "orders",
                ClaimStatus::Confirmed,
            )])],
            0,
        );
        let te = terminal_event(event).expect("this event renders headlessly");
        let rendered = render_event(&te, RenderFormat::Json);
        assert!(
            rendered.stdout.contains(r#""event":"knowledge_supplied""#),
            "{:?}",
            rendered.stdout
        );
        // The NotImplemented fallback is not what the JSON adapter emits here.
        assert!(
            !rendered.stdout.contains("not_implemented"),
            "{:?}",
            rendered.stdout
        );
    }

    #[test]
    fn text_status_closes_an_open_delta_line_once() {
        let mut open = false;
        assert_eq!(
            render_agent(
                AgentEvent::assistant_text("thinking"),
                RenderFormat::Text,
                &mut open
            )
            .stdout,
            "thinking"
        );
        assert_eq!(
            render_agent(
                AgentEvent::tool_requested("schema", serde_json::Value::Null),
                RenderFormat::Text,
                &mut open
            )
            .stdout,
            "\nUsing read-only tool: schema\n"
        );
        assert_eq!(
            render_agent(AgentEvent::complete(), RenderFormat::Text, &mut open).stdout,
            ""
        );
    }
    #[test]
    fn ndjson_keeps_delta_and_complete_envelopes() {
        let mut open = false;
        assert_eq!(
            render_agent(
                AgentEvent::assistant_text("x"),
                RenderFormat::Ndjson,
                &mut open
            )
            .stdout,
            "{\"event\":\"assistant_text\",\"text\":\"x\"}\n"
        );
        assert_eq!(
            render_agent(AgentEvent::complete(), RenderFormat::Ndjson, &mut open).stdout,
            "{\"event\":\"complete\"}\n"
        );
    }
    #[test]
    fn text_complete_closes_an_open_delta_line_once() {
        let mut open = false;
        let _ = render_agent(
            AgentEvent::assistant_text("done"),
            RenderFormat::Text,
            &mut open,
        );
        assert_eq!(
            render_agent(AgentEvent::complete(), RenderFormat::Text, &mut open).stdout,
            "\n"
        );
    }

    /// KnowledgeOverridden maps to a real TerminalEvent variant (not
    /// NotImplemented) and renders through the text adapter (spec A1 §3).
    #[test]
    fn knowledge_overridden_renders_through_the_text_adapter() {
        let event = AgentEvent::knowledge_overridden(vec![OverrideFindingDto {
            claim_id: ClaimId::parse("c-rental-time").unwrap(),
            kind: "default_time_column".into(),
            claimed_value: "return_date".into(),
            observed_columns: vec!["rental_date".into()],
        }]);
        let rendered = render_agent(event, RenderFormat::Text, &mut false);
        assert!(
            rendered.stdout.contains("memory overridden · 1 finding"),
            "{:?}",
            rendered.stdout
        );
        assert!(
            rendered.stdout.contains("referenced rental_date"),
            "{:?}",
            rendered.stdout
        );
        // The wording constraint: no causal "used" about the time column.
        assert!(
            !rendered.stdout.contains("used"),
            "the text adapter must not assert a causal 'used': {:?}",
            rendered.stdout
        );
        assert_eq!(rendered.stderr, "");
    }

    /// The JSON/NDJSON adapter carries KnowledgeOverridden under its type tag
    /// rather than the NotImplemented fallback (spec §2: every adapter).
    #[test]
    fn json_adapter_carries_the_overridden_event_under_its_type_tag() {
        let event = AgentEvent::knowledge_overridden(vec![OverrideFindingDto {
            claim_id: ClaimId::parse("c-rental-time").unwrap(),
            kind: "default_time_column".into(),
            claimed_value: "return_date".into(),
            observed_columns: vec!["rental_date".into()],
        }]);
        let te = terminal_event(event).expect("this event renders headlessly");
        let rendered = render_event(&te, RenderFormat::Json);
        assert!(
            rendered
                .stdout
                .contains(r#""event":"knowledge_overridden""#),
            "{:?}",
            rendered.stdout
        );
        assert!(
            !rendered.stdout.contains("not_implemented"),
            "{:?}",
            rendered.stdout
        );
    }

    /// KnowledgeLearningSkipped maps to a real TerminalEvent variant (not
    /// NotImplemented) and renders the spec line through the text adapter
    /// (packet-54 decision 4 — both adapters render it).
    #[test]
    fn knowledge_learning_skipped_renders_through_the_text_adapter() {
        let event = AgentEvent::knowledge_learning_skipped(LearningSkipReason::TimedOut);
        let rendered = render_agent(event, RenderFormat::Text, &mut false);
        assert!(
            rendered
                .stdout
                .contains("memory not recorded · extraction timed out"),
            "text adapter renders the timeout line: {:?}",
            rendered.stdout
        );
        assert_eq!(rendered.stderr, "");

        let event = AgentEvent::knowledge_learning_skipped(LearningSkipReason::Failed);
        let rendered = render_agent(event, RenderFormat::Text, &mut false);
        assert!(
            rendered
                .stdout
                .contains("memory not recorded · extraction failed"),
            "text adapter renders the failure line: {:?}",
            rendered.stdout
        );
    }

    /// The JSON/NDJSON adapter carries KnowledgeLearningSkipped under its type
    /// tag rather than the NotImplemented fallback (packet-54 decision 4: every
    /// adapter that renders events — the headless path previously would have
    /// printed `unrecognized agent event`).
    #[test]
    fn json_adapter_carries_the_learning_skipped_event_under_its_type_tag() {
        let event = AgentEvent::knowledge_learning_skipped(LearningSkipReason::TimedOut);
        let te = terminal_event(event).expect("this event renders headlessly");
        let rendered = render_event(&te, RenderFormat::Json);
        assert!(
            rendered
                .stdout
                .contains(r#""event":"knowledge_learning_skipped""#),
            "type tag: {:?}",
            rendered.stdout
        );
        assert!(
            rendered.stdout.contains(r#""reason":"timed_out""#),
            "reason carried: {:?}",
            rendered.stdout
        );
        assert!(
            !rendered.stdout.contains("not_implemented"),
            "must not fall through to NotImplemented: {:?}",
            rendered.stdout
        );
    }

    /// A progress-only event must render to nothing, not to
    /// `unrecognized agent event`. The catch-all is deliberately loud so a
    /// content-bearing variant cannot be dropped silently, which means every
    /// contentless one needs an explicit arm — this has been missed three times
    /// (`KnowledgeLearningSkipped`, `KnowledgeProposed`, and
    /// `KnowledgeLearningStarted`, which printed the error string during a live
    /// `saya ask` while the whole suite was green).
    #[test]
    fn progress_only_events_render_to_nothing_not_to_an_error() {
        assert!(
            terminal_event(AgentEvent::KnowledgeLearningStarted).is_none(),
            "a progress signal must not reach the headless renderer"
        );

        // And the loud path still works for a variant that does carry content.
        let complete = terminal_event(AgentEvent::Complete).expect("Complete renders");
        assert!(
            !matches!(complete, TerminalEvent::NotImplemented { .. }),
            "a known content event must not fall through to the catch-all"
        );
    }
}