Skip to content

Commit ae5662a

Browse files
committed
fix: address review on original-content fetch and gray-screen tooling
Keep loaded originals across message updates and skip requests for rows expanded under a previous task. Pass currentTaskId to FileChangesPanel. Move shared tooling helpers to lib.mjs: validate numeric flags and --dir (no "." or option-like segments), stage generated tasks in a separate directory before renaming them into place, and add node:test coverage.
1 parent 71c8fb6 commit ae5662a

9 files changed

Lines changed: 266 additions & 68 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import assert from "node:assert/strict"
2+
import fs from "node:fs"
3+
import os from "node:os"
4+
import path from "node:path"
5+
import { afterEach, beforeEach, describe, it } from "node:test"
6+
7+
import { integerFlag, parseFlagArgs, resolveServedFile, validateRelativeDir, writeTaskAtomically } from "../lib.mjs"
8+
9+
let tmp
10+
11+
beforeEach(() => {
12+
tmp = fs.mkdtempSync(path.join(os.tmpdir(), "gray-screen-test-"))
13+
})
14+
15+
afterEach(() => {
16+
fs.rmSync(tmp, { recursive: true, force: true })
17+
})
18+
19+
describe("parseFlagArgs / integerFlag", () => {
20+
it("parses flag/value pairs", () => {
21+
assert.deepEqual(parseFlagArgs(["--messages", "10", "--two-byte", "true"]), { messages: "10", "two-byte": "true" })
22+
})
23+
24+
it("rejects a flag with a missing operand", () => {
25+
assert.throws(() => parseFlagArgs(["--messages", "--text-bytes", "5"]), /Missing value for --messages/)
26+
assert.throws(() => parseFlagArgs(["--messages"]), /Missing value for --messages/)
27+
})
28+
29+
it("accepts integers at or above the minimum and rejects everything else", () => {
30+
assert.equal(integerFlag("messages", "5000", 1), 5000)
31+
assert.equal(integerFlag("image-every", "0", 0), 0)
32+
for (const bad of ["0", "-3", "1.5", "abc", "", "NaN"]) {
33+
assert.throws(() => integerFlag("messages", bad, 1), /--messages must be an integer >= 1/)
34+
}
35+
})
36+
})
37+
38+
describe("validateRelativeDir", () => {
39+
it("accepts plain relative paths", () => {
40+
for (const ok of [".mock-session", "a", "src/mock_1", "a.b/c-d"]) assert.equal(validateRelativeDir(ok), ok)
41+
})
42+
43+
it("rejects shell metacharacters, absolute paths, traversal, dot and option-like segments", () => {
44+
const bad = ['safe"; touch /tmp/pwned; #', "a b", "$(id)", "/abs", "../x", "a/../b", ".", "a/./b", "-rf", "a/-rf", "", "a//b"]
45+
for (const dir of bad) assert.throws(() => validateRelativeDir(dir), /Invalid --dir/, JSON.stringify(dir))
46+
})
47+
})
48+
49+
describe("resolveServedFile", () => {
50+
beforeEach(() => {
51+
fs.mkdirSync(path.join(tmp, "build", "assets"), { recursive: true })
52+
fs.mkdirSync(path.join(tmp, "build-secret"))
53+
fs.writeFileSync(path.join(tmp, "build", "index.html"), "index")
54+
fs.writeFileSync(path.join(tmp, "build", "assets", "app.js"), "js")
55+
fs.writeFileSync(path.join(tmp, "build-secret", "credentials.json"), "secret")
56+
fs.symlinkSync(path.join(tmp, "build-secret"), path.join(tmp, "build", "link"))
57+
})
58+
59+
const root = () => path.join(tmp, "build")
60+
61+
it("serves files inside the build directory, with / mapping to index.html", () => {
62+
assert.equal(resolveServedFile(root(), "/"), fs.realpathSync(path.join(root(), "index.html")))
63+
assert.equal(resolveServedFile(root(), "/assets/app.js"), fs.realpathSync(path.join(root(), "assets", "app.js")))
64+
})
65+
66+
it("returns undefined for traversal (plain, encoded and sibling-prefix), symlink escapes, directories and bad input", () => {
67+
for (const p of [
68+
"/../build-secret/credentials.json",
69+
"/%2e%2e%2fbuild-secret%2fcredentials.json",
70+
"/link/credentials.json",
71+
"/assets",
72+
"/missing.js",
73+
"/%E0%A4%A",
74+
"/a\0b",
75+
]) {
76+
assert.equal(resolveServedFile(root(), p), undefined, p)
77+
}
78+
})
79+
80+
it("returns undefined when the build directory does not exist", () => {
81+
assert.equal(resolveServedFile(path.join(tmp, "nope"), "/"), undefined)
82+
})
83+
})
84+
85+
describe("writeTaskAtomically", () => {
86+
it("writes every file into tasks/<id> and leaves no staging directory", () => {
87+
const taskDir = writeTaskAtomically(tmp, "t1", { "a.json": { a: 1 }, "b.json": [2] })
88+
89+
assert.equal(taskDir, path.join(tmp, "tasks", "t1"))
90+
assert.deepEqual(JSON.parse(fs.readFileSync(path.join(taskDir, "a.json"), "utf8")), { a: 1 })
91+
assert.deepEqual(JSON.parse(fs.readFileSync(path.join(taskDir, "b.json"), "utf8")), [2])
92+
assert.deepEqual(fs.readdirSync(tmp).sort(), ["tasks"])
93+
})
94+
95+
it("leaves neither a task directory nor staging files when a write fails", () => {
96+
const circular = {}
97+
circular.self = circular
98+
99+
assert.throws(() => writeTaskAtomically(tmp, "t2", { "ok.json": { ok: true }, "bad.json": circular }), /circular/i)
100+
101+
assert.equal(fs.existsSync(path.join(tmp, "tasks", "t2")), false)
102+
assert.deepEqual(fs.readdirSync(tmp), [])
103+
})
104+
})

‎scripts/gray-screen/generate-large-task.mjs‎

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@ import fs from "node:fs"
99
import os from "node:os"
1010
import path from "node:path"
1111
import crypto from "node:crypto"
12+
import { integerFlag, parseFlagArgs, writeTaskAtomically } from "./lib.mjs"
1213

13-
const args = Object.fromEntries(
14-
process.argv.slice(2).reduce((acc, cur, i, all) => {
15-
if (cur.startsWith("--")) acc.push([cur.slice(2), all[i + 1]])
16-
return acc
17-
}, []),
18-
)
14+
const args = parseFlagArgs(process.argv.slice(2))
1915

20-
const messageCount = Number(args["messages"] ?? 5000)
21-
const textBytes = Number(args["text-bytes"] ?? 800)
22-
const toolBytes = Number(args["tool-bytes"] ?? textBytes)
16+
// Validate every numeric flag up front, before anything is written.
17+
const messageCount = integerFlag("messages", args["messages"] ?? 5000, 1)
18+
const textBytes = integerFlag("text-bytes", args["text-bytes"] ?? 800, 1)
19+
const toolBytes = integerFlag("tool-bytes", args["tool-bytes"] ?? textBytes, 1)
2320
const toolKind = args["tool-kind"] ?? "mixed"
2421
const batchable = args["batchable"] === "true"
2522
const twoByte = args["two-byte"] === "true" // Korean chars in tool text -> UTF-16 strings (2 bytes/char) in V8
26-
const imageEvery = Number(args["image-every"] ?? 0)
27-
const imageKb = Number(args["image-kb"] ?? 200)
23+
const imageEvery = integerFlag("image-every", args["image-every"] ?? 0, 0)
24+
const imageKb = integerFlag("image-kb", args["image-kb"] ?? 200, 1)
2825
const workspace = args["workspace"] ?? process.cwd()
2926
const storageCandidates = [
3027
path.join(os.homedir(), ".vscode-server", "data", "User", "globalStorage", "codemate.zoo-code"),
@@ -33,8 +30,6 @@ const storageCandidates = [
3330
const storage = args["storage"] ?? storageCandidates.find((p) => fs.existsSync(p)) ?? storageCandidates[1]
3431

3532
const taskId = crypto.randomUUID()
36-
const taskDir = path.join(storage, "tasks", taskId)
37-
fs.mkdirSync(taskDir, { recursive: true })
3833

3934
const filler = (n, seed) => {
4035
const base = `line ${seed}: ${twoByte ? "\uD55C\uAE00 " : ""}The quick brown fox jumps over the lazy dog. `
@@ -104,20 +99,12 @@ const historyItem = {
10499
status: "completed",
105100
}
106101

107-
// Write every file under a temporary name and rename only after all writes succeeded, so an interrupted run
108-
// (or a full disk) never leaves a truncated or partial task; history_item.json goes last.
109-
const taskFiles = [
110-
["ui_messages.json", messages],
111-
["api_conversation_history.json", apiHistory],
112-
["history_item.json", historyItem],
113-
]
114-
try {
115-
for (const [name, value] of taskFiles) fs.writeFileSync(path.join(taskDir, `${name}.tmp`), JSON.stringify(value))
116-
for (const [name] of taskFiles) fs.renameSync(path.join(taskDir, `${name}.tmp`), path.join(taskDir, name))
117-
} catch (error) {
118-
fs.rmSync(taskDir, { recursive: true, force: true })
119-
throw error
120-
}
102+
// The task becomes visible under tasks/<id> only once all three files are written.
103+
const taskDir = writeTaskAtomically(storage, taskId, {
104+
"ui_messages.json": messages,
105+
"api_conversation_history.json": apiHistory,
106+
"history_item.json": historyItem,
107+
})
121108

122109
const mb = (f) => (fs.statSync(path.join(taskDir, f)).size / 1048576).toFixed(1)
123110
console.log(`Task ${taskId}: ${messages.length} messages, ui_messages.json ${mb("ui_messages.json")} MB`)

‎scripts/gray-screen/lib.mjs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Shared helpers for the gray-screen tooling. Tests: node --test scripts/gray-screen/__tests__/lib.test.mjs
2+
import fs from "node:fs"
3+
import path from "node:path"
4+
5+
/** `--flag value` pairs; a flag without a value (or followed by another flag) is an error. */
6+
export function parseFlagArgs(argv) {
7+
const entries = []
8+
9+
argv.forEach((cur, i) => {
10+
if (!cur.startsWith("--")) return
11+
const value = argv[i + 1]
12+
if (value === undefined || value.startsWith("--")) throw new Error(`Missing value for ${cur}`)
13+
entries.push([cur.slice(2), value])
14+
})
15+
16+
return Object.fromEntries(entries)
17+
}
18+
19+
export function integerFlag(name, value, min) {
20+
const number = Number(value)
21+
if (!Number.isInteger(number) || number < min) throw new Error(`--${name} must be an integer >= ${min}`)
22+
return number
23+
}
24+
25+
/** A plain relative path that is safe to interpolate into shell commands and to resolve under a workspace. */
26+
export function validateRelativeDir(dir) {
27+
const segments = dir.split("/")
28+
const isSafe =
29+
/^[A-Za-z0-9._-]+(\/[A-Za-z0-9._-]+)*$/.test(dir) &&
30+
segments.every((part) => part !== "." && part !== ".." && !part.startsWith("-"))
31+
32+
if (!isSafe) {
33+
throw new Error(
34+
`Invalid --dir "${dir}": use a relative path of letters, digits, ".", "_" and "-" without "." or ".." segments and without segments starting with "-".`,
35+
)
36+
}
37+
38+
return dir
39+
}
40+
41+
/** Resolves a request path to a regular file inside `root` (after symlink resolution), or undefined. */
42+
export function resolveServedFile(root, rawPath) {
43+
try {
44+
const realRoot = fs.realpathSync(root)
45+
const decoded = decodeURIComponent(rawPath)
46+
const real = fs.realpathSync(path.resolve(realRoot, "." + (decoded === "/" ? "/index.html" : decoded)))
47+
const rel = path.relative(realRoot, real)
48+
if (rel === "" || rel === ".." || rel.startsWith(".." + path.sep) || path.isAbsolute(rel)) return undefined
49+
return fs.statSync(real).isFile() ? real : undefined
50+
} catch {
51+
return undefined
52+
}
53+
}
54+
55+
/**
56+
* Writes a task into `<storage>/tasks/<taskId>` all-or-nothing: the files are written into a staging directory
57+
* next to `tasks/`, which is renamed into place only after every write succeeded.
58+
* `files` maps a file name to the value to serialize. Returns the final task directory.
59+
*/
60+
export function writeTaskAtomically(storage, taskId, files) {
61+
const staging = path.join(storage, `.task-staging-${taskId}`)
62+
const taskDir = path.join(storage, "tasks", taskId)
63+
64+
try {
65+
fs.mkdirSync(staging, { recursive: true })
66+
for (const [name, value] of Object.entries(files)) {
67+
fs.writeFileSync(path.join(staging, name), JSON.stringify(value))
68+
}
69+
fs.mkdirSync(path.dirname(taskDir), { recursive: true })
70+
fs.renameSync(staging, taskDir)
71+
} catch (error) {
72+
fs.rmSync(staging, { recursive: true, force: true })
73+
throw error
74+
}
75+
76+
return taskDir
77+
}

‎scripts/gray-screen/mock-openai-server.mjs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
// Zoo Code settings: provider "OpenAI Compatible", base URL http://127.0.0.1:<port>/v1, any API key, model id "mock",
3434
// context window 1000000 (avoid condensing); auto-approve read/write/execute with allowed command "*".
3535
import http from "node:http"
36+
import { validateRelativeDir } from "./lib.mjs"
3637

3738
const args = Object.fromEntries(
3839
process.argv.slice(2).reduce((acc, cur, i, all) => {
@@ -61,8 +62,10 @@ const cfg = {
6162
}
6263

6364
// cfg.dir is interpolated into the shell commands the mock asks the extension to run, so only plain relative paths are allowed.
64-
if (!/^[A-Za-z0-9._-]+(\/[A-Za-z0-9._-]+)*$/.test(cfg.dir) || cfg.dir.split("/").some((part) => part === "..")) {
65-
console.error(`Invalid --dir "${cfg.dir}": use a relative path of letters, digits, ".", "_" and "-" without ".." segments.`)
65+
try {
66+
validateRelativeDir(cfg.dir)
67+
} catch (error) {
68+
console.error(error.message)
6669
process.exit(1)
6770
}
6871

‎scripts/gray-screen/webview-heap-matrix.mjs‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import http from "node:http"
1111
import { createRequire } from "node:module"
1212
import path from "node:path"
1313
import { fileURLToPath } from "node:url"
14+
import { resolveServedFile } from "./lib.mjs"
1415

1516
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..")
1617
const require = createRequire(path.join(root, "webview-ui", "package.json"))
@@ -68,21 +69,8 @@ const EXPERIMENTS = [
6869
]
6970

7071
const types = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".json": "application/json", ".wasm": "application/wasm", ".svg": "image/svg+xml", ".map": "application/json", ".woff2": "font/woff2", ".ttf": "font/ttf" }
71-
const serveRoot = fs.realpathSync(buildDir)
72-
// Resolves a request path to a regular file inside serveRoot (after symlink resolution), or undefined.
73-
const resolveServedFile = (rawPath) => {
74-
try {
75-
const decoded = decodeURIComponent(rawPath)
76-
const real = fs.realpathSync(path.resolve(serveRoot, "." + (decoded === "/" ? "/index.html" : decoded)))
77-
const rel = path.relative(serveRoot, real)
78-
if (rel === "" || rel === ".." || rel.startsWith(".." + path.sep) || path.isAbsolute(rel)) return undefined
79-
return fs.statSync(real).isFile() ? real : undefined
80-
} catch {
81-
return undefined
82-
}
83-
}
8472
const httpServer = http.createServer((req, res) => {
85-
const file = resolveServedFile(new URL(req.url, "http://x").pathname)
73+
const file = resolveServedFile(buildDir, new URL(req.url, "http://x").pathname)
8674
if (!file) return void res.writeHead(404).end()
8775
res.writeHead(200, {
8876
"content-type": types[path.extname(file)] ?? "application/octet-stream",

‎scripts/gray-screen/webview-render-stress.mjs‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import http from "node:http"
2828
import { createRequire } from "node:module"
2929
import path from "node:path"
3030
import { fileURLToPath } from "node:url"
31+
import { resolveServedFile } from "./lib.mjs"
3132

3233
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..")
3334
const webviewDir = path.join(root, "webview-ui")
@@ -78,21 +79,8 @@ async function startServer() {
7879
})
7980
}
8081
const types = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".json": "application/json", ".wasm": "application/wasm", ".svg": "image/svg+xml", ".map": "application/json", ".woff2": "font/woff2", ".ttf": "font/ttf" }
81-
const serveRoot = fs.realpathSync(buildDir)
82-
// Resolves a request path to a regular file inside serveRoot (after symlink resolution), or undefined.
83-
const resolveServedFile = (rawPath) => {
84-
try {
85-
const decoded = decodeURIComponent(rawPath)
86-
const real = fs.realpathSync(path.resolve(serveRoot, "." + (decoded === "/" ? "/index.html" : decoded)))
87-
const rel = path.relative(serveRoot, real)
88-
if (rel === "" || rel === ".." || rel.startsWith(".." + path.sep) || path.isAbsolute(rel)) return undefined
89-
return fs.statSync(real).isFile() ? real : undefined
90-
} catch {
91-
return undefined
92-
}
93-
}
9482
const httpServer = http.createServer((req, res) => {
95-
const file = resolveServedFile(new URL(req.url, "http://x").pathname)
83+
const file = resolveServedFile(buildDir, new URL(req.url, "http://x").pathname)
9684
if (!file) {
9785
res.writeHead(404).end()
9886
return

‎webview-ui/src/__tests__/FileChangesPanel.spec.tsx‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,44 @@ describe("FileChangesPanel", () => {
380380
expect(requestsOfType("readOriginalContent").filter((m) => m.taskId === "task-A")).toHaveLength(2)
381381
})
382382

383+
it("does not request for rows expanded under the previous task when the task id changes", () => {
384+
const messages = [createEditWithOriginal({ originalContentLength: 5000 })]
385+
const { rerender } = renderPanel(messages, "task-A")
386+
expandRow()
387+
mockPostMessage.mockClear()
388+
389+
rerender(
390+
<TranslationProvider>
391+
<FileChangesPanel clineMessages={messages} taskId="task-B" />
392+
</TranslationProvider>,
393+
)
394+
395+
expect(mockPostMessage).not.toHaveBeenCalled()
396+
})
397+
398+
it("keeps a loaded original when the messages are replaced by an update of the same task", () => {
399+
const first = [createEditWithOriginal({ originalContentLength: 5000 })]
400+
const { rerender } = renderPanel(first, "task-A")
401+
expandRow()
402+
respond({ type: "fileContent", fileContent: { path: "src/foo.ts", content: "new line\n" } })
403+
respond({
404+
type: "originalContent",
405+
originalContentInfo: { ts: TS, taskId: "task-A", content: "old line\n" },
406+
})
407+
expect(requestsOfType("readOriginalContent")).toHaveLength(1)
408+
409+
rerender(
410+
<TranslationProvider>
411+
<FileChangesPanel clineMessages={[...first]} taskId="task-A" />
412+
</TranslationProvider>,
413+
)
414+
fireEvent.click(screen.getByTestId("accordian-toggle"))
415+
respond({ type: "fileContent", fileContent: { path: "src/foo.ts", content: "new line\n" } })
416+
417+
expect(requestsOfType("readOriginalContent")).toHaveLength(1)
418+
expect(screen.getByTestId("accordian-code")).toHaveTextContent("-old line")
419+
})
420+
383421
it("ignores an original answered for a different task", () => {
384422
renderPanel([createEditWithOriginal({ originalContentLength: 5000 })], "task-1")
385423
expandRow()

‎webview-ui/src/components/chat/ChatView.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
17381738
atBottomThreshold={10}
17391739
/>
17401740
</div>
1741-
<FileChangesPanel clineMessages={messages} taskId={currentTaskItem?.id} />
1741+
<FileChangesPanel clineMessages={messages} taskId={currentTaskId} />
17421742
{areButtonsVisible && (
17431743
<div
17441744
className={`flex h-9 items-center mb-1 px-[15px] ${

0 commit comments

Comments
 (0)