@ai-devkit/daemon-client 0.2.0 → 0.3.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/__tests__/autostart-stale.test.d.ts +2 -0
- package/dist/__tests__/autostart-stale.test.d.ts.map +1 -0
- package/dist/__tests__/autostart-stale.test.js +809 -0
- package/dist/__tests__/integration.test.js +193 -169
- package/dist/autostart.d.ts +6 -0
- package/dist/autostart.d.ts.map +1 -1
- package/dist/autostart.js +373 -48
- package/dist/binary.d.ts +18 -1
- package/dist/binary.d.ts.map +1 -1
- package/dist/binary.js +42 -6
- package/dist/client.d.ts +24 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +85 -0
- package/dist/gen/AgentLookupResult.d.ts +29 -0
- package/dist/gen/AgentLookupResult.d.ts.map +1 -0
- package/dist/gen/AgentLookupResult.js +12 -0
- package/dist/gen/AgentNameParams.d.ts +8 -0
- package/dist/gen/AgentNameParams.d.ts.map +1 -0
- package/dist/gen/AgentNameParams.js +5 -0
- package/dist/gen/AgentRenameParams.d.ts +8 -0
- package/dist/gen/AgentRenameParams.d.ts.map +1 -0
- package/dist/gen/AgentRenameParams.js +4 -0
- package/dist/gen/AgentRenameResult.d.ts +18 -0
- package/dist/gen/AgentRenameResult.d.ts.map +1 -0
- package/dist/gen/AgentRenameResult.js +6 -0
- package/dist/gen/AgentUnregisterResult.d.ts +8 -0
- package/dist/gen/AgentUnregisterResult.d.ts.map +1 -0
- package/dist/gen/AgentUnregisterResult.js +4 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/__tests__/daemon-sweep.test.d.ts +0 -2
- package/dist/__tests__/daemon-sweep.test.d.ts.map +0 -1
- package/dist/__tests__/daemon-sweep.test.js +0 -356
package/dist/autostart.js
CHANGED
|
@@ -27,6 +27,13 @@ function _async_to_generator(fn) {
|
|
|
27
27
|
});
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
|
+
function _instanceof(left, right) {
|
|
31
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
32
|
+
return !!right[Symbol.hasInstance](left);
|
|
33
|
+
} else {
|
|
34
|
+
return left instanceof right;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
30
37
|
function _ts_generator(thisArg, body) {
|
|
31
38
|
var f, y, t, _ = {
|
|
32
39
|
label: 0,
|
|
@@ -130,15 +137,19 @@ import * as fs from "node:fs";
|
|
|
130
137
|
import * as path from "node:path";
|
|
131
138
|
import { spawn } from "node:child_process";
|
|
132
139
|
import { DaemonClient, daemonSocketPath } from "./client.js";
|
|
133
|
-
import {
|
|
140
|
+
import { readBinaryBuildVersion, resolveDaemonBinaryInfo } from "./binary.js";
|
|
141
|
+
/** A crashed spawner's lock is reclaimed after this long. */ var STALE_LOCK_MS = 30000;
|
|
134
142
|
/**
|
|
135
143
|
* tmux-style implicit spawn: if the socket answers, return a client; if not,
|
|
136
144
|
* spawn the daemon (single-spawner guarded by an exclusive lockfile) and wait
|
|
137
145
|
* for readiness. Returns null when no binary exists or spawn fails — callers
|
|
138
146
|
* degrade to today's code paths.
|
|
147
|
+
*
|
|
148
|
+
* A live daemon older than the bundled platform package is replaced (#376);
|
|
149
|
+
* see staleDaemonBinary for the rule.
|
|
139
150
|
*/ export function ensureDaemon() {
|
|
140
151
|
return _async_to_generator(function() {
|
|
141
|
-
var opts, _opts_waitMs, waitMs, debug, existing, bin, sockPath, dir,
|
|
152
|
+
var opts, _opts_waitMs, _opts_resolveBinary, waitMs, debug, resolveBinary, spawnSkipped, existing, bin, bin1, sockPath, dir, lockPath, acquired;
|
|
142
153
|
var _arguments = arguments;
|
|
143
154
|
return _ts_generator(this, function(_state) {
|
|
144
155
|
switch(_state.label){
|
|
@@ -146,6 +157,7 @@ import { resolveDaemonBinary } from "./binary.js";
|
|
|
146
157
|
opts = _arguments.length > 0 && _arguments[0] !== void 0 ? _arguments[0] : {};
|
|
147
158
|
waitMs = (_opts_waitMs = opts.waitMs) !== null && _opts_waitMs !== void 0 ? _opts_waitMs : 3000;
|
|
148
159
|
debug = opts.onDebug;
|
|
160
|
+
resolveBinary = (_opts_resolveBinary = opts.resolveBinary) !== null && _opts_resolveBinary !== void 0 ? _opts_resolveBinary : resolveDaemonBinaryInfo;
|
|
149
161
|
// Test runs must never spawn a real daemon. Callers that want to exercise
|
|
150
162
|
// autostart do so by pointing DEVKITD_BIN at a real binary in an explicit
|
|
151
163
|
// integration test — the env below is checked there too via VITEST_SKIP.
|
|
@@ -156,29 +168,46 @@ import { resolveDaemonBinary } from "./binary.js";
|
|
|
156
168
|
null
|
|
157
169
|
];
|
|
158
170
|
}
|
|
171
|
+
spawnSkipped = function() {
|
|
172
|
+
return !!process.env.VITEST && !process.env.DEVKITD_BIN && !process.env.AI_DEVKITD_BIN;
|
|
173
|
+
};
|
|
159
174
|
return [
|
|
160
175
|
4,
|
|
161
176
|
DaemonClient.tryConnect()
|
|
162
177
|
];
|
|
163
178
|
case 1:
|
|
164
179
|
existing = _state.sent();
|
|
165
|
-
if (existing)
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
180
|
+
if (!existing) return [
|
|
181
|
+
3,
|
|
182
|
+
3
|
|
183
|
+
];
|
|
184
|
+
debug === null || debug === void 0 ? void 0 : debug("connected to live daemon at ".concat(daemonSocketPath()));
|
|
185
|
+
return [
|
|
186
|
+
4,
|
|
187
|
+
staleDaemonBinary(existing, resolveBinary(debug), debug)
|
|
188
|
+
];
|
|
189
|
+
case 2:
|
|
190
|
+
bin = _state.sent();
|
|
191
|
+
if (!bin || spawnSkipped()) return [
|
|
192
|
+
2,
|
|
193
|
+
existing
|
|
194
|
+
];
|
|
195
|
+
existing.close();
|
|
196
|
+
return [
|
|
197
|
+
2,
|
|
198
|
+
replaceStaleDaemon(bin, waitMs, debug)
|
|
199
|
+
];
|
|
200
|
+
case 3:
|
|
172
201
|
debug === null || debug === void 0 ? void 0 : debug("no live daemon on socket; attempting autostart");
|
|
173
|
-
if (
|
|
202
|
+
if (spawnSkipped()) {
|
|
174
203
|
debug === null || debug === void 0 ? void 0 : debug("autostart skipped in test env (VITEST set, no DEVKITD_BIN)");
|
|
175
204
|
return [
|
|
176
205
|
2,
|
|
177
206
|
null
|
|
178
207
|
];
|
|
179
208
|
}
|
|
180
|
-
|
|
181
|
-
if (!
|
|
209
|
+
bin1 = resolveBinary(debug);
|
|
210
|
+
if (!bin1) {
|
|
182
211
|
debug === null || debug === void 0 ? void 0 : debug("no devkitd binary; daemon path unavailable");
|
|
183
212
|
return [
|
|
184
213
|
2,
|
|
@@ -193,7 +222,6 @@ import { resolveDaemonBinary } from "./binary.js";
|
|
|
193
222
|
// Single-spawner lockfile: O_EXCL create serializes concurrent starters, and
|
|
194
223
|
// the holder unlinks it when done. A stale lock (crashed spawner) is reclaimed
|
|
195
224
|
// after STALE_LOCK_MS so a dead starter can't wedge autostart forever.
|
|
196
|
-
STALE_LOCK_MS = 30000;
|
|
197
225
|
lockPath = "".concat(sockPath, ".spawn.lock");
|
|
198
226
|
acquired = acquireSpawnLock(lockPath, STALE_LOCK_MS);
|
|
199
227
|
if (!acquired) {
|
|
@@ -204,66 +232,271 @@ import { resolveDaemonBinary } from "./binary.js";
|
|
|
204
232
|
waitForSocket(waitMs, debug)
|
|
205
233
|
];
|
|
206
234
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
235
|
+
_state.label = 4;
|
|
236
|
+
case 4:
|
|
237
|
+
_state.trys.push([
|
|
238
|
+
4,
|
|
239
|
+
,
|
|
240
|
+
6,
|
|
241
|
+
7
|
|
242
|
+
]);
|
|
243
|
+
if (!spawnDaemon(bin1.path, dir, debug)) return [
|
|
244
|
+
2,
|
|
245
|
+
null
|
|
246
|
+
];
|
|
247
|
+
return [
|
|
248
|
+
4,
|
|
249
|
+
waitForSocket(waitMs, debug)
|
|
250
|
+
];
|
|
251
|
+
case 5:
|
|
252
|
+
return [
|
|
253
|
+
2,
|
|
254
|
+
_state.sent()
|
|
255
|
+
];
|
|
256
|
+
case 6:
|
|
257
|
+
releaseSpawnLock(lockPath);
|
|
258
|
+
return [
|
|
259
|
+
7
|
|
260
|
+
];
|
|
261
|
+
case 7:
|
|
262
|
+
return [
|
|
263
|
+
2
|
|
264
|
+
];
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}).apply(this, arguments);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Decide whether the live daemon should be replaced by `bin`; returns the
|
|
271
|
+
* binary to spawn, or null to keep the live daemon. The rule:
|
|
272
|
+
*
|
|
273
|
+
* - No package version for the bundled binary (env override, ~/.ai-devkit/bin,
|
|
274
|
+
* dev checkout) → keep. Only platform-package installs know which release
|
|
275
|
+
* they expect.
|
|
276
|
+
* - Live `buildVersion` equals the package version → keep.
|
|
277
|
+
* - Live `buildVersion` is semver-newer → keep. A newer CLI started it and
|
|
278
|
+
* downgrading would make the two CLIs fight over the socket; older
|
|
279
|
+
* clients already fall back on `unknown method`.
|
|
280
|
+
* - Older, missing (pre-#376 daemon or unstamped build), or `daemon.status`
|
|
281
|
+
* unknown → stale, but only if the bundled binary itself reports the
|
|
282
|
+
* package version. Otherwise the respawn would mismatch again and every
|
|
283
|
+
* invocation would restart it.
|
|
284
|
+
* - `daemon.status` failing any other way (timeout, socket error) → keep:
|
|
285
|
+
* daemon-first, never kill what we can't identify.
|
|
286
|
+
*/ function staleDaemonBinary(client, bin, debug) {
|
|
287
|
+
return _async_to_generator(function() {
|
|
288
|
+
var running, status, e, msg, label, self;
|
|
289
|
+
return _ts_generator(this, function(_state) {
|
|
290
|
+
switch(_state.label){
|
|
291
|
+
case 0:
|
|
292
|
+
if (!(bin === null || bin === void 0 ? void 0 : bin.version)) {
|
|
293
|
+
debug === null || debug === void 0 ? void 0 : debug("daemon version check skipped (bundled devkitd has no package version)");
|
|
230
294
|
return [
|
|
231
295
|
2,
|
|
232
296
|
null
|
|
233
297
|
];
|
|
234
298
|
}
|
|
235
|
-
|
|
236
|
-
|
|
299
|
+
running = null;
|
|
300
|
+
_state.label = 1;
|
|
301
|
+
case 1:
|
|
237
302
|
_state.trys.push([
|
|
238
|
-
|
|
303
|
+
1,
|
|
304
|
+
3,
|
|
239
305
|
,
|
|
306
|
+
4
|
|
307
|
+
]);
|
|
308
|
+
return [
|
|
240
309
|
4,
|
|
241
|
-
|
|
310
|
+
client.request("daemon.status", {}, 1500)
|
|
311
|
+
];
|
|
312
|
+
case 2:
|
|
313
|
+
status = _state.sent();
|
|
314
|
+
if (typeof (status === null || status === void 0 ? void 0 : status.buildVersion) === "string") running = status.buildVersion;
|
|
315
|
+
return [
|
|
316
|
+
3,
|
|
317
|
+
4
|
|
318
|
+
];
|
|
319
|
+
case 3:
|
|
320
|
+
e = _state.sent();
|
|
321
|
+
msg = _instanceof(e, Error) ? e.message : String(e);
|
|
322
|
+
if (!msg.startsWith("unknown method")) {
|
|
323
|
+
debug === null || debug === void 0 ? void 0 : debug("daemon.status failed (".concat(msg, "); keeping live daemon"));
|
|
324
|
+
return [
|
|
325
|
+
2,
|
|
326
|
+
null
|
|
327
|
+
];
|
|
328
|
+
}
|
|
329
|
+
return [
|
|
330
|
+
3,
|
|
331
|
+
4
|
|
332
|
+
];
|
|
333
|
+
case 4:
|
|
334
|
+
if (running === bin.version) {
|
|
335
|
+
debug === null || debug === void 0 ? void 0 : debug("live devkitd ".concat(running, " matches bundled package"));
|
|
336
|
+
return [
|
|
337
|
+
2,
|
|
338
|
+
null
|
|
339
|
+
];
|
|
340
|
+
}
|
|
341
|
+
if (running && isNewerVersion(running, bin.version)) {
|
|
342
|
+
debug === null || debug === void 0 ? void 0 : debug("live devkitd ".concat(running, " is newer than bundled ").concat(bin.version, "; keeping it"));
|
|
343
|
+
return [
|
|
344
|
+
2,
|
|
345
|
+
null
|
|
346
|
+
];
|
|
347
|
+
}
|
|
348
|
+
label = running !== null && running !== void 0 ? running : "without build version";
|
|
349
|
+
self = readBinaryBuildVersion(bin.path);
|
|
350
|
+
if (self !== bin.version) {
|
|
351
|
+
debug === null || debug === void 0 ? void 0 : debug("stale devkitd ".concat(label, ", but bundled binary reports ").concat(self !== null && self !== void 0 ? self : "no build version", " ") + "(package ".concat(bin.version, "); not replacing"));
|
|
352
|
+
return [
|
|
353
|
+
2,
|
|
354
|
+
null
|
|
355
|
+
];
|
|
356
|
+
}
|
|
357
|
+
debug === null || debug === void 0 ? void 0 : debug("stale devkitd ".concat(label, " on socket; replacing with bundled ").concat(bin.version));
|
|
358
|
+
return [
|
|
359
|
+
2,
|
|
360
|
+
bin
|
|
361
|
+
];
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
})();
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Graceful replacement under the spawn lock: re-check (a concurrent CLI may
|
|
368
|
+
* have replaced it already), request `shutdown`, wait for the socket to go
|
|
369
|
+
* dead, then spawn the bundled binary. A daemon that won't exit is kept —
|
|
370
|
+
* never two daemons, never a dead socket.
|
|
371
|
+
*/ function replaceStaleDaemon(bin, waitMs, debug) {
|
|
372
|
+
return _async_to_generator(function() {
|
|
373
|
+
var sockPath, dir, lockPath, current;
|
|
374
|
+
return _ts_generator(this, function(_state) {
|
|
375
|
+
switch(_state.label){
|
|
376
|
+
case 0:
|
|
377
|
+
sockPath = daemonSocketPath();
|
|
378
|
+
dir = path.dirname(sockPath);
|
|
379
|
+
fs.mkdirSync(dir, {
|
|
380
|
+
recursive: true
|
|
381
|
+
});
|
|
382
|
+
lockPath = "".concat(sockPath, ".spawn.lock");
|
|
383
|
+
if (!acquireSpawnLock(lockPath, STALE_LOCK_MS)) {
|
|
384
|
+
// The holder may need waitMs to stop the old daemon plus waitMs to spawn.
|
|
385
|
+
debug === null || debug === void 0 ? void 0 : debug("another process is replacing devkitd; waiting for it");
|
|
386
|
+
return [
|
|
387
|
+
2,
|
|
388
|
+
waitForLockRelease(lockPath, 2 * waitMs + 1000, debug)
|
|
389
|
+
];
|
|
390
|
+
}
|
|
391
|
+
_state.label = 1;
|
|
392
|
+
case 1:
|
|
393
|
+
_state.trys.push([
|
|
394
|
+
1,
|
|
395
|
+
,
|
|
396
|
+
8,
|
|
397
|
+
9
|
|
242
398
|
]);
|
|
243
399
|
return [
|
|
244
400
|
4,
|
|
245
|
-
|
|
401
|
+
DaemonClient.tryConnect()
|
|
402
|
+
];
|
|
403
|
+
case 2:
|
|
404
|
+
current = _state.sent();
|
|
405
|
+
if (!current) return [
|
|
406
|
+
3,
|
|
407
|
+
6
|
|
408
|
+
];
|
|
409
|
+
return [
|
|
410
|
+
4,
|
|
411
|
+
staleDaemonBinary(current, bin, debug)
|
|
246
412
|
];
|
|
247
413
|
case 3:
|
|
414
|
+
if (!_state.sent()) return [
|
|
415
|
+
2,
|
|
416
|
+
current
|
|
417
|
+
];
|
|
418
|
+
return [
|
|
419
|
+
4,
|
|
420
|
+
current.request("shutdown", {}, 1000).catch(function() {})
|
|
421
|
+
];
|
|
422
|
+
case 4:
|
|
423
|
+
_state.sent();
|
|
424
|
+
current.close();
|
|
425
|
+
return [
|
|
426
|
+
4,
|
|
427
|
+
waitForSocketDead(waitMs)
|
|
428
|
+
];
|
|
429
|
+
case 5:
|
|
430
|
+
if (!_state.sent()) {
|
|
431
|
+
debug === null || debug === void 0 ? void 0 : debug("stale devkitd did not exit; continuing with it");
|
|
432
|
+
return [
|
|
433
|
+
2,
|
|
434
|
+
DaemonClient.tryConnect()
|
|
435
|
+
];
|
|
436
|
+
}
|
|
437
|
+
debug === null || debug === void 0 ? void 0 : debug("stale devkitd stopped");
|
|
438
|
+
_state.label = 6;
|
|
439
|
+
case 6:
|
|
440
|
+
if (!spawnDaemon(bin.path, dir, debug)) return [
|
|
441
|
+
2,
|
|
442
|
+
null
|
|
443
|
+
];
|
|
444
|
+
return [
|
|
445
|
+
4,
|
|
446
|
+
waitForSocket(waitMs, debug)
|
|
447
|
+
];
|
|
448
|
+
case 7:
|
|
248
449
|
return [
|
|
249
450
|
2,
|
|
250
451
|
_state.sent()
|
|
251
452
|
];
|
|
252
|
-
case
|
|
253
|
-
|
|
254
|
-
fs.unlinkSync(lockPath);
|
|
255
|
-
} catch (unused) {
|
|
256
|
-
/* already gone */ }
|
|
453
|
+
case 8:
|
|
454
|
+
releaseSpawnLock(lockPath);
|
|
257
455
|
return [
|
|
258
456
|
7
|
|
259
457
|
];
|
|
260
|
-
case
|
|
458
|
+
case 9:
|
|
261
459
|
return [
|
|
262
460
|
2
|
|
263
461
|
];
|
|
264
462
|
}
|
|
265
463
|
});
|
|
266
|
-
})
|
|
464
|
+
})();
|
|
465
|
+
}
|
|
466
|
+
/** True when `a` is a newer x.y.z release than `b`; false if either isn't one. */ function isNewerVersion(a, b) {
|
|
467
|
+
var parse = function(v) {
|
|
468
|
+
var _exec;
|
|
469
|
+
return (_exec = /^(\d+)\.(\d+)\.(\d+)/.exec(v)) === null || _exec === void 0 ? void 0 : _exec.slice(1).map(Number);
|
|
470
|
+
};
|
|
471
|
+
var pa = parse(a);
|
|
472
|
+
var pb = parse(b);
|
|
473
|
+
if (!pa || !pb) return false;
|
|
474
|
+
for(var i = 0; i < 3; i++)if (pa[i] !== pb[i]) return pa[i] > pb[i];
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
function spawnDaemon(bin, dir, debug) {
|
|
478
|
+
try {
|
|
479
|
+
var logPath = path.join(dir, "daemon.log");
|
|
480
|
+
capDaemonLog(logPath);
|
|
481
|
+
var logFd = fs.openSync(logPath, "a");
|
|
482
|
+
var child = spawn(bin, [
|
|
483
|
+
"serve"
|
|
484
|
+
], {
|
|
485
|
+
detached: true,
|
|
486
|
+
stdio: [
|
|
487
|
+
"ignore",
|
|
488
|
+
logFd,
|
|
489
|
+
logFd
|
|
490
|
+
]
|
|
491
|
+
});
|
|
492
|
+
child.unref();
|
|
493
|
+
fs.closeSync(logFd);
|
|
494
|
+
debug === null || debug === void 0 ? void 0 : debug("spawned devkitd (pid ".concat(child.pid, ") from ").concat(bin, "; waiting for socket"));
|
|
495
|
+
return true;
|
|
496
|
+
} catch (unused) {
|
|
497
|
+
debug === null || debug === void 0 ? void 0 : debug("devkitd spawn failed");
|
|
498
|
+
return false;
|
|
499
|
+
}
|
|
267
500
|
}
|
|
268
501
|
/** daemon.log is append-only per spawn — rotate to .1 past 1 MiB. */ var MAX_LOG_BYTES = 1 << 20;
|
|
269
502
|
function capDaemonLog(logPath) {
|
|
@@ -274,6 +507,12 @@ function capDaemonLog(logPath) {
|
|
|
274
507
|
} catch (unused) {
|
|
275
508
|
/* missing or unwritable — the open() caller surfaces real problems */ }
|
|
276
509
|
}
|
|
510
|
+
function releaseSpawnLock(lockPath) {
|
|
511
|
+
try {
|
|
512
|
+
fs.unlinkSync(lockPath);
|
|
513
|
+
} catch (unused) {
|
|
514
|
+
/* already gone */ }
|
|
515
|
+
}
|
|
277
516
|
function acquireSpawnLock(lockPath, staleMs) {
|
|
278
517
|
try {
|
|
279
518
|
fs.closeSync(fs.openSync(lockPath, "wx", 384));
|
|
@@ -340,3 +579,89 @@ function waitForSocket(waitMs, debug) {
|
|
|
340
579
|
});
|
|
341
580
|
})();
|
|
342
581
|
}
|
|
582
|
+
function waitForSocketDead(waitMs) {
|
|
583
|
+
return _async_to_generator(function() {
|
|
584
|
+
var deadline, client;
|
|
585
|
+
return _ts_generator(this, function(_state) {
|
|
586
|
+
switch(_state.label){
|
|
587
|
+
case 0:
|
|
588
|
+
deadline = Date.now() + waitMs;
|
|
589
|
+
_state.label = 1;
|
|
590
|
+
case 1:
|
|
591
|
+
if (!(Date.now() < deadline)) return [
|
|
592
|
+
3,
|
|
593
|
+
4
|
|
594
|
+
];
|
|
595
|
+
return [
|
|
596
|
+
4,
|
|
597
|
+
DaemonClient.tryConnect()
|
|
598
|
+
];
|
|
599
|
+
case 2:
|
|
600
|
+
client = _state.sent();
|
|
601
|
+
if (!client) return [
|
|
602
|
+
2,
|
|
603
|
+
true
|
|
604
|
+
];
|
|
605
|
+
client.close();
|
|
606
|
+
return [
|
|
607
|
+
4,
|
|
608
|
+
new Promise(function(r) {
|
|
609
|
+
return setTimeout(r, 50);
|
|
610
|
+
})
|
|
611
|
+
];
|
|
612
|
+
case 3:
|
|
613
|
+
_state.sent();
|
|
614
|
+
return [
|
|
615
|
+
3,
|
|
616
|
+
1
|
|
617
|
+
];
|
|
618
|
+
case 4:
|
|
619
|
+
return [
|
|
620
|
+
2,
|
|
621
|
+
false
|
|
622
|
+
];
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
})();
|
|
626
|
+
}
|
|
627
|
+
function waitForLockRelease(lockPath, waitMs, debug) {
|
|
628
|
+
return _async_to_generator(function() {
|
|
629
|
+
var deadline, client;
|
|
630
|
+
return _ts_generator(this, function(_state) {
|
|
631
|
+
switch(_state.label){
|
|
632
|
+
case 0:
|
|
633
|
+
deadline = Date.now() + waitMs;
|
|
634
|
+
_state.label = 1;
|
|
635
|
+
case 1:
|
|
636
|
+
if (!(fs.existsSync(lockPath) && Date.now() < deadline)) return [
|
|
637
|
+
3,
|
|
638
|
+
3
|
|
639
|
+
];
|
|
640
|
+
return [
|
|
641
|
+
4,
|
|
642
|
+
new Promise(function(r) {
|
|
643
|
+
return setTimeout(r, 50);
|
|
644
|
+
})
|
|
645
|
+
];
|
|
646
|
+
case 2:
|
|
647
|
+
_state.sent();
|
|
648
|
+
return [
|
|
649
|
+
3,
|
|
650
|
+
1
|
|
651
|
+
];
|
|
652
|
+
case 3:
|
|
653
|
+
return [
|
|
654
|
+
4,
|
|
655
|
+
DaemonClient.tryConnect()
|
|
656
|
+
];
|
|
657
|
+
case 4:
|
|
658
|
+
client = _state.sent();
|
|
659
|
+
debug === null || debug === void 0 ? void 0 : debug(client ? "devkitd socket ready" : "no devkitd after waiting for replacement");
|
|
660
|
+
return [
|
|
661
|
+
2,
|
|
662
|
+
client
|
|
663
|
+
];
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
})();
|
|
667
|
+
}
|
package/dist/binary.d.ts
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
export interface DaemonBinary {
|
|
2
|
+
path: string;
|
|
3
|
+
/**
|
|
4
|
+
* Platform package version — the release this CLI expects to be serving
|
|
5
|
+
* the socket. Only known for platform-package installs; other sources skip the
|
|
6
|
+
* stale-daemon check.
|
|
7
|
+
*/
|
|
8
|
+
version?: string;
|
|
9
|
+
}
|
|
10
|
+
/** Path-only view of resolveDaemonBinaryInfo. */
|
|
11
|
+
export declare function resolveDaemonBinary(onDebug?: (msg: string) => void): string | null;
|
|
12
|
+
/**
|
|
13
|
+
* Ask the binary which release it is (`devkitd version`). Null when the
|
|
14
|
+
* build is unstamped or the binary predates the command. Spawns a process —
|
|
15
|
+
* only called on the rare version-mismatch path.
|
|
16
|
+
*/
|
|
17
|
+
export declare function readBinaryBuildVersion(bin: string): string | null;
|
|
1
18
|
/**
|
|
2
19
|
* Locate the devkitd binary. Resolution order:
|
|
3
20
|
* 1. DEVKITD_BIN env override (AI_DEVKITD_BIN accepted as a deprecated alias)
|
|
@@ -7,5 +24,5 @@
|
|
|
7
24
|
* Returns null when nothing is found — callers
|
|
8
25
|
* must degrade gracefully.
|
|
9
26
|
*/
|
|
10
|
-
export declare function
|
|
27
|
+
export declare function resolveDaemonBinaryInfo(onDebug?: (msg: string) => void): DaemonBinary | null;
|
|
11
28
|
//# sourceMappingURL=binary.d.ts.map
|
package/dist/binary.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../src/binary.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../src/binary.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,iDAAiD;AACjD,wBAAgB,mBAAmB,CAAC,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,GAAG,IAAI,CAElF;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CASjE;AAED;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GAAG,YAAY,GAAG,IAAI,CAqE5F"}
|
package/dist/binary.js
CHANGED
|
@@ -2,7 +2,32 @@ import * as fs from "node:fs";
|
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
import * as os from "node:os";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
|
+
import { spawnSync } from "node:child_process";
|
|
5
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
+
/** Path-only view of resolveDaemonBinaryInfo. */ export function resolveDaemonBinary(onDebug) {
|
|
8
|
+
var _ref;
|
|
9
|
+
var _resolveDaemonBinaryInfo;
|
|
10
|
+
return (_ref = (_resolveDaemonBinaryInfo = resolveDaemonBinaryInfo(onDebug)) === null || _resolveDaemonBinaryInfo === void 0 ? void 0 : _resolveDaemonBinaryInfo.path) !== null && _ref !== void 0 ? _ref : null;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Ask the binary which release it is (`devkitd version`). Null when the
|
|
14
|
+
* build is unstamped or the binary predates the command. Spawns a process —
|
|
15
|
+
* only called on the rare version-mismatch path.
|
|
16
|
+
*/ export function readBinaryBuildVersion(bin) {
|
|
17
|
+
try {
|
|
18
|
+
var res = spawnSync(bin, [
|
|
19
|
+
"version"
|
|
20
|
+
], {
|
|
21
|
+
encoding: "utf8",
|
|
22
|
+
timeout: 2000
|
|
23
|
+
});
|
|
24
|
+
if (res.status !== 0) return null;
|
|
25
|
+
var v = JSON.parse(res.stdout.trim()).buildVersion;
|
|
26
|
+
return typeof v === "string" ? v : null;
|
|
27
|
+
} catch (unused) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
6
31
|
/**
|
|
7
32
|
* Locate the devkitd binary. Resolution order:
|
|
8
33
|
* 1. DEVKITD_BIN env override (AI_DEVKITD_BIN accepted as a deprecated alias)
|
|
@@ -11,12 +36,14 @@ import { fileURLToPath } from "node:url";
|
|
|
11
36
|
* 4. rust/target/{release,debug}/devkitd relative to the monorepo (dev)
|
|
12
37
|
* Returns null when nothing is found — callers
|
|
13
38
|
* must degrade gracefully.
|
|
14
|
-
*/ export function
|
|
39
|
+
*/ export function resolveDaemonBinaryInfo(onDebug) {
|
|
15
40
|
var _process_env_DEVKITD_BIN;
|
|
16
41
|
var env = (_process_env_DEVKITD_BIN = process.env.DEVKITD_BIN) !== null && _process_env_DEVKITD_BIN !== void 0 ? _process_env_DEVKITD_BIN : process.env.AI_DEVKITD_BIN;
|
|
17
42
|
if (env && fs.existsSync(env)) {
|
|
18
43
|
onDebug === null || onDebug === void 0 ? void 0 : onDebug("devkitd binary from env override: ".concat(env));
|
|
19
|
-
return
|
|
44
|
+
return {
|
|
45
|
+
path: env
|
|
46
|
+
};
|
|
20
47
|
}
|
|
21
48
|
var plats = {
|
|
22
49
|
linux: "linux",
|
|
@@ -35,8 +62,13 @@ import { fileURLToPath } from "node:url";
|
|
|
35
62
|
var pkgJson = req.resolve("@ai-devkit/devkitd-".concat(plat, "-").concat(arch, "/package.json"));
|
|
36
63
|
var candidate = path.join(path.dirname(pkgJson), process.platform === "win32" ? "devkitd.exe" : "devkitd");
|
|
37
64
|
if (fs.existsSync(candidate)) {
|
|
38
|
-
|
|
39
|
-
|
|
65
|
+
var version = JSON.parse(fs.readFileSync(pkgJson, "utf8")).version;
|
|
66
|
+
onDebug === null || onDebug === void 0 ? void 0 : onDebug("devkitd binary from platform package ".concat(version, ": ").concat(candidate));
|
|
67
|
+
// 0.0.0 is the unstamped in-repo manifest, not a release.
|
|
68
|
+
return {
|
|
69
|
+
path: candidate,
|
|
70
|
+
version: typeof version === "string" && version !== "0.0.0" ? version : undefined
|
|
71
|
+
};
|
|
40
72
|
}
|
|
41
73
|
} catch (unused) {
|
|
42
74
|
/* package not installed */ }
|
|
@@ -44,7 +76,9 @@ import { fileURLToPath } from "node:url";
|
|
|
44
76
|
var home = path.join(os.homedir(), ".ai-devkit", "bin", "devkitd");
|
|
45
77
|
if (fs.existsSync(home)) {
|
|
46
78
|
onDebug === null || onDebug === void 0 ? void 0 : onDebug("devkitd binary from ~/.ai-devkit/bin: ".concat(home));
|
|
47
|
-
return
|
|
79
|
+
return {
|
|
80
|
+
path: home
|
|
81
|
+
};
|
|
48
82
|
}
|
|
49
83
|
// Dev checkout: walk up to the nearest rust/target/<profile>/devkitd and
|
|
50
84
|
// pick the freshest profile — a stale release build otherwise shadows a
|
|
@@ -70,7 +104,9 @@ import { fileURLToPath } from "node:url";
|
|
|
70
104
|
}
|
|
71
105
|
if (best) {
|
|
72
106
|
onDebug === null || onDebug === void 0 ? void 0 : onDebug("devkitd binary from dev checkout: ".concat(best.file));
|
|
73
|
-
return
|
|
107
|
+
return {
|
|
108
|
+
path: best.file
|
|
109
|
+
};
|
|
74
110
|
}
|
|
75
111
|
var parent = path.dirname(dir);
|
|
76
112
|
if (parent === dir) break;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { AgentStartParams } from "./gen/AgentStartParams.js";
|
|
2
2
|
import type { AgentStartResult } from "./gen/AgentStartResult.js";
|
|
3
|
+
import type { AgentLookupResult } from "./gen/AgentLookupResult.js";
|
|
4
|
+
import type { AgentUnregisterResult } from "./gen/AgentUnregisterResult.js";
|
|
5
|
+
import type { AgentRenameResult } from "./gen/AgentRenameResult.js";
|
|
3
6
|
import type { EnrichedAgent } from "./gen/EnrichedAgent.js";
|
|
4
7
|
import type { Event } from "./gen/Event.js";
|
|
5
8
|
/** Wire event pushed by the daemon — generated from devkit-core via ts-rs. */
|
|
@@ -53,6 +56,27 @@ export declare class DaemonClient {
|
|
|
53
56
|
* timeout covers the pid-resolution deadline plus a margin.
|
|
54
57
|
*/
|
|
55
58
|
startAgent(params: AgentStartParams): Promise<AgentStartResult>;
|
|
59
|
+
/**
|
|
60
|
+
* Resolve an exact registered name to its location data (pid, runtime,
|
|
61
|
+
* runtimeRef, cwd, liveness). Dead/tombstoned registrations resolve with
|
|
62
|
+
* `alive: false`; unknown names reject with `DaemonRpcError` code
|
|
63
|
+
* `not_found`. Generic on purpose — kill, send, open, channel and console
|
|
64
|
+
* all drive their runtime ops from this payload.
|
|
65
|
+
*/
|
|
66
|
+
lookupAgent(name: string): Promise<AgentLookupResult>;
|
|
67
|
+
/**
|
|
68
|
+
* Remove a name's registration outright (no tombstone) so the name is free
|
|
69
|
+
* for the next start. Does not touch the runtime — teardown is the
|
|
70
|
+
* caller's job, using `lookupAgent` data. Rejects `not_found` if unknown.
|
|
71
|
+
*/
|
|
72
|
+
unregisterAgent(name: string): Promise<AgentUnregisterResult>;
|
|
73
|
+
/**
|
|
74
|
+
* Rename a registered agent and its runtime handle (tmux session / live
|
|
75
|
+
* herdr agent) atomically, daemon-side. Rejects coded `not_found`,
|
|
76
|
+
* `name_in_use`, `runtime_rename_failed` (registry left unchanged),
|
|
77
|
+
* `runtime_unavailable`, `invalid_params`.
|
|
78
|
+
*/
|
|
79
|
+
renameAgent(from: string, to: string): Promise<AgentRenameResult>;
|
|
56
80
|
private onData;
|
|
57
81
|
/** Reject every in-flight request and mark the socket dead. Idempotent. */
|
|
58
82
|
private onSocketDead;
|