Skip to content

fix(memory): pass session and user context to on_turn_start - #7789

Open
hughpyle wants to merge 1 commit into
NousResearch:mainfrom
keepnotes-ai:feat/memory-turn-context
Open

fix(memory): pass session and user context to on_turn_start#7789
hughpyle wants to merge 1 commit into
NousResearch:mainfrom
keepnotes-ai:feat/memory-turn-context

Conversation

@hughpyle

@hughpyle hughpyle commented Apr 11, 2026

Copy link
Copy Markdown

Summary

  • External memory providers receive per-turn context (user_id, user_name, session_title, platform) into MemoryManager.on_turn_start (in addition to the existing remaining_tokens/model/tool_count slots).
  • Adds turn_user_id / turn_user_name to run_conversation; gateway forwards source.user_id/source.user_name so multi-user threads attribute correctly.

Fully backward compatible — the base class and all in-tree providers already accept **kwargs, so unknown keys are silently absorbed.

Fixes #7193
Fixes #7781
Fixes #7777

Test plan

  • test_on_turn_start — existing test updated to verify empty kwargs passthrough
  • test_on_turn_start_passes_turn_context — new test: all four context keys reach the provider
  • test_turn_context_reaches_memory_manager_before_prefetch — new test: verifies on_turn_start fires before prefetch_all with correct gateway user override

@hughpyle hughpyle changed the title feat(memory): pass session and user context to on_turn_start fix(memory): pass session and user context to on_turn_start Apr 11, 2026
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint tool/memory Memory tool and memory providers comp/plugins Plugin system and bundled plugins labels Apr 29, 2026
@terion-labs

Copy link
Copy Markdown

why still unmerged?((

@hughpyle

Copy link
Copy Markdown
Author

Actually I think this is obsoleted by parallel work, will take a look...

Complete the still-open part of PR NousResearch#7789 after upstream separately shipped session_id propagation through sync_turn/prefetch/queue_prefetch and session rotation hooks.

Gateway turns now pass per-message source.user_id and source.user_name into run_conversation so shared-thread messages can override the cached agent user. AIAgent forwards user_id, user_name, platform, and session_title through the existing on_turn_start hook for provider attribution.

This intentionally does not reintroduce session_id threading in on_turn_start; upstream already covers session attribution for writes and recalls. Providers that write during sync_turn can stash attribution received from on_turn_start and reuse it when syncing the completed turn.
@hughpyle
hughpyle force-pushed the feat/memory-turn-context branch from d364386 to fc4ac4a Compare May 13, 2026 21:49
@hughpyle

hughpyle commented May 13, 2026

Copy link
Copy Markdown
Author

PR #17409 reworked the session-id to sync_turn etc. But the username and other context is still not available, this PR addresses those gaps.

Updated the descriptions in #7781 and #7777.

#7193 was largely addressed by PR#10619, this closes its remaining ask.

@terion-labs

Copy link
Copy Markdown

soooo....?

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for identifying the missing per-turn provider context. The exact on_turn_start metadata capability is still absent on current main: agent/turn_context.py:536-540 calls the hook with only turn number and message.

Problems

  • The shared-thread stale-user-ID premise has since changed: gateway/run.py:15741-15753 keys the agent cache on user_id/user_id_alt and rebuilds for another participant; agent/agent_init.py:1375-1410 already supplies initial user/title metadata to providers.
  • This branch targets code moved by 053025238; the live hook is now in agent/turn_context.py, while run_agent.py:5775-5798 is only a forwarder.
  • The proposed user_name handling has no fallback to the stored agent name, and it omits an empty session_title, contrary to issue [Bug]: MemoryProvider doesn't see session titles #7777's stated contract.

Suggested changes

  • Port the parameters through the forwarder, agent/conversation_loop.py, and agent/turn_context.py; pass the current gateway source at gateway/run.py:18777-18789.
  • Add turn-context tests for cached-name fallback and titled/untitled sessions.

Automated hermes-sweeper review.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 12, 2026
@alt-glitch alt-glitch added type/feature New feature or request and removed type/bug Something isn't working sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades labels Jul 12, 2026
@teknium1 teknium1 added area/sessions Session lifecycle, resume, persistence, history area/memory Memory subsystem: store, providers, sync, background reviews labels Jul 19, 2026
@alt-glitch alt-glitch added comp/gateway Gateway runner, session dispatch, delivery needs-decision Awaiting maintainer decision before any implementation labels Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/memory Memory subsystem: store, providers, sync, background reviews area/sessions Session lifecycle, resume, persistence, history comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery comp/plugins Plugin system and bundled plugins needs-decision Awaiting maintainer decision before any implementation P3 Low — cosmetic, nice to have sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state tool/memory Memory tool and memory providers type/feature New feature or request

Projects

None yet

4 participants