Skip to content

fix(proxy): replace global _session_context_force with per-key forced context map (issue #149)#169

Merged
arniesaha merged 2 commits intomainfrom
fix/issue-149-session-context-race
Apr 17, 2026
Merged

fix(proxy): replace global _session_context_force with per-key forced context map (issue #149)#169
arniesaha merged 2 commits intomainfrom
fix/issue-149-session-context-race

Conversation

@arniesaha
Copy link
Copy Markdown
Owner

Summary

  • Replaces the single global _session_context_force: bool with _forced_session_contexts: dict[str, dict[str, str]] keyed by session_key
  • POST /session with force: true + session_key stores the override in the per-key map; the global force flag is not set, so concurrent requests from other channels are unaffected
  • The proxy request handler reads X-AgentWeave-Session-Key from incoming requests and looks up the matching forced context; unkeyed requests see neither the forced context nor its session/agent attribution
  • X-AgentWeave-Session-Key is added to _SKIP_HEADERS_ALWAYS so it is never forwarded upstream
  • Legacy path (no session_key) preserved for backward compatibility — global _session_context_force still works as before

Test plan

  • test_session_key_stores_in_forced_map — POST with force+key stores in map, does NOT set global flag
  • test_session_key_clear_removes_from_map — POST with force:false+key removes the entry
  • test_unkeyed_request_not_affected_by_forced_key — request without session-key header ignores the forced context, uses its own request headers
  • test_keyed_request_uses_forced_context — request with matching session-key header picks up the forced agent_id/session_id
  • test_session_key_header_stripped_from_forwarding — header is in _SKIP_HEADERS_ALWAYS
  • test_legacy_force_without_key_still_works — no session_key → global flag set as before

Closes #149

🤖 Generated with Claude Code

arniesaha and others added 2 commits April 17, 2026 02:12
… context map

Fixes issue #149: concurrent requests from different channels (e.g. Telegram
arriving while a sub-agent is running) were misattributed because a single
global _session_context_force flag caused ALL proxy requests to use the forced
session context, not just the ones belonging to that sub-agent window.

Replace the boolean flag with _forced_session_contexts: dict[str, dict] keyed
by session_key. POST /session with force:true + session_key stores the override
in the map; requests carrying a matching X-AgentWeave-Session-Key header use
that entry. Unkeyed concurrent requests are completely unaffected.

The global _session_context_force flag is kept for backward compatibility with
callers that don't supply a session_key (legacy path).

Also strips X-AgentWeave-Session-Key from upstream-forwarded headers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Prevents slow memory leak when bridge plugins orphan forced-context
entries (e.g. crash mid sub-agent window). Switches the map to an
OrderedDict and adds _set_forced_context that evicts the oldest entry
once size exceeds _MAX_FORCED_CONTEXTS.

Follow-up to PR #169 review.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@arniesaha arniesaha merged commit 9579d23 into main Apr 17, 2026
4 checks passed
@arniesaha arniesaha deleted the fix/issue-149-session-context-race branch April 17, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(proxy): force:true session context can race with concurrent requests

1 participant