// 零副作用复现:count 未达到可读总量时,返回条数是否仍会少于 count(边界丢条)。 // // 起因:history-count-scan 观测到群侧 count=120 只返回 119 条(seq 3079..3197), // 而 count=150 返回 122 条(seq 3076..3197,编号连续无空洞)。两次都远低于可读上限, // 说明「返回条数 == min(count, 可读量)」并非严格成立:count=120 时编号 3078 明明可得却未返回。 // // 这条为什么重要:如果 QLinker 用「本次返回条数 == 请求 count」来判断「已拉全 / 无遗漏」, // 该判据在本版本不成立,会把漏拉当成拉全,属静默数据丢失。补拉对账必须以 seq 连续性 + // 与事件流交叉比对为准,不能以条数为准。 // // 只读。用法: NAPCAT_WS_TOKEN=xxx node tools/probe/history-edge-offbyone.mjs [--rounds=3] const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099'; const TOKEN = process.env.NAPCAT_WS_TOKEN; const GROUP = Number(process.env.PROBE_GROUP || 933280401); const ROUNDS = Number((process.argv.find((a) => a.startsWith('--rounds=')) || '--rounds=3').split('=')[1]); if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); } const fs = await import('node:fs'); const path = await import('node:path'); const OUT_DIR = process.env.PROBE_OUT || 'local'; fs.mkdirSync(OUT_DIR, { recursive: true }); const OUT = path.join(OUT_DIR, `history-edge-${new Date().toISOString().replace(/[:.]/g, '-')}.jsonl`); const rec = (o) => { fs.appendFileSync(OUT, JSON.stringify(o) + '\n'); return o; }; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`); const pending = new Map(); let authed = false; ws.onmessage = (ev) => { let o; try { o = JSON.parse(String(ev.data)); } catch { return; } if (o.echo && pending.has(o.echo)) { const p = pending.get(o.echo); pending.delete(o.echo); clearTimeout(p.to); p.resolve(o); return; } if (o.post_type === 'meta_event' && o.meta_event_type === 'lifecycle') authed = true; }; for (let i = 0; i < 80 && !authed; i++) await sleep(100); if (!authed) { console.error('未收到 lifecycle,认证失败(GC-008)'); process.exit(2); } const api = (action, params, ms = 25000) => new Promise((resolve) => { const echo = `${action}-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`; const to = setTimeout(() => { pending.delete(echo); resolve({ __timeout: true }); }, ms); pending.set(echo, { resolve: (o) => { clearTimeout(to); resolve(o); }, to }); ws.send(JSON.stringify({ action, params, echo })); }); const full = (await api('get_group_msg_history', { group_id: GROUP, count: 500 }))?.data?.messages ?? []; const allSeqs = full.map((x) => Number(x.real_seq)).sort((a, b) => a - b); const maxSeq = allSeqs[allSeqs.length - 1]; const readable = allSeqs.length; console.log(`可读窗口 ${readable} 条,seq ${allSeqs[0]}..${maxSeq},编号连续=${allSeqs.length === maxSeq - allSeqs[0] + 1}`); const allSet = new Set(allSeqs); rec({ kind: 'reference', readable, from: allSeqs[0], to: maxSeq, seqs: allSeqs }); // 在「小于可读总量」的区间做细粒度扫描,找出所有 count -> 返回条数不相等的点 const lo = Math.max(1, allSeqs[0] - maxSeq + 1); const probeCounts = []; for (let c = Math.min(100, readable - 4); c <= readable + 2; c++) probeCounts.push(c); console.log(`细扫 count=${probeCounts[0]}..${probeCounts[probeCounts.length - 1]}(可读总量 ${readable}),重复 ${ROUNDS} 轮\n`); const shortMap = new Map(); for (let round = 1; round <= ROUNDS; round++) { for (const c of probeCounts) { const m = (await api('get_group_msg_history', { group_id: GROUP, count: c }))?.data?.messages ?? []; const got = m.map((x) => Number(x.real_seq)).sort((a, b) => a - b); const expect = allSeqs.slice(-c); const missing = expect.filter((s) => !new Set(got).has(s)); const extra = got.filter((s) => !new Set(expect).has(s)); const short = got.length < Math.min(c, readable); if (short || missing.length || extra.length) { const key = c; const n = (shortMap.get(key)?.n ?? 0) + 1; const prev = shortMap.get(key) ?? { n, samples: [] }; prev.n = n; if (prev.samples.length < 3) prev.samples.push({ round, returned: got.length, expected: expect.length, missing, extra, from: got[0], to: got[got.length - 1] }); shortMap.set(key, prev); console.log(` [r${round}] count=${c} -> 返回 ${got.length}(期望 ${Math.min(c, readable)})缺失 seq=${missing.join(',') || '-'} 多出=${extra.join(',') || '-'}`); } rec({ round, count: c, returned: got.length, expected: Math.min(c, readable), missing, extra, from: got[0], to: got[got.length - 1] }); await sleep(250); } } console.log('\n===== 判定 ====='); if (shortMap.size === 0) { console.log(' 本轮未复现「count 未达可读量却少给」,上一轮的 count=120->119 属偶发或已被新消息推移 ⇒ 仍禁止用条数做完整性判据(无法证伪即不可依赖)。'); } else { for (const [c, v] of shortMap) console.log(` count=${c}: ${v.n}/${ROUNDS} 轮出现异常 ` + v.samples.map((s) => `->${s.returned} 缺[${s.missing.join(',')}]`).join(' | ')); console.log(' ⇒ 复现成立:返回条数不保证等于 min(count, 可读量),存在边界丢条。'); } rec({ kind: 'verdict', readable, anomalies: Object.fromEntries([...shortMap].map(([c, v]) => [c, { n: v.n, samples: v.samples }])) }); console.log('\n结果 -> ' + OUT); try { ws.close(); } catch {} await sleep(300); process.exit(0);