@agent-link/agent 0.1.296 → 0.1.297
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/cli.js +55 -1
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +2 -0
- package/dist/config.js.map +1 -1
- package/dist/daemon.js +1 -1
- package/dist/daemon.js.map +1 -1
- package/dist/external-upgrade.d.ts +22 -0
- package/dist/external-upgrade.js +186 -0
- package/dist/external-upgrade.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/dist/local-control-client.d.ts +4 -0
- package/dist/local-control-client.js.map +1 -1
- package/dist/local-control-http.js +28 -4
- package/dist/local-control-http.js.map +1 -1
- package/dist/local-control.d.ts +7 -2
- package/dist/local-control.js +12 -0
- package/dist/local-control.js.map +1 -1
- package/dist/process-identity.d.ts +30 -0
- package/dist/process-identity.js +99 -0
- package/dist/process-identity.js.map +1 -0
- package/dist/upgrade-job.d.ts +51 -0
- package/dist/upgrade-job.js +233 -20
- package/dist/upgrade-job.js.map +1 -1
- package/dist/upgrade-lock.d.ts +25 -0
- package/dist/upgrade-lock.js +207 -0
- package/dist/upgrade-lock.js.map +1 -0
- package/dist/upgrade-persistence.d.ts +1 -0
- package/dist/upgrade-persistence.js +25 -0
- package/dist/upgrade-persistence.js.map +1 -0
- package/dist/worker-host.d.ts +38 -0
- package/dist/worker-host.js +172 -0
- package/dist/worker-host.js.map +1 -0
- package/dist/worker-shutdown-authorization.d.ts +7 -0
- package/dist/worker-shutdown-authorization.js +71 -0
- package/dist/worker-shutdown-authorization.js.map +1 -0
- package/package.json +3 -2
- package/worker/worker.cjs +612 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { spawn } from 'child_process';
|
|
2
|
+
import { chmodSync, closeSync, mkdirSync, openSync, rmSync, writeFileSync, } from 'fs';
|
|
3
|
+
import { dirname, join, resolve } from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import { CONFIG_DIR } from './config.js';
|
|
6
|
+
import { getExternalUpgradeJobDirectory } from './upgrade-job.js';
|
|
7
|
+
function escapeXml(value) {
|
|
8
|
+
return value
|
|
9
|
+
.replace(/&/g, '&')
|
|
10
|
+
.replace(/</g, '<')
|
|
11
|
+
.replace(/>/g, '>')
|
|
12
|
+
.replace(/"/g, '"')
|
|
13
|
+
.replace(/'/g, ''');
|
|
14
|
+
}
|
|
15
|
+
function defaultSpawn(command) {
|
|
16
|
+
return spawn(command.executable, command.args, command.options);
|
|
17
|
+
}
|
|
18
|
+
function execute(command, deps) {
|
|
19
|
+
return (deps.spawnProcess ?? defaultSpawn)(command);
|
|
20
|
+
}
|
|
21
|
+
function observeSpawn(child) {
|
|
22
|
+
if (!('once' in child) || typeof child.once !== 'function')
|
|
23
|
+
return Promise.resolve();
|
|
24
|
+
return new Promise((resolveSpawn, rejectSpawn) => {
|
|
25
|
+
child.once('spawn', resolveSpawn);
|
|
26
|
+
child.once('error', rejectSpawn);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export function getPackagedWorkerPath() {
|
|
30
|
+
return resolve(dirname(fileURLToPath(import.meta.url)), '..', 'worker', 'worker.cjs');
|
|
31
|
+
}
|
|
32
|
+
export function startUpgradeWorker(input, deps = {}) {
|
|
33
|
+
const platform = deps.platform ?? process.platform;
|
|
34
|
+
const nodePath = deps.nodePath ?? process.execPath;
|
|
35
|
+
const jobDirectory = getExternalUpgradeJobDirectory(input.jobId);
|
|
36
|
+
const workerLog = join(jobDirectory, 'worker.log');
|
|
37
|
+
mkdirSync(jobDirectory, { recursive: true, mode: 0o700 });
|
|
38
|
+
const environment = {
|
|
39
|
+
...process.env,
|
|
40
|
+
AGENTLINK_HOME: CONFIG_DIR,
|
|
41
|
+
};
|
|
42
|
+
if (input.hostType === 'standalone'
|
|
43
|
+
|| input.hostType === 'windows-startup'
|
|
44
|
+
|| platform === 'win32') {
|
|
45
|
+
const output = openSync(workerLog, 'a', 0o600);
|
|
46
|
+
const command = {
|
|
47
|
+
executable: nodePath,
|
|
48
|
+
args: [input.workerPath, '--job', input.jobPath],
|
|
49
|
+
options: {
|
|
50
|
+
cwd: jobDirectory,
|
|
51
|
+
env: environment,
|
|
52
|
+
detached: true,
|
|
53
|
+
stdio: ['ignore', output, output],
|
|
54
|
+
windowsHide: true,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
try {
|
|
58
|
+
const child = execute(command, deps);
|
|
59
|
+
const started = observeSpawn(child);
|
|
60
|
+
child.unref?.();
|
|
61
|
+
return { kind: 'detached', pid: child.pid, command, started };
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
closeSync(output);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (input.hostType === 'systemd') {
|
|
68
|
+
const unit = `agentlink-upgrade-${input.jobId}`;
|
|
69
|
+
const command = {
|
|
70
|
+
executable: 'systemd-run',
|
|
71
|
+
args: [
|
|
72
|
+
'--user',
|
|
73
|
+
`--unit=${unit}`,
|
|
74
|
+
'--collect',
|
|
75
|
+
'--property=Type=exec',
|
|
76
|
+
'--property=KillMode=process',
|
|
77
|
+
`--property=StandardOutput=append:${workerLog}`,
|
|
78
|
+
`--property=StandardError=append:${workerLog}`,
|
|
79
|
+
`--setenv=AGENTLINK_HOME=${CONFIG_DIR}`,
|
|
80
|
+
nodePath,
|
|
81
|
+
input.workerPath,
|
|
82
|
+
'--job',
|
|
83
|
+
input.jobPath,
|
|
84
|
+
],
|
|
85
|
+
options: {
|
|
86
|
+
cwd: jobDirectory,
|
|
87
|
+
env: environment,
|
|
88
|
+
stdio: 'ignore',
|
|
89
|
+
windowsHide: true,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
const child = execute(command, deps);
|
|
93
|
+
const started = observeSpawn(child);
|
|
94
|
+
child.unref?.();
|
|
95
|
+
return { kind: 'systemd-transient', pid: child.pid, command, started };
|
|
96
|
+
}
|
|
97
|
+
if (input.hostType === 'launchd') {
|
|
98
|
+
const uid = deps.uid ?? process.getuid?.();
|
|
99
|
+
if (uid === undefined)
|
|
100
|
+
throw new Error('Cannot create an updater launchd job without a user id');
|
|
101
|
+
const label = `com.agentlink.upgrade.${input.jobId}`;
|
|
102
|
+
const plistPath = join(jobDirectory, 'worker.plist');
|
|
103
|
+
const plist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
104
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
105
|
+
<plist version="1.0">
|
|
106
|
+
<dict>
|
|
107
|
+
<key>Label</key><string>${escapeXml(label)}</string>
|
|
108
|
+
<key>ProgramArguments</key>
|
|
109
|
+
<array>
|
|
110
|
+
<string>${escapeXml(nodePath)}</string>
|
|
111
|
+
<string>${escapeXml(input.workerPath)}</string>
|
|
112
|
+
<string>--job</string>
|
|
113
|
+
<string>${escapeXml(input.jobPath)}</string>
|
|
114
|
+
</array>
|
|
115
|
+
<key>WorkingDirectory</key><string>${escapeXml(jobDirectory)}</string>
|
|
116
|
+
<key>EnvironmentVariables</key>
|
|
117
|
+
<dict><key>AGENTLINK_HOME</key><string>${escapeXml(CONFIG_DIR)}</string></dict>
|
|
118
|
+
<key>RunAtLoad</key><true/>
|
|
119
|
+
<key>StandardOutPath</key><string>${escapeXml(workerLog)}</string>
|
|
120
|
+
<key>StandardErrorPath</key><string>${escapeXml(workerLog)}</string>
|
|
121
|
+
</dict>
|
|
122
|
+
</plist>
|
|
123
|
+
`;
|
|
124
|
+
writeFileSync(plistPath, plist, { encoding: 'utf8', mode: 0o600 });
|
|
125
|
+
chmodSync(plistPath, 0o600);
|
|
126
|
+
const command = {
|
|
127
|
+
executable: 'launchctl',
|
|
128
|
+
args: ['bootstrap', `gui/${uid}`, plistPath],
|
|
129
|
+
options: {
|
|
130
|
+
cwd: jobDirectory,
|
|
131
|
+
env: environment,
|
|
132
|
+
stdio: 'ignore',
|
|
133
|
+
windowsHide: true,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
const child = execute(command, deps);
|
|
137
|
+
const started = observeSpawn(child);
|
|
138
|
+
child.unref?.();
|
|
139
|
+
return {
|
|
140
|
+
kind: 'launchd-temporary',
|
|
141
|
+
pid: child.pid,
|
|
142
|
+
command,
|
|
143
|
+
artifactPath: plistPath,
|
|
144
|
+
serviceIdentifier: `gui/${uid}/${label}`,
|
|
145
|
+
started,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
throw new Error(`Unsupported worker host: ${input.hostType}/${platform}`);
|
|
149
|
+
}
|
|
150
|
+
export function cleanupUpgradeWorkerHost(result, deps = {}) {
|
|
151
|
+
if (result.kind !== 'launchd-temporary')
|
|
152
|
+
return;
|
|
153
|
+
if (result.artifactPath)
|
|
154
|
+
rmSync(result.artifactPath, { force: true });
|
|
155
|
+
if (!result.serviceIdentifier)
|
|
156
|
+
return;
|
|
157
|
+
const command = {
|
|
158
|
+
executable: 'launchctl',
|
|
159
|
+
args: ['bootout', result.serviceIdentifier],
|
|
160
|
+
options: {
|
|
161
|
+
cwd: dirname(result.artifactPath ?? CONFIG_DIR),
|
|
162
|
+
env: { ...process.env, AGENTLINK_HOME: CONFIG_DIR },
|
|
163
|
+
stdio: 'ignore',
|
|
164
|
+
windowsHide: true,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
const child = execute(command, deps);
|
|
168
|
+
if ('on' in child && typeof child.on === 'function')
|
|
169
|
+
child.on('error', () => { });
|
|
170
|
+
child.unref?.();
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=worker-host.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-host.js","sourceRoot":"","sources":["../src/worker-host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAqB,MAAM,eAAe,CAAC;AACzD,OAAO,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,QAAQ,EACR,M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} from './local-control.js';
|
|
2
|
+
import { type ExternalUpgradeJob } from './upgrade-job.js';
|
|
3
|
+
export interface WorkerShutdownAuthorizationDeps {
|
|
4
|
+
now?: () => number;
|
|
5
|
+
processIdentity?: (pid: number) => string | null;
|
|
6
|
+
}
|
|
7
|
+
export declare function authorizeWorkerUpgradeShutdown(request: ShutdownRequest, deps?: WorkerShutdownAuthorizationDeps): ExternalUpgradeJob;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { realpathSync } from 'fs';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { CONFIG_DIR } from './config.js';
|
|
4
|
+
import { computeLaunchSpecDigest, loadLaunchSpec } from './launch-spec.js';
|
|
5
|
+
import { getProcessStartIdentity } from './process-identity.js';
|
|
6
|
+
import { loadExternalUpgradeJob, } from './upgrade-job.js';
|
|
7
|
+
import { loadUpgradeLockOwner } from './upgrade-lock.js';
|
|
8
|
+
import { loadCurrentVersion, validateVersionInstallation, } from './version-store.js';
|
|
9
|
+
function sameSelection(left, right) {
|
|
10
|
+
return left.version === right.version
|
|
11
|
+
&& left.integrity === right.integrity
|
|
12
|
+
&& resolve(left.entry) === resolve(right.entry);
|
|
13
|
+
}
|
|
14
|
+
export function authorizeWorkerUpgradeShutdown(request, deps = {}) {
|
|
15
|
+
if (request.reason !== 'worker-upgrade') {
|
|
16
|
+
throw new Error('Worker shutdown authorization only accepts worker-upgrade');
|
|
17
|
+
}
|
|
18
|
+
if (!request.jobId
|
|
19
|
+
|| !request.targetVersion
|
|
20
|
+
|| !request.lockNonce
|
|
21
|
+
|| !request.workerPid
|
|
22
|
+
|| !request.workerProcessStartIdentity) {
|
|
23
|
+
throw new Error('Worker upgrade shutdown metadata is incomplete');
|
|
24
|
+
}
|
|
25
|
+
const job = loadExternalUpgradeJob(request.jobId);
|
|
26
|
+
if (job.phase !== 'worker-ready' || !job.worker) {
|
|
27
|
+
throw new Error('External upgrade job is not worker-ready');
|
|
28
|
+
}
|
|
29
|
+
if (realpathSync(job.dataRoot) !== realpathSync(CONFIG_DIR)) {
|
|
30
|
+
throw new Error('External upgrade job belongs to a different data root');
|
|
31
|
+
}
|
|
32
|
+
if (job.oldRuntime.pid !== process.pid
|
|
33
|
+
|| request.expectedPid !== process.pid
|
|
34
|
+
|| job.oldRuntime.processInstanceId !== request.expectedProcessInstanceId) {
|
|
35
|
+
throw new Error('External upgrade job does not identify this Agent process');
|
|
36
|
+
}
|
|
37
|
+
const processIdentity = deps.processIdentity ?? getProcessStartIdentity;
|
|
38
|
+
if (processIdentity(process.pid) !== job.oldRuntime.processStartIdentity) {
|
|
39
|
+
throw new Error('External upgrade old process identity does not match');
|
|
40
|
+
}
|
|
41
|
+
if (job.worker.pid !== request.workerPid
|
|
42
|
+
|| job.worker.processStartIdentity !== request.workerProcessStartIdentity
|
|
43
|
+
|| processIdentity(job.worker.pid) !== job.worker.processStartIdentity) {
|
|
44
|
+
throw new Error('External upgrade worker identity does not match a live process');
|
|
45
|
+
}
|
|
46
|
+
if (job.targetVersion !== request.targetVersion || job.lockNonce !== request.lockNonce) {
|
|
47
|
+
throw new Error('External upgrade target or lock nonce does not match');
|
|
48
|
+
}
|
|
49
|
+
const lock = loadUpgradeLockOwner();
|
|
50
|
+
if (lock.jobId !== job.jobId
|
|
51
|
+
|| lock.nonce !== job.lockNonce
|
|
52
|
+
|| lock.dataRoot !== job.dataRoot
|
|
53
|
+
|| lock.worker?.pid !== job.worker.pid
|
|
54
|
+
|| lock.worker?.processStartIdentity !== job.worker.processStartIdentity) {
|
|
55
|
+
throw new Error('External upgrade lock ownership does not match the worker');
|
|
56
|
+
}
|
|
57
|
+
if ((deps.now ?? Date.now)() > Date.parse(job.handoffExpiresAt)) {
|
|
58
|
+
throw new Error('External upgrade worker handoff window expired');
|
|
59
|
+
}
|
|
60
|
+
const spec = loadLaunchSpec(job.launchSpecPath);
|
|
61
|
+
if (computeLaunchSpecDigest(spec) !== job.launchSpecDigest) {
|
|
62
|
+
throw new Error('External upgrade LaunchSpec digest does not match');
|
|
63
|
+
}
|
|
64
|
+
validateVersionInstallation(job.targetSelection);
|
|
65
|
+
const current = loadCurrentVersion();
|
|
66
|
+
if (!current || !sameSelection(current, job.sourceSelection)) {
|
|
67
|
+
throw new Error('External upgrade source selection is no longer current');
|
|
68
|
+
}
|
|
69
|
+
return job;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=worker-shutdown-authorization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-shutdown-authorization.js","sourceRoot":"","sources":["../src/worker-shutdown-authorization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,uBAAuB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAC3E,OAAO,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAEhE,OAAO,EACL,sBAAsB,GAEvB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EACL,kBAAkB,EAClB,2BAA2B,GAC5B,MAAM,oBAAoB,CAAC;AAE5B,SAAS,aAAa,CACpB,IAA2C,EAC3C,KAA4C;IAE5C,OAAO,IAAI,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO;WAChC,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS;WAClC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACpD,CAAC;AAOD,MAAM,UAAU,8BAA8B,CAC5C,OAAwB,EACxB,OAAwC,EAAE;IAE1C,IAAI,OAAO,CAAC,MAAM,KAAK,gBAAgB,EAAE,CAAC;QACxC,MAAM,IAAI,KAAK,CAAC,2DAA2D,CA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O,GAAG,kBAAkB,EAAE,CAAC;IACrC,IAAI,CAAC,OAAO,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,GAAG,CAAC,eAAe,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;IAC5E,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-link/agent",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.297",
|
|
4
4
|
"description": "AgentLink local agent CLI",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"types": "dist/index.d.ts",
|
|
26
26
|
"files": [
|
|
27
27
|
"dist",
|
|
28
|
-
"launcher"
|
|
28
|
+
"launcher",
|
|
29
|
+
"worker"
|
|
29
30
|
],
|
|
30
31
|
"bin": {
|
|
31
32
|
"agentlink-client": "dist/cli.js"
|