Skip to content

Commit 2f47db1

Browse files
committed
fix(paths): preserve identity canonicalization
1 parent 0ea9967 commit 2f47db1

4 files changed

Lines changed: 84 additions & 6 deletions

File tree

‎src/agents/agent-dir-registry.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Process-local reverse registry from prepared agent directories to agent ids. */
2-
import { resolvePathViaExistingAncestorSync } from "../infra/boundary-path.js";
2+
import { resolveIdentityPathViaExistingAncestorSync } from "../infra/boundary-path.js";
33
import { isPathInside } from "../infra/path-guards.js";
44
import { normalizeAgentId } from "../routing/session-key.js";
55
import { resolveUserPath } from "../utils.js";
@@ -12,7 +12,7 @@ export function normalizeAgentDirRegistryPath(
1212
agentDir: string,
1313
env: NodeJS.ProcessEnv = process.env,
1414
): string {
15-
return resolvePathViaExistingAncestorSync(resolveUserPath(agentDir, env));
15+
return resolveIdentityPathViaExistingAncestorSync(resolveUserPath(agentDir, env));
1616
}
1717

1818
/** Register a resolved agent directory for later reverse lookup. */

‎src/infra/boundary-path.test.ts‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
// Tests path boundary enforcement for safe file access.
2+
import fsSync from "node:fs";
23
import fs from "node:fs/promises";
34
import path from "node:path";
4-
import { describe, expect, it } from "vitest";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
56
import { withTestDir } from "../test-helpers/temp-dir.js";
6-
import { resolveRootPath, resolveRootPathSync } from "./boundary-path.js";
7+
import {
8+
resolveIdentityPathViaExistingAncestorSync,
9+
resolveRootPath,
10+
resolveRootPathSync,
11+
} from "./boundary-path.js";
712
import { isPathInside } from "./path-guards.js";
813

14+
afterEach(() => {
15+
vi.restoreAllMocks();
16+
});
17+
918
function createSeededRandom(seed: number): () => number {
1019
let state = seed >>> 0;
1120
return () => {
@@ -14,6 +23,53 @@ function createSeededRandom(seed: number): () => number {
1423
};
1524
}
1625

26+
describe("resolveIdentityPathViaExistingAncestorSync", () => {
27+
it("continues through native realpath failures to preserve ancestor identity", () => {
28+
const aliasRoot = path.resolve("identity-alias");
29+
const targetPath = path.join(aliasRoot, "locked", "leaf");
30+
const lockedPath = path.dirname(targetPath);
31+
const canonicalRoot = path.resolve("identity-real");
32+
const calls: string[] = [];
33+
34+
vi.spyOn(fsSync.realpathSync, "native").mockImplementation((candidate) => {
35+
const resolved = path.resolve(String(candidate));
36+
calls.push(resolved);
37+
if (resolved === aliasRoot) {
38+
return canonicalRoot;
39+
}
40+
throw new Error("simulated realpath failure");
41+
});
42+
43+
expect(resolveIdentityPathViaExistingAncestorSync(targetPath)).toBe(
44+
path.join(canonicalRoot, "locked", "leaf"),
45+
);
46+
expect(calls).toEqual([targetPath, lockedPath, aliasRoot]);
47+
});
48+
49+
it("falls back lexically only after native realpath fails through the root", () => {
50+
const targetPath = path.resolve("identity-alias", "locked", "leaf");
51+
const calls: string[] = [];
52+
const expectedCalls: string[] = [];
53+
let cursor = targetPath;
54+
while (true) {
55+
expectedCalls.push(cursor);
56+
const parent = path.dirname(cursor);
57+
if (parent === cursor) {
58+
break;
59+
}
60+
cursor = parent;
61+
}
62+
63+
vi.spyOn(fsSync.realpathSync, "native").mockImplementation((candidate) => {
64+
calls.push(path.resolve(String(candidate)));
65+
throw new Error("simulated realpath failure");
66+
});
67+
68+
expect(resolveIdentityPathViaExistingAncestorSync(targetPath)).toBe(targetPath);
69+
expect(calls).toEqual(expectedCalls);
70+
});
71+
});
72+
1773
describe("resolveRootPath", () => {
1874
it("resolves symlink parents with non-existent leafs inside root", async () => {
1975
if (process.platform === "win32") {

‎src/infra/boundary-path.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Exposes boundary path resolution helpers with fs-safe defaults.
22
import "./fs-safe-defaults.js";
3+
import fs from "node:fs";
34
import path from "node:path";
45
import { safeRealpathSync } from "@openclaw/fs-safe/path";
56
export { safeRealpathSync } from "@openclaw/fs-safe/path";
@@ -9,6 +10,27 @@ export function resolveRealpathOrAbsolute(value: string): string {
910
return safeRealpathSync(value) ?? path.resolve(value);
1011
}
1112

13+
export function resolveIdentityPathViaExistingAncestorSync(targetPath: string): string {
14+
const fallback = path.resolve(targetPath);
15+
const missingSegments: string[] = [];
16+
let cursor = fallback;
17+
18+
while (true) {
19+
try {
20+
// Identity aliases must converge even after an intermediate realpath failure,
21+
// or ownership and lock callers can split one resource into separate identities.
22+
return path.join(fs.realpathSync.native(cursor), ...missingSegments.toReversed());
23+
} catch {
24+
const parent = path.dirname(cursor);
25+
if (parent === cursor) {
26+
return fallback;
27+
}
28+
missingSegments.push(path.basename(cursor));
29+
cursor = parent;
30+
}
31+
}
32+
}
33+
1234
// Boundary path resolution keeps alias expansion and realpath checks in one
1335
// shared contract before file IO happens.
1436
export {

‎src/infra/gateway-lock.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { z } from "zod";
1313
import { resolveConfigPath, resolveGatewayLockDir, resolveStateDir } from "../config/paths.js";
1414
import { getFileLockProcessStartTime, isPidAlive } from "../shared/pid-alive.js";
1515
import { safeParseJsonWithSchema } from "../utils/zod-parse.js";
16-
import { resolvePathViaExistingAncestorSync } from "./boundary-path.js";
16+
import { resolveIdentityPathViaExistingAncestorSync } from "./boundary-path.js";
1717
import { sha256HexPrefixCore } from "./crypto-digest.js";
1818
import { createFileLockManager } from "./file-lock-manager.js";
1919
import {
@@ -297,7 +297,7 @@ async function shouldReclaimGatewayLock(params: {
297297

298298
function resolveGatewayLockPaths(env: NodeJS.ProcessEnv, suppliedLockDir?: string) {
299299
const resolvedStateDir = resolveStateDir(env);
300-
const stateDir = resolvePathViaExistingAncestorSync(resolvedStateDir);
300+
const stateDir = resolveIdentityPathViaExistingAncestorSync(resolvedStateDir);
301301
const lockDir = suppliedLockDir ?? resolveGatewayLockDir(stateDir);
302302
const configPath = resolveConfigPath(env, resolvedStateDir);
303303
const configHash = sha256HexPrefixCore(configPath, 8);

0 commit comments

Comments
 (0)