@agentknight/client 0.9.47 → 0.9.49
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/LICENSE +201 -201
- package/README.md +341 -305
- package/dist/chunks/chunk-KYTI7A5R.js +735 -0
- package/dist/chunks/chunk-NHA7HI6P.js +32 -0
- package/dist/chunks/chunk-OZKXJ6L4.js +2684 -0
- package/dist/chunks/chunk-SPZWIFFP.js +52 -0
- package/dist/chunks/chunk-WZIU4HVH.js +318 -0
- package/dist/chunks/chunk-YNKNHR5K.js +74 -0
- package/dist/chunks/client-home-LAKFDPXO.js +268 -0
- package/dist/chunks/local-sessions-EZUANCR4.js +29 -0
- package/dist/chunks/runtime-CTACRJCK.js +20 -0
- package/dist/chunks/start-client-C3PK6POG.js +3428 -0
- package/dist/chunks/upgrade-T32WY4HC.js +181 -0
- package/dist/chunks/upgrade-state-QXZT3TTS.js +17 -0
- package/dist/cli.js +83 -7196
- package/dist/upgrade-worker.js +2 -2
- package/package.json +5 -4
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
assertInstallationIdle,
|
|
4
|
+
detectSupervisor,
|
|
5
|
+
fileHash,
|
|
6
|
+
globalPackageDirectory,
|
|
7
|
+
launchUpgradeWorker,
|
|
8
|
+
prepareUpgradePackages,
|
|
9
|
+
resolveUpgradeVersion,
|
|
10
|
+
runNpm,
|
|
11
|
+
updateJobState
|
|
12
|
+
} from "./chunk-KYTI7A5R.js";
|
|
13
|
+
import "./chunk-YNKNHR5K.js";
|
|
14
|
+
import {
|
|
15
|
+
processExists,
|
|
16
|
+
readRuntimeDescriptor,
|
|
17
|
+
requestRuntime
|
|
18
|
+
} from "./chunk-WZIU4HVH.js";
|
|
19
|
+
import {
|
|
20
|
+
readOptionalJson,
|
|
21
|
+
writePrivateJson
|
|
22
|
+
} from "./chunk-SPZWIFFP.js";
|
|
23
|
+
|
|
24
|
+
// src/upgrade.ts
|
|
25
|
+
import { randomUUID } from "node:crypto";
|
|
26
|
+
import { copyFile, mkdir, open, realpath, rm } from "node:fs/promises";
|
|
27
|
+
import { dirname, join, resolve } from "node:path";
|
|
28
|
+
import { z } from "zod";
|
|
29
|
+
function isNewerVersion(target, current) {
|
|
30
|
+
const parse = (version) => {
|
|
31
|
+
const match = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/u.exec(version);
|
|
32
|
+
if (match === null) throw new Error(`Invalid package version: ${version}`);
|
|
33
|
+
return {
|
|
34
|
+
release: [Number(match[1]), Number(match[2]), Number(match[3])],
|
|
35
|
+
...match[4] === void 0 ? {} : { prerelease: match[4].split(".") }
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const left = parse(target);
|
|
39
|
+
const right = parse(current);
|
|
40
|
+
for (let index = 0; index < 3; index++) {
|
|
41
|
+
const a = left.release[index] ?? 0;
|
|
42
|
+
const b = right.release[index] ?? 0;
|
|
43
|
+
if (a !== b) return a > b;
|
|
44
|
+
}
|
|
45
|
+
if (left.prerelease === void 0) return right.prerelease !== void 0;
|
|
46
|
+
if (right.prerelease === void 0) return false;
|
|
47
|
+
for (let index = 0; index < Math.max(left.prerelease.length, right.prerelease.length); index++) {
|
|
48
|
+
const a = left.prerelease[index];
|
|
49
|
+
const b = right.prerelease[index];
|
|
50
|
+
if (a === b) continue;
|
|
51
|
+
if (a === void 0) return false;
|
|
52
|
+
if (b === void 0) return true;
|
|
53
|
+
const numericA = /^\d+$/u.test(a);
|
|
54
|
+
const numericB = /^\d+$/u.test(b);
|
|
55
|
+
if (numericA && numericB) return Number(a) > Number(b);
|
|
56
|
+
if (numericA !== numericB) return !numericA;
|
|
57
|
+
return a > b;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
async function upgrade(options) {
|
|
62
|
+
const target = await resolveUpgradeVersion(options.targetVersion);
|
|
63
|
+
if (!isNewerVersion(target.version, options.currentVersion)) {
|
|
64
|
+
options.report(`Installed ${options.currentVersion}; ${target.version} is not a newer version.`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (options.check) {
|
|
68
|
+
options.report(`Available: ${options.currentVersion} -> ${target.version}`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const prefix = (await runNpm(["prefix", "--global"])).trim();
|
|
72
|
+
if (!prefix) throw new Error("npm returned an empty global installation prefix.");
|
|
73
|
+
const cliPath = join(globalPackageDirectory(prefix), "dist", "cli.js");
|
|
74
|
+
const invokedCli = process.argv[1];
|
|
75
|
+
if (invokedCli === void 0 || await realpath(cliPath) !== await realpath(invokedCli)) {
|
|
76
|
+
throw new Error("Self-upgrade requires the globally installed agentknight command, not npx or a source checkout.");
|
|
77
|
+
}
|
|
78
|
+
const descriptor = await readRuntimeDescriptor(options.dataDirectory);
|
|
79
|
+
const runtime = descriptor === void 0 || !processExists(descriptor.pid) ? void 0 : await requestRuntime(descriptor, "status");
|
|
80
|
+
await assertInstallationIdle(prefix, runtime?.pid);
|
|
81
|
+
if (runtime !== void 0 && (runtime.version !== options.currentVersion || resolve(runtime.cliPath) !== resolve(cliPath))) {
|
|
82
|
+
throw new Error("Running Agent and installed CLI differ. Restart the original service before upgrading.");
|
|
83
|
+
}
|
|
84
|
+
if (runtime !== void 0 && runtime.configPath === void 0) {
|
|
85
|
+
throw new Error("Save configuration in ~/.ak/agentknight.env and restart before upgrading a running Agent.");
|
|
86
|
+
}
|
|
87
|
+
if (runtime?.configPath !== void 0 && await fileHash(runtime.configPath) !== runtime.configHash) {
|
|
88
|
+
throw new Error("Configuration changed since the Agent started. Restart it before upgrading.");
|
|
89
|
+
}
|
|
90
|
+
const root = join(options.dataDirectory, "upgrades");
|
|
91
|
+
await mkdir(root, { recursive: true, mode: 448 });
|
|
92
|
+
const lockPath = join(root, "upgrade.lock");
|
|
93
|
+
const existing = await readOptionalJson(lockPath);
|
|
94
|
+
if (existing !== void 0) {
|
|
95
|
+
const owner = z.object({ pid: z.number().int().positive() }).parse(existing);
|
|
96
|
+
if (processExists(owner.pid)) throw new Error("Another upgrade is already running. Use agentknight status.");
|
|
97
|
+
throw new Error(`An interrupted upgrade needs review. Read ${join(root, "status.json")} before removing ${lockPath}.`);
|
|
98
|
+
}
|
|
99
|
+
const id = randomUUID();
|
|
100
|
+
const lock = await open(lockPath, "wx", 384);
|
|
101
|
+
try {
|
|
102
|
+
await lock.writeFile(JSON.stringify({ id, pid: process.pid }));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
await rm(lockPath, { force: true });
|
|
105
|
+
throw error;
|
|
106
|
+
} finally {
|
|
107
|
+
await lock.close();
|
|
108
|
+
}
|
|
109
|
+
const directory = join(root, id);
|
|
110
|
+
let dispatched = false;
|
|
111
|
+
let globalLockOwned = false;
|
|
112
|
+
const globalLockPath = join(prefix, ".agentknight-upgrade.json");
|
|
113
|
+
try {
|
|
114
|
+
const globalLock = await open(globalLockPath, "wx", 384);
|
|
115
|
+
globalLockOwned = true;
|
|
116
|
+
try {
|
|
117
|
+
await globalLock.writeFile(JSON.stringify({ id, pid: process.pid, dataDirectory: options.dataDirectory }));
|
|
118
|
+
} finally {
|
|
119
|
+
await globalLock.close();
|
|
120
|
+
}
|
|
121
|
+
await mkdir(directory, { mode: 448 });
|
|
122
|
+
await writePrivateJson(join(root, "status.json"), {
|
|
123
|
+
id,
|
|
124
|
+
phase: "preparing",
|
|
125
|
+
fromVersion: options.currentVersion,
|
|
126
|
+
toVersion: target.version,
|
|
127
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
128
|
+
});
|
|
129
|
+
options.report(`Preparing ${target.version} and rollback ${options.currentVersion}; the existing Agent stays online.`);
|
|
130
|
+
const supervisor = runtime === void 0 ? { kind: "process" } : await detectSupervisor(runtime);
|
|
131
|
+
if (runtime !== void 0 && supervisor.kind === "process" && !runtime.canRestart) {
|
|
132
|
+
throw new Error("This foreground Agent uses shell-only configuration or credentials. Save them in its private env file and restart before upgrading.");
|
|
133
|
+
}
|
|
134
|
+
const packages = await prepareUpgradePackages({
|
|
135
|
+
directory,
|
|
136
|
+
currentVersion: options.currentVersion,
|
|
137
|
+
targetVersion: target.version,
|
|
138
|
+
onProgress: options.report
|
|
139
|
+
});
|
|
140
|
+
const plan = {
|
|
141
|
+
id,
|
|
142
|
+
dataDirectory: options.dataDirectory,
|
|
143
|
+
directory,
|
|
144
|
+
prefix,
|
|
145
|
+
currentVersion: options.currentVersion,
|
|
146
|
+
targetVersion: target.version,
|
|
147
|
+
packages,
|
|
148
|
+
supervisor,
|
|
149
|
+
taskName: `AgentKnight Upgrade ${id}`,
|
|
150
|
+
...runtime === void 0 ? {} : { runtime },
|
|
151
|
+
...runtime?.configHash === void 0 ? {} : { configHash: runtime.configHash }
|
|
152
|
+
};
|
|
153
|
+
const planPath = join(directory, "plan.json");
|
|
154
|
+
await writePrivateJson(planPath, plan);
|
|
155
|
+
const workerPath = join(directory, "worker.mjs");
|
|
156
|
+
await copyFile(join(dirname(cliPath), "upgrade-worker.js"), workerPath);
|
|
157
|
+
await updateJobState(plan, "ready");
|
|
158
|
+
await launchUpgradeWorker(workerPath, planPath, plan.taskName, join(directory, "worker.log"));
|
|
159
|
+
dispatched = true;
|
|
160
|
+
options.report("Upgrade handed to an independent worker, which waits for idle before switching. This connection may close; use agentknight status for the result.");
|
|
161
|
+
} catch (error) {
|
|
162
|
+
await writePrivateJson(join(root, "status.json"), {
|
|
163
|
+
id,
|
|
164
|
+
phase: "failed",
|
|
165
|
+
fromVersion: options.currentVersion,
|
|
166
|
+
toVersion: target.version,
|
|
167
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
168
|
+
error: error instanceof Error ? error.message : String(error)
|
|
169
|
+
});
|
|
170
|
+
throw error;
|
|
171
|
+
} finally {
|
|
172
|
+
if (!dispatched) {
|
|
173
|
+
await rm(lockPath, { force: true });
|
|
174
|
+
if (globalLockOwned) await rm(globalLockPath, { force: true });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
export {
|
|
179
|
+
isNewerVersion,
|
|
180
|
+
upgrade
|
|
181
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
UpgradeStateSchema,
|
|
4
|
+
UpgradeSuccessSchema,
|
|
5
|
+
readLastUpgrade,
|
|
6
|
+
readOptionalJson,
|
|
7
|
+
readUpgradeState,
|
|
8
|
+
writePrivateJson
|
|
9
|
+
} from "./chunk-SPZWIFFP.js";
|
|
10
|
+
export {
|
|
11
|
+
UpgradeStateSchema,
|
|
12
|
+
UpgradeSuccessSchema,
|
|
13
|
+
readLastUpgrade,
|
|
14
|
+
readOptionalJson,
|
|
15
|
+
readUpgradeState,
|
|
16
|
+
writePrivateJson
|
|
17
|
+
};
|