@yugimob/pi-opencode-go-rotation 1.7.3 → 1.7.4
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 +5 -5
- package/dist/config-store.d.ts +1 -1
- package/dist/config-store.js +1 -1
- package/dist/index.js +50 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,13 +60,13 @@ The extension has three recovery paths:
|
|
|
60
60
|
|
|
61
61
|
1. **Usage-verified Go quota exhaustion**: when OpenCode Go returns HTTP 429, the extension sends the active key only to `https://opencode.ai/zen/go/v1/usage`. A key with a `rate-limited` window or a window at 100% is blocked until the latest reported reset, and the next candidate is activated. With `verifyCandidates` enabled, candidate keys are checked the same way before they are selected, so an exhausted key is blocked and skipped instead of being tried. Rotation prefers keys outside transient cooldown, but can clear a cooldown rather than keep using an exhausted key. When every key is quota-blocked, rotation stops and reports the earliest reset instead of cycling.
|
|
62
62
|
2. **Transient limit errors** (`full` only): when the usage endpoint is unavailable, does not report an exhausted window, or does not finish within 10 seconds, the extension aborts a timed-out usage request, marks the current key as on cooldown, and switches to the next key that is not quota-blocked. Under `quota-only` and `off` the key stays active and the extension reports the limit instead of rotating.
|
|
63
|
-
3. **Silent stalls**: when an `opencode-go` provider request has no response or stream activity for the watchdog window, the extension aborts the hung turn and
|
|
63
|
+
3. **Silent stalls**: when an `opencode-go` provider request has no response or stream activity for the watchdog window, the extension aborts the hung turn and, after the run settles, automatically restarts the request through a hidden continuation message. There is no retry cap: every stall schedules another continuation. It rotates under `full`; otherwise the continuation uses the same key. Setting `retry.enabled: false` in pi's settings also disables the continuation.
|
|
64
64
|
|
|
65
65
|
Checks happen at decision points only: a candidate is verified when `verifyCandidates` is enabled and it is about to be selected, and a key with a recorded block is left alone until its stored reset passes. Normal requests are not pre-checked. With the interactive footer active, the periodic footer refresh also re-checks the active key and, unless `rotationPolicy` is `off`, records a block if a window is full, so the next request can rotate before it fails.
|
|
66
66
|
|
|
67
67
|
Fixed-window Go plan quota errors reported at message end follow the same policy: a parseable reset timestamp is authoritative; without one, `quota-only` and `off` record the cooldown-length block without rotating, and `full` rotates. A response-hook rotation is deduplicated so the matching message-end error cannot rotate twice.
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
Rotations driven by a 429 or a response hook rely on pi's built-in auto-retry and its `retry.maxRetries`. A watchdog abort cannot use that retry because pi skips aborted runs, so the extension schedules its own continuation, with no cap.
|
|
70
70
|
|
|
71
71
|
Cooldowns default to 60 minutes. Quota blocks expire at their persisted deadline. Manual `/opencode use <n>` and `/opencode next` clear both restrictions on the selected key.
|
|
72
72
|
|
|
@@ -115,7 +115,7 @@ Keys are stored in `~/.pi/agent/opencode-keys.json` with file permissions `0600`
|
|
|
115
115
|
"activeKeyIndex": 0,
|
|
116
116
|
"cooldownMinutes": 60,
|
|
117
117
|
"watchdogEnabled": true,
|
|
118
|
-
"watchdogIdleMs":
|
|
118
|
+
"watchdogIdleMs": 300000,
|
|
119
119
|
"rotationPolicy": "quota-only",
|
|
120
120
|
"verifyCandidates": false,
|
|
121
121
|
"cooldowns": {},
|
|
@@ -137,12 +137,12 @@ Pair with pi's auto-retry for best results. In `~/.pi/agent/settings.json`:
|
|
|
137
137
|
}
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
-
Set `maxRetries` to at least the number of keys so all keys get a chance before pi gives up.
|
|
140
|
+
Set `maxRetries` to at least the number of keys so all keys get a chance before pi gives up. This applies to 429 and response-hook rotations. The watchdog continuation ignores `maxRetries` and has no cap, so it keeps restarting a silent stall; `retry.enabled: false` turns it off.
|
|
141
141
|
|
|
142
142
|
## Limitations
|
|
143
143
|
|
|
144
144
|
- The watchdog is scoped to the `opencode-go` provider only. Other providers are not aborted, and the watchdog rotates only under `rotationPolicy` `full`.
|
|
145
|
-
- A legitimate long-running request with no stream activity can be treated as stalled and aborted; tune with `/opencode watchdog <seconds>` or disable with `/opencode watchdog off`.
|
|
145
|
+
- The watchdog defaults to a 5-minute idle window. A legitimate long-running request with no stream activity can be treated as stalled and aborted; tune with `/opencode watchdog <seconds>` or disable with `/opencode watchdog off`.
|
|
146
146
|
- Under `quota-only`, a bare 429, an HTTP 401, an unconfirmed usage result, and a watchdog stall never switch keys; the extension reports them and leaves the active key in place. `full` restores the earlier behavior.
|
|
147
147
|
- When all non-quota-blocked keys are transiently cooling down (`full` only), the extension may clear the next eligible key's cooldown. It never automatically clears or force-selects a quota-blocked key.
|
|
148
148
|
- When all keys are quota-blocked, automatic rotation stops on the current runtime key; each key becomes eligible again automatically when its stored reset deadline passes, or immediately after `/opencode reset`.
|
package/dist/config-store.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const CONFIG_PATH_ENV = "PI_OPENCODE_ROTATION_CONFIG";
|
|
2
2
|
export declare const DEFAULT_COOLDOWN_MINUTES = 60;
|
|
3
|
-
export declare const DEFAULT_WATCHDOG_IDLE_MS =
|
|
3
|
+
export declare const DEFAULT_WATCHDOG_IDLE_MS = 300000;
|
|
4
4
|
export type RotationPolicy = "off" | "quota-only" | "full";
|
|
5
5
|
export interface KeyEntry {
|
|
6
6
|
name: string;
|
package/dist/config-store.js
CHANGED
|
@@ -4,7 +4,7 @@ import { dirname, join } from "node:path";
|
|
|
4
4
|
import { randomUUID } from "node:crypto";
|
|
5
5
|
export const CONFIG_PATH_ENV = "PI_OPENCODE_ROTATION_CONFIG";
|
|
6
6
|
export const DEFAULT_COOLDOWN_MINUTES = 60;
|
|
7
|
-
export const DEFAULT_WATCHDOG_IDLE_MS =
|
|
7
|
+
export const DEFAULT_WATCHDOG_IDLE_MS = 300_000;
|
|
8
8
|
const DEFAULT_ROTATION_POLICY = "quota-only";
|
|
9
9
|
const DEFAULT_VERIFY_CANDIDATES = false;
|
|
10
10
|
const CONFIG_FILE_MODE = 0o600;
|
package/dist/index.js
CHANGED
|
@@ -14,12 +14,34 @@ const USAGE_STATUS_KEY = "opencode-go-usage";
|
|
|
14
14
|
const USAGE_STATUS_REFRESH_MS = 60_000;
|
|
15
15
|
const USAGE_STATUS_STALE_MS = 300_000;
|
|
16
16
|
const USAGE_STATUS_DEBOUNCE_MS = 2_000;
|
|
17
|
+
const WATCHDOG_RETRY_MESSAGE_TYPE = "opencode-go-timeout-retry";
|
|
18
|
+
const WATCHDOG_RETRY_PROMPT = "Continue.";
|
|
17
19
|
function getCooldownMs(config) {
|
|
18
20
|
return (config.cooldownMinutes || DEFAULT_COOLDOWN_MINUTES) * 60_000;
|
|
19
21
|
}
|
|
20
22
|
function getWatchdogIdleMs(config) {
|
|
21
23
|
return config.watchdogIdleMs > 0 ? config.watchdogIdleMs : DEFAULT_WATCHDOG_IDLE_MS;
|
|
22
24
|
}
|
|
25
|
+
function retryEnabled(ctx) {
|
|
26
|
+
try {
|
|
27
|
+
const getSettings = ctx.getSettings;
|
|
28
|
+
if (typeof getSettings !== "function")
|
|
29
|
+
return true;
|
|
30
|
+
const enabled = getSettings.call(ctx).retry?.enabled;
|
|
31
|
+
return enabled !== false;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function hasUsableKey(config, currentTime) {
|
|
38
|
+
if (config.keys.length === 0)
|
|
39
|
+
return true;
|
|
40
|
+
const keyIndex = pickAvailableKeyIndex(config, currentTime);
|
|
41
|
+
if (keyIndex === undefined)
|
|
42
|
+
return false;
|
|
43
|
+
return getQuotaBlockedUntil(config, keyIndex, currentTime) === undefined;
|
|
44
|
+
}
|
|
23
45
|
export function shouldWatchProvider(provider) {
|
|
24
46
|
return provider === PROVIDER;
|
|
25
47
|
}
|
|
@@ -670,6 +692,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
670
692
|
let watchdogAbortMessage;
|
|
671
693
|
let watchdogTimeoutInfo;
|
|
672
694
|
let watchdogRequestTimedOut = false;
|
|
695
|
+
let watchdogRetryPending = false;
|
|
673
696
|
let usageDecisionEpoch = 0;
|
|
674
697
|
let requestRateLimitState;
|
|
675
698
|
const watchdogEvents = [];
|
|
@@ -1003,7 +1026,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1003
1026
|
return { rotated: false };
|
|
1004
1027
|
const currentTime = now();
|
|
1005
1028
|
if (config.keys.length <= 1 || !allowsWatchdogRotation(config)) {
|
|
1006
|
-
return { keyName: applyActiveKey(config, ctx.modelRegistry, currentTime), rotated: false };
|
|
1029
|
+
return { keyName: applyActiveKey(config, ctx.modelRegistry, currentTime) ?? ambientKey?.name, rotated: false };
|
|
1007
1030
|
}
|
|
1008
1031
|
if (shouldRotateAfterWatchdogTimeout(timeoutInfo, rateLimitAlreadyRotated)) {
|
|
1009
1032
|
const previousIndex = config.activeKeyIndex;
|
|
@@ -1049,11 +1072,16 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1049
1072
|
activeKey: rotation.keyName,
|
|
1050
1073
|
...timeoutInfo,
|
|
1051
1074
|
});
|
|
1075
|
+
const currentTime = now();
|
|
1076
|
+
const willRetry = rotation.keyName !== undefined && hasUsableKey(config, currentTime) && retryEnabled(ctx);
|
|
1077
|
+
watchdogRetryPending = willRetry;
|
|
1052
1078
|
watchdogAbortPending = true;
|
|
1053
|
-
watchdogAbortMessage =
|
|
1079
|
+
watchdogAbortMessage = willRetry
|
|
1054
1080
|
? `OpenCode Go timeout: ${formatTimeoutInfo(timeoutInfo)}; ${rotation.rotated ? "rotated to" : "using"} ${rotation.keyName}; retrying.`
|
|
1055
|
-
:
|
|
1056
|
-
|
|
1081
|
+
: rotation.keyName
|
|
1082
|
+
? `OpenCode Go timeout: ${formatTimeoutInfo(timeoutInfo)}; using ${rotation.keyName}.`
|
|
1083
|
+
: `OpenCode Go timeout: ${formatTimeoutInfo(timeoutInfo)}; no other key available.`;
|
|
1084
|
+
ctx.ui.notify(watchdogAbortMessage, willRetry ? "info" : "warning");
|
|
1057
1085
|
ctx.abort();
|
|
1058
1086
|
},
|
|
1059
1087
|
timers: options.timers,
|
|
@@ -1101,6 +1129,7 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1101
1129
|
invalidateAutomaticDecisions();
|
|
1102
1130
|
requestRateLimitState = undefined;
|
|
1103
1131
|
clearWatchdogTimeoutGuard();
|
|
1132
|
+
watchdogRetryPending = false;
|
|
1104
1133
|
setUsageStatus(undefined);
|
|
1105
1134
|
stopUsageStatusTimer();
|
|
1106
1135
|
usageStatusCtx = ctx;
|
|
@@ -1711,11 +1740,28 @@ export function createOpencodeGoRotationExtension(options = {}) {
|
|
|
1711
1740
|
cleanupLifecycleState();
|
|
1712
1741
|
scheduleUsageStatusRefresh(ctx);
|
|
1713
1742
|
});
|
|
1743
|
+
pi.on("agent_settled", (event, ctx) => {
|
|
1744
|
+
if (!watchdogRetryPending)
|
|
1745
|
+
return;
|
|
1746
|
+
watchdogRetryPending = false;
|
|
1747
|
+
if (!event.aborted || ctx.model?.provider !== PROVIDER || !ctx.isIdle())
|
|
1748
|
+
return;
|
|
1749
|
+
if (!refreshConfig())
|
|
1750
|
+
return;
|
|
1751
|
+
if (!hasUsableKey(config, now()))
|
|
1752
|
+
return;
|
|
1753
|
+
pi.sendMessage({
|
|
1754
|
+
customType: WATCHDOG_RETRY_MESSAGE_TYPE,
|
|
1755
|
+
content: [{ type: "text", text: WATCHDOG_RETRY_PROMPT }],
|
|
1756
|
+
display: false,
|
|
1757
|
+
}, { triggerTurn: true });
|
|
1758
|
+
});
|
|
1714
1759
|
pi.on("session_shutdown", () => {
|
|
1715
1760
|
stopUsageStatusTimer();
|
|
1716
1761
|
setUsageStatus(undefined);
|
|
1717
1762
|
usageStatusCtx = undefined;
|
|
1718
1763
|
ambientKey = undefined;
|
|
1764
|
+
watchdogRetryPending = false;
|
|
1719
1765
|
cleanupLifecycleState();
|
|
1720
1766
|
});
|
|
1721
1767
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yugimob/pi-opencode-go-rotation",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.4",
|
|
4
4
|
"description": "Rotate between multiple OpenCode Go API keys with reactive 429, rate-limit, and watchdog stall detection, plus live footer usage for the active key",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|