@cortexkit/common-auth 0.25.0 → 0.27.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/claustrum/consumer.d.ts +63 -2
- package/dist/claustrum/consumer.js +250 -9
- package/dist/claustrum/custody.d.ts +36 -1
- package/dist/claustrum/custody.js +95 -11
- package/dist/claustrum/hub.d.ts +187 -0
- package/dist/claustrum/hub.js +960 -0
- package/dist/claustrum/index.d.ts +3 -2
- package/dist/claustrum/index.js +2 -1
- package/dist/claustrum/quota-writer.js +4 -54
- package/dist/claustrum/roster.d.ts +47 -0
- package/dist/claustrum/roster.js +74 -3
- package/dist/claustrum/shutdown.d.ts +8 -0
- package/dist/claustrum/shutdown.js +42 -0
- package/dist/fs/refresh-file-lock.d.ts +1 -1
- package/dist/fs/refresh-file-lock.js +20 -11
- package/dist/opencode2/errors.d.ts +4 -1
- package/dist/opencode2/index.d.ts +2 -2
- package/dist/opencode2/index.js +1 -1
- package/dist/opencode2/install.d.ts +7 -5
- package/dist/opencode2/install.js +139 -13
- package/dist/opencode2/types.d.ts +42 -10
- package/dist/quota/pending.d.ts +11 -0
- package/dist/quota/pending.js +48 -0
- package/dist/redaction/index.d.ts +8 -0
- package/dist/redaction/index.js +490 -0
- package/dist/rpc/rpc-server.js +29 -19
- package/dist/setup/command.js +52 -14
- package/dist/setup/register.d.ts +2 -0
- package/dist/setup/register.js +90 -11
- package/dist/store/attribution.d.ts +5 -0
- package/dist/store/attribution.js +39 -25
- package/dist/store/pool.d.ts +17 -0
- package/dist/store/pool.js +18 -3
- package/dist/store/pull.js +3 -1
- package/dist/store/quota-writer.d.ts +40 -0
- package/dist/store/quota-writer.js +298 -0
- package/dist/store/runtime.d.ts +2 -0
- package/package.json +6 -2
package/dist/setup/command.js
CHANGED
|
@@ -10,6 +10,7 @@ import { assertHostsStopped, createProcessFence, ProcessFenceViolationError, Pro
|
|
|
10
10
|
import { selectSetupHosts } from './prompts.js';
|
|
11
11
|
import { planOpenCodeRegistration, registerOpenCode, registerPi, } from './register.js';
|
|
12
12
|
export const AMBIGUOUS_OPENCODE_MESSAGE = 'Multiple OpenCode generations found. Choose --opencode=1|2|both before running noninteractive setup.';
|
|
13
|
+
const UNAVAILABLE_HOST_MESSAGE = 'Requested setup host is unknown or unavailable. Use --host=opencode or --host=pi with a detected host.';
|
|
13
14
|
function flags(argv) {
|
|
14
15
|
const result = {
|
|
15
16
|
yes: false,
|
|
@@ -17,6 +18,8 @@ function flags(argv) {
|
|
|
17
18
|
claustrum: false,
|
|
18
19
|
noClaustrum: false,
|
|
19
20
|
login: false,
|
|
21
|
+
enrollOnly: false,
|
|
22
|
+
hosts: [],
|
|
20
23
|
opencode: undefined,
|
|
21
24
|
};
|
|
22
25
|
for (const arg of argv) {
|
|
@@ -30,6 +33,14 @@ function flags(argv) {
|
|
|
30
33
|
result.noClaustrum = true;
|
|
31
34
|
else if (arg === '--login')
|
|
32
35
|
result.login = true;
|
|
36
|
+
else if (arg === '--enroll-only')
|
|
37
|
+
result.enrollOnly = true;
|
|
38
|
+
else if (arg.startsWith('--host=')) {
|
|
39
|
+
const host = arg.slice('--host='.length);
|
|
40
|
+
if (host !== 'opencode' && host !== 'pi')
|
|
41
|
+
throw new Error(UNAVAILABLE_HOST_MESSAGE);
|
|
42
|
+
result.hosts.push(host);
|
|
43
|
+
}
|
|
33
44
|
else if (/^--opencode=(1|2|both)$/.test(arg))
|
|
34
45
|
result.opencode = arg.slice('--opencode='.length);
|
|
35
46
|
else
|
|
@@ -39,6 +50,10 @@ function flags(argv) {
|
|
|
39
50
|
throw new Error('--claustrum and --no-claustrum cannot be used together.');
|
|
40
51
|
if (result.login && result.claustrum)
|
|
41
52
|
throw new Error('--login and --claustrum cannot be used together.');
|
|
53
|
+
if (result.enrollOnly && result.login)
|
|
54
|
+
throw new Error('--enroll-only and --login cannot be used together.');
|
|
55
|
+
if (result.enrollOnly && result.noClaustrum)
|
|
56
|
+
throw new Error('--enroll-only and --no-claustrum cannot be used together.');
|
|
42
57
|
return result;
|
|
43
58
|
}
|
|
44
59
|
function validatePlugin(plugin) {
|
|
@@ -96,14 +111,18 @@ export async function runSetup(plugin, argv = [], io = {}) {
|
|
|
96
111
|
if (piWithoutCli)
|
|
97
112
|
printLine(terminal, 'Pi: skipped, because its folder exists but pi is not on PATH. Install the Pi CLI and rerun setup to add it.');
|
|
98
113
|
const found = ['opencode', 'pi'].filter((host) => detection[host].installed && !(host === 'pi' && piWithoutCli));
|
|
114
|
+
if (options.hosts.some((host) => !found.includes(host)))
|
|
115
|
+
throw new Error(UNAVAILABLE_HOST_MESSAGE);
|
|
99
116
|
// A dry run changes nothing, so without a terminal it plans the --yes path
|
|
100
117
|
// instead of refusing; scripts and CI can preview setup with --dry-run alone.
|
|
101
118
|
const assumeYes = options.yes || (options.dryRun && !isInteractive(terminal));
|
|
102
119
|
if (!assumeYes && !isInteractive(terminal))
|
|
103
120
|
throw new Error('Setup requires a TTY or --yes.');
|
|
104
|
-
const hosts =
|
|
105
|
-
? found
|
|
106
|
-
:
|
|
121
|
+
const hosts = options.hosts.length
|
|
122
|
+
? found.filter((host) => options.hosts.includes(host))
|
|
123
|
+
: assumeYes
|
|
124
|
+
? found
|
|
125
|
+
: ((await selectSetupHosts(terminal, found)) ?? []);
|
|
107
126
|
let generation = options.opencode ??
|
|
108
127
|
(detection.opencode.generation === 'ambiguous'
|
|
109
128
|
? '1'
|
|
@@ -160,9 +179,10 @@ export async function runSetup(plugin, argv = [], io = {}) {
|
|
|
160
179
|
}
|
|
161
180
|
else
|
|
162
181
|
await stopped();
|
|
163
|
-
if (options.claustrum
|
|
164
|
-
|
|
165
|
-
|
|
182
|
+
if ((options.claustrum || options.enrollOnly) &&
|
|
183
|
+
!detection.claustrum.available)
|
|
184
|
+
throw new Error('Claustrum is not available. Start ck, or omit vault-only flags and use --no-claustrum for local sign-in.');
|
|
185
|
+
let useVault = options.claustrum || options.enrollOnly;
|
|
166
186
|
if (!useVault &&
|
|
167
187
|
!options.login &&
|
|
168
188
|
!options.noClaustrum &&
|
|
@@ -182,10 +202,20 @@ export async function runSetup(plugin, argv = [], io = {}) {
|
|
|
182
202
|
useVault = choice === true;
|
|
183
203
|
}
|
|
184
204
|
const mode = useVault ? 'claustrum' : 'local';
|
|
185
|
-
// Validate
|
|
186
|
-
const writes = hosts.includes('opencode')
|
|
205
|
+
// Validate configs before changing credentials; enrollment-only needs no registration plan.
|
|
206
|
+
const writes = !options.enrollOnly && hosts.includes('opencode')
|
|
187
207
|
? planOpenCodeRegistration(plugin, generation, io)
|
|
188
208
|
: [];
|
|
209
|
+
for (const write of writes) {
|
|
210
|
+
for (const entry of write.existingLocalEntries ?? [])
|
|
211
|
+
printLine(terminal, `OpenCode: already registered from ${entry} in ${write.path}.`);
|
|
212
|
+
const entries = [
|
|
213
|
+
...(write.existingLocalEntries ?? []),
|
|
214
|
+
...(write.existingNpmEntries ?? []),
|
|
215
|
+
];
|
|
216
|
+
if (entries.length > 1)
|
|
217
|
+
printLine(terminal, `Warning: ${plugin.displayName} may load twice in ${write.path}: ${entries.join(', ')}. Entries were left unchanged.`);
|
|
218
|
+
}
|
|
189
219
|
if (options.dryRun) {
|
|
190
220
|
printLine(terminal, 'Dry run: no registrations, enrollment, sign-in or migration will be performed.');
|
|
191
221
|
for (const host of hosts) {
|
|
@@ -197,15 +227,16 @@ export async function runSetup(plugin, argv = [], io = {}) {
|
|
|
197
227
|
}
|
|
198
228
|
else
|
|
199
229
|
printLine(terminal, `Would sign in locally for ${host}.`);
|
|
200
|
-
if (plugin.beforeRegister)
|
|
230
|
+
if (!options.enrollOnly && plugin.beforeRegister)
|
|
201
231
|
printLine(terminal, `Would run beforeRegister(${host}, ${mode}).`);
|
|
202
232
|
}
|
|
203
233
|
for (const write of writes)
|
|
204
234
|
printLine(terminal, `${write.changed ? 'Would write' : 'Unchanged'} ${write.path}:\n${write.content}`);
|
|
205
|
-
if (hosts.includes('pi'))
|
|
235
|
+
if (!options.enrollOnly && hosts.includes('pi'))
|
|
206
236
|
printLine(terminal, `Would run ${detection.pi.executable} install npm:${plugin.packages.pi}@${plugin.version}`);
|
|
207
|
-
|
|
208
|
-
|
|
237
|
+
if (!options.enrollOnly)
|
|
238
|
+
for (const host of hosts)
|
|
239
|
+
printLine(terminal, `Would migrate ${host} using the plugin callback (provider-specific writes).`);
|
|
209
240
|
}
|
|
210
241
|
else {
|
|
211
242
|
const context = {
|
|
@@ -237,6 +268,8 @@ export async function runSetup(plugin, argv = [], io = {}) {
|
|
|
237
268
|
else
|
|
238
269
|
await plugin.signInLocal(host, context);
|
|
239
270
|
await stopped();
|
|
271
|
+
if (options.enrollOnly)
|
|
272
|
+
continue;
|
|
240
273
|
await plugin.beforeRegister?.(host, mode, context);
|
|
241
274
|
await stopped();
|
|
242
275
|
if (host === 'opencode')
|
|
@@ -265,7 +298,12 @@ export async function runSetup(plugin, argv = [], io = {}) {
|
|
|
265
298
|
throw error;
|
|
266
299
|
}
|
|
267
300
|
}
|
|
268
|
-
printLine(terminal, `${options.dryRun ? 'Planned' : 'Configured'} ${plugin.displayName} for ${hosts.join(', ')} (${mode}).`);
|
|
269
|
-
|
|
301
|
+
printLine(terminal, `${options.dryRun ? 'Planned' : options.enrollOnly ? 'Enrolled' : 'Configured'} ${plugin.displayName} for ${hosts.join(', ')} (${mode}).`);
|
|
302
|
+
if (options.enrollOnly) {
|
|
303
|
+
printLine(terminal, `Enroll-only: no host config or plugin registration ${options.dryRun ? 'will be' : 'was'} written.`);
|
|
304
|
+
printLine(terminal, 'Next: register the plugin yourself in each selected host and enable vault access as required by the plugin.');
|
|
305
|
+
}
|
|
306
|
+
else
|
|
307
|
+
printLine(terminal, `Next: ${hosts.includes('opencode') ? 'start OpenCode' : 'start Pi'} and run ${plugin.slashCommand}.`);
|
|
270
308
|
return { hosts, dryRun: options.dryRun, mode, detection };
|
|
271
309
|
}
|
package/dist/setup/register.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export interface PlannedWrite {
|
|
|
3
3
|
path: string;
|
|
4
4
|
content: string;
|
|
5
5
|
changed: boolean;
|
|
6
|
+
existingLocalEntries?: string[];
|
|
7
|
+
existingNpmEntries?: string[];
|
|
6
8
|
}
|
|
7
9
|
/** Edit only array entries so unrelated JSONC text, including comments, survives. */
|
|
8
10
|
export declare function updatePluginConfig(text: string, key: 'plugin' | 'plugins', pkg: string, version: string): string;
|
package/dist/setup/register.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { domainToUnicode } from 'node:url';
|
|
1
2
|
import { applyEdits, createScanner, findNodeAtLocation, modify, parse, parseTree, } from 'jsonc-parser';
|
|
2
3
|
import { SetupCommandError } from './command-error.js';
|
|
3
4
|
import { defaultCommandRunner, runResolvedCommand } from './command-runner.js';
|
|
4
5
|
import { writeHostConfigAtomic } from './config-write.js';
|
|
5
6
|
import { defaultSetupFileSystem } from './detect.js';
|
|
6
7
|
import { setupPaths } from './paths.js';
|
|
7
|
-
function
|
|
8
|
+
function specifierOf(value) {
|
|
8
9
|
const entry = typeof value === 'string'
|
|
9
10
|
? value
|
|
10
11
|
: Array.isArray(value)
|
|
@@ -12,11 +13,72 @@ function packageOf(value) {
|
|
|
12
13
|
: value && typeof value === 'object' && 'package' in value
|
|
13
14
|
? value.package
|
|
14
15
|
: null;
|
|
15
|
-
|
|
16
|
+
return typeof entry === 'string' ? entry : null;
|
|
17
|
+
}
|
|
18
|
+
function packageOf(value) {
|
|
19
|
+
const entry = specifierOf(value);
|
|
20
|
+
if (entry === null)
|
|
16
21
|
return null;
|
|
17
22
|
const at = entry.indexOf('@', entry.startsWith('@') ? 1 : 0);
|
|
18
23
|
return at < 0 ? entry : entry.slice(0, at);
|
|
19
24
|
}
|
|
25
|
+
function localFileURL(entry, windows) {
|
|
26
|
+
const url = new URL(entry);
|
|
27
|
+
if (/%2f/i.test(url.pathname) || (windows && /%5c/i.test(url.pathname)))
|
|
28
|
+
throw new Error('Encoded path separators are not valid in a file URL.');
|
|
29
|
+
const decoded = decodeURIComponent(url.pathname);
|
|
30
|
+
if (!windows) {
|
|
31
|
+
if (url.hostname)
|
|
32
|
+
throw new Error('A POSIX file URL must be local.');
|
|
33
|
+
return decoded;
|
|
34
|
+
}
|
|
35
|
+
const pathname = decoded.replaceAll('/', '\\');
|
|
36
|
+
if (url.hostname)
|
|
37
|
+
return `\\\\${domainToUnicode(url.hostname)}${pathname}`;
|
|
38
|
+
if (!/^\\[a-z]:/i.test(pathname))
|
|
39
|
+
throw new Error('A Windows file URL must have an absolute drive path.');
|
|
40
|
+
return pathname.slice(1);
|
|
41
|
+
}
|
|
42
|
+
function localPackageOf(entry, configFile, io) {
|
|
43
|
+
const { path, platform } = setupPaths(io);
|
|
44
|
+
try {
|
|
45
|
+
// Relative paths use the declaring config's directory, not setup's working directory:
|
|
46
|
+
// https://github.com/anomalyco/opencode/blob/b6478dce/packages/opencode/src/config/plugin.ts#L40-L60
|
|
47
|
+
// https://github.com/anomalyco/opencode/blob/v2.0.22/packages/core/src/config/plugin/source.ts#L139-L145
|
|
48
|
+
// Decode file URLs explicitly: Bun 1.3 ignores fileURLToPath's Windows override.
|
|
49
|
+
// The requested platform, not the OS running setup's tests, determines path rules.
|
|
50
|
+
const directory = /^file:\/\//i.test(entry)
|
|
51
|
+
? localFileURL(entry, platform === 'win32')
|
|
52
|
+
: path.isAbsolute(entry) || entry.startsWith('.')
|
|
53
|
+
? path.resolve(path.dirname(configFile), entry)
|
|
54
|
+
: null;
|
|
55
|
+
if (directory === null)
|
|
56
|
+
return null;
|
|
57
|
+
const manifest = JSON.parse((io.fs ?? defaultSetupFileSystem).readText(path.join(directory, 'package.json')));
|
|
58
|
+
return manifest &&
|
|
59
|
+
typeof manifest === 'object' &&
|
|
60
|
+
'name' in manifest &&
|
|
61
|
+
typeof manifest.name === 'string'
|
|
62
|
+
? manifest.name
|
|
63
|
+
: null;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// A missing, unreadable or invalid local package is not a registration of this plugin.
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function pluginConfig(text, key) {
|
|
71
|
+
const errors = [];
|
|
72
|
+
const value = parse(text, errors, { allowTrailingComma: true });
|
|
73
|
+
if (errors.length ||
|
|
74
|
+
!value ||
|
|
75
|
+
typeof value !== 'object' ||
|
|
76
|
+
Array.isArray(value))
|
|
77
|
+
throw new Error('Invalid OpenCode config; refusing to overwrite it.');
|
|
78
|
+
if (value[key] !== undefined && !Array.isArray(value[key]))
|
|
79
|
+
throw new Error(`OpenCode ${key} must be an array.`);
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
20
82
|
function removeArrayEntry(text, key, index) {
|
|
21
83
|
const tree = parseTree(text);
|
|
22
84
|
const array = tree && findNodeAtLocation(tree, [key]);
|
|
@@ -48,13 +110,7 @@ function removeArrayEntry(text, key, index) {
|
|
|
48
110
|
}
|
|
49
111
|
/** Edit only array entries so unrelated JSONC text, including comments, survives. */
|
|
50
112
|
export function updatePluginConfig(text, key, pkg, version) {
|
|
51
|
-
const
|
|
52
|
-
const value = parse(text, errors, { allowTrailingComma: true });
|
|
53
|
-
if (errors.length ||
|
|
54
|
-
!value ||
|
|
55
|
-
typeof value !== 'object' ||
|
|
56
|
-
Array.isArray(value))
|
|
57
|
-
throw new Error('Invalid OpenCode config; refusing to overwrite it.');
|
|
113
|
+
const value = pluginConfig(text, key);
|
|
58
114
|
const options = {
|
|
59
115
|
formattingOptions: {
|
|
60
116
|
insertSpaces: !/^\t+/m.test(text),
|
|
@@ -64,8 +120,6 @@ export function updatePluginConfig(text, key, pkg, version) {
|
|
|
64
120
|
};
|
|
65
121
|
let output = text;
|
|
66
122
|
const array = value[key];
|
|
67
|
-
if (array !== undefined && !Array.isArray(array))
|
|
68
|
-
throw new Error(`OpenCode ${key} must be an array.`);
|
|
69
123
|
const entries = Array.isArray(array) ? [...array] : [];
|
|
70
124
|
const matches = entries.flatMap((entry, index) => packageOf(entry) === pkg ? [index] : []);
|
|
71
125
|
const entry = `${pkg}@${version}`;
|
|
@@ -109,6 +163,31 @@ export function planOpenCodeRegistration(plugin, generation, io = {}) {
|
|
|
109
163
|
const before = exists
|
|
110
164
|
? fs.readText(file)
|
|
111
165
|
: `${JSON.stringify({ $schema: `https://opencode.ai/${name === 'tui' ? 'tui' : 'config'}.json` }, null, 2)}\n`;
|
|
166
|
+
const config = pluginConfig(before, key);
|
|
167
|
+
const existingLocalEntries = [];
|
|
168
|
+
const existingNpmEntries = [];
|
|
169
|
+
for (const field of ['plugin', 'plugins']) {
|
|
170
|
+
if (!Array.isArray(config[field]))
|
|
171
|
+
continue;
|
|
172
|
+
for (const entry of config[field]) {
|
|
173
|
+
const spec = specifierOf(entry);
|
|
174
|
+
if (spec === null)
|
|
175
|
+
continue;
|
|
176
|
+
if (localPackageOf(spec, file, io) === plugin.packages.opencode)
|
|
177
|
+
existingLocalEntries.push(JSON.stringify(entry));
|
|
178
|
+
else if (packageOf(entry) === plugin.packages.opencode)
|
|
179
|
+
existingNpmEntries.push(JSON.stringify(entry));
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// A local build is deliberate: do not replace it, deduplicate it or migrate its key.
|
|
183
|
+
if (existingLocalEntries.length > 0)
|
|
184
|
+
return {
|
|
185
|
+
path: file,
|
|
186
|
+
content: before,
|
|
187
|
+
changed: false,
|
|
188
|
+
existingLocalEntries,
|
|
189
|
+
existingNpmEntries,
|
|
190
|
+
};
|
|
112
191
|
const content = updatePluginConfig(before, key, plugin.packages.opencode, plugin.version);
|
|
113
192
|
return { path: file, content, changed: !exists || content !== before };
|
|
114
193
|
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { type StoreContext } from './mutate.js';
|
|
1
2
|
import { type StoreRuntime } from './runtime.js';
|
|
3
|
+
import { type PoolRow } from './schema.js';
|
|
2
4
|
/**
|
|
3
5
|
* What a pull or refresh captured about its row (named by id alongside) when
|
|
4
6
|
* it was issued. A result applies only while the row with that id still has
|
|
@@ -20,6 +22,9 @@ export interface Attribution {
|
|
|
20
22
|
credentialEpoch: number;
|
|
21
23
|
identity?: string;
|
|
22
24
|
}
|
|
25
|
+
export declare function assertQuotaEpoch(id: string, attribution: Attribution): void;
|
|
26
|
+
export declare function assertQuotaRow(id: string, attribution: Attribution, row: PoolRow | undefined): asserts row is PoolRow;
|
|
27
|
+
export declare function mergedQuota(ctx: StoreContext, id: string, stored: unknown, observation: unknown): unknown;
|
|
23
28
|
/**
|
|
24
29
|
* Merges a quota observation into a row's stored map under the store locks,
|
|
25
30
|
* after attribution passes. Needs no row lock, so it is permitted from
|
|
@@ -1,8 +1,41 @@
|
|
|
1
1
|
import { PoolOperationError } from './errors.js';
|
|
2
|
-
import { toFailure, withTransaction } from './mutate.js';
|
|
2
|
+
import { toFailure, withTransaction, } from './mutate.js';
|
|
3
3
|
import { LockStack } from './refresh-lock.js';
|
|
4
4
|
import { refusal, requireBound, unknownRow, } from './runtime.js';
|
|
5
5
|
import { isCredentialEpoch } from './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
|
},
|
package/dist/store/pool.d.ts
CHANGED
|
@@ -16,6 +16,19 @@ export interface OpenPoolStoreOptions {
|
|
|
16
16
|
configPath: string;
|
|
17
17
|
statePath: string;
|
|
18
18
|
quota: QuotaCodec;
|
|
19
|
+
/**
|
|
20
|
+
* Opt in to detached, coalesced quota publication, with any `quota` codec.
|
|
21
|
+
* Each accepted observation is queued whole for its row and binding, and
|
|
22
|
+
* the queue is applied in arrival order through `quota.merge` under the
|
|
23
|
+
* store locks, so the codec's own merge decides which fields a newer or
|
|
24
|
+
* older reading replaces. Accepted readings appear in read/load
|
|
25
|
+
* immediately, merged the same way; the returned promise still rejects
|
|
26
|
+
* current-row refusals before queueing. Await dispose or close at shutdown
|
|
27
|
+
* to drain.
|
|
28
|
+
*/
|
|
29
|
+
coalesceQuota?: boolean;
|
|
30
|
+
/** Bounded quota drain on close/dispose, in milliseconds; defaults to 1,000. */
|
|
31
|
+
quotaDrainTimeoutMs?: number;
|
|
19
32
|
/**
|
|
20
33
|
* The codec of the provider state kept beside each row's credential (see
|
|
21
34
|
* `ProviderStateCodec`). Without it no row shows a provider state, and
|
|
@@ -189,6 +202,10 @@ export interface PoolStore {
|
|
|
189
202
|
}>;
|
|
190
203
|
refresh(id: string, provider: ProviderRefresh, options?: RefreshOptions): Promise<RefreshOutcome>;
|
|
191
204
|
recordQuota(id: string, attribution: Attribution, observation: unknown): Promise<void>;
|
|
205
|
+
/** Drain opted-in quota publication; subsequent coalesced readings refuse. Idempotent. */
|
|
206
|
+
close(): Promise<void>;
|
|
207
|
+
/** Same bounded quota drain as close(). Does not wait for provider pull hooks. */
|
|
208
|
+
dispose(): Promise<void>;
|
|
192
209
|
/** Fires a pull for a row admission refused for want of a reading. */
|
|
193
210
|
requestReading(id: string): void;
|
|
194
211
|
/** Resolves once every pull fired so far has settled. */
|
package/dist/store/pool.js
CHANGED
|
@@ -3,6 +3,7 @@ import { initializePool, readPool, } from './mutate.js';
|
|
|
3
3
|
import { updateProviderStateRow, } from './provider-state.js';
|
|
4
4
|
import { publication, publishRoster, } from './publication.js';
|
|
5
5
|
import { PullScheduler } from './pull.js';
|
|
6
|
+
import { PoolQuotaWriter } from './quota-writer.js';
|
|
6
7
|
import { refreshRow, } from './refresh.js';
|
|
7
8
|
import { POOL_LOCK_DEFAULTS, } from './refresh-lock.js';
|
|
8
9
|
import { addRow, disableRow, enableRow, recordRowIdentity, removeRow, reorderRows, replaceRow, rotateRow, } from './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
|
};
|
package/dist/store/pull.js
CHANGED
|
@@ -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 }
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Attribution } from './attribution.js';
|
|
2
|
+
import { type StoreRuntime } from './runtime.js';
|
|
3
|
+
import { type PoolRow } from './schema.js';
|
|
4
|
+
/**
|
|
5
|
+
* The most readings one row keeps waiting for a flush. Hosts record a reading
|
|
6
|
+
* about every 100 ms at most, so for them a queue this long means another
|
|
7
|
+
* writer has held the store locks for many seconds. Reaching it starts
|
|
8
|
+
* a flush at once instead of waiting for the coalescing timer or a retry
|
|
9
|
+
* backoff; a further reading for the row waits for room (never dropping an
|
|
10
|
+
* older one) for as long as the store's normal lock budget, and is then
|
|
11
|
+
* refused with `lock-contention`, as the immediate writer would refuse it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const POOL_QUOTA_QUEUE_LIMIT = 128;
|
|
14
|
+
export declare class PoolQuotaWriter {
|
|
15
|
+
private readonly rt;
|
|
16
|
+
private readonly timeoutMs;
|
|
17
|
+
private readonly pending;
|
|
18
|
+
private readonly warned;
|
|
19
|
+
private readonly wake;
|
|
20
|
+
private readonly logger;
|
|
21
|
+
private timer?;
|
|
22
|
+
private running?;
|
|
23
|
+
private closing?;
|
|
24
|
+
private closed;
|
|
25
|
+
private stopped;
|
|
26
|
+
private failures;
|
|
27
|
+
constructor(rt: StoreRuntime, timeoutMs?: number);
|
|
28
|
+
record(id: string, attribution: Attribution, observation: unknown): Promise<void>;
|
|
29
|
+
private queued;
|
|
30
|
+
private room;
|
|
31
|
+
private assertOpen;
|
|
32
|
+
private overlayRow;
|
|
33
|
+
overlay(rows: PoolRow[]): PoolRow[];
|
|
34
|
+
private schedule;
|
|
35
|
+
private flushNow;
|
|
36
|
+
private flush;
|
|
37
|
+
private warn;
|
|
38
|
+
close(): Promise<void>;
|
|
39
|
+
private drain;
|
|
40
|
+
}
|