Файли
openidea/src/features/agents/MemoryPicker.test.tsx
T
Deng Liuтаdengliu 19aef36354 feat(agents): add Agent Memory settings and a per-session memory picker (#6826)
Adds Settings › Agents › Memory and a Session Memory picker (/memory, /memories, the composer + menu and the context meter link). They list each agent's own instruction files, path-scoped rules and auto-memory notes: AGENTS.md, CLAUDE.md and .claude/rules, .github/copilot-instructions.md and .github/instructions, .cursor/rules, GEMINI.md, and the Claude Code, Codex and Hermes auto-memory folders.

Per-file modes (Auto / Always / Off) and the auto-memory policy (Inherit / Read & Write / Read Only / Off) are stored in settings.toml at user or workspace scope and never edit tracked files. Workspace entries load only in trusted projects. Each driver receives the policy natively: Claude Code through --settings and --add-dir, Codex through its memories config and /memories, GitHub Copilot through excludedTools and the system message, and Hermes through config.yaml. A file set to Always is staged into other agents' sessions. Auto-memory notes can be promoted into the workspace AGENTS.md, and an idle session restarts when a memory policy changes.

Groups of the same agent are titled by scope, for example Claude Code (User) and Claude Code (Auto Memory).

Closes #6810

Co-authored-by: dengliu <[email protected]>
2026-11-29 22:38:20 +08:00

389 рядки
12 KiB
TypeScript

import { useState } from "react";
import { describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import type { AgentMemoryCatalog, AgentMemoryOverrides, AgentTask } from "@/bindings";
import { MemoryPicker } from "./MemoryPicker";
import { resolveSessionMemory } from "./memoryModel";
import { ContextMeter } from "./session/ContextMeter";
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
return {
...real,
commands: {
...real.commands,
agentContextUsage: vi.fn(async () => ({
status: "ok",
data: {
model: "claude-sonnet-5-5",
totalTokens: 12_000,
maxTokens: 200_000,
reservedTokens: 8_000,
isAutoCompactEnabled: true,
autoCompactThreshold: 180_000,
categories: [
{ name: "System prompt", tokens: 4_000, kind: "system" },
{ name: "Memory files", tokens: 1_200, kind: null },
{ name: "Messages", tokens: 6_800, kind: null },
],
},
})),
},
};
});
const CATALOG: AgentMemoryCatalog = {
autoMemory: "inherit",
autoMemoryOrigin: "none",
providers: [
{
provider: "claude",
label: "Claude Code",
mode: "readWrite",
origin: "none",
noteCount: 1,
rolloutCount: 0,
approxTokens: 100,
supportsAutoControl: true,
supportsFileExclusion: true,
supportsReset: true,
},
{
provider: "codex",
label: "Codex",
mode: "inherit",
origin: "none",
noteCount: 1,
rolloutCount: 2,
approxTokens: 150,
supportsAutoControl: true,
supportsFileExclusion: false,
supportsReset: true,
},
{
provider: "copilot",
label: "GitHub Copilot",
mode: "inherit",
origin: "none",
noteCount: 0,
rolloutCount: 0,
approxTokens: 0,
supportsAutoControl: true,
supportsFileExclusion: false,
supportsReset: false,
},
{
provider: "acp",
label: "Cursor / Gemini / ACP",
mode: "inherit",
origin: "none",
noteCount: 0,
rolloutCount: 0,
approxTokens: 0,
supportsAutoControl: false,
supportsFileExclusion: false,
supportsReset: false,
},
],
entries: [
{
entry: {
id: "workspace::AGENTS.md",
title: "AGENTS.md",
description: "Shared repo rules",
kind: "instruction",
source: "workspace",
path: "/repo/AGENTS.md",
approxTokens: 400,
lineCount: 80,
byteLen: 1600,
writable: false,
valid: true,
},
mode: "auto",
origin: "none",
},
{
entry: {
id: "claudeWorkspace::CLAUDE.local.md",
title: "CLAUDE.local.md",
description: "Local Claude notes",
kind: "instruction",
source: "claudeWorkspace",
path: "/repo/CLAUDE.local.md",
approxTokens: 120,
lineCount: 25,
byteLen: 480,
writable: false,
valid: true,
},
mode: "auto",
origin: "none",
},
{
entry: {
id: "claudeAuto::MEMORY.md",
title: "MEMORY.md",
description: "Claude auto index",
kind: "autoNote",
source: "claudeAuto",
path: "/home/.claude/projects/repo/memory/MEMORY.md",
approxTokens: 100,
lineCount: 20,
byteLen: 400,
writable: true,
valid: true,
},
mode: "auto",
origin: "none",
},
{
entry: {
id: "cursorWorkspace::react.mdc",
title: "react.mdc",
description: "Cursor TSX rules",
kind: "rule",
source: "cursorWorkspace",
path: "/repo/.cursor/rules/react.mdc",
paths: ["src/**/*.tsx"],
approxTokens: 90,
lineCount: 18,
byteLen: 360,
writable: false,
valid: true,
},
mode: "auto",
origin: "none",
},
{
entry: {
id: "workspace::untrusted.md",
title: "untrusted.md",
description: "Locked by workspace trust",
kind: "instruction",
source: "workspace",
path: "/repo/untrusted.md",
approxTokens: 50,
lineCount: 10,
byteLen: 200,
writable: true,
valid: true,
},
mode: "off",
origin: "trust",
},
],
};
function Harness({ provider }: { provider: string }) {
const [overrides, setOverrides] = useState<AgentMemoryOverrides>({ autoMemory: null, entries: {} });
return (
<MemoryPicker provider={provider} overrides={overrides} onChange={setOverrides} testData={{ catalog: CATALOG }} />
);
}
describe("MemoryPicker", () => {
it("filters relevant entries for Claude, overrides Auto Memory and individual files, and resets", async () => {
render(<Harness provider="claude" />);
const trigger = screen.getByRole("button", { name: "Memory" });
// Claude sees workspace (2: AGENTS.md active, untrusted.md off), claudeWorkspace (1), claudeAuto (1) -> 3/4 active
expect(trigger).toHaveTextContent("Memory3/4");
fireEvent.click(trigger);
expect(await screen.findByText("Session Memory")).toBeInTheDocument();
expect(screen.getByText("AGENTS.md")).toBeInTheDocument();
expect(screen.getByText("CLAUDE.local.md")).toBeInTheDocument();
expect(screen.getByText("MEMORY.md")).toBeInTheDocument();
expect(screen.queryByText("react.mdc")).not.toBeInTheDocument();
expect(screen.getByText("Untrusted workspace")).toBeInTheDocument();
// Override Auto Memory to Read Only
const autoSelect = screen.getByRole("button", { name: "Session auto-memory mode" });
fireEvent.click(autoSelect);
fireEvent.click(within(screen.getByRole("listbox", { name: "Session auto-memory mode" })).getByText("Read Only"));
// Mute CLAUDE.local.md for this session
fireEvent.click(screen.getByRole("switch", { name: "Enable CLAUDE.local.md for this session" }));
await waitFor(() => expect(screen.getByRole("button", { name: "Memory" })).toHaveTextContent("Memory2/4"));
expect(screen.getAllByText("Session")).toHaveLength(2);
// Reset clears both overrides
fireEvent.click(screen.getByRole("button", { name: "Reset" }));
await waitFor(() => expect(screen.getByRole("button", { name: "Memory" })).toHaveTextContent("Memory3/4"));
expect(screen.queryByText("Session")).not.toBeInTheDocument();
});
it("shows Cursor rules and the ACP capability note when running an ACP provider without native auto-memory control", async () => {
render(<Harness provider="cursor" />);
fireEvent.click(screen.getByRole("button", { name: "Memory" }));
expect(await screen.findByText("Session Memory")).toBeInTheDocument();
expect(screen.getByText("AGENTS.md")).toBeInTheDocument();
expect(screen.getByText("react.mdc")).toBeInTheDocument();
expect(screen.queryByText("CLAUDE.local.md")).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Session auto-memory mode" })).not.toBeInTheDocument();
expect(
screen.getByText("This agent manages its own memory. Set a file to Always in Settings to pass it to this agent."),
).toBeInTheDocument();
});
it("resolves provider-specific entries across Codex, Copilot (including CLAUDE.md), Gemini, OpenCode, and Hermes profiles", () => {
const extended: AgentMemoryCatalog = {
...CATALOG,
providers: [
...CATALOG.providers,
{
provider: "hermes",
label: "Hermes Agent",
mode: "readWrite",
origin: "none",
noteCount: 2,
rolloutCount: 0,
approxTokens: 80,
supportsAutoControl: true,
supportsFileExclusion: false,
supportsReset: true,
},
],
entries: [
...CATALOG.entries,
{
entry: {
id: "codexAuto::memory_summary.md",
title: "memory_summary.md",
description: "Codex summary",
kind: "autoNote",
source: "codexAuto",
path: "/home/.codex/memories/memory_summary.md",
approxTokens: 150,
lineCount: 30,
byteLen: 600,
writable: true,
valid: true,
},
mode: "auto",
origin: "none",
},
{
entry: {
id: "geminiWorkspace::GEMINI.md",
title: "GEMINI.md",
description: "Gemini workspace rules",
kind: "instruction",
source: "geminiWorkspace",
path: "/repo/GEMINI.md",
approxTokens: 70,
lineCount: 15,
byteLen: 280,
writable: false,
valid: true,
},
mode: "auto",
origin: "none",
},
{
entry: {
id: "hermesAuto::work/USER.md",
title: "work/USER.md",
description: "Hermes profile note",
kind: "autoNote",
source: "hermesAuto",
path: "/home/.hermes/profiles/work/memories/USER.md",
approxTokens: 60,
lineCount: 12,
byteLen: 240,
writable: true,
valid: true,
},
mode: "auto",
origin: "none",
},
],
};
// Copilot natively reads `claudeWorkspace` (`CLAUDE.local.md`) + `workspace` (`AGENTS.md`), not `codexAuto` or `geminiWorkspace`.
const copilot = resolveSessionMemory(extended, "copilot");
expect(copilot.supportsAutoControl).toBe(true);
const copilotIds = copilot.groups.flatMap((g) => g.items.map((i) => i.id));
expect(copilotIds).toContain("workspace::AGENTS.md");
expect(copilotIds).toContain("claudeWorkspace::CLAUDE.local.md");
expect(copilotIds).not.toContain("codexAuto::memory_summary.md");
// Codex sees `codexAuto` (`codex-acp` normalizes to `codex`).
const codex = resolveSessionMemory(extended, "codex-acp");
expect(codex.supportsAutoControl).toBe(true);
expect(codex.groups.flatMap((g) => g.items.map((i) => i.id))).toContain("codexAuto::memory_summary.md");
// Gemini sees `GEMINI.md` and lacks native auto-memory toggle (`supportsAutoControl: false`).
const gemini = resolveSessionMemory(extended, "gemini");
expect(gemini.supportsAutoControl).toBe(false);
expect(gemini.groups.flatMap((g) => g.items.map((i) => i.id))).toContain("geminiWorkspace::GEMINI.md");
// OpenCode (generic ACP) sees portable `workspace::AGENTS.md` only.
const opencode = resolveSessionMemory(extended, "opencode");
expect(opencode.supportsAutoControl).toBe(false);
expect(opencode.groups.flatMap((g) => g.items.map((i) => i.id))).toEqual([
"workspace::AGENTS.md",
"workspace::untrusted.md",
]);
// Named Hermes profile (`hermes-work`) normalizes to `hermes` and sees `hermesAuto` with auto-memory control!
const hermes = resolveSessionMemory(extended, "hermes-work");
expect(hermes.supportsAutoControl).toBe(true);
expect(hermes.groups.flatMap((g) => g.items.map((i) => i.id))).toContain("hermesAuto::work/USER.md");
});
it("opens Session Memory when clicking 'Memory files' in the ContextMeter breakdown", async () => {
const onOpenMemory = vi.fn();
const task: AgentTask = {
id: "task-1",
title: "Refactor",
provider: "claude",
model: "claude-sonnet-5-5",
status: "ready",
isolation: "local",
cwd: "/repo",
branch: "main",
base: null,
baseCommit: null,
error: null,
createdMs: 1,
updatedMs: 1,
config: [],
commands: [],
usage: { used: 12_000, size: 200_000, cost: null },
queue: [],
capabilities: {
images: true,
fork: true,
rollback: true,
steer: true,
backgroundTasks: true,
contextUsage: true,
},
resetsMs: null,
retry: null,
background: [],
stats: { tokens: null, compressions: 0 },
};
render(
<ContextMeter
task={task}
usage={task.usage}
limits={[]}
cost={null}
turnTokens={null}
now={Date.now()}
onOpenMemory={onOpenMemory}
/>,
);
fireEvent.click(screen.getByRole("button", { name: /Context window/ }));
const memBtn = await screen.findByRole("button", { name: "Memory files" });
fireEvent.click(memBtn);
expect(onOpenMemory).toHaveBeenCalledOnce();
});
});