@miphamai/cli 0.85.10 → 0.85.12
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 +1 -1
- package/package.json +1 -1
- package/src/agent/sub-agent.ts +49 -1
- package/src/commands/autoloop-journal.ts +16 -1
- package/src/config/loader.ts +23 -3
- package/src/core/context-microcompact.ts +3 -5
- package/src/core/context.ts +58 -16
- package/src/core/credential-masker/output-scrub.ts +7 -1
- package/src/core/crsi-producer.ts +86 -6
- package/src/core/crsi-sandbox.ts +101 -49
- package/src/core/engine.ts +108 -26
- package/src/core/hooks-executor.ts +165 -16
- package/src/core/instructions.ts +6 -5
- package/src/core/memory/memory-manager.ts +9 -1
- package/src/core/permission-config.ts +28 -0
- package/src/core/permission.ts +51 -5
- package/src/core/rules-loader.ts +96 -5
- package/src/core/task-performance.ts +32 -4
- package/src/i18n-core/locales/en-US.json +2 -0
- package/src/i18n-core/locales/zh-CN.json +2 -0
- package/src/mcp/registry.ts +8 -0
- package/src/plugin/plugin-loader.ts +10 -9
- package/src/providers/anthropic.ts +11 -3
- package/src/providers/fetch-utils.ts +34 -1
- package/src/providers/openai-compat.ts +24 -2
- package/src/shared/package-info.ts +2 -2
- package/src/shared/sanitize.ts +65 -0
- package/src/shared/types.ts +30 -0
- package/src/tools/agent/agent.ts +18 -3
- package/src/tools/agent/exit-plan.ts +49 -9
- package/src/tools/artifact/artifact.ts +0 -1
- package/src/tools/exec/bash.ts +28 -3
- package/src/tools/index.ts +7 -1
- package/src/tools/network/web-fetch.ts +2 -1
- package/src/tools/system/tool-search.ts +1 -1
- package/src/ui/chat.tsx +2 -1
- package/src/ui/commands.ts +28 -11
- package/src/ui/input.tsx +5 -0
- package/src/workflow/event-bus.ts +13 -0
- package/src/workflow/journal.ts +8 -0
- package/src/workflow/runtime.ts +59 -11
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Mipham Code v0.
|
|
1
|
+
# Mipham Code v0.85.12
|
|
2
2
|
|
|
3
3
|
**Multi-model open-core intelligent coding terminal** — 12 AI providers, 138 commands, 31 tools, 28 skills + marketplace, self-update. AI-assisted code generation, security auditing, MCP protocol, and extensible skills — in a single CLI.
|
|
4
4
|
|
package/package.json
CHANGED
package/src/agent/sub-agent.ts
CHANGED
|
@@ -22,6 +22,27 @@ function getPatternAnalyzer(): PatternAnalyzer {
|
|
|
22
22
|
return _patternAnalyzer
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/** 每进程每条只念一次 —— 与 `core/permission-audit.ts` 的 `warnedOnce` 同形。 */
|
|
26
|
+
const warnedAgentCaps = new Set<string>()
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 项目级 agent 想把自己抬到比父会话更宽、被 `resolveAgentMode` 顶回去时,把这件事念出来。
|
|
30
|
+
*
|
|
31
|
+
* **不静默**是本仓库的既有口径 —— `config/loader.ts` 的 `stripProjectPermission` 对项目级
|
|
32
|
+
* 权限是**拒绝并报告**,不是忽略;agent 定义这条路径此前既不拒绝也不报告。stderr 是本仓库
|
|
33
|
+
* 既有的告警通道(`core/permission-audit.ts` / `core/hooks.ts` / `core/workspace-trust.ts` 同形)。
|
|
34
|
+
*
|
|
35
|
+
* 判读与措辞在 `resolveAgentMode` 里,这里只补上它拿不到的那一半 —— agent 的名字。
|
|
36
|
+
*/
|
|
37
|
+
function reportAgentCapWarnings(gate: PermissionSystem, agentName?: string): void {
|
|
38
|
+
for (const warning of gate.getAgentCapWarnings()) {
|
|
39
|
+
const line = `⚠️ agent "${agentName ?? 'unknown'}": ${warning}\n`
|
|
40
|
+
if (warnedAgentCaps.has(line)) continue
|
|
41
|
+
warnedAgentCaps.add(line)
|
|
42
|
+
process.stderr.write(line)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
25
46
|
const TYPE_SYSTEM_PROMPTS: Record<SubAgentType, string> = {
|
|
26
47
|
general: 'You are a focused sub-agent. Complete the assigned task thoroughly and return results.',
|
|
27
48
|
explore:
|
|
@@ -262,7 +283,9 @@ export class SubAgent {
|
|
|
262
283
|
agentDef?.permissionMode && agentDef.permissionMode !== 'inherit'
|
|
263
284
|
? agentDef.permissionMode
|
|
264
285
|
: 'inherit'
|
|
265
|
-
|
|
286
|
+
// `source` decides whether the cap applies: a repository-supplied definition may
|
|
287
|
+
// narrow the parent's gate but not widen it. See `resolveAgentMode`.
|
|
288
|
+
subPermission = this.permission.createSubAgentPermission(agentPermMode, agentDef?.source)
|
|
266
289
|
}
|
|
267
290
|
|
|
268
291
|
// Absence is not a licence to run everything. With no permission system handed in,
|
|
@@ -271,6 +294,9 @@ export class SubAgent {
|
|
|
271
294
|
// what the CLI itself runs under, so this is the same policy, not a stricter one.
|
|
272
295
|
const gate = subPermission ?? new PermissionSystem('default')
|
|
273
296
|
|
|
297
|
+
// The cap is never silent: say which agent asked for what and what it got instead.
|
|
298
|
+
reportAgentCapWarnings(gate, agentDef?.name)
|
|
299
|
+
|
|
274
300
|
// Resolve execution directory: worktree isolation or process cwd
|
|
275
301
|
const execCwd = options.worktreePath || process.cwd()
|
|
276
302
|
|
|
@@ -432,10 +458,32 @@ export class SubAgent {
|
|
|
432
458
|
model: finalModel,
|
|
433
459
|
registry: this.registry,
|
|
434
460
|
toolRegistry: this.toolRegistry,
|
|
461
|
+
// 交出去的是**上面那个 `subPermission`**,不是缺省时新建的 `gate`。两者在生产路径上
|
|
462
|
+
// 恒为同一个对象 —— `Engine` 的 `permission` 字段有默认值(`engine.ts` 的构造参数),
|
|
463
|
+
// `getPermission()` 的返回类型不含 `undefined`,而全仓五个 `new SubAgent(...)` 生产
|
|
464
|
+
// 构造点全部追溯到它(`agent.ts` / `workflow/runtime.ts` / `skill.ts` 各自的
|
|
465
|
+
// `ctx.permissionSystem`,以及 `ui/commands.ts` 的两处 `engine.getPermission()`)。
|
|
466
|
+
// 所以 `subPermission` 为 `undefined` 只在「没人递闸门」时出现,那时下层自建 `default`。
|
|
467
|
+
//
|
|
468
|
+
// **别把它改成 `gate`。** 喂进去一个真实的 `default` 闸门,会让下层的 agent 定义从
|
|
469
|
+
// 「`permissionMode` 被整段忽略」变成「被兑现」(`resolveAgentMode` 那一支整个挂在
|
|
470
|
+
// `if (this.permission)` 下),其中 `acceptEdits` / `bypassPermissions` 比 `default`
|
|
471
|
+
// **宽** —— 为消掉一处对象身份的不一致而放宽闸门,方向是反的。要收口就该往另一边走:
|
|
472
|
+
// 让「无父闸」时对**所有**来源都按合成的 `default` 顶回去(那样 `plan` 这类**收窄**的
|
|
473
|
+
// 声明才不会被丢掉),但为一条生产不可达的路径加参数不值当,故记在这里。
|
|
474
|
+
//
|
|
475
|
+
// 生产侧那条不变量 ——「交出去的就是替它自己裁决的那一个,不是父系统、也不是新建的
|
|
476
|
+
// default」—— 由 `test/agent/sub-agent.test.ts` 的
|
|
477
|
+
// "hands nested tools the clamped permission, never the parent permission" 钉住。
|
|
435
478
|
permissionSystem: subPermission,
|
|
436
479
|
ruleEngine: this.ruleEngine,
|
|
437
480
|
llm: this.llm,
|
|
438
481
|
readFiles: new Set<string>(),
|
|
482
|
+
// Every tool call from here on is one level down, and a few tools must know
|
|
483
|
+
// it: `Agent` cannot consume a background handle, so its default flips to
|
|
484
|
+
// synchronous (see `resolveRunInBackground`). Set on the shared literal so it
|
|
485
|
+
// covers the synchronous *and* background runs — both go through here.
|
|
486
|
+
isSubAgent: true,
|
|
439
487
|
}
|
|
440
488
|
|
|
441
489
|
try {
|
|
@@ -80,13 +80,28 @@ export function recordLoopTurn(sessionId: string, summary: string, delta: number
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* The row written when an iteration produced **no visible text**.
|
|
85
|
+
*
|
|
86
|
+
* The summary is the assistant's visible output (`app.tsx` passes
|
|
87
|
+
* `assistantContent`, which only accumulates `type === 'text'` chunks), so a
|
|
88
|
+
* turn that emitted only tool calls — or only thinking, which `formatThinking`
|
|
89
|
+
* hides by default — arrives here as an empty string. Written as `#N: ` that
|
|
90
|
+
* row is indistinguishable from "we recorded nothing at all", which is exactly
|
|
91
|
+
* what `/loop status` must not conflate. So it is spelled out.
|
|
92
|
+
*/
|
|
93
|
+
export const NO_VISIBLE_OUTPUT = '(no visible output — tool calls or thinking only)'
|
|
94
|
+
|
|
83
95
|
/** Log an iteration in the autonomous loop journal. */
|
|
84
96
|
export function logAutoloopIteration(sessionId: string, summary: string): void {
|
|
85
97
|
const journal = readAutoloopJournal(sessionId)
|
|
86
98
|
if (!journal) return
|
|
87
99
|
journal.iterations++
|
|
88
100
|
journal.lastIteration = new Date().toISOString()
|
|
89
|
-
|
|
101
|
+
const text = summary.trim()
|
|
102
|
+
journal.logs.push(
|
|
103
|
+
`[${journal.lastIteration}] #${journal.iterations}: ${text ? text.slice(0, 200) : NO_VISIBLE_OUTPUT}`,
|
|
104
|
+
)
|
|
90
105
|
// Keep last 50 log entries
|
|
91
106
|
if (journal.logs.length > 50) journal.logs = journal.logs.slice(-50)
|
|
92
107
|
atomicWriteFileSync(journalPath(sessionId), JSON.stringify(journal, null, 2), { mode: 0o644 })
|
package/src/config/loader.ts
CHANGED
|
@@ -519,8 +519,22 @@ export function loadSettingsJson(
|
|
|
519
519
|
|
|
520
520
|
for (const { path, readHooks, isProject } of searchPaths) {
|
|
521
521
|
try {
|
|
522
|
+
// `readRegularFileSync` folds "absent" and "present but unreadable"
|
|
523
|
+
// (EACCES, a directory, a FIFO) into the same `null`. Absent is the ordinary
|
|
524
|
+
// case here — most of these paths simply do not exist — so it stays quiet.
|
|
525
|
+
// But a settings file that *is* there and could not be read drops its
|
|
526
|
+
// `permissions.deny` wholesale, and the direction of that failure is
|
|
527
|
+
// **looser**. A security-relevant file must not fail open in silence.
|
|
528
|
+
const present = existsSync(path)
|
|
522
529
|
const raw = readRegularFileSync(path)
|
|
523
|
-
if (raw === null)
|
|
530
|
+
if (raw === null) {
|
|
531
|
+
if (present) {
|
|
532
|
+
process.stderr.write(
|
|
533
|
+
`⚠ Mipham Code: settings file (${path}) is not a readable regular file — ignoring it.\n`,
|
|
534
|
+
)
|
|
535
|
+
}
|
|
536
|
+
continue
|
|
537
|
+
}
|
|
524
538
|
const parsed = JSON.parse(raw) as {
|
|
525
539
|
hooks?: Record<string, unknown>
|
|
526
540
|
permissions?: { allow?: unknown; deny?: unknown; defaultMode?: unknown }
|
|
@@ -584,8 +598,14 @@ export function loadSettingsJson(
|
|
|
584
598
|
}
|
|
585
599
|
}
|
|
586
600
|
}
|
|
587
|
-
} catch {
|
|
588
|
-
//
|
|
601
|
+
} catch (err: unknown) {
|
|
602
|
+
// Malformed JSON is the other half of the same problem: the file exists and
|
|
603
|
+
// declares a `deny` list we cannot see. Say so, with the parse reason, the
|
|
604
|
+
// same way `safeParseYaml` does for `config.yml`.
|
|
605
|
+
const msg = err instanceof Error ? err.message : String(err)
|
|
606
|
+
process.stderr.write(
|
|
607
|
+
`⚠ Mipham Code: failed to parse settings file (${path}): ${msg} — ignoring it.\n`,
|
|
608
|
+
)
|
|
589
609
|
}
|
|
590
610
|
}
|
|
591
611
|
|
|
@@ -108,12 +108,10 @@ function compressToolResult(msg: Message): Message {
|
|
|
108
108
|
*
|
|
109
109
|
* Only compress when tokens saved > tokens lost from cache invalidation.
|
|
110
110
|
* The multiplier of 1.5 provides a safety margin.
|
|
111
|
+
*
|
|
112
|
+
* 与「要不要压缩」的触发尺无关:那把尺在 `checkCompression`(本函数只在它之后被问)。
|
|
111
113
|
*/
|
|
112
|
-
export function shouldMicrocompact(
|
|
113
|
-
messages: Message[],
|
|
114
|
-
cacheTracker: CacheTracker,
|
|
115
|
-
_threshold: number,
|
|
116
|
-
): boolean {
|
|
114
|
+
export function shouldMicrocompact(messages: Message[], cacheTracker: CacheTracker): boolean {
|
|
117
115
|
let savingsTokens = 0
|
|
118
116
|
let cacheLossTokens = 0
|
|
119
117
|
|
package/src/core/context.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Message, ToolResult } from '../shared/index.ts'
|
|
2
2
|
import { snipMessages } from './context-snip'
|
|
3
|
-
import { microcompact } from './context-microcompact'
|
|
3
|
+
import { microcompact, shouldMicrocompact } from './context-microcompact'
|
|
4
4
|
import { NoopCacheTracker, type CacheTracker, type CacheStatus } from './context-token'
|
|
5
5
|
import {
|
|
6
6
|
SessionLog,
|
|
@@ -34,6 +34,30 @@ interface Checkpoint {
|
|
|
34
34
|
label: string
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* 一次 `compact()` 的调度表 —— 「压完再判一次,仍超预算就再压、且压得更狠」。
|
|
39
|
+
*
|
|
40
|
+
* 存在的理由:**触发尺与削减尺不是同一把**。`needsCompaction()` 按 token 判,而一趟压缩
|
|
41
|
+
* 只按条数削,于是**一条超大消息(大文本 / 图片 / 长工具结果)可以同时穿过两把尺** ——
|
|
42
|
+
* 判着要压,压完却仍超预算,此后每一轮都带着超长请求打上游,直到用户手动 `/compact`
|
|
43
|
+
* 或上游报错。只压一趟且从不复查,就是那个缺口。
|
|
44
|
+
*
|
|
45
|
+
* - 第一趟 `{keep: 20, minLength: 30}` 与历史行为**逐字等价**(`minLength` 就是原来那条
|
|
46
|
+
* `messages.length <= 30` 的 no-op)。
|
|
47
|
+
* - 第二趟的闸取 `keep` 本身而不是 `keep + 10`:第一趟之后只剩 20 条(截断回落)或 21 条
|
|
48
|
+
* (有 summarizer 时多一条摘要),沿用 `keep + 10 = 20` 会让截断那条路命中 `20 <= 20`
|
|
49
|
+
* 而**永不跑第二趟**,整个改动自我抵消。此时唯一要问的是「还有没有可丢的」。
|
|
50
|
+
* - 表是常量、无 `while` ⇒ 有界,不可能死循环。
|
|
51
|
+
*
|
|
52
|
+
* `recountMessageTokens()` 随每趟一起跑(见 `compactOnce`)—— 它是第二趟那道「仍超预算吗」
|
|
53
|
+
* 的**闸所读的那个数**。放在循环外只算一次,闸读到的就是压缩前的旧值,永远为真 ⇒
|
|
54
|
+
* 第二趟变成无条件跑,「复查」就名存实亡。
|
|
55
|
+
*/
|
|
56
|
+
const COMPACTION_PASSES: ReadonlyArray<{ keep: number; minLength: number }> = [
|
|
57
|
+
{ keep: 20, minLength: 30 },
|
|
58
|
+
{ keep: 10, minLength: 10 },
|
|
59
|
+
]
|
|
60
|
+
|
|
37
61
|
export class ContextManager {
|
|
38
62
|
private messages: Message[] = []
|
|
39
63
|
private systemPrompt = ''
|
|
@@ -296,11 +320,25 @@ export class ContextManager {
|
|
|
296
320
|
async compact(heading: string): Promise<{ before: number; after: number }> {
|
|
297
321
|
const beforeTokens = this.getEstimatedTokens()
|
|
298
322
|
|
|
299
|
-
|
|
300
|
-
|
|
323
|
+
for (let i = 0; i < COMPACTION_PASSES.length; i++) {
|
|
324
|
+
const pass = COMPACTION_PASSES[i]!
|
|
325
|
+
// 第一趟无条件跑(手动 `/compact` 即使未超预算也必须动);之后**只**在复查
|
|
326
|
+
// 仍超预算时继续 —— 这一句就是「压完再判一次」。
|
|
327
|
+
if (i > 0 && !this.needsCompaction()) break
|
|
328
|
+
if (this.messages.length <= pass.minLength) continue
|
|
329
|
+
await this.compactOnce(heading, pass.keep)
|
|
301
330
|
}
|
|
302
331
|
|
|
303
|
-
|
|
332
|
+
return { before: beforeTokens, after: this.getEstimatedTokens() }
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* 压缩一趟:把消息削到最近 `keep` 条。
|
|
337
|
+
*
|
|
338
|
+
* body(除 `keep` 外)逐行取自旧 `compact()`,两种分支原样保留:LLM 摘要(失败回落截断)
|
|
339
|
+
* 与纯截断。`recountMessageTokens()` 留在**这里**而不是调用方 —— 下一趟的闸要读它。
|
|
340
|
+
*/
|
|
341
|
+
private async compactOnce(heading: string, keep: number): Promise<void> {
|
|
304
342
|
const toDrop = this.messages.slice(0, -keep)
|
|
305
343
|
|
|
306
344
|
if (this.summarizer && toDrop.length >= 4) {
|
|
@@ -340,8 +378,6 @@ export class ContextManager {
|
|
|
340
378
|
|
|
341
379
|
// Re-estimate the message half (the prompt half is derived on read).
|
|
342
380
|
this.recountMessageTokens()
|
|
343
|
-
|
|
344
|
-
return { before: beforeTokens, after: this.getEstimatedTokens() }
|
|
345
381
|
}
|
|
346
382
|
|
|
347
383
|
/**
|
|
@@ -478,12 +514,7 @@ export class ContextManager {
|
|
|
478
514
|
|
|
479
515
|
const usage = this.getEstimatedTokens() / this.config.maxTokens
|
|
480
516
|
|
|
481
|
-
|
|
482
|
-
const microThreshold = this.config.contextWindow
|
|
483
|
-
? Math.max(0.7, 1 - 150000 / this.config.contextWindow)
|
|
484
|
-
: 0.7
|
|
485
|
-
|
|
486
|
-
if (usage > microThreshold) {
|
|
517
|
+
if (usage > this.microcompactThreshold()) {
|
|
487
518
|
this.compressionPending = true
|
|
488
519
|
// Schedule microcompact asynchronously (fire-and-forget)
|
|
489
520
|
void Promise.resolve().then(() => {
|
|
@@ -493,6 +524,11 @@ export class ContextManager {
|
|
|
493
524
|
}
|
|
494
525
|
}
|
|
495
526
|
|
|
527
|
+
/** Adaptive microcompact threshold: 200K→0.70, 500K→0.80, 1M→0.85 */
|
|
528
|
+
private microcompactThreshold(): number {
|
|
529
|
+
return this.config.contextWindow ? Math.max(0.7, 1 - 150000 / this.config.contextWindow) : 0.7
|
|
530
|
+
}
|
|
531
|
+
|
|
496
532
|
/** Run snip + microcompact inline and update stats. */
|
|
497
533
|
private runMicrocompact(): void {
|
|
498
534
|
const { messages: snipped, removed } = snipMessages(this.messages)
|
|
@@ -502,11 +538,17 @@ export class ContextManager {
|
|
|
502
538
|
this.compactionStats.snipMessagesRemoved += removed
|
|
503
539
|
}
|
|
504
540
|
|
|
505
|
-
// Microcompact: compress tool_results not needed for recent context
|
|
541
|
+
// Microcompact: compress tool_results not needed for recent context.
|
|
542
|
+
// 先问 `shouldMicrocompact` —— 它比的是「省下的」与「因缓存失效赔掉的」:压掉一条消息会让
|
|
543
|
+
// 它**之后**的整段前缀缓存失效,赔的常常比省的多。不闸这一道的话,这次压在有缓存时纯属白跑
|
|
544
|
+
// (实测:带 PrefixCacheTracker 时 `microcompact` 的 tokensSaved 恒为 0 —— 它能碰的只有最新
|
|
545
|
+
// 那条,而那条被下面这个 keepRecent 护着)。**只闸这一步**:上面的 snip 是另一层(零成本裁剪
|
|
546
|
+
// 空 tool_result),与缓存经济无关,照跑。
|
|
506
547
|
const keepRecent = 3
|
|
507
|
-
const
|
|
508
|
-
|
|
509
|
-
|
|
548
|
+
const worthCompacting = shouldMicrocompact(snipped, this.cacheTracker)
|
|
549
|
+
const { messages: compacted, tokensSaved } = worthCompacting
|
|
550
|
+
? microcompact(snipped, this.cacheTracker, { keepRecent })
|
|
551
|
+
: { messages: snipped, tokensSaved: 0 }
|
|
510
552
|
|
|
511
553
|
if (tokensSaved > 0) {
|
|
512
554
|
this.compactionStats.microcompactCount++
|
|
@@ -19,7 +19,13 @@ const TOKEN_REDACTION_PATTERN =
|
|
|
19
19
|
* out every non-secret endpoint too). The shape is the only reliable tell.
|
|
20
20
|
* Redacts the password and keeps user + host, or the line stops being readable.
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
// The password group must NOT exclude `@`: a literal `@` inside a password is
|
|
23
|
+
// illegal per URL syntax, yet `postgres://user:p@ss@host` is exactly how people
|
|
24
|
+
// paste connection strings into a shell. Excluding `@` made the group stop at the
|
|
25
|
+
// *first* `@`, redacting only `p` and leaving `ss@host` in the clear. Greedy
|
|
26
|
+
// `[^/\s]+` runs to the last `@` on the line and backtracks, so the whole password
|
|
27
|
+
// goes and the host survives.
|
|
28
|
+
const URL_USERINFO_PATTERN = /([a-z][a-z0-9+.-]*:\/\/)([^/\s:@]+):([^/\s]+)@/gi
|
|
23
29
|
|
|
24
30
|
/**
|
|
25
31
|
* Scrub credential patterns from stdout/stderr output.
|
|
@@ -115,7 +115,27 @@ export function buildLessonContent(
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
|
-
*
|
|
118
|
+
* 档位的**来历** —— 这一格回答的是「谁定的档」,不是「档是什么」。
|
|
119
|
+
*
|
|
120
|
+
* - `declared` —— 文件里逐字写了 `- 严重度: critical` 或 `- 严重度: warning`。
|
|
121
|
+
* - `defaulted` —— 没有那一行,或写了但取值在闭集之外(如 `info`、笔误)⇒
|
|
122
|
+
* {@link normalizeLessonSeverity} fail-open 落 `critical`,于是这条进了常驻档。
|
|
123
|
+
*
|
|
124
|
+
* 为什么非记不可:`severity` 是**归一化之后**的读数,而归一化是 fail-open 的 ⇒
|
|
125
|
+
* 「有人判定它该每次请求都在场」与「压根没人写过档位」在 `severity` 上**同形**。
|
|
126
|
+
* 常驻档的成本是人付的、能动手的也只有人(改文件那一行),看不出这条是被判过的
|
|
127
|
+
* 还是漏写的,就无从复核 —— 这正是本仓库记过的「无声消失与从没写过同形」。
|
|
128
|
+
*
|
|
129
|
+
* 不改任何**判定**行为:`severity` 照旧 fail-open 落 `critical`,常驻/择点/预算一概不看
|
|
130
|
+
* 这一格。它只把不可见的默认**变成可复核的清单**(见 {@link buildResidentLessonReport})。
|
|
131
|
+
*
|
|
132
|
+
* 一条不变量:`severity === 'warning'` ⇒ `severitySource === 'declared'`。
|
|
133
|
+
* 因为归一化只有**逐字** `warning` 才落 `warning`,其余一切落 `critical`。
|
|
134
|
+
*/
|
|
135
|
+
export type LessonSeveritySource = 'declared' | 'defaulted'
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 教训精华(标题 + 建议 + 严重度 + 档位来历),用于运行时召回注入系统提示。
|
|
119
139
|
*
|
|
120
140
|
* 严重度决定**注入位置**,不只是标签:`critical` 常驻每一次请求,
|
|
121
141
|
* `warning` 移出常驻块、按需召回(见 {@link isAlwaysOnLesson}),且常驻档整体受
|
|
@@ -127,6 +147,12 @@ export interface CrsiLessonSummary {
|
|
|
127
147
|
title: string
|
|
128
148
|
suggestion: string
|
|
129
149
|
severity: LessonSeverity
|
|
150
|
+
/**
|
|
151
|
+
* 必填而非可选 —— 同 `buildLessonContent` 的 `severity`:可选的话,下一个构造点忘了给
|
|
152
|
+
* 就等于「来历是默认落的」,而那个默认值恰好**说反了**(它说的是「有人判过」)。
|
|
153
|
+
* 忘了要给不出来的东西,必须是编译错误。
|
|
154
|
+
*/
|
|
155
|
+
severitySource: LessonSeveritySource
|
|
130
156
|
}
|
|
131
157
|
|
|
132
158
|
/**
|
|
@@ -153,25 +179,50 @@ export function normalizeLessonSeverity(raw: string | undefined): LessonSeverity
|
|
|
153
179
|
}
|
|
154
180
|
|
|
155
181
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
182
|
+
* 闭集成员的**运行时**面 —— `LessonSeverity` 是类型,运行时只存在这一份字面量。
|
|
183
|
+
*
|
|
184
|
+
* 定义在**一处**:{@link isDeclaredLessonSeverity} 读它,而它与
|
|
185
|
+
* {@link normalizeLessonSeverity} 必须对**同一个**闭集说话(一个问「折叠前在不在集里」、
|
|
186
|
+
* 一个做折叠)。两处各写一份字面量的话,哪天闭集加一档,会出现「归一是对的、来历却说
|
|
187
|
+
* 它不在闭集」这种自相矛盾的读数 —— 且没有任何东西会报。
|
|
188
|
+
*/
|
|
189
|
+
const LESSON_SEVERITY_SET: ReadonlySet<string> = new Set(['critical', 'warning'])
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 那一行是否**逐字**落在闭集内(= 档位有人判过)。缺席、空串与闭集外取值一律 false。
|
|
193
|
+
*
|
|
194
|
+
* 参数类型与 {@link normalizeLessonSeverity} 刻意**同形**(`string | undefined`):
|
|
195
|
+
* 两者是同一个原始捕获的一对读者(一个折叠、一个问来历),签名不一致的话,调用点迟早
|
|
196
|
+
* 会把某个缺席的捕获只喂给其中一个。
|
|
197
|
+
*/
|
|
198
|
+
function isDeclaredLessonSeverity(raw: string | undefined): boolean {
|
|
199
|
+
return typeof raw === 'string' && LESSON_SEVERITY_SET.has(raw)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 从 crsi-lessons.md 提取每条教训的「精华」(标题 + 建议 + 严重度 + 档位来历),
|
|
204
|
+
* 跳过证据段落。这是「只写不读」缺口 → 「写后召回」的读取侧。
|
|
158
205
|
*
|
|
159
206
|
* 按 `##` 块累积、块边界 flush —— 因为 `- 严重度:` 行写在 `- 建议:` **之后**,
|
|
160
207
|
* 「见到建议即 push」的写法读不到它。块级累积对行序不敏感。
|
|
161
208
|
*
|
|
162
|
-
* 缺 `严重度` 时的落档见 {@link DEFAULT_LESSON_SEVERITY}
|
|
209
|
+
* 缺 `严重度` 时的落档见 {@link DEFAULT_LESSON_SEVERITY};**它是怎么落的**记在
|
|
210
|
+
* `severitySource` 里(见 {@link LessonSeveritySource})—— 归一化把「有人判过」与
|
|
211
|
+
* 「没人写过」折叠成了同一个 `critical`,本读取侧把这两者**重新分开**。
|
|
163
212
|
*/
|
|
164
213
|
export function extractCrsiLessonSummaries(content: string): CrsiLessonSummary[] {
|
|
165
214
|
const out: CrsiLessonSummary[] = []
|
|
166
215
|
let title = ''
|
|
167
216
|
let suggestion = ''
|
|
168
217
|
let severity = DEFAULT_LESSON_SEVERITY
|
|
218
|
+
let severitySource: LessonSeveritySource = 'defaulted'
|
|
169
219
|
|
|
170
220
|
const flush = () => {
|
|
171
|
-
if (title && suggestion) out.push({ title, suggestion, severity })
|
|
221
|
+
if (title && suggestion) out.push({ title, suggestion, severity, severitySource })
|
|
172
222
|
title = ''
|
|
173
223
|
suggestion = ''
|
|
174
224
|
severity = DEFAULT_LESSON_SEVERITY
|
|
225
|
+
severitySource = 'defaulted'
|
|
175
226
|
}
|
|
176
227
|
|
|
177
228
|
for (const line of content.split('\n')) {
|
|
@@ -191,6 +242,8 @@ export function extractCrsiLessonSummaries(content: string): CrsiLessonSummary[]
|
|
|
191
242
|
// 闭集外的取值一律落常驻档 —— 未知严重度不倒向「按需」。规则在 normalizeLessonSeverity
|
|
192
243
|
// 一处;写入者同读它(正则已吃掉两侧空白,故传进去的是已经 trim 过的捕获)。
|
|
193
244
|
severity = normalizeLessonSeverity(v[1])
|
|
245
|
+
// 来历与档位**同一处**算出:分开写迟早出现「档是折叠后的、来历说的还是原文」的错配。
|
|
246
|
+
severitySource = isDeclaredLessonSeverity(v[1]) ? 'declared' : 'defaulted'
|
|
194
247
|
}
|
|
195
248
|
}
|
|
196
249
|
flush()
|
|
@@ -380,6 +433,12 @@ export function buildLessonRevisitNudge(
|
|
|
380
433
|
* 报告不可能与真正注入的那一份漂移。逐条成本按**渲染后的文本量**算,且报出可验算的
|
|
381
434
|
* 加法(表头 + 逐条 + 分隔 == 合计)—— 否则「逐条和 ≠ 合计」会被当成 bug 查一遍。
|
|
382
435
|
*
|
|
436
|
+
* 另报**档位来历**({@link LessonSeveritySource}):成本答「这档涨得值不值」,来历答
|
|
437
|
+
* 「这一格是谁定的」。两个读者的输出都不需要它(模型不问档位怎么来的),只有人有这个
|
|
438
|
+
* 问题 —— 而「没人复核谁把一条教训标成 critical」正是因此才存在:归一化 fail-open,
|
|
439
|
+
* 「有人判过」与「没人写过」在 `severity` 上同形,名册里读到的就是一个普通的 critical。
|
|
440
|
+
* 故这里把两者分开列、逐条点名 —— 只报数不点名,读者仍要自己去文件里比对。
|
|
441
|
+
*
|
|
383
442
|
* 只读:**不加写路径**。人的杠杆是改文件那一行;写入侧是生产者与 `/crsi propose`。
|
|
384
443
|
*/
|
|
385
444
|
export function buildResidentLessonReport(
|
|
@@ -415,9 +474,24 @@ export function buildResidentLessonReport(
|
|
|
415
474
|
)
|
|
416
475
|
|
|
417
476
|
if (resident.length > 0) {
|
|
477
|
+
// 档位**来历**与成本分开报:成本答「这档涨得值不值」,来历答「这一格是谁定的」。
|
|
478
|
+
// 两者都要人动手才能改,但改动完全不同(前者缩短、后者补/改 `- 严重度:` 那一行)。
|
|
479
|
+
const defaulted = resident.filter((s) => s.severitySource === 'defaulted')
|
|
480
|
+
// 两个分支**同一个形状**(`**默认落档 N** 条`):形状随取值变的话,读者与判据都得先
|
|
481
|
+
// 分辨自己看到的是哪一支 —— 而「0 条」与「这一支没打印」本来就会长得一样。
|
|
482
|
+
lines.push(
|
|
483
|
+
`档位来历:显式判定 **${resident.length - defaulted.length}** 条 · ` +
|
|
484
|
+
`**默认落档 ${defaulted.length}** 条` +
|
|
485
|
+
(defaulted.length > 0
|
|
486
|
+
? ' —— 文件里没写 `- 严重度:`、或写了闭集外的取值,fail-open 落 critical ⇒ ' +
|
|
487
|
+
'**没有任何人判定过**它们该常驻'
|
|
488
|
+
: ''),
|
|
489
|
+
)
|
|
490
|
+
|
|
418
491
|
lines.push('', '### 逐条成本(渲染后字符)', '')
|
|
419
492
|
for (let i = 0; i < resident.length; i++) {
|
|
420
|
-
|
|
493
|
+
const mark = resident[i]!.severitySource === 'defaulted' ? '(档位默认落档)' : ''
|
|
494
|
+
lines.push(`- ${num(itemChars[i]!)} 字符 — ${resident[i]!.title}${mark}`)
|
|
421
495
|
}
|
|
422
496
|
// 加法自证:表头是每条共担的,逐条和必然大于合计 —— 与其让人去猜差额,
|
|
423
497
|
// 不如把算式写出来,且当场验算(`block` 走的是真渲染器,不是我在这里的手算)。
|
|
@@ -1036,6 +1110,12 @@ export function removeLessonSections(content: string, headers: string[]): string
|
|
|
1036
1110
|
* 3. **不停在「缺省 critical」** —— 那个缺省是给**人类新写**的条目兜底的;合并是**降维**,
|
|
1037
1111
|
* 没有依据说产物比两个源更紧急。实测(真模块):两条 warning 合并曾升进常驻档,
|
|
1038
1112
|
* 常驻 6 条 2,155 字符 → 7 条 2,198,而这一切发生在**没有人决定过**的情况下。
|
|
1113
|
+
*
|
|
1114
|
+
* **已知边界(如实写,不假装覆盖)**:来历**不跨代传递**。产物由 `buildLessonContent`
|
|
1115
|
+
* 逐字写 `- 严重度: <merged>`,于是它读回来的来历恒为 `declared` —— 哪怕两个源都是
|
|
1116
|
+
* 默认落档。也就是说 crossover 会把「没人判过」**洗成**「有人判过」,名册看不出来。
|
|
1117
|
+
* 修它要把来历写进文件(新字段),是改教训文件的格式,不在本格口径内 —— 故此处的
|
|
1118
|
+
* 判据只对**人写进去的**条目成立。见 {@link LessonSeveritySource}。
|
|
1039
1119
|
*/
|
|
1040
1120
|
export function deriveMergedSeverity(a: LessonSeverity, b: LessonSeverity): LessonSeverity {
|
|
1041
1121
|
return a === 'critical' || b === 'critical' ? 'critical' : 'warning'
|