Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
268 changes: 134 additions & 134 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions src/__tests__/gateway-retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,20 @@ describe("withRetry", () => {

describe("exhausting all retries", () => {
it("throws the last error after 3 total attempts for retryable errors", async () => {
const networkErr = talonErr("network");
// retryAfterMs: 1 keeps real delays to ~1 ms each so no fake timers needed
const networkErr = talonErr("network", 1);
const fn = vi.fn(async () => {
throw networkErr;
});

await expect(withRetry(fn)).rejects.toThrow();
// withRetry is configured with retries: 2 (3 total attempts)
await expect(withRetry(fn)).rejects.toThrow();
expect(fn).toHaveBeenCalledTimes(3);
});

it("throws the last error after 3 total attempts for overloaded errors", async () => {
const fn = vi.fn(async () => {
throw talonErr("overloaded");
throw talonErr("overloaded", 1);
});

await expect(withRetry(fn)).rejects.toThrow();
Expand Down Expand Up @@ -310,11 +311,10 @@ describe("withRetry", () => {
});

it("after exhausted retries the thrown error is a TalonError", async () => {
await expect(
withRetry(async () => {
throw talonErr("network");
}),
).rejects.toBeInstanceOf(TalonError);
const fn = async () => {
throw talonErr("network", 1);
};
await expect(withRetry(fn)).rejects.toBeInstanceOf(TalonError);
});

it("non-Error throws are classified and the TalonError is thrown for non-retryable", async () => {
Expand Down
13 changes: 9 additions & 4 deletions src/core/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ export async function withRetry<T>(fn: () => Promise<T>): Promise<T> {
"gateway",
`Retry ${attempt}/3 (${classified.reason}) after ${delayMs}ms`,
);
throw classified; // rethrow to trigger p-retry delay
// Actually wait for the computed delay before rethrowing so that
// rate-limit retry-after hints (often 60 s) are honoured rather than
// silently ignored. p-retry's own minTimeout/factor backoff would
// otherwise cap the wait at 1–2 s regardless of the API's instruction.
await new Promise<void>((resolve) => setTimeout(resolve, delayMs));
throw classified; // rethrow; p-retry records the attempt
}
},
{
retries: 2, // 3 total attempts
minTimeout: 1000,
maxTimeout: 60_000,
factor: 2,
minTimeout: 0, // delay is applied manually above
maxTimeout: 0,
factor: 1,
onFailedAttempt: (err) => {
if (err.retriesLeft === 0) {
logError("gateway", `All retries exhausted: ${err.error.message}`);
Expand Down
2 changes: 1 addition & 1 deletion src/storage/cron-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function validateCronExpression(
const nextDate = cron.nextRun();
return {
valid: true,
next: (nextDate as Date).toISOString(),
next: nextDate != null ? nextDate.toISOString() : undefined,
};
} catch (err) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/storage/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function loadHistory(): void {
/* backup also corrupt */
}
logError(
"sessions",
"history",
"History data corrupt and no valid backup — starting fresh",
);
}
Expand Down
Loading