// 零副作用复测:history 单次返回条数上限到底是多少,以及是否随时间/窗口状态变化。 // // 起因:上一轮记为「群 count=100/200 均返回 50 条 ⇒ 50 条是本地可读量耗尽,不是接口截断」。 // 本轮 seq-namespace-probe 用 count=200 拿到 122 条,直接证伪「可读量只有 50」。 // 两种可能必须区分: // H1 存在真实接口截断(例如 50),上一轮观测正确,本轮 122 条是别的原因(参数被忽略/走了不同分支)。 // H2 不存在 50 截断,上一轮的 50 是当时可读量或调用方式差异所致。 // 判别法:同一进程内、同一连接上,对同一目标做完整 count 扫描,并额外测「count 很大 + 不传 count」, // 若返回条数随 count 单调增长并越过 50,则 H2 成立;若恒等于 50 则 H1 成立。 // // 只读,不发送任何消息。 // 用法: NAPCAT_WS_TOKEN=xxx node tools/probe/history-count-scan.mjs 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 FRIEND = Number(process.env.PROBE_FRIEND || 2161656029); 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-count-scan-${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 COUNTS = [10, 20, 30, 50, 51, 60, 80, 100, 120, 150, 200, 500, 1000]; const targets = [ { label: '群', action: 'get_group_msg_history', key: 'group_id', id: GROUP }, { label: '私聊', action: 'get_friend_msg_history', key: 'user_id', id: FRIEND }, ]; const table = []; for (const t of targets) { console.log(`\n===== ${t.label} ${t.id} =====`); const rows = []; for (const c of COUNTS) { const r = await api(t.action, { [t.key]: t.id, count: c }); const m = r?.data?.messages ?? []; const seqs = m.map((x) => Number(x.real_seq)); const row = { count: c, retcode: r.retcode ?? null, returned: m.length, from: seqs.length ? seqs[0] : null, to: seqs.length ? seqs[seqs.length - 1] : null, ascending: seqs.length > 1 && seqs[seqs.length - 1] > seqs[0], distinct: new Set(seqs).size, timeout: !!r.__timeout }; rows.push(row); rec({ label: t.label, ...row }); console.log(` count=${String(c).padStart(4)} -> ${String(m.length).padStart(4)} 条 seq ${row.from}..${row.to} 升序=${row.ascending} 去重=${row.distinct}`); await sleep(400); } // 不传 count 的默认值 const d = await api(t.action, { [t.key]: t.id }); const dm = d?.data?.messages ?? []; console.log(` count 不传 -> ${dm.length} 条(默认值)`); rec({ label: t.label, count: null, retcode: d.retcode ?? null, returned: dm.length, from: dm.length ? Number(dm[0].real_seq) : null, to: dm.length ? Number(dm[dm.length - 1].real_seq) : null }); await sleep(400); // 反向重复同一 count,排除瞬时抖动 const rep = await api(t.action, { [t.key]: t.id, count: 200 }); const rm = rep?.data?.messages ?? []; console.log(` 重复 count=200 -> ${rm.length} 条(一致性核对)`); rec({ label: t.label, kind: 'repeat', count: 200, returned: rm.length }); table.push({ label: t.label, rows, defaultCount: dm.length }); } console.log('\n===== 判定 ====='); // 注意区分两件不同的事,上一轮踩过的坑就是把它们混为一谈: // 「返回条数封顶于 N」既可能是【接口硬截断】,也可能是【该会话可读总量恰好是 N】。 // 唯一判别法:让可读总量增长(发消息)后重跑,若上限跟着涨则是可读量,若不动才是截断。 // 因此本探针单独跑不能给「上限」定性,只能给出「上限的下界 = 本次最大返回条数」。 for (const t of table) { const max = Math.max(...t.rows.map((r) => r.returned)); const peakRows = t.rows.filter((r) => r.returned === max); const minCountForMax = Math.min(...peakRows.map((r) => r.count)); const mono = t.rows.every((r, i) => i === 0 || r.returned >= t.rows[i - 1].returned); const from = Math.min(...t.rows.filter((r) => r.from).map((r) => r.from)); const to = Math.max(...t.rows.filter((r) => r.to).map((r) => r.to)); const span = to - from + 1; console.log(` ${t.label}: 本次最大返回 ${max} 条 ⇒ 单次条数上限的下界 = ${max}(真实上限未测到)`); console.log(` 达到该值的最小 count = ${minCountForMax};不传 count 默认 ${t.defaultCount} 条;条数随 count 单调不减 = ${mono}`); console.log(` 窗口 seq 跨度 ${from}..${to}(${span} 个编号)而实际返回 ${max} 条 ⇒ ` + (span === max ? '窗口内编号连续无空洞' : `存在 ${span - max} 个编号空洞(撤回/未入库/编号不分配给本会话)`)); const offByOne = t.rows.filter((r) => r.count < max && r.returned < r.count); if (offByOne.length) { console.log(` ⚠️ count 未达可读量却少给:` + offByOne.map((r) => `count=${r.count}->${r.returned}`).join(', ') + ` ⇒ 不能假设「返回条数 == min(count, 可读量)」严格成立,落库对账禁止用条数做完整性判据`); } rec({ kind: 'verdict', label: t.label, max_returned: max, upper_bound_of_limit_is_lower_bound: true, min_count_for_max, default_count: t.defaultCount, monotonic: mono, seq_from: from, seq_to: to, seq_span: span, holes: span - max, short_of_count: offByOne.map((r) => ({ count: r.count, returned: r.returned })) }); } console.log('\n 提示:要判定「接口硬上限」,必须让可读总量超过候选上限后重跑;本探针输出的是下界,不得写成上限值。'); console.log('\n结果 -> ' + OUT); try { ws.close(); } catch {} await sleep(300); process.exit(0);