@chaoset/adaptive-perf 0.3.1 → 0.3.2

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.
Files changed (2) hide show
  1. package/lib/index.mjs +59 -27
  2. package/package.json +1 -1
package/lib/index.mjs CHANGED
@@ -42,7 +42,9 @@
42
42
  * - agent.ctx.systemPrompt.suppressRuntimeContext() 抑制上下文快照
43
43
  * - agent.ctx.tools.schemas(agent) 读取该 agent 当前可见工具(限制交集)
44
44
  * - agent.ctx.tools.restrict({ deny }) 按 agent 作用域裁剪继承工具
45
- * - system-prompt/assemble(waterfall) 按会话阶段收窄装配后的工具目录
45
+ * - system-prompt/assemble(waterfall) 请求装配(早期实现;bootstrap 目录
46
+ * 收窄改用 tools.restrict——PTC 模式下 assembly.tools 只有 run_code,
47
+ * 过滤会降级失效,而 restrict 同时驱动 API 目录与 PTC SDK 参考段)
46
48
  * - agent/pre-step(waterfall) 剥离自动注入的上下文消息(source.kind)
47
49
  * - agent/request(waterfall) 首轮输出预算封顶
48
50
  * - session/event 增量喂入持久事件(晋升 / compaction 纪元)
@@ -520,6 +522,8 @@ export function apply(ctx, config) {
520
522
  agent,
521
523
  suppressDisposer: null,
522
524
  familyDisposers: new Map(),
525
+ bootstrapDisposer: null,
526
+ bootstrapKey: null,
523
527
  escalated: new Set(),
524
528
  visibleNames: new Set(),
525
529
  };
@@ -537,6 +541,7 @@ export function apply(ctx, config) {
537
541
  agents.set(agent.id, a);
538
542
  applySuppression(a);
539
543
  applyFamilies(a);
544
+ syncBootstrap(a);
540
545
  info(`agent ${agent.id}: adaptive-perf active (preset ${String(agentPresets.composedPreset(agent.ctx))})`);
541
546
  }
542
547
 
@@ -549,6 +554,9 @@ export function apply(ctx, config) {
549
554
  if (a.suppressDisposer !== null) {
550
555
  try { a.suppressDisposer(); } catch {}
551
556
  }
557
+ if (a.bootstrapDisposer !== null) {
558
+ try { a.bootstrapDisposer(); } catch {}
559
+ }
552
560
  for (const disposer of a.familyDisposers.values()) {
553
561
  try { disposer(); } catch {}
554
562
  }
@@ -635,37 +643,60 @@ export function apply(ctx, config) {
635
643
  return isTarget(agent);
636
644
  }
637
645
 
638
- // 持久事件增量喂入:晋升信号 / compaction 纪元。
646
+ /**
647
+ * bootstrap 阶段的保留工具集:bootstrap 工具对 + PTC 的直接调用工具
648
+ * (run_code)+ compaction 后恢复期的工作集。已晋升返回 null。
649
+ *
650
+ * 注意:不能靠过滤 system-prompt/assemble 的 assembly.tools 来实现——
651
+ * PTC 模式下 assembly.tools 只有 [run_code],Minimal 工具对不在其中,
652
+ * 过滤会触发降级(实测首轮模型仍看到 15 个工具)。正确做法是
653
+ * tools.restrict(与族限制同一机制):它同时驱动 API 目录和 PTC 的
654
+ * SDK 参考段,且天然保留 run_code(不在 deny 里)。
655
+ */
656
+ function bootstrapKeepSet(a) {
657
+ const phase = bootstrapPhaseOf(a.agent);
658
+ if (phase.promoted) return null;
659
+ const keep = new Set(cfg.bootstrap.tools);
660
+ if (a.visibleNames.has('run_code')) keep.add('run_code');
661
+ if (phase.boundary >= 0) for (const name of cfg.bootstrap.compactionTools) keep.add(name);
662
+ return keep;
663
+ }
664
+
665
+ /** 按当前阶段同步 bootstrap 的临时 restrict(幂等;仅阶段/配置变化时重挂)。 */
666
+ function syncBootstrap(a) {
667
+ const keep = bootstrapKeepSet(a);
668
+ const key = keep === null ? null : [...keep].sort().join(',');
669
+ if (key === a.bootstrapKey) return;
670
+ if (a.bootstrapDisposer !== null) {
671
+ try { a.bootstrapDisposer(); } catch {}
672
+ a.bootstrapDisposer = null;
673
+ a.bootstrapKey = null;
674
+ }
675
+ if (keep === null) return;
676
+ const deny = [...a.visibleNames].filter((name) => !keep.has(name));
677
+ if (deny.length === 0) return;
678
+ try {
679
+ a.bootstrapDisposer = a.agent.ctx.tools.restrict({ deny });
680
+ a.bootstrapKey = key;
681
+ info(`agent ${a.agent.id}: bootstrap restrict deny=${deny.length} keep=[${[...keep].join(',')}]`);
682
+ } catch (error) {
683
+ warn(`bootstrap restrict failed for agent ${a.agent.id}: ${String((error && error.message) || error)}`);
684
+ }
685
+ }
686
+
687
+ // 持久事件增量喂入:晋升信号 / compaction 纪元;阶段变化时同步 bootstrap 限制。
639
688
  ctx.on('session/event', (session, event) => {
640
689
  if (session === null || typeof session !== 'object' || typeof session.id !== 'string') return;
690
+ const before = bootstrapState.get(session.id);
641
691
  observePhase(bootstrapState, session.id, event);
642
- });
643
-
644
- // 请求装配时按阶段收窄工具目录。请求 #1(未晋升):只保留 bootstrap
645
- // 工具对(compaction/end 之后额外放行 compactionTools 核心工作集);
646
- // 已晋升:放行(leanByDefault 的族限制仍由 tools.restrict 生效)。
647
- ctx.on('system-prompt/assemble', async (assembly, _context, next) => {
648
- const resolved = await next();
649
- try {
650
- const agent = agentsService?.currentInitiator?.() ?? void 0;
651
- if (!bootstrapActiveFor(agent)) return resolved;
652
- const phase = bootstrapPhaseOf(agent);
653
- if (phase.promoted) return resolved;
654
- const keep = new Set(cfg.bootstrap.tools);
655
- if (phase.boundary >= 0) for (const name of cfg.bootstrap.compactionTools) keep.add(name);
656
- const filtered = filterBootstrapTools(resolved, keep);
657
- if (filtered.missing.length > 0) {
658
- // 目录缺失 bootstrap 工具对:降级为全目录(一次告警),绝不能
659
- // 把每个请求都卡死在空目录。
660
- warnBootstrapOnce(`bootstrap tools missing from catalog (${JSON.stringify(filtered.missing)}); exposing the full catalog`);
661
- return resolved;
692
+ const a = agents.get(session.id);
693
+ if (a !== void 0) {
694
+ const after = bootstrapState.get(session.id);
695
+ if (before === void 0 || after === void 0 || before.promoted !== after.promoted || before.boundary !== after.boundary) {
696
+ syncBootstrap(a);
662
697
  }
663
- return { ...resolved, tools: filtered.tools };
664
- } catch (error) {
665
- warnBootstrapOnce(`bootstrap tool filter failed, exposing the full catalog: ${String((error && error.message) || error)}`);
666
- return resolved;
667
698
  }
668
- }, { prepend: true });
699
+ });
669
700
 
670
701
  // 首轮剥离自动注入的上下文(技能目录提醒 / AGENTS.md 摘要)。prepend +
671
702
  // 根作用域注册保证是最后一道变换(后注册的注入者无法再补回)。
@@ -733,6 +764,7 @@ export function apply(ctx, config) {
733
764
  for (const a of agents.values()) {
734
765
  applySuppression(a);
735
766
  applyFamilies(a);
767
+ syncBootstrap(a);
736
768
  }
737
769
  },
738
770
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaoset/adaptive-perf",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "DSH host plugin: bring standard / PTC presets to minimal-mode-level performance via runtime-context suppression and adaptive tool-catalog trimming with demand-driven escalation",
5
5
  "type": "module",
6
6
  "license": "MIT",