@saltpub/agent-autoloop 0.1.7 → 0.1.9
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/CHANGELOG.md +13 -0
- package/dist/bin/agent-autoloop.js +72 -1
- package/dist/cli/main.js +8 -2
- package/dist/host/global-shim.js +79 -0
- package/dist/host/up.js +8 -0
- package/dist/host/update.js +27 -3
- package/docs/auto-update.md +3 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @saltpub/agent-autoloop
|
|
2
2
|
|
|
3
|
+
## 0.1.9
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- d7a8b22: 修好 0.1.8 装不上:`update` 把新版本装到 `versions/` 后会先试跑它,但新副本的 shim 把自己转手给了正在跑的旧版,试跑看到的版本号不对,于是报「起不来」并把新副本删掉。现在 `versions/` 下的受管副本一律运行自己,只有 npm 全局安装跟随指针。
|
|
8
|
+
- 6d1bbc6: `agent-autoloop update` 装好新版本(或新版本已经在等重启)时,列出这一版的更新内容,取自包里的 CHANGELOG,不再只显示一个版本号。
|
|
9
|
+
|
|
10
|
+
## 0.1.8
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 781cc95: npm 全局安装的 `agent-autoloop` 命令改为版本 shim:读 `update/current.json` 指针,把命令转给正在跑的版本执行。以前 `update` 之后中台是新版、敲命令还是旧全局包的行为(help、子命令都停在装的那版);指针缺失、损坏、指向自己或源码检出时回退包自身。launcher 拉起的中台启动时一次性把 shim 之前的旧全局包升级为 shim 版(只升级已装的全局包,失败只告警不挡启动)。
|
|
15
|
+
|
|
3
16
|
## 0.1.7
|
|
4
17
|
|
|
5
18
|
### Patch Changes
|
|
@@ -1,2 +1,73 @@
|
|
|
1
1
|
#!/usr/bin/env -S node --disable-warning=ExperimentalWarning
|
|
2
|
-
|
|
2
|
+
// AGENT_AUTOLOOP_SHIM_V1 — version shim, rustup-style. The npm global install resolves the self-update pointer and
|
|
3
|
+
// re-runs the versioned copy, so a command follows the version the hub runs after an update instead of the stale
|
|
4
|
+
// global package. Every failure mode (no pointer, corrupt JSON, missing target, a pointer naming this very
|
|
5
|
+
// install, a git checkout, a copy the updater unpacked under versions/) falls back to this package's own CLI —
|
|
6
|
+
// the behaviour before the shim.
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
const HERE = import.meta.dirname;
|
|
12
|
+
const COMPILED = path.extname(import.meta.filename) === ".js";
|
|
13
|
+
/** The folder with package.json: above dist/bin when installed, above bin/ in a checkout. */
|
|
14
|
+
const ROOT = COMPILED ? path.dirname(path.dirname(HERE)) : path.dirname(HERE);
|
|
15
|
+
const HOME = process.env.AGENT_AUTOLOOP_HOME?.trim() || path.join(homedir(), ".agent-autoloop");
|
|
16
|
+
/** The managed copy's entry, or undefined to fall back to this package. */
|
|
17
|
+
function pointedEntry() {
|
|
18
|
+
// A copy the updater unpacked under versions/ runs as itself: the trial run checks this very version, and the
|
|
19
|
+
// launcher names the copy to start. Only an install outside versions/ (the npm global one) follows the pointer.
|
|
20
|
+
if (path.resolve(ROOT).startsWith(`${path.resolve(path.join(HOME, "versions"))}${path.sep}`))
|
|
21
|
+
return undefined;
|
|
22
|
+
let pointer;
|
|
23
|
+
try {
|
|
24
|
+
pointer = JSON.parse(readFileSync(path.join(HOME, "update", "current.json"), "utf8"));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return undefined; // no managed install, or a pointer file half-written
|
|
28
|
+
}
|
|
29
|
+
const dir = pointer?.dir;
|
|
30
|
+
if (typeof dir !== "string" || dir.length === 0)
|
|
31
|
+
return undefined;
|
|
32
|
+
// A pointer naming this very install: re-running it would fork-bomb.
|
|
33
|
+
if (path.resolve(dir) === path.resolve(ROOT))
|
|
34
|
+
return undefined;
|
|
35
|
+
const entry = path.join(dir, "dist", "bin", "agent-autoloop.js");
|
|
36
|
+
return existsSync(entry) ? entry : undefined;
|
|
37
|
+
}
|
|
38
|
+
// A checkout runs its own sources, pointer or not. The fallback imports are dynamic on purpose: a static import
|
|
39
|
+
// would run this package's CLI even when the command belongs to the managed copy.
|
|
40
|
+
if (existsSync(path.join(ROOT, ".git"))) {
|
|
41
|
+
await import("../cli/main.js");
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const entry = pointedEntry();
|
|
45
|
+
if (!entry) {
|
|
46
|
+
await import("../cli/main.js");
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const child = spawn(process.execPath, ["--disable-warning=ExperimentalWarning", entry, ...process.argv.slice(2)], {
|
|
50
|
+
stdio: "inherit",
|
|
51
|
+
});
|
|
52
|
+
// POSIX: forward termination so Ctrl-C / SIGTERM reach the real CLI.
|
|
53
|
+
const forward = process.platform === "win32" ? undefined : (signal) => void child.kill(signal);
|
|
54
|
+
if (forward) {
|
|
55
|
+
process.on("SIGINT", forward);
|
|
56
|
+
process.on("SIGTERM", forward);
|
|
57
|
+
}
|
|
58
|
+
child.on("error", (error) => {
|
|
59
|
+
console.error(`agent-autoloop: 没能启动 ${entry}:${error.message}`);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
});
|
|
62
|
+
child.on("exit", (code, signal) => {
|
|
63
|
+
if (signal && process.platform !== "win32" && forward) {
|
|
64
|
+
// Die the way the child did: drop the handlers (default action is terminate) and re-raise.
|
|
65
|
+
process.off("SIGINT", forward);
|
|
66
|
+
process.off("SIGTERM", forward);
|
|
67
|
+
process.kill(process.pid, signal);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
process.exit(code ?? 1);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
package/dist/cli/main.js
CHANGED
|
@@ -1078,8 +1078,12 @@ async function update() {
|
|
|
1078
1078
|
const result = stageLatest();
|
|
1079
1079
|
if (result.status === "current")
|
|
1080
1080
|
return console.log(`已经是最新的(v${result.version})。`);
|
|
1081
|
-
if (result.status === "pending")
|
|
1082
|
-
|
|
1081
|
+
if (result.status === "pending") {
|
|
1082
|
+
console.log(`v${result.version} 已经准备好,中台重启时换上。`);
|
|
1083
|
+
for (const note of result.notes)
|
|
1084
|
+
console.log(`- ${note}`);
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1083
1087
|
if (result.status === "bad")
|
|
1084
1088
|
return fail(`v${result.version} 之前启动失败过,先不装它;修好后删掉 ${path.join(HOME, "update", "bad.json")} 再试。`);
|
|
1085
1089
|
if (result.status === "busy")
|
|
@@ -1091,6 +1095,8 @@ async function update() {
|
|
|
1091
1095
|
if (told === undefined && service)
|
|
1092
1096
|
await waitForHub();
|
|
1093
1097
|
console.log(`更新好了(v${result.version})。${told ?? (service ? "" : "下次启动中台时换上。")}`);
|
|
1098
|
+
for (const note of result.notes)
|
|
1099
|
+
console.log(`- ${note}`);
|
|
1094
1100
|
}
|
|
1095
1101
|
async function askAtTerminal(question) {
|
|
1096
1102
|
const terminal = createInterface({ input: process.stdin, output: process.stdout });
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// A one-time heal for global installs from before the bin became a version shim: without it, commands typed at the
|
|
2
|
+
// global bin keep running the old package's own code while the hub and --version follow the update pointer. The
|
|
3
|
+
// launcher child upgrades the global package once, so its bin becomes the shim, and writes a marker so every later
|
|
4
|
+
// boot is a no-op. It never installs a global package the machine doesn't have, and never breaks startup: every
|
|
5
|
+
// failure is a result, not a throw.
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { commandLine } from "./command.js";
|
|
10
|
+
import { HOME } from "./home.js";
|
|
11
|
+
import { PACKAGE } from "./npm.js";
|
|
12
|
+
import { REGISTRY, updateDir } from "./update.js";
|
|
13
|
+
import { versionName } from "./version.js";
|
|
14
|
+
/** Written into the shim's bin; an old global package's bin doesn't have it. */
|
|
15
|
+
export const SHIM_MARKER = "AGENT_AUTOLOOP_SHIM_V1";
|
|
16
|
+
const runNpm = (args) => {
|
|
17
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
18
|
+
const [program, rest] = commandLine(["npm", ...args]);
|
|
19
|
+
const child = spawn(program, rest, { stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
|
|
20
|
+
let stdout = "";
|
|
21
|
+
let stderr = "";
|
|
22
|
+
child.stdout.on("data", (chunk) => (stdout += chunk));
|
|
23
|
+
child.stderr.on("data", (chunk) => (stderr += chunk));
|
|
24
|
+
child.on("error", reject);
|
|
25
|
+
child.on("close", (code) => resolve({ code: code ?? 1, stdout, stderr }));
|
|
26
|
+
return promise;
|
|
27
|
+
};
|
|
28
|
+
export async function healGlobalShim(options = {}) {
|
|
29
|
+
const home = options.home ?? HOME;
|
|
30
|
+
const markerFile = options.markerFile ?? path.join(updateDir(home), "global-shim.json");
|
|
31
|
+
const run = options.run ?? runNpm;
|
|
32
|
+
const exists = options.exists ?? existsSync;
|
|
33
|
+
const read = options.read ?? ((file) => readFileSync(file, "utf8"));
|
|
34
|
+
const version = options.version ?? versionName();
|
|
35
|
+
const now = options.now ?? (() => new Date());
|
|
36
|
+
const mark = () => {
|
|
37
|
+
mkdirSync(path.dirname(markerFile), { recursive: true });
|
|
38
|
+
writeFileSync(markerFile, JSON.stringify({ healedAt: now().toISOString(), version }), { mode: 0o600 });
|
|
39
|
+
};
|
|
40
|
+
try {
|
|
41
|
+
if (exists(markerFile))
|
|
42
|
+
return { healed: false, reason: "already-healed" };
|
|
43
|
+
const root = await run(["root", "-g"]);
|
|
44
|
+
const globalRoot = root.stdout.trim();
|
|
45
|
+
if (root.code !== 0 || !globalRoot)
|
|
46
|
+
return { healed: false, reason: "failed" };
|
|
47
|
+
const globalPackage = path.join(globalRoot, ...PACKAGE.split("/"));
|
|
48
|
+
// No global package: nothing to heal, and never install one ourselves.
|
|
49
|
+
if (!exists(path.join(globalPackage, "package.json"))) {
|
|
50
|
+
mark();
|
|
51
|
+
return { healed: false, reason: "no-global-package" };
|
|
52
|
+
}
|
|
53
|
+
let bin = "";
|
|
54
|
+
try {
|
|
55
|
+
bin = read(path.join(globalPackage, "dist", "bin", "agent-autoloop.js"));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// A bin that can't be read can't carry the marker either; installing repairs it.
|
|
59
|
+
}
|
|
60
|
+
if (bin.includes(SHIM_MARKER)) {
|
|
61
|
+
mark();
|
|
62
|
+
return { healed: false, reason: "already-shim" };
|
|
63
|
+
}
|
|
64
|
+
const install = await run([
|
|
65
|
+
"install",
|
|
66
|
+
"-g",
|
|
67
|
+
`${PACKAGE}@latest`,
|
|
68
|
+
`--registry=${REGISTRY}`,
|
|
69
|
+
`--${PACKAGE.split("/")[0]}:registry=${REGISTRY}`,
|
|
70
|
+
]);
|
|
71
|
+
if (install.code !== 0)
|
|
72
|
+
return { healed: false, reason: "failed" };
|
|
73
|
+
mark();
|
|
74
|
+
return { healed: true };
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return { healed: false, reason: "failed" };
|
|
78
|
+
}
|
|
79
|
+
}
|
package/dist/host/up.js
CHANGED
|
@@ -5,6 +5,7 @@ import { existsSync, mkdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { loadChannel } from "./channels.js";
|
|
7
7
|
import { cantListen, parseListen, readConfig } from "./config.js";
|
|
8
|
+
import { healGlobalShim } from "./global-shim.js";
|
|
8
9
|
import { HOME } from "./home.js";
|
|
9
10
|
import { runningUp } from "./service.js";
|
|
10
11
|
import { commitOf } from "./version.js";
|
|
@@ -131,6 +132,13 @@ export async function up() {
|
|
|
131
132
|
jobs.push(keepChannelRunning(id, channel, config.url, stop.signal));
|
|
132
133
|
}
|
|
133
134
|
refreshLauncher();
|
|
135
|
+
// The npm-global bin became a version shim in 0.1.8: upgrade a pre-shim global install once, never blocking startup.
|
|
136
|
+
if (managedAt(HOME) && process.env.AGENT_AUTOLOOP_LAUNCHED === "1") {
|
|
137
|
+
void healGlobalShim().then((result) => {
|
|
138
|
+
if (result.healed)
|
|
139
|
+
log("update")("全局命令已换成跟随中台版本的 shim");
|
|
140
|
+
});
|
|
141
|
+
}
|
|
134
142
|
// A hub that starts matters more than the link to its manual: a folder the user's home won't take is worth a line, not
|
|
135
143
|
// refusing to run.
|
|
136
144
|
try {
|
package/dist/host/update.js
CHANGED
|
@@ -63,6 +63,28 @@ export function readPending(home = HOME) {
|
|
|
63
63
|
return null;
|
|
64
64
|
return pending;
|
|
65
65
|
}
|
|
66
|
+
/** The bullets this version's CHANGELOG section lists, commit hashes stripped. [] when there is nothing to show. */
|
|
67
|
+
export function releaseNotes(dir, version) {
|
|
68
|
+
let changelog;
|
|
69
|
+
try {
|
|
70
|
+
changelog = readFileSync(path.join(dir, "CHANGELOG.md"), "utf8");
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
const notes = [];
|
|
76
|
+
let inside = false;
|
|
77
|
+
for (const line of changelog.split("\n")) {
|
|
78
|
+
if (line.startsWith("## ")) {
|
|
79
|
+
inside = line.trimEnd() === `## ${version}`;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const bullet = /^- (?:[0-9a-f]{7,}: )?(.+?)\s*$/.exec(line.trimStart());
|
|
83
|
+
if (inside && bullet)
|
|
84
|
+
notes.push(bullet[1]);
|
|
85
|
+
}
|
|
86
|
+
return notes;
|
|
87
|
+
}
|
|
66
88
|
export function managedAt(home = HOME) {
|
|
67
89
|
return readPointer(home) !== null && existsSync(launcherFile(home));
|
|
68
90
|
}
|
|
@@ -250,8 +272,10 @@ export function stageLatest(home = HOME) {
|
|
|
250
272
|
return { status: "current", version: pointer.version };
|
|
251
273
|
if (badVersions(home).includes(latest))
|
|
252
274
|
return { status: "bad", version: latest };
|
|
253
|
-
|
|
254
|
-
|
|
275
|
+
const waiting = readPending(home);
|
|
276
|
+
if (waiting?.version === latest) {
|
|
277
|
+
return { status: "pending", version: latest, notes: releaseNotes(waiting.dir, latest) };
|
|
278
|
+
}
|
|
255
279
|
const lock = acquireLock(home);
|
|
256
280
|
if (!lock)
|
|
257
281
|
return { status: "busy" };
|
|
@@ -273,7 +297,7 @@ export function stageLatest(home = HOME) {
|
|
|
273
297
|
throw new Error(`${PACKAGE}@${latest} 起不来:${done.stderr?.trim() || done.stdout?.trim() || done.error}`);
|
|
274
298
|
}
|
|
275
299
|
writeJson(pendingFile(home), { version: latest, dir, at: Date.now() });
|
|
276
|
-
return { status: "staged", version: latest };
|
|
300
|
+
return { status: "staged", version: latest, notes: releaseNotes(dir, latest) };
|
|
277
301
|
}
|
|
278
302
|
catch (error) {
|
|
279
303
|
rmSync(prefix, { recursive: true, force: true });
|
package/docs/auto-update.md
CHANGED
|
@@ -34,7 +34,7 @@ npm 安装的副本不在原地覆盖着换版本。每个版本解包在自己
|
|
|
34
34
|
|
|
35
35
|
## 手动更新
|
|
36
36
|
|
|
37
|
-
`agent-autoloop update` 先数没跑完的轮次,有就拒绝。没有就做上面第 1 到第 3 步,然后重启服务;没装服务时,下次 `up`
|
|
37
|
+
`agent-autoloop update` 先数没跑完的轮次,有就拒绝。没有就做上面第 1 到第 3 步,然后重启服务;没装服务时,下次 `up` 换上。装好或已经在等的版本,会把这一版的更新内容(包里 CHANGELOG 的条目)一并列出来。
|
|
38
38
|
|
|
39
39
|
`update` 只准备版本,不改 `rules.json`、密钥和记录。投影格式变了会在新版第一次启动时从日志重建,见 [安装说明](../INSTALL.md)。
|
|
40
40
|
|
|
@@ -51,3 +51,5 @@ npm 安装的副本不在原地覆盖着换版本。每个版本解包在自己
|
|
|
51
51
|
## 换的是哪一份
|
|
52
52
|
|
|
53
53
|
`agent-autoloop --version` 在 npm 的全局安装上打印 current 指向的版本,在 `versions/` 里的副本上打印自己清单里的版本。启动器拉起的是后者,所以版本号始终是正在跑的代码,不是当初敲命令的那份。
|
|
54
|
+
|
|
55
|
+
npm 全局安装里的 `agent-autoloop` 命令本身也是一个 shim(bin 里带 `AGENT_AUTOLOOP_SHIM_V1` 标记):它读 `update/current.json`,把命令转给指针指向的 `versions/<版本>/` 里的副本执行,敲命令用的和正在跑的中台始终是同一版。指针缺失、损坏、指向自己,或者在源码检出里,回退到这份安装自己的代码(shim 之前的行为)。0.1.8 之前装的全局包没有 shim:launcher 拉起的中台每次启动时检查一次,发现旧全局包就执行一次 `npm install -g @saltpub/agent-autoloop@latest` 把它换成带 shim 的版本,成功了写 `update/global-shim.json` 以后不再检查;失败只告警,不挡启动;机器上没装全局包就只记标记,绝不主动安装。
|