-
Notifications
You must be signed in to change notification settings - Fork 52
fix(templates): remove global agent singleton that leaks history across sessions #1314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,17 +173,12 @@ def create_agent(): | |
| hooks=[ConfigBundleHook()], | ||
| ) | ||
| {{else}} | ||
| _agent = None | ||
|
|
||
| def get_or_create_agent(): | ||
| global _agent | ||
| if _agent is None: | ||
| _agent = Agent( | ||
| model=load_model(), | ||
| system_prompt=DEFAULT_SYSTEM_PROMPT, | ||
| tools=tools | ||
| ) | ||
| return _agent | ||
| def create_agent(): | ||
| return Agent( | ||
| model=load_model(), | ||
| system_prompt=DEFAULT_SYSTEM_PROMPT, | ||
| tools=tools | ||
| ) | ||
| {{/if}} | ||
| {{/if}} | ||
|
|
||
|
|
@@ -197,11 +192,7 @@ async def invoke(payload, context): | |
| user_id = getattr(context, 'user_id', 'default-user') | ||
| agent = get_or_create_agent(session_id, user_id) | ||
| {{else}} | ||
| {{#if hasConfigBundle}} | ||
| agent = create_agent() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question / possible follow-up: Could you confirm whether
The PR description and linked issue (#809) only mention the HTTP template, so it's worth being explicit about the scope. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| {{else}} | ||
| agent = get_or_create_agent() | ||
| {{/if}} | ||
| {{/if}} | ||
|
|
||
| # Execute and format response | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same global-singleton bug exists in the TypeScript HTTP Strands template at
src/assets/typescript/http/strands/base/main.ts(lines 39–51), which uses a module-levellet cachedAgent: Agent | null = nulland agetOrCreateAgent()that caches the first agent forever. The TS Strands SDK has the same conversation-accumulation behavior onAgentinstances, so the same cross-session history leak applies.Please apply the equivalent fix there (replace
getOrCreateAgentwith a per-invocation factory) and update the corresponding snapshot. Otherwise the security fix only lands for half the users.