notesoup 0.54.0 → 0.54.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/plugins/claude/src/helper.d.ts +2 -1
- package/dist/plugins/claude/src/helper.js +19 -23
- package/dist/plugins/claude/src/history.d.ts +3 -14
- package/dist/plugins/claude/src/history.js +2 -298
- package/dist/plugins/claude/src/text.d.ts +0 -1
- package/dist/plugins/claude/src/text.js +0 -1
- package/dist/plugins/codex/src/history.d.ts +3 -8
- package/dist/plugins/codex/src/history.js +2 -238
- package/dist/plugins/codex/src/mcp.js +7 -7
- package/dist/plugins/codex/src/transcript.d.ts +3 -39
- package/dist/plugins/codex/src/transcript.js +2 -313
- package/dist/plugins/dsh/history.d.ts +3 -0
- package/dist/plugins/dsh/history.js +15 -0
- package/dist/plugins/dsh/tools.js +4 -5
- package/dist/plugins/pi/history-tools.d.ts +1 -1
- package/dist/plugins/pi/history-tools.js +9 -5
- package/dist/plugins/pi/history.d.ts +2 -3
- package/dist/plugins/pi/history.js +4 -147
- package/dist/src/boot/render.js +2 -4
- package/dist/src/boot/text.d.ts +3 -1
- package/dist/src/boot/text.js +11 -5
- package/dist/src/history/claude.d.ts +16 -0
- package/dist/src/history/claude.js +336 -0
- package/dist/src/history/codex-transcript.d.ts +39 -0
- package/dist/src/history/codex-transcript.js +315 -0
- package/dist/src/history/codex.d.ts +8 -0
- package/dist/src/history/codex.js +238 -0
- package/dist/src/history/corpus.d.ts +35 -0
- package/dist/src/history/corpus.js +54 -0
- package/dist/src/history/history.d.ts +4 -8
- package/dist/src/history/history.js +3 -11
- package/dist/src/history/local.d.ts +17 -0
- package/dist/src/history/local.js +101 -0
- package/dist/src/history/pi.d.ts +36 -0
- package/dist/src/history/pi.js +202 -0
- package/dist/src/history/query.d.ts +15 -5
- package/dist/src/history/query.js +42 -13
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.js +4 -3
- package/dist/src/tools/history.d.ts +7 -19
- package/dist/src/tools/history.js +80 -93
- package/dist/test/boot.integration.test.js +1 -2
- package/dist/test/claude-history.test.js +47 -34
- package/dist/test/claude-mod.test.js +7 -7
- package/dist/test/codex-history.test.js +4 -10
- package/dist/test/codex-plugin.test.js +32 -11
- package/dist/test/dependencies.test.js +3 -3
- package/dist/test/dsh.test.js +26 -4
- package/dist/test/helpers/extension.d.ts +0 -1
- package/dist/test/helpers/history-corpus.d.ts +7 -0
- package/dist/test/helpers/history-corpus.js +26 -0
- package/dist/test/history-corpus.test.d.ts +1 -0
- package/dist/test/history-corpus.test.js +307 -0
- package/dist/test/history.integration.test.js +40 -48
- package/dist/test/host-neutral.test.js +3 -18
- package/dist/test/model-facing-text.test.js +2 -0
- package/dist/test/notes.integration.test.js +1 -1
- package/dist/test/notes.test.js +1 -1
- package/docs/architecture.md +7 -5
- package/docs/claude-code-mod.md +7 -5
- package/docs/codex.md +3 -3
- package/docs/dsh.md +2 -2
- package/docs/history-sessions.md +52 -0
- package/docs/notes-library.md +1 -1
- package/docs/skills.md +1 -1
- package/docs/tool-results.md +8 -6
- package/package.json +3 -4
- package/plugins/claude/.claude-plugin/plugin.json +1 -1
- package/plugins/claude/dist/claude/helper.js +1992 -1231
- package/plugins/claude/skills/memory/SKILL.md +5 -3
- package/plugins/claude/src/helper.ts +19 -23
- package/plugins/claude/src/history.ts +3 -257
- package/plugins/claude/src/text.ts +0 -1
- package/plugins/codex/dist/codex/notesoup.js +1364 -813
- package/plugins/codex/src/history.ts +3 -193
- package/plugins/codex/src/mcp.ts +7 -7
- package/plugins/codex/src/transcript.ts +3 -275
- package/plugins/dsh/history.ts +17 -0
- package/plugins/dsh/tools.ts +4 -5
- package/plugins/paseo/client/markdown.tsx +19 -39
- package/plugins/paseo/client/notes-op-card.tsx +1 -1
- package/plugins/pi/history-tools.ts +11 -5
- package/plugins/pi/history.ts +8 -157
- package/skills/memory/SKILL.md +5 -3
- package/src/boot/render.ts +2 -5
- package/src/boot/text.ts +11 -5
- package/src/history/claude.ts +286 -0
- package/src/history/codex-transcript.ts +276 -0
- package/src/history/codex.ts +193 -0
- package/src/history/corpus.ts +69 -0
- package/src/history/history.ts +5 -12
- package/src/history/local.ts +93 -0
- package/src/history/pi.ts +199 -0
- package/src/history/query.ts +45 -15
- package/src/index.ts +5 -3
- package/src/tools/history.ts +71 -101
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/** Claude's persisted main transcript -> the shared history projection. No inference-context API. */
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { isDeepStrictEqual } from "node:util";
|
|
4
|
+
import { contentText, projectHistory } from "./history.js";
|
|
5
|
+
const MESSAGE_TYPES = new Set(["user", "assistant", "system", "attachment"]);
|
|
6
|
+
const NON_TEXT_BLOCKS = new Set(["thinking", "redacted_thinking", "image", "document", "audio", "video", "server_tool_use", "web_search_tool_result", "tool_reference"]);
|
|
7
|
+
function object(value) { return typeof value === "object" && value !== null && !Array.isArray(value); }
|
|
8
|
+
function requiredString(value, field) {
|
|
9
|
+
if (typeof value !== "string" || value.length === 0)
|
|
10
|
+
throw new Error(`invalid Claude transcript ${field}`);
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
function parent(row) {
|
|
14
|
+
if (row.parentUuid === null)
|
|
15
|
+
return undefined;
|
|
16
|
+
return requiredString(row.parentUuid, "parentUuid");
|
|
17
|
+
}
|
|
18
|
+
function boundary(row) { return row.type === "system" && row.subtype === "compact_boundary"; }
|
|
19
|
+
/** A trailing incomplete write is not a committed line. Valid unterminated JSON is still readable. */
|
|
20
|
+
function rowsOf(text) {
|
|
21
|
+
const lines = text.split("\n"), rows = [];
|
|
22
|
+
for (let index = 0; index < lines.length; index++) {
|
|
23
|
+
const line = lines[index];
|
|
24
|
+
if (line.trim() === "")
|
|
25
|
+
continue;
|
|
26
|
+
let value;
|
|
27
|
+
try {
|
|
28
|
+
value = JSON.parse(line);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
if (index === lines.length - 1 && !text.endsWith("\n"))
|
|
32
|
+
break;
|
|
33
|
+
throw new Error(`malformed Claude transcript JSON at line ${index + 1}`);
|
|
34
|
+
}
|
|
35
|
+
if (!object(value) || typeof value.type !== "string")
|
|
36
|
+
throw new Error(`invalid Claude transcript record at line ${index + 1}`);
|
|
37
|
+
rows.push(value);
|
|
38
|
+
}
|
|
39
|
+
return rows;
|
|
40
|
+
}
|
|
41
|
+
/** Only recorded visible text; never stringify unknown blocks or attachment internals (thinking included). */
|
|
42
|
+
function blocksOf(value) {
|
|
43
|
+
if (typeof value === "string")
|
|
44
|
+
return [{ type: "text", text: value }];
|
|
45
|
+
if (!Array.isArray(value))
|
|
46
|
+
throw new Error("invalid Claude transcript message content");
|
|
47
|
+
return value.map((block) => {
|
|
48
|
+
if (!object(block) || typeof block.type !== "string")
|
|
49
|
+
throw new Error("invalid Claude transcript content block");
|
|
50
|
+
if (block.type === "text") {
|
|
51
|
+
if (typeof block.text !== "string")
|
|
52
|
+
throw new Error("invalid Claude transcript text block");
|
|
53
|
+
}
|
|
54
|
+
else if (block.type !== "tool_use" && block.type !== "tool_result" && !NON_TEXT_BLOCKS.has(block.type)) {
|
|
55
|
+
throw new Error(`unsupported Claude transcript content block: ${block.type}`);
|
|
56
|
+
}
|
|
57
|
+
return block;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function decode(row, seq) {
|
|
61
|
+
// A system record without a body is graph metadata, not a message.
|
|
62
|
+
if (row.type === "system" && row.content == null)
|
|
63
|
+
return [];
|
|
64
|
+
const common = { windowId: "", ...(typeof row.timestamp === "string" ? { createdAt: row.timestamp } : {}) };
|
|
65
|
+
let blocks, role;
|
|
66
|
+
if (row.type === "user" || row.type === "assistant") {
|
|
67
|
+
if (!object(row.message) || row.message.role !== row.type)
|
|
68
|
+
throw new Error("invalid Claude transcript message role");
|
|
69
|
+
blocks = blocksOf(row.message.content);
|
|
70
|
+
role = row.isCompactSummary === true || row.isMeta === true ? "system" : row.type;
|
|
71
|
+
}
|
|
72
|
+
else if (row.type === "system") {
|
|
73
|
+
blocks = blocksOf(row.content);
|
|
74
|
+
role = "system";
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// Old/new attachments are graph nodes even when they put no bytes in the request.
|
|
78
|
+
// `rendered` is a list of native rendered messages, not raw attachment payloads.
|
|
79
|
+
if (row.rendered === undefined)
|
|
80
|
+
blocks = [];
|
|
81
|
+
else if (Array.isArray(row.rendered))
|
|
82
|
+
blocks = row.rendered.flatMap((message) => {
|
|
83
|
+
if (!object(message))
|
|
84
|
+
throw new Error("invalid Claude transcript rendered attachment");
|
|
85
|
+
return blocksOf(message.content);
|
|
86
|
+
});
|
|
87
|
+
else
|
|
88
|
+
throw new Error("invalid Claude transcript rendered attachment");
|
|
89
|
+
role = "system";
|
|
90
|
+
}
|
|
91
|
+
const items = [{ ...common, seq, role, text: contentText(blocks) }];
|
|
92
|
+
for (const block of blocks) {
|
|
93
|
+
if (block.type === "tool_use") {
|
|
94
|
+
if (!object(block.input))
|
|
95
|
+
throw new Error("invalid Claude transcript tool arguments");
|
|
96
|
+
items.push({ ...common, seq: seq + items.length, role: "tool_call", text: "", toolName: requiredString(block.name, "tool name"), toolCallId: requiredString(block.id, "tool id"), arguments: block.input });
|
|
97
|
+
}
|
|
98
|
+
else if (block.type === "tool_result") {
|
|
99
|
+
if (block.is_error !== undefined && typeof block.is_error !== "boolean")
|
|
100
|
+
throw new Error("invalid Claude transcript tool error flag");
|
|
101
|
+
const output = block.content === undefined ? "" : contentText(blocksOf(block.content));
|
|
102
|
+
items.push({ ...common, seq: seq + items.length, role: "tool", text: output, toolCallId: requiredString(block.tool_use_id, "tool result id"), toolError: block.is_error === true });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return items;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The protocol tags a complete boot carries; `context_window` is the pre-rename spelling that
|
|
109
|
+
* sessions and Claude transcripts already on disk still use, so both stay readable.
|
|
110
|
+
*/
|
|
111
|
+
const BOOT_IDENTITY = /^<(?:notesoup:window_identity|context_window)>\nAgent name: claude \(/;
|
|
112
|
+
const BOOT_PROTOCOL_OPEN = /\n<(?:notesoup:notes_protocol|context_window_protocol)>\n/;
|
|
113
|
+
const BOOT_PROTOCOL_CLOSE = /\n<\/(?:notesoup:notes_protocol|context_window_protocol)>/;
|
|
114
|
+
/** Match the complete retained notesoup boot, not a casually quoted window-id line. */
|
|
115
|
+
function bootOrdinal(row, retained) {
|
|
116
|
+
if (!retained || row.type !== "user" || !object(row.message))
|
|
117
|
+
return undefined;
|
|
118
|
+
const text = contentText(row.message.content);
|
|
119
|
+
if (!BOOT_IDENTITY.test(text) || !BOOT_PROTOCOL_OPEN.test(text) || !BOOT_PROTOCOL_CLOSE.test(text))
|
|
120
|
+
return undefined;
|
|
121
|
+
// A fork copies the retained boot verbatim. Its ordinal survives, while
|
|
122
|
+
// public window ids below always belong to the current transcript session.
|
|
123
|
+
const suffix = /^Current context window id: claude:[^\n]+:(\d+)$/m.exec(text)?.[1];
|
|
124
|
+
if (suffix === undefined)
|
|
125
|
+
return undefined;
|
|
126
|
+
const ordinal = Number(suffix);
|
|
127
|
+
return Number.isSafeInteger(ordinal) ? ordinal : undefined;
|
|
128
|
+
}
|
|
129
|
+
/** All addresses are allocated on the full first-occurrence log, before branch selection. */
|
|
130
|
+
export function claudeHistoryFromJsonl(text, source) {
|
|
131
|
+
const nodes = new Map();
|
|
132
|
+
let nextSeq = 1;
|
|
133
|
+
for (const row of rowsOf(text)) {
|
|
134
|
+
// Ignore non-message records, including future metadata. If selected ancestry
|
|
135
|
+
// reaches a skipped UUID, it becomes a gap, never permission to expose its payload.
|
|
136
|
+
if (!MESSAGE_TYPES.has(row.type))
|
|
137
|
+
continue;
|
|
138
|
+
const uuid = requiredString(row.uuid, "uuid");
|
|
139
|
+
parent(row);
|
|
140
|
+
if (row.sessionId !== source.sessionId)
|
|
141
|
+
throw new Error("Claude transcript belongs to a different session");
|
|
142
|
+
if (row.isSidechain === true || row.agentId !== undefined)
|
|
143
|
+
throw new Error("Claude history supports only the main session transcript");
|
|
144
|
+
// Resolve the window in the graph as it existed at this append, not the final
|
|
145
|
+
// rewritten tree. A preserved old row stays in its original window; a NEW child
|
|
146
|
+
// of its rewritten version belongs to the new window.
|
|
147
|
+
const ancestor = boundary(row) && typeof row.logicalParentUuid === "string" ? row.logicalParentUuid : parent(row);
|
|
148
|
+
const version = { row, ancestorUuid: ancestor, ancestor: ancestor === undefined ? undefined : nodes.get(ancestor)?.latest };
|
|
149
|
+
const previous = nodes.get(uuid);
|
|
150
|
+
if (previous) {
|
|
151
|
+
// Native resume/compaction rewrites provenance and parent links, not logical identities.
|
|
152
|
+
if (previous.first.row.type !== row.type || !["message", "content", "rendered"].every((field) => isDeepStrictEqual(previous.first.row[field], row[field]))) {
|
|
153
|
+
throw new Error(`conflicting Claude transcript duplicate UUID: ${uuid}`);
|
|
154
|
+
}
|
|
155
|
+
previous.latest = version;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const items = decode(row, nextSeq);
|
|
159
|
+
nextSeq += items.length;
|
|
160
|
+
nodes.set(uuid, { first: version, latest: version, items });
|
|
161
|
+
}
|
|
162
|
+
const missingAncestors = new Set();
|
|
163
|
+
function positionOf(version) {
|
|
164
|
+
// Resolve long chains iteratively. A gap destroys ordinal evidence, not the
|
|
165
|
+
// confirmed suffix. Only a retained boot can re-anchor that suffix's ordinal.
|
|
166
|
+
const chain = [], calculating = new Set();
|
|
167
|
+
let cursor = version;
|
|
168
|
+
let before = { ordinal: 0 };
|
|
169
|
+
while (cursor.position === undefined) {
|
|
170
|
+
// Forward references use the parent's first eventual version, not a rewrite.
|
|
171
|
+
cursor.ancestor ??= cursor.ancestorUuid === undefined ? undefined : nodes.get(cursor.ancestorUuid)?.first;
|
|
172
|
+
if (calculating.has(cursor))
|
|
173
|
+
throw new Error("cyclic Claude transcript ancestry");
|
|
174
|
+
calculating.add(cursor);
|
|
175
|
+
chain.push(cursor);
|
|
176
|
+
if (cursor.ancestorUuid !== undefined && cursor.ancestor === undefined) {
|
|
177
|
+
missingAncestors.add(cursor.ancestorUuid);
|
|
178
|
+
before = { unknownAncestor: cursor.ancestorUuid };
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
if (cursor.ancestor === undefined)
|
|
182
|
+
break;
|
|
183
|
+
cursor = cursor.ancestor;
|
|
184
|
+
}
|
|
185
|
+
before = cursor.position ?? before;
|
|
186
|
+
for (const current of chain.reverse()) {
|
|
187
|
+
const retained = current.row.isCompactSummary === true || (current.ancestor !== undefined && boundary(current.ancestor.row));
|
|
188
|
+
const marked = bootOrdinal(current.row, retained);
|
|
189
|
+
current.position = marked !== undefined
|
|
190
|
+
? { ordinal: "ordinal" in before ? Math.max(before.ordinal, marked) : marked }
|
|
191
|
+
: "ordinal" in before ? { ordinal: before.ordinal + (boundary(current.row) ? 1 : 0) } : before;
|
|
192
|
+
before = current.position;
|
|
193
|
+
}
|
|
194
|
+
return version.position;
|
|
195
|
+
}
|
|
196
|
+
if (!source.headUuid)
|
|
197
|
+
throw new Error("Claude history has no confirmed active head yet; wait for a main-session append after startup/resume");
|
|
198
|
+
if (!nodes.has(source.headUuid))
|
|
199
|
+
throw new Error("Claude history active head is not persisted yet; retry after the transcript write");
|
|
200
|
+
// The inference tree uses the latest rewritten parents. At a compact boundary the logical
|
|
201
|
+
// parent and preserved segments use ORIGINAL ancestry: the old context is still history.
|
|
202
|
+
const selected = new Set(), visited = new Set(), visiting = new Set();
|
|
203
|
+
const stack = [{ uuid: source.headUuid, historical: false }];
|
|
204
|
+
while (stack.length > 0) {
|
|
205
|
+
const step = stack.pop(), { uuid, historical } = step;
|
|
206
|
+
const key = `${historical ? "old" : "live"}:${uuid}`;
|
|
207
|
+
if (step.exit) {
|
|
208
|
+
visiting.delete(key);
|
|
209
|
+
visited.add(key);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (visited.has(key))
|
|
213
|
+
continue;
|
|
214
|
+
if (visiting.has(key))
|
|
215
|
+
throw new Error("cyclic Claude transcript ancestry");
|
|
216
|
+
visiting.add(key);
|
|
217
|
+
stack.push({ ...step, exit: true });
|
|
218
|
+
const node = nodes.get(uuid);
|
|
219
|
+
if (!node) {
|
|
220
|
+
missingAncestors.add(uuid);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
selected.add(uuid);
|
|
224
|
+
const row = (historical ? node.first : node.latest).row;
|
|
225
|
+
const ancestor = parent(row);
|
|
226
|
+
if (ancestor !== undefined)
|
|
227
|
+
stack.push({ uuid: ancestor, historical });
|
|
228
|
+
if (!boundary(row))
|
|
229
|
+
continue;
|
|
230
|
+
if (row.logicalParentUuid != null)
|
|
231
|
+
stack.push({ uuid: requiredString(row.logicalParentUuid, "logicalParentUuid"), historical: true });
|
|
232
|
+
const meta = row.compactMetadata;
|
|
233
|
+
if (!object(meta))
|
|
234
|
+
continue;
|
|
235
|
+
if (object(meta.preservedSegment)) {
|
|
236
|
+
for (const field of ["headUuid", "tailUuid"])
|
|
237
|
+
if (meta.preservedSegment[field] !== undefined)
|
|
238
|
+
stack.push({ uuid: requiredString(meta.preservedSegment[field], field), historical: true });
|
|
239
|
+
}
|
|
240
|
+
if (object(meta.preservedMessages)) {
|
|
241
|
+
const uuids = meta.preservedMessages.allUuids ?? meta.preservedMessages.uuids;
|
|
242
|
+
if (uuids === undefined)
|
|
243
|
+
continue;
|
|
244
|
+
if (!Array.isArray(uuids))
|
|
245
|
+
throw new Error("invalid Claude preserved messages");
|
|
246
|
+
for (const id of uuids) {
|
|
247
|
+
const uuid = requiredString(id, "preserved UUID");
|
|
248
|
+
// allUuids also names virtual/non-persisted attachments. It is auxiliary
|
|
249
|
+
// evidence, not a reason to pretend the stored parent chain is missing.
|
|
250
|
+
if (nodes.has(uuid))
|
|
251
|
+
stack.push({ uuid, historical: true });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const current = positionOf(nodes.get(source.headUuid).latest);
|
|
256
|
+
const windowId = (position) => `claude:${source.sessionId}:${"ordinal" in position ? position.ordinal : `unknown:${position.unknownAncestor}`}`;
|
|
257
|
+
const windowsById = new Map();
|
|
258
|
+
const branchSeqs = new Set();
|
|
259
|
+
let maxOrdinal = "ordinal" in current ? current.ordinal : 0;
|
|
260
|
+
for (const [uuid, node] of nodes) {
|
|
261
|
+
if (!selected.has(uuid))
|
|
262
|
+
continue;
|
|
263
|
+
const position = positionOf(node.first);
|
|
264
|
+
if ("ordinal" in position)
|
|
265
|
+
maxOrdinal = Math.max(maxOrdinal, position.ordinal);
|
|
266
|
+
const id = windowId(position);
|
|
267
|
+
let window = windowsById.get(id);
|
|
268
|
+
if (!window) {
|
|
269
|
+
window = { windowId: id, ...("ordinal" in position ? { ordinal: position.ordinal } : {}), items: [] };
|
|
270
|
+
windowsById.set(id, window);
|
|
271
|
+
}
|
|
272
|
+
window.createdAt ??= node.items[0]?.createdAt;
|
|
273
|
+
for (const item of node.items) {
|
|
274
|
+
branchSeqs.add(item.seq);
|
|
275
|
+
window.items.push({ ...item, windowId: id });
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// Preserve complete-history empty windows (including copied boot ordinals).
|
|
279
|
+
// With gaps, report only evidenced windows, never invent the missing ordinals.
|
|
280
|
+
if (missingAncestors.size === 0) {
|
|
281
|
+
for (let ordinal = 0; ordinal <= maxOrdinal; ordinal++) {
|
|
282
|
+
const id = windowId({ ordinal });
|
|
283
|
+
if (!windowsById.has(id))
|
|
284
|
+
windowsById.set(id, { windowId: id, ordinal, items: [] });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
// Forward references can put a newer numbered window on disk first. Unknown
|
|
288
|
+
// segments are a separate, unordered group, not an inferred place in that timeline.
|
|
289
|
+
const windows = [...windowsById.values()].sort((a, b) => a.ordinal === undefined
|
|
290
|
+
? b.ordinal === undefined ? 0 : 1
|
|
291
|
+
: b.ordinal === undefined ? -1 : a.ordinal - b.ordinal);
|
|
292
|
+
return { ...projectHistory(windows, nextSeq - 1, branchSeqs), missingAncestors: [...missingAncestors], currentWindowOrdinal: "ordinal" in current ? current.ordinal : undefined };
|
|
293
|
+
}
|
|
294
|
+
/** Offline files have no live append binding. A unique graph leaf proves one ancestry; never pick among siblings by file order. */
|
|
295
|
+
export function offlineClaudeHead(text) {
|
|
296
|
+
const latest = new Map();
|
|
297
|
+
for (const row of rowsOf(text)) {
|
|
298
|
+
if (MESSAGE_TYPES.has(row.type))
|
|
299
|
+
latest.set(requiredString(row.uuid, "uuid"), row);
|
|
300
|
+
}
|
|
301
|
+
const ancestors = new Set();
|
|
302
|
+
for (const row of latest.values()) {
|
|
303
|
+
const id = parent(row);
|
|
304
|
+
if (id !== undefined)
|
|
305
|
+
ancestors.add(id);
|
|
306
|
+
if (!boundary(row))
|
|
307
|
+
continue;
|
|
308
|
+
if (typeof row.logicalParentUuid === "string")
|
|
309
|
+
ancestors.add(row.logicalParentUuid);
|
|
310
|
+
const meta = row.compactMetadata;
|
|
311
|
+
if (!object(meta))
|
|
312
|
+
continue;
|
|
313
|
+
if (object(meta.preservedSegment)) {
|
|
314
|
+
for (const field of ["headUuid", "tailUuid"]) {
|
|
315
|
+
if (typeof meta.preservedSegment[field] === "string")
|
|
316
|
+
ancestors.add(meta.preservedSegment[field]);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (object(meta.preservedMessages)) {
|
|
320
|
+
const uuids = meta.preservedMessages.allUuids ?? meta.preservedMessages.uuids;
|
|
321
|
+
if (Array.isArray(uuids))
|
|
322
|
+
for (const uuid of uuids)
|
|
323
|
+
if (typeof uuid === "string")
|
|
324
|
+
ancestors.add(uuid);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const leaves = [...latest.keys()].filter((uuid) => !ancestors.has(uuid));
|
|
328
|
+
if (leaves.length !== 1)
|
|
329
|
+
throw new Error(`Offline Claude transcript has ${leaves.length} possible heads; no confirmed unique leaf (refusing to guess a selected branch)`);
|
|
330
|
+
return leaves[0];
|
|
331
|
+
}
|
|
332
|
+
export async function readClaudeHistory(source) {
|
|
333
|
+
if (!source || typeof source.transcriptPath !== "string" || !source.transcriptPath || typeof source.sessionId !== "string")
|
|
334
|
+
throw new Error("Claude history transcript_path is unavailable");
|
|
335
|
+
return claudeHistoryFromJsonl(await readFile(source.transcriptPath, "utf8"), source);
|
|
336
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type CodexTranscriptSource = {
|
|
2
|
+
transcriptPath: string;
|
|
3
|
+
threadId: string;
|
|
4
|
+
};
|
|
5
|
+
export type CodexTranscriptState = {
|
|
6
|
+
threadId: string;
|
|
7
|
+
rootSessionId: string;
|
|
8
|
+
cliVersion: string;
|
|
9
|
+
cwd: string;
|
|
10
|
+
model?: string;
|
|
11
|
+
/** Native, UNPREFIXED identities, also used by MCP _meta.windowId. */
|
|
12
|
+
currentWindowId?: string;
|
|
13
|
+
currentWindowNumber?: number;
|
|
14
|
+
firstWindowId?: string;
|
|
15
|
+
previousWindowId?: string;
|
|
16
|
+
};
|
|
17
|
+
export type NativeObject = Record<string, unknown>;
|
|
18
|
+
export type CodexRecord = {
|
|
19
|
+
type: string;
|
|
20
|
+
payload: NativeObject;
|
|
21
|
+
metadata?: NativeObject;
|
|
22
|
+
timestamp: string;
|
|
23
|
+
/** One record owns one seq, including records omitted from presentation. */
|
|
24
|
+
seq: number;
|
|
25
|
+
/** Metadata belongs to its physical segment, not the root-scoped session_id. */
|
|
26
|
+
owner: string;
|
|
27
|
+
/** Native window at this point, absent only when the producer did not persist it. */
|
|
28
|
+
windowId?: string;
|
|
29
|
+
};
|
|
30
|
+
export declare function nativeObject(value: unknown, field: string): NativeObject;
|
|
31
|
+
export declare function nativeString(value: unknown, field: string): string;
|
|
32
|
+
/** Internal shared read: both boot facts and history use exactly this lineage and window owner. */
|
|
33
|
+
export declare function readCodexTranscript(source: CodexTranscriptSource): Promise<{
|
|
34
|
+
state: CodexTranscriptState;
|
|
35
|
+
records: CodexRecord[];
|
|
36
|
+
highestSeq: number;
|
|
37
|
+
}>;
|
|
38
|
+
/** Same decoder and ownership as history; no independently guessed boot window. */
|
|
39
|
+
export declare function readCodexTranscriptState(source: CodexTranscriptSource): Promise<CodexTranscriptState>;
|