From 68beae55b5aa925188af01429a349e239a7f43c3 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Thu, 31 Jul 2025 17:46:28 -0700 Subject: [PATCH] refactor: move the next editable region list to prefetch queue --- core/nextEdit/NextEditPrefetchQueue.ts | 59 +++++++++++++++++--------- core/nextEdit/NextEditProvider.ts | 59 +++----------------------- 2 files changed, 46 insertions(+), 72 deletions(-) diff --git a/core/nextEdit/NextEditPrefetchQueue.ts b/core/nextEdit/NextEditPrefetchQueue.ts index 66e680835..38b5589c6 100644 --- a/core/nextEdit/NextEditPrefetchQueue.ts +++ b/core/nextEdit/NextEditPrefetchQueue.ts @@ -1,26 +1,43 @@ +import { RangeInFile } from ".."; import { NextEditProvider } from "./NextEditProvider"; +import { NextEditOutcome } from "./types"; + +interface ProcessedItem { + location: RangeInFile; + outcome: NextEditOutcome; // Result from the model +} export class PrefetchQueue { - private unprocessedQueue: EditLocation[] = []; + private static instance: PrefetchQueue | null = null; + + private unprocessedQueue: RangeInFile[] = []; private processedQueue: ProcessedItem[] = []; private prefetchLimit: number; private abortController: AbortController; - constructor(prefetchLimit: number = 3) { + private constructor(prefetchLimit: number = 3) { this.prefetchLimit = prefetchLimit; this.abortController = new AbortController(); } + public static getInstance(prefetchLimit: number = 3): PrefetchQueue { + if (!PrefetchQueue.instance) { + PrefetchQueue.instance = new PrefetchQueue(prefetchLimit); + } + + return PrefetchQueue.instance; + } + // Queue management methods - enqueueUnprocessed(location: EditLocation): void { + enqueueUnprocessed(location: RangeInFile): void { this.unprocessedQueue.push(location); } - dequeueUnprocessed(): EditLocation | undefined { + private dequeueUnprocessed(): RangeInFile | undefined { return this.unprocessedQueue.shift(); } - enqueueProcessed(item: ProcessedItem): void { + private enqueueProcessed(item: ProcessedItem): void { this.processedQueue.push(item); } @@ -30,11 +47,9 @@ export class PrefetchQueue { // Process items from unprocessed queue async process(ctx: any): Promise { - let processCount = 0; - while ( this.unprocessedQueue.length > 0 && - processCount < this.prefetchLimit && + this.processedQueue.length < this.prefetchLimit && !this.abortController.signal.aborted ) { const location = this.dequeueUnprocessed(); @@ -48,12 +63,12 @@ export class PrefetchQueue { this.abortController.signal, ); + if (!outcome) continue; + this.enqueueProcessed({ location, outcome, }); - - processCount++; } catch (error) { if (!this.abortController.signal.aborted) { // Handle error @@ -79,15 +94,21 @@ export class PrefetchQueue { this.unprocessedQueue = []; this.processedQueue = []; } -} -// Types -interface EditLocation { - // Define properties for edit location - // e.g., file, position, etc. -} + // Additional helper methods + get unprocessedCount(): number { + return this.unprocessedQueue.length; + } -interface ProcessedItem { - location: EditLocation; - outcome: any; // Result from the model + get processedCount(): number { + return this.processedQueue.length; + } + + peekProcessed(): ProcessedItem | undefined { + return this.processedQueue[0]; + } + + setPreetchLimit(limit: number): void { + this.prefetchLimit = limit; + } } diff --git a/core/nextEdit/NextEditProvider.ts b/core/nextEdit/NextEditProvider.ts index 1ae21c152..cfb44a950 100644 --- a/core/nextEdit/NextEditProvider.ts +++ b/core/nextEdit/NextEditProvider.ts @@ -28,10 +28,6 @@ import { import { createDiff, DiffFormatType } from "./context/diffFormatting.js"; import { calculateFinalCursorPosition } from "./diff/diff.js"; import { DocumentHistoryTracker } from "./DocumentHistoryTracker.js"; -import { - EditableRegionStrategy, - getNextEditableRegion, -} from "./NextEditEditableRegionCalculator.js"; import { NextEditLoggingService } from "./NextEditLoggingService.js"; import { renderDefaultSystemPrompt, @@ -71,7 +67,7 @@ export class NextEditProvider { private currentEditChainId: string | null = null; private previousRequest: AutocompleteInput | null = null; private previousCompletions: NextEditOutcome[] = []; - private nextEditableRegionsInTheCurrentChain: RangeInFile[] = []; + // private nextEditableRegionsInTheCurrentChain: RangeInFile[] = []; private constructor( private readonly configHandler: ConfigHandler, @@ -220,7 +216,8 @@ export class NextEditProvider { public async deleteChain(): Promise { this.currentEditChainId = null; this.previousCompletions = []; - this.nextEditableRegionsInTheCurrentChain = []; + // TODO: this should be cleaned up in the prefetch queue. + // this.nextEditableRegionsInTheCurrentChain = []; if (this.previousRequest) { const fileContent = ( @@ -273,14 +270,6 @@ export class NextEditProvider { return undefined; } - // Depending on whether this method is called from provideInlineCompletionItemsWithChain, - // shift the next editable regions. - // This is handled here because this must run after the debouncer - // to prevent excessive shifts. - if (opts?.withChain) { - this.shiftNextEditableRegionsInTheCurrentChain(); - } - const llm = await this._prepareLlm(); if (!llm) { return undefined; @@ -527,6 +516,7 @@ export class NextEditProvider { recentlyVisitedRanges: AutocompleteCodeSnippet[]; recentlyEditedRanges: RecentlyEditedRange[]; }, + nextEditLocation: RangeInFile, token: AbortSignal | undefined, ) { try { @@ -535,31 +525,10 @@ export class NextEditProvider { return undefined; } - const filepath = localPathOrUriToPath(previousOutcome.fileUri); - - // If we don't have a precomputed list of next editable regions already, compute and load it. - if (this.nextEditableRegionsInTheCurrentChain.length === 0) { - this.loadNextEditableRegionsInTheCurrentChain( - (await getNextEditableRegion(EditableRegionStrategy.Static, { - cursorPosition: previousOutcome.cursorPosition, - filepath, - ide: this.ide, - })) ?? [], - ); - - if (this.nextEditableRegionsInTheCurrentChain.length === 0) { - console.log( - "deleteChain called from provideInlineCompletionItemsWithChain", - ); - await this.deleteChain(); - return undefined; - } - } - // Use the frontmost RangeInFile to build an input. - const input = await this.buildAutocompleteInputFromChain( + const input = this.buildAutocompleteInputFromChain( previousOutcome, - this.nextEditableRegionsInTheCurrentChain[0], // this will be shifted after debouncer check + nextEditLocation, ctx, ); if (!input) { @@ -601,20 +570,4 @@ export class NextEditProvider { return input; } - - public loadNextEditableRegionsInTheCurrentChain(regions: RangeInFile[]) { - this.nextEditableRegionsInTheCurrentChain = regions; - } - - public shiftNextEditableRegionsInTheCurrentChain() { - return this.nextEditableRegionsInTheCurrentChain.shift(); - } - - public getNextEditableRegionsInTheCurrentChainLength() { - return this.nextEditableRegionsInTheCurrentChain.length; - } - - public getNextEditableRegionsInTheCurrentChain(): RangeInFile[] { - return [...this.nextEditableRegionsInTheCurrentChain]; // Return a copy to prevent external modification - } }