@howaboua/pi-notes-compaction 0.0.14 → 0.0.16
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 +21 -1
- package/LICENSE +21 -0
- package/README.md +8 -3
- package/index.ts +8 -0
- package/package.json +16 -11
- package/src/board/context-briefing.ts +245 -0
- package/src/board/delivery.ts +85 -0
- package/src/guidance.ts +121 -0
- package/src/notes/AGENTS.md +27 -0
- package/src/{activation.ts → notes/activation.ts} +8 -10
- package/src/{attachment-sharing.ts → notes/attachment-sharing.ts} +1 -1
- package/src/{attachment-snapshot.ts → notes/attachment-snapshot.ts} +33 -20
- package/src/{bridge.ts → notes/bridge.ts} +1 -0
- package/src/{context-sharing.ts → notes/context-sharing.ts} +22 -2
- package/src/{extension-hooks.ts → notes/extension-hooks.ts} +1 -12
- package/src/{extension-sharing.ts → notes/extension-sharing.ts} +2 -1
- package/src/notes/feature.ts +168 -0
- package/src/{history-windows.ts → notes/history-windows.ts} +10 -22
- package/src/{history.ts → notes/history.ts} +6 -5
- package/src/{index.ts → notes/index.ts} +20 -11
- package/src/{integrations.ts → notes/integrations.ts} +19 -3
- package/src/{lifecycle.ts → notes/lifecycle.ts} +0 -2
- package/src/{note-query.ts → notes/note-query.ts} +3 -5
- package/src/notes/note-result.ts +119 -0
- package/src/{note-save-marker.ts → notes/note-save-marker.ts} +3 -1
- package/src/{query-executor.ts → notes/query-executor.ts} +34 -9
- package/src/{query-input.ts → notes/query-input.ts} +1 -0
- package/src/{remote.ts → notes/remote.ts} +1 -1
- package/src/notes/saved-session-access.ts +121 -0
- package/src/notes/saved-session-index.mjs +83 -0
- package/src/notes/saved-session.mjs +241 -0
- package/src/notes/saved-session.ts +67 -0
- package/src/notes/saved-sharing-records.ts +170 -0
- package/src/{session-pruning.ts → notes/session-pruning.ts} +5 -5
- package/src/{settings-command.ts → notes/settings-command.ts} +29 -42
- package/src/{settings-controls.ts → notes/settings-controls.ts} +62 -72
- package/src/{settings-screen.ts → notes/settings-screen.ts} +25 -7
- package/src/{settings.ts → notes/settings.ts} +37 -6
- package/src/notes/shared-notes.ts +419 -0
- package/src/{shared-session-reader.ts → notes/shared-session-reader.ts} +35 -26
- package/src/{sharing-service.ts → notes/sharing-service.ts} +32 -1
- package/src/{store.ts → notes/store.ts} +10 -0
- package/src/{tool-contracts.ts → notes/tool-contracts.ts} +71 -44
- package/src/{tools.ts → notes/tools.ts} +2 -2
- package/src/notes/tree-archive-manifest.mjs +27 -0
- package/src/{tree-archive.ts → notes/tree-archive.ts} +6 -26
- package/src/{tree-handoff-read.ts → notes/tree-handoff-read.ts} +47 -32
- package/src/{window-message.ts → notes/window-message.ts} +15 -20
- package/src/settings-navigation.ts +11 -0
- package/src/timestamps.ts +3 -0
- package/tsconfig.json +9 -0
- package/src/note-result.ts +0 -70
- package/src/saved-session.ts +0 -169
- package/src/saved-sharing-records.ts +0 -112
- package/src/shared-notes.ts +0 -290
- /package/src/{agent-identity.ts → notes/agent-identity.ts} +0 -0
- /package/src/{binding-startup.ts → notes/binding-startup.ts} +0 -0
- /package/src/{compaction-projection.ts → notes/compaction-projection.ts} +0 -0
- /package/src/{context-owner.ts → notes/context-owner.ts} +0 -0
- /package/src/{fresh-checkpoint.ts → notes/fresh-checkpoint.ts} +0 -0
- /package/src/{fresh-notes.ts → notes/fresh-notes.ts} +0 -0
- /package/src/{history-search-protocol.ts → notes/history-search-protocol.ts} +0 -0
- /package/src/{history-search-pruning.mjs → notes/history-search-pruning.mjs} +0 -0
- /package/src/{history-search-worker.mjs → notes/history-search-worker.mjs} +0 -0
- /package/src/{history-search.ts → notes/history-search.ts} +0 -0
- /package/src/{idle-admission.ts → notes/idle-admission.ts} +0 -0
- /package/src/{model-role-config.ts → notes/model-role-config.ts} +0 -0
- /package/src/{model-role-screen.ts → notes/model-role-screen.ts} +0 -0
- /package/src/{model-roles.ts → notes/model-roles.ts} +0 -0
- /package/src/{notes-lookup.ts → notes/notes-lookup.ts} +0 -0
- /package/src/{notes.ts → notes/notes.ts} +0 -0
- /package/src/{rollover.ts → notes/rollover.ts} +0 -0
- /package/src/{session-pruning-content.ts → notes/session-pruning-content.ts} +0 -0
- /package/src/{session-pruning-plan.ts → notes/session-pruning-plan.ts} +0 -0
- /package/src/{session-pruning-policy.ts → notes/session-pruning-policy.ts} +0 -0
- /package/src/{session-pruning-screen.ts → notes/session-pruning-screen.ts} +0 -0
- /package/src/{session-pruning-source.ts → notes/session-pruning-source.ts} +0 -0
- /package/src/{session-pruning-transaction.ts → notes/session-pruning-transaction.ts} +0 -0
- /package/src/{sqlite.mjs → notes/sqlite.mjs} +0 -0
- /package/src/{task-boundary.ts → notes/task-boundary.ts} +0 -0
- /package/src/{tree-checkpoint.ts → notes/tree-checkpoint.ts} +0 -0
- /package/src/{window-checkpoint.ts → notes/window-checkpoint.ts} +0 -0
- /package/src/{windows.ts → notes/windows.ts} +0 -0
package/src/saved-session.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { type FileHandle, open } from "node:fs/promises";
|
|
2
|
-
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { Type } from "typebox";
|
|
4
|
-
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
|
-
"codex-context-backend",
|
|
42
|
-
"herdr-context-child",
|
|
43
|
-
"shepherdr-attachment-route",
|
|
44
|
-
"shepherdr-attachment-owner",
|
|
45
|
-
"shepherdr-attachment-detached",
|
|
46
|
-
]);
|
|
47
|
-
|
|
48
|
-
export interface SavedNotesSession {
|
|
49
|
-
threadId: string;
|
|
50
|
-
entries: readonly SessionEntry[];
|
|
51
|
-
branch: readonly SessionEntry[];
|
|
52
|
-
savedAt?: number;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async function* reverseLines(file: FileHandle, size: number) {
|
|
56
|
-
let position = size;
|
|
57
|
-
let partial = Buffer.alloc(0);
|
|
58
|
-
while (position > 0) {
|
|
59
|
-
const length = Math.min(64 * 1024, position);
|
|
60
|
-
position -= length;
|
|
61
|
-
const chunk = Buffer.allocUnsafe(length);
|
|
62
|
-
const { bytesRead } = await file.read(chunk, 0, length, position);
|
|
63
|
-
const data = Buffer.concat([chunk.subarray(0, bytesRead), partial]);
|
|
64
|
-
let end = data.length;
|
|
65
|
-
for (let index = data.length - 1; index >= 0; index--) {
|
|
66
|
-
if (data[index] !== 10) continue;
|
|
67
|
-
yield data.subarray(index + 1, end);
|
|
68
|
-
end = index;
|
|
69
|
-
}
|
|
70
|
-
partial = data.subarray(0, end);
|
|
71
|
-
}
|
|
72
|
-
yield partial;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Read only recorded session paths. Identity is session-wide; notes follow the active parent chain. */
|
|
76
|
-
export async function readSavedNotesSession(
|
|
77
|
-
path: string,
|
|
78
|
-
signal?: AbortSignal,
|
|
79
|
-
history = false,
|
|
80
|
-
): Promise<SavedNotesSession> {
|
|
81
|
-
signal?.throwIfAborted();
|
|
82
|
-
const file = await open(path, "r");
|
|
83
|
-
try {
|
|
84
|
-
const before = await file.stat();
|
|
85
|
-
const entries: SessionEntry[] = [];
|
|
86
|
-
const branch: SessionEntry[] = [];
|
|
87
|
-
const archived = new Map<
|
|
88
|
-
string,
|
|
89
|
-
{ base: string | null; depth: number }[]
|
|
90
|
-
>();
|
|
91
|
-
let threadId: string | undefined;
|
|
92
|
-
let target: string | null | undefined;
|
|
93
|
-
for await (const line of reverseLines(file, before.size)) {
|
|
94
|
-
signal?.throwIfAborted();
|
|
95
|
-
if (!line.length) continue;
|
|
96
|
-
let entry: unknown;
|
|
97
|
-
try {
|
|
98
|
-
entry = JSON.parse(line.toString("utf8"));
|
|
99
|
-
} catch {
|
|
100
|
-
throw new Error(
|
|
101
|
-
"Saved notes are being updated. Retry after the session settles.",
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
if (Check(SessionHeader, entry)) {
|
|
105
|
-
threadId = entry.id;
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
if (!Check(RecordHeader, entry))
|
|
109
|
-
throw new Error("Invalid saved session record");
|
|
110
|
-
if (target === undefined) target = entry.id;
|
|
111
|
-
const active = entry.id === target;
|
|
112
|
-
if (active) target = entry.parentId;
|
|
113
|
-
if (history) {
|
|
114
|
-
// Pi appends descendants after their parents, including Tree manifests
|
|
115
|
-
// after their archived leaves. Follow only active manifests' ancestry.
|
|
116
|
-
const paths = archived.get(entry.id) ?? [];
|
|
117
|
-
archived.delete(entry.id);
|
|
118
|
-
for (const path of paths) {
|
|
119
|
-
if (++path.depth >= MAX_ARCHIVE_DEPTH || entry.parentId === path.base)
|
|
120
|
-
continue;
|
|
121
|
-
if (entry.parentId !== null) {
|
|
122
|
-
const parents = archived.get(entry.parentId) ?? [];
|
|
123
|
-
parents.push(path);
|
|
124
|
-
archived.set(entry.parentId, parents);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (
|
|
128
|
-
active &&
|
|
129
|
-
Check(Custom, entry) &&
|
|
130
|
-
entry.customType === TREE_ARCHIVE_ENTRY_TYPE &&
|
|
131
|
-
isTreeArchiveManifestData(entry.data) &&
|
|
132
|
-
entry.data.archivedLeafId !== entry.data.branchBaseId
|
|
133
|
-
) {
|
|
134
|
-
const paths = archived.get(entry.data.archivedLeafId) ?? [];
|
|
135
|
-
paths.push({ base: entry.data.branchBaseId, depth: 0 });
|
|
136
|
-
archived.set(entry.data.archivedLeafId, paths);
|
|
137
|
-
}
|
|
138
|
-
if (
|
|
139
|
-
active ||
|
|
140
|
-
paths.length ||
|
|
141
|
-
(Check(Custom, entry) && SHARING_ENTRIES.has(entry.customType))
|
|
142
|
-
)
|
|
143
|
-
entries.push(entry as SessionEntry);
|
|
144
|
-
if (active) branch.push(entry as SessionEntry);
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
if (Check(Custom, entry)) {
|
|
148
|
-
if (SHARING_ENTRIES.has(entry.customType)) entries.push(entry);
|
|
149
|
-
else if (active && NOTE_ENTRIES.has(entry.customType))
|
|
150
|
-
branch.push(entry);
|
|
151
|
-
else continue;
|
|
152
|
-
} else if (active && Check(Handoff, entry)) branch.push(entry);
|
|
153
|
-
else continue;
|
|
154
|
-
}
|
|
155
|
-
const after = await file.stat();
|
|
156
|
-
if (before.size !== after.size || before.mtimeMs !== after.mtimeMs)
|
|
157
|
-
throw new Error("Saved notes changed during the read. Retry the lookup.");
|
|
158
|
-
if (!threadId || (target !== undefined && target !== null))
|
|
159
|
-
throw new Error("Saved notes have an incomplete session branch");
|
|
160
|
-
return {
|
|
161
|
-
threadId,
|
|
162
|
-
entries: entries.reverse(),
|
|
163
|
-
branch: branch.reverse(),
|
|
164
|
-
savedAt: before.mtimeMs,
|
|
165
|
-
};
|
|
166
|
-
} finally {
|
|
167
|
-
await file.close();
|
|
168
|
-
}
|
|
169
|
-
}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { type Static, Type } from "typebox";
|
|
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";
|
|
10
|
-
import type { SavedNotesSession } from "./saved-session.js";
|
|
11
|
-
|
|
12
|
-
const Agent = Type.String({ pattern: CONTEXT_AGENT_PATTERN });
|
|
13
|
-
export const Identity = Type.Object({
|
|
14
|
-
protocol: Type.Literal(1),
|
|
15
|
-
sessionId: Type.String(),
|
|
16
|
-
threadId: Type.String(),
|
|
17
|
-
agentName: Agent,
|
|
18
|
-
storageAgentName: Type.Optional(Agent),
|
|
19
|
-
storage: Type.Optional(
|
|
20
|
-
Type.Union([Type.Literal("session"), Type.Literal("remote")]),
|
|
21
|
-
),
|
|
22
|
-
routing: Type.Optional(Type.Unknown()),
|
|
23
|
-
});
|
|
24
|
-
export const Member = Type.Object({
|
|
25
|
-
threadId: Type.String(),
|
|
26
|
-
sessionId: Type.String(),
|
|
27
|
-
agentName: Agent,
|
|
28
|
-
machine: Type.String(),
|
|
29
|
-
sessionFile: Type.String(),
|
|
30
|
-
});
|
|
31
|
-
export const Plan = Type.Object({
|
|
32
|
-
controllerSessionId: Type.String(),
|
|
33
|
-
targetSessionId: Type.String(),
|
|
34
|
-
context: Type.Optional(
|
|
35
|
-
Type.Object({
|
|
36
|
-
controller: Identity,
|
|
37
|
-
target: Identity,
|
|
38
|
-
alias: Agent,
|
|
39
|
-
controllerAlias: Agent,
|
|
40
|
-
controllerFile: Type.Optional(Type.String()),
|
|
41
|
-
controllerNotes: Type.Unknown(),
|
|
42
|
-
targetNotes: Type.Unknown(),
|
|
43
|
-
}),
|
|
44
|
-
),
|
|
45
|
-
});
|
|
46
|
-
export const Route = Type.Object({
|
|
47
|
-
plan: Plan,
|
|
48
|
-
machine: Type.String(),
|
|
49
|
-
sessionFile: Type.String(),
|
|
50
|
-
phase: Type.Union([Type.Literal("pending"), Type.Literal("ready")]),
|
|
51
|
-
detached: Type.Optional(
|
|
52
|
-
Type.Object({ context: Type.Optional(Type.Unknown()) }),
|
|
53
|
-
),
|
|
54
|
-
});
|
|
55
|
-
export const Detachment = Type.Object({
|
|
56
|
-
plan: Plan,
|
|
57
|
-
context: Type.Optional(Type.Unknown()),
|
|
58
|
-
});
|
|
59
|
-
export const Snapshot = Type.Object({
|
|
60
|
-
protocol: Type.Literal(1),
|
|
61
|
-
timestamp: Type.Number(),
|
|
62
|
-
files: Type.Array(
|
|
63
|
-
Type.Object({
|
|
64
|
-
path: Type.String(),
|
|
65
|
-
text: Type.String(),
|
|
66
|
-
createdAt: Type.Number(),
|
|
67
|
-
updatedAt: Type.Number(),
|
|
68
|
-
operation: Type.Union([
|
|
69
|
-
Type.Literal("local_append_overlay"),
|
|
70
|
-
Type.Literal("local_replacement"),
|
|
71
|
-
]),
|
|
72
|
-
}),
|
|
73
|
-
),
|
|
74
|
-
});
|
|
75
|
-
export const Parent = Type.Object({
|
|
76
|
-
transport: Type.Literal("shepherdr"),
|
|
77
|
-
parent: Type.String(),
|
|
78
|
-
});
|
|
79
|
-
export type Identity = Static<typeof Identity>;
|
|
80
|
-
|
|
81
|
-
/** Persisted sharing is session-scoped. Forks never inherit the source agent's address. */
|
|
82
|
-
export function savedNotesIdentity(
|
|
83
|
-
session: Pick<SavedNotesSession, "threadId" | "entries">,
|
|
84
|
-
): Identity {
|
|
85
|
-
let found: Identity | undefined;
|
|
86
|
-
for (const entry of session.entries) {
|
|
87
|
-
if (entry.type !== "custom" || entry.customType !== "codex-context-agent")
|
|
88
|
-
continue;
|
|
89
|
-
if (!Check(Identity, entry.data))
|
|
90
|
-
throw new Error("Invalid saved notes identity");
|
|
91
|
-
if (entry.data.threadId !== session.threadId) continue;
|
|
92
|
-
if (found) throw new Error("Conflicting saved notes identities");
|
|
93
|
-
found = entry.data;
|
|
94
|
-
}
|
|
95
|
-
return canonicalContextIdentity(
|
|
96
|
-
found ??
|
|
97
|
-
pinnedContextIdentity(session.threadId, session.entries) ?? {
|
|
98
|
-
protocol: 1,
|
|
99
|
-
threadId: session.threadId,
|
|
100
|
-
sessionId: session.threadId,
|
|
101
|
-
agentName: canonicalAgentName(session.threadId),
|
|
102
|
-
},
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function sameOwner(a: Identity, b: Identity): boolean {
|
|
107
|
-
return (
|
|
108
|
-
a.threadId === b.threadId &&
|
|
109
|
-
a.sessionId === b.sessionId &&
|
|
110
|
-
contextStorageAgentName(a) === contextStorageAgentName(b)
|
|
111
|
-
);
|
|
112
|
-
}
|
package/src/shared-notes.ts
DELETED
|
@@ -1,290 +0,0 @@
|
|
|
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 { HistorySearch } from "./history-search.js";
|
|
13
|
-
import {
|
|
14
|
-
readSavedNotesSession,
|
|
15
|
-
type SavedNotesSession,
|
|
16
|
-
} from "./saved-session.js";
|
|
17
|
-
import {
|
|
18
|
-
Detachment,
|
|
19
|
-
Member,
|
|
20
|
-
Parent,
|
|
21
|
-
Plan,
|
|
22
|
-
Route,
|
|
23
|
-
sameOwner,
|
|
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
|
-
/^(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)/.test(
|
|
61
|
-
error.message,
|
|
62
|
-
)
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Saved local notes do not need PCC, a router, or a running owner. Writes still belong to the live session. */
|
|
67
|
-
export async function readSharedNotes(
|
|
68
|
-
ctx: ExtensionContext,
|
|
69
|
-
store: Pick<NotesStore, "read">,
|
|
70
|
-
params: Record<string, unknown>,
|
|
71
|
-
signal?: AbortSignal,
|
|
72
|
-
): Promise<Record<string, unknown> | undefined> {
|
|
73
|
-
return readSharedSession(ctx, store, params, { namespace: "notes" }, signal);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export async function readSharedHistory(
|
|
77
|
-
ctx: ExtensionContext,
|
|
78
|
-
store: Pick<NotesStore, "read">,
|
|
79
|
-
search: HistorySearch,
|
|
80
|
-
params: Record<string, unknown>,
|
|
81
|
-
signal?: AbortSignal,
|
|
82
|
-
): Promise<Record<string, unknown> | undefined> {
|
|
83
|
-
return readSharedSession(
|
|
84
|
-
ctx,
|
|
85
|
-
store,
|
|
86
|
-
params,
|
|
87
|
-
{ namespace: "history", search },
|
|
88
|
-
signal,
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function readSharedSession(
|
|
93
|
-
ctx: ExtensionContext,
|
|
94
|
-
store: Pick<NotesStore, "read">,
|
|
95
|
-
params: Record<string, unknown>,
|
|
96
|
-
access:
|
|
97
|
-
| { namespace: "notes" }
|
|
98
|
-
| { namespace: "history"; search: HistorySearch },
|
|
99
|
-
signal?: AbortSignal,
|
|
100
|
-
): Promise<Record<string, unknown> | undefined> {
|
|
101
|
-
const { namespace } = access;
|
|
102
|
-
if (
|
|
103
|
-
namespace === "notes" &&
|
|
104
|
-
!["read_file", "list_files_by_prefix", "search_contents"].includes(
|
|
105
|
-
String(params["action"]),
|
|
106
|
-
)
|
|
107
|
-
)
|
|
108
|
-
return;
|
|
109
|
-
const path =
|
|
110
|
-
namespace === "history"
|
|
111
|
-
? params["agent_name"]
|
|
112
|
-
: (params["path"] ?? params["prefix"] ?? params["path_prefix"]);
|
|
113
|
-
if (typeof path !== "string" || !path.startsWith("/")) return;
|
|
114
|
-
const target =
|
|
115
|
-
namespace === "history"
|
|
116
|
-
? isContextAgentName(path)
|
|
117
|
-
? path
|
|
118
|
-
: undefined
|
|
119
|
-
: contextTargetAgent(
|
|
120
|
-
"notes",
|
|
121
|
-
{ path },
|
|
122
|
-
canonicalAgentName(ctx.sessionManager.getSessionId()),
|
|
123
|
-
);
|
|
124
|
-
if (!target) return;
|
|
125
|
-
const outputAgent = target;
|
|
126
|
-
const current: SavedNotesSession = {
|
|
127
|
-
threadId: ctx.sessionManager.getSessionId(),
|
|
128
|
-
entries: ctx.sessionManager.getEntries(),
|
|
129
|
-
branch: ctx.sessionManager.getBranch(),
|
|
130
|
-
};
|
|
131
|
-
if (contextAgentNames(savedNotesIdentity(current)).includes(target)) return;
|
|
132
|
-
const visited = new Set<string>();
|
|
133
|
-
|
|
134
|
-
const { read, reference } = createSharedSessionReader(
|
|
135
|
-
store,
|
|
136
|
-
access,
|
|
137
|
-
outputAgent,
|
|
138
|
-
signal,
|
|
139
|
-
resolve,
|
|
140
|
-
);
|
|
141
|
-
async function resolve(
|
|
142
|
-
session: SavedNotesSession,
|
|
143
|
-
destination: string,
|
|
144
|
-
query: Record<string, unknown>,
|
|
145
|
-
): Promise<Record<string, unknown> | undefined> {
|
|
146
|
-
signal?.throwIfAborted();
|
|
147
|
-
if (visited.has(session.threadId)) return;
|
|
148
|
-
visited.add(session.threadId);
|
|
149
|
-
const identity = savedNotesIdentity(session);
|
|
150
|
-
if (contextAgentNames(identity).includes(destination)) {
|
|
151
|
-
if (
|
|
152
|
-
namespace === "notes" &&
|
|
153
|
-
identity.storage === "remote" &&
|
|
154
|
-
!session.branch.some(
|
|
155
|
-
(entry) =>
|
|
156
|
-
entry.type === "custom" &&
|
|
157
|
-
entry.customType === "notes-compaction:note:v1",
|
|
158
|
-
)
|
|
159
|
-
)
|
|
160
|
-
return;
|
|
161
|
-
return read(session, query, destination);
|
|
162
|
-
}
|
|
163
|
-
const routes = new Map<string, Static<typeof Route>>();
|
|
164
|
-
for (const value of records(session, "shepherdr-attachment-route"))
|
|
165
|
-
if (
|
|
166
|
-
Check(Route, value) &&
|
|
167
|
-
value.plan.controllerSessionId === identity.threadId
|
|
168
|
-
)
|
|
169
|
-
routes.set(`${value.machine}\0${value.sessionFile}`, value);
|
|
170
|
-
for (const route of routes.values()) {
|
|
171
|
-
const link = route.plan.context;
|
|
172
|
-
if (route.phase !== "ready" || !link) continue;
|
|
173
|
-
const alias =
|
|
174
|
-
destination === canonicalAgentName(link.target.threadId)
|
|
175
|
-
? destination
|
|
176
|
-
: link.alias;
|
|
177
|
-
if (!inside(destination, alias)) continue;
|
|
178
|
-
if (!sameOwner(identity, link.controller))
|
|
179
|
-
throw new Error("Saved shared notes identity changed");
|
|
180
|
-
return reference(
|
|
181
|
-
link.target,
|
|
182
|
-
`${link.target.agentName}${destination.slice(alias.length)}`,
|
|
183
|
-
remap(query, alias, link.target.agentName),
|
|
184
|
-
route.machine === "local" ? route.sessionFile : undefined,
|
|
185
|
-
route.detached?.context ?? link.targetNotes,
|
|
186
|
-
route.detached?.context !== undefined,
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
const owner = records(session, "shepherdr-attachment-owner").findLast(
|
|
190
|
-
(value) =>
|
|
191
|
-
Check(Plan, value) && value.targetSessionId === identity.threadId,
|
|
192
|
-
);
|
|
193
|
-
if (
|
|
194
|
-
Check(Plan, owner) &&
|
|
195
|
-
owner.context &&
|
|
196
|
-
(inside(destination, owner.context.controllerAlias) ||
|
|
197
|
-
destination === canonicalAgentName(owner.context.controller.threadId))
|
|
198
|
-
) {
|
|
199
|
-
const link = owner.context;
|
|
200
|
-
const alias =
|
|
201
|
-
destination === canonicalAgentName(link.controller.threadId)
|
|
202
|
-
? destination
|
|
203
|
-
: link.controllerAlias;
|
|
204
|
-
if (!sameOwner(identity, link.target))
|
|
205
|
-
throw new Error("Saved shared notes identity changed");
|
|
206
|
-
const detached = records(
|
|
207
|
-
session,
|
|
208
|
-
"shepherdr-attachment-detached",
|
|
209
|
-
).findLast(
|
|
210
|
-
(value) =>
|
|
211
|
-
Check(Detachment, value) && isDeepStrictEqual(value.plan, owner),
|
|
212
|
-
);
|
|
213
|
-
const snapshot = Check(Detachment, detached)
|
|
214
|
-
? detached.context
|
|
215
|
-
: undefined;
|
|
216
|
-
return reference(
|
|
217
|
-
link.controller,
|
|
218
|
-
`${link.controller.agentName}${destination.slice(alias.length)}`,
|
|
219
|
-
remap(query, alias, link.controller.agentName),
|
|
220
|
-
link.controllerFile,
|
|
221
|
-
snapshot ?? link.controllerNotes,
|
|
222
|
-
snapshot !== undefined,
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
const members = records(session, "herdr-context-child").filter(
|
|
226
|
-
(value) =>
|
|
227
|
-
Check(Member, value) &&
|
|
228
|
-
value.threadId === identity.threadId &&
|
|
229
|
-
value.sessionId === identity.sessionId,
|
|
230
|
-
);
|
|
231
|
-
for (const member of members) {
|
|
232
|
-
if (!Check(Member, member) || member.machine !== "local") continue;
|
|
233
|
-
const saved = await readSavedNotesSession(
|
|
234
|
-
member.sessionFile,
|
|
235
|
-
signal,
|
|
236
|
-
namespace === "history",
|
|
237
|
-
);
|
|
238
|
-
const child = savedNotesIdentity(saved);
|
|
239
|
-
if (
|
|
240
|
-
child.sessionId !== identity.sessionId ||
|
|
241
|
-
(child.agentName !== member.agentName &&
|
|
242
|
-
contextStorageAgentName(child) !== member.agentName)
|
|
243
|
-
)
|
|
244
|
-
throw new Error(
|
|
245
|
-
"Saved shared notes belong to a different agent family",
|
|
246
|
-
);
|
|
247
|
-
const result = await resolve(saved, destination, query);
|
|
248
|
-
if (result) return result;
|
|
249
|
-
}
|
|
250
|
-
const routing = identity.routing;
|
|
251
|
-
const suffix = ".jsonl.shepherdr-context.json";
|
|
252
|
-
if (Check(Parent, routing) && routing.parent.endsWith(suffix)) {
|
|
253
|
-
const saved = await readSavedNotesSession(
|
|
254
|
-
routing.parent.slice(0, -".shepherdr-context.json".length),
|
|
255
|
-
signal,
|
|
256
|
-
namespace === "history",
|
|
257
|
-
);
|
|
258
|
-
const parent = savedNotesIdentity(saved);
|
|
259
|
-
if (
|
|
260
|
-
parent.sessionId !== identity.sessionId ||
|
|
261
|
-
!records(saved, "herdr-context-child").some(
|
|
262
|
-
(value) =>
|
|
263
|
-
Check(Member, value) &&
|
|
264
|
-
value.threadId === parent.threadId &&
|
|
265
|
-
value.sessionId === parent.sessionId &&
|
|
266
|
-
[identity.agentName, contextStorageAgentName(identity)].includes(
|
|
267
|
-
value.agentName,
|
|
268
|
-
),
|
|
269
|
-
)
|
|
270
|
-
)
|
|
271
|
-
throw new Error("Saved shared notes parent identity changed");
|
|
272
|
-
return resolve(saved, destination, query);
|
|
273
|
-
}
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
try {
|
|
277
|
-
return await resolve(current, target, params);
|
|
278
|
-
} catch (error) {
|
|
279
|
-
if (
|
|
280
|
-
error instanceof Error &&
|
|
281
|
-
"code" in error &&
|
|
282
|
-
["ENOENT", "EACCES", "EPERM"].includes(String(error.code))
|
|
283
|
-
)
|
|
284
|
-
throw new Error(
|
|
285
|
-
`Saved shared ${namespace} are unavailable on this machine. Restore access to the saved Pi JSONL session, then retry.`,
|
|
286
|
-
{ cause: error },
|
|
287
|
-
);
|
|
288
|
-
throw error;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|