fix: dispatch guard and Defect-safe rollback (#32)
A readable intent that fails in handling now always gets exactly one ack: the new static internal code, unless the handler's own ack was already enqueued. Both transaction helpers roll back on every unwind path, Defect included, so the shared SQLite connection never keeps an open transaction. Test-only dispfault build covers catchable/defect/after-ack; DESIGN 6.5 records internal as an owner decision and 6.2 names refusal codes as additive.
This commit is contained in:
+103
-1
@@ -11,9 +11,11 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import fs from 'node:fs'
|
||||
import { DatabaseSync } from 'node:sqlite'
|
||||
import path from 'node:path'
|
||||
import { runLeak, runChecker } from './leak.mjs'
|
||||
import { buildServer, gmKeyOf, makeStateDir, startServer, stopServer } from './withserver.mjs'
|
||||
import { buildServer, gmKeyOf, makeStateDir, signIn, startServer, stopServer } from './withserver.mjs'
|
||||
import { openWs } from './lib.mjs'
|
||||
import { projectRoot } from './lib.mjs'
|
||||
|
||||
const results = path.join(projectRoot, 'results')
|
||||
@@ -85,3 +87,103 @@ for (const [fault, expected] of Object.entries(FAULTS)) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// #32: an escaping failure in intent handling must still answer the request. The `dispfault` build
|
||||
// is the only build with an injection point; the shipped binary must remain incapable of faulting.
|
||||
test('the shipped build carries no dispatch fault hook', () => {
|
||||
const bin = buildServer()
|
||||
const bytes = fs.readFileSync(bin)
|
||||
assert.equal(bytes.includes(Buffer.from('DISP_FAULT')), false,
|
||||
`${bin} contains the dispatch fault hook: a production binary must be incapable of faulting`)
|
||||
})
|
||||
|
||||
async function gmSession(s) {
|
||||
const gmKey = gmKeyOf(s.stateDir)
|
||||
const { cookie } = await signIn(s.origin, gmKey)
|
||||
const ws = await openWs(s.origin, cookie)
|
||||
const hello = await ws.waitText('hello')
|
||||
await ws.waitText('walls')
|
||||
return { ws, sceneId: hello.scene.id }
|
||||
}
|
||||
|
||||
test('a CatchableError inside a locked mutation: one internal ack, then the server keeps serving',
|
||||
async () => {
|
||||
const stateDir = makeStateDir()
|
||||
const s = await startServer({ stateDir, defines: ['dispfault'], env: { DISP_FAULT: 'txn-catchable' } })
|
||||
try {
|
||||
const gm = await gmSession(s)
|
||||
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
|
||||
pos: [100, 100], descriptive_name: 'a', actual_name: 'A' })
|
||||
assert.equal(tok.ok, true, JSON.stringify(tok))
|
||||
const bad = await gm.ws.request({ t: 'token.move', id: tok.id, pos: [150, 150] })
|
||||
assert.equal(bad.ok, false)
|
||||
assert.equal(bad.code, 'internal', 'an escaping CatchableError is the internal code')
|
||||
assert.deepEqual(Object.keys(bad).sort(), ['code', 'n', 'ok', 'reqId', 't'])
|
||||
const acks = gm.ws.messages.filter(m => m.t === 'ack' && m.reqId === bad.reqId)
|
||||
assert.equal(acks.length, 1, 'exactly one ack for the failed intent')
|
||||
// Nothing from the failed intent is durable: the row still holds the create position.
|
||||
const db = new DatabaseSync(s.dbPath)
|
||||
assert.deepEqual({ ...db.prepare('SELECT x, y FROM entity WHERE id = ?').get(tok.id) },
|
||||
{ x: 100, y: 100 })
|
||||
db.close()
|
||||
// One log event names the intent and the error class, never the message or payload.
|
||||
const log = s.log()
|
||||
assert.match(log, /ERROR intent_failed intent=token\.move reqId=\d+ class=ValueError message=unhandled/)
|
||||
assert.equal(log.includes('injected fault'), false, 'the exception message is not logged')
|
||||
|
||||
// The transaction is closed: the next move on the same connection commits its own row.
|
||||
const good = await gm.ws.request({ t: 'token.move', id: tok.id, pos: [120, 120] })
|
||||
assert.equal(good.ok, true, JSON.stringify(good))
|
||||
// And a second, unaffected connection can mutate too.
|
||||
const gm2 = await gmSession(s)
|
||||
const good2 = await gm2.ws.request({ t: 'token.move', id: tok.id, pos: [130, 130] })
|
||||
assert.equal(good2.ok, true, JSON.stringify(good2))
|
||||
gm.ws.close(); gm2.ws.close()
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('a Defect inside a locked mutation: one internal ack and no open transaction', async () => {
|
||||
const stateDir = makeStateDir()
|
||||
const s = await startServer({ stateDir, defines: ['dispfault'], env: { DISP_FAULT: 'txn-defect' } })
|
||||
try {
|
||||
const gm = await gmSession(s)
|
||||
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
|
||||
pos: [100, 100], descriptive_name: 'a', actual_name: 'A' })
|
||||
assert.equal(tok.ok, true, JSON.stringify(tok))
|
||||
const bad = await gm.ws.request({ t: 'token.move', id: tok.id, pos: [150, 150] })
|
||||
assert.equal(bad.code, 'internal', 'a Defect is answered like any other escaping failure')
|
||||
assert.equal(gm.ws.messages.filter(m => m.t === 'ack' && m.reqId === bad.reqId).length, 1)
|
||||
assert.equal(gm.ws.closeInfo, null, 'the connection stays usable by default')
|
||||
const good = await gm.ws.request({ t: 'token.move', id: tok.id, pos: [120, 120] })
|
||||
assert.equal(good.ok, true, JSON.stringify(good))
|
||||
gm.ws.close()
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('a failure after the ack was enqueued never appends a second ack', async () => {
|
||||
const stateDir = makeStateDir()
|
||||
const s = await startServer({ stateDir, defines: ['dispfault'], env: { DISP_FAULT: 'after-ack' } })
|
||||
try {
|
||||
const gm = await gmSession(s)
|
||||
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
|
||||
pos: [100, 100], descriptive_name: 'a', actual_name: 'A' })
|
||||
assert.equal(tok.ok, true, JSON.stringify(tok))
|
||||
const ack = await gm.ws.request({ t: 'token.move', id: tok.id, pos: [150, 150] })
|
||||
assert.equal(ack.ok, true, 'the handler\'s own ack is the one the client sees')
|
||||
const all = gm.ws.messages.filter(m => m.t === 'ack' && m.reqId === ack.reqId)
|
||||
assert.equal(all.length, 1, 'the guard must not append an internal ack after a sent ack')
|
||||
assert.equal(all[0].code, undefined)
|
||||
const next = await gm.ws.request({ t: 'token.move', id: tok.id, pos: [120, 120] })
|
||||
assert.equal(next.ok, true, JSON.stringify(next))
|
||||
gm.ws.close()
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user