@miphamai/cli 0.85.8 → 0.85.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bin/mipham.ts +6 -4
- package/package.json +1 -1
- package/src/commands/project.ts +39 -7
- package/src/config/loader.ts +262 -71
- package/src/core/crsi-producer.ts +341 -29
- package/src/core/engine.ts +54 -0
- package/src/core/eval-harness.ts +13 -4
- package/src/core/instructions.ts +35 -5
- package/src/core/permission-config.ts +22 -0
- package/src/daemon/auth.ts +50 -3
- package/src/daemon/feishu/adapter.ts +57 -1
- package/src/daemon/server.ts +24 -12
- package/src/index.tsx +27 -1
- package/src/shared/package-info.ts +3 -3
- package/src/telemetry/consent.ts +18 -2
- package/src/tools/index.ts +5 -3
- package/src/tools/system/config.ts +119 -52
- package/src/ui/app.tsx +7 -1
- package/src/ui/commands.ts +49 -0
|
@@ -24,6 +24,16 @@ export const LESSONS_FILE = 'apps/cli/crsi-lessons.md'
|
|
|
24
24
|
/** 组件归因(修复决策,非因果断言):失败最可能被哪个记忆组件的局部干预修复。 */
|
|
25
25
|
export type MemoryComponent = 'experiential' | 'working' | 'invocation' | 'checker'
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* 教训档位的**闭集** —— 只有两档。
|
|
29
|
+
*
|
|
30
|
+
* 与 `CrsiSignal.severity`(自由字符串,来自 insight,可能是 `info`)不同:这是**落进教训文件**
|
|
31
|
+
* 之后的口径。两者之间的桥是 {@link normalizeLessonSeverity},而它只有一个定义 —— 抽取器
|
|
32
|
+
* (读侧)与写入者(写侧)同读它。写入者若自写一份映射,「写进去的 warning」与「读出来的
|
|
33
|
+
* warning」就可能是两个判据(本仓库记过的「一个渲染器,两个读者」)。
|
|
34
|
+
*/
|
|
35
|
+
export type LessonSeverity = 'critical' | 'warning'
|
|
36
|
+
|
|
27
37
|
/** 归一化的教训信号(insight 与 meta-rule 的公共面)。 */
|
|
28
38
|
export interface CrsiSignal {
|
|
29
39
|
category: string
|
|
@@ -76,10 +86,19 @@ export function selectCrsiSignal(
|
|
|
76
86
|
return null
|
|
77
87
|
}
|
|
78
88
|
|
|
79
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* 模板化地把信号渲染成一段教训 markdown(不动 LLM)。
|
|
91
|
+
*
|
|
92
|
+
* `severity` 是**必填参数**,且是档位的**唯一来源**(不再读 `signal.severity`)。
|
|
93
|
+
* 此前它读一个可选字段、缺了就**整行不打** —— 而那条缺席的后果不是「没有标签」,
|
|
94
|
+
* 是抽取器 fail-open 落 `critical`(常驻、每次请求都占字符)。于是「生产者忘了给档位」
|
|
95
|
+
* 这件事在外部读数上与「它本来就该常驻」**同形**,没有任何东西能报。
|
|
96
|
+
* 改成必填后,「忘了」是编译错误 —— 同 `produceProseProposal` 的 `lessonsBlock` 必填第 5 参。
|
|
97
|
+
*/
|
|
80
98
|
export function buildLessonContent(
|
|
81
99
|
signal: CrsiSignal,
|
|
82
100
|
timestamp: string,
|
|
101
|
+
severity: LessonSeverity,
|
|
83
102
|
source = 'CRSI producer (autoApplicable)',
|
|
84
103
|
): string {
|
|
85
104
|
const lines: string[] = [
|
|
@@ -87,8 +106,8 @@ export function buildLessonContent(
|
|
|
87
106
|
'',
|
|
88
107
|
`- 建议: ${signal.suggestion}`,
|
|
89
108
|
`- 组件: ${signal.component ?? 'experiential'}`,
|
|
109
|
+
`- 严重度: ${severity}`,
|
|
90
110
|
]
|
|
91
|
-
if (signal.severity) lines.push(`- 严重度: ${signal.severity}`)
|
|
92
111
|
lines.push(`- 生成时间: ${timestamp}`, `- 来源: ${source}`, '', '### 证据')
|
|
93
112
|
for (const e of signal.evidence) lines.push(`- ${e}`)
|
|
94
113
|
lines.push('')
|
|
@@ -99,14 +118,15 @@ export function buildLessonContent(
|
|
|
99
118
|
* 教训精华(标题 + 建议 + 严重度),用于运行时召回注入系统提示。
|
|
100
119
|
*
|
|
101
120
|
* 严重度决定**注入位置**,不只是标签:`critical` 常驻每一次请求,
|
|
102
|
-
* `warning` 移出常驻块、按需召回(见 {@link isAlwaysOnLesson}
|
|
121
|
+
* `warning` 移出常驻块、按需召回(见 {@link isAlwaysOnLesson}),且常驻档整体受
|
|
122
|
+
* {@link RESIDENT_LESSONS_BUDGET} 约束(见 {@link selectResidentLessons})。
|
|
103
123
|
* 这两个数是**当时的读数、不是不变量**(2026-09-28 实测:39 条全量 11,743 字符,
|
|
104
124
|
* 占 40k 指令预算 29.2%;分档后 6 条常驻)。教训加减后它们就作废,别当断言读。
|
|
105
125
|
*/
|
|
106
126
|
export interface CrsiLessonSummary {
|
|
107
127
|
title: string
|
|
108
128
|
suggestion: string
|
|
109
|
-
severity:
|
|
129
|
+
severity: LessonSeverity
|
|
110
130
|
}
|
|
111
131
|
|
|
112
132
|
/**
|
|
@@ -117,7 +137,20 @@ export interface CrsiLessonSummary {
|
|
|
117
137
|
* 定义在**一处**:抽取器与 `flush()` 都读它,改一处即改全。
|
|
118
138
|
* 不导出 —— 它没有第二个读者,导出只会让「谁在用」这个问题多一个假答案。
|
|
119
139
|
*/
|
|
120
|
-
const DEFAULT_LESSON_SEVERITY:
|
|
140
|
+
const DEFAULT_LESSON_SEVERITY: LessonSeverity = 'critical'
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 把任意档位取归一化到闭集:只有**逐字**的 `warning` 是 `warning`,其余一切
|
|
144
|
+
* (含缺席、空串、闭集外取值)落 `critical`。
|
|
145
|
+
*
|
|
146
|
+
* 方向是刻意的:不倒向「按需」。宁可多花字符,也不把一条守卫静默降级成「按需读」——
|
|
147
|
+
* 那正是「只写不读」缺口的复发形态。副作用要认下:`info` 也会落 `critical`(它不在闭集里)。
|
|
148
|
+
*
|
|
149
|
+
* **定义在一处**:抽取器(读侧)与两个写入者(`produceCrsiProposal` / crossover)同读它。
|
|
150
|
+
*/
|
|
151
|
+
export function normalizeLessonSeverity(raw: string | undefined): LessonSeverity {
|
|
152
|
+
return raw === 'warning' ? 'warning' : 'critical'
|
|
153
|
+
}
|
|
121
154
|
|
|
122
155
|
/**
|
|
123
156
|
* 从 crsi-lessons.md 提取每条教训的「精华」(标题 + 建议 + 严重度),跳过证据段落。
|
|
@@ -155,46 +188,256 @@ export function extractCrsiLessonSummaries(content: string): CrsiLessonSummary[]
|
|
|
155
188
|
}
|
|
156
189
|
const v = line.match(/^-\s*严重度[::]\s*(.+?)\s*$/)
|
|
157
190
|
if (v) {
|
|
158
|
-
// 闭集外的取值一律落常驻档 ——
|
|
159
|
-
|
|
191
|
+
// 闭集外的取值一律落常驻档 —— 未知严重度不倒向「按需」。规则在 normalizeLessonSeverity
|
|
192
|
+
// 一处;写入者同读它(正则已吃掉两侧空白,故传进去的是已经 trim 过的捕获)。
|
|
193
|
+
severity = normalizeLessonSeverity(v[1])
|
|
160
194
|
}
|
|
161
195
|
}
|
|
162
196
|
flush()
|
|
163
197
|
return out
|
|
164
198
|
}
|
|
165
199
|
|
|
166
|
-
/**
|
|
200
|
+
/**
|
|
201
|
+
* 这条教训是否**有资格**常驻(非 `warning` 的一切,含缺省,都有资格)。
|
|
202
|
+
*
|
|
203
|
+
* 这是**逐条**谓词,只说「档位」,不说「进不进得去」—— 进去还要过预算,
|
|
204
|
+
* 见 {@link selectResidentLessons}。两个问题分开是因为两个问题的答案不同:
|
|
205
|
+
* 全是 critical 时这条谓词全真,而预算仍然会拦下一部分。
|
|
206
|
+
*/
|
|
167
207
|
export function isAlwaysOnLesson(summary: CrsiLessonSummary): boolean {
|
|
168
208
|
return summary.severity !== 'warning'
|
|
169
209
|
}
|
|
170
210
|
|
|
171
|
-
/**
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
211
|
+
/**
|
|
212
|
+
* 常驻块的字符预算,按**渲染后**的文本量算。
|
|
213
|
+
*
|
|
214
|
+
* 为什么用字符而不是条数:预算约束的是**每次请求的注入成本**,而单条教训的
|
|
215
|
+
* 长度差三倍以上(2026-09-28 实测 157–570 字符)—— 条数相同,成本可以差一倍。
|
|
216
|
+
*
|
|
217
|
+
* 3,000 ≈ 40k 指令预算的 7.5%(当时 6 条 critical 共 2,155 字符,还有余量)。
|
|
218
|
+
*/
|
|
219
|
+
export const RESIDENT_LESSONS_BUDGET = 3000
|
|
220
|
+
|
|
221
|
+
/** {@link selectResidentLessons} 的结果。每个字段各有各的读者,不互相重算。 */
|
|
222
|
+
export interface ResidentLessonSelection {
|
|
223
|
+
/** 进常驻块的那些,**文件序** —— 渲染序稳定,不随择优顺序抖动。 */
|
|
224
|
+
resident: CrsiLessonSummary[]
|
|
225
|
+
/** 未常驻的全部(全部 warning + 被预算挤出的 critical),文件序:指针报的是它的条数。 */
|
|
226
|
+
demoted: CrsiLessonSummary[]
|
|
227
|
+
/** `demoted` 里「因预算被挤出」的那部分,文件序:指针点名的是它。 */
|
|
228
|
+
overBudget: CrsiLessonSummary[]
|
|
229
|
+
/**
|
|
230
|
+
* 这次选择**实际用的**预算。
|
|
231
|
+
*
|
|
232
|
+
* 由结果携带而不是让指针去读常量:常量是「默认值」,不一定是「这一次的值」。
|
|
233
|
+
* 指针报一个与实际不符的数,就是又一个「报告描述的不是发出去的那份」。
|
|
234
|
+
*/
|
|
235
|
+
budget: number
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 选出真正进常驻块的那些 —— **唯一的择优点**,两个读者都必须走这里。
|
|
240
|
+
*
|
|
241
|
+
* 为什么要有预算:`severity` 是**逐条**的谓词,预算是**整档**的性质。两者错配的
|
|
242
|
+
* 后果是没有东西能说「常驻档太大了」—— 全是 critical 时,每加一条教训就是每次
|
|
243
|
+
* 请求都多付一份钱,且没有上限。
|
|
244
|
+
*
|
|
245
|
+
* 超预算的 critical **不丢弃**:它们落进 `demoted`,由指针点名。无声消失与
|
|
246
|
+
* 「从来没写过这条教训」在外部读数上同形。
|
|
247
|
+
*
|
|
248
|
+
* 择优方向是**文件倒序**(新的优先留下):文件是追加写的 ⇒ 越靠后越新,而越近的
|
|
249
|
+
* 失败越相关。选中后仍按文件序渲染,避免常驻块的顺序随预算抖动。
|
|
250
|
+
*
|
|
251
|
+
* 单条自身就超预算时它自己出局(在指针里被点名):预算约束的是**总量**,最长的
|
|
252
|
+
* 单条没有豁免权;截断正文会篡改建议本身,所以不做。
|
|
253
|
+
*/
|
|
254
|
+
export function selectResidentLessons(
|
|
255
|
+
summaries: CrsiLessonSummary[],
|
|
256
|
+
budget: number = RESIDENT_LESSONS_BUDGET,
|
|
257
|
+
): ResidentLessonSelection {
|
|
258
|
+
const criticals = summaries.filter(isAlwaysOnLesson)
|
|
259
|
+
let resident: CrsiLessonSummary[] = []
|
|
260
|
+
const overBudget: CrsiLessonSummary[] = []
|
|
261
|
+
|
|
262
|
+
for (let i = criticals.length - 1; i >= 0; i--) {
|
|
263
|
+
const lesson = criticals[i]!
|
|
264
|
+
const candidate = [lesson, ...resident]
|
|
265
|
+
if (buildCrsiLessonsBlock(candidate).length <= budget) {
|
|
266
|
+
resident = candidate
|
|
267
|
+
} else {
|
|
268
|
+
overBudget.unshift(lesson)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
resident,
|
|
274
|
+
demoted: summaries.filter((s) => !resident.includes(s)),
|
|
275
|
+
overBudget,
|
|
276
|
+
budget,
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 常驻块的表头(含与正文之间的那个空行),与 {@link buildCrsiLessonsBlock} **共用**。
|
|
282
|
+
*
|
|
283
|
+
* 抽出来是因为常驻档名册要报**逐条**成本,而「逐条和 ≠ 合计」的差额正是这块表头 ——
|
|
284
|
+
* 各写一份,两处迟早对不上,且对不上的时候没人知道该信哪个。
|
|
285
|
+
*/
|
|
286
|
+
const CRSI_LESSONS_HEADER = `## CRSI Lessons (Self-Improvement Recall)
|
|
176
287
|
|
|
177
288
|
These are hard-won lessons consolidated by the CRSI self-improvement
|
|
178
289
|
loop from past sessions. Apply them proactively — do not repeat these
|
|
179
290
|
mistakes:
|
|
180
291
|
|
|
181
|
-
|
|
292
|
+
`
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 一条教训在块里的**逐字**形状,与 {@link buildCrsiLessonsBlock} 共用。
|
|
296
|
+
*
|
|
297
|
+
* 序号参与渲染 ⇒ 同一个 `summary` 在不同位置长度不同;常驻档名册按文件序渲染,
|
|
298
|
+
* 与块里看到的序号一致。
|
|
299
|
+
*/
|
|
300
|
+
const renderLessonItem = (s: CrsiLessonSummary, i: number): string =>
|
|
301
|
+
`${i + 1}. **${s.title}**\n ${s.suggestion}`
|
|
302
|
+
|
|
303
|
+
/** 逐条之间的分隔符 —— `.join('\n\n')` 的那个 `\n\n`。名册的加法算式要用它的长度。 */
|
|
304
|
+
const LESSON_ITEM_SEPARATOR = '\n\n'
|
|
305
|
+
|
|
306
|
+
/** 把教训精华渲染为系统提示召回块。无教训时返回空串。 */
|
|
307
|
+
export function buildCrsiLessonsBlock(summaries: CrsiLessonSummary[]): string {
|
|
308
|
+
if (summaries.length === 0) return ''
|
|
309
|
+
const items = summaries.map(renderLessonItem).join(LESSON_ITEM_SEPARATOR)
|
|
310
|
+
return `${CRSI_LESSONS_HEADER}${items}`
|
|
182
311
|
}
|
|
183
312
|
|
|
313
|
+
/** 指针最多点名几条被挤出的 critical —— 否则指针自己成了新的无界常驻成本。 */
|
|
314
|
+
const POINTER_NAME_LIMIT = 3
|
|
315
|
+
|
|
184
316
|
/**
|
|
185
|
-
*
|
|
317
|
+
* 未常驻教训的指针行,由 {@link selectResidentLessons} 的结果渲染。全部常驻时返回空串。
|
|
186
318
|
*
|
|
187
319
|
* 指针是**召回触发点**:没有它,移出常驻块就等于把教训变成只写不读。
|
|
188
|
-
*
|
|
320
|
+
* 它不重复正文,只报条数、点名被预算挤出的 critical、给出文件路径 —— 模型用已有的
|
|
321
|
+
* Read/Grep 工具自取(那份文件的路径就是 `lessonsPath`)。
|
|
322
|
+
*
|
|
323
|
+
* 为什么必须把「被预算挤出」与「本来就是 warning」分开说:对读者而言这是两件事 ——
|
|
324
|
+
* 前者是**预算问题**(有杠杆可拉:给别的降档、或把这条缩短),后者是**设计如此**。
|
|
325
|
+
* 混成一句,前者看上去就是后者,问题永远不会有人发现。
|
|
189
326
|
*/
|
|
190
327
|
export function buildCrsiLessonsPointer(
|
|
191
|
-
|
|
328
|
+
selection: ResidentLessonSelection,
|
|
192
329
|
lessonsPath: string,
|
|
193
330
|
): string {
|
|
194
|
-
const
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
|
|
331
|
+
const { demoted, overBudget, budget } = selection
|
|
332
|
+
if (demoted.length === 0) return ''
|
|
333
|
+
|
|
334
|
+
const lines = [`另有 ${demoted.length} 条教训未常驻。`]
|
|
335
|
+
|
|
336
|
+
if (overBudget.length > 0) {
|
|
337
|
+
const named = overBudget.slice(0, POINTER_NAME_LIMIT).map((s) => s.title)
|
|
338
|
+
const tail = overBudget.length > named.length ? ` 等 ${overBudget.length} 条` : ''
|
|
339
|
+
lines.push(
|
|
340
|
+
`其中 ${overBudget.length} 条 critical 因常驻档预算(${budget} 字符)` +
|
|
341
|
+
`被挤出:${named.join('、')}${tail}。`,
|
|
342
|
+
)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
lines.push(`需要时读 ${lessonsPath}(含标题/建议/证据)。`)
|
|
346
|
+
return lines.join('\n')
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* 事件型召回扳机的正文,由 {@link selectResidentLessons} 的结果渲染。无可召回时返回空串。
|
|
351
|
+
*
|
|
352
|
+
* 与 {@link buildCrsiLessonsPointer} **共用同一个判据**(`demoted.length === 0`):
|
|
353
|
+
* 没有指针就没有扳机。指针说的是「东西在那儿」,扳机说的是「现在是看它的时机」——
|
|
354
|
+
* 后者只能挂在**事件**上(工具刚失败),挂在系统提示里就是在复述指针本身。
|
|
355
|
+
*
|
|
356
|
+
* 措辞是**非指令式**的:不点名、不排序、不读、不摘要任何一条教训,也不替代理决定
|
|
357
|
+
* 读不读。代理可以完全忽略它,代价只是这一条上下文。
|
|
358
|
+
*/
|
|
359
|
+
export function buildLessonRevisitNudge(
|
|
360
|
+
selection: ResidentLessonSelection,
|
|
361
|
+
lessonsPath: string,
|
|
362
|
+
): string {
|
|
363
|
+
if (selection.demoted.length === 0) return ''
|
|
364
|
+
return (
|
|
365
|
+
`[可回顾] 上一步工具调用失败。若有教训覆盖同一情形,可自行决定是否读 ${lessonsPath}` +
|
|
366
|
+
`(含标题/建议/证据);不作要求,也可以完全忽略。`
|
|
367
|
+
)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* 常驻档名册 —— `selectResidentLessons` 的**第三个读者**,也是**唯一给人看的**那一个。
|
|
372
|
+
*
|
|
373
|
+
* 前两个读者的输出都进了**模型**的上下文(系统提示、`--prose` 算子)。而常驻档的成本
|
|
374
|
+
* 是**人**在付:每一条 `critical` 都随每次请求发出去,永远。能动手的也只有人 —— 把某条
|
|
375
|
+
* 改成 `- 严重度: warning`、或把最长的那条缩短。在此之前,「这档涨得值不值」这件事
|
|
376
|
+
* **没有任何材料可查**:预算只是**上限**,不是**理由**;而启动期那条体积告警只在指令
|
|
377
|
+
* **总额**超 40k 时说话、且只点名最大的三个文件 —— 教训块那两千多字符排不进去,等于不报。
|
|
378
|
+
*
|
|
379
|
+
* 与两个既有读者的关系:**共用** {@link selectResidentLessons}(唯一的择优点)⇒
|
|
380
|
+
* 报告不可能与真正注入的那一份漂移。逐条成本按**渲染后的文本量**算,且报出可验算的
|
|
381
|
+
* 加法(表头 + 逐条 + 分隔 == 合计)—— 否则「逐条和 ≠ 合计」会被当成 bug 查一遍。
|
|
382
|
+
*
|
|
383
|
+
* 只读:**不加写路径**。人的杠杆是改文件那一行;写入侧是生产者与 `/crsi propose`。
|
|
384
|
+
*/
|
|
385
|
+
export function buildResidentLessonReport(
|
|
386
|
+
selection: ResidentLessonSelection,
|
|
387
|
+
lessonsPath: string,
|
|
388
|
+
): string {
|
|
389
|
+
const { resident, demoted, overBudget, budget } = selection
|
|
390
|
+
const num = (n: number) => n.toLocaleString('en-US')
|
|
391
|
+
|
|
392
|
+
if (resident.length === 0 && demoted.length === 0) {
|
|
393
|
+
return `## CRSI 常驻教训(每次请求都注入)\n\n教训文件里没有可召回的教训:${lessonsPath}`
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const itemChars = resident.map((s, i) => renderLessonItem(s, i).length)
|
|
397
|
+
const items = itemChars.reduce((a, b) => a + b, 0)
|
|
398
|
+
const seps = LESSON_ITEM_SEPARATOR.length * Math.max(0, resident.length - 1)
|
|
399
|
+
const header = resident.length > 0 ? CRSI_LESSONS_HEADER.length : 0
|
|
400
|
+
const total = header + items + seps
|
|
401
|
+
const block = buildCrsiLessonsBlock(resident).length
|
|
402
|
+
const headroom = budget - total
|
|
403
|
+
const used = budget > 0 ? ((total / budget) * 100).toFixed(1) : '—'
|
|
404
|
+
|
|
405
|
+
const lines: string[] = ['## CRSI 常驻教训(每次请求都注入)', '']
|
|
406
|
+
lines.push(
|
|
407
|
+
`常驻 **${resident.length}** 条 / ${num(total)} 字符 · 预算 ${num(budget)}` +
|
|
408
|
+
`(余量 ${num(headroom)},已用 ${used}%)`,
|
|
409
|
+
)
|
|
410
|
+
lines.push(
|
|
411
|
+
`未常驻 **${demoted.length}** 条` +
|
|
412
|
+
(overBudget.length > 0
|
|
413
|
+
? `(其中 **${overBudget.length}** 条因预算被挤出:${overBudget.map((s) => s.title).join('、')})`
|
|
414
|
+
: '(无因预算被挤出的)'),
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
if (resident.length > 0) {
|
|
418
|
+
lines.push('', '### 逐条成本(渲染后字符)', '')
|
|
419
|
+
for (let i = 0; i < resident.length; i++) {
|
|
420
|
+
lines.push(`- ${num(itemChars[i]!)} 字符 — ${resident[i]!.title}`)
|
|
421
|
+
}
|
|
422
|
+
// 加法自证:表头是每条共担的,逐条和必然大于合计 —— 与其让人去猜差额,
|
|
423
|
+
// 不如把算式写出来,且当场验算(`block` 走的是真渲染器,不是我在这里的手算)。
|
|
424
|
+
lines.push(
|
|
425
|
+
'',
|
|
426
|
+
`合计 ${num(total)} = 表头 ${num(header)} + 逐条 ${num(items)} + 分隔 ${num(seps)}` +
|
|
427
|
+
`(${LESSON_ITEM_SEPARATOR.length} × ${Math.max(0, resident.length - 1)})` +
|
|
428
|
+
` — 与渲染器实测${block === total ? '相符' : `**不符(${num(block)})**`}`,
|
|
429
|
+
)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
lines.push(
|
|
433
|
+
'',
|
|
434
|
+
`指针另有 ${num(buildCrsiLessonsPointer(selection, lessonsPath).length)} 字符,**不含在预算内**` +
|
|
435
|
+
`(预算量的是常驻块本身)。`,
|
|
436
|
+
'',
|
|
437
|
+
`文件:${lessonsPath}`,
|
|
438
|
+
'把某条移出常驻档 = 把它的 `- 严重度:` 改成 `warning`。',
|
|
439
|
+
)
|
|
440
|
+
return lines.join('\n')
|
|
198
441
|
}
|
|
199
442
|
|
|
200
443
|
/**
|
|
@@ -204,13 +447,17 @@ export function buildCrsiLessonsPointer(
|
|
|
204
447
|
* 指针要求读者用 Read/Grep 自取,而生成算子是无工具的 `llm.chat` 单条消息 ——
|
|
205
448
|
* 对它而言指针等于零,只能内联或什么都不给。
|
|
206
449
|
*
|
|
450
|
+
* 择点必须与那份投影**共用** `selectResidentLessons`:两份投影各选各的,
|
|
451
|
+
* 就会出现「主代理看到的常驻集」与「算子看到的常驻集」不是同一个 ——
|
|
452
|
+
* 局部正确、全局遗漏。
|
|
453
|
+
*
|
|
207
454
|
* 文件缺席 ⇒ 空串:算子退回「无教训」的旧形状(纯增量,不改既有行为)。
|
|
208
455
|
*/
|
|
209
456
|
export function loadAlwaysOnLessonsBlock(lessonsPath: string): string {
|
|
210
457
|
if (!existsSync(lessonsPath)) return ''
|
|
211
458
|
try {
|
|
212
459
|
const summaries = extractCrsiLessonSummaries(readFileSync(lessonsPath, 'utf-8'))
|
|
213
|
-
return buildCrsiLessonsBlock(summaries.
|
|
460
|
+
return buildCrsiLessonsBlock(selectResidentLessons(summaries).resident)
|
|
214
461
|
} catch {
|
|
215
462
|
return ''
|
|
216
463
|
}
|
|
@@ -235,7 +482,9 @@ export function produceCrsiProposal(
|
|
|
235
482
|
// 幂等:同一信号的教训标题已在文件中,不再重复产出。
|
|
236
483
|
if (currentLessons.includes(`## ${signal.category}: ${signal.title}`)) return null
|
|
237
484
|
|
|
238
|
-
|
|
485
|
+
// 档位显式取归一化后的值:`signal.severity` 来自 insight(可能是 `info` 或缺席),
|
|
486
|
+
// 它**不是**闭集口径。不给这个参数是编译错误 —— 这就是「忘了给档位」的落点。
|
|
487
|
+
const lesson = buildLessonContent(signal, timestamp, normalizeLessonSeverity(signal.severity))
|
|
239
488
|
const newContent = currentLessons ? `${currentLessons.trimEnd()}\n\n${lesson}\n` : `${lesson}\n`
|
|
240
489
|
|
|
241
490
|
return {
|
|
@@ -365,7 +614,7 @@ export function produceRuleProposal(
|
|
|
365
614
|
// ── Producer 散文提议(块 1):从失败信号生成「改 skill 散文」提议 ──
|
|
366
615
|
// A1 边界首次实演:LLM 只作「生成」(候选),判定仍走确定性(guard 预筛 / 行为效果 / 人审)。
|
|
367
616
|
|
|
368
|
-
const PROSE_SELECT_PROMPT_VERSION = '1.
|
|
617
|
+
const PROSE_SELECT_PROMPT_VERSION = '1.1.0'
|
|
369
618
|
|
|
370
619
|
function buildSelectSkillPrompt(signal: CrsiSignal, skillFiles: string[]): string {
|
|
371
620
|
return [
|
|
@@ -374,15 +623,16 @@ function buildSelectSkillPrompt(signal: CrsiSignal, skillFiles: string[]): strin
|
|
|
374
623
|
'失败信号:',
|
|
375
624
|
`- category: ${signal.category}`,
|
|
376
625
|
`- title: ${signal.title}`,
|
|
377
|
-
|
|
626
|
+
// 缺席(空串)时不占行 —— 与 `buildGenerateProsePrompt` 同一个写法。
|
|
627
|
+
// **不能靠 `.filter(Boolean)` 做这件事**:它分不清「本该丢的条件空串」与「上面那几个
|
|
628
|
+
// 故意的段落分隔符」,于是把三段式结构一并吃掉(实测本函数的提示词里 `\n\n` 一次都没有)。
|
|
629
|
+
...(signal.severity ? [`- severity: ${signal.severity}`] : []),
|
|
378
630
|
`- suggestion: ${signal.suggestion}`,
|
|
379
631
|
`- evidence: ${signal.evidence.join(' | ')}`,
|
|
380
632
|
'',
|
|
381
633
|
'候选 skill 文件:',
|
|
382
634
|
...skillFiles.map((f) => `- ${f}`),
|
|
383
|
-
]
|
|
384
|
-
.filter(Boolean)
|
|
385
|
-
.join('\n')
|
|
635
|
+
].join('\n')
|
|
386
636
|
}
|
|
387
637
|
|
|
388
638
|
async function collectLlmText(llm: Llm, prompt: string): Promise<string> {
|
|
@@ -489,6 +739,28 @@ function countLines(s: string): number {
|
|
|
489
739
|
return s.endsWith('\n') ? breaks : breaks + 1
|
|
490
740
|
}
|
|
491
741
|
|
|
742
|
+
/**
|
|
743
|
+
* 档位迁移(**只呈现、不判定**)—— 与 `formatNetChange` / `formatCostLine` / 事前风险同一纪律。
|
|
744
|
+
*
|
|
745
|
+
* 报的是**实际写进去的**档位(第 3 参 `merged`),不是按 `deriveMergedSeverity` 应得的那个:
|
|
746
|
+
* 报告要报实际用的那个数,否则读数描述的是规则、不是这次渲染(同 `ResidentLessonSelection.budget`)。
|
|
747
|
+
*
|
|
748
|
+
* **恒打**(含无变化),同 `formatNetChange` 的理由:「这次没升档」正是那条可证伪的基线读数,
|
|
749
|
+
* 只打有变化的等于让读者看不见基线。两个源与产物都已知 ⇒ 语法上不存在「打不出来」的情况。
|
|
750
|
+
*
|
|
751
|
+
* `⬆` 的判据是**相对更宽的那个源**(合并把地板抬起来了吗)—— 因为那才是成本事件:
|
|
752
|
+
* 一条非常驻的教训经合并变成常驻的,占预算。收尾不带换行:回执自己加(否则会与前一行粘连)。
|
|
753
|
+
*/
|
|
754
|
+
export function formatSeverityShift(
|
|
755
|
+
a: LessonSeverity,
|
|
756
|
+
b: LessonSeverity,
|
|
757
|
+
merged: LessonSeverity,
|
|
758
|
+
): string {
|
|
759
|
+
const floor: LessonSeverity = a === 'warning' || b === 'warning' ? 'warning' : 'critical'
|
|
760
|
+
const arrow = merged === 'critical' && floor === 'warning' ? '⬆' : '='
|
|
761
|
+
return `${arrow} 档位(按更严来源派生): ${a} + ${b} → ${merged}`
|
|
762
|
+
}
|
|
763
|
+
|
|
492
764
|
/**
|
|
493
765
|
* 解析 prose 响应:可选的一行 JSON 前缀(ε)+ 正文。
|
|
494
766
|
*
|
|
@@ -753,9 +1025,28 @@ export function removeLessonSections(content: string, headers: string[]): string
|
|
|
753
1025
|
return out.join('\n')
|
|
754
1026
|
}
|
|
755
1027
|
|
|
1028
|
+
/**
|
|
1029
|
+
* 合并产物的档位:取两个源中**更严**的那个。
|
|
1030
|
+
*
|
|
1031
|
+
* 三条理由,每条都取自本仓库既有的决定而非新偏好:
|
|
1032
|
+
* 1. **不问 LLM** —— 档位是「这条要不要每次请求都在场」的判定,属 A1 铁律的判定侧,
|
|
1033
|
+
* 只能由确定性规则给(LLM 只生成)⇒ 不可伪造、不可幻觉。
|
|
1034
|
+
* 2. **不取更宽** —— 那会把一个 critical 经合并洗成按需,正是抽取器注释里
|
|
1035
|
+
* 「未知严重度不倒向『按需』」拒绝的方向。
|
|
1036
|
+
* 3. **不停在「缺省 critical」** —— 那个缺省是给**人类新写**的条目兜底的;合并是**降维**,
|
|
1037
|
+
* 没有依据说产物比两个源更紧急。实测(真模块):两条 warning 合并曾升进常驻档,
|
|
1038
|
+
* 常驻 6 条 2,155 字符 → 7 条 2,198,而这一切发生在**没有人决定过**的情况下。
|
|
1039
|
+
*/
|
|
1040
|
+
export function deriveMergedSeverity(a: LessonSeverity, b: LessonSeverity): LessonSeverity {
|
|
1041
|
+
return a === 'critical' || b === 'critical' ? 'critical' : 'warning'
|
|
1042
|
+
}
|
|
1043
|
+
|
|
756
1044
|
/**
|
|
757
1045
|
* Crossover:合并两条重叠教训 → 「删二增一」的教训文件变更候选。
|
|
758
1046
|
* LLM 只生成(选对 + 合并版),guard 校验所选教训真实存在(fail-closed 防幻觉)。
|
|
1047
|
+
*
|
|
1048
|
+
* 档位**不在 LLM 的契约里**(提示词只列举 category/title/suggestion/evidence),
|
|
1049
|
+
* 由 `deriveMergedSeverity` 从两个源确定性派生 —— 见该函数的理由。
|
|
759
1050
|
*/
|
|
760
1051
|
export async function produceCrossoverProposal(
|
|
761
1052
|
llm: Llm,
|
|
@@ -768,6 +1059,10 @@ export async function produceCrossoverProposal(
|
|
|
768
1059
|
originalContent: string
|
|
769
1060
|
blastRadius: string[]
|
|
770
1061
|
merge: boolean
|
|
1062
|
+
/** 两个源的档位(按 LLM 选定的顺序)与实际写进产物的档位,供回执呈现。 */
|
|
1063
|
+
severityA: LessonSeverity
|
|
1064
|
+
severityB: LessonSeverity
|
|
1065
|
+
mergedSeverity: LessonSeverity
|
|
771
1066
|
} | null> {
|
|
772
1067
|
const response = await collectLlmText(llm, buildCrossoverPrompt(currentLessons))
|
|
773
1068
|
if (!response) return null
|
|
@@ -783,8 +1078,22 @@ export async function produceCrossoverProposal(
|
|
|
783
1078
|
const lessonLines = currentLessons.split('\n').map((l) => l.trim())
|
|
784
1079
|
if (!lessonLines.includes(headerA) || !lessonLines.includes(headerB)) return null
|
|
785
1080
|
|
|
1081
|
+
// 档位走**读侧同一个抽取器**(不另写一个解析器):`- 严重度:` 写在 `- 建议:` 之后,
|
|
1082
|
+
// 且「有标题没建议」的段抽取器不产出 ⇒ 查不到即 fail-closed 落 critical(同上面第 3 条方向)。
|
|
1083
|
+
const summaries = extractCrsiLessonSummaries(currentLessons)
|
|
1084
|
+
const severityOf = (header: string): LessonSeverity =>
|
|
1085
|
+
normalizeLessonSeverity(summaries.find((s) => `## ${s.title}` === header)?.severity)
|
|
1086
|
+
const severityA = severityOf(headerA)
|
|
1087
|
+
const severityB = severityOf(headerB)
|
|
1088
|
+
const mergedSeverity = deriveMergedSeverity(severityA, severityB)
|
|
1089
|
+
|
|
786
1090
|
const withoutTwo = removeLessonSections(currentLessons, [headerA, headerB])
|
|
787
|
-
const mergedSection = buildLessonContent(
|
|
1091
|
+
const mergedSection = buildLessonContent(
|
|
1092
|
+
parsed.merged,
|
|
1093
|
+
timestamp,
|
|
1094
|
+
mergedSeverity,
|
|
1095
|
+
'CRSI producer (crossover)',
|
|
1096
|
+
)
|
|
788
1097
|
const newContent = `${withoutTwo.trimEnd()}\n\n${mergedSection}\n`
|
|
789
1098
|
|
|
790
1099
|
return {
|
|
@@ -794,5 +1103,8 @@ export async function produceCrossoverProposal(
|
|
|
794
1103
|
originalContent: currentLessons,
|
|
795
1104
|
blastRadius: [LESSONS_FILE],
|
|
796
1105
|
merge: true,
|
|
1106
|
+
severityA,
|
|
1107
|
+
severityB,
|
|
1108
|
+
mergedSeverity,
|
|
797
1109
|
}
|
|
798
1110
|
}
|
package/src/core/engine.ts
CHANGED
|
@@ -23,6 +23,7 @@ import type { AgentViewManager } from '../agent-view/agent-view-manager'
|
|
|
23
23
|
import type { Skills } from '../skills/seam'
|
|
24
24
|
import { getBackgroundAgentRegistry } from '../agent/background-registry'
|
|
25
25
|
import { RulesLoader } from './rules-loader'
|
|
26
|
+
import type { InstructionsLoader } from './instructions'
|
|
26
27
|
import { ExperienceRuleEngine } from './rule-engine.js'
|
|
27
28
|
import { PatternAnalyzer } from '../agent/pattern-analyzer.js'
|
|
28
29
|
import { EffectivenessTracker } from '../agent/effectiveness-tracker.js'
|
|
@@ -359,8 +360,14 @@ export class QueryEngine {
|
|
|
359
360
|
|
|
360
361
|
/** Rules loader for path-scoped rules injection. */
|
|
361
362
|
private rulesLoader?: RulesLoader
|
|
363
|
+
/** Instruction loader — the source of the CRSI lessons *recall nudge* (see injectLessonNudge). */
|
|
364
|
+
private instructionsLoader?: InstructionsLoader
|
|
362
365
|
/** Files touched in the current turn (for rules matching). */
|
|
363
366
|
private touchedFiles: Set<string> = new Set()
|
|
367
|
+
/** A tool call failed since the last nudge check. */
|
|
368
|
+
private pendingToolFailure = false
|
|
369
|
+
/** The recall nudge is offered at most once per engine (session). */
|
|
370
|
+
private lessonNudgeOffered = false
|
|
364
371
|
private usageTracker = new UsageTracker()
|
|
365
372
|
/** Inference hook (DLP) configuration. */
|
|
366
373
|
private inferenceHookConfig?: InferenceHookConfig
|
|
@@ -373,6 +380,17 @@ export class QueryEngine {
|
|
|
373
380
|
this.rulesLoader.load()
|
|
374
381
|
}
|
|
375
382
|
|
|
383
|
+
/**
|
|
384
|
+
* Register the instruction loader — the recall nudge's text source.
|
|
385
|
+
*
|
|
386
|
+
* daemon 侧有意不接:daemon **从不设置系统提示**(`setSystemPrompt` 在 `daemon/`
|
|
387
|
+
* 下零调用点),因此那条路径上既没有常驻块、也没有指针 —— 接上去只会让扳机指向
|
|
388
|
+
* 一个该会话从未被告知过的文件。见守卫的 `daemon:setInstructions` 豁免条目。
|
|
389
|
+
*/
|
|
390
|
+
setInstructions(loader: InstructionsLoader): void {
|
|
391
|
+
this.instructionsLoader = loader
|
|
392
|
+
}
|
|
393
|
+
|
|
376
394
|
/** Register inference hook (DLP) configuration. */
|
|
377
395
|
setInferenceHookConfig(config: InferenceHookConfig): void {
|
|
378
396
|
this.inferenceHookConfig = config
|
|
@@ -406,6 +424,31 @@ export class QueryEngine {
|
|
|
406
424
|
this.touchedFiles.clear()
|
|
407
425
|
}
|
|
408
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Offer the CRSI lessons **recall nudge** after a failed tool call.
|
|
429
|
+
*
|
|
430
|
+
* 为什么落点是「工具失败」而不是更早设想的会话开始/压缩后:那两处指针都是**刚生成的**
|
|
431
|
+
* (`/resume` 会重跑 `setSystemPrompt()`),在那里再说一遍「你可以去读教训」只是在复述
|
|
432
|
+
* 指针本身,却要每次会话都付。工具失败是唯一一个「系统提示没说过、且与当前事件绑定」
|
|
433
|
+
* 的时机 —— 这次失败可能有一条教训正好覆盖它。
|
|
434
|
+
*
|
|
435
|
+
* 与 {@link injectRules} 同形(含**同一个调用时机约束**):必须在本轮 tool_result 都
|
|
436
|
+
* 追加完**之后**才注入。`injectContext` 推的是一条 user 消息,插进 assistant 的
|
|
437
|
+
* `tool_use` 与配对的 user `tool_result` 之间会把消息配对打断。
|
|
438
|
+
*
|
|
439
|
+
* 每会话最多一次:失败循环里同一条命令重试十次就是十次注入。代价是首个失败若不具
|
|
440
|
+
* 代表性,这次机会就用掉了 —— 可接受,因为指针常驻、代理随时仍可去读。
|
|
441
|
+
*/
|
|
442
|
+
private injectLessonNudge(): void {
|
|
443
|
+
if (!this.pendingToolFailure) return
|
|
444
|
+
this.pendingToolFailure = false
|
|
445
|
+
if (this.lessonNudgeOffered) return
|
|
446
|
+
const nudge = this.instructionsLoader?.crsiLessonNudgeText() ?? ''
|
|
447
|
+
if (!nudge) return
|
|
448
|
+
this.lessonNudgeOffered = true
|
|
449
|
+
this.context.injectContext('crsi-revisit', nudge)
|
|
450
|
+
}
|
|
451
|
+
|
|
409
452
|
/**
|
|
410
453
|
* Drain pending background task notifications.
|
|
411
454
|
* Call this after tool execution to surface completed/failed background agent results.
|
|
@@ -721,6 +764,9 @@ export class QueryEngine {
|
|
|
721
764
|
for (const toolUse of toolUses) {
|
|
722
765
|
const toolStart = Date.now()
|
|
723
766
|
const result = await this.executeTool(toolUse.name, toolUse.input, signal)
|
|
767
|
+
// 判据取 `result.success`,不取「有没有抛」:失败返回有八种形状(未知工具、
|
|
768
|
+
// 权限拒绝、cwd 已删…),全都在这里汇成同一个 `success: false`。
|
|
769
|
+
if (!result.success) this.pendingToolFailure = true
|
|
724
770
|
yield {
|
|
725
771
|
type: 'tool_result',
|
|
726
772
|
tool_use_id: toolUse.id,
|
|
@@ -797,6 +843,9 @@ export class QueryEngine {
|
|
|
797
843
|
// Inject path-scoped rules for touched files
|
|
798
844
|
this.injectRules()
|
|
799
845
|
|
|
846
|
+
// Offer the CRSI recall nudge if this round had a failed tool call.
|
|
847
|
+
this.injectLessonNudge()
|
|
848
|
+
|
|
800
849
|
// Drain task notifications after tool execution
|
|
801
850
|
for (const chunk of this.drainTaskNotifications()) {
|
|
802
851
|
yield chunk
|
|
@@ -1062,6 +1111,7 @@ export class QueryEngine {
|
|
|
1062
1111
|
// Execute tools and feed results back to the model for the next turn
|
|
1063
1112
|
for (const toolUse of toolUses) {
|
|
1064
1113
|
const result = await this.executeTool(toolUse.name, toolUse.input, signal)
|
|
1114
|
+
if (!result.success) this.pendingToolFailure = true
|
|
1065
1115
|
lastActivity = Date.now()
|
|
1066
1116
|
yield {
|
|
1067
1117
|
type: 'tool_result',
|
|
@@ -1084,6 +1134,10 @@ export class QueryEngine {
|
|
|
1084
1134
|
// Path-scoped rules for files touched in this round: process() injects for
|
|
1085
1135
|
// the first tool round only, so the multi-turn rounds need their own call.
|
|
1086
1136
|
this.injectRules()
|
|
1137
|
+
|
|
1138
|
+
// Same reason the rules injection is repeated here: the nudge must land after
|
|
1139
|
+
// this round's tool results, and this loop owns those rounds.
|
|
1140
|
+
this.injectLessonNudge()
|
|
1087
1141
|
}
|
|
1088
1142
|
// Max turns reached — safety limit, stop gracefully
|
|
1089
1143
|
}
|
package/src/core/eval-harness.ts
CHANGED
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
LESSONS_FILE,
|
|
35
35
|
buildLessonContent,
|
|
36
36
|
renderManagedRuleSource,
|
|
37
|
+
normalizeLessonSeverity,
|
|
37
38
|
} from './crsi-producer'
|
|
38
39
|
import type { CrsiSignal } from './crsi-producer'
|
|
39
40
|
import { predictionHit } from './improvement-track'
|
|
@@ -455,10 +456,18 @@ export function runEval(): EvalReport {
|
|
|
455
456
|
id: 'producer-component-tag',
|
|
456
457
|
description: '组件归因:缺省 experiential、显式组件透传、非 experiential 不进 managed-rule',
|
|
457
458
|
passed:
|
|
458
|
-
buildLessonContent(
|
|
459
|
-
|
|
460
|
-
'
|
|
461
|
-
|
|
459
|
+
buildLessonContent(
|
|
460
|
+
frozenSignal,
|
|
461
|
+
't',
|
|
462
|
+
normalizeLessonSeverity(frozenSignal.severity),
|
|
463
|
+
'src',
|
|
464
|
+
).includes('- 组件: experiential') &&
|
|
465
|
+
buildLessonContent(
|
|
466
|
+
{ ...frozenSignal, component: 'checker' },
|
|
467
|
+
't',
|
|
468
|
+
normalizeLessonSeverity(frozenSignal.severity),
|
|
469
|
+
'src',
|
|
470
|
+
).includes('- 组件: checker') &&
|
|
462
471
|
renderManagedRuleSource({ ...frozenSignal, component: 'working' }) === null &&
|
|
463
472
|
renderManagedRuleSource(frozenSignal) !== null,
|
|
464
473
|
})
|