@cortexkit/opencode-openai-auth 1.5.0 → 1.6.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/{codex-request-BjGT9yWg.d.ts → codex-request-D_In8uaR.d.ts} +43 -26
- package/dist/{index-gmmakhbt.js → index-1qxz4s8a.js} +2 -2
- package/dist/{index-2rw81dtm.js → index-79y7w9bz.js} +1 -1
- package/dist/{index-2yszc75z.js → index-7nmdsmpp.js} +2 -2
- package/dist/{index-3y453wg4.js → index-8xq099qf.js} +2 -2
- package/dist/index-agaatt62.js +65 -0
- package/dist/{index-mhfja8yr.js → index-b0ty9qam.js} +2 -2
- package/dist/index-ge60xry3.js +90 -0
- package/dist/{index-krcqvyqf.js → index-jkpg9d1p.js} +1 -1
- package/dist/index-k5anexvs.js +69 -0
- package/dist/{index-e416r885.js → index-rf0ht3gc.js} +2 -2
- package/dist/{index-4w0nsq5s.js → index-x9nffaye.js} +1 -1
- package/dist/{index-7vkn2e46.js → index-ya9c5e4m.js} +1 -1
- package/dist/{index-djnfq652.js → index-z8bptbrk.js} +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/{model-BMq5SCUB.d.ts → model-D22uj0OB.d.ts} +1 -1
- package/dist/{port-file-CjMFUWcX.d.ts → port-file-8ElfwDcq.d.ts} +1 -1
- package/dist/{protocol-BC3ndToM.d.ts → protocol-CK9guiFy.d.ts} +1 -1
- package/dist/rpc/port-file.d.ts +1 -1
- package/dist/rpc/port-file.js +1 -1
- package/dist/rpc/protocol.d.ts +1 -1
- package/dist/rpc/protocol.js +1 -1
- package/dist/rpc/rpc-client.d.ts +3 -3
- package/dist/rpc/rpc-client.js +1 -1
- package/dist/rpc/rpc-dir.js +1 -1
- package/dist/{sidebar-state-DfYyqiOH.d.ts → sidebar-state-Bb9L9oPq.d.ts} +4 -4
- package/dist/sidebar-state.d.ts +1 -1
- package/dist/sidebar-state.js +1 -1
- package/dist/token-util-nyvn9866.js +1 -0
- package/dist/{token-ycw7dyev.js → token-xtq3457d.js} +1 -1
- package/dist/tui-preferences.js +1 -1
- package/dist/tui.d.ts +2 -2
- package/dist/v2/server.d.ts +2 -2
- package/dist/v2/server.js +2 -2
- package/package.json +2 -2
- package/src/tui-compiled/raw/shared/81dddd203f1b92a3-quota-writer.js +4 -54
- package/src/tui-compiled/raw/shared/a62a27a204266acd-attribution.js +39 -25
- package/src/tui-compiled/raw/shared/a910277ac43a8626-pull.js +3 -1
- package/src/tui-compiled/raw/shared/c852a15cfe489d12-pool.js +18 -3
- package/src/tui-compiled/raw/shared/c911dbb64c9d85c7-quota-writer.js +298 -0
- package/src/tui-compiled/raw/shared/e1c5e56df0ba4053-pending.js +48 -0
- package/src/tui-compiled/runtime/shared/81dddd203f1b92a3-quota-writer.js +4 -54
- package/src/tui-compiled/runtime/shared/a62a27a204266acd-attribution.js +39 -25
- package/src/tui-compiled/runtime/shared/a910277ac43a8626-pull.js +3 -1
- package/src/tui-compiled/runtime/shared/c852a15cfe489d12-pool.js +18 -3
- package/src/tui-compiled/runtime/shared/c911dbb64c9d85c7-quota-writer.js +298 -0
- package/src/tui-compiled/runtime/shared/e1c5e56df0ba4053-pending.js +48 -0
- package/dist/index-2p3gcjsa.js +0 -69
- package/dist/index-6fdmqk6p.js +0 -90
- package/dist/index-fztcacjr.js +0 -65
- package/dist/token-util-ed4az5gg.js +0 -1
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { createLogger } from "./cc9e0702a752cea7-index.js";
|
|
2
|
+
import { assertQuotaEpoch, assertQuotaRow, mergedQuota, } from "./a62a27a204266acd-attribution.js";
|
|
3
|
+
import { PoolOperationError } from "./04a6357627de5ed0-errors.js";
|
|
4
|
+
import { toFailure, withTransaction } from "./458b934e92490331-mutate.js";
|
|
5
|
+
import { LockStack } from "./43fc9e6b994519c7-refresh-lock.js";
|
|
6
|
+
import { readRow, refusal } from "./83bc3ba9d0fe2761-runtime.js";
|
|
7
|
+
import { entryIn } from "./cd94316a888b0a8e-schema.js";
|
|
8
|
+
const COALESCE_MS = 100;
|
|
9
|
+
/**
|
|
10
|
+
* The most readings one row keeps waiting for a flush. Hosts record a reading
|
|
11
|
+
* about every 100 ms at most, so for them a queue this long means another
|
|
12
|
+
* writer has held the store locks for many seconds. Reaching it starts
|
|
13
|
+
* a flush at once instead of waiting for the coalescing timer or a retry
|
|
14
|
+
* backoff; a further reading for the row waits for room (never dropping an
|
|
15
|
+
* older one) for as long as the store's normal lock budget, and is then
|
|
16
|
+
* refused with `lock-contention`, as the immediate writer would refuse it.
|
|
17
|
+
*/
|
|
18
|
+
export const POOL_QUOTA_QUEUE_LIMIT = 128;
|
|
19
|
+
// Readings are queued whole per row and applied at flush in arrival order
|
|
20
|
+
// through the codec's own merge, so the codec alone decides which fields a
|
|
21
|
+
// newer or older reading replaces. Like the roster writer, a reading stays
|
|
22
|
+
// queued until the flush that took it commits: one queued while a flush waits
|
|
23
|
+
// for its locks must survive that flush.
|
|
24
|
+
export class PoolQuotaWriter {
|
|
25
|
+
rt;
|
|
26
|
+
timeoutMs;
|
|
27
|
+
pending = new Map();
|
|
28
|
+
warned = new Set();
|
|
29
|
+
wake = new Set();
|
|
30
|
+
logger;
|
|
31
|
+
timer;
|
|
32
|
+
running;
|
|
33
|
+
closing;
|
|
34
|
+
closed = false;
|
|
35
|
+
stopped = false;
|
|
36
|
+
failures = 0;
|
|
37
|
+
constructor(rt, timeoutMs = 1_000) {
|
|
38
|
+
this.rt = rt;
|
|
39
|
+
this.timeoutMs = timeoutMs;
|
|
40
|
+
this.logger = rt.ctx.logger ?? createLogger('store');
|
|
41
|
+
}
|
|
42
|
+
async record(id, attribution, observation) {
|
|
43
|
+
try {
|
|
44
|
+
this.assertOpen(id);
|
|
45
|
+
assertQuotaEpoch(id, attribution);
|
|
46
|
+
// Snapshot before the first await: callers may reuse their headers object.
|
|
47
|
+
const publication = structuredClone({
|
|
48
|
+
id,
|
|
49
|
+
attribution,
|
|
50
|
+
observation,
|
|
51
|
+
});
|
|
52
|
+
const { row, result } = await readRow(this.rt, 'pull', id);
|
|
53
|
+
assertQuotaRow(id, publication.attribution, row);
|
|
54
|
+
// Merge into what read() shows now, so a reading the codec refuses is
|
|
55
|
+
// refused to its caller here, as the immediate writer would refuse it.
|
|
56
|
+
mergedQuota(this.rt.ctx, id, this.overlayRow({ ...row, quota: entryIn(result.config, id)?.quota })
|
|
57
|
+
.quota, publication.observation);
|
|
58
|
+
await this.room(id);
|
|
59
|
+
this.assertOpen(id);
|
|
60
|
+
// No await between the room check and the push, so the cap holds.
|
|
61
|
+
const queue = this.pending.get(id) ?? [];
|
|
62
|
+
queue.push(publication);
|
|
63
|
+
this.pending.set(id, queue);
|
|
64
|
+
if (queue.length >= POOL_QUOTA_QUEUE_LIMIT)
|
|
65
|
+
this.flushNow();
|
|
66
|
+
else
|
|
67
|
+
this.schedule(COALESCE_MS);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
throw toFailure(error, 'pull', id, { writes: 0 });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
queued(id) {
|
|
74
|
+
return this.pending.get(id)?.length ?? 0;
|
|
75
|
+
}
|
|
76
|
+
// Waits while the row's queue is full, flushing at once on every wake-up,
|
|
77
|
+
// for at most the store's own lock acquisition budget.
|
|
78
|
+
async room(id) {
|
|
79
|
+
if (this.queued(id) < POOL_QUOTA_QUEUE_LIMIT)
|
|
80
|
+
return;
|
|
81
|
+
const { ctx } = this.rt;
|
|
82
|
+
const budget = Math.max(0, ...ctx.storeLocks.map((spec) => spec.timeoutMs ?? ctx.lockDefaults.timeoutMs));
|
|
83
|
+
const deadline = performance.now() + budget;
|
|
84
|
+
while (this.queued(id) >= POOL_QUOTA_QUEUE_LIMIT) {
|
|
85
|
+
this.assertOpen(id);
|
|
86
|
+
this.flushNow();
|
|
87
|
+
const left = deadline - performance.now();
|
|
88
|
+
if (left <= 0)
|
|
89
|
+
throw refusal('pull', id, 'lock-contention', `pool quota queue for ${id} is full (${POOL_QUOTA_QUEUE_LIMIT} readings) and the store locks stayed held`, true);
|
|
90
|
+
await new Promise((resolve) => {
|
|
91
|
+
const done = () => {
|
|
92
|
+
clearTimeout(timer);
|
|
93
|
+
this.wake.delete(done);
|
|
94
|
+
resolve();
|
|
95
|
+
};
|
|
96
|
+
const timer = setTimeout(done, left);
|
|
97
|
+
this.wake.add(done);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
assertOpen(id) {
|
|
102
|
+
if (this.closed)
|
|
103
|
+
throw refusal('pull', id, 'invalid-input', 'pool quota writer is closed');
|
|
104
|
+
}
|
|
105
|
+
// What read() and load() show: the stored map, then each queued reading of
|
|
106
|
+
// the row still bound to it, in arrival order through the codec's merge.
|
|
107
|
+
// A reading the codec now refuses is left out, as the flush drops it.
|
|
108
|
+
overlayRow(row) {
|
|
109
|
+
let next = row;
|
|
110
|
+
for (const publication of this.pending.get(row.id) ?? []) {
|
|
111
|
+
try {
|
|
112
|
+
assertQuotaRow(row.id, publication.attribution, row);
|
|
113
|
+
next = {
|
|
114
|
+
...next,
|
|
115
|
+
quota: mergedQuota(this.rt.ctx, row.id, next.quota, publication.observation),
|
|
116
|
+
needsFirstReading: false,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
catch { }
|
|
120
|
+
}
|
|
121
|
+
return next;
|
|
122
|
+
}
|
|
123
|
+
overlay(rows) {
|
|
124
|
+
return rows.map((row) => this.overlayRow(row));
|
|
125
|
+
}
|
|
126
|
+
schedule(delay) {
|
|
127
|
+
if (this.running || this.timer || !this.pending.size || this.stopped)
|
|
128
|
+
return;
|
|
129
|
+
this.timer = setTimeout(() => {
|
|
130
|
+
this.timer = undefined;
|
|
131
|
+
this.flush();
|
|
132
|
+
}, delay);
|
|
133
|
+
if (!this.closed)
|
|
134
|
+
this.timer.unref?.();
|
|
135
|
+
}
|
|
136
|
+
// Starts a flush now rather than at the coalescing timer or retry backoff;
|
|
137
|
+
// a flush already running is left to finish.
|
|
138
|
+
flushNow() {
|
|
139
|
+
if (this.running)
|
|
140
|
+
return;
|
|
141
|
+
clearTimeout(this.timer);
|
|
142
|
+
this.timer = undefined;
|
|
143
|
+
this.flush();
|
|
144
|
+
}
|
|
145
|
+
flush() {
|
|
146
|
+
if (this.running || this.stopped || !this.pending.size)
|
|
147
|
+
return;
|
|
148
|
+
// Every queued reading, each row's in arrival order.
|
|
149
|
+
const batch = [...this.pending.values()].flat();
|
|
150
|
+
// Short lock attempts allow retries to pick up freshly queued readings and
|
|
151
|
+
// bound shutdown even when the normal store acquisition budget is long.
|
|
152
|
+
const ctx = {
|
|
153
|
+
...this.rt.ctx,
|
|
154
|
+
storeLocks: this.rt.ctx.storeLocks.map((spec) => ({
|
|
155
|
+
...spec,
|
|
156
|
+
timeoutMs: Math.min(spec.timeoutMs ?? this.rt.ctx.lockDefaults.timeoutMs, 100),
|
|
157
|
+
})),
|
|
158
|
+
};
|
|
159
|
+
const locks = new LockStack(ctx.lockDefaults, ctx.lockEnv);
|
|
160
|
+
this.running = (async () => {
|
|
161
|
+
try {
|
|
162
|
+
await withTransaction(ctx, locks, { writes: 0 }, { operation: 'pull', rowId: undefined }, async (tx) => {
|
|
163
|
+
if (this.stopped)
|
|
164
|
+
return;
|
|
165
|
+
let changed = false;
|
|
166
|
+
for (const publication of batch) {
|
|
167
|
+
const { id, attribution, observation } = publication;
|
|
168
|
+
const witness = JSON.stringify([
|
|
169
|
+
id,
|
|
170
|
+
attribution.credentialEpoch,
|
|
171
|
+
attribution.identity,
|
|
172
|
+
]);
|
|
173
|
+
try {
|
|
174
|
+
tx.assertNotStaged(id);
|
|
175
|
+
assertQuotaRow(id, attribution, tx.row(id));
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (!(error instanceof PoolOperationError))
|
|
179
|
+
throw error;
|
|
180
|
+
if (!this.warned.has(witness)) {
|
|
181
|
+
this.warned.add(witness);
|
|
182
|
+
this.warn('pool quota publication dropped; account binding changed', { id, kind: error.kind });
|
|
183
|
+
}
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const entry = tx.entry(id);
|
|
187
|
+
if (!entry)
|
|
188
|
+
continue;
|
|
189
|
+
let quota;
|
|
190
|
+
try {
|
|
191
|
+
quota = mergedQuota(ctx, id, entry.quota, observation);
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
// The codec accepted this reading when it was queued, but not
|
|
195
|
+
// against what is stored now (another writer changed it).
|
|
196
|
+
// Drop only this reading: failing the transaction would retry
|
|
197
|
+
// the same refusal forever and hold back every other row.
|
|
198
|
+
const key = `codec:${witness}`;
|
|
199
|
+
if (!this.warned.has(key)) {
|
|
200
|
+
this.warned.add(key);
|
|
201
|
+
this.warn('pool quota publication dropped; the quota codec refused it', {
|
|
202
|
+
id,
|
|
203
|
+
error: error instanceof Error ? error.message : String(error),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
tx.setEntry(id, { ...entry, quota, needsFirstReading: false });
|
|
209
|
+
changed = true;
|
|
210
|
+
}
|
|
211
|
+
if (changed && !this.stopped)
|
|
212
|
+
await tx.commitConfig();
|
|
213
|
+
}, { completeTorn: false });
|
|
214
|
+
const flushed = new Set(batch);
|
|
215
|
+
for (const [id, queue] of this.pending) {
|
|
216
|
+
const rest = queue.filter((publication) => !flushed.has(publication));
|
|
217
|
+
if (rest.length)
|
|
218
|
+
this.pending.set(id, rest);
|
|
219
|
+
else
|
|
220
|
+
this.pending.delete(id);
|
|
221
|
+
}
|
|
222
|
+
this.failures = 0;
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
if (!this.stopped && this.failures++ === 0)
|
|
226
|
+
this.warn('pool quota publication delayed; retrying', {
|
|
227
|
+
error: error instanceof Error ? error.message : String(error),
|
|
228
|
+
});
|
|
229
|
+
// Retain the mailbox on failure, including fresher readings queued meanwhile.
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
await locks.releaseAll();
|
|
233
|
+
this.running = undefined;
|
|
234
|
+
for (const wake of this.wake)
|
|
235
|
+
wake();
|
|
236
|
+
const delay = this.failures
|
|
237
|
+
? Math.min(5_000, 100 * 2 ** Math.min(this.failures, 6)) *
|
|
238
|
+
(0.5 + Math.random())
|
|
239
|
+
: COALESCE_MS;
|
|
240
|
+
this.schedule(delay);
|
|
241
|
+
}
|
|
242
|
+
})();
|
|
243
|
+
}
|
|
244
|
+
warn(message, data) {
|
|
245
|
+
try {
|
|
246
|
+
this.logger.warn(message, data);
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
/* Logging cannot stop publication. */
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
close() {
|
|
253
|
+
if (this.closing)
|
|
254
|
+
return this.closing;
|
|
255
|
+
this.closed = true;
|
|
256
|
+
this.closing = this.drain();
|
|
257
|
+
return this.closing;
|
|
258
|
+
}
|
|
259
|
+
async drain() {
|
|
260
|
+
clearTimeout(this.timer);
|
|
261
|
+
this.timer = undefined;
|
|
262
|
+
let deadline;
|
|
263
|
+
let wake;
|
|
264
|
+
const drained = new Promise((done) => {
|
|
265
|
+
wake = () => {
|
|
266
|
+
if (!this.pending.size)
|
|
267
|
+
done();
|
|
268
|
+
};
|
|
269
|
+
this.wake.add(wake);
|
|
270
|
+
wake();
|
|
271
|
+
});
|
|
272
|
+
try {
|
|
273
|
+
this.flush();
|
|
274
|
+
await Promise.race([
|
|
275
|
+
drained,
|
|
276
|
+
new Promise((done) => {
|
|
277
|
+
deadline = setTimeout(done, Math.max(0, this.timeoutMs));
|
|
278
|
+
}),
|
|
279
|
+
]);
|
|
280
|
+
}
|
|
281
|
+
finally {
|
|
282
|
+
this.stopped = true;
|
|
283
|
+
clearTimeout(deadline);
|
|
284
|
+
clearTimeout(this.timer);
|
|
285
|
+
this.wake.delete(wake);
|
|
286
|
+
if (this.pending.size)
|
|
287
|
+
this.warn('pool quota drain timed out; pending readings left', {
|
|
288
|
+
accounts: this.pending.size,
|
|
289
|
+
readings: [...this.pending.values()].reduce((sum, queue) => sum + queue.length, 0),
|
|
290
|
+
});
|
|
291
|
+
this.pending.clear();
|
|
292
|
+
// A reading waiting for room in a full queue is refused as closed when
|
|
293
|
+
// the drain ends, not when its own lock budget runs out.
|
|
294
|
+
for (const wake of this.wake)
|
|
295
|
+
wake();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Use the same independently dated parts as mergeQuotaObservation. A header
|
|
2
|
+
// reading cannot erase a poll-only window, counter, credits record or budget.
|
|
3
|
+
export function quotaFields(publication, witness = '') {
|
|
4
|
+
const result = new Map();
|
|
5
|
+
const { observation } = publication;
|
|
6
|
+
const add = (key, at, part) => result.set(`${witness}:${key}`, {
|
|
7
|
+
at,
|
|
8
|
+
order: 0,
|
|
9
|
+
publication: { ...publication, observation: part },
|
|
10
|
+
});
|
|
11
|
+
const keyFor = (scope, label) => JSON.stringify([scope ?? 'all', label]);
|
|
12
|
+
const read = new Set();
|
|
13
|
+
for (const reading of observation.readings ?? []) {
|
|
14
|
+
const key = keyFor(reading.scope, reading.label);
|
|
15
|
+
read.add(key);
|
|
16
|
+
add(key, observation.checkedAt, {
|
|
17
|
+
checkedAt: observation.checkedAt,
|
|
18
|
+
readings: [{ ...reading }],
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
for (const pair of observation.coverage ?? []) {
|
|
22
|
+
const key = keyFor(pair.scope, pair.label);
|
|
23
|
+
if (!read.has(key))
|
|
24
|
+
add(key, observation.checkedAt, {
|
|
25
|
+
checkedAt: observation.checkedAt,
|
|
26
|
+
coverage: [{ ...pair }],
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (observation.budget)
|
|
30
|
+
add('budget', observation.checkedAt, {
|
|
31
|
+
checkedAt: observation.checkedAt,
|
|
32
|
+
budget: { ...observation.budget },
|
|
33
|
+
});
|
|
34
|
+
for (const [name, counter] of Object.entries(observation.counters ?? {})) {
|
|
35
|
+
add(`counter:${name}`, counter.checkedAt, {
|
|
36
|
+
checkedAt: observation.checkedAt,
|
|
37
|
+
counters: { [name]: { ...counter } },
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (observation.credits)
|
|
41
|
+
add('credits', observation.credits.checkedAt, {
|
|
42
|
+
checkedAt: observation.checkedAt,
|
|
43
|
+
credits: { ...observation.credits },
|
|
44
|
+
});
|
|
45
|
+
if (!result.size)
|
|
46
|
+
add('empty', observation.checkedAt, { checkedAt: observation.checkedAt });
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import { createLogger } from "./cc9e0702a752cea7-index.js";
|
|
3
3
|
import { isQuotaObservation } from "./d5a8f1061ee2f114-index.js";
|
|
4
|
+
import { quotaFields } from "./e1c5e56df0ba4053-pending.js";
|
|
4
5
|
import { applyVaultQuota, mutateVaultRoster, } from "./30f5bc480f31f51c-roster.js";
|
|
5
6
|
// Every consumer of the same file shares one mailbox and one lock waiter.
|
|
6
7
|
const writers = new Map();
|
|
@@ -34,65 +35,14 @@ export function retainVaultQuotaWriter(path, listener) {
|
|
|
34
35
|
return writer.listeners.size ? Promise.resolve() : drainVaultQuota(path);
|
|
35
36
|
})());
|
|
36
37
|
}
|
|
37
|
-
//
|
|
38
|
-
// window (scope and label), each covered-but-unread window, the budget, each
|
|
39
|
-
// counter and the credits record. The mailbox keeps the newest of each part on
|
|
40
|
-
// its own, so a later observation that carries only some parts can't erase an
|
|
41
|
-
// older but still newest reading of another part, which is how
|
|
42
|
-
// mergeQuotaObservation treats them on disk. Each key also carries the
|
|
43
|
-
// receipt's identity, so the identity fence still applies to every part.
|
|
38
|
+
// Each field retains the receipt fence as well as its independent timestamp.
|
|
44
39
|
function fields(publication) {
|
|
45
|
-
|
|
46
|
-
const { observation } = publication;
|
|
47
|
-
const witness = JSON.stringify([
|
|
40
|
+
return quotaFields(publication, JSON.stringify([
|
|
48
41
|
publication.routeId,
|
|
49
42
|
publication.receipt?.credentialId,
|
|
50
43
|
publication.receipt?.accountIdentitySource,
|
|
51
44
|
publication.receipt?.expectedAccountIdentity,
|
|
52
|
-
]);
|
|
53
|
-
const add = (key, at, part) => result.set(`${witness}:${key}`, {
|
|
54
|
-
at,
|
|
55
|
-
order: 0,
|
|
56
|
-
publication: { ...publication, observation: part },
|
|
57
|
-
});
|
|
58
|
-
const keyFor = (scope, label) => JSON.stringify([scope ?? 'all', label]);
|
|
59
|
-
const read = new Set();
|
|
60
|
-
for (const reading of observation.readings ?? []) {
|
|
61
|
-
const key = keyFor(reading.scope, reading.label);
|
|
62
|
-
read.add(key);
|
|
63
|
-
add(key, observation.checkedAt, {
|
|
64
|
-
checkedAt: observation.checkedAt,
|
|
65
|
-
readings: [{ ...reading }],
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
for (const pair of observation.coverage ?? []) {
|
|
69
|
-
const key = keyFor(pair.scope, pair.label);
|
|
70
|
-
if (!read.has(key))
|
|
71
|
-
add(key, observation.checkedAt, {
|
|
72
|
-
checkedAt: observation.checkedAt,
|
|
73
|
-
coverage: [{ ...pair }],
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
if (observation.budget)
|
|
77
|
-
add('budget', observation.checkedAt, {
|
|
78
|
-
checkedAt: observation.checkedAt,
|
|
79
|
-
budget: { ...observation.budget },
|
|
80
|
-
});
|
|
81
|
-
for (const [name, counter] of Object.entries(observation.counters ?? {})) {
|
|
82
|
-
add(`counter:${name}`, counter.checkedAt, {
|
|
83
|
-
checkedAt: observation.checkedAt,
|
|
84
|
-
counters: { [name]: { ...counter } },
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
if (observation.credits)
|
|
88
|
-
add('credits', observation.credits.checkedAt, {
|
|
89
|
-
checkedAt: observation.checkedAt,
|
|
90
|
-
credits: { ...observation.credits },
|
|
91
|
-
});
|
|
92
|
-
// Even an empty observation can bind an account proved by its receipt.
|
|
93
|
-
if (!result.size)
|
|
94
|
-
add('empty', observation.checkedAt, { checkedAt: observation.checkedAt });
|
|
95
|
-
return result;
|
|
45
|
+
]));
|
|
96
46
|
}
|
|
97
47
|
/** Enqueue without filesystem work; each quota field retains its own receipt fence. */
|
|
98
48
|
export function enqueueVaultQuota(path, publication) {
|
|
@@ -1,8 +1,41 @@
|
|
|
1
1
|
import { PoolOperationError } from "./04a6357627de5ed0-errors.js";
|
|
2
|
-
import { toFailure, withTransaction } from "./458b934e92490331-mutate.js";
|
|
2
|
+
import { toFailure, withTransaction, } from "./458b934e92490331-mutate.js";
|
|
3
3
|
import { LockStack } from "./43fc9e6b994519c7-refresh-lock.js";
|
|
4
4
|
import { refusal, requireBound, unknownRow, } from "./83bc3ba9d0fe2761-runtime.js";
|
|
5
5
|
import { isCredentialEpoch } from "./cd94316a888b0a8e-schema.js";
|
|
6
|
+
export function assertQuotaEpoch(id, attribution) {
|
|
7
|
+
if (!isCredentialEpoch(attribution?.credentialEpoch))
|
|
8
|
+
throw refusal('pull', id, 'invalid-input', 'the credential epoch the reading was issued for must be a positive safe integer');
|
|
9
|
+
}
|
|
10
|
+
// Shared by preflight and the locked commit: accepting into memory must never
|
|
11
|
+
// hide a refusal the immediate writer would have returned to its caller.
|
|
12
|
+
export function assertQuotaRow(id, attribution, row) {
|
|
13
|
+
if (!row)
|
|
14
|
+
throw unknownRow('pull', id);
|
|
15
|
+
if (row.invalid)
|
|
16
|
+
throw refusal('pull', id, 'invalid-row', `row ${id} is invalid`);
|
|
17
|
+
if (!row.credential)
|
|
18
|
+
throw refusal('pull', id, 'no-credential', `row ${id} holds no credential`);
|
|
19
|
+
requireBound('pull', row);
|
|
20
|
+
if (row.torn ||
|
|
21
|
+
!row.hasEntry ||
|
|
22
|
+
row.credentialEpoch !== attribution.credentialEpoch ||
|
|
23
|
+
row.identity !== attribution.identity)
|
|
24
|
+
throw new PoolOperationError({
|
|
25
|
+
operation: 'pull',
|
|
26
|
+
rowId: id,
|
|
27
|
+
phase: 'pull',
|
|
28
|
+
retryable: true,
|
|
29
|
+
kind: 'attribution',
|
|
30
|
+
message: `quota for ${id} was issued for a credential the row no longer holds`,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export function mergedQuota(ctx, id, stored, observation) {
|
|
34
|
+
const merged = ctx.codec.merge(stored, observation);
|
|
35
|
+
if (!ctx.codec.validate(merged))
|
|
36
|
+
throw refusal('pull', id, 'invalid-quota', 'the quota codec rejected the merged map');
|
|
37
|
+
return merged;
|
|
38
|
+
}
|
|
6
39
|
/**
|
|
7
40
|
* Merges a quota observation into a row's stored map under the store locks,
|
|
8
41
|
* after attribution passes. Needs no row lock, so it is permitted from
|
|
@@ -13,37 +46,18 @@ export async function recordQuota(rt, id, attribution, observation) {
|
|
|
13
46
|
const locks = new LockStack(ctx.lockDefaults, ctx.lockEnv);
|
|
14
47
|
const progress = { writes: 0 };
|
|
15
48
|
try {
|
|
16
|
-
|
|
17
|
-
throw refusal('pull', id, 'invalid-input', 'the credential epoch the reading was issued for must be a positive safe integer');
|
|
49
|
+
assertQuotaEpoch(id, attribution);
|
|
18
50
|
await withTransaction(ctx, locks, progress, { operation: 'pull', rowId: id }, async (tx) => {
|
|
19
51
|
tx.assertNotStaged(id);
|
|
20
52
|
const row = tx.row(id);
|
|
21
|
-
if (!row)
|
|
22
|
-
throw unknownRow('pull', id);
|
|
23
|
-
if (row.invalid)
|
|
24
|
-
throw refusal('pull', id, 'invalid-row', `row ${id} is invalid`);
|
|
25
53
|
// A reading belongs to one (epoch, identity, credential). A row
|
|
26
54
|
// holding no credential, or torn between the writes of a replace,
|
|
27
55
|
// has no such triple on disk, so no reading is recorded for it.
|
|
28
|
-
|
|
29
|
-
throw refusal('pull', id, 'no-credential', `row ${id} holds no credential`);
|
|
30
|
-
requireBound('pull', row);
|
|
56
|
+
assertQuotaRow(id, attribution, row);
|
|
31
57
|
const entry = tx.entry(id);
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
row.identity !== attribution.identity)
|
|
36
|
-
throw new PoolOperationError({
|
|
37
|
-
operation: 'pull',
|
|
38
|
-
rowId: id,
|
|
39
|
-
phase: 'pull',
|
|
40
|
-
retryable: true,
|
|
41
|
-
kind: 'attribution',
|
|
42
|
-
message: `quota for ${id} was issued for a credential the row no longer holds`,
|
|
43
|
-
});
|
|
44
|
-
const merged = ctx.codec.merge(entry.quota, observation);
|
|
45
|
-
if (!ctx.codec.validate(merged))
|
|
46
|
-
throw refusal('pull', id, 'invalid-quota', 'the quota codec rejected the merged map');
|
|
58
|
+
if (!entry)
|
|
59
|
+
throw unknownRow('pull', id);
|
|
60
|
+
const merged = mergedQuota(ctx, id, entry.quota, observation);
|
|
47
61
|
tx.setEntry(id, { ...entry, quota: merged, needsFirstReading: false });
|
|
48
62
|
await tx.commitConfig();
|
|
49
63
|
},
|
|
@@ -105,7 +105,9 @@ export class PullScheduler {
|
|
|
105
105
|
cause,
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
|
-
|
|
108
|
+
const publish = rt.publishQuota ??
|
|
109
|
+
((rowId, attribution, value) => recordQuota(rt, rowId, attribution, value));
|
|
110
|
+
await publish(id, {
|
|
109
111
|
credentialEpoch: request.credentialEpoch,
|
|
110
112
|
...(request.identity !== undefined
|
|
111
113
|
? { identity: request.identity }
|
|
@@ -3,6 +3,7 @@ import { initializePool, readPool, } from "./458b934e92490331-mutate.js";
|
|
|
3
3
|
import { updateProviderStateRow, } from "./97484d9bb98c705b-provider-state.js";
|
|
4
4
|
import { publication, publishRoster, } from "./83068c0dbe8a6f13-publication.js";
|
|
5
5
|
import { PullScheduler } from "./a910277ac43a8626-pull.js";
|
|
6
|
+
import { PoolQuotaWriter } from "./c911dbb64c9d85c7-quota-writer.js";
|
|
6
7
|
import { refreshRow, } from "./9d3fc9e37456504e-refresh.js";
|
|
7
8
|
import { POOL_LOCK_DEFAULTS, } from "./43fc9e6b994519c7-refresh-lock.js";
|
|
8
9
|
import { addRow, disableRow, enableRow, recordRowIdentity, removeRow, reorderRows, replaceRow, rotateRow, } from "./fe02abeadc535f77-rows.js";
|
|
@@ -80,9 +81,21 @@ export function openPoolStore(options) {
|
|
|
80
81
|
rowLockOptions: options.rowLockOptions ?? {},
|
|
81
82
|
firePull: (id, reason) => pulls.fire(id, reason),
|
|
82
83
|
};
|
|
84
|
+
const quotaWriter = options.coalesceQuota
|
|
85
|
+
? new PoolQuotaWriter(rt, options.quotaDrainTimeoutMs)
|
|
86
|
+
: undefined;
|
|
87
|
+
const record = (id, attribution, observation) => quotaWriter
|
|
88
|
+
? quotaWriter.record(id, attribution, observation)
|
|
89
|
+
: recordQuota(rt, id, attribution, observation);
|
|
90
|
+
rt.publishQuota = record;
|
|
91
|
+
const overlay = (result) => {
|
|
92
|
+
if (result.status === 'ready' && quotaWriter)
|
|
93
|
+
return { ...result, rows: quotaWriter.overlay(result.rows) };
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
83
96
|
return {
|
|
84
97
|
async load() {
|
|
85
|
-
const result = await readPool(ctx);
|
|
98
|
+
const result = overlay(await readPool(ctx));
|
|
86
99
|
if (result.status === 'ready') {
|
|
87
100
|
// A torn row is no candidate, but its pull is fired too: the pull
|
|
88
101
|
// completes the row on disk before reading it, so an interrupted
|
|
@@ -96,7 +109,7 @@ export function openPoolStore(options) {
|
|
|
96
109
|
return toLoad(result);
|
|
97
110
|
},
|
|
98
111
|
async read() {
|
|
99
|
-
return toLoad(await readPool(ctx));
|
|
112
|
+
return toLoad(overlay(await readPool(ctx)));
|
|
100
113
|
},
|
|
101
114
|
async initialize(input = {}) {
|
|
102
115
|
return { status: await initializePool(ctx, input.dropKeys ?? []) };
|
|
@@ -115,7 +128,9 @@ export function openPoolStore(options) {
|
|
|
115
128
|
updateSettings: (mutator, callOptions) => updatePoolSettings(rt, mutator, callOptions),
|
|
116
129
|
recordIdentity: (id, identity, attribution, callOptions) => recordRowIdentity(rt, id, identity, attribution, callOptions),
|
|
117
130
|
refresh: (id, provider, callOptions) => refreshRow(rt, id, provider, callOptions),
|
|
118
|
-
recordQuota:
|
|
131
|
+
recordQuota: record,
|
|
132
|
+
close: () => quotaWriter?.close() ?? Promise.resolve(),
|
|
133
|
+
dispose: () => quotaWriter?.close() ?? Promise.resolve(),
|
|
119
134
|
requestReading: (id) => pulls.fire(id, 'admission'),
|
|
120
135
|
pullsSettled: () => pulls.settled(),
|
|
121
136
|
};
|