dsh-context-compression-improved 0.13.0 → 0.13.1
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 +1 -1
- package/packages/selector/dsh.plugin.json +1 -1
- package/packages/selector/lib/profiles.d.ts +6 -0
- package/packages/selector/package.json +1 -1
- package/packages/selector/src/profiles.ts +6 -0
- package/packages/selector/src/runtime/injected-kinds.ts +62 -0
- package/packages/selector/src/runtime/netgain.ts +48 -0
- package/packages/selector/tests/runtime/injected-kinds.spec.ts +86 -0
- package/packages/selector/tests/runtime/netgain.spec.ts +61 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "dsh-external/dsh-context-compression-improved",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.1",
|
|
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": {
|
|
@@ -151,6 +151,12 @@ interface ContextCompressionSettings {
|
|
|
151
151
|
sediment?: SedimentSettings;
|
|
152
152
|
/** Optional tokenpilot-inspired sub-capability overrides (presence-validated only). */
|
|
153
153
|
presetOptions?: PresetOptionsSettings;
|
|
154
|
+
/**
|
|
155
|
+
* F1 (v0.13.2): Minimum net-gain threshold in tokens for small-knife cuts.
|
|
156
|
+
* A candidate is skipped if (tokensBefore - tokensAfter - cardOverhead) < minNetGain.
|
|
157
|
+
* 0 disables the check. Default 1000.
|
|
158
|
+
*/
|
|
159
|
+
minNetGain?: number;
|
|
154
160
|
}
|
|
155
161
|
//#endregion
|
|
156
162
|
export { PresetOptionsSettings as i, ContextCompressionSettings as n, CustomCompressionPolicy as r, CompressionProfile as t };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-context-compression-improved",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.1",
|
|
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",
|
|
@@ -451,6 +451,12 @@ export interface ContextCompressionSettings {
|
|
|
451
451
|
sediment?: SedimentSettings
|
|
452
452
|
/** Optional tokenpilot-inspired sub-capability overrides (presence-validated only). */
|
|
453
453
|
presetOptions?: PresetOptionsSettings
|
|
454
|
+
/**
|
|
455
|
+
* F1 (v0.13.2): Minimum net-gain threshold in tokens for small-knife cuts.
|
|
456
|
+
* A candidate is skipped if (tokensBefore - tokensAfter - cardOverhead) < minNetGain.
|
|
457
|
+
* 0 disables the check. Default 1000.
|
|
458
|
+
*/
|
|
459
|
+
minNetGain?: number
|
|
454
460
|
}
|
|
455
461
|
|
|
456
462
|
/**
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injected-source whitelist: identifies host-injected messages that must NOT
|
|
3
|
+
* be treated as real user boundaries when determining turns, spans, or
|
|
4
|
+
* working-set protection.
|
|
5
|
+
*
|
|
6
|
+
* Plan: cci-longterm-refactor v0.13.2 (F13 — live-compact #69 lesson)
|
|
7
|
+
*
|
|
8
|
+
* DSH host injects user/message events with a specific source.kind for
|
|
9
|
+
* time-context, tool-jobs, skill-catalog, etc. These are NOT user turns and
|
|
10
|
+
* must not affect span boundary decisions.
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-context-compression-improved/runtime/injected-kinds
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Source kinds that represent host-injected content, NOT real user messages.
|
|
17
|
+
* Any event whose data.source.kind is in this set is an injection and must
|
|
18
|
+
* be skipped by all boundary/turn detection logic.
|
|
19
|
+
*/
|
|
20
|
+
export const INJECTED_SOURCE_KINDS: ReadonlySet<string> = new Set([
|
|
21
|
+
'time-context',
|
|
22
|
+
'tool-jobs',
|
|
23
|
+
'skill-catalog',
|
|
24
|
+
'dsh-context-compression',
|
|
25
|
+
'dsh-ops-ptc',
|
|
26
|
+
'compaction-basic',
|
|
27
|
+
'dsh-agent-presets',
|
|
28
|
+
])
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Determine whether an event represents a real user message (not injected).
|
|
32
|
+
*
|
|
33
|
+
* An event is a "real user boundary" if:
|
|
34
|
+
* - type is 'user/message', AND
|
|
35
|
+
* - its source.kind (if present) is NOT in INJECTED_SOURCE_KINDS
|
|
36
|
+
*
|
|
37
|
+
* Events without a source field are treated as real user messages
|
|
38
|
+
* (backward-compatible with older hosts that don't tag sources).
|
|
39
|
+
*/
|
|
40
|
+
export function isRealUserBoundary(event: {
|
|
41
|
+
type?: unknown
|
|
42
|
+
data?: { source?: { kind?: unknown } }
|
|
43
|
+
}): boolean {
|
|
44
|
+
if (event.type !== 'user/message') return false
|
|
45
|
+
const kind = event.data?.source?.kind
|
|
46
|
+
if (typeof kind !== 'string') return true // no source = real user (old host compat)
|
|
47
|
+
return !INJECTED_SOURCE_KINDS.has(kind)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Check if a message event is a plugin injection (non-user boundary).
|
|
52
|
+
* Returns true for injected messages that should be skipped.
|
|
53
|
+
*/
|
|
54
|
+
export function isInjectedMessage(event: {
|
|
55
|
+
type?: unknown
|
|
56
|
+
data?: { source?: { kind?: unknown } }
|
|
57
|
+
}): boolean {
|
|
58
|
+
if (event.type !== 'user/message') return false
|
|
59
|
+
const kind = event.data?.source?.kind
|
|
60
|
+
if (typeof kind !== 'string') return false // no source = not injected
|
|
61
|
+
return INJECTED_SOURCE_KINDS.has(kind)
|
|
62
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Net-gain threshold check (F1).
|
|
3
|
+
*
|
|
4
|
+
* Plan: cci-longterm-refactor v0.13.2
|
|
5
|
+
*
|
|
6
|
+
* A cut is only worthwhile if the tokens saved (minus the cost of the
|
|
7
|
+
* replacement card/pointer) exceed a configurable minimum. This prevents
|
|
8
|
+
* the "切了个寂寞" degeneration where small outputs are cut into cards
|
|
9
|
+
* that cost almost as much as the original.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-context-compression-improved/runtime/netgain
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Fixed card overhead estimate: sourceRef + header + retrieve hint ≈ 80 tokens. */
|
|
15
|
+
const CARD_OVERHEAD_TOKENS = 80
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Check whether a candidate cut has sufficient net gain to be published.
|
|
19
|
+
*
|
|
20
|
+
* @param tokensBefore - tokens of the original content
|
|
21
|
+
* @param tokensAfter - tokens of the replacement card/pointer
|
|
22
|
+
* @param minNetGain - minimum required net savings (0 = disabled)
|
|
23
|
+
* @returns true if the cut should proceed, false if it should be skipped
|
|
24
|
+
*/
|
|
25
|
+
export function checkNetGain(
|
|
26
|
+
tokensBefore: number,
|
|
27
|
+
tokensAfter: number,
|
|
28
|
+
minNetGain: number,
|
|
29
|
+
): boolean {
|
|
30
|
+
// Feature disabled
|
|
31
|
+
if (minNetGain <= 0) return true
|
|
32
|
+
const netGain = tokensBefore - tokensAfter - CARD_OVERHEAD_TOKENS
|
|
33
|
+
return netGain >= minNetGain
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Compute the effective net gain of a proposed cut.
|
|
38
|
+
* Useful for logging/audit even when the cut is allowed.
|
|
39
|
+
*/
|
|
40
|
+
export function computeNetGain(
|
|
41
|
+
tokensBefore: number,
|
|
42
|
+
tokensAfter: number,
|
|
43
|
+
): number {
|
|
44
|
+
return tokensBefore - tokensAfter - CARD_OVERHEAD_TOKENS
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The card overhead constant (exported for tests). */
|
|
48
|
+
export { CARD_OVERHEAD_TOKENS }
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* F13: Injected-source whitelist filtering tests.
|
|
3
|
+
*
|
|
4
|
+
* @module dsh-context-compression-improved/tests/runtime/injected-kinds
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it } from 'vitest'
|
|
7
|
+
import { isInjectedMessage, isRealUserBoundary, INJECTED_SOURCE_KINDS } from '../../src/runtime/injected-kinds.ts'
|
|
8
|
+
|
|
9
|
+
describe('F13: INJECTED_SOURCE_KINDS whitelist', () => {
|
|
10
|
+
it('contains at least 5 known injection kinds', () => {
|
|
11
|
+
expect(INJECTED_SOURCE_KINDS.size).toBeGreaterThanOrEqual(5)
|
|
12
|
+
expect(INJECTED_SOURCE_KINDS.has('time-context')).toBe(true)
|
|
13
|
+
expect(INJECTED_SOURCE_KINDS.has('tool-jobs')).toBe(true)
|
|
14
|
+
expect(INJECTED_SOURCE_KINDS.has('skill-catalog')).toBe(true)
|
|
15
|
+
expect(INJECTED_SOURCE_KINDS.has('dsh-context-compression')).toBe(true)
|
|
16
|
+
expect(INJECTED_SOURCE_KINDS.has('dsh-ops-ptc')).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
describe('F13: isInjectedMessage', () => {
|
|
21
|
+
it('returns true for injected user/message events', () => {
|
|
22
|
+
expect(isInjectedMessage({ type: 'user/message', data: { source: { kind: 'time-context' } } })).toBe(true)
|
|
23
|
+
expect(isInjectedMessage({ type: 'user/message', data: { source: { kind: 'dsh-ops-ptc' } } })).toBe(true)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('returns false for real user messages (no source)', () => {
|
|
27
|
+
expect(isInjectedMessage({ type: 'user/message', data: {} })).toBe(false)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('returns false for non-user events', () => {
|
|
31
|
+
expect(isInjectedMessage({ type: 'tool/result', data: { source: { kind: 'time-context' } } })).toBe(false)
|
|
32
|
+
expect(isInjectedMessage({ type: 'agent/step' })).toBe(false)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('returns false for user/message with unknown source kind', () => {
|
|
36
|
+
expect(isInjectedMessage({ type: 'user/message', data: { source: { kind: 'something-new' } } })).toBe(false)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('F13: isRealUserBoundary', () => {
|
|
41
|
+
it('returns true for user/message without source (old host compat)', () => {
|
|
42
|
+
expect(isRealUserBoundary({ type: 'user/message', data: {} })).toBe(true)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('returns true for user/message with non-injected source', () => {
|
|
46
|
+
expect(isRealUserBoundary({ type: 'user/message', data: { source: { kind: 'user-typed' } } })).toBe(true)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('returns false for injected user/message', () => {
|
|
50
|
+
expect(isRealUserBoundary({ type: 'user/message', data: { source: { kind: 'skill-catalog' } } })).toBe(false)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('returns false for non-user events', () => {
|
|
54
|
+
expect(isRealUserBoundary({ type: 'tool/result' })).toBe(false)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('F13: mixed event stream simulation', () => {
|
|
59
|
+
const events = [
|
|
60
|
+
{ type: 'user/message', data: { source: { kind: 'time-context' } } },
|
|
61
|
+
{ type: 'user/message', data: {} }, // real user
|
|
62
|
+
{ type: 'tool/result', data: {} },
|
|
63
|
+
{ type: 'user/message', data: { source: { kind: 'dsh-context-compression' } } },
|
|
64
|
+
{ type: 'tool/result', data: {} },
|
|
65
|
+
{ type: 'user/message', data: { source: { kind: 'tool-jobs' } } },
|
|
66
|
+
{ type: 'user/message', data: {} }, // real user
|
|
67
|
+
{ type: 'tool/result', data: {} },
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
it('correctly identifies 2 real user boundaries out of 8 events', () => {
|
|
71
|
+
const real = events.filter(isRealUserBoundary)
|
|
72
|
+
expect(real).toHaveLength(2)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('correctly identifies 3 injected messages out of 8 events', () => {
|
|
76
|
+
const injected = events.filter(isInjectedMessage)
|
|
77
|
+
expect(injected).toHaveLength(3)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('injected + real = total user/message count (no double-counting)', () => {
|
|
81
|
+
const allUser = events.filter(e => e.type === 'user/message')
|
|
82
|
+
const injected = events.filter(isInjectedMessage)
|
|
83
|
+
const real = events.filter(isRealUserBoundary)
|
|
84
|
+
expect(injected.length + real.length).toBe(allUser.length)
|
|
85
|
+
})
|
|
86
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* F1: Net-gain threshold tests.
|
|
3
|
+
*
|
|
4
|
+
* @module dsh-context-compression-improved/tests/runtime/netgain
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it } from 'vitest'
|
|
7
|
+
import { checkNetGain, computeNetGain, CARD_OVERHEAD_TOKENS } from '../../src/runtime/netgain.ts'
|
|
8
|
+
|
|
9
|
+
describe('F1: checkNetGain boundary cases', () => {
|
|
10
|
+
it('disabled when minNetGain = 0 (always allows)', () => {
|
|
11
|
+
expect(checkNetGain(100, 90, 0)).toBe(true)
|
|
12
|
+
expect(checkNetGain(200, 190, 0)).toBe(true)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('disabled when minNetGain negative (always allows)', () => {
|
|
16
|
+
expect(checkNetGain(100, 90, -1)).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('skips tiny gain: 500→400 tokens with overhead 80 = gain 20 < 1000', () => {
|
|
20
|
+
expect(checkNetGain(500, 400, 1000)).toBe(false)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('allows good gain: 5000→1000 tokens with overhead 80 = gain 3920 > 1000', () => {
|
|
24
|
+
expect(checkNetGain(5000, 1000, 1000)).toBe(true)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('boundary exact: gain = minNetGain → allows (>=)', () => {
|
|
28
|
+
// 1000 - 0 - 80 = 920 → need exactly 920 threshold
|
|
29
|
+
expect(checkNetGain(1000, 0, 920)).toBe(true)
|
|
30
|
+
expect(checkNetGain(1000, 0, 921)).toBe(false)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('negative gain (card bigger than original) → always skip', () => {
|
|
34
|
+
expect(checkNetGain(100, 200, 1000)).toBe(false)
|
|
35
|
+
expect(checkNetGain(100, 200, 0)).toBe(true) // disabled = allows even bad cuts
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('zero tokens before → skip', () => {
|
|
39
|
+
expect(checkNetGain(0, 0, 1000)).toBe(false)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('huge output: 50000→5000, gain = 44920 >> 1000', () => {
|
|
43
|
+
expect(checkNetGain(50000, 5000, 1000)).toBe(true)
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
describe('F1: computeNetGain', () => {
|
|
48
|
+
it('returns tokensBefore - tokensAfter - overhead', () => {
|
|
49
|
+
expect(computeNetGain(5000, 1000)).toBe(5000 - 1000 - CARD_OVERHEAD_TOKENS)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('can be negative when card > original', () => {
|
|
53
|
+
expect(computeNetGain(100, 200)).toBeLessThan(0)
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
describe('F1: CARD_OVERHEAD_TOKENS is reasonable', () => {
|
|
58
|
+
it('overhead is 80 tokens (sourceRef + header + hint)', () => {
|
|
59
|
+
expect(CARD_OVERHEAD_TOKENS).toBe(80)
|
|
60
|
+
})
|
|
61
|
+
})
|