@cortexkit/opencode-openai-auth 1.17.0 → 1.17.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.
Files changed (40) hide show
  1. package/dist/bin.js +2 -2
  2. package/dist/{index-fxqghceg.js → index-2dqwctzp.js} +1 -1
  3. package/dist/{index-dqxk628t.js → index-4thkhwyp.js} +2 -2
  4. package/dist/{index-d7bsrp0m.js → index-6pakkcrn.js} +1 -1
  5. package/dist/index-7ea4dbe9.js +8 -0
  6. package/dist/{index-vmcmpezd.js → index-b2pc3ze2.js} +1 -1
  7. package/dist/{index-k2f9vrcr.js → index-emcf633z.js} +1 -1
  8. package/dist/{index-v9p38mg5.js → index-h8ychf80.js} +2 -2
  9. package/dist/{index-rm4zt5dv.js → index-jmzh060w.js} +24 -24
  10. package/dist/{index-xx2z6bs3.js → index-q7d43xxn.js} +2 -2
  11. package/dist/{index-3dv16qvb.js → index-qjh3h3wr.js} +11 -11
  12. package/dist/{index-99c2fepe.js → index-srdm5a4d.js} +1 -1
  13. package/dist/{index-1m3vv9rh.js → index-t09qaj5w.js} +2 -2
  14. package/dist/{index-6ts9w66n.js → index-vjk4wv2b.js} +1 -1
  15. package/dist/{index-0mhdyzax.js → index-y1b9ka54.js} +1 -1
  16. package/dist/index.js +1 -1
  17. package/dist/rpc/port-file.js +1 -1
  18. package/dist/rpc/protocol.js +1 -1
  19. package/dist/rpc/rpc-client.js +1 -1
  20. package/dist/rpc/rpc-dir.js +1 -1
  21. package/dist/sidebar-state.js +1 -1
  22. package/dist/{token-r3vbz9pw.js → token-6ya278eg.js} +1 -1
  23. package/dist/token-util-ynanp4np.js +1 -0
  24. package/dist/tui-preferences.js +1 -1
  25. package/dist/v2/server.js +1 -1
  26. package/package.json +2 -2
  27. package/src/tui-compiled/raw/shared/0766d78c6f481f18-watcher.js +9 -2
  28. package/src/tui-compiled/raw/shared/44449e891fab9fbc-edit.js +48 -2
  29. package/src/tui-compiled/raw/shared/5f4bc45522ab9bc6-update.js +19 -18
  30. package/src/tui-compiled/raw/shared/6948d70969aa7a55-fs.js +1 -0
  31. package/src/tui-compiled/raw/shared/cf4c37f3975c3a8d-canonical-file-path.js +53 -0
  32. package/src/tui-compiled/raw/shared/ec971142548df90e-index.js +3 -0
  33. package/src/tui-compiled/runtime/shared/0766d78c6f481f18-watcher.js +9 -2
  34. package/src/tui-compiled/runtime/shared/44449e891fab9fbc-edit.js +48 -2
  35. package/src/tui-compiled/runtime/shared/5f4bc45522ab9bc6-update.js +19 -18
  36. package/src/tui-compiled/runtime/shared/6948d70969aa7a55-fs.js +1 -0
  37. package/src/tui-compiled/runtime/shared/cf4c37f3975c3a8d-canonical-file-path.js +53 -0
  38. package/src/tui-compiled/runtime/shared/ec971142548df90e-index.js +3 -0
  39. package/dist/index-ydtpn6ma.js +0 -8
  40. package/dist/token-util-vks3kgse.js +0 -1
@@ -37,7 +37,12 @@ function descriptorAtPath(descriptor, path) {
37
37
  }
38
38
  return current;
39
39
  }
40
- function destination(declaration, setting, value, { host, model }) {
40
+ function destination(declaration, setting, value, { host, model }, level) {
41
+ if (level !== undefined) {
42
+ return level === 'default' && !isJsonObject(value)
43
+ ? [setting]
44
+ : [setting, level];
45
+ }
41
46
  if (declaration.overrides !== undefined && isJsonObject(value)) {
42
47
  if (declaration.overrides.model &&
43
48
  model !== undefined &&
@@ -75,6 +80,20 @@ function checkPath(parsed, path) {
75
80
  invalid();
76
81
  }
77
82
  }
83
+ function preservePlainDefault(parsed, setting) {
84
+ const node = parsed.tree && findNodeAtLocation(parsed.tree, [setting]);
85
+ if (!node)
86
+ invalid();
87
+ // Keep the original value text so comments inside a list survive promotion.
88
+ const value = parsed.text.slice(node.offset, node.offset + node.length);
89
+ return parseForEdit(applyEdits(parsed.text, [
90
+ {
91
+ offset: node.offset,
92
+ length: node.length,
93
+ content: `{ "default": ${value} }`,
94
+ },
95
+ ]));
96
+ }
78
97
  /** Remove only the member's own tokens; intervening comment lines belong to neither neighbor. */
79
98
  function removeMember(text, tree, path) {
80
99
  const property = tree && findNodeAtLocation(tree, path)?.parent;
@@ -157,6 +176,18 @@ export function applySettingsEdits(definition, source, edits, context) {
157
176
  if (!Object.hasOwn(definition.settings, edit.setting))
158
177
  invalid();
159
178
  const declaration = definition.settings[edit.setting];
179
+ if (edit.level !== undefined) {
180
+ if (edit.path !== undefined)
181
+ invalid();
182
+ if (edit.level === 'default') {
183
+ if (declaration.overrides === undefined)
184
+ invalid();
185
+ }
186
+ else if ((edit.level !== 'opencode' && edit.level !== 'pi') ||
187
+ !declaration.overrides?.host) {
188
+ invalid();
189
+ }
190
+ }
160
191
  if (edit.path !== undefined &&
161
192
  declaration.value.kind !== 'object' &&
162
193
  declaration.value.kind !== 'record')
@@ -170,9 +201,24 @@ export function applySettingsEdits(definition, source, edits, context) {
170
201
  const units = [];
171
202
  for (const edit of edits) {
172
203
  const declaration = definition.settings[edit.setting];
204
+ if (edit.level !== undefined &&
205
+ edit.level !== 'default' &&
206
+ !isJsonObject(working.value[edit.setting])) {
207
+ if (edit.remove === true)
208
+ continue;
209
+ if (Object.hasOwn(working.value, edit.setting)) {
210
+ working = preservePlainDefault(working, edit.setting);
211
+ }
212
+ // Earlier root edits now refer to the override's default, if it exists.
213
+ for (const unit of units) {
214
+ if (unit.path.length === 1 && unit.path[0] === edit.setting) {
215
+ unit.path.push('default');
216
+ }
217
+ }
218
+ }
173
219
  const base = edit.path !== undefined
174
220
  ? [edit.setting]
175
- : destination(declaration, edit.setting, working.value[edit.setting], context);
221
+ : destination(declaration, edit.setting, working.value[edit.setting], context, edit.level);
176
222
  const path = [...base, ...(edit.path ?? [])];
177
223
  checkPath(working, path);
178
224
  units.push(resolutionUnit(declaration.value, base, edit.path ?? []));
@@ -1,6 +1,8 @@
1
- import { basename, dirname, join } from "node:path";
1
+ import { dirname } from "node:path";
2
+ import { canonicalFilePath } from "./cf4c37f3975c3a8d-canonical-file-path.js";
2
3
  import { ensurePrivateDirectory } from "./375d9cca2def037d-private-files.js";
3
4
  import { withLock } from "./4825e03209d6ae3e-with-lock.js";
5
+ import { deepFreeze } from "./5803fadb30af0ec0-descriptors.js";
4
6
  import { applySettingsEdits, } from "./44449e891fab9fbc-edit.js";
5
7
  import { SettingsFs } from "./6948d70969aa7a55-fs.js";
6
8
  import { rethrowSettingsLockError } from "./eac0bed49f3d4110-lock-errors.js";
@@ -18,25 +20,9 @@ function unavailable(file, cause) {
18
20
  }
19
21
  async function writeTarget(file) {
20
22
  try {
21
- return await SettingsFs.realpath(file);
23
+ return await canonicalFilePath(file, SettingsFs);
22
24
  }
23
25
  catch (error) {
24
- if (error.code !== 'ENOENT')
25
- unavailable(file, error);
26
- try {
27
- await SettingsFs.lstat(file);
28
- }
29
- catch (lstatError) {
30
- if (lstatError.code !== 'ENOENT')
31
- unavailable(file, lstatError);
32
- try {
33
- return join(await SettingsFs.realpath(dirname(file)), basename(file));
34
- }
35
- catch (parentError) {
36
- unavailable(file, parentError);
37
- }
38
- }
39
- // A directory entry whose realpath is missing is a dangling symlink.
40
26
  unavailable(file, error);
41
27
  }
42
28
  }
@@ -97,6 +83,21 @@ export async function updateSettings(definition, options, edits, context) {
97
83
  if (Number.isInteger(version) && version > 1) {
98
84
  throw new SettingsRefusal('settings-newer-version', 'Settings were written by a newer version');
99
85
  }
86
+ if (typeof edits === 'function') {
87
+ const planned = edits(deepFreeze(missing
88
+ ? { status: 'missing' }
89
+ : {
90
+ status: 'ready',
91
+ document: structuredClone(parsed.value),
92
+ }));
93
+ if (planned !== null &&
94
+ (typeof planned === 'object' || typeof planned === 'function') &&
95
+ 'then' in planned &&
96
+ typeof planned.then === 'function') {
97
+ throw new SettingsRefusal('settings-invalid-value', 'Settings update plans must be synchronous');
98
+ }
99
+ edits = planned;
100
+ }
100
101
  if (!parsed.tree) {
101
102
  source = `${parsed.hasBom ? '\uFEFF' : ''}// ${definition.plugin} settings\n{\n "schema_version": 1\n}\n`;
102
103
  }
@@ -3,6 +3,7 @@ const defaults = {
3
3
  readFile: fs.readFile,
4
4
  stat: fs.stat,
5
5
  lstat: fs.lstat,
6
+ readlink: fs.readlink,
6
7
  realpath: fs.realpath,
7
8
  open: fs.open,
8
9
  rename: fs.rename,
@@ -0,0 +1,53 @@
1
+ import { basename, dirname, join, resolve } from "node:path";
2
+ const maxSymlinkHops = 40;
3
+ /**
4
+ * Canonical path of a file that other processes may replace by write-and-rename.
5
+ *
6
+ * On Linux, Bun's realpath on such a file can answer "<file> (deleted)" (Bun
7
+ * 1.3) or ENOENT (Bun 1.4) during the rename. So this resolves the folder with
8
+ * realpath and appends the name, then follows a file symlink one readlink hop at
9
+ * a time, at most 40 hops. A missing file resolves to its would-be path; a
10
+ * dangling link or a loop throws. Pass `node:fs/promises` (or a test double).
11
+ */
12
+ export async function canonicalFilePath(file, fs) {
13
+ let target = file;
14
+ for (let hops = 0;; hops++) {
15
+ // Bun can report an unlinked inode's " (deleted)" name for realpath(file).
16
+ target = join(await fs.realpath(dirname(target)), basename(target));
17
+ let isSymlink;
18
+ try {
19
+ isSymlink = (await fs.lstat(target)).isSymbolicLink();
20
+ }
21
+ catch (error) {
22
+ if (error.code === 'ENOENT' && hops === 0)
23
+ return target;
24
+ throw error;
25
+ }
26
+ if (!isSymlink)
27
+ return target;
28
+ if (hops >= maxSymlinkHops)
29
+ throw new Error('Too many file symlink hops');
30
+ target = resolve(dirname(target), await fs.readlink(target));
31
+ }
32
+ }
33
+ /** Synchronous counterpart of canonicalFilePath; pass `node:fs`. */
34
+ export function canonicalFilePathSync(file, fs) {
35
+ let target = file;
36
+ for (let hops = 0;; hops++) {
37
+ target = join(fs.realpathSync(dirname(target)), basename(target));
38
+ let isSymlink;
39
+ try {
40
+ isSymlink = fs.lstatSync(target).isSymbolicLink();
41
+ }
42
+ catch (error) {
43
+ if (error.code === 'ENOENT' && hops === 0)
44
+ return target;
45
+ throw error;
46
+ }
47
+ if (!isSymlink)
48
+ return target;
49
+ if (hops >= maxSymlinkHops)
50
+ throw new Error('Too many file symlink hops');
51
+ target = resolve(dirname(target), fs.readlinkSync(target));
52
+ }
53
+ }
@@ -1,4 +1,7 @@
1
1
  export { writeJsonAtomic } from "./7211cfec06a298bc-atomic-write.js";
2
+ // Canonical path of a file other processes may atomically replace: resolves
3
+ // the folder, never the file itself (see canonical-file-path.ts).
4
+ export { canonicalFilePath, canonicalFilePathSync, } from "./cf4c37f3975c3a8d-canonical-file-path.js";
2
5
  export { WRITER_LOCK_CONSTANTS } from "./9b4da614c45f9cd0-lock-constants.js";
3
6
  export { PrivateFileError } from "./375d9cca2def037d-private-files.js";
4
7
  // The try-once lock underneath withLock, for callers that hold a lock across
@@ -1,8 +0,0 @@
1
- import{Jg as m0}from"./index-1m3vv9rh.js";import{Kg as X,Lg as R,Mg as B0,Ng as u0,Pg as w,Qg as X0,Rg as P,Sg as N0,Tg as g0,Vg as H0}from"./index-xx2z6bs3.js";import{randomUUID as o}from"node:crypto";import{link as zz,lstat as u,mkdir as O0,open as K0,readdir as Bz,readFile as f0,rename as b0,rm as c,stat as t,unlink as Gz}from"node:fs/promises";import{basename as Hz,dirname as G0,join as U0}from"node:path";import{resolve as M0}from"node:path";class i extends Error{path;code="ENOENT";constructor(z,B){super(`Lock record disappeared: ${z}`,{cause:B});this.path=z}}function m(z,B){return z instanceof R&&z.reason==="io-error"&&z.win32Code===5&&(z.stage==="read"||z.stage==="verify")&&B.some((K)=>M0(K)===z.path)}function p(z,B,K){return z instanceof R&&z.reason==="io-error"&&z.win32Code===5&&z.stage===K&&B.some((Y)=>M0(Y)===z.path)}async function L(z,B){try{return await B()}catch(K){if(!m(K,[z]))throw K;try{return await B()}catch(Y){if(Y.code==="ENOENT")throw new i(z,K);throw K}}}import{join as o0,resolve as r0}from"node:path";function J0(z,B){return`${z}.${B}.lock`}class L0 extends Error{details;constructor(z){super(`Timed out acquiring ${z.name} lock for ${z.target}`);this.name="LockContentionError",this.details=z}}class a extends Error{details;constructor(z){super(`Lost ${z.name} lock for ${z.target}`);this.name="LockOwnershipError",this.details=z}}function e0(z,B,K){let Y=r0(J0(B,K)),J=[Y,o0(Y,"owner.json")];return(B0(z)||m(z,J))&&(z.stage==="read"||z.stage==="verify")&&J.includes(z.path)||X&&(p(z,[Y],"create")||p(z,[`${Y}.evicting`],"directory-create"))}async function Pz(z,B,K){let Y=performance.now(),J;for(;;){B.signal?.throwIfAborted();let _;try{_=await R0({path:z,name:B.name,ttlMs:B.ttlMs,renew:B.renew??!1,...B.onStep?{onStep:B.onStep}:{},...B.onRenewalTimer?{onRenewalTimer:B.onRenewalTimer}:{}}),J=void 0}catch(N){if(!e0(N,z,B.name))throw N;_=null,J??=N}if(_)try{return B.signal?.throwIfAborted(),await K(_)}finally{await _.release(B.signal)}let V=B.timeoutMs-(performance.now()-Y);if(V<=0){if(J)throw J;throw new L0({target:z,name:B.name,timeoutMs:B.timeoutMs})}await new Promise((N)=>setTimeout(N,Math.min(25,Math.ceil(V))))}}var l;async function Kz(z,B){let K=`${z}.${o()}.tmp`,Y;try{Y=await K0(K,"wx",384);try{if(l?.beforeWrite)await l.beforeWrite(K);await Y.writeFile(B,"utf8")}finally{await Y.close()}try{if(l?.beforeLink)await l.beforeLink(K,z);await zz(K,z)}catch(J){let _=J.code;if(_!=="EPERM"&&_!=="ENOTSUP"&&_!=="EOPNOTSUPP"&&_!=="EXDEV")throw J;await w0(z,B)}}finally{if(Y)await c(K,{force:!0}).catch(()=>{})}}async function w0(z,B){let K=await K0(z,"wx",384),Y;try{Y=await K.stat()}catch{Y=void 0}try{if(l?.beforeWrite)await l.beforeWrite(z);await K.writeFile(B,"utf8")}catch(J){try{let _=await u(z).catch(()=>{return});if(Y&&_&&_.dev===Y.dev&&_.ino===Y.ino)await c(z,{force:!0}).catch(()=>{})}catch{}throw J}finally{await K.close()}}var v0=/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,Qz=600000,Yz=256,Zz=4096,E0=new Set,y0=new Set,$z;function Jz(z,B){let K=`${B}.`,Y=".tmp";if(!z.startsWith(K)||!z.endsWith(".tmp"))return!1;return v0.test(z.slice(K.length,-4))}function h0(z,B){return z.dev===B.dev&&z.ino===B.ino&&z.size===B.size&&z.mtimeMs===B.mtimeMs&&z.birthtimeMs===B.birthtimeMs}async function k0(z){let B=await u(z);if(!B.isFile()||B.isSymbolicLink())return;if(B.size>Zz)return;let K=await K0(z,"r"),Y;try{if(!h0(B,await K.stat()))return;Y=await K.readFile("utf8")}finally{await K.close()}let J;try{J=JSON.parse(Y)}catch{return}if(!J||typeof J!=="object"||Array.isArray(J))return;let _=Object.keys(J).sort();if(_.length!==2||_[0]!=="expiresAt"||_[1]!=="ownerId")return;let{ownerId:V,expiresAt:N}=J;if(typeof V!=="string"||!v0.test(V))return;if(typeof N!=="number"||!Number.isFinite(N))return;return{identity:B,expiresAt:N}}async function _z(z,B,K){let Y=G0(z),J=Hz(z),_=$z,N=(await(_?.readdir??Bz)(Y)).filter((O)=>Jz(O,J)).slice(0,Yz),W=Math.max(Number.isFinite(B)?B:0,Qz);for(let O of N){let f=U0(Y,O);try{if(K()-(await u(f)).mtimeMs<W)continue;let y=await k0(f);if(!y||!(y.expiresAt<K()-W))continue;if(_?.beforeUnlink)await _.beforeUnlink(f);let S=await k0(f);if(!S||!h0(y.identity,S.identity)||!(S.expiresAt<K()-W))continue;await(_?.unlink??Gz)(f)}catch{}}}function Xz(z,B,K){if(E0.has(z))return;E0.add(z);let Y=Promise.resolve().then(()=>_z(z,B,K)).catch(()=>{});y0.add(Y),Y.finally(()=>y0.delete(Y))}var Nz=globalThis.setTimeout.bind(globalThis),Uz=globalThis.clearTimeout.bind(globalThis);function Vz(z){let B=z?.code;return B==="ENOENT"||!X&&(B==="EINVAL"||B==="ENOTDIR")}var P0;function _0(z,B){if(z.dev!==B.dev||z.ino!==B.ino)return!1;if(z.ino===0&&z.birthtimeMs!==B.birthtimeMs)return!1;return!0}var Wz=1000;function R0(z){return xz({...z,now:Date.now})}async function xz(z){let B=J0(z.path,z.name),K=U0(B,"owner.json"),Y=o(),J=z.now??Date.now,_=null,V=!1,N=!1,W,O,f=new Promise((G)=>{O=G});function y(G){try{let H=z.onRenewalTimer?.(G);if(H&&(typeof H==="object"||typeof H==="function")&&"then"in H&&typeof H.then==="function")Promise.resolve(H).catch(()=>{})}catch{}}function S(){if(!_)return;Uz(_),_=null,y("cancelled")}function q(G,H){if(W||V)return;W=Object.freeze({reason:G,expectedOwnerId:Y,...typeof H?.ownerId==="string"?{observedOwnerId:H.ownerId}:{},...typeof H?.expiresAt==="number"?{observedExpiresAt:H.expiresAt}:{}}),S(),O(W)}let b,E=null,U=`${B}.evicting`,x=U0(U,"owner.json"),d=o(),g=5000,s=8;function v(G){return X&&(p(G,[B],"create")||p(G,[U],"directory-create"))}async function F(){if(X)try{return JSON.parse(await L(B,()=>P(B,!0)))}catch(G){if(G.code!=="EISDIR"&&G.reason!=="wrong-kind")throw G;return await L(B,()=>w(B,"directory",!0)),JSON.parse(await L(K,()=>P(K,!0)))}try{return JSON.parse(await f0(B,"utf8"))}catch(G){if(G.code!=="EISDIR")throw G;return JSON.parse(await f0(K,"utf8"))}}async function r(){if(X){let Z=await F(),$=Number(Z?.expiresAt)-J();await H0(B,`${JSON.stringify({ownerId:Y,expiresAt:J()+z.ttlMs})}
2
- `,{deadline:performance.now()+Math.max(0,$),beforeRename:async()=>{let j=await F();if(j?.ownerId!==Y||!(Number(j?.expiresAt)>J())||!await M())throw new a({target:z.path,name:z.name})}});return}let G=`${B}.${o()}.tmp`,H,Q=!1;try{H=await K0(G,"wx",384),Q=!0,await H.writeFile(`${JSON.stringify({ownerId:Y,expiresAt:J()+z.ttlMs})}
3
- `,"utf8"),await H.chmod(384),await H.close(),H=void 0;let Z=await F();if(Z?.ownerId!==Y||!(Number(Z?.expiresAt)>J())||!await M())throw new a({target:z.path,name:z.name});await b0(G,B),Q=!1}finally{if(await H?.close().catch(()=>{}),Q)await c(G,{force:!0}).catch(()=>{})}}async function h(){let G=`${JSON.stringify({ownerId:Y,expiresAt:J()+z.ttlMs})}
4
- `;if(X)await N0(B,G);else await Kz(B,G)}async function I(G){let H=G.code;if(H!=="EEXIST"&&H!=="EISDIR")return!1;if(X){if(z.onStep)await z.onStep("lease-contention-read-ready");try{if(H==="EISDIR")await L(B,()=>w(B,"directory",!0)),await L(K,()=>P(K,!0));else await F()}catch(Q){if(Q.code!=="ENOENT")throw Q}}return!0}async function A(){if(X)await X0(G0(B));try{return await h(),!0}catch(G){let H=G.code;if(await I(G))return!1;if(H==="ENOENT"){if(X)await X0(G0(B));else await O0(G0(B),{recursive:!0});try{return await h(),!0}catch(Q){if(await I(Q))return!1;throw Q}}throw G}}async function C(){await new Promise((G)=>setTimeout(G,Math.floor(Math.random()*4)))}async function T(G=!0){if(G&&z.onStep)await z.onStep("lock-read-ready");let H;try{H=await F()}catch(Z){if(Z instanceof i)return{status:"missing"};if(X&&Z.code!=="ENOENT")throw Z}let Q=Number(H?.expiresAt);if(Number.isFinite(Q)&&Q>J())return{status:"live"};if(typeof H?.ownerId==="string"&&Number.isFinite(Q))return{status:"stale",identity:{kind:"owner",ownerId:H.ownerId,expiresAt:Q}};try{let Z=await t(B),$=Number.isFinite(Q)?Q:Z.mtimeMs+z.ttlMs;if($>J())return{status:"live"};return{status:"stale",identity:{kind:"stat",dev:Z.dev,ino:Z.ino,mtimeMs:Z.mtimeMs,expiresAt:$}}}catch(Z){if(Z.code==="ENOENT"){if(z.onStep)await z.onStep("lock-missing");return{status:"missing"}}throw Z}}function D0(G,H){if(H.status!=="stale")return!1;let Q=H.identity;if(G.kind==="owner"&&Q.kind==="owner")return G.ownerId===Q.ownerId&&G.expiresAt===Q.expiresAt;return G.kind==="stat"&&Q.kind==="stat"&&G.dev===Q.dev&&G.ino===Q.ino&&G.mtimeMs===Q.mtimeMs&&G.expiresAt===Q.expiresAt}async function M(){try{return JSON.parse(await L(x,()=>P(x,X)))?.ownerId===d}catch(G){if(X&&G.code!=="ENOENT")throw G;return!1}}async function Y0(G){if(X)await u0(G,"remove",()=>c(G,{recursive:!0,force:!0}));else await c(G,{recursive:!0,force:!0}).catch(()=>{})}function e(G,H){if(!X||!(G instanceof R)||G.stage!=="remove"||G.path!==H)return!1;return B0(G)||G.win32Code===void 0&&G.reason===void 0&&(G.code==="EBUSY"||G.code==="EPERM")}async function Z0(G,H,Q=performance.now()+g+1000){let Z,$=5;while(await H()){try{await Y0(G);return}catch(D){if(!e(D,G))throw D;Z??=D}let j=Q-performance.now();if(j<=0)throw Z;if(await new Promise((D)=>setTimeout(D,Math.min($,j))),performance.now()>=Q)throw Z;$=Math.min($*2,50)}}async function $0(G){let H;await Z0(U,async()=>{if(!X)return M();let Q=await u(U).catch((Z)=>{if(Z.code!=="ENOENT")throw Z;return});if(!Q||H&&!_0(H,Q))return!1;if(await M())return H=Q,!0;if(!H)return!1;try{return await t(x),!1}catch(Z){if(Z.code!=="ENOENT")throw Z;return!0}},G)}async function t0(G,H){if(!G)return!1;let Q=!1;try{await Z0(U,async()=>{Q=!1;let Z=await u(U).catch(()=>{return});if(!Z||!_0(G,Z))return!1;Q=!0;try{return await t(x),!1}catch($){return $.code==="ENOENT"}},H)}catch{}return Q}async function j0(G){if(X)await g0(U);else await O0(U);let H;try{let Q=await u(U);H={dev:Q.dev,ino:Q.ino,birthtimeMs:Q.birthtimeMs}}catch{H=void 0}try{await P0?.beforeOwnerWrite?.(U,x);let Q=await P0?.failOwnerWrite?.(U,x);if(Q!==void 0)throw Q;let Z=`${JSON.stringify({ownerId:d,createdAt:J()})}
5
- `;if(X)await N0(x,Z);else await w0(x,Z)}catch(Q){let Z=await t0(H,G);if(Vz(Q)&&!Z)return!1;try{await $0(G)}catch{}throw Q}if(z.onStep)await z.onStep("eviction-marker-acquired");return!0}async function q0(G){let H;try{H=await t(U)}catch($){if($.code==="ENOENT")return"missing";throw $}if(X)try{await L(U,()=>w(U,"directory",!0))}catch($){if($.code==="ENOENT")return"missing";throw $}if(H.mtimeMs+g>J()){let $=!0;try{await t(x)}catch(j){if(j.code==="ENOENT")$=!1;else return"fresh"}if($)return"fresh";if(H.mtimeMs+Wz>J())return"fresh"}if(z.onStep)await z.onStep("stale-marker-stat");let Q=`${U}.${o()}`;try{await b0(U,Q)}catch($){if($.code==="ENOENT")return"missing";throw $}if(z.onStep)await z.onStep("stale-marker-claimed");let Z=X?await u(Q):void 0;return await Z0(Q,async()=>{if(!Z)return!0;let $=await u(Q).catch((j)=>{if(j.code!=="ENOENT")throw j;return});return!!$&&_0(Z,$)},G),"recovered"}async function F0(G,H){try{if(!await j0(H))return!1}catch($){if($.code==="EEXIST"){if(X)try{await L(U,()=>w(U,"directory",!0)),await L(x,()=>P(x,!0))}catch(j){if(j.code!=="ENOENT")throw j}return!1}throw $}let Q=!1,Z;try{await G()}catch($){Q=!0,Z=$}try{await $0(H)}catch($){if((e(Z,B)||m(Z,[B,K,x]))&&(e($,U)||m($,[x])))throw Z;throw $}if(Q)throw Z;return!0}async function I0(){for(let G=0;G<s;G++){if(z.onStep)await z.onStep("relinquish-read");let H;try{H=await F()}catch(Q){if(X&&!(Q instanceof i))throw Q;return}if(H?.ownerId!==Y)return;try{await c(B,{recursive:!0,force:!0});return}catch(Q){if(X)throw Q;await C()}}}function A0(){if(!z.renew||V||W)return;let G=z.renewIntervalMs??Math.max(1000,Math.floor(z.ttlMs/3));if(_=Nz(()=>{_=null;let H=(async()=>{let Q=!V;try{if(!await F0(async()=>{let $=await F(),j=J();if(V||W){Q=!1;return}if($?.ownerId!==Y){q("taken-over",$),Q=!1;return}if(!(Number($?.expiresAt)>j)){q("expired",$),Q=!1;return}if(z.onStep)await z.onStep("renewal-owner-confirmed");if(V||W){Q=!1;return}if(!await M())return;if(z.onStep)await z.onStep("renewal-write-fenced");if(V||W){Q=!1;return}if(!await M())return;if(z.onStep)await z.onStep("renewal-write-ready");if(V||W){Q=!1;return}if(await r(),z.onStep)await z.onStep("renewal-write-finished");if(!await M()){q("marker-lost"),Q=!1,await I0();return}})&&z.onStep)await z.onStep("renewal-marker-unavailable")}catch(Z){if(X&&Z instanceof R&&!v(Z))b=Z,q("renewal-failed"),Q=!1;try{let $=await F();if($?.ownerId!==Y)q("taken-over",$);else if(!(Number($?.expiresAt)>J()))q("expired",$)}catch{q("renewal-failed")}}finally{if(z.onStep)try{await z.onStep("renewal-finished")}catch{}if(Q&&!V&&!W)A0()}})();E=H,H.finally(()=>{if(E===H)E=null})},G),"unref"in _)_.unref();y("scheduled")}function S0(){try{let G=z.onContended?.();if(G&&(typeof G==="object"||typeof G==="function")&&"then"in G&&typeof G.then==="function")Promise.resolve(G).catch(()=>{})}catch{}return null}let k=!1,z0;try{k=await A()}catch(G){if(!v(G))throw G;z0=G}if(!k)for(let G=0;G<s;G++)try{if(k=await A(),k)break;let H=await T();if(H.status==="live")return S0();if(H.status==="missing")continue;if(z.onStep)await z.onStep("stale-lock-observed");try{if(!await j0()){await C();continue}}catch(Q){if(Q.code!=="EEXIST")throw Q;if(await q0()==="fresh")return null;await C();continue}try{let Q=await T();if(Q.status==="live")return S0();if(!D0(H.identity,Q))continue;if(!await M())return null;if(z.onStep)await z.onStep("stale-lock-confirmed");if(!await M())return null;let Z=await T(!1);if(!D0(H.identity,Z))continue;if(await Y0(B),!await M())return null;if(z.onStep)await z.onStep("stale-lock-removed");if(k=await A(),!k)return null;if(z.onStep)await z.onStep("stale-lock-recreated");if(!await M())return await I0(),k=!1,null;break}finally{await $0()}}catch(H){if(!v(H))throw H;z0??=H,await C()}if(!k){if(z0)throw z0;return null}return A0(),Xz(B,z.ttlMs,J),{ownerId:Y,whenLost:()=>f,hasLost:()=>W!==void 0,assertOwned:async()=>{if(b)throw b;let G;try{let H=await F();if(G=H,!V&&!W&&H?.ownerId===Y&&Number(H?.expiresAt)>J())return;q(H?.ownerId!==Y?"taken-over":"expired",H)}catch(H){if(q("unreadable"),X&&H instanceof R)throw H}throw new a({target:z.path,name:z.name,expectedOwnerId:Y,...typeof G?.ownerId==="string"?{observedOwnerId:G.ownerId}:{},...typeof G?.expiresAt==="number"?{observedExpiresAt:G.expiresAt}:{}})},release:async(G)=>{if(X&&N)return;V=!0,S(),await E;let H=performance.now()+g+1000,Q=5,Z,$;for(;;){try{let D=!1,n=await F0(async()=>{let C0;try{C0=await F()}catch(T0){if(X&&T0.code==="ENOENT"){D=!0;return}throw T0}if(C0?.ownerId!==Y){D=!0;return}if(z.onStep)await z.onStep("release-owner-confirmed");if(!await M())return;await Y0(B),D=!0},H);if($=void 0,n&&D){N=!0;return}if(!n){if(G?.aborted)return;await q0(H)}}catch(D){let n=X&&(v(D)||m(D,[B,K,U,x]));if(!e(D,B)&&!n){if(X)throw D;return}if(n)$??=D;else Z??=D}let j=H-performance.now();if(j<=0)break;if(await new Promise((D)=>setTimeout(D,Math.min(Q,j))),X&&performance.now()>=H)break;Q=Math.min(Q*2,50)}if(X)throw $??Z??new R(B,"release-not-confirmed",{code:"ETIMEDOUT"});m0("fs").warn("File lock release deadline expired",{lockPath:B})}}}import{readFileSync as Dz,realpathSync as jz,statSync as qz,watch as Fz}from"node:fs";import{readFile as Iz,stat as Az}from"node:fs/promises";import{basename as l0,dirname as d0,join as Sz,resolve as s0}from"node:path";import{clearTimeout as V0,setTimeout as p0}from"node:timers";function n0(z){let B=s0(z);if(X)return B;try{return jz(B)}catch{let K=d0(B);return K===B?B:Sz(n0(K),l0(B))}}function c0(z,B){if(!z)return null;let K=`${z.dev}:${z.ino}:${z.mtimeMs}:${z.size}`;return B?`${K}:${z.ctimeMs}`:K}function tz(z,B,K={}){return i0(z,B,K,!1)}function oz(z,B,K={}){return i0(z,B,K,!0)}function i0(z,B,K,Y){let J=globalThis,_=Symbol.for(Y?"cortexkit.common-auth.settings.watchers.v1":"cortexkit.common-auth.tui-prefs.watchers.v1"),V=J[_]??new Map;J[_]=V;let N=Y?s0(z):n0(z),W=V.get(N);if(W)return W.subscribe(B);let O=l0(N),f=X&&!Y?(I)=>P(I):K.fs?.readFile??((I)=>Iz(I,"utf8")),y=K.fs?.stat??Az,S=null,q=null,b=null,E=null;if(Y||!X)try{let I=c0((K.fs?.statSync??qz)(N),Y);E=(K.fs?.readFileSync??((A)=>Dz(A,"utf8")))(N),b=I}catch{}let U=!1,x=new Set,d=()=>{for(let I of[...x]){if(!x.has(I))continue;try{I.notify()}catch{}}},g=Promise.resolve(),s=(I)=>{return g=g.then(async()=>{if(U)return;let A=c0(await y(N).catch(()=>null),Y);if(U)return;if(!I&&A===b)return;if(A===null){let T=b!==null;if(b=null,Y&&T)E=null,d();return}let C=await f(N).catch((T)=>{if(T instanceof R&&T.code!=="ENOENT")if(K.onError)K.onError(T);else process.emitWarning(T.message,T.name);return null});if(U)return;if(C===null)return;if(b=A,C===E)return;E=C,d()}),g},v=()=>{if(U)return;if(S)V0(S);S=p0(()=>{S=null,s(!0)},150)},F=null;try{F=(K.watchDirectory??Fz)(d0(N),(I,A)=>{let C=A?.toString(),T=C===O||C?.startsWith(`${O}.`)&&C.endsWith(".tmp");if(C!=null&&!T)return;v()}),F.on("error",()=>{F?.close(),F=null})}catch{}let r=()=>{if(U||q)return;q=p0(()=>{if(q=null,U)return;s(!1).finally(r)},1000),q.unref()},h={subscribe:(I)=>{let A={notify:I};return x.add(A),()=>{if(!x.delete(A)||x.size>0)return;if(U=!0,S)V0(S);if(q)V0(q);if(F?.close(),V.get(N)===h)V.delete(N)}}};return V.set(N,h),r(),h.subscribe(B)}import{randomUUID as a0}from"node:crypto";import{mkdir as Cz,open as Q0,rename as Tz,rm as Mz}from"node:fs/promises";import{dirname as W0}from"node:path";class x0 extends Error{constructor(z){super("file or directory sync failed",{cause:z});this.name="AtomicSyncError"}}async function H1(z,B,K={}){await Rz(z,B,K)}async function Rz(z,B,K,Y){if(X)return H0(z,K.serialize?K.serialize(B):`${JSON.stringify(B,null,2)}
6
- `,{beforeRename:K.beforeRename,stagePath:`${z}.${(K.stageName??a0)()}.tmp`,onRenamed:Y,durable:K.durable,durability:K.durability,onOutcome:K.onOutcome});await Cz(W0(z),{recursive:!0});let J=`${z}.${(K.stageName??a0)()}.tmp`,_,V=!1;try{if(_=await Q0(J,"wx",384),V=!0,await _.writeFile(K.serialize?K.serialize(B):`${JSON.stringify(B,null,2)}
7
- `,"utf8"),await _.chmod(384),K.durable)try{await _.sync()}catch(N){throw new x0(N)}if(await _.close(),_=void 0,await K.beforeRename?.(),await Tz(J,z),V=!1,Y?.(),K.durable){let N=await Q0(W0(z),"r");try{await N.sync()}catch(W){throw new x0(W)}finally{await N.close()}}}finally{if(await _?.close().catch(()=>{}),V)await Mz(J,{force:!0}).catch(()=>{})}}async function K1(z){if(X){await w(z,"file");let Y=new R(z,"directory-sync-unsupported",{code:"ENOTSUP"});throw Y.published=!0,Y}let B=await Q0(z,"r");try{await B.sync()}finally{await B.close()}let K=await Q0(W0(z),"r");try{await K.sync()}finally{await K.close()}}var Y1=Object.freeze({sidebar:Object.freeze({name:"sidebar-write",ttlMs:1e4,timeoutMs:15000,renew:!0}),preferences:Object.freeze({name:"preferences",ttlMs:1e4,timeoutMs:2000,renew:!0})});
8
- export{x0 as Xf,H1 as Yf,Rz as Zf,K1 as _f,R0 as $f,J0 as ag,L0 as bg,a as cg,e0 as dg,Pz as eg,Y1 as fg,tz as gg,oz as hg};
@@ -1 +0,0 @@
1
- import{b as a}from"./index-fxqghceg.js";import"./index-d7bsrp0m.js";export default a();