Skip to content

Commit d1e30cf

Browse files
committed
fix(gray-screen): bound mock server input and cancel work on disconnect
- bind to loopback by default, cap request body size, set a request timeout - stop streaming when the client closes and only commit a scripted turn once delivered - cover task isolation of loaded originals in FileChangesPanel
1 parent 1bf5763 commit d1e30cf

3 files changed

Lines changed: 87 additions & 8 deletions

File tree

‎scripts/gray-screen/__tests__/tools.test.mjs‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe("mock-openai-server", () => {
171171
base = `http://127.0.0.1:${port}`
172172
child = spawn(
173173
process.execPath,
174-
[script("mock-openai-server.mjs"), "--scenario", "rapid", "--port", String(port), "--max-requests", "1"],
174+
[script("mock-openai-server.mjs"), "--scenario", "rapid", "--port", String(port), "--max-requests", "1", "--max-body-mb", "1", "--chunk-ms", "20"],
175175
{ stdio: ["ignore", "pipe", "inherit"] },
176176
)
177177
await new Promise((resolve, reject) => {
@@ -218,6 +218,32 @@ describe("mock-openai-server", () => {
218218
assert.match(body.choices[0].message.content, /Summary/)
219219
})
220220

221+
it("rejects a request body over --max-body-mb", async () => {
222+
const response = await complete({ messages: [{ role: "user", content: "x".repeat(2 * 1048576) }] }).catch(() => null)
223+
224+
assert.ok(response === null || response.status === 413)
225+
assert.equal((await fetch(`${base}/v1/models`)).status, 200)
226+
})
227+
228+
it("does not advance the scripted turn when the client cancels a stream", async () => {
229+
const request = { stream: true, messages: [{ role: "user", content: "go" }], tools: [{ type: "function", function: { name: "x" } }] }
230+
const controller = new AbortController()
231+
const cancelled = await fetch(`${base}/v1/chat/completions`, {
232+
method: "POST",
233+
headers: { "content-type": "application/json" },
234+
body: JSON.stringify(request),
235+
signal: controller.signal,
236+
})
237+
await cancelled.body.getReader().read()
238+
controller.abort()
239+
await new Promise((resolve) => setTimeout(resolve, 100))
240+
241+
const retry = await (await complete(request)).text()
242+
243+
assert.match(retry, /"tool_calls"/)
244+
assert.doesNotMatch(retry, /attempt_completion/)
245+
})
246+
221247
it("streams a scripted tool call, then attempt_completion once --max-requests is exceeded", async () => {
222248
const request = { stream: true, messages: [{ role: "user", content: "go" }], tools: [{ type: "function", function: { name: "x" } }] }
223249

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +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"
36+
import { integerFlag, validateRelativeDir } from "./lib.mjs"
3737

3838
const args = Object.fromEntries(
3939
process.argv.slice(2).reduce((acc, cur, i, all) => {
@@ -58,6 +58,8 @@ const cfg = {
5858
cmdLines: Number(args["cmd-lines"] ?? 400),
5959
cmdSleep: Number(args["cmd-sleep"] ?? 0.005),
6060
mermaidEvery: Number(args["mermaid-every"] ?? (args["scenario"] === "rapid" ? 0 : 10)),
61+
host: args["host"] ?? "127.0.0.1",
62+
maxBodyBytes: integerFlag("max-body-mb", args["max-body-mb"] ?? 256, 1) * 1048576,
6163
dir: (args["dir"] ?? ".mock-session").replace(/\/+$/, ""),
6264
}
6365

@@ -327,9 +329,11 @@ function sseChunk(res, id, delta, finishReason = null) {
327329
res.write(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: "mock", choices: [{ index: 0, delta, finish_reason: finishReason }] })}\n\n`)
328330
}
329331

332+
const clientGone = (res) => res.destroyed || res.writableEnded
333+
330334
async function streamText(res, id, text, field, chunkMs = cfg.chunkMs) {
331335
const r = rng(text.length)
332-
for (let i = 0; i < text.length; ) {
336+
for (let i = 0; i < text.length && !clientGone(res); ) {
333337
const size = cfg.chunkMin + Math.floor(r() * (cfg.chunkMax - cfg.chunkMin + 1))
334338
sseChunk(res, id, { [field]: text.slice(i, i + size) })
335339
i += size
@@ -360,11 +364,13 @@ async function handleCompletion(req, res, body) {
360364
return
361365
}
362366

363-
const n = ++requestCount
367+
// The scripted turn is only committed once the reply was delivered, so a cancelled request followed by a retry replays the same turn.
368+
const n = requestCount + 1
364369
const plan = planFor(n)
365370
console.log(`[mock] turn #${n} tool=${plan.tool.name} messages=${messages.length} bodyBytes=${JSON.stringify(body).length}`)
366371

367372
if (body.stream !== true) {
373+
requestCount = n
368374
res.writeHead(200, { "content-type": "application/json" })
369375
res.end(JSON.stringify({ id, object: "chat.completion", created: Math.floor(Date.now() / 1000), model: "mock", choices: [{ index: 0, message: { role: "assistant", content: markdownFor(n, Math.floor((n - 1) / CYCLE.length), plan), tool_calls: [{ id: `call_${n}`, type: "function", function: { name: plan.tool.name, arguments: JSON.stringify(plan.tool.args) } }] }, finish_reason: "tool_calls" }], usage: { prompt_tokens: 100, completion_tokens: 30, total_tokens: 130 } }))
370376
return
@@ -378,10 +384,12 @@ async function handleCompletion(req, res, body) {
378384

379385
const argsJson = JSON.stringify(plan.tool.args)
380386
sseChunk(res, id, { tool_calls: [{ index: 0, id: `call_${n}`, type: "function", function: { name: plan.tool.name, arguments: "" } }] })
381-
for (let i = 0; i < argsJson.length; i += cfg.toolChunk) {
387+
for (let i = 0; i < argsJson.length && !clientGone(res); i += cfg.toolChunk) {
382388
sseChunk(res, id, { tool_calls: [{ index: 0, function: { arguments: argsJson.slice(i, i + cfg.toolChunk) } }] })
383389
if (cfg.chunkMs > 0) await sleep(Math.max(1, cfg.chunkMs / 3))
384390
}
391+
if (clientGone(res)) return
392+
requestCount = n
385393
sseChunk(res, id, {}, "tool_calls")
386394
const promptTokens = 1000 + messages.length * 400
387395
res.write(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: "mock", choices: [], usage: { prompt_tokens: promptTokens, completion_tokens: 400, total_tokens: promptTokens + 400 } })}\n\n`)
@@ -398,8 +406,22 @@ const server = http.createServer((req, res) => {
398406
}
399407
if (req.method === "POST" && url.pathname.endsWith("/chat/completions")) {
400408
const chunks = []
401-
req.on("data", (c) => chunks.push(c))
409+
let size = 0
410+
let tooLarge = false
411+
req.on("data", (c) => {
412+
if (tooLarge) return
413+
size += c.length
414+
if (size > cfg.maxBodyBytes) {
415+
tooLarge = true
416+
chunks.length = 0
417+
res.writeHead(413, { connection: "close" }).end("request body too large")
418+
req.destroy()
419+
return
420+
}
421+
chunks.push(c)
422+
})
402423
req.on("end", () => {
424+
if (tooLarge) return
403425
let body = {}
404426
try {
405427
body = JSON.parse(Buffer.concat(chunks).toString("utf8"))
@@ -414,6 +436,7 @@ const server = http.createServer((req, res) => {
414436
res.writeHead(404).end()
415437
})
416438

417-
server.listen(cfg.port, "0.0.0.0", () => {
418-
console.log(`[mock] OpenAI-compatible server listening on 0.0.0.0:${cfg.port} -> use http://127.0.0.1:${cfg.port}/v1 (scenario=${cfg.scenario}, maxRequests=${cfg.maxRequests}, sandbox=${cfg.dir}/)`)
439+
server.requestTimeout = 120_000
440+
server.listen(cfg.port, cfg.host, () => {
441+
console.log(`[mock] OpenAI-compatible server listening on ${cfg.host}:${cfg.port} -> use http://127.0.0.1:${cfg.port}/v1 (scenario=${cfg.scenario}, maxRequests=${cfg.maxRequests}, sandbox=${cfg.dir}/)`)
419442
})

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,36 @@ describe("FileChangesPanel", () => {
418418
expect(screen.getByTestId("accordian-code")).toHaveTextContent("-old line")
419419
})
420420

421+
it("does not show a loaded original of the previous task for an entry with the same key in the next task", () => {
422+
const messages = [createEditWithOriginal({ originalContentLength: 5000 })]
423+
const { rerender } = renderPanel(messages, "task-A")
424+
expandRow()
425+
respond({ type: "fileContent", fileContent: { path: "src/foo.ts", content: "new line\n" } })
426+
respond({
427+
type: "originalContent",
428+
originalContentInfo: { ts: TS, taskId: "task-A", content: "old line of task A\n" },
429+
})
430+
expect(screen.getByTestId("accordian-code")).toHaveTextContent("-old line of task A")
431+
432+
rerender(
433+
<TranslationProvider>
434+
<FileChangesPanel clineMessages={[...messages]} taskId="task-B" />
435+
</TranslationProvider>,
436+
)
437+
fireEvent.click(screen.getByTestId("accordian-toggle"))
438+
respond({ type: "fileContent", fileContent: { path: "src/foo.ts", content: "new line\n" } })
439+
440+
expect(requestsOfType("readOriginalContent").filter((m) => m.taskId === "task-B")).toHaveLength(1)
441+
expect(screen.getByTestId("accordian-code")).not.toHaveTextContent("old line of task A")
442+
expect(screen.getByTestId("accordian-code")).toHaveTextContent("the recorded diff")
443+
444+
respond({
445+
type: "originalContent",
446+
originalContentInfo: { ts: TS, taskId: "task-B", content: "old line of task B\n" },
447+
})
448+
expect(screen.getByTestId("accordian-code")).toHaveTextContent("-old line of task B")
449+
})
450+
421451
it("ignores an original answered for a different task", () => {
422452
renderPanel([createEditWithOriginal({ originalContentLength: 5000 })], "task-1")
423453
expandRow()

0 commit comments

Comments
 (0)