@howaboua/pi-notes-compaction 0.0.10 → 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 +5 -0
- package/package.json +1 -1
- package/src/history-search-pruning.mjs +2 -2
- package/src/history-search-worker.mjs +13 -11
- package/src/session-pruning-screen.ts +168 -94
- package/src/session-pruning.ts +15 -2
- package/src/settings-command.ts +2 -2
- package/src/sqlite.mjs +33 -0
- package/src/store.ts +15 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# @howaboua/pi-notes-compaction
|
|
2
2
|
|
|
3
|
+
## 0.0.11
|
|
4
|
+
|
|
5
|
+
- - 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.
|
|
6
|
+
- Fixed the session auto-pruning submenu losing its frame and disappearing during pruning. Preview, confirmation and progress now stay inside the menu.
|
|
7
|
+
|
|
3
8
|
## 0.0.10
|
|
4
9
|
|
|
5
10
|
- ### Features
|
package/package.json
CHANGED
|
@@ -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) {
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type ExtensionContext,
|
|
3
|
+
getSelectListTheme,
|
|
3
4
|
getSettingsListTheme,
|
|
4
5
|
} from "@earendil-works/pi-coding-agent";
|
|
5
6
|
import {
|
|
7
|
+
SelectList,
|
|
6
8
|
SettingsList,
|
|
7
9
|
truncateToWidth,
|
|
8
10
|
wrapTextWithAnsi,
|
|
9
11
|
} from "@earendil-works/pi-tui";
|
|
12
|
+
import type { SessionPruningInteraction } from "./session-pruning.js";
|
|
10
13
|
import {
|
|
11
14
|
CONVERSATION_DAYS,
|
|
12
15
|
HISTORY_WINDOWS,
|
|
@@ -17,7 +20,7 @@ import {
|
|
|
17
20
|
export interface SessionPruningSettings {
|
|
18
21
|
get: () => SessionPruningPolicy;
|
|
19
22
|
save: (policy: SessionPruningPolicy) => void;
|
|
20
|
-
run: () => Promise<void>;
|
|
23
|
+
run: (interaction: SessionPruningInteraction) => Promise<void>;
|
|
21
24
|
}
|
|
22
25
|
const windowLabel = (value: number) =>
|
|
23
26
|
value ? `Keep ${value} context windows` : "Off";
|
|
@@ -28,103 +31,174 @@ export async function openSessionPruningSettings(
|
|
|
28
31
|
ctx: ExtensionContext,
|
|
29
32
|
options: SessionPruningSettings,
|
|
30
33
|
): Promise<void> {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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(
|
|
36
52
|
[
|
|
37
|
-
{
|
|
38
|
-
|
|
39
|
-
label: "Full/tool history",
|
|
40
|
-
currentValue: windowLabel(policy.windows),
|
|
41
|
-
values: HISTORY_WINDOWS.map(windowLabel),
|
|
42
|
-
description:
|
|
43
|
-
"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.",
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
id: "days",
|
|
47
|
-
label: "Assistant & user messages",
|
|
48
|
-
currentValue: ageLabel(policy.days),
|
|
49
|
-
values: CONVERSATION_DAYS.map(ageLabel),
|
|
50
|
-
description:
|
|
51
|
-
"Expire conversation older than this limit, independently of full-history retention. The current context window and native active context are always protected.",
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
id: "run",
|
|
55
|
-
label: "Run now",
|
|
56
|
-
currentValue: "select…",
|
|
57
|
-
values: ["select…", "preview…"],
|
|
58
|
-
description:
|
|
59
|
-
"Preview and confirm irreversible pruning. Saved SQLite notes are never deleted. No archive or backup is kept.",
|
|
60
|
-
},
|
|
53
|
+
{ value: "no", label: "Cancel" },
|
|
54
|
+
{ value: "yes", label: "Prune permanently" },
|
|
61
55
|
],
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
(id, value) => {
|
|
65
|
-
if (id === "run") {
|
|
66
|
-
done("run");
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const current = options.get();
|
|
70
|
-
try {
|
|
71
|
-
if (id === "windows") {
|
|
72
|
-
const windows = HISTORY_WINDOWS.find(
|
|
73
|
-
(item) => windowLabel(item) === value,
|
|
74
|
-
);
|
|
75
|
-
if (windows !== undefined)
|
|
76
|
-
options.save({ ...current, windows });
|
|
77
|
-
} else {
|
|
78
|
-
const days = CONVERSATION_DAYS.find(
|
|
79
|
-
(item) => ageLabel(item) === value,
|
|
80
|
-
);
|
|
81
|
-
if (days !== undefined) options.save({ ...current, days });
|
|
82
|
-
}
|
|
83
|
-
} catch (error) {
|
|
84
|
-
list.updateValue(
|
|
85
|
-
id,
|
|
86
|
-
id === "windows"
|
|
87
|
-
? windowLabel(current.windows)
|
|
88
|
-
: ageLabel(current.days),
|
|
89
|
-
);
|
|
90
|
-
ctx.ui.notify(
|
|
91
|
-
`Could not save session pruning settings: ${String(error)}`,
|
|
92
|
-
"error",
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
tui.requestRender();
|
|
96
|
-
},
|
|
97
|
-
() => done("close"),
|
|
98
|
-
{ enableSearch: false },
|
|
56
|
+
2,
|
|
57
|
+
getSelectListTheme(),
|
|
99
58
|
);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
);
|
|
106
|
-
return [
|
|
107
|
-
theme.bold(" Session auto-pruning"),
|
|
108
|
-
"",
|
|
109
|
-
...wrap(
|
|
110
|
-
"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.",
|
|
111
|
-
),
|
|
112
|
-
"",
|
|
113
|
-
...wrap(RESTART_NOTICE),
|
|
114
|
-
"",
|
|
115
|
-
...list.render(width),
|
|
116
|
-
...wrap("↑/↓ select · Enter/Space change · Esc back"),
|
|
117
|
-
].map((line) => truncateToWidth(line, width, ""));
|
|
118
|
-
},
|
|
119
|
-
invalidate: () => list.invalidate(),
|
|
120
|
-
handleInput: (data) => {
|
|
121
|
-
list.handleInput(data);
|
|
122
|
-
tui.requestRender();
|
|
123
|
-
},
|
|
59
|
+
const finish = (approved: boolean) => {
|
|
60
|
+
progress(
|
|
61
|
+
approved ? "Pruning session history…" : "Cancelling preview…",
|
|
62
|
+
);
|
|
63
|
+
resolve(approved);
|
|
124
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();
|
|
125
146
|
},
|
|
147
|
+
() => done(),
|
|
148
|
+
{ enableSearch: false },
|
|
126
149
|
);
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
+
});
|
|
130
204
|
}
|
package/src/session-pruning.ts
CHANGED
|
@@ -11,6 +11,11 @@ import { windowDetails } from "./windows.js";
|
|
|
11
11
|
|
|
12
12
|
const bytes = (value: number) => `${(value / 1024 / 1024).toFixed(1)} MiB`;
|
|
13
13
|
|
|
14
|
+
export interface SessionPruningInteraction {
|
|
15
|
+
progress: (message: string) => void;
|
|
16
|
+
confirm: (title: string, message: string) => Promise<boolean>;
|
|
17
|
+
}
|
|
18
|
+
|
|
14
19
|
export class SessionPruning {
|
|
15
20
|
private running = false;
|
|
16
21
|
private observed: string | undefined;
|
|
@@ -69,7 +74,11 @@ export class SessionPruning {
|
|
|
69
74
|
});
|
|
70
75
|
}
|
|
71
76
|
|
|
72
|
-
async run(
|
|
77
|
+
async run(
|
|
78
|
+
ctx: ExtensionContext,
|
|
79
|
+
manual: boolean,
|
|
80
|
+
interaction?: SessionPruningInteraction,
|
|
81
|
+
): Promise<void> {
|
|
73
82
|
if (this.running) {
|
|
74
83
|
if (manual) ctx.ui.notify("Session pruning is already running.", "info");
|
|
75
84
|
return;
|
|
@@ -89,8 +98,10 @@ export class SessionPruning {
|
|
|
89
98
|
);
|
|
90
99
|
return;
|
|
91
100
|
}
|
|
101
|
+
interaction?.progress("Preparing pruning preview…");
|
|
92
102
|
const plan = await planSessionPruning(ctx.sessionManager, policy);
|
|
93
103
|
if (!plan.changed) {
|
|
104
|
+
interaction?.progress("Cleaning ranked history…");
|
|
94
105
|
await this.search.pruneSession(plan.sessionId, plan.path);
|
|
95
106
|
if (manual)
|
|
96
107
|
ctx.ui.notify(
|
|
@@ -101,7 +112,7 @@ export class SessionPruning {
|
|
|
101
112
|
}
|
|
102
113
|
if (
|
|
103
114
|
manual &&
|
|
104
|
-
!(await ctx.ui.confirm(
|
|
115
|
+
!(await (interaction?.confirm ?? ctx.ui.confirm)(
|
|
105
116
|
"Prune this session's history?",
|
|
106
117
|
`${bytes(plan.before)} → about ${bytes(plan.after)}; ${plan.changed} records changed. Current context and saved SQLite notes are protected. Older tool activity, injected instructions and exact execution replay are permanently lost. No backup is kept.${plan.opaque.length ? ` Opaque extension state is retained: ${plan.opaque.join(", ")}.` : ""} ${RESTART_NOTICE}`,
|
|
107
118
|
))
|
|
@@ -113,6 +124,7 @@ export class SessionPruning {
|
|
|
113
124
|
currentPolicy.days !== policy.days
|
|
114
125
|
)
|
|
115
126
|
throw new Error("Session pruning settings changed. Run now again.");
|
|
127
|
+
interaction?.progress("Pruning session history…");
|
|
116
128
|
const result = await commitSessionPruning(this.pi, ctx, plan, () => {
|
|
117
129
|
if (this.epoch !== epoch)
|
|
118
130
|
throw new Error(
|
|
@@ -123,6 +135,7 @@ export class SessionPruning {
|
|
|
123
135
|
throw new Error("Session pruning settings changed. Run now again.");
|
|
124
136
|
});
|
|
125
137
|
historyPruned = true;
|
|
138
|
+
interaction?.progress("Cleaning ranked history…");
|
|
126
139
|
await this.search.pruneSession(plan.sessionId, plan.path);
|
|
127
140
|
ctx.ui.notify(
|
|
128
141
|
`Pruned session history: ${bytes(result.before)} → ${bytes(result.after)}. Saved notes unchanged. ${RESTART_NOTICE}`,
|
package/src/settings-command.ts
CHANGED
|
@@ -53,9 +53,9 @@ export function registerNotesSettingsCommand(
|
|
|
53
53
|
get: () => store.pruningPolicy(ctx.sessionManager.getSessionId()),
|
|
54
54
|
save: (value) =>
|
|
55
55
|
store.setPruningPolicy(ctx.sessionManager.getSessionId(), value),
|
|
56
|
-
run: async () => {
|
|
56
|
+
run: async (interaction) => {
|
|
57
57
|
await ctx.waitForIdle();
|
|
58
|
-
await sessionPruning.run(ctx, true);
|
|
58
|
+
await sessionPruning.run(ctx, true, interaction);
|
|
59
59
|
},
|
|
60
60
|
},
|
|
61
61
|
status: () => {
|
package/src/sqlite.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
// Only the shared SQLite surface used by Notes. Bun returns null for a missing
|
|
4
|
+
// row (better-sqlite3 returns undefined); blobs may be Uint8Array, not Buffer.
|
|
5
|
+
/** @template {unknown[]} P
|
|
6
|
+
* @template R
|
|
7
|
+
* @typedef {{get(...params:P): R | null | undefined; all(...params:P): R[]; run(...params:P): {changes:number; lastInsertRowid:number|bigint}}} Statement
|
|
8
|
+
*/
|
|
9
|
+
/** @template {unknown[]} P
|
|
10
|
+
* @template R
|
|
11
|
+
* @typedef {((...params:P) => R) & {immediate(...params:P):R}} Transaction
|
|
12
|
+
*/
|
|
13
|
+
/** @typedef {{
|
|
14
|
+
* prepare<P extends unknown[] = unknown[], R = unknown>(sql:string): Statement<P,R>;
|
|
15
|
+
* transaction<P extends unknown[], R>(fn:(...params:P) => R): Transaction<P,R>;
|
|
16
|
+
* exec(sql:string):void;
|
|
17
|
+
* close():void;
|
|
18
|
+
* }} Database
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const require = createRequire(import.meta.url);
|
|
22
|
+
|
|
23
|
+
/** Avoid loading the native addon at all in Bun, including compiled Pi builds.
|
|
24
|
+
* @param {string} path
|
|
25
|
+
* @returns {Database}
|
|
26
|
+
*/
|
|
27
|
+
export function openDatabase(path) {
|
|
28
|
+
/** @type {new(path:string) => Database} */
|
|
29
|
+
const Database = process.versions["bun"]
|
|
30
|
+
? require("bun:sqlite").Database
|
|
31
|
+
: require("better-sqlite3");
|
|
32
|
+
return new Database(path);
|
|
33
|
+
}
|
package/src/store.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { existsSync, mkdirSync, statSync } from "node:fs";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
|
-
import Database from "better-sqlite3";
|
|
5
4
|
import type { CachedResponse, LookupQuery } from "./bridge.js";
|
|
6
5
|
import { queryKey } from "./bridge.js";
|
|
7
6
|
import {
|
|
@@ -9,6 +8,7 @@ import {
|
|
|
9
8
|
pruningPolicy,
|
|
10
9
|
type SessionPruningPolicy,
|
|
11
10
|
} from "./session-pruning-policy.js";
|
|
11
|
+
import { type Database, openDatabase } from "./sqlite.mjs";
|
|
12
12
|
|
|
13
13
|
export interface SessionRef {
|
|
14
14
|
id: string;
|
|
@@ -24,7 +24,7 @@ export interface NoteRevision {
|
|
|
24
24
|
}
|
|
25
25
|
interface CacheRow {
|
|
26
26
|
query_json: string;
|
|
27
|
-
bytes:
|
|
27
|
+
bytes: Uint8Array;
|
|
28
28
|
source: string;
|
|
29
29
|
encoding: string;
|
|
30
30
|
fetched_at: number;
|
|
@@ -33,21 +33,23 @@ interface CacheRow {
|
|
|
33
33
|
|
|
34
34
|
/** One connection per extension process. Read-only access never creates a database or session. */
|
|
35
35
|
export class NotesStore {
|
|
36
|
-
private db: Database
|
|
36
|
+
private db: Database | undefined;
|
|
37
37
|
readonly path: string;
|
|
38
38
|
constructor(path: string) {
|
|
39
39
|
this.path = path;
|
|
40
40
|
}
|
|
41
|
-
private open(write = false): Database
|
|
41
|
+
private open(write = false): Database | undefined {
|
|
42
42
|
if (this.db) return this.db;
|
|
43
43
|
if (!write && !existsSync(this.path)) return undefined;
|
|
44
44
|
if (write) mkdirSync(dirname(this.path), { recursive: true, mode: 0o700 });
|
|
45
|
-
const db =
|
|
45
|
+
const db = openDatabase(this.path);
|
|
46
46
|
try {
|
|
47
|
-
db.
|
|
48
|
-
db.
|
|
49
|
-
db.
|
|
50
|
-
const version = db
|
|
47
|
+
db.exec("PRAGMA busy_timeout = 5000");
|
|
48
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
49
|
+
db.exec("PRAGMA foreign_keys = ON");
|
|
50
|
+
const version = db
|
|
51
|
+
.prepare<[], { user_version: number }>("PRAGMA user_version")
|
|
52
|
+
.get()?.user_version;
|
|
51
53
|
if (version !== 0 && version !== 1) {
|
|
52
54
|
throw new Error("Notes database version is not supported");
|
|
53
55
|
}
|
|
@@ -77,7 +79,7 @@ export class NotesStore {
|
|
|
77
79
|
);
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
|
-
private record(db: Database
|
|
82
|
+
private record(db: Database, session: SessionRef): void {
|
|
81
83
|
db.prepare(
|
|
82
84
|
"INSERT INTO sessions VALUES (?, ?, ?) ON CONFLICT(id) DO UPDATE SET file_path = COALESCE(excluded.file_path, sessions.file_path), updated_at = excluded.updated_at",
|
|
83
85
|
).run(session.id, session.file ?? null, Date.now());
|
|
@@ -118,11 +120,11 @@ export class NotesStore {
|
|
|
118
120
|
}
|
|
119
121
|
read(id: string): NoteRevision | undefined {
|
|
120
122
|
const row = this.open()
|
|
121
|
-
?.prepare(
|
|
123
|
+
?.prepare<[string], NoteRevision>(
|
|
122
124
|
"SELECT id, path, text, mode, created_at AS createdAt, updated_at AS updatedAt FROM notes WHERE id = ?",
|
|
123
125
|
)
|
|
124
|
-
.get(id)
|
|
125
|
-
return row;
|
|
126
|
+
.get(id);
|
|
127
|
+
return row ?? undefined;
|
|
126
128
|
}
|
|
127
129
|
updateFile(session: SessionRef): void {
|
|
128
130
|
if (!session.file) return;
|