bug(context-manager): two removals starting at the same message share a stash key
Summary
Stash.storeRemoved derives its key from the first message of the removed run, so two removals that begin at the same message write the same key. store overwrites unconditionally, so the earlier removal's content becomes unretrievable and its placeholder's ref resolves to the later run.
Where
strands-ts/src/context-manager/stash.ts:
async storeRemoved(removed: Message[]): Promise<string> {
const id = `removed-${removed[0]!.trackingId}`
await this.store(id, 0, encode(removed.map((message) => message.toJSON())))
return id
}
async store(id: string, blockIndex: number, data: Uint8Array): Promise<string> {
const key = formatStashKey({ id, index: blockIndex })
await this._storage.write(key, data) // no keep-existing guard
return key
}
Why two removals share a first message
A server-side summary always stands for the messages after message 0, so its run begins at messages[1]. A later client-side removal whose run also begins at messages[1] — or a second server-side summary — derives the same removed-<trackingId>. The first run's entry is replaced, and the placeholder still carrying its ref now resolves to the second run's messages.
trackingId being always populated in TypeScript does not help: the collision is between two removals that legitimately start at the same message, not between two id-less messages.
Reproduction
An Anthropic model (server-side summary), summarizing twice over a long conversation:
const agent = new Agent({
model: new AnthropicModel({ /* ... */ }),
contextManager: {
strategies: [Offload.summarize('*').when({ contextUtilization: '50%', preserveRecent: 2 })],
},
})
After the second summary, retrieveContext on the first summary's ref returns the second run's messages. The facts only the first run held are gone.
Expected
The reference should be fresh per removal rather than a function of where the run starts:
const id = `removed-${crypto.randomUUID()}`
Context
Found while porting #5325 to Python; fixed on the Python side in strands-agents/harness-sdk#6101 with exactly that change. Related TypeScript twins from the same port: #6195, #6196.
I'd like to take the TypeScript fix for this issue. My proposed scope is to give each storeRemoved call a fresh unique stash ID and add a regression test where two removals begin at the same rackingId, verifying that each reference still retrieves its own messages. I will keep the change within strands-ts. Please confirm this direction before I begin implementation, as required by the repository's contributor guidance.