Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
fix: carry citation-numbering and research notes as system messages instead of inside assistant history content, so the model can't imitate them into its answers; renderer strips any leaked note as backstop
Murtaza Nasir205343b8afd4
diff --git a/src/services/inquire_agent.py b/src/services/inquire_agent.pyindex b31442ff..b52a0c64 100644--- a/src/services/inquire_agent.py+++ b/src/services/inquire_agent.py@@ -606,11 +606,15 @@ KEEP_VERBATIM_MESSAGES = 8 # last 4 turnsdef sanitize_history(message_history):- """Client history -> [{role, content}] with activity provenance folded in.-- Assistant messages also carry the citation numbering the UI actually- displayed (the client assigns numbers at render time), so a follow-up- like "tell me more about 5" resolves to what the user saw on screen.+ """Client history -> [{role, content, note?}].++ Assistant messages carry provenance out-of-band in ``note`` (research+ performed, plus the citation numbering the UI actually displayed, so a+ follow-up like "tell me more about 5" resolves to what the user saw).+ The note is injected as a SEPARATE system message at build time, never+ folded into the assistant's own content: models imitate the tail of+ their previous answers, and an inline annotation ends up reproduced+ verbatim at the bottom of new answers."""out = []for msg in message_history or []:@@ -620,9 +624,11 @@ def sanitize_history(message_history):content = msg.get('content')if role not in ('user', 'assistant') or not isinstance(content, str) or not content.strip():continue+ entry = {'role': role, 'content': content}if role == 'assistant':+ note_bits = []if msg.get('activity'):- content = f"{content}\n\n[research performed for this answer: {str(msg['activity'])[:300]}]"+ note_bits.append(f"research performed: {str(msg['activity'])[:300]}")sources = msg.get('sources')if isinstance(sources, list) and sources:lines = []@@ -637,15 +643,18 @@ def sanitize_history(message_history):title = str(s.get('title') or '')[:120]lines.append(f"{n} = {title} (recording {rid})")if lines:- content = (f"{content}\n\n[citations in this answer were displayed to the "- f"user numbered: {'; '.join(lines)}]")- out.append({'role': role, 'content': content})+ note_bits.append(f"citations were displayed to the user numbered: {'; '.join(lines)}")+ if note_bits:+ entry['note'] = ("Note about the assistant's previous answer (system record, "+ "never reproduce in answers): " + " | ".join(note_bits))+ out.append(entry)return outdef estimate_context_tokens(system_prompt, memory, history, toolbox_schemas):total = _estimate_tokens(system_prompt) + _estimate_tokens(memory or '')- total += sum(_estimate_tokens(m['content']) for m in history)+ total += sum(_estimate_tokens(m['content']) + _estimate_tokens(m.get('note', ''))+ for m in history)total += _estimate_tokens(json.dumps(toolbox_schemas))return total@@ -660,7 +669,10 @@ def compact_history(user_id, memory, history, keep_last=KEEP_VERBATIM_MESSAGES):return memory, history, 0to_fold = history[:-keep_last]kept = history[-keep_last:]- transcript = "\n\n".join(f"{m['role'].upper()}: {m['content']}" for m in to_fold)+ transcript = "\n\n".join(+ f"{m['role'].upper()}: {m['content']}"+ + (f"\n[{m['note']}]" if m.get('note') else '')+ for m in to_fold)prompt = f"""Summarize this conversation-so-far into a compact memory block that lets an assistant \continue the conversation seamlessly. Capture: topics discussed, questions asked and their answers (with \any recording titles/ids referenced), decisions or conclusions, and open threads. Be dense and factual; \@@ -822,7 +834,12 @@ def run_inquire_agent(app, user_id, user_ctx, user_message, message_history,if memory:msgs.append({'role': 'system','content': f"Conversation memory (earlier turns, summarized):\n{memory}"})- msgs.extend(history)+ for m in history:+ msgs.append({'role': m['role'], 'content': m['content']})+ # Provenance notes travel as system messages so the model+ # never mistakes them for part of its own answer style.+ if m.get('note'):+ msgs.append({'role': 'system', 'content': m['note']})msgs.append({'role': 'user', 'content': user_message})return msgsdiff --git a/templates/inquire.html b/templates/inquire.htmlindex a7b02a25..38b3c21d 100644--- a/templates/inquire.html+++ b/templates/inquire.html@@ -985,6 +985,15 @@// this mapping is sent back with the history.message.sources = [...refs.entries()].map(([id, e]) => ({ n: e.n, recording_id: Number(id), title: e.title }));}+ // Backstop: strip leaked system-record notes if the+ // model ever echoes them ("[citations in this answer+ // were displayed...]", "[research performed...]").+ [...tpl.content.querySelectorAll('p')].forEach(p => {+ const txt = p.textContent.trim();+ if (/^\[(citations in this answer|research performed|note about the assistant)/i.test(txt) && txt.endsWith(']')) {+ p.remove();+ }+ });// Strip a model-written trailing Sources/References// section (the interface appends its own). Removed// ONLY when everything after the heading is barediff --git a/tests/test_inquire_agent.py b/tests/test_inquire_agent.pyindex d5903ec1..5c0e9af4 100644--- a/tests/test_inquire_agent.py+++ b/tests/test_inquire_agent.py@@ -489,14 +489,16 @@ def test_sanitize_history_folds_activity_and_drops_junk():]out = ia.sanitize_history(history)assert len(out) == 2- assert out[1]['content'].startswith('a1')- assert 'research performed' in out[1]['content']- assert all(set(m) == {'role', 'content'} for m in out)+ # Provenance goes into the out-of-band note, NEVER into the assistant's+ # own content (models imitate their previous answers' tails).+ assert out[1]['content'] == 'a1'+ assert 'research performed' in out[1]['note']-def test_sanitize_history_folds_displayed_citation_numbering():+def test_sanitize_history_carries_displayed_citation_numbering_as_note():"""The UI numbers citations at render time; the mapping must reach the- model so 'tell me more about 5' resolves to what the user saw."""+ model (as a system-side note) so 'tell me more about 5' resolves to what+ the user saw — without polluting the assistant content it would mimic."""history = [{'role': 'assistant', 'content': 'answer text','sources': [{'n': 1, 'recording_id': 58, 'title': 'Integrating AI'},@@ -505,12 +507,32 @@ def test_sanitize_history_folds_displayed_citation_numbering():{'role': 'user', 'content': 'tell me more about 2'},]out = ia.sanitize_history(history)- folded = out[0]['content']- assert 'displayed to the user numbered' in folded- assert '1 = Integrating AI (recording 58)' in folded- assert '2 = Impact of AI (recording 73)' in folded- assert 'dropped' not in folded- assert set(out[0]) == {'role', 'content'}+ assert out[0]['content'] == 'answer text'+ note = out[0]['note']+ assert 'displayed to the user numbered' in note+ assert '1 = Integrating AI (recording 58)' in note+ assert '2 = Impact of AI (recording 73)' in note+ assert 'dropped' not in note+++def test_history_notes_become_system_messages_in_the_loop(data):+ os.environ['INQUIRE_AGENT_TOOL_MODE'] = 'prompt'+ history = [+ {'role': 'user', 'content': 'earlier question'},+ {'role': 'assistant', 'content': 'earlier answer', 'activity': '2 searches',+ 'sources': [{'n': 1, 'recording_id': data['r1'], 'title': 'Weekly Sync'}]},+ ]+ fake = FakeLLM([nonstream_response('follow-up answer')])+ events = _run(data, fake, history=history)+ msgs = fake.calls[0]['messages']+ assistant_msgs = [m for m in msgs if m['role'] == 'assistant']+ assert assistant_msgs[0]['content'] == 'earlier answer'+ notes = [m for m in msgs if m['role'] == 'system' and 'displayed to the user numbered' in m['content']]+ assert len(notes) == 1+ # The note follows its assistant message+ idx = msgs.index(assistant_msgs[0])+ assert msgs[idx + 1] == notes[0]+ assert any(e.get('delta') == 'follow-up answer' for e in events)def test_compact_history_uses_llm_and_keeps_tail(data, monkeypatch):