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:
@@ -76,7 +76,15 @@ export function isOverwriteEnabled(value: boolean | string | undefined): boolean
|
|||||||
export function createWriteExistingFileGuardHook(ctx: PluginInput): Hooks {
|
export function createWriteExistingFileGuardHook(ctx: PluginInput): Hooks {
|
||||||
const readPermissionsBySession = new Map<string, Set<string>>()
|
const readPermissionsBySession = new Map<string, Set<string>>()
|
||||||
const sessionLastAccess = new Map<string, number>()
|
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 {
|
return {
|
||||||
"tool.execute.before": async (input, output) => {
|
"tool.execute.before": async (input, output) => {
|
||||||
@@ -86,7 +94,7 @@ export function createWriteExistingFileGuardHook(ctx: PluginInput): Hooks {
|
|||||||
output,
|
output,
|
||||||
readPermissionsBySession,
|
readPermissionsBySession,
|
||||||
sessionLastAccess,
|
sessionLastAccess,
|
||||||
canonicalSessionRoot,
|
getCanonicalSessionRoot,
|
||||||
maxTrackedSessions: MAX_TRACKED_SESSIONS,
|
maxTrackedSessions: MAX_TRACKED_SESSIONS,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ describe("createWriteExistingFileGuardHook", () => {
|
|||||||
).rejects.toThrow("File already exists. Use edit tool instead.")
|
).rejects.toThrow("File already exists. Use edit tool instead.")
|
||||||
|
|
||||||
// then
|
// then
|
||||||
expect(existsSyncMock).toHaveBeenCalledTimes(2)
|
expect(existsSyncMock).toHaveBeenCalledTimes(3)
|
||||||
expect(realpathNativeMock).toHaveBeenCalledTimes(1)
|
expect(realpathNativeMock).toHaveBeenCalledTimes(2)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -90,10 +90,10 @@ export async function handleWriteExistingFileGuardToolExecuteBefore(params: {
|
|||||||
output: { args?: unknown }
|
output: { args?: unknown }
|
||||||
readPermissionsBySession: Map<string, Set<string>>
|
readPermissionsBySession: Map<string, Set<string>>
|
||||||
sessionLastAccess: Map<string, number>
|
sessionLastAccess: Map<string, number>
|
||||||
canonicalSessionRoot: string
|
getCanonicalSessionRoot: () => string
|
||||||
maxTrackedSessions: number
|
maxTrackedSessions: number
|
||||||
}): Promise<void> {
|
}): 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()
|
const toolName = input.tool?.toLowerCase()
|
||||||
if (toolName !== "write" && toolName !== "read") {
|
if (toolName !== "write" && toolName !== "read") {
|
||||||
return
|
return
|
||||||
@@ -107,6 +107,7 @@ export async function handleWriteExistingFileGuardToolExecuteBefore(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const resolvedPath = resolveInputPath(ctx, filePath)
|
const resolvedPath = resolveInputPath(ctx, filePath)
|
||||||
|
const canonicalSessionRoot = getCanonicalSessionRoot()
|
||||||
const canonicalPath = toCanonicalPath(resolvedPath)
|
const canonicalPath = toCanonicalPath(resolvedPath)
|
||||||
if (!isPathInsideDirectory(canonicalPath, canonicalSessionRoot)) {
|
if (!isPathInsideDirectory(canonicalPath, canonicalSessionRoot)) {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user