dsh-context-compression-improved 0.6.5 → 0.7.0-beta.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.
Files changed (67) hide show
  1. package/CHANGELOG.ja.md +9 -0
  2. package/CHANGELOG.ko.md +9 -0
  3. package/CHANGELOG.md +26 -0
  4. package/CHANGELOG.zh.md +9 -0
  5. package/README.md +1 -0
  6. package/README.zh.md +1 -0
  7. package/eslint.config.js +76 -30
  8. package/package.json +18 -17
  9. package/packages/selector/dsh.plugin.json +1 -1
  10. package/packages/selector/lib/client.js +14 -3
  11. package/packages/selector/lib/index.js +92 -1
  12. package/packages/selector/lib/{advisor-state.js → intent-gate.js} +91 -3
  13. package/packages/selector/lib/profiles.d.ts +9 -0
  14. package/packages/selector/lib/pruner.d.ts +59 -0
  15. package/packages/selector/lib/pruner.js +410 -8
  16. package/packages/selector/lib/style.css +46 -46
  17. package/packages/selector/src/client/decode.ts +6 -2
  18. package/packages/selector/src/deepseek-v4-tokenizer.ts +1 -1
  19. package/packages/selector/src/index.ts +418 -411
  20. package/packages/selector/src/preset-overlay.ts +3 -0
  21. package/packages/selector/src/profiles.ts +30 -10
  22. package/packages/selector/src/pruner/content.ts +1 -1
  23. package/packages/selector/src/pruner/state.ts +7 -0
  24. package/packages/selector/src/pruner.ts +253 -9
  25. package/packages/selector/src/runtime/audit.ts +17 -0
  26. package/packages/selector/src/runtime/config.ts +36 -11
  27. package/packages/selector/src/runtime/reducers.ts +1 -1
  28. package/packages/selector/src/runtime/summary-command.ts +133 -0
  29. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +46 -1
  30. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +55 -0
  31. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  32. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  33. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  34. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  35. package/packages/selector/src/runtime/tokenpilot/locator.ts +2 -2
  36. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +4 -1
  37. package/packages/selector/src/runtime/types.ts +12 -0
  38. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
  39. package/packages/selector/tests/built/client-artifact.spec.ts +2 -0
  40. package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
  41. package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
  42. package/packages/selector/tests/estimator-channel.client.spec.tsx +252 -252
  43. package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -15
  44. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -41
  45. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -647
  46. package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -78
  47. package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -234
  48. package/packages/selector/tests/host-preset-overlay.host.spec.ts +203 -203
  49. package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
  50. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  51. package/packages/selector/tests/runtime/audit.spec.ts +275 -274
  52. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +3 -3
  53. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +4 -4
  54. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  55. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  56. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +5 -0
  57. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  58. package/packages/selector/tests/standing-generation.host.spec.ts +15 -35
  59. package/scripts/packed-components-smoke.mjs +2 -0
  60. package/scripts/packed-install-e2e.mjs +1275 -1273
  61. package/scripts/verify-release.mjs +318 -317
  62. package/tsconfig.tests.json +3 -1
  63. package/vitest.config.ts +14 -0
  64. package/.agents-stash-064.patch +0 -39
  65. package/_dsh_devdeps.json +0 -50
  66. package/_verify064/package/package.json +0 -162
  67. package/_verify064/package/packages/selector/src/client/index.ts +0 -232
@@ -1,204 +1,204 @@
1
- import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
2
- import { tmpdir } from 'node:os'
3
- import { dirname, join } from 'node:path'
4
- import { Context, Service } from '@deepseek-ai/cordis'
5
- import type { AgentPreset } from '@deepseek-ai/dsh-agent-presets'
1
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
2
+ import { tmpdir } from 'node:os'
3
+ import { dirname, join } from 'node:path'
4
+ import { Context, Service } from '@deepseek-ai/cordis'
5
+ import type { AgentPreset } from '@deepseek-ai/dsh-agent-presets'
6
6
  import { type SettingsNamespace } from '@deepseek-ai/dsh-settings'
7
- import { LegacySettingsProvider } from './helpers/legacy-settings/index.ts'
8
- import { afterEach, describe, expect, it } from 'vitest'
9
- import { apply } from '../src/index.ts'
10
- import type { OverlayableAgentPresets } from '../src/preset-overlay.ts'
11
- import { LegacyNamespaceRegistrar } from './helpers/legacy-namespace.ts'
12
-
13
- // 0.1.5 removed the settingsNamespace() wrapper; namespaces are validated at runtime.
14
- const nsBrand = (value: string): SettingsNamespace => value as unknown as SettingsNamespace
15
-
16
- let root: string | undefined
17
- let ctx: Context | undefined
18
-
19
- afterEach(async () => {
20
- await ctx?.fiber.dispose()
21
- ctx = undefined
22
- if (root !== undefined) await rm(root, { recursive: true, force: true })
23
- root = undefined
24
- })
25
-
26
- class MemorySettings extends LegacySettingsProvider {
27
- readonly writable = true
28
- private readonly stored: Record<string, unknown> = {}
29
-
30
- protected load(): Promise<Record<string, unknown>> {
31
- return Promise.resolve(structuredClone(this.stored))
32
- }
33
-
34
- protected persist(ns: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
35
- this.stored[ns] = structuredClone(section)
36
- return Promise.resolve()
37
- }
38
- }
39
-
40
- class FakeAgentPresets extends Service implements OverlayableAgentPresets {
41
- constructor(ctx: Context, readonly preset: AgentPreset) {
42
- super(ctx, 'agentPresets')
43
- }
44
-
45
- async resolve(): Promise<AgentPreset> {
46
- return this.preset
47
- }
48
-
49
- async mount(_agentCtx: unknown): Promise<AgentPreset> {
50
- return await this.resolve()
51
- }
52
-
53
- async recompose(_agentCtx: unknown, _id: string): Promise<AgentPreset> {
54
- return await this.resolve()
55
- }
56
-
57
- async standingKeyFor(): Promise<string> {
58
- return (await this.resolve()).path
59
- }
60
- }
61
-
62
- async function sourcePreset(): Promise<AgentPreset> {
63
- root = await mkdtemp(join(tmpdir(), 'dsh-selector-host-overlay-'))
64
- const path = join(root, 'standard', 'agent.cordis.yml')
65
- await mkdir(dirname(path), { recursive: true })
66
- await writeFile(path, "- id: persona\n name: '/opt/preset/persona.js'\n")
67
- return { id: 'standard', trust: 'system', path }
68
- }
69
-
70
- describe('context compression selector Host preset integration', () => {
71
- it('decorates native composition only for the lifetime of the selector fiber', async () => {
72
- const preset = await sourcePreset()
73
- ctx = new Context()
74
- await ctx.plugin(MemorySettings).await()
75
- await ctx.plugin(LegacyNamespaceRegistrar).await()
76
- await ctx.plugin(FakeAgentPresets, preset).await()
77
-
78
- const selector = ctx.plugin({ apply: (child) => {
79
- apply(child, { presetOverlay: true })
80
- } })
81
- await selector.await()
82
-
83
- const service = ctx.agentPresets as unknown as OverlayableAgentPresets
84
- const mounted = await service.mount({})
85
- expect(mounted.path).not.toBe(preset.path)
86
- expect(await readFile(mounted.path, 'utf8')).toContain('id: tool-result-pruner')
87
- expect((await service.resolve()).path).toBe(preset.path)
88
-
89
- await selector.dispose()
90
- expect((await service.mount({})).path).toBe(preset.path)
91
- })
92
-
93
- it('0.1.7: duplicate Host rows own independent entry configs — one row\u2019s disposal never disturbs the other', async () => {
94
- const preset = await sourcePreset()
95
- ctx = new Context()
96
- await ctx.plugin(MemorySettings).await()
97
- await ctx.plugin(FakeAgentPresets, preset).await()
98
-
99
- const builtIn = ctx.plugin({ apply })
100
- await builtIn.await()
101
-
102
- const service = ctx.agentPresets as unknown as OverlayableAgentPresets
103
- expect((await service.mount({})).path).toBe(preset.path)
104
-
105
- // 0.1.7: there is no shared settings registration to lease — each row
106
- // carries its own compression doc on its entry config, so duplicate rows
107
- // cannot disturb each other and disposal is purely per row.
108
- const bundle = ctx.plugin({ apply: (child) => {
109
- apply(child, { presetOverlay: true, settings: { profile: 'cache-strict' } } as never)
110
- } })
111
- await bundle.await()
112
-
113
- const mounted = await service.mount({})
114
- expect(mounted.path).not.toBe(preset.path)
115
- expect((await service.recompose({}, 'standard')).path).toBe(mounted.path)
116
-
117
- // Disposing the non-overlay row never touches the overlay row's decoration
118
- // or its entry config.
119
- await builtIn.dispose()
120
- expect((await service.mount({})).path).toBe(mounted.path)
121
-
122
- await bundle.dispose()
123
- expect((await service.mount({})).path).toBe(preset.path)
124
- })
125
-
126
- it('writes the saved Auto Compact threshold into the generated compaction-basic composition', async () => {
127
- const preset = await sourcePreset()
128
- ctx = new Context()
129
- await ctx.plugin(MemorySettings).await()
130
- await ctx.plugin(LegacyNamespaceRegistrar).await()
131
- await ctx.plugin(FakeAgentPresets, preset).await()
132
- // A settings-owning row must register the namespace before the update.
133
- await ctx.plugin({ apply }).await()
134
- const namespace = nsBrand('context-compression')
135
- await ctx.settings.update(namespace, { autoCompact: { thresholdPercent: 73 } })
136
-
137
- const bundle = ctx.plugin({ apply: (child) => {
138
- apply(child, { presetOverlay: true })
139
- } })
140
- await bundle.await()
141
-
142
- const service = ctx.agentPresets as unknown as OverlayableAgentPresets
143
- const mounted = await service.mount({})
144
- const rendered = await readFile(mounted.path, 'utf8')
145
- expect(rendered).toContain('id: compaction-basic')
146
- expect(rendered).toContain('thresholdRatio: 0.73')
147
- // First-release retention stays pinned at 0.16 next to the threshold.
148
- expect(rendered).toContain('retainRatio: 0.16')
149
- // The SAME read feeds the runtime deployment config, so one generation
150
- // cannot split Auto Compact and micro compact across two thresholds.
151
- expect(rendered).toContain('id: tool-result-pruner')
152
- expect(rendered).toContain('autoCompactThresholdPercent: 73')
153
- await bundle.dispose()
154
- })
155
-
156
- it('defaults the generated threshold to 0.8 and keeps it absent from settings until saved', async () => {
157
- const preset = await sourcePreset()
158
- ctx = new Context()
159
- await ctx.plugin(MemorySettings).await()
160
- await ctx.plugin(LegacyNamespaceRegistrar).await()
161
- await ctx.plugin(FakeAgentPresets, preset).await()
162
-
163
- const bundle = ctx.plugin({ apply: (child) => {
164
- apply(child, { presetOverlay: true })
165
- } })
166
- await bundle.await()
167
-
168
- const service = ctx.agentPresets as unknown as OverlayableAgentPresets
169
- const rendered = await readFile((await service.mount({})).path, 'utf8')
170
- expect(rendered).toContain('thresholdRatio: 0.8')
171
- await bundle.dispose()
172
- })
173
-
174
- it('moves the standing composition generation when an equal-length threshold changes', async () => {
175
- const preset = await sourcePreset()
176
- ctx = new Context()
177
- await ctx.plugin(MemorySettings).await()
178
- await ctx.plugin(LegacyNamespaceRegistrar).await()
179
- await ctx.plugin(FakeAgentPresets, preset).await()
180
- // A settings-owning row must register the namespace before the update.
181
- await ctx.plugin({ apply }).await()
182
- const namespace = nsBrand('context-compression')
183
- await ctx.settings.update(namespace, { autoCompact: { thresholdPercent: 70 } })
184
-
185
- const bundle = ctx.plugin({ apply: (child) => {
186
- apply(child, { presetOverlay: true })
187
- } })
188
- await bundle.await()
189
-
190
- const service = ctx.agentPresets as unknown as OverlayableAgentPresets
191
- const first = await service.mount({})
192
- expect(await readFile(first.path, 'utf8')).toContain('thresholdRatio: 0.7')
193
- expect(await service.standingKeyFor()).toBe(first.path)
194
-
195
- // Same character length, different content: the generated identity and
196
- // standing composition generation must both move to the new threshold.
197
- await ctx.settings.update(namespace, { autoCompact: { thresholdPercent: 80 } })
198
- const second = await service.mount({})
199
- expect(second.path).not.toBe(first.path)
200
- expect(await readFile(second.path, 'utf8')).toContain('thresholdRatio: 0.8')
201
- expect(await service.standingKeyFor()).toBe(second.path)
202
- await bundle.dispose()
203
- })
204
- })
7
+ import { LegacySettingsProvider } from './helpers/legacy-settings/index.ts'
8
+ import { afterEach, describe, expect, it } from 'vitest'
9
+ import { apply } from '../src/index.ts'
10
+ import type { OverlayableAgentPresets } from '../src/preset-overlay.ts'
11
+ import { LegacyNamespaceRegistrar } from './helpers/legacy-namespace.ts'
12
+
13
+ // 0.1.5 removed the settingsNamespace() wrapper; namespaces are validated at runtime.
14
+ const nsBrand = (value: string): SettingsNamespace => value as unknown as SettingsNamespace
15
+
16
+ let root: string | undefined
17
+ let ctx: Context | undefined
18
+
19
+ afterEach(async () => {
20
+ await ctx?.fiber.dispose()
21
+ ctx = undefined
22
+ if (root !== undefined) await rm(root, { recursive: true, force: true })
23
+ root = undefined
24
+ })
25
+
26
+ class MemorySettings extends LegacySettingsProvider {
27
+ readonly writable = true
28
+ private readonly stored: Record<string, unknown> = {}
29
+
30
+ protected load(): Promise<Record<string, unknown>> {
31
+ return Promise.resolve(structuredClone(this.stored))
32
+ }
33
+
34
+ protected persist(ns: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
35
+ this.stored[ns] = structuredClone(section)
36
+ return Promise.resolve()
37
+ }
38
+ }
39
+
40
+ class FakeAgentPresets extends Service implements OverlayableAgentPresets {
41
+ constructor(ctx: Context, readonly preset: AgentPreset) {
42
+ super(ctx, 'agentPresets')
43
+ }
44
+
45
+ async resolve(): Promise<AgentPreset> {
46
+ return this.preset
47
+ }
48
+
49
+ async mount(_agentCtx: unknown): Promise<AgentPreset> {
50
+ return await this.resolve()
51
+ }
52
+
53
+ async recompose(_agentCtx: unknown, _id: string): Promise<AgentPreset> {
54
+ return await this.resolve()
55
+ }
56
+
57
+ async standingKeyFor(): Promise<string> {
58
+ return (await this.resolve()).path
59
+ }
60
+ }
61
+
62
+ async function sourcePreset(): Promise<AgentPreset> {
63
+ root = await mkdtemp(join(tmpdir(), 'dsh-selector-host-overlay-'))
64
+ const path = join(root, 'standard', 'agent.cordis.yml')
65
+ await mkdir(dirname(path), { recursive: true })
66
+ await writeFile(path, "- id: persona\n name: '/opt/preset/persona.js'\n")
67
+ return { id: 'standard', trust: 'system', path }
68
+ }
69
+
70
+ describe('context compression selector Host preset integration', () => {
71
+ it('decorates native composition only for the lifetime of the selector fiber', async () => {
72
+ const preset = await sourcePreset()
73
+ ctx = new Context()
74
+ await ctx.plugin(MemorySettings).await()
75
+ await ctx.plugin(LegacyNamespaceRegistrar).await()
76
+ await ctx.plugin(FakeAgentPresets, preset).await()
77
+
78
+ const selector = ctx.plugin({ apply: (child) => {
79
+ apply(child, { presetOverlay: true })
80
+ } })
81
+ await selector.await()
82
+
83
+ const service = ctx.agentPresets as unknown as OverlayableAgentPresets
84
+ const mounted = await service.mount({})
85
+ expect(mounted.path).not.toBe(preset.path)
86
+ expect(await readFile(mounted.path, 'utf8')).toContain('id: tool-result-pruner')
87
+ expect((await service.resolve()).path).toBe(preset.path)
88
+
89
+ await selector.dispose()
90
+ expect((await service.mount({})).path).toBe(preset.path)
91
+ })
92
+
93
+ it('0.1.7: duplicate Host rows own independent entry configs — one row\u2019s disposal never disturbs the other', async () => {
94
+ const preset = await sourcePreset()
95
+ ctx = new Context()
96
+ await ctx.plugin(MemorySettings).await()
97
+ await ctx.plugin(FakeAgentPresets, preset).await()
98
+
99
+ const builtIn = ctx.plugin({ apply })
100
+ await builtIn.await()
101
+
102
+ const service = ctx.agentPresets as unknown as OverlayableAgentPresets
103
+ expect((await service.mount({})).path).toBe(preset.path)
104
+
105
+ // 0.1.7: there is no shared settings registration to lease — each row
106
+ // carries its own compression doc on its entry config, so duplicate rows
107
+ // cannot disturb each other and disposal is purely per row.
108
+ const bundle = ctx.plugin({ apply: (child) => {
109
+ apply(child, { presetOverlay: true, settings: { profile: 'cache-strict' } } as never)
110
+ } })
111
+ await bundle.await()
112
+
113
+ const mounted = await service.mount({})
114
+ expect(mounted.path).not.toBe(preset.path)
115
+ expect((await service.recompose({}, 'standard')).path).toBe(mounted.path)
116
+
117
+ // Disposing the non-overlay row never touches the overlay row's decoration
118
+ // or its entry config.
119
+ await builtIn.dispose()
120
+ expect((await service.mount({})).path).toBe(mounted.path)
121
+
122
+ await bundle.dispose()
123
+ expect((await service.mount({})).path).toBe(preset.path)
124
+ })
125
+
126
+ it('writes the saved Auto Compact threshold into the generated compaction-basic composition', async () => {
127
+ const preset = await sourcePreset()
128
+ ctx = new Context()
129
+ await ctx.plugin(MemorySettings).await()
130
+ await ctx.plugin(LegacyNamespaceRegistrar).await()
131
+ await ctx.plugin(FakeAgentPresets, preset).await()
132
+ // A settings-owning row must register the namespace before the update.
133
+ await ctx.plugin({ apply }).await()
134
+ const namespace = nsBrand('context-compression')
135
+ await ctx.settings.update(namespace, { autoCompact: { thresholdPercent: 73 } })
136
+
137
+ const bundle = ctx.plugin({ apply: (child) => {
138
+ apply(child, { presetOverlay: true })
139
+ } })
140
+ await bundle.await()
141
+
142
+ const service = ctx.agentPresets as unknown as OverlayableAgentPresets
143
+ const mounted = await service.mount({})
144
+ const rendered = await readFile(mounted.path, 'utf8')
145
+ expect(rendered).toContain('id: compaction-basic')
146
+ expect(rendered).toContain('thresholdRatio: 0.73')
147
+ // First-release retention stays pinned at 0.16 next to the threshold.
148
+ expect(rendered).toContain('retainRatio: 0.16')
149
+ // The SAME read feeds the runtime deployment config, so one generation
150
+ // cannot split Auto Compact and micro compact across two thresholds.
151
+ expect(rendered).toContain('id: tool-result-pruner')
152
+ expect(rendered).toContain('autoCompactThresholdPercent: 73')
153
+ await bundle.dispose()
154
+ })
155
+
156
+ it('defaults the generated threshold to 0.8 and keeps it absent from settings until saved', async () => {
157
+ const preset = await sourcePreset()
158
+ ctx = new Context()
159
+ await ctx.plugin(MemorySettings).await()
160
+ await ctx.plugin(LegacyNamespaceRegistrar).await()
161
+ await ctx.plugin(FakeAgentPresets, preset).await()
162
+
163
+ const bundle = ctx.plugin({ apply: (child) => {
164
+ apply(child, { presetOverlay: true })
165
+ } })
166
+ await bundle.await()
167
+
168
+ const service = ctx.agentPresets as unknown as OverlayableAgentPresets
169
+ const rendered = await readFile((await service.mount({})).path, 'utf8')
170
+ expect(rendered).toContain('thresholdRatio: 0.8')
171
+ await bundle.dispose()
172
+ })
173
+
174
+ it('moves the standing composition generation when an equal-length threshold changes', async () => {
175
+ const preset = await sourcePreset()
176
+ ctx = new Context()
177
+ await ctx.plugin(MemorySettings).await()
178
+ await ctx.plugin(LegacyNamespaceRegistrar).await()
179
+ await ctx.plugin(FakeAgentPresets, preset).await()
180
+ // A settings-owning row must register the namespace before the update.
181
+ await ctx.plugin({ apply }).await()
182
+ const namespace = nsBrand('context-compression')
183
+ await ctx.settings.update(namespace, { autoCompact: { thresholdPercent: 70 } })
184
+
185
+ const bundle = ctx.plugin({ apply: (child) => {
186
+ apply(child, { presetOverlay: true })
187
+ } })
188
+ await bundle.await()
189
+
190
+ const service = ctx.agentPresets as unknown as OverlayableAgentPresets
191
+ const first = await service.mount({})
192
+ expect(await readFile(first.path, 'utf8')).toContain('thresholdRatio: 0.7')
193
+ expect(await service.standingKeyFor()).toBe(first.path)
194
+
195
+ // Same character length, different content: the generated identity and
196
+ // standing composition generation must both move to the new threshold.
197
+ await ctx.settings.update(namespace, { autoCompact: { thresholdPercent: 80 } })
198
+ const second = await service.mount({})
199
+ expect(second.path).not.toBe(first.path)
200
+ expect(await readFile(second.path, 'utf8')).toContain('thresholdRatio: 0.8')
201
+ expect(await service.standingKeyFor()).toBe(second.path)
202
+ await bundle.dispose()
203
+ })
204
+ })