@narumitw/pi-goal 0.54.3 → 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 +117 -190
- 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 +111 -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 -58
- 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 +371 -347
- 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/dist/index.ts
CHANGED
|
@@ -43,7 +43,7 @@ import {
|
|
|
43
43
|
stoppedStatusLabel,
|
|
44
44
|
transitionGoal,
|
|
45
45
|
validateObjective
|
|
46
|
-
} from "./chunks/chunk-
|
|
46
|
+
} from "./chunks/chunk-VJ5LR26S.js";
|
|
47
47
|
import {
|
|
48
48
|
DEFAULT_GOAL_SETTINGS,
|
|
49
49
|
currentTokenTotal,
|
|
@@ -52,83 +52,85 @@ import {
|
|
|
52
52
|
formatTokenCount,
|
|
53
53
|
isGoalContextOverflow,
|
|
54
54
|
isRetryableGoalInterruption,
|
|
55
|
+
isStaleContextError,
|
|
55
56
|
isUsageLimitedGoalInterruption,
|
|
56
57
|
notifyTerminal,
|
|
57
58
|
readGoalSettings,
|
|
58
59
|
safeGoalMenuText,
|
|
59
60
|
safeTerminalText,
|
|
60
61
|
truncateNotification
|
|
61
|
-
} from "./chunks/chunk-
|
|
62
|
+
} from "./chunks/chunk-WQIA35U2.js";
|
|
62
63
|
|
|
63
64
|
// src/command-registration.ts
|
|
64
65
|
function registerGoalCommand(pi, runtime, commands, options = {}) {
|
|
65
|
-
const loadGoalManager = cachedModuleLoader(
|
|
66
|
-
|
|
67
|
-
);
|
|
68
|
-
const loadGoalSettings = cachedModuleLoader(
|
|
69
|
-
options.loadGoalSettings ?? (() => import("./chunks/settings-ui-MVYTTBHW.js"))
|
|
70
|
-
);
|
|
66
|
+
const loadGoalManager = cachedModuleLoader(options.loadGoalManager ?? (() => import("./chunks/menu-JMGWQZIT.js")));
|
|
67
|
+
const loadGoalSettings = cachedModuleLoader(options.loadGoalSettings ?? (() => import("./chunks/settings-ui-JG3PFRB5.js")));
|
|
71
68
|
pi.registerCommand("goal", {
|
|
72
69
|
description: "Run a goal to completion: /goal [--tokens 100k] <goal_to_complete>",
|
|
73
70
|
getArgumentCompletions: (prefix) => completeGoalArguments(prefix),
|
|
74
71
|
handler: async (args, ctx) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (result.kind === "show" && args.trim() === "") {
|
|
85
|
-
const menuIsCurrent = captureMenuOwnership(runtime);
|
|
86
|
-
let managerModule;
|
|
87
|
-
try {
|
|
88
|
-
managerModule = await loadGoalManager();
|
|
89
|
-
} catch (error) {
|
|
90
|
-
if (!menuIsCurrent()) return;
|
|
91
|
-
throw error;
|
|
72
|
+
try {
|
|
73
|
+
if (runtime.hasLegacyQueueInterface() && isRemovedQueueCommand(args)) {
|
|
74
|
+
reportRemovedQueueCommand(ctx, runtime);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const result = parseCommand(args);
|
|
78
|
+
if (typeof result === "string") {
|
|
79
|
+
reportCommandError(result, ctx);
|
|
80
|
+
return;
|
|
92
81
|
}
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const settingsAreCurrent = captureMenuOwnership(runtime);
|
|
97
|
-
let settingsModule;
|
|
82
|
+
if (result.kind === "show" && args.trim() === "") {
|
|
83
|
+
const menuIsCurrent = captureMenuOwnership(runtime);
|
|
84
|
+
let managerModule;
|
|
98
85
|
try {
|
|
99
|
-
|
|
86
|
+
managerModule = await loadGoalManager();
|
|
100
87
|
} catch (error) {
|
|
101
|
-
if (!
|
|
88
|
+
if (!menuIsCurrent()) return;
|
|
102
89
|
throw error;
|
|
103
90
|
}
|
|
104
|
-
if (!
|
|
105
|
-
const {
|
|
106
|
-
await
|
|
107
|
-
|
|
108
|
-
|
|
91
|
+
if (!menuIsCurrent()) return;
|
|
92
|
+
const { showGoalManager } = managerModule;
|
|
93
|
+
await showGoalManager(runtime, commands, ctx, async (menuCtx, target) => {
|
|
94
|
+
const settingsAreCurrent = captureMenuOwnership(runtime);
|
|
95
|
+
let settingsModule;
|
|
96
|
+
try {
|
|
97
|
+
settingsModule = await loadGoalSettings();
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (!settingsAreCurrent()) return;
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
if (!settingsAreCurrent()) return;
|
|
103
|
+
const { showGoalSettings } = settingsModule;
|
|
104
|
+
await showGoalSettings(runtime, menuCtx, {
|
|
105
|
+
settingsPath: options.settingsPath,
|
|
106
|
+
initialScreen: target
|
|
107
|
+
});
|
|
109
108
|
});
|
|
110
|
-
});
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
switch (result.kind) {
|
|
114
|
-
case "show":
|
|
115
|
-
commands.showGoal(ctx);
|
|
116
|
-
return;
|
|
117
|
-
case "pause":
|
|
118
|
-
commands.pauseGoal(ctx);
|
|
119
|
-
return;
|
|
120
|
-
case "resume":
|
|
121
|
-
await commands.resumeGoal(ctx);
|
|
122
|
-
return;
|
|
123
|
-
case "clear":
|
|
124
|
-
commands.clearGoal(ctx);
|
|
125
|
-
return;
|
|
126
|
-
case "edit":
|
|
127
|
-
await commands.editGoal(result.objective ?? "", result.tokenBudget, ctx);
|
|
128
|
-
return;
|
|
129
|
-
case "start":
|
|
130
|
-
await commands.startGoal(result.objective ?? "", result.tokenBudget, ctx);
|
|
131
109
|
return;
|
|
110
|
+
}
|
|
111
|
+
switch (result.kind) {
|
|
112
|
+
case "show":
|
|
113
|
+
commands.showGoal(ctx);
|
|
114
|
+
return;
|
|
115
|
+
case "pause":
|
|
116
|
+
commands.pauseGoal(ctx);
|
|
117
|
+
return;
|
|
118
|
+
case "resume":
|
|
119
|
+
await commands.resumeGoal(ctx);
|
|
120
|
+
return;
|
|
121
|
+
case "clear":
|
|
122
|
+
commands.clearGoal(ctx);
|
|
123
|
+
return;
|
|
124
|
+
case "edit":
|
|
125
|
+
await commands.editGoal(result.objective ?? "", result.tokenBudget, ctx);
|
|
126
|
+
return;
|
|
127
|
+
case "start":
|
|
128
|
+
await commands.startGoal(result.objective ?? "", result.tokenBudget, ctx);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
} catch (error) {
|
|
132
|
+
if (isStaleContextError(error)) return;
|
|
133
|
+
throw error;
|
|
132
134
|
}
|
|
133
135
|
}
|
|
134
136
|
});
|
|
@@ -189,11 +191,7 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
189
191
|
}
|
|
190
192
|
if (isRequestCurrent && !isRequestCurrent()) return;
|
|
191
193
|
if (this.runtime.activeGoal?.id !== existingGoal.id) {
|
|
192
|
-
notifyTerminal(
|
|
193
|
-
ctx.ui,
|
|
194
|
-
"The active goal changed while confirmation was open. Try again.",
|
|
195
|
-
"warning"
|
|
196
|
-
);
|
|
194
|
+
notifyTerminal(ctx.ui, "The active goal changed while confirmation was open. Try again.", "warning");
|
|
197
195
|
return;
|
|
198
196
|
}
|
|
199
197
|
}
|
|
@@ -299,11 +297,7 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
299
297
|
return;
|
|
300
298
|
}
|
|
301
299
|
if (this.runtime.activeGoal.status !== "active") {
|
|
302
|
-
notifyTerminal(
|
|
303
|
-
ctx.ui,
|
|
304
|
-
`Goal is ${this.runtime.activeGoal.status}; only active goals can be paused.`,
|
|
305
|
-
"warning"
|
|
306
|
-
);
|
|
300
|
+
notifyTerminal(ctx.ui, `Goal is ${this.runtime.activeGoal.status}; only active goals can be paused.`, "warning");
|
|
307
301
|
return;
|
|
308
302
|
}
|
|
309
303
|
const stoppedGoal = this.runtime.stopActiveGoal(ctx, {
|
|
@@ -330,11 +324,7 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
330
324
|
return;
|
|
331
325
|
}
|
|
332
326
|
if (this.runtime.activeGoal.tokenBudget !== void 0 && this.runtime.activeGoal.tokensUsed >= this.runtime.activeGoal.tokenBudget) {
|
|
333
|
-
notifyTerminal(
|
|
334
|
-
ctx.ui,
|
|
335
|
-
`Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`,
|
|
336
|
-
"warning"
|
|
337
|
-
);
|
|
327
|
+
notifyTerminal(ctx.ui, `Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`, "warning");
|
|
338
328
|
return;
|
|
339
329
|
}
|
|
340
330
|
if (!this.runtime.acquireWorkflow(ctx.sessionManager)) {
|
|
@@ -354,18 +344,12 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
354
344
|
this.runtime.clearGoalRecovery();
|
|
355
345
|
this.runtime.clearBudgetWrapUp();
|
|
356
346
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
357
|
-
this.runtime.activeGoal = queueGoalSafetyReset(
|
|
358
|
-
transitionGoal(nextGoalInstance(this.runtime.activeGoal), "active")
|
|
359
|
-
);
|
|
347
|
+
this.runtime.activeGoal = queueGoalSafetyReset(transitionGoal(nextGoalInstance(this.runtime.activeGoal), "active"));
|
|
360
348
|
this.runtime.persistGoal(this.runtime.activeGoal);
|
|
361
349
|
if (this.runtime.activeGoal.status !== "active") {
|
|
362
350
|
this.runtime.updateStatus(ctx, this.runtime.activeGoal);
|
|
363
351
|
this.runtime.releaseWorkflow();
|
|
364
|
-
notifyTerminal(
|
|
365
|
-
ctx.ui,
|
|
366
|
-
`Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`,
|
|
367
|
-
"warning"
|
|
368
|
-
);
|
|
352
|
+
notifyTerminal(ctx.ui, `Goal token budget is still reached: ${formatBudget(this.runtime.activeGoal)}`, "warning");
|
|
369
353
|
return;
|
|
370
354
|
}
|
|
371
355
|
if (!this.runtime.ownsWorkflow(this.runtime.activeGoal)) return;
|
|
@@ -412,8 +396,7 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
412
396
|
}
|
|
413
397
|
if (!this.runtime.clearGoalWait(ctx, waitingGoal.id)) return;
|
|
414
398
|
const resumedGoal = this.runtime.activeGoal;
|
|
415
|
-
if (!resumedGoal || resumedGoal.id !== waitingGoal.id || resumedGoal.status !== "active")
|
|
416
|
-
return;
|
|
399
|
+
if (!resumedGoal || resumedGoal.id !== waitingGoal.id || resumedGoal.status !== "active") return;
|
|
417
400
|
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
418
401
|
ctx,
|
|
419
402
|
resumedGoal.id,
|
|
@@ -513,11 +496,7 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
513
496
|
if (editedGoal.status === "active") {
|
|
514
497
|
if (!this.runtime.ownsWorkflow(editedGoal)) return;
|
|
515
498
|
this.runtime.clearStaleGoalToolCallBlock();
|
|
516
|
-
const sent = await this.runtime.sendOwnedGoalPrompt(
|
|
517
|
-
ctx,
|
|
518
|
-
editedGoal.id,
|
|
519
|
-
buildObjectiveUpdatedPrompt(editedGoal)
|
|
520
|
-
);
|
|
499
|
+
const sent = await this.runtime.sendOwnedGoalPrompt(ctx, editedGoal.id, buildObjectiveUpdatedPrompt(editedGoal));
|
|
521
500
|
if (!sent) {
|
|
522
501
|
if (this.runtime.activeGoal?.id === editedGoal.id) {
|
|
523
502
|
if (previousStatus === "active" && previousGoal.waiting) {
|
|
@@ -604,7 +583,9 @@ New goal: ${safeGoalMenuText(objective, 4e3)}`
|
|
|
604
583
|
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.";
|
|
605
584
|
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.";
|
|
606
585
|
function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
586
|
+
let sessionActive = true;
|
|
607
587
|
pi.on("session_start", async (_event, ctx) => {
|
|
588
|
+
sessionActive = true;
|
|
608
589
|
runtime.bindWorkflowSession(ctx.sessionManager);
|
|
609
590
|
runtime.replaceMenuSession();
|
|
610
591
|
runtime.clearCompletionStatusTimer();
|
|
@@ -628,11 +609,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
628
609
|
runtime.legacyExperimentalGoalsSetting = settingsResult.kind !== "invalid" && settingsResult.legacyExperimentalGoals;
|
|
629
610
|
runController.bindSession(ctx);
|
|
630
611
|
if (settingsResult.kind === "invalid") {
|
|
631
|
-
notifyTerminal(
|
|
632
|
-
ctx.ui,
|
|
633
|
-
`pi-goal settings ignored: ${settingsResult.reason}. Using default settings.`,
|
|
634
|
-
"warning"
|
|
635
|
-
);
|
|
612
|
+
notifyTerminal(ctx.ui, `pi-goal settings ignored: ${settingsResult.reason}. Using default settings.`, "warning");
|
|
636
613
|
}
|
|
637
614
|
if (runtime.legacyExperimentalGoalsSetting && !runtime.legacyQueueState) {
|
|
638
615
|
notifyTerminal(ctx.ui, REMOVED_QUEUE_SETTING_WARNING, "warning");
|
|
@@ -690,6 +667,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
690
667
|
}
|
|
691
668
|
});
|
|
692
669
|
pi.on("session_shutdown", (_event, ctx) => {
|
|
670
|
+
sessionActive = false;
|
|
693
671
|
const shutdownSession = ctx.sessionManager;
|
|
694
672
|
runController.unbindSession();
|
|
695
673
|
runtime.closeMenuSession();
|
|
@@ -717,6 +695,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
717
695
|
runtime.unbindWorkflowSession(shutdownSession);
|
|
718
696
|
});
|
|
719
697
|
pi.on("session_before_compact", (event, ctx) => {
|
|
698
|
+
if (!sessionActive) return;
|
|
720
699
|
if (runtime.activeGoal?.status === "budget_limited") {
|
|
721
700
|
if (event.willRetry === true) return { cancel: true };
|
|
722
701
|
return;
|
|
@@ -731,6 +710,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
731
710
|
if (runtime.limitActiveGoalForBudget(ctx, false)) return { cancel: true };
|
|
732
711
|
});
|
|
733
712
|
pi.on("session_compact", async (event, ctx) => {
|
|
713
|
+
if (!sessionActive) return;
|
|
734
714
|
if (runtime.activeGoal?.status !== "active" || !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
735
715
|
runtime.clearGoalRecovery();
|
|
736
716
|
return;
|
|
@@ -758,6 +738,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
758
738
|
runtime.scheduleContinuationDispatch(ctx, runtime.activeGoal.id);
|
|
759
739
|
});
|
|
760
740
|
pi.on("input", (event, ctx) => {
|
|
741
|
+
if (!sessionActive) return;
|
|
761
742
|
if (event.source === "extension") {
|
|
762
743
|
if (runtime.consumeCancelledGoalPrompt(event.text) || runtime.consumeCancelledContinuationPrompt(event.text) || runtime.consumeStaleOwnedGoalPrompt(event.text)) {
|
|
763
744
|
return { action: "handled" };
|
|
@@ -786,6 +767,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
786
767
|
runtime.resetActiveSafetyEpoch(ctx);
|
|
787
768
|
});
|
|
788
769
|
pi.on("message_start", (event, ctx) => {
|
|
770
|
+
if (!sessionActive) return;
|
|
789
771
|
const message = event.message;
|
|
790
772
|
if (message.role === "assistant" && runtime.activeGoal?.status === "paused" && runtime.guardAbortGoalId === runtime.activeGoal.id) {
|
|
791
773
|
abortCurrentTurn(ctx);
|
|
@@ -802,9 +784,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
802
784
|
return;
|
|
803
785
|
}
|
|
804
786
|
if (message.role !== "user") return;
|
|
805
|
-
const prompt = Array.isArray(message.content) ? message.content.filter(
|
|
806
|
-
(part) => part && typeof part === "object" && Reflect.get(part, "type") === "text"
|
|
807
|
-
).map((part) => Reflect.get(part, "text")).filter((text) => typeof text === "string").join("\n") : typeof message.content === "string" ? message.content : "";
|
|
787
|
+
const prompt = Array.isArray(message.content) ? message.content.filter((part) => part && typeof part === "object" && Reflect.get(part, "type") === "text").map((part) => Reflect.get(part, "text")).filter((text) => typeof text === "string").join("\n") : typeof message.content === "string" ? message.content : "";
|
|
808
788
|
const ownedPrompt = runtime.consumeOwnedGoalPrompt(prompt);
|
|
809
789
|
const ownedPromptBoundary = runtime.hasOwnedPromptBoundary(prompt);
|
|
810
790
|
const queuedNonGoalInput = runtime.consumeQueuedNonGoalInput(prompt, !ownedPromptBoundary);
|
|
@@ -818,10 +798,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
818
798
|
return;
|
|
819
799
|
}
|
|
820
800
|
if (runtime.agentRunGoalId !== void 0 && runtime.agentRunGoalId !== ownedPrompt.goalId) {
|
|
821
|
-
runtime.activeGoal.baselineTokens = Math.max(
|
|
822
|
-
0,
|
|
823
|
-
currentTokenTotal(ctx) - runtime.activeGoal.tokensUsed
|
|
824
|
-
);
|
|
801
|
+
runtime.activeGoal.baselineTokens = Math.max(0, currentTokenTotal(ctx) - runtime.activeGoal.tokensUsed);
|
|
825
802
|
}
|
|
826
803
|
runtime.beginAgentRun(ownedPrompt.goalId, "manual");
|
|
827
804
|
if (ownedPrompt.resetSafetyEpoch) {
|
|
@@ -831,9 +808,8 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
831
808
|
runtime.updateStatus(ctx, runtime.activeGoal);
|
|
832
809
|
});
|
|
833
810
|
pi.on("context", (event, ctx) => {
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
);
|
|
811
|
+
if (!sessionActive) return;
|
|
812
|
+
const keptMessages = event.messages.filter((message) => runtime.keepBudgetWrapUpMessage(message));
|
|
837
813
|
const hasGoalContractHistory = keptMessages.some(isGoalContextContract) || runtime.hasGoalContextContractHistory(ctx);
|
|
838
814
|
const messages = runtime.activeGoal?.status === "active" && runtime.ownsWorkflow(runtime.activeGoal) ? reconcileGoalContextContract(keptMessages, runtime.activeGoal) : hasGoalContractHistory ? reconcileInactiveGoalContextContract(keptMessages) : keptMessages;
|
|
839
815
|
if (runtime.activeGoal?.status === "paused" && runtime.guardAbortGoalId === runtime.activeGoal.id) {
|
|
@@ -844,6 +820,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
844
820
|
}
|
|
845
821
|
});
|
|
846
822
|
pi.on("tool_call", (event, ctx) => {
|
|
823
|
+
if (!sessionActive) return;
|
|
847
824
|
runtime.markAgentToolAttempted();
|
|
848
825
|
if (runtime.activeGoal?.status === "budget_limited" && runtime.budgetWrapUp?.goalId === runtime.activeGoal.id && event.toolName !== "goal_complete") {
|
|
849
826
|
abortCurrentTurn(ctx);
|
|
@@ -864,6 +841,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
864
841
|
};
|
|
865
842
|
});
|
|
866
843
|
pi.on("tool_execution_end", (_event, ctx) => {
|
|
844
|
+
if (!sessionActive) return;
|
|
867
845
|
if (runtime.activeGoal?.status === "budget_limited" && runtime.budgetWrapUp?.goalId === runtime.activeGoal.id && !runtime.budgetWrapUp.delivered) {
|
|
868
846
|
runtime.queueBudgetWrapUp(ctx, runtime.activeGoal);
|
|
869
847
|
return;
|
|
@@ -878,6 +856,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
878
856
|
if (!runtime.goalToolsAvailable()) runtime.pauseGoalForUnavailableTools(ctx);
|
|
879
857
|
});
|
|
880
858
|
pi.on("before_agent_start", (event, ctx) => {
|
|
859
|
+
if (!sessionActive) return;
|
|
881
860
|
runtime.clearAgentRun();
|
|
882
861
|
if (runtime.guardAbortGoalId) runtime.guardAbortGoalId = void 0;
|
|
883
862
|
const goalPrompt = runtime.consumeOwnedGoalPrompt(event.prompt);
|
|
@@ -933,6 +912,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
933
912
|
return goalContractBoundaryResult(ctx, runtime.activeGoal);
|
|
934
913
|
});
|
|
935
914
|
pi.on("agent_start", (_event, _ctx) => {
|
|
915
|
+
if (!sessionActive) return;
|
|
936
916
|
const activeGoal = runtime.activeGoal;
|
|
937
917
|
if (activeGoal && runtime.guardAbortGoalId === activeGoal.id && activeGoal.status === "paused") {
|
|
938
918
|
if (runtime.consumeQueuedNonGoalFollowUpForAgentStart()) {
|
|
@@ -945,12 +925,14 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
945
925
|
runtime.beginRecoveryRunIfNeeded();
|
|
946
926
|
});
|
|
947
927
|
pi.on("turn_end", (event, ctx) => {
|
|
928
|
+
if (!sessionActive) return;
|
|
948
929
|
runtime.recordAutomaticTurn(ctx, event.message);
|
|
949
930
|
if (runtime.activeGoal?.status !== "active") {
|
|
950
931
|
runtime.ensureInactiveGoalContextContract(ctx);
|
|
951
932
|
}
|
|
952
933
|
});
|
|
953
934
|
pi.on("agent_end", (event, ctx) => {
|
|
935
|
+
if (!sessionActive) return;
|
|
954
936
|
const run = runtime.finishAgentRun();
|
|
955
937
|
if (run.goalId === null) return;
|
|
956
938
|
if (!runtime.canRecordGoalUsage() && !runtime.hasActiveBudgetWrapUp()) return;
|
|
@@ -1025,6 +1007,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
1025
1007
|
runtime.requestContinuation(currentGoal);
|
|
1026
1008
|
});
|
|
1027
1009
|
pi.on("agent_settled", (_event, ctx) => {
|
|
1010
|
+
if (!sessionActive) return;
|
|
1028
1011
|
if (runtime.activeGoal?.status === "active" && !runtime.ownsWorkflow(runtime.activeGoal)) {
|
|
1029
1012
|
runtime.cancelContinuationWork();
|
|
1030
1013
|
runtime.clearGoalRecovery();
|
|
@@ -1058,11 +1041,7 @@ function registerGoalLifecycle(pi, runtime, runController, options = {}) {
|
|
|
1058
1041
|
if (!stoppedGoal) return;
|
|
1059
1042
|
const details = assistant.errorMessage ? ` (${truncateNotification(assistant.errorMessage)})` : "";
|
|
1060
1043
|
if (status === "paused") {
|
|
1061
|
-
notifyTerminal(
|
|
1062
|
-
ctx.ui,
|
|
1063
|
-
`Goal paused after interruption${details}. Run /goal resume to continue.`,
|
|
1064
|
-
"warning"
|
|
1065
|
-
);
|
|
1044
|
+
notifyTerminal(ctx.ui, `Goal paused after interruption${details}. Run /goal resume to continue.`, "warning");
|
|
1066
1045
|
return;
|
|
1067
1046
|
}
|
|
1068
1047
|
if (status === "usage_limited") {
|
|
@@ -1192,12 +1171,7 @@ var GoalRunController = class {
|
|
|
1192
1171
|
return;
|
|
1193
1172
|
}
|
|
1194
1173
|
if (this.session !== session || this.generation !== session.generation) {
|
|
1195
|
-
this.emitError(
|
|
1196
|
-
runId,
|
|
1197
|
-
"start",
|
|
1198
|
-
"SUPERSEDED",
|
|
1199
|
-
"The pi-goal session changed while validating the request."
|
|
1200
|
-
);
|
|
1174
|
+
this.emitError(runId, "start", "SUPERSEDED", "The pi-goal session changed while validating the request.");
|
|
1201
1175
|
return;
|
|
1202
1176
|
}
|
|
1203
1177
|
if (this.usedRunIds.has(runId)) {
|
|
@@ -1239,12 +1213,7 @@ var GoalRunController = class {
|
|
|
1239
1213
|
} catch (error) {
|
|
1240
1214
|
if (this.ownsRun(run, session.generation)) {
|
|
1241
1215
|
this.closeCurrentRun();
|
|
1242
|
-
this.emitError(
|
|
1243
|
-
runId,
|
|
1244
|
-
"start",
|
|
1245
|
-
"ACTIVATION_FAILED",
|
|
1246
|
-
`Goal activation failed: ${formatError(error)}`
|
|
1247
|
-
);
|
|
1216
|
+
this.emitError(runId, "start", "ACTIVATION_FAILED", `Goal activation failed: ${formatError(error)}`);
|
|
1248
1217
|
}
|
|
1249
1218
|
return;
|
|
1250
1219
|
}
|
|
@@ -1314,12 +1283,11 @@ var GoalRunController = class {
|
|
|
1314
1283
|
});
|
|
1315
1284
|
return;
|
|
1316
1285
|
}
|
|
1317
|
-
if (
|
|
1286
|
+
if (run.lastStatus === snapshot.status) return;
|
|
1318
1287
|
this.publishStateEvent(run, snapshot);
|
|
1319
1288
|
}
|
|
1320
1289
|
publishStateEvent(run, snapshot) {
|
|
1321
1290
|
const status = snapshot.status;
|
|
1322
|
-
if (status === "queued") return;
|
|
1323
1291
|
run.lastStatus = status;
|
|
1324
1292
|
const event = {
|
|
1325
1293
|
type: "state",
|
|
@@ -1363,8 +1331,10 @@ import {
|
|
|
1363
1331
|
DEFAULT_MAX_BYTES,
|
|
1364
1332
|
DEFAULT_MAX_LINES,
|
|
1365
1333
|
defineTool,
|
|
1334
|
+
getMarkdownTheme,
|
|
1366
1335
|
truncateHead
|
|
1367
1336
|
} from "@earendil-works/pi-coding-agent";
|
|
1337
|
+
import { Markdown } from "@earendil-works/pi-tui";
|
|
1368
1338
|
import { Type } from "typebox";
|
|
1369
1339
|
var MAX_GOAL_TEXT_LENGTH = 4e3;
|
|
1370
1340
|
var MAX_COMPLETION_SUMMARY_LENGTH = 4e3;
|
|
@@ -1387,6 +1357,9 @@ function registerGoalTools(pi, runtime) {
|
|
|
1387
1357
|
description: "State what was completed and what evidence verified it. Do not use this tool to report partial progress, blockers, failures, or remaining work."
|
|
1388
1358
|
})
|
|
1389
1359
|
}),
|
|
1360
|
+
renderResult(result) {
|
|
1361
|
+
return renderGoalCompletion(result);
|
|
1362
|
+
},
|
|
1390
1363
|
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
1391
1364
|
const completedGoal = runtime.activeGoal;
|
|
1392
1365
|
const goal2 = completedGoal?.text ?? "unknown goal";
|
|
@@ -1521,8 +1494,7 @@ function registerGoalTools(pi, runtime) {
|
|
|
1521
1494
|
if (blockedGoal.status !== "active") {
|
|
1522
1495
|
return reject(`goal is ${blockedGoal.status}, not active`);
|
|
1523
1496
|
}
|
|
1524
|
-
if (!runtime.ownsWorkflow(blockedGoal))
|
|
1525
|
-
return reject("active Goal no longer owns its workflow");
|
|
1497
|
+
if (!runtime.ownsWorkflow(blockedGoal)) return reject("active Goal no longer owns its workflow");
|
|
1526
1498
|
if (!reason) return reject("reason is empty");
|
|
1527
1499
|
if (reason.length > MAX_BLOCKER_REASON_LENGTH) return reject("reason is too long");
|
|
1528
1500
|
if (!evidence) return reject("evidence is empty");
|
|
@@ -1589,8 +1561,7 @@ function registerGoalTools(pi, runtime) {
|
|
|
1589
1561
|
if (activeGoal.status !== "active") {
|
|
1590
1562
|
return reject(`goal is ${activeGoal.status}, not active`);
|
|
1591
1563
|
}
|
|
1592
|
-
if (!runtime.ownsWorkflow(activeGoal))
|
|
1593
|
-
return reject("active Goal no longer owns its workflow");
|
|
1564
|
+
if (!runtime.ownsWorkflow(activeGoal)) return reject("active Goal no longer owns its workflow");
|
|
1594
1565
|
if (activeGoal.waiting) return reject("goal is already waiting");
|
|
1595
1566
|
if (!reason) return reject("reason is empty");
|
|
1596
1567
|
if (reason.length > MAX_GOAL_WAIT_REASON_LENGTH) return reject("reason is too long");
|
|
@@ -1624,6 +1595,20 @@ Requested resume_after_ms ${requestedMs} was clamped to ${effectiveMs}.` : `Goal
|
|
|
1624
1595
|
pi.registerTool(goalBlockedTool);
|
|
1625
1596
|
pi.registerTool(goalWaitTool);
|
|
1626
1597
|
}
|
|
1598
|
+
function goalCompletionMarkdown(result) {
|
|
1599
|
+
const content = result.content.filter((block) => block.type === "text" && typeof block.text === "string").map((block) => block.text).join("\n").trim();
|
|
1600
|
+
const completionPrefix = "Goal complete:";
|
|
1601
|
+
if (!content.startsWith(completionPrefix)) return content;
|
|
1602
|
+
const details = result.details;
|
|
1603
|
+
const summary = details && typeof details === "object" && "summary" in details && typeof details.summary === "string" ? details.summary : content.slice(completionPrefix.length);
|
|
1604
|
+
const safeSummary = safeTerminalText(summary);
|
|
1605
|
+
return safeSummary ? `**Goal complete**
|
|
1606
|
+
|
|
1607
|
+
${safeSummary}` : "**Goal complete**";
|
|
1608
|
+
}
|
|
1609
|
+
function renderGoalCompletion(result) {
|
|
1610
|
+
return new Markdown(goalCompletionMarkdown(result), 0, 0, getMarkdownTheme());
|
|
1611
|
+
}
|
|
1627
1612
|
function toolContent(text) {
|
|
1628
1613
|
return [
|
|
1629
1614
|
{
|