Keep the `Memory` snapshot with the latest user prompt instead of after tool results

Merged
MP
2mo ago
+144-11

Memory appended its refreshed <memory> block to the last request. After a tool call, or when a run resumes after a deferred tool approval, that request holds only tool results, so the block was the only user-role text in it. Models read it as a new user turn: they acknowledged it or called write_memory with facts already stored, instead of reporting the tool result.

The block now goes on the request that holds the latest user prompt, as the issue proposed. It falls back to the last request when the history has no user prompt. It is still request-only and refreshed on every request.

  • The latest user prompt is found in the persisted history (ctx.messages), not in the request copy. wrap_model_request hooks run before before_model_request, and SystemReminders has already added its request-only <system-reminder> user part to the tool-result request by then, whichever order the capabilities are listed in. Searching the request copy would bring the bug back for those agents.
  • The prompt's index is mapped to the request list. Hooks that rewrite history, such as compaction, rewrite both lists, so the indexes line up. If the request list has diverged and that index no longer holds a user prompt, the block goes on the last request, which is the old behavior.

Caching trade-off

This reverses the tail placement chosen in #8996, so it needs a maintainer decision (see the triage on the issue, #9275, #9156).

  • While the snapshot is unchanged, each request in a tool loop now starts with the previous one, so prefix caching keeps working across tool calls.
  • When the snapshot changes (write_memory or an external edit), everything from the user prompt onward differs from the previous request. With the tail placement only the tail differed.
  • When a new run starts, the block moves from the previous prompt to the new one, so the content from the previous prompt onward changes once per run.
  • An explicit CachePoint that SystemReminders places before its tail reminder now covers the memory block, so a memory change invalidates that cached prefix.

inject_memory=False remains the cache-stable option.

Tests and docs

  • Regression tests for the issue's tool-call example, with and without SystemReminders, and for resuming after an approved deferred tool. All three fail without the change.
  • Tests for the two fallbacks: no user prompt at all, and a request list that no longer matches the history.
  • The new code in _capability.py has full branch coverage locally.
  • docs/harness/memory.md, the memory README.md, and the harness skill's KNOWLEDGE-AND-MEMORY.md describe the placement and its caching behavior.

An earlier revision of this PR kept the tail placement and added a note to the default guidance instead. That note is removed.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.