@linxin666/dsh-liangshen 0.4.19 → 0.4.21

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/lib/index.js CHANGED
@@ -424,13 +424,21 @@ function parsePagedToolPatterns(text) {
424
424
  function overrideTargets(overrides) {
425
425
  const paged = parsePagedToolPatterns(overrides.pagedToolPatterns);
426
426
  const pagedPatch = () => paged === void 0 ? {} : { pagedToolPatterns: [...paged] };
427
+ const idlePatch = () => overrides.idleReclaimSteps === void 0 ? {} : { idleReclaimSteps: overrides.idleReclaimSteps };
427
428
  return [
428
429
  ["tool-catalog", {
429
430
  ...overrides.presentation === void 0 ? {} : { presentation: overrides.presentation },
430
- ...pagedPatch()
431
+ ...pagedPatch(),
432
+ ...idlePatch()
433
+ }],
434
+ ["tool-activate", {
435
+ ...pagedPatch(),
436
+ ...idlePatch()
437
+ }],
438
+ ["working-context", {
439
+ ...pagedPatch(),
440
+ ...idlePatch()
431
441
  }],
432
- ["tool-activate", pagedPatch()],
433
- ["working-context", pagedPatch()],
434
442
  ["minimal-prompt", { ...overrides.dispatcher === void 0 ? {} : { dispatcher: overrides.dispatcher } }]
435
443
  ];
436
444
  }
@@ -655,7 +663,8 @@ const Config = z.object({
655
663
  announceToAgent: z.boolean().default(false).volatile(),
656
664
  presentation: z.union([...PRESENTATION_OPTIONS, z.transform("both", () => "ptc").deprecated().hidden()]).default("ptc").volatile(),
657
665
  dispatcher: z.boolean().default(true).volatile(),
658
- pagedToolPatterns: z.string().default("").volatile()
666
+ pagedToolPatterns: z.string().default("").volatile(),
667
+ idleReclaimSteps: z.number().min(0).default(0).volatile()
659
668
  });
660
669
  /** Schema defaults, re-read for hand-built test contexts. */
661
670
  const DEFAULT_CONFIG = {
@@ -663,7 +672,8 @@ const DEFAULT_CONFIG = {
663
672
  announceToAgent: false,
664
673
  presentation: "ptc",
665
674
  dispatcher: true,
666
- pagedToolPatterns: ""
675
+ pagedToolPatterns: "",
676
+ idleReclaimSteps: 0
667
677
  };
668
678
  /**
669
679
  * Read one activation field. The Host validates the raw profile config and
@@ -689,11 +699,12 @@ function resolveConfig(config) {
689
699
  announceToAgent: readField(config?.announceToAgent, DEFAULT_CONFIG.announceToAgent),
690
700
  presentation: rawPresentation === "both" ? "ptc" : rawPresentation,
691
701
  dispatcher: readField(config?.dispatcher, DEFAULT_CONFIG.dispatcher),
692
- pagedToolPatterns: readField(config?.pagedToolPatterns, DEFAULT_CONFIG.pagedToolPatterns)
702
+ pagedToolPatterns: readField(config?.pagedToolPatterns, DEFAULT_CONFIG.pagedToolPatterns),
703
+ idleReclaimSteps: readField(config?.idleReclaimSteps, DEFAULT_CONFIG.idleReclaimSteps)
693
704
  };
694
705
  }
695
706
  /** Model-facing announcement: plugin presence, principle, and limits. */
696
- const LIANGSHEN_GUIDANCE = "本机已安装 dsh-liangshen 插件(梁神模式 agent preset):新建会话的预设选择器中可选「梁神模式」。原理:系统提示词保持极简 persona(minimal-prompt 放行该段与 plan 模式的 plan:policy),persona 内置本模式工作纪律(反思熔断——同一假设推演不超过两轮、缺事实立即闭合思考并调用原生检测工具;行动导向——思考只决定下一步具体操作、不在思考中预演代码实现;并发探索——多处独立检查或搜索在单轮内并发发射多个工具调用;YAGNI/PDCA——单步验证单一假设、不写冗余注释;有界收敛——不无限下钻依赖链、前置检查最多2-3轮后立即收敛并作答或编辑),并在组装时追加工作区目录行 Your working directory is <cwd>.。AGENTS.md 工作区指令默认交还宿主自身的 agent-instructions 行,以 user 角色注入,本插件不追加任何系统提示词段、也不改动 pre-step 的消息批次;可选 instructionSource: system-prompt 才由本插件在组装时读取 AGENTS.md 链并追加 workspace-instructions 段(65536 字节预算,每次组装重读)。wire 呈现由 tool-catalog 按会话一次声明,取值 'ptc'(默认:wire 收拢为唯一的 run_code,其余工具经生成的 SDK 调用)或 'native'(组装出的原生清单,无程序化传输),并可在插件设置界面切换(写入所声明预设的 tool-catalog 行);未挂载 code runtime 时不做声明,会话运行原生工具面。按需工具族出厂开启(pagedToolPatterns 默认 ['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*'],可在插件设置的「按需工具族」中用逗号分隔的前缀改写):匹配工具在激活前被作用域级工具限制移出可见面(既不在 wire,也不在生成的 SDK 声明中),目录消息列出常驻工具签名与未激活命名空间摘要,调用 tool_activate({ namespace }) 按需激活(LRU 上限 3 个活跃命名空间,驱逐最久未用);激活状态从持久会话事件流重建,resume/压缩后自然恢复。关键事实登记簿(fact_register)让模型把硬约束/已确认决策/失败路径登记为单行事实,随 working-context 行每步投射进局部注意力窗口。working-context 插件在 pre-step 注入单行 [Working Context: ...] 就近状态投射(plan 模式、活跃命名空间、进行中 todo、登记事实,全部从事件流折叠,读不到则省略,全部为空则不注入)。历史工具结果修剪为 4096 字符阈值(head 2048 / tail 1024)。文件操作受宿主沙箱约束;shell 在每个平台都挂上游标准 Stdio 栈(POSIX 为 bash,Windows 为 pwsh),带简短描述标题卡片与确定性退出码。真实推理探针通过不等于模式集成通过,更不等于统计效果提升。预设由插件在激活时向 agent-preset registry 直接注册(不写任何预设目录),插件停用即注销;默认预设由用户自行选择。用户提到「梁神模式 / 锚定模式 / anchored standard」时即指本插件,请据此协作。";
707
+ const LIANGSHEN_GUIDANCE = "本机已安装 dsh-liangshen 插件(梁神模式 agent preset):新建会话的预设选择器中可选「梁神模式」。原理:系统提示词保持极简 persona(minimal-prompt 放行该段与 plan 模式的 plan:policy),persona 内置本模式工作纪律(反思熔断——同一假设推演不超过两轮、缺事实立即闭合思考并调用原生检测工具;行动导向——思考只决定下一步具体操作、不在思考中预演代码实现;并发探索——多处独立检查或搜索在单轮内并发发射多个工具调用;YAGNI/PDCA——单步验证单一假设、不写冗余注释;有界收敛——不无限下钻依赖链、前置检查最多2-3轮后立即收敛并作答或编辑),并在组装时追加工作区目录行 Your working directory is <cwd>.。AGENTS.md 工作区指令默认交还宿主自身的 agent-instructions 行,以 user 角色注入,本插件不追加任何系统提示词段、也不改动 pre-step 的消息批次;可选 instructionSource: system-prompt 才由本插件在组装时读取 AGENTS.md 链并追加 workspace-instructions 段(65536 字节预算,每次组装重读)。wire 呈现由 tool-catalog 按会话一次声明,取值 'ptc'(默认:wire 收拢为唯一的 run_code,其余工具经生成的 SDK 调用)或 'native'(组装出的原生清单,无程序化传输),并可在插件设置界面切换(写入所声明预设的 tool-catalog 行);未挂载 code runtime 时不做声明,会话运行原生工具面。按需工具族出厂开启(pagedToolPatterns 默认 ['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*'],可在插件设置的「按需工具族」中用逗号分隔的前缀改写):匹配工具在激活前被作用域级工具限制移出可见面(既不在 wire,也不在生成的 SDK 声明中),目录消息列出常驻工具签名与未激活命名空间摘要,调用 tool_activate({ namespace }) 按需激活(LRU 上限 3 个活跃命名空间,驱逐最久未用);激活状态从持久会话事件流重建,resume/压缩后自然恢复。可选 idleReclaimSteps(设置「空闲收回步数」,默认 0=不收回)让连续 N 步无成功调用的已激活命名空间过期,但只在下次激活或压缩时延迟收回——这些时机前缀缓存本就要重建,因此不新增任何全量重建;应用前命名空间仍常驻可达,再次调用即续命。关键事实登记簿(fact_register)让模型把硬约束/已确认决策/失败路径登记为单行事实,随 working-context 行每步投射进局部注意力窗口。working-context 插件在 pre-step 注入单行 [Working Context: ...] 就近状态投射(plan 模式、活跃命名空间、进行中 todo、登记事实,全部从事件流折叠,读不到则省略,全部为空则不注入)。历史工具结果修剪为 4096 字符阈值(head 2048 / tail 1024)。文件操作受宿主沙箱约束;shell 在每个平台都挂上游标准 Stdio 栈(POSIX 为 bash,Windows 为 pwsh),带简短描述标题卡片与确定性退出码。真实推理探针通过不等于模式集成通过,更不等于统计效果提升。预设由插件在激活时向 agent-preset registry 直接注册(不写任何预设目录),插件停用即注销;默认预设由用户自行选择。用户提到「梁神模式 / 锚定模式 / anchored standard」时即指本插件,请据此协作。";
697
708
  /** Order of the announcement section within the tool-guidance band. */
698
709
  const SECTION_ORDER = 150;
699
710
  /**
@@ -33,6 +33,8 @@ export interface LiangShenSettings {
33
33
  dispatcher?: boolean;
34
34
  /** Comma-separated on-demand tool families the Host writes to the tool-catalog row. */
35
35
  pagedToolPatterns?: string;
36
+ /** Deferred idle-reclaim threshold in steps the Host writes to every paging row. */
37
+ idleReclaimSteps?: number;
36
38
  }
37
39
  /** What the LiangShen card renders. */
38
40
  export interface LiangShenSettingsCardState extends CardShell {
@@ -41,6 +43,7 @@ export interface LiangShenSettingsCardState extends CardShell {
41
43
  presentation: CardFieldState;
42
44
  dispatcher: CardFieldState;
43
45
  pagedToolPatterns: CardFieldState;
46
+ idleReclaimSteps: CardFieldState;
44
47
  }
45
48
  /** The registration-side face the card's slot entry injects. */
46
49
  export interface LiangShenSettingsCardFace extends CardActions {
@@ -33,6 +33,9 @@ export declare const zh: {
33
33
  'settings.pagedToolPatterns': string;
34
34
  'settings.pagedToolPatternsHint': string;
35
35
  'settings.pagedToolPatternsInvalid': string;
36
+ 'settings.idleReclaimSteps': string;
37
+ 'settings.idleReclaimStepsHint': string;
38
+ 'settings.idleReclaimStepsInvalid': string;
36
39
  'settings.on': string;
37
40
  'settings.off': string;
38
41
  'settings.inherit': string;
@@ -111,6 +111,8 @@ export interface NumberConstraints {
111
111
  integer?: boolean;
112
112
  /** The accepted value must be at least this. */
113
113
  min?: number;
114
+ /** The accepted value must be at most this. */
115
+ max?: number;
114
116
  }
115
117
  /** A whole- or decimal-number field. An empty draft clears the field; any other draft that is not a finite number within the constraints stays unwritten. */
116
118
  export declare function numberField(field: string, constraints?: NumberConstraints): FieldSpec;
@@ -98,6 +98,12 @@ export interface PresetOverrides {
98
98
  * when blank or malformed.
99
99
  */
100
100
  pagedToolPatterns?: string;
101
+ /**
102
+ * Deferred idle-reclaim threshold in steps; written to every row that pages
103
+ * from the list (tool-catalog, tool-activate, working-context), skipped when
104
+ * absent.
105
+ */
106
+ idleReclaimSteps?: number;
101
107
  }
102
108
  /**
103
109
  * Parse the on-demand tool family text: comma separated, each entry a prefix
@@ -95,6 +95,15 @@ export interface Config {
95
95
  * malformed text is ignored.
96
96
  */
97
97
  pagedToolPatterns?: ConfigField<string>;
98
+ /**
99
+ * Deferred idle-reclaim threshold in steps, written to every row that replays
100
+ * the page (tool-catalog, tool-activate, working-context). 0 (the default)
101
+ * keeps every activation resident until LRU eviction — the shipped behavior.
102
+ * A positive N expires a namespace with no successful call for N steps, but
103
+ * the replay applies the expiry only at the next activation or compaction,
104
+ * so reclaim never adds a prefix-cache rebuild (issue #2066).
105
+ */
106
+ idleReclaimSteps?: ConfigField<number>;
98
107
  }
99
108
  export declare const Config: z<Schemastery.ObjectS<NoInfer<{
100
109
  enabled: z<boolean, boolean, "volatile-defined">;
@@ -102,12 +111,14 @@ export declare const Config: z<Schemastery.ObjectS<NoInfer<{
102
111
  presentation: z<string, "ptc" | "native", "volatile-defined">;
103
112
  dispatcher: z<boolean, boolean, "volatile-defined">;
104
113
  pagedToolPatterns: z<string, string, "volatile-defined">;
114
+ idleReclaimSteps: z<number, number, "volatile-defined">;
105
115
  }>>, Schemastery.ObjectT<NoInfer<{
106
116
  enabled: z<boolean, boolean, "volatile-defined">;
107
117
  announceToAgent: z<boolean, boolean, "volatile-defined">;
108
118
  presentation: z<string, "ptc" | "native", "volatile-defined">;
109
119
  dispatcher: z<boolean, boolean, "volatile-defined">;
110
120
  pagedToolPatterns: z<string, string, "volatile-defined">;
121
+ idleReclaimSteps: z<number, number, "volatile-defined">;
111
122
  }>>, "plain">;
112
123
  /** The settings the runtime acts on, every field read at its use site. */
113
124
  export interface ResolvedConfig {
@@ -121,6 +132,8 @@ export interface ResolvedConfig {
121
132
  dispatcher: boolean;
122
133
  /** On-demand tool family text; empty inherits the preset's shipped list. */
123
134
  pagedToolPatterns: string;
135
+ /** Deferred idle-reclaim threshold in steps; 0 keeps the shipped resident behavior. */
136
+ idleReclaimSteps: number;
124
137
  }
125
138
  /** Schema defaults, re-read for hand-built test contexts. */
126
139
  export declare const DEFAULT_CONFIG: ResolvedConfig;
@@ -131,7 +144,7 @@ export declare const DEFAULT_CONFIG: ResolvedConfig;
131
144
  */
132
145
  export declare function resolveConfig(config?: Config): ResolvedConfig;
133
146
  /** Model-facing announcement: plugin presence, principle, and limits. */
134
- export declare const LIANGSHEN_GUIDANCE = "\u672C\u673A\u5DF2\u5B89\u88C5 dsh-liangshen \u63D2\u4EF6\uFF08\u6881\u795E\u6A21\u5F0F agent preset\uFF09\uFF1A\u65B0\u5EFA\u4F1A\u8BDD\u7684\u9884\u8BBE\u9009\u62E9\u5668\u4E2D\u53EF\u9009\u300C\u6881\u795E\u6A21\u5F0F\u300D\u3002\u539F\u7406\uFF1A\u7CFB\u7EDF\u63D0\u793A\u8BCD\u4FDD\u6301\u6781\u7B80 persona\uFF08minimal-prompt \u653E\u884C\u8BE5\u6BB5\u4E0E plan \u6A21\u5F0F\u7684 plan:policy\uFF09\uFF0Cpersona \u5185\u7F6E\u672C\u6A21\u5F0F\u5DE5\u4F5C\u7EAA\u5F8B\uFF08\u53CD\u601D\u7194\u65AD\u2014\u2014\u540C\u4E00\u5047\u8BBE\u63A8\u6F14\u4E0D\u8D85\u8FC7\u4E24\u8F6E\u3001\u7F3A\u4E8B\u5B9E\u7ACB\u5373\u95ED\u5408\u601D\u8003\u5E76\u8C03\u7528\u539F\u751F\u68C0\u6D4B\u5DE5\u5177\uFF1B\u884C\u52A8\u5BFC\u5411\u2014\u2014\u601D\u8003\u53EA\u51B3\u5B9A\u4E0B\u4E00\u6B65\u5177\u4F53\u64CD\u4F5C\u3001\u4E0D\u5728\u601D\u8003\u4E2D\u9884\u6F14\u4EE3\u7801\u5B9E\u73B0\uFF1B\u5E76\u53D1\u63A2\u7D22\u2014\u2014\u591A\u5904\u72EC\u7ACB\u68C0\u67E5\u6216\u641C\u7D22\u5728\u5355\u8F6E\u5185\u5E76\u53D1\u53D1\u5C04\u591A\u4E2A\u5DE5\u5177\u8C03\u7528\uFF1BYAGNI/PDCA\u2014\u2014\u5355\u6B65\u9A8C\u8BC1\u5355\u4E00\u5047\u8BBE\u3001\u4E0D\u5199\u5197\u4F59\u6CE8\u91CA\uFF1B\u6709\u754C\u6536\u655B\u2014\u2014\u4E0D\u65E0\u9650\u4E0B\u94BB\u4F9D\u8D56\u94FE\u3001\u524D\u7F6E\u68C0\u67E5\u6700\u591A2-3\u8F6E\u540E\u7ACB\u5373\u6536\u655B\u5E76\u4F5C\u7B54\u6216\u7F16\u8F91\uFF09\uFF0C\u5E76\u5728\u7EC4\u88C5\u65F6\u8FFD\u52A0\u5DE5\u4F5C\u533A\u76EE\u5F55\u884C Your working directory is <cwd>.\u3002AGENTS.md \u5DE5\u4F5C\u533A\u6307\u4EE4\u9ED8\u8BA4\u4EA4\u8FD8\u5BBF\u4E3B\u81EA\u8EAB\u7684 agent-instructions \u884C\uFF0C\u4EE5 user \u89D2\u8272\u6CE8\u5165\uFF0C\u672C\u63D2\u4EF6\u4E0D\u8FFD\u52A0\u4EFB\u4F55\u7CFB\u7EDF\u63D0\u793A\u8BCD\u6BB5\u3001\u4E5F\u4E0D\u6539\u52A8 pre-step \u7684\u6D88\u606F\u6279\u6B21\uFF1B\u53EF\u9009 instructionSource: system-prompt \u624D\u7531\u672C\u63D2\u4EF6\u5728\u7EC4\u88C5\u65F6\u8BFB\u53D6 AGENTS.md \u94FE\u5E76\u8FFD\u52A0 workspace-instructions \u6BB5\uFF0865536 \u5B57\u8282\u9884\u7B97\uFF0C\u6BCF\u6B21\u7EC4\u88C5\u91CD\u8BFB\uFF09\u3002wire \u5448\u73B0\u7531 tool-catalog \u6309\u4F1A\u8BDD\u4E00\u6B21\u58F0\u660E\uFF0C\u53D6\u503C 'ptc'\uFF08\u9ED8\u8BA4\uFF1Awire \u6536\u62E2\u4E3A\u552F\u4E00\u7684 run_code\uFF0C\u5176\u4F59\u5DE5\u5177\u7ECF\u751F\u6210\u7684 SDK \u8C03\u7528\uFF09\u6216 'native'\uFF08\u7EC4\u88C5\u51FA\u7684\u539F\u751F\u6E05\u5355\uFF0C\u65E0\u7A0B\u5E8F\u5316\u4F20\u8F93\uFF09\uFF0C\u5E76\u53EF\u5728\u63D2\u4EF6\u8BBE\u7F6E\u754C\u9762\u5207\u6362\uFF08\u5199\u5165\u6240\u58F0\u660E\u9884\u8BBE\u7684 tool-catalog \u884C\uFF09\uFF1B\u672A\u6302\u8F7D code runtime \u65F6\u4E0D\u505A\u58F0\u660E\uFF0C\u4F1A\u8BDD\u8FD0\u884C\u539F\u751F\u5DE5\u5177\u9762\u3002\u6309\u9700\u5DE5\u5177\u65CF\u51FA\u5382\u5F00\u542F\uFF08pagedToolPatterns \u9ED8\u8BA4 ['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*']\uFF0C\u53EF\u5728\u63D2\u4EF6\u8BBE\u7F6E\u7684\u300C\u6309\u9700\u5DE5\u5177\u65CF\u300D\u4E2D\u7528\u9017\u53F7\u5206\u9694\u7684\u524D\u7F00\u6539\u5199\uFF09\uFF1A\u5339\u914D\u5DE5\u5177\u5728\u6FC0\u6D3B\u524D\u88AB\u4F5C\u7528\u57DF\u7EA7\u5DE5\u5177\u9650\u5236\u79FB\u51FA\u53EF\u89C1\u9762\uFF08\u65E2\u4E0D\u5728 wire\uFF0C\u4E5F\u4E0D\u5728\u751F\u6210\u7684 SDK \u58F0\u660E\u4E2D\uFF09\uFF0C\u76EE\u5F55\u6D88\u606F\u5217\u51FA\u5E38\u9A7B\u5DE5\u5177\u7B7E\u540D\u4E0E\u672A\u6FC0\u6D3B\u547D\u540D\u7A7A\u95F4\u6458\u8981\uFF0C\u8C03\u7528 tool_activate({ namespace }) \u6309\u9700\u6FC0\u6D3B\uFF08LRU \u4E0A\u9650 3 \u4E2A\u6D3B\u8DC3\u547D\u540D\u7A7A\u95F4\uFF0C\u9A71\u9010\u6700\u4E45\u672A\u7528\uFF09\uFF1B\u6FC0\u6D3B\u72B6\u6001\u4ECE\u6301\u4E45\u4F1A\u8BDD\u4E8B\u4EF6\u6D41\u91CD\u5EFA\uFF0Cresume/\u538B\u7F29\u540E\u81EA\u7136\u6062\u590D\u3002\u5173\u952E\u4E8B\u5B9E\u767B\u8BB0\u7C3F\uFF08fact_register\uFF09\u8BA9\u6A21\u578B\u628A\u786C\u7EA6\u675F/\u5DF2\u786E\u8BA4\u51B3\u7B56/\u5931\u8D25\u8DEF\u5F84\u767B\u8BB0\u4E3A\u5355\u884C\u4E8B\u5B9E\uFF0C\u968F working-context \u884C\u6BCF\u6B65\u6295\u5C04\u8FDB\u5C40\u90E8\u6CE8\u610F\u529B\u7A97\u53E3\u3002working-context \u63D2\u4EF6\u5728 pre-step \u6CE8\u5165\u5355\u884C [Working Context: ...] \u5C31\u8FD1\u72B6\u6001\u6295\u5C04\uFF08plan \u6A21\u5F0F\u3001\u6D3B\u8DC3\u547D\u540D\u7A7A\u95F4\u3001\u8FDB\u884C\u4E2D todo\u3001\u767B\u8BB0\u4E8B\u5B9E\uFF0C\u5168\u90E8\u4ECE\u4E8B\u4EF6\u6D41\u6298\u53E0\uFF0C\u8BFB\u4E0D\u5230\u5219\u7701\u7565\uFF0C\u5168\u90E8\u4E3A\u7A7A\u5219\u4E0D\u6CE8\u5165\uFF09\u3002\u5386\u53F2\u5DE5\u5177\u7ED3\u679C\u4FEE\u526A\u4E3A 4096 \u5B57\u7B26\u9608\u503C\uFF08head 2048 / tail 1024\uFF09\u3002\u6587\u4EF6\u64CD\u4F5C\u53D7\u5BBF\u4E3B\u6C99\u7BB1\u7EA6\u675F\uFF1Bshell \u5728\u6BCF\u4E2A\u5E73\u53F0\u90FD\u6302\u4E0A\u6E38\u6807\u51C6 Stdio \u6808\uFF08POSIX \u4E3A bash\uFF0CWindows \u4E3A pwsh\uFF09\uFF0C\u5E26\u7B80\u77ED\u63CF\u8FF0\u6807\u9898\u5361\u7247\u4E0E\u786E\u5B9A\u6027\u9000\u51FA\u7801\u3002\u771F\u5B9E\u63A8\u7406\u63A2\u9488\u901A\u8FC7\u4E0D\u7B49\u4E8E\u6A21\u5F0F\u96C6\u6210\u901A\u8FC7\uFF0C\u66F4\u4E0D\u7B49\u4E8E\u7EDF\u8BA1\u6548\u679C\u63D0\u5347\u3002\u9884\u8BBE\u7531\u63D2\u4EF6\u5728\u6FC0\u6D3B\u65F6\u5411 agent-preset registry \u76F4\u63A5\u6CE8\u518C\uFF08\u4E0D\u5199\u4EFB\u4F55\u9884\u8BBE\u76EE\u5F55\uFF09\uFF0C\u63D2\u4EF6\u505C\u7528\u5373\u6CE8\u9500\uFF1B\u9ED8\u8BA4\u9884\u8BBE\u7531\u7528\u6237\u81EA\u884C\u9009\u62E9\u3002\u7528\u6237\u63D0\u5230\u300C\u6881\u795E\u6A21\u5F0F / \u951A\u5B9A\u6A21\u5F0F / anchored standard\u300D\u65F6\u5373\u6307\u672C\u63D2\u4EF6\uFF0C\u8BF7\u636E\u6B64\u534F\u4F5C\u3002";
147
+ export declare const LIANGSHEN_GUIDANCE = "\u672C\u673A\u5DF2\u5B89\u88C5 dsh-liangshen \u63D2\u4EF6\uFF08\u6881\u795E\u6A21\u5F0F agent preset\uFF09\uFF1A\u65B0\u5EFA\u4F1A\u8BDD\u7684\u9884\u8BBE\u9009\u62E9\u5668\u4E2D\u53EF\u9009\u300C\u6881\u795E\u6A21\u5F0F\u300D\u3002\u539F\u7406\uFF1A\u7CFB\u7EDF\u63D0\u793A\u8BCD\u4FDD\u6301\u6781\u7B80 persona\uFF08minimal-prompt \u653E\u884C\u8BE5\u6BB5\u4E0E plan \u6A21\u5F0F\u7684 plan:policy\uFF09\uFF0Cpersona \u5185\u7F6E\u672C\u6A21\u5F0F\u5DE5\u4F5C\u7EAA\u5F8B\uFF08\u53CD\u601D\u7194\u65AD\u2014\u2014\u540C\u4E00\u5047\u8BBE\u63A8\u6F14\u4E0D\u8D85\u8FC7\u4E24\u8F6E\u3001\u7F3A\u4E8B\u5B9E\u7ACB\u5373\u95ED\u5408\u601D\u8003\u5E76\u8C03\u7528\u539F\u751F\u68C0\u6D4B\u5DE5\u5177\uFF1B\u884C\u52A8\u5BFC\u5411\u2014\u2014\u601D\u8003\u53EA\u51B3\u5B9A\u4E0B\u4E00\u6B65\u5177\u4F53\u64CD\u4F5C\u3001\u4E0D\u5728\u601D\u8003\u4E2D\u9884\u6F14\u4EE3\u7801\u5B9E\u73B0\uFF1B\u5E76\u53D1\u63A2\u7D22\u2014\u2014\u591A\u5904\u72EC\u7ACB\u68C0\u67E5\u6216\u641C\u7D22\u5728\u5355\u8F6E\u5185\u5E76\u53D1\u53D1\u5C04\u591A\u4E2A\u5DE5\u5177\u8C03\u7528\uFF1BYAGNI/PDCA\u2014\u2014\u5355\u6B65\u9A8C\u8BC1\u5355\u4E00\u5047\u8BBE\u3001\u4E0D\u5199\u5197\u4F59\u6CE8\u91CA\uFF1B\u6709\u754C\u6536\u655B\u2014\u2014\u4E0D\u65E0\u9650\u4E0B\u94BB\u4F9D\u8D56\u94FE\u3001\u524D\u7F6E\u68C0\u67E5\u6700\u591A2-3\u8F6E\u540E\u7ACB\u5373\u6536\u655B\u5E76\u4F5C\u7B54\u6216\u7F16\u8F91\uFF09\uFF0C\u5E76\u5728\u7EC4\u88C5\u65F6\u8FFD\u52A0\u5DE5\u4F5C\u533A\u76EE\u5F55\u884C Your working directory is <cwd>.\u3002AGENTS.md \u5DE5\u4F5C\u533A\u6307\u4EE4\u9ED8\u8BA4\u4EA4\u8FD8\u5BBF\u4E3B\u81EA\u8EAB\u7684 agent-instructions \u884C\uFF0C\u4EE5 user \u89D2\u8272\u6CE8\u5165\uFF0C\u672C\u63D2\u4EF6\u4E0D\u8FFD\u52A0\u4EFB\u4F55\u7CFB\u7EDF\u63D0\u793A\u8BCD\u6BB5\u3001\u4E5F\u4E0D\u6539\u52A8 pre-step \u7684\u6D88\u606F\u6279\u6B21\uFF1B\u53EF\u9009 instructionSource: system-prompt \u624D\u7531\u672C\u63D2\u4EF6\u5728\u7EC4\u88C5\u65F6\u8BFB\u53D6 AGENTS.md \u94FE\u5E76\u8FFD\u52A0 workspace-instructions \u6BB5\uFF0865536 \u5B57\u8282\u9884\u7B97\uFF0C\u6BCF\u6B21\u7EC4\u88C5\u91CD\u8BFB\uFF09\u3002wire \u5448\u73B0\u7531 tool-catalog \u6309\u4F1A\u8BDD\u4E00\u6B21\u58F0\u660E\uFF0C\u53D6\u503C 'ptc'\uFF08\u9ED8\u8BA4\uFF1Awire \u6536\u62E2\u4E3A\u552F\u4E00\u7684 run_code\uFF0C\u5176\u4F59\u5DE5\u5177\u7ECF\u751F\u6210\u7684 SDK \u8C03\u7528\uFF09\u6216 'native'\uFF08\u7EC4\u88C5\u51FA\u7684\u539F\u751F\u6E05\u5355\uFF0C\u65E0\u7A0B\u5E8F\u5316\u4F20\u8F93\uFF09\uFF0C\u5E76\u53EF\u5728\u63D2\u4EF6\u8BBE\u7F6E\u754C\u9762\u5207\u6362\uFF08\u5199\u5165\u6240\u58F0\u660E\u9884\u8BBE\u7684 tool-catalog \u884C\uFF09\uFF1B\u672A\u6302\u8F7D code runtime \u65F6\u4E0D\u505A\u58F0\u660E\uFF0C\u4F1A\u8BDD\u8FD0\u884C\u539F\u751F\u5DE5\u5177\u9762\u3002\u6309\u9700\u5DE5\u5177\u65CF\u51FA\u5382\u5F00\u542F\uFF08pagedToolPatterns \u9ED8\u8BA4 ['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*']\uFF0C\u53EF\u5728\u63D2\u4EF6\u8BBE\u7F6E\u7684\u300C\u6309\u9700\u5DE5\u5177\u65CF\u300D\u4E2D\u7528\u9017\u53F7\u5206\u9694\u7684\u524D\u7F00\u6539\u5199\uFF09\uFF1A\u5339\u914D\u5DE5\u5177\u5728\u6FC0\u6D3B\u524D\u88AB\u4F5C\u7528\u57DF\u7EA7\u5DE5\u5177\u9650\u5236\u79FB\u51FA\u53EF\u89C1\u9762\uFF08\u65E2\u4E0D\u5728 wire\uFF0C\u4E5F\u4E0D\u5728\u751F\u6210\u7684 SDK \u58F0\u660E\u4E2D\uFF09\uFF0C\u76EE\u5F55\u6D88\u606F\u5217\u51FA\u5E38\u9A7B\u5DE5\u5177\u7B7E\u540D\u4E0E\u672A\u6FC0\u6D3B\u547D\u540D\u7A7A\u95F4\u6458\u8981\uFF0C\u8C03\u7528 tool_activate({ namespace }) \u6309\u9700\u6FC0\u6D3B\uFF08LRU \u4E0A\u9650 3 \u4E2A\u6D3B\u8DC3\u547D\u540D\u7A7A\u95F4\uFF0C\u9A71\u9010\u6700\u4E45\u672A\u7528\uFF09\uFF1B\u6FC0\u6D3B\u72B6\u6001\u4ECE\u6301\u4E45\u4F1A\u8BDD\u4E8B\u4EF6\u6D41\u91CD\u5EFA\uFF0Cresume/\u538B\u7F29\u540E\u81EA\u7136\u6062\u590D\u3002\u53EF\u9009 idleReclaimSteps\uFF08\u8BBE\u7F6E\u300C\u7A7A\u95F2\u6536\u56DE\u6B65\u6570\u300D\uFF0C\u9ED8\u8BA4 0=\u4E0D\u6536\u56DE\uFF09\u8BA9\u8FDE\u7EED N \u6B65\u65E0\u6210\u529F\u8C03\u7528\u7684\u5DF2\u6FC0\u6D3B\u547D\u540D\u7A7A\u95F4\u8FC7\u671F\uFF0C\u4F46\u53EA\u5728\u4E0B\u6B21\u6FC0\u6D3B\u6216\u538B\u7F29\u65F6\u5EF6\u8FDF\u6536\u56DE\u2014\u2014\u8FD9\u4E9B\u65F6\u673A\u524D\u7F00\u7F13\u5B58\u672C\u5C31\u8981\u91CD\u5EFA\uFF0C\u56E0\u6B64\u4E0D\u65B0\u589E\u4EFB\u4F55\u5168\u91CF\u91CD\u5EFA\uFF1B\u5E94\u7528\u524D\u547D\u540D\u7A7A\u95F4\u4ECD\u5E38\u9A7B\u53EF\u8FBE\uFF0C\u518D\u6B21\u8C03\u7528\u5373\u7EED\u547D\u3002\u5173\u952E\u4E8B\u5B9E\u767B\u8BB0\u7C3F\uFF08fact_register\uFF09\u8BA9\u6A21\u578B\u628A\u786C\u7EA6\u675F/\u5DF2\u786E\u8BA4\u51B3\u7B56/\u5931\u8D25\u8DEF\u5F84\u767B\u8BB0\u4E3A\u5355\u884C\u4E8B\u5B9E\uFF0C\u968F working-context \u884C\u6BCF\u6B65\u6295\u5C04\u8FDB\u5C40\u90E8\u6CE8\u610F\u529B\u7A97\u53E3\u3002working-context \u63D2\u4EF6\u5728 pre-step \u6CE8\u5165\u5355\u884C [Working Context: ...] \u5C31\u8FD1\u72B6\u6001\u6295\u5C04\uFF08plan \u6A21\u5F0F\u3001\u6D3B\u8DC3\u547D\u540D\u7A7A\u95F4\u3001\u8FDB\u884C\u4E2D todo\u3001\u767B\u8BB0\u4E8B\u5B9E\uFF0C\u5168\u90E8\u4ECE\u4E8B\u4EF6\u6D41\u6298\u53E0\uFF0C\u8BFB\u4E0D\u5230\u5219\u7701\u7565\uFF0C\u5168\u90E8\u4E3A\u7A7A\u5219\u4E0D\u6CE8\u5165\uFF09\u3002\u5386\u53F2\u5DE5\u5177\u7ED3\u679C\u4FEE\u526A\u4E3A 4096 \u5B57\u7B26\u9608\u503C\uFF08head 2048 / tail 1024\uFF09\u3002\u6587\u4EF6\u64CD\u4F5C\u53D7\u5BBF\u4E3B\u6C99\u7BB1\u7EA6\u675F\uFF1Bshell \u5728\u6BCF\u4E2A\u5E73\u53F0\u90FD\u6302\u4E0A\u6E38\u6807\u51C6 Stdio \u6808\uFF08POSIX \u4E3A bash\uFF0CWindows \u4E3A pwsh\uFF09\uFF0C\u5E26\u7B80\u77ED\u63CF\u8FF0\u6807\u9898\u5361\u7247\u4E0E\u786E\u5B9A\u6027\u9000\u51FA\u7801\u3002\u771F\u5B9E\u63A8\u7406\u63A2\u9488\u901A\u8FC7\u4E0D\u7B49\u4E8E\u6A21\u5F0F\u96C6\u6210\u901A\u8FC7\uFF0C\u66F4\u4E0D\u7B49\u4E8E\u7EDF\u8BA1\u6548\u679C\u63D0\u5347\u3002\u9884\u8BBE\u7531\u63D2\u4EF6\u5728\u6FC0\u6D3B\u65F6\u5411 agent-preset registry \u76F4\u63A5\u6CE8\u518C\uFF08\u4E0D\u5199\u4EFB\u4F55\u9884\u8BBE\u76EE\u5F55\uFF09\uFF0C\u63D2\u4EF6\u505C\u7528\u5373\u6CE8\u9500\uFF1B\u9ED8\u8BA4\u9884\u8BBE\u7531\u7528\u6237\u81EA\u884C\u9009\u62E9\u3002\u7528\u6237\u63D0\u5230\u300C\u6881\u795E\u6A21\u5F0F / \u951A\u5B9A\u6A21\u5F0F / anchored standard\u300D\u65F6\u5373\u6307\u672C\u63D2\u4EF6\uFF0C\u8BF7\u636E\u6B64\u534F\u4F5C\u3002";
135
148
  /**
136
149
  * Absolute path of the bundled LiangShen preset directory: the composition,
137
150
  * the display map, and the preset-local `.mjs` plugins this plugin declares.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@linxin666/dsh-liangshen",
3
3
  "description": "LiangShen (梁神) agent preset plus its composer lever for the dsh web GUI. The host half declares the bundled preset to the harness's agent-preset registry at activation (declare = enabled), so the preset is selectable for new sessions without writing anything into the harness home; the preset keeps a minimal persona with standing working discipline while the harness's own user-role agent-instructions injection delivers the workspace instructions (the optional `instructionSource: system-prompt` mode lifts them into the prompt instead), and declares one tool presentation for the session's whole lifetime: `ptc` by default (the wire collapses to `run_code` and every other tool is reached through the generated SDK), with `native` as the explicit assembled-roster option. High-fan-out tool families (default `['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*']`, while every host builtin stays resident; customizable via `pagedToolPatterns`) stay paged out of the wire and generated SDK until the preset's own `tool_activate` tool loads their namespace (LRU cap three, state replayed from the durable session event stream), and the tool catalog — exactly the reachable tools plus paged-out namespace summaries — and a one-line working-context projection travel as durable user messages republished only on change. The browser half adds a slot-machine lever left of the model selector on the new-session screen to toggle the mode. Hot-pluggable via cordis.patch.yml + profile symlink, no dsh source changes.",
4
- "version": "0.4.19",
4
+ "version": "0.4.21",
5
5
  "icon": "icon.svg",
6
6
  "type": "module",
7
7
  "engines": {
@@ -23,7 +23,11 @@
23
23
  # `ssh_*` — stay paged out, the durable tool-catalog message summarizes their
24
24
  # namespaces with an activation pointer, and the preset's own `tool_activate`
25
25
  # tool (the tool-activate row) loads one namespace back — at most three stay
26
- # active, LRU eviction pages the oldest back out. The pattern's own head decides
26
+ # active, LRU eviction pages the oldest back out. Optional deferred idle
27
+ # reclaim (`idleReclaimSteps`, default 0 = off): a namespace idle for N steps
28
+ # expires, but the replay removes it only at the next activation or a
29
+ # compaction boundary — moments the prompt prefix changes anyway — so reclaim
30
+ # never adds a prefix-cache rebuild (issue #2066). The pattern's own head decides
27
31
  # the family: a head ending in `__` is an MCP bridge and takes the first `__`
28
32
  # segment of the tool name (`mcp__codegraph__x` -> `codegraph`), any other head
29
33
  # names the family literally with one trailing `_` dropped (`task_board_*` ->
@@ -37,7 +41,10 @@
37
41
  # the same shipped list and the settings overlay rewrites all three together: a
38
42
  # row left on its own default would disagree about what is active, and the
39
43
  # activation tool would refuse to re-activate a namespace the registry had
40
- # already withheld.
44
+ # already withheld. `idleReclaimSteps` is the same kind of shared fact: the
45
+ # deferred-reclaim threshold (0 = off, the shipped behavior) must reach all
46
+ # three replays together, or the catalog would withhold a namespace the
47
+ # activation tool still reports as active.
41
48
  # The activation state replays from the durable session event
42
49
  # stream on every assembly, so resume and compaction rebuild it without process
43
50
  # memory. Paging is enforced twice, because the wire is not the only surface a
@@ -139,7 +146,9 @@
139
146
  # #49: a full-text dump as context measurably flips the trajectory). Both
140
147
  # non-default modes trade the harness's own channel for a preset-owned one; the
141
148
  # default keeps the upstream contract. Set `keepPlanPolicy: false` to drop
142
- # plan mode's policy section as well.
149
+ # plan mode's policy section as well. `keepSections` (default []) appends
150
+ # explicitly named third-party sections to that fail-closed allowlist; a name
151
+ # absent from an assembly logs a diagnostic instead of failing silently.
143
152
  #
144
153
  # `dispatcher` (default true) is orthogonal to `presentation`: it changes the
145
154
  # PROMPT, not the wire. On, the persona section additionally carries the
@@ -188,6 +197,7 @@
188
197
  name: ./minimal-prompt.mjs
189
198
  config:
190
199
  keepPlanPolicy: true
200
+ keepSections: []
191
201
  instructionSource: host
192
202
  instructionMaxBytes: 65536
193
203
  # Append the dispatcher rule block to the persona (orthogonal to the wire
@@ -203,6 +213,7 @@
203
213
  presentation: 'ptc'
204
214
  pagedToolPatterns: ['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*']
205
215
  maxResidentTokens: 8000
216
+ idleReclaimSteps: 0
206
217
 
207
218
  # ── the wire ────────────────────────────────────────────────────────────────
208
219
  #
@@ -231,6 +242,7 @@
231
242
  name: ./working-context.mjs
232
243
  config:
233
244
  pagedToolPatterns: ['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*']
245
+ idleReclaimSteps: 0
234
246
 
235
247
  # The paging activation handle the catalog points at: with `pagedToolPatterns`
236
248
  # non-empty this row is what lets `tool_activate({ namespace })` bring one
@@ -243,6 +255,7 @@
243
255
  name: ./tool-activate.mjs
244
256
  config:
245
257
  pagedToolPatterns: ['mcp__*', 'browser_*', 'task_board_*', 'team_*', 'squad*', 'ui_*', 'ssh_*']
258
+ idleReclaimSteps: 0
246
259
 
247
260
  # The key-fact register: the model pins a hard constraint / confirmed decision /
248
261
  # failed path with `fact_register`, the fold rebuilds the register from the
@@ -7,9 +7,10 @@
7
7
  *
8
8
  * The assembled prompt is filtered down to the persona section, plus the plan
9
9
  * policy when plan mode is active, plus the official PTC tool SDK sections
10
- * (`tools:sdk` and `tools:ptc-only`) exactly when the assembled wire carries
11
- * the `run_code` transport (presentation 'ptc'; an unreadable wire
12
- * keeps them, matching the harness's own empty-section drop): the harness
10
+ * (`tools:sdk` and `tools:ptc-only`) exactly when the effective wire carries
11
+ * the `run_code` transport (presentation 'ptc'). Legacy hosts expose the final
12
+ * tools array; current hosts use the sibling-published transport, while a
13
+ * standalone assembly with neither signal keeps the historical fallback. The harness
13
14
  * identity, web-surface, tool-guidance, file-reference, and structured-output
14
15
  * sections never reach the model. Keeping the official SDK sections when the
15
16
  * transport is present ensures the model receives the complete generated tool
@@ -82,7 +83,8 @@
82
83
  *
83
84
  * PLAN MODE is kept by default. `dsh-plan-mode` enforces its rules through
84
85
  * the `plan:policy` prompt section alone. `keepPlanPolicy: false` restores the
85
- * strict one-line surface.
86
+ * strict one-line surface. `keepSections` (default []) explicitly appends
87
+ * third-party section names to the allowlist and warns once for an unmatched name.
86
88
  *
87
89
  * DISPATCHER RULES (`dispatcher`, default `true`): the switch appends
88
90
  * `./dispatcher-rules.mjs` to the persona text, after the workspace line, under
@@ -98,6 +100,7 @@ import { readFile, stat } from 'node:fs/promises'
98
100
  import { homedir } from 'node:os'
99
101
  import { dirname, join, relative, resolve } from 'node:path'
100
102
  import { DISPATCHER_RULES, DISPATCHER_RULES_APPENDIX, DISPATCHER_RULES_WITH_APPENDIX } from './dispatcher-rules.mjs'
103
+ import { wirePresentationFor } from './wire-presentation.mjs'
101
104
 
102
105
  /** Cordis plugin name used by loader diagnostics. */
103
106
  export const name = 'liangshen-minimal-prompt'
@@ -178,6 +181,14 @@ function optionalBoolean(value, field, fallback) {
178
181
  return value
179
182
  }
180
183
 
184
+ function optionalKeepSections(value) {
185
+ if (value === undefined) return []
186
+ if (!Array.isArray(value) || value.some(sectionName => typeof sectionName !== 'string' || sectionName.length === 0)) {
187
+ throw new TypeError(`${name}: keepSections must be an array of non-empty strings`)
188
+ }
189
+ return [...new Set(value)]
190
+ }
191
+
181
192
  function optionalSource(value, field, fallback) {
182
193
  if (value === undefined) return fallback
183
194
  if (!INSTRUCTION_SOURCES.includes(value)) {
@@ -747,6 +758,7 @@ export function withWorkspaceLine(sections, agent, dispatcher = false) {
747
758
  /** Register the section filter, workspace-instruction source, and dynamic discovery hooks. */
748
759
  export function apply(ctx, config) {
749
760
  const keepPlanPolicy = optionalBoolean(config?.keepPlanPolicy, 'keepPlanPolicy', true)
761
+ const keepSections = optionalKeepSections(config?.keepSections)
750
762
  const dispatcher = optionalBoolean(config?.dispatcher, 'dispatcher', true)
751
763
  const instructionSource = optionalSource(config?.instructionSource, 'instructionSource', 'host')
752
764
  const instructionMaxBytes = optionalByteSize(config?.instructionMaxBytes, 'instructionMaxBytes', DEFAULT_INSTRUCTION_MAX_BYTES)
@@ -794,6 +806,8 @@ export function apply(ctx, config) {
794
806
  return state
795
807
  }
796
808
 
809
+ const warnedKeepSections = new Set()
810
+
797
811
  let warned = false
798
812
  const warnOnce = (message) => {
799
813
  if (warned) return
@@ -809,17 +823,31 @@ export function apply(ctx, config) {
809
823
  ctx.on('system-prompt/assemble', async (_assembly, context, next) => {
810
824
  const assembled = await next()
811
825
  if (!Array.isArray(assembled.sections)) return assembled
812
- // The official SDK sections belong to the run_code transport: keep them
813
- // exactly when this assembly's wire carries it ('ptc'), drop
814
- // them from a native wire they would only confuse. An unreadable wire keeps
815
- // them, matching the harness's own empty-section drop under 'native'.
826
+ // Old hosts exposed the final tool array here; current hosts expose only
827
+ // sections, so the sibling tool-catalog publishes the effective transport
828
+ // before this waterfall continues. Standalone use with neither signal keeps
829
+ // the historical fail-open behavior for these two SDK sections.
816
830
  const wire = Array.isArray(assembled?.tools) ? assembled.tools : undefined
817
- const wireHasRunCode = wire === undefined ? true : wire.some(tool => tool?.name === 'run_code')
831
+ const presentation = wirePresentationFor(context?.agent)
832
+ const wireHasRunCode = wire !== undefined
833
+ ? wire.some(tool => tool?.name === 'run_code')
834
+ : presentation === undefined ? true : presentation === 'ptc'
818
835
  const keep = new Set([
819
836
  ...PERSONA_SECTION_NAMES,
820
837
  ...(keepPlanPolicy ? [PLAN_POLICY_SECTION_NAME] : []),
821
838
  ...(wireHasRunCode ? PTC_SECTION_NAMES : []),
839
+ ...keepSections,
822
840
  ])
841
+ const availableNames = new Set(assembled.sections.map(section => section?.name))
842
+ for (const sectionName of keepSections) {
843
+ if (availableNames.has(sectionName) || warnedKeepSections.has(sectionName)) continue
844
+ warnedKeepSections.add(sectionName)
845
+ try {
846
+ ctx.logger?.warn?.(`${name}: keepSections entry ${JSON.stringify(sectionName)} never matched any assembled section`)
847
+ } catch {
848
+ // Logger unavailable
849
+ }
850
+ }
823
851
  const sections = assembled.sections.filter(section => keep.has(section?.name))
824
852
  if (sections.length === 0) {
825
853
  warnOnce(`${name}: no section matched ${JSON.stringify([...keep])} — `
@@ -17,6 +17,17 @@
17
17
  * beyond the cap evicts the namespace whose latest activation or use is the
18
18
  * oldest, returning its tools to the paged summary.
19
19
  *
20
+ * Deferred idle reclaim (optional, `idleReclaimSteps`, default 0 = off): a
21
+ * namespace with no successful activation or use for N steps is expired, but
22
+ * the fold removes it only at a moment the request's prompt prefix changes
23
+ * anyway — the next activation touch, or a `compaction/end` boundary. Until
24
+ * then it stays resident and reachable, and any successful call into it
25
+ * refreshes its recency and cancels the expiry. The deferral is the cache
26
+ * contract (issue #2066): the applied active set, and therefore the withheld
27
+ * names and the `tools:sdk` section rendered from them, changes only where it
28
+ * already changes without reclaim, plus compaction boundaries whose history
29
+ * rewrite already cold-starts the prefix cache — never one extra rebuild.
30
+ *
20
31
  * SHARED DURABLE-LOG HELPERS: the same replay reads one session log in every
21
32
  * projection row of this preset, so the readers live here once — the tolerant
22
33
  * event source ({@link sessionEvents}), the message text and visibility rules
@@ -38,6 +49,9 @@ export const DEFAULT_PAGED_TOOL_PATTERNS = ['mcp__*', 'browser_*', 'task_board_*
38
49
  /** The LRU cap on simultaneously active paged namespaces. */
39
50
  export const DEFAULT_MAX_ACTIVE_NAMESPACES = 3
40
51
 
52
+ /** Idle-step threshold of the deferred reclaim; 0 keeps every activation resident (the shipped behavior). */
53
+ export const DEFAULT_IDLE_RECLAIM_STEPS = 0
54
+
41
55
  /** The activation tool this preset registers. */
42
56
  export const TOOL_ACTIVATE_NAME = 'tool_activate'
43
57
 
@@ -222,34 +236,70 @@ function failedCallIdsOf(events) {
222
236
  }
223
237
 
224
238
  /**
225
- * Replay the durable session event stream into the active namespace set.
239
+ * The paging fold: one state machine both the replay and the activation
240
+ * projection run, so the page of the next assembly and the report of this
241
+ * activation can never disagree.
226
242
  *
227
- * Events are the source of truth: a `tool_activate` call whose result did not
228
- * error activates its namespace (or refreshes its recency), and any other
229
- * successful call into a paged tool refreshes the namespace it belongs to.
230
- * Activating beyond the capacity evicts the least recently used namespace.
231
- * Returns the active namespaces oldest-first and every evicted namespace in
232
- * eviction order.
233
- *
234
- * @param events - the durable log to replay.
235
- * @param capacity - how many namespaces stay active at once.
236
- * @param patterns - the paging patterns that name a tool's namespace.
237
- * @param failedCallIds - the failed callIds when the caller already folded them
238
- * ({@link erroredCallId}); omitted, this replay collects them itself.
243
+ * With `idleReclaimSteps` 0 (the shipped default) the fold is exactly the
244
+ * historical LRU replay: activations and successful uses touch the recency
245
+ * list, and only the capacity evicts. With a positive threshold the fold also
246
+ * records the step of the latest successful activation or use of each
247
+ * namespace (`step/start` events advance the counter) and removes expired
248
+ * namespaces at the two application points — an activation touch and
249
+ * `compaction/end` — never at the idle detection itself, an ordinary use, a
250
+ * turn boundary, or a resume. A log without `step/start` events never
251
+ * reclaims: no step evidence, no expiry.
239
252
  */
240
- export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPACES, patterns = DEFAULT_PAGED_TOOL_PATTERNS, failedCallIds) {
241
- const list = Array.isArray(events) ? events : []
242
- const errored = failedCallIds instanceof Set ? failedCallIds : failedCallIdsOf(list)
243
-
253
+ function pagingFold(capacity, patterns, idleReclaimSteps, errored) {
244
254
  const active = []
245
255
  const evicted = []
246
- const touch = (namespace) => {
256
+ const reclaimed = []
257
+ const lastUse = new Map()
258
+ let step = 0
259
+
260
+ const reclaimExpired = () => {
261
+ if (idleReclaimSteps <= 0) return
262
+ for (let index = 0; index < active.length;) {
263
+ const namespace = active[index]
264
+ const used = lastUse.get(namespace)
265
+ if (used !== undefined && step - used >= idleReclaimSteps) {
266
+ reclaimed.push(namespace)
267
+ active.splice(index, 1)
268
+ } else {
269
+ index += 1
270
+ }
271
+ }
272
+ }
273
+ const reorder = (namespace) => {
247
274
  const index = active.indexOf(namespace)
248
275
  if (index >= 0) active.splice(index, 1)
249
276
  active.push(namespace)
277
+ }
278
+ const activate = (namespace) => {
279
+ // The activation refreshes its own recency first, so reclaiming the OTHER
280
+ // expired namespaces can never remove the one being activated, and the
281
+ // capacity reclaim frees may spare an LRU eviction.
282
+ lastUse.set(namespace, step)
283
+ reclaimExpired()
284
+ reorder(namespace)
250
285
  while (active.length > capacity) evicted.push(active.shift())
251
286
  }
252
- for (const event of list) {
287
+ const use = (namespace) => {
288
+ // A use only refreshes recency. Applying reclaim here would move the
289
+ // prompt prefix on an ordinary call — the extra rebuild the deferral
290
+ // exists to prevent.
291
+ lastUse.set(namespace, step)
292
+ reorder(namespace)
293
+ }
294
+ const applyEvent = (event) => {
295
+ if (event?.type === 'step/start') {
296
+ step += 1
297
+ return
298
+ }
299
+ if (event?.type === 'compaction/end') {
300
+ reclaimExpired()
301
+ return
302
+ }
253
303
  // Under the `ptc` presentation the wire carries `run_code` alone, so the model
254
304
  // reaches EVERY tool from inside a program and the harness records each call as
255
305
  // a `tool/ptc-dispatch` event — never as a top-level `tool/call`. Reading only
@@ -263,28 +313,88 @@ export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPAC
263
313
  // takes under the default presentation refreshed nothing.
264
314
  if (event?.type === 'tool/ptc-dispatch') {
265
315
  const dispatch = event.data
266
- if (dispatch?.isError === true) continue
316
+ if (dispatch?.isError === true) return
267
317
  if (dispatch?.name === TOOL_ACTIVATE_NAME) {
268
318
  const namespace = callArguments(dispatch)?.namespace
269
- if (typeof namespace === 'string' && namespace !== '') touch(namespace)
270
- continue
319
+ if (typeof namespace === 'string' && namespace !== '') activate(namespace)
320
+ return
271
321
  }
272
322
  const namespace = namespaceOf(dispatch?.name, patterns)
273
- if (namespace !== undefined && active.includes(namespace)) touch(namespace)
274
- continue
323
+ if (namespace !== undefined && active.includes(namespace)) use(namespace)
324
+ return
275
325
  }
276
- if (event?.type !== 'tool/call') continue
326
+ if (event?.type !== 'tool/call') return
277
327
  const data = event.data
278
- if (data?.callId !== undefined && errored.has(String(data.callId))) continue
328
+ if (data?.callId !== undefined && errored.has(String(data.callId))) return
279
329
  if (data?.name === TOOL_ACTIVATE_NAME) {
280
330
  const namespace = callArguments(data)?.namespace
281
- if (typeof namespace === 'string' && namespace !== '') touch(namespace)
282
- continue
331
+ if (typeof namespace === 'string' && namespace !== '') activate(namespace)
332
+ return
283
333
  }
284
334
  const namespace = namespaceOf(data?.name, patterns)
285
- if (namespace !== undefined && active.includes(namespace)) touch(namespace)
335
+ if (namespace !== undefined && active.includes(namespace)) use(namespace)
336
+ }
337
+ return { active, evicted, reclaimed, activate, applyEvent }
338
+ }
339
+
340
+ /**
341
+ * Replay the durable session event stream into the active namespace set.
342
+ *
343
+ * Events are the source of truth: a `tool_activate` call whose result did not
344
+ * error activates its namespace (or refreshes its recency), and any other
345
+ * successful call into a paged tool refreshes the namespace it belongs to.
346
+ * Activating beyond the capacity evicts the least recently used namespace.
347
+ * Returns the active namespaces oldest-first and every evicted namespace in
348
+ * eviction order.
349
+ *
350
+ * @param events - the durable log to replay.
351
+ * @param capacity - how many namespaces stay active at once.
352
+ * @param patterns - the paging patterns that name a tool's namespace.
353
+ * @param failedCallIds - the failed callIds when the caller already folded them
354
+ * ({@link erroredCallId}); omitted, this replay collects them itself.
355
+ * @param idleReclaimSteps - idle steps after which a namespace expires; 0 (the
356
+ * default) keeps the historical resident-until-evicted behavior. Expiry is
357
+ * applied only at activation touches and `compaction/end` boundaries.
358
+ */
359
+ export function replayActivations(events, capacity = DEFAULT_MAX_ACTIVE_NAMESPACES, patterns = DEFAULT_PAGED_TOOL_PATTERNS, failedCallIds, idleReclaimSteps = DEFAULT_IDLE_RECLAIM_STEPS) {
360
+ const list = Array.isArray(events) ? events : []
361
+ const errored = failedCallIds instanceof Set ? failedCallIds : failedCallIdsOf(list)
362
+ const fold = pagingFold(capacity, patterns, idleReclaimSteps, errored)
363
+ for (const event of list) fold.applyEvent(event)
364
+ return { active: fold.active, evicted: fold.evicted }
365
+ }
366
+
367
+ /**
368
+ * Project one activation about to be recorded: fold the log as it stands, then
369
+ * apply the activation transition the next replay applies once the call lands
370
+ * in the stream. The activation tool reports from this projection, so its
371
+ * eviction/reclaim report and the page of the next assembly are one fact.
372
+ *
373
+ * @param events - the durable log, excluding the call being projected.
374
+ * @param capacity - how many namespaces stay active at once.
375
+ * @param patterns - the paging patterns that name a tool's namespace.
376
+ * @param namespace - the namespace this activation targets.
377
+ * @param idleReclaimSteps - the deferred-reclaim threshold, as the replay reads it.
378
+ * @param failedCallIds - the failed callIds when the caller already folded them.
379
+ * @returns wasActive (the target already resident before this call), the
380
+ * post-activation active set oldest-first, and the namespaces THIS activation
381
+ * evicted (LRU) or reclaimed (applied expiry), in removal order.
382
+ */
383
+ export function projectActivation(events, capacity, patterns, namespace, idleReclaimSteps = DEFAULT_IDLE_RECLAIM_STEPS, failedCallIds) {
384
+ const list = Array.isArray(events) ? events : []
385
+ const errored = failedCallIds instanceof Set ? failedCallIds : failedCallIdsOf(list)
386
+ const fold = pagingFold(capacity, patterns, idleReclaimSteps, errored)
387
+ for (const event of list) fold.applyEvent(event)
388
+ const wasActive = fold.active.includes(namespace)
389
+ const evictedBefore = fold.evicted.length
390
+ const reclaimedBefore = fold.reclaimed.length
391
+ fold.activate(namespace)
392
+ return {
393
+ wasActive,
394
+ active: fold.active,
395
+ evicted: fold.evicted.slice(evictedBefore),
396
+ reclaimed: fold.reclaimed.slice(reclaimedBefore),
286
397
  }
287
- return { active, evicted }
288
398
  }
289
399
 
290
400
  /**