@mindexec/cli 0.2.122 → 0.2.123

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 (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +119 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mindexec/cli",
3
- "version": "0.2.122",
3
+ "version": "0.2.123",
4
4
  "description": "MindExec local runtime and bridge CLI",
5
5
  "main": "server.js",
6
6
  "type": "module",
package/server.js CHANGED
@@ -2909,12 +2909,17 @@ const REMOTE_AGENT_DEFAULT_ENGINE = 'auto';
2909
2909
  const REMOTE_AGENT_SYNC_REPORT_LOG_REPEAT_MS = 30000;
2910
2910
  const REMOTE_AGENT_RACE_START_STAGGER_MS = 120;
2911
2911
  const REMOTE_AGENT_MAX_PARALLEL_CANDIDATES = 6;
2912
+ const REMOTE_AGENT_RECENT_MANAGER_CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000;
2913
+ const REMOTE_AGENT_RECENT_MANAGER_CACHE_LIMIT = 64;
2914
+ const REMOTE_AGENT_RECENT_MANAGER_DEFAULT_KEY = 'default';
2912
2915
  let remoteAgentState = createRemoteAgentIdleState();
2913
2916
  let remoteAgentSyncReportState = null;
2914
2917
  let remoteAgentSyncReportLogKey = '';
2915
2918
  let remoteAgentSyncReportLogAt = 0;
2916
2919
  let remoteAgentConnectPromise = null;
2917
2920
  const remoteAgentRecentSuccessfulManagers = new Map();
2921
+ let remoteAgentRecentSuccessfulManagersLoaded = false;
2922
+ let remoteAgentRecentSuccessfulManagersSavePromise = null;
2918
2923
 
2919
2924
  function createRemoteAgentIdleState(overrides = {}) {
2920
2925
  return {
@@ -3216,7 +3221,104 @@ function createRemoteAgentConnectionKey(manager, leaseId) {
3216
3221
  }
3217
3222
 
3218
3223
  function getRemoteAgentRecentManagerKey(leaseId = '') {
3219
- return safeRemoteAgentField(leaseId || 'default', 128) || 'default';
3224
+ return safeRemoteAgentField(leaseId || REMOTE_AGENT_RECENT_MANAGER_DEFAULT_KEY, 128)
3225
+ || REMOTE_AGENT_RECENT_MANAGER_DEFAULT_KEY;
3226
+ }
3227
+
3228
+ function getRemoteAgentRecentManagerCachePath() {
3229
+ return path.join(getDataRoot(workspacePath), 'cache', 'remote-agent-recent-managers.json');
3230
+ }
3231
+
3232
+ function normalizeRemoteAgentRecentManagerEntry(entry) {
3233
+ const endpoint = normalizeRemoteManagerEndpoint(entry?.endpoint);
3234
+ const updatedAt = Number(entry?.updatedAt || 0);
3235
+ if (!endpoint || !Number.isFinite(updatedAt) || updatedAt <= 0) {
3236
+ return null;
3237
+ }
3238
+
3239
+ const now = Date.now();
3240
+ if (now - updatedAt > REMOTE_AGENT_RECENT_MANAGER_CACHE_TTL_MS) {
3241
+ return null;
3242
+ }
3243
+
3244
+ return {
3245
+ endpoint,
3246
+ updatedAt
3247
+ };
3248
+ }
3249
+
3250
+ function loadRemoteAgentRecentSuccessfulManagers() {
3251
+ if (remoteAgentRecentSuccessfulManagersLoaded) {
3252
+ return;
3253
+ }
3254
+
3255
+ remoteAgentRecentSuccessfulManagersLoaded = true;
3256
+ const cachePath = getRemoteAgentRecentManagerCachePath();
3257
+ let payload = null;
3258
+ try {
3259
+ payload = JSON.parse(readFileSync(cachePath, 'utf8'));
3260
+ } catch (err) {
3261
+ if (err?.code !== 'ENOENT') {
3262
+ logWarn('remote', `managed RemoteAgent recent manager cache ignored: ${err?.message || err}`);
3263
+ }
3264
+ return;
3265
+ }
3266
+
3267
+ const rawEntries = Array.isArray(payload?.entries)
3268
+ ? payload.entries
3269
+ : Object.entries(payload?.managers || {}).map(([key, value]) => ({ key, ...value }));
3270
+ let loaded = 0;
3271
+ for (const rawEntry of rawEntries) {
3272
+ if (loaded >= REMOTE_AGENT_RECENT_MANAGER_CACHE_LIMIT) {
3273
+ break;
3274
+ }
3275
+
3276
+ const key = getRemoteAgentRecentManagerKey(rawEntry?.key || rawEntry?.leaseId || '');
3277
+ const entry = normalizeRemoteAgentRecentManagerEntry(rawEntry);
3278
+ if (!entry) {
3279
+ continue;
3280
+ }
3281
+
3282
+ remoteAgentRecentSuccessfulManagers.set(key, entry);
3283
+ loaded += 1;
3284
+ }
3285
+ }
3286
+
3287
+ async function saveRemoteAgentRecentSuccessfulManagers() {
3288
+ const entries = Array.from(remoteAgentRecentSuccessfulManagers.entries())
3289
+ .map(([key, value]) => ({
3290
+ key,
3291
+ endpoint: normalizeRemoteManagerEndpoint(value?.endpoint),
3292
+ updatedAt: Number(value?.updatedAt || 0)
3293
+ }))
3294
+ .map(entry => ({
3295
+ ...entry,
3296
+ normalized: normalizeRemoteAgentRecentManagerEntry(entry)
3297
+ }))
3298
+ .filter(entry => entry.normalized)
3299
+ .sort((left, right) => Number(right.updatedAt || 0) - Number(left.updatedAt || 0))
3300
+ .slice(0, REMOTE_AGENT_RECENT_MANAGER_CACHE_LIMIT)
3301
+ .map(entry => ({
3302
+ key: entry.key,
3303
+ endpoint: entry.normalized.endpoint,
3304
+ updatedAt: entry.normalized.updatedAt
3305
+ }));
3306
+
3307
+ const payload = {
3308
+ version: 1,
3309
+ savedAt: new Date().toISOString(),
3310
+ entries
3311
+ };
3312
+ await writeFileAtomically(getRemoteAgentRecentManagerCachePath(), JSON.stringify(payload, null, 2), 'utf8');
3313
+ }
3314
+
3315
+ function scheduleRemoteAgentRecentSuccessfulManagersSave() {
3316
+ remoteAgentRecentSuccessfulManagersSavePromise = Promise.resolve(remoteAgentRecentSuccessfulManagersSavePromise)
3317
+ .catch(() => null)
3318
+ .then(() => saveRemoteAgentRecentSuccessfulManagers())
3319
+ .catch(err => {
3320
+ logWarn('remote', `managed RemoteAgent recent manager cache save failed: ${err?.message || err}`);
3321
+ });
3220
3322
  }
3221
3323
 
3222
3324
  function rememberRemoteAgentSuccessfulManager(manager, leaseId = '') {
@@ -3225,10 +3327,14 @@ function rememberRemoteAgentSuccessfulManager(manager, leaseId = '') {
3225
3327
  return;
3226
3328
  }
3227
3329
 
3228
- remoteAgentRecentSuccessfulManagers.set(getRemoteAgentRecentManagerKey(leaseId), {
3330
+ loadRemoteAgentRecentSuccessfulManagers();
3331
+ const entry = {
3229
3332
  endpoint,
3230
3333
  updatedAt: Date.now()
3231
- });
3334
+ };
3335
+ remoteAgentRecentSuccessfulManagers.set(getRemoteAgentRecentManagerKey(leaseId), entry);
3336
+ remoteAgentRecentSuccessfulManagers.set(REMOTE_AGENT_RECENT_MANAGER_DEFAULT_KEY, entry);
3337
+ scheduleRemoteAgentRecentSuccessfulManagersSave();
3232
3338
  }
3233
3339
 
3234
3340
  function prioritizeRemoteAgentManagers(managers, leaseId = '') {
@@ -3237,8 +3343,10 @@ function prioritizeRemoteAgentManagers(managers, leaseId = '') {
3237
3343
  return normalized;
3238
3344
  }
3239
3345
 
3240
- const recent = remoteAgentRecentSuccessfulManagers.get(getRemoteAgentRecentManagerKey(leaseId));
3241
- const recentEndpoint = normalizeRemoteManagerEndpoint(recent?.endpoint);
3346
+ loadRemoteAgentRecentSuccessfulManagers();
3347
+ const leaseRecent = remoteAgentRecentSuccessfulManagers.get(getRemoteAgentRecentManagerKey(leaseId));
3348
+ const defaultRecent = remoteAgentRecentSuccessfulManagers.get(REMOTE_AGENT_RECENT_MANAGER_DEFAULT_KEY);
3349
+ const recentEndpoint = normalizeRemoteManagerEndpoint(leaseRecent?.endpoint || defaultRecent?.endpoint);
3242
3350
  if (!recentEndpoint) {
3243
3351
  return normalized;
3244
3352
  }
@@ -10333,6 +10441,12 @@ async function shutdownBridge(signal) {
10333
10441
  // Ignore managed RemoteAgent close errors during shutdown
10334
10442
  }
10335
10443
 
10444
+ try {
10445
+ await remoteAgentRecentSuccessfulManagersSavePromise;
10446
+ } catch {
10447
+ // Ignore recent-manager cache save errors during shutdown
10448
+ }
10449
+
10336
10450
  try {
10337
10451
  await remoteHub.close();
10338
10452
  } catch {