From 62176ce0d813a15344dd1da9dafeffd5278d7e60 Mon Sep 17 00:00:00 2001 From: Shivaditya Shivganesh Date: Sat, 11 Jan 2025 07:55:18 +0530 Subject: [PATCH] fix: write tool malformed requests --- src/tools/create-pr/index.ts | 2 ++ src/tools/write-file/index.ts | 58 +++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/tools/create-pr/index.ts b/src/tools/create-pr/index.ts index 30a95f26..66139dc6 100644 --- a/src/tools/create-pr/index.ts +++ b/src/tools/create-pr/index.ts @@ -52,6 +52,8 @@ export class CreatePr implements Tool { try { // Stage all changes this._context.logger.info("Staging changes"); + const res = await this._terminal.runCommand("ls -la"); + this._context.logger.info("ls -la:", { res }); await this._terminal.runCommand("git add ."); // Get status to log what's being committed diff --git a/src/tools/write-file/index.ts b/src/tools/write-file/index.ts index 71a8f9b1..6fbc497b 100644 --- a/src/tools/write-file/index.ts +++ b/src/tools/write-file/index.ts @@ -91,20 +91,16 @@ export class WriteFile implements Tool { newContent = content; } - // Write content and verify + // Write content writeFileSync(resolvedPath, newContent, "utf8"); - // Verify write succeeded - if (!existsSync(resolvedPath)) { - throw new Error("File write failed - file does not exist after write"); + // Verify the write operation + const verificationResult = this._verifyWrite(resolvedPath, newContent); + if (!verificationResult.success || verificationResult.bytesWritten === undefined) { + throw new Error(`File write verification failed: ${verificationResult.error || "Unknown error"}`); } - const writtenContent = readFileSync(resolvedPath, "utf-8"); - if (writtenContent !== newContent) { - throw new Error("File write verification failed - content mismatch"); - } - - const bytesWritten = Buffer.from(newContent).length; + const bytesWritten = verificationResult.bytesWritten; return { success: true, @@ -130,4 +126,46 @@ export class WriteFile implements Tool { }; } } + + private _verifyWrite(filePath: string, expectedContent: string): { success: boolean; error?: string; bytesWritten?: number } { + try { + // Check if file exists + if (!existsSync(filePath)) { + return { success: false, error: "File does not exist after write operation" }; + } + + // Read back the written content + const writtenContent = readFileSync(filePath, "utf-8"); + + // Check content length + const expectedBytes = Buffer.from(expectedContent).length; + const actualBytes = Buffer.from(writtenContent).length; + + if (actualBytes !== expectedBytes) { + return { + success: false, + error: `Content length mismatch - expected ${expectedBytes} bytes but got ${actualBytes} bytes`, + }; + } + + // Check content equality + if (writtenContent !== expectedContent) { + return { + success: false, + error: "Written content does not match expected content", + }; + } + + // All verifications passed + return { + success: true, + bytesWritten: actualBytes, + }; + } catch (error) { + return { + success: false, + error: `Verification failed with error: ${error instanceof Error ? error.message : "Unknown error"}`, + }; + } + } }