@narumitw/pi-goal 0.50.0 → 0.52.0

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/settings.ts CHANGED
@@ -11,9 +11,6 @@ export type ContinuationLimit = number | null;
11
11
 
12
12
  export interface GoalSettings {
13
13
  toolVisibility: GoalToolVisibility;
14
- experimental: {
15
- goals: boolean;
16
- };
17
14
  rpc: {
18
15
  enabled: boolean;
19
16
  };
@@ -24,16 +21,15 @@ export interface GoalSettings {
24
21
  }
25
22
 
26
23
  export const DEFAULT_GOAL_SETTINGS: GoalSettings = {
27
- toolVisibility: "always",
28
- experimental: { goals: false },
24
+ toolVisibility: "after-first-goal",
29
25
  rpc: { enabled: false },
30
26
  continuationLimits: { automaticTurns: 25, noProgressTurns: 3 },
31
27
  };
32
28
 
33
29
  export type GoalSettingsLoadResult =
34
- | { kind: "missing" }
30
+ | { kind: "missing"; legacyExperimentalGoals: false }
35
31
  | { kind: "invalid"; reason: string }
36
- | { kind: "loaded"; settings: GoalSettings };
32
+ | { kind: "loaded"; settings: GoalSettings; legacyExperimentalGoals: boolean };
37
33
 
38
34
  export type GoalSettingsLoadIssue = Extract<GoalSettingsLoadResult, { kind: "invalid" }>;
39
35
 
@@ -51,23 +47,6 @@ export function normalizeGoalSettings(value: unknown): GoalSettings | undefined
51
47
  : DEFAULT_GOAL_SETTINGS.toolVisibility;
52
48
  if (!GOAL_TOOL_VISIBILITIES.includes(toolVisibility as GoalToolVisibility)) return undefined;
53
49
 
54
- const experimentalValue = Object.hasOwn(value, "experimental")
55
- ? Reflect.get(value, "experimental")
56
- : undefined;
57
- if (
58
- experimentalValue !== undefined &&
59
- (typeof experimentalValue !== "object" ||
60
- experimentalValue === null ||
61
- Array.isArray(experimentalValue))
62
- ) {
63
- return undefined;
64
- }
65
- const goals =
66
- experimentalValue && Object.hasOwn(experimentalValue, "goals")
67
- ? Reflect.get(experimentalValue, "goals")
68
- : DEFAULT_GOAL_SETTINGS.experimental.goals;
69
- if (typeof goals !== "boolean") return undefined;
70
-
71
50
  const rpcValue = Object.hasOwn(value, "rpc") ? Reflect.get(value, "rpc") : undefined;
72
51
  if (
73
52
  rpcValue !== undefined &&
@@ -108,7 +87,6 @@ export function normalizeGoalSettings(value: unknown): GoalSettings | undefined
108
87
 
109
88
  return {
110
89
  toolVisibility: toolVisibility as GoalToolVisibility,
111
- experimental: { goals },
112
90
  rpc: { enabled: rpcEnabled },
113
91
  continuationLimits: { automaticTurns, noProgressTurns },
114
92
  };
@@ -145,14 +123,12 @@ export function saveGoalSettings(
145
123
  }
146
124
  }
147
125
 
148
- const experimental = ownRecord(raw.experimental) ?? {};
149
126
  const rpc = ownRecord(raw.rpc) ?? {};
150
127
  const continuationLimits = ownRecord(raw.continuationLimits) ?? {};
151
128
  const document = `${JSON.stringify(
152
129
  {
153
130
  ...raw,
154
131
  toolVisibility: normalized.toolVisibility,
155
- experimental: { ...experimental, goals: normalized.experimental.goals },
156
132
  rpc: { ...rpc, enabled: normalized.rpc.enabled },
157
133
  continuationLimits: {
158
134
  ...continuationLimits,
@@ -188,20 +164,28 @@ export function readGoalSettings(
188
164
  try {
189
165
  contents = readFileSync(settingsPath, "utf8");
190
166
  } catch (error: unknown) {
191
- if (isNodeError(error) && error.code === "ENOENT") return { kind: "missing" };
167
+ if (isNodeError(error) && error.code === "ENOENT") {
168
+ return { kind: "missing", legacyExperimentalGoals: false };
169
+ }
192
170
  return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
193
171
  }
194
172
 
195
173
  try {
196
- const settings = normalizeGoalSettings(JSON.parse(contents) as unknown);
174
+ const parsed = JSON.parse(contents) as unknown;
175
+ const settings = normalizeGoalSettings(parsed);
197
176
  return settings
198
- ? { kind: "loaded", settings }
177
+ ? { kind: "loaded", settings, legacyExperimentalGoals: hasLegacyExperimentalGoals(parsed) }
199
178
  : { kind: "invalid", reason: `${settingsPath}: invalid settings shape` };
200
179
  } catch (error: unknown) {
201
180
  return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
202
181
  }
203
182
  }
204
183
 
184
+ function hasLegacyExperimentalGoals(value: unknown) {
185
+ const experimental = ownRecord(ownRecord(value)?.experimental);
186
+ return experimental?.goals === true;
187
+ }
188
+
205
189
  function ownRecord(value: unknown): Record<string, unknown> | undefined {
206
190
  return value && typeof value === "object" && !Array.isArray(value)
207
191
  ? (value as Record<string, unknown>)
package/src/tools.ts CHANGED
@@ -108,19 +108,6 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
108
108
  details: completionDetails(goal, requestedGoalId, summary),
109
109
  };
110
110
  }
111
- if (hasPendingSkipForGoal(runtime, completedGoal.id)) {
112
- runtime.recordGoalUsage(completedGoal, ctx);
113
- runtime.persistGoal(completedGoal);
114
- runtime.updateStatus(ctx, completedGoal);
115
- runtime.clearBudgetWrapUp();
116
- const rejection = "Goal completion rejected: goal is queued to be skipped.";
117
- notifyTerminal(ctx.ui, rejection, "warning");
118
- return {
119
- content: toolContent(rejection),
120
- details: completionDetails(goal, requestedGoalId, summary),
121
- terminate: true,
122
- };
123
- }
124
111
  const staleGoalRejection = goalIdRejectionReason(completedGoal, requestedGoalId);
125
112
  if (staleGoalRejection) {
126
113
  const rejection = `Goal completion rejected: ${staleGoalRejection}.`;
@@ -174,42 +161,6 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
174
161
  runtime.activeGoal = transitionGoal(completedGoal, "complete");
175
162
  runtime.setCompletionSummary(runtime.activeGoal.id, summary);
176
163
  runtime.recordGoalUsage(runtime.activeGoal, ctx);
177
- if (runtime.pendingQueueAction?.kind === "prioritize") {
178
- runtime.persistGoal(runtime.activeGoal);
179
- ctx.ui.setStatus(STATUS_KEY, "complete");
180
- notifyTerminal(
181
- ctx.ui,
182
- `Goal complete: ${goal}. Priority goal waits for Pi to settle.`,
183
- "info",
184
- );
185
- return {
186
- content: toolContent(`Goal complete: ${summary}`),
187
- details: completionDetails(goal, requestedGoalId, summary),
188
- terminate: true,
189
- };
190
- }
191
- if (runtime.queuedGoals.length > 0) {
192
- runtime.pendingQueueAction = {
193
- kind: "advance",
194
- goalId: runtime.activeGoal.id,
195
- reason: "complete",
196
- completedText: goal,
197
- };
198
- runtime.persistGoal(runtime.activeGoal);
199
- ctx.ui.setStatus(STATUS_KEY, "complete");
200
- notifyTerminal(
201
- ctx.ui,
202
- `Goal complete: ${goal}. Next goal queued: ${runtime.queuedGoals[0]?.text}`,
203
- "info",
204
- );
205
- return {
206
- content: toolContent(
207
- `Goal complete: ${summary}\nNext goal queued: ${runtime.queuedGoals[0]?.text}`,
208
- ),
209
- details: completionDetails(goal, requestedGoalId, summary),
210
- terminate: true,
211
- };
212
- }
213
164
  runtime.persistGoal(runtime.activeGoal);
214
165
 
215
166
  ctx.ui.setStatus(STATUS_KEY, formatStatus(runtime.activeGoal));
@@ -281,13 +232,6 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
281
232
  if (!runtime.canRecordGoalUsage()) {
282
233
  return reject("current run does not own the active goal");
283
234
  }
284
- if (hasPendingSkipForGoal(runtime, blockedGoal.id)) {
285
- runtime.recordGoalUsage(blockedGoal, ctx);
286
- runtime.persistGoal(blockedGoal);
287
- runtime.updateStatus(ctx, blockedGoal);
288
- runtime.clearBudgetWrapUp();
289
- return reject("goal is queued to be skipped", true);
290
- }
291
235
  const staleGoalRejection = goalIdRejectionReason(blockedGoal, requestedGoalId);
292
236
  if (staleGoalRejection) return reject(staleGoalRejection);
293
237
  if (blockedGoal.status !== "active") {
@@ -368,7 +312,6 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
368
312
  if (!runtime.canRecordGoalUsage()) {
369
313
  return reject("current run does not own the active goal");
370
314
  }
371
- if (runtime.pendingQueueAction) return reject("a goal queue transition is pending");
372
315
  const staleGoalRejection = goalIdRejectionReason(activeGoal, requestedGoalId);
373
316
  if (staleGoalRejection) return reject(staleGoalRejection);
374
317
  if (activeGoal.status !== "active") {
@@ -471,11 +414,3 @@ function waitDetails(
471
414
  ...(resumeAt === undefined ? {} : { resume_at: resumeAt }),
472
415
  };
473
416
  }
474
-
475
- function hasPendingSkipForGoal(runtime: GoalRuntime, goalId: string) {
476
- return (
477
- runtime.pendingQueueAction?.kind === "advance" &&
478
- runtime.pendingQueueAction.reason === "skip" &&
479
- runtime.pendingQueueAction.goalId === goalId
480
- );
481
- }
package/src/queue.ts DELETED
@@ -1,126 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { checkpointGoalActiveTime } from "./accounting.js";
3
- import type { ActiveGoal, PendingQueueAction } from "./persistence.js";
4
- import { resetGoalSafetyEpoch } from "./safety.js";
5
-
6
- export interface GoalQueueResult {
7
- goal: ActiveGoal | undefined;
8
- queue: ActiveGoal[];
9
- }
10
-
11
- export interface DropLastGoalResult extends GoalQueueResult {
12
- removed: ActiveGoal | undefined;
13
- }
14
-
15
- export function goalQueueIdentity(
16
- goal: ActiveGoal | undefined,
17
- queue: readonly ActiveGoal[],
18
- pendingAction: PendingQueueAction | undefined,
19
- ) {
20
- const pendingIdentity =
21
- pendingAction?.kind === "prioritize"
22
- ? [
23
- pendingAction.kind,
24
- pendingAction.objective,
25
- pendingAction.tokenBudget,
26
- pendingAction.displacedUsageFinalized,
27
- ]
28
- : pendingAction
29
- ? [
30
- pendingAction.kind,
31
- pendingAction.goalId,
32
- pendingAction.reason,
33
- pendingAction.completedText,
34
- ]
35
- : undefined;
36
- return JSON.stringify([goal?.id, queue.map((queuedGoal) => queuedGoal.id), pendingIdentity]);
37
- }
38
-
39
- export function createQueuedGoal(
40
- text: string,
41
- tokenBudget: number | undefined,
42
- now = Date.now(),
43
- ): ActiveGoal {
44
- return {
45
- id: randomUUID(),
46
- text,
47
- status: "queued",
48
- startedAt: now,
49
- updatedAt: now,
50
- iteration: 0,
51
- tokenBudget,
52
- tokensUsed: 0,
53
- timeUsedSeconds: 0,
54
- baselineTokens: 0,
55
- automaticModelTurns: 0,
56
- toolFreeRepeatCount: 0,
57
- };
58
- }
59
-
60
- export function appendGoal(queue: readonly ActiveGoal[], goal: ActiveGoal): ActiveGoal[] {
61
- return [...queue, goal];
62
- }
63
-
64
- export function prioritizeGoal(
65
- currentGoal: ActiveGoal,
66
- queue: readonly ActiveGoal[],
67
- prioritizedGoal: ActiveGoal,
68
- now = Date.now(),
69
- ): GoalQueueResult {
70
- return {
71
- goal: prioritizedGoal,
72
- queue: [shelveGoal(currentGoal, now), ...queue],
73
- };
74
- }
75
-
76
- export function dropLastGoal(
77
- currentGoal: ActiveGoal,
78
- queue: readonly ActiveGoal[],
79
- ): DropLastGoalResult {
80
- if (queue.length === 0) {
81
- return { goal: undefined, queue: [], removed: currentGoal };
82
- }
83
- return {
84
- goal: currentGoal,
85
- queue: queue.slice(0, -1),
86
- removed: queue.at(-1),
87
- };
88
- }
89
-
90
- export function skipGoal(queue: readonly ActiveGoal[]): GoalQueueResult {
91
- return { goal: queue[0], queue: queue.slice(1) };
92
- }
93
-
94
- export function shelveGoal(goal: ActiveGoal, now = Date.now()): ActiveGoal {
95
- const { waiting: _waiting, ...withoutWait } = goal;
96
- if (goal.status !== "active") {
97
- return { ...withoutWait, activeStartedAt: undefined, updatedAt: now };
98
- }
99
- const shelved = { ...withoutWait, status: "queued" as const, updatedAt: now };
100
- checkpointGoalActiveTime(shelved, now, false);
101
- return shelved;
102
- }
103
-
104
- export function activateQueuedGoal(
105
- goal: ActiveGoal,
106
- currentTokenTotal: number,
107
- now = Date.now(),
108
- ): ActiveGoal {
109
- const rebased = {
110
- ...goal,
111
- baselineTokens: Math.max(0, currentTokenTotal - goal.tokensUsed),
112
- activeStartedAt: undefined,
113
- updatedAt: now,
114
- };
115
- if (goal.status !== "queued") return rebased;
116
- const activated = {
117
- ...rebased,
118
- id: randomUUID(),
119
- status: "active" as const,
120
- };
121
- checkpointGoalActiveTime(activated, now, true);
122
- if (activated.tokenBudget !== undefined && activated.tokensUsed >= activated.tokenBudget) {
123
- return { ...activated, status: "budget_limited", activeStartedAt: undefined };
124
- }
125
- return activated.safetyResetPending ? resetGoalSafetyEpoch(activated) : activated;
126
- }