refactor: unify typing dispatch lifecycle and policy boundaries

This commit is contained in:
Peter Steinberger
2026-02-26 17:36:09 +01:00
parent 6fd9ec97de
commit 273973d374
19 changed files with 420 additions and 164 deletions

View File

@@ -0,0 +1,65 @@
import { describe, expect, it, vi } from "vitest";
import { createTypingStartGuard } from "./typing-start-guard.js";
describe("createTypingStartGuard", () => {
it("skips starts when sealed", async () => {
const start = vi.fn();
const guard = createTypingStartGuard({
isSealed: () => true,
});
const result = await guard.run(start);
expect(result).toBe("skipped");
expect(start).not.toHaveBeenCalled();
});
it("trips breaker after max consecutive failures", async () => {
const onStartError = vi.fn();
const onTrip = vi.fn();
const guard = createTypingStartGuard({
isSealed: () => false,
onStartError,
onTrip,
maxConsecutiveFailures: 2,
});
const start = vi.fn().mockRejectedValue(new Error("fail"));
const first = await guard.run(start);
const second = await guard.run(start);
const third = await guard.run(start);
expect(first).toBe("failed");
expect(second).toBe("tripped");
expect(third).toBe("skipped");
expect(onStartError).toHaveBeenCalledTimes(2);
expect(onTrip).toHaveBeenCalledTimes(1);
});
it("resets breaker state", async () => {
const guard = createTypingStartGuard({
isSealed: () => false,
maxConsecutiveFailures: 1,
});
const failStart = vi.fn().mockRejectedValue(new Error("fail"));
const okStart = vi.fn().mockResolvedValue(undefined);
const trip = await guard.run(failStart);
expect(trip).toBe("tripped");
expect(guard.isTripped()).toBe(true);
guard.reset();
const started = await guard.run(okStart);
expect(started).toBe("started");
expect(guard.isTripped()).toBe(false);
});
it("rethrows start errors when configured", async () => {
const guard = createTypingStartGuard({
isSealed: () => false,
rethrowOnError: true,
});
const start = vi.fn().mockRejectedValue(new Error("boom"));
await expect(guard.run(start)).rejects.toThrow("boom");
});
});

View File

@@ -0,0 +1,63 @@
export type TypingStartGuard = {
run: (start: () => Promise<void> | void) => Promise<"started" | "skipped" | "failed" | "tripped">;
reset: () => void;
isTripped: () => boolean;
};
export function createTypingStartGuard(params: {
isSealed: () => boolean;
shouldBlock?: () => boolean;
onStartError?: (err: unknown) => void;
maxConsecutiveFailures?: number;
onTrip?: () => void;
rethrowOnError?: boolean;
}): TypingStartGuard {
const maxConsecutiveFailures =
typeof params.maxConsecutiveFailures === "number" && params.maxConsecutiveFailures > 0
? Math.floor(params.maxConsecutiveFailures)
: undefined;
let consecutiveFailures = 0;
let tripped = false;
const isBlocked = () => {
if (params.isSealed()) {
return true;
}
if (tripped) {
return true;
}
return params.shouldBlock?.() === true;
};
const run: TypingStartGuard["run"] = async (start) => {
if (isBlocked()) {
return "skipped";
}
try {
await start();
consecutiveFailures = 0;
return "started";
} catch (err) {
consecutiveFailures += 1;
params.onStartError?.(err);
if (params.rethrowOnError) {
throw err;
}
if (maxConsecutiveFailures && consecutiveFailures >= maxConsecutiveFailures) {
tripped = true;
params.onTrip?.();
return "tripped";
}
return "failed";
}
};
return {
run,
reset: () => {
consecutiveFailures = 0;
tripped = false;
},
isTripped: () => tripped,
};
}

View File

@@ -1,4 +1,5 @@
import { createTypingKeepaliveLoop } from "./typing-lifecycle.js";
import { createTypingStartGuard } from "./typing-start-guard.js";
export type TypingCallbacks = {
onReplyStart: () => Promise<void>;
@@ -26,28 +27,19 @@ export function createTypingCallbacks(params: CreateTypingCallbacksParams): Typi
const maxDurationMs = params.maxDurationMs ?? 60_000; // Default 60s TTL
let stopSent = false;
let closed = false;
let consecutiveFailures = 0;
let breakerTripped = false;
let ttlTimer: ReturnType<typeof setTimeout> | undefined;
const startGuard = createTypingStartGuard({
isSealed: () => closed,
onStartError: params.onStartError,
maxConsecutiveFailures,
onTrip: () => {
keepaliveLoop.stop();
},
});
const fireStart = async (): Promise<void> => {
if (closed) {
return;
}
if (breakerTripped) {
return;
}
try {
await params.start();
consecutiveFailures = 0;
} catch (err) {
consecutiveFailures += 1;
params.onStartError(err);
if (consecutiveFailures >= maxConsecutiveFailures) {
breakerTripped = true;
keepaliveLoop.stop();
}
}
await startGuard.run(() => params.start());
};
const keepaliveLoop = createTypingKeepaliveLoop({
@@ -81,12 +73,11 @@ export function createTypingCallbacks(params: CreateTypingCallbacksParams): Typi
return;
}
stopSent = false;
breakerTripped = false;
consecutiveFailures = 0;
startGuard.reset();
keepaliveLoop.stop();
clearTtlTimer();
await fireStart();
if (breakerTripped) {
if (startGuard.isTripped()) {
return;
}
keepaliveLoop.start();