
















@@ -83,6 +83,7 @@ const installRunEmbeddedMocks = () => {
8383};
84848585let runEmbeddedPiAgent: typeof import("./pi-embedded-runner/run.js").runEmbeddedPiAgent;
86+let authProfileUsageTesting: typeof import("./auth-profiles/usage.js").__testing;
8687let createDiagnosticLogRecordCaptureFn: typeof import("../logging/test-helpers/diagnostic-log-capture.js").createDiagnosticLogRecordCapture;
8788let cleanupLogCapture: (() => void) | undefined;
8889let resetLoggerFn: typeof import("../logging/logger.js").resetLogger;
@@ -93,6 +94,7 @@ beforeAll(async () => {
9394vi.resetModules();
9495installRunEmbeddedMocks();
9596({ runEmbeddedPiAgent } = await import("./pi-embedded-runner/run.js"));
97+({ __testing: authProfileUsageTesting } = await import("./auth-profiles/usage.js"));
9698({ createDiagnosticLogRecordCapture: createDiagnosticLogRecordCaptureFn } =
9799await import("../logging/test-helpers/diagnostic-log-capture.js"));
98100({ resetLogger: resetLoggerFn, setLoggerOverride: setLoggerOverrideFn } =
@@ -128,6 +130,7 @@ beforeEach(() => {
128130129131afterEach(() => {
130132globalThis.fetch = originalFetch;
133+authProfileUsageTesting.setDepsForTest(null);
131134cleanupLogCapture?.();
132135cleanupLogCapture = undefined;
133136setLoggerOverrideFn(null);
@@ -905,6 +908,46 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
905908expect(sleepWithAbortMock).not.toHaveBeenCalled();
906909});
907910911+it("starts the retry attempt before prompt failure cooldown marking finishes", async () => {
912+let releaseMark: (() => void) | undefined;
913+const markCanFinish = new Promise<void>((resolve) => {
914+releaseMark = resolve;
915+});
916+let markStarted = false;
917+authProfileUsageTesting.setDepsForTest({
918+updateAuthProfileStoreWithLock: async () => {
919+markStarted = true;
920+await markCanFinish;
921+return null;
922+},
923+});
924+925+try {
926+await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
927+await writeAuthStore(agentDir);
928+mockPromptErrorThenSuccessfulAttempt("rate limit exceeded");
929+930+const runPromise = runAutoPinnedOpenAiTurn({
931+ agentDir,
932+ workspaceDir,
933+sessionKey: "agent:test:prompt-deferred-mark",
934+runId: "run:prompt-deferred-mark",
935+});
936+937+await vi.waitFor(() => expect(runEmbeddedAttemptMock).toHaveBeenCalledTimes(2));
938+expect(markStarted).toBe(true);
939+releaseMark?.();
940+releaseMark = undefined;
941+await runPromise;
942+943+const usageStats = await readUsageStats(agentDir);
944+expect(typeof usageStats["openai:p2"]?.lastUsed).toBe("number");
945+});
946+} finally {
947+releaseMark?.();
948+}
949+});
950+908951it("uses configured overload backoff before rotating profiles", async () => {
909952const { usageStats } = await runAutoPinnedRotationCase({
910953errorMessage: '{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}',
@@ -1507,8 +1550,9 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
15071550});
1508155115091552it("skips profiles in cooldown when rotating after failure", async () => {
1510-await withTimedAgentWorkspace(async ({ agentDir, workspaceDir, now }) => {
1553+await withAgentWorkspace(async ({ agentDir, workspaceDir }) => {
15111554const authPath = path.join(agentDir, "auth-profiles.json");
1555+const p2CooldownUntil = Date.now() + 60 * 60 * 1000;
15121556const payload = {
15131557version: 1,
15141558profiles: {
@@ -1518,7 +1562,7 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
15181562},
15191563usageStats: {
15201564"openai:p1": { lastUsed: 1 },
1521-"openai:p2": { cooldownUntil: now + 60 * 60 * 1000 }, // p2 in cooldown
1565+"openai:p2": { cooldownUntil: p2CooldownUntil }, // p2 in cooldown
15221566"openai:p3": { lastUsed: 3 },
15231567},
15241568};
@@ -1536,7 +1580,7 @@ describe("runEmbeddedPiAgent auth profile rotation", () => {
15361580const usageStats = await readUsageStats(agentDir);
15371581expect(typeof usageStats["openai:p1"]?.lastUsed).toBe("number");
15381582expect(typeof usageStats["openai:p3"]?.lastUsed).toBe("number");
1539-expect(usageStats["openai:p2"]?.cooldownUntil).toBe(now + 60 * 60 * 1000);
1583+expect(usageStats["openai:p2"]?.cooldownUntil).toBe(p2CooldownUntil);
15401584});
15411585});
15421586});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。