@saltpub/agent-autoloop 0.1.7 → 0.1.8
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 +6 -0
- package/dist/bin/agent-autoloop.js +67 -1
- package/dist/host/global-shim.js +79 -0
- package/dist/host/up.js +8 -0
- package/docs/auto-update.md +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @saltpub/agent-autoloop
|
|
2
2
|
|
|
3
|
+
## 0.1.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 781cc95: npm 全局安装的 `agent-autoloop` 命令改为版本 shim:读 `update/current.json` 指针,把命令转给正在跑的版本执行。以前 `update` 之后中台是新版、敲命令还是旧全局包的行为(help、子命令都停在装的那版);指针缺失、损坏、指向自己或源码检出时回退包自身。launcher 拉起的中台启动时一次性把 shim 之前的旧全局包升级为 shim 版(只升级已装的全局包,失败只告警不挡启动)。
|
|
8
|
+
|
|
3
9
|
## 0.1.7
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -1,2 +1,68 @@
|
|
|
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) falls back to this package's own CLI — the behaviour before the shim.
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
const HERE = import.meta.dirname;
|
|
11
|
+
const COMPILED = path.extname(import.meta.filename) === ".js";
|
|
12
|
+
/** The folder with package.json: above dist/bin when installed, above bin/ in a checkout. */
|
|
13
|
+
const ROOT = COMPILED ? path.dirname(path.dirname(HERE)) : path.dirname(HERE);
|
|
14
|
+
const HOME = process.env.AGENT_AUTOLOOP_HOME?.trim() || path.join(homedir(), ".agent-autoloop");
|
|
15
|
+
/** The managed copy's entry, or undefined to fall back to this package. */
|
|
16
|
+
function pointedEntry() {
|
|
17
|
+
let pointer;
|
|
18
|
+
try {
|
|
19
|
+
pointer = JSON.parse(readFileSync(path.join(HOME, "update", "current.json"), "utf8"));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return undefined; // no managed install, or a pointer file half-written
|
|
23
|
+
}
|
|
24
|
+
const dir = pointer?.dir;
|
|
25
|
+
if (typeof dir !== "string" || dir.length === 0)
|
|
26
|
+
return undefined;
|
|
27
|
+
// A pointer naming this very install: re-running it would fork-bomb.
|
|
28
|
+
if (path.resolve(dir) === path.resolve(ROOT))
|
|
29
|
+
return undefined;
|
|
30
|
+
const entry = path.join(dir, "dist", "bin", "agent-autoloop.js");
|
|
31
|
+
return existsSync(entry) ? entry : undefined;
|
|
32
|
+
}
|
|
33
|
+
// A checkout runs its own sources, pointer or not. The fallback imports are dynamic on purpose: a static import
|
|
34
|
+
// would run this package's CLI even when the command belongs to the managed copy.
|
|
35
|
+
if (existsSync(path.join(ROOT, ".git"))) {
|
|
36
|
+
await import("../cli/main.js");
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const entry = pointedEntry();
|
|
40
|
+
if (!entry) {
|
|
41
|
+
await import("../cli/main.js");
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const child = spawn(process.execPath, ["--disable-warning=ExperimentalWarning", entry, ...process.argv.slice(2)], {
|
|
45
|
+
stdio: "inherit",
|
|
46
|
+
});
|
|
47
|
+
// POSIX: forward termination so Ctrl-C / SIGTERM reach the real CLI.
|
|
48
|
+
const forward = process.platform === "win32" ? undefined : (signal) => void child.kill(signal);
|
|
49
|
+
if (forward) {
|
|
50
|
+
process.on("SIGINT", forward);
|
|
51
|
+
process.on("SIGTERM", forward);
|
|
52
|
+
}
|
|
53
|
+
child.on("error", (error) => {
|
|
54
|
+
console.error(`agent-autoloop: 没能启动 ${entry}:${error.message}`);
|
|
55
|
+
process.exit(1);
|
|
56
|
+
});
|
|
57
|
+
child.on("exit", (code, signal) => {
|
|
58
|
+
if (signal && process.platform !== "win32" && forward) {
|
|
59
|
+
// Die the way the child did: drop the handlers (default action is terminate) and re-raise.
|
|
60
|
+
process.off("SIGINT", forward);
|
|
61
|
+
process.off("SIGTERM", forward);
|
|
62
|
+
process.kill(process.pid, signal);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
process.exit(code ?? 1);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -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/docs/auto-update.md
CHANGED
|
@@ -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` 以后不再检查;失败只告警,不挡启动;机器上没装全局包就只记标记,绝不主动安装。
|