diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d7ae6b643..d76ab2c1f4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Docs: https://docs.openclaw.ai - macOS Talk Mode: set the speech recognition request `taskHint` to `.dictation` for mic capture, and add regression coverage for the request defaults. (#38445) Thanks @dmiv. - macOS release packaging: default `scripts/package-mac-app.sh` to universal binaries for `BUILD_CONFIG=release`, and clarify that `scripts/package-mac-dist.sh` already produces the release zip + DMG. (#33891) Thanks @cgdusek. - Tools/web search: restore Perplexity OpenRouter/Sonar compatibility for legacy `OPENROUTER_API_KEY`, `sk-or-...`, and explicit `perplexity.baseUrl` / `model` setups while keeping direct Perplexity keys on the native Search API path. (#39937) Thanks @obviyus. +- Hooks/session-memory: keep `/new` and `/reset` memory artifacts in the bound agent workspace and align saved reset session keys with that workspace when stale main-agent keys leak into the hook path. (#39875) thanks @rbutera. ## 2026.3.7 diff --git a/src/auto-reply/reply/commands-core.test.ts b/src/auto-reply/reply/commands-core.test.ts new file mode 100644 index 00000000000..226037f957a --- /dev/null +++ b/src/auto-reply/reply/commands-core.test.ts @@ -0,0 +1,88 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import type { HookRunner } from "../../plugins/hooks.js"; +import type { HandleCommandsParams } from "./commands-types.js"; + +const hookRunnerMocks = vi.hoisted(() => ({ + hasHooks: vi.fn(), + runBeforeReset: vi.fn(), +})); + +vi.mock("../../plugins/hook-runner-global.js", () => ({ + getGlobalHookRunner: () => + ({ + hasHooks: hookRunnerMocks.hasHooks, + runBeforeReset: hookRunnerMocks.runBeforeReset, + }) as unknown as HookRunner, +})); + +const { emitResetCommandHooks } = await import("./commands-core.js"); + +describe("emitResetCommandHooks", () => { + async function runBeforeResetContext(sessionKey?: string) { + const command = { + surface: "discord", + senderId: "rai", + channel: "discord", + from: "discord:rai", + to: "discord:bot", + resetHookTriggered: false, + } as HandleCommandsParams["command"]; + + await emitResetCommandHooks({ + action: "new", + ctx: {} as HandleCommandsParams["ctx"], + cfg: {} as HandleCommandsParams["cfg"], + command, + sessionKey, + previousSessionEntry: { + sessionId: "prev-session", + } as HandleCommandsParams["previousSessionEntry"], + workspaceDir: "/tmp/openclaw-workspace", + }); + + await vi.waitFor(() => expect(hookRunnerMocks.runBeforeReset).toHaveBeenCalledTimes(1)); + const [, ctx] = hookRunnerMocks.runBeforeReset.mock.calls[0] ?? []; + return ctx; + } + + beforeEach(() => { + hookRunnerMocks.hasHooks.mockReset(); + hookRunnerMocks.runBeforeReset.mockReset(); + hookRunnerMocks.hasHooks.mockImplementation((hookName) => hookName === "before_reset"); + hookRunnerMocks.runBeforeReset.mockResolvedValue(undefined); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("passes the bound agent id to before_reset hooks for multi-agent session keys", async () => { + const ctx = await runBeforeResetContext("agent:navi:main"); + expect(ctx).toMatchObject({ + agentId: "navi", + sessionKey: "agent:navi:main", + sessionId: "prev-session", + workspaceDir: "/tmp/openclaw-workspace", + }); + }); + + it("falls back to main when the reset hook has no session key", async () => { + const ctx = await runBeforeResetContext(undefined); + expect(ctx).toMatchObject({ + agentId: "main", + sessionKey: undefined, + sessionId: "prev-session", + workspaceDir: "/tmp/openclaw-workspace", + }); + }); + + it("keeps the main-agent path on the main agent workspace", async () => { + const ctx = await runBeforeResetContext("agent:main:main"); + expect(ctx).toMatchObject({ + agentId: "main", + sessionKey: "agent:main:main", + sessionId: "prev-session", + workspaceDir: "/tmp/openclaw-workspace", + }); + }); +}); diff --git a/src/auto-reply/reply/commands-core.ts b/src/auto-reply/reply/commands-core.ts index d57d679fdb6..894724bcfb0 100644 --- a/src/auto-reply/reply/commands-core.ts +++ b/src/auto-reply/reply/commands-core.ts @@ -3,7 +3,7 @@ import { resetAcpSessionInPlace } from "../../acp/persistent-bindings.js"; import { logVerbose } from "../../globals.js"; import { createInternalHookEvent, triggerInternalHook } from "../../hooks/internal-hooks.js"; import { getGlobalHookRunner } from "../../plugins/hook-runner-global.js"; -import { isAcpSessionKey } from "../../routing/session-key.js"; +import { isAcpSessionKey, resolveAgentIdFromSessionKey } from "../../routing/session-key.js"; import { resolveSendPolicy } from "../../sessions/send-policy.js"; import { shouldHandleTextCommands } from "../commands-registry.js"; import { handleAcpCommand } from "./commands-acp.js"; @@ -63,6 +63,7 @@ export async function emitResetCommandHooks(params: { previousSessionEntry: params.previousSessionEntry, commandSource: params.command.surface, senderId: params.command.senderId, + workspaceDir: params.workspaceDir, cfg: params.cfg, // Pass config for LLM slug generation }); await triggerInternalHook(hookEvent); @@ -120,7 +121,7 @@ export async function emitResetCommandHooks(params: { await hookRunner.runBeforeReset( { sessionFile, messages, reason: params.action }, { - agentId: params.sessionKey?.split(":")[0] ?? "main", + agentId: resolveAgentIdFromSessionKey(params.sessionKey), sessionKey: params.sessionKey, sessionId: prevEntry?.sessionId, workspaceDir: params.workspaceDir, diff --git a/src/auto-reply/reply/commands.test.ts b/src/auto-reply/reply/commands.test.ts index 2c05690ebd0..38be7c43531 100644 --- a/src/auto-reply/reply/commands.test.ts +++ b/src/auto-reply/reply/commands.test.ts @@ -1138,6 +1138,9 @@ describe("handleCommands hooks", () => { type: "command", action: "new", sessionKey: "agent:main:telegram:direct:123", + context: expect.objectContaining({ + workspaceDir: testWorkspaceDir, + }), }), ); spy.mockRestore(); diff --git a/src/hooks/bundled/session-memory/handler.test.ts b/src/hooks/bundled/session-memory/handler.test.ts index 7f29c58b128..fb7e9ca0a4d 100644 --- a/src/hooks/bundled/session-memory/handler.test.ts +++ b/src/hooks/bundled/session-memory/handler.test.ts @@ -65,15 +65,23 @@ async function runNewWithPreviousSessionEntry(params: { previousSessionEntry: { sessionId: string; sessionFile?: string }; cfg?: OpenClawConfig; action?: "new" | "reset"; + sessionKey?: string; + workspaceDirOverride?: string; }): Promise<{ files: string[]; memoryContent: string }> { - const event = createHookEvent("command", params.action ?? "new", "agent:main:main", { - cfg: - params.cfg ?? - ({ - agents: { defaults: { workspace: params.tempDir } }, - } satisfies OpenClawConfig), - previousSessionEntry: params.previousSessionEntry, - }); + const event = createHookEvent( + "command", + params.action ?? "new", + params.sessionKey ?? "agent:main:main", + { + cfg: + params.cfg ?? + ({ + agents: { defaults: { workspace: params.tempDir } }, + } satisfies OpenClawConfig), + previousSessionEntry: params.previousSessionEntry, + ...(params.workspaceDirOverride ? { workspaceDir: params.workspaceDirOverride } : {}), + }, + ); await handler(event); @@ -242,6 +250,44 @@ describe("session-memory hook", () => { expect(memoryContent).toContain("assistant: Captured before reset"); }); + it("prefers workspaceDir from hook context when sessionKey points at main", async () => { + const mainWorkspace = await createCaseWorkspace("workspace-main"); + const naviWorkspace = await createCaseWorkspace("workspace-navi"); + const naviSessionsDir = path.join(naviWorkspace, "sessions"); + await fs.mkdir(naviSessionsDir, { recursive: true }); + + const sessionFile = await writeWorkspaceFile({ + dir: naviSessionsDir, + name: "navi-session.jsonl", + content: createMockSessionContent([ + { role: "user", content: "Remember this under Navi" }, + { role: "assistant", content: "Stored in the bound workspace" }, + ]), + }); + + const { files, memoryContent } = await runNewWithPreviousSessionEntry({ + tempDir: naviWorkspace, + cfg: { + agents: { + defaults: { workspace: mainWorkspace }, + list: [{ id: "navi", workspace: naviWorkspace }], + }, + } satisfies OpenClawConfig, + sessionKey: "agent:main:main", + workspaceDirOverride: naviWorkspace, + previousSessionEntry: { + sessionId: "navi-session", + sessionFile, + }, + }); + + expect(files.length).toBe(1); + expect(memoryContent).toContain("user: Remember this under Navi"); + expect(memoryContent).toContain("assistant: Stored in the bound workspace"); + expect(memoryContent).toContain("- **Session Key**: agent:navi:main"); + await expect(fs.access(path.join(mainWorkspace, "memory"))).rejects.toThrow(); + }); + it("filters out non-message entries (tool calls, system)", async () => { // Create session with mixed entry types const sessionContent = createMockSessionContent([ diff --git a/src/hooks/bundled/session-memory/handler.ts b/src/hooks/bundled/session-memory/handler.ts index 79bfa1cf329..32fc36b23f0 100644 --- a/src/hooks/bundled/session-memory/handler.ts +++ b/src/hooks/bundled/session-memory/handler.ts @@ -8,12 +8,19 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { resolveAgentWorkspaceDir } from "../../../agents/agent-scope.js"; +import { + resolveAgentIdByWorkspacePath, + resolveAgentWorkspaceDir, +} from "../../../agents/agent-scope.js"; import type { OpenClawConfig } from "../../../config/config.js"; import { resolveStateDir } from "../../../config/paths.js"; import { writeFileWithinRoot } from "../../../infra/fs-safe.js"; import { createSubsystemLogger } from "../../../logging/subsystem.js"; -import { resolveAgentIdFromSessionKey } from "../../../routing/session-key.js"; +import { + parseAgentSessionKey, + resolveAgentIdFromSessionKey, + toAgentStoreSessionKey, +} from "../../../routing/session-key.js"; import { hasInterSessionUserProvenance } from "../../../sessions/input-provenance.js"; import { resolveHookConfig } from "../../config.js"; import type { HookHandler } from "../../hooks.js"; @@ -21,6 +28,25 @@ import { generateSlugViaLLM } from "../../llm-slug-generator.js"; const log = createSubsystemLogger("hooks/session-memory"); +function resolveDisplaySessionKey(params: { + cfg?: OpenClawConfig; + workspaceDir?: string; + sessionKey: string; +}): string { + if (!params.cfg || !params.workspaceDir) { + return params.sessionKey; + } + const workspaceAgentId = resolveAgentIdByWorkspacePath(params.cfg, params.workspaceDir); + const parsed = parseAgentSessionKey(params.sessionKey); + if (!workspaceAgentId || !parsed || workspaceAgentId === parsed.agentId) { + return params.sessionKey; + } + return toAgentStoreSessionKey({ + agentId: workspaceAgentId, + requestKey: parsed.rest, + }); +} + /** * Read recent messages from session file for slug generation */ @@ -182,10 +208,21 @@ const saveSessionToMemory: HookHandler = async (event) => { const context = event.context || {}; const cfg = context.cfg as OpenClawConfig | undefined; + const contextWorkspaceDir = + typeof context.workspaceDir === "string" && context.workspaceDir.trim().length > 0 + ? context.workspaceDir + : undefined; const agentId = resolveAgentIdFromSessionKey(event.sessionKey); - const workspaceDir = cfg - ? resolveAgentWorkspaceDir(cfg, agentId) - : path.join(resolveStateDir(process.env, os.homedir), "workspace"); + const workspaceDir = + contextWorkspaceDir || + (cfg + ? resolveAgentWorkspaceDir(cfg, agentId) + : path.join(resolveStateDir(process.env, os.homedir), "workspace")); + const displaySessionKey = resolveDisplaySessionKey({ + cfg, + workspaceDir: contextWorkspaceDir, + sessionKey: event.sessionKey, + }); const memoryDir = path.join(workspaceDir, "memory"); await fs.mkdir(memoryDir, { recursive: true }); @@ -293,7 +330,7 @@ const saveSessionToMemory: HookHandler = async (event) => { const entryParts = [ `# Session: ${dateStr} ${timeStr} UTC`, "", - `- **Session Key**: ${event.sessionKey}`, + `- **Session Key**: ${displaySessionKey}`, `- **Session ID**: ${sessionId}`, `- **Source**: ${source}`, "",