@modusensus/dsh-mneme 0.8.16 → 0.9.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.
Files changed (135) hide show
  1. package/README.md +91 -16
  2. package/assets/icon.svg +2 -2
  3. package/bin/cli.mjs +8 -2
  4. package/docs/CONFIGURATION.md +27 -1
  5. package/docs/CONTINUITY.md +18 -3
  6. package/docs/SEMANTIC.md +3 -3
  7. package/docs/WORKFLOWS.md +299 -0
  8. package/lib/api-standalone.js +78 -12
  9. package/lib/api.js +403 -62
  10. package/lib/candidates.js +454 -0
  11. package/lib/capabilities.js +819 -0
  12. package/lib/client.js +2294 -423
  13. package/lib/config.js +63 -6
  14. package/lib/continuity.js +35 -6
  15. package/lib/dream/decisions.js +17 -1
  16. package/lib/dream/sleep.js +10 -2
  17. package/lib/dream.js +5 -1
  18. package/lib/embedding.js +116 -15
  19. package/lib/entities/extractor.js +4 -0
  20. package/lib/index.js +81 -12
  21. package/lib/inject.js +40 -4
  22. package/lib/lang.js +124 -0
  23. package/lib/pipeline-activities.js +178 -0
  24. package/lib/pipeline-attribution.js +165 -0
  25. package/lib/pipeline-command.js +279 -0
  26. package/lib/pipeline-confirm.js +142 -0
  27. package/lib/pipeline-exec.js +174 -0
  28. package/lib/pipeline-nudge.js +1281 -0
  29. package/lib/pipeline-observations.js +181 -0
  30. package/lib/pipeline-observe.js +133 -0
  31. package/lib/pipeline-runner.js +346 -0
  32. package/lib/pipeline-runs.js +442 -0
  33. package/lib/pipeline.js +655 -0
  34. package/lib/scope.js +16 -4
  35. package/lib/semantic.js +66 -10
  36. package/lib/serve.js +1 -1
  37. package/lib/service.js +59 -18
  38. package/lib/session-usage.js +350 -0
  39. package/lib/settings.js +138 -1
  40. package/lib/store.js +132 -6
  41. package/lib/summarize.js +30 -0
  42. package/lib/tools.js +11 -2
  43. package/package.json +3 -3
  44. package/scripts/changelog-prep.mjs +62 -7
  45. package/scripts/check-sync.js +8 -6
  46. package/scripts/publish-gate.mjs +197 -0
  47. package/scripts/release-docs.mjs +204 -0
  48. package/scripts/release-notes.mjs +162 -0
  49. package/src/api-standalone.js +78 -12
  50. package/src/api.js +403 -62
  51. package/src/candidates.js +454 -0
  52. package/src/capabilities.js +819 -0
  53. package/src/config.js +63 -6
  54. package/src/continuity.js +35 -6
  55. package/src/dream/decisions.js +17 -1
  56. package/src/dream/sleep.js +10 -2
  57. package/src/dream.js +5 -1
  58. package/src/embedding.js +116 -15
  59. package/src/entities/extractor.js +4 -0
  60. package/src/index.js +81 -12
  61. package/src/inject.js +40 -4
  62. package/src/lang.js +124 -0
  63. package/src/pipeline-activities.js +178 -0
  64. package/src/pipeline-attribution.js +165 -0
  65. package/src/pipeline-command.js +279 -0
  66. package/src/pipeline-confirm.js +142 -0
  67. package/src/pipeline-exec.js +174 -0
  68. package/src/pipeline-nudge.js +1281 -0
  69. package/src/pipeline-observations.js +181 -0
  70. package/src/pipeline-observe.js +133 -0
  71. package/src/pipeline-runner.js +346 -0
  72. package/src/pipeline-runs.js +442 -0
  73. package/src/pipeline.js +655 -0
  74. package/src/scope.js +16 -4
  75. package/src/semantic.js +66 -10
  76. package/src/serve.js +1 -1
  77. package/src/service.js +59 -18
  78. package/src/session-usage.js +350 -0
  79. package/src/settings.js +138 -1
  80. package/src/store.js +132 -6
  81. package/src/summarize.js +30 -0
  82. package/src/tools.js +11 -2
  83. package/test/api-hardening.test.js +173 -0
  84. package/test/api.test.js +244 -7
  85. package/test/candidates.test.js +345 -0
  86. package/test/capabilities.test.js +577 -0
  87. package/test/changelog-prep.test.js +74 -8
  88. package/test/cli-enums.test.js +37 -0
  89. package/test/client.test.js +510 -2
  90. package/test/continuity.test.js +84 -0
  91. package/test/docs-config-keys.test.js +65 -0
  92. package/test/embed-ready-poll.test.js +94 -0
  93. package/test/entities.test.js +13 -2
  94. package/test/failure-query.test.js +147 -0
  95. package/test/failure-retention.test.js +105 -0
  96. package/test/graph-api.test.js +36 -2
  97. package/test/helpers/write-admission-samples.js +6 -2
  98. package/test/inject-recall.test.js +32 -4
  99. package/test/lib-smoke.test.js +20 -0
  100. package/test/llm-audit.test.js +92 -1
  101. package/test/mint-source.test.js +202 -0
  102. package/test/persona-api.test.js +172 -0
  103. package/test/persona-flag.test.js +36 -0
  104. package/test/persona-inject.test.js +145 -0
  105. package/test/persona-settings.test.js +145 -0
  106. package/test/pipeline-activities.test.js +155 -0
  107. package/test/pipeline-attribution.test.js +175 -0
  108. package/test/pipeline-command.test.js +243 -0
  109. package/test/pipeline-confirm.test.js +148 -0
  110. package/test/pipeline-exec.test.js +172 -0
  111. package/test/pipeline-nudge.test.js +1036 -0
  112. package/test/pipeline-observations.test.js +157 -0
  113. package/test/pipeline-observe.test.js +127 -0
  114. package/test/pipeline-r1r2.test.js +149 -0
  115. package/test/pipeline-runner.test.js +442 -0
  116. package/test/pipeline-runs.test.js +767 -0
  117. package/test/pipeline.test.js +636 -0
  118. package/test/placement.test.js +1 -1
  119. package/test/publish-gate.test.js +272 -0
  120. package/test/recall-retention.test.js +128 -0
  121. package/test/reindex-backfill.test.js +83 -4
  122. package/test/release-docs.test.js +130 -0
  123. package/test/release-notes.test.js +240 -0
  124. package/test/release-workflow.test.js +93 -0
  125. package/test/route-policy.test.js +113 -0
  126. package/test/scope-distill-stamp.test.js +135 -0
  127. package/test/security-doc.test.js +139 -0
  128. package/test/sensitive-scan.test.js +44 -0
  129. package/test/serve.test.js +1 -1
  130. package/test/session-usage.test.js +308 -0
  131. package/test/store-write-path-columns.test.js +191 -0
  132. package/test/summarize.test.js +176 -0
  133. package/test/supersede-state-tracking.test.js +4 -4
  134. package/test/tools.test.js +16 -1
  135. package/test/workflow-guards.test.js +156 -0
package/src/inject.js CHANGED
@@ -38,7 +38,12 @@ function visibleSeqsOf(session) {
38
38
  // session event log (same event shape summarize.js consumes) and returns the
39
39
  // most recent human message. Any failure degrades to "" — the injector then
40
40
  // falls back to the legacy rule-based pick, never breaking the render.
41
- function lastUserQuery(ctx) {
41
+ //
42
+ // v0.9.0:导出给 tools.js 的 memory_update 用。失败记录表的 query 与注入
43
+ // 候选问的是同一件事——「当时到底想干什么」;这条取原话的路只该有一份实现。
44
+ // 复制一份的代价本仓库付过几次(src/lib 平价、snapshotEvents 兼容层),所以
45
+ // 这里只加 export,不改行为。
46
+ export function lastUserQuery(ctx) {
42
47
  try {
43
48
  const session = ctx?.agent?.session;
44
49
  // DSH ≥0.1.2-rc only exposes events via snapshotEvents(); older builds
@@ -301,13 +306,44 @@ export function createInjector(ctx, service, settings, config, gate = null) {
301
306
  }).catch(() => { /* prefetch is best-effort */ });
302
307
  }
303
308
 
309
+ // 会话身份解析(v1.2 人格寻址用):读 session.header.agentPreset——冻结的
310
+ // SessionHeader(issue #17 身份契约)。⚠️ 不能先读 requestHeader():那返回
311
+ // EpochHeader(请求级头,首请求后为真),会把 session.header 遮蔽掉,身份取空
312
+ // (scope.js 同坑,本地实测踩过)。任何失败静默 → null(落 default 卡)。
313
+ function sessionAgentId(ctx) {
314
+ try {
315
+ const session = ctx?.agent?.session;
316
+ const header = session?.header ?? null;
317
+ const preset = header?.agentPreset;
318
+ return typeof preset === "string" && preset.trim() ? preset.trim() : null;
319
+ } catch {
320
+ return null;
321
+ }
322
+ }
323
+
304
324
  // User profile + rules: injected ahead of the memory block because they are
305
325
  // always-relevant instructions the agent should follow every turn.
306
- function renderUserSettings() {
326
+ // 人格段(beta):双闸门(config.personaEnabled && 卡.enabled)开时渲染在最前
327
+ // ——设计稿⑥:特化指令(人格)先出,画像与规则后出、后写覆盖占上风,rules 恒
328
+ // 最硬。v1.2 多卡:按会话身份取 byAgent 卡,身份缺失/无卡/卡关落 default。
329
+ // 任一闸关着时本函数输出与历史版本逐字节一致(老用户零变化)。人格段非空即
330
+ // 触发整块渲染(与 profile 同权)。
331
+ function renderUserSettings(ctx) {
307
332
  const profile = settings.getProfile().trim();
308
333
  const rules = settings.getRules();
309
- if (!profile && !rules.length) return "";
334
+ const persona = config?.personaEnabled === true
335
+ ? settings.getPersonaFor(sessionAgentId(ctx))
336
+ : {};
337
+ const personaLines = persona?.enabled === true
338
+ ? [
339
+ ...(typeof persona.name === "string" && persona.name.trim() ? [STR.personaNameLine[language](persona.name.trim())] : []),
340
+ ...(typeof persona.personality === "string" && persona.personality.trim() ? [STR.personaPersonalityLine[language](persona.personality.trim())] : []),
341
+ ...(typeof persona.replyStyle === "string" && persona.replyStyle.trim() ? [STR.personaReplyStyleLine[language](persona.replyStyle.trim())] : [])
342
+ ]
343
+ : [];
344
+ if (!profile && !rules.length && !personaLines.length) return "";
310
345
  const lines = [STR.userSettingsHeader[language]];
346
+ lines.push(...personaLines);
311
347
  if (profile) lines.push(STR.profileLine[language](profile));
312
348
  for (const rule of rules) lines.push(STR.ruleLine[language](rule));
313
349
  return escapePromptVars(lines.join("\n"));
@@ -413,7 +449,7 @@ export function createInjector(ctx, service, settings, config, gate = null) {
413
449
  ctx.systemPrompt.context({
414
450
  name: "user-settings",
415
451
  order: 85,
416
- text: renderUserSettings
452
+ text: (ctx) => renderUserSettings(ctx)
417
453
  })
418
454
  ];
419
455
 
package/src/lang.js CHANGED
@@ -122,6 +122,20 @@ export const STR = {
122
122
  zh: (rule) => `- 规则:${rule}`,
123
123
  en: (rule) => `- Rule: ${rule}`
124
124
  },
125
+ // 人格定义页(beta):人格段行(renderUserSettings 拼装,双闸门开时渲染)。
126
+ // 设计稿⑥:人格段在 profile/rules 之前——特化指令后出、通用底座压轴,rules 恒最硬。
127
+ personaNameLine: {
128
+ zh: (name) => `- 你的人格名字:${name}`,
129
+ en: (name) => `- Your persona name: ${name}`
130
+ },
131
+ personaPersonalityLine: {
132
+ zh: (text) => `- 人格性格设定:${text}`,
133
+ en: (text) => `- Persona personality: ${text}`
134
+ },
135
+ personaReplyStyleLine: {
136
+ zh: (text) => `- 回复要求:${text}`,
137
+ en: (text) => `- Reply style: ${text}`
138
+ },
125
139
 
126
140
  // --- summarize.js:蒸馏转录标签(进入 LLM 上下文) --------------------------
127
141
  transcriptUser: { zh: (t) => `用户:${t}`, en: (t) => `User: ${t}` },
@@ -223,6 +237,116 @@ export const STR = {
223
237
  en: (name) => `Failed to submit command ${name}; see logs`
224
238
  },
225
239
 
240
+ // --- pipeline-command.js:/mo-run 的开窗命令 -----------------------------------
241
+ // 回执是这个功能唯一的对外窗口(面板侧还没做),所以每一条都要说清「发生了什么、
242
+ // 为什么」——尤其失败时不许含糊成「完成」。
243
+ moRunDescription: {
244
+ zh: "M.O. 记账:显式开一次执行窗口,之后跑过的命令才可能被记下来",
245
+ en: "M.O. ledger: explicitly open an execution window; only commands run inside it can be recorded"
246
+ },
247
+ moRunInputHint: {
248
+ zh: "start <工作流 id> | stop | confirm <做了的步骤> | status",
249
+ en: "start <pipeline id> | stop | confirm <steps that ran> | status"
250
+ },
251
+ moRunUsage: {
252
+ zh: "用法:/mo-run start <工作流 id> → 走一遍 → /mo-run stop → /mo-run confirm <做了的步骤 id>\n\n停止时会给出一份待确认清单(含本次观察到的业务动作);**确认之后才写账**。\n确认写法:`/mo-run confirm P6 V2` 表示这两步做了;`V2:-` 表示没做(记 skipped),`V2:?` 表示不确定(不写)。\n未提到的步骤什么都不写——「没确认」不等于「没发生」。\n\n它不会自己判断「你是不是在走某条流程」,也不会自己判断「哪几步走了」:这两件事都会猜错。",
253
+ en: "Usage: /mo-run start <pipeline id> → walk through it → /mo-run stop → /mo-run confirm <step ids that ran>\n\nStop gives you a checklist (with the business actions observed); **the ledger is only written after you confirm**.\nConfirm syntax: `/mo-run confirm P6 V2` means both ran; `V2:-` means not done (recorded as skipped), `V2:?` means unsure (not written).\nSteps you do not mention are written as nothing — unconfirmed is not the same as not-happened.\n\nIt never guesses whether you are walking a workflow, nor which steps ran: both guesses would be wrong."
254
+ },
255
+ moRunStart: {
256
+ zh: {
257
+ ok: (id, steps) => `已开窗:${id}(${steps} 步)。走完用 /mo-run stop 结账——它会给你一份待确认清单,**确认之后才写账**。`,
258
+ alreadyOpen: (id) => `这个会话已经开着窗口了:${id}。先用 /mo-run stop 结账,再开新的。`,
259
+ notFound: (ids) => `找不到这条工作流。可用:${ids}`,
260
+ noneAvailable: "(当前项目里没有读到任何工作流)",
261
+ unreadable: (detail) => `工作流读不出来:${detail}`,
262
+ noProject: "拿不到这个会话的工作目录,无法定位工作流。",
263
+ noSession: "拿不到会话身份,无法记账。",
264
+ failed: (reason) => `开窗失败:${reason}`
265
+ },
266
+ en: {
267
+ ok: (id, steps) => `Window opened: ${id} (${steps} steps). When done, run /mo-run stop — it gives you a checklist, and **the ledger is only written after you confirm**.`,
268
+ alreadyOpen: (id) => `This session already has an open window: ${id}. Run /mo-run stop first.`,
269
+ notFound: (ids) => `No such workflow. Available: ${ids}`,
270
+ noneAvailable: "(no workflow found in this project)",
271
+ unreadable: (detail) => `Workflow unreadable: ${detail}`,
272
+ noProject: "Cannot determine this session's working directory, so the workflow cannot be located.",
273
+ noSession: "No session identity available; nothing can be recorded.",
274
+ failed: (reason) => `Failed to open window: ${reason}`
275
+ }
276
+ },
277
+ moRunStop: {
278
+ zh: {
279
+ ok: (id, steps) => `已结账并落盘:${id},记下 ${steps} 步。`,
280
+ awaiting: (id, steps) => `已结账:${id}(${steps} 步)。**还没有写账**——请确认哪几步真的走了:`,
281
+ confirmHow: (sample) =>
282
+ `\n用 /mo-run confirm ${sample} 列出「做了」的步骤;要标「没做」就写 ${String(sample).split(" ")[0]}:-\n未提到的步骤什么都不写(「没确认」不等于「没发生」)。`,
283
+ notCommands: (n) => `${n} 次调用不是命令执行器(读文件、检索这类),与工作流步骤无关。`,
284
+ truncated: "⚠️ 事件缓冲到上限被截断,这次记账可能不完整。",
285
+ noWindow: "这个会话没有开着窗口,没什么可结账的。",
286
+ activities: (n) => `本次观察到 ${n} 种业务动作(只留动作与旗标,位置参数略去):`,
287
+ shellNoise: (n) => `另有 ${n} 段是 shell 语法(赋值、管道、输出),不是工作流的一步。`,
288
+ writeFailed: (detail) => `写盘失败:${detail}`,
289
+ failed: (reason) => `结账失败:${reason}`
290
+ },
291
+ en: {
292
+ ok: (id, steps) => `Closed and persisted: ${id}, ${steps} step(s) recorded.`,
293
+ awaiting: (id, steps) => `Closed: ${id} (${steps} steps). **Nothing persisted yet** — confirm which steps really ran:`,
294
+ confirmHow: (sample) =>
295
+ `\nUse /mo-run confirm ${sample} to list the steps that ran; mark one as not-done with ${String(sample).split(" ")[0]}:-\nSteps you do not mention are written as nothing (unconfirmed is not the same as not-happened).`,
296
+ notCommands: (n) => `${n} call(s) were not command executors (file reads, searches) and are irrelevant to workflow steps.`,
297
+ truncated: "⚠️ Event buffer hit its cap and was truncated; this record may be incomplete.",
298
+ noWindow: "No window is open for this session, so there is nothing to close.",
299
+ activities: (n) => `${n} kind(s) of business action observed this pass (actions and flags only; positional arguments omitted):`,
300
+ shellNoise: (n) => `Another ${n} segment(s) were shell syntax (assignments, pipes, output), not steps of a workflow.`,
301
+ writeFailed: (detail) => `Failed to write: ${detail}`,
302
+ failed: (reason) => `Failed to close: ${reason}`
303
+ }
304
+ },
305
+ moRunConfirm: {
306
+ zh: {
307
+ ok: (id, steps) => `已记入账本:${id},${steps} 步。`,
308
+ notDone: (list) => `记为「这次没做」(skipped):${list}`,
309
+ unsure: (list) => `你说不确定,没有写进账:${list}`,
310
+ unanswered: (list) => `没提到的步骤没有写进账:${list}`,
311
+ nothingToConfirm: "没有待确认的清单。先 /mo-run start <工作流 id>,走一遍,再 /mo-run stop。",
312
+ mismatch: (n, rows) => `⚠️ 有 ${n} 步你说做了、但声明的命令这次一次都没出现(是声明该改、还是本来不必敲?由你判):\n${rows}`,
313
+ obsSaved: (file) => `本次观察已旁挂:${file}`, noAnswers: (notDone, unsure, unanswered) =>
314
+ `一条都没确认,**没有落盘**。你说「没做」的 ${notDone} 步、不确定的 ${unsure} 步、没提到的 ${unanswered} 步。\n确认「做了」的步骤才会写账:/mo-run confirm <步骤 id>`,
315
+ badValue: (stepId, value) => `${stepId} 的回答「${value}」不合法。只认「做了」(或不写标记)/「没做」写作 -/「不确定」写作 ?`,
316
+ unknownStep: (stepId) => `没有这一步:${stepId}`,
317
+ writeFailed: (detail) => `写盘失败:${detail}`,
318
+ failed: (reason) => `确认失败:${reason}`
319
+ },
320
+ en: {
321
+ ok: (id, steps) => `Recorded: ${id}, ${steps} step(s).`,
322
+ notDone: (list) => `Marked as not done this pass (skipped): ${list}`,
323
+ unsure: (list) => `You were unsure; not written to the ledger: ${list}`,
324
+ unanswered: (list) => `Steps you did not mention were not written to the ledger: ${list}`,
325
+ nothingToConfirm: "Nothing to confirm. Run /mo-run start <pipeline id>, walk through it, then /mo-run stop.",
326
+ mismatch: (n, rows) => `⚠️ ${n} step(s) you marked done never showed their declared command this pass (stale declaration, or simply not needed? your call):\n${rows}`,
327
+ obsSaved: (file) => `Observations side-written to: ${file}`, noAnswers: (notDone, unsure, unanswered) =>
328
+ `Nothing was confirmed; **nothing was persisted**. Not-done: ${notDone}, unsure: ${unsure}, unmentioned: ${unanswered}.\nOnly steps confirmed as done are recorded: /mo-run confirm <step id>`,
329
+ badValue: (stepId, value) => `Answer "${value}" for ${stepId} is invalid. Use done (no marker) / - for not-done / ? for unsure.`,
330
+ unknownStep: (stepId) => `No such step: ${stepId}`,
331
+ writeFailed: (detail) => `Failed to write: ${detail}`,
332
+ failed: (reason) => `Failed to confirm: ${reason}`
333
+ }
334
+ },
335
+ moRunStatus: {
336
+ zh: {
337
+ closed: "当前没有开着的执行窗口。用 /mo-run start <工作流 id> 开一个。",
338
+ awaiting: (id, steps) => `等你在确认:${id}(${steps} 步)。用 /mo-run confirm <做了的步骤 id> 写账。`,
339
+ open: (s, recorded) => `窗口开着:${s.pipelineId}\n开始于 ${s.startedAt}\n已缓冲 ${s.bufferedEvents} 条事件,识别出 ${recorded} 步${s.truncated ? "(缓冲已截断)" : ""}\n认不出 ${s.unattributed},待确认 ${s.ambiguous}`,
340
+ none: "(还没有)"
341
+ },
342
+ en: {
343
+ closed: "No execution window is open. Open one with /mo-run start <pipeline id>.",
344
+ awaiting: (id, steps) => `Waiting for your confirmation: ${id} (${steps} steps). Write the ledger with /mo-run confirm <step ids that ran>.`,
345
+ open: (s, recorded) => `Window open: ${s.pipelineId}\nStarted ${s.startedAt}\n${s.bufferedEvents} event(s) buffered, ${recorded} step(s) recognized${s.truncated ? " (buffer truncated)" : ""}\nUnattributed ${s.unattributed}, ambiguous ${s.ambiguous}`,
346
+ none: "(none yet)"
347
+ }
348
+ },
349
+
226
350
  // --- mirror.js:镜像文件标签(渲染随实例语言;解析两种语言都认) ----------------
227
351
  mirrorLabel: {
228
352
  zh: { type: "类型", importance: "重要性", tags: "标签", updated: "更新时间", source: "来源", scope: "作用域", sensitivity: "敏感度" },
@@ -0,0 +1,178 @@
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
+ // 实测 21723 次调用里,真正命中的只有这四个:git / gh / npm / node。
45
+ // 其余先不收——认不出来只会让清单更短,不会让它更假;
46
+ // 等真有项目用上了再加(这一版刻意不做"猜未来会用到"的预留)。
47
+ "git", "gh", "npm", "npx", "node", "docker"
48
+ ]);
49
+
50
+ /** 命令分隔符:与 pipeline-attribution 同一口径(只认两侧带空白的)。 */
51
+ const SEPARATOR_RE = /\s*(?:;|&&|\|\||\|)\s*|\n/u;
52
+
53
+ /** 重定向尾巴(`2>&1`、`> file`、`2>nul`):不是命令的一部分。 */
54
+ const REDIRECT_RE = /\s*\d?>\s*&?\s*\S+/gu;
55
+
56
+ /** `cd <路径>`:前导定位语句,不是活动。 */
57
+ const CD_RE = /^cd\s/iu;
58
+
59
+ /**
60
+ * 这个 token 像不像一个**子命令**。
61
+ *
62
+ * 必须做这道校验,否则三件事都会发生(都是端到端实测抓到的):
63
+ * 1. `node d:\dsh\...\probe.mjs` → 把**脚本路径**当成 sub,路径里的用户名被带出去;
64
+ * 2. `git -C $(Join-Path ...) log --oneline` → 把 `$(Join-Path` 当成 sub,清单里出现
65
+ * `git (join-path -C --oneline` 这种读不懂的东西;
66
+ * 3. `node x.test.js` → **带点的文件名**被当成 sub(`x.test.js` 完全符合"小写字母起头
67
+ * 的短词")。这条是加完前两条修复后复验时才冒出来的。
68
+ *
69
+ * 所以除了字符集,还要**排除含点的 token**:子命令带点是极罕见的
70
+ * (`git log` / `gh pr` / `npm test` / `kubectl get` 都不带),而文件名几乎都带。
71
+ * 宁可漏认一个,不可把文件名当子命令——后者会出现在给人看的清单里。
72
+ */
73
+ function looksLikeSubcommand(token) {
74
+ if (typeof token !== "string") return false;
75
+ const t = token.replace(/^["']|["']$/gu, "");
76
+ // 一个正则同时挡住四类坏值:旗标(`-` 开头)、含点的文件名、`$(…)` 这类
77
+ // shell 片段、以及超长的东西。上一版写成五道 if,等价但更啰嗦。
78
+ return /^[a-z0-9][a-z0-9_-]{0,19}$/u.test(t);
79
+ }
80
+
81
+ /**
82
+ * 一条命令 → 一个业务动作。
83
+ *
84
+ * 返回 `{ verb, sub, flags }` 或 `null`(不是业务命令,或抽不出动词)。
85
+ * - `verb`:白名单里的动词(小写)
86
+ * - `sub`:**扫描出的第一个像子命令的 token**(不是"第一个非旗标 token"——那个
87
+ * 可能是路径或参数,见 `looksLikeSubcommand`)。找不到就是空串
88
+ * - `flags`:`-` 开头的旗标。**位置参数一律丢弃**(理由见文件头隐私一节)
89
+ */
90
+ export function businessCommandOf(command) {
91
+ if (typeof command !== "string") return null;
92
+ const cleaned = command.replace(REDIRECT_RE, " ").trim();
93
+ if (cleaned === "" || CD_RE.test(cleaned)) return null;
94
+
95
+ const tokens = cleaned.split(/\s+/u).filter((t) => t !== "");
96
+ if (tokens.length === 0) return null;
97
+
98
+ const verb = tokens[0].toLowerCase().replace(/^["']|["']$/gu, "");
99
+ if (!BUSINESS_VERBS.has(verb)) return null;
100
+
101
+ // **扫描**而不是取第一个非旗标:`git -C <path> log` 的子命令在 `-C <值>` 之后。
102
+ let sub = "";
103
+ for (let i = 1; i < tokens.length; i += 1) {
104
+ if (looksLikeSubcommand(tokens[i])) {
105
+ sub = tokens[i].replace(/^["']|["']$/gu, "").toLowerCase();
106
+ break;
107
+ }
108
+ }
109
+
110
+ // 旗标:只留 `-` 开头的、短的、不含路径分隔符的(`--base=<path>` 这类直接丢)。
111
+ const flags = [];
112
+ for (const raw of tokens.slice(1)) {
113
+ const t = raw.replace(/^["']|["']$/gu, "");
114
+ if (t.startsWith("-") && t.length > 1 && t.length <= 24 && !/[/\\=]/u.test(t)) flags.push(t);
115
+ }
116
+
117
+ return { verb, sub, flags: [...new Set(flags)].slice(0, 4) };
118
+ }
119
+
120
+ /**
121
+ * 分组的键。
122
+ *
123
+ * 优先用子命令(`git status`)。**没有子命令时退而用第一个旗标**——因为对
124
+ * `node` / `npm` 这类工具,真正区分语义的常常就是旗标:`node --test`(跑测试)与
125
+ * `node --check`(查语法)是两件事,若键里只有 `node`,它们会被合并成一行,
126
+ * 清单就读不出差别了。这条是端到端实测里发现的(16 次 node 调用被并成一条)。
127
+ *
128
+ * **旗标仍然只是"补充",不是主体**:`git status -C --porcelain` 的键是 `git status`,
129
+ * `-C`(它后面本来跟着一个被丢弃的路径)不该挤进键里。
130
+ */
131
+ export function activityKey(activity) {
132
+ if (!activity) return "";
133
+ const tail = activity.sub || (Array.isArray(activity.flags) ? activity.flags[0] : "") || "";
134
+ return tail ? `${activity.verb} ${tail}` : activity.verb;
135
+ }
136
+
137
+ /**
138
+ * 一批观察 → 业务动作清单(按出现次数降序)。
139
+ *
140
+ * 每条:`{ key, verb, sub, count, flags }`;`flags` 是出现过的旗标并集(截断)。
141
+ * **不含任何位置参数**,因此不含路径与用户名。
142
+ */
143
+ export function tallyActivities(observations) {
144
+ const groups = new Map();
145
+ let shellNoise = 0;
146
+ let totalSegments = 0;
147
+
148
+ for (const observation of observations || []) {
149
+ const command = observation?.command;
150
+ if (typeof command !== "string" || command.trim() === "") continue;
151
+ for (const raw of command.split(SEPARATOR_RE)) {
152
+ if (raw.trim() === "") continue;
153
+ totalSegments += 1;
154
+ const activity = businessCommandOf(raw);
155
+ if (!activity) {
156
+ shellNoise += 1;
157
+ continue;
158
+ }
159
+ const key = activityKey(activity);
160
+ const cur = groups.get(key) || { key, verb: activity.verb, sub: activity.sub, count: 0, flags: new Set() };
161
+ cur.count += 1;
162
+ for (const f of activity.flags) cur.flags.add(f);
163
+ groups.set(key, cur);
164
+ }
165
+ }
166
+
167
+ const activities = [...groups.values()]
168
+ .map((g) => ({ key: g.key, verb: g.verb, sub: g.sub, count: g.count, flags: [...g.flags].slice(0, 6) }))
169
+ .sort((a, b) => (b.count === a.count ? a.key.localeCompare(b.key) : b.count - a.count));
170
+
171
+ return {
172
+ activities,
173
+ // 两个数分开报:「业务动作很少」与「这条命令根本不是动作」是两件事。
174
+ businessSegments: activities.reduce((n, a) => n + a.count, 0),
175
+ shellNoise,
176
+ totalSegments
177
+ };
178
+ }
@@ -0,0 +1,165 @@
1
+ /**
2
+ * M.O.(Modus Operandi)的**归属规则**:把宿主的一次工具动作,映射成「它可能是工作流里的哪一步」。
3
+ *
4
+ * 这个模块存在的唯一理由,是执行链要记账时必须先回答一个问题:**这一次动作算哪一步?**
5
+ * 没有这条规则,观察到的动作与工作流里的步骤编号是两套东西,记下来的账谁也读不懂;
6
+ * 而「认真记了但没人能对账」比不记更糟——它会让面板显示一份看起来完整的假历史。
7
+ *
8
+ * ## 为什么只出候选,绝不下结论
9
+ *
10
+ * 本仓工作流里的 `tool` 字段是**人手写的自由文本**,实测形态:单命令(`npm test`)、
11
+ * 多命令并列(`git log / gh pr list`)、带占位符(`git push <tag>`),19 步里还有 4 步
12
+ * 是空的。拿这种文本去和宿主动作做精确匹配,命中率注定不高,而**误匹配的代价远大于漏匹配**:
13
+ * 把「推 tag」记成「推分支」,`deriveState` 就会据此把错误的一步染成可信。
14
+ *
15
+ * 所以本模块的产出是**候选 + 理由**,由人(或上游的显式动作)确认;认不出来就是空数组,
16
+ * **「认不出来」是合法答案,不许猜**。这与反事实判据那边同一条纪律:机器只写观察到的。
17
+ *
18
+ * ## 匹配分档
19
+ *
20
+ * - `exact`:归一化后整条命令相等(`npm test` ↔ `npm test`)
21
+ * - `weak`:命令头相同、参数不同或缺失(`git push` ↔ `git push <tag>`)
22
+ * - 其余不给候选
23
+ *
24
+ * 同一档内出现多条候选时,**全部返回**、不挑一个——挑一个就等于替人做了决定,而
25
+ * 「两条工作流步骤长得一样」这件事该由人看着消歧。
26
+ *
27
+ * ## 调用方要自己分的三桶
28
+ *
29
+ * 本模块只给 `attributionCandidates` 一个入口;把一批观察归类是调用方的事,但要照这条口径分:
30
+ * matched 有候选(可能多条,由人消歧)
31
+ * unmatched 命令拼得出来,但没有任何步骤对得上
32
+ * skipped 命令行都拼不出来,归属无从谈起
33
+ *
34
+ * **「认不出来」与「没有这一步」必须长得不一样**,否则一份全是 unmatched 的报告会被读成
35
+ * 「这次什么也没做」——与账本那边「缺账」和「坏账」要分开是同一条纪律。
36
+ */
37
+
38
+ /** 命令里的占位符:`<tag>` / `<版本>`。它们不是字面量,不该参与比较。
39
+ * 实测真实工作流只用尖括号这一种形态(`{}` 与 `[]` 各 0 次),不为假想的写法留分支。 */
40
+ const PLACEHOLDER_RE = /<[^>]*>/gu;
41
+
42
+ /**
43
+ * 命令分隔符:一人写的 `tool` 里常见 `git log / gh pr list` 这种并列写法。
44
+ * 用 `/` 分隔有歧义(路径里也有 `/`),所以只在**两侧有空白**时才当分隔符,
45
+ * 否则 `/` 属于参数的一部分(`gh workflow run release-prep.yml` 不能被切碎)。
46
+ * 实测真实工作流只出现这一种(`|` / `;` / `&&` 各 0 次),同样不留假想分支。
47
+ */
48
+ const SEPARATOR_RE = /\s+\/\s+/u;
49
+
50
+ /** 归一化:小写、去占位符、折叠空白。占位符去掉而不是替换成通配,否则 `git push <tag>` 会宽到能匹配一切。 */
51
+ export function normalizeCommand(text) {
52
+ if (typeof text !== "string") return "";
53
+ return text
54
+ .replace(PLACEHOLDER_RE, " ")
55
+ .replace(/\s+/gu, " ")
56
+ .trim()
57
+ .toLowerCase();
58
+ }
59
+
60
+ /** 一条 `tool` 文本可能包含多条并列命令:拆开,逐条参与匹配。 */
61
+ export function commandParts(tool) {
62
+ if (typeof tool !== "string") return [];
63
+ return tool
64
+ .split(SEPARATOR_RE)
65
+ .map((part) => normalizeCommand(part))
66
+ .filter((part) => part !== "");
67
+ }
68
+
69
+ /** 切成词。命令行的词就是空格分隔的片段;这里不做完整的 shell 解析(引号、转义),
70
+ * 因为 `tool` 字段是**人写的说明文本**而不是可执行脚本,解析得越精细越容易把说明当脚本。 */
71
+ function tokensOf(command) {
72
+ return command.split(" ").filter((t) => t !== "");
73
+ }
74
+
75
+ /**
76
+ * 一个是否是另一个的**词前缀**:`["gh","pr","create"]` 是 `["gh","pr","create","--base","main"]`
77
+ * 的前缀。这是「弱匹配」的判据。
78
+ *
79
+ * 为什么不用「第一个词相同」:真实工作流里 `git` 出现 5 次、`gh pr` 出现 4 次,按首词匹配
80
+ * 会让一次 `git push` 报出六条候选——**噪声盖过信号**,人看一眼就放弃了。
81
+ * 词前缀表达的是「这次跑的就是那一步,只是多带了参数」,这才是真实形态:
82
+ * 人写 `gh pr create`,agent 跑的是 `gh pr create --base main --title …`。
83
+ */
84
+ function isTokenPrefix(shorter, longer) {
85
+ if (shorter.length === 0 || shorter.length > longer.length) return false;
86
+ for (let i = 0; i < shorter.length; i += 1) {
87
+ if (shorter[i] !== longer[i]) return false;
88
+ }
89
+ return true;
90
+ }
91
+
92
+ /**
93
+ * 观察记录的形状(上游负责拼)。
94
+ *
95
+ * `command` 是**尽力而为**的命令行:宿主给的是工具名 + 结构化参数,把参数拼成命令行这件事
96
+ * 由调用方按工具类型做。拼不出来就给空串——空串不会匹配上任何东西,是安全的默认。
97
+ */
98
+ export function normalizeObservation(raw) {
99
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
100
+ const command = normalizeCommand(raw.command);
101
+ if (command === "") return null; // 认不出命令行 = 这条观察对归属无用,直接丢给上游
102
+ return {
103
+ tool: typeof raw.tool === "string" ? raw.tool : "",
104
+ command,
105
+ ok: raw.ok === true ? true : raw.ok === false ? false : null // 三态:真 / 假 / 不知道
106
+ };
107
+ }
108
+
109
+ /**
110
+ * 给一次观察找候选步骤。返回按档位排序的数组(exact 在前),可能为空。
111
+ *
112
+ * **不返回「最佳候选」**:那会诱使调用方直接采用它。调用方要决策,就得自己面对
113
+ * 「可能为空」「可能多条」这两个事实。
114
+ */
115
+ export function attributionCandidates(observation, pipeline) {
116
+ const obs = normalizeObservation(observation);
117
+ if (!obs || !pipeline || !Array.isArray(pipeline.steps)) return [];
118
+
119
+ const obsTokens = tokensOf(obs.command);
120
+ const out = [];
121
+ for (const step of pipeline.steps) {
122
+ const parts = commandParts(step.tool);
123
+ // 空 tool 的步骤**结构性地无法被归属**。这不是缺陷,是事实:作者没写它怎么跑。
124
+ // 把它报告出来(而不是跳过),好让「为什么这一步永远没账」有据可查。
125
+ if (parts.length === 0) continue;
126
+
127
+ for (const part of parts) {
128
+ if (part === obs.command) {
129
+ out.push({
130
+ stepId: step.id,
131
+ confidence: "exact",
132
+ matched: part,
133
+ why: `命令与步骤里写的完全一致(${part})`
134
+ });
135
+ break;
136
+ }
137
+ const partTokens = tokensOf(part);
138
+ if (isTokenPrefix(partTokens, obsTokens) || isTokenPrefix(obsTokens, partTokens)) {
139
+ const longer = partTokens.length >= obsTokens.length ? part : obs.command;
140
+ out.push({
141
+ stepId: step.id,
142
+ confidence: "weak",
143
+ matched: part,
144
+ why: `像同一步但参数不同:步骤写的是「${part}」,这次是「${obs.command}」(${longer} 多带了参数)`
145
+ });
146
+ break;
147
+ }
148
+ }
149
+ }
150
+
151
+ out.sort((a, b) => (a.confidence === b.confidence ? a.stepId.localeCompare(b.stepId) : a.confidence === "exact" ? -1 : 1));
152
+ return out;
153
+ }
154
+
155
+ /**
156
+ * 结构性不可归属的步骤:作者没写 `tool`,所以它**永远不会**拿到账。
157
+ *
158
+ * 单独给一个入口,是为了让「为什么这一步永远没账」有据可查——否则它看起来只是
159
+ * 「还没跑过」,而真相是这条工作流根本没写明它怎么跑。
160
+ */
161
+ export function unattributableSteps(pipeline) {
162
+ return (pipeline && Array.isArray(pipeline.steps) ? pipeline.steps : [])
163
+ .filter((s) => commandParts(s.tool).length === 0)
164
+ .map((s) => s.id);
165
+ }