@cortexkit/common-auth 0.14.0 → 0.14.1
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/pi-slot/lock.d.ts +1 -0
- package/dist/pi-slot/lock.js +38 -5
- package/dist/pi-slot/slot.js +3 -0
- package/package.json +2 -1
package/dist/pi-slot/lock.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** The same mkdir/mtime/rmdir protocol as Pi's proper-lockfile, with realpath disabled. */
|
|
2
2
|
export declare function acquirePiLock(authPath: string, onCompromised: () => void, onRenew?: (renew: () => Promise<void>) => void): Promise<{
|
|
3
3
|
release(): Promise<void>;
|
|
4
|
+
assertFresh(): void;
|
|
4
5
|
}>;
|
package/dist/pi-slot/lock.js
CHANGED
|
@@ -2,13 +2,25 @@ import { rmdirSync, statSync } from 'node:fs';
|
|
|
2
2
|
import { mkdir, rmdir, stat, utimes } from 'node:fs/promises';
|
|
3
3
|
// Protocol source: proper-lockfile 4.1.2 lib/lockfile.js and lib/mtime-precision.js;
|
|
4
4
|
// line-level citations and the Pi lock options are recorded in docs/sources.md.
|
|
5
|
+
// Pi's async acquireLockAsync uses stale=30s (update=15s). Keep takeover at 30s
|
|
6
|
+
// so a live async writer's lock cannot be reclaimed between its renewals.
|
|
5
7
|
const STALE = 30_000;
|
|
6
|
-
|
|
8
|
+
// Pi's sync acquireLockSyncWithRetry uses proper-lockfile defaults: stale=10s,
|
|
9
|
+
// update=5s. Renew every 3s for margin and stop retrying failed renewals after 10s.
|
|
10
|
+
const RENEWAL_STALE = 10_000;
|
|
11
|
+
const UPDATE = 3_000;
|
|
7
12
|
let precision;
|
|
13
|
+
/** Held locks, with the time each last renewed, for cleanup at process exit. */
|
|
8
14
|
const active = new Map();
|
|
9
15
|
process.once('exit', () => {
|
|
10
16
|
for (const [path, identity] of active) {
|
|
11
17
|
try {
|
|
18
|
+
// Pi's synchronous auth.json writer treats a lock whose mtime has not
|
|
19
|
+
// been refreshed for 10s as abandoned and may take it over. A lease that
|
|
20
|
+
// went 10s without renewing may therefore already be that writer's, so
|
|
21
|
+
// it is left in place; whoever next needs the lock clears it as stale.
|
|
22
|
+
if (identity.renewedAt + RENEWAL_STALE < Date.now())
|
|
23
|
+
continue;
|
|
12
24
|
const observed = statSync(path);
|
|
13
25
|
if (sameDirectory(observed, identity) &&
|
|
14
26
|
observed.mtime.getTime() === identity.mtime)
|
|
@@ -86,7 +98,7 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
86
98
|
let lastUpdate = Date.now();
|
|
87
99
|
let released = false;
|
|
88
100
|
let timer;
|
|
89
|
-
active.set(path, identity);
|
|
101
|
+
active.set(path, { ...identity, renewedAt: lastUpdate });
|
|
90
102
|
function compromise() {
|
|
91
103
|
released = true;
|
|
92
104
|
if (timer)
|
|
@@ -94,6 +106,12 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
94
106
|
active.delete(path);
|
|
95
107
|
onCompromised();
|
|
96
108
|
}
|
|
109
|
+
function assertFresh() {
|
|
110
|
+
// A sync writer may already have reclaimed an unrenewed lock after 10s,
|
|
111
|
+
// even if the directory still matches. Never revive that expired lease.
|
|
112
|
+
if (!released && lastUpdate + RENEWAL_STALE < Date.now())
|
|
113
|
+
compromise();
|
|
114
|
+
}
|
|
97
115
|
function schedule(delay = UPDATE) {
|
|
98
116
|
if (released)
|
|
99
117
|
return;
|
|
@@ -105,12 +123,14 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
105
123
|
function retry(error) {
|
|
106
124
|
if (released)
|
|
107
125
|
return;
|
|
108
|
-
if (errorCode(error) === 'ENOENT' ||
|
|
126
|
+
if (errorCode(error) === 'ENOENT' ||
|
|
127
|
+
lastUpdate + RENEWAL_STALE < Date.now())
|
|
109
128
|
compromise();
|
|
110
129
|
else
|
|
111
130
|
schedule(1_000);
|
|
112
131
|
}
|
|
113
132
|
async function update() {
|
|
133
|
+
assertFresh();
|
|
114
134
|
if (released)
|
|
115
135
|
return;
|
|
116
136
|
let observed;
|
|
@@ -121,6 +141,7 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
121
141
|
retry(error);
|
|
122
142
|
return;
|
|
123
143
|
}
|
|
144
|
+
assertFresh();
|
|
124
145
|
if (released)
|
|
125
146
|
return;
|
|
126
147
|
if (!sameDirectory(observed, identity) ||
|
|
@@ -139,8 +160,8 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
139
160
|
if (released)
|
|
140
161
|
return;
|
|
141
162
|
mtime = next.getTime();
|
|
142
|
-
active.set(path, { ...identity, mtime });
|
|
143
163
|
lastUpdate = Date.now();
|
|
164
|
+
active.set(path, { ...identity, mtime, renewedAt: lastUpdate });
|
|
144
165
|
schedule();
|
|
145
166
|
}
|
|
146
167
|
// Tests can resume a paused renewal without waiting for the production timer.
|
|
@@ -151,6 +172,7 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
151
172
|
});
|
|
152
173
|
schedule();
|
|
153
174
|
return {
|
|
175
|
+
assertFresh,
|
|
154
176
|
async release() {
|
|
155
177
|
if (released)
|
|
156
178
|
return;
|
|
@@ -158,6 +180,16 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
158
180
|
if (timer)
|
|
159
181
|
clearTimeout(timer);
|
|
160
182
|
active.delete(path);
|
|
183
|
+
// An expired lease is never removed, even when the directory still
|
|
184
|
+
// matches. Pi's synchronous auth.json writer may take over a lock that
|
|
185
|
+
// went 10s without an mtime refresh, so after 10s without renewing the
|
|
186
|
+
// directory may be that writer's. Whoever next needs the lock clears it
|
|
187
|
+
// as stale.
|
|
188
|
+
const expired = () => lastUpdate + RENEWAL_STALE < Date.now();
|
|
189
|
+
if (expired()) {
|
|
190
|
+
compromise();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
161
193
|
let observed;
|
|
162
194
|
try {
|
|
163
195
|
observed = await stat(path);
|
|
@@ -168,7 +200,8 @@ export async function acquirePiLock(authPath, onCompromised, onRenew) {
|
|
|
168
200
|
compromise();
|
|
169
201
|
return;
|
|
170
202
|
}
|
|
171
|
-
if (
|
|
203
|
+
if (expired() ||
|
|
204
|
+
!sameDirectory(observed, identity) ||
|
|
172
205
|
mtime !== observed.mtime.getTime()) {
|
|
173
206
|
compromise();
|
|
174
207
|
return;
|
package/dist/pi-slot/slot.js
CHANGED
|
@@ -266,8 +266,10 @@ export function createPiSlot(options, onStep, hooks) {
|
|
|
266
266
|
}
|
|
267
267
|
async function locked(fn) {
|
|
268
268
|
let release;
|
|
269
|
+
let assertFresh;
|
|
269
270
|
let compromised = false;
|
|
270
271
|
const assertLock = () => {
|
|
272
|
+
assertFresh?.();
|
|
271
273
|
if (compromised)
|
|
272
274
|
throw new PiSlotError('lock-compromised');
|
|
273
275
|
};
|
|
@@ -281,6 +283,7 @@ export function createPiSlot(options, onStep, hooks) {
|
|
|
281
283
|
compromised = true;
|
|
282
284
|
}, hooks?.onRenew);
|
|
283
285
|
release = lease.release;
|
|
286
|
+
assertFresh = lease.assertFresh;
|
|
284
287
|
}
|
|
285
288
|
catch (error) {
|
|
286
289
|
if (code(error) !== 'ELOCKED')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cortexkit/common-auth",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.1",
|
|
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": {
|
|
@@ -140,6 +140,7 @@
|
|
|
140
140
|
"bun-types": "1.4.2",
|
|
141
141
|
"inline-default": "workspace:*",
|
|
142
142
|
"lefthook": "2.1.14",
|
|
143
|
+
"pi-coding-agent-086": "npm:@earendil-works/pi-coding-agent@0.86.1",
|
|
143
144
|
"proper-lockfile": "4.1.2",
|
|
144
145
|
"solid-js": "1.9.12",
|
|
145
146
|
"typescript": "7.0.2"
|