Skip to content

Retrieve-inject long-term memory into main-agent system prompt (follow-up to #151) #154

Closed

Description

@dahai80

What

Follow-up to #151 (merged, commit-only half) and the original #150. PR #151 landed turn-end commit to fusion-memory but explicitly deferred retrieve-inject — recalling relevant long-term memory into the main agent's system prompt at turn start. This issue tracks the deferred half so fusion-code gets the full cross-session memory loop (retrieve → act → commit), matching what fusion-cowork #68 and fusion-agent-studio #247 already have.

Why it's deferred

#151's body notes the subagent spawn path (src/tools/AgentTool/AgentTool.tsx) is the wrong seam — it builds subagent prompts, not the main conversational agent's system prompt. The correct main-agent system-prompt assembly seam needs maintainer confirmation before a broader edit, so retrieve-inject was scoped out rather than guessed.

Proposal

Add retrieve-inject that, at main-agent turn start:

  1. Calls fm-server retrieve (POST /v1/memory/retrieve, Bearer auth) with the latest user message, top_k and a token budget.
  2. Injects the returned FormattedContext.blocks into the main agent's system prompt (alongside git context and chub hints), not subagent prompts.
  3. Degrades gracefully — fm-server down or FUSION_MEMORY_API_KEY unset → skip with a debug log, never throw (same pattern as the commit half).

Wire contract (already implemented in fusion-memory fm-server)

JSON-RPC 2.0 envelope {jsonrpc, method, params, id} -> {result|error, id}:

  • retrieve -> {blocks: ContextBlock[], total_tokens: number}

HTTP: POST /v1/memory/retrieve (Bearer auth). Full contract: fusion-memory/clients/README.md.

Needed from maintainer

  • Correct seam: where is the main conversational agent's system prompt assembled (the one that runs when no agentId is set, i.e. !toolUseContext.agentId)? Candidate files to confirm: the main prompt builder / system-prompt assembly path that AgentTool.tsx feeds into but does not own.
  • Token budget: how much of the system-prompt budget can retrieve-inject consume (avoid blowing the context window fusion-memory exists to protect).

Config (operator env, same as #151)

Env Default Purpose
FUSION_MEMORY_BASE_URL http://127.0.0.1:11440 fm-server base URL
FUSION_MEMORY_API_KEY (none) Bearer token; if unset, retrieve is skipped

Scope (expected)

  • New: a retrieveContext helper alongside src/services/memory/commitTurn.ts (reuse fusionMemoryClient.ts's rpc()).
  • Edit: the main-agent system-prompt assembly seam (to be identified by maintainer).
  • Tests: wire-contract asserts mirroring fusionMemoryClient.test.ts + an inject-format test.

100% offline — HTTP only to 127.0.0.1. No cloud calls.

Activity

  1. added
    enhancement
    on Aug 27, 2026
  2. added 2 commits that reference this issue on Aug 27, 2026
    26e0ac9
    2c6733f
  3. dahai80 commented on Aug 30, 2026

    @dahai80
    OwnerAuthor

    Landed in 2c6733f (direct-push main). Wired the retrieve half symmetric to the commit half (#151).

    Seam (found via code, not subagent prompts): src/utils/processUserInput/processUserInput.ts — after query_hooks_end, inject recalled memory as a hook_additional_context attachment (same path as UserPromptSubmit hook additionalContext). Main-agent only.

    Race fix: AWAIT retrieveMemorySection (not fire-and-forget) — both callers read result.messages synchronously after await processUserInput, so fire-and-forget would lose the injected message.

    Token budget: FUSION_MEMORY_RETRIEVE_BUDGET (default 4096) → retrieve RPC + applyTruncation (10000 char cap). Fail-empty (no fetch when API key unset/subagent/empty).

    Gates: typecheck 0 / 961 tests pass / biome 0 new errors / build:dev + build:dev:full green.

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions