@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/run-protocol.ts
CHANGED
|
@@ -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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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 =
|
|
18
|
+
export type GoalRunStatus = GoalStateSnapshot["status"];
|
|
19
19
|
export type GoalRunErrorCode =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
runId: string;
|
|
47
|
+
objective: string;
|
|
48
|
+
tokenBudget?: number;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
interface BoundSession {
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
generation: number;
|
|
53
|
+
ctx: StatusContext;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
interface ManagedRun {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
67
|
+
return `pi-goal:event:${runId}`;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
function isPayloadRecord(data: unknown): data is object {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
81
|
-
|
|
80
|
+
data: object,
|
|
81
|
+
key: "runId" | "objective" | "tokenBudget" | "reason",
|
|
82
82
|
): { ok: true; value: unknown } | { ok: false } {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
97
|
+
return runtime.activeGoal;
|
|
100
98
|
}
|
|
101
99
|
|
|
102
100
|
function parseStartPayload(data: unknown): string | Omit<GoalRunStartPayload, "runId"> {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
165
|
+
unbindSession() {
|
|
166
|
+
this.generation += 1;
|
|
167
|
+
this.session = undefined;
|
|
168
|
+
this.closeCurrentRun();
|
|
169
|
+
this.usedRunIds.clear();
|
|
170
|
+
}
|
|
176
171
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
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
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
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
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
-
|
|
378
|
-
|
|
379
|
-
|
|
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
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
360
|
+
private closeCurrentRun() {
|
|
361
|
+
if (this.run) this.run.closed = true;
|
|
362
|
+
this.run = undefined;
|
|
363
|
+
}
|
|
390
364
|
}
|