mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 10:24:02 +00:00
fix(realtime): bound the debug-log sink and stop blocking on its writes
Ported onto the split provider layout: the route now lives in server/providers/openai/debug-log.js and its state belongs to the handler the factory returns rather than to module scope. /api/realtime/debug-log is the one route in its family left unhardened. Both siblings enforce a limiter through openai/rate-limit.js, and this was the only handler still writing with fs.appendFileSync on the request path. - Always-on per-IP limiter (120/min, 400 global). Deliberately not the opt-in openAiRateLimiter(): that one defaults to unlimited so throttling cannot silently change what a user spends, and this sink spends disk and event-loop time rather than provider quota. - Serialized async appends with a 32 MB rotation ceiling and one retained generation. REALTIME_DEBUG_LOG_MAX_BYTES caps a single request body; it never bounded the file those bodies accumulate into, so any local page could grow .gev-logs/realtime-conversations.jsonl for as long as the dev server ran. Serializing also closes a read-then-write race on the size check, where concurrent callers all stat under the ceiling and none rotates. - Generic 400/500 answers separating a malformed record or oversized body from a failed write, instead of echoing error.message — an errno and an absolute path for an fs failure. - readRequestBody marks an oversized body with code 'BODY_TOO_LARGE', the marker its sibling readRequestBodyCapped already sets, so the two cases are distinguishable without matching on text. Coverage extends src/tooling/localServices.test.mjs, which already drives the real handler over a temporary sourceRoot. Both new tests fail on the previous implementation and pass with this one. Refs #23. Does not add authentication: these proxies have no auth mechanism today, so introducing one is a maintainer design decision.
This commit is contained in:
1 parent
96a6b49477
commit
44817e1286
5 files changed
+154
-16
No files matched your search
@@ -1,16 +1,61 @@
|
||||
import { defaultSourceRoot } from '../common/source-root.js';
|
||||
import path from 'node:path';
|
||||
import { readRequestBody } from '../common/request.js';
|
||||
import fs from 'node:fs';
|
||||
import { promises as fsp } from 'node:fs';
|
||||
import { makeRateLimiter, clientKey } from '../common/rate-limit.js';
|
||||
|
||||
/** Cap on one request body. */
|
||||
const REALTIME_DEBUG_LOG_MAX_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Ceiling for the live log before it rotates. One generation is kept
|
||||
* (`…jsonl.1`), so the sink is bounded at twice this however long a session
|
||||
* runs. REALTIME_DEBUG_LOG_MAX_BYTES caps a single request; it never bounded
|
||||
* the file those requests accumulate into.
|
||||
*/
|
||||
const REALTIME_DEBUG_LOG_MAX_FILE_BYTES = 32 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Per-IP write ceiling. Always on, unlike the opt-in limiter the cost-bearing
|
||||
* OpenAI routes share: throttling those by default would change what a user
|
||||
* spends, while this sink spends disk and event-loop time. A voice session
|
||||
* writes far below this, so the cap needs no configuration to stay invisible.
|
||||
*/
|
||||
const REALTIME_DEBUG_LOG_MAX_PER_MIN = 120;
|
||||
|
||||
function createDebugLogHandler({ sourceRoot = defaultSourceRoot } = {}) {
|
||||
const REALTIME_DEBUG_LOG_DIR = path.join(sourceRoot, '.gev-logs');
|
||||
const REALTIME_DEBUG_LOG_FILE = path.join(
|
||||
REALTIME_DEBUG_LOG_DIR,
|
||||
'realtime-conversations.jsonl',
|
||||
);
|
||||
const logDir = path.join(sourceRoot, '.gev-logs');
|
||||
const logFile = path.join(logDir, 'realtime-conversations.jsonl');
|
||||
const allow = makeRateLimiter({
|
||||
windowMs: 60_000,
|
||||
max: REALTIME_DEBUG_LOG_MAX_PER_MIN,
|
||||
globalMax: 400,
|
||||
});
|
||||
|
||||
// Appends are chained rather than fired concurrently because the rotation
|
||||
// check is a read-then-write on file size: run in parallel, every caller
|
||||
// stats a file still under the ceiling, none of them rotates, and the log
|
||||
// grows past it. The records themselves are O_APPEND and never interleave.
|
||||
let queue = Promise.resolve();
|
||||
|
||||
function append(line) {
|
||||
const done = queue.then(async () => {
|
||||
await fsp.mkdir(logDir, { recursive: true });
|
||||
const size = await fsp.stat(logFile).then(
|
||||
(stat) => stat.size,
|
||||
() => 0,
|
||||
);
|
||||
if (size + Buffer.byteLength(line) > REALTIME_DEBUG_LOG_MAX_FILE_BYTES) {
|
||||
await fsp.rename(logFile, `${logFile}.1`);
|
||||
}
|
||||
await fsp.appendFile(logFile, line);
|
||||
});
|
||||
// The caller still sees its own rejection, but one failed append must not
|
||||
// poison the chain for every request queued behind it.
|
||||
queue = done.catch(() => {});
|
||||
return done;
|
||||
}
|
||||
|
||||
return async (req, res) => {
|
||||
if (req.method !== 'POST') {
|
||||
res.statusCode = 405;
|
||||
@@ -19,12 +64,19 @@ function createDebugLogHandler({ sourceRoot = defaultSourceRoot } = {}) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!allow(clientKey(req))) {
|
||||
res.writeHead(429, {
|
||||
'Content-Type': 'application/json',
|
||||
'Retry-After': '5',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'Rate limit exceeded' }));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await readRequestBody(req, REALTIME_DEBUG_LOG_MAX_BYTES);
|
||||
const record = JSON.parse(body || '{}');
|
||||
fs.mkdirSync(REALTIME_DEBUG_LOG_DIR, { recursive: true });
|
||||
fs.appendFileSync(
|
||||
REALTIME_DEBUG_LOG_FILE,
|
||||
await append(
|
||||
`${JSON.stringify({
|
||||
loggedAt: new Date().toISOString(),
|
||||
...record,
|
||||
@@ -33,13 +85,16 @@ function createDebugLogHandler({ sourceRoot = defaultSourceRoot } = {}) {
|
||||
res.statusCode = 204;
|
||||
res.end();
|
||||
} catch (error) {
|
||||
res.statusCode = 400;
|
||||
// A malformed record or an oversized body is the caller's fault; a failed
|
||||
// write is ours, and only the second is worth a log line. Neither answer
|
||||
// carries the error text, which for an fs failure is an errno and an
|
||||
// absolute path.
|
||||
const callerFault =
|
||||
error instanceof SyntaxError || error?.code === 'BODY_TOO_LARGE';
|
||||
if (!callerFault) console.warn('[realtime-debug-log] append failed');
|
||||
res.statusCode = callerFault ? 400 : 500;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
error: error?.message || 'Failed to write Realtime debug log',
|
||||
}),
|
||||
);
|
||||
res.end(JSON.stringify({ error: 'Failed to write Realtime debug log' }));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user