refactor: move the next editable region list to prefetch queue

This commit is contained in:
Jacob Kim 2025-07-31 17:46:28 -07:00
parent 90d07b4b4a
commit 68beae55b5
2 changed files with 46 additions and 72 deletions

View file

@ -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<void> {
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;
}
}

View file

@ -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<void> {
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
}
}