@cortexkit/opencode-openai-auth 1.6.2 → 1.8.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 +9 -9
- package/dist/{codex-request-D_In8uaR.d.ts → codex-request-DohnbsFR.d.ts} +66 -848
- package/dist/index-3v868rnp.js +63 -0
- package/dist/{index-b0ty9qam.js → index-5d4y47h8.js} +2 -2
- package/dist/{index-7nmdsmpp.js → index-5dxpsa3t.js} +2 -2
- package/dist/{index-8xq099qf.js → index-8pk9savd.js} +6 -6
- package/dist/{index-jkpg9d1p.js → index-cbcka7sa.js} +1 -1
- package/dist/{index-79y7w9bz.js → index-cxstwq02.js} +1 -1
- package/dist/{index-x9nffaye.js → index-dtwgf694.js} +1 -1
- package/dist/index-dzaqj248.js +2 -0
- package/dist/index-g3d50jz0.js +74 -0
- package/dist/{index-1qxz4s8a.js → index-n7fxqgfb.js} +2 -2
- package/dist/{index-rf0ht3gc.js → index-th47aqez.js} +2 -2
- package/dist/index-tvban5vp.js +91 -0
- package/dist/{index-ya9c5e4m.js → index-x7jzcvqy.js} +1 -1
- package/dist/index.d.ts +6 -79
- package/dist/index.js +1 -1
- package/dist/{port-file-8ElfwDcq.d.ts → port-file-DQ7-ItDK.d.ts} +1 -1
- package/dist/{model-D22uj0OB.d.ts → protocol-CVQK-d3j.d.ts} +25 -67
- package/dist/rpc/port-file.d.ts +4 -2
- 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 +2 -12
- package/dist/rpc/rpc-client.js +1 -1
- package/dist/rpc/rpc-dir.js +1 -1
- package/dist/rpc-client-BQfIKrXI.d.ts +13 -0
- package/dist/{sidebar-state-Bb9L9oPq.d.ts → sidebar-state-B1cq68UB.d.ts} +5 -7
- package/dist/sidebar-state.d.ts +2 -2
- package/dist/sidebar-state.js +1 -1
- package/dist/{token-xtq3457d.js → token-gy442bba.js} +1 -1
- package/dist/token-util-rxy0p234.js +1 -0
- package/dist/tui-preferences.js +1 -1
- package/dist/tui.d.ts +11 -2
- package/dist/v2/server.d.ts +15 -7
- package/dist/v2/server.js +2 -2
- package/package.json +2 -2
- package/src/tui-compiled/raw/shared/0f77ebc3c914c9c6-custody.js +95 -11
- package/src/tui-compiled/raw/shared/169b2b4bf30909bc-consumer.js +320 -35
- package/src/tui-compiled/raw/shared/30f5bc480f31f51c-roster.js +187 -12
- package/src/tui-compiled/raw/shared/352d228be2832bd4-hub.js +988 -0
- package/src/tui-compiled/raw/shared/458b934e92490331-mutate.js +4 -0
- package/src/tui-compiled/raw/shared/81dddd203f1b92a3-quota-writer.js +34 -12
- package/src/tui-compiled/raw/shared/836fc615de6afbd9-settings.js +16 -6
- package/src/tui-compiled/raw/shared/88b7fc40900fb003-commit-interval.js +18 -0
- package/src/tui-compiled/raw/shared/92e38944b51b2405-file-read-cache.js +57 -0
- package/src/tui-compiled/raw/shared/a670cb7f5987bc1b-shutdown.js +42 -0
- package/src/tui-compiled/raw/shared/b9588ad4725c1d10-enrollment.js +37 -12
- package/src/tui-compiled/raw/shared/c852a15cfe489d12-pool.js +1 -1
- package/src/tui-compiled/raw/shared/c911dbb64c9d85c7-quota-writer.js +27 -13
- package/src/tui-compiled/raw/shared/e8cd4929507508d8-refresh-file-lock.js +20 -11
- package/src/tui-compiled/raw/shared/e8d3c8da776ef9cd-index.js +37 -4
- package/src/tui-compiled/raw/tui/v2.tsx +88 -6
- package/src/tui-compiled/raw/tui.tsx +45 -3
- package/src/tui-compiled/runtime/shared/0f77ebc3c914c9c6-custody.js +95 -11
- package/src/tui-compiled/runtime/shared/169b2b4bf30909bc-consumer.js +320 -35
- package/src/tui-compiled/runtime/shared/30f5bc480f31f51c-roster.js +187 -12
- package/src/tui-compiled/runtime/shared/352d228be2832bd4-hub.js +988 -0
- package/src/tui-compiled/runtime/shared/458b934e92490331-mutate.js +4 -0
- package/src/tui-compiled/runtime/shared/81dddd203f1b92a3-quota-writer.js +34 -12
- package/src/tui-compiled/runtime/shared/836fc615de6afbd9-settings.js +16 -6
- package/src/tui-compiled/runtime/shared/88b7fc40900fb003-commit-interval.js +18 -0
- package/src/tui-compiled/runtime/shared/92e38944b51b2405-file-read-cache.js +57 -0
- package/src/tui-compiled/runtime/shared/a670cb7f5987bc1b-shutdown.js +42 -0
- package/src/tui-compiled/runtime/shared/b9588ad4725c1d10-enrollment.js +37 -12
- package/src/tui-compiled/runtime/shared/c852a15cfe489d12-pool.js +1 -1
- package/src/tui-compiled/runtime/shared/c911dbb64c9d85c7-quota-writer.js +27 -13
- package/src/tui-compiled/runtime/shared/e8cd4929507508d8-refresh-file-lock.js +20 -11
- package/src/tui-compiled/runtime/shared/e8d3c8da776ef9cd-index.js +37 -4
- package/src/tui-compiled/runtime/tui/v2.js +66 -5
- package/src/tui-compiled/runtime/tui.js +32 -3
- package/dist/index-ge60xry3.js +0 -90
- package/dist/index-k5anexvs.js +0 -69
- package/dist/index-xpc2hm9h.js +0 -65
- package/dist/index-z8bptbrk.js +0 -2
- package/dist/protocol-CK9guiFy.d.ts +0 -29
- package/dist/token-util-nyvn9866.js +0 -1
|
@@ -1,16 +1,44 @@
|
|
|
1
1
|
/** @jsxImportSource @opentui/solid */
|
|
2
2
|
import type { Plugin } from "@opencode/plugin/tui"
|
|
3
3
|
import type { JSX } from "solid-js"
|
|
4
|
+
import { createLogger } from "../logger.js"
|
|
4
5
|
import {
|
|
5
6
|
type ApplyRequest,
|
|
6
7
|
type ApplyResult,
|
|
7
8
|
isNotifyPayload,
|
|
8
9
|
type OpenDialogPayload,
|
|
9
10
|
} from "../rpc/protocol.js"
|
|
10
|
-
import { createRpcClient } from "../rpc/rpc-client.js"
|
|
11
|
+
import { createRpcClient, type RpcClient } from "../rpc/rpc-client.js"
|
|
11
12
|
import { resolveRpcDir } from "../rpc/rpc-dir.js"
|
|
12
13
|
import { openControlDialog } from "./v2-dialog.js"
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* How long one `pending` poll may take before the client gives up on it.
|
|
17
|
+
* The shared default is 2 s, which a busy OpenCode server exceeds on every
|
|
18
|
+
* poll, so a queued menu waits for a lucky fast poll. Retrying with a longer
|
|
19
|
+
* budget is safe: the server prunes only notifications at or below the
|
|
20
|
+
* client's cursor (`drainNotifications` in common-auth
|
|
21
|
+
* `src/rpc/notifications.ts`), so a reply the client never read is sent again.
|
|
22
|
+
*/
|
|
23
|
+
export const PENDING_POLL_TIMEOUT_MS = 15_000
|
|
24
|
+
/** Consecutive failed polls before the TUI says the server is slow. */
|
|
25
|
+
export const SLOW_SERVER_POLL_FAILURES = 3
|
|
26
|
+
/** Poll failures log at most one line per minute, no matter how fast polls fail. */
|
|
27
|
+
export const POLL_FAILURE_LOG_INTERVAL_MS = 60_000
|
|
28
|
+
/** Toast shown once per stretch of failed polls, through the host's surface. */
|
|
29
|
+
export const SLOW_SERVER_MESSAGE =
|
|
30
|
+
'Waiting for the OpenAI plugin: the OpenCode server is slow.'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The error's class for poll-failure logs: never its message, which can carry
|
|
34
|
+
* the request URL with the RPC bearer token.
|
|
35
|
+
*/
|
|
36
|
+
export function pollFailureClass(error: unknown): string {
|
|
37
|
+
return error instanceof Error ? error.constructor.name : typeof error
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const log = createLogger('rpc-tui')
|
|
41
|
+
|
|
14
42
|
export interface TuiTransportOptions {
|
|
15
43
|
/** Test-only label for proving that the host can render a sidebar component. */
|
|
16
44
|
sidebarText?: string
|
|
@@ -22,6 +50,14 @@ export interface TuiTransportOptions {
|
|
|
22
50
|
payload: OpenDialogPayload,
|
|
23
51
|
apply: (request: Omit<ApplyRequest, 'sessionId'>) => Promise<ApplyResult>,
|
|
24
52
|
) => Promise<void>
|
|
53
|
+
/**
|
|
54
|
+
* Builds the RPC client the poll drains. Tests inject failures here; a
|
|
55
|
+
* host setup that runs again needs its own RPC client.
|
|
56
|
+
*/
|
|
57
|
+
createRpcClient?: (dir: string, expectedPid?: number) => RpcClient
|
|
58
|
+
/** Timer seam so tests can drive poll cycles without wall-clock waits. */
|
|
59
|
+
setIntervalImpl?: typeof globalThis.setInterval
|
|
60
|
+
clearIntervalImpl?: typeof globalThis.clearInterval
|
|
25
61
|
}
|
|
26
62
|
|
|
27
63
|
const KEY = Symbol.for('cortexkit.openai-auth.v2.tui')
|
|
@@ -45,7 +81,16 @@ export async function setupTui(
|
|
|
45
81
|
let inFlight = false
|
|
46
82
|
let dialogOpen = false
|
|
47
83
|
let directory = ''
|
|
48
|
-
let client:
|
|
84
|
+
let client: RpcClient | undefined
|
|
85
|
+
// Stretches of consecutive failed polls end at the next success, which
|
|
86
|
+
// re-arms both the log line and the slow-server toast.
|
|
87
|
+
let consecutiveFailures = 0
|
|
88
|
+
let lastPollFailureLogAt = 0
|
|
89
|
+
const makeClient =
|
|
90
|
+
options.createRpcClient ??
|
|
91
|
+
((dir: string, expectedPid?: number) => createRpcClient(dir, expectedPid))
|
|
92
|
+
const setIntervalImpl = options.setIntervalImpl ?? setInterval
|
|
93
|
+
const clearIntervalImpl = options.clearIntervalImpl ?? clearInterval
|
|
49
94
|
const unmount =
|
|
50
95
|
options.sidebarText === undefined
|
|
51
96
|
? options.sidebar
|
|
@@ -66,20 +111,56 @@ export async function setupTui(
|
|
|
66
111
|
const sessionId = route.sessionID
|
|
67
112
|
const session = context.data.session.get(sessionId)
|
|
68
113
|
if (!session) return
|
|
114
|
+
// Counts one failed poll: after a few in a row the TUI says through the
|
|
115
|
+
// host's toast that the server is slow instead of staying silent, and
|
|
116
|
+
// logs at most one line a minute with the error class only.
|
|
117
|
+
const notePollFailure = (error: unknown) => {
|
|
118
|
+
consecutiveFailures++
|
|
119
|
+
const now = Date.now()
|
|
120
|
+
if (now - lastPollFailureLogAt >= POLL_FAILURE_LOG_INTERVAL_MS) {
|
|
121
|
+
lastPollFailureLogAt = now
|
|
122
|
+
log.warn('OpenAI plugin poll failed', {
|
|
123
|
+
error: pollFailureClass(error),
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
if (consecutiveFailures === SLOW_SERVER_POLL_FAILURES)
|
|
127
|
+
context.ui.toast.show({
|
|
128
|
+
message: SLOW_SERVER_MESSAGE,
|
|
129
|
+
sessionID: sessionId,
|
|
130
|
+
})
|
|
131
|
+
}
|
|
69
132
|
inFlight = true
|
|
70
133
|
try {
|
|
71
134
|
if (directory !== session.location.directory) {
|
|
72
135
|
directory = session.location.directory
|
|
73
136
|
const resolved = await resolveRpcDir(directory)
|
|
74
137
|
if (disposed) return
|
|
75
|
-
client =
|
|
138
|
+
client = makeClient(resolved.dir, process.pid)
|
|
76
139
|
}
|
|
77
140
|
const rpc = client
|
|
78
141
|
if (!rpc) return
|
|
142
|
+
const pollStartedAt = Date.now()
|
|
79
143
|
const messages = await rpc.pending(
|
|
80
144
|
state.lastIds.get(sessionId) ?? 0,
|
|
81
145
|
sessionId,
|
|
146
|
+
PENDING_POLL_TIMEOUT_MS,
|
|
82
147
|
)
|
|
148
|
+
// A poll counts as failed when it throws, or when it answers empty
|
|
149
|
+
// after taking at least its own timeout: a socket timeout resolves to
|
|
150
|
+
// no messages instead of throwing, so elapsed time is the only signal
|
|
151
|
+
// a slow server leaves. A fast empty poll is a healthy idle server.
|
|
152
|
+
if (
|
|
153
|
+
messages.length === 0 &&
|
|
154
|
+
Date.now() - pollStartedAt >= PENDING_POLL_TIMEOUT_MS
|
|
155
|
+
) {
|
|
156
|
+
notePollFailure(
|
|
157
|
+
new Error(
|
|
158
|
+
`RPC pending poll timed out after ${PENDING_POLL_TIMEOUT_MS} ms`,
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
consecutiveFailures = 0
|
|
83
164
|
if (
|
|
84
165
|
disposed ||
|
|
85
166
|
context.ui.router.current().type !== 'session' ||
|
|
@@ -107,20 +188,21 @@ export async function setupTui(
|
|
|
107
188
|
})
|
|
108
189
|
}
|
|
109
190
|
}
|
|
110
|
-
} catch {
|
|
191
|
+
} catch (error) {
|
|
111
192
|
// The server can be absent while a location starts or is reloaded.
|
|
193
|
+
notePollFailure(error)
|
|
112
194
|
} finally {
|
|
113
195
|
inFlight = false
|
|
114
196
|
}
|
|
115
197
|
}
|
|
116
|
-
const timer =
|
|
198
|
+
const timer = setIntervalImpl(() => {
|
|
117
199
|
void poll()
|
|
118
200
|
}, 500)
|
|
119
201
|
const mount: Mount = {
|
|
120
202
|
dispose() {
|
|
121
203
|
if (disposed) return
|
|
122
204
|
disposed = true
|
|
123
|
-
|
|
205
|
+
clearIntervalImpl(timer)
|
|
124
206
|
unmount?.()
|
|
125
207
|
options.sidebar?.dispose()
|
|
126
208
|
if (dialogOpen) context.ui.dialog.clear()
|
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
} from "solid-js"
|
|
27
27
|
import { createLogger } from "./logger.js"
|
|
28
28
|
import { type ApplyRequest, isNotifyPayload } from "./rpc/protocol.js"
|
|
29
|
-
import { createRpcClient } from "./rpc/rpc-client.js"
|
|
29
|
+
import { createRpcClient, DEFAULT_RPC_TIMEOUT_MS } from "./rpc/rpc-client.js"
|
|
30
30
|
import { resolveRpcDir } from "./rpc/rpc-dir.js"
|
|
31
31
|
import {
|
|
32
32
|
type AccountQuota,
|
|
@@ -51,7 +51,14 @@ import {
|
|
|
51
51
|
sidebarQuotaProjection,
|
|
52
52
|
} from "./sidebar-state.js"
|
|
53
53
|
import { openCommandDialog } from "./tui/command-dialogs.tsx"
|
|
54
|
-
import {
|
|
54
|
+
import {
|
|
55
|
+
POLL_FAILURE_LOG_INTERVAL_MS,
|
|
56
|
+
pollFailureClass,
|
|
57
|
+
SLOW_SERVER_MESSAGE,
|
|
58
|
+
SLOW_SERVER_POLL_FAILURES,
|
|
59
|
+
setupTui,
|
|
60
|
+
type TuiTransportOptions,
|
|
61
|
+
} from "./tui/v2.tsx"
|
|
55
62
|
import {
|
|
56
63
|
type AppearancePrefs,
|
|
57
64
|
computeEffectiveOrder,
|
|
@@ -1337,6 +1344,25 @@ const tui: TuiPlugin = async (api) => {
|
|
|
1337
1344
|
})
|
|
1338
1345
|
let lastNotificationId = 0
|
|
1339
1346
|
let rpcInFlight = false
|
|
1347
|
+
// A stretch of consecutive failed polls ends at the next success, which
|
|
1348
|
+
// re-arms the slow-server toast.
|
|
1349
|
+
let rpcFailures = 0
|
|
1350
|
+
let lastRpcFailureLogAt = 0
|
|
1351
|
+
// Counts one failed poll: a throw, or an empty answer after taking at
|
|
1352
|
+
// least the poll's own timeout (a socket timeout resolves to no messages
|
|
1353
|
+
// instead of throwing, so elapsed time is the slow server's only signal).
|
|
1354
|
+
const noteRpcPollFailure = (error: unknown) => {
|
|
1355
|
+
rpcFailures++
|
|
1356
|
+
const now = Date.now()
|
|
1357
|
+
if (now - lastRpcFailureLogAt >= POLL_FAILURE_LOG_INTERVAL_MS) {
|
|
1358
|
+
lastRpcFailureLogAt = now
|
|
1359
|
+
log.warn('OpenAI plugin poll failed', {
|
|
1360
|
+
error: pollFailureClass(error),
|
|
1361
|
+
})
|
|
1362
|
+
}
|
|
1363
|
+
if (rpcFailures === SLOW_SERVER_POLL_FAILURES)
|
|
1364
|
+
api.ui.toast({ message: SLOW_SERVER_MESSAGE })
|
|
1365
|
+
}
|
|
1340
1366
|
setInterval(() => {
|
|
1341
1367
|
if (rpcInFlight) return
|
|
1342
1368
|
const current = (api.route as { current?: unknown }).current
|
|
@@ -1346,9 +1372,22 @@ const tui: TuiPlugin = async (api) => {
|
|
|
1346
1372
|
resolved as { params?: { sessionID?: string } } | undefined
|
|
1347
1373
|
)?.params?.sessionID
|
|
1348
1374
|
rpcInFlight = true
|
|
1375
|
+
const pollStartedAt = Date.now()
|
|
1349
1376
|
void rpcClient
|
|
1350
1377
|
.pending(lastNotificationId, sessionId)
|
|
1351
1378
|
.then((messages) => {
|
|
1379
|
+
if (
|
|
1380
|
+
messages.length === 0 &&
|
|
1381
|
+
Date.now() - pollStartedAt >= DEFAULT_RPC_TIMEOUT_MS
|
|
1382
|
+
) {
|
|
1383
|
+
noteRpcPollFailure(
|
|
1384
|
+
new Error(
|
|
1385
|
+
`RPC pending poll timed out after ${DEFAULT_RPC_TIMEOUT_MS} ms`,
|
|
1386
|
+
),
|
|
1387
|
+
)
|
|
1388
|
+
return
|
|
1389
|
+
}
|
|
1390
|
+
rpcFailures = 0
|
|
1352
1391
|
for (const message of [...messages].sort((a, b) => a.id - b.id)) {
|
|
1353
1392
|
lastNotificationId = Math.max(lastNotificationId, message.id)
|
|
1354
1393
|
const payload = message.payload
|
|
@@ -1364,7 +1403,10 @@ const tui: TuiPlugin = async (api) => {
|
|
|
1364
1403
|
)
|
|
1365
1404
|
}
|
|
1366
1405
|
})
|
|
1367
|
-
.catch(() => {
|
|
1406
|
+
.catch((error) => {
|
|
1407
|
+
// The server can be absent while a location starts or is reloaded.
|
|
1408
|
+
noteRpcPollFailure(error)
|
|
1409
|
+
})
|
|
1368
1410
|
.finally(() => {
|
|
1369
1411
|
rpcInFlight = false
|
|
1370
1412
|
})
|
|
@@ -4,6 +4,7 @@ import { ClaustrumUnreachableError, classifyVaultFailure, } from "./94f53cfd572b
|
|
|
4
4
|
import { readClaustrumEnrollmentToken } from "./b9588ad4725c1d10-enrollment.js";
|
|
5
5
|
import { ClaustrumConsumerError } from "./ca0fccaba1d87af4-errors.js";
|
|
6
6
|
import { CUSTODY_PLACEHOLDER_PREFIX } from "./d9d2892a4c371301-host-slot.js";
|
|
7
|
+
import { drainShutdown, shutdownWarning } from "./a670cb7f5987bc1b-shutdown.js";
|
|
7
8
|
import { ScopedRosterWatch, } from "./dc9290c2a7552ba6-watch.js";
|
|
8
9
|
/** Only a new version of the same account may replace an in-flight 401. */
|
|
9
10
|
export function isScopedCredentialRotation(served, current) {
|
|
@@ -79,15 +80,56 @@ function accessTokenFromMaterial(material) {
|
|
|
79
80
|
return access;
|
|
80
81
|
}
|
|
81
82
|
function credentialTypeOf(row, family) {
|
|
83
|
+
const notMine = { kind: 'not-mine' };
|
|
82
84
|
if (!row.operations.includes('read'))
|
|
83
|
-
return
|
|
85
|
+
return notMine;
|
|
84
86
|
if (!row.categories.includes(family.category))
|
|
85
|
-
return
|
|
87
|
+
return notMine;
|
|
88
|
+
if (row.credentialType !== 'oauth' && row.credentialType !== 'api_key')
|
|
89
|
+
return notMine;
|
|
90
|
+
if ('classify' in family) {
|
|
91
|
+
// Older clients omit these facts entirely; do not call a policy that needs them.
|
|
92
|
+
const facts = {
|
|
93
|
+
credentialId: row.id,
|
|
94
|
+
credentialType: row.credentialType,
|
|
95
|
+
categories: row.categories,
|
|
96
|
+
refreshAdapter: row.refreshAdapter,
|
|
97
|
+
...rowFactsOf(row),
|
|
98
|
+
};
|
|
99
|
+
if (facts.providerIds === undefined)
|
|
100
|
+
return { kind: 'refused', code: 'unmapped' };
|
|
101
|
+
try {
|
|
102
|
+
const result = family.classify(facts);
|
|
103
|
+
if (result.kind === 'not-mine')
|
|
104
|
+
return notMine;
|
|
105
|
+
if (result.kind === 'mine' && typeof result.member === 'string')
|
|
106
|
+
return { kind: 'mine', type: row.credentialType, member: result.member };
|
|
107
|
+
if (result.kind === 'refused' &&
|
|
108
|
+
typeof result.code === 'string' &&
|
|
109
|
+
/^[a-z][a-z0-9-]{0,31}$/.test(result.code) &&
|
|
110
|
+
!result.code.includes('\n') &&
|
|
111
|
+
family.refusalCodes.includes(result.code))
|
|
112
|
+
return { kind: 'refused', code: result.code };
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// A classifier error may contain vault data; expose only a fixed code.
|
|
116
|
+
}
|
|
117
|
+
return { kind: 'refused', code: 'unclassified' };
|
|
118
|
+
}
|
|
86
119
|
if (row.credentialType === 'oauth')
|
|
87
|
-
return row.refreshAdapter === family.refreshAdapter
|
|
120
|
+
return row.refreshAdapter === family.refreshAdapter
|
|
121
|
+
? { kind: 'mine', type: 'oauth' }
|
|
122
|
+
: notMine;
|
|
88
123
|
if (row.credentialType === 'api_key' && family.apiKeys)
|
|
89
|
-
return row.refreshAdapter === undefined
|
|
90
|
-
|
|
124
|
+
return row.refreshAdapter === undefined
|
|
125
|
+
? { kind: 'mine', type: 'api_key' }
|
|
126
|
+
: notMine;
|
|
127
|
+
return notMine;
|
|
128
|
+
}
|
|
129
|
+
/** Structural access keeps the public facts independent of client declarations. */
|
|
130
|
+
function rowFactsOf(row) {
|
|
131
|
+
const metadata = row;
|
|
132
|
+
return { providerIds: metadata.providerIds, authMethod: metadata.authMethod };
|
|
91
133
|
}
|
|
92
134
|
/**
|
|
93
135
|
* The grants named in the vault's list reply, or undefined when it names none.
|
|
@@ -136,6 +178,9 @@ export class ClaustrumScopedCustody {
|
|
|
136
178
|
/** The view of the last successful `discover()`, for `watchRoster`. */
|
|
137
179
|
#discoveredView;
|
|
138
180
|
#watches = new Set();
|
|
181
|
+
#tokenReads = new Set();
|
|
182
|
+
#warnShutdown;
|
|
183
|
+
#disposing;
|
|
139
184
|
constructor(options) {
|
|
140
185
|
const tokenPath = options.tokenPath;
|
|
141
186
|
if (options.readToken) {
|
|
@@ -153,6 +198,7 @@ export class ClaustrumScopedCustody {
|
|
|
153
198
|
this.#requireAssertion = options.requireAssertion ?? false;
|
|
154
199
|
this.#now = options.now ?? Date.now;
|
|
155
200
|
this.#logger = options.logger ?? defaultLogger;
|
|
201
|
+
this.#warnShutdown = shutdownWarning(this.#logger);
|
|
156
202
|
this.#setTimeout = options.setTimeoutImpl ?? setTimeout;
|
|
157
203
|
this.#clearTimeout = options.clearTimeoutImpl ?? clearTimeout;
|
|
158
204
|
}
|
|
@@ -220,7 +266,15 @@ export class ClaustrumScopedCustody {
|
|
|
220
266
|
}
|
|
221
267
|
async #token(signal) {
|
|
222
268
|
this.#check(signal);
|
|
223
|
-
const
|
|
269
|
+
const pending = this.#readToken();
|
|
270
|
+
this.#tokenReads.add(pending);
|
|
271
|
+
let value;
|
|
272
|
+
try {
|
|
273
|
+
value = await pending;
|
|
274
|
+
}
|
|
275
|
+
finally {
|
|
276
|
+
this.#tokenReads.delete(pending);
|
|
277
|
+
}
|
|
224
278
|
this.#check(signal);
|
|
225
279
|
if (!/^[0-9a-f]{64}$/.test(value.token) ||
|
|
226
280
|
!Number.isSafeInteger(value.token_generation) ||
|
|
@@ -299,17 +353,32 @@ export class ClaustrumScopedCustody {
|
|
|
299
353
|
throw error;
|
|
300
354
|
}
|
|
301
355
|
const skipped = [];
|
|
356
|
+
const refused = [];
|
|
357
|
+
let notMine = 0;
|
|
302
358
|
const candidates = [];
|
|
303
359
|
const counts = new Map();
|
|
304
360
|
for (const row of inventory.rows) {
|
|
305
|
-
const
|
|
306
|
-
if (
|
|
361
|
+
const classified = credentialTypeOf(row, this.#family);
|
|
362
|
+
if (classified.kind === 'not-mine') {
|
|
363
|
+
notMine++;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (classified.kind === 'refused') {
|
|
367
|
+
refused.push({
|
|
368
|
+
...(row.id.trim() && { credentialId: row.id }),
|
|
369
|
+
code: classified.code,
|
|
370
|
+
});
|
|
307
371
|
continue;
|
|
308
|
-
|
|
372
|
+
}
|
|
373
|
+
candidates.push({
|
|
374
|
+
row,
|
|
375
|
+
type: classified.type,
|
|
376
|
+
...(classified.member !== undefined && { member: classified.member }),
|
|
377
|
+
});
|
|
309
378
|
counts.set(row.id, (counts.get(row.id) ?? 0) + 1);
|
|
310
379
|
}
|
|
311
380
|
const credentials = [];
|
|
312
|
-
for (const { row, type } of candidates) {
|
|
381
|
+
for (const { row, type, member } of candidates) {
|
|
313
382
|
let reason;
|
|
314
383
|
if (!row.id.trim())
|
|
315
384
|
reason = 'empty credential id';
|
|
@@ -320,12 +389,17 @@ export class ClaustrumScopedCustody {
|
|
|
320
389
|
else if (!row.state.trim())
|
|
321
390
|
reason = 'empty state';
|
|
322
391
|
if (reason) {
|
|
323
|
-
skipped.push({
|
|
392
|
+
skipped.push({
|
|
393
|
+
...(row.id.trim() && { credentialId: row.id }),
|
|
394
|
+
...(member !== undefined && { member }),
|
|
395
|
+
reason,
|
|
396
|
+
});
|
|
324
397
|
continue;
|
|
325
398
|
}
|
|
326
399
|
credentials.push(Object.freeze({
|
|
327
400
|
credentialId: row.id,
|
|
328
401
|
credentialType: type,
|
|
402
|
+
...(member !== undefined && { member }),
|
|
329
403
|
...(row.accountId !== undefined && {
|
|
330
404
|
accountIdentity: row.accountId,
|
|
331
405
|
}),
|
|
@@ -341,6 +415,10 @@ export class ClaustrumScopedCustody {
|
|
|
341
415
|
view: inventory.view,
|
|
342
416
|
credentials: Object.freeze(credentials),
|
|
343
417
|
skipped: Object.freeze(skipped),
|
|
418
|
+
...('classify' in this.#family && {
|
|
419
|
+
refused: Object.freeze(refused),
|
|
420
|
+
notMine,
|
|
421
|
+
}),
|
|
344
422
|
...(grantReach && { grantReach }),
|
|
345
423
|
};
|
|
346
424
|
}
|
|
@@ -646,4 +724,10 @@ export class ClaustrumScopedCustody {
|
|
|
646
724
|
this.#kept.clear();
|
|
647
725
|
this.#client.close();
|
|
648
726
|
}
|
|
727
|
+
/** Stop immediately; allow token reads 2 s and share an owner's timeout warning. */
|
|
728
|
+
async dispose(onTimeout = this.#warnShutdown) {
|
|
729
|
+
this.close();
|
|
730
|
+
this.#disposing ??= drainShutdown(this.#tokenReads, onTimeout);
|
|
731
|
+
await this.#disposing;
|
|
732
|
+
}
|
|
649
733
|
}
|