@kanadego/dsh-heartbeat 2.2.0 → 2.3.0

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 (61) hide show
  1. package/README.md +112 -302
  2. package/client.js +186 -24
  3. package/dist/{bili-login-XQVEE4M6.js → bili-login-KDDU63Z2.js} +4 -4
  4. package/dist/{bindings-U2M3CO2D.js → bindings-LTSGGNIH.js} +3 -3
  5. package/dist/{chunk-AT32XCCI.js → chunk-43Q42HWH.js} +8 -2
  6. package/dist/chunk-43Q42HWH.js.map +1 -0
  7. package/dist/{chunk-LQKOYTRL.js → chunk-4OQXAHU5.js} +2 -2
  8. package/dist/{chunk-3QJJRXIR.js → chunk-5CVAW5AS.js} +11 -2
  9. package/dist/chunk-5CVAW5AS.js.map +1 -0
  10. package/dist/{chunk-IBBAISSW.js → chunk-66IUDHCY.js} +50 -1
  11. package/dist/chunk-66IUDHCY.js.map +1 -0
  12. package/dist/{chunk-3DNGY4DS.js → chunk-77UYDI2A.js} +2 -2
  13. package/dist/{chunk-WF7ZBW5C.js → chunk-ABFO4QII.js} +902 -167
  14. package/dist/chunk-ABFO4QII.js.map +1 -0
  15. package/dist/{chunk-M72T5MZU.js → chunk-GOLMRQRW.js} +3 -2
  16. package/dist/{chunk-XYBII5PR.js → chunk-HSTGRQLP.js} +178 -39
  17. package/dist/chunk-HSTGRQLP.js.map +1 -0
  18. package/dist/{chunk-KZLVR4J7.js → chunk-IPTNSXOY.js} +2 -2
  19. package/dist/{chunk-DLOYERAK.js → chunk-JIRQ36F7.js} +3 -3
  20. package/dist/{chunk-ECXYNTEV.js → chunk-K47VV6IQ.js} +5 -3
  21. package/dist/chunk-K47VV6IQ.js.map +1 -0
  22. package/dist/{chunk-F2XVKX55.js → chunk-KGOC6EX4.js} +3 -3
  23. package/dist/{chunk-SIY3YHFY.js → chunk-MMI6OBEC.js} +2 -2
  24. package/dist/{chunk-N6SMIGYJ.js → chunk-QOLKUYQ3.js} +2 -2
  25. package/dist/cli/index.js +94 -23
  26. package/dist/cli/index.js.map +1 -1
  27. package/dist/{focus-store-RNHBAFYS.js → focus-store-XGTPIVF6.js} +3 -3
  28. package/dist/{inbox-ZLX2MH7A.js → inbox-7GGEMBUD.js} +4 -4
  29. package/dist/index.d.ts +60 -1
  30. package/dist/index.js +468 -155
  31. package/dist/index.js.map +1 -1
  32. package/dist/{migrate-XFZU5EBE.js → migrate-6HH2ZARV.js} +4 -4
  33. package/dist/{orchestrator-QE2XVVM6.js → orchestrator-OJJM5QSV.js} +20 -11
  34. package/dist/{pool-76VLAZER.js → pool-QTY2MI56.js} +4 -4
  35. package/dist/runtime-RGZKOFP3.js +15 -0
  36. package/dist/{snapshot-SU2VGM4C.js → snapshot-6DFNGXTR.js} +5 -5
  37. package/package.json +1 -1
  38. package/dist/chunk-3QJJRXIR.js.map +0 -1
  39. package/dist/chunk-AT32XCCI.js.map +0 -1
  40. package/dist/chunk-ECXYNTEV.js.map +0 -1
  41. package/dist/chunk-IBBAISSW.js.map +0 -1
  42. package/dist/chunk-WF7ZBW5C.js.map +0 -1
  43. package/dist/chunk-XYBII5PR.js.map +0 -1
  44. package/dist/runtime-W2OWJIHY.js +0 -11
  45. /package/dist/{bili-login-XQVEE4M6.js.map → bili-login-KDDU63Z2.js.map} +0 -0
  46. /package/dist/{bindings-U2M3CO2D.js.map → bindings-LTSGGNIH.js.map} +0 -0
  47. /package/dist/{chunk-LQKOYTRL.js.map → chunk-4OQXAHU5.js.map} +0 -0
  48. /package/dist/{chunk-3DNGY4DS.js.map → chunk-77UYDI2A.js.map} +0 -0
  49. /package/dist/{chunk-M72T5MZU.js.map → chunk-GOLMRQRW.js.map} +0 -0
  50. /package/dist/{chunk-KZLVR4J7.js.map → chunk-IPTNSXOY.js.map} +0 -0
  51. /package/dist/{chunk-DLOYERAK.js.map → chunk-JIRQ36F7.js.map} +0 -0
  52. /package/dist/{chunk-F2XVKX55.js.map → chunk-KGOC6EX4.js.map} +0 -0
  53. /package/dist/{chunk-SIY3YHFY.js.map → chunk-MMI6OBEC.js.map} +0 -0
  54. /package/dist/{chunk-N6SMIGYJ.js.map → chunk-QOLKUYQ3.js.map} +0 -0
  55. /package/dist/{focus-store-RNHBAFYS.js.map → focus-store-XGTPIVF6.js.map} +0 -0
  56. /package/dist/{inbox-ZLX2MH7A.js.map → inbox-7GGEMBUD.js.map} +0 -0
  57. /package/dist/{migrate-XFZU5EBE.js.map → migrate-6HH2ZARV.js.map} +0 -0
  58. /package/dist/{orchestrator-QE2XVVM6.js.map → orchestrator-OJJM5QSV.js.map} +0 -0
  59. /package/dist/{pool-76VLAZER.js.map → pool-QTY2MI56.js.map} +0 -0
  60. /package/dist/{runtime-W2OWJIHY.js.map → runtime-RGZKOFP3.js.map} +0 -0
  61. /package/dist/{snapshot-SU2VGM4C.js.map → snapshot-6DFNGXTR.js.map} +0 -0
@@ -9,9 +9,9 @@ import {
9
9
  decryptContainer,
10
10
  encryptContainer,
11
11
  isMigratablePath
12
- } from "./chunk-AT32XCCI.js";
13
- import "./chunk-M72T5MZU.js";
14
- import "./chunk-IBBAISSW.js";
12
+ } from "./chunk-43Q42HWH.js";
13
+ import "./chunk-GOLMRQRW.js";
14
+ import "./chunk-66IUDHCY.js";
15
15
  export {
16
16
  MIGRATE_FILES,
17
17
  MIGRATE_MAGIC,
@@ -24,4 +24,4 @@ export {
24
24
  encryptContainer,
25
25
  isMigratablePath
26
26
  };
27
- //# sourceMappingURL=migrate-XFZU5EBE.js.map
27
+ //# sourceMappingURL=migrate-6HH2ZARV.js.map
@@ -1,10 +1,12 @@
1
1
  import {
2
2
  BeatAbortedError,
3
3
  DEFAULT_INTERVAL_JITTER_PCT,
4
+ DISPOSE_ABORT_TEXT,
4
5
  ENGINE_ROOM_EXTRA_TOOLS,
5
6
  HOME_ROTATE_EVENT_COUNT,
6
7
  TOKEN_SAVER_IDLE_SECONDS,
7
8
  TOKEN_SAVER_WAKE_MS,
9
+ TURN_END_FAILURE_REASONS,
8
10
  agentLooksAlive,
9
11
  agentTurn,
10
12
  applyHeartbeatInterval,
@@ -25,6 +27,7 @@ import {
25
27
  looksPermanentResumeFailure,
26
28
  makeHomeSetup,
27
29
  markHomeRotateIfOverflow,
30
+ nativeVisionTurn,
28
31
  parseJsonBlock,
29
32
  pauseFocus,
30
33
  pickSpokenLine,
@@ -37,6 +40,7 @@ import {
37
40
  sessionEvents,
38
41
  shouldArchiveRotatedHome,
39
42
  shouldRotateHome,
43
+ silentAuditLine,
40
44
  spokeTextSince,
41
45
  startOrchestrator,
42
46
  throwIfAborted,
@@ -44,23 +48,26 @@ import {
44
48
  userActiveInBoundSessions,
45
49
  waitForVisibleTool,
46
50
  withTimeout
47
- } from "./chunk-WF7ZBW5C.js";
48
- import "./chunk-XYBII5PR.js";
49
- import "./chunk-3QJJRXIR.js";
50
- import "./chunk-N6SMIGYJ.js";
51
- import "./chunk-LQKOYTRL.js";
52
- import "./chunk-KZLVR4J7.js";
53
- import "./chunk-DLOYERAK.js";
54
- import "./chunk-ECXYNTEV.js";
55
- import "./chunk-M72T5MZU.js";
56
- import "./chunk-IBBAISSW.js";
51
+ } from "./chunk-ABFO4QII.js";
52
+ import "./chunk-HSTGRQLP.js";
53
+ import "./chunk-5CVAW5AS.js";
54
+ import "./chunk-QOLKUYQ3.js";
55
+ import "./chunk-77UYDI2A.js";
56
+ import "./chunk-4OQXAHU5.js";
57
+ import "./chunk-IPTNSXOY.js";
58
+ import "./chunk-JIRQ36F7.js";
59
+ import "./chunk-K47VV6IQ.js";
60
+ import "./chunk-GOLMRQRW.js";
61
+ import "./chunk-66IUDHCY.js";
57
62
  export {
58
63
  BeatAbortedError,
59
64
  DEFAULT_INTERVAL_JITTER_PCT,
65
+ DISPOSE_ABORT_TEXT,
60
66
  ENGINE_ROOM_EXTRA_TOOLS,
61
67
  HOME_ROTATE_EVENT_COUNT,
62
68
  TOKEN_SAVER_IDLE_SECONDS,
63
69
  TOKEN_SAVER_WAKE_MS,
70
+ TURN_END_FAILURE_REASONS,
64
71
  agentLooksAlive,
65
72
  agentTurn,
66
73
  applyHeartbeatInterval,
@@ -81,6 +88,7 @@ export {
81
88
  looksPermanentResumeFailure,
82
89
  makeHomeSetup,
83
90
  markHomeRotateIfOverflow,
91
+ nativeVisionTurn,
84
92
  parseJsonBlock,
85
93
  pauseFocus,
86
94
  pickSpokenLine,
@@ -93,6 +101,7 @@ export {
93
101
  sessionEvents,
94
102
  shouldArchiveRotatedHome,
95
103
  shouldRotateHome,
104
+ silentAuditLine,
96
105
  spokeTextSince,
97
106
  startOrchestrator,
98
107
  throwIfAborted,
@@ -101,4 +110,4 @@ export {
101
110
  waitForVisibleTool,
102
111
  withTimeout
103
112
  };
104
- //# sourceMappingURL=orchestrator-QE2XVVM6.js.map
113
+ //# sourceMappingURL=orchestrator-OJJM5QSV.js.map
@@ -20,9 +20,9 @@ import {
20
20
  seedSimilarity,
21
21
  seedsFilePath,
22
22
  surfaceSeed
23
- } from "./chunk-N6SMIGYJ.js";
24
- import "./chunk-M72T5MZU.js";
25
- import "./chunk-IBBAISSW.js";
23
+ } from "./chunk-QOLKUYQ3.js";
24
+ import "./chunk-GOLMRQRW.js";
25
+ import "./chunk-66IUDHCY.js";
26
26
  export {
27
27
  SEED_CATEGORY_CAPS,
28
28
  SEED_SIMILARITY_THRESHOLD,
@@ -46,4 +46,4 @@ export {
46
46
  seedsFilePath,
47
47
  surfaceSeed
48
48
  };
49
- //# sourceMappingURL=pool-76VLAZER.js.map
49
+ //# sourceMappingURL=pool-QTY2MI56.js.map
@@ -0,0 +1,15 @@
1
+ import {
2
+ getCurrentDeliveryId,
3
+ getRuntime,
4
+ resetRuntimeForTest,
5
+ setCurrentDeliveryId,
6
+ setRuntime
7
+ } from "./chunk-5CVAW5AS.js";
8
+ export {
9
+ getCurrentDeliveryId,
10
+ getRuntime,
11
+ resetRuntimeForTest,
12
+ setCurrentDeliveryId,
13
+ setRuntime
14
+ };
15
+ //# sourceMappingURL=runtime-RGZKOFP3.js.map
@@ -3,14 +3,14 @@ import {
3
3
  snapshotDue,
4
4
  snapshotIfDue,
5
5
  snapshotProfile
6
- } from "./chunk-DLOYERAK.js";
7
- import "./chunk-ECXYNTEV.js";
8
- import "./chunk-M72T5MZU.js";
9
- import "./chunk-IBBAISSW.js";
6
+ } from "./chunk-JIRQ36F7.js";
7
+ import "./chunk-K47VV6IQ.js";
8
+ import "./chunk-GOLMRQRW.js";
9
+ import "./chunk-66IUDHCY.js";
10
10
  export {
11
11
  SNAPSHOT_THRESHOLD,
12
12
  snapshotDue,
13
13
  snapshotIfDue,
14
14
  snapshotProfile
15
15
  };
16
- //# sourceMappingURL=snapshot-SU2VGM4C.js.map
16
+ //# sourceMappingURL=snapshot-6DFNGXTR.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kanadego/dsh-heartbeat",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Heartbeat plugin for DeepSeek Harness: scheduled autonomous reflection loop with user profile, material pool, pace gate, and gated proactive expression.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/runtime.ts"],"sourcesContent":["// Runtime singleton shared by all modules after the plugin entry initializes.\n\nimport type { PathGuard } from './path-guard.js';\nimport type { WorkspacePaths } from './paths.js';\nimport type { Policy } from '../config/schema.js';\n\nexport interface HeartbeatRuntime {\n paths: WorkspacePaths;\n guard: PathGuard;\n policy: Policy;\n /** Live UI flags (M7): re-read by the RPC status endpoint for the card. */\n flags: {\n statusbarEnabled(): boolean;\n timeInjectMin(): number;\n idleMode(): boolean;\n /** Token-saver (v1.7.0): pause the whole beat while the user is away. */\n tokenSaver(): boolean;\n };\n}\n\nlet runtime: HeartbeatRuntime | null = null;\n\nexport function setRuntime(r: HeartbeatRuntime): void {\n runtime = r;\n}\n\nexport function getRuntime(): HeartbeatRuntime {\n if (!runtime) throw new Error('heartbeat runtime not initialized');\n return runtime;\n}\n\nexport function resetRuntimeForTest(): void {\n runtime = null;\n}\n"],"mappings":";AAoBA,IAAI,UAAmC;AAEhC,SAAS,WAAW,GAA2B;AACpD,YAAU;AACZ;AAEO,SAAS,aAA+B;AAC7C,MAAI,CAAC,QAAS,OAAM,IAAI,MAAM,mCAAmC;AACjE,SAAO;AACT;AAEO,SAAS,sBAA4B;AAC1C,YAAU;AACZ;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/vault/migrate.ts"],"sourcesContent":["// Cross-machine migration (v1.8.0). DPAPI binds the sensitive stores to one\n// machine + account, so a new machine cannot read them — this module packs\n// the memory files into ONE passphrase-encrypted container (AES-256-GCM,\n// scrypt-derived key, node:crypto only) that can travel safely, and restores\n// them on the other side re-encrypted with the LOCAL DPAPI.\n//\n// Restore is byte-identical: for each file we record whether it was DPAPI\n// encrypted on disk (isEncrypted magic check) and write it back through the\n// same path — no per-module write semantics to keep in sync. Existing files\n// are backed up before overwrite (rule 4).\n\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport {\n createCipheriv,\n createDecipheriv,\n randomBytes,\n scryptSync,\n} from 'node:crypto';\nimport type { PathGuard } from '../core/path-guard.js';\nimport type { WorkspacePaths } from '../core/paths.js';\nimport { isEncrypted, loadEncryptedText, saveEncryptedText } from './vault.js';\n\nexport const MIGRATE_MAGIC = 'HBMIG1';\nexport const MIGRATE_VERSION = 1;\n/** scrypt cost for NEW containers (2^14 = Node's default, 16 MiB of work).\n * Reading is parameterised by the container's own kdf.N, so raising this\n * never orphans an existing export. */\nconst KDF_N = 163_84;\n/** Hard bounds for a container-supplied N: a hostile/broken container must not\n * be able to make the import allocate unbounded memory. */\nconst KDF_N_MIN = 1 << 12; // 2^12 = 4 MiB\nconst KDF_N_MAX = 1 << 16; // 2^16 = 64 MiB\n/** Node's scrypt maxmem defaults to 32 MiB, which N=2^15 already exceeds — a\n * legal container with a larger N would fail without an explicit cap. */\nconst SCRYPT_MAXMEM = 128 * 1024 * 1024;\nconst KEY_LEN = 32;\n\n/** Memory-relevant runtime files (dataDir-relative). Deliberately NOT here:\n * gate.json (machine-local counters + home session id — the new machine\n * builds its own) and screen snapshots (per-beat transients, not memory). */\nexport const MIGRATE_FILES = [\n 'profile.json',\n 'profile_snapshot.json',\n 'profile_inbox.jsonl',\n 'profile_journal.jsonl',\n 'profile_rhythm.json',\n 'sent.json',\n 'browse.json',\n 'seeds.jsonl',\n 'ledger.md',\n 'cursors.json',\n // v1.9.0: persona report log + topic preference stats\n 'seed_report.jsonl',\n 'preference.json',\n];\n\n/** HB-18: an import writes with the user's privileges inside data/, so the set\n * of names a container may touch is closed, not merely sanitised. Anything an\n * export can produce is here; everything else (gate.json, settings/, vault.key,\n * other modules' files) is refused. */\nconst MIGRATE_ARCHIVE_RE = /^profile_journal\\.archive-\\d+\\.jsonl$/;\nconst MIGRATE_WEEKLY_RE = /^weekly\\/[^/\\\\]+\\.json$/;\n\nexport function isMigratablePath(rel: string): boolean {\n if (MIGRATE_FILES.includes(rel)) return true;\n if (MIGRATE_ARCHIVE_RE.test(rel)) return true;\n if (MIGRATE_WEEKLY_RE.test(rel)) return true;\n return false;\n}\n\n/** HB-18: budget for a container-supplied payload. Generous next to a real\n * memory (a few MB), small next to what a hostile container would like. */\nexport const MIGRATE_MAX_FILES = 5_000;\nexport const MIGRATE_MAX_BYTES = 64 * 1024 * 1024;\n\nexport interface MigrateEntry {\n path: string; // dataDir-relative\n encrypted: boolean; // was DPAPI-encrypted on the source machine\n content: string;\n}\n\nexport interface MigrateProgress {\n packed: string[];\n missing: string[];\n}\n\nfunction collectWeeklyFiles(guard: PathGuard, paths: WorkspacePaths): string[] {\n const dir = path.join(paths.dataDir, 'weekly');\n try {\n return fs.readdirSync(guard.assert(dir))\n .filter((f) => f.endsWith('.json'))\n .map((f) => path.join('weekly', f).replace(/\\\\/g, '/'));\n } catch {\n return [];\n }\n}\n\n/** Journal archive shards (snapshot rotation output) travel with the memory. */\nfunction collectJournalArchives(guard: PathGuard, paths: WorkspacePaths): string[] {\n try {\n return fs.readdirSync(guard.assert(paths.dataDir))\n .filter((f) => f.startsWith('profile_journal.archive-') && f.endsWith('.jsonl'));\n } catch {\n return [];\n }\n}\n\n/** Read every migration file. A DPAPI file that fails to decrypt aborts the\n * export — a silent half-memory container would be worse than a loud error. */\nexport function collectMigrationEntries(guard: PathGuard, paths: WorkspacePaths): { entries: MigrateEntry[]; progress: MigrateProgress } {\n const entries: MigrateEntry[] = [];\n const packed: string[] = [];\n const missing: string[] = [];\n const rels = [...MIGRATE_FILES, ...collectJournalArchives(guard, paths), ...collectWeeklyFiles(guard, paths)];\n for (const rel of rels) {\n const abs = path.join(paths.dataDir, rel);\n let absCanon: string;\n try {\n absCanon = guard.assert(abs);\n } catch {\n missing.push(rel);\n continue;\n }\n if (!fs.existsSync(absCanon)) {\n missing.push(rel);\n continue;\n }\n const encrypted = isEncrypted(absCanon);\n const content = encrypted\n ? loadEncryptedText(guard, absCanon) ?? (() => { throw new Error(`cannot decrypt ${rel} on this machine`); })()\n : fs.readFileSync(absCanon, 'utf8');\n entries.push({ path: rel.replace(/\\\\/g, '/'), encrypted, content });\n packed.push(rel);\n }\n return { entries, progress: { packed, missing } };\n}\n\n/** Derive the key with the container's OWN cost parameter. `n` must already\n * have passed assertKdfN (export uses the current default). */\nfunction deriveKey(passphrase: string, salt: Buffer, n: number): Buffer {\n return scryptSync(passphrase, salt, KEY_LEN, { N: n, r: 8, p: 1, maxmem: SCRYPT_MAXMEM });\n}\n\n/** Validate the scrypt cost carried by a container. A missing/absurd N is a\n * BROKEN container — reported as such, never as \"wrong passphrase\" (before\n * 2026-10-06 the N in the container was ignored and the two were the same\n * error, so raising the cost would have orphaned every old export). */\nfunction assertKdfN(v: unknown): number {\n const n = Number(v);\n if (!Number.isInteger(n) || n <= 0 || (n & (n - 1)) !== 0) {\n throw new Error('migrate: container has invalid kdf parameters (N must be a power of two)');\n }\n if (n < KDF_N_MIN || n > KDF_N_MAX) {\n throw new Error(`migrate: container kdf N ${n} is outside the supported range [${KDF_N_MIN}, ${KDF_N_MAX}]`);\n }\n return n;\n}\n\n/** HB-18: a container does not get to decide how much work the import does. */\nfunction assertMigrateBudget(files: unknown[]): asserts files is MigrateEntry[] {\n if (files.length > MIGRATE_MAX_FILES) {\n throw new Error(`migrate: container lists ${files.length} files, over the ${MIGRATE_MAX_FILES} limit`);\n }\n let bytes = 0;\n for (const f of files) {\n const entry = f as { path?: unknown; content?: unknown } | null;\n if (!entry || typeof entry.path !== 'string' || typeof entry.content !== 'string') {\n throw new Error('migrate: container file entry is malformed');\n }\n bytes += Buffer.byteLength(entry.content, 'utf8');\n if (bytes > MIGRATE_MAX_BYTES) {\n throw new Error(`migrate: container payload exceeds ${MIGRATE_MAX_BYTES} bytes`);\n }\n }\n}\n\n/** Build the container file content. Everything sensitive lives inside the\n * ciphertext; the outer JSON carries only crypto parameters. */\nexport function encryptContainer(entries: MigrateEntry[], passphrase: string, now = new Date()): string {\n if (!passphrase) throw new Error('migrate: passphrase must not be empty');\n const salt = randomBytes(16);\n const iv = randomBytes(12);\n const key = deriveKey(passphrase, salt, KDF_N);\n const cipher = createCipheriv('aes-256-gcm', key, iv);\n const plain = Buffer.from(JSON.stringify({ version: MIGRATE_VERSION, createdAt: now.toISOString(), files: entries }), 'utf8');\n const data = Buffer.concat([cipher.update(plain), cipher.final()]);\n const tag = cipher.getAuthTag();\n return JSON.stringify({\n magic: MIGRATE_MAGIC,\n kdf: { name: 'scrypt', salt: salt.toString('hex'), N: KDF_N },\n iv: iv.toString('hex'),\n tag: tag.toString('hex'),\n data: data.toString('base64'),\n }, null, 1) + '\\n';\n}\n\n/** Parse + decrypt a container. Throws (loudly) on a wrong passphrase or a\n * corrupted file — never returns partial data. */\nexport function decryptContainer(text: string, passphrase: string): { createdAt: string; files: MigrateEntry[] } {\n let outer: { magic?: string; kdf?: { salt?: string; N?: unknown }; iv?: string; tag?: string; data?: string };\n try {\n outer = JSON.parse(text);\n } catch {\n throw new Error('migrate: not a migration container (bad JSON)');\n }\n if (outer.magic !== MIGRATE_MAGIC) throw new Error('migrate: not a migration container (bad magic)');\n const n = assertKdfN(outer.kdf?.N);\n const salt = Buffer.from(String(outer.kdf?.salt ?? ''), 'hex');\n const iv = Buffer.from(String(outer.iv ?? ''), 'hex');\n const tag = Buffer.from(String(outer.tag ?? ''), 'hex');\n // HB-18: refuse an oversized payload before decoding it. base64 runs ~4/3 of\n // the ciphertext, so this cheap length check bounds the allocation below.\n const dataB64 = String(outer.data ?? '');\n if (dataB64.length > MIGRATE_MAX_BYTES * 2) {\n throw new Error(`migrate: container payload is too large (${dataB64.length} base64 chars)`);\n }\n const data = Buffer.from(dataB64, 'base64');\n const key = deriveKey(passphrase, salt, n);\n const decipher = createDecipheriv('aes-256-gcm', key, iv);\n decipher.setAuthTag(tag);\n let plain: Buffer;\n try {\n plain = Buffer.concat([decipher.update(data), decipher.final()]);\n } catch {\n throw new Error('migrate: wrong passphrase or corrupted container');\n }\n const inner = JSON.parse(plain.toString('utf8')) as { version: number; createdAt: string; files: MigrateEntry[] };\n if (inner.version !== MIGRATE_VERSION) throw new Error(`migrate: unsupported container version ${inner.version}`);\n if (!Array.isArray(inner.files)) throw new Error('migrate: container has no file list');\n assertMigrateBudget(inner.files);\n return { createdAt: inner.createdAt, files: inner.files };\n}\n\nexport interface MigrateImportResult {\n restored: string[];\n backedUp: string[];\n skipped: string[];\n /** H-21: files put back from their backups after a failed commit phase. */\n rolledBack?: string[];\n /** H-21: set when the commit phase failed and the import was rolled back. */\n error?: string;\n}\n\n/**\n * Restore entries into the local dataDir, re-encrypting DPAPI files with the\n * LOCAL machine key.\n *\n * H-21: this used to be a per-file \"back up, then overwrite\" loop with no\n * pre-flight and no rollback — a failure halfway through left a mixed memory\n * (a new `profile.json` next to a stale `profile_journal.jsonl`). Now it runs\n * in two phases: stage every entry next to its target (nothing visible changes\n * yet), then commit each one by rename. A commit failure rolls the\n * already-committed files back from their backups.\n *\n * HB-06 (2026-10-17): the phases are all-or-nothing at BOTH ends. Earlier a\n * staging failure only skipped that entry and the rest were still committed,\n * so one bad path was enough to leave a half-migrated memory. Now any skipped\n * entry aborts the whole import with nothing written; the reason is returned\n * in `error` alongside the offending names.\n */\nexport function applyMigrationEntries(guard: PathGuard, paths: WorkspacePaths, files: MigrateEntry[], now = Date.now()): MigrateImportResult {\n const restored: string[] = [];\n const backedUp: string[] = [];\n const skipped: string[] = [];\n const stamp = new Date(now).toISOString().replace(/[-:T]/g, '').slice(0, 14);\n const staged: Array<{ rel: string; abs: string; incoming: string }> = [];\n for (const f of files) {\n const rel = String(f.path ?? '');\n // String-level rejection BEFORE the path is resolved: the guard's realpath\n // check already refuses escapes, but an import must not depend on another\n // module staying correct. HB-18: and the name must be one an export can\n // produce - a container may not reach settings/, gate.json or vault.key.\n if (!rel || rel.includes('..') || path.isAbsolute(rel) || !isMigratablePath(rel)) {\n skipped.push(rel);\n continue;\n }\n const abs = path.join(paths.dataDir, rel);\n let absCanon: string;\n try {\n absCanon = guard.assert(abs);\n } catch {\n skipped.push(rel); // path refused by the guard — never write it\n continue;\n }\n const incoming = `${absCanon}.incoming-${stamp}`;\n try {\n fs.mkdirSync(path.dirname(absCanon), { recursive: true });\n if (f.encrypted) saveEncryptedText(guard, incoming, f.content);\n else fs.writeFileSync(incoming, f.content, 'utf8');\n staged.push({ rel, abs: absCanon, incoming });\n } catch {\n skipped.push(rel);\n try {\n fs.rmSync(incoming, { force: true });\n } catch {\n /* nothing staged */\n }\n }\n }\n // HB-06: staging is the point of no return. A single entry that could not be\n // staged used to be skipped while the rest were committed, which left a\n // half-migrated memory (a new profile.json next to a stale journal). Any skip\n // now aborts the import with nothing written; the reason travels in `error`.\n if (skipped.length > 0) {\n for (const s of staged) {\n try {\n fs.rmSync(s.incoming, { force: true });\n } catch {\n /* nothing staged */\n }\n }\n return {\n restored: [],\n backedUp: [],\n skipped,\n error: `staging failed for ${skipped.length} file(s): ${skipped.join(', ')}; nothing was imported`,\n };\n }\n const committed: Array<{ rel: string; abs: string; backup: string | null }> = [];\n try {\n for (const s of staged) {\n let backup: string | null = null;\n if (fs.existsSync(s.abs)) {\n backup = `${s.abs}.bak-migrate-${stamp}`;\n fs.copyFileSync(s.abs, backup);\n backedUp.push(s.rel);\n }\n fs.renameSync(s.incoming, s.abs);\n committed.push({ rel: s.rel, abs: s.abs, backup });\n restored.push(s.rel);\n }\n return { restored, backedUp, skipped };\n } catch (e) {\n // Roll back so the dataDir never holds a half-migrated mix; staged files\n // that were never committed are simply dropped.\n const rolledBack: string[] = [];\n for (const c of committed.reverse()) {\n try {\n if (c.backup) fs.renameSync(c.backup, c.abs);\n else fs.rmSync(c.abs, { force: true });\n rolledBack.push(c.rel);\n } catch {\n /* best effort — the backup file itself is still on disk */\n }\n }\n for (const s of staged) {\n if (committed.some((c) => c.abs === s.abs)) continue;\n try {\n fs.rmSync(s.incoming, { force: true });\n } catch {\n /* already gone */\n }\n }\n return { restored: [], backedUp, skipped, rolledBack, error: String(e) };\n }\n}\n"],"mappings":";;;;;;;AAWA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAKA,IAAM,gBAAgB;AACtB,IAAM,kBAAkB;AAI/B,IAAM,QAAQ;AAGd,IAAM,YAAY,KAAK;AACvB,IAAM,YAAY,KAAK;AAGvB,IAAM,gBAAgB,MAAM,OAAO;AACnC,IAAM,UAAU;AAKT,IAAM,gBAAgB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAEA;AAAA,EACA;AACF;AAMA,IAAM,qBAAqB;AAC3B,IAAM,oBAAoB;AAEnB,SAAS,iBAAiB,KAAsB;AACrD,MAAI,cAAc,SAAS,GAAG,EAAG,QAAO;AACxC,MAAI,mBAAmB,KAAK,GAAG,EAAG,QAAO;AACzC,MAAI,kBAAkB,KAAK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,EAAE,KAAK,OAAO,CAAC;AACjD,eAAS,KAAK,EAAE,GAAG;AAAA,IACrB;AACA,WAAO,EAAE,UAAU,UAAU,QAAQ;AAAA,EACvC,SAAS,GAAG;AAGV,UAAM,aAAuB,CAAC;AAC9B,eAAW,KAAK,UAAU,QAAQ,GAAG;AACnC,UAAI;AACF,YAAI,EAAE,OAAQ,IAAG,WAAW,EAAE,QAAQ,EAAE,GAAG;AAAA,YACtC,IAAG,OAAO,EAAE,KAAK,EAAE,OAAO,KAAK,CAAC;AACrC,mBAAW,KAAK,EAAE,GAAG;AAAA,MACvB,QAAQ;AAAA,MAER;AAAA,IACF;AACA,eAAW,KAAK,QAAQ;AACtB,UAAI,UAAU,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAG;AAC5C,UAAI;AACF,WAAG,OAAO,EAAE,UAAU,EAAE,OAAO,KAAK,CAAC;AAAA,MACvC,QAAQ;AAAA,MAER;AAAA,IACF;AACA,WAAO,EAAE,UAAU,CAAC,GAAG,UAAU,SAAS,YAAY,OAAO,OAAO,CAAC,EAAE;AAAA,EACzE;AACF;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/audit-log.ts","../src/profile/store.ts","../src/profile/types.ts","../src/profile/schema.ts","../src/profile/shapes.ts"],"sourcesContent":["// Append-only JSONL audit log with age-based retention pruning.\n// Audit files are plaintext by charter (transparency), and must never contain\n// sensitive raw observations (window titles, conversation text).\n\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport { atomicWriteFileSync } from './atomic-fs.js';\n\nexport interface AuditEvent {\n ts: string;\n [key: string]: unknown;\n}\n\n/**\n * H-15: audit is best-effort by contract — a full disk, a locked log file or an\n * unserialisable payload must never abort a beat (one caller inside a catch\n * block turning a handled failure into an unhandled one is the worst case).\n * Failures go to stderr so they stay visible instead of silently vanishing.\n */\nexport function appendAuditLine(file: string, event: Omit<AuditEvent, 'ts'> & { ts?: string }): void {\n try {\n fs.mkdirSync(path.dirname(file), { recursive: true });\n const line = JSON.stringify({ ts: event.ts ?? new Date().toISOString(), ...event });\n fs.appendFileSync(file, line + '\\n', 'utf8');\n } catch (e) {\n process.stderr.write(`[heartbeat] audit append failed: ${file}: ${String(e)}\\n`);\n }\n}\n\nexport function readAuditLines<T = AuditEvent>(file: string): T[] {\n if (!fs.existsSync(file)) return [];\n const out: T[] = [];\n const raw = fs.readFileSync(file, 'utf8');\n for (const line of raw.split('\\n')) {\n const trimmed = line.trim();\n if (!trimmed) continue;\n try {\n out.push(JSON.parse(trimmed) as T);\n } catch {\n // Skip corrupt lines; the log is diagnostic, not authoritative data.\n out.push({ ts: '', corrupt: true, raw: trimmed.slice(0, 200) } as unknown as T);\n }\n }\n return out;\n}\n\n/**\n * Drop entries older than maxAgeMs. Returns the number of removed lines.\n * Rewrites the file atomically; on rewrite failure the original is untouched.\n */\nexport function pruneAuditFile(file: string, maxAgeMs: number, now = Date.now()): number {\n if (!fs.existsSync(file)) return 0;\n const lines = readAuditLines(file);\n const kept = lines.filter((e) => {\n const ev = e as unknown as AuditEvent;\n const ts = Date.parse(ev.ts ?? '');\n if (!Number.isFinite(ts)) return true; // keep unparseable lines, never lose audit data silently\n return now - ts <= maxAgeMs;\n });\n const removed = lines.length - kept.length;\n if (removed === 0) return 0;\n const body = kept.map((e) => JSON.stringify(e)).join('\\n');\n atomicWriteFileSync(file, body ? body + '\\n' : '');\n return removed;\n}\n","// Profile store: materialized view + journal (the journal is the ONLY\n// authority; profile.json is a pure projection - r2 §14 verify/rebuild).\n// Every LLM-proposed op passes deterministic guards here (LLM nominates,\n// code decides - §3.3).\n\nimport path from 'node:path';\nimport { randomUUID } from 'node:crypto';\nimport fs from 'node:fs';\nimport type { PathGuard } from '../core/path-guard.js';\nimport { loadJson, saveJson, readText, writeText } from '../vault/vault.js';\nimport type { Policy } from '../config/schema.js';\nimport {\n CONFIDENCE_CAP,\n emptyProfile,\n JOURNAL_VERSION,\n PARTITIONS,\n type ApplyReport,\n type Evidence,\n type EvidenceKind,\n type InboxItem,\n type JournalRecord,\n type Partition,\n type ProfileDoc,\n type ProfileEntry,\n type ProfileOp,\n} from './types.js';\nimport { checkAddAgainstSchema, type ProfileSchema } from './schema.js';\nimport { tryParseJournalRecord } from './shapes.js';\nimport { matchFilesRecursive } from '../core/fs-walk.js';\n\nconst DAY_MS = 86_400_000;\n\nexport function profileFilePath(dataDir: string): string {\n return path.join(dataDir, 'profile.json');\n}\n\nexport function journalFilePath(dataDir: string): string {\n return path.join(dataDir, 'profile_journal.jsonl');\n}\n\n// ── snapshot basepoint (v1.8.0, design doc §14 ③) ─────────────────────────\n// The journal grows forever; the snapshot folds its history into one DPAPI\n// document and moves the folded records into a timestamped archive shard.\n// Replay order: snapshot → archives (name order) → live journal, skipping\n// records at or before the snapshot baseline — so verify/rebuild stay correct\n// without knowing whether a snapshot exists.\n\nexport const SNAPSHOT_FILE = 'profile_snapshot.json';\nexport const ARCHIVE_PREFIX = 'profile_journal.archive-';\n\nexport interface ProfileSnapshot {\n version: 1;\n /** ts of the LAST journal record folded into `doc`. */\n baselineTs: string;\n recordsFolded: number;\n doc: ProfileDoc;\n}\n\nexport function snapshotFilePath(dataDir: string): string {\n return path.join(dataDir, SNAPSHOT_FILE);\n}\n\nfunction loadSnapshot(guard: PathGuard, dataDir: string): ProfileSnapshot | null {\n const snap = loadJson<ProfileSnapshot>(guard, snapshotFilePath(dataDir));\n if (!snap || snap.version !== 1 || typeof snap.baselineTs !== 'string' || !snap.doc?.partitions) return null;\n return snap;\n}\n\nfunction listArchiveFiles(guard: PathGuard, dataDir: string): string[] {\n try {\n return fs.readdirSync(guard.assert(dataDir))\n .filter((f) => f.startsWith(ARCHIVE_PREFIX) && f.endsWith('.jsonl'))\n .sort(); // timestamped names sort chronologically\n } catch {\n return [];\n }\n}\n\n/** Replay one JSONL text into `doc`, skipping records at/before the baseline. */\nfunction replayText(\n doc: ProfileDoc,\n text: string,\n baselineTs: string,\n stats: { records: number; truncatedTail: number; badRecords: number; isLive: boolean },\n): void {\n const lines = text.split('\\n');\n for (let i = 0; i < lines.length; i++) {\n const trimmed = lines[i]!.trim();\n if (!trimmed) continue;\n // A torn tail only counts on the LIVE journal — archives are closed files.\n const tornTail = (): boolean => {\n const isLast = lines.slice(i + 1).every((l) => !l.trim());\n if (stats.isLive && isLast) {\n stats.truncatedTail = lines.length - i;\n return true;\n }\n return false;\n };\n let rec: JournalRecord | null = null;\n try {\n rec = tryParseJournalRecord(JSON.parse(trimmed));\n } catch {\n rec = null;\n }\n if (!rec) {\n if (tornTail()) break;\n // mid-file unreadable record: count it and keep going (the journal is\n // append-only/immutable, so a bad line is never rewritten).\n stats.badRecords += 1;\n continue;\n }\n // Baseline skip: only records CARRYING a ts can be proven folded. A\n // ts-less record is always applied (applyOpPermissive tolerates it).\n if (baselineTs && rec.ts && String(rec.ts) <= baselineTs) continue;\n try {\n if (rec.v === JOURNAL_VERSION && rec.entries) {\n // v2 (③A): the record carries each touched entry's FULL final state,\n // so replay is an upsert; live and replay share one state machine (HB-09).\n for (const entry of rec.entries) upsertEntry(doc, entry);\n } else {\n for (const op of rec.applied ?? []) applyOpPermissive(doc, op, rec.ts);\n }\n stats.records += 1;\n } catch {\n stats.badRecords += 1;\n }\n }\n}\n\nfunction bucketFor(doc: ProfileDoc, partition: Partition): { entries: ProfileEntry[] } {\n let bucket = doc.partitions[partition];\n if (!bucket) {\n bucket = { entries: [] };\n doc.partitions[partition] = bucket;\n }\n return bucket;\n}\n\n/** Journal v2 replay: `entry` is the entry's final state, so replay is an\n * upsert by id (position preserved for updates, appended for new entries). */\nfunction upsertEntry(doc: ProfileDoc, entry: ProfileEntry): void {\n const next = structuredClone(entry);\n for (const p of PARTITIONS) {\n const list = doc.partitions[p]?.entries;\n if (!list) continue;\n const at = list.findIndex((e) => e.id === next.id);\n if (at < 0) continue;\n if (p === next.partition) {\n list[at] = next;\n } else {\n list.splice(at, 1);\n bucketFor(doc, next.partition).entries.push(next);\n }\n return;\n }\n bucketFor(doc, next.partition).entries.push(next);\n}\n\n/** Full replay: snapshot basepoint + archive shards + live journal. */\nexport function replayJournal(guard: PathGuard, dataDir: string): ReplayResult {\n const snap = loadSnapshot(guard, dataDir);\n const doc = snap ? snap.doc : emptyProfile();\n const baselineTs = snap?.baselineTs ?? '';\n const stats = { records: snap?.recordsFolded ?? 0, truncatedTail: 0, badRecords: 0, isLive: false };\n for (const file of listArchiveFiles(guard, dataDir)) {\n try {\n // shard record counts are not reported (the snapshot already folded\n // them); only the live journal's torn tail is meaningful.\n replayText(doc, fs.readFileSync(path.join(dataDir, file), 'utf8'), baselineTs, { records: 0, truncatedTail: 0, badRecords: 0, isLive: false });\n } catch { /* unreadable shard: skip */ }\n }\n stats.isLive = true;\n replayText(doc, readText(guard, journalFilePath(dataDir), ''), baselineTs, stats);\n return { doc, truncatedTail: stats.truncatedTail, records: stats.records, badRecords: stats.badRecords };\n}\n\n/** Every plaintext journal text in chronological order: archive shards first,\n * then the live journal — the RAW record timeline, for callers that need the\n * history itself rather than the accumulated doc (the weekly report counts\n * the week's ADD ops). A rotation must not hide history from them (H-01). */\nexport function readJournalTexts(guard: PathGuard, dataDir: string): string[] {\n const texts: string[] = [];\n for (const file of listArchiveFiles(guard, dataDir)) {\n try {\n texts.push(fs.readFileSync(path.join(dataDir, file), 'utf8'));\n } catch { /* unreadable shard: skip */ }\n }\n texts.push(readText(guard, journalFilePath(dataDir), ''));\n return texts;\n}\n\nexport function loadProfile(guard: PathGuard, file: string): ProfileDoc {\n const doc = loadJson<ProfileDoc>(guard, file);\n if (!doc || !doc.partitions) return emptyProfile();\n // ensure all partitions exist\n for (const p of PARTITIONS) {\n if (!doc.partitions[p]) doc.partitions[p] = { entries: [] };\n }\n return doc;\n}\n\nfunction parseIso(v: string): number {\n const t = Date.parse(v);\n return Number.isFinite(t) ? t : 0;\n}\n\n/** ref format \"<datafile>#<locator>\": the referenced file must exist under data/. */\nfunction refExists(guard: PathGuard, dataDir: string, ref: string): boolean {\n const base = ref.split('#')[0] ?? '';\n if (!base) return false;\n const target = path.join(dataDir, base);\n try {\n return fs.existsSync(guard.assert(target));\n } catch {\n return false;\n }\n}\n\nfunction capForKinds(kinds: EvidenceKind[]): number {\n if (kinds.length === 0) return 0.4;\n return Math.min(...kinds.map((k) => CONFIDENCE_CAP[k] ?? 0.4));\n}\n\nfunction findActive(doc: ProfileDoc, id: string): ProfileEntry | undefined {\n for (const p of PARTITIONS) {\n const hit = doc.partitions[p]!.entries.find((e) => e.id === id && e.validTo === null);\n if (hit) return hit;\n }\n return undefined;\n}\n\n/**\n * Apply validated ops to the doc (mutates). Returns applied/rejected.\n * Guards (§3.3): whitelist, evidence gate, per-run ops cap is enforced by the\n * caller, confidence caps by evidence kind, psy gating, INVALIDATE ownership:\n * volatile expiry is code-driven (LLM may not), stable invalidation needs a\n * contradicting observation attached.\n */\nexport function applyOpsToDoc(\n guard: PathGuard,\n dataDir: string,\n doc: ProfileDoc,\n ops: ProfileOp[],\n schema: ProfileSchema | undefined,\n policy: Policy,\n now: number,\n): ApplyReport {\n const applied: ProfileOp[] = [];\n const rejected: { op: ProfileOp; reason: string }[] = [];\n // Journal v2 events: one FULL snapshot per touched entry (HB-09).\n const entries: ProfileEntry[] = [];\n const nowIso = new Date(now).toISOString();\n\n for (const op of ops) {\n if (op.op === 'NOOP') {\n applied.push(op);\n continue;\n }\n if (op.op === 'ADD') {\n if (op.partition === 'psy' && !policy.profile.psyEnabled) {\n rejected.push({ op, reason: 'psy partition is disabled' });\n continue;\n }\n let temporal: 'stable' | 'volatile' = 'stable';\n if (schema) {\n const check = checkAddAgainstSchema(schema, op.partition, op.topic, op.subTopic, op.temporal);\n if (!check.ok) {\n rejected.push({ op, reason: check.reason! });\n continue;\n }\n temporal = check.temporal;\n } else if (!(op.partition in doc.partitions)) {\n // H-17: no usable schema — the partition set is the only boundary left.\n rejected.push({ op, reason: `unknown partition: ${op.partition}` });\n continue;\n } else {\n temporal = op.temporal === 'volatile' ? 'volatile' : 'stable';\n }\n if (!op.evidence || op.evidence.length === 0) {\n rejected.push({ op, reason: 'ADD without evidence (no provenance, axiom 1)' });\n continue;\n }\n const badRef = op.evidence.find((e) => !refExists(guard, dataDir, e.ref));\n if (badRef) {\n rejected.push({ op, reason: `evidence ref does not resolve: ${badRef.ref}` });\n continue;\n }\n const cap = capForKinds(op.evidence.map((e) => e.kind));\n const active = doc.partitions[op.partition]!.entries.filter((e) => e.validTo === null);\n if (active.length >= policy.profile.partitionCap) {\n rejected.push({ op, reason: `partition ${op.partition} at cap (${policy.profile.partitionCap}); converge first` });\n continue;\n }\n dbSeq += 1;\n const entry: ProfileEntry = {\n id: `p${dbSeq.toString(36)}${randomUUID().slice(0, 4)}`,\n partition: op.partition,\n topic: op.topic,\n subTopic: op.subTopic,\n content: op.content.trim(),\n confidence: Math.min(op.confidence ?? cap, cap),\n temporal,\n validFrom: nowIso,\n validTo: null,\n supersededBy: null,\n evidence: op.evidence,\n createdAt: nowIso,\n updatedAt: nowIso,\n updateCount: 0,\n };\n op.assignedId = entry.id; // journal replay must reproduce this id\n doc.partitions[op.partition]!.entries.push(entry);\n entries.push(structuredClone(entry));\n applied.push(op);\n continue;\n }\n if (op.op === 'UPDATE') {\n const entry = findActive(doc, op.id);\n if (!entry) {\n rejected.push({ op, reason: `unknown or inactive entry: ${op.id}` });\n continue;\n }\n // HB-04 (2026-10-17): validate BEFORE mutating. The old order wrote\n // `content` first and then bailed out on the confidence rule, so an\n // UPDATE that was reported as REJECTED had already changed the text.\n let nextConfidence: number | null = null;\n if (op.changes.confidence !== undefined) {\n const cap = capForKinds(entry.evidence.map((e) => e.kind));\n // confidence upgrades need a second confirming observation (§3.3):\n // only allowed up to cap, and only when the entry already has 2+ evidence\n if (op.changes.confidence > entry.confidence && entry.evidence.length < 2) {\n rejected.push({ op, reason: 'confidence upgrade requires a second confirming observation' });\n continue;\n }\n nextConfidence = Math.min(op.changes.confidence, cap);\n }\n if (op.changes.content !== undefined) entry.content = op.changes.content.trim();\n if (nextConfidence !== null) entry.confidence = nextConfidence;\n entry.updatedAt = nowIso;\n entry.updateCount += 1;\n entries.push(structuredClone(entry));\n applied.push(op);\n continue;\n }\n if (op.op === 'INVALIDATE') {\n const entry = findActive(doc, op.id);\n if (!entry) {\n rejected.push({ op, reason: `unknown or inactive entry: ${op.id}` });\n continue;\n }\n if (entry.temporal === 'volatile') {\n rejected.push({ op, reason: 'volatile expiry is code-owned (time-driven), not LLM-nominated' });\n continue;\n }\n // stable: rebuttal-driven; requires a contradicting observation attached\n const hasNewObservation = (op.evidence ?? []).length > 0\n && (op.evidence ?? []).some((e) => parseIso(e.at) > parseIso(entry.evidence[entry.evidence.length - 1]?.at ?? ''));\n if (!hasNewObservation) {\n rejected.push({ op, reason: 'stable INVALIDATE requires a newer contradicting observation' });\n continue;\n }\n entry.validTo = nowIso;\n entry.supersededBy = null;\n entry.updatedAt = nowIso;\n entry.updateCount += 1;\n if (op.evidence) entry.evidence.push(...op.evidence);\n entries.push(structuredClone(entry));\n applied.push(op);\n continue;\n }\n }\n return { applied, rejected, entries };\n}\n\nlet dbSeq = 0;\n\n/** Deterministic aging (D9): volatile expiry + stable low-activity marking. */\nexport function runDeterministicAging(\n doc: ProfileDoc,\n policy: Policy,\n now: number,\n): { volatileExpired: number; lowActivityMarked: number; entries: ProfileEntry[] } {\n const nowIso = new Date(now).toISOString();\n const entries: ProfileEntry[] = [];\n let volatileExpired = 0;\n let lowActivityMarked = 0;\n for (const p of PARTITIONS) {\n for (const e of doc.partitions[p]!.entries) {\n if (e.validTo !== null) continue;\n const lastEvidence = Math.max(...e.evidence.map((x) => parseIso(x.at)), parseIso(e.updatedAt));\n if (e.temporal === 'volatile') {\n if (now - lastEvidence > policy.profile.volatileDays * DAY_MS) {\n e.validTo = nowIso;\n e.updatedAt = nowIso;\n e.updateCount += 1;\n volatileExpired += 1;\n entries.push(structuredClone(e));\n }\n } else if (!e.lowActivity && now - lastEvidence > policy.profile.stableLowActivityDays * DAY_MS) {\n e.lowActivity = true;\n lowActivityMarked += 1;\n entries.push(structuredClone(e));\n }\n }\n }\n return { volatileExpired, lowActivityMarked, entries };\n}\n\n/** Persist the materialized view atomically + append the journal record.\n * A record carrying `entries` (every touched entry's final state) is written\n * as v2 so replay is an upsert; a record without them stays v1 and replays\n * permissively, so old journals keep working unchanged. */\nexport function persistWithJournal(\n guard: PathGuard,\n dataDir: string,\n doc: ProfileDoc,\n record: Omit<JournalRecord, 'ts'>,\n): void {\n saveJson(guard, profileFilePath(dataDir), doc);\n const v = record.entries === undefined ? undefined : JOURNAL_VERSION;\n const line = JSON.stringify({ ts: new Date().toISOString(), ...record, v });\n const journal = journalFilePath(dataDir);\n try {\n fs.appendFileSync(guard.assert(journal), line + '\\n', 'utf8');\n } catch {\n fs.mkdirSync(dataDir, { recursive: true });\n fs.appendFileSync(guard.assert(journal), line + '\\n', 'utf8');\n }\n}\n\n// ── journal replay (verify / rebuild, r2 §14) ───────────────────────────\n\nfunction applyOpPermissive(doc: ProfileDoc, op: ProfileOp, ts: string): void {\n // Journal is trusted history: replay applies without revalidation.\n if (op.op === 'ADD') {\n dbSeq += 1;\n doc.partitions[op.partition]!.entries.push({\n id: op.assignedId ?? `r${dbSeq.toString(36)}${randomUUID().slice(0, 4)}`,\n partition: op.partition,\n topic: op.topic,\n subTopic: op.subTopic,\n content: op.content,\n confidence: op.confidence ?? 0.5,\n temporal: op.temporal ?? 'stable',\n validFrom: ts,\n validTo: null,\n supersededBy: null,\n evidence: op.evidence,\n createdAt: ts,\n updatedAt: ts,\n updateCount: 0,\n });\n return;\n }\n if (op.op === 'UPDATE') {\n const e = [...PARTITIONS].flatMap((p) => doc.partitions[p]!.entries).find((x) => x.id === op.id);\n if (e) {\n if (op.changes.content !== undefined) e.content = op.changes.content;\n if (op.changes.confidence !== undefined) e.confidence = op.changes.confidence;\n e.updatedAt = ts;\n e.updateCount += 1;\n }\n return;\n }\n if (op.op === 'INVALIDATE') {\n const e = [...PARTITIONS].flatMap((p) => doc.partitions[p]!.entries).find((x) => x.id === op.id);\n if (e) {\n e.validTo = ts;\n e.updatedAt = ts;\n e.updateCount += 1;\n }\n }\n // NOOP: nothing\n}\n\nexport interface ReplayResult {\n doc: ProfileDoc;\n truncatedTail: number;\n records: number;\n /** Journal lines that neither parsed nor replayed (counted, never fatal). */\n badRecords?: number;\n}\n\nexport interface VerifyReport {\n ok: boolean;\n firstDivergence?: { id: string; expected: string; actual: string };\n truncatedTail: number;\n records: number;\n}\n\n/** profile verify: replay vs disk, report first divergence, never fix. */\nexport function verifyProfile(guard: PathGuard, dataDir: string): VerifyReport {\n const replayed = replayJournal(guard, dataDir);\n const onDisk = loadProfile(guard, profileFilePath(dataDir));\n // Timestamps legitimately differ (journal record ts >= op ts); identity\n // fields are also normalized. Compare semantic content only.\n const strip = (doc: ProfileDoc): string =>\n JSON.stringify(doc.partitions, (k, v) => (['id', 'supersededBy', 'validFrom', 'createdAt', 'updatedAt', 'retiredAt'].includes(k) ? '<norm>' : v));\n const ok = strip(replayed.doc) === strip(onDisk);\n if (ok) return { ok: true, truncatedTail: replayed.truncatedTail, records: replayed.records };\n // first divergence: first entry id present in only one view\n const diskIds = new Set([...PARTITIONS].flatMap((p) => onDisk.partitions[p]!.entries.map((e) => e.content))); const replayIds = new Set([...PARTITIONS].flatMap((p) => replayed.doc.partitions[p]!.entries.map((e) => e.content)));\n const onlyDisk = [...diskIds].find((c) => !replayIds.has(c));\n const onlyReplay = [...replayIds].find((c) => !diskIds.has(c));\n return {\n ok: false,\n firstDivergence: {\n id: onlyDisk ?? onlyReplay ?? '(content)',\n expected: onlyReplay ? 'absent in journal replay' : 'present in journal replay',\n actual: onlyDisk ? 'present on disk' : 'absent on disk',\n },\n truncatedTail: replayed.truncatedTail,\n records: replayed.records,\n };\n}\n\nexport interface RebuildReport {\n ok: boolean;\n truncatedTail: number;\n records: number;\n wrote: boolean;\n}\n\n/** profile rebuild: journal is authoritative; atomic replace; torn tail reported. */\nexport function rebuildProfile(\n guard: PathGuard,\n dataDir: string,\n opts: { check?: boolean } = {},\n): RebuildReport & { diffSummary?: string } {\n const replayed = replayJournal(guard, dataDir);\n const target = profileFilePath(dataDir);\n if (opts.check) {\n const onDisk = loadProfile(guard, target);\n const same = JSON.stringify(onDisk) === JSON.stringify(replayed.doc);\n return {\n ok: same,\n truncatedTail: replayed.truncatedTail,\n records: replayed.records,\n wrote: false,\n diffSummary: same ? 'no diff' : 'materialized view differs from journal replay',\n };\n }\n saveJson(guard, target, replayed.doc);\n if (replayed.truncatedTail > 0) {\n // explicit audit: never silently rebuild a view that lost its tail\n writeText(\n guard,\n path.join(dataDir, 'logs', 'rebuild-report.txt'),\n `rebuild truncated ${replayed.truncatedTail} torn line(s) at journal tail; ${replayed.records} records applied\\n`,\n );\n }\n return { ok: true, truncatedTail: replayed.truncatedTail, records: replayed.records, wrote: true };\n}\n\n// ── wipe (HB-21, 2026-10-17) ───────────────────────────────────────────────\n// `profile wipe` used to delete three files: the materialized view, the inbox\n// and the live journal. The journal is the authority, and its history lives on\n// in profile_snapshot.json plus the archive shards — so a rebuild after a wipe\n// replayed the whole memory back, and a user who asked to forget got the\n// profile regrown instead. Migration backups (HB-07) and crash leftovers carry\n// copies of the same data.\n//\n// Wipe therefore removes every file the profile can be rebuilt from, and only\n// that: settings, seeds, weekly reports and audit logs are not profile data and\n// stay (burn is the tool for a full shred). Shredding remains burn's job — wipe\n// is a delete, not a destroy.\n\nconst WIPE_FILES = ['profile.json', 'profile_inbox.jsonl', 'profile_journal.jsonl', SNAPSHOT_FILE];\nconst WIPE_PATTERNS: RegExp[] = [\n /^profile_journal\\.archive-/,\n /\\.bak-migrate-\\d{14}$/,\n /\\.incoming-\\d{14}$/,\n];\n\n/** Delete every file the profile can be rebuilt from; returns what was removed\n * (relative to dataDir, sorted per family). */\nexport function wipeProfile(guard: PathGuard, dataDir: string): string[] {\n const removed: string[] = [];\n for (const f of WIPE_FILES) {\n const abs = guard.assert(path.join(dataDir, f));\n if (fs.existsSync(abs)) {\n fs.rmSync(abs, { force: true });\n removed.push(f);\n }\n }\n for (const re of WIPE_PATTERNS) {\n let rels: string[] = [];\n try {\n rels = matchFilesRecursive(guard.assert(dataDir), re);\n } catch {\n rels = [];\n }\n for (const rel of rels) {\n try {\n fs.rmSync(guard.assert(path.join(dataDir, rel)), { force: true });\n removed.push(rel);\n } catch {\n /* already gone */\n }\n }\n }\n return removed;\n}\n","// User profile types (design doc §3). Fourth store: warm, structured,\n// slow-evolving model of the user. Not the material pool (hot cache), not\n// DSH long-term memory (conversation-validated layer).\n\nexport type Partition = 'interest' | 'projects' | 'comm' | 'psy';\nexport type Temporal = 'volatile' | 'stable';\nexport type EvidenceKind = 'chat' | 'screen' | 'browse' | 'hand' | 'ledger';\n\nexport interface Evidence {\n kind: EvidenceKind;\n at: string;\n /** Pointer to an existing audit/log location, e.g. \"heartbeat.jsonl#2026-09-06T12:00:00Z\". */\n ref: string;\n /** At most ONE short quote (<=1 sentence). Never raw conversation/screen text. */\n quote?: string;\n}\n\nexport interface ProfileEntry {\n id: string;\n partition: Partition;\n topic: string;\n subTopic: string;\n content: string;\n /** 0..1; capped per source kind (chat .6 / screen .4 / browse .4). */\n confidence: number;\n temporal: Temporal;\n validFrom: string;\n validTo: string | null;\n supersededBy: string | null;\n evidence: Evidence[];\n createdAt: string;\n updatedAt: string;\n updateCount: number;\n /** stable-tier audit flag (180d without observation): digest deprioritizes. */\n lowActivity?: boolean;\n}\n\nexport interface ProfileDoc {\n version: number;\n partitions: Record<Partition, { entries: ProfileEntry[] }>;\n}\n\nexport interface InboxItem {\n id?: string;\n kind: EvidenceKind;\n at: string;\n ref: string;\n /** <= 1 short sentence; never raw conversation/screen text (truncated by writer). */\n note: string;\n}\n\nexport type ProfileOp =\n | {\n op: 'ADD';\n partition: Partition;\n topic: string;\n subTopic: string;\n content: string;\n temporal?: Temporal;\n confidence?: number;\n why: string;\n evidence: Evidence[];\n /** Assigned by the store at apply time so journal replay preserves ids. */\n assignedId?: string;\n }\n | { op: 'UPDATE'; id: string; changes: { content?: string; confidence?: number }; why: string }\n | { op: 'INVALIDATE'; id: string; why: string; evidence?: Evidence[] }\n | { op: 'NOOP'; why: string };\n\n/**\n * Journal line schema version. Absent = v1: replay applies the applied ops\n * permissively. v2 adds entries — the canonical, code-written post-run entry\n * states (aging included) — and replay upserts those verbatim. That is what\n * makes the live write path and replay ONE state machine (review ③A / HB-09).\n */\nexport const JOURNAL_VERSION = 2;\n\nexport interface JournalRecord {\n v?: number;\n ts: string;\n runId: string;\n /** The model's original intent: audit mirror + the v1 replay path. */\n applied: ProfileOp[];\n rejected: { op: unknown; reason: string }[];\n /** v2 replay path: one full entry snapshot per entry this run touched.\n * Written by code; the model can never reach this field. */\n entries?: ProfileEntry[];\n}\n\nexport interface ApplyReport {\n applied: ProfileOp[];\n rejected: { op: unknown; reason: string }[];\n /** v2 journal events: the full post-state of every entry this run touched. */\n entries?: ProfileEntry[];\n}\n\nexport const PARTITIONS: readonly Partition[] = ['interest', 'projects', 'comm', 'psy'];\n\nexport const CONFIDENCE_CAP: Record<EvidenceKind, number> = {\n chat: 0.6,\n screen: 0.4,\n browse: 0.4,\n hand: 1.0,\n ledger: 0.6,\n};\n\nexport function emptyProfile(): ProfileDoc {\n return {\n version: 1,\n partitions: { interest: { entries: [] }, projects: { entries: [] }, comm: { entries: [] }, psy: { entries: [] } },\n };\n}\n","// profile-schema.json loading + ADD validation (r4 B10: temporal tier is an\n// ENTRY-level attribute; the schema declares, per sub_topic, the ALLOWED tier\n// set and the DEFAULT. Unlisted defaults to 'stable'.)\n\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport type { Partition, Temporal } from './types.js';\n\nexport interface SubTopicDecl {\n allowed?: Temporal[];\n default?: Temporal;\n}\n\nexport interface ProfileSchema {\n version: number;\n partitions: Partial<Record<Partition, {\n topics: Record<string, { subtopics: Record<string, SubTopicDecl> }>;\n }>>;\n}\n\nexport interface SchemaLoad {\n /** The schema to use, or undefined when even the factory copy is unusable. */\n schema?: ProfileSchema;\n /** Where the returned schema came from. */\n source: 'user' | 'factory' | 'none';\n /** Set when the chosen schema is not the first choice (H-17 fallback). */\n fallbackReason?: string;\n}\n\n/**\n * H-17: three-tier load. This file is user-editable by design, so a broken\n * comma used to throw on every beat — `runConsolidation` aborted before\n * `inboxClear`, the inbox grew forever and the profile froze with no way back.\n * Now: a bad user copy falls back to the factory copy (audited by the caller);\n * an unusable factory copy yields `undefined`, which callers read as \"no\n * whitelist\" instead of a fatal error.\n */\nexport function loadProfileSchema(paths: { configDir: string; settingsDir: string }): SchemaLoad {\n const userPath = path.join(paths.settingsDir, 'profile-schema.json');\n const factoryPath = path.join(paths.configDir, 'profile-schema.json');\n const read = (file: string): { schema?: ProfileSchema; error: string } => {\n try {\n const raw = JSON.parse(fs.readFileSync(file, 'utf8')) as ProfileSchema;\n if (!raw.partitions) return { error: 'partitions missing' };\n return { schema: raw, error: '' };\n } catch (e) {\n return { error: String(e) };\n }\n };\n const user = fs.existsSync(userPath) ? read(userPath) : null;\n if (user?.schema) return { schema: user.schema, source: 'user' };\n const factory = read(factoryPath);\n if (factory.schema) {\n return user\n ? {\n schema: factory.schema,\n source: 'factory',\n fallbackReason: `user profile-schema.json unusable (${user.error}) — fell back to the factory copy`,\n }\n : { schema: factory.schema, source: 'factory' };\n }\n return {\n source: 'none',\n fallbackReason: `no usable profile-schema.json (user: ${user?.error ?? 'absent'}; factory: ${factory.error})`,\n };\n}\n\nexport interface SchemaCheck {\n ok: boolean;\n reason?: string;\n temporal: Temporal;\n}\n\n/**\n * Validate an ADD against the whitelist and resolve the entry's temporal tier:\n * LLM nominates within the sub_topic's allowed set; missing nomination falls\n * back to the declared default; fully unlisted -> reject (charter boundary).\n */\nexport function checkAddAgainstSchema(\n schema: ProfileSchema,\n partition: Partition,\n topic: string,\n subTopic: string,\n nominated?: Temporal,\n): SchemaCheck {\n const p = schema.partitions[partition];\n if (!p) return { ok: false, reason: `partition not in schema: ${partition}`, temporal: 'stable' };\n const t = p.topics[topic];\n if (!t) return { ok: false, reason: `topic not in schema: ${partition}/${topic}`, temporal: 'stable' };\n const st = t.subtopics[subTopic];\n if (!st) return { ok: false, reason: `sub_topic not in schema: ${partition}/${topic}/${subTopic}`, temporal: 'stable' };\n const allowed: Temporal[] = st.allowed && st.allowed.length > 0 ? st.allowed : ['stable'];\n const def: Temporal = st.default && allowed.includes(st.default) ? st.default : allowed[0]!;\n if (!nominated) return { ok: true, temporal: def };\n if (!allowed.includes(nominated)) {\n return {\n ok: false,\n reason: `temporal \"${nominated}\" not allowed for ${partition}/${topic}/${subTopic} (allowed: ${allowed.join('|')})`,\n temporal: def,\n };\n }\n return { ok: true, temporal: nominated };\n}\n","// Foreign-shape boundaries (review 2026-10-17 ⑧A). Every place that accepts\n// data this code did not just build itself — the model's op array, journal\n// lines, inbox lines, migration packages, RPC payloads — is checked against\n// ONE schemastery definition here, instead of poking at properties ad hoc at\n// each call site (HB-12: a malformed op used to reach the guards and throw\n// somewhere in the middle of apply).\n//\n// schemastery objects are permissive on purpose: an absent field is simply\n// absent (no error) and unknown keys ride through. Profile boundaries need the\n// opposite, so closed() adds the two rules schemastery leaves out — required\n// keys and a closed key set — and check() applies them before field types run.\n\nimport z from '@deepseek-ai/schemastery';\nimport {\n JOURNAL_VERSION,\n PARTITIONS,\n type Evidence,\n type EvidenceKind,\n type InboxItem,\n type JournalRecord,\n type Partition,\n type ProfileEntry,\n type ProfileOp,\n type Temporal,\n} from './types.js';\n\nexport class ShapeError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'ShapeError';\n }\n}\n\nfunction isPlainObject(v: unknown): v is Record<string, unknown> {\n return typeof v === 'object' && v !== null && !Array.isArray(v);\n}\n\nfunction describe(v: unknown): string {\n if (v === null) return 'null';\n if (Array.isArray(v)) return 'an array';\n return typeof v;\n}\n\n/** A required key. A nullable one accepts `null` as a real value (validTo /\n * supersededBy) instead of reading it as absent. */\ntype Req = string | { key: string; nullable: true };\n\ninterface Closed {\n schema: Schemastery<any, any>;\n keys: readonly string[];\n required: readonly Req[];\n}\n\nfunction closed(\n schema: Schemastery<any, any>,\n keys: readonly string[],\n required: readonly Req[] = keys,\n): Closed {\n return { schema, keys, required };\n}\n\n/** object-ness -> required keys -> closed key set -> schemastery field types. */\nfunction check(shape: Closed, value: unknown, label: string): Record<string, unknown> {\n if (!isPlainObject(value)) throw new ShapeError(`${label}: expected an object, got ${describe(value)}`);\n for (const req of shape.required) {\n const key = typeof req === 'string' ? req : req.key;\n const nullable = typeof req !== 'string' && req.nullable;\n if (value[key] === undefined || (value[key] === null && !nullable)) {\n throw new ShapeError(`${label}.${key}: missing required value`);\n }\n }\n for (const k of Object.keys(value)) {\n if (!shape.keys.includes(k)) throw new ShapeError(`${label}.${k}: unknown field`);\n }\n try {\n return shape.schema(value) as Record<string, unknown>;\n } catch (e) {\n throw new ShapeError(`${label}: ${e instanceof Error ? e.message : String(e)}`);\n }\n}\n\nconst Str = z.string().required();\nconst Num = z.number().required();\n// No .required() on this one: schemastery reads null as \"absent\", but null is\n// a real value for validTo / supersededBy. check() enforces presence instead.\nconst NullableStr = z.union([z.string(), z.const(null)]);\nconst PartitionEnum = z.union(PARTITIONS.map((p) => z.const(p))).required();\nconst TemporalEnum = z.union([z.const('stable'), z.const('volatile')]).required();\n/** Same set, optional: the model may omit temporal and take the schema default. */\nconst OptionalTemporal = z.union([z.const('stable'), z.const('volatile')]);\nconst KindEnum = z.union([\n z.const('chat'),\n z.const('screen'),\n z.const('browse'),\n z.const('hand'),\n z.const('ledger'),\n]).required();\nconst ConfidenceNum = z.number().min(0).max(1).required();\n\n// ── evidence ────────────────────────────────────────────────────────────────\n\nconst EvidenceShape = closed(\n z.object({ kind: KindEnum, at: Str, ref: Str, quote: z.string() }),\n ['kind', 'at', 'ref', 'quote'],\n ['kind', 'at', 'ref'],\n);\n\nexport function parseEvidence(value: unknown, label = 'evidence'): Evidence {\n const o = check(EvidenceShape, value, label);\n return {\n kind: o.kind as EvidenceKind,\n at: String(o.at),\n ref: String(o.ref),\n ...(o.quote === undefined ? {} : { quote: String(o.quote) }),\n };\n}\n\nfunction parseEvidenceList(value: unknown, label: string): Evidence[] {\n if (!Array.isArray(value)) throw new ShapeError(`${label}: expected an array`);\n return value.map((v, i) => parseEvidence(v, `${label}[${i}]`));\n}\n\n// ── stored entry (journal v2 / migration packages) ──────────────────────────\n\nconst EntryShape = closed(\n z.object({\n id: Str,\n partition: PartitionEnum,\n topic: Str,\n subTopic: Str,\n content: Str,\n confidence: ConfidenceNum,\n temporal: TemporalEnum,\n validFrom: Str,\n validTo: NullableStr,\n supersededBy: NullableStr,\n evidence: z.array(z.any()).required(),\n createdAt: Str,\n updatedAt: Str,\n updateCount: Num,\n lowActivity: z.boolean(),\n }),\n ['id', 'partition', 'topic', 'subTopic', 'content', 'confidence', 'temporal', 'validFrom', 'validTo',\n 'supersededBy', 'evidence', 'createdAt', 'updatedAt', 'updateCount', 'lowActivity'],\n ['id', 'partition', 'topic', 'subTopic', 'content', 'confidence', 'temporal', 'validFrom',\n { key: 'validTo', nullable: true }, { key: 'supersededBy', nullable: true },\n 'evidence', 'createdAt', 'updatedAt', 'updateCount'],\n);\n\nexport function parseProfileEntry(value: unknown, label = 'entry'): ProfileEntry {\n const o = check(EntryShape, value, label);\n return {\n id: String(o.id),\n partition: o.partition as Partition,\n topic: String(o.topic),\n subTopic: String(o.subTopic),\n content: String(o.content),\n confidence: Number(o.confidence),\n temporal: o.temporal as Temporal,\n validFrom: String(o.validFrom),\n validTo: o.validTo === null ? null : String(o.validTo),\n supersededBy: o.supersededBy === null ? null : String(o.supersededBy),\n evidence: parseEvidenceList(o.evidence, `${label}.evidence`),\n createdAt: String(o.createdAt),\n updatedAt: String(o.updatedAt),\n updateCount: Number(o.updateCount),\n ...(o.lowActivity === undefined ? {} : { lowActivity: Boolean(o.lowActivity) }),\n };\n}\n\n// ── inbox line ──────────────────────────────────────────────────────────────\n\nconst InboxShape = closed(\n z.object({ id: z.string(), kind: KindEnum, at: Str, ref: Str, note: z.string() }),\n ['id', 'kind', 'at', 'ref', 'note'],\n ['kind', 'at', 'ref'],\n);\n\nexport function parseInboxItem(value: unknown, label = 'inbox item'): InboxItem {\n const o = check(InboxShape, value, label);\n return {\n ...(o.id === undefined ? {} : { id: String(o.id) }),\n kind: o.kind as EvidenceKind,\n at: String(o.at),\n ref: String(o.ref),\n note: o.note === undefined ? '' : String(o.note),\n };\n}\n\n// ── the model's op array (③A: ids in, evidence out) ─────────────────────────\n//\n// The model never supplies refs, timestamps or evidence kinds any more — it\n// cites observation ids from THIS batch (o1, o2, …) and the caller turns them\n// into Evidence objects. HB-05's forged provenance (a made-up \"hand\" ref used\n// to pin confidence at 1.0) is unreachable by construction.\n\nexport type ProposedOp =\n | {\n op: 'ADD';\n partition: Partition;\n topic: string;\n subTopic: string;\n content: string;\n temporal?: Temporal;\n confidence?: number;\n why: string;\n evidenceRefs: string[];\n }\n | { op: 'UPDATE'; id: string; changes: { content?: string; confidence?: number }; why: string }\n | { op: 'INVALIDATE'; id: string; why: string; evidenceRefs: string[] }\n | { op: 'NOOP'; why: string };\n\nconst AddShape = closed(\n z.object({\n op: z.const('ADD').required(),\n partition: PartitionEnum,\n topic: Str,\n subTopic: Str,\n content: Str,\n temporal: OptionalTemporal,\n confidence: z.number().min(0).max(1),\n why: z.string(),\n evidence: z.array(z.any()).required(),\n }),\n ['op', 'partition', 'topic', 'subTopic', 'content', 'temporal', 'confidence', 'why', 'evidence'],\n ['op', 'partition', 'topic', 'subTopic', 'content', 'evidence'],\n);\n\nconst UpdateShape = closed(\n z.object({\n op: z.const('UPDATE').required(),\n id: Str,\n changes: z.object({ content: z.string(), confidence: z.number().min(0).max(1) }),\n why: z.string(),\n }),\n ['op', 'id', 'changes', 'why'],\n ['op', 'id', 'changes'],\n);\n\nconst InvalidateShape = closed(\n z.object({\n op: z.const('INVALIDATE').required(),\n id: Str,\n why: z.string(),\n evidence: z.array(z.any()),\n }),\n ['op', 'id', 'why', 'evidence'],\n ['op', 'id'],\n);\n\nconst NoopShape = closed(\n z.object({ op: z.const('NOOP').required(), why: z.string() }),\n ['op', 'why'],\n ['op'],\n);\n\nfunction parseObservationRefs(value: unknown, label: string): string[] {\n if (value === undefined) return [];\n if (!Array.isArray(value)) throw new ShapeError(`${label}: expected an array of observation ids`);\n return value.map((v, i) => {\n if (typeof v !== 'string' || !v.trim()) {\n throw new ShapeError(`${label}[${i}]: expected an observation id like \"o1\" (refs, timestamps and kinds are code-generated)`);\n }\n return v.trim();\n });\n}\n\nfunction parseProposedOp(raw: unknown, label: string): ProposedOp {\n if (!isPlainObject(raw)) throw new ShapeError(`${label}: expected an object, got ${describe(raw)}`);\n const tag = raw.op;\n if (tag === 'ADD') {\n const o = check(AddShape, raw, label);\n const evidenceRefs = parseObservationRefs(o.evidence, `${label}.evidence`);\n if (evidenceRefs.length === 0) {\n throw new ShapeError(`${label}.evidence: ADD needs at least one observation id (axiom 1: no provenance, no memory)`);\n }\n return {\n op: 'ADD',\n partition: o.partition as Partition,\n topic: String(o.topic),\n subTopic: String(o.subTopic),\n content: String(o.content),\n ...(o.temporal === undefined ? {} : { temporal: o.temporal as Temporal }),\n ...(o.confidence === undefined ? {} : { confidence: Number(o.confidence) }),\n why: typeof o.why === 'string' ? o.why : '',\n evidenceRefs,\n };\n }\n if (tag === 'UPDATE') {\n const o = check(UpdateShape, raw, label);\n const changes = o.changes as Record<string, unknown>;\n const out: { content?: string; confidence?: number } = {};\n if (changes.content !== undefined) out.content = String(changes.content);\n if (changes.confidence !== undefined) out.confidence = Number(changes.confidence);\n if (out.content === undefined && out.confidence === undefined) {\n throw new ShapeError(`${label}.changes: needs content or confidence`);\n }\n return { op: 'UPDATE', id: String(o.id), changes: out, why: typeof o.why === 'string' ? o.why : '' };\n }\n if (tag === 'INVALIDATE') {\n const o = check(InvalidateShape, raw, label);\n return {\n op: 'INVALIDATE',\n id: String(o.id),\n why: typeof o.why === 'string' ? o.why : '',\n evidenceRefs: parseObservationRefs(o.evidence, `${label}.evidence`),\n };\n }\n if (tag === 'NOOP') {\n const o = check(NoopShape, raw, label);\n return { op: 'NOOP', why: typeof o.why === 'string' ? o.why : '' };\n }\n throw new ShapeError(`${label}.op: unknown op ${JSON.stringify(tag)}`);\n}\n\nexport interface OpParseResult {\n ops: ProposedOp[];\n /** One malformed row never discards the rest of the batch, but it is always\n * reported — silence here used to look like \"the model proposed nothing\". */\n rejected: { op: unknown; reason: string }[];\n}\n\nexport function parseProposedOps(list: unknown[]): OpParseResult {\n const ops: ProposedOp[] = [];\n const rejected: { op: unknown; reason: string }[] = [];\n list.forEach((raw, i) => {\n try {\n ops.push(parseProposedOp(raw, `op[${i}]`));\n } catch (e) {\n rejected.push({ op: raw, reason: e instanceof Error ? e.message : String(e) });\n }\n });\n return { ops, rejected };\n}\n\n/** Turn cited observation ids into code-built evidence. Unknown ids are a\n * rejection, never a silent drop: the op then has no provenance. */\nexport function materializeEvidence(\n refs: string[],\n observations: Map<string, InboxItem>,\n label: string,\n): { evidence: Evidence[]; reason?: string } {\n const evidence: Evidence[] = [];\n for (const ref of refs) {\n const item = observations.get(ref);\n if (!item) return { evidence: [], reason: `${label}: ${ref} does not match any observation in this batch` };\n evidence.push({ kind: item.kind, at: item.at, ref: item.ref });\n }\n return { evidence };\n}\n\n// ── journal line reader (replay path) ───────────────────────────────────────\n//\n// Lenient on purpose: a line this code did not write is skipped by the caller,\n// never fatal. The journal is append-only and immutable, so one bad line must\n// not freeze the profile — but it IS counted (ReplayResult.badRecords) so a\n// silent data loss is visible in profile verify.\n\nexport function tryParseJournalRecord(value: unknown): JournalRecord | null {\n try {\n if (!isPlainObject(value)) return null;\n const rec: JournalRecord = {\n ts: typeof value.ts === 'string' ? value.ts : '',\n runId: typeof value.runId === 'string' ? value.runId : '',\n applied: Array.isArray(value.applied) ? (value.applied as ProfileOp[]) : [],\n rejected: Array.isArray(value.rejected) ? (value.rejected as { op: unknown; reason: string }[]) : [],\n };\n if (value.v === JOURNAL_VERSION) {\n if (!Array.isArray(value.entries)) return null;\n r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AAAA,IAChB;AAAA,EACF;AACA,aAAW,MAAM,eAAe;AAC9B,QAAI,OAAiB,CAAC;AACtB,QAAI;AACF,aAAO,oBAAoB,MAAM,OAAO,OAAO,GAAG,EAAE;AAAA,IACtD,QAAQ;AACN,aAAO,CAAC;AAAA,IACV;AACA,eAAW,OAAO,MAAM;AACtB,UAAI;AACF,QAAAA,IAAG,OAAO,MAAM,OAAOD,MAAK,KAAK,SAAS,GAAG,CAAC,GAAG,EAAE,OAAO,KAAK,CAAC;AAChE,gBAAQ,KAAK,GAAG;AAAA,MAClB,QAAQ;AAAA,MAER;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;","names":["path","fs","fs","path","path","fs","check"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/atomic-fs.ts"],"sourcesContent":["// Crash-safe file replacement: write to a random-suffix temp file in the SAME\n// directory as the target, then rename over it. Same-directory rename stays\n// on one volume (atomic) and Node's rename replaces existing files on Windows.\n\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport { randomUUID, randomFillSync } from 'node:crypto';\n\nexport function tmpSibling(target: string, tag = 'w'): string {\n return path.join(\n path.dirname(target),\n `.${path.basename(target)}.${tag}-${randomUUID().slice(0, 8)}.tmp`,\n );\n}\n\nexport function atomicWriteFileSync(target: string, data: string | Uint8Array): void {\n const tmp = tmpSibling(target);\n let fd: number | null = null;\n try {\n fd = fs.openSync(tmp, 'w');\n fs.writeFileSync(fd, data);\n // 低优先级 review (2026-10-17): rename is atomic against a crash of THIS\n // process, but without a flush the renamed file can still point at blocks\n // the OS never wrote — a power loss then leaves an empty or partial file\n // where a complete one is promised. Flush before publishing.\n fs.fsyncSync(fd);\n fs.closeSync(fd);\n fd = null;\n fs.renameSync(tmp, target);\n } finally {\n if (fd !== null) {\n try { fs.closeSync(fd); } catch { /* the write path already failed */ }\n }\n fs.rmSync(tmp, { force: true });\n }\n}\n\nexport function atomicWriteJsonSync(target: string, value: unknown): void {\n atomicWriteFileSync(target, JSON.stringify(value, null, 2));\n}\n\n/** Overwrite a file's bytes with random data before unlinking. */\nexport function shredFileSync(target: string, passes = 3): void {\n const stat = fs.statSync(target);\n if (!stat.isFile()) throw new Error(`shred: not a file: ${target}`);\n const buf = Buffer.alloc(Math.max(stat.size, 1));\n for (let i = 0; i < passes; i++) {\n randomFillSync(buf);\n fs.writeFileSync(target, buf);\n }\n fs.rmSync(target, { force: true });\n}\n\n/**\n * Best-effort burn for a plaintext window: shred it when it exists, ignore it\n * when it is already gone (H-59 — `rm` alone leaves the bytes on disk).\n */\nexport function burnFileSync(target: string): void {\n try {\n shredFileSync(target);\n } catch {\n fs.rmSync(target, { force: true });\n }\n}\n"],"mappings":";AAIA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,SAAS,YAAY,sBAAsB;AAEpC,SAAS,WAAW,QAAgB,MAAM,KAAa;AAC5D,SAAO,KAAK;AAAA,IACV,KAAK,QAAQ,MAAM;AAAA,IACnB,IAAI,KAAK,SAAS,MAAM,CAAC,IAAI,GAAG,IAAI,WAAW,EAAE,MAAM,GAAG,CAAC,CAAC;AAAA,EAC9D;AACF;AAEO,SAAS,oBAAoB,QAAgB,MAAiC;AACnF,QAAM,MAAM,WAAW,MAAM;AAC7B,MAAI,KAAoB;AACxB,MAAI;AACF,SAAK,GAAG,SAAS,KAAK,GAAG;AACzB,OAAG,cAAc,IAAI,IAAI;AAKzB,OAAG,UAAU,EAAE;AACf,OAAG,UAAU,EAAE;AACf,SAAK;AACL,OAAG,WAAW,KAAK,MAAM;AAAA,EAC3B,UAAE;AACA,QAAI,OAAO,MAAM;AACf,UAAI;AAAE,WAAG,UAAU,EAAE;AAAA,MAAG,QAAQ;AAAA,MAAsC;AAAA,IACxE;AACA,OAAG,OAAO,KAAK,EAAE,OAAO,KAAK,CAAC;AAAA,EAChC;AACF;AAEO,SAAS,oBAAoB,QAAgB,OAAsB;AACxE,sBAAoB,QAAQ,KAAK,UAAU,OAAO,MAAM,CAAC,CAAC;AAC5D;AAGO,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