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),
};
}