@howaboua/pi-notes-compaction 0.0.13 → 0.0.15
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/CHANGELOG.md +23 -0
- package/PCC-INTEGRATION.md +3 -1
- package/README.md +3 -1
- package/package.json +7 -7
- package/src/agent-identity.ts +112 -10
- package/src/attachment-sharing.ts +37 -6
- package/src/attachment-snapshot.ts +16 -6
- package/src/bridge.ts +1 -0
- package/src/context-sharing.ts +30 -1
- package/src/extension-hooks.ts +15 -2
- package/src/extension-sharing.ts +26 -11
- package/src/fresh-checkpoint.ts +30 -4
- package/src/fresh-notes.ts +44 -33
- package/src/history-search.ts +1 -1
- package/src/history-windows.ts +12 -2
- package/src/history.ts +7 -1
- package/src/index.ts +3 -0
- package/src/integrations.ts +24 -24
- package/src/model-roles.ts +28 -4
- package/src/note-result.ts +70 -0
- package/src/note-save-marker.ts +23 -9
- package/src/notes-lookup.ts +5 -2
- package/src/notes.ts +8 -3
- package/src/query-executor.ts +58 -19
- package/src/query-input.ts +12 -1
- package/src/remote.ts +26 -7
- package/src/saved-session-access.ts +117 -0
- package/src/saved-session-index.mjs +83 -0
- package/src/saved-session.mjs +241 -0
- package/src/saved-session.ts +37 -154
- package/src/saved-sharing-records.ts +41 -9
- package/src/shared-notes.ts +219 -69
- package/src/shared-session-reader.ts +35 -19
- package/src/sharing-service.ts +57 -9
- package/src/store.ts +10 -0
- package/src/task-boundary.ts +1 -1
- package/src/tool-contracts.ts +40 -27
- package/src/tools.ts +21 -7
- package/src/tree-archive-manifest.mjs +27 -0
- package/src/tree-archive.ts +6 -26
- package/src/window-message.ts +19 -8
- package/src/windows.ts +2 -2
- package/changelog.ts +0 -337
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { open, stat } from "node:fs/promises";
|
|
3
|
+
import {
|
|
4
|
+
isTreeArchiveManifestData,
|
|
5
|
+
MAX_ARCHIVE_DEPTH,
|
|
6
|
+
TREE_ARCHIVE_ENTRY_TYPE,
|
|
7
|
+
} from "./tree-archive-manifest.mjs";
|
|
8
|
+
|
|
9
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
10
|
+
function object(value) {
|
|
11
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
/** @param {unknown} value @returns {value is {type:"session",id:string}} */
|
|
14
|
+
function isSessionHeader(value) {
|
|
15
|
+
return (
|
|
16
|
+
object(value) &&
|
|
17
|
+
value["type"] === "session" &&
|
|
18
|
+
typeof value["id"] === "string"
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
/** @param {unknown} value @returns {value is import("@earendil-works/pi-coding-agent").SessionEntry} */
|
|
22
|
+
function isRecord(value) {
|
|
23
|
+
return (
|
|
24
|
+
object(value) &&
|
|
25
|
+
typeof value["type"] === "string" &&
|
|
26
|
+
typeof value["id"] === "string" &&
|
|
27
|
+
(value["parentId"] === null || typeof value["parentId"] === "string") &&
|
|
28
|
+
typeof value["timestamp"] === "string"
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
/** @param {unknown} value @returns {value is import("@earendil-works/pi-coding-agent").CustomEntry} */
|
|
32
|
+
function isCustom(value) {
|
|
33
|
+
return (
|
|
34
|
+
isRecord(value) &&
|
|
35
|
+
value.type === "custom" &&
|
|
36
|
+
typeof value.customType === "string" &&
|
|
37
|
+
"data" in value
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
/** @param {unknown} value @returns {value is import("@earendil-works/pi-coding-agent").BranchSummaryEntry} */
|
|
41
|
+
function isHandoff(value) {
|
|
42
|
+
return (
|
|
43
|
+
isRecord(value) &&
|
|
44
|
+
value.type === "branch_summary" &&
|
|
45
|
+
typeof value.fromId === "string" &&
|
|
46
|
+
typeof value.summary === "string" &&
|
|
47
|
+
object(value.details) &&
|
|
48
|
+
"codexContextNoteHandoff" in value.details
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
const NOTE_ENTRIES = new Set([
|
|
52
|
+
"notes-compaction:note:v1",
|
|
53
|
+
"notes-compaction:cache:v1",
|
|
54
|
+
"codex-context-note",
|
|
55
|
+
"codex-context-note-snapshot",
|
|
56
|
+
]);
|
|
57
|
+
const SHARING_ENTRIES = new Set([
|
|
58
|
+
"codex-context-agent",
|
|
59
|
+
"codex-context-backend",
|
|
60
|
+
"herdr-context-child",
|
|
61
|
+
"shepherdr-attachment-route",
|
|
62
|
+
"shepherdr-attachment-owner",
|
|
63
|
+
"shepherdr-attachment-detached",
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
/** @param {import("node:fs/promises").FileHandle} file @param {number} size @param {AbortSignal} [signal] */
|
|
67
|
+
async function* reverseChunks(file, size, signal) {
|
|
68
|
+
let position = size;
|
|
69
|
+
while (position > 0) {
|
|
70
|
+
signal?.throwIfAborted();
|
|
71
|
+
const length = Math.min(64 * 1024, position);
|
|
72
|
+
position -= length;
|
|
73
|
+
const chunk = Buffer.allocUnsafe(length);
|
|
74
|
+
let offset = 0;
|
|
75
|
+
while (offset < length) {
|
|
76
|
+
const { bytesRead } = await file.read(
|
|
77
|
+
chunk,
|
|
78
|
+
offset,
|
|
79
|
+
length - offset,
|
|
80
|
+
position + offset,
|
|
81
|
+
);
|
|
82
|
+
if (!bytesRead)
|
|
83
|
+
throw new Error(
|
|
84
|
+
"Saved notes changed during the read. Retry the lookup.",
|
|
85
|
+
);
|
|
86
|
+
offset += bytesRead;
|
|
87
|
+
}
|
|
88
|
+
yield chunk;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @param {import("node:fs/promises").FileHandle} file @param {number} size @param {import("node:crypto").Hash} hash @param {AbortSignal} [signal] */
|
|
93
|
+
async function* reverseLines(file, size, hash, signal) {
|
|
94
|
+
/** @type {Buffer[]} */
|
|
95
|
+
let parts = [];
|
|
96
|
+
for await (const chunk of reverseChunks(file, size, signal)) {
|
|
97
|
+
hash.update(chunk);
|
|
98
|
+
let end = chunk.length;
|
|
99
|
+
for (let index = chunk.length - 1; index >= 0; index--) {
|
|
100
|
+
if (chunk[index] !== 10) continue;
|
|
101
|
+
const tail = chunk.subarray(index + 1, end);
|
|
102
|
+
// Long JSONL records span many chunks. Scan each byte once and join
|
|
103
|
+
// fragments only at the newline, rather than recopying a growing line.
|
|
104
|
+
if (parts.length) {
|
|
105
|
+
parts.push(tail);
|
|
106
|
+
yield Buffer.concat(parts.reverse());
|
|
107
|
+
parts = [];
|
|
108
|
+
} else yield tail;
|
|
109
|
+
end = index;
|
|
110
|
+
}
|
|
111
|
+
if (end) parts.push(chunk.subarray(0, end));
|
|
112
|
+
}
|
|
113
|
+
yield Buffer.concat(parts.reverse());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Read only recorded session paths. Identity is session-wide; notes follow the active parent chain. */
|
|
117
|
+
/** @param {string} path @param {AbortSignal} [signal] @param {boolean} [history] @returns {Promise<import("./saved-session.js").SavedNotesSession>} */
|
|
118
|
+
export async function readSavedNotesSession(path, signal, history = false) {
|
|
119
|
+
signal?.throwIfAborted();
|
|
120
|
+
const file = await open(path, "r");
|
|
121
|
+
try {
|
|
122
|
+
const before = await file.stat();
|
|
123
|
+
const hash = createHash("sha256");
|
|
124
|
+
/** @type {import("@earendil-works/pi-coding-agent").SessionEntry[]} */
|
|
125
|
+
const entries = [];
|
|
126
|
+
/** @type {import("@earendil-works/pi-coding-agent").SessionEntry[]} */
|
|
127
|
+
const branch = [];
|
|
128
|
+
/** @type {Map<string, {base:string|null,depth:number}[]>} */
|
|
129
|
+
const archived = new Map();
|
|
130
|
+
/** @type {string | undefined} */
|
|
131
|
+
let threadId;
|
|
132
|
+
/** @type {string | null | undefined} */
|
|
133
|
+
let target;
|
|
134
|
+
for await (const line of reverseLines(file, before.size, hash, signal)) {
|
|
135
|
+
signal?.throwIfAborted();
|
|
136
|
+
if (!line.length) continue;
|
|
137
|
+
/** @type {unknown} */
|
|
138
|
+
let entry;
|
|
139
|
+
try {
|
|
140
|
+
entry = JSON.parse(line.toString("utf8"));
|
|
141
|
+
} catch {
|
|
142
|
+
throw new Error(
|
|
143
|
+
"Saved notes are being updated. Retry after the session settles.",
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if (isSessionHeader(entry)) {
|
|
147
|
+
threadId = entry.id;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (!isRecord(entry)) throw new Error("Invalid saved session record");
|
|
151
|
+
if (target === undefined) target = entry.id;
|
|
152
|
+
const active = entry.id === target;
|
|
153
|
+
if (active) target = entry.parentId;
|
|
154
|
+
if (history) {
|
|
155
|
+
// Pi appends descendants after their parents, including Tree manifests
|
|
156
|
+
// after their archived leaves. Follow only active manifests' ancestry.
|
|
157
|
+
const paths = archived.get(entry.id) ?? [];
|
|
158
|
+
archived.delete(entry.id);
|
|
159
|
+
for (const path of paths) {
|
|
160
|
+
if (++path.depth >= MAX_ARCHIVE_DEPTH || entry.parentId === path.base)
|
|
161
|
+
continue;
|
|
162
|
+
if (entry.parentId !== null) {
|
|
163
|
+
const parents = archived.get(entry.parentId) ?? [];
|
|
164
|
+
parents.push(path);
|
|
165
|
+
archived.set(entry.parentId, parents);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (
|
|
169
|
+
active &&
|
|
170
|
+
isCustom(entry) &&
|
|
171
|
+
entry.customType === TREE_ARCHIVE_ENTRY_TYPE &&
|
|
172
|
+
isTreeArchiveManifestData(entry.data) &&
|
|
173
|
+
entry.data.archivedLeafId !== entry.data.branchBaseId
|
|
174
|
+
) {
|
|
175
|
+
const paths = archived.get(entry.data.archivedLeafId) ?? [];
|
|
176
|
+
paths.push({ base: entry.data.branchBaseId, depth: 0 });
|
|
177
|
+
archived.set(entry.data.archivedLeafId, paths);
|
|
178
|
+
}
|
|
179
|
+
if (
|
|
180
|
+
active ||
|
|
181
|
+
paths.length ||
|
|
182
|
+
(isCustom(entry) && SHARING_ENTRIES.has(entry.customType))
|
|
183
|
+
)
|
|
184
|
+
entries.push(entry);
|
|
185
|
+
if (active) branch.push(entry);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (isCustom(entry)) {
|
|
189
|
+
if (SHARING_ENTRIES.has(entry.customType)) entries.push(entry);
|
|
190
|
+
else if (active && NOTE_ENTRIES.has(entry.customType))
|
|
191
|
+
branch.push(entry);
|
|
192
|
+
else continue;
|
|
193
|
+
} else if (active && isHandoff(entry)) branch.push(entry);
|
|
194
|
+
else continue;
|
|
195
|
+
}
|
|
196
|
+
// Pruning replaces the path. An open descriptor alone still sees the old inode.
|
|
197
|
+
const after = await stat(path);
|
|
198
|
+
if (
|
|
199
|
+
after.dev !== before.dev ||
|
|
200
|
+
after.ino !== before.ino ||
|
|
201
|
+
after.size < before.size ||
|
|
202
|
+
(after.size === before.size &&
|
|
203
|
+
(after.mtimeMs !== before.mtimeMs || after.ctimeMs !== before.ctimeMs))
|
|
204
|
+
)
|
|
205
|
+
throw new Error("Saved notes changed during the read. Retry the lookup.");
|
|
206
|
+
if (after.size > before.size) {
|
|
207
|
+
// Flushed Pi sessions append. Keep the original leaf/byte boundary,
|
|
208
|
+
// but prove its prefix did not change, including abandoned identities.
|
|
209
|
+
// Hash the same reverse chunks, without retaining the whole JSONL.
|
|
210
|
+
const verified = createHash("sha256");
|
|
211
|
+
for await (const chunk of reverseChunks(file, before.size, signal))
|
|
212
|
+
verified.update(chunk);
|
|
213
|
+
if (hash.digest("hex") !== verified.digest("hex"))
|
|
214
|
+
throw new Error(
|
|
215
|
+
"Saved notes changed during the read. Retry the lookup.",
|
|
216
|
+
);
|
|
217
|
+
const current = await stat(path);
|
|
218
|
+
if (
|
|
219
|
+
current.dev !== before.dev ||
|
|
220
|
+
current.ino !== before.ino ||
|
|
221
|
+
current.size < after.size ||
|
|
222
|
+
(current.size === after.size &&
|
|
223
|
+
(current.mtimeMs !== after.mtimeMs ||
|
|
224
|
+
current.ctimeMs !== after.ctimeMs))
|
|
225
|
+
)
|
|
226
|
+
throw new Error(
|
|
227
|
+
"Saved notes changed during the read. Retry the lookup.",
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
if (!threadId || (target !== undefined && target !== null))
|
|
231
|
+
throw new Error("Saved notes have an incomplete session branch");
|
|
232
|
+
return {
|
|
233
|
+
threadId,
|
|
234
|
+
entries: entries.reverse(),
|
|
235
|
+
branch: branch.reverse(),
|
|
236
|
+
savedAt: before.mtimeMs,
|
|
237
|
+
};
|
|
238
|
+
} finally {
|
|
239
|
+
await file.close();
|
|
240
|
+
}
|
|
241
|
+
}
|
package/src/saved-session.ts
CHANGED
|
@@ -1,49 +1,8 @@
|
|
|
1
|
-
import { type FileHandle, open } from "node:fs/promises";
|
|
2
1
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
2
|
import { Type } from "typebox";
|
|
4
3
|
import { Check } from "typebox/value";
|
|
5
|
-
import {
|
|
6
|
-
isTreeArchiveManifestData,
|
|
7
|
-
MAX_ARCHIVE_DEPTH,
|
|
8
|
-
TREE_ARCHIVE_ENTRY_TYPE,
|
|
9
|
-
} from "./tree-archive.js";
|
|
10
|
-
|
|
11
|
-
const SessionHeader = Type.Object({
|
|
12
|
-
type: Type.Literal("session"),
|
|
13
|
-
id: Type.String(),
|
|
14
|
-
});
|
|
15
|
-
const RecordHeader = Type.Object({
|
|
16
|
-
type: Type.String(),
|
|
17
|
-
id: Type.String(),
|
|
18
|
-
parentId: Type.Union([Type.String(), Type.Null()]),
|
|
19
|
-
timestamp: Type.String(),
|
|
20
|
-
});
|
|
21
|
-
const Custom = Type.Object({
|
|
22
|
-
...RecordHeader.properties,
|
|
23
|
-
type: Type.Literal("custom"),
|
|
24
|
-
customType: Type.String(),
|
|
25
|
-
data: Type.Unknown(),
|
|
26
|
-
});
|
|
27
|
-
const Handoff = Type.Object({
|
|
28
|
-
...RecordHeader.properties,
|
|
29
|
-
type: Type.Literal("branch_summary"),
|
|
30
|
-
fromId: Type.String(),
|
|
31
|
-
summary: Type.String(),
|
|
32
|
-
details: Type.Object({ codexContextNoteHandoff: Type.Unknown() }),
|
|
33
|
-
});
|
|
34
|
-
const NOTE_ENTRIES = new Set([
|
|
35
|
-
"notes-compaction:note:v1",
|
|
36
|
-
"codex-context-note",
|
|
37
|
-
"codex-context-note-snapshot",
|
|
38
|
-
]);
|
|
39
|
-
const SHARING_ENTRIES = new Set([
|
|
40
|
-
"codex-context-agent",
|
|
41
|
-
"herdr-context-child",
|
|
42
|
-
"shepherdr-attachment-route",
|
|
43
|
-
"shepherdr-attachment-owner",
|
|
44
|
-
"shepherdr-attachment-detached",
|
|
45
|
-
]);
|
|
46
4
|
|
|
5
|
+
export { readSavedNotesSession } from "./saved-session.mjs";
|
|
47
6
|
export interface SavedNotesSession {
|
|
48
7
|
threadId: string;
|
|
49
8
|
entries: readonly SessionEntry[];
|
|
@@ -51,118 +10,42 @@ export interface SavedNotesSession {
|
|
|
51
10
|
savedAt?: number;
|
|
52
11
|
}
|
|
53
12
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
yield data.subarray(index + 1, end);
|
|
67
|
-
end = index;
|
|
68
|
-
}
|
|
69
|
-
partial = data.subarray(0, end);
|
|
70
|
-
}
|
|
71
|
-
yield partial;
|
|
72
|
-
}
|
|
13
|
+
const SavedEntry = Type.Object({
|
|
14
|
+
type: Type.String(),
|
|
15
|
+
id: Type.String(),
|
|
16
|
+
parentId: Type.Union([Type.String(), Type.Null()]),
|
|
17
|
+
timestamp: Type.String(),
|
|
18
|
+
});
|
|
19
|
+
const SavedSession = Type.Object({
|
|
20
|
+
threadId: Type.String({ minLength: 1 }),
|
|
21
|
+
entries: Type.Array(SavedEntry),
|
|
22
|
+
branch: Type.Array(SavedEntry),
|
|
23
|
+
savedAt: Type.Optional(Type.Number()),
|
|
24
|
+
});
|
|
73
25
|
|
|
74
|
-
/**
|
|
75
|
-
export
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
let entry: unknown;
|
|
96
|
-
try {
|
|
97
|
-
entry = JSON.parse(line.toString("utf8"));
|
|
98
|
-
} catch {
|
|
99
|
-
throw new Error(
|
|
100
|
-
"Saved notes are being updated. Retry after the session settles.",
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
if (Check(SessionHeader, entry)) {
|
|
104
|
-
threadId = entry.id;
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
if (!Check(RecordHeader, entry))
|
|
108
|
-
throw new Error("Invalid saved session record");
|
|
109
|
-
if (target === undefined) target = entry.id;
|
|
110
|
-
const active = entry.id === target;
|
|
111
|
-
if (active) target = entry.parentId;
|
|
112
|
-
if (history) {
|
|
113
|
-
// Pi appends descendants after their parents, including Tree manifests
|
|
114
|
-
// after their archived leaves. Follow only active manifests' ancestry.
|
|
115
|
-
const paths = archived.get(entry.id) ?? [];
|
|
116
|
-
archived.delete(entry.id);
|
|
117
|
-
for (const path of paths) {
|
|
118
|
-
if (++path.depth >= MAX_ARCHIVE_DEPTH || entry.parentId === path.base)
|
|
119
|
-
continue;
|
|
120
|
-
if (entry.parentId !== null) {
|
|
121
|
-
const parents = archived.get(entry.parentId) ?? [];
|
|
122
|
-
parents.push(path);
|
|
123
|
-
archived.set(entry.parentId, parents);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (
|
|
127
|
-
active &&
|
|
128
|
-
Check(Custom, entry) &&
|
|
129
|
-
entry.customType === TREE_ARCHIVE_ENTRY_TYPE &&
|
|
130
|
-
isTreeArchiveManifestData(entry.data) &&
|
|
131
|
-
entry.data.archivedLeafId !== entry.data.branchBaseId
|
|
132
|
-
) {
|
|
133
|
-
const paths = archived.get(entry.data.archivedLeafId) ?? [];
|
|
134
|
-
paths.push({ base: entry.data.branchBaseId, depth: 0 });
|
|
135
|
-
archived.set(entry.data.archivedLeafId, paths);
|
|
136
|
-
}
|
|
137
|
-
if (
|
|
138
|
-
active ||
|
|
139
|
-
paths.length ||
|
|
140
|
-
(Check(Custom, entry) && SHARING_ENTRIES.has(entry.customType))
|
|
141
|
-
)
|
|
142
|
-
entries.push(entry as SessionEntry);
|
|
143
|
-
if (active) branch.push(entry as SessionEntry);
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
if (Check(Custom, entry)) {
|
|
147
|
-
if (SHARING_ENTRIES.has(entry.customType)) entries.push(entry);
|
|
148
|
-
else if (active && NOTE_ENTRIES.has(entry.customType))
|
|
149
|
-
branch.push(entry);
|
|
150
|
-
else continue;
|
|
151
|
-
} else if (active && Check(Handoff, entry)) branch.push(entry);
|
|
152
|
-
else continue;
|
|
153
|
-
}
|
|
154
|
-
const after = await file.stat();
|
|
155
|
-
if (before.size !== after.size || before.mtimeMs !== after.mtimeMs)
|
|
156
|
-
throw new Error("Saved notes changed during the read. Retry the lookup.");
|
|
157
|
-
if (!threadId || (target !== undefined && target !== null))
|
|
158
|
-
throw new Error("Saved notes have an incomplete session branch");
|
|
159
|
-
return {
|
|
160
|
-
threadId,
|
|
161
|
-
entries: entries.reverse(),
|
|
162
|
-
branch: branch.reverse(),
|
|
163
|
-
savedAt: before.mtimeMs,
|
|
164
|
-
};
|
|
165
|
-
} finally {
|
|
166
|
-
await file.close();
|
|
26
|
+
/** Remote loaders carry data, not authority. Native identity is checked by the graph reader. */
|
|
27
|
+
export function validateSavedNotesSession(value: unknown): SavedNotesSession {
|
|
28
|
+
if (!Check(SavedSession, value))
|
|
29
|
+
throw new Error(
|
|
30
|
+
"Saved session response is invalid. Update Notes on the other machine and retry.",
|
|
31
|
+
);
|
|
32
|
+
for (const entry of [...value.entries, ...value.branch]) {
|
|
33
|
+
if (
|
|
34
|
+
entry.type === "custom" &&
|
|
35
|
+
(!("customType" in entry) ||
|
|
36
|
+
typeof entry.customType !== "string" ||
|
|
37
|
+
!("data" in entry))
|
|
38
|
+
)
|
|
39
|
+
throw new Error("Invalid saved session custom record");
|
|
40
|
+
if (
|
|
41
|
+
entry.type === "message" &&
|
|
42
|
+
(!("message" in entry) ||
|
|
43
|
+
!entry.message ||
|
|
44
|
+
typeof entry.message !== "object")
|
|
45
|
+
)
|
|
46
|
+
throw new Error("Invalid saved session message record");
|
|
167
47
|
}
|
|
48
|
+
// The same persisted entry envelope is accepted by the Node reader. Feature readers
|
|
49
|
+
// validate their own custom data, including ownership and note receipts.
|
|
50
|
+
return value as SavedNotesSession;
|
|
168
51
|
}
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { type Static, Type } from "typebox";
|
|
2
2
|
import { Check } from "typebox/value";
|
|
3
|
+
import {
|
|
4
|
+
CONTEXT_AGENT_PATTERN,
|
|
5
|
+
canonicalAgentName,
|
|
6
|
+
canonicalContextIdentity,
|
|
7
|
+
contextStorageAgentName,
|
|
8
|
+
pinnedContextIdentity,
|
|
9
|
+
} from "./agent-identity.js";
|
|
3
10
|
import type { SavedNotesSession } from "./saved-session.js";
|
|
4
11
|
|
|
5
|
-
const Agent = Type.String({ pattern:
|
|
12
|
+
const Agent = Type.String({ pattern: CONTEXT_AGENT_PATTERN });
|
|
6
13
|
export const Identity = Type.Object({
|
|
7
14
|
protocol: Type.Literal(1),
|
|
8
15
|
sessionId: Type.String(),
|
|
9
16
|
threadId: Type.String(),
|
|
10
17
|
agentName: Agent,
|
|
18
|
+
storageAgentName: Type.Optional(Agent),
|
|
11
19
|
storage: Type.Optional(
|
|
12
20
|
Type.Union([Type.Literal("session"), Type.Literal("remote")]),
|
|
13
21
|
),
|
|
@@ -30,6 +38,7 @@ export const Plan = Type.Object({
|
|
|
30
38
|
alias: Agent,
|
|
31
39
|
controllerAlias: Agent,
|
|
32
40
|
controllerFile: Type.Optional(Type.String()),
|
|
41
|
+
controllerMachine: Type.Optional(Type.String()),
|
|
33
42
|
controllerNotes: Type.Unknown(),
|
|
34
43
|
targetNotes: Type.Unknown(),
|
|
35
44
|
}),
|
|
@@ -67,9 +76,31 @@ export const Snapshot = Type.Object({
|
|
|
67
76
|
export const Parent = Type.Object({
|
|
68
77
|
transport: Type.Literal("shepherdr"),
|
|
69
78
|
parent: Type.String(),
|
|
79
|
+
savedParent: Type.Optional(
|
|
80
|
+
Type.Object({ machine: Type.String(), sessionFile: Type.String() }),
|
|
81
|
+
),
|
|
70
82
|
});
|
|
71
83
|
export type Identity = Static<typeof Identity>;
|
|
72
84
|
|
|
85
|
+
/** Ordinary family membership is immutable, explicit and native-session owned. */
|
|
86
|
+
export function savedFamilyIdentity(
|
|
87
|
+
session: Pick<SavedNotesSession, "threadId" | "entries">,
|
|
88
|
+
): Identity | undefined {
|
|
89
|
+
const identity = savedNotesIdentity(session);
|
|
90
|
+
if (
|
|
91
|
+
identity.storage === undefined ||
|
|
92
|
+
!session.entries.some(
|
|
93
|
+
(entry) =>
|
|
94
|
+
entry.type === "custom" &&
|
|
95
|
+
entry.customType === "codex-context-agent" &&
|
|
96
|
+
Check(Identity, entry.data) &&
|
|
97
|
+
entry.data.threadId === session.threadId,
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
return;
|
|
101
|
+
return identity;
|
|
102
|
+
}
|
|
103
|
+
|
|
73
104
|
/** Persisted sharing is session-scoped. Forks never inherit the source agent's address. */
|
|
74
105
|
export function savedNotesIdentity(
|
|
75
106
|
session: Pick<SavedNotesSession, "threadId" | "entries">,
|
|
@@ -84,13 +115,14 @@ export function savedNotesIdentity(
|
|
|
84
115
|
if (found) throw new Error("Conflicting saved notes identities");
|
|
85
116
|
found = entry.data;
|
|
86
117
|
}
|
|
87
|
-
return (
|
|
88
|
-
found ??
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
118
|
+
return canonicalContextIdentity(
|
|
119
|
+
found ??
|
|
120
|
+
pinnedContextIdentity(session.threadId, session.entries) ?? {
|
|
121
|
+
protocol: 1,
|
|
122
|
+
threadId: session.threadId,
|
|
123
|
+
sessionId: session.threadId,
|
|
124
|
+
agentName: canonicalAgentName(session.threadId),
|
|
125
|
+
},
|
|
94
126
|
);
|
|
95
127
|
}
|
|
96
128
|
|
|
@@ -98,6 +130,6 @@ export function sameOwner(a: Identity, b: Identity): boolean {
|
|
|
98
130
|
return (
|
|
99
131
|
a.threadId === b.threadId &&
|
|
100
132
|
a.sessionId === b.sessionId &&
|
|
101
|
-
a
|
|
133
|
+
contextStorageAgentName(a) === contextStorageAgentName(b)
|
|
102
134
|
);
|
|
103
135
|
}
|