@chainlesschain/personal-data-hub 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/__tests__/adapters/ai-chat-cookie-capture-spec.test.js +211 -0
  2. package/__tests__/adapters/ai-chat-health-checker.test.js +262 -0
  3. package/__tests__/adapters/ai-chat-history.test.js +8 -7
  4. package/__tests__/adapters/ai-chat-vendors.test.js +149 -8
  5. package/__tests__/adapters/social-toutiao-kuaishou-scaffold.test.js +269 -0
  6. package/__tests__/adapters/system-data-android-ingest.test.js +144 -0
  7. package/__tests__/adapters/system-data-android.test.js +387 -0
  8. package/__tests__/adapters/wechat-bootstrap.test.js +240 -0
  9. package/__tests__/adapters/wechat-env-probe.test.js +162 -0
  10. package/__tests__/adapters/wechat-frida-agent.test.js +322 -0
  11. package/__tests__/adapters/wechat-frida-integration.test.js +149 -0
  12. package/__tests__/adapters/wechat-frida-key-provider.test.js +188 -0
  13. package/__tests__/adapters/wechat-md5-key-provider.test.js +101 -0
  14. package/__tests__/analysis-skills.test.js +147 -0
  15. package/__tests__/analysis.test.js +329 -1
  16. package/__tests__/e2e/ai-chat-cross-source-journey.test.js +213 -0
  17. package/__tests__/e2e/full-user-journey.test.js +188 -0
  18. package/__tests__/integration/ai-chat-history-registry.test.js +228 -0
  19. package/__tests__/integration/aichat-wizard-end-to-end.test.js +282 -0
  20. package/__tests__/integration/cross-adapter-pipelines.test.js +396 -0
  21. package/__tests__/integration/social-bilibili-pipeline.test.js +261 -0
  22. package/__tests__/integration/wechat-bootstrap-end-to-end.test.js +390 -0
  23. package/__tests__/registry.test.js +4 -2
  24. package/__tests__/social-adapters.test.js +63 -14
  25. package/__tests__/social-bilibili-snapshot.test.js +278 -0
  26. package/__tests__/wechat-adapter.test.js +118 -0
  27. package/lib/adapters/ai-chat-history/ai-chat-adapter.js +55 -16
  28. package/lib/adapters/ai-chat-history/cookie-capture-spec.js +331 -0
  29. package/lib/adapters/ai-chat-history/health-checker.js +210 -0
  30. package/lib/adapters/ai-chat-history/schema-map.js +42 -5
  31. package/lib/adapters/ai-chat-history/vendor-spec.js +1 -0
  32. package/lib/adapters/ai-chat-history/vendors/doubao.js +255 -0
  33. package/lib/adapters/ai-chat-history/wizard-controller.js +473 -0
  34. package/lib/adapters/alipay-bill/alipay-bill-adapter.js +4 -0
  35. package/lib/adapters/social-bilibili/adapter.js +500 -0
  36. package/lib/adapters/social-bilibili/index.js +21 -169
  37. package/lib/adapters/social-kuaishou/index.js +237 -0
  38. package/lib/adapters/social-toutiao/index.js +236 -0
  39. package/lib/adapters/system-data-android/adapter.js +348 -0
  40. package/lib/adapters/system-data-android/index.js +76 -0
  41. package/lib/adapters/wechat/bootstrap.js +146 -0
  42. package/lib/adapters/wechat/content-parser.js +11 -2
  43. package/lib/adapters/wechat/db-reader.js +88 -10
  44. package/lib/adapters/wechat/env-probe.js +218 -0
  45. package/lib/adapters/wechat/frida-agent/loader.js +74 -0
  46. package/lib/adapters/wechat/frida-agent/wechat-key-hook.js +248 -0
  47. package/lib/adapters/wechat/index.js +9 -0
  48. package/lib/adapters/wechat/key-providers/frida-key-provider.js +252 -0
  49. package/lib/adapters/wechat/key-providers/index.js +22 -0
  50. package/lib/adapters/wechat/key-providers/key-provider-base.js +44 -0
  51. package/lib/adapters/wechat/key-providers/md5-key-provider.js +81 -0
  52. package/lib/adapters/wechat/normalize.js +12 -3
  53. package/lib/analysis-skills/spending.js +4 -1
  54. package/lib/analysis.js +191 -2
  55. package/lib/index.js +16 -0
  56. package/lib/prompt-builder.js +11 -1
  57. package/lib/query-parser.js +7 -1
  58. package/lib/vault.js +77 -0
  59. package/package.json +8 -1
@@ -37,9 +37,47 @@ describe("BilibiliAdapter", () => {
37
37
  expect(a.extractMode).toBe("device-pull");
38
38
  });
39
39
 
40
- it("rejects missing account.uid", () => {
41
- expect(() => new BilibiliAdapter({})).toThrow();
42
- expect(() => new BilibiliAdapter({ account: {} })).toThrow(/uid/);
40
+ it("accepts stateless construction (snapshot mode added in A8)", () => {
41
+ // Before A8: constructor required opts.account.uid. After A8 the adapter
42
+ // is stateless when running snapshot mode (in-APK Android cc reads a JSON
43
+ // produced by the phone). Sqlite mode still needs account.uid but the
44
+ // check moved into _syncViaSqlite where it actually matters.
45
+ expect(() => new BilibiliAdapter({})).not.toThrow();
46
+ expect(() => new BilibiliAdapter({ account: {} })).not.toThrow();
47
+ expect(() => new BilibiliAdapter()).not.toThrow();
48
+ });
49
+
50
+ it("sqlite mode rejects missing account.uid at sync time", async () => {
51
+ const a = new BilibiliAdapter({ dbPath: "/tmp/bili.db" });
52
+ // Path-existence check happens before account.uid validation, so we
53
+ // exercise the guard via dbPath=null + account=null which falls to
54
+ // "sync needs inputPath OR dbPath" first. Use a real-looking dbPath
55
+ // with no account to surface the account.uid throw deterministically.
56
+ const fs = require("node:fs");
57
+ const path = require("node:path");
58
+ const os = require("node:os");
59
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-no-acct-"));
60
+ const dbPath = path.join(dir, "bili.db");
61
+ fs.writeFileSync(dbPath, "fake");
62
+ try {
63
+ const b = new BilibiliAdapter({
64
+ dbPath,
65
+ dbDriverFactory: () => () => ({
66
+ prepare: () => ({ all: () => [] }),
67
+ close() {},
68
+ }),
69
+ });
70
+ let threw = null;
71
+ try {
72
+ for await (const _r of b.sync()) { /* drain */ }
73
+ } catch (err) {
74
+ threw = err;
75
+ }
76
+ expect(threw).toBeTruthy();
77
+ expect(String(threw.message)).toMatch(/account\.uid/);
78
+ } finally {
79
+ fs.rmSync(dir, { recursive: true, force: true });
80
+ }
43
81
  });
44
82
 
45
83
  it("sync yields history + favourite records via mocked driver", async () => {
@@ -83,26 +121,34 @@ describe("BilibiliAdapter", () => {
83
121
  }
84
122
  });
85
123
 
86
- it("idle when DB path missing", async () => {
124
+ it("throws when neither inputPath nor dbPath provided (A8: surface config errors)", async () => {
125
+ // Before A8: sync silently yielded 0 if dbPath missing — masked typos and
126
+ // misconfigured callers. After A8 we throw so callers see the problem.
87
127
  const a = new BilibiliAdapter({ account: { uid: "1234" } });
88
- const raws = [];
89
- for await (const r of a.sync()) raws.push(r);
90
- expect(raws).toHaveLength(0);
128
+ let threw = null;
129
+ try {
130
+ for await (const _r of a.sync()) { /* drain */ }
131
+ } catch (err) {
132
+ threw = err;
133
+ }
134
+ expect(threw).toBeTruthy();
135
+ expect(String(threw.message)).toMatch(/inputPath|dbPath/);
91
136
  });
92
137
 
93
- it("normalize captures bvid/avid/uploader into extra", async () => {
138
+ it("normalize captures bvid/avid/uploader into extra (flat payload, A8 shape)", async () => {
94
139
  const a = new BilibiliAdapter({ account: { uid: "1234" } });
95
140
  const raw = {
96
141
  adapter: "social-bilibili",
97
- originalId: "history-1",
142
+ kind: "history",
143
+ originalId: "bilibili:history:BV1abc",
98
144
  capturedAt: 1700000000000,
99
145
  payload: {
100
146
  kind: "history",
101
- row: {
102
- id: 1, bvid: "BV1abc", avid: "1234",
103
- title: "Test", view_at: 1700000000,
104
- uploader: "UpA", duration: 300,
105
- },
147
+ title: "Test",
148
+ bvid: "BV1abc",
149
+ avid: "1234",
150
+ uploader: "UpA",
151
+ duration: 300,
106
152
  },
107
153
  };
108
154
  const batch = a.normalize(raw);
@@ -110,6 +156,9 @@ describe("BilibiliAdapter", () => {
110
156
  expect(batch.events[0].extra.avid).toBe("1234");
111
157
  expect(batch.events[0].extra.uploader).toBe("UpA");
112
158
  expect(batch.events[0].extra.duration).toBe(300);
159
+ // A8: history also yields an item entity (video) for KG linkage
160
+ expect(batch.items).toHaveLength(1);
161
+ expect(batch.items[0].extra.bvid).toBe("BV1abc");
113
162
  });
114
163
  });
115
164
 
@@ -0,0 +1,278 @@
1
+ "use strict";
2
+
3
+ import { describe, it, expect, beforeEach } from "vitest";
4
+
5
+ const fs = require("node:fs");
6
+ const path = require("node:path");
7
+ const os = require("node:os");
8
+
9
+ const {
10
+ BilibiliAdapter,
11
+ SNAPSHOT_SCHEMA_VERSION,
12
+ VALID_KINDS,
13
+ } = require("../lib/adapters/social-bilibili");
14
+ const { validateBatch } = require("../lib/batch");
15
+
16
+ // A8 v0.1 (2026-05-22) — snapshot-mode tests, mirroring system-data-android.
17
+ //
18
+ // Why a separate file? `social-adapters.test.js` covers the legacy sqlite
19
+ // path (Phase 7.5 device-pull). Snapshot mode is a brand-new ingestion path
20
+ // driven by in-APK Android cc reading JSON from the phone's own WebView+OkHttp
21
+ // pipeline. Keeping tests separated makes it obvious which mode a regression
22
+ // belongs to.
23
+
24
+ function writeSnapshot(dir, snapshot) {
25
+ const p = path.join(dir, "social-bilibili.json");
26
+ fs.writeFileSync(p, JSON.stringify(snapshot), "utf-8");
27
+ return p;
28
+ }
29
+
30
+ describe("BilibiliAdapter snapshot mode", () => {
31
+ let tmpDir;
32
+ beforeEach(() => {
33
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-snap-"));
34
+ });
35
+
36
+ it("exports SNAPSHOT_SCHEMA_VERSION = 1 + 4 VALID_KINDS", () => {
37
+ expect(SNAPSHOT_SCHEMA_VERSION).toBe(1);
38
+ expect(VALID_KINDS).toEqual(["history", "favourite", "dynamic", "follow"]);
39
+ });
40
+
41
+ it("authenticate(inputPath) ok when readable", async () => {
42
+ const p = writeSnapshot(tmpDir, {
43
+ schemaVersion: 1,
44
+ snapshottedAt: Date.now(),
45
+ events: [],
46
+ });
47
+ const a = new BilibiliAdapter();
48
+ const res = await a.authenticate({ inputPath: p });
49
+ expect(res.ok).toBe(true);
50
+ expect(res.mode).toBe("snapshot-file");
51
+ });
52
+
53
+ it("authenticate(inputPath) fails when path unreadable", async () => {
54
+ const a = new BilibiliAdapter();
55
+ const res = await a.authenticate({ inputPath: path.join(tmpDir, "missing.json") });
56
+ expect(res.ok).toBe(false);
57
+ expect(res.reason).toBe("INPUT_PATH_UNREADABLE");
58
+ });
59
+
60
+ it("authenticate() with neither inputPath nor dbPath returns NO_INPUT", async () => {
61
+ const a = new BilibiliAdapter();
62
+ const res = await a.authenticate({});
63
+ expect(res.ok).toBe(false);
64
+ expect(res.reason).toBe("NO_INPUT");
65
+ });
66
+
67
+ it("rejects schemaVersion mismatch", async () => {
68
+ const p = writeSnapshot(tmpDir, {
69
+ schemaVersion: 99,
70
+ snapshottedAt: Date.now(),
71
+ events: [],
72
+ });
73
+ const a = new BilibiliAdapter();
74
+ let threw = null;
75
+ try {
76
+ for await (const _r of a.sync({ inputPath: p })) { /* drain */ }
77
+ } catch (err) {
78
+ threw = err;
79
+ }
80
+ expect(threw).toBeTruthy();
81
+ expect(String(threw.message)).toMatch(/schemaVersion mismatch/);
82
+ });
83
+
84
+ it("empty events array yields nothing (no crash)", async () => {
85
+ const p = writeSnapshot(tmpDir, {
86
+ schemaVersion: 1,
87
+ snapshottedAt: Date.now(),
88
+ events: [],
89
+ });
90
+ const a = new BilibiliAdapter();
91
+ const raws = [];
92
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
93
+ expect(raws).toHaveLength(0);
94
+ });
95
+
96
+ it("yields all 4 kinds + normalize produces valid batches", async () => {
97
+ const p = writeSnapshot(tmpDir, {
98
+ schemaVersion: 1,
99
+ snapshottedAt: 1716000000000,
100
+ account: { uid: "12345", displayName: "alice" },
101
+ events: [
102
+ {
103
+ kind: "history",
104
+ id: "BV1abc",
105
+ capturedAt: 1715000000000,
106
+ title: "Rust 异步学习",
107
+ bvid: "BV1abc",
108
+ avid: 42,
109
+ duration: 600,
110
+ uploader: "技术UP主",
111
+ uploaderMid: 100,
112
+ part: "01 介绍",
113
+ },
114
+ {
115
+ kind: "favourite",
116
+ id: "fav-BV2def",
117
+ capturedAt: 1714000000000,
118
+ title: "前端架构",
119
+ bvid: "BV2def",
120
+ folderName: "学习",
121
+ uploader: "码农UP",
122
+ },
123
+ {
124
+ kind: "dynamic",
125
+ id: "dyn-99",
126
+ capturedAt: 1713000000000,
127
+ summary: "今天发了一个新视频",
128
+ dynamicType: "video",
129
+ rid: 99,
130
+ authorMid: 200,
131
+ authorName: "我关注的UP",
132
+ },
133
+ {
134
+ kind: "follow",
135
+ id: "follow-300",
136
+ capturedAt: 1712000000000,
137
+ mid: 300,
138
+ uname: "美食UP",
139
+ face: "https://i0.hdslb.com/...",
140
+ sign: "好吃的视频",
141
+ },
142
+ ],
143
+ });
144
+ const a = new BilibiliAdapter();
145
+ const raws = [];
146
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
147
+
148
+ expect(raws).toHaveLength(4);
149
+ expect(raws.map((r) => r.kind).sort()).toEqual([
150
+ "dynamic",
151
+ "favourite",
152
+ "follow",
153
+ "history",
154
+ ]);
155
+ // Stable originalId format
156
+ expect(raws.find((r) => r.kind === "history").originalId).toBe("bilibili:history:BV1abc");
157
+ expect(raws.find((r) => r.kind === "favourite").originalId).toBe("bilibili:favourite:fav-BV2def");
158
+ expect(raws.find((r) => r.kind === "dynamic").originalId).toBe("bilibili:dynamic:dyn-99");
159
+ expect(raws.find((r) => r.kind === "follow").originalId).toBe("bilibili:follow:follow-300");
160
+
161
+ // Normalize each + validate
162
+ for (const raw of raws) {
163
+ const batch = a.normalize(raw);
164
+ const v = validateBatch(batch);
165
+ expect(v.valid).toBe(true);
166
+
167
+ if (raw.kind === "history") {
168
+ expect(batch.events[0].subtype).toBe("browse");
169
+ expect(batch.events[0].extra.bvid).toBe("BV1abc");
170
+ expect(batch.events[0].extra.duration).toBe(600);
171
+ expect(batch.items).toHaveLength(1);
172
+ expect(batch.items[0].name).toBe("Rust 异步学习");
173
+ } else if (raw.kind === "favourite") {
174
+ expect(batch.events[0].subtype).toBe("like");
175
+ expect(batch.events[0].extra.folderName).toBe("学习");
176
+ expect(batch.items).toHaveLength(1);
177
+ } else if (raw.kind === "dynamic") {
178
+ expect(batch.events[0].subtype).toBe("browse");
179
+ expect(batch.events[0].extra.dynamicType).toBe("video");
180
+ expect(batch.events[0].extra.authorName).toBe("我关注的UP");
181
+ } else if (raw.kind === "follow") {
182
+ // Follow yields a person, not an event
183
+ expect(batch.events).toHaveLength(0);
184
+ expect(batch.persons).toHaveLength(1);
185
+ expect(batch.persons[0].names[0]).toBe("美食UP");
186
+ expect(batch.persons[0].identifiers["bilibili-mid"]).toEqual(["300"]);
187
+ }
188
+ }
189
+ });
190
+
191
+ it("per-kind include filter (e.g. include.follow=false drops follows)", async () => {
192
+ const p = writeSnapshot(tmpDir, {
193
+ schemaVersion: 1,
194
+ snapshottedAt: Date.now(),
195
+ events: [
196
+ { kind: "history", id: "h1", title: "x" },
197
+ { kind: "follow", id: "f1", mid: 1, uname: "u" },
198
+ ],
199
+ });
200
+ const a = new BilibiliAdapter();
201
+ const raws = [];
202
+ for await (const r of a.sync({ inputPath: p, include: { follow: false } })) {
203
+ raws.push(r);
204
+ }
205
+ expect(raws).toHaveLength(1);
206
+ expect(raws[0].kind).toBe("history");
207
+ });
208
+
209
+ it("limit caps emission", async () => {
210
+ const p = writeSnapshot(tmpDir, {
211
+ schemaVersion: 1,
212
+ snapshottedAt: Date.now(),
213
+ events: [
214
+ { kind: "history", id: "1", title: "a" },
215
+ { kind: "history", id: "2", title: "b" },
216
+ { kind: "history", id: "3", title: "c" },
217
+ ],
218
+ });
219
+ const a = new BilibiliAdapter();
220
+ const raws = [];
221
+ for await (const r of a.sync({ inputPath: p, limit: 2 })) raws.push(r);
222
+ expect(raws).toHaveLength(2);
223
+ });
224
+
225
+ it("skips unknown kinds (forward-compat with future event types)", async () => {
226
+ const p = writeSnapshot(tmpDir, {
227
+ schemaVersion: 1,
228
+ snapshottedAt: Date.now(),
229
+ events: [
230
+ { kind: "history", id: "1", title: "ok" },
231
+ { kind: "fancy-new-kind-from-future", id: "x", data: "?" },
232
+ { kind: "favourite", id: "f", title: "also ok" },
233
+ ],
234
+ });
235
+ const a = new BilibiliAdapter();
236
+ const raws = [];
237
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
238
+ expect(raws).toHaveLength(2);
239
+ expect(raws.map((r) => r.kind).sort()).toEqual(["favourite", "history"]);
240
+ });
241
+
242
+ it("uses fallback originalId when event.id absent (no crash, still ingestable)", async () => {
243
+ const p = writeSnapshot(tmpDir, {
244
+ schemaVersion: 1,
245
+ snapshottedAt: Date.now(),
246
+ events: [
247
+ // Missing id — adapter should derive from bvid/mid/rid or generate fallback
248
+ { kind: "history", bvid: "BV1xyz", title: "no-id" },
249
+ { kind: "follow", mid: 999, uname: "with-mid-no-id" },
250
+ { kind: "dynamic", summary: "no id no rid" },
251
+ ],
252
+ });
253
+ const a = new BilibiliAdapter();
254
+ const raws = [];
255
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
256
+ expect(raws).toHaveLength(3);
257
+ // history derives from bvid
258
+ expect(raws[0].originalId).toBe("bilibili:history:BV1xyz");
259
+ // follow derives from mid
260
+ expect(raws[1].originalId).toBe("bilibili:follow:999");
261
+ // dynamic with no id/bvid/mid/rid → fallback unknown- prefix
262
+ expect(raws[2].originalId).toMatch(/^bilibili:dynamic:unknown-/);
263
+ });
264
+
265
+ it("snapshot account propagates to payload (Path Y can re-attribute later)", async () => {
266
+ const p = writeSnapshot(tmpDir, {
267
+ schemaVersion: 1,
268
+ snapshottedAt: Date.now(),
269
+ account: { uid: "55555", displayName: "tester" },
270
+ events: [{ kind: "history", id: "1", title: "x" }],
271
+ });
272
+ const a = new BilibiliAdapter();
273
+ const raws = [];
274
+ for await (const r of a.sync({ inputPath: p })) raws.push(r);
275
+ expect(raws).toHaveLength(1);
276
+ expect(raws[0].payload.account.uid).toBe("55555");
277
+ });
278
+ });
@@ -331,6 +331,29 @@ describe("normalizeWeChatContact", () => {
331
331
  const b = normalizeWeChatContact({});
332
332
  expect(b.persons).toHaveLength(0);
333
333
  });
334
+
335
+ // sjqz parity audit follow-up (post-Phase 12.6.10) — classify
336
+ // 公众号 / Official Accounts (gh_*) as merchant subtype so the Ask
337
+ // flow / EntityResolver can filter them out of human contacts.
338
+ it("gh_* username → subtype merchant (公众号 / Official Account)", () => {
339
+ const b = normalizeWeChatContact({
340
+ username: "gh_abc123def",
341
+ nickname: "某品牌官方",
342
+ type: 3,
343
+ });
344
+ expect(b.persons).toHaveLength(1);
345
+ expect(b.persons[0].subtype).toBe("merchant");
346
+ expect(b.persons[0].identifiers.wechatId).toBe("gh_abc123def");
347
+ });
348
+
349
+ it("regular wxid_* → subtype contact (default)", () => {
350
+ const b = normalizeWeChatContact({
351
+ username: "wxid_realfriend",
352
+ nickname: "好友",
353
+ type: 1,
354
+ });
355
+ expect(b.persons[0].subtype).toBe("contact");
356
+ });
334
357
  });
335
358
 
336
359
  // ─── WechatAdapter contract + sync flow ──────────────────────────────────
@@ -441,6 +464,101 @@ describe("WechatAdapter.sync with mocked DB reader", () => {
441
464
  }
442
465
  });
443
466
 
467
+ // sjqz parity audit follow-up — fetchContacts must exclude
468
+ // @stranger and fake_* by default (vault pollution prevention).
469
+ it("fetchContacts excludes @stranger and fake_* by default", async () => {
470
+ // Pure DI smoke — capture the SQL passed to .prepare() to verify the
471
+ // junk filter is in the query. We mock just enough of better-sqlite3's
472
+ // shape: db.prepare(sql) → { all(limit) → rows }, exec, pragma.
473
+ const seenSql = [];
474
+ const fakeDriver = function Database(_path, _opts) {
475
+ return {
476
+ pragma: () => undefined,
477
+ exec: () => undefined,
478
+ prepare(sql) {
479
+ seenSql.push(sql);
480
+ return {
481
+ all: () => {
482
+ if (sql.startsWith("PRAGMA table_info")) {
483
+ return [
484
+ { name: "username" },
485
+ { name: "alias" },
486
+ { name: "nickname" },
487
+ { name: "conRemark" },
488
+ { name: "type" },
489
+ ];
490
+ }
491
+ if (sql.startsWith("SELECT count")) return [{ n: 5 }];
492
+ if (sql.includes("FROM rcontact")) return [];
493
+ return [];
494
+ },
495
+ get: () => ({ n: 5 }),
496
+ };
497
+ },
498
+ close: () => undefined,
499
+ };
500
+ };
501
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "wechat-junkfilt-"));
502
+ const dbPath = path.join(dir, "EnMicroMsg.db");
503
+ fs.writeFileSync(dbPath, "fake");
504
+ try {
505
+ const reader = new WeChatDBReader({
506
+ dbPath,
507
+ keyProvider: { getKey: async () => "0".repeat(64) },
508
+ driver: fakeDriver,
509
+ });
510
+ await reader.open();
511
+ reader.fetchContacts({ limit: 100 });
512
+ const contactsSql = seenSql.find((s) => s.includes("FROM rcontact"));
513
+ expect(contactsSql).toBeDefined();
514
+ expect(contactsSql).toMatch(/NOT LIKE '%@stranger'/);
515
+ expect(contactsSql).toMatch(/NOT LIKE 'fake_%'/);
516
+ } finally {
517
+ fs.rmSync(dir, { recursive: true, force: true });
518
+ }
519
+ });
520
+
521
+ it("fetchContacts with includeJunk:true drops the filter (forensic mode)", async () => {
522
+ const seenSql = [];
523
+ const fakeDriver = function Database() {
524
+ return {
525
+ pragma: () => undefined,
526
+ exec: () => undefined,
527
+ prepare(sql) {
528
+ seenSql.push(sql);
529
+ return {
530
+ all: () => {
531
+ if (sql.startsWith("PRAGMA table_info")) {
532
+ return ["username", "alias", "nickname", "conRemark", "type"].map((name) => ({ name }));
533
+ }
534
+ if (sql.startsWith("SELECT count")) return [{ n: 5 }];
535
+ return [];
536
+ },
537
+ get: () => ({ n: 5 }),
538
+ };
539
+ },
540
+ close: () => undefined,
541
+ };
542
+ };
543
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "wechat-incljunk-"));
544
+ const dbPath = path.join(dir, "EnMicroMsg.db");
545
+ fs.writeFileSync(dbPath, "fake");
546
+ try {
547
+ const reader = new WeChatDBReader({
548
+ dbPath,
549
+ keyProvider: { getKey: async () => "0".repeat(64) },
550
+ driver: fakeDriver,
551
+ });
552
+ await reader.open();
553
+ reader.fetchContacts({ limit: 100, includeJunk: true });
554
+ const contactsSql = seenSql.find((s) => s.includes("FROM rcontact"));
555
+ expect(contactsSql).toBeDefined();
556
+ expect(contactsSql).not.toMatch(/NOT LIKE/);
557
+ } finally {
558
+ fs.rmSync(dir, { recursive: true, force: true });
559
+ }
560
+ });
561
+
444
562
  it("idle no-op when DB path missing", async () => {
445
563
  const a = new WechatAdapter({
446
564
  account: { uin: "self123" },
@@ -45,6 +45,7 @@ const { SPEC: hunyuanSpec } = require("./vendors/hunyuan");
45
45
  const { SPEC: qianfanSpec } = require("./vendors/qianfan");
46
46
  const { SPEC: cozeSpec } = require("./vendors/coze");
47
47
  const { SPEC: dreaminaSpec } = require("./vendors/dreamina");
48
+ const { SPEC: doubaoSpec } = require("./vendors/doubao");
48
49
 
49
50
  const DEFAULT_VENDOR_SPECS = Object.freeze({
50
51
  deepseek: deepseekSpec,
@@ -55,6 +56,7 @@ const DEFAULT_VENDOR_SPECS = Object.freeze({
55
56
  qianfan: qianfanSpec,
56
57
  coze: cozeSpec,
57
58
  dreamina: dreaminaSpec,
59
+ doubao: doubaoSpec,
58
60
  });
59
61
 
60
62
  class AIChatHistoryAdapter {
@@ -186,13 +188,19 @@ class AIChatHistoryAdapter {
186
188
  /**
187
189
  * Stream conversation + message envelopes across all configured vendors.
188
190
  *
189
- * Yields raw events of two shapes:
190
- * { kind: "conversation", vendor, conversation: RawConversation }
191
- * { kind: "message", vendor, message: RawMessage }
191
+ * Yields AdapterRegistry-compliant envelopes:
192
+ * { originalId, capturedAt, payload: { kind, vendor, conversation|message } }
192
193
  *
193
- * The registry calls `normalize(raw)` per yielded event. We deliberately
194
- * keep one Raw per yield (rather than batching) so a slow vendor doesn't
195
- * block faster ones at the registry boundary.
194
+ * The inner `payload.kind` distinguishes:
195
+ * - "conversation" → emit Topic + vendor Person (no Event yet)
196
+ * - "message" → emit Event + items + vendor Person
197
+ * - "vendor-not-wired" → no-op normalize (Phase 10.1 stub trace)
198
+ * - "vendor-cookie-expired" → no-op normalize (401/403 trace)
199
+ * - "vendor-rate-limited" → no-op normalize (429 trace after retries)
200
+ *
201
+ * The registry calls `normalize(raw)` per yielded envelope. One yield per
202
+ * conversation/message keeps registry batches small so a slow vendor
203
+ * doesn't block faster ones at the registry boundary.
196
204
  *
197
205
  * @param {object} [opts]
198
206
  * @param {string[]} [opts.vendors] restrict to a subset
@@ -216,28 +224,54 @@ class AIChatHistoryAdapter {
216
224
 
217
225
  try {
218
226
  for await (const conv of spec.listConversations(ctx, { since: vendorWatermark })) {
219
- yield { kind: "conversation", vendor, conversation: conv };
227
+ yield {
228
+ originalId: `${vendor}:conv:${conv.originalId}`,
229
+ capturedAt: Number(conv.updatedAt) || Number(conv.createdAt) || Date.now(),
230
+ payload: { kind: "conversation", vendor, conversation: conv },
231
+ };
220
232
 
221
233
  for await (const msg of spec.listMessages(ctx, conv.originalId, {})) {
222
- yield { kind: "message", vendor, message: msg };
234
+ yield {
235
+ originalId: `${vendor}:msg:${msg.originalId}`,
236
+ capturedAt: Number(msg.createdAt) || Date.now(),
237
+ payload: { kind: "message", vendor, message: msg },
238
+ };
223
239
  }
224
240
  }
225
241
  } catch (err) {
242
+ const traceCapturedAt = Date.now();
226
243
  if (err instanceof NotImplementedYetError) {
227
244
  this._logger.warn(
228
245
  `[ai-chat] vendor=${vendor} not wired (Phase 10.2+ work): ${err.message}`,
229
246
  );
230
- yield { kind: "vendor-not-wired", vendor, error: err.code };
247
+ yield {
248
+ originalId: `${vendor}:trace:not-wired:${traceCapturedAt}`,
249
+ capturedAt: traceCapturedAt,
250
+ payload: { kind: "vendor-not-wired", vendor, error: err.code },
251
+ };
231
252
  continue;
232
253
  }
233
254
  if (err instanceof CookieExpiredError) {
234
255
  this._logger.warn(`[ai-chat] vendor=${vendor} cookie expired: ${err.message}`);
235
- yield { kind: "vendor-cookie-expired", vendor, error: err.code };
256
+ yield {
257
+ originalId: `${vendor}:trace:cookie-expired:${traceCapturedAt}`,
258
+ capturedAt: traceCapturedAt,
259
+ payload: { kind: "vendor-cookie-expired", vendor, error: err.code },
260
+ };
236
261
  continue;
237
262
  }
238
263
  if (err instanceof RateLimitedError) {
239
264
  this._logger.warn(`[ai-chat] vendor=${vendor} rate limited: ${err.message}`);
240
- yield { kind: "vendor-rate-limited", vendor, error: err.code, retryAfterMs: err.retryAfterMs };
265
+ yield {
266
+ originalId: `${vendor}:trace:rate-limited:${traceCapturedAt}`,
267
+ capturedAt: traceCapturedAt,
268
+ payload: {
269
+ kind: "vendor-rate-limited",
270
+ vendor,
271
+ error: err.code,
272
+ retryAfterMs: err.retryAfterMs,
273
+ },
274
+ };
241
275
  continue;
242
276
  }
243
277
  throw err;
@@ -256,14 +290,19 @@ class AIChatHistoryAdapter {
256
290
  if (!raw || typeof raw !== "object") {
257
291
  return { events: [], persons: [], places: [], items: [], topics: [] };
258
292
  }
293
+ // Registry-compliant envelopes wrap kind inside payload. Adapter-internal
294
+ // tests (Phase 10.1) sometimes pass the inner shape directly — accept
295
+ // both for forward compat.
296
+ const inner = raw.payload && typeof raw.payload === "object" ? raw.payload : raw;
297
+ const kind = inner.kind;
259
298
 
260
- if (raw.kind === "vendor-not-wired") {
299
+ if (kind === "vendor-not-wired" || kind === "vendor-cookie-expired" || kind === "vendor-rate-limited") {
261
300
  // Nothing to write; the warning was already logged by sync().
262
301
  return { events: [], persons: [], places: [], items: [], topics: [] };
263
302
  }
264
303
 
265
- if (raw.kind === "conversation") {
266
- const conv = raw.conversation;
304
+ if (kind === "conversation") {
305
+ const conv = inner.conversation;
267
306
  const spec = this._vendorSpecs[conv.vendor];
268
307
  const displayName = spec ? spec.displayName : conv.vendor;
269
308
  return {
@@ -275,8 +314,8 @@ class AIChatHistoryAdapter {
275
314
  };
276
315
  }
277
316
 
278
- if (raw.kind === "message") {
279
- const msg = raw.message;
317
+ if (kind === "message") {
318
+ const msg = inner.message;
280
319
  const spec = this._vendorSpecs[msg.vendor];
281
320
  const displayName = spec ? spec.displayName : msg.vendor;
282
321
  return {