From 574315f5b57c95a2b29c6e9a051e88c25c64ede0 Mon Sep 17 00:00:00 2001 From: QQ Date: Thu, 12 Nov 2026 00:03:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20verify-concurrency.mjs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- verify-concurrency.mjs | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 verify-concurrency.mjs diff --git a/verify-concurrency.mjs b/verify-concurrency.mjs new file mode 100644 index 0000000..4d93c5f --- /dev/null +++ b/verify-concurrency.mjs @@ -0,0 +1,70 @@ +// P5 concurrency / mutex verification. +// +// Every mutating skill path must refuse to run while a sync pass is running: +// install staging, install commit, toggle, remove, update apply and rollback. +// The refusal is a typed 409 at the route layer, never a silent partial write. +import http from 'node:http' +import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +process.env.DSH_HOME = mkdtempSync(join(tmpdir(), 'brain-conc-')) + +const results = [] +function check(name, ok, detail) { + results.push({ name, ok }) + console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail === undefined ? '' : ` :: ${detail}`}`) +} + +const { SkillAdmin } = await import('./lib/skill-admin.js') +const { SkillUpdater } = await import('./lib/skill-update.js') +const { SettingsStore } = await import('./lib/store.js') +const { Reporter } = await import('./lib/reporter.js') +const { createRoutes } = await import('./lib/routes.js') + +const root = join(process.env.DSH_HOME, 'skills') +const holding = join(process.env.DSH_HOME, 'skills-disabled') +mkdirSync(join(root, 'alpha'), { recursive: true }) +writeFileSync(join(root, 'alpha', 'SKILL.md'), '# alpha\n') + +let running = true +const admin = new SkillAdmin({ skillsRoot: () => root, holdingDir: () => holding, syncStatePath: () => join(process.env.DSH_HOME, 'dsh-brain-skills.json'), syncRunning: () => running, log: () => {} }) +const updater = new SkillUpdater({ admin, skillsRoot: () => root, holdingDir: () => holding, syncRunning: () => running, log: () => {} }) + +async function expectCode(fn, code) { + try { await fn(); return false } catch (error) { return error.code === code || error.message === code } +} +check('prepareInstall refuses while sync runs', await expectCode(() => admin.prepareInstall('https://example.org/x.git'), 'sync_running')) +check('commitInstall refuses while sync runs', await expectCode(() => admin.commitInstall({ planId: 'x', folders: [] }), 'sync_running')) +check('setEnabled refuses while sync runs', await expectCode(() => admin.setEnabled('alpha', false), 'sync_running')) +check('remove refuses while sync runs', await expectCode(() => admin.remove('alpha'), 'sync_running')) +check('applyUpdate refuses while sync runs', await expectCode(() => updater.applyUpdate('alpha', 'deadbeef'), 'sync_running')) +check('rollback refuses while sync runs', await expectCode(() => updater.rollback('alpha'), 'sync_running')) + +// Route layer maps the refusal to 409. +const store = new SettingsStore({ clientId: 't' }, join(process.env.DSH_HOME, 'c.json')) +const reporter = new Reporter({ client: () => ({ reportTurn: async () => ({ ok: true, status: 200, data: {} }), submitSummary: async () => ({ ok: true, status: 200, data: {} }) }), settings: () => ({ enabled: true, clientId: 't' }), activityToken: () => '', summary: () => ({ enabled: false, maxChars: 1 }), summaryToken: () => '', log: () => {} }) +const routes = createRoutes({ version: '0.6.0', store, reporter, client: () => ({ reach: async () => ({ ok: false, status: 0, message: 'x' }), listSkills: async () => ({ ok: true, status: 200, data: { items: [] } }) }), admin, updates: updater }) +const server = http.createServer((req, res) => { + const path = (req.url ?? '/').split('?')[0] + for (const route of routes) { + if (route.kind === 'exact' && path === route.path) return route.handler(req, res) + if (route.kind === 'prefix' && (path === route.path || path.startsWith(`${route.path}/`))) return route.handler(req, res) + } + res.writeHead(404); res.end('{"ok":false}') +}) +await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) +const origin = `http://127.0.0.1:${server.address().port}` +const response = await fetch(`${origin}/dsh-brain/skills/toggle`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ folder: 'alpha', enabled: false }) }) +const body = await response.json() +check('route answers 409 sync_running during a sync', response.status === 409 && body.code === 'sync_running', JSON.stringify(body)) +await new Promise((resolve) => server.close(resolve)) + +// After the sync ends, the same toggle succeeds. +running = false +const enabled = await admin.setEnabled('alpha', false) +check('after the sync ends the mutation is allowed', enabled.folder === 'alpha' && enabled.enabled === false) + +const passed = results.filter((r) => r.ok).length +console.log(`\n${passed}/${results.length} passed`) +process.exit(passed === results.length ? 0 : 1)