Files
Moltbot/src/agents/pi-embedded-runner/usage-reporting.test.ts
Frank Yang 8306eabf85 fix(agents): forward memory flush write path (#41761)
Merged via squash.

Prepared head SHA: 0a8ebf8e5b426c5b402adc34509830f46e4bb849
Co-authored-by: frankekn <4488090+frankekn@users.noreply.github.com>
Co-authored-by: frankekn <4488090+frankekn@users.noreply.github.com>
Reviewed-by: @frankekn
2026-03-10 14:18:41 +08:00

161 lines
4.9 KiB
TypeScript

import "./run.overflow-compaction.mocks.shared.js";
import { beforeEach, describe, expect, it, vi } from "vitest";
const runtimePluginMocks = vi.hoisted(() => ({
ensureRuntimePluginsLoaded: vi.fn(),
}));
vi.mock("../runtime-plugins.js", () => ({
ensureRuntimePluginsLoaded: runtimePluginMocks.ensureRuntimePluginsLoaded,
}));
import { runEmbeddedPiAgent } from "./run.js";
import { runEmbeddedAttempt } from "./run/attempt.js";
const mockedRunEmbeddedAttempt = vi.mocked(runEmbeddedAttempt);
describe("runEmbeddedPiAgent usage reporting", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("bootstraps runtime plugins with the resolved workspace before running", async () => {
mockedRunEmbeddedAttempt.mockResolvedValueOnce({
aborted: false,
promptError: null,
timedOut: false,
sessionIdUsed: "test-session",
assistantTexts: ["Response 1"],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
await runEmbeddedPiAgent({
sessionId: "test-session",
sessionKey: "test-key",
sessionFile: "/tmp/session.json",
workspaceDir: "/tmp/workspace",
prompt: "hello",
timeoutMs: 30000,
runId: "run-plugin-bootstrap",
});
expect(runtimePluginMocks.ensureRuntimePluginsLoaded).toHaveBeenCalledWith({
config: undefined,
workspaceDir: "/tmp/workspace",
});
});
it("forwards sender identity fields into embedded attempts", async () => {
mockedRunEmbeddedAttempt.mockResolvedValueOnce({
aborted: false,
promptError: null,
timedOut: false,
sessionIdUsed: "test-session",
assistantTexts: ["Response 1"],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
await runEmbeddedPiAgent({
sessionId: "test-session",
sessionKey: "test-key",
sessionFile: "/tmp/session.json",
workspaceDir: "/tmp/workspace",
prompt: "hello",
timeoutMs: 30000,
runId: "run-sender-forwarding",
senderId: "user-123",
senderName: "Josh Lehman",
senderUsername: "josh",
senderE164: "+15551234567",
});
expect(mockedRunEmbeddedAttempt).toHaveBeenCalledWith(
expect.objectContaining({
senderId: "user-123",
senderName: "Josh Lehman",
senderUsername: "josh",
senderE164: "+15551234567",
}),
);
});
it("forwards memory flush write paths into memory-triggered attempts", async () => {
mockedRunEmbeddedAttempt.mockResolvedValueOnce({
aborted: false,
promptError: null,
timedOut: false,
sessionIdUsed: "test-session",
assistantTexts: [],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
await runEmbeddedPiAgent({
sessionId: "test-session",
sessionKey: "test-key",
sessionFile: "/tmp/session.json",
workspaceDir: "/tmp/workspace",
prompt: "flush",
timeoutMs: 30000,
runId: "run-memory-forwarding",
trigger: "memory",
memoryFlushWritePath: "memory/2026-03-10.md",
});
expect(mockedRunEmbeddedAttempt).toHaveBeenCalledWith(
expect.objectContaining({
trigger: "memory",
memoryFlushWritePath: "memory/2026-03-10.md",
}),
);
});
it("reports total usage from the last turn instead of accumulated total", async () => {
// Simulate a multi-turn run result.
// Turn 1: Input 100, Output 50. Total 150.
// Turn 2: Input 150, Output 50. Total 200.
// The accumulated usage (attemptUsage) will be the sum:
// Input: 100 + 150 = 250 (Note: runEmbeddedAttempt actually returns accumulated usage)
// Output: 50 + 50 = 100
// Total: 150 + 200 = 350
// The last assistant usage (lastAssistant.usage) will be Turn 2:
// Input: 150, Output 50, Total 200.
// We expect result.meta.agentMeta.usage.total to be 200 (last turn total).
// The bug causes it to be 350 (accumulated total).
mockedRunEmbeddedAttempt.mockResolvedValueOnce({
aborted: false,
promptError: null,
timedOut: false,
sessionIdUsed: "test-session",
assistantTexts: ["Response 1", "Response 2"],
lastAssistant: {
usage: { input: 150, output: 50, total: 200 },
stopReason: "end_turn",
},
attemptUsage: { input: 250, output: 100, total: 350 },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
const result = await runEmbeddedPiAgent({
sessionId: "test-session",
sessionKey: "test-key",
sessionFile: "/tmp/session.json",
workspaceDir: "/tmp/workspace",
prompt: "hello",
timeoutMs: 30000,
runId: "run-1",
});
// Check usage in meta
const usage = result.meta.agentMeta?.usage;
expect(usage).toBeDefined();
// Check if total matches the last turn's total (200)
// If the bug exists, it will likely be 350
expect(usage?.total).toBe(200);
});
});