@remnic/core 9.3.645 → 9.3.647

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 (98) hide show
  1. package/dist/access-cli.js +18 -17
  2. package/dist/access-cli.js.map +1 -1
  3. package/dist/access-http.js +13 -13
  4. package/dist/access-mcp.js +10 -10
  5. package/dist/access-schema.js +3 -3
  6. package/dist/access-service.js +8 -8
  7. package/dist/adapters/index.js +4 -4
  8. package/dist/adapters/registry.js +2 -2
  9. package/dist/{capsule-crypto-YO5QJ6L3.js → capsule-crypto-GWVG7LGC.js} +2 -2
  10. package/dist/{chunk-6YLJZ2EU.js → chunk-3D6L7CEP.js} +92 -29
  11. package/dist/chunk-3D6L7CEP.js.map +1 -0
  12. package/dist/{chunk-3SF7QNKF.js → chunk-6BNFVP7Y.js} +2 -2
  13. package/dist/{chunk-AGNBY3VG.js → chunk-APJQ6UEA.js} +4 -4
  14. package/dist/{chunk-HRXCPKN4.js → chunk-APWJRJFW.js} +2 -2
  15. package/dist/{chunk-VDX2J7OX.js → chunk-BUKK5SWA.js} +2 -2
  16. package/dist/{chunk-T22TZJWJ.js → chunk-DC66QVL2.js} +2 -2
  17. package/dist/chunk-EVWIEEKZ.js +315 -0
  18. package/dist/chunk-EVWIEEKZ.js.map +1 -0
  19. package/dist/{chunk-IZXOO6NP.js → chunk-FAYDM5WD.js} +2 -2
  20. package/dist/{chunk-Y2XKJJEI.js → chunk-IMWFHBG2.js} +10 -33
  21. package/dist/chunk-IMWFHBG2.js.map +1 -0
  22. package/dist/{chunk-W5ISYSIL.js → chunk-L7W5YW6Y.js} +4 -4
  23. package/dist/{chunk-DGNQRNLL.js → chunk-NT5TINK5.js} +2 -2
  24. package/dist/{chunk-BXLOS5AJ.js → chunk-OWHERGF2.js} +2 -2
  25. package/dist/{chunk-CDHRUNBX.js → chunk-QKE4LHNR.js} +2 -2
  26. package/dist/{chunk-BTRL6GG7.js → chunk-RAELB5NX.js} +5 -5
  27. package/dist/{chunk-2QSZNTDO.js → chunk-RKNJBZ55.js} +4 -4
  28. package/dist/chunk-S4DDLTPX.js +140 -0
  29. package/dist/chunk-S4DDLTPX.js.map +1 -0
  30. package/dist/{chunk-MBT3ASYV.js → chunk-TA4LQ5SR.js} +12 -12
  31. package/dist/{chunk-KVUG7FF7.js → chunk-U55D5UD5.js} +7 -7
  32. package/dist/{chunk-DQEMWVMT.js → chunk-UVYI6VIX.js} +1 -1
  33. package/dist/{chunk-D2B22JDF.js → chunk-WEPMT6SC.js} +7 -7
  34. package/dist/{chunk-IHG27THY.js → chunk-XUGQQPGO.js} +77 -61
  35. package/dist/chunk-XUGQQPGO.js.map +1 -0
  36. package/dist/{chunk-YOXCJP3A.js → chunk-ZPPFKVSD.js} +3 -3
  37. package/dist/cli.js +30 -28
  38. package/dist/{first-start-migration-FF7YFGRP.js → first-start-migration-PG5HBC3K.js} +4 -4
  39. package/dist/index.d.ts +2 -0
  40. package/dist/index.js +77 -63
  41. package/dist/index.js.map +1 -1
  42. package/dist/lcm/engine.js +3 -3
  43. package/dist/lcm/index.js +11 -11
  44. package/dist/namespaces/migrate.js +5 -5
  45. package/dist/namespaces/search.js +4 -4
  46. package/dist/operator-toolkit.js +6 -6
  47. package/dist/orchestrator.js +14 -13
  48. package/dist/resume-bundles.js +3 -2
  49. package/dist/schemas.d.ts +22 -22
  50. package/dist/search/factory.js +3 -3
  51. package/dist/search/index.js +7 -7
  52. package/dist/session-identity.d.ts +118 -0
  53. package/dist/session-identity.js +21 -0
  54. package/dist/session-identity.js.map +1 -0
  55. package/dist/session-transcript-migration.d.ts +65 -0
  56. package/dist/session-transcript-migration.js +16 -0
  57. package/dist/session-transcript-migration.js.map +1 -0
  58. package/dist/summarizer.js +2 -1
  59. package/dist/transcript.d.ts +18 -6
  60. package/dist/transcript.js +2 -1
  61. package/dist/transfer/backup.js +2 -2
  62. package/dist/transfer/capsule-export.js +2 -2
  63. package/dist/transfer/capsule-import.js +2 -2
  64. package/dist/transfer/import-sqlite.js +2 -2
  65. package/dist/transfer/types.d.ts +12 -12
  66. package/package.json +1 -1
  67. package/src/cli.ts +79 -0
  68. package/src/index.ts +18 -0
  69. package/src/session-identity.test.ts +130 -0
  70. package/src/session-identity.ts +281 -0
  71. package/src/session-transcript-migration.test.ts +350 -0
  72. package/src/session-transcript-migration.ts +527 -0
  73. package/src/summarizer.ts +15 -35
  74. package/src/transcript-session-identity.test.ts +424 -0
  75. package/src/transcript.test.ts +110 -0
  76. package/src/transcript.ts +121 -73
  77. package/dist/chunk-6YLJZ2EU.js.map +0 -1
  78. package/dist/chunk-IHG27THY.js.map +0 -1
  79. package/dist/chunk-Y2XKJJEI.js.map +0 -1
  80. /package/dist/{capsule-crypto-YO5QJ6L3.js.map → capsule-crypto-GWVG7LGC.js.map} +0 -0
  81. /package/dist/{chunk-3SF7QNKF.js.map → chunk-6BNFVP7Y.js.map} +0 -0
  82. /package/dist/{chunk-AGNBY3VG.js.map → chunk-APJQ6UEA.js.map} +0 -0
  83. /package/dist/{chunk-HRXCPKN4.js.map → chunk-APWJRJFW.js.map} +0 -0
  84. /package/dist/{chunk-VDX2J7OX.js.map → chunk-BUKK5SWA.js.map} +0 -0
  85. /package/dist/{chunk-T22TZJWJ.js.map → chunk-DC66QVL2.js.map} +0 -0
  86. /package/dist/{chunk-IZXOO6NP.js.map → chunk-FAYDM5WD.js.map} +0 -0
  87. /package/dist/{chunk-W5ISYSIL.js.map → chunk-L7W5YW6Y.js.map} +0 -0
  88. /package/dist/{chunk-DGNQRNLL.js.map → chunk-NT5TINK5.js.map} +0 -0
  89. /package/dist/{chunk-BXLOS5AJ.js.map → chunk-OWHERGF2.js.map} +0 -0
  90. /package/dist/{chunk-CDHRUNBX.js.map → chunk-QKE4LHNR.js.map} +0 -0
  91. /package/dist/{chunk-BTRL6GG7.js.map → chunk-RAELB5NX.js.map} +0 -0
  92. /package/dist/{chunk-2QSZNTDO.js.map → chunk-RKNJBZ55.js.map} +0 -0
  93. /package/dist/{chunk-MBT3ASYV.js.map → chunk-TA4LQ5SR.js.map} +0 -0
  94. /package/dist/{chunk-KVUG7FF7.js.map → chunk-U55D5UD5.js.map} +0 -0
  95. /package/dist/{chunk-DQEMWVMT.js.map → chunk-UVYI6VIX.js.map} +0 -0
  96. /package/dist/{chunk-D2B22JDF.js.map → chunk-WEPMT6SC.js.map} +0 -0
  97. /package/dist/{chunk-YOXCJP3A.js.map → chunk-ZPPFKVSD.js.map} +0 -0
  98. /package/dist/{first-start-migration-FF7YFGRP.js.map → first-start-migration-PG5HBC3K.js.map} +0 -0
@@ -0,0 +1,281 @@
1
+ import path from "node:path";
2
+ import {
3
+ encodeStoragePathSegment,
4
+ encodeStoragePathSegmentWithHash,
5
+ isSafeLegacyPathSegment,
6
+ storagePathHash,
7
+ } from "./storage-paths.js";
8
+
9
+ /**
10
+ * Shared session-identity / transcript-pathing layer (issue #1496).
11
+ *
12
+ * This module is the SINGLE source of truth for turning a `sessionKey` into a
13
+ * channel identity and a set of deterministic, collision-resistant storage
14
+ * paths. Every subsystem that previously re-parsed session keys (transcript
15
+ * pathing, tool-usage pathing, hourly summaries, session auditing, conversation
16
+ * indexing) MUST route through `parseSessionIdentity()` / `sessionStoragePaths()`
17
+ * so behavior stays identical across paths (rule #22, #39).
18
+ *
19
+ * Two shapes are recognized:
20
+ *
21
+ * 1. Legacy `agent:<agentId>:<channelType>:...` keys keep their existing
22
+ * readable channel identity:
23
+ * - agent:<id>:main → type="main", id="default"
24
+ * - agent:<id>:discord:channel:<chanId> → type="discord", id="<chanId>"
25
+ * - agent:<id>:slack:channel:<chanId> → type="slack", id="<chanId>"
26
+ * - agent:<id>:cron:<jobId> → type="cron", id="<jobId>"
27
+ * - agent:<id>:<other>[:<id>] → type="<other>", id="<id?>"
28
+ *
29
+ * 2. Arbitrary / non-legacy keys (e.g. `pi-geek:abc123`) become first-class
30
+ * from the FIRST write — they NEVER start life under `other/default`:
31
+ * channelType = "session"
32
+ * channelId = storagePathHash(sessionKey) (collision-resistant)
33
+ * yielding `transcripts/session/<hash>/YYYY-MM-DD.jsonl`.
34
+ *
35
+ * The reserved `"session"` channel type is what makes arbitrary keys isolated
36
+ * and auditable. Legacy data under `other/default` (and any legacy channel
37
+ * directory) remains READABLE via `legacyDir` / `alternateDir` candidates.
38
+ */
39
+
40
+ /** Channel type reserved for first-class arbitrary (non-legacy) session keys. */
41
+ export const SESSION_CHANNEL_TYPE = "session";
42
+
43
+ /** Legacy fallback channel identity for un-parseable keys (read-back only). */
44
+ export const LEGACY_FALLBACK_CHANNEL_TYPE = "other";
45
+ export const LEGACY_FALLBACK_CHANNEL_ID = "default";
46
+
47
+ /** Max characters of the raw session key kept in a human-readable display label. */
48
+ const DISPLAY_LABEL_MAX_LENGTH = 64;
49
+
50
+ export interface SessionIdentity {
51
+ /** Channel type — legacy channel type for known shapes, else `"session"`. */
52
+ channelType: string;
53
+ /** Channel id — legacy channel id for known shapes, else the key hash. */
54
+ channelId: string;
55
+ /** Human-readable, non-authoritative label derived from the raw key. */
56
+ displayLabel: string;
57
+ /** The original session key, unchanged. */
58
+ canonicalSessionKey: string;
59
+ /** True when the key matched a known legacy `agent:<id>:...` shape. */
60
+ legacy: boolean;
61
+ }
62
+
63
+ export interface SessionStoragePaths {
64
+ /** Channel type (mirrors {@link SessionIdentity.channelType}). */
65
+ channelType: string;
66
+ /** Channel id (mirrors {@link SessionIdentity.channelId}). */
67
+ channelId: string;
68
+ /** Primary encoded storage subdirectory: `<encodedType>/<encodedId>`. */
69
+ dir: string;
70
+ /**
71
+ * Collision-resistant alternate subdirectory that always embeds the full
72
+ * session-key hash. Used both as a write-time collision escape hatch and as a
73
+ * read-back candidate for data written before this layer existed.
74
+ */
75
+ alternateDir: string;
76
+ /**
77
+ * Un-encoded `<type>/<id>` directory for reading data written by older
78
+ * builds that did not URL-encode path segments. Undefined when it would be
79
+ * identical to `dir` or when either segment is path-unsafe.
80
+ */
81
+ legacyDir?: string;
82
+ /**
83
+ * Ordered, de-duplicated list of READ-BACK-ONLY directories where an older
84
+ * build may have stranded this key's data. NEW writes never target these —
85
+ * they exist purely so pre-#1496 transcripts/tool-usage stay discoverable
86
+ * (and migratable). Populated only for non-legacy keys. Includes:
87
+ * 1. the shared `other/default` fallback (every arbitrary key landed here);
88
+ * 2. the directory the OLD `parts.length >= 3` parser would have chosen
89
+ * (e.g. `foo:bar:baz` → `baz/default`, `foo:bar:baz:qux` → `baz/qux`).
90
+ * See {@link legacyParserReadbackDir}.
91
+ */
92
+ readbackDirs: string[];
93
+ }
94
+
95
+ /**
96
+ * Parse a session key into a stable channel identity.
97
+ *
98
+ * Pure and deterministic — no filesystem access. Same input always yields the
99
+ * same identity, which is what makes arbitrary keys collision-resistant from
100
+ * the first write.
101
+ */
102
+ export function parseSessionIdentity(sessionKey: string): SessionIdentity {
103
+ const canonicalSessionKey = typeof sessionKey === "string" ? sessionKey : "";
104
+ const legacy = parseLegacyChannelIdentity(canonicalSessionKey);
105
+ if (legacy) {
106
+ return {
107
+ channelType: legacy.channelType,
108
+ channelId: legacy.channelId,
109
+ displayLabel: safeDisplayLabel(canonicalSessionKey),
110
+ canonicalSessionKey,
111
+ legacy: true,
112
+ };
113
+ }
114
+
115
+ return {
116
+ channelType: SESSION_CHANNEL_TYPE,
117
+ channelId: storagePathHash(canonicalSessionKey),
118
+ displayLabel: safeDisplayLabel(canonicalSessionKey),
119
+ canonicalSessionKey,
120
+ legacy: false,
121
+ };
122
+ }
123
+
124
+ /**
125
+ * Resolve the storage path pieces for a session key. Wraps
126
+ * {@link parseSessionIdentity} so transcript, tool-usage, and summary writers
127
+ * share one implementation (rule #22).
128
+ */
129
+ export function sessionStoragePaths(sessionKey: string): SessionStoragePaths {
130
+ const identity = parseSessionIdentity(sessionKey);
131
+ const { channelType, channelId, canonicalSessionKey } = identity;
132
+
133
+ const dir = path.join(encodeStoragePathSegment(channelType), encodeStoragePathSegment(channelId));
134
+
135
+ const alternateDir = path.join(
136
+ encodeStoragePathSegmentWithHash(channelType),
137
+ `${encodeStoragePathSegmentWithHash(channelId)}--session-${storagePathHash(canonicalSessionKey)}`
138
+ );
139
+
140
+ let legacyDir: string | undefined;
141
+ if (isSafeLegacyPathSegment(channelType) && isSafeLegacyPathSegment(channelId)) {
142
+ const candidate = path.join(channelType, channelId);
143
+ if (candidate !== dir) {
144
+ legacyDir = candidate;
145
+ }
146
+ }
147
+
148
+ // Read-back-only candidates are only relevant for non-legacy keys: legacy
149
+ // `agent:<id>:...` keys still resolve to their original channel directory, so
150
+ // nothing about their on-disk location moved.
151
+ const readbackDirs: string[] = [];
152
+ if (!identity.legacy) {
153
+ const seen = new Set<string>([dir]);
154
+ if (alternateDir) seen.add(alternateDir);
155
+ if (legacyDir) seen.add(legacyDir);
156
+ for (const candidate of [OTHER_DEFAULT_READBACK_DIR, legacyParserReadbackDir(canonicalSessionKey)]) {
157
+ if (!candidate || seen.has(candidate)) continue;
158
+ seen.add(candidate);
159
+ readbackDirs.push(candidate);
160
+ }
161
+ }
162
+
163
+ return { channelType, channelId, dir, alternateDir, legacyDir, readbackDirs };
164
+ }
165
+
166
+ /**
167
+ * The shared `other/default` directory every arbitrary key was routed into by
168
+ * builds predating issue #1496. Exposed so transcript/tool-usage/summary read
169
+ * paths and the migration scanner all agree on the same fallback location.
170
+ */
171
+ export const OTHER_DEFAULT_READBACK_DIR = path.join(
172
+ LEGACY_FALLBACK_CHANNEL_TYPE,
173
+ LEGACY_FALLBACK_CHANNEL_ID
174
+ );
175
+
176
+ /**
177
+ * Reconstruct the transcript/tool-usage directory the OLD `getTranscriptPath`
178
+ * parser (pre-#1496) would have produced for a key the NEW parser reclassifies
179
+ * as a first-class `session/<hash>` identity.
180
+ *
181
+ * The OLD parser treated ANY key with `parts.length >= 3` as legacy — it did
182
+ * NOT require a leading `agent` segment — so an arbitrary key like
183
+ * `foo:bar:baz` was stored under `baz/default` and `foo:bar:baz:qux` under
184
+ * `baz/qux`. Those directories must stay readable (and migratable) for existing
185
+ * installs even though the key now writes to `session/<hash>` (Thread B / codex
186
+ * review on PR #1504). Path segments are encoded exactly as the old `dir` was
187
+ * built (`encodeStoragePathSegment`) so the candidate matches the bytes on disk.
188
+ *
189
+ * Returns `undefined` when the key has fewer than three colon parts (the old
190
+ * parser would have used the `other/default` fallback, already covered), when
191
+ * the channel type is empty, or when the result is unsafe.
192
+ */
193
+ export function legacyParserReadbackDir(sessionKey: string): string | undefined {
194
+ if (typeof sessionKey !== "string" || sessionKey.length === 0) return undefined;
195
+ const parts = sessionKey.split(":");
196
+ if (parts.length < 3) return undefined;
197
+
198
+ const channelType = parts[2];
199
+ if (!channelType || channelType.length === 0) return undefined;
200
+
201
+ // Mirror the OLD parser's channelId derivation for parts.length >= 3 keys.
202
+ let channelId = LEGACY_FALLBACK_CHANNEL_ID;
203
+ if (channelType === "main") {
204
+ channelId = "default";
205
+ } else if (channelType === "discord" && parts.length >= 5 && parts[3] === "channel") {
206
+ channelId = parts[4];
207
+ } else if (channelType === "slack" && parts.length >= 5 && parts[3] === "channel") {
208
+ channelId = parts[4];
209
+ } else if (channelType === "cron" && parts.length >= 4) {
210
+ channelId = parts[3];
211
+ } else if (parts.length >= 4) {
212
+ channelId = parts[3];
213
+ }
214
+ if (!channelId || channelId.length === 0) {
215
+ channelId = LEGACY_FALLBACK_CHANNEL_ID;
216
+ }
217
+
218
+ return path.join(encodeStoragePathSegment(channelType), encodeStoragePathSegment(channelId));
219
+ }
220
+
221
+ /**
222
+ * Parse the legacy `agent:<agentId>:<channelType>:...` shape. Returns
223
+ * `undefined` for anything that is not a recognized legacy key, so the caller
224
+ * can route it to a first-class `session/<hash>` identity instead.
225
+ *
226
+ * A legacy key must start with the literal `agent` segment and a non-empty
227
+ * agent id; otherwise an arbitrary key like `pi-geek:abc123` would be
228
+ * misread as `channelType = "abc123"`.
229
+ */
230
+ function parseLegacyChannelIdentity(sessionKey: string): { channelType: string; channelId: string } | undefined {
231
+ if (sessionKey.length === 0) return undefined;
232
+ const parts = sessionKey.split(":");
233
+ if (parts.length < 3) return undefined;
234
+ if (parts[0] !== "agent") return undefined;
235
+ if (!parts[1] || parts[1].length === 0) return undefined;
236
+
237
+ const channelType = parts[2];
238
+ if (!channelType || channelType.length === 0) return undefined;
239
+
240
+ let channelId = LEGACY_FALLBACK_CHANNEL_ID;
241
+ if (channelType === "main") {
242
+ channelId = "default";
243
+ } else if (channelType === "discord" && parts.length >= 5 && parts[3] === "channel") {
244
+ channelId = parts[4];
245
+ } else if (channelType === "slack" && parts.length >= 5 && parts[3] === "channel") {
246
+ channelId = parts[4];
247
+ } else if (channelType === "cron" && parts.length >= 4) {
248
+ channelId = parts[3];
249
+ } else if (parts.length >= 4) {
250
+ channelId = parts[3];
251
+ }
252
+
253
+ if (!channelId || channelId.length === 0) {
254
+ channelId = LEGACY_FALLBACK_CHANNEL_ID;
255
+ }
256
+
257
+ return { channelType, channelId };
258
+ }
259
+
260
+ /**
261
+ * A short, human-readable label for dashboards/audits. Never used for storage
262
+ * routing — `channelId` (the hash) owns isolation. Control characters and path
263
+ * separators are stripped so the label is safe to render.
264
+ */
265
+ function safeDisplayLabel(sessionKey: string): string {
266
+ if (sessionKey.length === 0) return "(empty)";
267
+ let out = "";
268
+ for (const char of sessionKey) {
269
+ const code = char.codePointAt(0) ?? 0;
270
+ // Drop ASCII control characters (0x00-0x1F) and DEL (0x7F).
271
+ if (code <= 0x1f || code === 0x7f) continue;
272
+ if (char === "/" || char === "\\") {
273
+ out += "_";
274
+ continue;
275
+ }
276
+ out += char;
277
+ }
278
+ const trimmed = out.trim();
279
+ if (trimmed.length === 0) return "(empty)";
280
+ return trimmed.length > DISPLAY_LABEL_MAX_LENGTH ? `${trimmed.slice(0, DISPLAY_LABEL_MAX_LENGTH - 1)}…` : trimmed;
281
+ }
@@ -0,0 +1,350 @@
1
+ import assert from "node:assert/strict";
2
+ import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import test from "node:test";
6
+
7
+ import { sessionStoragePaths } from "./session-identity.js";
8
+ import { migrateSessionTranscripts, planSessionTranscriptMigration } from "./session-transcript-migration.js";
9
+
10
+ async function makeMemoryDir(): Promise<string> {
11
+ return mkdtemp(path.join(os.tmpdir(), "remnic-sess-mig-"));
12
+ }
13
+
14
+ function entryLine(sessionKey: string, turnId: string, role: "user" | "assistant"): string {
15
+ return JSON.stringify({
16
+ sessionKey,
17
+ turnId,
18
+ role,
19
+ content: `content-${turnId}`,
20
+ timestamp: `2026-06-29T10:0${turnId}:00.000Z`,
21
+ });
22
+ }
23
+
24
+ async function seedMixedOtherDefault(memoryDir: string, fileName: string, lines: string[]): Promise<string> {
25
+ const dir = path.join(memoryDir, "transcripts", "other", "default");
26
+ await mkdir(dir, { recursive: true });
27
+ const filePath = path.join(dir, fileName);
28
+ await writeFile(filePath, `${lines.join("\n")}\n`, "utf-8");
29
+ return filePath;
30
+ }
31
+
32
+ test("dry-run reports expected splits for mixed other/default file and changes nothing", async () => {
33
+ const memoryDir = await makeMemoryDir();
34
+ try {
35
+ const fileName = "2026-06-29.jsonl";
36
+ const lines = [
37
+ entryLine("pi-geek:abc123", "1", "user"),
38
+ entryLine("pi-friend:def456", "2", "user"),
39
+ entryLine("pi-geek:abc123", "3", "assistant"),
40
+ ];
41
+ const sourcePath = await seedMixedOtherDefault(memoryDir, fileName, lines);
42
+
43
+ const plan = await planSessionTranscriptMigration({ memoryDir });
44
+ assert.equal(plan.dryRun, true);
45
+ assert.equal(plan.files.length, 1);
46
+ assert.equal(plan.distinctSessions, 2);
47
+ assert.equal(plan.movedEntries, 3);
48
+
49
+ const groupKeys = plan.files[0].groups.map((g) => g.sessionKey).sort();
50
+ assert.deepEqual(groupKeys, ["pi-friend:def456", "pi-geek:abc123"]);
51
+
52
+ // Nothing moved.
53
+ const before = await readFile(sourcePath, "utf-8");
54
+ assert.equal(before.split("\n").filter(Boolean).length, 3);
55
+ const sessionDir = path.join(memoryDir, "transcripts", "session");
56
+ await assert.rejects(() => readdir(sessionDir));
57
+ } finally {
58
+ await rm(memoryDir, { recursive: true, force: true });
59
+ }
60
+ });
61
+
62
+ test("apply is lossless: every entry lands in its session dir, source is emptied", async () => {
63
+ const memoryDir = await makeMemoryDir();
64
+ try {
65
+ const fileName = "2026-06-29.jsonl";
66
+ const lines = [
67
+ entryLine("pi-geek:abc123", "1", "user"),
68
+ entryLine("pi-friend:def456", "2", "user"),
69
+ entryLine("pi-geek:abc123", "3", "assistant"),
70
+ ];
71
+ await seedMixedOtherDefault(memoryDir, fileName, lines);
72
+
73
+ const result = await migrateSessionTranscripts({ memoryDir, apply: true });
74
+ assert.equal(result.applied, true);
75
+ assert.equal(result.errors.length, 0);
76
+
77
+ const geekDir = sessionStoragePaths("pi-geek:abc123").dir;
78
+ const friendDir = sessionStoragePaths("pi-friend:def456").dir;
79
+
80
+ const geekContent = await readFile(path.join(memoryDir, "transcripts", geekDir, fileName), "utf-8");
81
+ const friendContent = await readFile(path.join(memoryDir, "transcripts", friendDir, fileName), "utf-8");
82
+
83
+ const geekLines = geekContent.split("\n").filter(Boolean);
84
+ const friendLines = friendContent.split("\n").filter(Boolean);
85
+ assert.equal(geekLines.length, 2);
86
+ assert.equal(friendLines.length, 1);
87
+
88
+ // Ordering preserved within a session (turn 1 before turn 3).
89
+ assert.ok(geekLines[0].includes('"turnId":"1"'));
90
+ assert.ok(geekLines[1].includes('"turnId":"3"'));
91
+
92
+ // Source other/default file removed (no entries retained).
93
+ await assert.rejects(() => readFile(path.join(memoryDir, "transcripts", "other", "default", fileName), "utf-8"));
94
+
95
+ // No entries lost: 3 in, 3 out.
96
+ assert.equal(geekLines.length + friendLines.length, 3);
97
+ } finally {
98
+ await rm(memoryDir, { recursive: true, force: true });
99
+ }
100
+ });
101
+
102
+ test("apply is idempotent: a second run finds nothing to migrate and does not duplicate", async () => {
103
+ const memoryDir = await makeMemoryDir();
104
+ try {
105
+ const fileName = "2026-06-29.jsonl";
106
+ const lines = [entryLine("pi-geek:abc123", "1", "user"), entryLine("pi-geek:abc123", "2", "assistant")];
107
+ await seedMixedOtherDefault(memoryDir, fileName, lines);
108
+
109
+ const first = await migrateSessionTranscripts({ memoryDir, apply: true });
110
+ assert.equal(first.errors.length, 0);
111
+
112
+ const second = await migrateSessionTranscripts({ memoryDir, apply: true });
113
+ assert.equal(second.errors.length, 0);
114
+ assert.equal(second.plan.files.length, 0);
115
+ assert.equal(second.plan.movedEntries, 0);
116
+
117
+ const geekDir = sessionStoragePaths("pi-geek:abc123").dir;
118
+ const geekContent = await readFile(path.join(memoryDir, "transcripts", geekDir, fileName), "utf-8");
119
+ // Still exactly 2 lines — no duplication.
120
+ assert.equal(geekContent.split("\n").filter(Boolean).length, 2);
121
+ } finally {
122
+ await rm(memoryDir, { recursive: true, force: true });
123
+ }
124
+ });
125
+
126
+ test("unparseable and legacy-homed lines are retained in the source file", async () => {
127
+ const memoryDir = await makeMemoryDir();
128
+ try {
129
+ const fileName = "2026-06-29.jsonl";
130
+ // One arbitrary key (movable), one malformed line (retained).
131
+ const lines = [entryLine("pi-geek:abc123", "1", "user"), "{not valid json"];
132
+ await seedMixedOtherDefault(memoryDir, fileName, lines);
133
+
134
+ const result = await migrateSessionTranscripts({ memoryDir, apply: true });
135
+ assert.equal(result.errors.length, 0);
136
+
137
+ // Source retains the malformed line (not deleted).
138
+ const sourceContent = await readFile(path.join(memoryDir, "transcripts", "other", "default", fileName), "utf-8");
139
+ assert.ok(sourceContent.includes("{not valid json"));
140
+
141
+ // The movable entry landed in its session dir.
142
+ const geekDir = sessionStoragePaths("pi-geek:abc123").dir;
143
+ const geekContent = await readFile(path.join(memoryDir, "transcripts", geekDir, fileName), "utf-8");
144
+ assert.equal(geekContent.split("\n").filter(Boolean).length, 1);
145
+ } finally {
146
+ await rm(memoryDir, { recursive: true, force: true });
147
+ }
148
+ });
149
+
150
+ async function seedLegacyParserDir(
151
+ memoryDir: string,
152
+ channelType: string,
153
+ channelId: string,
154
+ fileName: string,
155
+ lines: string[]
156
+ ): Promise<string> {
157
+ const dir = path.join(memoryDir, "transcripts", channelType, channelId);
158
+ await mkdir(dir, { recursive: true });
159
+ const filePath = path.join(dir, fileName);
160
+ await writeFile(filePath, `${lines.join("\n")}\n`, "utf-8");
161
+ return filePath;
162
+ }
163
+
164
+ test("migration scan picks up pre-existing foo:bar:baz data under old baz/default", async () => {
165
+ const memoryDir = await makeMemoryDir();
166
+ try {
167
+ const fileName = "2026-06-29.jsonl";
168
+ // OLD build stored foo:bar:baz under baz/default (parts.length >= 3 parser).
169
+ const sourcePath = await seedLegacyParserDir(memoryDir, "baz", "default", fileName, [
170
+ entryLine("foo:bar:baz", "1", "user"),
171
+ entryLine("foo:bar:baz", "2", "assistant"),
172
+ ]);
173
+
174
+ const plan = await planSessionTranscriptMigration({ memoryDir });
175
+ assert.equal(plan.files.length, 1, "baz/default must be a migration candidate");
176
+ assert.equal(plan.distinctSessions, 1);
177
+ assert.equal(plan.movedEntries, 2);
178
+
179
+ const result = await migrateSessionTranscripts({ memoryDir, apply: true });
180
+ assert.equal(result.errors.length, 0);
181
+
182
+ const destDir = sessionStoragePaths("foo:bar:baz").dir;
183
+ const destContent = await readFile(path.join(memoryDir, "transcripts", destDir, fileName), "utf-8");
184
+ assert.equal(destContent.split("\n").filter(Boolean).length, 2);
185
+
186
+ // Source emptied/removed.
187
+ await assert.rejects(() => readFile(sourcePath, "utf-8"));
188
+
189
+ // Idempotent: a second run finds nothing.
190
+ const second = await migrateSessionTranscripts({ memoryDir, apply: true });
191
+ assert.equal(second.plan.files.length, 0);
192
+ assert.equal(second.plan.movedEntries, 0);
193
+ } finally {
194
+ await rm(memoryDir, { recursive: true, force: true });
195
+ }
196
+ });
197
+
198
+ test("migration leaves legitimate legacy agent:<id>:main data in place (dest === source)", async () => {
199
+ const memoryDir = await makeMemoryDir();
200
+ try {
201
+ const fileName = "2026-06-29.jsonl";
202
+ // agent:generalist:main legitimately lives in main/default and must NOT move.
203
+ await seedLegacyParserDir(memoryDir, "main", "default", fileName, [
204
+ entryLine("agent:generalist:main", "1", "user"),
205
+ ]);
206
+
207
+ const plan = await planSessionTranscriptMigration({ memoryDir });
208
+ assert.equal(plan.files.length, 0, "legacy agent data must not be a migration source");
209
+ assert.equal(plan.movedEntries, 0);
210
+
211
+ const result = await migrateSessionTranscripts({ memoryDir, apply: true });
212
+ assert.equal(result.errors.length, 0);
213
+ const stillThere = await readFile(path.join(memoryDir, "transcripts", "main", "default", fileName), "utf-8");
214
+ assert.equal(stillThere.split("\n").filter(Boolean).length, 1);
215
+ } finally {
216
+ await rm(memoryDir, { recursive: true, force: true });
217
+ }
218
+ });
219
+
220
+ test("migration never treats session/<hash> dirs as sources", async () => {
221
+ const memoryDir = await makeMemoryDir();
222
+ try {
223
+ const fileName = "2026-06-29.jsonl";
224
+ const sessionKey = "pi-geek:abc123";
225
+ const destDir = sessionStoragePaths(sessionKey).dir; // session/<hash>
226
+ const [type, id] = destDir.split("/");
227
+ await seedLegacyParserDir(memoryDir, type, id, fileName, [entryLine(sessionKey, "1", "user")]);
228
+
229
+ const plan = await planSessionTranscriptMigration({ memoryDir });
230
+ assert.equal(plan.files.length, 0, "already-homed session/<hash> must never be scanned");
231
+ } finally {
232
+ await rm(memoryDir, { recursive: true, force: true });
233
+ }
234
+ });
235
+
236
+ test("migration splits a LEGACY session/<name> dir while leaving real session/<hash> untouched", async () => {
237
+ // Thread 3 (codex review on PR #1504): pre-#1496 the OLD parser stored a key
238
+ // whose 3rd colon segment was literally `session` (e.g. foo:bar:session:baz)
239
+ // under transcripts/session/baz — a NON-hash id. The new layer maps that key
240
+ // to session/<hash>, but the migration previously BLANKET-SKIPPED everything
241
+ // under session/, so the legacy session/baz dir was never scanned. Only the
242
+ // canonical 16-hex session/<hash> dirs must be skipped.
243
+ const memoryDir = await makeMemoryDir();
244
+ try {
245
+ const fileName = "2026-06-29.jsonl";
246
+ const legacyKey = "foo:bar:session:baz";
247
+
248
+ // LEGACY: OLD parser homed foo:bar:session:baz under session/baz (non-hash).
249
+ const legacySourcePath = await seedLegacyParserDir(memoryDir, "session", "baz", fileName, [
250
+ entryLine(legacyKey, "1", "user"),
251
+ entryLine(legacyKey, "2", "assistant"),
252
+ ]);
253
+
254
+ // CANONICAL: a genuinely-homed session/<hash> dir for a different key.
255
+ const homedKey = "pi-geek:abc123";
256
+ const homedDir = sessionStoragePaths(homedKey).dir; // session/<16hex>
257
+ const [homedType, homedId] = homedDir.split("/");
258
+ assert.match(homedId, /^[0-9a-f]{16}$/);
259
+ await seedLegacyParserDir(memoryDir, homedType, homedId, fileName, [
260
+ entryLine(homedKey, "1", "user"),
261
+ ]);
262
+ const homedContentBefore = await readFile(path.join(memoryDir, "transcripts", homedDir, fileName), "utf-8");
263
+
264
+ // Plan: only the legacy session/baz dir is a source; session/<hash> is left.
265
+ const plan = await planSessionTranscriptMigration({ memoryDir });
266
+ assert.equal(plan.files.length, 1, "only the legacy session/<name> dir must be a source");
267
+ assert.equal(plan.distinctSessions, 1);
268
+ assert.equal(plan.movedEntries, 2);
269
+ assert.equal(plan.files[0].sourceRelPath, path.join("session", "baz", fileName));
270
+
271
+ const result = await migrateSessionTranscripts({ memoryDir, apply: true });
272
+ assert.equal(result.errors.length, 0);
273
+
274
+ // Legacy data re-homed to its canonical session/<hash> dir, order preserved.
275
+ const legacyDestDir = sessionStoragePaths(legacyKey).dir;
276
+ assert.notEqual(legacyDestDir, path.join("session", "baz"));
277
+ const legacyDestContent = await readFile(
278
+ path.join(memoryDir, "transcripts", legacyDestDir, fileName),
279
+ "utf-8",
280
+ );
281
+ const legacyDestLines = legacyDestContent.split("\n").filter(Boolean);
282
+ assert.equal(legacyDestLines.length, 2);
283
+ assert.ok(legacyDestLines[0].includes('"turnId":"1"'));
284
+ assert.ok(legacyDestLines[1].includes('"turnId":"2"'));
285
+
286
+ // Legacy source emptied/removed (lossless move).
287
+ await assert.rejects(() => readFile(legacySourcePath, "utf-8"));
288
+
289
+ // The real session/<hash> dir is byte-for-byte untouched.
290
+ const homedContentAfter = await readFile(path.join(memoryDir, "transcripts", homedDir, fileName), "utf-8");
291
+ assert.equal(homedContentAfter, homedContentBefore);
292
+
293
+ // Idempotent: a second run finds nothing and does not duplicate.
294
+ const second = await migrateSessionTranscripts({ memoryDir, apply: true });
295
+ assert.equal(second.plan.files.length, 0);
296
+ assert.equal(second.plan.movedEntries, 0);
297
+ const reread = await readFile(path.join(memoryDir, "transcripts", legacyDestDir, fileName), "utf-8");
298
+ assert.equal(reread.split("\n").filter(Boolean).length, 2);
299
+ } finally {
300
+ await rm(memoryDir, { recursive: true, force: true });
301
+ }
302
+ });
303
+
304
+ test("migration error detail is sanitized (no raw filesystem/stack message leak)", async () => {
305
+ // Thread 2 (cursor review on PR #1504): the per-file catch must route
306
+ // operator-facing strings (CLI output + audit manifest) through the shared
307
+ // displayErrorDetail() sanitizer so a raw fs path or stack detail cannot leak.
308
+ const memoryDir = await makeMemoryDir();
309
+ try {
310
+ const fileName = "2026-06-29.jsonl";
311
+ // Arbitrary key routes to transcripts/session/<hash>. Planting a FILE at
312
+ // transcripts/session makes the destination `mkdir` fail with ENOTDIR — a
313
+ // realistic write-time failure whose raw OS message embeds an absolute path.
314
+ await seedMixedOtherDefault(memoryDir, fileName, [entryLine("pi-geek:abc123", "1", "user")]);
315
+ await writeFile(path.join(memoryDir, "transcripts", "session"), "blocker", "utf-8");
316
+
317
+ const result = await migrateSessionTranscripts({ memoryDir, apply: true });
318
+
319
+ assert.equal(result.errors.length, 1);
320
+ const message = result.errors[0];
321
+ // Sanitized: includes the relative source path + the error name/code only.
322
+ assert.ok(
323
+ message.startsWith(`Failed to migrate ${path.join("other", "default", fileName)}`),
324
+ `unexpected error prefix: ${message}`,
325
+ );
326
+ assert.match(message, /\(ENOTDIR\)/);
327
+ // Must NOT leak the raw OS message or any absolute filesystem path.
328
+ assert.ok(!message.includes(memoryDir), "sanitized error must not leak an absolute fs path");
329
+ assert.ok(
330
+ !/not a directory|open '|write '/i.test(message),
331
+ `sanitized error must not leak the raw OS message: ${message}`,
332
+ );
333
+ } finally {
334
+ await rm(memoryDir, { recursive: true, force: true });
335
+ }
336
+ });
337
+
338
+ test("apply writes an audit manifest", async () => {
339
+ const memoryDir = await makeMemoryDir();
340
+ try {
341
+ await seedMixedOtherDefault(memoryDir, "2026-06-29.jsonl", [entryLine("pi-geek:abc123", "1", "user")]);
342
+ const result = await migrateSessionTranscripts({ memoryDir, apply: true });
343
+ const manifestPath = result.manifestPath;
344
+ assert.ok(manifestPath);
345
+ const manifest = JSON.parse(await readFile(manifestPath, "utf-8"));
346
+ assert.equal(manifest.applied, true);
347
+ } finally {
348
+ await rm(memoryDir, { recursive: true, force: true });
349
+ }
350
+ });