All files / lib/logging logger.ts

100% Statements 573/573
94.66% Branches 71/75
100% Functions 18/18
100% Lines 573/573

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 5741x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 221x 221x 221x 221x 221x 221x 221x 221x 221x 221x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 416x 416x 416x 416x 416x 416x 416x 416x 416x 416x 416x 12x 12x 11x 11x 11x 11x 11x 11x 12x 416x 12x 416x 416x 416x 416x 1x 1x 1x 1x 1x 1x 1x 1x 419x 419x 419x 419x 419x 419x 419x 8x 8x 419x 1x 1x 1x 1x 419x 419x 6x 6x 1x 1x 1x 1x 6x 5x 419x 404x 404x 419x 2x 2x 2x 2x 2x 2x 2x 419x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 208x 1248x 1248x 1248x 208x 208x 208x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 208x 208x 1x 1x 1x 1x 1x 1x 1x 1x 208x 208x 208x 208x 208x 208x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 1x 2x 3x 3x 2x 2x 3x 3x 2x 208x 474x 1x 1x 1x 1x 1x 1x 1x 1x 1x 335x 335x 335x 335x 335x 335x 335x 1x 1x 1x 1x 1x 1x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 335x 1x 1x 1x 1x 1x 1x 335x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 335x 1x 1x 1x 1x 1x 1x 1x 1x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 477x 477x 477x 477x 477x 477x 8x 8x 477x 469x 469x 469x 469x 469x 469x 477x 477x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 477x 1x 1x 1x 1x 1x 1x 1x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 39x 39x 1x 1x 1x 25x 50x 50x 50x 50x 2x 2x 50x 50x 50x 4x 4x 50x 25x 1x  
"use strict";
/**
 * Structured logging with Pino, in two standard formats.
 *
 * Two event categories are produced, each in the standard suited to it:
 *
 * - `operational`: lifecycle, builds, transports, performance, failures.
 *   OpenTelemetry log records (see {@link module:otel}), filtered by the
 *   configurable operational log level.
 * - `security_audit`: authentication, authorisation, credential handling and
 *   policy enforcement. OCSF events (see {@link module:ocsf}), always
 *   recorded independent of the operational level, and never sampled.
 *
 * Every record carries a stable event name: `EventName` in operational
 * records, `metadata.event_code` in audit records. Destinations are
 * synchronous sonic-boom destinations over file descriptors, so a failing
 * audit destination is detected immediately; with fail-closed enabled,
 * callers must refuse execution while auditing is unavailable.
 *
 * @module logging/logger
 */
import crypto from "node:crypto";
import fs from "node:fs";
import tty from "node:tty";
 
import pino, { type Logger as PinoLogger } from "pino";
import pinoPretty, { prettyFactory } from "pino-pretty";
import sonicBoomModule, { type SonicBoomOpts } from "sonic-boom";
 
import {
  type AuditEvent,
  type AuditLevel,
  type AuditOutcome,
  type OcsfProduct,
  toOcsfEvent,
} from "./ocsf.js";
import {
  type OtelSource,
  SEVERITY_NUMBER,
  otelRecord,
  otelTimestamp,
  toOtelAttributes,
} from "./otel.js";
import { boundValue } from "../util/redact.js";
import { oagMcpAuthor, oagMcpName, oagMcpVersion } from "../util/version.js";
 
/** An operational log level: one of pino's levels, or `silent`. */
export type LogLevel = pino.LevelWithSilent;
 
/**
 * Operational log levels accepted by the CLI and configuration, taken from
 * pino: its levels from most to least severe, then `silent`.
 */
export const LOG_LEVELS: readonly LogLevel[] = [
  ...Object.entries(pino.levels.values)
    .sort(([, a], [, b]) => b - a)
    .map(([label]) => label as pino.Level),
  "silent",
];
 
/** Log event categories. */
export type LogCategory = "operational" | "security_audit";
 
/** Logging configuration. */
export interface LoggingOptions {
  /** Operational level; audit records ignore it. */
  level?: LogLevel;
  /** Output format for stderr destinations. Files are always JSON. */
  format?: "json" | "pretty";
  /** Operational destination: a path, `stderr` or `fd:N`. Default stderr. */
  file?: string;
  /** Audit destination: a path, `stderr` or `fd:N`. Default stderr. */
  auditFile?: string;
  /** Refuse execute calls while audit records cannot be written. */
  auditFailClosed?: boolean;
  /** Server instance identifier included in every record (default: random). */
  instanceId?: string;
}
 
/** Context fields that may be attached to any record. */
export interface LogContext {
  requestId?: string;
  executionId?: string;
  transport?: string;
  principalId?: string;
  /** Address and port of a Streamable HTTP client. */
  clientAddress?: string;
  clientPort?: number;
  /** MCP session and JSON-RPC request of a tool call. */
  mcpSessionId?: string;
  jsonrpcRequestId?: string | number;
  /** MCP tool being called. */
  toolName?: string;
  [key: string]: unknown;
}
 
/**
 * How many times in a row a write to a full, non-blocking destination
 * (typically a stderr pipe whose reader is falling behind) is retried before
 * it counts as failed. sonic-boom waits 100 ms between attempts, so this is
 * about two seconds. The count resets only when a write makes progress, so
 * once it is exhausted further writes fail fast until the reader catches up.
 */
export const MAX_WRITE_RETRIES = 20;
 
// sonic-boom is CommonJS (`module.exports = SonicBoom`), but its type
// definitions declare an ES default export, so TypeScript places the class one
// level deeper than it is at runtime.
const SonicBoom = sonicBoomModule as unknown as typeof sonicBoomModule.default;
type SonicBoom = InstanceType<typeof SonicBoom>;
 
/** Upper bound on log data held in memory while a destination is failing. */
const MAX_BUFFERED_BYTES = 1024 * 1024;
 
/** Depth to which logged fields are kept; deeper values are cut. */
const MAX_FIELD_DEPTH = 5;
 
/**
 * OpenTelemetry resource and scope of operational records.
 *
 * @param {String} [instanceId] - server instance identifier
 * @return {Object} source
 * @private
 */
function _otelSource(instanceId?: string): OtelSource {
  return {
    resource: {
      "service.name": oagMcpName(),
      "service.version": oagMcpVersion(),
      ...(instanceId ? { "service.instance.id": instanceId } : {}),
    },
    scope: { Name: `${oagMcpName()}.operational`, Version: oagMcpVersion() },
  };
}
 
/**
 * Write one operational record to stderr directly, never through the
 * logger, e.g. to report that a destination failed.
 *
 * @private
 */
function _reportToStderr(
  event: string,
  fields: Record<string, unknown>,
  instanceId?: string,
): void {
  try {
    fs.writeSync(
      2,
      `${JSON.stringify(otelRecord("error", event, { ...fields, outcome: "failure" }, _otelSource(instanceId)))}\n`,
    );
  } catch {
    // stderr itself is unavailable; nothing more can be done.
  }
}
 
/** A synchronous destination together with its failure count. */
interface TrackedDestination {
  fd: number;
  stream: SonicBoom;
  health: { failures: number };
}
 
/**
 * Create a synchronous sonic-boom destination over a file descriptor.
 * Failed writes are counted instead of thrown, and the first few are
 * reported to stderr directly, never through the logger.
 *
 * - Writes are synchronous, so a failing audit destination is detected during
 *   the logging call (needed for fail-closed auditing).
 * - Partial writes are completed, and a record whose write failed stays
 *   buffered (up to `MAX_BUFFERED_BYTES`) and is written, in order, once the
 *   destination recovers.
 * - A full non-blocking pipe (`EAGAIN`) is retried up to `MAX_WRITE_RETRIES`
 *   times, so a reader that never drains stderr cannot stall the server.
 *
 * sonic-boom 5 is used directly rather than through `pino.destination()`,
 * whose bundled sonic-boom 4 retries a persistent write error forever in
 * `flushSync()`; pino calls `flushSync()` after every `fatal` record.
 *
 * @param {Number} fd - file descriptor
 * @param {String} label - destination label used in failure reports
 * @return {Object} { fd, stream, health }
 * @private
 */
function _trackedDestination(fd: number, label: string): TrackedDestination {
  const health = { failures: 0 };
  let reported = 0;
  const stream = new SonicBoom({
    fd,
    sync: true,
    maxLength: MAX_BUFFERED_BYTES,
    // Supported since sonic-boom 5.0.0, not yet in its type definitions.
    maxWriteRetries: MAX_WRITE_RETRIES,
  } as SonicBoomOpts);
  const fail = (errorCode: string) => {
    health.failures++;
    if (reported < 3) {
      reported++;
      _reportToStderr("logging.destination_failed", {
        destination: label,
        errorCode,
      });
    }
  };
  stream.on("error", (err: NodeJS.ErrnoException) =>
    fail(err.code ?? "UNKNOWN"),
  );
  stream.on("drop", () => fail("BUFFER_FULL"));
  return { fd, stream, health };
}
 
/**
 * Open a destination specifier.
 *
 * @param {String} spec - `stderr`, `fd:N` or a file path
 * @return {Object} { fd, label, isStderr }
 * @private
 */
function _openDestination(spec: string | undefined): {
  fd: number;
  label: string;
  isStderr: boolean;
  openError?: string;
} {
  if (!spec || spec === "stderr") {
    return { fd: 2, label: "stderr", isStderr: true };
  }
  if (spec === "stdout") {
    throw new Error(
      "Logging to stdout is not allowed; stdout is reserved for MCP protocol traffic",
    );
  }
  const fdMatch = /^fd:(\d+)$/.exec(spec);
  if (fdMatch) {
    const fd = parseInt(fdMatch[1], 10);
    if (fd === 1) {
      throw new Error(
        "Logging to fd:1 (stdout) is not allowed; stdout is reserved for MCP protocol traffic",
      );
    }
    return { fd, label: spec, isStderr: fd === 2 };
  }
  try {
    const fd = fs.openSync(spec, "a", 0o600);
    return { fd, label: "file", isStderr: false };
  } catch (err) {
    return {
      fd: -1,
      label: "file",
      isStderr: false,
      openError: (err as NodeJS.ErrnoException).code ?? "OPEN_FAILED",
    };
  }
}
 
/**
 * Local wall-clock time of a timestamp, for pretty output.
 *
 * @param {Number} ms - milliseconds since the epoch
 * @return {String} HH:MM:ss.mmm
 * @private
 */
function _localTime(ms: number): string {
  const d = new Date(ms);
  const pad = (n: number, w = 2) => String(n).padStart(w, "0");
  return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}.${pad(d.getMilliseconds(), 3)}`;
}
 
/**
 * Build the pino logger for operational records: OpenTelemetry log records
 * with the data model's field names.
 *
 * @private
 */
function _buildOperationalPino(
  level: string,
  dest: { fd: number; isStderr: boolean },
  tracked: TrackedDestination,
  format: "json" | "pretty",
  source: OtelSource,
): PinoLogger {
  const opts: pino.LoggerOptions = {
    level,
    base: { Resource: source.resource, InstrumentationScope: source.scope },
    messageKey: "Body",
    timestamp: () => `,"Timestamp":"${otelTimestamp()}"`,
    formatters: {
      level: (label) => ({
        SeverityText: label.toUpperCase(),
        SeverityNumber: SEVERITY_NUMBER[label as pino.Level],
      }),
    },
  };
  if (format === "pretty" && dest.isStderr) {
    // Pretty output goes through the same tracked destination, so its write
    // failures are counted too.
    const stream = pinoPretty({
      destination: tracked.stream,
      colorize: tty.isatty(dest.fd),
      levelKey: "SeverityText",
      timestampKey: "Timestamp",
      translateTime: false,
      ignore: "Resource,InstrumentationScope,EventName,SeverityNumber,Body",
      messageFormat: (log) => {
        const attributes = (log.Attributes ?? {}) as Record<string, unknown>;
        const outcome = attributes["oag_mcp.outcome"];
        return `[operational] ${String(log.EventName)}${outcome ? ` ${String(outcome)}` : ""}`;
      },
      customPrettifiers: {
        // `Timestamp` is in nanoseconds, which pino-pretty cannot translate.
        time: (ts) => `[${_localTime(Number(BigInt(String(ts)) / 1000000n))}]`,
      },
    });
    return pino(opts, stream);
  }
  return pino(opts, tracked.stream);
}
 
/**
 * Build the writer for security audit records. Each OCSF event is written
 * exactly as built, one JSON object per line; pino is not used, since it
 * would add its own level and time fields.
 *
 * @private
 */
function _buildAuditWriter(
  dest: { fd: number; isStderr: boolean },
  tracked: TrackedDestination,
  format: "json" | "pretty",
): (event: Record<string, unknown>) => void {
  if (format === "pretty" && dest.isStderr) {
    const pretty = prettyFactory({
      colorize: tty.isatty(dest.fd),
      timestampKey: "time",
      translateTime: false,
      customPrettifiers: {
        time: (ts) => `[${_localTime(Number(ts))}]`,
        // OCSF severity, e.g. "Informational", rather than a pino level.
        level: (severity) => String(severity).toUpperCase(),
      },
      levelKey: "severity",
      hideObject: true,
      messageFormat: (log) => {
        const metadata = (log.metadata ?? {}) as Record<string, unknown>;
        const detail = log.status_detail
          ? ` (${String(log.status_detail)})`
          : "";
        return `[security_audit] ${String(metadata.event_code)} ${String(log.class_name)}: ${String(log.activity_name)} ${String(log.action ?? log.status)}${detail}`;
      },
    });
    return (event) => {
      tracked.stream.write(pretty(event));
    };
  }
  return (event) => {
    tracked.stream.write(`${JSON.stringify(event)}\n`);
  };
}
 
/**
 * oag-mcp logger facade: a pino logger for operational records and an
 * OCSF writer for security audit records.
 */
export class Logs {
  private readonly opLogger: PinoLogger;
  private readonly writeAudit: (event: Record<string, unknown>) => void;
  private readonly opDest: TrackedDestination;
  private readonly auditDest: TrackedDestination;
  private readonly context: LogContext;
  private readonly state: { auditOpenFailed: boolean };
  private readonly product: OcsfProduct;
  readonly failClosed: boolean;
 
  /**
   * @param {Object} [opts] - logging options
   * @param {Object} [shared] - internal: shared state for child loggers
   */
  constructor(
    opts: LoggingOptions = {},
    shared?: {
      opLogger: PinoLogger;
      writeAudit: (event: Record<string, unknown>) => void;
      opDest: TrackedDestination;
      auditDest: TrackedDestination;
      context: LogContext;
      state: { auditOpenFailed: boolean };
      product: OcsfProduct;
      failClosed: boolean;
    },
  ) {
    if (shared) {
      this.opLogger = shared.opLogger;
      this.writeAudit = shared.writeAudit;
      this.opDest = shared.opDest;
      this.auditDest = shared.auditDest;
      this.context = shared.context;
      this.state = shared.state;
      this.product = shared.product;
      this.failClosed = shared.failClosed;
      return;
    }
    const format = opts.format ?? "json";
    const instanceId = opts.instanceId ?? `inst_${crypto.randomUUID()}`;
    this.product = {
      name: oagMcpName(),
      version: oagMcpVersion(),
      vendorName: oagMcpAuthor(),
      instanceId,
    };
    const opDest = _openDestination(opts.file);
    const auditDest = _openDestination(opts.auditFile);
    this.opDest = _trackedDestination(opDest.fd, `operational:${opDest.label}`);
    this.auditDest = _trackedDestination(
      auditDest.fd,
      `security_audit:${auditDest.label}`,
    );
    this.state = { auditOpenFailed: Boolean(auditDest.openError) };
    this.failClosed = Boolean(opts.auditFailClosed);
    this.context = {};
    this.opLogger = _buildOperationalPino(
      opts.level ?? "info",
      opDest,
      this.opDest,
      format,
      _otelSource(instanceId),
    );
    // Audit records are never filtered by the operational level.
    this.writeAudit = _buildAuditWriter(auditDest, this.auditDest, format);
    if (opDest.openError) {
      _reportToStderr(
        "logging.destination_open_failed",
        { destination: "operational", errorCode: opDest.openError },
        instanceId,
      );
    }
    if (auditDest.openError) {
      this.auditDest.health.failures++;
      _reportToStderr(
        "logging.destination_open_failed",
        {
          destination: "security_audit",
          errorCode: auditDest.openError,
          failClosed: this.failClosed,
        },
        instanceId,
      );
    }
  }
 
  /**
   * Create a child logger with additional context fields.
   *
   * @param {Object} ctx - context fields such as requestId
   * @return {Logs} child logger sharing destinations
   */
  child(ctx: LogContext): Logs {
    return new Logs(
      {},
      {
        opLogger: this.opLogger,
        writeAudit: this.writeAudit,
        opDest: this.opDest,
        auditDest: this.auditDest,
        context: { ...this.context, ...ctx },
        state: this.state,
        product: this.product,
        failClosed: this.failClosed,
      },
    );
  }
 
  /** Context fields attached to this logger. */
  get ctx(): LogContext {
    return { ...this.context };
  }
 
  /**
   * Record an operational event as an OpenTelemetry log record.
   *
   * Fields are redacted and bounded, then named by the OpenTelemetry
   * semantic conventions (see {@link toOtelAttributes}).
   *
   * @param {String} level - pino level
   * @param {String} event - stable event name, e.g. "server.ready"
   * @param {Object} [fields] - structured context (never secrets or bodies)
   * @param {String} [message] - optional human message (default: the event)
   */
  op(
    level: pino.Level,
    event: string,
    fields: Record<string, unknown> = {},
    message?: string,
  ): void {
    if (!this.opLogger.isLevelEnabled(level)) {
      return;
    }
    const bounded = boundValue(
      { ...this.context, ...fields },
      { maxDepth: MAX_FIELD_DEPTH },
    ) as Record<string, unknown>;
    this.opLogger[level](
      { EventName: event, Attributes: toOtelAttributes(bounded) },
      message ?? event,
    );
  }
 
  /**
   * Record a security audit event as an OCSF event. Audit records bypass
   * the operational level filter and are never sampled.
   *
   * @param {String} event - stable event name, e.g. "authorization.denied"
   * @param {String} outcome - "success", "failure", "allowed", "denied", ...
   * @param {Object} [fields] - identifiers, policy ids and reason codes only
   * @param {String} [level] - "info" (default), "warn" or "error"
   * @return {Boolean} whether the record was written successfully
   */
  audit(
    event: AuditEvent,
    outcome: AuditOutcome,
    fields: Record<string, unknown> = {},
    level: AuditLevel = "info",
  ): boolean {
    const before = this.auditDest.health.failures;
    const bounded = boundValue(
      { ...this.context, ...fields },
      { maxDepth: MAX_FIELD_DEPTH },
    ) as Record<string, unknown>;
    this.writeAudit({
      ...toOcsfEvent(event, outcome, level, bounded, this.product),
      message: event,
    });
    return (
      this.auditDest.health.failures === before && !this.state.auditOpenFailed
    );
  }
 
  /**
   * Whether the audit destination has failed at least once.
   *
   * @return {Boolean} true when healthy
   */
  get auditHealthy(): boolean {
    return this.auditDest.health.failures === 0 && !this.state.auditOpenFailed;
  }
 
  /**
   * Whether execution must be refused because auditing is required
   * (fail-closed) but unavailable.
   *
   * @return {Boolean} true when execution must be refused
   */
  auditBlocksExecution(): boolean {
    return this.failClosed && !this.auditHealthy;
  }
 
  /** Flush destinations, e.g. on graceful shutdown. */
  flush(): void {
    for (const { fd, stream } of [this.opDest, this.auditDest]) {
      try {
        // Retry anything still buffered after an earlier failed write.
        stream.flushSync();
      } catch {
        // The destination is still failing; it has already been counted.
      }
      try {
        fs.fsyncSync(fd);
      } catch {
        // fsync is unsupported on pipes and ttys.
      }
    }
  }
}