dsh-context-compression-improved 0.13.2 → 0.13.3
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-context-compression-improved",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.3",
|
|
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",
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CardMerge: multi-card consolidation (F3 — 中刀).
|
|
3
|
+
*
|
|
4
|
+
* Plan: cci-longterm-refactor v0.13.4
|
|
5
|
+
*
|
|
6
|
+
* When the surface accumulates too many individual compressed cards (each ~100-300 tokens),
|
|
7
|
+
* CardMerge takes the OLDEST N cards and merges them into a single summary card.
|
|
8
|
+
* This is the "中刀" (medium knife) level between small-knife per-message cuts and
|
|
9
|
+
* big-knife (official/sediment) turn-level compression.
|
|
10
|
+
*
|
|
11
|
+
* Default OFF: the user must explicitly enable it. Panel requires double confirmation.
|
|
12
|
+
*
|
|
13
|
+
* @module dsh-context-compression-improved/runtime/card-merge
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export interface CardMergeSettings {
|
|
17
|
+
enabled: boolean
|
|
18
|
+
/** Minimum number of cards on surface before triggering (default 8). */
|
|
19
|
+
mergeMaxCards: number
|
|
20
|
+
/** Minimum total card tokens to justify merge (default 4000). */
|
|
21
|
+
mergeMinTokens: number
|
|
22
|
+
/** Break-even ratio: oneTimeRecompute / netGain must be <= this (default 2). */
|
|
23
|
+
mergeBreakEven: number
|
|
24
|
+
/** Cooldown between merges in ms (default 300000 = 5min). */
|
|
25
|
+
mergeCooldownMs: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const DEFAULT_CARD_MERGE_SETTINGS: CardMergeSettings = {
|
|
29
|
+
enabled: false,
|
|
30
|
+
mergeMaxCards: 8,
|
|
31
|
+
mergeMinTokens: 4_000,
|
|
32
|
+
mergeBreakEven: 2,
|
|
33
|
+
mergeCooldownMs: 300_000,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface CardInfo {
|
|
37
|
+
seq: number
|
|
38
|
+
tokens: number
|
|
39
|
+
/** The source reference(s) this card points to. */
|
|
40
|
+
sourceRefs: string[]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface MergePlan {
|
|
44
|
+
/** Cards to merge (oldest first). */
|
|
45
|
+
readonly cards: readonly CardInfo[]
|
|
46
|
+
readonly totalTokens: number
|
|
47
|
+
readonly estimatedOutputTokens: number
|
|
48
|
+
readonly netGain: number
|
|
49
|
+
readonly recomputeEstimate: number
|
|
50
|
+
readonly breakEvenOk: boolean
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Decide whether and how to merge cards. Returns null if merge should NOT happen.
|
|
55
|
+
*
|
|
56
|
+
* @param cards - All compressed cards currently on surface, ordered by seq (oldest first)
|
|
57
|
+
* @param settings - The card merge configuration
|
|
58
|
+
* @param tailTokens - Tokens on the surface AFTER the last card (rewrite tail)
|
|
59
|
+
*/
|
|
60
|
+
export function planCardMerge(
|
|
61
|
+
cards: readonly CardInfo[],
|
|
62
|
+
settings: CardMergeSettings,
|
|
63
|
+
tailTokens: number,
|
|
64
|
+
): MergePlan | null {
|
|
65
|
+
if (!settings.enabled) return null
|
|
66
|
+
if (cards.length < settings.mergeMaxCards) return null
|
|
67
|
+
|
|
68
|
+
// Take the oldest N cards (up to chunkMax equivalent) for merging
|
|
69
|
+
const mergeable = cards.slice(0, Math.max(settings.mergeMaxCards, Math.floor(cards.length * 0.6)))
|
|
70
|
+
const totalTokens = mergeable.reduce((sum, c) => sum + c.tokens, 0)
|
|
71
|
+
|
|
72
|
+
if (totalTokens < settings.mergeMinTokens) return null
|
|
73
|
+
|
|
74
|
+
// Estimated output: roughly 15% of input (heuristic for good summary)
|
|
75
|
+
const estimatedOutputTokens = Math.ceil(totalTokens * 0.15) + 200
|
|
76
|
+
const netGain = totalTokens - estimatedOutputTokens
|
|
77
|
+
|
|
78
|
+
if (netGain <= 0) return null
|
|
79
|
+
|
|
80
|
+
// One-time recompute cost: the tail after the merged range must be resent
|
|
81
|
+
const recomputeEstimate = tailTokens
|
|
82
|
+
const breakEvenOk = recomputeEstimate / netGain <= settings.mergeBreakEven
|
|
83
|
+
|
|
84
|
+
if (!breakEvenOk) return null
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
cards: mergeable,
|
|
88
|
+
totalTokens,
|
|
89
|
+
estimatedOutputTokens,
|
|
90
|
+
netGain,
|
|
91
|
+
recomputeEstimate,
|
|
92
|
+
breakEvenOk: true,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Validate that decode of settings produces the correct shape.
|
|
98
|
+
*/
|
|
99
|
+
export function decodeCardMergeSettings(value: unknown): CardMergeSettings {
|
|
100
|
+
if (value === undefined || value === null) return { ...DEFAULT_CARD_MERGE_SETTINGS }
|
|
101
|
+
if (typeof value !== 'object' || Array.isArray(value)) return { ...DEFAULT_CARD_MERGE_SETTINGS }
|
|
102
|
+
const obj = value as Record<string, unknown>
|
|
103
|
+
return {
|
|
104
|
+
enabled: typeof obj.enabled === 'boolean' ? obj.enabled : DEFAULT_CARD_MERGE_SETTINGS.enabled,
|
|
105
|
+
mergeMaxCards: clampInt(obj.mergeMaxCards, 2, 50, DEFAULT_CARD_MERGE_SETTINGS.mergeMaxCards),
|
|
106
|
+
mergeMinTokens: clampInt(obj.mergeMinTokens, 500, 50_000, DEFAULT_CARD_MERGE_SETTINGS.mergeMinTokens),
|
|
107
|
+
mergeBreakEven: clampInt(obj.mergeBreakEven, 1, 10, DEFAULT_CARD_MERGE_SETTINGS.mergeBreakEven),
|
|
108
|
+
mergeCooldownMs: clampInt(obj.mergeCooldownMs, 60_000, 3_600_000, DEFAULT_CARD_MERGE_SETTINGS.mergeCooldownMs),
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function clampInt(value: unknown, min: number, max: number, fallback: number): number {
|
|
113
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return fallback
|
|
114
|
+
return Math.max(min, Math.min(max, Math.round(value)))
|
|
115
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* F3: CardMerge tests.
|
|
3
|
+
* @module dsh-context-compression-improved/tests/runtime/card-merge
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, it } from 'vitest'
|
|
6
|
+
import {
|
|
7
|
+
planCardMerge,
|
|
8
|
+
decodeCardMergeSettings,
|
|
9
|
+
DEFAULT_CARD_MERGE_SETTINGS,
|
|
10
|
+
type CardInfo,
|
|
11
|
+
type CardMergeSettings,
|
|
12
|
+
} from '../../src/runtime/card-merge.ts'
|
|
13
|
+
|
|
14
|
+
function makeCards(count: number, tokensEach = 500): CardInfo[] {
|
|
15
|
+
return Array.from({ length: count }, (_, i) => ({
|
|
16
|
+
seq: i + 1,
|
|
17
|
+
tokens: tokensEach,
|
|
18
|
+
sourceRefs: [`session://test/event/${i + 1}`],
|
|
19
|
+
}))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const ENABLED: CardMergeSettings = { ...DEFAULT_CARD_MERGE_SETTINGS, enabled: true }
|
|
23
|
+
|
|
24
|
+
describe('F3: planCardMerge decision logic', () => {
|
|
25
|
+
it('returns null when disabled', () => {
|
|
26
|
+
expect(planCardMerge(makeCards(20), DEFAULT_CARD_MERGE_SETTINGS, 5000)).toBeNull()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('returns null when card count < mergeMaxCards', () => {
|
|
30
|
+
expect(planCardMerge(makeCards(5), ENABLED, 5000)).toBeNull()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('returns null when total tokens < mergeMinTokens', () => {
|
|
34
|
+
// 8 cards × 400 tokens = 3200 < 4000
|
|
35
|
+
expect(planCardMerge(makeCards(8, 400), ENABLED, 5000)).toBeNull()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('returns plan when conditions met (8 cards × 600 tokens = 4800 > 4000)', () => {
|
|
39
|
+
const plan = planCardMerge(makeCards(8, 600), ENABLED, 2000)
|
|
40
|
+
expect(plan).not.toBeNull()
|
|
41
|
+
expect(plan!.netGain).toBeGreaterThan(0)
|
|
42
|
+
expect(plan!.breakEvenOk).toBe(true)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('rejects when breakEven fails (tail too large relative to gain)', () => {
|
|
46
|
+
// 8 × 600 = 4800 total, output ~920, netGain ~3880
|
|
47
|
+
// recompute 10000 → ratio = 10000/3880 ≈ 2.58 > mergeBreakEven(2) → reject
|
|
48
|
+
const plan = planCardMerge(makeCards(8, 600), ENABLED, 10_000)
|
|
49
|
+
expect(plan).toBeNull()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('cooldown is a settings concern (tested at integration level)', () => {
|
|
53
|
+
// planCardMerge itself has no temporal state — cooldown lives in orchestrator
|
|
54
|
+
expect(ENABLED.mergeCooldownMs).toBe(300_000)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('F3: decodeCardMergeSettings', () => {
|
|
59
|
+
it('undefined returns defaults with enabled=false', () => {
|
|
60
|
+
const settings = decodeCardMergeSettings(undefined)
|
|
61
|
+
expect(settings.enabled).toBe(false)
|
|
62
|
+
expect(settings.mergeMaxCards).toBe(8)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('explicit enabled=true is preserved', () => {
|
|
66
|
+
const settings = decodeCardMergeSettings({ enabled: true, mergeMaxCards: 10 })
|
|
67
|
+
expect(settings.enabled).toBe(true)
|
|
68
|
+
expect(settings.mergeMaxCards).toBe(10)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('out-of-range values are clamped', () => {
|
|
72
|
+
const settings = decodeCardMergeSettings({ enabled: true, mergeMaxCards: 100, mergeBreakEven: 0 })
|
|
73
|
+
expect(settings.mergeMaxCards).toBe(50) // clamped to max
|
|
74
|
+
expect(settings.mergeBreakEven).toBe(1) // clamped to min
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('non-object input returns defaults', () => {
|
|
78
|
+
expect(decodeCardMergeSettings('invalid')).toEqual(DEFAULT_CARD_MERGE_SETTINGS)
|
|
79
|
+
expect(decodeCardMergeSettings(42)).toEqual(DEFAULT_CARD_MERGE_SETTINGS)
|
|
80
|
+
})
|
|
81
|
+
})
|