@cortexkit/common-auth 0.39.0 → 0.39.2
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/fs/refresh-file-lock.d.ts +6 -0
- package/dist/fs/refresh-file-lock.js +47 -3
- package/dist/opencode2/install.js +42 -9
- package/dist/opencode2/types.d.ts +3 -1
- package/dist/routing-accessor/index.js +2 -1
- package/dist/tui-build/publish-list.d.ts +6 -0
- package/dist/tui-build/publish-list.js +13 -1
- package/package.json +1 -1
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
type LeaseCreationTestHooks = {
|
|
2
|
+
beforeWrite?: (path: string) => void | Promise<void>;
|
|
3
|
+
beforeLink?: (tempPath: string, lockPath: string) => void | Promise<void>;
|
|
4
|
+
};
|
|
5
|
+
/** Test-only creation faults; not re-exported from the public fs entry point. */
|
|
6
|
+
export declare function setLeaseCreationTestHooksForTest(hooks?: LeaseCreationTestHooks): void;
|
|
1
7
|
export declare function isLostMarkerRaceError(error: unknown): boolean;
|
|
2
8
|
/** Test-only fault injection between marker mkdir and owner.json write. */
|
|
3
9
|
export interface EvictionMarkerTestHooks {
|
|
@@ -1,8 +1,50 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { lstat, mkdir, open, readFile, rename, rm, stat, } from 'node:fs/promises';
|
|
2
|
+
import { link, lstat, mkdir, open, readFile, rename, rm, stat, } from 'node:fs/promises';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
4
|
import { createPrivateBytes, createPrivateDirectoryExclusive, ensurePrivateDirectory, isWindowsSharingViolation, PrivateFileError, privateOperation, readPrivateText, verifyPrivate, windowsPrivateFiles, writePrivateAtomic, } from './private-files.js';
|
|
5
5
|
import { LockOwnershipError, lockPathFor } from './with-lock.js';
|
|
6
|
+
let leaseCreationTestHooks;
|
|
7
|
+
/** Test-only creation faults; not re-exported from the public fs entry point. */
|
|
8
|
+
export function setLeaseCreationTestHooksForTest(hooks) {
|
|
9
|
+
leaseCreationTestHooks = hooks;
|
|
10
|
+
}
|
|
11
|
+
async function createExclusiveText(path, text) {
|
|
12
|
+
// Publish only a complete owner record. A crash before link leaves at most
|
|
13
|
+
// an orphaned staging file, never an ownerless lease at the lock path.
|
|
14
|
+
const tempPath = `${path}.${randomUUID()}.tmp`;
|
|
15
|
+
let handle;
|
|
16
|
+
try {
|
|
17
|
+
handle = await open(tempPath, 'wx', 0o600);
|
|
18
|
+
try {
|
|
19
|
+
if (leaseCreationTestHooks?.beforeWrite)
|
|
20
|
+
await leaseCreationTestHooks.beforeWrite(tempPath);
|
|
21
|
+
await handle.writeFile(text, 'utf8');
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
await handle.close();
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
if (leaseCreationTestHooks?.beforeLink)
|
|
28
|
+
await leaseCreationTestHooks.beforeLink(tempPath, path);
|
|
29
|
+
await link(tempPath, path);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
const code = error.code;
|
|
33
|
+
if (code !== 'EPERM' &&
|
|
34
|
+
code !== 'ENOTSUP' &&
|
|
35
|
+
code !== 'EOPNOTSUPP' &&
|
|
36
|
+
code !== 'EXDEV')
|
|
37
|
+
throw error;
|
|
38
|
+
// Filesystems without hard links still use exclusive create then write.
|
|
39
|
+
// A crash can leave an incomplete lease there, but locking stays available.
|
|
40
|
+
await writeExclusiveText(path, text);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
if (handle)
|
|
45
|
+
await rm(tempPath, { force: true }).catch(() => { });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
6
48
|
/**
|
|
7
49
|
* Create a new owner-only file and write `text` into it, failing with EEXIST if
|
|
8
50
|
* the file already exists. Writing through the handle rather than path-based
|
|
@@ -11,7 +53,7 @@ import { LockOwnershipError, lockPathFor } from './with-lock.js';
|
|
|
11
53
|
* that redundant ftruncate stalls for hundreds of milliseconds, inside every
|
|
12
54
|
* lock acquisition.
|
|
13
55
|
*/
|
|
14
|
-
async function
|
|
56
|
+
async function writeExclusiveText(path, text) {
|
|
15
57
|
const handle = await open(path, 'wx', 0o600);
|
|
16
58
|
let createdStat;
|
|
17
59
|
try {
|
|
@@ -21,6 +63,8 @@ async function createExclusiveText(path, text) {
|
|
|
21
63
|
createdStat = undefined;
|
|
22
64
|
}
|
|
23
65
|
try {
|
|
66
|
+
if (leaseCreationTestHooks?.beforeWrite)
|
|
67
|
+
await leaseCreationTestHooks.beforeWrite(path);
|
|
24
68
|
await handle.writeFile(text, 'utf8');
|
|
25
69
|
}
|
|
26
70
|
catch (error) {
|
|
@@ -491,7 +535,7 @@ export async function acquireRefreshFileLockWithClock(options) {
|
|
|
491
535
|
if (windowsPrivateFiles)
|
|
492
536
|
await createPrivateBytes(evictOwnerPath, bytes);
|
|
493
537
|
else
|
|
494
|
-
await
|
|
538
|
+
await writeExclusiveText(evictOwnerPath, bytes);
|
|
495
539
|
}
|
|
496
540
|
catch (error) {
|
|
497
541
|
// A competing contender can rename our just-created marker directory
|
|
@@ -290,6 +290,27 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
290
290
|
}
|
|
291
291
|
}));
|
|
292
292
|
};
|
|
293
|
+
const emitRetry = async (payload) => {
|
|
294
|
+
const transports = new Set([...attempts.values()]
|
|
295
|
+
.filter((rec) => rec.scope.sessionID === payload.sessionID)
|
|
296
|
+
.map((rec) => rec.transport)
|
|
297
|
+
.filter((transport) => transport !== undefined));
|
|
298
|
+
const transport = payload.handle?.transport ??
|
|
299
|
+
(transports.size === 1 ? [...transports][0] : 'unknown');
|
|
300
|
+
try {
|
|
301
|
+
logger?.info?.('opencode2 auth retry decision', {
|
|
302
|
+
sessionID: payload.sessionID,
|
|
303
|
+
transport,
|
|
304
|
+
attempt: payload.attempt,
|
|
305
|
+
reason: payload.reason === 'host-decides' && mixed(payload.sessionID)
|
|
306
|
+
? 'mixed-session'
|
|
307
|
+
: payload.reason,
|
|
308
|
+
retry: payload.decision.retry,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
catch { }
|
|
312
|
+
await emit('retry', payload);
|
|
313
|
+
};
|
|
293
314
|
const deliverEnd = async (attempt, outcome) => {
|
|
294
315
|
try {
|
|
295
316
|
await adapter.onAttemptEnd?.(attempt, outcome);
|
|
@@ -805,7 +826,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
805
826
|
}
|
|
806
827
|
response = new Response(trackBody(body, (error) => {
|
|
807
828
|
if (error?.reason === 'failed')
|
|
808
|
-
rec.
|
|
829
|
+
rec.readFailed = true;
|
|
809
830
|
finish(rec, error);
|
|
810
831
|
}), {
|
|
811
832
|
status: original.status,
|
|
@@ -915,7 +936,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
915
936
|
if (ownership && latestTransportOwned.get(draft.sessionID) !== true)
|
|
916
937
|
return;
|
|
917
938
|
const hostDecision = draft.decision;
|
|
918
|
-
const hostDecides = () =>
|
|
939
|
+
const hostDecides = () => emitRetry({
|
|
919
940
|
sessionID: draft.sessionID,
|
|
920
941
|
attempt: draft.attempt,
|
|
921
942
|
reason: 'host-decides',
|
|
@@ -932,7 +953,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
932
953
|
if (picked === 'ambiguous') {
|
|
933
954
|
// Nothing tells which open attempt failed: keep the host's
|
|
934
955
|
// decision and leave every attempt as it is.
|
|
935
|
-
await
|
|
956
|
+
await emitRetry({
|
|
936
957
|
sessionID: draft.sessionID,
|
|
937
958
|
attempt: draft.attempt,
|
|
938
959
|
reason: 'host-decides',
|
|
@@ -948,8 +969,18 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
948
969
|
const retrySameAccount = adapter.retrySameAccount?.bind(adapter);
|
|
949
970
|
const handle = rec.attempt;
|
|
950
971
|
const afterOutput = rec.outputStarted;
|
|
972
|
+
if (rec.transport === 'ws' &&
|
|
973
|
+
afterOutput &&
|
|
974
|
+
!rec.ended &&
|
|
975
|
+
!rec.limit &&
|
|
976
|
+
!(rec.status !== undefined && rec.status >= 400) &&
|
|
977
|
+
!(draft.error.status !== undefined && draft.error.status >= 400) &&
|
|
978
|
+
draft.error.type === 'provider.transport') {
|
|
979
|
+
rec.readFailed = true;
|
|
980
|
+
finish(rec, { reason: 'failed', message: draft.error.message });
|
|
981
|
+
}
|
|
951
982
|
if (afterOutput &&
|
|
952
|
-
rec.
|
|
983
|
+
rec.readFailed &&
|
|
953
984
|
retrySameAccount &&
|
|
954
985
|
handle &&
|
|
955
986
|
!mixed(draft.sessionID)) {
|
|
@@ -964,9 +995,11 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
964
995
|
await rec.limit.delivered;
|
|
965
996
|
checkRetryActive();
|
|
966
997
|
}
|
|
967
|
-
// The host drops read-operation metadata from its retry error.
|
|
968
|
-
// our
|
|
969
|
-
|
|
998
|
+
// The host drops read-operation metadata from its retry error. HTTP
|
|
999
|
+
// needs our body reader's failure; WebSocket needs inspected output
|
|
1000
|
+
// without a terminal frame, since the host only sends before reading.
|
|
1001
|
+
// Ending the socket attempt above also excludes any late frames.
|
|
1002
|
+
const canContinue = () => rec.readFailed === true &&
|
|
970
1003
|
draft.error.type === 'provider.transport' &&
|
|
971
1004
|
hostDecision.retry &&
|
|
972
1005
|
hostRetryLimit - draft.attempt >= 1 &&
|
|
@@ -1072,7 +1105,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1072
1105
|
});
|
|
1073
1106
|
checkRetryActive();
|
|
1074
1107
|
if (retried) {
|
|
1075
|
-
await
|
|
1108
|
+
await emitRetry({
|
|
1076
1109
|
sessionID: draft.sessionID,
|
|
1077
1110
|
accountId: rec.accountId,
|
|
1078
1111
|
kind: rec.scope.kind,
|
|
@@ -1327,7 +1360,7 @@ export async function installOpenCode2Auth(ctx, adapter, options = {}) {
|
|
|
1327
1360
|
return { decision, reason };
|
|
1328
1361
|
});
|
|
1329
1362
|
checkRetryActive();
|
|
1330
|
-
await
|
|
1363
|
+
await emitRetry({
|
|
1331
1364
|
sessionID: draft.sessionID,
|
|
1332
1365
|
...(rec.accountId === undefined
|
|
1333
1366
|
? {}
|
|
@@ -487,6 +487,8 @@ export interface OpenCode2AuthAdapter<Q = unknown, A = unknown, Choice extends C
|
|
|
487
487
|
}
|
|
488
488
|
export interface OpenCode2AuthLogger {
|
|
489
489
|
warn(message: string, data?: unknown): void;
|
|
490
|
+
/** Retry decisions, without account details or provider error contents. */
|
|
491
|
+
info?(message: string, data?: unknown): void;
|
|
490
492
|
}
|
|
491
493
|
export interface InstallOpenCode2AuthOptions {
|
|
492
494
|
/**
|
|
@@ -541,7 +543,7 @@ export interface InstallOpenCode2AuthOptions {
|
|
|
541
543
|
* - `reroute`: refused for a limit before any output, and `chooseAccount`
|
|
542
544
|
* named another account for the next attempt, so the host retries at once;
|
|
543
545
|
* - `output-started`: output reached the user without a safe continuation, so no retry;
|
|
544
|
-
* - `continue-same-account`: a witnessed
|
|
546
|
+
* - `continue-same-account`: a witnessed transport read failure after output, with
|
|
545
547
|
* adapter-approved identity preservation; keeps the host's continuation and delay;
|
|
546
548
|
* - `no-account`: the attempt had no account, so no retry;
|
|
547
549
|
* - `host-decides-last-account`: refused for a limit before any output, but
|
|
@@ -38,7 +38,8 @@ export function turnNotes(sessionId) {
|
|
|
38
38
|
if (typeof afterToolUseId !== 'string' ||
|
|
39
39
|
afterToolUseId.length === 0 ||
|
|
40
40
|
typeof text !== 'string' ||
|
|
41
|
-
|
|
41
|
+
// A whitespace-only note says nothing and a provider may refuse it.
|
|
42
|
+
text.trim().length === 0 ||
|
|
42
43
|
encoder.encode(text).byteLength > 16_384)
|
|
43
44
|
continue;
|
|
44
45
|
notes.push({ afterToolUseId, text });
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package records `npm pack --json` printed. Up to npm 11 it prints an
|
|
3
|
+
* array of records; from npm 12 an object keyed by package name. The records
|
|
4
|
+
* themselves (`filename`, `files[].path`) have the same fields in both.
|
|
5
|
+
*/
|
|
6
|
+
export declare function packedRecords<T>(parsed: unknown): T[];
|
|
1
7
|
/** Compare the files npm actually publishes, not the manifest's glob patterns. */
|
|
2
8
|
export declare function assertEmittedPublishList(packageRoot: string, destinationDir: string, emitted: readonly string[]): Promise<void>;
|
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import { execFile } from 'node:child_process';
|
|
2
2
|
import { relative, resolve } from 'node:path';
|
|
3
3
|
import { promisify } from 'node:util';
|
|
4
|
+
/**
|
|
5
|
+
* The package records `npm pack --json` printed. Up to npm 11 it prints an
|
|
6
|
+
* array of records; from npm 12 an object keyed by package name. The records
|
|
7
|
+
* themselves (`filename`, `files[].path`) have the same fields in both.
|
|
8
|
+
*/
|
|
9
|
+
export function packedRecords(parsed) {
|
|
10
|
+
if (Array.isArray(parsed))
|
|
11
|
+
return parsed;
|
|
12
|
+
if (parsed !== null && typeof parsed === 'object')
|
|
13
|
+
return Object.values(parsed);
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
4
16
|
/** Compare the files npm actually publishes, not the manifest's glob patterns. */
|
|
5
17
|
export async function assertEmittedPublishList(packageRoot, destinationDir, emitted) {
|
|
6
18
|
const { stdout } = await promisify(execFile)('npm', ['pack', '--json', '--dry-run', '--ignore-scripts'], { cwd: packageRoot });
|
|
7
|
-
const packed = JSON.parse(stdout);
|
|
19
|
+
const packed = packedRecords(JSON.parse(stdout));
|
|
8
20
|
const prefix = `${relative(resolve(packageRoot), resolve(destinationDir)).split('\\').join('/')}/`;
|
|
9
21
|
const packageInfo = packed[0];
|
|
10
22
|
if (!packageInfo)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/common-auth",
|
|
3
|
-
"version": "0.39.
|
|
3
|
+
"version": "0.39.2",
|
|
4
4
|
"description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|