feat: complete TASK-WP0-08 logging

This commit is contained in:
suyx
2026-07-28 00:11:02 +08:00
parent 096a1f1279
commit 361bda2f22
21 changed files with 1050 additions and 22 deletions
+47
View File
@@ -0,0 +1,47 @@
import type { StructuredJsonlLogger } from "./structured-log.js";
export class WorkerAiCallGate {
private readonly getStorageStatus: () => "active" | "full" | "unavailable";
private readonly logger: StructuredJsonlLogger;
constructor(input: {
getStorageStatus: () => "active" | "full" | "unavailable";
logger: StructuredJsonlLogger;
}) {
this.getStorageStatus = input.getStorageStatus;
this.logger = input.logger;
}
async execute<T>(correlationId: string, objectId: string, externalCall: () => Promise<T>) {
const initialStatus = this.getStorageStatus();
if (initialStatus === "full") return { reason: "storage_full" as const, status: "blocked" as const };
if (initialStatus === "unavailable") return { reason: "storage_unavailable" as const, status: "blocked" as const };
try {
this.logger.write({
correlation_id: correlationId,
error_category: "none",
object_id: objectId,
status_category: "starting",
});
} catch {
return { reason: "storage_unavailable" as const, status: "blocked" as const };
}
const statusAfterLog = this.getStorageStatus();
if (statusAfterLog === "full") return { reason: "storage_full" as const, status: "blocked" as const };
if (statusAfterLog === "unavailable") return { reason: "storage_unavailable" as const, status: "blocked" as const };
const started = Date.now();
const value = await externalCall();
try {
this.logger.write({
correlation_id: correlationId,
duration_ms: Date.now() - started,
error_category: "none",
object_id: objectId,
status_category: "completed",
});
} catch {
// The completed call cannot be undone; the write failure blocks every later call.
}
return { status: "completed" as const, value };
}
}