DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(turnrewind): claim rewind notices inside one BEGIN IMMEDIATE transaction
yingtianlan2275e78d8d1a
diff --git a/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.ts b/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.tsindex 8fb2efd3..726afa28 100644--- a/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.ts+++ b/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.ts@@ -266,35 +266,40 @@ export interface ClaimedNotice extends NoticeRow {}export function claimRewindNotices(db: Ledger, sessionId: string, workspaceKey: string): ClaimedNotice[] {- const notices = db.prepare(`- SELECT * FROM rewind_notices- WHERE session_id = ? AND workspace_key = ? AND status = 'pending'- ORDER BY created_at ASC- `).all(sessionId, workspaceKey) as unknown as NoticeRow[]- if (notices.length === 0)- return []const claimedAt = new Date().toISOString()- const statement = db.prepare(`- UPDATE rewind_notices SET status = 'consumed', claimed_at = ?- WHERE notice_id = ? AND status = 'pending'- `)+ // BEGIN IMMEDIATE + 事务内 SELECT:读取与消费在同一个写锁内完成,两个+ // 并发 claim(双 Host 进程)不可能读到同一批 pending 行——事务外的+ // 预读会让第二个调用者返回 0 行更新却仍吐出整批 notice。db.exec('BEGIN IMMEDIATE')try {+ const notices = db.prepare(`+ SELECT * FROM rewind_notices+ WHERE session_id = ? AND workspace_key = ? AND status = 'pending'+ ORDER BY created_at ASC+ `).all(sessionId, workspaceKey) as unknown as NoticeRow[]+ if (notices.length === 0) {+ db.exec('COMMIT')+ return []+ }+ const statement = db.prepare(`+ UPDATE rewind_notices SET status = 'consumed', claimed_at = ?+ WHERE notice_id = ? AND status = 'pending'+ `)for (const notice of notices)statement.run(claimedAt, notice.notice_id)db.exec('COMMIT')+ // Rows pass through verbatim (snake_case): the JS tests and the host's+ // message builder both consume raw column names.+ return notices.map(notice => ({+ ...notice,+ paths: JSON.parse(notice.paths_json),+ turns: JSON.parse(notice.turns_json || '[]'),+ }))}catch (error) {db.exec('ROLLBACK')throw error}- // Rows pass through verbatim (snake_case): the JS tests and the host's- // message builder both consume raw column names.- return notices.map(notize => ({- ...notize,- paths: JSON.parse(notize.paths_json),- turns: JSON.parse(notize.turns_json || '[]'),- }))}export function listNeedsRecoveryWorkspaces(db: Ledger): string[] {diff --git a/packages/dsh-tauri-turnrewind-ts/test/ledger.test.js b/packages/dsh-tauri-turnrewind-ts/test/ledger.test.jsindex c0c9122b..00d0073e 100644--- a/packages/dsh-tauri-turnrewind-ts/test/ledger.test.js+++ b/packages/dsh-tauri-turnrewind-ts/test/ledger.test.js@@ -105,6 +105,38 @@ it('merges multiple pending rewind notices and delivers them once', async () =>}})+it('never double-claims notices across concurrent ledger connections', async () => {+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-notice-race-'))+ // Two connections stand in for two host processes sharing one ledger file:+ // the claim is a BEGIN IMMEDIATE transaction, so the reader cannot observe+ // a pending batch that another claimer is about to consume.+ const first = openLedger(root)+ const second = openLedger(root)+ try {+ queueRewindNotice(first, {+ noticeId: 'notice-race',+ sessionId: 'session',+ workspaceKey: 'workspace',+ targetTurnId: 'session:1',+ paths: ['src/only.ts'],+ createdAt: '2026-01-01T00:00:00.000Z',+ })++ const claimedByFirst = claimRewindNotices(first, 'session', 'workspace')+ assert.equal(claimedByFirst.length, 1)+ assert.equal(claimedByFirst[0].notice_id, 'notice-race')+ // The second connection must see the consumed state, not a stale+ // pre-transaction snapshot of the same pending row.+ assert.deepEqual(claimRewindNotices(second, 'session', 'workspace'), [])+ assert.deepEqual(claimRewindNotices(second, 'session', 'workspace'), [])+ }+ finally {+ first.close()+ second.close()+ await rm(root, { recursive: true, force: true })+ }+})+it('records a skipped turn with a single unsupported-workspace heads-up', async () => {const root = await mkdtemp(join(tmpdir(), 'turnrewind-skip-test-'))const db = openLedger(root)