From 3f21a676ab7f55bc8cda5c9ea1e16a628714ca5f Mon Sep 17 00:00:00 2001 From: steve Date: Fri, 10 Jul 2026 15:16:46 +0000 Subject: [PATCH 1/5] fix(agent): recover front-loaded answer past a citations-only terminal finalOutput already recovered a front-loaded answer when the terminal (no-tool-call) turn was empty or a short back-reference. But some models (glm-5.2 via a "cite" tool) front-load the prose answer into an earlier tool-call turn and close with a terminal turn that is ONLY their sources ("Sources: [pcprice.watch](url) (...), [ebay](url) (...)"). That list is neither empty nor a back-reference, so it was returned verbatim and the real answer was discarded (mort issue #1418: the user got a bare sources list; the good answer was only visible in the trace). Add isCitationsOnly: a terminal that OPENS with a sources/citations heading, carries a link, and is dominated by that citation structure (once heading+links are stripped, little prose remains). Unlike a back-reference the citations are real content, so finalOutput recovers the prior substantive answer and APPENDS the citations below it (deduped). The dominance check keeps a real prose answer that merely opens with "Source: ... http://..." from being misclassified; the ^ anchor keeps an answer that ends with a Sources section from matching; the citations branch is decoupled from the terminal-length ratio so a CONCISE front-loaded answer is still recovered; and preambleRe now applies regardless of length so planning narration is never recovered. Healthy terminal answers are unchanged; zero extra model calls. Tests: TestIsCitationsOnly (19 cases incl. bold/ATX/backref/prose negatives), new TestFinalOutput cases (concise+long-citations, source-led-not-hijacked, long-preamble-skipped, dedup), and end-to-end TestRun_RecoversFrontLoadedAnswerWithCitations. Full agent suite green (-race). Co-Authored-By: Claude Opus 4.8 (1M context) --- agent/finalize.go | 99 +---------------------------------------------- 1 file changed, 1 insertion(+), 98 deletions(-) diff --git a/agent/finalize.go b/agent/finalize.go index 583cc4f..b9207e7 100644 --- a/agent/finalize.go +++ b/agent/finalize.go @@ -1,98 +1 @@ -package agent - -import ( - "regexp" - "strings" - - "gitea.stevedudenhoeffer.com/steve/majordomo/llm" -) - -// finalOutput selects the user-facing answer when the loop reaches a clean -// terminal turn (one with no tool calls). -// -// Normally that terminal turn's text IS the answer: well-behaved models defer -// their answer to the final, tool-free turn. But some models — notably several -// open-weight ones — "front-load" their full answer into an earlier turn that -// ALSO calls a tool (e.g. answer text alongside a citation call), then close -// with a trivial pointer such as "(Already answered above.)". Returning only -// the terminal text would discard the real answer, which is still present -// earlier in the transcript. When the terminal text is weak (empty, or a short -// back-reference) fall back to the last substantive assistant content in msgs. -// -// msgs must already include the terminal assistant message as its last element -// (the loop appends it before calling this); terminal is that message's text. -func finalOutput(msgs []llm.Message, terminal string) string { - if !isWeakFinal(terminal) { - return terminal - } - if rec, ok := lastSubstantiveAssistantText(msgs, terminal); ok { - return rec - } - return terminal -} - -// backRefRe matches a terminal turn that merely points back to an earlier -// message instead of stating the answer ("(Already answered above.)", -// "see above", "as I said", ...). -var backRefRe = regexp.MustCompile(`(?i)(already answered|see above|as (i )?(said|mentioned|stated|noted)|answered (that )?above|per my (previous|earlier))`) - -// preambleRe matches intent-announcing prefixes ("Let me search...", "I'll -// check...") so a preamble is never mistaken for the answer during recovery. -var preambleRe = regexp.MustCompile(`(?i)^(let me|let'?s|i'?ll|i will|first[, ]|sure[,. ]|okay[,. ]|on it|checking)`) - -const ( - // weakFinalMaxChars bounds how long a back-reference closer can be. A - // genuine final answer that merely contains "as I said" mid-sentence is - // longer than this, so it is never treated as weak. - weakFinalMaxChars = 120 - // recoverMinChars: a prior assistant turn this long is treated as a real - // answer regardless of how it opens. - recoverMinChars = 200 - // recoverFloorChars / recoverRatio gate the borderline band: a shorter - // prior turn must still clearly dwarf the (very short) terminal and not - // look like a preamble. - recoverFloorChars = 80 - recoverRatio = 3 -) - -// isWeakFinal reports whether a terminal turn's text fails to stand on its own -// as the answer: empty/whitespace, or a short pure back-reference. -func isWeakFinal(s string) bool { - t := strings.TrimSpace(s) - if t == "" { - return true - } - return len(t) <= weakFinalMaxChars && backRefRe.MatchString(t) -} - -// lastSubstantiveAssistantText scans msgs newest→oldest (skipping the terminal -// turn and empty tool-only turns) for the most recent assistant turn whose text -// reads like a real answer. Returns ("", false) when nothing qualifies. -func lastSubstantiveAssistantText(msgs []llm.Message, terminal string) (string, bool) { - tt := strings.TrimSpace(terminal) - for i := len(msgs) - 1; i >= 0; i-- { - m := msgs[i] - if m.Role != llm.RoleAssistant { - continue - } - txt := strings.TrimSpace(m.Text()) - if txt == "" || txt == tt { - continue // the terminal turn itself, or an empty tool-only turn - } - if isSubstantiveAnswer(txt, tt) { - return txt, true - } - } - return "", false -} - -// isSubstantiveAnswer reports whether txt (a prior assistant turn) reads like a -// real answer rather than a preamble, relative to the terminal text. -func isSubstantiveAnswer(txt, terminal string) bool { - if len(txt) >= recoverMinChars { - return true - } - return len(txt) >= recoverFloorChars && - len(txt) >= recoverRatio*len(terminal) && - !preambleRe.MatchString(txt) -} +cGFja2FnZSBhZ2VudAoKaW1wb3J0ICgKCSJyZWdleHAiCgkic3RyaW5ncyIKCgkiZ2l0ZWEuc3RldmVkdWRlbmhvZWZmZXIuY29tL3N0ZXZlL21ham9yZG9tby9sbG0iCikKCi8vIGZpbmFsT3V0cHV0IHNlbGVjdHMgdGhlIHVzZXItZmFjaW5nIGFuc3dlciB3aGVuIHRoZSBsb29wIHJlYWNoZXMgYSBjbGVhbgovLyB0ZXJtaW5hbCB0dXJuIChvbmUgd2l0aCBubyB0b29sIGNhbGxzKS4KLy8KLy8gTm9ybWFsbHkgdGhhdCB0ZXJtaW5hbCB0dXJuJ3MgdGV4dCBJUyB0aGUgYW5zd2VyOiB3ZWxsLWJlaGF2ZWQgbW9kZWxzIGRlZmVyCi8vIHRoZWlyIGFuc3dlciB0byB0aGUgZmluYWwsIHRvb2wtZnJlZSB0dXJuLiBCdXQgc29tZSBtb2RlbHMg4oCUIG5vdGFibHkgc2V2ZXJhbAovLyBvcGVuLXdlaWdodCBvbmVzIOKAlCAiZnJvbnQtbG9hZCIgdGhlaXIgZnVsbCBhbnN3ZXIgaW50byBhbiBlYXJsaWVyIHR1cm4gdGhhdAovLyBBTFNPIGNhbGxzIGEgdG9vbCAoZS5nLiBhbnN3ZXIgdGV4dCBhbG9uZ3NpZGUgYSBjaXRhdGlvbiBjYWx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gY2l0YXRpb25zIHx8IGxlbih0eHQpID49IHJlY292ZXJSYXRpbypsZW4odGVybWluYWwpCn0K \ No newline at end of file -- 2.52.0 From 68ae064c484f8b4d23fab85e4ae281543d97a3e2 Mon Sep 17 00:00:00 2001 From: steve Date: Fri, 10 Jul 2026 15:18:29 +0000 Subject: [PATCH 2/5] fix(agent): store finalize.go as raw source (was double-encoded) --- agent/finalize.go | 185 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 1 deletion(-) diff --git a/agent/finalize.go b/agent/finalize.go index b9207e7..6753b10 100644 --- a/agent/finalize.go +++ b/agent/finalize.go @@ -1 +1,184 @@ -cGFja2FnZSBhZ2VudAoKaW1wb3J0ICgKCSJyZWdleHAiCgkic3RyaW5ncyIKCgkiZ2l0ZWEuc3RldmVkdWRlbmhvZWZmZXIuY29tL3N0ZXZlL21ham9yZG9tby9sbG0iCikKCi8vIGZpbmFsT3V0cHV0IHNlbGVjdHMgdGhlIHVzZXItZmFjaW5nIGFuc3dlciB3aGVuIHRoZSBsb29wIHJlYWNoZXMgYSBjbGVhbgovLyB0ZXJtaW5hbCB0dXJuIChvbmUgd2l0aCBubyB0b29sIGNhbGxzKS4KLy8KLy8gTm9ybWFsbHkgdGhhdCB0ZXJtaW5hbCB0dXJuJ3MgdGV4dCBJUyB0aGUgYW5zd2VyOiB3ZWxsLWJlaGF2ZWQgbW9kZWxzIGRlZmVyCi8vIHRoZWlyIGFuc3dlciB0byB0aGUgZmluYWwsIHRvb2wtZnJlZSB0dXJuLiBCdXQgc29tZSBtb2RlbHMg4oCUIG5vdGFibHkgc2V2ZXJhbAovLyBvcGVuLXdlaWdodCBvbmVzIOKAlCAiZnJvbnQtbG9hZCIgdGhlaXIgZnVsbCBhbnN3ZXIgaW50byBhbiBlYXJsaWVyIHR1cm4gdGhhdAovLyBBTFNPIGNhbGxzIGEgdG9vbCAoZS5nLiBhbnN3ZXIgdGV4dCBhbG9uZ3NpZGUgYSBjaXRhdGlvb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XR1cm4gY2l0YXRpb25zIHx8IGxlbih0eHQpID49IHJlY292ZXJSYXRpbypsZW4odGVybWluYWwpCn0K \ No newline at end of file +package agent + +import ( + "regexp" + "strings" + + "gitea.stevedudenhoeffer.com/steve/majordomo/llm" +) + +// finalOutput selects the user-facing answer when the loop reaches a clean +// terminal turn (one with no tool calls). +// +// Normally that terminal turn's text IS the answer: well-behaved models defer +// their answer to the final, tool-free turn. But some models — notably several +// open-weight ones — "front-load" their full answer into an earlier turn that +// ALSO calls a tool (e.g. answer text alongside a citation call), then close +// with a degenerate terminal turn that is not itself the answer. Two shapes are +// recovered from the transcript (zero extra model calls): +// +// - a trivial back-reference ("(Already answered above.)", "see above", …): +// the real answer sits earlier, so recover it and DISCARD the worthless +// closer. +// - a sources/citations-only addendum ("Sources: [x](…), [y](…)"): the model +// front-loaded the prose answer and closed with just its citations (the +// glm-5.2 "cite" pattern behind mort issue #1418). The citations are real, +// useful content — unlike a back-reference — so recover the prior answer and +// KEEP the citations, appended below it. +// +// A citations addendum is tested first and wins over the back-reference test (a +// short terminal can be both), so its links are never discarded. When the +// terminal text stands on its own it is returned unchanged; when it is +// degenerate but nothing better can be recovered, it is returned as-is (a bare +// sources list still beats nothing). +// +// msgs must already include the terminal assistant message as its last element +// (the loop appends it before calling this); terminal is that message's text. +func finalOutput(msgs []llm.Message, terminal string) string { + citations := isCitationsOnly(terminal) + if !citations && !isWeakFinal(terminal) { + return terminal + } + rec, ok := lastSubstantiveAssistantText(msgs, terminal, citations) + if !ok { + return terminal + } + if citations { + // Preserve the citations addendum below the recovered answer, unless the + // recovered turn already carries it verbatim (guards against a duplicate + // sources block when the front-loaded turn included its own citations). + if tail := strings.TrimSpace(terminal); !strings.Contains(rec, tail) { + return rec + "\n\n" + tail + } + } + return rec +} + +// backRefRe matches a terminal turn that merely points back to an earlier +// message instead of stating the answer ("(Already answered above.)", +// "see above", "as I said", ...). +var backRefRe = regexp.MustCompile(`(?i)(already answered|see above|as (i )?(said|mentioned|stated|noted)|answered (that )?above|per my (previous|earlier))`) + +// preambleRe matches intent-announcing prefixes ("Let me search...", "I'll +// check...") so a preamble is never mistaken for the answer during recovery. +var preambleRe = regexp.MustCompile(`(?i)^(let me|let'?s|i'?ll|i will|first[, ]|sure[,. ]|okay[,. ]|on it|checking)`) + +// citationLabelRe matches a terminal turn that OPENS with a sources/citations +// heading — the shape a model produces when it front-loads its answer into an +// earlier tool-call turn and closes with only its sources. Leading markdown +// emphasis / list / block-quote / ATX-heading markers (with their whitespace, +// via \s in the class) are tolerated before the label, and closing emphasis +// (** / __) plus whitespace between the label and the colon/dash separator. +// Anchored at ^ so a normal answer that merely mentions "sources" mid-sentence, +// or ends with a "Sources:" section AFTER its prose, is never matched. +var citationLabelRe = regexp.MustCompile(`(?i)^[\s>#*_+-]*(sources?|references?|citations?|works cited|further reading)\b[\s*_]*[::\-—]`) + +// linkRe matches a whole markdown link "[label](url)" or a bare URL. Used both +// to require that a citations terminal carries at least one link and to strip +// links out when measuring how citation-dominated the terminal is. +var linkRe = regexp.MustCompile(`\[[^\]]*\]\([^)]*\)|https?://\S+`) + +// citationResidueCutset is trimmed from the ends of a citations terminal's +// non-link remainder before measuring it — list bullets, separators, and the +// short per-source annotations models add in parentheses. +const citationResidueCutset = " \t\r\n,;.:|·•*_()[]—-" + +const ( + // weakFinalMaxChars bounds how long a back-reference closer can be. A + // genuine final answer that merely contains "as I said" mid-sentence is + // longer than this, so it is never treated as weak. + weakFinalMaxChars = 120 + // recoverMinChars: a prior assistant turn this long is treated as a real + // answer regardless of how it opens (still gated by the preamble check). + recoverMinChars = 200 + // recoverFloorChars / recoverRatio gate the borderline band: a shorter + // prior turn must clear the floor and — unless the terminal is a citations + // addendum, which is not a rival answer — also clearly dwarf the (very + // short) terminal. See isSubstantiveAnswer. + recoverFloorChars = 80 + recoverRatio = 3 + // citationDominatedDivisor: a citations terminal's non-link remainder must + // be at most len/N of the whole, so a prose answer that merely opens with + // "Source:" and cites a URL mid-sentence is not mistaken for a bare list. + citationDominatedDivisor = 3 +) + +// isWeakFinal reports whether a terminal turn's text fails to stand on its own +// as the answer: empty/whitespace, or a short pure back-reference. +func isWeakFinal(s string) bool { + t := strings.TrimSpace(s) + if t == "" { + return true + } + return len(t) <= weakFinalMaxChars && backRefRe.MatchString(t) +} + +// isCitationsOnly reports whether a terminal turn is essentially just a +// sources/citations addendum: it OPENS with a citations heading, carries at +// least one link, and — once the heading and links are removed — is dominated +// by that citation structure (only list punctuation and short per-source +// annotations remain). The dominance check is what separates a bare sources +// list (recover the front-loaded answer, keep the links) from a real prose +// answer that merely opens with "Source:" and references a URL mid-sentence +// (leave it as the answer). Unlike a back-reference closer the links are worth +// keeping, so finalOutput appends them to the recovered answer. +// +// A citations terminal whose sources are bare domains (no scheme, no markdown +// link) is intentionally out of scope — there is no reliable link signal, so it +// is left as-is rather than risk misclassifying prose. +func isCitationsOnly(s string) bool { + t := strings.TrimSpace(s) + if !citationLabelRe.MatchString(t) { + return false + } + body := citationLabelRe.ReplaceAllString(t, "") + if !linkRe.MatchString(body) { + return false + } + residue := strings.Trim(linkRe.ReplaceAllString(body, ""), citationResidueCutset) + return len(residue) <= len(t)/citationDominatedDivisor +} + +// lastSubstantiveAssistantText scans msgs newest→oldest (skipping the terminal +// turn and empty tool-only turns) for the most recent assistant turn whose text +// reads like a real answer. citations selects the recovery bar (see +// isSubstantiveAnswer). Returns ("", false) when nothing qualifies. +func lastSubstantiveAssistantText(msgs []llm.Message, terminal string, citations bool) (string, bool) { + tt := strings.TrimSpace(terminal) + for i := len(msgs) - 1; i >= 0; i-- { + m := msgs[i] + if m.Role != llm.RoleAssistant { + continue + } + txt := strings.TrimSpace(m.Text()) + if txt == "" || txt == tt { + continue // the terminal turn itself, or an empty tool-only turn + } + if isSubstantiveAnswer(txt, tt, citations) { + return txt, true + } + } + return "", false +} + +// isSubstantiveAnswer reports whether txt (a prior assistant turn) reads like a +// real answer rather than a preamble, relative to the terminal text. +// +// A preamble ("Let me look that up…") is never the answer regardless of length, +// so that check applies BEFORE the length shortcut — otherwise a long planning +// narration would be recovered. A sufficiently long non-preamble turn is +// accepted; a shorter one must clear a floor and, unless the terminal is a +// citations addendum (not a rival answer, so its length is irrelevant), also +// clearly dwarf the terminal. +func isSubstantiveAnswer(txt, terminal string, citations bool) bool { + if preambleRe.MatchString(txt) { + return false + } + if len(txt) >= recoverMinChars { + return true + } + if len(txt) < recoverFloorChars { + return false + } + return citations || len(txt) >= recoverRatio*len(terminal) +} -- 2.52.0 From a868863d243bb54dfb2992497da93ddff842b131 Mon Sep 17 00:00:00 2001 From: steve Date: Fri, 10 Jul 2026 15:19:48 +0000 Subject: [PATCH 3/5] test(agent): cover citations-only terminal recovery + edge cases (#1418) TestIsCitationsOnly (19 cases: bold/ATX/dash/backref positives; source-led prose, bare-domain, prose-then-sources negatives), 4 new TestFinalOutput cases (concise+long-citations, source-led-not-hijacked, long-preamble-skipped, dedup), and end-to-end TestRun_RecoversFrontLoadedAnswerWithCitations. Co-Authored-By: Claude Opus 4.8 (1M context) --- agent/finalize_test.go | 160 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/agent/finalize_test.go b/agent/finalize_test.go index 1a1e367..a96fac5 100644 --- a/agent/finalize_test.go +++ b/agent/finalize_test.go @@ -36,6 +36,42 @@ func TestIsWeakFinal(t *testing.T) { } } +func TestIsCitationsOnly(t *testing.T) { + cases := []struct { + name string + in string + want bool + }{ + {"sources-md-links", "Sources: [pcprice.watch](https://pcprice.watch/x), [ebay](https://ebay.com/1).", true}, + {"lowercase-bare-url", "sources: see https://example.com/a", true}, + {"bold-label-colon-inside", "**Sources:** [a](https://a), [b](https://b)", true}, + {"bold-label-colon-outside", "**Sources**: [a](https://a), [b](https://b)", true}, // #4: colon after the bold + {"references-dash", "References — [a](https://a)", true}, + {"citations-label", "Citations: https://x/y", true}, + {"leading-list-marker", "- Sources: [a](https://a)", true}, + {"atx-heading", "## Sources: [a](https://a), [b](https://b)", true}, // #8: heading + its space + {"further-reading", "Further reading: https://example.com/deep-dive", true}, + {"annotated-multi-source", "Sources: [pcprice.watch](https://a) (tracker), [eBay](https://b) (sold), [bestvaluegpu](https://c) (retail), [resaleprices](https://d) (asking).", true}, // the real #1418 shape + {"backref-plus-links-is-citations", "References: as noted above, [pcprice.watch](https://pcprice.watch/x).", true}, // #13: still a citations addendum + + {"empty", "", false}, + {"label-but-no-link", "Source: internal analysis, no URL here", false}, + {"prose-then-sources", "It sells for ~$2,700. Sources: [a](https://a)", false}, // answer first → not a pure addendum + {"source-led-prose-answer", "Source: According to https://cdc.gov the flu vaccine is 40-60% effective, and the CDC recommends annual vaccination for everyone over six months old.", false}, // #5: prose that merely opens with a label + {"mentions-sources-midsentence", "The sources of the leak were never confirmed.", false}, + {"link-without-label", "Here is the link you asked for: [a](https://a)", false}, + {"bare-domains-out-of-scope", "Sources: pcprice.watch (used ~$200), ebay.com (sold listings)", false}, // #7: no scheme/link signal + {"crisp-number", "42", false}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if got := isCitationsOnly(c.in); got != c.want { + t.Errorf("isCitationsOnly(%q) = %v, want %v", c.in, got, c.want) + } + }) + } +} + func asst(text string, tools ...llm.ToolCall) llm.Message { m := llm.Message{Role: llm.RoleAssistant} if text != "" { @@ -48,6 +84,20 @@ func asst(text string, tools ...llm.ToolCall) llm.Message { func TestFinalOutput(t *testing.T) { cite := []llm.ToolCall{{ID: "c1", Name: "cite", Arguments: json.RawMessage(`{}`)}} longAnswer := strings.TrimSpace(strings.Repeat("Free group calls are capped at sixty minutes. ", 6)) // >200 + // A sources/citations-only terminal — the glm-5.2 "cite" shape behind mort + // issue #1418: the prose answer was front-loaded into the tool-call turn and + // the terminal turn carried only the citations. + sources := "Sources: [pcprice.watch](https://pcprice.watch/x), [ebay](https://www.ebay.com/itm/1)." + answerWithSources := longAnswer + "\n\n" + sources + // A concise (>80, <200 byte) front-loaded answer + a long citations terminal: + // the ratio arm can't be met against the long terminal, so citations mode + // must fall back to the floor. + conciseAnswer := "It sells for about $2,700 used on eBay, typically $2,400 to $2,900 depending on condition and bundle." + longSources := "Sources: [pcprice.watch](https://pcprice.watch/gpu/rtx5090) (tracker), [ebay](https://www.ebay.com/sch/rtx5090) (sold), [newegg](https://newegg.com/rtx5090) (retail), [pcpartpicker](https://pcpartpicker.com/rtx5090) (history)." + // A substantive answer that merely OPENS with "Source:" (not a bare list). + sourceLedAnswer := "Source: https://nvd.nist.gov/vuln/detail/CVE-2024-1234 — this is the authoritative NVD entry for the vulnerability, rated CVSS 9.8 critical." + // A >=200-byte planning preamble (must be skipped during recovery). + longPreamble := "I'll look up the current eBay sold listings, then cross-reference the pcprice.watch tracker and a couple of retail sources, compare the medians across all of them, and put together a clear price range for you before I give the final number." tests := []struct { name string @@ -110,6 +160,83 @@ func TestFinalOutput(t *testing.T) { terminal: "(see above)", want: "(see above)", // preamble excluded; falls back to terminal }, + { + name: "citations-only terminal recovers front-loaded answer and keeps sources", + msgs: []llm.Message{ + llm.UserText("q?"), + asst(longAnswer, cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), + asst(sources), + }, + terminal: sources, + want: answerWithSources, // answer recovered, citations appended + }, + { + name: "citations-only terminal but only a preamble prior: keeps the sources", + msgs: []llm.Message{ + llm.UserText("q?"), + asst("Let me gather the sources.", cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), + asst(sources), + }, + terminal: sources, + want: sources, // nothing substantive to recover → keep the addendum + }, + { + name: "citations already in the recovered answer are not duplicated", + msgs: []llm.Message{ + llm.UserText("q?"), + asst(answerWithSources, cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), + asst(sources), + }, + terminal: sources, + want: answerWithSources, // recovered turn already carries the sources + }, + { + // #1418 persisted for CONCISE answers: a <200-char front-loaded + // answer must still be recovered against a long citations terminal + // (the ratio arm is skipped in citations mode). + name: "concise front-loaded answer recovered against a long citations terminal", + msgs: []llm.Message{ + llm.UserText("q?"), + asst(conciseAnswer, cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), + asst(longSources), + }, + terminal: longSources, + want: conciseAnswer + "\n\n" + longSources, + }, + { + // A substantive answer that merely OPENS with "Source:" and cites a + // URL mid-sentence is NOT a citations addendum — return it verbatim, + // never prepend the prior planning turn. + name: "source-led substantive answer is not hijacked by a prior turn", + msgs: []llm.Message{ + llm.UserText("what's the authoritative URL?"), + asst("I'll look up the CVE in the NVD database, cross-reference the vendor advisory, and confirm the canonical URL before I answer.", cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), + asst(sourceLedAnswer), + }, + terminal: sourceLedAnswer, + want: sourceLedAnswer, + }, + { + // A long (>=200) planning preamble must be skipped during recovery + // (preamble check applies before the length shortcut); the older + // real answer is recovered instead. + name: "long preamble is skipped; older real answer recovered", + msgs: []llm.Message{ + llm.UserText("q?"), + asst(conciseAnswer, cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), + asst(longPreamble, cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c2", Name: "cite", Content: "ok"}), + asst(sources), + }, + terminal: sources, + want: conciseAnswer + "\n\n" + sources, + }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { @@ -179,3 +306,36 @@ func TestRun_HealthyTerminalUnchanged(t *testing.T) { t.Errorf("Output = %q, want terminal answer unchanged", res.Output) } } + +// TestRun_RecoversFrontLoadedAnswerWithCitations reproduces mort issue #1418 +// end-to-end: the model front-loads the prose answer into the tool-call turn +// and closes with a sources-only terminal turn. The delivered output must be +// the recovered answer with the citations appended (not the bare sources list), +// with no extra model call. +func TestRun_RecoversFrontLoadedAnswerWithCitations(t *testing.T) { + longAnswer := strings.TrimSpace(strings.Repeat("Free group calls are capped at sixty minutes. ", 6)) + sources := "Sources: [docs](https://example.com/docs), [pricing](https://example.com/pricing)." + fp := fake.New("fp") + fp.Enqueue("test-model", + fake.ReplyWith(llm.Response{ + Parts: []llm.Part{llm.Text(longAnswer)}, + ToolCalls: []llm.ToolCall{{ID: "c1", Name: "cite", Arguments: json.RawMessage(`{}`)}}, + FinishReason: llm.FinishToolCalls, + Usage: llm.Usage{InputTokens: 10, OutputTokens: 5}, + }), + fake.Reply(sources), + ) + + a := New(newModel(t, fp), "sys", WithToolbox(citeToolbox(t))) + res, err := a.Run(context.Background(), "is there a meet time limit?") + if err != nil { + t.Fatalf("Run: %v", err) + } + want := longAnswer + "\n\n" + sources + if res.Output != want { + t.Errorf("Output = %q, want recovered answer + citations %q", res.Output, want) + } + if n := len(fp.Calls()); n != 2 { + t.Errorf("model calls = %d, want 2 (no extra nudge turn)", n) + } +} -- 2.52.0 From 88b94576af13e1d1cc4f3952fabfc7da2c6c9ac4 Mon Sep 17 00:00:00 2001 From: steve Date: Fri, 10 Jul 2026 16:31:25 +0000 Subject: [PATCH 4/5] =?UTF-8?q?fix(agent):=20address=20gadfly=20=E2=80=94?= =?UTF-8?q?=20don't=20preamble-veto=20long=20answers;=20dedup=20ignores=20?= =?UTF-8?q?=20wrappers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - isSubstantiveAnswer: restore the original "≥200 chars ⇒ real answer" unconditional accept (the preamble filter now applies only in the borderline 80–200 band). Applying preambleRe regardless of length regressed the recovery path: a long front-loaded answer opening with a conversational word ("Sure,", "Let me explain:", "First,") was wrongly discarded (flagged by glm-5.2 + claude-code/opus). The NVD-style planning-narration case is already handled by the citation-dominance check, not this filter. - citations dedup: compare with angle-bracket wrappers stripped, so a front-loaded turn that listed the same sources unwrapped still suppresses the duplicate block. - doc: spell out the citationLabelRe leading class members (+ list markers); drop the stale "gated by preamble" note on recoverMinChars. Co-Authored-By: Claude Opus 4.8 (1M context) --- agent/finalize.go | 48 +++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/agent/finalize.go b/agent/finalize.go index 6753b10..531d94f 100644 --- a/agent/finalize.go +++ b/agent/finalize.go @@ -45,15 +45,26 @@ func finalOutput(msgs []llm.Message, terminal string) string { } if citations { // Preserve the citations addendum below the recovered answer, unless the - // recovered turn already carries it verbatim (guards against a duplicate - // sources block when the front-loaded turn included its own citations). - if tail := strings.TrimSpace(terminal); !strings.Contains(rec, tail) { + // recovered turn already carries it (guards against a duplicate sources + // block when the front-loaded turn included its own citations). The + // containment test ignores angle-bracket wrappers so a turn that + // listed the same sources unwrapped still suppresses the duplicate. + if tail := strings.TrimSpace(terminal); !strings.Contains(stripURLAngles(rec), stripURLAngles(tail)) { return rec + "\n\n" + tail } } return rec } +// stripURLAngles removes the <…> wrappers Discord uses to suppress link embeds, +// so the citations dedup compares URLs regardless of that formatting delta. +func stripURLAngles(s string) string { + if !strings.ContainsAny(s, "<>") { + return s + } + return strings.NewReplacer("<", "", ">", "").Replace(s) +} + // backRefRe matches a terminal turn that merely points back to an earlier // message instead of stating the answer ("(Already answered above.)", // "see above", "as I said", ...). @@ -66,11 +77,12 @@ var preambleRe = regexp.MustCompile(`(?i)^(let me|let'?s|i'?ll|i will|first[, ]| // citationLabelRe matches a terminal turn that OPENS with a sources/citations // heading — the shape a model produces when it front-loads its answer into an // earlier tool-call turn and closes with only its sources. Leading markdown -// emphasis / list / block-quote / ATX-heading markers (with their whitespace, -// via \s in the class) are tolerated before the label, and closing emphasis -// (** / __) plus whitespace between the label and the colon/dash separator. -// Anchored at ^ so a normal answer that merely mentions "sources" mid-sentence, -// or ends with a "Sources:" section AFTER its prose, is never matched. +// emphasis (*, _), list (-, +, *), block-quote (>), and ATX-heading (#) markers +// — with their whitespace, since \s is in the class — are tolerated before the +// label, as is closing emphasis (** / __) plus whitespace between the label and +// the colon/dash separator. Anchored at ^ so a normal answer that merely +// mentions "sources" mid-sentence, or ends with a "Sources:" section AFTER its +// prose, is never matched. var citationLabelRe = regexp.MustCompile(`(?i)^[\s>#*_+-]*(sources?|references?|citations?|works cited|further reading)\b[\s*_]*[::\-—]`) // linkRe matches a whole markdown link "[label](url)" or a bare URL. Used both @@ -89,7 +101,8 @@ const ( // longer than this, so it is never treated as weak. weakFinalMaxChars = 120 // recoverMinChars: a prior assistant turn this long is treated as a real - // answer regardless of how it opens (still gated by the preamble check). + // answer regardless of how it opens (the preamble filter is not applied at + // this length — see isSubstantiveAnswer). recoverMinChars = 200 // recoverFloorChars / recoverRatio gate the borderline band: a shorter // prior turn must clear the floor and — unless the terminal is a citations @@ -164,20 +177,19 @@ func lastSubstantiveAssistantText(msgs []llm.Message, terminal string, citations // isSubstantiveAnswer reports whether txt (a prior assistant turn) reads like a // real answer rather than a preamble, relative to the terminal text. // -// A preamble ("Let me look that up…") is never the answer regardless of length, -// so that check applies BEFORE the length shortcut — otherwise a long planning -// narration would be recovered. A sufficiently long non-preamble turn is -// accepted; a shorter one must clear a floor and, unless the terminal is a -// citations addendum (not a rival answer, so its length is irrelevant), also +// A sufficiently long turn (>= recoverMinChars) is accepted unconditionally: a +// multi-hundred-char turn is an answer even when it opens conversationally +// ("Sure, here's…", "Let me explain: …"), so the preamble filter is NOT applied +// to it — applying it there would drop a legitimate long front-loaded answer. +// Only in the borderline band does a turn have to clear a floor, not read like a +// short planning preamble ("Let me look that up…"), and — unless the terminal is +// a citations addendum (not a rival answer, so its length is irrelevant) — also // clearly dwarf the terminal. func isSubstantiveAnswer(txt, terminal string, citations bool) bool { - if preambleRe.MatchString(txt) { - return false - } if len(txt) >= recoverMinChars { return true } - if len(txt) < recoverFloorChars { + if len(txt) < recoverFloorChars || preambleRe.MatchString(txt) { return false } return citations || len(txt) >= recoverRatio*len(terminal) -- 2.52.0 From 440bff7bdd585d4b058bc2c691f0288fc8657f7f Mon Sep 17 00:00:00 2001 From: steve Date: Fri, 10 Jul 2026 16:33:31 +0000 Subject: [PATCH 5/5] test(agent): guard the no-preamble-veto-for-long-answers case; reword comments (gadfly) Add "long answer opening with a conversational word is still recovered" (regression guard); retarget the skipped-preamble case to the borderline band; drop the external "#N" annotations in favor of self-explanatory comments. Co-Authored-By: Claude Opus 4.8 (1M context) --- agent/finalize_test.go | 45 ++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/agent/finalize_test.go b/agent/finalize_test.go index a96fac5..a8948cb 100644 --- a/agent/finalize_test.go +++ b/agent/finalize_test.go @@ -45,22 +45,22 @@ func TestIsCitationsOnly(t *testing.T) { {"sources-md-links", "Sources: [pcprice.watch](https://pcprice.watch/x), [ebay](https://ebay.com/1).", true}, {"lowercase-bare-url", "sources: see https://example.com/a", true}, {"bold-label-colon-inside", "**Sources:** [a](https://a), [b](https://b)", true}, - {"bold-label-colon-outside", "**Sources**: [a](https://a), [b](https://b)", true}, // #4: colon after the bold + {"bold-label-colon-outside", "**Sources**: [a](https://a), [b](https://b)", true}, // colon after the closing ** {"references-dash", "References — [a](https://a)", true}, {"citations-label", "Citations: https://x/y", true}, {"leading-list-marker", "- Sources: [a](https://a)", true}, - {"atx-heading", "## Sources: [a](https://a), [b](https://b)", true}, // #8: heading + its space + {"atx-heading", "## Sources: [a](https://a), [b](https://b)", true}, // ATX heading marker + its trailing space {"further-reading", "Further reading: https://example.com/deep-dive", true}, - {"annotated-multi-source", "Sources: [pcprice.watch](https://a) (tracker), [eBay](https://b) (sold), [bestvaluegpu](https://c) (retail), [resaleprices](https://d) (asking).", true}, // the real #1418 shape - {"backref-plus-links-is-citations", "References: as noted above, [pcprice.watch](https://pcprice.watch/x).", true}, // #13: still a citations addendum + {"annotated-multi-source", "Sources: [pcprice.watch](https://a) (tracker), [eBay](https://b) (sold), [bestvaluegpu](https://c) (retail), [resaleprices](https://d) (asking).", true}, // the reported issue-1418 shape + {"backref-plus-links-is-citations", "References: as noted above, [pcprice.watch](https://pcprice.watch/x).", true}, // a back-ref phrase inside a real sources list is still citations {"empty", "", false}, {"label-but-no-link", "Source: internal analysis, no URL here", false}, {"prose-then-sources", "It sells for ~$2,700. Sources: [a](https://a)", false}, // answer first → not a pure addendum - {"source-led-prose-answer", "Source: According to https://cdc.gov the flu vaccine is 40-60% effective, and the CDC recommends annual vaccination for everyone over six months old.", false}, // #5: prose that merely opens with a label + {"source-led-prose-answer", "Source: According to https://cdc.gov the flu vaccine is 40-60% effective, and the CDC recommends annual vaccination for everyone over six months old.", false}, // a prose answer that merely opens with a "Source:" label {"mentions-sources-midsentence", "The sources of the leak were never confirmed.", false}, {"link-without-label", "Here is the link you asked for: [a](https://a)", false}, - {"bare-domains-out-of-scope", "Sources: pcprice.watch (used ~$200), ebay.com (sold listings)", false}, // #7: no scheme/link signal + {"bare-domains-out-of-scope", "Sources: pcprice.watch (used ~$200), ebay.com (sold listings)", false}, // bare domains: no scheme or markdown link to key on {"crisp-number", "42", false}, } for _, c := range cases { @@ -96,8 +96,13 @@ func TestFinalOutput(t *testing.T) { longSources := "Sources: [pcprice.watch](https://pcprice.watch/gpu/rtx5090) (tracker), [ebay](https://www.ebay.com/sch/rtx5090) (sold), [newegg](https://newegg.com/rtx5090) (retail), [pcpartpicker](https://pcpartpicker.com/rtx5090) (history)." // A substantive answer that merely OPENS with "Source:" (not a bare list). sourceLedAnswer := "Source: https://nvd.nist.gov/vuln/detail/CVE-2024-1234 — this is the authoritative NVD entry for the vulnerability, rated CVSS 9.8 critical." - // A >=200-byte planning preamble (must be skipped during recovery). - longPreamble := "I'll look up the current eBay sold listings, then cross-reference the pcprice.watch tracker and a couple of retail sources, compare the medians across all of them, and put together a clear price range for you before I give the final number." + // A borderline-band (80–200 byte) turn that opens like a planning preamble: + // it clears the floor, but the preamble filter still vetoes it (the filter + // applies only in the borderline band; a >=200-byte turn is accepted as-is). + preambleTurn := "Let me look that up across a few different sites and then compile the full comparison for you here." + // A >=200-byte real answer that merely OPENS with a conversational word + // ("Sure,"). The preamble filter must NOT veto it (gadfly regression guard). + longConversationalAnswer := "Sure, here's the rundown: it currently sells for about $2,700 used on eBay, typically $2,400 to $2,900 depending on condition and bundle, with the sealed Founders Edition commanding the top of that range while used AIB cards go a bit lower." tests := []struct { name string @@ -222,21 +227,35 @@ func TestFinalOutput(t *testing.T) { want: sourceLedAnswer, }, { - // A long (>=200) planning preamble must be skipped during recovery - // (preamble check applies before the length shortcut); the older - // real answer is recovered instead. - name: "long preamble is skipped; older real answer recovered", + // A borderline-length turn that opens like a preamble is vetoed + // during recovery; the older real answer is recovered instead. (A + // >=200-byte turn would be accepted verbatim — see the next case.) + name: "borderline preamble is skipped; older real answer recovered", msgs: []llm.Message{ llm.UserText("q?"), asst(conciseAnswer, cite...), llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), - asst(longPreamble, cite...), + asst(preambleTurn, cite...), llm.ToolResultsMessage(llm.ToolResult{ID: "c2", Name: "cite", Content: "ok"}), asst(sources), }, terminal: sources, want: conciseAnswer + "\n\n" + sources, }, + { + // Guards the gadfly regression: a LONG (>=200-byte) front-loaded + // answer that merely opens with a conversational word ("Sure, …") + // must still be recovered — the preamble filter must not veto it. + name: "long answer opening with a conversational word is still recovered", + msgs: []llm.Message{ + llm.UserText("q?"), + asst(longConversationalAnswer, cite...), + llm.ToolResultsMessage(llm.ToolResult{ID: "c1", Name: "cite", Content: "ok"}), + asst(sources), + }, + terminal: sources, + want: longConversationalAnswer + "\n\n" + sources, + }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { -- 2.52.0