fix(write-existing-file-guard): defer realpath/existsSync to first tool call

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-18 14:11:03 +09:00
parent a6e3c6a5ed
commit a4c45e2770
3 changed files with 15 additions and 6 deletions
+10 -2
View File
@@ -76,7 +76,15 @@ export function isOverwriteEnabled(value: boolean | string | undefined): boolean
export function createWriteExistingFileGuardHook(ctx: PluginInput): Hooks {
const readPermissionsBySession = new Map<string, Set<string>>()
const sessionLastAccess = new Map<string, number>()
const canonicalSessionRoot = toCanonicalPath(resolveInputPath(ctx, ctx.directory))
let canonicalSessionRoot: string | undefined
function getCanonicalSessionRoot(): string {
if (!canonicalSessionRoot) {
canonicalSessionRoot = toCanonicalPath(resolveInputPath(ctx, ctx.directory))
}
return canonicalSessionRoot
}
return {
"tool.execute.before": async (input, output) => {
@@ -86,7 +94,7 @@ export function createWriteExistingFileGuardHook(ctx: PluginInput): Hooks {
output,
readPermissionsBySession,
sessionLastAccess,
canonicalSessionRoot,
getCanonicalSessionRoot,
maxTrackedSessions: MAX_TRACKED_SESSIONS,
})
},
@@ -59,7 +59,7 @@ describe("createWriteExistingFileGuardHook", () => {
).rejects.toThrow("File already exists. Use edit tool instead.")
// then
expect(existsSyncMock).toHaveBeenCalledTimes(2)
expect(realpathNativeMock).toHaveBeenCalledTimes(1)
expect(existsSyncMock).toHaveBeenCalledTimes(3)
expect(realpathNativeMock).toHaveBeenCalledTimes(2)
})
})
@@ -90,10 +90,10 @@ export async function handleWriteExistingFileGuardToolExecuteBefore(params: {
output: { args?: unknown }
readPermissionsBySession: Map<string, Set<string>>
sessionLastAccess: Map<string, number>
canonicalSessionRoot: string
getCanonicalSessionRoot: () => string
maxTrackedSessions: number
}): Promise<void> {
const { ctx, input, output, readPermissionsBySession, sessionLastAccess, canonicalSessionRoot, maxTrackedSessions } = params
const { ctx, input, output, readPermissionsBySession, sessionLastAccess, getCanonicalSessionRoot, maxTrackedSessions } = params
const toolName = input.tool?.toLowerCase()
if (toolName !== "write" && toolName !== "read") {
return
@@ -107,6 +107,7 @@ export async function handleWriteExistingFileGuardToolExecuteBefore(params: {
}
const resolvedPath = resolveInputPath(ctx, filePath)
const canonicalSessionRoot = getCanonicalSessionRoot()
const canonicalPath = toCanonicalPath(resolvedPath)
if (!isPathInsideDirectory(canonicalPath, canonicalSessionRoot)) {
return