@howaboua/pi-notes-compaction 0.0.9 → 0.0.11
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 +28 -5
- package/PCC-INTEGRATION.md +7 -98
- package/README.md +21 -13
- package/package.json +7 -3
- package/src/activation.ts +152 -0
- package/src/agent-identity.ts +119 -0
- package/src/attachment-snapshot.ts +103 -0
- package/src/binding-startup.ts +30 -0
- package/src/bridge.ts +1 -43
- package/src/compaction-projection.ts +88 -0
- package/src/context-owner.ts +103 -0
- package/src/context-sharing.ts +144 -0
- package/src/extension-hooks.ts +198 -0
- package/src/extension-sharing.ts +118 -0
- package/src/fresh-checkpoint.ts +117 -0
- package/src/fresh-notes.ts +141 -26
- package/src/history-search-protocol.ts +39 -0
- package/src/history-search-pruning.mjs +120 -0
- package/src/history-search-worker.mjs +258 -0
- package/src/history-search.ts +269 -0
- package/src/history.ts +212 -40
- package/src/idle-admission.ts +201 -0
- package/src/index.ts +71 -401
- package/src/integrations.ts +159 -0
- package/src/lifecycle.ts +63 -448
- package/src/note-save-marker.ts +112 -0
- package/src/notes-lookup.ts +112 -0
- package/src/notes.ts +24 -28
- package/src/query-executor.ts +190 -0
- package/src/remote.ts +357 -0
- package/src/rollover.ts +212 -0
- package/src/saved-session.ts +44 -7
- package/src/session-pruning-content.ts +237 -0
- package/src/session-pruning-plan.ts +243 -0
- package/src/session-pruning-policy.ts +20 -0
- package/src/session-pruning-screen.ts +204 -0
- package/src/session-pruning-source.ts +44 -0
- package/src/session-pruning-transaction.ts +141 -0
- package/src/session-pruning.ts +155 -0
- package/src/settings-command.ts +77 -0
- package/src/settings-screen.ts +84 -19
- package/src/settings.ts +143 -11
- package/src/shared-notes.ts +37 -14
- package/src/sharing-service.ts +348 -0
- package/src/sqlite.mjs +33 -0
- package/src/store.ts +46 -13
- package/src/task-boundary.ts +6 -1
- package/src/tool-contracts.ts +279 -0
- package/src/tools.ts +19 -560
- package/src/tree-archive.ts +149 -0
- package/src/tree-checkpoint.ts +170 -0
- package/src/tree-handoff-read.ts +192 -0
- package/src/window-checkpoint.ts +105 -0
- package/src/windows.ts +25 -9
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { windowDetails } from "./windows.js";
|
|
3
|
+
|
|
4
|
+
const PRUNED_ENTRY = "notes-compaction:pruned:v1";
|
|
5
|
+
const PROMPT_STATE = new Set([
|
|
6
|
+
"codex-toolkit-update",
|
|
7
|
+
"codex-current-time-reminder",
|
|
8
|
+
"codex-notebook-status",
|
|
9
|
+
"codex-reasoning-update",
|
|
10
|
+
"codex-context-briefing",
|
|
11
|
+
"codex-remote-delivery",
|
|
12
|
+
"codex-context-idle-checkpoint",
|
|
13
|
+
"codex-note-save-marker",
|
|
14
|
+
"notes-compaction:settled:v1",
|
|
15
|
+
"notes-compaction:run:v1",
|
|
16
|
+
"notes-compaction:nested-write:v1",
|
|
17
|
+
"notes-compaction:continuation",
|
|
18
|
+
]);
|
|
19
|
+
const KNOWN_STATE = new Set([
|
|
20
|
+
"codex-context-agent",
|
|
21
|
+
"codex-context-backend",
|
|
22
|
+
"codex-agent-relationship",
|
|
23
|
+
"notes-compaction:note:v1",
|
|
24
|
+
"notes-compaction:cache:v1",
|
|
25
|
+
"codex-context-note",
|
|
26
|
+
"codex-context-note-snapshot",
|
|
27
|
+
"pi-codex-conversion-notebook-tree-epoch",
|
|
28
|
+
"herdr-context-child",
|
|
29
|
+
"shepherdr-attachment-route",
|
|
30
|
+
"shepherdr-attachment-owner",
|
|
31
|
+
"shepherdr-attachment-detached",
|
|
32
|
+
"herdr-agents-monitor-state",
|
|
33
|
+
"notes-session-prune",
|
|
34
|
+
PRUNED_ENTRY,
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
function object(value: unknown): Record<string, unknown> | undefined {
|
|
38
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
39
|
+
? (value as Record<string, unknown>)
|
|
40
|
+
: undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function deliveryId(value: {
|
|
44
|
+
customType: string;
|
|
45
|
+
details?: unknown;
|
|
46
|
+
}): string | undefined {
|
|
47
|
+
const details = object(value.details);
|
|
48
|
+
const metadata =
|
|
49
|
+
value.customType === "codex-developer-message"
|
|
50
|
+
? details
|
|
51
|
+
: object(details?.["@howaboua/pi-codex-conversion/developer-message"]);
|
|
52
|
+
return typeof metadata?.["id"] === "string" ? metadata["id"] : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function pendingId(entry: SessionEntry): string | undefined {
|
|
56
|
+
if (entry.type !== "custom") return;
|
|
57
|
+
if (
|
|
58
|
+
entry.customType !== "codex-pending-developer-message" &&
|
|
59
|
+
entry.customType !== "herdr-pending-message"
|
|
60
|
+
)
|
|
61
|
+
return;
|
|
62
|
+
const message = object(object(entry.data)?.["message"]);
|
|
63
|
+
if (!message || typeof message["customType"] !== "string")
|
|
64
|
+
throw new Error("Invalid pending coordination state.");
|
|
65
|
+
if (entry.customType === "herdr-pending-message") {
|
|
66
|
+
const id = object(message["details"])?.["herdrAdmissionId"];
|
|
67
|
+
return typeof id === "string" ? id : undefined;
|
|
68
|
+
}
|
|
69
|
+
return deliveryId({
|
|
70
|
+
customType: message["customType"],
|
|
71
|
+
details: message["details"],
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Only complete owner snapshots are coalesced; unknown/delta protocols stay intact. */
|
|
76
|
+
export function snapshotKey(entry: SessionEntry): string | undefined {
|
|
77
|
+
if (entry.type !== "custom") return;
|
|
78
|
+
if (entry.customType === "herdr-agents-monitor-state") return "monitor";
|
|
79
|
+
if (entry.customType === "pi-codex-conversion-notebook-tree-epoch")
|
|
80
|
+
return "notebook-epoch";
|
|
81
|
+
if (entry.customType === "notes-compaction:note:v1") {
|
|
82
|
+
const revision = object(object(entry.data)?.["revision"]);
|
|
83
|
+
if (typeof revision?.["path"] === "string")
|
|
84
|
+
return `note:${revision["path"]}`;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function opaqueState(entry: SessionEntry): string | undefined {
|
|
89
|
+
if (
|
|
90
|
+
entry.type !== "custom" ||
|
|
91
|
+
KNOWN_STATE.has(entry.customType) ||
|
|
92
|
+
PROMPT_STATE.has(entry.customType) ||
|
|
93
|
+
entry.customType.startsWith("shepherdr-board-") ||
|
|
94
|
+
pendingId(entry)
|
|
95
|
+
)
|
|
96
|
+
return;
|
|
97
|
+
return entry.customType;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function tombstone(entry: SessionEntry): SessionEntry {
|
|
101
|
+
return {
|
|
102
|
+
type: "custom",
|
|
103
|
+
customType: PRUNED_ENTRY,
|
|
104
|
+
id: entry.id,
|
|
105
|
+
parentId: entry.parentId,
|
|
106
|
+
timestamp: entry.timestamp,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Retired conversation is reconstructed from visible blocks, never replay/details. */
|
|
111
|
+
export function pruneContent(
|
|
112
|
+
entry: SessionEntry,
|
|
113
|
+
full: boolean,
|
|
114
|
+
expire: boolean,
|
|
115
|
+
stateNeeded: boolean,
|
|
116
|
+
delivered: ReadonlySet<string>,
|
|
117
|
+
): SessionEntry {
|
|
118
|
+
if (entry.type === "message") {
|
|
119
|
+
const message = entry.message;
|
|
120
|
+
if (message.role === "user") {
|
|
121
|
+
if (expire) return tombstone(entry);
|
|
122
|
+
if (full) return entry;
|
|
123
|
+
return {
|
|
124
|
+
type: "message",
|
|
125
|
+
id: entry.id,
|
|
126
|
+
parentId: entry.parentId,
|
|
127
|
+
timestamp: entry.timestamp,
|
|
128
|
+
message: {
|
|
129
|
+
role: "user",
|
|
130
|
+
content: message.content,
|
|
131
|
+
timestamp: message.timestamp,
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (message.role === "assistant") {
|
|
136
|
+
if (full && !expire) return entry;
|
|
137
|
+
const content = message.content
|
|
138
|
+
.filter((part) =>
|
|
139
|
+
full ? part.type !== "text" : !expire && part.type === "text",
|
|
140
|
+
)
|
|
141
|
+
.map((part) =>
|
|
142
|
+
part.type === "text"
|
|
143
|
+
? { type: "text" as const, text: part.text }
|
|
144
|
+
: part,
|
|
145
|
+
);
|
|
146
|
+
if (!content.length) return tombstone(entry);
|
|
147
|
+
return {
|
|
148
|
+
type: "message",
|
|
149
|
+
id: entry.id,
|
|
150
|
+
parentId: entry.parentId,
|
|
151
|
+
timestamp: entry.timestamp,
|
|
152
|
+
message: {
|
|
153
|
+
role: "assistant",
|
|
154
|
+
content,
|
|
155
|
+
api: message.api,
|
|
156
|
+
provider: message.provider,
|
|
157
|
+
model: message.model,
|
|
158
|
+
usage: message.usage,
|
|
159
|
+
stopReason: full ? message.stopReason : "stop",
|
|
160
|
+
timestamp: message.timestamp,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return full ? entry : tombstone(entry);
|
|
165
|
+
}
|
|
166
|
+
if (full) return entry;
|
|
167
|
+
if (entry.type === "custom_message") {
|
|
168
|
+
const window = windowDetails(entry);
|
|
169
|
+
const identity = window && {
|
|
170
|
+
firstWindowId: window.identity.firstWindowId,
|
|
171
|
+
currentWindowId: window.identity.currentWindowId,
|
|
172
|
+
windowNumber: window.identity.windowNumber,
|
|
173
|
+
...(window.identity.previousWindowId === undefined
|
|
174
|
+
? {}
|
|
175
|
+
: { previousWindowId: window.identity.previousWindowId }),
|
|
176
|
+
};
|
|
177
|
+
return window
|
|
178
|
+
? {
|
|
179
|
+
...entry,
|
|
180
|
+
content: "",
|
|
181
|
+
details:
|
|
182
|
+
entry.customType === "codex-context-window"
|
|
183
|
+
? {
|
|
184
|
+
contextManagement: {
|
|
185
|
+
kind: window.kind,
|
|
186
|
+
...identity,
|
|
187
|
+
trimPreviousWindow: window.trim,
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
: { protocol: 1, kind: window.kind, identity, trim: window.trim },
|
|
191
|
+
}
|
|
192
|
+
: tombstone(entry);
|
|
193
|
+
}
|
|
194
|
+
if (entry.type === "custom") {
|
|
195
|
+
const pending = pendingId(entry);
|
|
196
|
+
if (pending && delivered.has(pending)) return tombstone(entry);
|
|
197
|
+
if (
|
|
198
|
+
PROMPT_STATE.has(entry.customType) ||
|
|
199
|
+
(snapshotKey(entry) && !stateNeeded)
|
|
200
|
+
)
|
|
201
|
+
return tombstone(entry);
|
|
202
|
+
return entry;
|
|
203
|
+
}
|
|
204
|
+
if (entry.type === "compaction")
|
|
205
|
+
return {
|
|
206
|
+
type: "compaction",
|
|
207
|
+
id: entry.id,
|
|
208
|
+
parentId: entry.parentId,
|
|
209
|
+
timestamp: entry.timestamp,
|
|
210
|
+
firstKeptEntryId: entry.firstKeptEntryId,
|
|
211
|
+
summary: "",
|
|
212
|
+
tokensBefore: 0,
|
|
213
|
+
...(object(entry.details)?.["pi-notes-compaction/window-checkpoint"] === 1
|
|
214
|
+
? { details: { "pi-notes-compaction/window-checkpoint": 1 } }
|
|
215
|
+
: {}),
|
|
216
|
+
};
|
|
217
|
+
if (entry.type === "branch_summary")
|
|
218
|
+
return {
|
|
219
|
+
type: "branch_summary",
|
|
220
|
+
id: entry.id,
|
|
221
|
+
parentId: entry.parentId,
|
|
222
|
+
timestamp: entry.timestamp,
|
|
223
|
+
fromId: entry.fromId,
|
|
224
|
+
summary: "",
|
|
225
|
+
...(object(entry.details)?.["codexContextNoteHandoff"]
|
|
226
|
+
? {
|
|
227
|
+
details: {
|
|
228
|
+
codexContextNoteHandoff: object(entry.details)?.[
|
|
229
|
+
"codexContextNoteHandoff"
|
|
230
|
+
],
|
|
231
|
+
},
|
|
232
|
+
}
|
|
233
|
+
: {}),
|
|
234
|
+
};
|
|
235
|
+
if (entry.type === "context_edit") return tombstone(entry);
|
|
236
|
+
return entry;
|
|
237
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { type Stats, statSync } from "node:fs";
|
|
2
|
+
import {
|
|
3
|
+
buildContextEntries,
|
|
4
|
+
type ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import {
|
|
7
|
+
deliveryId,
|
|
8
|
+
opaqueState,
|
|
9
|
+
pendingId,
|
|
10
|
+
pruneContent,
|
|
11
|
+
snapshotKey,
|
|
12
|
+
} from "./session-pruning-content.js";
|
|
13
|
+
import type { SessionPruningPolicy } from "./session-pruning-policy.js";
|
|
14
|
+
import { scanSession } from "./session-pruning-source.js";
|
|
15
|
+
import { projectTreeCheckpointBranch } from "./tree-checkpoint.js";
|
|
16
|
+
import { windowDetails } from "./windows.js";
|
|
17
|
+
|
|
18
|
+
interface RecordIndex {
|
|
19
|
+
id: string;
|
|
20
|
+
parent: string | null;
|
|
21
|
+
window: string | undefined;
|
|
22
|
+
state: string | undefined;
|
|
23
|
+
calls: string[];
|
|
24
|
+
result: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
export interface PruningPlan {
|
|
27
|
+
path: string;
|
|
28
|
+
sessionId: string;
|
|
29
|
+
policy: SessionPruningPolicy;
|
|
30
|
+
now: number;
|
|
31
|
+
source: Stats;
|
|
32
|
+
protectedIds: Set<string>;
|
|
33
|
+
fullIds: Set<string>;
|
|
34
|
+
stateIds: Set<string>;
|
|
35
|
+
delivered: Set<string>;
|
|
36
|
+
ids: Set<string>;
|
|
37
|
+
before: number;
|
|
38
|
+
after: number;
|
|
39
|
+
changed: number;
|
|
40
|
+
opaque: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** An active legacy checkpoint can live outside Pi's physical branch. */
|
|
44
|
+
export function pruningContext(manager: ExtensionContext["sessionManager"]) {
|
|
45
|
+
const branch = manager.getBranch();
|
|
46
|
+
const projected = projectTreeCheckpointBranch(branch, manager.getEntries());
|
|
47
|
+
const protectedIds = new Set(
|
|
48
|
+
manager.buildContextEntries().map((entry) => entry.id),
|
|
49
|
+
);
|
|
50
|
+
if (projected !== branch)
|
|
51
|
+
for (const entry of buildContextEntries([...projected]))
|
|
52
|
+
protectedIds.add(entry.id);
|
|
53
|
+
return { branch: projected, protectedIds };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function planSessionPruning(
|
|
57
|
+
manager: ExtensionContext["sessionManager"],
|
|
58
|
+
policy: SessionPruningPolicy,
|
|
59
|
+
now = Date.now(),
|
|
60
|
+
): Promise<PruningPlan> {
|
|
61
|
+
const path = manager.getSessionFile();
|
|
62
|
+
if (!path) throw new Error("Session has not been saved yet.");
|
|
63
|
+
const source = statSync(path);
|
|
64
|
+
const { branch, protectedIds } = pruningContext(manager);
|
|
65
|
+
const windows = [
|
|
66
|
+
...new Set(
|
|
67
|
+
branch.flatMap((entry) => {
|
|
68
|
+
const details = windowDetails(entry);
|
|
69
|
+
return details?.kind === "window"
|
|
70
|
+
? [details.identity.currentWindowId]
|
|
71
|
+
: [];
|
|
72
|
+
}),
|
|
73
|
+
),
|
|
74
|
+
];
|
|
75
|
+
const current = windows.at(-1);
|
|
76
|
+
const retained = new Set(
|
|
77
|
+
policy.windows ? windows.slice(-policy.windows) : [],
|
|
78
|
+
);
|
|
79
|
+
const index = new Map<string, RecordIndex>();
|
|
80
|
+
const delivered = new Set<string>();
|
|
81
|
+
const opaque = new Set<string>();
|
|
82
|
+
let header = false;
|
|
83
|
+
await scanSession(path, source.size, (entry) => {
|
|
84
|
+
if (entry.type === "session") {
|
|
85
|
+
if (header || entry.id !== manager.getSessionId() || entry.version !== 3)
|
|
86
|
+
throw new Error(
|
|
87
|
+
"Session identity or format changed. Resume it before pruning.",
|
|
88
|
+
);
|
|
89
|
+
header = true;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!header || index.has(entry.id))
|
|
93
|
+
throw new Error("Session has duplicate or misplaced records.");
|
|
94
|
+
const details = windowDetails(entry);
|
|
95
|
+
index.set(entry.id, {
|
|
96
|
+
id: entry.id,
|
|
97
|
+
parent: entry.parentId,
|
|
98
|
+
window:
|
|
99
|
+
details?.kind === "window"
|
|
100
|
+
? details.identity.currentWindowId
|
|
101
|
+
: undefined,
|
|
102
|
+
state: snapshotKey(entry),
|
|
103
|
+
calls:
|
|
104
|
+
entry.type === "message" && entry.message.role === "assistant"
|
|
105
|
+
? entry.message.content.flatMap((part) =>
|
|
106
|
+
part.type === "toolCall" ? [part.id] : [],
|
|
107
|
+
)
|
|
108
|
+
: [],
|
|
109
|
+
result:
|
|
110
|
+
entry.type === "message" && entry.message.role === "toolResult"
|
|
111
|
+
? entry.message.toolCallId
|
|
112
|
+
: undefined,
|
|
113
|
+
});
|
|
114
|
+
if (entry.type === "custom_message") {
|
|
115
|
+
const id = deliveryId(entry);
|
|
116
|
+
if (id) delivered.add(id);
|
|
117
|
+
if (
|
|
118
|
+
entry.details &&
|
|
119
|
+
typeof entry.details === "object" &&
|
|
120
|
+
"herdrAdmissionId" in entry.details &&
|
|
121
|
+
typeof entry.details.herdrAdmissionId === "string"
|
|
122
|
+
)
|
|
123
|
+
delivered.add(entry.details.herdrAdmissionId);
|
|
124
|
+
}
|
|
125
|
+
const unknown = opaqueState(entry);
|
|
126
|
+
if (unknown) opaque.add(unknown);
|
|
127
|
+
// Validate known pending state even when protected.
|
|
128
|
+
pendingId(entry);
|
|
129
|
+
});
|
|
130
|
+
const parents = new Set<string>();
|
|
131
|
+
for (const node of index.values()) {
|
|
132
|
+
if (node.parent !== null && !index.has(node.parent))
|
|
133
|
+
throw new Error("Session has a broken parent chain.");
|
|
134
|
+
if (node.parent) parents.add(node.parent);
|
|
135
|
+
}
|
|
136
|
+
const stateIds = new Set<string>();
|
|
137
|
+
for (const leaf of index.values()) {
|
|
138
|
+
if (parents.has(leaf.id)) continue;
|
|
139
|
+
const seen = new Set<string>();
|
|
140
|
+
const visited = new Set<string>();
|
|
141
|
+
let node: RecordIndex | undefined = leaf;
|
|
142
|
+
while (node) {
|
|
143
|
+
if (visited.has(node.id))
|
|
144
|
+
throw new Error("Session contains a parent cycle.");
|
|
145
|
+
visited.add(node.id);
|
|
146
|
+
if (node.state && !seen.has(node.state)) {
|
|
147
|
+
stateIds.add(node.id);
|
|
148
|
+
seen.add(node.state);
|
|
149
|
+
}
|
|
150
|
+
node = node.parent ? index.get(node.parent) : undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const fullIds = new Set<string>();
|
|
154
|
+
for (const node of index.values()) {
|
|
155
|
+
if (node.window === undefined) {
|
|
156
|
+
const path: RecordIndex[] = [];
|
|
157
|
+
const visited = new Set<string>();
|
|
158
|
+
let ancestor: RecordIndex | undefined = node;
|
|
159
|
+
while (ancestor && ancestor.window === undefined) {
|
|
160
|
+
if (visited.has(ancestor.id))
|
|
161
|
+
throw new Error("Session contains a parent cycle.");
|
|
162
|
+
visited.add(ancestor.id);
|
|
163
|
+
path.push(ancestor);
|
|
164
|
+
ancestor = ancestor.parent ? index.get(ancestor.parent) : undefined;
|
|
165
|
+
}
|
|
166
|
+
for (const item of path) item.window = ancestor?.window ?? "";
|
|
167
|
+
}
|
|
168
|
+
if (current && node.window === current) protectedIds.add(node.id);
|
|
169
|
+
if (
|
|
170
|
+
!policy.windows ||
|
|
171
|
+
(node.window && retained.has(node.window)) ||
|
|
172
|
+
protectedIds.has(node.id)
|
|
173
|
+
)
|
|
174
|
+
fullIds.add(node.id);
|
|
175
|
+
}
|
|
176
|
+
// A retained result/call keeps its counterpart, including pairs crossing a window cut.
|
|
177
|
+
const calls = new Map<string, string[]>();
|
|
178
|
+
const results = new Map<string, string[]>();
|
|
179
|
+
for (const node of index.values()) {
|
|
180
|
+
for (const call of node.calls)
|
|
181
|
+
calls.set(call, [...(calls.get(call) ?? []), node.id]);
|
|
182
|
+
if (node.result)
|
|
183
|
+
results.set(node.result, [...(results.get(node.result) ?? []), node.id]);
|
|
184
|
+
}
|
|
185
|
+
const queue = [...fullIds];
|
|
186
|
+
const keep = (id: string) => {
|
|
187
|
+
if (!fullIds.has(id)) {
|
|
188
|
+
fullIds.add(id);
|
|
189
|
+
queue.push(id);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
for (let offset = 0; offset < queue.length; offset++) {
|
|
193
|
+
const id = queue[offset];
|
|
194
|
+
const node = id === undefined ? undefined : index.get(id);
|
|
195
|
+
if (!node) continue;
|
|
196
|
+
if (node.result) {
|
|
197
|
+
for (const call of calls.get(node.result) ?? []) keep(call);
|
|
198
|
+
}
|
|
199
|
+
for (const call of node.calls)
|
|
200
|
+
for (const result of results.get(call) ?? []) keep(result);
|
|
201
|
+
}
|
|
202
|
+
const plan: PruningPlan = {
|
|
203
|
+
path,
|
|
204
|
+
sessionId: manager.getSessionId(),
|
|
205
|
+
policy,
|
|
206
|
+
now,
|
|
207
|
+
source,
|
|
208
|
+
protectedIds,
|
|
209
|
+
fullIds,
|
|
210
|
+
stateIds,
|
|
211
|
+
delivered,
|
|
212
|
+
ids: new Set(index.keys()),
|
|
213
|
+
before: source.size,
|
|
214
|
+
after: 0,
|
|
215
|
+
changed: 0,
|
|
216
|
+
opaque: [...opaque].sort(),
|
|
217
|
+
};
|
|
218
|
+
await scanSession(path, source.size, (entry, raw) => {
|
|
219
|
+
const rewritten =
|
|
220
|
+
entry.type === "session" ? entry : transformPruningEntry(plan, entry);
|
|
221
|
+
const text = rewritten === entry ? raw : JSON.stringify(rewritten);
|
|
222
|
+
plan.after += Buffer.byteLength(text) + 1;
|
|
223
|
+
if (text !== raw) plan.changed++;
|
|
224
|
+
});
|
|
225
|
+
return plan;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function transformPruningEntry(
|
|
229
|
+
plan: PruningPlan,
|
|
230
|
+
entry: Parameters<typeof pruneContent>[0],
|
|
231
|
+
) {
|
|
232
|
+
if (plan.protectedIds.has(entry.id)) return entry;
|
|
233
|
+
const expire =
|
|
234
|
+
plan.policy.days > 0 &&
|
|
235
|
+
Date.parse(entry.timestamp) < plan.now - plan.policy.days * 86_400_000;
|
|
236
|
+
return pruneContent(
|
|
237
|
+
entry,
|
|
238
|
+
plan.fullIds.has(entry.id),
|
|
239
|
+
expire,
|
|
240
|
+
plan.stateIds.has(entry.id),
|
|
241
|
+
plan.delivered,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const HISTORY_WINDOWS = [0, 5, 10, 25] as const;
|
|
2
|
+
export const CONVERSATION_DAYS = [0, 7, 30] as const;
|
|
3
|
+
export interface SessionPruningPolicy {
|
|
4
|
+
windows: (typeof HISTORY_WINDOWS)[number];
|
|
5
|
+
days: (typeof CONVERSATION_DAYS)[number];
|
|
6
|
+
}
|
|
7
|
+
export const PRUNING_OFF: SessionPruningPolicy = { windows: 0, days: 0 };
|
|
8
|
+
export const RESTART_NOTICE =
|
|
9
|
+
"Memory savings take effect after restarting Pi. /reload is not sufficient.";
|
|
10
|
+
export function pruningPolicy(value: unknown): SessionPruningPolicy {
|
|
11
|
+
if (!value || typeof value !== "object")
|
|
12
|
+
throw new Error("Invalid pruning policy");
|
|
13
|
+
const policy = value as SessionPruningPolicy;
|
|
14
|
+
if (
|
|
15
|
+
!HISTORY_WINDOWS.includes(policy.windows) ||
|
|
16
|
+
!CONVERSATION_DAYS.includes(policy.days)
|
|
17
|
+
)
|
|
18
|
+
throw new Error("Invalid pruning policy");
|
|
19
|
+
return { windows: policy.windows, days: policy.days };
|
|
20
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ExtensionContext,
|
|
3
|
+
getSelectListTheme,
|
|
4
|
+
getSettingsListTheme,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import {
|
|
7
|
+
SelectList,
|
|
8
|
+
SettingsList,
|
|
9
|
+
truncateToWidth,
|
|
10
|
+
wrapTextWithAnsi,
|
|
11
|
+
} from "@earendil-works/pi-tui";
|
|
12
|
+
import type { SessionPruningInteraction } from "./session-pruning.js";
|
|
13
|
+
import {
|
|
14
|
+
CONVERSATION_DAYS,
|
|
15
|
+
HISTORY_WINDOWS,
|
|
16
|
+
RESTART_NOTICE,
|
|
17
|
+
type SessionPruningPolicy,
|
|
18
|
+
} from "./session-pruning-policy.js";
|
|
19
|
+
|
|
20
|
+
export interface SessionPruningSettings {
|
|
21
|
+
get: () => SessionPruningPolicy;
|
|
22
|
+
save: (policy: SessionPruningPolicy) => void;
|
|
23
|
+
run: (interaction: SessionPruningInteraction) => Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
const windowLabel = (value: number) =>
|
|
26
|
+
value ? `Keep ${value} context windows` : "Off";
|
|
27
|
+
const ageLabel = (value: number) =>
|
|
28
|
+
value === 7 ? "1 week" : value === 30 ? "1 month" : "Off";
|
|
29
|
+
|
|
30
|
+
export async function openSessionPruningSettings(
|
|
31
|
+
ctx: ExtensionContext,
|
|
32
|
+
options: SessionPruningSettings,
|
|
33
|
+
): Promise<void> {
|
|
34
|
+
await ctx.ui.custom<void>((tui, theme, _kb, done) => {
|
|
35
|
+
type State =
|
|
36
|
+
| { kind: "idle" }
|
|
37
|
+
| { kind: "busy"; message: string }
|
|
38
|
+
| {
|
|
39
|
+
kind: "confirm";
|
|
40
|
+
title: string;
|
|
41
|
+
message: string;
|
|
42
|
+
choices: SelectList;
|
|
43
|
+
};
|
|
44
|
+
let state: State = { kind: "idle" };
|
|
45
|
+
const progress = (message: string) => {
|
|
46
|
+
state = { kind: "busy", message };
|
|
47
|
+
tui.requestRender();
|
|
48
|
+
};
|
|
49
|
+
const confirm: SessionPruningInteraction["confirm"] = (title, message) =>
|
|
50
|
+
new Promise((resolve) => {
|
|
51
|
+
const choices = new SelectList(
|
|
52
|
+
[
|
|
53
|
+
{ value: "no", label: "Cancel" },
|
|
54
|
+
{ value: "yes", label: "Prune permanently" },
|
|
55
|
+
],
|
|
56
|
+
2,
|
|
57
|
+
getSelectListTheme(),
|
|
58
|
+
);
|
|
59
|
+
const finish = (approved: boolean) => {
|
|
60
|
+
progress(
|
|
61
|
+
approved ? "Pruning session history…" : "Cancelling preview…",
|
|
62
|
+
);
|
|
63
|
+
resolve(approved);
|
|
64
|
+
};
|
|
65
|
+
choices.onSelect = (item) => finish(item.value === "yes");
|
|
66
|
+
choices.onCancel = () => finish(false);
|
|
67
|
+
state = { kind: "confirm", title, message, choices };
|
|
68
|
+
tui.requestRender();
|
|
69
|
+
});
|
|
70
|
+
const run = async () => {
|
|
71
|
+
progress("Waiting for the current turn to finish…");
|
|
72
|
+
try {
|
|
73
|
+
await options.run({ progress, confirm });
|
|
74
|
+
} catch (error) {
|
|
75
|
+
ctx.ui.notify(
|
|
76
|
+
`Session pruning did not complete: ${String(error)}`,
|
|
77
|
+
"error",
|
|
78
|
+
);
|
|
79
|
+
} finally {
|
|
80
|
+
state = { kind: "idle" };
|
|
81
|
+
list.updateValue("run", "select…");
|
|
82
|
+
tui.requestRender();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const policy = options.get();
|
|
86
|
+
const list = new SettingsList(
|
|
87
|
+
[
|
|
88
|
+
{
|
|
89
|
+
id: "windows",
|
|
90
|
+
label: "Full/tool history",
|
|
91
|
+
currentValue: windowLabel(policy.windows),
|
|
92
|
+
values: HISTORY_WINDOWS.map(windowLabel),
|
|
93
|
+
description:
|
|
94
|
+
"This session only. Outside retained windows, keep actual user messages (including attachments) and assistant prose, not tools or injected instructions. Off leaves full history intact.",
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
id: "days",
|
|
98
|
+
label: "Assistant & user messages",
|
|
99
|
+
currentValue: ageLabel(policy.days),
|
|
100
|
+
values: CONVERSATION_DAYS.map(ageLabel),
|
|
101
|
+
description:
|
|
102
|
+
"Expire conversation older than this limit, independently of full-history retention. The current context window and native active context are always protected.",
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
id: "run",
|
|
106
|
+
label: "Run now",
|
|
107
|
+
currentValue: "select…",
|
|
108
|
+
values: ["select…", "preview…"],
|
|
109
|
+
description:
|
|
110
|
+
"Preview and confirm irreversible pruning. Saved SQLite notes are never deleted. No archive or backup is kept.",
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
5,
|
|
114
|
+
getSettingsListTheme(),
|
|
115
|
+
(id, value) => {
|
|
116
|
+
if (id === "run") {
|
|
117
|
+
void run();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const current = options.get();
|
|
121
|
+
try {
|
|
122
|
+
if (id === "windows") {
|
|
123
|
+
const windows = HISTORY_WINDOWS.find(
|
|
124
|
+
(item) => windowLabel(item) === value,
|
|
125
|
+
);
|
|
126
|
+
if (windows !== undefined) options.save({ ...current, windows });
|
|
127
|
+
} else {
|
|
128
|
+
const days = CONVERSATION_DAYS.find(
|
|
129
|
+
(item) => ageLabel(item) === value,
|
|
130
|
+
);
|
|
131
|
+
if (days !== undefined) options.save({ ...current, days });
|
|
132
|
+
}
|
|
133
|
+
} catch (error) {
|
|
134
|
+
list.updateValue(
|
|
135
|
+
id,
|
|
136
|
+
id === "windows"
|
|
137
|
+
? windowLabel(current.windows)
|
|
138
|
+
: ageLabel(current.days),
|
|
139
|
+
);
|
|
140
|
+
ctx.ui.notify(
|
|
141
|
+
`Could not save session pruning settings: ${String(error)}`,
|
|
142
|
+
"error",
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
tui.requestRender();
|
|
146
|
+
},
|
|
147
|
+
() => done(),
|
|
148
|
+
{ enableSearch: false },
|
|
149
|
+
);
|
|
150
|
+
return {
|
|
151
|
+
render: (width) => {
|
|
152
|
+
const wrap = (text: string) =>
|
|
153
|
+
wrapTextWithAnsi(text, Math.max(1, width - 2)).map(
|
|
154
|
+
(line) => ` ${line}`,
|
|
155
|
+
);
|
|
156
|
+
const rule = theme.fg("accent", "─".repeat(Math.max(0, width)));
|
|
157
|
+
return [
|
|
158
|
+
rule,
|
|
159
|
+
theme.bold(" Session auto-pruning"),
|
|
160
|
+
"",
|
|
161
|
+
...wrap(
|
|
162
|
+
"Runs once after a new context window's first completed turn commits its checkpoint. Settings follow this session on resume; new and forked sessions start Off. Forks made before restarting can retain unpruned history.",
|
|
163
|
+
),
|
|
164
|
+
"",
|
|
165
|
+
...wrap(RESTART_NOTICE),
|
|
166
|
+
"",
|
|
167
|
+
...(state.kind === "confirm"
|
|
168
|
+
? [
|
|
169
|
+
...wrap(theme.bold(state.title)),
|
|
170
|
+
...wrap(state.message),
|
|
171
|
+
"",
|
|
172
|
+
...state.choices.render(width),
|
|
173
|
+
]
|
|
174
|
+
: list.render(width).slice(0, -2)),
|
|
175
|
+
"",
|
|
176
|
+
...wrap(
|
|
177
|
+
state.kind === "busy"
|
|
178
|
+
? state.message
|
|
179
|
+
: state.kind === "confirm"
|
|
180
|
+
? "↑/↓ select · Enter confirm · Esc cancel"
|
|
181
|
+
: "↑/↓ select · Enter/Space change · Esc back",
|
|
182
|
+
).map((line) =>
|
|
183
|
+
theme.fg(state.kind === "busy" ? "accent" : "dim", line),
|
|
184
|
+
),
|
|
185
|
+
...(state.kind === "busy"
|
|
186
|
+
? wrap("Please wait; settings and closing are paused.").map(
|
|
187
|
+
(line) => theme.fg("dim", line),
|
|
188
|
+
)
|
|
189
|
+
: []),
|
|
190
|
+
rule,
|
|
191
|
+
].map((line) => truncateToWidth(line, width, ""));
|
|
192
|
+
},
|
|
193
|
+
invalidate: () => {
|
|
194
|
+
list.invalidate();
|
|
195
|
+
if (state.kind === "confirm") state.choices.invalidate();
|
|
196
|
+
},
|
|
197
|
+
handleInput: (data) => {
|
|
198
|
+
if (state.kind === "idle") list.handleInput(data);
|
|
199
|
+
else if (state.kind === "confirm") state.choices.handleInput(data);
|
|
200
|
+
tui.requestRender();
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
}
|