SOURCE / PINNED RELEASE
Made of little things.
Body Journal
- Release
- e8049f8da4e2…
- Author-recorded commit
- 61c04bf0e8aa…
- License
- LICENSE
- Author’s source reference
- nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-6c4554844c7
Archive hash verified: 4369d778da046a8b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
// Bounded application diagnostics, not a NAP domain. Never persisted with a case.
export const OUTPUT_LIMIT = 48_000;
export const REASONING_LIMIT = 24_000;
export const DIAGNOSTIC_CHUNK = 2000;
export const diagnosticStages = ['checking', 'uploading', 'queued', 'requesting', 'receiving', 'validating', 'complete', 'failed', 'stopped'] as const;
export type DiagnosticStage = typeof diagnosticStages[number];
export interface Diagnostic {
stage: DiagnosticStage; elapsedMs: number; output: string; reasoning: string;
events: { stage: DiagnosticStage; elapsedMs: number }[];
requestId?: string; model?: string; responseId?: string; httpStatus?: number; finishReason?: string;
usage?: { input?: number; output?: number; total?: number; reasoning?: number };
issue?: { path: string; message: string }; truncated?: boolean;
formatRecovery?: string;
evidenceNotice?: string;
}
export function redact(value: string, key: string): string {
if (!key) return value;
for (const secret of new Set([key, JSON.stringify(key).slice(1, -1), encodeURIComponent(key)])) value = value.split(secret).join('[REDACTED]');
return value;
}
export function diagnosticPacket(d: Diagnostic, outputOffset = 0, reasoningOffset = 0) {
return { ...d, output: d.output.slice(outputOffset, outputOffset + DIAGNOSTIC_CHUNK), reasoning: d.reasoning.slice(reasoningOffset, reasoningOffset + DIAGNOSTIC_CHUNK), outputOffset, reasoningOffset, outputTotal: d.output.length, reasoningTotal: d.reasoning.length };
}
export function readDiagnostic(value: unknown, previous: Diagnostic | undefined, key: string): { diagnostic: Diagnostic; more: boolean } {
const d = value as Record<string, unknown>;
if (!d || !diagnosticStages.includes(d.stage as DiagnosticStage)) throw new Error('Invalid diagnostic stage.');
const number = (n: unknown, max = 1e9) => { if (!Number.isInteger(n) || Number(n) < 0 || Number(n) > max) throw new Error('Invalid diagnostic number.'); return Number(n); };
const text = (v: unknown, max: number) => { if (typeof v !== 'string' || v.length > max) throw new Error('Invalid diagnostic text.'); return redact(v, key); };
const offset = number(d.outputOffset, OUTPUT_LIMIT), reasoningOffset = number(d.reasoningOffset, REASONING_LIMIT);
const old = previous || { output: '', reasoning: '' };
if (offset !== old.output.length || reasoningOffset !== old.reasoning.length) throw new Error('Diagnostic cursor mismatch.');
const chunk = (v: unknown) => { if(typeof v!=='string'||v.length>DIAGNOSTIC_CHUNK)throw new Error('Invalid diagnostic chunk.');return v; };
const output = old.output + chunk(d.output), reasoning = old.reasoning + chunk(d.reasoning);
const total = number(d.outputTotal, OUTPUT_LIMIT), reasoningTotal = number(d.reasoningTotal, REASONING_LIMIT);
if (output.length > total || reasoning.length > reasoningTotal) throw new Error('Invalid diagnostic length.');
const diagnostic: Diagnostic = { stage: d.stage as DiagnosticStage, elapsedMs: number(d.elapsedMs), output, reasoning, events: [] };
if (!Array.isArray(d.events) || d.events.length > 24) throw new Error('Invalid diagnostic events.');
diagnostic.events = d.events.map(e => { if (!diagnosticStages.includes(e?.stage)) throw new Error('Invalid diagnostic event.'); return { stage: e.stage, elapsedMs: number(e.elapsedMs) }; });
for (const field of ['requestId', 'model', 'responseId', 'finishReason'] as const) if (d[field] !== undefined) diagnostic[field] = text(d[field], 160);
if (d.httpStatus !== undefined) diagnostic.httpStatus = number(d.httpStatus, 599);
if (d.issue) { const issue = d.issue as Record<string, unknown>; diagnostic.issue = { path: text(issue.path, 160), message: text(issue.message, 500) }; }
if (d.usage) { diagnostic.usage = {}; for (const field of ['input', 'output', 'total', 'reasoning'] as const) { const n = (d.usage as Record<string, unknown>)[field]; if (n !== undefined) diagnostic.usage[field] = number(n); } }
diagnostic.truncated = d.truncated === true;
if (d.formatRecovery !== undefined) diagnostic.formatRecovery = text(d.formatRecovery, 300);
if (d.evidenceNotice !== undefined) diagnostic.evidenceNotice = text(d.evidenceNotice, 300);
return { diagnostic, more: output.length < total || reasoning.length < reasoningTotal };
}
export function visibleDiagnostic(d: Diagnostic, key: string): Diagnostic {
const done = ['complete','failed','stopped'].includes(d.stage);
const clean = (value: string) => { const safe=redact(value,key);return done?safe:safe.slice(0,Math.max(0,safe.length-key.length*3)); };
return {...d,output:clean(d.output),reasoning:clean(d.reasoning)};
}
