@howaboua/pi-notes-compaction 0.0.10 → 0.0.12
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 +12 -0
- package/README.md +28 -0
- package/package.json +3 -3
- package/src/attachment-sharing.ts +134 -0
- package/src/context-owner.ts +15 -19
- package/src/extension-hooks.ts +2 -2
- package/src/fresh-notes.ts +5 -11
- package/src/history-search-pruning.mjs +2 -2
- package/src/history-search-worker.mjs +13 -11
- package/src/history-search.ts +1 -1
- package/src/history-windows.ts +223 -0
- package/src/history.ts +4 -222
- package/src/index.ts +27 -2
- package/src/integrations.ts +3 -1
- package/src/model-role-config.ts +99 -0
- package/src/model-role-editor.ts +40 -0
- package/src/model-role-screen.ts +58 -0
- package/src/model-roles.ts +363 -0
- package/src/note-query.ts +150 -0
- package/src/notes-lookup.ts +1 -1
- package/src/notes.ts +3 -145
- package/src/query-executor.ts +10 -2
- package/src/query-input.ts +115 -0
- package/src/saved-sharing-records.ts +103 -0
- package/src/session-pruning-screen.ts +168 -94
- package/src/session-pruning.ts +15 -2
- package/src/settings-command.ts +41 -10
- package/src/settings-controls.ts +209 -0
- package/src/settings-screen.ts +23 -211
- package/src/shared-notes.ts +20 -203
- package/src/shared-session-reader.ts +132 -0
- package/src/sharing-service.ts +2 -116
- package/src/sqlite.mjs +33 -0
- package/src/store.ts +15 -13
- package/src/tool-contracts.ts +121 -80
- package/src/tools.ts +46 -6
- package/src/window-message.ts +179 -0
- package/src/windows.ts +13 -172
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @howaboua/pi-notes-compaction
|
|
2
2
|
|
|
3
|
+
## 0.0.12
|
|
4
|
+
|
|
5
|
+
- Added `/roles` for model preferences across providers and optional model-switch recommendations that preserve the current conversation.
|
|
6
|
+
- Simplified Notes and History action names and Notes path and limit arguments. Existing calls remain supported.
|
|
7
|
+
- Fixed fresh child agents being unable to join shared Notes when another extension had saved startup metadata.
|
|
8
|
+
- Notes guidance now requires task checkpoints at meaningful milestones, completion and task switches, even before context runs low.
|
|
9
|
+
|
|
10
|
+
## 0.0.11
|
|
11
|
+
|
|
12
|
+
- - Fixed native SQLite addon crashes when saving notes or searching ranked history in standalone Bun-based Pi builds. Existing notes and history indexes remain compatible with Node-based Pi.
|
|
13
|
+
- Fixed the session auto-pruning submenu losing its frame and disappearing during pruning. Preview, confirmation and progress now stay inside the menu.
|
|
14
|
+
|
|
3
15
|
## 0.0.10
|
|
4
16
|
|
|
5
17
|
- ### Features
|
package/README.md
CHANGED
|
@@ -40,6 +40,34 @@ Idle rollover is off by default. Choose 25m, 55m, 2h, 6h or 12h in `/notes`. Hou
|
|
|
40
40
|
|
|
41
41
|
## Management
|
|
42
42
|
|
|
43
|
+
### Model roles
|
|
44
|
+
|
|
45
|
+
`/roles` opens the Roles tab. `/notes` opens its existing Notes tab. Press Tab
|
|
46
|
+
or Shift-Tab to move between them. Add a role with a description of when it
|
|
47
|
+
fits, choose any available Pi provider and model, and set preferred reasoning.
|
|
48
|
+
Roles have no shipped model assignments. You can switch to a role manually
|
|
49
|
+
without enabling recommendations.
|
|
50
|
+
|
|
51
|
+
Recommendations are off by default. Enable them in `/roles` to let the agent
|
|
52
|
+
discover your preferences through Notes and recommend a handoff when the
|
|
53
|
+
conversation gives a better-fitting role a clear advantage. Recommendations
|
|
54
|
+
appear after a successful run settles, with a reason and `/roles accept` as the
|
|
55
|
+
next action. Accept to switch in the same conversation, or use `/roles dismiss`
|
|
56
|
+
to stay put. There is no automatic hand-back or turn-count schedule.
|
|
57
|
+
|
|
58
|
+
Manual model or reasoning changes, a new run, and session or branch navigation
|
|
59
|
+
cancel pending recommendations. Notes must be active for both the current and
|
|
60
|
+
target models. Pi handles authentication and clamps unsupported reasoning
|
|
61
|
+
levels. Model switches retain Pi's existing context and tool results. Provider
|
|
62
|
+
capabilities and context limits still apply, and switching does not guarantee
|
|
63
|
+
cache reuse. Context retirement and recovery keep their existing Notes settings.
|
|
64
|
+
|
|
65
|
+
Role preferences are saved in
|
|
66
|
+
`<Pi agent directory>/notes-compaction/model-roles.json`. Changes take effect
|
|
67
|
+
through the Roles tab.
|
|
68
|
+
|
|
69
|
+
### Notes settings
|
|
70
|
+
|
|
43
71
|
`/notes` opens the settings panel in interactive Pi. It shows continuity status, storage counts and remote capabilities. Changes save immediately.
|
|
44
72
|
- **Active** selects **All models** (the default) or **Whitelist**. Whitelist enables the extension only for exact model IDs in the global settings file, regardless of provider. Switching models applies the choice immediately; other models keep their installed compaction behaviour.
|
|
45
73
|
- **Normal compaction** uses the installed Pi or PCC compaction flow for `/compact` and `new_context`. Off keeps notes-only rollover, the default.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howaboua/pi-notes-compaction",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"better-sqlite3": "13.0.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@howaboua/pi-codex-conversion": "4.0.
|
|
42
|
+
"@howaboua/pi-codex-conversion": "4.0.6",
|
|
43
43
|
"@types/better-sqlite3": "9.6.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@earendil-works/pi-agent-core": ">=1.0.4",
|
|
48
48
|
"@earendil-works/pi-ai": ">=1.0.4",
|
|
49
49
|
"@earendil-works/pi-tui": ">=1.0.4",
|
|
50
|
-
"@howaboua/pi-codex-conversion": ">=4.0.
|
|
50
|
+
"@howaboua/pi-codex-conversion": ">=4.0.6",
|
|
51
51
|
"typebox": "*"
|
|
52
52
|
},
|
|
53
53
|
"peerDependenciesMeta": {
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { contextTargetAgent } from "./agent-identity.js";
|
|
4
|
+
import {
|
|
5
|
+
readAttachmentSnapshot,
|
|
6
|
+
readRemoteNoteReference,
|
|
7
|
+
} from "./attachment-snapshot.js";
|
|
8
|
+
import type { ContextSharingService } from "./context-sharing.js";
|
|
9
|
+
import { exportNotesSnapshot } from "./notes.js";
|
|
10
|
+
|
|
11
|
+
export function createAttachmentSharing(
|
|
12
|
+
describe: ContextSharingService["describe"],
|
|
13
|
+
) {
|
|
14
|
+
return {
|
|
15
|
+
exportAttachmentNotes(ctx) {
|
|
16
|
+
const identity = describe(ctx);
|
|
17
|
+
if (!identity)
|
|
18
|
+
throw new Error("Checkpoint export requires notes-based continuity");
|
|
19
|
+
if (identity.storage === "remote") {
|
|
20
|
+
if (!identity.backendUrl)
|
|
21
|
+
throw new Error("Saved remote context has no backend address");
|
|
22
|
+
return {
|
|
23
|
+
protocol: 1,
|
|
24
|
+
storage: "remote",
|
|
25
|
+
timestamp: Date.now(),
|
|
26
|
+
identity,
|
|
27
|
+
baseUrl: identity.backendUrl,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const snapshot = exportNotesSnapshot(
|
|
32
|
+
ctx.sessionManager.getBranch(),
|
|
33
|
+
identity.agentName,
|
|
34
|
+
);
|
|
35
|
+
return {
|
|
36
|
+
...snapshot,
|
|
37
|
+
files: snapshot.files.filter(
|
|
38
|
+
(file) =>
|
|
39
|
+
contextTargetAgent(
|
|
40
|
+
"notes",
|
|
41
|
+
{ path: file.path },
|
|
42
|
+
identity.agentName,
|
|
43
|
+
) === identity.agentName,
|
|
44
|
+
),
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
validateAttachmentNotes(snapshot, identity) {
|
|
48
|
+
if (identity.storage === "remote") {
|
|
49
|
+
if (
|
|
50
|
+
!isDeepStrictEqual(
|
|
51
|
+
readRemoteNoteReference(snapshot).identity,
|
|
52
|
+
identity,
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
throw new Error("Remote checkpoint owner changed");
|
|
56
|
+
} else
|
|
57
|
+
readAttachmentSnapshot(snapshot, {
|
|
58
|
+
action: "list_files_by_prefix",
|
|
59
|
+
prefix: `${identity.agentName}/notes`,
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
async verifyAttachmentAccess(ctx, snapshot) {
|
|
63
|
+
if (
|
|
64
|
+
!snapshot ||
|
|
65
|
+
typeof snapshot !== "object" ||
|
|
66
|
+
!("storage" in snapshot) ||
|
|
67
|
+
snapshot.storage !== "remote"
|
|
68
|
+
)
|
|
69
|
+
return;
|
|
70
|
+
const { verifyRemoteReference } = await import("./remote.js");
|
|
71
|
+
await verifyRemoteReference(ctx, readRemoteNoteReference(snapshot));
|
|
72
|
+
},
|
|
73
|
+
async executeRemoteAttachment(ctx, snapshot, request, signal) {
|
|
74
|
+
if (request.namespace === "history")
|
|
75
|
+
throw new Error(
|
|
76
|
+
"History is unavailable from a saved remote notes attachment. Open its saved Pi JSONL session on this machine, then retry.",
|
|
77
|
+
);
|
|
78
|
+
const { executeRemoteAttachment } = await import("./remote.js");
|
|
79
|
+
return executeRemoteAttachment(
|
|
80
|
+
ctx,
|
|
81
|
+
readRemoteNoteReference(snapshot),
|
|
82
|
+
request,
|
|
83
|
+
signal,
|
|
84
|
+
);
|
|
85
|
+
},
|
|
86
|
+
parseAttachmentNotes(input, identity) {
|
|
87
|
+
if (!Array.isArray(input))
|
|
88
|
+
throw new Error("Invalid persisted checkpoint entries");
|
|
89
|
+
const entries: SessionEntry[] = input.map((entry: unknown) => {
|
|
90
|
+
if (
|
|
91
|
+
!entry ||
|
|
92
|
+
typeof entry !== "object" ||
|
|
93
|
+
!("type" in entry) ||
|
|
94
|
+
!(
|
|
95
|
+
(entry.type === "custom" &&
|
|
96
|
+
"customType" in entry &&
|
|
97
|
+
typeof entry.customType === "string" &&
|
|
98
|
+
"data" in entry) ||
|
|
99
|
+
(entry.type === "branch_summary" && "details" in entry)
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
throw new Error("Invalid persisted checkpoint entry");
|
|
103
|
+
return entry as SessionEntry;
|
|
104
|
+
});
|
|
105
|
+
const snapshot = exportNotesSnapshot(entries, identity.agentName);
|
|
106
|
+
return {
|
|
107
|
+
...snapshot,
|
|
108
|
+
files: snapshot.files.filter(
|
|
109
|
+
(file) =>
|
|
110
|
+
contextTargetAgent(
|
|
111
|
+
"notes",
|
|
112
|
+
{ path: file.path },
|
|
113
|
+
identity.agentName,
|
|
114
|
+
) === identity.agentName,
|
|
115
|
+
),
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
readAttachmentNotes(snapshot, params) {
|
|
119
|
+
const details = readAttachmentSnapshot(snapshot, params);
|
|
120
|
+
return {
|
|
121
|
+
content: [{ type: "text", text: JSON.stringify(details) }],
|
|
122
|
+
details: { codexHistoryNotes: details },
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
} satisfies Pick<
|
|
126
|
+
ContextSharingService,
|
|
127
|
+
| "exportAttachmentNotes"
|
|
128
|
+
| "validateAttachmentNotes"
|
|
129
|
+
| "verifyAttachmentAccess"
|
|
130
|
+
| "executeRemoteAttachment"
|
|
131
|
+
| "parseAttachmentNotes"
|
|
132
|
+
| "readAttachmentNotes"
|
|
133
|
+
>;
|
|
134
|
+
}
|
package/src/context-owner.ts
CHANGED
|
@@ -37,13 +37,6 @@ export function createNotesContextOwner(
|
|
|
37
37
|
lifecycle.active &&
|
|
38
38
|
ctx.isIdle() &&
|
|
39
39
|
entries.every((entry) => {
|
|
40
|
-
// The first startup after an update records a UI-only changelog.
|
|
41
|
-
if (
|
|
42
|
-
entry.type === "custom" &&
|
|
43
|
-
entry.customType === "@howaboua/pi-stuff/changelog"
|
|
44
|
-
)
|
|
45
|
-
return true;
|
|
46
|
-
// PCC links the fresh worker before binding its shared notes family.
|
|
47
40
|
if (
|
|
48
41
|
entry.type === "custom" &&
|
|
49
42
|
entry.customType === "codex-agent-relationship"
|
|
@@ -52,7 +45,6 @@ export function createNotesContextOwner(
|
|
|
52
45
|
(entry.data as { threadId?: unknown } | undefined)?.threadId ===
|
|
53
46
|
ctx.sessionManager.getSessionId()
|
|
54
47
|
);
|
|
55
|
-
// Herdr activation persists its empty registry before first task delivery.
|
|
56
48
|
if (
|
|
57
49
|
entry.type === "custom" &&
|
|
58
50
|
entry.customType === "herdr-agents-monitor-state"
|
|
@@ -66,17 +58,21 @@ export function createNotesContextOwner(
|
|
|
66
58
|
data.agents.length === 0,
|
|
67
59
|
);
|
|
68
60
|
}
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
);
|
|
80
76
|
if (
|
|
81
77
|
["model_change", "thinking_level_change", "session_info"].includes(
|
|
82
78
|
entry.type,
|
package/src/extension-hooks.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { NESTED_NOTE_OUTCOME, type NestedNoteOutcome } from "./fresh-notes.js";
|
|
|
5
5
|
import { registerNotesCompactionIntegration } from "./integrations.js";
|
|
6
6
|
import type { NotesLifecycle } from "./lifecycle.js";
|
|
7
7
|
import type { registerNoteSaveMarkers } from "./note-save-marker.js";
|
|
8
|
+
import { isNoteWrite } from "./query-input.js";
|
|
8
9
|
import {
|
|
9
10
|
projectTreeCheckpointBranch,
|
|
10
11
|
projectTreeCheckpointMessages,
|
|
@@ -61,8 +62,7 @@ export async function registerNotesHooks(
|
|
|
61
62
|
)
|
|
62
63
|
return;
|
|
63
64
|
const input = event.args as { action?: unknown } | undefined;
|
|
64
|
-
if (input
|
|
65
|
-
return;
|
|
65
|
+
if (!isNoteWrite(input)) return;
|
|
66
66
|
nestedWrites.set(event.toolCallId, {
|
|
67
67
|
runId: lifecycle.runId,
|
|
68
68
|
windowId: lifecycle.windows.current.currentWindowId,
|
package/src/fresh-notes.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type SessionEntry,
|
|
5
5
|
} from "@earendil-works/pi-coding-agent";
|
|
6
6
|
import { NOTE_RECEIPT, type NoteReceipt, noteReceipt } from "./notes.js";
|
|
7
|
+
import { isNoteWrite } from "./query-input.js";
|
|
7
8
|
|
|
8
9
|
export const NESTED_NOTE_OUTCOME = "notes-compaction:nested-write:v1";
|
|
9
10
|
export interface NestedNoteOutcome {
|
|
@@ -171,10 +172,7 @@ function completedWrites(
|
|
|
171
172
|
): boolean {
|
|
172
173
|
if (!receipts.length) return false;
|
|
173
174
|
const writes = calls.filter(
|
|
174
|
-
(call) =>
|
|
175
|
-
call.name === "notes" &&
|
|
176
|
-
(call.arguments["action"] === "write_file" ||
|
|
177
|
-
call.arguments["action"] === "append_to_file"),
|
|
175
|
+
(call) => call.name === "notes" && isNoteWrite(call.arguments),
|
|
178
176
|
);
|
|
179
177
|
let completedWrite = false;
|
|
180
178
|
for (const call of writes) {
|
|
@@ -274,17 +272,13 @@ function completedWrites(
|
|
|
274
272
|
uncovered.some(
|
|
275
273
|
(call) =>
|
|
276
274
|
call.status !== "ok" &&
|
|
277
|
-
(!call.arguments ||
|
|
278
|
-
call.arguments["action"] === "write_file" ||
|
|
279
|
-
call.arguments["action"] === "append_to_file"),
|
|
275
|
+
(!call.arguments || isNoteWrite(call.arguments)),
|
|
280
276
|
) ||
|
|
281
277
|
(uncovered.length && !nested?.complete)
|
|
282
278
|
)
|
|
283
279
|
return false;
|
|
284
|
-
const nestedWrites = uncovered.filter(
|
|
285
|
-
(call)
|
|
286
|
-
call.arguments?.["action"] === "write_file" ||
|
|
287
|
-
call.arguments?.["action"] === "append_to_file",
|
|
280
|
+
const nestedWrites = uncovered.filter((call) =>
|
|
281
|
+
isNoteWrite(call.arguments),
|
|
288
282
|
);
|
|
289
283
|
if (
|
|
290
284
|
nestedWrites.some((call) => call.status !== "ok") ||
|
|
@@ -9,7 +9,7 @@ export function createHistoryPruning() {
|
|
|
9
9
|
/** @type {PrunedHistory | undefined} */
|
|
10
10
|
let cached;
|
|
11
11
|
const empty = new Set();
|
|
12
|
-
/** @param {import("
|
|
12
|
+
/** @param {import("./sqlite.mjs").Database} db
|
|
13
13
|
* @param {string} sessionId
|
|
14
14
|
* @param {string | undefined} file
|
|
15
15
|
* @returns {Promise<ReadonlySet<string>>}
|
|
@@ -91,7 +91,7 @@ export function createHistoryPruning() {
|
|
|
91
91
|
const removeDocument = db.prepare(
|
|
92
92
|
"DELETE FROM history_documents WHERE id = ?",
|
|
93
93
|
);
|
|
94
|
-
/** @type {import("
|
|
94
|
+
/** @type {import("./sqlite.mjs").Statement<[string], {id:number; item_id:string}>} */
|
|
95
95
|
const documents = db.prepare(
|
|
96
96
|
"SELECT id, item_id FROM history_documents WHERE session_id = ?",
|
|
97
97
|
);
|
|
@@ -2,27 +2,29 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { existsSync, mkdirSync, statSync } from "node:fs";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
4
|
import { parentPort, workerData } from "node:worker_threads";
|
|
5
|
-
import Database from "better-sqlite3";
|
|
6
5
|
import { createHistoryPruning } from "./history-search-pruning.mjs";
|
|
6
|
+
import { openDatabase } from "./sqlite.mjs";
|
|
7
7
|
|
|
8
8
|
// Shipped as checked JavaScript: Node cannot strip TypeScript in node_modules.
|
|
9
9
|
// Pi's jiti resolver is not installed in worker threads.
|
|
10
10
|
/** @typedef {import("./history-search-protocol.js").SearchRequest} SearchRequest */
|
|
11
11
|
/** @typedef {import("./history-search-protocol.js").SearchResponse} SearchResponse */
|
|
12
12
|
/** @typedef {import("./history-search-protocol.js").PruneResult} PruneResult */
|
|
13
|
-
/** @type {import("
|
|
13
|
+
/** @type {import("./sqlite.mjs").Database | undefined} */
|
|
14
14
|
let database;
|
|
15
15
|
const pruneSession = createHistoryPruning();
|
|
16
|
-
/** @returns {import("
|
|
16
|
+
/** @returns {import("./sqlite.mjs").Database} */
|
|
17
17
|
function open() {
|
|
18
18
|
if (database) return database;
|
|
19
19
|
mkdirSync(dirname(workerData.path), { recursive: true, mode: 0o700 });
|
|
20
|
-
const db =
|
|
20
|
+
const db = openDatabase(workerData.path);
|
|
21
21
|
try {
|
|
22
|
-
db.
|
|
23
|
-
db.
|
|
22
|
+
db.exec("PRAGMA busy_timeout = 5000");
|
|
23
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
24
24
|
db.transaction(() => {
|
|
25
|
-
|
|
25
|
+
/** @type {import("./sqlite.mjs").Statement<[], {user_version:number}>} */
|
|
26
|
+
const versionQuery = db.prepare("PRAGMA user_version");
|
|
27
|
+
const version = versionQuery.get()?.user_version;
|
|
26
28
|
if (version !== 0 && version !== 1 && version !== 2 && version !== 3)
|
|
27
29
|
throw new Error("Unsupported history index version");
|
|
28
30
|
// Older formats indexed full transcripts without source-kind metadata.
|
|
@@ -66,7 +68,7 @@ async function index(request) {
|
|
|
66
68
|
return null;
|
|
67
69
|
const db = open();
|
|
68
70
|
const excluded = await pruneSession(db, request.sessionId, request.file);
|
|
69
|
-
/** @type {import("
|
|
71
|
+
/** @type {import("./sqlite.mjs").Statement<[string,string], {id:number; fingerprint:string}>} */
|
|
70
72
|
const find = db.prepare(
|
|
71
73
|
"SELECT id, fingerprint FROM history_documents WHERE session_id = ? AND item_id = ?",
|
|
72
74
|
);
|
|
@@ -127,7 +129,7 @@ function search(request) {
|
|
|
127
129
|
);
|
|
128
130
|
for (const [position, id] of request.ids.entries())
|
|
129
131
|
scope.run(position, request.sessionId, id);
|
|
130
|
-
/** @type {import("
|
|
132
|
+
/** @type {import("./sqlite.mjs").Statement<[string,number], {item_id:string}>} */
|
|
131
133
|
const select = db.prepare(`
|
|
132
134
|
SELECT history_documents.item_id FROM history_terms
|
|
133
135
|
JOIN search_scope ON search_scope.document_id = history_terms.rowid
|
|
@@ -164,7 +166,7 @@ function prune(activeSessionId, sources) {
|
|
|
164
166
|
);
|
|
165
167
|
for (const [id, file] of Object.entries(sources)) source.run(file, id);
|
|
166
168
|
}).immediate();
|
|
167
|
-
/** @type {import("
|
|
169
|
+
/** @type {import("./sqlite.mjs").Statement<[], {session_id:string;file_path:string|null}>} */
|
|
168
170
|
const owners = db.prepare(`
|
|
169
171
|
SELECT session_id, file_path FROM history_sessions
|
|
170
172
|
UNION SELECT DISTINCT session_id, NULL FROM history_documents
|
|
@@ -204,7 +206,7 @@ function prune(activeSessionId, sources) {
|
|
|
204
206
|
try {
|
|
205
207
|
db.exec("INSERT INTO history_terms(history_terms) VALUES ('optimize')");
|
|
206
208
|
db.exec("VACUUM");
|
|
207
|
-
/** @type {import("
|
|
209
|
+
/** @type {import("./sqlite.mjs").Statement<[], {busy:number}>} */
|
|
208
210
|
const checkpoint = db.prepare("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
209
211
|
result.storageBusy = checkpoint.all().some((row) => row.busy !== 0);
|
|
210
212
|
} catch (error) {
|
package/src/history-search.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
} from "./history-search-protocol.js";
|
|
9
9
|
|
|
10
10
|
const UNAVAILABLE =
|
|
11
|
-
"Ranked history search is unavailable.
|
|
11
|
+
"Ranked history search is unavailable. list and read still work. Ask the user to check local storage or turn off Ranked history search in /notes.";
|
|
12
12
|
|
|
13
13
|
/** Worker owns derived storage; JSONL and the caller own content and scope. */
|
|
14
14
|
export class HistorySearch {
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { setImmediate } from "node:timers/promises";
|
|
2
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { buildTreeArchiveIndex } from "./tree-archive.js";
|
|
4
|
+
import { windowDetails } from "./window-message.js";
|
|
5
|
+
|
|
6
|
+
interface HistoryItem {
|
|
7
|
+
window_id: string;
|
|
8
|
+
item_id: string;
|
|
9
|
+
role: string;
|
|
10
|
+
tool_name?: string;
|
|
11
|
+
tool_namespace?: string;
|
|
12
|
+
content: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Pi entries are immutable once persisted. Rendering is lazy so indexing never
|
|
16
|
+
// serializes the ongoing context, and repeated lookups reuse unchanged text.
|
|
17
|
+
const rendered = new WeakMap<SessionEntry, string>();
|
|
18
|
+
const cachedItems = new WeakMap<SessionEntry, HistoryItem>();
|
|
19
|
+
const indexItems = new WeakMap<
|
|
20
|
+
HistoryItem,
|
|
21
|
+
Pick<HistoryItem, "item_id" | "content">
|
|
22
|
+
>();
|
|
23
|
+
function content(entry: SessionEntry, render: () => string): string {
|
|
24
|
+
let value = rendered.get(entry);
|
|
25
|
+
if (value === undefined) {
|
|
26
|
+
value = render();
|
|
27
|
+
rendered.set(entry, value);
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function customHistoryRole(
|
|
33
|
+
entry: Extract<SessionEntry, { type: "custom_message" }>,
|
|
34
|
+
): string {
|
|
35
|
+
// Persisted developer metadata is a wire contract, not a package dependency.
|
|
36
|
+
const details = entry.details;
|
|
37
|
+
const metadata =
|
|
38
|
+
details &&
|
|
39
|
+
typeof details === "object" &&
|
|
40
|
+
"@howaboua/pi-codex-conversion/developer-message" in details
|
|
41
|
+
? details["@howaboua/pi-codex-conversion/developer-message"]
|
|
42
|
+
: [
|
|
43
|
+
"codex-developer-message",
|
|
44
|
+
"codex-context-window",
|
|
45
|
+
"codex-current-time-reminder",
|
|
46
|
+
"codex-toolkit-update",
|
|
47
|
+
"codex-notebook-status",
|
|
48
|
+
"codex-context-briefing",
|
|
49
|
+
].includes(entry.customType)
|
|
50
|
+
? details
|
|
51
|
+
: undefined;
|
|
52
|
+
return metadata &&
|
|
53
|
+
typeof metadata === "object" &&
|
|
54
|
+
"protocol" in metadata &&
|
|
55
|
+
metadata.protocol === 1 &&
|
|
56
|
+
"id" in metadata &&
|
|
57
|
+
typeof metadata.id === "string" &&
|
|
58
|
+
metadata.id.trim() !== ""
|
|
59
|
+
? "developer"
|
|
60
|
+
: "user";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Pi's selected JSONL branch is the archive. */
|
|
64
|
+
export async function historyWindows(
|
|
65
|
+
entries: readonly SessionEntry[],
|
|
66
|
+
sessionId: string,
|
|
67
|
+
allEntries: readonly SessionEntry[],
|
|
68
|
+
signal?: AbortSignal,
|
|
69
|
+
): Promise<{
|
|
70
|
+
windows: Map<string, HistoryItem[]>;
|
|
71
|
+
retired: Set<string>;
|
|
72
|
+
indexItems: Pick<typeof indexItems, "get" | "has">;
|
|
73
|
+
}> {
|
|
74
|
+
const archive = buildTreeArchiveIndex(allEntries, entries);
|
|
75
|
+
if (archive.invalidManifest)
|
|
76
|
+
throw new Error(
|
|
77
|
+
"Saved history could not be read. Open an intact copy of the source session.",
|
|
78
|
+
);
|
|
79
|
+
if (archive.archives.some((item) => !item.entries))
|
|
80
|
+
throw new Error(
|
|
81
|
+
"Archived history is unavailable in this session copy. Resume the source session.",
|
|
82
|
+
);
|
|
83
|
+
const seen = new Set<string>();
|
|
84
|
+
entries = [
|
|
85
|
+
...archive.archives.flatMap((item) => item.entries ?? []),
|
|
86
|
+
...entries,
|
|
87
|
+
].filter((entry) => {
|
|
88
|
+
if (seen.has(entry.id)) return false;
|
|
89
|
+
seen.add(entry.id);
|
|
90
|
+
return true;
|
|
91
|
+
});
|
|
92
|
+
let windowId = `session:${sessionId}`;
|
|
93
|
+
let activeStart = 0;
|
|
94
|
+
const positions = new Map(
|
|
95
|
+
entries.map((entry, position) => [entry.id, position]),
|
|
96
|
+
);
|
|
97
|
+
for (const [position, entry] of entries.entries()) {
|
|
98
|
+
const boundary = windowDetails(entry);
|
|
99
|
+
if (boundary?.kind === "window" && boundary.trim)
|
|
100
|
+
activeStart = position + 1;
|
|
101
|
+
if (entry.type === "compaction") {
|
|
102
|
+
const kept = positions.get(entry.firstKeptEntryId);
|
|
103
|
+
if (kept !== undefined && kept <= position)
|
|
104
|
+
activeStart = Math.max(activeStart, kept);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const retired = new Set(
|
|
108
|
+
entries.slice(0, activeStart).map((entry) => entry.id),
|
|
109
|
+
);
|
|
110
|
+
const windows = new Map<string, HistoryItem[]>();
|
|
111
|
+
windows.set(windowId, []);
|
|
112
|
+
for (const [position, entry] of entries.entries()) {
|
|
113
|
+
if (position % 512 === 0) {
|
|
114
|
+
await setImmediate();
|
|
115
|
+
signal?.throwIfAborted();
|
|
116
|
+
}
|
|
117
|
+
const boundary = windowDetails(entry);
|
|
118
|
+
if (boundary?.kind === "window") {
|
|
119
|
+
windowId = boundary.identity.currentWindowId;
|
|
120
|
+
if (!windows.has(windowId)) windows.set(windowId, []);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const cached = cachedItems.get(entry);
|
|
124
|
+
if (cached?.window_id === windowId) {
|
|
125
|
+
windows.get(windowId)!.push(cached);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
let item: HistoryItem | undefined;
|
|
129
|
+
if (entry.type === "message") {
|
|
130
|
+
const message = entry.message;
|
|
131
|
+
const call =
|
|
132
|
+
message.role === "assistant"
|
|
133
|
+
? message.content.find((block) => block.type === "toolCall")
|
|
134
|
+
: undefined;
|
|
135
|
+
const toolName =
|
|
136
|
+
message.role === "toolResult"
|
|
137
|
+
? message.toolName
|
|
138
|
+
: call?.type === "toolCall"
|
|
139
|
+
? call.name
|
|
140
|
+
: undefined;
|
|
141
|
+
const render = () =>
|
|
142
|
+
!("content" in message)
|
|
143
|
+
? JSON.stringify(message)
|
|
144
|
+
: typeof message.content === "string"
|
|
145
|
+
? message.content
|
|
146
|
+
: message.content
|
|
147
|
+
.map((block) => {
|
|
148
|
+
if (block.type === "text") return block.text;
|
|
149
|
+
if (block.type === "thinking") return block.thinking;
|
|
150
|
+
if (block.type === "toolCall")
|
|
151
|
+
return JSON.stringify({
|
|
152
|
+
tool: block.name,
|
|
153
|
+
arguments: block.arguments,
|
|
154
|
+
});
|
|
155
|
+
if (block.type === "image")
|
|
156
|
+
return `[image ${block.mimeType}]`;
|
|
157
|
+
return JSON.stringify(block);
|
|
158
|
+
})
|
|
159
|
+
.join("\n");
|
|
160
|
+
item = {
|
|
161
|
+
window_id: windowId,
|
|
162
|
+
item_id: entry.id,
|
|
163
|
+
role: message.role === "toolResult" ? "tool" : message.role,
|
|
164
|
+
...(toolName
|
|
165
|
+
? {
|
|
166
|
+
tool_name: toolName,
|
|
167
|
+
...(["notes", "history"].includes(toolName)
|
|
168
|
+
? { tool_namespace: toolName }
|
|
169
|
+
: {}),
|
|
170
|
+
}
|
|
171
|
+
: {}),
|
|
172
|
+
get content() {
|
|
173
|
+
return content(entry, render);
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
if (message.role === "user" || message.role === "assistant") {
|
|
177
|
+
const body = message.content;
|
|
178
|
+
const hasText =
|
|
179
|
+
typeof body === "string"
|
|
180
|
+
? body.length > 0
|
|
181
|
+
: body.some(
|
|
182
|
+
(block) => block.type === "text" && block.text.length > 0,
|
|
183
|
+
);
|
|
184
|
+
let indexedContent: string | undefined;
|
|
185
|
+
if (hasText)
|
|
186
|
+
indexItems.set(item, {
|
|
187
|
+
item_id: item.item_id,
|
|
188
|
+
get content() {
|
|
189
|
+
return (indexedContent ??=
|
|
190
|
+
typeof body === "string"
|
|
191
|
+
? body
|
|
192
|
+
: body
|
|
193
|
+
.filter((block) => block.type === "text")
|
|
194
|
+
.map((block) => block.text)
|
|
195
|
+
.join("\n"));
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
} else if (entry.type === "custom_message" && !boundary) {
|
|
200
|
+
item = {
|
|
201
|
+
window_id: windowId,
|
|
202
|
+
item_id: entry.id,
|
|
203
|
+
role: customHistoryRole(entry),
|
|
204
|
+
get content() {
|
|
205
|
+
return content(entry, () =>
|
|
206
|
+
typeof entry.content === "string"
|
|
207
|
+
? entry.content
|
|
208
|
+
: entry.content
|
|
209
|
+
.map((block) =>
|
|
210
|
+
block.type === "text" ? block.text : "[image]",
|
|
211
|
+
)
|
|
212
|
+
.join("\n"),
|
|
213
|
+
);
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (item) {
|
|
218
|
+
cachedItems.set(entry, item);
|
|
219
|
+
windows.get(windowId)!.push(item);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return { windows, retired, indexItems };
|
|
223
|
+
}
|