Repository navigation
fix(kiloclaw): size snapshot restore volume from snapshot source #6239
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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { handleSnapshotRestoreQueue } from './snapshot-restore'; | ||
| import * as fly from '../fly/client'; | ||
| import type * as flyClientModule from '../fly/client'; | ||
| import type { FlyVolume, FlyVolumeSnapshot } from '../fly/types'; | ||
| import type { KiloClawEnv } from '../types'; | ||
| import type { SnapshotRestoreMessage } from '../schemas/snapshot-restore'; | ||
|
|
||
| vi.mock('../fly/client', async importOriginal => { | ||
| const actual = await importOriginal<typeof flyClientModule>(); | ||
| return { | ||
| ...actual, | ||
| getVolume: vi.fn(), | ||
| listVolumes: vi.fn(), | ||
| listVolumeSnapshots: vi.fn(), | ||
| createVolume: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| const flyMock = vi.mocked(fly); | ||
| const GIB = 1024 ** 3; | ||
|
|
||
| const MESSAGE: SnapshotRestoreMessage = { | ||
| userId: 'user-1', | ||
| snapshotId: 'vs-1', | ||
| previousVolumeId: 'vol-current', | ||
| region: 'ord', | ||
| instanceId: 'inst-1', | ||
| }; | ||
|
|
||
| function volume(overrides: Partial<FlyVolume> = {}): FlyVolume { | ||
| return { | ||
| id: 'vol-current', | ||
| name: 'kiloclaw_volume', | ||
| state: 'created', | ||
| size_gb: 10, | ||
| region: 'ord', | ||
| attached_machine_id: null, | ||
| created_at: '2026-01-01T00:00:00Z', | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function snapshot(overrides: Partial<FlyVolumeSnapshot> = {}): FlyVolumeSnapshot { | ||
| return { | ||
| id: 'vs-1', | ||
| created_at: '2026-01-01T00:00:00Z', | ||
| digest: 'digest', | ||
| retention_days: 5, | ||
| size: 1, | ||
| status: 'created', | ||
| volume_size: 10 * GIB, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function makeStub() { | ||
| return { | ||
| getStatus: vi.fn().mockResolvedValue({ flyVolumeId: 'vol-current', flyAppName: 'acct-test' }), | ||
| stop: vi.fn().mockResolvedValue(undefined), | ||
| destroyMachineForRestore: vi.fn().mockResolvedValue(undefined), | ||
| getDebugState: vi.fn().mockResolvedValue({ pendingRestoreVolumeId: null }), | ||
| setPendingRestoreVolumeId: vi.fn().mockResolvedValue(undefined), | ||
| completeSnapshotRestore: vi.fn().mockResolvedValue(undefined), | ||
| start: vi.fn().mockResolvedValue(undefined), | ||
| }; | ||
| } | ||
|
|
||
| function makeEnv(stub: ReturnType<typeof makeStub>): KiloClawEnv { | ||
| return { | ||
| KILOCLAW_INSTANCE: { | ||
| idFromName: (name: string) => name, | ||
| get: () => stub, | ||
| }, | ||
| FLY_API_TOKEN: 'token', | ||
| FLY_APP_NAME: 'fallback-app', | ||
| } as unknown as KiloClawEnv; | ||
| } | ||
|
|
||
| function makeBatch(body: SnapshotRestoreMessage = MESSAGE) { | ||
| const message = { body, ack: vi.fn(), retry: vi.fn(), attempts: 1 }; | ||
| const batch = { | ||
| messages: [message], | ||
| } as unknown as Parameters<typeof handleSnapshotRestoreQueue>[0]; | ||
| return { batch, message }; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| flyMock.getVolume.mockResolvedValue(volume()); | ||
| flyMock.listVolumes.mockResolvedValue([volume(), volume({ id: 'vol-previous', size_gb: 11 })]); | ||
| flyMock.listVolumeSnapshots.mockResolvedValue([]); | ||
| flyMock.createVolume.mockResolvedValue(volume({ id: 'vol-restored' })); | ||
| }); | ||
|
|
||
| describe('handleSnapshotRestoreQueue', () => { | ||
| it('sizes the restore volume from the snapshot source when it is larger than the current volume', async () => { | ||
| flyMock.listVolumeSnapshots.mockImplementation(async (_config, volumeId) => | ||
| volumeId === 'vol-previous' ? [snapshot({ volume_size: 11 * GIB })] : [] | ||
| ); | ||
|
|
||
| const { batch, message } = makeBatch(); | ||
| await handleSnapshotRestoreQueue(batch, makeEnv(makeStub())); | ||
|
|
||
| expect(flyMock.createVolume).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| expect.objectContaining({ snapshot_id: 'vs-1', size_gb: 11 }) | ||
| ); | ||
| expect(message.ack).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('keeps the current volume size when the snapshot source is smaller', async () => { | ||
| flyMock.getVolume.mockResolvedValue(volume({ size_gb: 20 })); | ||
| flyMock.listVolumeSnapshots.mockResolvedValue([snapshot({ volume_size: 11 * GIB })]); | ||
|
|
||
| const { batch } = makeBatch(); | ||
| await handleSnapshotRestoreQueue(batch, makeEnv(makeStub())); | ||
|
|
||
| expect(flyMock.createVolume).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| expect.objectContaining({ size_gb: 20 }) | ||
| ); | ||
| }); | ||
|
|
||
| it('falls back to the current volume size when the snapshot is not found', async () => { | ||
| const { batch } = makeBatch(); | ||
| await handleSnapshotRestoreQueue(batch, makeEnv(makeStub())); | ||
|
|
||
| expect(flyMock.createVolume).toHaveBeenCalledWith( | ||
| expect.anything(), | ||
| expect.objectContaining({ size_gb: 10 }) | ||
| ); | ||
| }); | ||
|
|
||
| it('swaps the volume and starts the machine on success', async () => { | ||
| flyMock.listVolumeSnapshots.mockResolvedValue([snapshot({ volume_size: 10 * GIB })]); | ||
| flyMock.createVolume.mockResolvedValue(volume({ id: 'vol-restored', region: 'ord' })); | ||
| const stub = makeStub(); | ||
|
|
||
| const { batch, message } = makeBatch(); | ||
| await handleSnapshotRestoreQueue(batch, makeEnv(stub)); | ||
|
|
||
| expect(stub.setPendingRestoreVolumeId).toHaveBeenCalledWith('vol-restored'); | ||
| expect(stub.completeSnapshotRestore).toHaveBeenCalledWith('vol-restored', 'ord'); | ||
| expect(stub.start).toHaveBeenCalledWith('user-1', { reason: 'snapshot_restore' }); | ||
| expect(message.ack).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('acks without restoring when the volume was already swapped', async () => { | ||
| const stub = makeStub(); | ||
| stub.getStatus.mockResolvedValue({ flyVolumeId: 'vol-other', flyAppName: 'acct-test' }); | ||
|
|
||
| const { batch, message } = makeBatch(); | ||
| await handleSnapshotRestoreQueue(batch, makeEnv(stub)); | ||
|
|
||
| expect(flyMock.createVolume).not.toHaveBeenCalled(); | ||
| expect(message.ack).toHaveBeenCalled(); | ||
| }); | ||
| }); |
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.
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.