Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/workspace-service/models/artifacts-damaged-record.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { mkdir, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { afterEach, expect, it } from 'vitest'
import { tmpDataFolder } from '../../../test/helpers/tmp-data-folder.js'
import { fakeCoreLink } from '../../../test/helpers/fake-core-link.js'
import { defaultConfig } from '../../config/index.js'
import { WorkspaceService } from '../index.js'

const cleanup: Array<() => void> = []
afterEach(() => cleanup.splice(0).forEach((fn) => fn()))

// The workspace snapshot lists every media installation record; one that cannot be read must not make
// the snapshot (and with it api.start, api.restart and chat.start) fail.
it('keeps the workspace readable when one media installation record is damaged', async () => {
const tmp = tmpDataFolder()
cleanup.push(tmp.cleanup)
const directory = join(tmp.paths.atcDir, 'media-installations')
await mkdir(directory, { recursive: true })
await writeFile(
join(directory, 'kept.json'),
JSON.stringify({ id: 'kept', stage: 'ready', updated_at: 1, plan: { digest: 'plan-kept', files: [] } })
)
await writeFile(join(directory, 'damaged.json'), '{ not json')
const service = new WorkspaceService({ paths: tmp.paths, link: fakeCoreLink(), config: defaultConfig() })
expect(await service.execute('artifacts.list')).toMatchObject([{ id: 'kept' }])
expect((await service.snapshot()).setups).toMatchObject([{ id: 'kept' }])
})
3 changes: 2 additions & 1 deletion src/workspace-service/models/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ export class ArtifactService {
if (e.code === 'ENOENT') return []
throw e
})
// One unreadable record must not hide every other installation or fail the workspace snapshot that lists them.
const docs = await Promise.all(
names
.filter((n) => /^[\w-]+\.json$/.test(n))
.map((n) => readDocument<ArtifactJob | null>(join(this.directory, n), null))
.map((n) => readDocument<ArtifactJob | null>(join(this.directory, n), null).catch(() => null))
)
return docs
.filter((d): d is ArtifactJob => !!d?.plan)
Expand Down
Loading