skillhub/cli/test/unit/integrations/operation-store.test.ts
XiaoSeS 6c276d38fc
Some checks are pending
Security / Dependency Review (push) Waiting to run
Security / CodeQL (java-kotlin) (push) Waiting to run
Security / CodeQL (javascript-typescript) (push) Waiting to run
Security / CodeQL (python) (push) Waiting to run
fix(dsh): preserve legacy compatibility and Windows lifecycle safety (#1033)
保持共享库存 v2 与旧版可读普通日志,从真实入口派生启停;隔离扩展日志并保护恢复时的合法后续写入。修复 Windows 库存/日志原子替换及目标锁的短时共享冲突,不删除原数据、不抢占活锁、不重放事务。

补充安全探针诊断与仅公共下载内容的 CI 缓存,Profile 和安装状态仍逐次隔离。准备 CLI 0.3.0 / DSH 0.6.0,npm 发布另行执行。

最终提交的 CLI 三平台、DSH 14 项矩阵及 12 份启停场景摘要均通过。Windows 新 RC 依赖准备仍接近限额,作为 CI 准备稳定性风险保留。

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-10-29 21:08:14 +08:00

217 lines
16 KiB
TypeScript

import { renameAtomicFile } from '../../../src/platform/atomic-rename'
import { expect, test } from 'bun:test'
import { randomUUID } from 'node:crypto'
import { mkdir, mkdtemp, readdir, readFile, rename, rm, symlink, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { OperationStore, operationRequestDigest } from '../../../src/integrations/operation-store'
import type { AcceptedOperationInput, OperationRequest } from '../../../src/integrations/operation-store'
const accepted: AcceptedOperationInput = { kind: 'install', namespace: 'team', slug: 'writer', version: '1.0.0', targetLabel: '当前项目',
context: { hostLabel: '测试 Host', sourceIdentity: 'https://registry.example', sourceLabel: 'SkillHub', sourceRevision: randomUUID(), initiatedFrom: 'conversation' } }
function request(store: OperationStore): OperationRequest {
return { hostInstanceId: store.hostInstanceId, ownerId: 'session-1', requestKey: randomUUID(), mode: 'new',
requestDigest: operationRequestDigest({ detailToken: 'original-private-reference', scope: 'project' }) }
}
async function fixture(run: (store: OperationStore, root: string) => Promise<void>) {
const root = await mkdtemp(join(tmpdir(), 'skillhub-operations-'))
try { await run(await OperationStore.open(root), root) } finally { await rm(root, { recursive: true, force: true }) }
}
test('restarts preserve host identity and accepted requests before any stale-token validation', () => fixture(async (store, root) => {
const key = request(store)
const first = (await store.accept(key, async () => accepted))!
const reopened = await OperationStore.open(root)
expect(reopened.hostInstanceId).toBe(store.hostInstanceId)
const restored = await reopened.accept({ ...key, mode: 'retry' }, async () => { throw new Error('expired token must not be validated') })
expect(restored).toEqual(first)
expect(await readFile(join(root, `${first.operationId}.json`), 'utf8')).not.toContain('original-private-reference')
expect(await reopened.get(first.operationId, 'other-session')).toBeUndefined()
expect((await readdir(root)).sort()).toEqual([`${first.operationId}.json`, 'host.json'].sort())
}))
test('conflicting request arguments are rejected and missing retries never validate or create', () => fixture(async (store) => {
const key = request(store)
await store.accept(key, async () => accepted)
await expect(store.accept({ ...key, requestDigest: operationRequestDigest({ scope: 'user' }) }, async () => accepted))
.rejects.toMatchObject({ code: 'REQUEST_KEY_CONFLICT' })
expect(await store.accept({ ...key, requestKey: randomUUID(), mode: 'retry' }, async () => { throw new Error('must not run') })).toBeUndefined()
await expect(store.accept({ ...key, hostInstanceId: randomUUID() }, async () => accepted)).rejects.toMatchObject({ code: 'HOST_MISMATCH' })
}))
test('independent store instances deduplicate simultaneous accepts using file locks', () => fixture(async (store, root) => {
const key = request(store)
const stores = await Promise.all(Array.from({ length: 5 }, () => OperationStore.open(root)))
const records = await Promise.all(stores.map(instance => instance.accept(key, async () => accepted)))
expect(new Set(records.map(value => value!.operationId)).size).toBe(1)
expect((await readdir(root)).filter(name => name.endsWith('.json'))).toHaveLength(2)
}))
test('status updates require the current revision, preserve context, and cannot replay a terminal operation', () => fixture(async store => {
const key = request(store)
const initial = (await store.accept(key, async () => accepted))!
const staged = await store.update(initial.operationId, key.ownerId, 0, { ...initial.operation.status, phase: 'staging' })
await expect(store.update(initial.operationId, key.ownerId, 0, staged.operation.status)).rejects.toMatchObject({ code: 'REVISION_CONFLICT' })
await expect(store.update(initial.operationId, key.ownerId, 1, { ...staged.operation.status, phase: 'completed', files: 'installed' }))
.rejects.toMatchObject({ code: 'INVALID_TRANSITION' })
const checking = await store.update(initial.operationId, key.ownerId, 1, { ...staged.operation.status, phase: 'revalidating' })
const committing = await store.update(initial.operationId, key.ownerId, 2, { ...checking.operation.status, phase: 'committing' })
const done = await store.update(initial.operationId, key.ownerId, 3, { ...committing.operation.status, phase: 'completed', files: 'installed' })
expect(done.operation.context.createdAt).toBe(initial.operation.context.createdAt)
expect(done.operation.context.sourceRevision).toBe(accepted.context.sourceRevision)
await expect(store.update(initial.operationId, key.ownerId, 4, { ...done.operation.status, phase: 'staging' })).rejects.toMatchObject({ code: 'INVALID_TRANSITION' })
expect((await store.update(initial.operationId, key.ownerId, 4, { ...done.operation.status, discovery: 'confirmed' })).operation.status.discovery).toBe('confirmed')
}))
test('invalid durable records block new acceptance rather than losing duplicate protection', () => fixture(async (store, root) => {
const key = request(store)
const first = (await store.accept(key, async () => accepted))!
await writeFile(join(root, `${first.operationId}.json`), '{}')
await expect(store.accept(request(store), async () => accepted)).rejects.toMatchObject({ code: 'CORRUPT_RECORD' })
}))
test('request digests ignore key ordering but preserve explicit parameters', () => {
expect(operationRequestDigest({ a: 'one', b: 2 })).toBe(operationRequestDigest({ b: 2, a: 'one' }))
expect(operationRequestDigest({ a: 'one', b: 2 })).not.toBe(operationRequestDigest({ a: 'one', b: 3 }))
expect(() => operationRequestDigest({ a: Number.NaN })).toThrow()
})
test('startup scan blocks unresolved target intents, permits unrelated paths and clears an explicitly restored status', () => fixture(async (store, root) => {
const target = join(root, 'skills/writer')
const pending = (await store.accept(request(store), async () => ({ ...accepted, targetPaths: [target] })))!
await store.update(pending.operationId, pending.ownerId, 0, { ...pending.operation.status, phase: 'staging' })
await store.recordTransaction(pending.operationId, pending.ownerId, 1, { type: 'beforeMove', move: {
phase: 'install', index: 0, agent: 'dsh', canonicalSkillDir: target,
from: join(root, '.stage'), to: target, stagingDir: join(root, '.stage'), backupDir: null
} })
const reopened = await OperationStore.open(root)
expect((await reopened.listForRecovery()).map(value => value.operationId)).toContain(pending.operationId)
const next = (await reopened.accept(request(reopened), async () => ({ ...accepted, targetPaths: [target] })))!
await reopened.update(next.operationId, next.ownerId, 0, { ...next.operation.status, phase: 'staging' })
const current = { operationId: next.operationId, ownerId: next.ownerId }
await expect(reopened.assertTargetsAvailable([target], current)).rejects.toMatchObject({ code: 'TARGET_RECOVERY_REQUIRED' })
await expect(reopened.assertTargetsAvailable([join(root, 'forged')], current)).rejects.toMatchObject({ code: 'INVALID_TRANSITION' })
const otherTarget = join(root, 'other/writer')
const other = (await reopened.accept(request(reopened), async () => ({ ...accepted, targetPaths: [otherTarget] })))!
await reopened.update(other.operationId, other.ownerId, 0, { ...other.operation.status, phase: 'staging' })
await reopened.assertTargetsAvailable([otherTarget], { operationId: other.operationId, ownerId: other.ownerId })
const old = (await reopened.get(pending.operationId, pending.ownerId))!
await reopened.update(old.operationId, old.ownerId, old.revision, { ...old.operation.status, phase: 'failed', files: 'failed', recovery: 'restored' })
await reopened.assertTargetsAvailable([target], current)
}))
test('pending journals block parent and child targets including aliases with missing descendants', () => fixture(async (store, root) => {
const target = join(root, 'skills/writer')
const pending = (await store.accept(request(store), async () => ({ ...accepted, targetPaths: [target] })))!
await store.update(pending.operationId, pending.ownerId, 0, { ...pending.operation.status, phase: 'unknown', recovery: 'unknown' })
await mkdir(join(root, 'skills'))
const candidates = [join(root, 'skills'), join(target, 'nested/child')]
if (process.platform !== 'win32') {
await symlink(join(root, 'skills'), join(root, 'alias'))
candidates.push(join(root, 'alias/writer/nested/child'))
}
for (const path of candidates) {
const current = (await store.accept(request(store), async () => ({ ...accepted, targetPaths: [path] })))!
await store.update(current.operationId, current.ownerId, 0, { ...current.operation.status, phase: 'staging' })
await expect(store.assertTargetsAvailable([path], current)).rejects.toMatchObject({ code: 'TARGET_RECOVERY_REQUIRED' })
}
const sibling = join(root, 'skills/writer-other')
const current = (await store.accept(request(store), async () => ({ ...accepted, targetPaths: [sibling] })))!
await store.update(current.operationId, current.ownerId, 0, { ...current.operation.status, phase: 'staging' })
await store.assertTargetsAvailable([sibling], current)
expect((await store.get(pending.operationId, pending.ownerId))!.operation.status.phase).toBe('unknown')
}))
test('ordinary journals remain v2 while activation journals are isolated and replayable', () => fixture(async (store, root) => {
const ordinary = (await store.accept(request(store), async () => accepted))!
const key = request(store)
const activation = (await store.accept(key, async () => ({ ...accepted, kind: 'disable' })))!
expect(JSON.parse(await readFile(join(root, `${ordinary.operationId}.json`), 'utf8')).schemaVersion).toBe(2)
expect(JSON.parse(await readFile(join(root, 'activation', `${activation.operationId}.json`), 'utf8')).schemaVersion).toBe(3)
expect((await readdir(root)).filter(name => name.endsWith('.json')).sort()).toEqual([`${ordinary.operationId}.json`, 'host.json'].sort())
const reopened = await OperationStore.open(root)
expect((await reopened.accept({ ...key, mode: 'retry' }, async () => { throw new Error('No replay mutation') }))?.operationId).toBe(activation.operationId)
expect((await reopened.listForRecovery()).map(record => record.operationId)).toContain(activation.operationId)
expect((await reopened.accept(request(reopened), async () => accepted))?.schemaVersion).toBe(2)
}))
test('existing root v3 journal is moved byte-for-byte outside legacy scan', () => fixture(async (store, root) => {
const ordinary = (await store.accept(request(store), async () => accepted))!
const bytes = JSON.stringify({ ...ordinary, schemaVersion: 3 }, null, 3)
await writeFile(join(root, `${ordinary.operationId}.json`), bytes)
const reopened = await OperationStore.open(root)
expect(await readFile(join(root, 'activation', `${ordinary.operationId}.json`), 'utf8')).toBe(bytes)
expect(await readdir(root)).not.toContain(`${ordinary.operationId}.json`)
expect((await reopened.get(ordinary.operationId, ordinary.ownerId))?.schemaVersion).toBe(3)
}))
test.each(['identical', 'changed'] as const)('interrupted journal isolation handles %s duplicate without hiding evidence', mode => fixture(async (store, root) => {
const ordinary = (await store.accept(request(store), async () => accepted))!
const path = join(root, `${ordinary.operationId}.json`)
await mkdir(join(root, 'activation'))
await writeFile(join(root, 'activation', `${ordinary.operationId}.json`), await readFile(path))
if (mode === 'changed') {
await writeFile(path, JSON.stringify({ ...ordinary, revision: 1 }))
await expect(OperationStore.open(root)).rejects.toMatchObject({ code: 'CORRUPT_RECORD' })
expect(JSON.parse(await readFile(path, 'utf8')).revision).toBe(1)
expect(JSON.parse(await readFile(join(root, 'activation', `${ordinary.operationId}.json`), 'utf8')).revision).toBe(0)
} else {
const reopened = await OperationStore.open(root)
expect(await readdir(root)).not.toContain(`${ordinary.operationId}.json`)
await reopened.update(ordinary.operationId, ordinary.ownerId, 0, { ...ordinary.operation.status, phase: 'staging' })
expect((await reopened.get(ordinary.operationId, ordinary.ownerId))?.revision).toBe(1)
expect(await readdir(root)).not.toContain(`${ordinary.operationId}.json`)
}
}))
test.each(['EPERM', 'EBUSY'])('journal replacement retries %s under the store lock without replaying validation or revision', async code => {
const root = await mkdtemp(join(tmpdir(), 'skillhub-journal-retry-'))
let armed = false; let attempts = 0; let validations = 0
const reachedRename = Promise.withResolvers<void>()
let firstAttempt: { source: string; target: string; payload: string } | undefined
let previous: string | undefined
let competing: Promise<PromiseSettledResult<unknown>[]> | undefined
try {
const store = await OperationStore.open(root, (from, to) => renameAtomicFile(from, to, async (source, target) => {
if (armed) {
const observation = { source: String(source), target: String(target), payload: (await readFile(source)).toString('hex') }
if (!firstAttempt) firstAttempt = observation
else expect(observation).toEqual(firstAttempt)
expect((await readFile(target)).toString('hex')).toEqual(previous!)
if (++attempts < 3) { reachedRename.resolve(); throw Object.assign(new Error('Sharing failure'), { code }) }
}
await rename(source, target)
}, 'win32'))
const key = request(store)
const initial = (await store.accept(key, async () => { validations++; return accepted }))!
const other = await OperationStore.open(root)
previous = (await readFile(join(root, initial.operationId + '.json'))).toString('hex')
armed = true
const updating = store.update(initial.operationId, key.ownerId, 0, { ...initial.operation.status, phase: 'staging' })
await reachedRename.promise
competing = Promise.allSettled([other.update(initial.operationId, key.ownerId, 0, { ...initial.operation.status, phase: 'staging' })])
const staged = await updating
expect(staged.revision).toBe(1)
expect((await competing)[0]!.status).toBe('rejected')
expect(attempts).toBe(3)
expect(validations).toBe(1)
expect((await store.get(initial.operationId, key.ownerId))!.revision).toBe(1)
expect((await store.accept({ ...key, mode: 'retry' }, async () => { throw new Error('must not replay') }))!.operationId).toBe(initial.operationId)
} finally { await competing; await rm(root, { recursive: true, force: true }) }
})
test('exhausted journal replacement preserves original bytes and releases the store lock', async () => {
const root = await mkdtemp(join(tmpdir(), 'skillhub-journal-exhaust-'))
let armed = false; let attempts = 0
const failure = Object.assign(new Error('Sharing failure'), { code: 'EPERM' })
try {
const store = await OperationStore.open(root, (from, to) => renameAtomicFile(from, to, async (source, target) => {
if (armed) { attempts++; throw failure }
await rename(source, target)
}, 'win32'))
const key = request(store); const initial = (await store.accept(key, async () => accepted))!
const path = join(root, initial.operationId + '.json'); const before = await readFile(path)
armed = true
await expect(store.update(initial.operationId, key.ownerId, 0, { ...initial.operation.status, phase: 'staging' })).rejects.toBe(failure)
expect(attempts).toBe(6)
expect(await readFile(path)).toEqual(before)
expect((await readdir(root)).filter(name => name.endsWith('.tmp'))).toEqual([])
armed = false
expect((await store.update(initial.operationId, key.ownerId, 0, { ...initial.operation.status, phase: 'staging' })).revision).toBe(1)
} finally { await rm(root, { recursive: true, force: true }) }
})