@dreb/coding-agent 2.30.1 → 2.31.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nested-context.js","sourceRoot":"","sources":["../../src/core/nested-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC3E,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,uBAAuB,EAA2B,MAAM,sBAAsB,CAAC;AACjH,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAE3E;;;;;;;;;GASG;AAEH,yEAAyE;AACzE,MAAM,cAAc,GAAG,EAAE,CAAC;AAE1B,8EAA8E;AAC9E,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;AAgB5E;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,OAAe,EAAiB;IAC9D,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAE7C,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAC7C,IAAI,CAAC,SAAS;QAAE,OAAO,IAAI,CAAC;IAE5B,IAAI,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IAC9C,OAAO,IAAI,EAAE,CAAC;QACb,gFAAgF;QACh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IAC5F,sEAAsE;IACtE,MAAM,aAAa,GAAG,WAAW;QAChC,CAAC,CAAC,IAAI,GAAG,CAAC,yBAAyB,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAC9E,CAAC,CAAC,IAAI,CAAC;IACR,MAAM,QAAQ,GAAsB,CAAC,IAAI,EAAE,EAAE,CAAC;QAC7C,MAAM,QAAQ,GAAG,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzC,uFAAuF;QACvF,qFAAqF;QACrF,wFAAwF;QACxF,sFAAsF;QACtF,oDAAoD;QACpD,IAAI,QAAQ,KAAK,gBAAgB;YAAE,OAAO,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC3E,IAAI,aAAa,EAAE,GAAG,CAAC,QAAQ,CAAC;YAAE,OAAO,eAAe,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QACzE,OAAO,KAAK,CAAC;IAAA,CACb,CAAC;IAEF,MAAM,SAAS,GAAG,oBAAoB,CAAC,SAAS,EAAE,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACrF,IAAI,CAAC,SAAS,CAAC,YAAY,EAAE,CAAC;QAC7B,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACnC,CAAC;IACD,IAAI,SAAS,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAC9C,OAAO,wBAAwB,CAAC,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC;AAAA,CAC5D","sourcesContent":["import { existsSync, readFileSync, realpathSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, isAbsolute, join, resolve, sep } from \"node:path\";\nimport { CONTEXT_FILE_CANDIDATES, loadContextFilesFromDir, type ResourceDiagnostic } from \"./resource-loader.js\";\nimport { renderTerminalOutput } from \"./tools/terminal-render.js\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES } from \"./tools/truncate.js\";\n\n/**\n * Auto-load of nested AGENTS.md/CLAUDE.md context files.\n *\n * Project context files are only loaded at session start by walking *upward* from\n * `cwd`. When the agent (or a subagent) operates in a subdirectory — or in an entirely\n * different repo/project — that directory's context files are never loaded. This module\n * detects the directory a tool is about to operate in, walks up to a sensible ceiling\n * collecting context files, and returns a formatted block for injection into the tool\n * result (which is cache-safe — it does not rebuild the system prompt).\n */\n\n/** A safety bound on how many directories the upward walk will visit. */\nconst MAX_WALK_DEPTH = 64;\n\n/** Tools whose `path` argument identifies the directory being operated on. */\nconst PATH_TOOLS = new Set([\"read\", \"edit\", \"write\", \"grep\", \"find\", \"ls\"]);\n\nexport interface LoadedContextFile {\n\t/** Absolute path of the loaded context file. */\n\tpath: string;\n\t/** File content (HTML comments already stripped by the loader). */\n\tcontent: string;\n}\n\nexport interface NestedContextCollection {\n\t/** Context files newly loaded during this collection pass. */\n\tfiles: LoadedContextFile[];\n\t/** Whether any existing context file failed to read and should be retried later. */\n\thadReadError: boolean;\n}\n\n/**\n * Extract the target of a leading `cd <dir>` from a bash command.\n *\n * Covers the overwhelming majority of directory-changing bash commands (analysis of\n * real session logs: ~75% of bash calls start with `cd`, ~97% of those with an absolute\n * path). Returns the raw, unresolved path string (with a leading `~` preserved) or\n * `null` when the command does not begin with a simple `cd`.\n */\nexport function parseLeadingCd(command: string): string | null {\n\tif (typeof command !== \"string\") return null;\n\n\tconst leadingCd = command.match(/^\\s*cd\\s+/);\n\tif (!leadingCd) return null;\n\n\tlet rest = command.slice(leadingCd[0].length);\n\twhile (true) {\n\t\t// Match either a quoted path or an unquoted token that stops at the first shell\n\t\t// separator (&&, ;, |, newline) or whitespace.\n\t\tconst match = rest.match(/^\\s*(?:\"([^\"]+)\"|'([^']+)'|([^\\s&;|<>]+))/);\n\t\tif (!match) return null;\n\n\t\tconst target = (match[1] ?? match[2] ?? match[3] ?? \"\").trim();\n\t\tif (!target) return null;\n\n\t\t// `cd -` means \"previous directory\" and cannot be resolved cheaply.\n\t\tif (target === \"-\") return null;\n\n\t\t// Skip leading options (`cd -P /x`, `cd -L /x`). After `--`, the next token is\n\t\t// the path even if it begins with `-`.\n\t\tif (target === \"--\") {\n\t\t\trest = rest.slice(match[0].length);\n\t\t\tconst pathMatch = rest.match(/^\\s*(?:\"([^\"]+)\"|'([^']+)'|([^\\s&;|<>]+))/);\n\t\t\tif (!pathMatch) return null;\n\t\t\tconst pathTarget = (pathMatch[1] ?? pathMatch[2] ?? pathMatch[3] ?? \"\").trim();\n\t\t\tif (!pathTarget || pathTarget.startsWith(\"$\")) return null;\n\t\t\treturn pathTarget;\n\t\t}\n\t\tif (target.startsWith(\"-\")) {\n\t\t\trest = rest.slice(match[0].length);\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Skip variable-based targets we cannot resolve cheaply.\n\t\tif (target.startsWith(\"$\")) return null;\n\t\treturn target;\n\t}\n}\n\n/**\n * Expand a leading `~` to the home directory and resolve a raw path string to an absolute\n * path against `baseDir`. Shared by every place that turns a user-supplied path token into\n * an absolute path (`resolveTargetDir`, `resolveSelfReadFile`, bash argument resolution).\n */\nfunction expandToAbsolute(rawPath: string, baseDir: string): string {\n\tif (rawPath === \"~\") {\n\t\trawPath = homedir();\n\t} else if (rawPath.startsWith(`~${sep}`) || rawPath.startsWith(\"~/\")) {\n\t\trawPath = join(homedir(), rawPath.slice(2));\n\t}\n\treturn isAbsolute(rawPath) ? rawPath : resolve(baseDir, rawPath);\n}\n\n/**\n * Resolve the absolute directory a tool call is about to operate in, or `null` when the\n * tool/argument shape does not identify a directory we should react to.\n */\nexport function resolveTargetDir(\n\ttoolName: string,\n\targs: Record<string, unknown> | undefined,\n\tcwd: string,\n): string | null {\n\tif (!args) return null;\n\n\tlet rawPath: string | null = null;\n\n\tif (toolName === \"bash\") {\n\t\trawPath = parseLeadingCd(typeof args.command === \"string\" ? args.command : \"\");\n\t} else if (PATH_TOOLS.has(toolName)) {\n\t\tconst p = args.path;\n\t\tif (typeof p === \"string\" && p.trim() !== \"\") {\n\t\t\trawPath = p;\n\t\t}\n\t}\n\n\tif (!rawPath) return null;\n\n\tconst absolute = expandToAbsolute(rawPath, cwd);\n\n\t// For path-bearing tools the argument is usually a file; for bash `cd` it is a\n\t// directory. Resolve to a directory: existing dirs are used as-is, everything else\n\t// (existing files, not-yet-created files) maps to its parent directory.\n\ttry {\n\t\tif (existsSync(absolute) && statSync(absolute).isDirectory()) {\n\t\t\treturn absolute;\n\t\t}\n\t} catch {\n\t\t// Fall through to dirname on permission/stat errors.\n\t}\n\treturn dirname(absolute);\n}\n\n/** Safe realpath that falls back to the input on error. */\nfunction safeRealpath(p: string): string {\n\ttry {\n\t\treturn realpathSync(p);\n\t} catch {\n\t\treturn p;\n\t}\n}\n\nfunction isWithin(parent: string, child: string): boolean {\n\tconst p = safeRealpath(parent);\n\tconst c = safeRealpath(child);\n\treturn c === p || c.startsWith(p.endsWith(sep) ? p : p + sep);\n}\n\n/**\n * Build the ordered list of directories to inspect, from the target directory up to the\n * appropriate ceiling. Ordered outermost-first so the most specific (closest to the\n * target) context appears last, matching session-start precedence.\n *\n * Ceiling priority:\n * 1. `cwd` — when the target is within the cwd subtree (ancestors already loaded at start).\n * 2. The outermost git repo root in the chain (a directory containing `.git`).\n * 3. The outermost directory containing a CLAUDE.md/AGENTS.md.\n * 4. Hard stop at filesystem root, the depth bound, or a permission/stat failure.\n */\nfunction resolveWalkDirs(targetDir: string, cwd: string): string[] {\n\tconst root = resolve(\"/\");\n\n\t// Case 1: target within cwd subtree — never walk above cwd.\n\tif (isWithin(cwd, targetDir)) {\n\t\tconst dirs: string[] = [];\n\t\tlet current = targetDir;\n\t\tconst stop = safeRealpath(cwd);\n\t\tfor (let i = 0; i < MAX_WALK_DEPTH; i++) {\n\t\t\tdirs.push(current);\n\t\t\tif (safeRealpath(current) === stop) break;\n\t\t\tconst parent = resolve(current, \"..\");\n\t\t\tif (parent === current) break;\n\t\t\tcurrent = parent;\n\t\t}\n\t\treturn dirs.reverse();\n\t}\n\n\t// Case 2/3/4: target outside cwd — walk to the hard ceiling, recording git roots and\n\t// directories that hold context files, then bound to the outermost relevant ceiling.\n\tconst chain: string[] = [];\n\tlet highestGitRootIdx = -1;\n\tlet highestContextIdx = -1;\n\tlet current = targetDir;\n\tfor (let i = 0; i < MAX_WALK_DEPTH; i++) {\n\t\t// A permission/stat failure on the directory itself stops the walk.\n\t\ttry {\n\t\t\tstatSync(current);\n\t\t} catch {\n\t\t\tbreak;\n\t\t}\n\t\tchain.push(current);\n\t\tconst idx = chain.length - 1;\n\t\ttry {\n\t\t\tif (existsSync(join(current, \".git\"))) highestGitRootIdx = idx;\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t\tif (dirHasContextFile(current)) highestContextIdx = idx;\n\n\t\tif (current === root) break;\n\t\tconst parent = resolve(current, \"..\");\n\t\tif (parent === current) break;\n\t\tcurrent = parent;\n\t}\n\n\tlet ceilingIdx: number;\n\tif (highestGitRootIdx >= 0) {\n\t\tceilingIdx = highestGitRootIdx;\n\t} else if (highestContextIdx >= 0) {\n\t\tceilingIdx = highestContextIdx;\n\t} else {\n\t\tceilingIdx = chain.length - 1;\n\t}\n\n\treturn chain.slice(0, ceilingIdx + 1).reverse();\n}\n\n/** Cheap check: does this directory hold any candidate context file? */\nfunction dirHasContextFile(dir: string): boolean {\n\tfor (const c of CONTEXT_FILE_CANDIDATES) {\n\t\ttry {\n\t\t\tif (existsSync(join(dir, c))) return true;\n\t\t} catch {\n\t\t\t// ignore\n\t\t}\n\t}\n\treturn false;\n}\n\n/**\n * Predicate deciding whether a collected context file should be *suppressed* from the\n * injected block because the triggering tool call already delivers its content (e.g. a\n * `read` of the file itself, or a `bash` command that prints it). Suppressed files are\n * still marked as loaded so they are never injected later — they are simply not\n * duplicated into the result that already contains them.\n */\nexport type SuppressPredicate = (file: LoadedContextFile) => boolean;\n\n/**\n * Collect nested context files for `targetDir`, walking up to the ceiling described in\n * {@link resolveWalkDirs}. Files whose realpath is already in `alreadyLoaded` are skipped\n * (and not re-reported). Newly collected realpaths are added to `alreadyLoaded` so the\n * caller's per-session set stays authoritative and each file loads at most once. Also\n * reports whether an existing context file failed to read so callers can retry later\n * instead of negatively caching a transient failure.\n *\n * When `suppress` matches a newly-seen file, that file is marked loaded but excluded from\n * the returned `files` — the triggering tool result already contains it, so re-injecting\n * would duplicate the content and waste tokens.\n */\nexport function collectNestedContext(\n\ttargetDir: string,\n\tcwd: string,\n\talreadyLoaded: Set<string>,\n\tsuppress?: SuppressPredicate,\n): NestedContextCollection {\n\tconst dirs = resolveWalkDirs(targetDir, cwd);\n\tconst collected: LoadedContextFile[] = [];\n\tlet hadReadError = false;\n\tfor (const dir of dirs) {\n\t\tconst diagnostics: ResourceDiagnostic[] = [];\n\t\tconst files = loadContextFilesFromDir(dir, diagnostics);\n\t\tfor (const diagnostic of diagnostics) {\n\t\t\tif (diagnostic.type !== \"warning\") continue;\n\t\t\thadReadError = true;\n\t\t\tconsole.warn(\n\t\t\t\t`[nested-context] Nested context file existed but could not be read: ${diagnostic.path ?? dir} — ${diagnostic.message}`,\n\t\t\t);\n\t\t}\n\t\tfor (const file of files) {\n\t\t\tconst real = safeRealpath(file.path);\n\t\t\tif (alreadyLoaded.has(real)) continue;\n\t\t\talreadyLoaded.add(real);\n\t\t\t// Mark loaded but do not inject: the triggering tool already delivers this file.\n\t\t\tif (suppress?.(file)) continue;\n\t\t\tcollected.push(file);\n\t\t}\n\t}\n\treturn { files: collected, hadReadError };\n}\n\n/**\n * Format collected context files into a single text block for injection into a tool\n * result. Leads with *why* the load happened and headers each file with its source path.\n * There is intentionally no size cap — oversized context files are the project's concern.\n */\nexport function formatNestedContextBlock(targetDir: string, files: LoadedContextFile[]): string {\n\tconst header =\n\t\t`[dreb] Auto-loaded project context\\n\\n` +\n\t\t`A tool just operated in \\`${targetDir}\\`, whose project context had not been loaded yet. ` +\n\t\t`The file(s) below were loaded automatically to prevent missing important project context ` +\n\t\t`when working across multiple repos / projects / folders. ` +\n\t\t`(Disable with the \\`context.autoLoadNested\\` setting.)`;\n\n\tconst sections = files.map(\n\t\t(f) =>\n\t\t\t`===== BEGIN project context: ${f.path} =====\\n${f.content.trim()}\\n===== END project context: ${f.path} =====`,\n\t);\n\n\treturn `${header}\\n\\n${sections.join(\"\\n\\n\")}`;\n}\n\n/**\n * Resolve the absolute file path a `read` tool call delivers, or `null` when the tool is\n * not `read` or has no usable `path`. Only `read` returns the *full* file content, so it\n * is the only path-tool whose result fully duplicates an injected context file. (`grep`\n * returns matched lines, `ls`/`edit`/`write` do not echo the whole file — those still\n * benefit from injection.)\n */\nexport function resolveSelfReadFile(\n\ttoolName: string,\n\targs: Record<string, unknown> | undefined,\n\tcwd: string,\n): string | null {\n\tif (!args || toolName !== \"read\") return null;\n\t// A sliced read (`offset`/`limit`) delivers only a fragment of the file — the same\n\t// hazard for which bash partial viewers (`head`/`tail`) are excluded. Treating it as a\n\t// full delivery would suppress (and permanently mark loaded) a file the result only\n\t// partially contains, silently dropping the rest. Fall back to the safe double-load.\n\tif (args.offset !== undefined || args.limit !== undefined) return null;\n\tconst p = args.path;\n\tif (typeof p !== \"string\" || p.trim() === \"\") return null;\n\treturn expandToAbsolute(p, cwd);\n}\n\n/**\n * Bash commands that dump a file's *full* contents to stdout. Deliberately narrow: only\n * commands that emit the whole file qualify. Partial viewers (`head`/`tail`) and\n * interactive pagers (`less`/`more`) are excluded — they may show only a fragment, so\n * treating them as \"delivered\" could silently drop the rest of a context file. The safe\n * failure mode is a harmless double-load (we still inject), never a silent context drop.\n *\n * `bat` is included but is *not* unconditionally a full dump: its `-r`/`--line-range` flag\n * emits only a range (same hazard as `head`/`tail`). Segments carrying that flag are\n * disqualified in {@link resolveBashDeliveredFiles}.\n */\nconst FULL_DUMP_COMMANDS = new Set([\"cat\", \"bat\"]);\n\n/** `bat` flags that limit output to a partial range — disqualify the segment if present. */\nconst BAT_RANGE_FLAGS = [\"-r\", \"--line-range\"];\n\n/**\n * Whether a `bat` token requests a partial line range. Matches the space-separated form\n * (`-r`, `--line-range`), the `=`-attached long form (`--line-range=10:20`), and the\n * attached short form (`-r10:20`) — clap accepts an attached value on a short flag, so a\n * bare `startsWith` check on each known range flag covers every spelling. A partial range\n * is the same hazard as `head`/`tail`: only a fragment is emitted, so the segment must not\n * be treated as a full dump.\n */\nfunction isBatRangeFlag(token: string): boolean {\n\treturn BAT_RANGE_FLAGS.some((flag) => token.startsWith(flag));\n}\n\n/** Strip a single layer of matching surrounding quotes from a shell token. */\nfunction unquoteToken(token: string): string {\n\tif (token.length >= 2) {\n\t\tconst first = token[0];\n\t\tconst last = token[token.length - 1];\n\t\tif ((first === '\"' || first === \"'\") && first === last) {\n\t\t\treturn token.slice(1, -1);\n\t\t}\n\t}\n\treturn token;\n}\n\n/**\n * Resolve the absolute paths of files a bash command fully delivers to stdout via a\n * full-dump command (`cat`/`bat`). Path arguments are resolved against `workingDir` (the\n * command's effective cwd — e.g. a leading `cd` target). Conservative on purpose:\n *\n * - Segments are split on `&&`, `||`, `;`. Segments that are *only* a `cd` produce no\n * stdout and are ignored, but there must be **exactly one** remaining output-producing\n * segment. The bash tool truncates its *combined* command output from the **tail**\n * (keeping the last {@link DEFAULT_MAX_LINES} lines / {@link DEFAULT_MAX_BYTES} bytes and\n * dropping the head), so any *additional* output-producing segment could evict the dumped\n * file from the visible window while {@link deliveredInFull} — which measures the file\n * alone — still reports a full delivery. Bail to the safe double-load in that case.\n * - That sole segment must not contain a pipe (`|`), output redirection (`>`), or input\n * redirection / here-doc / here-string (`<`, `<<`, `<<<`) — its output is filtered /\n * redirected, or its operands are stdin body words rather than dumped files.\n * - Its first token must be `cat`/`bat`; flags (`-…`) are ignored.\n * - A `bat` segment carrying a partial-range flag (`-r`/`--line-range`, any spelling) is\n * skipped — it emits only a fragment, like `head`/`tail`.\n * - It must have **exactly one** file operand. A multi-file dump (`cat A.md B.md`)\n * concatenates several files; under tail truncation an earlier operand can be evicted\n * while still appearing fully sized on disk, so it is not a provable full delivery.\n * - If the command chains more than one `cd`, the effective cwd is ambiguous (we only\n * resolved the *first* `cd`), so operands cannot be resolved reliably — return nothing.\n *\n * Anything we cannot confidently classify as a full delivery is omitted, so the worst case\n * is a double-load rather than a silently dropped context file.\n */\nexport function resolveBashDeliveredFiles(command: string, workingDir: string): string[] {\n\tif (typeof command !== \"string\" || command.trim() === \"\") return [];\n\tconst segments = command.split(/&&|\\|\\||;/);\n\tconst isCdSegment = (s: string) => /^\\s*cd(\\s|$)/.test(s);\n\n\t// More than one `cd` means the effective cwd differs from the first `cd` target we\n\t// resolved as `workingDir`; resolving operands against it would suppress the wrong\n\t// (same-named) file. Bail to the safe double-load.\n\tif (segments.filter(isCdSegment).length > 1) return [];\n\n\t// Segments that are only a `cd` emit no stdout. Everything else produces output, and\n\t// because the bash tool tail-truncates the *combined* output, a context file is only\n\t// provably delivered in full when it is the command's *sole* output-producing segment.\n\tconst outputSegments = segments.filter((s) => s.trim() !== \"\" && !isCdSegment(s));\n\tif (outputSegments.length !== 1) return [];\n\n\tconst segment = outputSegments[0];\n\t// Output piped/redirected, or operands fed via input redirection / here-doc, are not raw\n\t// file dumps to stdout.\n\tif (segment.includes(\"|\") || segment.includes(\">\") || segment.includes(\"<\")) return [];\n\tconst tokens = segment.trim().split(/\\s+/).filter(Boolean);\n\tif (tokens.length === 0) return [];\n\t// Match the command verb case-sensitively: shell PATH lookup is case-sensitive on\n\t// Linux, so `CAT`/`Bat` are command-not-found and emit nothing to stdout. Lowercasing\n\t// would let them match the allowlist and falsely suppress a file they never printed —\n\t// a silent context drop. Exact matching keeps the failure mode a harmless double-load.\n\tconst cmd = tokens[0];\n\tif (!FULL_DUMP_COMMANDS.has(cmd)) return [];\n\t// `bat -r 10:20` / `bat -r10:20` / `bat --line-range=10:20` shows only a range — not a full dump.\n\tif (cmd === \"bat\" && tokens.slice(1).some(isBatRangeFlag)) return [];\n\n\tconst operands: string[] = [];\n\tfor (const token of tokens.slice(1)) {\n\t\tif (token.startsWith(\"-\")) continue; // flag, not a file argument\n\t\tconst arg = unquoteToken(token);\n\t\tif (arg === \"\") continue;\n\t\toperands.push(arg);\n\t}\n\t// A single operand is the only provable full delivery: multi-file dumps concatenate,\n\t// and tail truncation can evict an earlier file while it still looks fully sized.\n\tif (operands.length !== 1) return [];\n\treturn [expandToAbsolute(operands[0], workingDir)];\n}\n\nexport interface NestedContextState {\n\t/** Whether auto-loading is enabled (the `context.autoLoadNested` setting). */\n\tenabled: boolean;\n\t/** The session's working directory. */\n\tcwd: string;\n\t/** Realpaths of context files already loaded this session (seeded at session start). Mutated. */\n\tloaded: Set<string>;\n\t/** Realpaths of directories already scanned (negative cache). Mutated. */\n\tscannedDirs: Set<string>;\n}\n\n/**\n * Whether a tool that delivers `realPath` actually delivers its *full* content. Both `read`\n * and `bash` truncate their output at {@link DEFAULT_MAX_LINES} lines / {@link DEFAULT_MAX_BYTES}\n * bytes, while {@link formatNestedContextBlock} is uncapped. If the file exceeds either limit\n * it is delivered truncated, so suppressing (and permanently marking loaded) would silently\n * drop the remainder. Any stat/read failure also returns `false` — the safe double-load.\n *\n * `rendered` selects which delivery the measure must mirror:\n * - `read` delivers the file's raw content unchanged (`truncateHead` with no transform), so\n * the raw byte/line count is exact.\n * - `bash` delivers `truncateTail(renderTerminalOutput(...))`, and terminal rendering expands\n * tabs to 8-column stops and resolves cursor/ANSI sequences — the rendered output can be\n * *larger* than the file on disk. A tab-dense file just under the budget on disk can render\n * past it and be tail-truncated (its head dropped) while the raw measure still reports a\n * full delivery. Measuring the rendered output keeps the failure mode a harmless double-load\n * rather than a silent context drop.\n */\nfunction deliveredInFull(realPath: string, rendered: boolean): boolean {\n\ttry {\n\t\t// Cheap early-out: the raw on-disk size is a lower bound on the delivered size\n\t\t// (terminal rendering only ever grows the byte count), so a file already over the\n\t\t// byte budget on disk is certainly delivered truncated.\n\t\tif (statSync(realPath).size > DEFAULT_MAX_BYTES) return false;\n\t\tconst raw = readFileSync(realPath, \"utf8\");\n\t\tconst delivered = rendered ? renderTerminalOutput(raw) : raw;\n\t\tif (Buffer.byteLength(delivered, \"utf-8\") > DEFAULT_MAX_BYTES) return false;\n\t\treturn delivered.split(\"\\n\").length <= DEFAULT_MAX_LINES;\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Orchestrate a single nested-context decision for a tool call: gate on the setting,\n * resolve the target directory, skip directories already scanned (negative cache),\n * collect not-yet-loaded context files, and format them. Returns the injection block or\n * `null` when nothing should be injected. Mutates `state.scannedDirs` and `state.loaded`.\n */\nexport function computeNestedContextBlock(\n\ttoolName: string,\n\targs: Record<string, unknown> | undefined,\n\tstate: NestedContextState,\n): string | null {\n\tif (!state.enabled) return null;\n\n\tconst targetDir = resolveTargetDir(toolName, args, state.cwd);\n\tif (!targetDir) return null;\n\n\tconst realTarget = safeRealpath(targetDir);\n\tif (state.scannedDirs.has(realTarget)) return null;\n\n\t// A context file the triggering tool already delivers should be marked loaded but not\n\t// re-injected (the result already contains it). Two cases: a `read` of the file itself,\n\t// or a `bash` command that dumps its full contents (`cat`/`bat`). Both are matched by\n\t// full resolved realpath — never by basename — so printing one file never suppresses a\n\t// same-named sibling/ancestor or a file in a different directory.\n\tconst selfReadFile = resolveSelfReadFile(toolName, args, state.cwd);\n\tconst realSelfReadFile = selfReadFile ? safeRealpath(selfReadFile) : null;\n\tconst bashCommand = toolName === \"bash\" && typeof args?.command === \"string\" ? args.command : null;\n\t// Bash file arguments resolve against the command's effective cwd, which `resolveTargetDir`\n\t// has already computed as `targetDir` (the leading `cd` destination).\n\tconst bashDelivered = bashCommand\n\t\t? new Set(resolveBashDeliveredFiles(bashCommand, targetDir).map(safeRealpath))\n\t\t: null;\n\tconst suppress: SuppressPredicate = (file) => {\n\t\tconst realFile = safeRealpath(file.path);\n\t\t// Only suppress when the file was delivered *in full*: a truncated delivery (oversized\n\t\t// file) would drop the remainder if we marked it fully loaded and skipped injection.\n\t\t// `read` delivers the file's raw content unchanged; `bash` delivers it through terminal\n\t\t// rendering (tab/ANSI expansion can grow it past the truncation budget), so each path\n\t\t// measures fullness against what it actually emits.\n\t\tif (realFile === realSelfReadFile) return deliveredInFull(realFile, false);\n\t\tif (bashDelivered?.has(realFile)) return deliveredInFull(realFile, true);\n\t\treturn false;\n\t};\n\n\tconst collected = collectNestedContext(targetDir, state.cwd, state.loaded, suppress);\n\tif (!collected.hadReadError) {\n\t\tstate.scannedDirs.add(realTarget);\n\t}\n\tif (collected.files.length === 0) return null;\n\treturn formatNestedContextBlock(targetDir, collected.files);\n}\n"]}
@@ -63,6 +63,11 @@ export interface ResourceLoader {
63
63
  extendResources(paths: ResourceExtensionPaths): void;
64
64
  reload(): Promise<void>;
65
65
  }
66
+ export declare const CONTEXT_FILE_CANDIDATES: readonly ["AGENTS.md", "AGENTS.MD", "CLAUDE.md", "CLAUDE.MD", string, string, string, string];
67
+ export declare function loadContextFilesFromDir(dir: string, diagnostics?: ResourceDiagnostic[]): Array<{
68
+ path: string;
69
+ content: string;
70
+ }>;
66
71
  /**
67
72
  * Encode an absolute POSIX path the way Claude Code does for ~/.claude/projects/ directories.
68
73
  * Replaces path separators and underscores with hyphens.
@@ -1 +1 @@
1
- {"version":3,"file":"resource-loader.d.ts","sourceRoot":"","sources":["../../src/core/resource-loader.ts"],"names":[],"mappings":"AAIA,OAAO,EAAqB,KAAK,KAAK,EAAE,MAAM,qCAAqC,CAAC;AACpF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAG3D,YAAY,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE9E,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/D,OAAO,KAAK,EAAa,gBAAgB,EAAoB,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACjH,OAAO,EAAyB,KAAK,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,KAAK,EAAE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oDnC,OAAO,CAAC,UAAU;IAclB,OAAO,CAAC,mBAAmB;IAa3B,OAAO,CAAC,UAAU;IA0ClB,OAAO,CAAC,iBAAiB;IA+BzB,OAAO,CAAC,iBAAiB;YASX,sBAAsB;IAqBpC,OAAO,CAAC,aAAa;IA0BrB,OAAO,CAAC,YAAY;IA2BpB,OAAO,CAAC,wBAAwB;IAchC,OAAO,CAAC,8BAA8B;IActC,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,wBAAwB;CAqChC","sourcesContent":["import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve, sep } from \"node:path\";\nimport { CONFIG_DIR_NAME, getAgentDir } from \"../config.js\";\nimport { loadThemeFromPath, type Theme } from \"../modes/interactive/theme/theme.js\";\nimport type { ResourceDiagnostic } from \"./diagnostics.js\";\nimport { findGitRoot } from \"./git-root.js\";\n\nexport type { ResourceCollision, ResourceDiagnostic } from \"./diagnostics.js\";\n\nimport { createEventBus, type EventBus } from \"./event-bus.js\";\nimport { createExtensionRuntime, loadExtensionFromFactory, loadExtensions } from \"./extensions/loader.js\";\nimport type { Extension, ExtensionFactory, ExtensionRuntime, LoadExtensionsResult } from \"./extensions/types.js\";\nimport { DefaultPackageManager, type PathMetadata } from \"./package-manager.js\";\nimport type { PromptTemplate } from \"./prompt-templates.js\";\nimport { loadPromptTemplates } from \"./prompt-templates.js\";\nimport { SettingsManager } from \"./settings-manager.js\";\nimport type { Skill } from \"./skills.js\";\nimport { loadSkills } from \"./skills.js\";\nimport { createSourceInfo, type SourceInfo } from \"./source-info.js\";\n\nexport interface ResourceExtensionPaths {\n\tskillPaths?: Array<{ path: string; metadata: PathMetadata }>;\n\tpromptPaths?: Array<{ path: string; metadata: PathMetadata }>;\n\tthemePaths?: Array<{ path: string; metadata: PathMetadata }>;\n}\n\nexport interface MemorySource {\n\treadonly content: string;\n\treadonly dir: string;\n\treadonly source: \"dreb\" | \"claude\";\n}\n\nexport interface MemoryIndexes {\n\treadonly global: readonly MemorySource[];\n\treadonly project: readonly MemorySource[];\n\treadonly globalMemoryDir: string;\n\treadonly projectMemoryDir: string;\n\t/** ISO timestamp from .dream-last-run, or null if file is missing/invalid. */\n\treadonly dreamLastRun: string | null;\n}\n\nexport interface ResourceLoader {\n\tgetExtensions(): LoadExtensionsResult;\n\tgetSkills(): { skills: Skill[]; diagnostics: ResourceDiagnostic[] };\n\tgetPrompts(): { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] };\n\tgetThemes(): { themes: Theme[]; diagnostics: ResourceDiagnostic[] };\n\tgetContextDiagnostics(): ResourceDiagnostic[];\n\tgetAgentsFiles(): { agentsFiles: Array<{ path: string; content: string }> };\n\tgetMemoryIndexes(): MemoryIndexes;\n\t/** Re-read the .dream-last-run timestamp from disk without a full reload. */\n\trefreshDreamLastRun(): void;\n\tgetSystemPrompt(): string | undefined;\n\tgetAppendSystemPrompt(): string[];\n\textendResources(paths: ResourceExtensionPaths): void;\n\treload(): Promise<void>;\n}\n\nfunction resolvePromptInput(\n\tinput: string | undefined,\n\tdescription: string,\n\tdiagnostics?: ResourceDiagnostic[],\n): string | undefined {\n\tif (!input) {\n\t\treturn undefined;\n\t}\n\n\tif (existsSync(input)) {\n\t\ttry {\n\t\t\treturn readFileSync(input, \"utf-8\");\n\t\t} catch (error) {\n\t\t\tdiagnostics?.push({\n\t\t\t\ttype: \"warning\",\n\t\t\t\tmessage: `Could not read ${description} file ${input}: ${error}`,\n\t\t\t\tpath: input,\n\t\t\t});\n\t\t\treturn input;\n\t\t}\n\t}\n\n\treturn input;\n}\n\nfunction stripHtmlComments(content: string): string {\n\treturn content.replace(/<!--[\\s\\S]*?-->/g, \"\");\n}\n\nfunction loadContextFilesFromDir(\n\tdir: string,\n\tdiagnostics?: ResourceDiagnostic[],\n): Array<{ path: string; content: string }> {\n\tconst candidates = [\n\t\t\"AGENTS.md\",\n\t\t\"AGENTS.MD\",\n\t\t\"CLAUDE.md\",\n\t\t\"CLAUDE.MD\",\n\t\tjoin(\".claude\", \"CLAUDE.md\"),\n\t\tjoin(\".claude\", \"CLAUDE.MD\"),\n\t\tjoin(\".dreb\", \"CONTEXT.md\"),\n\t\tjoin(\".dreb\", \"CONTEXT.MD\"),\n\t];\n\tconst results: Array<{ path: string; content: string }> = [];\n\tconst seenRealPaths = new Set<string>();\n\tfor (const filename of candidates) {\n\t\tconst filePath = join(dir, filename);\n\t\tif (existsSync(filePath)) {\n\t\t\t// Deduplicate by realpath to avoid loading the same file twice on\n\t\t\t// case-insensitive filesystems (macOS) where AGENTS.md and AGENTS.MD\n\t\t\t// resolve to the same inode.\n\t\t\tlet realPath: string;\n\t\t\ttry {\n\t\t\t\trealPath = realpathSync(filePath);\n\t\t\t} catch {\n\t\t\t\trealPath = filePath;\n\t\t\t}\n\t\t\tif (seenRealPaths.has(realPath)) continue;\n\t\t\tseenRealPaths.add(realPath);\n\t\t\ttry {\n\t\t\t\tresults.push({\n\t\t\t\t\tpath: filePath,\n\t\t\t\t\tcontent: stripHtmlComments(readFileSync(filePath, \"utf-8\")),\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read ${filePath}: ${error}`, path: filePath });\n\t\t\t}\n\t\t}\n\t}\n\treturn results;\n}\n\nfunction loadRulesFromDir(\n\tdir: string,\n\tcontextFiles: Array<{ path: string; content: string }>,\n\tseenPaths: Set<string>,\n\tdepth: number = 0,\n\tdiagnostics?: ResourceDiagnostic[],\n): void {\n\tif (depth > 10) return;\n\tlet entries: import(\"node:fs\").Dirent[];\n\ttry {\n\t\tentries = readdirSync(dir, { withFileTypes: true });\n\t} catch (error) {\n\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read rules directory ${dir}: ${error}`, path: dir });\n\t\treturn;\n\t}\n\tfor (const entry of entries) {\n\t\tconst fullPath = join(dir, entry.name);\n\t\tif (entry.isDirectory() && !entry.isSymbolicLink()) {\n\t\t\tloadRulesFromDir(fullPath, contextFiles, seenPaths, depth + 1, diagnostics);\n\t\t} else if (entry.isFile() && entry.name.endsWith(\".md\") && !seenPaths.has(fullPath)) {\n\t\t\ttry {\n\t\t\t\tconst content = readFileSync(fullPath, \"utf-8\");\n\t\t\t\t// Skip path-scoped rules (paths: in frontmatter) — not yet implemented, just skip\n\t\t\t\tif (content.startsWith(\"---\")) {\n\t\t\t\t\tconst endIdx = content.indexOf(\"---\", 3);\n\t\t\t\t\tif (endIdx !== -1) {\n\t\t\t\t\t\tconst frontmatter = content.slice(3, endIdx);\n\t\t\t\t\t\tif (/^\\s*paths\\s*:/m.test(frontmatter)) {\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tseenPaths.add(fullPath);\n\t\t\t\tcontextFiles.push({ path: fullPath, content: stripHtmlComments(content) });\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({\n\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\tmessage: `Could not read rule ${fullPath}: ${error}`,\n\t\t\t\t\tpath: fullPath,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunction loadProjectContextFiles(\n\toptions: { cwd?: string; agentDir?: string } = {},\n\tdiagnostics?: ResourceDiagnostic[],\n): Array<{ path: string; content: string }> {\n\tconst resolvedCwd = options.cwd ?? process.cwd();\n\tconst resolvedAgentDir = options.agentDir ?? getAgentDir();\n\n\tconst contextFiles: Array<{ path: string; content: string }> = [];\n\tconst seenPaths = new Set<string>();\n\n\t// 1. User-level context files (global, loaded first — project files follow and can override)\n\tconst userLevelPaths = [\n\t\tjoin(resolvedAgentDir, \"AGENTS.md\"),\n\t\tjoin(resolvedAgentDir, \"CLAUDE.md\"),\n\t\tjoin(homedir(), \".claude\", \"CLAUDE.md\"),\n\t\tjoin(homedir(), \".dreb\", \"CONTEXT.md\"),\n\t];\n\tfor (const filePath of userLevelPaths) {\n\t\tif (existsSync(filePath)) {\n\t\t\ttry {\n\t\t\t\tconst content = stripHtmlComments(readFileSync(filePath, \"utf-8\"));\n\t\t\t\tif (!seenPaths.has(filePath)) {\n\t\t\t\t\tseenPaths.add(filePath);\n\t\t\t\t\tcontextFiles.push({ path: filePath, content });\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read ${filePath}: ${error}`, path: filePath });\n\t\t\t}\n\t\t}\n\t}\n\n\t// 2. Walk upward from cwd to root, collecting all context files per directory (ordered root-first via unshift)\n\tconst ancestorContextFiles: Array<{ path: string; content: string }> = [];\n\tlet currentDir = resolvedCwd;\n\tconst root = resolve(\"/\");\n\n\twhile (true) {\n\t\tconst dirFiles = loadContextFilesFromDir(currentDir, diagnostics);\n\t\tconst newFiles = dirFiles.filter((f) => !seenPaths.has(f.path));\n\t\tfor (const file of newFiles) {\n\t\t\tseenPaths.add(file.path);\n\t\t}\n\t\tancestorContextFiles.unshift(...newFiles);\n\n\t\tif (currentDir === root) break;\n\t\tconst parentDir = resolve(currentDir, \"..\");\n\t\tif (parentDir === currentDir) break;\n\t\tcurrentDir = parentDir;\n\t}\n\n\tcontextFiles.push(...ancestorContextFiles);\n\n\t// 3. Rules directories (unconditional rules loaded now)\n\tconst rulesSearchDirs = [\n\t\tjoin(resolvedCwd, \".dreb\", \"rules\"),\n\t\tjoin(resolvedCwd, \".claude\", \"rules\"),\n\t\tjoin(resolvedCwd, CONFIG_DIR_NAME, \"rules\"),\n\t];\n\tfor (const rulesDir of rulesSearchDirs) {\n\t\tif (existsSync(rulesDir)) {\n\t\t\ttry {\n\t\t\t\tloadRulesFromDir(rulesDir, contextFiles, seenPaths, 0, diagnostics);\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({\n\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\tmessage: `Could not read rules from ${rulesDir}: ${error}`,\n\t\t\t\t\tpath: rulesDir,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\treturn contextFiles;\n}\n\nconst MEMORY_INDEX_MAX_LINES = 200;\n\n/**\n * Encode an absolute POSIX path the way Claude Code does for ~/.claude/projects/ directories.\n * Replaces path separators and underscores with hyphens.\n * /home/drew/projects/deep_yellow -> -home-drew-projects-deep-yellow\n */\nexport function encodeClaudeProjectPath(absolutePath: string): string {\n\treturn absolutePath.replace(/[/_]/g, \"-\");\n}\n\nfunction readMemoryIndex(dir: string, diagnostics?: ResourceDiagnostic[]): string | undefined {\n\tconst indexPath = join(dir, \"MEMORY.md\");\n\ttry {\n\t\tif (!existsSync(indexPath)) return undefined;\n\t\tconst content = readFileSync(indexPath, \"utf-8\");\n\t\tconst lines = content.split(\"\\n\");\n\t\tif (lines.length > MEMORY_INDEX_MAX_LINES) {\n\t\t\tdiagnostics?.push({\n\t\t\t\ttype: \"warning\",\n\t\t\t\tmessage: `${indexPath} has ${lines.length} lines, truncated to ${MEMORY_INDEX_MAX_LINES}. Consider cleaning up older entries.`,\n\t\t\t\tpath: indexPath,\n\t\t\t});\n\t\t\treturn lines.slice(0, MEMORY_INDEX_MAX_LINES).join(\"\\n\");\n\t\t}\n\t\treturn content;\n\t} catch (error) {\n\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read ${indexPath}: ${error}`, path: indexPath });\n\t\treturn undefined;\n\t}\n}\n\nfunction loadMemoryIndexes(\n\toptions: { cwd?: string; agentDir?: string },\n\tdiagnostics?: ResourceDiagnostic[],\n): MemoryIndexes {\n\tconst resolvedCwd = options.cwd ?? process.cwd();\n\tconst projectRoot = findGitRoot(resolvedCwd) ?? resolvedCwd;\n\tconst drebRoot = options.agentDir ? resolve(options.agentDir, \"..\") : join(homedir(), \".dreb\");\n\n\t// Primary write targets for dreb\n\tconst globalMemoryDir = join(drebRoot, \"memory\");\n\tconst projectMemoryDir = join(projectRoot, \".dreb\", \"memory\");\n\n\t// Claude Code memory paths (read-only — dreb reads these but writes to .dreb/)\n\tconst claudeGlobalMemoryDir = join(homedir(), \".claude\", \"projects\", encodeClaudeProjectPath(homedir()), \"memory\");\n\tconst claudeProjectMemoryDir = join(\n\t\thomedir(),\n\t\t\".claude\",\n\t\t\"projects\",\n\t\tencodeClaudeProjectPath(projectRoot),\n\t\t\"memory\",\n\t);\n\n\tconst globalSources: MemorySource[] = [];\n\tconst projectSources: MemorySource[] = [];\n\n\t// Load dreb memory (primary)\n\tconst drebGlobal = readMemoryIndex(globalMemoryDir, diagnostics);\n\tif (drebGlobal) {\n\t\tglobalSources.push({ content: drebGlobal, dir: globalMemoryDir, source: \"dreb\" });\n\t}\n\n\tconst drebProject =\n\t\tglobalMemoryDir !== projectMemoryDir ? readMemoryIndex(projectMemoryDir, diagnostics) : undefined;\n\tif (drebProject) {\n\t\tprojectSources.push({ content: drebProject, dir: projectMemoryDir, source: \"dreb\" });\n\t}\n\n\t// Load Claude Code memory (read-only, compat)\n\tconst claudeGlobal = readMemoryIndex(claudeGlobalMemoryDir, diagnostics);\n\tif (claudeGlobal) {\n\t\tglobalSources.push({ content: claudeGlobal, dir: claudeGlobalMemoryDir, source: \"claude\" });\n\t}\n\n\t// Only load Claude project memory if it's a different path than Claude global\n\tif (claudeProjectMemoryDir !== claudeGlobalMemoryDir) {\n\t\tconst claudeProject = readMemoryIndex(claudeProjectMemoryDir, diagnostics);\n\t\tif (claudeProject) {\n\t\t\tprojectSources.push({ content: claudeProject, dir: claudeProjectMemoryDir, source: \"claude\" });\n\t\t}\n\t}\n\n\t// Read dream last-run timestamp (graceful null on missing/invalid)\n\tlet dreamLastRun: string | null = null;\n\ttry {\n\t\tconst dreamLastRunPath = join(globalMemoryDir, \".dream-last-run\");\n\t\tif (existsSync(dreamLastRunPath)) {\n\t\t\tconst raw = readFileSync(dreamLastRunPath, \"utf-8\").trim();\n\t\t\t// Validate it parses as a date\n\t\t\tif (raw && !Number.isNaN(Date.parse(raw))) {\n\t\t\t\tdreamLastRun = raw;\n\t\t\t}\n\t\t}\n\t} catch {\n\t\t// Silently ignore — missing/unreadable file is fine\n\t}\n\n\treturn {\n\t\tglobal: globalSources,\n\t\tproject: projectSources,\n\t\tglobalMemoryDir,\n\t\tprojectMemoryDir,\n\t\tdreamLastRun,\n\t};\n}\n\nexport interface DefaultResourceLoaderOptions {\n\tcwd?: string;\n\tagentDir?: string;\n\tsettingsManager?: SettingsManager;\n\teventBus?: EventBus;\n\tadditionalExtensionPaths?: string[];\n\tadditionalSkillPaths?: string[];\n\tadditionalPromptTemplatePaths?: string[];\n\tadditionalThemePaths?: string[];\n\textensionFactories?: ExtensionFactory[];\n\tnoExtensions?: boolean;\n\tnoSkills?: boolean;\n\tnoPromptTemplates?: boolean;\n\tnoThemes?: boolean;\n\tsystemPrompt?: string;\n\tappendSystemPrompt?: string;\n\textensionsOverride?: (base: LoadExtensionsResult) => LoadExtensionsResult;\n\tskillsOverride?: (base: { skills: Skill[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tskills: Skill[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tpromptsOverride?: (base: { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tprompts: PromptTemplate[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tthemesOverride?: (base: { themes: Theme[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tthemes: Theme[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tagentsFilesOverride?: (base: { agentsFiles: Array<{ path: string; content: string }> }) => {\n\t\tagentsFiles: Array<{ path: string; content: string }>;\n\t};\n\tsystemPromptOverride?: (base: string | undefined) => string | undefined;\n\tappendSystemPromptOverride?: (base: string[]) => string[];\n}\n\nexport class DefaultResourceLoader implements ResourceLoader {\n\tprivate cwd: string;\n\tprivate agentDir: string;\n\tprivate settingsManager: SettingsManager;\n\tprivate eventBus: EventBus;\n\tprivate packageManager: DefaultPackageManager;\n\tprivate additionalExtensionPaths: string[];\n\tprivate additionalSkillPaths: string[];\n\tprivate additionalPromptTemplatePaths: string[];\n\tprivate additionalThemePaths: string[];\n\tprivate extensionFactories: ExtensionFactory[];\n\tprivate noExtensions: boolean;\n\tprivate noSkills: boolean;\n\tprivate noPromptTemplates: boolean;\n\tprivate noThemes: boolean;\n\tprivate systemPromptSource?: string;\n\tprivate appendSystemPromptSource?: string;\n\tprivate extensionsOverride?: (base: LoadExtensionsResult) => LoadExtensionsResult;\n\tprivate skillsOverride?: (base: { skills: Skill[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tskills: Skill[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tprivate promptsOverride?: (base: { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tprompts: PromptTemplate[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tprivate themesOverride?: (base: { themes: Theme[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tthemes: Theme[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tprivate agentsFilesOverride?: (base: { agentsFiles: Array<{ path: string; content: string }> }) => {\n\t\tagentsFiles: Array<{ path: string; content: string }>;\n\t};\n\tprivate systemPromptOverride?: (base: string | undefined) => string | undefined;\n\tprivate appendSystemPromptOverride?: (base: string[]) => string[];\n\n\tprivate extensionsResult: LoadExtensionsResult;\n\tprivate skills: Skill[];\n\tprivate skillDiagnostics: ResourceDiagnostic[];\n\tprivate prompts: PromptTemplate[];\n\tprivate promptDiagnostics: ResourceDiagnostic[];\n\tprivate themes: Theme[];\n\tprivate themeDiagnostics: ResourceDiagnostic[];\n\tprivate agentsFiles: Array<{ path: string; content: string }>;\n\tprivate memoryIndexes: MemoryIndexes;\n\tprivate systemPrompt?: string;\n\tprivate appendSystemPrompt: string[];\n\tprivate contextDiagnostics: ResourceDiagnostic[];\n\tprivate lastSkillPaths: string[];\n\tprivate extensionSkillSourceInfos: Map<string, SourceInfo>;\n\tprivate extensionPromptSourceInfos: Map<string, SourceInfo>;\n\tprivate extensionThemeSourceInfos: Map<string, SourceInfo>;\n\tprivate lastPromptPaths: string[];\n\tprivate lastThemePaths: string[];\n\n\tconstructor(options: DefaultResourceLoaderOptions) {\n\t\tthis.cwd = options.cwd ?? process.cwd();\n\t\tthis.agentDir = options.agentDir ?? getAgentDir();\n\t\tthis.settingsManager = options.settingsManager ?? SettingsManager.create(this.cwd, this.agentDir);\n\t\tthis.eventBus = options.eventBus ?? createEventBus();\n\t\tthis.packageManager = new DefaultPackageManager({\n\t\t\tcwd: this.cwd,\n\t\t\tagentDir: this.agentDir,\n\t\t\tsettingsManager: this.settingsManager,\n\t\t});\n\t\tthis.additionalExtensionPaths = options.additionalExtensionPaths ?? [];\n\t\tthis.additionalSkillPaths = options.additionalSkillPaths ?? [];\n\t\tthis.additionalPromptTemplatePaths = options.additionalPromptTemplatePaths ?? [];\n\t\tthis.additionalThemePaths = options.additionalThemePaths ?? [];\n\t\tthis.extensionFactories = options.extensionFactories ?? [];\n\t\tthis.noExtensions = options.noExtensions ?? false;\n\t\tthis.noSkills = options.noSkills ?? false;\n\t\tthis.noPromptTemplates = options.noPromptTemplates ?? false;\n\t\tthis.noThemes = options.noThemes ?? false;\n\t\tthis.systemPromptSource = options.systemPrompt;\n\t\tthis.appendSystemPromptSource = options.appendSystemPrompt;\n\t\tthis.extensionsOverride = options.extensionsOverride;\n\t\tthis.skillsOverride = options.skillsOverride;\n\t\tthis.promptsOverride = options.promptsOverride;\n\t\tthis.themesOverride = options.themesOverride;\n\t\tthis.agentsFilesOverride = options.agentsFilesOverride;\n\t\tthis.systemPromptOverride = options.systemPromptOverride;\n\t\tthis.appendSystemPromptOverride = options.appendSystemPromptOverride;\n\n\t\tthis.extensionsResult = { extensions: [], errors: [], runtime: createExtensionRuntime() };\n\t\tthis.skills = [];\n\t\tthis.skillDiagnostics = [];\n\t\tthis.prompts = [];\n\t\tthis.promptDiagnostics = [];\n\t\tthis.themes = [];\n\t\tthis.themeDiagnostics = [];\n\t\tthis.agentsFiles = [];\n\t\tthis.memoryIndexes = {\n\t\t\tglobal: [],\n\t\t\tproject: [],\n\t\t\tglobalMemoryDir: join(resolve(this.agentDir, \"..\"), \"memory\"),\n\t\t\tprojectMemoryDir: join(this.cwd, \".dreb\", \"memory\"),\n\t\t\tdreamLastRun: null,\n\t\t};\n\t\tthis.appendSystemPrompt = [];\n\t\tthis.contextDiagnostics = [];\n\t\tthis.lastSkillPaths = [];\n\t\tthis.extensionSkillSourceInfos = new Map();\n\t\tthis.extensionPromptSourceInfos = new Map();\n\t\tthis.extensionThemeSourceInfos = new Map();\n\t\tthis.lastPromptPaths = [];\n\t\tthis.lastThemePaths = [];\n\t}\n\n\tgetExtensions(): LoadExtensionsResult {\n\t\treturn this.extensionsResult;\n\t}\n\n\tgetSkills(): { skills: Skill[]; diagnostics: ResourceDiagnostic[] } {\n\t\treturn { skills: this.skills, diagnostics: this.skillDiagnostics };\n\t}\n\n\tgetPrompts(): { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] } {\n\t\treturn { prompts: this.prompts, diagnostics: this.promptDiagnostics };\n\t}\n\n\tgetThemes(): { themes: Theme[]; diagnostics: ResourceDiagnostic[] } {\n\t\treturn { themes: this.themes, diagnostics: this.themeDiagnostics };\n\t}\n\n\tgetContextDiagnostics(): ResourceDiagnostic[] {\n\t\treturn this.contextDiagnostics;\n\t}\n\n\tgetAgentsFiles(): { agentsFiles: Array<{ path: string; content: string }> } {\n\t\treturn { agentsFiles: this.agentsFiles };\n\t}\n\n\tgetMemoryIndexes(): MemoryIndexes {\n\t\treturn this.memoryIndexes;\n\t}\n\n\trefreshDreamLastRun(): void {\n\t\tconst globalMemoryDir = join(resolve(this.agentDir, \"..\"), \"memory\");\n\t\tlet dreamLastRun: string | null = null;\n\t\ttry {\n\t\t\tconst dreamLastRunPath = join(globalMemoryDir, \".dream-last-run\");\n\t\t\tif (existsSync(dreamLastRunPath)) {\n\t\t\t\tconst raw = readFileSync(dreamLastRunPath, \"utf-8\").trim();\n\t\t\t\tif (raw && !Number.isNaN(Date.parse(raw))) {\n\t\t\t\t\tdreamLastRun = raw;\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Silently ignore — missing/unreadable file is fine\n\t\t}\n\t\tif (dreamLastRun !== this.memoryIndexes.dreamLastRun) {\n\t\t\tthis.memoryIndexes = { ...this.memoryIndexes, dreamLastRun };\n\t\t}\n\t}\n\n\tgetSystemPrompt(): string | undefined {\n\t\treturn this.systemPrompt;\n\t}\n\n\tgetAppendSystemPrompt(): string[] {\n\t\treturn this.appendSystemPrompt;\n\t}\n\n\textendResources(paths: ResourceExtensionPaths): void {\n\t\tconst skillPaths = this.normalizeExtensionPaths(paths.skillPaths ?? []);\n\t\tconst promptPaths = this.normalizeExtensionPaths(paths.promptPaths ?? []);\n\t\tconst themePaths = this.normalizeExtensionPaths(paths.themePaths ?? []);\n\n\t\tfor (const entry of skillPaths) {\n\t\t\tthis.extensionSkillSourceInfos.set(entry.path, createSourceInfo(entry.path, entry.metadata));\n\t\t}\n\t\tfor (const entry of promptPaths) {\n\t\t\tthis.extensionPromptSourceInfos.set(entry.path, createSourceInfo(entry.path, entry.metadata));\n\t\t}\n\t\tfor (const entry of themePaths) {\n\t\t\tthis.extensionThemeSourceInfos.set(entry.path, createSourceInfo(entry.path, entry.metadata));\n\t\t}\n\n\t\tif (skillPaths.length > 0) {\n\t\t\tthis.lastSkillPaths = this.mergePaths(\n\t\t\t\tthis.lastSkillPaths,\n\t\t\t\tskillPaths.map((entry) => entry.path),\n\t\t\t);\n\t\t\tthis.updateSkillsFromPaths(this.lastSkillPaths);\n\t\t}\n\n\t\tif (promptPaths.length > 0) {\n\t\t\tthis.lastPromptPaths = this.mergePaths(\n\t\t\t\tthis.lastPromptPaths,\n\t\t\t\tpromptPaths.map((entry) => entry.path),\n\t\t\t);\n\t\t\tthis.updatePromptsFromPaths(this.lastPromptPaths);\n\t\t}\n\n\t\tif (themePaths.length > 0) {\n\t\t\tthis.lastThemePaths = this.mergePaths(\n\t\t\t\tthis.lastThemePaths,\n\t\t\t\tthemePaths.map((entry) => entry.path),\n\t\t\t);\n\t\t\tthis.updateThemesFromPaths(this.lastThemePaths);\n\t\t}\n\t}\n\n\tasync reload(): Promise<void> {\n\t\tthis.contextDiagnostics = [];\n\t\tconst resolvedPaths = await this.packageManager.resolve();\n\t\tconst cliExtensionPaths = await this.packageManager.resolveExtensionSources(this.additionalExtensionPaths, {\n\t\t\ttemporary: true,\n\t\t});\n\t\tconst metadataByPath = new Map<string, PathMetadata>();\n\n\t\tthis.extensionSkillSourceInfos = new Map();\n\t\tthis.extensionPromptSourceInfos = new Map();\n\t\tthis.extensionThemeSourceInfos = new Map();\n\n\t\t// Helper to extract enabled paths and store metadata\n\t\tconst getEnabledResources = (\n\t\t\tresources: Array<{ path: string; enabled: boolean; metadata: PathMetadata }>,\n\t\t): Array<{ path: string; enabled: boolean; metadata: PathMetadata }> => {\n\t\t\tfor (const r of resources) {\n\t\t\t\tif (!metadataByPath.has(r.path)) {\n\t\t\t\t\tmetadataByPath.set(r.path, r.metadata);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn resources.filter((r) => r.enabled);\n\t\t};\n\n\t\tconst getEnabledPaths = (\n\t\t\tresources: Array<{ path: string; enabled: boolean; metadata: PathMetadata }>,\n\t\t): string[] => getEnabledResources(resources).map((r) => r.path);\n\t\tconst enabledExtensions = getEnabledPaths(resolvedPaths.extensions);\n\t\tconst enabledSkillResources = getEnabledResources(resolvedPaths.skills);\n\t\tconst enabledPrompts = getEnabledPaths(resolvedPaths.prompts);\n\t\tconst enabledThemes = getEnabledPaths(resolvedPaths.themes);\n\n\t\tconst mapSkillPath = (resource: { path: string; metadata: PathMetadata }): string => {\n\t\t\tif (resource.metadata.source !== \"auto\" && resource.metadata.origin !== \"package\") {\n\t\t\t\treturn resource.path;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst stats = statSync(resource.path);\n\t\t\t\tif (!stats.isDirectory()) {\n\t\t\t\t\treturn resource.path;\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// stat failed — path may not exist yet; use as-is\n\t\t\t\treturn resource.path;\n\t\t\t}\n\t\t\tconst skillFile = join(resource.path, \"SKILL.md\");\n\t\t\tif (existsSync(skillFile)) {\n\t\t\t\tif (!metadataByPath.has(skillFile)) {\n\t\t\t\t\tmetadataByPath.set(skillFile, resource.metadata);\n\t\t\t\t}\n\t\t\t\treturn skillFile;\n\t\t\t}\n\t\t\treturn resource.path;\n\t\t};\n\n\t\tconst enabledSkills = enabledSkillResources.map(mapSkillPath);\n\n\t\t// Add CLI paths metadata\n\t\tfor (const r of cliExtensionPaths.extensions) {\n\t\t\tif (!metadataByPath.has(r.path)) {\n\t\t\t\tmetadataByPath.set(r.path, { source: \"cli\", scope: \"temporary\", origin: \"top-level\" });\n\t\t\t}\n\t\t}\n\t\tfor (const r of cliExtensionPaths.skills) {\n\t\t\tif (!metadataByPath.has(r.path)) {\n\t\t\t\tmetadataByPath.set(r.path, { source: \"cli\", scope: \"temporary\", origin: \"top-level\" });\n\t\t\t}\n\t\t}\n\n\t\tconst cliEnabledExtensions = getEnabledPaths(cliExtensionPaths.extensions);\n\t\tconst cliEnabledSkills = getEnabledPaths(cliExtensionPaths.skills);\n\t\tconst cliEnabledPrompts = getEnabledPaths(cliExtensionPaths.prompts);\n\t\tconst cliEnabledThemes = getEnabledPaths(cliExtensionPaths.themes);\n\n\t\tconst extensionPaths = this.noExtensions\n\t\t\t? cliEnabledExtensions\n\t\t\t: this.mergePaths(cliEnabledExtensions, enabledExtensions);\n\n\t\tconst extensionsResult = await loadExtensions(extensionPaths, this.cwd, this.eventBus);\n\t\tconst inlineExtensions = await this.loadExtensionFactories(extensionsResult.runtime);\n\t\textensionsResult.extensions.push(...inlineExtensions.extensions);\n\t\textensionsResult.errors.push(...inlineExtensions.errors);\n\n\t\t// Detect extension conflicts (tools, commands, flags with same names from different extensions)\n\t\t// Keep all extensions loaded. Conflicts are reported as diagnostics, and precedence is handled by load order.\n\t\tconst conflicts = this.detectExtensionConflicts(extensionsResult.extensions);\n\t\tfor (const conflict of conflicts) {\n\t\t\textensionsResult.errors.push({ path: conflict.path, error: conflict.message });\n\t\t}\n\n\t\tthis.extensionsResult = this.extensionsOverride ? this.extensionsOverride(extensionsResult) : extensionsResult;\n\t\tthis.applyExtensionSourceInfo(this.extensionsResult.extensions, metadataByPath);\n\n\t\tconst skillPaths = this.noSkills\n\t\t\t? this.mergePaths(cliEnabledSkills, this.additionalSkillPaths)\n\t\t\t: this.mergePaths([...enabledSkills, ...cliEnabledSkills], this.additionalSkillPaths);\n\n\t\tthis.lastSkillPaths = skillPaths;\n\t\tthis.updateSkillsFromPaths(skillPaths, metadataByPath);\n\n\t\tconst promptPaths = this.noPromptTemplates\n\t\t\t? this.mergePaths(cliEnabledPrompts, this.additionalPromptTemplatePaths)\n\t\t\t: this.mergePaths([...enabledPrompts, ...cliEnabledPrompts], this.additionalPromptTemplatePaths);\n\n\t\tthis.lastPromptPaths = promptPaths;\n\t\tthis.updatePromptsFromPaths(promptPaths, metadataByPath);\n\n\t\tconst themePaths = this.noThemes\n\t\t\t? this.mergePaths(cliEnabledThemes, this.additionalThemePaths)\n\t\t\t: this.mergePaths([...enabledThemes, ...cliEnabledThemes], this.additionalThemePaths);\n\n\t\tthis.lastThemePaths = themePaths;\n\t\tthis.updateThemesFromPaths(themePaths, metadataByPath);\n\n\t\tconst agentsFiles = {\n\t\t\tagentsFiles: loadProjectContextFiles({ cwd: this.cwd, agentDir: this.agentDir }, this.contextDiagnostics),\n\t\t};\n\t\tconst resolvedAgentsFiles = this.agentsFilesOverride ? this.agentsFilesOverride(agentsFiles) : agentsFiles;\n\t\tthis.agentsFiles = resolvedAgentsFiles.agentsFiles;\n\n\t\t// Load memory indexes and ensure global memory directory exists.\n\t\t// Wrapped in try/catch so memory loading failures degrade gracefully rather than crashing the session.\n\t\ttry {\n\t\t\tthis.memoryIndexes = loadMemoryIndexes({ cwd: this.cwd, agentDir: this.agentDir }, this.contextDiagnostics);\n\t\t\ttry {\n\t\t\t\tmkdirSync(this.memoryIndexes.globalMemoryDir, { recursive: true });\n\t\t\t} catch (error) {\n\t\t\t\tthis.contextDiagnostics.push({\n\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\tmessage: `Could not create memory directory ${this.memoryIndexes.globalMemoryDir}: ${error}. Memory saves to this directory will fail.`,\n\t\t\t\t\tpath: this.memoryIndexes.globalMemoryDir,\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis.contextDiagnostics.push({\n\t\t\t\ttype: \"warning\",\n\t\t\t\tmessage: `Could not load memory indexes: ${error}. Session will start without memory context.`,\n\t\t\t});\n\t\t}\n\n\t\tconst baseSystemPrompt = resolvePromptInput(\n\t\t\tthis.systemPromptSource ?? this.discoverSystemPromptFile(),\n\t\t\t\"system prompt\",\n\t\t\tthis.contextDiagnostics,\n\t\t);\n\t\tthis.systemPrompt = this.systemPromptOverride ? this.systemPromptOverride(baseSystemPrompt) : baseSystemPrompt;\n\n\t\tconst appendSource = this.appendSystemPromptSource ?? this.discoverAppendSystemPromptFile();\n\t\tconst resolvedAppend = resolvePromptInput(appendSource, \"append system prompt\", this.contextDiagnostics);\n\t\tconst baseAppend = resolvedAppend ? [resolvedAppend] : [];\n\t\tthis.appendSystemPrompt = this.appendSystemPromptOverride\n\t\t\t? this.appendSystemPromptOverride(baseAppend)\n\t\t\t: baseAppend;\n\t}\n\n\tprivate normalizeExtensionPaths(\n\t\tentries: Array<{ path: string; metadata: PathMetadata }>,\n\t): Array<{ path: string; metadata: PathMetadata }> {\n\t\treturn entries.map((entry) => ({\n\t\t\tpath: this.resolveResourcePath(entry.path),\n\t\t\tmetadata: entry.metadata,\n\t\t}));\n\t}\n\n\tprivate updateSkillsFromPaths(skillPaths: string[], metadataByPath?: Map<string, PathMetadata>): void {\n\t\tlet skillsResult: { skills: Skill[]; diagnostics: ResourceDiagnostic[] };\n\t\tif (this.noSkills && skillPaths.length === 0) {\n\t\t\tskillsResult = { skills: [], diagnostics: [] };\n\t\t} else {\n\t\t\tskillsResult = loadSkills({\n\t\t\t\tcwd: this.cwd,\n\t\t\t\tagentDir: this.agentDir,\n\t\t\t\tskillPaths,\n\t\t\t\tincludeDefaults: false,\n\t\t\t});\n\t\t}\n\t\tconst resolvedSkills = this.skillsOverride ? this.skillsOverride(skillsResult) : skillsResult;\n\t\tthis.skills = resolvedSkills.skills.map((skill) => ({\n\t\t\t...skill,\n\t\t\tsourceInfo:\n\t\t\t\tthis.findSourceInfoForPath(skill.filePath, this.extensionSkillSourceInfos, metadataByPath) ??\n\t\t\t\tskill.sourceInfo ??\n\t\t\t\tthis.getDefaultSourceInfoForPath(skill.filePath),\n\t\t}));\n\t\tthis.skillDiagnostics = resolvedSkills.diagnostics;\n\t}\n\n\tprivate updatePromptsFromPaths(promptPaths: string[], metadataByPath?: Map<string, PathMetadata>): void {\n\t\tlet promptsResult: { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] };\n\t\tif (this.noPromptTemplates && promptPaths.length === 0) {\n\t\t\tpromptsResult = { prompts: [], diagnostics: [] };\n\t\t} else {\n\t\t\tconst allPrompts = loadPromptTemplates({\n\t\t\t\tcwd: this.cwd,\n\t\t\t\tagentDir: this.agentDir,\n\t\t\t\tpromptPaths,\n\t\t\t\tincludeDefaults: false,\n\t\t\t});\n\t\t\tpromptsResult = this.dedupePrompts(allPrompts);\n\t\t}\n\t\tconst resolvedPrompts = this.promptsOverride ? this.promptsOverride(promptsResult) : promptsResult;\n\t\tthis.prompts = resolvedPrompts.prompts.map((prompt) => ({\n\t\t\t...prompt,\n\t\t\tsourceInfo:\n\t\t\t\tthis.findSourceInfoForPath(prompt.filePath, this.extensionPromptSourceInfos, metadataByPath) ??\n\t\t\t\tprompt.sourceInfo ??\n\t\t\t\tthis.getDefaultSourceInfoForPath(prompt.filePath),\n\t\t}));\n\t\tthis.promptDiagnostics = resolvedPrompts.diagnostics;\n\t}\n\n\tprivate updateThemesFromPaths(themePaths: string[], metadataByPath?: Map<string, PathMetadata>): void {\n\t\tlet themesResult: { themes: Theme[]; diagnostics: ResourceDiagnostic[] };\n\t\tif (this.noThemes && themePaths.length === 0) {\n\t\t\tthemesResult = { themes: [], diagnostics: [] };\n\t\t} else {\n\t\t\tconst loaded = this.loadThemes(themePaths, false);\n\t\t\tconst deduped = this.dedupeThemes(loaded.themes);\n\t\t\tthemesResult = { themes: deduped.themes, diagnostics: [...loaded.diagnostics, ...deduped.diagnostics] };\n\t\t}\n\t\tconst resolvedThemes = this.themesOverride ? this.themesOverride(themesResult) : themesResult;\n\t\tthis.themes = resolvedThemes.themes.map((theme) => {\n\t\t\tconst sourcePath = theme.sourcePath;\n\t\t\ttheme.sourceInfo = sourcePath\n\t\t\t\t? (this.findSourceInfoForPath(sourcePath, this.extensionThemeSourceInfos, metadataByPath) ??\n\t\t\t\t\ttheme.sourceInfo ??\n\t\t\t\t\tthis.getDefaultSourceInfoForPath(sourcePath))\n\t\t\t\t: theme.sourceInfo;\n\t\t\treturn theme;\n\t\t});\n\t\tthis.themeDiagnostics = resolvedThemes.diagnostics;\n\t}\n\n\tprivate applyExtensionSourceInfo(extensions: Extension[], metadataByPath: Map<string, PathMetadata>): void {\n\t\tfor (const extension of extensions) {\n\t\t\textension.sourceInfo =\n\t\t\t\tthis.findSourceInfoForPath(extension.path, undefined, metadataByPath) ??\n\t\t\t\tthis.getDefaultSourceInfoForPath(extension.path);\n\t\t\tfor (const command of extension.commands.values()) {\n\t\t\t\tcommand.sourceInfo = extension.sourceInfo;\n\t\t\t}\n\t\t\tfor (const tool of extension.tools.values()) {\n\t\t\t\ttool.sourceInfo = extension.sourceInfo;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate findSourceInfoForPath(\n\t\tresourcePath: string,\n\t\textraSourceInfos?: Map<string, SourceInfo>,\n\t\tmetadataByPath?: Map<string, PathMetadata>,\n\t): SourceInfo | undefined {\n\t\tif (!resourcePath) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif (resourcePath.startsWith(\"<\")) {\n\t\t\treturn this.getDefaultSourceInfoForPath(resourcePath);\n\t\t}\n\n\t\tconst normalizedResourcePath = resolve(resourcePath);\n\t\tif (extraSourceInfos) {\n\t\t\tfor (const [sourcePath, sourceInfo] of extraSourceInfos.entries()) {\n\t\t\t\tconst normalizedSourcePath = resolve(sourcePath);\n\t\t\t\tif (\n\t\t\t\t\tnormalizedResourcePath === normalizedSourcePath ||\n\t\t\t\t\tnormalizedResourcePath.startsWith(`${normalizedSourcePath}${sep}`)\n\t\t\t\t) {\n\t\t\t\t\treturn { ...sourceInfo, path: resourcePath };\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (metadataByPath) {\n\t\t\tconst exact = metadataByPath.get(normalizedResourcePath) ?? metadataByPath.get(resourcePath);\n\t\t\tif (exact) {\n\t\t\t\treturn createSourceInfo(resourcePath, exact);\n\t\t\t}\n\n\t\t\tfor (const [sourcePath, metadata] of metadataByPath.entries()) {\n\t\t\t\tconst normalizedSourcePath = resolve(sourcePath);\n\t\t\t\tif (\n\t\t\t\t\tnormalizedResourcePath === normalizedSourcePath ||\n\t\t\t\t\tnormalizedResourcePath.startsWith(`${normalizedSourcePath}${sep}`)\n\t\t\t\t) {\n\t\t\t\t\treturn createSourceInfo(resourcePath, metadata);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tprivate getDefaultSourceInfoForPath(filePath: string): SourceInfo {\n\t\tif (filePath.startsWith(\"<\") && filePath.endsWith(\">\")) {\n\t\t\treturn {\n\t\t\t\tpath: filePath,\n\t\t\t\tsource: filePath.slice(1, -1).split(\":\")[0] || \"temporary\",\n\t\t\t\tscope: \"temporary\",\n\t\t\t\torigin: \"top-level\",\n\t\t\t};\n\t\t}\n\n\t\tconst normalizedPath = resolve(filePath);\n\t\tconst agentRoots = [\n\t\t\tjoin(this.agentDir, \"skills\"),\n\t\t\tjoin(this.agentDir, \"prompts\"),\n\t\t\tjoin(this.agentDir, \"themes\"),\n\t\t\tjoin(this.agentDir, \"extensions\"),\n\t\t];\n\t\tconst projectRoots = [\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"skills\"),\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"prompts\"),\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"themes\"),\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"extensions\"),\n\t\t];\n\n\t\tfor (const root of agentRoots) {\n\t\t\tif (this.isUnderPath(normalizedPath, root)) {\n\t\t\t\treturn { path: filePath, source: \"local\", scope: \"user\", origin: \"top-level\", baseDir: root };\n\t\t\t}\n\t\t}\n\n\t\tfor (const root of projectRoots) {\n\t\t\tif (this.isUnderPath(normalizedPath, root)) {\n\t\t\t\treturn { path: filePath, source: \"local\", scope: \"project\", origin: \"top-level\", baseDir: root };\n\t\t\t}\n\t\t}\n\n\t\tlet baseDir: string;\n\t\ttry {\n\t\t\tbaseDir = statSync(normalizedPath).isDirectory() ? normalizedPath : resolve(normalizedPath, \"..\");\n\t\t} catch {\n\t\t\t// Path doesn't exist (deleted file, broken symlink, race condition) — fall back to parent dir\n\t\t\tbaseDir = resolve(normalizedPath, \"..\");\n\t\t}\n\t\treturn {\n\t\t\tpath: filePath,\n\t\t\tsource: \"local\",\n\t\t\tscope: \"temporary\",\n\t\t\torigin: \"top-level\",\n\t\t\tbaseDir,\n\t\t};\n\t}\n\n\tprivate mergePaths(primary: string[], additional: string[]): string[] {\n\t\tconst merged: string[] = [];\n\t\tconst seen = new Set<string>();\n\n\t\tfor (const p of [...primary, ...additional]) {\n\t\t\tconst resolved = this.resolveResourcePath(p);\n\t\t\tif (seen.has(resolved)) continue;\n\t\t\tseen.add(resolved);\n\t\t\tmerged.push(resolved);\n\t\t}\n\n\t\treturn merged;\n\t}\n\n\tprivate resolveResourcePath(p: string): string {\n\t\tconst trimmed = p.trim();\n\t\tlet expanded = trimmed;\n\t\tif (trimmed === \"~\") {\n\t\t\texpanded = homedir();\n\t\t} else if (trimmed.startsWith(\"~/\")) {\n\t\t\texpanded = join(homedir(), trimmed.slice(2));\n\t\t} else if (trimmed.startsWith(\"~\")) {\n\t\t\texpanded = join(homedir(), trimmed.slice(1));\n\t\t}\n\t\treturn resolve(this.cwd, expanded);\n\t}\n\n\tprivate loadThemes(\n\t\tpaths: string[],\n\t\tincludeDefaults: boolean = true,\n\t): {\n\t\tthemes: Theme[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t} {\n\t\tconst themes: Theme[] = [];\n\t\tconst diagnostics: ResourceDiagnostic[] = [];\n\t\tif (includeDefaults) {\n\t\t\tconst defaultDirs = [join(this.agentDir, \"themes\"), join(this.cwd, CONFIG_DIR_NAME, \"themes\")];\n\n\t\t\tfor (const dir of defaultDirs) {\n\t\t\t\tthis.loadThemesFromDir(dir, themes, diagnostics);\n\t\t\t}\n\t\t}\n\n\t\tfor (const p of paths) {\n\t\t\tconst resolved = resolve(this.cwd, p);\n\t\t\tif (!existsSync(resolved)) {\n\t\t\t\tdiagnostics.push({ type: \"warning\", message: \"theme path does not exist\", path: resolved });\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst stats = statSync(resolved);\n\t\t\t\tif (stats.isDirectory()) {\n\t\t\t\t\tthis.loadThemesFromDir(resolved, themes, diagnostics);\n\t\t\t\t} else if (stats.isFile() && resolved.endsWith(\".json\")) {\n\t\t\t\t\tthis.loadThemeFromFile(resolved, themes, diagnostics);\n\t\t\t\t} else {\n\t\t\t\t\tdiagnostics.push({ type: \"warning\", message: \"theme path is not a json file\", path: resolved });\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconst message = error instanceof Error ? error.message : \"failed to read theme path\";\n\t\t\t\tdiagnostics.push({ type: \"warning\", message, path: resolved });\n\t\t\t}\n\t\t}\n\n\t\treturn { themes, diagnostics };\n\t}\n\n\tprivate loadThemesFromDir(dir: string, themes: Theme[], diagnostics: ResourceDiagnostic[]): void {\n\t\tif (!existsSync(dir)) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst entries = readdirSync(dir, { withFileTypes: true });\n\t\t\tfor (const entry of entries) {\n\t\t\t\tlet isFile = entry.isFile();\n\t\t\t\tif (entry.isSymbolicLink()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tisFile = statSync(join(dir, entry.name)).isFile();\n\t\t\t\t\t} catch {\n\t\t\t\t\t\t// Broken symlink — skip entry\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!isFile) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (!entry.name.endsWith(\".json\")) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tthis.loadThemeFromFile(join(dir, entry.name), themes, diagnostics);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : \"failed to read theme directory\";\n\t\t\tdiagnostics.push({ type: \"warning\", message, path: dir });\n\t\t}\n\t}\n\n\tprivate loadThemeFromFile(filePath: string, themes: Theme[], diagnostics: ResourceDiagnostic[]): void {\n\t\ttry {\n\t\t\tthemes.push(loadThemeFromPath(filePath));\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : \"failed to load theme\";\n\t\t\tdiagnostics.push({ type: \"warning\", message, path: filePath });\n\t\t}\n\t}\n\n\tprivate async loadExtensionFactories(runtime: ExtensionRuntime): Promise<{\n\t\textensions: Extension[];\n\t\terrors: Array<{ path: string; error: string }>;\n\t}> {\n\t\tconst extensions: Extension[] = [];\n\t\tconst errors: Array<{ path: string; error: string }> = [];\n\n\t\tfor (const [index, factory] of this.extensionFactories.entries()) {\n\t\t\tconst extensionPath = `<inline:${index + 1}>`;\n\t\t\ttry {\n\t\t\t\tconst extension = await loadExtensionFromFactory(factory, this.cwd, this.eventBus, runtime, extensionPath);\n\t\t\t\textensions.push(extension);\n\t\t\t} catch (error) {\n\t\t\t\tconst message = error instanceof Error ? error.message : \"failed to load extension\";\n\t\t\t\terrors.push({ path: extensionPath, error: message });\n\t\t\t}\n\t\t}\n\n\t\treturn { extensions, errors };\n\t}\n\n\tprivate dedupePrompts(prompts: PromptTemplate[]): { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] } {\n\t\tconst seen = new Map<string, PromptTemplate>();\n\t\tconst diagnostics: ResourceDiagnostic[] = [];\n\n\t\tfor (const prompt of prompts) {\n\t\t\tconst existing = seen.get(prompt.name);\n\t\t\tif (existing) {\n\t\t\t\tdiagnostics.push({\n\t\t\t\t\ttype: \"collision\",\n\t\t\t\t\tmessage: `name \"/${prompt.name}\" collision`,\n\t\t\t\t\tpath: prompt.filePath,\n\t\t\t\t\tcollision: {\n\t\t\t\t\t\tresourceType: \"prompt\",\n\t\t\t\t\t\tname: prompt.name,\n\t\t\t\t\t\twinnerPath: existing.filePath,\n\t\t\t\t\t\tloserPath: prompt.filePath,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tseen.set(prompt.name, prompt);\n\t\t\t}\n\t\t}\n\n\t\treturn { prompts: Array.from(seen.values()), diagnostics };\n\t}\n\n\tprivate dedupeThemes(themes: Theme[]): { themes: Theme[]; diagnostics: ResourceDiagnostic[] } {\n\t\tconst seen = new Map<string, Theme>();\n\t\tconst diagnostics: ResourceDiagnostic[] = [];\n\n\t\tfor (const t of themes) {\n\t\t\tconst name = t.name ?? \"unnamed\";\n\t\t\tconst existing = seen.get(name);\n\t\t\tif (existing) {\n\t\t\t\tdiagnostics.push({\n\t\t\t\t\ttype: \"collision\",\n\t\t\t\t\tmessage: `name \"${name}\" collision`,\n\t\t\t\t\tpath: t.sourcePath,\n\t\t\t\t\tcollision: {\n\t\t\t\t\t\tresourceType: \"theme\",\n\t\t\t\t\t\tname,\n\t\t\t\t\t\twinnerPath: existing.sourcePath ?? \"<builtin>\",\n\t\t\t\t\t\tloserPath: t.sourcePath ?? \"<builtin>\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tseen.set(name, t);\n\t\t\t}\n\t\t}\n\n\t\treturn { themes: Array.from(seen.values()), diagnostics };\n\t}\n\n\tprivate discoverSystemPromptFile(): string | undefined {\n\t\tconst projectPath = join(this.cwd, CONFIG_DIR_NAME, \"SYSTEM.md\");\n\t\tif (existsSync(projectPath)) {\n\t\t\treturn projectPath;\n\t\t}\n\n\t\tconst globalPath = join(this.agentDir, \"SYSTEM.md\");\n\t\tif (existsSync(globalPath)) {\n\t\t\treturn globalPath;\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tprivate discoverAppendSystemPromptFile(): string | undefined {\n\t\tconst projectPath = join(this.cwd, CONFIG_DIR_NAME, \"APPEND_SYSTEM.md\");\n\t\tif (existsSync(projectPath)) {\n\t\t\treturn projectPath;\n\t\t}\n\n\t\tconst globalPath = join(this.agentDir, \"APPEND_SYSTEM.md\");\n\t\tif (existsSync(globalPath)) {\n\t\t\treturn globalPath;\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tprivate isUnderPath(target: string, root: string): boolean {\n\t\tconst normalizedRoot = resolve(root);\n\t\tif (target === normalizedRoot) {\n\t\t\treturn true;\n\t\t}\n\t\tconst prefix = normalizedRoot.endsWith(sep) ? normalizedRoot : `${normalizedRoot}${sep}`;\n\t\treturn target.startsWith(prefix);\n\t}\n\n\tprivate detectExtensionConflicts(extensions: Extension[]): Array<{ path: string; message: string }> {\n\t\tconst conflicts: Array<{ path: string; message: string }> = [];\n\n\t\t// Track which extension registered each tool and flag\n\t\tconst toolOwners = new Map<string, string>();\n\t\tconst flagOwners = new Map<string, string>();\n\n\t\tfor (const ext of extensions) {\n\t\t\t// Check tools\n\t\t\tfor (const toolName of ext.tools.keys()) {\n\t\t\t\tconst existingOwner = toolOwners.get(toolName);\n\t\t\t\tif (existingOwner && existingOwner !== ext.path) {\n\t\t\t\t\tconflicts.push({\n\t\t\t\t\t\tpath: ext.path,\n\t\t\t\t\t\tmessage: `Tool \"${toolName}\" conflicts with ${existingOwner}`,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\ttoolOwners.set(toolName, ext.path);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check flags\n\t\t\tfor (const flagName of ext.flags.keys()) {\n\t\t\t\tconst existingOwner = flagOwners.get(flagName);\n\t\t\t\tif (existingOwner && existingOwner !== ext.path) {\n\t\t\t\t\tconflicts.push({\n\t\t\t\t\t\tpath: ext.path,\n\t\t\t\t\t\tmessage: `Flag \"--${flagName}\" conflicts with ${existingOwner}`,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tflagOwners.set(flagName, ext.path);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn conflicts;\n\t}\n}\n"]}
1
+ {"version":3,"file":"resource-loader.d.ts","sourceRoot":"","sources":["../../src/core/resource-loader.ts"],"names":[],"mappings":"AAIA,OAAO,EAAqB,KAAK,KAAK,EAAE,MAAM,qCAAqC,CAAC;AACpF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAG3D,YAAY,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE9E,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/D,OAAO,KAAK,EAAa,gBAAgB,EAAoB,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACjH,OAAO,EAAyB,KAAK,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAIzC,MAAM,WAAW,sBAAsB;IACtC,UAAU,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,YAAY,CAAA;KAAE,CAAC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IAa3B,OAAO,CAAC,UAAU;IA0ClB,OAAO,CAAC,iBAAiB;IA+BzB,OAAO,CAAC,iBAAiB;YASX,sBAAsB;IAqBpC,OAAO,CAAC,aAAa;IA0BrB,OAAO,CAAC,YAAY;IA2BpB,OAAO,CAAC,wBAAwB;IAchC,OAAO,CAAC,8BAA8B;IActC,OAAO,CAAC,WAAW;IASnB,OAAO,CAAC,wBAAwB;CAqChC","sourcesContent":["import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, statSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join, resolve, sep } from \"node:path\";\nimport { CONFIG_DIR_NAME, getAgentDir } from \"../config.js\";\nimport { loadThemeFromPath, type Theme } from \"../modes/interactive/theme/theme.js\";\nimport type { ResourceDiagnostic } from \"./diagnostics.js\";\nimport { findGitRoot } from \"./git-root.js\";\n\nexport type { ResourceCollision, ResourceDiagnostic } from \"./diagnostics.js\";\n\nimport { createEventBus, type EventBus } from \"./event-bus.js\";\nimport { createExtensionRuntime, loadExtensionFromFactory, loadExtensions } from \"./extensions/loader.js\";\nimport type { Extension, ExtensionFactory, ExtensionRuntime, LoadExtensionsResult } from \"./extensions/types.js\";\nimport { DefaultPackageManager, type PathMetadata } from \"./package-manager.js\";\nimport type { PromptTemplate } from \"./prompt-templates.js\";\nimport { loadPromptTemplates } from \"./prompt-templates.js\";\nimport { SettingsManager } from \"./settings-manager.js\";\nimport type { Skill } from \"./skills.js\";\nimport { loadSkills } from \"./skills.js\";\nimport { createSourceInfo, type SourceInfo } from \"./source-info.js\";\n\nexport interface ResourceExtensionPaths {\n\tskillPaths?: Array<{ path: string; metadata: PathMetadata }>;\n\tpromptPaths?: Array<{ path: string; metadata: PathMetadata }>;\n\tthemePaths?: Array<{ path: string; metadata: PathMetadata }>;\n}\n\nexport interface MemorySource {\n\treadonly content: string;\n\treadonly dir: string;\n\treadonly source: \"dreb\" | \"claude\";\n}\n\nexport interface MemoryIndexes {\n\treadonly global: readonly MemorySource[];\n\treadonly project: readonly MemorySource[];\n\treadonly globalMemoryDir: string;\n\treadonly projectMemoryDir: string;\n\t/** ISO timestamp from .dream-last-run, or null if file is missing/invalid. */\n\treadonly dreamLastRun: string | null;\n}\n\nexport interface ResourceLoader {\n\tgetExtensions(): LoadExtensionsResult;\n\tgetSkills(): { skills: Skill[]; diagnostics: ResourceDiagnostic[] };\n\tgetPrompts(): { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] };\n\tgetThemes(): { themes: Theme[]; diagnostics: ResourceDiagnostic[] };\n\tgetContextDiagnostics(): ResourceDiagnostic[];\n\tgetAgentsFiles(): { agentsFiles: Array<{ path: string; content: string }> };\n\tgetMemoryIndexes(): MemoryIndexes;\n\t/** Re-read the .dream-last-run timestamp from disk without a full reload. */\n\trefreshDreamLastRun(): void;\n\tgetSystemPrompt(): string | undefined;\n\tgetAppendSystemPrompt(): string[];\n\textendResources(paths: ResourceExtensionPaths): void;\n\treload(): Promise<void>;\n}\n\nfunction resolvePromptInput(\n\tinput: string | undefined,\n\tdescription: string,\n\tdiagnostics?: ResourceDiagnostic[],\n): string | undefined {\n\tif (!input) {\n\t\treturn undefined;\n\t}\n\n\tif (existsSync(input)) {\n\t\ttry {\n\t\t\treturn readFileSync(input, \"utf-8\");\n\t\t} catch (error) {\n\t\t\tdiagnostics?.push({\n\t\t\t\ttype: \"warning\",\n\t\t\t\tmessage: `Could not read ${description} file ${input}: ${error}`,\n\t\t\t\tpath: input,\n\t\t\t});\n\t\t\treturn input;\n\t\t}\n\t}\n\n\treturn input;\n}\n\nfunction stripHtmlComments(content: string): string {\n\treturn content.replace(/<!--[\\s\\S]*?-->/g, \"\");\n}\n\nexport const CONTEXT_FILE_CANDIDATES = [\n\t\"AGENTS.md\",\n\t\"AGENTS.MD\",\n\t\"CLAUDE.md\",\n\t\"CLAUDE.MD\",\n\tjoin(\".claude\", \"CLAUDE.md\"),\n\tjoin(\".claude\", \"CLAUDE.MD\"),\n\tjoin(\".dreb\", \"CONTEXT.md\"),\n\tjoin(\".dreb\", \"CONTEXT.MD\"),\n] as const;\n\nexport function loadContextFilesFromDir(\n\tdir: string,\n\tdiagnostics?: ResourceDiagnostic[],\n): Array<{ path: string; content: string }> {\n\tconst results: Array<{ path: string; content: string }> = [];\n\tconst seenRealPaths = new Set<string>();\n\tfor (const filename of CONTEXT_FILE_CANDIDATES) {\n\t\tconst filePath = join(dir, filename);\n\t\tif (existsSync(filePath)) {\n\t\t\t// Deduplicate by realpath to avoid loading the same file twice on\n\t\t\t// case-insensitive filesystems (macOS) where AGENTS.md and AGENTS.MD\n\t\t\t// resolve to the same inode.\n\t\t\tlet realPath: string;\n\t\t\ttry {\n\t\t\t\trealPath = realpathSync(filePath);\n\t\t\t} catch {\n\t\t\t\trealPath = filePath;\n\t\t\t}\n\t\t\tif (seenRealPaths.has(realPath)) continue;\n\t\t\tseenRealPaths.add(realPath);\n\t\t\ttry {\n\t\t\t\tresults.push({\n\t\t\t\t\tpath: filePath,\n\t\t\t\t\tcontent: stripHtmlComments(readFileSync(filePath, \"utf-8\")),\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read ${filePath}: ${error}`, path: filePath });\n\t\t\t}\n\t\t}\n\t}\n\treturn results;\n}\n\nfunction loadRulesFromDir(\n\tdir: string,\n\tcontextFiles: Array<{ path: string; content: string }>,\n\tseenPaths: Set<string>,\n\tdepth: number = 0,\n\tdiagnostics?: ResourceDiagnostic[],\n): void {\n\tif (depth > 10) return;\n\tlet entries: import(\"node:fs\").Dirent[];\n\ttry {\n\t\tentries = readdirSync(dir, { withFileTypes: true });\n\t} catch (error) {\n\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read rules directory ${dir}: ${error}`, path: dir });\n\t\treturn;\n\t}\n\tfor (const entry of entries) {\n\t\tconst fullPath = join(dir, entry.name);\n\t\tif (entry.isDirectory() && !entry.isSymbolicLink()) {\n\t\t\tloadRulesFromDir(fullPath, contextFiles, seenPaths, depth + 1, diagnostics);\n\t\t} else if (entry.isFile() && entry.name.endsWith(\".md\") && !seenPaths.has(fullPath)) {\n\t\t\ttry {\n\t\t\t\tconst content = readFileSync(fullPath, \"utf-8\");\n\t\t\t\t// Skip path-scoped rules (paths: in frontmatter) — not yet implemented, just skip\n\t\t\t\tif (content.startsWith(\"---\")) {\n\t\t\t\t\tconst endIdx = content.indexOf(\"---\", 3);\n\t\t\t\t\tif (endIdx !== -1) {\n\t\t\t\t\t\tconst frontmatter = content.slice(3, endIdx);\n\t\t\t\t\t\tif (/^\\s*paths\\s*:/m.test(frontmatter)) {\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tseenPaths.add(fullPath);\n\t\t\t\tcontextFiles.push({ path: fullPath, content: stripHtmlComments(content) });\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({\n\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\tmessage: `Could not read rule ${fullPath}: ${error}`,\n\t\t\t\t\tpath: fullPath,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunction loadProjectContextFiles(\n\toptions: { cwd?: string; agentDir?: string } = {},\n\tdiagnostics?: ResourceDiagnostic[],\n): Array<{ path: string; content: string }> {\n\tconst resolvedCwd = options.cwd ?? process.cwd();\n\tconst resolvedAgentDir = options.agentDir ?? getAgentDir();\n\n\tconst contextFiles: Array<{ path: string; content: string }> = [];\n\tconst seenPaths = new Set<string>();\n\n\t// 1. User-level context files (global, loaded first — project files follow and can override)\n\tconst userLevelPaths = [\n\t\tjoin(resolvedAgentDir, \"AGENTS.md\"),\n\t\tjoin(resolvedAgentDir, \"CLAUDE.md\"),\n\t\tjoin(homedir(), \".claude\", \"CLAUDE.md\"),\n\t\tjoin(homedir(), \".dreb\", \"CONTEXT.md\"),\n\t];\n\tfor (const filePath of userLevelPaths) {\n\t\tif (existsSync(filePath)) {\n\t\t\ttry {\n\t\t\t\tconst content = stripHtmlComments(readFileSync(filePath, \"utf-8\"));\n\t\t\t\tif (!seenPaths.has(filePath)) {\n\t\t\t\t\tseenPaths.add(filePath);\n\t\t\t\t\tcontextFiles.push({ path: filePath, content });\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read ${filePath}: ${error}`, path: filePath });\n\t\t\t}\n\t\t}\n\t}\n\n\t// 2. Walk upward from cwd to root, collecting all context files per directory (ordered root-first via unshift)\n\tconst ancestorContextFiles: Array<{ path: string; content: string }> = [];\n\tlet currentDir = resolvedCwd;\n\tconst root = resolve(\"/\");\n\n\twhile (true) {\n\t\tconst dirFiles = loadContextFilesFromDir(currentDir, diagnostics);\n\t\tconst newFiles = dirFiles.filter((f) => !seenPaths.has(f.path));\n\t\tfor (const file of newFiles) {\n\t\t\tseenPaths.add(file.path);\n\t\t}\n\t\tancestorContextFiles.unshift(...newFiles);\n\n\t\tif (currentDir === root) break;\n\t\tconst parentDir = resolve(currentDir, \"..\");\n\t\tif (parentDir === currentDir) break;\n\t\tcurrentDir = parentDir;\n\t}\n\n\tcontextFiles.push(...ancestorContextFiles);\n\n\t// 3. Rules directories (unconditional rules loaded now)\n\tconst rulesSearchDirs = [\n\t\tjoin(resolvedCwd, \".dreb\", \"rules\"),\n\t\tjoin(resolvedCwd, \".claude\", \"rules\"),\n\t\tjoin(resolvedCwd, CONFIG_DIR_NAME, \"rules\"),\n\t];\n\tfor (const rulesDir of rulesSearchDirs) {\n\t\tif (existsSync(rulesDir)) {\n\t\t\ttry {\n\t\t\t\tloadRulesFromDir(rulesDir, contextFiles, seenPaths, 0, diagnostics);\n\t\t\t} catch (error) {\n\t\t\t\tdiagnostics?.push({\n\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\tmessage: `Could not read rules from ${rulesDir}: ${error}`,\n\t\t\t\t\tpath: rulesDir,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\treturn contextFiles;\n}\n\nconst MEMORY_INDEX_MAX_LINES = 200;\n\n/**\n * Encode an absolute POSIX path the way Claude Code does for ~/.claude/projects/ directories.\n * Replaces path separators and underscores with hyphens.\n * /home/drew/projects/deep_yellow -> -home-drew-projects-deep-yellow\n */\nexport function encodeClaudeProjectPath(absolutePath: string): string {\n\treturn absolutePath.replace(/[/_]/g, \"-\");\n}\n\nfunction readMemoryIndex(dir: string, diagnostics?: ResourceDiagnostic[]): string | undefined {\n\tconst indexPath = join(dir, \"MEMORY.md\");\n\ttry {\n\t\tif (!existsSync(indexPath)) return undefined;\n\t\tconst content = readFileSync(indexPath, \"utf-8\");\n\t\tconst lines = content.split(\"\\n\");\n\t\tif (lines.length > MEMORY_INDEX_MAX_LINES) {\n\t\t\tdiagnostics?.push({\n\t\t\t\ttype: \"warning\",\n\t\t\t\tmessage: `${indexPath} has ${lines.length} lines, truncated to ${MEMORY_INDEX_MAX_LINES}. Consider cleaning up older entries.`,\n\t\t\t\tpath: indexPath,\n\t\t\t});\n\t\t\treturn lines.slice(0, MEMORY_INDEX_MAX_LINES).join(\"\\n\");\n\t\t}\n\t\treturn content;\n\t} catch (error) {\n\t\tdiagnostics?.push({ type: \"warning\", message: `Could not read ${indexPath}: ${error}`, path: indexPath });\n\t\treturn undefined;\n\t}\n}\n\nfunction loadMemoryIndexes(\n\toptions: { cwd?: string; agentDir?: string },\n\tdiagnostics?: ResourceDiagnostic[],\n): MemoryIndexes {\n\tconst resolvedCwd = options.cwd ?? process.cwd();\n\tconst projectRoot = findGitRoot(resolvedCwd) ?? resolvedCwd;\n\tconst drebRoot = options.agentDir ? resolve(options.agentDir, \"..\") : join(homedir(), \".dreb\");\n\n\t// Primary write targets for dreb\n\tconst globalMemoryDir = join(drebRoot, \"memory\");\n\tconst projectMemoryDir = join(projectRoot, \".dreb\", \"memory\");\n\n\t// Claude Code memory paths (read-only — dreb reads these but writes to .dreb/)\n\tconst claudeGlobalMemoryDir = join(homedir(), \".claude\", \"projects\", encodeClaudeProjectPath(homedir()), \"memory\");\n\tconst claudeProjectMemoryDir = join(\n\t\thomedir(),\n\t\t\".claude\",\n\t\t\"projects\",\n\t\tencodeClaudeProjectPath(projectRoot),\n\t\t\"memory\",\n\t);\n\n\tconst globalSources: MemorySource[] = [];\n\tconst projectSources: MemorySource[] = [];\n\n\t// Load dreb memory (primary)\n\tconst drebGlobal = readMemoryIndex(globalMemoryDir, diagnostics);\n\tif (drebGlobal) {\n\t\tglobalSources.push({ content: drebGlobal, dir: globalMemoryDir, source: \"dreb\" });\n\t}\n\n\tconst drebProject =\n\t\tglobalMemoryDir !== projectMemoryDir ? readMemoryIndex(projectMemoryDir, diagnostics) : undefined;\n\tif (drebProject) {\n\t\tprojectSources.push({ content: drebProject, dir: projectMemoryDir, source: \"dreb\" });\n\t}\n\n\t// Load Claude Code memory (read-only, compat)\n\tconst claudeGlobal = readMemoryIndex(claudeGlobalMemoryDir, diagnostics);\n\tif (claudeGlobal) {\n\t\tglobalSources.push({ content: claudeGlobal, dir: claudeGlobalMemoryDir, source: \"claude\" });\n\t}\n\n\t// Only load Claude project memory if it's a different path than Claude global\n\tif (claudeProjectMemoryDir !== claudeGlobalMemoryDir) {\n\t\tconst claudeProject = readMemoryIndex(claudeProjectMemoryDir, diagnostics);\n\t\tif (claudeProject) {\n\t\t\tprojectSources.push({ content: claudeProject, dir: claudeProjectMemoryDir, source: \"claude\" });\n\t\t}\n\t}\n\n\t// Read dream last-run timestamp (graceful null on missing/invalid)\n\tlet dreamLastRun: string | null = null;\n\ttry {\n\t\tconst dreamLastRunPath = join(globalMemoryDir, \".dream-last-run\");\n\t\tif (existsSync(dreamLastRunPath)) {\n\t\t\tconst raw = readFileSync(dreamLastRunPath, \"utf-8\").trim();\n\t\t\t// Validate it parses as a date\n\t\t\tif (raw && !Number.isNaN(Date.parse(raw))) {\n\t\t\t\tdreamLastRun = raw;\n\t\t\t}\n\t\t}\n\t} catch {\n\t\t// Silently ignore — missing/unreadable file is fine\n\t}\n\n\treturn {\n\t\tglobal: globalSources,\n\t\tproject: projectSources,\n\t\tglobalMemoryDir,\n\t\tprojectMemoryDir,\n\t\tdreamLastRun,\n\t};\n}\n\nexport interface DefaultResourceLoaderOptions {\n\tcwd?: string;\n\tagentDir?: string;\n\tsettingsManager?: SettingsManager;\n\teventBus?: EventBus;\n\tadditionalExtensionPaths?: string[];\n\tadditionalSkillPaths?: string[];\n\tadditionalPromptTemplatePaths?: string[];\n\tadditionalThemePaths?: string[];\n\textensionFactories?: ExtensionFactory[];\n\tnoExtensions?: boolean;\n\tnoSkills?: boolean;\n\tnoPromptTemplates?: boolean;\n\tnoThemes?: boolean;\n\tsystemPrompt?: string;\n\tappendSystemPrompt?: string;\n\textensionsOverride?: (base: LoadExtensionsResult) => LoadExtensionsResult;\n\tskillsOverride?: (base: { skills: Skill[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tskills: Skill[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tpromptsOverride?: (base: { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tprompts: PromptTemplate[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tthemesOverride?: (base: { themes: Theme[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tthemes: Theme[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tagentsFilesOverride?: (base: { agentsFiles: Array<{ path: string; content: string }> }) => {\n\t\tagentsFiles: Array<{ path: string; content: string }>;\n\t};\n\tsystemPromptOverride?: (base: string | undefined) => string | undefined;\n\tappendSystemPromptOverride?: (base: string[]) => string[];\n}\n\nexport class DefaultResourceLoader implements ResourceLoader {\n\tprivate cwd: string;\n\tprivate agentDir: string;\n\tprivate settingsManager: SettingsManager;\n\tprivate eventBus: EventBus;\n\tprivate packageManager: DefaultPackageManager;\n\tprivate additionalExtensionPaths: string[];\n\tprivate additionalSkillPaths: string[];\n\tprivate additionalPromptTemplatePaths: string[];\n\tprivate additionalThemePaths: string[];\n\tprivate extensionFactories: ExtensionFactory[];\n\tprivate noExtensions: boolean;\n\tprivate noSkills: boolean;\n\tprivate noPromptTemplates: boolean;\n\tprivate noThemes: boolean;\n\tprivate systemPromptSource?: string;\n\tprivate appendSystemPromptSource?: string;\n\tprivate extensionsOverride?: (base: LoadExtensionsResult) => LoadExtensionsResult;\n\tprivate skillsOverride?: (base: { skills: Skill[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tskills: Skill[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tprivate promptsOverride?: (base: { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tprompts: PromptTemplate[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tprivate themesOverride?: (base: { themes: Theme[]; diagnostics: ResourceDiagnostic[] }) => {\n\t\tthemes: Theme[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t};\n\tprivate agentsFilesOverride?: (base: { agentsFiles: Array<{ path: string; content: string }> }) => {\n\t\tagentsFiles: Array<{ path: string; content: string }>;\n\t};\n\tprivate systemPromptOverride?: (base: string | undefined) => string | undefined;\n\tprivate appendSystemPromptOverride?: (base: string[]) => string[];\n\n\tprivate extensionsResult: LoadExtensionsResult;\n\tprivate skills: Skill[];\n\tprivate skillDiagnostics: ResourceDiagnostic[];\n\tprivate prompts: PromptTemplate[];\n\tprivate promptDiagnostics: ResourceDiagnostic[];\n\tprivate themes: Theme[];\n\tprivate themeDiagnostics: ResourceDiagnostic[];\n\tprivate agentsFiles: Array<{ path: string; content: string }>;\n\tprivate memoryIndexes: MemoryIndexes;\n\tprivate systemPrompt?: string;\n\tprivate appendSystemPrompt: string[];\n\tprivate contextDiagnostics: ResourceDiagnostic[];\n\tprivate lastSkillPaths: string[];\n\tprivate extensionSkillSourceInfos: Map<string, SourceInfo>;\n\tprivate extensionPromptSourceInfos: Map<string, SourceInfo>;\n\tprivate extensionThemeSourceInfos: Map<string, SourceInfo>;\n\tprivate lastPromptPaths: string[];\n\tprivate lastThemePaths: string[];\n\n\tconstructor(options: DefaultResourceLoaderOptions) {\n\t\tthis.cwd = options.cwd ?? process.cwd();\n\t\tthis.agentDir = options.agentDir ?? getAgentDir();\n\t\tthis.settingsManager = options.settingsManager ?? SettingsManager.create(this.cwd, this.agentDir);\n\t\tthis.eventBus = options.eventBus ?? createEventBus();\n\t\tthis.packageManager = new DefaultPackageManager({\n\t\t\tcwd: this.cwd,\n\t\t\tagentDir: this.agentDir,\n\t\t\tsettingsManager: this.settingsManager,\n\t\t});\n\t\tthis.additionalExtensionPaths = options.additionalExtensionPaths ?? [];\n\t\tthis.additionalSkillPaths = options.additionalSkillPaths ?? [];\n\t\tthis.additionalPromptTemplatePaths = options.additionalPromptTemplatePaths ?? [];\n\t\tthis.additionalThemePaths = options.additionalThemePaths ?? [];\n\t\tthis.extensionFactories = options.extensionFactories ?? [];\n\t\tthis.noExtensions = options.noExtensions ?? false;\n\t\tthis.noSkills = options.noSkills ?? false;\n\t\tthis.noPromptTemplates = options.noPromptTemplates ?? false;\n\t\tthis.noThemes = options.noThemes ?? false;\n\t\tthis.systemPromptSource = options.systemPrompt;\n\t\tthis.appendSystemPromptSource = options.appendSystemPrompt;\n\t\tthis.extensionsOverride = options.extensionsOverride;\n\t\tthis.skillsOverride = options.skillsOverride;\n\t\tthis.promptsOverride = options.promptsOverride;\n\t\tthis.themesOverride = options.themesOverride;\n\t\tthis.agentsFilesOverride = options.agentsFilesOverride;\n\t\tthis.systemPromptOverride = options.systemPromptOverride;\n\t\tthis.appendSystemPromptOverride = options.appendSystemPromptOverride;\n\n\t\tthis.extensionsResult = { extensions: [], errors: [], runtime: createExtensionRuntime() };\n\t\tthis.skills = [];\n\t\tthis.skillDiagnostics = [];\n\t\tthis.prompts = [];\n\t\tthis.promptDiagnostics = [];\n\t\tthis.themes = [];\n\t\tthis.themeDiagnostics = [];\n\t\tthis.agentsFiles = [];\n\t\tthis.memoryIndexes = {\n\t\t\tglobal: [],\n\t\t\tproject: [],\n\t\t\tglobalMemoryDir: join(resolve(this.agentDir, \"..\"), \"memory\"),\n\t\t\tprojectMemoryDir: join(this.cwd, \".dreb\", \"memory\"),\n\t\t\tdreamLastRun: null,\n\t\t};\n\t\tthis.appendSystemPrompt = [];\n\t\tthis.contextDiagnostics = [];\n\t\tthis.lastSkillPaths = [];\n\t\tthis.extensionSkillSourceInfos = new Map();\n\t\tthis.extensionPromptSourceInfos = new Map();\n\t\tthis.extensionThemeSourceInfos = new Map();\n\t\tthis.lastPromptPaths = [];\n\t\tthis.lastThemePaths = [];\n\t}\n\n\tgetExtensions(): LoadExtensionsResult {\n\t\treturn this.extensionsResult;\n\t}\n\n\tgetSkills(): { skills: Skill[]; diagnostics: ResourceDiagnostic[] } {\n\t\treturn { skills: this.skills, diagnostics: this.skillDiagnostics };\n\t}\n\n\tgetPrompts(): { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] } {\n\t\treturn { prompts: this.prompts, diagnostics: this.promptDiagnostics };\n\t}\n\n\tgetThemes(): { themes: Theme[]; diagnostics: ResourceDiagnostic[] } {\n\t\treturn { themes: this.themes, diagnostics: this.themeDiagnostics };\n\t}\n\n\tgetContextDiagnostics(): ResourceDiagnostic[] {\n\t\treturn this.contextDiagnostics;\n\t}\n\n\tgetAgentsFiles(): { agentsFiles: Array<{ path: string; content: string }> } {\n\t\treturn { agentsFiles: this.agentsFiles };\n\t}\n\n\tgetMemoryIndexes(): MemoryIndexes {\n\t\treturn this.memoryIndexes;\n\t}\n\n\trefreshDreamLastRun(): void {\n\t\tconst globalMemoryDir = join(resolve(this.agentDir, \"..\"), \"memory\");\n\t\tlet dreamLastRun: string | null = null;\n\t\ttry {\n\t\t\tconst dreamLastRunPath = join(globalMemoryDir, \".dream-last-run\");\n\t\t\tif (existsSync(dreamLastRunPath)) {\n\t\t\t\tconst raw = readFileSync(dreamLastRunPath, \"utf-8\").trim();\n\t\t\t\tif (raw && !Number.isNaN(Date.parse(raw))) {\n\t\t\t\t\tdreamLastRun = raw;\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Silently ignore — missing/unreadable file is fine\n\t\t}\n\t\tif (dreamLastRun !== this.memoryIndexes.dreamLastRun) {\n\t\t\tthis.memoryIndexes = { ...this.memoryIndexes, dreamLastRun };\n\t\t}\n\t}\n\n\tgetSystemPrompt(): string | undefined {\n\t\treturn this.systemPrompt;\n\t}\n\n\tgetAppendSystemPrompt(): string[] {\n\t\treturn this.appendSystemPrompt;\n\t}\n\n\textendResources(paths: ResourceExtensionPaths): void {\n\t\tconst skillPaths = this.normalizeExtensionPaths(paths.skillPaths ?? []);\n\t\tconst promptPaths = this.normalizeExtensionPaths(paths.promptPaths ?? []);\n\t\tconst themePaths = this.normalizeExtensionPaths(paths.themePaths ?? []);\n\n\t\tfor (const entry of skillPaths) {\n\t\t\tthis.extensionSkillSourceInfos.set(entry.path, createSourceInfo(entry.path, entry.metadata));\n\t\t}\n\t\tfor (const entry of promptPaths) {\n\t\t\tthis.extensionPromptSourceInfos.set(entry.path, createSourceInfo(entry.path, entry.metadata));\n\t\t}\n\t\tfor (const entry of themePaths) {\n\t\t\tthis.extensionThemeSourceInfos.set(entry.path, createSourceInfo(entry.path, entry.metadata));\n\t\t}\n\n\t\tif (skillPaths.length > 0) {\n\t\t\tthis.lastSkillPaths = this.mergePaths(\n\t\t\t\tthis.lastSkillPaths,\n\t\t\t\tskillPaths.map((entry) => entry.path),\n\t\t\t);\n\t\t\tthis.updateSkillsFromPaths(this.lastSkillPaths);\n\t\t}\n\n\t\tif (promptPaths.length > 0) {\n\t\t\tthis.lastPromptPaths = this.mergePaths(\n\t\t\t\tthis.lastPromptPaths,\n\t\t\t\tpromptPaths.map((entry) => entry.path),\n\t\t\t);\n\t\t\tthis.updatePromptsFromPaths(this.lastPromptPaths);\n\t\t}\n\n\t\tif (themePaths.length > 0) {\n\t\t\tthis.lastThemePaths = this.mergePaths(\n\t\t\t\tthis.lastThemePaths,\n\t\t\t\tthemePaths.map((entry) => entry.path),\n\t\t\t);\n\t\t\tthis.updateThemesFromPaths(this.lastThemePaths);\n\t\t}\n\t}\n\n\tasync reload(): Promise<void> {\n\t\tthis.contextDiagnostics = [];\n\t\tconst resolvedPaths = await this.packageManager.resolve();\n\t\tconst cliExtensionPaths = await this.packageManager.resolveExtensionSources(this.additionalExtensionPaths, {\n\t\t\ttemporary: true,\n\t\t});\n\t\tconst metadataByPath = new Map<string, PathMetadata>();\n\n\t\tthis.extensionSkillSourceInfos = new Map();\n\t\tthis.extensionPromptSourceInfos = new Map();\n\t\tthis.extensionThemeSourceInfos = new Map();\n\n\t\t// Helper to extract enabled paths and store metadata\n\t\tconst getEnabledResources = (\n\t\t\tresources: Array<{ path: string; enabled: boolean; metadata: PathMetadata }>,\n\t\t): Array<{ path: string; enabled: boolean; metadata: PathMetadata }> => {\n\t\t\tfor (const r of resources) {\n\t\t\t\tif (!metadataByPath.has(r.path)) {\n\t\t\t\t\tmetadataByPath.set(r.path, r.metadata);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn resources.filter((r) => r.enabled);\n\t\t};\n\n\t\tconst getEnabledPaths = (\n\t\t\tresources: Array<{ path: string; enabled: boolean; metadata: PathMetadata }>,\n\t\t): string[] => getEnabledResources(resources).map((r) => r.path);\n\t\tconst enabledExtensions = getEnabledPaths(resolvedPaths.extensions);\n\t\tconst enabledSkillResources = getEnabledResources(resolvedPaths.skills);\n\t\tconst enabledPrompts = getEnabledPaths(resolvedPaths.prompts);\n\t\tconst enabledThemes = getEnabledPaths(resolvedPaths.themes);\n\n\t\tconst mapSkillPath = (resource: { path: string; metadata: PathMetadata }): string => {\n\t\t\tif (resource.metadata.source !== \"auto\" && resource.metadata.origin !== \"package\") {\n\t\t\t\treturn resource.path;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst stats = statSync(resource.path);\n\t\t\t\tif (!stats.isDirectory()) {\n\t\t\t\t\treturn resource.path;\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// stat failed — path may not exist yet; use as-is\n\t\t\t\treturn resource.path;\n\t\t\t}\n\t\t\tconst skillFile = join(resource.path, \"SKILL.md\");\n\t\t\tif (existsSync(skillFile)) {\n\t\t\t\tif (!metadataByPath.has(skillFile)) {\n\t\t\t\t\tmetadataByPath.set(skillFile, resource.metadata);\n\t\t\t\t}\n\t\t\t\treturn skillFile;\n\t\t\t}\n\t\t\treturn resource.path;\n\t\t};\n\n\t\tconst enabledSkills = enabledSkillResources.map(mapSkillPath);\n\n\t\t// Add CLI paths metadata\n\t\tfor (const r of cliExtensionPaths.extensions) {\n\t\t\tif (!metadataByPath.has(r.path)) {\n\t\t\t\tmetadataByPath.set(r.path, { source: \"cli\", scope: \"temporary\", origin: \"top-level\" });\n\t\t\t}\n\t\t}\n\t\tfor (const r of cliExtensionPaths.skills) {\n\t\t\tif (!metadataByPath.has(r.path)) {\n\t\t\t\tmetadataByPath.set(r.path, { source: \"cli\", scope: \"temporary\", origin: \"top-level\" });\n\t\t\t}\n\t\t}\n\n\t\tconst cliEnabledExtensions = getEnabledPaths(cliExtensionPaths.extensions);\n\t\tconst cliEnabledSkills = getEnabledPaths(cliExtensionPaths.skills);\n\t\tconst cliEnabledPrompts = getEnabledPaths(cliExtensionPaths.prompts);\n\t\tconst cliEnabledThemes = getEnabledPaths(cliExtensionPaths.themes);\n\n\t\tconst extensionPaths = this.noExtensions\n\t\t\t? cliEnabledExtensions\n\t\t\t: this.mergePaths(cliEnabledExtensions, enabledExtensions);\n\n\t\tconst extensionsResult = await loadExtensions(extensionPaths, this.cwd, this.eventBus);\n\t\tconst inlineExtensions = await this.loadExtensionFactories(extensionsResult.runtime);\n\t\textensionsResult.extensions.push(...inlineExtensions.extensions);\n\t\textensionsResult.errors.push(...inlineExtensions.errors);\n\n\t\t// Detect extension conflicts (tools, commands, flags with same names from different extensions)\n\t\t// Keep all extensions loaded. Conflicts are reported as diagnostics, and precedence is handled by load order.\n\t\tconst conflicts = this.detectExtensionConflicts(extensionsResult.extensions);\n\t\tfor (const conflict of conflicts) {\n\t\t\textensionsResult.errors.push({ path: conflict.path, error: conflict.message });\n\t\t}\n\n\t\tthis.extensionsResult = this.extensionsOverride ? this.extensionsOverride(extensionsResult) : extensionsResult;\n\t\tthis.applyExtensionSourceInfo(this.extensionsResult.extensions, metadataByPath);\n\n\t\tconst skillPaths = this.noSkills\n\t\t\t? this.mergePaths(cliEnabledSkills, this.additionalSkillPaths)\n\t\t\t: this.mergePaths([...enabledSkills, ...cliEnabledSkills], this.additionalSkillPaths);\n\n\t\tthis.lastSkillPaths = skillPaths;\n\t\tthis.updateSkillsFromPaths(skillPaths, metadataByPath);\n\n\t\tconst promptPaths = this.noPromptTemplates\n\t\t\t? this.mergePaths(cliEnabledPrompts, this.additionalPromptTemplatePaths)\n\t\t\t: this.mergePaths([...enabledPrompts, ...cliEnabledPrompts], this.additionalPromptTemplatePaths);\n\n\t\tthis.lastPromptPaths = promptPaths;\n\t\tthis.updatePromptsFromPaths(promptPaths, metadataByPath);\n\n\t\tconst themePaths = this.noThemes\n\t\t\t? this.mergePaths(cliEnabledThemes, this.additionalThemePaths)\n\t\t\t: this.mergePaths([...enabledThemes, ...cliEnabledThemes], this.additionalThemePaths);\n\n\t\tthis.lastThemePaths = themePaths;\n\t\tthis.updateThemesFromPaths(themePaths, metadataByPath);\n\n\t\tconst agentsFiles = {\n\t\t\tagentsFiles: loadProjectContextFiles({ cwd: this.cwd, agentDir: this.agentDir }, this.contextDiagnostics),\n\t\t};\n\t\tconst resolvedAgentsFiles = this.agentsFilesOverride ? this.agentsFilesOverride(agentsFiles) : agentsFiles;\n\t\tthis.agentsFiles = resolvedAgentsFiles.agentsFiles;\n\n\t\t// Load memory indexes and ensure global memory directory exists.\n\t\t// Wrapped in try/catch so memory loading failures degrade gracefully rather than crashing the session.\n\t\ttry {\n\t\t\tthis.memoryIndexes = loadMemoryIndexes({ cwd: this.cwd, agentDir: this.agentDir }, this.contextDiagnostics);\n\t\t\ttry {\n\t\t\t\tmkdirSync(this.memoryIndexes.globalMemoryDir, { recursive: true });\n\t\t\t} catch (error) {\n\t\t\t\tthis.contextDiagnostics.push({\n\t\t\t\t\ttype: \"warning\",\n\t\t\t\t\tmessage: `Could not create memory directory ${this.memoryIndexes.globalMemoryDir}: ${error}. Memory saves to this directory will fail.`,\n\t\t\t\t\tpath: this.memoryIndexes.globalMemoryDir,\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis.contextDiagnostics.push({\n\t\t\t\ttype: \"warning\",\n\t\t\t\tmessage: `Could not load memory indexes: ${error}. Session will start without memory context.`,\n\t\t\t});\n\t\t}\n\n\t\tconst baseSystemPrompt = resolvePromptInput(\n\t\t\tthis.systemPromptSource ?? this.discoverSystemPromptFile(),\n\t\t\t\"system prompt\",\n\t\t\tthis.contextDiagnostics,\n\t\t);\n\t\tthis.systemPrompt = this.systemPromptOverride ? this.systemPromptOverride(baseSystemPrompt) : baseSystemPrompt;\n\n\t\tconst appendSource = this.appendSystemPromptSource ?? this.discoverAppendSystemPromptFile();\n\t\tconst resolvedAppend = resolvePromptInput(appendSource, \"append system prompt\", this.contextDiagnostics);\n\t\tconst baseAppend = resolvedAppend ? [resolvedAppend] : [];\n\t\tthis.appendSystemPrompt = this.appendSystemPromptOverride\n\t\t\t? this.appendSystemPromptOverride(baseAppend)\n\t\t\t: baseAppend;\n\t}\n\n\tprivate normalizeExtensionPaths(\n\t\tentries: Array<{ path: string; metadata: PathMetadata }>,\n\t): Array<{ path: string; metadata: PathMetadata }> {\n\t\treturn entries.map((entry) => ({\n\t\t\tpath: this.resolveResourcePath(entry.path),\n\t\t\tmetadata: entry.metadata,\n\t\t}));\n\t}\n\n\tprivate updateSkillsFromPaths(skillPaths: string[], metadataByPath?: Map<string, PathMetadata>): void {\n\t\tlet skillsResult: { skills: Skill[]; diagnostics: ResourceDiagnostic[] };\n\t\tif (this.noSkills && skillPaths.length === 0) {\n\t\t\tskillsResult = { skills: [], diagnostics: [] };\n\t\t} else {\n\t\t\tskillsResult = loadSkills({\n\t\t\t\tcwd: this.cwd,\n\t\t\t\tagentDir: this.agentDir,\n\t\t\t\tskillPaths,\n\t\t\t\tincludeDefaults: false,\n\t\t\t});\n\t\t}\n\t\tconst resolvedSkills = this.skillsOverride ? this.skillsOverride(skillsResult) : skillsResult;\n\t\tthis.skills = resolvedSkills.skills.map((skill) => ({\n\t\t\t...skill,\n\t\t\tsourceInfo:\n\t\t\t\tthis.findSourceInfoForPath(skill.filePath, this.extensionSkillSourceInfos, metadataByPath) ??\n\t\t\t\tskill.sourceInfo ??\n\t\t\t\tthis.getDefaultSourceInfoForPath(skill.filePath),\n\t\t}));\n\t\tthis.skillDiagnostics = resolvedSkills.diagnostics;\n\t}\n\n\tprivate updatePromptsFromPaths(promptPaths: string[], metadataByPath?: Map<string, PathMetadata>): void {\n\t\tlet promptsResult: { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] };\n\t\tif (this.noPromptTemplates && promptPaths.length === 0) {\n\t\t\tpromptsResult = { prompts: [], diagnostics: [] };\n\t\t} else {\n\t\t\tconst allPrompts = loadPromptTemplates({\n\t\t\t\tcwd: this.cwd,\n\t\t\t\tagentDir: this.agentDir,\n\t\t\t\tpromptPaths,\n\t\t\t\tincludeDefaults: false,\n\t\t\t});\n\t\t\tpromptsResult = this.dedupePrompts(allPrompts);\n\t\t}\n\t\tconst resolvedPrompts = this.promptsOverride ? this.promptsOverride(promptsResult) : promptsResult;\n\t\tthis.prompts = resolvedPrompts.prompts.map((prompt) => ({\n\t\t\t...prompt,\n\t\t\tsourceInfo:\n\t\t\t\tthis.findSourceInfoForPath(prompt.filePath, this.extensionPromptSourceInfos, metadataByPath) ??\n\t\t\t\tprompt.sourceInfo ??\n\t\t\t\tthis.getDefaultSourceInfoForPath(prompt.filePath),\n\t\t}));\n\t\tthis.promptDiagnostics = resolvedPrompts.diagnostics;\n\t}\n\n\tprivate updateThemesFromPaths(themePaths: string[], metadataByPath?: Map<string, PathMetadata>): void {\n\t\tlet themesResult: { themes: Theme[]; diagnostics: ResourceDiagnostic[] };\n\t\tif (this.noThemes && themePaths.length === 0) {\n\t\t\tthemesResult = { themes: [], diagnostics: [] };\n\t\t} else {\n\t\t\tconst loaded = this.loadThemes(themePaths, false);\n\t\t\tconst deduped = this.dedupeThemes(loaded.themes);\n\t\t\tthemesResult = { themes: deduped.themes, diagnostics: [...loaded.diagnostics, ...deduped.diagnostics] };\n\t\t}\n\t\tconst resolvedThemes = this.themesOverride ? this.themesOverride(themesResult) : themesResult;\n\t\tthis.themes = resolvedThemes.themes.map((theme) => {\n\t\t\tconst sourcePath = theme.sourcePath;\n\t\t\ttheme.sourceInfo = sourcePath\n\t\t\t\t? (this.findSourceInfoForPath(sourcePath, this.extensionThemeSourceInfos, metadataByPath) ??\n\t\t\t\t\ttheme.sourceInfo ??\n\t\t\t\t\tthis.getDefaultSourceInfoForPath(sourcePath))\n\t\t\t\t: theme.sourceInfo;\n\t\t\treturn theme;\n\t\t});\n\t\tthis.themeDiagnostics = resolvedThemes.diagnostics;\n\t}\n\n\tprivate applyExtensionSourceInfo(extensions: Extension[], metadataByPath: Map<string, PathMetadata>): void {\n\t\tfor (const extension of extensions) {\n\t\t\textension.sourceInfo =\n\t\t\t\tthis.findSourceInfoForPath(extension.path, undefined, metadataByPath) ??\n\t\t\t\tthis.getDefaultSourceInfoForPath(extension.path);\n\t\t\tfor (const command of extension.commands.values()) {\n\t\t\t\tcommand.sourceInfo = extension.sourceInfo;\n\t\t\t}\n\t\t\tfor (const tool of extension.tools.values()) {\n\t\t\t\ttool.sourceInfo = extension.sourceInfo;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate findSourceInfoForPath(\n\t\tresourcePath: string,\n\t\textraSourceInfos?: Map<string, SourceInfo>,\n\t\tmetadataByPath?: Map<string, PathMetadata>,\n\t): SourceInfo | undefined {\n\t\tif (!resourcePath) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tif (resourcePath.startsWith(\"<\")) {\n\t\t\treturn this.getDefaultSourceInfoForPath(resourcePath);\n\t\t}\n\n\t\tconst normalizedResourcePath = resolve(resourcePath);\n\t\tif (extraSourceInfos) {\n\t\t\tfor (const [sourcePath, sourceInfo] of extraSourceInfos.entries()) {\n\t\t\t\tconst normalizedSourcePath = resolve(sourcePath);\n\t\t\t\tif (\n\t\t\t\t\tnormalizedResourcePath === normalizedSourcePath ||\n\t\t\t\t\tnormalizedResourcePath.startsWith(`${normalizedSourcePath}${sep}`)\n\t\t\t\t) {\n\t\t\t\t\treturn { ...sourceInfo, path: resourcePath };\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (metadataByPath) {\n\t\t\tconst exact = metadataByPath.get(normalizedResourcePath) ?? metadataByPath.get(resourcePath);\n\t\t\tif (exact) {\n\t\t\t\treturn createSourceInfo(resourcePath, exact);\n\t\t\t}\n\n\t\t\tfor (const [sourcePath, metadata] of metadataByPath.entries()) {\n\t\t\t\tconst normalizedSourcePath = resolve(sourcePath);\n\t\t\t\tif (\n\t\t\t\t\tnormalizedResourcePath === normalizedSourcePath ||\n\t\t\t\t\tnormalizedResourcePath.startsWith(`${normalizedSourcePath}${sep}`)\n\t\t\t\t) {\n\t\t\t\t\treturn createSourceInfo(resourcePath, metadata);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tprivate getDefaultSourceInfoForPath(filePath: string): SourceInfo {\n\t\tif (filePath.startsWith(\"<\") && filePath.endsWith(\">\")) {\n\t\t\treturn {\n\t\t\t\tpath: filePath,\n\t\t\t\tsource: filePath.slice(1, -1).split(\":\")[0] || \"temporary\",\n\t\t\t\tscope: \"temporary\",\n\t\t\t\torigin: \"top-level\",\n\t\t\t};\n\t\t}\n\n\t\tconst normalizedPath = resolve(filePath);\n\t\tconst agentRoots = [\n\t\t\tjoin(this.agentDir, \"skills\"),\n\t\t\tjoin(this.agentDir, \"prompts\"),\n\t\t\tjoin(this.agentDir, \"themes\"),\n\t\t\tjoin(this.agentDir, \"extensions\"),\n\t\t];\n\t\tconst projectRoots = [\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"skills\"),\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"prompts\"),\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"themes\"),\n\t\t\tjoin(this.cwd, CONFIG_DIR_NAME, \"extensions\"),\n\t\t];\n\n\t\tfor (const root of agentRoots) {\n\t\t\tif (this.isUnderPath(normalizedPath, root)) {\n\t\t\t\treturn { path: filePath, source: \"local\", scope: \"user\", origin: \"top-level\", baseDir: root };\n\t\t\t}\n\t\t}\n\n\t\tfor (const root of projectRoots) {\n\t\t\tif (this.isUnderPath(normalizedPath, root)) {\n\t\t\t\treturn { path: filePath, source: \"local\", scope: \"project\", origin: \"top-level\", baseDir: root };\n\t\t\t}\n\t\t}\n\n\t\tlet baseDir: string;\n\t\ttry {\n\t\t\tbaseDir = statSync(normalizedPath).isDirectory() ? normalizedPath : resolve(normalizedPath, \"..\");\n\t\t} catch {\n\t\t\t// Path doesn't exist (deleted file, broken symlink, race condition) — fall back to parent dir\n\t\t\tbaseDir = resolve(normalizedPath, \"..\");\n\t\t}\n\t\treturn {\n\t\t\tpath: filePath,\n\t\t\tsource: \"local\",\n\t\t\tscope: \"temporary\",\n\t\t\torigin: \"top-level\",\n\t\t\tbaseDir,\n\t\t};\n\t}\n\n\tprivate mergePaths(primary: string[], additional: string[]): string[] {\n\t\tconst merged: string[] = [];\n\t\tconst seen = new Set<string>();\n\n\t\tfor (const p of [...primary, ...additional]) {\n\t\t\tconst resolved = this.resolveResourcePath(p);\n\t\t\tif (seen.has(resolved)) continue;\n\t\t\tseen.add(resolved);\n\t\t\tmerged.push(resolved);\n\t\t}\n\n\t\treturn merged;\n\t}\n\n\tprivate resolveResourcePath(p: string): string {\n\t\tconst trimmed = p.trim();\n\t\tlet expanded = trimmed;\n\t\tif (trimmed === \"~\") {\n\t\t\texpanded = homedir();\n\t\t} else if (trimmed.startsWith(\"~/\")) {\n\t\t\texpanded = join(homedir(), trimmed.slice(2));\n\t\t} else if (trimmed.startsWith(\"~\")) {\n\t\t\texpanded = join(homedir(), trimmed.slice(1));\n\t\t}\n\t\treturn resolve(this.cwd, expanded);\n\t}\n\n\tprivate loadThemes(\n\t\tpaths: string[],\n\t\tincludeDefaults: boolean = true,\n\t): {\n\t\tthemes: Theme[];\n\t\tdiagnostics: ResourceDiagnostic[];\n\t} {\n\t\tconst themes: Theme[] = [];\n\t\tconst diagnostics: ResourceDiagnostic[] = [];\n\t\tif (includeDefaults) {\n\t\t\tconst defaultDirs = [join(this.agentDir, \"themes\"), join(this.cwd, CONFIG_DIR_NAME, \"themes\")];\n\n\t\t\tfor (const dir of defaultDirs) {\n\t\t\t\tthis.loadThemesFromDir(dir, themes, diagnostics);\n\t\t\t}\n\t\t}\n\n\t\tfor (const p of paths) {\n\t\t\tconst resolved = resolve(this.cwd, p);\n\t\t\tif (!existsSync(resolved)) {\n\t\t\t\tdiagnostics.push({ type: \"warning\", message: \"theme path does not exist\", path: resolved });\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst stats = statSync(resolved);\n\t\t\t\tif (stats.isDirectory()) {\n\t\t\t\t\tthis.loadThemesFromDir(resolved, themes, diagnostics);\n\t\t\t\t} else if (stats.isFile() && resolved.endsWith(\".json\")) {\n\t\t\t\t\tthis.loadThemeFromFile(resolved, themes, diagnostics);\n\t\t\t\t} else {\n\t\t\t\t\tdiagnostics.push({ type: \"warning\", message: \"theme path is not a json file\", path: resolved });\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconst message = error instanceof Error ? error.message : \"failed to read theme path\";\n\t\t\t\tdiagnostics.push({ type: \"warning\", message, path: resolved });\n\t\t\t}\n\t\t}\n\n\t\treturn { themes, diagnostics };\n\t}\n\n\tprivate loadThemesFromDir(dir: string, themes: Theme[], diagnostics: ResourceDiagnostic[]): void {\n\t\tif (!existsSync(dir)) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst entries = readdirSync(dir, { withFileTypes: true });\n\t\t\tfor (const entry of entries) {\n\t\t\t\tlet isFile = entry.isFile();\n\t\t\t\tif (entry.isSymbolicLink()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tisFile = statSync(join(dir, entry.name)).isFile();\n\t\t\t\t\t} catch {\n\t\t\t\t\t\t// Broken symlink — skip entry\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!isFile) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (!entry.name.endsWith(\".json\")) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tthis.loadThemeFromFile(join(dir, entry.name), themes, diagnostics);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : \"failed to read theme directory\";\n\t\t\tdiagnostics.push({ type: \"warning\", message, path: dir });\n\t\t}\n\t}\n\n\tprivate loadThemeFromFile(filePath: string, themes: Theme[], diagnostics: ResourceDiagnostic[]): void {\n\t\ttry {\n\t\t\tthemes.push(loadThemeFromPath(filePath));\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : \"failed to load theme\";\n\t\t\tdiagnostics.push({ type: \"warning\", message, path: filePath });\n\t\t}\n\t}\n\n\tprivate async loadExtensionFactories(runtime: ExtensionRuntime): Promise<{\n\t\textensions: Extension[];\n\t\terrors: Array<{ path: string; error: string }>;\n\t}> {\n\t\tconst extensions: Extension[] = [];\n\t\tconst errors: Array<{ path: string; error: string }> = [];\n\n\t\tfor (const [index, factory] of this.extensionFactories.entries()) {\n\t\t\tconst extensionPath = `<inline:${index + 1}>`;\n\t\t\ttry {\n\t\t\t\tconst extension = await loadExtensionFromFactory(factory, this.cwd, this.eventBus, runtime, extensionPath);\n\t\t\t\textensions.push(extension);\n\t\t\t} catch (error) {\n\t\t\t\tconst message = error instanceof Error ? error.message : \"failed to load extension\";\n\t\t\t\terrors.push({ path: extensionPath, error: message });\n\t\t\t}\n\t\t}\n\n\t\treturn { extensions, errors };\n\t}\n\n\tprivate dedupePrompts(prompts: PromptTemplate[]): { prompts: PromptTemplate[]; diagnostics: ResourceDiagnostic[] } {\n\t\tconst seen = new Map<string, PromptTemplate>();\n\t\tconst diagnostics: ResourceDiagnostic[] = [];\n\n\t\tfor (const prompt of prompts) {\n\t\t\tconst existing = seen.get(prompt.name);\n\t\t\tif (existing) {\n\t\t\t\tdiagnostics.push({\n\t\t\t\t\ttype: \"collision\",\n\t\t\t\t\tmessage: `name \"/${prompt.name}\" collision`,\n\t\t\t\t\tpath: prompt.filePath,\n\t\t\t\t\tcollision: {\n\t\t\t\t\t\tresourceType: \"prompt\",\n\t\t\t\t\t\tname: prompt.name,\n\t\t\t\t\t\twinnerPath: existing.filePath,\n\t\t\t\t\t\tloserPath: prompt.filePath,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tseen.set(prompt.name, prompt);\n\t\t\t}\n\t\t}\n\n\t\treturn { prompts: Array.from(seen.values()), diagnostics };\n\t}\n\n\tprivate dedupeThemes(themes: Theme[]): { themes: Theme[]; diagnostics: ResourceDiagnostic[] } {\n\t\tconst seen = new Map<string, Theme>();\n\t\tconst diagnostics: ResourceDiagnostic[] = [];\n\n\t\tfor (const t of themes) {\n\t\t\tconst name = t.name ?? \"unnamed\";\n\t\t\tconst existing = seen.get(name);\n\t\t\tif (existing) {\n\t\t\t\tdiagnostics.push({\n\t\t\t\t\ttype: \"collision\",\n\t\t\t\t\tmessage: `name \"${name}\" collision`,\n\t\t\t\t\tpath: t.sourcePath,\n\t\t\t\t\tcollision: {\n\t\t\t\t\t\tresourceType: \"theme\",\n\t\t\t\t\t\tname,\n\t\t\t\t\t\twinnerPath: existing.sourcePath ?? \"<builtin>\",\n\t\t\t\t\t\tloserPath: t.sourcePath ?? \"<builtin>\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tseen.set(name, t);\n\t\t\t}\n\t\t}\n\n\t\treturn { themes: Array.from(seen.values()), diagnostics };\n\t}\n\n\tprivate discoverSystemPromptFile(): string | undefined {\n\t\tconst projectPath = join(this.cwd, CONFIG_DIR_NAME, \"SYSTEM.md\");\n\t\tif (existsSync(projectPath)) {\n\t\t\treturn projectPath;\n\t\t}\n\n\t\tconst globalPath = join(this.agentDir, \"SYSTEM.md\");\n\t\tif (existsSync(globalPath)) {\n\t\t\treturn globalPath;\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tprivate discoverAppendSystemPromptFile(): string | undefined {\n\t\tconst projectPath = join(this.cwd, CONFIG_DIR_NAME, \"APPEND_SYSTEM.md\");\n\t\tif (existsSync(projectPath)) {\n\t\t\treturn projectPath;\n\t\t}\n\n\t\tconst globalPath = join(this.agentDir, \"APPEND_SYSTEM.md\");\n\t\tif (existsSync(globalPath)) {\n\t\t\treturn globalPath;\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tprivate isUnderPath(target: string, root: string): boolean {\n\t\tconst normalizedRoot = resolve(root);\n\t\tif (target === normalizedRoot) {\n\t\t\treturn true;\n\t\t}\n\t\tconst prefix = normalizedRoot.endsWith(sep) ? normalizedRoot : `${normalizedRoot}${sep}`;\n\t\treturn target.startsWith(prefix);\n\t}\n\n\tprivate detectExtensionConflicts(extensions: Extension[]): Array<{ path: string; message: string }> {\n\t\tconst conflicts: Array<{ path: string; message: string }> = [];\n\n\t\t// Track which extension registered each tool and flag\n\t\tconst toolOwners = new Map<string, string>();\n\t\tconst flagOwners = new Map<string, string>();\n\n\t\tfor (const ext of extensions) {\n\t\t\t// Check tools\n\t\t\tfor (const toolName of ext.tools.keys()) {\n\t\t\t\tconst existingOwner = toolOwners.get(toolName);\n\t\t\t\tif (existingOwner && existingOwner !== ext.path) {\n\t\t\t\t\tconflicts.push({\n\t\t\t\t\t\tpath: ext.path,\n\t\t\t\t\t\tmessage: `Tool \"${toolName}\" conflicts with ${existingOwner}`,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\ttoolOwners.set(toolName, ext.path);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check flags\n\t\t\tfor (const flagName of ext.flags.keys()) {\n\t\t\t\tconst existingOwner = flagOwners.get(flagName);\n\t\t\t\tif (existingOwner && existingOwner !== ext.path) {\n\t\t\t\t\tconflicts.push({\n\t\t\t\t\t\tpath: ext.path,\n\t\t\t\t\t\tmessage: `Flag \"--${flagName}\" conflicts with ${existingOwner}`,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tflagOwners.set(flagName, ext.path);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn conflicts;\n\t}\n}\n"]}
@@ -33,20 +33,20 @@ function resolvePromptInput(input, description, diagnostics) {
33
33
  function stripHtmlComments(content) {
34
34
  return content.replace(/<!--[\s\S]*?-->/g, "");
35
35
  }
36
- function loadContextFilesFromDir(dir, diagnostics) {
37
- const candidates = [
38
- "AGENTS.md",
39
- "AGENTS.MD",
40
- "CLAUDE.md",
41
- "CLAUDE.MD",
42
- join(".claude", "CLAUDE.md"),
43
- join(".claude", "CLAUDE.MD"),
44
- join(".dreb", "CONTEXT.md"),
45
- join(".dreb", "CONTEXT.MD"),
46
- ];
36
+ export const CONTEXT_FILE_CANDIDATES = [
37
+ "AGENTS.md",
38
+ "AGENTS.MD",
39
+ "CLAUDE.md",
40
+ "CLAUDE.MD",
41
+ join(".claude", "CLAUDE.md"),
42
+ join(".claude", "CLAUDE.MD"),
43
+ join(".dreb", "CONTEXT.md"),
44
+ join(".dreb", "CONTEXT.MD"),
45
+ ];
46
+ export function loadContextFilesFromDir(dir, diagnostics) {
47
47
  const results = [];
48
48
  const seenRealPaths = new Set();
49
- for (const filename of candidates) {
49
+ for (const filename of CONTEXT_FILE_CANDIDATES) {
50
50
  const filePath = join(dir, filename);
51
51
  if (existsSync(filePath)) {
52
52
  // Deduplicate by realpath to avoid loading the same file twice on