@yeaft/webchat-agent 1.0.41 → 1.0.43

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.
@@ -2,13 +2,24 @@ import { execFile, execFileSync, spawn } from 'child_process';
2
2
  import { writeFileSync, mkdirSync, existsSync, cpSync } from 'fs';
3
3
  import { join, dirname } from 'path';
4
4
  import { fileURLToPath } from 'url';
5
- import { platform } from 'os';
5
+ import { platform, homedir } from 'os';
6
6
  import ctx from '../context.js';
7
- import { getConfigDir } from '../service.js';
7
+ import { getConfigDir, getServiceName, getPm2AppName, getLaunchdPlistPath, DEFAULT_INSTANCE_ID } from '../service.js';
8
8
  import { sendToServer } from './buffer.js';
9
9
  import { stopAgentHeartbeat } from './heartbeat.js';
10
10
 
11
- const PM2_APP_NAME = 'yeaft-agent';
11
+ /**
12
+ * Resolve the local service instance id for this running agent. Each agent
13
+ * process is pinned to one instance (set in index.js from
14
+ * YEAFT_AGENT_INSTANCE / config), so the upgrade flow must target THAT
15
+ * instance's service unit — not the bare `yeaft-agent` default. Without this,
16
+ * a named instance (e.g. `server-e7a9eb`) installs the new package but its
17
+ * systemd unit `yeaft-agent@server-e7a9eb` is never restarted, leaving the
18
+ * agent permanently offline after a UI-triggered upgrade.
19
+ */
20
+ export function resolveInstanceId() {
21
+ return ctx.CONFIG?.instanceId || process.env.YEAFT_AGENT_INSTANCE || DEFAULT_INSTANCE_ID;
22
+ }
12
23
 
13
24
  // Derive absolute paths for npm/pm2 from current node executable.
14
25
  // In launchd/systemd environments, PATH may not include nvm/node dirs,
@@ -203,11 +214,12 @@ export async function handleUpgradeAgent() {
203
214
  });
204
215
 
205
216
  const isWindows = platform() === 'win32';
217
+ const instanceId = resolveInstanceId();
206
218
 
207
219
  if (isWindows) {
208
- spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion);
220
+ spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion, instanceId);
209
221
  } else {
210
- spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion);
222
+ spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion, instanceId);
211
223
  }
212
224
 
213
225
  // On PM2: delete the app BEFORE exiting so PM2 won't auto-restart the old version.
@@ -215,7 +227,7 @@ export async function handleUpgradeAgent() {
215
227
  const isPm2 = !!process.env.pm_id;
216
228
  if (isPm2) {
217
229
  try {
218
- execFileSync(pm2Path, ['delete', PM2_APP_NAME], { stdio: 'pipe', env: safeEnv, ...shellOpt });
230
+ execFileSync(pm2Path, ['delete', getPm2AppName(instanceId)], { stdio: 'pipe', env: safeEnv, ...shellOpt });
219
231
  console.log(`[Agent] PM2 app deleted to prevent auto-restart during upgrade`);
220
232
  } catch {
221
233
  console.log(`[Agent] PM2 delete skipped (app may not be registered)`);
@@ -230,9 +242,9 @@ export async function handleUpgradeAgent() {
230
242
  }
231
243
  }
232
244
 
233
- function spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion) {
245
+ function spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion, instanceId = DEFAULT_INSTANCE_ID) {
234
246
  const pid = process.pid;
235
- const configDir = getConfigDir();
247
+ const configDir = getConfigDir(instanceId);
236
248
  mkdirSync(configDir, { recursive: true });
237
249
  const logDir = join(configDir, 'logs');
238
250
  mkdirSync(logDir, { recursive: true });
@@ -355,13 +367,45 @@ function spawnWindowsUpgradeScript(pkgName, installDir, isGlobalInstall, latestV
355
367
  sendToServer({ type: 'upgrade_agent_ack', success: true, version: latestVersion, pendingRestart: true });
356
368
  }
357
369
 
358
- function spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion) {
359
- const pid = process.pid;
360
- const configDir = getConfigDir();
361
- mkdirSync(configDir, { recursive: true });
370
+ /**
371
+ * Build the Unix (systemd/launchd) upgrade shell script as a string.
372
+ *
373
+ * Side-effect-free apart from reading the environment FS (`existsSync` to
374
+ * detect which service manager is installed) — and crucially it does NOT
375
+ * spawn, so the generated unit names can be asserted in unit tests. The
376
+ * service-manager target is resolved from `instanceId` via the shared helpers
377
+ * (`getServiceName` / `getLaunchdPlistPath`), so a named instance restarts
378
+ * `yeaft-agent@<id>` instead of the bare `yeaft-agent` default.
379
+ *
380
+ * @param {object} opts
381
+ * @param {string} opts.pkgName npm package name
382
+ * @param {string} opts.installDir install dir (local install) — used as cwd
383
+ * @param {boolean} opts.isGlobalInstall global vs local npm install
384
+ * @param {number} opts.pid pid of the exiting agent to wait on
385
+ * @param {string} opts.configDir instance config dir (holds logs/upgrade.log)
386
+ * @param {string} opts.npmPath absolute npm path
387
+ * @param {string} opts.safePath PATH with nodeBinDir prepended
388
+ * @param {string} opts.instanceId local service instance id
389
+ * @param {boolean} opts.isDarwin whether the platform is macOS
390
+ * @returns {string} the upgrade.sh contents
391
+ */
392
+ export function buildUnixUpgradeScript({
393
+ pkgName,
394
+ installDir,
395
+ isGlobalInstall,
396
+ pid,
397
+ configDir,
398
+ npmPath,
399
+ safePath,
400
+ instanceId = DEFAULT_INSTANCE_ID,
401
+ isDarwin = platform() === 'darwin',
402
+ }) {
362
403
  const shPath = join(configDir, 'upgrade.sh');
363
- const isSystemd = existsSync(join(process.env.HOME || '', '.config', 'systemd', 'user', 'yeaft-agent.service'));
364
- const isLaunchd = platform() === 'darwin' && existsSync(join(process.env.HOME || '', 'Library', 'LaunchAgents', 'com.yeaft.agent.plist'));
404
+ const serviceName = getServiceName(instanceId);
405
+ const systemdUnitPath = join(homedir(), '.config', 'systemd', 'user', `${serviceName}.service`);
406
+ const isSystemd = existsSync(systemdUnitPath);
407
+ const plistPath = getLaunchdPlistPath(instanceId);
408
+ const isLaunchd = isDarwin && existsSync(plistPath);
365
409
  const cwd = isGlobalInstall ? undefined : installDir;
366
410
 
367
411
  const shLines = [
@@ -395,12 +439,11 @@ function spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVers
395
439
  if (isSystemd) {
396
440
  shLines.push(
397
441
  '# Stop systemd service to prevent restart loop',
398
- 'systemctl --user stop yeaft-agent 2>/dev/null',
442
+ `systemctl --user stop "${serviceName}" 2>/dev/null`,
399
443
  'sleep 1',
400
444
  '',
401
445
  );
402
446
  } else if (isLaunchd) {
403
- const plistPath = join(process.env.HOME || '', 'Library', 'LaunchAgents', 'com.yeaft.agent.plist');
404
447
  shLines.push(
405
448
  '# Unload launchd service to prevent restart loop',
406
449
  `launchctl unload "${plistPath}" 2>/dev/null`,
@@ -430,11 +473,10 @@ function spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVers
430
473
  if (isSystemd) {
431
474
  shLines.push(
432
475
  '# Restart systemd service',
433
- 'systemctl --user start yeaft-agent',
476
+ `systemctl --user start "${serviceName}"`,
434
477
  'echo "[Upgrade] Service restarted via systemd"',
435
478
  );
436
479
  } else if (isLaunchd) {
437
- const plistPath = join(process.env.HOME || '', 'Library', 'LaunchAgents', 'com.yeaft.agent.plist');
438
480
  shLines.push(
439
481
  '# Reload launchd service',
440
482
  `launchctl load "${plistPath}"`,
@@ -445,7 +487,26 @@ function spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVers
445
487
  // 清理脚本自身
446
488
  shLines.push('', `rm -f "${shPath}"`);
447
489
 
448
- writeFileSync(shPath, shLines.join('\n'), { mode: 0o755 });
490
+ return shLines.join('\n');
491
+ }
492
+
493
+ function spawnUnixUpgradeScript(pkgName, installDir, isGlobalInstall, latestVersion, instanceId = DEFAULT_INSTANCE_ID) {
494
+ const configDir = getConfigDir(instanceId);
495
+ mkdirSync(configDir, { recursive: true });
496
+ const shPath = join(configDir, 'upgrade.sh');
497
+
498
+ const script = buildUnixUpgradeScript({
499
+ pkgName,
500
+ installDir,
501
+ isGlobalInstall,
502
+ pid: process.pid,
503
+ configDir,
504
+ npmPath,
505
+ safePath,
506
+ instanceId,
507
+ });
508
+
509
+ writeFileSync(shPath, script, { mode: 0o755 });
449
510
  const child = spawn('bash', [shPath], {
450
511
  detached: true,
451
512
  stdio: 'ignore',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yeaft/webchat-agent",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "Remote worker agent for Yeaft Web Code Agent — connects the native Yeaft engine, CLI providers, and workbench tools",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/service/index.js CHANGED
@@ -34,6 +34,9 @@ export {
34
34
  getLaunchdLabel,
35
35
  getDefaultYeaftDir,
36
36
  } from './config.js';
37
+ // Re-export the launchd plist-path resolver so consumers (e.g. the upgrade
38
+ // flow) reach it via the service barrel instead of deep-importing macos.js.
39
+ export { getLaunchdPlistPath } from './macos.js';
37
40
 
38
41
  const os = platform();
39
42
 
package/service.js CHANGED
@@ -25,6 +25,7 @@ export {
25
25
  getServiceName,
26
26
  getPm2AppName,
27
27
  getLaunchdLabel,
28
+ getLaunchdPlistPath,
28
29
  getDefaultYeaftDir,
29
30
  install,
30
31
  uninstall,
package/yeaft/engine.js CHANGED
@@ -1665,7 +1665,7 @@ export class Engine {
1665
1665
 
1666
1666
  try {
1667
1667
  this.#currentThreadId = threadId || MAIN_THREAD_ID;
1668
- yield* this.#runQuery({ prompt: effectivePrompt, promptParts: effectivePromptParts, messages, signal: runSignal, userEffort: effectiveUserEffort, scenario, vpPersona, router, senderVpId, inboundEnvelope, taskId, taskMembers, sessionId, sessionMembers, sessionTopics, vpPlan, sessionAnnouncement, workDir, userAlreadyPersisted, getCurrentTodos, setCurrentTodos, threadId: this.#currentThreadId, drainPendingUserMessages, collabToolPolicy: effectiveCollabToolPolicy, explicitSkillName: parsedSkill.skillName });
1668
+ yield* this.#runQuery({ prompt: effectivePrompt, promptParts: effectivePromptParts, messages, signal: runSignal, userEffort: effectiveUserEffort, scenario, vpPersona, router, senderVpId, inboundEnvelope, taskId, taskMembers, sessionId, sessionMembers, sessionTopics, vpPlan, sessionAnnouncement, workDir, userAlreadyPersisted, getCurrentTodos, setCurrentTodos, threadId: this.#currentThreadId, vpTurnId, drainPendingUserMessages, collabToolPolicy: effectiveCollabToolPolicy, explicitSkillName: parsedSkill.skillName });
1669
1669
  } finally {
1670
1670
  if (signal) {
1671
1671
  try { signal.removeEventListener('abort', onExternalAbort); } catch { /* ignore */ }
@@ -1709,7 +1709,7 @@ export class Engine {
1709
1709
  * in a try/finally without indenting the whole loop.
1710
1710
  * @private
1711
1711
  */
1712
- async *#runQuery({ prompt, promptParts = null, messages, signal, userEffort = null, scenario = 'chat', vpPersona, router, senderVpId, inboundEnvelope, taskId, taskMembers, sessionId, sessionMembers, sessionTopics = null, vpPlan, sessionAnnouncement, workDir, userAlreadyPersisted = false, getCurrentTodos = null, setCurrentTodos = null, threadId = MAIN_THREAD_ID, drainPendingUserMessages = null, collabToolPolicy = null, explicitSkillName = null }) {
1712
+ async *#runQuery({ prompt, promptParts = null, messages, signal, userEffort = null, scenario = 'chat', vpPersona, router, senderVpId, inboundEnvelope, taskId, taskMembers, sessionId, sessionMembers, sessionTopics = null, vpPlan, sessionAnnouncement, workDir, userAlreadyPersisted = false, getCurrentTodos = null, setCurrentTodos = null, threadId = MAIN_THREAD_ID, vpTurnId = null, drainPendingUserMessages = null, collabToolPolicy = null, explicitSkillName = null }) {
1713
1713
 
1714
1714
  const effectiveCollabToolPolicy = collabToolPolicy === COLLAB_TOOL_POLICY.SINGLE_VP || collabToolPolicy === COLLAB_TOOL_POLICY.MULTI_VP
1715
1715
  ? collabToolPolicy