fix(coding-agent): dedupe symlinked resources (#3818)

* fix(coding-agent): dedupe symlinked resources

Fixes #3767

* refactor(coding-agent): extract canonicalizePath util for symlink resolution
This commit is contained in:
Aliou Diallo
2026-04-27 18:33:10 +02:00
committed by GitHub
parent 6a10c17219
commit 10425abb87
8 changed files with 215 additions and 53 deletions

View File

@@ -1,15 +1,6 @@
import { type ChildProcess, type ChildProcessByStdio, spawn, spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
realpathSync,
rmSync,
statSync,
writeFileSync,
} from "node:fs";
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { basename, dirname, join, relative, resolve, sep } from "node:path";
import type { Readable } from "node:stream";
@@ -18,7 +9,7 @@ import ignore from "ignore";
import { minimatch } from "minimatch";
import { CONFIG_DIR_NAME } from "../config.js";
import { type GitSource, parseGitUrl } from "../utils/git.js";
import { isLocalPath } from "../utils/paths.js";
import { canonicalizePath, isLocalPath } from "../utils/paths.js";
import { isStdoutTakenOver } from "./output-guard.js";
import type { PackageSource, SettingsManager } from "./settings-manager.js";
@@ -2286,40 +2277,30 @@ export class DefaultPackageManager implements PackageManager {
}
private toResolvedPaths(accumulator: ResourceAccumulator): ResolvedPaths {
const toResolved = (entries: Map<string, { metadata: PathMetadata; enabled: boolean }>): ResolvedResource[] => {
const mapToResolved = (
entries: Map<string, { metadata: PathMetadata; enabled: boolean }>,
): ResolvedResource[] => {
const resolved = Array.from(entries.entries()).map(([path, { metadata, enabled }]) => ({
path,
enabled,
metadata,
}));
resolved.sort((a, b) => resourcePrecedenceRank(a.metadata) - resourcePrecedenceRank(b.metadata));
return resolved;
const seen = new Set<string>();
return resolved.filter((entry) => {
const canonicalPath = canonicalizePath(entry.path);
if (seen.has(canonicalPath)) return false;
seen.add(canonicalPath);
return true;
});
};
const seenCanonicalSkillPaths = new Set<string>();
const resolvedSkills = toResolved(accumulator.skills).filter((entry) => {
let canonicalPath: string;
try {
// Resolve symlink aliases to detect duplicate files.
canonicalPath = realpathSync(entry.path);
} catch {
// Fallback to raw path to match loadSkills() behavior.
canonicalPath = entry.path;
}
if (seenCanonicalSkillPaths.has(canonicalPath)) {
return false;
}
seenCanonicalSkillPaths.add(canonicalPath);
return true;
});
return {
extensions: toResolved(accumulator.extensions),
skills: resolvedSkills,
prompts: toResolved(accumulator.prompts),
themes: toResolved(accumulator.themes),
extensions: mapToResolved(accumulator.extensions),
skills: mapToResolved(accumulator.skills),
prompts: mapToResolved(accumulator.prompts),
themes: mapToResolved(accumulator.themes),
};
}

View File

@@ -8,7 +8,7 @@ import type { ResourceDiagnostic } from "./diagnostics.js";
export type { ResourceCollision, ResourceDiagnostic } from "./diagnostics.js";
import { isLocalPath } from "../utils/paths.js";
import { canonicalizePath, isLocalPath } from "../utils/paths.js";
import { createEventBus, type EventBus } from "./event-bus.js";
import { createExtensionRuntime, loadExtensionFromFactory, loadExtensions } from "./extensions/loader.js";
import type { Extension, ExtensionFactory, ExtensionRuntime, LoadExtensionsResult } from "./extensions/types.js";
@@ -664,8 +664,9 @@ export class DefaultResourceLoader implements ResourceLoader {
for (const p of [...primary, ...additional]) {
const resolved = this.resolveResourcePath(p);
if (seen.has(resolved)) continue;
seen.add(resolved);
const canonicalPath = canonicalizePath(resolved);
if (seen.has(canonicalPath)) continue;
seen.add(canonicalPath);
merged.push(resolved);
}

View File

@@ -1,9 +1,10 @@
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from "fs";
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
import ignore from "ignore";
import { homedir } from "os";
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "path";
import { CONFIG_DIR_NAME, getAgentDir } from "../config.js";
import { parseFrontmatter } from "../utils/frontmatter.js";
import { canonicalizePath } from "../utils/paths.js";
import type { ResourceDiagnostic } from "./diagnostics.js";
import { createSyntheticSourceInfo, type SourceInfo } from "./source-info.js";
@@ -416,12 +417,7 @@ export function loadSkills(options: LoadSkillsOptions): LoadSkillsResult {
allDiagnostics.push(...result.diagnostics);
for (const skill of result.skills) {
// Resolve symlinks to detect duplicate files
let realPath: string;
try {
realPath = realpathSync(skill.filePath);
} catch {
realPath = skill.filePath;
}
const realPath = canonicalizePath(skill.filePath);
// Skip silently if we've already loaded this exact file (via symlink)
if (realPathSet.has(realPath)) {