Files
QLinker/tools/probe/private-history-audit.mjs
xiaowang a2a781ab4c docs(p1): 补齐 record/私聊/转发读回三证据,定性合并转发不可还原
- 新增 forward-readback-probe(只读):对 history 全部 8 条外层 multimsg 逐条
  重试 get_forward_msg,含数分钟前的 seq=3115,19 种参数组合全失败,
  排除「消息已过期」解释,确认为接口能力缺口(第 20.2 旧依据就地标注缺陷)
- 关键发现:news[].text 能否 parse 成段数组取决于当初 node.content 传数组还是
  JSON 字符串,唯一能 parse 的两条恰是发送形态用错的那两条 ⇒ 属字面文本假象,
  非还原能力。正确形态下段类型与嵌套结构不可逆丢失(GC-012, severity high)
- 未采边界显式登记:正确形态样本节点全是 text,媒体节点降级形态无实测依据,
  不得宣称「只是降级为文本」
- get_msg 实测 9/9 retcode=0 ⇒ 新增 OP-get-msg,作为不依赖 history 窗口分页的
  单条定点读回路径,用于兜底 file 段零事件与私聊 history 分页缺口
- resid 非恒定存在(tsum=0 的静默失效消息 detail_keys 无 resid)⇒ 按可空建模
- record 定性收紧为 MSG-record / MSG-record-silk / MSG-record-transcode 三项;
  私聊段矩阵第三证据 6/6 一致;门禁缺测项 1 → 0
- 能力基线 29 → 30 项、globalConstraints 11 → 12;capability-check 识别
  forwardReadback 自动源。结构校验 0 错误 0 警告,漂移比对自动 21 项、
  漂移 0、缺测 0、人工确认 4 → PASS
2026-10-29 16:27:51 +08:00

111 lines
5.6 KiB
JavaScript
Raw Permalink 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.
// 私聊段类型矩阵的第三证据补齐:用 get_friend_msg_history 回读。
//
// 此前私聊用例(p_text / p_face / p_image_b64 / p_json / p_reply / p_at / p_upload_file)
// 只有「API retcode + 事件回推段」两条证据,缺 history 回读,
// 因为 get_group_msg_history 不适用于私聊。本脚本改用 get_friend_msg_history,
// 纯只读,不发送任何消息,把已有事件的段类型与私聊 history 的段类型逐条对齐。
//
// 判定口径与三证据矩阵一致:retcode=0 不等于送达,必须看回读段。
// 凭据只从环境变量读取。
import fs from 'node:fs';
import path from 'node:path';
const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099';
const TOKEN = process.env.NAPCAT_WS_TOKEN;
const FRIEND = Number(process.env.PROBE_FRIEND || 2161656029);
const OUT_DIR = process.env.PROBE_OUT || 'local';
const EVENT_FILE = process.env.PROBE_EV || 'local/matrix-ev-2026-10-29T06-48-10-358Z.jsonl';
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
fs.mkdirSync(OUT_DIR, { recursive: true });
const OUT = path.join(OUT_DIR, `private-history-audit-${stamp}.jsonl`);
const say = (...a) => console.log(...a);
const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`);
const pending = new Map();
let opened = false, authed = false;
ws.onopen = () => { opened = true; };
ws.onmessage = (ev) => {
let o; try { o = JSON.parse(String(ev.data)); } catch { return; }
if (o.echo && pending.has(o.echo)) { pending.get(o.echo).resolve(o); pending.delete(o.echo); return; }
if (o.post_type === 'meta_event' && o.meta_event_type === 'lifecycle') authed = true;
};
for (let i = 0; i < 100 && !opened; i++) await new Promise((r) => setTimeout(r, 100));
if (!opened) { console.error('连接超时'); process.exit(1); }
for (let i = 0; i < 100 && !authed; i++) await new Promise((r) => setTimeout(r, 100));
say('认证=' + authed);
const api = (action, params, ms = 40000) => new Promise((resolve) => {
const echo = `ph-${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 }));
});
// 1) 从已落盘的事件样本里提取私聊自发送消息的标记与段类型
const segsOf = (o) => (Array.isArray(o.message) ? o.message : []).map((s) => s.type).join('+');
const labelOf = (o) => {
const t = (Array.isArray(o.message) ? o.message : [])
.filter((s) => s.type === 'text').map((s) => s.data?.text ?? '').join('');
return (t.match(/\[QLm?:([a-z0-9_]+)\]/i) || [])[1] ?? null;
};
const evById = new Map();
if (fs.existsSync(EVENT_FILE)) {
for (const l of fs.readFileSync(EVENT_FILE, 'utf8').trim().split('\n')) {
let o; try { o = JSON.parse(l); } catch { continue; }
const d = o.data ?? o;
if (d.post_type !== 'message_sent' && d.message_type !== 'private') continue;
if (d.message_type !== 'private') continue;
const lab = labelOf(d);
if (lab) evById.set(lab, { segs: segsOf(d), message_id: d.message_id ?? null, seq: d.real_seq ?? null });
}
}
say(`事件样本中的私聊消息 ${evById.size} 条:${[...evById.keys()].join(', ') || '(无)'}`);
// 2) 只读回读私聊 history
const h = await api('get_friend_msg_history', { user_id: FRIEND, count: Number(process.env.PROBE_COUNT || 60) });
if (h.__timeout || h.retcode !== 0) {
say('get_friend_msg_history 失败:' + JSON.stringify(h).slice(0, 200));
try { ws.close(); } catch {} process.exit(2);
}
const msgs = h?.data?.messages ?? [];
say(`私聊 history 回读 ${msgs.length} 条`);
fs.appendFileSync(OUT, JSON.stringify({ kind: 'history_raw', count: msgs.length }) + '\n');
for (const m of msgs) {
fs.appendFileSync(OUT, JSON.stringify({ kind: 'history_msg', seq: m.real_seq,
message_id: m.message_id, segs: segsOf(m), raw: String(m.raw_message ?? '').slice(0, 300),
message: m.message }) + '\n');
}
// 3) 逐条对齐:事件段 vs history 段
const histByLabel = new Map();
for (const m of msgs) {
const lab = labelOf(m);
if (lab && !histByLabel.has(lab)) histByLabel.set(lab, { segs: segsOf(m), seq: m.real_seq, message_id: m.message_id });
}
say('\n--- 私聊三证据对齐(事件段 vs history 回读段)---');
const rows = [];
let hit = 0, total = 0;
for (const [lab, ev] of evById) {
total++;
const hb = histByLabel.get(lab);
if (!hb) { rows.push({ id: lab, event_segs: ev.segs, readback_segs: null, verdict: 'readback_gap' });
say(` ${lab.padEnd(16)} 事件段=${ev.segs.padEnd(18)} history=未命中 → 回读缺口`); continue; }
const same = ev.segs === hb.segs;
if (same) hit++;
rows.push({ id: lab, event_segs: ev.segs, readback_segs: hb.segs, seq: hb.seq,
verdict: same ? 'consistent' : 'seg_mismatch' });
// 与 history-audit / record-amr 探针保持同一字段口径,使 capability-check 能自动比对 history 列
fs.appendFileSync(OUT, JSON.stringify({ kind: 'history', id: lab, event_segs: ev.segs,
history_segs: hb.segs, history_seq: hb.seq }) + '\n');
say(` ${lab.padEnd(16)} 事件段=${ev.segs.padEnd(18)} history=${hb.segs.padEnd(18)} seq=${hb.seq} ${same ? '一致' : '★段不一致'}`);
}
say(`\n命中并对齐 ${hit}/${total}(history 中未带标记的私聊消息不参与统计)`);
fs.appendFileSync(OUT, JSON.stringify({ __summary: true, rows, hit, total }) + '\n');
say('结果 -> ' + OUT);
try { ws.close(); } catch {}
process.exit(0);