@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,113 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import { Check } from "typebox/value";
|
|
3
|
+
import { contextTargetAgent, isContextAgentName } from "./agent-identity.js";
|
|
4
|
+
import type { RemoteNoteReference } from "./context-sharing.js";
|
|
5
|
+
import { normalizeNoteParams, readNotes } from "./notes.js";
|
|
6
|
+
import type { NoteRevision } from "./store.js";
|
|
7
|
+
|
|
8
|
+
const Snapshot = Type.Object({
|
|
9
|
+
protocol: Type.Literal(1),
|
|
10
|
+
timestamp: Type.Number({ minimum: 0 }),
|
|
11
|
+
files: Type.Array(
|
|
12
|
+
Type.Object({
|
|
13
|
+
path: Type.String(),
|
|
14
|
+
text: Type.String(),
|
|
15
|
+
createdAt: Type.Number(),
|
|
16
|
+
updatedAt: Type.Number(),
|
|
17
|
+
operation: Type.Optional(
|
|
18
|
+
Type.Union([
|
|
19
|
+
Type.Literal("local_append_overlay"),
|
|
20
|
+
Type.Literal("local_replacement"),
|
|
21
|
+
]),
|
|
22
|
+
),
|
|
23
|
+
}),
|
|
24
|
+
),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/** Retained checkpoints remain readable without a running owner or database. */
|
|
28
|
+
export function readAttachmentSnapshot(
|
|
29
|
+
snapshot: unknown,
|
|
30
|
+
params: Record<string, unknown>,
|
|
31
|
+
): Record<string, unknown> {
|
|
32
|
+
if (!Check(Snapshot, snapshot))
|
|
33
|
+
throw new Error("Invalid retained note snapshot");
|
|
34
|
+
if (
|
|
35
|
+
!["list_files_by_prefix", "read_file", "search_contents"].includes(
|
|
36
|
+
String(params["action"]),
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
throw new Error(
|
|
40
|
+
"Retained checkpoints are read-only; resume the attached owner to write notes",
|
|
41
|
+
);
|
|
42
|
+
const path = params["path"] ?? params["prefix"] ?? params["path_prefix"];
|
|
43
|
+
const owner = snapshot.files[0]
|
|
44
|
+
? contextTargetAgent("notes", { path: snapshot.files[0].path }, "/root")
|
|
45
|
+
: "/root";
|
|
46
|
+
const agent =
|
|
47
|
+
typeof path === "string" && path.startsWith("/")
|
|
48
|
+
? contextTargetAgent("notes", { path }, "/root")
|
|
49
|
+
: owner;
|
|
50
|
+
const notes = new Map<string, NoteRevision>();
|
|
51
|
+
for (const file of snapshot.files) {
|
|
52
|
+
if (
|
|
53
|
+
!isContextAgentName(
|
|
54
|
+
contextTargetAgent("notes", { path: file.path }, agent),
|
|
55
|
+
) ||
|
|
56
|
+
!file.path.includes("/notes/")
|
|
57
|
+
)
|
|
58
|
+
throw new Error("Invalid retained note path");
|
|
59
|
+
if (!file.path.startsWith(`${agent}/notes/`)) continue;
|
|
60
|
+
const local = file.path.slice(`${agent}/notes/`.length);
|
|
61
|
+
notes.set(local, {
|
|
62
|
+
...file,
|
|
63
|
+
path: local,
|
|
64
|
+
id: file.path,
|
|
65
|
+
mode: file.operation === "local_append_overlay" ? "append" : "replace",
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return readNotes(notes, normalizeNoteParams(params, agent), agent);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function readRemoteNoteReference(input: unknown): RemoteNoteReference {
|
|
72
|
+
if (
|
|
73
|
+
!input ||
|
|
74
|
+
typeof input !== "object" ||
|
|
75
|
+
!("protocol" in input) ||
|
|
76
|
+
input.protocol !== 1 ||
|
|
77
|
+
!("storage" in input) ||
|
|
78
|
+
input.storage !== "remote" ||
|
|
79
|
+
!("timestamp" in input) ||
|
|
80
|
+
typeof input.timestamp !== "number" ||
|
|
81
|
+
!Number.isFinite(input.timestamp) ||
|
|
82
|
+
input.timestamp < 0 ||
|
|
83
|
+
!("baseUrl" in input) ||
|
|
84
|
+
typeof input.baseUrl !== "string" ||
|
|
85
|
+
!input.baseUrl ||
|
|
86
|
+
input.baseUrl.length > 8192 ||
|
|
87
|
+
!("identity" in input) ||
|
|
88
|
+
!input.identity ||
|
|
89
|
+
typeof input.identity !== "object"
|
|
90
|
+
)
|
|
91
|
+
throw new Error("Invalid Remote checkpoint reference");
|
|
92
|
+
const identity = input.identity as Partial<RemoteNoteReference["identity"]>;
|
|
93
|
+
if (
|
|
94
|
+
identity.protocol !== 1 ||
|
|
95
|
+
identity.storage !== "remote" ||
|
|
96
|
+
typeof identity.threadId !== "string" ||
|
|
97
|
+
!identity.threadId ||
|
|
98
|
+
identity.threadId.length > 512 ||
|
|
99
|
+
typeof identity.sessionId !== "string" ||
|
|
100
|
+
!identity.sessionId ||
|
|
101
|
+
identity.sessionId.length > 512 ||
|
|
102
|
+
typeof identity.agentName !== "string" ||
|
|
103
|
+
identity.agentName.length > 4096 ||
|
|
104
|
+
!isContextAgentName(identity.agentName) ||
|
|
105
|
+
(identity.storageAgentName !== undefined &&
|
|
106
|
+
!isContextAgentName(identity.storageAgentName)) ||
|
|
107
|
+
typeof identity.accountScope !== "string" ||
|
|
108
|
+
!/^[a-f0-9]{64}$/.test(identity.accountScope) ||
|
|
109
|
+
identity.backendUrl !== input.baseUrl
|
|
110
|
+
)
|
|
111
|
+
throw new Error("Invalid Remote checkpoint identity");
|
|
112
|
+
return input as RemoteNoteReference;
|
|
113
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionContext,
|
|
3
|
+
SessionEntry,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
export interface ContextFamilyBindingOptions {
|
|
7
|
+
/** Caller asserts entries through this boundary were inherited by a new native fork, before child work. */
|
|
8
|
+
fork?: { lastInheritedEntryId: string };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Classify only child-owned startup records, without modifying inherited history. */
|
|
12
|
+
export function bindingStartupEntries(
|
|
13
|
+
ctx: ExtensionContext,
|
|
14
|
+
options?: ContextFamilyBindingOptions,
|
|
15
|
+
): SessionEntry[] {
|
|
16
|
+
const entries = ctx.sessionManager.getEntries();
|
|
17
|
+
if (!options?.fork) return entries;
|
|
18
|
+
const id = options.fork.lastInheritedEntryId;
|
|
19
|
+
const boundary = entries.findIndex((entry) => entry.id === id);
|
|
20
|
+
if (
|
|
21
|
+
!ctx.sessionManager.getHeader()?.parentSession ||
|
|
22
|
+
typeof id !== "string" ||
|
|
23
|
+
!id ||
|
|
24
|
+
boundary < 0
|
|
25
|
+
)
|
|
26
|
+
throw new Error(
|
|
27
|
+
"Fork family binding requires an inherited entry boundary in a new native fork",
|
|
28
|
+
);
|
|
29
|
+
return entries.slice(boundary + 1);
|
|
30
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type {
|
|
3
|
+
AgentToolResult,
|
|
4
|
+
CustomMessageEntryDraft,
|
|
5
|
+
ExtensionContext,
|
|
6
|
+
SessionEntry,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
|
|
9
|
+
export const OWNER_CHANNEL = "pi-notes-compaction:owner:v1";
|
|
10
|
+
export const TOOL_NAMES = [
|
|
11
|
+
"notes",
|
|
12
|
+
"history",
|
|
13
|
+
"new_context",
|
|
14
|
+
"get_context_remaining",
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
export interface LookupQuery {
|
|
18
|
+
namespace: "notes" | "history";
|
|
19
|
+
params: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Raw backend response, not a reconstructed ciphertext or an old delivery receipt. */
|
|
23
|
+
export interface CachedResponse {
|
|
24
|
+
query: LookupQuery;
|
|
25
|
+
bytes: Uint8Array;
|
|
26
|
+
source: string;
|
|
27
|
+
encoding: string;
|
|
28
|
+
fetchedAt: number;
|
|
29
|
+
coverage: "complete" | "partial" | "unknown";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface WindowIdentity {
|
|
33
|
+
firstWindowId: string;
|
|
34
|
+
currentWindowId: string;
|
|
35
|
+
previousWindowId?: string;
|
|
36
|
+
windowNumber: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface NoteSnapshotData {
|
|
40
|
+
protocol: 1;
|
|
41
|
+
timestamp: number;
|
|
42
|
+
files: Array<{
|
|
43
|
+
path: string;
|
|
44
|
+
text: string;
|
|
45
|
+
createdAt: number;
|
|
46
|
+
updatedAt: number;
|
|
47
|
+
operation: "local_append_overlay" | "local_replacement";
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** JSON-safe raw provenance. Projection belongs to the requesting runtime. */
|
|
52
|
+
export interface SerializedCachedResponse
|
|
53
|
+
extends Omit<CachedResponse, "bytes"> {
|
|
54
|
+
bytesBase64: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface SharedNotesDetails {
|
|
58
|
+
codexHistoryNotes: Record<string, unknown>;
|
|
59
|
+
externalNotesResponses?: SerializedCachedResponse[];
|
|
60
|
+
notesCompactionRouted?: true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface NotesOwner {
|
|
64
|
+
protocol: 1;
|
|
65
|
+
owner: "pi-notes-compaction";
|
|
66
|
+
identity(ctx: ExtensionContext): WindowIdentity | undefined;
|
|
67
|
+
/** Current branch refreshes projection before later lifecycle hooks have run. */
|
|
68
|
+
projectMessages(
|
|
69
|
+
messages: readonly AgentMessage[],
|
|
70
|
+
entries?: readonly SessionEntry[],
|
|
71
|
+
): AgentMessage[];
|
|
72
|
+
projectBranch(entries: readonly SessionEntry[]): SessionEntry[];
|
|
73
|
+
canBind?(ctx: ExtensionContext, entries?: readonly SessionEntry[]): boolean;
|
|
74
|
+
snapshotNotes?(
|
|
75
|
+
entries: readonly SessionEntry[],
|
|
76
|
+
agentName: string,
|
|
77
|
+
): NoteSnapshotData;
|
|
78
|
+
executeShared?(
|
|
79
|
+
query: LookupQuery,
|
|
80
|
+
callId: string,
|
|
81
|
+
ctx: ExtensionContext,
|
|
82
|
+
signal?: AbortSignal,
|
|
83
|
+
): Promise<AgentToolResult<SharedNotesDetails>>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface NotesBridge {
|
|
87
|
+
protocol: 1;
|
|
88
|
+
loadSavedSession?: import("./context-sharing.js").SavedSessionLoader;
|
|
89
|
+
/** Preserve the owner's chronological entry while enabling developer-role delivery. */
|
|
90
|
+
prepareReminder?(entry: CustomMessageEntryDraft): CustomMessageEntryDraft;
|
|
91
|
+
lookup?(
|
|
92
|
+
query: LookupQuery,
|
|
93
|
+
ctx: ExtensionContext,
|
|
94
|
+
signal?: AbortSignal,
|
|
95
|
+
): Promise<readonly CachedResponse[]>;
|
|
96
|
+
canProject?(ctx: ExtensionContext): boolean;
|
|
97
|
+
/** Fresh current-call delivery, including original exec ancestry when nested. */
|
|
98
|
+
projectResult?(
|
|
99
|
+
result: AgentToolResult<unknown>,
|
|
100
|
+
responses: readonly CachedResponse[],
|
|
101
|
+
callId: string,
|
|
102
|
+
ctx: ExtensionContext,
|
|
103
|
+
signal?: AbortSignal,
|
|
104
|
+
relay?: boolean,
|
|
105
|
+
): Promise<AgentToolResult<unknown>>;
|
|
106
|
+
agentName?(ctx: ExtensionContext): string;
|
|
107
|
+
/** Optional family routing. undefined means this agent owns the requested path. */
|
|
108
|
+
route?(
|
|
109
|
+
query: LookupQuery,
|
|
110
|
+
callId: string,
|
|
111
|
+
ctx: ExtensionContext,
|
|
112
|
+
signal?: AbortSignal,
|
|
113
|
+
): Promise<AgentToolResult<unknown> | undefined>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface OwnerRequest {
|
|
117
|
+
protocol: 1;
|
|
118
|
+
ctx?: ExtensionContext;
|
|
119
|
+
projection?(
|
|
120
|
+
owner: Pick<NotesOwner, "projectMessages" | "projectBranch">,
|
|
121
|
+
): void;
|
|
122
|
+
reply(owner: NotesOwner): void;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function queryKey(query: LookupQuery): string {
|
|
126
|
+
return JSON.stringify([
|
|
127
|
+
query.namespace,
|
|
128
|
+
Object.entries(query.params).sort(([a], [b]) => a.localeCompare(b)),
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionContext,
|
|
3
|
+
SessionBeforeCompactEvent,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import {
|
|
6
|
+
buildSessionProjection,
|
|
7
|
+
prepareBranchEntries,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { projectTreeCheckpointBranch } from "./tree-checkpoint.js";
|
|
10
|
+
import { projectTreeHandoffBranch } from "./tree-handoff-read.js";
|
|
11
|
+
import type { Windows } from "./windows.js";
|
|
12
|
+
|
|
13
|
+
export function prepareCompactionProjection(
|
|
14
|
+
event: SessionBeforeCompactEvent,
|
|
15
|
+
ctx: ExtensionContext,
|
|
16
|
+
windows: Windows,
|
|
17
|
+
): { cancel: true } | undefined {
|
|
18
|
+
try {
|
|
19
|
+
const branch = projectTreeHandoffBranch(
|
|
20
|
+
windows.projectBranch(
|
|
21
|
+
projectTreeCheckpointBranch(
|
|
22
|
+
event.branchEntries,
|
|
23
|
+
ctx.sessionManager.getEntries(),
|
|
24
|
+
),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
if (
|
|
28
|
+
branch.length === event.branchEntries.length &&
|
|
29
|
+
branch.every((entry, index) => entry === event.branchEntries[index])
|
|
30
|
+
)
|
|
31
|
+
return;
|
|
32
|
+
const physicalCut = event.branchEntries.findIndex(
|
|
33
|
+
(entry) => entry.id === event.preparation.firstKeptEntryId,
|
|
34
|
+
);
|
|
35
|
+
if (physicalCut < 0) throw new Error("Compaction kept boundary is missing");
|
|
36
|
+
const keptIds = new Set(
|
|
37
|
+
event.branchEntries.slice(physicalCut).map((entry) => entry.id),
|
|
38
|
+
);
|
|
39
|
+
const projection = buildSessionProjection([...branch]);
|
|
40
|
+
const projected = projection.entries;
|
|
41
|
+
const summary = projection.messages.find(
|
|
42
|
+
(message) => message.role === "compactionSummary",
|
|
43
|
+
);
|
|
44
|
+
const cut = projected.findIndex((entry) =>
|
|
45
|
+
keptIds.has(entry.sourceEntry.id),
|
|
46
|
+
);
|
|
47
|
+
if (cut < 0) throw new Error("Current context kept boundary is missing");
|
|
48
|
+
const summarized = projected.slice(0, cut);
|
|
49
|
+
const messages = summarized
|
|
50
|
+
.flatMap((entry) => entry.messages)
|
|
51
|
+
.filter(
|
|
52
|
+
(message) =>
|
|
53
|
+
message.role !== "system" && message.role !== "compactionSummary",
|
|
54
|
+
);
|
|
55
|
+
if (!messages.length) {
|
|
56
|
+
ctx.ui.notify(
|
|
57
|
+
"Nothing to compact in the current context window. The existing context remains.",
|
|
58
|
+
"warning",
|
|
59
|
+
);
|
|
60
|
+
return { cancel: true };
|
|
61
|
+
}
|
|
62
|
+
event.branchEntries = [...branch];
|
|
63
|
+
// Pi retains this object for its default summarizer after every hook.
|
|
64
|
+
// Keep that route's auth, provider hooks and retries, and physical kept IDs.
|
|
65
|
+
delete event.preparation.previousSummary;
|
|
66
|
+
Object.assign(event.preparation, {
|
|
67
|
+
...(summary ? { previousSummary: summary.summary } : {}),
|
|
68
|
+
messagesToSummarize: messages,
|
|
69
|
+
turnPrefixMessages: [],
|
|
70
|
+
isSplitTurn: false,
|
|
71
|
+
fileOps: prepareBranchEntries(
|
|
72
|
+
summarized.flatMap((entry) =>
|
|
73
|
+
entry.messages.map((message) => ({
|
|
74
|
+
...entry.sourceEntry,
|
|
75
|
+
type: "message" as const,
|
|
76
|
+
message,
|
|
77
|
+
})),
|
|
78
|
+
),
|
|
79
|
+
).fileOps,
|
|
80
|
+
});
|
|
81
|
+
} catch (error) {
|
|
82
|
+
ctx.ui.notify(
|
|
83
|
+
`Compaction failed: ${error instanceof Error ? error.message : String(error)}. The existing context remains.`,
|
|
84
|
+
"error",
|
|
85
|
+
);
|
|
86
|
+
return { cancel: true };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { NotesOwner } from "./bridge.js";
|
|
3
|
+
import type { HistorySearch } from "./history-search.js";
|
|
4
|
+
import type { NotesLifecycle } from "./lifecycle.js";
|
|
5
|
+
import { exportNotesSnapshot } from "./notes.js";
|
|
6
|
+
import type { NotesStore } from "./store.js";
|
|
7
|
+
import { createSharedExecutor } from "./tools.js";
|
|
8
|
+
import { WINDOW_MESSAGE, windowDetails } from "./windows.js";
|
|
9
|
+
|
|
10
|
+
export function createNotesContextOwner(
|
|
11
|
+
pi: ExtensionAPI,
|
|
12
|
+
store: NotesStore,
|
|
13
|
+
lifecycle: NotesLifecycle,
|
|
14
|
+
search: HistorySearch,
|
|
15
|
+
): NotesOwner {
|
|
16
|
+
const owner: NotesOwner = {
|
|
17
|
+
protocol: 1,
|
|
18
|
+
owner: "pi-notes-compaction",
|
|
19
|
+
identity: () =>
|
|
20
|
+
lifecycle.windows.current ? { ...lifecycle.windows.current } : undefined,
|
|
21
|
+
projectMessages: (messages, entries) => {
|
|
22
|
+
if (entries) lifecycle.windows.restore(entries);
|
|
23
|
+
const projected = lifecycle.windows.projectMessages(
|
|
24
|
+
messages,
|
|
25
|
+
lifecycle.prepareReminder,
|
|
26
|
+
);
|
|
27
|
+
return lifecycle.active
|
|
28
|
+
? projected
|
|
29
|
+
: projected.filter(
|
|
30
|
+
(message) =>
|
|
31
|
+
message.role !== "custom" ||
|
|
32
|
+
!message.customType.startsWith("notes-compaction:"),
|
|
33
|
+
);
|
|
34
|
+
},
|
|
35
|
+
projectBranch: (entries) => lifecycle.windows.projectBranch(entries),
|
|
36
|
+
canBind: (ctx, entries = ctx.sessionManager.getEntries()) =>
|
|
37
|
+
lifecycle.active &&
|
|
38
|
+
ctx.isIdle() &&
|
|
39
|
+
entries.every((entry) => {
|
|
40
|
+
if (
|
|
41
|
+
entry.type === "custom" &&
|
|
42
|
+
entry.customType === "codex-agent-relationship"
|
|
43
|
+
)
|
|
44
|
+
return (
|
|
45
|
+
(entry.data as { threadId?: unknown } | undefined)?.threadId ===
|
|
46
|
+
ctx.sessionManager.getSessionId()
|
|
47
|
+
);
|
|
48
|
+
if (
|
|
49
|
+
entry.type === "custom" &&
|
|
50
|
+
entry.customType === "herdr-agents-monitor-state"
|
|
51
|
+
) {
|
|
52
|
+
const data = entry.data as
|
|
53
|
+
| { version?: unknown; agents?: unknown }
|
|
54
|
+
| undefined;
|
|
55
|
+
return Boolean(
|
|
56
|
+
data?.version === 2 &&
|
|
57
|
+
Array.isArray(data.agents) &&
|
|
58
|
+
data.agents.length === 0,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
// Foreign plain custom entries may be startup metadata. Notes state
|
|
62
|
+
// proves prior ownership or work, even without a delivered first turn.
|
|
63
|
+
if (entry.type === "custom")
|
|
64
|
+
return (
|
|
65
|
+
!entry.customType.startsWith("notes-compaction:") &&
|
|
66
|
+
![
|
|
67
|
+
"codex-context-agent",
|
|
68
|
+
"codex-context-backend",
|
|
69
|
+
"codex-context-note",
|
|
70
|
+
"codex-context-note-snapshot",
|
|
71
|
+
"codex-context-window",
|
|
72
|
+
"codex-context-tree-archive",
|
|
73
|
+
"codex-note-save-marker",
|
|
74
|
+
].includes(entry.customType)
|
|
75
|
+
);
|
|
76
|
+
if (
|
|
77
|
+
["model_change", "thinking_level_change", "session_info"].includes(
|
|
78
|
+
entry.type,
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
return true;
|
|
82
|
+
if (
|
|
83
|
+
entry.type !== "custom_message" ||
|
|
84
|
+
entry.customType !== WINDOW_MESSAGE
|
|
85
|
+
)
|
|
86
|
+
return false;
|
|
87
|
+
const details = windowDetails(entry);
|
|
88
|
+
return Boolean(
|
|
89
|
+
details &&
|
|
90
|
+
details.identity.windowNumber === 0 &&
|
|
91
|
+
!details.trim &&
|
|
92
|
+
(details.kind === "window" || details.kind === "identity"),
|
|
93
|
+
);
|
|
94
|
+
}),
|
|
95
|
+
snapshotNotes: exportNotesSnapshot,
|
|
96
|
+
executeShared: createSharedExecutor(pi, store, lifecycle, search),
|
|
97
|
+
};
|
|
98
|
+
return owner;
|
|
99
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentToolResult,
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type {
|
|
7
|
+
ContextAgentBinding,
|
|
8
|
+
ContextAgentIdentity,
|
|
9
|
+
} from "./agent-identity.js";
|
|
10
|
+
import type { NoteSnapshotData, SerializedCachedResponse } from "./bridge.js";
|
|
11
|
+
|
|
12
|
+
export type {
|
|
13
|
+
ContextAgentBinding,
|
|
14
|
+
ContextAgentIdentity,
|
|
15
|
+
} from "./agent-identity.js";
|
|
16
|
+
export {
|
|
17
|
+
canonicalAgentName,
|
|
18
|
+
canonicalContextIdentity,
|
|
19
|
+
contextAgentNames,
|
|
20
|
+
contextStorageAgentName,
|
|
21
|
+
contextTargetAgent,
|
|
22
|
+
isContextAgentName,
|
|
23
|
+
} from "./agent-identity.js";
|
|
24
|
+
export type { ContextFamilyBindingOptions } from "./binding-startup.js";
|
|
25
|
+
export type { NoteSnapshotData } from "./bridge.js";
|
|
26
|
+
|
|
27
|
+
export interface SharedContextRequest {
|
|
28
|
+
sessionId: string;
|
|
29
|
+
agentName: string;
|
|
30
|
+
namespace: "history" | "notes";
|
|
31
|
+
params: Record<string, unknown>;
|
|
32
|
+
/** Legacy encrypted arguments are rejected by local storage. */
|
|
33
|
+
encryptedArguments?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type SharedContextResult = AgentToolResult<{
|
|
37
|
+
codexHistoryNotes: Record<string, unknown>;
|
|
38
|
+
externalNotesResponses?: SerializedCachedResponse[];
|
|
39
|
+
}>;
|
|
40
|
+
|
|
41
|
+
export type ContextRouter = ((
|
|
42
|
+
ctx: ExtensionContext,
|
|
43
|
+
request: SharedContextRequest,
|
|
44
|
+
signal?: AbortSignal,
|
|
45
|
+
) => Promise<SharedContextResult | undefined>) & {
|
|
46
|
+
requiresRemoteScope?(ctx: ExtensionContext): boolean;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export interface SavedSessionLocator {
|
|
50
|
+
machine: string;
|
|
51
|
+
sessionFile: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type SavedSessionLoader = ((
|
|
55
|
+
locator: SavedSessionLocator,
|
|
56
|
+
history: boolean,
|
|
57
|
+
signal?: AbortSignal,
|
|
58
|
+
) => Promise<unknown>) & {
|
|
59
|
+
/** Exact native-thread discovery only; returned locators are untrusted hints. */
|
|
60
|
+
find?(threadId: string, signal?: AbortSignal): Promise<unknown>;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Remote checkpoints retain an authenticated address, never decrypted note contents. */
|
|
64
|
+
export interface RemoteNoteReference {
|
|
65
|
+
protocol: 1;
|
|
66
|
+
storage: "remote";
|
|
67
|
+
timestamp: number;
|
|
68
|
+
identity: ContextAgentIdentity;
|
|
69
|
+
baseUrl: string;
|
|
70
|
+
}
|
|
71
|
+
export type AttachmentNotesData = NoteSnapshotData | RemoteNoteReference;
|
|
72
|
+
|
|
73
|
+
export interface ContextSharingService {
|
|
74
|
+
protocol: 1;
|
|
75
|
+
canCreateChild(ctx: ExtensionContext): boolean;
|
|
76
|
+
describe(ctx: ExtensionContext): ContextAgentIdentity | undefined;
|
|
77
|
+
verify(ctx: ExtensionContext): Promise<void>;
|
|
78
|
+
createChild(
|
|
79
|
+
ctx: ExtensionContext,
|
|
80
|
+
options: { threadId: string; routing?: unknown },
|
|
81
|
+
): Promise<{
|
|
82
|
+
binding: ContextAgentBinding;
|
|
83
|
+
adopt(): Promise<void>;
|
|
84
|
+
}>;
|
|
85
|
+
bind(
|
|
86
|
+
ctx: ExtensionContext,
|
|
87
|
+
binding: unknown,
|
|
88
|
+
options?: import("./binding-startup.js").ContextFamilyBindingOptions,
|
|
89
|
+
): Promise<ContextAgentIdentity>;
|
|
90
|
+
/** Inspect an idle standalone owner for alias attachment without rebinding its identity. */
|
|
91
|
+
inspectAttachment?(
|
|
92
|
+
ctx: ExtensionContext,
|
|
93
|
+
standalone?: boolean,
|
|
94
|
+
): Promise<ContextAgentIdentity> | ContextAgentIdentity;
|
|
95
|
+
/** Keep the native family identity when retaining a live attachment. */
|
|
96
|
+
retainAttachmentIdentity?(ctx: ExtensionContext): ContextAgentIdentity;
|
|
97
|
+
exportAttachmentNotes?(
|
|
98
|
+
ctx: ExtensionContext,
|
|
99
|
+
): Promise<AttachmentNotesData> | AttachmentNotesData;
|
|
100
|
+
validateAttachmentNotes?(
|
|
101
|
+
snapshot: unknown,
|
|
102
|
+
identity: ContextAgentIdentity,
|
|
103
|
+
): void;
|
|
104
|
+
verifyAttachmentAccess?(
|
|
105
|
+
ctx: ExtensionContext,
|
|
106
|
+
snapshot: unknown,
|
|
107
|
+
): Promise<void>;
|
|
108
|
+
executeRemoteAttachment?(
|
|
109
|
+
ctx: ExtensionContext,
|
|
110
|
+
reference: unknown,
|
|
111
|
+
request: SharedContextRequest,
|
|
112
|
+
signal?: AbortSignal,
|
|
113
|
+
): Promise<SharedContextResult>;
|
|
114
|
+
/** Parse only the verified owner's persisted active note branch. */
|
|
115
|
+
parseAttachmentNotes?(
|
|
116
|
+
entries: unknown,
|
|
117
|
+
identity: ContextAgentIdentity,
|
|
118
|
+
): NoteSnapshotData;
|
|
119
|
+
readAttachmentNotes?(
|
|
120
|
+
snapshot: unknown,
|
|
121
|
+
params: Record<string, unknown>,
|
|
122
|
+
annotation?: {
|
|
123
|
+
read_only: true;
|
|
124
|
+
captured_at: string;
|
|
125
|
+
saved_at?: string;
|
|
126
|
+
warning: string;
|
|
127
|
+
},
|
|
128
|
+
): SharedContextResult;
|
|
129
|
+
execute(
|
|
130
|
+
ctx: ExtensionContext,
|
|
131
|
+
request: SharedContextRequest,
|
|
132
|
+
signal?: AbortSignal,
|
|
133
|
+
): Promise<SharedContextResult>;
|
|
134
|
+
registerRouter(router: ContextRouter): () => void;
|
|
135
|
+
registerSavedSessionLoader?(loader: SavedSessionLoader): () => void;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const CONTEXT_SHARING_AVAILABLE =
|
|
139
|
+
"pi-notes-compaction:context-sharing:available";
|
|
140
|
+
export const CONTEXT_SHARING_REQUEST =
|
|
141
|
+
"pi-notes-compaction:context-sharing:request";
|
|
142
|
+
|
|
143
|
+
/** Optional, load-order-independent integration. No provider or tool schema changes. */
|
|
144
|
+
export function connectNotesContextSharing(
|
|
145
|
+
pi: ExtensionAPI,
|
|
146
|
+
onAvailable?: () => void,
|
|
147
|
+
): {
|
|
148
|
+
readonly service: ContextSharingService | undefined;
|
|
149
|
+
dispose(): void;
|
|
150
|
+
} {
|
|
151
|
+
let service: ContextSharingService | undefined;
|
|
152
|
+
const off = pi.events.on(CONTEXT_SHARING_AVAILABLE, (value) => {
|
|
153
|
+
const candidate = value as Partial<ContextSharingService> | undefined;
|
|
154
|
+
if (
|
|
155
|
+
candidate?.protocol === 1 &&
|
|
156
|
+
typeof candidate.canCreateChild === "function" &&
|
|
157
|
+
typeof candidate.describe === "function" &&
|
|
158
|
+
typeof candidate.verify === "function" &&
|
|
159
|
+
typeof candidate.createChild === "function" &&
|
|
160
|
+
typeof candidate.bind === "function" &&
|
|
161
|
+
typeof candidate.execute === "function" &&
|
|
162
|
+
typeof candidate.registerRouter === "function"
|
|
163
|
+
) {
|
|
164
|
+
service = candidate as ContextSharingService;
|
|
165
|
+
onAvailable?.();
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
pi.events.emit(CONTEXT_SHARING_REQUEST, { protocol: 1 });
|
|
169
|
+
return {
|
|
170
|
+
get service() {
|
|
171
|
+
return service;
|
|
172
|
+
},
|
|
173
|
+
dispose() {
|
|
174
|
+
off();
|
|
175
|
+
service = undefined;
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|