@dommaker/harness 1.4.0 → 1.6.0

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 (258) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +0 -2
  3. package/dist/cli/commands/check.d.ts.map +1 -1
  4. package/dist/cli/commands/check.js +28 -0
  5. package/dist/cli/commands/check.js.map +1 -1
  6. package/dist/cli/commands/definitions.d.ts.map +1 -1
  7. package/dist/cli/commands/definitions.js +1 -39
  8. package/dist/cli/commands/definitions.js.map +1 -1
  9. package/dist/cli/commands/knowledge.d.ts +0 -19
  10. package/dist/cli/commands/knowledge.d.ts.map +1 -1
  11. package/dist/cli/commands/knowledge.js +0 -105
  12. package/dist/cli/commands/knowledge.js.map +1 -1
  13. package/dist/cli/commands/passes-gate.d.ts +0 -2
  14. package/dist/cli/commands/passes-gate.d.ts.map +1 -1
  15. package/dist/cli/commands/passes-gate.js +0 -1
  16. package/dist/cli/commands/passes-gate.js.map +1 -1
  17. package/dist/cli/commands/performance.d.ts +0 -4
  18. package/dist/cli/commands/performance.d.ts.map +1 -1
  19. package/dist/cli/commands/performance.js +10 -20
  20. package/dist/cli/commands/performance.js.map +1 -1
  21. package/dist/cli/commands/report.d.ts +1 -1
  22. package/dist/cli/commands/report.d.ts.map +1 -1
  23. package/dist/cli/commands/report.js +2 -55
  24. package/dist/cli/commands/report.js.map +1 -1
  25. package/dist/cli/commands/status.d.ts.map +1 -1
  26. package/dist/cli/commands/status.js +4 -8
  27. package/dist/cli/commands/status.js.map +1 -1
  28. package/dist/context/index.d.ts +2 -5
  29. package/dist/context/index.d.ts.map +1 -1
  30. package/dist/context/index.js +2 -8
  31. package/dist/context/index.js.map +1 -1
  32. package/dist/context/token-budget.d.ts +0 -29
  33. package/dist/context/token-budget.d.ts.map +1 -1
  34. package/dist/context/token-budget.js +1 -65
  35. package/dist/context/token-budget.js.map +1 -1
  36. package/dist/context/types.d.ts +0 -1
  37. package/dist/context/types.d.ts.map +1 -1
  38. package/dist/context/types.js +0 -9
  39. package/dist/context/types.js.map +1 -1
  40. package/dist/core/constraints/checker.d.ts +14 -0
  41. package/dist/core/constraints/checker.d.ts.map +1 -1
  42. package/dist/core/constraints/checker.js +27 -5
  43. package/dist/core/constraints/checker.js.map +1 -1
  44. package/dist/core/constraints/checkers/capability-sync.d.ts +17 -6
  45. package/dist/core/constraints/checkers/capability-sync.d.ts.map +1 -1
  46. package/dist/core/constraints/checkers/capability-sync.js +48 -19
  47. package/dist/core/constraints/checkers/capability-sync.js.map +1 -1
  48. package/dist/core/constraints/checkers/docs-freshness.d.ts.map +1 -1
  49. package/dist/core/constraints/checkers/docs-freshness.js +15 -6
  50. package/dist/core/constraints/checkers/docs-freshness.js.map +1 -1
  51. package/dist/core/constraints/checkers/index.d.ts +2 -2
  52. package/dist/core/constraints/checkers/index.d.ts.map +1 -1
  53. package/dist/core/constraints/checkers/index.js +2 -1
  54. package/dist/core/constraints/checkers/index.js.map +1 -1
  55. package/dist/core/constraints/checkers/types.d.ts +41 -2
  56. package/dist/core/constraints/checkers/types.d.ts.map +1 -1
  57. package/dist/core/constraints/checkers/types.js +33 -0
  58. package/dist/core/constraints/checkers/types.js.map +1 -1
  59. package/dist/core/constraints/git-evidence.d.ts +6 -3
  60. package/dist/core/constraints/git-evidence.d.ts.map +1 -1
  61. package/dist/core/constraints/git-evidence.js +6 -3
  62. package/dist/core/constraints/git-evidence.js.map +1 -1
  63. package/dist/core/index.d.ts +1 -2
  64. package/dist/core/index.d.ts.map +1 -1
  65. package/dist/core/index.js +1 -9
  66. package/dist/core/index.js.map +1 -1
  67. package/dist/core/spec/validator.d.ts +2 -0
  68. package/dist/core/spec/validator.d.ts.map +1 -1
  69. package/dist/core/spec/validator.js +9 -11
  70. package/dist/core/spec/validator.js.map +1 -1
  71. package/dist/core/validators/index.d.ts +1 -1
  72. package/dist/core/validators/index.d.ts.map +1 -1
  73. package/dist/core/validators/passes-gate.d.ts +1 -15
  74. package/dist/core/validators/passes-gate.d.ts.map +1 -1
  75. package/dist/core/validators/passes-gate.js +0 -125
  76. package/dist/core/validators/passes-gate.js.map +1 -1
  77. package/dist/failure/index.d.ts +0 -1
  78. package/dist/failure/index.d.ts.map +1 -1
  79. package/dist/failure/index.js +1 -7
  80. package/dist/failure/index.js.map +1 -1
  81. package/dist/gates/checker-gate.d.ts +2 -1
  82. package/dist/gates/checker-gate.d.ts.map +1 -1
  83. package/dist/gates/checker-gate.js +10 -7
  84. package/dist/gates/checker-gate.js.map +1 -1
  85. package/dist/gates/definitions.d.ts.map +1 -1
  86. package/dist/gates/definitions.js +0 -4
  87. package/dist/gates/definitions.js.map +1 -1
  88. package/dist/gates/performance.d.ts +7 -22
  89. package/dist/gates/performance.d.ts.map +1 -1
  90. package/dist/gates/performance.js +7 -89
  91. package/dist/gates/performance.js.map +1 -1
  92. package/dist/gates/types.d.ts +2 -8
  93. package/dist/gates/types.d.ts.map +1 -1
  94. package/dist/index.d.ts +6 -8
  95. package/dist/index.d.ts.map +1 -1
  96. package/dist/index.js +4 -22
  97. package/dist/index.js.map +1 -1
  98. package/dist/knowledge/index.d.ts +0 -1
  99. package/dist/knowledge/index.d.ts.map +1 -1
  100. package/dist/knowledge/index.js +1 -3
  101. package/dist/knowledge/index.js.map +1 -1
  102. package/dist/monitoring/trace-analyzer.d.ts +25 -10
  103. package/dist/monitoring/trace-analyzer.d.ts.map +1 -1
  104. package/dist/monitoring/trace-analyzer.js +147 -116
  105. package/dist/monitoring/trace-analyzer.js.map +1 -1
  106. package/dist/types/constraint.d.ts +8 -0
  107. package/dist/types/constraint.d.ts.map +1 -1
  108. package/dist/types/constraint.js +6 -1
  109. package/dist/types/constraint.js.map +1 -1
  110. package/dist/types/passes-gate.d.ts +0 -19
  111. package/dist/types/passes-gate.d.ts.map +1 -1
  112. package/dist/types/trace.d.ts +9 -0
  113. package/dist/types/trace.d.ts.map +1 -1
  114. package/package.json +1 -1
  115. package/src/CONTEXT.md +1 -0
  116. package/src/__tests__/passes-gate-check.test.ts +20 -4
  117. package/src/__tests__/passes-gate.test.ts +18 -119
  118. package/src/__tests__/performance-gate.test.ts +3 -11
  119. package/src/__tests__/public-exports.test.ts +46 -16
  120. package/src/__tests__/trace-analyzer.test.ts +52 -2
  121. package/src/cli/commands/CONTEXT.md +5 -4
  122. package/src/cli/commands/__tests__/bin-exit-mapping.test.ts +6 -0
  123. package/src/cli/commands/__tests__/check.test.ts +74 -0
  124. package/src/cli/commands/__tests__/passes-gate-dead-options.test.ts +70 -0
  125. package/src/cli/commands/__tests__/passes-gate.test.ts +7 -3
  126. package/src/cli/commands/__tests__/performance.test.ts +5 -16
  127. package/src/cli/commands/__tests__/project-path-convention.test.ts +85 -23
  128. package/src/cli/commands/__tests__/registry.test.ts +52 -3
  129. package/src/cli/commands/__tests__/report-extra.test.ts +0 -19
  130. package/src/cli/commands/__tests__/report-false-green.test.ts +46 -0
  131. package/src/cli/commands/__tests__/report.test.ts +0 -20
  132. package/src/cli/commands/__tests__/status-analysis-wiring.test.ts +68 -0
  133. package/src/cli/commands/__tests__/status-extra.test.ts +63 -116
  134. package/src/cli/commands/__tests__/status.test.ts +66 -148
  135. package/src/cli/commands/check.ts +39 -1
  136. package/src/cli/commands/definitions.ts +1 -39
  137. package/src/cli/commands/knowledge.ts +0 -119
  138. package/src/cli/commands/passes-gate.ts +0 -3
  139. package/src/cli/commands/performance.ts +13 -27
  140. package/src/cli/commands/report.ts +3 -61
  141. package/src/cli/commands/status.ts +5 -10
  142. package/src/context/CONTEXT.md +2 -3
  143. package/src/context/__tests__/token-budget.test.ts +1 -81
  144. package/src/context/index.ts +2 -7
  145. package/src/context/token-budget.ts +0 -73
  146. package/src/context/types.ts +0 -9
  147. package/src/core/CONTEXT.md +6 -6
  148. package/src/core/constraints/__tests__/collect-constraints.test.ts +66 -0
  149. package/src/core/constraints/checker.ts +41 -6
  150. package/src/core/constraints/checkers/__tests__/capability-sync.test.ts +90 -31
  151. package/src/core/constraints/checkers/__tests__/check-outcome.test.ts +176 -0
  152. package/src/core/constraints/checkers/__tests__/docs-freshness.test.ts +18 -9
  153. package/src/core/constraints/checkers/__tests__/three-state-consistency.test.ts +12 -4
  154. package/src/core/constraints/checkers/capability-sync.ts +56 -18
  155. package/src/core/constraints/checkers/docs-freshness.ts +18 -5
  156. package/src/core/constraints/checkers/index.ts +9 -2
  157. package/src/core/constraints/checkers/types.ts +58 -2
  158. package/src/core/constraints/git-evidence.ts +6 -3
  159. package/src/core/index.ts +0 -11
  160. package/src/core/spec/__tests__/validator.test.ts +24 -0
  161. package/src/core/spec/validator.ts +9 -10
  162. package/src/core/validators/__tests__/passes-gate.test.ts +2 -9
  163. package/src/core/validators/index.ts +0 -1
  164. package/src/core/validators/passes-gate.ts +1 -149
  165. package/src/failure/CONTEXT.md +8 -6
  166. package/src/failure/index.ts +0 -10
  167. package/src/gates/CONTEXT.md +4 -3
  168. package/src/gates/__tests__/checker-gate.test.ts +24 -0
  169. package/src/gates/__tests__/performance.test.ts +6 -74
  170. package/src/gates/checker-gate.ts +11 -15
  171. package/src/gates/definitions.ts +0 -4
  172. package/src/gates/performance.ts +9 -111
  173. package/src/gates/types.ts +2 -8
  174. package/src/index.ts +1 -36
  175. package/src/knowledge/CONTEXT.md +1 -2
  176. package/src/knowledge/index.ts +0 -1
  177. package/src/monitoring/CONTEXT.md +2 -2
  178. package/src/monitoring/trace-analyzer.ts +160 -126
  179. package/src/types/constraint.ts +16 -2
  180. package/src/types/passes-gate.ts +0 -26
  181. package/src/types/trace.ts +10 -0
  182. package/src/utils/__tests__/jsonl-skip-disposition.test.ts +0 -1
  183. package/dist/cli/commands/analyze-sessions.d.ts +0 -20
  184. package/dist/cli/commands/analyze-sessions.d.ts.map +0 -1
  185. package/dist/cli/commands/analyze-sessions.js +0 -290
  186. package/dist/cli/commands/analyze-sessions.js.map +0 -1
  187. package/dist/cli/commands/update-user-model.d.ts +0 -25
  188. package/dist/cli/commands/update-user-model.d.ts.map +0 -1
  189. package/dist/cli/commands/update-user-model.js +0 -397
  190. package/dist/cli/commands/update-user-model.js.map +0 -1
  191. package/dist/cli/session-mining/corrections.d.ts +0 -17
  192. package/dist/cli/session-mining/corrections.d.ts.map +0 -1
  193. package/dist/cli/session-mining/corrections.js +0 -58
  194. package/dist/cli/session-mining/corrections.js.map +0 -1
  195. package/dist/cli/session-mining/index.d.ts +0 -13
  196. package/dist/cli/session-mining/index.d.ts.map +0 -1
  197. package/dist/cli/session-mining/index.js +0 -29
  198. package/dist/cli/session-mining/index.js.map +0 -1
  199. package/dist/cli/session-mining/text.d.ts +0 -31
  200. package/dist/cli/session-mining/text.d.ts.map +0 -1
  201. package/dist/cli/session-mining/text.js +0 -126
  202. package/dist/cli/session-mining/text.js.map +0 -1
  203. package/dist/cli/session-mining/transcript.d.ts +0 -34
  204. package/dist/cli/session-mining/transcript.d.ts.map +0 -1
  205. package/dist/cli/session-mining/transcript.js +0 -130
  206. package/dist/cli/session-mining/transcript.js.map +0 -1
  207. package/dist/context/compaction.d.ts +0 -57
  208. package/dist/context/compaction.d.ts.map +0 -1
  209. package/dist/context/compaction.js +0 -165
  210. package/dist/context/compaction.js.map +0 -1
  211. package/dist/core/session/clean-state.d.ts +0 -17
  212. package/dist/core/session/clean-state.d.ts.map +0 -1
  213. package/dist/core/session/clean-state.js +0 -192
  214. package/dist/core/session/clean-state.js.map +0 -1
  215. package/dist/core/session/index.d.ts +0 -6
  216. package/dist/core/session/index.d.ts.map +0 -1
  217. package/dist/core/session/index.js +0 -15
  218. package/dist/core/session/index.js.map +0 -1
  219. package/dist/core/session/startup.d.ts +0 -47
  220. package/dist/core/session/startup.d.ts.map +0 -1
  221. package/dist/core/session/startup.js +0 -263
  222. package/dist/core/session/startup.js.map +0 -1
  223. package/dist/failure/constraint-handler.d.ts +0 -83
  224. package/dist/failure/constraint-handler.d.ts.map +0 -1
  225. package/dist/failure/constraint-handler.js +0 -138
  226. package/dist/failure/constraint-handler.js.map +0 -1
  227. package/dist/knowledge/lifecycle-hooks.d.ts +0 -62
  228. package/dist/knowledge/lifecycle-hooks.d.ts.map +0 -1
  229. package/dist/knowledge/lifecycle-hooks.js +0 -143
  230. package/dist/knowledge/lifecycle-hooks.js.map +0 -1
  231. package/dist/types/session.d.ts +0 -113
  232. package/dist/types/session.d.ts.map +0 -1
  233. package/dist/types/session.js +0 -6
  234. package/dist/types/session.js.map +0 -1
  235. package/src/__tests__/clean-state-extra.test.ts +0 -301
  236. package/src/__tests__/clean-state.test.ts +0 -169
  237. package/src/__tests__/constraint-handler.test.ts +0 -132
  238. package/src/__tests__/passes-gate-extra.test.ts +0 -93
  239. package/src/__tests__/session-startup.test.ts +0 -334
  240. package/src/cli/commands/__tests__/analyze-sessions.test.ts +0 -184
  241. package/src/cli/commands/__tests__/update-user-model.test.ts +0 -234
  242. package/src/cli/commands/analyze-sessions.ts +0 -335
  243. package/src/cli/commands/update-user-model.ts +0 -461
  244. package/src/cli/session-mining/__tests__/transcript.test.ts +0 -118
  245. package/src/cli/session-mining/corrections.ts +0 -56
  246. package/src/cli/session-mining/index.ts +0 -13
  247. package/src/cli/session-mining/text.ts +0 -117
  248. package/src/cli/session-mining/transcript.ts +0 -125
  249. package/src/context/__tests__/compaction.test.ts +0 -169
  250. package/src/context/compaction.ts +0 -201
  251. package/src/core/session/__tests__/startup.test.ts +0 -281
  252. package/src/core/session/clean-state.ts +0 -178
  253. package/src/core/session/index.ts +0 -15
  254. package/src/core/session/startup.ts +0 -253
  255. package/src/failure/constraint-handler.ts +0 -171
  256. package/src/knowledge/__tests__/lifecycle-hooks.test.ts +0 -140
  257. package/src/knowledge/lifecycle-hooks.ts +0 -202
  258. package/src/types/session.ts +0 -132
@@ -1,461 +0,0 @@
1
- /**
2
- * harness update-user-model — 用户模型持续演化引擎
3
- *
4
- * 不依赖固定规则快照。每天从最新数据中提取增量信号,
5
- * 对比昨天状态,输出变化,更新模型。
6
- *
7
- * 数据源:
8
- * ~/.claude/projects/-root--claude/*.jsonl (对话)
9
- * /tmp/claude-knowledge-hooks.log (行为信号)
10
- * studio/.harness/knowledge/ (知识新鲜度)
11
- * ~/.claude/projects/-root-projects/memory/ (规则库)
12
- *
13
- * 模型状态: ~/.claude/user-model-state.json
14
- * 画像输出: ~/.claude/projects/-root-projects/memory/user_profile.md
15
- *
16
- * 工单 19-C:transcript 解析/纠正模式/相似度收敛至 cli/session-mining/。
17
- */
18
-
19
- import chalk from 'chalk';
20
- import * as fs from 'fs';
21
- import * as path from 'path';
22
- import * as os from 'os';
23
- import {
24
- readTranscriptSessions,
25
- type TranscriptFilter,
26
- extractCorrectionMatches,
27
- cleanCorrectionConcept,
28
- jaccardChinese,
29
- } from '../session-mining';
30
- import { log, processIO, type CommandIO, type CommandResult } from '../command-contract';
31
-
32
- export interface UpdateUserModelOptions {
33
- days?: number; // 只处理最近 N 天(自然日,含今天)的会话;缺省不过滤(向后兼容)
34
- json?: boolean;
35
- dryRun?: boolean; // don't update state, just show what would change
36
- }
37
-
38
- interface ModelState {
39
- lastUpdated: string;
40
- sessionsProcessed: string[]; // session IDs already counted
41
- patterns: Record<string, { // pattern → running stats
42
- firstSeen: string;
43
- occurrences: number;
44
- sessions: string[];
45
- trend: 'rising' | 'stable' | 'falling' | 'new' | 'gone';
46
- lastSeen: string;
47
- }>;
48
- lensWeights: Record<string, number>; // lens → activation count → normalized weight
49
- principleWeights: Record<string, number>;
50
- evolutionLog: Array<{ date: string; change: string }>;
51
- }
52
-
53
- const STATE_FILE = path.join(os.homedir(), '.claude', 'user-model-state.json');
54
- const PROFILE_FILE = path.join(os.homedir(), '.claude', 'projects', '-root-projects', 'memory', 'user_profile.md');
55
-
56
- export async function updateUserModel(options: UpdateUserModelOptions, io: CommandIO = processIO): Promise<CommandResult> {
57
- const transcriptDir = process.env.CLAUDE_TRANSCRIPTS_DIR
58
- || path.join(os.homedir(), '.claude', 'projects', '-root--claude');
59
-
60
- // 1. Load previous state
61
- const state = loadState();
62
-
63
- // 2. Scan new data
64
- const newSessions = findNewSessions(transcriptDir, state.sessionsProcessed, options.days);
65
- if (newSessions.length === 0) {
66
- log(io, chalk.gray('No new sessions to process'));
67
- return { kind: 'skip', reason: '没有新会话可处理' };
68
- }
69
-
70
- // 3. Extract signals from new data
71
- const signals = extractSignals(newSessions);
72
-
73
- // 4. Build concept clusters (semantic dedup)
74
- const mergedConcepts = buildMergedConcepts(signals);
75
-
76
- // 5. Diff against previous state → find changes
77
- const changes = diffState(state, mergedConcepts, signals);
78
-
79
- // 6. Update state
80
- if (!options.dryRun) {
81
- for (const sid of newSessions.map(s => s.id)) {
82
- if (!state.sessionsProcessed.includes(sid)) {
83
- state.sessionsProcessed.push(sid);
84
- }
85
- }
86
- applySignals(state, signals, mergedConcepts);
87
- state.lastUpdated = new Date().toISOString();
88
-
89
- if (state.sessionsProcessed.length > 200) {
90
- state.sessionsProcessed = state.sessionsProcessed.slice(-200);
91
- }
92
-
93
- saveState(state);
94
- updateProfile(state);
95
- }
96
-
97
- // 6. Output
98
- if (options.json) {
99
- log(io, JSON.stringify({ newSessions: newSessions.length, changes }, null, 2));
100
- return { kind: 'ok' };
101
- }
102
-
103
- log(io, chalk.blue(`📊 Processed ${newSessions.length} new sessions\n`));
104
- printChanges(changes, signals, io);
105
- return { kind: 'ok' };
106
- }
107
-
108
- // ── State I/O ──
109
-
110
- function loadState(): ModelState {
111
- try {
112
- if (fs.existsSync(STATE_FILE)) {
113
- return JSON.parse(fs.readFileSync(STATE_FILE, 'utf-8'));
114
- }
115
- } catch {}
116
- return {
117
- lastUpdated: new Date(0).toISOString(),
118
- sessionsProcessed: [],
119
- patterns: {},
120
- lensWeights: {},
121
- principleWeights: {},
122
- evolutionLog: [],
123
- };
124
- }
125
-
126
- function saveState(state: ModelState): void {
127
- try {
128
- const dir = path.dirname(STATE_FILE);
129
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
130
- fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2), 'utf-8');
131
- } catch {}
132
- }
133
-
134
- // ── Session scanning ──
135
-
136
- interface SimpleSession {
137
- id: string;
138
- date: string;
139
- userText: string; // all user messages joined
140
- assistantText: string; // all assistant messages joined
141
- toolCalls: string[]; // tool names used
142
- }
143
-
144
- function findNewSessions(dir: string, processed: string[], days?: number): SimpleSession[] {
145
- // 过滤下推 seam(harness#112):excludeIds 走文件名、since 走 stat,未命中不 parse
146
- const filter: TranscriptFilter = { excludeIds: processed };
147
-
148
- // 「最近 N 天」:自然日窗口(含今天)。days<=0 视为不过滤,与缺省一致。
149
- // date 是 mtime 的 UTC 日,date >= cutoff ⟺ mtimeMs >= cutoff 日 UTC 零点
150
- if (days !== undefined && days > 0) {
151
- const cutoff = new Date(Date.now() - (days - 1) * 86_400_000)
152
- .toISOString()
153
- .slice(0, 10);
154
- filter.since = Date.parse(cutoff);
155
- }
156
-
157
- const sessions = readTranscriptSessions(dir, filter);
158
-
159
- sessions.sort((a, b) => a.date.localeCompare(b.date));
160
-
161
- return sessions.map(s => ({
162
- id: s.id,
163
- date: s.date,
164
- userText: s.turns.filter(t => t.role === 'user').map(t => t.content).join('\n'),
165
- assistantText: s.turns.filter(t => t.role === 'assistant').map(t => t.content).join('\n'),
166
- toolCalls: s.toolCalls,
167
- }));
168
- }
169
-
170
- // ── Signal extraction ──
171
-
172
- interface SessionSignals {
173
- sessionId: string;
174
- date: string;
175
- correctionPhrases: string[]; // "你又..." etc.
176
- concepts: Record<string, number>; // N-gram → frequency
177
- toolPatterns: Record<string, number>; // tool → count
178
- sessionDuration: number; // estimated from turn count
179
- turnCount: number;
180
- deepAnalysis: boolean; // EnterPlanMode or Agent(Explore)
181
- knowledgeCaptured: boolean; // Write to knowledge-docs
182
- }
183
-
184
- function extractCorrectionConcepts(userText: string): string[] {
185
- const concepts: string[] = [];
186
- for (const sentence of extractCorrectionMatches(userText)) {
187
- const cleaned = cleanCorrectionConcept(sentence);
188
- if (cleaned.length >= 4 && cleaned.length <= 60) concepts.push(cleaned);
189
- }
190
- return [...new Set(concepts)];
191
- }
192
-
193
- // ── Semantic clustering ──
194
-
195
- function clusterConcepts(
196
- agg: Record<string, { count: number; sessions: string[] }>
197
- ): Record<string, { count: number; sessions: string[] }> {
198
- const entries = Object.entries(agg).filter(([k]) => k.length >= 4);
199
- if (entries.length <= 1) return agg;
200
-
201
- const merged: Record<string, { count: number; sessions: string[] }> = {};
202
- const used = new Set<number>();
203
-
204
- for (let i = 0; i < entries.length; i++) {
205
- if (used.has(i)) continue;
206
- const [concept, data] = entries[i];
207
- let clusterName = concept;
208
- let clusterCount = data.count;
209
- const clusterSessions = new Set(data.sessions);
210
- used.add(i);
211
-
212
- for (let j = i + 1; j < entries.length; j++) {
213
- if (used.has(j)) continue;
214
- const [other, otherData] = entries[j];
215
- const sim = jaccardChinese(concept, other);
216
- if (sim > 0.5) {
217
- clusterCount += otherData.count;
218
- for (const s of otherData.sessions) clusterSessions.add(s);
219
- // Use the shorter name as cluster label
220
- if (other.length < clusterName.length) clusterName = other;
221
- used.add(j);
222
- }
223
- }
224
-
225
- merged[clusterName] = { count: clusterCount, sessions: [...clusterSessions] };
226
- }
227
-
228
- return merged;
229
- }
230
-
231
- function extractSignals(sessions: SimpleSession[]): SessionSignals[] {
232
- return sessions.map(s => {
233
- const correctionPhrases = extractCorrectionConcepts(s.userText);
234
-
235
- // Concept extraction (Chinese char N-grams, etc.)
236
- const concepts: Record<string, number> = {};
237
- const cleanText = s.userText
238
- .replace(/```[\s\S]*?```/g, '')
239
- .replace(/\{[^{}]*\}/g, '')
240
- .replace(/[{}[\]"':,\\]+/g, ' ');
241
- let cnSeq = '';
242
- for (const char of cleanText) {
243
- if (/[\u4e00-\u9fff]/.test(char)) {
244
- cnSeq += char;
245
- } else {
246
- if (cnSeq.length >= 4) concepts[cnSeq] = (concepts[cnSeq] || 0) + 1;
247
- cnSeq = '';
248
- }
249
- }
250
- if (cnSeq.length >= 4) concepts[cnSeq] = (concepts[cnSeq] || 0) + 1;
251
-
252
- // Tool patterns
253
- const toolPatterns: Record<string, number> = {};
254
- for (const t of s.toolCalls) {
255
- toolPatterns[t] = (toolPatterns[t] || 0) + 1;
256
- }
257
-
258
- return {
259
- sessionId: s.id,
260
- date: s.date,
261
- correctionPhrases,
262
- concepts,
263
- toolPatterns,
264
- sessionDuration: s.userText.length + s.assistantText.length, // proxy
265
- turnCount: (s.userText.match(/\n/g) || []).length,
266
- deepAnalysis: s.toolCalls.includes('EnterPlanMode') || s.toolCalls.includes('Agent'),
267
- knowledgeCaptured: s.toolCalls.includes('Write') && (
268
- s.assistantText.includes('.harness/knowledge-docs/') ||
269
- s.assistantText.includes('.harness/knowledge/')
270
- ),
271
- };
272
- });
273
- }
274
-
275
- // ── Concept aggregation ──
276
-
277
- type ConceptMap = Record<string, { count: number; sessions: string[] }>;
278
-
279
- function buildMergedConcepts(signals: SessionSignals[]): ConceptMap {
280
- const agg: ConceptMap = {};
281
- for (const sig of signals) {
282
- for (const [concept, count] of Object.entries(sig.concepts)) {
283
- if (!agg[concept]) agg[concept] = { count: 0, sessions: [] };
284
- agg[concept].count += count;
285
- if (!agg[concept].sessions.includes(sig.sessionId)) {
286
- agg[concept].sessions.push(sig.sessionId);
287
- }
288
- }
289
- for (const phrase of sig.correctionPhrases) {
290
- if (phrase.length < 4) continue;
291
- if (!agg[phrase]) agg[phrase] = { count: 0, sessions: [] };
292
- agg[phrase].count += 3;
293
- if (!agg[phrase].sessions.includes(sig.sessionId)) {
294
- agg[phrase].sessions.push(sig.sessionId);
295
- }
296
- }
297
- }
298
- return clusterConcepts(agg);
299
- }
300
-
301
- // ── Diff & Apply ──
302
-
303
- interface Change {
304
- type: 'new_pattern' | 'rising' | 'falling' | 'gone' | 'lens_shift';
305
- key: string;
306
- detail: string;
307
- }
308
-
309
- function diffState(state: ModelState, mergedConcepts: ConceptMap, signals: SessionSignals[]): Change[] {
310
- const changes: Change[] = [];
311
-
312
- // Detect new/rising/falling/gone concepts (from merged clusters)
313
- for (const [concept, agg] of Object.entries(mergedConcepts)) {
314
- const existing = state.patterns[concept];
315
- if (!existing) {
316
- if (agg.sessions.length >= 2) {
317
- changes.push({ type: 'new_pattern', key: concept, detail: `Appeared in ${agg.sessions.length} sessions` });
318
- }
319
- } else if (existing.trend === 'gone' || existing.trend === 'falling') {
320
- changes.push({ type: 'rising', key: concept, detail: `Re-emerged after being ${existing.trend}` });
321
- }
322
- }
323
-
324
- // Detect lens weight shifts (from correction patterns)
325
- const totalCorrections = signals.reduce((sum, s) => sum + s.correctionPhrases.length, 0);
326
- if (totalCorrections >= 3) {
327
- changes.push({ type: 'lens_shift', key: 'completeness',
328
- detail: `${totalCorrections} corrections in these sessions — completeness lens may need weight increase` });
329
- }
330
-
331
- return changes;
332
- }
333
-
334
- function applySignals(state: ModelState, signals: SessionSignals[], mergedConcepts: ConceptMap): void {
335
- const now = new Date().toISOString().slice(0, 10);
336
-
337
- // Update patterns from merged concept clusters
338
- // occurrences 唯一来源:mergedConcepts 聚合值(含 correction phrase 的 ×3 加权,
339
- // 见 buildMergedConcepts)。不再叠加 per-session 原始 count —— 那是 #113 裁决的双计 bug。
340
- for (const [concept, agg] of Object.entries(mergedConcepts)) {
341
- if (!state.patterns[concept]) {
342
- state.patterns[concept] = {
343
- firstSeen: now, occurrences: 0, sessions: [], trend: 'new', lastSeen: now,
344
- };
345
- }
346
- const p = state.patterns[concept];
347
- p.occurrences += agg.count;
348
- for (const sid of agg.sessions) {
349
- if (!p.sessions.includes(sid)) p.sessions.push(sid);
350
- }
351
- p.lastSeen = now;
352
- p.trend = p.occurrences >= 5 ? 'stable' : 'rising';
353
- }
354
-
355
- // Update lens weights from correction signals
356
- for (const sig of signals) {
357
- if (sig.correctionPhrases.length > 0) {
358
- state.lensWeights.completeness = (state.lensWeights.completeness || 0) + 1;
359
- state.lensWeights.automation = (state.lensWeights.automation || 0) + (sig.correctionPhrases.length > 2 ? 1 : 0);
360
- }
361
- if (sig.deepAnalysis) {
362
- state.lensWeights.first_principles = (state.lensWeights.first_principles || 0) + 1;
363
- }
364
- }
365
-
366
- // Detect falling/stable shifts
367
- for (const [, p] of Object.entries(state.patterns)) {
368
- if (p.trend === 'stable' && p.lastSeen < new Date(Date.now() - 7 * 86400000).toISOString().slice(0, 10)) {
369
- p.trend = 'falling';
370
- }
371
- }
372
- }
373
-
374
- // ── Profile update ──
375
-
376
- function updateProfile(state: ModelState): void {
377
- try {
378
- let content = fs.readFileSync(PROFILE_FILE, 'utf-8');
379
-
380
- // Replace Derived Rules section
381
- const derivedStart = '## Derived Rules';
382
- const derivedEnd = '\n## Evolution';
383
- const derivedIdx = content.indexOf(derivedStart);
384
-
385
- const activePatterns = Object.entries(state.patterns)
386
- .filter(([, p]) => (p.trend === 'rising' || p.trend === 'stable') && p.sessions.length >= 2)
387
- .sort(([, a], [, b]) => b.occurrences - a.occurrences)
388
- .slice(0, 10);
389
-
390
- const derivedContent = [
391
- '## Derived Rules (auto-generated by update-user-model)',
392
- `Last scan: ${state.lastUpdated}. ${state.sessionsProcessed.length} sessions analyzed.`,
393
- '',
394
- '| Pattern | Trend | Occurrences | Sessions |',
395
- '|---------|-------|-------------|----------|',
396
- ...activePatterns.map(([k, p]) =>
397
- `| ${k} | ${p.trend} | ${p.occurrences} | ${p.sessions.length} |`),
398
- '',
399
- ].join('\n');
400
-
401
- if (derivedIdx >= 0) {
402
- content = content.slice(0, derivedIdx) + derivedContent + '\n' + content.slice(content.indexOf(derivedEnd, derivedIdx));
403
- } else {
404
- // No derived section yet — append before Evolution
405
- const evIdx = content.indexOf('## Evolution');
406
- if (evIdx >= 0) {
407
- content = content.slice(0, evIdx) + derivedContent + '\n' + content.slice(evIdx);
408
- } else {
409
- content += '\n' + derivedContent;
410
- }
411
- }
412
-
413
- fs.writeFileSync(PROFILE_FILE, content, 'utf-8');
414
- } catch (e) {
415
- // Profile file might not exist yet — skip
416
- }
417
- }
418
-
419
- // ── Output ──
420
-
421
- function printChanges(changes: Change[], signals: SessionSignals[], io: CommandIO): void {
422
- if (changes.length === 0) {
423
- log(io, chalk.green('No significant changes detected'));
424
- return;
425
- }
426
-
427
- const byType = {
428
- new_pattern: changes.filter(c => c.type === 'new_pattern'),
429
- rising: changes.filter(c => c.type === 'rising'),
430
- lens_shift: changes.filter(c => c.type === 'lens_shift'),
431
- };
432
-
433
- if (byType.new_pattern.length > 0) {
434
- log(io, chalk.yellow(`🌱 New patterns: ${byType.new_pattern.length}`));
435
- for (const c of byType.new_pattern.slice(0, 5)) {
436
- log(io, chalk.gray(` ${c.key}: ${c.detail}`));
437
- }
438
- log(io);
439
- }
440
-
441
- if (byType.rising.length > 0) {
442
- log(io, chalk.green(`📈 Re-emerging: ${byType.rising.length}`));
443
- for (const c of byType.rising.slice(0, 5)) {
444
- log(io, chalk.gray(` ${c.key}: ${c.detail}`));
445
- }
446
- log(io);
447
- }
448
-
449
- if (byType.lens_shift.length > 0) {
450
- log(io, chalk.cyan(`🎯 Lens shifts:`));
451
- for (const c of byType.lens_shift) {
452
- log(io, chalk.gray(` ${c.key}: ${c.detail}`));
453
- }
454
- log(io);
455
- }
456
-
457
- log(io, chalk.bold(`Total signals processed:`));
458
- log(io, ` Sessions: ${signals.length}`);
459
- log(io, ` Correction phrases: ${signals.reduce((s, sig) => s + sig.correctionPhrases.length, 0)}`);
460
- log(io, ` Concepts extracted: ${signals.reduce((s, sig) => s + Object.keys(sig.concepts).length, 0)}`);
461
- }
@@ -1,118 +0,0 @@
1
- /**
2
- * readTranscriptSessions 坏行/null 记录容错测试(harness#82)
3
- *
4
- * transcript 行可 parse 但值为 null(或提取异常)时,
5
- * 与原逐行 catch 实现一致:该条跳过,不影响其余记录。
6
- */
7
-
8
- import * as fs from 'fs';
9
- import * as os from 'os';
10
- import * as path from 'path';
11
- import { readTranscriptSessions } from '../transcript';
12
-
13
- describe('readTranscriptSessions 坏行容错(harness#82)', () => {
14
- let dir: string;
15
-
16
- beforeEach(() => {
17
- dir = fs.mkdtempSync(path.join(os.tmpdir(), 'harness-transcript-'));
18
- });
19
-
20
- afterEach(() => {
21
- fs.rmSync(dir, { recursive: true, force: true });
22
- });
23
-
24
- it('null 行与坏行只丢该行,合法 turn 照常提取', () => {
25
- const lines = [
26
- JSON.stringify({ message: { role: 'user', content: 'hello' } }),
27
- 'null', // parse 合法但值为 null,原实现经逐行 catch 跳过
28
- '{"broken',
29
- JSON.stringify({ message: { role: 'assistant', content: [{ type: 'text', text: 'hi' }] } }),
30
- ];
31
- fs.writeFileSync(path.join(dir, 'session-a.jsonl'), lines.join('\n') + '\n', 'utf-8');
32
-
33
- const sessions = readTranscriptSessions(dir);
34
- expect(sessions).toHaveLength(1);
35
- expect(sessions[0].turns.map(t => t.role)).toEqual(['user', 'assistant']);
36
- expect(sessions[0].turns[1].content).toBe('hi');
37
- });
38
-
39
- it('目录不可读返回空数组', () => {
40
- expect(readTranscriptSessions(path.join(dir, 'missing'))).toEqual([]);
41
- });
42
- });
43
-
44
- describe('readTranscriptSessions 过滤(harness#112)', () => {
45
- let dir: string;
46
-
47
- beforeEach(() => {
48
- dir = fs.mkdtempSync(path.join(os.tmpdir(), 'harness-transcript-filter-'));
49
- });
50
-
51
- afterEach(() => {
52
- fs.rmSync(dir, { recursive: true, force: true });
53
- });
54
-
55
- function writeSession(name: string, text: string, mtime?: Date): void {
56
- const p = path.join(dir, name);
57
- fs.writeFileSync(
58
- p,
59
- JSON.stringify({ message: { role: 'user', content: text } }) + '\n',
60
- 'utf-8',
61
- );
62
- if (mtime) fs.utimesSync(p, mtime, mtime);
63
- }
64
-
65
- it('excludeIds:命中的会话不进结果(文件名级过滤,不 parse)', () => {
66
- writeSession('session-a.jsonl', '甲会话内容');
67
- writeSession('session-b.jsonl', '乙会话内容');
68
-
69
- const sessions = readTranscriptSessions(dir, { excludeIds: ['session-a'] });
70
- expect(sessions.map(s => s.id)).toEqual(['session-b']);
71
- });
72
-
73
- it('excludeIds 按截断 40 字符的会话 ID 匹配', () => {
74
- const longName = 'a'.repeat(50);
75
- writeSession(`${longName}.jsonl`, '长名会话内容');
76
- writeSession('session-b.jsonl', '乙会话内容');
77
-
78
- const sessions = readTranscriptSessions(dir, { excludeIds: [longName.slice(0, 40)] });
79
- expect(sessions.map(s => s.id)).toEqual(['session-b']);
80
- });
81
-
82
- it('since:mtimeMs 早于窗口的文件不进结果(stat 级过滤,不 parse)', () => {
83
- writeSession('old.jsonl', '旧会话内容', new Date(Date.now() - 30 * 86_400_000));
84
- writeSession('recent.jsonl', '新会话内容');
85
-
86
- const since = Date.now() - 7 * 86_400_000;
87
- const sessions = readTranscriptSessions(dir, { since });
88
- expect(sessions.map(s => s.id)).toEqual(['recent']);
89
- });
90
-
91
- it('since 边界含等于:mtimeMs == since 仍计入', () => {
92
- const t = new Date('2026-08-01T12:00:00.000Z');
93
- writeSession('edge.jsonl', '边界会话内容', t);
94
-
95
- const sessions = readTranscriptSessions(dir, { since: t.getTime() });
96
- expect(sessions.map(s => s.id)).toEqual(['edge']);
97
- });
98
-
99
- it('since 与 excludeIds 可叠加', () => {
100
- writeSession('old.jsonl', '旧会话内容', new Date(Date.now() - 30 * 86_400_000));
101
- writeSession('excluded.jsonl', '被排除会话');
102
- writeSession('kept.jsonl', '保留会话内容');
103
-
104
- const sessions = readTranscriptSessions(dir, {
105
- since: Date.now() - 7 * 86_400_000,
106
- excludeIds: ['excluded'],
107
- });
108
- expect(sessions.map(s => s.id)).toEqual(['kept']);
109
- });
110
-
111
- it('无 filter:全量读取(向后兼容)', () => {
112
- writeSession('a.jsonl', '甲会话内容', new Date(Date.now() - 30 * 86_400_000));
113
- writeSession('b.jsonl', '乙会话内容');
114
-
115
- const sessions = readTranscriptSessions(dir);
116
- expect(sessions).toHaveLength(2);
117
- });
118
- });
@@ -1,56 +0,0 @@
1
- /**
2
- * 纠正信号模式表与概念清洗(工单 19-C)
3
- *
4
- * 合并 analyze-sessions 与 update-user-model 两份几乎相同的模式表,
5
- * 采用较宽窗口(update-user-model 版本)作为唯一实现。
6
- */
7
-
8
- /** 用户纠正/重复强调语句的匹配模式(两命令合并后的超集,取较宽窗口) */
9
- export const CORRECTION_PATTERNS: RegExp[] = [
10
- /你(?:又|还是)(?:在)?(?:犯|忘|没|不).{0,30}?[了]?/g,
11
- /我不是(?:说|让|让做|讲)[了过]?.{0,50}?[了吗??]?/g,
12
- /怎么(?:又|还|老)(?:是|在)?.{0,30}?[了]?/g,
13
- /我(?:一直|反复|总是)(?:在|说|强调)?.{0,30}?[了]?/g,
14
- /老(?:是|在|犯|忘)(?:了)?.{0,20}?[了]?/g,
15
- /这(?:个|种)(?:问题|模式|错误).{0,20}?[.。!]?/g,
16
- /(?:第[一二三四五六七八九十\d]+次|反复|重复)(?:说|提醒|强调).{0,20}?/g,
17
- /不(?:要|能|想|愿意).{0,20}?老.{0,10}?[了]?/g,
18
- /(?:补上|加上|记上|修复).{0,10}?[了吗??]?/g,
19
- /(?:沉淀|监控|日志|记录).{0,5}?[了吗??]?/g,
20
- ];
21
-
22
- /** 纠正语句前缀(提取概念时剥离) */
23
- const CORRECTION_PREFIX_STRIP: RegExp[] = [
24
- /你(?:又|还是)(?:在)?(?:犯|忘|没|不)/g,
25
- /我不是(?:说|让|让做|讲)[了过]?/g,
26
- /怎么(?:又|还|老)(?:是|在)?/g,
27
- /我(?:一直|反复|总是)(?:在|说|强调)?/g,
28
- /老(?:是|在|犯|忘)(?:了)?/g,
29
- ];
30
-
31
- /**
32
- * 从纠正语句中提取概念:剥离纠正前缀与标点
33
- */
34
- export function cleanCorrectionConcept(sentence: string): string {
35
- let cleaned = sentence;
36
- for (const re of CORRECTION_PREFIX_STRIP) {
37
- re.lastIndex = 0;
38
- cleaned = cleaned.replace(re, '');
39
- }
40
- return cleaned.replace(/[,。!?、;:""''()\s]+/g, '').trim();
41
- }
42
-
43
- /**
44
- * 用模式表在文本中抽取纠正语句(去重)
45
- */
46
- export function extractCorrectionMatches(text: string): string[] {
47
- const matches: string[] = [];
48
- for (const pattern of CORRECTION_PATTERNS) {
49
- pattern.lastIndex = 0;
50
- let match: RegExpExecArray | null;
51
- while ((match = pattern.exec(text)) !== null) {
52
- matches.push(match[0].trim());
53
- }
54
- }
55
- return matches;
56
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * 会话挖掘共享层(工单 19-C)
3
- *
4
- * update-user-model 与 analyze-sessions 两个命令此前各自维护一份
5
- * transcript 解析 / 纠正模式 / 分词与相似度实现,收敛到本目录:
6
- * - transcript.ts Claude Code transcript(.jsonl)读取
7
- * - corrections.ts 纠正信号模式表与概念清洗
8
- * - text.ts 分词 / 停用词 / Jaccard / 文本清洗
9
- */
10
-
11
- export * from './transcript';
12
- export * from './corrections';
13
- export * from './text';