gitoria
All repositories: gitoria
5.7 KB
#!/usr/bin/env node// Usage: node check-index-keys.mjs <directory>//// READ ONLY. For every <name>.mpack in the directory (not below it) this lists the live// records whose indexed text field holds a comma, a newline, a carriage return or starts with// a double quote, and says which of them are MISSING from that field's index file// <name>.<field>.txt (mission 486: the engine before the fix dropped such entries from the// index when it was reopened). The indexed fields are the <name>.<field>.txt files beside// the .mpack; nothing is written, locked or created. Exit status 1 when any entry is missing.//// A missing entry is repaired by opening the table with the fixed hl:mpackdb: an index whose// idxstate carries no "idxFmt":2 mark is rebuilt from the data once, the data itself holds// every record.import { readFileSync, readdirSync, existsSync } from 'node:fs'import { join } from 'node:path'const dir = process.argv[2]if (!dir) {console.error('usage: node check-index-keys.mjs <directory>')process.exit(2)}// --- a small MessagePack reader (the subset the engine writes: maps, arrays, strings, numbers)function decode(buf, start) {let p = startconst val = () => {const b = buf[p++]if (b <= 0x7f) return bif (b >= 0xe0) return b - 256if ((b & 0xe0) === 0xa0) return str(b & 0x1f)if ((b & 0xf0) === 0x80) return map(b & 0x0f)if ((b & 0xf0) === 0x90) return arr(b & 0x0f)switch (b) {case 0xc0: return nullcase 0xc2: return falsecase 0xc3: return truecase 0xc4: return bin(buf[p++])case 0xc5: { const n = buf.readUInt16BE(p); p += 2; return bin(n) }case 0xc6: { const n = buf.readUInt32BE(p); p += 4; return bin(n) }case 0xca: { const v = buf.readFloatBE(p); p += 4; return v }case 0xcb: { const v = buf.readDoubleBE(p); p += 8; return v }case 0xcc: return buf[p++]case 0xcd: { const v = buf.readUInt16BE(p); p += 2; return v }case 0xce: { const v = buf.readUInt32BE(p); p += 4; return v }case 0xcf: { const v = Number(buf.readBigUInt64BE(p)); p += 8; return v }case 0xd0: return buf.readInt8(p++)case 0xd1: { const v = buf.readInt16BE(p); p += 2; return v }case 0xd2: { const v = buf.readInt32BE(p); p += 4; return v }case 0xd3: { const v = Number(buf.readBigInt64BE(p)); p += 8; return v }case 0xd9: return str(buf[p++])case 0xda: { const n = buf.readUInt16BE(p); p += 2; return str(n) }case 0xdb: { const n = buf.readUInt32BE(p); p += 4; return str(n) }case 0xdc: { const n = buf.readUInt16BE(p); p += 2; return arr(n) }case 0xdd: { const n = buf.readUInt32BE(p); p += 4; return arr(n) }case 0xde: { const n = buf.readUInt16BE(p); p += 2; return map(n) }case 0xdf: { const n = buf.readUInt32BE(p); p += 4; return map(n) }default: throw new Error('unsupported msgpack byte 0x' + b.toString(16))}}const str = (n) => { const s = buf.toString('utf8', p, p + n); p += n; return s }const bin = (n) => { p += n; return null }const arr = (n) => { const a = []; for (let i = 0; i < n; i++) a.push(val()); return a }const map = (n) => { const o = {}; for (let i = 0; i < n; i++) { const k = val(); o[k] = val() } return o }return val()}// --- an index file: the offsets it names (a quoted key is a JSON string, else split from the right)function indexOffsets(text) {const offsets = new Set()for (const line of text.split('\n')) {if (!line) continueif (line[0] === '"') {let rest = nullfor (let i = 1; i < line.length; i++) {if (line[i] === '\\') { i++; continue }if (line[i] === '"') { rest = line.slice(i + 1); break }}if (rest !== null && /^,\d+,\d+$/.test(rest)) { offsets.add(Number(rest.split(',')[1])); continue }}const c2 = line.lastIndexOf(',')const c1 = c2 > 0 ? line.lastIndexOf(',', c2 - 1) : -1if (c1 < 0) continueconst off = Number(line.slice(c1 + 1, c2))if (Number.isInteger(off)) offsets.add(off)}return offsets}const risky = (v) => typeof v === 'string' && (/[,\n\r]/.test(v) || v[0] === '"')let missingTotal = 0const all = readdirSync(dir)const names = all.filter((f) => f.endsWith('.mpack')).map((f) => f.slice(0, -'.mpack'.length)).sort()for (const name of names) {const data = readFileSync(join(dir, name + '.mpack'))let deleted = new Set()const metaPath = join(dir, name + '.meta.json')if (existsSync(metaPath)) {try { deleted = new Set(JSON.parse(readFileSync(metaPath, 'utf8')).deleted || []) } catch { /* no usable meta */ }}const fields = all.filter((f) => f.startsWith(name + '.') && f.endsWith('.txt')).map((f) => f.slice(name.length + 1, -4)).filter((f) => f && !f.includes('.'))const indexes = new Map(fields.map((f) => [f, indexOffsets(readFileSync(join(dir, name + '.' + f + '.txt'), 'utf8'))]))let live = 0, riskyCount = 0, missing = 0for (let pos = 0; pos + 4 <= data.length;) {const size = data.readUInt32LE(pos)if (size < 5 || pos + size > data.length) { console.log(`${name}.mpack: record frame at ${pos} is damaged or cut short; stopped there`); break }if (!deleted.has(pos)) {live++let rec = nulltry { rec = decode(data, pos + 4) } catch (e) { console.log(`${name}.mpack: record at ${pos} cannot be read: ${e.message}`) }if (rec && typeof rec === 'object') {for (const [field, offsets] of indexes) {if (!risky(rec[field])) continueriskyCount++const lost = !offsets.has(pos)if (lost) { missing++; missingTotal++ }console.log(`${name}.${field}: record at offset ${pos} key ${JSON.stringify(rec[field])} ${lost ? 'MISSING from the index file' : 'present'}`)}}}pos += size}console.log(`${name}: ${live} live records, indexed fields [${fields.join(', ')}], ${riskyCount} with a comma/newline/CR/leading quote, ${missing} missing from their index`)}if (!names.length) console.log('no .mpack files in ' + dir)process.exit(missingTotal ? 1 : 0)
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 6e03be0apull state: keep the resync marker until the sync ran (#56)mre
- 93bdd26cpull state: wait for a claimed row, resync when it stays busy (#56)mre
- e53d284bpull state: stop on a failed branch scan, claim the row on writes (#56)mre
- d8608224close and reopen: keep a manual close against sync (#56)mre
- ffb60746|||PR close and reopen pull requests (#56)mre
- fe209ffeMerge branch 'w56-owner-members' into mainGitoria
- fa0754c8|||PR repository owner transfer and members API (#55)mre
- 8862e537Merge branch 'w55-rename-org' into mainGitoria
- 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
- 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
- 499f25f2Review fixes for the Settings organization move (#53)mre
- ed3b922b|||PR move a repository into an organization in Settings (#53)mre
- 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
- 9f7ce8d8Preserve rename compatibility and live page titlesmre
- c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
- ce08920dMerge branch 'w52c-header-phone' into mainGitoria
- 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
- e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
- c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
- 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria