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
//! Maps streamed agent events onto transcript blocks.
use super::table;
use super::transcript::{BlockKind, Transcript};
use saya_agent::AgentEvent;
/// Applies one streamed agent event to the transcript.
pub(crate) fn apply_event(transcript: &mut Transcript, event: AgentEvent) {
match event {
AgentEvent::AssistantText { text } => {
if !matches!(
transcript.blocks().last().map(|b| b.kind),
Some(BlockKind::Assistant)
) {
// first chunk of the answer: separate it from the tool/SQL lines above
if transcript
.blocks()
.last()
.is_some_and(|b| !b.text.is_empty())
{
transcript.push(BlockKind::System, String::new());
}
}
transcript.append_delta(BlockKind::Assistant, &text);
}
AgentEvent::ToolRequested { name, arguments } => {
if let Some(call) = crate::agent::tools::sql_tool_call(&name, &arguments) {
let header = match &call.target {
Some(t) => format!("SQL · {t}"),
None => "SQL".to_string(),
};
let body = call
.sql
.lines()
.map(|l| format!(" {l}"))
.collect::<Vec<_>>()
.join("\n");
let text = format!("{header}\n{body}");
transcript.push(BlockKind::Tool, text);
} else {
let line = match crate::agent::tools::tool_call_detail(&name, &arguments) {
Some(detail) => format!("→ {name}: {detail}"),
None => format!("→ {name}"),
};
transcript.push(BlockKind::Tool, line);
}
}
AgentEvent::ToolCompleted { name, summary } => {
transcript.push(BlockKind::Tool, format!("✓ {name}: {summary}"))
}
AgentEvent::ToolDenied { name, reason } => {
transcript.push(BlockKind::System, format!("✗ {name} denied: {reason}"))
}
// What memory supplied, shown before the answer streams (spec P1c §6: the
// TUI transcript is persistent scrollback, so a block pushed before the
// answer stays visible above it — it leads, and does not compete with the
// status-bar spinner). The shared shaper centralizes the wording; an empty
// result (Ran-and-found-nothing) is silence — push nothing.
AgentEvent::KnowledgeSupplied {
outcome,
contracts,
dropped_by_bounds,
} => {
let text =
crate::render::knowledge_supplied_text(outcome, &contracts, dropped_by_bounds);
if !text.is_empty() {
// Strip the trailing newline: the transcript stores line text
// without a delimiter and re-wraps per line; a trailing '\n' would
// push a blank line into the block.
transcript.push(BlockKind::System, text.trim_end_matches('\n'));
}
}
// A confirmed claim the turn's SQL contradicted (spec A1). Trails the
// answer — emitted after the loop — so a System block pushed here lands
// below the assistant text, where a "the SQL contradicted a confirmed
// claim" notice belongs. The shared shaper centralizes the wording; an
// empty finding set is silence (the runtime emits nothing, but this
// guards a directly-constructed event too).
AgentEvent::KnowledgeOverridden { findings } => {
let text = crate::render::knowledge_overridden_text(&findings);
if !text.is_empty() {
transcript.push(BlockKind::System, text.trim_end_matches('\n'));
}
}
// Extraction timed out or errored after the turn succeeded (spec
// packet-54). Trails the answer — emitted after the loop — so a System
// block pushed here lands below the assistant text, where "and I did
// not learn from this turn" belongs. Shares the shaper with the
// headless path so the wording lives in one place; the line is never
// empty for a known reason, so the block always pushes.
AgentEvent::KnowledgeLearningSkipped { reason } => {
let text = crate::render::learning_skipped_text(reason);
if !text.is_empty() {
transcript.push(BlockKind::System, text.trim_end_matches('\n'));
}
}
// One fact learned this turn. Trails the answer — the runtime emits it
// after the loop — so it lands below the assistant text, where "and I
// kept this" belongs. Shares the shaper with the headless path; an
// undescribable claim is silence, never a raw token.
AgentEvent::KnowledgeProposed { claim } => {
let text = crate::render::knowledge_learned_text(&claim);
if !text.is_empty() {
transcript.push(BlockKind::System, text.trim_end_matches('\n'));
}
}
AgentEvent::Complete => {
transcript.reformat_last(BlockKind::Assistant, table::format_markdown_tables);
}
_ => {}
}
}
#[cfg(test)]
mod tests {
use super::*;
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,
}
}
fn last_block_text(transcript: &Transcript) -> Option<&str> {
transcript.blocks().last().map(|b| b.text.as_str())
}
/// KnowledgeSupplied pushes a System block whose text names the supplied
/// claims (spec P1c §5). Asserts on the rendered transcript, not state.
#[test]
fn knowledge_supplied_pushes_a_system_block_with_the_claims() {
let mut t = Transcript::new();
apply_event(
&mut t,
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 block = last_block_text(&t).expect("a block was pushed");
assert!(
block.starts_with("memory supplied · 2 claims (1 unconfirmed)"),
"{block}"
);
assert!(block.contains("table_alias orders confirmed"), "{block}");
// The candidate is marked on its line.
assert!(block.contains("candidate (unconfirmed)"), "{block}");
// The block is a System block, not a Tool block.
assert_eq!(t.blocks().last().unwrap().kind, BlockKind::System);
}
/// The three outcomes are distinguishable in the transcript, and
/// Ran-and-found-nothing pushes nothing (silence) (spec §5 / §4).
#[test]
fn tui_distinguishes_the_three_outcomes() {
let mut t = Transcript::new();
apply_event(
&mut t,
AgentEvent::knowledge_supplied(KnowledgeOutcome::Off, Vec::new(), 0),
);
assert_eq!(last_block_text(&t), Some("memory off · recall disabled"));
let mut t = Transcript::new();
apply_event(
&mut t,
AgentEvent::knowledge_supplied(KnowledgeOutcome::Skipped, Vec::new(), 0),
);
assert_eq!(
last_block_text(&t),
Some("memory skipped · not permitted to read saved claims")
);
// Ran-and-found-nothing: nothing is pushed (silence).
let mut t = Transcript::new();
apply_event(
&mut t,
AgentEvent::knowledge_supplied(
KnowledgeOutcome::Ran {
store_unavailable: false,
},
Vec::new(),
0,
),
);
assert!(
t.blocks().is_empty(),
"Ran-nothing pushes nothing: {:?}",
t.blocks()
);
}
/// A non-zero dropped count is visible in the pushed block (spec §5).
#[test]
fn tui_shows_a_nonzero_dropped_count() {
let mut t = Transcript::new();
apply_event(
&mut t,
AgentEvent::knowledge_supplied(
KnowledgeOutcome::Ran {
store_unavailable: false,
},
vec![dto_contract(vec![dto_claim(
"c-1",
"table_alias",
"orders",
ClaimStatus::Confirmed,
)])],
30,
),
);
let block = last_block_text(&t).expect("a block was pushed");
assert!(block.contains("· 30 more dropped by bounds"), "{block}");
}
/// No opaque profile identity reaches the transcript: the profile name
/// appears, a fabricated identity does not (spec §5 / §4).
#[test]
fn tui_does_not_leak_an_opaque_identity() {
let fake_identity =
"sha256:deadbeefcafef00d1234567890abcdef1234567890abcdef1234567890abcdef";
let mut t = Transcript::new();
apply_event(
&mut t,
AgentEvent::knowledge_supplied(
KnowledgeOutcome::Ran {
store_unavailable: false,
},
vec![dto_contract(vec![dto_claim(
"c-1",
"table_alias",
"orders",
ClaimStatus::Confirmed,
)])],
0,
),
);
let block = last_block_text(&t).expect("a block was pushed");
assert!(block.contains("analytics"), "profile name appears: {block}");
assert!(!block.contains(fake_identity), "identity leaked: {block}");
}
/// KnowledgeOverridden pushes a System block whose text names the referenced
/// column and the specified value (spec A1 §3). Trails the answer — the
/// block lands below the assistant text in the transcript.
#[test]
fn knowledge_overridden_pushes_a_system_block_naming_the_finding() {
let mut t = Transcript::new();
apply_event(
&mut t,
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 block = last_block_text(&t).expect("a block was pushed");
assert!(
block.contains("memory overridden · 1 finding"),
"header: {block}"
);
assert!(
block.contains("referenced rental_date"),
"names the referenced column: {block}"
);
assert!(
block.contains("where you specified return_date"),
"names the specified value: {block}"
);
// The wording constraint: no causal "used" about the time column.
assert!(
!block.contains("used"),
"the TUI block must not assert a causal 'used': {block}"
);
assert_eq!(t.blocks().last().unwrap().kind, BlockKind::System);
}
/// An empty finding set pushes nothing — silence (spec A1 §3).
#[test]
fn an_empty_knowledge_overridden_event_pushes_nothing() {
let mut t = Transcript::new();
apply_event(&mut t, AgentEvent::knowledge_overridden(Vec::new()));
assert!(
t.blocks().is_empty(),
"no findings → no block: {:?}",
t.blocks()
);
}
/// KnowledgeLearningSkipped pushes a System block whose text names the skip
/// reason (packet-54 decision 4 — the TUI renders it, it does not fall
/// through to the catch-all that would drop it). Trails the answer.
#[test]
fn knowledge_learning_skipped_pushes_a_system_block_naming_the_reason() {
let mut t = Transcript::new();
apply_event(
&mut t,
AgentEvent::knowledge_learning_skipped(LearningSkipReason::TimedOut),
);
let block = last_block_text(&t).expect("a block was pushed");
assert!(
block.contains("memory not recorded · extraction timed out"),
"TUI block names the timeout: {block}"
);
assert_eq!(t.blocks().last().unwrap().kind, BlockKind::System);
let mut t = Transcript::new();
apply_event(
&mut t,
AgentEvent::knowledge_learning_skipped(LearningSkipReason::Failed),
);
let block = last_block_text(&t).expect("a block was pushed");
assert!(
block.contains("memory not recorded · extraction failed"),
"TUI block names the failure: {block}"
);
}
}