@narumitw/pi-goal 0.49.5 → 0.49.7
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 +3 -3
- package/package.json +9 -5
- package/src/command-registration.ts +70 -10
- package/src/commands.ts +60 -40
- package/src/errors.ts +30 -1
- package/src/lifecycle.ts +22 -11
- package/src/menu.ts +22 -26
- package/src/runtime.ts +39 -8
- package/src/settings-ui.ts +26 -21
- package/src/tools.ts +98 -67
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ pi -e npm:@narumitw/pi-goal
|
|
|
52
52
|
Try this package locally from the repository root:
|
|
53
53
|
|
|
54
54
|
```bash
|
|
55
|
-
pi -e ./
|
|
55
|
+
pi -e ./packages/pi-goal
|
|
56
56
|
```
|
|
57
57
|
|
|
58
58
|
## ⚙️ Configuration
|
|
@@ -310,11 +310,11 @@ This breaking contract replaces and removes `pi-goal:rpc:start`, `pi-goal:rpc:pa
|
|
|
310
310
|
## 🗂️ Package layout
|
|
311
311
|
|
|
312
312
|
```txt
|
|
313
|
-
|
|
313
|
+
packages/pi-goal/
|
|
314
314
|
├── src/
|
|
315
315
|
│ ├── index.ts # Pi package entrypoint
|
|
316
316
|
│ ├── goal.ts # Order-explicit extension composition root
|
|
317
|
-
│ ├── command-registration.ts #
|
|
317
|
+
│ ├── command-registration.ts # Lightweight slash-command adapter with lazy manager/settings UI
|
|
318
318
|
│ ├── commands.ts # Per-factory user-command and queue mutation controller
|
|
319
319
|
│ ├── tools.ts # Goal completion and blocker tool adapters
|
|
320
320
|
│ ├── lifecycle.ts # Pi session, agent, tool, and compaction event adapter
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@narumitw/pi-goal",
|
|
3
|
-
"version": "0.49.
|
|
3
|
+
"version": "0.49.7",
|
|
4
4
|
"description": "Pi extension for autonomous /goal completion with opt-in experimental ordered queues.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,6 +23,9 @@
|
|
|
23
23
|
"./src/index.ts"
|
|
24
24
|
]
|
|
25
25
|
},
|
|
26
|
+
"piExtension": {
|
|
27
|
+
"lifecycle": "stable"
|
|
28
|
+
},
|
|
26
29
|
"scripts": {
|
|
27
30
|
"check": "biome check . && npm run typecheck",
|
|
28
31
|
"format": "biome check --write .",
|
|
@@ -37,9 +40,10 @@
|
|
|
37
40
|
},
|
|
38
41
|
"devDependencies": {
|
|
39
42
|
"@biomejs/biome": "2.5.7",
|
|
40
|
-
"@earendil-works/pi-ai": "0.
|
|
41
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
42
|
-
"
|
|
43
|
+
"@earendil-works/pi-ai": "0.84.1",
|
|
44
|
+
"@earendil-works/pi-coding-agent": "0.84.1",
|
|
45
|
+
"@earendil-works/pi-tui": "0.84.1",
|
|
46
|
+
"typebox": "1.3.11",
|
|
43
47
|
"typescript": "7.0.2"
|
|
44
48
|
},
|
|
45
49
|
"dependencies": {
|
|
@@ -48,6 +52,6 @@
|
|
|
48
52
|
"repository": {
|
|
49
53
|
"type": "git",
|
|
50
54
|
"url": "https://github.com/narumiruna/pi-extensions",
|
|
51
|
-
"directory": "
|
|
55
|
+
"directory": "packages/pi-goal"
|
|
52
56
|
}
|
|
53
57
|
}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { completeGoalArguments, parseCommand } from "./command.js";
|
|
3
3
|
import type { GoalCommandController } from "./commands.js";
|
|
4
|
-
import {
|
|
4
|
+
import { notifyTerminal, safeTerminalText } from "./errors.js";
|
|
5
5
|
import type { GoalRuntime } from "./runtime.js";
|
|
6
|
-
import { showGoalSettings } from "./settings-ui.js";
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
type GoalManagerModule = Pick<typeof import("./menu.js"), "showGoalManager">;
|
|
8
|
+
type GoalSettingsModule = Pick<typeof import("./settings-ui.js"), "showGoalSettings">;
|
|
9
|
+
|
|
10
|
+
export interface GoalCommandRegistrationOptions {
|
|
9
11
|
settingsPath?: string;
|
|
12
|
+
loadGoalManager?: () => Promise<GoalManagerModule>;
|
|
13
|
+
loadGoalSettings?: () => Promise<GoalSettingsModule>;
|
|
10
14
|
}
|
|
11
15
|
|
|
12
16
|
export function registerGoalCommand(
|
|
@@ -15,6 +19,13 @@ export function registerGoalCommand(
|
|
|
15
19
|
commands: GoalCommandController,
|
|
16
20
|
options: GoalCommandRegistrationOptions = {},
|
|
17
21
|
) {
|
|
22
|
+
const loadGoalManager = cachedModuleLoader(
|
|
23
|
+
options.loadGoalManager ?? (() => import("./menu.js")),
|
|
24
|
+
);
|
|
25
|
+
const loadGoalSettings = cachedModuleLoader(
|
|
26
|
+
options.loadGoalSettings ?? (() => import("./settings-ui.js")),
|
|
27
|
+
);
|
|
28
|
+
|
|
18
29
|
pi.registerCommand("goal", {
|
|
19
30
|
description: "Run a goal to completion: /goal [--tokens 100k] <goal_to_complete>",
|
|
20
31
|
getArgumentCompletions: (prefix) =>
|
|
@@ -26,19 +37,39 @@ export function registerGoalCommand(
|
|
|
26
37
|
experimentalGoals: runtime.settings.experimental.goals,
|
|
27
38
|
});
|
|
28
39
|
if (typeof result === "string") {
|
|
29
|
-
|
|
40
|
+
reportCommandError(result, ctx);
|
|
30
41
|
return;
|
|
31
42
|
}
|
|
32
43
|
if (result.kind === "show" && args.trim() === "") {
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
const menuIsCurrent = captureMenuOwnership(runtime);
|
|
45
|
+
let managerModule: GoalManagerModule;
|
|
46
|
+
try {
|
|
47
|
+
managerModule = await loadGoalManager();
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (!menuIsCurrent()) return;
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
if (!menuIsCurrent()) return;
|
|
53
|
+
const { showGoalManager } = managerModule;
|
|
54
|
+
await showGoalManager(runtime, commands, ctx, async (menuCtx, target) => {
|
|
55
|
+
const settingsAreCurrent = captureMenuOwnership(runtime);
|
|
56
|
+
let settingsModule: GoalSettingsModule;
|
|
57
|
+
try {
|
|
58
|
+
settingsModule = await loadGoalSettings();
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (!settingsAreCurrent()) return;
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
if (!settingsAreCurrent()) return;
|
|
64
|
+
const { showGoalSettings } = settingsModule;
|
|
65
|
+
await showGoalSettings(runtime, menuCtx, {
|
|
35
66
|
settingsPath: options.settingsPath,
|
|
36
67
|
initialScreen: target,
|
|
37
68
|
onQueueUnfrozen: async (settingsCtx) => {
|
|
38
69
|
await commands.resumeQueueAfterUnfreeze(settingsCtx);
|
|
39
70
|
},
|
|
40
|
-
})
|
|
41
|
-
);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
42
73
|
return;
|
|
43
74
|
}
|
|
44
75
|
if (runtime.queueFrozen) {
|
|
@@ -48,7 +79,8 @@ export function registerGoalCommand(
|
|
|
48
79
|
return;
|
|
49
80
|
}
|
|
50
81
|
if (runtime.pendingQueueAction && result.kind !== "show" && result.kind !== "clear") {
|
|
51
|
-
|
|
82
|
+
notifyTerminal(
|
|
83
|
+
ctx.ui,
|
|
52
84
|
"A queued goal change is waiting for Pi to settle. Retry after it finishes.",
|
|
53
85
|
"warning",
|
|
54
86
|
);
|
|
@@ -90,3 +122,31 @@ export function registerGoalCommand(
|
|
|
90
122
|
},
|
|
91
123
|
});
|
|
92
124
|
}
|
|
125
|
+
|
|
126
|
+
function reportCommandError(message: string, ctx: ExtensionCommandContext) {
|
|
127
|
+
const safeMessage = safeTerminalText(message);
|
|
128
|
+
if (ctx.mode === "print" || ctx.mode === "json") throw new Error(safeMessage);
|
|
129
|
+
notifyTerminal(ctx.ui, safeMessage, "warning");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function captureMenuOwnership(runtime: GoalRuntime): () => boolean {
|
|
133
|
+
const generation = runtime.menuGeneration;
|
|
134
|
+
const controller = runtime.menuController;
|
|
135
|
+
return () =>
|
|
136
|
+
runtime.menuGeneration === generation &&
|
|
137
|
+
runtime.menuController === controller &&
|
|
138
|
+
!controller.signal.aborted;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function cachedModuleLoader<Module>(load: () => Promise<Module>): () => Promise<Module> {
|
|
142
|
+
let pending: Promise<Module> | undefined;
|
|
143
|
+
return () => {
|
|
144
|
+
if (!pending) {
|
|
145
|
+
pending = load().catch((error) => {
|
|
146
|
+
pending = undefined;
|
|
147
|
+
throw error;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return pending;
|
|
151
|
+
};
|
|
152
|
+
}
|
package/src/commands.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { checkpointGoalActiveTime, currentTokenTotal, formatTokenCount } from "./accounting.js";
|
|
2
2
|
import { validateObjective } from "./command.js";
|
|
3
|
-
import { safeGoalMenuText } from "./
|
|
3
|
+
import { notifyTerminal, safeGoalMenuText } from "./errors.js";
|
|
4
4
|
import type { ActiveGoal } from "./persistence.js";
|
|
5
5
|
import { buildGoalPrompt, buildObjectiveUpdatedPrompt, buildResumePrompt } from "./prompts.js";
|
|
6
6
|
import {
|
|
@@ -50,7 +50,7 @@ export class GoalCommandController {
|
|
|
50
50
|
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
51
51
|
const validationError = validateObjective(objective);
|
|
52
52
|
if (validationError) {
|
|
53
|
-
ctx.ui
|
|
53
|
+
notifyTerminal(ctx.ui, validationError, "warning");
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -74,7 +74,7 @@ export class GoalCommandController {
|
|
|
74
74
|
`Current goal: ${safeGoalMenuText(existingGoal.text, 4_000)}${queuedRemovalPreview}\n\nNew goal: ${safeGoalMenuText(objective, 4_000)}`,
|
|
75
75
|
);
|
|
76
76
|
if (!shouldReplace) {
|
|
77
|
-
ctx.ui
|
|
77
|
+
notifyTerminal(ctx.ui, `Goal kept: ${existingGoal.text}`, "info");
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
80
|
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
@@ -85,7 +85,11 @@ export class GoalCommandController {
|
|
|
85
85
|
this.runtime.pendingQueueAction,
|
|
86
86
|
) !== existingQueueIdentity
|
|
87
87
|
) {
|
|
88
|
-
|
|
88
|
+
notifyTerminal(
|
|
89
|
+
ctx.ui,
|
|
90
|
+
"The goal queue changed while confirmation was open. Try again.",
|
|
91
|
+
"warning",
|
|
92
|
+
);
|
|
89
93
|
return;
|
|
90
94
|
}
|
|
91
95
|
}
|
|
@@ -97,7 +101,7 @@ export class GoalCommandController {
|
|
|
97
101
|
try {
|
|
98
102
|
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
99
103
|
} catch (error) {
|
|
100
|
-
ctx.ui
|
|
104
|
+
notifyTerminal(ctx.ui, `Cannot start /goal: ${formatError(error)}`, "error");
|
|
101
105
|
if (existingGoal?.status === "active") this.runtime.pauseGoalForUnavailableTools(ctx);
|
|
102
106
|
return;
|
|
103
107
|
}
|
|
@@ -167,7 +171,8 @@ export class GoalCommandController {
|
|
|
167
171
|
return;
|
|
168
172
|
}
|
|
169
173
|
const automaticLimit = this.runtime.settings.continuationLimits.automaticTurns;
|
|
170
|
-
|
|
174
|
+
notifyTerminal(
|
|
175
|
+
ctx.ui,
|
|
171
176
|
`${existingGoal ? "Goal replaced" : "Goal started"}: ${objective}. ${
|
|
172
177
|
startedGoal.tokenBudget === undefined
|
|
173
178
|
? ""
|
|
@@ -184,7 +189,7 @@ export class GoalCommandController {
|
|
|
184
189
|
async addGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
|
|
185
190
|
const validationError = validateObjective(objective);
|
|
186
191
|
if (validationError) {
|
|
187
|
-
ctx.ui
|
|
192
|
+
notifyTerminal(ctx.ui, validationError, "warning");
|
|
188
193
|
return;
|
|
189
194
|
}
|
|
190
195
|
if (!this.runtime.activeGoal) {
|
|
@@ -196,7 +201,8 @@ export class GoalCommandController {
|
|
|
196
201
|
createQueuedGoal(objective, tokenBudget),
|
|
197
202
|
);
|
|
198
203
|
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
199
|
-
|
|
204
|
+
notifyTerminal(
|
|
205
|
+
ctx.ui,
|
|
200
206
|
`Goal added at position ${this.runtime.queuedGoals.length + 1}: ${objective}`,
|
|
201
207
|
"info",
|
|
202
208
|
);
|
|
@@ -205,7 +211,7 @@ export class GoalCommandController {
|
|
|
205
211
|
async prioritizeGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
|
|
206
212
|
const validationError = validateObjective(objective);
|
|
207
213
|
if (validationError) {
|
|
208
|
-
ctx.ui
|
|
214
|
+
notifyTerminal(ctx.ui, validationError, "warning");
|
|
209
215
|
return;
|
|
210
216
|
}
|
|
211
217
|
if (!this.runtime.activeGoal) {
|
|
@@ -216,7 +222,7 @@ export class GoalCommandController {
|
|
|
216
222
|
this.runtime.pendingQueueAction = { kind: "prioritize", objective, tokenBudget };
|
|
217
223
|
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
218
224
|
if (ctx.isIdle?.() !== true || hasPendingMessages(ctx)) {
|
|
219
|
-
ctx.ui
|
|
225
|
+
notifyTerminal(ctx.ui, `Priority goal queued until Pi settles: ${objective}`, "info");
|
|
220
226
|
return;
|
|
221
227
|
}
|
|
222
228
|
await this.dispatchPendingQueueActionIfSettled(ctx);
|
|
@@ -225,29 +231,33 @@ export class GoalCommandController {
|
|
|
225
231
|
dropLastGoal(ctx: StatusContext) {
|
|
226
232
|
const currentGoal = this.runtime.activeGoal;
|
|
227
233
|
if (!currentGoal) {
|
|
228
|
-
ctx.ui
|
|
234
|
+
notifyTerminal(ctx.ui, "No goals to drop.", "info");
|
|
229
235
|
return;
|
|
230
236
|
}
|
|
231
237
|
const result = dropLastQueuedGoal(currentGoal, this.runtime.queuedGoals);
|
|
232
238
|
if (!result.goal) {
|
|
233
239
|
this.runtime.clearActiveGoal(ctx);
|
|
234
|
-
|
|
240
|
+
notifyTerminal(
|
|
241
|
+
ctx.ui,
|
|
242
|
+
`Goal dropped: ${result.removed?.text ?? currentGoal.text}`,
|
|
243
|
+
"warning",
|
|
244
|
+
);
|
|
235
245
|
return;
|
|
236
246
|
}
|
|
237
247
|
this.runtime.queuedGoals = result.queue;
|
|
238
248
|
this.runtime.persistGoal(result.goal);
|
|
239
|
-
ctx.ui
|
|
249
|
+
notifyTerminal(ctx.ui, `Goal dropped: ${result.removed?.text ?? "unknown goal"}`, "warning");
|
|
240
250
|
}
|
|
241
251
|
|
|
242
252
|
async skipGoal(ctx: StatusContext) {
|
|
243
253
|
const currentGoal = this.runtime.activeGoal;
|
|
244
254
|
if (!currentGoal) {
|
|
245
|
-
ctx.ui
|
|
255
|
+
notifyTerminal(ctx.ui, "No goals to skip.", "info");
|
|
246
256
|
return;
|
|
247
257
|
}
|
|
248
258
|
if (this.runtime.queuedGoals.length === 0) {
|
|
249
259
|
this.runtime.clearActiveGoal(ctx);
|
|
250
|
-
ctx.ui
|
|
260
|
+
notifyTerminal(ctx.ui, `Goal skipped: ${currentGoal.text}. No goals remain.`, "warning");
|
|
251
261
|
return;
|
|
252
262
|
}
|
|
253
263
|
if (currentGoal.status === "active") this.runtime.recordGoalUsage(currentGoal, ctx);
|
|
@@ -262,7 +272,7 @@ export class GoalCommandController {
|
|
|
262
272
|
completedText: currentGoal.text,
|
|
263
273
|
};
|
|
264
274
|
this.runtime.persistGoal(currentGoal);
|
|
265
|
-
ctx.ui
|
|
275
|
+
notifyTerminal(ctx.ui, `Goal skip queued until Pi settles: ${currentGoal.text}`, "info");
|
|
266
276
|
if (ctx.isIdle?.() === true && !hasPendingMessages(ctx)) {
|
|
267
277
|
await this.dispatchPendingQueueActionIfSettled(ctx);
|
|
268
278
|
}
|
|
@@ -334,7 +344,8 @@ export class GoalCommandController {
|
|
|
334
344
|
: undefined;
|
|
335
345
|
if (!this.runtime.activeGoal) {
|
|
336
346
|
this.runtime.clearActiveGoal(ctx);
|
|
337
|
-
|
|
347
|
+
notifyTerminal(
|
|
348
|
+
ctx.ui,
|
|
338
349
|
reason === "complete"
|
|
339
350
|
? `Goal complete: ${previousText}. No goals remain.`
|
|
340
351
|
: `Goal skipped: ${previousText}. No goals remain.`,
|
|
@@ -349,7 +360,8 @@ export class GoalCommandController {
|
|
|
349
360
|
if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
|
|
350
361
|
this.runtime.blockStaleGoalToolCalls();
|
|
351
362
|
}
|
|
352
|
-
|
|
363
|
+
notifyTerminal(
|
|
364
|
+
ctx.ui,
|
|
353
365
|
`${reason === "complete" ? "Goal complete" : "Goal skipped"}: ${previousText}. Next goal remains ${this.runtime.activeGoal.status}: ${this.runtime.activeGoal.text}`,
|
|
354
366
|
"info",
|
|
355
367
|
);
|
|
@@ -365,7 +377,7 @@ export class GoalCommandController {
|
|
|
365
377
|
restoreGoal: this.runtime.activeGoal,
|
|
366
378
|
abortTurn: false,
|
|
367
379
|
});
|
|
368
|
-
ctx.ui
|
|
380
|
+
notifyTerminal(ctx.ui, `Cannot start the next /goal: ${formatError(error)}`, "error");
|
|
369
381
|
return false;
|
|
370
382
|
}
|
|
371
383
|
const activatedGoal = this.runtime.activeGoal;
|
|
@@ -382,13 +394,15 @@ export class GoalCommandController {
|
|
|
382
394
|
restoreGoal: activatedGoal,
|
|
383
395
|
abortTurn: false,
|
|
384
396
|
});
|
|
385
|
-
|
|
397
|
+
notifyTerminal(
|
|
398
|
+
ctx.ui,
|
|
386
399
|
`Next goal paused after prompt delivery failed: ${activatedGoal.text}`,
|
|
387
400
|
"warning",
|
|
388
401
|
);
|
|
389
402
|
return false;
|
|
390
403
|
}
|
|
391
|
-
|
|
404
|
+
notifyTerminal(
|
|
405
|
+
ctx.ui,
|
|
392
406
|
`${reason === "complete" ? "Goal complete" : "Goal skipped"}: ${previousText}. Started next goal: ${activatedGoal.text}`,
|
|
393
407
|
"info",
|
|
394
408
|
);
|
|
@@ -396,7 +410,8 @@ export class GoalCommandController {
|
|
|
396
410
|
}
|
|
397
411
|
|
|
398
412
|
notifyFrozenQueue(ctx: StatusContext) {
|
|
399
|
-
|
|
413
|
+
notifyTerminal(
|
|
414
|
+
ctx.ui,
|
|
400
415
|
"The experimental goal queue is frozen. Re-enable experimental.goals in pi-goal.json and run /reload, or use /goal clear.",
|
|
401
416
|
"warning",
|
|
402
417
|
);
|
|
@@ -404,11 +419,12 @@ export class GoalCommandController {
|
|
|
404
419
|
|
|
405
420
|
pauseGoal(ctx: StatusContext) {
|
|
406
421
|
if (!this.runtime.activeGoal) {
|
|
407
|
-
ctx.ui
|
|
422
|
+
notifyTerminal(ctx.ui, "No active goal.", "info");
|
|
408
423
|
return;
|
|
409
424
|
}
|
|
410
425
|
if (this.runtime.activeGoal.status !== "active") {
|
|
411
|
-
|
|
426
|
+
notifyTerminal(
|
|
427
|
+
ctx.ui,
|
|
412
428
|
`Goal is ${this.runtime.activeGoal.status}; only active goals can be paused.`,
|
|
413
429
|
"warning",
|
|
414
430
|
);
|
|
@@ -418,16 +434,17 @@ export class GoalCommandController {
|
|
|
418
434
|
kind: "explicit_pause",
|
|
419
435
|
expectedGoalId: this.runtime.activeGoal.id,
|
|
420
436
|
});
|
|
421
|
-
if (stoppedGoal) ctx.ui
|
|
437
|
+
if (stoppedGoal) notifyTerminal(ctx.ui, `Goal paused: ${stoppedGoal.text}`, "info");
|
|
422
438
|
}
|
|
423
439
|
|
|
424
440
|
async resumeGoal(ctx: StatusContext) {
|
|
425
441
|
if (!this.runtime.activeGoal) {
|
|
426
|
-
ctx.ui
|
|
442
|
+
notifyTerminal(ctx.ui, "No active goal.", "info");
|
|
427
443
|
return;
|
|
428
444
|
}
|
|
429
445
|
if (!isResumableGoalStatus(this.runtime.activeGoal.status)) {
|
|
430
|
-
|
|
446
|
+
notifyTerminal(
|
|
447
|
+
ctx.ui,
|
|
431
448
|
`Goal is ${this.runtime.activeGoal.status}; only paused, blocked, usage-limited, or budget-limited goals can be resumed.`,
|
|
432
449
|
"warning",
|
|
433
450
|
);
|
|
@@ -437,7 +454,8 @@ export class GoalCommandController {
|
|
|
437
454
|
this.runtime.activeGoal.tokenBudget !== undefined &&
|
|
438
455
|
this.runtime.activeGoal.tokensUsed >= this.runtime.activeGoal.tokenBudget
|
|
439
456
|
) {
|
|
440
|
-
|
|
457
|
+
notifyTerminal(
|
|
458
|
+
ctx.ui,
|
|
441
459
|
`Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`,
|
|
442
460
|
"warning",
|
|
443
461
|
);
|
|
@@ -447,7 +465,7 @@ export class GoalCommandController {
|
|
|
447
465
|
try {
|
|
448
466
|
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
449
467
|
} catch (error) {
|
|
450
|
-
ctx.ui
|
|
468
|
+
notifyTerminal(ctx.ui, `Cannot resume /goal: ${formatError(error)}`, "error");
|
|
451
469
|
return;
|
|
452
470
|
}
|
|
453
471
|
const stoppedGoal = this.runtime.activeGoal;
|
|
@@ -462,7 +480,8 @@ export class GoalCommandController {
|
|
|
462
480
|
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
463
481
|
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
464
482
|
if (this.runtime.activeGoal.status !== "active") {
|
|
465
|
-
|
|
483
|
+
notifyTerminal(
|
|
484
|
+
ctx.ui,
|
|
466
485
|
`Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`,
|
|
467
486
|
"warning",
|
|
468
487
|
);
|
|
@@ -490,7 +509,8 @@ export class GoalCommandController {
|
|
|
490
509
|
return;
|
|
491
510
|
}
|
|
492
511
|
const automaticLimit = this.runtime.settings.continuationLimits.automaticTurns;
|
|
493
|
-
|
|
512
|
+
notifyTerminal(
|
|
513
|
+
ctx.ui,
|
|
494
514
|
`Goal resumed from ${stoppedStatusLabel(stoppedStatus)}: ${resumedGoal.text}. ${
|
|
495
515
|
automaticLimit === null
|
|
496
516
|
? "Automatic work remains Unlimited; goal progress and cumulative usage are preserved."
|
|
@@ -502,7 +522,7 @@ export class GoalCommandController {
|
|
|
502
522
|
|
|
503
523
|
clearGoal(ctx: StatusContext) {
|
|
504
524
|
if (!this.runtime.activeGoal) {
|
|
505
|
-
ctx.ui
|
|
525
|
+
notifyTerminal(ctx.ui, "No active goal.", "info");
|
|
506
526
|
this.runtime.cancelContinuationWork();
|
|
507
527
|
this.runtime.clearGoalRecovery();
|
|
508
528
|
this.runtime.clearBudgetWrapUp();
|
|
@@ -514,17 +534,17 @@ export class GoalCommandController {
|
|
|
514
534
|
|
|
515
535
|
const stoppedGoal = this.runtime.activeGoal.text;
|
|
516
536
|
this.runtime.clearActiveGoal(ctx);
|
|
517
|
-
ctx.ui
|
|
537
|
+
notifyTerminal(ctx.ui, `Goal cleared: ${stoppedGoal}`, "warning");
|
|
518
538
|
}
|
|
519
539
|
|
|
520
540
|
async editGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
|
|
521
541
|
const validationError = validateObjective(objective);
|
|
522
542
|
if (validationError) {
|
|
523
|
-
ctx.ui
|
|
543
|
+
notifyTerminal(ctx.ui, validationError, "warning");
|
|
524
544
|
return;
|
|
525
545
|
}
|
|
526
546
|
if (!this.runtime.activeGoal) {
|
|
527
|
-
ctx.ui
|
|
547
|
+
notifyTerminal(ctx.ui, "No active goal. Use /goal <objective> to start one.", "warning");
|
|
528
548
|
return;
|
|
529
549
|
}
|
|
530
550
|
|
|
@@ -553,7 +573,7 @@ export class GoalCommandController {
|
|
|
553
573
|
try {
|
|
554
574
|
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
555
575
|
} catch (error) {
|
|
556
|
-
ctx.ui
|
|
576
|
+
notifyTerminal(ctx.ui, `Cannot reactivate /goal: ${formatError(error)}`, "error");
|
|
557
577
|
if (this.runtime.activeGoal?.status === "active") {
|
|
558
578
|
this.runtime.pauseGoalForUnavailableTools(ctx);
|
|
559
579
|
}
|
|
@@ -602,7 +622,7 @@ export class GoalCommandController {
|
|
|
602
622
|
} else {
|
|
603
623
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
604
624
|
}
|
|
605
|
-
ctx.ui
|
|
625
|
+
notifyTerminal(ctx.ui, `Goal updated: ${objective}`, "info");
|
|
606
626
|
}
|
|
607
627
|
|
|
608
628
|
showGoal(ctx: StatusContext) {
|
|
@@ -636,7 +656,7 @@ export class GoalCommandController {
|
|
|
636
656
|
`/goal status is unavailable in ${ctx.mode} mode because Pi does not expose an extension-command output channel. Use TUI or RPC mode.`,
|
|
637
657
|
);
|
|
638
658
|
}
|
|
639
|
-
ctx.ui
|
|
659
|
+
notifyTerminal(ctx.ui, message, "info");
|
|
640
660
|
}
|
|
641
661
|
|
|
642
662
|
private async activatePrioritizedGoal(
|
|
@@ -659,7 +679,7 @@ export class GoalCommandController {
|
|
|
659
679
|
try {
|
|
660
680
|
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
661
681
|
} catch (error) {
|
|
662
|
-
ctx.ui
|
|
682
|
+
notifyTerminal(ctx.ui, `Cannot prioritize /goal: ${formatError(error)}`, "error");
|
|
663
683
|
if (currentGoal.status === "complete") {
|
|
664
684
|
// Completion already committed, so retain the priority intent for a
|
|
665
685
|
// later /reload after the tool policy is restored.
|
|
@@ -725,7 +745,7 @@ export class GoalCommandController {
|
|
|
725
745
|
this.runtime.toolPolicy.restore(visibilityBeforeActivation);
|
|
726
746
|
return false;
|
|
727
747
|
}
|
|
728
|
-
ctx.ui
|
|
748
|
+
notifyTerminal(ctx.ui, `Goal prioritized: ${objective}`, "info");
|
|
729
749
|
return true;
|
|
730
750
|
}
|
|
731
751
|
}
|
package/src/errors.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type AssistantMessage as PiAssistantMessage,
|
|
5
5
|
type Usage,
|
|
6
6
|
} from "@earendil-works/pi-ai";
|
|
7
|
+
import { stripTerminalSequences } from "@earendil-works/pi-tui";
|
|
7
8
|
import { assistantUsageTokens, nonNegativeFiniteNumber } from "./accounting.js";
|
|
8
9
|
|
|
9
10
|
export type AgentStopReason = "stop" | "length" | "toolUse" | "error" | "aborted";
|
|
@@ -36,12 +37,40 @@ const RETRYABLE_GOAL_ERROR_PATTERNS = [
|
|
|
36
37
|
/context[_\s-]*length[_\s-]*exceeded|input exceeds the context window/i,
|
|
37
38
|
] as const;
|
|
38
39
|
|
|
40
|
+
export function safeTerminalText(value: string) {
|
|
41
|
+
return [...stripTerminalSequences(value)]
|
|
42
|
+
.map((character) => {
|
|
43
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
44
|
+
if (character === "\n") return character;
|
|
45
|
+
return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? " " : character;
|
|
46
|
+
})
|
|
47
|
+
.join("")
|
|
48
|
+
.trim();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function safeGoalMenuText(value: string, maxCharacters = 120) {
|
|
52
|
+
const sanitized = safeTerminalText(value).replace(/\s+/gu, " ").trim();
|
|
53
|
+
const characters = [...sanitized];
|
|
54
|
+
return characters.length <= maxCharacters
|
|
55
|
+
? sanitized
|
|
56
|
+
: `${characters.slice(0, maxCharacters).join("")}…`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function notifyTerminal(
|
|
60
|
+
ui: { notify: (message: string, level?: "info" | "warning" | "error") => void },
|
|
61
|
+
message: string,
|
|
62
|
+
level?: "info" | "warning" | "error",
|
|
63
|
+
) {
|
|
64
|
+
ui.notify(safeTerminalText(message), level);
|
|
65
|
+
}
|
|
66
|
+
|
|
39
67
|
export function formatError(error: unknown) {
|
|
40
68
|
return truncateNotification(error instanceof Error ? error.message : String(error));
|
|
41
69
|
}
|
|
42
70
|
|
|
43
71
|
export function truncateNotification(value: string) {
|
|
44
|
-
|
|
72
|
+
const safe = [...safeTerminalText(value)];
|
|
73
|
+
return safe.length > 160 ? `${safe.slice(0, 157).join("")}...` : safe.join("");
|
|
45
74
|
}
|
|
46
75
|
|
|
47
76
|
export function isUsageLimitedGoalInterruption(assistant: AssistantMessageLike) {
|
package/src/lifecycle.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { currentTokenTotal } from "./accounting.js";
|
|
3
3
|
import type { GoalCommandController } from "./commands.js";
|
|
4
|
+
import { notifyTerminal } from "./errors.js";
|
|
4
5
|
import { type ActiveGoal, loadGoalStateFromSession } from "./persistence.js";
|
|
5
6
|
import { buildGoalPrompt, buildGoalSystemPrompt } from "./prompts.js";
|
|
6
7
|
import { activateQueuedGoal } from "./queue.js";
|
|
@@ -59,13 +60,14 @@ export function registerGoalLifecycle(
|
|
|
59
60
|
settingsResult.kind === "loaded" ? settingsResult.settings : DEFAULT_GOAL_SETTINGS;
|
|
60
61
|
runtime.settingsLoadIssue = settingsResult.kind === "invalid" ? settingsResult : undefined;
|
|
61
62
|
if (settingsResult.kind === "invalid") {
|
|
62
|
-
|
|
63
|
+
notifyTerminal(
|
|
64
|
+
ctx.ui,
|
|
63
65
|
`pi-goal settings ignored: ${settingsResult.reason}. Using default settings.`,
|
|
64
66
|
"warning",
|
|
65
67
|
);
|
|
66
68
|
}
|
|
67
69
|
if (runtime.settings.experimental.goals) {
|
|
68
|
-
ctx.ui
|
|
70
|
+
notifyTerminal(ctx.ui, EXPERIMENTAL_GOALS_WARNING, "warning");
|
|
69
71
|
}
|
|
70
72
|
try {
|
|
71
73
|
runtime.toolPolicy.prepareSessionStart(
|
|
@@ -73,7 +75,11 @@ export function registerGoalLifecycle(
|
|
|
73
75
|
previousToolVisibility,
|
|
74
76
|
);
|
|
75
77
|
} catch (error) {
|
|
76
|
-
|
|
78
|
+
notifyTerminal(
|
|
79
|
+
ctx.ui,
|
|
80
|
+
`Could not restore always-visible goal tools: ${formatError(error)}`,
|
|
81
|
+
"error",
|
|
82
|
+
);
|
|
77
83
|
}
|
|
78
84
|
|
|
79
85
|
const loaded = loadGoalStateFromSession(ctx);
|
|
@@ -85,7 +91,8 @@ export function registerGoalLifecycle(
|
|
|
85
91
|
if (runtime.queueFrozen) {
|
|
86
92
|
if (runtime.activeGoal) runtime.persistGoal(runtime.activeGoal);
|
|
87
93
|
ctx.ui.setStatus(STATUS_KEY, "queue off");
|
|
88
|
-
|
|
94
|
+
notifyTerminal(
|
|
95
|
+
ctx.ui,
|
|
89
96
|
"An experimental goal queue is frozen because experimental.goals is disabled. Re-enable it and run /reload to continue, or use /goal clear.",
|
|
90
97
|
"warning",
|
|
91
98
|
);
|
|
@@ -213,10 +220,11 @@ export function registerGoalLifecycle(
|
|
|
213
220
|
if (wasPiRetry) return;
|
|
214
221
|
runtime.clearGoalRecoveryForGoal(runtime.activeGoal.id);
|
|
215
222
|
runtime.requestContinuation(runtime.activeGoal);
|
|
216
|
-
//
|
|
217
|
-
//
|
|
218
|
-
//
|
|
219
|
-
|
|
223
|
+
// Pi emits session_compact before it clears its manual-compaction controller,
|
|
224
|
+
// so sendUserMessage still rejects inside this hook even when ctx reports idle.
|
|
225
|
+
// Defer one task; threshold compaction retains the intent for agent_settled
|
|
226
|
+
// when Pi is still busy.
|
|
227
|
+
runtime.scheduleContinuationDispatch(ctx, runtime.activeGoal.id);
|
|
220
228
|
});
|
|
221
229
|
|
|
222
230
|
pi.on("input", (event, ctx) => {
|
|
@@ -641,20 +649,23 @@ export function registerGoalLifecycle(
|
|
|
641
649
|
? ` (${truncateNotification(assistant.errorMessage)})`
|
|
642
650
|
: "";
|
|
643
651
|
if (status === "paused") {
|
|
644
|
-
|
|
652
|
+
notifyTerminal(
|
|
653
|
+
ctx.ui,
|
|
645
654
|
`Goal paused after interruption${details}. Run /goal resume to continue.`,
|
|
646
655
|
"warning",
|
|
647
656
|
);
|
|
648
657
|
return;
|
|
649
658
|
}
|
|
650
659
|
if (status === "usage_limited") {
|
|
651
|
-
|
|
660
|
+
notifyTerminal(
|
|
661
|
+
ctx.ui,
|
|
652
662
|
`Goal stopped after provider usage limit${details}. Run /goal resume when usage is available.`,
|
|
653
663
|
"warning",
|
|
654
664
|
);
|
|
655
665
|
return;
|
|
656
666
|
}
|
|
657
|
-
|
|
667
|
+
notifyTerminal(
|
|
668
|
+
ctx.ui,
|
|
658
669
|
`Goal blocked after agent error${details}. Resolve the blocker or run /goal resume to retry.`,
|
|
659
670
|
"warning",
|
|
660
671
|
);
|