From 6504bd55659823c8117d04ddb447f91d9932d086 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Sat, 12 Apr 2025 22:34:15 -0700 Subject: [PATCH 1/6] feat: reintroduce lazy apply for full files --- core/edit/lazy/applyCodeBlock.ts | 44 +++++++++++-------- core/edit/lazy/deterministic.ts | 29 +++++++++--- extensions/vscode/src/apply/ApplyManager.ts | 8 ++-- .../Lump/BlockSettingsTopToolbar.tsx | 4 +- 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/core/edit/lazy/applyCodeBlock.ts b/core/edit/lazy/applyCodeBlock.ts index d2323aca3..b117c7dea 100644 --- a/core/edit/lazy/applyCodeBlock.ts +++ b/core/edit/lazy/applyCodeBlock.ts @@ -2,7 +2,7 @@ import { DiffLine, ILLM } from "../.."; import { generateLines } from "../../diff/util"; import { supportedLanguages } from "../../util/treeSitter"; import { getUriFileExtension } from "../../util/uri"; - +import { deterministicApplyLazyEdit } from "./deterministic"; import { streamLazyApply } from "./streamLazyApply"; import { applyUnifiedDiff, isUnifiedDiffFormat } from "./unifiedDiffApply"; @@ -16,32 +16,40 @@ export async function applyCodeBlock( newFile: string, filename: string, llm: ILLM, -): Promise<[boolean, AsyncGenerator]> { - // This was buggy, removed for now, maybe forever - // if (canUseInstantApply(filename)) { - // const diffLines = await deterministicApplyLazyEdit( - // oldFile, - // newFile, - // filename, - // ); +): Promise<{ + isInstantApply: boolean; + diffLinesGenerator: AsyncGenerator; +}> { + if (canUseInstantApply(filename)) { + const diffLines = await deterministicApplyLazyEdit( + oldFile, + newFile, + filename, + ); - // // Fall back to LLM method if we couldn't apply deterministically - // if (diffLines !== undefined) { - // const diffGenerator = generateLines(diffLines!); - // return [true, diffGenerator]; - // } - // } + if (diffLines !== undefined) { + return { + isInstantApply: true, + diffLinesGenerator: generateLines(diffLines!), + }; + } + } // If the code block is a diff if (isUnifiedDiffFormat(newFile)) { try { const diffLines = applyUnifiedDiff(oldFile, newFile); - const diffGenerator = generateLines(diffLines!); - return [true, diffGenerator]; + return { + isInstantApply: true, + diffLinesGenerator: generateLines(diffLines!), + }; } catch (e) { console.error("Failed to apply unified diff", e); } } - return [false, streamLazyApply(oldFile, filename, newFile, llm)]; + return { + isInstantApply: false, + diffLinesGenerator: streamLazyApply(oldFile, filename, newFile, llm), + }; } diff --git a/core/edit/lazy/deterministic.ts b/core/edit/lazy/deterministic.ts index 1e3a0ec4e..b4c6611c9 100644 --- a/core/edit/lazy/deterministic.ts +++ b/core/edit/lazy/deterministic.ts @@ -15,6 +15,19 @@ type AstReplacements = Array<{ replacementNodes: Parser.SyntaxNode[]; }>; +/** + * Using this as a flag to slowly reintroduce lazy applies. + * With this set, we will only attempt to deterministically apply + * when there are no lazy blocks and then just replace the whole file, + * and otherwise never use instant apply + */ +const ONLY_FULL_FILE_REWRITE = true; + +const LAZY_COMMENT_REGEX = /\.{3}\s*(.+?)\s*\.{3}/; +export function isLazyText(text: string): boolean { + return LAZY_COMMENT_REGEX.test(text); +} + function reconstructNewFile( oldFile: string, newFile: string, @@ -135,6 +148,15 @@ export async function deterministicApplyLazyEdit( let newTree = parser.parse(newLazyFile); let reconstructedNewFile: string | undefined = undefined; + // See comments on `ONLY_FULL_FILE_REWRITE` for context + if (ONLY_FULL_FILE_REWRITE) { + if (!isLazyText(newTree.rootNode.text)) { + return myersDiff(oldFile, newLazyFile); + } else { + return undefined; + } + } + // If there is no lazy block anywhere, we add our own to the outsides // so that large chunks of the file don't get removed if (!findInAst(newTree.rootNode, isLazyBlock)) { @@ -198,11 +220,6 @@ export async function deterministicApplyLazyEdit( return diff; } -const LAZY_COMMENT_REGEX = /\.{3}\s*(.+?)\s*\.{3}/; -export function isLazyLine(text: string): boolean { - return LAZY_COMMENT_REGEX.test(text); -} - function isLazyBlock(node: Parser.SyntaxNode): boolean { // Special case for "{/* ... existing code ... */}" if ( @@ -213,7 +230,7 @@ function isLazyBlock(node: Parser.SyntaxNode): boolean { return true; } - return node.type.includes("comment") && isLazyLine(node.text); + return node.type.includes("comment") && isLazyText(node.text); } function stringsWithinLevDistThreshold( diff --git a/extensions/vscode/src/apply/ApplyManager.ts b/extensions/vscode/src/apply/ApplyManager.ts index 4942cb0c1..2741fea0b 100644 --- a/extensions/vscode/src/apply/ApplyManager.ts +++ b/extensions/vscode/src/apply/ApplyManager.ts @@ -116,17 +116,17 @@ export class ApplyManager { return; } - const [instant, diffLines] = await applyCodeBlock( + const { isInstantApply, diffLinesGenerator } = await applyCodeBlock( editor.document.getText(), text, getUriPathBasename(editor.document.uri.toString()), llm, ); - if (instant) { + if (isInstantApply) { await this.verticalDiffManager.streamDiffLines( - diffLines, - instant, + diffLinesGenerator, + isInstantApply, streamId, toolCallId, ); diff --git a/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx b/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx index 4ecf299b7..f7464e793 100644 --- a/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx +++ b/gui/src/components/mainInput/Lump/BlockSettingsTopToolbar.tsx @@ -116,7 +116,7 @@ export function BlockSettingsTopToolbar() { ); return ( -
+
-
+
From 6852ed148631aadd8f1813e5c507a4a7e2b6d297 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 14 Apr 2025 10:07:29 -0700 Subject: [PATCH 2/6] Update deterministic.test.ts --- core/edit/lazy/deterministic.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/edit/lazy/deterministic.test.ts b/core/edit/lazy/deterministic.test.ts index 7590e5025..33140a0a5 100644 --- a/core/edit/lazy/deterministic.test.ts +++ b/core/edit/lazy/deterministic.test.ts @@ -83,7 +83,8 @@ async function expectDiff(file: string) { ); } -describe("deterministicApplyLazyEdit(", () => { +// Skipped while we have `ONLY_FULL_FILE_REWRITE` enabled +describe.skip("deterministicApplyLazyEdit(", () => { test("no changes", async () => { const file = dedent` function test() { From a5619e29c8a0230f578c092c028c7cc6f3066afb Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 14 Apr 2025 10:54:27 -0700 Subject: [PATCH 3/6] feat: unskip tests --- .continue/rules/rules.yaml | 6 +++++ core/edit/lazy/applyCodeBlock.ts | 9 ++++---- core/edit/lazy/deterministic.test.ts | 8 +++---- core/edit/lazy/deterministic.ts | 33 +++++++++++++++------------- 4 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 .continue/rules/rules.yaml diff --git a/.continue/rules/rules.yaml b/.continue/rules/rules.yaml new file mode 100644 index 000000000..a5b63ab33 --- /dev/null +++ b/.continue/rules/rules.yaml @@ -0,0 +1,6 @@ +name: Continue Repo rules +version: 1.0.0 +schema: v1 + +mcpServers: + - uses: continuedev/continue-repo-rules diff --git a/core/edit/lazy/applyCodeBlock.ts b/core/edit/lazy/applyCodeBlock.ts index b117c7dea..c533b34e6 100644 --- a/core/edit/lazy/applyCodeBlock.ts +++ b/core/edit/lazy/applyCodeBlock.ts @@ -13,7 +13,7 @@ function canUseInstantApply(filename: string) { export async function applyCodeBlock( oldFile: string, - newFile: string, + newLazyFile: string, filename: string, llm: ILLM, ): Promise<{ @@ -21,11 +21,12 @@ export async function applyCodeBlock( diffLinesGenerator: AsyncGenerator; }> { if (canUseInstantApply(filename)) { - const diffLines = await deterministicApplyLazyEdit( + const diffLines = await deterministicApplyLazyEdit({ oldFile, - newFile, + newLazyFile, filename, - ); + onlyFullFileRewrite: true, + }); if (diffLines !== undefined) { return { diff --git a/core/edit/lazy/deterministic.test.ts b/core/edit/lazy/deterministic.test.ts index 33140a0a5..728c6d8f9 100644 --- a/core/edit/lazy/deterministic.test.ts +++ b/core/edit/lazy/deterministic.test.ts @@ -21,11 +21,11 @@ async function collectDiffs( ): Promise<{ ourDiffs: DiffLine[]; myersDiffs: any }> { const ourDiffs: DiffLine[] = []; - for (const diffLine of (await deterministicApplyLazyEdit( + for (const diffLine of (await deterministicApplyLazyEdit({ oldFile, - newFile, + newLazyFile: newFile, filename, - )) ?? []) { + })) ?? []) { ourDiffs.push(diffLine); } @@ -84,7 +84,7 @@ async function expectDiff(file: string) { } // Skipped while we have `ONLY_FULL_FILE_REWRITE` enabled -describe.skip("deterministicApplyLazyEdit(", () => { +describe("deterministicApplyLazyEdit(", () => { test("no changes", async () => { const file = dedent` function test() { diff --git a/core/edit/lazy/deterministic.ts b/core/edit/lazy/deterministic.ts index b4c6611c9..947f628e4 100644 --- a/core/edit/lazy/deterministic.ts +++ b/core/edit/lazy/deterministic.ts @@ -15,14 +15,6 @@ type AstReplacements = Array<{ replacementNodes: Parser.SyntaxNode[]; }>; -/** - * Using this as a flag to slowly reintroduce lazy applies. - * With this set, we will only attempt to deterministically apply - * when there are no lazy blocks and then just replace the whole file, - * and otherwise never use instant apply - */ -const ONLY_FULL_FILE_REWRITE = true; - const LAZY_COMMENT_REGEX = /\.{3}\s*(.+?)\s*\.{3}/; export function isLazyText(text: string): boolean { return LAZY_COMMENT_REGEX.test(text); @@ -134,11 +126,23 @@ function nodeSurroundedInLazyBlocks( } // TODO: If we don't have high confidence, return undefined to fall back to slower methods -export async function deterministicApplyLazyEdit( - oldFile: string, - newLazyFile: string, - filename: string, -): Promise { +export async function deterministicApplyLazyEdit({ + oldFile, + newLazyFile, + filename, + /** + * Using this as a flag to slowly reintroduce lazy applies. + * With this set, we will only attempt to deterministically apply + * when there are no lazy blocks and then just replace the whole file, + * and otherwise never use instant apply + */ + onlyFullFileRewrite = false, +}: { + oldFile: string; + newLazyFile: string; + filename: string; + onlyFullFileRewrite?: boolean; +}): Promise { const parser = await getParserForFile(filename); if (!parser) { return undefined; @@ -148,8 +152,7 @@ export async function deterministicApplyLazyEdit( let newTree = parser.parse(newLazyFile); let reconstructedNewFile: string | undefined = undefined; - // See comments on `ONLY_FULL_FILE_REWRITE` for context - if (ONLY_FULL_FILE_REWRITE) { + if (onlyFullFileRewrite) { if (!isLazyText(newTree.rootNode.text)) { return myersDiff(oldFile, newLazyFile); } else { From d7e91548870c2d8a3299930a3ce531eab3db2f24 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 14 Apr 2025 10:55:23 -0700 Subject: [PATCH 4/6] Delete rules.yaml --- .continue/rules/rules.yaml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .continue/rules/rules.yaml diff --git a/.continue/rules/rules.yaml b/.continue/rules/rules.yaml deleted file mode 100644 index a5b63ab33..000000000 --- a/.continue/rules/rules.yaml +++ /dev/null @@ -1,6 +0,0 @@ -name: Continue Repo rules -version: 1.0.0 -schema: v1 - -mcpServers: - - uses: continuedev/continue-repo-rules From 1c018592d2811cf00cab27c68eea1bcf228928d9 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 14 Apr 2025 11:00:55 -0700 Subject: [PATCH 5/6] Update deterministic.test.ts --- core/edit/lazy/deterministic.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/core/edit/lazy/deterministic.test.ts b/core/edit/lazy/deterministic.test.ts index 728c6d8f9..adfaafa5e 100644 --- a/core/edit/lazy/deterministic.test.ts +++ b/core/edit/lazy/deterministic.test.ts @@ -83,7 +83,6 @@ async function expectDiff(file: string) { ); } -// Skipped while we have `ONLY_FULL_FILE_REWRITE` enabled describe("deterministicApplyLazyEdit(", () => { test("no changes", async () => { const file = dedent` From 2436bc2437e67ee7c45176b4a1b1b159e216a6bf Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 14 Apr 2025 11:05:44 -0700 Subject: [PATCH 6/6] Update applyCodeBlock.ts --- core/edit/lazy/applyCodeBlock.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/edit/lazy/applyCodeBlock.ts b/core/edit/lazy/applyCodeBlock.ts index c533b34e6..8ca9a8154 100644 --- a/core/edit/lazy/applyCodeBlock.ts +++ b/core/edit/lazy/applyCodeBlock.ts @@ -37,9 +37,9 @@ export async function applyCodeBlock( } // If the code block is a diff - if (isUnifiedDiffFormat(newFile)) { + if (isUnifiedDiffFormat(newLazyFile)) { try { - const diffLines = applyUnifiedDiff(oldFile, newFile); + const diffLines = applyUnifiedDiff(oldFile, newLazyFile); return { isInstantApply: true, diffLinesGenerator: generateLines(diffLines!), @@ -51,6 +51,6 @@ export async function applyCodeBlock( return { isInstantApply: false, - diffLinesGenerator: streamLazyApply(oldFile, filename, newFile, llm), + diffLinesGenerator: streamLazyApply(oldFile, filename, newLazyFile, llm), }; }