Skip to content

Commit 8d382c3

Browse files
easonliang28easonLiangWorldedtech
authored andcommitted
feat(task): per-task file observation registry (A2, Zoo-Code-Org#1375)
1 parent f015dae commit 8d382c3

5 files changed

Lines changed: 488 additions & 18 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import { restoreTodoListForTask } from "../tools/UpdateTodoListTool"
108108
import { FileContextTracker } from "../context-tracking/FileContextTracker"
109109
import { ObservationRegistry } from "./observationRegistry"
110110
import { RooIgnoreController } from "../ignore/RooIgnoreController"
111+
import { ObservationRegistry } from "./observationRegistry"
111112
import { RooProtectedController } from "../protect/RooProtectedController"
112113
import { type AssistantMessageContent, presentAssistantMessage } from "../assistant-message"
113114
import { DispatchState, type PendingWork } from "./dispatchState"
@@ -307,6 +308,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
307308
readonly parentTask: Task | undefined = undefined
308309
readonly taskNumber: number
309310
readonly workspacePath: string
311+
readonly observationRegistry = new ObservationRegistry()
310312

311313
/**
312314
* The mode associated with this task. Persisted across sessions

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

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,40 @@ describe("ObservationRegistry", () => {
88
})
99

1010
it("observe → get returns the recorded version and observedAt", () => {
11-
const reg = new ObservationRegistry()
12-
reg.observe("/a/b/c.ts", "1:2:300:4000000000:5000000000")
13-
14-
const obs = reg.get("/a/b/c.ts")
15-
expect(obs).toBeDefined()
16-
expect(obs!.version).toBe("1:2:300:4000000000:5000000000")
17-
expect(typeof obs!.observedAt).toBe("number")
11+
// typeof number accepts 0 and NaN, so pin the exact timestamp instead: a
12+
// broken clock that returns some other number must not pass.
13+
vi.useFakeTimers()
14+
vi.setSystemTime(new Date(Date.UTC(2025, 0, 1, 0, 0, 0, 0)))
15+
try {
16+
const reg = new ObservationRegistry()
17+
reg.observe("/a/b/c.ts", "1:2:300:4000000000:5000000000")
18+
const obs = reg.get("/a/b/c.ts")
19+
expect(obs).toBeDefined()
20+
expect(obs!.version).toBe("1:2:300:4000000000:5000000000")
21+
expect(obs!.observedAt).toBe(Date.UTC(2025, 0, 1, 0, 0, 0, 0))
22+
} finally {
23+
vi.useRealTimers()
24+
}
1825
})
1926

2027
it("re-observe replaces the entry with a fresh observedAt", () => {
2128
vi.useFakeTimers()
22-
const reg = new ObservationRegistry()
23-
reg.observe("/a/b/c.ts", "v1")
24-
const first = reg.get("/a/b/c.ts")!
25-
expect(first.version).toBe("v1")
26-
27-
vi.advanceTimersByTime(50)
28-
reg.observe("/a/b/c.ts", "v2")
29-
const second = reg.get("/a/b/c.ts")!
30-
expect(second.version).toBe("v2")
31-
expect(second.observedAt).toBeGreaterThan(first.observedAt)
29+
try {
30+
const reg = new ObservationRegistry()
31+
reg.observe("/a/b/c.ts", "v1")
32+
const first = reg.get("/a/b/c.ts")!
33+
expect(first.version).toBe("v1")
34+
35+
vi.advanceTimersByTime(50)
36+
reg.observe("/a/b/c.ts", "v2")
37+
const second = reg.get("/a/b/c.ts")!
38+
expect(second.version).toBe("v2")
39+
expect(second.observedAt).toBeGreaterThan(first.observedAt)
40+
} finally {
41+
// Teardown in finally so a failing assertion cannot leak fake
42+
// timers or the mocked Date into subsequent tests.
43+
vi.useRealTimers()
44+
}
3245
})
3346

3447
it("has returns true for observed paths, false otherwise", () => {
@@ -131,4 +144,18 @@ describe("ObservationRegistry", () => {
131144
expect(reg.has("/late.ts")).toBe(false)
132145
})
133146
})
147+
148+
it("forget drops a warm entry so a later read cannot be guarded by a stale token", () => {
149+
const reg = new ObservationRegistry()
150+
reg.observe("/x.ts", "t1")
151+
152+
// The warm case: an entry from an earlier read must not survive a read that could not
153+
// compute a token for the current on-disk version.
154+
expect(reg.forget("/x.ts")).toBe(true)
155+
expect(reg.get("/x.ts")).toBeUndefined()
156+
157+
// The cold case: forgetting an unobserved path is a no-op, not an error.
158+
expect(reg.forget("/y.ts")).toBe(false)
159+
expect(reg.get("/y.ts")).toBeUndefined()
160+
})
134161
})

‎src/core/task/observationRegistry.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ export class ObservationRegistry {
6060
this.entries.clear()
6161
}
6262

63+
/**
64+
* Drop the observation for a file. A read that cannot compute a version token must not
65+
* leave an earlier entry behind, because the write guard would then compare against a
66+
* token that no longer describes the last read.
67+
*/
68+
forget(absolutePath: string): boolean {
69+
return this.entries.delete(absolutePath)
70+
}
71+
6372
/**
6473
* Retire the registry: drop every entry and refuse later observations.
6574
*

‎src/core/tools/ReadFileTool.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ 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 { computeVersionToken } from "../../utils/versionToken"
2021
import { formatResponse } from "../prompts/responses"
2122
import { RecordSource } from "../context-tracking/FileContextTrackerTypes"
2223
import { isPathOutsideWorkspace } from "../../utils/pathUtils"
@@ -221,6 +222,11 @@ export class ReadFileTool extends BaseTool<"read_file"> {
221222

222223
await task.fileContextTracker.trackFileContext(relPath, "read_tool" as RecordSource)
223224

225+
// A2 (plan #33 / epic #1375): record the observed on-disk version for the future write guard.
226+
// A stat failure leaves the target unobserved and never fails the read.
227+
const version = await computeVersionToken(fullPath).catch(() => undefined)
228+
if (version) task.observationRegistry.observe(fullPath, version)
229+
224230
updateFileResult(relPath, {
225231
nativeContent: `File: ${relPath}\n${result}`,
226232
})
@@ -801,6 +807,12 @@ export class ReadFileTool extends BaseTool<"read_file"> {
801807

802808
// Track file in context
803809
await task.fileContextTracker.trackFileContext(relPath, "read_tool")
810+
811+
// A2 (plan #33 / epic #1375): mirror the native path — record the observed
812+
// on-disk version so legacy-format reads also feed the future write guard.
813+
// A stat failure leaves the target unobserved and never fails the read.
814+
const version = await computeVersionToken(fullPath).catch(() => undefined)
815+
if (version) task.observationRegistry.observe(fullPath, version)
804816
} catch (error) {
805817
const errorMsg = error instanceof Error ? error.message : String(error)
806818
results.push(`File: ${relPath}\nError: ${errorMsg}`)

0 commit comments

Comments
 (0)