Skip to content

Commit 69776b1

Browse files
fix(tools): keep host paths out of guarded-write rejections (U7, #1375)
The dangling-link rejection interpolated the guard's own absolute ancestor into model-facing text, while every other rejection in the same file names only the caller's own spelling. Name the link in the caller's spelling instead, and assert in the spec that the rejection carries no absolute workspace path. The queue title spec claimed it renders each queued message's text but asserted only the heading above the queue, so assert the queued text itself. Answers review comment 4563093440 (guardedWrite.ts 464-467) and review comment 4568686685 (QueuedMessages.title.spec.tsx 22) on pull 1918.
1 parent c83e96c commit 69776b1

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎src/core/tools/__tests__/guardedWrite.spec.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,16 @@ describe("guardedWrite (S4a, epic #1375)", () => {
365365
})
366366
const task = createMockTask()
367367

368-
await expect(guardedWrite(task, "linkdir/nested/new.txt", "data", "create")).rejects.toThrow(
369-
"runs through a link",
370-
)
368+
let rejection: Error | undefined
369+
try {
370+
await guardedWrite(task, "linkdir/nested/new.txt", "data", "create")
371+
} catch (error: unknown) {
372+
rejection = error instanceof Error ? error : new Error(String(error))
373+
}
374+
// The rejection names the link in the caller's own spelling and carries none
375+
// of the guard's absolute ancestor, which is a host path on the user's machine.
376+
expect(rejection?.message).toContain("runs through a link (linkdir)")
377+
expect(rejection?.message).not.toContain(path.resolve(WORKSPACE))
371378

372379
expect(mockedSafeWriteText).not.toHaveBeenCalled()
373380
})

‎src/core/tools/guardedWrite.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,17 @@ async function realpathNearest(target: string, displayPath: string): Promise<str
461461
// directory this check authorized.
462462
const asLink = await fs.lstat(ancestor, { bigint: true }).catch(() => undefined)
463463
if (asLink?.isSymbolicLink()) {
464+
// This rejection is model-facing text. The guard's own absolute ancestor is a
465+
// host path on the user's machine, and every other rejection here keeps
466+
// host paths out of the model's context. The walk already collected the
467+
// components below the link, so cutting them off the caller's own
468+
// spelling names the link the way the caller named it.
469+
const belowLink = missing.slice().reverse()
470+
const spelled = displayPath.split(/[\\/]/)
471+
const keep = Math.max(1, spelled.length - belowLink.length)
472+
const linkName = spelled.slice(0, keep).join("/") || path.basename(ancestor)
464473
throw new GuardRejectedError(
465-
`Path runs through a link (${ancestor}) that does not resolve -- retry with a path inside the workspace.`,
474+
`Path runs through a link (${linkName}) that does not resolve -- retry with a path inside the workspace.`,
466475
displayPath,
467476
)
468477
}

‎webview-ui/src/components/chat/__tests__/QueuedMessages.title.spec.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ describe("QueuedMessages - the heading it puts over the queue", () => {
2020

2121
expect(tSpy).toHaveBeenCalledWith("chat:queuedMessages.title")
2222
expect(screen.getByTestId("queued-messages")).toHaveTextContent("chat:queuedMessages.title")
23+
// The heading alone does not prove the queue hands each message's own text to
24+
// the render, so assert the queued text itself, not only the title above it.
25+
expect(screen.getByTestId("queued-messages")).toHaveTextContent("keep the diff open")
2326
})
2427
})

0 commit comments

Comments
 (0)