import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { createHash } from "node:crypto"; import { getAgentDir } from "@earendil-works/pi-coding-agent"; export const DEFAULT_DEBOUNCE_MS = 3000; export const DEFAULT_MAX_FILE_BYTES = 1_000_000; export const DEFAULT_INCLUDE: string[] = []; export const DEFAULT_EXCLUDE = [ "**/.git/**", "**/.hg/**", "**/.svn/**", "**/.pi/**", "**/node_modules/**", "**/vendor/**", "**/dist/**", "**/build/**", "**/coverage/**", "**/target/**", "**/.cache/**", "**/.next/**", ]; export const DEFAULT_MARKERS = ["LLM", "PROMPT"]; export const STORE_NAMESPACE = "llmiterate"; export const STATE_FILE = "state.json"; export const APP_STATE_DIR = "pi"; export interface LlmiterateConfig { enabled: boolean; include: string[]; exclude: string[]; debounceMs: number; maxFileBytes: number; markers: string[]; } export interface MarkerPrompt { marker: string; file: string; startLine: number; endLine: number; prompt: string; key: string; promptHash: string; } export interface ProcessedPrompt { file: string; startLine: number; endLine: number; promptHash: string; promptPreview: string; queuedAt: number; } export interface LlmiterateState { version: 1; processed: Record; } const REGEX_CHARS = /[|\\{}()[\]^$+?.]/; // File scans call the parser for every saved/scanned file. Cache marker regexes by // normalized marker list so steady-state scans do not rebuild/sort/escape regexes. const markerRegexCache = new Map(); export function defaultConfig(): LlmiterateConfig { return { enabled: true, include: [...DEFAULT_INCLUDE], exclude: [...DEFAULT_EXCLUDE], debounceMs: DEFAULT_DEBOUNCE_MS, maxFileBytes: DEFAULT_MAX_FILE_BYTES, markers: [...DEFAULT_MARKERS], }; } export function normalizeConfig(raw: unknown): LlmiterateConfig { const fallback = defaultConfig(); if (!isObject(raw)) return fallback; return { enabled: typeof raw.enabled === "boolean" ? raw.enabled : fallback.enabled, include: stringArray(raw.include, fallback.include), exclude: stringArray(raw.exclude, fallback.exclude), debounceMs: positiveInt(raw.debounceMs, fallback.debounceMs), maxFileBytes: positiveInt(raw.maxFileBytes, fallback.maxFileBytes), markers: cleanMarkers(raw.markers, fallback.markers), }; } export function loadConfig(cwd: string): LlmiterateConfig { const global = readSettingsNamespace(path.join(getAgentDir(), "settings.json")); const project = readSettingsNamespace(path.join(cwd, ".pi", "settings.json")); return normalizeConfig({ ...global, ...project }); } function readSettingsNamespace(settingsPath: string): Record { try { const parsed = JSON.parse(fs.readFileSync(settingsPath, "utf-8")); return isObject(parsed) && isObject(parsed.llmiterate) ? parsed.llmiterate : {}; } catch { return {}; } } export function projectStoreDir(root: string): string { const storeDir = path.join(platformStateDir(), STORE_NAMESPACE, hashProjectRoot(root)); migrateLegacyProjectStore(root, storeDir); return storeDir; } export function platformStateDir( platform: NodeJS.Platform = process.platform, env: NodeJS.ProcessEnv = process.env, home = os.homedir(), ): string { const paths = platform === "win32" ? path.win32 : path.posix; if (platform === "win32") { return paths.join(env.LOCALAPPDATA ?? paths.join(home, "AppData", "Local"), APP_STATE_DIR); } if (env.XDG_STATE_HOME) return paths.join(env.XDG_STATE_HOME, APP_STATE_DIR); if (platform === "darwin") { return paths.join(home, "Library", "Application Support", APP_STATE_DIR); } return paths.join(home, ".local", "state", APP_STATE_DIR); } function legacyProjectStoreDir(root: string): string { return path.join(getAgentDir(), STORE_NAMESPACE, hashProjectRoot(root)); } function migrateLegacyProjectStore(root: string, storeDir: string): void { const legacy = legacyProjectStoreDir(root); if (legacy === storeDir || fs.existsSync(storeDir) || !fs.existsSync(legacy)) return; fs.mkdirSync(path.dirname(storeDir), { recursive: true }); fs.cpSync(legacy, storeDir, { recursive: true, filter: (source) => path.basename(source) !== "lock", }); } export function hashProjectRoot(root: string): string { return createHash("sha256").update(root).digest("hex"); } export function loadState(storeDir: string): LlmiterateState { try { const parsed = JSON.parse(fs.readFileSync(path.join(storeDir, STATE_FILE), "utf-8")); if (!isObject(parsed) || parsed.version !== 1 || !isObject(parsed.processed)) { return emptyState(); } return { version: 1, processed: parseProcessedPrompts(parsed.processed) }; } catch { return emptyState(); } } export function saveState(storeDir: string, state: LlmiterateState): void { fs.mkdirSync(storeDir, { recursive: true }); fs.writeFileSync(path.join(storeDir, STATE_FILE), JSON.stringify(state, null, 2) + "\n", "utf-8"); } export function emptyState(): LlmiterateState { return { version: 1, processed: {} }; } function parseProcessedPrompts(raw: Record): Record { const processed: Record = {}; for (const [key, value] of Object.entries(raw)) { if (isProcessedPrompt(value)) processed[key] = value; } return processed; } function isProcessedPrompt(value: unknown): value is ProcessedPrompt { if (!isObject(value)) return false; const keys = Object.keys(value).sort(); if (keys.join("\0") !== "endLine\0file\0promptHash\0promptPreview\0queuedAt\0startLine") return false; return typeof value.file === "string" && typeof value.startLine === "number" && typeof value.endLine === "number" && typeof value.promptHash === "string" && typeof value.promptPreview === "string" && typeof value.queuedAt === "number"; } export function parseMarkerPrompts(file: string, text: string, markers = DEFAULT_MARKERS): MarkerPrompt[] { const markerLine = markerRegex(markers); const lines = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n"); const prompts: MarkerPrompt[] = []; const markdown = isMarkdownPath(file); let markdownFence: "`" | "~" | undefined; for (let i = 0; i < lines.length; i++) { const rawLine = lines[i] ?? ""; const fence = markdownFenceDelimiter(markdown, rawLine); if (fence) { markdownFence = markdownFence === fence ? undefined : markdownFence ?? fence; continue; } if (markdownFence) continue; const first = markerLine.exec(rawLine.trimStart()); if (!first) continue; const marker = first[1] ?? "LLM"; const firstPromptLine = (first[2] ?? "").trim(); if (!firstPromptLine || isCodeAssignmentPrompt(firstPromptLine)) continue; const startLine = i + 1; const body = [firstPromptLine]; let endLine = startLine; for (let j = i + 1; j < lines.length; j++) { const nextRawLine = lines[j] ?? ""; if (markdownFenceDelimiter(markdown, nextRawLine)) break; const next = markerLine.exec(nextRawLine.trimStart()); if (!next || next[1] !== marker) break; const promptLine = (next[2] ?? "").trim(); if (isCodeAssignmentPrompt(promptLine)) break; if (promptLine) body.push(promptLine); endLine = j + 1; i = j; } const prompt = body.join("\n").trim(); if (prompt) prompts.push(makePrompt(file, marker, startLine, endLine, prompt)); } return prompts; } function makePrompt(file: string, marker: string, startLine: number, endLine: number, prompt: string): MarkerPrompt { const promptHash = hashString(prompt); return { marker, file, startLine, endLine, prompt, promptHash, key: `${file}:${startLine}:${promptHash}`, }; } export function buildAgentPrompt(block: MarkerPrompt): string { return [ `llmiterate request from @${block.file}:${block.startLine}`, "", "Prompt marker contents:", "```text", block.prompt, "```", "", "Instructions:", `- Work in/around @${block.file}.`, `- Marker span to replace: ${block.file}:${block.startLine}-${block.endLine}.`, "- Treat the marker line(s) as an inline prompt/TODO, not program code.", "- Replace the whole marker span with the requested implementation or code change.", "- Do not leave the marker line(s) behind unless impossible; explain if impossible.", ].join("\n"); } export type CompiledGlobSegment = "**" | RegExp; export interface CompiledPathFilter { include: RegExp[]; includeDirPatterns: CompiledGlobSegment[][]; exclude: RegExp[]; excludeRootDirs: Set; excludePrefixes: string[]; } export function compilePathFilter(config: Pick): CompiledPathFilter { // Path filtering is the hottest repository-scan path. Split cheap "dir/**" // excludes into root-dir Set lookups / prefix checks before falling back to regex. const includeDirPatterns = config.include.map(compileGlobSegments); const excludeRootDirs = new Set(); const excludePrefixes: string[] = []; const exclude: RegExp[] = []; for (const glob of config.exclude) { const prefix = simpleGlobstarPrefix(glob); if (!prefix) { exclude.push(globToRegExp(glob)); continue; } const rootDir = rootDirPrefix(prefix); if (rootDir) excludeRootDirs.add(rootDir); else excludePrefixes.push(prefix); } return { include: config.include.map(globToRegExp), includeDirPatterns, exclude, excludeRootDirs, excludePrefixes }; } export function shouldIncludePath(relPath: string, config: Pick | CompiledPathFilter): boolean { const filter = isCompiledPathFilter(config) ? config : compilePathFilter(config); return shouldIncludeCompiledPath(relPath, filter); } export function shouldIncludeCompiledPath(relPath: string, filter: CompiledPathFilter): boolean { const rel = toSlash(relPath); if (isExcludedPath(rel, filter)) return false; return filter.include.some((pattern) => pattern.test(rel)); } export function isExcludedPath(relPath: string, filter: CompiledPathFilter): boolean { const rel = toSlash(relPath); return filter.excludeRootDirs.has(firstPathSegment(rel)) || filter.excludePrefixes.some((prefix) => rel.startsWith(prefix)) || filter.exclude.some((pattern) => pattern.test(rel)); } export function shouldWatchDirectory(relPath: string, filter: CompiledPathFilter): boolean { const rel = toSlash(relPath); if (rel && (isExcludedPath(rel, filter) || isExcludedPath(`${rel}/`, filter))) return false; const segments = rel ? rel.split("/") : []; return filter.includeDirPatterns.some((include) => includeCanMatchUnderDir(include, segments)); } export function globToRegExp(pattern: string): RegExp { const glob = toSlash(pattern); let s = "^"; for (let i = 0; i < glob.length;) { const c = glob[i]; if (c === "*") { if (glob[i + 1] === "*") { i += 2; if (glob[i] === "/") { i++; s += "(?:.*/)?"; } else { s += ".*"; } continue; } s += "[^/]*"; i++; continue; } if (c === "?") { s += "[^/]"; i++; continue; } if (c === "{") { const end = glob.indexOf("}", i + 1); if (end !== -1) { const parts = glob.slice(i + 1, end).split(",").map((p) => p.trim()).filter(Boolean).map(escapeRegExp); if (parts.length) { s += `(?:${parts.join("|")})`; i = end + 1; continue; } } } s += REGEX_CHARS.test(c) ? `\\${c}` : c; i++; } return new RegExp(`${s}$`); } export function toSlash(s: string): string { // Hot path during repository scans. Avoid split/join allocation when already normalized. return s.includes("\\") ? s.replace(/\\/g, "/") : s; } function includeCanMatchUnderDir(pattern: CompiledGlobSegment[], dir: string[]): boolean { const memo = new Map(); const visit = (patternIndex: number, dirIndex: number): boolean => { const key = `${patternIndex}:${dirIndex}`; const cached = memo.get(key); if (cached !== undefined) return cached; let result: boolean; if (dirIndex >= dir.length) { result = patternIndex < pattern.length; } else if (patternIndex >= pattern.length) { result = false; } else { const segment = pattern[patternIndex]!; result = segment === "**" ? visit(patternIndex + 1, dirIndex) || visit(patternIndex, dirIndex + 1) : segment.test(dir[dirIndex]!) && visit(patternIndex + 1, dirIndex + 1); } memo.set(key, result); return result; }; return visit(0, 0); } function compileGlobSegments(pattern: string): CompiledGlobSegment[] { return toSlash(pattern).split("/").filter(Boolean).map((segment) => segment === "**" ? "**" : globToRegExp(segment)); } function hashString(input: string): string { let hash = 0x811c9dc5; for (let i = 0; i < input.length; i++) { hash ^= input.charCodeAt(i); hash = Math.imul(hash, 0x01000193); } return (hash >>> 0).toString(16).padStart(8, "0"); } export function previewText(text: string, maxChars: number): string { const oneLine = text.replace(/\s+/g, " ").trim(); return oneLine.length > maxChars ? `${oneLine.slice(0, Math.max(0, maxChars - 1))}…` : oneLine; } function markerRegex(markers: string[]): RegExp { const cleaned = cleanMarkers(markers, DEFAULT_MARKERS).sort((a, b) => b.length - a.length); const cacheKey = cleaned.join("\0"); const cached = markerRegexCache.get(cacheKey); if (cached) return cached; const markerAlternatives = cleaned.map(escapeRegExp).join("|"); const compiled = new RegExp(`^(${markerAlternatives})(?:\\s+(.*))?$`); markerRegexCache.set(cacheKey, compiled); return compiled; } function markdownFenceDelimiter(markdown: boolean, line: string): "`" | "~" | undefined { if (!markdown) return undefined; const trimmed = line.trimStart(); if (/^```/.test(trimmed)) return "`"; if (/^~~~/.test(trimmed)) return "~"; return undefined; } function isMarkdownPath(file: string): boolean { return /(?:^|\.)(?:md|mdx|markdown)$/i.test(file); } function isCodeAssignmentPrompt(prompt: string): boolean { return prompt.startsWith("="); } function simpleGlobstarPrefix(glob: string): string | undefined { const normalized = toSlash(glob); if (!normalized.endsWith("/**")) return undefined; const prefix = normalized.slice(0, -2); return /[*?{[]/.test(prefix) ? undefined : prefix; } function rootDirPrefix(prefix: string): string | undefined { const dir = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix; return dir && !dir.includes("/") ? dir : undefined; } function firstPathSegment(path: string): string { const slash = path.indexOf("/"); return slash === -1 ? path : path.slice(0, slash); } function escapeRegExp(s: string): string { return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } function isObject(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } function stringArray(value: unknown, fallback: string[]): string[] { return Array.isArray(value) && value.every((v) => typeof v === "string" && v.trim()) ? value.map((v) => v.trim()) : [...fallback]; } function positiveInt(value: unknown, fallback: number): number { return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : fallback; } function cleanMarkers(value: unknown, fallback: string[]): string[] { const raw = stringArray(value, fallback); const cleaned = raw.map((m) => m.trim()).filter((m) => m.length > 0 && !/[\r\n]/.test(m)); return cleaned.length ? cleaned : [...fallback]; } function isCompiledPathFilter(value: Pick | CompiledPathFilter): value is CompiledPathFilter { return "includeDirPatterns" in value; }