Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Merge #928: filter live terminal citations

justrach9a7d2f94c227
diff --cc docs/adr/0114-live-citations-are-terminal-presentation.md
index 00000000,410c7d95..facc6ba3
mode 000000,100644..100644
--- a/docs/adr/0114-live-citations-are-terminal-presentation.md
+++ b/docs/adr/0114-live-citations-are-terminal-presentation.md
@@@ -1,0 -1,15 +1,15 @@@
-# 0111. Live citation filtering belongs to terminal presentation
++# 0114. Live citation filtering belongs to terminal presentation
+
+ Status: accepted 2026-09-14
+
+ ## Context
+
+ Completed-text sanitization did not protect incremental reasoning, answers, or completion arguments (#874). A citation delimiter or its payload can cross any chunk boundary. Filtering individual chunks loses that context; filtering engine events would also change structured clients' input.
+
+ ## Decision
+
+ Apply the existing incremental citation recognizer at terminal display boundaries, before markdown rendering, reasoning replay buffers, or fullscreen live prose buffers. Keep reasoning state independent from answer state and reset it at stream start and completion, interruption, or transport failure. One-shot and hosted terminal output use the same recognition contract. Keep structured JSON and ACP serialization and raw tool output unchanged.
+
+ ## Consequences
+
+ The filter holds only delimiter-prefix bytes and suppression state, not citation payloads. Unclosed annotations are suppressed until the stream boundary; a fresh stream cannot inherit them. Each terminal adapter must retain lifecycle coverage. Real sink-dispatch regressions cover split delimiters, independent channels, reset recovery, and unchanged structured output; completed-text tests alone are insufficient.
diff --cc docs/adr/README.md
index eacbf395,2bd32551..5f46f4a9
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@@ -121,9 -121,7 +121,10 @@@ record only when you need the evidence
| [0108](0108-browser-events-outrank-pending-snapshots.md) | Browser page-info events invalidate older pending IPC snapshots; a loaded page must not revert to loading. |
| [0109](0109-resume-retains-catalog-and-layout-snapshots.md) | Resume restores loaded tool selections and the same-workspace layout snapshot, without freezing current instructions or granting permissions. |
| [0110](0110-project-mcp-listing-opts-in-optional-servers.md) | Project MCP entries opt in optional servers; inherited extras still require opt-in, and startup consent applies to both. |
-| [0111](0111-live-citations-are-terminal-presentation.md) | Live terminal citations use per-stream display filters with independent reasoning state; structured JSON/ACP output stays unchanged. |
+| [0111](0111-peer-inbox-retains-bodies-and-reports-loss.md) | Peer inbox owns complete bodies, reports and persists loss, and clears only after a complete read result is allocated. |
+| [0112](0112-mcp-wire-names-preserve-raw-routing.md) | MCP wire names are escaped and bounded; raw server/tool identities control policy and dispatch. |
+| [0113](0113-clipboard-errors-need-evidence.md) | Direct AppKit clipboard failures need typed evidence; generic helper failures never imply Automation permission. |
++| [0114](0114-live-citations-are-terminal-presentation.md) | Live terminal citations use per-stream display filters with independent reasoning state; structured JSON/ACP output stays unchanged. |
## When to write one
diff --cc src/agent.zig
index c8df567b,4d1cf6dc..49277a1c
--- a/src/agent.zig
+++ b/src/agent.zig
@@@ -131,8 -131,8 +131,9 @@@ pub const Agent = struct
model_calls_this_turn: u64 = 0,
handle_note_shown: bool = false, // #541: the handle-protocol lesson rides this agent's FIRST handle, once
seen_tool_keys: std.ArrayList([]const u8) = .empty, // root-only per-turn dedupe keys
+ cite_stream: @import("terminal_citations.zig").State = .{}, // display-only, independent prose channels
md_buf: std.ArrayList(u8) = .empty, // current incomplete streamed line (markdown rendering)
+ md_cite: @import("cite_markup.zig").Stream = .{}, // display-only, survives split citation markers
md_fence: bool = false, // inside a ``` code fence while streaming
md_kind: MdKind = .classify, // incremental renderer: current line's classification
md_span: MdSpan = .normal, // incremental renderer: inline **bold**/`code` span state
diff --cc src/agent_stream.zig
index 20495305,ec0ebe97..cfc105d5
--- a/src/agent_stream.zig
+++ b/src/agent_stream.zig
@@@ -486,10 -486,9 +486,9 @@@ pub fn printDelta(self: *Agent, raw_lin
},
};
if (no_ui) {
- if (oneshotShouldPaint(main_mod.unattended, main_mod.json_mode, text.len)) {
+ if (!self.stream_quiet and oneshotShouldPaint(main_mod.unattended, main_mod.json_mode, text.len)) {
if (main_mod.g_out) |w| {
- w.writeAll(text) catch {};
- w.flush() catch {};
+ @import("terminal_citations.zig").plain(&self.cite_stream.answer, w, text);
}
self.streamed_text = true;
self.traceFirstToken();
diff --cc src/engine_sink.zig
index c750acff,cb69e9ec..643cef0e
--- a/src/engine_sink.zig
+++ b/src/engine_sink.zig
@@@ -208,19 -208,14 +208,24 @@@ fn lifecycleEmit(ctx: *anyopaque, ev: S
/// text now shares the Markdown renderer so link targets exclude delimiters (#729).
fn tuiEmit(ctx: *anyopaque, ev: Stamped) void {
const a: *Agent = @ptrCast(@alignCast(ctx));
+ const citations = @import("terminal_citations.zig");
+ switch (ev.event) {
+ .stream_begin, .stream_complete, .stream_aborted, .stream_finished, .transport_aborted => a.cite_stream = .{},
+ else => {},
+ }
+ // Compaction uses the live transport too, but its reply is bookkeeping,
+ // not a new answer. Gate presentation, including the hosted fallback.
+ if (a.stream_quiet) switch (ev.event) {
+ .reasoning_delta, .text_delta, .tool_arg_delta, .stream_complete => return,
+ else => {},
+ };
if (hosted_frontend) return hostedEmit(a, ev.event);
switch (ev.event) {
- .stream_begin => render.spinnerStart(a),
+ .stream_begin => {
+ a.md_cite = .{};
+ a.thinking_cite = .{};
+ render.spinnerStart(a);
+ },
// Reasoning streams into the live dimmed "Thinking" block when
// /thinking is on for a live, colored root turn; otherwise the
// spinner stands in for it. TRANSITIONAL (slice-1 debt, see header):
diff --cc src/main.zig
index 090bc08d,507ba998..d9bab7ba
--- a/src/main.zig
+++ b/src/main.zig
@@@ -562,7 -563,7 +562,7 @@@ const agentJobsReap = subagent.agentJob
const workflow = @import("workflow_test.zig"); // the engine's tests live here (workflow.zig hit the 600-line cap)
const exec = @import("exec.zig");
test { // ── Unit tests (`zig build test`): pull in tests from imported modules (mcp.zig)
- _ = .{ @import("mcp_apps.zig"), @import("html_view.zig"), @import("providers_confirmation_tests.zig"), @import("repl_model_confirmation_tests.zig"), @import("repl_model_pick_tests.zig") };
- _ = .{ @import("mcp_apps.zig"), @import("html_view.zig"), @import("engine_sink_citation_tests.zig"), @import("cite_markup_stream_tests.zig"), @import("oneshot_citation_tests.zig"), @import("tui_acp.zig") };
++ _ = .{ @import("mcp_apps.zig"), @import("html_view.zig"), @import("providers_confirmation_tests.zig"), @import("repl_model_confirmation_tests.zig"), @import("repl_model_pick_tests.zig"), @import("engine_sink_citation_tests.zig"), @import("cite_markup_stream_tests.zig"), @import("oneshot_citation_tests.zig"), @import("tui_acp.zig") };
_ = @import("main_test.zig");
_ = @import("session_catalog.zig");
_ = @import("session_prompt.zig");