@robhowley/pi-remembrancer 0.2.0 → 0.4.0
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/README.md +36 -24
- package/extensions/remembrancer/checkpoints.ts +138 -26
- package/extensions/remembrancer/discovery.ts +270 -0
- package/extensions/remembrancer/evidence.ts +499 -62
- package/extensions/remembrancer/index.ts +625 -50
- package/extensions/remembrancer/render.ts +434 -0
- package/extensions/remembrancer/warnings.ts +29 -0
- package/package.json +7 -4
- package/skills/remembrancer/SKILL.md +35 -11
|
@@ -4,36 +4,394 @@ import { StringEnum, Type } from '@earendil-works/pi-ai';
|
|
|
4
4
|
import type { ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-agent';
|
|
5
5
|
import { CheckpointStore } from './checkpoints.js';
|
|
6
6
|
import { loadRemembrancerConfig } from './config.js';
|
|
7
|
-
import {
|
|
7
|
+
import { discoverSources } from './discovery.js';
|
|
8
|
+
import {
|
|
9
|
+
renderReviewCall,
|
|
10
|
+
renderReviewResult,
|
|
11
|
+
type ReviewResultDetails,
|
|
12
|
+
type ReviewAction,
|
|
13
|
+
} from './render.js';
|
|
14
|
+
import { copyWarnings, warningMessages, type WarningRecord } from './warnings.js';
|
|
15
|
+
import {
|
|
16
|
+
branchEvidence,
|
|
17
|
+
prepareReview,
|
|
18
|
+
resolveSeedSince,
|
|
19
|
+
ReviewBudget,
|
|
20
|
+
type Checkpoint,
|
|
21
|
+
type Evidence,
|
|
22
|
+
type SeedDefault,
|
|
23
|
+
type SessionSnapshot,
|
|
24
|
+
type Source,
|
|
25
|
+
type Tip,
|
|
26
|
+
} from './evidence.js';
|
|
27
|
+
|
|
28
|
+
type Selected = {
|
|
29
|
+
sessionId: string;
|
|
30
|
+
branchId: string;
|
|
31
|
+
entryId: string;
|
|
32
|
+
editId: string | null;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type ReviewArgs = {
|
|
36
|
+
action: 'begin' | 'next' | 'finish' | 'cancel' | 'context';
|
|
37
|
+
batch?: string;
|
|
38
|
+
seed?: string;
|
|
39
|
+
selected?: Selected;
|
|
40
|
+
before?: number;
|
|
41
|
+
offset?: number;
|
|
42
|
+
};
|
|
8
43
|
|
|
9
44
|
type Batch = {
|
|
10
45
|
token: string;
|
|
11
46
|
tips: Tip[];
|
|
47
|
+
checkpoints: Checkpoint[];
|
|
48
|
+
sourceDefaults: SeedDefault[];
|
|
49
|
+
stagedSources: Source[];
|
|
50
|
+
snapshots: SessionSnapshot[];
|
|
12
51
|
evidence: Evidence[];
|
|
52
|
+
warnings: WarningRecord[];
|
|
53
|
+
incomplete: boolean;
|
|
54
|
+
coverageChanged: boolean;
|
|
55
|
+
seed?: string;
|
|
56
|
+
requestedSince: string | null;
|
|
13
57
|
index: number;
|
|
14
58
|
offset: number;
|
|
15
59
|
};
|
|
16
60
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
61
|
+
export const REMEMBRANCER_COMMAND_NAME = 'remembrancer';
|
|
62
|
+
export const REMEMBRANCER_COMMAND_USAGE =
|
|
63
|
+
'Usage: /remembrancer seed <Nd>\n /remembrancer help';
|
|
64
|
+
export const REMEMBRANCER_HELP = [
|
|
65
|
+
REMEMBRANCER_COMMAND_USAGE,
|
|
66
|
+
'Seed saved-session evidence from a bounded historical window, then follow the Remembrancer skill.',
|
|
67
|
+
'Natural-language review requests remain supported.',
|
|
68
|
+
].join('\n');
|
|
69
|
+
|
|
70
|
+
const REMEMBRANCER_SUBCOMMANDS = [
|
|
71
|
+
{
|
|
72
|
+
value: 'seed ',
|
|
73
|
+
label: 'seed',
|
|
74
|
+
description: 'Review saved-session evidence from a bounded historical window',
|
|
75
|
+
},
|
|
76
|
+
{ value: 'help', label: 'help', description: 'Show Remembrancer command usage' },
|
|
77
|
+
];
|
|
78
|
+
const REMEMBRANCER_SEED_DURATIONS = [
|
|
79
|
+
{ value: '1d', label: '1d', description: 'Last day' },
|
|
80
|
+
{ value: '7d', label: '7d', description: 'Last 7 days' },
|
|
81
|
+
{ value: '14d', label: '14d', description: 'Last 14 days' },
|
|
82
|
+
{ value: '30d', label: '30d', description: 'Last 30 days' },
|
|
83
|
+
{ value: '90d', label: '90d', description: 'Last 90 days' },
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
type RemembrancerCommandArgs =
|
|
87
|
+
| { ok: true; mode: 'help' }
|
|
88
|
+
| { ok: true; mode: 'seed'; duration: string }
|
|
89
|
+
| { ok: false; message: string };
|
|
90
|
+
|
|
91
|
+
export function parseRemembrancerCommandArgs(args: string): RemembrancerCommandArgs {
|
|
92
|
+
const tokens = args.trim().length === 0 ? [] : args.trim().split(/\s+/u);
|
|
93
|
+
const subcommand = tokens[0];
|
|
94
|
+
|
|
95
|
+
if (tokens.length === 0 || (subcommand === 'help' && tokens.length === 1))
|
|
96
|
+
return { ok: true, mode: 'help' };
|
|
97
|
+
if (subcommand === 'help')
|
|
98
|
+
return { ok: false, message: `Help does not accept arguments. ${REMEMBRANCER_COMMAND_USAGE}` };
|
|
99
|
+
if (subcommand !== 'seed')
|
|
100
|
+
return {
|
|
101
|
+
ok: false,
|
|
102
|
+
message: `Unknown subcommand: ${subcommand}. ${REMEMBRANCER_COMMAND_USAGE}`,
|
|
103
|
+
};
|
|
104
|
+
if (tokens.length !== 2)
|
|
105
|
+
return {
|
|
106
|
+
ok: false,
|
|
107
|
+
message: `Seed requires exactly one duration such as 14d. ${REMEMBRANCER_COMMAND_USAGE}`,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const duration = tokens[1]!;
|
|
111
|
+
try {
|
|
112
|
+
resolveSeedSince(duration);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
115
|
+
return {
|
|
116
|
+
ok: false,
|
|
117
|
+
message: `Invalid seed duration "${duration}": ${detail}. ${REMEMBRANCER_COMMAND_USAGE}`,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return { ok: true, mode: 'seed', duration };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function getRemembrancerArgumentCompletions(prefix: string) {
|
|
124
|
+
const input = prefix.trimStart();
|
|
125
|
+
const trailingWhitespace = /\s$/u.test(input);
|
|
126
|
+
const tokens = input.trim().length === 0 ? [] : input.trim().split(/\s+/u);
|
|
127
|
+
|
|
128
|
+
if (tokens.length === 0) return REMEMBRANCER_SUBCOMMANDS;
|
|
129
|
+
if (tokens.length === 1) {
|
|
130
|
+
const token = tokens[0]!;
|
|
131
|
+
if (trailingWhitespace) {
|
|
132
|
+
return token === 'seed'
|
|
133
|
+
? REMEMBRANCER_SEED_DURATIONS.map((item) => ({ ...item, value: `seed ${item.value}` }))
|
|
134
|
+
: null;
|
|
34
135
|
}
|
|
136
|
+
const matches = REMEMBRANCER_SUBCOMMANDS.filter((item) => item.value.startsWith(token));
|
|
137
|
+
return matches.length > 0 ? matches : null;
|
|
35
138
|
}
|
|
36
|
-
|
|
139
|
+
if (tokens[0] !== 'seed' || tokens.length !== 2 || trailingWhitespace) return null;
|
|
140
|
+
|
|
141
|
+
const token = tokens[1]!;
|
|
142
|
+
const matches = REMEMBRANCER_SEED_DURATIONS.filter((item) => item.value.startsWith(token));
|
|
143
|
+
return matches.length > 0
|
|
144
|
+
? matches.map((item) => ({ ...item, value: `seed ${item.value}` }))
|
|
145
|
+
: null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const selectedParameters = Type.Object({
|
|
149
|
+
sessionId: Type.String(),
|
|
150
|
+
branchId: Type.String(),
|
|
151
|
+
entryId: Type.String(),
|
|
152
|
+
editId: Type.Union([Type.String(), Type.Null()]),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
export const REVIEW_RESPONSE_MAX_BYTES = 50 * 1024;
|
|
156
|
+
const PAGE_MAX_TEXT_UNITS = 48_000;
|
|
157
|
+
const PAGE_MAX_ENTRIES = 200;
|
|
158
|
+
|
|
159
|
+
export type EvidencePageEntry = Evidence & { offset: number; continues: boolean };
|
|
160
|
+
type EvidencePageItem = Pick<Evidence, 'entryId' | 'role' | 'text' | 'timestamp' | 'editId'> & {
|
|
161
|
+
offset: number;
|
|
162
|
+
continues: boolean;
|
|
163
|
+
};
|
|
164
|
+
type EvidenceGroup = Pick<
|
|
165
|
+
Evidence,
|
|
166
|
+
'file' | 'sessionId' | 'cwd' | 'branchId' | 'reviewedThrough'
|
|
167
|
+
> & {
|
|
168
|
+
entries: EvidencePageItem[];
|
|
169
|
+
};
|
|
170
|
+
export type PageCursor = { index: number; offset: number };
|
|
171
|
+
|
|
172
|
+
function serializedResponse(data: object): string {
|
|
173
|
+
const text = JSON.stringify(data);
|
|
174
|
+
if (text === undefined) throw new Error('Remembrancer response is not JSON serializable.');
|
|
175
|
+
const bytes = Buffer.byteLength(text, 'utf8');
|
|
176
|
+
if (bytes > REVIEW_RESPONSE_MAX_BYTES)
|
|
177
|
+
throw new Error(
|
|
178
|
+
`Remembrancer response is ${bytes} UTF-8 bytes, over the ${REVIEW_RESPONSE_MAX_BYTES}-byte limit; metadata or envelope cannot fit.`,
|
|
179
|
+
);
|
|
180
|
+
return text;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function fitsResponse(data: object): boolean {
|
|
184
|
+
const text = JSON.stringify(data);
|
|
185
|
+
if (text === undefined) throw new Error('Remembrancer response is not JSON serializable.');
|
|
186
|
+
return Buffer.byteLength(text, 'utf8') <= REVIEW_RESPONSE_MAX_BYTES;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function sameGroup(
|
|
190
|
+
a: Pick<Evidence, 'file' | 'sessionId' | 'cwd' | 'branchId' | 'reviewedThrough'>,
|
|
191
|
+
b: Pick<Evidence, 'file' | 'sessionId' | 'cwd' | 'branchId' | 'reviewedThrough'>,
|
|
192
|
+
): boolean {
|
|
193
|
+
return (
|
|
194
|
+
a.file === b.file &&
|
|
195
|
+
a.sessionId === b.sessionId &&
|
|
196
|
+
a.cwd === b.cwd &&
|
|
197
|
+
a.branchId === b.branchId &&
|
|
198
|
+
a.reviewedThrough === b.reviewedThrough
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function groupEvidencePage(entries: readonly EvidencePageEntry[]): EvidenceGroup[] {
|
|
203
|
+
const groups: EvidenceGroup[] = [];
|
|
204
|
+
for (const entry of entries) {
|
|
205
|
+
let group = groups.at(-1);
|
|
206
|
+
if (!group || !sameGroup(group, entry)) {
|
|
207
|
+
group = {
|
|
208
|
+
file: entry.file,
|
|
209
|
+
sessionId: entry.sessionId,
|
|
210
|
+
cwd: entry.cwd,
|
|
211
|
+
branchId: entry.branchId,
|
|
212
|
+
reviewedThrough: entry.reviewedThrough,
|
|
213
|
+
entries: [],
|
|
214
|
+
};
|
|
215
|
+
groups.push(group);
|
|
216
|
+
}
|
|
217
|
+
group.entries.push({
|
|
218
|
+
entryId: entry.entryId,
|
|
219
|
+
role: entry.role,
|
|
220
|
+
text: entry.text,
|
|
221
|
+
timestamp: entry.timestamp,
|
|
222
|
+
editId: entry.editId,
|
|
223
|
+
offset: entry.offset,
|
|
224
|
+
continues: entry.continues,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
return groups;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function splitsSurrogatePair(text: string, boundary: number): boolean {
|
|
231
|
+
if (boundary <= 0 || boundary >= text.length) return false;
|
|
232
|
+
const before = text.charCodeAt(boundary - 1);
|
|
233
|
+
const after = text.charCodeAt(boundary);
|
|
234
|
+
return before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function safePrefixLength(text: string, start: number, maxUnits: number): number {
|
|
238
|
+
let length = Math.min(maxUnits, text.length - start);
|
|
239
|
+
if (length > 0 && splitsSurrogatePair(text, start + length)) length--;
|
|
240
|
+
return length;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function boundedEvidencePage(
|
|
244
|
+
evidence: readonly Evidence[],
|
|
245
|
+
initial: PageCursor,
|
|
246
|
+
buildResponse: (entries: EvidencePageEntry[], cursor: PageCursor) => Record<string, unknown>,
|
|
247
|
+
): { data: Record<string, unknown>; cursor: PageCursor } {
|
|
248
|
+
let { index, offset } = initial;
|
|
249
|
+
const entries: EvidencePageEntry[] = [];
|
|
250
|
+
let remainingText = PAGE_MAX_TEXT_UNITS;
|
|
251
|
+
|
|
252
|
+
const candidateFits = (entry: EvidencePageEntry, cursor: PageCursor) =>
|
|
253
|
+
fitsResponse(buildResponse([...entries, entry], cursor));
|
|
254
|
+
const append = (item: Evidence, length: number) => {
|
|
255
|
+
const start = offset;
|
|
256
|
+
const continues = start + length < item.text.length;
|
|
257
|
+
const entry: EvidencePageEntry = {
|
|
258
|
+
...item,
|
|
259
|
+
text: item.text.slice(start, start + length),
|
|
260
|
+
offset: start,
|
|
261
|
+
continues,
|
|
262
|
+
};
|
|
263
|
+
const cursor = continues ? { index, offset: start + length } : { index: index + 1, offset: 0 };
|
|
264
|
+
if (!candidateFits(entry, cursor)) return false;
|
|
265
|
+
entries.push(entry);
|
|
266
|
+
remainingText -= length;
|
|
267
|
+
({ index, offset } = cursor);
|
|
268
|
+
return true;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
while (entries.length < PAGE_MAX_ENTRIES && remainingText > 0 && index < evidence.length) {
|
|
272
|
+
const item = evidence[index];
|
|
273
|
+
if (!item) break;
|
|
274
|
+
const wanted = safePrefixLength(item.text, offset, remainingText);
|
|
275
|
+
|
|
276
|
+
if (item.text.length === offset) {
|
|
277
|
+
if (!append(item, 0)) {
|
|
278
|
+
if (entries.length === 0)
|
|
279
|
+
throw new Error(
|
|
280
|
+
`Remembrancer response cannot fit a complete evidence entry within the ${REVIEW_RESPONSE_MAX_BYTES}-byte UTF-8 limit; metadata or envelope cannot fit.`,
|
|
281
|
+
);
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (wanted === 0) break;
|
|
287
|
+
|
|
288
|
+
if (append(item, wanted)) {
|
|
289
|
+
if (entries.at(-1)?.continues || remainingText === 0) break;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
let low = 0;
|
|
294
|
+
let high = wanted - 1;
|
|
295
|
+
while (low < high) {
|
|
296
|
+
const middle = Math.ceil((low + high) / 2);
|
|
297
|
+
const length = safePrefixLength(item.text, offset, middle);
|
|
298
|
+
if (length === 0) {
|
|
299
|
+
low = middle + 1;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const start = offset;
|
|
303
|
+
const entry: EvidencePageEntry = {
|
|
304
|
+
...item,
|
|
305
|
+
text: item.text.slice(start, start + length),
|
|
306
|
+
offset: start,
|
|
307
|
+
continues: true,
|
|
308
|
+
};
|
|
309
|
+
if (candidateFits(entry, { index, offset: start + length })) {
|
|
310
|
+
low = middle;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
high = middle - 1;
|
|
314
|
+
}
|
|
315
|
+
const length = safePrefixLength(item.text, offset, low);
|
|
316
|
+
if (length > 0 && append(item, length)) break;
|
|
317
|
+
if (entries.length === 0)
|
|
318
|
+
throw new Error(
|
|
319
|
+
`Remembrancer response cannot fit a complete evidence entry within the ${REVIEW_RESPONSE_MAX_BYTES}-byte UTF-8 limit; metadata or envelope cannot fit.`,
|
|
320
|
+
);
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const cursor = { index, offset };
|
|
325
|
+
const data = buildResponse(entries, cursor);
|
|
326
|
+
serializedResponse(data);
|
|
327
|
+
return { data, cursor };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function page(batch: Batch, envelope: Record<string, unknown> = {}) {
|
|
331
|
+
const result = boundedEvidencePage(batch.evidence, batch, (entries, cursor) => ({
|
|
332
|
+
batch: batch.token,
|
|
333
|
+
groups: groupEvidencePage(entries),
|
|
334
|
+
hasMore: cursor.index < batch.evidence.length,
|
|
335
|
+
...envelope,
|
|
336
|
+
}));
|
|
337
|
+
batch.index = result.cursor.index;
|
|
338
|
+
batch.offset = result.cursor.offset;
|
|
339
|
+
return result.data;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function sourceKey(source: Pick<Source, 'sessionId' | 'file'>): string {
|
|
343
|
+
return `${source.sessionId}\u0000${resolve(source.file)}`;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function usesDefaultSessionDir(ctx: ExtensionContext): boolean {
|
|
347
|
+
const manager = ctx.sessionManager as ExtensionContext['sessionManager'] & {
|
|
348
|
+
usesDefaultSessionDir?: () => boolean;
|
|
349
|
+
};
|
|
350
|
+
if (typeof manager.usesDefaultSessionDir !== 'function')
|
|
351
|
+
throw new Error(
|
|
352
|
+
'Upgrade Pi to use Remembrancer seed: this version cannot identify session archive storage.',
|
|
353
|
+
);
|
|
354
|
+
return manager.usesDefaultSessionDir();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function contextPage(batch: string, selected: Selected, items: Evidence[], offset: number) {
|
|
358
|
+
const total = items.reduce((length, item) => length + item.text.length, 0);
|
|
359
|
+
if (offset > total) throw new Error('Context offset is beyond the requested range');
|
|
360
|
+
|
|
361
|
+
let index = 0;
|
|
362
|
+
let localOffset = 0;
|
|
363
|
+
let position = 0;
|
|
364
|
+
while (index < items.length) {
|
|
365
|
+
const item = items[index]!;
|
|
366
|
+
if (item.text.length === 0) {
|
|
367
|
+
if (offset === position) break;
|
|
368
|
+
index++;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
const end = position + item.text.length;
|
|
372
|
+
if (offset < end) {
|
|
373
|
+
localOffset = offset - position;
|
|
374
|
+
if (splitsSurrogatePair(item.text, localOffset))
|
|
375
|
+
throw new Error('Context offset splits a surrogate pair; use a returned nextOffset.');
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
position = end;
|
|
379
|
+
index++;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const result = boundedEvidencePage(items, { index, offset: localOffset }, (entries, cursor) => {
|
|
383
|
+
const nextOffset =
|
|
384
|
+
items.slice(0, cursor.index).reduce((length, item) => length + item.text.length, 0) +
|
|
385
|
+
cursor.offset;
|
|
386
|
+
return {
|
|
387
|
+
batch,
|
|
388
|
+
selected,
|
|
389
|
+
contextOnly: true,
|
|
390
|
+
groups: groupEvidencePage(entries),
|
|
391
|
+
nextOffset: nextOffset < total ? nextOffset : null,
|
|
392
|
+
};
|
|
393
|
+
});
|
|
394
|
+
return result.data;
|
|
37
395
|
}
|
|
38
396
|
|
|
39
397
|
export default function remembrancer(pi: ExtensionAPI) {
|
|
@@ -41,12 +399,15 @@ export default function remembrancer(pi: ExtensionAPI) {
|
|
|
41
399
|
let batch: Batch | undefined;
|
|
42
400
|
const store = () => (db ??= new CheckpointStore());
|
|
43
401
|
|
|
44
|
-
function
|
|
402
|
+
function currentSource(ctx: ExtensionContext): Source | undefined {
|
|
45
403
|
const sm = ctx.sessionManager;
|
|
46
404
|
const file = sm.getSessionFile();
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
405
|
+
return file ? { file: resolve(file), sessionId: sm.getSessionId() } : undefined;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function enroll(ctx: ExtensionContext) {
|
|
409
|
+
const source = currentSource(ctx);
|
|
410
|
+
if (source) store().enroll(source.sessionId, source.file);
|
|
50
411
|
}
|
|
51
412
|
|
|
52
413
|
function release() {
|
|
@@ -54,6 +415,186 @@ export default function remembrancer(pi: ExtensionAPI) {
|
|
|
54
415
|
db?.releaseReview();
|
|
55
416
|
}
|
|
56
417
|
|
|
418
|
+
function result(data: object, details: ReviewResultDetails) {
|
|
419
|
+
return {
|
|
420
|
+
content: [{ type: 'text' as const, text: serializedResponse(data) }],
|
|
421
|
+
details,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function resultDetails(action: ReviewAction, current: Batch): ReviewResultDetails {
|
|
426
|
+
return {
|
|
427
|
+
action,
|
|
428
|
+
batch: current.token,
|
|
429
|
+
...(current.seed === undefined ? {} : { seed: current.seed }),
|
|
430
|
+
...(current.requestedSince === null ? {} : { requestedSince: current.requestedSince }),
|
|
431
|
+
incomplete: current.incomplete,
|
|
432
|
+
warnings: copyWarnings(current.warnings),
|
|
433
|
+
coverageChanged: current.coverageChanged,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
pi.registerCommand(REMEMBRANCER_COMMAND_NAME, {
|
|
438
|
+
description: 'Start a bounded historical Remembrancer review',
|
|
439
|
+
getArgumentCompletions: getRemembrancerArgumentCompletions,
|
|
440
|
+
handler: async (args, ctx) => {
|
|
441
|
+
const parsed = parseRemembrancerCommandArgs(args);
|
|
442
|
+
if (!parsed.ok) {
|
|
443
|
+
ctx.ui.notify(parsed.message, 'error');
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (parsed.mode === 'help') {
|
|
447
|
+
ctx.ui.notify(REMEMBRANCER_HELP, 'info');
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (loadRemembrancerConfig(ctx.cwd, ctx.isProjectTrusted()).disabled) {
|
|
451
|
+
ctx.ui.notify(
|
|
452
|
+
'Remembrancer is disabled in settings. Set pi-remembrancer.disabled to false to enable it.',
|
|
453
|
+
'warning',
|
|
454
|
+
);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
if (!ctx.isIdle()) {
|
|
458
|
+
ctx.ui.notify(
|
|
459
|
+
'Pi is busy. Wait for the current run to finish, then retry the seed command.',
|
|
460
|
+
'warning',
|
|
461
|
+
);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (batch) {
|
|
465
|
+
ctx.ui.notify(
|
|
466
|
+
'A Remembrancer review is already open in this Pi session. Finish or cancel it before starting another seed.',
|
|
467
|
+
'warning',
|
|
468
|
+
);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
pi.sendUserMessage(`/skill:remembrancer seed ${parsed.duration}`, {
|
|
473
|
+
expandPromptTemplates: true,
|
|
474
|
+
});
|
|
475
|
+
},
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
function beginReview(args: ReviewArgs, ctx: ExtensionContext, requestedSince: string | null) {
|
|
479
|
+
const current = args.seed === undefined ? currentSource(ctx) : undefined;
|
|
480
|
+
const budget = new ReviewBudget();
|
|
481
|
+
const enrolled = store().sources();
|
|
482
|
+
const reviewSources = current
|
|
483
|
+
? enrolled.some((source) => source.sessionId === current.sessionId)
|
|
484
|
+
? enrolled.map((source) =>
|
|
485
|
+
source.sessionId === current.sessionId ? { ...source, file: current.file } : source,
|
|
486
|
+
)
|
|
487
|
+
: [...enrolled, current]
|
|
488
|
+
: enrolled;
|
|
489
|
+
const discovery =
|
|
490
|
+
requestedSince === null
|
|
491
|
+
? undefined
|
|
492
|
+
: discoverSources(
|
|
493
|
+
ctx.sessionManager.getSessionDir(),
|
|
494
|
+
enrolled,
|
|
495
|
+
requestedSince,
|
|
496
|
+
usesDefaultSessionDir(ctx),
|
|
497
|
+
budget,
|
|
498
|
+
);
|
|
499
|
+
const sources = discovery?.sources ?? reviewSources;
|
|
500
|
+
const review = prepareReview(
|
|
501
|
+
sources,
|
|
502
|
+
store().reviewed(),
|
|
503
|
+
{ requestedSince },
|
|
504
|
+
discovery?.snapshots,
|
|
505
|
+
budget,
|
|
506
|
+
);
|
|
507
|
+
const successful = new Set(review.successfulSources.map(sourceKey));
|
|
508
|
+
if (current && successful.has(sourceKey(current)))
|
|
509
|
+
store().enroll(current.sessionId, current.file);
|
|
510
|
+
const stagedSources = (discovery?.newSources ?? []).filter((source) =>
|
|
511
|
+
successful.has(sourceKey(source)),
|
|
512
|
+
);
|
|
513
|
+
const warningRecords = [...(discovery?.warnings ?? []), ...review.warnings];
|
|
514
|
+
const warnings = warningMessages(warningRecords);
|
|
515
|
+
const incomplete = Boolean(discovery?.incomplete || review.incomplete);
|
|
516
|
+
const coverageChanged = review.coverageChanged || stagedSources.length > 0;
|
|
517
|
+
|
|
518
|
+
batch = {
|
|
519
|
+
token: randomUUID(),
|
|
520
|
+
tips: review.tips,
|
|
521
|
+
checkpoints: review.checkpoints,
|
|
522
|
+
sourceDefaults: review.sourceDefaults,
|
|
523
|
+
stagedSources,
|
|
524
|
+
snapshots: review.snapshots,
|
|
525
|
+
evidence: review.evidence,
|
|
526
|
+
warnings: warningRecords,
|
|
527
|
+
incomplete,
|
|
528
|
+
coverageChanged,
|
|
529
|
+
...(args.seed === undefined ? {} : { seed: args.seed }),
|
|
530
|
+
requestedSince,
|
|
531
|
+
index: 0,
|
|
532
|
+
offset: 0,
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
const status = incomplete
|
|
536
|
+
? 'incomplete'
|
|
537
|
+
: review.evidence.length > 0
|
|
538
|
+
? 'review'
|
|
539
|
+
: coverageChanged
|
|
540
|
+
? 'coverage-pending'
|
|
541
|
+
: 'already-covered';
|
|
542
|
+
return page(batch, {
|
|
543
|
+
status,
|
|
544
|
+
warnings,
|
|
545
|
+
incomplete,
|
|
546
|
+
coverageChanged,
|
|
547
|
+
...(requestedSince === null ? {} : { requestedSince }),
|
|
548
|
+
capturedTips: batch.tips.length,
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function contextReview(args: ReviewArgs): object {
|
|
553
|
+
if (!batch || args.batch !== batch.token)
|
|
554
|
+
throw new Error('No matching open review in this Pi session.');
|
|
555
|
+
const selected = args.selected;
|
|
556
|
+
if (!selected) throw new Error('Context requires a selected evidence item.');
|
|
557
|
+
const selectedEvidence = batch.evidence.find(
|
|
558
|
+
(item) =>
|
|
559
|
+
item.sessionId === selected.sessionId &&
|
|
560
|
+
item.branchId === selected.branchId &&
|
|
561
|
+
item.entryId === selected.entryId &&
|
|
562
|
+
item.editId === selected.editId,
|
|
563
|
+
);
|
|
564
|
+
if (!selectedEvidence) throw new Error('Selected evidence is not in this review batch.');
|
|
565
|
+
|
|
566
|
+
const snapshot = batch.snapshots.find(
|
|
567
|
+
(candidate) =>
|
|
568
|
+
candidate.source.sessionId === selected.sessionId &&
|
|
569
|
+
resolve(candidate.source.file) === resolve(selectedEvidence.file),
|
|
570
|
+
);
|
|
571
|
+
if (!snapshot) throw new Error('Selected evidence snapshot is unavailable.');
|
|
572
|
+
|
|
573
|
+
const branch = branchEvidence(
|
|
574
|
+
snapshot.tree,
|
|
575
|
+
{
|
|
576
|
+
file: snapshot.source.file,
|
|
577
|
+
sessionId: selected.sessionId,
|
|
578
|
+
entryId: selected.branchId,
|
|
579
|
+
},
|
|
580
|
+
selectedEvidence.reviewedThrough,
|
|
581
|
+
);
|
|
582
|
+
const selectedIndex = branch.findIndex(
|
|
583
|
+
(item) => item.entryId === selected.entryId && item.editId === selected.editId,
|
|
584
|
+
);
|
|
585
|
+
if (selectedIndex < 0) throw new Error('Selected evidence is not in the frozen branch.');
|
|
586
|
+
|
|
587
|
+
const before = args.before ?? 1;
|
|
588
|
+
const offset = args.offset ?? 0;
|
|
589
|
+
if (!Number.isInteger(before) || before < 1 || before > 40)
|
|
590
|
+
throw new Error('Context before must be an integer from 1 to 40.');
|
|
591
|
+
if (!Number.isInteger(offset) || offset < 0)
|
|
592
|
+
throw new Error('Context offset must be a nonnegative integer.');
|
|
593
|
+
|
|
594
|
+
const items = branch.slice(Math.max(0, selectedIndex - before), selectedIndex);
|
|
595
|
+
return contextPage(batch.token, selected, items, offset);
|
|
596
|
+
}
|
|
597
|
+
|
|
57
598
|
pi.on('session_start', (_event, ctx) => {
|
|
58
599
|
if (loadRemembrancerConfig(ctx.cwd, ctx.isProjectTrusted()).disabled) return;
|
|
59
600
|
registerReviewTool();
|
|
@@ -70,59 +611,93 @@ export default function remembrancer(pi: ExtensionAPI) {
|
|
|
70
611
|
name: 'remembrancer_review',
|
|
71
612
|
label: 'Remembrancer review',
|
|
72
613
|
description:
|
|
73
|
-
'Read incremental cross-session evidence or finish a fully resolved review. Follow the remembrancer skill
|
|
614
|
+
'Read incremental cross-session evidence or finish a fully resolved review. Evidence is grouped by consecutive source and branch metadata. Each complete JSON response is capped at 50 KiB UTF-8. Follow the remembrancer skill and read every page before finishing.',
|
|
615
|
+
renderCall: renderReviewCall,
|
|
616
|
+
renderResult: renderReviewResult,
|
|
74
617
|
parameters: Type.Object({
|
|
75
|
-
action: StringEnum(['begin', 'next', 'finish', 'cancel'] as const),
|
|
618
|
+
action: StringEnum(['begin', 'next', 'context', 'finish', 'cancel'] as const),
|
|
76
619
|
batch: Type.Optional(
|
|
77
|
-
Type.String({
|
|
620
|
+
Type.String({
|
|
621
|
+
description: 'Token from begin. Required for next, context, finish, or cancel.',
|
|
622
|
+
}),
|
|
623
|
+
),
|
|
624
|
+
seed: Type.Optional(
|
|
625
|
+
Type.String({ description: 'Seed duration such as 14d. Valid only with action begin.' }),
|
|
78
626
|
),
|
|
627
|
+
selected: Type.Optional(selectedParameters),
|
|
628
|
+
before: Type.Optional(Type.Integer({ minimum: 1, maximum: 40 })),
|
|
629
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
79
630
|
}),
|
|
80
|
-
async execute(_id, args, _signal, _update, ctx) {
|
|
81
|
-
const result = (data: object) => ({
|
|
82
|
-
content: [{ type: 'text' as const, text: JSON.stringify(data) }],
|
|
83
|
-
details: {},
|
|
84
|
-
});
|
|
631
|
+
async execute(_id, args: ReviewArgs, _signal, _update, ctx) {
|
|
85
632
|
if (!ctx.sessionManager.getSessionFile())
|
|
86
633
|
throw new Error('Full review requires a saved Pi session.');
|
|
634
|
+
|
|
635
|
+
const hasContextArguments =
|
|
636
|
+
args.selected !== undefined || args.before !== undefined || args.offset !== undefined;
|
|
637
|
+
if (args.action !== 'context' && hasContextArguments)
|
|
638
|
+
throw new Error('Context arguments require action context.');
|
|
639
|
+
if (args.action !== 'begin' && args.seed !== undefined)
|
|
640
|
+
throw new Error('Seed is valid only with action begin.');
|
|
641
|
+
if (args.action === 'begin' && args.batch !== undefined)
|
|
642
|
+
throw new Error('Begin does not accept a batch token.');
|
|
643
|
+
|
|
87
644
|
if (args.action === 'begin') {
|
|
88
645
|
if (batch)
|
|
89
646
|
throw new Error('Review already open. Continue or cancel batch ' + batch.token);
|
|
647
|
+
// Resolve the seed before claiming the lock. A rejected request must
|
|
648
|
+
// not leave an interactive review open, and later pages must reuse the
|
|
649
|
+
// same absolute instant.
|
|
650
|
+
const requestedSince = args.seed === undefined ? null : resolveSeedSince(args.seed);
|
|
90
651
|
store().claimReview();
|
|
91
652
|
try {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
batch = {
|
|
95
|
-
token: randomUUID(),
|
|
96
|
-
tips: review.tips,
|
|
97
|
-
evidence: review.evidence,
|
|
98
|
-
index: 0,
|
|
99
|
-
offset: 0,
|
|
100
|
-
};
|
|
101
|
-
return result({
|
|
102
|
-
...page(batch),
|
|
103
|
-
warnings: review.warnings,
|
|
104
|
-
capturedTips: batch.tips.length,
|
|
105
|
-
});
|
|
653
|
+
const response = beginReview(args, ctx, requestedSince);
|
|
654
|
+
return result(response, resultDetails('begin', batch!));
|
|
106
655
|
} catch (error) {
|
|
107
656
|
release();
|
|
108
657
|
throw error;
|
|
109
658
|
}
|
|
110
659
|
}
|
|
660
|
+
|
|
111
661
|
if (!batch || args.batch !== batch.token)
|
|
112
662
|
throw new Error('No matching open review in this Pi session.');
|
|
113
|
-
if (args.action === 'next') return result(page(batch));
|
|
663
|
+
if (args.action === 'next') return result(page(batch), resultDetails('next', batch));
|
|
664
|
+
if (args.action === 'context') {
|
|
665
|
+
const response = contextReview(args);
|
|
666
|
+
return result(response, resultDetails('context', batch));
|
|
667
|
+
}
|
|
114
668
|
if (args.action === 'cancel') {
|
|
669
|
+
const details = resultDetails('cancel', batch);
|
|
115
670
|
release();
|
|
116
|
-
return result({ cancelled: true });
|
|
671
|
+
return result({ cancelled: true }, details);
|
|
117
672
|
}
|
|
118
673
|
if (args.action !== 'finish') throw new Error('Unknown remembrancer_review action');
|
|
119
674
|
if (batch.index < batch.evidence.length)
|
|
120
675
|
throw new Error('Read all evidence pages before finishing.');
|
|
121
|
-
|
|
122
|
-
store().finish(batch.tips);
|
|
676
|
+
|
|
123
677
|
const reviewedTips = batch.tips.length;
|
|
678
|
+
const finishedBatch = batch;
|
|
679
|
+
const details = resultDetails('finish', finishedBatch);
|
|
680
|
+
const response: Record<string, unknown> = { finished: true, reviewedTips };
|
|
681
|
+
if (finishedBatch.requestedSince !== null || finishedBatch.incomplete) {
|
|
682
|
+
response['coverageChanged'] = finishedBatch.coverageChanged;
|
|
683
|
+
response['incomplete'] = finishedBatch.incomplete;
|
|
684
|
+
response['warnings'] = warningMessages(finishedBatch.warnings);
|
|
685
|
+
response['status'] = finishedBatch.incomplete
|
|
686
|
+
? 'partial'
|
|
687
|
+
: finishedBatch.coverageChanged
|
|
688
|
+
? 'coverage-finished'
|
|
689
|
+
: 'already-covered';
|
|
690
|
+
if (finishedBatch.requestedSince !== null)
|
|
691
|
+
response['requestedSince'] = finishedBatch.requestedSince;
|
|
692
|
+
}
|
|
693
|
+
const responseResult = result(response, details);
|
|
694
|
+
store().finish({
|
|
695
|
+
sources: finishedBatch.stagedSources,
|
|
696
|
+
sourceDefaults: finishedBatch.sourceDefaults,
|
|
697
|
+
checkpoints: finishedBatch.checkpoints,
|
|
698
|
+
});
|
|
124
699
|
release();
|
|
125
|
-
return
|
|
700
|
+
return responseResult;
|
|
126
701
|
},
|
|
127
702
|
});
|
|
128
703
|
}
|