Skip to content

fix(reflect): keep facts read through expand in citations and based_on - #5677

Merged
nicoloboschi merged 1 commit into
mainfrom
fix/5474-reflect-expanded-provenance
Oct 16, 2026
Merged

nicoloboschi merged 1 commit into
mainfrom
fix/5474-reflect-expanded-provenance

Conversation

@nicoloboschi

Copy link
Copy Markdown
Collaborator

Fixes #5474.

Problem

Reflect only let the agent cite facts that recall returned. When the agent read a fact through expand and cited it, done dropped the id. With no valid citation left, based_on fell back to every recalled fact — so the answer was credited to a fact it never used.

Fix

returned_memories() (in reflect/tools.py) returns the facts a recall or expand call actually handed back, as MemoryFact. Ids expand reported missing, malformed or outside the tag scope carry no memory and stay uncitable. Three readers use it:

  • the done citation allowlist (reflect/agent.py)
  • based_on assembly in reflect_async
  • _count_retrieved_facts (refresh retrieval counts)

No API, schema or config change.

Tests

  • New system story test_47_expanded_source_provenance.py: reflect over world facts recalls "Alice moved to Berlin"; the stub LLM expands the experience fact "agent booked a flight to Munich" and cites it. On main it fails with based_on = the Berlin fact; with the fix based_on = only the flight.
  • Unit: test_expanded_memory_is_citable (expanded id citable, missing id dropped) and test_retrieval_counts_include_expanded_memories.
  • Related system stories 40, 81, 94 pass; lint passes.

Left as is

When the model cites ids and all are invalid, based_on still falls back to every recalled fact. That's a separate behaviour decision (noted on the issue).

#5474)

Reflect only treated facts returned by recall as citable. A fact the agent
read through expand was dropped from done(memory_ids), and with no valid
citation left, based_on fell back to every recalled fact — crediting a fact
the answer never used.

returned_memories() now gives the facts a recall or expand call actually
returned, as MemoryFact. Ids expand reported missing, malformed or out of
tag scope stay uncitable. It feeds the three readers: the done citation
allowlist, based_on assembly in reflect_async, and refresh retrieval counts.

Tests: system story test_47 (fails on main: based_on credits the wrong
fact), agent citation unit test, retrieval-count unit test.
@nicoloboschi
nicoloboschi merged commit 45616ff into main Oct 16, 2026
117 checks passed
@nicoloboschi
nicoloboschi deleted the fix/5474-reflect-expanded-provenance branch October 16, 2026 13:55
@nicoloboschi nicoloboschi mentioned this pull request Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reflect: expanded-only source is lost from citations and public based_on

1 participant