@miphamai/cli 0.85.34 → 0.85.36
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 +4 -4
- package/package.json +1 -1
- package/src/commands/environment.ts +53 -69
- package/src/commands/git.ts +53 -16
- package/src/commands/keys.ts +28 -17
- package/src/commands/project.ts +10 -17
- package/src/commands/workflow-view.ts +24 -6
- package/src/core/context.ts +22 -5
- package/src/core/engine.ts +32 -5
- package/src/core/hooks-config.ts +32 -0
- package/src/core/improvement-track.ts +37 -1
- package/src/core/memory/memory-loader.ts +2 -2
- package/src/core/meta-rule-engine.ts +3 -1
- package/src/core/rule-engine.ts +11 -5
- package/src/core/session-log.ts +14 -6
- package/src/core/task-performance.ts +41 -0
- package/src/daemon/attach-protocol.ts +25 -0
- package/src/daemon/engine-capabilities.ts +124 -2
- package/src/daemon/remote-engine.ts +161 -4
- package/src/daemon/server.ts +13 -1
- package/src/daemon/session-worker.ts +50 -0
- package/src/i18n-core/detect.ts +16 -4
- package/src/i18n-core/locales/en-US.json +7 -5
- package/src/i18n-core/locales/zh-CN.json +7 -5
- package/src/i18n-core/preference.ts +55 -0
- package/src/index.tsx +3 -2
- package/src/mcp/registry.ts +29 -1
- package/src/shared/package-info.ts +3 -3
- package/src/tools/exec/git.ts +82 -0
- package/src/ui/app.tsx +26 -19
- package/src/ui/commands.ts +527 -100
- package/src/ui/strip-indent.ts +9 -4
package/src/core/hooks-config.ts
CHANGED
|
@@ -18,12 +18,36 @@ export interface SettingsHooks {
|
|
|
18
18
|
UserPromptSubmit?: HookConfigEntry[]
|
|
19
19
|
PreCompact?: HookConfigEntry[]
|
|
20
20
|
PostCompact?: HookConfigEntry[]
|
|
21
|
+
/** ⚠️ 目前**没有派发点** —— 写在这里不报错,但不会跑。见 `UNDISPATCHED_EVENTS`。 */
|
|
21
22
|
ConfigChange?: HookConfigEntry[]
|
|
22
23
|
SubagentStart?: HookConfigEntry[]
|
|
23
24
|
SubagentStop?: HookConfigEntry[]
|
|
25
|
+
/** ⚠️ 目前**没有派发点** —— 写在这里不报错,但不会跑。见 `UNDISPATCHED_EVENTS`。 */
|
|
24
26
|
PreInference?: HookConfigEntry[]
|
|
25
27
|
}
|
|
26
28
|
|
|
29
|
+
/**
|
|
30
|
+
* `settings.json` 接受、但**生产代码里没有任何派发点**的事件。
|
|
31
|
+
*
|
|
32
|
+
* `HookEngine` 上这两个事件各有一个现成方法(`hooks.ts:162` / `:222`,名字见 ROADMAP D18),
|
|
33
|
+
* 而它们在 `src/` + `bin/` 里**零引用** —— `grep` 只剩定义行与测试。注册进去的后果不是
|
|
34
|
+
* 「静默不生效」那么简单:`/hooks` 遍历的是**配置文件本身**(`ui/commands.ts`),于是用户
|
|
35
|
+
* 看到自己那条 hook 被列出来、计入 `hooks.found N` —— 读起来是「已安装」,实际一次都不会跑。
|
|
36
|
+
* **被告知的东西与发生的事分叉,比「不支持」更坏。**
|
|
37
|
+
*
|
|
38
|
+
* 所以这里**不注册**并说出原因。`/hooks` 读的是配置文件本身、不读注册表,故单靠这一层
|
|
39
|
+
* 并不能改变它的输出 —— 它会**继续列出**这些条目,所以 `ui/commands.ts` 的 `/hooks`
|
|
40
|
+
* 必须用同一个集合给它们打「不会运行」标记并排除出计数。两处共用一个集合,名单才不会
|
|
41
|
+
* 各写一份、各自漂移。接线本身是独立决定(ROADMAP D18):`PreInference` 要先定
|
|
42
|
+
* 「settings.json 的任意命令 hook」与「把整段对话正文发往组织端点的 DLP」如何共存 ——
|
|
43
|
+
* 后者是数据出境面,不能顺手接。
|
|
44
|
+
*
|
|
45
|
+
* ⚠️ 本注释**刻意不逐字写出那两个方法名**:`unwired-disposition.test.ts` 的
|
|
46
|
+
* `referencesOutside` 匹配的是**文本出现**、不是调用点,写出来就会把那条守卫打红
|
|
47
|
+
* (它自己的 docblock 里写着这条反向边界)。要查名字去看 ROADMAP D18。
|
|
48
|
+
*/
|
|
49
|
+
export const UNDISPATCHED_EVENTS = new Set<string>(['ConfigChange', 'PreInference'])
|
|
50
|
+
|
|
27
51
|
/**
|
|
28
52
|
* Load hook configurations from a settings object and convert them to
|
|
29
53
|
* executable HookDefinitions suitable for the HookEngine.
|
|
@@ -34,6 +58,14 @@ export function loadHookConfigs(configs: SettingsHooks): HookDefinition[] {
|
|
|
34
58
|
for (const [eventName, entries] of Object.entries(configs)) {
|
|
35
59
|
if (!entries || !Array.isArray(entries)) continue
|
|
36
60
|
|
|
61
|
+
if (UNDISPATCHED_EVENTS.has(eventName)) {
|
|
62
|
+
process.stderr.write(
|
|
63
|
+
`⚠️ Hook event ${eventName} has no dispatch point in this version — ` +
|
|
64
|
+
`${entries.length} configured hook group(s) under it will not run and are not registered.\n`,
|
|
65
|
+
)
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
|
|
37
69
|
for (const entry of entries) {
|
|
38
70
|
// A matcher is only a *filter*, so a malformed one must not take the whole
|
|
39
71
|
// hook set down with it. `new RegExp` throws here, and both callers register
|
|
@@ -32,6 +32,14 @@ export interface ImprovementReport {
|
|
|
32
32
|
*/
|
|
33
33
|
baselineDurations?: number[]
|
|
34
34
|
postDurations?: number[]
|
|
35
|
+
/**
|
|
36
|
+
* 空模型闸(null-model ablation):空程序也能通过的题数 / 总题数。
|
|
37
|
+
* 两个字段同生同灭;缺席 = 旧记录或未测。
|
|
38
|
+
*/
|
|
39
|
+
nullModelPassed?: number
|
|
40
|
+
nullModelTotal?: number
|
|
41
|
+
/** 闸的结论:failed = 有题空程序也能过 ⇒ `improved` 不可归因。 */
|
|
42
|
+
nullModelGate?: 'passed' | 'failed'
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
export interface ImprovementRecord extends ImprovementReport {
|
|
@@ -70,7 +78,20 @@ export function buildImprovementReport(
|
|
|
70
78
|
const deltaMean = mean(sample.postScores) - mean(sample.baselineScores)
|
|
71
79
|
const noise = stdDev(sample.baselineScores)
|
|
72
80
|
const minEffect = computeMinEffect(noise)
|
|
73
|
-
const
|
|
81
|
+
const rawVerdict = classifyDelta(deltaMean, minEffect)
|
|
82
|
+
|
|
83
|
+
// 空模型闸:只拦「假赢」。空程序能通过的题不测模型,故 improved 不可归因;
|
|
84
|
+
// regressed 照旧 —— 它说的是「变差了」,与题是不是真空题无关。
|
|
85
|
+
const nullModelMeasured =
|
|
86
|
+
sample.nullModelPassed !== undefined && sample.nullModelTotal !== undefined
|
|
87
|
+
const nullModelGate = nullModelMeasured
|
|
88
|
+
? sample.nullModelPassed! > 0
|
|
89
|
+
? ('failed' as const)
|
|
90
|
+
: ('passed' as const)
|
|
91
|
+
: undefined
|
|
92
|
+
const verdict =
|
|
93
|
+
nullModelGate === 'failed' && rawVerdict === 'improved' ? 'inconclusive' : rawVerdict
|
|
94
|
+
|
|
74
95
|
return {
|
|
75
96
|
skillName: sample.skillName,
|
|
76
97
|
changeSet,
|
|
@@ -94,9 +115,24 @@ export function buildImprovementReport(
|
|
|
94
115
|
...(sample.baselineDurations !== undefined && sample.postDurations !== undefined
|
|
95
116
|
? { baselineDurations: sample.baselineDurations, postDurations: sample.postDurations }
|
|
96
117
|
: {}),
|
|
118
|
+
// 三个键同生同灭:只写一半会让「测过且通过」与「没测」在记录里同形。
|
|
119
|
+
...(nullModelMeasured
|
|
120
|
+
? {
|
|
121
|
+
nullModelPassed: sample.nullModelPassed!,
|
|
122
|
+
nullModelTotal: sample.nullModelTotal!,
|
|
123
|
+
nullModelGate,
|
|
124
|
+
}
|
|
125
|
+
: {}),
|
|
97
126
|
}
|
|
98
127
|
}
|
|
99
128
|
|
|
129
|
+
/** 空模型闸失败时给出一行可见读数;闸通过则不出声(不占显示行)。 */
|
|
130
|
+
export function formatNullModelLine(report: ImprovementReport): string | null {
|
|
131
|
+
if (report.nullModelGate !== 'failed' || report.nullModelPassed === undefined) return null
|
|
132
|
+
const total = report.nullModelTotal ?? 0
|
|
133
|
+
return `🕳️ 空模型闸: 空程序已通过 ${report.nullModelPassed}/${total} 题 —— 这些题测的不是模型,改进判定已降级`
|
|
134
|
+
}
|
|
135
|
+
|
|
100
136
|
/** Wilson score 区间(z 默认 1.96 = 95%)。n=0 → {0,0}。 */
|
|
101
137
|
export function wilsonInterval(
|
|
102
138
|
improved: number,
|
|
@@ -18,7 +18,7 @@ export function getMemoryManager(): MemoryManagerType {
|
|
|
18
18
|
* Load relevant memories for the current session and inject as
|
|
19
19
|
* a system-reminder block. Called at SessionStart.
|
|
20
20
|
*/
|
|
21
|
-
export function loadSessionMemories(context: string): string {
|
|
21
|
+
export function loadSessionMemories(context: string, maxTokens?: number): string {
|
|
22
22
|
const mm = getMemoryManager()
|
|
23
|
-
return mm.buildSystemReminder(context)
|
|
23
|
+
return mm.buildSystemReminder(context, maxTokens)
|
|
24
24
|
}
|
|
@@ -572,7 +572,9 @@ export class MetaRuleEngine {
|
|
|
572
572
|
|
|
573
573
|
const autoApplicable = metaRules.filter((m) => m.autoApplicable)
|
|
574
574
|
if (autoApplicable.length > 0) {
|
|
575
|
-
recommendations.push(
|
|
575
|
+
recommendations.push(
|
|
576
|
+
`${autoApplicable.length} 个元规则可自动应用;当前版本不自动落盘,请人工复核后固化`,
|
|
577
|
+
)
|
|
576
578
|
}
|
|
577
579
|
|
|
578
580
|
if (recommendations.length === 0) {
|
package/src/core/rule-engine.ts
CHANGED
|
@@ -165,12 +165,18 @@ export class ExperienceRuleEngine {
|
|
|
165
165
|
return this.rules.filter((r) => r.enabled)
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
|
|
168
|
+
/** Returns false when no rule has that id — callers must not report success. */
|
|
169
|
+
setRuleEnabled(id: string, enabled: boolean): boolean {
|
|
169
170
|
const rule = this.rules.find((r) => r.id === id)
|
|
170
|
-
if (rule)
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
171
|
+
if (!rule) return false
|
|
172
|
+
rule.enabled = enabled
|
|
173
|
+
this.persist()
|
|
174
|
+
return true
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Find a rule by id (used to tell the user whether a toggle will persist). */
|
|
178
|
+
getRule(id: string): ToolRule | undefined {
|
|
179
|
+
return this.rules.find((r) => r.id === id)
|
|
174
180
|
}
|
|
175
181
|
|
|
176
182
|
/** Persist runtime rules to disk. Builtin 与 managed 规则永不落盘(源码即真相)。 */
|
package/src/core/session-log.ts
CHANGED
|
@@ -253,7 +253,15 @@ export class SessionLog {
|
|
|
253
253
|
private buf: SessionEvent[] = []
|
|
254
254
|
private flushed = 0
|
|
255
255
|
|
|
256
|
-
|
|
256
|
+
/**
|
|
257
|
+
* @param dir 落盘目录,**默认 `~/.mipham/sessions`**(CLI 的历史真源)。
|
|
258
|
+
* daemon 传自己的目录:它的日志是**审计增量**而不是可恢复历史(真源是 SQLite),
|
|
259
|
+
* 落进 `sessions/` 会被 `--resume` 当成一个"历史不全"的会话列出来。
|
|
260
|
+
*/
|
|
261
|
+
constructor(
|
|
262
|
+
private name: string,
|
|
263
|
+
private dir: string = LOG_DIR,
|
|
264
|
+
) {}
|
|
257
265
|
|
|
258
266
|
/**
|
|
259
267
|
* Append one event — in memory *and* on disk.
|
|
@@ -279,8 +287,8 @@ export class SessionLog {
|
|
|
279
287
|
save(): void {
|
|
280
288
|
// Session logs contain full conversation + tool results (possibly credentials):
|
|
281
289
|
// restrict to owner-only (dir 0700, file 0600).
|
|
282
|
-
mkdirSync(
|
|
283
|
-
const path = join(
|
|
290
|
+
mkdirSync(this.dir, { recursive: true, mode: 0o700 })
|
|
291
|
+
const path = join(this.dir, `${sanitizeSessionName(this.name)}.jsonl`)
|
|
284
292
|
|
|
285
293
|
// A writer killed mid-append can leave a partial line with no trailing
|
|
286
294
|
// newline. `open()` skips that line, but the next append would glue the new
|
|
@@ -309,9 +317,9 @@ export class SessionLog {
|
|
|
309
317
|
}
|
|
310
318
|
|
|
311
319
|
/** 从既有 JSONL 打开,逐行解析为事件(已落盘事件标记为已 flush)。 */
|
|
312
|
-
static open(name: string): SessionLog {
|
|
313
|
-
const log = new SessionLog(name)
|
|
314
|
-
const path = join(
|
|
320
|
+
static open(name: string, dir: string = LOG_DIR): SessionLog {
|
|
321
|
+
const log = new SessionLog(name, dir)
|
|
322
|
+
const path = join(dir, `${sanitizeSessionName(name)}.jsonl`)
|
|
315
323
|
// 类型闸在读取之前:`SessionStore.load` 在快照读不出来时**回落到这里**,所以路径上
|
|
316
324
|
// 是个 FIFO 时不能在这里换个函数继续等 —— 读不动就当空日志(见 shared/regular-file.ts)。
|
|
317
325
|
let raw = readRegularFileSync(path)
|
|
@@ -68,6 +68,35 @@ export interface TaskPerformanceResult {
|
|
|
68
68
|
truncated?: boolean
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** 空模型闸的读数:`passed` = 空程序也能通过的题数。 */
|
|
72
|
+
export interface NullModelCheck {
|
|
73
|
+
total: number
|
|
74
|
+
passed: number
|
|
75
|
+
/** 空程序也通过的题 id —— 它们正是这道闸抓到的「不是模型题」的题。 */
|
|
76
|
+
failures: string[]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 空模型消融:对每道题只用**空程序**跑冻结测试。
|
|
81
|
+
*
|
|
82
|
+
* 来源是自改进研究的 null-model control:把模型换成空 stub 重跑,分数仍涨就说明
|
|
83
|
+
* 「改进」不是模型带来的。在我们的架构里模型不产出 ⇒ 空程序;能通过,就说明这道
|
|
84
|
+
* 题测的是测试自身的空转(例如恒真断言),不是模型能力。
|
|
85
|
+
*/
|
|
86
|
+
export function countEmptySolutionPasses(tasks: PerformanceTask[]): NullModelCheck {
|
|
87
|
+
const failures: string[] = []
|
|
88
|
+
for (const task of tasks) {
|
|
89
|
+
if (judgeGeneratedCode(task.testCode, '').passed) failures.push(task.id)
|
|
90
|
+
}
|
|
91
|
+
return { total: tasks.length, passed: failures.length, failures }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** 按 skill 名取任务集再跑空模型闸;未绑定任何任务的 skill → total 0(空转,不是通过)。 */
|
|
95
|
+
export function runNullModelCheck(skill?: string): NullModelCheck {
|
|
96
|
+
const tasks = loadPerformanceTasks().filter((t) => (t.skill ?? undefined) === skill)
|
|
97
|
+
return countEmptySolutionPasses(tasks)
|
|
98
|
+
}
|
|
99
|
+
|
|
71
100
|
export interface TaskPerformanceReport {
|
|
72
101
|
total: number
|
|
73
102
|
passed: number
|
|
@@ -242,6 +271,13 @@ export interface SkillDeltaSample {
|
|
|
242
271
|
skillName: string
|
|
243
272
|
baselineScores: number[]
|
|
244
273
|
postScores: number[]
|
|
274
|
+
/**
|
|
275
|
+
* 空模型闸:把**空程序**喂给同一组冻结测试,通过的题数。
|
|
276
|
+
* 通过 > 0 ⇒ 这些题空程序也能过,测的不是模型能力 ⇒ 改进判定不可归因。
|
|
277
|
+
* 缺席 = 旧记录 / 未测(与 nullModelTotal 同生同灭)。
|
|
278
|
+
*/
|
|
279
|
+
nullModelPassed?: number
|
|
280
|
+
nullModelTotal?: number
|
|
245
281
|
/**
|
|
246
282
|
* B2 代价维:与分数数组**逐项对齐**的耗时(第 i 项就是产出第 i 个分数的那次采样)。
|
|
247
283
|
* 缺席 = 该样本未记代价(旧记录 / 旧调用点),读侧须按「缺席」处理、不得当成 0。
|
|
@@ -283,11 +319,16 @@ export async function measureSkillDeltaRepeated(
|
|
|
283
319
|
postDurations.push(r.durationMs)
|
|
284
320
|
}
|
|
285
321
|
|
|
322
|
+
// 空模型闸在最后一次采样后算一次:与模型无关,重复跑只是重复同一组测试。
|
|
323
|
+
const nullCheck = runNullModelCheck(resolved.skillName)
|
|
324
|
+
|
|
286
325
|
return {
|
|
287
326
|
skillName: resolved.skillName,
|
|
288
327
|
baselineScores,
|
|
289
328
|
postScores,
|
|
290
329
|
baselineDurations,
|
|
291
330
|
postDurations,
|
|
331
|
+
nullModelPassed: nullCheck.passed,
|
|
332
|
+
nullModelTotal: nullCheck.total,
|
|
292
333
|
}
|
|
293
334
|
}
|
|
@@ -88,6 +88,30 @@ export interface ServerModeMessage {
|
|
|
88
88
|
sessionId: string
|
|
89
89
|
mode: PermissionMode
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* 会话读数(daemon → 客户端)。`attach` 的本地 `getContext()` 是个薄桩,
|
|
93
|
+
* `/status`、`/cost`、`/doctor` 这类只读命令要用到的 token / 窗口 / checkpoint
|
|
94
|
+
* 数都在 daemon 手里 —— 由这一帧送过来,客户端据此作答,而不是显示一排 0。
|
|
95
|
+
*
|
|
96
|
+
* 每个字段都按「daemon 可能没送」处理:两端版本独立,缺字段或类型不对时客户端
|
|
97
|
+
* 保持原值(不拿 NaN 去渲染)。
|
|
98
|
+
*/
|
|
99
|
+
export interface ServerStatsMessage {
|
|
100
|
+
type: 'stats'
|
|
101
|
+
sessionId: string
|
|
102
|
+
/** 引擎上下文当前的估算 token 数。 */
|
|
103
|
+
estimatedTokens: number
|
|
104
|
+
/** 模型上下文窗口。 */
|
|
105
|
+
maxTokens: number
|
|
106
|
+
/** 触发压缩的阈值(token)。 */
|
|
107
|
+
compactionThreshold: number
|
|
108
|
+
/** 引擎上下文里的消息数。 */
|
|
109
|
+
messageCount: number
|
|
110
|
+
/** 本会话已保存的 checkpoint 数。 */
|
|
111
|
+
checkpointCount: number
|
|
112
|
+
/** 当前由 provider prompt cache 提供的 token 数。 */
|
|
113
|
+
cachedTokens: number
|
|
114
|
+
}
|
|
91
115
|
|
|
92
116
|
export type ServerMessage =
|
|
93
117
|
| ServerTextMessage
|
|
@@ -99,3 +123,4 @@ export type ServerMessage =
|
|
|
99
123
|
| ServerErrorMessage
|
|
100
124
|
| ServerSessionStateMessage
|
|
101
125
|
| ServerModeMessage
|
|
126
|
+
| ServerStatsMessage
|
|
@@ -20,8 +20,9 @@
|
|
|
20
20
|
* `test/integrity/daemon-capability-parity.test.ts` 能咬住的源码差异,而不是靠人记得。
|
|
21
21
|
*
|
|
22
22
|
* 不搬 `index.tsx` 的整套装配:ArtifactServer 是 TUI 画廊的 localhost 监听器、
|
|
23
|
-
* AgentViewManager 的唯一消费者是 TUI slash
|
|
24
|
-
*
|
|
23
|
+
* AgentViewManager 的唯一消费者是 TUI slash 命令。这里接**有后果的六条**
|
|
24
|
+
* (skills / rulesLoader / hookEngine / agentRegistry / instructions + 系统提示 / llm);
|
|
25
|
+
* 其余按后果分级进守卫的具名豁免表,逐条写理由。
|
|
25
26
|
*/
|
|
26
27
|
|
|
27
28
|
import type { QueryEngine } from '../core/engine'
|
|
@@ -35,6 +36,10 @@ import { isSafeMode } from '../core/safe-mode'
|
|
|
35
36
|
import { loadSettingsJson } from '../config/loader'
|
|
36
37
|
import { getWorkspaceTrust, warnProjectHooksSkipped } from '../core/workspace-trust'
|
|
37
38
|
import { AgentRegistry } from '../agent/agent-registry'
|
|
39
|
+
import { InstructionsLoader } from '../core/instructions'
|
|
40
|
+
import { loadSessionMemories } from '../core/memory/memory-loader'
|
|
41
|
+
import { SessionLog } from '../core/session-log'
|
|
42
|
+
import { miphamHome } from '../core/paths'
|
|
38
43
|
|
|
39
44
|
export interface DaemonEngineCapabilities {
|
|
40
45
|
/** 会话工作目录 —— 三者的派生源(外部 skill 路径 / hooks / agents 都从它算)。 */
|
|
@@ -43,6 +48,8 @@ export interface DaemonEngineCapabilities {
|
|
|
43
48
|
registry: ProviderRegistry
|
|
44
49
|
/** `config.skills.paths`(外部 skill 目录)。省略即只加载内置与用户 skill。 */
|
|
45
50
|
skillsPaths?: string[]
|
|
51
|
+
/** `config.skills.reminder`。省略即 `full`,与 CLI 侧的默认同。 */
|
|
52
|
+
skillsReminder?: 'full' | 'compact' | 'off'
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
/**
|
|
@@ -127,6 +134,22 @@ function agentsFor(cwd: string): AgentRegistry {
|
|
|
127
134
|
return registry
|
|
128
135
|
}
|
|
129
136
|
|
|
137
|
+
/**
|
|
138
|
+
* 指令装载器,**每会话新建、不记忆化** —— 与 `RulesLoader` 同一个理由(见 D7):
|
|
139
|
+
* 一个长命 daemon 服务多个会话,按 cwd 共享一只的话,会话期间改过的 `CLAUDE.md` /
|
|
140
|
+
* `MIPHAM.md` 对所有后来新建的会话都**永远看不见**,直到那个 loader 自己 `refresh()`。
|
|
141
|
+
* 代价只是每次建引擎多读几个小文件 —— 与「配额规则」那点成本同量级。
|
|
142
|
+
*
|
|
143
|
+
* 它是**系统提示里「项目规则」那一段**的唯一来源(`MIPHAM.md` / `CLAUDE.md` / `AGENTS.md`
|
|
144
|
+
* / `USER.md` 链 + CRSI 教训),也是 CRSI 召回扳机的指针来源 —— 两者必须是同一份选择,
|
|
145
|
+
* 否则「扳机说去读那份文件」与「提示里写的是另一份」会分叉。
|
|
146
|
+
*/
|
|
147
|
+
function loadInstructionsFor(cwd: string): InstructionsLoader {
|
|
148
|
+
const loader = new InstructionsLoader()
|
|
149
|
+
loader.loadAll(cwd)
|
|
150
|
+
return loader
|
|
151
|
+
}
|
|
152
|
+
|
|
130
153
|
/**
|
|
131
154
|
* 把一个 daemon 引擎接成「和交互式 CLI 同等能干活」的样子。
|
|
132
155
|
*
|
|
@@ -146,7 +169,106 @@ export function wireDaemonEngine(engine: QueryEngine, opts: DaemonEngineCapabili
|
|
|
146
169
|
engine.setHookEngine(hooksFor(opts.cwd, skills))
|
|
147
170
|
engine.setAgentRegistry(agentsFor(opts.cwd))
|
|
148
171
|
|
|
172
|
+
// ── 系统提示:daemon 此前**一条都没有** ──────────────────────────────
|
|
173
|
+
// 后果不是「提示朴素一点」,而是整条 headless 路径上模型不知道自己是谁:
|
|
174
|
+
// MIPHAM.md 的身份段(集团 MIPHAM.md §〇,强制性)、CLAUDE.md 的项目规则、
|
|
175
|
+
// 记忆、skills 提醒**一条都到不了**经渠道(飞书 / Telegram / 企微 / 钉钉)、
|
|
176
|
+
// `attach` 远端、以及基准 harness 驱动的会话。
|
|
177
|
+
//
|
|
178
|
+
// 与 CLI 同源、同顺序(`index.tsx:524` + `:605-771`),只少三样**会话脚手架**:
|
|
179
|
+
// 首跑引导 / 密钥到期提醒 / 上一会话摘要 —— 它们属于「一次性的终端会话」,
|
|
180
|
+
// 而 daemon 一个进程服务多个会话,把它们烘进每个会话的提示只是噪音。
|
|
181
|
+
const instructions = loadInstructionsFor(opts.cwd)
|
|
182
|
+
engine.setInstructions(instructions)
|
|
183
|
+
|
|
184
|
+
const context = engine.getContext()
|
|
185
|
+
// 权限段**读时派生**。烘进去的会是模式的一份拷贝,而 daemon 的档位会经 `set_mode`
|
|
186
|
+
// 在会话中途变(`server.ts` 的 `sessionModes`):往窄切是自纠正的,**往宽切**则让
|
|
187
|
+
// 模型拿着旧指令拒绝做它已被允许做的事。接的是 live 的 `permission`,同一实例也交给
|
|
188
|
+
// 引擎去执行,故下一次请求就与新档一致。
|
|
189
|
+
context.setPermissionContextSource(() =>
|
|
190
|
+
instructions.buildPermissionBlock(engine.getPermission().getMode()),
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
const basePrompt = instructions.buildSystemPrompt()
|
|
194
|
+
const extras = [
|
|
195
|
+
skills.buildSystemReminder(5000, opts.skillsReminder ?? 'full'),
|
|
196
|
+
// 预算显式给 5000:`getMemoryManager()` 是进程级单例,跟着某个会话的窗口走会让
|
|
197
|
+
// 后建的会话改掉前一个的预算 —— daemon 的会话窗口各不相同,这是唯一不互相干扰的取值。
|
|
198
|
+
loadSessionMemories(basePrompt, 5000),
|
|
199
|
+
]
|
|
200
|
+
.filter((part) => part.length > 0)
|
|
201
|
+
.join('\n\n')
|
|
202
|
+
|
|
203
|
+
// `buildSystemPrompt()` 结尾**没有换行**,`extras` 以 `<system-reminder>` 开头:
|
|
204
|
+
// 直接相加会把标签粘在上一句末尾(CLI 侧 `index.tsx` 是 `\n\n` 连接)。建提示与
|
|
205
|
+
// 压缩后的重建必须走同一个连接方式,否则两条路径的提示形状会分叉。
|
|
206
|
+
const withExtras = (base: string): string => (extras ? `${base}\n\n${extras}` : base)
|
|
207
|
+
context.setSystemPrompt(withExtras(basePrompt))
|
|
208
|
+
// 压缩之后引擎会让提示重建一次;重建前先回读磁盘,**只换指令那一段**,`extras`
|
|
209
|
+
// 原样保留 —— 那是建会话时拼进来的会话级上下文,再生成一遍等于把它们重复塞进每个请求。
|
|
210
|
+
context.setSystemPromptRebuilder(() => {
|
|
211
|
+
instructions.refresh()
|
|
212
|
+
return withExtras(instructions.buildSystemPrompt())
|
|
213
|
+
})
|
|
214
|
+
|
|
149
215
|
// 语义等价于不接(`llmChat` 本就是 `this.llm ?? this.registry`),接上是为了让
|
|
150
216
|
// 两个入口的能力集合真的相等;D2 靠它证明「先修语义再对等」这条顺序约束。
|
|
151
217
|
engine.setLlm(opts.registry)
|
|
152
218
|
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* daemon 会话日志目录 —— **与 CLI 的 `~/.mipham/sessions` 分开**是设计的一部分。
|
|
222
|
+
*
|
|
223
|
+
* daemon 的历史真源是它自己的 SQLite(`daemon.db`,REST API 也读它),日志只记**本进程
|
|
224
|
+
* 窗口内**发生的事。若落进 `sessions/`,`SessionStore.list()` 会把它当成一个可恢复会话
|
|
225
|
+
* 列出来,而它的历史是不全的 —— 那就造出一个「看起来能恢复、恢复出来缺半截」的假广告,
|
|
226
|
+
* 正是本仓反复在修的那一类。分目录之后,`--resume` **结构上**够不到它。
|
|
227
|
+
*/
|
|
228
|
+
export function daemonSessionLogDir(): string {
|
|
229
|
+
return miphamHome('daemon-sessions')
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export interface DaemonSessionLogFacts {
|
|
233
|
+
sessionId: string
|
|
234
|
+
cwd: string
|
|
235
|
+
provider: string
|
|
236
|
+
model: string
|
|
237
|
+
permissionMode: string
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 给 daemon 会话挂一份**审计用**日志 —— 收口「daemon 会话一条 JSONL 都不写」(ROADMAP T5)。
|
|
242
|
+
*
|
|
243
|
+
* 三条约束,每条都有后果:
|
|
244
|
+
*
|
|
245
|
+
* 1. **必须在 DB 回灌之后调用**(`server.ts` 里紧跟 `wireDaemonEngine`)。日志记的是
|
|
246
|
+
* 「本进程期间发生了什么」。把回灌的历史镜像进来会**重复追加**同一批消息 ——
|
|
247
|
+
* `WorkerPool` 会驱逐空闲 worker,重建引擎时那段历史会被再写一遍 ——
|
|
248
|
+
* 而且把一个审计增量伪装成完整历史。
|
|
249
|
+
* 2. **`authoritative: false`**:`ContextManager` 的「model-visible means logged」断言
|
|
250
|
+
* 只对**完整**日志有意义。残缺日志会让它对每一行旧历史报警,开着的调试断言会因此
|
|
251
|
+
* 炸掉整个 daemon 会话。关掉的是**断言**,写通照常。
|
|
252
|
+
* 3. 名字取 `daemon-<uuid>`:让人一眼看出它不是 CLI 的会话名。
|
|
253
|
+
*
|
|
254
|
+
* **已知边界(如实记)**:日志里 `compaction/summary` 的 `replacedCount` 是按**日志窗口**
|
|
255
|
+
* 算的(`context.ts` 用 `deriveMessages(log)` 的长度),不是按整段对话 —— 旧历史从没进过
|
|
256
|
+
* 这份日志。读审计文件时要知道这个数只对「被记录的那一段」成立。
|
|
257
|
+
*/
|
|
258
|
+
export function attachDaemonSessionLog(
|
|
259
|
+
engine: QueryEngine,
|
|
260
|
+
facts: DaemonSessionLogFacts,
|
|
261
|
+
): SessionLog {
|
|
262
|
+
const log = new SessionLog(`daemon-${facts.sessionId}`, daemonSessionLogDir())
|
|
263
|
+
log.append({
|
|
264
|
+
type: 'session/start',
|
|
265
|
+
at: Date.now(),
|
|
266
|
+
sessionId: facts.sessionId,
|
|
267
|
+
provider: facts.provider,
|
|
268
|
+
model: facts.model,
|
|
269
|
+
cwd: facts.cwd,
|
|
270
|
+
permissionMode: facts.permissionMode,
|
|
271
|
+
})
|
|
272
|
+
engine.getContext().setLog(log, { authoritative: false })
|
|
273
|
+
return log
|
|
274
|
+
}
|