Files
QLinker/tools/probe/seq-namespace-probe.mjs
T
xiaowang d42938f52d 采样第 26 章:可靠性四项定案,排除 WS 并发上限,纠正 history count 语义
- 新增 GC-018~GC-022:WS 按连接扇出且无并发上限、断线零补推、事件乱序、
  history 窗口边界稳定少返回一条、real_seq 仅会话内唯一(去重键须含会话类型+会话ID)
- REL-dedup/order/replay/id-consistency 由未采集转为 verified 并挂回归探针;
  REL-replay 的 parseStatus 枚举违规一并修正
- capability-check 支持 reliability 的 kind=verdict/phase 判定行与 expect.fields
  数值/空数组断言,已用构造坏样本自检出 4 项漂移并 FAIL
- 新增 7 个探针(reliability/timeline/ws-conn-limit/ws-reconnect/seq-namespace/
  history-count-scan/history-edge-offbyone),只读探针无写副作用
- 作废旧结论:群 50 条硬上限、WS 并发上限归因、min(count,可读量) 完整性判据
2026-10-30 11:01:59 +08:00

91 lines
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 零副作用:判定 real_seq 是「每会话独立命名空间」还是「全局递增」。
//
// 这是去重键能否定稿的最后一个硬前提。候选键 (bindingUid, group_id, real_seq) 隐含一个假设:
// real_seq 在同一会话内唯一。若群与私聊各自从 1 递增且取值区间重叠,则:
// - 只用 real_seq 做键必然跨会话撞号,会把好消息当重复丢掉(数据丢失级后果);
// - 键必须含会话维度(group_id / user_id),且群与私聊要用不同的列或联合判别式。
// 同时反查:事件通道的 real_seq 与 history 的 real_seq 是否同命名空间(值可直接相等比较)。
//
// 只调用 get_group_msg_history / get_friend_msg_history / get_group_member_list,不发送任何消息。
// 用法: NAPCAT_WS_TOKEN=xxx node tools/probe/seq-namespace-probe.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, `seq-namespace-${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 = 20000) => 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 }));
});
async function pull(action, key, id) {
const r = await api(action, { [key]: id, count: 200 });
const m = r?.data?.messages ?? [];
return m.map((x) => ({ seqRaw: x.real_seq, seq: Number(x.real_seq), mid: x.message_id,
uid: x.user_id ?? null, time: x.time }));
}
const G = await pull('get_group_msg_history', 'group_id', GROUP);
const P = await pull('get_friend_msg_history', 'user_id', FRIEND);
console.log(`群 ${GROUP} 窗口 ${G.length} 条 seq ${G.length ? G[0].seq + '..' + G[G.length - 1].seq : '-'}`);
console.log(`私聊 ${FRIEND} 窗口 ${P.length} 条 seq ${P.length ? P[0].seq + '..' + P[P.length - 1].seq : '-'}`);
const gset = new Set(G.map((x) => String(x.seqRaw)));
const pset = new Set(P.map((x) => String(x.seqRaw)));
const overlap = [...pset].filter((s) => gset.has(s));
console.log(`\n群内 real_seq 去重 ${gset.size}/${G.length} ⇒ ${gset.size === G.length ? '会话内唯一' : '存在重复'}`);
console.log(`私聊 real_seq 去重 ${pset.size}/${P.length} ⇒ ${pset.size === P.length ? '会话内唯一' : '存在重复'}`);
console.log(`跨会话取值重叠 ${overlap.length} 个` + (overlap.length ? `:${overlap.slice(0, 20).join(',')}${overlap.length > 20 ? '…' : ''}` : ''));
// 时间维度交叉验证:若为全局递增,则 seq 大小关系应与 time 大小关系一致(跨会话比较)。
// 取两侧同一时刻附近的消息,看 seq 是否可跨会话比较。
const gLast = G[G.length - 1], pLast = P[P.length - 1];
if (gLast && pLast) {
const seqGt = gLast.seq > pLast.seq, timeGt = gLast.time > pLast.time;
console.log(`\n最新一条:群 seq=${gLast.seq} time=${gLast.time} / 私聊 seq=${pLast.seq} time=${pLast.time}`);
console.log(`seq 大小方向与 time 大小方向${seqGt === timeGt ? '一致(样本太少不足以判定全局递增)' : '不一致 ⇒ seq 不是全局递增,属会话内命名空间'}`);
}
// 消息密度推断:群消息量远大于私聊,若 seq 全局递增则两者区间应连续衔接而非各自独立量级
if (G.length && P.length) {
const gRatio = G[G.length - 1].seq / (P[P.length - 1].seq || 1);
console.log(`量级比 群max/私聊max = ${gRatio.toFixed(2)}(群历史消息量远多于私聊,若比值接近消息量比则更像全局计数;若两区间互相穿插则更像独立命名空间)`);
}
const gInP = G.filter((x) => pset.has(String(x.seqRaw))).length;
console.log(`\n结论:`);
console.log(` 1) 去重键必须含会话维度。跨会话重叠 ${overlap.length} 个 ⇒ ` +
(overlap.length > 0 ? '若只用 real_seq 会把不同会话的消息误判为重复,属数据丢失级错误,禁止。'
: '本批样本未重叠,但两侧量级独立,仍不得只用 real_seq。'));
console.log(` 2) (bindingUid, group_id, real_seq) 中的 group_id 需扩展为「会话标识」而非仅群号,私聊用 user_id 填入同一列时必须有会话类型判别,否则群号与 QQ 号同为无符号 64 位整数、可撞号。`);
rec({ kind: 'verdict', group_window: G.length, group_distinct: gset.size,
private_window: P.length, private_distinct: pset.size,
cross_overlap_count: overlap.length, cross_overlap_sample: overlap.slice(0, 50),
group_max: gLast?.seq ?? null, private_max: pLast?.seq ?? null,
group_time: gLast?.time ?? null, private_time: pLast?.time ?? null });
console.log('\n结果 -> ' + OUT);
try { ws.close(); } catch {}
await sleep(300);
process.exit(0);