Repository navigation
feat(tools): route the remaining write tools through the guard (U7, #1375) #1918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8ceee66
13dfd2d
dbc719e
1f2d808
555aca8
d6c687d
22656b2
07daaa2
7363088
b48968d
2961d02
4deef9c
06f342c
c607a86
12721cf
a4959f5
01d6a3b
c130d69
68578de
5b0f727
6bc58c2
fea19cd
248c7bf
4a61c31
9da2825
2e84807
e514949
9fdc39f
c566827
df2e644
97765e9
1709f5e
253f66b
f8fe189
5f0b8ec
ccbf424
b999203
21588dc
eb1b1cf
7e22b03
b303f16
7bd4687
1af2405
72c066a
5787d53
f38055a
63319a7
173938b
3be3d8f
843159b
d580c73
e3c7dc0
638f6ae
ebf8386
e70e5de
087b95c
514beb5
0dd330a
35120d7
889a6f3
93a0e9d
86d7ccd
29afbce
b1aaa8b
4bb8be7
1c2069c
b184a25
d5bab08
b97b458
38341c8
8b9e136
710ebe3
b54b3ba
e3cabb5
41a0509
fa8621f
6d18a9f
79f3b57
d4896e2
8f30fc1
084b0ff
d6a995f
da1b0d5
da08f7c
4880ea9
63cb834
e134645
0149602
f6c86e0
2204e17
9c43549
180ff82
27e2829
bbd6751
328f2f9
8a8bcde
0594845
8f696c4
60df49c
dded9b2
7678ce6
3d9fd4d
9308576
ea38831
e869a4f
57cf926
ddf52ed
02fd22b
112a8a8
4bb5ec4
7a96cdc
fbe40ba
78a5e90
28f7dd9
addfa35
31f0d9c
4c2bff6
c6a9942
eaedbff
e5fd8df
ceae294
87eb10f
0adabf3
cf0cf01
23745fd
5c802f6
09a37ae
d6eae83
2749739
e4a3069
aa9f801
5db9266
d67491c
2133ce3
79d320e
0fde677
db352c2
30c9ee6
a99b9d9
f13d36e
44f1531
235d4c4
a051608
aeb741c
6c0dfff
b4e149f
7b3496b
b8d1c72
098b820
3b9a505
0698e12
c5808de
65c1c7d
6f1ac0e
67cc641
6e0c6db
7a7fe1d
4e4ffbd
c2b7f9b
fc5d6cf
4f00bb4
b535500
855c98b
931cc05
29e7a62
5feac54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { TelemetryService } from "@roo-code/telemetry" | |
|
|
||
| import { getReadablePath } from "../../utils/path" | ||
| import { isPathOutsideWorkspace } from "../../utils/pathUtils" | ||
| import { versionTokenOfStat } from "../../utils/versionToken" | ||
| import { Task } from "../task/Task" | ||
| import { formatResponse } from "../prompts/responses" | ||
| import { fileExistsAtPath } from "../../utils/fs" | ||
|
|
@@ -38,6 +39,24 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> { | |
| diffContent = unescapeHtmlEntities(diffContent) | ||
| } | ||
|
|
||
| // Identity of a partial observation this call created (undefined when the model had | ||
| // already observed the file): only this exact observation may be removed on a failure | ||
| // exit, so a replaced or published observation is never discarded here. Declared outside | ||
| // the try so the catch exit can use it too. | ||
| let partialObservation: { path: string; version: string; complete: boolean } | undefined | ||
| const forgetPartialObservationIfCurrent = () => { | ||
| if (!partialObservation) { | ||
| return | ||
| } | ||
| const current = task.observationRegistry.get(partialObservation.path) | ||
| // A matching version token is not the same observation: a concurrent complete read | ||
| // can record this very version with `complete` true, and forgetting that entry would | ||
| // discard authority this call never held. Compare both fields the entry carries. | ||
| if (current?.version === partialObservation.version && current?.complete === partialObservation.complete) { | ||
| task.observationRegistry.forget(partialObservation.path) | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| if (!relPath) { | ||
| task.consecutiveMistakeCount++ | ||
|
|
@@ -74,7 +93,32 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> { | |
| return | ||
| } | ||
|
|
||
| // The diff below is built from this exact read, so the save that follows must be | ||
| // authorized against the version captured here - not against whatever version the | ||
| // preview happens to stat afterwards. Same contract as ApplyPatchTool's hunk read: | ||
| // stat around the read and observe only when the file did not change underneath it. | ||
| const preReadStats = await fs.stat(absolutePath, { bigint: true }).catch(() => undefined) | ||
| const originalContent: string = await fs.readFile(absolutePath, "utf-8") | ||
| const postReadStats = await fs.stat(absolutePath, { bigint: true }).catch(() => undefined) | ||
| if (preReadStats && postReadStats) { | ||
| const preReadToken = versionTokenOfStat(preReadStats) | ||
| if (preReadToken === versionTokenOfStat(postReadStats)) { | ||
| // A tool read is not a model read. With no prior observation this stays a | ||
| // partial observation of the version the diff was computed against - the only | ||
| // authorization the save can have, since apply_diff computes its hunks from | ||
| // this read. When the model already observed the file, keep the completeness it | ||
| // observation to the current version would let content the model built from a | ||
| // stale read pass the compare-and-swap, so an out-of-date observation is left | ||
| // alone and the save fails with the re-read remediation. | ||
| const prior = task.observationRegistry.get(absolutePath) | ||
| if (prior === undefined) { | ||
| task.observationRegistry.observe(absolutePath, preReadToken, false) | ||
| partialObservation = { path: absolutePath, version: preReadToken, complete: false } | ||
| } else if (prior.version === preReadToken) { | ||
| task.observationRegistry.observe(absolutePath, preReadToken, prior.complete === true) | ||
| } | ||
|
Comment on lines
+113
to
+119
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refuse undecodable files before recording the observation, as Line 97 reads with This PR gives
|
||
| } | ||
| } | ||
|
|
||
| // Apply the diff to the original content | ||
| const diffResult = (await task.diffStrategy?.applyDiff( | ||
|
|
@@ -116,6 +160,7 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> { | |
| task.recordToolError("apply_diff", formattedError) | ||
|
|
||
| pushToolResult(formattedError) | ||
| forgetPartialObservationIfCurrent() | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -174,10 +219,12 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> { | |
| const didApprove = await askApproval("tool", completeMessage, toolProgressStatus, isWriteProtected) | ||
|
|
||
| if (!didApprove) { | ||
| forgetPartialObservationIfCurrent() | ||
| return | ||
| } | ||
|
|
||
| // Save directly without showing diff view or opening the file | ||
| // Save directly without showing diff view or opening the file. The diff is | ||
| // applied to an existing file, so edit-guard semantics require a prior read. | ||
| task.diffViewProvider.editType = "modify" | ||
| task.diffViewProvider.originalContent = originalContent | ||
| await task.diffViewProvider.saveDirectly( | ||
|
|
@@ -186,6 +233,7 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> { | |
| false, | ||
| diagnosticsEnabled, | ||
| writeDelayMs, | ||
| "edit", | ||
| ) | ||
| } else { | ||
| // Original behavior with diff view | ||
|
|
@@ -220,12 +268,13 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> { | |
|
|
||
| if (!didApprove) { | ||
| await task.diffViewProvider.revertChanges() | ||
| forgetPartialObservationIfCurrent() | ||
| task.processQueuedMessages() | ||
| return | ||
| } | ||
|
|
||
| // Call saveChanges to update the DiffViewProvider properties | ||
| await task.diffViewProvider.saveChanges(diagnosticsEnabled, writeDelayMs) | ||
| await task.diffViewProvider.saveChanges(diagnosticsEnabled, writeDelayMs, "edit") | ||
| } | ||
|
|
||
| // Track file edit operation | ||
|
|
@@ -276,6 +325,7 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> { | |
| return | ||
| } catch (error) { | ||
| await handleError("applying diff", error as Error) | ||
| forgetPartialObservationIfCurrent() | ||
| await task.diffViewProvider.reset() | ||
| this.resetPartialState() | ||
| task.processQueuedMessages() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep independent partial observations.
A partial read can replace this call's entry with a new entry for the same version during approval. If the user rejects the diff, this comparison deletes the new entry. A later targeted edit then requires a read the model already performed.
Capture the stored entry and remove it only if the registry still holds that object. Preserve that identity through
saveChangeswhen it restores the baseline. The provider currently recreates the entry. (raw.githubusercontent.com)