@howaboua/pi-agent-comms 0.0.0-stage → 0.0.1-dev.13.1.gef1ce49691d6
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/INTEGRATION.md +31 -0
- package/LICENSE +21 -0
- package/PCC-INTEGRATION.md +17 -0
- package/README.md +53 -2
- package/dist/viewer/archive.js +249 -0
- package/dist/viewer/config.js +85 -0
- package/dist/viewer/contracts.js +1 -0
- package/dist/viewer/server.js +177 -0
- package/dist/viewer/source.js +29 -0
- package/dist/viewer/web/app.js +88 -0
- package/dist/viewer/web/index.html +41 -0
- package/dist/viewer/web/navigation.js +72 -0
- package/dist/viewer/web/posts.js +105 -0
- package/dist/viewer/web/render.js +147 -0
- package/dist/viewer/web/session.js +86 -0
- package/dist/viewer/web/style.css +517 -0
- package/dist/viewer/web/updates.js +106 -0
- package/docs/BOARD-CHANGELOG.md +49 -0
- package/docs/BOARD.md +124 -0
- package/docs/NOTES-CHANGELOG.md +127 -0
- package/docs/NOTES.md +122 -0
- package/index.ts +21 -0
- package/package.json +75 -5
- package/src/board/AGENTS.md +12 -0
- package/src/board/activity.ts +274 -0
- package/src/board/archive.ts +118 -0
- package/src/board/awareness.ts +179 -0
- package/src/board/briefing.ts +25 -0
- package/src/board/config.ts +192 -0
- package/src/board/consolidation.ts +32 -0
- package/src/board/context-briefing.ts +245 -0
- package/src/board/contract.ts +254 -0
- package/src/board/delivery.ts +85 -0
- package/src/board/feature.ts +91 -0
- package/src/board/identity.ts +238 -0
- package/src/board/integration.ts +55 -0
- package/src/board/paging.ts +54 -0
- package/src/board/protocol.ts +78 -0
- package/src/board/queries.ts +168 -0
- package/src/board/response.ts +82 -0
- package/src/board/runtime/activation.ts +50 -0
- package/src/board/runtime/catchup.ts +96 -0
- package/src/board/runtime/coordinator.ts +311 -0
- package/src/board/runtime/engine.ts +40 -0
- package/src/board/runtime/index.ts +63 -0
- package/src/board/runtime/owner.ts +168 -0
- package/src/board/runtime/routing.ts +152 -0
- package/src/board/runtime/turns.ts +109 -0
- package/src/board/search.ts +95 -0
- package/src/board/settings/panel.ts +193 -0
- package/src/board/store.ts +427 -0
- package/src/board/summaries.ts +124 -0
- package/src/board/summarizer.ts +201 -0
- package/src/board/tool.ts +27 -0
- package/src/board/viewer/AGENTS.md +1 -0
- package/src/board/viewer/archive.ts +308 -0
- package/src/board/viewer/config.ts +111 -0
- package/src/board/viewer/contracts.ts +98 -0
- package/src/board/viewer/index.ts +162 -0
- package/src/board/viewer/server.ts +204 -0
- package/src/board/viewer/source.ts +33 -0
- package/src/board/viewer/web/app.js +88 -0
- package/src/board/viewer/web/index.html +41 -0
- package/src/board/viewer/web/navigation.js +72 -0
- package/src/board/viewer/web/posts.js +105 -0
- package/src/board/viewer/web/render.js +147 -0
- package/src/board/viewer/web/session.js +86 -0
- package/src/board/viewer/web/style.css +517 -0
- package/src/board/viewer/web/updates.js +106 -0
- package/src/guidance.ts +121 -0
- package/src/notes/AGENTS.md +27 -0
- package/src/notes/activation.ts +150 -0
- package/src/notes/agent-identity.ts +221 -0
- package/src/notes/attachment-sharing.ts +165 -0
- package/src/notes/attachment-snapshot.ts +113 -0
- package/src/notes/binding-startup.ts +30 -0
- package/src/notes/bridge.ts +130 -0
- package/src/notes/compaction-projection.ts +88 -0
- package/src/notes/context-owner.ts +99 -0
- package/src/notes/context-sharing.ts +178 -0
- package/src/notes/extension-hooks.ts +200 -0
- package/src/notes/extension-sharing.ts +133 -0
- package/src/notes/feature.ts +168 -0
- package/src/notes/fresh-checkpoint.ts +143 -0
- package/src/notes/fresh-notes.ts +323 -0
- package/src/notes/history-search-protocol.ts +39 -0
- package/src/notes/history-search-pruning.mjs +120 -0
- package/src/notes/history-search-worker.mjs +258 -0
- package/src/notes/history-search.ts +269 -0
- package/src/notes/history-windows.ts +221 -0
- package/src/notes/history.ts +158 -0
- package/src/notes/idle-admission.ts +201 -0
- package/src/notes/index.ts +125 -0
- package/src/notes/integrations.ts +174 -0
- package/src/notes/lifecycle.ts +308 -0
- package/src/notes/model-role-config.ts +79 -0
- package/src/notes/model-role-screen.ts +74 -0
- package/src/notes/model-roles.ts +225 -0
- package/src/notes/note-query.ts +148 -0
- package/src/notes/note-result.ts +88 -0
- package/src/notes/note-save-marker.ts +128 -0
- package/src/notes/notes-lookup.ts +115 -0
- package/src/notes/notes.ts +302 -0
- package/src/notes/query-executor.ts +242 -0
- package/src/notes/query-input.ts +126 -0
- package/src/notes/remote.ts +376 -0
- package/src/notes/rollover.ts +224 -0
- package/src/notes/saved-session-access.ts +117 -0
- package/src/notes/saved-session-index.mjs +83 -0
- package/src/notes/saved-session.mjs +241 -0
- package/src/notes/saved-session.ts +51 -0
- package/src/notes/saved-sharing-records.ts +135 -0
- package/src/notes/session-pruning-content.ts +237 -0
- package/src/notes/session-pruning-plan.ts +243 -0
- package/src/notes/session-pruning-policy.ts +20 -0
- package/src/notes/session-pruning-screen.ts +204 -0
- package/src/notes/session-pruning-source.ts +44 -0
- package/src/notes/session-pruning-transaction.ts +141 -0
- package/src/notes/session-pruning.ts +155 -0
- package/src/notes/settings-command.ts +95 -0
- package/src/notes/settings-controls.ts +199 -0
- package/src/notes/settings-screen.ts +113 -0
- package/src/notes/settings.ts +287 -0
- package/src/notes/shared-notes.ts +411 -0
- package/src/notes/shared-session-reader.ts +143 -0
- package/src/notes/sharing-service.ts +282 -0
- package/src/notes/sqlite.mjs +33 -0
- package/src/notes/store.ts +283 -0
- package/src/notes/task-boundary.ts +37 -0
- package/src/notes/tool-contracts.ts +312 -0
- package/src/notes/tools.ts +148 -0
- package/src/notes/tree-archive-manifest.mjs +27 -0
- package/src/notes/tree-archive.ts +129 -0
- package/src/notes/tree-checkpoint.ts +170 -0
- package/src/notes/tree-handoff-read.ts +192 -0
- package/src/notes/window-checkpoint.ts +105 -0
- package/src/notes/window-message.ts +185 -0
- package/src/notes/windows.ts +272 -0
- package/src/settings-navigation.ts +11 -0
- package/src/timestamps.ts +3 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { Static } from "typebox";
|
|
4
|
+
import { Check } from "typebox/value";
|
|
5
|
+
import {
|
|
6
|
+
canonicalAgentName,
|
|
7
|
+
contextAgentNames,
|
|
8
|
+
contextStorageAgentName,
|
|
9
|
+
contextTargetAgent,
|
|
10
|
+
isContextAgentName,
|
|
11
|
+
} from "./agent-identity.js";
|
|
12
|
+
import type { SavedSessionLoader } from "./context-sharing.js";
|
|
13
|
+
import type { HistorySearch } from "./history-search.js";
|
|
14
|
+
import type { SavedNotesSession } from "./saved-session.js";
|
|
15
|
+
import { createSavedSessionAccess } from "./saved-session-access.js";
|
|
16
|
+
import {
|
|
17
|
+
Detachment,
|
|
18
|
+
Member,
|
|
19
|
+
Parent,
|
|
20
|
+
Plan,
|
|
21
|
+
Route,
|
|
22
|
+
sameOwner,
|
|
23
|
+
savedFamilyIdentity,
|
|
24
|
+
savedNotesIdentity,
|
|
25
|
+
} from "./saved-sharing-records.js";
|
|
26
|
+
import { createSharedSessionReader } from "./shared-session-reader.js";
|
|
27
|
+
import type { NotesStore } from "./store.js";
|
|
28
|
+
|
|
29
|
+
export { savedNotesIdentity } from "./saved-sharing-records.js";
|
|
30
|
+
|
|
31
|
+
function records(session: SavedNotesSession, kind: string): unknown[] {
|
|
32
|
+
return session.entries.flatMap((entry) =>
|
|
33
|
+
entry.type === "custom" && entry.customType === kind ? [entry.data] : [],
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function inside(path: string, root: string): boolean {
|
|
38
|
+
return path === root || path.startsWith(`${root}/`);
|
|
39
|
+
}
|
|
40
|
+
function remap(
|
|
41
|
+
params: Record<string, unknown>,
|
|
42
|
+
from: string,
|
|
43
|
+
to: string,
|
|
44
|
+
): Record<string, unknown> {
|
|
45
|
+
return Object.fromEntries(
|
|
46
|
+
Object.entries(params).map(([key, value]) => [
|
|
47
|
+
key,
|
|
48
|
+
["path", "prefix", "path_prefix"].includes(key) &&
|
|
49
|
+
typeof value === "string" &&
|
|
50
|
+
inside(value, from)
|
|
51
|
+
? `${to}${value.slice(from.length)}`
|
|
52
|
+
: value,
|
|
53
|
+
]),
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function sharedNotesUnavailable(error: unknown): boolean {
|
|
58
|
+
return (
|
|
59
|
+
error instanceof Error &&
|
|
60
|
+
(("code" in error &&
|
|
61
|
+
[
|
|
62
|
+
"ENOENT",
|
|
63
|
+
"EACCES",
|
|
64
|
+
"EPERM",
|
|
65
|
+
"SAVED_SESSION_UNAVAILABLE",
|
|
66
|
+
"ECONNREFUSED",
|
|
67
|
+
"ECONNRESET",
|
|
68
|
+
"EPIPE",
|
|
69
|
+
"ETIMEDOUT",
|
|
70
|
+
].includes(String(error.code))) ||
|
|
71
|
+
/^(No shared context agent\b|Cross-agent (?:context requires an available context router|notes (?:are unavailable here|owner is unavailable))|Shared context (?:owner|controller) is unavailable|Shared context response was lost|Herdr machine .* is (?:unavailable|disconnected)|remote Shepherdr bridge (?:is unavailable|closed|exited\b)|remote Shepherdr context timed out|Unknown Core machine |Core machine .* is (?:unavailable|connecting)\b|Core is unavailable\b|Remote connection (?:closed|is not ready)\b|Remote request timed out\b)/.test(
|
|
72
|
+
error.message,
|
|
73
|
+
))
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Saved local notes do not need PCC, a router, or a running owner. Writes still belong to the live session. */
|
|
78
|
+
export async function readSharedNotes(
|
|
79
|
+
ctx: ExtensionContext,
|
|
80
|
+
store: Pick<NotesStore, "read" | "sessionFile">,
|
|
81
|
+
params: Record<string, unknown>,
|
|
82
|
+
signal?: AbortSignal,
|
|
83
|
+
loader?: SavedSessionLoader,
|
|
84
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
85
|
+
return readSharedSession(
|
|
86
|
+
ctx,
|
|
87
|
+
store,
|
|
88
|
+
params,
|
|
89
|
+
{ namespace: "notes" },
|
|
90
|
+
signal,
|
|
91
|
+
loader,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function readSharedHistory(
|
|
96
|
+
ctx: ExtensionContext,
|
|
97
|
+
store: Pick<NotesStore, "read" | "sessionFile">,
|
|
98
|
+
search: HistorySearch,
|
|
99
|
+
params: Record<string, unknown>,
|
|
100
|
+
signal?: AbortSignal,
|
|
101
|
+
loader?: SavedSessionLoader,
|
|
102
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
103
|
+
return readSharedSession(
|
|
104
|
+
ctx,
|
|
105
|
+
store,
|
|
106
|
+
params,
|
|
107
|
+
{ namespace: "history", search },
|
|
108
|
+
signal,
|
|
109
|
+
loader,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function readSharedSession(
|
|
114
|
+
ctx: ExtensionContext,
|
|
115
|
+
store: Pick<NotesStore, "read" | "sessionFile">,
|
|
116
|
+
params: Record<string, unknown>,
|
|
117
|
+
access:
|
|
118
|
+
| { namespace: "notes" }
|
|
119
|
+
| { namespace: "history"; search: HistorySearch },
|
|
120
|
+
signal?: AbortSignal,
|
|
121
|
+
loader?: SavedSessionLoader,
|
|
122
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
123
|
+
const { namespace } = access;
|
|
124
|
+
if (
|
|
125
|
+
namespace === "notes" &&
|
|
126
|
+
!["read_file", "list_files_by_prefix", "search_contents"].includes(
|
|
127
|
+
String(params["action"]),
|
|
128
|
+
)
|
|
129
|
+
)
|
|
130
|
+
return;
|
|
131
|
+
const path =
|
|
132
|
+
namespace === "history"
|
|
133
|
+
? params["agent_name"]
|
|
134
|
+
: (params["path"] ?? params["prefix"] ?? params["path_prefix"]);
|
|
135
|
+
if (typeof path !== "string" || !path.startsWith("/")) return;
|
|
136
|
+
const target =
|
|
137
|
+
namespace === "history"
|
|
138
|
+
? isContextAgentName(path)
|
|
139
|
+
? path
|
|
140
|
+
: undefined
|
|
141
|
+
: contextTargetAgent(
|
|
142
|
+
"notes",
|
|
143
|
+
{ path },
|
|
144
|
+
canonicalAgentName(ctx.sessionManager.getSessionId()),
|
|
145
|
+
);
|
|
146
|
+
if (!target) return;
|
|
147
|
+
const outputAgent = target;
|
|
148
|
+
const current: SavedNotesSession = {
|
|
149
|
+
threadId: ctx.sessionManager.getSessionId(),
|
|
150
|
+
entries: ctx.sessionManager.getEntries(),
|
|
151
|
+
branch: ctx.sessionManager.getBranch(),
|
|
152
|
+
};
|
|
153
|
+
if (contextAgentNames(savedNotesIdentity(current)).includes(target)) return;
|
|
154
|
+
const visited = new Set<string>();
|
|
155
|
+
const { containingMachine, locator, load, indexed, find } =
|
|
156
|
+
createSavedSessionAccess(store, namespace, loader, signal);
|
|
157
|
+
let unavailable: unknown;
|
|
158
|
+
let targetReached = false;
|
|
159
|
+
async function exploratoryLoad(location: Parameters<typeof load>[0]) {
|
|
160
|
+
try {
|
|
161
|
+
return await load(location);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
signal?.throwIfAborted();
|
|
164
|
+
if (!sharedNotesUnavailable(error)) throw error;
|
|
165
|
+
unavailable ??= error;
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const { read, reference } = createSharedSessionReader(
|
|
171
|
+
store,
|
|
172
|
+
access,
|
|
173
|
+
outputAgent,
|
|
174
|
+
signal,
|
|
175
|
+
load,
|
|
176
|
+
resolve,
|
|
177
|
+
sharedNotesUnavailable,
|
|
178
|
+
);
|
|
179
|
+
async function resolve(
|
|
180
|
+
session: SavedNotesSession,
|
|
181
|
+
destination: string,
|
|
182
|
+
query: Record<string, unknown>,
|
|
183
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
184
|
+
signal?.throwIfAborted();
|
|
185
|
+
const key = `${containingMachine(session)}\0${session.threadId}`;
|
|
186
|
+
if (visited.has(key)) return;
|
|
187
|
+
visited.add(key);
|
|
188
|
+
const identity = savedNotesIdentity(session);
|
|
189
|
+
if (contextAgentNames(identity).includes(destination)) {
|
|
190
|
+
targetReached = true;
|
|
191
|
+
return read(session, query, destination);
|
|
192
|
+
}
|
|
193
|
+
const routes = new Map<string, Static<typeof Route>>();
|
|
194
|
+
for (const value of records(session, "shepherdr-attachment-route"))
|
|
195
|
+
if (
|
|
196
|
+
Check(Route, value) &&
|
|
197
|
+
value.plan.controllerSessionId === identity.threadId
|
|
198
|
+
)
|
|
199
|
+
routes.set(`${value.machine}\0${value.sessionFile}`, value);
|
|
200
|
+
const matching = [...routes.values()].filter((route) => {
|
|
201
|
+
const link = route.plan.context;
|
|
202
|
+
return (
|
|
203
|
+
route.phase === "ready" &&
|
|
204
|
+
link &&
|
|
205
|
+
(destination === canonicalAgentName(link.target.threadId) ||
|
|
206
|
+
inside(destination, link.alias))
|
|
207
|
+
);
|
|
208
|
+
});
|
|
209
|
+
if (matching.length > 1)
|
|
210
|
+
throw new Error(
|
|
211
|
+
"Saved shared address is ambiguous across machines. Use a unique attached alias, then retry.",
|
|
212
|
+
);
|
|
213
|
+
for (const route of matching) {
|
|
214
|
+
targetReached = true;
|
|
215
|
+
const link = route.plan.context;
|
|
216
|
+
if (route.phase !== "ready" || !link) continue;
|
|
217
|
+
const alias =
|
|
218
|
+
destination === canonicalAgentName(link.target.threadId)
|
|
219
|
+
? destination
|
|
220
|
+
: link.alias;
|
|
221
|
+
if (!inside(destination, alias)) continue;
|
|
222
|
+
if (!sameOwner(identity, link.controller))
|
|
223
|
+
throw new Error("Saved shared notes identity changed");
|
|
224
|
+
return reference(
|
|
225
|
+
link.target,
|
|
226
|
+
`${link.target.agentName}${destination.slice(alias.length)}`,
|
|
227
|
+
remap(query, alias, link.target.agentName),
|
|
228
|
+
locator(session, route.machine, route.sessionFile),
|
|
229
|
+
route.detached?.context ?? link.targetNotes,
|
|
230
|
+
route.detached?.context !== undefined,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
const owner = records(session, "shepherdr-attachment-owner").findLast(
|
|
234
|
+
(value) =>
|
|
235
|
+
Check(Plan, value) && value.targetSessionId === identity.threadId,
|
|
236
|
+
);
|
|
237
|
+
if (
|
|
238
|
+
Check(Plan, owner) &&
|
|
239
|
+
owner.context &&
|
|
240
|
+
(inside(destination, owner.context.controllerAlias) ||
|
|
241
|
+
destination === canonicalAgentName(owner.context.controller.threadId))
|
|
242
|
+
) {
|
|
243
|
+
targetReached = true;
|
|
244
|
+
const link = owner.context;
|
|
245
|
+
const alias =
|
|
246
|
+
destination === canonicalAgentName(link.controller.threadId)
|
|
247
|
+
? destination
|
|
248
|
+
: link.controllerAlias;
|
|
249
|
+
if (!sameOwner(identity, link.target))
|
|
250
|
+
throw new Error("Saved shared notes identity changed");
|
|
251
|
+
const detached = records(
|
|
252
|
+
session,
|
|
253
|
+
"shepherdr-attachment-detached",
|
|
254
|
+
).findLast(
|
|
255
|
+
(value) =>
|
|
256
|
+
Check(Detachment, value) && isDeepStrictEqual(value.plan, owner),
|
|
257
|
+
);
|
|
258
|
+
const snapshot = Check(Detachment, detached)
|
|
259
|
+
? detached.context
|
|
260
|
+
: undefined;
|
|
261
|
+
return reference(
|
|
262
|
+
link.controller,
|
|
263
|
+
`${link.controller.agentName}${destination.slice(alias.length)}`,
|
|
264
|
+
remap(query, alias, link.controller.agentName),
|
|
265
|
+
link.controllerFile
|
|
266
|
+
? locator(
|
|
267
|
+
session,
|
|
268
|
+
link.controllerMachine ?? "local",
|
|
269
|
+
link.controllerFile,
|
|
270
|
+
)
|
|
271
|
+
: undefined,
|
|
272
|
+
snapshot ?? link.controllerNotes,
|
|
273
|
+
snapshot !== undefined,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
// Explicit attachment routes (including detach snapshots) own their addresses.
|
|
277
|
+
if (session === current) {
|
|
278
|
+
let saved: SavedNotesSession | undefined;
|
|
279
|
+
try {
|
|
280
|
+
saved = await indexed(current, destination);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
signal?.throwIfAborted();
|
|
283
|
+
if (!sharedNotesUnavailable(error)) throw error;
|
|
284
|
+
unavailable ??= error;
|
|
285
|
+
}
|
|
286
|
+
if (saved) return resolve(saved, destination, query);
|
|
287
|
+
}
|
|
288
|
+
if (!savedFamilyIdentity(session)) return;
|
|
289
|
+
let result: Record<string, unknown> | undefined;
|
|
290
|
+
const members = records(session, "herdr-context-child").filter(
|
|
291
|
+
(value) =>
|
|
292
|
+
Check(Member, value) &&
|
|
293
|
+
value.threadId === identity.threadId &&
|
|
294
|
+
value.sessionId === identity.sessionId,
|
|
295
|
+
);
|
|
296
|
+
const direct = members.filter(
|
|
297
|
+
(member) => Check(Member, member) && member.agentName === destination,
|
|
298
|
+
);
|
|
299
|
+
for (const member of direct.length ? direct : members) {
|
|
300
|
+
if (!Check(Member, member)) continue;
|
|
301
|
+
const location = locator(session, member.machine, member.sessionFile);
|
|
302
|
+
// Only exploratory edges may be skipped. A recorded target must report its failure.
|
|
303
|
+
const saved = await (direct.length
|
|
304
|
+
? load(location)
|
|
305
|
+
: exploratoryLoad(location));
|
|
306
|
+
if (!saved) continue;
|
|
307
|
+
const child = savedFamilyIdentity(saved);
|
|
308
|
+
if (
|
|
309
|
+
!child ||
|
|
310
|
+
child.sessionId !== identity.sessionId ||
|
|
311
|
+
(child.agentName !== member.agentName &&
|
|
312
|
+
contextStorageAgentName(child) !== member.agentName)
|
|
313
|
+
)
|
|
314
|
+
throw new Error(
|
|
315
|
+
"This session cannot access the requested saved context. Request an accessible handoff or note path from its owner.",
|
|
316
|
+
);
|
|
317
|
+
const candidate = await resolve(saved, destination, query);
|
|
318
|
+
if (candidate) {
|
|
319
|
+
if (result)
|
|
320
|
+
throw new Error(
|
|
321
|
+
"Saved shared address is ambiguous across machines. Use a unique attached alias, then retry.",
|
|
322
|
+
);
|
|
323
|
+
result = candidate;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (direct.length) return result;
|
|
327
|
+
const routing = identity.routing;
|
|
328
|
+
const suffix = ".jsonl.shepherdr-context.json";
|
|
329
|
+
if (Check(Parent, routing)) {
|
|
330
|
+
const parentLocator = routing.savedParent
|
|
331
|
+
? locator(
|
|
332
|
+
session,
|
|
333
|
+
routing.savedParent.machine,
|
|
334
|
+
routing.savedParent.sessionFile,
|
|
335
|
+
)
|
|
336
|
+
: routing.parent.endsWith(suffix)
|
|
337
|
+
? locator(
|
|
338
|
+
session,
|
|
339
|
+
"local",
|
|
340
|
+
routing.parent.slice(0, -".shepherdr-context.json".length),
|
|
341
|
+
)
|
|
342
|
+
: undefined;
|
|
343
|
+
if (parentLocator) {
|
|
344
|
+
const saved = await exploratoryLoad(parentLocator);
|
|
345
|
+
if (!saved) return result;
|
|
346
|
+
const parent = savedFamilyIdentity(saved);
|
|
347
|
+
if (!parent || parent.sessionId !== identity.sessionId)
|
|
348
|
+
throw new Error("Saved shared notes parent identity changed");
|
|
349
|
+
const candidate = await resolve(saved, destination, query);
|
|
350
|
+
if (candidate && result)
|
|
351
|
+
throw new Error(
|
|
352
|
+
"Saved shared address is ambiguous across machines. Use a unique attached alias, then retry.",
|
|
353
|
+
);
|
|
354
|
+
result = candidate ?? result;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return result;
|
|
358
|
+
}
|
|
359
|
+
try {
|
|
360
|
+
let result: Record<string, unknown> | undefined;
|
|
361
|
+
try {
|
|
362
|
+
result = await resolve(current, target, params);
|
|
363
|
+
} catch (error) {
|
|
364
|
+
signal?.throwIfAborted();
|
|
365
|
+
if (!sharedNotesUnavailable(error)) throw error;
|
|
366
|
+
unavailable ??= error;
|
|
367
|
+
}
|
|
368
|
+
// Legacy remote children may record only a now-dead relay parent. Find the
|
|
369
|
+
// requested native thread once, never aliases or intermediate graph nodes.
|
|
370
|
+
const family = savedFamilyIdentity(current);
|
|
371
|
+
if (!result && !targetReached && family) {
|
|
372
|
+
const candidates: SavedNotesSession[] = [];
|
|
373
|
+
for (const location of await find(target)) {
|
|
374
|
+
const saved = await exploratoryLoad(location);
|
|
375
|
+
if (!saved) continue;
|
|
376
|
+
if (saved.threadId !== target.slice(1))
|
|
377
|
+
throw new Error(
|
|
378
|
+
"Saved session discovery returned a different thread",
|
|
379
|
+
);
|
|
380
|
+
const member = savedFamilyIdentity(saved);
|
|
381
|
+
if (
|
|
382
|
+
member?.storage === "session" &&
|
|
383
|
+
member.sessionId === family.sessionId &&
|
|
384
|
+
member.agentName === target
|
|
385
|
+
)
|
|
386
|
+
candidates.push(saved);
|
|
387
|
+
}
|
|
388
|
+
if (candidates.length > 1)
|
|
389
|
+
throw new Error(
|
|
390
|
+
"Saved shared address is ambiguous across machines. Use a unique attached alias, then retry.",
|
|
391
|
+
);
|
|
392
|
+
if (candidates[0]) result = await read(candidates[0], params, target);
|
|
393
|
+
}
|
|
394
|
+
if (!result && unavailable) throw unavailable;
|
|
395
|
+
return result;
|
|
396
|
+
} catch (error) {
|
|
397
|
+
if (
|
|
398
|
+
error instanceof Error &&
|
|
399
|
+
"code" in error &&
|
|
400
|
+
["ENOENT", "EACCES", "EPERM"].includes(String(error.code))
|
|
401
|
+
)
|
|
402
|
+
throw Object.assign(
|
|
403
|
+
new Error(
|
|
404
|
+
`Saved ${namespace} could not be read on this machine. Request a handoff from the owner, or ask the user to restore access before retrying.`,
|
|
405
|
+
{ cause: error },
|
|
406
|
+
),
|
|
407
|
+
{ code: "SAVED_SESSION_UNAVAILABLE" },
|
|
408
|
+
);
|
|
409
|
+
throw error;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Check } from "typebox/value";
|
|
3
|
+
import { minuteTimestamp } from "../timestamps.js";
|
|
4
|
+
import { contextStorageAgentName } from "./agent-identity.js";
|
|
5
|
+
import type { SavedSessionLocator } from "./context-sharing.js";
|
|
6
|
+
import { useHistory } from "./history.js";
|
|
7
|
+
import type { HistorySearch } from "./history-search.js";
|
|
8
|
+
import { normalizeNoteParams, readNotes } from "./note-query.js";
|
|
9
|
+
import { collectNotes } from "./notes.js";
|
|
10
|
+
import { type SavedNotesSession } from "./saved-session.js";
|
|
11
|
+
import {
|
|
12
|
+
type Identity,
|
|
13
|
+
Snapshot,
|
|
14
|
+
sameOwner,
|
|
15
|
+
savedNotesIdentity,
|
|
16
|
+
} from "./saved-sharing-records.js";
|
|
17
|
+
import type { NotesStore } from "./store.js";
|
|
18
|
+
|
|
19
|
+
export function createSharedSessionReader(
|
|
20
|
+
store: Pick<NotesStore, "read">,
|
|
21
|
+
access:
|
|
22
|
+
| { namespace: "notes" }
|
|
23
|
+
| { namespace: "history"; search: HistorySearch },
|
|
24
|
+
outputAgent: string,
|
|
25
|
+
signal: AbortSignal | undefined,
|
|
26
|
+
load: (locator: SavedSessionLocator) => Promise<SavedNotesSession>,
|
|
27
|
+
resolve: (
|
|
28
|
+
session: SavedNotesSession,
|
|
29
|
+
destination: string,
|
|
30
|
+
query: Record<string, unknown>,
|
|
31
|
+
) => Promise<Record<string, unknown> | undefined>,
|
|
32
|
+
unavailable: (error: unknown) => boolean,
|
|
33
|
+
) {
|
|
34
|
+
const { namespace } = access;
|
|
35
|
+
async function read(
|
|
36
|
+
session: SavedNotesSession,
|
|
37
|
+
query: Record<string, unknown>,
|
|
38
|
+
native: string,
|
|
39
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
40
|
+
if (namespace === "notes") {
|
|
41
|
+
const normalized = normalizeNoteParams(query, native);
|
|
42
|
+
const replacement =
|
|
43
|
+
normalized["action"] === "read_file" &&
|
|
44
|
+
collectNotes(store, session.branch).get(String(normalized["path"]))
|
|
45
|
+
?.mode === "replace";
|
|
46
|
+
// Never replace a merged hybrid read with a local-only saved branch.
|
|
47
|
+
if (
|
|
48
|
+
!replacement &&
|
|
49
|
+
(savedNotesIdentity(session).storage === "remote" ||
|
|
50
|
+
session.branch.some(
|
|
51
|
+
(entry) =>
|
|
52
|
+
entry.type === "custom" &&
|
|
53
|
+
entry.customType === "notes-compaction:cache:v1",
|
|
54
|
+
))
|
|
55
|
+
)
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
...(access.namespace === "history"
|
|
60
|
+
? await useHistory(
|
|
61
|
+
session.branch,
|
|
62
|
+
query,
|
|
63
|
+
session.threadId,
|
|
64
|
+
access.search,
|
|
65
|
+
session.entries,
|
|
66
|
+
signal,
|
|
67
|
+
)
|
|
68
|
+
: readNotes(
|
|
69
|
+
collectNotes(store, session.branch),
|
|
70
|
+
normalizeNoteParams(query, native),
|
|
71
|
+
outputAgent,
|
|
72
|
+
)),
|
|
73
|
+
...(session.savedAt === undefined
|
|
74
|
+
? {}
|
|
75
|
+
: { saved_at: minuteTimestamp(session.savedAt) }),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async function reference(
|
|
79
|
+
identity: Identity,
|
|
80
|
+
destination: string,
|
|
81
|
+
query: Record<string, unknown>,
|
|
82
|
+
locator: SavedSessionLocator | undefined,
|
|
83
|
+
retained?: unknown,
|
|
84
|
+
detached = false,
|
|
85
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
86
|
+
if (locator && (!detached || namespace === "history")) {
|
|
87
|
+
let saved: SavedNotesSession | undefined;
|
|
88
|
+
try {
|
|
89
|
+
saved = await load(locator);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
signal?.throwIfAborted();
|
|
92
|
+
if (
|
|
93
|
+
!(
|
|
94
|
+
unavailable(error) &&
|
|
95
|
+
retained !== undefined &&
|
|
96
|
+
namespace === "notes"
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
if (saved) {
|
|
102
|
+
if (!sameOwner(savedNotesIdentity(saved), identity))
|
|
103
|
+
throw new Error("Saved shared notes belong to a different session");
|
|
104
|
+
return resolve(saved, destination, query);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (
|
|
108
|
+
namespace === "history" ||
|
|
109
|
+
(destination !== identity.agentName &&
|
|
110
|
+
destination !== contextStorageAgentName(identity)) ||
|
|
111
|
+
!Check(Snapshot, retained)
|
|
112
|
+
)
|
|
113
|
+
return;
|
|
114
|
+
const entry: SessionEntry = {
|
|
115
|
+
type: "custom",
|
|
116
|
+
id: "retained",
|
|
117
|
+
parentId: null,
|
|
118
|
+
timestamp: new Date(retained.timestamp).toISOString(),
|
|
119
|
+
customType: "codex-context-note-snapshot",
|
|
120
|
+
data: {
|
|
121
|
+
...retained,
|
|
122
|
+
files: retained.files.filter(
|
|
123
|
+
(file) =>
|
|
124
|
+
file.path.startsWith(`${identity.agentName}/notes/`) ||
|
|
125
|
+
file.path.startsWith(`${contextStorageAgentName(identity)}/notes/`),
|
|
126
|
+
),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
...(await read(
|
|
131
|
+
{ threadId: identity.threadId, entries: [], branch: [entry] },
|
|
132
|
+
query,
|
|
133
|
+
destination,
|
|
134
|
+
)),
|
|
135
|
+
read_only: true,
|
|
136
|
+
captured_at: minuteTimestamp(retained.timestamp),
|
|
137
|
+
warning: detached
|
|
138
|
+
? "Read-only notes captured when sharing ended; later changes are not included"
|
|
139
|
+
: "The latest notes are unavailable; this read-only copy may be outdated",
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return { read, reference };
|
|
143
|
+
}
|