Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): replay memory provider tools with live history (#4310)

e0-gh-vercel-connect[bot]42c57a9ccb14
diff --git a/.changeset/memory-tool-replay-history.md b/.changeset/memory-tool-replay-history.md
index 100482f43..401ac19fb 100644
--- a/.changeset/memory-tool-replay-history.md
+++ b/.changeset/memory-tool-replay-history.md
@@ -2,4 +2,4 @@
"eve": patch
---
-Memory provider tools now survive a mid-turn restart when the turn's history holds non-JSON tool results, such as dates. eve no longer snapshots history into each tool's replay state, so a replayed `tools()` call receives empty `messages`.
+Memory provider tools now survive a mid-turn restart when the turn's history holds non-JSON tool results, such as dates. eve no longer snapshots history into each tool's replay state; a replayed `tools()` call receives the same `messages` the current process resolved the turn's tools with.
diff --git a/docs/memory/custom-provider.md b/docs/memory/custom-provider.md
index 0dd16a95a..fba0c71da 100644
--- a/docs/memory/custom-provider.md
+++ b/docs/memory/custom-provider.md
@@ -144,8 +144,9 @@ authored tools.
A tool closes over the locked scope for the current turn, so it cannot be
redirected to another tenant or caller by the model. eve keeps each tool
callback replayable after a process restart or redeployment. On replay, eve
-calls `tools()` again with the captured `memory`, `turn`, and session context,
-but with empty `messages`, so a tool's identity must not depend on history.
+calls `tools()` again with the captured `memory`, `turn`, and session context.
+`messages` is the history the current process resolved the turn's tools with,
+so after a mid-turn restart it also includes the turn's earlier tool results.
## Lifecycle
diff --git a/packages/eve/src/context/memory-tools.test.ts b/packages/eve/src/context/memory-tools.test.ts
index f7b4bebbd..46198ae0a 100644
--- a/packages/eve/src/context/memory-tools.test.ts
+++ b/packages/eve/src/context/memory-tools.test.ts
@@ -1,3 +1,4 @@
+import type { ModelMessage } from "ai";
import { z } from "#compiled/zod/index.js";
import { isToolSchema } from "#tools/schema.js";
import { describe, expect, it } from "vitest";
@@ -13,13 +14,14 @@ import {
SessionIdKey,
SessionKey,
StaticModelReferenceKey,
+ TurnDynamicToolMetadataKey,
TurnMemoryLocksKey,
} from "#context/keys.js";
import { createMemoryToolDynamicDefinition } from "#context/memory-tools.js";
import { resolveApprovalPolicy } from "#approval/definition.js";
import { defineTool } from "#tools/definition.js";
import { clearDurableDynamicCallbacks } from "#tools/durable-callbacks.js";
-import { defineMemory } from "#public/memory/index.js";
+import { defineMemory, type MemoryToolsContext } from "#public/memory/index.js";
import { always } from "#public/tools/approval/index.js";
import type { ResolvedDynamicToolResolver } from "#runtime/types.js";
import { createMemoryLock } from "#shared/memory-state.js";
@@ -55,21 +57,27 @@ function createContext(scope: string) {
return ctx;
}
-function resolver(version: () => number): ResolvedDynamicToolResolver {
+function resolver(
+ version: () => number,
+ onTools?: (context: MemoryToolsContext) => void,
+): ResolvedDynamicToolResolver {
const definition = defineMemory({
description: "Manage the profile.",
provider: {
recall: { "turn.started": async () => null },
- tools: async (context) => ({
- save: defineTool({
- approval: always(),
- description: "Save a field.",
- execute: async () => `${version()}:${String(context.memory.scope.value)}`,
- inputSchema: z.object({
- scope: z.string().refine((value) => value === context.memory.scope.value),
+ tools: async (context) => {
+ onTools?.(context);
+ return {
+ save: defineTool({
+ approval: always(),
+ description: "Save a field.",
+ execute: async () => `${version()}:${String(context.memory.scope.value)}`,
+ inputSchema: z.object({
+ scope: z.string().refine((value) => value === context.memory.scope.value),
+ }),
}),
- }),
- }),
+ };
+ },
},
scope: "unused",
});
@@ -147,9 +155,13 @@ describe("memory provider tools", () => {
});
});
- it("rebinds after a restart when mid-turn history holds non-JSON values", async () => {
+ it("rebinds after a restart with live history and keeps it out of the durable closure", async () => {
const ctx = createContext("user_1");
- const compiledResolver = resolver(() => 1);
+ const seen: (readonly unknown[])[] = [];
+ const compiledResolver = resolver(
+ () => 1,
+ (context) => seen.push(context.messages),
+ );
await contextStorage.run(
ctx,
async () =>
@@ -164,25 +176,27 @@ describe("memory provider tools", () => {
// A fresh process resumes mid-turn: history now carries this turn's tool
// results, which are durable values rather than plain JSON.
+ const history: ModelMessage[] = [
+ { content: "hello", role: "user" },
+ {
+ content: [
+ {
+ output: { type: "json", value: { createdAt: new Date(0) } as never },
+ toolCallId: "call_0",
+ toolName: "lookup",
+ type: "tool-result",
+ },
+ ],
+ role: "tool",
+ },
+ ];
await rebindMissingCompiledDynamicToolCallbacks({
ctx,
event,
- messages: [
- { content: "hello", role: "user" },
- {
- content: [
- {
- output: { type: "json", value: { createdAt: new Date(0) } as never },
- toolCallId: "call_0",
- toolName: "lookup",
- type: "tool-result",
- },
- ],
- role: "tool",
- },
- ],
+ messages: history,
resolvers: [compiledResolver],
});
+ seen.length = 0;
const [replayed] = buildDynamicTools(ctx);
const output = await contextStorage.run(
@@ -190,6 +204,9 @@ describe("memory provider tools", () => {
async () => await replayed?.execute?.({}, { messages: [], toolCallId: "call_1" }),
);
expect(output).toBe("1:user_1");
+ expect(seen).toEqual([history]);
+ const [metadata] = ctx.require(TurnDynamicToolMetadataKey);
+ expect(JSON.stringify(metadata)).not.toContain("hello");
});
it("omits tools when the provider has no tool factory", async () => {
diff --git a/packages/eve/src/context/memory-tools.ts b/packages/eve/src/context/memory-tools.ts
index 00693ae0b..1048e2a7c 100644
--- a/packages/eve/src/context/memory-tools.ts
+++ b/packages/eve/src/context/memory-tools.ts
@@ -67,19 +67,20 @@ function createProviderToolCallbacks(input: {
readonly key: string;
readonly tool: MemoryToolSet[string];
}) {
- // History is not part of a tool's identity and can hold durable non-JSON
- // values (dates, bytes) once a turn has run tools, so replay omits it.
- const closure = parseJsonObject({
- context: { ...input.context, messages: [] },
- key: input.key,
- });
+ // History stays out of the durable closure: it is already durable session
+ // state and can hold non-JSON values (dates, bytes) once a turn has run
+ // tools. Replay reuses the messages this process resolved the tools with.
+ const { messages, ...durableContext } = input.context;
+ const closure = parseJsonObject({ context: durableContext, key: input.key });
const loadTool = async (rawClosure: JsonObject) => {
const key = rawClosure.key;
const context = rawClosure.context;
if (typeof key !== "string" || typeof context !== "object" || context === null) {
throw new Error("Memory provider tool callback has an invalid durable closure.");
}
- const tools = await input.definition.provider.tools?.(readMemoryToolsContext(context));
+ const tools = await input.definition.provider.tools?.(
+ readMemoryToolsContext({ ...context, messages }),
+ );
const tool = tools?.[key];
if (tool === undefined || !isBrandedToolEntry(tool)) {
throw new Error(`Memory provider tool "${key}" was removed or renamed.`);