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

bug(context-manager): an eager blockThreshold takes media the model has not read

Open lizradway opened this issue 2 weeks ago • 0 comments

Summary

A blockThreshold rests on the model's token count, which reports image, document, video, and audio blocks as 0 tokens. A threshold can therefore never rule media out — every media block passes it. On the eager path that means a threshold-only strategy takes media before the model has read it, which is the exact failure Offload.<method>("media") rejects a threshold-only config to prevent.

Where

strands-ts/src/context-manager/strategies/offload/base.ts — the per-block threshold check. A strategy whose target is anything other than "*" and whose conditions carry neither contextUtilization nor preserveRecent runs on MessageAdded, i.e. before the model sees the message.

Reproduction

const agent = new Agent({
  contextManager: {
    strategies: [Offload.drop('*').when({ blockThreshold: 500 })],
  },
})

await agent('What is in this image?', { /* ...an image block... */ })

'*' is message-level, so use a per-block target to hit the eager path:

Offload.truncate(['tool::readFile']).when({ blockThreshold: 500 })

A readFile result holding an image is measured by its text alone. The image measures 0, passes the 500-token threshold, and is replaced with a placeholder on arrival — the model never reads it.

Expected

The eager path should leave media a threshold cannot size alone, the same way Offload.<method>('media') requires contextUtilization or preserveRecent. Media should still pass a threshold on the deferred path, where the model has already read it and a contextUtilization strategy is reclaiming the window.

Context

Found while porting #5325 to Python. strands-agents/harness-sdk#6101 closes the hole on the Python side:

def _exempt_from_threshold(self, block: ContentBlock, measured: int) -> bool:
    """Whether ``block`` is media a threshold cannot rule out, because the token count measures it as 0."""
    return measured == 0 and not self._runs_eagerly and _holds_media(block)

Filing rather than porting the hole, so the two SDKs can converge on the fixed behavior.

lizradway avatar Nov 16 '26 19:11 lizradway