Skip to content

Commit 11c2151

Browse files
committed
🤖 Fix ENOENT error in todo_write for SSH runtimes
Ensure temp directory exists before writing todos.json. Previously, writeTodos() assumed the directory already existed, but SSH runtime initialization could fail to create it, resulting in ENOENT errors. Added fs.mkdir() with recursive: true to create the directory if needed.
1 parent 842609c commit 11c2151

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/services/tools/todo.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,37 @@ describe("Todo Storage", () => {
209209
await setTodosForTempDir(runtimeTempDir, todos);
210210
expect(await getTodosForTempDir(runtimeTempDir)).toEqual(todos);
211211
});
212+
213+
it("should create directory if it doesn't exist", async () => {
214+
// Use a non-existent nested directory path
215+
const nonExistentDir = path.join(os.tmpdir(), "todo-nonexistent-test", "nested", "path");
216+
217+
try {
218+
const todos: TodoItem[] = [
219+
{
220+
content: "Test task",
221+
status: "pending",
222+
},
223+
];
224+
225+
// Should not throw even though directory doesn't exist
226+
await setTodosForTempDir(nonExistentDir, todos);
227+
228+
// Verify the file was created and is readable
229+
const retrievedTodos = await getTodosForTempDir(nonExistentDir);
230+
expect(retrievedTodos).toEqual(todos);
231+
232+
// Verify the directory was actually created
233+
const dirStats = await fs.stat(nonExistentDir);
234+
expect(dirStats.isDirectory()).toBe(true);
235+
} finally {
236+
// Clean up the created directory
237+
await fs.rm(path.join(os.tmpdir(), "todo-nonexistent-test"), {
238+
recursive: true,
239+
force: true,
240+
});
241+
}
242+
});
212243
});
213244

214245
describe("getTodosForTempDir", () => {

src/services/tools/todo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ function validateTodos(todos: TodoItem[]): void {
104104
async function writeTodos(tempDir: string, todos: TodoItem[]): Promise<void> {
105105
validateTodos(todos);
106106
const todoFile = getTodoFilePath(tempDir);
107+
// Ensure directory exists before writing (SSH runtime might not have created it yet)
108+
await fs.mkdir(tempDir, { recursive: true });
107109
await fs.writeFile(todoFile, JSON.stringify(todos, null, 2), "utf-8");
108110
}
109111

0 commit comments

Comments
 (0)