dsh-context-compression-improved 0.2.2 → 0.2.4
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/.github/workflows/ci.yml +39 -39
- package/CHANGELOG.md +137 -135
- package/README.md +112 -103
- package/package.json +142 -138
- package/packages/selector/README.md +31 -26
- package/packages/selector/README.zh.md +31 -26
- package/packages/selector/dsh.plugin.json +16 -16
- package/packages/selector/lib/client.d.ts +228 -228
- package/packages/selector/lib/client.js +1462 -1462
- package/packages/selector/lib/config.js +856 -842
- package/packages/selector/lib/index.d.ts +37 -37
- package/packages/selector/lib/index.js +693 -645
- package/packages/selector/lib/invariant.js +70 -70
- package/packages/selector/lib/pruner.d.ts +728 -683
- package/packages/selector/lib/pruner.js +4765 -3807
- package/packages/selector/lib/style.css +46 -46
- package/packages/selector/lib/tail-trim.js +136 -136
- package/packages/selector/package.json +15 -7
- package/packages/selector/src/client/index.ts +111 -111
- package/packages/selector/src/preset-overlay.ts +60 -1
- package/packages/selector/src/pruner/content.ts +18 -5
- package/packages/selector/src/pruner/types.ts +23 -5
- package/packages/selector/src/pruner.ts +164 -154
- package/packages/selector/src/runtime/adaptive-cost.ts +23 -12
- package/packages/selector/src/runtime/audit.ts +18 -2
- package/packages/selector/src/runtime/config.ts +30 -1
- package/packages/selector/src/runtime/measurement.ts +31 -2
- package/packages/selector/src/runtime/reducers.ts +1115 -97
- package/packages/selector/src/runtime/tokenpilot/dedup.ts +1 -1
- package/packages/selector/src/runtime/tokenpilot/locator.ts +1 -1
- package/packages/selector/src/runtime/tokenpilot/read-state.ts +23 -2
- package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +275 -0
- package/packages/selector/src/runtime/toolclass.ts +103 -0
- package/packages/selector/src/runtime/types.ts +16 -0
- package/packages/selector/tests/preset-overlay.host.spec.ts +256 -243
- package/packages/selector/tests/runtime/adaptive-cost.spec.ts +7 -7
- package/packages/selector/tests/runtime/audit.spec.ts +44 -1
- package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
- package/packages/selector/tests/runtime/code-skeleton.spec.ts +14 -3
- package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
- package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
- package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
- package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +198 -27
- package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
- package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
- package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
- package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
- package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +24 -0
- package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
- package/packages/selector/tests/standing-generation.host.spec.ts +642 -631
- package/scripts/toolclass-corpus-replay.mjs +273 -0
|
@@ -1,243 +1,256 @@
|
|
|
1
|
-
import { access, mkdir, mkdtemp, readFile, readdir, rm, stat, utimes, writeFile } from 'node:fs/promises'
|
|
2
|
-
import { tmpdir } from 'node:os'
|
|
3
|
-
import { dirname, join } from 'node:path'
|
|
4
|
-
import { afterEach, describe, expect, it } from 'vitest'
|
|
5
|
-
import { load } from 'js-yaml'
|
|
6
|
-
import type { AgentPreset } from '@deepseek-ai/dsh-agent-presets'
|
|
7
|
-
import {
|
|
8
|
-
decorateAgentPresets,
|
|
9
|
-
type CompressionModulePaths,
|
|
10
|
-
type OverlayableAgentPresets,
|
|
11
|
-
type PresetOverlayMetadataIo,
|
|
12
|
-
} from '../src/preset-overlay.ts'
|
|
13
|
-
|
|
14
|
-
const MODULES: CompressionModulePaths = {
|
|
15
|
-
compactionBasic: '/opt/context-selector/compaction-basic.js',
|
|
16
|
-
commandCompact: '/opt/context-selector/command-compact.js',
|
|
17
|
-
toolResultPruner: '/opt/context-selector/tool-result-pruner.js',
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const roots: string[] = []
|
|
21
|
-
|
|
22
|
-
afterEach(async () => {
|
|
23
|
-
await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true })))
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
async function fixture(): Promise<{
|
|
27
|
-
root: string
|
|
28
|
-
standard: AgentPreset
|
|
29
|
-
minimal: AgentPreset
|
|
30
|
-
}> {
|
|
31
|
-
const root = await mkdtemp(join(tmpdir(), 'dsh-selector-overlay-test-'))
|
|
32
|
-
roots.push(root)
|
|
33
|
-
const standardPath = join(root, 'standard', 'agent.cordis.yml')
|
|
34
|
-
const minimalPath = join(root, 'minimal', 'agent.cordis.yml')
|
|
35
|
-
await mkdir(dirname(standardPath), { recursive: true })
|
|
36
|
-
await mkdir(dirname(minimalPath), { recursive: true })
|
|
37
|
-
await writeFile(standardPath, [
|
|
38
|
-
'- id: persona',
|
|
39
|
-
" name: '/opt/preset/persona.js'",
|
|
40
|
-
'- id: tool-context-retrieve',
|
|
41
|
-
" name: '@deepseek-ai/dsh-tool-context-retrieve'",
|
|
42
|
-
'- id: compaction',
|
|
43
|
-
' name: cordis:group',
|
|
44
|
-
' group: true',
|
|
45
|
-
' isolate:',
|
|
46
|
-
' compaction: true',
|
|
47
|
-
' toolResultPruner: true',
|
|
48
|
-
' config:',
|
|
49
|
-
' - id: compaction-basic',
|
|
50
|
-
" name: '@deepseek-ai/dsh-compaction-basic'",
|
|
51
|
-
' - id: command-compact',
|
|
52
|
-
" name: '@deepseek-ai/dsh-command-compact'",
|
|
53
|
-
' - id: tool-result-pruner',
|
|
54
|
-
" name: '@deepseek-ai/dsh-compaction-tool-result-pruner'",
|
|
55
|
-
'',
|
|
56
|
-
].join('\n'))
|
|
57
|
-
await writeFile(minimalPath, "- id: persona\n name: '/opt/preset/minimal.js'\n")
|
|
58
|
-
return {
|
|
59
|
-
root,
|
|
60
|
-
standard: { id: 'standard', trust: 'system', path: standardPath },
|
|
61
|
-
minimal: { id: 'minimal', trust: 'system', path: minimalPath },
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
class FakeAgentPresets implements OverlayableAgentPresets {
|
|
66
|
-
constructor(private readonly presets: ReadonlyMap<string, AgentPreset>) {}
|
|
67
|
-
|
|
68
|
-
async resolve(id = 'standard'): Promise<AgentPreset> {
|
|
69
|
-
const preset = this.presets.get(id)
|
|
70
|
-
if (preset === undefined) throw new Error(`unknown preset: ${id}`)
|
|
71
|
-
return preset
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async mount(_agentCtx: unknown, id?: string): Promise<AgentPreset> {
|
|
75
|
-
return await this.resolve(id)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async recompose(_agentCtx: unknown, id: string): Promise<AgentPreset> {
|
|
79
|
-
return await this.resolve(id)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async standingKeyFor(id?: string): Promise<string> {
|
|
83
|
-
return (await this.resolve(id)).path
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async read(id: string): Promise<string> {
|
|
87
|
-
return await readFile((await this.resolve(id)).path, 'utf8')
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function rowsAt(text: string): Array<Record<string, unknown>> {
|
|
92
|
-
const rows = load(text)
|
|
93
|
-
if (!Array.isArray(rows)) throw new TypeError('expected entry list')
|
|
94
|
-
return rows as Array<Record<string, unknown>>
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function flatten(rows: Array<Record<string, unknown>>): Array<Record<string, unknown>> {
|
|
98
|
-
return rows.flatMap((row) => {
|
|
99
|
-
const nested = row.group === true && Array.isArray(row.config)
|
|
100
|
-
? flatten(row.config as Array<Record<string, unknown>>)
|
|
101
|
-
: []
|
|
102
|
-
return [row, ...nested]
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
describe('plugin-owned preset overlay decorator', () => {
|
|
107
|
-
it('enhances only composition operations while authoring sees the source preset', async () => {
|
|
108
|
-
const { root, standard, minimal } = await fixture()
|
|
109
|
-
const presets = new FakeAgentPresets(new Map([
|
|
110
|
-
['standard', standard],
|
|
111
|
-
['minimal', minimal],
|
|
112
|
-
]))
|
|
113
|
-
const installation = decorateAgentPresets(presets, {
|
|
114
|
-
modules: MODULES,
|
|
115
|
-
excludedPresetIds: ['minimal'],
|
|
116
|
-
tempParent: root,
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
expect((await presets.resolve('standard')).path).toBe(standard.path)
|
|
120
|
-
expect(await presets.read('standard')).toBe(await readFile(standard.path, 'utf8'))
|
|
121
|
-
|
|
122
|
-
const mounted = await presets.mount({}, 'standard')
|
|
123
|
-
expect(mounted.path).not.toBe(standard.path)
|
|
124
|
-
expect((await presets.recompose({}, 'standard')).path).toBe(mounted.path)
|
|
125
|
-
expect(await presets.standingKeyFor('standard')).toBe(mounted.path)
|
|
126
|
-
expect((await presets.mount({}, 'minimal')).path).toBe(minimal.path)
|
|
127
|
-
expect((await presets.resolve('standard')).path).toBe(standard.path)
|
|
128
|
-
|
|
129
|
-
const all = flatten(rowsAt(await readFile(mounted.path, 'utf8')))
|
|
130
|
-
expect(all.filter(row => row.id === 'tool-context-retrieve')).toHaveLength(1)
|
|
131
|
-
expect(all.filter(row => row.id === 'compaction')).toHaveLength(1)
|
|
132
|
-
expect(all.filter(row => row.id === 'compaction-basic')).toHaveLength(1)
|
|
133
|
-
expect(all.filter(row => row.id === 'command-compact')).toHaveLength(1)
|
|
134
|
-
expect(all.filter(row => row.id === 'tool-result-pruner')).toHaveLength(1)
|
|
135
|
-
expect(all.find(row => row.id === 'tool-context-retrieve')?.name)
|
|
136
|
-
.toBe('@deepseek-ai/dsh-tool-context-retrieve')
|
|
137
|
-
expect(all.find(row => row.id === 'tool-result-pruner')?.name).toBe(MODULES.toolResultPruner)
|
|
138
|
-
|
|
139
|
-
await installation.dispose()
|
|
140
|
-
expect((await presets.mount({}, 'standard')).path).toBe(standard.path)
|
|
141
|
-
await expect(access(dirname(mounted.path))).rejects.toThrow()
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
it('keeps direct concurrent resolve outside the operation-local overlay', async () => {
|
|
145
|
-
const { root, standard } = await fixture()
|
|
146
|
-
const presets = new FakeAgentPresets(new Map([['standard', standard]]))
|
|
147
|
-
const installation = decorateAgentPresets(presets, { modules: MODULES, tempParent: root })
|
|
148
|
-
|
|
149
|
-
const [mounted, resolved] = await Promise.all([
|
|
150
|
-
presets.mount({}, 'standard'),
|
|
151
|
-
presets.resolve('standard'),
|
|
152
|
-
])
|
|
153
|
-
|
|
154
|
-
expect(mounted.path).not.toBe(standard.path)
|
|
155
|
-
expect(resolved.path).toBe(standard.path)
|
|
156
|
-
await installation.dispose()
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
expect((await
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
expect(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
await
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
1
|
+
import { access, mkdir, mkdtemp, readFile, readdir, rm, stat, utimes, writeFile } from 'node:fs/promises'
|
|
2
|
+
import { tmpdir } from 'node:os'
|
|
3
|
+
import { dirname, join } from 'node:path'
|
|
4
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
5
|
+
import { load } from 'js-yaml'
|
|
6
|
+
import type { AgentPreset } from '@deepseek-ai/dsh-agent-presets'
|
|
7
|
+
import {
|
|
8
|
+
decorateAgentPresets,
|
|
9
|
+
type CompressionModulePaths,
|
|
10
|
+
type OverlayableAgentPresets,
|
|
11
|
+
type PresetOverlayMetadataIo,
|
|
12
|
+
} from '../src/preset-overlay.ts'
|
|
13
|
+
|
|
14
|
+
const MODULES: CompressionModulePaths = {
|
|
15
|
+
compactionBasic: '/opt/context-selector/compaction-basic.js',
|
|
16
|
+
commandCompact: '/opt/context-selector/command-compact.js',
|
|
17
|
+
toolResultPruner: '/opt/context-selector/tool-result-pruner.js',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const roots: string[] = []
|
|
21
|
+
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true })))
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
async function fixture(): Promise<{
|
|
27
|
+
root: string
|
|
28
|
+
standard: AgentPreset
|
|
29
|
+
minimal: AgentPreset
|
|
30
|
+
}> {
|
|
31
|
+
const root = await mkdtemp(join(tmpdir(), 'dsh-selector-overlay-test-'))
|
|
32
|
+
roots.push(root)
|
|
33
|
+
const standardPath = join(root, 'standard', 'agent.cordis.yml')
|
|
34
|
+
const minimalPath = join(root, 'minimal', 'agent.cordis.yml')
|
|
35
|
+
await mkdir(dirname(standardPath), { recursive: true })
|
|
36
|
+
await mkdir(dirname(minimalPath), { recursive: true })
|
|
37
|
+
await writeFile(standardPath, [
|
|
38
|
+
'- id: persona',
|
|
39
|
+
" name: '/opt/preset/persona.js'",
|
|
40
|
+
'- id: tool-context-retrieve',
|
|
41
|
+
" name: '@deepseek-ai/dsh-tool-context-retrieve'",
|
|
42
|
+
'- id: compaction',
|
|
43
|
+
' name: cordis:group',
|
|
44
|
+
' group: true',
|
|
45
|
+
' isolate:',
|
|
46
|
+
' compaction: true',
|
|
47
|
+
' toolResultPruner: true',
|
|
48
|
+
' config:',
|
|
49
|
+
' - id: compaction-basic',
|
|
50
|
+
" name: '@deepseek-ai/dsh-compaction-basic'",
|
|
51
|
+
' - id: command-compact',
|
|
52
|
+
" name: '@deepseek-ai/dsh-command-compact'",
|
|
53
|
+
' - id: tool-result-pruner',
|
|
54
|
+
" name: '@deepseek-ai/dsh-compaction-tool-result-pruner'",
|
|
55
|
+
'',
|
|
56
|
+
].join('\n'))
|
|
57
|
+
await writeFile(minimalPath, "- id: persona\n name: '/opt/preset/minimal.js'\n")
|
|
58
|
+
return {
|
|
59
|
+
root,
|
|
60
|
+
standard: { id: 'standard', trust: 'system', path: standardPath },
|
|
61
|
+
minimal: { id: 'minimal', trust: 'system', path: minimalPath },
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
class FakeAgentPresets implements OverlayableAgentPresets {
|
|
66
|
+
constructor(private readonly presets: ReadonlyMap<string, AgentPreset>) {}
|
|
67
|
+
|
|
68
|
+
async resolve(id = 'standard'): Promise<AgentPreset> {
|
|
69
|
+
const preset = this.presets.get(id)
|
|
70
|
+
if (preset === undefined) throw new Error(`unknown preset: ${id}`)
|
|
71
|
+
return preset
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async mount(_agentCtx: unknown, id?: string): Promise<AgentPreset> {
|
|
75
|
+
return await this.resolve(id)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async recompose(_agentCtx: unknown, id: string): Promise<AgentPreset> {
|
|
79
|
+
return await this.resolve(id)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async standingKeyFor(id?: string): Promise<string> {
|
|
83
|
+
return (await this.resolve(id)).path
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async read(id: string): Promise<string> {
|
|
87
|
+
return await readFile((await this.resolve(id)).path, 'utf8')
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function rowsAt(text: string): Array<Record<string, unknown>> {
|
|
92
|
+
const rows = load(text)
|
|
93
|
+
if (!Array.isArray(rows)) throw new TypeError('expected entry list')
|
|
94
|
+
return rows as Array<Record<string, unknown>>
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function flatten(rows: Array<Record<string, unknown>>): Array<Record<string, unknown>> {
|
|
98
|
+
return rows.flatMap((row) => {
|
|
99
|
+
const nested = row.group === true && Array.isArray(row.config)
|
|
100
|
+
? flatten(row.config as Array<Record<string, unknown>>)
|
|
101
|
+
: []
|
|
102
|
+
return [row, ...nested]
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
describe('plugin-owned preset overlay decorator', () => {
|
|
107
|
+
it('enhances only composition operations while authoring sees the source preset', async () => {
|
|
108
|
+
const { root, standard, minimal } = await fixture()
|
|
109
|
+
const presets = new FakeAgentPresets(new Map([
|
|
110
|
+
['standard', standard],
|
|
111
|
+
['minimal', minimal],
|
|
112
|
+
]))
|
|
113
|
+
const installation = decorateAgentPresets(presets, {
|
|
114
|
+
modules: MODULES,
|
|
115
|
+
excludedPresetIds: ['minimal'],
|
|
116
|
+
tempParent: root,
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
expect((await presets.resolve('standard')).path).toBe(standard.path)
|
|
120
|
+
expect(await presets.read('standard')).toBe(await readFile(standard.path, 'utf8'))
|
|
121
|
+
|
|
122
|
+
const mounted = await presets.mount({}, 'standard')
|
|
123
|
+
expect(mounted.path).not.toBe(standard.path)
|
|
124
|
+
expect((await presets.recompose({}, 'standard')).path).toBe(mounted.path)
|
|
125
|
+
expect(await presets.standingKeyFor('standard')).toBe(mounted.path)
|
|
126
|
+
expect((await presets.mount({}, 'minimal')).path).toBe(minimal.path)
|
|
127
|
+
expect((await presets.resolve('standard')).path).toBe(standard.path)
|
|
128
|
+
|
|
129
|
+
const all = flatten(rowsAt(await readFile(mounted.path, 'utf8')))
|
|
130
|
+
expect(all.filter(row => row.id === 'tool-context-retrieve')).toHaveLength(1)
|
|
131
|
+
expect(all.filter(row => row.id === 'compaction')).toHaveLength(1)
|
|
132
|
+
expect(all.filter(row => row.id === 'compaction-basic')).toHaveLength(1)
|
|
133
|
+
expect(all.filter(row => row.id === 'command-compact')).toHaveLength(1)
|
|
134
|
+
expect(all.filter(row => row.id === 'tool-result-pruner')).toHaveLength(1)
|
|
135
|
+
expect(all.find(row => row.id === 'tool-context-retrieve')?.name)
|
|
136
|
+
.toBe('@deepseek-ai/dsh-tool-context-retrieve')
|
|
137
|
+
expect(all.find(row => row.id === 'tool-result-pruner')?.name).toBe(MODULES.toolResultPruner)
|
|
138
|
+
|
|
139
|
+
await installation.dispose()
|
|
140
|
+
expect((await presets.mount({}, 'standard')).path).toBe(standard.path)
|
|
141
|
+
await expect(access(dirname(mounted.path))).rejects.toThrow()
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('keeps direct concurrent resolve outside the operation-local overlay', async () => {
|
|
145
|
+
const { root, standard } = await fixture()
|
|
146
|
+
const presets = new FakeAgentPresets(new Map([['standard', standard]]))
|
|
147
|
+
const installation = decorateAgentPresets(presets, { modules: MODULES, tempParent: root })
|
|
148
|
+
|
|
149
|
+
const [mounted, resolved] = await Promise.all([
|
|
150
|
+
presets.mount({}, 'standard'),
|
|
151
|
+
presets.resolve('standard'),
|
|
152
|
+
])
|
|
153
|
+
|
|
154
|
+
expect(mounted.path).not.toBe(standard.path)
|
|
155
|
+
expect(resolved.path).toBe(standard.path)
|
|
156
|
+
await installation.dispose()
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
// chmod(2) is only observable on POSIX: libuv on Windows synthesizes stat
|
|
160
|
+
// mode bits (0o666) that never reflect the requested 0o700/0o600, so the
|
|
161
|
+
// strong permission assertions run there only.
|
|
162
|
+
it.skipIf(process.platform === 'win32')('uses owner-only files for generated compositions', async () => {
|
|
163
|
+
const { root, standard } = await fixture()
|
|
164
|
+
const presets = new FakeAgentPresets(new Map([['standard', standard]]))
|
|
165
|
+
const installation = decorateAgentPresets(presets, { modules: MODULES, tempParent: root })
|
|
166
|
+
|
|
167
|
+
const first = await presets.mount({}, 'standard')
|
|
168
|
+
expect((await presets.mount({}, 'standard')).path).toBe(first.path)
|
|
169
|
+
expect((await stat(dirname(first.path))).mode & 0o777).toBe(0o700)
|
|
170
|
+
expect((await stat(first.path)).mode & 0o777).toBe(0o600)
|
|
171
|
+
|
|
172
|
+
await installation.dispose()
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('starts a new generation after source content changes', async () => {
|
|
176
|
+
const { root, standard } = await fixture()
|
|
177
|
+
const presets = new FakeAgentPresets(new Map([['standard', standard]]))
|
|
178
|
+
const installation = decorateAgentPresets(presets, { modules: MODULES, tempParent: root })
|
|
179
|
+
|
|
180
|
+
const first = await presets.mount({}, 'standard')
|
|
181
|
+
expect((await presets.mount({}, 'standard')).path).toBe(first.path)
|
|
182
|
+
|
|
183
|
+
await writeFile(standard.path, `${await readFile(standard.path, 'utf8')}\n- id: later\n name: '/opt/preset/later.js'\n`)
|
|
184
|
+
const changed = await presets.mount({}, 'standard')
|
|
185
|
+
expect(changed.path).not.toBe(first.path)
|
|
186
|
+
expect(await readFile(changed.path, 'utf8')).toContain('id: later')
|
|
187
|
+
|
|
188
|
+
await installation.dispose()
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('shares one decoration across duplicate Bundle and built-in loads', async () => {
|
|
192
|
+
const { root, standard } = await fixture()
|
|
193
|
+
const presets = new FakeAgentPresets(new Map([['standard', standard]]))
|
|
194
|
+
const first = decorateAgentPresets(presets, { modules: MODULES, tempParent: root })
|
|
195
|
+
const second = decorateAgentPresets(presets, { modules: MODULES, tempParent: root })
|
|
196
|
+
|
|
197
|
+
const mounted = await presets.mount({}, 'standard')
|
|
198
|
+
const all = flatten(rowsAt(await readFile(mounted.path, 'utf8')))
|
|
199
|
+
expect(all.filter(row => row.id === 'compaction')).toHaveLength(1)
|
|
200
|
+
expect(all.filter(row => row.id === 'tool-result-pruner')).toHaveLength(1)
|
|
201
|
+
|
|
202
|
+
await first.dispose()
|
|
203
|
+
expect((await presets.mount({}, 'standard')).path).toBe(mounted.path)
|
|
204
|
+
|
|
205
|
+
await second.dispose()
|
|
206
|
+
expect((await presets.mount({}, 'standard')).path).toBe(standard.path)
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it('does not share one decoration across different metadata policies', async () => {
|
|
210
|
+
const { root, standard } = await fixture()
|
|
211
|
+
const presets = new FakeAgentPresets(new Map([['standard', standard]]))
|
|
212
|
+
const metadataIo = (): PresetOverlayMetadataIo => ({
|
|
213
|
+
async setTimes(path, stamp) {
|
|
214
|
+
await utimes(path, stamp, stamp)
|
|
215
|
+
},
|
|
216
|
+
async read(path) {
|
|
217
|
+
const observed = await stat(path)
|
|
218
|
+
return { mtimeMs: observed.mtimeMs, size: observed.size }
|
|
219
|
+
},
|
|
220
|
+
})
|
|
221
|
+
const first = decorateAgentPresets(presets, {
|
|
222
|
+
modules: MODULES,
|
|
223
|
+
tempParent: root,
|
|
224
|
+
metadataIo: metadataIo(),
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
expect(() => decorateAgentPresets(presets, {
|
|
228
|
+
modules: MODULES,
|
|
229
|
+
tempParent: root,
|
|
230
|
+
metadataIo: metadataIo(),
|
|
231
|
+
})).toThrow(/different compression overlay/u)
|
|
232
|
+
|
|
233
|
+
await first.dispose()
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('removes staging files when metadata preparation fails before publish', async () => {
|
|
237
|
+
const { root, standard } = await fixture()
|
|
238
|
+
const presets = new FakeAgentPresets(new Map([['standard', standard]]))
|
|
239
|
+
const installation = decorateAgentPresets(presets, {
|
|
240
|
+
modules: MODULES,
|
|
241
|
+
tempParent: root,
|
|
242
|
+
metadataIo: {
|
|
243
|
+
setTimes: () => Promise.reject(new Error('synthetic metadata failure')),
|
|
244
|
+
read: () => Promise.reject(new Error('metadata read must not run')),
|
|
245
|
+
},
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
await expect(presets.mount({}, 'standard')).rejects.toThrow(/synthetic metadata failure/u)
|
|
249
|
+
const generatedDirectories = (await readdir(root, { withFileTypes: true }))
|
|
250
|
+
.filter(entry => entry.isDirectory() && entry.name.startsWith('dsh-context-compression-presets-'))
|
|
251
|
+
expect(generatedDirectories).toHaveLength(1)
|
|
252
|
+
expect(await readdir(join(root, generatedDirectories[0]!.name))).toEqual([])
|
|
253
|
+
|
|
254
|
+
await installation.dispose()
|
|
255
|
+
})
|
|
256
|
+
})
|
|
@@ -31,9 +31,9 @@ describe('Conservative Adaptive interval', () => {
|
|
|
31
31
|
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
32
32
|
previousRequestMeasurement: exact(30_500),
|
|
33
33
|
measuredNodes: [
|
|
34
|
-
{ seq: 0, count: exact(10_000) },
|
|
35
|
-
{ seq: 2, count: exact(20_000) },
|
|
36
|
-
{ seq: 3, count: exact(500) },
|
|
34
|
+
{ seq: 0, count: exact(10_000), characterPressure: 40_000 },
|
|
35
|
+
{ seq: 2, count: exact(20_000), characterPressure: 80_000 },
|
|
36
|
+
{ seq: 3, count: exact(500), characterPressure: 2_000 },
|
|
37
37
|
],
|
|
38
38
|
})).toEqual({
|
|
39
39
|
kind: 'available',
|
|
@@ -57,7 +57,7 @@ describe('Conservative Adaptive interval', () => {
|
|
|
57
57
|
estimatorRevision: 'r1',
|
|
58
58
|
calibration: { sampleCount: 1, conservativeMarginTokens: 200 },
|
|
59
59
|
}),
|
|
60
|
-
measuredNodes: [{ seq: 0, count: exact(10_000) }],
|
|
60
|
+
measuredNodes: [{ seq: 0, count: exact(10_000), characterPressure: 40_000 }],
|
|
61
61
|
})).toMatchObject({
|
|
62
62
|
kind: 'available',
|
|
63
63
|
measurementKind: 'tokenizer-estimate',
|
|
@@ -79,7 +79,7 @@ describe('Conservative Adaptive interval', () => {
|
|
|
79
79
|
previousPromptTokens: 200,
|
|
80
80
|
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
81
81
|
previousRequestMeasurement: measurement,
|
|
82
|
-
measuredNodes: [{ seq: 0, count: exact(50) }],
|
|
82
|
+
measuredNodes: [{ seq: 0, count: exact(50), characterPressure: 200 }],
|
|
83
83
|
})).toEqual({ kind: 'unknown', reason })
|
|
84
84
|
})
|
|
85
85
|
|
|
@@ -90,7 +90,7 @@ describe('Conservative Adaptive interval', () => {
|
|
|
90
90
|
previousPromptTokens: 200,
|
|
91
91
|
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
92
92
|
previousRequestMeasurement: exact(200, 'DeepSeek-V4-Flash-0731'),
|
|
93
|
-
measuredNodes: [{ seq: 0, count: exact(50) }],
|
|
93
|
+
measuredNodes: [{ seq: 0, count: exact(50), characterPressure: 200 }],
|
|
94
94
|
})).toEqual({ kind: 'unknown', reason: 'request-tokenizer-revision-mismatch' })
|
|
95
95
|
})
|
|
96
96
|
|
|
@@ -101,7 +101,7 @@ describe('Conservative Adaptive interval', () => {
|
|
|
101
101
|
previousPromptTokens: 200,
|
|
102
102
|
expectedTokenizerRevision: 'DeepSeek-V4-Pro-0813',
|
|
103
103
|
previousRequestMeasurement: exact(200),
|
|
104
|
-
measuredNodes: [{ seq: 0, count: exact(50, 'DeepSeek-V4-Flash-0731') }],
|
|
104
|
+
measuredNodes: [{ seq: 0, count: exact(50, 'DeepSeek-V4-Flash-0731'), characterPressure: 200 }],
|
|
105
105
|
})).toEqual({ kind: 'unknown', reason: 'exact-prefix-tokenizer-revision-mismatch' })
|
|
106
106
|
})
|
|
107
107
|
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
emitCompressionAudit,
|
|
5
5
|
formatCompressionAudit,
|
|
6
6
|
} from '../../src/runtime/audit.ts'
|
|
7
|
-
import type { CompressionAuditRecord } from '../../src/runtime/audit.ts'
|
|
7
|
+
import type { CompressionAuditRecord, CompressionRewriteAuditRecord } from '../../src/runtime/audit.ts'
|
|
8
8
|
|
|
9
9
|
const custom = {
|
|
10
10
|
version: 3 as const,
|
|
@@ -71,6 +71,7 @@ describe('context-compression audit records', () => {
|
|
|
71
71
|
tokensRemoved: 600_000,
|
|
72
72
|
tokenizerId: 'mock-tokenizer',
|
|
73
73
|
tokenizerRevision: 'r1',
|
|
74
|
+
measurementBasis: 'exact-tokenizer',
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
const parsed = JSON.parse(
|
|
@@ -82,6 +83,48 @@ describe('context-compression audit records', () => {
|
|
|
82
83
|
.toBe(record.tokensBefore - record.tokensAfter)
|
|
83
84
|
})
|
|
84
85
|
|
|
86
|
+
// task_4c/G7: the elided-line count rides the rewrite audit record (optional,
|
|
87
|
+
// JSON round-trip) so the compress→retrieve M/N ratio is computable from
|
|
88
|
+
// session logs alone; records without reducer telemetry omit the field.
|
|
89
|
+
it('round-trips elidedLines on rewrite records and omits it when absent', () => {
|
|
90
|
+
const withTelemetry: CompressionAuditRecord = {
|
|
91
|
+
schemaVersion: 1,
|
|
92
|
+
kind: 'rewrite',
|
|
93
|
+
sessionId: 'telemetry-session',
|
|
94
|
+
profile: 'balanced',
|
|
95
|
+
component: 'fresh',
|
|
96
|
+
stage: 'fresh',
|
|
97
|
+
reducer: 'hypa-code-skeleton',
|
|
98
|
+
manifestEventType: 'compaction/prune',
|
|
99
|
+
manifestSeq: 7,
|
|
100
|
+
replacementSeq: 8,
|
|
101
|
+
sourceSeqs: [6],
|
|
102
|
+
tokensBefore: 20_000,
|
|
103
|
+
tokensAfter: 3_000,
|
|
104
|
+
tokensRemoved: 17_000,
|
|
105
|
+
tokenizerId: 'mock-tokenizer',
|
|
106
|
+
tokenizerRevision: 'r1',
|
|
107
|
+
measurementBasis: 'exact-tokenizer',
|
|
108
|
+
elidedLines: 412,
|
|
109
|
+
}
|
|
110
|
+
const parsed = JSON.parse(
|
|
111
|
+
formatCompressionAudit(withTelemetry).slice(COMPRESSION_AUDIT_PREFIX.length),
|
|
112
|
+
) as CompressionRewriteAuditRecord
|
|
113
|
+
expect(parsed.elidedLines).toBe(412)
|
|
114
|
+
|
|
115
|
+
const { elidedLines: _omitted, ...placeholderRecord } = withTelemetry
|
|
116
|
+
void _omitted
|
|
117
|
+
const withoutTelemetry: CompressionAuditRecord = {
|
|
118
|
+
...placeholderRecord,
|
|
119
|
+
reducer: 'error-evidence-placeholder',
|
|
120
|
+
}
|
|
121
|
+
expect('elidedLines' in withoutTelemetry).toBe(false)
|
|
122
|
+
const parsedWithout = JSON.parse(
|
|
123
|
+
formatCompressionAudit(withoutTelemetry).slice(COMPRESSION_AUDIT_PREFIX.length),
|
|
124
|
+
) as CompressionRewriteAuditRecord
|
|
125
|
+
expect(parsedWithout.elidedLines).toBeUndefined()
|
|
126
|
+
})
|
|
127
|
+
|
|
85
128
|
it('publishes exactly one single-line info message', () => {
|
|
86
129
|
const info = vi.fn()
|
|
87
130
|
const record: CompressionAuditRecord = {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { CHARS_PER_TOKEN, charsForTokens, charsToTokens } from '../../src/runtime/config.ts'
|
|
4
|
+
|
|
5
|
+
describe('character basis conversion', () => {
|
|
6
|
+
it('derives zero tokens for non-positive or non-finite character counts', () => {
|
|
7
|
+
expect(charsToTokens(0)).toBe(0)
|
|
8
|
+
expect(charsToTokens(-1)).toBe(0)
|
|
9
|
+
expect(charsToTokens(NaN)).toBe(0)
|
|
10
|
+
expect(charsToTokens(Infinity)).toBe(0)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('derives the telemetry token figure from characters', () => {
|
|
14
|
+
expect(charsToTokens(1)).toBe(1)
|
|
15
|
+
expect(charsToTokens(4)).toBe(1)
|
|
16
|
+
expect(charsToTokens(5)).toBe(1)
|
|
17
|
+
expect(charsToTokens(8)).toBe(2)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('expresses token-named gates on the character basis', () => {
|
|
21
|
+
expect(charsForTokens(0)).toBe(0)
|
|
22
|
+
expect(charsForTokens(1000)).toBe(4000)
|
|
23
|
+
expect(CHARS_PER_TOKEN).toBe(4.0)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('stays finite at the integer upper bound (off/native gates must not misfire)', () => {
|
|
27
|
+
expect(Number.isFinite(charsForTokens(Number.MAX_SAFE_INTEGER))).toBe(true)
|
|
28
|
+
expect(charsForTokens(Number.MAX_SAFE_INTEGER)).toBeLessThan(Infinity)
|
|
29
|
+
})
|
|
30
|
+
})
|