




























@@ -34,6 +34,78 @@ function createReplyOperation() {
3434} as never;
3535}
363637+type RefreshQueuedFollowupSessionParams = {
38+key?: string;
39+previousSessionId?: string;
40+nextSessionId?: string;
41+nextSessionFile?: string;
42+};
43+44+type ModelFallbackParams = {
45+provider?: string;
46+model?: string;
47+fallbacksOverride?: unknown[];
48+};
49+50+type EmbeddedPiAgentParams = {
51+provider?: string;
52+model?: string;
53+authProfileId?: unknown;
54+authProfileIdSource?: unknown;
55+prompt?: string;
56+transcriptPrompt?: string;
57+memoryFlushWritePath?: string;
58+silentExpected?: boolean;
59+extraSystemPrompt?: string;
60+bootstrapPromptWarningSignaturesSeen?: string[];
61+bootstrapPromptWarningSignature?: string;
62+};
63+64+type CompactEmbeddedPiSessionParams = {
65+sessionKey?: string;
66+sandboxSessionKey?: string;
67+currentTokenCount?: number;
68+sessionFile?: string;
69+sessionId?: string;
70+trigger?: string;
71+};
72+73+function requireRefreshQueuedFollowupSessionCall(index = 0) {
74+const call = refreshQueuedFollowupSessionMock.mock.calls[index]?.[0] as
75+| RefreshQueuedFollowupSessionParams
76+| undefined;
77+if (!call) {
78+throw new Error(`refreshQueuedFollowupSession call ${index} missing`);
79+}
80+return call;
81+}
82+83+function requireModelFallbackCall(index = 0) {
84+const call = runWithModelFallbackMock.mock.calls[index]?.[0] as ModelFallbackParams | undefined;
85+if (!call) {
86+throw new Error(`runWithModelFallback call ${index} missing`);
87+}
88+return call;
89+}
90+91+function requireEmbeddedPiAgentCall(index = 0) {
92+const call = runEmbeddedPiAgentMock.mock.calls[index]?.[0] as EmbeddedPiAgentParams | undefined;
93+if (!call) {
94+throw new Error(`runEmbeddedPiAgent call ${index} missing`);
95+}
96+return call;
97+}
98+99+function requireCompactEmbeddedPiSessionCall(index = 0) {
100+const call = compactEmbeddedPiSessionMock.mock.calls[index]?.[0] as
101+| CompactEmbeddedPiSessionParams
102+| undefined;
103+if (!call) {
104+throw new Error(`compactEmbeddedPiSession call ${index} missing`);
105+}
106+return call;
107+}
108+37109describe("runMemoryFlushIfNeeded", () => {
38110let rootDir = "";
39111@@ -157,23 +229,18 @@ describe("runMemoryFlushIfNeeded", () => {
157229expect(entry?.sessionId).toBe("session-rotated");
158230expect(followupRun.run.sessionId).toBe("session-rotated");
159231expect(runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1);
160-const flushCall = runEmbeddedPiAgentMock.mock.calls[0]?.[0] as {
161-prompt?: string;
162-transcriptPrompt?: string;
163-memoryFlushWritePath?: string;
164-silentExpected?: boolean;
165-};
232+const flushCall = requireEmbeddedPiAgentCall();
166233expect(flushCall.prompt).toContain("Pre-compaction memory flush.");
167234expect(flushCall.transcriptPrompt).toBe("");
168235expect(flushCall.prompt).not.toBe(flushCall.transcriptPrompt);
169236expect(flushCall.memoryFlushWritePath).toMatch(/^memory\/\d{4}-\d{2}-\d{2}\.md$/);
170237expect(flushCall.silentExpected).toBe(true);
171-expect(refreshQueuedFollowupSessionMock).toHaveBeenCalledWith({
172- key: sessionKey,
173- previousSessionId: "session",
174- nextSessionId: "session-rotated",
175- nextSessionFile: expect.stringContaining("session-rotated.jsonl"),
176-});
238+expect(refreshQueuedFollowupSessionMock).toHaveBeenCalledTimes(1);
239+const refreshCall = requireRefreshQueuedFollowupSessionCall();
240+expect(refreshCall.key).toBe(sessionKey);
241+expect(refreshCall.previousSessionId).toBe("session");
242+expect(refreshCall.nextSessionId).toBe("session-rotated");
243+expect(refreshCall.nextSessionFile).toContain("session-rotated.jsonl");
177244178245const persisted = JSON.parse(await fs.readFile(storePath, "utf8")) as {
179246main: SessionEntry;
@@ -229,21 +296,17 @@ describe("runMemoryFlushIfNeeded", () => {
229296replyOperation: createReplyOperation(),
230297});
231298232-expect(runWithModelFallbackMock).toHaveBeenCalledWith(
233-expect.objectContaining({
234-provider: "ollama",
235-model: "qwen3:8b",
236-fallbacksOverride: [],
237-}),
238-);
239-expect(runEmbeddedPiAgentMock).toHaveBeenCalledWith(
240-expect.objectContaining({
241-provider: "ollama",
242-model: "qwen3:8b",
243-authProfileId: undefined,
244-authProfileIdSource: undefined,
245-}),
246-);
299+expect(runWithModelFallbackMock).toHaveBeenCalledTimes(1);
300+const fallbackCall = requireModelFallbackCall();
301+expect(fallbackCall.provider).toBe("ollama");
302+expect(fallbackCall.model).toBe("qwen3:8b");
303+expect(fallbackCall.fallbacksOverride).toEqual([]);
304+expect(runEmbeddedPiAgentMock).toHaveBeenCalledTimes(1);
305+const agentCall = requireEmbeddedPiAgentCall();
306+expect(agentCall.provider).toBe("ollama");
307+expect(agentCall.model).toBe("qwen3:8b");
308+expect(agentCall.authProfileId).toBeUndefined();
309+expect(agentCall.authProfileIdSource).toBeUndefined();
247310});
248311249312it("skips memory flush for CLI providers", async () => {
@@ -356,12 +419,10 @@ describe("runMemoryFlushIfNeeded", () => {
356419replyOperation: createReplyOperation(),
357420});
358421359-expect(compactEmbeddedPiSessionMock).toHaveBeenCalledWith(
360-expect.objectContaining({
361-sessionKey: "agent:main:main",
362-sandboxSessionKey: "agent:main:telegram:default:direct:12345",
363-}),
364-);
422+expect(compactEmbeddedPiSessionMock).toHaveBeenCalledTimes(1);
423+const compactCall = requireCompactEmbeddedPiSessionCall();
424+expect(compactCall.sessionKey).toBe("agent:main:main");
425+expect(compactCall.sandboxSessionKey).toBe("agent:main:telegram:default:direct:12345");
365426});
366427367428it("updates the active preflight run after transcript rotation", async () => {
@@ -529,12 +590,10 @@ describe("runMemoryFlushIfNeeded", () => {
529590replyOperation: createReplyOperation(),
530591});
531592532-expect(compactEmbeddedPiSessionMock).toHaveBeenCalledWith(
533-expect.objectContaining({
534-sessionId: "session",
535-sessionFile: expect.stringContaining("active-run-session.jsonl"),
536-}),
537-);
593+expect(compactEmbeddedPiSessionMock).toHaveBeenCalledTimes(1);
594+const compactCall = requireCompactEmbeddedPiSessionCall();
595+expect(compactCall.sessionId).toBe("session");
596+expect(compactCall.sessionFile).toContain("active-run-session.jsonl");
538597});
539598540599it("keeps preflight compaction conservative for content appended after latest usage", async () => {
@@ -829,19 +888,10 @@ describe("runMemoryFlushIfNeeded", () => {
829888830889expect(entry?.compactionCount).toBe(1);
831890expect(replyOperation.setPhase).toHaveBeenCalledWith("preflight_compacting");
832-const compactCall = compactEmbeddedPiSessionMock.mock.calls[0]?.[0] as {
833-currentTokenCount?: number;
834-sessionFile?: string;
835-sessionId?: string;
836-trigger?: string;
837-};
838-expect(compactCall).toEqual(
839-expect.objectContaining({
840-sessionId: "session",
841-trigger: "budget",
842-currentTokenCount: 10,
843-}),
844-);
891+const compactCall = requireCompactEmbeddedPiSessionCall();
892+expect(compactCall.sessionId).toBe("session");
893+expect(compactCall.trigger).toBe("budget");
894+expect(compactCall.currentTokenCount).toBe(10);
845895expect(compactCall.sessionFile).toContain("large-session.jsonl");
846896});
847897@@ -936,15 +986,7 @@ describe("runMemoryFlushIfNeeded", () => {
936986replyOperation: createReplyOperation(),
937987});
938988939-const flushCall = runEmbeddedPiAgentMock.mock.calls[0]?.[0] as {
940-prompt?: string;
941-transcriptPrompt?: string;
942-extraSystemPrompt?: string;
943-bootstrapPromptWarningSignaturesSeen?: string[];
944-bootstrapPromptWarningSignature?: string;
945-memoryFlushWritePath?: string;
946-silentExpected?: boolean;
947-};
989+const flushCall = requireEmbeddedPiAgentCall();
948990expect(flushCall.prompt).toContain("Write notes.");
949991expect(flushCall.prompt).toContain("NO_REPLY");
950992expect(flushCall.prompt).toContain("MEMORY.md");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。