Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/five-candles-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@voltagent/core": patch
---

fix(core): persist reasoning and tool parts across step checkpoint flushes (#1130)

When `conversationPersistence.mode = "step"` was used with tool calls, some checkpoint flows could
persist incomplete assistant messages and lose non-text parts in stored conversation history.

This update preserves complete assistant message parts during checkpoint merges and persistence
flushes, including reasoning, tool-call, tool-result, and text parts.

Regression coverage is expanded with end-to-end agent persistence tests against both LibSQL and
PostgreSQL backends to reduce the chance of similar regressions.
21 changes: 20 additions & 1 deletion .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,28 @@ jobs:
- name: Test Package
run: lerna run test --scope "${{ steps.scope.outputs.scope }}"

e2e-tests:
runs-on: ubuntu-latest
name: E2E Tests
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Dependencies
run: pnpm install
env:
NODE_OPTIONS: "--max-old-space-size=8192"
- name: Build E2E Dependencies
run: pnpm lerna run build --scope @voltagent/e2e --include-dependencies
- name: Run E2E Tests
run: pnpm --filter @voltagent/e2e test

prerelease:
name: Prerelease
needs: test-packages
needs: [test-packages, e2e-tests]
runs-on: ubuntu-latest
permissions:
contents: write
Expand Down
53 changes: 53 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,56 @@ jobs:
npm run test:integration:ci
env:
DATABASE_URL: postgresql://test:test@localhost:5432/voltagent_test

e2e-tests:
runs-on: ubuntu-latest
name: E2E Tests
concurrency:
group: ${{ github.ref }}-e2e-tests
cancel-in-progress: true
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check for E2E-related changes
id: check_e2e
run: |
BASE_SHA="${{ github.event.pull_request.base.sha }}"

if ! git cat-file -e "$BASE_SHA^{commit}" 2>/dev/null; then
git fetch origin "${{ github.event.pull_request.base.ref }}" --depth=1
BASE_SHA="origin/${{ github.event.pull_request.base.ref }}"
fi

if git diff --name-only "$BASE_SHA"...HEAD | grep -E '^(\.github/workflows/pull-request\.yml|packages/e2e/|packages/core/|packages/libsql/|packages/postgres/|packages/internal/|packages/logger/|package\.json|pnpm-lock\.yaml)'; then
echo "e2e_changed=true" >> $GITHUB_OUTPUT
else
echo "e2e_changed=false" >> $GITHUB_OUTPUT
fi

- name: Skip if no E2E-related changes
if: steps.check_e2e.outputs.e2e_changed == 'false'
run: echo "No E2E-related changes detected, skipping E2E tests"

- uses: pnpm/action-setup@v4
if: steps.check_e2e.outputs.e2e_changed == 'true'
- uses: actions/setup-node@v4
if: steps.check_e2e.outputs.e2e_changed == 'true'
with:
node-version: 20
cache: "pnpm"

- name: Install Dependencies
if: steps.check_e2e.outputs.e2e_changed == 'true'
run: pnpm install
env:
NODE_OPTIONS: "--max-old-space-size=8192"

- name: Build E2E Dependencies
if: steps.check_e2e.outputs.e2e_changed == 'true'
run: pnpm lerna run build --scope @voltagent/e2e --include-dependencies

- name: Run E2E Tests
if: steps.check_e2e.outputs.e2e_changed == 'true'
run: pnpm --filter @voltagent/e2e test
21 changes: 20 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,27 @@ jobs:
- name: Test Package
run: lerna run test --scope "${{ steps.scope.outputs.scope }}"

e2e-tests:
runs-on: ubuntu-latest
name: E2E Tests
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"
- name: Install Dependencies
run: pnpm install
env:
NODE_OPTIONS: "--max-old-space-size=8192"
- name: Build E2E Dependencies
run: pnpm lerna run build --scope @voltagent/e2e --include-dependencies
- name: Run E2E Tests
run: pnpm --filter @voltagent/e2e test

publish:
needs: test-packages
needs: [test-packages, e2e-tests]
permissions:
contents: write
id-token: write
Expand Down
72 changes: 72 additions & 0 deletions packages/core/src/agent/conversation-buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,78 @@ describe("ConversationBuffer", () => {
expect(reasoningParts[1]?.providerMetadata).toEqual({ openai: { itemId: "rs_TWO" } });
});

it("merges subsequent response checkpoints with same assistant id after an intermediate flush", () => {
const buffer = new ConversationBuffer();

buffer.addModelMessages(
[
{
id: "response-1",
role: "assistant",
content: [
{
type: "tool-call",
toolCallId: "call-1",
toolName: "getWeather",
input: { location: "Berlin" },
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "getWeather",
output: { condition: "sunny" },
},
],
},
],
"response",
);

const firstFlush = buffer.drainPendingMessages();
expect(firstFlush).toHaveLength(1);
expect(firstFlush[0]?.id).toBe("response-1");

buffer.addModelMessages(
[
{
id: "response-1",
role: "assistant",
content: [
{
type: "text",
text: "Berlin hava durumu güneşli.",
},
],
},
],
"response",
);

const secondFlush = buffer.drainPendingMessages();
expect(secondFlush).toHaveLength(1);
expect(secondFlush[0]?.id).toBe("response-1");
expect(secondFlush[0]?.parts).toEqual([
{
type: "tool-getWeather",
toolCallId: "call-1",
state: "output-available",
input: { location: "Berlin" },
output: { condition: "sunny" },
providerExecuted: false,
},
{ type: "step-start" },
{ type: "text", text: "Berlin hava durumu güneşli." },
]);

const all = buffer.getAllMessages();
expect(all).toHaveLength(1);
});

it("preserves preloaded memory messages without marking them pending", () => {
const existing: UIMessage[] = [
{
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/agent/conversation-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ export class ConversationBuffer {
}

if (source === "response") {
const hasSameAssistantId =
typeof message.id === "string" &&
message.id.length > 0 &&
typeof lastMessage.id === "string" &&
lastMessage.id.length > 0 &&
message.id === lastMessage.id;

if (hasSameAssistantId) {
this.mergeAssistantMessage(message);
this.activeAssistantMessageId = lastMessage.id;
return;
}

const isActiveTarget =
this.activeAssistantMessageId !== undefined &&
this.activeAssistantMessageId === lastMessage.id;
Expand Down
101 changes: 101 additions & 0 deletions packages/core/src/agent/memory-persistence.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,105 @@ describe("Conversation persistence integration", () => {
output: { results: ["Result A", "Result B"] },
});
});

it("keeps tool call/result parts when final text arrives after an intermediate checkpoint flush", async () => {
const logger = createLogger();
const memoryManager = {
saveMessage: vi.fn().mockResolvedValue(undefined),
};

const buffer = new ConversationBuffer(undefined, logger as any);
const queue = new MemoryPersistQueue(memoryManager as any, {
debounceMs: 0,
logger: logger as any,
});
const oc = createOperationContext(logger);

buffer.addModelMessages(
[
{
id: "assistant-checkpoint",
role: "assistant",
content: [
{
type: "reasoning",
text: "First I should read the calendar for that date.",
},
{
type: "tool-call",
toolCallId: "call-1",
toolName: "checkCalendar",
input: { date: "2023-11-15" },
},
],
},
{
role: "tool",
content: [
{
type: "tool-result",
toolCallId: "call-1",
toolName: "checkCalendar",
output: {
events: [{ title: "Team meeting" }],
},
},
],
},
],
"response",
);

await queue.flush(buffer, oc);

buffer.addModelMessages(
[
{
id: "assistant-checkpoint",
role: "assistant",
content: [
{
type: "text",
text: "You have one event: Team meeting.",
},
],
},
],
"response",
);

await queue.flush(buffer, oc);

expect(memoryManager.saveMessage).toHaveBeenCalledTimes(2);

const persistedAssistantMessages = memoryManager.saveMessage.mock.calls.map(
(call) => call[1] as UIMessage,
);
const firstMessage = persistedAssistantMessages[0];
const finalMessage = persistedAssistantMessages[1];

expect(firstMessage?.id).toBe("assistant-checkpoint");
expect(finalMessage?.id).toBe("assistant-checkpoint");
expect(finalMessage?.parts.map((part) => part.type)).toEqual([
"reasoning",
"tool-checkCalendar",
"step-start",
"text",
]);
expect(finalMessage?.parts[0]).toMatchObject({
type: "reasoning",
text: "First I should read the calendar for that date.",
});
expect(finalMessage?.parts[1]).toMatchObject({
toolCallId: "call-1",
state: "output-available",
output: {
events: [{ title: "Team meeting" }],
},
});
expect(finalMessage?.parts[3]).toMatchObject({
type: "text",
text: "You have one event: Team meeting.",
});
});
});
Loading