@aeondave/pi-persona-mind 0.9.2 → 0.9.3
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/AGENTS.md +131 -123
- package/CHANGELOG.md +224 -205
- package/README.md +169 -145
- package/docs/DESIGN.md +533 -475
- package/docs/MEMORY-RESEARCH.md +25 -1
- package/package.json +90 -90
- package/src/core/handoff-store.ts +70 -0
- package/src/core/handoff.ts +70 -0
- package/src/core/host-compat.ts +2 -2
- package/src/core/memory.ts +7 -0
- package/src/core/optimize-history.ts +48 -6
- package/src/core/optimize-service.ts +18 -24
- package/src/core/retrieval.ts +70 -22
- package/src/core/service.ts +69 -27
- package/src/extension/handoff.ts +23 -0
- package/src/extension/mind-command.ts +449 -404
- package/src/index.ts +14 -3
- package/src/tools/memory-handoff.ts +30 -0
- package/src/tools/memory-history.ts +46 -0
- package/src/tools/memory-optimize.ts +121 -133
- package/src/tools/memory.ts +466 -427
|
@@ -1,404 +1,449 @@
|
|
|
1
|
-
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
import { inspectStoreFile, type StoreDiagnostic } from "../core/migrate.ts";
|
|
6
|
-
import type { Scope } from "../core/scope.ts";
|
|
7
|
-
import type { MindService } from "../core/service.ts";
|
|
8
|
-
import type { SessionMemory } from "../session-memory.ts";
|
|
9
|
-
import type { MemoryOptimizer } from "../core/optimize-service.ts";
|
|
10
|
-
import { formatOptimizeApplied, formatOptimizeFailure,
|
|
11
|
-
import { isOptimizeTarget } from "../core/optimize.ts";
|
|
12
|
-
import { runMindActivity } from "./activity.ts";
|
|
13
|
-
import type { MindActivityRunner } from "./activity.ts";
|
|
14
|
-
import type { InspectorTab } from "./inspector-data.ts";
|
|
15
|
-
import type { ReviewTarget } from "../core/knowledge-review.ts";
|
|
16
|
-
import type { createKnowledgeReview } from "./knowledge-review.ts";
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
| { type: "
|
|
26
|
-
| { type: "
|
|
27
|
-
| { type: "
|
|
28
|
-
| { type: "
|
|
29
|
-
| { type: "
|
|
30
|
-
| { type: "
|
|
31
|
-
| { type: "
|
|
32
|
-
| { type: "
|
|
33
|
-
| { type: "
|
|
34
|
-
| { type: "
|
|
35
|
-
| { type: "
|
|
36
|
-
| { type: "
|
|
37
|
-
| { type: "
|
|
38
|
-
| { type: "optimize-
|
|
39
|
-
| { type: "optimize-
|
|
40
|
-
| { type: "
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
"/persona-mind
|
|
109
|
-
"/
|
|
110
|
-
"/
|
|
111
|
-
"/
|
|
112
|
-
"/persona-mind
|
|
113
|
-
"/persona-mind
|
|
114
|
-
"/
|
|
115
|
-
"/mind
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
const
|
|
134
|
-
if (!
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if (command.type === "optimize-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
]
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (parts.length ===
|
|
390
|
-
if (parts.length ===
|
|
391
|
-
if (parts.length ===
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { inspectStoreFile, type StoreDiagnostic } from "../core/migrate.ts";
|
|
6
|
+
import type { Scope } from "../core/scope.ts";
|
|
7
|
+
import type { MindService } from "../core/service.ts";
|
|
8
|
+
import type { SessionMemory } from "../session-memory.ts";
|
|
9
|
+
import type { MemoryOptimizer } from "../core/optimize-service.ts";
|
|
10
|
+
import { formatOptimizeApplied, formatOptimizeFailure, formatOptimizeInspection, formatOptimizePlan, formatOptimizeUndone } from "../tools/memory-optimize.ts";
|
|
11
|
+
import { isOptimizeTarget } from "../core/optimize.ts";
|
|
12
|
+
import { runMindActivity } from "./activity.ts";
|
|
13
|
+
import type { MindActivityRunner } from "./activity.ts";
|
|
14
|
+
import type { InspectorTab } from "./inspector-data.ts";
|
|
15
|
+
import type { ReviewTarget } from "../core/knowledge-review.ts";
|
|
16
|
+
import type { createKnowledgeReview } from "./knowledge-review.ts";
|
|
17
|
+
import type { HandoffInput } from "../core/handoff.ts";
|
|
18
|
+
import { runHandoff } from "./handoff.ts";
|
|
19
|
+
import { formatHandoffResult, validateHandoffInput } from "../tools/memory-handoff.ts";
|
|
20
|
+
import { formatMemoryHistory, runMemoryHistory } from "../tools/memory-history.ts";
|
|
21
|
+
|
|
22
|
+
export type MindOptimizeTarget = "project" | "project-long" | "persona" | "shared";
|
|
23
|
+
|
|
24
|
+
export type MindCommand =
|
|
25
|
+
| { type: "snapshot" }
|
|
26
|
+
| { type: "browse"; tab: InspectorTab }
|
|
27
|
+
| { type: "review"; target?: ReviewTarget }
|
|
28
|
+
| { type: "review-show" }
|
|
29
|
+
| { type: "review-apply" }
|
|
30
|
+
| { type: "review-discard" }
|
|
31
|
+
| { type: "help" }
|
|
32
|
+
| { type: "doctor" }
|
|
33
|
+
| { type: "reset" }
|
|
34
|
+
| { type: "migrate-persona" }
|
|
35
|
+
| { type: "session" }
|
|
36
|
+
| { type: "handoff"; input: HandoffInput }
|
|
37
|
+
| { type: "history"; target?: MindOptimizeTarget; receiptId?: string; offset?: number }
|
|
38
|
+
| { type: "optimize-run"; target?: Exclude<MindOptimizeTarget, "shared"> }
|
|
39
|
+
| { type: "optimize-shared-refused" }
|
|
40
|
+
| { type: "optimize-inspect"; target: MindOptimizeTarget }
|
|
41
|
+
| { type: "optimize-show"; planId: string }
|
|
42
|
+
| { type: "optimize-apply"; planId: string }
|
|
43
|
+
| { type: "optimize-history"; target: MindOptimizeTarget }
|
|
44
|
+
| { type: "optimize-undo"; receiptId: string; target: MindOptimizeTarget }
|
|
45
|
+
| { type: "optimize-auto"; action: "status" | "on" | "off" }
|
|
46
|
+
| { type: "invalid"; message: string };
|
|
47
|
+
|
|
48
|
+
const USAGE =
|
|
49
|
+
"usage: /persona-mind [help|browse|review|doctor|session|handoff list|show <id>|complete <id> <revision> <resolution>|forget <id> <revision>|history [target]|history show <receiptId> <target> [offset]|reset|migrate-persona|optimize]";
|
|
50
|
+
|
|
51
|
+
export interface MindCommandDeps {
|
|
52
|
+
agentDir: string;
|
|
53
|
+
resolveScope(ctx: ExtensionContext): Scope;
|
|
54
|
+
rawPersona(ctx: ExtensionContext): string | null;
|
|
55
|
+
getMind(ctx: ExtensionContext): MindService;
|
|
56
|
+
getSessionMemory(): SessionMemory;
|
|
57
|
+
openInspector?(ctx: ExtensionContext, tab: InspectorTab): Promise<void>;
|
|
58
|
+
knowledgeReview?: ReturnType<typeof createKnowledgeReview>;
|
|
59
|
+
getOptimizer(ctx: ExtensionContext): MemoryOptimizer;
|
|
60
|
+
activity?(ctx: ExtensionContext): MindActivityRunner | undefined;
|
|
61
|
+
autoOptimize?: { status(): string; setEnabled(enabled: boolean): void; targets?(): readonly Exclude<MindOptimizeTarget, "shared">[]; startExplicit?(target?: Exclude<MindOptimizeTarget, "shared">): boolean };
|
|
62
|
+
isCurrent(ctx: ExtensionContext): boolean;
|
|
63
|
+
ensureLegacyMigrated(ctx: ExtensionContext, includeAmbiguousPersonaAlias?: boolean, awaitCompletion?: boolean): Promise<void>;
|
|
64
|
+
migrationStatus(): string;
|
|
65
|
+
unavailableReason(ctx: ExtensionContext): string | undefined;
|
|
66
|
+
surfaceWarning(ctx: ExtensionContext, message: string): void;
|
|
67
|
+
pendingWarnings(): readonly string[];
|
|
68
|
+
acknowledgeWarnings(ctx: ExtensionContext, warningCount: number): void;
|
|
69
|
+
capturePolicy(): string;
|
|
70
|
+
isDelegatedLeg: boolean;
|
|
71
|
+
sessionActive(): boolean;
|
|
72
|
+
afterWorkspaceReset(ctx: ExtensionContext): Promise<void>;
|
|
73
|
+
appendResult(ctx: ExtensionContext, title: string, body: string): void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function registerMindCommands(pi: ExtensionAPI, deps: MindCommandDeps): void {
|
|
77
|
+
const handler = async (args: string, ctx: ExtensionContext): Promise<void> => {
|
|
78
|
+
const command = parseMindCommand(args);
|
|
79
|
+
if (command.type !== "help") {
|
|
80
|
+
const unavailable = deps.unavailableReason(ctx);
|
|
81
|
+
if (unavailable) {
|
|
82
|
+
ctx.ui.notify(unavailable, "warning");
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (command.type === "invalid") {
|
|
87
|
+
ctx.ui.notify(command.message, "warning");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (command.type === "review" || command.type === "review-show" || command.type === "review-apply" || command.type === "review-discard") {
|
|
91
|
+
if (deps.isDelegatedLeg || !deps.isCurrent(ctx) || !deps.knowledgeReview) { ctx.ui.notify("Knowledge review requires an active supervisor session.", "warning"); return; }
|
|
92
|
+
if (command.type === "review-show") await deps.knowledgeReview.show(ctx);
|
|
93
|
+
else if (command.type === "review-apply") await deps.knowledgeReview.apply(ctx);
|
|
94
|
+
else if (command.type === "review-discard") deps.knowledgeReview.discard(ctx);
|
|
95
|
+
else await deps.knowledgeReview.start(ctx, command.target ?? (deps.resolveScope(ctx).homeWorkspace ? "persona" : "project-long"));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (command.type === "browse") {
|
|
99
|
+
if (deps.isDelegatedLeg || !deps.isCurrent(ctx) || !deps.openInspector) {
|
|
100
|
+
ctx.ui.notify("Memory inspector requires an active supervisor session.", "warning");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
await deps.openInspector(ctx, command.tab);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (command.type === "help") {
|
|
107
|
+
const body = [
|
|
108
|
+
"/persona-mind — persona memory and active-session notes",
|
|
109
|
+
"F10 or /mind browse [memories|context|timeline] — memory inspector; a opens explicit consolidation/review actions; /mind timeline opens active-branch conversation history",
|
|
110
|
+
"/mind review [project|project-long|persona] — evidence-grounded knowledge review on the session model; stages corrections, never auto-applies",
|
|
111
|
+
"/mind review show|apply|discard — inspect the proposal, approve guarded corrections, or cancel/discard; retirement findings are recommendations only",
|
|
112
|
+
"/persona-mind doctor — inspect store health and migration warnings",
|
|
113
|
+
"/persona-mind session — list notes on this active session branch",
|
|
114
|
+
"/mind handoff list|show <id>|complete <id> <revision> <resolution>|forget <id> <revision> — explicit project handoffs; save through memory action=handoff",
|
|
115
|
+
"/mind history [target] | history show <receiptId> <target> [offset] — inspect retained memory receipts",
|
|
116
|
+
"/persona-mind reset [workspace] — clear this project's short-term memory and backlog",
|
|
117
|
+
"/persona-mind migrate-persona — explicitly import an ambiguous historical persona alias",
|
|
118
|
+
"/persona-mind optimize [all|project|project-long|persona] — bare/all reviews each allowed tier in one bounded sequential pass; a named tier stays single-target",
|
|
119
|
+
"/persona-mind optimize inspect [project|project-long|persona|shared] — read-only inspection; shared changes require a manually reviewed workflow.",
|
|
120
|
+
"/persona-mind optimize show <planId> — inspect exact staged text; /persona-mind optimize apply <planId> commits that plan only",
|
|
121
|
+
"/persona-mind optimize history [target] — inspect bounded durable before/after receipts; /persona-mind optimize undo <receiptId> [target] restores unchanged affected facts (target defaults to project)",
|
|
122
|
+
"/persona-mind optimize auto [status|on|off] — inspect or switch automatic review for this session; off cancels pending work",
|
|
123
|
+
"/mind is an alias. Explicit review bypasses automatic gates but not worker/busy/owner safeguards; it reports bounded coverage and applies only validated CAS plans. Automatic review remains idle-gated.",
|
|
124
|
+
].join("\n");
|
|
125
|
+
publish(ctx, deps, "pi-persona-mind help", body, "Command reference is in the expandable card.");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (command.type === "handoff") {
|
|
129
|
+
if (deps.isDelegatedLeg) { ctx.ui.notify("Handoffs require an active supervisor session.", "warning"); return; }
|
|
130
|
+
const ownerCurrent = captureSessionOwner(deps, ctx);
|
|
131
|
+
const scope = deps.resolveScope(ctx);
|
|
132
|
+
const current = () => { try { return ownerCurrent() && sameScope(scope, deps.resolveScope(ctx)); } catch { return false; } };
|
|
133
|
+
const result = await runHandoff(ctx, scope, current, command.input);
|
|
134
|
+
if (!current()) { ctx.ui.notify("Memory owner changed; inspect the original scope before retrying.", "warning"); return; }
|
|
135
|
+
publish(ctx, deps, "Project handoff", formatHandoffResult(result), result.ok ? "Project handoff is in the expandable card." : "Project handoff was refused; see the expandable card.", result.ok ? "info" : "warning");
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (command.type === "history") {
|
|
139
|
+
const active = captureOptimizer(deps, ctx);
|
|
140
|
+
if (!active || !optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) { ctx.ui.notify("Memory owner unavailable.", "warning"); return; }
|
|
141
|
+
const formatted = await runMemoryHistory(active.optimizer, { ...(command.target === undefined ? {} : { target: command.target }), ...(command.receiptId === undefined ? {} : { receiptId: command.receiptId }), ...(command.offset === undefined ? {} : { offset: command.offset }) });
|
|
142
|
+
if (!optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) { ctx.ui.notify("Memory owner changed; history was withheld.", "warning"); return; }
|
|
143
|
+
publish(ctx, deps, "Memory history", formatted.content[0]?.text ?? "No result.");
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (command.type === "optimize-shared-refused") {
|
|
147
|
+
ctx.ui.notify("Shared memory is excluded from LLM optimization. Use `/mind optimize inspect shared` and a manually reviewed workflow.", "warning");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (command.type === "optimize-run") {
|
|
151
|
+
if (deps.isDelegatedLeg || !deps.autoOptimize?.startExplicit) {
|
|
152
|
+
ctx.ui.notify("Visible Persona-hosted memory review is unavailable in this session.", "warning");
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!captureSessionOwner(deps, ctx)()) {
|
|
156
|
+
ctx.ui.notify("Memory session owner changed; optimization was not started.", "warning");
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const accepted = command.target === undefined ? deps.autoOptimize.startExplicit() : deps.autoOptimize.startExplicit(command.target);
|
|
160
|
+
if (!accepted) ctx.ui.notify("Mind is busy, already has a review worker active, or its memory owner is unavailable; no review was accepted.", "warning");
|
|
161
|
+
else if (command.target !== undefined) ctx.ui.notify(`Mind ${command.target} review requested; bounded inspection and worker admission are preparing in the background.`, "info");
|
|
162
|
+
else {
|
|
163
|
+
const targets = deps.autoOptimize.targets?.() ?? ["project", "project-long", "persona"];
|
|
164
|
+
const scopes = targets.length ? targets.join(", ") : "currently allowed scopes";
|
|
165
|
+
ctx.ui.notify(`Mind review requested for ${scopes} (currently allowed scopes; at most one bounded inspection window per tier, workers sequentially). Inspection and worker admission are preparing in the background.`, "info");
|
|
166
|
+
}
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (command.type === "optimize-auto") {
|
|
170
|
+
if (!captureSessionOwner(deps, ctx)()) {
|
|
171
|
+
ctx.ui.notify("Memory session owner changed; automatic optimization was not changed.", "warning");
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (deps.isDelegatedLeg || !deps.autoOptimize) {
|
|
175
|
+
ctx.ui.notify("Automatic memory optimization is unavailable in this session.", "warning");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (command.action !== "status") deps.autoOptimize.setEnabled(command.action === "on");
|
|
179
|
+
publish(ctx, deps, "Automatic memory optimization", deps.autoOptimize.status(), "Automatic memory optimization status is in the expandable card.");
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (command.type === "migrate-persona") {
|
|
183
|
+
const ownerCurrent = captureSessionOwner(deps, ctx);
|
|
184
|
+
const scopeAtStart = deps.resolveScope(ctx);
|
|
185
|
+
if (!ownerCurrent()) return;
|
|
186
|
+
const rawPersona = deps.rawPersona(ctx);
|
|
187
|
+
await deps.ensureLegacyMigrated(ctx, true, true);
|
|
188
|
+
if (!ownerCurrent() || !sameScope(scopeAtStart, deps.resolveScope(ctx))) return;
|
|
189
|
+
deps.surfaceWarning(ctx, rawPersona === null
|
|
190
|
+
? "AMBIGUOUS persona migration requested, but no active persona is selected; only project aliases were reconciled."
|
|
191
|
+
: `AMBIGUOUS persona migration imported the historical alias for '${rawPersona}'. Review the result; source files were left untouched.`);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (command.type === "optimize-history" || command.type === "optimize-undo") {
|
|
196
|
+
const active = captureOptimizer(deps, ctx);
|
|
197
|
+
if (!active || !optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) {
|
|
198
|
+
ctx.ui.notify("Memory owner changed; optimization history is unavailable in the active scope.", "warning");
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const formatted = command.type === "optimize-history"
|
|
202
|
+
? await runMindActivity(deps.activity?.(ctx), "inspect", command.target, async () => {
|
|
203
|
+
const result = await active.optimizer.history(command.target);
|
|
204
|
+
return result.ok ? formatMemoryHistory(result) : formatOptimizeFailure(result);
|
|
205
|
+
}, (result) => result.details.ok === true)
|
|
206
|
+
: await runMindActivity(deps.activity?.(ctx), "apply", command.target, async () => {
|
|
207
|
+
const result = await active.optimizer.undo(command.receiptId, command.target);
|
|
208
|
+
return result.ok ? formatOptimizeUndone(result) : formatOptimizeFailure(result);
|
|
209
|
+
}, (result) => result.details.ok === true);
|
|
210
|
+
if (!optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) {
|
|
211
|
+
ctx.ui.notify(command.type === "optimize-undo" ? "Memory owner changed while undo was running. Inspect the target memory before retrying; undo may already have completed." : "Memory owner changed while history was read; no stale result was displayed.", "warning");
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
publish(ctx, deps, command.type === "optimize-history" ? "Memory optimization history" : "Memory consolidation undo", formatted.content[0]?.text ?? "No result.");
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (command.type === "optimize-apply") {
|
|
218
|
+
const active = captureOptimizer(deps, ctx);
|
|
219
|
+
if (!active || !optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) {
|
|
220
|
+
ctx.ui.notify("Memory owner changed; this staged plan is no longer available in the active scope.", "warning");
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const plan = active.optimizer.getPlan(command.planId);
|
|
224
|
+
const result = plan.ok
|
|
225
|
+
? await runMindActivity(deps.activity?.(ctx), "apply", plan.target, () => active.optimizer.apply(command.planId), (outcome) => outcome.ok)
|
|
226
|
+
: await active.optimizer.apply(command.planId);
|
|
227
|
+
if (!optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) {
|
|
228
|
+
ctx.ui.notify("The memory owner changed while apply was running. Inspect the target memory before retrying; the staged apply may already have completed.", "warning");
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const formatted = result.ok ? formatOptimizeApplied(result) : formatOptimizeFailure(result);
|
|
232
|
+
publish(ctx, deps, "Memory optimization result", formatted.content[0]?.text ?? "No result.", "Memory optimization result is in the expandable card.");
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (command.type === "optimize-show") {
|
|
236
|
+
const active = captureOptimizer(deps, ctx);
|
|
237
|
+
if (!active || !optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) {
|
|
238
|
+
ctx.ui.notify("Memory owner changed; this staged plan is unavailable in the active scope.", "warning");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const result = active.optimizer.getPlan(command.planId);
|
|
242
|
+
const formatted = result.ok ? formatOptimizePlan(result) : formatOptimizeFailure(result);
|
|
243
|
+
publish(ctx, deps, "Staged memory optimization plan", formatted.content[0]?.text ?? "No result.", "Staged memory plan is in the expandable card.");
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (command.type === "optimize-inspect") {
|
|
247
|
+
await deps.ensureLegacyMigrated(ctx);
|
|
248
|
+
const active = captureOptimizer(deps, ctx);
|
|
249
|
+
if (!active || !optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) {
|
|
250
|
+
ctx.ui.notify("Memory owner changed before inspection; no result was displayed.", "warning");
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const result = await runMindActivity(deps.activity?.(ctx), "inspect", command.target, () => active.optimizer.inspect(command.target), (outcome) => outcome.ok);
|
|
254
|
+
if (!optimizerIsCurrent(deps, ctx, active.optimizer, active.ownerCurrent)) {
|
|
255
|
+
ctx.ui.notify("Memory scope changed while inspection was running; no stale result was displayed.", "warning");
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
const formatted = result.ok ? formatOptimizeInspection(result) : formatOptimizeFailure(result);
|
|
259
|
+
publish(ctx, deps, `Memory optimization · ${command.target}`, formatted.content[0]?.text ?? "No result.", `Memory inspection for ${command.target} is in the expandable card.`);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const ownerCurrent = captureSessionOwner(deps, ctx);
|
|
264
|
+
const scopeAtStart = deps.resolveScope(ctx);
|
|
265
|
+
if (!ownerCurrent()) return;
|
|
266
|
+
if (command.type === "doctor") await deps.ensureLegacyMigrated(ctx);
|
|
267
|
+
else await deps.ensureLegacyMigrated(ctx);
|
|
268
|
+
if (!ownerCurrent() || !sameScope(scopeAtStart, deps.resolveScope(ctx))) return;
|
|
269
|
+
const scope = scopeAtStart;
|
|
270
|
+
if (command.type === "doctor") {
|
|
271
|
+
const diagnostics: Array<[string, StoreDiagnostic]> = await Promise.all([
|
|
272
|
+
inspectStoreFile(scope.paths.ltm, "ltm").then((d): [string, StoreDiagnostic] => ["long-term", d]),
|
|
273
|
+
inspectStoreFile(scope.paths.shared, "ltm").then((d): [string, StoreDiagnostic] => ["shared", d]),
|
|
274
|
+
inspectStoreFile(scope.paths.projectLtm, "ltm").then((d): [string, StoreDiagnostic] => ["project durable", d]),
|
|
275
|
+
inspectStoreFile(scope.paths.stm, "stm").then((d): [string, StoreDiagnostic] => ["short-term", d]),
|
|
276
|
+
inspectStoreFile(scope.paths.backlog, "backlog").then((d): [string, StoreDiagnostic] => ["backlog", d]),
|
|
277
|
+
]);
|
|
278
|
+
if (!ownerCurrent() || !sameScope(scope, deps.resolveScope(ctx))) return;
|
|
279
|
+
const pathLine = ([label, diagnostic]: [string, StoreDiagnostic]): string => {
|
|
280
|
+
const suffix = diagnostic.status === "missing" ? "not created" : diagnostic.status === "ok" ? `ok (${diagnostic.validEntries} entries)` : `${diagnostic.status} (${diagnostic.message ?? `${diagnostic.invalidEntries} invalid entries`})`;
|
|
281
|
+
return `${label}: ${diagnostic.path} (${suffix})`;
|
|
282
|
+
};
|
|
283
|
+
const diagnosticWarnings = diagnostics.filter(([, diagnostic]) => diagnostic.status !== "missing" && diagnostic.status !== "ok").map(([label, diagnostic]) => `${label}: ${diagnostic.status} — ${diagnostic.message ?? "invalid store"}`);
|
|
284
|
+
const allWarnings = [...deps.pendingWarnings(), ...diagnosticWarnings];
|
|
285
|
+
const lines = [
|
|
286
|
+
"pi-persona-mind doctor",
|
|
287
|
+
`persona: ${scope.persona}`,
|
|
288
|
+
`cwd: ${ctx.cwd}`,
|
|
289
|
+
`project root: ${scope.projectRoot}${scope.homeWorkspace ? " (home — STM/backlog not injected, wakes off)" : ""}`,
|
|
290
|
+
`project slug: ${scope.slug}`,
|
|
291
|
+
`capture: ${deps.capturePolicy()} (PI_PERSONA_MIND_CAPTURE)`,
|
|
292
|
+
`automatic optimization: ${deps.autoOptimize?.status() ?? "unavailable in this session"}`,
|
|
293
|
+
`migration: ${deps.migrationStatus()}`,
|
|
294
|
+
...diagnostics.map(pathLine),
|
|
295
|
+
`legacy root: ${join(deps.agentDir, "pi-persona-mind")} (${existsSync(join(deps.agentDir, "pi-persona-mind")) ? "detected; imported non-destructively" : "absent"})`,
|
|
296
|
+
`warnings: ${allWarnings.length === 0 ? "none" : allWarnings.join(" | ")}`,
|
|
297
|
+
];
|
|
298
|
+
deps.acknowledgeWarnings(ctx, allWarnings.length);
|
|
299
|
+
publish(ctx, deps, "Memory doctor", lines.join("\n"), allWarnings.length === 0 ? "Memory doctor: healthy; see the expandable card." : `Memory doctor found ${allWarnings.length} warning(s); see the expandable card.`, allWarnings.length === 0 ? "info" : "warning");
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (command.type === "reset") {
|
|
303
|
+
if (deps.isDelegatedLeg) {
|
|
304
|
+
ctx.ui.notify("a delegated worker cannot reset the supervisor's workspace mind", "warning");
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const mind = deps.getMind(ctx);
|
|
308
|
+
const result = await mind.resetWorkspace();
|
|
309
|
+
if (!ownerCurrent() || !sameScope(scope, deps.resolveScope(ctx))) return;
|
|
310
|
+
if (!result.ok) {
|
|
311
|
+
ctx.ui.notify(`mind reset failed: ${result.reason}`, "error");
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (deps.sessionActive()) {
|
|
315
|
+
await deps.afterWorkspaceReset(ctx);
|
|
316
|
+
if (!ownerCurrent() || !sameScope(scope, deps.resolveScope(ctx))) return;
|
|
317
|
+
}
|
|
318
|
+
const receipt = `Cleared this workspace (${scope.projectRoot}): ${result.stmRemoved} short-term · ${result.backlogRemoved} backlog. Long-term identity (${result.ltmKept}) left intact.`;
|
|
319
|
+
publish(ctx, deps, "Workspace memory reset", receipt, receipt);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (command.type === "session") {
|
|
323
|
+
const result = deps.getSessionMemory().snapshot(ctx);
|
|
324
|
+
if (!result.ok) {
|
|
325
|
+
ctx.ui.notify(`Session notes unavailable: ${result.reason ?? "active session owner is missing"}`, "warning");
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const rows = result.notes.slice(-20).reverse().map((note) => `- [${note.id}] (${note.kind}) ${note.text.replace(/\s+/g, " ").slice(0, 160)}${note.text.length > 160 ? "…" : ""}`);
|
|
329
|
+
const omitted = result.notes.length > rows.length ? `\n… ${result.notes.length - rows.length} older note(s) omitted; use memory read with an id for full text.` : "";
|
|
330
|
+
const body = `Active-session notes (${result.notes.length}, storage: ${result.storage})${rows.length ? `\n${rows.join("\n")}` : "\n(none)"}${omitted}`;
|
|
331
|
+
publish(ctx, deps, "Active-session notes", body, `Active-session notes: ${result.notes.length}; details are in the expandable card.`);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const mind = deps.getMind(ctx);
|
|
335
|
+
const [summary, block] = await Promise.all([mind.summary(), mind.buildInjection()]);
|
|
336
|
+
if (!ownerCurrent() || !sameScope(scope, deps.resolveScope(ctx))) return;
|
|
337
|
+
const counts = `${summary.ltm} persona/shared · ${summary.projectLtm} project durable · ${summary.stm} working · ${summary.backlogOpen} open backlog`;
|
|
338
|
+
const header = `pi-persona-mind (${scope.persona}) — ${counts}`;
|
|
339
|
+
publish(ctx, deps, "Persona mind snapshot", block ? `${header}\n\n${block}` : `${header}\n\n(empty)`, counts);
|
|
340
|
+
};
|
|
341
|
+
const description = "Persona memory, active-session notes, diagnostics, and reviewed optimization";
|
|
342
|
+
pi.registerCommand("persona-mind", { description, handler });
|
|
343
|
+
pi.registerCommand("mind", { description: `${description} (alias)`, handler });
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function publish(ctx: ExtensionContext, deps: MindCommandDeps, title: string, body: string, notice = `${title} is in the expandable card.`, level: "info" | "warning" = "info"): void {
|
|
347
|
+
try {
|
|
348
|
+
deps.appendResult(ctx, title, body);
|
|
349
|
+
ctx.ui.notify(notice, level);
|
|
350
|
+
} catch {
|
|
351
|
+
ctx.ui.notify(`${title}: ${body.split("\n", 1)[0]?.slice(0, 180) ?? "result unavailable"}`, "warning");
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function captureOptimizer(deps: MindCommandDeps, ctx: ExtensionContext): { optimizer: MemoryOptimizer; ownerCurrent: () => boolean } | undefined {
|
|
356
|
+
try {
|
|
357
|
+
if (!deps.isCurrent(ctx)) return undefined;
|
|
358
|
+
const ownerCurrent = deps.getSessionMemory().captureOwner(ctx);
|
|
359
|
+
if (!ownerCurrent()) return undefined;
|
|
360
|
+
return { optimizer: deps.getOptimizer(ctx), ownerCurrent };
|
|
361
|
+
} catch { return undefined; }
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function optimizerIsCurrent(deps: MindCommandDeps, ctx: ExtensionContext, captured: MemoryOptimizer, ownerCurrent: () => boolean): boolean {
|
|
365
|
+
try { return ownerCurrent() && deps.isCurrent(ctx) && deps.getOptimizer(ctx) === captured; }
|
|
366
|
+
catch { return false; }
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function captureSessionOwner(deps: MindCommandDeps, ctx: ExtensionContext): () => boolean {
|
|
370
|
+
try {
|
|
371
|
+
const ownerCurrent = deps.getSessionMemory().captureOwner(ctx);
|
|
372
|
+
return () => { try { return ownerCurrent() && deps.isCurrent(ctx); } catch { return false; } };
|
|
373
|
+
} catch { return () => false; }
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function sameScope(left: Scope, right: Scope): boolean {
|
|
377
|
+
return left.persona === right.persona && left.projectRoot === right.projectRoot && left.slug === right.slug &&
|
|
378
|
+
left.paths.ltm === right.paths.ltm && left.paths.shared === right.paths.shared && left.paths.projectLtm === right.paths.projectLtm && left.paths.stm === right.paths.stm && left.paths.backlog === right.paths.backlog;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Parse the shared /persona-mind and /mind command grammar without performing any action. */
|
|
382
|
+
export function parseMindCommand(args: string): MindCommand {
|
|
383
|
+
if (typeof args !== "string") return { type: "invalid", message: USAGE };
|
|
384
|
+
const parts = args.trim().toLowerCase().split(/\s+/).filter(Boolean);
|
|
385
|
+
const original = args.trim().split(/\s+/).filter(Boolean);
|
|
386
|
+
if (parts.length === 0) return { type: "snapshot" };
|
|
387
|
+
if (parts[0] === "handoff") {
|
|
388
|
+
let input: unknown;
|
|
389
|
+
if (parts.length === 2 && parts[1] === "list") input = { operation: "list" };
|
|
390
|
+
else if (parts.length === 3 && parts[1] === "show") input = { operation: "read", id: original[2] };
|
|
391
|
+
else if (parts.length === 4 && parts[1] === "forget") input = { operation: "forget", id: original[2], revision: Number(original[3]) };
|
|
392
|
+
else if (parts.length >= 5 && parts[1] === "complete") input = { operation: "complete", id: original[2], revision: Number(original[3]), resolution: original.slice(4).join(" ") };
|
|
393
|
+
return validateHandoffInput(input) ? { type: "handoff", input } : { type: "invalid", message: USAGE };
|
|
394
|
+
}
|
|
395
|
+
if (parts[0] === "history") {
|
|
396
|
+
if (parts.length === 1) return { type: "history" };
|
|
397
|
+
if (parts.length === 2 && isOptimizeTarget(parts[1])) return { type: "history", target: parts[1] };
|
|
398
|
+
if ((parts.length === 4 || parts.length === 5) && parts[1] === "show" && isPlanId(original[2]) && isOptimizeTarget(parts[3])) {
|
|
399
|
+
const offset = parts.length === 5 ? Number(original[4]) : undefined;
|
|
400
|
+
if (offset !== undefined && (!Number.isSafeInteger(offset) || offset < 0)) return { type: "invalid", message: USAGE };
|
|
401
|
+
return { type: "history", receiptId: original[2], target: parts[3], ...(offset === undefined ? {} : { offset }) };
|
|
402
|
+
}
|
|
403
|
+
return { type: "invalid", message: USAGE };
|
|
404
|
+
}
|
|
405
|
+
if (parts.length === 1 && parts[0] === "help") return { type: "help" };
|
|
406
|
+
if (parts[0] === "review") {
|
|
407
|
+
if (parts.length === 1) return { type: "review" };
|
|
408
|
+
if (parts.length === 2 && (parts[1] === "project" || parts[1] === "project-long" || parts[1] === "persona")) return { type: "review", target: parts[1] };
|
|
409
|
+
if (parts.length === 2 && (parts[1] === "show" || parts[1] === "apply" || parts[1] === "discard")) return { type: `review-${parts[1]}` };
|
|
410
|
+
return { type: "invalid", message: "usage: /mind review [project|project-long|persona|show|apply|discard]. Shared and session notes are not model-review targets." };
|
|
411
|
+
}
|
|
412
|
+
if (parts.length === 1 && parts[0] === "timeline") return { type: "browse", tab: "timeline" };
|
|
413
|
+
if (parts[0] === "browse") {
|
|
414
|
+
if (parts.length === 1) return { type: "browse", tab: "memories" };
|
|
415
|
+
if (parts.length === 2 && (parts[1] === "memories" || parts[1] === "context" || parts[1] === "timeline")) return { type: "browse", tab: parts[1] };
|
|
416
|
+
return { type: "invalid", message: USAGE };
|
|
417
|
+
}
|
|
418
|
+
if (parts.length === 1 && parts[0] === "doctor") return { type: "doctor" };
|
|
419
|
+
if (parts.length === 1 && parts[0] === "session") return { type: "session" };
|
|
420
|
+
if (parts.length === 1 && parts[0] === "reset") return { type: "reset" };
|
|
421
|
+
if (parts.length === 2 && parts[0] === "reset" && parts[1] === "workspace") return { type: "reset" };
|
|
422
|
+
if (parts[0] === "reset") return { type: "invalid", message: "reset only clears THIS PROJECT's short-term memory and backlog. Long-term persona identity is not workspace-scoped — forget individual facts with `memory forget <id>`." };
|
|
423
|
+
if (parts.length === 1 && parts[0] === "migrate-persona") return { type: "migrate-persona" };
|
|
424
|
+
if (parts[0] === "optimize") {
|
|
425
|
+
if (parts[1] === "history" && (parts.length === 2 || parts.length === 3 && isOptimizeTarget(parts[2]))) return { type: "optimize-history", target: parts[2] as MindOptimizeTarget ?? "project" };
|
|
426
|
+
if (parts[1] === "undo" && (parts.length === 3 || parts.length === 4 && isOptimizeTarget(parts[3])) && isPlanId(args.trim().split(/\s+/)[2])) return { type: "optimize-undo", receiptId: args.trim().split(/\s+/)[2]!, target: parts[3] as MindOptimizeTarget ?? "project" };
|
|
427
|
+
if (parts[1] === "auto") {
|
|
428
|
+
if (parts.length === 2) return { type: "optimize-auto", action: "status" };
|
|
429
|
+
if (parts.length === 3 && (parts[2] === "status" || parts[2] === "on" || parts[2] === "off")) return { type: "optimize-auto", action: parts[2] };
|
|
430
|
+
return { type: "invalid", message: USAGE };
|
|
431
|
+
}
|
|
432
|
+
if (parts[1] === "inspect" && parts.length === 2) return { type: "optimize-inspect", target: "project" };
|
|
433
|
+
if (parts[1] === "inspect" && parts.length === 3 && isOptimizeTarget(parts[2])) return { type: "optimize-inspect", target: parts[2] };
|
|
434
|
+
if (parts.length === 1 || parts.length === 2 && parts[1] === "all") return { type: "optimize-run" };
|
|
435
|
+
if (parts.length === 2 && parts[1] === "shared") return { type: "optimize-shared-refused" };
|
|
436
|
+
if (parts.length === 2 && (parts[1] === "project" || parts[1] === "project-long" || parts[1] === "persona")) {
|
|
437
|
+
return { type: "optimize-run", target: parts[1] };
|
|
438
|
+
}
|
|
439
|
+
if (parts.length === 3 && (parts[1] === "show" || parts[1] === "apply") && isPlanId(args.trim().split(/\s+/)[2])) {
|
|
440
|
+
const planId = args.trim().split(/\s+/)[2]!;
|
|
441
|
+
return parts[1] === "show" ? { type: "optimize-show", planId } : { type: "optimize-apply", planId };
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return { type: "invalid", message: USAGE };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function isPlanId(value: string | undefined): value is string {
|
|
448
|
+
return typeof value === "string" && /^[A-Za-z0-9_-]{1,160}$/.test(value);
|
|
449
|
+
}
|