@narumitw/pi-goal 0.54.17 → 0.55.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/CHANGELOG.md +17 -0
- package/README.md +21 -11
- package/dist/chunks/{chunk-MHW7VEET.ts → chunk-7RO3AIWI.ts} +457 -33
- package/dist/chunks/chunk-7RO3AIWI.ts.map +7 -0
- package/dist/chunks/{chunk-ZLKWPYIF.ts → chunk-OPW4T4QN.ts} +6 -1
- package/dist/chunks/{chunk-ZLKWPYIF.ts.map → chunk-OPW4T4QN.ts.map} +2 -2
- package/dist/chunks/{menu-3AYWQ637.ts → menu-P4DPWLVL.ts} +3 -3
- package/dist/chunks/{settings-ui-RVQTWGJ5.ts → settings-ui-X5XS74CD.ts} +39 -3
- package/dist/chunks/settings-ui-X5XS74CD.ts.map +7 -0
- package/dist/index.ts +14 -8
- package/dist/index.ts.map +2 -2
- package/docs/continuation-lifecycle.md +81 -11
- package/docs/managing-goals.md +18 -0
- package/docs/settings.md +48 -2
- package/package.json +3 -3
- package/src/lifecycle.ts +12 -3
- package/src/runtime.ts +175 -30
- package/src/settings-ui.ts +51 -2
- package/src/settings.ts +9 -0
- package/src/usage-limit-recovery.ts +162 -0
- package/src/usage-reset.ts +309 -0
- package/src/wait.ts +21 -1
- package/dist/chunks/chunk-MHW7VEET.ts.map +0 -7
- package/dist/chunks/settings-ui-RVQTWGJ5.ts.map +0 -7
- /package/dist/chunks/{menu-3AYWQ637.ts.map → menu-P4DPWLVL.ts.map} +0 -0
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import {
|
|
4
4
|
DEFAULT_GOAL_SETTINGS,
|
|
5
5
|
checkpointGoalActiveTime,
|
|
6
|
+
findFinalAssistantMessage,
|
|
6
7
|
formatDuration,
|
|
7
8
|
formatError,
|
|
8
9
|
formatTokenCount,
|
|
@@ -15,7 +16,7 @@ import {
|
|
|
15
16
|
safeGoalMenuText,
|
|
16
17
|
truncateNotification,
|
|
17
18
|
updateGoalUsage
|
|
18
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-OPW4T4QN.ts";
|
|
19
20
|
|
|
20
21
|
// src/command.ts
|
|
21
22
|
var MAX_OBJECTIVE_LENGTH = 4e3;
|
|
@@ -157,11 +158,18 @@ function normalizeGoalWait(value) {
|
|
|
157
158
|
if (!isRecord(value)) return void 0;
|
|
158
159
|
const reason = typeof value.reason === "string" ? value.reason.trim() : "";
|
|
159
160
|
if (!reason || reason.length > MAX_GOAL_WAIT_REASON_LENGTH) return void 0;
|
|
161
|
+
let usageLimit;
|
|
162
|
+
if (Object.hasOwn(value, "usageLimit")) {
|
|
163
|
+
const scope = value.usageLimit;
|
|
164
|
+
if (!isRecord(scope) || typeof scope.provider !== "string" || typeof scope.model !== "string" || typeof scope.api !== "string" || !Object.hasOwn(value, "resumeAt"))
|
|
165
|
+
return void 0;
|
|
166
|
+
usageLimit = { provider: scope.provider, model: scope.model, api: scope.api };
|
|
167
|
+
}
|
|
160
168
|
if (!Object.hasOwn(value, "resumeAt")) return { reason };
|
|
161
169
|
if (typeof value.resumeAt !== "number" || !Number.isSafeInteger(value.resumeAt) || value.resumeAt < 0 || value.resumeAt > MAX_DATE_TIMESTAMP_MS) {
|
|
162
170
|
return void 0;
|
|
163
171
|
}
|
|
164
|
-
return { reason, resumeAt: value.resumeAt };
|
|
172
|
+
return { reason, resumeAt: value.resumeAt, ...usageLimit ? { usageLimit } : {} };
|
|
165
173
|
}
|
|
166
174
|
var GoalWaitTimer = class {
|
|
167
175
|
generation = 0;
|
|
@@ -657,6 +665,320 @@ function assertGoalToolsAvailable(pi) {
|
|
|
657
665
|
);
|
|
658
666
|
}
|
|
659
667
|
|
|
668
|
+
// src/usage-reset.ts
|
|
669
|
+
var DEADLINE_MS = 15e3;
|
|
670
|
+
var MAX_TIMESTAMP_MS = 864e13;
|
|
671
|
+
var UNITS = /* @__PURE__ */ new Set(["percent", "usd", "currency", "count"]);
|
|
672
|
+
async function readUsageReset(pi, ctx, signal) {
|
|
673
|
+
try {
|
|
674
|
+
if (signal.aborted) return void 0;
|
|
675
|
+
const session = ctx.sessionManager;
|
|
676
|
+
const sessionId = session.getSessionId();
|
|
677
|
+
const model = ctx.model;
|
|
678
|
+
if (!model?.provider || !model.id || !model.name || typeof sessionId !== "string" || !sessionId) {
|
|
679
|
+
return void 0;
|
|
680
|
+
}
|
|
681
|
+
const provider = model.provider;
|
|
682
|
+
const modelId = model.id;
|
|
683
|
+
const modelName = model.name;
|
|
684
|
+
const snapshot = JSON.stringify(model);
|
|
685
|
+
const startedAt = Date.now();
|
|
686
|
+
const deadline = performance.now() + DEADLINE_MS;
|
|
687
|
+
const current = () => !signal.aborted && performance.now() < deadline && ctx.sessionManager === session && session.getSessionId() === sessionId && ctx.model === model && JSON.stringify(ctx.model) === snapshot;
|
|
688
|
+
const result = await new Promise((resolve) => {
|
|
689
|
+
const controller = new AbortController();
|
|
690
|
+
let open = true;
|
|
691
|
+
let offered = false;
|
|
692
|
+
let done = false;
|
|
693
|
+
let timer;
|
|
694
|
+
const finish = (value) => {
|
|
695
|
+
if (done) return;
|
|
696
|
+
done = true;
|
|
697
|
+
open = false;
|
|
698
|
+
clearTimeout(timer);
|
|
699
|
+
signal.removeEventListener("abort", cancel);
|
|
700
|
+
resolve(value);
|
|
701
|
+
controller.abort();
|
|
702
|
+
};
|
|
703
|
+
const cancel = () => finish();
|
|
704
|
+
try {
|
|
705
|
+
timer = setTimeout(cancel, DEADLINE_MS);
|
|
706
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
707
|
+
if (signal.aborted) {
|
|
708
|
+
finish();
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
pi.events.emit("usage:query:v1", {
|
|
712
|
+
session,
|
|
713
|
+
sessionId,
|
|
714
|
+
providerId: provider,
|
|
715
|
+
refresh: true,
|
|
716
|
+
signal: controller.signal,
|
|
717
|
+
respond(value) {
|
|
718
|
+
const accepted = open && !offered && !done;
|
|
719
|
+
if (accepted) offered = true;
|
|
720
|
+
void Promise.resolve(value).then(
|
|
721
|
+
(answer) => {
|
|
722
|
+
if (accepted && !done) finish(answer);
|
|
723
|
+
},
|
|
724
|
+
() => {
|
|
725
|
+
if (accepted && !done) finish();
|
|
726
|
+
}
|
|
727
|
+
);
|
|
728
|
+
return accepted;
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
if (!current()) finish();
|
|
732
|
+
} catch {
|
|
733
|
+
finish();
|
|
734
|
+
} finally {
|
|
735
|
+
open = false;
|
|
736
|
+
}
|
|
737
|
+
if (!offered) finish();
|
|
738
|
+
});
|
|
739
|
+
if (!current()) return void 0;
|
|
740
|
+
const reset = selectReset(result, provider, modelId, modelName, startedAt);
|
|
741
|
+
return current() ? reset : void 0;
|
|
742
|
+
} catch {
|
|
743
|
+
return void 0;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
function dataProperty(value, key) {
|
|
747
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
748
|
+
if (!descriptor) return void 0;
|
|
749
|
+
if (!("value" in descriptor)) throw new TypeError("Usage fields must be data properties");
|
|
750
|
+
return descriptor.value;
|
|
751
|
+
}
|
|
752
|
+
function snapshotFields(value, fields) {
|
|
753
|
+
if (!isRecord4(value)) throw new TypeError("Usage records must be objects");
|
|
754
|
+
const snapshot = {};
|
|
755
|
+
for (const field of fields) snapshot[field] = dataProperty(value, field);
|
|
756
|
+
return snapshot;
|
|
757
|
+
}
|
|
758
|
+
function snapshotArray(value, project = (item) => item) {
|
|
759
|
+
if (!Array.isArray(value)) throw new TypeError("Usage lists must be arrays");
|
|
760
|
+
const length = dataProperty(value, "length");
|
|
761
|
+
if (!isFiniteNumber(length) || !Number.isInteger(length) || length < 0 || length > 4294967295) {
|
|
762
|
+
throw new TypeError("Usage arrays must have a valid length");
|
|
763
|
+
}
|
|
764
|
+
const snapshot = [];
|
|
765
|
+
for (let index = 0; index < length; index++) snapshot.push(project(dataProperty(value, String(index))));
|
|
766
|
+
return snapshot;
|
|
767
|
+
}
|
|
768
|
+
function snapshotBucket(value) {
|
|
769
|
+
const bucket = snapshotFields(value, [
|
|
770
|
+
"id",
|
|
771
|
+
"label",
|
|
772
|
+
"groupId",
|
|
773
|
+
"groupLabel",
|
|
774
|
+
"modelKeys",
|
|
775
|
+
"used",
|
|
776
|
+
"remaining",
|
|
777
|
+
"limit",
|
|
778
|
+
"unit",
|
|
779
|
+
"period",
|
|
780
|
+
"windowMinutes",
|
|
781
|
+
"resetsAt"
|
|
782
|
+
]);
|
|
783
|
+
if (bucket.modelKeys !== void 0) bucket.modelKeys = snapshotArray(bucket.modelKeys);
|
|
784
|
+
return bucket;
|
|
785
|
+
}
|
|
786
|
+
function snapshotResult(input) {
|
|
787
|
+
const value = snapshotFields(input, ["status", "providerId", "source", "expiresAt", "report"]);
|
|
788
|
+
const report = snapshotFields(value.report, [
|
|
789
|
+
"providerId",
|
|
790
|
+
"providerName",
|
|
791
|
+
"capturedAt",
|
|
792
|
+
"source",
|
|
793
|
+
"semantics",
|
|
794
|
+
"accountLabel",
|
|
795
|
+
"buckets",
|
|
796
|
+
"metrics",
|
|
797
|
+
"notes"
|
|
798
|
+
]);
|
|
799
|
+
value.report = report;
|
|
800
|
+
report.semantics = snapshotFields(report.semantics, ["kind", "label"]);
|
|
801
|
+
report.buckets = snapshotArray(report.buckets, snapshotBucket);
|
|
802
|
+
report.metrics = snapshotArray(
|
|
803
|
+
report.metrics,
|
|
804
|
+
(metric) => snapshotFields(metric, ["id", "label", "value", "unit", "currency"])
|
|
805
|
+
);
|
|
806
|
+
if (report.notes !== void 0) report.notes = snapshotArray(report.notes);
|
|
807
|
+
return value;
|
|
808
|
+
}
|
|
809
|
+
function selectReset(input, provider, modelId, modelName, startedAt) {
|
|
810
|
+
const value = snapshotResult(input);
|
|
811
|
+
const now = Date.now();
|
|
812
|
+
if (value.status !== "ready" || value.providerId !== provider || value.source !== "refresh" || !isTimestamp(value.expiresAt) || value.expiresAt <= now) {
|
|
813
|
+
return void 0;
|
|
814
|
+
}
|
|
815
|
+
const report = value.report;
|
|
816
|
+
if (!isRecord4(report) || report.providerId !== provider || typeof report.providerName !== "string" || typeof report.source !== "string" || !isTimestamp(report.capturedAt) || report.capturedAt < startedAt || report.capturedAt > now || !isRecord4(report.semantics) || typeof report.semantics.kind !== "string" || !["consumer-subscription", "api-key", "project"].includes(report.semantics.kind) || typeof report.semantics.label !== "string" || !optional(report.accountLabel, isString) || !optional(report.notes, isStrings) || !Array.isArray(report.buckets) || !Array.isArray(report.metrics) || ![...report.metrics].every(isMetric)) {
|
|
817
|
+
return void 0;
|
|
818
|
+
}
|
|
819
|
+
const buckets = [...report.buckets];
|
|
820
|
+
if (!buckets.every(isBucket)) return void 0;
|
|
821
|
+
let latest;
|
|
822
|
+
const constraints = /* @__PURE__ */ new Set();
|
|
823
|
+
for (const bucket of buckets) {
|
|
824
|
+
if (constraints.has(bucket.id)) return void 0;
|
|
825
|
+
constraints.add(bucket.id);
|
|
826
|
+
if (!bucket.modelKeys?.length) return void 0;
|
|
827
|
+
if (!bucket.modelKeys.includes(modelId) && !bucket.modelKeys.includes(modelName)) continue;
|
|
828
|
+
const { used, remaining, limit } = bucket;
|
|
829
|
+
if (used !== void 0 && used < 0 || limit !== void 0 && limit <= 0) return void 0;
|
|
830
|
+
const byRemaining = remaining === void 0 ? void 0 : remaining <= 0;
|
|
831
|
+
const byUsage = used === void 0 || limit === void 0 ? void 0 : used >= limit;
|
|
832
|
+
if (byRemaining === void 0 && byUsage === void 0) return void 0;
|
|
833
|
+
if (byRemaining !== void 0 && byUsage !== void 0 && byRemaining !== byUsage) return void 0;
|
|
834
|
+
if (!(byRemaining ?? byUsage)) continue;
|
|
835
|
+
if (!isTimestamp(bucket.resetsAt) || bucket.resetsAt <= now) return void 0;
|
|
836
|
+
latest = Math.max(latest ?? bucket.resetsAt, bucket.resetsAt);
|
|
837
|
+
}
|
|
838
|
+
const completedAt = Date.now();
|
|
839
|
+
return isTimestamp(latest) && latest > completedAt && value.expiresAt > completedAt && report.capturedAt <= completedAt ? latest : void 0;
|
|
840
|
+
}
|
|
841
|
+
function isBucket(value) {
|
|
842
|
+
return isRecord4(value) && typeof value.id === "string" && typeof value.label === "string" && typeof value.unit === "string" && UNITS.has(value.unit) && optional(value.groupId, isString) && optional(value.groupLabel, isString) && optional(value.modelKeys, isStrings) && optional(value.used, isFiniteNumber) && optional(value.remaining, isFiniteNumber) && optional(value.limit, isFiniteNumber) && optional(value.period, isString) && optional(value.windowMinutes, (number) => isFiniteNumber(number) && number > 0) && optional(value.resetsAt, isTimestamp);
|
|
843
|
+
}
|
|
844
|
+
function isMetric(value) {
|
|
845
|
+
return isRecord4(value) && typeof value.id === "string" && typeof value.label === "string" && (typeof value.value === "string" || isFiniteNumber(value.value)) && optional(value.unit, (unit) => typeof unit === "string" && UNITS.has(unit)) && optional(value.currency, isString);
|
|
846
|
+
}
|
|
847
|
+
function isRecord4(value) {
|
|
848
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
849
|
+
}
|
|
850
|
+
function isString(value) {
|
|
851
|
+
return typeof value === "string";
|
|
852
|
+
}
|
|
853
|
+
function isStrings(value) {
|
|
854
|
+
return Array.isArray(value) && [...value].every(isString);
|
|
855
|
+
}
|
|
856
|
+
function isFiniteNumber(value) {
|
|
857
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
858
|
+
}
|
|
859
|
+
function isTimestamp(value) {
|
|
860
|
+
return isFiniteNumber(value) && Number.isSafeInteger(value) && value >= 0 && value <= MAX_TIMESTAMP_MS;
|
|
861
|
+
}
|
|
862
|
+
function optional(value, validate) {
|
|
863
|
+
return value === void 0 || validate(value);
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// src/usage-limit-recovery.ts
|
|
867
|
+
var USAGE_LIMIT_RETRY_MS = 15 * 60 * 1e3;
|
|
868
|
+
var UsageLimitRecovery = class {
|
|
869
|
+
query;
|
|
870
|
+
runtime;
|
|
871
|
+
constructor(runtime) {
|
|
872
|
+
this.runtime = runtime;
|
|
873
|
+
}
|
|
874
|
+
cancel() {
|
|
875
|
+
const query = this.query;
|
|
876
|
+
this.query = void 0;
|
|
877
|
+
query?.controller.abort();
|
|
878
|
+
}
|
|
879
|
+
enter(ctx, errorMessage, origin) {
|
|
880
|
+
const runtime = this.runtime;
|
|
881
|
+
const goal = runtime.activeGoal;
|
|
882
|
+
if (!goal || !runtime.settings.retryUsageLimits || !runtime.ownsWorkflow(goal)) return false;
|
|
883
|
+
if (origin && !sameUsageLimitScope(origin, usageLimitScope(ctx))) {
|
|
884
|
+
this.stop(ctx, "model changed before usage-limit settlement");
|
|
885
|
+
return true;
|
|
886
|
+
}
|
|
887
|
+
if (runtime.enforceAutomaticTurnLimit(ctx, false) || runtime.enforceNoProgressLimit(ctx) || runtime.limitActiveGoalForBudget(ctx, false))
|
|
888
|
+
return true;
|
|
889
|
+
if (!runtime.goalToolsAvailable()) return runtime.pauseGoalForUnavailableTools(ctx, false);
|
|
890
|
+
const waiting = {
|
|
891
|
+
reason: `Provider usage limit${errorMessage ? `: ${truncateNotification(errorMessage)}` : ""}`,
|
|
892
|
+
resumeAt: Date.now() + USAGE_LIMIT_RETRY_MS,
|
|
893
|
+
usageLimit: usageLimitScope(ctx)
|
|
894
|
+
};
|
|
895
|
+
const entered = runtime.enterGoalWait(ctx, goal.id, waiting);
|
|
896
|
+
if (entered?.id !== goal.id || entered.waiting !== waiting || !runtime.ownsWorkflow(entered)) return true;
|
|
897
|
+
notifyTerminal(
|
|
898
|
+
ctx.ui,
|
|
899
|
+
"Goal waiting after provider usage limit. Automatic recovery uses a reset hint when available, otherwise retries in 15 minutes. Pause or clear the Goal to stop recovery.",
|
|
900
|
+
"warning"
|
|
901
|
+
);
|
|
902
|
+
if (runtime.activeGoal?.waiting === waiting && runtime.ownsWorkflow()) this.refreshHint(ctx, goal.id, waiting);
|
|
903
|
+
return true;
|
|
904
|
+
}
|
|
905
|
+
stopIfDisabled(ctx) {
|
|
906
|
+
if (!this.scope() || this.runtime.settings.retryUsageLimits) return false;
|
|
907
|
+
this.stop(ctx, "automatic usage-limit recovery disabled");
|
|
908
|
+
return true;
|
|
909
|
+
}
|
|
910
|
+
modelChanged(ctx) {
|
|
911
|
+
const scope = this.scope();
|
|
912
|
+
if (!scope || sameUsageLimitScope(scope, usageLimitScope(ctx))) return;
|
|
913
|
+
this.stop(ctx, "model changed during usage-limit recovery");
|
|
914
|
+
}
|
|
915
|
+
matches(ctx, waiting) {
|
|
916
|
+
return !waiting.usageLimit || sameUsageLimitScope(waiting.usageLimit, usageLimitScope(ctx));
|
|
917
|
+
}
|
|
918
|
+
scope() {
|
|
919
|
+
const runtime = this.runtime;
|
|
920
|
+
return runtime.activeGoal?.waiting?.usageLimit ?? runtime.continuationIntent?.usageLimitRetry ?? runtime.continuationDelivery?.usageLimitRetry ?? runtime.agentRunUsageLimitRetry ?? (runtime.goalRecovery?.goalId === runtime.activeGoal?.id ? runtime.goalRecovery?.usageLimitScope : void 0);
|
|
921
|
+
}
|
|
922
|
+
stop(ctx, reason) {
|
|
923
|
+
const runtime = this.runtime;
|
|
924
|
+
const goal = runtime.activeGoal;
|
|
925
|
+
if (!goal || !runtime.ownsWorkflow(goal)) return;
|
|
926
|
+
runtime.stopActiveGoal(ctx, {
|
|
927
|
+
kind: runtime.agentRunGoalId === goal.id ? "agent_interruption" : "recovery_ended",
|
|
928
|
+
expectedGoalId: goal.id,
|
|
929
|
+
status: "usage_limited",
|
|
930
|
+
reason
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
refreshHint(ctx, goalId, waiting) {
|
|
934
|
+
const session = ctx.sessionManager;
|
|
935
|
+
if (!ctx.model || typeof session?.getSessionId !== "function") return;
|
|
936
|
+
const generation = this.runtime.menuGeneration;
|
|
937
|
+
const query = { controller: new AbortController(), waiting };
|
|
938
|
+
this.query = query;
|
|
939
|
+
const current = () => this.query === query && !query.controller.signal.aborted && generation === this.runtime.menuGeneration && this.runtime.settings.retryUsageLimits && this.runtime.activeGoal?.id === goalId && this.runtime.activeGoal.waiting === waiting && this.runtime.ownsWorkflow() && this.matches(ctx, waiting);
|
|
940
|
+
void readUsageReset(
|
|
941
|
+
this.runtime.pi,
|
|
942
|
+
ctx,
|
|
943
|
+
query.controller.signal
|
|
944
|
+
).then((resetAt) => {
|
|
945
|
+
if (!current()) return;
|
|
946
|
+
this.query = void 0;
|
|
947
|
+
query.controller.abort();
|
|
948
|
+
if (resetAt === void 0) return;
|
|
949
|
+
const goal = this.runtime.activeGoal;
|
|
950
|
+
if (!goal || goal.id !== goalId || goal.waiting !== waiting) return;
|
|
951
|
+
const nextWait = { ...waiting, resumeAt: Math.max(Date.now() + MIN_GOAL_WAIT_DELAY_MS, resetAt) };
|
|
952
|
+
const hintedGoal = { ...goal, waiting: nextWait, updatedAt: Date.now() };
|
|
953
|
+
this.runtime.activeGoal = hintedGoal;
|
|
954
|
+
try {
|
|
955
|
+
this.runtime.persistGoal(hintedGoal);
|
|
956
|
+
} catch {
|
|
957
|
+
if (this.runtime.activeGoal !== hintedGoal || !this.runtime.ownsWorkflow()) return;
|
|
958
|
+
this.runtime.activeGoal = goal;
|
|
959
|
+
try {
|
|
960
|
+
this.runtime.persistGoal(goal);
|
|
961
|
+
} catch {
|
|
962
|
+
}
|
|
963
|
+
if (this.runtime.activeGoal === goal && this.runtime.ownsWorkflow()) this.runtime.restoreGoalWaitTimer(ctx);
|
|
964
|
+
return;
|
|
965
|
+
}
|
|
966
|
+
if (this.runtime.activeGoal === hintedGoal && this.runtime.ownsWorkflow() && generation === this.runtime.menuGeneration)
|
|
967
|
+
this.runtime.restoreGoalWaitTimer(ctx);
|
|
968
|
+
}).catch(() => {
|
|
969
|
+
}).finally(() => {
|
|
970
|
+
if (this.query === query) this.query = void 0;
|
|
971
|
+
query.controller.abort();
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
};
|
|
975
|
+
function usageLimitScope(ctx) {
|
|
976
|
+
return { provider: ctx.model?.provider ?? "", model: ctx.model?.id ?? "", api: ctx.model?.api ?? "" };
|
|
977
|
+
}
|
|
978
|
+
function sameUsageLimitScope(left, right) {
|
|
979
|
+
return left.provider === right.provider && left.model === right.model && left.api === right.api;
|
|
980
|
+
}
|
|
981
|
+
|
|
660
982
|
// src/workflow-mutex.ts
|
|
661
983
|
var WORKFLOW_MUTEX_CHANNEL = "workflow:mutex:v1";
|
|
662
984
|
var AGENT_WORKFLOW_GROUP = "agent-workflow";
|
|
@@ -752,6 +1074,9 @@ var GoalRuntime = class {
|
|
|
752
1074
|
/** Terminal details captured for the matching persisted-state snapshot. */
|
|
753
1075
|
terminalDetails;
|
|
754
1076
|
goalStateSink;
|
|
1077
|
+
settingsStatePublications;
|
|
1078
|
+
settingsCommitEffects;
|
|
1079
|
+
usageLimitRecovery;
|
|
755
1080
|
legacyQueueState;
|
|
756
1081
|
legacyExperimentalGoalsSetting = false;
|
|
757
1082
|
completionStatusTimer;
|
|
@@ -769,11 +1094,14 @@ var GoalRuntime = class {
|
|
|
769
1094
|
agentRunGoalId;
|
|
770
1095
|
agentRunOrigin;
|
|
771
1096
|
agentRunToolAttempted = false;
|
|
1097
|
+
agentRunUsageLimitRetry;
|
|
1098
|
+
agentRunResponseSeen = false;
|
|
772
1099
|
guardAbortGoalId;
|
|
773
1100
|
staleGoalToolCallsBlocked = false;
|
|
774
1101
|
workflowMutex;
|
|
775
1102
|
workflowOwner;
|
|
776
1103
|
workflowSession;
|
|
1104
|
+
workflowSessionId;
|
|
777
1105
|
pendingGoalContract;
|
|
778
1106
|
pendingGoalPromptMarkers = /* @__PURE__ */ new Map();
|
|
779
1107
|
claimedGoalPromptMarkers = /* @__PURE__ */ new Map();
|
|
@@ -787,6 +1115,7 @@ var GoalRuntime = class {
|
|
|
787
1115
|
constructor(pi) {
|
|
788
1116
|
this.pi = pi;
|
|
789
1117
|
this.workflowMutex = new WorkflowMutex(pi);
|
|
1118
|
+
this.usageLimitRecovery = new UsageLimitRecovery(this);
|
|
790
1119
|
}
|
|
791
1120
|
goalToolsAvailable() {
|
|
792
1121
|
return goalToolsAvailable(this.pi);
|
|
@@ -797,6 +1126,7 @@ var GoalRuntime = class {
|
|
|
797
1126
|
bindWorkflowSession(session) {
|
|
798
1127
|
this.pendingGoalContract = void 0;
|
|
799
1128
|
this.workflowSession = session;
|
|
1129
|
+
this.workflowSessionId = goalWaitSessionId(session);
|
|
800
1130
|
this.workflowOwner = void 0;
|
|
801
1131
|
this.workflowMutex.bindSession(session);
|
|
802
1132
|
}
|
|
@@ -806,9 +1136,10 @@ var GoalRuntime = class {
|
|
|
806
1136
|
this.workflowMutex.unbindSession(session);
|
|
807
1137
|
this.workflowOwner = void 0;
|
|
808
1138
|
this.workflowSession = void 0;
|
|
1139
|
+
this.workflowSessionId = void 0;
|
|
809
1140
|
}
|
|
810
1141
|
acquireWorkflow(session) {
|
|
811
|
-
if (session && typeof session === "object" && this.workflowSession !== session) {
|
|
1142
|
+
if (session && typeof session === "object" && (this.workflowSession !== session || this.workflowSessionId !== goalWaitSessionId(session))) {
|
|
812
1143
|
this.bindWorkflowSession(session);
|
|
813
1144
|
}
|
|
814
1145
|
if (this.workflowMutex.isOwner(this.workflowOwner)) return true;
|
|
@@ -818,7 +1149,11 @@ var GoalRuntime = class {
|
|
|
818
1149
|
return true;
|
|
819
1150
|
}
|
|
820
1151
|
ownsWorkflow(goal = this.activeGoal) {
|
|
821
|
-
|
|
1152
|
+
try {
|
|
1153
|
+
return goal?.status === "active" && this.workflowMutex.isOwner(this.workflowOwner) && this.workflowSessionId === goalWaitSessionId(this.workflowSession);
|
|
1154
|
+
} catch {
|
|
1155
|
+
return false;
|
|
1156
|
+
}
|
|
822
1157
|
}
|
|
823
1158
|
releaseWorkflow() {
|
|
824
1159
|
const owner = this.workflowOwner;
|
|
@@ -832,17 +1167,58 @@ var GoalRuntime = class {
|
|
|
832
1167
|
this.goalStateSink = sink;
|
|
833
1168
|
}
|
|
834
1169
|
publishGoalState(snapshot) {
|
|
1170
|
+
if (this.settingsStatePublications) {
|
|
1171
|
+
this.settingsStatePublications.push(snapshot);
|
|
1172
|
+
return;
|
|
1173
|
+
}
|
|
835
1174
|
try {
|
|
836
1175
|
this.goalStateSink?.(snapshot);
|
|
837
1176
|
} catch {
|
|
838
1177
|
}
|
|
839
1178
|
}
|
|
1179
|
+
/** Publish managed-run transitions only after the settings transaction commits. */
|
|
1180
|
+
withSettingsPublication(apply) {
|
|
1181
|
+
const previous = this.settingsStatePublications;
|
|
1182
|
+
const previousEffects = this.settingsCommitEffects;
|
|
1183
|
+
const pending = [];
|
|
1184
|
+
const effects = [];
|
|
1185
|
+
this.settingsStatePublications = pending;
|
|
1186
|
+
this.settingsCommitEffects = effects;
|
|
1187
|
+
try {
|
|
1188
|
+
apply();
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
this.settingsStatePublications = previous;
|
|
1191
|
+
this.settingsCommitEffects = previousEffects;
|
|
1192
|
+
throw error;
|
|
1193
|
+
}
|
|
1194
|
+
this.settingsStatePublications = previous;
|
|
1195
|
+
this.settingsCommitEffects = previousEffects;
|
|
1196
|
+
if (previousEffects) previousEffects.push(...effects);
|
|
1197
|
+
else for (const effect of effects) effect();
|
|
1198
|
+
for (const snapshot of pending) this.publishGoalState(snapshot);
|
|
1199
|
+
}
|
|
1200
|
+
afterSettingsCommit(ctx, goalId, effect) {
|
|
1201
|
+
if (!this.settingsCommitEffects) {
|
|
1202
|
+
effect();
|
|
1203
|
+
return;
|
|
1204
|
+
}
|
|
1205
|
+
this.settingsCommitEffects.push(() => {
|
|
1206
|
+
if (this.activeGoal?.id !== goalId) return;
|
|
1207
|
+
try {
|
|
1208
|
+
effect();
|
|
1209
|
+
} catch (error) {
|
|
1210
|
+
notifyWhenSessionAlive(ctx, `Goal settings saved, but cleanup failed: ${formatError(error)}`, "error");
|
|
1211
|
+
}
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
840
1214
|
replaceMenuSession() {
|
|
1215
|
+
this.usageLimitRecovery.cancel();
|
|
841
1216
|
this.menuGeneration += 1;
|
|
842
1217
|
this.menuController.abort(new DOMException("Goal session replaced", "AbortError"));
|
|
843
1218
|
this.menuController = new AbortController();
|
|
844
1219
|
}
|
|
845
1220
|
closeMenuSession() {
|
|
1221
|
+
this.usageLimitRecovery.cancel();
|
|
846
1222
|
this.menuGeneration += 1;
|
|
847
1223
|
this.menuController.abort(new DOMException("Goal session shut down", "AbortError"));
|
|
848
1224
|
}
|
|
@@ -855,10 +1231,12 @@ var GoalRuntime = class {
|
|
|
855
1231
|
hasActiveGoalRecovery() {
|
|
856
1232
|
return Boolean(this.activeGoal && this.goalRecovery?.goalId === this.activeGoal.id);
|
|
857
1233
|
}
|
|
858
|
-
beginAgentRun(goalId, origin) {
|
|
1234
|
+
beginAgentRun(goalId, origin, usageLimitRetry) {
|
|
859
1235
|
this.agentRunGoalId = goalId;
|
|
860
1236
|
this.agentRunOrigin = origin;
|
|
861
1237
|
this.agentRunToolAttempted = false;
|
|
1238
|
+
this.agentRunUsageLimitRetry = usageLimitRetry;
|
|
1239
|
+
this.agentRunResponseSeen = false;
|
|
862
1240
|
}
|
|
863
1241
|
beginRecoveryRunIfNeeded() {
|
|
864
1242
|
if (this.agentRunGoalId !== void 0 || !this.activeGoal) return;
|
|
@@ -873,7 +1251,8 @@ var GoalRuntime = class {
|
|
|
873
1251
|
const run = {
|
|
874
1252
|
goalId: this.agentRunGoalId,
|
|
875
1253
|
origin: this.agentRunOrigin,
|
|
876
|
-
toolAttempted: this.agentRunToolAttempted
|
|
1254
|
+
toolAttempted: this.agentRunToolAttempted,
|
|
1255
|
+
usageLimitRetry: this.agentRunUsageLimitRetry
|
|
877
1256
|
};
|
|
878
1257
|
this.clearAgentRun();
|
|
879
1258
|
return run;
|
|
@@ -882,6 +1261,8 @@ var GoalRuntime = class {
|
|
|
882
1261
|
this.agentRunGoalId = void 0;
|
|
883
1262
|
this.agentRunOrigin = void 0;
|
|
884
1263
|
this.agentRunToolAttempted = false;
|
|
1264
|
+
this.agentRunUsageLimitRetry = void 0;
|
|
1265
|
+
this.agentRunResponseSeen = false;
|
|
885
1266
|
}
|
|
886
1267
|
reclassifyAgentRunAsManual() {
|
|
887
1268
|
if (this.agentRunGoalId !== void 0) this.agentRunOrigin = "manual";
|
|
@@ -894,7 +1275,7 @@ var GoalRuntime = class {
|
|
|
894
1275
|
updateGoalUsage(goal, ctx, checkpointActiveTime);
|
|
895
1276
|
return true;
|
|
896
1277
|
}
|
|
897
|
-
requestContinuation(goal) {
|
|
1278
|
+
requestContinuation(goal, usageLimitRetry) {
|
|
898
1279
|
if (!this.ownsWorkflow(goal)) return false;
|
|
899
1280
|
if (goal.waiting || this.hasContinuationWorkForGoal(goal.id)) return false;
|
|
900
1281
|
const marker = continuationMarker(goal);
|
|
@@ -902,13 +1283,14 @@ var GoalRuntime = class {
|
|
|
902
1283
|
goalId: goal.id,
|
|
903
1284
|
iteration: goal.iteration,
|
|
904
1285
|
marker,
|
|
905
|
-
prompt: buildContinuePrompt(goal, marker)
|
|
1286
|
+
prompt: buildContinuePrompt(goal, marker),
|
|
1287
|
+
...usageLimitRetry ? { usageLimitRetry } : {}
|
|
906
1288
|
};
|
|
907
1289
|
return true;
|
|
908
1290
|
}
|
|
909
1291
|
dispatchContinuationIfSettled(ctx) {
|
|
910
1292
|
const intent = this.continuationIntent;
|
|
911
|
-
if (!this.ownsWorkflow()) {
|
|
1293
|
+
if (!this.ownsWorkflow() || this.workflowSession && ctx.sessionManager !== this.workflowSession) {
|
|
912
1294
|
this.cancelContinuationWork();
|
|
913
1295
|
return false;
|
|
914
1296
|
}
|
|
@@ -921,7 +1303,7 @@ var GoalRuntime = class {
|
|
|
921
1303
|
this.continuationIntent = void 0;
|
|
922
1304
|
return false;
|
|
923
1305
|
}
|
|
924
|
-
if (this.enforceAutomaticTurnLimit(ctx, false) || this.enforceNoProgressLimit(ctx)) {
|
|
1306
|
+
if (this.limitActiveGoalForBudget(ctx, false) || this.enforceAutomaticTurnLimit(ctx, false) || this.enforceNoProgressLimit(ctx)) {
|
|
925
1307
|
return false;
|
|
926
1308
|
}
|
|
927
1309
|
if (ctx.isIdle?.() !== true || hasPendingMessages(ctx)) return false;
|
|
@@ -958,10 +1340,13 @@ var GoalRuntime = class {
|
|
|
958
1340
|
activeStartedAt: void 0,
|
|
959
1341
|
updatedAt: Date.now()
|
|
960
1342
|
};
|
|
961
|
-
this.
|
|
962
|
-
this.
|
|
1343
|
+
const waitingGoal = this.activeGoal;
|
|
1344
|
+
this.persistGoal(waitingGoal);
|
|
1345
|
+
if (this.activeGoal !== waitingGoal || !this.ownsWorkflow(waitingGoal)) return void 0;
|
|
1346
|
+
this.updateStatus(ctx, waitingGoal);
|
|
1347
|
+
if (this.activeGoal !== waitingGoal || !this.ownsWorkflow(waitingGoal)) return void 0;
|
|
963
1348
|
this.restoreGoalWaitTimer(ctx);
|
|
964
|
-
return
|
|
1349
|
+
return waitingGoal;
|
|
965
1350
|
}
|
|
966
1351
|
clearGoalWait(ctx, goalId) {
|
|
967
1352
|
const goal = this.activeGoal;
|
|
@@ -972,9 +1357,10 @@ var GoalRuntime = class {
|
|
|
972
1357
|
checkpointGoalActiveTime(nextGoal, now, true);
|
|
973
1358
|
nextGoal.updatedAt = now;
|
|
974
1359
|
this.activeGoal = nextGoal;
|
|
975
|
-
this.persistGoal(
|
|
976
|
-
this.
|
|
977
|
-
|
|
1360
|
+
this.persistGoal(nextGoal);
|
|
1361
|
+
if (this.activeGoal !== nextGoal || !this.ownsWorkflow(nextGoal)) return false;
|
|
1362
|
+
this.updateStatus(ctx, nextGoal);
|
|
1363
|
+
return this.activeGoal === nextGoal && this.ownsWorkflow(nextGoal);
|
|
978
1364
|
}
|
|
979
1365
|
restoreGoalWaitTimer(ctx) {
|
|
980
1366
|
this.clearGoalWaitTimer();
|
|
@@ -982,15 +1368,25 @@ var GoalRuntime = class {
|
|
|
982
1368
|
if (!this.ownsWorkflow(goal)) return false;
|
|
983
1369
|
const resumeAt = goal?.status === "active" ? goal.waiting?.resumeAt : void 0;
|
|
984
1370
|
if (!goal || resumeAt === void 0) return false;
|
|
1371
|
+
if (this.usageLimitRecovery.stopIfDisabled(ctx)) return false;
|
|
1372
|
+
if (goal.waiting && !this.usageLimitRecovery.matches(ctx, goal.waiting)) {
|
|
1373
|
+
this.usageLimitRecovery.modelChanged(ctx);
|
|
1374
|
+
return false;
|
|
1375
|
+
}
|
|
985
1376
|
this.scheduleGoalWaitTimer(ctx, goal.id, resumeAt);
|
|
986
1377
|
return true;
|
|
987
1378
|
}
|
|
988
1379
|
dispatchDueGoalWait(ctx) {
|
|
989
1380
|
const goal = this.activeGoal;
|
|
990
|
-
if (!this.ownsWorkflow(goal)) return false;
|
|
1381
|
+
if (!this.ownsWorkflow(goal) || this.workflowSession && ctx.sessionManager !== this.workflowSession) return false;
|
|
991
1382
|
const waiting = goal?.status === "active" ? goal.waiting : void 0;
|
|
992
1383
|
const resumeAt = waiting?.resumeAt;
|
|
993
1384
|
if (!goal || !waiting || resumeAt === void 0) return false;
|
|
1385
|
+
if (this.usageLimitRecovery.stopIfDisabled(ctx)) return false;
|
|
1386
|
+
if (!this.usageLimitRecovery.matches(ctx, waiting)) {
|
|
1387
|
+
this.usageLimitRecovery.modelChanged(ctx);
|
|
1388
|
+
return false;
|
|
1389
|
+
}
|
|
994
1390
|
const retry = this.goalWaitDeadlineRetry;
|
|
995
1391
|
const matchingRetry = retry?.goalId === goal.id && retry.resumeAt === resumeAt ? retry : void 0;
|
|
996
1392
|
if (matchingRetry?.exhausted) return false;
|
|
@@ -1002,7 +1398,7 @@ var GoalRuntime = class {
|
|
|
1002
1398
|
if (!this.clearGoalWait(ctx, goal.id)) return false;
|
|
1003
1399
|
const resumedGoal = this.activeGoal;
|
|
1004
1400
|
if (!resumedGoal || resumedGoal.id !== goal.id || resumedGoal.status !== "active") return false;
|
|
1005
|
-
this.requestContinuation(resumedGoal);
|
|
1401
|
+
this.requestContinuation(resumedGoal, waiting.usageLimit);
|
|
1006
1402
|
const dispatched = this.dispatchContinuationIfSettled(ctx);
|
|
1007
1403
|
if (dispatched) return true;
|
|
1008
1404
|
if (this.activeGoal?.id === goal.id && this.activeGoal.status === "active" && this.continuationIntent?.goalId === goal.id) {
|
|
@@ -1011,16 +1407,20 @@ var GoalRuntime = class {
|
|
|
1011
1407
|
return false;
|
|
1012
1408
|
}
|
|
1013
1409
|
clearGoalWaitTimer() {
|
|
1410
|
+
this.usageLimitRecovery.cancel();
|
|
1014
1411
|
this.goalWaitTimer.clear();
|
|
1015
1412
|
this.goalWaitDeadlineRetry = void 0;
|
|
1016
1413
|
}
|
|
1017
1414
|
scheduleGoalWaitTimer(ctx, goalId, wakeAt) {
|
|
1018
1415
|
const generation = this.menuGeneration;
|
|
1416
|
+
const session = ctx.sessionManager;
|
|
1417
|
+
const sessionId = goalWaitSessionId(session);
|
|
1019
1418
|
this.goalWaitTimer.schedule(wakeAt, () => {
|
|
1020
1419
|
if (generation !== this.menuGeneration || this.activeGoal?.id !== goalId || !this.ownsWorkflow(this.activeGoal)) {
|
|
1021
1420
|
return;
|
|
1022
1421
|
}
|
|
1023
1422
|
try {
|
|
1423
|
+
if (ctx.sessionManager !== session || goalWaitSessionId(session) !== sessionId) return;
|
|
1024
1424
|
this.dispatchDueGoalWait(ctx);
|
|
1025
1425
|
} catch (error) {
|
|
1026
1426
|
notifyWhenSessionAlive(ctx, `Goal wait deadline failed: ${formatError(error)}`, "error");
|
|
@@ -1070,7 +1470,7 @@ var GoalRuntime = class {
|
|
|
1070
1470
|
this.clearGoalRecoveryForGoal(goal.id);
|
|
1071
1471
|
this.clearBudgetWrapUp();
|
|
1072
1472
|
this.blockStaleGoalToolCalls();
|
|
1073
|
-
abortCurrentTurn(ctx);
|
|
1473
|
+
this.afterSettingsCommit(ctx, goal.id, () => abortCurrentTurn(ctx));
|
|
1074
1474
|
status = "paused";
|
|
1075
1475
|
break;
|
|
1076
1476
|
case "budget_limit":
|
|
@@ -1078,7 +1478,7 @@ var GoalRuntime = class {
|
|
|
1078
1478
|
this.clearGoalRecoveryForGoal(goal.id);
|
|
1079
1479
|
this.clearBudgetWrapUp();
|
|
1080
1480
|
this.blockStaleGoalToolCalls();
|
|
1081
|
-
abortCurrentTurn(ctx);
|
|
1481
|
+
this.afterSettingsCommit(ctx, goal.id, () => abortCurrentTurn(ctx));
|
|
1082
1482
|
status = "budget_limited";
|
|
1083
1483
|
terminalReason = request.reason;
|
|
1084
1484
|
break;
|
|
@@ -1089,7 +1489,7 @@ var GoalRuntime = class {
|
|
|
1089
1489
|
this.blockStaleGoalToolCalls();
|
|
1090
1490
|
if (request.abortTurn) {
|
|
1091
1491
|
this.guardAbortGoalId = goal.id;
|
|
1092
|
-
abortCurrentTurn(ctx);
|
|
1492
|
+
this.afterSettingsCommit(ctx, goal.id, () => abortCurrentTurn(ctx));
|
|
1093
1493
|
}
|
|
1094
1494
|
goal = { ...goal, safetyPauseCause: request.cause };
|
|
1095
1495
|
status = "paused";
|
|
@@ -1110,7 +1510,7 @@ var GoalRuntime = class {
|
|
|
1110
1510
|
this.clearBudgetWrapUp();
|
|
1111
1511
|
if (request.abortTurn) {
|
|
1112
1512
|
this.blockStaleGoalToolCalls();
|
|
1113
|
-
abortCurrentTurn(ctx);
|
|
1513
|
+
this.afterSettingsCommit(ctx, goal.id, () => abortCurrentTurn(ctx));
|
|
1114
1514
|
} else {
|
|
1115
1515
|
this.clearStaleGoalToolCallBlock();
|
|
1116
1516
|
}
|
|
@@ -1129,13 +1529,13 @@ var GoalRuntime = class {
|
|
|
1129
1529
|
this.cancelContinuationWork();
|
|
1130
1530
|
this.clearBudgetWrapUp();
|
|
1131
1531
|
this.blockStaleGoalToolCalls();
|
|
1132
|
-
abortCurrentTurn(ctx);
|
|
1532
|
+
this.afterSettingsCommit(ctx, goal.id, () => abortCurrentTurn(ctx));
|
|
1133
1533
|
status = request.status;
|
|
1134
1534
|
terminalReason = request.reason;
|
|
1135
1535
|
break;
|
|
1136
1536
|
case "activation_rollback":
|
|
1137
1537
|
goal = request.restoreGoal;
|
|
1138
|
-
if (request.abortTurn) abortCurrentTurn(ctx);
|
|
1538
|
+
if (request.abortTurn) this.afterSettingsCommit(ctx, goal.id, () => abortCurrentTurn(ctx));
|
|
1139
1539
|
this.blockStaleGoalToolCalls();
|
|
1140
1540
|
status = "paused";
|
|
1141
1541
|
break;
|
|
@@ -1144,10 +1544,16 @@ var GoalRuntime = class {
|
|
|
1144
1544
|
if (terminalReason !== void 0) this.setTerminalReason(this.activeGoal.id, terminalReason);
|
|
1145
1545
|
const stoppedGoal = this.activeGoal;
|
|
1146
1546
|
this.persistGoal(stoppedGoal);
|
|
1147
|
-
if (request.kind !== "blocker_report")
|
|
1547
|
+
if (request.kind !== "blocker_report") {
|
|
1548
|
+
this.afterSettingsCommit(ctx, stoppedGoal.id, () => {
|
|
1549
|
+
if (this.activeGoal?.status !== "active") this.ensureInactiveGoalContextContract(ctx);
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1148
1552
|
if (this.activeGoal?.id === stoppedGoal.id && this.activeGoal.status === stoppedGoal.status) {
|
|
1149
1553
|
this.updateStatus(ctx, stoppedGoal);
|
|
1150
|
-
this.
|
|
1554
|
+
this.afterSettingsCommit(ctx, stoppedGoal.id, () => {
|
|
1555
|
+
if (this.activeGoal?.status === stoppedGoal.status) this.releaseWorkflow();
|
|
1556
|
+
});
|
|
1151
1557
|
}
|
|
1152
1558
|
return stoppedGoal;
|
|
1153
1559
|
}
|
|
@@ -1227,10 +1633,20 @@ var GoalRuntime = class {
|
|
|
1227
1633
|
}
|
|
1228
1634
|
recordAutomaticTurn(ctx, message) {
|
|
1229
1635
|
const goal = this.activeGoal;
|
|
1230
|
-
if (goal?.status !== "active"
|
|
1231
|
-
const candidate = message;
|
|
1232
|
-
if (
|
|
1233
|
-
|
|
1636
|
+
if (goal?.status !== "active") return false;
|
|
1637
|
+
const candidate = findFinalAssistantMessage([message]);
|
|
1638
|
+
if (this.agentRunGoalId === goal.id && candidate && candidate.stopReason !== "error" && candidate.stopReason !== "aborted") {
|
|
1639
|
+
if (this.goalRecovery?.goalId === goal.id) this.goalRecovery.usageLimitScope = void 0;
|
|
1640
|
+
this.agentRunUsageLimitRetry = void 0;
|
|
1641
|
+
}
|
|
1642
|
+
if (!this.isAutomaticRunForGoal(goal.id)) return false;
|
|
1643
|
+
if (candidate?.stopReason === "aborted") return false;
|
|
1644
|
+
const quotaRejected = this.agentRunUsageLimitRetry !== void 0 && !this.agentRunResponseSeen && !this.agentRunToolAttempted && candidate && isUsageLimitedGoalInterruption(candidate) && (candidate.content ?? []).every((block) => block.type === "text" && block.text.trim() === "");
|
|
1645
|
+
this.agentRunResponseSeen = true;
|
|
1646
|
+
if (!quotaRejected) {
|
|
1647
|
+
this.agentRunUsageLimitRetry = void 0;
|
|
1648
|
+
goal.automaticModelTurns = Math.min(Number.MAX_SAFE_INTEGER, goal.automaticModelTurns + 1);
|
|
1649
|
+
}
|
|
1234
1650
|
this.recordGoalUsage(goal, ctx);
|
|
1235
1651
|
this.persistGoal(goal);
|
|
1236
1652
|
this.updateStatus(ctx, goal);
|
|
@@ -1310,6 +1726,8 @@ var GoalRuntime = class {
|
|
|
1310
1726
|
stopReason: "error",
|
|
1311
1727
|
errorMessage: recovery?.errorMessage
|
|
1312
1728
|
});
|
|
1729
|
+
if (!aborted && usageLimited && this.settings.retryUsageLimits && this.usageLimitRecovery.enter(ctx, recovery?.errorMessage, recovery?.usageLimitScope))
|
|
1730
|
+
return true;
|
|
1313
1731
|
if (!aborted && !usageLimited && recovery && recovery.kind !== "compaction_retry") {
|
|
1314
1732
|
if (this.enforceAutomaticTurnLimit(ctx, false) || this.enforceNoProgressLimit(ctx) || this.limitActiveGoalForBudget(ctx, false)) {
|
|
1315
1733
|
return true;
|
|
@@ -1416,6 +1834,7 @@ var GoalRuntime = class {
|
|
|
1416
1834
|
return true;
|
|
1417
1835
|
}
|
|
1418
1836
|
cancelContinuationWork() {
|
|
1837
|
+
this.usageLimitRecovery.cancel();
|
|
1419
1838
|
this.clearContinuationDispatchTimer();
|
|
1420
1839
|
if (this.continuationDelivery) {
|
|
1421
1840
|
this.rememberCancelledContinuationMarker(this.continuationDelivery);
|
|
@@ -1561,12 +1980,12 @@ var GoalRuntime = class {
|
|
|
1561
1980
|
if (preservesOwnedPromptAtTerminalBoundary(prompt, delivery.prompt)) {
|
|
1562
1981
|
this.continuationDelivery = void 0;
|
|
1563
1982
|
this.rememberClaimedContinuationMarker(marker, prompt);
|
|
1564
|
-
return marker.split(":", 1)[0];
|
|
1983
|
+
return { goalId: marker.split(":", 1)[0], usageLimitRetry: delivery.usageLimitRetry };
|
|
1565
1984
|
}
|
|
1566
1985
|
}
|
|
1567
1986
|
const cancelledPrompt = this.cancelledContinuationMarkers.get(marker);
|
|
1568
1987
|
if (this.claimedContinuationMarkers.get(marker) === fingerprint || cancelledPrompt && preservesOwnedPromptAtTerminalBoundary(prompt, cancelledPrompt)) {
|
|
1569
|
-
return marker.split(":", 1)[0];
|
|
1988
|
+
return { goalId: marker.split(":", 1)[0], usageLimitRetry: void 0 };
|
|
1570
1989
|
}
|
|
1571
1990
|
this.cancelContinuationWork();
|
|
1572
1991
|
return void 0;
|
|
@@ -1849,6 +2268,10 @@ function goalIdRejectionReason(goal, requestedGoalId) {
|
|
|
1849
2268
|
if (requestedGoalId !== goal.id) return "goal_id does not match the active goal";
|
|
1850
2269
|
return void 0;
|
|
1851
2270
|
}
|
|
2271
|
+
function goalWaitSessionId(session) {
|
|
2272
|
+
const manager = session;
|
|
2273
|
+
return typeof manager?.getSessionId === "function" ? manager.getSessionId() : void 0;
|
|
2274
|
+
}
|
|
1852
2275
|
function preservesOwnedPromptAtTerminalBoundary(prompt, ownedPrompt) {
|
|
1853
2276
|
return prompt === ownedPrompt || prompt.endsWith(ownedPrompt);
|
|
1854
2277
|
}
|
|
@@ -1923,6 +2346,7 @@ export {
|
|
|
1923
2346
|
GOAL_COMPLETE_TOOL,
|
|
1924
2347
|
GOAL_BLOCKED_TOOL,
|
|
1925
2348
|
GOAL_WAIT_TOOL,
|
|
2349
|
+
usageLimitScope,
|
|
1926
2350
|
STATUS_KEY,
|
|
1927
2351
|
MAX_GOAL_ID_LENGTH,
|
|
1928
2352
|
isTerminalGoalStatus,
|
|
@@ -1942,4 +2366,4 @@ export {
|
|
|
1942
2366
|
isContradictoryCompletionSummary,
|
|
1943
2367
|
goalIdRejectionReason
|
|
1944
2368
|
};
|
|
1945
|
-
//# sourceMappingURL=chunk-
|
|
2369
|
+
//# sourceMappingURL=chunk-7RO3AIWI.ts.map
|