@narumitw/pi-goal 0.24.0 → 0.27.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 +29 -11
- package/package.json +3 -3
- package/src/commands.ts +12 -3
- package/src/errors.ts +147 -0
- package/src/goal.ts +195 -43
- package/src/markers.ts +25 -0
- package/src/persistence.ts +26 -1
- package/src/queue.ts +4 -1
- package/src/runtime.ts +350 -191
- package/src/safety.ts +78 -0
- package/src/settings.ts +41 -0
package/src/goal.ts
CHANGED
|
@@ -23,11 +23,13 @@ import {
|
|
|
23
23
|
isGoalContextOverflow,
|
|
24
24
|
isRetryableGoalInterruption,
|
|
25
25
|
isUsageLimitedGoalInterruption,
|
|
26
|
+
resetGoalSafetyEpoch,
|
|
26
27
|
STATUS_KEY,
|
|
27
28
|
type StatusContext,
|
|
28
29
|
transitionGoal,
|
|
29
30
|
truncateNotification,
|
|
30
31
|
} from "./runtime.js";
|
|
32
|
+
import { hasAssistantToolCall } from "./safety.js";
|
|
31
33
|
import { DEFAULT_GOAL_SETTINGS, readGoalSettings } from "./settings.js";
|
|
32
34
|
|
|
33
35
|
// goal.ts is the Pi-facing composition root: it keeps tool contracts and event
|
|
@@ -57,16 +59,6 @@ const EXPERIMENTAL_GOALS_WARNING =
|
|
|
57
59
|
const MAX_BLOCKER_REASON_LENGTH = 1_000;
|
|
58
60
|
const MAX_BLOCKER_EVIDENCE_LENGTH = 4_000;
|
|
59
61
|
|
|
60
|
-
type AgentSettledHandler = (event: unknown, ctx: StatusContext) => unknown;
|
|
61
|
-
|
|
62
|
-
function onAgentSettled(pi: ExtensionAPI, handler: AgentSettledHandler) {
|
|
63
|
-
(
|
|
64
|
-
pi as unknown as {
|
|
65
|
-
on(event: "agent_settled", callback: AgentSettledHandler): void;
|
|
66
|
-
}
|
|
67
|
-
).on("agent_settled", handler);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
62
|
function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
71
63
|
const runtime = new GoalRuntime(pi);
|
|
72
64
|
const commands = new GoalCommandController(runtime);
|
|
@@ -103,6 +95,10 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
103
95
|
const consumePendingGoalPrompt = runtime.consumeOwnedGoalPrompt.bind(runtime);
|
|
104
96
|
const markContinuationStarted = runtime.markContinuationStarted.bind(runtime);
|
|
105
97
|
const hasContinuationWorkForGoal = runtime.hasContinuationWorkForGoal.bind(runtime);
|
|
98
|
+
const recordAutomaticTurn = runtime.recordAutomaticTurn.bind(runtime);
|
|
99
|
+
const recordAutomaticRunProgress = runtime.recordAutomaticRunProgress.bind(runtime);
|
|
100
|
+
const enforceAutomaticTurnLimit = runtime.enforceAutomaticTurnLimit.bind(runtime);
|
|
101
|
+
const enforceNoProgressLimit = runtime.enforceNoProgressLimit.bind(runtime);
|
|
106
102
|
const clearActiveGoal = runtime.clearActiveGoal.bind(runtime);
|
|
107
103
|
const showCompletionStatus = runtime.showCompletionStatus.bind(runtime);
|
|
108
104
|
const restoreGoalToolsHiddenByPolicy = runtime.restoreGoalToolsHiddenByPolicy.bind(runtime);
|
|
@@ -111,7 +107,8 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
111
107
|
ctx: StatusContext,
|
|
112
108
|
goalId: string,
|
|
113
109
|
prompt: string,
|
|
114
|
-
|
|
110
|
+
resetSafetyEpoch = true,
|
|
111
|
+
) => runtime.sendOwnedGoalPrompt(ctx, goalId, prompt, resetSafetyEpoch);
|
|
115
112
|
const dispatchPendingQueueActionIfSettled =
|
|
116
113
|
commands.dispatchPendingQueueActionIfSettled.bind(commands);
|
|
117
114
|
|
|
@@ -464,7 +461,8 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
464
461
|
clearCompletionStatusTimer();
|
|
465
462
|
clearContinuationTracking();
|
|
466
463
|
clearPendingGoalPrompts();
|
|
467
|
-
runtime.
|
|
464
|
+
runtime.clearAgentRun();
|
|
465
|
+
runtime.guardAbortGoalId = undefined;
|
|
468
466
|
clearGoalRecovery();
|
|
469
467
|
clearBudgetWrapUp();
|
|
470
468
|
clearStaleGoalToolCallBlock();
|
|
@@ -528,9 +526,15 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
528
526
|
}
|
|
529
527
|
if (runtime.pendingQueueAction) await dispatchPendingQueueActionIfSettled(ctx);
|
|
530
528
|
if (runtime.activeGoal) {
|
|
529
|
+
if (runtime.activeGoal.status === "active" && runtime.activeGoal.safetyResetPending) {
|
|
530
|
+
// Resume/edit activation is persisted before its queued prompt starts. A
|
|
531
|
+
// reload must commit that promised reset before enforcing the old limits.
|
|
532
|
+
runtime.activeGoal = resetGoalSafetyEpoch(runtime.activeGoal);
|
|
533
|
+
}
|
|
531
534
|
if (runtime.activeGoal.status === "active") {
|
|
532
535
|
updateGoalUsage(runtime.activeGoal, ctx);
|
|
533
536
|
if (limitActiveGoalForBudget(ctx, false)) return;
|
|
537
|
+
if (enforceAutomaticTurnLimit(ctx, false) || enforceNoProgressLimit(ctx)) return;
|
|
534
538
|
}
|
|
535
539
|
if (runtime.settings.toolVisibility === "after-first-goal") {
|
|
536
540
|
// Registered tools are already active on an unrestricted fresh runtime.
|
|
@@ -552,6 +556,7 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
552
556
|
ctx,
|
|
553
557
|
restoredGoal.id,
|
|
554
558
|
buildGoalPrompt(restoredGoal),
|
|
559
|
+
false, // Reloaded queue activation preserves its persisted safety epoch.
|
|
555
560
|
);
|
|
556
561
|
if (!sent && runtime.activeGoal?.id === restoredGoal.id) {
|
|
557
562
|
runtime.activeGoal = transitionGoal(restoredGoal, "paused");
|
|
@@ -577,7 +582,8 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
577
582
|
}
|
|
578
583
|
clearContinuationTracking();
|
|
579
584
|
clearPendingGoalPrompts();
|
|
580
|
-
runtime.
|
|
585
|
+
runtime.clearAgentRun();
|
|
586
|
+
runtime.guardAbortGoalId = undefined;
|
|
581
587
|
clearGoalRecovery();
|
|
582
588
|
clearBudgetWrapUp();
|
|
583
589
|
clearStaleGoalToolCallBlock();
|
|
@@ -633,8 +639,8 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
633
639
|
if (limitActiveGoalForBudget(ctx, false)) return;
|
|
634
640
|
|
|
635
641
|
const wasPiRetry = isPiOwnedCompactionRetry(event, runtime.activeGoal.id);
|
|
636
|
-
clearGoalRecoveryForGoal(runtime.activeGoal.id);
|
|
637
642
|
if (wasPiRetry) return;
|
|
643
|
+
clearGoalRecoveryForGoal(runtime.activeGoal.id);
|
|
638
644
|
requestContinuation(runtime.activeGoal);
|
|
639
645
|
// Manual compaction does not emit agent_settled. This common dispatcher is
|
|
640
646
|
// therefore the narrow fallback; threshold compaction leaves the intent for
|
|
@@ -642,7 +648,7 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
642
648
|
dispatchContinuationIfSettled(ctx);
|
|
643
649
|
});
|
|
644
650
|
|
|
645
|
-
pi.on("input", (event) => {
|
|
651
|
+
pi.on("input", (event, ctx) => {
|
|
646
652
|
if (event.source === "extension") {
|
|
647
653
|
if (
|
|
648
654
|
consumeCancelledContinuationPrompt(event.text) ||
|
|
@@ -651,22 +657,102 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
651
657
|
return { action: "handled" as const };
|
|
652
658
|
}
|
|
653
659
|
if (runtime.queueFrozen) return;
|
|
660
|
+
// Streaming input is queued before its model work starts. Keep owned
|
|
661
|
+
// markers pending for message_start, and track non-goal delivery mode so a
|
|
662
|
+
// steer cannot consume a later follow-up's cleanup protection.
|
|
663
|
+
if (runtime.hasPendingOwnedGoalPrompt(event.text)) return;
|
|
664
|
+
if (event.streamingBehavior === "steer" || event.streamingBehavior === "followUp") {
|
|
665
|
+
runtime.noteQueuedNonGoalInput(event.text, event.streamingBehavior);
|
|
666
|
+
}
|
|
654
667
|
clearGoalRecovery();
|
|
655
668
|
return;
|
|
656
669
|
}
|
|
657
670
|
if (runtime.queueFrozen) return;
|
|
658
671
|
if (/^\/goal(?:\s|$)/u.test(event.text.trimStart())) return;
|
|
672
|
+
if (event.streamingBehavior === "followUp") {
|
|
673
|
+
runtime.noteQueuedNonGoalInput(event.text, "followUp", true);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
if (event.streamingBehavior === "steer") {
|
|
677
|
+
runtime.noteQueuedNonGoalInput(event.text, "steer");
|
|
678
|
+
}
|
|
659
679
|
clearGoalRecovery();
|
|
660
680
|
clearBudgetWrapUp();
|
|
661
681
|
clearStaleGoalToolCallBlock();
|
|
682
|
+
runtime.resetActiveSafetyEpoch(ctx);
|
|
662
683
|
});
|
|
663
684
|
|
|
664
|
-
pi.on("
|
|
685
|
+
pi.on("message_start", (event, ctx) => {
|
|
686
|
+
const message = event.message as { role?: unknown; content?: unknown };
|
|
687
|
+
if (
|
|
688
|
+
message.role === "assistant" &&
|
|
689
|
+
runtime.activeGoal?.status === "paused" &&
|
|
690
|
+
runtime.guardAbortGoalId === runtime.activeGoal.id
|
|
691
|
+
) {
|
|
692
|
+
abortCurrentTurn(ctx);
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
if (message.role === "custom") {
|
|
696
|
+
if (runtime.isActiveBudgetWrapUpMessage(message)) return;
|
|
697
|
+
if (runtime.guardAbortGoalId === runtime.activeGoal?.id) {
|
|
698
|
+
runtime.guardAbortGoalId = undefined;
|
|
699
|
+
}
|
|
700
|
+
beginNonGoalFollowUp(ctx, false);
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
if (message.role !== "user") return;
|
|
704
|
+
const prompt = Array.isArray(message.content)
|
|
705
|
+
? message.content
|
|
706
|
+
.filter(
|
|
707
|
+
(part) => part && typeof part === "object" && Reflect.get(part, "type") === "text",
|
|
708
|
+
)
|
|
709
|
+
.map((part) => Reflect.get(part as object, "text"))
|
|
710
|
+
.filter((text): text is string => typeof text === "string")
|
|
711
|
+
.join("\n")
|
|
712
|
+
: typeof message.content === "string"
|
|
713
|
+
? message.content
|
|
714
|
+
: "";
|
|
715
|
+
const ownedPrompt = consumePendingGoalPrompt(prompt);
|
|
716
|
+
const ownedPromptBoundary = runtime.hasOwnedPromptBoundary(prompt);
|
|
717
|
+
const queuedNonGoalInput = runtime.consumeQueuedNonGoalInput(prompt, !ownedPromptBoundary);
|
|
718
|
+
if (!ownedPrompt) {
|
|
719
|
+
if (queuedNonGoalInput?.behavior === "followUp") {
|
|
720
|
+
beginNonGoalFollowUp(ctx, queuedNonGoalInput.resetSafetyEpoch);
|
|
721
|
+
}
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
if (runtime.activeGoal?.id !== ownedPrompt.goalId || runtime.activeGoal.status !== "active") {
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
if (runtime.agentRunGoalId !== undefined && runtime.agentRunGoalId !== ownedPrompt.goalId) {
|
|
728
|
+
runtime.activeGoal.baselineTokens = Math.max(
|
|
729
|
+
0,
|
|
730
|
+
currentTokenTotal(ctx) - runtime.activeGoal.tokensUsed,
|
|
731
|
+
);
|
|
732
|
+
}
|
|
733
|
+
runtime.beginAgentRun(ownedPrompt.goalId, "manual");
|
|
734
|
+
if (ownedPrompt.resetSafetyEpoch) {
|
|
735
|
+
runtime.activeGoal = resetGoalSafetyEpoch(runtime.activeGoal);
|
|
736
|
+
}
|
|
737
|
+
persistGoal(runtime.activeGoal);
|
|
738
|
+
updateStatus(ctx, runtime.activeGoal);
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
pi.on("context", (event, ctx) => {
|
|
665
742
|
const messages = event.messages.filter((message) => keepBudgetWrapUpMessage(message));
|
|
743
|
+
if (
|
|
744
|
+
runtime.activeGoal?.status === "paused" &&
|
|
745
|
+
runtime.guardAbortGoalId === runtime.activeGoal.id
|
|
746
|
+
) {
|
|
747
|
+
// A current custom follow-up clears the guard at message_start. Otherwise,
|
|
748
|
+
// context transformation aborts before the provider adapter receives the signal.
|
|
749
|
+
abortCurrentTurn(ctx);
|
|
750
|
+
}
|
|
666
751
|
if (messages.length !== event.messages.length) return { messages };
|
|
667
752
|
});
|
|
668
753
|
|
|
669
754
|
pi.on("tool_call", (event, ctx) => {
|
|
755
|
+
runtime.markAgentToolAttempted();
|
|
670
756
|
if (runtime.queueFrozen) {
|
|
671
757
|
if (!isGoalToolName(event.toolName)) return;
|
|
672
758
|
return {
|
|
@@ -721,15 +807,33 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
721
807
|
});
|
|
722
808
|
|
|
723
809
|
pi.on("before_agent_start", (event, ctx) => {
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
810
|
+
runtime.clearAgentRun();
|
|
811
|
+
if (runtime.queueFrozen) return;
|
|
812
|
+
// Pi-owned retries emit agent_start directly. Reaching a normal prompt
|
|
813
|
+
// boundary means cleanup no longer owns the next run, so the hard-cap guard
|
|
814
|
+
// must not abort it.
|
|
815
|
+
if (runtime.guardAbortGoalId) runtime.guardAbortGoalId = undefined;
|
|
816
|
+
const goalPrompt = consumePendingGoalPrompt(event.prompt);
|
|
817
|
+
const goalPromptGoalId = goalPrompt?.goalId;
|
|
729
818
|
const continuationGoalId = goalPromptGoalId ? undefined : markContinuationStarted(event.prompt);
|
|
730
819
|
const ownedPromptGoalId = goalPromptGoalId ?? continuationGoalId;
|
|
820
|
+
const ownedPromptBoundary = runtime.hasOwnedPromptBoundary(event.prompt);
|
|
731
821
|
const activeBudgetWrapUp = runtime.hasActiveBudgetWrapUp();
|
|
732
822
|
const activeGoalRecovery = runtime.hasActiveGoalRecovery();
|
|
823
|
+
const queuedNonGoalInput = activeBudgetWrapUp
|
|
824
|
+
? undefined
|
|
825
|
+
: runtime.consumeQueuedNonGoalInput(
|
|
826
|
+
event.prompt,
|
|
827
|
+
!activeGoalRecovery && ownedPromptGoalId === undefined && !ownedPromptBoundary,
|
|
828
|
+
);
|
|
829
|
+
if (queuedNonGoalInput?.behavior === "followUp") {
|
|
830
|
+
beginNonGoalFollowUp(ctx, queuedNonGoalInput.resetSafetyEpoch);
|
|
831
|
+
}
|
|
832
|
+
const runOrigin = continuationGoalId
|
|
833
|
+
? "automatic"
|
|
834
|
+
: activeGoalRecovery && runtime.goalRecovery?.automaticOwner
|
|
835
|
+
? "automatic"
|
|
836
|
+
: "manual";
|
|
733
837
|
if (
|
|
734
838
|
runtime.pendingQueueAction?.kind === "prioritize" &&
|
|
735
839
|
!activeBudgetWrapUp &&
|
|
@@ -748,56 +852,77 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
748
852
|
updateStatus(ctx, runtime.activeGoal);
|
|
749
853
|
}
|
|
750
854
|
}
|
|
751
|
-
runtime.
|
|
855
|
+
runtime.beginAgentRun(null, undefined);
|
|
752
856
|
if (ownedPromptGoalId) abortCurrentTurn(ctx);
|
|
753
857
|
return;
|
|
754
858
|
}
|
|
755
859
|
if (activeBudgetWrapUp && runtime.activeGoal) {
|
|
756
|
-
runtime.
|
|
860
|
+
runtime.beginAgentRun(runtime.activeGoal.id, "manual");
|
|
757
861
|
return;
|
|
758
862
|
}
|
|
759
863
|
if (
|
|
760
864
|
runtime.pendingQueueAction?.kind === "advance" &&
|
|
761
865
|
runtime.pendingQueueAction.goalId === runtime.activeGoal?.id
|
|
762
866
|
) {
|
|
763
|
-
runtime.
|
|
867
|
+
runtime.beginAgentRun(ownedPromptGoalId ?? runtime.activeGoal.id, runOrigin);
|
|
764
868
|
if (ownedPromptGoalId) abortCurrentTurn(ctx);
|
|
765
869
|
return;
|
|
766
870
|
}
|
|
767
871
|
if (ownedPromptGoalId && ownedPromptGoalId !== runtime.activeGoal?.id) {
|
|
768
|
-
runtime.
|
|
872
|
+
runtime.beginAgentRun(ownedPromptGoalId, runOrigin);
|
|
769
873
|
if (runtime.activeGoal?.status === "active" && !goalToolsAvailable()) {
|
|
770
874
|
pauseGoalForUnavailableTools(ctx, false);
|
|
771
875
|
}
|
|
772
876
|
abortCurrentTurn(ctx);
|
|
773
877
|
return;
|
|
774
878
|
}
|
|
775
|
-
if (runtime.activeGoal?.status !== "active")
|
|
776
|
-
|
|
777
|
-
return;
|
|
778
|
-
}
|
|
779
|
-
runtime.agentRunGoalId = runtime.activeGoal.id;
|
|
879
|
+
if (runtime.activeGoal?.status !== "active") return;
|
|
880
|
+
runtime.beginAgentRun(runtime.activeGoal.id, runOrigin);
|
|
780
881
|
if (!goalToolsAvailable()) {
|
|
781
882
|
pauseGoalForUnavailableTools(ctx, ownedPromptGoalId !== undefined);
|
|
782
883
|
return;
|
|
783
884
|
}
|
|
885
|
+
if (goalPrompt?.resetSafetyEpoch && goalPromptGoalId === runtime.activeGoal.id) {
|
|
886
|
+
runtime.activeGoal = resetGoalSafetyEpoch(runtime.activeGoal);
|
|
887
|
+
persistGoal(runtime.activeGoal);
|
|
888
|
+
updateStatus(ctx, runtime.activeGoal);
|
|
889
|
+
}
|
|
784
890
|
|
|
785
891
|
return {
|
|
786
892
|
systemPrompt: `${event.systemPrompt}\n\n${buildGoalSystemPrompt(runtime.activeGoal)}`,
|
|
787
893
|
};
|
|
788
894
|
});
|
|
789
895
|
|
|
790
|
-
pi.on("
|
|
896
|
+
pi.on("agent_start", (_event, _ctx) => {
|
|
791
897
|
if (runtime.queueFrozen) return;
|
|
792
|
-
const
|
|
793
|
-
runtime.agentRunGoalId = undefined;
|
|
898
|
+
const activeGoal = runtime.activeGoal;
|
|
794
899
|
if (
|
|
795
|
-
|
|
796
|
-
|
|
900
|
+
activeGoal &&
|
|
901
|
+
runtime.guardAbortGoalId === activeGoal.id &&
|
|
902
|
+
activeGoal.status === "paused"
|
|
797
903
|
) {
|
|
904
|
+
if (runtime.consumeQueuedNonGoalFollowUpForAgentStart()) {
|
|
905
|
+
runtime.guardAbortGoalId = undefined;
|
|
906
|
+
clearStaleGoalToolCallBlock();
|
|
907
|
+
runtime.beginAgentRun(null, undefined);
|
|
908
|
+
}
|
|
909
|
+
// Unknown runs defer cleanup until their message/context boundary: custom
|
|
910
|
+
// follow-ups have no input event, while bare recovery is aborted pre-provider.
|
|
798
911
|
return;
|
|
799
912
|
}
|
|
800
|
-
|
|
913
|
+
runtime.beginRecoveryRunIfNeeded();
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
pi.on("turn_end", (event, ctx) => {
|
|
917
|
+
if (runtime.queueFrozen) return;
|
|
918
|
+
recordAutomaticTurn(ctx, event.message);
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
pi.on("agent_end", (event, ctx) => {
|
|
922
|
+
const run = runtime.finishAgentRun();
|
|
923
|
+
if (runtime.queueFrozen || run.goalId === null) return;
|
|
924
|
+
if (!runtime.canRecordGoalUsage() && !runtime.hasActiveBudgetWrapUp()) return;
|
|
925
|
+
if (run.goalId && run.goalId !== runtime.activeGoal?.id) return;
|
|
801
926
|
if (!runtime.activeGoal) return;
|
|
802
927
|
if (
|
|
803
928
|
runtime.activeGoal.status === "budget_limited" &&
|
|
@@ -835,6 +960,7 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
835
960
|
|
|
836
961
|
if (finalAssistant?.stopReason === "error") {
|
|
837
962
|
if (isRetryableGoalInterruption(finalAssistant)) {
|
|
963
|
+
if (run.origin === "automatic" && enforceAutomaticTurnLimit(ctx, true)) return;
|
|
838
964
|
if (limitActiveGoalForBudget(ctx, false)) return;
|
|
839
965
|
if (!goalToolsAvailable()) {
|
|
840
966
|
pauseGoalForUnavailableTools(ctx);
|
|
@@ -843,6 +969,8 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
843
969
|
runtime.goalRecovery = {
|
|
844
970
|
goalId,
|
|
845
971
|
kind: isGoalContextOverflow(finalAssistant) ? "compaction_retry" : "provider_retry",
|
|
972
|
+
automaticOwner: run.origin === "automatic",
|
|
973
|
+
errorMessage: finalAssistant.errorMessage,
|
|
846
974
|
};
|
|
847
975
|
cancelContinuationWork();
|
|
848
976
|
persistGoal(runtime.activeGoal);
|
|
@@ -866,6 +994,17 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
866
994
|
pauseGoalForUnavailableTools(ctx);
|
|
867
995
|
return;
|
|
868
996
|
}
|
|
997
|
+
if (
|
|
998
|
+
run.origin === "automatic" &&
|
|
999
|
+
recordAutomaticRunProgress(
|
|
1000
|
+
ctx,
|
|
1001
|
+
goalId,
|
|
1002
|
+
event.messages,
|
|
1003
|
+
run.toolAttempted || hasAssistantToolCall(event.messages),
|
|
1004
|
+
)
|
|
1005
|
+
) {
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
869
1008
|
|
|
870
1009
|
persistGoal(runtime.activeGoal);
|
|
871
1010
|
updateStatus(ctx, runtime.activeGoal);
|
|
@@ -876,17 +1015,30 @@ function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
|
876
1015
|
requestContinuation(currentGoal);
|
|
877
1016
|
});
|
|
878
1017
|
|
|
879
|
-
|
|
880
|
-
if (runtime.queueFrozen)
|
|
881
|
-
|
|
882
|
-
dispatchContinuationIfSettled(ctx);
|
|
1018
|
+
pi.on("agent_settled", async (_event, ctx) => {
|
|
1019
|
+
if (runtime.queueFrozen) {
|
|
1020
|
+
runtime.clearSettledSafetyTracking();
|
|
883
1021
|
return;
|
|
884
1022
|
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
1023
|
+
runtime.finalizeSettledRecovery(ctx);
|
|
1024
|
+
let dispatchedQueueAction = false;
|
|
1025
|
+
if (runtime.pendingQueueAction) {
|
|
1026
|
+
dispatchedQueueAction = await dispatchPendingQueueActionIfSettled(ctx);
|
|
1027
|
+
}
|
|
1028
|
+
if (!dispatchedQueueAction) dispatchContinuationIfSettled(ctx);
|
|
1029
|
+
runtime.clearSettledSafetyTracking();
|
|
888
1030
|
});
|
|
889
1031
|
|
|
1032
|
+
function beginNonGoalFollowUp(ctx: StatusContext, resetSafetyEpoch: boolean) {
|
|
1033
|
+
clearGoalRecovery();
|
|
1034
|
+
clearStaleGoalToolCallBlock();
|
|
1035
|
+
if (resetSafetyEpoch) clearBudgetWrapUp();
|
|
1036
|
+
const activeGoalId =
|
|
1037
|
+
runtime.activeGoal?.status === "active" ? runtime.activeGoal.id : undefined;
|
|
1038
|
+
runtime.beginAgentRun(activeGoalId ?? null, activeGoalId ? "manual" : undefined);
|
|
1039
|
+
if (resetSafetyEpoch && activeGoalId) runtime.resetActiveSafetyEpoch(ctx);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
890
1042
|
function hasPendingSkipForGoal(goalId: string) {
|
|
891
1043
|
return (
|
|
892
1044
|
runtime.pendingQueueAction?.kind === "advance" &&
|
package/src/markers.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const GOAL_PROMPT_MARKER_PREFIX = "pi-goal-prompt:";
|
|
2
|
+
const CONTINUATION_MARKER_PREFIX = "pi-goal-continuation:";
|
|
3
|
+
|
|
4
|
+
const GOAL_PROMPT_MARKER_PATTERN = new RegExp(
|
|
5
|
+
`<!--\\s*${escapeRegExpText(GOAL_PROMPT_MARKER_PREFIX)}([^\\s>]+)\\s*-->`,
|
|
6
|
+
);
|
|
7
|
+
const CONTINUATION_MARKER_PATTERN = new RegExp(
|
|
8
|
+
`<!--\\s*${escapeRegExpText(CONTINUATION_MARKER_PREFIX)}([^\\s>]+)\\s*-->`,
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
export function extractGoalPromptMarker(prompt: string) {
|
|
12
|
+
return GOAL_PROMPT_MARKER_PATTERN.exec(prompt)?.[1];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function extractContinuationMarker(prompt: string) {
|
|
16
|
+
return CONTINUATION_MARKER_PATTERN.exec(prompt)?.[1];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function appendGoalPromptMarker(prompt: string, marker: string) {
|
|
20
|
+
return `${prompt}\n\n<!-- ${GOAL_PROMPT_MARKER_PREFIX}${marker} -->`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function escapeRegExpText(value: string) {
|
|
24
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
25
|
+
}
|
package/src/persistence.ts
CHANGED
|
@@ -15,6 +15,8 @@ const STATE_FILE = join(
|
|
|
15
15
|
"pi-goal-state.json",
|
|
16
16
|
);
|
|
17
17
|
|
|
18
|
+
export type SafetyPauseCause = "continuation_limit" | "no_progress";
|
|
19
|
+
|
|
18
20
|
export interface ActiveGoal {
|
|
19
21
|
id: string;
|
|
20
22
|
text: string;
|
|
@@ -27,6 +29,11 @@ export interface ActiveGoal {
|
|
|
27
29
|
timeUsedSeconds: number;
|
|
28
30
|
baselineTokens: number;
|
|
29
31
|
activeStartedAt?: number;
|
|
32
|
+
automaticModelTurns: number;
|
|
33
|
+
toolFreeRepeatCount: number;
|
|
34
|
+
lastToolFreeOutputFingerprint?: string;
|
|
35
|
+
safetyPauseCause?: SafetyPauseCause;
|
|
36
|
+
safetyResetPending?: boolean;
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
export type PendingQueueAction =
|
|
@@ -225,9 +232,26 @@ export function normalizeLoadedGoal(goal: ActiveGoal): ActiveGoal {
|
|
|
225
232
|
timeUsedSeconds: nonNegativeFiniteNumber(goal.timeUsedSeconds),
|
|
226
233
|
baselineTokens: nonNegativeFiniteNumber(goal.baselineTokens),
|
|
227
234
|
activeStartedAt: goal.status === "active" ? now : undefined,
|
|
235
|
+
automaticModelTurns: normalizeSafetyCounter(goal.automaticModelTurns),
|
|
236
|
+
toolFreeRepeatCount: normalizeSafetyCounter(goal.toolFreeRepeatCount),
|
|
237
|
+
lastToolFreeOutputFingerprint: normalizeOutputFingerprint(goal.lastToolFreeOutputFingerprint),
|
|
238
|
+
safetyPauseCause: normalizeSafetyPauseCause(goal.safetyPauseCause),
|
|
239
|
+
safetyResetPending: goal.safetyResetPending === true ? true : undefined,
|
|
228
240
|
};
|
|
229
241
|
}
|
|
230
242
|
|
|
243
|
+
function normalizeSafetyCounter(value: unknown) {
|
|
244
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : 0;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function normalizeOutputFingerprint(value: unknown) {
|
|
248
|
+
return typeof value === "string" && /^[a-f0-9]{64}$/u.test(value) ? value : undefined;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function normalizeSafetyPauseCause(value: unknown): SafetyPauseCause | undefined {
|
|
252
|
+
return value === "continuation_limit" || value === "no_progress" ? value : undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
231
255
|
export function clearLegacyPersistedGoal(cwd: string) {
|
|
232
256
|
if (!existsSync(STATE_FILE)) return;
|
|
233
257
|
const goals = readState();
|
|
@@ -270,7 +294,8 @@ function isGoal(value: unknown): value is ActiveGoal {
|
|
|
270
294
|
typeof value.tokensUsed === "number" &&
|
|
271
295
|
typeof value.timeUsedSeconds === "number" &&
|
|
272
296
|
typeof value.baselineTokens === "number" &&
|
|
273
|
-
(value.activeStartedAt === undefined || typeof value.activeStartedAt === "number")
|
|
297
|
+
(value.activeStartedAt === undefined || typeof value.activeStartedAt === "number") &&
|
|
298
|
+
(value.safetyResetPending === undefined || typeof value.safetyResetPending === "boolean")
|
|
274
299
|
);
|
|
275
300
|
}
|
|
276
301
|
|
package/src/queue.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { checkpointGoalActiveTime } from "./accounting.js";
|
|
3
3
|
import type { ActiveGoal } from "./persistence.js";
|
|
4
|
+
import { resetGoalSafetyEpoch } from "./safety.js";
|
|
4
5
|
|
|
5
6
|
export interface GoalQueueResult {
|
|
6
7
|
goal: ActiveGoal | undefined;
|
|
@@ -27,6 +28,8 @@ export function createQueuedGoal(
|
|
|
27
28
|
tokensUsed: 0,
|
|
28
29
|
timeUsedSeconds: 0,
|
|
29
30
|
baselineTokens: 0,
|
|
31
|
+
automaticModelTurns: 0,
|
|
32
|
+
toolFreeRepeatCount: 0,
|
|
30
33
|
};
|
|
31
34
|
}
|
|
32
35
|
|
|
@@ -92,5 +95,5 @@ export function activateQueuedGoal(
|
|
|
92
95
|
if (activated.tokenBudget !== undefined && activated.tokensUsed >= activated.tokenBudget) {
|
|
93
96
|
return { ...activated, status: "budget_limited", activeStartedAt: undefined };
|
|
94
97
|
}
|
|
95
|
-
return activated;
|
|
98
|
+
return activated.safetyResetPending ? resetGoalSafetyEpoch(activated) : activated;
|
|
96
99
|
}
|