// Server-Sent Events framing for the run event stream (ADR-0011 D5/D7). One harness/workflow event
// becomes one SSE message: `id:` is the event `seq` (the Last-Event-ID resume cursor), `event:` is
// the event `type`, `data:` is the redacted event JSON on a single line, terminated by a blank line.
// The event JSON is produced by `JSON.stringify`, which never emits a raw newline inside a string
// (newlines are escaped as `\n`), so a single `data:` line is always valid — no regex, no manual
// escaping. A synthetic `ready` message is sent after the buffered replay.
export const SSE_HEADERS = {
    "Content-Type": "text/event-stream; charset=utf-8",
    "Cache-Control": "no-store",
    Connection: "keep-alive",
};
// Frames one event as an SSE message. The event is redacted (defense in depth: live events are
// already redacted by the harness emitter, D7) before serialization.
export function frameEvent(event, redactor) {
    const data = JSON.stringify(redactor(event));
    return `id: ${String(event.seq)}\nevent: ${event.type}\ndata: ${data}\n\n`;
}
// The synthetic message sent once the buffered replay completes, signalling the client that it is
// now live. Carries no data payload.
export function readyMessage() {
    return `event: ready\ndata: {}\n\n`;
}
// Writes one framed event to the response stream. Returns Node's backpressure signal so the caller can
// detach a slow client instead of letting the HTTP response buffer grow without bound.
export function writeEvent(res, event, redactor) {
    return res.write(frameEvent(event, redactor));
}
