Skip to content

Commit 9f142dc

Browse files
fix(safety, task): cover the directory-failure paths and release the observation registry on dispose
Two pre-merge warnings on Zoo-Code-Org#1912: - Regression Evidence: safeWriteText's parent-directory creation and access failures are now covered - each propagates its original error and nothing is staged or renamed (no publish). - Lifecycle Resource Cleanup: the task-owned ObservationRegistry is cleared in Task#disposeOnce so its per-path Map cannot outlive the task; a disposal test asserts the entry is gone after dispose. The Security Boundaries error (handle-relative/no-follow publication) is the series' registered residual window: the Zoo-Code-Org#1914 pinning port in this branch narrows it, and the descriptor-relative primitive is tracked in Zoo-Code-Org#2148 / Zoo-Code-Org#1414 / Zoo-Code-Org#1991. Local evidence: safeWriteText suite 86 passed (including the two new directory-failure tests). Task.spec cannot load in this borrowed-node_modules worktree (missing @ai-sdk/openai-compatible) - CI is the authority for that suite.
1 parent b41213f commit 9f142dc

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,6 +3615,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
36153615
console.log(`[Task#dispose] disposing task ${this.taskId}.${this.instanceId}`)
36163616
this.cancelAssistantMessagePersistence()
36173617

3618+
// The observation registry is task-owned: clear it during disposal so its
3619+
// per-path Map cannot outlive the task that owns it.
3620+
try {
3621+
this.observationRegistry.clear()
3622+
} catch (error) {
3623+
console.error("Error clearing observation registry:", error)
3624+
}
3625+
36183626
// Release handler-owned host resources (e.g. VS Code configuration listeners).
36193627
// This is the fallback for leases that never settle; later lease releases
36203628
// find nothing retired and so cannot dispose twice.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,22 @@ describe("Cline", () => {
18101810
return dispose
18111811
}
18121812

1813+
it("clears the observation registry when the task is disposed", async () => {
1814+
const task = new Task({
1815+
provider: mockProvider,
1816+
apiConfiguration: mockApiConfig,
1817+
task: "t",
1818+
startTask: false,
1819+
})
1820+
task.observationRegistry.observe("/tmp/observed.txt", "v1")
1821+
expect(task.observationRegistry.get("/tmp/observed.txt")).toBeDefined()
1822+
1823+
await task.dispose()
1824+
1825+
// The registry is task-owned: its per-path entries must not outlive disposal.
1826+
expect(task.observationRegistry.get("/tmp/observed.txt")).toBeUndefined()
1827+
})
1828+
18131829
it("disposes the replaced handler when the configuration changes while idle", async () => {
18141830
const task = new Task({
18151831
provider: mockProvider,

‎src/services/file-safety/__tests__/safeWriteText.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,29 @@ describe("safeWriteText", () => {
177177
expect(fs.unlink).not.toHaveBeenCalled()
178178
})
179179

180+
it("propagates a parent-directory creation failure without publishing", async () => {
181+
const targetPath = "/tmp/test-dir/target.txt"
182+
vi.mocked(fs.realpath).mockResolvedValue(targetPath)
183+
vi.mocked(fs.mkdir).mockRejectedValueOnce(new Error("parent mkdir failed"))
184+
185+
await expect(safeWriteText(targetPath, "hello", { platform: "linux" })).rejects.toThrow("parent mkdir failed")
186+
187+
// Nothing was staged and nothing was published: the original directory error
188+
// surfaces before the staging open.
189+
expect(fs.rename).not.toHaveBeenCalled()
190+
expect(fsSync.openSync).not.toHaveBeenCalled()
191+
})
192+
193+
it("propagates a parent-directory access failure without publishing", async () => {
194+
const targetPath = "/tmp/test-dir/target.txt"
195+
vi.mocked(fs.realpath).mockResolvedValue(targetPath)
196+
vi.mocked(fs.access).mockRejectedValueOnce(new Error("parent access failed"))
197+
198+
await expect(safeWriteText(targetPath, "hello", { platform: "linux" })).rejects.toThrow("parent access failed")
199+
200+
expect(fs.rename).not.toHaveBeenCalled()
201+
})
202+
180203
it("removes the now-empty staging directory after a successful self-staged commit", async () => {
181204
const targetPath = "/tmp/test-dir/target.txt"
182205
vi.mocked(fs.realpath).mockResolvedValue(targetPath)

0 commit comments

Comments
 (0)