@narumitw/pi-goal 0.54.4 → 0.54.5
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 +41 -166
- package/dist/chunks/{chunk-QWIUWKRW.js → chunk-VJ5LR26S.js} +57 -77
- package/dist/chunks/chunk-VJ5LR26S.js.map +7 -0
- package/dist/chunks/{chunk-UQRRW4CY.js → chunk-WQIA35U2.js} +6 -5
- package/dist/chunks/chunk-WQIA35U2.js.map +7 -0
- package/dist/chunks/{menu-WEFDREGC.js → menu-JMGWQZIT.js} +7 -22
- package/dist/chunks/menu-JMGWQZIT.js.map +7 -0
- package/dist/chunks/{settings-ui-MVYTTBHW.js → settings-ui-JG3PFRB5.js} +6 -18
- package/dist/chunks/settings-ui-JG3PFRB5.js.map +7 -0
- package/dist/index.ts +92 -126
- package/dist/index.ts.map +2 -2
- package/docs/managing-goals.md +47 -0
- package/docs/settings.md +91 -0
- package/package.json +55 -55
- package/src/accounting.ts +62 -66
- package/src/command-registration.ts +104 -102
- package/src/command.ts +101 -112
- package/src/commands.ts +465 -501
- package/src/errors.ts +127 -122
- package/src/goal-contract.ts +64 -64
- package/src/goal.ts +25 -25
- package/src/lifecycle.ts +615 -628
- package/src/markers.ts +6 -6
- package/src/menu.ts +565 -591
- package/src/persistence.ts +177 -192
- package/src/prompts.ts +53 -67
- package/src/run-protocol.ts +299 -325
- package/src/runtime.ts +1422 -1495
- package/src/safety.ts +51 -51
- package/src/settings-ui.ts +366 -393
- package/src/settings.ts +134 -147
- package/src/tool-policy.ts +6 -6
- package/src/tools.ts +360 -371
- package/src/wait.ts +47 -51
- package/src/workflow-mutex.ts +68 -68
- package/dist/chunks/chunk-QWIUWKRW.js.map +0 -7
- package/dist/chunks/chunk-UQRRW4CY.js.map +0 -7
- package/dist/chunks/menu-WEFDREGC.js.map +0 -7
- package/dist/chunks/settings-ui-MVYTTBHW.js.map +0 -7
package/src/commands.ts
CHANGED
|
@@ -3,529 +3,493 @@ import { validateObjective } from "./command.js";
|
|
|
3
3
|
import { notifyTerminal, safeGoalMenuText } from "./errors.js";
|
|
4
4
|
import type { ActiveGoal } from "./persistence.js";
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
buildGoalPrompt,
|
|
7
|
+
buildObjectiveUpdatedPrompt,
|
|
8
|
+
buildResumePrompt,
|
|
9
|
+
buildWaitingResumePrompt,
|
|
10
10
|
} from "./prompts.js";
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
blocksStaleGoalToolCalls,
|
|
13
|
+
createGoal,
|
|
14
|
+
editedGoalStatus,
|
|
15
|
+
formatBudget,
|
|
16
|
+
formatError,
|
|
17
|
+
type GoalRuntime,
|
|
18
|
+
goalSummary,
|
|
19
|
+
isResumableGoalStatus,
|
|
20
|
+
nextGoalInstance,
|
|
21
|
+
queueGoalSafetyReset,
|
|
22
|
+
STATUS_KEY,
|
|
23
|
+
type StatusContext,
|
|
24
|
+
stoppedStatusLabel,
|
|
25
|
+
transitionGoal,
|
|
26
26
|
} from "./runtime.js";
|
|
27
27
|
|
|
28
28
|
// User-command mutations are kept separate from Pi event wiring. Every controller
|
|
29
29
|
// receives exactly one per-factory GoalRuntime, preserving session isolation.
|
|
30
30
|
export class GoalCommandController {
|
|
31
|
-
|
|
31
|
+
private readonly runtime: GoalRuntime;
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
constructor(runtime: GoalRuntime) {
|
|
34
|
+
this.runtime = runtime;
|
|
35
|
+
}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
37
|
+
async startGoal(
|
|
38
|
+
objective: string,
|
|
39
|
+
tokenBudget: number | undefined,
|
|
40
|
+
ctx: StatusContext,
|
|
41
|
+
onActivated?: (goal: ActiveGoal) => void,
|
|
42
|
+
isActivationCurrent?: (goal: ActiveGoal) => boolean,
|
|
43
|
+
isRequestCurrent?: () => boolean,
|
|
44
|
+
) {
|
|
45
|
+
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
46
|
+
const validationError = validateObjective(objective);
|
|
47
|
+
if (validationError) {
|
|
48
|
+
notifyTerminal(ctx.ui, validationError, "warning");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"warning",
|
|
74
|
-
);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
52
|
+
const existingGoal = this.runtime.activeGoal?.status !== "complete" ? this.runtime.activeGoal : undefined;
|
|
53
|
+
const legacyQueueBeforeActivation = existingGoal
|
|
54
|
+
? undefined
|
|
55
|
+
: this.runtime.legacyQueueState
|
|
56
|
+
? structuredClone(this.runtime.legacyQueueState)
|
|
57
|
+
: undefined;
|
|
58
|
+
if (existingGoal) {
|
|
59
|
+
const shouldReplace = await ctx.ui.confirm(
|
|
60
|
+
"Replace goal?",
|
|
61
|
+
`Current goal: ${safeGoalMenuText(existingGoal.text, 4_000)}\n\nNew goal: ${safeGoalMenuText(objective, 4_000)}`,
|
|
62
|
+
);
|
|
63
|
+
if (!shouldReplace) {
|
|
64
|
+
notifyTerminal(ctx.ui, `Goal kept: ${existingGoal.text}`, "info");
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
68
|
+
if (this.runtime.activeGoal?.id !== existingGoal.id) {
|
|
69
|
+
notifyTerminal(ctx.ui, "The active goal changed while confirmation was open. Try again.", "warning");
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
78
73
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
74
|
+
// Tool registration keeps the Goal schema stable. A missing tool means another
|
|
75
|
+
// policy or allowlist intentionally removed it, so activation must not widen it.
|
|
76
|
+
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
77
|
+
const retainedOwner = this.runtime.ownsWorkflow(existingGoal);
|
|
78
|
+
if (!this.runtime.acquireWorkflow(ctx.sessionManager)) return this.reportWorkflowBusy(ctx);
|
|
79
|
+
const acquiredForRequest = !retainedOwner;
|
|
80
|
+
try {
|
|
81
|
+
this.runtime.assertGoalToolsAvailable();
|
|
82
|
+
} catch (error) {
|
|
83
|
+
notifyTerminal(ctx.ui, `Cannot start /goal: ${formatError(error)}`, "error");
|
|
84
|
+
if (existingGoal?.status === "active") this.runtime.pauseGoalForUnavailableTools(ctx);
|
|
85
|
+
else if (acquiredForRequest) this.runtime.releaseWorkflow();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
93
88
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
? "Automatic work is Unlimited; tool loops may consume substantial tokens and provider cost. Open /goal to monitor."
|
|
192
|
-
: `Automatic work pauses after ${automaticLimit} responses; open /goal to monitor progress.`
|
|
193
|
-
}`,
|
|
194
|
-
automaticLimit === null ? "warning" : "info",
|
|
195
|
-
);
|
|
196
|
-
}
|
|
89
|
+
this.runtime.clearGoalWaitTimer();
|
|
90
|
+
this.runtime.cancelContinuationWork();
|
|
91
|
+
this.runtime.clearGoalRecovery();
|
|
92
|
+
this.runtime.clearBudgetWrapUp();
|
|
93
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
94
|
+
if (!legacyQueueBeforeActivation) this.runtime.legacyQueueState = undefined;
|
|
95
|
+
this.runtime.activeGoal = createGoal(objective, tokenBudget, currentTokenTotal(ctx));
|
|
96
|
+
const startedGoal = this.runtime.activeGoal;
|
|
97
|
+
onActivated?.(startedGoal);
|
|
98
|
+
if (!legacyQueueBeforeActivation) this.runtime.persistGoal(startedGoal);
|
|
99
|
+
if (this.runtime.activeGoal?.id !== startedGoal.id || !this.runtime.ownsWorkflow(this.runtime.activeGoal)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.runtime.updateStatus(ctx, startedGoal);
|
|
103
|
+
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
104
|
+
ctx,
|
|
105
|
+
startedGoal.id,
|
|
106
|
+
buildGoalPrompt(startedGoal),
|
|
107
|
+
true,
|
|
108
|
+
() => (isRequestCurrent?.() ?? true) && (isActivationCurrent?.(startedGoal) ?? true),
|
|
109
|
+
);
|
|
110
|
+
if (
|
|
111
|
+
sent &&
|
|
112
|
+
legacyQueueBeforeActivation &&
|
|
113
|
+
this.runtime.activeGoal?.id === startedGoal.id &&
|
|
114
|
+
this.runtime.activeGoal.status === "active"
|
|
115
|
+
) {
|
|
116
|
+
this.runtime.legacyQueueState = undefined;
|
|
117
|
+
this.runtime.persistGoal(startedGoal);
|
|
118
|
+
}
|
|
119
|
+
if (isActivationCurrent && !isActivationCurrent(startedGoal)) return;
|
|
120
|
+
if (!sent) {
|
|
121
|
+
let rolledBackStartedGoal = false;
|
|
122
|
+
if (this.runtime.activeGoal?.id === startedGoal.id) {
|
|
123
|
+
rolledBackStartedGoal = true;
|
|
124
|
+
if (existingGoal) {
|
|
125
|
+
this.runtime.recordGoalUsage(existingGoal, ctx);
|
|
126
|
+
if (existingGoal.status === "active" && existingGoal.waiting) {
|
|
127
|
+
this.runtime.activeGoal = existingGoal;
|
|
128
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
129
|
+
this.runtime.persistGoal(existingGoal);
|
|
130
|
+
this.runtime.updateStatus(ctx, existingGoal);
|
|
131
|
+
this.runtime.restoreGoalWaitTimer(ctx);
|
|
132
|
+
} else if (existingGoal.status === "active") {
|
|
133
|
+
this.runtime.activeGoal = existingGoal;
|
|
134
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
135
|
+
this.runtime.persistGoal(existingGoal);
|
|
136
|
+
if (this.runtime.activeGoal?.id === existingGoal.id) {
|
|
137
|
+
this.runtime.updateStatus(ctx, existingGoal);
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
this.runtime.activeGoal = existingGoal;
|
|
141
|
+
if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
|
|
142
|
+
this.runtime.blockStaleGoalToolCalls();
|
|
143
|
+
} else {
|
|
144
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
145
|
+
}
|
|
146
|
+
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
147
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
148
|
+
}
|
|
149
|
+
} else if (legacyQueueBeforeActivation) {
|
|
150
|
+
this.runtime.activeGoal = undefined;
|
|
151
|
+
this.runtime.legacyQueueState = legacyQueueBeforeActivation;
|
|
152
|
+
this.runtime.cancelContinuationWork();
|
|
153
|
+
this.runtime.clearGoalRecovery();
|
|
154
|
+
this.runtime.clearBudgetWrapUp();
|
|
155
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
156
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
157
|
+
} else {
|
|
158
|
+
this.runtime.clearActiveGoal(ctx, "goal activation rolled back", false);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (rolledBackStartedGoal) {
|
|
162
|
+
if (acquiredForRequest && !this.runtime.ownsWorkflow(existingGoal)) {
|
|
163
|
+
this.runtime.releaseWorkflow();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (this.runtime.activeGoal?.id !== startedGoal.id || !this.runtime.ownsWorkflow(this.runtime.activeGoal)) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const automaticLimit = this.runtime.settings.continuationLimits.automaticTurns;
|
|
172
|
+
notifyTerminal(
|
|
173
|
+
ctx.ui,
|
|
174
|
+
`${existingGoal ? "Goal replaced" : "Goal started"}: ${objective}. ${
|
|
175
|
+
startedGoal.tokenBudget === undefined
|
|
176
|
+
? ""
|
|
177
|
+
: `Token budget: ${formatTokenCount(startedGoal.tokenBudget)} cumulative; the final model call may exceed it. `
|
|
178
|
+
}${
|
|
179
|
+
automaticLimit === null
|
|
180
|
+
? "Automatic work is Unlimited; tool loops may consume substantial tokens and provider cost. Open /goal to monitor."
|
|
181
|
+
: `Automatic work pauses after ${automaticLimit} responses; open /goal to monitor progress.`
|
|
182
|
+
}`,
|
|
183
|
+
automaticLimit === null ? "warning" : "info",
|
|
184
|
+
);
|
|
185
|
+
}
|
|
197
186
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
expectedGoalId: this.runtime.activeGoal.id,
|
|
214
|
-
});
|
|
215
|
-
if (stoppedGoal) notifyTerminal(ctx.ui, `Goal paused: ${stoppedGoal.text}`, "info");
|
|
216
|
-
}
|
|
187
|
+
pauseGoal(ctx: StatusContext) {
|
|
188
|
+
if (!this.runtime.activeGoal) {
|
|
189
|
+
notifyTerminal(ctx.ui, "No active goal.", "info");
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (this.runtime.activeGoal.status !== "active") {
|
|
193
|
+
notifyTerminal(ctx.ui, `Goal is ${this.runtime.activeGoal.status}; only active goals can be paused.`, "warning");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const stoppedGoal = this.runtime.stopActiveGoal(ctx, {
|
|
197
|
+
kind: "explicit_pause",
|
|
198
|
+
expectedGoalId: this.runtime.activeGoal.id,
|
|
199
|
+
});
|
|
200
|
+
if (stoppedGoal) notifyTerminal(ctx.ui, `Goal paused: ${stoppedGoal.text}`, "info");
|
|
201
|
+
}
|
|
217
202
|
|
|
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
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
-
if (!this.runtime.ownsWorkflow(resumedGoal)) return;
|
|
301
|
-
const automaticLimit = this.runtime.settings.continuationLimits.automaticTurns;
|
|
302
|
-
notifyTerminal(
|
|
303
|
-
ctx.ui,
|
|
304
|
-
`Goal resumed from ${stoppedStatusLabel(stoppedStatus)}: ${resumedGoal.text}. ${
|
|
305
|
-
automaticLimit === null
|
|
306
|
-
? "Automatic work remains Unlimited; goal progress and cumulative usage are preserved."
|
|
307
|
-
: `The automatic-work counter will reset to 0 of ${automaticLimit} when the resumed prompt starts; goal progress and cumulative usage are preserved.`
|
|
308
|
-
}`,
|
|
309
|
-
automaticLimit === null ? "warning" : "info",
|
|
310
|
-
);
|
|
311
|
-
}
|
|
203
|
+
async resumeGoal(ctx: StatusContext) {
|
|
204
|
+
if (!this.runtime.activeGoal) {
|
|
205
|
+
notifyTerminal(ctx.ui, "No active goal.", "info");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (this.runtime.activeGoal.status === "active" && this.runtime.activeGoal.waiting) {
|
|
209
|
+
await this.resumeWaitingGoal(ctx);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (!isResumableGoalStatus(this.runtime.activeGoal.status)) {
|
|
213
|
+
notifyTerminal(
|
|
214
|
+
ctx.ui,
|
|
215
|
+
`Goal is ${this.runtime.activeGoal.status}; only paused, blocked, usage-limited, or budget-limited goals can be resumed.`,
|
|
216
|
+
"warning",
|
|
217
|
+
);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (
|
|
221
|
+
this.runtime.activeGoal.tokenBudget !== undefined &&
|
|
222
|
+
this.runtime.activeGoal.tokensUsed >= this.runtime.activeGoal.tokenBudget
|
|
223
|
+
) {
|
|
224
|
+
notifyTerminal(ctx.ui, `Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`, "warning");
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (!this.runtime.acquireWorkflow(ctx.sessionManager)) {
|
|
228
|
+
this.reportWorkflowBusy(ctx);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
this.runtime.assertGoalToolsAvailable();
|
|
233
|
+
} catch (error) {
|
|
234
|
+
notifyTerminal(ctx.ui, `Cannot resume /goal: ${formatError(error)}`, "error");
|
|
235
|
+
this.runtime.releaseWorkflow();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const stoppedGoal = this.runtime.activeGoal;
|
|
239
|
+
const stoppedStatus = stoppedGoal.status;
|
|
240
|
+
this.runtime.cancelContinuationWork();
|
|
241
|
+
this.runtime.clearGoalRecovery();
|
|
242
|
+
this.runtime.clearBudgetWrapUp();
|
|
243
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
244
|
+
this.runtime.activeGoal = queueGoalSafetyReset(transitionGoal(nextGoalInstance(this.runtime.activeGoal), "active"));
|
|
245
|
+
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
246
|
+
if (this.runtime.activeGoal.status !== "active") {
|
|
247
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
248
|
+
this.runtime.releaseWorkflow();
|
|
249
|
+
notifyTerminal(ctx.ui, `Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`, "warning");
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (!this.runtime.ownsWorkflow(this.runtime.activeGoal)) return;
|
|
253
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
254
|
+
const resumedGoal = this.runtime.activeGoal;
|
|
255
|
+
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
256
|
+
ctx,
|
|
257
|
+
resumedGoal.id,
|
|
258
|
+
buildResumePrompt(resumedGoal, stoppedStatus),
|
|
259
|
+
);
|
|
260
|
+
if (!sent) {
|
|
261
|
+
if (this.runtime.activeGoal?.id === resumedGoal.id && this.runtime.activeGoal.status === "active") {
|
|
262
|
+
this.runtime.activeGoal = stoppedGoal;
|
|
263
|
+
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
264
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
265
|
+
if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
|
|
266
|
+
this.runtime.blockStaleGoalToolCalls();
|
|
267
|
+
}
|
|
268
|
+
this.runtime.releaseWorkflow();
|
|
269
|
+
}
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (!this.runtime.ownsWorkflow(resumedGoal)) return;
|
|
273
|
+
const automaticLimit = this.runtime.settings.continuationLimits.automaticTurns;
|
|
274
|
+
notifyTerminal(
|
|
275
|
+
ctx.ui,
|
|
276
|
+
`Goal resumed from ${stoppedStatusLabel(stoppedStatus)}: ${resumedGoal.text}. ${
|
|
277
|
+
automaticLimit === null
|
|
278
|
+
? "Automatic work remains Unlimited; goal progress and cumulative usage are preserved."
|
|
279
|
+
: `The automatic-work counter will reset to 0 of ${automaticLimit} when the resumed prompt starts; goal progress and cumulative usage are preserved.`
|
|
280
|
+
}`,
|
|
281
|
+
automaticLimit === null ? "warning" : "info",
|
|
282
|
+
);
|
|
283
|
+
}
|
|
312
284
|
|
|
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
|
-
}
|
|
285
|
+
private async resumeWaitingGoal(ctx: StatusContext) {
|
|
286
|
+
const waitingGoal = this.runtime.activeGoal;
|
|
287
|
+
const waiting = waitingGoal?.waiting;
|
|
288
|
+
if (waitingGoal?.status !== "active" || !waiting) return;
|
|
289
|
+
if (!this.runtime.acquireWorkflow(ctx.sessionManager)) {
|
|
290
|
+
this.reportWorkflowBusy(ctx);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
try {
|
|
294
|
+
this.runtime.assertGoalToolsAvailable();
|
|
295
|
+
} catch (error) {
|
|
296
|
+
notifyTerminal(ctx.ui, `Cannot resume /goal: ${formatError(error)}`, "error");
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (!this.runtime.clearGoalWait(ctx, waitingGoal.id)) return;
|
|
300
|
+
const resumedGoal = this.runtime.activeGoal;
|
|
301
|
+
if (!resumedGoal || resumedGoal.id !== waitingGoal.id || resumedGoal.status !== "active") return;
|
|
302
|
+
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
303
|
+
ctx,
|
|
304
|
+
resumedGoal.id,
|
|
305
|
+
buildWaitingResumePrompt(resumedGoal, waiting.reason),
|
|
306
|
+
false,
|
|
307
|
+
);
|
|
308
|
+
if (!sent) {
|
|
309
|
+
if (this.runtime.activeGoal?.id === waitingGoal.id) {
|
|
310
|
+
this.runtime.enterGoalWait(ctx, waitingGoal.id, waiting);
|
|
311
|
+
}
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (!this.runtime.ownsWorkflow(resumedGoal)) return;
|
|
315
|
+
notifyTerminal(ctx.ui, `Goal resumed from waiting: ${waitingGoal.text}`, "info");
|
|
316
|
+
}
|
|
346
317
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
318
|
+
clearGoal(ctx: StatusContext) {
|
|
319
|
+
if (!this.runtime.activeGoal) {
|
|
320
|
+
const hadLegacyQueue = this.runtime.legacyQueueState !== undefined;
|
|
321
|
+
this.runtime.legacyQueueState = undefined;
|
|
322
|
+
this.runtime.cancelContinuationWork();
|
|
323
|
+
this.runtime.clearGoalRecovery();
|
|
324
|
+
this.runtime.clearBudgetWrapUp();
|
|
325
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
326
|
+
this.runtime.clearPersistedGoal(ctx.cwd);
|
|
327
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
328
|
+
notifyTerminal(
|
|
329
|
+
ctx.ui,
|
|
330
|
+
hadLegacyQueue ? "Removed legacy ordered goal queue state." : "No active goal.",
|
|
331
|
+
hadLegacyQueue ? "warning" : "info",
|
|
332
|
+
);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
364
335
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
336
|
+
const stoppedGoal = this.runtime.activeGoal.text;
|
|
337
|
+
this.runtime.clearActiveGoal(ctx);
|
|
338
|
+
notifyTerminal(ctx.ui, `Goal cleared: ${stoppedGoal}`, "warning");
|
|
339
|
+
}
|
|
369
340
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
341
|
+
async editGoal(objective: string, tokenBudget: number | undefined, ctx: StatusContext) {
|
|
342
|
+
const validationError = validateObjective(objective);
|
|
343
|
+
if (validationError) {
|
|
344
|
+
notifyTerminal(ctx.ui, validationError, "warning");
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (!this.runtime.activeGoal) {
|
|
348
|
+
notifyTerminal(ctx.ui, "No active goal. Use /goal <objective> to start one.", "warning");
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
380
351
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
352
|
+
const currentGoal = this.runtime.activeGoal;
|
|
353
|
+
const effectiveTokenBudget = tokenBudget ?? currentGoal.tokenBudget;
|
|
354
|
+
if (
|
|
355
|
+
currentGoal.status === "budget_limited" &&
|
|
356
|
+
effectiveTokenBudget !== undefined &&
|
|
357
|
+
effectiveTokenBudget <= currentGoal.tokensUsed
|
|
358
|
+
) {
|
|
359
|
+
notifyTerminal(
|
|
360
|
+
ctx.ui,
|
|
361
|
+
`Goal token budget is still reached: ${formatBudget(currentGoal)}. Raise the budget above current usage before editing.`,
|
|
362
|
+
"warning",
|
|
363
|
+
);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const previousStatus = currentGoal.status;
|
|
367
|
+
const intendsActive = editedGoalStatus(previousStatus) === "active";
|
|
368
|
+
const retainedOwner = this.runtime.ownsWorkflow(currentGoal);
|
|
369
|
+
if (intendsActive && !this.runtime.acquireWorkflow(ctx.sessionManager)) {
|
|
370
|
+
this.reportWorkflowBusy(ctx);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const acquiredForEdit = intendsActive && !retainedOwner;
|
|
374
|
+
if (intendsActive) {
|
|
375
|
+
try {
|
|
376
|
+
this.runtime.assertGoalToolsAvailable();
|
|
377
|
+
} catch (error) {
|
|
378
|
+
notifyTerminal(ctx.ui, `Cannot reactivate /goal: ${formatError(error)}`, "error");
|
|
379
|
+
if (currentGoal.status === "active") this.runtime.pauseGoalForUnavailableTools(ctx);
|
|
380
|
+
else if (acquiredForEdit) this.runtime.releaseWorkflow();
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
413
384
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
this.runtime.clearStaleGoalToolCallBlock();
|
|
483
|
-
}
|
|
484
|
-
if (editedGoal.status !== "active" && (retainedOwner || acquiredForEdit)) {
|
|
485
|
-
this.runtime.releaseWorkflow();
|
|
486
|
-
}
|
|
487
|
-
notifyTerminal(ctx.ui, `Goal updated: ${objective}`, "info");
|
|
488
|
-
}
|
|
385
|
+
this.runtime.recordGoalUsage(currentGoal, ctx);
|
|
386
|
+
const previousGoal = { ...currentGoal };
|
|
387
|
+
this.runtime.clearGoalWaitTimer();
|
|
388
|
+
this.runtime.cancelContinuationWork();
|
|
389
|
+
this.runtime.clearGoalRecovery();
|
|
390
|
+
this.runtime.clearBudgetWrapUp();
|
|
391
|
+
const rotatedGoal = nextGoalInstance(currentGoal);
|
|
392
|
+
const transitionedGoal = transitionGoal(
|
|
393
|
+
{
|
|
394
|
+
...rotatedGoal,
|
|
395
|
+
text: objective,
|
|
396
|
+
tokenBudget: effectiveTokenBudget,
|
|
397
|
+
waiting: undefined,
|
|
398
|
+
},
|
|
399
|
+
editedGoalStatus(previousStatus),
|
|
400
|
+
);
|
|
401
|
+
const nextGoal = transitionedGoal.status === "active" ? queueGoalSafetyReset(transitionedGoal) : transitionedGoal;
|
|
402
|
+
this.runtime.activeGoal = nextGoal;
|
|
403
|
+
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
404
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
405
|
+
const editedGoal = this.runtime.activeGoal;
|
|
406
|
+
if (!editedGoal) return;
|
|
407
|
+
if (editedGoal.status === "active") {
|
|
408
|
+
if (!this.runtime.ownsWorkflow(editedGoal)) return;
|
|
409
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
410
|
+
const sent = await this.runtime.sendOwnedGoalPrompt(ctx, editedGoal.id, buildObjectiveUpdatedPrompt(editedGoal));
|
|
411
|
+
if (!sent) {
|
|
412
|
+
if (this.runtime.activeGoal?.id === editedGoal.id) {
|
|
413
|
+
if (previousStatus === "active" && previousGoal.waiting) {
|
|
414
|
+
this.runtime.activeGoal = previousGoal;
|
|
415
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
416
|
+
this.runtime.persistGoal(previousGoal);
|
|
417
|
+
this.runtime.updateStatus(ctx, previousGoal);
|
|
418
|
+
this.runtime.restoreGoalWaitTimer(ctx);
|
|
419
|
+
} else if (previousStatus === "active") {
|
|
420
|
+
this.runtime.activeGoal = previousGoal;
|
|
421
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
422
|
+
this.runtime.persistGoal(previousGoal);
|
|
423
|
+
if (this.runtime.activeGoal?.id === previousGoal.id) {
|
|
424
|
+
this.runtime.updateStatus(ctx, previousGoal);
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
this.runtime.activeGoal = previousGoal;
|
|
428
|
+
if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
|
|
429
|
+
this.runtime.blockStaleGoalToolCalls();
|
|
430
|
+
} else {
|
|
431
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
432
|
+
}
|
|
433
|
+
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
434
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
435
|
+
}
|
|
436
|
+
if (acquiredForEdit && previousStatus !== "active") {
|
|
437
|
+
this.runtime.releaseWorkflow();
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (!this.runtime.ownsWorkflow(editedGoal)) return;
|
|
443
|
+
} else if (blocksStaleGoalToolCalls(editedGoal.status)) {
|
|
444
|
+
this.runtime.blockStaleGoalToolCalls();
|
|
445
|
+
} else {
|
|
446
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
447
|
+
}
|
|
448
|
+
if (editedGoal.status !== "active" && (retainedOwner || acquiredForEdit)) {
|
|
449
|
+
this.runtime.releaseWorkflow();
|
|
450
|
+
}
|
|
451
|
+
notifyTerminal(ctx.ui, `Goal updated: ${objective}`, "info");
|
|
452
|
+
}
|
|
489
453
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
454
|
+
private reportWorkflowBusy(ctx: StatusContext) {
|
|
455
|
+
const message = "Another workflow is active in this session. End it before starting Goal.";
|
|
456
|
+
if (ctx.mode === "print" || ctx.mode === "json") throw new Error(message);
|
|
457
|
+
notifyTerminal(ctx.ui, message, "warning");
|
|
458
|
+
return { kind: "busy" as const };
|
|
459
|
+
}
|
|
496
460
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
461
|
+
showGoal(ctx: StatusContext) {
|
|
462
|
+
if (!this.runtime.activeGoal) {
|
|
463
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
464
|
+
this.reportGoalStatus(ctx, this.emptyGoalMessage());
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
this.runtime.recordGoalUsage(this.runtime.activeGoal, ctx);
|
|
468
|
+
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
469
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
470
|
+
this.reportGoalStatus(
|
|
471
|
+
ctx,
|
|
472
|
+
goalSummary(this.runtime.activeGoal, this.runtime.settings.continuationLimits.automaticTurns),
|
|
473
|
+
);
|
|
474
|
+
}
|
|
511
475
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
476
|
+
private emptyGoalMessage() {
|
|
477
|
+
const legacy = this.runtime.legacyQueueState;
|
|
478
|
+
if (!legacy) return "Usage: /goal <objective>\nNo goal is currently set.";
|
|
479
|
+
return [
|
|
480
|
+
"Ordered goal queue has been removed.",
|
|
481
|
+
`Legacy queue state with ${legacy.retainedGoals} retained ${legacy.retainedGoals === 1 ? "goal" : "goals"} will not run automatically.`,
|
|
482
|
+
"Use /goal edit to reprioritize an active objective, start /goal <objectives>, or use /goal clear to discard the old queue state.",
|
|
483
|
+
'Example objective: "task b is complete; do task a next, then task c and task d."',
|
|
484
|
+
].join("\n");
|
|
485
|
+
}
|
|
522
486
|
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
487
|
+
private reportGoalStatus(ctx: StatusContext, message: string) {
|
|
488
|
+
if (ctx.mode === "print" || ctx.mode === "json") {
|
|
489
|
+
throw new Error(
|
|
490
|
+
`/goal status is unavailable in ${ctx.mode} mode because Pi does not expose an extension-command output channel. Use TUI or RPC mode.`,
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
notifyTerminal(ctx.ui, message, "info");
|
|
494
|
+
}
|
|
531
495
|
}
|