Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): place compaction memory recall before the turn's user message (#4613)

roboeve[bot]351f66d428b6
diff --git a/.changeset/compaction-memory-before-question.md b/.changeset/compaction-memory-before-question.md
new file mode 100644
index 000000000..2a5ea33b1
--- /dev/null
+++ b/.changeset/compaction-memory-before-question.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Memory recalled on `compaction.completed` during a turn now joins history before the turn's latest user message, where `turn.started` recall goes, instead of after it. The question stays the last user message the model reads, with client context between the recalled memory and the question. Compaction between turns still appends the recall.
diff --git a/docs/memory/custom-provider.md b/docs/memory/custom-provider.md
index fba0c71da..aa31b1b1d 100644
--- a/docs/memory/custom-provider.md
+++ b/docs/memory/custom-provider.md
@@ -165,7 +165,9 @@ During compaction, eve excludes recalled records from the summarizer, keeps the
latest value for each keyed record plus every unkeyed record, and then calls
`recall["compaction.completed"]` against the new checkpoint. This keeps
provider content attributable and prevents a summary from turning it into
-ordinary conversation history. If raw superseded records exceed 512 entries or
+ordinary conversation history. During a turn, eve places those recalled records
+before the turn's latest user message, as it does at turn start; between turns
+they follow the checkpoint. If raw superseded records exceed 512 entries or
256 KiB, eve canonicalizes them without waiting for the normal token threshold;
this changes session history only, not the provider's store.
diff --git a/packages/eve/src/harness/compaction/step.integration.test.ts b/packages/eve/src/harness/compaction/step.integration.test.ts
index dc7c33079..2d3076630 100644
--- a/packages/eve/src/harness/compaction/step.integration.test.ts
+++ b/packages/eve/src/harness/compaction/step.integration.test.ts
@@ -1,9 +1,19 @@
+import { jsonSchema } from "ai";
import { MockLanguageModelV3 } from "ai/test";
import { afterEach, describe, expect, it, vi } from "vitest";
+import { ContextContainer, contextStorage } from "#context/container.js";
+import { SessionKey, StaticModelReferenceKey } from "#context/keys.js";
+import { dispatchMemoryLifecycleEvent } from "#context/memory-event-lifecycle.js";
import { createToolLoopHarness } from "#harness/tool-loop.js";
import { getTurnUsageState, takeSessionUsageDelta } from "#harness/turn-tag-state.js";
-import type { HarnessSession } from "#harness/types.js";
+import type { HarnessModelMessage } from "#harness/messages.js";
+import type { HarnessSession, StepInput } from "#harness/types.js";
+import { attachClientContext } from "#internal/client-context.js";
+import { foldingHandler } from "#internal/testing/session-machine.js";
+import { defineMemory } from "#public/memory/index.js";
+import type { ResolvedMemoryDefinition } from "#runtime/types.js";
+import { projectMemoryHistoryFromSessionState } from "#shared/memory-state.js";
type StreamResult = Awaited<ReturnType<MockLanguageModelV3["doStream"]>>;
type StreamPart = StreamResult["stream"] extends ReadableStream<infer Part> ? Part : never;
@@ -134,3 +144,214 @@ describe("compaction summary call", () => {
expect(getTurnUsageState(result.session.state)?.session).toMatchObject({ inputTokens: 120 });
});
});
+
+describe("memory recalled after a compaction", () => {
+ const question = "Which sources back the second claim?";
+ const brief: HarnessModelMessage = {
+ content: `Read this brief. ${"lorem ipsum ".repeat(1_500)}`,
+ kind: "user",
+ role: "user",
+ };
+ const notes: ResolvedMemoryDefinition = {
+ ...defineMemory({
+ description: "Notes about Alice's research.",
+ provider: {
+ recall: {
+ "turn.started": async () => ({ messages: [{ content: "Notes at turn start." }] }),
+ "compaction.completed": async () => ({
+ messages: [{ content: "Notes after compaction." }],
+ }),
+ },
+ },
+ scope: "alice",
+ }),
+ logicalPath: "memory/notes.ts",
+ slot: "notes",
+ sourceId: "memory/notes.ts",
+ sourceKind: "module",
+ visibility: "scope",
+ };
+
+ const finish = (unified: "stop" | "tool-calls"): StreamPart => ({
+ finishReason: { raw: undefined, unified },
+ type: "finish",
+ usage: {
+ inputTokens: { cacheRead: 0, cacheWrite: 0, noCache: 50, total: 50 },
+ outputTokens: { reasoning: 0, text: 8, total: 8 },
+ },
+ });
+ const answer = (): StreamResult =>
+ streamOf([
+ { type: "stream-start", warnings: [] },
+ { id: "reply", type: "text-start" },
+ { delta: "The second claim cites two cases.", id: "reply", type: "text-delta" },
+ { id: "reply", type: "text-end" },
+ finish("stop"),
+ ]);
+ const lookup = (preamble?: string): StreamResult =>
+ streamOf([
+ { type: "stream-start", warnings: [] },
+ ...(preamble === undefined
+ ? []
+ : ([
+ { id: "preamble", type: "text-start" },
+ { delta: preamble, id: "preamble", type: "text-delta" },
+ { id: "preamble", type: "text-end" },
+ ] satisfies StreamPart[])),
+ { input: "{}", toolCallId: "lookup-1", toolName: "lookup", type: "tool-call" },
+ finish("tool-calls"),
+ ]);
+
+ /**
+ * Runs a turn's steps with the `notes` memory slot. Returns the user-role text of each step's
+ * model request and of the resulting history, in order.
+ */
+ async function runWithNotes(options: {
+ readonly compactOnly?: boolean;
+ readonly history: HarnessSession["history"];
+ readonly input: StepInput;
+ readonly steps?: readonly (() => StreamResult)[];
+ }) {
+ const stepPrompts: string[][] = [];
+ const model = new MockLanguageModelV3({
+ doStream: async ({ prompt }) => {
+ const system = prompt[0]?.role === "system" ? prompt[0].content : "";
+ if (!system.includes("You help Alice.")) return summaryStream();
+ stepPrompts.push(
+ prompt.flatMap((message) =>
+ message.role === "user"
+ ? message.content.flatMap((part) => (part.type === "text" ? [part.text] : []))
+ : [],
+ ),
+ );
+ return (options.steps?.[stepPrompts.length - 1] ?? answer)();
+ },
+ });
+ const ctx = new ContextContainer();
+ const auth = {
+ attributes: {},
+ authenticator: "test",
+ principalId: "alice",
+ principalType: "user",
+ };
+ ctx.set(StaticModelReferenceKey, { id: "step-model" });
+ ctx.set(SessionKey, {
+ auth: { current: auth, initiator: auth },
+ sessionId: "memory-compaction-session",
+ turn: { id: "turn_0", sequence: 0 },
+ });
+ const session: HarnessSession = {
+ agent: { modelReference: { id: "step-model" }, system: "You help Alice.", tools: [] },
+ // Small enough that the long brief compacts before the turn's first model call.
+ compaction: { recentWindowSize: 1, threshold: 2_000 },
+ continuationToken: "http:memory-compaction-session",
+ history: options.history,
+ sessionId: "memory-compaction-session",
+ };
+ const result = await contextStorage.run(ctx, async () => {
+ let stepped = await createToolLoopHarness({
+ compactOnly: options.compactOnly,
+ handleEvent: foldingHandler(async (event, messages) => {
+ await dispatchMemoryLifecycleEvent({
+ appRoot: "",
+ ctx,
+ event,
+ memories: [notes],
+ messages,
+ nodeId: "__root__",
+ });
+ }),
+ historyProjector: projectMemoryHistoryFromSessionState,
+ resolveModel: async () => model,
+ tools: new Map([
+ [
+ "lookup",
+ {
+ description: "Looks up the sources.",
+ execute: async () => "Two cases.",
+ inputSchema: jsonSchema({ type: "object" }),
+ name: "lookup",
+ },
+ ],
+ ]),
+ })(session, options.input);
+ while (typeof stepped.next === "function") stepped = await stepped.next(stepped.session);
+ return stepped;
+ });
+ const history = result.session.history.flatMap((message) =>
+ message.role === "user" && typeof message.content === "string" ? [message.content] : [],
+ );
+ return { history, stepPrompts };
+ }
+
+ it("places the recall before the turn's user message, as turn.started does", async () => {
+ const { stepPrompts } = await runWithNotes({
+ history: [brief, { content: "I read the brief.", role: "assistant" }],
+ input: { message: question },
+ });
+
+ // The user-role text the model reads: the turn's question stays the latest user message.
+ expect(stepPrompts).toEqual([
+ [
+ "Notes at turn start.",
+ "Summary of our conversation so far:",
+ "Notes after compaction.",
+ question,
+ ],
+ ]);
+ });
+
+ it("keeps the client's context between the recall and the question on every step", async () => {
+ const { history, stepPrompts } = await runWithNotes({
+ history: [brief, { content: "I read the brief.", role: "assistant" }],
+ input: attachClientContext({ message: question }, ["Alice is viewing claim 2."]),
+ steps: [lookup, answer],
+ });
+
+ const expected = [
+ "Notes at turn start.",
+ "Summary of our conversation so far:",
+ "Notes after compaction.",
+ "Alice is viewing claim 2.",
+ question,
+ ];
+ expect(stepPrompts).toEqual([expected, expected]);
+ expect(history).toEqual(expected.filter((text) => text !== "Alice is viewing claim 2."));
+ });
+
+ it("places the recall before the question compaction replays", async () => {
+ const { stepPrompts } = await runWithNotes({
+ history: [],
+ input: { message: question },
+ // A long first reply pushes the second step over the threshold and folds the question
+ // into the summary, so compaction replays it.
+ steps: [() => lookup(`Let me check. ${"lorem ipsum ".repeat(1_500)}`), answer],
+ });
+
+ expect(stepPrompts).toEqual([
+ ["Notes at turn start.", question],
+ [
+ "Notes at turn start.",
+ "Summary of our conversation so far:",
+ "Notes after compaction.",
+ question,
+ ],
+ ]);
+ });
+
+ it("appends the recall after a compaction between turns", async () => {
+ const { history } = await runWithNotes({
+ compactOnly: true,
+ history: [
+ brief,
+ { content: "I read the brief.", role: "assistant" },
+ { content: question, kind: "user", role: "user" },
+ { content: "The second claim cites two cases.", role: "assistant" },
+ ],
+ input: {},
+ });
+
+ // The next turn's input follows the recall, as it follows a turn.started recall.
+ expect(history.at(-1)).toBe("Notes after compaction.");
+ });
+});
diff --git a/packages/eve/src/harness/compaction/step.ts b/packages/eve/src/harness/compaction/step.ts
index 0767ff327..40217f479 100644
--- a/packages/eve/src/harness/compaction/step.ts
+++ b/packages/eve/src/harness/compaction/step.ts
@@ -9,7 +9,11 @@ import {
type StepResult,
type ToolLoopHarnessConfig,
} from "#harness/types.js";
-import { type HarnessModelMessage, validateHarnessModelMessages } from "#harness/messages.js";
+import {
+ type HarnessModelMessage,
+ isUserModelMessage,
+ validateHarnessModelMessages,
+} from "#harness/messages.js";
import type { HistoryViewProjector } from "#shared/history-view.js";
import type { Publish } from "#harness/session-machine/commit.js";
import type { SessionAuthContext } from "#channel/types.js";
@@ -311,7 +315,9 @@ export async function maybeCompact(input: {
if (ctx !== undefined) {
const commit = drainMemoryCommit(ctx);
if (commit !== undefined) {
- messages = validateHarnessModelMessages([...messages, ...commit.recalledMessages]);
+ messages = validateHarnessModelMessages(
+ placeRecalledMemory(messages, commit.recalledMessages, input.betweenTurns === true),
+ );
session = { ...session, state: commit.state };
}
}
@@ -320,6 +326,23 @@ export async function maybeCompact(input: {
return { compacted: true, messages, session: replaceSessionHistory(session, messages) };
}
+/**
+ * Memory recalled after a compaction during a turn goes where `turn.started` puts it: before the
+ * turn's latest user message, or its replay. Between turns it follows the history, so it still
+ * comes before the next turn's input.
+ */
+function placeRecalledMemory(
+ messages: readonly ModelMessage[],
+ recalled: readonly ModelMessage[],
+ betweenTurns: boolean,
+): ModelMessage[] {
+ const index = betweenTurns
+ ? -1
+ : messages.findLastIndex((message) => isUserModelMessage(message) && message.kind === "user");
+ if (index === -1) return [...messages, ...recalled];
+ return [...messages.slice(0, index), ...recalled, ...messages.slice(index)];
+}
+
type CompactionSummaryPrompt = Parameters<CompactionSummarizer>[0];
/**