Skip to content

Commit 94b07fa

Browse files
committed
feat(mo): P1 落地——回执不再只报"认不出",而是列出这一遍干了哪些业务动作
## 背景:自动归属被实测否决 三条实测把原方案一步步逼掉(详见 mneme-research 的 08/09): 1. 单条命令匹配手写步骤表 → 唯一归属仅 14.2%(非发版会话 4.4%,区分力 3 倍) 2. 整段命令当分组键 → 每会话中位 539 组、最多 7373 组,人扫不过来 3. 换动词当键 → 密度最高的是 PowerShell 管道语法(write-output/select-object/} …) → **「命令 ≈ 动作」这个预设是错的** 4. 只留业务命令(git/gh/npm/node/docker…)→ 业务段占 6.5%,动词 8 个、两词组 1010 个 → 这才是人能扫的量级 ## 这一版做什么 不猜。开窗走一遍后,回执列出**本次观察到哪些业务动作、各几次**,由人判断对得上哪一步。 为此新增 pipeline-activities.js(业务命令分桶)并接进 runner / command 回执。 `stop` 在没归属到步骤时**不再只说"没有落盘"**,而是同时给出观察清单—— 清单才是这一版真正的交付物。 ## 🔴 一条硬规则:位置参数一律丢弃 回执会显示在界面上(可能被截图),而完整命令行里几乎必然含绝对路径 (路径含本机用户名)。所以只保留**动词 / 子命令 / 旗标**: `git status --porcelain ×4` 可以,`git add src/foo.js` 不行。 这条不是顺手过滤——它是 pipeline-activities.js 存在的第二个理由。 ## 端到端实测抓出并修掉四个真 bug(都补了回归) 1. 脚本路径被当成子命令 → 回执带出本机路径(踩了上面那条红线) 2. `git -C <path> log` 把 `-C` 的值当子命令 → 清单出现读不懂的 `git (join-path -C …` 3. 带点的文件名(`x.test.js`)通过子命令校验 → 同样是路径泄露 4. 键用旗标时又在展示里追加同一个旗标 → 渲染成 `node --test --test` 修法:子命令做**形状校验**(字符集窄 + 排除含点的),且**扫描**而非取第一个非旗标 token。 实测后清单长这样(真实会话,33 条命令): node --test ×9 / npm test ×5 / git status ×4 / git log ×2 / node --check ×1 / git diff ×1 另有 338 段 shell 语法被正确排除 测试 1973 → 1989。
1 parent 66fdc53 commit 94b07fa

9 files changed

Lines changed: 621 additions & 6 deletions

‎dsh-mneme/lib/lang.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ export const STR = {
272272
nothingObserved: (buffered, notCommands) =>
273273
`这一轮一条都没认出来,**没有落盘**。缓冲到 ${buffered} 条事件,其中 ${notCommands} 条不是命令执行器。\n若你确实走过这条流程,说明实时事件通道没带工具调用——请把这条回执留着。`,
274274
noSteps: "这一轮没有认出的步骤,按「不许编造」的规矩没有落盘。",
275+
activities: (n) => `本次观察到 ${n} 种业务动作(只留动作与旗标,位置参数略去):`,
276+
shellNoise: (n) => `另有 ${n} 段是 shell 语法(赋值、管道、输出),不是工作流的一步。`,
275277
writeFailed: (detail) => `认出来了但写盘失败:${detail}`,
276278
failed: (reason) => `结账失败:${reason}`
277279
},
@@ -286,6 +288,8 @@ export const STR = {
286288
nothingObserved: (buffered, notCommands) =>
287289
`Nothing was recognized this pass; **nothing was persisted**. ${buffered} event(s) were buffered, ${notCommands} of which were not command executors.\nIf you really walked this workflow, the live event channel is not carrying tool calls — please keep this receipt.`,
288290
noSteps: "No step was recognized this pass; per the no-fabrication rule nothing was persisted.",
291+
activities: (n) => `${n} kind(s) of business action observed this pass (actions and flags only; positional arguments omitted):`,
292+
shellNoise: (n) => `Another ${n} segment(s) were shell syntax (assignments, pipes, output), not steps of a workflow.`,
289293
writeFailed: (detail) => `Recognized but failed to write: ${detail}`,
290294
failed: (reason) => `Failed to close: ${reason}`
291295
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/**
2+
* 把 shell 观察压成**人能扫的「业务动作」清单**。
3+
*
4+
* ## 为什么需要这一层(三组实测逼出来的)
5+
*
6+
* 1. 拿单条命令去匹配手写步骤表,命中率只有 14.2~15.2%(非发版会话 4.4%,
7+
* 区分力仅 3 倍)→ **拿清单套行为这条路走不通**。
8+
* 2. 想用「整段命令」当分组键:每个会话中位 **539 组**、最多 7373 组
9+
* → 文献那套「聚类 + 人命名」直接照搬也不成立(网页活动本来只有几十个页面)。
10+
* 3. 换「动词」当键:密度最高的全是 **PowerShell 管道语法**
11+
* (`write-output` 10619、`select-object` 10422、`}` 4864…)
12+
* → **「命令 ≈ 动作」这个预设是错的**:大部分"命令"不是活动,是句子成分。
13+
* 4. 只留业务命令(git/gh/npm/node/docker…)后:业务段占 **6.5%**,
14+
* 不同动词 **8 个**、不同前两词组 **1010 个** —— 这才是人能扫的量级。
15+
*
16+
* ## 这个模块的产出:一份「本次窗口观察到什么」清单
17+
*
18+
* 形态:`git status ×12`、`gh pr ×5`、`npm test ×3`。
19+
* **不猜它对应哪一步**——那件事交给人(见 mneme-research 的 09 号文档)。
20+
*
21+
* ## 🔴 隐私:为什么只输出「动词 + 子命令」
22+
*
23+
* 完整命令行里**几乎必然含绝对路径**,而路径里含本机用户名。例如实测里真实出现过
24+
* `set-location "c:\users\<用户名>\desktop\..."`。这条回执是要显示在界面上、
25+
* 可能被截图的那一面——所以本模块有一条硬规则:
26+
*
27+
* > **位置参数一律丢弃,只保留动词、子命令与 `-` 开头的旗标。**
28+
*
29+
* 旗标是语义(`--rebase` / `--force` / `-m`),位置参数是数据(路径、文件名、id)。
30+
* 只留前者,清单既表意又不泄露。这不是"顺手过滤",是这一层存在的第二个理由。
31+
*/
32+
33+
/**
34+
* 业务命令的动词白名单。
35+
*
36+
* 判据不是"常见",而是"**它的执行会构成工作流的一步**"——即它有语义
37+
* (改仓库状态、发版、跑测试),而不是在组装文本(`write-output`、`foreach-object`)。
38+
*
39+
* 这张名单是**拍的**(实测里 6.5% 的段落在名单内),未验证覆盖率:
40+
* 可能有业务动作用了别的工具(`python`、`curl`、`cargo`)。认不出来不产清单项,
41+
* 是安全的方向——它只会让清单更短,不会让它更假。
42+
*/
43+
const BUSINESS_VERBS = new Set([
44+
// 版本控制与平台
45+
"git", "gh", "glab",
46+
// JS 生态
47+
"npm", "npx", "pnpm", "yarn", "node", "bun", "deno",
48+
// 其它语言/构建(宁可先收着,认不出来也不产假项)
49+
"python", "python3", "pip", "pytest", "cargo", "go", "rustc", "make", "cmake",
50+
// 容器与部署
51+
"docker", "kubectl", "helm",
52+
// 通用(可能出现在流程里)
53+
"curl", "tar", "zip"
54+
]);
55+
56+
/** 命令分隔符:与 pipeline-attribution 同一口径(只认两侧带空白的)。 */
57+
const SEPARATOR_RE = /\s*(?:;|&&|\|\||\|)\s*|\n/u;
58+
59+
/** 重定向尾巴(`2>&1`、`> file`、`2>nul`):不是命令的一部分。 */
60+
const REDIRECT_RE = /\s*\d?>\s*&?\s*\S+/gu;
61+
62+
/** `cd <路径>`:前导定位语句,不是活动。 */
63+
const CD_RE = /^cd\s/iu;
64+
65+
/**
66+
* 这个 token 像不像一个**子命令**。
67+
*
68+
* 必须做这道校验,否则三件事都会发生(都是端到端实测抓到的):
69+
* 1. `node d:\dsh\...\probe.mjs` → 把**脚本路径**当成 sub,路径里的用户名被带出去;
70+
* 2. `git -C $(Join-Path ...) log --oneline` → 把 `$(Join-Path` 当成 sub,清单里出现
71+
* `git (join-path -C --oneline` 这种读不懂的东西;
72+
* 3. `node x.test.js` → **带点的文件名**被当成 sub(`x.test.js` 完全符合"小写字母起头
73+
* 的短词")。这条是加完前两条修复后复验时才冒出来的。
74+
*
75+
* 所以除了字符集,还要**排除含点的 token**:子命令带点是极罕见的
76+
* (`git log` / `gh pr` / `npm test` / `kubectl get` 都不带),而文件名几乎都带。
77+
* 宁可漏认一个,不可把文件名当子命令——后者会出现在给人看的清单里。
78+
*/
79+
function looksLikeSubcommand(token) {
80+
if (typeof token !== "string") return false;
81+
const t = token.replace(/^["']|["']$/gu, "");
82+
if (t === "" || t.length > 20) return false;
83+
if (t.startsWith("-")) return false; // 旗标不是子命令
84+
if (t.includes(".")) return false; // 文件名(x.test.js / release-prep.yml)
85+
if (!/^[a-z0-9][a-z0-9_-]*$/u.test(t)) return false; // 只允许这一小撮字符
86+
return true;
87+
}
88+
89+
/**
90+
* 一条命令 → 一个业务动作。
91+
*
92+
* 返回 `{ verb, sub, flags }` 或 `null`(不是业务命令,或抽不出动词)。
93+
* - `verb`:白名单里的动词(小写)
94+
* - `sub`:**扫描出的第一个像子命令的 token**(不是"第一个非旗标 token"——那个
95+
* 可能是路径或参数,见 `looksLikeSubcommand`)。找不到就是空串
96+
* - `flags`:`-` 开头的旗标。**位置参数一律丢弃**(理由见文件头隐私一节)
97+
*/
98+
export function businessCommandOf(command) {
99+
if (typeof command !== "string") return null;
100+
const cleaned = command.replace(REDIRECT_RE, " ").trim();
101+
if (cleaned === "" || CD_RE.test(cleaned)) return null;
102+
103+
const tokens = cleaned.split(/\s+/u).filter((t) => t !== "");
104+
if (tokens.length === 0) return null;
105+
106+
const verb = tokens[0].toLowerCase().replace(/^["']|["']$/gu, "");
107+
if (!BUSINESS_VERBS.has(verb)) return null;
108+
109+
// **扫描**而不是取第一个非旗标:`git -C <path> log` 的子命令在 `-C <值>` 之后。
110+
let sub = "";
111+
for (let i = 1; i < tokens.length; i += 1) {
112+
if (looksLikeSubcommand(tokens[i])) {
113+
sub = tokens[i].replace(/^["']|["']$/gu, "").toLowerCase();
114+
break;
115+
}
116+
}
117+
118+
// 旗标:只留 `-` 开头的、短的、不含路径分隔符的(`--base=<path>` 这类直接丢)。
119+
const flags = [];
120+
for (const raw of tokens.slice(1)) {
121+
const t = raw.replace(/^["']|["']$/gu, "");
122+
if (t.startsWith("-") && t.length > 1 && t.length <= 24 && !/[/\\=]/u.test(t)) flags.push(t);
123+
}
124+
125+
return { verb, sub, flags: [...new Set(flags)].slice(0, 4) };
126+
}
127+
128+
/**
129+
* 分组的键。
130+
*
131+
* 优先用子命令(`git status`)。**没有子命令时退而用第一个旗标**——因为对
132+
* `node` / `npm` 这类工具,真正区分语义的常常就是旗标:`node --test`(跑测试)与
133+
* `node --check`(查语法)是两件事,若键里只有 `node`,它们会被合并成一行,
134+
* 清单就读不出差别了。这条是端到端实测里发现的(16 次 node 调用被并成一条)。
135+
*
136+
* **旗标仍然只是"补充",不是主体**:`git status -C --porcelain` 的键是 `git status`,
137+
* `-C`(它后面本来跟着一个被丢弃的路径)不该挤进键里。
138+
*/
139+
export function activityKey(activity) {
140+
if (!activity) return "";
141+
const tail = activity.sub || (Array.isArray(activity.flags) ? activity.flags[0] : "") || "";
142+
return tail ? `${activity.verb} ${tail}` : activity.verb;
143+
}
144+
145+
/**
146+
* 一批观察 → 业务动作清单(按出现次数降序)。
147+
*
148+
* 每条:`{ key, verb, sub, count, flags }`;`flags` 是出现过的旗标并集(截断)。
149+
* **不含任何位置参数**,因此不含路径与用户名。
150+
*/
151+
export function tallyActivities(observations) {
152+
const groups = new Map();
153+
let shellNoise = 0;
154+
let totalSegments = 0;
155+
156+
for (const observation of observations || []) {
157+
const command = observation?.command;
158+
if (typeof command !== "string" || command.trim() === "") continue;
159+
for (const raw of command.split(SEPARATOR_RE)) {
160+
if (raw.trim() === "") continue;
161+
totalSegments += 1;
162+
const activity = businessCommandOf(raw);
163+
if (!activity) {
164+
shellNoise += 1;
165+
continue;
166+
}
167+
const key = activityKey(activity);
168+
const cur = groups.get(key) || { key, verb: activity.verb, sub: activity.sub, count: 0, flags: new Set() };
169+
cur.count += 1;
170+
for (const f of activity.flags) cur.flags.add(f);
171+
groups.set(key, cur);
172+
}
173+
}
174+
175+
const activities = [...groups.values()]
176+
.map((g) => ({ key: g.key, verb: g.verb, sub: g.sub, count: g.count, flags: [...g.flags].slice(0, 6) }))
177+
.sort((a, b) => (b.count === a.count ? a.key.localeCompare(b.key) : b.count - a.count));
178+
179+
return {
180+
activities,
181+
// 两个数分开报:「业务动作很少」与「这条命令根本不是动作」是两件事。
182+
businessSegments: activities.reduce((n, a) => n + a.count, 0),
183+
shellNoise,
184+
totalSegments
185+
};
186+
}

‎dsh-mneme/lib/pipeline-command.js‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,38 @@ export function parseMoRunInput(rawInput) {
4747
}
4848
}
4949

50+
/**
51+
* 把「本次观察到哪些业务动作」渲染成几行。
52+
*
53+
* 这是这一版回执的**主菜**:自动归属的命中率实测只有 14%,所以回执不再声称
54+
* "我替你对上了哪几步",而是如实列出"这一遍你干了什么",由人决定它对不对得上。
55+
*
56+
* 只显示 `动词 子命令 ×次数`(可带旗标)——**位置参数在分桶层就被丢弃了**,
57+
* 因为完整命令行含绝对路径(路径含本机用户名),而这条回执会显示在界面上。
58+
* 那也是 pipeline-activities.js 存在的第二个理由。
59+
*/
60+
function renderActivities(activities, t, maxRows = 12) {
61+
if (!Array.isArray(activities) || activities.length === 0) return "";
62+
const rows = activities.slice(0, maxRows).map((a) => {
63+
// 键在没有子命令时用的是第一个旗标(`node --test`)——那种情况下别再把同一个旗标
64+
// 追加一遍,否则会渲染成 `node --test --test`(端到端复验时抓到的重复)。
65+
const shown = (a.flags || []).filter((f) => !a.key.endsWith(f));
66+
const flags = shown.length ? ` ${shown.join(" ")}` : "";
67+
return ` ${a.key}${flags} ×${a.count}`;
68+
});
69+
const more = activities.length > maxRows ? `\n …另有 ${activities.length - maxRows} 种` : "";
70+
return `${t.activities(activities.length)}\n${rows.join("\n")}${more}`;
71+
}
72+
5073
/** 把一次 `stop()` 的结果翻成人话。**失败要说清为什么**,不许含糊成「完成」。 */
5174
function describeStop(result, language) {
5275
const t = STR.moRunStop[language];
76+
// 观察清单无论成败都要给——它是这个功能当前最可靠的产出。
77+
const activityBlock = renderActivities(result.activities, t);
78+
5379
if (result.ok) {
5480
const parts = [t.ok(result.pipelineId, result.steps)];
81+
if (activityBlock) parts.push(activityBlock);
5582
if (result.unobserved?.length) parts.push(t.unobserved(result.unobserved.length, result.unobserved.join("、")));
5683
if (result.unattributed) parts.push(t.unattributed(result.unattributed));
5784
if (result.ambiguous) parts.push(t.ambiguous(result.ambiguous));
@@ -65,8 +92,13 @@ function describeStop(result, language) {
6592
case "no-observations":
6693
// 这条最要紧:一条观察都没认出来,绝不写成「完成」。
6794
return t.nothingObserved(result.bufferedEvents, result.notCommands);
68-
case "no-steps":
69-
return t.noSteps;
95+
case "no-steps": {
96+
// 「没归属到步骤」不等于「什么都没干」——清单才是这一版真正的交付物。
97+
const parts = [t.noSteps];
98+
if (activityBlock) parts.push(activityBlock);
99+
if (result.shellNoise) parts.push(t.shellNoise(result.shellNoise));
100+
return parts.filter(Boolean).join("\n");
101+
}
70102
case "write-failed":
71103
return t.writeFailed(result.detail);
72104
default:

‎dsh-mneme/lib/pipeline-runner.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { loadPipelines, pipelineSources, projectPipelineDir } from "./pipeline.j
4040
import { appendRun } from "./pipeline-runs.js";
4141
import { openRun } from "./pipeline-exec.js";
4242
import { pairObservations } from "./pipeline-observe.js";
43+
import { tallyActivities } from "./pipeline-activities.js";
4344

4445
/** 一个窗口最多缓冲多少条会话事件。忘了 stop 时,这是内存的兜底。 */
4546
export const MAX_BUFFERED_EVENTS = 4000;
@@ -161,6 +162,11 @@ export function createExecutionRecorder({
161162
for (const observation of observations) w.run.observe(observation);
162163
const closed = w.run.close();
163164

165+
// 本次窗口**观察到哪些业务动作**——与「能不能归属到步骤」无关。
166+
// 这是这一版的重点:自动归属命中率实测只有 14%,所以回执的主要价值
167+
// 不再是"我替你对上了哪几步",而是"我如实告诉你这一遍都干了什么"。
168+
const activityTally = tallyActivities(observations);
169+
164170
const common = {
165171
pipelineId: w.pipeline.id,
166172
bufferedEvents: w.events.length,
@@ -170,7 +176,9 @@ export function createExecutionRecorder({
170176
dangling: dangling.length,
171177
unattributed: closed.unattributed.length,
172178
ambiguous: closed.ambiguous.length,
173-
unobserved: closed.unobserved
179+
unobserved: closed.unobserved,
180+
activities: activityTally.activities,
181+
shellNoise: activityTally.shellNoise
174182
};
175183

176184
// 一条观察都没认出来:如实说,不写空账。这同时是「实时通道到底带不带工具事件」

‎dsh-mneme/src/lang.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ export const STR = {
272272
nothingObserved: (buffered, notCommands) =>
273273
`这一轮一条都没认出来,**没有落盘**。缓冲到 ${buffered} 条事件,其中 ${notCommands} 条不是命令执行器。\n若你确实走过这条流程,说明实时事件通道没带工具调用——请把这条回执留着。`,
274274
noSteps: "这一轮没有认出的步骤,按「不许编造」的规矩没有落盘。",
275+
activities: (n) => `本次观察到 ${n} 种业务动作(只留动作与旗标,位置参数略去):`,
276+
shellNoise: (n) => `另有 ${n} 段是 shell 语法(赋值、管道、输出),不是工作流的一步。`,
275277
writeFailed: (detail) => `认出来了但写盘失败:${detail}`,
276278
failed: (reason) => `结账失败:${reason}`
277279
},
@@ -286,6 +288,8 @@ export const STR = {
286288
nothingObserved: (buffered, notCommands) =>
287289
`Nothing was recognized this pass; **nothing was persisted**. ${buffered} event(s) were buffered, ${notCommands} of which were not command executors.\nIf you really walked this workflow, the live event channel is not carrying tool calls — please keep this receipt.`,
288290
noSteps: "No step was recognized this pass; per the no-fabrication rule nothing was persisted.",
291+
activities: (n) => `${n} kind(s) of business action observed this pass (actions and flags only; positional arguments omitted):`,
292+
shellNoise: (n) => `Another ${n} segment(s) were shell syntax (assignments, pipes, output), not steps of a workflow.`,
289293
writeFailed: (detail) => `Recognized but failed to write: ${detail}`,
290294
failed: (reason) => `Failed to close: ${reason}`
291295
}

0 commit comments

Comments
 (0)