Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,7 @@ export interface ApplyState {
fileContent?: string;
originalFileContent?: string;
toolCallId?: string;
accepted?: boolean;
autoFormattingDiff?: string;
}

Expand Down
1 change: 1 addition & 0 deletions extensions/vscode/src/diff/processDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function processDiff(
status: "closed",
numDiffs: 0,
toolCallId,
accepted: action === "accept",
autoFormattingDiff, // Include autoformatting diff
});
} else {
Expand Down
3 changes: 3 additions & 0 deletions extensions/vscode/src/diff/vertical/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface VerticalDiffHandlerOptions {
status?: ApplyState["status"],
numDiffs?: ApplyState["numDiffs"],
fileContent?: ApplyState["fileContent"],
accepted?: ApplyState["accepted"],
) => void;
streamId?: string;
}
Expand Down Expand Up @@ -145,6 +146,7 @@ export class VerticalDiffHandler implements vscode.Disposable {
"closed",
this.editorToVerticalDiffCodeLens.get(this.fileUri)?.length ?? 0,
this.editor.document.getText(),
accept,
);

this.cancelled = true;
Expand Down Expand Up @@ -279,6 +281,7 @@ export class VerticalDiffHandler implements vscode.Disposable {
status,
numDiffs,
this.editor.document.getText(),
status === "closed" ? accept : undefined,
);
}
}
Expand Down
11 changes: 7 additions & 4 deletions extensions/vscode/src/diff/vertical/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class VerticalDiffManager {
);

if (blocks.length === 1) {
this.clearForfileUri(fileUri, true);
this.clearForfileUri(fileUri, accept);
} else {
// Re-enable listener for user changes to file
this.enableDocumentChangeListener();
Expand Down Expand Up @@ -239,14 +239,15 @@ export class VerticalDiffManager {
endLine,
{
instant,
onStatusUpdate: (status, numDiffs, fileContent) =>
onStatusUpdate: (status, numDiffs, fileContent, accepted) =>
void this.webviewProtocol.request("updateApplyState", {
streamId,
status,
numDiffs,
fileContent,
filepath: fileUri,
toolCallId,
accepted,
}),
streamId,
},
Expand Down Expand Up @@ -318,14 +319,15 @@ export class VerticalDiffManager {
editor.document.lineCount - 1,
{
instant: true,
onStatusUpdate: (status, numDiffs, fileContent) =>
onStatusUpdate: (status, numDiffs, fileContent, accepted) =>
void this.webviewProtocol.request("updateApplyState", {
streamId,
status,
numDiffs,
fileContent,
filepath: fileUri,
toolCallId,
accepted,
}),
streamId,
},
Expand Down Expand Up @@ -446,7 +448,7 @@ export class VerticalDiffManager {
{
instant: isFastApplyModel(llm),
input,
onStatusUpdate: (status, numDiffs, fileContent) =>
onStatusUpdate: (status, numDiffs, fileContent, accepted) =>
streamId &&
void this.webviewProtocol.request("updateApplyState", {
streamId,
Expand All @@ -455,6 +457,7 @@ export class VerticalDiffManager {
fileContent,
filepath: fileUri,
toolCallId,
accepted,
}),
streamId,
},
Expand Down
3 changes: 3 additions & 0 deletions gui/src/redux/slices/sessionSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,9 @@ export const sessionSlice = createSlice({
applyState.fileContent = payload.fileContent ?? applyState.fileContent;
applyState.originalFileContent =
payload.originalFileContent ?? applyState.originalFileContent;
applyState.accepted = payload.accepted ?? applyState.accepted;
applyState.autoFormattingDiff =
payload.autoFormattingDiff ?? applyState.autoFormattingDiff;
}

if (payload.status === "done") {
Expand Down
56 changes: 56 additions & 0 deletions gui/src/redux/thunks/handleApplyStateUpdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,62 @@ describe("handleApplyStateUpdate", () => {
expect(streamResponseAfterToolCall).not.toHaveBeenCalled();
});

it("should not mark a rejected closed apply state as accepted", async () => {
const toolCallState: ToolCallState = {
toolCallId: "test-tool-call",
status: "done",
...UNUSED_TOOL_CALL_PARAMS,
};
const newApplyState = { streamId: "chat-stream" };

vi.mocked(findToolCallById).mockReturnValue(toolCallState);
mockGetState.mockReturnValue({
session: {
history: [],
codeBlockApplyStates: {
states: [newApplyState],
},
},
config: { config: {} },
});

const applyState: ApplyState = {
streamId: "chat-stream",
toolCallId: "test-tool-call",
status: "closed",
filepath: "test.txt",
numDiffs: 0,
accepted: false,
};

const thunk = handleApplyStateUpdate(applyState);
await thunk(mockDispatch, mockGetState, mockExtra);

expect(logToolUsage).toHaveBeenCalledWith(
toolCallState,
false,
true,
mockExtra.ideMessenger,
);
expect(logAgentModeEditOutcome).toHaveBeenCalledWith(
[],
{},
toolCallState,
newApplyState,
false,
mockExtra.ideMessenger,
);
expect(acceptToolCall).not.toHaveBeenCalled();
expect(updateToolCallOutput).not.toHaveBeenCalledWith(
expect.objectContaining({
contextItems: expect.arrayContaining([
expect.objectContaining({ name: "Edit Success" }),
]),
}),
);
expect(streamResponseAfterToolCall).not.toHaveBeenCalled();
});

it("should handle errored tool call closure", async () => {
const toolCallState: ToolCallState = {
toolCallId: "test-tool-call",
Expand Down
6 changes: 5 additions & 1 deletion gui/src/redux/thunks/handleApplyStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export const handleApplyStateUpdate = createAsyncThunk<

if (applyState.status === "closed") {
if (toolCallState) {
const accepted = toolCallState.status !== "canceled";
// Closing a diff does not always mean the user accepted it.
// Only report edit success when the close event confirms acceptance.
const accepted =
toolCallState.status !== "canceled" &&
applyState.accepted !== false;

logToolUsage(toolCallState, accepted, true, extra.ideMessenger);

Expand Down
Loading