fix(coding-agent): use async operations in tools
This commit is contained in:
@@ -10,6 +10,18 @@ function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function createDeferred(): { promise: Promise<void>; resolve: () => void } {
|
||||
let resolve!: () => void;
|
||||
const promise = new Promise<void>((promiseResolve) => {
|
||||
resolve = promiseResolve;
|
||||
});
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
async function resolvesWithin(promise: Promise<unknown>, ms: number): Promise<boolean> {
|
||||
return Promise.race([promise.then(() => true), delay(ms).then(() => false)]);
|
||||
}
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
async function createTempDir(): Promise<string> {
|
||||
@@ -160,4 +172,103 @@ describe("built-in edit and write tools", () => {
|
||||
const content = await readFile(filePath, "utf8");
|
||||
expect(content).toBe("replacement\n");
|
||||
});
|
||||
|
||||
it("keeps write queue locked while an aborted write is still in flight", async () => {
|
||||
const dir = await createTempDir();
|
||||
const filePath = join(dir, "abort-write.txt");
|
||||
const firstWriteStarted = createDeferred();
|
||||
const finishFirstWrite = createDeferred();
|
||||
const secondWriteStarted = createDeferred();
|
||||
let firstWriteSettled = false;
|
||||
|
||||
const writeTool = createWriteTool(dir, {
|
||||
operations: {
|
||||
mkdir: async () => {},
|
||||
writeFile: async (path, content) => {
|
||||
if (content === "first\n") {
|
||||
firstWriteStarted.resolve();
|
||||
await finishFirstWrite.promise;
|
||||
await writeFile(path, content, "utf8");
|
||||
firstWriteSettled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (content === "second\n") {
|
||||
expect(firstWriteSettled).toBe(true);
|
||||
secondWriteStarted.resolve();
|
||||
}
|
||||
await writeFile(path, content, "utf8");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const controller = new AbortController();
|
||||
const firstWrite = writeTool.execute("call-1", { path: filePath, content: "first\n" }, controller.signal);
|
||||
await firstWriteStarted.promise;
|
||||
controller.abort();
|
||||
|
||||
const secondWrite = writeTool.execute("call-2", { path: filePath, content: "second\n" });
|
||||
expect(await resolvesWithin(secondWriteStarted.promise, 20)).toBe(false);
|
||||
|
||||
finishFirstWrite.resolve();
|
||||
await expect(firstWrite).rejects.toThrow("Operation aborted");
|
||||
await secondWrite;
|
||||
|
||||
const content = await readFile(filePath, "utf8");
|
||||
expect(content).toBe("second\n");
|
||||
});
|
||||
|
||||
it("keeps edit queue locked while an aborted edit write is still in flight", async () => {
|
||||
const dir = await createTempDir();
|
||||
const filePath = join(dir, "abort-edit.txt");
|
||||
await writeFile(filePath, "alpha\nbeta\n", "utf8");
|
||||
const firstWriteStarted = createDeferred();
|
||||
const finishFirstWrite = createDeferred();
|
||||
const secondWriteStarted = createDeferred();
|
||||
let firstWriteSettled = false;
|
||||
|
||||
const editTool = createEditTool(dir, {
|
||||
operations: {
|
||||
access,
|
||||
readFile,
|
||||
writeFile: async (path, content) => {
|
||||
if (content === "ALPHA\nbeta\n") {
|
||||
firstWriteStarted.resolve();
|
||||
await finishFirstWrite.promise;
|
||||
await writeFile(path, content, "utf8");
|
||||
firstWriteSettled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (content === "ALPHA\nBETA\n" || content === "alpha\nBETA\n") {
|
||||
expect(firstWriteSettled).toBe(true);
|
||||
secondWriteStarted.resolve();
|
||||
}
|
||||
await writeFile(path, content, "utf8");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const controller = new AbortController();
|
||||
const firstEdit = editTool.execute(
|
||||
"call-1",
|
||||
{ path: filePath, edits: [{ oldText: "alpha", newText: "ALPHA" }] },
|
||||
controller.signal,
|
||||
);
|
||||
await firstWriteStarted.promise;
|
||||
controller.abort();
|
||||
|
||||
const secondEdit = editTool.execute("call-2", {
|
||||
path: filePath,
|
||||
edits: [{ oldText: "beta", newText: "BETA" }],
|
||||
});
|
||||
expect(await resolvesWithin(secondWriteStarted.promise, 20)).toBe(false);
|
||||
|
||||
finishFirstWrite.resolve();
|
||||
await expect(firstEdit).rejects.toThrow("Operation aborted");
|
||||
await secondEdit;
|
||||
|
||||
const content = await readFile(filePath, "utf8");
|
||||
expect(content).toBe("ALPHA\nBETA\n");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,6 +22,10 @@ const MEDIUM_PNG_100x100 =
|
||||
const LARGE_PNG_200x200 =
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAMgAAADIEAIAAAByquWKAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAGYktHRP///////wlY99wAAAAHdElNRQfqAQ4AMzqU03BFAAAEEElEQVR42u3dvWqUURSF4XXMRAyIhSAWQvwnWtjaWusFx9bW1kKD/wELESxEiBjDjJdwysOC57mCXW32+zGcGbtdACpsdi+T8Xz1GABzm91xMl6sHgNgblzcT/Y+rh4DYG6cv0/2H64eA2BOEgI1fHQHaoyzbXIwVo8BMCcJgRqSEKgxft9Lrn5aPQbA3Ph1klw7Wj0GwJwkBGpYWECN8fMiub63egyAORcWUMPCAmqMH3eTG59XjwEwN76/S24+Wj0GwJwkBGpYWECN8e1fcmuzegyAORcWUMPCAmqM09vJ4dfVYwDMjS9vkzuPV48BMCcJgRqeSAZqjA/nyYP91WMAzElCoIYkBGqMk8Pk6HT1GABzm+3L5JIkBApIQqCGj+5AjfHmb/Lk8uoxAOYkIVBDEgI1LCygxmZ7nFyShEABFxZQw8ICaozXf5KnV1aPATDnwgJqWFhADQsLqOFnDUANFxZQw8ICaoxXZ8mzg9VjAMy5sIAaFhZQw3tYQA1/QgHUkIRADUkI1JCEQA1JCNSQhEANFxZQwzcsoIYkBGpIQqCGJARqSEKghiQEalhYQA1/QgHUcGEBNSwsoIYkBGq4sIAaFhZQw8ICaviGBdRwYQE1LCyghiQEariwgBoWFlDDe1hADS+OAjUkIVBDEgI1JCFQQxICNSQhUMOFBdTwDQuoIQmBGpIQqCEJgRqSEKghCYEaFhZQw4ujQA0XFlDDwgJqSEKghgsLqGFhATUsLKCGb1hADRcWUMPCAmpIQqCGCwuoYWEBNbyHBdTw4ihQQxICNSQhUEMSAjUkIVBDEgI1XFhADd+wgBqSEKghCYEakhCoIQmBGpIQqGFhATW8OArUcGEBNSwsoIYkBGq4sIAaFhZQw8ICaviGBdRwYQE1LCyghiQEariwgBoWFlDDe1hADS+OAjUkIVBDEgI1JCFQQxICNSQhUMOFBdTwDQuoIQmBGpIQqCEJgRqSEKghCYEa49lB8ups9RgAc14cBWpIQqCGhQXUkIRADRcWUMPCAmqMp1eS139WjwEw5xsWUEMSAjUsLKCGJARquLCAGhYWUGM8uZy8+bt6DIA5L44CNSQhUMMTyUCNcXSanByuHgNgThICNSQhUGM82E8+nK8eA2DOzxqAGpIQqDHuPE6+vF09BsDcOPyanN5ePQbAnCQEavgdFlBj3Nok3/6tHgNgzoujQA1JCNQYNx8l39+tHgNgbtz4nPy4u3oMgDlJCNSwsIAa4/pe8vNi9RgAc37WANSQhECNce0o+XWyegyAuXH1U/L73uoxAOYkIVDDwgJqjIORnG1XjwEw508ogBqSEKgx9h8m5+9XjwEwN/Y+Jhf3V48BMCcJgRpjPE+2x6vHAJgbSbLbrR4DYO4/GqiSgXN+ksgAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjYtMDEtMTRUMDA6NTE6NTcrMDA6MDDpysx4AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDI2LTAxLTE0VDAwOjUxOjU3KzAwOjAwmJd0xAAAACh0RVh0ZGF0ZTp0aW1lc3RhbXAAMjAyNi0wMS0xNFQwMDo1MTo1NyswMDowMM+CVRsAAAAASUVORK5CYII=";
|
||||
|
||||
function imageBytes(base64Data: string): Uint8Array {
|
||||
return Buffer.from(base64Data, "base64");
|
||||
}
|
||||
|
||||
describe("convertToPng", () => {
|
||||
it("should return original data for PNG input", async () => {
|
||||
const result = await convertToPng(TINY_PNG, "image/png");
|
||||
@@ -46,11 +50,28 @@ describe("convertToPng", () => {
|
||||
});
|
||||
|
||||
describe("resizeImage", () => {
|
||||
it("should keep caller input bytes intact", async () => {
|
||||
const input = new Uint8Array(imageBytes(TINY_PNG));
|
||||
const originalByteLength = input.byteLength;
|
||||
const originalFirstByte = input[0];
|
||||
|
||||
const result = await resizeImage(input, "image/png", {
|
||||
maxWidth: 100,
|
||||
maxHeight: 100,
|
||||
maxBytes: 1024 * 1024,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(input.byteLength).toBe(originalByteLength);
|
||||
expect(input[0]).toBe(originalFirstByte);
|
||||
});
|
||||
|
||||
it("should return original image if within limits", async () => {
|
||||
const result = await resizeImage(
|
||||
{ type: "image", data: TINY_PNG, mimeType: "image/png" },
|
||||
{ maxWidth: 100, maxHeight: 100, maxBytes: 1024 * 1024 },
|
||||
);
|
||||
const result = await resizeImage(imageBytes(TINY_PNG), "image/png", {
|
||||
maxWidth: 100,
|
||||
maxHeight: 100,
|
||||
maxBytes: 1024 * 1024,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.wasResized).toBe(false);
|
||||
@@ -62,10 +83,11 @@ describe("resizeImage", () => {
|
||||
});
|
||||
|
||||
it("should resize image exceeding dimension limits", async () => {
|
||||
const result = await resizeImage(
|
||||
{ type: "image", data: MEDIUM_PNG_100x100, mimeType: "image/png" },
|
||||
{ maxWidth: 50, maxHeight: 50, maxBytes: 1024 * 1024 },
|
||||
);
|
||||
const result = await resizeImage(imageBytes(MEDIUM_PNG_100x100), "image/png", {
|
||||
maxWidth: 50,
|
||||
maxHeight: 50,
|
||||
maxBytes: 1024 * 1024,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.wasResized).toBe(true);
|
||||
@@ -80,10 +102,11 @@ describe("resizeImage", () => {
|
||||
const originalSize = originalBuffer.length;
|
||||
|
||||
// Set maxBytes to less than the original encoded image size
|
||||
const result = await resizeImage(
|
||||
{ type: "image", data: LARGE_PNG_200x200, mimeType: "image/png" },
|
||||
{ maxWidth: 2000, maxHeight: 2000, maxBytes: Math.floor(LARGE_PNG_200x200.length * 0.9) },
|
||||
);
|
||||
const result = await resizeImage(imageBytes(LARGE_PNG_200x200), "image/png", {
|
||||
maxWidth: 2000,
|
||||
maxHeight: 2000,
|
||||
maxBytes: Math.floor(LARGE_PNG_200x200.length * 0.9),
|
||||
});
|
||||
|
||||
// Should have tried to reduce size
|
||||
expect(result).not.toBeNull();
|
||||
@@ -93,19 +116,21 @@ describe("resizeImage", () => {
|
||||
});
|
||||
|
||||
it("should return null when image cannot be resized below maxBytes", async () => {
|
||||
const result = await resizeImage(
|
||||
{ type: "image", data: LARGE_PNG_200x200, mimeType: "image/png" },
|
||||
{ maxWidth: 2000, maxHeight: 2000, maxBytes: 1 },
|
||||
);
|
||||
const result = await resizeImage(imageBytes(LARGE_PNG_200x200), "image/png", {
|
||||
maxWidth: 2000,
|
||||
maxHeight: 2000,
|
||||
maxBytes: 1,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should handle JPEG input", async () => {
|
||||
const result = await resizeImage(
|
||||
{ type: "image", data: TINY_JPEG, mimeType: "image/jpeg" },
|
||||
{ maxWidth: 100, maxHeight: 100, maxBytes: 1024 * 1024 },
|
||||
);
|
||||
const result = await resizeImage(imageBytes(TINY_JPEG), "image/jpeg", {
|
||||
maxWidth: 100,
|
||||
maxHeight: 100,
|
||||
maxBytes: 1024 * 1024,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.wasResized).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user