ai-agentmanager 1.63.0 → 1.65.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/src/service.js CHANGED
@@ -43,6 +43,19 @@ export function passEnv(source = process.env) {
43
43
  return env;
44
44
  }
45
45
 
46
+ /**
47
+ * Whether the service file's daemon is the one this process talks to: the same home and port. A
48
+ * second daemon (another home or port, as in a trial or a test) is not the login service's.
49
+ */
50
+ export function serviceIsOurs(text, source = process.env) {
51
+ const of = (key) => new RegExp(`<key>${key}</key><string>([^<]*)</string>|^Environment=${key}=(.*)$`, 'm').exec(String(text ?? ''));
52
+ const unxml = (v) => v.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&');
53
+ return ['AGENTMGR_HOME', 'AGENTMGR_PORT'].every((key) => {
54
+ const m = of(key);
55
+ return (m ? unxml(m[1] ?? m[2]) : '') === (source[key] ?? '');
56
+ });
57
+ }
58
+
46
59
  const xml = (s) => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
47
60
 
48
61
  export function plist() {
@@ -128,18 +141,26 @@ export async function uninstall() {
128
141
  throw new Error(`Login service is not supported on ${process.platform}`);
129
142
  }
130
143
 
144
+ const ours = (file) => {
145
+ try {
146
+ return serviceIsOurs(fs.readFileSync(file, 'utf8'));
147
+ } catch {
148
+ return true; // loaded without a file we can read: as before
149
+ }
150
+ };
151
+
131
152
  export async function status() {
132
153
  if (process.platform === 'darwin') {
133
154
  const installed = fs.existsSync(PLIST);
134
155
  const loaded = await run('launchctl', ['print', `gui/${uid()}/${LABEL}`]).then(() => true, () => false);
135
- return { installed, loaded, file: PLIST };
156
+ return { installed, loaded, ours: ours(PLIST), file: PLIST };
136
157
  }
137
158
  if (process.platform === 'linux') {
138
159
  const installed = fs.existsSync(UNIT);
139
160
  const loaded = await run('systemctl', ['--user', 'is-active', UNIT_NAME]).then(() => true, () => false);
140
- return { installed, loaded, file: UNIT };
161
+ return { installed, loaded, ours: ours(UNIT), file: UNIT };
141
162
  }
142
- return { installed: false, loaded: false, file: null };
163
+ return { installed: false, loaded: false, ours: false, file: null };
143
164
  }
144
165
 
145
166
  /** Start the service-managed daemon now (after "agentmgr shutdown", launchd won't on its own). */