Skip to content

fix(memory): normalize restored history to a Converse-valid sequence#548

Open
dtaniwaki wants to merge 1 commit into
aws:mainfrom
dtaniwaki:worktree-session-restore-tail-normalization
Open

fix(memory): normalize restored history to a Converse-valid sequence#548
dtaniwaki wants to merge 1 commit into
aws:mainfrom
dtaniwaki:worktree-session-restore-tail-normalization

Conversation

@dtaniwaki

@dtaniwaki dtaniwaki commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Thanks for maintaining this integration! Would appreciate your review of the fix below.

Fixes #547.

Problem

With filter_restored_tool_context=True, stripping tool blocks drops a toolResult-only user turn, which leaves the surrounding assistant turns adjacent and breaks Converse's role alternation. The next invocation then fails with an assistant-prefill ValidationException (full details in #547).

Solution

After stripping tool blocks, _filter_restored_tool_context now merges adjacent same-role turns and drops leading non-user turns, so the restored history alternates strictly and starts on a user turn:

restored (after strip):  user(q) → assistant(text) → assistant(answer)
normalized:              user(q) → assistant([text, answer])
+ runtime appends user:  user(q) → assistant([text, answer]) → user(next)   ✅ Converse-valid

The trailing turn is left as-is, since the runtime appends the next user turn before the model call (forcing a user tail here would create two adjacent user turns). Scoped to the existing filter_restored_tool_context opt-in, so behavior is unchanged unless enabled.

Note on merging

Merging is lossy: collapsing two assistant turns into one drops the turn boundary, and the merged message keeps a single event id (the earlier turn's id is not retained). This is consistent with what the flag already does — it discards the tool round-trip from restored history — and merging preserves all text content (concatenated as separate content blocks) rather than dropping a turn. Happy to take a different approach if you'd prefer the lost context handled another way.

Signed-off-by: Daisuke Taniwaki <daisuketaniwaki@gmail.com>
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.

Restored history with filter_restored_tool_context can be Converse-invalid (assistant-prefill ValidationException)

1 participant