@cortexkit/antigravity-auth-core 3.0.7 → 3.0.9
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/common-auth-embedded/auth-menu/login.d.ts +6 -10
- package/dist/common-auth-embedded/auth-menu/login.js +14 -11
- package/dist/common-auth-embedded/claustrum/availability.d.ts +89 -0
- package/dist/common-auth-embedded/claustrum/availability.js +106 -0
- package/dist/common-auth-embedded/claustrum/consumer.d.ts +36 -2
- package/dist/common-auth-embedded/claustrum/consumer.js +154 -6
- package/dist/common-auth-embedded/claustrum/custody.d.ts +106 -14
- package/dist/common-auth-embedded/claustrum/custody.js +294 -16
- package/dist/common-auth-embedded/claustrum/errors.d.ts +1 -0
- package/dist/common-auth-embedded/claustrum/index.d.ts +5 -3
- package/dist/common-auth-embedded/claustrum/index.js +4 -1
- package/dist/common-auth-embedded/claustrum/insula.d.ts +71 -0
- package/dist/common-auth-embedded/claustrum/insula.js +348 -21
- package/dist/common-auth-embedded/claustrum/watch.d.ts +141 -0
- package/dist/common-auth-embedded/claustrum/watch.js +254 -0
- package/dist/common-auth-embedded/fs/private-files.js +10 -2
- package/dist/common-auth-embedded/quota/format.d.ts +3 -0
- package/dist/common-auth-embedded/quota/format.js +69 -1
- package/dist/common-auth-embedded/quota/index.d.ts +1 -1
- package/dist/common-auth-embedded/quota/projection.d.ts +16 -0
- package/dist/common-auth-embedded/source-output.json +87 -55
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -1
- package/dist/quota-presentation.d.ts.map +1 -1
- package/dist/quota-presentation.js +9 -1
- package/dist/quota-presentation.js.map +1 -1
- package/dist/vault-account-source.d.ts +20 -3
- package/dist/vault-account-source.d.ts.map +1 -1
- package/dist/vault-account-source.js +45 -1
- package/dist/vault-account-source.js.map +1 -1
- package/dist/vault-folder-refusal.js +3 -3
- package/package.json +3 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ClaustrumClientOptions } from '@cortexkit/claustrum-client';
|
|
2
|
+
import { type WatchMode } from './watch.js';
|
|
2
3
|
/** Insula's window fields are open: plugins choose which limits they understand. */
|
|
3
4
|
export interface InsulaUsageWindow {
|
|
4
5
|
usedPercent: number;
|
|
@@ -33,6 +34,27 @@ export interface InsulaQuotaContext {
|
|
|
33
34
|
fetchedAt: number;
|
|
34
35
|
source?: string;
|
|
35
36
|
}
|
|
37
|
+
export type InsulaQuotaMiss = 'cold' | 'stale' | 'absent' | 'upstream-failed' | 'credential' | 'unreachable';
|
|
38
|
+
export interface InsulaQuotaReading {
|
|
39
|
+
entry: InsulaUsageEntry;
|
|
40
|
+
context: InsulaQuotaContext;
|
|
41
|
+
}
|
|
42
|
+
/** A miss may retain a dated reading; it is not a current quota observation. */
|
|
43
|
+
export type InsulaQuotaLookup = {
|
|
44
|
+
reading: InsulaQuotaReading;
|
|
45
|
+
miss?: never;
|
|
46
|
+
} | {
|
|
47
|
+
miss: InsulaQuotaMiss;
|
|
48
|
+
reading?: InsulaQuotaReading;
|
|
49
|
+
};
|
|
50
|
+
/** The normalized equivalent of a source lookup, for a plugin's quota type. */
|
|
51
|
+
export type InsulaQuotaResult<Q> = {
|
|
52
|
+
reading: Q;
|
|
53
|
+
miss?: never;
|
|
54
|
+
} | {
|
|
55
|
+
miss: InsulaQuotaMiss;
|
|
56
|
+
reading?: Q;
|
|
57
|
+
};
|
|
36
58
|
export interface InsulaQuotaOptions<Q> {
|
|
37
59
|
provider: 'codex' | 'claude' | 'antigravity';
|
|
38
60
|
/**
|
|
@@ -43,25 +65,74 @@ export interface InsulaQuotaOptions<Q> {
|
|
|
43
65
|
*/
|
|
44
66
|
normalize: (entry: InsulaUsageEntry, context: InsulaQuotaContext) => Q | undefined;
|
|
45
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* A held-open `usage.watch` stream. `closed` resolves when insula ends the
|
|
70
|
+
* stream (STREAM_END at shutdown, or a reply that ends it) or after `stop()`,
|
|
71
|
+
* and rejects with insula's refusal (an error with a `code`, such as
|
|
72
|
+
* `unknown_method` from an insula that predates the method, or
|
|
73
|
+
* `usage_watch_limit` when 64 watches are already open) or with the
|
|
74
|
+
* transport failure.
|
|
75
|
+
*/
|
|
76
|
+
export interface InsulaUsageWatch {
|
|
77
|
+
stop(): void;
|
|
78
|
+
readonly closed: Promise<void>;
|
|
79
|
+
}
|
|
46
80
|
/** Optional transport for custom scoped clients; getUsage must not retry refusals. */
|
|
47
81
|
export interface InsulaUsageTransport {
|
|
48
82
|
connectionFile: string;
|
|
49
83
|
getUsage(): Promise<unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* Open a `usage.watch` stream, passing each notice's raw JSON body to
|
|
86
|
+
* `onNotice`. Optional: a transport without it is polled as before.
|
|
87
|
+
*/
|
|
88
|
+
watchUsage?(onNotice: (body: Uint8Array) => void): InsulaUsageWatch;
|
|
50
89
|
}
|
|
51
90
|
/** Constructing this transport opens no connection; getUsage opens one per call. */
|
|
52
91
|
export declare function insulaUsageTransport(options: ClaustrumClientOptions & {
|
|
53
92
|
connectionFile: string;
|
|
54
93
|
}): InsulaUsageTransport;
|
|
94
|
+
export interface InsulaWatchOptions {
|
|
95
|
+
/**
|
|
96
|
+
* The provider this watcher reads. A notice that names only other
|
|
97
|
+
* providers then needs no read; unset means every notice does.
|
|
98
|
+
*/
|
|
99
|
+
provider?: string;
|
|
100
|
+
/**
|
|
101
|
+
* setTimeout and clearTimeout for the waits before re-subscribing or retrying
|
|
102
|
+
* a watch. Defaults to the global timers; tests inject virtual ones.
|
|
103
|
+
*/
|
|
104
|
+
setTimeoutImpl?: typeof setTimeout;
|
|
105
|
+
clearTimeoutImpl?: typeof clearTimeout;
|
|
106
|
+
}
|
|
55
107
|
export declare class InsulaQuotaSource {
|
|
56
108
|
#private;
|
|
57
109
|
private readonly transport;
|
|
58
110
|
private readonly key;
|
|
59
111
|
constructor(transport: InsulaUsageTransport, key: string);
|
|
112
|
+
/**
|
|
113
|
+
* The background poll: at most one usage.get per minute per daemon and
|
|
114
|
+
* enrollment token. While a watch is open it reads nothing, because insula's
|
|
115
|
+
* notices announce every new reading. If insula stops announcing an account,
|
|
116
|
+
* that account's last entry grows older than the freshness limit `current()`
|
|
117
|
+
* applies, `current()` returns nothing for it, and the plugin falls back to
|
|
118
|
+
* its own provider poll for that account.
|
|
119
|
+
*/
|
|
60
120
|
refresh(now: number): Promise<void>;
|
|
121
|
+
/** How this source currently learns about usage changes, for logs. */
|
|
122
|
+
watchMode(): WatchMode;
|
|
123
|
+
/**
|
|
124
|
+
* Hold a `usage.watch` open while any watcher wants it, and re-read
|
|
125
|
+
* usage.get when a notice says something changed. Watchers of one source
|
|
126
|
+
* share one watch, since insula admits only 64 per process. Returns the
|
|
127
|
+
* release; the last release closes the watch.
|
|
128
|
+
*/
|
|
129
|
+
watch(options?: InsulaWatchOptions): () => void;
|
|
61
130
|
current(provider: string, accountId: string, now: number): {
|
|
62
131
|
entry: InsulaUsageEntry;
|
|
63
132
|
context: InsulaQuotaContext;
|
|
64
133
|
} | undefined;
|
|
134
|
+
/** Cache-only background lookup; current() retains its original miss behavior. */
|
|
135
|
+
lookup(provider: string, accountId: string, now: number): InsulaQuotaLookup;
|
|
65
136
|
}
|
|
66
137
|
export declare function acquireInsulaQuotaSource(transport: InsulaUsageTransport, tokenPath: string): {
|
|
67
138
|
source: InsulaQuotaSource;
|
|
@@ -5,6 +5,8 @@ import { storeIdentity } from '@cortexkit/claustrum-client';
|
|
|
5
5
|
// import makes bundlers emit a separate chunk that a running host loses when
|
|
6
6
|
// its dist folder is rebuilt underneath it.
|
|
7
7
|
import { SubcClient } from '@cortexkit/subc-client';
|
|
8
|
+
import { classifyVaultFailure } from './availability.js';
|
|
9
|
+
import { ChangeNoticeReader, CoalescedRead, RESUBSCRIBE_BACKOFF_MS, WATCH_RETRY_MS, } from './watch.js';
|
|
8
10
|
/** Constructing this transport opens no connection; getUsage opens one per call. */
|
|
9
11
|
export function insulaUsageTransport(options) {
|
|
10
12
|
const identity = options.identity ??
|
|
@@ -49,10 +51,86 @@ export function insulaUsageTransport(options) {
|
|
|
49
51
|
client?.close();
|
|
50
52
|
}
|
|
51
53
|
},
|
|
54
|
+
watchUsage(onNotice) {
|
|
55
|
+
// The watch holds its own connection for as long as it is open: each
|
|
56
|
+
// usage.get above opens and closes a short-lived one, and the scoped
|
|
57
|
+
// credential connection is never shared with insula. The method name
|
|
58
|
+
// alone makes the request a watch; subc-client does not yet set the
|
|
59
|
+
// SUBSCRIPTION flag, and insula does not require it.
|
|
60
|
+
let client;
|
|
61
|
+
let subscription;
|
|
62
|
+
let stopped = false;
|
|
63
|
+
const closed = (async () => {
|
|
64
|
+
let deadline;
|
|
65
|
+
const timeout = new Promise((_resolve, reject) => {
|
|
66
|
+
deadline = setTimeout(
|
|
67
|
+
// Coded as a timeout, so InsulaQuotaSource treats a watch that could
|
|
68
|
+
// not open in time as an unreachable insula and tries again later,
|
|
69
|
+
// not as a refusal.
|
|
70
|
+
() => reject(Object.assign(new Error('insula watch timeout'), {
|
|
71
|
+
code: 'timeout',
|
|
72
|
+
})), 3_000);
|
|
73
|
+
deadline.unref?.();
|
|
74
|
+
});
|
|
75
|
+
const open = (async () => {
|
|
76
|
+
const handshakeTimeoutMs = Math.min(options.handshakeTimeoutMs ?? 3_000, 3_000);
|
|
77
|
+
const connected = options.connector
|
|
78
|
+
? await options.connector({
|
|
79
|
+
connectionFile: options.connectionFile,
|
|
80
|
+
handshakeTimeoutMs,
|
|
81
|
+
})
|
|
82
|
+
: await SubcClient.connect({
|
|
83
|
+
connectionFile: options.connectionFile,
|
|
84
|
+
handshakeTimeoutMs,
|
|
85
|
+
});
|
|
86
|
+
client = connected;
|
|
87
|
+
// A connection that arrives after stop() or after the open deadline
|
|
88
|
+
// must not keep a route or socket alive.
|
|
89
|
+
if (stopped) {
|
|
90
|
+
connected.close();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const route = await connected.routeOpen({ kind: 'management_surface', module_id: 'insula' }, identity);
|
|
94
|
+
if (stopped) {
|
|
95
|
+
connected.close();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
subscription = connected.subscribe(route, { method: 'usage.watch' }, onNotice);
|
|
99
|
+
})();
|
|
100
|
+
try {
|
|
101
|
+
await Promise.race([open, timeout]);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
stopped = true;
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
clearTimeout(deadline);
|
|
109
|
+
}
|
|
110
|
+
if (subscription)
|
|
111
|
+
await subscription.closed;
|
|
112
|
+
})().finally(() => {
|
|
113
|
+
stopped = true;
|
|
114
|
+
client?.close();
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
stop() {
|
|
118
|
+
if (stopped)
|
|
119
|
+
return;
|
|
120
|
+
stopped = true;
|
|
121
|
+
subscription?.unsubscribe();
|
|
122
|
+
client?.close();
|
|
123
|
+
},
|
|
124
|
+
closed,
|
|
125
|
+
};
|
|
126
|
+
},
|
|
52
127
|
};
|
|
53
128
|
}
|
|
54
129
|
const MINUTE = 60_000;
|
|
55
130
|
const MAX_AGE = 4 * MINUTE;
|
|
131
|
+
// consumer-contract.md:331 refetches a slot after 60 s. Two such intervals
|
|
132
|
+
// bound our willingness to wait, not a claim that insula completed its sweep.
|
|
133
|
+
const COLD_WAIT = 2 * MINUTE;
|
|
56
134
|
const sources = new Map();
|
|
57
135
|
// Keep only the minute budget after the last owner closes, so project churn
|
|
58
136
|
// cannot reset the bound. Expired budgets are pruned on the next tick.
|
|
@@ -60,18 +138,107 @@ const attempts = new Map();
|
|
|
60
138
|
function record(value) {
|
|
61
139
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
62
140
|
}
|
|
141
|
+
function decodeUsageNotice(body) {
|
|
142
|
+
let value;
|
|
143
|
+
try {
|
|
144
|
+
value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(body));
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
if (!record(value) ||
|
|
150
|
+
typeof value.kind !== 'string' ||
|
|
151
|
+
typeof value.epoch !== 'string' ||
|
|
152
|
+
value.epoch.length === 0 ||
|
|
153
|
+
typeof value.seq !== 'number' ||
|
|
154
|
+
!Number.isSafeInteger(value.seq) ||
|
|
155
|
+
value.seq < 1 ||
|
|
156
|
+
(value.provider !== undefined && typeof value.provider !== 'string'))
|
|
157
|
+
return undefined;
|
|
158
|
+
return {
|
|
159
|
+
kind: value.kind,
|
|
160
|
+
epoch: value.epoch,
|
|
161
|
+
seq: value.seq,
|
|
162
|
+
...(typeof value.provider === 'string' && { provider: value.provider }),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function refusalCode(error) {
|
|
166
|
+
return error instanceof Error &&
|
|
167
|
+
'code' in error &&
|
|
168
|
+
typeof error.code === 'string'
|
|
169
|
+
? error.code
|
|
170
|
+
: undefined;
|
|
171
|
+
}
|
|
63
172
|
export class InsulaQuotaSource {
|
|
64
173
|
transport;
|
|
65
174
|
key;
|
|
66
175
|
#entries = [];
|
|
67
|
-
#
|
|
176
|
+
#available = false;
|
|
177
|
+
#completeProviders = new Set();
|
|
178
|
+
#coldSince = new Map();
|
|
179
|
+
/** Every usage.get, polled or watch-driven, shares this one slot. */
|
|
180
|
+
#reads = new CoalescedRead(async () => {
|
|
181
|
+
try {
|
|
182
|
+
const reply = await this.transport.getUsage();
|
|
183
|
+
if (!record(reply) ||
|
|
184
|
+
!Array.isArray(reply.result) ||
|
|
185
|
+
reply.error !== undefined)
|
|
186
|
+
throw new Error('unavailable insula usage');
|
|
187
|
+
// Each response is a snapshot, not a delta. Only completeProviders
|
|
188
|
+
// asserts completeness; neither pending nor the entry count does.
|
|
189
|
+
this.#entries = reply.result.filter((entry) => record(entry) && typeof entry.provider === 'string');
|
|
190
|
+
this.#available =
|
|
191
|
+
this.#entries.length === reply.result.length &&
|
|
192
|
+
(reply.completeProviders === undefined ||
|
|
193
|
+
(Array.isArray(reply.completeProviders) &&
|
|
194
|
+
reply.completeProviders.every((value) => typeof value === 'string')));
|
|
195
|
+
this.#completeProviders = new Set(this.#available && Array.isArray(reply.completeProviders)
|
|
196
|
+
? reply.completeProviders
|
|
197
|
+
: []);
|
|
198
|
+
for (const provider of this.#completeProviders)
|
|
199
|
+
this.#coldSince.delete(provider);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
// Refusal is absence, even if the previous reading was still young.
|
|
203
|
+
this.#entries = [];
|
|
204
|
+
this.#available = false;
|
|
205
|
+
this.#completeProviders.clear();
|
|
206
|
+
}
|
|
207
|
+
}, () => { });
|
|
208
|
+
#reader = new ChangeNoticeReader();
|
|
209
|
+
#mode = 'polling';
|
|
210
|
+
#watchers = 0;
|
|
211
|
+
/**
|
|
212
|
+
* How many registered watchers read every provider. Watchers limited to one
|
|
213
|
+
* provider are counted per provider in `#providers` instead.
|
|
214
|
+
*/
|
|
215
|
+
#everyProvider = 0;
|
|
216
|
+
#providers = new Map();
|
|
217
|
+
#watch;
|
|
218
|
+
#timer;
|
|
219
|
+
#timers = {};
|
|
220
|
+
/**
|
|
221
|
+
* How many accepted watches in a row have closed; each one doubles the wait
|
|
222
|
+
* before the next re-subscribe. Reset once a watch brings a second notice.
|
|
223
|
+
*/
|
|
224
|
+
#closes = 0;
|
|
68
225
|
constructor(transport, key) {
|
|
69
226
|
this.transport = transport;
|
|
70
227
|
this.key = key;
|
|
71
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* The background poll: at most one usage.get per minute per daemon and
|
|
231
|
+
* enrollment token. While a watch is open it reads nothing, because insula's
|
|
232
|
+
* notices announce every new reading. If insula stops announcing an account,
|
|
233
|
+
* that account's last entry grows older than the freshness limit `current()`
|
|
234
|
+
* applies, `current()` returns nothing for it, and the plugin falls back to
|
|
235
|
+
* its own provider poll for that account.
|
|
236
|
+
*/
|
|
72
237
|
refresh(now) {
|
|
73
|
-
if (this.#
|
|
74
|
-
return this.#
|
|
238
|
+
if (this.#reads.running)
|
|
239
|
+
return this.#reads.running;
|
|
240
|
+
if (this.#mode === 'watching')
|
|
241
|
+
return Promise.resolve();
|
|
75
242
|
for (const [key, time] of attempts) {
|
|
76
243
|
if (now - time >= MINUTE)
|
|
77
244
|
attempts.delete(key);
|
|
@@ -81,25 +248,141 @@ export class InsulaQuotaSource {
|
|
|
81
248
|
return Promise.resolve();
|
|
82
249
|
// Reserve the minute before I/O, so concurrent projects cannot each poll.
|
|
83
250
|
attempts.set(this.key, now);
|
|
84
|
-
this.#
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
251
|
+
return this.#reads.request();
|
|
252
|
+
}
|
|
253
|
+
/** How this source currently learns about usage changes, for logs. */
|
|
254
|
+
watchMode() {
|
|
255
|
+
return this.#mode;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Hold a `usage.watch` open while any watcher wants it, and re-read
|
|
259
|
+
* usage.get when a notice says something changed. Watchers of one source
|
|
260
|
+
* share one watch, since insula admits only 64 per process. Returns the
|
|
261
|
+
* release; the last release closes the watch.
|
|
262
|
+
*/
|
|
263
|
+
watch(options = {}) {
|
|
264
|
+
this.#watchers++;
|
|
265
|
+
const provider = options.provider;
|
|
266
|
+
if (provider === undefined)
|
|
267
|
+
this.#everyProvider++;
|
|
268
|
+
else
|
|
269
|
+
this.#providers.set(provider, (this.#providers.get(provider) ?? 0) + 1);
|
|
270
|
+
if (this.#watchers === 1) {
|
|
271
|
+
this.#timers = options;
|
|
272
|
+
this.#open();
|
|
273
|
+
}
|
|
274
|
+
let released = false;
|
|
275
|
+
return () => {
|
|
276
|
+
if (released)
|
|
277
|
+
return;
|
|
278
|
+
released = true;
|
|
279
|
+
if (provider === undefined)
|
|
280
|
+
this.#everyProvider--;
|
|
281
|
+
else {
|
|
282
|
+
const left = (this.#providers.get(provider) ?? 1) - 1;
|
|
283
|
+
if (left > 0)
|
|
284
|
+
this.#providers.set(provider, left);
|
|
285
|
+
else
|
|
286
|
+
this.#providers.delete(provider);
|
|
98
287
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
288
|
+
if (--this.#watchers === 0)
|
|
289
|
+
this.#stopWatch();
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
#stopWatch() {
|
|
293
|
+
if (this.#timer)
|
|
294
|
+
(this.#timers.clearTimeoutImpl ?? clearTimeout)(this.#timer);
|
|
295
|
+
this.#timer = undefined;
|
|
296
|
+
const watch = this.#watch;
|
|
297
|
+
this.#watch = undefined;
|
|
298
|
+
watch?.handle?.stop();
|
|
299
|
+
this.#mode = 'polling';
|
|
300
|
+
}
|
|
301
|
+
#later(delay) {
|
|
302
|
+
this.#timer = (this.#timers.setTimeoutImpl ?? setTimeout)(() => {
|
|
303
|
+
this.#timer = undefined;
|
|
304
|
+
if (this.#watchers > 0 && !this.#watch)
|
|
305
|
+
this.#open();
|
|
306
|
+
}, delay);
|
|
307
|
+
this.#timer.unref?.();
|
|
308
|
+
}
|
|
309
|
+
#open() {
|
|
310
|
+
const watchUsage = this.transport.watchUsage;
|
|
311
|
+
if (!watchUsage) {
|
|
312
|
+
// A custom transport that cannot watch: poll exactly as before.
|
|
313
|
+
this.#mode = 'fallback';
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
this.#mode = 'polling';
|
|
317
|
+
this.#reader.restart();
|
|
318
|
+
const watch = { notices: 0 };
|
|
319
|
+
this.#watch = watch;
|
|
320
|
+
let handle;
|
|
321
|
+
try {
|
|
322
|
+
handle = watchUsage.call(this.transport, (body) => this.#notice(watch, body));
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
this.#closed(watch, error);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
watch.handle = handle;
|
|
329
|
+
handle.closed.then(() => this.#closed(watch, undefined), (error) => this.#closed(watch, error));
|
|
330
|
+
}
|
|
331
|
+
#notice(watch, body) {
|
|
332
|
+
// Frames already in transport can arrive after a local stop.
|
|
333
|
+
if (this.#watch !== watch)
|
|
334
|
+
return;
|
|
335
|
+
watch.notices++;
|
|
336
|
+
if (watch.notices === 1)
|
|
337
|
+
this.#mode = 'watching';
|
|
338
|
+
// A second notice means the watch stayed up past its acceptance.
|
|
339
|
+
if (watch.notices === 2)
|
|
340
|
+
this.#closes = 0;
|
|
341
|
+
const notice = decodeUsageNotice(body);
|
|
342
|
+
// An unreadable notice may hide any change: treat it as a break, and
|
|
343
|
+
// make the next notice read as one too.
|
|
344
|
+
if (!notice)
|
|
345
|
+
this.#reader.restart();
|
|
346
|
+
const continuous = notice ? this.#reader.continuous(notice) : false;
|
|
347
|
+
// The first notice of every watch names no provider, so it always reads:
|
|
348
|
+
// nothing was watched before it. After a gap or a new epoch notices were
|
|
349
|
+
// lost, so read even when this notice names another provider.
|
|
350
|
+
if (!continuous ||
|
|
351
|
+
notice?.provider === undefined ||
|
|
352
|
+
this.#everyProvider > 0 ||
|
|
353
|
+
this.#providers.has(notice.provider))
|
|
354
|
+
void this.#reads.request();
|
|
355
|
+
}
|
|
356
|
+
#closed(watch, error) {
|
|
357
|
+
if (this.#watch !== watch)
|
|
358
|
+
return;
|
|
359
|
+
this.#watch = undefined;
|
|
360
|
+
const code = refusalCode(error);
|
|
361
|
+
if (code === 'unknown_method' || code === 'usage_watch_limit') {
|
|
362
|
+
// An insula that predates usage.watch, or one already holding 64
|
|
363
|
+
// watches: poll exactly as before until every watcher has released.
|
|
364
|
+
this.#mode = 'fallback';
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (watch.notices > 0) {
|
|
368
|
+
// An accepted watch that closed (insula shut down, or the connection
|
|
369
|
+
// dropped) gets one new watch, whose first notice reads usage.get.
|
|
370
|
+
const delay = Math.min(RESUBSCRIBE_BACKOFF_MS * 2 ** Math.min(this.#closes, 16), WATCH_RETRY_MS);
|
|
371
|
+
this.#closes++;
|
|
372
|
+
this.#mode = 'polling';
|
|
373
|
+
this.#later(delay);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
// Closed before any notice, so never accepted: read once to learn
|
|
377
|
+
// whether insula answers at all, then poll as before. A refusal insula
|
|
378
|
+
// answered stands until every watcher has released. A close it did not
|
|
379
|
+
// explain (a
|
|
380
|
+
// dropped connection, a timeout, a stream ended at once while insula
|
|
381
|
+
// shuts down) earns one new watch much later.
|
|
382
|
+
this.#mode = 'fallback';
|
|
383
|
+
void this.#reads.request();
|
|
384
|
+
if (error === undefined || classifyVaultFailure(error) === 'unreachable')
|
|
385
|
+
this.#later(WATCH_RETRY_MS);
|
|
103
386
|
}
|
|
104
387
|
current(provider, accountId, now) {
|
|
105
388
|
for (const entry of this.#entries) {
|
|
@@ -126,6 +409,50 @@ export class InsulaQuotaSource {
|
|
|
126
409
|
}
|
|
127
410
|
return undefined;
|
|
128
411
|
}
|
|
412
|
+
/** Cache-only background lookup; current() retains its original miss behavior. */
|
|
413
|
+
lookup(provider, accountId, now) {
|
|
414
|
+
if (!this.#available)
|
|
415
|
+
return { miss: 'unreachable' };
|
|
416
|
+
const entry = this.#entries.find((value) => value.provider === provider &&
|
|
417
|
+
typeof value.account === 'string' &&
|
|
418
|
+
value.account.trim() === accountId);
|
|
419
|
+
// An unlabelled error describes the provider's credential path, not an
|
|
420
|
+
// unrelated labelled account. Never borrow another account's verdict.
|
|
421
|
+
const errorClass = (entry ??
|
|
422
|
+
this.#entries.find((value) => value.provider === provider &&
|
|
423
|
+
value.account === undefined &&
|
|
424
|
+
value.errorClass !== undefined))?.errorClass;
|
|
425
|
+
if (errorClass === 'upstream_failed')
|
|
426
|
+
return { miss: 'upstream-failed' };
|
|
427
|
+
if (errorClass === 'credential_absent' ||
|
|
428
|
+
errorClass === 'credential_unusable' ||
|
|
429
|
+
errorClass === 'credential_rejected')
|
|
430
|
+
return { miss: 'credential' };
|
|
431
|
+
if (errorClass !== undefined)
|
|
432
|
+
return { miss: 'unreachable' };
|
|
433
|
+
if (entry) {
|
|
434
|
+
const fetchedAt = typeof entry.fetchedAt === 'string'
|
|
435
|
+
? Date.parse(entry.fetchedAt)
|
|
436
|
+
: Number.NaN;
|
|
437
|
+
if (!record(entry.usage) || !Number.isFinite(fetchedAt))
|
|
438
|
+
return { miss: 'unreachable' };
|
|
439
|
+
const reading = {
|
|
440
|
+
entry,
|
|
441
|
+
context: {
|
|
442
|
+
fetchedAt,
|
|
443
|
+
...(typeof entry.source === 'string' && { source: entry.source }),
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
return now >= fetchedAt + MAX_AGE || entry.stale !== undefined
|
|
447
|
+
? { miss: 'stale', reading }
|
|
448
|
+
: { reading };
|
|
449
|
+
}
|
|
450
|
+
if (this.#completeProviders.has(provider))
|
|
451
|
+
return { miss: 'absent' };
|
|
452
|
+
const since = this.#coldSince.get(provider) ?? now;
|
|
453
|
+
this.#coldSince.set(provider, since);
|
|
454
|
+
return { miss: now - since >= COLD_WAIT ? 'unreachable' : 'cold' };
|
|
455
|
+
}
|
|
129
456
|
}
|
|
130
457
|
export function acquireInsulaQuotaSource(transport, tokenPath) {
|
|
131
458
|
const key = JSON.stringify([
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { ClaustrumLogger } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* How a host currently learns about changes, for logs and status displays.
|
|
4
|
+
* `watching`: a watch was accepted and is open, so polling is reduced to a
|
|
5
|
+
* slow safety read. `polling`: no watch is open yet (not started, opening, or
|
|
6
|
+
* re-subscribing after a close), so the host polls at its usual interval.
|
|
7
|
+
* `fallback`: the watch is unavailable (an older module or client, a refusal,
|
|
8
|
+
* the watch limit, or repeated failures), so the host polls at its usual
|
|
9
|
+
* interval and may try a watch again later. Never carries secrets.
|
|
10
|
+
*/
|
|
11
|
+
export type WatchMode = 'watching' | 'polling' | 'fallback';
|
|
12
|
+
/**
|
|
13
|
+
* While a vault watch is healthy, the roster is still read this often: the
|
|
14
|
+
* watch carries no history, so a lost notice or a silently dead stream is
|
|
15
|
+
* caught within this period.
|
|
16
|
+
*/
|
|
17
|
+
export declare const SAFETY_POLL_MS: number;
|
|
18
|
+
/**
|
|
19
|
+
* After a watch fails without being accepted, the host polls and tries one
|
|
20
|
+
* new watch this much later, so a refusing or draining module is never asked
|
|
21
|
+
* in a tight loop.
|
|
22
|
+
*/
|
|
23
|
+
export declare const WATCH_RETRY_MS: number;
|
|
24
|
+
/**
|
|
25
|
+
* First wait before re-subscribing a watch that closed after it had been
|
|
26
|
+
* accepted. It doubles for each further close in a row, up to WATCH_RETRY_MS.
|
|
27
|
+
*/
|
|
28
|
+
export declare const RESUBSCRIBE_BACKOFF_MS = 1000;
|
|
29
|
+
/**
|
|
30
|
+
* The fields every change notice carries, from the vault's watchScoped and
|
|
31
|
+
* insula's usage.watch alike.
|
|
32
|
+
*/
|
|
33
|
+
export interface ChangeNotice {
|
|
34
|
+
readonly kind: string;
|
|
35
|
+
readonly epoch: string;
|
|
36
|
+
readonly seq: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Tells whether a notice follows the previous one with nothing lost between
|
|
40
|
+
* them. `seq` restarts at 1 for every watch and `epoch` names the sending
|
|
41
|
+
* module's process lifetime. A sequence gap (notices are sheddable), a
|
|
42
|
+
* different epoch (the module restarted) or a notice of an unknown kind all
|
|
43
|
+
* mean changes may have been missed, so the caller must re-read everything.
|
|
44
|
+
*/
|
|
45
|
+
export declare class ChangeNoticeReader {
|
|
46
|
+
#private;
|
|
47
|
+
/**
|
|
48
|
+
* Begin a new watch, whose first notice is seq 1 again. The epoch is kept,
|
|
49
|
+
* so a module restart between two watches still reads as a break.
|
|
50
|
+
*/
|
|
51
|
+
restart(): void;
|
|
52
|
+
continuous(notice: ChangeNotice): boolean;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Runs `read` with at most one call in flight and at most one more queued
|
|
56
|
+
* behind it, however many requests arrive meanwhile. A request made while a
|
|
57
|
+
* read is running may have arrived after that read took its snapshot, so it
|
|
58
|
+
* earns exactly one more read; any further requests share that one.
|
|
59
|
+
*/
|
|
60
|
+
export declare class CoalescedRead {
|
|
61
|
+
#private;
|
|
62
|
+
constructor(read: () => Promise<void>, onError: (error: unknown) => void);
|
|
63
|
+
/** The read in flight (including its queued follow-up), if any. */
|
|
64
|
+
get running(): Promise<void> | undefined;
|
|
65
|
+
request(): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
/** A vault watch notice: `view` is the same digest `list_scoped` returns. */
|
|
68
|
+
export interface ScopedWatchNoticeShape extends ChangeNotice {
|
|
69
|
+
readonly view: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* How a vault watch ended, as claustrum-client 0.12.0 reports it. `refused`
|
|
73
|
+
* and `not_accepting` both mean the watch closed before its first notice; the
|
|
74
|
+
* client then made one `list_scoped` read to learn why, and `refused` carries
|
|
75
|
+
* that read's refusal.
|
|
76
|
+
*/
|
|
77
|
+
export type ScopedWatchEnd = {
|
|
78
|
+
readonly kind: 'refused';
|
|
79
|
+
readonly error: unknown;
|
|
80
|
+
} | {
|
|
81
|
+
readonly kind: 'transport_lost';
|
|
82
|
+
readonly error: unknown;
|
|
83
|
+
} | {
|
|
84
|
+
readonly kind: 'ended' | 'not_accepting' | 'unsupported' | 'stopped';
|
|
85
|
+
};
|
|
86
|
+
export interface ScopedWatchHandle {
|
|
87
|
+
stop(): void;
|
|
88
|
+
readonly closed: Promise<ScopedWatchEnd>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The part of a vault client that can watch. claustrum-client added
|
|
92
|
+
* `watchScoped` in 0.12.0; the peer range still admits 0.6.2, so it is
|
|
93
|
+
* optional and detected at run time.
|
|
94
|
+
*/
|
|
95
|
+
export interface ScopedWatchSource {
|
|
96
|
+
watchScoped?(onNotice: (notice: ScopedWatchNoticeShape) => void, options?: {
|
|
97
|
+
enrollmentToken?: string;
|
|
98
|
+
}): ScopedWatchHandle;
|
|
99
|
+
}
|
|
100
|
+
export interface ScopedRosterWatchOptions {
|
|
101
|
+
client: ScopedWatchSource;
|
|
102
|
+
/** The enrollment token, read again for every watch so a new one is used. */
|
|
103
|
+
enrollmentToken: () => Promise<string>;
|
|
104
|
+
/** The view of the roster this host last discovered; undefined for none. */
|
|
105
|
+
view: () => string | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* The host's existing discovery: `list_scoped`, the roster commit and
|
|
108
|
+
* everything that follows it. The watch never reads the vault list itself.
|
|
109
|
+
* `view` is the view the newest notice carried.
|
|
110
|
+
*/
|
|
111
|
+
discover: (view: string) => Promise<unknown>;
|
|
112
|
+
/**
|
|
113
|
+
* The vault refused the watch, and the confirming `list_scoped` read it
|
|
114
|
+
* with this error (a revoked enrollment ends both with `not_found`). Handle
|
|
115
|
+
* it as discovery handles the same refusal from `list_scoped`.
|
|
116
|
+
*/
|
|
117
|
+
onRefused: (error: unknown) => void;
|
|
118
|
+
onTransportLost?: () => void;
|
|
119
|
+
onReachable?: () => void;
|
|
120
|
+
onError?: (error: unknown) => void;
|
|
121
|
+
onMode?: (mode: WatchMode) => void;
|
|
122
|
+
logger?: ClaustrumLogger;
|
|
123
|
+
now?: () => number;
|
|
124
|
+
setTimeoutImpl?: typeof setTimeout;
|
|
125
|
+
clearTimeoutImpl?: typeof clearTimeout;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Keeps one vault `watchScoped` open for a host and runs the host's discovery
|
|
129
|
+
* when a notice shows the visible account list changed. Discovery runs only
|
|
130
|
+
* when the notice's view differs from the last discovered roster's view, when
|
|
131
|
+
* a notice shows a gap or a new epoch, or on the first notice of a watch that
|
|
132
|
+
* was re-subscribed after a close (the time between two watches was not
|
|
133
|
+
* observed). It never opens a second watch while one is open.
|
|
134
|
+
*/
|
|
135
|
+
export declare class ScopedRosterWatch {
|
|
136
|
+
#private;
|
|
137
|
+
constructor(options: ScopedRosterWatchOptions);
|
|
138
|
+
get mode(): WatchMode;
|
|
139
|
+
start(): void;
|
|
140
|
+
stop(): void;
|
|
141
|
+
}
|