@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/tools.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
DEFAULT_MAX_BYTES,
|
|
3
|
+
DEFAULT_MAX_LINES,
|
|
4
|
+
defineTool,
|
|
5
|
+
type ExtensionAPI,
|
|
6
|
+
getMarkdownTheme,
|
|
7
|
+
truncateHead,
|
|
8
8
|
} from "@earendil-works/pi-coding-agent";
|
|
9
9
|
import { Markdown } from "@earendil-works/pi-tui";
|
|
10
10
|
import { Type } from "typebox";
|
|
11
11
|
import { notifyTerminal, safeTerminalText } from "./errors.js";
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
formatStatus,
|
|
14
|
+
GOAL_BLOCKED_TOOL,
|
|
15
|
+
GOAL_COMPLETE_TOOL,
|
|
16
|
+
GOAL_WAIT_TOOL,
|
|
17
|
+
type GoalRuntime,
|
|
18
|
+
goalIdRejectionReason,
|
|
19
|
+
isContradictoryCompletionSummary,
|
|
20
|
+
MAX_GOAL_ID_LENGTH,
|
|
21
|
+
STATUS_KEY,
|
|
22
|
+
transitionGoal,
|
|
23
|
+
truncateNotification,
|
|
24
24
|
} from "./runtime.js";
|
|
25
25
|
import {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
createGoalWait,
|
|
27
|
+
MAX_GOAL_WAIT_DELAY_MS,
|
|
28
|
+
MAX_GOAL_WAIT_REASON_LENGTH,
|
|
29
|
+
MIN_GOAL_WAIT_DELAY_MS,
|
|
30
|
+
resolveGoalWaitDelay,
|
|
31
31
|
} from "./wait.js";
|
|
32
32
|
|
|
33
33
|
interface GoalCompleteDetails {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
goal: string;
|
|
35
|
+
goal_id: string;
|
|
36
|
+
summary: string;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
interface GoalBlockedDetails {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
goal: string;
|
|
41
|
+
goal_id: string;
|
|
42
|
+
reason: string;
|
|
43
|
+
evidence: string;
|
|
44
|
+
repeated_turns: number;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
interface GoalWaitDetails {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
goal: string;
|
|
49
|
+
goal_id: string;
|
|
50
|
+
reason: string;
|
|
51
|
+
requested_resume_after_ms?: number;
|
|
52
|
+
resume_after_ms?: number;
|
|
53
|
+
resume_at?: number;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
const MAX_GOAL_TEXT_LENGTH = 4_000;
|
|
@@ -59,380 +59,369 @@ const MAX_BLOCKER_REASON_LENGTH = 1_000;
|
|
|
59
59
|
const MAX_BLOCKER_EVIDENCE_LENGTH = 4_000;
|
|
60
60
|
|
|
61
61
|
export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
|
|
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
|
-
|
|
62
|
+
const goalCompleteTool = defineTool({
|
|
63
|
+
name: GOAL_COMPLETE_TOOL,
|
|
64
|
+
label: "Goal Complete",
|
|
65
|
+
description:
|
|
66
|
+
"Mark an active /goal complete only when the latest effective Goal contract explicitly says Goal mode is active, supplies the matching current goal_id, and every requirement is verified. Tool visibility alone does not activate Goal mode. Never call for ordinary work, partial progress, blockers, failures, or unverified work.",
|
|
67
|
+
parameters: Type.Object({
|
|
68
|
+
goal_id: Type.String({
|
|
69
|
+
minLength: 1,
|
|
70
|
+
maxLength: MAX_GOAL_ID_LENGTH,
|
|
71
|
+
description:
|
|
72
|
+
"The exact goal_id shown in the current active /goal prompt. Used only to reject stale completion calls from older turns.",
|
|
73
|
+
}),
|
|
74
|
+
summary: Type.String({
|
|
75
|
+
minLength: 1,
|
|
76
|
+
maxLength: MAX_COMPLETION_SUMMARY_LENGTH,
|
|
77
|
+
description:
|
|
78
|
+
"State what was completed and what evidence verified it. Do not use this tool to report partial progress, blockers, failures, or remaining work.",
|
|
79
|
+
}),
|
|
80
|
+
}),
|
|
81
|
+
renderResult(result) {
|
|
82
|
+
return renderGoalCompletion(result);
|
|
83
|
+
},
|
|
84
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
85
|
+
const completedGoal = runtime.activeGoal;
|
|
86
|
+
const goal = completedGoal?.text ?? "unknown goal";
|
|
87
|
+
const requestedGoalId = typeof params.goal_id === "string" ? params.goal_id.trim() : "";
|
|
88
|
+
const summary = typeof params.summary === "string" ? params.summary.trim() : "";
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
if (!completedGoal) {
|
|
91
|
+
const rejection = "Goal completion rejected: no active goal.";
|
|
92
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
93
93
|
|
|
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
|
-
|
|
94
|
+
return {
|
|
95
|
+
content: toolContent(rejection),
|
|
96
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const completingDuringBudgetWrapUp = runtime.hasActiveBudgetWrapUp();
|
|
100
|
+
if (completedGoal.status === "active" && !runtime.ownsWorkflow(completedGoal)) {
|
|
101
|
+
const rejection = "Goal completion rejected: active Goal no longer owns its workflow.";
|
|
102
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
103
|
+
return {
|
|
104
|
+
content: toolContent(rejection),
|
|
105
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (!runtime.canRecordGoalUsage() && !completingDuringBudgetWrapUp) {
|
|
109
|
+
const rejection = "Goal completion rejected: current run does not own the active goal.";
|
|
110
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
111
|
+
return {
|
|
112
|
+
content: toolContent(rejection),
|
|
113
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const staleGoalRejection = goalIdRejectionReason(completedGoal, requestedGoalId);
|
|
117
|
+
if (staleGoalRejection) {
|
|
118
|
+
const rejection = `Goal completion rejected: ${staleGoalRejection}.`;
|
|
119
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
120
|
+
if (completingDuringBudgetWrapUp) {
|
|
121
|
+
runtime.recordGoalUsage(completedGoal, ctx);
|
|
122
|
+
runtime.persistGoal(completedGoal);
|
|
123
|
+
runtime.updateStatus(ctx, completedGoal);
|
|
124
|
+
runtime.clearBudgetWrapUp();
|
|
125
|
+
}
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
127
|
+
return {
|
|
128
|
+
content: toolContent(rejection),
|
|
129
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
130
|
+
terminate: completingDuringBudgetWrapUp || undefined,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (completedGoal.status !== "active" && !completingDuringBudgetWrapUp) {
|
|
134
|
+
const rejection = `Goal completion rejected: goal is ${completedGoal.status}, not active.`;
|
|
135
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
136
136
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
137
|
+
return {
|
|
138
|
+
content: toolContent(rejection),
|
|
139
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
142
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
143
|
+
const rejectionReason = !summary
|
|
144
|
+
? "summary is empty"
|
|
145
|
+
: summary.length > MAX_COMPLETION_SUMMARY_LENGTH
|
|
146
|
+
? "summary is too long"
|
|
147
|
+
: isContradictoryCompletionSummary(summary)
|
|
148
|
+
? "summary says the goal is not complete"
|
|
149
|
+
: undefined;
|
|
150
|
+
if (rejectionReason) {
|
|
151
|
+
runtime.recordGoalUsage(completedGoal, ctx);
|
|
152
|
+
runtime.persistGoal(completedGoal);
|
|
153
|
+
runtime.updateStatus(ctx, completedGoal);
|
|
154
|
+
const rejection = `Goal completion rejected: ${rejectionReason}.`;
|
|
155
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
156
|
+
if (completingDuringBudgetWrapUp) runtime.clearBudgetWrapUp();
|
|
157
157
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
158
|
+
return {
|
|
159
|
+
content: toolContent(rejection),
|
|
160
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
161
|
+
terminate: completingDuringBudgetWrapUp || undefined,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
164
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
165
|
+
runtime.clearGoalWaitTimer();
|
|
166
|
+
runtime.activeGoal = transitionGoal(completedGoal, "complete");
|
|
167
|
+
runtime.setCompletionSummary(runtime.activeGoal.id, summary);
|
|
168
|
+
runtime.recordGoalUsage(runtime.activeGoal, ctx);
|
|
169
|
+
runtime.persistGoal(runtime.activeGoal);
|
|
170
170
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
171
|
+
ctx.ui.setStatus(STATUS_KEY, formatStatus(runtime.activeGoal));
|
|
172
|
+
runtime.clearCompletedGoal(ctx);
|
|
173
|
+
runtime.showCompletionStatus(ctx);
|
|
174
|
+
notifyTerminal(ctx.ui, `Goal complete: ${goal}`, "info");
|
|
175
175
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
176
|
+
return {
|
|
177
|
+
content: toolContent(`Goal complete: ${summary}`),
|
|
178
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
179
|
+
terminate: true,
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
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
|
-
};
|
|
184
|
+
const goalBlockedTool = defineTool({
|
|
185
|
+
name: GOAL_BLOCKED_TOOL,
|
|
186
|
+
label: "Goal Blocked",
|
|
187
|
+
description:
|
|
188
|
+
"Stop an active /goal only when the latest effective Goal contract explicitly says Goal mode is active, supplies the matching current goal_id, and the same evidenced external blocker recurred for at least three consecutive Goal turns. Tool visibility alone does not activate Goal mode. Never call for ordinary clarification, uncertainty, incomplete work, or recoverable failures.",
|
|
189
|
+
parameters: Type.Object({
|
|
190
|
+
goal_id: Type.String({
|
|
191
|
+
minLength: 1,
|
|
192
|
+
maxLength: MAX_GOAL_ID_LENGTH,
|
|
193
|
+
description: "The exact goal_id shown in the current active /goal prompt.",
|
|
194
|
+
}),
|
|
195
|
+
reason: Type.String({
|
|
196
|
+
minLength: 1,
|
|
197
|
+
maxLength: MAX_BLOCKER_REASON_LENGTH,
|
|
198
|
+
description: "The specific user or external action required to unblock the goal.",
|
|
199
|
+
}),
|
|
200
|
+
evidence: Type.String({
|
|
201
|
+
minLength: 1,
|
|
202
|
+
maxLength: MAX_BLOCKER_EVIDENCE_LENGTH,
|
|
203
|
+
description: "Concrete evidence from the repeated attempts that proves the impasse.",
|
|
204
|
+
}),
|
|
205
|
+
repeated_turns: Type.Integer({
|
|
206
|
+
minimum: 3,
|
|
207
|
+
description: "Number of separate turns spent trying to resolve this same blocker.",
|
|
208
|
+
}),
|
|
209
|
+
}),
|
|
210
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
211
|
+
const blockedGoal = runtime.activeGoal;
|
|
212
|
+
const goal = blockedGoal?.text ?? "unknown goal";
|
|
213
|
+
const requestedGoalId = typeof params.goal_id === "string" ? params.goal_id.trim() : "";
|
|
214
|
+
const reason = typeof params.reason === "string" ? params.reason.trim() : "";
|
|
215
|
+
const evidence = typeof params.evidence === "string" ? params.evidence.trim() : "";
|
|
216
|
+
const repeatedTurns = typeof params.repeated_turns === "number" ? params.repeated_turns : Number.NaN;
|
|
217
|
+
const reject = (rejectionReason: string, terminate = false) => {
|
|
218
|
+
const rejection = `goal_blocked rejected: ${rejectionReason}.`;
|
|
219
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
220
|
+
return {
|
|
221
|
+
content: toolContent(rejection),
|
|
222
|
+
details: blockerDetails(goal, requestedGoalId, reason, evidence, repeatedTurns),
|
|
223
|
+
...(terminate ? { terminate: true as const } : {}),
|
|
224
|
+
};
|
|
225
|
+
};
|
|
227
226
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (repeatedTurns < 3) return reject("repeated_turns must be at least 3");
|
|
227
|
+
if (!blockedGoal) return reject("no active goal");
|
|
228
|
+
if (!runtime.canRecordGoalUsage()) {
|
|
229
|
+
return reject("current run does not own the active goal");
|
|
230
|
+
}
|
|
231
|
+
const staleGoalRejection = goalIdRejectionReason(blockedGoal, requestedGoalId);
|
|
232
|
+
if (staleGoalRejection) return reject(staleGoalRejection);
|
|
233
|
+
if (blockedGoal.status !== "active") {
|
|
234
|
+
return reject(`goal is ${blockedGoal.status}, not active`);
|
|
235
|
+
}
|
|
236
|
+
if (!runtime.ownsWorkflow(blockedGoal)) return reject("active Goal no longer owns its workflow");
|
|
237
|
+
if (!reason) return reject("reason is empty");
|
|
238
|
+
if (reason.length > MAX_BLOCKER_REASON_LENGTH) return reject("reason is too long");
|
|
239
|
+
if (!evidence) return reject("evidence is empty");
|
|
240
|
+
if (evidence.length > MAX_BLOCKER_EVIDENCE_LENGTH) return reject("evidence is too long");
|
|
241
|
+
if (!Number.isInteger(repeatedTurns)) return reject("repeated_turns must be a whole number");
|
|
242
|
+
if (repeatedTurns < 3) return reject("repeated_turns must be at least 3");
|
|
245
243
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
244
|
+
const stoppedGoal = runtime.stopActiveGoal(ctx, {
|
|
245
|
+
kind: "blocker_report",
|
|
246
|
+
expectedGoalId: blockedGoal.id,
|
|
247
|
+
reason,
|
|
248
|
+
});
|
|
249
|
+
if (!stoppedGoal) return reject("active goal changed before blocker transition");
|
|
250
|
+
notifyTerminal(ctx.ui, `Goal blocked: ${truncateNotification(reason)}`, "warning");
|
|
253
251
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
252
|
+
return {
|
|
253
|
+
content: toolContent(`Goal blocked: ${reason}`),
|
|
254
|
+
details: blockerDetails(goal, requestedGoalId, reason, evidence, repeatedTurns),
|
|
255
|
+
terminate: true,
|
|
256
|
+
};
|
|
257
|
+
},
|
|
258
|
+
});
|
|
261
259
|
|
|
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
|
-
};
|
|
260
|
+
const goalWaitTool = defineTool({
|
|
261
|
+
name: GOAL_WAIT_TOOL,
|
|
262
|
+
label: "Goal Wait",
|
|
263
|
+
description: `Keep an active /goal quiet only when the latest effective Goal contract explicitly says Goal mode is active, supplies the matching current goal_id, and progress depends on an arranged external wake event or one safety deadline. Tool visibility alone does not activate Goal mode. Call goal_wait alone. Requests below ${MIN_GOAL_WAIT_DELAY_MS}ms are clamped to ${MIN_GOAL_WAIT_DELAY_MS}ms. Never call for ordinary unfinished work.`,
|
|
264
|
+
parameters: Type.Object({
|
|
265
|
+
goal_id: Type.String({
|
|
266
|
+
minLength: 1,
|
|
267
|
+
maxLength: MAX_GOAL_ID_LENGTH,
|
|
268
|
+
description: "The exact goal_id shown in the current active /goal prompt.",
|
|
269
|
+
}),
|
|
270
|
+
reason: Type.String({
|
|
271
|
+
minLength: 1,
|
|
272
|
+
maxLength: MAX_GOAL_WAIT_REASON_LENGTH,
|
|
273
|
+
description: "Why the goal is waiting and which external event should wake it.",
|
|
274
|
+
}),
|
|
275
|
+
resume_after_ms: Type.Optional(
|
|
276
|
+
Type.Integer({
|
|
277
|
+
minimum: 1,
|
|
278
|
+
maximum: MAX_GOAL_WAIT_DELAY_MS,
|
|
279
|
+
description: `Optional safety deadline in milliseconds that requests one continuation if no wake message arrives. Values below ${MIN_GOAL_WAIT_DELAY_MS} are accepted but clamped to ${MIN_GOAL_WAIT_DELAY_MS}.`,
|
|
280
|
+
}),
|
|
281
|
+
),
|
|
282
|
+
}),
|
|
283
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
284
|
+
const activeGoal = runtime.activeGoal;
|
|
285
|
+
const goal = activeGoal?.text ?? "unknown goal";
|
|
286
|
+
const requestedGoalId = typeof params.goal_id === "string" ? params.goal_id.trim() : "";
|
|
287
|
+
const reason = typeof params.reason === "string" ? params.reason.trim() : "";
|
|
288
|
+
const resumeAfterMs = typeof params.resume_after_ms === "number" ? params.resume_after_ms : undefined;
|
|
289
|
+
const reject = (rejectionReason: string) => {
|
|
290
|
+
const rejection = `goal_wait rejected: ${rejectionReason}.`;
|
|
291
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
292
|
+
return {
|
|
293
|
+
content: toolContent(rejection),
|
|
294
|
+
details: waitDetails(goal, requestedGoalId, reason, resumeAfterMs),
|
|
295
|
+
};
|
|
296
|
+
};
|
|
300
297
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
) {
|
|
321
|
-
return reject(`resume_after_ms must be a whole number from 1 to ${MAX_GOAL_WAIT_DELAY_MS}`);
|
|
322
|
-
}
|
|
298
|
+
if (!activeGoal) return reject("no active goal");
|
|
299
|
+
if (!runtime.canRecordGoalUsage()) {
|
|
300
|
+
return reject("current run does not own the active goal");
|
|
301
|
+
}
|
|
302
|
+
const staleGoalRejection = goalIdRejectionReason(activeGoal, requestedGoalId);
|
|
303
|
+
if (staleGoalRejection) return reject(staleGoalRejection);
|
|
304
|
+
if (activeGoal.status !== "active") {
|
|
305
|
+
return reject(`goal is ${activeGoal.status}, not active`);
|
|
306
|
+
}
|
|
307
|
+
if (!runtime.ownsWorkflow(activeGoal)) return reject("active Goal no longer owns its workflow");
|
|
308
|
+
if (activeGoal.waiting) return reject("goal is already waiting");
|
|
309
|
+
if (!reason) return reject("reason is empty");
|
|
310
|
+
if (reason.length > MAX_GOAL_WAIT_REASON_LENGTH) return reject("reason is too long");
|
|
311
|
+
if (
|
|
312
|
+
resumeAfterMs !== undefined &&
|
|
313
|
+
(!Number.isInteger(resumeAfterMs) || resumeAfterMs < 1 || resumeAfterMs > MAX_GOAL_WAIT_DELAY_MS)
|
|
314
|
+
) {
|
|
315
|
+
return reject(`resume_after_ms must be a whole number from 1 to ${MAX_GOAL_WAIT_DELAY_MS}`);
|
|
316
|
+
}
|
|
323
317
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
318
|
+
const { requestedMs, effectiveMs } = resolveGoalWaitDelay(resumeAfterMs);
|
|
319
|
+
const waiting = createGoalWait(reason, resumeAfterMs);
|
|
320
|
+
const waitingGoal = runtime.enterGoalWait(ctx, activeGoal.id, waiting);
|
|
321
|
+
if (!waitingGoal) return reject("active goal changed before waiting transition");
|
|
322
|
+
const clamped = requestedMs !== undefined && effectiveMs !== requestedMs;
|
|
323
|
+
notifyTerminal(ctx.ui, `Goal waiting: ${truncateNotification(reason)}`, "info");
|
|
324
|
+
return {
|
|
325
|
+
content: toolContent(
|
|
326
|
+
clamped
|
|
327
|
+
? `Goal waiting: ${reason}\nRequested resume_after_ms ${requestedMs} was clamped to ${effectiveMs}.`
|
|
328
|
+
: `Goal waiting: ${reason}`,
|
|
329
|
+
),
|
|
330
|
+
details: waitDetails(
|
|
331
|
+
goal,
|
|
332
|
+
requestedGoalId,
|
|
333
|
+
reason,
|
|
334
|
+
effectiveMs,
|
|
335
|
+
waiting.resumeAt,
|
|
336
|
+
clamped ? requestedMs : undefined,
|
|
337
|
+
),
|
|
338
|
+
terminate: true,
|
|
339
|
+
};
|
|
340
|
+
},
|
|
341
|
+
});
|
|
348
342
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
343
|
+
pi.registerTool(goalCompleteTool);
|
|
344
|
+
pi.registerTool(goalBlockedTool);
|
|
345
|
+
pi.registerTool(goalWaitTool);
|
|
352
346
|
}
|
|
353
347
|
|
|
354
348
|
interface GoalCompletionRenderResult {
|
|
355
|
-
|
|
356
|
-
|
|
349
|
+
content: Array<{ type: string; text?: string }>;
|
|
350
|
+
details?: unknown;
|
|
357
351
|
}
|
|
358
352
|
|
|
359
353
|
export function goalCompletionMarkdown(result: GoalCompletionRenderResult) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
354
|
+
const content = result.content
|
|
355
|
+
.filter((block) => block.type === "text" && typeof block.text === "string")
|
|
356
|
+
.map((block) => block.text)
|
|
357
|
+
.join("\n")
|
|
358
|
+
.trim();
|
|
359
|
+
const completionPrefix = "Goal complete:";
|
|
360
|
+
if (!content.startsWith(completionPrefix)) return content;
|
|
367
361
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
: content.slice(completionPrefix.length);
|
|
376
|
-
const safeSummary = safeTerminalText(summary);
|
|
377
|
-
return safeSummary ? `**Goal complete**\n\n${safeSummary}` : "**Goal complete**";
|
|
362
|
+
const details = result.details;
|
|
363
|
+
const summary =
|
|
364
|
+
details && typeof details === "object" && "summary" in details && typeof details.summary === "string"
|
|
365
|
+
? details.summary
|
|
366
|
+
: content.slice(completionPrefix.length);
|
|
367
|
+
const safeSummary = safeTerminalText(summary);
|
|
368
|
+
return safeSummary ? `**Goal complete**\n\n${safeSummary}` : "**Goal complete**";
|
|
378
369
|
}
|
|
379
370
|
|
|
380
371
|
export function renderGoalCompletion(result: GoalCompletionRenderResult) {
|
|
381
|
-
|
|
372
|
+
return new Markdown(goalCompletionMarkdown(result), 0, 0, getMarkdownTheme());
|
|
382
373
|
}
|
|
383
374
|
|
|
384
375
|
function toolContent(text: string) {
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
376
|
+
return [
|
|
377
|
+
{
|
|
378
|
+
type: "text" as const,
|
|
379
|
+
text: truncateHead(safeTerminalText(text), {
|
|
380
|
+
maxBytes: DEFAULT_MAX_BYTES,
|
|
381
|
+
maxLines: DEFAULT_MAX_LINES,
|
|
382
|
+
}).content,
|
|
383
|
+
},
|
|
384
|
+
];
|
|
394
385
|
}
|
|
395
386
|
|
|
396
387
|
function completionDetails(goal: string, goalId: string, summary: string): GoalCompleteDetails {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
388
|
+
return {
|
|
389
|
+
goal: goal.slice(0, MAX_GOAL_TEXT_LENGTH),
|
|
390
|
+
goal_id: goalId.slice(0, MAX_GOAL_ID_LENGTH),
|
|
391
|
+
summary: summary.slice(0, MAX_COMPLETION_SUMMARY_LENGTH),
|
|
392
|
+
};
|
|
402
393
|
}
|
|
403
394
|
|
|
404
395
|
function blockerDetails(
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
396
|
+
goal: string,
|
|
397
|
+
goalId: string,
|
|
398
|
+
reason: string,
|
|
399
|
+
evidence: string,
|
|
400
|
+
repeatedTurns: number,
|
|
410
401
|
): GoalBlockedDetails {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
402
|
+
return {
|
|
403
|
+
goal: goal.slice(0, MAX_GOAL_TEXT_LENGTH),
|
|
404
|
+
goal_id: goalId.slice(0, MAX_GOAL_ID_LENGTH),
|
|
405
|
+
reason: reason.slice(0, MAX_BLOCKER_REASON_LENGTH),
|
|
406
|
+
evidence: evidence.slice(0, MAX_BLOCKER_EVIDENCE_LENGTH),
|
|
407
|
+
repeated_turns: Number.isFinite(repeatedTurns) ? repeatedTurns : 0,
|
|
408
|
+
};
|
|
418
409
|
}
|
|
419
410
|
|
|
420
411
|
function waitDetails(
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
412
|
+
goal: string,
|
|
413
|
+
goalId: string,
|
|
414
|
+
reason: string,
|
|
415
|
+
resumeAfterMs: number | undefined,
|
|
416
|
+
resumeAt?: number,
|
|
417
|
+
requestedResumeAfterMs?: number,
|
|
427
418
|
): GoalWaitDetails {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
...(resumeAt === undefined ? {} : { resume_at: resumeAt }),
|
|
437
|
-
};
|
|
419
|
+
return {
|
|
420
|
+
goal: goal.slice(0, MAX_GOAL_TEXT_LENGTH),
|
|
421
|
+
goal_id: goalId.slice(0, MAX_GOAL_ID_LENGTH),
|
|
422
|
+
reason: reason.slice(0, MAX_GOAL_WAIT_REASON_LENGTH),
|
|
423
|
+
...(requestedResumeAfterMs === undefined ? {} : { requested_resume_after_ms: requestedResumeAfterMs }),
|
|
424
|
+
...(resumeAfterMs === undefined ? {} : { resume_after_ms: resumeAfterMs }),
|
|
425
|
+
...(resumeAt === undefined ? {} : { resume_at: resumeAt }),
|
|
426
|
+
};
|
|
438
427
|
}
|