@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.
Files changed (39) hide show
  1. package/README.md +41 -166
  2. package/dist/chunks/{chunk-QWIUWKRW.js → chunk-VJ5LR26S.js} +57 -77
  3. package/dist/chunks/chunk-VJ5LR26S.js.map +7 -0
  4. package/dist/chunks/{chunk-UQRRW4CY.js → chunk-WQIA35U2.js} +6 -5
  5. package/dist/chunks/chunk-WQIA35U2.js.map +7 -0
  6. package/dist/chunks/{menu-WEFDREGC.js → menu-JMGWQZIT.js} +7 -22
  7. package/dist/chunks/menu-JMGWQZIT.js.map +7 -0
  8. package/dist/chunks/{settings-ui-MVYTTBHW.js → settings-ui-JG3PFRB5.js} +6 -18
  9. package/dist/chunks/settings-ui-JG3PFRB5.js.map +7 -0
  10. package/dist/index.ts +92 -126
  11. package/dist/index.ts.map +2 -2
  12. package/docs/managing-goals.md +47 -0
  13. package/docs/settings.md +91 -0
  14. package/package.json +55 -55
  15. package/src/accounting.ts +62 -66
  16. package/src/command-registration.ts +104 -102
  17. package/src/command.ts +101 -112
  18. package/src/commands.ts +465 -501
  19. package/src/errors.ts +127 -122
  20. package/src/goal-contract.ts +64 -64
  21. package/src/goal.ts +25 -25
  22. package/src/lifecycle.ts +615 -628
  23. package/src/markers.ts +6 -6
  24. package/src/menu.ts +565 -591
  25. package/src/persistence.ts +177 -192
  26. package/src/prompts.ts +53 -67
  27. package/src/run-protocol.ts +299 -325
  28. package/src/runtime.ts +1422 -1495
  29. package/src/safety.ts +51 -51
  30. package/src/settings-ui.ts +366 -393
  31. package/src/settings.ts +134 -147
  32. package/src/tool-policy.ts +6 -6
  33. package/src/tools.ts +360 -371
  34. package/src/wait.ts +47 -51
  35. package/src/workflow-mutex.ts +68 -68
  36. package/dist/chunks/chunk-QWIUWKRW.js.map +0 -7
  37. package/dist/chunks/chunk-UQRRW4CY.js.map +0 -7
  38. package/dist/chunks/menu-WEFDREGC.js.map +0 -7
  39. package/dist/chunks/settings-ui-MVYTTBHW.js.map +0 -7
@@ -2,11 +2,11 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import { validateObjective } from "./command.js";
3
3
  import type { GoalCommandController } from "./commands.js";
4
4
  import {
5
- formatError,
6
- type GoalRuntime,
7
- type GoalStateSnapshot,
8
- isTerminalGoalStatus,
9
- type StatusContext,
5
+ formatError,
6
+ type GoalRuntime,
7
+ type GoalStateSnapshot,
8
+ isTerminalGoalStatus,
9
+ type StatusContext,
10
10
  } from "./runtime.js";
11
11
 
12
12
  export const GOAL_RUN_START_CHANNEL = "pi-goal:start";
@@ -15,376 +15,350 @@ export const GOAL_RUN_CANCEL_CHANNEL = "pi-goal:cancel";
15
15
  const RUN_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/u;
16
16
  const MAX_CANCEL_REASON_LENGTH = 1_000;
17
17
 
18
- export type GoalRunStatus = Exclude<GoalStateSnapshot["status"], "queued">;
18
+ export type GoalRunStatus = GoalStateSnapshot["status"];
19
19
  export type GoalRunErrorCode =
20
- | "RPC_DISABLED"
21
- | "INVALID_REQUEST"
22
- | "NO_ACTIVE_SESSION"
23
- | "RUN_ID_IN_USE"
24
- | "RUN_NOT_FOUND"
25
- | "GOAL_ALREADY_EXISTS"
26
- | "ACTIVATION_FAILED"
27
- | "SUPERSEDED";
20
+ | "RPC_DISABLED"
21
+ | "INVALID_REQUEST"
22
+ | "NO_ACTIVE_SESSION"
23
+ | "RUN_ID_IN_USE"
24
+ | "RUN_NOT_FOUND"
25
+ | "GOAL_ALREADY_EXISTS"
26
+ | "ACTIVATION_FAILED"
27
+ | "SUPERSEDED";
28
28
 
29
29
  export type GoalRunEvent =
30
- | {
31
- type: "state";
32
- runId: string;
33
- goalId: string;
34
- status: GoalRunStatus;
35
- summary?: string;
36
- reason?: string;
37
- }
38
- | {
39
- type: "error";
40
- runId: string;
41
- operation: "start" | "cancel";
42
- error: { code: GoalRunErrorCode; message: string };
43
- };
30
+ | {
31
+ type: "state";
32
+ runId: string;
33
+ goalId: string;
34
+ status: GoalRunStatus;
35
+ summary?: string;
36
+ reason?: string;
37
+ }
38
+ | {
39
+ type: "error";
40
+ runId: string;
41
+ operation: "start" | "cancel";
42
+ error: { code: GoalRunErrorCode; message: string };
43
+ };
44
44
 
45
45
  interface GoalRunStartPayload {
46
- runId: string;
47
- objective: string;
48
- tokenBudget?: number;
46
+ runId: string;
47
+ objective: string;
48
+ tokenBudget?: number;
49
49
  }
50
50
 
51
51
  interface BoundSession {
52
- generation: number;
53
- ctx: StatusContext;
52
+ generation: number;
53
+ ctx: StatusContext;
54
54
  }
55
55
 
56
56
  interface ManagedRun {
57
- runId: string;
58
- generation: number;
59
- goalId?: string;
60
- lastStatus?: GoalRunStatus;
61
- closed: boolean;
62
- cancelRequested: boolean;
63
- cancelReason?: string;
57
+ runId: string;
58
+ generation: number;
59
+ goalId?: string;
60
+ lastStatus?: GoalRunStatus;
61
+ closed: boolean;
62
+ cancelRequested: boolean;
63
+ cancelReason?: string;
64
64
  }
65
65
 
66
66
  export function goalRunEventChannel(runId: string) {
67
- return `pi-goal:event:${runId}`;
67
+ return `pi-goal:event:${runId}`;
68
68
  }
69
69
 
70
70
  function isPayloadRecord(data: unknown): data is object {
71
- if (!data || typeof data !== "object") return false;
72
- try {
73
- return !Array.isArray(data);
74
- } catch {
75
- return false;
76
- }
71
+ if (!data || typeof data !== "object") return false;
72
+ try {
73
+ return !Array.isArray(data);
74
+ } catch {
75
+ return false;
76
+ }
77
77
  }
78
78
 
79
79
  function readPayloadProperty(
80
- data: object,
81
- key: "runId" | "objective" | "tokenBudget" | "reason",
80
+ data: object,
81
+ key: "runId" | "objective" | "tokenBudget" | "reason",
82
82
  ): { ok: true; value: unknown } | { ok: false } {
83
- try {
84
- return { ok: true, value: Reflect.get(data, key) };
85
- } catch {
86
- return { ok: false };
87
- }
83
+ try {
84
+ return { ok: true, value: Reflect.get(data, key) };
85
+ } catch {
86
+ return { ok: false };
87
+ }
88
88
  }
89
89
 
90
90
  function parseRunId(data: unknown): string | undefined {
91
- if (!isPayloadRecord(data)) return undefined;
92
- const runId = readPayloadProperty(data, "runId");
93
- return runId.ok && typeof runId.value === "string" && RUN_ID_PATTERN.test(runId.value)
94
- ? runId.value
95
- : undefined;
91
+ if (!isPayloadRecord(data)) return undefined;
92
+ const runId = readPayloadProperty(data, "runId");
93
+ return runId.ok && typeof runId.value === "string" && RUN_ID_PATTERN.test(runId.value) ? runId.value : undefined;
96
94
  }
97
95
 
98
96
  function currentActiveGoal(runtime: GoalRuntime) {
99
- return runtime.activeGoal;
97
+ return runtime.activeGoal;
100
98
  }
101
99
 
102
100
  function parseStartPayload(data: unknown): string | Omit<GoalRunStartPayload, "runId"> {
103
- if (!isPayloadRecord(data)) return "start payload must be an object";
104
- const objectiveValue = readPayloadProperty(data, "objective");
105
- if (!objectiveValue.ok || typeof objectiveValue.value !== "string") {
106
- return "objective must be a string";
107
- }
108
- const objective = objectiveValue.value.trim();
109
- const objectiveError = validateObjective(objective);
110
- if (objectiveError) return objectiveError;
111
- const tokenBudgetValue = readPayloadProperty(data, "tokenBudget");
112
- if (
113
- !tokenBudgetValue.ok ||
114
- (tokenBudgetValue.value !== undefined &&
115
- (typeof tokenBudgetValue.value !== "number" ||
116
- !Number.isFinite(tokenBudgetValue.value) ||
117
- !Number.isSafeInteger(tokenBudgetValue.value) ||
118
- tokenBudgetValue.value <= 0))
119
- ) {
120
- return "tokenBudget must be a positive integer";
121
- }
122
- return { objective, tokenBudget: tokenBudgetValue.value as number | undefined };
101
+ if (!isPayloadRecord(data)) return "start payload must be an object";
102
+ const objectiveValue = readPayloadProperty(data, "objective");
103
+ if (!objectiveValue.ok || typeof objectiveValue.value !== "string") {
104
+ return "objective must be a string";
105
+ }
106
+ const objective = objectiveValue.value.trim();
107
+ const objectiveError = validateObjective(objective);
108
+ if (objectiveError) return objectiveError;
109
+ const tokenBudgetValue = readPayloadProperty(data, "tokenBudget");
110
+ if (
111
+ !tokenBudgetValue.ok ||
112
+ (tokenBudgetValue.value !== undefined &&
113
+ (typeof tokenBudgetValue.value !== "number" ||
114
+ !Number.isFinite(tokenBudgetValue.value) ||
115
+ !Number.isSafeInteger(tokenBudgetValue.value) ||
116
+ tokenBudgetValue.value <= 0))
117
+ ) {
118
+ return "tokenBudget must be a positive integer";
119
+ }
120
+ return { objective, tokenBudget: tokenBudgetValue.value as number | undefined };
123
121
  }
124
122
 
125
123
  function parseCancelReason(data: unknown): string | undefined | { error: string } {
126
- if (!isPayloadRecord(data)) return { error: "cancel payload must be an object" };
127
- const reasonValue = readPayloadProperty(data, "reason");
128
- if (
129
- !reasonValue.ok ||
130
- (reasonValue.value !== undefined && typeof reasonValue.value !== "string")
131
- ) {
132
- return { error: "reason must be a string" };
133
- }
134
- if (reasonValue.value === undefined) return undefined;
135
- const reason = reasonValue.value.trim();
136
- if (reason.length > MAX_CANCEL_REASON_LENGTH) {
137
- return { error: `reason must be at most ${MAX_CANCEL_REASON_LENGTH} characters` };
138
- }
139
- return reason || undefined;
124
+ if (!isPayloadRecord(data)) return { error: "cancel payload must be an object" };
125
+ const reasonValue = readPayloadProperty(data, "reason");
126
+ if (!reasonValue.ok || (reasonValue.value !== undefined && typeof reasonValue.value !== "string")) {
127
+ return { error: "reason must be a string" };
128
+ }
129
+ if (reasonValue.value === undefined) return undefined;
130
+ const reason = reasonValue.value.trim();
131
+ if (reason.length > MAX_CANCEL_REASON_LENGTH) {
132
+ return { error: `reason must be at most ${MAX_CANCEL_REASON_LENGTH} characters` };
133
+ }
134
+ return reason || undefined;
140
135
  }
141
136
 
142
137
  export class GoalRunController {
143
- private readonly runtime: GoalRuntime;
144
- private readonly commands: GoalCommandController;
145
- private generation = 0;
146
- private session?: BoundSession;
147
- private run?: ManagedRun;
148
- private readonly usedRunIds = new Set<string>();
138
+ private readonly runtime: GoalRuntime;
139
+ private readonly commands: GoalCommandController;
140
+ private generation = 0;
141
+ private session?: BoundSession;
142
+ private run?: ManagedRun;
143
+ private readonly usedRunIds = new Set<string>();
149
144
 
150
- constructor(runtime: GoalRuntime, commands: GoalCommandController) {
151
- this.runtime = runtime;
152
- this.commands = commands;
153
- this.runtime.setGoalStateSink((snapshot) => this.handleGoalState(snapshot));
154
- }
145
+ constructor(runtime: GoalRuntime, commands: GoalCommandController) {
146
+ this.runtime = runtime;
147
+ this.commands = commands;
148
+ this.runtime.setGoalStateSink((snapshot) => this.handleGoalState(snapshot));
149
+ }
155
150
 
156
- register(pi: ExtensionAPI) {
157
- pi.events.on(GOAL_RUN_START_CHANNEL, (data) => this.handleStart(data));
158
- pi.events.on(GOAL_RUN_CANCEL_CHANNEL, (data) => {
159
- this.handleCancel(data);
160
- });
161
- }
151
+ register(pi: ExtensionAPI) {
152
+ pi.events.on(GOAL_RUN_START_CHANNEL, (data) => this.handleStart(data));
153
+ pi.events.on(GOAL_RUN_CANCEL_CHANNEL, (data) => {
154
+ this.handleCancel(data);
155
+ });
156
+ }
162
157
 
163
- bindSession(ctx: StatusContext) {
164
- this.generation += 1;
165
- this.session = { generation: this.generation, ctx };
166
- this.closeCurrentRun();
167
- this.usedRunIds.clear();
168
- }
158
+ bindSession(ctx: StatusContext) {
159
+ this.generation += 1;
160
+ this.session = { generation: this.generation, ctx };
161
+ this.closeCurrentRun();
162
+ this.usedRunIds.clear();
163
+ }
169
164
 
170
- unbindSession() {
171
- this.generation += 1;
172
- this.session = undefined;
173
- this.closeCurrentRun();
174
- this.usedRunIds.clear();
175
- }
165
+ unbindSession() {
166
+ this.generation += 1;
167
+ this.session = undefined;
168
+ this.closeCurrentRun();
169
+ this.usedRunIds.clear();
170
+ }
176
171
 
177
- private async handleStart(data: unknown) {
178
- const runId = parseRunId(data);
179
- if (!runId) return;
180
- const session = this.session;
181
- if (!session) {
182
- this.emitError(runId, "start", "NO_ACTIVE_SESSION", "No active pi-goal session.");
183
- return;
184
- }
185
- if (!this.runtime.settings.rpc.enabled) {
186
- this.emitError(runId, "start", "RPC_DISABLED", "Managed run RPC is disabled.");
187
- return;
188
- }
189
- if (this.usedRunIds.has(runId)) {
190
- this.emitError(runId, "start", "RUN_ID_IN_USE", "runId was already used in this session.");
191
- return;
192
- }
193
- const parsed = parseStartPayload(data);
194
- if (typeof parsed === "string") {
195
- this.emitError(runId, "start", "INVALID_REQUEST", parsed);
196
- return;
197
- }
198
- if (this.session !== session || this.generation !== session.generation) {
199
- this.emitError(
200
- runId,
201
- "start",
202
- "SUPERSEDED",
203
- "The pi-goal session changed while validating the request.",
204
- );
205
- return;
206
- }
207
- if (this.usedRunIds.has(runId)) {
208
- this.emitError(runId, "start", "RUN_ID_IN_USE", "runId was already used in this session.");
209
- return;
210
- }
211
- if (this.runtime.activeGoal || (this.run && !this.run.closed)) {
212
- this.emitError(runId, "start", "GOAL_ALREADY_EXISTS", "A Goal already exists.");
213
- return;
214
- }
172
+ private async handleStart(data: unknown) {
173
+ const runId = parseRunId(data);
174
+ if (!runId) return;
175
+ const session = this.session;
176
+ if (!session) {
177
+ this.emitError(runId, "start", "NO_ACTIVE_SESSION", "No active pi-goal session.");
178
+ return;
179
+ }
180
+ if (!this.runtime.settings.rpc.enabled) {
181
+ this.emitError(runId, "start", "RPC_DISABLED", "Managed run RPC is disabled.");
182
+ return;
183
+ }
184
+ if (this.usedRunIds.has(runId)) {
185
+ this.emitError(runId, "start", "RUN_ID_IN_USE", "runId was already used in this session.");
186
+ return;
187
+ }
188
+ const parsed = parseStartPayload(data);
189
+ if (typeof parsed === "string") {
190
+ this.emitError(runId, "start", "INVALID_REQUEST", parsed);
191
+ return;
192
+ }
193
+ if (this.session !== session || this.generation !== session.generation) {
194
+ this.emitError(runId, "start", "SUPERSEDED", "The pi-goal session changed while validating the request.");
195
+ return;
196
+ }
197
+ if (this.usedRunIds.has(runId)) {
198
+ this.emitError(runId, "start", "RUN_ID_IN_USE", "runId was already used in this session.");
199
+ return;
200
+ }
201
+ if (this.runtime.activeGoal || (this.run && !this.run.closed)) {
202
+ this.emitError(runId, "start", "GOAL_ALREADY_EXISTS", "A Goal already exists.");
203
+ return;
204
+ }
215
205
 
216
- const run: ManagedRun = {
217
- runId,
218
- generation: session.generation,
219
- closed: false,
220
- cancelRequested: false,
221
- };
222
- this.run = run;
223
- this.usedRunIds.add(runId);
206
+ const run: ManagedRun = {
207
+ runId,
208
+ generation: session.generation,
209
+ closed: false,
210
+ cancelRequested: false,
211
+ };
212
+ this.run = run;
213
+ this.usedRunIds.add(runId);
224
214
 
225
- try {
226
- const activation = await this.commands.startGoal(
227
- parsed.objective,
228
- parsed.tokenBudget,
229
- session.ctx,
230
- (goal) => {
231
- if (this.ownsRun(run, session.generation)) run.goalId = goal.id;
232
- },
233
- () => this.ownsRun(run, session.generation),
234
- );
235
- if (activation?.kind === "busy" && this.ownsRun(run, session.generation)) {
236
- this.closeCurrentRun();
237
- this.emitError(
238
- runId,
239
- "start",
240
- "ACTIVATION_FAILED",
241
- "Another workflow is active in this session. End it before starting Goal.",
242
- );
243
- return;
244
- }
245
- } catch (error) {
246
- if (this.ownsRun(run, session.generation)) {
247
- this.closeCurrentRun();
248
- this.emitError(
249
- runId,
250
- "start",
251
- "ACTIVATION_FAILED",
252
- `Goal activation failed: ${formatError(error)}`,
253
- );
254
- }
255
- return;
256
- }
215
+ try {
216
+ const activation = await this.commands.startGoal(
217
+ parsed.objective,
218
+ parsed.tokenBudget,
219
+ session.ctx,
220
+ (goal) => {
221
+ if (this.ownsRun(run, session.generation)) run.goalId = goal.id;
222
+ },
223
+ () => this.ownsRun(run, session.generation),
224
+ );
225
+ if (activation?.kind === "busy" && this.ownsRun(run, session.generation)) {
226
+ this.closeCurrentRun();
227
+ this.emitError(
228
+ runId,
229
+ "start",
230
+ "ACTIVATION_FAILED",
231
+ "Another workflow is active in this session. End it before starting Goal.",
232
+ );
233
+ return;
234
+ }
235
+ } catch (error) {
236
+ if (this.ownsRun(run, session.generation)) {
237
+ this.closeCurrentRun();
238
+ this.emitError(runId, "start", "ACTIVATION_FAILED", `Goal activation failed: ${formatError(error)}`);
239
+ }
240
+ return;
241
+ }
257
242
 
258
- if (!this.ownsRun(run, session.generation)) return;
259
- if (!run.goalId) {
260
- this.closeCurrentRun();
261
- this.emitError(runId, "start", "ACTIVATION_FAILED", "Goal activation did not create a Goal.");
262
- return;
263
- }
264
- if (currentActiveGoal(this.runtime)?.id !== run.goalId) {
265
- this.closeCurrentRun();
266
- this.emitError(runId, "start", "SUPERSEDED", "The managed Goal was superseded.");
267
- return;
268
- }
269
- if (run.cancelRequested) this.cancelActiveRun(run, session, run.cancelReason);
270
- }
243
+ if (!this.ownsRun(run, session.generation)) return;
244
+ if (!run.goalId) {
245
+ this.closeCurrentRun();
246
+ this.emitError(runId, "start", "ACTIVATION_FAILED", "Goal activation did not create a Goal.");
247
+ return;
248
+ }
249
+ if (currentActiveGoal(this.runtime)?.id !== run.goalId) {
250
+ this.closeCurrentRun();
251
+ this.emitError(runId, "start", "SUPERSEDED", "The managed Goal was superseded.");
252
+ return;
253
+ }
254
+ if (run.cancelRequested) this.cancelActiveRun(run, session, run.cancelReason);
255
+ }
271
256
 
272
- private handleCancel(data: unknown) {
273
- const runId = parseRunId(data);
274
- if (!runId) return;
275
- const session = this.session;
276
- if (!session) {
277
- this.emitError(runId, "cancel", "NO_ACTIVE_SESSION", "No active pi-goal session.");
278
- return;
279
- }
280
- const reason = parseCancelReason(data);
281
- if (reason && typeof reason === "object") {
282
- this.emitError(runId, "cancel", "INVALID_REQUEST", reason.error);
283
- return;
284
- }
285
- const run = this.run;
286
- if (!run || run.closed || run.runId !== runId || run.generation !== session.generation) {
287
- this.emitError(runId, "cancel", "RUN_NOT_FOUND", "No active managed run matches runId.");
288
- return;
289
- }
290
- if (!run.goalId) {
291
- run.cancelRequested = true;
292
- run.cancelReason = reason;
293
- return;
294
- }
295
- this.cancelActiveRun(run, session, reason);
296
- }
257
+ private handleCancel(data: unknown) {
258
+ const runId = parseRunId(data);
259
+ if (!runId) return;
260
+ const session = this.session;
261
+ if (!session) {
262
+ this.emitError(runId, "cancel", "NO_ACTIVE_SESSION", "No active pi-goal session.");
263
+ return;
264
+ }
265
+ const reason = parseCancelReason(data);
266
+ if (reason && typeof reason === "object") {
267
+ this.emitError(runId, "cancel", "INVALID_REQUEST", reason.error);
268
+ return;
269
+ }
270
+ const run = this.run;
271
+ if (!run || run.closed || run.runId !== runId || run.generation !== session.generation) {
272
+ this.emitError(runId, "cancel", "RUN_NOT_FOUND", "No active managed run matches runId.");
273
+ return;
274
+ }
275
+ if (!run.goalId) {
276
+ run.cancelRequested = true;
277
+ run.cancelReason = reason;
278
+ return;
279
+ }
280
+ this.cancelActiveRun(run, session, reason);
281
+ }
297
282
 
298
- private cancelActiveRun(run: ManagedRun, session: BoundSession, reason: string | undefined) {
299
- if (!this.ownsRun(run, session.generation)) return;
300
- const goal = this.runtime.activeGoal;
301
- if (!goal || goal.id !== run.goalId) {
302
- this.closeCurrentRun();
303
- this.emitError(run.runId, "cancel", "SUPERSEDED", "The managed Goal was superseded.");
304
- return;
305
- }
306
- if (goal.status !== "active") {
307
- this.closeCurrentRun();
308
- this.emitError(run.runId, "cancel", "RUN_NOT_FOUND", "The managed run is no longer active.");
309
- return;
310
- }
311
- this.runtime.setTerminalReason(goal.id, reason ?? "goal cancelled by managed run");
312
- this.commands.pauseGoal(session.ctx);
313
- }
283
+ private cancelActiveRun(run: ManagedRun, session: BoundSession, reason: string | undefined) {
284
+ if (!this.ownsRun(run, session.generation)) return;
285
+ const goal = this.runtime.activeGoal;
286
+ if (!goal || goal.id !== run.goalId) {
287
+ this.closeCurrentRun();
288
+ this.emitError(run.runId, "cancel", "SUPERSEDED", "The managed Goal was superseded.");
289
+ return;
290
+ }
291
+ if (goal.status !== "active") {
292
+ this.closeCurrentRun();
293
+ this.emitError(run.runId, "cancel", "RUN_NOT_FOUND", "The managed run is no longer active.");
294
+ return;
295
+ }
296
+ this.runtime.setTerminalReason(goal.id, reason ?? "goal cancelled by managed run");
297
+ this.commands.pauseGoal(session.ctx);
298
+ }
314
299
 
315
- private handleGoalState(snapshot: GoalStateSnapshot) {
316
- const run = this.run;
317
- if (!run || run.closed || !run.goalId) return;
318
- if (run.goalId !== snapshot.goalId) {
319
- if (currentActiveGoal(this.runtime)?.id !== snapshot.goalId) return;
320
- this.publishStateEvent(run, {
321
- goalId: run.goalId,
322
- status: "cleared",
323
- reason: "managed Goal superseded by another Goal",
324
- });
325
- return;
326
- }
327
- if (snapshot.status === "queued" || run.lastStatus === snapshot.status) return;
328
- this.publishStateEvent(run, snapshot);
329
- }
300
+ private handleGoalState(snapshot: GoalStateSnapshot) {
301
+ const run = this.run;
302
+ if (!run || run.closed || !run.goalId) return;
303
+ if (run.goalId !== snapshot.goalId) {
304
+ if (currentActiveGoal(this.runtime)?.id !== snapshot.goalId) return;
305
+ this.publishStateEvent(run, {
306
+ goalId: run.goalId,
307
+ status: "cleared",
308
+ reason: "managed Goal superseded by another Goal",
309
+ });
310
+ return;
311
+ }
312
+ if (run.lastStatus === snapshot.status) return;
313
+ this.publishStateEvent(run, snapshot);
314
+ }
330
315
 
331
- private publishStateEvent(
332
- run: ManagedRun,
333
- snapshot: Pick<GoalStateSnapshot, "goalId" | "status" | "summary" | "reason">,
334
- ) {
335
- const status = snapshot.status;
336
- if (status === "queued") return;
337
- run.lastStatus = status;
338
- const event: GoalRunEvent = {
339
- type: "state",
340
- runId: run.runId,
341
- goalId: snapshot.goalId,
342
- status,
343
- ...(snapshot.summary ? { summary: snapshot.summary } : {}),
344
- ...(snapshot.reason ? { reason: snapshot.reason } : {}),
345
- };
346
- if (!isTerminalGoalStatus(status)) {
347
- this.runtime.pi.events.emit(goalRunEventChannel(run.runId), event);
348
- return;
349
- }
316
+ private publishStateEvent(
317
+ run: ManagedRun,
318
+ snapshot: Pick<GoalStateSnapshot, "goalId" | "status" | "summary" | "reason">,
319
+ ) {
320
+ const status = snapshot.status;
321
+ run.lastStatus = status;
322
+ const event: GoalRunEvent = {
323
+ type: "state",
324
+ runId: run.runId,
325
+ goalId: snapshot.goalId,
326
+ status,
327
+ ...(snapshot.summary ? { summary: snapshot.summary } : {}),
328
+ ...(snapshot.reason ? { reason: snapshot.reason } : {}),
329
+ };
330
+ if (!isTerminalGoalStatus(status)) {
331
+ this.runtime.pi.events.emit(goalRunEventChannel(run.runId), event);
332
+ return;
333
+ }
350
334
 
351
- run.closed = true;
352
- this.run = undefined;
353
- const generation = run.generation;
354
- // Active stays synchronous so a listener can cancel before kickoff. Terminal
355
- // publication waits until the transition finishes, preventing listener work
356
- // from being overwritten by the old transition's remaining UI or cleanup.
357
- queueMicrotask(() => {
358
- if (this.generation !== generation) return;
359
- this.runtime.pi.events.emit(goalRunEventChannel(run.runId), event);
360
- });
361
- }
335
+ run.closed = true;
336
+ this.run = undefined;
337
+ const generation = run.generation;
338
+ // Active stays synchronous so a listener can cancel before kickoff. Terminal
339
+ // publication waits until the transition finishes, preventing listener work
340
+ // from being overwritten by the old transition's remaining UI or cleanup.
341
+ queueMicrotask(() => {
342
+ if (this.generation !== generation) return;
343
+ this.runtime.pi.events.emit(goalRunEventChannel(run.runId), event);
344
+ });
345
+ }
362
346
 
363
- private emitError(
364
- runId: string,
365
- operation: "start" | "cancel",
366
- code: GoalRunErrorCode,
367
- message: string,
368
- ) {
369
- this.runtime.pi.events.emit(goalRunEventChannel(runId), {
370
- type: "error",
371
- runId,
372
- operation,
373
- error: { code, message },
374
- } satisfies GoalRunEvent);
375
- }
347
+ private emitError(runId: string, operation: "start" | "cancel", code: GoalRunErrorCode, message: string) {
348
+ this.runtime.pi.events.emit(goalRunEventChannel(runId), {
349
+ type: "error",
350
+ runId,
351
+ operation,
352
+ error: { code, message },
353
+ } satisfies GoalRunEvent);
354
+ }
376
355
 
377
- private ownsRun(run: ManagedRun, generation: number) {
378
- return (
379
- this.session?.generation === generation &&
380
- this.generation === generation &&
381
- this.run === run &&
382
- !run.closed
383
- );
384
- }
356
+ private ownsRun(run: ManagedRun, generation: number) {
357
+ return this.session?.generation === generation && this.generation === generation && this.run === run && !run.closed;
358
+ }
385
359
 
386
- private closeCurrentRun() {
387
- if (this.run) this.run.closed = true;
388
- this.run = undefined;
389
- }
360
+ private closeCurrentRun() {
361
+ if (this.run) this.run.closed = true;
362
+ this.run = undefined;
363
+ }
390
364
  }