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