Read the source and its declared rule
The original active table has idsA/B. Its replacement must contain exactly that id set with required positive quantities. A candidate with onlyA is incomplete. Corrected code loads shadow and changes the active registry in one transaction, checking actual missing/extra ids and write failures before commit.
| Record | Input JSON |
|---|---|
| Base input | {"candidate":[{"id":"A","quantity":2}]} |
Compare the actual paths
The incomplete candidate rolls back and originalA/B stay visible. If a candidate write violates CHECK, the failed load likewise leaves the original active. The checks cover this finite supplied contract.
| Path | Output JSON |
|---|---|
| Misleading path | {"error":null,"current":"shadow","active":[{"id":"A","quantity":2}],"shadow":[{"id":"A","quantity":2}]} |
| Corrected path | {"error":"incomplete-candidate","current":"original","active":[{"id":"A","quantity":1},{"id":"B","quantity":1}],"shadow":[]} |
import {DatabaseSync} from 'node:sqlite';
import {TextDecoder} from 'node:util';
import {inflateSync} from 'node:zlib';
import {posix} from 'node:path';
import {createHash} from 'node:crypto';
const rows=(db,sql)=>db.prepare(sql).all().map(r=>({...r}));
const failed=(action)=>{try{action();return null;}catch(e){return {code:e.errcode??e.code??e.name};}};
import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
const db=new DatabaseSync(':memory:');try{db.exec("CREATE TABLE original(id TEXT PRIMARY KEY,quantity INTEGER);INSERT INTO original VALUES('A',1),('B',1);CREATE TABLE shadow(id TEXT PRIMARY KEY,quantity INTEGER NOT NULL CHECK(quantity>0));CREATE TABLE registry(current TEXT);INSERT INTO registry VALUES('original');CREATE VIEW active AS SELECT * FROM original WHERE(SELECT current FROM registry)='original' UNION ALL SELECT * FROM shadow WHERE(SELECT current FROM registry)='shadow';");
if(corrected)db.exec('BEGIN;');let error=failed(()=>{for(const r of i.candidate)db.prepare('INSERT INTO shadow VALUES(?,?)').run(r.id,r.quantity);});
if(corrected&&!error){const missing=rows(db,'SELECT id FROM original EXCEPT SELECT id FROM shadow');const extra=rows(db,'SELECT id FROM shadow EXCEPT SELECT id FROM original');if(missing.length||extra.length)error='incomplete-candidate';}
if(!error||!corrected)db.exec("UPDATE registry SET current='shadow';");if(corrected)db.exec(error?'ROLLBACK;':'COMMIT;');
return {error,current:db.prepare('SELECT current FROM registry').get().current,active:rows(db,'SELECT * FROM active ORDER BY id'),shadow:rows(db,'SELECT * FROM shadow ORDER BY id')};
}finally{db.close();}
};
const cases=[{"label":"misleading base","input":{"candidate":[{"id":"A","quantity":2}]},"expected":{"error":null,"current":"shadow","active":[{"id":"A","quantity":2}],"shadow":[{"id":"A","quantity":2}]},"corrected":false},{"label":"corrected base","input":{"candidate":[{"id":"A","quantity":2}]},"expected":{"error":"incomplete-candidate","current":"original","active":[{"id":"A","quantity":1},{"id":"B","quantity":1}],"shadow":[]},"corrected":true},{"label":"complete candidate commits publication","input":{"candidate":[{"id":"A","quantity":2},{"id":"B","quantity":3}]},"expected":{"error":null,"current":"shadow","active":[{"id":"A","quantity":2},{"id":"B","quantity":3}],"shadow":[{"id":"A","quantity":2},{"id":"B","quantity":3}]},"corrected":true},{"label":"failed candidate write leaves original active","input":{"candidate":[{"id":"A","quantity":2},{"id":"B","quantity":-1}]},"expected":{"error":{"code":275},"current":"original","active":[{"id":"A","quantity":1},{"id":"B","quantity":1}],"shadow":[]},"corrected":true}];
for(const test of cases){
const actual=run(test.input,test.corrected);
assert.deepEqual(actual,test.expected,test.label);
console.log(test.label,actual);
}
Check boundaries before the next load
Complete A2/B3 commits the switch. Failed negativeB write rolls back shadow allocations and the switch.
| Case | Input JSON | Expected output JSON |
|---|---|---|
| Complete candidate commits switch | {"candidate":[{"id":"A","quantity":2},{"id":"B","quantity":3}]} | {"error":null,"current":"shadow","active":[{"id":"A","quantity":2},{"id":"B","quantity":3}],"shadow":[{"id":"A","quantity":2},{"id":"B","quantity":3}]} |
| Candidate write fails its constraint | {"candidate":[{"id":"A","quantity":2},{"id":"B","quantity":-1}]} | {"error":{"code":275},"current":"original","active":[{"id":"A","quantity":1},{"id":"B","quantity":1}],"shadow":[]} |
Should the base candidateA alone become active?
Show the worked answer
No. Hold the incomplete candidate and retain originalA/B. Publish only the complete verified transaction.