@dexto/agent-management 1.19.1 → 1.19.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.
@@ -48,6 +48,7 @@ var import_node_crypto = require("node:crypto");
48
48
  var import_promises = require("node:fs/promises");
49
49
  var import_node_path = __toESM(require("node:path"), 1);
50
50
  var import_zod = require("zod");
51
+ var import_refresh_lock = require("./refresh-lock.js");
51
52
  const DEFAULT_PROFILE = "default";
52
53
  const SignedInAsSchema = import_zod.z.object({
53
54
  account: import_zod.z.object({ email: import_zod.z.string(), id: import_zod.z.string(), name: import_zod.z.string() }),
@@ -218,18 +219,55 @@ const REFRESH_MARGIN_MS = 6e4;
218
219
  function isExpiring(login) {
219
220
  return Date.parse(login.expiresAt) - Date.now() < REFRESH_MARGIN_MS;
220
221
  }
221
- async function refreshSaved(dir, profile, stored, refresh) {
222
+ const refreshesInFlight = /* @__PURE__ */ new Map();
223
+ const REFRESH_TIMEOUT_MS = 1e4;
224
+ function refreshSaved(dir, profile, stored, refresh) {
225
+ const file = loginFile(dir, profile);
226
+ const inFlight = refreshesInFlight.get(file);
227
+ if (inFlight !== void 0) return inFlight;
228
+ const refreshing = (0, import_refresh_lock.withRefreshLock)(
229
+ `${file.slice(0, -".json".length)}.refresh.lock`,
230
+ () => refreshUnlessSaved(dir, profile, stored, refresh)
231
+ ).finally(() => refreshesInFlight.delete(file));
232
+ refreshesInFlight.set(file, refreshing);
233
+ return refreshing;
234
+ }
235
+ async function refreshUnlessSaved(dir, profile, stored, refresh) {
236
+ const savedByOther = async () => {
237
+ const current = await readLogin(dir, profile);
238
+ return current?.kind === "oauth" && current.refreshToken !== stored.refreshToken && !isExpiring(current) ? current : null;
239
+ };
240
+ const saved = await savedByOther();
241
+ if (saved !== null) return saved;
222
242
  try {
223
- const refreshed = await refresh(stored);
243
+ const refreshed = await withDeadline(
244
+ REFRESH_TIMEOUT_MS,
245
+ (signal) => refresh(stored, signal)
246
+ );
224
247
  await writeLogin(dir, profile, refreshed);
225
248
  return refreshed;
226
249
  } catch (error) {
227
- const current = await readLogin(dir, profile);
228
- if (current?.kind === "oauth" && current.refreshToken !== stored.refreshToken && !isExpiring(current))
229
- return current;
250
+ const current = await savedByOther();
251
+ if (current !== null) return current;
230
252
  throw error;
231
253
  }
232
254
  }
255
+ async function withDeadline(ms, work) {
256
+ const controller = new AbortController();
257
+ let timer;
258
+ const deadline = new Promise((_, reject) => {
259
+ timer = setTimeout(() => {
260
+ const error = new Error(`Refreshing the Dexto sign-in timed out after ${ms} ms.`);
261
+ controller.abort(error);
262
+ reject(error);
263
+ }, ms);
264
+ });
265
+ try {
266
+ return await Promise.race([work(controller.signal), deadline]);
267
+ } finally {
268
+ clearTimeout(timer);
269
+ }
270
+ }
233
271
  async function readOptional(file) {
234
272
  try {
235
273
  return await (0, import_promises.readFile)(file, "utf8");
@@ -133,7 +133,7 @@ export interface ResolveCredentialInput {
133
133
  /** The global --profile value. */
134
134
  readonly profile: string | null;
135
135
  readonly environment: Readonly<Record<string, string | undefined>>;
136
- readonly refresh: (login: OAuthLogin) => Promise<OAuthLogin>;
136
+ readonly refresh: (login: OAuthLogin, signal: AbortSignal) => Promise<OAuthLogin>;
137
137
  }
138
138
  /**
139
139
  * --api-key, then DEXTO_API_KEY, then the profile's saved login; null when signed out. A saved
@@ -1 +1 @@
1
- {"version":3,"file":"dexto-accounts.d.ts","sourceRoot":"","sources":["../../src/auth/dexto-accounts.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,eAAO,MAAM,eAAe,YAAY,CAAC;AAEzC,0FAA0F;AAC1F,eAAO,MAAM,gBAAgB;;;;;;;;;;;iBAG3B,CAAC;AAEH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE3D,QAAA,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAsBT,CAAC;AAW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MAAM,UAAU,GAChB;IACI,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC5B,GACD;IACI,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC9B,CAAC;AAER,MAAM,WAAW,sBAAsB;IACnC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,kCAAkC;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,kCAAkC;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IACnE,QAAQ,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;CACrF;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAejG"}
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
2
2
  import { chmod, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { z } from "zod";
5
+ import { withRefreshLock } from "./refresh-lock.js";
5
6
  const DEFAULT_PROFILE = "default";
6
7
  const SignedInAsSchema = z.object({
7
8
  account: z.object({ email: z.string(), id: z.string(), name: z.string() }),
@@ -172,18 +173,55 @@ const REFRESH_MARGIN_MS = 6e4;
172
173
  function isExpiring(login) {
173
174
  return Date.parse(login.expiresAt) - Date.now() < REFRESH_MARGIN_MS;
174
175
  }
175
- async function refreshSaved(dir, profile, stored, refresh) {
176
+ const refreshesInFlight = /* @__PURE__ */ new Map();
177
+ const REFRESH_TIMEOUT_MS = 1e4;
178
+ function refreshSaved(dir, profile, stored, refresh) {
179
+ const file = loginFile(dir, profile);
180
+ const inFlight = refreshesInFlight.get(file);
181
+ if (inFlight !== void 0) return inFlight;
182
+ const refreshing = withRefreshLock(
183
+ `${file.slice(0, -".json".length)}.refresh.lock`,
184
+ () => refreshUnlessSaved(dir, profile, stored, refresh)
185
+ ).finally(() => refreshesInFlight.delete(file));
186
+ refreshesInFlight.set(file, refreshing);
187
+ return refreshing;
188
+ }
189
+ async function refreshUnlessSaved(dir, profile, stored, refresh) {
190
+ const savedByOther = async () => {
191
+ const current = await readLogin(dir, profile);
192
+ return current?.kind === "oauth" && current.refreshToken !== stored.refreshToken && !isExpiring(current) ? current : null;
193
+ };
194
+ const saved = await savedByOther();
195
+ if (saved !== null) return saved;
176
196
  try {
177
- const refreshed = await refresh(stored);
197
+ const refreshed = await withDeadline(
198
+ REFRESH_TIMEOUT_MS,
199
+ (signal) => refresh(stored, signal)
200
+ );
178
201
  await writeLogin(dir, profile, refreshed);
179
202
  return refreshed;
180
203
  } catch (error) {
181
- const current = await readLogin(dir, profile);
182
- if (current?.kind === "oauth" && current.refreshToken !== stored.refreshToken && !isExpiring(current))
183
- return current;
204
+ const current = await savedByOther();
205
+ if (current !== null) return current;
184
206
  throw error;
185
207
  }
186
208
  }
209
+ async function withDeadline(ms, work) {
210
+ const controller = new AbortController();
211
+ let timer;
212
+ const deadline = new Promise((_, reject) => {
213
+ timer = setTimeout(() => {
214
+ const error = new Error(`Refreshing the Dexto sign-in timed out after ${ms} ms.`);
215
+ controller.abort(error);
216
+ reject(error);
217
+ }, ms);
218
+ });
219
+ try {
220
+ return await Promise.race([work(controller.signal), deadline]);
221
+ } finally {
222
+ clearTimeout(timer);
223
+ }
224
+ }
187
225
  async function readOptional(file) {
188
226
  try {
189
227
  return await readFile(file, "utf8");
@@ -42,7 +42,7 @@ function defaultDextoGatewayOptions() {
42
42
  ""
43
43
  ),
44
44
  fetch,
45
- refresh: (login) => (0, import_dexto_sign_in.refreshSignIn)({ fetch, signal: AbortSignal.timeout(1e4) }, login)
45
+ refresh: (login, signal) => (0, import_dexto_sign_in.refreshSignIn)({ fetch, signal }, login)
46
46
  };
47
47
  }
48
48
  async function dextoModelAccess(options) {
@@ -7,7 +7,7 @@ export interface DextoGatewayOptions {
7
7
  /** The public API new sign-ins use; requests go to the one their sign-in was saved for. */
8
8
  readonly apiBaseUrl: string;
9
9
  readonly fetch: typeof globalThis.fetch;
10
- readonly refresh: (login: OAuthLogin) => Promise<OAuthLogin>;
10
+ readonly refresh: (login: OAuthLogin, signal: AbortSignal) => Promise<OAuthLogin>;
11
11
  }
12
12
  /** This machine's sign-in, Dexto's public API and the global fetch. */
13
13
  export declare function defaultDextoGatewayOptions(): DextoGatewayOptions;
@@ -1 +1 @@
1
- {"version":3,"file":"dexto-gateway.d.ts","sourceRoot":"","sources":["../../src/auth/dexto-gateway.ts"],"names":[],"mappings":"AAGA,OAAO,EAAsB,KAAK,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAE/E,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,UAAU,EAAqB,MAAM,qBAAqB,CAAC;AAO1F,MAAM,WAAW,mBAAmB;IAChC,wBAAwB;IACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;IACnE,2FAA2F;IAC3F,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,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
15
15
  ),
16
16
  fetch,
17
- refresh: (login) => refreshSignIn({ fetch, signal: AbortSignal.timeout(1e4) }, login)
17
+ refresh: (login, signal) => refreshSignIn({ fetch, signal }, login)
18
18
  };
19
19
  }
20
20
  async function dextoModelAccess(options) {
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var refresh_lock_exports = {};
30
+ __export(refresh_lock_exports, {
31
+ takeOverStaleLock: () => takeOverStaleLock,
32
+ withRefreshLock: () => withRefreshLock
33
+ });
34
+ module.exports = __toCommonJS(refresh_lock_exports);
35
+ var import_node_crypto = require("node:crypto");
36
+ var import_promises = require("node:fs/promises");
37
+ var import_node_os = __toESM(require("node:os"), 1);
38
+ var import_node_path = __toESM(require("node:path"), 1);
39
+ const REFRESH_LOCK_STALE_MS = 3e4;
40
+ const REFRESH_LOCK_WAIT_MS = REFRESH_LOCK_STALE_MS + 5e3;
41
+ const REFRESH_LOCK_POLL_MS = 50;
42
+ async function withRefreshLock(lock, work) {
43
+ const owner = `${import_node_os.default.hostname()} ${process.pid} ${(0, import_node_crypto.randomUUID)()}`;
44
+ const deadline = Date.now() + REFRESH_LOCK_WAIT_MS;
45
+ await (0, import_promises.mkdir)(import_node_path.default.dirname(lock), { mode: 448, recursive: true });
46
+ for (; ; ) {
47
+ await (0, import_promises.writeFile)(lock, owner, { flag: "wx", mode: 384 }).catch((error) => {
48
+ if (!hasCode(error, "EEXIST")) throw error;
49
+ });
50
+ const held = await readLock(lock);
51
+ if (held?.owner === owner) break;
52
+ if (held !== null && isStale(held)) {
53
+ await takeOverStaleLock(lock, held.owner);
54
+ continue;
55
+ }
56
+ if (Date.now() > deadline)
57
+ throw new Error(`Another dexto process is still refreshing this sign-in (${lock}).`);
58
+ await new Promise((resolve) => setTimeout(resolve, REFRESH_LOCK_POLL_MS));
59
+ }
60
+ try {
61
+ return await work();
62
+ } finally {
63
+ if ((await readLock(lock))?.owner === owner) await (0, import_promises.rm)(lock, { force: true });
64
+ }
65
+ }
66
+ async function takeOverStaleLock(lock, staleOwner) {
67
+ const moved = `${lock}.${(0, import_node_crypto.randomUUID)()}`;
68
+ try {
69
+ await (0, import_promises.rename)(lock, moved);
70
+ } catch (error) {
71
+ if (hasCode(error, "ENOENT")) return;
72
+ throw error;
73
+ }
74
+ if ((await readLock(moved))?.owner !== staleOwner) {
75
+ await (0, import_promises.link)(moved, lock).catch((error) => {
76
+ if (!hasCode(error, "EEXIST")) throw error;
77
+ });
78
+ }
79
+ await (0, import_promises.rm)(moved, { force: true });
80
+ }
81
+ async function readLock(lock) {
82
+ let handle;
83
+ try {
84
+ handle = await (0, import_promises.open)(lock, "r");
85
+ } catch (error) {
86
+ if (hasCode(error, "ENOENT")) return null;
87
+ throw error;
88
+ }
89
+ try {
90
+ const [owner, held] = await Promise.all([handle.readFile("utf8"), handle.stat()]);
91
+ return { owner, modifiedAt: held.mtimeMs };
92
+ } finally {
93
+ await handle.close();
94
+ }
95
+ }
96
+ function isStale(held) {
97
+ if (Date.now() - held.modifiedAt > REFRESH_LOCK_STALE_MS) return true;
98
+ const [host, pid] = held.owner.split(" ");
99
+ return host === import_node_os.default.hostname() && Number.isInteger(Number(pid)) && !processIsAlive(Number(pid));
100
+ }
101
+ function processIsAlive(pid) {
102
+ try {
103
+ process.kill(pid, 0);
104
+ return true;
105
+ } catch (error) {
106
+ return !hasCode(error, "ESRCH");
107
+ }
108
+ }
109
+ function hasCode(error, code) {
110
+ return error instanceof Error && "code" in error && error.code === code;
111
+ }
112
+ // Annotate the CommonJS export names for ESM import in node:
113
+ 0 && (module.exports = {
114
+ takeOverStaleLock,
115
+ withRefreshLock
116
+ });
@@ -0,0 +1,9 @@
1
+ /** Runs `work` holding the lock, taking over one a crashed process left behind. */
2
+ export declare function withRefreshLock<T>(lock: string, work: () => Promise<T>): Promise<T>;
3
+ /**
4
+ * Moves the lock aside atomically, so of several processes taking over one stale lock only one
5
+ * removes it. Holder ids are unique, so a moved lock that names another holder is one a process
6
+ * took over after `staleOwner` was read, and goes back.
7
+ */
8
+ export declare function takeOverStaleLock(lock: string, staleOwner: string): Promise<void>;
9
+ //# sourceMappingURL=refresh-lock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refresh-lock.d.ts","sourceRoot":"","sources":["../../src/auth/refresh-lock.ts"],"names":[],"mappings":"AAeA,mFAAmF;AACnF,wBAAsB,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAuBzF;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAcvF"}
@@ -0,0 +1,81 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { link, mkdir, open, rename, rm, writeFile } from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ const REFRESH_LOCK_STALE_MS = 3e4;
6
+ const REFRESH_LOCK_WAIT_MS = REFRESH_LOCK_STALE_MS + 5e3;
7
+ const REFRESH_LOCK_POLL_MS = 50;
8
+ async function withRefreshLock(lock, work) {
9
+ const owner = `${os.hostname()} ${process.pid} ${randomUUID()}`;
10
+ const deadline = Date.now() + REFRESH_LOCK_WAIT_MS;
11
+ await mkdir(path.dirname(lock), { mode: 448, recursive: true });
12
+ for (; ; ) {
13
+ await writeFile(lock, owner, { flag: "wx", mode: 384 }).catch((error) => {
14
+ if (!hasCode(error, "EEXIST")) throw error;
15
+ });
16
+ const held = await readLock(lock);
17
+ if (held?.owner === owner) break;
18
+ if (held !== null && isStale(held)) {
19
+ await takeOverStaleLock(lock, held.owner);
20
+ continue;
21
+ }
22
+ if (Date.now() > deadline)
23
+ throw new Error(`Another dexto process is still refreshing this sign-in (${lock}).`);
24
+ await new Promise((resolve) => setTimeout(resolve, REFRESH_LOCK_POLL_MS));
25
+ }
26
+ try {
27
+ return await work();
28
+ } finally {
29
+ if ((await readLock(lock))?.owner === owner) await rm(lock, { force: true });
30
+ }
31
+ }
32
+ async function takeOverStaleLock(lock, staleOwner) {
33
+ const moved = `${lock}.${randomUUID()}`;
34
+ try {
35
+ await rename(lock, moved);
36
+ } catch (error) {
37
+ if (hasCode(error, "ENOENT")) return;
38
+ throw error;
39
+ }
40
+ if ((await readLock(moved))?.owner !== staleOwner) {
41
+ await link(moved, lock).catch((error) => {
42
+ if (!hasCode(error, "EEXIST")) throw error;
43
+ });
44
+ }
45
+ await rm(moved, { force: true });
46
+ }
47
+ async function readLock(lock) {
48
+ let handle;
49
+ try {
50
+ handle = await open(lock, "r");
51
+ } catch (error) {
52
+ if (hasCode(error, "ENOENT")) return null;
53
+ throw error;
54
+ }
55
+ try {
56
+ const [owner, held] = await Promise.all([handle.readFile("utf8"), handle.stat()]);
57
+ return { owner, modifiedAt: held.mtimeMs };
58
+ } finally {
59
+ await handle.close();
60
+ }
61
+ }
62
+ function isStale(held) {
63
+ if (Date.now() - held.modifiedAt > REFRESH_LOCK_STALE_MS) return true;
64
+ const [host, pid] = held.owner.split(" ");
65
+ return host === os.hostname() && Number.isInteger(Number(pid)) && !processIsAlive(Number(pid));
66
+ }
67
+ function processIsAlive(pid) {
68
+ try {
69
+ process.kill(pid, 0);
70
+ return true;
71
+ } catch (error) {
72
+ return !hasCode(error, "ESRCH");
73
+ }
74
+ }
75
+ function hasCode(error, code) {
76
+ return error instanceof Error && "code" in error && error.code === code;
77
+ }
78
+ export {
79
+ takeOverStaleLock,
80
+ withRefreshLock
81
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dexto/agent-management",
3
- "version": "1.19.1",
3
+ "version": "1.19.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,10 +22,10 @@
22
22
  "dependencies": {
23
23
  "yaml": "^2.8.3",
24
24
  "zod": "^4.3.6",
25
- "@dexto/agent-config": "1.19.1",
26
- "@dexto/core": "1.19.1",
27
- "@dexto/llm": "1.19.1",
28
- "@dexto/tools-builtins": "1.19.1"
25
+ "@dexto/agent-config": "1.19.2",
26
+ "@dexto/core": "1.19.2",
27
+ "@dexto/llm": "1.19.2",
28
+ "@dexto/tools-builtins": "1.19.2"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/node": "^22.13.5"