@narumitw/pi-goal 0.52.2 → 0.53.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 +268 -119
- package/dist/chunks/{chunk-ODVXYZXS.ts → chunk-W7AFYZ6J.ts} +148 -9
- package/dist/chunks/chunk-W7AFYZ6J.ts.map +7 -0
- package/dist/chunks/{menu-YILEIJE6.ts → menu-73TCS5P6.ts} +2 -2
- package/dist/chunks/{settings-ui-HMSJEEZN.ts → settings-ui-Y5LMEFEH.ts} +45 -31
- package/dist/chunks/settings-ui-Y5LMEFEH.ts.map +7 -0
- package/dist/index.ts +214 -72
- package/dist/index.ts.map +2 -2
- package/package.json +5 -5
- package/src/commands.ts +95 -36
- package/src/lifecycle.ts +114 -41
- package/src/run-protocol.ts +11 -1
- package/src/runtime.ts +104 -8
- package/src/settings-ui.ts +47 -30
- package/src/tool-policy.ts +2 -0
- package/src/tools.ts +12 -0
- package/src/workflow-mutex.ts +89 -0
- package/dist/chunks/chunk-ODVXYZXS.ts.map +0 -7
- package/dist/chunks/settings-ui-HMSJEEZN.ts.map +0 -7
- /package/dist/chunks/{menu-YILEIJE6.ts.map → menu-73TCS5P6.ts.map} +0 -0
package/src/runtime.ts
CHANGED
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
} from "./settings.js";
|
|
32
32
|
import { GoalToolPolicy, type GoalToolVisibilitySnapshot } from "./tool-policy.js";
|
|
33
33
|
import { type GoalWait, GoalWaitTimer } from "./wait.js";
|
|
34
|
+
import { WorkflowMutex, type WorkflowMutexOwner } from "./workflow-mutex.js";
|
|
34
35
|
|
|
35
36
|
export {
|
|
36
37
|
GOAL_BLOCKED_TOOL,
|
|
@@ -230,6 +231,9 @@ export class GoalRuntime {
|
|
|
230
231
|
guardAbortGoalId?: string;
|
|
231
232
|
staleGoalToolCallsBlocked = false;
|
|
232
233
|
readonly toolPolicy: GoalToolPolicy;
|
|
234
|
+
private readonly workflowMutex: WorkflowMutex;
|
|
235
|
+
private workflowOwner?: WorkflowMutexOwner;
|
|
236
|
+
private workflowSession?: object;
|
|
233
237
|
pendingGoalPromptMarkers = new Map<string, PendingGoalPrompt>();
|
|
234
238
|
claimedGoalPromptMarkers = new Map<string, string>();
|
|
235
239
|
cancelledGoalPromptMarkers = new Map<string, string>();
|
|
@@ -243,9 +247,69 @@ export class GoalRuntime {
|
|
|
243
247
|
|
|
244
248
|
constructor(pi: ExtensionAPI) {
|
|
245
249
|
this.pi = pi;
|
|
250
|
+
this.workflowMutex = new WorkflowMutex(pi);
|
|
246
251
|
this.toolPolicy = new GoalToolPolicy(pi);
|
|
247
252
|
}
|
|
248
253
|
|
|
254
|
+
bindWorkflowSession(session: object) {
|
|
255
|
+
this.workflowSession = session;
|
|
256
|
+
this.workflowOwner = undefined;
|
|
257
|
+
this.workflowMutex.bindSession(session);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
unbindWorkflowSession(session: object) {
|
|
261
|
+
if (this.workflowSession !== session) return;
|
|
262
|
+
this.workflowMutex.unbindSession(session);
|
|
263
|
+
this.workflowOwner = undefined;
|
|
264
|
+
this.workflowSession = undefined;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
acquireWorkflow(session?: unknown) {
|
|
268
|
+
if (session && typeof session === "object" && this.workflowSession !== session) {
|
|
269
|
+
this.bindWorkflowSession(session);
|
|
270
|
+
}
|
|
271
|
+
if (this.workflowMutex.isOwner(this.workflowOwner)) return true;
|
|
272
|
+
const owner = this.workflowMutex.acquire();
|
|
273
|
+
if (!owner) return false;
|
|
274
|
+
this.workflowOwner = owner;
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
ownsWorkflow(goal: ActiveGoal | undefined = this.activeGoal) {
|
|
279
|
+
return goal?.status === "active" && this.workflowMutex.isOwner(this.workflowOwner);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
releaseWorkflow() {
|
|
283
|
+
const owner = this.workflowOwner;
|
|
284
|
+
this.workflowMutex.release(owner);
|
|
285
|
+
if (!this.workflowMutex.isOwner(owner)) this.workflowOwner = undefined;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
beginTemporaryWorkflowAccess(session?: unknown) {
|
|
289
|
+
if (session && typeof session === "object" && this.workflowSession !== session) {
|
|
290
|
+
this.bindWorkflowSession(session);
|
|
291
|
+
}
|
|
292
|
+
const alreadyOwned = this.workflowMutex.isOwner(this.workflowOwner);
|
|
293
|
+
if (!alreadyOwned && !this.acquireWorkflow()) return undefined;
|
|
294
|
+
let released = false;
|
|
295
|
+
return () => {
|
|
296
|
+
if (released) return;
|
|
297
|
+
released = true;
|
|
298
|
+
if (!alreadyOwned) this.releaseWorkflow();
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
withTemporaryWorkflowAccess(action: () => void, session?: unknown) {
|
|
303
|
+
const release = this.beginTemporaryWorkflowAccess(session);
|
|
304
|
+
if (!release) return false;
|
|
305
|
+
try {
|
|
306
|
+
action();
|
|
307
|
+
return true;
|
|
308
|
+
} finally {
|
|
309
|
+
release();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
249
313
|
hasLegacyQueueInterface() {
|
|
250
314
|
return this.legacyExperimentalGoalsSetting || this.legacyQueueState !== undefined;
|
|
251
315
|
}
|
|
@@ -344,6 +408,7 @@ export class GoalRuntime {
|
|
|
344
408
|
}
|
|
345
409
|
|
|
346
410
|
requestContinuation(goal: ActiveGoal) {
|
|
411
|
+
if (!this.ownsWorkflow(goal)) return false;
|
|
347
412
|
if (goal.waiting || this.hasContinuationWorkForGoal(goal.id)) return false;
|
|
348
413
|
const marker = continuationMarker(goal);
|
|
349
414
|
this.continuationIntent = {
|
|
@@ -357,6 +422,10 @@ export class GoalRuntime {
|
|
|
357
422
|
|
|
358
423
|
dispatchContinuationIfSettled(ctx: StatusContext) {
|
|
359
424
|
const intent = this.continuationIntent;
|
|
425
|
+
if (!this.ownsWorkflow()) {
|
|
426
|
+
this.cancelContinuationWork();
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
360
429
|
if (!intent) return false;
|
|
361
430
|
if (this.activeGoal?.status === "active" && !this.toolPolicy.toolsAvailable()) {
|
|
362
431
|
this.pauseGoalForUnavailableTools(ctx);
|
|
@@ -402,7 +471,7 @@ export class GoalRuntime {
|
|
|
402
471
|
|
|
403
472
|
enterGoalWait(ctx: StatusContext, goalId: string, waiting: GoalWait) {
|
|
404
473
|
const goal = this.activeGoal;
|
|
405
|
-
if (!goal || goal.id !== goalId || goal
|
|
474
|
+
if (!goal || goal.id !== goalId || !this.ownsWorkflow(goal)) return undefined;
|
|
406
475
|
this.recordGoalUsage(goal, ctx, false);
|
|
407
476
|
this.cancelContinuationWork();
|
|
408
477
|
this.clearGoalRecoveryForGoal(goal.id);
|
|
@@ -421,7 +490,7 @@ export class GoalRuntime {
|
|
|
421
490
|
|
|
422
491
|
clearGoalWait(ctx: StatusContext, goalId: string) {
|
|
423
492
|
const goal = this.activeGoal;
|
|
424
|
-
if (!goal || goal.id !== goalId || goal
|
|
493
|
+
if (!goal || goal.id !== goalId || !this.ownsWorkflow(goal) || !goal.waiting) return false;
|
|
425
494
|
this.clearGoalWaitTimer();
|
|
426
495
|
const { waiting: _waiting, ...nextGoal } = goal;
|
|
427
496
|
const now = Date.now();
|
|
@@ -436,6 +505,7 @@ export class GoalRuntime {
|
|
|
436
505
|
restoreGoalWaitTimer(ctx: StatusContext) {
|
|
437
506
|
this.clearGoalWaitTimer();
|
|
438
507
|
const goal = this.activeGoal;
|
|
508
|
+
if (!this.ownsWorkflow(goal)) return false;
|
|
439
509
|
const resumeAt = goal?.status === "active" ? goal.waiting?.resumeAt : undefined;
|
|
440
510
|
if (!goal || resumeAt === undefined) return false;
|
|
441
511
|
this.scheduleGoalWaitTimer(ctx, goal.id, resumeAt);
|
|
@@ -444,6 +514,7 @@ export class GoalRuntime {
|
|
|
444
514
|
|
|
445
515
|
dispatchDueGoalWait(ctx: StatusContext) {
|
|
446
516
|
const goal = this.activeGoal;
|
|
517
|
+
if (!this.ownsWorkflow(goal)) return false;
|
|
447
518
|
const waiting = goal?.status === "active" ? goal.waiting : undefined;
|
|
448
519
|
const resumeAt = waiting?.resumeAt;
|
|
449
520
|
if (!goal || !waiting || resumeAt === undefined) return false;
|
|
@@ -480,7 +551,13 @@ export class GoalRuntime {
|
|
|
480
551
|
private scheduleGoalWaitTimer(ctx: StatusContext, goalId: string, wakeAt: number) {
|
|
481
552
|
const generation = this.menuGeneration;
|
|
482
553
|
this.goalWaitTimer.schedule(wakeAt, () => {
|
|
483
|
-
if (
|
|
554
|
+
if (
|
|
555
|
+
generation !== this.menuGeneration ||
|
|
556
|
+
this.activeGoal?.id !== goalId ||
|
|
557
|
+
!this.ownsWorkflow(this.activeGoal)
|
|
558
|
+
) {
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
484
561
|
try {
|
|
485
562
|
this.dispatchDueGoalWait(ctx);
|
|
486
563
|
} catch (error) {
|
|
@@ -550,6 +627,8 @@ export class GoalRuntime {
|
|
|
550
627
|
this.cancelContinuationWork();
|
|
551
628
|
this.clearGoalRecoveryForGoal(goal.id);
|
|
552
629
|
this.clearBudgetWrapUp();
|
|
630
|
+
this.blockStaleGoalToolCalls();
|
|
631
|
+
abortCurrentTurn(ctx);
|
|
553
632
|
status = "budget_limited";
|
|
554
633
|
terminalReason = request.reason;
|
|
555
634
|
break;
|
|
@@ -618,6 +697,7 @@ export class GoalRuntime {
|
|
|
618
697
|
this.persistGoal(stoppedGoal);
|
|
619
698
|
if (this.activeGoal?.id === stoppedGoal.id && this.activeGoal.status === stoppedGoal.status) {
|
|
620
699
|
this.updateStatus(ctx, stoppedGoal);
|
|
700
|
+
this.releaseWorkflow();
|
|
621
701
|
}
|
|
622
702
|
return stoppedGoal;
|
|
623
703
|
}
|
|
@@ -676,6 +756,7 @@ export class GoalRuntime {
|
|
|
676
756
|
}
|
|
677
757
|
|
|
678
758
|
queueBudgetWrapUp(ctx: StatusContext, goal: ActiveGoal) {
|
|
759
|
+
if (!this.ownsWorkflow(goal)) return false;
|
|
679
760
|
if (!this.budgetWrapUp || this.budgetWrapUp.goalId !== goal.id) {
|
|
680
761
|
this.budgetWrapUp = { goalId: goal.id, delivered: false };
|
|
681
762
|
}
|
|
@@ -872,9 +953,15 @@ export class GoalRuntime {
|
|
|
872
953
|
resetSafetyEpoch = true,
|
|
873
954
|
isCurrent?: () => boolean,
|
|
874
955
|
) {
|
|
956
|
+
if (this.activeGoal?.id !== goalId || !this.ownsWorkflow(this.activeGoal)) return false;
|
|
875
957
|
const pending = this.rememberPendingGoalPrompt(goalId, prompt, resetSafetyEpoch);
|
|
876
958
|
const sent = await sendPrompt(this.pi, ctx, pending.prompt, isCurrent);
|
|
877
|
-
if (
|
|
959
|
+
if (
|
|
960
|
+
!sent ||
|
|
961
|
+
(isCurrent && !isCurrent()) ||
|
|
962
|
+
this.activeGoal?.id !== goalId ||
|
|
963
|
+
!this.ownsWorkflow(this.activeGoal)
|
|
964
|
+
) {
|
|
878
965
|
this.pendingGoalPromptMarkers.delete(pending.marker);
|
|
879
966
|
return false;
|
|
880
967
|
}
|
|
@@ -898,7 +985,7 @@ export class GoalRuntime {
|
|
|
898
985
|
if (
|
|
899
986
|
generation !== this.menuGeneration ||
|
|
900
987
|
this.activeGoal?.id !== goalId ||
|
|
901
|
-
this.activeGoal
|
|
988
|
+
!this.ownsWorkflow(this.activeGoal)
|
|
902
989
|
) {
|
|
903
990
|
return;
|
|
904
991
|
}
|
|
@@ -1113,7 +1200,7 @@ export class GoalRuntime {
|
|
|
1113
1200
|
clearLegacyPersistedGoal(cwd);
|
|
1114
1201
|
}
|
|
1115
1202
|
|
|
1116
|
-
clearActiveGoal(ctx: StatusContext, reason = "goal cleared") {
|
|
1203
|
+
clearActiveGoal(ctx: StatusContext, reason = "goal cleared", releaseWorkflow = true) {
|
|
1117
1204
|
const clearedGoal = this.activeGoal;
|
|
1118
1205
|
this.clearGoalWaitTimer();
|
|
1119
1206
|
this.cancelContinuationWork();
|
|
@@ -1124,6 +1211,7 @@ export class GoalRuntime {
|
|
|
1124
1211
|
this.legacyQueueState = undefined;
|
|
1125
1212
|
this.clearPersistedGoal(ctx.cwd, clearedGoal, reason);
|
|
1126
1213
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
1214
|
+
if (releaseWorkflow) this.releaseWorkflow();
|
|
1127
1215
|
// Do not relock toolPolicy: after first activation, keep tools visible for the
|
|
1128
1216
|
// rest of this extension runtime to avoid repeated tool-schema churn.
|
|
1129
1217
|
}
|
|
@@ -1151,6 +1239,9 @@ export class GoalRuntime {
|
|
|
1151
1239
|
}
|
|
1152
1240
|
|
|
1153
1241
|
restoreSettingsApplicationState(snapshot: GoalSettingsRuntimeSnapshot) {
|
|
1242
|
+
if (snapshot.activeGoal?.status === "active" && !this.acquireWorkflow()) {
|
|
1243
|
+
throw new Error("another workflow became active before Goal settings could roll back");
|
|
1244
|
+
}
|
|
1154
1245
|
this.settings = structuredClone(snapshot.settings);
|
|
1155
1246
|
this.activeGoal = snapshot.activeGoal ? structuredClone(snapshot.activeGoal) : undefined;
|
|
1156
1247
|
this.legacyQueueState = snapshot.legacyQueueState
|
|
@@ -1408,11 +1499,16 @@ export function abortCurrentTurn(ctx: StatusContext) {
|
|
|
1408
1499
|
}
|
|
1409
1500
|
|
|
1410
1501
|
export function blocksStaleGoalToolCalls(status: GoalStatus) {
|
|
1411
|
-
return
|
|
1502
|
+
return (
|
|
1503
|
+
status === "paused" ||
|
|
1504
|
+
status === "blocked" ||
|
|
1505
|
+
status === "usage_limited" ||
|
|
1506
|
+
status === "budget_limited"
|
|
1507
|
+
);
|
|
1412
1508
|
}
|
|
1413
1509
|
|
|
1414
1510
|
export function isResumableGoalStatus(status: GoalStatus) {
|
|
1415
|
-
return blocksStaleGoalToolCalls(status)
|
|
1511
|
+
return blocksStaleGoalToolCalls(status);
|
|
1416
1512
|
}
|
|
1417
1513
|
|
|
1418
1514
|
export function stoppedStatusLabel(status: GoalStatus) {
|
package/src/settings-ui.ts
CHANGED
|
@@ -19,6 +19,8 @@ interface GoalSettingsApplyOptions {
|
|
|
19
19
|
save?: (settings: GoalSettings) => void;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
class WorkflowBusyError extends Error {}
|
|
23
|
+
|
|
22
24
|
type LimitField = "automaticTurns" | "noProgressTurns";
|
|
23
25
|
type LimitSelection = "unlimited" | "default" | "custom" | "off";
|
|
24
26
|
export async function showGoalSettings(
|
|
@@ -324,45 +326,59 @@ export function applyGoalSettings(
|
|
|
324
326
|
options: GoalSettingsApplyOptions = {},
|
|
325
327
|
) {
|
|
326
328
|
const snapshot = runtime.snapshotSettingsApplicationState();
|
|
329
|
+
const visibilityChanges = snapshot.settings.toolVisibility !== next.toolVisibility;
|
|
330
|
+
const releaseWorkflow = visibilityChanges
|
|
331
|
+
? runtime.beginTemporaryWorkflowAccess(ctx.sessionManager)
|
|
332
|
+
: () => undefined;
|
|
333
|
+
if (!releaseWorkflow) {
|
|
334
|
+
throw new WorkflowBusyError(
|
|
335
|
+
"Another workflow is active in this session. Goal tool visibility was not changed.",
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
327
339
|
let fileSaved = false;
|
|
328
340
|
try {
|
|
329
|
-
runtime.settings = structuredClone(next);
|
|
330
|
-
applyToolVisibility(runtime, snapshot.settings, next, ctx);
|
|
331
|
-
options.save?.(next);
|
|
332
|
-
fileSaved = options.save !== undefined;
|
|
333
|
-
const activeGoalId = runtime.activeGoal?.id;
|
|
334
|
-
const abortOwnedRun = activeGoalId !== undefined && runtime.agentRunGoalId === activeGoalId;
|
|
335
|
-
const pausedByAutomaticLimit = runtime.enforceAutomaticTurnLimit(ctx, abortOwnedRun);
|
|
336
|
-
if (!pausedByAutomaticLimit) runtime.enforceNoProgressLimit(ctx, abortOwnedRun);
|
|
337
|
-
if (runtime.activeGoal) {
|
|
338
|
-
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
339
|
-
}
|
|
340
|
-
} catch (error) {
|
|
341
|
-
const rollbackErrors: unknown[] = [];
|
|
342
341
|
try {
|
|
343
|
-
runtime.
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
342
|
+
applyToolVisibility(runtime, snapshot.settings, next, ctx);
|
|
343
|
+
runtime.settings = structuredClone(next);
|
|
344
|
+
options.save?.(next);
|
|
345
|
+
fileSaved = options.save !== undefined;
|
|
346
|
+
const activeGoalId = runtime.activeGoal?.id;
|
|
347
|
+
const abortOwnedRun = activeGoalId !== undefined && runtime.agentRunGoalId === activeGoalId;
|
|
348
|
+
const pausedByAutomaticLimit = runtime.enforceAutomaticTurnLimit(ctx, abortOwnedRun);
|
|
349
|
+
if (!pausedByAutomaticLimit) runtime.enforceNoProgressLimit(ctx, abortOwnedRun);
|
|
350
|
+
if (runtime.activeGoal) {
|
|
351
|
+
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
352
352
|
}
|
|
353
|
+
} catch (error) {
|
|
354
|
+
const rollbackErrors: unknown[] = [];
|
|
353
355
|
try {
|
|
354
|
-
|
|
356
|
+
runtime.restoreSettingsApplicationState(snapshot);
|
|
355
357
|
} catch (rollbackError) {
|
|
356
358
|
rollbackErrors.push(rollbackError);
|
|
357
359
|
}
|
|
360
|
+
if (fileSaved) {
|
|
361
|
+
try {
|
|
362
|
+
options.save?.(snapshot.settings);
|
|
363
|
+
} catch (rollbackError) {
|
|
364
|
+
rollbackErrors.push(rollbackError);
|
|
365
|
+
}
|
|
366
|
+
try {
|
|
367
|
+
restorePersistedRuntime(runtime, ctx);
|
|
368
|
+
} catch (rollbackError) {
|
|
369
|
+
rollbackErrors.push(rollbackError);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (rollbackErrors.length > 0) {
|
|
373
|
+
throw new AggregateError(
|
|
374
|
+
[error, ...rollbackErrors],
|
|
375
|
+
`pi-goal settings application failed and rollback was incomplete: ${formatError(error)}`,
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
throw error;
|
|
358
379
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
[error, ...rollbackErrors],
|
|
362
|
-
`pi-goal settings application failed and rollback was incomplete: ${formatError(error)}`,
|
|
363
|
-
);
|
|
364
|
-
}
|
|
365
|
-
throw error;
|
|
380
|
+
} finally {
|
|
381
|
+
releaseWorkflow();
|
|
366
382
|
}
|
|
367
383
|
}
|
|
368
384
|
|
|
@@ -424,6 +440,7 @@ function applyToolVisibility(
|
|
|
424
440
|
next: GoalSettings,
|
|
425
441
|
ctx: ExtensionCommandContext,
|
|
426
442
|
) {
|
|
443
|
+
if (previous.toolVisibility === next.toolVisibility) return;
|
|
427
444
|
runtime.toolPolicy.applyVisibilityChange(
|
|
428
445
|
previous.toolVisibility,
|
|
429
446
|
next.toolVisibility,
|
package/src/tool-policy.ts
CHANGED
package/src/tools.ts
CHANGED
|
@@ -100,6 +100,14 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
|
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
102
|
const completingDuringBudgetWrapUp = runtime.hasActiveBudgetWrapUp();
|
|
103
|
+
if (completedGoal.status === "active" && !runtime.ownsWorkflow(completedGoal)) {
|
|
104
|
+
const rejection = "Goal completion rejected: active Goal no longer owns its workflow.";
|
|
105
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
106
|
+
return {
|
|
107
|
+
content: toolContent(rejection),
|
|
108
|
+
details: completionDetails(goal, requestedGoalId, summary),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
103
111
|
if (!runtime.canRecordGoalUsage() && !completingDuringBudgetWrapUp) {
|
|
104
112
|
const rejection = "Goal completion rejected: current run does not own the active goal.";
|
|
105
113
|
notifyTerminal(ctx.ui, rejection, "warning");
|
|
@@ -237,6 +245,8 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
|
|
|
237
245
|
if (blockedGoal.status !== "active") {
|
|
238
246
|
return reject(`goal is ${blockedGoal.status}, not active`);
|
|
239
247
|
}
|
|
248
|
+
if (!runtime.ownsWorkflow(blockedGoal))
|
|
249
|
+
return reject("active Goal no longer owns its workflow");
|
|
240
250
|
if (!reason) return reject("reason is empty");
|
|
241
251
|
if (reason.length > MAX_BLOCKER_REASON_LENGTH) return reject("reason is too long");
|
|
242
252
|
if (!evidence) return reject("evidence is empty");
|
|
@@ -317,6 +327,8 @@ export function registerGoalTools(pi: ExtensionAPI, runtime: GoalRuntime) {
|
|
|
317
327
|
if (activeGoal.status !== "active") {
|
|
318
328
|
return reject(`goal is ${activeGoal.status}, not active`);
|
|
319
329
|
}
|
|
330
|
+
if (!runtime.ownsWorkflow(activeGoal))
|
|
331
|
+
return reject("active Goal no longer owns its workflow");
|
|
320
332
|
if (activeGoal.waiting) return reject("goal is already waiting");
|
|
321
333
|
if (!reason) return reject("reason is empty");
|
|
322
334
|
if (reason.length > MAX_GOAL_WAIT_REASON_LENGTH) return reject("reason is too long");
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export const WORKFLOW_MUTEX_CHANNEL = "workflow:mutex:v1";
|
|
4
|
+
export const AGENT_WORKFLOW_GROUP = "agent-workflow";
|
|
5
|
+
|
|
6
|
+
export type WorkflowMutexOwner = symbol;
|
|
7
|
+
|
|
8
|
+
interface WorkflowMutexAttemptV1 {
|
|
9
|
+
session: object;
|
|
10
|
+
group: string;
|
|
11
|
+
busy: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class WorkflowMutex {
|
|
15
|
+
private session: object | undefined;
|
|
16
|
+
private readonly heldGroups = new Map<string, WorkflowMutexOwner>();
|
|
17
|
+
private generation = 0;
|
|
18
|
+
private readonly pi: Pick<ExtensionAPI, "events">;
|
|
19
|
+
|
|
20
|
+
constructor(pi: Pick<ExtensionAPI, "events">) {
|
|
21
|
+
this.pi = pi;
|
|
22
|
+
pi.events.on(WORKFLOW_MUTEX_CHANNEL, (payload) => {
|
|
23
|
+
this.answer(payload);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
bindSession(session: object): void {
|
|
28
|
+
this.generation += 1;
|
|
29
|
+
this.heldGroups.clear();
|
|
30
|
+
this.session = session;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
unbindSession(session: object): void {
|
|
34
|
+
if (this.session !== session) return;
|
|
35
|
+
this.generation += 1;
|
|
36
|
+
this.heldGroups.clear();
|
|
37
|
+
this.session = undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
acquire(group = AGENT_WORKFLOW_GROUP): WorkflowMutexOwner | undefined {
|
|
41
|
+
const session = this.session;
|
|
42
|
+
const generation = this.generation;
|
|
43
|
+
if (!session || this.heldGroups.has(group)) return undefined;
|
|
44
|
+
|
|
45
|
+
const attempt: WorkflowMutexAttemptV1 = { session, group, busy: false };
|
|
46
|
+
try {
|
|
47
|
+
this.pi.events.emit(WORKFLOW_MUTEX_CHANNEL, attempt);
|
|
48
|
+
} catch {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
if (
|
|
52
|
+
this.session !== session ||
|
|
53
|
+
this.generation !== generation ||
|
|
54
|
+
attempt.session !== session ||
|
|
55
|
+
attempt.group !== group ||
|
|
56
|
+
attempt.busy !== false ||
|
|
57
|
+
this.heldGroups.has(group)
|
|
58
|
+
) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const owner = Symbol(group);
|
|
63
|
+
this.heldGroups.set(group, owner);
|
|
64
|
+
return owner;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
isOwner(owner: WorkflowMutexOwner | undefined, group = AGENT_WORKFLOW_GROUP): boolean {
|
|
68
|
+
return owner !== undefined && this.heldGroups.get(group) === owner;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
release(owner: WorkflowMutexOwner | undefined, group = AGENT_WORKFLOW_GROUP): void {
|
|
72
|
+
if (!this.isOwner(owner, group)) return;
|
|
73
|
+
this.heldGroups.delete(group);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private answer(payload: unknown): void {
|
|
77
|
+
try {
|
|
78
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return;
|
|
79
|
+
const attempt = payload as Partial<WorkflowMutexAttemptV1>;
|
|
80
|
+
if (attempt.session !== this.session) return;
|
|
81
|
+
if (attempt.group !== AGENT_WORKFLOW_GROUP) return;
|
|
82
|
+
if (typeof attempt.busy !== "boolean") return;
|
|
83
|
+
if (!this.heldGroups.has(attempt.group)) return;
|
|
84
|
+
attempt.busy = true;
|
|
85
|
+
} catch {
|
|
86
|
+
// Protocol listeners must ignore unusual objects without interrupting sibling listeners.
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|