@miphamai/cli 0.85.9 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miphamai/cli",
3
- "version": "0.85.9",
3
+ "version": "0.85.10",
4
4
  "description": "Mipham Code — Multi-model open-core intelligent coding terminal by MiphamAI",
5
5
  "keywords": [
6
6
  "ai",
@@ -346,6 +346,27 @@ export function buildCrsiLessonsPointer(
346
346
  return lines.join('\n')
347
347
  }
348
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
+
349
370
  /**
350
371
  * 常驻档名册 —— `selectResidentLessons` 的**第三个读者**,也是**唯一给人看的**那一个。
351
372
  *
@@ -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
  }
@@ -9,6 +9,7 @@ import {
9
9
  extractCrsiLessonSummaries,
10
10
  buildCrsiLessonsBlock,
11
11
  buildCrsiLessonsPointer,
12
+ buildLessonRevisitNudge,
12
13
  selectResidentLessons,
13
14
  type CrsiLessonSummary,
14
15
  } from './crsi-producer'
@@ -481,6 +482,23 @@ Never omit it or present the work as purely human-authored.`)
481
482
  .join('\n\n')
482
483
  }
483
484
 
485
+ /**
486
+ * 事件型召回扳机 —— {@link crsiLessonsText} 的姊妹件,走**同一个择点**、同一份
487
+ * `lessonsPath`,因此不可能与系统提示里那条指针漂移(一处说「有 33 条未常驻」、
488
+ * 另一处说「没有」是最坏的形状:两声哨,读者不知道信哪个)。
489
+ *
490
+ * 与 `crsiLessonsText` 的分工:那份进**系统提示**,一次生成、随每次请求发出;
491
+ * 这份在**工具失败**那一刻注入,只在事件上说话。没有未常驻教训时返回空串 ——
492
+ * 与指针同一条件,故不会出现「扳机响了但没有指针」。
493
+ */
494
+ crsiLessonNudgeText(): string {
495
+ if (!this.lessonsPath) return ''
496
+ return buildLessonRevisitNudge(
497
+ selectResidentLessons(this.crsiLessonSummaries),
498
+ this.lessonsPath,
499
+ )
500
+ }
501
+
484
502
  /**
485
503
  * Loader-shaped alias of {@link buildPermissionBlock}(模块级那个才是实现)。
486
504
  * 保留它是因为**已有**的调用点都握着一个装载器:`index.tsx` 的接线行与
package/src/index.tsx CHANGED
@@ -746,6 +746,12 @@ export async function runApp(options: RunOptions): Promise<void> {
746
746
  // matching file. Loaded once at startup; setRulesLoader performs the load.
747
747
  engine.setRulesLoader(new RulesLoader(process.cwd()))
748
748
 
749
+ // CRSI lessons recall nudge: the instruction loader is where the *pointer* to the
750
+ // non-resident lessons comes from, so the nudge reads its emptiness condition from
751
+ // the same selection rather than guessing at a path. Same loader instance that
752
+ // builds the system prompt above — no second copy of the lessons state.
753
+ engine.setInstructions(instructions)
754
+
749
755
  // Wire inference hooks (DLP) configuration
750
756
  const inferenceHookConfig = loadInferenceHookConfig()
751
757
  engine.setInferenceHookConfig(inferenceHookConfig)
@@ -9,7 +9,7 @@
9
9
  export const PACKAGE_NAME = '@miphamai/cli' as const
10
10
 
11
11
  /** 当前发布版本 */
12
- export const PACKAGE_VERSION = '0.85.9' as const
12
+ export const PACKAGE_VERSION = '0.85.10' as const
13
13
 
14
14
  /** npm install 全局安装命令 */
15
15
  export const NPM_INSTALL_COMMAND = `npm install -g ${PACKAGE_NAME}` as const
@@ -90,4 +90,4 @@ export const TOOL_COUNT = 31 as const
90
90
  * —— `test/e2e/full-pipeline.test.ts` 在 Linux 上整文件 skip、在 macOS 上跑 ——
91
91
  * 但**总数相同**(两边都把被 skip 的算进去)。
92
92
  */
93
- export const TEST_COUNT = 3696 as const
93
+ export const TEST_COUNT = 3714 as const