@howaboua/pi-notes-compaction 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -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 +256 -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 +130 -0
- package/src/session-pruning-source.ts +44 -0
- package/src/session-pruning-transaction.ts +141 -0
- package/src/session-pruning.ts +142 -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/store.ts +31 -0
- 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
package/src/store.ts
CHANGED
|
@@ -4,6 +4,11 @@ import { dirname } from "node:path";
|
|
|
4
4
|
import Database from "better-sqlite3";
|
|
5
5
|
import type { CachedResponse, LookupQuery } from "./bridge.js";
|
|
6
6
|
import { queryKey } from "./bridge.js";
|
|
7
|
+
import {
|
|
8
|
+
PRUNING_OFF,
|
|
9
|
+
pruningPolicy,
|
|
10
|
+
type SessionPruningPolicy,
|
|
11
|
+
} from "./session-pruning-policy.js";
|
|
7
12
|
|
|
8
13
|
export interface SessionRef {
|
|
9
14
|
id: string;
|
|
@@ -48,6 +53,7 @@ export class NotesStore {
|
|
|
48
53
|
}
|
|
49
54
|
db.exec(`
|
|
50
55
|
CREATE TABLE IF NOT EXISTS sessions (id TEXT PRIMARY KEY, file_path TEXT, updated_at INTEGER NOT NULL);
|
|
56
|
+
CREATE TABLE IF NOT EXISTS session_pruning (session_id TEXT PRIMARY KEY, windows INTEGER NOT NULL, days INTEGER NOT NULL);
|
|
51
57
|
CREATE TABLE IF NOT EXISTS notes (
|
|
52
58
|
id TEXT PRIMARY KEY, session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
|
|
53
59
|
path TEXT NOT NULL, text TEXT NOT NULL, mode TEXT NOT NULL,
|
|
@@ -76,6 +82,22 @@ export class NotesStore {
|
|
|
76
82
|
"INSERT INTO sessions VALUES (?, ?, ?) ON CONFLICT(id) DO UPDATE SET file_path = COALESCE(excluded.file_path, sessions.file_path), updated_at = excluded.updated_at",
|
|
77
83
|
).run(session.id, session.file ?? null, Date.now());
|
|
78
84
|
}
|
|
85
|
+
pruningPolicy(sessionId: string): SessionPruningPolicy {
|
|
86
|
+
const row = this.open()
|
|
87
|
+
?.prepare(
|
|
88
|
+
"SELECT windows, days FROM session_pruning WHERE session_id = ?",
|
|
89
|
+
)
|
|
90
|
+
.get(sessionId);
|
|
91
|
+
return row ? pruningPolicy(row) : { ...PRUNING_OFF };
|
|
92
|
+
}
|
|
93
|
+
setPruningPolicy(sessionId: string, value: SessionPruningPolicy): void {
|
|
94
|
+
const policy = pruningPolicy(value);
|
|
95
|
+
this.open(true)!
|
|
96
|
+
.prepare(
|
|
97
|
+
"INSERT INTO session_pruning VALUES (?, ?, ?) ON CONFLICT(session_id) DO UPDATE SET windows = excluded.windows, days = excluded.days",
|
|
98
|
+
)
|
|
99
|
+
.run(sessionId, policy.windows, policy.days);
|
|
100
|
+
}
|
|
79
101
|
/** Persist only after a real mutation. Snapshot revisions remain immutable for branch navigation. */
|
|
80
102
|
write(session: SessionRef, note: Omit<NoteRevision, "id">): NoteRevision {
|
|
81
103
|
const db = this.open(true)!;
|
|
@@ -203,6 +225,15 @@ export class NotesStore {
|
|
|
203
225
|
cachedResponses: count("remote_cache"),
|
|
204
226
|
};
|
|
205
227
|
}
|
|
228
|
+
sessionFiles(): Record<string, string | null> {
|
|
229
|
+
const rows =
|
|
230
|
+
this.open()
|
|
231
|
+
?.prepare<[], { id: string; file_path: string | null }>(
|
|
232
|
+
"SELECT id, file_path FROM sessions",
|
|
233
|
+
)
|
|
234
|
+
.all() ?? [];
|
|
235
|
+
return Object.fromEntries(rows.map((row) => [row.id, row.file_path]));
|
|
236
|
+
}
|
|
206
237
|
prune(activeSessionId: string): {
|
|
207
238
|
removed: number;
|
|
208
239
|
unknownPaths: number;
|
package/src/task-boundary.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
|
|
3
|
-
export function renderTaskBoundaryReminder(
|
|
3
|
+
export function renderTaskBoundaryReminder(
|
|
4
|
+
remainingPercent: number,
|
|
5
|
+
gpt = false,
|
|
6
|
+
): string {
|
|
7
|
+
if (gpt)
|
|
8
|
+
return `<context_window_reminder>\nSuggested budget reached; ${remainingPercent}% left. Task sequence→finish current task→save notes→new_context before next; otherwise continue here.\n</context_window_reminder>`;
|
|
4
9
|
return `<context_window_reminder>\nSuggested context budget reached; ${remainingPercent}% remains. For a sequence of tasks, finish the current task, save notes and call new_context before the next. Otherwise, continue here.\n</context_window_reminder>`;
|
|
5
10
|
}
|
|
6
11
|
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import { Check } from "typebox/value";
|
|
5
|
+
import type { LookupQuery } from "./bridge.js";
|
|
6
|
+
|
|
7
|
+
const NOTES_ACTIONS = [
|
|
8
|
+
"help",
|
|
9
|
+
"list_files_by_prefix",
|
|
10
|
+
"read_file",
|
|
11
|
+
"search_contents",
|
|
12
|
+
"append_to_file",
|
|
13
|
+
"write_file",
|
|
14
|
+
] as const;
|
|
15
|
+
const HISTORY_ACTIONS = [
|
|
16
|
+
"help",
|
|
17
|
+
"list_windows",
|
|
18
|
+
"list_items",
|
|
19
|
+
"read_item",
|
|
20
|
+
"search_contents",
|
|
21
|
+
] as const;
|
|
22
|
+
export const NOTES_USAGE = "await tools.notes()";
|
|
23
|
+
export const HISTORY_USAGE = "await tools.history()";
|
|
24
|
+
export const NOTES_DESCRIPTION =
|
|
25
|
+
"Persistent notes for handing off task state between context windows";
|
|
26
|
+
export const GPT_NOTES_DESCRIPTION =
|
|
27
|
+
"Persistent notes: task state handoff between context windows";
|
|
28
|
+
export const NEW_CONTEXT_DESCRIPTION =
|
|
29
|
+
"Start fresh from saved notes; recover earlier conversation through history";
|
|
30
|
+
export const GPT_NEW_CONTEXT_DESCRIPTION =
|
|
31
|
+
"Saved notes→fresh context; earlier conversation→history";
|
|
32
|
+
const STRING = Type.Optional(Type.String());
|
|
33
|
+
const INTEGER = Type.Optional(Type.Integer({ minimum: 1 }));
|
|
34
|
+
const NULLABLE_STRING = Type.Optional(Type.Union([Type.String(), Type.Null()]));
|
|
35
|
+
const LINE = Type.Optional(Type.Union([Type.Integer(), Type.Null()]));
|
|
36
|
+
export const NOTES_SCHEMA = Type.Object(
|
|
37
|
+
{
|
|
38
|
+
action: Type.Optional(StringEnum([...NOTES_ACTIONS, "read"])),
|
|
39
|
+
path: STRING,
|
|
40
|
+
text: STRING,
|
|
41
|
+
prefix: NULLABLE_STRING,
|
|
42
|
+
query: STRING,
|
|
43
|
+
path_prefix: NULLABLE_STRING,
|
|
44
|
+
start_line: LINE,
|
|
45
|
+
stop_line: LINE,
|
|
46
|
+
max_results: INTEGER,
|
|
47
|
+
max_files: INTEGER,
|
|
48
|
+
max_matches_per_file: INTEGER,
|
|
49
|
+
file_order: Type.Optional(StringEnum(["ascending", "descending"])),
|
|
50
|
+
file_order_by: Type.Optional(
|
|
51
|
+
StringEnum(["name", "created_at", "updated_at"]),
|
|
52
|
+
),
|
|
53
|
+
recent_file_first: Type.Optional(Type.Boolean()),
|
|
54
|
+
},
|
|
55
|
+
{ additionalProperties: false },
|
|
56
|
+
);
|
|
57
|
+
export const HISTORY_SCHEMA = Type.Object(
|
|
58
|
+
{
|
|
59
|
+
action: Type.Optional(StringEnum(HISTORY_ACTIONS)),
|
|
60
|
+
agent_name: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
61
|
+
item_id: STRING,
|
|
62
|
+
window_id: Type.Optional(
|
|
63
|
+
Type.Union([Type.String(), Type.Null()], {
|
|
64
|
+
description: "Required with item_id for read_item",
|
|
65
|
+
}),
|
|
66
|
+
),
|
|
67
|
+
query: STRING,
|
|
68
|
+
limit: INTEGER,
|
|
69
|
+
limit_chars: INTEGER,
|
|
70
|
+
max_chars_per_item: INTEGER,
|
|
71
|
+
offset_chars: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
72
|
+
recent_first: Type.Optional(Type.Boolean()),
|
|
73
|
+
role: Type.Optional(
|
|
74
|
+
Type.Union([
|
|
75
|
+
StringEnum(["user", "assistant", "tool", "system", "developer"]),
|
|
76
|
+
Type.Null(),
|
|
77
|
+
]),
|
|
78
|
+
),
|
|
79
|
+
tool_name: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
80
|
+
tool_namespace: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
81
|
+
},
|
|
82
|
+
{ additionalProperties: false },
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const FIELDS: Record<string, readonly string[]> = {
|
|
86
|
+
list_files_by_prefix: [
|
|
87
|
+
"prefix",
|
|
88
|
+
"file_order",
|
|
89
|
+
"file_order_by",
|
|
90
|
+
"max_results",
|
|
91
|
+
],
|
|
92
|
+
read_file: ["path", "start_line", "stop_line"],
|
|
93
|
+
search_notes: [
|
|
94
|
+
"query",
|
|
95
|
+
"path_prefix",
|
|
96
|
+
"max_files",
|
|
97
|
+
"max_matches_per_file",
|
|
98
|
+
"recent_file_first",
|
|
99
|
+
],
|
|
100
|
+
append_to_file: ["path", "text"],
|
|
101
|
+
write_file: ["path", "text"],
|
|
102
|
+
list_windows: ["agent_name", "limit", "recent_first"],
|
|
103
|
+
list_items: [
|
|
104
|
+
"agent_name",
|
|
105
|
+
"window_id",
|
|
106
|
+
"role",
|
|
107
|
+
"tool_name",
|
|
108
|
+
"tool_namespace",
|
|
109
|
+
"limit",
|
|
110
|
+
"max_chars_per_item",
|
|
111
|
+
"recent_first",
|
|
112
|
+
],
|
|
113
|
+
read_item: [
|
|
114
|
+
"agent_name",
|
|
115
|
+
"window_id",
|
|
116
|
+
"item_id",
|
|
117
|
+
"offset_chars",
|
|
118
|
+
"limit_chars",
|
|
119
|
+
],
|
|
120
|
+
search_history: [
|
|
121
|
+
"agent_name",
|
|
122
|
+
"window_id",
|
|
123
|
+
"role",
|
|
124
|
+
"tool_name",
|
|
125
|
+
"tool_namespace",
|
|
126
|
+
"query",
|
|
127
|
+
"limit",
|
|
128
|
+
"recent_first",
|
|
129
|
+
],
|
|
130
|
+
};
|
|
131
|
+
export function queryArguments(
|
|
132
|
+
namespace: LookupQuery["namespace"],
|
|
133
|
+
input: unknown,
|
|
134
|
+
): Record<string, unknown> {
|
|
135
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
136
|
+
throw new Error("Supply an action object");
|
|
137
|
+
const params = input as Record<string, unknown>;
|
|
138
|
+
const action = params["action"];
|
|
139
|
+
const key =
|
|
140
|
+
action === "search_contents" ? `search_${namespace}` : String(action);
|
|
141
|
+
const fields = FIELDS[key];
|
|
142
|
+
if (!fields)
|
|
143
|
+
throw new Error(
|
|
144
|
+
namespace === "notes"
|
|
145
|
+
? "notes requires a supported action"
|
|
146
|
+
: "Unknown action",
|
|
147
|
+
);
|
|
148
|
+
if (namespace === "notes") {
|
|
149
|
+
for (const field of Object.keys(params))
|
|
150
|
+
if (field !== "action" && !fields.includes(field))
|
|
151
|
+
throw new Error(`notes ${String(action)} does not accept ${field}`);
|
|
152
|
+
if (
|
|
153
|
+
["read_file", "write_file", "append_to_file"].includes(String(action)) &&
|
|
154
|
+
(typeof params["path"] !== "string" || !params["path"])
|
|
155
|
+
)
|
|
156
|
+
throw new Error(`notes ${String(action)} requires path`);
|
|
157
|
+
if (
|
|
158
|
+
action === "search_contents" &&
|
|
159
|
+
(typeof params["query"] !== "string" || !params["query"])
|
|
160
|
+
)
|
|
161
|
+
throw new Error("notes search_contents requires query");
|
|
162
|
+
if (
|
|
163
|
+
["write_file", "append_to_file"].includes(String(action)) &&
|
|
164
|
+
typeof params["text"] !== "string"
|
|
165
|
+
)
|
|
166
|
+
throw new Error(`notes ${String(action)} requires text`);
|
|
167
|
+
}
|
|
168
|
+
return Object.fromEntries(
|
|
169
|
+
Object.entries(params).filter(
|
|
170
|
+
([key]) => key === "action" || fields.includes(key),
|
|
171
|
+
),
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function result(payload: unknown): AgentToolResult<unknown> {
|
|
176
|
+
return {
|
|
177
|
+
content: [{ type: "text", text: JSON.stringify(payload) }],
|
|
178
|
+
details: {},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function queryHelp(
|
|
183
|
+
namespace: LookupQuery["namespace"],
|
|
184
|
+
input: unknown,
|
|
185
|
+
rankedHistory = false,
|
|
186
|
+
gpt = false,
|
|
187
|
+
): AgentToolResult<unknown> | undefined {
|
|
188
|
+
const params = input === undefined ? {} : input;
|
|
189
|
+
if (!params || typeof params !== "object" || Array.isArray(params))
|
|
190
|
+
throw new Error("Supply an action object, or omit arguments for help");
|
|
191
|
+
const entries = Object.entries(params);
|
|
192
|
+
if (
|
|
193
|
+
!entries.length ||
|
|
194
|
+
(params as Record<string, unknown>)["action"] === "help"
|
|
195
|
+
) {
|
|
196
|
+
if (entries.some(([key]) => key !== "action"))
|
|
197
|
+
throw new Error("Help accepts only action: help");
|
|
198
|
+
const actions = namespace === "notes" ? NOTES_ACTIONS : HISTORY_ACTIONS;
|
|
199
|
+
return result({
|
|
200
|
+
actions: Object.fromEntries(
|
|
201
|
+
actions.map((action) => [
|
|
202
|
+
action,
|
|
203
|
+
action === "help"
|
|
204
|
+
? ""
|
|
205
|
+
: FIELDS[
|
|
206
|
+
action === "search_contents" ? `search_${namespace}` : action
|
|
207
|
+
]!.map((field) =>
|
|
208
|
+
["path", "text", "query", "item_id"].includes(field) ||
|
|
209
|
+
(action === "read_item" && field === "window_id")
|
|
210
|
+
? field
|
|
211
|
+
: `${field}?`,
|
|
212
|
+
).join(" "),
|
|
213
|
+
]),
|
|
214
|
+
),
|
|
215
|
+
...(namespace === "notes"
|
|
216
|
+
? {
|
|
217
|
+
writes: gpt
|
|
218
|
+
? "write_file=replace; append_to_file=add"
|
|
219
|
+
: "write_file replaces; append_to_file adds",
|
|
220
|
+
paths: gpt
|
|
221
|
+
? "Relative=current agent; cross-agent=<agent>/notes[/path]"
|
|
222
|
+
: "Relative uses current agent; cross-agent uses <agent>/notes[/path]",
|
|
223
|
+
usage: '{ "action": "read_file", "path": "checkpoint" }',
|
|
224
|
+
ranges: gpt
|
|
225
|
+
? "start_line/stop_line=1-based inclusive; negative=from end; 0/null/omitted=first/last; out-of-range→clamp; reversed→empty"
|
|
226
|
+
: "start_line/stop_line inclusive, 1-based; negative counts from end; 0/null/omitted uses first/last line; out-of-range clamps; reversed range returns empty content",
|
|
227
|
+
defaults: gpt
|
|
228
|
+
? "list max_results=20; search max_files/max_matches_per_file=20; limits clamp 1..100; list/search name ascending; recent_file_first→created_at descending"
|
|
229
|
+
: "list max_results 20; search max_files/max_matches_per_file 20; limits clamp to 1..100; list name ascending; search name ascending, recent_file_first sorts created_at descending",
|
|
230
|
+
}
|
|
231
|
+
: {
|
|
232
|
+
usage: gpt
|
|
233
|
+
? "Earlierconversation→search≠browse; returned IDs unchanged"
|
|
234
|
+
: "Search earlier conversation, don't browse it. Pass returned IDs unchanged",
|
|
235
|
+
search: rankedHistory
|
|
236
|
+
? gpt
|
|
237
|
+
? "Own past user/assistant visible text: all keywords, case/accent-insensitive, BM25-ranked. Explicit live/tool/metadata/agent_name scopes→case-sensitive literal substring. list/read=full transcript; recent_first=true→newest first"
|
|
238
|
+
: "Own earlier conversation: user/assistant visible text, all keywords, case/accent-insensitive, relevance order. Explicit live/tool/metadata scopes or agent_name: case-sensitive literal substring. list/read retain the full transcript. recent_first=true uses newest first"
|
|
239
|
+
: gpt
|
|
240
|
+
? "Case-sensitive literal substring; recent_first=true→newest first"
|
|
241
|
+
: "Case-sensitive literal substring. recent_first=true uses newest first",
|
|
242
|
+
}),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function prepareNotesArguments(args: unknown) {
|
|
249
|
+
if (args === undefined) return {};
|
|
250
|
+
if (!args || typeof args !== "object" || Array.isArray(args))
|
|
251
|
+
throw new Error(
|
|
252
|
+
'Supply an action object, e.g. { "action": "read_file", "path": "checkpoint" }. Use {} for help',
|
|
253
|
+
);
|
|
254
|
+
const params = { ...args } as Record<string, unknown>;
|
|
255
|
+
if (params["action"] === "read") params["action"] = "read_file";
|
|
256
|
+
if (
|
|
257
|
+
(params["action"] === "write_file" ||
|
|
258
|
+
params["action"] === "append_to_file") &&
|
|
259
|
+
"content" in params
|
|
260
|
+
) {
|
|
261
|
+
if (!("text" in params)) params["text"] = params["content"];
|
|
262
|
+
delete params["content"];
|
|
263
|
+
}
|
|
264
|
+
if (
|
|
265
|
+
params["action"] === "list_files_by_prefix" &&
|
|
266
|
+
params["max_files"] !== undefined
|
|
267
|
+
) {
|
|
268
|
+
if (!("max_results" in params)) params["max_results"] = params["max_files"];
|
|
269
|
+
delete params["max_files"];
|
|
270
|
+
}
|
|
271
|
+
if (!queryHelp("notes", params)) queryArguments("notes", params);
|
|
272
|
+
for (const [field, value] of Object.entries(params)) {
|
|
273
|
+
const schema =
|
|
274
|
+
NOTES_SCHEMA.properties[field as keyof typeof NOTES_SCHEMA.properties];
|
|
275
|
+
if (!schema || !Check(schema, value))
|
|
276
|
+
throw new Error(`Invalid notes ${field}; use {} for exact arguments`);
|
|
277
|
+
}
|
|
278
|
+
return params;
|
|
279
|
+
}
|