dsh-context-compression-improved 0.13.3 → 0.14.0

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": "dsh-context-compression-improved",
3
- "version": "0.13.3",
3
+ "version": "0.14.0",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org",
6
6
  "tag": "latest"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "dsh-external/dsh-context-compression-improved",
3
- "version": "0.13.3",
3
+ "version": "0.14.0",
4
4
  "main": "./lib/index.js",
5
5
  "description": "Token-saving, auditable tool-result context-compression selector with code-skeleton gate for DeepSeek Harness — fewer tokens on every turn",
6
6
  "engines": {
@@ -512,6 +512,14 @@ interface PrunerState {
512
512
  readonly intentFoldedSeqs: WeakMap<Session, Set<number>>;
513
513
  /** Live-surface tokens when the last intent fold landed (growth-gate baseline). */
514
514
  readonly intentBaselines: WeakMap<Session, number>;
515
+ /** F14 (v0.14.1): Incremental calls cache — rebuilt only when event count changes. */
516
+ readonly callsCache: WeakMap<Session, {
517
+ map: Map<string, {
518
+ name: string;
519
+ arguments: string;
520
+ }>;
521
+ eventCount: number;
522
+ }>;
515
523
  }
516
524
  //#endregion
517
525
  //#region src/runtime/token-count.d.ts
@@ -4759,7 +4759,8 @@ var ToolResultPruner = class extends Service {
4759
4759
  projectedSavings: /* @__PURE__ */ new WeakSet(),
4760
4760
  pendingIntentFolds: /* @__PURE__ */ new WeakMap(),
4761
4761
  intentFoldedSeqs: /* @__PURE__ */ new WeakMap(),
4762
- intentBaselines: /* @__PURE__ */ new WeakMap()
4762
+ intentBaselines: /* @__PURE__ */ new WeakMap(),
4763
+ callsCache: /* @__PURE__ */ new WeakMap()
4763
4764
  };
4764
4765
  try {
4765
4766
  ctx.on("session/disposed", (session) => {
@@ -6016,11 +6017,25 @@ var ToolResultPruner = class extends Service {
6016
6017
  }
6017
6018
  snapshot(session, view) {
6018
6019
  const events = sessionEvents(session);
6019
- const calls = /* @__PURE__ */ new Map();
6020
- for (const event of events) if (event.type === "tool/call") calls.set(event.data.callId, {
6021
- name: event.data.name,
6022
- arguments: event.data.arguments
6023
- });
6020
+ let cached = this.state.callsCache.get(session);
6021
+ if (cached === void 0 || cached.eventCount !== events.length) {
6022
+ const calls = /* @__PURE__ */ new Map();
6023
+ const startIndex = cached?.eventCount ?? 0;
6024
+ if (cached !== void 0 && cached.eventCount < events.length) for (const [key, value] of cached.map) calls.set(key, value);
6025
+ for (let i = cached !== void 0 ? startIndex : 0; i < events.length; i++) {
6026
+ const event = events[i];
6027
+ if (event.type === "tool/call") calls.set(event.data.callId, {
6028
+ name: event.data.name,
6029
+ arguments: event.data.arguments
6030
+ });
6031
+ }
6032
+ cached = {
6033
+ map: calls,
6034
+ eventCount: events.length
6035
+ };
6036
+ this.state.callsCache.set(session, cached);
6037
+ }
6038
+ const calls = cached.map;
6024
6039
  const candidates = [];
6025
6040
  const measured = new Map(view.measuredNodes.map((node) => [node.seq, node.count]));
6026
6041
  const projectionPrices = new Map(view.nodes.map((node) => [node.seq, node.tokens]));
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-context-compression-improved",
3
- "version": "0.13.3",
3
+ "version": "0.14.0",
4
4
  "description": "Token-saving context-compression selector bundle for DeepSeek Harness — fewer tool-result tokens on every turn, fully auditable",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -58,4 +58,6 @@ export interface PrunerState {
58
58
  readonly intentFoldedSeqs: WeakMap<Session, Set<number>>
59
59
  /** Live-surface tokens when the last intent fold landed (growth-gate baseline). */
60
60
  readonly intentBaselines: WeakMap<Session, number>
61
+ /** F14 (v0.14.1): Incremental calls cache — rebuilt only when event count changes. */
62
+ readonly callsCache: WeakMap<Session, { map: Map<string, { name: string; arguments: string }>; eventCount: number }>
61
63
  }
@@ -363,6 +363,7 @@ export class ToolResultPruner extends Service {
363
363
  pendingIntentFolds: new WeakMap(),
364
364
  intentFoldedSeqs: new WeakMap(),
365
365
  intentBaselines: new WeakMap(),
366
+ callsCache: new WeakMap(),
366
367
  }
367
368
 
368
369
  // 会话结束汇总(dsh-token-optimizer monitor 模式的同款体验):输出该会话的
@@ -2021,12 +2022,25 @@ export class ToolResultPruner extends Service {
2021
2022
 
2022
2023
  private snapshot(session: Session, view: CompactionTokenView): SnapshotCandidate[] {
2023
2024
  const events = sessionEvents(session)
2024
- const calls = new Map<string, ToolCallInfo>()
2025
- for (const event of events) {
2026
- if (event.type === 'tool/call') {
2027
- calls.set(event.data.callId, { name: event.data.name, arguments: event.data.arguments })
2025
+ // F14 incremental cache: rebuild calls map only when event count changes
2026
+ let cached = this.state.callsCache.get(session)
2027
+ if (cached === undefined || cached.eventCount !== events.length) {
2028
+ const calls = new Map<string, ToolCallInfo>()
2029
+ const startIndex = cached?.eventCount ?? 0
2030
+ // Start from existing map if available, only process new events
2031
+ if (cached !== undefined && cached.eventCount < events.length) {
2032
+ for (const [key, value] of cached.map) calls.set(key, value)
2033
+ }
2034
+ for (let i = cached !== undefined ? startIndex : 0; i < events.length; i++) {
2035
+ const event = events[i]!
2036
+ if (event.type === 'tool/call') {
2037
+ calls.set(event.data.callId, { name: event.data.name, arguments: event.data.arguments })
2038
+ }
2028
2039
  }
2040
+ cached = { map: calls, eventCount: events.length }
2041
+ this.state.callsCache.set(session, cached)
2029
2042
  }
2043
+ const calls = cached.map
2030
2044
  const candidates: SnapshotCandidate[] = []
2031
2045
  const measured = new Map(view.measuredNodes.map(node => [node.seq, node.count]))
2032
2046
  const projectionPrices = new Map(view.nodes.map(node => [node.seq, node.tokens]))
@@ -0,0 +1,32 @@
1
+ /**
2
+ * PTC disclosurePolicy awareness (F5).
3
+ *
4
+ * Plan: cci-longterm-refactor v0.13.5
5
+ *
6
+ * When the host preset uses disclosurePolicy='full', the first-turn schema
7
+ * is already ~30K tokens (dsh-ops #994 evidence). Aggressive tool-result
8
+ * trimming becomes less necessary because the per-request token overhead is
9
+ * dominated by schema, not tool output.
10
+ *
11
+ * This module provides the adjustment function: full → historyMinReclaim × 1.2.
12
+ *
13
+ * @module dsh-context-compression-improved/runtime/ptc-disclosure
14
+ */
15
+
16
+ export type DisclosurePolicy = 'on-demand' | 'full' | undefined
17
+
18
+ /**
19
+ * Compute the effective minReclaim based on disclosure policy.
20
+ * Under 'full', schema already eats the budget → relax tool-result aging by 20%.
21
+ *
22
+ * @param baseMinReclaim - The profile's base minReclaim in tokens
23
+ * @param policy - The disclosure policy as read from the host (undefined = unknown/old host)
24
+ * @returns The effective minReclaim value
25
+ */
26
+ export function effectiveMinReclaim(baseMinReclaim: number, policy: DisclosurePolicy): number {
27
+ if (policy === 'full') {
28
+ return Math.round(baseMinReclaim * 1.2)
29
+ }
30
+ // 'on-demand' or undefined (old host) → no change
31
+ return baseMinReclaim
32
+ }