Skip to content

feat(memory): integrate fusion-memory long-term memory commit - #151

Merged
dahai80 merged 1 commit into
mainfrom
feat/memory-adapter
Aug 27, 2026
Merged

dahai80 merged 1 commit into
mainfrom
feat/memory-adapter

Conversation

@dahai80

@dahai80 dahai80 commented Aug 27, 2026

Copy link
Copy Markdown
Owner

Closes #150.

What

Adds cross-session long-term memory to fusion-code by integrating fusion-memory (the Fusion ecosystem memory hub). Each main-agent turn is committed to fm-server at turn end, so the agent accumulates memory across sessions instead of starting stateless every time.

How

  • src/services/memory/fusionMemoryClient.ts — HTTP client for fm-server's JSON-RPC 2.0 API. Native fetch + AbortSignal.timeout, Bearer auth, fail-empty (logForDebugging + return null, never throws). Style mirrors fusion-kb-client.ts. Default port 11440 (avoids the fusion-kb 11435 collision).
  • src/services/memory/commitTurn.ts — extracts the last completed user/assistant turn from the message history, derives session_id via getSessionId(), commits fire-and-forget. Main-agent only (!agentId), no-op on empty or missing turns.
  • src/query/stopHooks.ts — fires commitLastTurn alongside extractMemories in the !isBareMode() block, guarded by !toolUseContext.agentId.

Wire contract

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

  • commit -> string[] (turn-level memory ids)

HTTP: POST /v1/memory/commit (Bearer auth).

Config (operator env)

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, memory is skipped

Degradation

fm-server down or FUSION_MEMORY_API_KEY unset -> memory is skipped with a debug log, never blocks the main loop. 100% offline — HTTP only to 127.0.0.1.

Tests

15 tests across fusionMemoryClient.test.ts (10) and commitTurn.test.ts (5). Fetch spyOn wire-contract asserts with env snapshot/restore, covering success, RPC error, HTTP non-2xx, fetch throw, missing API key, subagent skip, empty/missing turn. All pass.

15 pass, 0 fail, 33 expect() calls

bun run build green; tsc --noEmit clean on touched files.

Out of scope (deferred)

Retrieve-inject (recall memory into the system prompt at turn start) is deferred to a follow-up issue. The subagent spawn path (AgentTool.tsx) is the wrong seam — it builds subagent prompts, not the main conversational agent's. The correct main-agent system-prompt assembly seam needs maintainer confirmation before a broader edit.

Scope

  • New: src/services/memory/{fusionMemoryClient,commitTurn}.ts + 2 test files
  • Edit: src/query/stopHooks.ts (commit call, 1 require-gated module)
  • No changes to existing session memory, extract-memories, KB, or auto-dream logic.

Add cross-session memory adapter to close the stateless-session gap.
On each main-agent turn end (stopHooks), commit the last user/assistant
turn to fusion-memory (fm-server HTTP JSON-RPC 2.0, 127.0.0.1).

New src/services/memory/:
- fusionMemoryClient.ts: HTTP client (native fetch + AbortSignal.timeout,
  Bearer auth, fail-empty logForDebugging on error — mirrors
  fusion-kb-client.ts). Port 11440 (avoids fusion-kb 11435 collision).
- commitTurn.ts: extract last completed turn from message history,
  derive session_id via getSessionId(), commit fire-and-forget.
  Main-agent only (!agentId), no-op on empty/missing turn.
- fusionMemoryClient.test.ts + commitTurn.test.ts: 15 tests, fetch
  spyOn wire-contract asserts (env snapshot/restore, fail-empty paths).

Edit src/query/stopHooks.ts: fire commitLastTurn alongside extractMemories
in the !isBareMode() block, guarded by !toolUseContext.agentId.

Degradation: fm-server down or FUSION_MEMORY_API_KEY unset -> skipped
with debug log, never blocks the main loop. 100% offline (HTTP only to
127.0.0.1). Retrieve-inject deferred to a follow-up (separate issue) —
the main-agent system-prompt assembly seam needs maintainer confirmation.

Closes #150.
@dahai80
dahai80 merged commit 5311b00 into main Aug 27, 2026
1 check passed
@dahai80
dahai80 deleted the feat/memory-adapter branch August 27, 2026 06:51
dahai80 added a commit that referenced this pull request Aug 27, 2026
Closes #154. Completes the retrieve half deferred by #151 (which landed
turn-end commit only). fusion-cowork #68 and fusion-agent-studio #247
already have both commit+retrieve; this brings fusion-code to parity.

## What

At main-agent turn start (processUserInput), recall relevant cross-session
memory from fusion-memory (fm-server) and inject it as an attachment message
on the same path as UserPromptSubmit hook additionalContext. The agent now
starts each turn with relevant prior context, not stateless.

## How

- src/services/memory/retrieveContext.ts (new) — retrieveMemorySection():
  calls the existing retrieveContext RPC + formatContextToPrompt (both added
  by #151 but unwired), returns a <fusion_memory_context> section string or
  . Main-agent guard (!agentId), fail-empty (logForDebugging, never throws).
  Env override: FUSION_MEMORY_RETRIEVE_TOP_K / FUSION_MEMORY_RETRIEVE_BUDGET.
- src/utils/processUserInput/processUserInput.ts (edit) — after UserPromptSubmit
  hooks, on the happy path with real input and !context.agentId, fire-and-forget
  retrieveMemorySection and push the section as a hook_additional_context
  attachment message. Mirrors the existing hook additionalContext push. Skips
  silently if fm-server down or FUSION_MEMORY_API_KEY unset.

## Degradation

fm-server down / FUSION_MEMORY_API_KEY unset / no memory matched -> section
is empty, nothing injected, debug log only. Never blocks the main loop.
100% offline — HTTP only to 127.0.0.1.

## Tests

10 tests in retrieveContext.test.ts (subagent skip, empty input, success
format, RPC error, HTTP non-2xx, fetch throw, missing API key, no-match,
top_k env override, invalid env fallback). Full memory suite 25 pass,
bun run build green.
dahai80 added a commit that referenced this pull request Aug 30, 2026
…154)

Wire the retrieve half of fusion-memory integration symmetric to the
commit half (commitLastTurn, landed by #151). At turn start, recall
relevant cross-session memory from fusion-memory and inject it as a
hook_additional_context attachment message — same path as
UserPromptSubmit hook additionalContext.

Seam: src/utils/processUserInput/processUserInput.ts, after
queryCheckpoint("query_hooks_end"), before return. Main-agent only
(!context.agentId), only when result.shouldQuery and inputMessage is
non-empty.

Race fix: AWAIT retrieveMemorySection instead of fire-and-forget. Both
callers of processUserInput read result.messages synchronously after the
await (handlePromptSubmit.ts newMessages.push(...result.messages);
QueryEngine.ts this.mutableMessages.push(...messagesFromUserInput)). A
fire-and-forget .then fires AFTER return, so the injected message would
be lost. Awaiting guarantees the attachment is present before callers
read it. This deliberately deviates from a prior branch commit's
(26e0ac9) fire-and-forget design, which had this race.

Convention: unconditional require() for the memory module (no feature()
gate), matching stopHooks.ts memoryCommitModule — memory is always
wired, fail-empty at runtime.

Fail-empty: retrieveMemorySection returns "" immediately (no fetch)
when FUSION_MEMORY_API_KEY is unset, subagent, or empty input — hot-path
zero overhead. Fetch only when fm-server opted-in (10s bounded via
AbortSignal.timeout in rpc()). Try/catch + logForDebugging, never
throws, never blocks the turn.

Token budget: FUSION_MEMORY_RETRIEVE_BUDGET (default 4096) passed to the
retrieve RPC; applyTruncation (10000 char cap) on the injected section.

Env (operator):
  FUSION_MEMORY_API_KEY        — enables retrieve (and commit)
  FUSION_MEMORY_BASE_URL       — default http://127.0.0.1:11440
  FUSION_MEMORY_RETRIEVE_TOP_K — default 10
  FUSION_MEMORY_RETRIEVE_BUDGET — default 4096

Tests: src/services/memory/retrieveContext.test.ts — 10 tests (subagent
skip, empty input, success format, RPC error, HTTP 500, fetch throw,
API key unset, no-match, top_k env override, invalid env fallback).

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

Co-Authored-By: Claude Fable 5 <[email protected]>
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.

feat(memory): integrate fusion-memory long-term memory adapter

1 participant