@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/README.md +32 -49
- package/package.json +8 -9
- package/src/command-registration.ts +15 -38
- package/src/command.ts +25 -70
- package/src/commands.ts +51 -383
- package/src/goal.ts +1 -1
- package/src/lifecycle.ts +25 -138
- package/src/menu.ts +15 -242
- package/src/persistence.ts +72 -108
- package/src/runtime.ts +22 -64
- package/src/settings-ui.ts +3 -125
- package/src/settings.ts +14 -30
- package/src/tools.ts +0 -65
- package/src/queue.ts +0 -126
package/src/persistence.ts
CHANGED
|
@@ -35,31 +35,18 @@ export interface ActiveGoal {
|
|
|
35
35
|
waiting?: GoalWait;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
export type PendingQueueAction =
|
|
39
|
-
| {
|
|
40
|
-
kind: "prioritize";
|
|
41
|
-
objective: string;
|
|
42
|
-
tokenBudget?: number;
|
|
43
|
-
displacedUsageFinalized?: boolean;
|
|
44
|
-
}
|
|
45
|
-
| {
|
|
46
|
-
kind: "advance";
|
|
47
|
-
goalId: string;
|
|
48
|
-
reason: "complete" | "skip";
|
|
49
|
-
completedText: string;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
38
|
export interface GoalStateEntryData {
|
|
53
39
|
goal: ActiveGoal | null;
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface LegacyQueueState {
|
|
43
|
+
reason: "canonical-queue" | "legacy-goals";
|
|
44
|
+
retainedGoals: number;
|
|
56
45
|
}
|
|
57
46
|
|
|
58
47
|
export interface LoadedGoalState {
|
|
59
48
|
goal: ActiveGoal | undefined;
|
|
60
|
-
|
|
61
|
-
pendingAction: PendingQueueAction | undefined;
|
|
62
|
-
hasExperimentalQueueState: boolean;
|
|
49
|
+
legacyQueueState: LegacyQueueState | undefined;
|
|
63
50
|
source: "none" | "canonical" | "legacy-goals";
|
|
64
51
|
}
|
|
65
52
|
|
|
@@ -76,16 +63,8 @@ interface SessionContext {
|
|
|
76
63
|
};
|
|
77
64
|
}
|
|
78
65
|
|
|
79
|
-
export function serializeGoalState(
|
|
80
|
-
goal:
|
|
81
|
-
queue: readonly ActiveGoal[],
|
|
82
|
-
pendingAction: PendingQueueAction | undefined,
|
|
83
|
-
): GoalStateEntryData {
|
|
84
|
-
return {
|
|
85
|
-
goal: goal ?? null,
|
|
86
|
-
...(queue.length > 0 ? { queue: [...queue] } : {}),
|
|
87
|
-
...(pendingAction ? { pendingAction } : {}),
|
|
88
|
-
};
|
|
66
|
+
export function serializeGoalState(goal: ActiveGoal | undefined): GoalStateEntryData {
|
|
67
|
+
return { goal: goal ?? null };
|
|
89
68
|
}
|
|
90
69
|
|
|
91
70
|
export function loadGoalStateFromSession(ctx: SessionContext): LoadedGoalState {
|
|
@@ -105,7 +84,7 @@ export function loadGoalStateFromSession(ctx: SessionContext): LoadedGoalState {
|
|
|
105
84
|
|
|
106
85
|
export function loadGoalFromSession(ctx: SessionContext): ActiveGoal | undefined {
|
|
107
86
|
const loaded = loadGoalStateFromSession(ctx);
|
|
108
|
-
if (loaded.
|
|
87
|
+
if (loaded.legacyQueueState || loaded.goal?.status === "complete") return undefined;
|
|
109
88
|
return loaded.goal;
|
|
110
89
|
}
|
|
111
90
|
|
|
@@ -113,27 +92,51 @@ function loadCanonicalGoalState(data: unknown): LoadedGoalState {
|
|
|
113
92
|
if (!isRecord(data)) return emptyGoalState("canonical");
|
|
114
93
|
const rawGoal = data.goal;
|
|
115
94
|
if (rawGoal !== null && !isGoal(rawGoal)) return emptyGoalState("canonical");
|
|
116
|
-
const rawQueue = Object.hasOwn(data, "queue") ? data.queue :
|
|
117
|
-
if (!Array.isArray(rawQueue) || !rawQueue.every(isQueueGoal)) {
|
|
95
|
+
const rawQueue = Object.hasOwn(data, "queue") ? data.queue : undefined;
|
|
96
|
+
if (rawQueue !== undefined && (!Array.isArray(rawQueue) || !rawQueue.every(isQueueGoal))) {
|
|
118
97
|
return emptyGoalState("canonical");
|
|
119
98
|
}
|
|
120
|
-
const pendingAction =
|
|
121
|
-
|
|
122
|
-
|
|
99
|
+
const pendingAction = Object.hasOwn(data, "pendingAction")
|
|
100
|
+
? normalizeCanonicalPendingAction(data.pendingAction)
|
|
101
|
+
: undefined;
|
|
102
|
+
if (Object.hasOwn(data, "pendingAction") && !pendingAction) return emptyGoalState("canonical");
|
|
103
|
+
const hasQueueFields = rawQueue !== undefined || pendingAction !== undefined;
|
|
104
|
+
if (hasQueueFields || (isGoal(rawGoal) && rawGoal.status === "queued")) {
|
|
105
|
+
return legacyQueueState("canonical", {
|
|
106
|
+
reason: "canonical-queue",
|
|
107
|
+
retainedGoals: countCanonicalLegacyGoals(rawGoal, rawQueue, pendingAction),
|
|
108
|
+
});
|
|
123
109
|
}
|
|
124
110
|
|
|
125
|
-
const queue = rawQueue.map(normalizeQueuedGoal);
|
|
126
111
|
let goal = rawGoal === null ? undefined : normalizeLoadedGoal(rawGoal);
|
|
127
|
-
if (goal?.status === "complete"
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
112
|
+
if (goal?.status === "complete") goal = undefined;
|
|
113
|
+
return { goal, legacyQueueState: undefined, source: "canonical" };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function countCanonicalLegacyGoals(
|
|
117
|
+
goal: unknown,
|
|
118
|
+
queue: ActiveGoal[] | undefined,
|
|
119
|
+
pendingAction: { kind: string } | undefined,
|
|
120
|
+
) {
|
|
121
|
+
let count = isGoal(goal) && goal.status !== "complete" ? 1 : 0;
|
|
122
|
+
count += (queue ?? []).filter((queuedGoal) => queuedGoal.status !== "complete").length;
|
|
123
|
+
if (pendingAction?.kind === "prioritize") count += 1;
|
|
124
|
+
return count;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function normalizeCanonicalPendingAction(value: unknown): { kind: string } | undefined {
|
|
128
|
+
if (!isRecord(value)) return undefined;
|
|
129
|
+
if (value.kind === "prioritize" && validObjective(value.objective)) return { kind: "prioritize" };
|
|
130
|
+
if (
|
|
131
|
+
value.kind === "advance" &&
|
|
132
|
+
typeof value.goalId === "string" &&
|
|
133
|
+
value.goalId.trim() &&
|
|
134
|
+
(value.reason === "complete" || value.reason === "skip") &&
|
|
135
|
+
validObjective(value.completedText)
|
|
136
|
+
) {
|
|
137
|
+
return { kind: "advance" };
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
137
140
|
}
|
|
138
141
|
|
|
139
142
|
function loadLegacyGoalsState(data: unknown): LoadedGoalState {
|
|
@@ -147,78 +150,30 @@ function loadLegacyGoalsState(data: unknown): LoadedGoalState {
|
|
|
147
150
|
} else {
|
|
148
151
|
rawGoals = [];
|
|
149
152
|
}
|
|
150
|
-
const goals = rawGoals.map((goal, index) =>
|
|
151
|
-
index === 0 ? normalizeLoadedGoal(goal) : normalizeQueuedGoal(goal),
|
|
152
|
-
);
|
|
153
153
|
const pendingAction = normalizeLegacyPendingPrioritize(data.pendingUnshift);
|
|
154
|
-
if (
|
|
155
|
-
return {
|
|
156
|
-
goal: goals[0],
|
|
157
|
-
queue: goals.slice(1),
|
|
158
|
-
pendingAction,
|
|
159
|
-
hasExperimentalQueueState:
|
|
160
|
-
goals[0]?.status === "queued" || goals.length > 1 || pendingAction !== undefined,
|
|
161
|
-
source: "legacy-goals",
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function normalizePendingQueueAction(value: unknown): PendingQueueAction | undefined {
|
|
166
|
-
if (!isRecord(value)) return undefined;
|
|
167
|
-
if (value.kind === "prioritize") {
|
|
168
|
-
if (
|
|
169
|
-
!validObjective(value.objective) ||
|
|
170
|
-
(Object.hasOwn(value, "displacedUsageFinalized") &&
|
|
171
|
-
typeof value.displacedUsageFinalized !== "boolean")
|
|
172
|
-
) {
|
|
173
|
-
return undefined;
|
|
174
|
-
}
|
|
154
|
+
if (rawGoals.length === 1 && rawGoals[0]?.status !== "queued" && pendingAction === undefined) {
|
|
175
155
|
return {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
...(value.displacedUsageFinalized === true ? { displacedUsageFinalized: true } : {}),
|
|
156
|
+
goal: normalizeLoadedGoal(rawGoals[0]),
|
|
157
|
+
legacyQueueState: undefined,
|
|
158
|
+
source: "legacy-goals",
|
|
180
159
|
};
|
|
181
160
|
}
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
(value.reason !== "complete" && value.reason !== "skip") ||
|
|
188
|
-
!validObjective(value.completedText)
|
|
189
|
-
) {
|
|
190
|
-
return undefined;
|
|
191
|
-
}
|
|
192
|
-
return {
|
|
193
|
-
kind: "advance",
|
|
194
|
-
goalId: value.goalId,
|
|
195
|
-
reason: value.reason,
|
|
196
|
-
completedText: value.completedText,
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
return undefined;
|
|
161
|
+
if (rawGoals.length === 0 && pendingAction === undefined) return emptyGoalState("legacy-goals");
|
|
162
|
+
return legacyQueueState("legacy-goals", {
|
|
163
|
+
reason: "legacy-goals",
|
|
164
|
+
retainedGoals: rawGoals.length + (pendingAction ? 1 : 0),
|
|
165
|
+
});
|
|
200
166
|
}
|
|
201
167
|
|
|
202
|
-
function normalizeLegacyPendingPrioritize(value: unknown):
|
|
168
|
+
function normalizeLegacyPendingPrioritize(value: unknown): { objective: string } | undefined {
|
|
203
169
|
if (!isRecord(value) || !validObjective(value.objective)) return undefined;
|
|
204
|
-
return {
|
|
205
|
-
kind: "prioritize",
|
|
206
|
-
objective: value.objective,
|
|
207
|
-
tokenBudget: normalizeTokenBudget(value.tokenBudget),
|
|
208
|
-
};
|
|
170
|
+
return { objective: value.objective };
|
|
209
171
|
}
|
|
210
172
|
|
|
211
173
|
function validObjective(value: unknown): value is string {
|
|
212
174
|
return typeof value === "string" && Boolean(value.trim()) && value.length <= 4_000;
|
|
213
175
|
}
|
|
214
176
|
|
|
215
|
-
function normalizeQueuedGoal(goal: ActiveGoal): ActiveGoal {
|
|
216
|
-
const normalized = normalizeLoadedGoal(goal);
|
|
217
|
-
return normalized.status === "active"
|
|
218
|
-
? { ...normalized, status: "queued", activeStartedAt: undefined }
|
|
219
|
-
: { ...normalized, activeStartedAt: undefined };
|
|
220
|
-
}
|
|
221
|
-
|
|
222
177
|
export function normalizeLoadedGoal(goal: ActiveGoal): ActiveGoal {
|
|
223
178
|
const now = Date.now();
|
|
224
179
|
const waiting = goal.status === "active" ? normalizeGoalWait(goal.waiting) : undefined;
|
|
@@ -311,9 +266,18 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
311
266
|
function emptyGoalState(source: LoadedGoalState["source"]): LoadedGoalState {
|
|
312
267
|
return {
|
|
313
268
|
goal: undefined,
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
269
|
+
legacyQueueState: undefined,
|
|
270
|
+
source,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function legacyQueueState(
|
|
275
|
+
source: LoadedGoalState["source"],
|
|
276
|
+
legacyQueue: LegacyQueueState,
|
|
277
|
+
): LoadedGoalState {
|
|
278
|
+
return {
|
|
279
|
+
goal: undefined,
|
|
280
|
+
legacyQueueState: legacyQueue,
|
|
317
281
|
source,
|
|
318
282
|
};
|
|
319
283
|
}
|
package/src/runtime.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
import {
|
|
16
16
|
type ActiveGoal,
|
|
17
17
|
clearLegacyPersistedGoal,
|
|
18
|
-
type
|
|
18
|
+
type LegacyQueueState,
|
|
19
19
|
type SafetyPauseCause,
|
|
20
20
|
serializeGoalState,
|
|
21
21
|
} from "./persistence.js";
|
|
@@ -156,8 +156,8 @@ interface GoalTerminalDetails {
|
|
|
156
156
|
export interface GoalSettingsRuntimeSnapshot {
|
|
157
157
|
settings: GoalSettings;
|
|
158
158
|
activeGoal?: ActiveGoal;
|
|
159
|
-
|
|
160
|
-
|
|
159
|
+
legacyQueueState?: LegacyQueueState;
|
|
160
|
+
legacyExperimentalGoalsSetting: boolean;
|
|
161
161
|
continuationIntent?: ContinuationTicket;
|
|
162
162
|
continuationDelivery?: ContinuationTicket;
|
|
163
163
|
goalRecovery?: GoalRecovery;
|
|
@@ -208,10 +208,8 @@ export class GoalRuntime {
|
|
|
208
208
|
/** Terminal details captured for the matching persisted-state snapshot. */
|
|
209
209
|
private terminalDetails?: GoalTerminalDetails;
|
|
210
210
|
private goalStateSink?: (snapshot: GoalStateSnapshot) => void;
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
queueFrozen = false;
|
|
214
|
-
queueFreezeAwaitingSettle = false;
|
|
211
|
+
legacyQueueState?: LegacyQueueState;
|
|
212
|
+
legacyExperimentalGoalsSetting = false;
|
|
215
213
|
completionStatusTimer?: NodeJS.Timeout;
|
|
216
214
|
private continuationDispatchTimer?: NodeJS.Timeout;
|
|
217
215
|
private readonly goalWaitTimer = new GoalWaitTimer();
|
|
@@ -248,6 +246,10 @@ export class GoalRuntime {
|
|
|
248
246
|
this.toolPolicy = new GoalToolPolicy(pi);
|
|
249
247
|
}
|
|
250
248
|
|
|
249
|
+
hasLegacyQueueInterface() {
|
|
250
|
+
return this.legacyExperimentalGoalsSetting || this.legacyQueueState !== undefined;
|
|
251
|
+
}
|
|
252
|
+
|
|
251
253
|
setGoalStateSink(sink: ((snapshot: GoalStateSnapshot) => void) | undefined) {
|
|
252
254
|
this.goalStateSink = sink;
|
|
253
255
|
}
|
|
@@ -274,13 +276,7 @@ export class GoalRuntime {
|
|
|
274
276
|
canRecordGoalUsage(goalId?: string) {
|
|
275
277
|
return (
|
|
276
278
|
this.agentRunGoalId !== null &&
|
|
277
|
-
(goalId === undefined ||
|
|
278
|
-
this.agentRunGoalId === undefined ||
|
|
279
|
-
this.agentRunGoalId === goalId) &&
|
|
280
|
-
!(
|
|
281
|
-
this.pendingQueueAction?.kind === "prioritize" &&
|
|
282
|
-
this.pendingQueueAction.displacedUsageFinalized === true
|
|
283
|
-
)
|
|
279
|
+
(goalId === undefined || this.agentRunGoalId === undefined || this.agentRunGoalId === goalId)
|
|
284
280
|
);
|
|
285
281
|
}
|
|
286
282
|
|
|
@@ -439,7 +435,6 @@ export class GoalRuntime {
|
|
|
439
435
|
|
|
440
436
|
restoreGoalWaitTimer(ctx: StatusContext) {
|
|
441
437
|
this.clearGoalWaitTimer();
|
|
442
|
-
if (this.queueFrozen || this.pendingQueueAction) return false;
|
|
443
438
|
const goal = this.activeGoal;
|
|
444
439
|
const resumeAt = goal?.status === "active" ? goal.waiting?.resumeAt : undefined;
|
|
445
440
|
if (!goal || resumeAt === undefined) return false;
|
|
@@ -448,7 +443,6 @@ export class GoalRuntime {
|
|
|
448
443
|
}
|
|
449
444
|
|
|
450
445
|
dispatchDueGoalWait(ctx: StatusContext) {
|
|
451
|
-
if (this.queueFrozen || this.pendingQueueAction) return false;
|
|
452
446
|
const goal = this.activeGoal;
|
|
453
447
|
const waiting = goal?.status === "active" ? goal.waiting : undefined;
|
|
454
448
|
const resumeAt = waiting?.resumeAt;
|
|
@@ -1008,12 +1002,7 @@ export class GoalRuntime {
|
|
|
1008
1002
|
if (!marker) return false;
|
|
1009
1003
|
const pending = this.pendingGoalPromptMarkers.get(marker);
|
|
1010
1004
|
if (!pending || !preservesOwnedPromptAtTerminalBoundary(prompt, pending.prompt)) return false;
|
|
1011
|
-
if (
|
|
1012
|
-
!this.queueFrozen &&
|
|
1013
|
-
!this.pendingQueueAction &&
|
|
1014
|
-
this.activeGoal?.id === pending.goalId &&
|
|
1015
|
-
this.activeGoal.status === "active"
|
|
1016
|
-
) {
|
|
1005
|
+
if (this.activeGoal?.id === pending.goalId && this.activeGoal.status === "active") {
|
|
1017
1006
|
return false;
|
|
1018
1007
|
}
|
|
1019
1008
|
this.pendingGoalPromptMarkers.delete(marker);
|
|
@@ -1105,17 +1094,14 @@ export class GoalRuntime {
|
|
|
1105
1094
|
if (!isTerminalGoalStatus(goal.status) || this.terminalDetails?.goalId !== goal.id) {
|
|
1106
1095
|
this.clearTerminalDetails();
|
|
1107
1096
|
}
|
|
1108
|
-
this.pi.appendEntry(
|
|
1109
|
-
GOAL_STATE_ENTRY_TYPE,
|
|
1110
|
-
serializeGoalState(goal, this.queuedGoals, this.pendingQueueAction),
|
|
1111
|
-
);
|
|
1097
|
+
this.pi.appendEntry(GOAL_STATE_ENTRY_TYPE, serializeGoalState(goal));
|
|
1112
1098
|
this.publishGoalState(
|
|
1113
1099
|
buildGoalStateSnapshot(goal, this.terminalDetails?.summary, this.terminalDetails?.reason),
|
|
1114
1100
|
);
|
|
1115
1101
|
}
|
|
1116
1102
|
|
|
1117
1103
|
clearPersistedGoal(cwd: string, clearedGoal?: ActiveGoal, reason = "goal cleared") {
|
|
1118
|
-
this.pi.appendEntry(GOAL_STATE_ENTRY_TYPE, serializeGoalState(undefined
|
|
1104
|
+
this.pi.appendEntry(GOAL_STATE_ENTRY_TYPE, serializeGoalState(undefined));
|
|
1119
1105
|
if (clearedGoal) {
|
|
1120
1106
|
this.publishGoalState({
|
|
1121
1107
|
goalId: clearedGoal.id,
|
|
@@ -1135,10 +1121,7 @@ export class GoalRuntime {
|
|
|
1135
1121
|
this.clearBudgetWrapUp();
|
|
1136
1122
|
this.clearStaleGoalToolCallBlock();
|
|
1137
1123
|
this.activeGoal = undefined;
|
|
1138
|
-
this.
|
|
1139
|
-
this.pendingQueueAction = undefined;
|
|
1140
|
-
this.queueFrozen = false;
|
|
1141
|
-
this.queueFreezeAwaitingSettle = false;
|
|
1124
|
+
this.legacyQueueState = undefined;
|
|
1142
1125
|
this.clearPersistedGoal(ctx.cwd, clearedGoal, reason);
|
|
1143
1126
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
1144
1127
|
// Do not relock toolPolicy: after first activation, keep tools visible for the
|
|
@@ -1149,8 +1132,8 @@ export class GoalRuntime {
|
|
|
1149
1132
|
return {
|
|
1150
1133
|
settings: structuredClone(this.settings),
|
|
1151
1134
|
activeGoal: this.activeGoal ? structuredClone(this.activeGoal) : undefined,
|
|
1152
|
-
|
|
1153
|
-
|
|
1135
|
+
legacyQueueState: this.legacyQueueState ? structuredClone(this.legacyQueueState) : undefined,
|
|
1136
|
+
legacyExperimentalGoalsSetting: this.legacyExperimentalGoalsSetting,
|
|
1154
1137
|
continuationIntent: this.continuationIntent
|
|
1155
1138
|
? structuredClone(this.continuationIntent)
|
|
1156
1139
|
: undefined,
|
|
@@ -1170,8 +1153,10 @@ export class GoalRuntime {
|
|
|
1170
1153
|
restoreSettingsApplicationState(snapshot: GoalSettingsRuntimeSnapshot) {
|
|
1171
1154
|
this.settings = structuredClone(snapshot.settings);
|
|
1172
1155
|
this.activeGoal = snapshot.activeGoal ? structuredClone(snapshot.activeGoal) : undefined;
|
|
1173
|
-
this.
|
|
1174
|
-
|
|
1156
|
+
this.legacyQueueState = snapshot.legacyQueueState
|
|
1157
|
+
? structuredClone(snapshot.legacyQueueState)
|
|
1158
|
+
: undefined;
|
|
1159
|
+
this.legacyExperimentalGoalsSetting = snapshot.legacyExperimentalGoalsSetting;
|
|
1175
1160
|
this.continuationIntent = snapshot.continuationIntent
|
|
1176
1161
|
? structuredClone(snapshot.continuationIntent)
|
|
1177
1162
|
: undefined;
|
|
@@ -1379,15 +1364,11 @@ export function formatBudget(goal: ActiveGoal) {
|
|
|
1379
1364
|
|
|
1380
1365
|
export function goalSummary(
|
|
1381
1366
|
goal: ActiveGoal,
|
|
1382
|
-
queuedGoals: readonly ActiveGoal[] = [],
|
|
1383
|
-
experimentalGoals = false,
|
|
1384
|
-
queueFrozen = false,
|
|
1385
|
-
pendingAction?: PendingQueueAction,
|
|
1386
1367
|
automaticTurnLimit: number | null = DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns,
|
|
1387
1368
|
) {
|
|
1388
1369
|
const summary = [
|
|
1389
1370
|
`Goal: ${goal.text}`,
|
|
1390
|
-
`Status: ${
|
|
1371
|
+
`Status: ${goal.waiting ? "waiting" : goal.status}`,
|
|
1391
1372
|
...(goal.waiting
|
|
1392
1373
|
? [
|
|
1393
1374
|
`Waiting: ${safeGoalMenuText(goal.waiting.reason, 1_000)}`,
|
|
@@ -1410,30 +1391,7 @@ export function goalSummary(
|
|
|
1410
1391
|
: "Safety pause: no progress. Progress is saved; open /goal to review and continue.",
|
|
1411
1392
|
);
|
|
1412
1393
|
}
|
|
1413
|
-
|
|
1414
|
-
const orderedGoals = [
|
|
1415
|
-
`[${goal.status}] ${goal.text}`,
|
|
1416
|
-
...(pendingAction?.kind === "prioritize" ? [`[pending] ${pendingAction.objective}`] : []),
|
|
1417
|
-
...queuedGoals.map((queuedGoal) => `[${queuedGoal.status}] ${queuedGoal.text}`),
|
|
1418
|
-
];
|
|
1419
|
-
summary.push(
|
|
1420
|
-
`Goals (${orderedGoals.length}):`,
|
|
1421
|
-
...orderedGoals.map((queuedGoal, index) => `${index + 1}. ${queuedGoal}`),
|
|
1422
|
-
);
|
|
1423
|
-
}
|
|
1424
|
-
if (pendingAction?.kind === "advance") {
|
|
1425
|
-
summary.push(
|
|
1426
|
-
`Pending queue action: ${pendingAction.reason === "complete" ? "complete" : "skip"} current goal when Pi settles.`,
|
|
1427
|
-
);
|
|
1428
|
-
}
|
|
1429
|
-
if (queueFrozen) {
|
|
1430
|
-
summary.push(
|
|
1431
|
-
"Queue is frozen. Re-enable experimental.goals and run /reload, or use /goal clear.",
|
|
1432
|
-
"Commands: /goal, /goal clear",
|
|
1433
|
-
);
|
|
1434
|
-
} else {
|
|
1435
|
-
summary.push(`Commands: ${goalCommandHint(goal, experimentalGoals)}`);
|
|
1436
|
-
}
|
|
1394
|
+
summary.push(`Commands: ${goalCommandHint(goal)}`);
|
|
1437
1395
|
return summary.join("\n");
|
|
1438
1396
|
}
|
|
1439
1397
|
|
package/src/settings-ui.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import { type ExtensionCommandContext, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { checkpointGoalActiveTime } from "./accounting.js";
|
|
4
3
|
import { notifyTerminal, safeTerminalText } from "./errors.js";
|
|
5
|
-
import {
|
|
4
|
+
import type { GoalRuntime } from "./runtime.js";
|
|
6
5
|
import {
|
|
7
6
|
DEFAULT_GOAL_SETTINGS,
|
|
8
7
|
GOAL_SETTINGS_FILE,
|
|
@@ -14,7 +13,6 @@ interface GoalSettingsUiOptions {
|
|
|
14
13
|
settingsPath?: string;
|
|
15
14
|
initialScreen?: "settings" | "automatic";
|
|
16
15
|
save?: (settings: GoalSettings, settingsPath: string) => void;
|
|
17
|
-
onQueueUnfrozen?: (ctx: ExtensionCommandContext) => Promise<void>;
|
|
18
16
|
}
|
|
19
17
|
|
|
20
18
|
interface GoalSettingsApplyOptions {
|
|
@@ -51,7 +49,6 @@ export async function showGoalSettings(
|
|
|
51
49
|
| "choose-automatic"
|
|
52
50
|
| "choose-no-progress"
|
|
53
51
|
| "set-visibility"
|
|
54
|
-
| "set-queue"
|
|
55
52
|
| "set-rpc";
|
|
56
53
|
const menu = defineMenu<undefined, Screen, Action, ExtensionCommandContext>({
|
|
57
54
|
start: invalid ? "invalid" : (options.initialScreen ?? "settings"),
|
|
@@ -87,14 +84,6 @@ export async function showGoalSettings(
|
|
|
87
84
|
values: ["Always", "After first goal"],
|
|
88
85
|
action: "set-visibility",
|
|
89
86
|
},
|
|
90
|
-
{
|
|
91
|
-
id: "experimentalGoals",
|
|
92
|
-
label: "Ordered goal queue",
|
|
93
|
-
description: "Enable experimental add, prioritize, skip, and drop-last workflows.",
|
|
94
|
-
currentValue: runtime.settings.experimental.goals ? "Experimental" : "Off",
|
|
95
|
-
values: ["Off", "Experimental"],
|
|
96
|
-
action: "set-queue",
|
|
97
|
-
},
|
|
98
87
|
{
|
|
99
88
|
id: "rpcEnabled",
|
|
100
89
|
label: "Managed run RPC",
|
|
@@ -116,7 +105,6 @@ export async function showGoalSettings(
|
|
|
116
105
|
`Automatic-work limit: ${formatAutomaticWork(runtime.settings.continuationLimits.automaticTurns)}`,
|
|
117
106
|
`No-progress guard: ${formatNoProgressProtection(runtime.settings.continuationLimits.noProgressTurns)}`,
|
|
118
107
|
`Goal tools: ${visibilityLabel(runtime.settings.toolVisibility)}`,
|
|
119
|
-
`Ordered goal queue: ${runtime.settings.experimental.goals ? "Experimental" : "Off"}`,
|
|
120
108
|
`Managed run RPC: ${runtime.settings.rpc.enabled ? "On" : "Off"}`,
|
|
121
109
|
],
|
|
122
110
|
hint: "back",
|
|
@@ -171,38 +159,6 @@ export async function showGoalSettings(
|
|
|
171
159
|
return { kind: "rejected" };
|
|
172
160
|
}
|
|
173
161
|
},
|
|
174
|
-
"set-queue": async ({ value }) => {
|
|
175
|
-
const enabled = value === "Experimental";
|
|
176
|
-
if (enabled === runtime.settings.experimental.goals) return { kind: "stay" };
|
|
177
|
-
const next = await nextQueueSettings(runtime, ctx, enabled);
|
|
178
|
-
if (!next) return { kind: "rejected" };
|
|
179
|
-
const wasFrozen = runtime.queueFrozen;
|
|
180
|
-
try {
|
|
181
|
-
applyGoalSettings(runtime, next, ctx, {
|
|
182
|
-
save: (settings) => (options.save ?? saveGoalSettings)(settings, settingsPath),
|
|
183
|
-
});
|
|
184
|
-
if (wasFrozen && !runtime.queueFrozen) {
|
|
185
|
-
try {
|
|
186
|
-
await options.onQueueUnfrozen?.(ctx);
|
|
187
|
-
} catch (error) {
|
|
188
|
-
notifyTerminal(
|
|
189
|
-
ctx.ui,
|
|
190
|
-
`Goal queue enabled, but automatic resume failed: ${safeTerminalText(formatError(error))}. Reopen /goal to retry.`,
|
|
191
|
-
"warning",
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
notifyTerminal(
|
|
196
|
-
ctx.ui,
|
|
197
|
-
`Ordered goal queue: ${enabled ? "Experimental" : "Off"}.`,
|
|
198
|
-
"info",
|
|
199
|
-
);
|
|
200
|
-
return { kind: "stay" };
|
|
201
|
-
} catch (error) {
|
|
202
|
-
notifySettingsFailure(ctx, settingsPath, error);
|
|
203
|
-
return { kind: "rejected" };
|
|
204
|
-
}
|
|
205
|
-
},
|
|
206
162
|
"set-rpc": async ({ value }) => {
|
|
207
163
|
const enabled = value === "On";
|
|
208
164
|
if (enabled === runtime.settings.rpc.enabled) return { kind: "stay" };
|
|
@@ -374,12 +330,11 @@ export function applyGoalSettings(
|
|
|
374
330
|
applyToolVisibility(runtime, snapshot.settings, next, ctx);
|
|
375
331
|
options.save?.(next);
|
|
376
332
|
fileSaved = options.save !== undefined;
|
|
377
|
-
applyQueueSetting(runtime, ctx);
|
|
378
333
|
const activeGoalId = runtime.activeGoal?.id;
|
|
379
334
|
const abortOwnedRun = activeGoalId !== undefined && runtime.agentRunGoalId === activeGoalId;
|
|
380
335
|
const pausedByAutomaticLimit = runtime.enforceAutomaticTurnLimit(ctx, abortOwnedRun);
|
|
381
336
|
if (!pausedByAutomaticLimit) runtime.enforceNoProgressLimit(ctx, abortOwnedRun);
|
|
382
|
-
if (runtime.activeGoal
|
|
337
|
+
if (runtime.activeGoal) {
|
|
383
338
|
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
384
339
|
}
|
|
385
340
|
} catch (error) {
|
|
@@ -463,35 +418,6 @@ async function resolveLimitSelection(
|
|
|
463
418
|
}
|
|
464
419
|
}
|
|
465
420
|
|
|
466
|
-
async function nextQueueSettings(
|
|
467
|
-
runtime: GoalRuntime,
|
|
468
|
-
ctx: ExtensionCommandContext,
|
|
469
|
-
enabled: boolean,
|
|
470
|
-
) {
|
|
471
|
-
if (runtime.settings.experimental.goals === enabled) return undefined;
|
|
472
|
-
if (enabled && !runtime.settings.experimental.goals) {
|
|
473
|
-
const confirmed = await ctx.ui.confirm(
|
|
474
|
-
"Enable experimental goal queue?",
|
|
475
|
-
"Queue behavior and persisted state may change between releases. Existing single-goal behavior remains available.",
|
|
476
|
-
);
|
|
477
|
-
if (!confirmed) return undefined;
|
|
478
|
-
}
|
|
479
|
-
if (
|
|
480
|
-
!enabled &&
|
|
481
|
-
(runtime.queuedGoals.length > 0 || runtime.pendingQueueAction !== undefined) &&
|
|
482
|
-
!(await ctx.ui.confirm(
|
|
483
|
-
"Freeze ordered goal queue?",
|
|
484
|
-
`Disabling the experiment preserves ${retainedGoalCount(runtime)} goal(s) but freezes automatic work until the setting is re-enabled. No goal data will be deleted.`,
|
|
485
|
-
))
|
|
486
|
-
) {
|
|
487
|
-
return undefined;
|
|
488
|
-
}
|
|
489
|
-
return {
|
|
490
|
-
...structuredClone(runtime.settings),
|
|
491
|
-
experimental: { goals: enabled },
|
|
492
|
-
} satisfies GoalSettings;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
421
|
function applyToolVisibility(
|
|
496
422
|
runtime: GoalRuntime,
|
|
497
423
|
previous: GoalSettings,
|
|
@@ -506,52 +432,12 @@ function applyToolVisibility(
|
|
|
506
432
|
);
|
|
507
433
|
}
|
|
508
434
|
|
|
509
|
-
function applyQueueSetting(runtime: GoalRuntime, ctx: ExtensionCommandContext) {
|
|
510
|
-
const hasQueueState = runtime.queuedGoals.length > 0 || runtime.pendingQueueAction !== undefined;
|
|
511
|
-
const shouldFreeze = !runtime.settings.experimental.goals && hasQueueState;
|
|
512
|
-
// Keep the freeze guard until the aborted Goal-owned run reaches agent_settled.
|
|
513
|
-
// Releasing it earlier lets the old agent_end pause newly resumed work.
|
|
514
|
-
if (runtime.queueFrozen && !shouldFreeze && runtime.queueFreezeAwaitingSettle) return;
|
|
515
|
-
if (runtime.queueFrozen === shouldFreeze) return;
|
|
516
|
-
const activeGoal = runtime.activeGoal?.status === "active" ? runtime.activeGoal : undefined;
|
|
517
|
-
const goalOwnedRun = activeGoal && runtime.agentRunGoalId === activeGoal.id;
|
|
518
|
-
if (shouldFreeze && activeGoal) {
|
|
519
|
-
if (goalOwnedRun) runtime.recordGoalUsage(activeGoal, ctx, false);
|
|
520
|
-
else {
|
|
521
|
-
const now = Date.now();
|
|
522
|
-
checkpointGoalActiveTime(activeGoal, now, false);
|
|
523
|
-
activeGoal.updatedAt = now;
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
runtime.queueFrozen = shouldFreeze;
|
|
527
|
-
if (runtime.activeGoal) runtime.persistGoal(runtime.activeGoal);
|
|
528
|
-
if (shouldFreeze) ctx.ui.setStatus(STATUS_KEY, "queue off");
|
|
529
|
-
else if (runtime.activeGoal) runtime.updateStatus(ctx, runtime.activeGoal);
|
|
530
|
-
else ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
531
|
-
if (!shouldFreeze) return;
|
|
532
|
-
|
|
533
|
-
runtime.clearGoalWaitTimer();
|
|
534
|
-
runtime.cancelContinuationWork();
|
|
535
|
-
runtime.clearGoalRecovery();
|
|
536
|
-
runtime.clearBudgetWrapUp();
|
|
537
|
-
if (goalOwnedRun) {
|
|
538
|
-
runtime.blockStaleGoalToolCalls();
|
|
539
|
-
runtime.guardAbortGoalId = activeGoal.id;
|
|
540
|
-
runtime.queueFreezeAwaitingSettle = true;
|
|
541
|
-
runtime.clearAgentRun();
|
|
542
|
-
abortCurrentTurn(ctx);
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
|
|
546
435
|
function restorePersistedRuntime(runtime: GoalRuntime, ctx: ExtensionCommandContext) {
|
|
547
436
|
if (runtime.activeGoal) {
|
|
548
437
|
runtime.persistGoal(runtime.activeGoal);
|
|
549
|
-
|
|
550
|
-
else runtime.updateStatus(ctx, runtime.activeGoal);
|
|
438
|
+
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
551
439
|
runtime.restoreGoalWaitTimer(ctx);
|
|
552
|
-
return;
|
|
553
440
|
}
|
|
554
|
-
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
555
441
|
}
|
|
556
442
|
|
|
557
443
|
async function confirmLowerActiveLimit(
|
|
@@ -612,14 +498,6 @@ function visibilityLabel(value: GoalSettings["toolVisibility"]) {
|
|
|
612
498
|
return value === "always" ? "Always" : "After first goal";
|
|
613
499
|
}
|
|
614
500
|
|
|
615
|
-
function retainedGoalCount(runtime: GoalRuntime) {
|
|
616
|
-
return (
|
|
617
|
-
(runtime.activeGoal ? 1 : 0) +
|
|
618
|
-
runtime.queuedGoals.length +
|
|
619
|
-
(runtime.pendingQueueAction?.kind === "prioritize" ? 1 : 0)
|
|
620
|
-
);
|
|
621
|
-
}
|
|
622
|
-
|
|
623
501
|
function notifySettingsFailure(ctx: ExtensionCommandContext, settingsPath: string, error: unknown) {
|
|
624
502
|
const path = safeTerminalText(settingsPath);
|
|
625
503
|
const detail = safeTerminalText(formatError(error));
|