-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathruntime-observability.ts
More file actions
346 lines (328 loc) · 10.6 KB
/
Copy pathruntime-observability.ts
File metadata and controls
346 lines (328 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { existsSync, readFileSync, promises as fs } from "node:fs";
import { join } from "node:path";
import { getCodexMultiAuthDir } from "../runtime-paths.js";
interface RuntimeMetricsSnapshot {
startedAt: number;
totalRequests: number;
successfulRequests: number;
failedRequests: number;
responsesRequests: number;
authRefreshRequests: number;
diagnosticProbeRequests: number;
outboundRequestAttemptBudget: number | null;
outboundRequestAttemptsConsumed: number;
requestAttemptBudgetExhaustions: number;
poolExhaustionFastFails: number;
serverBurstFastFails: number;
rateLimitedResponses: number;
serverErrors: number;
networkErrors: number;
userAborts: number;
authRefreshFailures: number;
emptyResponseRetries: number;
accountRotations: number;
sameAccountRetries: number;
streamFailoverAttempts: number;
streamFailoverCandidatesConsidered: number;
lastStreamFailoverCandidateCount: number;
streamFailoverRecoveries: number;
streamFailoverCrossAccountRecoveries: number;
cumulativeLatencyMs: number;
lastRequestAt: number | null;
lastError: string | null;
}
export interface RuntimeObservabilitySnapshot {
version: number;
updatedAt: number;
currentRequestId: string | null;
responsesRequests: number;
authRefreshRequests: number;
diagnosticProbeRequests: number;
poolExhaustionCooldownUntil: number | null;
serverBurstCooldownUntil: number | null;
lastAccountIndex?: number | null;
lastAccountLabel?: string | null;
lastAccountEmail?: string | null;
lastAccountId?: string | null;
lastAccountUpdatedAt?: number | null;
lastPoolExhaustionReason?: string | null;
lastPoolExhaustionRetryAfterMs?: number | null;
lastPoolExhaustionSkipReasons?: Record<string, string>;
lastRuntimeResetAt?: number | null;
lastRuntimeResetReason?: string | null;
lastRuntimeReloadAt?: number | null;
lastRuntimeReloadReason?: string | null;
accountSkipReasons?: Record<string, string>;
policyBlockedIndexes?: number[];
policyBlockedReasons?: Record<string, string>;
runtimeMetrics: RuntimeMetricsSnapshot;
}
const SNAPSHOT_FILE_NAME = "runtime-observability.json";
const PERSIST_RUNTIME_SNAPSHOT = process.env.VITEST !== "true";
const RUNTIME_OBSERVABILITY_SNAPSHOT_VERSION = 1;
const RETRYABLE_SNAPSHOT_ERRORS = new Set(["EBUSY", "EPERM"]);
let snapshotState: RuntimeObservabilitySnapshot | null = null;
let pendingWrite: Promise<void> | null = null;
function getSnapshotPath(): string {
return join(getCodexMultiAuthDir(), SNAPSHOT_FILE_NAME);
}
function createDefaultSnapshot(): RuntimeObservabilitySnapshot {
return {
version: RUNTIME_OBSERVABILITY_SNAPSHOT_VERSION,
updatedAt: 0,
currentRequestId: null,
responsesRequests: 0,
authRefreshRequests: 0,
diagnosticProbeRequests: 0,
poolExhaustionCooldownUntil: null,
serverBurstCooldownUntil: null,
lastAccountIndex: null,
lastAccountLabel: null,
lastAccountEmail: null,
lastAccountId: null,
lastAccountUpdatedAt: null,
lastPoolExhaustionReason: null,
lastPoolExhaustionRetryAfterMs: null,
lastPoolExhaustionSkipReasons: {},
lastRuntimeResetAt: null,
lastRuntimeResetReason: null,
lastRuntimeReloadAt: null,
lastRuntimeReloadReason: null,
accountSkipReasons: {},
policyBlockedIndexes: [],
policyBlockedReasons: {},
runtimeMetrics: {
startedAt: 0,
totalRequests: 0,
successfulRequests: 0,
failedRequests: 0,
responsesRequests: 0,
authRefreshRequests: 0,
diagnosticProbeRequests: 0,
outboundRequestAttemptBudget: null,
outboundRequestAttemptsConsumed: 0,
requestAttemptBudgetExhaustions: 0,
poolExhaustionFastFails: 0,
serverBurstFastFails: 0,
rateLimitedResponses: 0,
serverErrors: 0,
networkErrors: 0,
userAborts: 0,
authRefreshFailures: 0,
emptyResponseRetries: 0,
accountRotations: 0,
sameAccountRetries: 0,
streamFailoverAttempts: 0,
streamFailoverCandidatesConsidered: 0,
lastStreamFailoverCandidateCount: 0,
streamFailoverRecoveries: 0,
streamFailoverCrossAccountRecoveries: 0,
cumulativeLatencyMs: 0,
lastRequestAt: null,
lastError: null,
},
};
}
function normalizePersistedSnapshot(
parsed: Partial<RuntimeObservabilitySnapshot> | null,
): RuntimeObservabilitySnapshot | null {
if (!parsed || typeof parsed !== "object") {
return null;
}
if (
typeof parsed.version === "number" &&
parsed.version !== RUNTIME_OBSERVABILITY_SNAPSHOT_VERSION
) {
return null;
}
const base = createDefaultSnapshot();
return {
...base,
...parsed,
version: RUNTIME_OBSERVABILITY_SNAPSHOT_VERSION,
runtimeMetrics: {
...base.runtimeMetrics,
...(parsed.runtimeMetrics ?? {}),
},
lastPoolExhaustionSkipReasons: {
...(parsed.lastPoolExhaustionSkipReasons ?? {}),
},
accountSkipReasons: {
...(parsed.accountSkipReasons ?? {}),
},
policyBlockedIndexes: Array.isArray(parsed.policyBlockedIndexes)
? parsed.policyBlockedIndexes.filter((value): value is number =>
Number.isInteger(value),
)
: [],
policyBlockedReasons: {
...(parsed.policyBlockedReasons ?? {}),
},
};
}
function loadPersistedRuntimeObservabilitySnapshotSync(): RuntimeObservabilitySnapshot | null {
const path = getSnapshotPath();
if (!existsSync(path)) {
return null;
}
try {
const raw = readFileSync(path, "utf-8");
const parsed = JSON.parse(raw) as Partial<RuntimeObservabilitySnapshot> | null;
return normalizePersistedSnapshot(parsed);
} catch {
return null;
}
}
function ensureSnapshotState(): RuntimeObservabilitySnapshot {
if (!snapshotState) {
snapshotState =
(PERSIST_RUNTIME_SNAPSHOT
? loadPersistedRuntimeObservabilitySnapshotSync()
: null) ?? createDefaultSnapshot();
}
return snapshotState;
}
async function writeSnapshot(snapshot: RuntimeObservabilitySnapshot): Promise<void> {
const dir = getCodexMultiAuthDir();
const path = getSnapshotPath();
// The snapshot persists account identifiers (lastAccountId/label/index), so
// keep it owner-only on POSIX like the other sensitive writers (logger,
// local-client-tokens). mode is a no-op on win32 (ACL-based).
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
// mkdir's mode only applies to a freshly-created dir; an upgrade path with a
// pre-existing multi-auth dir keeps its old (possibly permissive) perms, so
// re-assert 0o700 on POSIX. Only ENOENT is swallowed (the dir was removed by a
// concurrent process — the snapshot write below will recreate/fail as needed);
// any other chmod failure is surfaced rather than silently leaving a
// world-readable dir to hold account ids/labels.
if (process.platform !== "win32") {
try {
await fs.chmod(dir, 0o700);
} catch (error) {
if ((error as NodeJS.ErrnoException | undefined)?.code !== "ENOENT") {
throw error;
}
}
}
let lastError: unknown = null;
for (let attempt = 0; attempt < 3; attempt += 1) {
const tempPath = `${path}.${process.pid}.${Date.now()}.${attempt}.tmp`;
let moved = false;
try {
await fs.writeFile(tempPath, JSON.stringify(snapshot, null, 2), {
encoding: "utf-8",
mode: 0o600,
});
await fs.rename(tempPath, path);
moved = true;
return;
} catch (error) {
lastError = error;
const code = (error as NodeJS.ErrnoException | undefined)?.code ?? "";
if (!RETRYABLE_SNAPSHOT_ERRORS.has(code) || attempt >= 2) {
throw error;
}
} finally {
if (!moved) {
try {
await fs.unlink(tempPath);
} catch {
// Best-effort cleanup for interrupted writes.
}
}
}
}
if (lastError) {
throw lastError;
}
}
export function getRuntimeObservabilitySnapshot(): RuntimeObservabilitySnapshot {
return structuredClone(ensureSnapshotState());
}
export function mutateRuntimeObservabilitySnapshot(
mutator: (snapshot: RuntimeObservabilitySnapshot) => void,
): void {
const snapshot = ensureSnapshotState();
mutator(snapshot);
snapshot.updatedAt = Date.now();
if (!PERSIST_RUNTIME_SNAPSHOT) {
return;
}
const nextSnapshot = structuredClone(snapshot);
pendingWrite = (pendingWrite ?? Promise.resolve())
.catch(() => undefined)
.then(() => writeSnapshot(nextSnapshot))
.catch(() => undefined);
}
export function recordRuntimePoolExhaustion(params: {
reason: string;
retryAfterMs: number;
accountSkipReasons: Record<string, string>;
}): void {
mutateRuntimeObservabilitySnapshot((snapshot) => {
snapshot.lastPoolExhaustionReason = params.reason;
snapshot.lastPoolExhaustionRetryAfterMs = params.retryAfterMs;
snapshot.lastPoolExhaustionSkipReasons = { ...params.accountSkipReasons };
snapshot.accountSkipReasons = { ...params.accountSkipReasons };
});
}
export function recordRuntimeReload(reason: string): void {
mutateRuntimeObservabilitySnapshot((snapshot) => {
snapshot.lastRuntimeReloadAt = Date.now();
snapshot.lastRuntimeReloadReason = reason;
});
}
export function recordRuntimeReset(reason: string): void {
mutateRuntimeObservabilitySnapshot((snapshot) => {
snapshot.lastRuntimeResetAt = Date.now();
snapshot.lastRuntimeResetReason = reason;
snapshot.lastPoolExhaustionReason = null;
snapshot.lastPoolExhaustionRetryAfterMs = null;
snapshot.lastPoolExhaustionSkipReasons = {};
snapshot.accountSkipReasons = {};
snapshot.policyBlockedIndexes = [];
snapshot.policyBlockedReasons = {};
});
}
/**
* Clear a single account's persisted runtime skip reason after it serves a
* successful request. The overlay (`accountSkipReasons`) is otherwise only
* written wholesale on pool exhaustion and cleared wholesale on a runtime
* reset, so without this a stale reason lingers on disk and the forecast keeps
* reporting a working account as unavailable. This is a no-op when the account
* has no recorded skip reason, so the common success path does not write.
*/
export function recordRuntimeAccountRecovery(index: number): void {
if (!Number.isInteger(index) || index < 0) {
return;
}
const key = String(index);
const current = ensureSnapshotState();
const hasOverlay = current.accountSkipReasons?.[key] !== undefined;
const hasPoolReason =
current.lastPoolExhaustionSkipReasons?.[key] !== undefined;
if (!hasOverlay && !hasPoolReason) {
return;
}
mutateRuntimeObservabilitySnapshot((snapshot) => {
if (snapshot.accountSkipReasons?.[key] !== undefined) {
delete snapshot.accountSkipReasons[key];
}
if (snapshot.lastPoolExhaustionSkipReasons?.[key] !== undefined) {
delete snapshot.lastPoolExhaustionSkipReasons[key];
}
});
}
export async function loadPersistedRuntimeObservabilitySnapshot(): Promise<RuntimeObservabilitySnapshot | null> {
const path = getSnapshotPath();
if (!existsSync(path)) {
return null;
}
try {
const raw = await fs.readFile(path, "utf-8");
const parsed = JSON.parse(raw) as Partial<RuntimeObservabilitySnapshot> | null;
return normalizePersistedSnapshot(parsed);
} catch {
return null;
}
}