focus-guard 3.3.3 → 3.4.1
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/README.md +6 -4
- package/README.zh-CN.md +4 -4
- package/docs/ARCH-MAP.md +8 -2
- package/docs/BENCH.md +0 -3
- package/docs/BRAIN-VIZ.md +60 -0
- package/docs/GATE-INPUTS.json +3 -3
- package/docs/MEMORY-4.5-PLAN.md +3 -3
- package/docs/RATCHET.json +16 -0
- package/docs/RELEASE-POLICY.md +16 -0
- package/docs/VERSION-PLAN.md +58 -6
- package/hooks/guard.mjs +2 -2
- package/package.json +20 -8
- package/skills/focus-thinking/SKILL.md +1 -1
- package/src/adapters/dsh/eligibility-gate.mjs +1 -1
- package/src/core/constants.mjs +5 -5
- package/src/core/env.mjs +122 -2
- package/src/core/patterns.mjs +51 -3
- package/src/core/redlines.mjs +1 -1
- package/src/core/state.mjs +95 -59
- package/src/dsh/audit.mjs +40 -24
- package/src/dsh/guard/evidence.mjs +9 -2
- package/src/dsh/guard/hardCheck.mjs +12 -1
- package/src/dsh/guard/runtime.mjs +7 -1
- package/src/dsh/index.js +52 -54
- package/src/dsh/seams/prompt-rules.mjs +30 -3
- package/src/dsh/seams/session-start.mjs +131 -12
- package/src/dsh/seams.mjs +6 -2
- package/src/dsh/tools.mjs +74 -0
- package/src/memory/workspace.mjs +10 -3
- package/tools/brain-cosmic.mjs +43 -0
- package/tools/brain.mjs +8 -4
- package/tools/capture-fixtures.mjs +15 -1
- package/tools/docs-lint.mjs +4 -4
- package/tools/env-fingerprint.mjs +126 -33
- package/tools/lib/envMap.mjs +31 -0
- package/tools/lib/open.mjs +38 -0
- package/tools/manifest-sync.mjs +111 -0
- package/tools/privacy-guard.mjs +19 -3
- package/tools/selftest.mjs +7 -3
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { decodeSession as decode } from './lib/sessionDump.mjs'
|
|
17
|
-
import { existsSync, mkdirSync, readdirSync, statSync, writeFileSync, rmSync } from 'node:fs'
|
|
17
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync, rmSync } from 'node:fs'
|
|
18
18
|
import { dirname, join, basename } from 'node:path'
|
|
19
19
|
import { fileURLToPath } from 'node:url'
|
|
20
20
|
|
|
@@ -23,6 +23,19 @@ const CORE = join(HERE, '..')
|
|
|
23
23
|
const SESSIONS = process.env.FG_SESSIONS_DIR || 'E:/DSH/.dsh/sessions'
|
|
24
24
|
const OUT = join(CORE, 'tests', 'fixtures', 'real-calls')
|
|
25
25
|
|
|
26
|
+
// 本机环境闸的规则来源是 `.ai/env-fingerprint.json`——**私有面文件,不入版本库**。
|
|
27
|
+
// 不把 map 一起记下来,这类样本就只能在录制机上复现:干净 clone 上该闸按设计 fail-open,
|
|
28
|
+
// 重放必报「漏拦」。记下来,重放时用 `FG_ENV_FP_FILE` 指到临时文件即可还原现场。
|
|
29
|
+
const ENV_FP = join(CORE, '..', '.ai', 'env-fingerprint.json')
|
|
30
|
+
const ENV_MAP = (() => {
|
|
31
|
+
try {
|
|
32
|
+
const j = JSON.parse(readFileSync(ENV_FP, 'utf8'))
|
|
33
|
+
return j && j.map && typeof j.map === 'object' ? j.map : null
|
|
34
|
+
} catch {
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
})()
|
|
38
|
+
|
|
26
39
|
const argv = process.argv.slice(2)
|
|
27
40
|
const LIMIT = Number(argv.includes('--limit') ? argv[argv.indexOf('--limit') + 1] : 30) || 30
|
|
28
41
|
const MAX_BYTES = 3 * 1024 * 1024
|
|
@@ -124,6 +137,7 @@ for (const { f } of files) {
|
|
|
124
137
|
args: call.args,
|
|
125
138
|
verdict: denied ? 'deny' : 'pass',
|
|
126
139
|
reason: denied ? text.replace(/\\n/g, ' ').slice(0, 160) : null,
|
|
140
|
+
...(denied && /本机环境/.test(text) && ENV_MAP ? { envMap: ENV_MAP } : {}),
|
|
127
141
|
})
|
|
128
142
|
}
|
|
129
143
|
}
|
package/tools/docs-lint.mjs
CHANGED
|
@@ -34,11 +34,11 @@ const BUDGET = 0
|
|
|
34
34
|
/**
|
|
35
35
|
* 按规则的存量豁免(3.3 A)。
|
|
36
36
|
*
|
|
37
|
-
* `dead-code-path
|
|
38
|
-
*
|
|
39
|
-
*
|
|
37
|
+
* 原为 `{ 'dead-code-path': 5 }`——那 5 条全在 CHANGELOG 的历史段里,指向当年存在、后来搬走的文件。
|
|
38
|
+
* 2026-11-07 历史段(≤3.0.7)原样移入 `docs/archive/`(不在扫描面内),存量归零,额度一并收回。
|
|
39
|
+
* 判据:豁免是**存量**的记账。存量没了还留着额度,等于给未来的死链预留藏身处。
|
|
40
40
|
*/
|
|
41
|
-
const RULE_BUDGET = { 'dead-code-path':
|
|
41
|
+
const RULE_BUDGET = { 'dead-code-path': 0 }
|
|
42
42
|
|
|
43
43
|
const files = listMarkdownFiles(ROOT)
|
|
44
44
|
const findings = [
|
|
@@ -1,16 +1,26 @@
|
|
|
1
|
-
// FocusGuard
|
|
1
|
+
// FocusGuard 环境档案生成器(v2,2026-11-09)
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
3
|
+
// 探测平台、可用执行体、工具可用性,产出 <项目根>/.ai/env-fingerprint.json。
|
|
4
|
+
// 该文件同时是两件事的来源:
|
|
5
|
+
// ① 「命令硬校验」的规则表(map):key = 本机不鼓励直接用的命令,value = 替代命令;
|
|
6
|
+
// ② 环境档案(machine / shells / probe):给模型看「这台机器有什么、哪个是自带的」。
|
|
5
7
|
// 指纹缺失时硬校验 fail-open(跳过 + warn),不会阻断任何命令。
|
|
6
8
|
//
|
|
7
|
-
// 用法:node packages/core/tools/env-fingerprint.mjs [--out <路径>]
|
|
9
|
+
// 用法:node packages/core/tools/env-fingerprint.mjs [--out <路径>] [--shell <id>]
|
|
10
|
+
// --shell 本会话实际执行体(由适配层传入)。不传则记探测到的首选,并标 source=probe。
|
|
11
|
+
//
|
|
12
|
+
// TTL 不固定:连续探测结果不变则间隔翻倍(6h → 12h → 24h … 上限 30 天),一变立刻回到 6h。
|
|
13
|
+
// 判据:环境是低频变化的东西,天天重探是浪费;但一旦变了(装了新终端),必须马上看见。
|
|
8
14
|
|
|
9
|
-
import { writeFileSync, mkdirSync, existsSync } from "node:fs";
|
|
15
|
+
import { writeFileSync, mkdirSync, existsSync, readFileSync } from "node:fs";
|
|
10
16
|
import { join, dirname, delimiter } from "node:path";
|
|
11
17
|
import { execFileSync } from "node:child_process";
|
|
18
|
+
import { hostname } from "node:os";
|
|
19
|
+
import { buildEnvMap, droppedAlternatives } from "./lib/envMap.mjs";
|
|
20
|
+
import { osLabel, probeShellDetails } from "../src/core/env.mjs";
|
|
12
21
|
|
|
13
22
|
// PATH 补全:宿主进程的 PATH 可能是启动时的旧快照,补上常见安装位置后再探测(只影响本脚本)。
|
|
23
|
+
// 注意:这不是"假设装在 C 盘"——只是把 scoop/winget 的 shim 目录补进 PATH,探不到就当没有。
|
|
14
24
|
const EXTRA_PATH_DIRS = [
|
|
15
25
|
join(process.env.USERPROFILE || "", "scoop", "shims"),
|
|
16
26
|
join(process.env.LOCALAPPDATA || "", "Microsoft", "WinGet", "Links"),
|
|
@@ -22,6 +32,10 @@ const MAP = { grep: "rg", find: "fd", ls: "eza", cat: "bat", sed: "sd" };
|
|
|
22
32
|
// 探测范围:map 的 key 与 value,外加常用运行时与增强工具
|
|
23
33
|
const PROBE = ["rg", "fd", "eza", "bat", "sd", "grep", "sed", "less", "jq", "yq", "fzf", "zoxide", "node", "python", "git"];
|
|
24
34
|
|
|
35
|
+
/** TTL 自适应:首次 6 小时,连续未变逐次翻倍,上限 30 天。 */
|
|
36
|
+
const TTL_FIRST = 6 * 3600e3;
|
|
37
|
+
const TTL_CEIL = 30 * 86400e3;
|
|
38
|
+
|
|
25
39
|
function probe(tool) {
|
|
26
40
|
for (const args of [["--version"], ["-v"], ["--help"]]) {
|
|
27
41
|
try {
|
|
@@ -34,25 +48,6 @@ function probe(tool) {
|
|
|
34
48
|
return false;
|
|
35
49
|
}
|
|
36
50
|
|
|
37
|
-
function shellVersion(exe) {
|
|
38
|
-
try {
|
|
39
|
-
return execFileSync(exe, ["-NoProfile", "-Command", "$PSVersionTable.PSVersion.ToString()"], { encoding: "utf8", timeout: 6000 }).trim();
|
|
40
|
-
} catch {
|
|
41
|
-
return "";
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function detectShell() {
|
|
46
|
-
if (process.platform !== "win32") return String(process.env.SHELL || "sh").split(/[\\/]/).pop() || "sh";
|
|
47
|
-
// Windows:ComSpec 恒指向 cmd.exe,会误判;以实际可用的现代 shell 为准,并带版本号。
|
|
48
|
-
const pwsh = shellVersion("pwsh");
|
|
49
|
-
if (pwsh) return `pwsh ${pwsh}`;
|
|
50
|
-
const ps = shellVersion("powershell");
|
|
51
|
-
if (ps) return `powershell ${ps}`;
|
|
52
|
-
const cs = String(process.env.ComSpec || "");
|
|
53
|
-
return cs.toLowerCase().includes("cmd") ? "cmd" : "unknown";
|
|
54
|
-
}
|
|
55
|
-
|
|
56
51
|
function detectEncoding() {
|
|
57
52
|
if (process.platform !== "win32") return "UTF-8";
|
|
58
53
|
try {
|
|
@@ -63,26 +58,124 @@ function detectEncoding() {
|
|
|
63
58
|
}
|
|
64
59
|
}
|
|
65
60
|
|
|
61
|
+
/**
|
|
62
|
+
* 命令工具的**语法实现**探测(2026-11-09)。
|
|
63
|
+
*
|
|
64
|
+
* 为什么需要:同一条命令在不同实现上行为不同,而且差别是**静默**的——
|
|
65
|
+
* · BSD sed 的 `-i` 必须带后缀(`sed -i '' …`),GNU sed 带了反而把后缀当文件名;
|
|
66
|
+
* · BSD grep 没有 `-P`(Perl 正则);
|
|
67
|
+
* · GNU find 有 `-printf`,BSD find 没有。
|
|
68
|
+
* 原来这套判断靠 `os === 'darwin'` 硬推——macOS 上 `brew install gnu-sed` 之后就全错了。
|
|
69
|
+
* 现在改成探**实际实现**:跑一次 `--version` 看它自报什么(BSD 工具不认这个参数,
|
|
70
|
+
* 会往 stderr 吐 "illegal option",那就判 bsd)。
|
|
71
|
+
*
|
|
72
|
+
* 只在生成器里跑(后台 spawn),会话启动只读档案——探测本身要花几百毫秒。
|
|
73
|
+
*/
|
|
74
|
+
function probeCommand(name) {
|
|
75
|
+
const run = (args) => {
|
|
76
|
+
try {
|
|
77
|
+
return String(execFileSync(name, args, { encoding: "utf8", timeout: 2500, stdio: ["ignore", "pipe", "pipe"] }) || "");
|
|
78
|
+
} catch (e) {
|
|
79
|
+
// BSD 工具把 --version 当非法参数,信息在 stderr;有输出就用它判型。
|
|
80
|
+
return String((e && (e.stdout || e.stderr)) || "");
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const v = run(["--version"]);
|
|
84
|
+
if (!v.trim()) return null;
|
|
85
|
+
let impl = "unknown";
|
|
86
|
+
if (/GNU/i.test(v)) impl = "gnu";
|
|
87
|
+
else if (/BSD/i.test(v)) impl = "bsd";
|
|
88
|
+
else if (/busybox/i.test(v)) impl = "busybox";
|
|
89
|
+
else if (/illegal option|invalid option/i.test(v)) impl = "bsd";
|
|
90
|
+
else if (/ripgrep/i.test(v)) impl = "ripgrep";
|
|
91
|
+
const ver = (v.match(/\b(\d+\.\d+(?:\.\d+)?)\b/) || [])[1] || "";
|
|
92
|
+
return { impl, version: ver };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 只探这几个有"同工具不同语法"问题的;其余工具没有跨实现的行为差异。 */
|
|
96
|
+
const SYNTAX_PROBES = ["grep", "sed", "find", "awk", "xargs", "tar"];
|
|
97
|
+
|
|
98
|
+
const argOf = (name) => {
|
|
99
|
+
const i = process.argv.indexOf(name);
|
|
100
|
+
return i >= 0 ? process.argv[i + 1] : undefined;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const out = argOf("--out") || join(process.cwd(), ".ai", "env-fingerprint.json");
|
|
104
|
+
|
|
105
|
+
/** 读旧档案:机器编号、首次见到的时刻、已有反馈都要继承,不能每次重探就重发一个新编号。 */
|
|
106
|
+
let prev = null;
|
|
107
|
+
try {
|
|
108
|
+
prev = JSON.parse(readFileSync(out, "utf8"));
|
|
109
|
+
} catch {
|
|
110
|
+
/* 首次生成,或文件损坏 —— 按新建处理 */
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
const shells = probeShellDetails();
|
|
115
|
+
// 命令语法实现(GNU / BSD / busybox + 版本)。只探有跨实现差异的那几个。
|
|
116
|
+
const commands = {};
|
|
117
|
+
for (const name of SYNTAX_PROBES) {
|
|
118
|
+
const p = probeCommand(name);
|
|
119
|
+
if (p) commands[name] = p;
|
|
120
|
+
}
|
|
66
121
|
const available = [];
|
|
67
122
|
const unavailable = [];
|
|
68
123
|
for (const t of PROBE) (probe(t) ? available : unavailable).push(t);
|
|
69
124
|
|
|
125
|
+
// 机器编号:首次分配 A;已有则沿用;FG_MACHINE_ID 可覆盖(多台机器时手动指定 B/C…)。
|
|
126
|
+
const machine = {
|
|
127
|
+
id: String(process.env.FG_MACHINE_ID || prev?.machine?.id || "A").trim() || "A",
|
|
128
|
+
host: hostname(),
|
|
129
|
+
arch: process.arch,
|
|
130
|
+
firstSeen: prev?.machine?.firstSeen || new Date(now).toISOString(),
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// 「变了没有」只看平台与执行体集合——工具装了新版本不该把 TTL 重置。
|
|
134
|
+
const shellKey = shells.map((s) => s.id).join(",");
|
|
135
|
+
const changed = Boolean(prev) && (prev?.platform?.os !== process.platform || prev?.shells?.map?.((s) => s.id).join(",") !== shellKey);
|
|
136
|
+
const streak = changed ? 0 : Math.min((prev?.probe?.streak || 0) + 1, 12);
|
|
137
|
+
const ttlMs = Math.min(TTL_FIRST * 2 ** streak, TTL_CEIL);
|
|
138
|
+
|
|
139
|
+
const sessionShell = String(argOf("--shell") || "").trim();
|
|
70
140
|
const fingerprint = {
|
|
71
|
-
|
|
72
|
-
|
|
141
|
+
schema: 2,
|
|
142
|
+
machine,
|
|
143
|
+
platform: {
|
|
144
|
+
os: process.platform,
|
|
145
|
+
// win32 是 Node 的 API 名,不是"32 位系统"——对外一律给 Windows/x64。
|
|
146
|
+
osLabel: osLabel(process.platform, process.arch),
|
|
147
|
+
shell: sessionShell || shells[0]?.id || "unknown",
|
|
148
|
+
shellSource: sessionShell ? "session" : "probe",
|
|
149
|
+
encoding: detectEncoding(),
|
|
150
|
+
},
|
|
151
|
+
shells,
|
|
152
|
+
// 同一条命令在不同实现上的语法差异(BSD sed 的 -i 要后缀、BSD grep 无 -P、GNU find 有 -printf)。
|
|
153
|
+
// 报告给模型,让它写命令前就知道该用哪套写法——这比事后拦一条更省事。
|
|
154
|
+
commands,
|
|
73
155
|
tools: { available, unavailable },
|
|
74
|
-
map:
|
|
156
|
+
map: buildEnvMap(MAP, available),
|
|
157
|
+
probe: {
|
|
158
|
+
at: new Date(now).toISOString(),
|
|
159
|
+
ttlMs,
|
|
160
|
+
streak,
|
|
161
|
+
changed,
|
|
162
|
+
nextAt: new Date(now + ttlMs).toISOString(),
|
|
163
|
+
},
|
|
164
|
+
// 模型/人类回填的补充说明(fg_env 工具写入)。探测只看得到 PATH,看不到"这个 pwsh 是谁装的"。
|
|
165
|
+
notes: Array.isArray(prev?.notes) ? prev.notes : [],
|
|
75
166
|
};
|
|
76
167
|
|
|
77
|
-
const outIdx = process.argv.indexOf("--out");
|
|
78
|
-
const out = outIdx >= 0 ? process.argv[outIdx + 1] : join(process.cwd(), ".ai", "env-fingerprint.json");
|
|
79
168
|
mkdirSync(dirname(out), { recursive: true });
|
|
80
169
|
writeFileSync(out, JSON.stringify(fingerprint, null, 2) + "\n", "utf8");
|
|
81
170
|
|
|
82
|
-
const missingAlt =
|
|
171
|
+
const missingAlt = droppedAlternatives(MAP, available);
|
|
172
|
+
const shellText = shells.map((s) => `${s.id}(${s.origin === "builtin" ? "自带" : "额外"})`).join(" ");
|
|
83
173
|
console.log(`env-fingerprint:${out}`);
|
|
84
|
-
console.log(`
|
|
85
|
-
console.log(`
|
|
174
|
+
console.log(` 机器 ${machine.id} · ${machine.host} · ${fingerprint.platform.osLabel}`);
|
|
175
|
+
console.log(` 执行体 ${fingerprint.platform.shell}(来源 ${fingerprint.platform.shellSource})/ 可用 ${shellText}`);
|
|
176
|
+
console.log(` 编码 ${fingerprint.platform.encoding} / 下次重探 ${fingerprint.probe.nextAt}(${Math.round(ttlMs / 3600e3)}h,连续未变 ${streak} 次)`);
|
|
177
|
+
console.log(` 命令语法 ${Object.entries(commands).map(([k, v]) => `${k}=${v.impl}${v.version ? " " + v.version : ""}`).join(" ") || "-"}`);
|
|
178
|
+
console.log(` 可用工具 ${available.join(" ") || "-"}`);
|
|
86
179
|
console.log(` 不可用 ${unavailable.join(" ") || "-"}`);
|
|
87
180
|
console.log(` map ${Object.entries(MAP).map(([k, v]) => `${k}→${v}`).join(" ")}`);
|
|
88
|
-
if (missingAlt.length) console.log(` [
|
|
181
|
+
if (missingAlt.length) console.log(` [已剔除] 替代工具未安装:${missingAlt.join(" ")}——这些 key 不进 map,硬校验不会拒它们(否则会建议一个不存在的命令)`);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 环境指纹的 `map` 表:只保留**替代品真实存在**的条目。
|
|
3
|
+
*
|
|
4
|
+
* 病根(2026-11-07):原实现是 `map: { ...MAP }`——无条件写全 5 条。
|
|
5
|
+
* 没装 `bat` 的机器上照样生成 `cat → bat`,于是第 1.5 层拒绝 `cat`,并建议一个
|
|
6
|
+
* 不存在的命令。拦得对(确实该换工具),但替代品是空的——这是**假阳性的生成器**。
|
|
7
|
+
* 当时 `missingAlt` 只打印警告、不改表,警告与行为正好相反。
|
|
8
|
+
*
|
|
9
|
+
* 为什么抽成纯函数:判据要能单独被测量。生成器本体要探测真实工具,测它就得伪造 PATH,
|
|
10
|
+
* 而"按可用性裁剪"这件事本身与探测无关,不该绑在子进程上。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {Record<string,string>} map 候选替代表(原命令 → 更好的替代品)
|
|
15
|
+
* @param {string[]} available 本机探测到可用的工具名
|
|
16
|
+
* @returns {Record<string,string>} 只含替代品可用的条目
|
|
17
|
+
*/
|
|
18
|
+
export function buildEnvMap(map, available) {
|
|
19
|
+
const have = new Set(available)
|
|
20
|
+
const out = {}
|
|
21
|
+
for (const [from, to] of Object.entries(map)) if (have.has(to)) out[from] = to
|
|
22
|
+
return out
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 被剔除的条目,形如 `cat→bat`;用于生成器提示「本机没有更好的替代品」。 */
|
|
26
|
+
export function droppedAlternatives(map, available) {
|
|
27
|
+
const have = new Set(available)
|
|
28
|
+
return Object.entries(map)
|
|
29
|
+
.filter(([, to]) => !have.has(to))
|
|
30
|
+
.map(([from, to]) => `${from}→${to}`)
|
|
31
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// 用系统默认程序打开一个文件(2026-11-08)
|
|
2
|
+
//
|
|
3
|
+
// 为什么单独一个文件:`brain.mjs`(模板版)与 `brain-cosmic.mjs`(本地皮层版)
|
|
4
|
+
// 都要「生成完顺手打开」。同一个三行分支抄两遍,下一次改平台适配就会只改一处——
|
|
5
|
+
// 重复块是这么长出来的,不是一开始就想抄。
|
|
6
|
+
//
|
|
7
|
+
// 用 detached + unref:启动器是 GUI 进程,不该把 CLI 挂着等它退出。
|
|
8
|
+
// 打不开不抛错:headless / 无关联程序的环境里这是常态,不是故障;
|
|
9
|
+
// 调用方在输出里已经给了绝对路径,兜底靠那一行。
|
|
10
|
+
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} file 绝对路径
|
|
15
|
+
* @returns {boolean} 是否成功发起(不代表对方一定打开)
|
|
16
|
+
*/
|
|
17
|
+
export function openInBrowser(file) {
|
|
18
|
+
/* win32 不再走 `cmd /c start`(2026-11-11,code-scanning #10 修复)。
|
|
19
|
+
cmd 会把参数**再解析一遍**,于是路径里的 `&` `|` `^` 都成了命令分隔符——
|
|
20
|
+
`.focus-guard/` 这种目录名不受影响,但工作区路径由用户决定,含 `&` 完全合法。
|
|
21
|
+
`explorer.exe` 不是 shell,参数经 CommandLineToArgvW 解析,不做元字符展开;
|
|
22
|
+
Node 的 spawn 在 win32 上会自动给含空格的参数加引号,所以空格也安全。
|
|
23
|
+
退出码不可靠(explorer 复用已有实例时常返回 1),这里本来就只看发起成功与否。 */
|
|
24
|
+
const [cmd, args] =
|
|
25
|
+
process.platform === "win32"
|
|
26
|
+
? ["explorer.exe", [file]]
|
|
27
|
+
: process.platform === "darwin"
|
|
28
|
+
? ["open", [file]]
|
|
29
|
+
: ["xdg-open", [file]];
|
|
30
|
+
try {
|
|
31
|
+
const p = spawn(cmd, args, { detached: true, stdio: "ignore", windowsHide: true });
|
|
32
|
+
p.on("error", () => {});
|
|
33
|
+
p.unref();
|
|
34
|
+
return true;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 清单文案单点(2026-11-08)
|
|
4
|
+
*
|
|
5
|
+
* 为什么有这个工具:
|
|
6
|
+
* 同一个插件的「名字 / 简介 / 显示名」曾经在 5 份 JSON 里各手写一遍——
|
|
7
|
+
* `packages/core/package.json`、仓库根 `marketplace.json`、
|
|
8
|
+
* `.claude-plugin/marketplace.json`、`.claude-plugin/plugin.json`、`.zcode-plugin/plugin.json`。
|
|
9
|
+
* 它们不能合并(每个宿主生态各要一个固定文件名),但**内容该只有一份**。
|
|
10
|
+
* 实测代价:自适应 TTL 早已从四档阶梯改成半衰期,`.zcode-plugin/plugin.json` 的简介
|
|
11
|
+
* 还写着「4h~7天」;「上下文污染检测(58条)」的条号也对不上。改了代码忘改副本,
|
|
12
|
+
* 这类漂移不会有任何闸门报错——因为每份副本都"自洽"。
|
|
13
|
+
*
|
|
14
|
+
* 真源:`packages/core/package.json` 的 `description` / `displayName` /
|
|
15
|
+
* `displayNameI18n` / `descriptionI18n`。
|
|
16
|
+
* 不新开真源文件:那等于为了收敛再加一个 JSON,本末倒置。
|
|
17
|
+
*
|
|
18
|
+
* 边界:
|
|
19
|
+
* ① **只写各文件本来就有的字段**。没有 `displayName_i18n` 的清单不硬塞一个——
|
|
20
|
+
* 不替宿主发明 schema,也不替它删字段。
|
|
21
|
+
* ② **不动 version**。版本归 `tools/version.mjs --set` 管,两个工具各写一份就是新的散乱。
|
|
22
|
+
* ③ **保留原缩进与行尾**。清单是 CRLF,`JSON.stringify` 吐 LF,写一次就留一条假改动。
|
|
23
|
+
*
|
|
24
|
+
* 用法:
|
|
25
|
+
* node tools/manifest-sync.mjs # 只查,漂移即 exit 1
|
|
26
|
+
* node tools/manifest-sync.mjs --write # 以真源为准写回副本
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
30
|
+
import { dirname, join, resolve } from "node:path";
|
|
31
|
+
import { fileURLToPath } from "node:url";
|
|
32
|
+
|
|
33
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
34
|
+
const ROOT = resolve(HERE, "..", "..", "..");
|
|
35
|
+
const TRUTH = "packages/core/package.json";
|
|
36
|
+
|
|
37
|
+
/** 副本清单:`at` 指明文案落在顶层还是 `plugins[0]`(市场清单的形状)。 */
|
|
38
|
+
const COPIES = [
|
|
39
|
+
{ rel: "marketplace.json", at: "plugins" },
|
|
40
|
+
{ rel: ".claude-plugin/marketplace.json", at: "plugins" },
|
|
41
|
+
{ rel: ".claude-plugin/plugin.json", at: "top" },
|
|
42
|
+
{ rel: ".zcode-plugin/plugin.json", at: "top" },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
/** 文案字段:真源键 → 清单键。只同步目标里**已存在**的那些。 */
|
|
46
|
+
const FIELDS = [
|
|
47
|
+
["description", "description"],
|
|
48
|
+
["displayName", "displayName"],
|
|
49
|
+
["displayNameI18n", "displayName_i18n"],
|
|
50
|
+
["descriptionI18n", "description_i18n"],
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
function readJson(rel) {
|
|
54
|
+
return JSON.parse(readFileSync(join(ROOT, rel), "utf8"));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function targetOf(json, at) {
|
|
58
|
+
return at === "plugins" ? json.plugins?.[0] : json;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function main() {
|
|
62
|
+
const write = process.argv.includes("--write");
|
|
63
|
+
const truth = readJson(TRUTH);
|
|
64
|
+
const drifts = [];
|
|
65
|
+
|
|
66
|
+
for (const { rel, at } of COPIES) {
|
|
67
|
+
const path = join(ROOT, rel);
|
|
68
|
+
const raw = readFileSync(path, "utf8");
|
|
69
|
+
const json = JSON.parse(raw);
|
|
70
|
+
const target = targetOf(json, at);
|
|
71
|
+
if (!target) {
|
|
72
|
+
drifts.push(`${rel}:找不到 plugins[0],形状不对`);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
let touched = false;
|
|
76
|
+
for (const [srcKey, dstKey] of FIELDS) {
|
|
77
|
+
if (!(dstKey in target)) continue; // 不替宿主发明字段
|
|
78
|
+
const want = truth[srcKey];
|
|
79
|
+
if (want === undefined) continue;
|
|
80
|
+
if (JSON.stringify(target[dstKey]) !== JSON.stringify(want)) {
|
|
81
|
+
drifts.push(`${rel} ${dstKey}`);
|
|
82
|
+
target[dstKey] = want;
|
|
83
|
+
touched = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// 插件名必须一致;市场的 `name` 是市场标识,不在此列
|
|
87
|
+
if (target.name !== truth.name) {
|
|
88
|
+
drifts.push(`${rel} name`);
|
|
89
|
+
target.name = truth.name;
|
|
90
|
+
touched = true;
|
|
91
|
+
}
|
|
92
|
+
if (!touched) continue;
|
|
93
|
+
const eol = raw.includes("\r\n") ? "\r\n" : "\n";
|
|
94
|
+
const after = (JSON.stringify(json, null, 2) + "\n").replace(/\n/g, eol);
|
|
95
|
+
if (write && after !== raw) writeFileSync(path, after);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!drifts.length) {
|
|
99
|
+
console.log(`✓ 清单文案与真源一致(${TRUTH}):${COPIES.length} 份副本无漂移`);
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
const lines = drifts.map((d) => ` ✗ ${d}`).join("\n");
|
|
103
|
+
if (write) {
|
|
104
|
+
console.log(`✓ 已按真源写回 ${new Set(drifts.map((d) => d.split(" ")[0])).size} 份清单:\n${lines}`);
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
107
|
+
console.log(`✗ 清单文案与真源漂移 ${drifts.length} 处:\n${lines}\n\n 修法:node packages/core/tools/manifest-sync.mjs --write`);
|
|
108
|
+
return 1;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
process.exit(main());
|
package/tools/privacy-guard.mjs
CHANGED
|
@@ -214,13 +214,24 @@ function auditSurface(files) {
|
|
|
214
214
|
const notes = []
|
|
215
215
|
|
|
216
216
|
// —— 断言 A:exclude 可读 ——
|
|
217
|
+
//
|
|
218
|
+
// 2026-11-11:**没有清单 = 这台机器没有私有面**,A 不可判,记 skip 不记 fail。
|
|
219
|
+
// 起因:`npm run check` 2026-11-08 起进了发布闸(publish.yml),而 CI checkout 里
|
|
220
|
+
// `.ai/` 整个目录不入库 → `.git/info/exclude` 为空 → A 必红 → npm 发不出去。
|
|
221
|
+
// 这不是"忘了装"(那才是 A 要抓的),是"这台机器根本没有私有面配置"。
|
|
222
|
+
// 与 D 断言、`changelog-audience`(无 origin/main 时 skip)、`env-platform.test.mjs`
|
|
223
|
+
// (缺环境指纹时 skip)同一惯例:前置缺失不该报成缺陷。
|
|
224
|
+
// **检出能力保留**:本地清单还在,"有清单但 exclude 为空"照样红。
|
|
217
225
|
let rules = []
|
|
226
|
+
const hasCanonical = existsSync(CANONICAL_PATH)
|
|
218
227
|
if (!existsSync(EXCLUDE_PATH)) {
|
|
219
|
-
failures.push({ code: 'A', msg: '.git/info/exclude 不存在,私有面没有设防' })
|
|
228
|
+
if (hasCanonical) failures.push({ code: 'A', msg: '.git/info/exclude 不存在,私有面没有设防' })
|
|
229
|
+
else notes.push('未找到 .git/info/exclude 与清单备份——本机没有私有面,跳过 A(CI / 新 clone 属此列)')
|
|
220
230
|
} else {
|
|
221
231
|
rules = parseRules(readFileSync(EXCLUDE_PATH, 'utf8'))
|
|
222
232
|
if (rules.length === 0) {
|
|
223
|
-
failures.push({ code: 'A', msg: '.git/info/exclude
|
|
233
|
+
if (hasCanonical) failures.push({ code: 'A', msg: '清单备份存在但 .git/info/exclude 为空(换机器忘了装?)' })
|
|
234
|
+
else notes.push('exclude 为空且无清单备份——本机没有私有面,跳过 A(CI / 新 clone 属此列)')
|
|
224
235
|
}
|
|
225
236
|
}
|
|
226
237
|
if (rules.length === 0) return { rules, failures, notes }
|
|
@@ -288,7 +299,12 @@ function printHuman({ rules, files, failures, notes }, verbose) {
|
|
|
288
299
|
console.log(`[privacy-guard] 私有规则 ${rules.length} 条 · 已跟踪 ${files.length} 个文件`)
|
|
289
300
|
for (const note of notes) console.log(` · ${note}`)
|
|
290
301
|
if (failures.length === 0) {
|
|
291
|
-
|
|
302
|
+
// skip 时不能说「清单已装」——那是没检查过的事。文案必须跟着判据走。
|
|
303
|
+
console.log(
|
|
304
|
+
rules.length === 0
|
|
305
|
+
? '[privacy-guard] PASS:本机无私有面(A/D 不可判,已跳过)'
|
|
306
|
+
: '[privacy-guard] PASS:私有面干净(无已跟踪命中、无 .gitignore 点名、清单已装)',
|
|
307
|
+
)
|
|
292
308
|
return
|
|
293
309
|
}
|
|
294
310
|
for (const f of failures) {
|
package/tools/selftest.mjs
CHANGED
|
@@ -190,6 +190,10 @@ check('注册了 tools/pre-execute 监听', events.some(([t, e]) => t === 'on' &
|
|
|
190
190
|
)
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
+
/** 「没命中绝对红线、但被资格闸接管」的共同断言:A1/A2 三个样本都是这个形状,抄三遍就漂三份。 */
|
|
194
|
+
const notRedline = (gate) =>
|
|
195
|
+
(gate.kind === 'deny' || gate.kind === 'ask') && !/绝对红线/.test(String(gate.reason || ''))
|
|
196
|
+
|
|
193
197
|
console.warn('== 测试 A:第1层绝对红线(短路 deny,不进第2层)==')
|
|
194
198
|
{
|
|
195
199
|
const gate = await run({ name: 'bash', arguments: { command: 'rm -rf /' } })
|
|
@@ -223,7 +227,7 @@ console.warn('== 测试 A:第1层绝对红线(短路 deny,不进第2层)
|
|
|
223
227
|
const gate = await run({ name: 'bash', arguments: { command: 'rm -rf "./app-src"' } })
|
|
224
228
|
check(
|
|
225
229
|
'带引号的非根路径不命中红线(改由资格闸拦)',
|
|
226
|
-
(gate
|
|
230
|
+
notRedline(gate),
|
|
227
231
|
JSON.stringify(gate),
|
|
228
232
|
)
|
|
229
233
|
}
|
|
@@ -234,7 +238,7 @@ console.warn('== 测试 A2:第1层两档分流(普通 rm -rf 不进红线,
|
|
|
234
238
|
const gate = await run({ name: 'bash', arguments: { command: 'rm -rf ./test-dir' } })
|
|
235
239
|
check(
|
|
236
240
|
'rm -rf ./test-dir 不在红线(改由资格闸拦)',
|
|
237
|
-
(gate
|
|
241
|
+
notRedline(gate),
|
|
238
242
|
JSON.stringify(gate),
|
|
239
243
|
)
|
|
240
244
|
}
|
|
@@ -242,7 +246,7 @@ console.warn('== 测试 A2:第1层两档分流(普通 rm -rf 不进红线,
|
|
|
242
246
|
const gate = await run({ name: 'bash', arguments: { command: 'cd /tmp && rm -fr app-src/' } })
|
|
243
247
|
check(
|
|
244
248
|
'rm -fr app-src/ 不在红线(改由资格闸拦)',
|
|
245
|
-
(gate
|
|
249
|
+
notRedline(gate),
|
|
246
250
|
)
|
|
247
251
|
}
|
|
248
252
|
{
|