Skip to content

Commit 3ca8992

Browse files
u917 recut rung 2: guarded write core with the unwired knip entry and the three small files it travels with
1 parent d616ec5 commit 3ca8992

10 files changed

Lines changed: 2350 additions & 9 deletions

File tree

‎knip.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"!utils/versionToken.ts!",
2929
"!services/tools/Bm25Ranker.ts!",
3030
"!services/tools/types.ts!",
31-
"!core/tools/helpers/toolResultFormatting.ts!"
31+
"!core/tools/helpers/toolResultFormatting.ts!",
32+
"!core/tools/guardedWrite.ts!"
3233
],
3334
"ignoreDependencies": [
3435
"@roo-code/config-typescript",

‎src/core/task/__tests__/Task.dispose.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,33 @@ describe("Task.run() idempotency", () => {
460460
mockApiConfiguration = { apiProvider: providerIdentifiers.anthropic, apiKey: "test-key" } as ProviderSettings
461461
})
462462

463+
test("each Task constructs its own observation registry", async () => {
464+
// The guard authorizes a write from the observations of the task that made it. One
465+
// shared registry would let a parent read authorize a subtask write, and one task
466+
// clearing its registry would revoke the other task authorization. The field is an
467+
// instance initializer, so this asserts the ownership rather than the type.
468+
const first = new Task({
469+
provider: mockProvider as unknown as ClineProvider,
470+
apiConfiguration: mockApiConfiguration,
471+
task: "first",
472+
startTask: false,
473+
})
474+
const second = new Task({
475+
provider: mockProvider as unknown as ClineProvider,
476+
apiConfiguration: mockApiConfiguration,
477+
task: "second",
478+
startTask: false,
479+
})
480+
481+
expect(first.observationRegistry).not.toBe(second.observationRegistry)
482+
first.observationRegistry.observe("/same/path.ts", "1:2:22:100:100", true)
483+
expect(second.observationRegistry.get("/same/path.ts")).toBeUndefined()
484+
expect(second.observationRegistry.size).toBe(0)
485+
486+
await first.dispose()
487+
await second.dispose()
488+
})
489+
463490
test("run() does not invoke startTask when task was already started by constructor", async () => {
464491
// Spy on the prototype before construction so we capture the constructor's call too.
465492
const startTaskSpy = vi.spyOn(Task.prototype as any, "startTask").mockResolvedValue(undefined)

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,43 @@ describe("ObservationRegistry", () => {
7070
expect(regB.get("/shared.ts")!.version).toBe("v2")
7171
})
7272

73+
describe("forget", () => {
74+
it("removes the entry and reports that it removed one", () => {
75+
const registry = new ObservationRegistry()
76+
registry.observe("/a.ts", "1:1:1:1:1", true)
77+
78+
expect(registry.forget("/a.ts")).toBe(true)
79+
expect(registry.has("/a.ts")).toBe(false)
80+
expect(registry.get("/a.ts")).toBeUndefined()
81+
expect(registry.size).toBe(0)
82+
})
83+
84+
it("reports false for a path it never observed, and changes nothing", () => {
85+
const registry = new ObservationRegistry()
86+
registry.observe("/a.ts", "1:1:1:1:1", true)
87+
88+
expect(registry.forget("/missing.ts")).toBe(false)
89+
expect(registry.size).toBe(1)
90+
expect(registry.get("/a.ts")?.version).toBe("1:1:1:1:1")
91+
})
92+
93+
it("leaves the other entries alone - that is why a caller uses forget and not clear", () => {
94+
// A caller revoking an authorization it did not earn must not discard the
95+
// observations other reads of the same task still rely on.
96+
const registry = new ObservationRegistry()
97+
registry.observe("/a.ts", "1:1:1:1:1", true)
98+
registry.observe("/b.ts", "2:2:2:2:2", false)
99+
100+
expect(registry.forget("/a.ts")).toBe(true)
101+
expect(registry.get("/b.ts")).toEqual({
102+
version: "2:2:2:2:2",
103+
observedAt: expect.any(Number),
104+
complete: false,
105+
})
106+
expect(registry.size).toBe(1)
107+
})
108+
})
109+
73110
describe("completeness scope (S4b follow-up #46)", () => {
74111
it("defaults to a complete observation when the read scope is not given", () => {
75112
const reg = new ObservationRegistry()

‎src/core/task/observationRegistry.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ export class ObservationRegistry {
5656
return this.entries.has(absolutePath)
5757
}
5858

59+
/**
60+
* Drop the observation for one path. A caller that must revoke an
61+
* authorization it did not earn - a preview that observed a version the model
62+
* never read - needs this instead of clear(), which would also discard the
63+
* observations other reads of the same task still rely on.
64+
*/
65+
forget(absolutePath: string): boolean {
66+
return this.entries.delete(absolutePath)
67+
}
68+
5969
clear(): void {
6070
this.entries.clear()
6171
}

0 commit comments

Comments
 (0)