Skip to content

Commit 8ae1974

Browse files
easonLiangWorldedtechZoo-Code campaign u5a
andcommitted
chore(u5a): refresh 2357 onto trunk tip 5f42a91
Single parent, the trunk tip, so the instrument prices only the content this pull itself changes rather than everything a merge would carry. The conflicted spec hunk is resolved by taking the trunk side of both added tie blocks, whose tie assertions are strictly stronger than the pair of contains-dir assertions this branch held, and by keeping the reader-failure rows the trunk does not carry, which assert the ENOENT the swallowing reader hands back and the EIO trace detail. Co-authored-by: Zoo-Code campaign u5a <[email protected]>
1 parent 5f42a91 commit 8ae1974

24 files changed

Lines changed: 6571 additions & 75 deletions

‎knip.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
"playwright-ct": false,
1717
"workspaces": {
1818
"src": {
19-
"entry": ["extension.ts!", "extension/api.ts!", "workers/countTokens.ts!"],
19+
"entry": [
20+
"extension.ts!",
21+
"extension/api.ts!",
22+
"workers/countTokens.ts!",
23+
"services/file-safety/safeWriteText.ts!"
24+
],
2025
"project": [
2126
"**/*.ts!",
2227
"!**/__tests__/**!",

‎src/core/task-persistence/__tests__/readFileWithMissingRetry.spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as fs from "fs/promises"
22
import * as os from "os"
33
import * as path from "path"
44

5+
import * as recovery from "../../../utils/jsonFileRecovery"
56
import { readFileWithMissingRetry } from "../readFileWithMissingRetry"
67

78
describe("readFileWithMissingRetry", () => {
@@ -26,4 +27,43 @@ describe("readFileWithMissingRetry", () => {
2627
it("propagates non-ENOENT errors", async () => {
2728
await expect(readFileWithMissingRetry(dir)).rejects.toMatchObject({ code: "EISDIR" })
2829
})
30+
31+
// The callers of this reader classify the failure they receive by error code:
32+
// apiMessages.ts maps ENOENT to "no file yet" and anything else to io_error,
33+
// taskMessages.ts maps it to the "not_found" kind, and TaskHistoryStore.ts swallows
34+
// it into null. A recovery that rejects must therefore not reach the caller as the
35+
// read's error - the reader's own ENOENT is the failure those classifiers know.
36+
it("reports the missing file, not the recovery failure, when recovery itself rejects", async () => {
37+
const missing = path.join(dir, "api_mock_latest.json")
38+
const recoveryError = Object.assign(new Error("recovery scan failed"), { code: "EBUSY" })
39+
// The recovery puts the file back and then fails - it loses the lock too late to
40+
// report success. A rejection still cannot be read as "recovered": the reader cannot
41+
// tell whether the recovery finished, and every caller classifies this read by error
42+
// code. If the rejection counted as a recovery, the reader would return the planted
43+
// bytes and the row below would pass on a third read.
44+
const recoverySpy = vi.spyOn(recovery, "recoverMissingJsonFile").mockImplementation(async () => {
45+
await fs.writeFile(missing, JSON.stringify({ recoveredBySomeoneElse: true }))
46+
throw recoveryError
47+
})
48+
try {
49+
let failure: unknown
50+
const value = await readFileWithMissingRetry(missing).catch((reason: unknown) => {
51+
failure = reason
52+
return undefined
53+
})
54+
// The rejection carries EBUSY, which every caller above would report as an I/O
55+
// error on a file that is simply not there yet; the reader's own failure is the
56+
// ENOENT it already saw, and a resolved read of the planted bytes would be a third
57+
// outcome neither the caller nor this row expects.
58+
expect(failure).toBeDefined()
59+
expect(value).toBeUndefined()
60+
expect(failure).not.toBe(recoveryError)
61+
expect(failure).toMatchObject({ code: "ENOENT" })
62+
// The recovery was attempted exactly once, so the missing-file retry the rows
63+
// above assert is unchanged.
64+
expect(recoverySpy).toHaveBeenCalledTimes(1)
65+
} finally {
66+
recoverySpy.mockRestore()
67+
}
68+
})
2969
})

‎src/core/task-persistence/readFileWithMissingRetry.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ export async function readFileWithMissingRetry(filePath: string): Promise<string
4646
return await fs.readFile(filePath, "utf8")
4747
} catch (retryError) {
4848
if (getErrorCode(retryError) !== "ENOENT") throw retryError
49-
if (!(await recoverMissingJsonFile(filePath))) throw retryError
49+
// A recovery that rejects failed for its own reasons - a readdir error, a lock it could
50+
// not take - and jsonFileRecovery traces that failure with its code before throwing.
51+
// It is not the reader's failure: the caller can only classify the read it asked for,
52+
// so an unreachable recovery counts as "nothing recovered" and the ENOENT read above
53+
// is what the caller receives.
54+
const recovered = await recoverMissingJsonFile(filePath).catch(() => false)
55+
if (!recovered) throw retryError
5056
return fs.readFile(filePath, "utf8")
5157
}
5258
}

‎src/core/task/Task.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,6 +3624,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
36243624
// A host teardown never calls abortTask(): disposal has to raise the same
36253625
// counter, or a write still parked on its path chain publishes after the
36263626
// task is gone.
3627+
// Disposal drives the abort latch through the kernel, which sets abort and disposed
3628+
// together on disposeRequested in src/core/task/run-state/runState.ts, so this
3629+
// increment is what keeps a disposed task's queued writes out of a later run.
36273630
this.cancellationGeneration++
36283631
this.driveRunEvent({ tag: "disposeRequested" })
36293632
}

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4820,6 +4820,38 @@ describe("Cline", () => {
48204820
expect(task.cancellationGeneration).toBe(1)
48214821
})
48224822

4823+
it("advances the cancellation generation for the disposal an unmocked abort performs", async () => {
4824+
// The two rows above mock dispose(), so each proves one increment in isolation. The
4825+
// sequence the guarded-write fence actually sees is an abort that tears the task down:
4826+
// requestAbort raises the fence, and the dispose() that abortTaskOnce drives raises it
4827+
// again. Nothing is mocked here, so if either increment stopped landing - or if disposal
4828+
// stopped being part of an abort - this row reads the stale value.
4829+
const task = new Task({
4830+
provider: mockProvider,
4831+
apiConfiguration: mockApiConfig,
4832+
task: "test task",
4833+
startTask: false,
4834+
})
4835+
4836+
expect(task.cancellationGeneration).toBe(0)
4837+
4838+
// Started, not awaited. requestAbort is synchronous and raises the fence before any of the
4839+
// abort work runs (Task.abortTask calls it before it hands back abortPromise), so the increment
4840+
// has to be visible while the abort promise is still pending. Asserting the generation only
4841+
// after the await would still pass if the increment landed at the end of the teardown, which is
4842+
// exactly the delay that lets a write parked on its path chain publish after the task is gone.
4843+
const aborting = task.abortTask()
4844+
expect(task.cancellationGeneration).toBe(1)
4845+
4846+
await aborting
4847+
4848+
expect(task.abort).toBe(true)
4849+
// Disposal never marks a task abandoned; only an explicit abandon does.
4850+
expect(task.abandoned).toBe(false)
4851+
// One from requestAbort, one from the disposal the abort performed.
4852+
expect(task.cancellationGeneration).toBe(2)
4853+
})
4854+
48234855
it("flushes pending state before TaskAborted and disposal while queue state is intact", async () => {
48244856
const task = new Task({
48254857
provider: mockProvider,

‎src/core/tools/ReadFileTool.ts‎

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ import type { ReadFileParams, ReadFileMode, ReadFileToolParams, FileEntry, LineR
1717
import { isLegacyReadFileParams, type ClineSayTool } from "@roo-code/types"
1818

1919
import { Task } from "../task/Task"
20+
import { versionTokenOfStat } from "../../utils/versionToken"
2021
import { formatResponse } from "../prompts/responses"
2122
import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
2223
import { isPathOutsideWorkspace } from "../../utils/pathUtils"
2324
import { getReadablePath } from "../../utils/path"
2425
import { extractTextFromFile, addLineNumbers, getSupportedBinaryFormats } from "../../integrations/misc/extract-text"
2526
import { readWithIndentation, readWithSlice } from "../../integrations/misc/indentation-reader"
26-
import { DEFAULT_LINE_LIMIT } from "../prompts/tools/native-tools/read_file"
27+
import { DEFAULT_LINE_LIMIT, MAX_LINE_LENGTH } from "../prompts/tools/native-tools/read_file"
2728
import type { ToolUse, PushToolResult } from "../../shared/tools"
2829

2930
import {
@@ -215,14 +216,49 @@ export class ReadFileTool extends BaseTool<"read_file"> {
215216
// Read text file content with lossy UTF-8 conversion
216217
// Reading as Buffer first allows graceful handling of non-UTF8 bytes
217218
// (they become U+FFFD replacement characters instead of throwing)
219+
// A2 (epic #1375): capture the on-disk token before the read so a mutation
220+
// landing mid-read is detected by the post-read stat below.
221+
const preReadStats = await fs.stat(fullPath, { bigint: true }).catch(() => undefined)
218222
const buffer = await fs.readFile(fullPath)
219223
const fileContent = buffer.toString("utf-8")
220-
const result = this.processTextFile(fileContent, entry)
224+
// A lossy decode is not the whole file: the model never saw those bytes.
225+
const lossyDecode = !Buffer.from(fileContent).equals(buffer)
226+
// S4b follow-up (#46 / epic #1375): processTextFile reports whether the
227+
// returned content is the whole file; the observation below records that
228+
// scope so the write guard can deny full-file updates built on a partial view.
229+
const processed = this.processTextFile(fileContent, entry)
221230

222231
await task.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource)
223232

233+
// A2 (plan #33 / epic #1375): record the observed on-disk version for the future write guard.
234+
// The token is captured before AND after the read; the target is observed only
235+
// when both match — a mutation between the two stats means the content the model
236+
// received is not the on-disk state, and observing it would let a later write
237+
// match a token the model never saw. A stat failure leaves the target
238+
// unobserved and never fails the read.
239+
// A task that was cancelled or disposed while this read was awaiting I/O must
240+
// not record an observation: disposeOnce() has retired the registry, and the
241+
// content this turn produced will never be acted on. Skipping also drops the
242+
// post-read stat work for a task that no longer has a consumer.
243+
if (!task.abort) {
244+
const postReadStats = await fs.stat(fullPath, { bigint: true }).catch(() => undefined)
245+
// Re-checked after the await: the guard above cannot see a cancellation
246+
// that lands while the stat is in flight, and that read belongs to a run
247+
// that will never act on it.
248+
if (preReadStats && postReadStats && !task.abort) {
249+
const preReadToken = versionTokenOfStat(preReadStats)
250+
if (preReadToken === versionTokenOfStat(postReadStats)) {
251+
task.observationRegistry.observe(
252+
fullPath,
253+
preReadToken,
254+
processed.complete && !lossyDecode,
255+
)
256+
}
257+
}
258+
}
259+
224260
updateFileResult(relPath, {
225-
nativeContent: `File: ${relPath}\n${result}`,
261+
nativeContent: `File: ${relPath}\n${processed.content}`,
226262
})
227263
} catch (error) {
228264
const errorMsg = error instanceof Error ? error.message : String(error)
@@ -266,8 +302,14 @@ export class ReadFileTool extends BaseTool<"read_file"> {
266302

267303
/**
268304
* Process a text file according to the requested mode.
305+
*
306+
* Returns the content string plus whether that content is the complete
307+
* file (S4b follow-up #46 / epic #1375): slice mode is complete only
308+
* when it starts at line 1, returns every line, and was not truncated;
309+
* indentation mode is never complete because it returns semantic blocks
310+
* of the file, not the file itself.
269311
*/
270-
private processTextFile(content: string, entry: InternalFileEntry): string {
312+
private processTextFile(content: string, entry: InternalFileEntry): { content: string; complete: boolean } {
271313
const mode = entry.mode || "slice"
272314

273315
if (mode === "indentation") {
@@ -300,7 +342,15 @@ export class ReadFileTool extends BaseTool<"read_file"> {
300342
output += `\n\nIncluded ranges: ${rangeStr} (total: ${result.totalLines} lines)`
301343
}
302344

303-
return output
345+
if (result.hasClippedLines) {
346+
// A returned line can still be a partial view: clipping is reported
347+
// separately from the omission notice, because the block may show
348+
// every line it includes in truncated form.
349+
output += `\nNote: Some lines in this view exceed ${MAX_LINE_LENGTH} characters and were clipped in this view.`
350+
}
351+
352+
// Indentation mode returns semantic blocks: never a complete file view.
353+
return { content: output, complete: false }
304354
}
305355

306356
// Slice mode (default): simple offset/limit reading
@@ -323,11 +373,27 @@ export class ReadFileTool extends BaseTool<"read_file"> {
323373
To read more: Use the read_file tool with offset=${nextOffset} and limit=${limit}.
324374
325375
${result.content}`
376+
if (result.hasClippedLines) {
377+
// The slice cut lines off and also clipped long lines inside it, so both
378+
// notices belong to the response.
379+
output += `\nNote: Some lines in this view exceed ${MAX_LINE_LENGTH} characters and were clipped in this view.`
380+
}
381+
} else if (result.hasClippedLines) {
382+
// Every line was returned, so there is no later offset to read: report the
383+
// clipping without a next-offset hint, and keep the read incomplete so a
384+
// full-file replacement cannot be built from a clipped line.
385+
output = `IMPORTANT: Some lines exceed ${MAX_LINE_LENGTH} characters and were clipped in this view. ${offset1 === 1 ? "The file was read in full" : `The returned slice starts at line ${offset1} and reaches the end of the file`}, but the clipped lines were not shown in full.\n${result.content}`
326386
} else if (result.returnedLines === 0 && result.totalLines === 0) {
327387
output = "Note: File is empty"
328388
}
329389

330-
return output
390+
// Complete only when the slice starts at line 1, returned every line, and
391+
// showed every line in full (returnedLines === totalLines follows from the
392+
// first two conditions): a partial start, a truncated tail, or a clipped
393+
// line means the model did not see the whole file.
394+
const complete = offset0 === 0 && !result.wasTruncated && !result.hasClippedLines
395+
396+
return { content: output, complete }
331397
}
332398

333399
/**
@@ -770,9 +836,20 @@ export class ReadFileTool extends BaseTool<"read_file"> {
770836
}
771837

772838
// Read text file
773-
const rawContent = await fs.readFile(fullPath, "utf8")
839+
// A2 (epic #1375): capture the on-disk token before the read so a mutation
840+
// landing mid-read is detected by the post-read stat below.
841+
const preReadStats = await fs.stat(fullPath, { bigint: true }).catch(() => undefined)
842+
const rawBuffer = await fs.readFile(fullPath)
843+
const rawContent = rawBuffer.toString("utf-8")
844+
// Same contract: a lossy decode is a partial view.
845+
const lossyDecode = !Buffer.from(rawContent).equals(rawBuffer)
774846

775847
// Handle line ranges if specified
848+
// S4b follow-up (#46 / epic #1375): a line-range read returns only the requested
849+
// ranges, and a slice truncated to DEFAULT_LINE_LIMIT returns only the head of
850+
// the file — record such observations as partial so the write guard denies a
851+
// full-file update built on them.
852+
let readComplete = false
776853
let content: string
777854
if (entry.lineRanges && entry.lineRanges.length > 0) {
778855
const lines = rawContent.split("\n")
@@ -792,15 +869,41 @@ export class ReadFileTool extends BaseTool<"read_file"> {
792869
// Read with default limits using slice mode
793870
const result = readWithSlice(rawContent, 0, DEFAULT_LINE_LIMIT)
794871
content = result.content
872+
readComplete = !result.wasTruncated && !result.hasClippedLines
795873
if (result.wasTruncated) {
796874
content += `\n\n[File truncated: showing ${result.returnedLines} of ${result.totalLines} total lines]`
875+
if (result.hasClippedLines) {
876+
// Both notices: the slice was truncated and a line inside it was
877+
// clipped.
878+
content += `\n\n[Some lines exceed the per-line length cap and were clipped in this view]`
879+
}
880+
} else if (result.hasClippedLines) {
881+
content += `\n\n[Some lines exceed the per-line length cap and were clipped in this view]`
797882
}
798883
}
799884

800885
results.push(`File: ${relPath}\n${content}`)
801886

802887
// Track file in context
803888
await task.fileContextTracker.trackFileContext(relPath, "read_tool")
889+
890+
// A2 (plan #33 / epic #1375): mirror the native path — record the observed
891+
// on-disk version so legacy-format reads also feed the future write guard.
892+
// Observe only when the pre-read and post-read tokens match (a mutation between
893+
// them means the returned content is not the on-disk state). A stat failure
894+
// leaves the target unobserved and never fails the read.
895+
// Same contract as the native path: an aborted or disposed task records nothing.
896+
if (!task.abort) {
897+
const postReadStats = await fs.stat(fullPath, { bigint: true }).catch(() => undefined)
898+
// Re-checked after the await, as on the native path: a cancel landing
899+
// during the stat must still record nothing.
900+
if (preReadStats && postReadStats && !task.abort) {
901+
const preReadToken = versionTokenOfStat(preReadStats)
902+
if (preReadToken === versionTokenOfStat(postReadStats)) {
903+
task.observationRegistry.observe(fullPath, preReadToken, readComplete && !lossyDecode)
904+
}
905+
}
906+
}
804907
} catch (error) {
805908
const errorMsg = error instanceof Error ? error.message : String(error)
806909
results.push(`File: ${relPath}\nError: ${errorMsg}`)

0 commit comments

Comments
 (0)