Repository navigation
Conversation
An AfterInvocationEvent hook that sets resume re-enters the agent loop within the same public stream() call. Each re-entry called Meter.startNewInvocation(), so latestAgentInvocation and the invocation counter described only the last pass, and the per-invocation usage in the returned metrics covered only that pass. Start the invocation entry once per public call, matching the request scope used by InvocationContext, so resumed passes accumulate into the same entry. The added test keeps the caps pinned across resumed passes.
d5dc96e to
76e6071
Compare
|
Rebased onto current Since #4667, limits are enforced through Tests on the new head |
Human Overview
Description
InvokeOptions.limitsapplies its caps to "the whole request thisinvoke()/stream()call starts" (strands-ts/src/types/agent.ts). A hook that setsAfterInvocationEvent.resumere-enters the agent loop inside that same public call, and every re-entry used to start a new metrics invocation entry in_stream.This branch is rebased onto current
main. Since #4667 moved limit enforcement ontoInvocationContextand_shouldResumealready refuses a hook resume once a cap is reached, the rebase drops the now-redundant cap guard. The remaining change startsMeter.startNewInvocation()once per public call in_streamInvocationinstead of once per pass in_stream, solatestAgentInvocation, its usage, and the invocation counter describe the wholeinvoke()/stream()call rather than only its last pass.Related Issues
None — no existing issue tracks this.
Documentation PR
None needed; the change keeps the metrics scope aligned with the documented per-call contract.
Type of Change
Bug fix
Testing
Added regression cases to
strands-ts/src/agent/__tests__/agent.test.ts:turns,totalTokens,outputTokens) trips on the first pass: the call ends after a single model call. The cap suppresses the resume; the result keeps the pass's ownendTurnstop reason.On current
mainthe second case fails (two invocation entries); with this change it passes. The focusedsrc/agent/__tests__tests, ESLint, Prettier, andtsc --noEmitpass locally.Additional Details
Commands run and results (strands-ts)
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.