import { createSecretRedactionContext } from './secret-redaction'; import { assertRuntimeReceiptOutputWithinLimit, jsonByteLengthUpTo, LEDGER_TERMINAL_RESULT_MAX_BYTES, } from './output-size-limits'; // This is an inline Convex event limit, not the durable scheduler-terminal // envelope. A full terminal return is projected to an out-of-line reference // before it reaches here. export const MAX_LEDGER_RESULT_PAYLOAD_BYTES = LEDGER_TERMINAL_RESULT_MAX_BYTES; export const MAX_LEDGER_LOG_LINES_PER_EVENT = 500; export const MAX_LEDGER_LOG_LINE_LENGTH = 2_000; const ledgerIngressRedactor = createSecretRedactionContext(); export function redactLedgerString(value: unknown): string | null { if (typeof value !== 'string' || !value.trim()) return null; return ledgerIngressRedactor.redactString(value.trim()); } export function sanitizeLedgerPayload(value: unknown): unknown { if (value === undefined) return undefined; const measurement = jsonByteLengthUpTo( value, MAX_LEDGER_RESULT_PAYLOAD_BYTES, ); if (measurement.exceeded) { throw new Error('run event result payload is too large'); } return ledgerIngressRedactor.redact(value); } /** Secret-redact a receipt payload without coupling its 10 MiB cell limit to the ledger. */ export function sanitizeRuntimeReceiptPayload(value: unknown): unknown { // Reject an obviously oversized source before doing redaction work, then // validate the exact value that will be persisted. Redaction normally shrinks // credential-shaped values but can expand short matched strings. assertRuntimeReceiptOutputWithinLimit({ output: value, path: 'runtime receipt output', }); const redacted = ledgerIngressRedactor.redact(value); assertRuntimeReceiptOutputWithinLimit({ output: redacted, path: 'runtime receipt output after secret redaction', }); return redacted; } export function sanitizeLedgerLogLines(value: unknown): string[] { if (!Array.isArray(value)) return []; return value .filter((line): line is string => typeof line === 'string') .slice(0, MAX_LEDGER_LOG_LINES_PER_EVENT) .map((line) => ledgerIngressRedactor .redactString(line.trim()) .slice(0, MAX_LEDGER_LOG_LINE_LENGTH), ) .filter(Boolean); }