Skip to content

Commit

Permalink
fix: write tool malformed requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sshivaditya committed Jan 11, 2025
1 parent 5ae2e8c commit 62176ce
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/tools/create-pr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class CreatePr implements Tool<PullRequestResult> {
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
Expand Down
58 changes: 48 additions & 10 deletions src/tools/write-file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,16 @@ export class WriteFile implements Tool<FileWriteResult> {
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,
Expand All @@ -130,4 +126,46 @@ export class WriteFile implements Tool<FileWriteResult> {
};
}
}

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"}`,
};
}
}
}

0 comments on commit 62176ce

Please sign in to comment.