export const DYNAMIC_DEBUG_TEMPLATE = `import { appendFile, mkdir, open, stat } from "node:fs/promises"; import { homedir } from "node:os"; import { basename, dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; const LOG_LIMIT_BYTES = 1024 * 1024; const MARKER_RESERVE_BYTES = 512; const VALUE_LIMIT = 12_000; function cacheRoot(): string { \tif (process.platform === "win32") \t\treturn process.env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"); \tif (process.platform === "darwin") \t\treturn join(homedir(), "Library", "Caches"); \treturn process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"); } function bounded(value: unknown): unknown { \tlet encoded: string; \ttry { \t\tencoded = JSON.stringify(value); \t} catch { \t\tencoded = String(value); \t} \treturn encoded.length <= VALUE_LIMIT \t\t? value \t\t: { truncated: true, preview: encoded.slice(0, VALUE_LIMIT) }; } export function dynamicExtensionRevision(moduleUrl: string): string { \treturn basename(dirname(fileURLToPath(moduleUrl))); } export function dynamicExtensionLogPath( \tsessionId: string, \textensionName: string, ): string { \treturn join( \t\tcacheRoot(), \t\t"pi", \t\t"ultra", \t\t"dynamic-extensions", \t\t"logs", \t\tsessionId, \t\t\`\${extensionName}.jsonl\`, \t); } async function hasTruncationMarker(path: string, size: number): Promise { \tif (size === 0) return false; \tconst handle = await open(path, "r"); \ttry { \t\tconst length = Math.min(size, 1024); \t\tconst bytes = Buffer.alloc(length); \t\tawait handle.read(bytes, 0, length, size - length); \t\treturn bytes.toString("utf8").includes('"event":"log_truncated"'); \t} finally { \t\tawait handle.close(); \t} } export async function logDynamicExtensionEvent(args: { \tname: string; \trevision: string; \tctx: ExtensionContext; \tevent: string; \tdurationMs?: number; \tcontext?: unknown; }): Promise { \tconst sessionId = args.ctx.sessionManager.getSessionId(); \tconst path = dynamicExtensionLogPath(sessionId, args.name); \tawait mkdir(dirname(path), { recursive: true }); \tconst size = await stat(path).then((value) => value.size, () => 0); \tif (size >= LOG_LIMIT_BYTES) return; \tconst record = { \t\ttimestamp: new Date().toISOString(), \t\textension: { name: args.name, revision: args.revision }, \t\tchildSessionId: sessionId, \t\tevent: args.event, \t\t...(args.durationMs === undefined ? {} : { durationMs: args.durationMs }), \t\t...(args.context === undefined ? {} : { context: bounded(args.context) }), \t}; \tconst line = \`\${JSON.stringify(record)}\\n\`; \tif ( \t\tsize < LOG_LIMIT_BYTES - MARKER_RESERVE_BYTES && \t\tsize + Buffer.byteLength(line) <= LOG_LIMIT_BYTES - MARKER_RESERVE_BYTES \t) { \t\tawait appendFile(path, line, "utf8"); \t\treturn; \t} \tif (await hasTruncationMarker(path, size)) return; \tconst marker = { \t\ttimestamp: new Date().toISOString(), \t\textension: { name: args.name, revision: args.revision }, \t\tchildSessionId: sessionId, \t\tevent: "log_truncated", \t\tcontext: { limitBytes: LOG_LIMIT_BYTES }, \t}; \tawait appendFile(path, \`\${JSON.stringify(marker)}\\n\`, "utf8"); } `;