@agent_forge/forge 2.0.0 → 2.0.2
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/package.json +6 -6
- package/run.js +56 -98
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent_forge/forge",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "AI 开发质量门禁引擎 — 结构化门禁管道,在 AI 生成的代码进入仓库前进行质量锻造",
|
|
5
5
|
"bin": {
|
|
6
6
|
"forge": "run.js"
|
|
7
7
|
},
|
|
8
8
|
"optionalDependencies": {
|
|
9
|
-
"@agent_forge/forge-darwin-arm64": "2.0.
|
|
10
|
-
"@agent_forge/forge-darwin-x64": "2.0.
|
|
11
|
-
"@agent_forge/forge-linux-arm64": "2.0.
|
|
12
|
-
"@agent_forge/forge-linux-x64": "2.0.
|
|
13
|
-
"@agent_forge/forge-win32-x64": "2.0.
|
|
9
|
+
"@agent_forge/forge-darwin-arm64": "2.0.2",
|
|
10
|
+
"@agent_forge/forge-darwin-x64": "2.0.2",
|
|
11
|
+
"@agent_forge/forge-linux-arm64": "2.0.2",
|
|
12
|
+
"@agent_forge/forge-linux-x64": "2.0.2",
|
|
13
|
+
"@agent_forge/forge-win32-x64": "2.0.2"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"run.js",
|
package/run.js
CHANGED
|
@@ -1,133 +1,91 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const { spawn } = require("child_process");
|
|
4
|
-
const crypto = require("crypto");
|
|
5
4
|
const fs = require("fs");
|
|
6
|
-
const os = require("os");
|
|
7
5
|
const path = require("path");
|
|
8
6
|
|
|
9
7
|
// 通过 optionalDependencies 平台子包定位当前平台的二进制。
|
|
10
8
|
// npm install 时按 os/cpu 只装匹配当前平台的一个子包(@agent_forge/forge-<platform>-<arch>),
|
|
11
9
|
// 二进制随子包落进 node_modules,无需 install script——npm 12 起 install scripts 默认
|
|
12
10
|
// 禁用,平台分包是官方推荐的二进制分发方式(esbuild/rollup/turbo 同模式)。
|
|
13
|
-
const exe = process.platform === "win32" ? "forge.exe" : "forge";
|
|
14
|
-
const platformPkg = `@agent_forge/forge-${process.platform}-${process.arch}`;
|
|
15
|
-
|
|
16
|
-
// 基础设施失败的可见性(docs/plans/hook-silent-failure-visibility-2026-10.md):
|
|
17
|
-
// 本包装器自己能看见的四类失败(平台不支持 / 子包缺失 / spawn 失败 / 被信号杀死)
|
|
18
|
-
// 都 fail-open,但此前对宿主是「静默 approve」——没人知道 hook 零拦截。现在:
|
|
19
|
-
// - hook 调用(argv[2] === "hook"):同一宿主进程(ppid)同一原因第一次 stderr 输出
|
|
20
|
-
// 带修复办法的提示并 exit 1(宿主把 stderr 展示给用户,且 1 从不阻断),之后同因
|
|
21
|
-
// 静默 exit 0。包装器不能消费 stdin(子进程要读),所以会话键取 ppid 而非 session id;
|
|
22
|
-
// `sh -c` 不 exec 末条命令的宿主上 ppid 逐次变化,去重退化为逐次提示,不比现状差。
|
|
23
|
-
// - 非 hook 调用(交互使用):stderr 总是输出提示,退出码保持 0(既有契约)。
|
|
24
|
-
// - stdout 契约不变:仍打印 {"decision":"approve"}。
|
|
25
|
-
// 去重状态与 Go 侧同目录($TMPDIR/forge-hook-infra-<uid>/ppid-<pid>/),键带 wrapper: 前缀
|
|
26
|
-
// 避免与 Go 侧撞车。FORGE_HOOK_INFRA_DEDUP=0 恢复逐次 exit 1(与 Go 侧同一逃生口)。
|
|
27
|
-
// 包装器侧的逃生口无法落 checklog——这些失败发生时 forge 二进制本身缺席或没起来。
|
|
28
11
|
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (!fs.lstatSync(root).isDirectory()) return true;
|
|
40
|
-
} catch (_) {
|
|
41
|
-
// 根尚不存在:下面创建。
|
|
42
|
-
}
|
|
43
|
-
const dir = path.join(root, `ppid-${process.ppid}`);
|
|
44
|
-
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
45
|
-
const key = crypto.createHash("sha256").update(`wrapper:${cause}`).digest("hex").slice(0, 16);
|
|
46
|
-
const marker = path.join(dir, key);
|
|
47
|
-
try {
|
|
48
|
-
fs.closeSync(fs.openSync(marker, "wx", 0o600));
|
|
49
|
-
return true;
|
|
50
|
-
} catch (e) {
|
|
51
|
-
if (!(e && e.code === "EEXIST")) throw e;
|
|
52
|
-
// PID 复用 / 久驻会话:超过 12 小时的旧标记视为陈旧,重新认领。
|
|
53
|
-
if (Date.now() - fs.statSync(marker).mtimeMs > 12 * 3600 * 1000) {
|
|
54
|
-
fs.rmSync(marker, { force: true });
|
|
55
|
-
fs.closeSync(fs.openSync(marker, "wx", 0o600));
|
|
56
|
-
return true;
|
|
57
|
-
}
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
} catch (e) {
|
|
61
|
-
// 状态记不下来时宁可多提示,不吞(与 Go 侧 ClaimInfraNotice 同取向)。
|
|
62
|
-
return !(e && e.code === "EEXIST");
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function failOpen(cause, message, fix) {
|
|
67
|
-
const dedupOn = (process.env.FORGE_HOOK_INFRA_DEDUP || "").trim() !== "0";
|
|
68
|
-
const first = !isHook || !dedupOn || claimNotice(cause);
|
|
69
|
-
console.log('{"decision":"approve"}');
|
|
70
|
-
if (first) {
|
|
71
|
-
console.error(
|
|
72
|
-
`[forge] hook 基础设施故障(${cause},已放行、不阻断${dedupOn ? ";本会话同因不再提示" : ""}): ${message}\n` +
|
|
73
|
-
`修复: ${fix}\n(FORGE_HOOK_INFRA_DEDUP=0 恢复逐次报错)`
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
process.exit(isHook && first ? 1 : 0);
|
|
77
|
-
}
|
|
12
|
+
// 包装器只做一件事:找到二进制并原样转发 argv/stdio/退出码;找不到就报错退出。
|
|
13
|
+
// 1.x 的 hook fail-open(approve JSON + 去重提示)随 1.88 删除 hook 层一并移除——
|
|
14
|
+
// 现在缺二进制必须是一次响亮的失败,而不是「假装命令成功」。
|
|
15
|
+
//
|
|
16
|
+
// 解析路径刻意用「本包的同级目录」而不是 require.resolve:require.resolve 会沿
|
|
17
|
+
// 祖先目录找 node_modules,用户主目录下残留的旧版 @agent_forge/forge-<platform>
|
|
18
|
+
// 会抢先命中(实测 1.42.2 就这样覆盖过新装的 2.0.0),表现为「装了新版却在跑旧版」。
|
|
19
|
+
const exe = process.platform === "win32" ? "forge.exe" : "forge";
|
|
20
|
+
const platKey = `${process.platform}-${process.arch}`;
|
|
21
|
+
const platformPkg = `@agent_forge/forge-${platKey}`;
|
|
78
22
|
|
|
79
23
|
// 支持的平台白名单(goreleaser 构建矩阵:linux/darwin × amd64/arm64 + windows/amd64)。
|
|
80
|
-
// 不在此列的平台(如 windows-arm64
|
|
81
|
-
// 不混入"子包未装"的静默 approve,避免用户误以为 Forge 正常工作却零拦截。
|
|
24
|
+
// 不在此列的平台(如 windows-arm64)无对应子包——明确 stderr 提示,不静默退出 0。
|
|
82
25
|
const SUPPORTED = new Set([
|
|
83
26
|
"darwin-arm64", "darwin-x64", "linux-arm64", "linux-x64", "win32-x64",
|
|
84
27
|
]);
|
|
85
|
-
|
|
28
|
+
|
|
29
|
+
function die(message, fix) {
|
|
30
|
+
console.error(`[forge] ${message}\n修复: ${fix}`);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
86
34
|
if (!SUPPORTED.has(platKey)) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
`unsupported platform: ${platKey} (no prebuilt binary); hooks will not fire`,
|
|
35
|
+
die(
|
|
36
|
+
`unsupported platform: ${platKey} (no prebuilt binary)`,
|
|
90
37
|
"该平台没有预编译二进制(支持 darwin/linux 的 arm64/x64 与 win32-x64);改用受支持平台,或从源码构建 forge 放进 PATH。"
|
|
91
38
|
);
|
|
92
39
|
}
|
|
93
40
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
41
|
+
// __dirname 是 …/node_modules/@agent_forge/forge。平台子包可能落在两处,都要找:
|
|
42
|
+
// 1) 包内嵌套:…/@agent_forge/forge/node_modules/@agent_forge/forge-<plat>/bin(全局安装实测)
|
|
43
|
+
// 2) 兄弟目录:…/@agent_forge/forge-<plat>/bin(本地安装被 hoist 时的常见形态)
|
|
44
|
+
// 不在此列的位置(祖先 node_modules)刻意不找——那正是 1.42.2 祖先遮蔽的来源。
|
|
45
|
+
const candidates = [
|
|
46
|
+
path.join(__dirname, "node_modules", platformPkg, "bin", exe),
|
|
47
|
+
path.join(__dirname, "..", `forge-${platKey}`, "bin", exe),
|
|
48
|
+
];
|
|
49
|
+
const binaryPath = candidates.find((p) => fs.existsSync(p));
|
|
50
|
+
if (binaryPath === undefined) {
|
|
51
|
+
die(
|
|
52
|
+
`platform package ${platformPkg} is not installed`,
|
|
103
53
|
"重装并保留 optionalDependencies:npm i -g @agent_forge/forge --include=optional(不要带 --omit=optional)。"
|
|
104
54
|
);
|
|
105
55
|
}
|
|
106
56
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
57
|
+
let child;
|
|
58
|
+
try {
|
|
59
|
+
child = spawn(binaryPath, process.argv.slice(2), {
|
|
60
|
+
stdio: "inherit",
|
|
61
|
+
env: process.env,
|
|
62
|
+
});
|
|
63
|
+
} catch (e) {
|
|
64
|
+
// spawn 也可能同步抛(Windows 上无效 exe 报 spawn UNKNOWN)——不能让它变成
|
|
65
|
+
// 带栈迹的 node 崩溃,更不能用退出码 0 掩盖失败。
|
|
66
|
+
die(
|
|
67
|
+
`spawn failed: ${e.message}`,
|
|
68
|
+
`检查 ${binaryPath} 是否存在且可执行(chmod +x);仍失败则重装:npm i -g @agent_forge/forge --include=optional。`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
111
71
|
|
|
112
|
-
// spawn 失败走 error 事件而非 exit
|
|
113
|
-
// fail-open 契约(宿主表现为 hook 异常而非 approve)。
|
|
72
|
+
// spawn 失败走 error 事件而非 exit——没有监听器会带栈迹崩溃并把失败说成成功。
|
|
114
73
|
child.on("error", (e) => {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
`spawn failed: ${e.message}; hooks will not fire`,
|
|
74
|
+
die(
|
|
75
|
+
`spawn failed: ${e.message}`,
|
|
118
76
|
`检查 ${binaryPath} 是否存在且可执行(chmod +x);仍失败则重装:npm i -g @agent_forge/forge --include=optional。`
|
|
119
77
|
);
|
|
120
78
|
});
|
|
121
79
|
|
|
122
|
-
// exit 的 code 为 null 表示被信号杀死(OOM/SIGKILL
|
|
123
|
-
//
|
|
80
|
+
// exit 的 code 为 null 表示被信号杀死(OOM/SIGKILL)——不能折叠成成功语义。
|
|
81
|
+
// 保留子进程的信号终止语义(128+signal),并给出可见提示。
|
|
124
82
|
child.on("exit", (code, signal) => {
|
|
125
83
|
if (code === null) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
"forge 被信号终止(常见于内存不足或被安全软件杀掉);重试,持续复现请带上本行报错反馈。"
|
|
84
|
+
console.error(
|
|
85
|
+
`[forge] forge exited on signal ${signal}\n` +
|
|
86
|
+
"修复: forge 被信号终止(常见于内存不足或被安全软件杀掉);重试,持续复现请带上本行报错反馈。"
|
|
130
87
|
);
|
|
88
|
+
process.exit(signal ? 128 + signal : 1);
|
|
131
89
|
}
|
|
132
|
-
process.exit(code
|
|
133
|
-
});
|
|
90
|
+
process.exit(code);
|
|
91
|
+
});
|