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:
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync, realpathSync } from "node:fs";
|
||||
import { existsSync } from "node:fs";
|
||||
import { unlink } from "node:fs/promises";
|
||||
import * as os from "node:os";
|
||||
import {
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
truncateToWidth,
|
||||
visibleWidth,
|
||||
} from "@mariozechner/pi-tui";
|
||||
import { canonicalizePath as _canonicalizePath } from "../../../../src/utils/paths.js";
|
||||
import { KeybindingsManager } from "../../../core/keybindings.js";
|
||||
import type { SessionInfo, SessionListProgress } from "../../../core/session-manager.js";
|
||||
import { theme } from "../theme/theme.js";
|
||||
@@ -49,11 +50,7 @@ function formatSessionDate(date: Date): string {
|
||||
|
||||
function canonicalizePath(path: string | undefined): string | undefined {
|
||||
if (!path) return path;
|
||||
try {
|
||||
return realpathSync(path);
|
||||
} catch {
|
||||
return path;
|
||||
}
|
||||
return _canonicalizePath(path);
|
||||
}
|
||||
|
||||
class SessionSelectorHeader implements Component {
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
import { realpathSync } from "node:fs";
|
||||
|
||||
/**
|
||||
* Resolve a path to its canonical (real) form, following symlinks.
|
||||
* Falls back to the raw path if resolution fails (e.g. the target does
|
||||
* not exist yet), so that callers never crash on missing filesystem
|
||||
* entries.
|
||||
*/
|
||||
export function canonicalizePath(path: string): string {
|
||||
try {
|
||||
return realpathSync(path);
|
||||
} catch {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the value is NOT a package source (npm:, git:, etc.)
|
||||
* or a URL protocol. Bare names and relative paths without ./ prefix
|
||||
|
||||
Reference in New Issue
Block a user