@narumitw/pi-goal 0.52.3 → 0.53.1
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 +52 -12
- package/dist/chunks/{chunk-ODVXYZXS.ts → chunk-4GOI2LMD.ts} +155 -16
- package/dist/chunks/chunk-4GOI2LMD.ts.map +7 -0
- package/dist/chunks/{menu-YILEIJE6.ts → menu-MMY32N6V.ts} +2 -2
- package/dist/chunks/{settings-ui-HMSJEEZN.ts → settings-ui-NXPYYRGD.ts} +45 -35
- package/dist/chunks/settings-ui-NXPYYRGD.ts.map +7 -0
- package/dist/index.ts +268 -80
- package/dist/index.ts.map +4 -4
- package/package.json +1 -1
- package/src/commands.ts +95 -36
- package/src/goal-contract.ts +62 -0
- package/src/lifecycle.ts +125 -48
- package/src/persistence.ts +0 -6
- package/src/prompts.ts +7 -1
- package/src/run-protocol.ts +11 -1
- package/src/runtime.ts +104 -8
- package/src/settings-ui.ts +47 -34
- 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-MMY32N6V.ts.map} +0 -0
package/dist/index.ts
CHANGED
|
@@ -12,8 +12,8 @@ import {
|
|
|
12
12
|
STATUS_KEY,
|
|
13
13
|
abortCurrentTurn,
|
|
14
14
|
blocksStaleGoalToolCalls,
|
|
15
|
+
buildGoalContextPrompt,
|
|
15
16
|
buildGoalPrompt,
|
|
16
|
-
buildGoalSystemPrompt,
|
|
17
17
|
buildObjectiveUpdatedPrompt,
|
|
18
18
|
buildResumePrompt,
|
|
19
19
|
buildWaitingResumePrompt,
|
|
@@ -40,7 +40,7 @@ import {
|
|
|
40
40
|
stoppedStatusLabel,
|
|
41
41
|
transitionGoal,
|
|
42
42
|
validateObjective
|
|
43
|
-
} from "./chunks/chunk-
|
|
43
|
+
} from "./chunks/chunk-4GOI2LMD.js";
|
|
44
44
|
import {
|
|
45
45
|
DEFAULT_GOAL_SETTINGS,
|
|
46
46
|
currentTokenTotal,
|
|
@@ -60,10 +60,10 @@ import {
|
|
|
60
60
|
// src/command-registration.ts
|
|
61
61
|
function registerGoalCommand(pi, runtime, commands, options = {}) {
|
|
62
62
|
const loadGoalManager = cachedModuleLoader(
|
|
63
|
-
options.loadGoalManager ?? (() => import("./chunks/menu-
|
|
63
|
+
options.loadGoalManager ?? (() => import("./chunks/menu-MMY32N6V.js"))
|
|
64
64
|
);
|
|
65
65
|
const loadGoalSettings = cachedModuleLoader(
|
|
66
|
-
options.loadGoalSettings ?? (() => import("./chunks/settings-ui-
|
|
66
|
+
options.loadGoalSettings ?? (() => import("./chunks/settings-ui-NXPYYRGD.js"))
|
|
67
67
|
);
|
|
68
68
|
pi.registerCommand("goal", {
|
|
69
69
|
description: "Run a goal to completion: /goal [--tokens 100k] <goal_to_complete>",
|
|
@@ -195,12 +195,16 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
197
|
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
198
|
+
const retainedOwner = this.runtime.ownsWorkflow(existingGoal);
|
|
199
|
+
if (!this.runtime.acquireWorkflow(ctx.sessionManager)) return this.reportWorkflowBusy(ctx);
|
|
200
|
+
const acquiredForRequest = !retainedOwner;
|
|
198
201
|
const goalToolVisibilityBeforeActivation = this.runtime.toolPolicy.snapshot();
|
|
199
202
|
try {
|
|
200
203
|
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
201
204
|
} catch (error) {
|
|
202
205
|
notifyTerminal(ctx.ui, `Cannot start /goal: ${formatError(error)}`, "error");
|
|
203
206
|
if (existingGoal?.status === "active") this.runtime.pauseGoalForUnavailableTools(ctx);
|
|
207
|
+
else if (acquiredForRequest) this.runtime.releaseWorkflow();
|
|
204
208
|
return;
|
|
205
209
|
}
|
|
206
210
|
this.runtime.clearGoalWaitTimer();
|
|
@@ -213,7 +217,7 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
213
217
|
const startedGoal = this.runtime.activeGoal;
|
|
214
218
|
onActivated?.(startedGoal);
|
|
215
219
|
if (!legacyQueueBeforeActivation) this.runtime.persistGoal(startedGoal);
|
|
216
|
-
if (this.runtime.activeGoal?.id !== startedGoal.id || this.runtime.activeGoal
|
|
220
|
+
if (this.runtime.activeGoal?.id !== startedGoal.id || !this.runtime.ownsWorkflow(this.runtime.activeGoal)) {
|
|
217
221
|
return;
|
|
218
222
|
}
|
|
219
223
|
this.runtime.updateStatus(ctx, startedGoal);
|
|
@@ -242,12 +246,12 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
242
246
|
this.runtime.updateStatus(ctx, existingGoal);
|
|
243
247
|
this.runtime.restoreGoalWaitTimer(ctx);
|
|
244
248
|
} else if (existingGoal.status === "active") {
|
|
245
|
-
this.runtime.
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
249
|
+
this.runtime.activeGoal = existingGoal;
|
|
250
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
251
|
+
this.runtime.persistGoal(existingGoal);
|
|
252
|
+
if (this.runtime.activeGoal?.id === existingGoal.id) {
|
|
253
|
+
this.runtime.updateStatus(ctx, existingGoal);
|
|
254
|
+
}
|
|
251
255
|
} else {
|
|
252
256
|
this.runtime.activeGoal = existingGoal;
|
|
253
257
|
if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
|
|
@@ -267,15 +271,18 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
267
271
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
268
272
|
ctx.ui.setStatus(STATUS_KEY, void 0);
|
|
269
273
|
} else {
|
|
270
|
-
this.runtime.clearActiveGoal(ctx);
|
|
274
|
+
this.runtime.clearActiveGoal(ctx, "goal activation rolled back", false);
|
|
271
275
|
}
|
|
272
276
|
}
|
|
273
277
|
if (rolledBackStartedGoal) {
|
|
274
278
|
this.runtime.toolPolicy.restore(goalToolVisibilityBeforeActivation);
|
|
279
|
+
if (acquiredForRequest && !this.runtime.ownsWorkflow(existingGoal)) {
|
|
280
|
+
this.runtime.releaseWorkflow();
|
|
281
|
+
}
|
|
275
282
|
}
|
|
276
283
|
return;
|
|
277
284
|
}
|
|
278
|
-
if (this.runtime.activeGoal?.id !== startedGoal.id || this.runtime.activeGoal
|
|
285
|
+
if (this.runtime.activeGoal?.id !== startedGoal.id || !this.runtime.ownsWorkflow(this.runtime.activeGoal)) {
|
|
279
286
|
return;
|
|
280
287
|
}
|
|
281
288
|
const automaticLimit = this.runtime.settings.continuationLimits.automaticTurns;
|
|
@@ -329,11 +336,16 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
329
336
|
);
|
|
330
337
|
return;
|
|
331
338
|
}
|
|
339
|
+
if (!this.runtime.acquireWorkflow(ctx.sessionManager)) {
|
|
340
|
+
this.reportWorkflowBusy(ctx);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
332
343
|
const goalToolVisibilityBeforeActivation = this.runtime.toolPolicy.snapshot();
|
|
333
344
|
try {
|
|
334
345
|
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
335
346
|
} catch (error) {
|
|
336
347
|
notifyTerminal(ctx.ui, `Cannot resume /goal: ${formatError(error)}`, "error");
|
|
348
|
+
this.runtime.releaseWorkflow();
|
|
337
349
|
return;
|
|
338
350
|
}
|
|
339
351
|
const stoppedGoal = this.runtime.activeGoal;
|
|
@@ -346,8 +358,9 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
346
358
|
transitionGoal(nextGoalInstance(this.runtime.activeGoal), "active")
|
|
347
359
|
);
|
|
348
360
|
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
349
|
-
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
350
361
|
if (this.runtime.activeGoal.status !== "active") {
|
|
362
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
363
|
+
this.runtime.releaseWorkflow();
|
|
351
364
|
notifyTerminal(
|
|
352
365
|
ctx.ui,
|
|
353
366
|
`Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`,
|
|
@@ -355,6 +368,8 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
355
368
|
);
|
|
356
369
|
return;
|
|
357
370
|
}
|
|
371
|
+
if (!this.runtime.ownsWorkflow(this.runtime.activeGoal)) return;
|
|
372
|
+
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
358
373
|
const resumedGoal = this.runtime.activeGoal;
|
|
359
374
|
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
360
375
|
ctx,
|
|
@@ -370,9 +385,11 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
370
385
|
this.runtime.blockStaleGoalToolCalls();
|
|
371
386
|
}
|
|
372
387
|
this.runtime.toolPolicy.restore(goalToolVisibilityBeforeActivation);
|
|
388
|
+
this.runtime.releaseWorkflow();
|
|
373
389
|
}
|
|
374
390
|
return;
|
|
375
391
|
}
|
|
392
|
+
if (!this.runtime.ownsWorkflow(resumedGoal)) return;
|
|
376
393
|
const automaticLimit = this.runtime.settings.continuationLimits.automaticTurns;
|
|
377
394
|
notifyTerminal(
|
|
378
395
|
ctx.ui,
|
|
@@ -384,6 +401,10 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
384
401
|
const waitingGoal = this.runtime.activeGoal;
|
|
385
402
|
const waiting = waitingGoal?.waiting;
|
|
386
403
|
if (waitingGoal?.status !== "active" || !waiting) return;
|
|
404
|
+
if (!this.runtime.acquireWorkflow(ctx.sessionManager)) {
|
|
405
|
+
this.reportWorkflowBusy(ctx);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
387
408
|
try {
|
|
388
409
|
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
389
410
|
} catch (error) {
|
|
@@ -406,6 +427,7 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
406
427
|
}
|
|
407
428
|
return;
|
|
408
429
|
}
|
|
430
|
+
if (!this.runtime.ownsWorkflow(resumedGoal)) return;
|
|
409
431
|
notifyTerminal(ctx.ui, `Goal resumed from waiting: ${waitingGoal.text}`, "info");
|
|
410
432
|
}
|
|
411
433
|
clearGoal(ctx) {
|
|
@@ -439,42 +461,59 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
439
461
|
notifyTerminal(ctx.ui, "No active goal. Use /goal <objective> to start one.", "warning");
|
|
440
462
|
return;
|
|
441
463
|
}
|
|
442
|
-
this.runtime.
|
|
443
|
-
const
|
|
464
|
+
const currentGoal = this.runtime.activeGoal;
|
|
465
|
+
const effectiveTokenBudget = tokenBudget ?? currentGoal.tokenBudget;
|
|
466
|
+
if (currentGoal.status === "budget_limited" && effectiveTokenBudget !== void 0 && effectiveTokenBudget <= currentGoal.tokensUsed) {
|
|
467
|
+
notifyTerminal(
|
|
468
|
+
ctx.ui,
|
|
469
|
+
`Goal token budget is still reached: ${formatBudget(currentGoal)}. Raise the budget above current usage before editing.`,
|
|
470
|
+
"warning"
|
|
471
|
+
);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const previousStatus = currentGoal.status;
|
|
475
|
+
const intendsActive = editedGoalStatus(previousStatus) === "active";
|
|
476
|
+
const retainedOwner = this.runtime.ownsWorkflow(currentGoal);
|
|
477
|
+
if (intendsActive && !this.runtime.acquireWorkflow(ctx.sessionManager)) {
|
|
478
|
+
this.reportWorkflowBusy(ctx);
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const acquiredForEdit = intendsActive && !retainedOwner;
|
|
482
|
+
const goalToolVisibilityBeforeActivation = intendsActive ? this.runtime.toolPolicy.snapshot() : void 0;
|
|
483
|
+
if (intendsActive) {
|
|
484
|
+
try {
|
|
485
|
+
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
486
|
+
} catch (error) {
|
|
487
|
+
notifyTerminal(ctx.ui, `Cannot reactivate /goal: ${formatError(error)}`, "error");
|
|
488
|
+
if (currentGoal.status === "active") this.runtime.pauseGoalForUnavailableTools(ctx);
|
|
489
|
+
else if (acquiredForEdit) this.runtime.releaseWorkflow();
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
this.runtime.recordGoalUsage(currentGoal, ctx);
|
|
494
|
+
const previousGoal = { ...currentGoal };
|
|
444
495
|
this.runtime.clearGoalWaitTimer();
|
|
445
496
|
this.runtime.cancelContinuationWork();
|
|
446
497
|
this.runtime.clearGoalRecovery();
|
|
447
498
|
this.runtime.clearBudgetWrapUp();
|
|
448
|
-
const
|
|
449
|
-
const rotatedGoal = nextGoalInstance(this.runtime.activeGoal);
|
|
499
|
+
const rotatedGoal = nextGoalInstance(currentGoal);
|
|
450
500
|
const transitionedGoal = transitionGoal(
|
|
451
501
|
{
|
|
452
502
|
...rotatedGoal,
|
|
453
503
|
text: objective,
|
|
454
|
-
tokenBudget:
|
|
504
|
+
tokenBudget: effectiveTokenBudget,
|
|
455
505
|
waiting: void 0
|
|
456
506
|
},
|
|
457
507
|
editedGoalStatus(previousStatus)
|
|
458
508
|
);
|
|
459
509
|
const nextGoal = transitionedGoal.status === "active" ? queueGoalSafetyReset(transitionedGoal) : transitionedGoal;
|
|
460
|
-
const goalToolVisibilityBeforeActivation = nextGoal.status === "active" ? this.runtime.toolPolicy.snapshot() : void 0;
|
|
461
|
-
if (nextGoal.status === "active") {
|
|
462
|
-
try {
|
|
463
|
-
this.runtime.toolPolicy.prepareActivation(this.runtime.settings.toolVisibility, ctx);
|
|
464
|
-
} catch (error) {
|
|
465
|
-
notifyTerminal(ctx.ui, `Cannot reactivate /goal: ${formatError(error)}`, "error");
|
|
466
|
-
if (this.runtime.activeGoal?.status === "active") {
|
|
467
|
-
this.runtime.pauseGoalForUnavailableTools(ctx);
|
|
468
|
-
}
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
510
|
this.runtime.activeGoal = nextGoal;
|
|
473
511
|
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
474
512
|
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
475
513
|
const editedGoal = this.runtime.activeGoal;
|
|
476
514
|
if (!editedGoal) return;
|
|
477
515
|
if (editedGoal.status === "active") {
|
|
516
|
+
if (!this.runtime.ownsWorkflow(editedGoal)) return;
|
|
478
517
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
479
518
|
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
480
519
|
ctx,
|
|
@@ -490,12 +529,12 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
490
529
|
this.runtime.updateStatus(ctx, previousGoal);
|
|
491
530
|
this.runtime.restoreGoalWaitTimer(ctx);
|
|
492
531
|
} else if (previousStatus === "active") {
|
|
493
|
-
this.runtime.
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}
|
|
532
|
+
this.runtime.activeGoal = previousGoal;
|
|
533
|
+
this.runtime.clearStaleGoalToolCallBlock();
|
|
534
|
+
this.runtime.persistGoal(previousGoal);
|
|
535
|
+
if (this.runtime.activeGoal?.id === previousGoal.id) {
|
|
536
|
+
this.runtime.updateStatus(ctx, previousGoal);
|
|
537
|
+
}
|
|
499
538
|
} else {
|
|
500
539
|
this.runtime.activeGoal = previousGoal;
|
|
501
540
|
if (blocksStaleGoalToolCalls(this.runtime.activeGoal.status)) {
|
|
@@ -509,16 +548,29 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
509
548
|
if (goalToolVisibilityBeforeActivation) {
|
|
510
549
|
this.runtime.toolPolicy.restore(goalToolVisibilityBeforeActivation);
|
|
511
550
|
}
|
|
551
|
+
if (acquiredForEdit && previousStatus !== "active") {
|
|
552
|
+
this.runtime.releaseWorkflow();
|
|
553
|
+
}
|
|
512
554
|
}
|
|
513
555
|
return;
|
|
514
556
|
}
|
|
557
|
+
if (!this.runtime.ownsWorkflow(editedGoal)) return;
|
|
515
558
|
} else if (blocksStaleGoalToolCalls(editedGoal.status)) {
|
|
516
559
|
this.runtime.blockStaleGoalToolCalls();
|
|
517
560
|
} else {
|
|
518
561
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
519
562
|
}
|
|
563
|
+
if (editedGoal.status !== "active" && (retainedOwner || acquiredForEdit)) {
|
|
564
|
+
this.runtime.releaseWorkflow();
|
|
565
|
+
}
|
|
520
566
|
notifyTerminal(ctx.ui, `Goal updated: ${objective}`, "info");
|
|
521
567
|
}
|
|
568
|
+
reportWorkflowBusy(ctx) {
|
|
569
|
+
const message = "Another workflow is active in this session. End it before starting Goal.";
|
|
570
|
+
if (ctx.mode === "print" || ctx.mode === "json") throw new Error(message);
|
|
571
|
+
notifyTerminal(ctx.ui, message, "warning");
|
|
572
|
+
return { kind: "busy" };
|
|
573
|
+
}
|
|
522
574
|
showGoal(ctx) {
|
|
523
575
|
if (!this.runtime.activeGoal) {
|
|
524
576
|
ctx.ui.setStatus(STATUS_KEY, void 0);
|
|
@@ -553,11 +605,58 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
553
605
|
}
|
|
554
606
|
};
|
|
555
607
|
|
|
608
|
+
// src/goal-contract.ts
|
|
609
|
+
var GOAL_CONTRACT_MESSAGE_TYPE = "goal-contract";
|
|
610
|
+
var GOAL_CONTRACT_VERSION = 1;
|
|
611
|
+
function createGoalContextContract(goal2) {
|
|
612
|
+
return {
|
|
613
|
+
role: "custom",
|
|
614
|
+
customType: GOAL_CONTRACT_MESSAGE_TYPE,
|
|
615
|
+
content: buildGoalContextPrompt(goal2),
|
|
616
|
+
display: false,
|
|
617
|
+
details: { version: GOAL_CONTRACT_VERSION, goalId: goal2.id },
|
|
618
|
+
timestamp: 0
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
function reconcileGoalContextContract(messages, goal2) {
|
|
622
|
+
const summaryBoundary = leadingSummaryBoundary(messages);
|
|
623
|
+
const expected = createGoalContextContract(goal2);
|
|
624
|
+
const contractIndexes = messages.flatMap(
|
|
625
|
+
(message, index) => unwrapMessage(message).customType === GOAL_CONTRACT_MESSAGE_TYPE ? [index] : []
|
|
626
|
+
);
|
|
627
|
+
if (contractIndexes.length === 1 && contractIndexes[0] === summaryBoundary && unwrapMessage(messages[summaryBoundary]).content === expected.content) {
|
|
628
|
+
return messages;
|
|
629
|
+
}
|
|
630
|
+
const withoutContracts = messages.filter(
|
|
631
|
+
(message) => unwrapMessage(message).customType !== GOAL_CONTRACT_MESSAGE_TYPE
|
|
632
|
+
);
|
|
633
|
+
const insertionIndex = leadingSummaryBoundary(withoutContracts);
|
|
634
|
+
return [
|
|
635
|
+
...withoutContracts.slice(0, insertionIndex),
|
|
636
|
+
expected,
|
|
637
|
+
...withoutContracts.slice(insertionIndex)
|
|
638
|
+
];
|
|
639
|
+
}
|
|
640
|
+
function leadingSummaryBoundary(messages) {
|
|
641
|
+
let index = 0;
|
|
642
|
+
while (index < messages.length) {
|
|
643
|
+
const role = unwrapMessage(messages[index]).role;
|
|
644
|
+
if (role !== "compactionSummary" && role !== "branchSummary") break;
|
|
645
|
+
index += 1;
|
|
646
|
+
}
|
|
647
|
+
return index;
|
|
648
|
+
}
|
|
649
|
+
function unwrapMessage(message) {
|
|
650
|
+
const entry = message;
|
|
651
|
+
return entry?.message ?? message ?? {};
|
|
652
|
+
}
|
|
653
|
+
|
|
556
654
|
// src/lifecycle.ts
|
|
557
655
|
var REMOVED_QUEUE_SETTING_WARNING = "Ordered goal queue has been removed. Use /goal edit to reprioritize an active objective, or start /goal <objectives> if no active goal exists.";
|
|
558
656
|
var REMOVED_PERSISTED_QUEUE_WARNING = "Ordered goal queue has been removed. Start /goal <objectives> to continue with one merged objective, or use /goal clear to discard the old queue state.";
|
|
559
657
|
function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
560
658
|
pi.on("session_start", async (_event, ctx) => {
|
|
659
|
+
runtime.bindWorkflowSession(ctx.sessionManager);
|
|
561
660
|
runtime.replaceMenuSession();
|
|
562
661
|
runtime.clearCompletionStatusTimer();
|
|
563
662
|
runtime.clearContinuationTracking();
|
|
@@ -573,8 +672,13 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
573
672
|
runtime.clearTerminalDetails();
|
|
574
673
|
const previousToolVisibility = runtime.settings.toolVisibility;
|
|
575
674
|
const settingsResult = readGoalSettings(options.settingsPath);
|
|
675
|
+
const loaded = loadGoalStateFromSession(ctx);
|
|
576
676
|
runtime.settings = settingsResult.kind === "loaded" ? settingsResult.settings : DEFAULT_GOAL_SETTINGS;
|
|
577
677
|
runtime.settingsLoadIssue = settingsResult.kind === "invalid" ? settingsResult : void 0;
|
|
678
|
+
runtime.activeGoal = void 0;
|
|
679
|
+
runtime.legacyQueueState = loaded.legacyQueueState;
|
|
680
|
+
runtime.legacyExperimentalGoalsSetting = settingsResult.kind !== "invalid" && settingsResult.legacyExperimentalGoals;
|
|
681
|
+
runController.bindSession(ctx);
|
|
578
682
|
if (settingsResult.kind === "invalid") {
|
|
579
683
|
notifyTerminal(
|
|
580
684
|
ctx.ui,
|
|
@@ -582,56 +686,96 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
582
686
|
"warning"
|
|
583
687
|
);
|
|
584
688
|
}
|
|
585
|
-
runtime.legacyExperimentalGoalsSetting
|
|
689
|
+
if (runtime.legacyExperimentalGoalsSetting && !runtime.legacyQueueState) {
|
|
690
|
+
notifyTerminal(ctx.ui, REMOVED_QUEUE_SETTING_WARNING, "warning");
|
|
691
|
+
}
|
|
692
|
+
if (loaded.goal?.status === "active") {
|
|
693
|
+
if (!runtime.acquireWorkflow()) {
|
|
694
|
+
runtime.activeGoal = transitionGoal(loaded.goal, "paused");
|
|
695
|
+
runtime.persistGoal(runtime.activeGoal);
|
|
696
|
+
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
697
|
+
notifyTerminal(
|
|
698
|
+
ctx.ui,
|
|
699
|
+
"Goal was paused during restore because another workflow is active in this session. Resume it after the other workflow ends.",
|
|
700
|
+
"warning"
|
|
701
|
+
);
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
runtime.activeGoal = loaded.goal;
|
|
705
|
+
try {
|
|
706
|
+
runtime.toolPolicy.prepareSessionStart(
|
|
707
|
+
runtime.settings.toolVisibility,
|
|
708
|
+
previousToolVisibility
|
|
709
|
+
);
|
|
710
|
+
} catch (error) {
|
|
711
|
+
notifyTerminal(
|
|
712
|
+
ctx.ui,
|
|
713
|
+
`Could not restore always-visible goal tools: ${formatError(error)}`,
|
|
714
|
+
"error"
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
if (runtime.activeGoal.safetyResetPending) {
|
|
718
|
+
runtime.activeGoal = resetGoalSafetyEpoch(runtime.activeGoal);
|
|
719
|
+
}
|
|
720
|
+
runtime.recordGoalUsage(runtime.activeGoal, ctx);
|
|
721
|
+
if (runtime.limitActiveGoalForBudget(ctx, false)) return;
|
|
722
|
+
if (runtime.enforceAutomaticTurnLimit(ctx, false) || runtime.enforceNoProgressLimit(ctx)) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
runtime.toolPolicy.reconcileRestoredState(runtime.settings.toolVisibility, true);
|
|
726
|
+
if (!runtime.toolPolicy.toolsAvailable()) {
|
|
727
|
+
runtime.pauseGoalForUnavailableTools(ctx, false);
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
runtime.persistGoal(runtime.activeGoal);
|
|
731
|
+
if (!runtime.ownsWorkflow(runtime.activeGoal)) return;
|
|
732
|
+
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
733
|
+
runtime.restoreGoalWaitTimer(ctx);
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
runtime.activeGoal = loaded.goal;
|
|
737
|
+
let appliedInactivePolicy = false;
|
|
738
|
+
let inactivePolicyFailed = false;
|
|
586
739
|
try {
|
|
587
|
-
runtime.
|
|
588
|
-
runtime.
|
|
589
|
-
|
|
590
|
-
|
|
740
|
+
appliedInactivePolicy = runtime.withTemporaryWorkflowAccess(() => {
|
|
741
|
+
runtime.toolPolicy.prepareSessionStart(
|
|
742
|
+
runtime.settings.toolVisibility,
|
|
743
|
+
previousToolVisibility
|
|
744
|
+
);
|
|
745
|
+
runtime.toolPolicy.reconcileRestoredState(
|
|
746
|
+
runtime.settings.toolVisibility,
|
|
747
|
+
runtime.activeGoal !== void 0 && runtime.legacyQueueState === void 0
|
|
748
|
+
);
|
|
749
|
+
});
|
|
591
750
|
} catch (error) {
|
|
751
|
+
inactivePolicyFailed = true;
|
|
592
752
|
notifyTerminal(
|
|
593
753
|
ctx.ui,
|
|
594
754
|
`Could not restore always-visible goal tools: ${formatError(error)}`,
|
|
595
755
|
"error"
|
|
596
756
|
);
|
|
597
757
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
758
|
+
if (!appliedInactivePolicy && !inactivePolicyFailed) {
|
|
759
|
+
notifyTerminal(
|
|
760
|
+
ctx.ui,
|
|
761
|
+
"Goal tool visibility was deferred because another workflow is active in this session.",
|
|
762
|
+
"warning"
|
|
763
|
+
);
|
|
764
|
+
}
|
|
602
765
|
if (runtime.legacyQueueState) {
|
|
603
|
-
runtime.toolPolicy.reconcileRestoredState(runtime.settings.toolVisibility, false);
|
|
604
766
|
ctx.ui.setStatus(STATUS_KEY, void 0);
|
|
605
767
|
notifyTerminal(ctx.ui, REMOVED_PERSISTED_QUEUE_WARNING, "warning");
|
|
606
768
|
return;
|
|
607
769
|
}
|
|
608
|
-
if (runtime.legacyExperimentalGoalsSetting) {
|
|
609
|
-
notifyTerminal(ctx.ui, REMOVED_QUEUE_SETTING_WARNING, "warning");
|
|
610
|
-
}
|
|
611
770
|
if (runtime.activeGoal) {
|
|
612
|
-
if (runtime.activeGoal.status === "active" && runtime.activeGoal.safetyResetPending) {
|
|
613
|
-
runtime.activeGoal = resetGoalSafetyEpoch(runtime.activeGoal);
|
|
614
|
-
}
|
|
615
|
-
if (runtime.activeGoal.status === "active") {
|
|
616
|
-
runtime.recordGoalUsage(runtime.activeGoal, ctx);
|
|
617
|
-
if (runtime.limitActiveGoalForBudget(ctx, false)) return;
|
|
618
|
-
if (runtime.enforceAutomaticTurnLimit(ctx, false) || runtime.enforceNoProgressLimit(ctx))
|
|
619
|
-
return;
|
|
620
|
-
}
|
|
621
|
-
runtime.toolPolicy.reconcileRestoredState(runtime.settings.toolVisibility, true);
|
|
622
|
-
if (runtime.activeGoal.status === "active" && !runtime.toolPolicy.toolsAvailable()) {
|
|
623
|
-
runtime.pauseGoalForUnavailableTools(ctx, false);
|
|
624
|
-
return;
|
|
625
|
-
}
|
|
626
771
|
runtime.persistGoal(runtime.activeGoal);
|
|
627
772
|
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
628
|
-
runtime.restoreGoalWaitTimer(ctx);
|
|
629
773
|
} else {
|
|
630
|
-
runtime.toolPolicy.reconcileRestoredState(runtime.settings.toolVisibility, false);
|
|
631
774
|
ctx.ui.setStatus(STATUS_KEY, void 0);
|
|
632
775
|
}
|
|
633
776
|
});
|
|
634
777
|
pi.on("session_shutdown", (_event, ctx) => {
|
|
778
|
+
const shutdownSession = ctx.sessionManager;
|
|
635
779
|
runController.unbindSession();
|
|
636
780
|
runtime.closeMenuSession();
|
|
637
781
|
runtime.clearGoalWaitTimer();
|
|
@@ -654,13 +798,17 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
654
798
|
ctx.ui.setStatus(STATUS_KEY, void 0);
|
|
655
799
|
runtime.clearCompletionStatusTimer();
|
|
656
800
|
runtime.clearTerminalDetails();
|
|
801
|
+
runtime.releaseWorkflow();
|
|
802
|
+
runtime.unbindWorkflowSession(shutdownSession);
|
|
657
803
|
});
|
|
658
804
|
pi.on("session_before_compact", (event, ctx) => {
|
|
659
805
|
if (runtime.activeGoal?.status === "budget_limited") {
|
|
660
806
|
if (event.willRetry === true) return { cancel: true };
|
|
661
807
|
return;
|
|
662
808
|
}
|
|
663
|
-
if (runtime.activeGoal?.status !== "active")
|
|
809
|
+
if (runtime.activeGoal?.status !== "active" || !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
664
812
|
if (!runtime.recordGoalUsage(runtime.activeGoal, ctx)) return;
|
|
665
813
|
runtime.cancelContinuationWork();
|
|
666
814
|
runtime.persistGoal(runtime.activeGoal);
|
|
@@ -668,7 +816,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
668
816
|
if (runtime.limitActiveGoalForBudget(ctx, false)) return { cancel: true };
|
|
669
817
|
});
|
|
670
818
|
pi.on("session_compact", async (event, ctx) => {
|
|
671
|
-
if (runtime.activeGoal?.status !== "active") {
|
|
819
|
+
if (runtime.activeGoal?.status !== "active" || !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
672
820
|
runtime.clearGoalRecovery();
|
|
673
821
|
return;
|
|
674
822
|
}
|
|
@@ -745,7 +893,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
745
893
|
}
|
|
746
894
|
return;
|
|
747
895
|
}
|
|
748
|
-
if (runtime.activeGoal?.id !== ownedPrompt.goalId || runtime.activeGoal
|
|
896
|
+
if (runtime.activeGoal?.id !== ownedPrompt.goalId || !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
749
897
|
return;
|
|
750
898
|
}
|
|
751
899
|
if (runtime.agentRunGoalId !== void 0 && runtime.agentRunGoalId !== ownedPrompt.goalId) {
|
|
@@ -762,11 +910,16 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
762
910
|
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
763
911
|
});
|
|
764
912
|
pi.on("context", (event, ctx) => {
|
|
765
|
-
const
|
|
913
|
+
const keptMessages = event.messages.filter(
|
|
914
|
+
(message) => runtime.keepBudgetWrapUpMessage(message)
|
|
915
|
+
);
|
|
916
|
+
const messages = runtime.activeGoal?.status === "active" && runtime.ownsWorkflow(runtime.activeGoal) ? reconcileGoalContextContract(keptMessages, runtime.activeGoal) : keptMessages;
|
|
766
917
|
if (runtime.activeGoal?.status === "paused" && runtime.guardAbortGoalId === runtime.activeGoal.id) {
|
|
767
918
|
abortCurrentTurn(ctx);
|
|
768
919
|
}
|
|
769
|
-
if (messages.length !== event.messages.length)
|
|
920
|
+
if (messages !== keptMessages || keptMessages.length !== event.messages.length) {
|
|
921
|
+
return { messages };
|
|
922
|
+
}
|
|
770
923
|
});
|
|
771
924
|
pi.on("tool_call", (event, ctx) => {
|
|
772
925
|
runtime.markAgentToolAttempted();
|
|
@@ -793,7 +946,9 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
793
946
|
runtime.queueBudgetWrapUp(ctx, runtime.activeGoal);
|
|
794
947
|
return;
|
|
795
948
|
}
|
|
796
|
-
if (runtime.activeGoal?.status !== "active")
|
|
949
|
+
if (runtime.activeGoal?.status !== "active" || !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
797
952
|
if (!runtime.recordGoalUsage(runtime.activeGoal, ctx)) return;
|
|
798
953
|
runtime.persistGoal(runtime.activeGoal);
|
|
799
954
|
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
@@ -821,6 +976,12 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
821
976
|
runtime.supersedeOwnedInputCollision(event.prompt);
|
|
822
977
|
if (runtime.activeGoal?.waiting) runtime.clearGoalWait(ctx, runtime.activeGoal.id);
|
|
823
978
|
}
|
|
979
|
+
if (runtime.activeGoal?.status === "active" && !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
980
|
+
runtime.cancelContinuationWork();
|
|
981
|
+
runtime.clearGoalRecovery();
|
|
982
|
+
abortCurrentTurn(ctx);
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
824
985
|
const runOrigin = continuationGoalId ? "automatic" : activeGoalRecovery && runtime.goalRecovery?.automaticOwner ? "automatic" : "manual";
|
|
825
986
|
if (activeBudgetWrapUp && runtime.activeGoal) {
|
|
826
987
|
runtime.beginAgentRun(runtime.activeGoal.id, "manual");
|
|
@@ -834,7 +995,9 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
834
995
|
abortCurrentTurn(ctx);
|
|
835
996
|
return;
|
|
836
997
|
}
|
|
837
|
-
if (runtime.activeGoal?.status !== "active")
|
|
998
|
+
if (runtime.activeGoal?.status !== "active" || !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
838
1001
|
runtime.beginAgentRun(runtime.activeGoal.id, runOrigin);
|
|
839
1002
|
if (!runtime.toolPolicy.toolsAvailable()) {
|
|
840
1003
|
runtime.pauseGoalForUnavailableTools(ctx, ownedPromptGoalId !== void 0);
|
|
@@ -845,11 +1008,6 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
845
1008
|
runtime.persistGoal(runtime.activeGoal);
|
|
846
1009
|
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
847
1010
|
}
|
|
848
|
-
return {
|
|
849
|
-
systemPrompt: `${event.systemPrompt}
|
|
850
|
-
|
|
851
|
-
${buildGoalSystemPrompt(runtime.activeGoal)}`
|
|
852
|
-
};
|
|
853
1011
|
});
|
|
854
1012
|
pi.on("agent_start", (_event, _ctx) => {
|
|
855
1013
|
const activeGoal = runtime.activeGoal;
|
|
@@ -879,7 +1037,9 @@ ${buildGoalSystemPrompt(runtime.activeGoal)}`
|
|
|
879
1037
|
runtime.clearBudgetWrapUp();
|
|
880
1038
|
return;
|
|
881
1039
|
}
|
|
882
|
-
if (runtime.activeGoal.status !== "active")
|
|
1040
|
+
if (runtime.activeGoal.status !== "active" || !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
883
1043
|
const goalId = runtime.activeGoal.id;
|
|
884
1044
|
const alreadyAwaitingContinuation = runtime.hasContinuationWorkForGoal(goalId);
|
|
885
1045
|
const finalAssistant = findFinalAssistantMessage(event.messages);
|
|
@@ -939,6 +1099,12 @@ ${buildGoalSystemPrompt(runtime.activeGoal)}`
|
|
|
939
1099
|
runtime.requestContinuation(currentGoal);
|
|
940
1100
|
});
|
|
941
1101
|
pi.on("agent_settled", (_event, ctx) => {
|
|
1102
|
+
if (runtime.activeGoal?.status === "active" && !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
1103
|
+
runtime.cancelContinuationWork();
|
|
1104
|
+
runtime.clearGoalRecovery();
|
|
1105
|
+
runtime.clearSettledSafetyTracking();
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
942
1108
|
runtime.finalizeSettledRecovery(ctx);
|
|
943
1109
|
const resumedWait = runtime.dispatchDueGoalWait(ctx);
|
|
944
1110
|
if (!resumedWait) runtime.dispatchContinuationIfSettled(ctx);
|
|
@@ -1121,7 +1287,7 @@ var GoalRunController = class {
|
|
|
1121
1287
|
this.run = run;
|
|
1122
1288
|
this.usedRunIds.add(runId);
|
|
1123
1289
|
try {
|
|
1124
|
-
await this.commands.startGoal(
|
|
1290
|
+
const activation = await this.commands.startGoal(
|
|
1125
1291
|
parsed.objective,
|
|
1126
1292
|
parsed.tokenBudget,
|
|
1127
1293
|
session.ctx,
|
|
@@ -1130,6 +1296,16 @@ var GoalRunController = class {
|
|
|
1130
1296
|
},
|
|
1131
1297
|
() => this.ownsRun(run, session.generation)
|
|
1132
1298
|
);
|
|
1299
|
+
if (activation?.kind === "busy" && this.ownsRun(run, session.generation)) {
|
|
1300
|
+
this.closeCurrentRun();
|
|
1301
|
+
this.emitError(
|
|
1302
|
+
runId,
|
|
1303
|
+
"start",
|
|
1304
|
+
"ACTIVATION_FAILED",
|
|
1305
|
+
"Another workflow is active in this session. End it before starting Goal."
|
|
1306
|
+
);
|
|
1307
|
+
return;
|
|
1308
|
+
}
|
|
1133
1309
|
} catch (error) {
|
|
1134
1310
|
if (this.ownsRun(run, session.generation)) {
|
|
1135
1311
|
this.closeCurrentRun();
|
|
@@ -1302,6 +1478,14 @@ function registerGoalTools(pi, runtime) {
|
|
|
1302
1478
|
};
|
|
1303
1479
|
}
|
|
1304
1480
|
const completingDuringBudgetWrapUp = runtime.hasActiveBudgetWrapUp();
|
|
1481
|
+
if (completedGoal.status === "active" && !runtime.ownsWorkflow(completedGoal)) {
|
|
1482
|
+
const rejection = "Goal completion rejected: active Goal no longer owns its workflow.";
|
|
1483
|
+
notifyTerminal(ctx.ui, rejection, "warning");
|
|
1484
|
+
return {
|
|
1485
|
+
content: toolContent(rejection),
|
|
1486
|
+
details: completionDetails(goal2, requestedGoalId, summary)
|
|
1487
|
+
};
|
|
1488
|
+
}
|
|
1305
1489
|
if (!runtime.canRecordGoalUsage() && !completingDuringBudgetWrapUp) {
|
|
1306
1490
|
const rejection = "Goal completion rejected: current run does not own the active goal.";
|
|
1307
1491
|
notifyTerminal(ctx.ui, rejection, "warning");
|
|
@@ -1421,6 +1605,8 @@ function registerGoalTools(pi, runtime) {
|
|
|
1421
1605
|
if (blockedGoal.status !== "active") {
|
|
1422
1606
|
return reject(`goal is ${blockedGoal.status}, not active`);
|
|
1423
1607
|
}
|
|
1608
|
+
if (!runtime.ownsWorkflow(blockedGoal))
|
|
1609
|
+
return reject("active Goal no longer owns its workflow");
|
|
1424
1610
|
if (!reason) return reject("reason is empty");
|
|
1425
1611
|
if (reason.length > MAX_BLOCKER_REASON_LENGTH) return reject("reason is too long");
|
|
1426
1612
|
if (!evidence) return reject("evidence is empty");
|
|
@@ -1495,6 +1681,8 @@ function registerGoalTools(pi, runtime) {
|
|
|
1495
1681
|
if (activeGoal.status !== "active") {
|
|
1496
1682
|
return reject(`goal is ${activeGoal.status}, not active`);
|
|
1497
1683
|
}
|
|
1684
|
+
if (!runtime.ownsWorkflow(activeGoal))
|
|
1685
|
+
return reject("active Goal no longer owns its workflow");
|
|
1498
1686
|
if (activeGoal.waiting) return reject("goal is already waiting");
|
|
1499
1687
|
if (!reason) return reject("reason is empty");
|
|
1500
1688
|
if (reason.length > MAX_GOAL_WAIT_REASON_LENGTH) return reject("reason is too long");
|