-
Notifications
You must be signed in to change notification settings - Fork 36
feat: add raw openclaw config editor #828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { atomicWrite, type AtomicWriteDeps } from './atomic-write.js'; | ||
|
|
||
| function makeDeps(overrides: Partial<AtomicWriteDeps> = {}): AtomicWriteDeps { | ||
| return { | ||
| writeFileSync: vi.fn(), | ||
| renameSync: vi.fn(), | ||
| unlinkSync: vi.fn(), | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('atomicWrite', () => { | ||
| it('writes to a temp file then renames into place', () => { | ||
| const deps = makeDeps(); | ||
| atomicWrite('/config/openclaw.json', '{"ok":true}', deps); | ||
|
|
||
| expect(deps.writeFileSync).toHaveBeenCalledOnce(); | ||
| expect(deps.renameSync).toHaveBeenCalledOnce(); | ||
|
|
||
| // The temp file should be in the same directory with a .kilotmp suffix | ||
| const tmpPath = (deps.writeFileSync as ReturnType<typeof vi.fn>).mock.calls[0][0] as string; | ||
| expect(tmpPath).toMatch(/^\/config\/\.openclaw\.json\.kilotmp\.[0-9a-f]+$/); | ||
| expect((deps.writeFileSync as ReturnType<typeof vi.fn>).mock.calls[0][1]).toBe('{"ok":true}'); | ||
|
|
||
| // Rename should move the temp file to the final path | ||
| expect(deps.renameSync).toHaveBeenCalledWith(tmpPath, '/config/openclaw.json'); | ||
|
|
||
| // No cleanup needed on success | ||
| expect(deps.unlinkSync).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not call rename when write fails, and cleans up temp file', () => { | ||
| const writeError = new Error('disk full'); | ||
| const deps = makeDeps({ | ||
| writeFileSync: vi.fn().mockImplementation(() => { | ||
| throw writeError; | ||
| }), | ||
| }); | ||
|
|
||
| expect(() => atomicWrite('/config/openclaw.json', 'data', deps)).toThrow(writeError); | ||
|
|
||
| expect(deps.renameSync).not.toHaveBeenCalled(); | ||
| expect(deps.unlinkSync).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('unlinks temp file and rethrows when rename fails', () => { | ||
| const renameError = new Error('rename failed'); | ||
| const deps = makeDeps({ | ||
| renameSync: vi.fn().mockImplementation(() => { | ||
| throw renameError; | ||
| }), | ||
| }); | ||
|
|
||
| expect(() => atomicWrite('/config/openclaw.json', 'data', deps)).toThrow(renameError); | ||
|
|
||
| // Write succeeded, so temp file was created — should be cleaned up | ||
| const tmpPath = (deps.writeFileSync as ReturnType<typeof vi.fn>).mock.calls[0][0] as string; | ||
| expect(deps.unlinkSync).toHaveBeenCalledWith(tmpPath); | ||
| }); | ||
|
|
||
| it('rethrows the original error when cleanup also fails', () => { | ||
| const renameError = new Error('rename failed'); | ||
| const unlinkError = new Error('unlink failed'); | ||
| const deps = makeDeps({ | ||
| renameSync: vi.fn().mockImplementation(() => { | ||
| throw renameError; | ||
| }), | ||
| unlinkSync: vi.fn().mockImplementation(() => { | ||
| throw unlinkError; | ||
| }), | ||
| }); | ||
|
|
||
| // Should throw the original rename error, not the unlink error | ||
| expect(() => atomicWrite('/config/openclaw.json', 'data', deps)).toThrow(renameError); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * Atomic file write: writes to a temp file then renames into place. | ||
| * Ensures a crash mid-write cannot leave a corrupted target file. | ||
| * Cleans up the temp file on failure. | ||
| */ | ||
| import crypto from 'node:crypto'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| export type AtomicWriteDeps = { | ||
| writeFileSync: (path: string, data: string) => void; | ||
| renameSync: (oldPath: string, newPath: string) => void; | ||
| unlinkSync: (path: string) => void; | ||
| }; | ||
|
|
||
| const defaultDeps: AtomicWriteDeps = { | ||
| writeFileSync: (p, data) => fs.writeFileSync(p, data), | ||
| renameSync: (oldPath, newPath) => fs.renameSync(oldPath, newPath), | ||
| unlinkSync: p => fs.unlinkSync(p), | ||
| }; | ||
|
|
||
| /** | ||
| * Atomically write `data` to `filePath` by writing to a temp file first, | ||
| * then renaming into place. The temp file is cleaned up on failure. | ||
| */ | ||
| export function atomicWrite( | ||
| filePath: string, | ||
| data: string, | ||
| deps: AtomicWriteDeps = defaultDeps | ||
| ): void { | ||
| const dir = path.dirname(filePath); | ||
| const base = path.basename(filePath); | ||
| const tmpPath = path.join(dir, `.${base}.kilotmp.${crypto.randomBytes(6).toString('hex')}`); | ||
|
|
||
| try { | ||
| deps.writeFileSync(tmpPath, data); | ||
| deps.renameSync(tmpPath, filePath); | ||
| } catch (error) { | ||
| // Clean up the temp file so we don't leak partial writes | ||
| try { | ||
| deps.unlinkSync(tmpPath); | ||
| } catch { | ||
| // Best-effort cleanup — the dotfile prefix keeps it hidden at least | ||
| } | ||
| throw error; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.