@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/src/tools.ts CHANGED
@@ -1,5 +1,12 @@
1
- import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
1
+ import {
2
+ DEFAULT_MAX_BYTES,
3
+ DEFAULT_MAX_LINES,
4
+ defineTool,
5
+ type ExtensionAPI,
6
+ truncateHead,
7
+ } from "@earendil-works/pi-coding-agent";
2
8
  import { Type } from "typebox";
9
+ import { notifyTerminal, safeTerminalText } from "./errors.js";
3
10
  import {
4
11
  formatStatus,
5
12
  GOAL_BLOCKED_TOOL,
@@ -7,6 +14,7 @@ import {
7
14
  type GoalRuntime,
8
15
  goalIdRejectionReason,
9
16
  isContradictoryCompletionSummary,
17
+ MAX_GOAL_ID_LENGTH,
10
18
  STATUS_KEY,
11
19
  transitionGoal,
12
20
  truncateNotification,
@@ -26,6 +34,8 @@ interface GoalBlockedDetails {
26
34
  repeated_turns: number;
27
35
  }
28
36
 
37
+ const MAX_GOAL_TEXT_LENGTH = 4_000;
38
+ const MAX_COMPLETION_SUMMARY_LENGTH = 4_000;
29
39
  const MAX_BLOCKER_REASON_LENGTH = 1_000;
30
40
  const MAX_BLOCKER_EVIDENCE_LENGTH = 4_000;
31
41
 
@@ -45,10 +55,14 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
45
55
  ],
46
56
  parameters: Type.Object({
47
57
  goal_id: Type.String({
58
+ minLength: 1,
59
+ maxLength: MAX_GOAL_ID_LENGTH,
48
60
  description:
49
61
  "The exact goal_id shown in the current active /goal prompt. Used only to reject stale completion calls from older turns.",
50
62
  }),
51
63
  summary: Type.String({
64
+ minLength: 1,
65
+ maxLength: MAX_COMPLETION_SUMMARY_LENGTH,
52
66
  description:
53
67
  "State what was completed and what evidence verified it. Do not use this tool to report partial progress, blockers, failures, or remaining work.",
54
68
  }),
@@ -61,20 +75,20 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
61
75
 
62
76
  if (!completedGoal) {
63
77
  const rejection = "Goal completion rejected: no active goal.";
64
- ctx.ui.notify(rejection, "warning");
78
+ notifyTerminal(ctx.ui, rejection, "warning");
65
79
 
66
80
  return {
67
- content: [{ type: "text", text: rejection }],
68
- details: { goal, goal_id: requestedGoalId, summary } satisfies GoalCompleteDetails,
81
+ content: toolContent(rejection),
82
+ details: completionDetails(goal, requestedGoalId, summary),
69
83
  };
70
84
  }
71
85
  const completingDuringBudgetWrapUp = runtime.hasActiveBudgetWrapUp();
72
86
  if (!runtime.canRecordGoalUsage() && !completingDuringBudgetWrapUp) {
73
87
  const rejection = "Goal completion rejected: current run does not own the active goal.";
74
- ctx.ui.notify(rejection, "warning");
88
+ notifyTerminal(ctx.ui, rejection, "warning");
75
89
  return {
76
- content: [{ type: "text", text: rejection }],
77
- details: { goal, goal_id: requestedGoalId, summary } satisfies GoalCompleteDetails,
90
+ content: toolContent(rejection),
91
+ details: completionDetails(goal, requestedGoalId, summary),
78
92
  };
79
93
  }
80
94
  if (hasPendingSkipForGoal(runtime, completedGoal.id)) {
@@ -83,17 +97,17 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
83
97
  runtime.updateStatus(ctx, completedGoal);
84
98
  runtime.clearBudgetWrapUp();
85
99
  const rejection = "Goal completion rejected: goal is queued to be skipped.";
86
- ctx.ui.notify(rejection, "warning");
100
+ notifyTerminal(ctx.ui, rejection, "warning");
87
101
  return {
88
- content: [{ type: "text", text: rejection }],
89
- details: { goal, goal_id: requestedGoalId, summary } satisfies GoalCompleteDetails,
102
+ content: toolContent(rejection),
103
+ details: completionDetails(goal, requestedGoalId, summary),
90
104
  terminate: true,
91
105
  };
92
106
  }
93
107
  const staleGoalRejection = goalIdRejectionReason(completedGoal, requestedGoalId);
94
108
  if (staleGoalRejection) {
95
109
  const rejection = `Goal completion rejected: ${staleGoalRejection}.`;
96
- ctx.ui.notify(rejection, "warning");
110
+ notifyTerminal(ctx.ui, rejection, "warning");
97
111
  if (completingDuringBudgetWrapUp) {
98
112
  runtime.recordGoalUsage(completedGoal, ctx);
99
113
  runtime.persistGoal(completedGoal);
@@ -102,42 +116,39 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
102
116
  }
103
117
 
104
118
  return {
105
- content: [{ type: "text", text: rejection }],
106
- details: { goal, goal_id: requestedGoalId, summary } satisfies GoalCompleteDetails,
119
+ content: toolContent(rejection),
120
+ details: completionDetails(goal, requestedGoalId, summary),
107
121
  terminate: completingDuringBudgetWrapUp || undefined,
108
122
  };
109
123
  }
110
124
  if (completedGoal.status !== "active" && !completingDuringBudgetWrapUp) {
111
125
  const rejection = `Goal completion rejected: goal is ${completedGoal.status}, not active.`;
112
- ctx.ui.notify(rejection, "warning");
126
+ notifyTerminal(ctx.ui, rejection, "warning");
113
127
 
114
128
  return {
115
- content: [{ type: "text", text: rejection }],
116
- details: { goal, goal_id: requestedGoalId, summary } satisfies GoalCompleteDetails,
129
+ content: toolContent(rejection),
130
+ details: completionDetails(goal, requestedGoalId, summary),
117
131
  };
118
132
  }
119
133
 
120
134
  const rejectionReason = !summary
121
135
  ? "summary is empty"
122
- : isContradictoryCompletionSummary(summary)
123
- ? "summary says the goal is not complete"
124
- : undefined;
136
+ : summary.length > MAX_COMPLETION_SUMMARY_LENGTH
137
+ ? "summary is too long"
138
+ : isContradictoryCompletionSummary(summary)
139
+ ? "summary says the goal is not complete"
140
+ : undefined;
125
141
  if (rejectionReason) {
126
142
  runtime.recordGoalUsage(completedGoal, ctx);
127
143
  runtime.persistGoal(completedGoal);
128
144
  runtime.updateStatus(ctx, completedGoal);
129
145
  const rejection = `Goal completion rejected: ${rejectionReason}.`;
130
- ctx.ui.notify(rejection, "warning");
146
+ notifyTerminal(ctx.ui, rejection, "warning");
131
147
  if (completingDuringBudgetWrapUp) runtime.clearBudgetWrapUp();
132
148
 
133
149
  return {
134
- content: [
135
- {
136
- type: "text",
137
- text: rejection,
138
- },
139
- ],
140
- details: { goal, goal_id: requestedGoalId, summary } satisfies GoalCompleteDetails,
150
+ content: toolContent(rejection),
151
+ details: completionDetails(goal, requestedGoalId, summary),
141
152
  terminate: completingDuringBudgetWrapUp || undefined,
142
153
  };
143
154
  }
@@ -148,14 +159,14 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
148
159
  if (runtime.pendingQueueAction?.kind === "prioritize") {
149
160
  runtime.persistGoal(runtime.activeGoal);
150
161
  ctx.ui.setStatus(STATUS_KEY, "complete");
151
- ctx.ui.notify(`Goal complete: ${goal}. Priority goal waits for Pi to settle.`, "info");
162
+ notifyTerminal(
163
+ ctx.ui,
164
+ `Goal complete: ${goal}. Priority goal waits for Pi to settle.`,
165
+ "info",
166
+ );
152
167
  return {
153
- content: [{ type: "text", text: `Goal complete: ${summary}` }],
154
- details: {
155
- goal,
156
- goal_id: requestedGoalId,
157
- summary,
158
- } satisfies GoalCompleteDetails,
168
+ content: toolContent(`Goal complete: ${summary}`),
169
+ details: completionDetails(goal, requestedGoalId, summary),
159
170
  terminate: true,
160
171
  };
161
172
  }
@@ -168,22 +179,16 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
168
179
  };
169
180
  runtime.persistGoal(runtime.activeGoal);
170
181
  ctx.ui.setStatus(STATUS_KEY, "complete");
171
- ctx.ui.notify(
182
+ notifyTerminal(
183
+ ctx.ui,
172
184
  `Goal complete: ${goal}. Next goal queued: ${runtime.queuedGoals[0]?.text}`,
173
185
  "info",
174
186
  );
175
187
  return {
176
- content: [
177
- {
178
- type: "text",
179
- text: `Goal complete: ${summary}\nNext goal queued: ${runtime.queuedGoals[0]?.text}`,
180
- },
181
- ],
182
- details: {
183
- goal,
184
- goal_id: requestedGoalId,
185
- summary,
186
- } satisfies GoalCompleteDetails,
188
+ content: toolContent(
189
+ `Goal complete: ${summary}\nNext goal queued: ${runtime.queuedGoals[0]?.text}`,
190
+ ),
191
+ details: completionDetails(goal, requestedGoalId, summary),
187
192
  terminate: true,
188
193
  };
189
194
  }
@@ -192,11 +197,11 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
192
197
  ctx.ui.setStatus(STATUS_KEY, formatStatus(runtime.activeGoal));
193
198
  runtime.clearActiveGoal(ctx);
194
199
  runtime.showCompletionStatus(ctx);
195
- ctx.ui.notify(`Goal complete: ${goal}`, "info");
200
+ notifyTerminal(ctx.ui, `Goal complete: ${goal}`, "info");
196
201
 
197
202
  return {
198
- content: [{ type: "text", text: `Goal complete: ${summary}` }],
199
- details: { goal, goal_id: requestedGoalId, summary } satisfies GoalCompleteDetails,
203
+ content: toolContent(`Goal complete: ${summary}`),
204
+ details: completionDetails(goal, requestedGoalId, summary),
200
205
  terminate: true,
201
206
  };
202
207
  },
@@ -217,6 +222,8 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
217
222
  ],
218
223
  parameters: Type.Object({
219
224
  goal_id: Type.String({
225
+ minLength: 1,
226
+ maxLength: MAX_GOAL_ID_LENGTH,
220
227
  description: "The exact goal_id shown in the current active /goal prompt.",
221
228
  }),
222
229
  reason: Type.String({
@@ -244,16 +251,10 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
244
251
  typeof params.repeated_turns === "number" ? params.repeated_turns : Number.NaN;
245
252
  const reject = (rejectionReason: string, terminate = false) => {
246
253
  const rejection = `goal_blocked rejected: ${rejectionReason}.`;
247
- ctx.ui.notify(rejection, "warning");
254
+ notifyTerminal(ctx.ui, rejection, "warning");
248
255
  return {
249
- content: [{ type: "text" as const, text: rejection }],
250
- details: {
251
- goal,
252
- goal_id: requestedGoalId,
253
- reason: reason.slice(0, MAX_BLOCKER_REASON_LENGTH),
254
- evidence: evidence.slice(0, MAX_BLOCKER_EVIDENCE_LENGTH),
255
- repeated_turns: Number.isFinite(repeatedTurns) ? repeatedTurns : 0,
256
- } satisfies GoalBlockedDetails,
256
+ content: toolContent(rejection),
257
+ details: blockerDetails(goal, requestedGoalId, reason, evidence, repeatedTurns),
257
258
  ...(terminate ? { terminate: true as const } : {}),
258
259
  };
259
260
  };
@@ -287,17 +288,11 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
287
288
  reason,
288
289
  });
289
290
  if (!stoppedGoal) return reject("active goal changed before blocker transition");
290
- ctx.ui.notify(`Goal blocked: ${truncateNotification(reason)}`, "warning");
291
+ notifyTerminal(ctx.ui, `Goal blocked: ${truncateNotification(reason)}`, "warning");
291
292
 
292
293
  return {
293
- content: [{ type: "text", text: `Goal blocked: ${reason}` }],
294
- details: {
295
- goal,
296
- goal_id: requestedGoalId,
297
- reason,
298
- evidence,
299
- repeated_turns: repeatedTurns,
300
- } satisfies GoalBlockedDetails,
294
+ content: toolContent(`Goal blocked: ${reason}`),
295
+ details: blockerDetails(goal, requestedGoalId, reason, evidence, repeatedTurns),
301
296
  terminate: true,
302
297
  };
303
298
  },
@@ -307,6 +302,42 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
307
302
  pi.registerTool(goalBlockedTool);
308
303
  }
309
304
 
305
+ function toolContent(text: string) {
306
+ return [
307
+ {
308
+ type: "text" as const,
309
+ text: truncateHead(safeTerminalText(text), {
310
+ maxBytes: DEFAULT_MAX_BYTES,
311
+ maxLines: DEFAULT_MAX_LINES,
312
+ }).content,
313
+ },
314
+ ];
315
+ }
316
+
317
+ function completionDetails(goal: string, goalId: string, summary: string): GoalCompleteDetails {
318
+ return {
319
+ goal: goal.slice(0, MAX_GOAL_TEXT_LENGTH),
320
+ goal_id: goalId.slice(0, MAX_GOAL_ID_LENGTH),
321
+ summary: summary.slice(0, MAX_COMPLETION_SUMMARY_LENGTH),
322
+ };
323
+ }
324
+
325
+ function blockerDetails(
326
+ goal: string,
327
+ goalId: string,
328
+ reason: string,
329
+ evidence: string,
330
+ repeatedTurns: number,
331
+ ): GoalBlockedDetails {
332
+ return {
333
+ goal: goal.slice(0, MAX_GOAL_TEXT_LENGTH),
334
+ goal_id: goalId.slice(0, MAX_GOAL_ID_LENGTH),
335
+ reason: reason.slice(0, MAX_BLOCKER_REASON_LENGTH),
336
+ evidence: evidence.slice(0, MAX_BLOCKER_EVIDENCE_LENGTH),
337
+ repeated_turns: Number.isFinite(repeatedTurns) ? repeatedTurns : 0,
338
+ };
339
+ }
340
+
310
341
  function hasPendingSkipForGoal(runtime: GoalRuntime, goalId: string) {
311
342
  return (
312
343
  runtime.pendingQueueAction?.kind === "advance" &&