Repository navigation
feat(memory): integrate fusion-memory long-term memory commit - #151
Merged
Merged
Conversation
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.
This was referenced Aug 27, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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. Nativefetch+AbortSignal.timeout, Bearer auth, fail-empty (logForDebugging+ return null, never throws). Style mirrorsfusion-kb-client.ts. Default port11440(avoids the fusion-kb11435collision).src/services/memory/commitTurn.ts— extracts the last completed user/assistant turn from the message history, derivessession_idviagetSessionId(), commits fire-and-forget. Main-agent only (!agentId), no-op on empty or missing turns.src/query/stopHooks.ts— firescommitLastTurnalongsideextractMemoriesin 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)
FUSION_MEMORY_BASE_URLhttp://127.0.0.1:11440FUSION_MEMORY_API_KEYDegradation
fm-server down or
FUSION_MEMORY_API_KEYunset -> memory is skipped with a debug log, never blocks the main loop. 100% offline — HTTP only to127.0.0.1.Tests
15 tests across
fusionMemoryClient.test.ts(10) andcommitTurn.test.ts(5). FetchspyOnwire-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.bun run buildgreen;tsc --noEmitclean 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
src/services/memory/{fusionMemoryClient,commitTurn}.ts+ 2 test filessrc/query/stopHooks.ts(commit call, 1 require-gated module)