made pipeline w/ LRU cache for recent edit sequences

This commit is contained in:
adarshramiyer 2025-07-03 17:05:16 -07:00
parent 7f756d7590
commit e387c0ac49
3 changed files with 90 additions and 4 deletions

View file

@ -722,8 +722,8 @@ export class Core {
const EDIT_AGGREGATION_OPTIONS = {
deltaT: 1.0,
deltaL: 5,
maxEdits: 250,
maxDuration: 100.0,
maxEdits: 500,
maxDuration: 120.0,
contextSize: 5,
};

View file

@ -0,0 +1,30 @@
import QuickLRU from "quick-lru";
export interface prevEdit {
unidiff: string;
fileUri: string;
workspaceUri: string;
timestamp: number;
}
const maxPrevEdits = 5;
export const prevEditLruCache = new QuickLRU<string, prevEdit>({
maxSize: maxPrevEdits,
});
export const setPrevEdit = (edit: prevEdit): void => {
const uniqueSuffix = Math.random().toString(36).substring(2, 8);
const key = `${edit.fileUri}:${edit.timestamp}:${uniqueSuffix}`;
prevEditLruCache.set(key, edit);
};
export const getPrevEditsDescending = (): prevEdit[] => {
const edits: prevEdit[] = [];
for (const [_, edit] of prevEditLruCache.entriesDescending()) {
if (edits.length >= maxPrevEdits) {
break;
}
edits.push(edit);
}
return edits;
};

View file

@ -5,6 +5,13 @@ import { ConfigHandler } from "../../config/ConfigHandler";
import { DataLogger } from "../../data/log";
import { RecentlyEditedRange } from "../types";
import { getAutocompleteContext } from "./autocompleteContextFetching";
import { createDiff, DiffFormatType } from "./diffFormatting";
import {
getPrevEditsDescending,
prevEdit,
prevEditLruCache,
setPrevEdit,
} from "./prevEditLruCache";
const randomNumberBetween = (min: number, max: number) => {
min = Math.ceil(min); // Ensure min is an integer
@ -27,6 +34,11 @@ interface ProcessNextEditDataParams {
modelNameOrInstance?: string | undefined;
}
interface filenameAndDiff {
filename: string;
diff: string;
}
export const processNextEditData = async ({
filePath,
beforeContent,
@ -48,7 +60,6 @@ export const processNextEditData = async ({
// undefined;
const modelName = "Codestral";
const maxPromptTokens = randomNumberBetween(500, 12000);
const autocompleteContext = await getAutocompleteContext(
@ -68,9 +79,39 @@ export const processNextEditData = async ({
// createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified),
// );
let filenamesAndDiffs: filenameAndDiff[] = [];
const timestamp = Date.now();
let prevEdits: prevEdit[] = getPrevEditsDescending(); // edits from most to least recent
if (prevEdits.length > 0) {
// if last edit was 10+ minutes ago or the workspace changed, forget previous edits
if (
timestamp - prevEdits[0].timestamp >= 1000 * 60 * 10 ||
workspaceDir !== prevEdits[0].workspaceUri
) {
prevEditLruCache.clear();
prevEdits = [];
}
// extract filenames and diffs for logging
filenamesAndDiffs = prevEdits.map(
(edit) =>
({
// filename relative to workspace dir
filename: edit.fileUri
.replace(edit.workspaceUri, "")
.replace(/^[/\\]/, ""),
// diff without the first 4 lines (the file header)
diff: edit.unidiff.split("\n").slice(4).join("\n"),
}) as filenameAndDiff,
);
}
void DataLogger.getInstance().logDevData({
name: "nextEdit",
name: "nextEditWithHistory",
data: {
previousEdits: filenamesAndDiffs,
fileURI: filePath,
workspaceDirURI: workspaceDir,
beforeContent,
@ -80,4 +121,19 @@ export const processNextEditData = async ({
context: autocompleteContext,
},
});
// add current edit to history
const thisEdit: prevEdit = {
unidiff: createDiff(
beforeContent,
afterContent,
filePath,
DiffFormatType.Unified,
),
fileUri: filePath,
workspaceUri: workspaceDir,
timestamp: timestamp,
};
setPrevEdit(thisEdit);
};