@robhowley/pi-remembrancer 0.1.0 → 0.3.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 +441 -31
- package/extensions/remembrancer/render.ts +421 -0
- package/extensions/remembrancer/warnings.ts +29 -0
- package/package.json +7 -4
- package/skills/remembrancer/SKILL.md +30 -10
|
@@ -4,16 +4,154 @@ 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
|
|
|
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;
|
|
135
|
+
}
|
|
136
|
+
const matches = REMEMBRANCER_SUBCOMMANDS.filter((item) => item.value.startsWith(token));
|
|
137
|
+
return matches.length > 0 ? matches : null;
|
|
138
|
+
}
|
|
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
|
+
|
|
17
155
|
// Return every character, even when one message is larger than an entire page.
|
|
18
156
|
function page(batch: Batch) {
|
|
19
157
|
const entries = [];
|
|
@@ -36,17 +174,68 @@ function page(batch: Batch) {
|
|
|
36
174
|
return { batch: batch.token, entries, hasMore: batch.index < batch.evidence.length };
|
|
37
175
|
}
|
|
38
176
|
|
|
177
|
+
function sourceKey(source: Pick<Source, 'sessionId' | 'file'>): string {
|
|
178
|
+
return `${source.sessionId}\u0000${resolve(source.file)}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function usesDefaultSessionDir(ctx: ExtensionContext): boolean {
|
|
182
|
+
const manager = ctx.sessionManager as ExtensionContext['sessionManager'] & {
|
|
183
|
+
usesDefaultSessionDir?: () => boolean;
|
|
184
|
+
};
|
|
185
|
+
if (typeof manager.usesDefaultSessionDir !== 'function')
|
|
186
|
+
throw new Error(
|
|
187
|
+
'Upgrade Pi to use Remembrancer seed: this version cannot identify session archive storage.',
|
|
188
|
+
);
|
|
189
|
+
return manager.usesDefaultSessionDir();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function contextPage(items: Evidence[], offset: number) {
|
|
193
|
+
const total = items.reduce((length, item) => length + item.text.length, 0);
|
|
194
|
+
if (offset > total) throw new Error('Context offset is beyond the requested range');
|
|
195
|
+
|
|
196
|
+
const entries = [];
|
|
197
|
+
let position = 0;
|
|
198
|
+
let remaining = 12000;
|
|
199
|
+
let written = 0;
|
|
200
|
+
for (const item of items) {
|
|
201
|
+
const end = position + item.text.length;
|
|
202
|
+
if (offset >= end) {
|
|
203
|
+
position = end;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const localOffset = Math.max(0, offset - position);
|
|
207
|
+
const text = item.text.slice(localOffset, localOffset + remaining);
|
|
208
|
+
if (text.length === 0) break;
|
|
209
|
+
entries.push({
|
|
210
|
+
...item,
|
|
211
|
+
text,
|
|
212
|
+
offset: localOffset,
|
|
213
|
+
continues: localOffset + text.length < item.text.length,
|
|
214
|
+
});
|
|
215
|
+
written += text.length;
|
|
216
|
+
remaining -= text.length;
|
|
217
|
+
position = end;
|
|
218
|
+
if (remaining === 0) break;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const nextOffset = offset + written < total ? offset + written : null;
|
|
222
|
+
return { entries, nextOffset };
|
|
223
|
+
}
|
|
224
|
+
|
|
39
225
|
export default function remembrancer(pi: ExtensionAPI) {
|
|
40
226
|
let db: CheckpointStore | undefined;
|
|
41
227
|
let batch: Batch | undefined;
|
|
42
228
|
const store = () => (db ??= new CheckpointStore());
|
|
43
229
|
|
|
44
|
-
function
|
|
230
|
+
function currentSource(ctx: ExtensionContext): Source | undefined {
|
|
45
231
|
const sm = ctx.sessionManager;
|
|
46
232
|
const file = sm.getSessionFile();
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
233
|
+
return file ? { file: resolve(file), sessionId: sm.getSessionId() } : undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function enroll(ctx: ExtensionContext) {
|
|
237
|
+
const source = currentSource(ctx);
|
|
238
|
+
if (source) store().enroll(source.sessionId, source.file);
|
|
50
239
|
}
|
|
51
240
|
|
|
52
241
|
function release() {
|
|
@@ -54,6 +243,194 @@ export default function remembrancer(pi: ExtensionAPI) {
|
|
|
54
243
|
db?.releaseReview();
|
|
55
244
|
}
|
|
56
245
|
|
|
246
|
+
function result(data: object, details: ReviewResultDetails) {
|
|
247
|
+
return {
|
|
248
|
+
content: [{ type: 'text' as const, text: JSON.stringify(data) }],
|
|
249
|
+
details,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function resultDetails(action: ReviewAction, current: Batch): ReviewResultDetails {
|
|
254
|
+
return {
|
|
255
|
+
action,
|
|
256
|
+
batch: current.token,
|
|
257
|
+
...(current.seed === undefined ? {} : { seed: current.seed }),
|
|
258
|
+
...(current.requestedSince === null ? {} : { requestedSince: current.requestedSince }),
|
|
259
|
+
incomplete: current.incomplete,
|
|
260
|
+
warnings: copyWarnings(current.warnings),
|
|
261
|
+
coverageChanged: current.coverageChanged,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
pi.registerCommand(REMEMBRANCER_COMMAND_NAME, {
|
|
266
|
+
description: 'Start a bounded historical Remembrancer review',
|
|
267
|
+
getArgumentCompletions: getRemembrancerArgumentCompletions,
|
|
268
|
+
handler: async (args, ctx) => {
|
|
269
|
+
const parsed = parseRemembrancerCommandArgs(args);
|
|
270
|
+
if (!parsed.ok) {
|
|
271
|
+
ctx.ui.notify(parsed.message, 'error');
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (parsed.mode === 'help') {
|
|
275
|
+
ctx.ui.notify(REMEMBRANCER_HELP, 'info');
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (loadRemembrancerConfig(ctx.cwd, ctx.isProjectTrusted()).disabled) {
|
|
279
|
+
ctx.ui.notify(
|
|
280
|
+
'Remembrancer is disabled in settings. Set pi-remembrancer.disabled to false to enable it.',
|
|
281
|
+
'warning',
|
|
282
|
+
);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (!ctx.isIdle()) {
|
|
286
|
+
ctx.ui.notify(
|
|
287
|
+
'Pi is busy. Wait for the current run to finish, then retry the seed command.',
|
|
288
|
+
'warning',
|
|
289
|
+
);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (batch) {
|
|
293
|
+
ctx.ui.notify(
|
|
294
|
+
'A Remembrancer review is already open in this Pi session. Finish or cancel it before starting another seed.',
|
|
295
|
+
'warning',
|
|
296
|
+
);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
pi.sendUserMessage(`/skill:remembrancer seed ${parsed.duration}`, {
|
|
301
|
+
expandPromptTemplates: true,
|
|
302
|
+
});
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
function beginReview(args: ReviewArgs, ctx: ExtensionContext, requestedSince: string | null) {
|
|
307
|
+
const current = args.seed === undefined ? currentSource(ctx) : undefined;
|
|
308
|
+
const budget = new ReviewBudget();
|
|
309
|
+
const enrolled = store().sources();
|
|
310
|
+
const reviewSources = current
|
|
311
|
+
? enrolled.some((source) => source.sessionId === current.sessionId)
|
|
312
|
+
? enrolled.map((source) =>
|
|
313
|
+
source.sessionId === current.sessionId ? { ...source, file: current.file } : source,
|
|
314
|
+
)
|
|
315
|
+
: [...enrolled, current]
|
|
316
|
+
: enrolled;
|
|
317
|
+
const discovery =
|
|
318
|
+
requestedSince === null
|
|
319
|
+
? undefined
|
|
320
|
+
: discoverSources(
|
|
321
|
+
ctx.sessionManager.getSessionDir(),
|
|
322
|
+
enrolled,
|
|
323
|
+
requestedSince,
|
|
324
|
+
usesDefaultSessionDir(ctx),
|
|
325
|
+
budget,
|
|
326
|
+
);
|
|
327
|
+
const sources = discovery?.sources ?? reviewSources;
|
|
328
|
+
const review = prepareReview(
|
|
329
|
+
sources,
|
|
330
|
+
store().reviewed(),
|
|
331
|
+
{ requestedSince },
|
|
332
|
+
discovery?.snapshots,
|
|
333
|
+
budget,
|
|
334
|
+
);
|
|
335
|
+
const successful = new Set(review.successfulSources.map(sourceKey));
|
|
336
|
+
if (current && successful.has(sourceKey(current)))
|
|
337
|
+
store().enroll(current.sessionId, current.file);
|
|
338
|
+
const stagedSources = (discovery?.newSources ?? []).filter((source) =>
|
|
339
|
+
successful.has(sourceKey(source)),
|
|
340
|
+
);
|
|
341
|
+
const warningRecords = [...(discovery?.warnings ?? []), ...review.warnings];
|
|
342
|
+
const warnings = warningMessages(warningRecords);
|
|
343
|
+
const incomplete = Boolean(discovery?.incomplete || review.incomplete);
|
|
344
|
+
const coverageChanged = review.coverageChanged || stagedSources.length > 0;
|
|
345
|
+
|
|
346
|
+
batch = {
|
|
347
|
+
token: randomUUID(),
|
|
348
|
+
tips: review.tips,
|
|
349
|
+
checkpoints: review.checkpoints,
|
|
350
|
+
sourceDefaults: review.sourceDefaults,
|
|
351
|
+
stagedSources,
|
|
352
|
+
snapshots: review.snapshots,
|
|
353
|
+
evidence: review.evidence,
|
|
354
|
+
warnings: warningRecords,
|
|
355
|
+
incomplete,
|
|
356
|
+
coverageChanged,
|
|
357
|
+
...(args.seed === undefined ? {} : { seed: args.seed }),
|
|
358
|
+
requestedSince,
|
|
359
|
+
index: 0,
|
|
360
|
+
offset: 0,
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const status = incomplete
|
|
364
|
+
? 'incomplete'
|
|
365
|
+
: review.evidence.length > 0
|
|
366
|
+
? 'review'
|
|
367
|
+
: coverageChanged
|
|
368
|
+
? 'coverage-pending'
|
|
369
|
+
: 'already-covered';
|
|
370
|
+
return {
|
|
371
|
+
...page(batch),
|
|
372
|
+
status,
|
|
373
|
+
warnings,
|
|
374
|
+
incomplete,
|
|
375
|
+
coverageChanged,
|
|
376
|
+
...(requestedSince === null ? {} : { requestedSince }),
|
|
377
|
+
capturedTips: batch.tips.length,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function contextReview(args: ReviewArgs): object {
|
|
382
|
+
if (!batch || args.batch !== batch.token)
|
|
383
|
+
throw new Error('No matching open review in this Pi session.');
|
|
384
|
+
const selected = args.selected;
|
|
385
|
+
if (!selected) throw new Error('Context requires a selected evidence item.');
|
|
386
|
+
const selectedEvidence = batch.evidence.find(
|
|
387
|
+
(item) =>
|
|
388
|
+
item.sessionId === selected.sessionId &&
|
|
389
|
+
item.branchId === selected.branchId &&
|
|
390
|
+
item.entryId === selected.entryId &&
|
|
391
|
+
item.editId === selected.editId,
|
|
392
|
+
);
|
|
393
|
+
if (!selectedEvidence) throw new Error('Selected evidence is not in this review batch.');
|
|
394
|
+
|
|
395
|
+
const snapshot = batch.snapshots.find(
|
|
396
|
+
(candidate) =>
|
|
397
|
+
candidate.source.sessionId === selected.sessionId &&
|
|
398
|
+
resolve(candidate.source.file) === resolve(selectedEvidence.file),
|
|
399
|
+
);
|
|
400
|
+
if (!snapshot) throw new Error('Selected evidence snapshot is unavailable.');
|
|
401
|
+
|
|
402
|
+
const branch = branchEvidence(
|
|
403
|
+
snapshot.tree,
|
|
404
|
+
{
|
|
405
|
+
file: snapshot.source.file,
|
|
406
|
+
sessionId: selected.sessionId,
|
|
407
|
+
entryId: selected.branchId,
|
|
408
|
+
},
|
|
409
|
+
selectedEvidence.reviewedThrough,
|
|
410
|
+
);
|
|
411
|
+
const selectedIndex = branch.findIndex(
|
|
412
|
+
(item) => item.entryId === selected.entryId && item.editId === selected.editId,
|
|
413
|
+
);
|
|
414
|
+
if (selectedIndex < 0) throw new Error('Selected evidence is not in the frozen branch.');
|
|
415
|
+
|
|
416
|
+
const before = args.before ?? 1;
|
|
417
|
+
const offset = args.offset ?? 0;
|
|
418
|
+
if (!Number.isInteger(before) || before < 1 || before > 40)
|
|
419
|
+
throw new Error('Context before must be an integer from 1 to 40.');
|
|
420
|
+
if (!Number.isInteger(offset) || offset < 0)
|
|
421
|
+
throw new Error('Context offset must be a nonnegative integer.');
|
|
422
|
+
|
|
423
|
+
const items = branch.slice(Math.max(0, selectedIndex - before), selectedIndex);
|
|
424
|
+
const paged = contextPage(items, offset);
|
|
425
|
+
return {
|
|
426
|
+
batch: batch.token,
|
|
427
|
+
selected,
|
|
428
|
+
contextOnly: true,
|
|
429
|
+
entries: paged.entries,
|
|
430
|
+
nextOffset: paged.nextOffset,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
57
434
|
pi.on('session_start', (_event, ctx) => {
|
|
58
435
|
if (loadRemembrancerConfig(ctx.cwd, ctx.isProjectTrusted()).disabled) return;
|
|
59
436
|
registerReviewTool();
|
|
@@ -71,58 +448,91 @@ export default function remembrancer(pi: ExtensionAPI) {
|
|
|
71
448
|
label: 'Remembrancer review',
|
|
72
449
|
description:
|
|
73
450
|
'Read incremental cross-session evidence or finish a fully resolved review. Follow the remembrancer skill. Read every page before finishing.',
|
|
451
|
+
renderCall: renderReviewCall,
|
|
452
|
+
renderResult: renderReviewResult,
|
|
74
453
|
parameters: Type.Object({
|
|
75
|
-
action: StringEnum(['begin', 'next', 'finish', 'cancel'] as const),
|
|
454
|
+
action: StringEnum(['begin', 'next', 'context', 'finish', 'cancel'] as const),
|
|
76
455
|
batch: Type.Optional(
|
|
77
|
-
Type.String({
|
|
456
|
+
Type.String({
|
|
457
|
+
description: 'Token from begin. Required for next, context, finish, or cancel.',
|
|
458
|
+
}),
|
|
78
459
|
),
|
|
460
|
+
seed: Type.Optional(
|
|
461
|
+
Type.String({ description: 'Seed duration such as 14d. Valid only with action begin.' }),
|
|
462
|
+
),
|
|
463
|
+
selected: Type.Optional(selectedParameters),
|
|
464
|
+
before: Type.Optional(Type.Integer({ minimum: 1, maximum: 40 })),
|
|
465
|
+
offset: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
79
466
|
}),
|
|
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
|
-
});
|
|
467
|
+
async execute(_id, args: ReviewArgs, _signal, _update, ctx) {
|
|
85
468
|
if (!ctx.sessionManager.getSessionFile())
|
|
86
469
|
throw new Error('Full review requires a saved Pi session.');
|
|
470
|
+
|
|
471
|
+
const hasContextArguments =
|
|
472
|
+
args.selected !== undefined || args.before !== undefined || args.offset !== undefined;
|
|
473
|
+
if (args.action !== 'context' && hasContextArguments)
|
|
474
|
+
throw new Error('Context arguments require action context.');
|
|
475
|
+
if (args.action !== 'begin' && args.seed !== undefined)
|
|
476
|
+
throw new Error('Seed is valid only with action begin.');
|
|
477
|
+
if (args.action === 'begin' && args.batch !== undefined)
|
|
478
|
+
throw new Error('Begin does not accept a batch token.');
|
|
479
|
+
|
|
87
480
|
if (args.action === 'begin') {
|
|
88
481
|
if (batch)
|
|
89
482
|
throw new Error('Review already open. Continue or cancel batch ' + batch.token);
|
|
483
|
+
// Resolve the seed before claiming the lock. A rejected request must
|
|
484
|
+
// not leave an interactive review open, and later pages must reuse the
|
|
485
|
+
// same absolute instant.
|
|
486
|
+
const requestedSince = args.seed === undefined ? null : resolveSeedSince(args.seed);
|
|
90
487
|
store().claimReview();
|
|
91
488
|
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
|
-
});
|
|
489
|
+
const response = beginReview(args, ctx, requestedSince);
|
|
490
|
+
return result(response, resultDetails('begin', batch!));
|
|
106
491
|
} catch (error) {
|
|
107
492
|
release();
|
|
108
493
|
throw error;
|
|
109
494
|
}
|
|
110
495
|
}
|
|
496
|
+
|
|
111
497
|
if (!batch || args.batch !== batch.token)
|
|
112
498
|
throw new Error('No matching open review in this Pi session.');
|
|
113
|
-
if (args.action === 'next') return result(page(batch));
|
|
499
|
+
if (args.action === 'next') return result(page(batch), resultDetails('next', batch));
|
|
500
|
+
if (args.action === 'context') {
|
|
501
|
+
const response = contextReview(args);
|
|
502
|
+
return result(response, resultDetails('context', batch));
|
|
503
|
+
}
|
|
114
504
|
if (args.action === 'cancel') {
|
|
505
|
+
const details = resultDetails('cancel', batch);
|
|
115
506
|
release();
|
|
116
|
-
return result({ cancelled: true });
|
|
507
|
+
return result({ cancelled: true }, details);
|
|
117
508
|
}
|
|
118
509
|
if (args.action !== 'finish') throw new Error('Unknown remembrancer_review action');
|
|
119
510
|
if (batch.index < batch.evidence.length)
|
|
120
511
|
throw new Error('Read all evidence pages before finishing.');
|
|
121
|
-
|
|
122
|
-
store().finish(
|
|
512
|
+
|
|
513
|
+
store().finish({
|
|
514
|
+
sources: batch.stagedSources,
|
|
515
|
+
sourceDefaults: batch.sourceDefaults,
|
|
516
|
+
checkpoints: batch.checkpoints,
|
|
517
|
+
});
|
|
123
518
|
const reviewedTips = batch.tips.length;
|
|
519
|
+
const finishedBatch = batch;
|
|
520
|
+
const details = resultDetails('finish', finishedBatch);
|
|
124
521
|
release();
|
|
125
|
-
|
|
522
|
+
const response: Record<string, unknown> = { finished: true, reviewedTips };
|
|
523
|
+
if (finishedBatch.requestedSince !== null || finishedBatch.incomplete) {
|
|
524
|
+
response['coverageChanged'] = finishedBatch.coverageChanged;
|
|
525
|
+
response['incomplete'] = finishedBatch.incomplete;
|
|
526
|
+
response['warnings'] = warningMessages(finishedBatch.warnings);
|
|
527
|
+
response['status'] = finishedBatch.incomplete
|
|
528
|
+
? 'partial'
|
|
529
|
+
: finishedBatch.coverageChanged
|
|
530
|
+
? 'coverage-finished'
|
|
531
|
+
: 'already-covered';
|
|
532
|
+
if (finishedBatch.requestedSince !== null)
|
|
533
|
+
response['requestedSince'] = finishedBatch.requestedSince;
|
|
534
|
+
}
|
|
535
|
+
return result(response, details);
|
|
126
536
|
},
|
|
127
537
|
});
|
|
128
538
|
}
|