feat(agent): add Agent.shutdown() for scope-based cleanup - #4519
opieter-aws merged 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
2cf90dd to
7e87c79
Compare
Documentation Preview ReadyYour documentation preview has been successfully deployed! Changed pages: Updated at: 2026-09-22T22:03:00.265Z |
|
Assessment: Comment Clean, well-scoped change that gives memory flushing a proper lifecycle home and removes the manual Review Themes
Nicely done removing a footgun — the scope-based flush on both normal exit and throw is a real DX win. |
3463a88 to
a1c15ce
Compare
|
Thanks for the quick turnaround on the API-design thread — the revision lands it cleanly:
One non-blocking note on the Codecov patch report (81.81%, "1 missing + 1 partial"): I ran the shutdown/context-manager tests locally with |
Description
Memory extraction runs in the background and is turn-triggered, so a short run can end with its latest turns never persisted. Today the only fix is a hand-written
try { ... } finally { await agent.memoryManager?.flush() }at every call site, and on the async path there is no built-in place to hang that cleanup at all — the sync__call__already auto-flushes, butinvoke_async/invokedo not.This adds a public
shutdown()on the core SDKAgentthat runs the agent's end-of-life procedures (today: flush pending memory) — safe to call more than once and a no-op when there's nothing to release. Scope-based disposal (async within Python,await usingin TypeScript) delegates to it, so the flush runs on normal exit and on a thrown error without a manualfinally.shutdown()is deliberately named for the lifecycle stage, not memory, so future teardown has a single home.Public API Changes
Python —
strands.Agent:TypeScript —
Agent:Related Issues
N/A
Documentation PR
included
Type of Change
New feature
Testing
Python:
hatch test tests/strands/agent/test_agent.py— covers directshutdown(),async withnormal exit, exception propagation with flush, and the no-memory-manager no-op.TypeScript:
npm test(strands-ts) — covers directshutdown(),await usingnormal exit, scope-throw flush + re-throw, and the no-memory-manager no-op.End-to-end against a real
FileMemoryStoreunder a temp dir (mocked LLM, no AWS creds): confirmed memory files are written on scope exit and on throw with no manual flush, nothing persists without shutdown, and a 25-concurrent-agent stress run wrote all 25 files.Format/lint clean:
ruff format/ruff check/mypy(Python),prettier/lint/type-check(TypeScript).I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.