@indigoai-us/hq-cli 5.345.215-beta.0 → 5.345.217
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 +206 -1
- package/README.md +45 -0
- package/assets/scaffold/core/scripts/hq-delegate-verify.sh +5 -2
- package/assets/scaffold/core/scripts/lib/epochrealtime.sh +30 -0
- package/assets/scaffold/core/scripts/lib/work-mesh-enqueue.sh +12 -5
- package/dist/command-catalog.generated.d.ts +105 -10
- package/dist/command-catalog.generated.js +127 -10
- package/dist/command-help.js +3 -1
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/auth.d.ts +1 -1
- package/dist/commands/auth.js +11 -52
- package/dist/commands/bot.js +75 -10
- package/dist/commands/capacity.d.ts +4 -1
- package/dist/commands/capacity.js +46 -10
- package/dist/commands/cloud-demote.d.ts +19 -4
- package/dist/commands/cloud-demote.js +85 -12
- package/dist/commands/cloud-provision.d.ts +5 -1
- package/dist/commands/cloud-provision.js +6 -2
- package/dist/commands/cloud.d.ts +36 -0
- package/dist/commands/cloud.js +110 -27
- package/dist/commands/core-delegate-w1b.js +15 -1
- package/dist/commands/core-policy.js +67 -8
- package/dist/commands/core.js +1 -0
- package/dist/commands/daemon.js +9 -1
- package/dist/commands/dm.js +7 -0
- package/dist/commands/doctor-usage.d.ts +8 -0
- package/dist/commands/doctor-usage.js +20 -0
- package/dist/commands/doctor.js +7 -1
- package/dist/commands/env.d.ts +18 -0
- package/dist/commands/env.js +82 -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/install-global.js +2 -2
- package/dist/commands/integrations-auth-rules.d.ts +27 -0
- package/dist/commands/integrations-auth-rules.js +79 -0
- package/dist/commands/integrations-import.js +124 -12
- package/dist/commands/lanes.js +102 -26
- package/dist/commands/logout.d.ts +7 -0
- package/dist/commands/logout.js +41 -34
- package/dist/commands/mesh.d.ts +2 -0
- package/dist/commands/mesh.js +60 -35
- package/dist/commands/sources.d.ts +8 -2
- package/dist/commands/sources.js +65 -9
- package/dist/commands/sync-held.js +5 -2
- package/dist/commands/whoami.js +26 -1
- package/dist/data/command-states.json +15 -0
- package/dist/index.js +52 -4
- package/dist/lazy-commands.js +5 -6
- package/dist/lib/anywhere-person-setting.d.ts +2 -0
- package/dist/lib/anywhere-person-setting.js +6 -3
- package/dist/lib/billing/usage-card.d.ts +25 -4
- package/dist/lib/billing/usage-card.js +76 -9
- package/dist/lib/billing/usage-cost.d.ts +3 -3
- package/dist/lib/billing/usage-cost.js +35 -13
- package/dist/lib/billing/usage-only.js +4 -2
- package/dist/lib/billing/usage-rate-card.d.ts +2 -2
- package/dist/lib/billing/usage-rate-card.js +3 -0
- package/dist/lib/bot/fleet-cron.d.ts +8 -0
- package/dist/lib/bot/fleet-cron.js +126 -7
- package/dist/lib/bot/fleet-daemon.d.ts +46 -0
- package/dist/lib/bot/fleet-daemon.js +99 -1
- package/dist/lib/bot/inflight.d.ts +9 -0
- package/dist/lib/bot/inflight.js +10 -0
- package/dist/lib/bot/jobs/hot-signal-sync.js +12 -1
- package/dist/lib/bot/probe.js +7 -1
- 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 +6 -1
- package/dist/lib/bot/runtime/grok-acp.js +32 -14
- package/dist/lib/bot/runtime/grok.js +1 -1
- 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-failure.d.ts +23 -0
- package/dist/lib/bot/runtime-failure.js +43 -1
- 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/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/flag-monitor.d.ts +8 -0
- package/dist/lib/daemon/flag-monitor.js +17 -6
- package/dist/lib/daemon/flag.d.ts +65 -1
- package/dist/lib/daemon/flag.js +190 -40
- package/dist/lib/daemon/ops/policy.js +62 -5
- package/dist/lib/daemon/paths.d.ts +72 -2
- package/dist/lib/daemon/paths.js +154 -3
- 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 +79 -21
- package/dist/lib/daemon/selfcheck.d.ts +22 -0
- package/dist/lib/daemon/selfcheck.js +40 -1
- package/dist/lib/daemon/server.d.ts +5 -1
- package/dist/lib/daemon/server.js +17 -23
- package/dist/lib/daemon/state-file-switch.d.ts +57 -0
- package/dist/lib/daemon/state-file-switch.js +88 -0
- package/dist/lib/daemon/state-file.d.ts +198 -0
- package/dist/lib/daemon/state-file.js +419 -0
- package/dist/lib/daemon/tcc-eperm.d.ts +153 -0
- package/dist/lib/daemon/tcc-eperm.js +310 -0
- package/dist/lib/daemon/timed-flag-monitors.d.ts +47 -0
- package/dist/lib/daemon/timed-flag-monitors.js +75 -0
- package/dist/lib/delivery/admission.d.ts +86 -0
- package/dist/lib/delivery/admission.js +148 -0
- package/dist/lib/delivery/artifacts.d.ts +7 -1
- package/dist/lib/delivery/artifacts.js +29 -10
- package/dist/lib/delivery/attempt-id.d.ts +13 -0
- package/dist/lib/delivery/attempt-id.js +16 -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 +21 -17
- package/dist/lib/delivery/enrollment.js +83 -5
- package/dist/lib/delivery/escalations.d.ts +51 -0
- package/dist/lib/delivery/escalations.js +60 -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 +120 -4
- package/dist/lib/delivery/migrations.js +215 -15
- package/dist/lib/delivery/mqtt.d.ts +2 -0
- package/dist/lib/delivery/mqtt.js +7 -0
- package/dist/lib/delivery/reconciler.d.ts +65 -1
- package/dist/lib/delivery/reconciler.js +224 -13
- 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 +2 -0
- package/dist/lib/delivery/store.js +4 -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.d.ts +16 -1
- package/dist/lib/delivery/workers/macos.js +54 -12
- 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 +32 -22
- package/dist/lib/delivery/workers/windows.js +50 -64
- package/dist/lib/doctor/checks/host-core.d.ts +50 -0
- package/dist/lib/doctor/checks/host-core.js +269 -0
- package/dist/lib/doctor/checks/replay-recorder.d.ts +39 -0
- package/dist/lib/doctor/checks/replay-recorder.js +261 -0
- package/dist/lib/doctor/registry.js +4 -0
- package/dist/lib/environment/profile.d.ts +239 -0
- package/dist/lib/environment/profile.js +251 -0
- package/dist/lib/hook-engine/reconcile.js +39 -13
- package/dist/lib/hook-engine/registration.d.ts +9 -0
- package/dist/lib/hook-engine/registration.js +19 -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/adapters/slack.d.ts +21 -5
- package/dist/lib/hot-channels/adapters/slack.js +64 -21
- package/dist/lib/hot-channels/alert.d.ts +2 -0
- package/dist/lib/hot-channels/alert.js +8 -1
- package/dist/lib/hot-channels/contract.d.ts +12 -0
- package/dist/lib/hot-channels/contract.js +7 -0
- package/dist/lib/hot-channels/loop.js +1 -0
- package/dist/lib/hot-channels/run.d.ts +6 -0
- package/dist/lib/hot-channels/run.js +4 -0
- package/dist/lib/hot-channels/signals-pass.d.ts +74 -1
- package/dist/lib/hot-channels/signals-pass.js +82 -41
- package/dist/lib/hot-channels/signals-redo.js +10 -2
- package/dist/lib/install/claude-targets.d.ts +12 -1
- package/dist/lib/install/claude-targets.js +57 -5
- package/dist/lib/install/codex-targets.d.ts +2 -2
- package/dist/lib/install/codex-targets.js +36 -6
- package/dist/lib/install/hook-target.d.ts +3 -0
- package/dist/lib/install/hook-target.js +11 -0
- package/dist/lib/lanes/canonical-host.d.ts +35 -0
- package/dist/lib/lanes/canonical-host.js +65 -0
- package/dist/lib/lanes/host-capacity.d.ts +30 -0
- package/dist/lib/lanes/host-capacity.js +29 -3
- package/dist/lib/lanes/watcher.js +35 -0
- package/dist/lib/mesh/live/daemon/run.d.ts +2 -0
- package/dist/lib/mesh/live/daemon/run.js +5 -2
- package/dist/lib/mesh/live/flush.d.ts +5 -1
- package/dist/lib/mesh/live/flush.js +12 -6
- package/dist/lib/mesh/live/reporting-privacy.d.ts +32 -0
- package/dist/lib/mesh/live/reporting-privacy.js +56 -1
- package/dist/lib/mesh/live/spool.d.ts +7 -0
- package/dist/lib/mesh/live/spool.js +25 -0
- package/dist/lib/person-setting-cache.d.ts +38 -0
- package/dist/lib/person-setting-cache.js +98 -0
- package/dist/lib/safe-store.d.ts +245 -0
- package/dist/lib/safe-store.js +777 -0
- package/dist/lib/signals/engine.d.ts +29 -2
- package/dist/lib/signals/engine.js +103 -4
- package/dist/lib/signals/reply.js +73 -18
- package/dist/lib/signals/steps/adapters.js +7 -2
- package/dist/lib/signals/steps/extract.d.ts +13 -0
- package/dist/lib/signals/steps/extract.js +122 -6
- package/dist/main.js +46 -3
- package/dist/schemas/hq-available-ai.v1.schema.json +76 -0
- package/dist/schemas/hq-environment-profile.v1.schema.json +438 -0
- package/dist/utils/auth-expiry.d.ts +8 -0
- package/dist/utils/auth-expiry.js +53 -0
- package/dist/utils/catalog-runtime-paths.d.ts +34 -0
- package/dist/utils/catalog-runtime-paths.js +53 -0
- package/dist/utils/cli-telemetry.d.ts +21 -0
- package/dist/utils/cli-telemetry.js +34 -0
- package/dist/utils/command-usage.d.ts +275 -0
- package/dist/utils/command-usage.js +797 -0
- package/dist/utils/files-browse-acl.js +1 -1
- package/dist/utils/hook-trust.d.ts +7 -0
- package/dist/utils/hook-trust.js +31 -4
- package/dist/utils/host-registration.d.ts +126 -0
- package/dist/utils/host-registration.js +378 -0
- package/dist/utils/resident-package-preload.js +2 -0
- package/package.json +10 -8
- package/scripts/bench-core.mjs +1096 -0
- package/scripts/check-function-manifest.mjs +702 -0
- package/dist/lib/delivery/workers/windows-boundary.d.ts +0 -453
- package/dist/lib/delivery/workers/windows-boundary.js +0 -693
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { codexSnapshotStale, hasCodexCreditRoom } from "./codex-availability.js";
|
|
1
|
+
import { canonicalCodexAccountId, codexSnapshotStale, hasCodexCreditRoom } from "./codex-availability.js";
|
|
2
|
+
import { AVAILABLE_FILE_NAME, availableForRebuild } from "./available.js";
|
|
3
|
+
import { mergeStoredView, readStoredIdentities } from "./project.js";
|
|
4
|
+
import { holdsInForce, holdsInForceById, refusalEnded } from "./refusal-rows.js";
|
|
2
5
|
/**
|
|
3
6
|
* Personal capacity ledger under ~/.hq/capacity/.
|
|
4
7
|
*
|
|
@@ -88,6 +91,11 @@ export class LedgerRejected extends Error {
|
|
|
88
91
|
this.name = "LedgerRejected";
|
|
89
92
|
}
|
|
90
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Set by a rebuild that writes one reading per login. A view without it came
|
|
96
|
+
* from an older hq process, which restores the two-id split.
|
|
97
|
+
*/
|
|
98
|
+
export const CAPACITY_VIEW_REVISION = 2;
|
|
91
99
|
const limitSchema = {
|
|
92
100
|
type: "object",
|
|
93
101
|
additionalProperties: false,
|
|
@@ -151,6 +159,7 @@ export const nowViewJsonSchema = {
|
|
|
151
159
|
machineId: { type: "string", minLength: 1 },
|
|
152
160
|
hqCliVersion: { type: "string", minLength: 1 },
|
|
153
161
|
generatedAt: { type: "string" },
|
|
162
|
+
viewRevision: { type: "number" },
|
|
154
163
|
providers: {
|
|
155
164
|
type: "array",
|
|
156
165
|
items: {
|
|
@@ -263,7 +272,8 @@ export function appendReadings(readings, failures = [], meta = null, options) {
|
|
|
263
272
|
const ctx = resolve(options);
|
|
264
273
|
const known = knownReadingIds(ctx.root);
|
|
265
274
|
const fresh = readings.filter((reading) => !known.has(reading.readingId));
|
|
266
|
-
|
|
275
|
+
const metas = meta === null ? [] : Array.isArray(meta) ? meta : [meta];
|
|
276
|
+
if (fresh.length === 0 && failures.length === 0 && metas.length === 0) {
|
|
267
277
|
try {
|
|
268
278
|
return readNow({ ...options, root: ctx.root });
|
|
269
279
|
}
|
|
@@ -277,8 +287,8 @@ export function appendReadings(readings, failures = [], meta = null, options) {
|
|
|
277
287
|
events.push(validateReading(reading));
|
|
278
288
|
for (const failure of failures)
|
|
279
289
|
events.push(validateParserFailure(failure));
|
|
280
|
-
|
|
281
|
-
events.push(validateProviderMeta(
|
|
290
|
+
for (const entry of metas)
|
|
291
|
+
events.push(validateProviderMeta(entry));
|
|
282
292
|
for (const event of events)
|
|
283
293
|
writeEventLine(event, { ...options, root: ctx.root });
|
|
284
294
|
rememberReadingIds(ctx.root, fresh.map((reading) => reading.readingId));
|
|
@@ -310,6 +320,24 @@ export function readNow(options) {
|
|
|
310
320
|
throw new LedgerCorrupt();
|
|
311
321
|
return parsed;
|
|
312
322
|
}
|
|
323
|
+
/**
|
|
324
|
+
* readNow for anything that routes. A view an older hq process wrote is rebuilt
|
|
325
|
+
* from the events first. With no events, or a busy lock, the view is returned as
|
|
326
|
+
* it stands.
|
|
327
|
+
*/
|
|
328
|
+
export function readCurrentNow(options) {
|
|
329
|
+
const view = readNow(options);
|
|
330
|
+
if (view.viewRevision === CAPACITY_VIEW_REVISION)
|
|
331
|
+
return view;
|
|
332
|
+
if (eventFileNames(resolve(options).root).length === 0)
|
|
333
|
+
return view;
|
|
334
|
+
try {
|
|
335
|
+
return rebuildNow(options);
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
return view;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
313
341
|
export function readLedgerEvents(options) {
|
|
314
342
|
const root = resolve(options).root;
|
|
315
343
|
if (!fs.existsSync(root))
|
|
@@ -352,8 +380,15 @@ export function rebuildNow(options) {
|
|
|
352
380
|
try {
|
|
353
381
|
deleteExpiredEvents(ctx.root, ctx.now, ctx.lockTtlSec * 1000);
|
|
354
382
|
const events = eventsForRebuild(ctx.root, ctx.nowMs);
|
|
355
|
-
const
|
|
383
|
+
const identities = readStoredIdentities(ctx.root);
|
|
384
|
+
const view = {
|
|
385
|
+
...mergeStoredView(materialize(canonicalCodexEvents(events), ctx), {
|
|
386
|
+
identityFor: (accountId) => identities.get(accountId) ?? null,
|
|
387
|
+
}),
|
|
388
|
+
viewRevision: CAPACITY_VIEW_REVISION,
|
|
389
|
+
};
|
|
356
390
|
writeNow(ctx.root, view, ctx.pid);
|
|
391
|
+
writeAvailableAfterRebuild(ctx, view, options);
|
|
357
392
|
return view;
|
|
358
393
|
}
|
|
359
394
|
finally {
|
|
@@ -623,6 +658,34 @@ function eventsForRebuild(dir, nowMs) {
|
|
|
623
658
|
atomicWriteText(dir, REBUILD_CHECKPOINT_FILE, `${JSON.stringify({ files, events: reduced })}\n`);
|
|
624
659
|
return reduced;
|
|
625
660
|
}
|
|
661
|
+
/**
|
|
662
|
+
* The newest refusal already on file for a limit, under any of a login's ids,
|
|
663
|
+
* from the last two event files. A hold that has ended is gone from now.json, so
|
|
664
|
+
* this is how a repeat refusal is told from a first one.
|
|
665
|
+
*/
|
|
666
|
+
export function latestRefusalOnFile(input, options) {
|
|
667
|
+
const ctx = resolve(options);
|
|
668
|
+
if (!fs.existsSync(ctx.root))
|
|
669
|
+
return null;
|
|
670
|
+
let latest = null;
|
|
671
|
+
for (const name of eventFileNames(ctx.root).slice(-2)) {
|
|
672
|
+
for (const event of readCompleteSuffix(path.join(ctx.root, name), 0).events) {
|
|
673
|
+
if ("event" in event)
|
|
674
|
+
continue;
|
|
675
|
+
const reading = event;
|
|
676
|
+
if (reading.source !== "refusal" || reading.provider !== input.provider)
|
|
677
|
+
continue;
|
|
678
|
+
if (reading.limit.limitId !== input.limitId || !input.accountIds.includes(reading.accountId))
|
|
679
|
+
continue;
|
|
680
|
+
const at = Date.parse(reading.observedAt);
|
|
681
|
+
if (!(at <= ctx.nowMs))
|
|
682
|
+
continue;
|
|
683
|
+
if (!latest || at >= Date.parse(latest.observedAt))
|
|
684
|
+
latest = reading;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return latest ? { observedAt: latest.observedAt, resetsAt: latest.limit.resetsAt ?? null } : null;
|
|
688
|
+
}
|
|
626
689
|
function eventFileNames(dir) {
|
|
627
690
|
if (!fs.existsSync(dir))
|
|
628
691
|
return [];
|
|
@@ -680,9 +743,12 @@ function reduceEvents(events, nowMs) {
|
|
|
680
743
|
if (Number.isNaN(at))
|
|
681
744
|
continue;
|
|
682
745
|
if (isProviderMeta(event)) {
|
|
683
|
-
|
|
746
|
+
// Keyed per account: Claude writes one provider_meta per account (banked
|
|
747
|
+
// resets differ by account), so a provider-only key kept just the last one.
|
|
748
|
+
const key = `${event.provider}\0${event.accountId ?? ""}`;
|
|
749
|
+
const prev = latestMeta.get(key);
|
|
684
750
|
if (!prev || at >= Date.parse(prev.observedAt))
|
|
685
|
-
latestMeta.set(
|
|
751
|
+
latestMeta.set(key, event);
|
|
686
752
|
continue;
|
|
687
753
|
}
|
|
688
754
|
if (isParserFailure(event)) {
|
|
@@ -799,6 +865,37 @@ function parseStoredLine(line) {
|
|
|
799
865
|
return null;
|
|
800
866
|
}
|
|
801
867
|
}
|
|
868
|
+
/**
|
|
869
|
+
* Move a Codex home's events to one account id for this rebuild, when that home
|
|
870
|
+
* holds more than one id. The event files stay as written, so nothing is deleted
|
|
871
|
+
* and a second rebuild gives the same view. An event with no home follows its
|
|
872
|
+
* id's home when that id was only ever seen on one home; otherwise it keeps its
|
|
873
|
+
* id. A home with one id already has one reading per limit and keeps that id.
|
|
874
|
+
*/
|
|
875
|
+
function canonicalCodexEvents(events) {
|
|
876
|
+
const homesById = new Map();
|
|
877
|
+
const idsByHome = new Map();
|
|
878
|
+
for (const event of events) {
|
|
879
|
+
if (event.provider !== "codex" || !event.accountId)
|
|
880
|
+
continue;
|
|
881
|
+
const raw = "accountHome" in event ? event.accountHome : undefined;
|
|
882
|
+
if (!raw)
|
|
883
|
+
continue;
|
|
884
|
+
const home = path.resolve(raw);
|
|
885
|
+
homesById.set(event.accountId, (homesById.get(event.accountId) ?? new Set()).add(home));
|
|
886
|
+
idsByHome.set(home, (idsByHome.get(home) ?? new Set()).add(event.accountId));
|
|
887
|
+
}
|
|
888
|
+
return events.map((event) => {
|
|
889
|
+
if (event.provider !== "codex" || !event.accountId)
|
|
890
|
+
return event;
|
|
891
|
+
const raw = "accountHome" in event ? event.accountHome : undefined;
|
|
892
|
+
const known = homesById.get(event.accountId);
|
|
893
|
+
const home = raw ? path.resolve(raw) : known?.size === 1 ? [...known][0] : undefined;
|
|
894
|
+
if (!home || (idsByHome.get(home)?.size ?? 0) < 2)
|
|
895
|
+
return event;
|
|
896
|
+
return { ...event, accountId: canonicalCodexAccountId(home) };
|
|
897
|
+
});
|
|
898
|
+
}
|
|
802
899
|
function materialize(events, ctx) {
|
|
803
900
|
const providers = new Map();
|
|
804
901
|
const bucketFor = (provider) => {
|
|
@@ -850,14 +947,15 @@ function materialize(events, ctx) {
|
|
|
850
947
|
const viewProviders = [...providers.entries()]
|
|
851
948
|
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
852
949
|
.map(([provider, bucket]) => {
|
|
853
|
-
const limits = [...bucket.limits.values()]
|
|
950
|
+
const limits = holdsInForceById([...bucket.limits.values()]
|
|
854
951
|
.sort((a, b) => {
|
|
855
952
|
const account = a.reading.accountId.localeCompare(b.reading.accountId);
|
|
856
953
|
if (account !== 0)
|
|
857
954
|
return account;
|
|
858
955
|
return a.reading.limit.limitId.localeCompare(b.reading.limit.limitId);
|
|
859
956
|
})
|
|
860
|
-
.map((limit) => toNowLimit(limit, ctx.nowMs))
|
|
957
|
+
.map((limit) => toNowLimit(limit, ctx.nowMs)), ctx.nowMs)
|
|
958
|
+
.filter((limit) => !refusalEnded(limit, ctx.nowMs));
|
|
861
959
|
if (provider === "codex") {
|
|
862
960
|
for (const limit of limits) {
|
|
863
961
|
if (codexSnapshotStale(limit, limits)) {
|
|
@@ -869,16 +967,7 @@ function materialize(events, ctx) {
|
|
|
869
967
|
}
|
|
870
968
|
}
|
|
871
969
|
const failed = bucket.failure !== null && bucket.failure.at >= bucket.latestReadingAt;
|
|
872
|
-
let state =
|
|
873
|
-
for (const limit of limits) {
|
|
874
|
-
if (provider === "codex" && codexSnapshotStale(limit, limits))
|
|
875
|
-
continue;
|
|
876
|
-
const availableState = provider === "codex" && limit.state === "exhausted"
|
|
877
|
-
&& hasCodexCreditRoom(limits.filter((row) => row.accountId === limit.accountId), ctx.nowMs)
|
|
878
|
-
? "ok" : limit.state;
|
|
879
|
-
if (STATE_RANK[availableState] > STATE_RANK[state])
|
|
880
|
-
state = availableState;
|
|
881
|
-
}
|
|
970
|
+
let state = providerLimitsState(provider, limits, ctx.nowMs);
|
|
882
971
|
if (failed)
|
|
883
972
|
state = "unknown";
|
|
884
973
|
// No mapped limit is never "ok". A labelled account's parse drift or HTTP
|
|
@@ -896,7 +985,7 @@ function materialize(events, ctx) {
|
|
|
896
985
|
entry.rateLimitResetCredits = bucket.meta.rateLimitResetCredits;
|
|
897
986
|
}
|
|
898
987
|
const unscopedFailure = bucket.failure !== null && bucket.failure.at >= bucket.latestReadingAt;
|
|
899
|
-
attachAccounts(entry, events, unscopedFailure);
|
|
988
|
+
attachAccounts(entry, events, unscopedFailure, ctx.nowMs);
|
|
900
989
|
// With no limits, show the newest account error (parse drift, HTTP error)
|
|
901
990
|
// that no later reading from that account has cleared.
|
|
902
991
|
if (limits.length === 0 && entry.lastError === null)
|
|
@@ -932,14 +1021,37 @@ function eventAnnotated(event) {
|
|
|
932
1021
|
const reading = event;
|
|
933
1022
|
return Boolean(reading.accountLabel) || Boolean(reading.accountLabels && reading.accountLabels.length > 0);
|
|
934
1023
|
}
|
|
935
|
-
function
|
|
1024
|
+
function providerLimitsState(provider, limits, nowMs) {
|
|
1025
|
+
let state = "ok";
|
|
1026
|
+
for (const limit of limits) {
|
|
1027
|
+
if (provider === "codex" && codexSnapshotStale(limit, limits))
|
|
1028
|
+
continue;
|
|
1029
|
+
const availableState = provider === "codex" && limit.state === "exhausted"
|
|
1030
|
+
&& hasCodexCreditRoom(limits.filter((row) => row.accountId === limit.accountId), nowMs)
|
|
1031
|
+
? "ok" : limit.state;
|
|
1032
|
+
if (STATE_RANK[availableState] > STATE_RANK[state])
|
|
1033
|
+
state = availableState;
|
|
1034
|
+
}
|
|
1035
|
+
return state;
|
|
1036
|
+
}
|
|
1037
|
+
function attachAccounts(provider, events, unscopedFailure, nowMs) {
|
|
936
1038
|
const relevant = events.filter((event) => event.provider === provider.provider && eventAnnotated(event));
|
|
937
1039
|
if (relevant.length === 0)
|
|
938
1040
|
return;
|
|
939
|
-
const accounts = clusterAccounts(provider, relevant);
|
|
1041
|
+
const accounts = clusterAccounts(provider, relevant, nowMs);
|
|
940
1042
|
if (accounts.length === 0)
|
|
941
1043
|
return;
|
|
1044
|
+
attachAccountResetCredits(accounts, events.filter((event) => event.provider === provider.provider));
|
|
942
1045
|
provider.accounts = accounts;
|
|
1046
|
+
// A hold another id of its login has already answered is gone from the provider too.
|
|
1047
|
+
const inForce = new Set(accounts.flatMap((account) => account.limits));
|
|
1048
|
+
const clusteredIds = new Set(accounts.flatMap((account) => account.limits.map((limit) => limit.accountId)));
|
|
1049
|
+
const remaining = provider.limits.filter((limit) => inForce.has(limit) || !clusteredIds.has(limit.accountId));
|
|
1050
|
+
if (remaining.length !== provider.limits.length) {
|
|
1051
|
+
provider.limits = remaining;
|
|
1052
|
+
if (!unscopedFailure)
|
|
1053
|
+
provider.state = providerLimitsState(provider.provider, remaining, nowMs);
|
|
1054
|
+
}
|
|
943
1055
|
if (unscopedFailure)
|
|
944
1056
|
return;
|
|
945
1057
|
const live = accounts.filter((account) => account.authState !== "expired" && account.authState !== "signed_out" && !account.signedOut);
|
|
@@ -963,7 +1075,32 @@ function attachAccounts(provider, events, unscopedFailure) {
|
|
|
963
1075
|
provider.lastError = null;
|
|
964
1076
|
}
|
|
965
1077
|
}
|
|
966
|
-
|
|
1078
|
+
/**
|
|
1079
|
+
* Banked resets belong to one account, not the provider: two Claude accounts can
|
|
1080
|
+
* hold different grants. The newest provider_meta for the account wins, so a read
|
|
1081
|
+
* that finds no grants (null) clears an earlier count.
|
|
1082
|
+
*/
|
|
1083
|
+
function attachAccountResetCredits(accounts, events) {
|
|
1084
|
+
const newest = new Map();
|
|
1085
|
+
for (const event of events) {
|
|
1086
|
+
if (!isProviderMeta(event) || !event.accountId)
|
|
1087
|
+
continue;
|
|
1088
|
+
const at = Date.parse(event.observedAt);
|
|
1089
|
+
if (Number.isNaN(at))
|
|
1090
|
+
continue;
|
|
1091
|
+
const seen = newest.get(event.accountId);
|
|
1092
|
+
if (!seen || at >= seen.at)
|
|
1093
|
+
newest.set(event.accountId, { at, credits: event.rateLimitResetCredits });
|
|
1094
|
+
}
|
|
1095
|
+
if (newest.size === 0)
|
|
1096
|
+
return;
|
|
1097
|
+
for (const account of accounts) {
|
|
1098
|
+
const found = newest.get(account.accountId);
|
|
1099
|
+
if (found)
|
|
1100
|
+
account.rateLimitResetCredits = found.credits;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
function clusterAccounts(provider, events, nowMs) {
|
|
967
1104
|
const seeds = [];
|
|
968
1105
|
for (const event of events) {
|
|
969
1106
|
const at = Date.parse(event.observedAt);
|
|
@@ -980,6 +1117,7 @@ function clusterAccounts(provider, events) {
|
|
|
980
1117
|
at,
|
|
981
1118
|
problem: problemFor(event.error),
|
|
982
1119
|
failure: problemFor(event.error) ? null : event.error,
|
|
1120
|
+
refusal: false,
|
|
983
1121
|
});
|
|
984
1122
|
continue;
|
|
985
1123
|
}
|
|
@@ -994,6 +1132,7 @@ function clusterAccounts(provider, events) {
|
|
|
994
1132
|
at,
|
|
995
1133
|
problem: null,
|
|
996
1134
|
failure: null,
|
|
1135
|
+
refusal: reading.source === "refusal",
|
|
997
1136
|
});
|
|
998
1137
|
}
|
|
999
1138
|
if (seeds.length === 0)
|
|
@@ -1046,6 +1185,7 @@ function clusterAccounts(provider, events) {
|
|
|
1046
1185
|
let latestAt = Number.NEGATIVE_INFINITY;
|
|
1047
1186
|
let latestDataAt = Number.NEGATIVE_INFINITY;
|
|
1048
1187
|
let failure = null;
|
|
1188
|
+
let latestRefusalAt = Number.NEGATIVE_INFINITY;
|
|
1049
1189
|
for (const seed of members) {
|
|
1050
1190
|
for (const label of seed.labels) {
|
|
1051
1191
|
if (!labels.includes(label))
|
|
@@ -1053,27 +1193,36 @@ function clusterAccounts(provider, events) {
|
|
|
1053
1193
|
}
|
|
1054
1194
|
if (seed.home && !homes.includes(seed.home))
|
|
1055
1195
|
homes.push(seed.home);
|
|
1056
|
-
|
|
1196
|
+
// Statusline readings carry no organization, so null says nothing about it.
|
|
1197
|
+
if (seed.organization && (!organization || seed.at >= organization.at)) {
|
|
1057
1198
|
organization = { value: seed.organization, at: seed.at };
|
|
1199
|
+
}
|
|
1058
1200
|
if (seed.problem && (!problem || seed.at >= problem.at)) {
|
|
1059
1201
|
problem = { ...seed.problem, at: seed.at };
|
|
1060
1202
|
}
|
|
1061
|
-
if (!seed.problem && seed.at > latestReadingAt)
|
|
1203
|
+
if (!seed.problem && !seed.refusal && seed.at > latestReadingAt)
|
|
1062
1204
|
latestReadingAt = seed.at;
|
|
1063
|
-
|
|
1205
|
+
// A refusal is not a reading: the figures on file are as old as they were.
|
|
1206
|
+
if (seed.refusal) {
|
|
1207
|
+
if (seed.at > latestRefusalAt)
|
|
1208
|
+
latestRefusalAt = seed.at;
|
|
1209
|
+
}
|
|
1210
|
+
else if (seed.at > latestAt)
|
|
1064
1211
|
latestAt = seed.at;
|
|
1065
1212
|
if (seed.failure !== null && (!failure || seed.at >= failure.at))
|
|
1066
1213
|
failure = { error: seed.failure, at: seed.at };
|
|
1067
|
-
if (seed.problem === null && seed.failure === null && seed.at > latestDataAt)
|
|
1214
|
+
if (seed.problem === null && seed.failure === null && !seed.refusal && seed.at > latestDataAt)
|
|
1068
1215
|
latestDataAt = seed.at;
|
|
1069
1216
|
}
|
|
1217
|
+
if (!Number.isFinite(latestAt))
|
|
1218
|
+
latestAt = latestRefusalAt;
|
|
1070
1219
|
// The account's newest parse or HTTP error, until a later reading clears it.
|
|
1071
1220
|
const accountError = failure && failure.at >= latestDataAt ? failure.error : null;
|
|
1072
1221
|
const ordered = orderLabels(labels);
|
|
1073
1222
|
const primary = ordered.find((label) => label !== "default") ?? ordered[0] ?? "default";
|
|
1074
1223
|
const limitedIds = ids.filter((id) => provider.limits.some((limit) => limit.accountId === id));
|
|
1075
1224
|
const accountId = limitedIds[0] ?? ids[0] ?? primary;
|
|
1076
|
-
const limits = provider.limits.filter((limit) => ids.includes(limit.accountId));
|
|
1225
|
+
const limits = holdsInForce(provider.limits.filter((limit) => ids.includes(limit.accountId)), nowMs);
|
|
1077
1226
|
const blocked = problem !== null && problem.at >= latestReadingAt;
|
|
1078
1227
|
const expired = blocked && problem?.kind === "expired";
|
|
1079
1228
|
const isSignedOut = blocked && problem?.kind === "signed_out";
|
|
@@ -1277,10 +1426,32 @@ function burnRate(history, nowMs) {
|
|
|
1277
1426
|
return delta / hours;
|
|
1278
1427
|
}
|
|
1279
1428
|
let nowWriteSerial = 0;
|
|
1429
|
+
/**
|
|
1430
|
+
* available.json follows now.json under the same lock. It is derived, so it
|
|
1431
|
+
* never fails the rebuild. A process that cannot read the account registry or
|
|
1432
|
+
* find the HQ root keeps the last file rather than writing a roster of only the
|
|
1433
|
+
* default homes, and logs one line with fixed text. A write that fails for any
|
|
1434
|
+
* other reason also keeps the last file and logs, redacted.
|
|
1435
|
+
*/
|
|
1436
|
+
function writeAvailableAfterRebuild(ctx, view, options = {}) {
|
|
1437
|
+
try {
|
|
1438
|
+
atomicViewWrite(ctx.root, AVAILABLE_FILE_NAME, availableForRebuild(view, ctx.root, ctx.now, options), ctx.pid);
|
|
1439
|
+
}
|
|
1440
|
+
catch (err) {
|
|
1441
|
+
// The cache is derived, so a write failure never fails the rebuild: the next
|
|
1442
|
+
// `hq capacity available` builds from now.json. Log it, redacted, so a
|
|
1443
|
+
// persistent failure is visible instead of silent.
|
|
1444
|
+
const reason = redactProviderTokens(err instanceof Error ? err.message : String(err));
|
|
1445
|
+
process.stderr.write(`hq capacity: available.json not updated: ${reason}\n`);
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1280
1448
|
function writeNow(dir, view, pid) {
|
|
1281
1449
|
const body = `${JSON.stringify(view, null, 2)}\n`;
|
|
1282
1450
|
assertNoProviderToken(body);
|
|
1283
|
-
|
|
1451
|
+
atomicViewWrite(dir, NOW_FILE_NAME, body, pid);
|
|
1452
|
+
}
|
|
1453
|
+
function atomicViewWrite(dir, name, body, pid) {
|
|
1454
|
+
const dest = path.join(dir, name);
|
|
1284
1455
|
// A fixed `.now.<pid>.tmp` disappeared before rename when rebuild ran in a
|
|
1285
1456
|
// tight loop in one process (ENOENT on rename). Each write gets its own name.
|
|
1286
1457
|
const tmp = path.join(dir, `.now.${pid}.${++nowWriteSerial}.${process.hrtime.bigint().toString(36)}.tmp`);
|
|
@@ -1380,6 +1551,9 @@ function validateProviderMeta(value) {
|
|
|
1380
1551
|
expiresAt: row.expiresAt,
|
|
1381
1552
|
title: row.title === null ? null : redactProviderTokens(row.title),
|
|
1382
1553
|
description: row.description === null ? null : redactProviderTokens(row.description),
|
|
1554
|
+
...(row.remaining !== undefined ? { remaining: row.remaining } : {}),
|
|
1555
|
+
...(row.usableNow !== undefined ? { usableNow: row.usableNow } : {}),
|
|
1556
|
+
...(row.clears !== undefined ? { clears: [...row.clears] } : {}),
|
|
1383
1557
|
})),
|
|
1384
1558
|
},
|
|
1385
1559
|
};
|
|
@@ -1487,7 +1661,10 @@ function isResetCredits(value) {
|
|
|
1487
1661
|
&& (row.grantedAt === null || finiteNumber(row.grantedAt))
|
|
1488
1662
|
&& (row.expiresAt === null || finiteNumber(row.expiresAt))
|
|
1489
1663
|
&& (row.title === null || typeof row.title === "string")
|
|
1490
|
-
&& (row.description === null || typeof row.description === "string")
|
|
1664
|
+
&& (row.description === null || typeof row.description === "string")
|
|
1665
|
+
&& (row.remaining === undefined || row.remaining === null || finiteNumber(row.remaining))
|
|
1666
|
+
&& (row.usableNow === undefined || row.usableNow === null || typeof row.usableNow === "boolean")
|
|
1667
|
+
&& (row.clears === undefined || (Array.isArray(row.clears) && row.clears.every((id) => typeof id === "string")));
|
|
1491
1668
|
});
|
|
1492
1669
|
}
|
|
1493
1670
|
function isParserFailure(value) {
|
|
@@ -9,4 +9,30 @@ export interface ProjectCapacityOptions {
|
|
|
9
9
|
identityKeyForAccount?: (accountId: string) => string | null;
|
|
10
10
|
}
|
|
11
11
|
export declare function projectCapacityView(view: NowView, options: ProjectCapacityOptions): NowView;
|
|
12
|
+
export interface StoredIdentity {
|
|
13
|
+
identityKey: string | null;
|
|
14
|
+
organization: string | null;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Identity keys and organizations from the Claude profile cache, for the merge
|
|
18
|
+
* in the rebuild. Same file `claudeIdentityKeys` reads. ledger.ts cannot import
|
|
19
|
+
* the collector without an import cycle. A missing or unreadable file gives none.
|
|
20
|
+
*/
|
|
21
|
+
export declare function readStoredIdentities(root: string): ReadonlyMap<string, StoredIdentity>;
|
|
22
|
+
/**
|
|
23
|
+
* The merge that belongs in now.json: accounts on one home or one identity key
|
|
24
|
+
* become one account, with the newest reading per limit id. Unlike
|
|
25
|
+
* projectCapacityView it needs no registry and drops no account, so it gives
|
|
26
|
+
* the same view whichever process rebuilds. A provider with nothing to merge is
|
|
27
|
+
* returned as is.
|
|
28
|
+
*
|
|
29
|
+
* Routing reads this view, so two rules hold. An account whose sign-in is not
|
|
30
|
+
* live stays its own row, because a merged account can hand a lane any of its
|
|
31
|
+
* homes. And homes merge on a profile id only with the same known organization:
|
|
32
|
+
* one profile id can hold separate seats with separate limits, so an unknown
|
|
33
|
+
* organization neither merges nor bridges, like a missing identity key.
|
|
34
|
+
*/
|
|
35
|
+
export declare function mergeStoredView(view: NowView, options: {
|
|
36
|
+
identityFor: (accountId: string) => StoredIdentity | null;
|
|
37
|
+
}): NowView;
|
|
12
38
|
//# sourceMappingURL=project.d.ts.map
|
|
@@ -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
|
}
|