import { isTruthyEnvValue } from "../infra/env.js"; import { redactToolPayloadText } from "./redact.js"; import type { SubsystemLogger } from "./subsystem.js"; const MAX_NAMES = 24; const MAX_NAME_LENGTH = 80; type CodeModeDiagnosticFields = Record< string, boolean | number | string | readonly string[] | undefined >; export function isCodeModeDiagnosticEnabled(): boolean { return isTruthyEnvValue(process.env.OPENCLAW_DEBUG_CODE_MODE); } export function logCodeModeDiagnostic( log: Pick, boundary: string, fields: CodeModeDiagnosticFields, ): void { if (!isCodeModeDiagnosticEnabled()) { return; } const bounded = Object.fromEntries( Object.entries(fields).map(([key, value]) => { if (Array.isArray(value)) { const names = [...new Set(value.map((name) => name.slice(0, MAX_NAME_LENGTH)))]; names.sort((left, right) => left.localeCompare(right)); return [key, names.slice(0, MAX_NAMES)]; } return [key, typeof value === "string" ? value.slice(0, MAX_NAME_LENGTH) : value]; }), ); log.info( redactToolPayloadText(`code-mode diagnostic ${JSON.stringify({ boundary, ...bounded })}`), ); }