sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

bug(context-manager): a server-side summary can take the in-flight prompt

Open lizradway opened this issue 2 weeks ago • 0 comments

Summary

A server-side summary can take the message the next model call is responding to, so the model answers a summary of the prompt instead of the prompt.

Where

strands-ts/src/context-manager/strategies/offload/summarize.ts:

function serverSideEnd(messages: Message[], model: Model, removable: Message[]): number | undefined {
  const end = endOf(messages, removable)

endOf is the index just past the last of removable, taken raw. The client-side path does not have this problem — removeMessages runs removable through withPairs, which drops the last message and any tool use whose result is the last message. Only the server-side path reads removable directly.

Why the last message is in removable

collectRemovableWithPair is the function that protects it:

if (index === 0 || index === messages.length - 1) return []

But base.ts's _removableMessages consults it as a membership test, not as a veto:

const removable = unprotected.filter(
  (message) =>
    message !== messages[0] &&
    collectRemovableWithPair(messages, messages.indexOf(message)).every((removed) => unprotectedSet.has(removed))
)

For the last message collectRemovableWithPair returns [], and [].every(...) is true, so the last message passes the filter. The explicit messages[0] guard beside it is there for the same reason — index 0 also returns [].

Reproduction

An Anthropic model (server-side summary) with no preserveRecent:

const agent = new Agent({
  model: new AnthropicModel({ /* ... */ }),
  contextManager: {
    strategies: [Offload.summarize('*').when({ contextUtilization: '70%' })],
  },
})

Once utilization crosses 70% on a user turn, removable includes that user message, end is messages.length, and the summary block replaces everything after message 0 — including the prompt the model has not answered yet. The same shape takes a tool use whose result is the last message, orphaning the use.

Expected

The range should stop before any message that may not be removed:

const end = endOf(messages, withPairs(messages, removable))

Context

Found while porting #5325 to Python; fixed on the Python side in strands-agents/harness-sdk#6101 with exactly that change.

lizradway avatar Nov 16 '26 19:11 lizradway