dsh-git-push 2.9.3 → 2.9.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.
Files changed (46) hide show
  1. package/README.md +22 -25
  2. package/lib/app/apply.js +8 -1
  3. package/lib/app/audit-api.js +4 -1
  4. package/lib/app/command-registry.js +17 -11
  5. package/lib/app/handlers/meta.js +38 -0
  6. package/lib/app/handlers/repo-actions.js +3 -1
  7. package/lib/app/handlers/settings.js +0 -0
  8. package/lib/app/http-handlers.js +3 -1
  9. package/lib/app/schema.js +0 -0
  10. package/lib/app/settings-bridge.js +0 -0
  11. package/lib/app/tool-call.js +26 -21
  12. package/lib/ast/control-flow.js +12 -3
  13. package/lib/ast/size.js +5 -2
  14. package/lib/audit/audit-file.js +4 -2
  15. package/lib/audit/file-context.js +89 -0
  16. package/lib/audit/finding.js +45 -2
  17. package/lib/audit/index.js +2 -2
  18. package/lib/audit/orchestrate.js +6 -1
  19. package/lib/checks/button-bind.js +25 -2
  20. package/lib/checks/regex.js +7 -3
  21. package/lib/commit-push.js +20 -2
  22. package/lib/core/exec.js +6 -4
  23. package/lib/exempt/index.js +72 -26
  24. package/lib/exempt/tool.js +198 -0
  25. package/lib/git/clone-history-mode.js +24 -5
  26. package/lib/git/push.js +28 -13
  27. package/lib/git/transport.js +49 -17
  28. package/lib/git/visibility.js +93 -9
  29. package/lib/npm/publish.js +27 -0
  30. package/lib/npm/registry.js +5 -2
  31. package/lib/self/index.js +1 -1
  32. package/package.json +2 -4
  33. package/skills/dev/dsh-git-push.md +1 -2
  34. package/skills/guide/guide-cli-scripts.md +3 -3
  35. package/skills/guide/guide-inject-ui.md +4 -4
  36. package/assets/minihost.sh +0 -35
  37. package/assets/preview.html +0 -36703
  38. package/lib/fs/edit-after-read.js +0 -142
  39. package/scripts/frontend-preview-check.mjs +0 -124
  40. package/scripts/preview-gen.mjs +0 -489
  41. package/scripts/preview-local-runner.mjs +0 -55
  42. package/scripts/preview-server-direct.mjs +0 -116
  43. package/scripts/preview-server.mjs +0 -305
  44. package/scripts/preview-slot-data.mjs +0 -123
  45. package/scripts/sync-plugin-install.mjs +0 -286
  46. package/scripts/sync-plugin.mjs +0 -284
@@ -1,142 +0,0 @@
1
- /**
2
- * 「读改合一」编辑核心(工具 edit_after_read 的实现层;纯逻辑、可单测、无副作用注入面)。
3
- *
4
- * 【为什么需要】宿主的编辑守卫只认「用 read 工具读过」,而**真实读过内容**的手段很多
5
- * (sed -n / cat / node -e / grep 上下文等)——守卫是工具偏见,不是安全边界。
6
- * 本核心把「读」与「改」合成一步:**自己读当前文件** → 校验 → 字面替换 → 写回。
7
- * 于是「确实读过」由构造保证,且比宿主守卫更严:
8
- * ① **版本比对**:读后到写前若文件被改动(mtime/size 变化)→ 拒绝,提示重读;
9
- * ② **唯一性校验**:默认要求 old 在文件里**恰好出现一次**(防误替换),需要多处替换显式传 all:true;
10
- * ③ **字面匹配**:old 按字面(含空白)精确匹配,不做正则解释,避免「以为在改 A 实际改了 B」。
11
- *
12
- * 【不做的事】不解析语法、不做结构化匹配 —— 那是 ast-grep 的活;本核心只管「安全的字面替换」。
13
- */
14
-
15
- import { readFileSync, statSync, writeFileSync } from 'node:fs';
16
-
17
- /** 统计不重叠出现次数。 */
18
- export function countOccurrences(text, needle) {
19
- if (!needle) return 0;
20
- let n = 0;
21
- let idx = text.indexOf(needle);
22
- while (idx !== -1) {
23
- n += 1;
24
- idx = text.indexOf(needle, idx + needle.length);
25
- }
26
- return n;
27
- }
28
-
29
- /** 文件版本指纹(mtime 毫秒 + 字节数)——用于「读后是否被改动」的比对。 */
30
- function fingerprint(statFile, path) {
31
- try {
32
- const st = statFile(path);
33
- return `${st.mtimeMs}:${st.size}`;
34
- } catch {
35
- return '';
36
- }
37
- }
38
-
39
- /**
40
- * 读当前文件 → 校验 → 字面替换 → 写回。
41
- * @param {object} args
42
- * @param {string} args.path 目标文件绝对路径(亦接受宿主的 file_path)
43
- * @param {string} args.old 要被替换的字面文本(含空白,必须精确匹配;亦接受 old_string)
44
- * @param {string} args.new 替换后的文本(**必传**;要删除请显式传空串 '';亦接受 new_string)
45
- * @param {boolean} [args.all] 是否替换全部匹配(默认 false:要求唯一匹配;亦接受 replace_all)
46
- * @param {Function} [args.readFile] 读函数(测试注入)
47
- * @param {Function} [args.writeFile] 写函数(测试注入)
48
- * @param {Function} [args.statFile] 状态函数(测试注入)
49
- * @returns {{ok:boolean, path:string, replaced:number, bytesBefore:number, bytesAfter:number, reason?:string}}
50
- */
51
- export function editAfterRead({
52
- path: pathArg,
53
- old: oldArg,
54
- new: newArg,
55
- all: allArg,
56
- allowDuplicate: allowDupArg,
57
- file_path: filePathAlias,
58
- old_string: oldStringAlias,
59
- new_string: newStringAlias,
60
- replace_all: replaceAllAlias,
61
- readFile = readFileSync,
62
- writeFile = writeFileSync,
63
- statFile = statSync,
64
- }) {
65
- // 参数名与宿主编辑工具对齐(file_path/old_string/new_string/replace_all),同时保留本插件原名,
66
- // 两套名字都认 —— 避免「记混了参数名」把内容改坏(真实事故:宿主名 + 本工具默认空串 ⇒ 内容被删)。
67
- const path = pathArg ?? filePathAlias;
68
- const old = oldArg ?? oldStringAlias;
69
- const next = newArg ?? newStringAlias;
70
- const all = (allArg ?? replaceAllAlias) === true;
71
- const allowDuplicate = allowDupArg === true;
72
- const fail = (reason) => ({ ok: false, path, replaced: 0, bytesBefore: 0, bytesAfter: 0, reason });
73
- if (!path) return fail('缺少 path(或 file_path)');
74
- if (!old) return fail('缺少 old(要被替换的字面文本;或 old_string)');
75
- // ⚠️ 安全闸:new 缺失**不再**当删除 —— 缺参数时报错,删除必须显式传空串。
76
- if (next === undefined || next === null) {
77
- return fail("缺少 new(替换文本;或 new_string)。要删除该内容请显式传 new: ''");
78
- }
79
-
80
- const before = fingerprint(statFile, path);
81
- let text;
82
- try {
83
- text = String(readFile(path, 'utf8'));
84
- } catch (e) {
85
- return fail(`读取失败:${(e && e.message) || e}`);
86
- }
87
-
88
- const hits = countOccurrences(text, old);
89
- if (hits === 0) return fail('未找到 old(字面精确匹配失败:注意空白/换行必须完全一致)');
90
- if (hits > 1 && !all) return fail(`old 出现 ${hits} 次,默认要求唯一匹配(确认要全改请传 all:true)`);
91
-
92
- // 读后到写前若被改动 → 拒绝,避免覆盖别人的修改
93
- const after = fingerprint(statFile, path);
94
- if (before && after && before !== after) {
95
- return fail('读后文件已被改动(版本变化),已拒绝写入;请重新读取后再改');
96
- }
97
-
98
- const out = all ? text.split(old).join(next) : text.replace(old, next);
99
-
100
- // ── 写前防呆(两道,专治"文档被写重"):实测事故 2026-10-16,一份 17.7KB 文档被改成
101
- // ~2 倍体积、`## 一`/`## 二` 各出现两次 —— 形态是「old 只是个小锚点,而 new 里带了**整份文档**」。
102
- // 复现结论:工具本身不会写重(new 含 old、同一 old 连调两次都不重 ✗)⇒ 重复来自参数形态;
103
- // 所以这里把该形态**拦在写盘之前**,而不是写坏文件再让人去 git checkout 恢复 ✓
104
- // ① 产物里若**包含整份原文**(text)⇒ new 必定把原文整份带上了 ⇒ 拒绝(正常编辑/追加都不会满足)
105
- // ② 产物里 old 出现多次 ⇒ 同样是"原文被带上"的另一种形态 ⇒ 拒绝
106
- // 两道都可显式 allowDuplicate:true 放行(确实要保留重复时)
107
- if (!allowDuplicate) {
108
- if (out.includes(text)) {
109
- return fail('拒绝写入:替换后的内容**完整包含原文件**(new 里很可能把整份原文又带上了)'
110
- + '—— 这会把文档写成两份。请让 new 只描述「被替换那一段替换后长什么样」;'
111
- + '确实要保留整份原文请显式传 allowDuplicate: true');
112
- }
113
- const dup = countOccurrences(out, old);
114
- if (dup > 1) {
115
- return fail(`拒绝写入:替换后,被替换的原文会**出现 ${dup} 次**(new 里很可能把 old 整份又带上了)`
116
- + '—— 这会把文档写重。请把 new 只写成「替换后的样子」;确实需要重复请显式传 allowDuplicate: true');
117
- }
118
- }
119
-
120
- try {
121
- writeFile(path, out, 'utf8');
122
- } catch (e) {
123
- return fail(`写入失败:${(e && e.message) || e}`);
124
- }
125
-
126
- // ── 写后自检 + 自动回滚(只在用默认真实读写时做;注入的读函数按测试约定可能返回定值 ✗)
127
- // 回读校验两条不变量:① 长度与预期一致 ② old 不再出现多次(除非 allowDuplicate)
128
- // 任一不满足 ⇒ 用内存里那份原文**回滚** ✓(原文就在手上,无需备份文件)⇒ 最坏情况也只是「没改成」
129
- if (readFile === readFileSync) {
130
- try {
131
- const back = String(readFile(path, 'utf8'));
132
- const badLen = back.length !== out.length;
133
- const badDup = !allowDuplicate && countOccurrences(back, old) > 1;
134
- if (badLen || badDup) {
135
- writeFile(path, text, 'utf8');
136
- return fail(`写后自检未通过(${badLen ? '长度不符' : ''}${badLen && badDup ? ' + ' : ''}${badDup ? '原文重复出现' : ''}),`
137
- + '已**回滚**为读到的原内容;请检查参数(很可能是 new 里带了整份文档)后重试');
138
- }
139
- } catch { /* 回读失败不阻断:写入已完成,交由调用方核对 */ }
140
- }
141
- return { ok: true, path, replaced: all ? hits : 1, bytesBefore: text.length, bytesAfter: out.length };
142
- }
@@ -1,124 +0,0 @@
1
- /**
2
- * 前端渲染自检(clone 预览弹窗)——把「改前端后必须实际看一眼」变成一条可重复命令。
3
- *
4
- * 为什么需要:宿主 GUI 里看插件页面要刷新/重启,前端报错又只藏在浏览器控制台;
5
- * 而 clone 预览弹窗只在「真实交互走到那一步」时才出现(面板状态由构造函数初始化,
6
- * 外部注入的终态会被覆盖)⇒ 必须**驱动**而不是塞状态。
7
- *
8
- * 依赖(都按环境变量传入,脚本内不写死绝对路径):
9
- * PW_ROOT 含 node_modules/playwright 的目录(createRequire 的基准)
10
- * CHROMIUM 无头浏览器可执行文件(chrome-headless-shell 亦可)
11
- * 运行库/字体由调用方通过 LD_LIBRARY_PATH / FONTCONFIG_FILE 提供(无头 chromium 的既有要求)
12
- *
13
- * 用法(⚠️ 两个实测坑,都踩过):
14
- * node scripts/frontend-preview-check.mjs --html "$PWD/assets/preview.html?mock=1" [--out <截图.png>]
15
- * · --html 必须给**绝对路径**(或自带 file://):相对路径会让 page.goto 直接 ERR_INVALID_URL。
16
- * · 建议带 `?mock=1`:预览页切内置假数据(见 README「离线调试」)。不带时页面 fetch 真后端,
17
- * /repos-cloud 等返回 404 ⇒ 报「仓库列表没有 clone 按钮」这类**假失败**(看着像前端坏了,其实是没数据)。
18
- * 运行前需 PW_ROOT(含 node_modules/playwright 的目录)+ CHROMIUM(无头浏览器),
19
- * 运行库/字体由调用方通过 LD_LIBRARY_PATH / FONTCONFIG_FILE 提供(无头 chromium 的既有要求)。
20
- * 前置(生成预览页;同样由调用方给出工具与 React UMD 路径):
21
- * <dsh>/skills/frontend-real-render-preview.mjs --client lib/client.js --out <预览页.html>
22
- * --react <dsh>/cache/react-umd --endpoints <伪造响应.json>
23
- * 伪造响应至少含三组:/repos-cloud、/browse、/clone-preview(形状见测试用例的断言数据)。
24
- *
25
- * 判据(任一不满足即非零退出):
26
- * ① 云端标签 → ② 加载仓库列表 → ③ 行内 clone → ④ 目录选择器确认 → ⑤ 预览弹窗出现
27
- * ⑥ 弹窗内「带真实历史」勾选框存在、且默认 checked=true(与后端默认一致)
28
- * ⑦ 全程无前端错误(pageerror / console.error)
29
- */
30
- import { createRequire } from 'node:module';
31
-
32
- const argv = process.argv.slice(2);
33
- const arg = (name, def = '') => {
34
- const i = argv.indexOf(name);
35
- return i >= 0 && argv[i + 1] ? argv[i + 1] : def;
36
- };
37
- const HTML = arg('--html');
38
- const OUT = arg('--out', 'frontend-preview-check.png');
39
- // plain = 普通克隆(伪造 /clone-preview 里 partial:false):不应出现「重来」
40
- // partial = 不完整克隆(partial:true):应出现「重来」与 partial 提示
41
- const EXPECT = arg('--expect', 'plain');
42
- const PW_ROOT = process.env.PW_ROOT || process.cwd();
43
- const CHROME = process.env.CHROMIUM || '';
44
- if (!HTML) { console.error('缺少 --html <预览页.html>'); process.exit(2); }
45
- if (!CHROME) { console.error('缺少环境变量 CHROMIUM(无头浏览器可执行文件路径)'); process.exit(2); }
46
-
47
- const require = createRequire(PW_ROOT.replace(/\/?$/, '/'));
48
- const { chromium } = require('playwright');
49
-
50
- const browser = await chromium.launch({
51
- executablePath: CHROME,
52
- // file:// 页面要能 fetch 真后端/伪造端点:与 frontend-real-render-preview 的 --open 同参数
53
- args: ['--no-sandbox', '--disable-gpu', '--disable-web-security', '--allow-file-access-from-files'],
54
- });
55
- const page = await browser.newPage({ viewport: { width: 1320, height: 1000 } });
56
- const errs = [];
57
- page.on('pageerror', (e) => errs.push('pageerror: ' + e.message.slice(0, 120)));
58
- page.on('console', (m) => { if (m.type() === 'error') errs.push('console: ' + m.text().slice(0, 140)); });
59
-
60
- const clickText = async (re) => {
61
- const b = page.locator('button', { hasText: re }).first();
62
- if (await b.count()) { await b.click({ timeout: 4000 }).catch(() => {}); await page.waitForTimeout(2200); return true; }
63
- return false;
64
- };
65
- const fails = [];
66
- const need = (cond, msg) => { if (!cond) fails.push(msg); };
67
-
68
- await page.goto('file://' + HTML, { waitUntil: 'load' });
69
- await page.waitForTimeout(2200);
70
-
71
- need(await clickText(/^云端$/), '未找到「云端」标签');
72
- need(await clickText(/加载仓库列表/), '未找到「加载仓库列表」按钮');
73
- const clones = page.locator('button', { hasText: /^clone$/ });
74
- need(await clones.count() > 0, '仓库列表没有出现 clone 按钮(伪造 /repos-cloud 或 /browse 响应不符)');
75
- if (await clones.count()) {
76
- await clones.first().click().catch(() => {});
77
- await page.waitForTimeout(2600);
78
- if (!(await clickText(/选择当前目录|确定|选择|确认|此目录|开始/))) {
79
- const row = page.locator('text=dshgp-demo').first();
80
- if (await row.count()) { await row.click().catch(() => {}); await page.waitForTimeout(1200); await clickText(/选择当前目录|确定|选择|确认|此目录|开始/); }
81
- else fails.push('目录选择器里既没有确认按钮也没有可点目录');
82
- }
83
- await page.waitForTimeout(2500);
84
- }
85
- const dlg = page.locator('.dshgp_clonepreview');
86
- const hasDlg = await dlg.count();
87
- need(hasDlg > 0, 'clone 预览弹窗未出现(clone-preview 未被触发或响应不符)');
88
- if (hasDlg) {
89
- const box = page.locator('.dshgp_previewopt input[type=checkbox]');
90
- need(await box.count() > 0, '弹窗内没有「带真实历史」勾选框(类名 .dshgp_previewopt 变了?)');
91
- if (await box.count()) {
92
- const checked = await box.first().isChecked();
93
- need(checked === true, `勾选框默认应为 checked=true(与后端默认「带历史」一致),实测 ${checked}`);
94
- // 切换有效性:「取消勾选 ⇒ 真的传 history:false ⇒ 退回整树快照」依赖前端能切换成功,
95
- // 故这里验「点一下真的变 false、再点回 true」;提交体是否带 history:false 由代码审查 + 用例覆盖。
96
- await box.first().click().catch(() => {});
97
- await page.waitForTimeout(600);
98
- const off = await box.first().isChecked();
99
- need(off === false, `点击勾选框应变为未勾选(实测 ${off})——说明 onChange 没接到或状态没重绘`);
100
- await box.first().click().catch(() => {});
101
- await page.waitForTimeout(600);
102
- const back = await box.first().isChecked();
103
- need(back === true, `再次点击应恢复勾选(实测 ${back})`);
104
- }
105
- // 条件渲染对照:「重来」按钮与 partial 提示只在「已存在且不完整」时出现。
106
- // 只验出现态无法区分「只在 partial 时显示」与「总是显示」——故两种期望值都要能断言。
107
- const labels = (await dlg.locator('button').allInnerTexts()).map((t) => t.trim());
108
- const hasRestart = labels.includes('重来');
109
- const hasHint = (await page.locator('.dshgp_previewpartial').count()) > 0;
110
- if (EXPECT === 'partial') {
111
- need(hasRestart, `不完整克隆应出现「重来」按钮(实测按钮 ${JSON.stringify(labels)})`);
112
- need(hasHint, '不完整克隆应显示 partial 提示(.dshgp_previewpartial)');
113
- } else {
114
- need(!hasRestart, `普通克隆不应出现「重来」按钮(实测按钮 ${JSON.stringify(labels)})`);
115
- need(!hasHint, '普通克隆不应显示 partial 提示');
116
- }
117
- }
118
- await page.screenshot({ path: OUT, fullPage: true });
119
- if (errs.length) fails.push('存在前端错误: ' + errs.slice(0, 2).join(' | '));
120
- await browser.close();
121
-
122
- console.log(fails.length ? '❌ 前端渲染自检未通过:\n - ' + fails.join('\n - ') + '\n 截图: ' + OUT
123
- : '✅ 前端渲染自检通过(弹窗出现 + 勾选框存在 + 默认勾选 + 切换有效 + 无前端错误)\n 截图: ' + OUT);
124
- process.exit(fails.length ? 1 : 0);