fix: handle apply errors

This commit is contained in:
Dallin Romney 2025-08-20 23:14:20 -07:00
parent dfd94bac33
commit 28565e6044
4 changed files with 78 additions and 62 deletions

View file

@ -32,6 +32,7 @@ export class ApplyManager {
if (filepath) {
await this.ensureFileOpen(filepath);
}
throw new Error("expected");
const { activeTextEditor } = vscode.window;
if (!activeTextEditor) {
@ -51,7 +52,6 @@ export class ApplyManager {
});
const hasExistingDocument = !!activeTextEditor.document.getText().trim();
if (hasExistingDocument) {
// Currently `isSearchAndReplace` will always provide a full file rewrite
// as the contents of `text`, so we can just instantly apply

View file

@ -1,7 +1,6 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { ApplyState } from "core";
import { EDIT_MODE_STREAM_ID } from "core/edit/constants";
import { WebviewSingleProtocolMessage } from "core/protocol/util";
import { logAgentModeEditOutcome } from "../../util/editOutcomeLogger";
import {
selectApplyStateByToolCallId,
@ -84,11 +83,14 @@ export const handleApplyStateUpdate = createAsyncThunk<
}
if (accepted) {
dispatch(
acceptToolCall({
toolCallId: applyState.toolCallId,
}),
);
if (toolCallState.status !== "errored") {
dispatch(
acceptToolCall({
toolCallId: applyState.toolCallId,
}),
);
}
void dispatch(
streamResponseAfterToolCall({
toolCallId: applyState.toolCallId,
@ -108,50 +110,49 @@ export const handleApplyStateUpdate = createAsyncThunk<
},
);
export const handleEditToolApplyResponse = createAsyncThunk<
export const handleEditToolApplyError = createAsyncThunk<
void,
{ response: WebviewSingleProtocolMessage<"applyToFile">; toolCallId: string },
{ toolCallId: string },
ThunkApiType
>(
"apply/handleEditResponse",
async ({ response, toolCallId }, { dispatch, getState, extra }) => {
if (response.status === "error") {
const state = getState();
const toolCallState = selectToolCallById(state, toolCallId);
const applyState = selectApplyStateByToolCallId(state, toolCallId);
if (
toolCallState &&
applyState &&
applyState.status !== "closed" &&
toolCallState.status === "generated"
) {
dispatch(
errorToolCall({
toolCallId,
}),
);
dispatch(
updateToolCallOutput({
toolCallId,
contextItems: [
{
icon: "problems",
name: "Apply Error",
description: "Failed to apply changes",
content: `Error editing file: failed to apply changes to file."\n\nPlease try again/something else or request further instructions.`,
hidden: false,
},
],
}),
);
void dispatch(
handleApplyStateUpdate({
status: "closed",
streamId: applyState.streamId,
toolCallId,
}),
);
}
"apply/handleEditError",
async ({ toolCallId }, { dispatch, getState, extra }) => {
const state = getState();
const toolCallState = selectToolCallById(state, toolCallId);
const applyState = selectApplyStateByToolCallId(state, toolCallId);
if (
toolCallState &&
applyState &&
applyState.status !== "closed" &&
toolCallState.status === "calling"
) {
dispatch(
errorToolCall({
toolCallId,
}),
);
dispatch(
updateToolCallOutput({
toolCallId,
contextItems: [
{
icon: "problems",
name: "Apply Error",
description: "Failed to apply changes",
content: `Error editing file: failed to apply changes to file.\n\nPlease try again with correct args or notify the user and request guidance.`,
hidden: false,
},
],
}),
);
void dispatch(
handleApplyStateUpdate({
status: "closed",
streamId: applyState.streamId,
toolCallId,
}),
);
}
},
);

View file

@ -1,6 +1,7 @@
import { resolveRelativePathInDir } from "core/util/ideUtils";
import { v4 as uuid } from "uuid";
import { handleEditToolApplyResponse } from "../../redux/thunks/handleApplyStateUpdate";
import { updateApplyState } from "../../redux/slices/sessionSlice";
import { handleEditToolApplyError } from "../../redux/thunks/handleApplyStateUpdate";
import { ClientToolImpl } from "./callClientTool";
export const editToolImpl: ClientToolImpl = async (
args,
@ -20,6 +21,13 @@ export const editToolImpl: ClientToolImpl = async (
throw new Error(`${args.filepath} does not exist`);
}
const streamId = uuid();
extras.dispatch(
updateApplyState({
streamId,
toolCallId,
status: "not-started",
}),
);
void extras.ideMessenger
.request("applyToFile", {
streamId,
@ -27,15 +35,13 @@ export const editToolImpl: ClientToolImpl = async (
toolCallId,
filepath: firstUriMatch,
})
.then((response) => {
.catch(() => {
void extras.dispatch(
handleEditToolApplyResponse({
response,
handleEditToolApplyError({
toolCallId,
}),
);
});
return {
respondImmediately: false,
output: undefined, // No immediate output.

View file

@ -3,7 +3,8 @@ import { parseAllSearchReplaceBlocks } from "core/edit/searchAndReplace/parseSea
import { resolveRelativePathInDir } from "core/util/ideUtils";
import posthog from "posthog-js";
import { v4 as uuid } from "uuid";
import { handleEditToolApplyResponse } from "../../redux/thunks/handleApplyStateUpdate";
import { updateApplyState } from "../../redux/slices/sessionSlice";
import { handleEditToolApplyError } from "../../redux/thunks/handleApplyStateUpdate";
import { ClientToolImpl } from "./callClientTool";
export const searchReplaceToolImpl: ClientToolImpl = async (
@ -82,7 +83,14 @@ export const searchReplaceToolImpl: ClientToolImpl = async (
// Single applyToFile call with all accumulated changes
// This works becaues of our logic in `applyCodeBlock` that determines
// that the full file rewrite here can be applied instantly, so the diff
// lines are just st
// lines are just
extras.dispatch(
updateApplyState({
streamId,
toolCallId,
status: "not-started",
}),
);
void extras.ideMessenger
.request("applyToFile", {
streamId,
@ -91,13 +99,14 @@ export const searchReplaceToolImpl: ClientToolImpl = async (
filepath: resolvedFilepath,
isSearchAndReplace: true,
})
.then((response) => {
void extras.dispatch(
handleEditToolApplyResponse({
response,
toolCallId,
}),
);
.then((res) => {
if (res.status === "error") {
void extras.dispatch(
handleEditToolApplyError({
toolCallId,
}),
);
}
});
// Return success - applyToFile will handle the completion state