@indigoai-us/hq-cli 5.345.214 → 5.345.215
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 +93 -0
- package/assets/scaffold/core/scripts/hq-delegate-verify.sh +5 -2
- package/dist/command-catalog.generated.d.ts +180 -13
- package/dist/command-catalog.generated.js +221 -13
- package/dist/commands/agents.d.ts +51 -1
- package/dist/commands/agents.js +154 -2
- package/dist/commands/bot-jobs.d.ts +2 -15
- package/dist/commands/bot-jobs.js +2 -94
- package/dist/commands/bot-runtime.d.ts +46 -5
- package/dist/commands/bot-runtime.js +228 -18
- package/dist/commands/bot.d.ts +16 -9
- package/dist/commands/bot.js +231 -55
- package/dist/commands/capacity.d.ts +4 -1
- package/dist/commands/capacity.js +46 -10
- package/dist/commands/cloud.d.ts +66 -13
- package/dist/commands/cloud.js +121 -40
- package/dist/commands/company-restore.d.ts +23 -0
- package/dist/commands/company-restore.js +128 -0
- package/dist/commands/company.js +2 -0
- package/dist/commands/core-delegate-w1b.js +15 -1
- package/dist/commands/core-policy.js +67 -8
- package/dist/commands/daemon.js +5 -0
- package/dist/commands/files-browse.d.ts +11 -4
- package/dist/commands/files-browse.js +21 -4
- package/dist/commands/files.d.ts +1 -1
- package/dist/commands/files.js +25 -11
- package/dist/commands/integrations-auth-rules.d.ts +27 -0
- package/dist/commands/integrations-auth-rules.js +79 -0
- package/dist/commands/integrations-connect.d.ts +10 -3
- package/dist/commands/integrations-connect.js +87 -13
- package/dist/commands/integrations-import.js +124 -12
- package/dist/commands/lanes.js +28 -11
- package/dist/commands/mesh.d.ts +2 -0
- package/dist/commands/mesh.js +60 -35
- package/dist/commands/sources.js +6 -0
- package/dist/commands/storage-offload.d.ts +12 -0
- package/dist/commands/storage-offload.js +41 -3
- package/dist/commands/sync-held.js +5 -2
- package/dist/commands/sync-mode.js +5 -0
- package/dist/commands/whoami.js +4 -0
- package/dist/lazy-commands.js +3 -5
- package/dist/lib/bot/config.d.ts +1 -0
- package/dist/lib/bot/config.js +10 -0
- package/dist/lib/bot/eventbridge-cron.d.ts +22 -0
- package/dist/lib/bot/eventbridge-cron.js +104 -0
- package/dist/lib/bot/fleet-cron.d.ts +8 -0
- package/dist/lib/bot/fleet-cron.js +35 -3
- package/dist/lib/bot/fleet-daemon.d.ts +46 -0
- package/dist/lib/bot/fleet-daemon.js +99 -1
- package/dist/lib/bot/fleet-local.d.ts +33 -1
- package/dist/lib/bot/fleet-local.js +215 -31
- package/dist/lib/bot/fleet-python.d.ts +3 -0
- package/dist/lib/bot/fleet-python.js +34 -4
- package/dist/lib/bot/fleet-runtime-sources.d.ts +15 -0
- package/dist/lib/bot/fleet-runtime-sources.js +30 -0
- package/dist/lib/bot/legacy-provider-login.d.ts +8 -0
- package/dist/lib/bot/legacy-provider-login.js +19 -2
- package/dist/lib/bot/probe.js +7 -1
- package/dist/lib/bot/prompt.js +1 -1
- package/dist/lib/bot/readiness.d.ts +8 -0
- package/dist/lib/bot/readiness.js +17 -3
- package/dist/lib/bot/run.js +20 -1
- package/dist/lib/bot/runtime/codex.js +1 -1
- package/dist/lib/bot/runtime/grok-acp.d.ts +5 -1
- package/dist/lib/bot/runtime/grok-acp.js +6 -3
- package/dist/lib/bot/runtime/index.js +19 -9
- package/dist/lib/bot/runtime/messages-stream.js +2 -2
- package/dist/lib/bot/runtime/types.d.ts +15 -0
- package/dist/lib/bot/runtime/types.js +9 -0
- package/dist/lib/bot/runtime-auth.d.ts +30 -0
- package/dist/lib/bot/runtime-auth.js +94 -3
- package/dist/lib/bot/runtime-failure.d.ts +23 -0
- package/dist/lib/bot/runtime-failure.js +43 -1
- package/dist/lib/bot/status.d.ts +2 -0
- package/dist/lib/capacity/accounts.d.ts +8 -0
- package/dist/lib/capacity/accounts.js +19 -0
- package/dist/lib/capacity/available.d.ts +154 -0
- package/dist/lib/capacity/available.js +905 -0
- package/dist/lib/capacity/codex-availability.d.ts +14 -1
- package/dist/lib/capacity/codex-availability.js +26 -1
- package/dist/lib/capacity/collectors/claude-oauth.d.ts +29 -2
- package/dist/lib/capacity/collectors/claude-oauth.js +104 -7
- package/dist/lib/capacity/collectors/codex.js +10 -7
- package/dist/lib/capacity/collectors/grok-refresh.d.ts +80 -0
- package/dist/lib/capacity/collectors/grok-refresh.js +189 -0
- package/dist/lib/capacity/collectors/grok.d.ts +5 -0
- package/dist/lib/capacity/collectors/grok.js +56 -5
- package/dist/lib/capacity/ledger.d.ts +37 -1
- package/dist/lib/capacity/ledger.js +207 -30
- package/dist/lib/capacity/project.d.ts +26 -0
- package/dist/lib/capacity/project.js +137 -8
- package/dist/lib/capacity/refusal-rows.d.ts +31 -0
- package/dist/lib/capacity/refusal-rows.js +61 -0
- package/dist/lib/capacity/refusal.d.ts +53 -2
- package/dist/lib/capacity/refusal.js +284 -65
- package/dist/lib/capacity/route.js +46 -11
- package/dist/lib/capacity/widget.d.ts +12 -1
- package/dist/lib/capacity/widget.js +91 -3
- package/dist/lib/daemon/catalog.js +11 -1
- package/dist/lib/daemon/resolver.d.ts +2 -0
- package/dist/lib/daemon/resolver.js +3 -0
- package/dist/lib/daemon/run.d.ts +9 -0
- package/dist/lib/daemon/run.js +54 -1
- package/dist/lib/daemon/server.d.ts +2 -0
- package/dist/lib/daemon/server.js +10 -1
- package/dist/lib/daemon/tcc-eperm.d.ts +153 -0
- package/dist/lib/daemon/tcc-eperm.js +310 -0
- package/dist/lib/db/sqlite-binding.d.ts +1 -0
- package/dist/lib/db/sqlite-binding.js +1 -1
- package/dist/lib/delivery/admission.d.ts +86 -0
- package/dist/lib/delivery/admission.js +148 -0
- package/dist/lib/delivery/artifacts.d.ts +105 -0
- package/dist/lib/delivery/artifacts.js +567 -0
- package/dist/lib/delivery/attempt-id.d.ts +13 -0
- package/dist/lib/delivery/attempt-id.js +16 -0
- package/dist/lib/delivery/backup.d.ts +78 -0
- package/dist/lib/delivery/backup.js +343 -0
- package/dist/lib/delivery/cache.d.ts +29 -0
- package/dist/lib/delivery/cache.js +33 -0
- package/dist/lib/delivery/contracts.d.ts +142 -0
- package/dist/lib/delivery/contracts.js +289 -0
- package/dist/lib/delivery/credentials.d.ts +54 -0
- package/dist/lib/delivery/credentials.js +119 -0
- package/dist/lib/delivery/disposable-target.d.ts +42 -0
- package/dist/lib/delivery/disposable-target.js +111 -0
- package/dist/lib/delivery/effects.d.ts +136 -0
- package/dist/lib/delivery/effects.js +541 -0
- package/dist/lib/delivery/enrollment.d.ts +146 -0
- package/dist/lib/delivery/enrollment.js +460 -0
- package/dist/lib/delivery/escalations.d.ts +51 -0
- package/dist/lib/delivery/escalations.js +60 -0
- package/dist/lib/delivery/identity.d.ts +25 -0
- package/dist/lib/delivery/identity.js +82 -0
- package/dist/lib/delivery/inbox.d.ts +116 -0
- package/dist/lib/delivery/inbox.js +260 -0
- package/dist/lib/delivery/lease.d.ts +80 -0
- package/dist/lib/delivery/lease.js +134 -0
- package/dist/lib/delivery/migrations.d.ts +255 -0
- package/dist/lib/delivery/migrations.js +823 -0
- package/dist/lib/delivery/mqtt-adapter.d.ts +50 -0
- package/dist/lib/delivery/mqtt-adapter.js +242 -0
- package/dist/lib/delivery/mqtt.d.ts +63 -0
- package/dist/lib/delivery/mqtt.js +119 -0
- package/dist/lib/delivery/outbox.d.ts +95 -0
- package/dist/lib/delivery/outbox.js +187 -0
- package/dist/lib/delivery/provenance.d.ts +129 -0
- package/dist/lib/delivery/provenance.js +93 -0
- package/dist/lib/delivery/reconciler.d.ts +215 -0
- package/dist/lib/delivery/reconciler.js +703 -0
- package/dist/lib/delivery/restore.d.ts +35 -0
- package/dist/lib/delivery/restore.js +134 -0
- package/dist/lib/delivery/rpc/client.d.ts +22 -0
- package/dist/lib/delivery/rpc/client.js +159 -0
- package/dist/lib/delivery/rpc/port.d.ts +40 -0
- package/dist/lib/delivery/rpc/port.js +16 -0
- package/dist/lib/delivery/rpc/server.d.ts +30 -0
- package/dist/lib/delivery/rpc/server.js +205 -0
- package/dist/lib/delivery/rpc/wire.d.ts +85 -0
- package/dist/lib/delivery/rpc/wire.js +143 -0
- package/dist/lib/delivery/store.d.ts +163 -0
- package/dist/lib/delivery/store.js +944 -0
- package/dist/lib/delivery/windows-service-install.d.ts +262 -0
- package/dist/lib/delivery/windows-service-install.js +892 -0
- package/dist/lib/delivery/workers/macos-job-helper.d.ts +103 -0
- package/dist/lib/delivery/workers/macos-job-helper.js +927 -0
- package/dist/lib/delivery/workers/macos.d.ts +191 -0
- package/dist/lib/delivery/workers/macos.js +943 -0
- package/dist/lib/delivery/workers/windows-job-helper.d.ts +35 -0
- package/dist/lib/delivery/workers/windows-job-helper.js +264 -0
- package/dist/lib/delivery/workers/windows-service-live.d.ts +121 -0
- package/dist/lib/delivery/workers/windows-service-live.js +711 -0
- package/dist/lib/delivery/workers/windows-service-runner.d.ts +33 -0
- package/dist/lib/delivery/workers/windows-service-runner.js +82 -0
- package/dist/lib/delivery/workers/windows-service.d.ts +140 -0
- package/dist/lib/delivery/workers/windows-service.js +584 -0
- package/dist/lib/delivery/workers/windows.d.ts +251 -0
- package/dist/lib/delivery/workers/windows.js +594 -0
- package/dist/lib/hot-channels/adapters/meetings.d.ts +15 -0
- package/dist/lib/hot-channels/adapters/meetings.js +36 -3
- package/dist/lib/hot-channels/signals-pass.d.ts +10 -0
- package/dist/lib/hot-channels/signals-pass.js +71 -2
- package/dist/lib/hot-channels/signals-redo.js +10 -2
- package/dist/lib/install/provider-wrappers.d.ts +2 -0
- package/dist/lib/install/provider-wrappers.js +68 -3
- package/dist/lib/lanes/watcher.js +35 -0
- package/dist/lib/signals/steps/extract.js +40 -4
- package/dist/schemas/hq-available-ai.v1.schema.json +76 -0
- package/dist/utils/catalog-runtime-paths.d.ts +34 -0
- package/dist/utils/catalog-runtime-paths.js +53 -0
- package/dist/utils/cognito-callback-port.d.ts +14 -0
- package/dist/utils/cognito-callback-port.js +24 -0
- package/dist/utils/cognito-session.js +3 -3
- package/dist/utils/deleted-company-notice.d.ts +59 -0
- package/dist/utils/deleted-company-notice.js +156 -0
- package/dist/utils/files-browse-acl.js +1 -1
- package/dist/utils/login-browser.d.ts +1 -1
- package/dist/utils/login-browser.js +7 -5
- package/dist/utils/resident-package-preload.js +30 -15
- package/dist/utils/vault-api.js +7 -0
- package/package.json +7 -7
|
@@ -1,27 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `hq capacity ingest refusal` — one inferred
|
|
3
|
-
* now.json rebuild. No network and no provider call.
|
|
2
|
+
* `hq capacity ingest refusal` — one inferred hold, on a limit row of its own
|
|
3
|
+
* (refusal-rows.ts), then one now.json rebuild. No network and no provider call.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* reading below 100% replaces the inference before that reset.
|
|
5
|
+
* The hold never replaces a real reading. It ends at its reset, or sooner when a
|
|
6
|
+
* newer reading of the limit it is about is below 100%.
|
|
8
7
|
*/
|
|
9
8
|
import { createHash } from "node:crypto";
|
|
10
|
-
import
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { usageLimitWindow } from "../bot/runtime-failure.js";
|
|
13
|
+
import { PROVIDER_HOME_ENV } from "../lanes/provider-home.js";
|
|
14
|
+
import { loadLanesConfig, providerQuotaDefaultHoldMs } from "../lanes/config.js";
|
|
15
|
+
import { resolveLanesHqRoot } from "../lanes/hq-root.js";
|
|
16
|
+
import { parseProviderResetTime } from "../lanes/quota.js";
|
|
17
|
+
import { REFUSAL_LIMIT_PREFIX, REFUSAL_SCOPE_RATE_LIMIT, REFUSAL_SCOPE_USAGE, isRefusalHold } from "./refusal-rows.js";
|
|
18
|
+
import { defaultProviderHome } from "./accounts.js";
|
|
19
|
+
import { LedgerCorrupt, LedgerMissing, PROVIDER_TOKEN_LEAK, latestRefusalOnFile, appendReading, readNow, rebuildNow, } from "./ledger.js";
|
|
20
|
+
import { LedgerBusy, capacityOptionsFromEnv, redactProviderTokens } from "./ledger.js";
|
|
11
21
|
export const REFUSAL_PROVIDERS = ["claude", "codex", "grok"];
|
|
12
22
|
export const REFUSAL_PARSER_VERSION = "capacity-refusal.1";
|
|
13
|
-
export const REFUSAL_DEFAULT_LIMIT_ID = "weekly";
|
|
14
23
|
export const REFUSAL_HELP = [
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
24
|
+
"A model-CLI bot's turn, hq bot probe and the lane failure path record their",
|
|
25
|
+
"own refusals. Run this by hand only for a provider failure that was classified",
|
|
26
|
+
"as a rate-limit refusal (policy",
|
|
27
|
+
"indigo-provider-failures-classified-not-masked-as-timeout), never for a",
|
|
28
|
+
"timeout that was not:",
|
|
19
29
|
"",
|
|
20
|
-
" hq capacity ingest refusal --provider <claude|codex|grok> [--limit-id <id>] [--resets-at <iso|epoch>]
|
|
30
|
+
" hq capacity ingest refusal --provider <claude|codex|grok> --account <hash> [--limit-id <id>] [--resets-at <iso|epoch>]",
|
|
21
31
|
"",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
32
|
+
"--account is required unless --limit-id names a limit that exactly one account",
|
|
33
|
+
"has. A limit id that several accounts share, or neither flag, is a usage error:",
|
|
34
|
+
"nothing is recorded against a guess, even when the provider has one account.",
|
|
35
|
+
"The refusal is a hold beside the account's readings, and never replaces one.",
|
|
36
|
+
"--limit-id names the limit it is about; without it the refusal claims no",
|
|
37
|
+
"window. The hold ends at --resets-at, at that limit's own reset, or after a",
|
|
38
|
+
"short default, or sooner when a newer reading of that limit is below 100%.",
|
|
25
39
|
].join("\n");
|
|
26
40
|
export class CapacityUsageError extends Error {
|
|
27
41
|
exitCode = 2;
|
|
@@ -36,42 +50,217 @@ export function ingestRefusal(input, options) {
|
|
|
36
50
|
const resetArg = input.resetsAt === undefined ? undefined : parseReset(input.resetsAt);
|
|
37
51
|
const limitArg = optionalTokenFree(input.limitId, "--limit-id must be a non-empty id");
|
|
38
52
|
const view = loadView(options);
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
|
|
53
|
+
const accountId = accountArg ?? resolveRefusalAccount(view, provider, limitArg);
|
|
54
|
+
const limits = (view?.providers.find((entry) => entry.provider === provider)?.limits ?? [])
|
|
55
|
+
.filter((limit) => limit.accountId === accountId && !isRefusalHold(limit));
|
|
56
|
+
const named = limitArg ? limits.find((limit) => limit.limitId === limitArg) : undefined;
|
|
57
|
+
const window = input.window ?? (named ? windowOfLimit(named) : { kind: "usage" });
|
|
42
58
|
const now = options?.now ? options.now() : new Date();
|
|
43
59
|
const observedAt = now.toISOString();
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
limitId,
|
|
49
|
-
observedAt,
|
|
50
|
-
resetsAt,
|
|
51
|
-
matched,
|
|
52
|
-
});
|
|
53
|
-
const rebuilt = appendReading(reading, options);
|
|
60
|
+
const real = limits.find((limit) => limitMatchesWindow(limit, window));
|
|
61
|
+
const resetsAt = resolveRefusalReset(resetArg, real?.resetsAt, now, input.holdMs);
|
|
62
|
+
const reading = buildReading({ provider, accountId, window, observedAt, resetsAt, real, account: input.account });
|
|
63
|
+
const rebuilt = appendThenRebuild(reading, input.busyRetryMs, options);
|
|
54
64
|
const row = rebuilt.providers
|
|
55
65
|
.find((entry) => entry.provider === provider)
|
|
56
|
-
?.limits.find((limit) => limit.
|
|
57
|
-
if (!row)
|
|
58
|
-
throw new Error("refusal reading was not materialized");
|
|
66
|
+
?.limits.find((limit) => limit.readingId === reading.readingId);
|
|
59
67
|
return {
|
|
60
68
|
provider,
|
|
61
69
|
accountId,
|
|
62
|
-
limitId,
|
|
70
|
+
limitId: reading.limit.limitId,
|
|
63
71
|
source: "refusal",
|
|
64
72
|
confidence: "inferred",
|
|
65
73
|
observedAt,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
74
|
+
inForce: row !== undefined,
|
|
75
|
+
state: row?.state ?? null,
|
|
76
|
+
resetsAt: row?.resetsAt ?? resetsAt,
|
|
77
|
+
readingId: reading.readingId,
|
|
78
|
+
usedPercent: null,
|
|
70
79
|
};
|
|
71
80
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
/** Busy is only ever the rebuild: the event line is on disk by then, so a retry rebuilds and does not append again. */
|
|
82
|
+
function appendThenRebuild(reading, busyRetryMs, options) {
|
|
83
|
+
try {
|
|
84
|
+
return appendReading(reading, options);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (!(err instanceof LedgerBusy) || !busyRetryMs)
|
|
88
|
+
throw err;
|
|
89
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, busyRetryMs);
|
|
90
|
+
return rebuildNow(options);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function windowOfLimit(limit) {
|
|
94
|
+
if (limit.kind === "session")
|
|
95
|
+
return { kind: "session" };
|
|
96
|
+
if (limit.kind === "weekly_all")
|
|
97
|
+
return { kind: "weekly" };
|
|
98
|
+
if (limit.kind === "weekly_scoped" && limit.scope)
|
|
99
|
+
return { kind: "model", model: limit.scope };
|
|
100
|
+
return { kind: "usage" };
|
|
101
|
+
}
|
|
102
|
+
function limitMatchesWindow(limit, window) {
|
|
103
|
+
if (window.kind === "session")
|
|
104
|
+
return limit.kind === "session";
|
|
105
|
+
if (window.kind === "weekly")
|
|
106
|
+
return limit.kind === "weekly_all";
|
|
107
|
+
if (window.kind === "model") {
|
|
108
|
+
return limit.kind === "weekly_scoped" && (limit.scope ?? "").toLowerCase() === window.model.toLowerCase();
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const defaultRefusalLog = (level, message) => {
|
|
113
|
+
if (level === "info")
|
|
114
|
+
return;
|
|
115
|
+
process.stderr.write(`hq capacity: ${message}\n`);
|
|
116
|
+
};
|
|
117
|
+
const BUSY_RETRY_MS = 250;
|
|
118
|
+
const REPEAT_REFUSAL_WINDOW_MS = 24 * 60 * 60 * 1000;
|
|
119
|
+
/**
|
|
120
|
+
* Record a real refusal against the account on a provider home. For callers in
|
|
121
|
+
* the middle of other work, so it never throws. It writes nothing unless exactly
|
|
122
|
+
* one account in the ledger uses that home: a bot's own login, or a home the
|
|
123
|
+
* ledger has not read, is not some other account's refusal. A failed write is
|
|
124
|
+
* logged and returns null.
|
|
125
|
+
*/
|
|
126
|
+
export function recordRefusalForHome(input, env = process.env) {
|
|
127
|
+
// A test that did not point at a fixture ledger must not write the real one.
|
|
128
|
+
if (env.VITEST && !env.HQ_CAPACITY_ROOT?.trim())
|
|
129
|
+
return null;
|
|
130
|
+
const log = input.log ?? defaultRefusalLog;
|
|
131
|
+
try {
|
|
132
|
+
const provider = requireProvider(input.provider);
|
|
133
|
+
const options = capacityOptionsFromEnv(env);
|
|
134
|
+
let view;
|
|
135
|
+
try {
|
|
136
|
+
view = readNow(options);
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
if (!(err instanceof LedgerMissing))
|
|
140
|
+
throw err;
|
|
141
|
+
log("info", `refusal not recorded for ${provider}: no capacity ledger on this computer`);
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const wanted = realHome(input.home);
|
|
145
|
+
const accounts = (view.providers.find((entry) => entry.provider === provider)?.accounts ?? [])
|
|
146
|
+
.filter((account) => account.homes.some((home) => realHome(home) === wanted));
|
|
147
|
+
if (accounts.length !== 1) {
|
|
148
|
+
log("info", `refusal not recorded for ${provider}: ${accounts.length} ledger accounts use this login's home`);
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const account = accounts[0];
|
|
152
|
+
const now = options.now ? options.now() : new Date();
|
|
153
|
+
const parsed = input.resetsAt ? Date.parse(input.resetsAt) : Number.NaN;
|
|
154
|
+
const named = Number.isFinite(parsed) && parsed > now.getTime() ? parsed : null;
|
|
155
|
+
const until = input.window.kind === "usage"
|
|
156
|
+
? unnamedRefusalEnd({ provider, account, named, holdMs: input.holdMs, now, options })
|
|
157
|
+
: named;
|
|
158
|
+
return ingestRefusal({
|
|
159
|
+
provider,
|
|
160
|
+
accountId: account.accountId,
|
|
161
|
+
window: input.window,
|
|
162
|
+
...(until !== null ? { resetsAt: new Date(until).toISOString() } : {}),
|
|
163
|
+
...(input.holdMs !== undefined ? { holdMs: input.holdMs } : {}),
|
|
164
|
+
account: { labels: account.labels, home: input.home },
|
|
165
|
+
busyRetryMs: BUSY_RETRY_MS,
|
|
166
|
+
}, options);
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
if (err instanceof LedgerBusy) {
|
|
170
|
+
log("warn", `refusal for ${input.provider} saved, but capacity is behind until its next rebuild: the ledger stayed busy`);
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
log("error", `refusal not recorded for ${input.provider}: ${redactProviderTokens(err instanceof Error ? err.message : String(err))}`);
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Record a usage-limit refusal from the tool's own words, for the login its turn
|
|
179
|
+
* really ran on: the turn's home variable, or the provider's default home.
|
|
180
|
+
*/
|
|
181
|
+
/**
|
|
182
|
+
* One refusal that names no window may be about the wrong subscription (incident
|
|
183
|
+
* 2026-10-13, held the only account for three days), so it gets the short hold
|
|
184
|
+
* even when the tool quotes a far reset. The same rule as applyDefaultQuotaHold
|
|
185
|
+
* and recordAccountQuota: a repeat after that hold lapsed goes to the provider's
|
|
186
|
+
* reset. A second refusal while the hold is live is the same strike, and neither
|
|
187
|
+
* promotes the hold nor shortens it.
|
|
188
|
+
*/
|
|
189
|
+
function unnamedRefusalEnd(input) {
|
|
190
|
+
const { account, named, now } = input;
|
|
191
|
+
const nowMs = now.getTime();
|
|
192
|
+
const limitId = `${REFUSAL_LIMIT_PREFIX}${REFUSAL_SCOPE_USAGE}`;
|
|
193
|
+
const shortEnd = nowMs + (input.holdMs ?? REFUSAL_DEFAULT_TTL_MS);
|
|
194
|
+
const live = account.limits.find((limit) => limit.limitId === limitId && isRefusalHold(limit));
|
|
195
|
+
const liveEnd = live?.resetsAt ? Date.parse(live.resetsAt) : Number.NaN;
|
|
196
|
+
if (Number.isFinite(liveEnd) && liveEnd > nowMs) {
|
|
197
|
+
return Math.max(liveEnd, named !== null && named <= shortEnd ? named : shortEnd);
|
|
198
|
+
}
|
|
199
|
+
if (named === null)
|
|
200
|
+
return null;
|
|
201
|
+
if (named <= shortEnd)
|
|
202
|
+
return named;
|
|
203
|
+
const prior = latestRefusalOnFile({
|
|
204
|
+
provider: input.provider,
|
|
205
|
+
accountIds: [...new Set([account.accountId, ...account.limits.map((limit) => limit.accountId)])],
|
|
206
|
+
limitId,
|
|
207
|
+
}, input.options);
|
|
208
|
+
const priorEnd = prior?.resetsAt ? Date.parse(prior.resetsAt) : Number.NaN;
|
|
209
|
+
const lapsedLately = prior !== null
|
|
210
|
+
&& nowMs - Date.parse(prior.observedAt) <= REPEAT_REFUSAL_WINDOW_MS
|
|
211
|
+
&& Number.isFinite(priorEnd) && priorEnd <= nowMs;
|
|
212
|
+
return lapsedLately ? named : null;
|
|
213
|
+
}
|
|
214
|
+
/** The provider's configured hold, so a bot or probe holds as long as a lane does. */
|
|
215
|
+
function configuredHoldMs(provider, hqRoot, env, log) {
|
|
216
|
+
try {
|
|
217
|
+
const config = loadLanesConfig(hqRoot ?? resolveLanesHqRoot({ env })).providers[provider];
|
|
218
|
+
return config ? providerQuotaDefaultHoldMs(config) : undefined;
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
log("warn", `using the default refusal hold for ${provider}: ${redactProviderTokens(err instanceof Error ? err.message : String(err))}`);
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
export function recordUsageLimitRefusal(input, env = process.env) {
|
|
226
|
+
const log = input.log ?? defaultRefusalLog;
|
|
227
|
+
try {
|
|
228
|
+
if (!REFUSAL_PROVIDERS.includes(input.provider))
|
|
229
|
+
return null;
|
|
230
|
+
const provider = input.provider;
|
|
231
|
+
const home = input.turnEnv[PROVIDER_HOME_ENV[provider]]?.trim()
|
|
232
|
+
|| defaultProviderHome(provider, input.turnEnv.HOME?.trim() || os.homedir());
|
|
233
|
+
const now = capacityOptionsFromEnv(env).now?.() ?? new Date();
|
|
234
|
+
return recordRefusalForHome({
|
|
235
|
+
provider,
|
|
236
|
+
home,
|
|
237
|
+
window: usageLimitWindow(input.text),
|
|
238
|
+
resetsAt: parseProviderResetTime(input.text, now),
|
|
239
|
+
holdMs: configuredHoldMs(provider, input.hqRoot, env, log),
|
|
240
|
+
log,
|
|
241
|
+
}, env);
|
|
242
|
+
}
|
|
243
|
+
catch (err) {
|
|
244
|
+
log("error", `refusal not recorded for ${input.provider}: ${redactProviderTokens(err instanceof Error ? err.message : String(err))}`);
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function realHome(home) {
|
|
249
|
+
const resolved = path.resolve(home);
|
|
250
|
+
try {
|
|
251
|
+
return fs.realpathSync(resolved);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return resolved;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** Fifteen minutes, the same default a lane's own hold uses. */
|
|
258
|
+
const REFUSAL_DEFAULT_TTL_MS = 15 * 60 * 1000;
|
|
259
|
+
/**
|
|
260
|
+
* An explicit reset wins. Otherwise the hold ends at the reset of the real limit
|
|
261
|
+
* the refusal named, when that is ahead. With neither, it is short.
|
|
262
|
+
*/
|
|
263
|
+
function resolveRefusalReset(explicit, inherited, now, holdMs) {
|
|
75
264
|
if (explicit !== undefined)
|
|
76
265
|
return explicit;
|
|
77
266
|
if (inherited) {
|
|
@@ -79,7 +268,7 @@ function resolveRefusalReset(explicit, inherited, now) {
|
|
|
79
268
|
if (!Number.isNaN(at) && at > now.getTime())
|
|
80
269
|
return inherited;
|
|
81
270
|
}
|
|
82
|
-
return new Date(now.getTime() + REFUSAL_DEFAULT_TTL_MS).toISOString();
|
|
271
|
+
return new Date(now.getTime() + (holdMs ?? REFUSAL_DEFAULT_TTL_MS)).toISOString();
|
|
83
272
|
}
|
|
84
273
|
function requireProvider(value) {
|
|
85
274
|
const provider = value?.trim() ?? "";
|
|
@@ -134,44 +323,74 @@ function loadView(options) {
|
|
|
134
323
|
throw err;
|
|
135
324
|
}
|
|
136
325
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
326
|
+
/**
|
|
327
|
+
* The account a refusal lands on when `--account` is omitted. Only a
|
|
328
|
+
* `--limit-id` that exactly one account has names it. The provider having one
|
|
329
|
+
* account is not enough: the refusal may come from a login the ledger has never
|
|
330
|
+
* read, such as a bot's own. Limit ids like `weekly` are shared across accounts,
|
|
331
|
+
* so one that several accounts have is ambiguous. Every case that does not name
|
|
332
|
+
* exactly one account is a usage error, never a guess.
|
|
333
|
+
*/
|
|
334
|
+
function resolveRefusalAccount(view, provider, limitArg) {
|
|
335
|
+
const providerView = view?.providers.find((entry) => entry.provider === provider);
|
|
336
|
+
const accounts = providerView?.accounts ?? [];
|
|
337
|
+
if (accounts.length === 0) {
|
|
338
|
+
throw new CapacityUsageError(`no account for ${provider} on this computer: name the account that refused with --account`);
|
|
339
|
+
}
|
|
340
|
+
if (!limitArg) {
|
|
341
|
+
throw new CapacityUsageError(`--account is required: name the ${provider} account that refused`);
|
|
342
|
+
}
|
|
343
|
+
const owners = [...new Set((providerView?.limits ?? [])
|
|
344
|
+
.filter((limit) => limit.limitId === limitArg && !isRefusalHold(limit))
|
|
345
|
+
.map((limit) => limit.accountId))];
|
|
346
|
+
if (owners.length === 1)
|
|
347
|
+
return owners[0];
|
|
348
|
+
if (owners.length === 0) {
|
|
349
|
+
throw new CapacityUsageError(`no ${provider} account has the limit '${limitArg}': name the account that refused with --account`);
|
|
350
|
+
}
|
|
351
|
+
throw new CapacityUsageError(`${owners.length} ${provider} accounts have the limit '${limitArg}': name the one that refused with --account`);
|
|
352
|
+
}
|
|
353
|
+
function holdShape(window) {
|
|
354
|
+
switch (window.kind) {
|
|
355
|
+
case "session":
|
|
356
|
+
return { limitId: `${REFUSAL_LIMIT_PREFIX}session`, kind: "session", scope: null };
|
|
357
|
+
case "weekly":
|
|
358
|
+
return { limitId: `${REFUSAL_LIMIT_PREFIX}weekly`, kind: "weekly_all", scope: null };
|
|
359
|
+
case "model": {
|
|
360
|
+
const model = window.model.toLowerCase();
|
|
361
|
+
return { limitId: `${REFUSAL_LIMIT_PREFIX}model:${model}`, kind: "weekly_scoped", scope: model };
|
|
362
|
+
}
|
|
363
|
+
case "rate_limit":
|
|
364
|
+
return { limitId: `${REFUSAL_LIMIT_PREFIX}${REFUSAL_SCOPE_RATE_LIMIT}`, kind: "unknown", scope: REFUSAL_SCOPE_RATE_LIMIT };
|
|
365
|
+
default:
|
|
366
|
+
return { limitId: `${REFUSAL_LIMIT_PREFIX}${REFUSAL_SCOPE_USAGE}`, kind: "unknown", scope: REFUSAL_SCOPE_USAGE };
|
|
367
|
+
}
|
|
143
368
|
}
|
|
144
369
|
function buildReading(input) {
|
|
145
|
-
const
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
readingId: readingId(input.provider, input.accountId,
|
|
370
|
+
const shape = holdShape(input.window);
|
|
371
|
+
const labels = input.account?.labels ?? [];
|
|
372
|
+
return {
|
|
373
|
+
readingId: readingId(input.provider, input.accountId, shape.limitId, input.observedAt, input.resetsAt),
|
|
149
374
|
observedAt: input.observedAt,
|
|
150
375
|
provider: input.provider,
|
|
151
376
|
accountId: input.accountId,
|
|
152
377
|
source: "refusal",
|
|
153
|
-
cliVersion:
|
|
378
|
+
cliVersion: input.real?.cliVersion ?? "unknown",
|
|
154
379
|
confidence: "inferred",
|
|
155
380
|
parserVersion: REFUSAL_PARSER_VERSION,
|
|
381
|
+
...(labels[0] ? { accountLabel: labels[0], accountLabels: [...labels] } : {}),
|
|
382
|
+
...(input.account?.home ? { accountHome: input.account.home } : {}),
|
|
156
383
|
limit: {
|
|
157
|
-
limitId:
|
|
158
|
-
kind,
|
|
159
|
-
scope:
|
|
384
|
+
limitId: shape.limitId,
|
|
385
|
+
kind: shape.kind,
|
|
386
|
+
scope: shape.scope,
|
|
160
387
|
usedPercent: null,
|
|
161
|
-
windowMinutes:
|
|
388
|
+
windowMinutes: input.real?.windowMinutes ?? null,
|
|
162
389
|
resetsAt: input.resetsAt,
|
|
163
|
-
balance:
|
|
164
|
-
planType:
|
|
390
|
+
balance: null,
|
|
391
|
+
planType: input.real?.planType ?? null,
|
|
165
392
|
},
|
|
166
393
|
};
|
|
167
|
-
if (matched?.hasCredits !== undefined)
|
|
168
|
-
reading.limit.hasCredits = matched.hasCredits;
|
|
169
|
-
if (matched?.unlimited !== undefined)
|
|
170
|
-
reading.limit.unlimited = matched.unlimited;
|
|
171
|
-
if (matched?.allowancePercentOfWeekly !== undefined) {
|
|
172
|
-
reading.limit.allowancePercentOfWeekly = matched.allowancePercentOfWeekly;
|
|
173
|
-
}
|
|
174
|
-
return reading;
|
|
175
394
|
}
|
|
176
395
|
function readingId(provider, accountId, limitId, observedAt, resetsAt) {
|
|
177
396
|
const digest = createHash("sha256")
|
|
@@ -17,6 +17,7 @@ import { codexSnapshotStale, hasCodexCreditRoom } from "./codex-availability.js"
|
|
|
17
17
|
import { canonicalizeHome } from "../lanes/config.js";
|
|
18
18
|
import { LANE_PROVIDERS } from "../lanes/types.js";
|
|
19
19
|
import { capacityLoginCommand } from "./ledger.js";
|
|
20
|
+
import { isRateLimitHold, isRefusalHold, refusalEnded } from "./refusal-rows.js";
|
|
20
21
|
/** All lane providers, in the stable order boss-route uses for ranking ties. */
|
|
21
22
|
export const ROUTE_PROVIDERS = LANE_PROVIDERS;
|
|
22
23
|
/** Lane states that count against an account's live load. */
|
|
@@ -73,6 +74,9 @@ export function providerForModel(model) {
|
|
|
73
74
|
}
|
|
74
75
|
/** True when the limit should factor into the pick for this request. */
|
|
75
76
|
export function limitApplies(provider, limit, model) {
|
|
77
|
+
// A refusal that named no window stops the account for every model.
|
|
78
|
+
if (limit.kind === "unknown" && isRefusalHold(limit))
|
|
79
|
+
return true;
|
|
76
80
|
if (!LIMIT_KINDS.has(limit.kind))
|
|
77
81
|
return false;
|
|
78
82
|
if (limit.kind !== "weekly_scoped")
|
|
@@ -83,8 +87,25 @@ export function limitApplies(provider, limit, model) {
|
|
|
83
87
|
return false;
|
|
84
88
|
return model.toLowerCase().includes(String(limit.scope).toLowerCase());
|
|
85
89
|
}
|
|
90
|
+
function longestExhausted(rows) {
|
|
91
|
+
const endOf = (row) => {
|
|
92
|
+
const at = row.limit.resetsAt ? Date.parse(row.limit.resetsAt) : Number.NaN;
|
|
93
|
+
return Number.isFinite(at) ? at : Number.POSITIVE_INFINITY;
|
|
94
|
+
};
|
|
95
|
+
let longest;
|
|
96
|
+
for (const row of rows) {
|
|
97
|
+
// On a tie the real row names the limit.
|
|
98
|
+
if (!longest || endOf(row) > endOf(longest)
|
|
99
|
+
|| (endOf(row) === endOf(longest) && isRefusalHold(longest.limit) && !isRefusalHold(row.limit))) {
|
|
100
|
+
longest = row;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return longest;
|
|
104
|
+
}
|
|
86
105
|
/** Human label for the limit a reason or binding row quotes. */
|
|
87
106
|
export function labelLimit(limit) {
|
|
107
|
+
if (limit.kind === "unknown" && isRefusalHold(limit))
|
|
108
|
+
return isRateLimitHold(limit) ? "rate limit" : "usage limit";
|
|
88
109
|
if (limit.kind === "session")
|
|
89
110
|
return "session (5h)";
|
|
90
111
|
if (limit.kind === "weekly_all")
|
|
@@ -97,14 +118,18 @@ export function laneAccountFor(provider, homes, lanesConfig) {
|
|
|
97
118
|
if (!homes || homes.length === 0)
|
|
98
119
|
return null;
|
|
99
120
|
const wanted = new Set(homes.map((h) => safeCanonical(h)));
|
|
121
|
+
// A merged login can name several homes. Prefer one a lane can run on.
|
|
122
|
+
let usageOnly = null;
|
|
100
123
|
for (const [id, cfg] of Object.entries(accounts)) {
|
|
101
124
|
if (!cfg?.home)
|
|
102
125
|
continue;
|
|
103
|
-
if (wanted.has(safeCanonical(cfg.home)))
|
|
104
|
-
|
|
105
|
-
|
|
126
|
+
if (!wanted.has(safeCanonical(cfg.home)))
|
|
127
|
+
continue;
|
|
128
|
+
if (cfg.enabled !== false)
|
|
129
|
+
return { id, enabled: true };
|
|
130
|
+
usageOnly ??= { id, enabled: false };
|
|
106
131
|
}
|
|
107
|
-
return
|
|
132
|
+
return usageOnly;
|
|
108
133
|
}
|
|
109
134
|
function safeCanonical(home) {
|
|
110
135
|
try {
|
|
@@ -412,7 +437,9 @@ export function pickAccount(input) {
|
|
|
412
437
|
});
|
|
413
438
|
continue;
|
|
414
439
|
}
|
|
415
|
-
|
|
440
|
+
// A refusal whose time is up is gone: the real readings answer again.
|
|
441
|
+
const applicable = (account.limits ?? [])
|
|
442
|
+
.filter((l) => limitApplies(providerId, l, model) && !refusalEnded(l, nowMs));
|
|
416
443
|
const viewRows = applicable.map((limit) => {
|
|
417
444
|
const resetMs = limit.resetsAt ? Date.parse(limit.resetsAt) : NaN;
|
|
418
445
|
const resetPassed = limit.state === "reset-pending"
|
|
@@ -447,13 +474,19 @@ export function pickAccount(input) {
|
|
|
447
474
|
continue;
|
|
448
475
|
}
|
|
449
476
|
}
|
|
450
|
-
const
|
|
477
|
+
const exhaustedRows = viewRows.filter((row) => !row.resetPassed
|
|
451
478
|
&& row.readAge <= LIMIT_STALE_MS
|
|
452
479
|
&& !(providerId === "codex" && codexSnapshotStale(row.limit, account.limits ?? []))
|
|
453
480
|
&& (row.limit.state === "exhausted" || (typeof row.used === "number" && row.used >= 100)));
|
|
481
|
+
// The limit that lasts longest is the one that says when the account is back,
|
|
482
|
+
// so a short hold cannot hide a real used-up limit.
|
|
483
|
+
const exhausted = longestExhausted(exhaustedRows);
|
|
454
484
|
if (exhausted) {
|
|
455
|
-
|
|
456
|
-
|
|
485
|
+
// Credits and extra usage do not help with a 429: the next lane would go
|
|
486
|
+
// straight back to the account that just refused one.
|
|
487
|
+
const rateLimited = exhaustedRows.some((row) => isRateLimitHold(row.limit));
|
|
488
|
+
const codexCredits = !rateLimited && providerId === "codex" && hasCodexCreditRoom(account.limits ?? [], nowMs);
|
|
489
|
+
const extraRoom = !rateLimited && providerId === "claude" && hasExtraUsageRoom(account);
|
|
457
490
|
if (codexCredits || (extraRoom && input.allowExtraUsage === true)) {
|
|
458
491
|
const live = liveLaneCount(lanes, providerId, laneMatch.id);
|
|
459
492
|
const weekly = applicable.find((l) => l.kind === "weekly_all");
|
|
@@ -492,9 +525,11 @@ export function pickAccount(input) {
|
|
|
492
525
|
}
|
|
493
526
|
skipped.push({
|
|
494
527
|
...base,
|
|
495
|
-
reason:
|
|
496
|
-
?
|
|
497
|
-
:
|
|
528
|
+
reason: isRateLimitHold(exhausted.limit)
|
|
529
|
+
? "rate limited"
|
|
530
|
+
: extraRoom
|
|
531
|
+
? `${labelLimit(exhausted.limit)} used up; extra usage is on`
|
|
532
|
+
: `${labelLimit(exhausted.limit)} used up`,
|
|
498
533
|
...(extraRoom
|
|
499
534
|
? { fix: "pass --allow-extra-usage to run on paid extra usage" }
|
|
500
535
|
: {}),
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* or account ids. The clock and time zone are arguments so tests can pin both.
|
|
6
6
|
*/
|
|
7
7
|
import type { CapacityAccount } from "./accounts.js";
|
|
8
|
-
import { type NowView } from "./ledger.js";
|
|
8
|
+
import { type NowAccount, type NowView, type RateLimitResetCredit } from "./ledger.js";
|
|
9
9
|
export interface CapacityWidgetInput {
|
|
10
10
|
view?: NowView;
|
|
11
11
|
accounts?: readonly CapacityAccount[];
|
|
@@ -19,4 +19,15 @@ export interface CapacityWidgetInput {
|
|
|
19
19
|
}
|
|
20
20
|
export declare function capacityWidgetTimeZone(env?: NodeJS.ProcessEnv): string;
|
|
21
21
|
export declare function renderCapacityWidget(input: CapacityWidgetInput): string;
|
|
22
|
+
/** Banked resets still on the account, newest read. Expired grants are dropped. */
|
|
23
|
+
export declare function bankedResets(account: NowAccount, nowMs: number): RateLimitResetCredit[];
|
|
24
|
+
/** The model a weekly_scoped limit counts (Claude's "Fable" weekly), if the account has one. */
|
|
25
|
+
export declare function accountScopeName(account: NowAccount): string | null;
|
|
26
|
+
/**
|
|
27
|
+
* Plain name for what one grant resets, from the limits it clears. Claude's own
|
|
28
|
+
* labels run long ("New model launch: one Fable reset for Max, ..."), so the label
|
|
29
|
+
* is the last resort. `seven_day_overage_included` is the model-scoped weekly
|
|
30
|
+
* limit (the account's weekly_scoped row, e.g. Fable).
|
|
31
|
+
*/
|
|
32
|
+
export declare function resetGrantName(credit: RateLimitResetCredit, scopeName?: string | null): string;
|
|
22
33
|
//# sourceMappingURL=widget.d.ts.map
|