Skip to content

fix(ts): keep InvokeOptions.limits caps across hook-triggered resume - #4982

Open
BlueX888 wants to merge 1 commit into
strands-agents:mainfrom
BlueX888:fix/prep-limits-caps-reset-on-hook-resume
Open

BlueX888 wants to merge 1 commit into
strands-agents:mainfrom
BlueX888:fix/prep-limits-caps-reset-on-hook-resume

Conversation

@BlueX888

@BlueX888 BlueX888 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Human Overview

Description

InvokeOptions.limits applies its caps to "the whole request this invoke() / stream() call starts" (strands-ts/src/types/agent.ts). A hook that sets AfterInvocationEvent.resume re-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 onto InvocationContext and _shouldResume already refuses a hook resume once a cap is reached, the rebase drops the now-redundant cap guard. The remaining change starts Meter.startNewInvocation() once per public call in _streamInvocation instead of once per pass in _stream, so latestAgentInvocation, its usage, and the invocation counter describe the whole invoke() / 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:

  • A hook that resumes on every pass while each cap (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 own endTurn stop reason.
  • A hook that resumes once without limits: the call records one invocation entry with two cycles.

On current main the second case fails (two invocation entries); with this change it passes. The focused src/agent/__tests__ tests, ESLint, Prettier, and tsc --noEmit pass locally.

Additional Details

Commands run and results (strands-ts)
node strands-ts/node_modules/vitest/vitest.mjs run --root strands-ts --project unit-node src/agent/__tests__/agent.test.ts
# Tests  139 passed (139)

node strands-ts/node_modules/vitest/vitest.mjs run --root strands-ts --project unit-node src/agent/__tests__/agent.test.ts src/agent/__tests__/agent.invocation-context.test.ts
# Tests  154 passed (154)

node strands-ts/node_modules/vitest/vitest.mjs run --root strands-ts --project unit-node
# Test Files  187 passed | 5 skipped (192)
# Tests  5200 passed | 137 skipped (5337)

npx eslint src/agent/agent.ts src/agent/__tests__/agent.test.ts   # passed
npx prettier --check src/agent/agent.ts src/agent/__tests__/agent.test.ts   # passed
npx tsc --noEmit --project src/tsconfig.json   # passed

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@BlueX888
BlueX888 requested a review from a team as a code owner October 7, 2026 19:23
@BlueX888
BlueX888 requested a review from Unshure October 7, 2026 19:23
@github-actions github-actions Bot added typescript Pull requests that update typescript code bug Something isn't working area-hooks Features or requests that might be implementable via hooks area-agent Related to the agent class or general agent questions labels Oct 7, 2026
@BlueX888 BlueX888 changed the title fix(agent): keep invoke() limits counting across hook-resumed passes fix(python): deduplicate usage nudges via stdlib warnings module Oct 12, 2026
@BlueX888 BlueX888 changed the title fix(python): deduplicate usage nudges via stdlib warnings module fix(ts): keep InvokeOptions.limits caps across hook-triggered resume Oct 12, 2026
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.
@BlueX888
BlueX888 force-pushed the fix/prep-limits-caps-reset-on-hook-resume branch from d5dc96e to 76e6071 Compare October 20, 2026 21:14
@BlueX888

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (0a99337e). The old head conflicted with #4667 in strands-ts/src/agent/agent.ts.

Since #4667, limits are enforced through InvocationContext and _shouldResume already declines a hook resume once a cap is reached, so the rebase drops the now-redundant cap-guard hunk. The remaining change starts Meter.startNewInvocation() once per public call in _streamInvocation instead of once per pass in _stream, so latestAgentInvocation, its usage, and the invocation counter cover resumed passes instead of only the last pass.

Tests on the new head 76e6071a8: strands-ts/src/agent/__tests__/agent.test.ts 139 passed; full unit-node suite 5200 passed, 5 files skipped; ESLint, Prettier, and tsc --noEmit pass. On current main the new per-call metrics case fails with two invocation entries; the cap cases already pass upstream.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-agent Related to the agent class or general agent questions area-hooks Features or requests that might be implementable via hooks bug Something isn't working typescript Pull requests that update typescript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant