@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
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import * as fs from "node:fs";
|
|
12
12
|
import * as path from "node:path";
|
|
13
13
|
import { newestAccountError } from "./ledger.js";
|
|
14
|
+
import { holdsInForce } from "./refusal-rows.js";
|
|
14
15
|
const STATE_RANK = {
|
|
15
16
|
ok: 0,
|
|
16
17
|
"reset-pending": 1,
|
|
@@ -25,10 +26,118 @@ export function projectCapacityView(view, options) {
|
|
|
25
26
|
const identityKeyForAccount = options.identityKeyForAccount ?? (() => null);
|
|
26
27
|
return {
|
|
27
28
|
...view,
|
|
28
|
-
providers: view.providers.map((provider) => projectProvider(provider, options.accounts, identityKeyForAccount)),
|
|
29
|
+
providers: view.providers.map((provider) => projectProvider(provider, options.accounts, identityKeyForAccount, Date.parse(view.generatedAt))),
|
|
29
30
|
};
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Identity keys and organizations from the Claude profile cache, for the merge
|
|
34
|
+
* in the rebuild. Same file `claudeIdentityKeys` reads. ledger.ts cannot import
|
|
35
|
+
* the collector without an import cycle. A missing or unreadable file gives none.
|
|
36
|
+
*/
|
|
37
|
+
export function readStoredIdentities(root) {
|
|
38
|
+
const identities = new Map();
|
|
39
|
+
try {
|
|
40
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(root, "claude-org-cache.json"), "utf8"));
|
|
41
|
+
for (const [accountId, entry] of Object.entries(parsed.accounts ?? {})) {
|
|
42
|
+
identities.set(accountId, {
|
|
43
|
+
identityKey: typeof entry?.identityKey === "string" && entry.identityKey ? entry.identityKey : null,
|
|
44
|
+
organization: typeof entry?.organization === "string" && entry.organization ? entry.organization : null,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return identities;
|
|
50
|
+
}
|
|
51
|
+
return identities;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The merge that belongs in now.json: accounts on one home or one identity key
|
|
55
|
+
* become one account, with the newest reading per limit id. Unlike
|
|
56
|
+
* projectCapacityView it needs no registry and drops no account, so it gives
|
|
57
|
+
* the same view whichever process rebuilds. A provider with nothing to merge is
|
|
58
|
+
* returned as is.
|
|
59
|
+
*
|
|
60
|
+
* Routing reads this view, so two rules hold. An account whose sign-in is not
|
|
61
|
+
* live stays its own row, because a merged account can hand a lane any of its
|
|
62
|
+
* homes. And homes merge on a profile id only with the same known organization:
|
|
63
|
+
* one profile id can hold separate seats with separate limits, so an unknown
|
|
64
|
+
* organization neither merges nor bridges, like a missing identity key.
|
|
65
|
+
*/
|
|
66
|
+
export function mergeStoredView(view, options) {
|
|
67
|
+
return {
|
|
68
|
+
...view,
|
|
69
|
+
providers: view.providers.map((provider) => mergeStoredProvider(provider, options.identityFor, Date.parse(view.generatedAt))),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function signInIsLive(account) {
|
|
73
|
+
return account.authState === "ok" && !account.signedOut && account.usageStale !== true;
|
|
74
|
+
}
|
|
75
|
+
function mergeStoredProvider(provider, identityFor, nowMs) {
|
|
76
|
+
const accounts = provider.accounts ?? [];
|
|
77
|
+
if (accounts.length < 2)
|
|
78
|
+
return provider;
|
|
79
|
+
const parent = accounts.map((_, index) => index);
|
|
80
|
+
const find = (index) => {
|
|
81
|
+
let cursor = index;
|
|
82
|
+
while (parent[cursor] !== cursor)
|
|
83
|
+
cursor = parent[cursor] ?? cursor;
|
|
84
|
+
return cursor;
|
|
85
|
+
};
|
|
86
|
+
const seen = new Map();
|
|
87
|
+
accounts.forEach((account, index) => {
|
|
88
|
+
if (!signInIsLive(account))
|
|
89
|
+
return;
|
|
90
|
+
const keys = account.homes.map((home) => `home\0${canonicalHome(home)}`);
|
|
91
|
+
const stored = identityFor(account.accountId);
|
|
92
|
+
const organization = account.organization ?? stored?.organization ?? null;
|
|
93
|
+
if (stored?.identityKey && organization)
|
|
94
|
+
keys.push(`identity\0${stored.identityKey}\0${organization}`);
|
|
95
|
+
for (const key of keys) {
|
|
96
|
+
const previous = seen.get(key);
|
|
97
|
+
if (previous === undefined)
|
|
98
|
+
seen.set(key, index);
|
|
99
|
+
else
|
|
100
|
+
parent[find(index)] = find(previous);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
const groups = new Map();
|
|
104
|
+
accounts.forEach((account, index) => {
|
|
105
|
+
const root = find(index);
|
|
106
|
+
groups.set(root, [...(groups.get(root) ?? []), account]);
|
|
107
|
+
});
|
|
108
|
+
if (groups.size === accounts.length)
|
|
109
|
+
return provider;
|
|
110
|
+
const merged = [];
|
|
111
|
+
const dropped = new Set();
|
|
112
|
+
const kept = [];
|
|
113
|
+
for (const group of groups.values()) {
|
|
114
|
+
if (group.length === 1) {
|
|
115
|
+
merged.push(group[0]);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const homes = [...new Set(group.flatMap((account) => account.homes.map((home) => canonicalHome(home))))];
|
|
119
|
+
const labels = [...new Set(group.flatMap((account) => account.labels))]
|
|
120
|
+
.sort((a, b) => Number(b === "default") - Number(a === "default") || a.localeCompare(b));
|
|
121
|
+
const account = mergeGroup(group.map((item) => ({ account: item })), homes, labels, provider.provider, nowMs);
|
|
122
|
+
for (const item of group) {
|
|
123
|
+
for (const limit of item.limits)
|
|
124
|
+
dropped.add(limit);
|
|
125
|
+
}
|
|
126
|
+
kept.push(...account.limits);
|
|
127
|
+
merged.push(account);
|
|
128
|
+
}
|
|
129
|
+
merged.sort((a, b) => (a.labels[0] ?? "").localeCompare(b.labels[0] ?? ""));
|
|
130
|
+
const live = merged.filter((account) => account.authState !== "expired" && account.authState !== "signed_out" && !account.signedOut);
|
|
131
|
+
return {
|
|
132
|
+
...provider,
|
|
133
|
+
accounts: merged,
|
|
134
|
+
limits: [...provider.limits.filter((limit) => !dropped.has(limit)), ...kept].sort((a, b) => a.accountId.localeCompare(b.accountId) || a.limitId.localeCompare(b.limitId)),
|
|
135
|
+
state: provider.state === "unknown" || live.length === 0
|
|
136
|
+
? "unknown"
|
|
137
|
+
: live.reduce((state, account) => STATE_RANK[account.state] > STATE_RANK[state] ? account.state : state, "ok"),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function projectProvider(provider, accounts, identityKeyForAccount, nowMs) {
|
|
32
141
|
if (!provider.accounts || provider.accounts.length === 0)
|
|
33
142
|
return provider;
|
|
34
143
|
const mine = accounts.filter((account) => account.provider === provider.provider);
|
|
@@ -81,6 +190,14 @@ function projectProvider(provider, accounts, identityKeyForAccount) {
|
|
|
81
190
|
};
|
|
82
191
|
const seenHome = new Map();
|
|
83
192
|
const seenIdentity = new Map();
|
|
193
|
+
const organizationsByIdentity = new Map();
|
|
194
|
+
for (const piece of pieces) {
|
|
195
|
+
const identity = identityKeyForAccount(piece.account.accountId);
|
|
196
|
+
if (!identity || !piece.account.organization)
|
|
197
|
+
continue;
|
|
198
|
+
organizationsByIdentity.set(identity, (organizationsByIdentity.get(identity) ?? new Set()).add(piece.account.organization));
|
|
199
|
+
}
|
|
200
|
+
const splitIdentities = new Set([...organizationsByIdentity].filter(([, names]) => names.size > 1).map(([identity]) => identity));
|
|
84
201
|
pieces.forEach((piece, index) => {
|
|
85
202
|
for (const home of piece.homes) {
|
|
86
203
|
const previous = seenHome.get(home);
|
|
@@ -90,11 +207,15 @@ function projectProvider(provider, accounts, identityKeyForAccount) {
|
|
|
90
207
|
union(previous, index);
|
|
91
208
|
}
|
|
92
209
|
const identity = identityKeyForAccount(piece.account.accountId);
|
|
93
|
-
|
|
210
|
+
// Display only, and routing does not read this: the stored view's rule is
|
|
211
|
+
// stricter. An unknown organization still joins here, as it always has, so
|
|
212
|
+
// one profile's homes read as one row. Two known names never do.
|
|
213
|
+
if (!identity || splitIdentities.has(identity))
|
|
94
214
|
return;
|
|
95
|
-
const
|
|
215
|
+
const seat = identity;
|
|
216
|
+
const previousIdentity = seenIdentity.get(seat);
|
|
96
217
|
if (previousIdentity === undefined)
|
|
97
|
-
seenIdentity.set(
|
|
218
|
+
seenIdentity.set(seat, index);
|
|
98
219
|
else
|
|
99
220
|
union(previousIdentity, index);
|
|
100
221
|
});
|
|
@@ -117,7 +238,7 @@ function projectProvider(provider, accounts, identityKeyForAccount) {
|
|
|
117
238
|
const labels = displayLabels(homes, byHome);
|
|
118
239
|
if (labels.length === 0)
|
|
119
240
|
continue;
|
|
120
|
-
projected.push(mergeGroup(group, homes, labels, provider.provider));
|
|
241
|
+
projected.push(mergeGroup(group, homes, labels, provider.provider, nowMs));
|
|
121
242
|
}
|
|
122
243
|
if (projected.length === 0)
|
|
123
244
|
return withoutAccounts(provider);
|
|
@@ -188,13 +309,19 @@ function displayLabels(homes, byHome) {
|
|
|
188
309
|
const rest = shown.filter((label) => label !== "default").sort((a, b) => a.localeCompare(b));
|
|
189
310
|
return shown.includes("default") ? ["default", ...rest] : rest;
|
|
190
311
|
}
|
|
191
|
-
function mergeGroup(group, homes, labels, provider) {
|
|
312
|
+
function mergeGroup(group, homes, labels, provider, nowMs) {
|
|
192
313
|
const ok = group.filter((piece) => piece.account.authState === "ok" && piece.account.limits.length > 0);
|
|
193
|
-
const limits = mergeLimits((ok.length > 0 ? ok : group).map((piece) => piece.account));
|
|
194
314
|
const observedAt = latestObserved(group.map((piece) => piece.account));
|
|
195
315
|
const organization = latestOrganization(group.map((piece) => piece.account));
|
|
196
316
|
const accountId = (ok[0] ?? group[0])?.account.accountId ?? labels[0] ?? provider;
|
|
317
|
+
// Readers find a login's figures by comparing limit ids to the account id.
|
|
318
|
+
const limits = holdsInForce(mergeLimits((ok.length > 0 ? ok : group).map((piece) => piece.account))
|
|
319
|
+
.map((limit) => ({ ...limit, accountId })), nowMs);
|
|
197
320
|
const usageStale = group.some((piece) => piece.account.usageStale === true);
|
|
321
|
+
// Banked resets live on the account. Keep them through the merge: an ok piece's
|
|
322
|
+
// read wins, else any piece that has one.
|
|
323
|
+
const resetPiece = [...ok, ...group].find((piece) => piece.account.rateLimitResetCredits !== undefined);
|
|
324
|
+
const resets = resetPiece ? { rateLimitResetCredits: resetPiece.account.rateLimitResetCredits } : {};
|
|
198
325
|
if (ok.length > 0) {
|
|
199
326
|
let state = "ok";
|
|
200
327
|
for (const limit of limits) {
|
|
@@ -213,6 +340,7 @@ function mergeGroup(group, homes, labels, provider) {
|
|
|
213
340
|
reason: null,
|
|
214
341
|
fixCommand: null,
|
|
215
342
|
usageStale: usageStale ? true : undefined,
|
|
343
|
+
...resets,
|
|
216
344
|
limits,
|
|
217
345
|
};
|
|
218
346
|
}
|
|
@@ -232,6 +360,7 @@ function mergeGroup(group, homes, labels, provider) {
|
|
|
232
360
|
fixCommand: problem?.fixCommand ?? null,
|
|
233
361
|
usageStale: usageStale ? true : undefined,
|
|
234
362
|
lastError: newestAccountError(group.map((piece) => piece.account)) ?? undefined,
|
|
363
|
+
...resets,
|
|
235
364
|
limits,
|
|
236
365
|
};
|
|
237
366
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A refusal is a hold of its own, on its own limit row. It never replaces the
|
|
3
|
+
* reading of a real limit, so when it ends the account is back on its last real
|
|
4
|
+
* reading, with that reading's percent and reset.
|
|
5
|
+
*
|
|
6
|
+
* Holds carry `limitId` `refusal:<window>`. The window says only what the
|
|
7
|
+
* provider's refusal said: a session limit, the weekly limit, one model, a usage
|
|
8
|
+
* limit that named no window, or a short rate limit.
|
|
9
|
+
*/
|
|
10
|
+
import type { NowLimit } from "./ledger.js";
|
|
11
|
+
export declare const REFUSAL_LIMIT_PREFIX = "refusal:";
|
|
12
|
+
/** Scope of a hold whose window the refusal did not name. */
|
|
13
|
+
export declare const REFUSAL_SCOPE_USAGE = "usage";
|
|
14
|
+
/** Scope of a hold for a short rate limit, which says nothing about the plan. */
|
|
15
|
+
export declare const REFUSAL_SCOPE_RATE_LIMIT = "rate_limit";
|
|
16
|
+
export declare function isRefusalHold(limit: Pick<NowLimit, "limitId" | "source">): boolean;
|
|
17
|
+
export declare function isRateLimitHold(limit: Pick<NowLimit, "limitId" | "source" | "scope">): boolean;
|
|
18
|
+
/** The hold's time is up. A row with no reset never counts as ended here. */
|
|
19
|
+
export declare function refusalEnded(limit: Pick<NowLimit, "limitId" | "source" | "resetsAt">, nowMs: number): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Holds still in force, for rows that all belong to one login, under any of its
|
|
22
|
+
* ids: after a login change a hold can sit on the old id while the live id reads.
|
|
23
|
+
*
|
|
24
|
+
* A newer real reading below 100% of the limit the hold is about ends it. A
|
|
25
|
+
* reading stamped ahead of now is clock skew and proves nothing yet. A rate limit
|
|
26
|
+
* is not about the plan, so a usage reading cannot end it: only its short time does.
|
|
27
|
+
*/
|
|
28
|
+
export declare function holdsInForce(limits: readonly NowLimit[], nowMs: number): NowLimit[];
|
|
29
|
+
/** The same check before ids are clustered into logins: each id on its own. */
|
|
30
|
+
export declare function holdsInForceById(limits: readonly NowLimit[], nowMs: number): NowLimit[];
|
|
31
|
+
//# sourceMappingURL=refusal-rows.d.ts.map
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export const REFUSAL_LIMIT_PREFIX = "refusal:";
|
|
2
|
+
/** Scope of a hold whose window the refusal did not name. */
|
|
3
|
+
export const REFUSAL_SCOPE_USAGE = "usage";
|
|
4
|
+
/** Scope of a hold for a short rate limit, which says nothing about the plan. */
|
|
5
|
+
export const REFUSAL_SCOPE_RATE_LIMIT = "rate_limit";
|
|
6
|
+
export function isRefusalHold(limit) {
|
|
7
|
+
return limit.source === "refusal" && limit.limitId.startsWith(REFUSAL_LIMIT_PREFIX);
|
|
8
|
+
}
|
|
9
|
+
export function isRateLimitHold(limit) {
|
|
10
|
+
return isRefusalHold(limit) && limit.scope === REFUSAL_SCOPE_RATE_LIMIT;
|
|
11
|
+
}
|
|
12
|
+
/** The hold's time is up. A row with no reset never counts as ended here. */
|
|
13
|
+
export function refusalEnded(limit, nowMs) {
|
|
14
|
+
if (!isRefusalHold(limit) || !limit.resetsAt)
|
|
15
|
+
return false;
|
|
16
|
+
const at = Date.parse(limit.resetsAt);
|
|
17
|
+
return Number.isFinite(at) && at <= nowMs;
|
|
18
|
+
}
|
|
19
|
+
function readingAnswers(hold, reading) {
|
|
20
|
+
if (hold.kind === "session")
|
|
21
|
+
return reading.kind === "session";
|
|
22
|
+
if (hold.kind === "weekly_all")
|
|
23
|
+
return reading.kind === "weekly_all";
|
|
24
|
+
if (hold.kind === "weekly_scoped") {
|
|
25
|
+
return reading.kind === "weekly_scoped"
|
|
26
|
+
&& (reading.scope ?? "").toLowerCase() === (hold.scope ?? "").toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
return reading.kind === "session" || reading.kind === "weekly_all";
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Holds still in force, for rows that all belong to one login, under any of its
|
|
32
|
+
* ids: after a login change a hold can sit on the old id while the live id reads.
|
|
33
|
+
*
|
|
34
|
+
* A newer real reading below 100% of the limit the hold is about ends it. A
|
|
35
|
+
* reading stamped ahead of now is clock skew and proves nothing yet. A rate limit
|
|
36
|
+
* is not about the plan, so a usage reading cannot end it: only its short time does.
|
|
37
|
+
*/
|
|
38
|
+
export function holdsInForce(limits, nowMs) {
|
|
39
|
+
return limits.filter((hold) => {
|
|
40
|
+
if (!isRefusalHold(hold) || isRateLimitHold(hold))
|
|
41
|
+
return true;
|
|
42
|
+
const at = Date.parse(hold.observedAt);
|
|
43
|
+
return !limits.some((reading) => {
|
|
44
|
+
if (isRefusalHold(reading) || !readingAnswers(hold, reading))
|
|
45
|
+
return false;
|
|
46
|
+
if (typeof reading.usedPercent !== "number" || reading.usedPercent >= 100)
|
|
47
|
+
return false;
|
|
48
|
+
const readAt = Date.parse(reading.observedAt);
|
|
49
|
+
return readAt > at && !(readAt > nowMs);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** The same check before ids are clustered into logins: each id on its own. */
|
|
54
|
+
export function holdsInForceById(limits, nowMs) {
|
|
55
|
+
const byId = new Map();
|
|
56
|
+
for (const limit of limits)
|
|
57
|
+
byId.set(limit.accountId, [...(byId.get(limit.accountId) ?? []), limit]);
|
|
58
|
+
const kept = new Set([...byId.values()].flatMap((rows) => holdsInForce(rows, nowMs)));
|
|
59
|
+
return limits.filter((limit) => kept.has(limit));
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=refusal-rows.js.map
|
|
@@ -2,17 +2,41 @@ import { type LedgerOptions, type NowLimit } from "./ledger.js";
|
|
|
2
2
|
export declare const REFUSAL_PROVIDERS: readonly ["claude", "codex", "grok"];
|
|
3
3
|
export type RefusalProvider = (typeof REFUSAL_PROVIDERS)[number];
|
|
4
4
|
export declare const REFUSAL_PARSER_VERSION = "capacity-refusal.1";
|
|
5
|
-
export declare const REFUSAL_DEFAULT_LIMIT_ID = "weekly";
|
|
6
5
|
export declare const REFUSAL_HELP: string;
|
|
7
6
|
export declare class CapacityUsageError extends Error {
|
|
8
7
|
readonly exitCode: 2;
|
|
9
8
|
constructor(message: string);
|
|
10
9
|
}
|
|
10
|
+
/** What a refusal says it is about, and nothing more. */
|
|
11
|
+
export type RefusalWindow = {
|
|
12
|
+
kind: "session";
|
|
13
|
+
} | {
|
|
14
|
+
kind: "weekly";
|
|
15
|
+
} | {
|
|
16
|
+
kind: "model";
|
|
17
|
+
model: string;
|
|
18
|
+
}
|
|
19
|
+
/** A usage limit that named no window. It must not claim to be weekly. */
|
|
20
|
+
| {
|
|
21
|
+
kind: "usage";
|
|
22
|
+
} | {
|
|
23
|
+
kind: "rate_limit";
|
|
24
|
+
};
|
|
11
25
|
export interface IngestRefusalInput {
|
|
12
26
|
provider?: string;
|
|
13
27
|
limitId?: string;
|
|
14
28
|
resetsAt?: string;
|
|
15
29
|
accountId?: string;
|
|
30
|
+
window?: RefusalWindow;
|
|
31
|
+
/** How long a hold lasts when nothing says when it ends. */
|
|
32
|
+
holdMs?: number;
|
|
33
|
+
/** Copied onto the reading, so the account keeps its row with no other reading. */
|
|
34
|
+
account?: {
|
|
35
|
+
labels: readonly string[];
|
|
36
|
+
home: string | null;
|
|
37
|
+
};
|
|
38
|
+
/** Wait this long and rebuild once more when a rebuild in progress holds the lock. */
|
|
39
|
+
busyRetryMs?: number;
|
|
16
40
|
}
|
|
17
41
|
export interface RefusalRecord {
|
|
18
42
|
provider: RefusalProvider;
|
|
@@ -21,10 +45,37 @@ export interface RefusalRecord {
|
|
|
21
45
|
source: "refusal";
|
|
22
46
|
confidence: "inferred";
|
|
23
47
|
observedAt: string;
|
|
24
|
-
|
|
48
|
+
/** False when the hold ended as it was recorded: its reset is past, or a reading already answers it. */
|
|
49
|
+
inForce: boolean;
|
|
50
|
+
/** The hold's state, or null when it is not in force. */
|
|
51
|
+
state: NowLimit["state"] | null;
|
|
25
52
|
resetsAt: string | null;
|
|
26
53
|
readingId: string;
|
|
27
54
|
usedPercent: number | null;
|
|
28
55
|
}
|
|
29
56
|
export declare function ingestRefusal(input: IngestRefusalInput, options?: LedgerOptions): RefusalRecord;
|
|
57
|
+
export type RefusalLog = (level: "info" | "warn" | "error", message: string) => void;
|
|
58
|
+
/**
|
|
59
|
+
* Record a real refusal against the account on a provider home. For callers in
|
|
60
|
+
* the middle of other work, so it never throws. It writes nothing unless exactly
|
|
61
|
+
* one account in the ledger uses that home: a bot's own login, or a home the
|
|
62
|
+
* ledger has not read, is not some other account's refusal. A failed write is
|
|
63
|
+
* logged and returns null.
|
|
64
|
+
*/
|
|
65
|
+
export declare function recordRefusalForHome(input: {
|
|
66
|
+
provider: string;
|
|
67
|
+
home: string;
|
|
68
|
+
window: RefusalWindow;
|
|
69
|
+
/** Used only when it parses to a time ahead of now. */
|
|
70
|
+
resetsAt?: string | null;
|
|
71
|
+
holdMs?: number;
|
|
72
|
+
log?: RefusalLog;
|
|
73
|
+
}, env?: NodeJS.ProcessEnv): RefusalRecord | null;
|
|
74
|
+
export declare function recordUsageLimitRefusal(input: {
|
|
75
|
+
provider: string;
|
|
76
|
+
turnEnv: NodeJS.ProcessEnv;
|
|
77
|
+
text: string;
|
|
78
|
+
hqRoot?: string;
|
|
79
|
+
log?: RefusalLog;
|
|
80
|
+
}, env?: NodeJS.ProcessEnv): RefusalRecord | null;
|
|
30
81
|
//# sourceMappingURL=refusal.d.ts.map
|