Skip to content

Global agent_context.load_memory preference is ignored unless the conversation is launched from an agent profile #4542

Closed

Description

@VascoSch92

Summary

agent_settings.agent_context.load_memory is exposed as a global user preference, but it is only applied when a conversation is started via agent_profile_id. Conversations started with a concrete agent (or an agent_settings payload) silently lose the setting, so persistent memory is never injected.

There is no error and no warning. <MEMORY_CONTEXT> is simply absent, and <MEMORY> renders its other variant, telling the agent that AGENTS.md is its persistent memory — which reads like a working feature.

Impact

Measured on a self-hosted Agent Canvas deployment with load_memory: true in settings:

  • 642 of 767 conversations launched with load_memory: false
  • 0 of 767 ever rendered a <MEMORY_CONTEXT> block
  • The 24 that did carry load_memory: true were ones whose client happened to copy the stored agent_context verbatim — it worked by accident, not by design

Stored preference vs. what actually launched, for a representative conversation:

field settings as launched
load_memory True False
load_user_skills False True
load_project_skills False True
load_public_skills False True
skills 0 57

Root cause

StartConversationRequest accepts three mutually-exclusive shapes (openhands-sdk/openhands/sdk/conversation/request.py:280):

  1. agent_profile_id — resolved server-side in conversation_service
  2. agent — a concrete agent payload
  3. agent_settings — converted to agent by the _populate_agent_from_settings validator (mode="before"), so request.agent is already populated before the service sees it

The load_memory stamp only exists on path 1. _resolve_agent_from_profile applies it at openhands-agent-server/openhands/agent_server/conversation_service.py:392:

if load_memory:
    context = agent.agent_context or AgentContext()
    agent = agent.model_copy(
        update={"agent_context": context.model_copy(update={"load_memory": True})}
    )

and that function is only called at :1480, inside if request.agent_profile_id is not None:. Paths 2 and 3 never touch it.

The helper's own docstring already makes the argument:

An AgentProfile has no agent_context field, so the preference cannot ride the profile — it is stamped onto the resolved agent below, else a profile-launched conversation would silently ignore the setting.

The same reasoning applies to paths 2 and 3, which are the common cases.

The rest of the chain is fine

For memory to render, all of these must hold. Only step 1 is broken:

  1. ❌ agent.agent_context.load_memory is True on the agent the conversation starts with
  2. ✅ LocalConversation resolves it — openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py:1156
  3. ✅ load_memory() reads ~/.openhands/memory/MEMORY.md and <workspace>/.openhands/memory/MEMORY.md, returns None if neither has content
  4. ✅ MemoryContextSection.guard() — openhands-sdk/openhands/sdk/context/prompts/sections/dynamic.py:82

Why it is easy to miss

  • Nothing is logged when the preference is dropped
  • MemoryContextSection correctly declines to emit an empty block, so "flag lost" and "no memory recorded yet" look identical
  • MemorySection renders exactly one of two variants; with the flag off the agent is told AGENTS.md is its memory, so transcripts look healthy
  • AgentContext.memory_context is excluded from serialization, so persisted state gives no signal either

Scope

In scope: make the stored load_memory preference apply to every conversation start, regardless of which of the three request shapes was used.

Out of scope: the memory file format, load_memory() itself, the prompt sections, and the AGENTS.md/memory guidance split — all behave correctly once the flag arrives.

Implementation plan

All changes in openhands-agent-server/openhands/agent_server/conversation_service.py.

Because agent_settings is already converted to agent by a validator, request.agent is populated for paths 2 and 3 by the time _start_conversation runs — and for path 1 immediately after the profile branch. So the stamp can happen once, after the branch, covering all three shapes with a single code path.

1. Add a helper next to _append_system_message_suffix (currently line 124).

That function is the existing precedent for this exact agent_context copy-update pattern; put the new one beside it. AgentBase and AgentContext are already imported at module level (lines 52 and 49) — no new imports needed.

def _with_load_memory(agent: AgentBase) -> AgentBase:
    """Stamp the global persistent-memory preference onto an agent.

    ``load_memory`` is a user-level setting, not part of any agent, profile or
    client payload, so it is applied here regardless of how the agent reached
    the request.
    """
    context = agent.agent_context or AgentContext()
    return agent.model_copy(
        update={"agent_context": context.model_copy(update={"load_memory": True})}
    )

2. Hoist the settings read above the profile branch in _start_conversation (~line 1462).

Keep the import function-local — it is inside the branch today, most likely to avoid a circular import; just move it up rather than to module scope.

launched_agent_profile: LaunchedAgentProfile | None = None

from openhands.agent_server.persistence import (
    PersistedSettings,
    get_settings_store,
)

settings = get_settings_store().load() or PersistedSettings()
# ``ACPAgentSettings.agent_context`` is nullable, hence the guard.
stored_context = settings.agent_settings.agent_context
load_memory = bool(stored_context and stored_context.load_memory)

if request.agent_profile_id is not None:
    mcp_config = settings.agent_settings.mcp_config
    resolved_agent, launched_agent_profile = await asyncio.to_thread(
        _resolve_agent_from_profile,
        request.agent_profile_id,
        self.cipher,
        mcp_config,
    )
    request = request.model_copy(update={"agent": resolved_agent})

if load_memory and request.agent is not None:
    request = request.model_copy(
        update={"agent": _with_load_memory(request.agent)}
    )

3. Remove load_memory from _resolve_agent_from_profile (line 296 signature, lines 307–311 docstring, lines 392–396 body).

The post-branch stamp now covers the profile path too, so the parameter and its inline block become dead. This is a net deletion.

Note request.agent is declared AgentBase = Field(default=cast(AgentBase, None)) — typed non-optional but nullable in practice — hence the is not None guard.

Tests

Existing coverage lives in tests/agent_server/test_agent_profile_conv_start.py:

  • test_profile_launch_inherits_the_stored_memory_preference (line 709, parametrized over ["openhands", "acp"])
  • test_profile_launch_leaves_memory_off_without_the_preference (line 751)

Both drive service.start_conversation(...) through the helper _start_from_profile(...), so they should stay green unchanged — they now exercise the post-branch stamp instead of the in-helper one. If any test calls _resolve_agent_from_profile directly with load_memory=, it needs updating.

Add direct-agent equivalents mirroring the existing pair:

  • test_direct_agent_launch_inherits_the_stored_memory_preference — persist PersistedSettings(agent_settings=OpenHandsAgentSettings(agent_context=AgentContext(load_memory=True))), start with a concrete agent, assert agent.agent_context.load_memory is True
  • test_direct_agent_launch_leaves_memory_off_without_the_preference — same parametrization as the existing negative case (preference-off and stored-settings-without-agent-context), assert False
  • Ideally one case starting from an agent_settings payload rather than a concrete agent, to lock in path 3

A new _start_with_agent(...) helper will be needed; _start_from_profile is profile-specific.

Acceptance criteria

  • With load_memory: true stored, a conversation started with a concrete agent has agent_context.load_memory is True
  • Same for an agent_settings payload
  • Same for agent_profile_id (existing behaviour preserved)
  • With the preference off, or with agent_context absent from stored settings, all three paths leave load_memory at False
  • An agent that already sets load_memory=True itself is unaffected
  • _resolve_agent_from_profile no longer takes a load_memory parameter
  • Existing tests in tests/agent_server/test_agent_profile_conv_start.py pass unchanged

Verifying end to end

The unit tests above are the reliable check. For a manual pass, examples/01_standalone_sdk/55_persistent_memory.py shows the working shape; the failing case is the same thing started through the agent-server with a concrete agent instead of a profile. Write a non-empty ~/.openhands/memory/MEMORY.md first — with no memory file, load_memory() returns None and the block is legitimately absent regardless of this bug.

Notes for the implementer

ACP agents. request.agent may be an ACPAgent. load_memory is declared acp_compatible: True in its field json_schema_extra, and the existing profile test already parametrizes over acp, so no special-casing is expected. Both start_conversation (line 1298) and start_acp_conversation (line 1303) delegate to _start_conversation (line 1308), so one fix covers both entry points.

Settings load frequency. get_settings_store().load() currently runs only for profile launches; hoisting makes it run on every start. It is a local store read, so the cost should be negligible — but if that is unwelcome, compute load_memory inside each branch instead. Same behaviour, two duplicated lines, no hoist.

Optional companion change

Independent of the above, load_skills_from_agent_server() (openhands-sdk/openhands/sdk/workspace/remote/base.py:912) discards the caller's AgentContext outright:

agent_context = AgentContext(skills=loaded_skills, load_public_skills=False)

Beyond load_memory, this also resets load_user_skills, load_project_skills, disabled_skills, marketplace_path, and both message suffixes. Accepting an optional base context and using model_copy(update={"skills": ...}) would make the helper non-destructive.

Worth doing on its own merits, but not a substitute for the fix above — hand-written agent payloads never pass a base context at all, so only the server-side stamp covers every client.

Not related: #4527

For anyone landing here from that PR — #4527 touches the same MemorySection class but is unrelated. It adds one line to _AGENTS_MD_GUIDANCE about locating previous conversation event history under workspace/conversations/; it does not touch the load_memory plumbing, and it will not conflict with this fix (different package).

It does illustrate the blast radius, though: because load_memory almost never arrives, nearly every conversation renders the _AGENTS_MD_GUIDANCE variant, so that line will be seen essentially everywhere — while the two-tier memory guidance it sits opposite stays unreachable in practice.

Actual Behavior

Reproduce on a self-hosted Agent Canvas deployment:

  1. Set agent_settings.agent_context.load_memory: true in persisted settings.
  2. Create a non-empty ~/.openhands/memory/MEMORY.md or <workspace>/.openhands/memory/MEMORY.md.
  3. Start a conversation through the agent-server with a concrete agent payload or an agent_settings payload, then inspect the launched agent context. The relevant server-side regression suite is run with:
uv run pytest tests/agent_server/test_agent_profile_conv_start.py -q

Observed: despite the stored preference being true and memory being present, the launched agent has agent_context.load_memory == false; therefore <MEMORY_CONTEXT> is not rendered. Launches via agent_profile_id do not show this failure.

Activity

  1. github-actions commented on Aug 19, 2026

    @github-actions
    Contributor

    ✅ ready-for-dev label applied

    This issue meets the readiness criteria and is ready for contribution.
    A contributor can now open a PR that links this issue (Fixes #<number>).

    This is an automated check — no AI was used to generate this comment.

  2. added
    ready-for-dev
    on Aug 19, 2026
  3. removed
    ready-for-dev
    on Aug 19, 2026
  4. added
    bug
    priority:high
    release-note-required
    on Aug 19, 2026
  5. vnktadithya commented on Aug 20, 2026

    @vnktadithya
    Contributor

    Hi @VascoSch92 — I'd like to work on this. Traced the same root cause: the load_memory stamp only exists in _resolve_agent_from_profile, which only runs on the agent_profile_id branch, so the 'agent' and agent_settings shapes never see it. I'll hoist the settings read above the profile branch in _start_conversation, apply the stamp once via a _with_load_memory helper (mirroring _append_system_message_suffix's pattern) after all three shapes resolve to request.agent, and drop the now-dead load_memory param from _resolve_agent_from_profile. Will add the direct-agent and agent_settings regression tests alongside the existing profile pair. Will drop the PR soon!

  6. VascoSch92 commented on Aug 20, 2026

    @VascoSch92
    MemberAuthor

    @vnktadithya Please in the PR attach also a video (or a script in the folder .pr) where you show that the functionality is working properly. Thanks.

  7. vnktadithya commented on Aug 20, 2026

    @vnktadithya
    Contributor

    Sure @VascoSch92. I'll include a script under .pr/ exercising all three launch paths rather than a video, since it's a backend-only change that you can run directly.

  8. vnktadithya commented on Aug 21, 2026

    @vnktadithya
    Contributor

    Opened #4566 — root cause and fix as scoped in the issue: hoisted the settings read above the agent_profile_id branch so the stamp applies once, after all three shapes converge on request.agent, instead of only on the profile path. Added new test cases for the agent and agent_settings shapes plus a case confirming an already-True agent is left untouched, and a .pr/ script verifying it against a real settings store and a real MEMORY.md, not mocks. I'll file the load_skills_from_agent_server() companion change as its own issue rather than including it here.

  9. vnktadithya commented on Aug 27, 2026

    @vnktadithya
    Contributor

    @VascoSch92 — checking in after a week, and I also found something that needs your input specifically, not just a review. The Validate PR description check on #4566 is failing with: "Linked issue(s) (#4542) carry neither ready-for-dev nor a pre-rollout creation date." You added ready-for-dev to this issue earlier, but the bot removed it again shortly after, and I want you to look into what's tripping the automated check. Not something I can fix from my side — let me know if it just needs the label re-applied or if something in the issue needs adjusting.

  10. added
    ready-for-dev
    on Sep 1, 2026
  11. added
    ready-for-dev
    and removed
    ready-for-dev
    on Sep 1, 2026
  12. VascoSch92 commented on Sep 1, 2026

    @VascoSch92
    MemberAuthor

    I think the @vnktadithya It should be good now

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmemorypriority:highready-for-devrelease-note-required

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions