@cortexkit/opencode-openai-auth 1.15.1 → 1.17.0
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/dist/bin.js +2 -2
- package/dist/index-0mhdyzax.js +76 -0
- package/dist/index-1m3vv9rh.js +2 -0
- package/dist/index-3dv16qvb.js +101 -0
- package/dist/{index-tes6vn59.js → index-6ts9w66n.js} +1 -1
- package/dist/{index-dzpjz8hv.js → index-99c2fepe.js} +2 -2
- package/dist/{index-f7kmsdfk.js → index-d7bsrp0m.js} +1 -1
- package/dist/{index-14x5shtc.js → index-dqxk628t.js} +2 -2
- package/dist/{index-hnfqppx9.js → index-fxqghceg.js} +1 -1
- package/dist/index-k2f9vrcr.js +1 -0
- package/dist/index-rm4zt5dv.js +64 -0
- package/dist/{index-1scvhkyy.js → index-v9p38mg5.js} +2 -2
- package/dist/{index-t65axpta.js → index-vmcmpezd.js} +1 -1
- package/dist/{index-m8m7rakq.js → index-xx2z6bs3.js} +2 -2
- package/dist/index-ydtpn6ma.js +8 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/rpc/port-file.js +1 -1
- package/dist/rpc/protocol.js +1 -1
- package/dist/rpc/rpc-client.js +1 -1
- package/dist/rpc/rpc-dir.js +1 -1
- package/dist/sidebar-state.js +1 -1
- package/dist/{token-j34j1zhm.js → token-r3vbz9pw.js} +1 -1
- package/dist/token-util-vks3kgse.js +1 -0
- package/dist/tui-preferences.js +1 -1
- package/dist/v2/server.d.ts +1 -1
- package/dist/v2/server.js +2 -2
- package/package.json +2 -2
- package/src/tui-compiled/raw/shared/0f77ebc3c914c9c6-custody.js +147 -2
- package/src/tui-compiled/raw/shared/352d228be2832bd4-hub.js +47 -1
- package/src/tui-compiled/raw/shared/e8cd4929507508d8-refresh-file-lock.js +13 -9
- package/src/tui-compiled/raw/shared/e8d3c8da776ef9cd-index.js +1 -1
- package/src/tui-compiled/runtime/shared/0f77ebc3c914c9c6-custody.js +147 -2
- package/src/tui-compiled/runtime/shared/352d228be2832bd4-hub.js +47 -1
- package/src/tui-compiled/runtime/shared/e8cd4929507508d8-refresh-file-lock.js +13 -9
- package/src/tui-compiled/runtime/shared/e8d3c8da776ef9cd-index.js +1 -1
- package/dist/index-1wcg7g9d.js +0 -2
- package/dist/index-50p400eg.js +0 -8
- package/dist/index-fkgd5yv6.js +0 -76
- package/dist/index-j2qfnmth.js +0 -64
- package/dist/index-xjtbg7sx.js +0 -101
- package/dist/token-util-bf619nmg.js +0 -1
|
@@ -65,6 +65,10 @@ export function decideScopedRetryAfter401(site, served, current, logger = defaul
|
|
|
65
65
|
export const SERVING_MARGIN_MS = 300_000;
|
|
66
66
|
/** A kept receipt needs a minute of headroom, not the vault's refresh demand. */
|
|
67
67
|
export const KEPT_RECEIPT_MARGIN_MS = 60_000;
|
|
68
|
+
/** Maximum request-path wait for a vault read without a usable kept receipt. */
|
|
69
|
+
export const VAULT_RECEIPT_WAIT_MS = 10_000;
|
|
70
|
+
/** Only the first minute of each observed outage earns a request-path wait. */
|
|
71
|
+
export const VAULT_YOUNG_OUTAGE_MS = 60_000;
|
|
68
72
|
function invalidMaterial() {
|
|
69
73
|
return new ClaustrumConsumerError('invalid-material', 'Claustrum returned invalid credential material');
|
|
70
74
|
}
|
|
@@ -198,7 +202,10 @@ export class ClaustrumScopedCustody {
|
|
|
198
202
|
#setTimeout;
|
|
199
203
|
#clearTimeout;
|
|
200
204
|
#outage = false;
|
|
205
|
+
#outageSince = 0;
|
|
206
|
+
#outageWaitSpent = false;
|
|
201
207
|
#outageError = new ClaustrumUnreachableError();
|
|
208
|
+
#receiptWaits = new Map();
|
|
202
209
|
#probeTimer;
|
|
203
210
|
#probing = false;
|
|
204
211
|
#lastProbeAt = -Infinity;
|
|
@@ -277,6 +284,8 @@ export class ClaustrumScopedCustody {
|
|
|
277
284
|
if (this.#closed || this.#outage)
|
|
278
285
|
return;
|
|
279
286
|
this.#outage = true;
|
|
287
|
+
this.#outageSince = this.#now();
|
|
288
|
+
this.#outageWaitSpent = false;
|
|
280
289
|
this.#outageError =
|
|
281
290
|
error instanceof ClaustrumCredentialError
|
|
282
291
|
? error
|
|
@@ -582,11 +591,13 @@ export class ClaustrumScopedCustody {
|
|
|
582
591
|
/** Ask the vault while reachable; retain one validated receipt in memory only. */
|
|
583
592
|
async authorize(identity, signal, routeId = 'vault') {
|
|
584
593
|
this.#check(signal);
|
|
594
|
+
const startedAt = this.#now();
|
|
585
595
|
const { credentialId, credentialType, accountIdentity } = identity;
|
|
586
596
|
let kept = this.#kept.get(credentialId);
|
|
587
597
|
if (kept &&
|
|
588
598
|
(kept.attempt.credentialType !== credentialType ||
|
|
589
|
-
kept.attempt.expectedAccountIdentity !== accountIdentity
|
|
599
|
+
kept.attempt.expectedAccountIdentity !== accountIdentity ||
|
|
600
|
+
!this.#usable(kept.attempt))) {
|
|
590
601
|
this.forget(credentialId);
|
|
591
602
|
kept = undefined;
|
|
592
603
|
}
|
|
@@ -605,7 +616,22 @@ export class ClaustrumScopedCustody {
|
|
|
605
616
|
return this.#serveKept(credentialId, kept, token, routeId);
|
|
606
617
|
}
|
|
607
618
|
try {
|
|
608
|
-
|
|
619
|
+
let attempt;
|
|
620
|
+
try {
|
|
621
|
+
// Once recovery is in flight, later arrivals join it rather than
|
|
622
|
+
// issuing another immediate read into the same known outage.
|
|
623
|
+
const recovering = this.#outage &&
|
|
624
|
+
this.#receiptWaits.has(this.#receiptWaitKey({ credentialId, credentialType, accountIdentity }, token));
|
|
625
|
+
if (recovering)
|
|
626
|
+
throw this.#outageError;
|
|
627
|
+
attempt = await this.#authorizeFresh({ credentialId, credentialType, accountIdentity }, token, signal);
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
this.#check(signal);
|
|
631
|
+
if (classifyVaultFailure(error) !== 'unreachable' || kept)
|
|
632
|
+
throw error;
|
|
633
|
+
attempt = await this.#waitForReceipt({ credentialId, credentialType, accountIdentity }, token, startedAt, error, signal);
|
|
634
|
+
}
|
|
609
635
|
this.#kept.set(credentialId, { attempt, routeId, since: null });
|
|
610
636
|
this.#usageServed.set(credentialId, { ...identity });
|
|
611
637
|
return attempt;
|
|
@@ -619,6 +645,122 @@ export class ClaustrumScopedCustody {
|
|
|
619
645
|
return this.#serveKept(credentialId, kept, token, routeId, error);
|
|
620
646
|
}
|
|
621
647
|
}
|
|
648
|
+
#receiptWaitKey(identity, token) {
|
|
649
|
+
// Never share authorization across a changed enrollment or account binding.
|
|
650
|
+
return JSON.stringify([
|
|
651
|
+
identity.credentialId,
|
|
652
|
+
identity.credentialType,
|
|
653
|
+
identity.accountIdentity,
|
|
654
|
+
token,
|
|
655
|
+
]);
|
|
656
|
+
}
|
|
657
|
+
async #waitForReceipt(identity, token, startedAt, error, signal) {
|
|
658
|
+
// Measure from the first unreachable observation, even at cold start.
|
|
659
|
+
// Otherwise a process that never connected would charge every request 10 s
|
|
660
|
+
// for an arbitrarily long outage. Any vault answer starts a new window.
|
|
661
|
+
// Once one wait has run out in this outage, the outage has outlasted the
|
|
662
|
+
// bound: later cold reads fail at once. Without that, a host that tries its
|
|
663
|
+
// accounts in turn would pay the full wait once per account.
|
|
664
|
+
const remaining = VAULT_RECEIPT_WAIT_MS - (this.#now() - startedAt);
|
|
665
|
+
if (remaining <= 0 ||
|
|
666
|
+
this.#outageWaitSpent ||
|
|
667
|
+
this.#now() - this.#outageSince >= VAULT_YOUNG_OUTAGE_MS)
|
|
668
|
+
throw error;
|
|
669
|
+
const key = this.#receiptWaitKey(identity, token);
|
|
670
|
+
let wait = this.#receiptWaits.get(key);
|
|
671
|
+
if (!wait) {
|
|
672
|
+
const controller = new AbortController();
|
|
673
|
+
const created = {
|
|
674
|
+
controller,
|
|
675
|
+
users: 0,
|
|
676
|
+
error,
|
|
677
|
+
pending: Promise.resolve().then(async () => {
|
|
678
|
+
let delay = 250;
|
|
679
|
+
for (;;) {
|
|
680
|
+
await this.#receiptBackoff(delay, controller.signal);
|
|
681
|
+
try {
|
|
682
|
+
return await this.#authorizeFresh(identity, token, controller.signal);
|
|
683
|
+
}
|
|
684
|
+
catch (failure) {
|
|
685
|
+
this.#check(controller.signal);
|
|
686
|
+
if (classifyVaultFailure(failure) !== 'unreachable')
|
|
687
|
+
throw failure;
|
|
688
|
+
created.error = failure;
|
|
689
|
+
delay = Math.min(delay * 2, 1_000);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}),
|
|
693
|
+
};
|
|
694
|
+
wait = created;
|
|
695
|
+
this.#receiptWaits.set(key, wait);
|
|
696
|
+
const remove = () => {
|
|
697
|
+
if (this.#receiptWaits.get(key) === created)
|
|
698
|
+
this.#receiptWaits.delete(key);
|
|
699
|
+
};
|
|
700
|
+
void wait.pending.then(remove, remove);
|
|
701
|
+
}
|
|
702
|
+
const shared = wait;
|
|
703
|
+
shared.users++;
|
|
704
|
+
let timer;
|
|
705
|
+
let abort;
|
|
706
|
+
try {
|
|
707
|
+
const attempt = await Promise.race([
|
|
708
|
+
shared.pending,
|
|
709
|
+
new Promise((_resolve, reject) => {
|
|
710
|
+
timer = this.#setTimeout(() => {
|
|
711
|
+
if (this.#outage)
|
|
712
|
+
this.#outageWaitSpent = true;
|
|
713
|
+
reject(shared.error);
|
|
714
|
+
}, remaining);
|
|
715
|
+
timer.unref?.();
|
|
716
|
+
if (signal) {
|
|
717
|
+
abort = () => reject(signal.reason);
|
|
718
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
719
|
+
if (signal.aborted)
|
|
720
|
+
abort();
|
|
721
|
+
}
|
|
722
|
+
}),
|
|
723
|
+
]);
|
|
724
|
+
this.#check(signal);
|
|
725
|
+
// Each physical send owns its receipt and later failure report, even
|
|
726
|
+
// when several requests shared the same recovery read.
|
|
727
|
+
const receipt = Object.freeze(Object.defineProperty({ ...attempt }, 'accessToken', {
|
|
728
|
+
value: attempt.accessToken,
|
|
729
|
+
enumerable: false,
|
|
730
|
+
}));
|
|
731
|
+
this.#provenance.set(receipt, { token });
|
|
732
|
+
return receipt;
|
|
733
|
+
}
|
|
734
|
+
finally {
|
|
735
|
+
if (timer)
|
|
736
|
+
this.#clearTimeout(timer);
|
|
737
|
+
if (abort)
|
|
738
|
+
signal?.removeEventListener('abort', abort);
|
|
739
|
+
if (--shared.users === 0) {
|
|
740
|
+
shared.controller.abort();
|
|
741
|
+
if (this.#receiptWaits.get(key) === shared)
|
|
742
|
+
this.#receiptWaits.delete(key);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
#receiptBackoff(delay, signal) {
|
|
747
|
+
this.#check(signal);
|
|
748
|
+
return new Promise((resolve, reject) => {
|
|
749
|
+
const abort = () => {
|
|
750
|
+
this.#clearTimeout(timer);
|
|
751
|
+
signal.removeEventListener('abort', abort);
|
|
752
|
+
reject(signal.reason);
|
|
753
|
+
};
|
|
754
|
+
const timer = this.#setTimeout(() => {
|
|
755
|
+
signal.removeEventListener('abort', abort);
|
|
756
|
+
resolve();
|
|
757
|
+
}, delay);
|
|
758
|
+
timer.unref?.();
|
|
759
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
760
|
+
if (signal.aborted)
|
|
761
|
+
abort();
|
|
762
|
+
});
|
|
763
|
+
}
|
|
622
764
|
#serveKept(credentialId, kept, token, routeId, error = this.#outageError) {
|
|
623
765
|
// A concurrent refusal or 401 may already have discarded this entry.
|
|
624
766
|
if (!kept ||
|
|
@@ -876,6 +1018,9 @@ export class ClaustrumScopedCustody {
|
|
|
876
1018
|
if (this.#closed)
|
|
877
1019
|
return;
|
|
878
1020
|
this.#closed = true;
|
|
1021
|
+
for (const wait of this.#receiptWaits.values())
|
|
1022
|
+
wait.controller.abort();
|
|
1023
|
+
this.#receiptWaits.clear();
|
|
879
1024
|
if (this.#probeTimer)
|
|
880
1025
|
this.#clearTimeout(this.#probeTimer);
|
|
881
1026
|
this.#probeTimer = undefined;
|
|
@@ -159,6 +159,13 @@ export class ClaustrumDiscoveryHub {
|
|
|
159
159
|
#own;
|
|
160
160
|
#watch;
|
|
161
161
|
#run;
|
|
162
|
+
/**
|
|
163
|
+
* Member commit fan-outs still running, each until every one of its
|
|
164
|
+
* commits has settled. Only a run whose generation ended can leave one
|
|
165
|
+
* behind while another run starts; `#discover` waits for them before it
|
|
166
|
+
* reads the files it lists over.
|
|
167
|
+
*/
|
|
168
|
+
#fanOuts = new Set();
|
|
162
169
|
/** The hub's last authority read; undefined before the first one. */
|
|
163
170
|
#active;
|
|
164
171
|
/** Counts inactive transitions, so local work started before one stops. */
|
|
@@ -665,6 +672,15 @@ export class ClaustrumDiscoveryHub {
|
|
|
665
672
|
// Folders that could not be made owner-only fail every member's commit,
|
|
666
673
|
// as an unreadable base fails its member's: each is marked pending.
|
|
667
674
|
const folders = await this.#ensureRosterDirectories().then(() => undefined, (error) => error instanceof Error ? error : new Error(String(error)));
|
|
675
|
+
// A generation that ended may still have member commits in flight. A
|
|
676
|
+
// commit that passed its last generation check just before vault mode
|
|
677
|
+
// went off still renames its file afterwards, so its older listing can
|
|
678
|
+
// land after this run read that file. The base-view rule below would
|
|
679
|
+
// then take it for another process's newer commit and keep it over this
|
|
680
|
+
// list. The files are read only once every such commit has settled.
|
|
681
|
+
await this.#settleFanOuts();
|
|
682
|
+
if (!this.#live(generation))
|
|
683
|
+
return undefined;
|
|
668
684
|
// What each member's file held before the list. Another process may
|
|
669
685
|
// commit while this list runs; the base-view rule then keeps its newer
|
|
670
686
|
// roster rather than replacing it with this older listing. Each file is
|
|
@@ -696,9 +712,17 @@ export class ClaustrumDiscoveryHub {
|
|
|
696
712
|
// does), so no commit ever holds two members' locks and no order between
|
|
697
713
|
// members is needed. The results are then published in member order.
|
|
698
714
|
const members = this.#open();
|
|
699
|
-
const
|
|
715
|
+
const fanOut = Promise.all(members.map((entry) => entry.hooks && !entry.closed
|
|
700
716
|
? this.#commit(entry, entry.hooks, inventory, folders ?? bases.get(entry), generation)
|
|
701
717
|
: undefined));
|
|
718
|
+
this.#fanOuts.add(fanOut);
|
|
719
|
+
let commits;
|
|
720
|
+
try {
|
|
721
|
+
commits = await fanOut;
|
|
722
|
+
}
|
|
723
|
+
finally {
|
|
724
|
+
this.#fanOuts.delete(fanOut);
|
|
725
|
+
}
|
|
702
726
|
if (!this.#live(generation))
|
|
703
727
|
return undefined;
|
|
704
728
|
const errors = new Map();
|
|
@@ -813,6 +837,28 @@ export class ClaustrumDiscoveryHub {
|
|
|
813
837
|
return { error };
|
|
814
838
|
}
|
|
815
839
|
}
|
|
840
|
+
/**
|
|
841
|
+
* Wait until no member commit fan-out of an earlier run is still running.
|
|
842
|
+
* Each of them belongs to a generation that ended, so its commits fail or
|
|
843
|
+
* finish at their next generation check and this wait stays short.
|
|
844
|
+
*/
|
|
845
|
+
async #settleFanOuts() {
|
|
846
|
+
if (!this.#fanOuts.size)
|
|
847
|
+
return;
|
|
848
|
+
const phaseDiagnostics = channel('common-auth.claustrum.phases');
|
|
849
|
+
const phase = (name) => {
|
|
850
|
+
if (phaseDiagnostics.hasSubscribers)
|
|
851
|
+
phaseDiagnostics.publish({ logger: this.#logger, phase: name });
|
|
852
|
+
};
|
|
853
|
+
phase('earlier fan-out drain begin');
|
|
854
|
+
try {
|
|
855
|
+
while (this.#fanOuts.size)
|
|
856
|
+
await Promise.allSettled([...this.#fanOuts]);
|
|
857
|
+
}
|
|
858
|
+
finally {
|
|
859
|
+
phase('earlier fan-out drain end');
|
|
860
|
+
}
|
|
861
|
+
}
|
|
816
862
|
/**
|
|
817
863
|
* Create the roster folders under the state folder (`rosters`, this host
|
|
818
864
|
* generation and this key) owner-only, as every other Claustrum private
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { link, lstat, mkdir, open, readdir, readFile, rename, rm, stat, unlink, } from "node:fs/promises";
|
|
3
3
|
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { createLogger } from "./cc9e0702a752cea7-index.js";
|
|
4
5
|
import { DisappearedLockRecordError, isWindowsAccessDeniedCreate, isWindowsAccessDeniedRead, readLockRecord, } from "./723b00f96290a097-lock-record.js";
|
|
5
6
|
import { createPrivateBytes, createPrivateDirectoryExclusive, ensurePrivateDirectory, isWindowsSharingViolation, PrivateFileError, privateOperation, readPrivateText, verifyPrivate, windowsPrivateFiles, writePrivateAtomic, } from "./375d9cca2def037d-private-files.js";
|
|
6
7
|
import { LockOwnershipError, lockPathFor } from "./4825e03209d6ae3e-with-lock.js";
|
|
@@ -1191,17 +1192,17 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
1191
1192
|
removalConfirmed = true;
|
|
1192
1193
|
}, deadline);
|
|
1193
1194
|
changingRecordError = undefined;
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
});
|
|
1199
|
-
releaseConfirmed = removalConfirmed;
|
|
1195
|
+
// Acquiring the marker does not confirm removal: stale-marker
|
|
1196
|
+
// recovery can take it away after the lease owner was checked.
|
|
1197
|
+
if (markerAcquired && removalConfirmed) {
|
|
1198
|
+
releaseConfirmed = true;
|
|
1200
1199
|
return;
|
|
1201
1200
|
}
|
|
1202
|
-
if (
|
|
1203
|
-
|
|
1204
|
-
|
|
1201
|
+
if (!markerAcquired) {
|
|
1202
|
+
if (signal?.aborted)
|
|
1203
|
+
return;
|
|
1204
|
+
await recoverStaleEvictionMarker(deadline);
|
|
1205
|
+
}
|
|
1205
1206
|
}
|
|
1206
1207
|
catch (error) {
|
|
1207
1208
|
const changingRecord = windowsPrivateFiles &&
|
|
@@ -1242,6 +1243,9 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
1242
1243
|
new PrivateFileError(lockPath, 'release-not-confirmed', {
|
|
1243
1244
|
code: 'ETIMEDOUT',
|
|
1244
1245
|
}));
|
|
1246
|
+
createLogger('fs').warn('File lock release deadline expired', {
|
|
1247
|
+
lockPath,
|
|
1248
|
+
});
|
|
1245
1249
|
},
|
|
1246
1250
|
};
|
|
1247
1251
|
}
|
|
@@ -9,7 +9,7 @@ import { insulaUsageTransport } from "./d0f0b6be200311af-insula.js";
|
|
|
9
9
|
export { ClaustrumCredentialError, } from "@cortexkit/claustrum-client";
|
|
10
10
|
export { ClaustrumUnreachableError, classifyVaultFailure, VAULT_FAILURE_CLASSES, } from "./94f53cfd572ba4bb-availability.js";
|
|
11
11
|
export { ClaustrumConsumer, DEFAULT_CONNECT_TIMEOUT_MS, } from "./169b2b4bf30909bc-consumer.js";
|
|
12
|
-
export { ClaustrumScopedCustody, decideScopedRetryAfter401, isScopedCredentialRotation, KEPT_RECEIPT_MARGIN_MS, SERVING_MARGIN_MS, } from "./0f77ebc3c914c9c6-custody.js";
|
|
12
|
+
export { ClaustrumScopedCustody, decideScopedRetryAfter401, isScopedCredentialRotation, KEPT_RECEIPT_MARGIN_MS, SERVING_MARGIN_MS, VAULT_RECEIPT_WAIT_MS, VAULT_YOUNG_OUTAGE_MS, } from "./0f77ebc3c914c9c6-custody.js";
|
|
13
13
|
export { ClaustrumEnrollmentManager, classifyEnrollmentError, connectClaustrumEnrollmentClient, disconnectClaustrumEnrollment, enrollmentAuthority, enrollmentName, getClaustrumEnrollmentPaths, hostEnrollmentPaths, RETRYABLE_ENROLLMENT_CODES, readClaustrumEnrollmentStatus, readClaustrumEnrollmentToken, resetClaustrumEnrollmentState, TERMINAL_ENROLLMENT_CODES, validEnrollmentDisplayName, validEnrollmentRequestedGrants, vaultAuthority, } from "./b9588ad4725c1d10-enrollment.js";
|
|
14
14
|
export { ClaustrumConsumerError, } from "./ca0fccaba1d87af4-errors.js";
|
|
15
15
|
export { connectVaultHost, hasVaultHost, VAULT_HOST_CONNECT_REFUSAL, VAULT_HOST_STATUS, } from "./c97089ed4243981c-host.js";
|
|
@@ -65,6 +65,10 @@ export function decideScopedRetryAfter401(site, served, current, logger = defaul
|
|
|
65
65
|
export const SERVING_MARGIN_MS = 300_000;
|
|
66
66
|
/** A kept receipt needs a minute of headroom, not the vault's refresh demand. */
|
|
67
67
|
export const KEPT_RECEIPT_MARGIN_MS = 60_000;
|
|
68
|
+
/** Maximum request-path wait for a vault read without a usable kept receipt. */
|
|
69
|
+
export const VAULT_RECEIPT_WAIT_MS = 10_000;
|
|
70
|
+
/** Only the first minute of each observed outage earns a request-path wait. */
|
|
71
|
+
export const VAULT_YOUNG_OUTAGE_MS = 60_000;
|
|
68
72
|
function invalidMaterial() {
|
|
69
73
|
return new ClaustrumConsumerError('invalid-material', 'Claustrum returned invalid credential material');
|
|
70
74
|
}
|
|
@@ -198,7 +202,10 @@ export class ClaustrumScopedCustody {
|
|
|
198
202
|
#setTimeout;
|
|
199
203
|
#clearTimeout;
|
|
200
204
|
#outage = false;
|
|
205
|
+
#outageSince = 0;
|
|
206
|
+
#outageWaitSpent = false;
|
|
201
207
|
#outageError = new ClaustrumUnreachableError();
|
|
208
|
+
#receiptWaits = new Map();
|
|
202
209
|
#probeTimer;
|
|
203
210
|
#probing = false;
|
|
204
211
|
#lastProbeAt = -Infinity;
|
|
@@ -277,6 +284,8 @@ export class ClaustrumScopedCustody {
|
|
|
277
284
|
if (this.#closed || this.#outage)
|
|
278
285
|
return;
|
|
279
286
|
this.#outage = true;
|
|
287
|
+
this.#outageSince = this.#now();
|
|
288
|
+
this.#outageWaitSpent = false;
|
|
280
289
|
this.#outageError =
|
|
281
290
|
error instanceof ClaustrumCredentialError
|
|
282
291
|
? error
|
|
@@ -582,11 +591,13 @@ export class ClaustrumScopedCustody {
|
|
|
582
591
|
/** Ask the vault while reachable; retain one validated receipt in memory only. */
|
|
583
592
|
async authorize(identity, signal, routeId = 'vault') {
|
|
584
593
|
this.#check(signal);
|
|
594
|
+
const startedAt = this.#now();
|
|
585
595
|
const { credentialId, credentialType, accountIdentity } = identity;
|
|
586
596
|
let kept = this.#kept.get(credentialId);
|
|
587
597
|
if (kept &&
|
|
588
598
|
(kept.attempt.credentialType !== credentialType ||
|
|
589
|
-
kept.attempt.expectedAccountIdentity !== accountIdentity
|
|
599
|
+
kept.attempt.expectedAccountIdentity !== accountIdentity ||
|
|
600
|
+
!this.#usable(kept.attempt))) {
|
|
590
601
|
this.forget(credentialId);
|
|
591
602
|
kept = undefined;
|
|
592
603
|
}
|
|
@@ -605,7 +616,22 @@ export class ClaustrumScopedCustody {
|
|
|
605
616
|
return this.#serveKept(credentialId, kept, token, routeId);
|
|
606
617
|
}
|
|
607
618
|
try {
|
|
608
|
-
|
|
619
|
+
let attempt;
|
|
620
|
+
try {
|
|
621
|
+
// Once recovery is in flight, later arrivals join it rather than
|
|
622
|
+
// issuing another immediate read into the same known outage.
|
|
623
|
+
const recovering = this.#outage &&
|
|
624
|
+
this.#receiptWaits.has(this.#receiptWaitKey({ credentialId, credentialType, accountIdentity }, token));
|
|
625
|
+
if (recovering)
|
|
626
|
+
throw this.#outageError;
|
|
627
|
+
attempt = await this.#authorizeFresh({ credentialId, credentialType, accountIdentity }, token, signal);
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
this.#check(signal);
|
|
631
|
+
if (classifyVaultFailure(error) !== 'unreachable' || kept)
|
|
632
|
+
throw error;
|
|
633
|
+
attempt = await this.#waitForReceipt({ credentialId, credentialType, accountIdentity }, token, startedAt, error, signal);
|
|
634
|
+
}
|
|
609
635
|
this.#kept.set(credentialId, { attempt, routeId, since: null });
|
|
610
636
|
this.#usageServed.set(credentialId, { ...identity });
|
|
611
637
|
return attempt;
|
|
@@ -619,6 +645,122 @@ export class ClaustrumScopedCustody {
|
|
|
619
645
|
return this.#serveKept(credentialId, kept, token, routeId, error);
|
|
620
646
|
}
|
|
621
647
|
}
|
|
648
|
+
#receiptWaitKey(identity, token) {
|
|
649
|
+
// Never share authorization across a changed enrollment or account binding.
|
|
650
|
+
return JSON.stringify([
|
|
651
|
+
identity.credentialId,
|
|
652
|
+
identity.credentialType,
|
|
653
|
+
identity.accountIdentity,
|
|
654
|
+
token,
|
|
655
|
+
]);
|
|
656
|
+
}
|
|
657
|
+
async #waitForReceipt(identity, token, startedAt, error, signal) {
|
|
658
|
+
// Measure from the first unreachable observation, even at cold start.
|
|
659
|
+
// Otherwise a process that never connected would charge every request 10 s
|
|
660
|
+
// for an arbitrarily long outage. Any vault answer starts a new window.
|
|
661
|
+
// Once one wait has run out in this outage, the outage has outlasted the
|
|
662
|
+
// bound: later cold reads fail at once. Without that, a host that tries its
|
|
663
|
+
// accounts in turn would pay the full wait once per account.
|
|
664
|
+
const remaining = VAULT_RECEIPT_WAIT_MS - (this.#now() - startedAt);
|
|
665
|
+
if (remaining <= 0 ||
|
|
666
|
+
this.#outageWaitSpent ||
|
|
667
|
+
this.#now() - this.#outageSince >= VAULT_YOUNG_OUTAGE_MS)
|
|
668
|
+
throw error;
|
|
669
|
+
const key = this.#receiptWaitKey(identity, token);
|
|
670
|
+
let wait = this.#receiptWaits.get(key);
|
|
671
|
+
if (!wait) {
|
|
672
|
+
const controller = new AbortController();
|
|
673
|
+
const created = {
|
|
674
|
+
controller,
|
|
675
|
+
users: 0,
|
|
676
|
+
error,
|
|
677
|
+
pending: Promise.resolve().then(async () => {
|
|
678
|
+
let delay = 250;
|
|
679
|
+
for (;;) {
|
|
680
|
+
await this.#receiptBackoff(delay, controller.signal);
|
|
681
|
+
try {
|
|
682
|
+
return await this.#authorizeFresh(identity, token, controller.signal);
|
|
683
|
+
}
|
|
684
|
+
catch (failure) {
|
|
685
|
+
this.#check(controller.signal);
|
|
686
|
+
if (classifyVaultFailure(failure) !== 'unreachable')
|
|
687
|
+
throw failure;
|
|
688
|
+
created.error = failure;
|
|
689
|
+
delay = Math.min(delay * 2, 1_000);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}),
|
|
693
|
+
};
|
|
694
|
+
wait = created;
|
|
695
|
+
this.#receiptWaits.set(key, wait);
|
|
696
|
+
const remove = () => {
|
|
697
|
+
if (this.#receiptWaits.get(key) === created)
|
|
698
|
+
this.#receiptWaits.delete(key);
|
|
699
|
+
};
|
|
700
|
+
void wait.pending.then(remove, remove);
|
|
701
|
+
}
|
|
702
|
+
const shared = wait;
|
|
703
|
+
shared.users++;
|
|
704
|
+
let timer;
|
|
705
|
+
let abort;
|
|
706
|
+
try {
|
|
707
|
+
const attempt = await Promise.race([
|
|
708
|
+
shared.pending,
|
|
709
|
+
new Promise((_resolve, reject) => {
|
|
710
|
+
timer = this.#setTimeout(() => {
|
|
711
|
+
if (this.#outage)
|
|
712
|
+
this.#outageWaitSpent = true;
|
|
713
|
+
reject(shared.error);
|
|
714
|
+
}, remaining);
|
|
715
|
+
timer.unref?.();
|
|
716
|
+
if (signal) {
|
|
717
|
+
abort = () => reject(signal.reason);
|
|
718
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
719
|
+
if (signal.aborted)
|
|
720
|
+
abort();
|
|
721
|
+
}
|
|
722
|
+
}),
|
|
723
|
+
]);
|
|
724
|
+
this.#check(signal);
|
|
725
|
+
// Each physical send owns its receipt and later failure report, even
|
|
726
|
+
// when several requests shared the same recovery read.
|
|
727
|
+
const receipt = Object.freeze(Object.defineProperty({ ...attempt }, 'accessToken', {
|
|
728
|
+
value: attempt.accessToken,
|
|
729
|
+
enumerable: false,
|
|
730
|
+
}));
|
|
731
|
+
this.#provenance.set(receipt, { token });
|
|
732
|
+
return receipt;
|
|
733
|
+
}
|
|
734
|
+
finally {
|
|
735
|
+
if (timer)
|
|
736
|
+
this.#clearTimeout(timer);
|
|
737
|
+
if (abort)
|
|
738
|
+
signal?.removeEventListener('abort', abort);
|
|
739
|
+
if (--shared.users === 0) {
|
|
740
|
+
shared.controller.abort();
|
|
741
|
+
if (this.#receiptWaits.get(key) === shared)
|
|
742
|
+
this.#receiptWaits.delete(key);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
#receiptBackoff(delay, signal) {
|
|
747
|
+
this.#check(signal);
|
|
748
|
+
return new Promise((resolve, reject) => {
|
|
749
|
+
const abort = () => {
|
|
750
|
+
this.#clearTimeout(timer);
|
|
751
|
+
signal.removeEventListener('abort', abort);
|
|
752
|
+
reject(signal.reason);
|
|
753
|
+
};
|
|
754
|
+
const timer = this.#setTimeout(() => {
|
|
755
|
+
signal.removeEventListener('abort', abort);
|
|
756
|
+
resolve();
|
|
757
|
+
}, delay);
|
|
758
|
+
timer.unref?.();
|
|
759
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
760
|
+
if (signal.aborted)
|
|
761
|
+
abort();
|
|
762
|
+
});
|
|
763
|
+
}
|
|
622
764
|
#serveKept(credentialId, kept, token, routeId, error = this.#outageError) {
|
|
623
765
|
// A concurrent refusal or 401 may already have discarded this entry.
|
|
624
766
|
if (!kept ||
|
|
@@ -876,6 +1018,9 @@ export class ClaustrumScopedCustody {
|
|
|
876
1018
|
if (this.#closed)
|
|
877
1019
|
return;
|
|
878
1020
|
this.#closed = true;
|
|
1021
|
+
for (const wait of this.#receiptWaits.values())
|
|
1022
|
+
wait.controller.abort();
|
|
1023
|
+
this.#receiptWaits.clear();
|
|
879
1024
|
if (this.#probeTimer)
|
|
880
1025
|
this.#clearTimeout(this.#probeTimer);
|
|
881
1026
|
this.#probeTimer = undefined;
|
|
@@ -159,6 +159,13 @@ export class ClaustrumDiscoveryHub {
|
|
|
159
159
|
#own;
|
|
160
160
|
#watch;
|
|
161
161
|
#run;
|
|
162
|
+
/**
|
|
163
|
+
* Member commit fan-outs still running, each until every one of its
|
|
164
|
+
* commits has settled. Only a run whose generation ended can leave one
|
|
165
|
+
* behind while another run starts; `#discover` waits for them before it
|
|
166
|
+
* reads the files it lists over.
|
|
167
|
+
*/
|
|
168
|
+
#fanOuts = new Set();
|
|
162
169
|
/** The hub's last authority read; undefined before the first one. */
|
|
163
170
|
#active;
|
|
164
171
|
/** Counts inactive transitions, so local work started before one stops. */
|
|
@@ -665,6 +672,15 @@ export class ClaustrumDiscoveryHub {
|
|
|
665
672
|
// Folders that could not be made owner-only fail every member's commit,
|
|
666
673
|
// as an unreadable base fails its member's: each is marked pending.
|
|
667
674
|
const folders = await this.#ensureRosterDirectories().then(() => undefined, (error) => error instanceof Error ? error : new Error(String(error)));
|
|
675
|
+
// A generation that ended may still have member commits in flight. A
|
|
676
|
+
// commit that passed its last generation check just before vault mode
|
|
677
|
+
// went off still renames its file afterwards, so its older listing can
|
|
678
|
+
// land after this run read that file. The base-view rule below would
|
|
679
|
+
// then take it for another process's newer commit and keep it over this
|
|
680
|
+
// list. The files are read only once every such commit has settled.
|
|
681
|
+
await this.#settleFanOuts();
|
|
682
|
+
if (!this.#live(generation))
|
|
683
|
+
return undefined;
|
|
668
684
|
// What each member's file held before the list. Another process may
|
|
669
685
|
// commit while this list runs; the base-view rule then keeps its newer
|
|
670
686
|
// roster rather than replacing it with this older listing. Each file is
|
|
@@ -696,9 +712,17 @@ export class ClaustrumDiscoveryHub {
|
|
|
696
712
|
// does), so no commit ever holds two members' locks and no order between
|
|
697
713
|
// members is needed. The results are then published in member order.
|
|
698
714
|
const members = this.#open();
|
|
699
|
-
const
|
|
715
|
+
const fanOut = Promise.all(members.map((entry) => entry.hooks && !entry.closed
|
|
700
716
|
? this.#commit(entry, entry.hooks, inventory, folders ?? bases.get(entry), generation)
|
|
701
717
|
: undefined));
|
|
718
|
+
this.#fanOuts.add(fanOut);
|
|
719
|
+
let commits;
|
|
720
|
+
try {
|
|
721
|
+
commits = await fanOut;
|
|
722
|
+
}
|
|
723
|
+
finally {
|
|
724
|
+
this.#fanOuts.delete(fanOut);
|
|
725
|
+
}
|
|
702
726
|
if (!this.#live(generation))
|
|
703
727
|
return undefined;
|
|
704
728
|
const errors = new Map();
|
|
@@ -813,6 +837,28 @@ export class ClaustrumDiscoveryHub {
|
|
|
813
837
|
return { error };
|
|
814
838
|
}
|
|
815
839
|
}
|
|
840
|
+
/**
|
|
841
|
+
* Wait until no member commit fan-out of an earlier run is still running.
|
|
842
|
+
* Each of them belongs to a generation that ended, so its commits fail or
|
|
843
|
+
* finish at their next generation check and this wait stays short.
|
|
844
|
+
*/
|
|
845
|
+
async #settleFanOuts() {
|
|
846
|
+
if (!this.#fanOuts.size)
|
|
847
|
+
return;
|
|
848
|
+
const phaseDiagnostics = channel('common-auth.claustrum.phases');
|
|
849
|
+
const phase = (name) => {
|
|
850
|
+
if (phaseDiagnostics.hasSubscribers)
|
|
851
|
+
phaseDiagnostics.publish({ logger: this.#logger, phase: name });
|
|
852
|
+
};
|
|
853
|
+
phase('earlier fan-out drain begin');
|
|
854
|
+
try {
|
|
855
|
+
while (this.#fanOuts.size)
|
|
856
|
+
await Promise.allSettled([...this.#fanOuts]);
|
|
857
|
+
}
|
|
858
|
+
finally {
|
|
859
|
+
phase('earlier fan-out drain end');
|
|
860
|
+
}
|
|
861
|
+
}
|
|
816
862
|
/**
|
|
817
863
|
* Create the roster folders under the state folder (`rosters`, this host
|
|
818
864
|
* generation and this key) owner-only, as every other Claustrum private
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { link, lstat, mkdir, open, readdir, readFile, rename, rm, stat, unlink, } from "node:fs/promises";
|
|
3
3
|
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { createLogger } from "./cc9e0702a752cea7-index.js";
|
|
4
5
|
import { DisappearedLockRecordError, isWindowsAccessDeniedCreate, isWindowsAccessDeniedRead, readLockRecord, } from "./723b00f96290a097-lock-record.js";
|
|
5
6
|
import { createPrivateBytes, createPrivateDirectoryExclusive, ensurePrivateDirectory, isWindowsSharingViolation, PrivateFileError, privateOperation, readPrivateText, verifyPrivate, windowsPrivateFiles, writePrivateAtomic, } from "./375d9cca2def037d-private-files.js";
|
|
6
7
|
import { LockOwnershipError, lockPathFor } from "./4825e03209d6ae3e-with-lock.js";
|
|
@@ -1191,17 +1192,17 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
1191
1192
|
removalConfirmed = true;
|
|
1192
1193
|
}, deadline);
|
|
1193
1194
|
changingRecordError = undefined;
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
});
|
|
1199
|
-
releaseConfirmed = removalConfirmed;
|
|
1195
|
+
// Acquiring the marker does not confirm removal: stale-marker
|
|
1196
|
+
// recovery can take it away after the lease owner was checked.
|
|
1197
|
+
if (markerAcquired && removalConfirmed) {
|
|
1198
|
+
releaseConfirmed = true;
|
|
1200
1199
|
return;
|
|
1201
1200
|
}
|
|
1202
|
-
if (
|
|
1203
|
-
|
|
1204
|
-
|
|
1201
|
+
if (!markerAcquired) {
|
|
1202
|
+
if (signal?.aborted)
|
|
1203
|
+
return;
|
|
1204
|
+
await recoverStaleEvictionMarker(deadline);
|
|
1205
|
+
}
|
|
1205
1206
|
}
|
|
1206
1207
|
catch (error) {
|
|
1207
1208
|
const changingRecord = windowsPrivateFiles &&
|
|
@@ -1242,6 +1243,9 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
1242
1243
|
new PrivateFileError(lockPath, 'release-not-confirmed', {
|
|
1243
1244
|
code: 'ETIMEDOUT',
|
|
1244
1245
|
}));
|
|
1246
|
+
createLogger('fs').warn('File lock release deadline expired', {
|
|
1247
|
+
lockPath,
|
|
1248
|
+
});
|
|
1245
1249
|
},
|
|
1246
1250
|
};
|
|
1247
1251
|
}
|
|
@@ -9,7 +9,7 @@ import { insulaUsageTransport } from "./d0f0b6be200311af-insula.js";
|
|
|
9
9
|
export { ClaustrumCredentialError, } from "@cortexkit/claustrum-client";
|
|
10
10
|
export { ClaustrumUnreachableError, classifyVaultFailure, VAULT_FAILURE_CLASSES, } from "./94f53cfd572ba4bb-availability.js";
|
|
11
11
|
export { ClaustrumConsumer, DEFAULT_CONNECT_TIMEOUT_MS, } from "./169b2b4bf30909bc-consumer.js";
|
|
12
|
-
export { ClaustrumScopedCustody, decideScopedRetryAfter401, isScopedCredentialRotation, KEPT_RECEIPT_MARGIN_MS, SERVING_MARGIN_MS, } from "./0f77ebc3c914c9c6-custody.js";
|
|
12
|
+
export { ClaustrumScopedCustody, decideScopedRetryAfter401, isScopedCredentialRotation, KEPT_RECEIPT_MARGIN_MS, SERVING_MARGIN_MS, VAULT_RECEIPT_WAIT_MS, VAULT_YOUNG_OUTAGE_MS, } from "./0f77ebc3c914c9c6-custody.js";
|
|
13
13
|
export { ClaustrumEnrollmentManager, classifyEnrollmentError, connectClaustrumEnrollmentClient, disconnectClaustrumEnrollment, enrollmentAuthority, enrollmentName, getClaustrumEnrollmentPaths, hostEnrollmentPaths, RETRYABLE_ENROLLMENT_CODES, readClaustrumEnrollmentStatus, readClaustrumEnrollmentToken, resetClaustrumEnrollmentState, TERMINAL_ENROLLMENT_CODES, validEnrollmentDisplayName, validEnrollmentRequestedGrants, vaultAuthority, } from "./b9588ad4725c1d10-enrollment.js";
|
|
14
14
|
export { ClaustrumConsumerError, } from "./ca0fccaba1d87af4-errors.js";
|
|
15
15
|
export { connectVaultHost, hasVaultHost, VAULT_HOST_CONNECT_REFUSAL, VAULT_HOST_STATUS, } from "./c97089ed4243981c-host.js";
|